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