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 - 2012 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 LICENSE.GPL.
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 - 2012 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>
69 #include "iwl-trans.h"
71 #include "iwl-shared.h"
72 #include "iwl-op-mode.h"
74 static void iwl_free_fw_desc(struct iwl_nic *nic, struct fw_desc *desc)
77 dma_free_coherent(trans(nic)->dev, desc->len,
78 desc->v_addr, desc->p_addr);
83 static void iwl_free_fw_img(struct iwl_nic *nic, struct fw_img *img)
85 iwl_free_fw_desc(nic, &img->code);
86 iwl_free_fw_desc(nic, &img->data);
89 static void iwl_dealloc_ucode(struct iwl_nic *nic)
91 iwl_free_fw_img(nic, &nic->fw.ucode_rt);
92 iwl_free_fw_img(nic, &nic->fw.ucode_init);
93 iwl_free_fw_img(nic, &nic->fw.ucode_wowlan);
96 static int iwl_alloc_fw_desc(struct iwl_nic *nic, struct fw_desc *desc,
97 const void *data, size_t len)
104 desc->v_addr = dma_alloc_coherent(trans(nic)->dev, len,
105 &desc->p_addr, GFP_KERNEL);
110 memcpy(desc->v_addr, data, len);
114 static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context);
116 #define UCODE_EXPERIMENTAL_INDEX 100
117 #define UCODE_EXPERIMENTAL_TAG "exp"
119 static int iwl_request_firmware(struct iwl_nic *nic, bool first)
121 const struct iwl_cfg *cfg = cfg(nic);
122 const char *name_pre = cfg->fw_name_pre;
126 #ifdef CONFIG_IWLWIFI_DEBUG_EXPERIMENTAL_UCODE
127 nic->fw_index = UCODE_EXPERIMENTAL_INDEX;
128 strcpy(tag, UCODE_EXPERIMENTAL_TAG);
129 } else if (nic->fw_index == UCODE_EXPERIMENTAL_INDEX) {
131 nic->fw_index = cfg->ucode_api_max;
132 sprintf(tag, "%d", nic->fw_index);
135 sprintf(tag, "%d", nic->fw_index);
138 if (nic->fw_index < cfg->ucode_api_min) {
139 IWL_ERR(nic, "no suitable firmware found!\n");
143 sprintf(nic->firmware_name, "%s%s%s", name_pre, tag, ".ucode");
145 IWL_DEBUG_INFO(nic, "attempting to load firmware %s'%s'\n",
146 (nic->fw_index == UCODE_EXPERIMENTAL_INDEX)
147 ? "EXPERIMENTAL " : "",
150 return request_firmware_nowait(THIS_MODULE, 1, nic->firmware_name,
152 GFP_KERNEL, nic, iwl_ucode_callback);
155 struct iwlagn_firmware_pieces {
156 const void *inst, *data, *init, *init_data, *wowlan_inst, *wowlan_data;
157 size_t inst_size, data_size, init_size, init_data_size,
158 wowlan_inst_size, wowlan_data_size;
160 u32 init_evtlog_ptr, init_evtlog_size, init_errlog_ptr;
161 u32 inst_evtlog_ptr, inst_evtlog_size, inst_errlog_ptr;
164 static int iwl_parse_v1_v2_firmware(struct iwl_nic *nic,
165 const struct firmware *ucode_raw,
166 struct iwlagn_firmware_pieces *pieces)
168 struct iwl_ucode_header *ucode = (void *)ucode_raw->data;
169 u32 api_ver, hdr_size, build;
173 nic->fw.ucode_ver = le32_to_cpu(ucode->ver);
174 api_ver = IWL_UCODE_API(nic->fw.ucode_ver);
179 if (ucode_raw->size < hdr_size) {
180 IWL_ERR(nic, "File size too small!\n");
183 build = le32_to_cpu(ucode->u.v2.build);
184 pieces->inst_size = le32_to_cpu(ucode->u.v2.inst_size);
185 pieces->data_size = le32_to_cpu(ucode->u.v2.data_size);
186 pieces->init_size = le32_to_cpu(ucode->u.v2.init_size);
187 pieces->init_data_size =
188 le32_to_cpu(ucode->u.v2.init_data_size);
189 src = ucode->u.v2.data;
195 if (ucode_raw->size < hdr_size) {
196 IWL_ERR(nic, "File size too small!\n");
200 pieces->inst_size = le32_to_cpu(ucode->u.v1.inst_size);
201 pieces->data_size = le32_to_cpu(ucode->u.v1.data_size);
202 pieces->init_size = le32_to_cpu(ucode->u.v1.init_size);
203 pieces->init_data_size =
204 le32_to_cpu(ucode->u.v1.init_data_size);
205 src = ucode->u.v1.data;
210 sprintf(buildstr, " build %u%s", build,
211 (nic->fw_index == UCODE_EXPERIMENTAL_INDEX)
216 snprintf(nic->fw.fw_version,
217 sizeof(nic->fw.fw_version),
219 IWL_UCODE_MAJOR(nic->fw.ucode_ver),
220 IWL_UCODE_MINOR(nic->fw.ucode_ver),
221 IWL_UCODE_API(nic->fw.ucode_ver),
222 IWL_UCODE_SERIAL(nic->fw.ucode_ver),
225 /* Verify size of file vs. image size info in file's header */
226 if (ucode_raw->size != hdr_size + pieces->inst_size +
227 pieces->data_size + pieces->init_size +
228 pieces->init_data_size) {
231 "uCode file size %d does not match expected size\n",
232 (int)ucode_raw->size);
237 src += pieces->inst_size;
239 src += pieces->data_size;
241 src += pieces->init_size;
242 pieces->init_data = src;
243 src += pieces->init_data_size;
248 static int iwl_parse_tlv_firmware(struct iwl_nic *nic,
249 const struct firmware *ucode_raw,
250 struct iwlagn_firmware_pieces *pieces,
251 struct iwl_ucode_capabilities *capa)
253 struct iwl_tlv_ucode_header *ucode = (void *)ucode_raw->data;
254 struct iwl_ucode_tlv *tlv;
255 size_t len = ucode_raw->size;
257 int wanted_alternative = iwlagn_mod_params.wanted_ucode_alternative;
261 enum iwl_ucode_tlv_type tlv_type;
266 if (len < sizeof(*ucode)) {
267 IWL_ERR(nic, "uCode has invalid length: %zd\n", len);
271 if (ucode->magic != cpu_to_le32(IWL_TLV_UCODE_MAGIC)) {
272 IWL_ERR(nic, "invalid uCode magic: 0X%x\n",
273 le32_to_cpu(ucode->magic));
278 * Check which alternatives are present, and "downgrade"
279 * when the chosen alternative is not present, warning
280 * the user when that happens. Some files may not have
281 * any alternatives, so don't warn in that case.
283 alternatives = le64_to_cpu(ucode->alternatives);
284 tmp = wanted_alternative;
285 if (wanted_alternative > 63)
286 wanted_alternative = 63;
287 while (wanted_alternative && !(alternatives & BIT(wanted_alternative)))
288 wanted_alternative--;
289 if (wanted_alternative && wanted_alternative != tmp)
291 "uCode alternative %d not available, choosing %d\n",
292 tmp, wanted_alternative);
294 nic->fw.ucode_ver = le32_to_cpu(ucode->ver);
295 build = le32_to_cpu(ucode->build);
298 sprintf(buildstr, " build %u%s", build,
299 (nic->fw_index == UCODE_EXPERIMENTAL_INDEX)
304 snprintf(nic->fw.fw_version,
305 sizeof(nic->fw.fw_version),
307 IWL_UCODE_MAJOR(nic->fw.ucode_ver),
308 IWL_UCODE_MINOR(nic->fw.ucode_ver),
309 IWL_UCODE_API(nic->fw.ucode_ver),
310 IWL_UCODE_SERIAL(nic->fw.ucode_ver),
315 len -= sizeof(*ucode);
317 while (len >= sizeof(*tlv)) {
323 tlv_len = le32_to_cpu(tlv->length);
324 tlv_type = le16_to_cpu(tlv->type);
325 tlv_alt = le16_to_cpu(tlv->alternative);
326 tlv_data = tlv->data;
329 IWL_ERR(nic, "invalid TLV len: %zd/%u\n",
333 len -= ALIGN(tlv_len, 4);
334 data += sizeof(*tlv) + ALIGN(tlv_len, 4);
337 * Alternative 0 is always valid.
339 * Skip alternative TLVs that are not selected.
341 if (tlv_alt != 0 && tlv_alt != wanted_alternative)
345 case IWL_UCODE_TLV_INST:
346 pieces->inst = tlv_data;
347 pieces->inst_size = tlv_len;
349 case IWL_UCODE_TLV_DATA:
350 pieces->data = tlv_data;
351 pieces->data_size = tlv_len;
353 case IWL_UCODE_TLV_INIT:
354 pieces->init = tlv_data;
355 pieces->init_size = tlv_len;
357 case IWL_UCODE_TLV_INIT_DATA:
358 pieces->init_data = tlv_data;
359 pieces->init_data_size = tlv_len;
361 case IWL_UCODE_TLV_BOOT:
362 IWL_ERR(nic, "Found unexpected BOOT ucode\n");
364 case IWL_UCODE_TLV_PROBE_MAX_LEN:
365 if (tlv_len != sizeof(u32))
366 goto invalid_tlv_len;
367 capa->max_probe_length =
368 le32_to_cpup((__le32 *)tlv_data);
370 case IWL_UCODE_TLV_PAN:
372 goto invalid_tlv_len;
373 capa->flags |= IWL_UCODE_TLV_FLAGS_PAN;
375 case IWL_UCODE_TLV_FLAGS:
376 /* must be at least one u32 */
377 if (tlv_len < sizeof(u32))
378 goto invalid_tlv_len;
379 /* and a proper number of u32s */
380 if (tlv_len % sizeof(u32))
381 goto invalid_tlv_len;
383 * This driver only reads the first u32 as
384 * right now no more features are defined,
385 * if that changes then either the driver
386 * will not work with the new firmware, or
387 * it'll not take advantage of new features.
389 capa->flags = le32_to_cpup((__le32 *)tlv_data);
391 case IWL_UCODE_TLV_INIT_EVTLOG_PTR:
392 if (tlv_len != sizeof(u32))
393 goto invalid_tlv_len;
394 pieces->init_evtlog_ptr =
395 le32_to_cpup((__le32 *)tlv_data);
397 case IWL_UCODE_TLV_INIT_EVTLOG_SIZE:
398 if (tlv_len != sizeof(u32))
399 goto invalid_tlv_len;
400 pieces->init_evtlog_size =
401 le32_to_cpup((__le32 *)tlv_data);
403 case IWL_UCODE_TLV_INIT_ERRLOG_PTR:
404 if (tlv_len != sizeof(u32))
405 goto invalid_tlv_len;
406 pieces->init_errlog_ptr =
407 le32_to_cpup((__le32 *)tlv_data);
409 case IWL_UCODE_TLV_RUNT_EVTLOG_PTR:
410 if (tlv_len != sizeof(u32))
411 goto invalid_tlv_len;
412 pieces->inst_evtlog_ptr =
413 le32_to_cpup((__le32 *)tlv_data);
415 case IWL_UCODE_TLV_RUNT_EVTLOG_SIZE:
416 if (tlv_len != sizeof(u32))
417 goto invalid_tlv_len;
418 pieces->inst_evtlog_size =
419 le32_to_cpup((__le32 *)tlv_data);
421 case IWL_UCODE_TLV_RUNT_ERRLOG_PTR:
422 if (tlv_len != sizeof(u32))
423 goto invalid_tlv_len;
424 pieces->inst_errlog_ptr =
425 le32_to_cpup((__le32 *)tlv_data);
427 case IWL_UCODE_TLV_ENHANCE_SENS_TBL:
429 goto invalid_tlv_len;
430 nic->fw.enhance_sensitivity_table = true;
432 case IWL_UCODE_TLV_WOWLAN_INST:
433 pieces->wowlan_inst = tlv_data;
434 pieces->wowlan_inst_size = tlv_len;
436 case IWL_UCODE_TLV_WOWLAN_DATA:
437 pieces->wowlan_data = tlv_data;
438 pieces->wowlan_data_size = tlv_len;
440 case IWL_UCODE_TLV_PHY_CALIBRATION_SIZE:
441 if (tlv_len != sizeof(u32))
442 goto invalid_tlv_len;
443 capa->standard_phy_calibration_size =
444 le32_to_cpup((__le32 *)tlv_data);
447 IWL_DEBUG_INFO(nic, "unknown TLV: %d\n", tlv_type);
453 IWL_ERR(nic, "invalid TLV after parsing: %zd\n", len);
454 iwl_print_hex_dump(nic, IWL_DL_FW, (u8 *)data, len);
461 IWL_ERR(nic, "TLV %d has invalid size: %u\n", tlv_type, tlv_len);
462 iwl_print_hex_dump(nic, IWL_DL_FW, tlv_data, tlv_len);
468 * iwl_ucode_callback - callback when firmware was loaded
470 * If loaded successfully, copies the firmware into buffers
471 * for the card to fetch (via DMA).
473 static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context)
475 struct iwl_nic *nic = context;
476 const struct iwl_cfg *cfg = cfg(nic);
477 struct iwl_fw *fw = &nic->fw;
478 struct iwl_ucode_header *ucode;
480 struct iwlagn_firmware_pieces pieces;
481 const unsigned int api_max = cfg->ucode_api_max;
482 unsigned int api_ok = cfg->ucode_api_ok;
483 const unsigned int api_min = cfg->ucode_api_min;
486 fw->ucode_capa.max_probe_length = 200;
487 fw->ucode_capa.standard_phy_calibration_size =
488 IWL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE;
493 memset(&pieces, 0, sizeof(pieces));
496 if (nic->fw_index <= api_ok)
498 "request for firmware file '%s' failed.\n",
503 IWL_DEBUG_INFO(nic, "Loaded firmware file '%s' (%zd bytes).\n",
504 nic->firmware_name, ucode_raw->size);
506 /* Make sure that we got at least the API version number */
507 if (ucode_raw->size < 4) {
508 IWL_ERR(nic, "File size way too small!\n");
512 /* Data from ucode file: header followed by uCode images */
513 ucode = (struct iwl_ucode_header *)ucode_raw->data;
516 err = iwl_parse_v1_v2_firmware(nic, ucode_raw, &pieces);
518 err = iwl_parse_tlv_firmware(nic, ucode_raw, &pieces,
524 api_ver = IWL_UCODE_API(nic->fw.ucode_ver);
527 * api_ver should match the api version forming part of the
528 * firmware filename ... but we don't check for that and only rely
529 * on the API version read from firmware header from here on forward
531 /* no api version check required for experimental uCode */
532 if (nic->fw_index != UCODE_EXPERIMENTAL_INDEX) {
533 if (api_ver < api_min || api_ver > api_max) {
535 "Driver unable to support your firmware API. "
536 "Driver supports v%u, firmware is v%u.\n",
541 if (api_ver < api_ok) {
542 if (api_ok != api_max)
543 IWL_ERR(nic, "Firmware has old API version, "
544 "expected v%u through v%u, got v%u.\n",
545 api_ok, api_max, api_ver);
547 IWL_ERR(nic, "Firmware has old API version, "
548 "expected v%u, got v%u.\n",
550 IWL_ERR(nic, "New firmware can be obtained from "
551 "http://www.intellinuxwireless.org/.\n");
555 IWL_INFO(nic, "loaded firmware version %s", nic->fw.fw_version);
558 * For any of the failures below (before allocating pci memory)
559 * we will try to load a version with a smaller API -- maybe the
560 * user just got a corrupted version of the latest API.
563 IWL_DEBUG_INFO(nic, "f/w package hdr ucode version raw = 0x%x\n",
565 IWL_DEBUG_INFO(nic, "f/w package hdr runtime inst size = %Zd\n",
567 IWL_DEBUG_INFO(nic, "f/w package hdr runtime data size = %Zd\n",
569 IWL_DEBUG_INFO(nic, "f/w package hdr init inst size = %Zd\n",
571 IWL_DEBUG_INFO(nic, "f/w package hdr init data size = %Zd\n",
572 pieces.init_data_size);
574 /* Verify that uCode images will fit in card's SRAM */
575 if (pieces.inst_size > cfg->max_inst_size) {
576 IWL_ERR(nic, "uCode instr len %Zd too large to fit in\n",
581 if (pieces.data_size > cfg->max_data_size) {
582 IWL_ERR(nic, "uCode data len %Zd too large to fit in\n",
587 if (pieces.init_size > cfg->max_inst_size) {
588 IWL_ERR(nic, "uCode init instr len %Zd too large to fit in\n",
593 if (pieces.init_data_size > cfg->max_data_size) {
594 IWL_ERR(nic, "uCode init data len %Zd too large to fit in\n",
595 pieces.init_data_size);
599 /* Allocate ucode buffers for card's bus-master loading ... */
601 /* Runtime instructions and 2 copies of data:
602 * 1) unmodified from disk
603 * 2) backup cache for save/restore during power-downs */
604 if (iwl_alloc_fw_desc(nic, &nic->fw.ucode_rt.code,
605 pieces.inst, pieces.inst_size))
607 if (iwl_alloc_fw_desc(nic, &nic->fw.ucode_rt.data,
608 pieces.data, pieces.data_size))
611 /* Initialization instructions and data */
612 if (pieces.init_size && pieces.init_data_size) {
613 if (iwl_alloc_fw_desc(nic,
614 &nic->fw.ucode_init.code,
615 pieces.init, pieces.init_size))
617 if (iwl_alloc_fw_desc(nic,
618 &nic->fw.ucode_init.data,
619 pieces.init_data, pieces.init_data_size))
623 /* WoWLAN instructions and data */
624 if (pieces.wowlan_inst_size && pieces.wowlan_data_size) {
625 if (iwl_alloc_fw_desc(nic,
626 &nic->fw.ucode_wowlan.code,
628 pieces.wowlan_inst_size))
630 if (iwl_alloc_fw_desc(nic,
631 &nic->fw.ucode_wowlan.data,
633 pieces.wowlan_data_size))
637 /* Now that we can no longer fail, copy information */
640 * The (size - 16) / 12 formula is based on the information recorded
641 * for each event, which is of mode 1 (including timestamp) for all
642 * new microcodes that include this information.
644 nic->init_evtlog_ptr = pieces.init_evtlog_ptr;
645 if (pieces.init_evtlog_size)
646 nic->init_evtlog_size = (pieces.init_evtlog_size - 16)/12;
648 nic->init_evtlog_size =
649 cfg->base_params->max_event_log_size;
650 nic->init_errlog_ptr = pieces.init_errlog_ptr;
651 nic->inst_evtlog_ptr = pieces.inst_evtlog_ptr;
652 if (pieces.inst_evtlog_size)
653 nic->inst_evtlog_size = (pieces.inst_evtlog_size - 16)/12;
655 nic->inst_evtlog_size =
656 cfg->base_params->max_event_log_size;
657 nic->inst_errlog_ptr = pieces.inst_errlog_ptr;
660 * figure out the offset of chain noise reset and gain commands
661 * base on the size of standard phy calibration commands table size
663 if (fw->ucode_capa.standard_phy_calibration_size >
664 IWL_MAX_PHY_CALIBRATE_TBL_SIZE)
665 fw->ucode_capa.standard_phy_calibration_size =
666 IWL_MAX_STANDARD_PHY_CALIBRATE_TBL_SIZE;
668 /* We have our copies now, allow OS release its copies */
669 release_firmware(ucode_raw);
670 complete(&nic->request_firmware_complete);
672 nic->op_mode = iwl_dvm_ops.start(nic->shrd->trans);
680 /* try next, if any */
681 release_firmware(ucode_raw);
682 if (iwl_request_firmware(nic, false))
687 IWL_ERR(nic, "failed to allocate pci memory\n");
688 iwl_dealloc_ucode(nic);
689 release_firmware(ucode_raw);
691 complete(&nic->request_firmware_complete);
692 device_release_driver(trans(nic)->dev);
695 int iwl_drv_start(struct iwl_shared *shrd,
696 struct iwl_trans *trans, const struct iwl_cfg *cfg)
702 shrd->nic = kzalloc(sizeof(*shrd->nic), GFP_KERNEL);
704 dev_printk(KERN_ERR, trans->dev, "Couldn't allocate iwl_nic");
707 shrd->nic->shrd = shrd;
709 init_completion(&shrd->nic->request_firmware_complete);
711 ret = iwl_request_firmware(shrd->nic, true);
714 dev_printk(KERN_ERR, trans->dev, "Couldn't request the fw");
721 void iwl_drv_stop(struct iwl_shared *shrd)
723 wait_for_completion(&shrd->nic->request_firmware_complete);
725 /* op_mode can be NULL if its start failed */
726 if (shrd->nic->op_mode)
727 iwl_op_mode_stop(shrd->nic->op_mode);
729 iwl_dealloc_ucode(shrd->nic);