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