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