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