1 /******************************************************************************
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
8 * Copyright(c) 2007 - 2014 Intel Corporation. All rights reserved.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
24 * The full GNU General Public License is included in this distribution
25 * in the file called COPYING.
27 * Contact Information:
28 * Intel Linux Wireless <ilw@linux.intel.com>
29 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
33 * Copyright(c) 2005 - 2014 Intel Corporation. All rights reserved.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
40 * * Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * * Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in
44 * the documentation and/or other materials provided with the
46 * * Neither the name Intel Corporation nor the names of its
47 * contributors may be used to endorse or promote products derived
48 * from this software without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62 *****************************************************************************/
63 #include <linux/completion.h>
64 #include <linux/dma-mapping.h>
65 #include <linux/firmware.h>
66 #include <linux/module.h>
67 #include <linux/vmalloc.h>
70 #include "iwl-debug.h"
71 #include "iwl-trans.h"
72 #include "iwl-op-mode.h"
73 #include "iwl-agn-hw.h"
75 #include "iwl-config.h"
76 #include "iwl-modparams.h"
78 /* private includes */
79 #include "iwl-fw-file.h"
81 /******************************************************************************
85 ******************************************************************************/
88 * module name, copyright, version, etc.
90 #define DRV_DESCRIPTION "Intel(R) Wireless WiFi driver for Linux"
92 #ifdef CONFIG_IWLWIFI_DEBUG
98 #define DRV_VERSION IWLWIFI_VERSION VD
100 MODULE_DESCRIPTION(DRV_DESCRIPTION);
101 MODULE_VERSION(DRV_VERSION);
102 MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
103 MODULE_LICENSE("GPL");
105 #ifdef CONFIG_IWLWIFI_DEBUGFS
106 static struct dentry *iwl_dbgfs_root;
110 * struct iwl_drv - drv common data
111 * @list: list of drv structures using this opmode
112 * @fw: the iwl_fw structure
113 * @op_mode: the running op_mode
114 * @trans: transport layer
115 * @dev: for debug prints only
116 * @cfg: configuration struct
117 * @fw_index: firmware revision to try loading
118 * @firmware_name: composite filename of ucode file to load
119 * @request_firmware_complete: the firmware has been obtained from user space
122 struct list_head list;
125 struct iwl_op_mode *op_mode;
126 struct iwl_trans *trans;
128 const struct iwl_cfg *cfg;
130 int fw_index; /* firmware we're trying to load */
131 char firmware_name[32]; /* name of firmware file to load */
133 struct completion request_firmware_complete;
135 #ifdef CONFIG_IWLWIFI_DEBUGFS
136 struct dentry *dbgfs_drv;
137 struct dentry *dbgfs_trans;
138 struct dentry *dbgfs_op_mode;
147 /* Protects the table contents, i.e. the ops pointer & drv list */
148 static struct mutex iwlwifi_opmode_table_mtx;
149 static struct iwlwifi_opmode_table {
150 const char *name; /* name: iwldvm, iwlmvm, etc */
151 const struct iwl_op_mode_ops *ops; /* pointer to op_mode ops */
152 struct list_head drv; /* list of devices using this op_mode */
153 } iwlwifi_opmode_table[] = { /* ops set when driver is initialized */
154 [DVM_OP_MODE] = { .name = "iwldvm", .ops = NULL },
155 [MVM_OP_MODE] = { .name = "iwlmvm", .ops = NULL },
159 * struct fw_sec: Just for the image parsing proccess.
160 * For the fw storage we are using struct fw_desc.
163 const void *data; /* the sec data */
164 size_t size; /* section size */
165 u32 offset; /* offset of writing in the device */
168 static void iwl_free_fw_desc(struct iwl_drv *drv, struct fw_desc *desc)
175 static void iwl_free_fw_img(struct iwl_drv *drv, struct fw_img *img)
178 for (i = 0; i < IWL_UCODE_SECTION_MAX; i++)
179 iwl_free_fw_desc(drv, &img->sec[i]);
182 static void iwl_dealloc_ucode(struct iwl_drv *drv)
185 for (i = 0; i < IWL_UCODE_TYPE_MAX; i++)
186 iwl_free_fw_img(drv, drv->fw.img + i);
189 static int iwl_alloc_fw_desc(struct iwl_drv *drv, struct fw_desc *desc,
196 if (!sec || !sec->size)
199 data = vmalloc(sec->size);
203 desc->len = sec->size;
204 desc->offset = sec->offset;
205 memcpy(data, sec->data, desc->len);
211 static void iwl_req_fw_callback(const struct firmware *ucode_raw,
214 #define UCODE_EXPERIMENTAL_INDEX 100
215 #define UCODE_EXPERIMENTAL_TAG "exp"
217 static int iwl_request_firmware(struct iwl_drv *drv, bool first)
219 const char *name_pre = drv->cfg->fw_name_pre;
223 #ifdef CONFIG_IWLWIFI_DEBUG_EXPERIMENTAL_UCODE
224 drv->fw_index = UCODE_EXPERIMENTAL_INDEX;
225 strcpy(tag, UCODE_EXPERIMENTAL_TAG);
226 } else if (drv->fw_index == UCODE_EXPERIMENTAL_INDEX) {
228 drv->fw_index = drv->cfg->ucode_api_max;
229 sprintf(tag, "%d", drv->fw_index);
232 sprintf(tag, "%d", drv->fw_index);
235 if (drv->fw_index < drv->cfg->ucode_api_min) {
236 IWL_ERR(drv, "no suitable firmware found!\n");
240 snprintf(drv->firmware_name, sizeof(drv->firmware_name), "%s%s.ucode",
243 IWL_DEBUG_INFO(drv, "attempting to load firmware %s'%s'\n",
244 (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
245 ? "EXPERIMENTAL " : "",
248 return request_firmware_nowait(THIS_MODULE, 1, drv->firmware_name,
250 GFP_KERNEL, drv, iwl_req_fw_callback);
253 struct fw_img_parsing {
254 struct fw_sec sec[IWL_UCODE_SECTION_MAX];
259 * struct fw_sec_parsing: to extract fw section and it's offset from tlv
261 struct fw_sec_parsing {
267 * struct iwl_tlv_calib_data - parse the default calib data from TLV
269 * @ucode_type: the uCode to which the following default calib relates.
270 * @calib: default calibrations.
272 struct iwl_tlv_calib_data {
274 struct iwl_tlv_calib_ctrl calib;
277 struct iwl_firmware_pieces {
278 struct fw_img_parsing img[IWL_UCODE_TYPE_MAX];
280 u32 init_evtlog_ptr, init_evtlog_size, init_errlog_ptr;
281 u32 inst_evtlog_ptr, inst_evtlog_size, inst_errlog_ptr;
285 * These functions are just to extract uCode section data from the pieces
288 static struct fw_sec *get_sec(struct iwl_firmware_pieces *pieces,
289 enum iwl_ucode_type type,
292 return &pieces->img[type].sec[sec];
295 static void set_sec_data(struct iwl_firmware_pieces *pieces,
296 enum iwl_ucode_type type,
300 pieces->img[type].sec[sec].data = data;
303 static void set_sec_size(struct iwl_firmware_pieces *pieces,
304 enum iwl_ucode_type type,
308 pieces->img[type].sec[sec].size = size;
311 static size_t get_sec_size(struct iwl_firmware_pieces *pieces,
312 enum iwl_ucode_type type,
315 return pieces->img[type].sec[sec].size;
318 static void set_sec_offset(struct iwl_firmware_pieces *pieces,
319 enum iwl_ucode_type type,
323 pieces->img[type].sec[sec].offset = offset;
326 static int iwl_store_cscheme(struct iwl_fw *fw, const u8 *data, const u32 len)
329 struct iwl_fw_cscheme_list *l = (struct iwl_fw_cscheme_list *)data;
330 struct iwl_fw_cipher_scheme *fwcs;
331 struct ieee80211_cipher_scheme *cs;
334 if (len < sizeof(*l) ||
335 len < sizeof(l->size) + l->size * sizeof(l->cs[0]))
338 for (i = 0, j = 0; i < IWL_UCODE_MAX_CS && i < l->size; i++) {
340 cipher = le32_to_cpu(fwcs->cipher);
342 /* we skip schemes with zero cipher suite selector */
348 cs->iftype = BIT(NL80211_IFTYPE_STATION);
349 cs->hdr_len = fwcs->hdr_len;
350 cs->pn_len = fwcs->pn_len;
351 cs->pn_off = fwcs->pn_off;
352 cs->key_idx_off = fwcs->key_idx_off;
353 cs->key_idx_mask = fwcs->key_idx_mask;
354 cs->key_idx_shift = fwcs->key_idx_shift;
355 cs->mic_len = fwcs->mic_len;
362 * Gets uCode section from tlv.
364 static int iwl_store_ucode_sec(struct iwl_firmware_pieces *pieces,
365 const void *data, enum iwl_ucode_type type,
368 struct fw_img_parsing *img;
370 struct fw_sec_parsing *sec_parse;
372 if (WARN_ON(!pieces || !data || type >= IWL_UCODE_TYPE_MAX))
375 sec_parse = (struct fw_sec_parsing *)data;
377 img = &pieces->img[type];
378 sec = &img->sec[img->sec_counter];
380 sec->offset = le32_to_cpu(sec_parse->offset);
381 sec->data = sec_parse->data;
382 sec->size = size - sizeof(sec_parse->offset);
389 static int iwl_set_default_calib(struct iwl_drv *drv, const u8 *data)
391 struct iwl_tlv_calib_data *def_calib =
392 (struct iwl_tlv_calib_data *)data;
393 u32 ucode_type = le32_to_cpu(def_calib->ucode_type);
394 if (ucode_type >= IWL_UCODE_TYPE_MAX) {
395 IWL_ERR(drv, "Wrong ucode_type %u for default calibration.\n",
399 drv->fw.default_calib[ucode_type].flow_trigger =
400 def_calib->calib.flow_trigger;
401 drv->fw.default_calib[ucode_type].event_trigger =
402 def_calib->calib.event_trigger;
407 static int iwl_parse_v1_v2_firmware(struct iwl_drv *drv,
408 const struct firmware *ucode_raw,
409 struct iwl_firmware_pieces *pieces)
411 struct iwl_ucode_header *ucode = (void *)ucode_raw->data;
412 u32 api_ver, hdr_size, build;
416 drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
417 api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
422 if (ucode_raw->size < hdr_size) {
423 IWL_ERR(drv, "File size too small!\n");
426 build = le32_to_cpu(ucode->u.v2.build);
427 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
428 le32_to_cpu(ucode->u.v2.inst_size));
429 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
430 le32_to_cpu(ucode->u.v2.data_size));
431 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
432 le32_to_cpu(ucode->u.v2.init_size));
433 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
434 le32_to_cpu(ucode->u.v2.init_data_size));
435 src = ucode->u.v2.data;
441 if (ucode_raw->size < hdr_size) {
442 IWL_ERR(drv, "File size too small!\n");
446 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
447 le32_to_cpu(ucode->u.v1.inst_size));
448 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
449 le32_to_cpu(ucode->u.v1.data_size));
450 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
451 le32_to_cpu(ucode->u.v1.init_size));
452 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
453 le32_to_cpu(ucode->u.v1.init_data_size));
454 src = ucode->u.v1.data;
459 sprintf(buildstr, " build %u%s", build,
460 (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
465 snprintf(drv->fw.fw_version,
466 sizeof(drv->fw.fw_version),
468 IWL_UCODE_MAJOR(drv->fw.ucode_ver),
469 IWL_UCODE_MINOR(drv->fw.ucode_ver),
470 IWL_UCODE_API(drv->fw.ucode_ver),
471 IWL_UCODE_SERIAL(drv->fw.ucode_ver),
474 /* Verify size of file vs. image size info in file's header */
476 if (ucode_raw->size != hdr_size +
477 get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) +
478 get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) +
479 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) +
480 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA)) {
483 "uCode file size %d does not match expected size\n",
484 (int)ucode_raw->size);
489 set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST, src);
490 src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST);
491 set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
492 IWLAGN_RTC_INST_LOWER_BOUND);
493 set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA, src);
494 src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA);
495 set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
496 IWLAGN_RTC_DATA_LOWER_BOUND);
497 set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST, src);
498 src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST);
499 set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
500 IWLAGN_RTC_INST_LOWER_BOUND);
501 set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA, src);
502 src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA);
503 set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
504 IWLAGN_RTC_DATA_LOWER_BOUND);
508 static int iwl_parse_tlv_firmware(struct iwl_drv *drv,
509 const struct firmware *ucode_raw,
510 struct iwl_firmware_pieces *pieces,
511 struct iwl_ucode_capabilities *capa)
513 struct iwl_tlv_ucode_header *ucode = (void *)ucode_raw->data;
514 struct iwl_ucode_tlv *tlv;
515 size_t len = ucode_raw->size;
518 enum iwl_ucode_tlv_type tlv_type;
524 if (len < sizeof(*ucode)) {
525 IWL_ERR(drv, "uCode has invalid length: %zd\n", len);
529 if (ucode->magic != cpu_to_le32(IWL_TLV_UCODE_MAGIC)) {
530 IWL_ERR(drv, "invalid uCode magic: 0X%x\n",
531 le32_to_cpu(ucode->magic));
535 drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
536 build = le32_to_cpu(ucode->build);
539 sprintf(buildstr, " build %u%s", build,
540 (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
545 snprintf(drv->fw.fw_version,
546 sizeof(drv->fw.fw_version),
548 IWL_UCODE_MAJOR(drv->fw.ucode_ver),
549 IWL_UCODE_MINOR(drv->fw.ucode_ver),
550 IWL_UCODE_API(drv->fw.ucode_ver),
551 IWL_UCODE_SERIAL(drv->fw.ucode_ver),
556 len -= sizeof(*ucode);
558 while (len >= sizeof(*tlv)) {
562 tlv_len = le32_to_cpu(tlv->length);
563 tlv_type = le32_to_cpu(tlv->type);
564 tlv_data = tlv->data;
567 IWL_ERR(drv, "invalid TLV len: %zd/%u\n",
571 len -= ALIGN(tlv_len, 4);
572 data += sizeof(*tlv) + ALIGN(tlv_len, 4);
575 case IWL_UCODE_TLV_INST:
576 set_sec_data(pieces, IWL_UCODE_REGULAR,
577 IWL_UCODE_SECTION_INST, tlv_data);
578 set_sec_size(pieces, IWL_UCODE_REGULAR,
579 IWL_UCODE_SECTION_INST, tlv_len);
580 set_sec_offset(pieces, IWL_UCODE_REGULAR,
581 IWL_UCODE_SECTION_INST,
582 IWLAGN_RTC_INST_LOWER_BOUND);
584 case IWL_UCODE_TLV_DATA:
585 set_sec_data(pieces, IWL_UCODE_REGULAR,
586 IWL_UCODE_SECTION_DATA, tlv_data);
587 set_sec_size(pieces, IWL_UCODE_REGULAR,
588 IWL_UCODE_SECTION_DATA, tlv_len);
589 set_sec_offset(pieces, IWL_UCODE_REGULAR,
590 IWL_UCODE_SECTION_DATA,
591 IWLAGN_RTC_DATA_LOWER_BOUND);
593 case IWL_UCODE_TLV_INIT:
594 set_sec_data(pieces, IWL_UCODE_INIT,
595 IWL_UCODE_SECTION_INST, tlv_data);
596 set_sec_size(pieces, IWL_UCODE_INIT,
597 IWL_UCODE_SECTION_INST, tlv_len);
598 set_sec_offset(pieces, IWL_UCODE_INIT,
599 IWL_UCODE_SECTION_INST,
600 IWLAGN_RTC_INST_LOWER_BOUND);
602 case IWL_UCODE_TLV_INIT_DATA:
603 set_sec_data(pieces, IWL_UCODE_INIT,
604 IWL_UCODE_SECTION_DATA, tlv_data);
605 set_sec_size(pieces, IWL_UCODE_INIT,
606 IWL_UCODE_SECTION_DATA, tlv_len);
607 set_sec_offset(pieces, IWL_UCODE_INIT,
608 IWL_UCODE_SECTION_DATA,
609 IWLAGN_RTC_DATA_LOWER_BOUND);
611 case IWL_UCODE_TLV_BOOT:
612 IWL_ERR(drv, "Found unexpected BOOT ucode\n");
614 case IWL_UCODE_TLV_PROBE_MAX_LEN:
615 if (tlv_len != sizeof(u32))
616 goto invalid_tlv_len;
617 capa->max_probe_length =
618 le32_to_cpup((__le32 *)tlv_data);
620 case IWL_UCODE_TLV_PAN:
622 goto invalid_tlv_len;
623 capa->flags |= IWL_UCODE_TLV_FLAGS_PAN;
625 case IWL_UCODE_TLV_FLAGS:
626 /* must be at least one u32 */
627 if (tlv_len < sizeof(u32))
628 goto invalid_tlv_len;
629 /* and a proper number of u32s */
630 if (tlv_len % sizeof(u32))
631 goto invalid_tlv_len;
633 * This driver only reads the first u32 as
634 * right now no more features are defined,
635 * if that changes then either the driver
636 * will not work with the new firmware, or
637 * it'll not take advantage of new features.
639 capa->flags = le32_to_cpup((__le32 *)tlv_data);
641 case IWL_UCODE_TLV_INIT_EVTLOG_PTR:
642 if (tlv_len != sizeof(u32))
643 goto invalid_tlv_len;
644 pieces->init_evtlog_ptr =
645 le32_to_cpup((__le32 *)tlv_data);
647 case IWL_UCODE_TLV_INIT_EVTLOG_SIZE:
648 if (tlv_len != sizeof(u32))
649 goto invalid_tlv_len;
650 pieces->init_evtlog_size =
651 le32_to_cpup((__le32 *)tlv_data);
653 case IWL_UCODE_TLV_INIT_ERRLOG_PTR:
654 if (tlv_len != sizeof(u32))
655 goto invalid_tlv_len;
656 pieces->init_errlog_ptr =
657 le32_to_cpup((__le32 *)tlv_data);
659 case IWL_UCODE_TLV_RUNT_EVTLOG_PTR:
660 if (tlv_len != sizeof(u32))
661 goto invalid_tlv_len;
662 pieces->inst_evtlog_ptr =
663 le32_to_cpup((__le32 *)tlv_data);
665 case IWL_UCODE_TLV_RUNT_EVTLOG_SIZE:
666 if (tlv_len != sizeof(u32))
667 goto invalid_tlv_len;
668 pieces->inst_evtlog_size =
669 le32_to_cpup((__le32 *)tlv_data);
671 case IWL_UCODE_TLV_RUNT_ERRLOG_PTR:
672 if (tlv_len != sizeof(u32))
673 goto invalid_tlv_len;
674 pieces->inst_errlog_ptr =
675 le32_to_cpup((__le32 *)tlv_data);
677 case IWL_UCODE_TLV_ENHANCE_SENS_TBL:
679 goto invalid_tlv_len;
680 drv->fw.enhance_sensitivity_table = true;
682 case IWL_UCODE_TLV_WOWLAN_INST:
683 set_sec_data(pieces, IWL_UCODE_WOWLAN,
684 IWL_UCODE_SECTION_INST, tlv_data);
685 set_sec_size(pieces, IWL_UCODE_WOWLAN,
686 IWL_UCODE_SECTION_INST, tlv_len);
687 set_sec_offset(pieces, IWL_UCODE_WOWLAN,
688 IWL_UCODE_SECTION_INST,
689 IWLAGN_RTC_INST_LOWER_BOUND);
691 case IWL_UCODE_TLV_WOWLAN_DATA:
692 set_sec_data(pieces, IWL_UCODE_WOWLAN,
693 IWL_UCODE_SECTION_DATA, tlv_data);
694 set_sec_size(pieces, IWL_UCODE_WOWLAN,
695 IWL_UCODE_SECTION_DATA, tlv_len);
696 set_sec_offset(pieces, IWL_UCODE_WOWLAN,
697 IWL_UCODE_SECTION_DATA,
698 IWLAGN_RTC_DATA_LOWER_BOUND);
700 case IWL_UCODE_TLV_PHY_CALIBRATION_SIZE:
701 if (tlv_len != sizeof(u32))
702 goto invalid_tlv_len;
703 capa->standard_phy_calibration_size =
704 le32_to_cpup((__le32 *)tlv_data);
706 case IWL_UCODE_TLV_SEC_RT:
707 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_REGULAR,
709 drv->fw.mvm_fw = true;
711 case IWL_UCODE_TLV_SEC_INIT:
712 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_INIT,
714 drv->fw.mvm_fw = true;
716 case IWL_UCODE_TLV_SEC_WOWLAN:
717 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_WOWLAN,
719 drv->fw.mvm_fw = true;
721 case IWL_UCODE_TLV_DEF_CALIB:
722 if (tlv_len != sizeof(struct iwl_tlv_calib_data))
723 goto invalid_tlv_len;
724 if (iwl_set_default_calib(drv, tlv_data))
727 case IWL_UCODE_TLV_PHY_SKU:
728 if (tlv_len != sizeof(u32))
729 goto invalid_tlv_len;
730 drv->fw.phy_config = le32_to_cpup((__le32 *)tlv_data);
731 drv->fw.valid_tx_ant = (drv->fw.phy_config &
732 FW_PHY_CFG_TX_CHAIN) >>
733 FW_PHY_CFG_TX_CHAIN_POS;
734 drv->fw.valid_rx_ant = (drv->fw.phy_config &
735 FW_PHY_CFG_RX_CHAIN) >>
736 FW_PHY_CFG_RX_CHAIN_POS;
738 case IWL_UCODE_TLV_SECURE_SEC_RT:
739 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_REGULAR,
741 drv->fw.mvm_fw = true;
742 drv->fw.img[IWL_UCODE_REGULAR].is_secure = true;
744 case IWL_UCODE_TLV_SECURE_SEC_INIT:
745 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_INIT,
747 drv->fw.mvm_fw = true;
748 drv->fw.img[IWL_UCODE_INIT].is_secure = true;
750 case IWL_UCODE_TLV_SECURE_SEC_WOWLAN:
751 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_WOWLAN,
753 drv->fw.mvm_fw = true;
754 drv->fw.img[IWL_UCODE_WOWLAN].is_secure = true;
756 case IWL_UCODE_TLV_NUM_OF_CPU:
757 if (tlv_len != sizeof(u32))
758 goto invalid_tlv_len;
760 le32_to_cpup((__le32 *)tlv_data);
762 if (num_of_cpus == 2) {
763 drv->fw.img[IWL_UCODE_REGULAR].is_dual_cpus =
765 drv->fw.img[IWL_UCODE_INIT].is_dual_cpus =
767 drv->fw.img[IWL_UCODE_WOWLAN].is_dual_cpus =
769 } else if ((num_of_cpus > 2) || (num_of_cpus < 1)) {
770 IWL_ERR(drv, "Driver support upto 2 CPUs\n");
774 case IWL_UCODE_TLV_CSCHEME:
775 if (iwl_store_cscheme(&drv->fw, tlv_data, tlv_len))
776 goto invalid_tlv_len;
779 IWL_DEBUG_INFO(drv, "unknown TLV: %d\n", tlv_type);
785 IWL_ERR(drv, "invalid TLV after parsing: %zd\n", len);
786 iwl_print_hex_dump(drv, IWL_DL_FW, (u8 *)data, len);
793 IWL_ERR(drv, "TLV %d has invalid size: %u\n", tlv_type, tlv_len);
795 iwl_print_hex_dump(drv, IWL_DL_FW, tlv_data, tlv_len);
800 static int iwl_alloc_ucode(struct iwl_drv *drv,
801 struct iwl_firmware_pieces *pieces,
802 enum iwl_ucode_type type)
806 i < IWL_UCODE_SECTION_MAX && get_sec_size(pieces, type, i);
808 if (iwl_alloc_fw_desc(drv, &(drv->fw.img[type].sec[i]),
809 get_sec(pieces, type, i)))
814 static int validate_sec_sizes(struct iwl_drv *drv,
815 struct iwl_firmware_pieces *pieces,
816 const struct iwl_cfg *cfg)
818 IWL_DEBUG_INFO(drv, "f/w package hdr runtime inst size = %Zd\n",
819 get_sec_size(pieces, IWL_UCODE_REGULAR,
820 IWL_UCODE_SECTION_INST));
821 IWL_DEBUG_INFO(drv, "f/w package hdr runtime data size = %Zd\n",
822 get_sec_size(pieces, IWL_UCODE_REGULAR,
823 IWL_UCODE_SECTION_DATA));
824 IWL_DEBUG_INFO(drv, "f/w package hdr init inst size = %Zd\n",
825 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST));
826 IWL_DEBUG_INFO(drv, "f/w package hdr init data size = %Zd\n",
827 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA));
829 /* Verify that uCode images will fit in card's SRAM. */
830 if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) >
831 cfg->max_inst_size) {
832 IWL_ERR(drv, "uCode instr len %Zd too large to fit in\n",
833 get_sec_size(pieces, IWL_UCODE_REGULAR,
834 IWL_UCODE_SECTION_INST));
838 if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) >
839 cfg->max_data_size) {
840 IWL_ERR(drv, "uCode data len %Zd too large to fit in\n",
841 get_sec_size(pieces, IWL_UCODE_REGULAR,
842 IWL_UCODE_SECTION_DATA));
846 if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) >
847 cfg->max_inst_size) {
848 IWL_ERR(drv, "uCode init instr len %Zd too large to fit in\n",
849 get_sec_size(pieces, IWL_UCODE_INIT,
850 IWL_UCODE_SECTION_INST));
854 if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA) >
855 cfg->max_data_size) {
856 IWL_ERR(drv, "uCode init data len %Zd too large to fit in\n",
857 get_sec_size(pieces, IWL_UCODE_REGULAR,
858 IWL_UCODE_SECTION_DATA));
864 static struct iwl_op_mode *
865 _iwl_op_mode_start(struct iwl_drv *drv, struct iwlwifi_opmode_table *op)
867 const struct iwl_op_mode_ops *ops = op->ops;
868 struct dentry *dbgfs_dir = NULL;
869 struct iwl_op_mode *op_mode = NULL;
871 #ifdef CONFIG_IWLWIFI_DEBUGFS
872 drv->dbgfs_op_mode = debugfs_create_dir(op->name,
874 if (!drv->dbgfs_op_mode) {
876 "failed to create opmode debugfs directory\n");
879 dbgfs_dir = drv->dbgfs_op_mode;
882 op_mode = ops->start(drv->trans, drv->cfg, &drv->fw, dbgfs_dir);
884 #ifdef CONFIG_IWLWIFI_DEBUGFS
886 debugfs_remove_recursive(drv->dbgfs_op_mode);
887 drv->dbgfs_op_mode = NULL;
894 static void _iwl_op_mode_stop(struct iwl_drv *drv)
896 /* op_mode can be NULL if its start failed */
898 iwl_op_mode_stop(drv->op_mode);
901 #ifdef CONFIG_IWLWIFI_DEBUGFS
902 debugfs_remove_recursive(drv->dbgfs_op_mode);
903 drv->dbgfs_op_mode = NULL;
909 * iwl_req_fw_callback - callback when firmware was loaded
911 * If loaded successfully, copies the firmware into buffers
912 * for the card to fetch (via DMA).
914 static void iwl_req_fw_callback(const struct firmware *ucode_raw, void *context)
916 struct iwl_drv *drv = context;
917 struct iwl_fw *fw = &drv->fw;
918 struct iwl_ucode_header *ucode;
919 struct iwlwifi_opmode_table *op;
921 struct iwl_firmware_pieces pieces;
922 const unsigned int api_max = drv->cfg->ucode_api_max;
923 unsigned int api_ok = drv->cfg->ucode_api_ok;
924 const unsigned int api_min = drv->cfg->ucode_api_min;
927 bool load_module = false;
929 fw->ucode_capa.max_probe_length = IWL_DEFAULT_MAX_PROBE_LENGTH;
930 fw->ucode_capa.standard_phy_calibration_size =
931 IWL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE;
936 memset(&pieces, 0, sizeof(pieces));
939 if (drv->fw_index <= api_ok)
941 "request for firmware file '%s' failed.\n",
946 IWL_DEBUG_INFO(drv, "Loaded firmware file '%s' (%zd bytes).\n",
947 drv->firmware_name, ucode_raw->size);
949 /* Make sure that we got at least the API version number */
950 if (ucode_raw->size < 4) {
951 IWL_ERR(drv, "File size way too small!\n");
955 /* Data from ucode file: header followed by uCode images */
956 ucode = (struct iwl_ucode_header *)ucode_raw->data;
959 err = iwl_parse_v1_v2_firmware(drv, ucode_raw, &pieces);
961 err = iwl_parse_tlv_firmware(drv, ucode_raw, &pieces,
967 api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
970 * api_ver should match the api version forming part of the
971 * firmware filename ... but we don't check for that and only rely
972 * on the API version read from firmware header from here on forward
974 /* no api version check required for experimental uCode */
975 if (drv->fw_index != UCODE_EXPERIMENTAL_INDEX) {
976 if (api_ver < api_min || api_ver > api_max) {
978 "Driver unable to support your firmware API. "
979 "Driver supports v%u, firmware is v%u.\n",
984 if (api_ver < api_ok) {
985 if (api_ok != api_max)
986 IWL_ERR(drv, "Firmware has old API version, "
987 "expected v%u through v%u, got v%u.\n",
988 api_ok, api_max, api_ver);
990 IWL_ERR(drv, "Firmware has old API version, "
991 "expected v%u, got v%u.\n",
993 IWL_ERR(drv, "New firmware can be obtained from "
994 "http://www.intellinuxwireless.org/.\n");
999 * In mvm uCode there is no difference between data and instructions
1002 if (!fw->mvm_fw && validate_sec_sizes(drv, &pieces, drv->cfg))
1005 /* Allocate ucode buffers for card's bus-master loading ... */
1007 /* Runtime instructions and 2 copies of data:
1008 * 1) unmodified from disk
1009 * 2) backup cache for save/restore during power-downs */
1010 for (i = 0; i < IWL_UCODE_TYPE_MAX; i++)
1011 if (iwl_alloc_ucode(drv, &pieces, i))
1014 /* Now that we can no longer fail, copy information */
1017 * The (size - 16) / 12 formula is based on the information recorded
1018 * for each event, which is of mode 1 (including timestamp) for all
1019 * new microcodes that include this information.
1021 fw->init_evtlog_ptr = pieces.init_evtlog_ptr;
1022 if (pieces.init_evtlog_size)
1023 fw->init_evtlog_size = (pieces.init_evtlog_size - 16)/12;
1025 fw->init_evtlog_size =
1026 drv->cfg->base_params->max_event_log_size;
1027 fw->init_errlog_ptr = pieces.init_errlog_ptr;
1028 fw->inst_evtlog_ptr = pieces.inst_evtlog_ptr;
1029 if (pieces.inst_evtlog_size)
1030 fw->inst_evtlog_size = (pieces.inst_evtlog_size - 16)/12;
1032 fw->inst_evtlog_size =
1033 drv->cfg->base_params->max_event_log_size;
1034 fw->inst_errlog_ptr = pieces.inst_errlog_ptr;
1037 * figure out the offset of chain noise reset and gain commands
1038 * base on the size of standard phy calibration commands table size
1040 if (fw->ucode_capa.standard_phy_calibration_size >
1041 IWL_MAX_PHY_CALIBRATE_TBL_SIZE)
1042 fw->ucode_capa.standard_phy_calibration_size =
1043 IWL_MAX_STANDARD_PHY_CALIBRATE_TBL_SIZE;
1045 /* We have our copies now, allow OS release its copies */
1046 release_firmware(ucode_raw);
1048 mutex_lock(&iwlwifi_opmode_table_mtx);
1050 op = &iwlwifi_opmode_table[MVM_OP_MODE];
1052 op = &iwlwifi_opmode_table[DVM_OP_MODE];
1054 IWL_INFO(drv, "loaded firmware version %s op_mode %s\n",
1055 drv->fw.fw_version, op->name);
1057 /* add this device to the list of devices using this op_mode */
1058 list_add_tail(&drv->list, &op->drv);
1061 drv->op_mode = _iwl_op_mode_start(drv, op);
1063 if (!drv->op_mode) {
1064 mutex_unlock(&iwlwifi_opmode_table_mtx);
1070 mutex_unlock(&iwlwifi_opmode_table_mtx);
1073 * Complete the firmware request last so that
1074 * a driver unbind (stop) doesn't run while we
1075 * are doing the start() above.
1077 complete(&drv->request_firmware_complete);
1080 * Load the module last so we don't block anything
1081 * else from proceeding if the module fails to load
1085 err = request_module("%s", op->name);
1086 #ifdef CONFIG_IWLWIFI_OPMODE_MODULAR
1089 "failed to load module %s (error %d), is dynamic loading enabled?\n",
1096 /* try next, if any */
1097 release_firmware(ucode_raw);
1098 if (iwl_request_firmware(drv, false))
1103 IWL_ERR(drv, "failed to allocate pci memory\n");
1104 iwl_dealloc_ucode(drv);
1105 release_firmware(ucode_raw);
1107 complete(&drv->request_firmware_complete);
1108 device_release_driver(drv->trans->dev);
1111 struct iwl_drv *iwl_drv_start(struct iwl_trans *trans,
1112 const struct iwl_cfg *cfg)
1114 struct iwl_drv *drv;
1117 drv = kzalloc(sizeof(*drv), GFP_KERNEL);
1124 drv->dev = trans->dev;
1127 init_completion(&drv->request_firmware_complete);
1128 INIT_LIST_HEAD(&drv->list);
1130 #ifdef CONFIG_IWLWIFI_DEBUGFS
1131 /* Create the device debugfs entries. */
1132 drv->dbgfs_drv = debugfs_create_dir(dev_name(trans->dev),
1135 if (!drv->dbgfs_drv) {
1136 IWL_ERR(drv, "failed to create debugfs directory\n");
1141 /* Create transport layer debugfs dir */
1142 drv->trans->dbgfs_dir = debugfs_create_dir("trans", drv->dbgfs_drv);
1144 if (!drv->trans->dbgfs_dir) {
1145 IWL_ERR(drv, "failed to create transport debugfs directory\n");
1147 goto err_free_dbgfs;
1151 ret = iwl_request_firmware(drv, true);
1153 IWL_ERR(trans, "Couldn't request the fw\n");
1160 #ifdef CONFIG_IWLWIFI_DEBUGFS
1162 debugfs_remove_recursive(drv->dbgfs_drv);
1167 return ERR_PTR(ret);
1170 void iwl_drv_stop(struct iwl_drv *drv)
1172 wait_for_completion(&drv->request_firmware_complete);
1174 _iwl_op_mode_stop(drv);
1176 iwl_dealloc_ucode(drv);
1178 mutex_lock(&iwlwifi_opmode_table_mtx);
1180 * List is empty (this item wasn't added)
1181 * when firmware loading failed -- in that
1182 * case we can't remove it from any list.
1184 if (!list_empty(&drv->list))
1185 list_del(&drv->list);
1186 mutex_unlock(&iwlwifi_opmode_table_mtx);
1188 #ifdef CONFIG_IWLWIFI_DEBUGFS
1189 debugfs_remove_recursive(drv->dbgfs_drv);
1196 /* shared module parameters */
1197 struct iwl_mod_params iwlwifi_mod_params = {
1199 .bt_coex_active = true,
1200 .power_level = IWL_POWER_INDEX_1,
1202 /* the rest are 0 by default */
1204 IWL_EXPORT_SYMBOL(iwlwifi_mod_params);
1206 int iwl_opmode_register(const char *name, const struct iwl_op_mode_ops *ops)
1209 struct iwl_drv *drv;
1210 struct iwlwifi_opmode_table *op;
1212 mutex_lock(&iwlwifi_opmode_table_mtx);
1213 for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++) {
1214 op = &iwlwifi_opmode_table[i];
1215 if (strcmp(op->name, name))
1218 /* TODO: need to handle exceptional case */
1219 list_for_each_entry(drv, &op->drv, list)
1220 drv->op_mode = _iwl_op_mode_start(drv, op);
1222 mutex_unlock(&iwlwifi_opmode_table_mtx);
1225 mutex_unlock(&iwlwifi_opmode_table_mtx);
1228 IWL_EXPORT_SYMBOL(iwl_opmode_register);
1230 void iwl_opmode_deregister(const char *name)
1233 struct iwl_drv *drv;
1235 mutex_lock(&iwlwifi_opmode_table_mtx);
1236 for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++) {
1237 if (strcmp(iwlwifi_opmode_table[i].name, name))
1239 iwlwifi_opmode_table[i].ops = NULL;
1241 /* call the stop routine for all devices */
1242 list_for_each_entry(drv, &iwlwifi_opmode_table[i].drv, list)
1243 _iwl_op_mode_stop(drv);
1245 mutex_unlock(&iwlwifi_opmode_table_mtx);
1248 mutex_unlock(&iwlwifi_opmode_table_mtx);
1250 IWL_EXPORT_SYMBOL(iwl_opmode_deregister);
1252 static int __init iwl_drv_init(void)
1256 mutex_init(&iwlwifi_opmode_table_mtx);
1258 for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++)
1259 INIT_LIST_HEAD(&iwlwifi_opmode_table[i].drv);
1261 pr_info(DRV_DESCRIPTION ", " DRV_VERSION "\n");
1262 pr_info(DRV_COPYRIGHT "\n");
1264 #ifdef CONFIG_IWLWIFI_DEBUGFS
1265 /* Create the root of iwlwifi debugfs subsystem. */
1266 iwl_dbgfs_root = debugfs_create_dir(DRV_NAME, NULL);
1268 if (!iwl_dbgfs_root)
1272 return iwl_pci_register_driver();
1274 module_init(iwl_drv_init);
1276 static void __exit iwl_drv_exit(void)
1278 iwl_pci_unregister_driver();
1280 #ifdef CONFIG_IWLWIFI_DEBUGFS
1281 debugfs_remove_recursive(iwl_dbgfs_root);
1284 module_exit(iwl_drv_exit);
1286 #ifdef CONFIG_IWLWIFI_DEBUG
1287 module_param_named(debug, iwlwifi_mod_params.debug_level, uint,
1289 MODULE_PARM_DESC(debug, "debug output mask");
1292 module_param_named(swcrypto, iwlwifi_mod_params.sw_crypto, int, S_IRUGO);
1293 MODULE_PARM_DESC(swcrypto, "using crypto in software (default 0 [hardware])");
1294 module_param_named(11n_disable, iwlwifi_mod_params.disable_11n, uint, S_IRUGO);
1295 MODULE_PARM_DESC(11n_disable,
1296 "disable 11n functionality, bitmap: 1: full, 2: agg TX, 4: agg RX");
1297 module_param_named(amsdu_size_8K, iwlwifi_mod_params.amsdu_size_8K,
1299 MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size (default 0)");
1300 module_param_named(fw_restart, iwlwifi_mod_params.restart_fw, bool, S_IRUGO);
1301 MODULE_PARM_DESC(fw_restart, "restart firmware in case of error (default true)");
1303 module_param_named(antenna_coupling, iwlwifi_mod_params.ant_coupling,
1305 MODULE_PARM_DESC(antenna_coupling,
1306 "specify antenna coupling in dB (defualt: 0 dB)");
1308 module_param_named(wd_disable, iwlwifi_mod_params.wd_disable, int, S_IRUGO);
1309 MODULE_PARM_DESC(wd_disable,
1310 "Disable stuck queue watchdog timer 0=system default, "
1311 "1=disable, 2=enable (default: 0)");
1313 module_param_named(nvm_file, iwlwifi_mod_params.nvm_file, charp, S_IRUGO);
1314 MODULE_PARM_DESC(nvm_file, "NVM file name");
1317 * set bt_coex_active to true, uCode will do kill/defer
1318 * every time the priority line is asserted (BT is sending signals on the
1319 * priority line in the PCIx).
1320 * set bt_coex_active to false, uCode will ignore the BT activity and
1321 * perform the normal operation
1323 * User might experience transmit issue on some platform due to WiFi/BT
1324 * co-exist problem. The possible behaviors are:
1325 * Able to scan and finding all the available AP
1326 * Not able to associate with any AP
1327 * On those platforms, WiFi communication can be restored by set
1328 * "bt_coex_active" module parameter to "false"
1330 * default: bt_coex_active = true (BT_COEX_ENABLE)
1332 module_param_named(bt_coex_active, iwlwifi_mod_params.bt_coex_active,
1334 MODULE_PARM_DESC(bt_coex_active, "enable wifi/bt co-exist (default: enable)");
1336 module_param_named(led_mode, iwlwifi_mod_params.led_mode, int, S_IRUGO);
1337 MODULE_PARM_DESC(led_mode, "0=system default, "
1338 "1=On(RF On)/Off(RF Off), 2=blinking, 3=Off (default: 0)");
1340 module_param_named(power_save, iwlwifi_mod_params.power_save,
1342 MODULE_PARM_DESC(power_save,
1343 "enable WiFi power management (default: disable)");
1345 module_param_named(power_level, iwlwifi_mod_params.power_level,
1347 MODULE_PARM_DESC(power_level,
1348 "default power save level (range from 1 - 5, default: 1)");