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