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