xref: /linux/drivers/net/wireless/ath/ath12k/core.c (revision 2aceb896ee18ae35b21b14c978d8c2ef8c7b439d)
1 // SPDX-License-Identifier: BSD-3-Clause-Clear
2 /*
3  * Copyright (c) 2018-2021 The Linux Foundation. All rights reserved.
4  * Copyright (c) 2021-2022 Qualcomm Innovation Center, Inc. All rights reserved.
5  */
6 
7 #include <linux/module.h>
8 #include <linux/slab.h>
9 #include <linux/remoteproc.h>
10 #include <linux/firmware.h>
11 #include <linux/of.h>
12 #include "core.h"
13 #include "dp_tx.h"
14 #include "dp_rx.h"
15 #include "debug.h"
16 #include "hif.h"
17 
18 unsigned int ath12k_debug_mask;
19 module_param_named(debug_mask, ath12k_debug_mask, uint, 0644);
20 MODULE_PARM_DESC(debug_mask, "Debugging mask");
21 
22 static int ath12k_core_rfkill_config(struct ath12k_base *ab)
23 {
24 	struct ath12k *ar;
25 	int ret = 0, i;
26 
27 	if (!(ab->target_caps.sys_cap_info & WMI_SYS_CAP_INFO_RFKILL))
28 		return 0;
29 
30 	for (i = 0; i < ab->num_radios; i++) {
31 		ar = ab->pdevs[i].ar;
32 
33 		ret = ath12k_mac_rfkill_config(ar);
34 		if (ret && ret != -EOPNOTSUPP) {
35 			ath12k_warn(ab, "failed to configure rfkill: %d", ret);
36 			return ret;
37 		}
38 	}
39 
40 	return ret;
41 }
42 
43 int ath12k_core_suspend(struct ath12k_base *ab)
44 {
45 	int ret;
46 
47 	if (!ab->hw_params->supports_suspend)
48 		return -EOPNOTSUPP;
49 
50 	/* TODO: there can frames in queues so for now add delay as a hack.
51 	 * Need to implement to handle and remove this delay.
52 	 */
53 	msleep(500);
54 
55 	ret = ath12k_dp_rx_pktlog_stop(ab, true);
56 	if (ret) {
57 		ath12k_warn(ab, "failed to stop dp rx (and timer) pktlog during suspend: %d\n",
58 			    ret);
59 		return ret;
60 	}
61 
62 	ret = ath12k_dp_rx_pktlog_stop(ab, false);
63 	if (ret) {
64 		ath12k_warn(ab, "failed to stop dp rx pktlog during suspend: %d\n",
65 			    ret);
66 		return ret;
67 	}
68 
69 	ath12k_hif_irq_disable(ab);
70 	ath12k_hif_ce_irq_disable(ab);
71 
72 	ret = ath12k_hif_suspend(ab);
73 	if (ret) {
74 		ath12k_warn(ab, "failed to suspend hif: %d\n", ret);
75 		return ret;
76 	}
77 
78 	return 0;
79 }
80 
81 int ath12k_core_resume(struct ath12k_base *ab)
82 {
83 	int ret;
84 
85 	if (!ab->hw_params->supports_suspend)
86 		return -EOPNOTSUPP;
87 
88 	ret = ath12k_hif_resume(ab);
89 	if (ret) {
90 		ath12k_warn(ab, "failed to resume hif during resume: %d\n", ret);
91 		return ret;
92 	}
93 
94 	ath12k_hif_ce_irq_enable(ab);
95 	ath12k_hif_irq_enable(ab);
96 
97 	ret = ath12k_dp_rx_pktlog_start(ab);
98 	if (ret) {
99 		ath12k_warn(ab, "failed to start rx pktlog during resume: %d\n",
100 			    ret);
101 		return ret;
102 	}
103 
104 	return 0;
105 }
106 
107 static int ath12k_core_create_board_name(struct ath12k_base *ab, char *name,
108 					 size_t name_len)
109 {
110 	/* strlen(',variant=') + strlen(ab->qmi.target.bdf_ext) */
111 	char variant[9 + ATH12K_QMI_BDF_EXT_STR_LENGTH] = { 0 };
112 
113 	if (ab->qmi.target.bdf_ext[0] != '\0')
114 		scnprintf(variant, sizeof(variant), ",variant=%s",
115 			  ab->qmi.target.bdf_ext);
116 
117 	scnprintf(name, name_len,
118 		  "bus=%s,qmi-chip-id=%d,qmi-board-id=%d%s",
119 		  ath12k_bus_str(ab->hif.bus),
120 		  ab->qmi.target.chip_id,
121 		  ab->qmi.target.board_id, variant);
122 
123 	ath12k_dbg(ab, ATH12K_DBG_BOOT, "boot using board name '%s'\n", name);
124 
125 	return 0;
126 }
127 
128 const struct firmware *ath12k_core_firmware_request(struct ath12k_base *ab,
129 						    const char *file)
130 {
131 	const struct firmware *fw;
132 	char path[100];
133 	int ret;
134 
135 	if (!file)
136 		return ERR_PTR(-ENOENT);
137 
138 	ath12k_core_create_firmware_path(ab, file, path, sizeof(path));
139 
140 	ret = firmware_request_nowarn(&fw, path, ab->dev);
141 	if (ret)
142 		return ERR_PTR(ret);
143 
144 	ath12k_dbg(ab, ATH12K_DBG_BOOT, "boot firmware request %s size %zu\n",
145 		   path, fw->size);
146 
147 	return fw;
148 }
149 
150 void ath12k_core_free_bdf(struct ath12k_base *ab, struct ath12k_board_data *bd)
151 {
152 	if (!IS_ERR(bd->fw))
153 		release_firmware(bd->fw);
154 
155 	memset(bd, 0, sizeof(*bd));
156 }
157 
158 static int ath12k_core_parse_bd_ie_board(struct ath12k_base *ab,
159 					 struct ath12k_board_data *bd,
160 					 const void *buf, size_t buf_len,
161 					 const char *boardname,
162 					 int bd_ie_type)
163 {
164 	const struct ath12k_fw_ie *hdr;
165 	bool name_match_found;
166 	int ret, board_ie_id;
167 	size_t board_ie_len;
168 	const void *board_ie_data;
169 
170 	name_match_found = false;
171 
172 	/* go through ATH12K_BD_IE_BOARD_ elements */
173 	while (buf_len > sizeof(struct ath12k_fw_ie)) {
174 		hdr = buf;
175 		board_ie_id = le32_to_cpu(hdr->id);
176 		board_ie_len = le32_to_cpu(hdr->len);
177 		board_ie_data = hdr->data;
178 
179 		buf_len -= sizeof(*hdr);
180 		buf += sizeof(*hdr);
181 
182 		if (buf_len < ALIGN(board_ie_len, 4)) {
183 			ath12k_err(ab, "invalid ATH12K_BD_IE_BOARD length: %zu < %zu\n",
184 				   buf_len, ALIGN(board_ie_len, 4));
185 			ret = -EINVAL;
186 			goto out;
187 		}
188 
189 		switch (board_ie_id) {
190 		case ATH12K_BD_IE_BOARD_NAME:
191 			ath12k_dbg_dump(ab, ATH12K_DBG_BOOT, "board name", "",
192 					board_ie_data, board_ie_len);
193 
194 			if (board_ie_len != strlen(boardname))
195 				break;
196 
197 			ret = memcmp(board_ie_data, boardname, strlen(boardname));
198 			if (ret)
199 				break;
200 
201 			name_match_found = true;
202 			ath12k_dbg(ab, ATH12K_DBG_BOOT,
203 				   "boot found match for name '%s'",
204 				   boardname);
205 			break;
206 		case ATH12K_BD_IE_BOARD_DATA:
207 			if (!name_match_found)
208 				/* no match found */
209 				break;
210 
211 			ath12k_dbg(ab, ATH12K_DBG_BOOT,
212 				   "boot found board data for '%s'", boardname);
213 
214 			bd->data = board_ie_data;
215 			bd->len = board_ie_len;
216 
217 			ret = 0;
218 			goto out;
219 		default:
220 			ath12k_warn(ab, "unknown ATH12K_BD_IE_BOARD found: %d\n",
221 				    board_ie_id);
222 			break;
223 		}
224 
225 		/* jump over the padding */
226 		board_ie_len = ALIGN(board_ie_len, 4);
227 
228 		buf_len -= board_ie_len;
229 		buf += board_ie_len;
230 	}
231 
232 	/* no match found */
233 	ret = -ENOENT;
234 
235 out:
236 	return ret;
237 }
238 
239 static int ath12k_core_fetch_board_data_api_n(struct ath12k_base *ab,
240 					      struct ath12k_board_data *bd,
241 					      const char *boardname)
242 {
243 	size_t len, magic_len;
244 	const u8 *data;
245 	char *filename, filepath[100];
246 	size_t ie_len;
247 	struct ath12k_fw_ie *hdr;
248 	int ret, ie_id;
249 
250 	filename = ATH12K_BOARD_API2_FILE;
251 
252 	if (!bd->fw)
253 		bd->fw = ath12k_core_firmware_request(ab, filename);
254 
255 	if (IS_ERR(bd->fw))
256 		return PTR_ERR(bd->fw);
257 
258 	data = bd->fw->data;
259 	len = bd->fw->size;
260 
261 	ath12k_core_create_firmware_path(ab, filename,
262 					 filepath, sizeof(filepath));
263 
264 	/* magic has extra null byte padded */
265 	magic_len = strlen(ATH12K_BOARD_MAGIC) + 1;
266 	if (len < magic_len) {
267 		ath12k_err(ab, "failed to find magic value in %s, file too short: %zu\n",
268 			   filepath, len);
269 		ret = -EINVAL;
270 		goto err;
271 	}
272 
273 	if (memcmp(data, ATH12K_BOARD_MAGIC, magic_len)) {
274 		ath12k_err(ab, "found invalid board magic\n");
275 		ret = -EINVAL;
276 		goto err;
277 	}
278 
279 	/* magic is padded to 4 bytes */
280 	magic_len = ALIGN(magic_len, 4);
281 	if (len < magic_len) {
282 		ath12k_err(ab, "failed: %s too small to contain board data, len: %zu\n",
283 			   filepath, len);
284 		ret = -EINVAL;
285 		goto err;
286 	}
287 
288 	data += magic_len;
289 	len -= magic_len;
290 
291 	while (len > sizeof(struct ath12k_fw_ie)) {
292 		hdr = (struct ath12k_fw_ie *)data;
293 		ie_id = le32_to_cpu(hdr->id);
294 		ie_len = le32_to_cpu(hdr->len);
295 
296 		len -= sizeof(*hdr);
297 		data = hdr->data;
298 
299 		if (len < ALIGN(ie_len, 4)) {
300 			ath12k_err(ab, "invalid length for board ie_id %d ie_len %zu len %zu\n",
301 				   ie_id, ie_len, len);
302 			ret = -EINVAL;
303 			goto err;
304 		}
305 
306 		switch (ie_id) {
307 		case ATH12K_BD_IE_BOARD:
308 			ret = ath12k_core_parse_bd_ie_board(ab, bd, data,
309 							    ie_len,
310 							    boardname,
311 							    ATH12K_BD_IE_BOARD);
312 			if (ret == -ENOENT)
313 				/* no match found, continue */
314 				break;
315 			else if (ret)
316 				/* there was an error, bail out */
317 				goto err;
318 			/* either found or error, so stop searching */
319 			goto out;
320 		}
321 
322 		/* jump over the padding */
323 		ie_len = ALIGN(ie_len, 4);
324 
325 		len -= ie_len;
326 		data += ie_len;
327 	}
328 
329 out:
330 	if (!bd->data || !bd->len) {
331 		ath12k_err(ab,
332 			   "failed to fetch board data for %s from %s\n",
333 			   boardname, filepath);
334 		ret = -ENODATA;
335 		goto err;
336 	}
337 
338 	return 0;
339 
340 err:
341 	ath12k_core_free_bdf(ab, bd);
342 	return ret;
343 }
344 
345 int ath12k_core_fetch_board_data_api_1(struct ath12k_base *ab,
346 				       struct ath12k_board_data *bd,
347 				       char *filename)
348 {
349 	bd->fw = ath12k_core_firmware_request(ab, filename);
350 	if (IS_ERR(bd->fw))
351 		return PTR_ERR(bd->fw);
352 
353 	bd->data = bd->fw->data;
354 	bd->len = bd->fw->size;
355 
356 	return 0;
357 }
358 
359 #define BOARD_NAME_SIZE 100
360 int ath12k_core_fetch_bdf(struct ath12k_base *ab, struct ath12k_board_data *bd)
361 {
362 	char boardname[BOARD_NAME_SIZE];
363 	int ret;
364 
365 	ret = ath12k_core_create_board_name(ab, boardname, BOARD_NAME_SIZE);
366 	if (ret) {
367 		ath12k_err(ab, "failed to create board name: %d", ret);
368 		return ret;
369 	}
370 
371 	ab->bd_api = 2;
372 	ret = ath12k_core_fetch_board_data_api_n(ab, bd, boardname);
373 	if (!ret)
374 		goto success;
375 
376 	ab->bd_api = 1;
377 	ret = ath12k_core_fetch_board_data_api_1(ab, bd, ATH12K_DEFAULT_BOARD_FILE);
378 	if (ret) {
379 		ath12k_err(ab, "failed to fetch board-2.bin or board.bin from %s\n",
380 			   ab->hw_params->fw.dir);
381 		return ret;
382 	}
383 
384 success:
385 	ath12k_dbg(ab, ATH12K_DBG_BOOT, "using board api %d\n", ab->bd_api);
386 	return 0;
387 }
388 
389 static void ath12k_core_stop(struct ath12k_base *ab)
390 {
391 	if (!test_bit(ATH12K_FLAG_CRASH_FLUSH, &ab->dev_flags))
392 		ath12k_qmi_firmware_stop(ab);
393 
394 	ath12k_hif_stop(ab);
395 	ath12k_wmi_detach(ab);
396 	ath12k_dp_rx_pdev_reo_cleanup(ab);
397 
398 	/* De-Init of components as needed */
399 }
400 
401 static void ath12k_core_check_bdfext(const struct dmi_header *hdr, void *data)
402 {
403 	struct ath12k_base *ab = data;
404 	const char *magic = ATH12K_SMBIOS_BDF_EXT_MAGIC;
405 	struct ath12k_smbios_bdf *smbios = (struct ath12k_smbios_bdf *)hdr;
406 	ssize_t copied;
407 	size_t len;
408 	int i;
409 
410 	if (ab->qmi.target.bdf_ext[0] != '\0')
411 		return;
412 
413 	if (hdr->type != ATH12K_SMBIOS_BDF_EXT_TYPE)
414 		return;
415 
416 	if (hdr->length != ATH12K_SMBIOS_BDF_EXT_LENGTH) {
417 		ath12k_dbg(ab, ATH12K_DBG_BOOT,
418 			   "wrong smbios bdf ext type length (%d).\n",
419 			   hdr->length);
420 		return;
421 	}
422 
423 	if (!smbios->bdf_enabled) {
424 		ath12k_dbg(ab, ATH12K_DBG_BOOT, "bdf variant name not found.\n");
425 		return;
426 	}
427 
428 	/* Only one string exists (per spec) */
429 	if (memcmp(smbios->bdf_ext, magic, strlen(magic)) != 0) {
430 		ath12k_dbg(ab, ATH12K_DBG_BOOT,
431 			   "bdf variant magic does not match.\n");
432 		return;
433 	}
434 
435 	len = min_t(size_t,
436 		    strlen(smbios->bdf_ext), sizeof(ab->qmi.target.bdf_ext));
437 	for (i = 0; i < len; i++) {
438 		if (!isascii(smbios->bdf_ext[i]) || !isprint(smbios->bdf_ext[i])) {
439 			ath12k_dbg(ab, ATH12K_DBG_BOOT,
440 				   "bdf variant name contains non ascii chars.\n");
441 			return;
442 		}
443 	}
444 
445 	/* Copy extension name without magic prefix */
446 	copied = strscpy(ab->qmi.target.bdf_ext, smbios->bdf_ext + strlen(magic),
447 			 sizeof(ab->qmi.target.bdf_ext));
448 	if (copied < 0) {
449 		ath12k_dbg(ab, ATH12K_DBG_BOOT,
450 			   "bdf variant string is longer than the buffer can accommodate\n");
451 		return;
452 	}
453 
454 	ath12k_dbg(ab, ATH12K_DBG_BOOT,
455 		   "found and validated bdf variant smbios_type 0x%x bdf %s\n",
456 		   ATH12K_SMBIOS_BDF_EXT_TYPE, ab->qmi.target.bdf_ext);
457 }
458 
459 int ath12k_core_check_smbios(struct ath12k_base *ab)
460 {
461 	ab->qmi.target.bdf_ext[0] = '\0';
462 	dmi_walk(ath12k_core_check_bdfext, ab);
463 
464 	if (ab->qmi.target.bdf_ext[0] == '\0')
465 		return -ENODATA;
466 
467 	return 0;
468 }
469 
470 static int ath12k_core_soc_create(struct ath12k_base *ab)
471 {
472 	int ret;
473 
474 	ret = ath12k_qmi_init_service(ab);
475 	if (ret) {
476 		ath12k_err(ab, "failed to initialize qmi :%d\n", ret);
477 		return ret;
478 	}
479 
480 	ret = ath12k_hif_power_up(ab);
481 	if (ret) {
482 		ath12k_err(ab, "failed to power up :%d\n", ret);
483 		goto err_qmi_deinit;
484 	}
485 
486 	return 0;
487 
488 err_qmi_deinit:
489 	ath12k_qmi_deinit_service(ab);
490 	return ret;
491 }
492 
493 static void ath12k_core_soc_destroy(struct ath12k_base *ab)
494 {
495 	ath12k_dp_free(ab);
496 	ath12k_reg_free(ab);
497 	ath12k_qmi_deinit_service(ab);
498 }
499 
500 static int ath12k_core_pdev_create(struct ath12k_base *ab)
501 {
502 	int ret;
503 
504 	ret = ath12k_mac_register(ab);
505 	if (ret) {
506 		ath12k_err(ab, "failed register the radio with mac80211: %d\n", ret);
507 		return ret;
508 	}
509 
510 	ret = ath12k_dp_pdev_alloc(ab);
511 	if (ret) {
512 		ath12k_err(ab, "failed to attach DP pdev: %d\n", ret);
513 		goto err_mac_unregister;
514 	}
515 
516 	return 0;
517 
518 err_mac_unregister:
519 	ath12k_mac_unregister(ab);
520 
521 	return ret;
522 }
523 
524 static void ath12k_core_pdev_destroy(struct ath12k_base *ab)
525 {
526 	ath12k_mac_unregister(ab);
527 	ath12k_hif_irq_disable(ab);
528 	ath12k_dp_pdev_free(ab);
529 }
530 
531 static int ath12k_core_start(struct ath12k_base *ab,
532 			     enum ath12k_firmware_mode mode)
533 {
534 	int ret;
535 
536 	ret = ath12k_wmi_attach(ab);
537 	if (ret) {
538 		ath12k_err(ab, "failed to attach wmi: %d\n", ret);
539 		return ret;
540 	}
541 
542 	ret = ath12k_htc_init(ab);
543 	if (ret) {
544 		ath12k_err(ab, "failed to init htc: %d\n", ret);
545 		goto err_wmi_detach;
546 	}
547 
548 	ret = ath12k_hif_start(ab);
549 	if (ret) {
550 		ath12k_err(ab, "failed to start HIF: %d\n", ret);
551 		goto err_wmi_detach;
552 	}
553 
554 	ret = ath12k_htc_wait_target(&ab->htc);
555 	if (ret) {
556 		ath12k_err(ab, "failed to connect to HTC: %d\n", ret);
557 		goto err_hif_stop;
558 	}
559 
560 	ret = ath12k_dp_htt_connect(&ab->dp);
561 	if (ret) {
562 		ath12k_err(ab, "failed to connect to HTT: %d\n", ret);
563 		goto err_hif_stop;
564 	}
565 
566 	ret = ath12k_wmi_connect(ab);
567 	if (ret) {
568 		ath12k_err(ab, "failed to connect wmi: %d\n", ret);
569 		goto err_hif_stop;
570 	}
571 
572 	ret = ath12k_htc_start(&ab->htc);
573 	if (ret) {
574 		ath12k_err(ab, "failed to start HTC: %d\n", ret);
575 		goto err_hif_stop;
576 	}
577 
578 	ret = ath12k_wmi_wait_for_service_ready(ab);
579 	if (ret) {
580 		ath12k_err(ab, "failed to receive wmi service ready event: %d\n",
581 			   ret);
582 		goto err_hif_stop;
583 	}
584 
585 	ret = ath12k_mac_allocate(ab);
586 	if (ret) {
587 		ath12k_err(ab, "failed to create new hw device with mac80211 :%d\n",
588 			   ret);
589 		goto err_hif_stop;
590 	}
591 
592 	ath12k_dp_cc_config(ab);
593 
594 	ath12k_dp_pdev_pre_alloc(ab);
595 
596 	ret = ath12k_dp_rx_pdev_reo_setup(ab);
597 	if (ret) {
598 		ath12k_err(ab, "failed to initialize reo destination rings: %d\n", ret);
599 		goto err_mac_destroy;
600 	}
601 
602 	ret = ath12k_wmi_cmd_init(ab);
603 	if (ret) {
604 		ath12k_err(ab, "failed to send wmi init cmd: %d\n", ret);
605 		goto err_reo_cleanup;
606 	}
607 
608 	ret = ath12k_wmi_wait_for_unified_ready(ab);
609 	if (ret) {
610 		ath12k_err(ab, "failed to receive wmi unified ready event: %d\n",
611 			   ret);
612 		goto err_reo_cleanup;
613 	}
614 
615 	/* put hardware to DBS mode */
616 	if (ab->hw_params->single_pdev_only) {
617 		ret = ath12k_wmi_set_hw_mode(ab, WMI_HOST_HW_MODE_DBS);
618 		if (ret) {
619 			ath12k_err(ab, "failed to send dbs mode: %d\n", ret);
620 			goto err_reo_cleanup;
621 		}
622 	}
623 
624 	ret = ath12k_dp_tx_htt_h2t_ver_req_msg(ab);
625 	if (ret) {
626 		ath12k_err(ab, "failed to send htt version request message: %d\n",
627 			   ret);
628 		goto err_reo_cleanup;
629 	}
630 
631 	return 0;
632 
633 err_reo_cleanup:
634 	ath12k_dp_rx_pdev_reo_cleanup(ab);
635 err_mac_destroy:
636 	ath12k_mac_destroy(ab);
637 err_hif_stop:
638 	ath12k_hif_stop(ab);
639 err_wmi_detach:
640 	ath12k_wmi_detach(ab);
641 	return ret;
642 }
643 
644 static int ath12k_core_start_firmware(struct ath12k_base *ab,
645 				      enum ath12k_firmware_mode mode)
646 {
647 	int ret;
648 
649 	ath12k_ce_get_shadow_config(ab, &ab->qmi.ce_cfg.shadow_reg_v3,
650 				    &ab->qmi.ce_cfg.shadow_reg_v3_len);
651 
652 	ret = ath12k_qmi_firmware_start(ab, mode);
653 	if (ret) {
654 		ath12k_err(ab, "failed to send firmware start: %d\n", ret);
655 		return ret;
656 	}
657 
658 	return ret;
659 }
660 
661 int ath12k_core_qmi_firmware_ready(struct ath12k_base *ab)
662 {
663 	int ret;
664 
665 	ret = ath12k_core_start_firmware(ab, ATH12K_FIRMWARE_MODE_NORMAL);
666 	if (ret) {
667 		ath12k_err(ab, "failed to start firmware: %d\n", ret);
668 		return ret;
669 	}
670 
671 	ret = ath12k_ce_init_pipes(ab);
672 	if (ret) {
673 		ath12k_err(ab, "failed to initialize CE: %d\n", ret);
674 		goto err_firmware_stop;
675 	}
676 
677 	ret = ath12k_dp_alloc(ab);
678 	if (ret) {
679 		ath12k_err(ab, "failed to init DP: %d\n", ret);
680 		goto err_firmware_stop;
681 	}
682 
683 	mutex_lock(&ab->core_lock);
684 	ret = ath12k_core_start(ab, ATH12K_FIRMWARE_MODE_NORMAL);
685 	if (ret) {
686 		ath12k_err(ab, "failed to start core: %d\n", ret);
687 		goto err_dp_free;
688 	}
689 
690 	ret = ath12k_core_pdev_create(ab);
691 	if (ret) {
692 		ath12k_err(ab, "failed to create pdev core: %d\n", ret);
693 		goto err_core_stop;
694 	}
695 	ath12k_hif_irq_enable(ab);
696 
697 	ret = ath12k_core_rfkill_config(ab);
698 	if (ret && ret != -EOPNOTSUPP) {
699 		ath12k_err(ab, "failed to config rfkill: %d\n", ret);
700 		goto err_core_stop;
701 	}
702 
703 	mutex_unlock(&ab->core_lock);
704 
705 	return 0;
706 
707 err_core_stop:
708 	ath12k_core_stop(ab);
709 	ath12k_mac_destroy(ab);
710 err_dp_free:
711 	ath12k_dp_free(ab);
712 	mutex_unlock(&ab->core_lock);
713 err_firmware_stop:
714 	ath12k_qmi_firmware_stop(ab);
715 
716 	return ret;
717 }
718 
719 static int ath12k_core_reconfigure_on_crash(struct ath12k_base *ab)
720 {
721 	int ret;
722 
723 	mutex_lock(&ab->core_lock);
724 	ath12k_hif_irq_disable(ab);
725 	ath12k_dp_pdev_free(ab);
726 	ath12k_hif_stop(ab);
727 	ath12k_wmi_detach(ab);
728 	ath12k_dp_rx_pdev_reo_cleanup(ab);
729 	mutex_unlock(&ab->core_lock);
730 
731 	ath12k_dp_free(ab);
732 	ath12k_hal_srng_deinit(ab);
733 
734 	ab->free_vdev_map = (1LL << (ab->num_radios * TARGET_NUM_VDEVS)) - 1;
735 
736 	ret = ath12k_hal_srng_init(ab);
737 	if (ret)
738 		return ret;
739 
740 	clear_bit(ATH12K_FLAG_CRASH_FLUSH, &ab->dev_flags);
741 
742 	ret = ath12k_core_qmi_firmware_ready(ab);
743 	if (ret)
744 		goto err_hal_srng_deinit;
745 
746 	clear_bit(ATH12K_FLAG_RECOVERY, &ab->dev_flags);
747 
748 	return 0;
749 
750 err_hal_srng_deinit:
751 	ath12k_hal_srng_deinit(ab);
752 	return ret;
753 }
754 
755 static void ath12k_rfkill_work(struct work_struct *work)
756 {
757 	struct ath12k_base *ab = container_of(work, struct ath12k_base, rfkill_work);
758 	struct ath12k *ar;
759 	bool rfkill_radio_on;
760 	int i;
761 
762 	spin_lock_bh(&ab->base_lock);
763 	rfkill_radio_on = ab->rfkill_radio_on;
764 	spin_unlock_bh(&ab->base_lock);
765 
766 	for (i = 0; i < ab->num_radios; i++) {
767 		ar = ab->pdevs[i].ar;
768 		if (!ar)
769 			continue;
770 
771 		ath12k_mac_rfkill_enable_radio(ar, rfkill_radio_on);
772 		wiphy_rfkill_set_hw_state(ar->hw->wiphy, !rfkill_radio_on);
773 	}
774 }
775 
776 void ath12k_core_halt(struct ath12k *ar)
777 {
778 	struct ath12k_base *ab = ar->ab;
779 
780 	lockdep_assert_held(&ar->conf_mutex);
781 
782 	ar->num_created_vdevs = 0;
783 	ar->allocated_vdev_map = 0;
784 
785 	ath12k_mac_scan_finish(ar);
786 	ath12k_mac_peer_cleanup_all(ar);
787 	cancel_delayed_work_sync(&ar->scan.timeout);
788 	cancel_work_sync(&ar->regd_update_work);
789 	cancel_work_sync(&ab->rfkill_work);
790 
791 	rcu_assign_pointer(ab->pdevs_active[ar->pdev_idx], NULL);
792 	synchronize_rcu();
793 	INIT_LIST_HEAD(&ar->arvifs);
794 	idr_init(&ar->txmgmt_idr);
795 }
796 
797 static void ath12k_core_pre_reconfigure_recovery(struct ath12k_base *ab)
798 {
799 	struct ath12k *ar;
800 	struct ath12k_pdev *pdev;
801 	int i;
802 
803 	spin_lock_bh(&ab->base_lock);
804 	ab->stats.fw_crash_counter++;
805 	spin_unlock_bh(&ab->base_lock);
806 
807 	if (ab->is_reset)
808 		set_bit(ATH12K_FLAG_CRASH_FLUSH, &ab->dev_flags);
809 
810 	for (i = 0; i < ab->num_radios; i++) {
811 		pdev = &ab->pdevs[i];
812 		ar = pdev->ar;
813 		if (!ar || ar->state == ATH12K_STATE_OFF)
814 			continue;
815 
816 		ieee80211_stop_queues(ar->hw);
817 		ath12k_mac_drain_tx(ar);
818 		complete(&ar->scan.started);
819 		complete(&ar->scan.completed);
820 		complete(&ar->peer_assoc_done);
821 		complete(&ar->peer_delete_done);
822 		complete(&ar->install_key_done);
823 		complete(&ar->vdev_setup_done);
824 		complete(&ar->vdev_delete_done);
825 		complete(&ar->bss_survey_done);
826 
827 		wake_up(&ar->dp.tx_empty_waitq);
828 		idr_for_each(&ar->txmgmt_idr,
829 			     ath12k_mac_tx_mgmt_pending_free, ar);
830 		idr_destroy(&ar->txmgmt_idr);
831 		wake_up(&ar->txmgmt_empty_waitq);
832 	}
833 
834 	wake_up(&ab->wmi_ab.tx_credits_wq);
835 	wake_up(&ab->peer_mapping_wq);
836 }
837 
838 static void ath12k_core_post_reconfigure_recovery(struct ath12k_base *ab)
839 {
840 	struct ath12k *ar;
841 	struct ath12k_pdev *pdev;
842 	int i;
843 
844 	for (i = 0; i < ab->num_radios; i++) {
845 		pdev = &ab->pdevs[i];
846 		ar = pdev->ar;
847 		if (!ar || ar->state == ATH12K_STATE_OFF)
848 			continue;
849 
850 		mutex_lock(&ar->conf_mutex);
851 
852 		switch (ar->state) {
853 		case ATH12K_STATE_ON:
854 			ar->state = ATH12K_STATE_RESTARTING;
855 			ath12k_core_halt(ar);
856 			ieee80211_restart_hw(ar->hw);
857 			break;
858 		case ATH12K_STATE_OFF:
859 			ath12k_warn(ab,
860 				    "cannot restart radio %d that hasn't been started\n",
861 				    i);
862 			break;
863 		case ATH12K_STATE_RESTARTING:
864 			break;
865 		case ATH12K_STATE_RESTARTED:
866 			ar->state = ATH12K_STATE_WEDGED;
867 			fallthrough;
868 		case ATH12K_STATE_WEDGED:
869 			ath12k_warn(ab,
870 				    "device is wedged, will not restart radio %d\n", i);
871 			break;
872 		}
873 		mutex_unlock(&ar->conf_mutex);
874 	}
875 	complete(&ab->driver_recovery);
876 }
877 
878 static void ath12k_core_restart(struct work_struct *work)
879 {
880 	struct ath12k_base *ab = container_of(work, struct ath12k_base, restart_work);
881 	int ret;
882 
883 	if (!ab->is_reset)
884 		ath12k_core_pre_reconfigure_recovery(ab);
885 
886 	ret = ath12k_core_reconfigure_on_crash(ab);
887 	if (ret) {
888 		ath12k_err(ab, "failed to reconfigure driver on crash recovery\n");
889 		return;
890 	}
891 
892 	if (ab->is_reset)
893 		complete_all(&ab->reconfigure_complete);
894 
895 	if (!ab->is_reset)
896 		ath12k_core_post_reconfigure_recovery(ab);
897 }
898 
899 static void ath12k_core_reset(struct work_struct *work)
900 {
901 	struct ath12k_base *ab = container_of(work, struct ath12k_base, reset_work);
902 	int reset_count, fail_cont_count;
903 	long time_left;
904 
905 	if (!(test_bit(ATH12K_FLAG_REGISTERED, &ab->dev_flags))) {
906 		ath12k_warn(ab, "ignore reset dev flags 0x%lx\n", ab->dev_flags);
907 		return;
908 	}
909 
910 	/* Sometimes the recovery will fail and then the next all recovery fail,
911 	 * this is to avoid infinite recovery since it can not recovery success
912 	 */
913 	fail_cont_count = atomic_read(&ab->fail_cont_count);
914 
915 	if (fail_cont_count >= ATH12K_RESET_MAX_FAIL_COUNT_FINAL)
916 		return;
917 
918 	if (fail_cont_count >= ATH12K_RESET_MAX_FAIL_COUNT_FIRST &&
919 	    time_before(jiffies, ab->reset_fail_timeout))
920 		return;
921 
922 	reset_count = atomic_inc_return(&ab->reset_count);
923 
924 	if (reset_count > 1) {
925 		/* Sometimes it happened another reset worker before the previous one
926 		 * completed, then the second reset worker will destroy the previous one,
927 		 * thus below is to avoid that.
928 		 */
929 		ath12k_warn(ab, "already resetting count %d\n", reset_count);
930 
931 		reinit_completion(&ab->reset_complete);
932 		time_left = wait_for_completion_timeout(&ab->reset_complete,
933 							ATH12K_RESET_TIMEOUT_HZ);
934 		if (time_left) {
935 			ath12k_dbg(ab, ATH12K_DBG_BOOT, "to skip reset\n");
936 			atomic_dec(&ab->reset_count);
937 			return;
938 		}
939 
940 		ab->reset_fail_timeout = jiffies + ATH12K_RESET_FAIL_TIMEOUT_HZ;
941 		/* Record the continuous recovery fail count when recovery failed*/
942 		fail_cont_count = atomic_inc_return(&ab->fail_cont_count);
943 	}
944 
945 	ath12k_dbg(ab, ATH12K_DBG_BOOT, "reset starting\n");
946 
947 	ab->is_reset = true;
948 	atomic_set(&ab->recovery_start_count, 0);
949 	reinit_completion(&ab->recovery_start);
950 	atomic_set(&ab->recovery_count, 0);
951 
952 	ath12k_core_pre_reconfigure_recovery(ab);
953 
954 	reinit_completion(&ab->reconfigure_complete);
955 	ath12k_core_post_reconfigure_recovery(ab);
956 
957 	ath12k_dbg(ab, ATH12K_DBG_BOOT, "waiting recovery start...\n");
958 
959 	time_left = wait_for_completion_timeout(&ab->recovery_start,
960 						ATH12K_RECOVER_START_TIMEOUT_HZ);
961 
962 	ath12k_hif_power_down(ab);
963 	ath12k_hif_power_up(ab);
964 
965 	ath12k_dbg(ab, ATH12K_DBG_BOOT, "reset started\n");
966 }
967 
968 int ath12k_core_pre_init(struct ath12k_base *ab)
969 {
970 	int ret;
971 
972 	ret = ath12k_hw_init(ab);
973 	if (ret) {
974 		ath12k_err(ab, "failed to init hw params: %d\n", ret);
975 		return ret;
976 	}
977 
978 	return 0;
979 }
980 
981 int ath12k_core_init(struct ath12k_base *ab)
982 {
983 	int ret;
984 
985 	ret = ath12k_core_soc_create(ab);
986 	if (ret) {
987 		ath12k_err(ab, "failed to create soc core: %d\n", ret);
988 		return ret;
989 	}
990 
991 	return 0;
992 }
993 
994 void ath12k_core_deinit(struct ath12k_base *ab)
995 {
996 	mutex_lock(&ab->core_lock);
997 
998 	ath12k_core_pdev_destroy(ab);
999 	ath12k_core_stop(ab);
1000 
1001 	mutex_unlock(&ab->core_lock);
1002 
1003 	ath12k_hif_power_down(ab);
1004 	ath12k_mac_destroy(ab);
1005 	ath12k_core_soc_destroy(ab);
1006 }
1007 
1008 void ath12k_core_free(struct ath12k_base *ab)
1009 {
1010 	timer_delete_sync(&ab->rx_replenish_retry);
1011 	destroy_workqueue(ab->workqueue_aux);
1012 	destroy_workqueue(ab->workqueue);
1013 	kfree(ab);
1014 }
1015 
1016 struct ath12k_base *ath12k_core_alloc(struct device *dev, size_t priv_size,
1017 				      enum ath12k_bus bus)
1018 {
1019 	struct ath12k_base *ab;
1020 
1021 	ab = kzalloc(sizeof(*ab) + priv_size, GFP_KERNEL);
1022 	if (!ab)
1023 		return NULL;
1024 
1025 	init_completion(&ab->driver_recovery);
1026 
1027 	ab->workqueue = create_singlethread_workqueue("ath12k_wq");
1028 	if (!ab->workqueue)
1029 		goto err_sc_free;
1030 
1031 	ab->workqueue_aux = create_singlethread_workqueue("ath12k_aux_wq");
1032 	if (!ab->workqueue_aux)
1033 		goto err_free_wq;
1034 
1035 	mutex_init(&ab->core_lock);
1036 	spin_lock_init(&ab->base_lock);
1037 	init_completion(&ab->reset_complete);
1038 	init_completion(&ab->reconfigure_complete);
1039 	init_completion(&ab->recovery_start);
1040 
1041 	INIT_LIST_HEAD(&ab->peers);
1042 	init_waitqueue_head(&ab->peer_mapping_wq);
1043 	init_waitqueue_head(&ab->wmi_ab.tx_credits_wq);
1044 	INIT_WORK(&ab->restart_work, ath12k_core_restart);
1045 	INIT_WORK(&ab->reset_work, ath12k_core_reset);
1046 	INIT_WORK(&ab->rfkill_work, ath12k_rfkill_work);
1047 
1048 	timer_setup(&ab->rx_replenish_retry, ath12k_ce_rx_replenish_retry, 0);
1049 	init_completion(&ab->htc_suspend);
1050 
1051 	ab->dev = dev;
1052 	ab->hif.bus = bus;
1053 
1054 	return ab;
1055 
1056 err_free_wq:
1057 	destroy_workqueue(ab->workqueue);
1058 err_sc_free:
1059 	kfree(ab);
1060 	return NULL;
1061 }
1062 
1063 MODULE_DESCRIPTION("Core module for Qualcomm Atheros 802.11be wireless LAN cards.");
1064 MODULE_LICENSE("Dual BSD/GPL");
1065