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