1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3 * Copyright (C) 2005-2014, 2018-2021 Intel Corporation
4 * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
5 * Copyright (C) 2016-2017 Intel Deutschland GmbH
6 */
7 #include <linux/completion.h>
8 #include <linux/dma-mapping.h>
9 #include <linux/firmware.h>
10 #include <linux/module.h>
11 #include <linux/vmalloc.h>
12
13 #include "iwl-drv.h"
14 #include "iwl-csr.h"
15 #include "iwl-debug.h"
16 #include "iwl-trans.h"
17 #include "iwl-op-mode.h"
18 #include "iwl-agn-hw.h"
19 #include "fw/img.h"
20 #include "iwl-dbg-tlv.h"
21 #include "iwl-config.h"
22 #include "iwl-modparams.h"
23 #include "fw/api/alive.h"
24 #include "fw/api/mac.h"
25
26 /******************************************************************************
27 *
28 * module boiler plate
29 *
30 ******************************************************************************/
31
32 #define DRV_DESCRIPTION "Intel(R) Wireless WiFi driver for Linux"
33 MODULE_DESCRIPTION(DRV_DESCRIPTION);
34 MODULE_LICENSE("GPL");
35
36 #ifdef CONFIG_IWLWIFI_DEBUGFS
37 static struct dentry *iwl_dbgfs_root;
38 #endif
39
40 /**
41 * struct iwl_drv - drv common data
42 * @list: list of drv structures using this opmode
43 * @fw: the iwl_fw structure
44 * @op_mode: the running op_mode
45 * @trans: transport layer
46 * @dev: for debug prints only
47 * @fw_index: firmware revision to try loading
48 * @firmware_name: composite filename of ucode file to load
49 * @request_firmware_complete: the firmware has been obtained from user space
50 * @dbgfs_drv: debugfs root directory entry
51 * @dbgfs_trans: debugfs transport directory entry
52 * @dbgfs_op_mode: debugfs op_mode directory entry
53 */
54 struct iwl_drv {
55 struct list_head list;
56 struct iwl_fw fw;
57
58 struct iwl_op_mode *op_mode;
59 struct iwl_trans *trans;
60 struct device *dev;
61
62 int fw_index; /* firmware we're trying to load */
63 char firmware_name[64]; /* name of firmware file to load */
64
65 struct completion request_firmware_complete;
66
67 #ifdef CONFIG_IWLWIFI_DEBUGFS
68 struct dentry *dbgfs_drv;
69 struct dentry *dbgfs_trans;
70 struct dentry *dbgfs_op_mode;
71 #endif
72 };
73
74 enum {
75 DVM_OP_MODE,
76 MVM_OP_MODE,
77 };
78
79 /* Protects the table contents, i.e. the ops pointer & drv list */
80 static DEFINE_MUTEX(iwlwifi_opmode_table_mtx);
81 static struct iwlwifi_opmode_table {
82 const char *name; /* name: iwldvm, iwlmvm, etc */
83 const struct iwl_op_mode_ops *ops; /* pointer to op_mode ops */
84 struct list_head drv; /* list of devices using this op_mode */
85 } iwlwifi_opmode_table[] = { /* ops set when driver is initialized */
86 [DVM_OP_MODE] = { .name = "iwldvm", .ops = NULL },
87 [MVM_OP_MODE] = { .name = "iwlmvm", .ops = NULL },
88 };
89
90 #define IWL_DEFAULT_SCAN_CHANNELS 40
91
92 /*
93 * struct fw_sec: Just for the image parsing process.
94 * For the fw storage we are using struct fw_desc.
95 */
96 struct fw_sec {
97 const void *data; /* the sec data */
98 size_t size; /* section size */
99 u32 offset; /* offset of writing in the device */
100 };
101
iwl_free_fw_desc(struct iwl_drv * drv,struct fw_desc * desc)102 static void iwl_free_fw_desc(struct iwl_drv *drv, struct fw_desc *desc)
103 {
104 vfree(desc->data);
105 desc->data = NULL;
106 desc->len = 0;
107 }
108
iwl_free_fw_img(struct iwl_drv * drv,struct fw_img * img)109 static void iwl_free_fw_img(struct iwl_drv *drv, struct fw_img *img)
110 {
111 int i;
112 for (i = 0; i < img->num_sec; i++)
113 iwl_free_fw_desc(drv, &img->sec[i]);
114 kfree(img->sec);
115 }
116
iwl_dealloc_ucode(struct iwl_drv * drv)117 static void iwl_dealloc_ucode(struct iwl_drv *drv)
118 {
119 int i;
120
121 kfree(drv->fw.dbg.dest_tlv);
122 for (i = 0; i < ARRAY_SIZE(drv->fw.dbg.conf_tlv); i++)
123 kfree(drv->fw.dbg.conf_tlv[i]);
124 for (i = 0; i < ARRAY_SIZE(drv->fw.dbg.trigger_tlv); i++)
125 kfree(drv->fw.dbg.trigger_tlv[i]);
126 kfree(drv->fw.dbg.mem_tlv);
127 kfree(drv->fw.iml);
128 kfree(drv->fw.ucode_capa.cmd_versions);
129 kfree(drv->fw.phy_integration_ver);
130
131 for (i = 0; i < IWL_UCODE_TYPE_MAX; i++)
132 iwl_free_fw_img(drv, drv->fw.img + i);
133 }
134
iwl_alloc_fw_desc(struct iwl_drv * drv,struct fw_desc * desc,struct fw_sec * sec)135 static int iwl_alloc_fw_desc(struct iwl_drv *drv, struct fw_desc *desc,
136 struct fw_sec *sec)
137 {
138 void *data;
139
140 desc->data = NULL;
141
142 if (!sec || !sec->size)
143 return -EINVAL;
144
145 data = vmalloc(sec->size);
146 if (!data)
147 return -ENOMEM;
148
149 desc->len = sec->size;
150 desc->offset = sec->offset;
151 memcpy(data, sec->data, desc->len);
152 desc->data = data;
153
154 return 0;
155 }
156
157 static void iwl_req_fw_callback(const struct firmware *ucode_raw,
158 void *context);
159
iwl_request_firmware(struct iwl_drv * drv,bool first)160 static int iwl_request_firmware(struct iwl_drv *drv, bool first)
161 {
162 const struct iwl_cfg *cfg = drv->trans->cfg;
163 char tag[8];
164
165 if (drv->trans->trans_cfg->device_family == IWL_DEVICE_FAMILY_9000 &&
166 (CSR_HW_REV_STEP(drv->trans->hw_rev) != SILICON_B_STEP &&
167 CSR_HW_REV_STEP(drv->trans->hw_rev) != SILICON_C_STEP)) {
168 IWL_ERR(drv,
169 "Only HW steps B and C are currently supported (0x%0x)\n",
170 drv->trans->hw_rev);
171 return -EINVAL;
172 }
173
174 if (first) {
175 drv->fw_index = cfg->ucode_api_max;
176 sprintf(tag, "%d", drv->fw_index);
177 } else {
178 drv->fw_index--;
179 sprintf(tag, "%d", drv->fw_index);
180 }
181
182 if (drv->fw_index < cfg->ucode_api_min) {
183 IWL_ERR(drv, "no suitable firmware found!\n");
184
185 if (cfg->ucode_api_min == cfg->ucode_api_max) {
186 IWL_ERR(drv, "%s%d is required\n", cfg->fw_name_pre,
187 cfg->ucode_api_max);
188 } else {
189 IWL_ERR(drv, "minimum version required: %s%d\n",
190 cfg->fw_name_pre, cfg->ucode_api_min);
191 IWL_ERR(drv, "maximum version supported: %s%d\n",
192 cfg->fw_name_pre, cfg->ucode_api_max);
193 }
194
195 IWL_ERR(drv,
196 "check git://git.kernel.org/pub/scm/linux/kernel/git/firmware/linux-firmware.git\n");
197 return -ENOENT;
198 }
199
200 snprintf(drv->firmware_name, sizeof(drv->firmware_name), "%s%s.ucode",
201 cfg->fw_name_pre, tag);
202
203 IWL_DEBUG_FW_INFO(drv, "attempting to load firmware '%s'\n",
204 drv->firmware_name);
205
206 return request_firmware_nowait(THIS_MODULE, 1, drv->firmware_name,
207 drv->trans->dev,
208 GFP_KERNEL, drv, iwl_req_fw_callback);
209 }
210
211 struct fw_img_parsing {
212 struct fw_sec *sec;
213 int sec_counter;
214 };
215
216 /*
217 * struct fw_sec_parsing: to extract fw section and it's offset from tlv
218 */
219 struct fw_sec_parsing {
220 __le32 offset;
221 const u8 data[];
222 } __packed;
223
224 /**
225 * struct iwl_tlv_calib_data - parse the default calib data from TLV
226 *
227 * @ucode_type: the uCode to which the following default calib relates.
228 * @calib: default calibrations.
229 */
230 struct iwl_tlv_calib_data {
231 __le32 ucode_type;
232 struct iwl_tlv_calib_ctrl calib;
233 } __packed;
234
235 struct iwl_firmware_pieces {
236 struct fw_img_parsing img[IWL_UCODE_TYPE_MAX];
237
238 u32 init_evtlog_ptr, init_evtlog_size, init_errlog_ptr;
239 u32 inst_evtlog_ptr, inst_evtlog_size, inst_errlog_ptr;
240
241 /* FW debug data parsed for driver usage */
242 bool dbg_dest_tlv_init;
243 u8 *dbg_dest_ver;
244 union {
245 struct iwl_fw_dbg_dest_tlv *dbg_dest_tlv;
246 struct iwl_fw_dbg_dest_tlv_v1 *dbg_dest_tlv_v1;
247 };
248 struct iwl_fw_dbg_conf_tlv *dbg_conf_tlv[FW_DBG_CONF_MAX];
249 size_t dbg_conf_tlv_len[FW_DBG_CONF_MAX];
250 struct iwl_fw_dbg_trigger_tlv *dbg_trigger_tlv[FW_DBG_TRIGGER_MAX];
251 size_t dbg_trigger_tlv_len[FW_DBG_TRIGGER_MAX];
252 struct iwl_fw_dbg_mem_seg_tlv *dbg_mem_tlv;
253 size_t n_mem_tlv;
254 };
255
256 /*
257 * These functions are just to extract uCode section data from the pieces
258 * structure.
259 */
get_sec(struct iwl_firmware_pieces * pieces,enum iwl_ucode_type type,int sec)260 static struct fw_sec *get_sec(struct iwl_firmware_pieces *pieces,
261 enum iwl_ucode_type type,
262 int sec)
263 {
264 return &pieces->img[type].sec[sec];
265 }
266
alloc_sec_data(struct iwl_firmware_pieces * pieces,enum iwl_ucode_type type,int sec)267 static void alloc_sec_data(struct iwl_firmware_pieces *pieces,
268 enum iwl_ucode_type type,
269 int sec)
270 {
271 struct fw_img_parsing *img = &pieces->img[type];
272 struct fw_sec *sec_memory;
273 int size = sec + 1;
274 size_t alloc_size = sizeof(*img->sec) * size;
275
276 if (img->sec && img->sec_counter >= size)
277 return;
278
279 sec_memory = krealloc(img->sec, alloc_size, GFP_KERNEL);
280 if (!sec_memory)
281 return;
282
283 img->sec = sec_memory;
284 img->sec_counter = size;
285 }
286
set_sec_data(struct iwl_firmware_pieces * pieces,enum iwl_ucode_type type,int sec,const void * data)287 static void set_sec_data(struct iwl_firmware_pieces *pieces,
288 enum iwl_ucode_type type,
289 int sec,
290 const void *data)
291 {
292 alloc_sec_data(pieces, type, sec);
293
294 pieces->img[type].sec[sec].data = data;
295 }
296
set_sec_size(struct iwl_firmware_pieces * pieces,enum iwl_ucode_type type,int sec,size_t size)297 static void set_sec_size(struct iwl_firmware_pieces *pieces,
298 enum iwl_ucode_type type,
299 int sec,
300 size_t size)
301 {
302 alloc_sec_data(pieces, type, sec);
303
304 pieces->img[type].sec[sec].size = size;
305 }
306
get_sec_size(struct iwl_firmware_pieces * pieces,enum iwl_ucode_type type,int sec)307 static size_t get_sec_size(struct iwl_firmware_pieces *pieces,
308 enum iwl_ucode_type type,
309 int sec)
310 {
311 return pieces->img[type].sec[sec].size;
312 }
313
set_sec_offset(struct iwl_firmware_pieces * pieces,enum iwl_ucode_type type,int sec,u32 offset)314 static void set_sec_offset(struct iwl_firmware_pieces *pieces,
315 enum iwl_ucode_type type,
316 int sec,
317 u32 offset)
318 {
319 alloc_sec_data(pieces, type, sec);
320
321 pieces->img[type].sec[sec].offset = offset;
322 }
323
iwl_store_cscheme(struct iwl_fw * fw,const u8 * data,const u32 len)324 static int iwl_store_cscheme(struct iwl_fw *fw, const u8 *data, const u32 len)
325 {
326 int i, j;
327 struct iwl_fw_cscheme_list *l = (struct iwl_fw_cscheme_list *)data;
328 struct iwl_fw_cipher_scheme *fwcs;
329
330 if (len < sizeof(*l) ||
331 len < sizeof(l->size) + l->size * sizeof(l->cs[0]))
332 return -EINVAL;
333
334 for (i = 0, j = 0; i < IWL_UCODE_MAX_CS && i < l->size; i++) {
335 fwcs = &l->cs[j];
336
337 /* we skip schemes with zero cipher suite selector */
338 if (!fwcs->cipher)
339 continue;
340
341 fw->cs[j++] = *fwcs;
342 }
343
344 return 0;
345 }
346
347 /*
348 * Gets uCode section from tlv.
349 */
iwl_store_ucode_sec(struct iwl_firmware_pieces * pieces,const void * data,enum iwl_ucode_type type,int size)350 static int iwl_store_ucode_sec(struct iwl_firmware_pieces *pieces,
351 const void *data, enum iwl_ucode_type type,
352 int size)
353 {
354 struct fw_img_parsing *img;
355 struct fw_sec *sec;
356 struct fw_sec_parsing *sec_parse;
357 size_t alloc_size;
358
359 if (WARN_ON(!pieces || !data || type >= IWL_UCODE_TYPE_MAX))
360 return -1;
361
362 sec_parse = (struct fw_sec_parsing *)data;
363
364 img = &pieces->img[type];
365
366 alloc_size = sizeof(*img->sec) * (img->sec_counter + 1);
367 sec = krealloc(img->sec, alloc_size, GFP_KERNEL);
368 if (!sec)
369 return -ENOMEM;
370 img->sec = sec;
371
372 sec = &img->sec[img->sec_counter];
373
374 sec->offset = le32_to_cpu(sec_parse->offset);
375 sec->data = sec_parse->data;
376 sec->size = size - sizeof(sec_parse->offset);
377
378 ++img->sec_counter;
379
380 return 0;
381 }
382
iwl_set_default_calib(struct iwl_drv * drv,const u8 * data)383 static int iwl_set_default_calib(struct iwl_drv *drv, const u8 *data)
384 {
385 struct iwl_tlv_calib_data *def_calib =
386 (struct iwl_tlv_calib_data *)data;
387 u32 ucode_type = le32_to_cpu(def_calib->ucode_type);
388 if (ucode_type >= IWL_UCODE_TYPE_MAX) {
389 IWL_ERR(drv, "Wrong ucode_type %u for default calibration.\n",
390 ucode_type);
391 return -EINVAL;
392 }
393 drv->fw.default_calib[ucode_type].flow_trigger =
394 def_calib->calib.flow_trigger;
395 drv->fw.default_calib[ucode_type].event_trigger =
396 def_calib->calib.event_trigger;
397
398 return 0;
399 }
400
iwl_set_ucode_api_flags(struct iwl_drv * drv,const u8 * data,struct iwl_ucode_capabilities * capa)401 static void iwl_set_ucode_api_flags(struct iwl_drv *drv, const u8 *data,
402 struct iwl_ucode_capabilities *capa)
403 {
404 const struct iwl_ucode_api *ucode_api = (void *)data;
405 u32 api_index = le32_to_cpu(ucode_api->api_index);
406 u32 api_flags = le32_to_cpu(ucode_api->api_flags);
407 int i;
408
409 if (api_index >= DIV_ROUND_UP(NUM_IWL_UCODE_TLV_API, 32)) {
410 IWL_WARN(drv,
411 "api flags index %d larger than supported by driver\n",
412 api_index);
413 return;
414 }
415
416 for (i = 0; i < 32; i++) {
417 if (api_flags & BIT(i))
418 __set_bit(i + 32 * api_index, capa->_api);
419 }
420 }
421
iwl_set_ucode_capabilities(struct iwl_drv * drv,const u8 * data,struct iwl_ucode_capabilities * capa)422 static void iwl_set_ucode_capabilities(struct iwl_drv *drv, const u8 *data,
423 struct iwl_ucode_capabilities *capa)
424 {
425 const struct iwl_ucode_capa *ucode_capa = (void *)data;
426 u32 api_index = le32_to_cpu(ucode_capa->api_index);
427 u32 api_flags = le32_to_cpu(ucode_capa->api_capa);
428 int i;
429
430 if (api_index >= DIV_ROUND_UP(NUM_IWL_UCODE_TLV_CAPA, 32)) {
431 IWL_WARN(drv,
432 "capa flags index %d larger than supported by driver\n",
433 api_index);
434 return;
435 }
436
437 for (i = 0; i < 32; i++) {
438 if (api_flags & BIT(i))
439 __set_bit(i + 32 * api_index, capa->_capa);
440 }
441 }
442
iwl_reduced_fw_name(struct iwl_drv * drv)443 static const char *iwl_reduced_fw_name(struct iwl_drv *drv)
444 {
445 const char *name = drv->firmware_name;
446
447 if (strncmp(name, "iwlwifi-", 8) == 0)
448 name += 8;
449
450 return name;
451 }
452
iwl_parse_v1_v2_firmware(struct iwl_drv * drv,const struct firmware * ucode_raw,struct iwl_firmware_pieces * pieces)453 static int iwl_parse_v1_v2_firmware(struct iwl_drv *drv,
454 const struct firmware *ucode_raw,
455 struct iwl_firmware_pieces *pieces)
456 {
457 struct iwl_ucode_header *ucode = (void *)ucode_raw->data;
458 u32 api_ver, hdr_size, build;
459 char buildstr[25];
460 const u8 *src;
461
462 drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
463 api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
464
465 switch (api_ver) {
466 default:
467 hdr_size = 28;
468 if (ucode_raw->size < hdr_size) {
469 IWL_ERR(drv, "File size too small!\n");
470 return -EINVAL;
471 }
472 build = le32_to_cpu(ucode->u.v2.build);
473 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
474 le32_to_cpu(ucode->u.v2.inst_size));
475 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
476 le32_to_cpu(ucode->u.v2.data_size));
477 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
478 le32_to_cpu(ucode->u.v2.init_size));
479 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
480 le32_to_cpu(ucode->u.v2.init_data_size));
481 src = ucode->u.v2.data;
482 break;
483 case 0:
484 case 1:
485 case 2:
486 hdr_size = 24;
487 if (ucode_raw->size < hdr_size) {
488 IWL_ERR(drv, "File size too small!\n");
489 return -EINVAL;
490 }
491 build = 0;
492 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
493 le32_to_cpu(ucode->u.v1.inst_size));
494 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
495 le32_to_cpu(ucode->u.v1.data_size));
496 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
497 le32_to_cpu(ucode->u.v1.init_size));
498 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
499 le32_to_cpu(ucode->u.v1.init_data_size));
500 src = ucode->u.v1.data;
501 break;
502 }
503
504 if (build)
505 sprintf(buildstr, " build %u", build);
506 else
507 buildstr[0] = '\0';
508
509 snprintf(drv->fw.fw_version,
510 sizeof(drv->fw.fw_version),
511 "%u.%u.%u.%u%s %s",
512 IWL_UCODE_MAJOR(drv->fw.ucode_ver),
513 IWL_UCODE_MINOR(drv->fw.ucode_ver),
514 IWL_UCODE_API(drv->fw.ucode_ver),
515 IWL_UCODE_SERIAL(drv->fw.ucode_ver),
516 buildstr, iwl_reduced_fw_name(drv));
517
518 /* Verify size of file vs. image size info in file's header */
519
520 if (ucode_raw->size != hdr_size +
521 get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) +
522 get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) +
523 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) +
524 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA)) {
525
526 IWL_ERR(drv,
527 "uCode file size %d does not match expected size\n",
528 (int)ucode_raw->size);
529 return -EINVAL;
530 }
531
532
533 set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST, src);
534 src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST);
535 set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
536 IWLAGN_RTC_INST_LOWER_BOUND);
537 set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA, src);
538 src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA);
539 set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
540 IWLAGN_RTC_DATA_LOWER_BOUND);
541 set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST, src);
542 src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST);
543 set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
544 IWLAGN_RTC_INST_LOWER_BOUND);
545 set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA, src);
546 src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA);
547 set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
548 IWLAGN_RTC_DATA_LOWER_BOUND);
549 return 0;
550 }
551
iwl_drv_set_dump_exclude(struct iwl_drv * drv,enum iwl_ucode_tlv_type tlv_type,const void * tlv_data,u32 tlv_len)552 static void iwl_drv_set_dump_exclude(struct iwl_drv *drv,
553 enum iwl_ucode_tlv_type tlv_type,
554 const void *tlv_data, u32 tlv_len)
555 {
556 const struct iwl_fw_dump_exclude *fw = tlv_data;
557 struct iwl_dump_exclude *excl;
558
559 if (tlv_len < sizeof(*fw))
560 return;
561
562 if (tlv_type == IWL_UCODE_TLV_SEC_TABLE_ADDR) {
563 excl = &drv->fw.dump_excl[0];
564
565 /* second time we find this, it's for WoWLAN */
566 if (excl->addr)
567 excl = &drv->fw.dump_excl_wowlan[0];
568 } else if (fw_has_capa(&drv->fw.ucode_capa,
569 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG)) {
570 /* IWL_UCODE_TLV_D3_KEK_KCK_ADDR is regular image */
571 excl = &drv->fw.dump_excl[0];
572 } else {
573 /* IWL_UCODE_TLV_D3_KEK_KCK_ADDR is WoWLAN image */
574 excl = &drv->fw.dump_excl_wowlan[0];
575 }
576
577 if (excl->addr)
578 excl++;
579
580 if (excl->addr) {
581 IWL_DEBUG_FW_INFO(drv, "found too many excludes in fw file\n");
582 return;
583 }
584
585 excl->addr = le32_to_cpu(fw->addr) & ~FW_ADDR_CACHE_CONTROL;
586 excl->size = le32_to_cpu(fw->size);
587 }
588
iwl_parse_tlv_firmware(struct iwl_drv * drv,const struct firmware * ucode_raw,struct iwl_firmware_pieces * pieces,struct iwl_ucode_capabilities * capa,bool * usniffer_images)589 static int iwl_parse_tlv_firmware(struct iwl_drv *drv,
590 const struct firmware *ucode_raw,
591 struct iwl_firmware_pieces *pieces,
592 struct iwl_ucode_capabilities *capa,
593 bool *usniffer_images)
594 {
595 struct iwl_tlv_ucode_header *ucode = (void *)ucode_raw->data;
596 const struct iwl_ucode_tlv *tlv;
597 size_t len = ucode_raw->size;
598 const u8 *data;
599 u32 tlv_len;
600 u32 usniffer_img;
601 enum iwl_ucode_tlv_type tlv_type;
602 const u8 *tlv_data;
603 char buildstr[25];
604 u32 build, paging_mem_size;
605 int num_of_cpus;
606 bool usniffer_req = false;
607
608 if (len < sizeof(*ucode)) {
609 IWL_ERR(drv, "uCode has invalid length: %zd\n", len);
610 return -EINVAL;
611 }
612
613 if (ucode->magic != cpu_to_le32(IWL_TLV_UCODE_MAGIC)) {
614 IWL_ERR(drv, "invalid uCode magic: 0X%x\n",
615 le32_to_cpu(ucode->magic));
616 return -EINVAL;
617 }
618
619 drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
620 memcpy(drv->fw.human_readable, ucode->human_readable,
621 sizeof(drv->fw.human_readable));
622 build = le32_to_cpu(ucode->build);
623
624 if (build)
625 sprintf(buildstr, " build %u", build);
626 else
627 buildstr[0] = '\0';
628
629 snprintf(drv->fw.fw_version,
630 sizeof(drv->fw.fw_version),
631 "%u.%u.%u.%u%s %s",
632 IWL_UCODE_MAJOR(drv->fw.ucode_ver),
633 IWL_UCODE_MINOR(drv->fw.ucode_ver),
634 IWL_UCODE_API(drv->fw.ucode_ver),
635 IWL_UCODE_SERIAL(drv->fw.ucode_ver),
636 buildstr, iwl_reduced_fw_name(drv));
637
638 data = ucode->data;
639
640 len -= sizeof(*ucode);
641
642 while (len >= sizeof(*tlv)) {
643 len -= sizeof(*tlv);
644 tlv = (void *)data;
645
646 tlv_len = le32_to_cpu(tlv->length);
647 tlv_type = le32_to_cpu(tlv->type);
648 tlv_data = tlv->data;
649
650 if (len < tlv_len) {
651 IWL_ERR(drv, "invalid TLV len: %zd/%u\n",
652 len, tlv_len);
653 return -EINVAL;
654 }
655 len -= ALIGN(tlv_len, 4);
656 data += sizeof(*tlv) + ALIGN(tlv_len, 4);
657
658 switch (tlv_type) {
659 case IWL_UCODE_TLV_INST:
660 set_sec_data(pieces, IWL_UCODE_REGULAR,
661 IWL_UCODE_SECTION_INST, tlv_data);
662 set_sec_size(pieces, IWL_UCODE_REGULAR,
663 IWL_UCODE_SECTION_INST, tlv_len);
664 set_sec_offset(pieces, IWL_UCODE_REGULAR,
665 IWL_UCODE_SECTION_INST,
666 IWLAGN_RTC_INST_LOWER_BOUND);
667 break;
668 case IWL_UCODE_TLV_DATA:
669 set_sec_data(pieces, IWL_UCODE_REGULAR,
670 IWL_UCODE_SECTION_DATA, tlv_data);
671 set_sec_size(pieces, IWL_UCODE_REGULAR,
672 IWL_UCODE_SECTION_DATA, tlv_len);
673 set_sec_offset(pieces, IWL_UCODE_REGULAR,
674 IWL_UCODE_SECTION_DATA,
675 IWLAGN_RTC_DATA_LOWER_BOUND);
676 break;
677 case IWL_UCODE_TLV_INIT:
678 set_sec_data(pieces, IWL_UCODE_INIT,
679 IWL_UCODE_SECTION_INST, tlv_data);
680 set_sec_size(pieces, IWL_UCODE_INIT,
681 IWL_UCODE_SECTION_INST, tlv_len);
682 set_sec_offset(pieces, IWL_UCODE_INIT,
683 IWL_UCODE_SECTION_INST,
684 IWLAGN_RTC_INST_LOWER_BOUND);
685 break;
686 case IWL_UCODE_TLV_INIT_DATA:
687 set_sec_data(pieces, IWL_UCODE_INIT,
688 IWL_UCODE_SECTION_DATA, tlv_data);
689 set_sec_size(pieces, IWL_UCODE_INIT,
690 IWL_UCODE_SECTION_DATA, tlv_len);
691 set_sec_offset(pieces, IWL_UCODE_INIT,
692 IWL_UCODE_SECTION_DATA,
693 IWLAGN_RTC_DATA_LOWER_BOUND);
694 break;
695 case IWL_UCODE_TLV_BOOT:
696 IWL_ERR(drv, "Found unexpected BOOT ucode\n");
697 break;
698 case IWL_UCODE_TLV_PROBE_MAX_LEN:
699 if (tlv_len != sizeof(u32))
700 goto invalid_tlv_len;
701 capa->max_probe_length =
702 le32_to_cpup((__le32 *)tlv_data);
703 break;
704 case IWL_UCODE_TLV_PAN:
705 if (tlv_len)
706 goto invalid_tlv_len;
707 capa->flags |= IWL_UCODE_TLV_FLAGS_PAN;
708 break;
709 case IWL_UCODE_TLV_FLAGS:
710 /* must be at least one u32 */
711 if (tlv_len < sizeof(u32))
712 goto invalid_tlv_len;
713 /* and a proper number of u32s */
714 if (tlv_len % sizeof(u32))
715 goto invalid_tlv_len;
716 /*
717 * This driver only reads the first u32 as
718 * right now no more features are defined,
719 * if that changes then either the driver
720 * will not work with the new firmware, or
721 * it'll not take advantage of new features.
722 */
723 capa->flags = le32_to_cpup((__le32 *)tlv_data);
724 break;
725 case IWL_UCODE_TLV_API_CHANGES_SET:
726 if (tlv_len != sizeof(struct iwl_ucode_api))
727 goto invalid_tlv_len;
728 iwl_set_ucode_api_flags(drv, tlv_data, capa);
729 break;
730 case IWL_UCODE_TLV_ENABLED_CAPABILITIES:
731 if (tlv_len != sizeof(struct iwl_ucode_capa))
732 goto invalid_tlv_len;
733 iwl_set_ucode_capabilities(drv, tlv_data, capa);
734 break;
735 case IWL_UCODE_TLV_INIT_EVTLOG_PTR:
736 if (tlv_len != sizeof(u32))
737 goto invalid_tlv_len;
738 pieces->init_evtlog_ptr =
739 le32_to_cpup((__le32 *)tlv_data);
740 break;
741 case IWL_UCODE_TLV_INIT_EVTLOG_SIZE:
742 if (tlv_len != sizeof(u32))
743 goto invalid_tlv_len;
744 pieces->init_evtlog_size =
745 le32_to_cpup((__le32 *)tlv_data);
746 break;
747 case IWL_UCODE_TLV_INIT_ERRLOG_PTR:
748 if (tlv_len != sizeof(u32))
749 goto invalid_tlv_len;
750 pieces->init_errlog_ptr =
751 le32_to_cpup((__le32 *)tlv_data);
752 break;
753 case IWL_UCODE_TLV_RUNT_EVTLOG_PTR:
754 if (tlv_len != sizeof(u32))
755 goto invalid_tlv_len;
756 pieces->inst_evtlog_ptr =
757 le32_to_cpup((__le32 *)tlv_data);
758 break;
759 case IWL_UCODE_TLV_RUNT_EVTLOG_SIZE:
760 if (tlv_len != sizeof(u32))
761 goto invalid_tlv_len;
762 pieces->inst_evtlog_size =
763 le32_to_cpup((__le32 *)tlv_data);
764 break;
765 case IWL_UCODE_TLV_RUNT_ERRLOG_PTR:
766 if (tlv_len != sizeof(u32))
767 goto invalid_tlv_len;
768 pieces->inst_errlog_ptr =
769 le32_to_cpup((__le32 *)tlv_data);
770 break;
771 case IWL_UCODE_TLV_ENHANCE_SENS_TBL:
772 if (tlv_len)
773 goto invalid_tlv_len;
774 drv->fw.enhance_sensitivity_table = true;
775 break;
776 case IWL_UCODE_TLV_WOWLAN_INST:
777 set_sec_data(pieces, IWL_UCODE_WOWLAN,
778 IWL_UCODE_SECTION_INST, tlv_data);
779 set_sec_size(pieces, IWL_UCODE_WOWLAN,
780 IWL_UCODE_SECTION_INST, tlv_len);
781 set_sec_offset(pieces, IWL_UCODE_WOWLAN,
782 IWL_UCODE_SECTION_INST,
783 IWLAGN_RTC_INST_LOWER_BOUND);
784 break;
785 case IWL_UCODE_TLV_WOWLAN_DATA:
786 set_sec_data(pieces, IWL_UCODE_WOWLAN,
787 IWL_UCODE_SECTION_DATA, tlv_data);
788 set_sec_size(pieces, IWL_UCODE_WOWLAN,
789 IWL_UCODE_SECTION_DATA, tlv_len);
790 set_sec_offset(pieces, IWL_UCODE_WOWLAN,
791 IWL_UCODE_SECTION_DATA,
792 IWLAGN_RTC_DATA_LOWER_BOUND);
793 break;
794 case IWL_UCODE_TLV_PHY_CALIBRATION_SIZE:
795 if (tlv_len != sizeof(u32))
796 goto invalid_tlv_len;
797 capa->standard_phy_calibration_size =
798 le32_to_cpup((__le32 *)tlv_data);
799 break;
800 case IWL_UCODE_TLV_SEC_RT:
801 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_REGULAR,
802 tlv_len);
803 drv->fw.type = IWL_FW_MVM;
804 break;
805 case IWL_UCODE_TLV_SEC_INIT:
806 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_INIT,
807 tlv_len);
808 drv->fw.type = IWL_FW_MVM;
809 break;
810 case IWL_UCODE_TLV_SEC_WOWLAN:
811 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_WOWLAN,
812 tlv_len);
813 drv->fw.type = IWL_FW_MVM;
814 break;
815 case IWL_UCODE_TLV_DEF_CALIB:
816 if (tlv_len != sizeof(struct iwl_tlv_calib_data))
817 goto invalid_tlv_len;
818 if (iwl_set_default_calib(drv, tlv_data))
819 goto tlv_error;
820 break;
821 case IWL_UCODE_TLV_PHY_SKU:
822 if (tlv_len != sizeof(u32))
823 goto invalid_tlv_len;
824 drv->fw.phy_config = le32_to_cpup((__le32 *)tlv_data);
825 drv->fw.valid_tx_ant = (drv->fw.phy_config &
826 FW_PHY_CFG_TX_CHAIN) >>
827 FW_PHY_CFG_TX_CHAIN_POS;
828 drv->fw.valid_rx_ant = (drv->fw.phy_config &
829 FW_PHY_CFG_RX_CHAIN) >>
830 FW_PHY_CFG_RX_CHAIN_POS;
831 break;
832 case IWL_UCODE_TLV_SECURE_SEC_RT:
833 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_REGULAR,
834 tlv_len);
835 drv->fw.type = IWL_FW_MVM;
836 break;
837 case IWL_UCODE_TLV_SECURE_SEC_INIT:
838 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_INIT,
839 tlv_len);
840 drv->fw.type = IWL_FW_MVM;
841 break;
842 case IWL_UCODE_TLV_SECURE_SEC_WOWLAN:
843 iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_WOWLAN,
844 tlv_len);
845 drv->fw.type = IWL_FW_MVM;
846 break;
847 case IWL_UCODE_TLV_NUM_OF_CPU:
848 if (tlv_len != sizeof(u32))
849 goto invalid_tlv_len;
850 num_of_cpus =
851 le32_to_cpup((__le32 *)tlv_data);
852
853 if (num_of_cpus == 2) {
854 drv->fw.img[IWL_UCODE_REGULAR].is_dual_cpus =
855 true;
856 drv->fw.img[IWL_UCODE_INIT].is_dual_cpus =
857 true;
858 drv->fw.img[IWL_UCODE_WOWLAN].is_dual_cpus =
859 true;
860 } else if ((num_of_cpus > 2) || (num_of_cpus < 1)) {
861 IWL_ERR(drv, "Driver support upto 2 CPUs\n");
862 return -EINVAL;
863 }
864 break;
865 case IWL_UCODE_TLV_CSCHEME:
866 if (iwl_store_cscheme(&drv->fw, tlv_data, tlv_len))
867 goto invalid_tlv_len;
868 break;
869 case IWL_UCODE_TLV_N_SCAN_CHANNELS:
870 if (tlv_len != sizeof(u32))
871 goto invalid_tlv_len;
872 capa->n_scan_channels =
873 le32_to_cpup((__le32 *)tlv_data);
874 break;
875 case IWL_UCODE_TLV_FW_VERSION: {
876 __le32 *ptr = (void *)tlv_data;
877 u32 major, minor;
878 u8 local_comp;
879
880 if (tlv_len != sizeof(u32) * 3)
881 goto invalid_tlv_len;
882
883 major = le32_to_cpup(ptr++);
884 minor = le32_to_cpup(ptr++);
885 local_comp = le32_to_cpup(ptr);
886
887 if (major >= 35)
888 snprintf(drv->fw.fw_version,
889 sizeof(drv->fw.fw_version),
890 "%u.%08x.%u %s", major, minor,
891 local_comp, iwl_reduced_fw_name(drv));
892 else
893 snprintf(drv->fw.fw_version,
894 sizeof(drv->fw.fw_version),
895 "%u.%u.%u %s", major, minor,
896 local_comp, iwl_reduced_fw_name(drv));
897 break;
898 }
899 case IWL_UCODE_TLV_FW_DBG_DEST: {
900 struct iwl_fw_dbg_dest_tlv *dest = NULL;
901 struct iwl_fw_dbg_dest_tlv_v1 *dest_v1 = NULL;
902 u8 mon_mode;
903
904 pieces->dbg_dest_ver = (u8 *)tlv_data;
905 if (*pieces->dbg_dest_ver == 1) {
906 dest = (void *)tlv_data;
907 } else if (*pieces->dbg_dest_ver == 0) {
908 dest_v1 = (void *)tlv_data;
909 } else {
910 IWL_ERR(drv,
911 "The version is %d, and it is invalid\n",
912 *pieces->dbg_dest_ver);
913 break;
914 }
915
916 if (pieces->dbg_dest_tlv_init) {
917 IWL_ERR(drv,
918 "dbg destination ignored, already exists\n");
919 break;
920 }
921
922 pieces->dbg_dest_tlv_init = true;
923
924 if (dest_v1) {
925 pieces->dbg_dest_tlv_v1 = dest_v1;
926 mon_mode = dest_v1->monitor_mode;
927 } else {
928 pieces->dbg_dest_tlv = dest;
929 mon_mode = dest->monitor_mode;
930 }
931
932 IWL_INFO(drv, "Found debug destination: %s\n",
933 get_fw_dbg_mode_string(mon_mode));
934
935 drv->fw.dbg.n_dest_reg = (dest_v1) ?
936 tlv_len -
937 offsetof(struct iwl_fw_dbg_dest_tlv_v1,
938 reg_ops) :
939 tlv_len -
940 offsetof(struct iwl_fw_dbg_dest_tlv,
941 reg_ops);
942
943 drv->fw.dbg.n_dest_reg /=
944 sizeof(drv->fw.dbg.dest_tlv->reg_ops[0]);
945
946 break;
947 }
948 case IWL_UCODE_TLV_FW_DBG_CONF: {
949 struct iwl_fw_dbg_conf_tlv *conf = (void *)tlv_data;
950
951 if (!pieces->dbg_dest_tlv_init) {
952 IWL_ERR(drv,
953 "Ignore dbg config %d - no destination configured\n",
954 conf->id);
955 break;
956 }
957
958 if (conf->id >= ARRAY_SIZE(drv->fw.dbg.conf_tlv)) {
959 IWL_ERR(drv,
960 "Skip unknown configuration: %d\n",
961 conf->id);
962 break;
963 }
964
965 if (pieces->dbg_conf_tlv[conf->id]) {
966 IWL_ERR(drv,
967 "Ignore duplicate dbg config %d\n",
968 conf->id);
969 break;
970 }
971
972 if (conf->usniffer)
973 usniffer_req = true;
974
975 IWL_INFO(drv, "Found debug configuration: %d\n",
976 conf->id);
977
978 pieces->dbg_conf_tlv[conf->id] = conf;
979 pieces->dbg_conf_tlv_len[conf->id] = tlv_len;
980 break;
981 }
982 case IWL_UCODE_TLV_FW_DBG_TRIGGER: {
983 struct iwl_fw_dbg_trigger_tlv *trigger =
984 (void *)tlv_data;
985 u32 trigger_id = le32_to_cpu(trigger->id);
986
987 if (trigger_id >= ARRAY_SIZE(drv->fw.dbg.trigger_tlv)) {
988 IWL_ERR(drv,
989 "Skip unknown trigger: %u\n",
990 trigger->id);
991 break;
992 }
993
994 if (pieces->dbg_trigger_tlv[trigger_id]) {
995 IWL_ERR(drv,
996 "Ignore duplicate dbg trigger %u\n",
997 trigger->id);
998 break;
999 }
1000
1001 IWL_INFO(drv, "Found debug trigger: %u\n", trigger->id);
1002
1003 pieces->dbg_trigger_tlv[trigger_id] = trigger;
1004 pieces->dbg_trigger_tlv_len[trigger_id] = tlv_len;
1005 break;
1006 }
1007 case IWL_UCODE_TLV_FW_DBG_DUMP_LST: {
1008 if (tlv_len != sizeof(u32)) {
1009 IWL_ERR(drv,
1010 "dbg lst mask size incorrect, skip\n");
1011 break;
1012 }
1013
1014 drv->fw.dbg.dump_mask =
1015 le32_to_cpup((__le32 *)tlv_data);
1016 break;
1017 }
1018 case IWL_UCODE_TLV_SEC_RT_USNIFFER:
1019 *usniffer_images = true;
1020 iwl_store_ucode_sec(pieces, tlv_data,
1021 IWL_UCODE_REGULAR_USNIFFER,
1022 tlv_len);
1023 break;
1024 case IWL_UCODE_TLV_PAGING:
1025 if (tlv_len != sizeof(u32))
1026 goto invalid_tlv_len;
1027 paging_mem_size = le32_to_cpup((__le32 *)tlv_data);
1028
1029 IWL_DEBUG_FW(drv,
1030 "Paging: paging enabled (size = %u bytes)\n",
1031 paging_mem_size);
1032
1033 if (paging_mem_size > MAX_PAGING_IMAGE_SIZE) {
1034 IWL_ERR(drv,
1035 "Paging: driver supports up to %lu bytes for paging image\n",
1036 MAX_PAGING_IMAGE_SIZE);
1037 return -EINVAL;
1038 }
1039
1040 if (paging_mem_size & (FW_PAGING_SIZE - 1)) {
1041 IWL_ERR(drv,
1042 "Paging: image isn't multiple %lu\n",
1043 FW_PAGING_SIZE);
1044 return -EINVAL;
1045 }
1046
1047 drv->fw.img[IWL_UCODE_REGULAR].paging_mem_size =
1048 paging_mem_size;
1049 usniffer_img = IWL_UCODE_REGULAR_USNIFFER;
1050 drv->fw.img[usniffer_img].paging_mem_size =
1051 paging_mem_size;
1052 break;
1053 case IWL_UCODE_TLV_FW_GSCAN_CAPA:
1054 /* ignored */
1055 break;
1056 case IWL_UCODE_TLV_FW_MEM_SEG: {
1057 struct iwl_fw_dbg_mem_seg_tlv *dbg_mem =
1058 (void *)tlv_data;
1059 size_t size;
1060 struct iwl_fw_dbg_mem_seg_tlv *n;
1061
1062 if (tlv_len != (sizeof(*dbg_mem)))
1063 goto invalid_tlv_len;
1064
1065 IWL_DEBUG_INFO(drv, "Found debug memory segment: %u\n",
1066 dbg_mem->data_type);
1067
1068 size = sizeof(*pieces->dbg_mem_tlv) *
1069 (pieces->n_mem_tlv + 1);
1070 n = krealloc(pieces->dbg_mem_tlv, size, GFP_KERNEL);
1071 if (!n)
1072 return -ENOMEM;
1073 pieces->dbg_mem_tlv = n;
1074 pieces->dbg_mem_tlv[pieces->n_mem_tlv] = *dbg_mem;
1075 pieces->n_mem_tlv++;
1076 break;
1077 }
1078 case IWL_UCODE_TLV_IML: {
1079 drv->fw.iml_len = tlv_len;
1080 drv->fw.iml = kmemdup(tlv_data, tlv_len, GFP_KERNEL);
1081 if (!drv->fw.iml)
1082 return -ENOMEM;
1083 break;
1084 }
1085 case IWL_UCODE_TLV_FW_RECOVERY_INFO: {
1086 struct {
1087 __le32 buf_addr;
1088 __le32 buf_size;
1089 } *recov_info = (void *)tlv_data;
1090
1091 if (tlv_len != sizeof(*recov_info))
1092 goto invalid_tlv_len;
1093 capa->error_log_addr =
1094 le32_to_cpu(recov_info->buf_addr);
1095 capa->error_log_size =
1096 le32_to_cpu(recov_info->buf_size);
1097 }
1098 break;
1099 case IWL_UCODE_TLV_FW_FSEQ_VERSION: {
1100 struct {
1101 u8 version[32];
1102 u8 sha1[20];
1103 } *fseq_ver = (void *)tlv_data;
1104
1105 if (tlv_len != sizeof(*fseq_ver))
1106 goto invalid_tlv_len;
1107 IWL_INFO(drv, "TLV_FW_FSEQ_VERSION: %s\n",
1108 fseq_ver->version);
1109 }
1110 break;
1111 case IWL_UCODE_TLV_FW_NUM_STATIONS:
1112 if (tlv_len != sizeof(u32))
1113 goto invalid_tlv_len;
1114 if (le32_to_cpup((__le32 *)tlv_data) >
1115 IWL_MVM_STATION_COUNT_MAX) {
1116 IWL_ERR(drv,
1117 "%d is an invalid number of station\n",
1118 le32_to_cpup((__le32 *)tlv_data));
1119 goto tlv_error;
1120 }
1121 capa->num_stations =
1122 le32_to_cpup((__le32 *)tlv_data);
1123 break;
1124 case IWL_UCODE_TLV_UMAC_DEBUG_ADDRS: {
1125 struct iwl_umac_debug_addrs *dbg_ptrs =
1126 (void *)tlv_data;
1127
1128 if (tlv_len != sizeof(*dbg_ptrs))
1129 goto invalid_tlv_len;
1130 if (drv->trans->trans_cfg->device_family <
1131 IWL_DEVICE_FAMILY_22000)
1132 break;
1133 drv->trans->dbg.umac_error_event_table =
1134 le32_to_cpu(dbg_ptrs->error_info_addr) &
1135 ~FW_ADDR_CACHE_CONTROL;
1136 drv->trans->dbg.error_event_table_tlv_status |=
1137 IWL_ERROR_EVENT_TABLE_UMAC;
1138 break;
1139 }
1140 case IWL_UCODE_TLV_LMAC_DEBUG_ADDRS: {
1141 struct iwl_lmac_debug_addrs *dbg_ptrs =
1142 (void *)tlv_data;
1143
1144 if (tlv_len != sizeof(*dbg_ptrs))
1145 goto invalid_tlv_len;
1146 if (drv->trans->trans_cfg->device_family <
1147 IWL_DEVICE_FAMILY_22000)
1148 break;
1149 drv->trans->dbg.lmac_error_event_table[0] =
1150 le32_to_cpu(dbg_ptrs->error_event_table_ptr) &
1151 ~FW_ADDR_CACHE_CONTROL;
1152 drv->trans->dbg.error_event_table_tlv_status |=
1153 IWL_ERROR_EVENT_TABLE_LMAC1;
1154 break;
1155 }
1156 case IWL_UCODE_TLV_TCM_DEBUG_ADDRS: {
1157 struct iwl_fw_tcm_error_addr *ptr = (void *)tlv_data;
1158
1159 if (tlv_len != sizeof(*ptr))
1160 goto invalid_tlv_len;
1161 drv->trans->dbg.tcm_error_event_table =
1162 le32_to_cpu(ptr->addr) & ~FW_ADDR_CACHE_CONTROL;
1163 drv->trans->dbg.error_event_table_tlv_status |=
1164 IWL_ERROR_EVENT_TABLE_TCM;
1165 break;
1166 }
1167 case IWL_UCODE_TLV_TYPE_DEBUG_INFO:
1168 case IWL_UCODE_TLV_TYPE_BUFFER_ALLOCATION:
1169 case IWL_UCODE_TLV_TYPE_HCMD:
1170 case IWL_UCODE_TLV_TYPE_REGIONS:
1171 case IWL_UCODE_TLV_TYPE_TRIGGERS:
1172 case IWL_UCODE_TLV_TYPE_CONF_SET:
1173 if (iwlwifi_mod_params.enable_ini)
1174 iwl_dbg_tlv_alloc(drv->trans, tlv, false);
1175 break;
1176 case IWL_UCODE_TLV_CMD_VERSIONS:
1177 if (tlv_len % sizeof(struct iwl_fw_cmd_version)) {
1178 IWL_ERR(drv,
1179 "Invalid length for command versions: %u\n",
1180 tlv_len);
1181 tlv_len /= sizeof(struct iwl_fw_cmd_version);
1182 tlv_len *= sizeof(struct iwl_fw_cmd_version);
1183 }
1184 if (WARN_ON(capa->cmd_versions))
1185 return -EINVAL;
1186 capa->cmd_versions = kmemdup(tlv_data, tlv_len,
1187 GFP_KERNEL);
1188 if (!capa->cmd_versions)
1189 return -ENOMEM;
1190 capa->n_cmd_versions =
1191 tlv_len / sizeof(struct iwl_fw_cmd_version);
1192 break;
1193 case IWL_UCODE_TLV_PHY_INTEGRATION_VERSION:
1194 if (drv->fw.phy_integration_ver) {
1195 IWL_ERR(drv,
1196 "phy integration str ignored, already exists\n");
1197 break;
1198 }
1199
1200 drv->fw.phy_integration_ver =
1201 kmemdup(tlv_data, tlv_len, GFP_KERNEL);
1202 if (!drv->fw.phy_integration_ver)
1203 return -ENOMEM;
1204 drv->fw.phy_integration_ver_len = tlv_len;
1205 break;
1206 case IWL_UCODE_TLV_SEC_TABLE_ADDR:
1207 case IWL_UCODE_TLV_D3_KEK_KCK_ADDR:
1208 iwl_drv_set_dump_exclude(drv, tlv_type,
1209 tlv_data, tlv_len);
1210 break;
1211 default:
1212 IWL_DEBUG_INFO(drv, "unknown TLV: %d\n", tlv_type);
1213 break;
1214 }
1215 }
1216
1217 if (!fw_has_capa(capa, IWL_UCODE_TLV_CAPA_USNIFFER_UNIFIED) &&
1218 usniffer_req && !*usniffer_images) {
1219 IWL_ERR(drv,
1220 "user selected to work with usniffer but usniffer image isn't available in ucode package\n");
1221 return -EINVAL;
1222 }
1223
1224 if (len) {
1225 IWL_ERR(drv, "invalid TLV after parsing: %zd\n", len);
1226 iwl_print_hex_dump(drv, IWL_DL_FW, (u8 *)data, len);
1227 return -EINVAL;
1228 }
1229
1230 return 0;
1231
1232 invalid_tlv_len:
1233 IWL_ERR(drv, "TLV %d has invalid size: %u\n", tlv_type, tlv_len);
1234 tlv_error:
1235 iwl_print_hex_dump(drv, IWL_DL_FW, tlv_data, tlv_len);
1236
1237 return -EINVAL;
1238 }
1239
iwl_alloc_ucode(struct iwl_drv * drv,struct iwl_firmware_pieces * pieces,enum iwl_ucode_type type)1240 static int iwl_alloc_ucode(struct iwl_drv *drv,
1241 struct iwl_firmware_pieces *pieces,
1242 enum iwl_ucode_type type)
1243 {
1244 int i;
1245 struct fw_desc *sec;
1246
1247 sec = kcalloc(pieces->img[type].sec_counter, sizeof(*sec), GFP_KERNEL);
1248 if (!sec)
1249 return -ENOMEM;
1250 drv->fw.img[type].sec = sec;
1251 drv->fw.img[type].num_sec = pieces->img[type].sec_counter;
1252
1253 for (i = 0; i < pieces->img[type].sec_counter; i++)
1254 if (iwl_alloc_fw_desc(drv, &sec[i], get_sec(pieces, type, i)))
1255 return -ENOMEM;
1256
1257 return 0;
1258 }
1259
validate_sec_sizes(struct iwl_drv * drv,struct iwl_firmware_pieces * pieces,const struct iwl_cfg * cfg)1260 static int validate_sec_sizes(struct iwl_drv *drv,
1261 struct iwl_firmware_pieces *pieces,
1262 const struct iwl_cfg *cfg)
1263 {
1264 IWL_DEBUG_INFO(drv, "f/w package hdr runtime inst size = %zd\n",
1265 get_sec_size(pieces, IWL_UCODE_REGULAR,
1266 IWL_UCODE_SECTION_INST));
1267 IWL_DEBUG_INFO(drv, "f/w package hdr runtime data size = %zd\n",
1268 get_sec_size(pieces, IWL_UCODE_REGULAR,
1269 IWL_UCODE_SECTION_DATA));
1270 IWL_DEBUG_INFO(drv, "f/w package hdr init inst size = %zd\n",
1271 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST));
1272 IWL_DEBUG_INFO(drv, "f/w package hdr init data size = %zd\n",
1273 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA));
1274
1275 /* Verify that uCode images will fit in card's SRAM. */
1276 if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) >
1277 cfg->max_inst_size) {
1278 IWL_ERR(drv, "uCode instr len %zd too large to fit in\n",
1279 get_sec_size(pieces, IWL_UCODE_REGULAR,
1280 IWL_UCODE_SECTION_INST));
1281 return -1;
1282 }
1283
1284 if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) >
1285 cfg->max_data_size) {
1286 IWL_ERR(drv, "uCode data len %zd too large to fit in\n",
1287 get_sec_size(pieces, IWL_UCODE_REGULAR,
1288 IWL_UCODE_SECTION_DATA));
1289 return -1;
1290 }
1291
1292 if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) >
1293 cfg->max_inst_size) {
1294 IWL_ERR(drv, "uCode init instr len %zd too large to fit in\n",
1295 get_sec_size(pieces, IWL_UCODE_INIT,
1296 IWL_UCODE_SECTION_INST));
1297 return -1;
1298 }
1299
1300 if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA) >
1301 cfg->max_data_size) {
1302 IWL_ERR(drv, "uCode init data len %zd too large to fit in\n",
1303 get_sec_size(pieces, IWL_UCODE_REGULAR,
1304 IWL_UCODE_SECTION_DATA));
1305 return -1;
1306 }
1307 return 0;
1308 }
1309
1310 static struct iwl_op_mode *
_iwl_op_mode_start(struct iwl_drv * drv,struct iwlwifi_opmode_table * op)1311 _iwl_op_mode_start(struct iwl_drv *drv, struct iwlwifi_opmode_table *op)
1312 {
1313 const struct iwl_op_mode_ops *ops = op->ops;
1314 struct dentry *dbgfs_dir = NULL;
1315 struct iwl_op_mode *op_mode = NULL;
1316 int retry, max_retry = !!iwlwifi_mod_params.fw_restart * IWL_MAX_INIT_RETRY;
1317
1318 for (retry = 0; retry <= max_retry; retry++) {
1319
1320 #ifdef CONFIG_IWLWIFI_DEBUGFS
1321 drv->dbgfs_op_mode = debugfs_create_dir(op->name,
1322 drv->dbgfs_drv);
1323 dbgfs_dir = drv->dbgfs_op_mode;
1324 #endif
1325
1326 op_mode = ops->start(drv->trans, drv->trans->cfg,
1327 &drv->fw, dbgfs_dir);
1328
1329 if (op_mode)
1330 return op_mode;
1331
1332 IWL_ERR(drv, "retry init count %d\n", retry);
1333
1334 #ifdef CONFIG_IWLWIFI_DEBUGFS
1335 debugfs_remove_recursive(drv->dbgfs_op_mode);
1336 drv->dbgfs_op_mode = NULL;
1337 #endif
1338 }
1339
1340 return NULL;
1341 }
1342
_iwl_op_mode_stop(struct iwl_drv * drv)1343 static void _iwl_op_mode_stop(struct iwl_drv *drv)
1344 {
1345 /* op_mode can be NULL if its start failed */
1346 if (drv->op_mode) {
1347 iwl_op_mode_stop(drv->op_mode);
1348 drv->op_mode = NULL;
1349
1350 #ifdef CONFIG_IWLWIFI_DEBUGFS
1351 debugfs_remove_recursive(drv->dbgfs_op_mode);
1352 drv->dbgfs_op_mode = NULL;
1353 #endif
1354 }
1355 }
1356
1357 /*
1358 * iwl_req_fw_callback - callback when firmware was loaded
1359 *
1360 * If loaded successfully, copies the firmware into buffers
1361 * for the card to fetch (via DMA).
1362 */
iwl_req_fw_callback(const struct firmware * ucode_raw,void * context)1363 static void iwl_req_fw_callback(const struct firmware *ucode_raw, void *context)
1364 {
1365 struct iwl_drv *drv = context;
1366 struct iwl_fw *fw = &drv->fw;
1367 struct iwl_ucode_header *ucode;
1368 struct iwlwifi_opmode_table *op;
1369 int err;
1370 struct iwl_firmware_pieces *pieces;
1371 const unsigned int api_max = drv->trans->cfg->ucode_api_max;
1372 const unsigned int api_min = drv->trans->cfg->ucode_api_min;
1373 size_t trigger_tlv_sz[FW_DBG_TRIGGER_MAX];
1374 u32 api_ver;
1375 int i;
1376 bool load_module = false;
1377 bool usniffer_images = false;
1378
1379 fw->ucode_capa.max_probe_length = IWL_DEFAULT_MAX_PROBE_LENGTH;
1380 fw->ucode_capa.standard_phy_calibration_size =
1381 IWL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE;
1382 fw->ucode_capa.n_scan_channels = IWL_DEFAULT_SCAN_CHANNELS;
1383 fw->ucode_capa.num_stations = IWL_MVM_STATION_COUNT_MAX;
1384 /* dump all fw memory areas by default */
1385 fw->dbg.dump_mask = 0xffffffff;
1386
1387 pieces = kzalloc(sizeof(*pieces), GFP_KERNEL);
1388 if (!pieces)
1389 goto out_free_fw;
1390
1391 if (!ucode_raw)
1392 goto try_again;
1393
1394 IWL_DEBUG_FW_INFO(drv, "Loaded firmware file '%s' (%zd bytes).\n",
1395 drv->firmware_name, ucode_raw->size);
1396
1397 /* Make sure that we got at least the API version number */
1398 if (ucode_raw->size < 4) {
1399 IWL_ERR(drv, "File size way too small!\n");
1400 goto try_again;
1401 }
1402
1403 /* Data from ucode file: header followed by uCode images */
1404 ucode = (struct iwl_ucode_header *)ucode_raw->data;
1405
1406 if (ucode->ver)
1407 err = iwl_parse_v1_v2_firmware(drv, ucode_raw, pieces);
1408 else
1409 err = iwl_parse_tlv_firmware(drv, ucode_raw, pieces,
1410 &fw->ucode_capa, &usniffer_images);
1411
1412 if (err)
1413 goto try_again;
1414
1415 if (fw_has_api(&drv->fw.ucode_capa, IWL_UCODE_TLV_API_NEW_VERSION))
1416 api_ver = drv->fw.ucode_ver;
1417 else
1418 api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
1419
1420 /*
1421 * api_ver should match the api version forming part of the
1422 * firmware filename ... but we don't check for that and only rely
1423 * on the API version read from firmware header from here on forward
1424 */
1425 if (api_ver < api_min || api_ver > api_max) {
1426 IWL_ERR(drv,
1427 "Driver unable to support your firmware API. "
1428 "Driver supports v%u, firmware is v%u.\n",
1429 api_max, api_ver);
1430 goto try_again;
1431 }
1432
1433 /*
1434 * In mvm uCode there is no difference between data and instructions
1435 * sections.
1436 */
1437 if (fw->type == IWL_FW_DVM && validate_sec_sizes(drv, pieces,
1438 drv->trans->cfg))
1439 goto try_again;
1440
1441 /* Allocate ucode buffers for card's bus-master loading ... */
1442
1443 /* Runtime instructions and 2 copies of data:
1444 * 1) unmodified from disk
1445 * 2) backup cache for save/restore during power-downs
1446 */
1447 for (i = 0; i < IWL_UCODE_TYPE_MAX; i++)
1448 if (iwl_alloc_ucode(drv, pieces, i))
1449 goto out_free_fw;
1450
1451 if (pieces->dbg_dest_tlv_init) {
1452 size_t dbg_dest_size = sizeof(*drv->fw.dbg.dest_tlv) +
1453 sizeof(drv->fw.dbg.dest_tlv->reg_ops[0]) *
1454 drv->fw.dbg.n_dest_reg;
1455
1456 drv->fw.dbg.dest_tlv = kmalloc(dbg_dest_size, GFP_KERNEL);
1457
1458 if (!drv->fw.dbg.dest_tlv)
1459 goto out_free_fw;
1460
1461 if (*pieces->dbg_dest_ver == 0) {
1462 memcpy(drv->fw.dbg.dest_tlv, pieces->dbg_dest_tlv_v1,
1463 dbg_dest_size);
1464 } else {
1465 struct iwl_fw_dbg_dest_tlv_v1 *dest_tlv =
1466 drv->fw.dbg.dest_tlv;
1467
1468 dest_tlv->version = pieces->dbg_dest_tlv->version;
1469 dest_tlv->monitor_mode =
1470 pieces->dbg_dest_tlv->monitor_mode;
1471 dest_tlv->size_power =
1472 pieces->dbg_dest_tlv->size_power;
1473 dest_tlv->wrap_count =
1474 pieces->dbg_dest_tlv->wrap_count;
1475 dest_tlv->write_ptr_reg =
1476 pieces->dbg_dest_tlv->write_ptr_reg;
1477 dest_tlv->base_shift =
1478 pieces->dbg_dest_tlv->base_shift;
1479 memcpy(dest_tlv->reg_ops,
1480 pieces->dbg_dest_tlv->reg_ops,
1481 sizeof(drv->fw.dbg.dest_tlv->reg_ops[0]) *
1482 drv->fw.dbg.n_dest_reg);
1483
1484 /* In version 1 of the destination tlv, which is
1485 * relevant for internal buffer exclusively,
1486 * the base address is part of given with the length
1487 * of the buffer, and the size shift is give instead of
1488 * end shift. We now store these values in base_reg,
1489 * and end shift, and when dumping the data we'll
1490 * manipulate it for extracting both the length and
1491 * base address */
1492 dest_tlv->base_reg = pieces->dbg_dest_tlv->cfg_reg;
1493 dest_tlv->end_shift =
1494 pieces->dbg_dest_tlv->size_shift;
1495 }
1496 }
1497
1498 for (i = 0; i < ARRAY_SIZE(drv->fw.dbg.conf_tlv); i++) {
1499 if (pieces->dbg_conf_tlv[i]) {
1500 drv->fw.dbg.conf_tlv[i] =
1501 kmemdup(pieces->dbg_conf_tlv[i],
1502 pieces->dbg_conf_tlv_len[i],
1503 GFP_KERNEL);
1504 if (!drv->fw.dbg.conf_tlv[i])
1505 goto out_free_fw;
1506 }
1507 }
1508
1509 memset(&trigger_tlv_sz, 0xff, sizeof(trigger_tlv_sz));
1510
1511 trigger_tlv_sz[FW_DBG_TRIGGER_MISSED_BEACONS] =
1512 sizeof(struct iwl_fw_dbg_trigger_missed_bcon);
1513 trigger_tlv_sz[FW_DBG_TRIGGER_CHANNEL_SWITCH] = 0;
1514 trigger_tlv_sz[FW_DBG_TRIGGER_FW_NOTIF] =
1515 sizeof(struct iwl_fw_dbg_trigger_cmd);
1516 trigger_tlv_sz[FW_DBG_TRIGGER_MLME] =
1517 sizeof(struct iwl_fw_dbg_trigger_mlme);
1518 trigger_tlv_sz[FW_DBG_TRIGGER_STATS] =
1519 sizeof(struct iwl_fw_dbg_trigger_stats);
1520 trigger_tlv_sz[FW_DBG_TRIGGER_RSSI] =
1521 sizeof(struct iwl_fw_dbg_trigger_low_rssi);
1522 trigger_tlv_sz[FW_DBG_TRIGGER_TXQ_TIMERS] =
1523 sizeof(struct iwl_fw_dbg_trigger_txq_timer);
1524 trigger_tlv_sz[FW_DBG_TRIGGER_TIME_EVENT] =
1525 sizeof(struct iwl_fw_dbg_trigger_time_event);
1526 trigger_tlv_sz[FW_DBG_TRIGGER_BA] =
1527 sizeof(struct iwl_fw_dbg_trigger_ba);
1528 trigger_tlv_sz[FW_DBG_TRIGGER_TDLS] =
1529 sizeof(struct iwl_fw_dbg_trigger_tdls);
1530
1531 for (i = 0; i < ARRAY_SIZE(drv->fw.dbg.trigger_tlv); i++) {
1532 if (pieces->dbg_trigger_tlv[i]) {
1533 /*
1534 * If the trigger isn't long enough, WARN and exit.
1535 * Someone is trying to debug something and he won't
1536 * be able to catch the bug he is trying to chase.
1537 * We'd better be noisy to be sure he knows what's
1538 * going on.
1539 */
1540 if (WARN_ON(pieces->dbg_trigger_tlv_len[i] <
1541 (trigger_tlv_sz[i] +
1542 sizeof(struct iwl_fw_dbg_trigger_tlv))))
1543 goto out_free_fw;
1544 drv->fw.dbg.trigger_tlv_len[i] =
1545 pieces->dbg_trigger_tlv_len[i];
1546 drv->fw.dbg.trigger_tlv[i] =
1547 kmemdup(pieces->dbg_trigger_tlv[i],
1548 drv->fw.dbg.trigger_tlv_len[i],
1549 GFP_KERNEL);
1550 if (!drv->fw.dbg.trigger_tlv[i])
1551 goto out_free_fw;
1552 }
1553 }
1554
1555 /* Now that we can no longer fail, copy information */
1556
1557 drv->fw.dbg.mem_tlv = pieces->dbg_mem_tlv;
1558 pieces->dbg_mem_tlv = NULL;
1559 drv->fw.dbg.n_mem_tlv = pieces->n_mem_tlv;
1560
1561 /*
1562 * The (size - 16) / 12 formula is based on the information recorded
1563 * for each event, which is of mode 1 (including timestamp) for all
1564 * new microcodes that include this information.
1565 */
1566 fw->init_evtlog_ptr = pieces->init_evtlog_ptr;
1567 if (pieces->init_evtlog_size)
1568 fw->init_evtlog_size = (pieces->init_evtlog_size - 16)/12;
1569 else
1570 fw->init_evtlog_size =
1571 drv->trans->trans_cfg->base_params->max_event_log_size;
1572 fw->init_errlog_ptr = pieces->init_errlog_ptr;
1573 fw->inst_evtlog_ptr = pieces->inst_evtlog_ptr;
1574 if (pieces->inst_evtlog_size)
1575 fw->inst_evtlog_size = (pieces->inst_evtlog_size - 16)/12;
1576 else
1577 fw->inst_evtlog_size =
1578 drv->trans->trans_cfg->base_params->max_event_log_size;
1579 fw->inst_errlog_ptr = pieces->inst_errlog_ptr;
1580
1581 /*
1582 * figure out the offset of chain noise reset and gain commands
1583 * base on the size of standard phy calibration commands table size
1584 */
1585 if (fw->ucode_capa.standard_phy_calibration_size >
1586 IWL_MAX_PHY_CALIBRATE_TBL_SIZE)
1587 fw->ucode_capa.standard_phy_calibration_size =
1588 IWL_MAX_STANDARD_PHY_CALIBRATE_TBL_SIZE;
1589
1590 /* We have our copies now, allow OS release its copies */
1591 release_firmware(ucode_raw);
1592
1593 mutex_lock(&iwlwifi_opmode_table_mtx);
1594 switch (fw->type) {
1595 case IWL_FW_DVM:
1596 op = &iwlwifi_opmode_table[DVM_OP_MODE];
1597 break;
1598 default:
1599 WARN(1, "Invalid fw type %d\n", fw->type);
1600 fallthrough;
1601 case IWL_FW_MVM:
1602 op = &iwlwifi_opmode_table[MVM_OP_MODE];
1603 break;
1604 }
1605
1606 IWL_INFO(drv, "loaded firmware version %s op_mode %s\n",
1607 drv->fw.fw_version, op->name);
1608
1609 iwl_dbg_tlv_load_bin(drv->trans->dev, drv->trans);
1610
1611 /* add this device to the list of devices using this op_mode */
1612 list_add_tail(&drv->list, &op->drv);
1613
1614 if (op->ops) {
1615 drv->op_mode = _iwl_op_mode_start(drv, op);
1616
1617 if (!drv->op_mode) {
1618 mutex_unlock(&iwlwifi_opmode_table_mtx);
1619 goto out_unbind;
1620 }
1621 } else {
1622 load_module = true;
1623 }
1624 mutex_unlock(&iwlwifi_opmode_table_mtx);
1625
1626 /*
1627 * Complete the firmware request last so that
1628 * a driver unbind (stop) doesn't run while we
1629 * are doing the start() above.
1630 */
1631 complete(&drv->request_firmware_complete);
1632
1633 /*
1634 * Load the module last so we don't block anything
1635 * else from proceeding if the module fails to load
1636 * or hangs loading.
1637 */
1638 if (load_module) {
1639 request_module("%s", op->name);
1640 #ifdef CONFIG_IWLWIFI_OPMODE_MODULAR
1641 if (err)
1642 IWL_ERR(drv,
1643 "failed to load module %s (error %d), is dynamic loading enabled?\n",
1644 op->name, err);
1645 #endif
1646 }
1647 goto free;
1648
1649 try_again:
1650 /* try next, if any */
1651 release_firmware(ucode_raw);
1652 if (iwl_request_firmware(drv, false))
1653 goto out_unbind;
1654 goto free;
1655
1656 out_free_fw:
1657 release_firmware(ucode_raw);
1658 out_unbind:
1659 complete(&drv->request_firmware_complete);
1660 device_release_driver(drv->trans->dev);
1661 free:
1662 if (pieces) {
1663 for (i = 0; i < ARRAY_SIZE(pieces->img); i++)
1664 kfree(pieces->img[i].sec);
1665 kfree(pieces->dbg_mem_tlv);
1666 kfree(pieces);
1667 }
1668 }
1669
iwl_drv_start(struct iwl_trans * trans)1670 struct iwl_drv *iwl_drv_start(struct iwl_trans *trans)
1671 {
1672 struct iwl_drv *drv;
1673 int ret;
1674
1675 drv = kzalloc(sizeof(*drv), GFP_KERNEL);
1676 if (!drv) {
1677 ret = -ENOMEM;
1678 goto err;
1679 }
1680
1681 drv->trans = trans;
1682 drv->dev = trans->dev;
1683
1684 init_completion(&drv->request_firmware_complete);
1685 INIT_LIST_HEAD(&drv->list);
1686
1687 #ifdef CONFIG_IWLWIFI_DEBUGFS
1688 /* Create the device debugfs entries. */
1689 drv->dbgfs_drv = debugfs_create_dir(dev_name(trans->dev),
1690 iwl_dbgfs_root);
1691
1692 /* Create transport layer debugfs dir */
1693 drv->trans->dbgfs_dir = debugfs_create_dir("trans", drv->dbgfs_drv);
1694 #endif
1695
1696 drv->trans->dbg.domains_bitmap = IWL_TRANS_FW_DBG_DOMAIN(drv->trans);
1697
1698 ret = iwl_request_firmware(drv, true);
1699 if (ret) {
1700 IWL_ERR(trans, "Couldn't request the fw\n");
1701 goto err_fw;
1702 }
1703
1704 return drv;
1705
1706 err_fw:
1707 #ifdef CONFIG_IWLWIFI_DEBUGFS
1708 debugfs_remove_recursive(drv->dbgfs_drv);
1709 iwl_dbg_tlv_free(drv->trans);
1710 #endif
1711 kfree(drv);
1712 err:
1713 return ERR_PTR(ret);
1714 }
1715
iwl_drv_stop(struct iwl_drv * drv)1716 void iwl_drv_stop(struct iwl_drv *drv)
1717 {
1718 wait_for_completion(&drv->request_firmware_complete);
1719
1720 _iwl_op_mode_stop(drv);
1721
1722 iwl_dealloc_ucode(drv);
1723
1724 mutex_lock(&iwlwifi_opmode_table_mtx);
1725 /*
1726 * List is empty (this item wasn't added)
1727 * when firmware loading failed -- in that
1728 * case we can't remove it from any list.
1729 */
1730 if (!list_empty(&drv->list))
1731 list_del(&drv->list);
1732 mutex_unlock(&iwlwifi_opmode_table_mtx);
1733
1734 #ifdef CONFIG_IWLWIFI_DEBUGFS
1735 drv->trans->ops->debugfs_cleanup(drv->trans);
1736
1737 debugfs_remove_recursive(drv->dbgfs_drv);
1738 #endif
1739
1740 iwl_dbg_tlv_free(drv->trans);
1741
1742 kfree(drv);
1743 }
1744
1745
1746 /* shared module parameters */
1747 struct iwl_mod_params iwlwifi_mod_params = {
1748 .fw_restart = true,
1749 .bt_coex_active = true,
1750 .power_level = IWL_POWER_INDEX_1,
1751 .uapsd_disable = IWL_DISABLE_UAPSD_BSS | IWL_DISABLE_UAPSD_P2P_CLIENT,
1752 .enable_ini = true,
1753 /* the rest are 0 by default */
1754 };
1755 IWL_EXPORT_SYMBOL(iwlwifi_mod_params);
1756
iwl_opmode_register(const char * name,const struct iwl_op_mode_ops * ops)1757 int iwl_opmode_register(const char *name, const struct iwl_op_mode_ops *ops)
1758 {
1759 int i;
1760 struct iwl_drv *drv;
1761 struct iwlwifi_opmode_table *op;
1762
1763 mutex_lock(&iwlwifi_opmode_table_mtx);
1764 for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++) {
1765 op = &iwlwifi_opmode_table[i];
1766 if (strcmp(op->name, name))
1767 continue;
1768 op->ops = ops;
1769 /* TODO: need to handle exceptional case */
1770 list_for_each_entry(drv, &op->drv, list)
1771 drv->op_mode = _iwl_op_mode_start(drv, op);
1772
1773 mutex_unlock(&iwlwifi_opmode_table_mtx);
1774 return 0;
1775 }
1776 mutex_unlock(&iwlwifi_opmode_table_mtx);
1777 return -EIO;
1778 }
1779 IWL_EXPORT_SYMBOL(iwl_opmode_register);
1780
iwl_opmode_deregister(const char * name)1781 void iwl_opmode_deregister(const char *name)
1782 {
1783 int i;
1784 struct iwl_drv *drv;
1785
1786 mutex_lock(&iwlwifi_opmode_table_mtx);
1787 for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++) {
1788 if (strcmp(iwlwifi_opmode_table[i].name, name))
1789 continue;
1790 iwlwifi_opmode_table[i].ops = NULL;
1791
1792 /* call the stop routine for all devices */
1793 list_for_each_entry(drv, &iwlwifi_opmode_table[i].drv, list)
1794 _iwl_op_mode_stop(drv);
1795
1796 mutex_unlock(&iwlwifi_opmode_table_mtx);
1797 return;
1798 }
1799 mutex_unlock(&iwlwifi_opmode_table_mtx);
1800 }
1801 IWL_EXPORT_SYMBOL(iwl_opmode_deregister);
1802
iwl_drv_init(void)1803 static int __init iwl_drv_init(void)
1804 {
1805 int i, err;
1806
1807 for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++)
1808 INIT_LIST_HEAD(&iwlwifi_opmode_table[i].drv);
1809
1810 pr_info(DRV_DESCRIPTION "\n");
1811
1812 #ifdef CONFIG_IWLWIFI_DEBUGFS
1813 /* Create the root of iwlwifi debugfs subsystem. */
1814 iwl_dbgfs_root = debugfs_create_dir(DRV_NAME, NULL);
1815 #endif
1816
1817 err = iwl_pci_register_driver();
1818 if (err)
1819 goto cleanup_debugfs;
1820
1821 return 0;
1822
1823 cleanup_debugfs:
1824 #ifdef CONFIG_IWLWIFI_DEBUGFS
1825 debugfs_remove_recursive(iwl_dbgfs_root);
1826 #endif
1827 return err;
1828 }
1829 module_init(iwl_drv_init);
1830
iwl_drv_exit(void)1831 static void __exit iwl_drv_exit(void)
1832 {
1833 iwl_pci_unregister_driver();
1834
1835 #ifdef CONFIG_IWLWIFI_DEBUGFS
1836 debugfs_remove_recursive(iwl_dbgfs_root);
1837 #endif
1838 }
1839 module_exit(iwl_drv_exit);
1840
1841 #ifdef CONFIG_IWLWIFI_DEBUG
1842 module_param_named(debug, iwlwifi_mod_params.debug_level, uint, 0644);
1843 MODULE_PARM_DESC(debug, "debug output mask");
1844 #endif
1845
1846 module_param_named(swcrypto, iwlwifi_mod_params.swcrypto, int, 0444);
1847 MODULE_PARM_DESC(swcrypto, "using crypto in software (default 0 [hardware])");
1848 module_param_named(11n_disable, iwlwifi_mod_params.disable_11n, uint, 0444);
1849 MODULE_PARM_DESC(11n_disable,
1850 "disable 11n functionality, bitmap: 1: full, 2: disable agg TX, 4: disable agg RX, 8 enable agg TX");
1851 module_param_named(amsdu_size, iwlwifi_mod_params.amsdu_size, int, 0444);
1852 MODULE_PARM_DESC(amsdu_size,
1853 "amsdu size 0: 12K for multi Rx queue devices, 2K for AX210 devices, "
1854 "4K for other devices 1:4K 2:8K 3:12K (16K buffers) 4: 2K (default 0)");
1855 module_param_named(fw_restart, iwlwifi_mod_params.fw_restart, bool, 0444);
1856 MODULE_PARM_DESC(fw_restart, "restart firmware in case of error (default true)");
1857
1858 module_param_named(nvm_file, iwlwifi_mod_params.nvm_file, charp, 0444);
1859 MODULE_PARM_DESC(nvm_file, "NVM file name");
1860
1861 module_param_named(uapsd_disable, iwlwifi_mod_params.uapsd_disable, uint, 0644);
1862 MODULE_PARM_DESC(uapsd_disable,
1863 "disable U-APSD functionality bitmap 1: BSS 2: P2P Client (default: 3)");
1864 module_param_named(enable_ini, iwlwifi_mod_params.enable_ini,
1865 bool, S_IRUGO | S_IWUSR);
1866 MODULE_PARM_DESC(enable_ini,
1867 "Enable debug INI TLV FW debug infrastructure (default: true");
1868
1869 /*
1870 * set bt_coex_active to true, uCode will do kill/defer
1871 * every time the priority line is asserted (BT is sending signals on the
1872 * priority line in the PCIx).
1873 * set bt_coex_active to false, uCode will ignore the BT activity and
1874 * perform the normal operation
1875 *
1876 * User might experience transmit issue on some platform due to WiFi/BT
1877 * co-exist problem. The possible behaviors are:
1878 * Able to scan and finding all the available AP
1879 * Not able to associate with any AP
1880 * On those platforms, WiFi communication can be restored by set
1881 * "bt_coex_active" module parameter to "false"
1882 *
1883 * default: bt_coex_active = true (BT_COEX_ENABLE)
1884 */
1885 module_param_named(bt_coex_active, iwlwifi_mod_params.bt_coex_active,
1886 bool, 0444);
1887 MODULE_PARM_DESC(bt_coex_active, "enable wifi/bt co-exist (default: enable)");
1888
1889 module_param_named(led_mode, iwlwifi_mod_params.led_mode, int, 0444);
1890 MODULE_PARM_DESC(led_mode, "0=system default, "
1891 "1=On(RF On)/Off(RF Off), 2=blinking, 3=Off (default: 0)");
1892
1893 module_param_named(power_save, iwlwifi_mod_params.power_save, bool, 0444);
1894 MODULE_PARM_DESC(power_save,
1895 "enable WiFi power management (default: disable)");
1896
1897 module_param_named(power_level, iwlwifi_mod_params.power_level, int, 0444);
1898 MODULE_PARM_DESC(power_level,
1899 "default power save level (range from 1 - 5, default: 1)");
1900
1901 module_param_named(disable_11ac, iwlwifi_mod_params.disable_11ac, bool, 0444);
1902 MODULE_PARM_DESC(disable_11ac, "Disable VHT capabilities (default: false)");
1903
1904 module_param_named(remove_when_gone,
1905 iwlwifi_mod_params.remove_when_gone, bool,
1906 0444);
1907 MODULE_PARM_DESC(remove_when_gone,
1908 "Remove dev from PCIe bus if it is deemed inaccessible (default: false)");
1909
1910 module_param_named(disable_11ax, iwlwifi_mod_params.disable_11ax, bool,
1911 S_IRUGO);
1912 MODULE_PARM_DESC(disable_11ax, "Disable HE capabilities (default: false)");
1913