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 
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 	    (drv->trans->hw_rev_step != SILICON_B_STEP &&
167 	     drv->trans->hw_rev_step != 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 static void iwl_drv_set_dump_exclude(struct iwl_drv *drv,
553 				     enum iwl_ucode_tlv_type tlv_type,
554 				     const void *tlv_data, u32 tlv_len)
555 {
556 	const struct iwl_fw_dump_exclude *fw = tlv_data;
557 	struct iwl_dump_exclude *excl;
558 
559 	if (tlv_len < sizeof(*fw))
560 		return;
561 
562 	if (tlv_type == IWL_UCODE_TLV_SEC_TABLE_ADDR) {
563 		excl = &drv->fw.dump_excl[0];
564 
565 		/* second time we find this, it's for WoWLAN */
566 		if (excl->addr)
567 			excl = &drv->fw.dump_excl_wowlan[0];
568 	} else if (fw_has_capa(&drv->fw.ucode_capa,
569 			       IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG)) {
570 		/* IWL_UCODE_TLV_D3_KEK_KCK_ADDR is regular image */
571 		excl = &drv->fw.dump_excl[0];
572 	} else {
573 		/* IWL_UCODE_TLV_D3_KEK_KCK_ADDR is WoWLAN image */
574 		excl = &drv->fw.dump_excl_wowlan[0];
575 	}
576 
577 	if (excl->addr)
578 		excl++;
579 
580 	if (excl->addr) {
581 		IWL_DEBUG_FW_INFO(drv, "found too many excludes in fw file\n");
582 		return;
583 	}
584 
585 	excl->addr = le32_to_cpu(fw->addr) & ~FW_ADDR_CACHE_CONTROL;
586 	excl->size = le32_to_cpu(fw->size);
587 }
588 
589 static void iwl_parse_dbg_tlv_assert_tables(struct iwl_drv *drv,
590 					    const struct iwl_ucode_tlv *tlv)
591 {
592 	const struct iwl_fw_ini_region_tlv *region;
593 	u32 length = le32_to_cpu(tlv->length);
594 	u32 addr;
595 
596 	if (length < offsetof(typeof(*region), special_mem) +
597 		     sizeof(region->special_mem))
598 		return;
599 
600 	region = (void *)tlv->data;
601 	addr = le32_to_cpu(region->special_mem.base_addr);
602 	addr += le32_to_cpu(region->special_mem.offset);
603 	addr &= ~FW_ADDR_CACHE_CONTROL;
604 
605 	if (region->type != IWL_FW_INI_REGION_SPECIAL_DEVICE_MEMORY)
606 		return;
607 
608 	switch (region->sub_type) {
609 	case IWL_FW_INI_REGION_DEVICE_MEMORY_SUBTYPE_UMAC_ERROR_TABLE:
610 		drv->trans->dbg.umac_error_event_table = addr;
611 		drv->trans->dbg.error_event_table_tlv_status |=
612 			IWL_ERROR_EVENT_TABLE_UMAC;
613 		break;
614 	case IWL_FW_INI_REGION_DEVICE_MEMORY_SUBTYPE_LMAC_1_ERROR_TABLE:
615 		drv->trans->dbg.lmac_error_event_table[0] = addr;
616 		drv->trans->dbg.error_event_table_tlv_status |=
617 			IWL_ERROR_EVENT_TABLE_LMAC1;
618 		break;
619 	case IWL_FW_INI_REGION_DEVICE_MEMORY_SUBTYPE_LMAC_2_ERROR_TABLE:
620 		drv->trans->dbg.lmac_error_event_table[1] = addr;
621 		drv->trans->dbg.error_event_table_tlv_status |=
622 			IWL_ERROR_EVENT_TABLE_LMAC2;
623 		break;
624 	case IWL_FW_INI_REGION_DEVICE_MEMORY_SUBTYPE_TCM_1_ERROR_TABLE:
625 		drv->trans->dbg.tcm_error_event_table = addr;
626 		drv->trans->dbg.error_event_table_tlv_status |=
627 			IWL_ERROR_EVENT_TABLE_TCM;
628 		break;
629 	default:
630 		break;
631 	}
632 }
633 
634 static int iwl_parse_tlv_firmware(struct iwl_drv *drv,
635 				const struct firmware *ucode_raw,
636 				struct iwl_firmware_pieces *pieces,
637 				struct iwl_ucode_capabilities *capa,
638 				bool *usniffer_images)
639 {
640 	struct iwl_tlv_ucode_header *ucode = (void *)ucode_raw->data;
641 	const struct iwl_ucode_tlv *tlv;
642 	size_t len = ucode_raw->size;
643 	const u8 *data;
644 	u32 tlv_len;
645 	u32 usniffer_img;
646 	enum iwl_ucode_tlv_type tlv_type;
647 	const u8 *tlv_data;
648 	char buildstr[25];
649 	u32 build, paging_mem_size;
650 	int num_of_cpus;
651 	bool usniffer_req = false;
652 
653 	if (len < sizeof(*ucode)) {
654 		IWL_ERR(drv, "uCode has invalid length: %zd\n", len);
655 		return -EINVAL;
656 	}
657 
658 	if (ucode->magic != cpu_to_le32(IWL_TLV_UCODE_MAGIC)) {
659 		IWL_ERR(drv, "invalid uCode magic: 0X%x\n",
660 			le32_to_cpu(ucode->magic));
661 		return -EINVAL;
662 	}
663 
664 	drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
665 	memcpy(drv->fw.human_readable, ucode->human_readable,
666 	       sizeof(drv->fw.human_readable));
667 	build = le32_to_cpu(ucode->build);
668 
669 	if (build)
670 		sprintf(buildstr, " build %u", build);
671 	else
672 		buildstr[0] = '\0';
673 
674 	snprintf(drv->fw.fw_version,
675 		 sizeof(drv->fw.fw_version),
676 		 "%u.%u.%u.%u%s %s",
677 		 IWL_UCODE_MAJOR(drv->fw.ucode_ver),
678 		 IWL_UCODE_MINOR(drv->fw.ucode_ver),
679 		 IWL_UCODE_API(drv->fw.ucode_ver),
680 		 IWL_UCODE_SERIAL(drv->fw.ucode_ver),
681 		 buildstr, iwl_reduced_fw_name(drv));
682 
683 	data = ucode->data;
684 
685 	len -= sizeof(*ucode);
686 
687 	while (len >= sizeof(*tlv)) {
688 		len -= sizeof(*tlv);
689 		tlv = (void *)data;
690 
691 		tlv_len = le32_to_cpu(tlv->length);
692 		tlv_type = le32_to_cpu(tlv->type);
693 		tlv_data = tlv->data;
694 
695 		if (len < tlv_len) {
696 			IWL_ERR(drv, "invalid TLV len: %zd/%u\n",
697 				len, tlv_len);
698 			return -EINVAL;
699 		}
700 		len -= ALIGN(tlv_len, 4);
701 		data += sizeof(*tlv) + ALIGN(tlv_len, 4);
702 
703 		switch (tlv_type) {
704 		case IWL_UCODE_TLV_INST:
705 			set_sec_data(pieces, IWL_UCODE_REGULAR,
706 				     IWL_UCODE_SECTION_INST, tlv_data);
707 			set_sec_size(pieces, IWL_UCODE_REGULAR,
708 				     IWL_UCODE_SECTION_INST, tlv_len);
709 			set_sec_offset(pieces, IWL_UCODE_REGULAR,
710 				       IWL_UCODE_SECTION_INST,
711 				       IWLAGN_RTC_INST_LOWER_BOUND);
712 			break;
713 		case IWL_UCODE_TLV_DATA:
714 			set_sec_data(pieces, IWL_UCODE_REGULAR,
715 				     IWL_UCODE_SECTION_DATA, tlv_data);
716 			set_sec_size(pieces, IWL_UCODE_REGULAR,
717 				     IWL_UCODE_SECTION_DATA, tlv_len);
718 			set_sec_offset(pieces, IWL_UCODE_REGULAR,
719 				       IWL_UCODE_SECTION_DATA,
720 				       IWLAGN_RTC_DATA_LOWER_BOUND);
721 			break;
722 		case IWL_UCODE_TLV_INIT:
723 			set_sec_data(pieces, IWL_UCODE_INIT,
724 				     IWL_UCODE_SECTION_INST, tlv_data);
725 			set_sec_size(pieces, IWL_UCODE_INIT,
726 				     IWL_UCODE_SECTION_INST, tlv_len);
727 			set_sec_offset(pieces, IWL_UCODE_INIT,
728 				       IWL_UCODE_SECTION_INST,
729 				       IWLAGN_RTC_INST_LOWER_BOUND);
730 			break;
731 		case IWL_UCODE_TLV_INIT_DATA:
732 			set_sec_data(pieces, IWL_UCODE_INIT,
733 				     IWL_UCODE_SECTION_DATA, tlv_data);
734 			set_sec_size(pieces, IWL_UCODE_INIT,
735 				     IWL_UCODE_SECTION_DATA, tlv_len);
736 			set_sec_offset(pieces, IWL_UCODE_INIT,
737 				       IWL_UCODE_SECTION_DATA,
738 				       IWLAGN_RTC_DATA_LOWER_BOUND);
739 			break;
740 		case IWL_UCODE_TLV_BOOT:
741 			IWL_ERR(drv, "Found unexpected BOOT ucode\n");
742 			break;
743 		case IWL_UCODE_TLV_PROBE_MAX_LEN:
744 			if (tlv_len != sizeof(u32))
745 				goto invalid_tlv_len;
746 			capa->max_probe_length =
747 					le32_to_cpup((__le32 *)tlv_data);
748 			break;
749 		case IWL_UCODE_TLV_PAN:
750 			if (tlv_len)
751 				goto invalid_tlv_len;
752 			capa->flags |= IWL_UCODE_TLV_FLAGS_PAN;
753 			break;
754 		case IWL_UCODE_TLV_FLAGS:
755 			/* must be at least one u32 */
756 			if (tlv_len < sizeof(u32))
757 				goto invalid_tlv_len;
758 			/* and a proper number of u32s */
759 			if (tlv_len % sizeof(u32))
760 				goto invalid_tlv_len;
761 			/*
762 			 * This driver only reads the first u32 as
763 			 * right now no more features are defined,
764 			 * if that changes then either the driver
765 			 * will not work with the new firmware, or
766 			 * it'll not take advantage of new features.
767 			 */
768 			capa->flags = le32_to_cpup((__le32 *)tlv_data);
769 			break;
770 		case IWL_UCODE_TLV_API_CHANGES_SET:
771 			if (tlv_len != sizeof(struct iwl_ucode_api))
772 				goto invalid_tlv_len;
773 			iwl_set_ucode_api_flags(drv, tlv_data, capa);
774 			break;
775 		case IWL_UCODE_TLV_ENABLED_CAPABILITIES:
776 			if (tlv_len != sizeof(struct iwl_ucode_capa))
777 				goto invalid_tlv_len;
778 			iwl_set_ucode_capabilities(drv, tlv_data, capa);
779 			break;
780 		case IWL_UCODE_TLV_INIT_EVTLOG_PTR:
781 			if (tlv_len != sizeof(u32))
782 				goto invalid_tlv_len;
783 			pieces->init_evtlog_ptr =
784 					le32_to_cpup((__le32 *)tlv_data);
785 			break;
786 		case IWL_UCODE_TLV_INIT_EVTLOG_SIZE:
787 			if (tlv_len != sizeof(u32))
788 				goto invalid_tlv_len;
789 			pieces->init_evtlog_size =
790 					le32_to_cpup((__le32 *)tlv_data);
791 			break;
792 		case IWL_UCODE_TLV_INIT_ERRLOG_PTR:
793 			if (tlv_len != sizeof(u32))
794 				goto invalid_tlv_len;
795 			pieces->init_errlog_ptr =
796 					le32_to_cpup((__le32 *)tlv_data);
797 			break;
798 		case IWL_UCODE_TLV_RUNT_EVTLOG_PTR:
799 			if (tlv_len != sizeof(u32))
800 				goto invalid_tlv_len;
801 			pieces->inst_evtlog_ptr =
802 					le32_to_cpup((__le32 *)tlv_data);
803 			break;
804 		case IWL_UCODE_TLV_RUNT_EVTLOG_SIZE:
805 			if (tlv_len != sizeof(u32))
806 				goto invalid_tlv_len;
807 			pieces->inst_evtlog_size =
808 					le32_to_cpup((__le32 *)tlv_data);
809 			break;
810 		case IWL_UCODE_TLV_RUNT_ERRLOG_PTR:
811 			if (tlv_len != sizeof(u32))
812 				goto invalid_tlv_len;
813 			pieces->inst_errlog_ptr =
814 					le32_to_cpup((__le32 *)tlv_data);
815 			break;
816 		case IWL_UCODE_TLV_ENHANCE_SENS_TBL:
817 			if (tlv_len)
818 				goto invalid_tlv_len;
819 			drv->fw.enhance_sensitivity_table = true;
820 			break;
821 		case IWL_UCODE_TLV_WOWLAN_INST:
822 			set_sec_data(pieces, IWL_UCODE_WOWLAN,
823 				     IWL_UCODE_SECTION_INST, tlv_data);
824 			set_sec_size(pieces, IWL_UCODE_WOWLAN,
825 				     IWL_UCODE_SECTION_INST, tlv_len);
826 			set_sec_offset(pieces, IWL_UCODE_WOWLAN,
827 				       IWL_UCODE_SECTION_INST,
828 				       IWLAGN_RTC_INST_LOWER_BOUND);
829 			break;
830 		case IWL_UCODE_TLV_WOWLAN_DATA:
831 			set_sec_data(pieces, IWL_UCODE_WOWLAN,
832 				     IWL_UCODE_SECTION_DATA, tlv_data);
833 			set_sec_size(pieces, IWL_UCODE_WOWLAN,
834 				     IWL_UCODE_SECTION_DATA, tlv_len);
835 			set_sec_offset(pieces, IWL_UCODE_WOWLAN,
836 				       IWL_UCODE_SECTION_DATA,
837 				       IWLAGN_RTC_DATA_LOWER_BOUND);
838 			break;
839 		case IWL_UCODE_TLV_PHY_CALIBRATION_SIZE:
840 			if (tlv_len != sizeof(u32))
841 				goto invalid_tlv_len;
842 			capa->standard_phy_calibration_size =
843 					le32_to_cpup((__le32 *)tlv_data);
844 			break;
845 		case IWL_UCODE_TLV_SEC_RT:
846 			iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_REGULAR,
847 					    tlv_len);
848 			drv->fw.type = IWL_FW_MVM;
849 			break;
850 		case IWL_UCODE_TLV_SEC_INIT:
851 			iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_INIT,
852 					    tlv_len);
853 			drv->fw.type = IWL_FW_MVM;
854 			break;
855 		case IWL_UCODE_TLV_SEC_WOWLAN:
856 			iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_WOWLAN,
857 					    tlv_len);
858 			drv->fw.type = IWL_FW_MVM;
859 			break;
860 		case IWL_UCODE_TLV_DEF_CALIB:
861 			if (tlv_len != sizeof(struct iwl_tlv_calib_data))
862 				goto invalid_tlv_len;
863 			if (iwl_set_default_calib(drv, tlv_data))
864 				goto tlv_error;
865 			break;
866 		case IWL_UCODE_TLV_PHY_SKU:
867 			if (tlv_len != sizeof(u32))
868 				goto invalid_tlv_len;
869 			drv->fw.phy_config = le32_to_cpup((__le32 *)tlv_data);
870 			drv->fw.valid_tx_ant = (drv->fw.phy_config &
871 						FW_PHY_CFG_TX_CHAIN) >>
872 						FW_PHY_CFG_TX_CHAIN_POS;
873 			drv->fw.valid_rx_ant = (drv->fw.phy_config &
874 						FW_PHY_CFG_RX_CHAIN) >>
875 						FW_PHY_CFG_RX_CHAIN_POS;
876 			break;
877 		case IWL_UCODE_TLV_SECURE_SEC_RT:
878 			iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_REGULAR,
879 					    tlv_len);
880 			drv->fw.type = IWL_FW_MVM;
881 			break;
882 		case IWL_UCODE_TLV_SECURE_SEC_INIT:
883 			iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_INIT,
884 					    tlv_len);
885 			drv->fw.type = IWL_FW_MVM;
886 			break;
887 		case IWL_UCODE_TLV_SECURE_SEC_WOWLAN:
888 			iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_WOWLAN,
889 					    tlv_len);
890 			drv->fw.type = IWL_FW_MVM;
891 			break;
892 		case IWL_UCODE_TLV_NUM_OF_CPU:
893 			if (tlv_len != sizeof(u32))
894 				goto invalid_tlv_len;
895 			num_of_cpus =
896 				le32_to_cpup((__le32 *)tlv_data);
897 
898 			if (num_of_cpus == 2) {
899 				drv->fw.img[IWL_UCODE_REGULAR].is_dual_cpus =
900 					true;
901 				drv->fw.img[IWL_UCODE_INIT].is_dual_cpus =
902 					true;
903 				drv->fw.img[IWL_UCODE_WOWLAN].is_dual_cpus =
904 					true;
905 			} else if ((num_of_cpus > 2) || (num_of_cpus < 1)) {
906 				IWL_ERR(drv, "Driver support upto 2 CPUs\n");
907 				return -EINVAL;
908 			}
909 			break;
910 		case IWL_UCODE_TLV_CSCHEME:
911 			if (iwl_store_cscheme(&drv->fw, tlv_data, tlv_len))
912 				goto invalid_tlv_len;
913 			break;
914 		case IWL_UCODE_TLV_N_SCAN_CHANNELS:
915 			if (tlv_len != sizeof(u32))
916 				goto invalid_tlv_len;
917 			capa->n_scan_channels =
918 				le32_to_cpup((__le32 *)tlv_data);
919 			break;
920 		case IWL_UCODE_TLV_FW_VERSION: {
921 			__le32 *ptr = (void *)tlv_data;
922 			u32 major, minor;
923 			u8 local_comp;
924 
925 			if (tlv_len != sizeof(u32) * 3)
926 				goto invalid_tlv_len;
927 
928 			major = le32_to_cpup(ptr++);
929 			minor = le32_to_cpup(ptr++);
930 			local_comp = le32_to_cpup(ptr);
931 
932 			if (major >= 35)
933 				snprintf(drv->fw.fw_version,
934 					 sizeof(drv->fw.fw_version),
935 					"%u.%08x.%u %s", major, minor,
936 					local_comp, iwl_reduced_fw_name(drv));
937 			else
938 				snprintf(drv->fw.fw_version,
939 					 sizeof(drv->fw.fw_version),
940 					"%u.%u.%u %s", major, minor,
941 					local_comp, iwl_reduced_fw_name(drv));
942 			break;
943 			}
944 		case IWL_UCODE_TLV_FW_DBG_DEST: {
945 			struct iwl_fw_dbg_dest_tlv *dest = NULL;
946 			struct iwl_fw_dbg_dest_tlv_v1 *dest_v1 = NULL;
947 			u8 mon_mode;
948 
949 			pieces->dbg_dest_ver = (u8 *)tlv_data;
950 			if (*pieces->dbg_dest_ver == 1) {
951 				dest = (void *)tlv_data;
952 			} else if (*pieces->dbg_dest_ver == 0) {
953 				dest_v1 = (void *)tlv_data;
954 			} else {
955 				IWL_ERR(drv,
956 					"The version is %d, and it is invalid\n",
957 					*pieces->dbg_dest_ver);
958 				break;
959 			}
960 
961 			if (pieces->dbg_dest_tlv_init) {
962 				IWL_ERR(drv,
963 					"dbg destination ignored, already exists\n");
964 				break;
965 			}
966 
967 			pieces->dbg_dest_tlv_init = true;
968 
969 			if (dest_v1) {
970 				pieces->dbg_dest_tlv_v1 = dest_v1;
971 				mon_mode = dest_v1->monitor_mode;
972 			} else {
973 				pieces->dbg_dest_tlv = dest;
974 				mon_mode = dest->monitor_mode;
975 			}
976 
977 			IWL_INFO(drv, "Found debug destination: %s\n",
978 				 get_fw_dbg_mode_string(mon_mode));
979 
980 			drv->fw.dbg.n_dest_reg = (dest_v1) ?
981 				tlv_len -
982 				offsetof(struct iwl_fw_dbg_dest_tlv_v1,
983 					 reg_ops) :
984 				tlv_len -
985 				offsetof(struct iwl_fw_dbg_dest_tlv,
986 					 reg_ops);
987 
988 			drv->fw.dbg.n_dest_reg /=
989 				sizeof(drv->fw.dbg.dest_tlv->reg_ops[0]);
990 
991 			break;
992 			}
993 		case IWL_UCODE_TLV_FW_DBG_CONF: {
994 			struct iwl_fw_dbg_conf_tlv *conf = (void *)tlv_data;
995 
996 			if (!pieces->dbg_dest_tlv_init) {
997 				IWL_ERR(drv,
998 					"Ignore dbg config %d - no destination configured\n",
999 					conf->id);
1000 				break;
1001 			}
1002 
1003 			if (conf->id >= ARRAY_SIZE(drv->fw.dbg.conf_tlv)) {
1004 				IWL_ERR(drv,
1005 					"Skip unknown configuration: %d\n",
1006 					conf->id);
1007 				break;
1008 			}
1009 
1010 			if (pieces->dbg_conf_tlv[conf->id]) {
1011 				IWL_ERR(drv,
1012 					"Ignore duplicate dbg config %d\n",
1013 					conf->id);
1014 				break;
1015 			}
1016 
1017 			if (conf->usniffer)
1018 				usniffer_req = true;
1019 
1020 			IWL_INFO(drv, "Found debug configuration: %d\n",
1021 				 conf->id);
1022 
1023 			pieces->dbg_conf_tlv[conf->id] = conf;
1024 			pieces->dbg_conf_tlv_len[conf->id] = tlv_len;
1025 			break;
1026 			}
1027 		case IWL_UCODE_TLV_FW_DBG_TRIGGER: {
1028 			struct iwl_fw_dbg_trigger_tlv *trigger =
1029 				(void *)tlv_data;
1030 			u32 trigger_id = le32_to_cpu(trigger->id);
1031 
1032 			if (trigger_id >= ARRAY_SIZE(drv->fw.dbg.trigger_tlv)) {
1033 				IWL_ERR(drv,
1034 					"Skip unknown trigger: %u\n",
1035 					trigger->id);
1036 				break;
1037 			}
1038 
1039 			if (pieces->dbg_trigger_tlv[trigger_id]) {
1040 				IWL_ERR(drv,
1041 					"Ignore duplicate dbg trigger %u\n",
1042 					trigger->id);
1043 				break;
1044 			}
1045 
1046 			IWL_INFO(drv, "Found debug trigger: %u\n", trigger->id);
1047 
1048 			pieces->dbg_trigger_tlv[trigger_id] = trigger;
1049 			pieces->dbg_trigger_tlv_len[trigger_id] = tlv_len;
1050 			break;
1051 			}
1052 		case IWL_UCODE_TLV_FW_DBG_DUMP_LST: {
1053 			if (tlv_len != sizeof(u32)) {
1054 				IWL_ERR(drv,
1055 					"dbg lst mask size incorrect, skip\n");
1056 				break;
1057 			}
1058 
1059 			drv->fw.dbg.dump_mask =
1060 				le32_to_cpup((__le32 *)tlv_data);
1061 			break;
1062 			}
1063 		case IWL_UCODE_TLV_SEC_RT_USNIFFER:
1064 			*usniffer_images = true;
1065 			iwl_store_ucode_sec(pieces, tlv_data,
1066 					    IWL_UCODE_REGULAR_USNIFFER,
1067 					    tlv_len);
1068 			break;
1069 		case IWL_UCODE_TLV_PAGING:
1070 			if (tlv_len != sizeof(u32))
1071 				goto invalid_tlv_len;
1072 			paging_mem_size = le32_to_cpup((__le32 *)tlv_data);
1073 
1074 			IWL_DEBUG_FW(drv,
1075 				     "Paging: paging enabled (size = %u bytes)\n",
1076 				     paging_mem_size);
1077 
1078 			if (paging_mem_size > MAX_PAGING_IMAGE_SIZE) {
1079 				IWL_ERR(drv,
1080 					"Paging: driver supports up to %lu bytes for paging image\n",
1081 					MAX_PAGING_IMAGE_SIZE);
1082 				return -EINVAL;
1083 			}
1084 
1085 			if (paging_mem_size & (FW_PAGING_SIZE - 1)) {
1086 				IWL_ERR(drv,
1087 					"Paging: image isn't multiple %lu\n",
1088 					FW_PAGING_SIZE);
1089 				return -EINVAL;
1090 			}
1091 
1092 			drv->fw.img[IWL_UCODE_REGULAR].paging_mem_size =
1093 				paging_mem_size;
1094 			usniffer_img = IWL_UCODE_REGULAR_USNIFFER;
1095 			drv->fw.img[usniffer_img].paging_mem_size =
1096 				paging_mem_size;
1097 			break;
1098 		case IWL_UCODE_TLV_FW_GSCAN_CAPA:
1099 			/* ignored */
1100 			break;
1101 		case IWL_UCODE_TLV_FW_MEM_SEG: {
1102 			struct iwl_fw_dbg_mem_seg_tlv *dbg_mem =
1103 				(void *)tlv_data;
1104 			size_t size;
1105 			struct iwl_fw_dbg_mem_seg_tlv *n;
1106 
1107 			if (tlv_len != (sizeof(*dbg_mem)))
1108 				goto invalid_tlv_len;
1109 
1110 			IWL_DEBUG_INFO(drv, "Found debug memory segment: %u\n",
1111 				       dbg_mem->data_type);
1112 
1113 			size = sizeof(*pieces->dbg_mem_tlv) *
1114 			       (pieces->n_mem_tlv + 1);
1115 			n = krealloc(pieces->dbg_mem_tlv, size, GFP_KERNEL);
1116 			if (!n)
1117 				return -ENOMEM;
1118 			pieces->dbg_mem_tlv = n;
1119 			pieces->dbg_mem_tlv[pieces->n_mem_tlv] = *dbg_mem;
1120 			pieces->n_mem_tlv++;
1121 			break;
1122 			}
1123 		case IWL_UCODE_TLV_IML: {
1124 			drv->fw.iml_len = tlv_len;
1125 			drv->fw.iml = kmemdup(tlv_data, tlv_len, GFP_KERNEL);
1126 			if (!drv->fw.iml)
1127 				return -ENOMEM;
1128 			break;
1129 			}
1130 		case IWL_UCODE_TLV_FW_RECOVERY_INFO: {
1131 			struct {
1132 				__le32 buf_addr;
1133 				__le32 buf_size;
1134 			} *recov_info = (void *)tlv_data;
1135 
1136 			if (tlv_len != sizeof(*recov_info))
1137 				goto invalid_tlv_len;
1138 			capa->error_log_addr =
1139 				le32_to_cpu(recov_info->buf_addr);
1140 			capa->error_log_size =
1141 				le32_to_cpu(recov_info->buf_size);
1142 			}
1143 			break;
1144 		case IWL_UCODE_TLV_FW_FSEQ_VERSION: {
1145 			struct {
1146 				u8 version[32];
1147 				u8 sha1[20];
1148 			} *fseq_ver = (void *)tlv_data;
1149 
1150 			if (tlv_len != sizeof(*fseq_ver))
1151 				goto invalid_tlv_len;
1152 			IWL_INFO(drv, "TLV_FW_FSEQ_VERSION: %s\n",
1153 				 fseq_ver->version);
1154 			}
1155 			break;
1156 		case IWL_UCODE_TLV_FW_NUM_STATIONS:
1157 			if (tlv_len != sizeof(u32))
1158 				goto invalid_tlv_len;
1159 			if (le32_to_cpup((__le32 *)tlv_data) >
1160 			    IWL_MVM_STATION_COUNT_MAX) {
1161 				IWL_ERR(drv,
1162 					"%d is an invalid number of station\n",
1163 					le32_to_cpup((__le32 *)tlv_data));
1164 				goto tlv_error;
1165 			}
1166 			capa->num_stations =
1167 				le32_to_cpup((__le32 *)tlv_data);
1168 			break;
1169 		case IWL_UCODE_TLV_UMAC_DEBUG_ADDRS: {
1170 			struct iwl_umac_debug_addrs *dbg_ptrs =
1171 				(void *)tlv_data;
1172 
1173 			if (tlv_len != sizeof(*dbg_ptrs))
1174 				goto invalid_tlv_len;
1175 			if (drv->trans->trans_cfg->device_family <
1176 			    IWL_DEVICE_FAMILY_22000)
1177 				break;
1178 			drv->trans->dbg.umac_error_event_table =
1179 				le32_to_cpu(dbg_ptrs->error_info_addr) &
1180 				~FW_ADDR_CACHE_CONTROL;
1181 			drv->trans->dbg.error_event_table_tlv_status |=
1182 				IWL_ERROR_EVENT_TABLE_UMAC;
1183 			break;
1184 			}
1185 		case IWL_UCODE_TLV_LMAC_DEBUG_ADDRS: {
1186 			struct iwl_lmac_debug_addrs *dbg_ptrs =
1187 				(void *)tlv_data;
1188 
1189 			if (tlv_len != sizeof(*dbg_ptrs))
1190 				goto invalid_tlv_len;
1191 			if (drv->trans->trans_cfg->device_family <
1192 			    IWL_DEVICE_FAMILY_22000)
1193 				break;
1194 			drv->trans->dbg.lmac_error_event_table[0] =
1195 				le32_to_cpu(dbg_ptrs->error_event_table_ptr) &
1196 				~FW_ADDR_CACHE_CONTROL;
1197 			drv->trans->dbg.error_event_table_tlv_status |=
1198 				IWL_ERROR_EVENT_TABLE_LMAC1;
1199 			break;
1200 			}
1201 		case IWL_UCODE_TLV_TYPE_REGIONS:
1202 			iwl_parse_dbg_tlv_assert_tables(drv, tlv);
1203 			fallthrough;
1204 		case IWL_UCODE_TLV_TYPE_DEBUG_INFO:
1205 		case IWL_UCODE_TLV_TYPE_BUFFER_ALLOCATION:
1206 		case IWL_UCODE_TLV_TYPE_HCMD:
1207 		case IWL_UCODE_TLV_TYPE_TRIGGERS:
1208 		case IWL_UCODE_TLV_TYPE_CONF_SET:
1209 			if (iwlwifi_mod_params.enable_ini)
1210 				iwl_dbg_tlv_alloc(drv->trans, tlv, false);
1211 			break;
1212 		case IWL_UCODE_TLV_CMD_VERSIONS:
1213 			if (tlv_len % sizeof(struct iwl_fw_cmd_version)) {
1214 				IWL_ERR(drv,
1215 					"Invalid length for command versions: %u\n",
1216 					tlv_len);
1217 				tlv_len /= sizeof(struct iwl_fw_cmd_version);
1218 				tlv_len *= sizeof(struct iwl_fw_cmd_version);
1219 			}
1220 			if (WARN_ON(capa->cmd_versions))
1221 				return -EINVAL;
1222 			capa->cmd_versions = kmemdup(tlv_data, tlv_len,
1223 						     GFP_KERNEL);
1224 			if (!capa->cmd_versions)
1225 				return -ENOMEM;
1226 			capa->n_cmd_versions =
1227 				tlv_len / sizeof(struct iwl_fw_cmd_version);
1228 			break;
1229 		case IWL_UCODE_TLV_PHY_INTEGRATION_VERSION:
1230 			if (drv->fw.phy_integration_ver) {
1231 				IWL_ERR(drv,
1232 					"phy integration str ignored, already exists\n");
1233 				break;
1234 			}
1235 
1236 			drv->fw.phy_integration_ver =
1237 				kmemdup(tlv_data, tlv_len, GFP_KERNEL);
1238 			if (!drv->fw.phy_integration_ver)
1239 				return -ENOMEM;
1240 			drv->fw.phy_integration_ver_len = tlv_len;
1241 			break;
1242 		case IWL_UCODE_TLV_SEC_TABLE_ADDR:
1243 		case IWL_UCODE_TLV_D3_KEK_KCK_ADDR:
1244 			iwl_drv_set_dump_exclude(drv, tlv_type,
1245 						 tlv_data, tlv_len);
1246 			break;
1247 		default:
1248 			IWL_DEBUG_INFO(drv, "unknown TLV: %d\n", tlv_type);
1249 			break;
1250 		}
1251 	}
1252 
1253 	if (!fw_has_capa(capa, IWL_UCODE_TLV_CAPA_USNIFFER_UNIFIED) &&
1254 	    usniffer_req && !*usniffer_images) {
1255 		IWL_ERR(drv,
1256 			"user selected to work with usniffer but usniffer image isn't available in ucode package\n");
1257 		return -EINVAL;
1258 	}
1259 
1260 	if (len) {
1261 		IWL_ERR(drv, "invalid TLV after parsing: %zd\n", len);
1262 		iwl_print_hex_dump(drv, IWL_DL_FW, (u8 *)data, len);
1263 		return -EINVAL;
1264 	}
1265 
1266 	return 0;
1267 
1268  invalid_tlv_len:
1269 	IWL_ERR(drv, "TLV %d has invalid size: %u\n", tlv_type, tlv_len);
1270  tlv_error:
1271 	iwl_print_hex_dump(drv, IWL_DL_FW, tlv_data, tlv_len);
1272 
1273 	return -EINVAL;
1274 }
1275 
1276 static int iwl_alloc_ucode(struct iwl_drv *drv,
1277 			   struct iwl_firmware_pieces *pieces,
1278 			   enum iwl_ucode_type type)
1279 {
1280 	int i;
1281 	struct fw_desc *sec;
1282 
1283 	sec = kcalloc(pieces->img[type].sec_counter, sizeof(*sec), GFP_KERNEL);
1284 	if (!sec)
1285 		return -ENOMEM;
1286 	drv->fw.img[type].sec = sec;
1287 	drv->fw.img[type].num_sec = pieces->img[type].sec_counter;
1288 
1289 	for (i = 0; i < pieces->img[type].sec_counter; i++)
1290 		if (iwl_alloc_fw_desc(drv, &sec[i], get_sec(pieces, type, i)))
1291 			return -ENOMEM;
1292 
1293 	return 0;
1294 }
1295 
1296 static int validate_sec_sizes(struct iwl_drv *drv,
1297 			      struct iwl_firmware_pieces *pieces,
1298 			      const struct iwl_cfg *cfg)
1299 {
1300 	IWL_DEBUG_INFO(drv, "f/w package hdr runtime inst size = %zd\n",
1301 		get_sec_size(pieces, IWL_UCODE_REGULAR,
1302 			     IWL_UCODE_SECTION_INST));
1303 	IWL_DEBUG_INFO(drv, "f/w package hdr runtime data size = %zd\n",
1304 		get_sec_size(pieces, IWL_UCODE_REGULAR,
1305 			     IWL_UCODE_SECTION_DATA));
1306 	IWL_DEBUG_INFO(drv, "f/w package hdr init inst size = %zd\n",
1307 		get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST));
1308 	IWL_DEBUG_INFO(drv, "f/w package hdr init data size = %zd\n",
1309 		get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA));
1310 
1311 	/* Verify that uCode images will fit in card's SRAM. */
1312 	if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) >
1313 	    cfg->max_inst_size) {
1314 		IWL_ERR(drv, "uCode instr len %zd too large to fit in\n",
1315 			get_sec_size(pieces, IWL_UCODE_REGULAR,
1316 				     IWL_UCODE_SECTION_INST));
1317 		return -1;
1318 	}
1319 
1320 	if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) >
1321 	    cfg->max_data_size) {
1322 		IWL_ERR(drv, "uCode data len %zd too large to fit in\n",
1323 			get_sec_size(pieces, IWL_UCODE_REGULAR,
1324 				     IWL_UCODE_SECTION_DATA));
1325 		return -1;
1326 	}
1327 
1328 	if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) >
1329 	     cfg->max_inst_size) {
1330 		IWL_ERR(drv, "uCode init instr len %zd too large to fit in\n",
1331 			get_sec_size(pieces, IWL_UCODE_INIT,
1332 				     IWL_UCODE_SECTION_INST));
1333 		return -1;
1334 	}
1335 
1336 	if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA) >
1337 	    cfg->max_data_size) {
1338 		IWL_ERR(drv, "uCode init data len %zd too large to fit in\n",
1339 			get_sec_size(pieces, IWL_UCODE_REGULAR,
1340 				     IWL_UCODE_SECTION_DATA));
1341 		return -1;
1342 	}
1343 	return 0;
1344 }
1345 
1346 static struct iwl_op_mode *
1347 _iwl_op_mode_start(struct iwl_drv *drv, struct iwlwifi_opmode_table *op)
1348 {
1349 	const struct iwl_op_mode_ops *ops = op->ops;
1350 	struct dentry *dbgfs_dir = NULL;
1351 	struct iwl_op_mode *op_mode = NULL;
1352 	int retry, max_retry = !!iwlwifi_mod_params.fw_restart * IWL_MAX_INIT_RETRY;
1353 
1354 	for (retry = 0; retry <= max_retry; retry++) {
1355 
1356 #ifdef CONFIG_IWLWIFI_DEBUGFS
1357 		drv->dbgfs_op_mode = debugfs_create_dir(op->name,
1358 							drv->dbgfs_drv);
1359 		dbgfs_dir = drv->dbgfs_op_mode;
1360 #endif
1361 
1362 		op_mode = ops->start(drv->trans, drv->trans->cfg,
1363 				     &drv->fw, dbgfs_dir);
1364 
1365 		if (op_mode)
1366 			return op_mode;
1367 
1368 		IWL_ERR(drv, "retry init count %d\n", retry);
1369 
1370 #ifdef CONFIG_IWLWIFI_DEBUGFS
1371 		debugfs_remove_recursive(drv->dbgfs_op_mode);
1372 		drv->dbgfs_op_mode = NULL;
1373 #endif
1374 	}
1375 
1376 	return NULL;
1377 }
1378 
1379 static void _iwl_op_mode_stop(struct iwl_drv *drv)
1380 {
1381 	/* op_mode can be NULL if its start failed */
1382 	if (drv->op_mode) {
1383 		iwl_op_mode_stop(drv->op_mode);
1384 		drv->op_mode = NULL;
1385 
1386 #ifdef CONFIG_IWLWIFI_DEBUGFS
1387 		debugfs_remove_recursive(drv->dbgfs_op_mode);
1388 		drv->dbgfs_op_mode = NULL;
1389 #endif
1390 	}
1391 }
1392 
1393 /*
1394  * iwl_req_fw_callback - callback when firmware was loaded
1395  *
1396  * If loaded successfully, copies the firmware into buffers
1397  * for the card to fetch (via DMA).
1398  */
1399 static void iwl_req_fw_callback(const struct firmware *ucode_raw, void *context)
1400 {
1401 	struct iwl_drv *drv = context;
1402 	struct iwl_fw *fw = &drv->fw;
1403 	struct iwl_ucode_header *ucode;
1404 	struct iwlwifi_opmode_table *op;
1405 	int err;
1406 	struct iwl_firmware_pieces *pieces;
1407 	const unsigned int api_max = drv->trans->cfg->ucode_api_max;
1408 	const unsigned int api_min = drv->trans->cfg->ucode_api_min;
1409 	size_t trigger_tlv_sz[FW_DBG_TRIGGER_MAX];
1410 	u32 api_ver;
1411 	int i;
1412 	bool load_module = false;
1413 	bool usniffer_images = false;
1414 
1415 	fw->ucode_capa.max_probe_length = IWL_DEFAULT_MAX_PROBE_LENGTH;
1416 	fw->ucode_capa.standard_phy_calibration_size =
1417 			IWL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE;
1418 	fw->ucode_capa.n_scan_channels = IWL_DEFAULT_SCAN_CHANNELS;
1419 	fw->ucode_capa.num_stations = IWL_MVM_STATION_COUNT_MAX;
1420 	/* dump all fw memory areas by default */
1421 	fw->dbg.dump_mask = 0xffffffff;
1422 
1423 	pieces = kzalloc(sizeof(*pieces), GFP_KERNEL);
1424 	if (!pieces)
1425 		goto out_free_fw;
1426 
1427 	if (!ucode_raw)
1428 		goto try_again;
1429 
1430 	IWL_DEBUG_FW_INFO(drv, "Loaded firmware file '%s' (%zd bytes).\n",
1431 			  drv->firmware_name, ucode_raw->size);
1432 
1433 	/* Make sure that we got at least the API version number */
1434 	if (ucode_raw->size < 4) {
1435 		IWL_ERR(drv, "File size way too small!\n");
1436 		goto try_again;
1437 	}
1438 
1439 	/* Data from ucode file:  header followed by uCode images */
1440 	ucode = (struct iwl_ucode_header *)ucode_raw->data;
1441 
1442 	if (ucode->ver)
1443 		err = iwl_parse_v1_v2_firmware(drv, ucode_raw, pieces);
1444 	else
1445 		err = iwl_parse_tlv_firmware(drv, ucode_raw, pieces,
1446 					     &fw->ucode_capa, &usniffer_images);
1447 
1448 	if (err)
1449 		goto try_again;
1450 
1451 	if (fw_has_api(&drv->fw.ucode_capa, IWL_UCODE_TLV_API_NEW_VERSION))
1452 		api_ver = drv->fw.ucode_ver;
1453 	else
1454 		api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
1455 
1456 	/*
1457 	 * api_ver should match the api version forming part of the
1458 	 * firmware filename ... but we don't check for that and only rely
1459 	 * on the API version read from firmware header from here on forward
1460 	 */
1461 	if (api_ver < api_min || api_ver > api_max) {
1462 		IWL_ERR(drv,
1463 			"Driver unable to support your firmware API. "
1464 			"Driver supports v%u, firmware is v%u.\n",
1465 			api_max, api_ver);
1466 		goto try_again;
1467 	}
1468 
1469 	/*
1470 	 * In mvm uCode there is no difference between data and instructions
1471 	 * sections.
1472 	 */
1473 	if (fw->type == IWL_FW_DVM && validate_sec_sizes(drv, pieces,
1474 							 drv->trans->cfg))
1475 		goto try_again;
1476 
1477 	/* Allocate ucode buffers for card's bus-master loading ... */
1478 
1479 	/* Runtime instructions and 2 copies of data:
1480 	 * 1) unmodified from disk
1481 	 * 2) backup cache for save/restore during power-downs
1482 	 */
1483 	for (i = 0; i < IWL_UCODE_TYPE_MAX; i++)
1484 		if (iwl_alloc_ucode(drv, pieces, i))
1485 			goto out_free_fw;
1486 
1487 	if (pieces->dbg_dest_tlv_init) {
1488 		size_t dbg_dest_size = sizeof(*drv->fw.dbg.dest_tlv) +
1489 			sizeof(drv->fw.dbg.dest_tlv->reg_ops[0]) *
1490 			drv->fw.dbg.n_dest_reg;
1491 
1492 		drv->fw.dbg.dest_tlv = kmalloc(dbg_dest_size, GFP_KERNEL);
1493 
1494 		if (!drv->fw.dbg.dest_tlv)
1495 			goto out_free_fw;
1496 
1497 		if (*pieces->dbg_dest_ver == 0) {
1498 			memcpy(drv->fw.dbg.dest_tlv, pieces->dbg_dest_tlv_v1,
1499 			       dbg_dest_size);
1500 		} else {
1501 			struct iwl_fw_dbg_dest_tlv_v1 *dest_tlv =
1502 				drv->fw.dbg.dest_tlv;
1503 
1504 			dest_tlv->version = pieces->dbg_dest_tlv->version;
1505 			dest_tlv->monitor_mode =
1506 				pieces->dbg_dest_tlv->monitor_mode;
1507 			dest_tlv->size_power =
1508 				pieces->dbg_dest_tlv->size_power;
1509 			dest_tlv->wrap_count =
1510 				pieces->dbg_dest_tlv->wrap_count;
1511 			dest_tlv->write_ptr_reg =
1512 				pieces->dbg_dest_tlv->write_ptr_reg;
1513 			dest_tlv->base_shift =
1514 				pieces->dbg_dest_tlv->base_shift;
1515 			memcpy(dest_tlv->reg_ops,
1516 			       pieces->dbg_dest_tlv->reg_ops,
1517 			       sizeof(drv->fw.dbg.dest_tlv->reg_ops[0]) *
1518 			       drv->fw.dbg.n_dest_reg);
1519 
1520 			/* In version 1 of the destination tlv, which is
1521 			 * relevant for internal buffer exclusively,
1522 			 * the base address is part of given with the length
1523 			 * of the buffer, and the size shift is give instead of
1524 			 * end shift. We now store these values in base_reg,
1525 			 * and end shift, and when dumping the data we'll
1526 			 * manipulate it for extracting both the length and
1527 			 * base address */
1528 			dest_tlv->base_reg = pieces->dbg_dest_tlv->cfg_reg;
1529 			dest_tlv->end_shift =
1530 				pieces->dbg_dest_tlv->size_shift;
1531 		}
1532 	}
1533 
1534 	for (i = 0; i < ARRAY_SIZE(drv->fw.dbg.conf_tlv); i++) {
1535 		if (pieces->dbg_conf_tlv[i]) {
1536 			drv->fw.dbg.conf_tlv[i] =
1537 				kmemdup(pieces->dbg_conf_tlv[i],
1538 					pieces->dbg_conf_tlv_len[i],
1539 					GFP_KERNEL);
1540 			if (!drv->fw.dbg.conf_tlv[i])
1541 				goto out_free_fw;
1542 		}
1543 	}
1544 
1545 	memset(&trigger_tlv_sz, 0xff, sizeof(trigger_tlv_sz));
1546 
1547 	trigger_tlv_sz[FW_DBG_TRIGGER_MISSED_BEACONS] =
1548 		sizeof(struct iwl_fw_dbg_trigger_missed_bcon);
1549 	trigger_tlv_sz[FW_DBG_TRIGGER_CHANNEL_SWITCH] = 0;
1550 	trigger_tlv_sz[FW_DBG_TRIGGER_FW_NOTIF] =
1551 		sizeof(struct iwl_fw_dbg_trigger_cmd);
1552 	trigger_tlv_sz[FW_DBG_TRIGGER_MLME] =
1553 		sizeof(struct iwl_fw_dbg_trigger_mlme);
1554 	trigger_tlv_sz[FW_DBG_TRIGGER_STATS] =
1555 		sizeof(struct iwl_fw_dbg_trigger_stats);
1556 	trigger_tlv_sz[FW_DBG_TRIGGER_RSSI] =
1557 		sizeof(struct iwl_fw_dbg_trigger_low_rssi);
1558 	trigger_tlv_sz[FW_DBG_TRIGGER_TXQ_TIMERS] =
1559 		sizeof(struct iwl_fw_dbg_trigger_txq_timer);
1560 	trigger_tlv_sz[FW_DBG_TRIGGER_TIME_EVENT] =
1561 		sizeof(struct iwl_fw_dbg_trigger_time_event);
1562 	trigger_tlv_sz[FW_DBG_TRIGGER_BA] =
1563 		sizeof(struct iwl_fw_dbg_trigger_ba);
1564 	trigger_tlv_sz[FW_DBG_TRIGGER_TDLS] =
1565 		sizeof(struct iwl_fw_dbg_trigger_tdls);
1566 
1567 	for (i = 0; i < ARRAY_SIZE(drv->fw.dbg.trigger_tlv); i++) {
1568 		if (pieces->dbg_trigger_tlv[i]) {
1569 			/*
1570 			 * If the trigger isn't long enough, WARN and exit.
1571 			 * Someone is trying to debug something and he won't
1572 			 * be able to catch the bug he is trying to chase.
1573 			 * We'd better be noisy to be sure he knows what's
1574 			 * going on.
1575 			 */
1576 			if (WARN_ON(pieces->dbg_trigger_tlv_len[i] <
1577 				    (trigger_tlv_sz[i] +
1578 				     sizeof(struct iwl_fw_dbg_trigger_tlv))))
1579 				goto out_free_fw;
1580 			drv->fw.dbg.trigger_tlv_len[i] =
1581 				pieces->dbg_trigger_tlv_len[i];
1582 			drv->fw.dbg.trigger_tlv[i] =
1583 				kmemdup(pieces->dbg_trigger_tlv[i],
1584 					drv->fw.dbg.trigger_tlv_len[i],
1585 					GFP_KERNEL);
1586 			if (!drv->fw.dbg.trigger_tlv[i])
1587 				goto out_free_fw;
1588 		}
1589 	}
1590 
1591 	/* Now that we can no longer fail, copy information */
1592 
1593 	drv->fw.dbg.mem_tlv = pieces->dbg_mem_tlv;
1594 	pieces->dbg_mem_tlv = NULL;
1595 	drv->fw.dbg.n_mem_tlv = pieces->n_mem_tlv;
1596 
1597 	/*
1598 	 * The (size - 16) / 12 formula is based on the information recorded
1599 	 * for each event, which is of mode 1 (including timestamp) for all
1600 	 * new microcodes that include this information.
1601 	 */
1602 	fw->init_evtlog_ptr = pieces->init_evtlog_ptr;
1603 	if (pieces->init_evtlog_size)
1604 		fw->init_evtlog_size = (pieces->init_evtlog_size - 16)/12;
1605 	else
1606 		fw->init_evtlog_size =
1607 			drv->trans->trans_cfg->base_params->max_event_log_size;
1608 	fw->init_errlog_ptr = pieces->init_errlog_ptr;
1609 	fw->inst_evtlog_ptr = pieces->inst_evtlog_ptr;
1610 	if (pieces->inst_evtlog_size)
1611 		fw->inst_evtlog_size = (pieces->inst_evtlog_size - 16)/12;
1612 	else
1613 		fw->inst_evtlog_size =
1614 			drv->trans->trans_cfg->base_params->max_event_log_size;
1615 	fw->inst_errlog_ptr = pieces->inst_errlog_ptr;
1616 
1617 	/*
1618 	 * figure out the offset of chain noise reset and gain commands
1619 	 * base on the size of standard phy calibration commands table size
1620 	 */
1621 	if (fw->ucode_capa.standard_phy_calibration_size >
1622 	    IWL_MAX_PHY_CALIBRATE_TBL_SIZE)
1623 		fw->ucode_capa.standard_phy_calibration_size =
1624 			IWL_MAX_STANDARD_PHY_CALIBRATE_TBL_SIZE;
1625 
1626 	/* We have our copies now, allow OS release its copies */
1627 	release_firmware(ucode_raw);
1628 
1629 	mutex_lock(&iwlwifi_opmode_table_mtx);
1630 	switch (fw->type) {
1631 	case IWL_FW_DVM:
1632 		op = &iwlwifi_opmode_table[DVM_OP_MODE];
1633 		break;
1634 	default:
1635 		WARN(1, "Invalid fw type %d\n", fw->type);
1636 		fallthrough;
1637 	case IWL_FW_MVM:
1638 		op = &iwlwifi_opmode_table[MVM_OP_MODE];
1639 		break;
1640 	}
1641 
1642 	IWL_INFO(drv, "loaded firmware version %s op_mode %s\n",
1643 		 drv->fw.fw_version, op->name);
1644 
1645 	iwl_dbg_tlv_load_bin(drv->trans->dev, drv->trans);
1646 
1647 	/* add this device to the list of devices using this op_mode */
1648 	list_add_tail(&drv->list, &op->drv);
1649 
1650 	if (op->ops) {
1651 		drv->op_mode = _iwl_op_mode_start(drv, op);
1652 
1653 		if (!drv->op_mode) {
1654 			mutex_unlock(&iwlwifi_opmode_table_mtx);
1655 			goto out_unbind;
1656 		}
1657 	} else {
1658 		load_module = true;
1659 	}
1660 	mutex_unlock(&iwlwifi_opmode_table_mtx);
1661 
1662 	/*
1663 	 * Complete the firmware request last so that
1664 	 * a driver unbind (stop) doesn't run while we
1665 	 * are doing the start() above.
1666 	 */
1667 	complete(&drv->request_firmware_complete);
1668 
1669 	/*
1670 	 * Load the module last so we don't block anything
1671 	 * else from proceeding if the module fails to load
1672 	 * or hangs loading.
1673 	 */
1674 	if (load_module) {
1675 		request_module("%s", op->name);
1676 #ifdef CONFIG_IWLWIFI_OPMODE_MODULAR
1677 		if (err)
1678 			IWL_ERR(drv,
1679 				"failed to load module %s (error %d), is dynamic loading enabled?\n",
1680 				op->name, err);
1681 #endif
1682 	}
1683 	goto free;
1684 
1685  try_again:
1686 	/* try next, if any */
1687 	release_firmware(ucode_raw);
1688 	if (iwl_request_firmware(drv, false))
1689 		goto out_unbind;
1690 	goto free;
1691 
1692  out_free_fw:
1693 	release_firmware(ucode_raw);
1694  out_unbind:
1695 	complete(&drv->request_firmware_complete);
1696 	device_release_driver(drv->trans->dev);
1697  free:
1698 	if (pieces) {
1699 		for (i = 0; i < ARRAY_SIZE(pieces->img); i++)
1700 			kfree(pieces->img[i].sec);
1701 		kfree(pieces->dbg_mem_tlv);
1702 		kfree(pieces);
1703 	}
1704 }
1705 
1706 struct iwl_drv *iwl_drv_start(struct iwl_trans *trans)
1707 {
1708 	struct iwl_drv *drv;
1709 	int ret;
1710 
1711 	drv = kzalloc(sizeof(*drv), GFP_KERNEL);
1712 	if (!drv) {
1713 		ret = -ENOMEM;
1714 		goto err;
1715 	}
1716 
1717 	drv->trans = trans;
1718 	drv->dev = trans->dev;
1719 
1720 	init_completion(&drv->request_firmware_complete);
1721 	INIT_LIST_HEAD(&drv->list);
1722 
1723 #ifdef CONFIG_IWLWIFI_DEBUGFS
1724 	/* Create the device debugfs entries. */
1725 	drv->dbgfs_drv = debugfs_create_dir(dev_name(trans->dev),
1726 					    iwl_dbgfs_root);
1727 
1728 	/* Create transport layer debugfs dir */
1729 	drv->trans->dbgfs_dir = debugfs_create_dir("trans", drv->dbgfs_drv);
1730 #endif
1731 
1732 	drv->trans->dbg.domains_bitmap = IWL_TRANS_FW_DBG_DOMAIN(drv->trans);
1733 
1734 	ret = iwl_request_firmware(drv, true);
1735 	if (ret) {
1736 		IWL_ERR(trans, "Couldn't request the fw\n");
1737 		goto err_fw;
1738 	}
1739 
1740 	return drv;
1741 
1742 err_fw:
1743 #ifdef CONFIG_IWLWIFI_DEBUGFS
1744 	debugfs_remove_recursive(drv->dbgfs_drv);
1745 	iwl_dbg_tlv_free(drv->trans);
1746 #endif
1747 	kfree(drv);
1748 err:
1749 	return ERR_PTR(ret);
1750 }
1751 
1752 void iwl_drv_stop(struct iwl_drv *drv)
1753 {
1754 	wait_for_completion(&drv->request_firmware_complete);
1755 
1756 	_iwl_op_mode_stop(drv);
1757 
1758 	iwl_dealloc_ucode(drv);
1759 
1760 	mutex_lock(&iwlwifi_opmode_table_mtx);
1761 	/*
1762 	 * List is empty (this item wasn't added)
1763 	 * when firmware loading failed -- in that
1764 	 * case we can't remove it from any list.
1765 	 */
1766 	if (!list_empty(&drv->list))
1767 		list_del(&drv->list);
1768 	mutex_unlock(&iwlwifi_opmode_table_mtx);
1769 
1770 #ifdef CONFIG_IWLWIFI_DEBUGFS
1771 	drv->trans->ops->debugfs_cleanup(drv->trans);
1772 
1773 	debugfs_remove_recursive(drv->dbgfs_drv);
1774 #endif
1775 
1776 	iwl_dbg_tlv_free(drv->trans);
1777 
1778 	kfree(drv);
1779 }
1780 
1781 
1782 /* shared module parameters */
1783 struct iwl_mod_params iwlwifi_mod_params = {
1784 	.fw_restart = true,
1785 	.bt_coex_active = true,
1786 	.power_level = IWL_POWER_INDEX_1,
1787 	.uapsd_disable = IWL_DISABLE_UAPSD_BSS | IWL_DISABLE_UAPSD_P2P_CLIENT,
1788 	.enable_ini = true,
1789 	/* the rest are 0 by default */
1790 };
1791 IWL_EXPORT_SYMBOL(iwlwifi_mod_params);
1792 
1793 int iwl_opmode_register(const char *name, const struct iwl_op_mode_ops *ops)
1794 {
1795 	int i;
1796 	struct iwl_drv *drv;
1797 	struct iwlwifi_opmode_table *op;
1798 
1799 	mutex_lock(&iwlwifi_opmode_table_mtx);
1800 	for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++) {
1801 		op = &iwlwifi_opmode_table[i];
1802 		if (strcmp(op->name, name))
1803 			continue;
1804 		op->ops = ops;
1805 		/* TODO: need to handle exceptional case */
1806 		list_for_each_entry(drv, &op->drv, list)
1807 			drv->op_mode = _iwl_op_mode_start(drv, op);
1808 
1809 		mutex_unlock(&iwlwifi_opmode_table_mtx);
1810 		return 0;
1811 	}
1812 	mutex_unlock(&iwlwifi_opmode_table_mtx);
1813 	return -EIO;
1814 }
1815 IWL_EXPORT_SYMBOL(iwl_opmode_register);
1816 
1817 void iwl_opmode_deregister(const char *name)
1818 {
1819 	int i;
1820 	struct iwl_drv *drv;
1821 
1822 	mutex_lock(&iwlwifi_opmode_table_mtx);
1823 	for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++) {
1824 		if (strcmp(iwlwifi_opmode_table[i].name, name))
1825 			continue;
1826 		iwlwifi_opmode_table[i].ops = NULL;
1827 
1828 		/* call the stop routine for all devices */
1829 		list_for_each_entry(drv, &iwlwifi_opmode_table[i].drv, list)
1830 			_iwl_op_mode_stop(drv);
1831 
1832 		mutex_unlock(&iwlwifi_opmode_table_mtx);
1833 		return;
1834 	}
1835 	mutex_unlock(&iwlwifi_opmode_table_mtx);
1836 }
1837 IWL_EXPORT_SYMBOL(iwl_opmode_deregister);
1838 
1839 static int __init iwl_drv_init(void)
1840 {
1841 	int i, err;
1842 
1843 	for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++)
1844 		INIT_LIST_HEAD(&iwlwifi_opmode_table[i].drv);
1845 
1846 	pr_info(DRV_DESCRIPTION "\n");
1847 
1848 #ifdef CONFIG_IWLWIFI_DEBUGFS
1849 	/* Create the root of iwlwifi debugfs subsystem. */
1850 	iwl_dbgfs_root = debugfs_create_dir(DRV_NAME, NULL);
1851 #endif
1852 
1853 	err = iwl_pci_register_driver();
1854 	if (err)
1855 		goto cleanup_debugfs;
1856 
1857 	return 0;
1858 
1859 cleanup_debugfs:
1860 #ifdef CONFIG_IWLWIFI_DEBUGFS
1861 	debugfs_remove_recursive(iwl_dbgfs_root);
1862 #endif
1863 	return err;
1864 }
1865 module_init(iwl_drv_init);
1866 
1867 static void __exit iwl_drv_exit(void)
1868 {
1869 	iwl_pci_unregister_driver();
1870 
1871 #ifdef CONFIG_IWLWIFI_DEBUGFS
1872 	debugfs_remove_recursive(iwl_dbgfs_root);
1873 #endif
1874 }
1875 module_exit(iwl_drv_exit);
1876 
1877 #ifdef CONFIG_IWLWIFI_DEBUG
1878 module_param_named(debug, iwlwifi_mod_params.debug_level, uint, 0644);
1879 MODULE_PARM_DESC(debug, "debug output mask");
1880 #endif
1881 
1882 module_param_named(swcrypto, iwlwifi_mod_params.swcrypto, int, 0444);
1883 MODULE_PARM_DESC(swcrypto, "using crypto in software (default 0 [hardware])");
1884 module_param_named(11n_disable, iwlwifi_mod_params.disable_11n, uint, 0444);
1885 MODULE_PARM_DESC(11n_disable,
1886 	"disable 11n functionality, bitmap: 1: full, 2: disable agg TX, 4: disable agg RX, 8 enable agg TX");
1887 module_param_named(amsdu_size, iwlwifi_mod_params.amsdu_size, int, 0444);
1888 MODULE_PARM_DESC(amsdu_size,
1889 		 "amsdu size 0: 12K for multi Rx queue devices, 2K for AX210 devices, "
1890 		 "4K for other devices 1:4K 2:8K 3:12K (16K buffers) 4: 2K (default 0)");
1891 module_param_named(fw_restart, iwlwifi_mod_params.fw_restart, bool, 0444);
1892 MODULE_PARM_DESC(fw_restart, "restart firmware in case of error (default true)");
1893 
1894 module_param_named(nvm_file, iwlwifi_mod_params.nvm_file, charp, 0444);
1895 MODULE_PARM_DESC(nvm_file, "NVM file name");
1896 
1897 module_param_named(uapsd_disable, iwlwifi_mod_params.uapsd_disable, uint, 0644);
1898 MODULE_PARM_DESC(uapsd_disable,
1899 		 "disable U-APSD functionality bitmap 1: BSS 2: P2P Client (default: 3)");
1900 module_param_named(enable_ini, iwlwifi_mod_params.enable_ini,
1901 		   bool, S_IRUGO | S_IWUSR);
1902 MODULE_PARM_DESC(enable_ini,
1903 		 "Enable debug INI TLV FW debug infrastructure (default: true");
1904 
1905 /*
1906  * set bt_coex_active to true, uCode will do kill/defer
1907  * every time the priority line is asserted (BT is sending signals on the
1908  * priority line in the PCIx).
1909  * set bt_coex_active to false, uCode will ignore the BT activity and
1910  * perform the normal operation
1911  *
1912  * User might experience transmit issue on some platform due to WiFi/BT
1913  * co-exist problem. The possible behaviors are:
1914  *   Able to scan and finding all the available AP
1915  *   Not able to associate with any AP
1916  * On those platforms, WiFi communication can be restored by set
1917  * "bt_coex_active" module parameter to "false"
1918  *
1919  * default: bt_coex_active = true (BT_COEX_ENABLE)
1920  */
1921 module_param_named(bt_coex_active, iwlwifi_mod_params.bt_coex_active,
1922 		   bool, 0444);
1923 MODULE_PARM_DESC(bt_coex_active, "enable wifi/bt co-exist (default: enable)");
1924 
1925 module_param_named(led_mode, iwlwifi_mod_params.led_mode, int, 0444);
1926 MODULE_PARM_DESC(led_mode, "0=system default, "
1927 		"1=On(RF On)/Off(RF Off), 2=blinking, 3=Off (default: 0)");
1928 
1929 module_param_named(power_save, iwlwifi_mod_params.power_save, bool, 0444);
1930 MODULE_PARM_DESC(power_save,
1931 		 "enable WiFi power management (default: disable)");
1932 
1933 module_param_named(power_level, iwlwifi_mod_params.power_level, int, 0444);
1934 MODULE_PARM_DESC(power_level,
1935 		 "default power save level (range from 1 - 5, default: 1)");
1936 
1937 module_param_named(disable_11ac, iwlwifi_mod_params.disable_11ac, bool, 0444);
1938 MODULE_PARM_DESC(disable_11ac, "Disable VHT capabilities (default: false)");
1939 
1940 module_param_named(remove_when_gone,
1941 		   iwlwifi_mod_params.remove_when_gone, bool,
1942 		   0444);
1943 MODULE_PARM_DESC(remove_when_gone,
1944 		 "Remove dev from PCIe bus if it is deemed inaccessible (default: false)");
1945 
1946 module_param_named(disable_11ax, iwlwifi_mod_params.disable_11ax, bool,
1947 		   S_IRUGO);
1948 MODULE_PARM_DESC(disable_11ax, "Disable HE capabilities (default: false)");
1949