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