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