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