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