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