]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/net/wireless/iwlwifi/iwl-drv.c
iwlwifi: remove iwl_tx_queue declaration
[mirror_ubuntu-jammy-kernel.git] / drivers / net / wireless / iwlwifi / iwl-drv.c
CommitLineData
5c58edc6
EG
1/******************************************************************************
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>
15854ef9
JB
64#include <linux/dma-mapping.h>
65#include <linux/firmware.h>
66#include <linux/module.h>
5c58edc6
EG
67
68#include "iwl-drv.h"
69#include "iwl-trans.h"
15854ef9 70#include "iwl-shared.h"
d0f76d68 71#include "iwl-op-mode.h"
0cedacc5 72#include "iwl-agn-hw.h"
5c58edc6 73
0692fe41 74/* private includes */
3995deaf 75#include "iwl-fw-file.h"
0692fe41 76
965974a6
JB
77/**
78 * struct iwl_drv - drv common data
79 * @fw: the iwl_fw structure
80 * @shrd: pointer to common shared structure
81 * @op_mode: the running op_mode
82 * @fw_index: firmware revision to try loading
83 * @firmware_name: composite filename of ucode file to load
84 * @request_firmware_complete: the firmware has been obtained from user space
85 */
86struct iwl_drv {
87 struct iwl_fw fw;
88
89 struct iwl_shared *shrd;
90 struct iwl_op_mode *op_mode;
91
92 int fw_index; /* firmware we're trying to load */
93 char firmware_name[25]; /* name of firmware file to load */
94
95 struct completion request_firmware_complete;
96};
97
98
99
0cedacc5
DS
100/*
101 * struct fw_sec: Just for the image parsing proccess.
102 * For the fw storage we are using struct fw_desc.
103 */
104struct fw_sec {
105 const void *data; /* the sec data */
106 size_t size; /* section size */
107 u32 offset; /* offset of writing in the device */
108};
109
965974a6 110static void iwl_free_fw_desc(struct iwl_drv *drv, struct fw_desc *desc)
15854ef9
JB
111{
112 if (desc->v_addr)
965974a6 113 dma_free_coherent(trans(drv)->dev, desc->len,
15854ef9
JB
114 desc->v_addr, desc->p_addr);
115 desc->v_addr = NULL;
116 desc->len = 0;
117}
118
965974a6 119static void iwl_free_fw_img(struct iwl_drv *drv, struct fw_img *img)
15854ef9 120{
6dfa8d01
DS
121 int i;
122 for (i = 0; i < IWL_UCODE_SECTION_MAX; i++)
123 iwl_free_fw_desc(drv, &img->sec[i]);
15854ef9
JB
124}
125
965974a6 126static void iwl_dealloc_ucode(struct iwl_drv *drv)
15854ef9 127{
6dfa8d01
DS
128 int i;
129 for (i = 0; i < IWL_UCODE_TYPE_MAX; i++)
130 iwl_free_fw_img(drv, drv->fw.img + i);
15854ef9
JB
131}
132
965974a6 133static int iwl_alloc_fw_desc(struct iwl_drv *drv, struct fw_desc *desc,
0cedacc5 134 struct fw_sec *sec)
15854ef9 135{
0cedacc5 136 if (!sec || !sec->size) {
15854ef9
JB
137 desc->v_addr = NULL;
138 return -EINVAL;
139 }
140
0cedacc5 141 desc->v_addr = dma_alloc_coherent(trans(drv)->dev, sec->size,
15854ef9
JB
142 &desc->p_addr, GFP_KERNEL);
143 if (!desc->v_addr)
144 return -ENOMEM;
145
0cedacc5
DS
146 desc->len = sec->size;
147 desc->offset = sec->offset;
148 memcpy(desc->v_addr, sec->data, sec->size);
15854ef9
JB
149 return 0;
150}
151
152static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context);
153
154#define UCODE_EXPERIMENTAL_INDEX 100
155#define UCODE_EXPERIMENTAL_TAG "exp"
156
965974a6 157static int iwl_request_firmware(struct iwl_drv *drv, bool first)
15854ef9 158{
965974a6 159 const struct iwl_cfg *cfg = cfg(drv);
15854ef9
JB
160 const char *name_pre = cfg->fw_name_pre;
161 char tag[8];
162
163 if (first) {
164#ifdef CONFIG_IWLWIFI_DEBUG_EXPERIMENTAL_UCODE
965974a6 165 drv->fw_index = UCODE_EXPERIMENTAL_INDEX;
15854ef9 166 strcpy(tag, UCODE_EXPERIMENTAL_TAG);
965974a6 167 } else if (drv->fw_index == UCODE_EXPERIMENTAL_INDEX) {
15854ef9 168#endif
965974a6
JB
169 drv->fw_index = cfg->ucode_api_max;
170 sprintf(tag, "%d", drv->fw_index);
15854ef9 171 } else {
965974a6
JB
172 drv->fw_index--;
173 sprintf(tag, "%d", drv->fw_index);
15854ef9
JB
174 }
175
965974a6
JB
176 if (drv->fw_index < cfg->ucode_api_min) {
177 IWL_ERR(drv, "no suitable firmware found!\n");
15854ef9
JB
178 return -ENOENT;
179 }
180
965974a6 181 sprintf(drv->firmware_name, "%s%s%s", name_pre, tag, ".ucode");
15854ef9 182
965974a6
JB
183 IWL_DEBUG_INFO(drv, "attempting to load firmware %s'%s'\n",
184 (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
15854ef9 185 ? "EXPERIMENTAL " : "",
965974a6 186 drv->firmware_name);
15854ef9 187
965974a6
JB
188 return request_firmware_nowait(THIS_MODULE, 1, drv->firmware_name,
189 trans(drv)->dev,
190 GFP_KERNEL, drv, iwl_ucode_callback);
15854ef9
JB
191}
192
0cedacc5 193struct fw_img_parsing {
6dfa8d01 194 struct fw_sec sec[IWL_UCODE_SECTION_MAX];
0cedacc5
DS
195 int sec_counter;
196};
197
ed8c8365
DS
198/*
199 * struct fw_sec_parsing: to extract fw section and it's offset from tlv
200 */
201struct fw_sec_parsing {
202 __le32 offset;
203 const u8 data[];
204} __packed;
205
206/**
207 * struct iwl_tlv_calib_data - parse the default calib data from TLV
208 *
209 * @ucode_type: the uCode to which the following default calib relates.
210 * @calib: default calibrations.
211 */
212struct iwl_tlv_calib_data {
213 __le32 ucode_type;
214 __le64 calib;
215} __packed;
216
0cedacc5
DS
217struct iwl_firmware_pieces {
218 struct fw_img_parsing img[IWL_UCODE_TYPE_MAX];
15854ef9
JB
219
220 u32 init_evtlog_ptr, init_evtlog_size, init_errlog_ptr;
221 u32 inst_evtlog_ptr, inst_evtlog_size, inst_errlog_ptr;
222};
223
0cedacc5
DS
224/*
225 * These functions are just to extract uCode section data from the pieces
226 * structure.
227 */
228static struct fw_sec *get_sec(struct iwl_firmware_pieces *pieces,
229 enum iwl_ucode_type type,
230 int sec)
231{
232 return &pieces->img[type].sec[sec];
233}
234
235static void set_sec_data(struct iwl_firmware_pieces *pieces,
236 enum iwl_ucode_type type,
237 int sec,
238 const void *data)
239{
240 pieces->img[type].sec[sec].data = data;
241}
242
243static void set_sec_size(struct iwl_firmware_pieces *pieces,
244 enum iwl_ucode_type type,
245 int sec,
246 size_t size)
247{
248 pieces->img[type].sec[sec].size = size;
249}
250
251static size_t get_sec_size(struct iwl_firmware_pieces *pieces,
252 enum iwl_ucode_type type,
253 int sec)
254{
255 return pieces->img[type].sec[sec].size;
256}
257
258static void set_sec_offset(struct iwl_firmware_pieces *pieces,
259 enum iwl_ucode_type type,
260 int sec,
261 u32 offset)
262{
263 pieces->img[type].sec[sec].offset = offset;
264}
265
ed8c8365
DS
266/*
267 * Gets uCode section from tlv.
268 */
269static int iwl_store_ucode_sec(struct iwl_firmware_pieces *pieces,
270 const void *data, enum iwl_ucode_type type,
271 int size)
272{
273 struct fw_img_parsing *img;
274 struct fw_sec *sec;
275 struct fw_sec_parsing *sec_parse;
276
277 if (WARN_ON(!pieces || !data || type >= IWL_UCODE_TYPE_MAX))
278 return -1;
279
280 sec_parse = (struct fw_sec_parsing *)data;
281
282 img = &pieces->img[type];
283 sec = &img->sec[img->sec_counter];
284
285 sec->offset = le32_to_cpu(sec_parse->offset);
286 sec->data = sec_parse->data;
287
288 ++img->sec_counter;
289
290 return 0;
291}
292
293static int iwl_set_default_calib(struct iwl_drv *drv, const u8 *data)
294{
295 struct iwl_tlv_calib_data *def_calib =
296 (struct iwl_tlv_calib_data *)data;
297 u32 ucode_type = le32_to_cpu(def_calib->ucode_type);
298 if (ucode_type >= IWL_UCODE_TYPE_MAX) {
299 IWL_ERR(drv, "Wrong ucode_type %u for default calibration.\n",
300 ucode_type);
301 return -EINVAL;
302 }
303 drv->fw.default_calib[ucode_type] = le64_to_cpu(def_calib->calib);
304 return 0;
305}
306
965974a6 307static int iwl_parse_v1_v2_firmware(struct iwl_drv *drv,
0cedacc5
DS
308 const struct firmware *ucode_raw,
309 struct iwl_firmware_pieces *pieces)
15854ef9
JB
310{
311 struct iwl_ucode_header *ucode = (void *)ucode_raw->data;
312 u32 api_ver, hdr_size, build;
313 char buildstr[25];
314 const u8 *src;
315
965974a6
JB
316 drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
317 api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
15854ef9
JB
318
319 switch (api_ver) {
320 default:
321 hdr_size = 28;
322 if (ucode_raw->size < hdr_size) {
965974a6 323 IWL_ERR(drv, "File size too small!\n");
15854ef9
JB
324 return -EINVAL;
325 }
326 build = le32_to_cpu(ucode->u.v2.build);
0cedacc5
DS
327 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
328 le32_to_cpu(ucode->u.v2.inst_size));
329 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
330 le32_to_cpu(ucode->u.v2.data_size));
331 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
332 le32_to_cpu(ucode->u.v2.init_size));
333 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
334 le32_to_cpu(ucode->u.v2.init_data_size));
15854ef9
JB
335 src = ucode->u.v2.data;
336 break;
337 case 0:
338 case 1:
339 case 2:
340 hdr_size = 24;
341 if (ucode_raw->size < hdr_size) {
965974a6 342 IWL_ERR(drv, "File size too small!\n");
15854ef9
JB
343 return -EINVAL;
344 }
345 build = 0;
0cedacc5
DS
346 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
347 le32_to_cpu(ucode->u.v1.inst_size));
348 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
349 le32_to_cpu(ucode->u.v1.data_size));
350 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
351 le32_to_cpu(ucode->u.v1.init_size));
352 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
353 le32_to_cpu(ucode->u.v1.init_data_size));
15854ef9
JB
354 src = ucode->u.v1.data;
355 break;
356 }
357
358 if (build)
359 sprintf(buildstr, " build %u%s", build,
965974a6 360 (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
15854ef9
JB
361 ? " (EXP)" : "");
362 else
363 buildstr[0] = '\0';
364
965974a6
JB
365 snprintf(drv->fw.fw_version,
366 sizeof(drv->fw.fw_version),
15854ef9 367 "%u.%u.%u.%u%s",
965974a6
JB
368 IWL_UCODE_MAJOR(drv->fw.ucode_ver),
369 IWL_UCODE_MINOR(drv->fw.ucode_ver),
370 IWL_UCODE_API(drv->fw.ucode_ver),
371 IWL_UCODE_SERIAL(drv->fw.ucode_ver),
15854ef9
JB
372 buildstr);
373
374 /* Verify size of file vs. image size info in file's header */
0cedacc5
DS
375
376 if (ucode_raw->size != hdr_size +
377 get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) +
378 get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) +
379 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) +
380 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA)) {
15854ef9 381
965974a6 382 IWL_ERR(drv,
15854ef9
JB
383 "uCode file size %d does not match expected size\n",
384 (int)ucode_raw->size);
385 return -EINVAL;
386 }
387
15854ef9 388
0cedacc5
DS
389 set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST, src);
390 src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST);
391 set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
392 IWLAGN_RTC_INST_LOWER_BOUND);
393 set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA, src);
394 src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA);
395 set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
396 IWLAGN_RTC_DATA_LOWER_BOUND);
397 set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST, src);
398 src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST);
399 set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
400 IWLAGN_RTC_INST_LOWER_BOUND);
401 set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA, src);
402 src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA);
403 set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
404 IWLAGN_RTC_DATA_LOWER_BOUND);
15854ef9
JB
405 return 0;
406}
407
965974a6 408static int iwl_parse_tlv_firmware(struct iwl_drv *drv,
15854ef9 409 const struct firmware *ucode_raw,
0cedacc5 410 struct iwl_firmware_pieces *pieces,
15854ef9
JB
411 struct iwl_ucode_capabilities *capa)
412{
413 struct iwl_tlv_ucode_header *ucode = (void *)ucode_raw->data;
414 struct iwl_ucode_tlv *tlv;
415 size_t len = ucode_raw->size;
416 const u8 *data;
15854ef9
JB
417 u32 tlv_len;
418 enum iwl_ucode_tlv_type tlv_type;
419 const u8 *tlv_data;
420 char buildstr[25];
421 u32 build;
422
423 if (len < sizeof(*ucode)) {
965974a6 424 IWL_ERR(drv, "uCode has invalid length: %zd\n", len);
15854ef9
JB
425 return -EINVAL;
426 }
427
428 if (ucode->magic != cpu_to_le32(IWL_TLV_UCODE_MAGIC)) {
965974a6 429 IWL_ERR(drv, "invalid uCode magic: 0X%x\n",
15854ef9
JB
430 le32_to_cpu(ucode->magic));
431 return -EINVAL;
432 }
433
965974a6 434 drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
15854ef9
JB
435 build = le32_to_cpu(ucode->build);
436
437 if (build)
438 sprintf(buildstr, " build %u%s", build,
965974a6 439 (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
15854ef9
JB
440 ? " (EXP)" : "");
441 else
442 buildstr[0] = '\0';
443
965974a6
JB
444 snprintf(drv->fw.fw_version,
445 sizeof(drv->fw.fw_version),
15854ef9 446 "%u.%u.%u.%u%s",
965974a6
JB
447 IWL_UCODE_MAJOR(drv->fw.ucode_ver),
448 IWL_UCODE_MINOR(drv->fw.ucode_ver),
449 IWL_UCODE_API(drv->fw.ucode_ver),
450 IWL_UCODE_SERIAL(drv->fw.ucode_ver),
15854ef9
JB
451 buildstr);
452
453 data = ucode->data;
454
455 len -= sizeof(*ucode);
456
457 while (len >= sizeof(*tlv)) {
15854ef9
JB
458 len -= sizeof(*tlv);
459 tlv = (void *)data;
460
461 tlv_len = le32_to_cpu(tlv->length);
0479c19d 462 tlv_type = le32_to_cpu(tlv->type);
15854ef9
JB
463 tlv_data = tlv->data;
464
465 if (len < tlv_len) {
965974a6 466 IWL_ERR(drv, "invalid TLV len: %zd/%u\n",
15854ef9
JB
467 len, tlv_len);
468 return -EINVAL;
469 }
470 len -= ALIGN(tlv_len, 4);
471 data += sizeof(*tlv) + ALIGN(tlv_len, 4);
472
15854ef9
JB
473 switch (tlv_type) {
474 case IWL_UCODE_TLV_INST:
0cedacc5
DS
475 set_sec_data(pieces, IWL_UCODE_REGULAR,
476 IWL_UCODE_SECTION_INST, tlv_data);
477 set_sec_size(pieces, IWL_UCODE_REGULAR,
478 IWL_UCODE_SECTION_INST, tlv_len);
479 set_sec_offset(pieces, IWL_UCODE_REGULAR,
480 IWL_UCODE_SECTION_INST,
481 IWLAGN_RTC_INST_LOWER_BOUND);
15854ef9
JB
482 break;
483 case IWL_UCODE_TLV_DATA:
0cedacc5
DS
484 set_sec_data(pieces, IWL_UCODE_REGULAR,
485 IWL_UCODE_SECTION_DATA, tlv_data);
486 set_sec_size(pieces, IWL_UCODE_REGULAR,
487 IWL_UCODE_SECTION_DATA, tlv_len);
488 set_sec_offset(pieces, IWL_UCODE_REGULAR,
489 IWL_UCODE_SECTION_DATA,
490 IWLAGN_RTC_DATA_LOWER_BOUND);
15854ef9
JB
491 break;
492 case IWL_UCODE_TLV_INIT:
0cedacc5
DS
493 set_sec_data(pieces, IWL_UCODE_INIT,
494 IWL_UCODE_SECTION_INST, tlv_data);
495 set_sec_size(pieces, IWL_UCODE_INIT,
496 IWL_UCODE_SECTION_INST, tlv_len);
497 set_sec_offset(pieces, IWL_UCODE_INIT,
498 IWL_UCODE_SECTION_INST,
499 IWLAGN_RTC_INST_LOWER_BOUND);
15854ef9
JB
500 break;
501 case IWL_UCODE_TLV_INIT_DATA:
0cedacc5
DS
502 set_sec_data(pieces, IWL_UCODE_INIT,
503 IWL_UCODE_SECTION_DATA, tlv_data);
504 set_sec_size(pieces, IWL_UCODE_INIT,
505 IWL_UCODE_SECTION_DATA, tlv_len);
506 set_sec_offset(pieces, IWL_UCODE_INIT,
507 IWL_UCODE_SECTION_DATA,
508 IWLAGN_RTC_DATA_LOWER_BOUND);
15854ef9
JB
509 break;
510 case IWL_UCODE_TLV_BOOT:
965974a6 511 IWL_ERR(drv, "Found unexpected BOOT ucode\n");
15854ef9
JB
512 break;
513 case IWL_UCODE_TLV_PROBE_MAX_LEN:
514 if (tlv_len != sizeof(u32))
515 goto invalid_tlv_len;
516 capa->max_probe_length =
517 le32_to_cpup((__le32 *)tlv_data);
518 break;
519 case IWL_UCODE_TLV_PAN:
520 if (tlv_len)
521 goto invalid_tlv_len;
522 capa->flags |= IWL_UCODE_TLV_FLAGS_PAN;
523 break;
524 case IWL_UCODE_TLV_FLAGS:
525 /* must be at least one u32 */
526 if (tlv_len < sizeof(u32))
527 goto invalid_tlv_len;
528 /* and a proper number of u32s */
529 if (tlv_len % sizeof(u32))
530 goto invalid_tlv_len;
531 /*
532 * This driver only reads the first u32 as
533 * right now no more features are defined,
534 * if that changes then either the driver
535 * will not work with the new firmware, or
536 * it'll not take advantage of new features.
537 */
538 capa->flags = le32_to_cpup((__le32 *)tlv_data);
539 break;
540 case IWL_UCODE_TLV_INIT_EVTLOG_PTR:
541 if (tlv_len != sizeof(u32))
542 goto invalid_tlv_len;
543 pieces->init_evtlog_ptr =
544 le32_to_cpup((__le32 *)tlv_data);
545 break;
546 case IWL_UCODE_TLV_INIT_EVTLOG_SIZE:
547 if (tlv_len != sizeof(u32))
548 goto invalid_tlv_len;
549 pieces->init_evtlog_size =
550 le32_to_cpup((__le32 *)tlv_data);
551 break;
552 case IWL_UCODE_TLV_INIT_ERRLOG_PTR:
553 if (tlv_len != sizeof(u32))
554 goto invalid_tlv_len;
555 pieces->init_errlog_ptr =
556 le32_to_cpup((__le32 *)tlv_data);
557 break;
558 case IWL_UCODE_TLV_RUNT_EVTLOG_PTR:
559 if (tlv_len != sizeof(u32))
560 goto invalid_tlv_len;
561 pieces->inst_evtlog_ptr =
562 le32_to_cpup((__le32 *)tlv_data);
563 break;
564 case IWL_UCODE_TLV_RUNT_EVTLOG_SIZE:
565 if (tlv_len != sizeof(u32))
566 goto invalid_tlv_len;
567 pieces->inst_evtlog_size =
568 le32_to_cpup((__le32 *)tlv_data);
569 break;
570 case IWL_UCODE_TLV_RUNT_ERRLOG_PTR:
571 if (tlv_len != sizeof(u32))
572 goto invalid_tlv_len;
573 pieces->inst_errlog_ptr =
574 le32_to_cpup((__le32 *)tlv_data);
575 break;
576 case IWL_UCODE_TLV_ENHANCE_SENS_TBL:
577 if (tlv_len)
578 goto invalid_tlv_len;
965974a6 579 drv->fw.enhance_sensitivity_table = true;
15854ef9
JB
580 break;
581 case IWL_UCODE_TLV_WOWLAN_INST:
0cedacc5
DS
582 set_sec_data(pieces, IWL_UCODE_WOWLAN,
583 IWL_UCODE_SECTION_INST, tlv_data);
584 set_sec_size(pieces, IWL_UCODE_WOWLAN,
585 IWL_UCODE_SECTION_INST, tlv_len);
586 set_sec_offset(pieces, IWL_UCODE_WOWLAN,
587 IWL_UCODE_SECTION_INST,
588 IWLAGN_RTC_INST_LOWER_BOUND);
15854ef9
JB
589 break;
590 case IWL_UCODE_TLV_WOWLAN_DATA:
0cedacc5
DS
591 set_sec_data(pieces, IWL_UCODE_WOWLAN,
592 IWL_UCODE_SECTION_DATA, tlv_data);
593 set_sec_size(pieces, IWL_UCODE_WOWLAN,
594 IWL_UCODE_SECTION_DATA, tlv_len);
595 set_sec_offset(pieces, IWL_UCODE_WOWLAN,
596 IWL_UCODE_SECTION_DATA,
597 IWLAGN_RTC_DATA_LOWER_BOUND);
15854ef9
JB
598 break;
599 case IWL_UCODE_TLV_PHY_CALIBRATION_SIZE:
600 if (tlv_len != sizeof(u32))
601 goto invalid_tlv_len;
602 capa->standard_phy_calibration_size =
603 le32_to_cpup((__le32 *)tlv_data);
604 break;
ed8c8365
DS
605 case IWL_UCODE_TLV_SEC_RT:
606 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_REGULAR,
607 tlv_len);
4db2c9ae 608 drv->fw.mvm_fw = true;
ed8c8365
DS
609 break;
610 case IWL_UCODE_TLV_SEC_INIT:
611 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_INIT,
612 tlv_len);
4db2c9ae 613 drv->fw.mvm_fw = true;
ed8c8365
DS
614 break;
615 case IWL_UCODE_TLV_SEC_WOWLAN:
616 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_WOWLAN,
617 tlv_len);
4db2c9ae 618 drv->fw.mvm_fw = true;
ed8c8365
DS
619 break;
620 case IWL_UCODE_TLV_DEF_CALIB:
621 if (tlv_len != sizeof(struct iwl_tlv_calib_data))
622 goto invalid_tlv_len;
623 if (iwl_set_default_calib(drv, tlv_data))
624 goto tlv_error;
625 break;
626 case IWL_UCODE_TLV_PHY_SKU:
627 if (tlv_len != sizeof(u32))
628 goto invalid_tlv_len;
629 drv->fw.phy_config = le32_to_cpup((__le32 *)tlv_data);
630 break;
15854ef9 631 default:
965974a6 632 IWL_DEBUG_INFO(drv, "unknown TLV: %d\n", tlv_type);
15854ef9
JB
633 break;
634 }
635 }
636
637 if (len) {
965974a6
JB
638 IWL_ERR(drv, "invalid TLV after parsing: %zd\n", len);
639 iwl_print_hex_dump(drv, IWL_DL_FW, (u8 *)data, len);
15854ef9
JB
640 return -EINVAL;
641 }
642
643 return 0;
644
645 invalid_tlv_len:
965974a6 646 IWL_ERR(drv, "TLV %d has invalid size: %u\n", tlv_type, tlv_len);
ed8c8365 647 tlv_error:
965974a6 648 iwl_print_hex_dump(drv, IWL_DL_FW, tlv_data, tlv_len);
15854ef9
JB
649
650 return -EINVAL;
651}
652
6dfa8d01
DS
653static int alloc_pci_desc(struct iwl_drv *drv,
654 struct iwl_firmware_pieces *pieces,
655 enum iwl_ucode_type type)
656{
657 int i;
658 for (i = 0;
659 i < IWL_UCODE_SECTION_MAX && get_sec_size(pieces, type, i);
660 i++)
661 if (iwl_alloc_fw_desc(drv, &(drv->fw.img[type].sec[i]),
662 get_sec(pieces, type, i)))
663 return -1;
664 return 0;
665}
666
667static int validate_sec_sizes(struct iwl_drv *drv,
668 struct iwl_firmware_pieces *pieces,
669 const struct iwl_cfg *cfg)
670{
671 IWL_DEBUG_INFO(drv, "f/w package hdr runtime inst size = %Zd\n",
672 get_sec_size(pieces, IWL_UCODE_REGULAR,
673 IWL_UCODE_SECTION_INST));
674 IWL_DEBUG_INFO(drv, "f/w package hdr runtime data size = %Zd\n",
675 get_sec_size(pieces, IWL_UCODE_REGULAR,
676 IWL_UCODE_SECTION_DATA));
677 IWL_DEBUG_INFO(drv, "f/w package hdr init inst size = %Zd\n",
678 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST));
679 IWL_DEBUG_INFO(drv, "f/w package hdr init data size = %Zd\n",
680 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA));
681
682 /* Verify that uCode images will fit in card's SRAM. */
683 if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) >
684 cfg->max_inst_size) {
685 IWL_ERR(drv, "uCode instr len %Zd too large to fit in\n",
686 get_sec_size(pieces, IWL_UCODE_REGULAR,
687 IWL_UCODE_SECTION_INST));
688 return -1;
689 }
690
691 if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) >
692 cfg->max_data_size) {
693 IWL_ERR(drv, "uCode data len %Zd too large to fit in\n",
694 get_sec_size(pieces, IWL_UCODE_REGULAR,
695 IWL_UCODE_SECTION_DATA));
696 return -1;
697 }
698
699 if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) >
700 cfg->max_inst_size) {
701 IWL_ERR(drv, "uCode init instr len %Zd too large to fit in\n",
702 get_sec_size(pieces, IWL_UCODE_INIT,
703 IWL_UCODE_SECTION_INST));
704 return -1;
705 }
706
707 if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA) >
708 cfg->max_data_size) {
709 IWL_ERR(drv, "uCode init data len %Zd too large to fit in\n",
710 get_sec_size(pieces, IWL_UCODE_REGULAR,
711 IWL_UCODE_SECTION_DATA));
712 return -1;
713 }
714 return 0;
715}
716
717
15854ef9
JB
718/**
719 * iwl_ucode_callback - callback when firmware was loaded
720 *
721 * If loaded successfully, copies the firmware into buffers
722 * for the card to fetch (via DMA).
723 */
724static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context)
725{
965974a6
JB
726 struct iwl_drv *drv = context;
727 const struct iwl_cfg *cfg = cfg(drv);
728 struct iwl_fw *fw = &drv->fw;
15854ef9
JB
729 struct iwl_ucode_header *ucode;
730 int err;
0cedacc5 731 struct iwl_firmware_pieces pieces;
15854ef9
JB
732 const unsigned int api_max = cfg->ucode_api_max;
733 unsigned int api_ok = cfg->ucode_api_ok;
734 const unsigned int api_min = cfg->ucode_api_min;
735 u32 api_ver;
6dfa8d01 736 int i;
15854ef9
JB
737
738 fw->ucode_capa.max_probe_length = 200;
739 fw->ucode_capa.standard_phy_calibration_size =
740 IWL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE;
741
742 if (!api_ok)
743 api_ok = api_max;
744
745 memset(&pieces, 0, sizeof(pieces));
746
747 if (!ucode_raw) {
965974a6
JB
748 if (drv->fw_index <= api_ok)
749 IWL_ERR(drv,
15854ef9 750 "request for firmware file '%s' failed.\n",
965974a6 751 drv->firmware_name);
15854ef9
JB
752 goto try_again;
753 }
754
965974a6
JB
755 IWL_DEBUG_INFO(drv, "Loaded firmware file '%s' (%zd bytes).\n",
756 drv->firmware_name, ucode_raw->size);
15854ef9
JB
757
758 /* Make sure that we got at least the API version number */
759 if (ucode_raw->size < 4) {
965974a6 760 IWL_ERR(drv, "File size way too small!\n");
15854ef9
JB
761 goto try_again;
762 }
763
764 /* Data from ucode file: header followed by uCode images */
765 ucode = (struct iwl_ucode_header *)ucode_raw->data;
766
767 if (ucode->ver)
965974a6 768 err = iwl_parse_v1_v2_firmware(drv, ucode_raw, &pieces);
15854ef9 769 else
965974a6 770 err = iwl_parse_tlv_firmware(drv, ucode_raw, &pieces,
15854ef9
JB
771 &fw->ucode_capa);
772
773 if (err)
774 goto try_again;
775
965974a6 776 api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
15854ef9
JB
777
778 /*
779 * api_ver should match the api version forming part of the
780 * firmware filename ... but we don't check for that and only rely
781 * on the API version read from firmware header from here on forward
782 */
783 /* no api version check required for experimental uCode */
965974a6 784 if (drv->fw_index != UCODE_EXPERIMENTAL_INDEX) {
15854ef9 785 if (api_ver < api_min || api_ver > api_max) {
965974a6 786 IWL_ERR(drv,
15854ef9
JB
787 "Driver unable to support your firmware API. "
788 "Driver supports v%u, firmware is v%u.\n",
789 api_max, api_ver);
790 goto try_again;
791 }
792
793 if (api_ver < api_ok) {
794 if (api_ok != api_max)
965974a6 795 IWL_ERR(drv, "Firmware has old API version, "
15854ef9
JB
796 "expected v%u through v%u, got v%u.\n",
797 api_ok, api_max, api_ver);
798 else
965974a6 799 IWL_ERR(drv, "Firmware has old API version, "
15854ef9
JB
800 "expected v%u, got v%u.\n",
801 api_max, api_ver);
965974a6 802 IWL_ERR(drv, "New firmware can be obtained from "
15854ef9
JB
803 "http://www.intellinuxwireless.org/.\n");
804 }
805 }
806
965974a6 807 IWL_INFO(drv, "loaded firmware version %s", drv->fw.fw_version);
15854ef9
JB
808
809 /*
810 * For any of the failures below (before allocating pci memory)
811 * we will try to load a version with a smaller API -- maybe the
812 * user just got a corrupted version of the latest API.
813 */
814
965974a6
JB
815 IWL_DEBUG_INFO(drv, "f/w package hdr ucode version raw = 0x%x\n",
816 drv->fw.ucode_ver);
817 IWL_DEBUG_INFO(drv, "f/w package hdr runtime inst size = %Zd\n",
0cedacc5
DS
818 get_sec_size(&pieces, IWL_UCODE_REGULAR,
819 IWL_UCODE_SECTION_INST));
965974a6 820 IWL_DEBUG_INFO(drv, "f/w package hdr runtime data size = %Zd\n",
0cedacc5
DS
821 get_sec_size(&pieces, IWL_UCODE_REGULAR,
822 IWL_UCODE_SECTION_DATA));
965974a6 823 IWL_DEBUG_INFO(drv, "f/w package hdr init inst size = %Zd\n",
0cedacc5 824 get_sec_size(&pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST));
965974a6 825 IWL_DEBUG_INFO(drv, "f/w package hdr init data size = %Zd\n",
0cedacc5 826 get_sec_size(&pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA));
15854ef9
JB
827
828 /* Verify that uCode images will fit in card's SRAM */
0cedacc5
DS
829 if (get_sec_size(&pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) >
830 cfg->max_inst_size) {
965974a6 831 IWL_ERR(drv, "uCode instr len %Zd too large to fit in\n",
0cedacc5
DS
832 get_sec_size(&pieces, IWL_UCODE_REGULAR,
833 IWL_UCODE_SECTION_INST));
15854ef9
JB
834 goto try_again;
835 }
836
0cedacc5
DS
837 if (get_sec_size(&pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) >
838 cfg->max_data_size) {
965974a6 839 IWL_ERR(drv, "uCode data len %Zd too large to fit in\n",
0cedacc5
DS
840 get_sec_size(&pieces, IWL_UCODE_REGULAR,
841 IWL_UCODE_SECTION_DATA));
15854ef9
JB
842 goto try_again;
843 }
844
4db2c9ae
DS
845 /*
846 * In mvm uCode there is no difference between data and instructions
847 * sections.
848 */
849 if (!fw->mvm_fw && validate_sec_sizes(drv, &pieces, cfg))
15854ef9 850 goto try_again;
15854ef9
JB
851
852 /* Allocate ucode buffers for card's bus-master loading ... */
853
854 /* Runtime instructions and 2 copies of data:
855 * 1) unmodified from disk
856 * 2) backup cache for save/restore during power-downs */
6dfa8d01
DS
857 for (i = 0; i < IWL_UCODE_TYPE_MAX; i++)
858 if (alloc_pci_desc(drv, &pieces, i))
15854ef9 859 goto err_pci_alloc;
15854ef9
JB
860
861 /* Now that we can no longer fail, copy information */
862
863 /*
864 * The (size - 16) / 12 formula is based on the information recorded
865 * for each event, which is of mode 1 (including timestamp) for all
866 * new microcodes that include this information.
867 */
0692fe41 868 fw->init_evtlog_ptr = pieces.init_evtlog_ptr;
15854ef9 869 if (pieces.init_evtlog_size)
0692fe41 870 fw->init_evtlog_size = (pieces.init_evtlog_size - 16)/12;
15854ef9 871 else
0692fe41 872 fw->init_evtlog_size =
15854ef9 873 cfg->base_params->max_event_log_size;
0692fe41
JB
874 fw->init_errlog_ptr = pieces.init_errlog_ptr;
875 fw->inst_evtlog_ptr = pieces.inst_evtlog_ptr;
15854ef9 876 if (pieces.inst_evtlog_size)
0692fe41 877 fw->inst_evtlog_size = (pieces.inst_evtlog_size - 16)/12;
15854ef9 878 else
0692fe41 879 fw->inst_evtlog_size =
15854ef9 880 cfg->base_params->max_event_log_size;
0692fe41 881 fw->inst_errlog_ptr = pieces.inst_errlog_ptr;
15854ef9
JB
882
883 /*
884 * figure out the offset of chain noise reset and gain commands
885 * base on the size of standard phy calibration commands table size
886 */
887 if (fw->ucode_capa.standard_phy_calibration_size >
888 IWL_MAX_PHY_CALIBRATE_TBL_SIZE)
889 fw->ucode_capa.standard_phy_calibration_size =
890 IWL_MAX_STANDARD_PHY_CALIBRATE_TBL_SIZE;
891
892 /* We have our copies now, allow OS release its copies */
893 release_firmware(ucode_raw);
965974a6 894 complete(&drv->request_firmware_complete);
15854ef9 895
965974a6 896 drv->op_mode = iwl_dvm_ops.start(drv->shrd->trans, &drv->fw);
15854ef9 897
965974a6 898 if (!drv->op_mode)
15854ef9
JB
899 goto out_unbind;
900
901 return;
902
903 try_again:
904 /* try next, if any */
905 release_firmware(ucode_raw);
965974a6 906 if (iwl_request_firmware(drv, false))
15854ef9
JB
907 goto out_unbind;
908 return;
909
910 err_pci_alloc:
965974a6
JB
911 IWL_ERR(drv, "failed to allocate pci memory\n");
912 iwl_dealloc_ucode(drv);
15854ef9
JB
913 release_firmware(ucode_raw);
914 out_unbind:
965974a6
JB
915 complete(&drv->request_firmware_complete);
916 device_release_driver(trans(drv)->dev);
15854ef9
JB
917}
918
5c58edc6 919int iwl_drv_start(struct iwl_shared *shrd,
706c4ff6 920 struct iwl_trans *trans, const struct iwl_cfg *cfg)
5c58edc6 921{
965974a6 922 struct iwl_drv *drv;
5c58edc6
EG
923 int ret;
924
925 shrd->cfg = cfg;
926
965974a6
JB
927 drv = kzalloc(sizeof(*drv), GFP_KERNEL);
928 if (!drv) {
929 dev_printk(KERN_ERR, trans->dev, "Couldn't allocate iwl_drv");
5c58edc6
EG
930 return -ENOMEM;
931 }
965974a6
JB
932 drv->shrd = shrd;
933 shrd->drv = drv;
5c58edc6 934
965974a6 935 init_completion(&drv->request_firmware_complete);
5c58edc6 936
965974a6 937 ret = iwl_request_firmware(drv, true);
5c58edc6
EG
938
939 if (ret) {
940 dev_printk(KERN_ERR, trans->dev, "Couldn't request the fw");
965974a6
JB
941 kfree(drv);
942 shrd->drv = NULL;
5c58edc6
EG
943 }
944
945 return ret;
946}
947
07590f08
EG
948void iwl_drv_stop(struct iwl_shared *shrd)
949{
965974a6 950 struct iwl_drv *drv = shrd->drv;
0692fe41 951
965974a6 952 wait_for_completion(&drv->request_firmware_complete);
2e7eb117 953
d0f76d68 954 /* op_mode can be NULL if its start failed */
965974a6
JB
955 if (drv->op_mode)
956 iwl_op_mode_stop(drv->op_mode);
07590f08 957
965974a6 958 iwl_dealloc_ucode(drv);
7db5b989 959
965974a6
JB
960 kfree(drv);
961 shrd->drv = NULL;
07590f08 962}