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