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