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