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