blob: f1beeab1e601d1aa31d35a8c43322cf6854064ba [file] [log] [blame]
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +02001/******************************************************************************
2 *
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.
5 *
6 * GPL LICENSE SUMMARY
7 *
8 * Copyright(c) 2007 - 2012 Intel Corporation. All rights reserved.
9 *
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.
13 *
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.
18 *
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,
22 * USA
23 *
24 * The full GNU General Public License is included in this distribution
25 * in the file called LICENSE.GPL.
26 *
27 * Contact Information:
28 * Intel Linux Wireless <ilw@linux.intel.com>
29 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30 *
31 * BSD LICENSE
32 *
33 * Copyright(c) 2005 - 2012 Intel Corporation. All rights reserved.
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 *
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
45 * distribution.
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.
49 *
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.
61 *
62 *****************************************************************************/
63#include <linux/completion.h>
Johannes Berg15854ef2012-03-05 11:24:50 -080064#include <linux/dma-mapping.h>
65#include <linux/firmware.h>
66#include <linux/module.h>
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +020067
68#include "iwl-drv.h"
69#include "iwl-trans.h"
Johannes Berg15854ef2012-03-05 11:24:50 -080070#include "iwl-shared.h"
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +020071#include "iwl-op-mode.h"
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +020072
Johannes Berg0692fe42012-03-06 13:30:37 -080073/* private includes */
74#include "iwl-ucode.h"
75
Johannes Berg965974a2012-03-06 13:30:38 -080076/**
77 * struct iwl_drv - drv common data
78 * @fw: the iwl_fw structure
79 * @shrd: pointer to common shared structure
80 * @op_mode: the running op_mode
81 * @fw_index: firmware revision to try loading
82 * @firmware_name: composite filename of ucode file to load
83 * @request_firmware_complete: the firmware has been obtained from user space
84 */
85struct iwl_drv {
86 struct iwl_fw fw;
87
88 struct iwl_shared *shrd;
89 struct iwl_op_mode *op_mode;
90
91 int fw_index; /* firmware we're trying to load */
92 char firmware_name[25]; /* name of firmware file to load */
93
94 struct completion request_firmware_complete;
95};
96
97
98
99static void iwl_free_fw_desc(struct iwl_drv *drv, struct fw_desc *desc)
Johannes Berg15854ef2012-03-05 11:24:50 -0800100{
101 if (desc->v_addr)
Johannes Berg965974a2012-03-06 13:30:38 -0800102 dma_free_coherent(trans(drv)->dev, desc->len,
Johannes Berg15854ef2012-03-05 11:24:50 -0800103 desc->v_addr, desc->p_addr);
104 desc->v_addr = NULL;
105 desc->len = 0;
106}
107
Johannes Berg965974a2012-03-06 13:30:38 -0800108static void iwl_free_fw_img(struct iwl_drv *drv, struct fw_img *img)
Johannes Berg15854ef2012-03-05 11:24:50 -0800109{
Johannes Berg965974a2012-03-06 13:30:38 -0800110 iwl_free_fw_desc(drv, &img->code);
111 iwl_free_fw_desc(drv, &img->data);
Johannes Berg15854ef2012-03-05 11:24:50 -0800112}
113
Johannes Berg965974a2012-03-06 13:30:38 -0800114static void iwl_dealloc_ucode(struct iwl_drv *drv)
Johannes Berg15854ef2012-03-05 11:24:50 -0800115{
Johannes Berg965974a2012-03-06 13:30:38 -0800116 iwl_free_fw_img(drv, &drv->fw.ucode_rt);
117 iwl_free_fw_img(drv, &drv->fw.ucode_init);
118 iwl_free_fw_img(drv, &drv->fw.ucode_wowlan);
Johannes Berg15854ef2012-03-05 11:24:50 -0800119}
120
Johannes Berg965974a2012-03-06 13:30:38 -0800121static int iwl_alloc_fw_desc(struct iwl_drv *drv, struct fw_desc *desc,
Johannes Berg15854ef2012-03-05 11:24:50 -0800122 const void *data, size_t len)
123{
124 if (!len) {
125 desc->v_addr = NULL;
126 return -EINVAL;
127 }
128
Johannes Berg965974a2012-03-06 13:30:38 -0800129 desc->v_addr = dma_alloc_coherent(trans(drv)->dev, len,
Johannes Berg15854ef2012-03-05 11:24:50 -0800130 &desc->p_addr, GFP_KERNEL);
131 if (!desc->v_addr)
132 return -ENOMEM;
133
134 desc->len = len;
135 memcpy(desc->v_addr, data, len);
136 return 0;
137}
138
139static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context);
140
141#define UCODE_EXPERIMENTAL_INDEX 100
142#define UCODE_EXPERIMENTAL_TAG "exp"
143
Johannes Berg965974a2012-03-06 13:30:38 -0800144static int iwl_request_firmware(struct iwl_drv *drv, bool first)
Johannes Berg15854ef2012-03-05 11:24:50 -0800145{
Johannes Berg965974a2012-03-06 13:30:38 -0800146 const struct iwl_cfg *cfg = cfg(drv);
Johannes Berg15854ef2012-03-05 11:24:50 -0800147 const char *name_pre = cfg->fw_name_pre;
148 char tag[8];
149
150 if (first) {
151#ifdef CONFIG_IWLWIFI_DEBUG_EXPERIMENTAL_UCODE
Johannes Berg965974a2012-03-06 13:30:38 -0800152 drv->fw_index = UCODE_EXPERIMENTAL_INDEX;
Johannes Berg15854ef2012-03-05 11:24:50 -0800153 strcpy(tag, UCODE_EXPERIMENTAL_TAG);
Johannes Berg965974a2012-03-06 13:30:38 -0800154 } else if (drv->fw_index == UCODE_EXPERIMENTAL_INDEX) {
Johannes Berg15854ef2012-03-05 11:24:50 -0800155#endif
Johannes Berg965974a2012-03-06 13:30:38 -0800156 drv->fw_index = cfg->ucode_api_max;
157 sprintf(tag, "%d", drv->fw_index);
Johannes Berg15854ef2012-03-05 11:24:50 -0800158 } else {
Johannes Berg965974a2012-03-06 13:30:38 -0800159 drv->fw_index--;
160 sprintf(tag, "%d", drv->fw_index);
Johannes Berg15854ef2012-03-05 11:24:50 -0800161 }
162
Johannes Berg965974a2012-03-06 13:30:38 -0800163 if (drv->fw_index < cfg->ucode_api_min) {
164 IWL_ERR(drv, "no suitable firmware found!\n");
Johannes Berg15854ef2012-03-05 11:24:50 -0800165 return -ENOENT;
166 }
167
Johannes Berg965974a2012-03-06 13:30:38 -0800168 sprintf(drv->firmware_name, "%s%s%s", name_pre, tag, ".ucode");
Johannes Berg15854ef2012-03-05 11:24:50 -0800169
Johannes Berg965974a2012-03-06 13:30:38 -0800170 IWL_DEBUG_INFO(drv, "attempting to load firmware %s'%s'\n",
171 (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
Johannes Berg15854ef2012-03-05 11:24:50 -0800172 ? "EXPERIMENTAL " : "",
Johannes Berg965974a2012-03-06 13:30:38 -0800173 drv->firmware_name);
Johannes Berg15854ef2012-03-05 11:24:50 -0800174
Johannes Berg965974a2012-03-06 13:30:38 -0800175 return request_firmware_nowait(THIS_MODULE, 1, drv->firmware_name,
176 trans(drv)->dev,
177 GFP_KERNEL, drv, iwl_ucode_callback);
Johannes Berg15854ef2012-03-05 11:24:50 -0800178}
179
180struct iwlagn_firmware_pieces {
181 const void *inst, *data, *init, *init_data, *wowlan_inst, *wowlan_data;
182 size_t inst_size, data_size, init_size, init_data_size,
183 wowlan_inst_size, wowlan_data_size;
184
185 u32 init_evtlog_ptr, init_evtlog_size, init_errlog_ptr;
186 u32 inst_evtlog_ptr, inst_evtlog_size, inst_errlog_ptr;
187};
188
Johannes Berg965974a2012-03-06 13:30:38 -0800189static int iwl_parse_v1_v2_firmware(struct iwl_drv *drv,
Johannes Berg15854ef2012-03-05 11:24:50 -0800190 const struct firmware *ucode_raw,
191 struct iwlagn_firmware_pieces *pieces)
192{
193 struct iwl_ucode_header *ucode = (void *)ucode_raw->data;
194 u32 api_ver, hdr_size, build;
195 char buildstr[25];
196 const u8 *src;
197
Johannes Berg965974a2012-03-06 13:30:38 -0800198 drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
199 api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
Johannes Berg15854ef2012-03-05 11:24:50 -0800200
201 switch (api_ver) {
202 default:
203 hdr_size = 28;
204 if (ucode_raw->size < hdr_size) {
Johannes Berg965974a2012-03-06 13:30:38 -0800205 IWL_ERR(drv, "File size too small!\n");
Johannes Berg15854ef2012-03-05 11:24:50 -0800206 return -EINVAL;
207 }
208 build = le32_to_cpu(ucode->u.v2.build);
209 pieces->inst_size = le32_to_cpu(ucode->u.v2.inst_size);
210 pieces->data_size = le32_to_cpu(ucode->u.v2.data_size);
211 pieces->init_size = le32_to_cpu(ucode->u.v2.init_size);
212 pieces->init_data_size =
213 le32_to_cpu(ucode->u.v2.init_data_size);
214 src = ucode->u.v2.data;
215 break;
216 case 0:
217 case 1:
218 case 2:
219 hdr_size = 24;
220 if (ucode_raw->size < hdr_size) {
Johannes Berg965974a2012-03-06 13:30:38 -0800221 IWL_ERR(drv, "File size too small!\n");
Johannes Berg15854ef2012-03-05 11:24:50 -0800222 return -EINVAL;
223 }
224 build = 0;
225 pieces->inst_size = le32_to_cpu(ucode->u.v1.inst_size);
226 pieces->data_size = le32_to_cpu(ucode->u.v1.data_size);
227 pieces->init_size = le32_to_cpu(ucode->u.v1.init_size);
228 pieces->init_data_size =
229 le32_to_cpu(ucode->u.v1.init_data_size);
230 src = ucode->u.v1.data;
231 break;
232 }
233
234 if (build)
235 sprintf(buildstr, " build %u%s", build,
Johannes Berg965974a2012-03-06 13:30:38 -0800236 (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
Johannes Berg15854ef2012-03-05 11:24:50 -0800237 ? " (EXP)" : "");
238 else
239 buildstr[0] = '\0';
240
Johannes Berg965974a2012-03-06 13:30:38 -0800241 snprintf(drv->fw.fw_version,
242 sizeof(drv->fw.fw_version),
Johannes Berg15854ef2012-03-05 11:24:50 -0800243 "%u.%u.%u.%u%s",
Johannes Berg965974a2012-03-06 13:30:38 -0800244 IWL_UCODE_MAJOR(drv->fw.ucode_ver),
245 IWL_UCODE_MINOR(drv->fw.ucode_ver),
246 IWL_UCODE_API(drv->fw.ucode_ver),
247 IWL_UCODE_SERIAL(drv->fw.ucode_ver),
Johannes Berg15854ef2012-03-05 11:24:50 -0800248 buildstr);
249
250 /* Verify size of file vs. image size info in file's header */
251 if (ucode_raw->size != hdr_size + pieces->inst_size +
252 pieces->data_size + pieces->init_size +
253 pieces->init_data_size) {
254
Johannes Berg965974a2012-03-06 13:30:38 -0800255 IWL_ERR(drv,
Johannes Berg15854ef2012-03-05 11:24:50 -0800256 "uCode file size %d does not match expected size\n",
257 (int)ucode_raw->size);
258 return -EINVAL;
259 }
260
261 pieces->inst = src;
262 src += pieces->inst_size;
263 pieces->data = src;
264 src += pieces->data_size;
265 pieces->init = src;
266 src += pieces->init_size;
267 pieces->init_data = src;
268 src += pieces->init_data_size;
269
270 return 0;
271}
272
Johannes Berg965974a2012-03-06 13:30:38 -0800273static int iwl_parse_tlv_firmware(struct iwl_drv *drv,
Johannes Berg15854ef2012-03-05 11:24:50 -0800274 const struct firmware *ucode_raw,
275 struct iwlagn_firmware_pieces *pieces,
276 struct iwl_ucode_capabilities *capa)
277{
278 struct iwl_tlv_ucode_header *ucode = (void *)ucode_raw->data;
279 struct iwl_ucode_tlv *tlv;
280 size_t len = ucode_raw->size;
281 const u8 *data;
282 int wanted_alternative = iwlagn_mod_params.wanted_ucode_alternative;
283 int tmp;
284 u64 alternatives;
285 u32 tlv_len;
286 enum iwl_ucode_tlv_type tlv_type;
287 const u8 *tlv_data;
288 char buildstr[25];
289 u32 build;
290
291 if (len < sizeof(*ucode)) {
Johannes Berg965974a2012-03-06 13:30:38 -0800292 IWL_ERR(drv, "uCode has invalid length: %zd\n", len);
Johannes Berg15854ef2012-03-05 11:24:50 -0800293 return -EINVAL;
294 }
295
296 if (ucode->magic != cpu_to_le32(IWL_TLV_UCODE_MAGIC)) {
Johannes Berg965974a2012-03-06 13:30:38 -0800297 IWL_ERR(drv, "invalid uCode magic: 0X%x\n",
Johannes Berg15854ef2012-03-05 11:24:50 -0800298 le32_to_cpu(ucode->magic));
299 return -EINVAL;
300 }
301
302 /*
303 * Check which alternatives are present, and "downgrade"
304 * when the chosen alternative is not present, warning
305 * the user when that happens. Some files may not have
306 * any alternatives, so don't warn in that case.
307 */
308 alternatives = le64_to_cpu(ucode->alternatives);
309 tmp = wanted_alternative;
310 if (wanted_alternative > 63)
311 wanted_alternative = 63;
312 while (wanted_alternative && !(alternatives & BIT(wanted_alternative)))
313 wanted_alternative--;
314 if (wanted_alternative && wanted_alternative != tmp)
Johannes Berg965974a2012-03-06 13:30:38 -0800315 IWL_WARN(drv,
Johannes Berg15854ef2012-03-05 11:24:50 -0800316 "uCode alternative %d not available, choosing %d\n",
317 tmp, wanted_alternative);
318
Johannes Berg965974a2012-03-06 13:30:38 -0800319 drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
Johannes Berg15854ef2012-03-05 11:24:50 -0800320 build = le32_to_cpu(ucode->build);
321
322 if (build)
323 sprintf(buildstr, " build %u%s", build,
Johannes Berg965974a2012-03-06 13:30:38 -0800324 (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
Johannes Berg15854ef2012-03-05 11:24:50 -0800325 ? " (EXP)" : "");
326 else
327 buildstr[0] = '\0';
328
Johannes Berg965974a2012-03-06 13:30:38 -0800329 snprintf(drv->fw.fw_version,
330 sizeof(drv->fw.fw_version),
Johannes Berg15854ef2012-03-05 11:24:50 -0800331 "%u.%u.%u.%u%s",
Johannes Berg965974a2012-03-06 13:30:38 -0800332 IWL_UCODE_MAJOR(drv->fw.ucode_ver),
333 IWL_UCODE_MINOR(drv->fw.ucode_ver),
334 IWL_UCODE_API(drv->fw.ucode_ver),
335 IWL_UCODE_SERIAL(drv->fw.ucode_ver),
Johannes Berg15854ef2012-03-05 11:24:50 -0800336 buildstr);
337
338 data = ucode->data;
339
340 len -= sizeof(*ucode);
341
342 while (len >= sizeof(*tlv)) {
343 u16 tlv_alt;
344
345 len -= sizeof(*tlv);
346 tlv = (void *)data;
347
348 tlv_len = le32_to_cpu(tlv->length);
349 tlv_type = le16_to_cpu(tlv->type);
350 tlv_alt = le16_to_cpu(tlv->alternative);
351 tlv_data = tlv->data;
352
353 if (len < tlv_len) {
Johannes Berg965974a2012-03-06 13:30:38 -0800354 IWL_ERR(drv, "invalid TLV len: %zd/%u\n",
Johannes Berg15854ef2012-03-05 11:24:50 -0800355 len, tlv_len);
356 return -EINVAL;
357 }
358 len -= ALIGN(tlv_len, 4);
359 data += sizeof(*tlv) + ALIGN(tlv_len, 4);
360
361 /*
362 * Alternative 0 is always valid.
363 *
364 * Skip alternative TLVs that are not selected.
365 */
366 if (tlv_alt != 0 && tlv_alt != wanted_alternative)
367 continue;
368
369 switch (tlv_type) {
370 case IWL_UCODE_TLV_INST:
371 pieces->inst = tlv_data;
372 pieces->inst_size = tlv_len;
373 break;
374 case IWL_UCODE_TLV_DATA:
375 pieces->data = tlv_data;
376 pieces->data_size = tlv_len;
377 break;
378 case IWL_UCODE_TLV_INIT:
379 pieces->init = tlv_data;
380 pieces->init_size = tlv_len;
381 break;
382 case IWL_UCODE_TLV_INIT_DATA:
383 pieces->init_data = tlv_data;
384 pieces->init_data_size = tlv_len;
385 break;
386 case IWL_UCODE_TLV_BOOT:
Johannes Berg965974a2012-03-06 13:30:38 -0800387 IWL_ERR(drv, "Found unexpected BOOT ucode\n");
Johannes Berg15854ef2012-03-05 11:24:50 -0800388 break;
389 case IWL_UCODE_TLV_PROBE_MAX_LEN:
390 if (tlv_len != sizeof(u32))
391 goto invalid_tlv_len;
392 capa->max_probe_length =
393 le32_to_cpup((__le32 *)tlv_data);
394 break;
395 case IWL_UCODE_TLV_PAN:
396 if (tlv_len)
397 goto invalid_tlv_len;
398 capa->flags |= IWL_UCODE_TLV_FLAGS_PAN;
399 break;
400 case IWL_UCODE_TLV_FLAGS:
401 /* must be at least one u32 */
402 if (tlv_len < sizeof(u32))
403 goto invalid_tlv_len;
404 /* and a proper number of u32s */
405 if (tlv_len % sizeof(u32))
406 goto invalid_tlv_len;
407 /*
408 * This driver only reads the first u32 as
409 * right now no more features are defined,
410 * if that changes then either the driver
411 * will not work with the new firmware, or
412 * it'll not take advantage of new features.
413 */
414 capa->flags = le32_to_cpup((__le32 *)tlv_data);
415 break;
416 case IWL_UCODE_TLV_INIT_EVTLOG_PTR:
417 if (tlv_len != sizeof(u32))
418 goto invalid_tlv_len;
419 pieces->init_evtlog_ptr =
420 le32_to_cpup((__le32 *)tlv_data);
421 break;
422 case IWL_UCODE_TLV_INIT_EVTLOG_SIZE:
423 if (tlv_len != sizeof(u32))
424 goto invalid_tlv_len;
425 pieces->init_evtlog_size =
426 le32_to_cpup((__le32 *)tlv_data);
427 break;
428 case IWL_UCODE_TLV_INIT_ERRLOG_PTR:
429 if (tlv_len != sizeof(u32))
430 goto invalid_tlv_len;
431 pieces->init_errlog_ptr =
432 le32_to_cpup((__le32 *)tlv_data);
433 break;
434 case IWL_UCODE_TLV_RUNT_EVTLOG_PTR:
435 if (tlv_len != sizeof(u32))
436 goto invalid_tlv_len;
437 pieces->inst_evtlog_ptr =
438 le32_to_cpup((__le32 *)tlv_data);
439 break;
440 case IWL_UCODE_TLV_RUNT_EVTLOG_SIZE:
441 if (tlv_len != sizeof(u32))
442 goto invalid_tlv_len;
443 pieces->inst_evtlog_size =
444 le32_to_cpup((__le32 *)tlv_data);
445 break;
446 case IWL_UCODE_TLV_RUNT_ERRLOG_PTR:
447 if (tlv_len != sizeof(u32))
448 goto invalid_tlv_len;
449 pieces->inst_errlog_ptr =
450 le32_to_cpup((__le32 *)tlv_data);
451 break;
452 case IWL_UCODE_TLV_ENHANCE_SENS_TBL:
453 if (tlv_len)
454 goto invalid_tlv_len;
Johannes Berg965974a2012-03-06 13:30:38 -0800455 drv->fw.enhance_sensitivity_table = true;
Johannes Berg15854ef2012-03-05 11:24:50 -0800456 break;
457 case IWL_UCODE_TLV_WOWLAN_INST:
458 pieces->wowlan_inst = tlv_data;
459 pieces->wowlan_inst_size = tlv_len;
460 break;
461 case IWL_UCODE_TLV_WOWLAN_DATA:
462 pieces->wowlan_data = tlv_data;
463 pieces->wowlan_data_size = tlv_len;
464 break;
465 case IWL_UCODE_TLV_PHY_CALIBRATION_SIZE:
466 if (tlv_len != sizeof(u32))
467 goto invalid_tlv_len;
468 capa->standard_phy_calibration_size =
469 le32_to_cpup((__le32 *)tlv_data);
470 break;
471 default:
Johannes Berg965974a2012-03-06 13:30:38 -0800472 IWL_DEBUG_INFO(drv, "unknown TLV: %d\n", tlv_type);
Johannes Berg15854ef2012-03-05 11:24:50 -0800473 break;
474 }
475 }
476
477 if (len) {
Johannes Berg965974a2012-03-06 13:30:38 -0800478 IWL_ERR(drv, "invalid TLV after parsing: %zd\n", len);
479 iwl_print_hex_dump(drv, IWL_DL_FW, (u8 *)data, len);
Johannes Berg15854ef2012-03-05 11:24:50 -0800480 return -EINVAL;
481 }
482
483 return 0;
484
485 invalid_tlv_len:
Johannes Berg965974a2012-03-06 13:30:38 -0800486 IWL_ERR(drv, "TLV %d has invalid size: %u\n", tlv_type, tlv_len);
487 iwl_print_hex_dump(drv, IWL_DL_FW, tlv_data, tlv_len);
Johannes Berg15854ef2012-03-05 11:24:50 -0800488
489 return -EINVAL;
490}
491
492/**
493 * iwl_ucode_callback - callback when firmware was loaded
494 *
495 * If loaded successfully, copies the firmware into buffers
496 * for the card to fetch (via DMA).
497 */
498static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context)
499{
Johannes Berg965974a2012-03-06 13:30:38 -0800500 struct iwl_drv *drv = context;
501 const struct iwl_cfg *cfg = cfg(drv);
502 struct iwl_fw *fw = &drv->fw;
Johannes Berg15854ef2012-03-05 11:24:50 -0800503 struct iwl_ucode_header *ucode;
504 int err;
505 struct iwlagn_firmware_pieces pieces;
506 const unsigned int api_max = cfg->ucode_api_max;
507 unsigned int api_ok = cfg->ucode_api_ok;
508 const unsigned int api_min = cfg->ucode_api_min;
509 u32 api_ver;
510
511 fw->ucode_capa.max_probe_length = 200;
512 fw->ucode_capa.standard_phy_calibration_size =
513 IWL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE;
514
515 if (!api_ok)
516 api_ok = api_max;
517
518 memset(&pieces, 0, sizeof(pieces));
519
520 if (!ucode_raw) {
Johannes Berg965974a2012-03-06 13:30:38 -0800521 if (drv->fw_index <= api_ok)
522 IWL_ERR(drv,
Johannes Berg15854ef2012-03-05 11:24:50 -0800523 "request for firmware file '%s' failed.\n",
Johannes Berg965974a2012-03-06 13:30:38 -0800524 drv->firmware_name);
Johannes Berg15854ef2012-03-05 11:24:50 -0800525 goto try_again;
526 }
527
Johannes Berg965974a2012-03-06 13:30:38 -0800528 IWL_DEBUG_INFO(drv, "Loaded firmware file '%s' (%zd bytes).\n",
529 drv->firmware_name, ucode_raw->size);
Johannes Berg15854ef2012-03-05 11:24:50 -0800530
531 /* Make sure that we got at least the API version number */
532 if (ucode_raw->size < 4) {
Johannes Berg965974a2012-03-06 13:30:38 -0800533 IWL_ERR(drv, "File size way too small!\n");
Johannes Berg15854ef2012-03-05 11:24:50 -0800534 goto try_again;
535 }
536
537 /* Data from ucode file: header followed by uCode images */
538 ucode = (struct iwl_ucode_header *)ucode_raw->data;
539
540 if (ucode->ver)
Johannes Berg965974a2012-03-06 13:30:38 -0800541 err = iwl_parse_v1_v2_firmware(drv, ucode_raw, &pieces);
Johannes Berg15854ef2012-03-05 11:24:50 -0800542 else
Johannes Berg965974a2012-03-06 13:30:38 -0800543 err = iwl_parse_tlv_firmware(drv, ucode_raw, &pieces,
Johannes Berg15854ef2012-03-05 11:24:50 -0800544 &fw->ucode_capa);
545
546 if (err)
547 goto try_again;
548
Johannes Berg965974a2012-03-06 13:30:38 -0800549 api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
Johannes Berg15854ef2012-03-05 11:24:50 -0800550
551 /*
552 * api_ver should match the api version forming part of the
553 * firmware filename ... but we don't check for that and only rely
554 * on the API version read from firmware header from here on forward
555 */
556 /* no api version check required for experimental uCode */
Johannes Berg965974a2012-03-06 13:30:38 -0800557 if (drv->fw_index != UCODE_EXPERIMENTAL_INDEX) {
Johannes Berg15854ef2012-03-05 11:24:50 -0800558 if (api_ver < api_min || api_ver > api_max) {
Johannes Berg965974a2012-03-06 13:30:38 -0800559 IWL_ERR(drv,
Johannes Berg15854ef2012-03-05 11:24:50 -0800560 "Driver unable to support your firmware API. "
561 "Driver supports v%u, firmware is v%u.\n",
562 api_max, api_ver);
563 goto try_again;
564 }
565
566 if (api_ver < api_ok) {
567 if (api_ok != api_max)
Johannes Berg965974a2012-03-06 13:30:38 -0800568 IWL_ERR(drv, "Firmware has old API version, "
Johannes Berg15854ef2012-03-05 11:24:50 -0800569 "expected v%u through v%u, got v%u.\n",
570 api_ok, api_max, api_ver);
571 else
Johannes Berg965974a2012-03-06 13:30:38 -0800572 IWL_ERR(drv, "Firmware has old API version, "
Johannes Berg15854ef2012-03-05 11:24:50 -0800573 "expected v%u, got v%u.\n",
574 api_max, api_ver);
Johannes Berg965974a2012-03-06 13:30:38 -0800575 IWL_ERR(drv, "New firmware can be obtained from "
Johannes Berg15854ef2012-03-05 11:24:50 -0800576 "http://www.intellinuxwireless.org/.\n");
577 }
578 }
579
Johannes Berg965974a2012-03-06 13:30:38 -0800580 IWL_INFO(drv, "loaded firmware version %s", drv->fw.fw_version);
Johannes Berg15854ef2012-03-05 11:24:50 -0800581
582 /*
583 * For any of the failures below (before allocating pci memory)
584 * we will try to load a version with a smaller API -- maybe the
585 * user just got a corrupted version of the latest API.
586 */
587
Johannes Berg965974a2012-03-06 13:30:38 -0800588 IWL_DEBUG_INFO(drv, "f/w package hdr ucode version raw = 0x%x\n",
589 drv->fw.ucode_ver);
590 IWL_DEBUG_INFO(drv, "f/w package hdr runtime inst size = %Zd\n",
Johannes Berg15854ef2012-03-05 11:24:50 -0800591 pieces.inst_size);
Johannes Berg965974a2012-03-06 13:30:38 -0800592 IWL_DEBUG_INFO(drv, "f/w package hdr runtime data size = %Zd\n",
Johannes Berg15854ef2012-03-05 11:24:50 -0800593 pieces.data_size);
Johannes Berg965974a2012-03-06 13:30:38 -0800594 IWL_DEBUG_INFO(drv, "f/w package hdr init inst size = %Zd\n",
Johannes Berg15854ef2012-03-05 11:24:50 -0800595 pieces.init_size);
Johannes Berg965974a2012-03-06 13:30:38 -0800596 IWL_DEBUG_INFO(drv, "f/w package hdr init data size = %Zd\n",
Johannes Berg15854ef2012-03-05 11:24:50 -0800597 pieces.init_data_size);
598
599 /* Verify that uCode images will fit in card's SRAM */
600 if (pieces.inst_size > cfg->max_inst_size) {
Johannes Berg965974a2012-03-06 13:30:38 -0800601 IWL_ERR(drv, "uCode instr len %Zd too large to fit in\n",
Johannes Berg15854ef2012-03-05 11:24:50 -0800602 pieces.inst_size);
603 goto try_again;
604 }
605
606 if (pieces.data_size > cfg->max_data_size) {
Johannes Berg965974a2012-03-06 13:30:38 -0800607 IWL_ERR(drv, "uCode data len %Zd too large to fit in\n",
Johannes Berg15854ef2012-03-05 11:24:50 -0800608 pieces.data_size);
609 goto try_again;
610 }
611
612 if (pieces.init_size > cfg->max_inst_size) {
Johannes Berg965974a2012-03-06 13:30:38 -0800613 IWL_ERR(drv, "uCode init instr len %Zd too large to fit in\n",
Johannes Berg15854ef2012-03-05 11:24:50 -0800614 pieces.init_size);
615 goto try_again;
616 }
617
618 if (pieces.init_data_size > cfg->max_data_size) {
Johannes Berg965974a2012-03-06 13:30:38 -0800619 IWL_ERR(drv, "uCode init data len %Zd too large to fit in\n",
Johannes Berg15854ef2012-03-05 11:24:50 -0800620 pieces.init_data_size);
621 goto try_again;
622 }
623
624 /* Allocate ucode buffers for card's bus-master loading ... */
625
626 /* Runtime instructions and 2 copies of data:
627 * 1) unmodified from disk
628 * 2) backup cache for save/restore during power-downs */
Johannes Berg965974a2012-03-06 13:30:38 -0800629 if (iwl_alloc_fw_desc(drv, &drv->fw.ucode_rt.code,
Johannes Berg15854ef2012-03-05 11:24:50 -0800630 pieces.inst, pieces.inst_size))
631 goto err_pci_alloc;
Johannes Berg965974a2012-03-06 13:30:38 -0800632 if (iwl_alloc_fw_desc(drv, &drv->fw.ucode_rt.data,
Johannes Berg15854ef2012-03-05 11:24:50 -0800633 pieces.data, pieces.data_size))
634 goto err_pci_alloc;
635
636 /* Initialization instructions and data */
637 if (pieces.init_size && pieces.init_data_size) {
Johannes Berg965974a2012-03-06 13:30:38 -0800638 if (iwl_alloc_fw_desc(drv,
639 &drv->fw.ucode_init.code,
Johannes Berg15854ef2012-03-05 11:24:50 -0800640 pieces.init, pieces.init_size))
641 goto err_pci_alloc;
Johannes Berg965974a2012-03-06 13:30:38 -0800642 if (iwl_alloc_fw_desc(drv,
643 &drv->fw.ucode_init.data,
Johannes Berg15854ef2012-03-05 11:24:50 -0800644 pieces.init_data, pieces.init_data_size))
645 goto err_pci_alloc;
646 }
647
648 /* WoWLAN instructions and data */
649 if (pieces.wowlan_inst_size && pieces.wowlan_data_size) {
Johannes Berg965974a2012-03-06 13:30:38 -0800650 if (iwl_alloc_fw_desc(drv,
651 &drv->fw.ucode_wowlan.code,
Johannes Berg15854ef2012-03-05 11:24:50 -0800652 pieces.wowlan_inst,
653 pieces.wowlan_inst_size))
654 goto err_pci_alloc;
Johannes Berg965974a2012-03-06 13:30:38 -0800655 if (iwl_alloc_fw_desc(drv,
656 &drv->fw.ucode_wowlan.data,
Johannes Berg15854ef2012-03-05 11:24:50 -0800657 pieces.wowlan_data,
658 pieces.wowlan_data_size))
659 goto err_pci_alloc;
660 }
661
662 /* Now that we can no longer fail, copy information */
663
664 /*
665 * The (size - 16) / 12 formula is based on the information recorded
666 * for each event, which is of mode 1 (including timestamp) for all
667 * new microcodes that include this information.
668 */
Johannes Berg0692fe42012-03-06 13:30:37 -0800669 fw->init_evtlog_ptr = pieces.init_evtlog_ptr;
Johannes Berg15854ef2012-03-05 11:24:50 -0800670 if (pieces.init_evtlog_size)
Johannes Berg0692fe42012-03-06 13:30:37 -0800671 fw->init_evtlog_size = (pieces.init_evtlog_size - 16)/12;
Johannes Berg15854ef2012-03-05 11:24:50 -0800672 else
Johannes Berg0692fe42012-03-06 13:30:37 -0800673 fw->init_evtlog_size =
Johannes Berg15854ef2012-03-05 11:24:50 -0800674 cfg->base_params->max_event_log_size;
Johannes Berg0692fe42012-03-06 13:30:37 -0800675 fw->init_errlog_ptr = pieces.init_errlog_ptr;
676 fw->inst_evtlog_ptr = pieces.inst_evtlog_ptr;
Johannes Berg15854ef2012-03-05 11:24:50 -0800677 if (pieces.inst_evtlog_size)
Johannes Berg0692fe42012-03-06 13:30:37 -0800678 fw->inst_evtlog_size = (pieces.inst_evtlog_size - 16)/12;
Johannes Berg15854ef2012-03-05 11:24:50 -0800679 else
Johannes Berg0692fe42012-03-06 13:30:37 -0800680 fw->inst_evtlog_size =
Johannes Berg15854ef2012-03-05 11:24:50 -0800681 cfg->base_params->max_event_log_size;
Johannes Berg0692fe42012-03-06 13:30:37 -0800682 fw->inst_errlog_ptr = pieces.inst_errlog_ptr;
Johannes Berg15854ef2012-03-05 11:24:50 -0800683
684 /*
685 * figure out the offset of chain noise reset and gain commands
686 * base on the size of standard phy calibration commands table size
687 */
688 if (fw->ucode_capa.standard_phy_calibration_size >
689 IWL_MAX_PHY_CALIBRATE_TBL_SIZE)
690 fw->ucode_capa.standard_phy_calibration_size =
691 IWL_MAX_STANDARD_PHY_CALIBRATE_TBL_SIZE;
692
693 /* We have our copies now, allow OS release its copies */
694 release_firmware(ucode_raw);
Johannes Berg965974a2012-03-06 13:30:38 -0800695 complete(&drv->request_firmware_complete);
Johannes Berg15854ef2012-03-05 11:24:50 -0800696
Johannes Berg965974a2012-03-06 13:30:38 -0800697 drv->op_mode = iwl_dvm_ops.start(drv->shrd->trans, &drv->fw);
Johannes Berg15854ef2012-03-05 11:24:50 -0800698
Johannes Berg965974a2012-03-06 13:30:38 -0800699 if (!drv->op_mode)
Johannes Berg15854ef2012-03-05 11:24:50 -0800700 goto out_unbind;
701
702 return;
703
704 try_again:
705 /* try next, if any */
706 release_firmware(ucode_raw);
Johannes Berg965974a2012-03-06 13:30:38 -0800707 if (iwl_request_firmware(drv, false))
Johannes Berg15854ef2012-03-05 11:24:50 -0800708 goto out_unbind;
709 return;
710
711 err_pci_alloc:
Johannes Berg965974a2012-03-06 13:30:38 -0800712 IWL_ERR(drv, "failed to allocate pci memory\n");
713 iwl_dealloc_ucode(drv);
Johannes Berg15854ef2012-03-05 11:24:50 -0800714 release_firmware(ucode_raw);
715 out_unbind:
Johannes Berg965974a2012-03-06 13:30:38 -0800716 complete(&drv->request_firmware_complete);
717 device_release_driver(trans(drv)->dev);
Johannes Berg15854ef2012-03-05 11:24:50 -0800718}
719
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200720int iwl_drv_start(struct iwl_shared *shrd,
Johannes Berg706c4ff2012-03-05 11:24:33 -0800721 struct iwl_trans *trans, const struct iwl_cfg *cfg)
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200722{
Johannes Berg965974a2012-03-06 13:30:38 -0800723 struct iwl_drv *drv;
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200724 int ret;
725
726 shrd->cfg = cfg;
727
Johannes Berg965974a2012-03-06 13:30:38 -0800728 drv = kzalloc(sizeof(*drv), GFP_KERNEL);
729 if (!drv) {
730 dev_printk(KERN_ERR, trans->dev, "Couldn't allocate iwl_drv");
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200731 return -ENOMEM;
732 }
Johannes Berg965974a2012-03-06 13:30:38 -0800733 drv->shrd = shrd;
734 shrd->drv = drv;
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200735
Johannes Berg965974a2012-03-06 13:30:38 -0800736 init_completion(&drv->request_firmware_complete);
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200737
Johannes Berg965974a2012-03-06 13:30:38 -0800738 ret = iwl_request_firmware(drv, true);
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200739
740 if (ret) {
741 dev_printk(KERN_ERR, trans->dev, "Couldn't request the fw");
Johannes Berg965974a2012-03-06 13:30:38 -0800742 kfree(drv);
743 shrd->drv = NULL;
Emmanuel Grumbach5c58edc2012-02-07 14:18:40 +0200744 }
745
746 return ret;
747}
748
Emmanuel Grumbach07590f02012-02-07 14:27:31 +0200749void iwl_drv_stop(struct iwl_shared *shrd)
750{
Johannes Berg965974a2012-03-06 13:30:38 -0800751 struct iwl_drv *drv = shrd->drv;
Johannes Berg0692fe42012-03-06 13:30:37 -0800752
Johannes Berg965974a2012-03-06 13:30:38 -0800753 wait_for_completion(&drv->request_firmware_complete);
Johannes Berg2e7eb112012-03-05 11:24:51 -0800754
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +0200755 /* op_mode can be NULL if its start failed */
Johannes Berg965974a2012-03-06 13:30:38 -0800756 if (drv->op_mode)
757 iwl_op_mode_stop(drv->op_mode);
Emmanuel Grumbach07590f02012-02-07 14:27:31 +0200758
Johannes Berg965974a2012-03-06 13:30:38 -0800759 iwl_dealloc_ucode(drv);
Johannes Berg7db5b982012-03-05 11:24:48 -0800760
Johannes Berg965974a2012-03-06 13:30:38 -0800761 kfree(drv);
762 shrd->drv = NULL;
Emmanuel Grumbach07590f02012-02-07 14:27:31 +0200763}