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