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