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