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