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