xref: /linux/drivers/net/wireless/ath/ath12k/core.c (revision 4eca0ef49af9b2b0c52ef2b58e045ab34629796b)
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 bd_api;
364 	int ret;
365 
366 	ret = ath12k_core_create_board_name(ab, boardname, BOARD_NAME_SIZE);
367 	if (ret) {
368 		ath12k_err(ab, "failed to create board name: %d", ret);
369 		return ret;
370 	}
371 
372 	bd_api = 2;
373 	ret = ath12k_core_fetch_board_data_api_n(ab, bd, boardname);
374 	if (!ret)
375 		goto success;
376 
377 	bd_api = 1;
378 	ret = ath12k_core_fetch_board_data_api_1(ab, bd, ATH12K_DEFAULT_BOARD_FILE);
379 	if (ret) {
380 		ath12k_err(ab, "failed to fetch board-2.bin or board.bin from %s\n",
381 			   ab->hw_params->fw.dir);
382 		return ret;
383 	}
384 
385 success:
386 	ath12k_dbg(ab, ATH12K_DBG_BOOT, "using board api %d\n", bd_api);
387 	return 0;
388 }
389 
390 static void ath12k_core_stop(struct ath12k_base *ab)
391 {
392 	if (!test_bit(ATH12K_FLAG_CRASH_FLUSH, &ab->dev_flags))
393 		ath12k_qmi_firmware_stop(ab);
394 
395 	ath12k_hif_stop(ab);
396 	ath12k_wmi_detach(ab);
397 	ath12k_dp_rx_pdev_reo_cleanup(ab);
398 
399 	/* De-Init of components as needed */
400 }
401 
402 static void ath12k_core_check_bdfext(const struct dmi_header *hdr, void *data)
403 {
404 	struct ath12k_base *ab = data;
405 	const char *magic = ATH12K_SMBIOS_BDF_EXT_MAGIC;
406 	struct ath12k_smbios_bdf *smbios = (struct ath12k_smbios_bdf *)hdr;
407 	ssize_t copied;
408 	size_t len;
409 	int i;
410 
411 	if (ab->qmi.target.bdf_ext[0] != '\0')
412 		return;
413 
414 	if (hdr->type != ATH12K_SMBIOS_BDF_EXT_TYPE)
415 		return;
416 
417 	if (hdr->length != ATH12K_SMBIOS_BDF_EXT_LENGTH) {
418 		ath12k_dbg(ab, ATH12K_DBG_BOOT,
419 			   "wrong smbios bdf ext type length (%d).\n",
420 			   hdr->length);
421 		return;
422 	}
423 
424 	if (!smbios->bdf_enabled) {
425 		ath12k_dbg(ab, ATH12K_DBG_BOOT, "bdf variant name not found.\n");
426 		return;
427 	}
428 
429 	/* Only one string exists (per spec) */
430 	if (memcmp(smbios->bdf_ext, magic, strlen(magic)) != 0) {
431 		ath12k_dbg(ab, ATH12K_DBG_BOOT,
432 			   "bdf variant magic does not match.\n");
433 		return;
434 	}
435 
436 	len = min_t(size_t,
437 		    strlen(smbios->bdf_ext), sizeof(ab->qmi.target.bdf_ext));
438 	for (i = 0; i < len; i++) {
439 		if (!isascii(smbios->bdf_ext[i]) || !isprint(smbios->bdf_ext[i])) {
440 			ath12k_dbg(ab, ATH12K_DBG_BOOT,
441 				   "bdf variant name contains non ascii chars.\n");
442 			return;
443 		}
444 	}
445 
446 	/* Copy extension name without magic prefix */
447 	copied = strscpy(ab->qmi.target.bdf_ext, smbios->bdf_ext + strlen(magic),
448 			 sizeof(ab->qmi.target.bdf_ext));
449 	if (copied < 0) {
450 		ath12k_dbg(ab, ATH12K_DBG_BOOT,
451 			   "bdf variant string is longer than the buffer can accommodate\n");
452 		return;
453 	}
454 
455 	ath12k_dbg(ab, ATH12K_DBG_BOOT,
456 		   "found and validated bdf variant smbios_type 0x%x bdf %s\n",
457 		   ATH12K_SMBIOS_BDF_EXT_TYPE, ab->qmi.target.bdf_ext);
458 }
459 
460 int ath12k_core_check_smbios(struct ath12k_base *ab)
461 {
462 	ab->qmi.target.bdf_ext[0] = '\0';
463 	dmi_walk(ath12k_core_check_bdfext, ab);
464 
465 	if (ab->qmi.target.bdf_ext[0] == '\0')
466 		return -ENODATA;
467 
468 	return 0;
469 }
470 
471 static int ath12k_core_soc_create(struct ath12k_base *ab)
472 {
473 	int ret;
474 
475 	ret = ath12k_qmi_init_service(ab);
476 	if (ret) {
477 		ath12k_err(ab, "failed to initialize qmi :%d\n", ret);
478 		return ret;
479 	}
480 
481 	ret = ath12k_hif_power_up(ab);
482 	if (ret) {
483 		ath12k_err(ab, "failed to power up :%d\n", ret);
484 		goto err_qmi_deinit;
485 	}
486 
487 	return 0;
488 
489 err_qmi_deinit:
490 	ath12k_qmi_deinit_service(ab);
491 	return ret;
492 }
493 
494 static void ath12k_core_soc_destroy(struct ath12k_base *ab)
495 {
496 	ath12k_dp_free(ab);
497 	ath12k_reg_free(ab);
498 	ath12k_qmi_deinit_service(ab);
499 }
500 
501 static int ath12k_core_pdev_create(struct ath12k_base *ab)
502 {
503 	int ret;
504 
505 	ret = ath12k_mac_register(ab);
506 	if (ret) {
507 		ath12k_err(ab, "failed register the radio with mac80211: %d\n", ret);
508 		return ret;
509 	}
510 
511 	ret = ath12k_dp_pdev_alloc(ab);
512 	if (ret) {
513 		ath12k_err(ab, "failed to attach DP pdev: %d\n", ret);
514 		goto err_mac_unregister;
515 	}
516 
517 	return 0;
518 
519 err_mac_unregister:
520 	ath12k_mac_unregister(ab);
521 
522 	return ret;
523 }
524 
525 static void ath12k_core_pdev_destroy(struct ath12k_base *ab)
526 {
527 	ath12k_mac_unregister(ab);
528 	ath12k_hif_irq_disable(ab);
529 	ath12k_dp_pdev_free(ab);
530 }
531 
532 static int ath12k_core_start(struct ath12k_base *ab,
533 			     enum ath12k_firmware_mode mode)
534 {
535 	int ret;
536 
537 	ret = ath12k_wmi_attach(ab);
538 	if (ret) {
539 		ath12k_err(ab, "failed to attach wmi: %d\n", ret);
540 		return ret;
541 	}
542 
543 	ret = ath12k_htc_init(ab);
544 	if (ret) {
545 		ath12k_err(ab, "failed to init htc: %d\n", ret);
546 		goto err_wmi_detach;
547 	}
548 
549 	ret = ath12k_hif_start(ab);
550 	if (ret) {
551 		ath12k_err(ab, "failed to start HIF: %d\n", ret);
552 		goto err_wmi_detach;
553 	}
554 
555 	ret = ath12k_htc_wait_target(&ab->htc);
556 	if (ret) {
557 		ath12k_err(ab, "failed to connect to HTC: %d\n", ret);
558 		goto err_hif_stop;
559 	}
560 
561 	ret = ath12k_dp_htt_connect(&ab->dp);
562 	if (ret) {
563 		ath12k_err(ab, "failed to connect to HTT: %d\n", ret);
564 		goto err_hif_stop;
565 	}
566 
567 	ret = ath12k_wmi_connect(ab);
568 	if (ret) {
569 		ath12k_err(ab, "failed to connect wmi: %d\n", ret);
570 		goto err_hif_stop;
571 	}
572 
573 	ret = ath12k_htc_start(&ab->htc);
574 	if (ret) {
575 		ath12k_err(ab, "failed to start HTC: %d\n", ret);
576 		goto err_hif_stop;
577 	}
578 
579 	ret = ath12k_wmi_wait_for_service_ready(ab);
580 	if (ret) {
581 		ath12k_err(ab, "failed to receive wmi service ready event: %d\n",
582 			   ret);
583 		goto err_hif_stop;
584 	}
585 
586 	ret = ath12k_mac_allocate(ab);
587 	if (ret) {
588 		ath12k_err(ab, "failed to create new hw device with mac80211 :%d\n",
589 			   ret);
590 		goto err_hif_stop;
591 	}
592 
593 	ath12k_dp_cc_config(ab);
594 
595 	ath12k_dp_pdev_pre_alloc(ab);
596 
597 	ret = ath12k_dp_rx_pdev_reo_setup(ab);
598 	if (ret) {
599 		ath12k_err(ab, "failed to initialize reo destination rings: %d\n", ret);
600 		goto err_mac_destroy;
601 	}
602 
603 	ret = ath12k_wmi_cmd_init(ab);
604 	if (ret) {
605 		ath12k_err(ab, "failed to send wmi init cmd: %d\n", ret);
606 		goto err_reo_cleanup;
607 	}
608 
609 	ret = ath12k_wmi_wait_for_unified_ready(ab);
610 	if (ret) {
611 		ath12k_err(ab, "failed to receive wmi unified ready event: %d\n",
612 			   ret);
613 		goto err_reo_cleanup;
614 	}
615 
616 	/* put hardware to DBS mode */
617 	if (ab->hw_params->single_pdev_only) {
618 		ret = ath12k_wmi_set_hw_mode(ab, WMI_HOST_HW_MODE_DBS);
619 		if (ret) {
620 			ath12k_err(ab, "failed to send dbs mode: %d\n", ret);
621 			goto err_reo_cleanup;
622 		}
623 	}
624 
625 	ret = ath12k_dp_tx_htt_h2t_ver_req_msg(ab);
626 	if (ret) {
627 		ath12k_err(ab, "failed to send htt version request message: %d\n",
628 			   ret);
629 		goto err_reo_cleanup;
630 	}
631 
632 	return 0;
633 
634 err_reo_cleanup:
635 	ath12k_dp_rx_pdev_reo_cleanup(ab);
636 err_mac_destroy:
637 	ath12k_mac_destroy(ab);
638 err_hif_stop:
639 	ath12k_hif_stop(ab);
640 err_wmi_detach:
641 	ath12k_wmi_detach(ab);
642 	return ret;
643 }
644 
645 static int ath12k_core_start_firmware(struct ath12k_base *ab,
646 				      enum ath12k_firmware_mode mode)
647 {
648 	int ret;
649 
650 	ath12k_ce_get_shadow_config(ab, &ab->qmi.ce_cfg.shadow_reg_v3,
651 				    &ab->qmi.ce_cfg.shadow_reg_v3_len);
652 
653 	ret = ath12k_qmi_firmware_start(ab, mode);
654 	if (ret) {
655 		ath12k_err(ab, "failed to send firmware start: %d\n", ret);
656 		return ret;
657 	}
658 
659 	return ret;
660 }
661 
662 int ath12k_core_qmi_firmware_ready(struct ath12k_base *ab)
663 {
664 	int ret;
665 
666 	ret = ath12k_core_start_firmware(ab, ATH12K_FIRMWARE_MODE_NORMAL);
667 	if (ret) {
668 		ath12k_err(ab, "failed to start firmware: %d\n", ret);
669 		return ret;
670 	}
671 
672 	ret = ath12k_ce_init_pipes(ab);
673 	if (ret) {
674 		ath12k_err(ab, "failed to initialize CE: %d\n", ret);
675 		goto err_firmware_stop;
676 	}
677 
678 	ret = ath12k_dp_alloc(ab);
679 	if (ret) {
680 		ath12k_err(ab, "failed to init DP: %d\n", ret);
681 		goto err_firmware_stop;
682 	}
683 
684 	mutex_lock(&ab->core_lock);
685 	ret = ath12k_core_start(ab, ATH12K_FIRMWARE_MODE_NORMAL);
686 	if (ret) {
687 		ath12k_err(ab, "failed to start core: %d\n", ret);
688 		goto err_dp_free;
689 	}
690 
691 	ret = ath12k_core_pdev_create(ab);
692 	if (ret) {
693 		ath12k_err(ab, "failed to create pdev core: %d\n", ret);
694 		goto err_core_stop;
695 	}
696 	ath12k_hif_irq_enable(ab);
697 
698 	ret = ath12k_core_rfkill_config(ab);
699 	if (ret && ret != -EOPNOTSUPP) {
700 		ath12k_err(ab, "failed to config rfkill: %d\n", ret);
701 		goto err_core_stop;
702 	}
703 
704 	mutex_unlock(&ab->core_lock);
705 
706 	return 0;
707 
708 err_core_stop:
709 	ath12k_core_stop(ab);
710 	ath12k_mac_destroy(ab);
711 err_dp_free:
712 	ath12k_dp_free(ab);
713 	mutex_unlock(&ab->core_lock);
714 err_firmware_stop:
715 	ath12k_qmi_firmware_stop(ab);
716 
717 	return ret;
718 }
719 
720 static int ath12k_core_reconfigure_on_crash(struct ath12k_base *ab)
721 {
722 	int ret;
723 
724 	mutex_lock(&ab->core_lock);
725 	ath12k_hif_irq_disable(ab);
726 	ath12k_dp_pdev_free(ab);
727 	ath12k_hif_stop(ab);
728 	ath12k_wmi_detach(ab);
729 	ath12k_dp_rx_pdev_reo_cleanup(ab);
730 	mutex_unlock(&ab->core_lock);
731 
732 	ath12k_dp_free(ab);
733 	ath12k_hal_srng_deinit(ab);
734 
735 	ab->free_vdev_map = (1LL << (ab->num_radios * TARGET_NUM_VDEVS)) - 1;
736 
737 	ret = ath12k_hal_srng_init(ab);
738 	if (ret)
739 		return ret;
740 
741 	clear_bit(ATH12K_FLAG_CRASH_FLUSH, &ab->dev_flags);
742 
743 	ret = ath12k_core_qmi_firmware_ready(ab);
744 	if (ret)
745 		goto err_hal_srng_deinit;
746 
747 	clear_bit(ATH12K_FLAG_RECOVERY, &ab->dev_flags);
748 
749 	return 0;
750 
751 err_hal_srng_deinit:
752 	ath12k_hal_srng_deinit(ab);
753 	return ret;
754 }
755 
756 static void ath12k_rfkill_work(struct work_struct *work)
757 {
758 	struct ath12k_base *ab = container_of(work, struct ath12k_base, rfkill_work);
759 	struct ath12k *ar;
760 	bool rfkill_radio_on;
761 	int i;
762 
763 	spin_lock_bh(&ab->base_lock);
764 	rfkill_radio_on = ab->rfkill_radio_on;
765 	spin_unlock_bh(&ab->base_lock);
766 
767 	for (i = 0; i < ab->num_radios; i++) {
768 		ar = ab->pdevs[i].ar;
769 		if (!ar)
770 			continue;
771 
772 		ath12k_mac_rfkill_enable_radio(ar, rfkill_radio_on);
773 		wiphy_rfkill_set_hw_state(ar->hw->wiphy, !rfkill_radio_on);
774 	}
775 }
776 
777 void ath12k_core_halt(struct ath12k *ar)
778 {
779 	struct ath12k_base *ab = ar->ab;
780 
781 	lockdep_assert_held(&ar->conf_mutex);
782 
783 	ar->num_created_vdevs = 0;
784 	ar->allocated_vdev_map = 0;
785 
786 	ath12k_mac_scan_finish(ar);
787 	ath12k_mac_peer_cleanup_all(ar);
788 	cancel_delayed_work_sync(&ar->scan.timeout);
789 	cancel_work_sync(&ar->regd_update_work);
790 	cancel_work_sync(&ab->rfkill_work);
791 
792 	rcu_assign_pointer(ab->pdevs_active[ar->pdev_idx], NULL);
793 	synchronize_rcu();
794 	INIT_LIST_HEAD(&ar->arvifs);
795 	idr_init(&ar->txmgmt_idr);
796 }
797 
798 static void ath12k_core_pre_reconfigure_recovery(struct ath12k_base *ab)
799 {
800 	struct ath12k *ar;
801 	struct ath12k_pdev *pdev;
802 	int i;
803 
804 	spin_lock_bh(&ab->base_lock);
805 	ab->stats.fw_crash_counter++;
806 	spin_unlock_bh(&ab->base_lock);
807 
808 	if (ab->is_reset)
809 		set_bit(ATH12K_FLAG_CRASH_FLUSH, &ab->dev_flags);
810 
811 	for (i = 0; i < ab->num_radios; i++) {
812 		pdev = &ab->pdevs[i];
813 		ar = pdev->ar;
814 		if (!ar || ar->state == ATH12K_STATE_OFF)
815 			continue;
816 
817 		ieee80211_stop_queues(ar->hw);
818 		ath12k_mac_drain_tx(ar);
819 		complete(&ar->scan.started);
820 		complete(&ar->scan.completed);
821 		complete(&ar->peer_assoc_done);
822 		complete(&ar->peer_delete_done);
823 		complete(&ar->install_key_done);
824 		complete(&ar->vdev_setup_done);
825 		complete(&ar->vdev_delete_done);
826 		complete(&ar->bss_survey_done);
827 
828 		wake_up(&ar->dp.tx_empty_waitq);
829 		idr_for_each(&ar->txmgmt_idr,
830 			     ath12k_mac_tx_mgmt_pending_free, ar);
831 		idr_destroy(&ar->txmgmt_idr);
832 		wake_up(&ar->txmgmt_empty_waitq);
833 	}
834 
835 	wake_up(&ab->wmi_ab.tx_credits_wq);
836 	wake_up(&ab->peer_mapping_wq);
837 }
838 
839 static void ath12k_core_post_reconfigure_recovery(struct ath12k_base *ab)
840 {
841 	struct ath12k *ar;
842 	struct ath12k_pdev *pdev;
843 	int i;
844 
845 	for (i = 0; i < ab->num_radios; i++) {
846 		pdev = &ab->pdevs[i];
847 		ar = pdev->ar;
848 		if (!ar || ar->state == ATH12K_STATE_OFF)
849 			continue;
850 
851 		mutex_lock(&ar->conf_mutex);
852 
853 		switch (ar->state) {
854 		case ATH12K_STATE_ON:
855 			ar->state = ATH12K_STATE_RESTARTING;
856 			ath12k_core_halt(ar);
857 			ieee80211_restart_hw(ar->hw);
858 			break;
859 		case ATH12K_STATE_OFF:
860 			ath12k_warn(ab,
861 				    "cannot restart radio %d that hasn't been started\n",
862 				    i);
863 			break;
864 		case ATH12K_STATE_RESTARTING:
865 			break;
866 		case ATH12K_STATE_RESTARTED:
867 			ar->state = ATH12K_STATE_WEDGED;
868 			fallthrough;
869 		case ATH12K_STATE_WEDGED:
870 			ath12k_warn(ab,
871 				    "device is wedged, will not restart radio %d\n", i);
872 			break;
873 		}
874 		mutex_unlock(&ar->conf_mutex);
875 	}
876 	complete(&ab->driver_recovery);
877 }
878 
879 static void ath12k_core_restart(struct work_struct *work)
880 {
881 	struct ath12k_base *ab = container_of(work, struct ath12k_base, restart_work);
882 	int ret;
883 
884 	if (!ab->is_reset)
885 		ath12k_core_pre_reconfigure_recovery(ab);
886 
887 	ret = ath12k_core_reconfigure_on_crash(ab);
888 	if (ret) {
889 		ath12k_err(ab, "failed to reconfigure driver on crash recovery\n");
890 		return;
891 	}
892 
893 	if (ab->is_reset)
894 		complete_all(&ab->reconfigure_complete);
895 
896 	if (!ab->is_reset)
897 		ath12k_core_post_reconfigure_recovery(ab);
898 }
899 
900 static void ath12k_core_reset(struct work_struct *work)
901 {
902 	struct ath12k_base *ab = container_of(work, struct ath12k_base, reset_work);
903 	int reset_count, fail_cont_count;
904 	long time_left;
905 
906 	if (!(test_bit(ATH12K_FLAG_REGISTERED, &ab->dev_flags))) {
907 		ath12k_warn(ab, "ignore reset dev flags 0x%lx\n", ab->dev_flags);
908 		return;
909 	}
910 
911 	/* Sometimes the recovery will fail and then the next all recovery fail,
912 	 * this is to avoid infinite recovery since it can not recovery success
913 	 */
914 	fail_cont_count = atomic_read(&ab->fail_cont_count);
915 
916 	if (fail_cont_count >= ATH12K_RESET_MAX_FAIL_COUNT_FINAL)
917 		return;
918 
919 	if (fail_cont_count >= ATH12K_RESET_MAX_FAIL_COUNT_FIRST &&
920 	    time_before(jiffies, ab->reset_fail_timeout))
921 		return;
922 
923 	reset_count = atomic_inc_return(&ab->reset_count);
924 
925 	if (reset_count > 1) {
926 		/* Sometimes it happened another reset worker before the previous one
927 		 * completed, then the second reset worker will destroy the previous one,
928 		 * thus below is to avoid that.
929 		 */
930 		ath12k_warn(ab, "already resetting count %d\n", reset_count);
931 
932 		reinit_completion(&ab->reset_complete);
933 		time_left = wait_for_completion_timeout(&ab->reset_complete,
934 							ATH12K_RESET_TIMEOUT_HZ);
935 		if (time_left) {
936 			ath12k_dbg(ab, ATH12K_DBG_BOOT, "to skip reset\n");
937 			atomic_dec(&ab->reset_count);
938 			return;
939 		}
940 
941 		ab->reset_fail_timeout = jiffies + ATH12K_RESET_FAIL_TIMEOUT_HZ;
942 		/* Record the continuous recovery fail count when recovery failed*/
943 		fail_cont_count = atomic_inc_return(&ab->fail_cont_count);
944 	}
945 
946 	ath12k_dbg(ab, ATH12K_DBG_BOOT, "reset starting\n");
947 
948 	ab->is_reset = true;
949 	atomic_set(&ab->recovery_start_count, 0);
950 	reinit_completion(&ab->recovery_start);
951 	atomic_set(&ab->recovery_count, 0);
952 
953 	ath12k_core_pre_reconfigure_recovery(ab);
954 
955 	reinit_completion(&ab->reconfigure_complete);
956 	ath12k_core_post_reconfigure_recovery(ab);
957 
958 	ath12k_dbg(ab, ATH12K_DBG_BOOT, "waiting recovery start...\n");
959 
960 	time_left = wait_for_completion_timeout(&ab->recovery_start,
961 						ATH12K_RECOVER_START_TIMEOUT_HZ);
962 
963 	ath12k_hif_power_down(ab);
964 	ath12k_qmi_free_resource(ab);
965 	ath12k_hif_power_up(ab);
966 
967 	ath12k_dbg(ab, ATH12K_DBG_BOOT, "reset started\n");
968 }
969 
970 int ath12k_core_pre_init(struct ath12k_base *ab)
971 {
972 	int ret;
973 
974 	ret = ath12k_hw_init(ab);
975 	if (ret) {
976 		ath12k_err(ab, "failed to init hw params: %d\n", ret);
977 		return ret;
978 	}
979 
980 	return 0;
981 }
982 
983 int ath12k_core_init(struct ath12k_base *ab)
984 {
985 	int ret;
986 
987 	ret = ath12k_core_soc_create(ab);
988 	if (ret) {
989 		ath12k_err(ab, "failed to create soc core: %d\n", ret);
990 		return ret;
991 	}
992 
993 	return 0;
994 }
995 
996 void ath12k_core_deinit(struct ath12k_base *ab)
997 {
998 	mutex_lock(&ab->core_lock);
999 
1000 	ath12k_core_pdev_destroy(ab);
1001 	ath12k_core_stop(ab);
1002 
1003 	mutex_unlock(&ab->core_lock);
1004 
1005 	ath12k_hif_power_down(ab);
1006 	ath12k_mac_destroy(ab);
1007 	ath12k_core_soc_destroy(ab);
1008 }
1009 
1010 void ath12k_core_free(struct ath12k_base *ab)
1011 {
1012 	timer_delete_sync(&ab->rx_replenish_retry);
1013 	destroy_workqueue(ab->workqueue_aux);
1014 	destroy_workqueue(ab->workqueue);
1015 	kfree(ab);
1016 }
1017 
1018 struct ath12k_base *ath12k_core_alloc(struct device *dev, size_t priv_size,
1019 				      enum ath12k_bus bus)
1020 {
1021 	struct ath12k_base *ab;
1022 
1023 	ab = kzalloc(sizeof(*ab) + priv_size, GFP_KERNEL);
1024 	if (!ab)
1025 		return NULL;
1026 
1027 	init_completion(&ab->driver_recovery);
1028 
1029 	ab->workqueue = create_singlethread_workqueue("ath12k_wq");
1030 	if (!ab->workqueue)
1031 		goto err_sc_free;
1032 
1033 	ab->workqueue_aux = create_singlethread_workqueue("ath12k_aux_wq");
1034 	if (!ab->workqueue_aux)
1035 		goto err_free_wq;
1036 
1037 	mutex_init(&ab->core_lock);
1038 	spin_lock_init(&ab->base_lock);
1039 	init_completion(&ab->reset_complete);
1040 	init_completion(&ab->reconfigure_complete);
1041 	init_completion(&ab->recovery_start);
1042 
1043 	INIT_LIST_HEAD(&ab->peers);
1044 	init_waitqueue_head(&ab->peer_mapping_wq);
1045 	init_waitqueue_head(&ab->wmi_ab.tx_credits_wq);
1046 	INIT_WORK(&ab->restart_work, ath12k_core_restart);
1047 	INIT_WORK(&ab->reset_work, ath12k_core_reset);
1048 	INIT_WORK(&ab->rfkill_work, ath12k_rfkill_work);
1049 
1050 	timer_setup(&ab->rx_replenish_retry, ath12k_ce_rx_replenish_retry, 0);
1051 	init_completion(&ab->htc_suspend);
1052 
1053 	ab->dev = dev;
1054 	ab->hif.bus = bus;
1055 
1056 	return ab;
1057 
1058 err_free_wq:
1059 	destroy_workqueue(ab->workqueue);
1060 err_sc_free:
1061 	kfree(ab);
1062 	return NULL;
1063 }
1064 
1065 MODULE_DESCRIPTION("Core module for Qualcomm Atheros 802.11be wireless LAN cards.");
1066 MODULE_LICENSE("Dual BSD/GPL");
1067