1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2005-2014, 2018-2021 Intel Corporation
4  * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
5  * Copyright (C) 2016-2017 Intel Deutschland GmbH
6  */
7 #include <linux/completion.h>
8 #include <linux/dma-mapping.h>
9 #include <linux/firmware.h>
10 #include <linux/module.h>
11 #include <linux/vmalloc.h>
12 
13 #include "iwl-drv.h"
14 #include "iwl-csr.h"
15 #include "iwl-debug.h"
16 #include "iwl-trans.h"
17 #include "iwl-op-mode.h"
18 #include "iwl-agn-hw.h"
19 #include "fw/img.h"
20 #include "iwl-dbg-tlv.h"
21 #include "iwl-config.h"
22 #include "iwl-modparams.h"
23 #include "fw/api/alive.h"
24 #include "fw/api/mac.h"
25 
26 /******************************************************************************
27  *
28  * module boiler plate
29  *
30  ******************************************************************************/
31 
32 #define DRV_DESCRIPTION	"Intel(R) Wireless WiFi driver for Linux"
33 MODULE_DESCRIPTION(DRV_DESCRIPTION);
34 MODULE_LICENSE("GPL");
35 
36 #ifdef CONFIG_IWLWIFI_DEBUGFS
37 static struct dentry *iwl_dbgfs_root;
38 #endif
39 
40 /**
41  * struct iwl_drv - drv common data
42  * @list: list of drv structures using this opmode
43  * @fw: the iwl_fw structure
44  * @op_mode: the running op_mode
45  * @trans: transport layer
46  * @dev: for debug prints only
47  * @fw_index: firmware revision to try loading
48  * @firmware_name: composite filename of ucode file to load
49  * @request_firmware_complete: the firmware has been obtained from user space
50  * @dbgfs_drv: debugfs root directory entry
51  * @dbgfs_trans: debugfs transport directory entry
52  * @dbgfs_op_mode: debugfs op_mode directory entry
53  */
54 struct iwl_drv {
55 	struct list_head list;
56 	struct iwl_fw fw;
57 
58 	struct iwl_op_mode *op_mode;
59 	struct iwl_trans *trans;
60 	struct device *dev;
61 
62 	int fw_index;                   /* firmware we're trying to load */
63 	char firmware_name[64];         /* name of firmware file to load */
64 
65 	struct completion request_firmware_complete;
66 
67 #ifdef CONFIG_IWLWIFI_DEBUGFS
68 	struct dentry *dbgfs_drv;
69 	struct dentry *dbgfs_trans;
70 	struct dentry *dbgfs_op_mode;
71 #endif
72 };
73 
74 enum {
75 	DVM_OP_MODE,
76 	MVM_OP_MODE,
77 };
78 
79 /* Protects the table contents, i.e. the ops pointer & drv list */
80 static DEFINE_MUTEX(iwlwifi_opmode_table_mtx);
81 static struct iwlwifi_opmode_table {
82 	const char *name;			/* name: iwldvm, iwlmvm, etc */
83 	const struct iwl_op_mode_ops *ops;	/* pointer to op_mode ops */
84 	struct list_head drv;		/* list of devices using this op_mode */
85 } iwlwifi_opmode_table[] = {		/* ops set when driver is initialized */
86 	[DVM_OP_MODE] = { .name = "iwldvm", .ops = NULL },
87 	[MVM_OP_MODE] = { .name = "iwlmvm", .ops = NULL },
88 };
89 
90 #define IWL_DEFAULT_SCAN_CHANNELS 40
91 
92 /*
93  * struct fw_sec: Just for the image parsing process.
94  * For the fw storage we are using struct fw_desc.
95  */
96 struct fw_sec {
97 	const void *data;		/* the sec data */
98 	size_t size;			/* section size */
99 	u32 offset;			/* offset of writing in the device */
100 };
101 
102 static void iwl_free_fw_desc(struct iwl_drv *drv, struct fw_desc *desc)
103 {
104 	vfree(desc->data);
105 	desc->data = NULL;
106 	desc->len = 0;
107 }
108 
109 static void iwl_free_fw_img(struct iwl_drv *drv, struct fw_img *img)
110 {
111 	int i;
112 	for (i = 0; i < img->num_sec; i++)
113 		iwl_free_fw_desc(drv, &img->sec[i]);
114 	kfree(img->sec);
115 }
116 
117 static void iwl_dealloc_ucode(struct iwl_drv *drv)
118 {
119 	int i;
120 
121 	kfree(drv->fw.dbg.dest_tlv);
122 	for (i = 0; i < ARRAY_SIZE(drv->fw.dbg.conf_tlv); i++)
123 		kfree(drv->fw.dbg.conf_tlv[i]);
124 	for (i = 0; i < ARRAY_SIZE(drv->fw.dbg.trigger_tlv); i++)
125 		kfree(drv->fw.dbg.trigger_tlv[i]);
126 	kfree(drv->fw.dbg.mem_tlv);
127 	kfree(drv->fw.iml);
128 	kfree(drv->fw.ucode_capa.cmd_versions);
129 	kfree(drv->fw.phy_integration_ver);
130 
131 	for (i = 0; i < IWL_UCODE_TYPE_MAX; i++)
132 		iwl_free_fw_img(drv, drv->fw.img + i);
133 
134 	/* clear the data for the aborted load case */
135 	memset(&drv->fw, 0, sizeof(drv->fw));
136 }
137 
138 static int iwl_alloc_fw_desc(struct iwl_drv *drv, struct fw_desc *desc,
139 			     struct fw_sec *sec)
140 {
141 	void *data;
142 
143 	desc->data = NULL;
144 
145 	if (!sec || !sec->size)
146 		return -EINVAL;
147 
148 	data = vmalloc(sec->size);
149 	if (!data)
150 		return -ENOMEM;
151 
152 	desc->len = sec->size;
153 	desc->offset = sec->offset;
154 	memcpy(data, sec->data, desc->len);
155 	desc->data = data;
156 
157 	return 0;
158 }
159 
160 static void iwl_req_fw_callback(const struct firmware *ucode_raw,
161 				void *context);
162 
163 static int iwl_request_firmware(struct iwl_drv *drv, bool first)
164 {
165 	const struct iwl_cfg *cfg = drv->trans->cfg;
166 	char tag[8];
167 
168 	if (drv->trans->trans_cfg->device_family == IWL_DEVICE_FAMILY_9000 &&
169 	    (drv->trans->hw_rev_step != SILICON_B_STEP &&
170 	     drv->trans->hw_rev_step != SILICON_C_STEP)) {
171 		IWL_ERR(drv,
172 			"Only HW steps B and C are currently supported (0x%0x)\n",
173 			drv->trans->hw_rev);
174 		return -EINVAL;
175 	}
176 
177 	if (first) {
178 		drv->fw_index = cfg->ucode_api_max;
179 		sprintf(tag, "%d", drv->fw_index);
180 	} else {
181 		drv->fw_index--;
182 		sprintf(tag, "%d", drv->fw_index);
183 	}
184 
185 	if (drv->fw_index < cfg->ucode_api_min) {
186 		IWL_ERR(drv, "no suitable firmware found!\n");
187 
188 		if (cfg->ucode_api_min == cfg->ucode_api_max) {
189 			IWL_ERR(drv, "%s%d is required\n", cfg->fw_name_pre,
190 				cfg->ucode_api_max);
191 		} else {
192 			IWL_ERR(drv, "minimum version required: %s%d\n",
193 				cfg->fw_name_pre, cfg->ucode_api_min);
194 			IWL_ERR(drv, "maximum version supported: %s%d\n",
195 				cfg->fw_name_pre, cfg->ucode_api_max);
196 		}
197 
198 		IWL_ERR(drv,
199 			"check git://git.kernel.org/pub/scm/linux/kernel/git/firmware/linux-firmware.git\n");
200 		return -ENOENT;
201 	}
202 
203 	snprintf(drv->firmware_name, sizeof(drv->firmware_name), "%s%s.ucode",
204 		 cfg->fw_name_pre, tag);
205 
206 	IWL_DEBUG_FW_INFO(drv, "attempting to load firmware '%s'\n",
207 			  drv->firmware_name);
208 
209 	return request_firmware_nowait(THIS_MODULE, 1, drv->firmware_name,
210 				       drv->trans->dev,
211 				       GFP_KERNEL, drv, iwl_req_fw_callback);
212 }
213 
214 struct fw_img_parsing {
215 	struct fw_sec *sec;
216 	int sec_counter;
217 };
218 
219 /*
220  * struct fw_sec_parsing: to extract fw section and it's offset from tlv
221  */
222 struct fw_sec_parsing {
223 	__le32 offset;
224 	const u8 data[];
225 } __packed;
226 
227 /**
228  * struct iwl_tlv_calib_data - parse the default calib data from TLV
229  *
230  * @ucode_type: the uCode to which the following default calib relates.
231  * @calib: default calibrations.
232  */
233 struct iwl_tlv_calib_data {
234 	__le32 ucode_type;
235 	struct iwl_tlv_calib_ctrl calib;
236 } __packed;
237 
238 struct iwl_firmware_pieces {
239 	struct fw_img_parsing img[IWL_UCODE_TYPE_MAX];
240 
241 	u32 init_evtlog_ptr, init_evtlog_size, init_errlog_ptr;
242 	u32 inst_evtlog_ptr, inst_evtlog_size, inst_errlog_ptr;
243 
244 	/* FW debug data parsed for driver usage */
245 	bool dbg_dest_tlv_init;
246 	u8 *dbg_dest_ver;
247 	union {
248 		struct iwl_fw_dbg_dest_tlv *dbg_dest_tlv;
249 		struct iwl_fw_dbg_dest_tlv_v1 *dbg_dest_tlv_v1;
250 	};
251 	struct iwl_fw_dbg_conf_tlv *dbg_conf_tlv[FW_DBG_CONF_MAX];
252 	size_t dbg_conf_tlv_len[FW_DBG_CONF_MAX];
253 	struct iwl_fw_dbg_trigger_tlv *dbg_trigger_tlv[FW_DBG_TRIGGER_MAX];
254 	size_t dbg_trigger_tlv_len[FW_DBG_TRIGGER_MAX];
255 	struct iwl_fw_dbg_mem_seg_tlv *dbg_mem_tlv;
256 	size_t n_mem_tlv;
257 };
258 
259 /*
260  * These functions are just to extract uCode section data from the pieces
261  * structure.
262  */
263 static struct fw_sec *get_sec(struct iwl_firmware_pieces *pieces,
264 			      enum iwl_ucode_type type,
265 			      int  sec)
266 {
267 	return &pieces->img[type].sec[sec];
268 }
269 
270 static void alloc_sec_data(struct iwl_firmware_pieces *pieces,
271 			   enum iwl_ucode_type type,
272 			   int sec)
273 {
274 	struct fw_img_parsing *img = &pieces->img[type];
275 	struct fw_sec *sec_memory;
276 	int size = sec + 1;
277 	size_t alloc_size = sizeof(*img->sec) * size;
278 
279 	if (img->sec && img->sec_counter >= size)
280 		return;
281 
282 	sec_memory = krealloc(img->sec, alloc_size, GFP_KERNEL);
283 	if (!sec_memory)
284 		return;
285 
286 	img->sec = sec_memory;
287 	img->sec_counter = size;
288 }
289 
290 static void set_sec_data(struct iwl_firmware_pieces *pieces,
291 			 enum iwl_ucode_type type,
292 			 int sec,
293 			 const void *data)
294 {
295 	alloc_sec_data(pieces, type, sec);
296 
297 	pieces->img[type].sec[sec].data = data;
298 }
299 
300 static void set_sec_size(struct iwl_firmware_pieces *pieces,
301 			 enum iwl_ucode_type type,
302 			 int sec,
303 			 size_t size)
304 {
305 	alloc_sec_data(pieces, type, sec);
306 
307 	pieces->img[type].sec[sec].size = size;
308 }
309 
310 static size_t get_sec_size(struct iwl_firmware_pieces *pieces,
311 			   enum iwl_ucode_type type,
312 			   int sec)
313 {
314 	return pieces->img[type].sec[sec].size;
315 }
316 
317 static void set_sec_offset(struct iwl_firmware_pieces *pieces,
318 			   enum iwl_ucode_type type,
319 			   int sec,
320 			   u32 offset)
321 {
322 	alloc_sec_data(pieces, type, sec);
323 
324 	pieces->img[type].sec[sec].offset = offset;
325 }
326 
327 static int iwl_store_cscheme(struct iwl_fw *fw, const u8 *data, const u32 len)
328 {
329 	int i, j;
330 	struct iwl_fw_cscheme_list *l = (struct iwl_fw_cscheme_list *)data;
331 	struct iwl_fw_cipher_scheme *fwcs;
332 
333 	if (len < sizeof(*l) ||
334 	    len < sizeof(l->size) + l->size * sizeof(l->cs[0]))
335 		return -EINVAL;
336 
337 	for (i = 0, j = 0; i < IWL_UCODE_MAX_CS && i < l->size; i++) {
338 		fwcs = &l->cs[j];
339 
340 		/* we skip schemes with zero cipher suite selector */
341 		if (!fwcs->cipher)
342 			continue;
343 
344 		fw->cs[j++] = *fwcs;
345 	}
346 
347 	return 0;
348 }
349 
350 /*
351  * Gets uCode section from tlv.
352  */
353 static int iwl_store_ucode_sec(struct iwl_firmware_pieces *pieces,
354 			       const void *data, enum iwl_ucode_type type,
355 			       int size)
356 {
357 	struct fw_img_parsing *img;
358 	struct fw_sec *sec;
359 	struct fw_sec_parsing *sec_parse;
360 	size_t alloc_size;
361 
362 	if (WARN_ON(!pieces || !data || type >= IWL_UCODE_TYPE_MAX))
363 		return -1;
364 
365 	sec_parse = (struct fw_sec_parsing *)data;
366 
367 	img = &pieces->img[type];
368 
369 	alloc_size = sizeof(*img->sec) * (img->sec_counter + 1);
370 	sec = krealloc(img->sec, alloc_size, GFP_KERNEL);
371 	if (!sec)
372 		return -ENOMEM;
373 	img->sec = sec;
374 
375 	sec = &img->sec[img->sec_counter];
376 
377 	sec->offset = le32_to_cpu(sec_parse->offset);
378 	sec->data = sec_parse->data;
379 	sec->size = size - sizeof(sec_parse->offset);
380 
381 	++img->sec_counter;
382 
383 	return 0;
384 }
385 
386 static int iwl_set_default_calib(struct iwl_drv *drv, const u8 *data)
387 {
388 	struct iwl_tlv_calib_data *def_calib =
389 					(struct iwl_tlv_calib_data *)data;
390 	u32 ucode_type = le32_to_cpu(def_calib->ucode_type);
391 	if (ucode_type >= IWL_UCODE_TYPE_MAX) {
392 		IWL_ERR(drv, "Wrong ucode_type %u for default calibration.\n",
393 			ucode_type);
394 		return -EINVAL;
395 	}
396 	drv->fw.default_calib[ucode_type].flow_trigger =
397 		def_calib->calib.flow_trigger;
398 	drv->fw.default_calib[ucode_type].event_trigger =
399 		def_calib->calib.event_trigger;
400 
401 	return 0;
402 }
403 
404 static void iwl_set_ucode_api_flags(struct iwl_drv *drv, const u8 *data,
405 				    struct iwl_ucode_capabilities *capa)
406 {
407 	const struct iwl_ucode_api *ucode_api = (void *)data;
408 	u32 api_index = le32_to_cpu(ucode_api->api_index);
409 	u32 api_flags = le32_to_cpu(ucode_api->api_flags);
410 	int i;
411 
412 	if (api_index >= DIV_ROUND_UP(NUM_IWL_UCODE_TLV_API, 32)) {
413 		IWL_WARN(drv,
414 			 "api flags index %d larger than supported by driver\n",
415 			 api_index);
416 		return;
417 	}
418 
419 	for (i = 0; i < 32; i++) {
420 		if (api_flags & BIT(i))
421 			__set_bit(i + 32 * api_index, capa->_api);
422 	}
423 }
424 
425 static void iwl_set_ucode_capabilities(struct iwl_drv *drv, const u8 *data,
426 				       struct iwl_ucode_capabilities *capa)
427 {
428 	const struct iwl_ucode_capa *ucode_capa = (void *)data;
429 	u32 api_index = le32_to_cpu(ucode_capa->api_index);
430 	u32 api_flags = le32_to_cpu(ucode_capa->api_capa);
431 	int i;
432 
433 	if (api_index >= DIV_ROUND_UP(NUM_IWL_UCODE_TLV_CAPA, 32)) {
434 		IWL_WARN(drv,
435 			 "capa flags index %d larger than supported by driver\n",
436 			 api_index);
437 		return;
438 	}
439 
440 	for (i = 0; i < 32; i++) {
441 		if (api_flags & BIT(i))
442 			__set_bit(i + 32 * api_index, capa->_capa);
443 	}
444 }
445 
446 static const char *iwl_reduced_fw_name(struct iwl_drv *drv)
447 {
448 	const char *name = drv->firmware_name;
449 
450 	if (strncmp(name, "iwlwifi-", 8) == 0)
451 		name += 8;
452 
453 	return name;
454 }
455 
456 static int iwl_parse_v1_v2_firmware(struct iwl_drv *drv,
457 				    const struct firmware *ucode_raw,
458 				    struct iwl_firmware_pieces *pieces)
459 {
460 	struct iwl_ucode_header *ucode = (void *)ucode_raw->data;
461 	u32 api_ver, hdr_size, build;
462 	char buildstr[25];
463 	const u8 *src;
464 
465 	drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
466 	api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
467 
468 	switch (api_ver) {
469 	default:
470 		hdr_size = 28;
471 		if (ucode_raw->size < hdr_size) {
472 			IWL_ERR(drv, "File size too small!\n");
473 			return -EINVAL;
474 		}
475 		build = le32_to_cpu(ucode->u.v2.build);
476 		set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
477 			     le32_to_cpu(ucode->u.v2.inst_size));
478 		set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
479 			     le32_to_cpu(ucode->u.v2.data_size));
480 		set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
481 			     le32_to_cpu(ucode->u.v2.init_size));
482 		set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
483 			     le32_to_cpu(ucode->u.v2.init_data_size));
484 		src = ucode->u.v2.data;
485 		break;
486 	case 0:
487 	case 1:
488 	case 2:
489 		hdr_size = 24;
490 		if (ucode_raw->size < hdr_size) {
491 			IWL_ERR(drv, "File size too small!\n");
492 			return -EINVAL;
493 		}
494 		build = 0;
495 		set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
496 			     le32_to_cpu(ucode->u.v1.inst_size));
497 		set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
498 			     le32_to_cpu(ucode->u.v1.data_size));
499 		set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
500 			     le32_to_cpu(ucode->u.v1.init_size));
501 		set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
502 			     le32_to_cpu(ucode->u.v1.init_data_size));
503 		src = ucode->u.v1.data;
504 		break;
505 	}
506 
507 	if (build)
508 		sprintf(buildstr, " build %u", build);
509 	else
510 		buildstr[0] = '\0';
511 
512 	snprintf(drv->fw.fw_version,
513 		 sizeof(drv->fw.fw_version),
514 		 "%u.%u.%u.%u%s %s",
515 		 IWL_UCODE_MAJOR(drv->fw.ucode_ver),
516 		 IWL_UCODE_MINOR(drv->fw.ucode_ver),
517 		 IWL_UCODE_API(drv->fw.ucode_ver),
518 		 IWL_UCODE_SERIAL(drv->fw.ucode_ver),
519 		 buildstr, iwl_reduced_fw_name(drv));
520 
521 	/* Verify size of file vs. image size info in file's header */
522 
523 	if (ucode_raw->size != hdr_size +
524 	    get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) +
525 	    get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) +
526 	    get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) +
527 	    get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA)) {
528 
529 		IWL_ERR(drv,
530 			"uCode file size %d does not match expected size\n",
531 			(int)ucode_raw->size);
532 		return -EINVAL;
533 	}
534 
535 
536 	set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST, src);
537 	src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST);
538 	set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
539 		       IWLAGN_RTC_INST_LOWER_BOUND);
540 	set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA, src);
541 	src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA);
542 	set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
543 		       IWLAGN_RTC_DATA_LOWER_BOUND);
544 	set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST, src);
545 	src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST);
546 	set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
547 		       IWLAGN_RTC_INST_LOWER_BOUND);
548 	set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA, src);
549 	src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA);
550 	set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
551 		       IWLAGN_RTC_DATA_LOWER_BOUND);
552 	return 0;
553 }
554 
555 static void iwl_drv_set_dump_exclude(struct iwl_drv *drv,
556 				     enum iwl_ucode_tlv_type tlv_type,
557 				     const void *tlv_data, u32 tlv_len)
558 {
559 	const struct iwl_fw_dump_exclude *fw = tlv_data;
560 	struct iwl_dump_exclude *excl;
561 
562 	if (tlv_len < sizeof(*fw))
563 		return;
564 
565 	if (tlv_type == IWL_UCODE_TLV_SEC_TABLE_ADDR) {
566 		excl = &drv->fw.dump_excl[0];
567 
568 		/* second time we find this, it's for WoWLAN */
569 		if (excl->addr)
570 			excl = &drv->fw.dump_excl_wowlan[0];
571 	} else if (fw_has_capa(&drv->fw.ucode_capa,
572 			       IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG)) {
573 		/* IWL_UCODE_TLV_D3_KEK_KCK_ADDR is regular image */
574 		excl = &drv->fw.dump_excl[0];
575 	} else {
576 		/* IWL_UCODE_TLV_D3_KEK_KCK_ADDR is WoWLAN image */
577 		excl = &drv->fw.dump_excl_wowlan[0];
578 	}
579 
580 	if (excl->addr)
581 		excl++;
582 
583 	if (excl->addr) {
584 		IWL_DEBUG_FW_INFO(drv, "found too many excludes in fw file\n");
585 		return;
586 	}
587 
588 	excl->addr = le32_to_cpu(fw->addr) & ~FW_ADDR_CACHE_CONTROL;
589 	excl->size = le32_to_cpu(fw->size);
590 }
591 
592 static void iwl_parse_dbg_tlv_assert_tables(struct iwl_drv *drv,
593 					    const struct iwl_ucode_tlv *tlv)
594 {
595 	const struct iwl_fw_ini_region_tlv *region;
596 	u32 length = le32_to_cpu(tlv->length);
597 	u32 addr;
598 
599 	if (length < offsetof(typeof(*region), special_mem) +
600 		     sizeof(region->special_mem))
601 		return;
602 
603 	region = (void *)tlv->data;
604 	addr = le32_to_cpu(region->special_mem.base_addr);
605 	addr += le32_to_cpu(region->special_mem.offset);
606 	addr &= ~FW_ADDR_CACHE_CONTROL;
607 
608 	if (region->type != IWL_FW_INI_REGION_SPECIAL_DEVICE_MEMORY)
609 		return;
610 
611 	switch (region->sub_type) {
612 	case IWL_FW_INI_REGION_DEVICE_MEMORY_SUBTYPE_UMAC_ERROR_TABLE:
613 		drv->trans->dbg.umac_error_event_table = addr;
614 		drv->trans->dbg.error_event_table_tlv_status |=
615 			IWL_ERROR_EVENT_TABLE_UMAC;
616 		break;
617 	case IWL_FW_INI_REGION_DEVICE_MEMORY_SUBTYPE_LMAC_1_ERROR_TABLE:
618 		drv->trans->dbg.lmac_error_event_table[0] = addr;
619 		drv->trans->dbg.error_event_table_tlv_status |=
620 			IWL_ERROR_EVENT_TABLE_LMAC1;
621 		break;
622 	case IWL_FW_INI_REGION_DEVICE_MEMORY_SUBTYPE_LMAC_2_ERROR_TABLE:
623 		drv->trans->dbg.lmac_error_event_table[1] = addr;
624 		drv->trans->dbg.error_event_table_tlv_status |=
625 			IWL_ERROR_EVENT_TABLE_LMAC2;
626 		break;
627 	case IWL_FW_INI_REGION_DEVICE_MEMORY_SUBTYPE_TCM_1_ERROR_TABLE:
628 		drv->trans->dbg.tcm_error_event_table[0] = addr;
629 		drv->trans->dbg.error_event_table_tlv_status |=
630 			IWL_ERROR_EVENT_TABLE_TCM1;
631 		break;
632 	case IWL_FW_INI_REGION_DEVICE_MEMORY_SUBTYPE_TCM_2_ERROR_TABLE:
633 		drv->trans->dbg.tcm_error_event_table[1] = addr;
634 		drv->trans->dbg.error_event_table_tlv_status |=
635 			IWL_ERROR_EVENT_TABLE_TCM2;
636 		break;
637 	case IWL_FW_INI_REGION_DEVICE_MEMORY_SUBTYPE_RCM_1_ERROR_TABLE:
638 		drv->trans->dbg.rcm_error_event_table[0] = addr;
639 		drv->trans->dbg.error_event_table_tlv_status |=
640 			IWL_ERROR_EVENT_TABLE_RCM1;
641 		break;
642 	case IWL_FW_INI_REGION_DEVICE_MEMORY_SUBTYPE_RCM_2_ERROR_TABLE:
643 		drv->trans->dbg.rcm_error_event_table[1] = addr;
644 		drv->trans->dbg.error_event_table_tlv_status |=
645 			IWL_ERROR_EVENT_TABLE_RCM2;
646 		break;
647 	default:
648 		break;
649 	}
650 }
651 
652 static int iwl_parse_tlv_firmware(struct iwl_drv *drv,
653 				const struct firmware *ucode_raw,
654 				struct iwl_firmware_pieces *pieces,
655 				struct iwl_ucode_capabilities *capa,
656 				bool *usniffer_images)
657 {
658 	struct iwl_tlv_ucode_header *ucode = (void *)ucode_raw->data;
659 	const struct iwl_ucode_tlv *tlv;
660 	size_t len = ucode_raw->size;
661 	const u8 *data;
662 	u32 tlv_len;
663 	u32 usniffer_img;
664 	enum iwl_ucode_tlv_type tlv_type;
665 	const u8 *tlv_data;
666 	char buildstr[25];
667 	u32 build, paging_mem_size;
668 	int num_of_cpus;
669 	bool usniffer_req = false;
670 
671 	if (len < sizeof(*ucode)) {
672 		IWL_ERR(drv, "uCode has invalid length: %zd\n", len);
673 		return -EINVAL;
674 	}
675 
676 	if (ucode->magic != cpu_to_le32(IWL_TLV_UCODE_MAGIC)) {
677 		IWL_ERR(drv, "invalid uCode magic: 0X%x\n",
678 			le32_to_cpu(ucode->magic));
679 		return -EINVAL;
680 	}
681 
682 	drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
683 	memcpy(drv->fw.human_readable, ucode->human_readable,
684 	       sizeof(drv->fw.human_readable));
685 	build = le32_to_cpu(ucode->build);
686 
687 	if (build)
688 		sprintf(buildstr, " build %u", build);
689 	else
690 		buildstr[0] = '\0';
691 
692 	snprintf(drv->fw.fw_version,
693 		 sizeof(drv->fw.fw_version),
694 		 "%u.%u.%u.%u%s %s",
695 		 IWL_UCODE_MAJOR(drv->fw.ucode_ver),
696 		 IWL_UCODE_MINOR(drv->fw.ucode_ver),
697 		 IWL_UCODE_API(drv->fw.ucode_ver),
698 		 IWL_UCODE_SERIAL(drv->fw.ucode_ver),
699 		 buildstr, iwl_reduced_fw_name(drv));
700 
701 	data = ucode->data;
702 
703 	len -= sizeof(*ucode);
704 
705 	while (len >= sizeof(*tlv)) {
706 		len -= sizeof(*tlv);
707 		tlv = (void *)data;
708 
709 		tlv_len = le32_to_cpu(tlv->length);
710 		tlv_type = le32_to_cpu(tlv->type);
711 		tlv_data = tlv->data;
712 
713 		if (len < tlv_len) {
714 			IWL_ERR(drv, "invalid TLV len: %zd/%u\n",
715 				len, tlv_len);
716 			return -EINVAL;
717 		}
718 		len -= ALIGN(tlv_len, 4);
719 		data += sizeof(*tlv) + ALIGN(tlv_len, 4);
720 
721 		switch (tlv_type) {
722 		case IWL_UCODE_TLV_INST:
723 			set_sec_data(pieces, IWL_UCODE_REGULAR,
724 				     IWL_UCODE_SECTION_INST, tlv_data);
725 			set_sec_size(pieces, IWL_UCODE_REGULAR,
726 				     IWL_UCODE_SECTION_INST, tlv_len);
727 			set_sec_offset(pieces, IWL_UCODE_REGULAR,
728 				       IWL_UCODE_SECTION_INST,
729 				       IWLAGN_RTC_INST_LOWER_BOUND);
730 			break;
731 		case IWL_UCODE_TLV_DATA:
732 			set_sec_data(pieces, IWL_UCODE_REGULAR,
733 				     IWL_UCODE_SECTION_DATA, tlv_data);
734 			set_sec_size(pieces, IWL_UCODE_REGULAR,
735 				     IWL_UCODE_SECTION_DATA, tlv_len);
736 			set_sec_offset(pieces, IWL_UCODE_REGULAR,
737 				       IWL_UCODE_SECTION_DATA,
738 				       IWLAGN_RTC_DATA_LOWER_BOUND);
739 			break;
740 		case IWL_UCODE_TLV_INIT:
741 			set_sec_data(pieces, IWL_UCODE_INIT,
742 				     IWL_UCODE_SECTION_INST, tlv_data);
743 			set_sec_size(pieces, IWL_UCODE_INIT,
744 				     IWL_UCODE_SECTION_INST, tlv_len);
745 			set_sec_offset(pieces, IWL_UCODE_INIT,
746 				       IWL_UCODE_SECTION_INST,
747 				       IWLAGN_RTC_INST_LOWER_BOUND);
748 			break;
749 		case IWL_UCODE_TLV_INIT_DATA:
750 			set_sec_data(pieces, IWL_UCODE_INIT,
751 				     IWL_UCODE_SECTION_DATA, tlv_data);
752 			set_sec_size(pieces, IWL_UCODE_INIT,
753 				     IWL_UCODE_SECTION_DATA, tlv_len);
754 			set_sec_offset(pieces, IWL_UCODE_INIT,
755 				       IWL_UCODE_SECTION_DATA,
756 				       IWLAGN_RTC_DATA_LOWER_BOUND);
757 			break;
758 		case IWL_UCODE_TLV_BOOT:
759 			IWL_ERR(drv, "Found unexpected BOOT ucode\n");
760 			break;
761 		case IWL_UCODE_TLV_PROBE_MAX_LEN:
762 			if (tlv_len != sizeof(u32))
763 				goto invalid_tlv_len;
764 			capa->max_probe_length =
765 					le32_to_cpup((__le32 *)tlv_data);
766 			break;
767 		case IWL_UCODE_TLV_PAN:
768 			if (tlv_len)
769 				goto invalid_tlv_len;
770 			capa->flags |= IWL_UCODE_TLV_FLAGS_PAN;
771 			break;
772 		case IWL_UCODE_TLV_FLAGS:
773 			/* must be at least one u32 */
774 			if (tlv_len < sizeof(u32))
775 				goto invalid_tlv_len;
776 			/* and a proper number of u32s */
777 			if (tlv_len % sizeof(u32))
778 				goto invalid_tlv_len;
779 			/*
780 			 * This driver only reads the first u32 as
781 			 * right now no more features are defined,
782 			 * if that changes then either the driver
783 			 * will not work with the new firmware, or
784 			 * it'll not take advantage of new features.
785 			 */
786 			capa->flags = le32_to_cpup((__le32 *)tlv_data);
787 			break;
788 		case IWL_UCODE_TLV_API_CHANGES_SET:
789 			if (tlv_len != sizeof(struct iwl_ucode_api))
790 				goto invalid_tlv_len;
791 			iwl_set_ucode_api_flags(drv, tlv_data, capa);
792 			break;
793 		case IWL_UCODE_TLV_ENABLED_CAPABILITIES:
794 			if (tlv_len != sizeof(struct iwl_ucode_capa))
795 				goto invalid_tlv_len;
796 			iwl_set_ucode_capabilities(drv, tlv_data, capa);
797 			break;
798 		case IWL_UCODE_TLV_INIT_EVTLOG_PTR:
799 			if (tlv_len != sizeof(u32))
800 				goto invalid_tlv_len;
801 			pieces->init_evtlog_ptr =
802 					le32_to_cpup((__le32 *)tlv_data);
803 			break;
804 		case IWL_UCODE_TLV_INIT_EVTLOG_SIZE:
805 			if (tlv_len != sizeof(u32))
806 				goto invalid_tlv_len;
807 			pieces->init_evtlog_size =
808 					le32_to_cpup((__le32 *)tlv_data);
809 			break;
810 		case IWL_UCODE_TLV_INIT_ERRLOG_PTR:
811 			if (tlv_len != sizeof(u32))
812 				goto invalid_tlv_len;
813 			pieces->init_errlog_ptr =
814 					le32_to_cpup((__le32 *)tlv_data);
815 			break;
816 		case IWL_UCODE_TLV_RUNT_EVTLOG_PTR:
817 			if (tlv_len != sizeof(u32))
818 				goto invalid_tlv_len;
819 			pieces->inst_evtlog_ptr =
820 					le32_to_cpup((__le32 *)tlv_data);
821 			break;
822 		case IWL_UCODE_TLV_RUNT_EVTLOG_SIZE:
823 			if (tlv_len != sizeof(u32))
824 				goto invalid_tlv_len;
825 			pieces->inst_evtlog_size =
826 					le32_to_cpup((__le32 *)tlv_data);
827 			break;
828 		case IWL_UCODE_TLV_RUNT_ERRLOG_PTR:
829 			if (tlv_len != sizeof(u32))
830 				goto invalid_tlv_len;
831 			pieces->inst_errlog_ptr =
832 					le32_to_cpup((__le32 *)tlv_data);
833 			break;
834 		case IWL_UCODE_TLV_ENHANCE_SENS_TBL:
835 			if (tlv_len)
836 				goto invalid_tlv_len;
837 			drv->fw.enhance_sensitivity_table = true;
838 			break;
839 		case IWL_UCODE_TLV_WOWLAN_INST:
840 			set_sec_data(pieces, IWL_UCODE_WOWLAN,
841 				     IWL_UCODE_SECTION_INST, tlv_data);
842 			set_sec_size(pieces, IWL_UCODE_WOWLAN,
843 				     IWL_UCODE_SECTION_INST, tlv_len);
844 			set_sec_offset(pieces, IWL_UCODE_WOWLAN,
845 				       IWL_UCODE_SECTION_INST,
846 				       IWLAGN_RTC_INST_LOWER_BOUND);
847 			break;
848 		case IWL_UCODE_TLV_WOWLAN_DATA:
849 			set_sec_data(pieces, IWL_UCODE_WOWLAN,
850 				     IWL_UCODE_SECTION_DATA, tlv_data);
851 			set_sec_size(pieces, IWL_UCODE_WOWLAN,
852 				     IWL_UCODE_SECTION_DATA, tlv_len);
853 			set_sec_offset(pieces, IWL_UCODE_WOWLAN,
854 				       IWL_UCODE_SECTION_DATA,
855 				       IWLAGN_RTC_DATA_LOWER_BOUND);
856 			break;
857 		case IWL_UCODE_TLV_PHY_CALIBRATION_SIZE:
858 			if (tlv_len != sizeof(u32))
859 				goto invalid_tlv_len;
860 			capa->standard_phy_calibration_size =
861 					le32_to_cpup((__le32 *)tlv_data);
862 			break;
863 		case IWL_UCODE_TLV_SEC_RT:
864 			iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_REGULAR,
865 					    tlv_len);
866 			drv->fw.type = IWL_FW_MVM;
867 			break;
868 		case IWL_UCODE_TLV_SEC_INIT:
869 			iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_INIT,
870 					    tlv_len);
871 			drv->fw.type = IWL_FW_MVM;
872 			break;
873 		case IWL_UCODE_TLV_SEC_WOWLAN:
874 			iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_WOWLAN,
875 					    tlv_len);
876 			drv->fw.type = IWL_FW_MVM;
877 			break;
878 		case IWL_UCODE_TLV_DEF_CALIB:
879 			if (tlv_len != sizeof(struct iwl_tlv_calib_data))
880 				goto invalid_tlv_len;
881 			if (iwl_set_default_calib(drv, tlv_data))
882 				goto tlv_error;
883 			break;
884 		case IWL_UCODE_TLV_PHY_SKU:
885 			if (tlv_len != sizeof(u32))
886 				goto invalid_tlv_len;
887 			drv->fw.phy_config = le32_to_cpup((__le32 *)tlv_data);
888 			drv->fw.valid_tx_ant = (drv->fw.phy_config &
889 						FW_PHY_CFG_TX_CHAIN) >>
890 						FW_PHY_CFG_TX_CHAIN_POS;
891 			drv->fw.valid_rx_ant = (drv->fw.phy_config &
892 						FW_PHY_CFG_RX_CHAIN) >>
893 						FW_PHY_CFG_RX_CHAIN_POS;
894 			break;
895 		case IWL_UCODE_TLV_SECURE_SEC_RT:
896 			iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_REGULAR,
897 					    tlv_len);
898 			drv->fw.type = IWL_FW_MVM;
899 			break;
900 		case IWL_UCODE_TLV_SECURE_SEC_INIT:
901 			iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_INIT,
902 					    tlv_len);
903 			drv->fw.type = IWL_FW_MVM;
904 			break;
905 		case IWL_UCODE_TLV_SECURE_SEC_WOWLAN:
906 			iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_WOWLAN,
907 					    tlv_len);
908 			drv->fw.type = IWL_FW_MVM;
909 			break;
910 		case IWL_UCODE_TLV_NUM_OF_CPU:
911 			if (tlv_len != sizeof(u32))
912 				goto invalid_tlv_len;
913 			num_of_cpus =
914 				le32_to_cpup((__le32 *)tlv_data);
915 
916 			if (num_of_cpus == 2) {
917 				drv->fw.img[IWL_UCODE_REGULAR].is_dual_cpus =
918 					true;
919 				drv->fw.img[IWL_UCODE_INIT].is_dual_cpus =
920 					true;
921 				drv->fw.img[IWL_UCODE_WOWLAN].is_dual_cpus =
922 					true;
923 			} else if ((num_of_cpus > 2) || (num_of_cpus < 1)) {
924 				IWL_ERR(drv, "Driver support upto 2 CPUs\n");
925 				return -EINVAL;
926 			}
927 			break;
928 		case IWL_UCODE_TLV_CSCHEME:
929 			if (iwl_store_cscheme(&drv->fw, tlv_data, tlv_len))
930 				goto invalid_tlv_len;
931 			break;
932 		case IWL_UCODE_TLV_N_SCAN_CHANNELS:
933 			if (tlv_len != sizeof(u32))
934 				goto invalid_tlv_len;
935 			capa->n_scan_channels =
936 				le32_to_cpup((__le32 *)tlv_data);
937 			break;
938 		case IWL_UCODE_TLV_FW_VERSION: {
939 			__le32 *ptr = (void *)tlv_data;
940 			u32 major, minor;
941 			u8 local_comp;
942 
943 			if (tlv_len != sizeof(u32) * 3)
944 				goto invalid_tlv_len;
945 
946 			major = le32_to_cpup(ptr++);
947 			minor = le32_to_cpup(ptr++);
948 			local_comp = le32_to_cpup(ptr);
949 
950 			if (major >= 35)
951 				snprintf(drv->fw.fw_version,
952 					 sizeof(drv->fw.fw_version),
953 					"%u.%08x.%u %s", major, minor,
954 					local_comp, iwl_reduced_fw_name(drv));
955 			else
956 				snprintf(drv->fw.fw_version,
957 					 sizeof(drv->fw.fw_version),
958 					"%u.%u.%u %s", major, minor,
959 					local_comp, iwl_reduced_fw_name(drv));
960 			break;
961 			}
962 		case IWL_UCODE_TLV_FW_DBG_DEST: {
963 			struct iwl_fw_dbg_dest_tlv *dest = NULL;
964 			struct iwl_fw_dbg_dest_tlv_v1 *dest_v1 = NULL;
965 			u8 mon_mode;
966 
967 			pieces->dbg_dest_ver = (u8 *)tlv_data;
968 			if (*pieces->dbg_dest_ver == 1) {
969 				dest = (void *)tlv_data;
970 			} else if (*pieces->dbg_dest_ver == 0) {
971 				dest_v1 = (void *)tlv_data;
972 			} else {
973 				IWL_ERR(drv,
974 					"The version is %d, and it is invalid\n",
975 					*pieces->dbg_dest_ver);
976 				break;
977 			}
978 
979 			if (pieces->dbg_dest_tlv_init) {
980 				IWL_ERR(drv,
981 					"dbg destination ignored, already exists\n");
982 				break;
983 			}
984 
985 			pieces->dbg_dest_tlv_init = true;
986 
987 			if (dest_v1) {
988 				pieces->dbg_dest_tlv_v1 = dest_v1;
989 				mon_mode = dest_v1->monitor_mode;
990 			} else {
991 				pieces->dbg_dest_tlv = dest;
992 				mon_mode = dest->monitor_mode;
993 			}
994 
995 			IWL_INFO(drv, "Found debug destination: %s\n",
996 				 get_fw_dbg_mode_string(mon_mode));
997 
998 			drv->fw.dbg.n_dest_reg = (dest_v1) ?
999 				tlv_len -
1000 				offsetof(struct iwl_fw_dbg_dest_tlv_v1,
1001 					 reg_ops) :
1002 				tlv_len -
1003 				offsetof(struct iwl_fw_dbg_dest_tlv,
1004 					 reg_ops);
1005 
1006 			drv->fw.dbg.n_dest_reg /=
1007 				sizeof(drv->fw.dbg.dest_tlv->reg_ops[0]);
1008 
1009 			break;
1010 			}
1011 		case IWL_UCODE_TLV_FW_DBG_CONF: {
1012 			struct iwl_fw_dbg_conf_tlv *conf = (void *)tlv_data;
1013 
1014 			if (!pieces->dbg_dest_tlv_init) {
1015 				IWL_ERR(drv,
1016 					"Ignore dbg config %d - no destination configured\n",
1017 					conf->id);
1018 				break;
1019 			}
1020 
1021 			if (conf->id >= ARRAY_SIZE(drv->fw.dbg.conf_tlv)) {
1022 				IWL_ERR(drv,
1023 					"Skip unknown configuration: %d\n",
1024 					conf->id);
1025 				break;
1026 			}
1027 
1028 			if (pieces->dbg_conf_tlv[conf->id]) {
1029 				IWL_ERR(drv,
1030 					"Ignore duplicate dbg config %d\n",
1031 					conf->id);
1032 				break;
1033 			}
1034 
1035 			if (conf->usniffer)
1036 				usniffer_req = true;
1037 
1038 			IWL_INFO(drv, "Found debug configuration: %d\n",
1039 				 conf->id);
1040 
1041 			pieces->dbg_conf_tlv[conf->id] = conf;
1042 			pieces->dbg_conf_tlv_len[conf->id] = tlv_len;
1043 			break;
1044 			}
1045 		case IWL_UCODE_TLV_FW_DBG_TRIGGER: {
1046 			struct iwl_fw_dbg_trigger_tlv *trigger =
1047 				(void *)tlv_data;
1048 			u32 trigger_id = le32_to_cpu(trigger->id);
1049 
1050 			if (trigger_id >= ARRAY_SIZE(drv->fw.dbg.trigger_tlv)) {
1051 				IWL_ERR(drv,
1052 					"Skip unknown trigger: %u\n",
1053 					trigger->id);
1054 				break;
1055 			}
1056 
1057 			if (pieces->dbg_trigger_tlv[trigger_id]) {
1058 				IWL_ERR(drv,
1059 					"Ignore duplicate dbg trigger %u\n",
1060 					trigger->id);
1061 				break;
1062 			}
1063 
1064 			IWL_INFO(drv, "Found debug trigger: %u\n", trigger->id);
1065 
1066 			pieces->dbg_trigger_tlv[trigger_id] = trigger;
1067 			pieces->dbg_trigger_tlv_len[trigger_id] = tlv_len;
1068 			break;
1069 			}
1070 		case IWL_UCODE_TLV_FW_DBG_DUMP_LST: {
1071 			if (tlv_len != sizeof(u32)) {
1072 				IWL_ERR(drv,
1073 					"dbg lst mask size incorrect, skip\n");
1074 				break;
1075 			}
1076 
1077 			drv->fw.dbg.dump_mask =
1078 				le32_to_cpup((__le32 *)tlv_data);
1079 			break;
1080 			}
1081 		case IWL_UCODE_TLV_SEC_RT_USNIFFER:
1082 			*usniffer_images = true;
1083 			iwl_store_ucode_sec(pieces, tlv_data,
1084 					    IWL_UCODE_REGULAR_USNIFFER,
1085 					    tlv_len);
1086 			break;
1087 		case IWL_UCODE_TLV_PAGING:
1088 			if (tlv_len != sizeof(u32))
1089 				goto invalid_tlv_len;
1090 			paging_mem_size = le32_to_cpup((__le32 *)tlv_data);
1091 
1092 			IWL_DEBUG_FW(drv,
1093 				     "Paging: paging enabled (size = %u bytes)\n",
1094 				     paging_mem_size);
1095 
1096 			if (paging_mem_size > MAX_PAGING_IMAGE_SIZE) {
1097 				IWL_ERR(drv,
1098 					"Paging: driver supports up to %lu bytes for paging image\n",
1099 					MAX_PAGING_IMAGE_SIZE);
1100 				return -EINVAL;
1101 			}
1102 
1103 			if (paging_mem_size & (FW_PAGING_SIZE - 1)) {
1104 				IWL_ERR(drv,
1105 					"Paging: image isn't multiple %lu\n",
1106 					FW_PAGING_SIZE);
1107 				return -EINVAL;
1108 			}
1109 
1110 			drv->fw.img[IWL_UCODE_REGULAR].paging_mem_size =
1111 				paging_mem_size;
1112 			usniffer_img = IWL_UCODE_REGULAR_USNIFFER;
1113 			drv->fw.img[usniffer_img].paging_mem_size =
1114 				paging_mem_size;
1115 			break;
1116 		case IWL_UCODE_TLV_FW_GSCAN_CAPA:
1117 			/* ignored */
1118 			break;
1119 		case IWL_UCODE_TLV_FW_MEM_SEG: {
1120 			struct iwl_fw_dbg_mem_seg_tlv *dbg_mem =
1121 				(void *)tlv_data;
1122 			size_t size;
1123 			struct iwl_fw_dbg_mem_seg_tlv *n;
1124 
1125 			if (tlv_len != (sizeof(*dbg_mem)))
1126 				goto invalid_tlv_len;
1127 
1128 			IWL_DEBUG_INFO(drv, "Found debug memory segment: %u\n",
1129 				       dbg_mem->data_type);
1130 
1131 			size = sizeof(*pieces->dbg_mem_tlv) *
1132 			       (pieces->n_mem_tlv + 1);
1133 			n = krealloc(pieces->dbg_mem_tlv, size, GFP_KERNEL);
1134 			if (!n)
1135 				return -ENOMEM;
1136 			pieces->dbg_mem_tlv = n;
1137 			pieces->dbg_mem_tlv[pieces->n_mem_tlv] = *dbg_mem;
1138 			pieces->n_mem_tlv++;
1139 			break;
1140 			}
1141 		case IWL_UCODE_TLV_IML: {
1142 			drv->fw.iml_len = tlv_len;
1143 			drv->fw.iml = kmemdup(tlv_data, tlv_len, GFP_KERNEL);
1144 			if (!drv->fw.iml)
1145 				return -ENOMEM;
1146 			break;
1147 			}
1148 		case IWL_UCODE_TLV_FW_RECOVERY_INFO: {
1149 			struct {
1150 				__le32 buf_addr;
1151 				__le32 buf_size;
1152 			} *recov_info = (void *)tlv_data;
1153 
1154 			if (tlv_len != sizeof(*recov_info))
1155 				goto invalid_tlv_len;
1156 			capa->error_log_addr =
1157 				le32_to_cpu(recov_info->buf_addr);
1158 			capa->error_log_size =
1159 				le32_to_cpu(recov_info->buf_size);
1160 			}
1161 			break;
1162 		case IWL_UCODE_TLV_FW_FSEQ_VERSION: {
1163 			struct {
1164 				u8 version[32];
1165 				u8 sha1[20];
1166 			} *fseq_ver = (void *)tlv_data;
1167 
1168 			if (tlv_len != sizeof(*fseq_ver))
1169 				goto invalid_tlv_len;
1170 			IWL_INFO(drv, "TLV_FW_FSEQ_VERSION: %s\n",
1171 				 fseq_ver->version);
1172 			}
1173 			break;
1174 		case IWL_UCODE_TLV_FW_NUM_STATIONS:
1175 			if (tlv_len != sizeof(u32))
1176 				goto invalid_tlv_len;
1177 			if (le32_to_cpup((__le32 *)tlv_data) >
1178 			    IWL_MVM_STATION_COUNT_MAX) {
1179 				IWL_ERR(drv,
1180 					"%d is an invalid number of station\n",
1181 					le32_to_cpup((__le32 *)tlv_data));
1182 				goto tlv_error;
1183 			}
1184 			capa->num_stations =
1185 				le32_to_cpup((__le32 *)tlv_data);
1186 			break;
1187 		case IWL_UCODE_TLV_UMAC_DEBUG_ADDRS: {
1188 			struct iwl_umac_debug_addrs *dbg_ptrs =
1189 				(void *)tlv_data;
1190 
1191 			if (tlv_len != sizeof(*dbg_ptrs))
1192 				goto invalid_tlv_len;
1193 			if (drv->trans->trans_cfg->device_family <
1194 			    IWL_DEVICE_FAMILY_22000)
1195 				break;
1196 			drv->trans->dbg.umac_error_event_table =
1197 				le32_to_cpu(dbg_ptrs->error_info_addr) &
1198 				~FW_ADDR_CACHE_CONTROL;
1199 			drv->trans->dbg.error_event_table_tlv_status |=
1200 				IWL_ERROR_EVENT_TABLE_UMAC;
1201 			break;
1202 			}
1203 		case IWL_UCODE_TLV_LMAC_DEBUG_ADDRS: {
1204 			struct iwl_lmac_debug_addrs *dbg_ptrs =
1205 				(void *)tlv_data;
1206 
1207 			if (tlv_len != sizeof(*dbg_ptrs))
1208 				goto invalid_tlv_len;
1209 			if (drv->trans->trans_cfg->device_family <
1210 			    IWL_DEVICE_FAMILY_22000)
1211 				break;
1212 			drv->trans->dbg.lmac_error_event_table[0] =
1213 				le32_to_cpu(dbg_ptrs->error_event_table_ptr) &
1214 				~FW_ADDR_CACHE_CONTROL;
1215 			drv->trans->dbg.error_event_table_tlv_status |=
1216 				IWL_ERROR_EVENT_TABLE_LMAC1;
1217 			break;
1218 			}
1219 		case IWL_UCODE_TLV_TYPE_REGIONS:
1220 			iwl_parse_dbg_tlv_assert_tables(drv, tlv);
1221 			fallthrough;
1222 		case IWL_UCODE_TLV_TYPE_DEBUG_INFO:
1223 		case IWL_UCODE_TLV_TYPE_BUFFER_ALLOCATION:
1224 		case IWL_UCODE_TLV_TYPE_HCMD:
1225 		case IWL_UCODE_TLV_TYPE_TRIGGERS:
1226 		case IWL_UCODE_TLV_TYPE_CONF_SET:
1227 			if (iwlwifi_mod_params.enable_ini)
1228 				iwl_dbg_tlv_alloc(drv->trans, tlv, false);
1229 			break;
1230 		case IWL_UCODE_TLV_CMD_VERSIONS:
1231 			if (tlv_len % sizeof(struct iwl_fw_cmd_version)) {
1232 				IWL_ERR(drv,
1233 					"Invalid length for command versions: %u\n",
1234 					tlv_len);
1235 				tlv_len /= sizeof(struct iwl_fw_cmd_version);
1236 				tlv_len *= sizeof(struct iwl_fw_cmd_version);
1237 			}
1238 			if (WARN_ON(capa->cmd_versions))
1239 				return -EINVAL;
1240 			capa->cmd_versions = kmemdup(tlv_data, tlv_len,
1241 						     GFP_KERNEL);
1242 			if (!capa->cmd_versions)
1243 				return -ENOMEM;
1244 			capa->n_cmd_versions =
1245 				tlv_len / sizeof(struct iwl_fw_cmd_version);
1246 			break;
1247 		case IWL_UCODE_TLV_PHY_INTEGRATION_VERSION:
1248 			if (drv->fw.phy_integration_ver) {
1249 				IWL_ERR(drv,
1250 					"phy integration str ignored, already exists\n");
1251 				break;
1252 			}
1253 
1254 			drv->fw.phy_integration_ver =
1255 				kmemdup(tlv_data, tlv_len, GFP_KERNEL);
1256 			if (!drv->fw.phy_integration_ver)
1257 				return -ENOMEM;
1258 			drv->fw.phy_integration_ver_len = tlv_len;
1259 			break;
1260 		case IWL_UCODE_TLV_SEC_TABLE_ADDR:
1261 		case IWL_UCODE_TLV_D3_KEK_KCK_ADDR:
1262 			iwl_drv_set_dump_exclude(drv, tlv_type,
1263 						 tlv_data, tlv_len);
1264 			break;
1265 		default:
1266 			IWL_DEBUG_INFO(drv, "unknown TLV: %d\n", tlv_type);
1267 			break;
1268 		}
1269 	}
1270 
1271 	if (!fw_has_capa(capa, IWL_UCODE_TLV_CAPA_USNIFFER_UNIFIED) &&
1272 	    usniffer_req && !*usniffer_images) {
1273 		IWL_ERR(drv,
1274 			"user selected to work with usniffer but usniffer image isn't available in ucode package\n");
1275 		return -EINVAL;
1276 	}
1277 
1278 	if (len) {
1279 		IWL_ERR(drv, "invalid TLV after parsing: %zd\n", len);
1280 		iwl_print_hex_dump(drv, IWL_DL_FW, (u8 *)data, len);
1281 		return -EINVAL;
1282 	}
1283 
1284 	return 0;
1285 
1286  invalid_tlv_len:
1287 	IWL_ERR(drv, "TLV %d has invalid size: %u\n", tlv_type, tlv_len);
1288  tlv_error:
1289 	iwl_print_hex_dump(drv, IWL_DL_FW, tlv_data, tlv_len);
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 	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 = (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 	mutex_lock(&iwlwifi_opmode_table_mtx);
1649 	switch (fw->type) {
1650 	case IWL_FW_DVM:
1651 		op = &iwlwifi_opmode_table[DVM_OP_MODE];
1652 		break;
1653 	default:
1654 		WARN(1, "Invalid fw type %d\n", fw->type);
1655 		fallthrough;
1656 	case IWL_FW_MVM:
1657 		op = &iwlwifi_opmode_table[MVM_OP_MODE];
1658 		break;
1659 	}
1660 
1661 	IWL_INFO(drv, "loaded firmware version %s op_mode %s\n",
1662 		 drv->fw.fw_version, op->name);
1663 
1664 	iwl_dbg_tlv_load_bin(drv->trans->dev, drv->trans);
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 	return drv;
1759 
1760 err_fw:
1761 #ifdef CONFIG_IWLWIFI_DEBUGFS
1762 	debugfs_remove_recursive(drv->dbgfs_drv);
1763 	iwl_dbg_tlv_free(drv->trans);
1764 #endif
1765 	kfree(drv);
1766 err:
1767 	return ERR_PTR(ret);
1768 }
1769 
1770 void iwl_drv_stop(struct iwl_drv *drv)
1771 {
1772 	wait_for_completion(&drv->request_firmware_complete);
1773 
1774 	_iwl_op_mode_stop(drv);
1775 
1776 	iwl_dealloc_ucode(drv);
1777 
1778 	mutex_lock(&iwlwifi_opmode_table_mtx);
1779 	/*
1780 	 * List is empty (this item wasn't added)
1781 	 * when firmware loading failed -- in that
1782 	 * case we can't remove it from any list.
1783 	 */
1784 	if (!list_empty(&drv->list))
1785 		list_del(&drv->list);
1786 	mutex_unlock(&iwlwifi_opmode_table_mtx);
1787 
1788 #ifdef CONFIG_IWLWIFI_DEBUGFS
1789 	drv->trans->ops->debugfs_cleanup(drv->trans);
1790 
1791 	debugfs_remove_recursive(drv->dbgfs_drv);
1792 #endif
1793 
1794 	iwl_dbg_tlv_free(drv->trans);
1795 
1796 	kfree(drv);
1797 }
1798 
1799 
1800 /* shared module parameters */
1801 struct iwl_mod_params iwlwifi_mod_params = {
1802 	.fw_restart = true,
1803 	.bt_coex_active = true,
1804 	.power_level = IWL_POWER_INDEX_1,
1805 	.uapsd_disable = IWL_DISABLE_UAPSD_BSS | IWL_DISABLE_UAPSD_P2P_CLIENT,
1806 	.enable_ini = true,
1807 	/* the rest are 0 by default */
1808 };
1809 IWL_EXPORT_SYMBOL(iwlwifi_mod_params);
1810 
1811 int iwl_opmode_register(const char *name, const struct iwl_op_mode_ops *ops)
1812 {
1813 	int i;
1814 	struct iwl_drv *drv;
1815 	struct iwlwifi_opmode_table *op;
1816 
1817 	mutex_lock(&iwlwifi_opmode_table_mtx);
1818 	for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++) {
1819 		op = &iwlwifi_opmode_table[i];
1820 		if (strcmp(op->name, name))
1821 			continue;
1822 		op->ops = ops;
1823 		/* TODO: need to handle exceptional case */
1824 		list_for_each_entry(drv, &op->drv, list)
1825 			drv->op_mode = _iwl_op_mode_start(drv, op);
1826 
1827 		mutex_unlock(&iwlwifi_opmode_table_mtx);
1828 		return 0;
1829 	}
1830 	mutex_unlock(&iwlwifi_opmode_table_mtx);
1831 	return -EIO;
1832 }
1833 IWL_EXPORT_SYMBOL(iwl_opmode_register);
1834 
1835 void iwl_opmode_deregister(const char *name)
1836 {
1837 	int i;
1838 	struct iwl_drv *drv;
1839 
1840 	mutex_lock(&iwlwifi_opmode_table_mtx);
1841 	for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++) {
1842 		if (strcmp(iwlwifi_opmode_table[i].name, name))
1843 			continue;
1844 		iwlwifi_opmode_table[i].ops = NULL;
1845 
1846 		/* call the stop routine for all devices */
1847 		list_for_each_entry(drv, &iwlwifi_opmode_table[i].drv, list)
1848 			_iwl_op_mode_stop(drv);
1849 
1850 		mutex_unlock(&iwlwifi_opmode_table_mtx);
1851 		return;
1852 	}
1853 	mutex_unlock(&iwlwifi_opmode_table_mtx);
1854 }
1855 IWL_EXPORT_SYMBOL(iwl_opmode_deregister);
1856 
1857 static int __init iwl_drv_init(void)
1858 {
1859 	int i, err;
1860 
1861 	for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++)
1862 		INIT_LIST_HEAD(&iwlwifi_opmode_table[i].drv);
1863 
1864 	pr_info(DRV_DESCRIPTION "\n");
1865 
1866 #ifdef CONFIG_IWLWIFI_DEBUGFS
1867 	/* Create the root of iwlwifi debugfs subsystem. */
1868 	iwl_dbgfs_root = debugfs_create_dir(DRV_NAME, NULL);
1869 #endif
1870 
1871 	err = iwl_pci_register_driver();
1872 	if (err)
1873 		goto cleanup_debugfs;
1874 
1875 	return 0;
1876 
1877 cleanup_debugfs:
1878 #ifdef CONFIG_IWLWIFI_DEBUGFS
1879 	debugfs_remove_recursive(iwl_dbgfs_root);
1880 #endif
1881 	return err;
1882 }
1883 module_init(iwl_drv_init);
1884 
1885 static void __exit iwl_drv_exit(void)
1886 {
1887 	iwl_pci_unregister_driver();
1888 
1889 #ifdef CONFIG_IWLWIFI_DEBUGFS
1890 	debugfs_remove_recursive(iwl_dbgfs_root);
1891 #endif
1892 }
1893 module_exit(iwl_drv_exit);
1894 
1895 #ifdef CONFIG_IWLWIFI_DEBUG
1896 module_param_named(debug, iwlwifi_mod_params.debug_level, uint, 0644);
1897 MODULE_PARM_DESC(debug, "debug output mask");
1898 #endif
1899 
1900 module_param_named(swcrypto, iwlwifi_mod_params.swcrypto, int, 0444);
1901 MODULE_PARM_DESC(swcrypto, "using crypto in software (default 0 [hardware])");
1902 module_param_named(11n_disable, iwlwifi_mod_params.disable_11n, uint, 0444);
1903 MODULE_PARM_DESC(11n_disable,
1904 	"disable 11n functionality, bitmap: 1: full, 2: disable agg TX, 4: disable agg RX, 8 enable agg TX");
1905 module_param_named(amsdu_size, iwlwifi_mod_params.amsdu_size, int, 0444);
1906 MODULE_PARM_DESC(amsdu_size,
1907 		 "amsdu size 0: 12K for multi Rx queue devices, 2K for AX210 devices, "
1908 		 "4K for other devices 1:4K 2:8K 3:12K (16K buffers) 4: 2K (default 0)");
1909 module_param_named(fw_restart, iwlwifi_mod_params.fw_restart, bool, 0444);
1910 MODULE_PARM_DESC(fw_restart, "restart firmware in case of error (default true)");
1911 
1912 module_param_named(nvm_file, iwlwifi_mod_params.nvm_file, charp, 0444);
1913 MODULE_PARM_DESC(nvm_file, "NVM file name");
1914 
1915 module_param_named(uapsd_disable, iwlwifi_mod_params.uapsd_disable, uint, 0644);
1916 MODULE_PARM_DESC(uapsd_disable,
1917 		 "disable U-APSD functionality bitmap 1: BSS 2: P2P Client (default: 3)");
1918 module_param_named(enable_ini, iwlwifi_mod_params.enable_ini,
1919 		   bool, S_IRUGO | S_IWUSR);
1920 MODULE_PARM_DESC(enable_ini,
1921 		 "Enable debug INI TLV FW debug infrastructure (default: true");
1922 
1923 /*
1924  * set bt_coex_active to true, uCode will do kill/defer
1925  * every time the priority line is asserted (BT is sending signals on the
1926  * priority line in the PCIx).
1927  * set bt_coex_active to false, uCode will ignore the BT activity and
1928  * perform the normal operation
1929  *
1930  * User might experience transmit issue on some platform due to WiFi/BT
1931  * co-exist problem. The possible behaviors are:
1932  *   Able to scan and finding all the available AP
1933  *   Not able to associate with any AP
1934  * On those platforms, WiFi communication can be restored by set
1935  * "bt_coex_active" module parameter to "false"
1936  *
1937  * default: bt_coex_active = true (BT_COEX_ENABLE)
1938  */
1939 module_param_named(bt_coex_active, iwlwifi_mod_params.bt_coex_active,
1940 		   bool, 0444);
1941 MODULE_PARM_DESC(bt_coex_active, "enable wifi/bt co-exist (default: enable)");
1942 
1943 module_param_named(led_mode, iwlwifi_mod_params.led_mode, int, 0444);
1944 MODULE_PARM_DESC(led_mode, "0=system default, "
1945 		"1=On(RF On)/Off(RF Off), 2=blinking, 3=Off (default: 0)");
1946 
1947 module_param_named(power_save, iwlwifi_mod_params.power_save, bool, 0444);
1948 MODULE_PARM_DESC(power_save,
1949 		 "enable WiFi power management (default: disable)");
1950 
1951 module_param_named(power_level, iwlwifi_mod_params.power_level, int, 0444);
1952 MODULE_PARM_DESC(power_level,
1953 		 "default power save level (range from 1 - 5, default: 1)");
1954 
1955 module_param_named(disable_11ac, iwlwifi_mod_params.disable_11ac, bool, 0444);
1956 MODULE_PARM_DESC(disable_11ac, "Disable VHT capabilities (default: false)");
1957 
1958 module_param_named(remove_when_gone,
1959 		   iwlwifi_mod_params.remove_when_gone, bool,
1960 		   0444);
1961 MODULE_PARM_DESC(remove_when_gone,
1962 		 "Remove dev from PCIe bus if it is deemed inaccessible (default: false)");
1963 
1964 module_param_named(disable_11ax, iwlwifi_mod_params.disable_11ax, bool,
1965 		   S_IRUGO);
1966 MODULE_PARM_DESC(disable_11ax, "Disable HE capabilities (default: false)");
1967