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