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