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