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