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_UMAC_DEBUG_ADDRS: {
1159 			const struct iwl_umac_debug_addrs *dbg_ptrs =
1160 				(const void *)tlv_data;
1161 
1162 			if (tlv_len != sizeof(*dbg_ptrs))
1163 				goto invalid_tlv_len;
1164 			if (drv->trans->trans_cfg->device_family <
1165 			    IWL_DEVICE_FAMILY_22000)
1166 				break;
1167 			drv->trans->dbg.umac_error_event_table =
1168 				le32_to_cpu(dbg_ptrs->error_info_addr) &
1169 				~FW_ADDR_CACHE_CONTROL;
1170 			drv->trans->dbg.error_event_table_tlv_status |=
1171 				IWL_ERROR_EVENT_TABLE_UMAC;
1172 			break;
1173 			}
1174 		case IWL_UCODE_TLV_LMAC_DEBUG_ADDRS: {
1175 			const struct iwl_lmac_debug_addrs *dbg_ptrs =
1176 				(const void *)tlv_data;
1177 
1178 			if (tlv_len != sizeof(*dbg_ptrs))
1179 				goto invalid_tlv_len;
1180 			if (drv->trans->trans_cfg->device_family <
1181 			    IWL_DEVICE_FAMILY_22000)
1182 				break;
1183 			drv->trans->dbg.lmac_error_event_table[0] =
1184 				le32_to_cpu(dbg_ptrs->error_event_table_ptr) &
1185 				~FW_ADDR_CACHE_CONTROL;
1186 			drv->trans->dbg.error_event_table_tlv_status |=
1187 				IWL_ERROR_EVENT_TABLE_LMAC1;
1188 			break;
1189 			}
1190 		case IWL_UCODE_TLV_TYPE_REGIONS:
1191 			iwl_parse_dbg_tlv_assert_tables(drv, tlv);
1192 			fallthrough;
1193 		case IWL_UCODE_TLV_TYPE_DEBUG_INFO:
1194 		case IWL_UCODE_TLV_TYPE_BUFFER_ALLOCATION:
1195 		case IWL_UCODE_TLV_TYPE_HCMD:
1196 		case IWL_UCODE_TLV_TYPE_TRIGGERS:
1197 		case IWL_UCODE_TLV_TYPE_CONF_SET:
1198 			if (iwlwifi_mod_params.enable_ini)
1199 				iwl_dbg_tlv_alloc(drv->trans, tlv, false);
1200 			break;
1201 		case IWL_UCODE_TLV_CMD_VERSIONS:
1202 			if (tlv_len % sizeof(struct iwl_fw_cmd_version)) {
1203 				IWL_ERR(drv,
1204 					"Invalid length for command versions: %u\n",
1205 					tlv_len);
1206 				tlv_len /= sizeof(struct iwl_fw_cmd_version);
1207 				tlv_len *= sizeof(struct iwl_fw_cmd_version);
1208 			}
1209 			if (WARN_ON(capa->cmd_versions))
1210 				return -EINVAL;
1211 			capa->cmd_versions = kmemdup(tlv_data, tlv_len,
1212 						     GFP_KERNEL);
1213 			if (!capa->cmd_versions)
1214 				return -ENOMEM;
1215 			capa->n_cmd_versions =
1216 				tlv_len / sizeof(struct iwl_fw_cmd_version);
1217 			break;
1218 		case IWL_UCODE_TLV_PHY_INTEGRATION_VERSION:
1219 			if (drv->fw.phy_integration_ver) {
1220 				IWL_ERR(drv,
1221 					"phy integration str ignored, already exists\n");
1222 				break;
1223 			}
1224 
1225 			drv->fw.phy_integration_ver =
1226 				kmemdup(tlv_data, tlv_len, GFP_KERNEL);
1227 			if (!drv->fw.phy_integration_ver)
1228 				return -ENOMEM;
1229 			drv->fw.phy_integration_ver_len = tlv_len;
1230 			break;
1231 		case IWL_UCODE_TLV_SEC_TABLE_ADDR:
1232 		case IWL_UCODE_TLV_D3_KEK_KCK_ADDR:
1233 			iwl_drv_set_dump_exclude(drv, tlv_type,
1234 						 tlv_data, tlv_len);
1235 			break;
1236 		case IWL_UCODE_TLV_CURRENT_PC:
1237 			if (tlv_len < sizeof(struct iwl_pc_data))
1238 				goto invalid_tlv_len;
1239 			drv->trans->dbg.num_pc =
1240 				tlv_len / sizeof(struct iwl_pc_data);
1241 			drv->trans->dbg.pc_data =
1242 				kmemdup(tlv_data, tlv_len, GFP_KERNEL);
1243 			break;
1244 		default:
1245 			IWL_DEBUG_INFO(drv, "unknown TLV: %d\n", tlv_type);
1246 			break;
1247 		}
1248 	}
1249 
1250 	if (!fw_has_capa(capa, IWL_UCODE_TLV_CAPA_USNIFFER_UNIFIED) &&
1251 	    usniffer_req && !*usniffer_images) {
1252 		IWL_ERR(drv,
1253 			"user selected to work with usniffer but usniffer image isn't available in ucode package\n");
1254 		return -EINVAL;
1255 	}
1256 
1257 	if (len) {
1258 		IWL_ERR(drv, "invalid TLV after parsing: %zd\n", len);
1259 		iwl_print_hex_dump(drv, IWL_DL_FW, data, len);
1260 		return -EINVAL;
1261 	}
1262 
1263 	return 0;
1264 
1265  invalid_tlv_len:
1266 	IWL_ERR(drv, "TLV %d has invalid size: %u\n", tlv_type, tlv_len);
1267  tlv_error:
1268 	iwl_print_hex_dump(drv, IWL_DL_FW, tlv_data, tlv_len);
1269 
1270 	return -EINVAL;
1271 }
1272 
1273 static int iwl_alloc_ucode(struct iwl_drv *drv,
1274 			   struct iwl_firmware_pieces *pieces,
1275 			   enum iwl_ucode_type type)
1276 {
1277 	int i;
1278 	struct fw_desc *sec;
1279 
1280 	sec = kcalloc(pieces->img[type].sec_counter, sizeof(*sec), GFP_KERNEL);
1281 	if (!sec)
1282 		return -ENOMEM;
1283 	drv->fw.img[type].sec = sec;
1284 	drv->fw.img[type].num_sec = pieces->img[type].sec_counter;
1285 
1286 	for (i = 0; i < pieces->img[type].sec_counter; i++)
1287 		if (iwl_alloc_fw_desc(drv, &sec[i], get_sec(pieces, type, i)))
1288 			return -ENOMEM;
1289 
1290 	return 0;
1291 }
1292 
1293 static int validate_sec_sizes(struct iwl_drv *drv,
1294 			      struct iwl_firmware_pieces *pieces,
1295 			      const struct iwl_cfg *cfg)
1296 {
1297 	IWL_DEBUG_INFO(drv, "f/w package hdr runtime inst size = %zd\n",
1298 		get_sec_size(pieces, IWL_UCODE_REGULAR,
1299 			     IWL_UCODE_SECTION_INST));
1300 	IWL_DEBUG_INFO(drv, "f/w package hdr runtime data size = %zd\n",
1301 		get_sec_size(pieces, IWL_UCODE_REGULAR,
1302 			     IWL_UCODE_SECTION_DATA));
1303 	IWL_DEBUG_INFO(drv, "f/w package hdr init inst size = %zd\n",
1304 		get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST));
1305 	IWL_DEBUG_INFO(drv, "f/w package hdr init data size = %zd\n",
1306 		get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA));
1307 
1308 	/* Verify that uCode images will fit in card's SRAM. */
1309 	if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) >
1310 	    cfg->max_inst_size) {
1311 		IWL_ERR(drv, "uCode instr len %zd too large to fit in\n",
1312 			get_sec_size(pieces, IWL_UCODE_REGULAR,
1313 				     IWL_UCODE_SECTION_INST));
1314 		return -1;
1315 	}
1316 
1317 	if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) >
1318 	    cfg->max_data_size) {
1319 		IWL_ERR(drv, "uCode data len %zd too large to fit in\n",
1320 			get_sec_size(pieces, IWL_UCODE_REGULAR,
1321 				     IWL_UCODE_SECTION_DATA));
1322 		return -1;
1323 	}
1324 
1325 	if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) >
1326 	     cfg->max_inst_size) {
1327 		IWL_ERR(drv, "uCode init instr len %zd too large to fit in\n",
1328 			get_sec_size(pieces, IWL_UCODE_INIT,
1329 				     IWL_UCODE_SECTION_INST));
1330 		return -1;
1331 	}
1332 
1333 	if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA) >
1334 	    cfg->max_data_size) {
1335 		IWL_ERR(drv, "uCode init data len %zd too large to fit in\n",
1336 			get_sec_size(pieces, IWL_UCODE_REGULAR,
1337 				     IWL_UCODE_SECTION_DATA));
1338 		return -1;
1339 	}
1340 	return 0;
1341 }
1342 
1343 static struct iwl_op_mode *
1344 _iwl_op_mode_start(struct iwl_drv *drv, struct iwlwifi_opmode_table *op)
1345 {
1346 	const struct iwl_op_mode_ops *ops = op->ops;
1347 	struct dentry *dbgfs_dir = NULL;
1348 	struct iwl_op_mode *op_mode = NULL;
1349 	int retry, max_retry = !!iwlwifi_mod_params.fw_restart * IWL_MAX_INIT_RETRY;
1350 
1351 	for (retry = 0; retry <= max_retry; retry++) {
1352 
1353 #ifdef CONFIG_IWLWIFI_DEBUGFS
1354 		drv->dbgfs_op_mode = debugfs_create_dir(op->name,
1355 							drv->dbgfs_drv);
1356 		dbgfs_dir = drv->dbgfs_op_mode;
1357 #endif
1358 
1359 		op_mode = ops->start(drv->trans, drv->trans->cfg,
1360 				     &drv->fw, dbgfs_dir);
1361 
1362 		if (op_mode)
1363 			return op_mode;
1364 
1365 		IWL_ERR(drv, "retry init count %d\n", retry);
1366 
1367 #ifdef CONFIG_IWLWIFI_DEBUGFS
1368 		debugfs_remove_recursive(drv->dbgfs_op_mode);
1369 		drv->dbgfs_op_mode = NULL;
1370 #endif
1371 	}
1372 
1373 	return NULL;
1374 }
1375 
1376 static void _iwl_op_mode_stop(struct iwl_drv *drv)
1377 {
1378 	/* op_mode can be NULL if its start failed */
1379 	if (drv->op_mode) {
1380 		iwl_op_mode_stop(drv->op_mode);
1381 		drv->op_mode = NULL;
1382 
1383 #ifdef CONFIG_IWLWIFI_DEBUGFS
1384 		debugfs_remove_recursive(drv->dbgfs_op_mode);
1385 		drv->dbgfs_op_mode = NULL;
1386 #endif
1387 	}
1388 }
1389 
1390 /*
1391  * iwl_req_fw_callback - callback when firmware was loaded
1392  *
1393  * If loaded successfully, copies the firmware into buffers
1394  * for the card to fetch (via DMA).
1395  */
1396 static void iwl_req_fw_callback(const struct firmware *ucode_raw, void *context)
1397 {
1398 	struct iwl_drv *drv = context;
1399 	struct iwl_fw *fw = &drv->fw;
1400 	const struct iwl_ucode_header *ucode;
1401 	struct iwlwifi_opmode_table *op;
1402 	int err;
1403 	struct iwl_firmware_pieces *pieces;
1404 	const unsigned int api_max = drv->trans->cfg->ucode_api_max;
1405 	const unsigned int api_min = drv->trans->cfg->ucode_api_min;
1406 	size_t trigger_tlv_sz[FW_DBG_TRIGGER_MAX];
1407 	u32 api_ver;
1408 	int i;
1409 	bool load_module = false;
1410 	bool usniffer_images = false;
1411 	bool failure = true;
1412 
1413 	fw->ucode_capa.max_probe_length = IWL_DEFAULT_MAX_PROBE_LENGTH;
1414 	fw->ucode_capa.standard_phy_calibration_size =
1415 			IWL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE;
1416 	fw->ucode_capa.n_scan_channels = IWL_DEFAULT_SCAN_CHANNELS;
1417 	fw->ucode_capa.num_stations = IWL_MVM_STATION_COUNT_MAX;
1418 	/* dump all fw memory areas by default */
1419 	fw->dbg.dump_mask = 0xffffffff;
1420 
1421 	pieces = kzalloc(sizeof(*pieces), GFP_KERNEL);
1422 	if (!pieces)
1423 		goto out_free_fw;
1424 
1425 	if (!ucode_raw)
1426 		goto try_again;
1427 
1428 	IWL_DEBUG_FW_INFO(drv, "Loaded firmware file '%s' (%zd bytes).\n",
1429 			  drv->firmware_name, ucode_raw->size);
1430 
1431 	/* Make sure that we got at least the API version number */
1432 	if (ucode_raw->size < 4) {
1433 		IWL_ERR(drv, "File size way too small!\n");
1434 		goto try_again;
1435 	}
1436 
1437 	/* Data from ucode file:  header followed by uCode images */
1438 	ucode = (const struct iwl_ucode_header *)ucode_raw->data;
1439 
1440 	if (ucode->ver)
1441 		err = iwl_parse_v1_v2_firmware(drv, ucode_raw, pieces);
1442 	else
1443 		err = iwl_parse_tlv_firmware(drv, ucode_raw, pieces,
1444 					     &fw->ucode_capa, &usniffer_images);
1445 
1446 	if (err)
1447 		goto try_again;
1448 
1449 	if (fw_has_api(&drv->fw.ucode_capa, IWL_UCODE_TLV_API_NEW_VERSION))
1450 		api_ver = drv->fw.ucode_ver;
1451 	else
1452 		api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
1453 
1454 	/*
1455 	 * api_ver should match the api version forming part of the
1456 	 * firmware filename ... but we don't check for that and only rely
1457 	 * on the API version read from firmware header from here on forward
1458 	 */
1459 	if (api_ver < api_min || api_ver > api_max) {
1460 		IWL_ERR(drv,
1461 			"Driver unable to support your firmware API. "
1462 			"Driver supports v%u, firmware is v%u.\n",
1463 			api_max, api_ver);
1464 		goto try_again;
1465 	}
1466 
1467 	/*
1468 	 * In mvm uCode there is no difference between data and instructions
1469 	 * sections.
1470 	 */
1471 	if (fw->type == IWL_FW_DVM && validate_sec_sizes(drv, pieces,
1472 							 drv->trans->cfg))
1473 		goto try_again;
1474 
1475 	/* Allocate ucode buffers for card's bus-master loading ... */
1476 
1477 	/* Runtime instructions and 2 copies of data:
1478 	 * 1) unmodified from disk
1479 	 * 2) backup cache for save/restore during power-downs
1480 	 */
1481 	for (i = 0; i < IWL_UCODE_TYPE_MAX; i++)
1482 		if (iwl_alloc_ucode(drv, pieces, i))
1483 			goto out_free_fw;
1484 
1485 	if (pieces->dbg_dest_tlv_init) {
1486 		size_t dbg_dest_size = sizeof(*drv->fw.dbg.dest_tlv) +
1487 			sizeof(drv->fw.dbg.dest_tlv->reg_ops[0]) *
1488 			drv->fw.dbg.n_dest_reg;
1489 
1490 		drv->fw.dbg.dest_tlv = kmalloc(dbg_dest_size, GFP_KERNEL);
1491 
1492 		if (!drv->fw.dbg.dest_tlv)
1493 			goto out_free_fw;
1494 
1495 		if (*pieces->dbg_dest_ver == 0) {
1496 			memcpy(drv->fw.dbg.dest_tlv, pieces->dbg_dest_tlv_v1,
1497 			       dbg_dest_size);
1498 		} else {
1499 			struct iwl_fw_dbg_dest_tlv_v1 *dest_tlv =
1500 				drv->fw.dbg.dest_tlv;
1501 
1502 			dest_tlv->version = pieces->dbg_dest_tlv->version;
1503 			dest_tlv->monitor_mode =
1504 				pieces->dbg_dest_tlv->monitor_mode;
1505 			dest_tlv->size_power =
1506 				pieces->dbg_dest_tlv->size_power;
1507 			dest_tlv->wrap_count =
1508 				pieces->dbg_dest_tlv->wrap_count;
1509 			dest_tlv->write_ptr_reg =
1510 				pieces->dbg_dest_tlv->write_ptr_reg;
1511 			dest_tlv->base_shift =
1512 				pieces->dbg_dest_tlv->base_shift;
1513 			memcpy(dest_tlv->reg_ops,
1514 			       pieces->dbg_dest_tlv->reg_ops,
1515 			       sizeof(drv->fw.dbg.dest_tlv->reg_ops[0]) *
1516 			       drv->fw.dbg.n_dest_reg);
1517 
1518 			/* In version 1 of the destination tlv, which is
1519 			 * relevant for internal buffer exclusively,
1520 			 * the base address is part of given with the length
1521 			 * of the buffer, and the size shift is give instead of
1522 			 * end shift. We now store these values in base_reg,
1523 			 * and end shift, and when dumping the data we'll
1524 			 * manipulate it for extracting both the length and
1525 			 * base address */
1526 			dest_tlv->base_reg = pieces->dbg_dest_tlv->cfg_reg;
1527 			dest_tlv->end_shift =
1528 				pieces->dbg_dest_tlv->size_shift;
1529 		}
1530 	}
1531 
1532 	for (i = 0; i < ARRAY_SIZE(drv->fw.dbg.conf_tlv); i++) {
1533 		if (pieces->dbg_conf_tlv[i]) {
1534 			drv->fw.dbg.conf_tlv[i] =
1535 				kmemdup(pieces->dbg_conf_tlv[i],
1536 					pieces->dbg_conf_tlv_len[i],
1537 					GFP_KERNEL);
1538 			if (!drv->fw.dbg.conf_tlv[i])
1539 				goto out_free_fw;
1540 		}
1541 	}
1542 
1543 	memset(&trigger_tlv_sz, 0xff, sizeof(trigger_tlv_sz));
1544 
1545 	trigger_tlv_sz[FW_DBG_TRIGGER_MISSED_BEACONS] =
1546 		sizeof(struct iwl_fw_dbg_trigger_missed_bcon);
1547 	trigger_tlv_sz[FW_DBG_TRIGGER_CHANNEL_SWITCH] = 0;
1548 	trigger_tlv_sz[FW_DBG_TRIGGER_FW_NOTIF] =
1549 		sizeof(struct iwl_fw_dbg_trigger_cmd);
1550 	trigger_tlv_sz[FW_DBG_TRIGGER_MLME] =
1551 		sizeof(struct iwl_fw_dbg_trigger_mlme);
1552 	trigger_tlv_sz[FW_DBG_TRIGGER_STATS] =
1553 		sizeof(struct iwl_fw_dbg_trigger_stats);
1554 	trigger_tlv_sz[FW_DBG_TRIGGER_RSSI] =
1555 		sizeof(struct iwl_fw_dbg_trigger_low_rssi);
1556 	trigger_tlv_sz[FW_DBG_TRIGGER_TXQ_TIMERS] =
1557 		sizeof(struct iwl_fw_dbg_trigger_txq_timer);
1558 	trigger_tlv_sz[FW_DBG_TRIGGER_TIME_EVENT] =
1559 		sizeof(struct iwl_fw_dbg_trigger_time_event);
1560 	trigger_tlv_sz[FW_DBG_TRIGGER_BA] =
1561 		sizeof(struct iwl_fw_dbg_trigger_ba);
1562 	trigger_tlv_sz[FW_DBG_TRIGGER_TDLS] =
1563 		sizeof(struct iwl_fw_dbg_trigger_tdls);
1564 
1565 	for (i = 0; i < ARRAY_SIZE(drv->fw.dbg.trigger_tlv); i++) {
1566 		if (pieces->dbg_trigger_tlv[i]) {
1567 			/*
1568 			 * If the trigger isn't long enough, WARN and exit.
1569 			 * Someone is trying to debug something and he won't
1570 			 * be able to catch the bug he is trying to chase.
1571 			 * We'd better be noisy to be sure he knows what's
1572 			 * going on.
1573 			 */
1574 			if (WARN_ON(pieces->dbg_trigger_tlv_len[i] <
1575 				    (trigger_tlv_sz[i] +
1576 				     sizeof(struct iwl_fw_dbg_trigger_tlv))))
1577 				goto out_free_fw;
1578 			drv->fw.dbg.trigger_tlv_len[i] =
1579 				pieces->dbg_trigger_tlv_len[i];
1580 			drv->fw.dbg.trigger_tlv[i] =
1581 				kmemdup(pieces->dbg_trigger_tlv[i],
1582 					drv->fw.dbg.trigger_tlv_len[i],
1583 					GFP_KERNEL);
1584 			if (!drv->fw.dbg.trigger_tlv[i])
1585 				goto out_free_fw;
1586 		}
1587 	}
1588 
1589 	/* Now that we can no longer fail, copy information */
1590 
1591 	drv->fw.dbg.mem_tlv = pieces->dbg_mem_tlv;
1592 	pieces->dbg_mem_tlv = NULL;
1593 	drv->fw.dbg.n_mem_tlv = pieces->n_mem_tlv;
1594 
1595 	/*
1596 	 * The (size - 16) / 12 formula is based on the information recorded
1597 	 * for each event, which is of mode 1 (including timestamp) for all
1598 	 * new microcodes that include this information.
1599 	 */
1600 	fw->init_evtlog_ptr = pieces->init_evtlog_ptr;
1601 	if (pieces->init_evtlog_size)
1602 		fw->init_evtlog_size = (pieces->init_evtlog_size - 16)/12;
1603 	else
1604 		fw->init_evtlog_size =
1605 			drv->trans->trans_cfg->base_params->max_event_log_size;
1606 	fw->init_errlog_ptr = pieces->init_errlog_ptr;
1607 	fw->inst_evtlog_ptr = pieces->inst_evtlog_ptr;
1608 	if (pieces->inst_evtlog_size)
1609 		fw->inst_evtlog_size = (pieces->inst_evtlog_size - 16)/12;
1610 	else
1611 		fw->inst_evtlog_size =
1612 			drv->trans->trans_cfg->base_params->max_event_log_size;
1613 	fw->inst_errlog_ptr = pieces->inst_errlog_ptr;
1614 
1615 	/*
1616 	 * figure out the offset of chain noise reset and gain commands
1617 	 * base on the size of standard phy calibration commands table size
1618 	 */
1619 	if (fw->ucode_capa.standard_phy_calibration_size >
1620 	    IWL_MAX_PHY_CALIBRATE_TBL_SIZE)
1621 		fw->ucode_capa.standard_phy_calibration_size =
1622 			IWL_MAX_STANDARD_PHY_CALIBRATE_TBL_SIZE;
1623 
1624 	/* We have our copies now, allow OS release its copies */
1625 	release_firmware(ucode_raw);
1626 
1627 	iwl_dbg_tlv_load_bin(drv->trans->dev, drv->trans);
1628 
1629 	mutex_lock(&iwlwifi_opmode_table_mtx);
1630 	switch (fw->type) {
1631 	case IWL_FW_DVM:
1632 		op = &iwlwifi_opmode_table[DVM_OP_MODE];
1633 		break;
1634 	default:
1635 		WARN(1, "Invalid fw type %d\n", fw->type);
1636 		fallthrough;
1637 	case IWL_FW_MVM:
1638 		op = &iwlwifi_opmode_table[MVM_OP_MODE];
1639 		break;
1640 	}
1641 
1642 	IWL_INFO(drv, "loaded firmware version %s op_mode %s\n",
1643 		 drv->fw.fw_version, op->name);
1644 
1645 	/* add this device to the list of devices using this op_mode */
1646 	list_add_tail(&drv->list, &op->drv);
1647 
1648 	if (op->ops) {
1649 		drv->op_mode = _iwl_op_mode_start(drv, op);
1650 
1651 		if (!drv->op_mode) {
1652 			mutex_unlock(&iwlwifi_opmode_table_mtx);
1653 			goto out_unbind;
1654 		}
1655 	} else {
1656 		load_module = true;
1657 	}
1658 	mutex_unlock(&iwlwifi_opmode_table_mtx);
1659 
1660 	/*
1661 	 * Complete the firmware request last so that
1662 	 * a driver unbind (stop) doesn't run while we
1663 	 * are doing the start() above.
1664 	 */
1665 	complete(&drv->request_firmware_complete);
1666 
1667 	/*
1668 	 * Load the module last so we don't block anything
1669 	 * else from proceeding if the module fails to load
1670 	 * or hangs loading.
1671 	 */
1672 	if (load_module)
1673 		request_module("%s", op->name);
1674 	failure = false;
1675 	goto free;
1676 
1677  try_again:
1678 	/* try next, if any */
1679 	release_firmware(ucode_raw);
1680 	if (iwl_request_firmware(drv, false))
1681 		goto out_unbind;
1682 	goto free;
1683 
1684  out_free_fw:
1685 	release_firmware(ucode_raw);
1686  out_unbind:
1687 	complete(&drv->request_firmware_complete);
1688 	device_release_driver(drv->trans->dev);
1689 	/* drv has just been freed by the release */
1690 	failure = false;
1691  free:
1692 	if (failure)
1693 		iwl_dealloc_ucode(drv);
1694 
1695 	if (pieces) {
1696 		for (i = 0; i < ARRAY_SIZE(pieces->img); i++)
1697 			kfree(pieces->img[i].sec);
1698 		kfree(pieces->dbg_mem_tlv);
1699 		kfree(pieces);
1700 	}
1701 }
1702 
1703 struct iwl_drv *iwl_drv_start(struct iwl_trans *trans)
1704 {
1705 	struct iwl_drv *drv;
1706 	int ret;
1707 
1708 	drv = kzalloc(sizeof(*drv), GFP_KERNEL);
1709 	if (!drv) {
1710 		ret = -ENOMEM;
1711 		goto err;
1712 	}
1713 
1714 	drv->trans = trans;
1715 	drv->dev = trans->dev;
1716 
1717 	init_completion(&drv->request_firmware_complete);
1718 	INIT_LIST_HEAD(&drv->list);
1719 
1720 #ifdef CONFIG_IWLWIFI_DEBUGFS
1721 	/* Create the device debugfs entries. */
1722 	drv->dbgfs_drv = debugfs_create_dir(dev_name(trans->dev),
1723 					    iwl_dbgfs_root);
1724 
1725 	/* Create transport layer debugfs dir */
1726 	drv->trans->dbgfs_dir = debugfs_create_dir("trans", drv->dbgfs_drv);
1727 #endif
1728 
1729 	drv->trans->dbg.domains_bitmap = IWL_TRANS_FW_DBG_DOMAIN(drv->trans);
1730 
1731 	ret = iwl_request_firmware(drv, true);
1732 	if (ret) {
1733 		IWL_ERR(trans, "Couldn't request the fw\n");
1734 		goto err_fw;
1735 	}
1736 
1737 	return drv;
1738 
1739 err_fw:
1740 #ifdef CONFIG_IWLWIFI_DEBUGFS
1741 	debugfs_remove_recursive(drv->dbgfs_drv);
1742 	iwl_dbg_tlv_free(drv->trans);
1743 #endif
1744 	kfree(drv);
1745 err:
1746 	return ERR_PTR(ret);
1747 }
1748 
1749 void iwl_drv_stop(struct iwl_drv *drv)
1750 {
1751 	wait_for_completion(&drv->request_firmware_complete);
1752 
1753 	_iwl_op_mode_stop(drv);
1754 
1755 	iwl_dealloc_ucode(drv);
1756 
1757 	mutex_lock(&iwlwifi_opmode_table_mtx);
1758 	/*
1759 	 * List is empty (this item wasn't added)
1760 	 * when firmware loading failed -- in that
1761 	 * case we can't remove it from any list.
1762 	 */
1763 	if (!list_empty(&drv->list))
1764 		list_del(&drv->list);
1765 	mutex_unlock(&iwlwifi_opmode_table_mtx);
1766 
1767 #ifdef CONFIG_IWLWIFI_DEBUGFS
1768 	drv->trans->ops->debugfs_cleanup(drv->trans);
1769 
1770 	debugfs_remove_recursive(drv->dbgfs_drv);
1771 #endif
1772 
1773 	iwl_dbg_tlv_free(drv->trans);
1774 
1775 	kfree(drv);
1776 }
1777 
1778 #define ENABLE_INI	(IWL_DBG_TLV_MAX_PRESET + 1)
1779 
1780 /* shared module parameters */
1781 struct iwl_mod_params iwlwifi_mod_params = {
1782 	.fw_restart = true,
1783 	.bt_coex_active = true,
1784 	.power_level = IWL_POWER_INDEX_1,
1785 	.uapsd_disable = IWL_DISABLE_UAPSD_BSS | IWL_DISABLE_UAPSD_P2P_CLIENT,
1786 	.enable_ini = ENABLE_INI,
1787 	/* the rest are 0 by default */
1788 };
1789 IWL_EXPORT_SYMBOL(iwlwifi_mod_params);
1790 
1791 int iwl_opmode_register(const char *name, const struct iwl_op_mode_ops *ops)
1792 {
1793 	int i;
1794 	struct iwl_drv *drv;
1795 	struct iwlwifi_opmode_table *op;
1796 
1797 	mutex_lock(&iwlwifi_opmode_table_mtx);
1798 	for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++) {
1799 		op = &iwlwifi_opmode_table[i];
1800 		if (strcmp(op->name, name))
1801 			continue;
1802 		op->ops = ops;
1803 		/* TODO: need to handle exceptional case */
1804 		list_for_each_entry(drv, &op->drv, list)
1805 			drv->op_mode = _iwl_op_mode_start(drv, op);
1806 
1807 		mutex_unlock(&iwlwifi_opmode_table_mtx);
1808 		return 0;
1809 	}
1810 	mutex_unlock(&iwlwifi_opmode_table_mtx);
1811 	return -EIO;
1812 }
1813 IWL_EXPORT_SYMBOL(iwl_opmode_register);
1814 
1815 void iwl_opmode_deregister(const char *name)
1816 {
1817 	int i;
1818 	struct iwl_drv *drv;
1819 
1820 	mutex_lock(&iwlwifi_opmode_table_mtx);
1821 	for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++) {
1822 		if (strcmp(iwlwifi_opmode_table[i].name, name))
1823 			continue;
1824 		iwlwifi_opmode_table[i].ops = NULL;
1825 
1826 		/* call the stop routine for all devices */
1827 		list_for_each_entry(drv, &iwlwifi_opmode_table[i].drv, list)
1828 			_iwl_op_mode_stop(drv);
1829 
1830 		mutex_unlock(&iwlwifi_opmode_table_mtx);
1831 		return;
1832 	}
1833 	mutex_unlock(&iwlwifi_opmode_table_mtx);
1834 }
1835 IWL_EXPORT_SYMBOL(iwl_opmode_deregister);
1836 
1837 static int __init iwl_drv_init(void)
1838 {
1839 	int i, err;
1840 
1841 	for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++)
1842 		INIT_LIST_HEAD(&iwlwifi_opmode_table[i].drv);
1843 
1844 	pr_info(DRV_DESCRIPTION "\n");
1845 
1846 #ifdef CONFIG_IWLWIFI_DEBUGFS
1847 	/* Create the root of iwlwifi debugfs subsystem. */
1848 	iwl_dbgfs_root = debugfs_create_dir(DRV_NAME, NULL);
1849 #endif
1850 
1851 	err = iwl_pci_register_driver();
1852 	if (err)
1853 		goto cleanup_debugfs;
1854 
1855 	return 0;
1856 
1857 cleanup_debugfs:
1858 #ifdef CONFIG_IWLWIFI_DEBUGFS
1859 	debugfs_remove_recursive(iwl_dbgfs_root);
1860 #endif
1861 	return err;
1862 }
1863 module_init(iwl_drv_init);
1864 
1865 static void __exit iwl_drv_exit(void)
1866 {
1867 	iwl_pci_unregister_driver();
1868 
1869 #ifdef CONFIG_IWLWIFI_DEBUGFS
1870 	debugfs_remove_recursive(iwl_dbgfs_root);
1871 #endif
1872 }
1873 module_exit(iwl_drv_exit);
1874 
1875 #ifdef CONFIG_IWLWIFI_DEBUG
1876 module_param_named(debug, iwlwifi_mod_params.debug_level, uint, 0644);
1877 MODULE_PARM_DESC(debug, "debug output mask");
1878 #endif
1879 
1880 module_param_named(swcrypto, iwlwifi_mod_params.swcrypto, int, 0444);
1881 MODULE_PARM_DESC(swcrypto, "using crypto in software (default 0 [hardware])");
1882 module_param_named(11n_disable, iwlwifi_mod_params.disable_11n, uint, 0444);
1883 MODULE_PARM_DESC(11n_disable,
1884 	"disable 11n functionality, bitmap: 1: full, 2: disable agg TX, 4: disable agg RX, 8 enable agg TX");
1885 module_param_named(amsdu_size, iwlwifi_mod_params.amsdu_size, int, 0444);
1886 MODULE_PARM_DESC(amsdu_size,
1887 		 "amsdu size 0: 12K for multi Rx queue devices, 2K for AX210 devices, "
1888 		 "4K for other devices 1:4K 2:8K 3:12K (16K buffers) 4: 2K (default 0)");
1889 module_param_named(fw_restart, iwlwifi_mod_params.fw_restart, bool, 0444);
1890 MODULE_PARM_DESC(fw_restart, "restart firmware in case of error (default true)");
1891 
1892 module_param_named(nvm_file, iwlwifi_mod_params.nvm_file, charp, 0444);
1893 MODULE_PARM_DESC(nvm_file, "NVM file name");
1894 
1895 module_param_named(uapsd_disable, iwlwifi_mod_params.uapsd_disable, uint, 0644);
1896 MODULE_PARM_DESC(uapsd_disable,
1897 		 "disable U-APSD functionality bitmap 1: BSS 2: P2P Client (default: 3)");
1898 
1899 static int enable_ini_set(const char *arg, const struct kernel_param *kp)
1900 {
1901 	int ret = 0;
1902 	bool res;
1903 	__u32 new_enable_ini;
1904 
1905 	/* in case the argument type is a number */
1906 	ret = kstrtou32(arg, 0, &new_enable_ini);
1907 	if (!ret) {
1908 		if (new_enable_ini > ENABLE_INI) {
1909 			pr_err("enable_ini cannot be %d, in range 0-16\n", new_enable_ini);
1910 			return -EINVAL;
1911 		}
1912 		goto out;
1913 	}
1914 
1915 	/* in case the argument type is boolean */
1916 	ret = kstrtobool(arg, &res);
1917 	if (ret)
1918 		return ret;
1919 	new_enable_ini = (res ? ENABLE_INI : 0);
1920 
1921 out:
1922 	iwlwifi_mod_params.enable_ini = new_enable_ini;
1923 	return 0;
1924 }
1925 
1926 static const struct kernel_param_ops enable_ini_ops = {
1927 	.set = enable_ini_set
1928 };
1929 
1930 module_param_cb(enable_ini, &enable_ini_ops, &iwlwifi_mod_params.enable_ini, 0644);
1931 MODULE_PARM_DESC(enable_ini,
1932 		 "0:disable, 1-15:FW_DBG_PRESET Values, 16:enabled without preset value defined,"
1933 		 "Debug INI TLV FW debug infrastructure (default: 16)");
1934 
1935 /*
1936  * set bt_coex_active to true, uCode will do kill/defer
1937  * every time the priority line is asserted (BT is sending signals on the
1938  * priority line in the PCIx).
1939  * set bt_coex_active to false, uCode will ignore the BT activity and
1940  * perform the normal operation
1941  *
1942  * User might experience transmit issue on some platform due to WiFi/BT
1943  * co-exist problem. The possible behaviors are:
1944  *   Able to scan and finding all the available AP
1945  *   Not able to associate with any AP
1946  * On those platforms, WiFi communication can be restored by set
1947  * "bt_coex_active" module parameter to "false"
1948  *
1949  * default: bt_coex_active = true (BT_COEX_ENABLE)
1950  */
1951 module_param_named(bt_coex_active, iwlwifi_mod_params.bt_coex_active,
1952 		   bool, 0444);
1953 MODULE_PARM_DESC(bt_coex_active, "enable wifi/bt co-exist (default: enable)");
1954 
1955 module_param_named(led_mode, iwlwifi_mod_params.led_mode, int, 0444);
1956 MODULE_PARM_DESC(led_mode, "0=system default, "
1957 		"1=On(RF On)/Off(RF Off), 2=blinking, 3=Off (default: 0)");
1958 
1959 module_param_named(power_save, iwlwifi_mod_params.power_save, bool, 0444);
1960 MODULE_PARM_DESC(power_save,
1961 		 "enable WiFi power management (default: disable)");
1962 
1963 module_param_named(power_level, iwlwifi_mod_params.power_level, int, 0444);
1964 MODULE_PARM_DESC(power_level,
1965 		 "default power save level (range from 1 - 5, default: 1)");
1966 
1967 module_param_named(disable_11ac, iwlwifi_mod_params.disable_11ac, bool, 0444);
1968 MODULE_PARM_DESC(disable_11ac, "Disable VHT capabilities (default: false)");
1969 
1970 module_param_named(remove_when_gone,
1971 		   iwlwifi_mod_params.remove_when_gone, bool,
1972 		   0444);
1973 MODULE_PARM_DESC(remove_when_gone,
1974 		 "Remove dev from PCIe bus if it is deemed inaccessible (default: false)");
1975 
1976 module_param_named(disable_11ax, iwlwifi_mod_params.disable_11ax, bool,
1977 		   S_IRUGO);
1978 MODULE_PARM_DESC(disable_11ax, "Disable HE capabilities (default: false)");
1979 
1980 module_param_named(disable_11be, iwlwifi_mod_params.disable_11be, bool, 0444);
1981 MODULE_PARM_DESC(disable_11be, "Disable EHT capabilities (default: false)");
1982