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