xref: /linux/drivers/net/wireless/ath/ath11k/core.c (revision 41fb0cf1bced59c1fe178cf6cc9f716b5da9e40e)
1 // SPDX-License-Identifier: BSD-3-Clause-Clear
2 /*
3  * Copyright (c) 2018-2019 The Linux Foundation. All rights reserved.
4  */
5 
6 #include <linux/module.h>
7 #include <linux/slab.h>
8 #include <linux/remoteproc.h>
9 #include <linux/firmware.h>
10 #include <linux/of.h>
11 #include "core.h"
12 #include "dp_tx.h"
13 #include "dp_rx.h"
14 #include "debug.h"
15 #include "hif.h"
16 #include "wow.h"
17 
18 unsigned int ath11k_debug_mask;
19 EXPORT_SYMBOL(ath11k_debug_mask);
20 module_param_named(debug_mask, ath11k_debug_mask, uint, 0644);
21 MODULE_PARM_DESC(debug_mask, "Debugging mask");
22 
23 static unsigned int ath11k_crypto_mode;
24 module_param_named(crypto_mode, ath11k_crypto_mode, uint, 0644);
25 MODULE_PARM_DESC(crypto_mode, "crypto mode: 0-hardware, 1-software");
26 
27 /* frame mode values are mapped as per enum ath11k_hw_txrx_mode */
28 unsigned int ath11k_frame_mode = ATH11K_HW_TXRX_NATIVE_WIFI;
29 module_param_named(frame_mode, ath11k_frame_mode, uint, 0644);
30 MODULE_PARM_DESC(frame_mode,
31 		 "Datapath frame mode (0: raw, 1: native wifi (default), 2: ethernet)");
32 
33 static const struct ath11k_hw_params ath11k_hw_params[] = {
34 	{
35 		.hw_rev = ATH11K_HW_IPQ8074,
36 		.name = "ipq8074 hw2.0",
37 		.fw = {
38 			.dir = "IPQ8074/hw2.0",
39 			.board_size = 256 * 1024,
40 			.cal_offset = 128 * 1024,
41 		},
42 		.max_radios = 3,
43 		.bdf_addr = 0x4B0C0000,
44 		.hw_ops = &ipq8074_ops,
45 		.ring_mask = &ath11k_hw_ring_mask_ipq8074,
46 		.internal_sleep_clock = false,
47 		.regs = &ipq8074_regs,
48 		.qmi_service_ins_id = ATH11K_QMI_WLFW_SERVICE_INS_ID_V01_IPQ8074,
49 		.host_ce_config = ath11k_host_ce_config_ipq8074,
50 		.ce_count = 12,
51 		.target_ce_config = ath11k_target_ce_config_wlan_ipq8074,
52 		.target_ce_count = 11,
53 		.svc_to_ce_map = ath11k_target_service_to_ce_map_wlan_ipq8074,
54 		.svc_to_ce_map_len = 21,
55 		.single_pdev_only = false,
56 		.rxdma1_enable = true,
57 		.num_rxmda_per_pdev = 1,
58 		.rx_mac_buf_ring = false,
59 		.vdev_start_delay = false,
60 		.htt_peer_map_v2 = true,
61 
62 		.spectral = {
63 			.fft_sz = 2,
64 			/* HW bug, expected BIN size is 2 bytes but HW report as 4 bytes.
65 			 * so added pad size as 2 bytes to compensate the BIN size
66 			 */
67 			.fft_pad_sz = 2,
68 			.summary_pad_sz = 0,
69 			.fft_hdr_len = 16,
70 			.max_fft_bins = 512,
71 		},
72 
73 		.interface_modes = BIT(NL80211_IFTYPE_STATION) |
74 					BIT(NL80211_IFTYPE_AP) |
75 					BIT(NL80211_IFTYPE_MESH_POINT),
76 		.supports_monitor = true,
77 		.supports_shadow_regs = false,
78 		.idle_ps = false,
79 		.supports_sta_ps = false,
80 		.cold_boot_calib = true,
81 		.supports_suspend = false,
82 		.hal_desc_sz = sizeof(struct hal_rx_desc_ipq8074),
83 		.fix_l1ss = true,
84 		.credit_flow = false,
85 		.max_tx_ring = DP_TCL_NUM_RING_MAX,
86 		.hal_params = &ath11k_hw_hal_params_ipq8074,
87 		.supports_dynamic_smps_6ghz = false,
88 		.alloc_cacheable_memory = true,
89 		.wakeup_mhi = false,
90 	},
91 	{
92 		.hw_rev = ATH11K_HW_IPQ6018_HW10,
93 		.name = "ipq6018 hw1.0",
94 		.fw = {
95 			.dir = "IPQ6018/hw1.0",
96 			.board_size = 256 * 1024,
97 			.cal_offset = 128 * 1024,
98 		},
99 		.max_radios = 2,
100 		.bdf_addr = 0x4ABC0000,
101 		.hw_ops = &ipq6018_ops,
102 		.ring_mask = &ath11k_hw_ring_mask_ipq8074,
103 		.internal_sleep_clock = false,
104 		.regs = &ipq8074_regs,
105 		.qmi_service_ins_id = ATH11K_QMI_WLFW_SERVICE_INS_ID_V01_IPQ8074,
106 		.host_ce_config = ath11k_host_ce_config_ipq8074,
107 		.ce_count = 12,
108 		.target_ce_config = ath11k_target_ce_config_wlan_ipq8074,
109 		.target_ce_count = 11,
110 		.svc_to_ce_map = ath11k_target_service_to_ce_map_wlan_ipq6018,
111 		.svc_to_ce_map_len = 19,
112 		.single_pdev_only = false,
113 		.rxdma1_enable = true,
114 		.num_rxmda_per_pdev = 1,
115 		.rx_mac_buf_ring = false,
116 		.vdev_start_delay = false,
117 		.htt_peer_map_v2 = true,
118 
119 		.spectral = {
120 			.fft_sz = 4,
121 			.fft_pad_sz = 0,
122 			.summary_pad_sz = 0,
123 			.fft_hdr_len = 16,
124 			.max_fft_bins = 512,
125 		},
126 
127 		.interface_modes = BIT(NL80211_IFTYPE_STATION) |
128 					BIT(NL80211_IFTYPE_AP) |
129 					BIT(NL80211_IFTYPE_MESH_POINT),
130 		.supports_monitor = true,
131 		.supports_shadow_regs = false,
132 		.idle_ps = false,
133 		.supports_sta_ps = false,
134 		.cold_boot_calib = true,
135 		.supports_suspend = false,
136 		.hal_desc_sz = sizeof(struct hal_rx_desc_ipq8074),
137 		.fix_l1ss = true,
138 		.credit_flow = false,
139 		.max_tx_ring = DP_TCL_NUM_RING_MAX,
140 		.hal_params = &ath11k_hw_hal_params_ipq8074,
141 		.supports_dynamic_smps_6ghz = false,
142 		.alloc_cacheable_memory = true,
143 		.wakeup_mhi = false,
144 	},
145 	{
146 		.name = "qca6390 hw2.0",
147 		.hw_rev = ATH11K_HW_QCA6390_HW20,
148 		.fw = {
149 			.dir = "QCA6390/hw2.0",
150 			.board_size = 256 * 1024,
151 			.cal_offset = 128 * 1024,
152 		},
153 		.max_radios = 3,
154 		.bdf_addr = 0x4B0C0000,
155 		.hw_ops = &qca6390_ops,
156 		.ring_mask = &ath11k_hw_ring_mask_qca6390,
157 		.internal_sleep_clock = true,
158 		.regs = &qca6390_regs,
159 		.qmi_service_ins_id = ATH11K_QMI_WLFW_SERVICE_INS_ID_V01_QCA6390,
160 		.host_ce_config = ath11k_host_ce_config_qca6390,
161 		.ce_count = 9,
162 		.target_ce_config = ath11k_target_ce_config_wlan_qca6390,
163 		.target_ce_count = 9,
164 		.svc_to_ce_map = ath11k_target_service_to_ce_map_wlan_qca6390,
165 		.svc_to_ce_map_len = 14,
166 		.single_pdev_only = true,
167 		.rxdma1_enable = false,
168 		.num_rxmda_per_pdev = 2,
169 		.rx_mac_buf_ring = true,
170 		.vdev_start_delay = true,
171 		.htt_peer_map_v2 = false,
172 
173 		.spectral = {
174 			.fft_sz = 0,
175 			.fft_pad_sz = 0,
176 			.summary_pad_sz = 0,
177 			.fft_hdr_len = 0,
178 			.max_fft_bins = 0,
179 		},
180 
181 		.interface_modes = BIT(NL80211_IFTYPE_STATION) |
182 					BIT(NL80211_IFTYPE_AP),
183 		.supports_monitor = false,
184 		.supports_shadow_regs = true,
185 		.idle_ps = true,
186 		.supports_sta_ps = true,
187 		.cold_boot_calib = false,
188 		.supports_suspend = true,
189 		.hal_desc_sz = sizeof(struct hal_rx_desc_ipq8074),
190 		.fix_l1ss = true,
191 		.credit_flow = true,
192 		.max_tx_ring = DP_TCL_NUM_RING_MAX_QCA6390,
193 		.hal_params = &ath11k_hw_hal_params_qca6390,
194 		.supports_dynamic_smps_6ghz = false,
195 		.alloc_cacheable_memory = false,
196 		.wakeup_mhi = true,
197 	},
198 	{
199 		.name = "qcn9074 hw1.0",
200 		.hw_rev = ATH11K_HW_QCN9074_HW10,
201 		.fw = {
202 			.dir = "QCN9074/hw1.0",
203 			.board_size = 256 * 1024,
204 			.cal_offset = 128 * 1024,
205 		},
206 		.max_radios = 1,
207 		.single_pdev_only = false,
208 		.qmi_service_ins_id = ATH11K_QMI_WLFW_SERVICE_INS_ID_V01_QCN9074,
209 		.hw_ops = &qcn9074_ops,
210 		.ring_mask = &ath11k_hw_ring_mask_qcn9074,
211 		.internal_sleep_clock = false,
212 		.regs = &qcn9074_regs,
213 		.host_ce_config = ath11k_host_ce_config_qcn9074,
214 		.ce_count = 6,
215 		.target_ce_config = ath11k_target_ce_config_wlan_qcn9074,
216 		.target_ce_count = 9,
217 		.svc_to_ce_map = ath11k_target_service_to_ce_map_wlan_qcn9074,
218 		.svc_to_ce_map_len = 18,
219 		.rxdma1_enable = true,
220 		.num_rxmda_per_pdev = 1,
221 		.rx_mac_buf_ring = false,
222 		.vdev_start_delay = false,
223 		.htt_peer_map_v2 = true,
224 
225 		.spectral = {
226 			.fft_sz = 2,
227 			.fft_pad_sz = 0,
228 			.summary_pad_sz = 16,
229 			.fft_hdr_len = 24,
230 			.max_fft_bins = 1024,
231 		},
232 
233 		.interface_modes = BIT(NL80211_IFTYPE_STATION) |
234 					BIT(NL80211_IFTYPE_AP) |
235 					BIT(NL80211_IFTYPE_MESH_POINT),
236 		.supports_monitor = true,
237 		.supports_shadow_regs = false,
238 		.idle_ps = false,
239 		.supports_sta_ps = false,
240 		.cold_boot_calib = false,
241 		.supports_suspend = false,
242 		.hal_desc_sz = sizeof(struct hal_rx_desc_qcn9074),
243 		.fix_l1ss = true,
244 		.credit_flow = false,
245 		.max_tx_ring = DP_TCL_NUM_RING_MAX,
246 		.hal_params = &ath11k_hw_hal_params_ipq8074,
247 		.supports_dynamic_smps_6ghz = true,
248 		.alloc_cacheable_memory = true,
249 		.wakeup_mhi = false,
250 	},
251 	{
252 		.name = "wcn6855 hw2.0",
253 		.hw_rev = ATH11K_HW_WCN6855_HW20,
254 		.fw = {
255 			.dir = "WCN6855/hw2.0",
256 			.board_size = 256 * 1024,
257 			.cal_offset = 128 * 1024,
258 		},
259 		.max_radios = 3,
260 		.bdf_addr = 0x4B0C0000,
261 		.hw_ops = &wcn6855_ops,
262 		.ring_mask = &ath11k_hw_ring_mask_qca6390,
263 		.internal_sleep_clock = true,
264 		.regs = &wcn6855_regs,
265 		.qmi_service_ins_id = ATH11K_QMI_WLFW_SERVICE_INS_ID_V01_QCA6390,
266 		.host_ce_config = ath11k_host_ce_config_qca6390,
267 		.ce_count = 9,
268 		.target_ce_config = ath11k_target_ce_config_wlan_qca6390,
269 		.target_ce_count = 9,
270 		.svc_to_ce_map = ath11k_target_service_to_ce_map_wlan_qca6390,
271 		.svc_to_ce_map_len = 14,
272 		.single_pdev_only = true,
273 		.rxdma1_enable = false,
274 		.num_rxmda_per_pdev = 2,
275 		.rx_mac_buf_ring = true,
276 		.vdev_start_delay = true,
277 		.htt_peer_map_v2 = false,
278 
279 		.spectral = {
280 			.fft_sz = 0,
281 			.fft_pad_sz = 0,
282 			.summary_pad_sz = 0,
283 			.fft_hdr_len = 0,
284 			.max_fft_bins = 0,
285 		},
286 
287 		.interface_modes = BIT(NL80211_IFTYPE_STATION) |
288 					BIT(NL80211_IFTYPE_AP),
289 		.supports_monitor = false,
290 		.supports_shadow_regs = true,
291 		.idle_ps = true,
292 		.supports_sta_ps = true,
293 		.cold_boot_calib = false,
294 		.supports_suspend = true,
295 		.hal_desc_sz = sizeof(struct hal_rx_desc_wcn6855),
296 		.fix_l1ss = false,
297 		.credit_flow = true,
298 		.max_tx_ring = DP_TCL_NUM_RING_MAX_QCA6390,
299 		.hal_params = &ath11k_hw_hal_params_qca6390,
300 		.supports_dynamic_smps_6ghz = false,
301 		.alloc_cacheable_memory = false,
302 		.wakeup_mhi = true,
303 	},
304 };
305 
306 int ath11k_core_suspend(struct ath11k_base *ab)
307 {
308 	int ret;
309 
310 	if (!ab->hw_params.supports_suspend)
311 		return -EOPNOTSUPP;
312 
313 	/* TODO: there can frames in queues so for now add delay as a hack.
314 	 * Need to implement to handle and remove this delay.
315 	 */
316 	msleep(500);
317 
318 	ret = ath11k_dp_rx_pktlog_stop(ab, true);
319 	if (ret) {
320 		ath11k_warn(ab, "failed to stop dp rx (and timer) pktlog during suspend: %d\n",
321 			    ret);
322 		return ret;
323 	}
324 
325 	ret = ath11k_wow_enable(ab);
326 	if (ret) {
327 		ath11k_warn(ab, "failed to enable wow during suspend: %d\n", ret);
328 		return ret;
329 	}
330 
331 	ret = ath11k_dp_rx_pktlog_stop(ab, false);
332 	if (ret) {
333 		ath11k_warn(ab, "failed to stop dp rx pktlog during suspend: %d\n",
334 			    ret);
335 		return ret;
336 	}
337 
338 	ath11k_ce_stop_shadow_timers(ab);
339 	ath11k_dp_stop_shadow_timers(ab);
340 
341 	ath11k_hif_irq_disable(ab);
342 	ath11k_hif_ce_irq_disable(ab);
343 
344 	ret = ath11k_hif_suspend(ab);
345 	if (ret) {
346 		ath11k_warn(ab, "failed to suspend hif: %d\n", ret);
347 		return ret;
348 	}
349 
350 	return 0;
351 }
352 EXPORT_SYMBOL(ath11k_core_suspend);
353 
354 int ath11k_core_resume(struct ath11k_base *ab)
355 {
356 	int ret;
357 
358 	if (!ab->hw_params.supports_suspend)
359 		return -EOPNOTSUPP;
360 
361 	ret = ath11k_hif_resume(ab);
362 	if (ret) {
363 		ath11k_warn(ab, "failed to resume hif during resume: %d\n", ret);
364 		return ret;
365 	}
366 
367 	ath11k_hif_ce_irq_enable(ab);
368 	ath11k_hif_irq_enable(ab);
369 
370 	ret = ath11k_dp_rx_pktlog_start(ab);
371 	if (ret) {
372 		ath11k_warn(ab, "failed to start rx pktlog during resume: %d\n",
373 			    ret);
374 		return ret;
375 	}
376 
377 	ret = ath11k_wow_wakeup(ab);
378 	if (ret) {
379 		ath11k_warn(ab, "failed to wakeup wow during resume: %d\n", ret);
380 		return ret;
381 	}
382 
383 	return 0;
384 }
385 EXPORT_SYMBOL(ath11k_core_resume);
386 
387 int ath11k_core_check_dt(struct ath11k_base *ab)
388 {
389 	size_t max_len = sizeof(ab->qmi.target.bdf_ext);
390 	const char *variant = NULL;
391 	struct device_node *node;
392 
393 	node = ab->dev->of_node;
394 	if (!node)
395 		return -ENOENT;
396 
397 	of_property_read_string(node, "qcom,ath11k-calibration-variant",
398 				&variant);
399 	if (!variant)
400 		return -ENODATA;
401 
402 	if (strscpy(ab->qmi.target.bdf_ext, variant, max_len) < 0)
403 		ath11k_dbg(ab, ATH11K_DBG_BOOT,
404 			   "bdf variant string is longer than the buffer can accommodate (variant: %s)\n",
405 			    variant);
406 
407 	return 0;
408 }
409 
410 static int ath11k_core_create_board_name(struct ath11k_base *ab, char *name,
411 					 size_t name_len)
412 {
413 	/* strlen(',variant=') + strlen(ab->qmi.target.bdf_ext) */
414 	char variant[9 + ATH11K_QMI_BDF_EXT_STR_LENGTH] = { 0 };
415 
416 	if (ab->qmi.target.bdf_ext[0] != '\0')
417 		scnprintf(variant, sizeof(variant), ",variant=%s",
418 			  ab->qmi.target.bdf_ext);
419 
420 	switch (ab->id.bdf_search) {
421 	case ATH11K_BDF_SEARCH_BUS_AND_BOARD:
422 		scnprintf(name, name_len,
423 			  "bus=%s,vendor=%04x,device=%04x,subsystem-vendor=%04x,subsystem-device=%04x,qmi-chip-id=%d,qmi-board-id=%d%s",
424 			  ath11k_bus_str(ab->hif.bus),
425 			  ab->id.vendor, ab->id.device,
426 			  ab->id.subsystem_vendor,
427 			  ab->id.subsystem_device,
428 			  ab->qmi.target.chip_id,
429 			  ab->qmi.target.board_id,
430 			  variant);
431 		break;
432 	default:
433 		scnprintf(name, name_len,
434 			  "bus=%s,qmi-chip-id=%d,qmi-board-id=%d%s",
435 			  ath11k_bus_str(ab->hif.bus),
436 			  ab->qmi.target.chip_id,
437 			  ab->qmi.target.board_id, variant);
438 		break;
439 	}
440 
441 	ath11k_dbg(ab, ATH11K_DBG_BOOT, "boot using board name '%s'\n", name);
442 
443 	return 0;
444 }
445 
446 const struct firmware *ath11k_core_firmware_request(struct ath11k_base *ab,
447 						    const char *file)
448 {
449 	const struct firmware *fw;
450 	char path[100];
451 	int ret;
452 
453 	if (file == NULL)
454 		return ERR_PTR(-ENOENT);
455 
456 	ath11k_core_create_firmware_path(ab, file, path, sizeof(path));
457 
458 	ret = firmware_request_nowarn(&fw, path, ab->dev);
459 	if (ret)
460 		return ERR_PTR(ret);
461 
462 	ath11k_dbg(ab, ATH11K_DBG_BOOT, "boot firmware request %s size %zu\n",
463 		   path, fw->size);
464 
465 	return fw;
466 }
467 
468 void ath11k_core_free_bdf(struct ath11k_base *ab, struct ath11k_board_data *bd)
469 {
470 	if (!IS_ERR(bd->fw))
471 		release_firmware(bd->fw);
472 
473 	memset(bd, 0, sizeof(*bd));
474 }
475 
476 static int ath11k_core_parse_bd_ie_board(struct ath11k_base *ab,
477 					 struct ath11k_board_data *bd,
478 					 const void *buf, size_t buf_len,
479 					 const char *boardname,
480 					 int bd_ie_type)
481 {
482 	const struct ath11k_fw_ie *hdr;
483 	bool name_match_found;
484 	int ret, board_ie_id;
485 	size_t board_ie_len;
486 	const void *board_ie_data;
487 
488 	name_match_found = false;
489 
490 	/* go through ATH11K_BD_IE_BOARD_ elements */
491 	while (buf_len > sizeof(struct ath11k_fw_ie)) {
492 		hdr = buf;
493 		board_ie_id = le32_to_cpu(hdr->id);
494 		board_ie_len = le32_to_cpu(hdr->len);
495 		board_ie_data = hdr->data;
496 
497 		buf_len -= sizeof(*hdr);
498 		buf += sizeof(*hdr);
499 
500 		if (buf_len < ALIGN(board_ie_len, 4)) {
501 			ath11k_err(ab, "invalid ATH11K_BD_IE_BOARD length: %zu < %zu\n",
502 				   buf_len, ALIGN(board_ie_len, 4));
503 			ret = -EINVAL;
504 			goto out;
505 		}
506 
507 		switch (board_ie_id) {
508 		case ATH11K_BD_IE_BOARD_NAME:
509 			ath11k_dbg_dump(ab, ATH11K_DBG_BOOT, "board name", "",
510 					board_ie_data, board_ie_len);
511 
512 			if (board_ie_len != strlen(boardname))
513 				break;
514 
515 			ret = memcmp(board_ie_data, boardname, strlen(boardname));
516 			if (ret)
517 				break;
518 
519 			name_match_found = true;
520 			ath11k_dbg(ab, ATH11K_DBG_BOOT,
521 				   "boot found match for name '%s'",
522 				   boardname);
523 			break;
524 		case ATH11K_BD_IE_BOARD_DATA:
525 			if (!name_match_found)
526 				/* no match found */
527 				break;
528 
529 			ath11k_dbg(ab, ATH11K_DBG_BOOT,
530 				   "boot found board data for '%s'", boardname);
531 
532 			bd->data = board_ie_data;
533 			bd->len = board_ie_len;
534 
535 			ret = 0;
536 			goto out;
537 		default:
538 			ath11k_warn(ab, "unknown ATH11K_BD_IE_BOARD found: %d\n",
539 				    board_ie_id);
540 			break;
541 		}
542 
543 		/* jump over the padding */
544 		board_ie_len = ALIGN(board_ie_len, 4);
545 
546 		buf_len -= board_ie_len;
547 		buf += board_ie_len;
548 	}
549 
550 	/* no match found */
551 	ret = -ENOENT;
552 
553 out:
554 	return ret;
555 }
556 
557 static int ath11k_core_fetch_board_data_api_n(struct ath11k_base *ab,
558 					      struct ath11k_board_data *bd,
559 					      const char *boardname)
560 {
561 	size_t len, magic_len;
562 	const u8 *data;
563 	char *filename, filepath[100];
564 	size_t ie_len;
565 	struct ath11k_fw_ie *hdr;
566 	int ret, ie_id;
567 
568 	filename = ATH11K_BOARD_API2_FILE;
569 
570 	if (!bd->fw)
571 		bd->fw = ath11k_core_firmware_request(ab, filename);
572 
573 	if (IS_ERR(bd->fw))
574 		return PTR_ERR(bd->fw);
575 
576 	data = bd->fw->data;
577 	len = bd->fw->size;
578 
579 	ath11k_core_create_firmware_path(ab, filename,
580 					 filepath, sizeof(filepath));
581 
582 	/* magic has extra null byte padded */
583 	magic_len = strlen(ATH11K_BOARD_MAGIC) + 1;
584 	if (len < magic_len) {
585 		ath11k_err(ab, "failed to find magic value in %s, file too short: %zu\n",
586 			   filepath, len);
587 		ret = -EINVAL;
588 		goto err;
589 	}
590 
591 	if (memcmp(data, ATH11K_BOARD_MAGIC, magic_len)) {
592 		ath11k_err(ab, "found invalid board magic\n");
593 		ret = -EINVAL;
594 		goto err;
595 	}
596 
597 	/* magic is padded to 4 bytes */
598 	magic_len = ALIGN(magic_len, 4);
599 	if (len < magic_len) {
600 		ath11k_err(ab, "failed: %s too small to contain board data, len: %zu\n",
601 			   filepath, len);
602 		ret = -EINVAL;
603 		goto err;
604 	}
605 
606 	data += magic_len;
607 	len -= magic_len;
608 
609 	while (len > sizeof(struct ath11k_fw_ie)) {
610 		hdr = (struct ath11k_fw_ie *)data;
611 		ie_id = le32_to_cpu(hdr->id);
612 		ie_len = le32_to_cpu(hdr->len);
613 
614 		len -= sizeof(*hdr);
615 		data = hdr->data;
616 
617 		if (len < ALIGN(ie_len, 4)) {
618 			ath11k_err(ab, "invalid length for board ie_id %d ie_len %zu len %zu\n",
619 				   ie_id, ie_len, len);
620 			ret = -EINVAL;
621 			goto err;
622 		}
623 
624 		switch (ie_id) {
625 		case ATH11K_BD_IE_BOARD:
626 			ret = ath11k_core_parse_bd_ie_board(ab, bd, data,
627 							    ie_len,
628 							    boardname,
629 							    ATH11K_BD_IE_BOARD);
630 			if (ret == -ENOENT)
631 				/* no match found, continue */
632 				break;
633 			else if (ret)
634 				/* there was an error, bail out */
635 				goto err;
636 			/* either found or error, so stop searching */
637 			goto out;
638 		}
639 
640 		/* jump over the padding */
641 		ie_len = ALIGN(ie_len, 4);
642 
643 		len -= ie_len;
644 		data += ie_len;
645 	}
646 
647 out:
648 	if (!bd->data || !bd->len) {
649 		ath11k_err(ab,
650 			   "failed to fetch board data for %s from %s\n",
651 			   boardname, filepath);
652 		ret = -ENODATA;
653 		goto err;
654 	}
655 
656 	return 0;
657 
658 err:
659 	ath11k_core_free_bdf(ab, bd);
660 	return ret;
661 }
662 
663 static int ath11k_core_fetch_board_data_api_1(struct ath11k_base *ab,
664 					      struct ath11k_board_data *bd)
665 {
666 	bd->fw = ath11k_core_firmware_request(ab, ATH11K_DEFAULT_BOARD_FILE);
667 	if (IS_ERR(bd->fw))
668 		return PTR_ERR(bd->fw);
669 
670 	bd->data = bd->fw->data;
671 	bd->len = bd->fw->size;
672 
673 	return 0;
674 }
675 
676 #define BOARD_NAME_SIZE 200
677 int ath11k_core_fetch_bdf(struct ath11k_base *ab, struct ath11k_board_data *bd)
678 {
679 	char boardname[BOARD_NAME_SIZE];
680 	int ret;
681 
682 	ret = ath11k_core_create_board_name(ab, boardname, BOARD_NAME_SIZE);
683 	if (ret) {
684 		ath11k_err(ab, "failed to create board name: %d", ret);
685 		return ret;
686 	}
687 
688 	ab->bd_api = 2;
689 	ret = ath11k_core_fetch_board_data_api_n(ab, bd, boardname);
690 	if (!ret)
691 		goto success;
692 
693 	ab->bd_api = 1;
694 	ret = ath11k_core_fetch_board_data_api_1(ab, bd);
695 	if (ret) {
696 		ath11k_err(ab, "failed to fetch board-2.bin or board.bin from %s\n",
697 			   ab->hw_params.fw.dir);
698 		return ret;
699 	}
700 
701 success:
702 	ath11k_dbg(ab, ATH11K_DBG_BOOT, "using board api %d\n", ab->bd_api);
703 	return 0;
704 }
705 
706 static void ath11k_core_stop(struct ath11k_base *ab)
707 {
708 	if (!test_bit(ATH11K_FLAG_CRASH_FLUSH, &ab->dev_flags))
709 		ath11k_qmi_firmware_stop(ab);
710 
711 	ath11k_hif_stop(ab);
712 	ath11k_wmi_detach(ab);
713 	ath11k_dp_pdev_reo_cleanup(ab);
714 
715 	/* De-Init of components as needed */
716 }
717 
718 static int ath11k_core_soc_create(struct ath11k_base *ab)
719 {
720 	int ret;
721 
722 	ret = ath11k_qmi_init_service(ab);
723 	if (ret) {
724 		ath11k_err(ab, "failed to initialize qmi :%d\n", ret);
725 		return ret;
726 	}
727 
728 	ret = ath11k_debugfs_soc_create(ab);
729 	if (ret) {
730 		ath11k_err(ab, "failed to create ath11k debugfs\n");
731 		goto err_qmi_deinit;
732 	}
733 
734 	ret = ath11k_hif_power_up(ab);
735 	if (ret) {
736 		ath11k_err(ab, "failed to power up :%d\n", ret);
737 		goto err_debugfs_reg;
738 	}
739 
740 	return 0;
741 
742 err_debugfs_reg:
743 	ath11k_debugfs_soc_destroy(ab);
744 err_qmi_deinit:
745 	ath11k_qmi_deinit_service(ab);
746 	return ret;
747 }
748 
749 static void ath11k_core_soc_destroy(struct ath11k_base *ab)
750 {
751 	ath11k_debugfs_soc_destroy(ab);
752 	ath11k_dp_free(ab);
753 	ath11k_reg_free(ab);
754 	ath11k_qmi_deinit_service(ab);
755 }
756 
757 static int ath11k_core_pdev_create(struct ath11k_base *ab)
758 {
759 	int ret;
760 
761 	ret = ath11k_debugfs_pdev_create(ab);
762 	if (ret) {
763 		ath11k_err(ab, "failed to create core pdev debugfs: %d\n", ret);
764 		return ret;
765 	}
766 
767 	ret = ath11k_mac_register(ab);
768 	if (ret) {
769 		ath11k_err(ab, "failed register the radio with mac80211: %d\n", ret);
770 		goto err_pdev_debug;
771 	}
772 
773 	ret = ath11k_dp_pdev_alloc(ab);
774 	if (ret) {
775 		ath11k_err(ab, "failed to attach DP pdev: %d\n", ret);
776 		goto err_mac_unregister;
777 	}
778 
779 	ret = ath11k_thermal_register(ab);
780 	if (ret) {
781 		ath11k_err(ab, "could not register thermal device: %d\n",
782 			   ret);
783 		goto err_dp_pdev_free;
784 	}
785 
786 	ret = ath11k_spectral_init(ab);
787 	if (ret) {
788 		ath11k_err(ab, "failed to init spectral %d\n", ret);
789 		goto err_thermal_unregister;
790 	}
791 
792 	return 0;
793 
794 err_thermal_unregister:
795 	ath11k_thermal_unregister(ab);
796 err_dp_pdev_free:
797 	ath11k_dp_pdev_free(ab);
798 err_mac_unregister:
799 	ath11k_mac_unregister(ab);
800 err_pdev_debug:
801 	ath11k_debugfs_pdev_destroy(ab);
802 
803 	return ret;
804 }
805 
806 static void ath11k_core_pdev_destroy(struct ath11k_base *ab)
807 {
808 	ath11k_spectral_deinit(ab);
809 	ath11k_thermal_unregister(ab);
810 	ath11k_mac_unregister(ab);
811 	ath11k_hif_irq_disable(ab);
812 	ath11k_dp_pdev_free(ab);
813 	ath11k_debugfs_pdev_destroy(ab);
814 }
815 
816 static int ath11k_core_start(struct ath11k_base *ab,
817 			     enum ath11k_firmware_mode mode)
818 {
819 	int ret;
820 
821 	ret = ath11k_qmi_firmware_start(ab, mode);
822 	if (ret) {
823 		ath11k_err(ab, "failed to attach wmi: %d\n", ret);
824 		return ret;
825 	}
826 
827 	ret = ath11k_wmi_attach(ab);
828 	if (ret) {
829 		ath11k_err(ab, "failed to attach wmi: %d\n", ret);
830 		goto err_firmware_stop;
831 	}
832 
833 	ret = ath11k_htc_init(ab);
834 	if (ret) {
835 		ath11k_err(ab, "failed to init htc: %d\n", ret);
836 		goto err_wmi_detach;
837 	}
838 
839 	ret = ath11k_hif_start(ab);
840 	if (ret) {
841 		ath11k_err(ab, "failed to start HIF: %d\n", ret);
842 		goto err_wmi_detach;
843 	}
844 
845 	ret = ath11k_htc_wait_target(&ab->htc);
846 	if (ret) {
847 		ath11k_err(ab, "failed to connect to HTC: %d\n", ret);
848 		goto err_hif_stop;
849 	}
850 
851 	ret = ath11k_dp_htt_connect(&ab->dp);
852 	if (ret) {
853 		ath11k_err(ab, "failed to connect to HTT: %d\n", ret);
854 		goto err_hif_stop;
855 	}
856 
857 	ret = ath11k_wmi_connect(ab);
858 	if (ret) {
859 		ath11k_err(ab, "failed to connect wmi: %d\n", ret);
860 		goto err_hif_stop;
861 	}
862 
863 	ret = ath11k_htc_start(&ab->htc);
864 	if (ret) {
865 		ath11k_err(ab, "failed to start HTC: %d\n", ret);
866 		goto err_hif_stop;
867 	}
868 
869 	ret = ath11k_wmi_wait_for_service_ready(ab);
870 	if (ret) {
871 		ath11k_err(ab, "failed to receive wmi service ready event: %d\n",
872 			   ret);
873 		goto err_hif_stop;
874 	}
875 
876 	ret = ath11k_mac_allocate(ab);
877 	if (ret) {
878 		ath11k_err(ab, "failed to create new hw device with mac80211 :%d\n",
879 			   ret);
880 		goto err_hif_stop;
881 	}
882 
883 	ath11k_dp_pdev_pre_alloc(ab);
884 
885 	ret = ath11k_dp_pdev_reo_setup(ab);
886 	if (ret) {
887 		ath11k_err(ab, "failed to initialize reo destination rings: %d\n", ret);
888 		goto err_mac_destroy;
889 	}
890 
891 	ret = ath11k_wmi_cmd_init(ab);
892 	if (ret) {
893 		ath11k_err(ab, "failed to send wmi init cmd: %d\n", ret);
894 		goto err_reo_cleanup;
895 	}
896 
897 	ret = ath11k_wmi_wait_for_unified_ready(ab);
898 	if (ret) {
899 		ath11k_err(ab, "failed to receive wmi unified ready event: %d\n",
900 			   ret);
901 		goto err_reo_cleanup;
902 	}
903 
904 	/* put hardware to DBS mode */
905 	if (ab->hw_params.single_pdev_only) {
906 		ret = ath11k_wmi_set_hw_mode(ab, WMI_HOST_HW_MODE_DBS);
907 		if (ret) {
908 			ath11k_err(ab, "failed to send dbs mode: %d\n", ret);
909 			goto err_hif_stop;
910 		}
911 	}
912 
913 	ret = ath11k_dp_tx_htt_h2t_ver_req_msg(ab);
914 	if (ret) {
915 		ath11k_err(ab, "failed to send htt version request message: %d\n",
916 			   ret);
917 		goto err_reo_cleanup;
918 	}
919 
920 	return 0;
921 
922 err_reo_cleanup:
923 	ath11k_dp_pdev_reo_cleanup(ab);
924 err_mac_destroy:
925 	ath11k_mac_destroy(ab);
926 err_hif_stop:
927 	ath11k_hif_stop(ab);
928 err_wmi_detach:
929 	ath11k_wmi_detach(ab);
930 err_firmware_stop:
931 	ath11k_qmi_firmware_stop(ab);
932 
933 	return ret;
934 }
935 
936 int ath11k_core_qmi_firmware_ready(struct ath11k_base *ab)
937 {
938 	int ret;
939 
940 	ret = ath11k_ce_init_pipes(ab);
941 	if (ret) {
942 		ath11k_err(ab, "failed to initialize CE: %d\n", ret);
943 		return ret;
944 	}
945 
946 	ret = ath11k_dp_alloc(ab);
947 	if (ret) {
948 		ath11k_err(ab, "failed to init DP: %d\n", ret);
949 		return ret;
950 	}
951 
952 	switch (ath11k_crypto_mode) {
953 	case ATH11K_CRYPT_MODE_SW:
954 		set_bit(ATH11K_FLAG_HW_CRYPTO_DISABLED, &ab->dev_flags);
955 		set_bit(ATH11K_FLAG_RAW_MODE, &ab->dev_flags);
956 		break;
957 	case ATH11K_CRYPT_MODE_HW:
958 		clear_bit(ATH11K_FLAG_HW_CRYPTO_DISABLED, &ab->dev_flags);
959 		clear_bit(ATH11K_FLAG_RAW_MODE, &ab->dev_flags);
960 		break;
961 	default:
962 		ath11k_info(ab, "invalid crypto_mode: %d\n", ath11k_crypto_mode);
963 		return -EINVAL;
964 	}
965 
966 	if (ath11k_frame_mode == ATH11K_HW_TXRX_RAW)
967 		set_bit(ATH11K_FLAG_RAW_MODE, &ab->dev_flags);
968 
969 	mutex_lock(&ab->core_lock);
970 	ret = ath11k_core_start(ab, ATH11K_FIRMWARE_MODE_NORMAL);
971 	if (ret) {
972 		ath11k_err(ab, "failed to start core: %d\n", ret);
973 		goto err_dp_free;
974 	}
975 
976 	ret = ath11k_core_pdev_create(ab);
977 	if (ret) {
978 		ath11k_err(ab, "failed to create pdev core: %d\n", ret);
979 		goto err_core_stop;
980 	}
981 	ath11k_hif_irq_enable(ab);
982 	mutex_unlock(&ab->core_lock);
983 
984 	return 0;
985 
986 err_core_stop:
987 	ath11k_core_stop(ab);
988 	ath11k_mac_destroy(ab);
989 err_dp_free:
990 	ath11k_dp_free(ab);
991 	mutex_unlock(&ab->core_lock);
992 	return ret;
993 }
994 
995 static int ath11k_core_reconfigure_on_crash(struct ath11k_base *ab)
996 {
997 	int ret;
998 
999 	mutex_lock(&ab->core_lock);
1000 	ath11k_thermal_unregister(ab);
1001 	ath11k_hif_irq_disable(ab);
1002 	ath11k_dp_pdev_free(ab);
1003 	ath11k_spectral_deinit(ab);
1004 	ath11k_hif_stop(ab);
1005 	ath11k_wmi_detach(ab);
1006 	ath11k_dp_pdev_reo_cleanup(ab);
1007 	mutex_unlock(&ab->core_lock);
1008 
1009 	ath11k_dp_free(ab);
1010 	ath11k_hal_srng_deinit(ab);
1011 
1012 	ab->free_vdev_map = (1LL << (ab->num_radios * TARGET_NUM_VDEVS)) - 1;
1013 
1014 	ret = ath11k_hal_srng_init(ab);
1015 	if (ret)
1016 		return ret;
1017 
1018 	clear_bit(ATH11K_FLAG_CRASH_FLUSH, &ab->dev_flags);
1019 
1020 	ret = ath11k_core_qmi_firmware_ready(ab);
1021 	if (ret)
1022 		goto err_hal_srng_deinit;
1023 
1024 	clear_bit(ATH11K_FLAG_RECOVERY, &ab->dev_flags);
1025 
1026 	return 0;
1027 
1028 err_hal_srng_deinit:
1029 	ath11k_hal_srng_deinit(ab);
1030 	return ret;
1031 }
1032 
1033 void ath11k_core_halt(struct ath11k *ar)
1034 {
1035 	struct ath11k_base *ab = ar->ab;
1036 
1037 	lockdep_assert_held(&ar->conf_mutex);
1038 
1039 	ar->num_created_vdevs = 0;
1040 	ar->allocated_vdev_map = 0;
1041 
1042 	ath11k_mac_scan_finish(ar);
1043 	ath11k_mac_peer_cleanup_all(ar);
1044 	cancel_delayed_work_sync(&ar->scan.timeout);
1045 	cancel_work_sync(&ar->regd_update_work);
1046 
1047 	rcu_assign_pointer(ab->pdevs_active[ar->pdev_idx], NULL);
1048 	synchronize_rcu();
1049 	INIT_LIST_HEAD(&ar->arvifs);
1050 	idr_init(&ar->txmgmt_idr);
1051 }
1052 
1053 static void ath11k_core_restart(struct work_struct *work)
1054 {
1055 	struct ath11k_base *ab = container_of(work, struct ath11k_base, restart_work);
1056 	struct ath11k *ar;
1057 	struct ath11k_pdev *pdev;
1058 	int i, ret = 0;
1059 
1060 	spin_lock_bh(&ab->base_lock);
1061 	ab->stats.fw_crash_counter++;
1062 	spin_unlock_bh(&ab->base_lock);
1063 
1064 	for (i = 0; i < ab->num_radios; i++) {
1065 		pdev = &ab->pdevs[i];
1066 		ar = pdev->ar;
1067 		if (!ar || ar->state == ATH11K_STATE_OFF)
1068 			continue;
1069 
1070 		ieee80211_stop_queues(ar->hw);
1071 		ath11k_mac_drain_tx(ar);
1072 		complete(&ar->scan.started);
1073 		complete(&ar->scan.completed);
1074 		complete(&ar->peer_assoc_done);
1075 		complete(&ar->peer_delete_done);
1076 		complete(&ar->install_key_done);
1077 		complete(&ar->vdev_setup_done);
1078 		complete(&ar->vdev_delete_done);
1079 		complete(&ar->bss_survey_done);
1080 		complete(&ar->thermal.wmi_sync);
1081 
1082 		wake_up(&ar->dp.tx_empty_waitq);
1083 		idr_for_each(&ar->txmgmt_idr,
1084 			     ath11k_mac_tx_mgmt_pending_free, ar);
1085 		idr_destroy(&ar->txmgmt_idr);
1086 	}
1087 
1088 	wake_up(&ab->wmi_ab.tx_credits_wq);
1089 	wake_up(&ab->peer_mapping_wq);
1090 
1091 	ret = ath11k_core_reconfigure_on_crash(ab);
1092 	if (ret) {
1093 		ath11k_err(ab, "failed to reconfigure driver on crash recovery\n");
1094 		return;
1095 	}
1096 
1097 	for (i = 0; i < ab->num_radios; i++) {
1098 		pdev = &ab->pdevs[i];
1099 		ar = pdev->ar;
1100 		if (!ar || ar->state == ATH11K_STATE_OFF)
1101 			continue;
1102 
1103 		mutex_lock(&ar->conf_mutex);
1104 
1105 		switch (ar->state) {
1106 		case ATH11K_STATE_ON:
1107 			ar->state = ATH11K_STATE_RESTARTING;
1108 			ath11k_core_halt(ar);
1109 			ieee80211_restart_hw(ar->hw);
1110 			break;
1111 		case ATH11K_STATE_OFF:
1112 			ath11k_warn(ab,
1113 				    "cannot restart radio %d that hasn't been started\n",
1114 				    i);
1115 			break;
1116 		case ATH11K_STATE_RESTARTING:
1117 			break;
1118 		case ATH11K_STATE_RESTARTED:
1119 			ar->state = ATH11K_STATE_WEDGED;
1120 			fallthrough;
1121 		case ATH11K_STATE_WEDGED:
1122 			ath11k_warn(ab,
1123 				    "device is wedged, will not restart radio %d\n", i);
1124 			break;
1125 		}
1126 		mutex_unlock(&ar->conf_mutex);
1127 	}
1128 	complete(&ab->driver_recovery);
1129 }
1130 
1131 static int ath11k_init_hw_params(struct ath11k_base *ab)
1132 {
1133 	const struct ath11k_hw_params *hw_params = NULL;
1134 	int i;
1135 
1136 	for (i = 0; i < ARRAY_SIZE(ath11k_hw_params); i++) {
1137 		hw_params = &ath11k_hw_params[i];
1138 
1139 		if (hw_params->hw_rev == ab->hw_rev)
1140 			break;
1141 	}
1142 
1143 	if (i == ARRAY_SIZE(ath11k_hw_params)) {
1144 		ath11k_err(ab, "Unsupported hardware version: 0x%x\n", ab->hw_rev);
1145 		return -EINVAL;
1146 	}
1147 
1148 	ab->hw_params = *hw_params;
1149 
1150 	ath11k_info(ab, "%s\n", ab->hw_params.name);
1151 
1152 	return 0;
1153 }
1154 
1155 int ath11k_core_pre_init(struct ath11k_base *ab)
1156 {
1157 	int ret;
1158 
1159 	ret = ath11k_init_hw_params(ab);
1160 	if (ret) {
1161 		ath11k_err(ab, "failed to get hw params: %d\n", ret);
1162 		return ret;
1163 	}
1164 
1165 	return 0;
1166 }
1167 EXPORT_SYMBOL(ath11k_core_pre_init);
1168 
1169 int ath11k_core_init(struct ath11k_base *ab)
1170 {
1171 	int ret;
1172 
1173 	ret = ath11k_core_soc_create(ab);
1174 	if (ret) {
1175 		ath11k_err(ab, "failed to create soc core: %d\n", ret);
1176 		return ret;
1177 	}
1178 
1179 	return 0;
1180 }
1181 EXPORT_SYMBOL(ath11k_core_init);
1182 
1183 void ath11k_core_deinit(struct ath11k_base *ab)
1184 {
1185 	mutex_lock(&ab->core_lock);
1186 
1187 	ath11k_core_pdev_destroy(ab);
1188 	ath11k_core_stop(ab);
1189 
1190 	mutex_unlock(&ab->core_lock);
1191 
1192 	ath11k_hif_power_down(ab);
1193 	ath11k_mac_destroy(ab);
1194 	ath11k_core_soc_destroy(ab);
1195 }
1196 EXPORT_SYMBOL(ath11k_core_deinit);
1197 
1198 void ath11k_core_free(struct ath11k_base *ab)
1199 {
1200 	kfree(ab);
1201 }
1202 EXPORT_SYMBOL(ath11k_core_free);
1203 
1204 struct ath11k_base *ath11k_core_alloc(struct device *dev, size_t priv_size,
1205 				      enum ath11k_bus bus,
1206 				      const struct ath11k_bus_params *bus_params)
1207 {
1208 	struct ath11k_base *ab;
1209 
1210 	ab = kzalloc(sizeof(*ab) + priv_size, GFP_KERNEL);
1211 	if (!ab)
1212 		return NULL;
1213 
1214 	init_completion(&ab->driver_recovery);
1215 
1216 	ab->workqueue = create_singlethread_workqueue("ath11k_wq");
1217 	if (!ab->workqueue)
1218 		goto err_sc_free;
1219 
1220 	mutex_init(&ab->core_lock);
1221 	spin_lock_init(&ab->base_lock);
1222 
1223 	INIT_LIST_HEAD(&ab->peers);
1224 	init_waitqueue_head(&ab->peer_mapping_wq);
1225 	init_waitqueue_head(&ab->wmi_ab.tx_credits_wq);
1226 	init_waitqueue_head(&ab->qmi.cold_boot_waitq);
1227 	INIT_WORK(&ab->restart_work, ath11k_core_restart);
1228 	timer_setup(&ab->rx_replenish_retry, ath11k_ce_rx_replenish_retry, 0);
1229 	init_completion(&ab->htc_suspend);
1230 	init_completion(&ab->wow.wakeup_completed);
1231 
1232 	ab->dev = dev;
1233 	ab->bus_params = *bus_params;
1234 	ab->hif.bus = bus;
1235 
1236 	return ab;
1237 
1238 err_sc_free:
1239 	kfree(ab);
1240 	return NULL;
1241 }
1242 EXPORT_SYMBOL(ath11k_core_alloc);
1243 
1244 MODULE_DESCRIPTION("Core module for Qualcomm Atheros 802.11ax wireless LAN cards.");
1245 MODULE_LICENSE("Dual BSD/GPL");
1246