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