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