xref: /linux/drivers/net/wireless/ath/ath12k/core.c (revision 2d7eaeb05ae265bd64270376e6a2c0d80244771f)
1 // SPDX-License-Identifier: BSD-3-Clause-Clear
2 /*
3  * Copyright (c) 2018-2021 The Linux Foundation. All rights reserved.
4  * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
5  */
6 
7 #include <linux/export.h>
8 #include <linux/module.h>
9 #include <linux/slab.h>
10 #include <linux/remoteproc.h>
11 #include <linux/firmware.h>
12 #include <linux/of.h>
13 #include <linux/of_graph.h>
14 #include "ahb.h"
15 #include "core.h"
16 #include "dp_tx.h"
17 #include "dp_rx.h"
18 #include "debug.h"
19 #include "debugfs.h"
20 #include "fw.h"
21 #include "hif.h"
22 #include "pci.h"
23 #include "wow.h"
24 #include "dp_cmn.h"
25 #include "peer.h"
26 #include "qmi.h"
27 
28 unsigned int ath12k_debug_mask;
29 module_param_named(debug_mask, ath12k_debug_mask, uint, 0644);
30 MODULE_PARM_DESC(debug_mask, "Debugging mask");
31 EXPORT_SYMBOL(ath12k_debug_mask);
32 
33 bool ath12k_ftm_mode;
34 module_param_named(ftm_mode, ath12k_ftm_mode, bool, 0444);
35 MODULE_PARM_DESC(ftm_mode, "Boots up in factory test mode");
36 EXPORT_SYMBOL(ath12k_ftm_mode);
37 
38 /* protected with ath12k_hw_group_mutex */
39 static struct list_head ath12k_hw_group_list = LIST_HEAD_INIT(ath12k_hw_group_list);
40 
41 static DEFINE_MUTEX(ath12k_hw_group_mutex);
42 
43 static const struct
44 ath12k_mem_profile_based_param ath12k_mem_profile_based_param[] = {
45 [ATH12K_QMI_MEMORY_MODE_DEFAULT] = {
46 		.num_vdevs = 17,
47 		.max_client_single = 512,
48 		.max_client_dbs = 128,
49 		.max_client_dbs_sbs = 128,
50 		.dp_params = {
51 			.tx_comp_ring_size = 32768,
52 			.rxdma_monitor_buf_ring_size = 4096,
53 			.rxdma_monitor_dst_ring_size = 8192,
54 			.num_pool_tx_desc = 32768,
55 			.rx_desc_count = 12288,
56 			.rx_release_ring_size = 16384,
57 			.rxdma_buf_ring_size = 8192,
58 		},
59 	},
60 [ATH12K_QMI_MEMORY_MODE_LOW_512_M] = {
61 		.num_vdevs = 9,
62 		.max_client_single = 128,
63 		.max_client_dbs = 64,
64 		.max_client_dbs_sbs = 64,
65 		.dp_params = {
66 			.tx_comp_ring_size = 16384,
67 			.rxdma_monitor_buf_ring_size = 256,
68 			.rxdma_monitor_dst_ring_size = 512,
69 			.num_pool_tx_desc = 16384,
70 			.rx_desc_count = 6144,
71 			.rx_release_ring_size = 8192,
72 			.rxdma_buf_ring_size = 4096,
73 		},
74 	},
75 };
76 
77 /*
78  * On these machines the OS cannot control the WCN7850 hardware rfkill
79  * input, which reads as "radio off", so hardware rfkill must stay
80  * disabled. Ideally this would come from the connector node of the board
81  * DT, but that binding does not exist yet.
82  */
83 static const struct ath12k_core_broken_rfkill_table {
84 	enum ath12k_hw_rev hw_rev;
85 	const char *compatible;
86 } ath12k_core_broken_rfkill_table[] = {
87 	{ ATH12K_HW_WCN7850_HW20, "microsoft,denali" },
88 	{ ATH12K_HW_WCN7850_HW20, "microsoft,romulus13" },
89 	{ ATH12K_HW_WCN7850_HW20, "microsoft,romulus15" },
90 	{ /* Sentinel */ }
91 };
92 
93 static bool ath12k_core_rfkill_is_broken(struct ath12k_base *ab)
94 {
95 	const struct ath12k_core_broken_rfkill_table *entry;
96 
97 	for (entry = ath12k_core_broken_rfkill_table; entry->compatible; entry++) {
98 		if (ab->hw_rev == entry->hw_rev &&
99 		    of_machine_is_compatible(entry->compatible))
100 			return true;
101 	}
102 
103 	return false;
104 }
105 
106 static int ath12k_core_rfkill_config(struct ath12k_base *ab)
107 {
108 	struct ath12k *ar;
109 	int ret = 0, i;
110 
111 	if (!(ab->target_caps.sys_cap_info & WMI_SYS_CAP_INFO_RFKILL))
112 		return 0;
113 
114 	if (ath12k_acpi_get_disable_rfkill(ab))
115 		return 0;
116 
117 	if (ath12k_core_rfkill_is_broken(ab))
118 		return 0;
119 
120 	for (i = 0; i < ab->num_radios; i++) {
121 		ar = ab->pdevs[i].ar;
122 
123 		ret = ath12k_mac_rfkill_config(ar);
124 		if (ret && ret != -EOPNOTSUPP) {
125 			ath12k_warn(ab, "failed to configure rfkill: %d", ret);
126 			return ret;
127 		}
128 	}
129 
130 	return ret;
131 }
132 
133 /* Check if we need to continue with suspend/resume operation.
134  * Return:
135  *	a negative value: error happens and don't continue.
136  *	0:  no error but don't continue.
137  *	positive value: no error and do continue.
138  */
139 static int ath12k_core_continue_suspend_resume(struct ath12k_base *ab)
140 {
141 	struct ath12k *ar;
142 
143 	if (!ab->hw_params->supports_suspend)
144 		return -EOPNOTSUPP;
145 
146 	/* so far single_pdev_only chips have supports_suspend as true
147 	 * so pass 0 as a dummy pdev_id here.
148 	 */
149 	ar = ab->pdevs[0].ar;
150 	if (!ar || !ar->ah || ar->ah->state != ATH12K_HW_STATE_OFF)
151 		return 0;
152 
153 	return 1;
154 }
155 
156 int ath12k_core_suspend(struct ath12k_base *ab)
157 {
158 	struct ath12k *ar;
159 	int ret, i;
160 
161 	ret = ath12k_core_continue_suspend_resume(ab);
162 	if (ret <= 0)
163 		return ret;
164 
165 	for (i = 0; i < ab->num_radios; i++) {
166 		ar = ab->pdevs[i].ar;
167 		if (!ar)
168 			continue;
169 
170 		wiphy_lock(ath12k_ar_to_hw(ar)->wiphy);
171 
172 		ret = ath12k_mac_wait_tx_complete(ar);
173 		if (ret) {
174 			wiphy_unlock(ath12k_ar_to_hw(ar)->wiphy);
175 			ath12k_warn(ab, "failed to wait tx complete: %d\n", ret);
176 			return ret;
177 		}
178 
179 		wiphy_unlock(ath12k_ar_to_hw(ar)->wiphy);
180 	}
181 
182 	/* PM framework skips suspend_late/resume_early callbacks
183 	 * if other devices report errors in their suspend callbacks.
184 	 * However ath12k_core_resume() would still be called because
185 	 * here we return success thus kernel put us on dpm_suspended_list.
186 	 * Since we won't go through a power down/up cycle, there is
187 	 * no chance to call complete(&ab->restart_completed) in
188 	 * ath12k_core_restart(), making ath12k_core_resume() timeout.
189 	 * So call it here to avoid this issue. This also works in case
190 	 * no error happens thus suspend_late/resume_early get called,
191 	 * because it will be reinitialized in ath12k_core_resume_early().
192 	 */
193 	complete(&ab->restart_completed);
194 
195 	return 0;
196 }
197 EXPORT_SYMBOL(ath12k_core_suspend);
198 
199 int ath12k_core_suspend_late(struct ath12k_base *ab)
200 {
201 	int ret;
202 
203 	ret = ath12k_core_continue_suspend_resume(ab);
204 	if (ret <= 0)
205 		return ret;
206 
207 	ath12k_acpi_stop(ab);
208 
209 	ath12k_hif_irq_disable(ab);
210 	ath12k_hif_ce_irq_disable(ab);
211 
212 	ath12k_hif_power_down(ab, true);
213 
214 	return 0;
215 }
216 EXPORT_SYMBOL(ath12k_core_suspend_late);
217 
218 int ath12k_core_resume_early(struct ath12k_base *ab)
219 {
220 	int ret;
221 
222 	ret = ath12k_core_continue_suspend_resume(ab);
223 	if (ret <= 0)
224 		return ret;
225 
226 	reinit_completion(&ab->restart_completed);
227 	ret = ath12k_hif_power_up(ab);
228 	if (ret)
229 		ath12k_warn(ab, "failed to power up hif during resume: %d\n", ret);
230 
231 	return ret;
232 }
233 EXPORT_SYMBOL(ath12k_core_resume_early);
234 
235 int ath12k_core_resume(struct ath12k_base *ab)
236 {
237 	long time_left;
238 	int ret;
239 
240 	ret = ath12k_core_continue_suspend_resume(ab);
241 	if (ret <= 0)
242 		return ret;
243 
244 	time_left = wait_for_completion_timeout(&ab->restart_completed,
245 						ATH12K_RESET_TIMEOUT_HZ);
246 	if (time_left == 0) {
247 		ath12k_warn(ab, "timeout while waiting for restart complete");
248 		return -ETIMEDOUT;
249 	}
250 
251 	return 0;
252 }
253 EXPORT_SYMBOL(ath12k_core_resume);
254 
255 static int __ath12k_core_create_board_name(struct ath12k_base *ab, char *name,
256 					   size_t name_len, bool with_variant,
257 					   bool bus_type_mode, bool with_default)
258 {
259 	/* strlen(',variant=') + strlen(ab->qmi.target.bdf_ext) */
260 	char variant[9 + ATH12K_QMI_BDF_EXT_STR_LENGTH] = {};
261 
262 	if (with_variant && ab->qmi.target.bdf_ext[0] != '\0')
263 		scnprintf(variant, sizeof(variant), ",variant=%s",
264 			  ab->qmi.target.bdf_ext);
265 
266 	switch (ab->id.bdf_search) {
267 	case ATH12K_BDF_SEARCH_BUS_AND_BOARD:
268 		if (bus_type_mode)
269 			scnprintf(name, name_len,
270 				  "bus=%s",
271 				  ath12k_bus_str(ab->hif.bus));
272 		else
273 			scnprintf(name, name_len,
274 				  "bus=%s,vendor=%04x,device=%04x,subsystem-vendor=%04x,subsystem-device=%04x,qmi-chip-id=%d,qmi-board-id=%d%s",
275 				  ath12k_bus_str(ab->hif.bus),
276 				  ab->id.vendor, ab->id.device,
277 				  ab->id.subsystem_vendor,
278 				  ab->id.subsystem_device,
279 				  ab->qmi.target.chip_id,
280 				  ab->qmi.target.board_id,
281 				  variant);
282 		break;
283 	default:
284 		scnprintf(name, name_len,
285 			  "bus=%s,qmi-chip-id=%d,qmi-board-id=%d%s",
286 			  ath12k_bus_str(ab->hif.bus),
287 			  ab->qmi.target.chip_id,
288 			  with_default ?
289 			  ATH12K_BOARD_ID_DEFAULT : ab->qmi.target.board_id,
290 			  variant);
291 		break;
292 	}
293 
294 	ath12k_dbg(ab, ATH12K_DBG_BOOT, "boot using board name '%s'\n", name);
295 
296 	return 0;
297 }
298 
299 static int ath12k_core_create_board_name(struct ath12k_base *ab, char *name,
300 					 size_t name_len)
301 {
302 	return __ath12k_core_create_board_name(ab, name, name_len, true, false, false);
303 }
304 
305 static int ath12k_core_create_fallback_board_name(struct ath12k_base *ab, char *name,
306 						  size_t name_len)
307 {
308 	return __ath12k_core_create_board_name(ab, name, name_len, false, false, true);
309 }
310 
311 static int ath12k_core_create_bus_type_board_name(struct ath12k_base *ab, char *name,
312 						  size_t name_len)
313 {
314 	return __ath12k_core_create_board_name(ab, name, name_len, false, true, true);
315 }
316 
317 const struct firmware *ath12k_core_firmware_request(struct ath12k_base *ab,
318 						    const char *file)
319 {
320 	const struct firmware *fw;
321 	char path[100];
322 	int ret;
323 
324 	if (!file)
325 		return ERR_PTR(-ENOENT);
326 
327 	ath12k_core_create_firmware_path(ab, file, path, sizeof(path));
328 
329 	ret = firmware_request_nowarn(&fw, path, ab->dev);
330 	if (ret)
331 		return ERR_PTR(ret);
332 
333 	ath12k_dbg(ab, ATH12K_DBG_BOOT, "boot firmware request %s size %zu\n",
334 		   path, fw->size);
335 
336 	return fw;
337 }
338 
339 void ath12k_core_free_bdf(struct ath12k_base *ab, struct ath12k_board_data *bd)
340 {
341 	if (!IS_ERR(bd->fw))
342 		release_firmware(bd->fw);
343 
344 	memset(bd, 0, sizeof(*bd));
345 }
346 
347 static int ath12k_core_parse_bd_ie_board(struct ath12k_base *ab,
348 					 struct ath12k_board_data *bd,
349 					 const void *buf, size_t buf_len,
350 					 const char *boardname,
351 					 int ie_id,
352 					 int name_id,
353 					 int data_id)
354 {
355 	const struct ath12k_fw_ie *hdr;
356 	bool name_match_found;
357 	int ret, board_ie_id;
358 	size_t board_ie_len;
359 	const void *board_ie_data;
360 
361 	name_match_found = false;
362 
363 	/* go through ATH12K_BD_IE_BOARD_/ATH12K_BD_IE_REGDB_ elements */
364 	while (buf_len > sizeof(struct ath12k_fw_ie)) {
365 		hdr = buf;
366 		board_ie_id = le32_to_cpu(hdr->id);
367 		board_ie_len = le32_to_cpu(hdr->len);
368 		board_ie_data = hdr->data;
369 
370 		buf_len -= sizeof(*hdr);
371 		buf += sizeof(*hdr);
372 
373 		if (buf_len < ALIGN(board_ie_len, 4)) {
374 			ath12k_err(ab, "invalid %s length: %zu < %zu\n",
375 				   ath12k_bd_ie_type_str(ie_id),
376 				   buf_len, ALIGN(board_ie_len, 4));
377 			ret = -EINVAL;
378 			goto out;
379 		}
380 
381 		if (board_ie_id == name_id) {
382 			ath12k_dbg_dump(ab, ATH12K_DBG_BOOT, "board name", "",
383 					board_ie_data, board_ie_len);
384 
385 			if (board_ie_len != strlen(boardname))
386 				goto next;
387 
388 			ret = memcmp(board_ie_data, boardname, strlen(boardname));
389 			if (ret)
390 				goto next;
391 
392 			name_match_found = true;
393 			ath12k_dbg(ab, ATH12K_DBG_BOOT,
394 				   "boot found match %s for name '%s'",
395 				   ath12k_bd_ie_type_str(ie_id),
396 				   boardname);
397 		} else if (board_ie_id == data_id) {
398 			if (!name_match_found)
399 				/* no match found */
400 				goto next;
401 
402 			ath12k_dbg(ab, ATH12K_DBG_BOOT,
403 				   "boot found %s for '%s'",
404 				   ath12k_bd_ie_type_str(ie_id),
405 				   boardname);
406 
407 			bd->data = board_ie_data;
408 			bd->len = board_ie_len;
409 
410 			ret = 0;
411 			goto out;
412 		} else {
413 			ath12k_warn(ab, "unknown %s id found: %d\n",
414 				    ath12k_bd_ie_type_str(ie_id),
415 				    board_ie_id);
416 		}
417 next:
418 		/* jump over the padding */
419 		board_ie_len = ALIGN(board_ie_len, 4);
420 
421 		buf_len -= board_ie_len;
422 		buf += board_ie_len;
423 	}
424 
425 	/* no match found */
426 	ret = -ENOENT;
427 
428 out:
429 	return ret;
430 }
431 
432 static int ath12k_core_fetch_board_data_api_n(struct ath12k_base *ab,
433 					      struct ath12k_board_data *bd,
434 					      const char *boardname,
435 					      int ie_id_match,
436 					      int name_id,
437 					      int data_id)
438 {
439 	size_t len, magic_len;
440 	const u8 *data;
441 	char *filename, filepath[100];
442 	size_t ie_len;
443 	struct ath12k_fw_ie *hdr;
444 	int ret, ie_id;
445 
446 	filename = ATH12K_BOARD_API2_FILE;
447 
448 	if (!bd->fw)
449 		bd->fw = ath12k_core_firmware_request(ab, filename);
450 
451 	if (IS_ERR(bd->fw))
452 		return PTR_ERR(bd->fw);
453 
454 	data = bd->fw->data;
455 	len = bd->fw->size;
456 
457 	ath12k_core_create_firmware_path(ab, filename,
458 					 filepath, sizeof(filepath));
459 
460 	/* magic has extra null byte padded */
461 	magic_len = strlen(ATH12K_BOARD_MAGIC) + 1;
462 	if (len < magic_len) {
463 		ath12k_err(ab, "failed to find magic value in %s, file too short: %zu\n",
464 			   filepath, len);
465 		ret = -EINVAL;
466 		goto err;
467 	}
468 
469 	if (memcmp(data, ATH12K_BOARD_MAGIC, magic_len)) {
470 		ath12k_err(ab, "found invalid board magic\n");
471 		ret = -EINVAL;
472 		goto err;
473 	}
474 
475 	/* magic is padded to 4 bytes */
476 	magic_len = ALIGN(magic_len, 4);
477 	if (len < magic_len) {
478 		ath12k_err(ab, "failed: %s too small to contain board data, len: %zu\n",
479 			   filepath, len);
480 		ret = -EINVAL;
481 		goto err;
482 	}
483 
484 	data += magic_len;
485 	len -= magic_len;
486 
487 	while (len > sizeof(struct ath12k_fw_ie)) {
488 		hdr = (struct ath12k_fw_ie *)data;
489 		ie_id = le32_to_cpu(hdr->id);
490 		ie_len = le32_to_cpu(hdr->len);
491 
492 		len -= sizeof(*hdr);
493 		data = hdr->data;
494 
495 		if (len < ALIGN(ie_len, 4)) {
496 			ath12k_err(ab, "invalid length for board ie_id %d ie_len %zu len %zu\n",
497 				   ie_id, ie_len, len);
498 			ret = -EINVAL;
499 			goto err;
500 		}
501 
502 		if (ie_id == ie_id_match) {
503 			ret = ath12k_core_parse_bd_ie_board(ab, bd, data,
504 							    ie_len,
505 							    boardname,
506 							    ie_id_match,
507 							    name_id,
508 							    data_id);
509 			if (ret == -ENOENT)
510 				/* no match found, continue */
511 				goto next;
512 			else if (ret)
513 				/* there was an error, bail out */
514 				goto err;
515 			/* either found or error, so stop searching */
516 			goto out;
517 		}
518 next:
519 		/* jump over the padding */
520 		ie_len = ALIGN(ie_len, 4);
521 
522 		len -= ie_len;
523 		data += ie_len;
524 	}
525 
526 out:
527 	if (!bd->data || !bd->len) {
528 		ath12k_dbg(ab, ATH12K_DBG_BOOT,
529 			   "failed to fetch %s for %s from %s\n",
530 			   ath12k_bd_ie_type_str(ie_id_match),
531 			   boardname, filepath);
532 		ret = -ENODATA;
533 		goto err;
534 	}
535 
536 	return 0;
537 
538 err:
539 	ath12k_core_free_bdf(ab, bd);
540 	return ret;
541 }
542 
543 int ath12k_core_fetch_board_data_api_1(struct ath12k_base *ab,
544 				       struct ath12k_board_data *bd,
545 				       char *filename)
546 {
547 	bd->fw = ath12k_core_firmware_request(ab, filename);
548 	if (IS_ERR(bd->fw))
549 		return PTR_ERR(bd->fw);
550 
551 	bd->data = bd->fw->data;
552 	bd->len = bd->fw->size;
553 
554 	return 0;
555 }
556 
557 #define BOARD_NAME_SIZE 200
558 int ath12k_core_fetch_bdf(struct ath12k_base *ab, struct ath12k_board_data *bd)
559 {
560 	char boardname[BOARD_NAME_SIZE], fallback_boardname[BOARD_NAME_SIZE];
561 	char *filename, filepath[100];
562 	int bd_api;
563 	int ret;
564 
565 	filename = ATH12K_BOARD_API2_FILE;
566 
567 	ret = ath12k_core_create_board_name(ab, boardname, sizeof(boardname));
568 	if (ret) {
569 		ath12k_err(ab, "failed to create board name: %d", ret);
570 		return ret;
571 	}
572 
573 	bd_api = 2;
574 	ret = ath12k_core_fetch_board_data_api_n(ab, bd, boardname,
575 						 ATH12K_BD_IE_BOARD,
576 						 ATH12K_BD_IE_BOARD_NAME,
577 						 ATH12K_BD_IE_BOARD_DATA);
578 	if (!ret)
579 		goto success;
580 
581 	ret = ath12k_core_create_fallback_board_name(ab, fallback_boardname,
582 						     sizeof(fallback_boardname));
583 	if (ret) {
584 		ath12k_err(ab, "failed to create fallback board name: %d", ret);
585 		return ret;
586 	}
587 
588 	ret = ath12k_core_fetch_board_data_api_n(ab, bd, fallback_boardname,
589 						 ATH12K_BD_IE_BOARD,
590 						 ATH12K_BD_IE_BOARD_NAME,
591 						 ATH12K_BD_IE_BOARD_DATA);
592 	if (!ret)
593 		goto success;
594 
595 	bd_api = 1;
596 	ret = ath12k_core_fetch_board_data_api_1(ab, bd, ATH12K_DEFAULT_BOARD_FILE);
597 	if (ret) {
598 		ath12k_core_create_firmware_path(ab, filename,
599 						 filepath, sizeof(filepath));
600 		ath12k_err(ab, "failed to fetch board data for %s from %s\n",
601 			   boardname, filepath);
602 		if (memcmp(boardname, fallback_boardname, strlen(boardname)))
603 			ath12k_err(ab, "failed to fetch board data for %s from %s\n",
604 				   fallback_boardname, filepath);
605 
606 		ath12k_err(ab, "failed to fetch board.bin from %s\n",
607 			   ab->hw_params->fw.dir);
608 		return ret;
609 	}
610 
611 success:
612 	ath12k_dbg(ab, ATH12K_DBG_BOOT, "using board api %d\n", bd_api);
613 	return 0;
614 }
615 
616 int ath12k_core_fetch_regdb(struct ath12k_base *ab, struct ath12k_board_data *bd)
617 {
618 	char boardname[BOARD_NAME_SIZE], default_boardname[BOARD_NAME_SIZE];
619 	int ret;
620 
621 	ret = ath12k_core_create_board_name(ab, boardname, BOARD_NAME_SIZE);
622 	if (ret) {
623 		ath12k_dbg(ab, ATH12K_DBG_BOOT,
624 			   "failed to create board name for regdb: %d", ret);
625 		goto exit;
626 	}
627 
628 	ret = ath12k_core_fetch_board_data_api_n(ab, bd, boardname,
629 						 ATH12K_BD_IE_REGDB,
630 						 ATH12K_BD_IE_REGDB_NAME,
631 						 ATH12K_BD_IE_REGDB_DATA);
632 	if (!ret)
633 		goto exit;
634 
635 	ret = ath12k_core_create_bus_type_board_name(ab, default_boardname,
636 						     BOARD_NAME_SIZE);
637 	if (ret) {
638 		ath12k_dbg(ab, ATH12K_DBG_BOOT,
639 			   "failed to create default board name for regdb: %d", ret);
640 		goto exit;
641 	}
642 
643 	ret = ath12k_core_fetch_board_data_api_n(ab, bd, default_boardname,
644 						 ATH12K_BD_IE_REGDB,
645 						 ATH12K_BD_IE_REGDB_NAME,
646 						 ATH12K_BD_IE_REGDB_DATA);
647 	if (!ret)
648 		goto exit;
649 
650 	ret = ath12k_core_fetch_board_data_api_1(ab, bd, ATH12K_REGDB_FILE_NAME);
651 	if (ret)
652 		ath12k_dbg(ab, ATH12K_DBG_BOOT, "failed to fetch %s from %s\n",
653 			   ATH12K_REGDB_FILE_NAME, ab->hw_params->fw.dir);
654 
655 exit:
656 	if (!ret)
657 		ath12k_dbg(ab, ATH12K_DBG_BOOT, "fetched regdb\n");
658 
659 	return ret;
660 }
661 
662 u32 ath12k_core_get_max_station_per_radio(struct ath12k_base *ab)
663 {
664 	if (ab->num_radios == 2)
665 		return TARGET_NUM_STATIONS(ab, DBS);
666 	if (ab->num_radios == 3)
667 		return TARGET_NUM_STATIONS(ab, DBS_SBS);
668 	return TARGET_NUM_STATIONS(ab, SINGLE);
669 }
670 
671 u32 ath12k_core_get_max_peers_per_radio(struct ath12k_base *ab)
672 {
673 	return ath12k_core_get_max_station_per_radio(ab) + TARGET_NUM_VDEVS(ab);
674 }
675 EXPORT_SYMBOL(ath12k_core_get_max_peers_per_radio);
676 
677 static inline
678 void ath12k_core_to_group_ref_get(struct ath12k_base *ab)
679 {
680 	struct ath12k_hw_group *ag = ab->ag;
681 
682 	lockdep_assert_held(&ag->mutex);
683 
684 	if (ab->hw_group_ref) {
685 		ath12k_dbg(ab, ATH12K_DBG_BOOT, "core already attached to group %d\n",
686 			   ag->id);
687 		return;
688 	}
689 
690 	ab->hw_group_ref = true;
691 	ag->num_started++;
692 
693 	ath12k_dbg(ab, ATH12K_DBG_BOOT, "core attached to group %d, num_started %d\n",
694 		   ag->id, ag->num_started);
695 }
696 
697 static inline
698 void ath12k_core_to_group_ref_put(struct ath12k_base *ab)
699 {
700 	struct ath12k_hw_group *ag = ab->ag;
701 
702 	lockdep_assert_held(&ag->mutex);
703 
704 	if (!ab->hw_group_ref) {
705 		ath12k_dbg(ab, ATH12K_DBG_BOOT, "core already de-attached from group %d\n",
706 			   ag->id);
707 		return;
708 	}
709 
710 	ab->hw_group_ref = false;
711 	ag->num_started--;
712 
713 	ath12k_dbg(ab, ATH12K_DBG_BOOT, "core de-attached from group %d, num_started %d\n",
714 		   ag->id, ag->num_started);
715 }
716 
717 static void ath12k_core_stop(struct ath12k_base *ab)
718 {
719 	ath12k_link_sta_rhash_tbl_destroy(ab);
720 
721 	ath12k_core_to_group_ref_put(ab);
722 
723 	if (!test_bit(ATH12K_FLAG_CRASH_FLUSH, &ab->dev_flags)) {
724 		ath12k_dp_reoq_lut_addr_reset(ath12k_ab_to_dp(ab));
725 		ath12k_qmi_firmware_stop(ab);
726 	}
727 
728 	ath12k_acpi_stop(ab);
729 
730 	ath12k_dp_rx_pdev_reo_cleanup(ab);
731 	ath12k_hif_stop(ab);
732 	ath12k_wmi_detach(ab);
733 	ath12k_dp_cmn_device_deinit(ath12k_ab_to_dp(ab));
734 
735 	/* De-Init of components as needed */
736 }
737 
738 static void ath12k_core_check_cc_code_bdfext(const struct dmi_header *hdr, void *data)
739 {
740 	struct ath12k_base *ab = data;
741 	const char *magic = ATH12K_SMBIOS_BDF_EXT_MAGIC;
742 	struct ath12k_smbios_bdf *smbios = (struct ath12k_smbios_bdf *)hdr;
743 	ssize_t copied;
744 	size_t len;
745 	int i;
746 
747 	if (ab->qmi.target.bdf_ext[0] != '\0')
748 		return;
749 
750 	if (hdr->type != ATH12K_SMBIOS_BDF_EXT_TYPE)
751 		return;
752 
753 	if (hdr->length != ATH12K_SMBIOS_BDF_EXT_LENGTH) {
754 		ath12k_dbg(ab, ATH12K_DBG_BOOT,
755 			   "wrong smbios bdf ext type length (%d).\n",
756 			   hdr->length);
757 		return;
758 	}
759 
760 	spin_lock_bh(&ab->base_lock);
761 
762 	switch (smbios->country_code_flag) {
763 	case ATH12K_SMBIOS_CC_ISO:
764 		ab->new_alpha2[0] = u16_get_bits(smbios->cc_code >> 8, 0xff);
765 		ab->new_alpha2[1] = u16_get_bits(smbios->cc_code, 0xff);
766 		ath12k_dbg(ab, ATH12K_DBG_BOOT, "boot smbios cc_code %c%c\n",
767 			   ab->new_alpha2[0], ab->new_alpha2[1]);
768 		break;
769 	case ATH12K_SMBIOS_CC_WW:
770 		ab->new_alpha2[0] = '0';
771 		ab->new_alpha2[1] = '0';
772 		ath12k_dbg(ab, ATH12K_DBG_BOOT, "boot smbios worldwide regdomain\n");
773 		break;
774 	default:
775 		ath12k_dbg(ab, ATH12K_DBG_BOOT, "boot ignore smbios country code setting %d\n",
776 			   smbios->country_code_flag);
777 		break;
778 	}
779 
780 	spin_unlock_bh(&ab->base_lock);
781 
782 	if (!smbios->bdf_enabled) {
783 		ath12k_dbg(ab, ATH12K_DBG_BOOT, "bdf variant name not found.\n");
784 		return;
785 	}
786 
787 	/* Only one string exists (per spec) */
788 	if (memcmp(smbios->bdf_ext, magic, strlen(magic)) != 0) {
789 		ath12k_dbg(ab, ATH12K_DBG_BOOT,
790 			   "bdf variant magic does not match.\n");
791 		return;
792 	}
793 
794 	len = min_t(size_t,
795 		    strlen(smbios->bdf_ext), sizeof(ab->qmi.target.bdf_ext));
796 	for (i = 0; i < len; i++) {
797 		if (!isascii(smbios->bdf_ext[i]) || !isprint(smbios->bdf_ext[i])) {
798 			ath12k_dbg(ab, ATH12K_DBG_BOOT,
799 				   "bdf variant name contains non ascii chars.\n");
800 			return;
801 		}
802 	}
803 
804 	/* Copy extension name without magic prefix */
805 	copied = strscpy(ab->qmi.target.bdf_ext, smbios->bdf_ext + strlen(magic),
806 			 sizeof(ab->qmi.target.bdf_ext));
807 	if (copied < 0) {
808 		ath12k_dbg(ab, ATH12K_DBG_BOOT,
809 			   "bdf variant string is longer than the buffer can accommodate\n");
810 		return;
811 	}
812 
813 	ath12k_dbg(ab, ATH12K_DBG_BOOT,
814 		   "found and validated bdf variant smbios_type 0x%x bdf %s\n",
815 		   ATH12K_SMBIOS_BDF_EXT_TYPE, ab->qmi.target.bdf_ext);
816 }
817 
818 int ath12k_core_check_smbios(struct ath12k_base *ab)
819 {
820 	ab->qmi.target.bdf_ext[0] = '\0';
821 	dmi_walk(ath12k_core_check_cc_code_bdfext, ab);
822 
823 	if (ab->qmi.target.bdf_ext[0] == '\0')
824 		return -ENODATA;
825 
826 	return 0;
827 }
828 
829 int ath12k_core_check_dt(struct ath12k_base *ab)
830 {
831 	size_t max_len = sizeof(ab->qmi.target.bdf_ext);
832 	const char *variant = NULL;
833 	struct device_node *node;
834 
835 	node = ab->dev->of_node;
836 	if (!node)
837 		return -ENOENT;
838 
839 	of_property_read_string(node, "qcom,calibration-variant",
840 				&variant);
841 	if (!variant)
842 		return -ENODATA;
843 
844 	if (strscpy(ab->qmi.target.bdf_ext, variant, max_len) < 0)
845 		ath12k_dbg(ab, ATH12K_DBG_BOOT,
846 			   "bdf variant string is longer than the buffer can accommodate (variant: %s)\n",
847 			   variant);
848 
849 	return 0;
850 }
851 
852 static int ath12k_core_soc_create(struct ath12k_base *ab)
853 {
854 	int ret;
855 
856 	if (ath12k_ftm_mode) {
857 		ab->fw_mode = ATH12K_QMI_FIRMWARE_MODE_FTM;
858 		ath12k_info(ab, "Booting in ftm mode\n");
859 	}
860 
861 	ret = ath12k_qmi_init_service(ab);
862 	if (ret) {
863 		ath12k_err(ab, "failed to initialize qmi :%d\n", ret);
864 		return ret;
865 	}
866 
867 	ath12k_debugfs_soc_create(ab);
868 
869 	ret = ath12k_hif_power_up(ab);
870 	if (ret) {
871 		ath12k_err(ab, "failed to power up :%d\n", ret);
872 		goto err_qmi_deinit;
873 	}
874 
875 	return 0;
876 
877 err_qmi_deinit:
878 	ath12k_debugfs_soc_destroy(ab);
879 	ath12k_qmi_deinit_service(ab);
880 	return ret;
881 }
882 
883 static void ath12k_core_soc_destroy(struct ath12k_base *ab)
884 {
885 	ath12k_hif_power_down(ab, false);
886 	ath12k_reg_free(ab);
887 	ath12k_debugfs_soc_destroy(ab);
888 	ath12k_qmi_deinit_service(ab);
889 }
890 
891 static int ath12k_core_pdev_create(struct ath12k_base *ab)
892 {
893 	int ret;
894 
895 	ret = ath12k_dp_pdev_alloc(ab);
896 	if (ret) {
897 		ath12k_err(ab, "failed to attach DP pdev: %d\n", ret);
898 		return ret;
899 	}
900 
901 	ret = ath12k_thermal_register(ab);
902 	if (ret) {
903 		ath12k_err(ab, "could not register thermal device: %d\n", ret);
904 		goto err_dp_pdev_free;
905 	}
906 
907 	ath12k_debugfs_pdev_create(ab);
908 
909 	return 0;
910 
911 err_dp_pdev_free:
912 	ath12k_dp_pdev_free(ab);
913 	return ret;
914 }
915 
916 static void ath12k_core_pdev_destroy(struct ath12k_base *ab)
917 {
918 	ath12k_thermal_unregister(ab);
919 	ath12k_dp_pdev_free(ab);
920 }
921 
922 static int ath12k_core_start(struct ath12k_base *ab)
923 {
924 	int ret;
925 
926 	lockdep_assert_held(&ab->core_lock);
927 
928 	ret = ath12k_wmi_attach(ab);
929 	if (ret) {
930 		ath12k_err(ab, "failed to attach wmi: %d\n", ret);
931 		return ret;
932 	}
933 
934 	ret = ath12k_htc_init(ab);
935 	if (ret) {
936 		ath12k_err(ab, "failed to init htc: %d\n", ret);
937 		goto err_wmi_detach;
938 	}
939 
940 	ret = ath12k_hif_start(ab);
941 	if (ret) {
942 		ath12k_err(ab, "failed to start HIF: %d\n", ret);
943 		goto err_wmi_detach;
944 	}
945 
946 	ret = ath12k_htc_wait_target(&ab->htc);
947 	if (ret) {
948 		ath12k_err(ab, "failed to connect to HTC: %d\n", ret);
949 		goto err_hif_stop;
950 	}
951 
952 	ret = ath12k_dp_htt_connect(ath12k_ab_to_dp(ab));
953 	if (ret) {
954 		ath12k_err(ab, "failed to connect to HTT: %d\n", ret);
955 		goto err_hif_stop;
956 	}
957 
958 	ret = ath12k_wmi_connect(ab);
959 	if (ret) {
960 		ath12k_err(ab, "failed to connect wmi: %d\n", ret);
961 		goto err_hif_stop;
962 	}
963 
964 	ret = ath12k_htc_start(&ab->htc);
965 	if (ret) {
966 		ath12k_err(ab, "failed to start HTC: %d\n", ret);
967 		goto err_hif_stop;
968 	}
969 
970 	ret = ath12k_wmi_wait_for_service_ready(ab);
971 	if (ret) {
972 		ath12k_err(ab, "failed to receive wmi service ready event: %d\n",
973 			   ret);
974 		goto err_hif_stop;
975 	}
976 
977 	ath12k_hal_cc_config(ab);
978 
979 	ret = ath12k_dp_rx_pdev_reo_setup(ab);
980 	if (ret) {
981 		ath12k_err(ab, "failed to initialize reo destination rings: %d\n", ret);
982 		goto err_hif_stop;
983 	}
984 
985 	ret = ath12k_wmi_cmd_init(ab);
986 	if (ret) {
987 		ath12k_err(ab, "failed to send wmi init cmd: %d\n", ret);
988 		goto err_reo_cleanup;
989 	}
990 
991 	ret = ath12k_wmi_wait_for_unified_ready(ab);
992 	if (ret) {
993 		ath12k_err(ab, "failed to receive wmi unified ready event: %d\n",
994 			   ret);
995 		goto err_reo_cleanup;
996 	}
997 
998 	/* put hardware to DBS mode */
999 	if (ab->hw_params->single_pdev_only) {
1000 		ret = ath12k_wmi_set_hw_mode(ab, WMI_HOST_HW_MODE_DBS);
1001 		if (ret) {
1002 			ath12k_err(ab, "failed to send dbs mode: %d\n", ret);
1003 			goto err_reo_cleanup;
1004 		}
1005 	}
1006 
1007 	ret = ath12k_dp_tx_htt_h2t_ver_req_msg(ab);
1008 	if (ret) {
1009 		ath12k_err(ab, "failed to send htt version request message: %d\n",
1010 			   ret);
1011 		goto err_reo_cleanup;
1012 	}
1013 
1014 	ath12k_acpi_set_dsm_func(ab);
1015 
1016 	/* Indicate the core start in the appropriate group */
1017 	ath12k_core_to_group_ref_get(ab);
1018 
1019 	ret = ath12k_link_sta_rhash_tbl_init(ab);
1020 	if (ret) {
1021 		ath12k_warn(ab, "failed to init peer addr rhash table %d\n", ret);
1022 		goto err_reo_cleanup;
1023 	}
1024 
1025 	return 0;
1026 
1027 err_reo_cleanup:
1028 	ath12k_dp_rx_pdev_reo_cleanup(ab);
1029 err_hif_stop:
1030 	ath12k_hif_stop(ab);
1031 err_wmi_detach:
1032 	ath12k_wmi_detach(ab);
1033 	return ret;
1034 }
1035 
1036 static void ath12k_core_device_cleanup(struct ath12k_base *ab)
1037 {
1038 	mutex_lock(&ab->core_lock);
1039 
1040 	ath12k_hif_irq_disable(ab);
1041 	ath12k_core_pdev_destroy(ab);
1042 
1043 	mutex_unlock(&ab->core_lock);
1044 }
1045 
1046 static int ath12k_core_device_setup(struct ath12k_base *ab)
1047 {
1048 	int ret;
1049 
1050 	guard(mutex)(&ab->core_lock);
1051 
1052 	ret = ath12k_core_pdev_create(ab);
1053 	if (ret) {
1054 		ath12k_err(ab, "failed to create pdev core %d\n", ret);
1055 		return ret;
1056 	}
1057 
1058 	ath12k_hif_irq_enable(ab);
1059 
1060 	ret = ath12k_core_rfkill_config(ab);
1061 	if (ret && ret != -EOPNOTSUPP)
1062 		return ret;
1063 
1064 	return 0;
1065 }
1066 
1067 static void ath12k_core_hw_group_stop(struct ath12k_hw_group *ag)
1068 {
1069 	struct ath12k_base *ab;
1070 	int i;
1071 
1072 	lockdep_assert_held(&ag->mutex);
1073 
1074 	clear_bit(ATH12K_GROUP_FLAG_REGISTERED, &ag->flags);
1075 
1076 	for (i = ag->num_devices - 1; i >= 0; i--) {
1077 		ab = ag->ab[i];
1078 		if (!ab)
1079 			continue;
1080 
1081 		clear_bit(ATH12K_FLAG_REGISTERED, &ab->dev_flags);
1082 
1083 		ath12k_core_device_cleanup(ab);
1084 	}
1085 
1086 	/* Unregister MAC (drops wiphys) only after per-device cleanup */
1087 	ath12k_mac_unregister(ag);
1088 
1089 	/* Teardown MLO state after MAC unregister for symmetry */
1090 	ath12k_mac_mlo_teardown(ag);
1091 
1092 	ath12k_mac_destroy(ag);
1093 }
1094 
1095 u8 ath12k_get_num_partner_link(struct ath12k *ar)
1096 {
1097 	struct ath12k_base *partner_ab, *ab = ar->ab;
1098 	struct ath12k_hw_group *ag = ab->ag;
1099 	struct ath12k_pdev *pdev;
1100 	u8 num_link = 0;
1101 	int i, j;
1102 
1103 	lockdep_assert_held(&ag->mutex);
1104 
1105 	for (i = 0; i < ag->num_devices; i++) {
1106 		partner_ab = ag->ab[i];
1107 
1108 		for (j = 0; j < partner_ab->num_radios; j++) {
1109 			pdev = &partner_ab->pdevs[j];
1110 
1111 			/* Avoid the self link */
1112 			if (ar == pdev->ar)
1113 				continue;
1114 
1115 			num_link++;
1116 		}
1117 	}
1118 
1119 	return num_link;
1120 }
1121 
1122 static int __ath12k_mac_mlo_ready(struct ath12k *ar)
1123 {
1124 	u8 num_link = ath12k_get_num_partner_link(ar);
1125 	int ret;
1126 
1127 	if (num_link == 0)
1128 		return 0;
1129 
1130 	ret = ath12k_wmi_mlo_ready(ar);
1131 	if (ret) {
1132 		ath12k_err(ar->ab, "MLO ready failed for pdev %d: %d\n",
1133 			   ar->pdev_idx, ret);
1134 		return ret;
1135 	}
1136 
1137 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mlo ready done for pdev %d\n",
1138 		   ar->pdev_idx);
1139 
1140 	return 0;
1141 }
1142 
1143 int ath12k_mac_mlo_ready(struct ath12k_hw_group *ag)
1144 {
1145 	struct ath12k_hw *ah;
1146 	struct ath12k *ar;
1147 	int ret;
1148 	int i, j;
1149 
1150 	for (i = 0; i < ag->num_hw; i++) {
1151 		ah = ag->ah[i];
1152 		if (!ah)
1153 			continue;
1154 
1155 		for_each_ar(ah, ar, j) {
1156 			ar = &ah->radio[j];
1157 			ret = __ath12k_mac_mlo_ready(ar);
1158 			if (ret)
1159 				return ret;
1160 		}
1161 	}
1162 
1163 	return 0;
1164 }
1165 
1166 static int ath12k_core_mlo_setup(struct ath12k_hw_group *ag)
1167 {
1168 	int ret, i;
1169 
1170 	if (!ag->mlo_capable)
1171 		return 0;
1172 
1173 	ret = ath12k_mac_mlo_setup(ag);
1174 	if (ret)
1175 		return ret;
1176 
1177 	for (i = 0; i < ag->num_devices; i++)
1178 		ath12k_dp_partner_cc_init(ag->ab[i]);
1179 
1180 	ret = ath12k_mac_mlo_ready(ag);
1181 	if (ret)
1182 		goto err_mlo_teardown;
1183 
1184 	return 0;
1185 
1186 err_mlo_teardown:
1187 	ath12k_mac_mlo_teardown(ag);
1188 
1189 	return ret;
1190 }
1191 
1192 static int ath12k_core_hw_group_start(struct ath12k_hw_group *ag)
1193 {
1194 	struct ath12k_base *ab;
1195 	int ret, i;
1196 
1197 	lockdep_assert_held(&ag->mutex);
1198 
1199 	if (test_bit(ATH12K_GROUP_FLAG_REGISTERED, &ag->flags)) {
1200 		ret = ath12k_core_mlo_setup(ag);
1201 		if (WARN_ON(ret)) {
1202 			ath12k_mac_unregister(ag);
1203 			goto err_mac_destroy;
1204 		}
1205 		goto core_pdev_create;
1206 	}
1207 
1208 	ret = ath12k_mac_allocate(ag);
1209 	if (WARN_ON(ret))
1210 		return ret;
1211 
1212 	ret = ath12k_core_mlo_setup(ag);
1213 	if (WARN_ON(ret))
1214 		goto err_mac_destroy;
1215 
1216 	ret = ath12k_mac_register(ag);
1217 	if (WARN_ON(ret))
1218 		goto err_mlo_teardown;
1219 
1220 	set_bit(ATH12K_GROUP_FLAG_REGISTERED, &ag->flags);
1221 
1222 core_pdev_create:
1223 	for (i = 0; i < ag->num_devices; i++) {
1224 		ab = ag->ab[i];
1225 		if (!ab)
1226 			continue;
1227 
1228 		set_bit(ATH12K_FLAG_REGISTERED, &ab->dev_flags);
1229 
1230 		ret = ath12k_core_device_setup(ab);
1231 		if (ret)
1232 			goto err;
1233 	}
1234 
1235 	return 0;
1236 
1237 err:
1238 	ath12k_core_hw_group_stop(ag);
1239 	return ret;
1240 
1241 err_mlo_teardown:
1242 	ath12k_mac_mlo_teardown(ag);
1243 
1244 err_mac_destroy:
1245 	ath12k_mac_destroy(ag);
1246 
1247 	return ret;
1248 }
1249 
1250 static int ath12k_core_start_firmware(struct ath12k_base *ab,
1251 				      enum ath12k_qmi_firmware_mode mode)
1252 {
1253 	int ret;
1254 
1255 	ath12k_ce_get_shadow_config(ab, &ab->qmi.ce_cfg.shadow_reg_v3,
1256 				    &ab->qmi.ce_cfg.shadow_reg_v3_len);
1257 
1258 	ret = ath12k_qmi_firmware_start(ab, mode);
1259 	if (ret) {
1260 		ath12k_err(ab, "failed to send firmware start: %d\n", ret);
1261 		return ret;
1262 	}
1263 
1264 	return ret;
1265 }
1266 
1267 static inline
1268 bool ath12k_core_hw_group_start_ready(struct ath12k_hw_group *ag)
1269 {
1270 	lockdep_assert_held(&ag->mutex);
1271 
1272 	return (ag->num_started == ag->num_devices);
1273 }
1274 
1275 static void ath12k_fw_stats_pdevs_free(struct list_head *head)
1276 {
1277 	struct ath12k_fw_stats_pdev *i, *tmp;
1278 
1279 	list_for_each_entry_safe(i, tmp, head, list) {
1280 		list_del(&i->list);
1281 		kfree(i);
1282 	}
1283 }
1284 
1285 void ath12k_fw_stats_bcn_free(struct list_head *head)
1286 {
1287 	struct ath12k_fw_stats_bcn *i, *tmp;
1288 
1289 	list_for_each_entry_safe(i, tmp, head, list) {
1290 		list_del(&i->list);
1291 		kfree(i);
1292 	}
1293 }
1294 
1295 static void ath12k_fw_stats_vdevs_free(struct list_head *head)
1296 {
1297 	struct ath12k_fw_stats_vdev *i, *tmp;
1298 
1299 	list_for_each_entry_safe(i, tmp, head, list) {
1300 		list_del(&i->list);
1301 		kfree(i);
1302 	}
1303 }
1304 
1305 void ath12k_fw_stats_init(struct ath12k *ar)
1306 {
1307 	INIT_LIST_HEAD(&ar->fw_stats.vdevs);
1308 	INIT_LIST_HEAD(&ar->fw_stats.pdevs);
1309 	INIT_LIST_HEAD(&ar->fw_stats.bcn);
1310 	init_completion(&ar->fw_stats_complete);
1311 	init_completion(&ar->fw_stats_done);
1312 }
1313 
1314 void ath12k_fw_stats_free(struct ath12k_fw_stats *stats)
1315 {
1316 	ath12k_fw_stats_pdevs_free(&stats->pdevs);
1317 	ath12k_fw_stats_vdevs_free(&stats->vdevs);
1318 	ath12k_fw_stats_bcn_free(&stats->bcn);
1319 }
1320 
1321 void ath12k_fw_stats_reset(struct ath12k *ar)
1322 {
1323 	spin_lock_bh(&ar->data_lock);
1324 	ath12k_fw_stats_free(&ar->fw_stats);
1325 	ar->fw_stats.num_vdev_recvd = 0;
1326 	spin_unlock_bh(&ar->data_lock);
1327 }
1328 
1329 static void ath12k_core_trigger_partner(struct ath12k_base *ab)
1330 {
1331 	struct ath12k_hw_group *ag = ab->ag;
1332 	struct ath12k_base *partner_ab;
1333 	bool found = false;
1334 	int i;
1335 
1336 	for (i = 0; i < ag->num_devices; i++) {
1337 		partner_ab = ag->ab[i];
1338 		if (!partner_ab)
1339 			continue;
1340 
1341 		if (found)
1342 			ath12k_qmi_trigger_host_cap(partner_ab);
1343 
1344 		found = (partner_ab == ab);
1345 	}
1346 }
1347 
1348 int ath12k_core_qmi_firmware_ready(struct ath12k_base *ab)
1349 {
1350 	struct ath12k_hw_group *ag = ath12k_ab_to_ag(ab);
1351 	int ret, i;
1352 
1353 	ret = ath12k_core_start_firmware(ab, ab->fw_mode);
1354 	if (ret) {
1355 		ath12k_err(ab, "failed to start firmware: %d\n", ret);
1356 		return ret;
1357 	}
1358 
1359 	ret = ath12k_ce_init_pipes(ab);
1360 	if (ret) {
1361 		ath12k_err(ab, "failed to initialize CE: %d\n", ret);
1362 		goto err_firmware_stop;
1363 	}
1364 
1365 	ret = ath12k_dp_cmn_device_init(ath12k_ab_to_dp(ab));
1366 	if (ret) {
1367 		ath12k_err(ab, "failed to init DP: %d\n", ret);
1368 		goto err_firmware_stop;
1369 	}
1370 
1371 	mutex_lock(&ag->mutex);
1372 	mutex_lock(&ab->core_lock);
1373 
1374 	ret = ath12k_core_start(ab);
1375 	if (ret) {
1376 		ath12k_err(ab, "failed to start core: %d\n", ret);
1377 		goto err_deinit;
1378 	}
1379 
1380 	mutex_unlock(&ab->core_lock);
1381 
1382 	if (ath12k_core_hw_group_start_ready(ag)) {
1383 		ret = ath12k_core_hw_group_start(ag);
1384 		if (ret) {
1385 			ath12k_warn(ab, "unable to start hw group\n");
1386 			goto err_core_stop;
1387 		}
1388 		ath12k_dbg(ab, ATH12K_DBG_BOOT, "group %d started\n", ag->id);
1389 	} else {
1390 		ath12k_core_trigger_partner(ab);
1391 	}
1392 
1393 	mutex_unlock(&ag->mutex);
1394 
1395 	return 0;
1396 
1397 err_core_stop:
1398 	for (i = ag->num_devices - 1; i >= 0; i--) {
1399 		ab = ag->ab[i];
1400 		if (!ab)
1401 			continue;
1402 
1403 		mutex_lock(&ab->core_lock);
1404 		ath12k_core_stop(ab);
1405 		mutex_unlock(&ab->core_lock);
1406 	}
1407 	mutex_unlock(&ag->mutex);
1408 	goto exit;
1409 
1410 err_deinit:
1411 	ath12k_dp_reoq_lut_addr_reset(ath12k_ab_to_dp(ab));
1412 	ath12k_dp_cmn_device_deinit(ath12k_ab_to_dp(ab));
1413 	mutex_unlock(&ab->core_lock);
1414 	mutex_unlock(&ag->mutex);
1415 
1416 err_firmware_stop:
1417 	ath12k_qmi_firmware_stop(ab);
1418 
1419 exit:
1420 	return ret;
1421 }
1422 
1423 static int ath12k_core_reconfigure_on_crash(struct ath12k_base *ab)
1424 {
1425 	int ret, total_vdev;
1426 
1427 	mutex_lock(&ab->core_lock);
1428 	ath12k_link_sta_rhash_tbl_destroy(ab);
1429 	ath12k_thermal_unregister(ab);
1430 	ath12k_dp_pdev_free(ab);
1431 	ath12k_ce_cleanup_pipes(ab);
1432 	ath12k_wmi_detach(ab);
1433 	ath12k_dp_rx_pdev_reo_cleanup(ab);
1434 	mutex_unlock(&ab->core_lock);
1435 
1436 	ath12k_dp_cmn_device_deinit(ath12k_ab_to_dp(ab));
1437 	ath12k_hal_srng_deinit(ab);
1438 	total_vdev = ab->num_radios * TARGET_NUM_VDEVS(ab);
1439 	ab->free_vdev_map = (1LL << total_vdev) - 1;
1440 
1441 	ret = ath12k_hal_srng_init(ab);
1442 	if (ret)
1443 		return ret;
1444 
1445 	clear_bit(ATH12K_FLAG_CRASH_FLUSH, &ab->dev_flags);
1446 
1447 	ret = ath12k_core_qmi_firmware_ready(ab);
1448 	if (ret)
1449 		goto err_hal_srng_deinit;
1450 
1451 	clear_bit(ATH12K_FLAG_RECOVERY, &ab->dev_flags);
1452 
1453 	return 0;
1454 
1455 err_hal_srng_deinit:
1456 	ath12k_hal_srng_deinit(ab);
1457 	return ret;
1458 }
1459 
1460 static void ath12k_rfkill_work(struct work_struct *work)
1461 {
1462 	struct ath12k_base *ab = container_of(work, struct ath12k_base, rfkill_work);
1463 	struct ath12k_hw_group *ag = ab->ag;
1464 	struct ath12k *ar;
1465 	struct ath12k_hw *ah;
1466 	struct ieee80211_hw *hw;
1467 	bool rfkill_radio_on;
1468 	int i, j;
1469 
1470 	spin_lock_bh(&ab->base_lock);
1471 	rfkill_radio_on = ab->rfkill_radio_on;
1472 	spin_unlock_bh(&ab->base_lock);
1473 
1474 	for (i = 0; i < ag->num_hw; i++) {
1475 		ah = ath12k_ag_to_ah(ag, i);
1476 		if (!ah)
1477 			continue;
1478 
1479 		for (j = 0; j < ah->num_radio; j++) {
1480 			ar = &ah->radio[j];
1481 			if (!ar)
1482 				continue;
1483 
1484 			ath12k_mac_rfkill_enable_radio(ar, rfkill_radio_on);
1485 		}
1486 
1487 		hw = ah->hw;
1488 		wiphy_rfkill_set_hw_state(hw->wiphy, !rfkill_radio_on);
1489 	}
1490 }
1491 
1492 void ath12k_core_halt(struct ath12k *ar)
1493 {
1494 	struct list_head *pos, *n;
1495 	struct ath12k_base *ab = ar->ab;
1496 
1497 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1498 
1499 	ar->num_created_vdevs = 0;
1500 	ar->allocated_vdev_map = 0;
1501 
1502 	ath12k_mac_scan_finish(ar);
1503 	ath12k_mac_peer_cleanup_all(ar);
1504 	cancel_delayed_work_sync(&ar->scan.timeout);
1505 	cancel_work_sync(&ar->regd_update_work);
1506 	cancel_work_sync(&ar->regd_channel_update_work);
1507 	cancel_work_sync(&ab->rfkill_work);
1508 	cancel_work_sync(&ab->update_11d_work);
1509 
1510 	rcu_assign_pointer(ab->pdevs_active[ar->pdev_idx], NULL);
1511 	synchronize_rcu();
1512 
1513 	spin_lock_bh(&ar->data_lock);
1514 	list_for_each_safe(pos, n, &ar->arvifs)
1515 		list_del_init(pos);
1516 	spin_unlock_bh(&ar->data_lock);
1517 
1518 	idr_init(&ar->txmgmt_idr);
1519 }
1520 
1521 static void ath12k_core_pre_reconfigure_recovery(struct ath12k_base *ab)
1522 {
1523 	struct ath12k_hw_group *ag = ab->ag;
1524 	struct ath12k *ar;
1525 	struct ath12k_hw *ah;
1526 	int i, j;
1527 
1528 	spin_lock_bh(&ab->base_lock);
1529 	ab->stats.fw_crash_counter++;
1530 	spin_unlock_bh(&ab->base_lock);
1531 
1532 	if (ab->is_reset)
1533 		set_bit(ATH12K_FLAG_CRASH_FLUSH, &ab->dev_flags);
1534 
1535 	for (i = 0; i < ag->num_hw; i++) {
1536 		ah = ath12k_ag_to_ah(ag, i);
1537 		if (!ah || ah->state == ATH12K_HW_STATE_OFF ||
1538 		    ah->state == ATH12K_HW_STATE_TM)
1539 			continue;
1540 
1541 		wiphy_lock(ah->hw->wiphy);
1542 
1543 		/* If queue 0 is stopped, it is safe to assume that all
1544 		 * other queues are stopped by driver via
1545 		 * ieee80211_stop_queues() below. This means, there is
1546 		 * no need to stop it again and hence continue
1547 		 */
1548 		if (ieee80211_queue_stopped(ah->hw, 0)) {
1549 			wiphy_unlock(ah->hw->wiphy);
1550 			continue;
1551 		}
1552 
1553 		ieee80211_stop_queues(ah->hw);
1554 
1555 		for (j = 0; j < ah->num_radio; j++) {
1556 			ar = &ah->radio[j];
1557 
1558 			ath12k_mac_drain_tx(ar);
1559 			ar->state_11d = ATH12K_11D_IDLE;
1560 			complete(&ar->completed_11d_scan);
1561 			complete(&ar->scan.started);
1562 			complete_all(&ar->scan.completed);
1563 			complete(&ar->scan.on_channel);
1564 			complete(&ar->peer_assoc_done);
1565 			ath12k_peer_delete_wait_flush(ar);
1566 			complete(&ar->install_key_done);
1567 			complete(&ar->vdev_setup_done);
1568 			complete(&ar->vdev_delete_done);
1569 			complete(&ar->bss_survey_done);
1570 			complete_all(&ar->regd_update_completed);
1571 			complete_all(&ar->thermal.wmi_sync);
1572 
1573 			wake_up(&ar->dp.tx_empty_waitq);
1574 			ath12k_mac_tx_mgmt_pending_free(ar);
1575 			idr_destroy(&ar->txmgmt_idr);
1576 			wake_up(&ar->txmgmt_empty_waitq);
1577 
1578 			ar->monitor_vdev_id = -1;
1579 			ar->monitor_vdev_created = false;
1580 			ar->monitor_started = false;
1581 		}
1582 
1583 		wiphy_unlock(ah->hw->wiphy);
1584 
1585 		complete(&ah->peer_ml_id_done);
1586 	}
1587 
1588 	wake_up(&ab->wmi_ab.tx_credits_wq);
1589 	wake_up(&ab->peer_mapping_wq);
1590 }
1591 
1592 static void ath12k_update_11d(struct work_struct *work)
1593 {
1594 	struct ath12k_base *ab = container_of(work, struct ath12k_base, update_11d_work);
1595 	struct ath12k *ar;
1596 	struct ath12k_pdev *pdev;
1597 	struct wmi_set_current_country_arg arg = {};
1598 	int ret, i;
1599 
1600 	spin_lock_bh(&ab->base_lock);
1601 	memcpy(&arg.alpha2, &ab->new_alpha2, 2);
1602 	spin_unlock_bh(&ab->base_lock);
1603 
1604 	ath12k_dbg(ab, ATH12K_DBG_WMI, "update 11d new cc %c%c\n",
1605 		   arg.alpha2[0], arg.alpha2[1]);
1606 
1607 	for (i = 0; i < ab->num_radios; i++) {
1608 		pdev = &ab->pdevs[i];
1609 		ar = pdev->ar;
1610 
1611 		memcpy(&ar->alpha2, &arg.alpha2, 2);
1612 
1613 		reinit_completion(&ar->regd_update_completed);
1614 
1615 		ret = ath12k_wmi_send_set_current_country_cmd(ar, &arg);
1616 		if (ret)
1617 			ath12k_warn(ar->ab,
1618 				    "pdev id %d failed set current country code: %d\n",
1619 				    i, ret);
1620 	}
1621 }
1622 
1623 static void ath12k_core_post_reconfigure_recovery(struct ath12k_base *ab)
1624 {
1625 	struct ath12k_hw_group *ag = ab->ag;
1626 	struct ath12k_hw *ah;
1627 	struct ath12k *ar;
1628 	int i, j;
1629 
1630 	for (i = 0; i < ag->num_hw; i++) {
1631 		ah = ath12k_ag_to_ah(ag, i);
1632 		if (!ah || ah->state == ATH12K_HW_STATE_OFF)
1633 			continue;
1634 
1635 		wiphy_lock(ah->hw->wiphy);
1636 		mutex_lock(&ah->hw_mutex);
1637 
1638 		switch (ah->state) {
1639 		case ATH12K_HW_STATE_ON:
1640 			ah->state = ATH12K_HW_STATE_RESTARTING;
1641 
1642 			for (j = 0; j < ah->num_radio; j++) {
1643 				ar = &ah->radio[j];
1644 				ath12k_core_halt(ar);
1645 			}
1646 
1647 			ath12k_mac_dp_peer_cleanup(ah);
1648 			break;
1649 		case ATH12K_HW_STATE_OFF:
1650 			ath12k_warn(ab,
1651 				    "cannot restart hw %d that hasn't been started\n",
1652 				    i);
1653 			break;
1654 		case ATH12K_HW_STATE_RESTARTING:
1655 			break;
1656 		case ATH12K_HW_STATE_RESTARTED:
1657 			ah->state = ATH12K_HW_STATE_WEDGED;
1658 			fallthrough;
1659 		case ATH12K_HW_STATE_WEDGED:
1660 			ath12k_warn(ab,
1661 				    "device is wedged, will not restart hw %d\n", i);
1662 			break;
1663 		case ATH12K_HW_STATE_TM:
1664 			ath12k_warn(ab, "fw mode reset done radio %d\n", i);
1665 			break;
1666 		}
1667 
1668 		mutex_unlock(&ah->hw_mutex);
1669 		wiphy_unlock(ah->hw->wiphy);
1670 	}
1671 
1672 	complete(&ab->driver_recovery);
1673 }
1674 
1675 static void ath12k_core_restart(struct work_struct *work)
1676 {
1677 	struct ath12k_base *ab = container_of(work, struct ath12k_base, restart_work);
1678 	struct ath12k_hw_group *ag = ab->ag;
1679 	struct ath12k_hw *ah;
1680 	int ret, i;
1681 
1682 	ret = ath12k_core_reconfigure_on_crash(ab);
1683 	if (ret) {
1684 		ath12k_err(ab, "failed to reconfigure driver on crash recovery\n");
1685 		return;
1686 	}
1687 
1688 	if (ab->is_reset) {
1689 		if (!test_bit(ATH12K_FLAG_REGISTERED, &ab->dev_flags)) {
1690 			atomic_dec(&ab->reset_count);
1691 			complete(&ab->reset_complete);
1692 			ab->is_reset = false;
1693 			atomic_set(&ab->fail_cont_count, 0);
1694 			ath12k_dbg(ab, ATH12K_DBG_BOOT, "reset success\n");
1695 		}
1696 
1697 		mutex_lock(&ag->mutex);
1698 
1699 		if (!ath12k_core_hw_group_start_ready(ag)) {
1700 			mutex_unlock(&ag->mutex);
1701 			goto exit_restart;
1702 		}
1703 
1704 		for (i = 0; i < ag->num_hw; i++) {
1705 			ah = ath12k_ag_to_ah(ag, i);
1706 			ieee80211_restart_hw(ah->hw);
1707 		}
1708 
1709 		mutex_unlock(&ag->mutex);
1710 	}
1711 
1712 exit_restart:
1713 	complete(&ab->restart_completed);
1714 }
1715 
1716 static void ath12k_core_reset(struct work_struct *work)
1717 {
1718 	struct ath12k_base *ab = container_of(work, struct ath12k_base, reset_work);
1719 	struct ath12k_hw_group *ag = ab->ag;
1720 	int reset_count, fail_cont_count, i;
1721 	long time_left;
1722 
1723 	if (!(test_bit(ATH12K_FLAG_QMI_FW_READY_COMPLETE, &ab->dev_flags))) {
1724 		ath12k_warn(ab, "ignore reset dev flags 0x%lx\n", ab->dev_flags);
1725 		return;
1726 	}
1727 
1728 	/* Sometimes the recovery will fail and then the next all recovery fail,
1729 	 * this is to avoid infinite recovery since it can not recovery success
1730 	 */
1731 	fail_cont_count = atomic_read(&ab->fail_cont_count);
1732 
1733 	if (fail_cont_count >= ATH12K_RESET_MAX_FAIL_COUNT_FINAL)
1734 		return;
1735 
1736 	if (fail_cont_count >= ATH12K_RESET_MAX_FAIL_COUNT_FIRST &&
1737 	    time_before(jiffies, ab->reset_fail_timeout))
1738 		return;
1739 
1740 	reset_count = atomic_inc_return(&ab->reset_count);
1741 
1742 	if (reset_count > 1) {
1743 		/* Sometimes it happened another reset worker before the previous one
1744 		 * completed, then the second reset worker will destroy the previous one,
1745 		 * thus below is to avoid that.
1746 		 */
1747 		ath12k_warn(ab, "already resetting count %d\n", reset_count);
1748 
1749 		reinit_completion(&ab->reset_complete);
1750 		time_left = wait_for_completion_timeout(&ab->reset_complete,
1751 							ATH12K_RESET_TIMEOUT_HZ);
1752 		if (time_left) {
1753 			ath12k_dbg(ab, ATH12K_DBG_BOOT, "to skip reset\n");
1754 			atomic_dec(&ab->reset_count);
1755 			return;
1756 		}
1757 
1758 		ab->reset_fail_timeout = jiffies + ATH12K_RESET_FAIL_TIMEOUT_HZ;
1759 		/* Record the continuous recovery fail count when recovery failed*/
1760 		fail_cont_count = atomic_inc_return(&ab->fail_cont_count);
1761 	}
1762 
1763 	ath12k_dbg(ab, ATH12K_DBG_BOOT, "reset starting\n");
1764 
1765 	ab->is_reset = true;
1766 	atomic_set(&ab->recovery_count, 0);
1767 
1768 	ath12k_coredump_collect(ab);
1769 	ath12k_core_pre_reconfigure_recovery(ab);
1770 
1771 	ath12k_core_post_reconfigure_recovery(ab);
1772 
1773 	ath12k_dbg(ab, ATH12K_DBG_BOOT, "waiting recovery start...\n");
1774 
1775 	ath12k_hif_irq_disable(ab);
1776 	ath12k_hif_ce_irq_disable(ab);
1777 
1778 	ath12k_hif_power_down(ab, false);
1779 
1780 	/* prepare for power up */
1781 	ab->qmi.num_radios = U8_MAX;
1782 
1783 	mutex_lock(&ag->mutex);
1784 	ath12k_core_to_group_ref_put(ab);
1785 
1786 	if (ag->num_started > 0) {
1787 		ath12k_dbg(ab, ATH12K_DBG_BOOT,
1788 			   "waiting for %d partner device(s) to reset\n",
1789 			   ag->num_started);
1790 		mutex_unlock(&ag->mutex);
1791 		return;
1792 	}
1793 
1794 	/* Prepare MLO global memory region for power up */
1795 	ath12k_qmi_reset_mlo_mem(ag);
1796 
1797 	for (i = 0; i < ag->num_devices; i++) {
1798 		ab = ag->ab[i];
1799 		if (!ab)
1800 			continue;
1801 
1802 		ath12k_hif_power_up(ab);
1803 		ath12k_dbg(ab, ATH12K_DBG_BOOT, "reset started\n");
1804 	}
1805 
1806 	mutex_unlock(&ag->mutex);
1807 }
1808 
1809 enum ath12k_qmi_mem_mode ath12k_core_get_memory_mode(struct ath12k_base *ab)
1810 {
1811 	unsigned long total_ram;
1812 	struct sysinfo si;
1813 
1814 	si_meminfo(&si);
1815 	total_ram = si.totalram * si.mem_unit;
1816 
1817 	if (total_ram < SZ_512M)
1818 		return ATH12K_QMI_MEMORY_MODE_LOW_512_M;
1819 
1820 	return ATH12K_QMI_MEMORY_MODE_DEFAULT;
1821 }
1822 EXPORT_SYMBOL(ath12k_core_get_memory_mode);
1823 
1824 int ath12k_core_pre_init(struct ath12k_base *ab)
1825 {
1826 	const struct ath12k_mem_profile_based_param *param;
1827 
1828 	param = &ath12k_mem_profile_based_param[ab->target_mem_mode];
1829 	ab->profile_param = param;
1830 	ath12k_fw_map(ab);
1831 
1832 	return 0;
1833 }
1834 
1835 static inline
1836 bool ath12k_core_hw_group_create_ready(struct ath12k_hw_group *ag)
1837 {
1838 	lockdep_assert_held(&ag->mutex);
1839 
1840 	return (ag->num_probed == ag->num_devices);
1841 }
1842 
1843 static struct ath12k_hw_group *ath12k_core_hw_group_alloc(struct ath12k_base *ab)
1844 {
1845 	struct ath12k_hw_group *ag;
1846 	int count = 0;
1847 
1848 	lockdep_assert_held(&ath12k_hw_group_mutex);
1849 
1850 	list_for_each_entry(ag, &ath12k_hw_group_list, list)
1851 		count++;
1852 
1853 	ag = kzalloc_obj(*ag);
1854 	if (!ag)
1855 		return NULL;
1856 
1857 	ag->id = count;
1858 	list_add(&ag->list, &ath12k_hw_group_list);
1859 	mutex_init(&ag->mutex);
1860 	ag->mlo_capable = false;
1861 
1862 	return ag;
1863 }
1864 
1865 static void ath12k_core_free_wsi_info(struct ath12k_hw_group *ag)
1866 {
1867 	int i;
1868 
1869 	for (i = 0; i < ag->num_devices; i++) {
1870 		of_node_put(ag->wsi_node[i]);
1871 		ag->wsi_node[i] = NULL;
1872 	}
1873 	ag->num_devices = 0;
1874 }
1875 
1876 static void ath12k_core_hw_group_free(struct ath12k_hw_group *ag)
1877 {
1878 	mutex_lock(&ath12k_hw_group_mutex);
1879 
1880 	ath12k_core_free_wsi_info(ag);
1881 	list_del(&ag->list);
1882 	kfree(ag);
1883 
1884 	mutex_unlock(&ath12k_hw_group_mutex);
1885 }
1886 
1887 static struct ath12k_hw_group *ath12k_core_hw_group_find_by_dt(struct ath12k_base *ab)
1888 {
1889 	struct ath12k_hw_group *ag;
1890 	int i;
1891 
1892 	if (!ab->dev->of_node)
1893 		return NULL;
1894 
1895 	list_for_each_entry(ag, &ath12k_hw_group_list, list)
1896 		for (i = 0; i < ag->num_devices; i++)
1897 			if (ag->wsi_node[i] == ab->dev->of_node)
1898 				return ag;
1899 
1900 	return NULL;
1901 }
1902 
1903 static int ath12k_core_get_wsi_info(struct ath12k_hw_group *ag,
1904 				    struct ath12k_base *ab)
1905 {
1906 	struct device_node *next_wsi_dev;
1907 	int device_count = 0, ret = 0;
1908 	struct device_node *wsi_dev;
1909 
1910 	wsi_dev = of_node_get(ab->dev->of_node);
1911 	if (!wsi_dev)
1912 		return -ENODEV;
1913 
1914 	do {
1915 		if (device_count >= ATH12K_MAX_DEVICES) {
1916 			ath12k_warn(ab, "device count in DT %d is more than limit %d\n",
1917 				    device_count, ATH12K_MAX_DEVICES);
1918 			ret = -EINVAL;
1919 			break;
1920 		}
1921 
1922 		ag->wsi_node[device_count++] = of_node_get(wsi_dev);
1923 
1924 		struct device_node *tx_endpoint __free(device_node) =
1925 					of_graph_get_endpoint_by_regs(wsi_dev, 0, -1);
1926 		if (!tx_endpoint) {
1927 			ret = -ENODEV;
1928 			break;
1929 		}
1930 
1931 		struct device_node *next_rx_endpoint __free(device_node) =
1932 					of_graph_get_remote_endpoint(tx_endpoint);
1933 		if (!next_rx_endpoint) {
1934 			ret = -ENODEV;
1935 			break;
1936 		}
1937 
1938 		next_wsi_dev = of_graph_get_port_parent(next_rx_endpoint);
1939 		if (!next_wsi_dev) {
1940 			ret = -ENODEV;
1941 			break;
1942 		}
1943 
1944 		of_node_put(wsi_dev);
1945 		wsi_dev = next_wsi_dev;
1946 	} while (ab->dev->of_node != wsi_dev);
1947 
1948 	if (ret) {
1949 		while (--device_count >= 0) {
1950 			of_node_put(ag->wsi_node[device_count]);
1951 			ag->wsi_node[device_count] = NULL;
1952 		}
1953 
1954 		of_node_put(wsi_dev);
1955 		return ret;
1956 	}
1957 
1958 	of_node_put(wsi_dev);
1959 	ag->num_devices = device_count;
1960 
1961 	return 0;
1962 }
1963 
1964 static int ath12k_core_get_wsi_index(struct ath12k_hw_group *ag,
1965 				     struct ath12k_base *ab)
1966 {
1967 	int i, wsi_controller_index = -1, node_index = -1;
1968 	bool control;
1969 
1970 	for (i = 0; i < ag->num_devices; i++) {
1971 		control = of_property_read_bool(ag->wsi_node[i], "qcom,wsi-controller");
1972 		if (control)
1973 			wsi_controller_index = i;
1974 
1975 		if (ag->wsi_node[i] == ab->dev->of_node)
1976 			node_index = i;
1977 	}
1978 
1979 	if (wsi_controller_index == -1) {
1980 		ath12k_dbg(ab, ATH12K_DBG_BOOT, "wsi controller is not defined in dt");
1981 		return -EINVAL;
1982 	}
1983 
1984 	if (node_index == -1) {
1985 		ath12k_dbg(ab, ATH12K_DBG_BOOT, "unable to get WSI node index");
1986 		return -EINVAL;
1987 	}
1988 
1989 	ab->wsi_info.index = (ag->num_devices + node_index - wsi_controller_index) %
1990 		ag->num_devices;
1991 
1992 	return 0;
1993 }
1994 
1995 static struct ath12k_hw_group *ath12k_core_hw_group_assign(struct ath12k_base *ab)
1996 {
1997 	struct ath12k_wsi_info *wsi = &ab->wsi_info;
1998 	struct ath12k_hw_group *ag;
1999 
2000 	lockdep_assert_held(&ath12k_hw_group_mutex);
2001 
2002 	if (ath12k_ftm_mode)
2003 		goto invalid_group;
2004 
2005 	/* The grouping of multiple devices will be done based on device tree file.
2006 	 * The platforms that do not have any valid group information would have
2007 	 * each device to be part of its own invalid group.
2008 	 *
2009 	 * We use group id ATH12K_INVALID_GROUP_ID for single device group
2010 	 * which didn't have dt entry or wrong dt entry, there could be many
2011 	 * groups with same group id, i.e ATH12K_INVALID_GROUP_ID. So
2012 	 * default group id of ATH12K_INVALID_GROUP_ID combined with
2013 	 * num devices in ath12k_hw_group determines if the group is
2014 	 * multi device or single device group
2015 	 */
2016 
2017 	ag = ath12k_core_hw_group_find_by_dt(ab);
2018 	if (!ag) {
2019 		ag = ath12k_core_hw_group_alloc(ab);
2020 		if (!ag) {
2021 			ath12k_warn(ab, "unable to create new hw group\n");
2022 			return NULL;
2023 		}
2024 
2025 		if (ath12k_core_get_wsi_info(ag, ab) ||
2026 		    ath12k_core_get_wsi_index(ag, ab)) {
2027 			ath12k_dbg(ab, ATH12K_DBG_BOOT,
2028 				   "unable to get wsi info from dt, grouping single device");
2029 			ath12k_core_free_wsi_info(ag);
2030 			ag->id = ATH12K_INVALID_GROUP_ID;
2031 			ag->num_devices = 1;
2032 			wsi->index = 0;
2033 		}
2034 
2035 		goto exit;
2036 	} else if (test_bit(ATH12K_GROUP_FLAG_UNREGISTER, &ag->flags)) {
2037 		ath12k_dbg(ab, ATH12K_DBG_BOOT, "group id %d in unregister state\n",
2038 			   ag->id);
2039 		goto invalid_group;
2040 	} else {
2041 		if (ath12k_core_get_wsi_index(ag, ab))
2042 			goto invalid_group;
2043 		goto exit;
2044 	}
2045 
2046 invalid_group:
2047 	ag = ath12k_core_hw_group_alloc(ab);
2048 	if (!ag) {
2049 		ath12k_warn(ab, "unable to create new hw group\n");
2050 		return NULL;
2051 	}
2052 
2053 	ag->id = ATH12K_INVALID_GROUP_ID;
2054 	ag->num_devices = 1;
2055 	wsi->index = 0;
2056 
2057 	ath12k_dbg(ab, ATH12K_DBG_BOOT, "single device added to hardware group\n");
2058 
2059 exit:
2060 	if (ag->num_probed >= ag->num_devices) {
2061 		ath12k_warn(ab, "unable to add new device to group, max limit reached\n");
2062 		goto invalid_group;
2063 	}
2064 
2065 	ab->device_id = ag->num_probed++;
2066 	ag->ab[ab->device_id] = ab;
2067 	ab->ag = ag;
2068 
2069 	ath12k_dp_cmn_hw_group_assign(ath12k_ab_to_dp(ab), ag);
2070 
2071 	ath12k_dbg(ab, ATH12K_DBG_BOOT, "wsi group-id %d num-devices %d index %d",
2072 		   ag->id, ag->num_devices, wsi->index);
2073 
2074 	return ag;
2075 }
2076 
2077 void ath12k_core_hw_group_unassign(struct ath12k_base *ab)
2078 {
2079 	struct ath12k_hw_group *ag = ath12k_ab_to_ag(ab);
2080 	u8 device_id = ab->device_id;
2081 	int num_probed;
2082 
2083 	if (!ag)
2084 		return;
2085 
2086 	mutex_lock(&ag->mutex);
2087 
2088 	if (WARN_ON(device_id >= ag->num_devices)) {
2089 		mutex_unlock(&ag->mutex);
2090 		return;
2091 	}
2092 
2093 	if (WARN_ON(ag->ab[device_id] != ab)) {
2094 		mutex_unlock(&ag->mutex);
2095 		return;
2096 	}
2097 
2098 	ath12k_dp_cmn_hw_group_unassign(ath12k_ab_to_dp(ab), ag);
2099 
2100 	ag->ab[device_id] = NULL;
2101 	ab->ag = NULL;
2102 	ab->device_id = ATH12K_INVALID_DEVICE_ID;
2103 
2104 	if (ag->num_probed)
2105 		ag->num_probed--;
2106 
2107 	num_probed = ag->num_probed;
2108 
2109 	mutex_unlock(&ag->mutex);
2110 
2111 	if (!num_probed)
2112 		ath12k_core_hw_group_free(ag);
2113 }
2114 
2115 static void ath12k_core_hw_group_destroy(struct ath12k_hw_group *ag)
2116 {
2117 	struct ath12k_base *ab;
2118 	int i;
2119 
2120 	if (WARN_ON(!ag))
2121 		return;
2122 
2123 	for (i = 0; i < ag->num_devices; i++) {
2124 		ab = ag->ab[i];
2125 		if (!ab)
2126 			continue;
2127 
2128 		ath12k_core_soc_destroy(ab);
2129 	}
2130 }
2131 
2132 void ath12k_core_hw_group_cleanup(struct ath12k_hw_group *ag)
2133 {
2134 	struct ath12k_base *ab;
2135 	int i;
2136 
2137 	if (!ag)
2138 		return;
2139 
2140 	mutex_lock(&ag->mutex);
2141 
2142 	if (test_bit(ATH12K_GROUP_FLAG_UNREGISTER, &ag->flags)) {
2143 		mutex_unlock(&ag->mutex);
2144 		return;
2145 	}
2146 
2147 	set_bit(ATH12K_GROUP_FLAG_UNREGISTER, &ag->flags);
2148 
2149 	ath12k_core_hw_group_stop(ag);
2150 
2151 	for (i = 0; i < ag->num_devices; i++) {
2152 		ab = ag->ab[i];
2153 		if (!ab)
2154 			continue;
2155 
2156 		mutex_lock(&ab->core_lock);
2157 		ath12k_core_stop(ab);
2158 		mutex_unlock(&ab->core_lock);
2159 	}
2160 
2161 	mutex_unlock(&ag->mutex);
2162 }
2163 
2164 static int ath12k_core_hw_group_create(struct ath12k_hw_group *ag)
2165 {
2166 	struct ath12k_base *ab;
2167 	int i, ret;
2168 
2169 	lockdep_assert_held(&ag->mutex);
2170 
2171 	for (i = 0; i < ag->num_devices; i++) {
2172 		ab = ag->ab[i];
2173 		if (!ab)
2174 			continue;
2175 
2176 		mutex_lock(&ab->core_lock);
2177 
2178 		ret = ath12k_core_soc_create(ab);
2179 		if (ret) {
2180 			mutex_unlock(&ab->core_lock);
2181 			ath12k_err(ab, "failed to create soc %d core: %d\n", i, ret);
2182 			goto destroy;
2183 		}
2184 
2185 		mutex_unlock(&ab->core_lock);
2186 	}
2187 
2188 	return 0;
2189 
2190 destroy:
2191 	for (i--; i >= 0; i--) {
2192 		ab = ag->ab[i];
2193 		if (!ab)
2194 			continue;
2195 
2196 		mutex_lock(&ab->core_lock);
2197 		ath12k_core_soc_destroy(ab);
2198 		mutex_unlock(&ab->core_lock);
2199 	}
2200 
2201 	return ret;
2202 }
2203 
2204 void ath12k_core_hw_group_set_mlo_capable(struct ath12k_hw_group *ag)
2205 {
2206 	struct ath12k_base *ab;
2207 	int i;
2208 
2209 	if (ath12k_ftm_mode)
2210 		return;
2211 
2212 	lockdep_assert_held(&ag->mutex);
2213 
2214 	if (ag->num_devices == 1) {
2215 		ab = ag->ab[0];
2216 		/* QCN9274 firmware uses firmware IE for MLO advertisement */
2217 		if (ab->fw.fw_features_valid) {
2218 			ag->mlo_capable =
2219 				ath12k_fw_feature_supported(ab, ATH12K_FW_FEATURE_MLO);
2220 			return;
2221 		}
2222 
2223 		/* while WCN7850 firmware uses QMI single_chip_mlo_support bit */
2224 		ag->mlo_capable = ab->single_chip_mlo_support;
2225 		return;
2226 	}
2227 
2228 	ag->mlo_capable = true;
2229 
2230 	for (i = 0; i < ag->num_devices; i++) {
2231 		ab = ag->ab[i];
2232 		if (!ab)
2233 			continue;
2234 
2235 		/* even if 1 device's firmware feature indicates MLO
2236 		 * unsupported, make MLO unsupported for the whole group
2237 		 */
2238 		if (!ath12k_fw_feature_supported(ab, ATH12K_FW_FEATURE_MLO)) {
2239 			ag->mlo_capable = false;
2240 			return;
2241 		}
2242 	}
2243 }
2244 
2245 int ath12k_core_init(struct ath12k_base *ab)
2246 {
2247 	struct ath12k_hw_group *ag;
2248 	int ret;
2249 
2250 	mutex_lock(&ath12k_hw_group_mutex);
2251 
2252 	ag = ath12k_core_hw_group_assign(ab);
2253 	if (!ag) {
2254 		mutex_unlock(&ath12k_hw_group_mutex);
2255 		ath12k_warn(ab, "unable to get hw group\n");
2256 		return -ENODEV;
2257 	}
2258 
2259 	mutex_unlock(&ath12k_hw_group_mutex);
2260 
2261 	mutex_lock(&ag->mutex);
2262 
2263 	ath12k_dbg(ab, ATH12K_DBG_BOOT, "num devices %d num probed %d\n",
2264 		   ag->num_devices, ag->num_probed);
2265 
2266 	if (ath12k_core_hw_group_create_ready(ag)) {
2267 		ret = ath12k_core_hw_group_create(ag);
2268 		if (ret) {
2269 			mutex_unlock(&ag->mutex);
2270 			ath12k_warn(ab, "unable to create hw group\n");
2271 			goto err_unassign_hw_group;
2272 		}
2273 	}
2274 
2275 	mutex_unlock(&ag->mutex);
2276 
2277 	return 0;
2278 
2279 err_unassign_hw_group:
2280 	ath12k_core_hw_group_unassign(ab);
2281 
2282 	return ret;
2283 }
2284 
2285 void ath12k_core_deinit(struct ath12k_base *ab)
2286 {
2287 	ath12k_core_hw_group_destroy(ab->ag);
2288 	ath12k_core_hw_group_unassign(ab);
2289 }
2290 
2291 void ath12k_core_free(struct ath12k_base *ab)
2292 {
2293 	timer_delete_sync(&ab->rx_replenish_retry);
2294 	ath12k_wmi_free();
2295 	destroy_workqueue(ab->workqueue_aux);
2296 	destroy_workqueue(ab->workqueue);
2297 	kfree(ab);
2298 }
2299 
2300 struct ath12k_base *ath12k_core_alloc(struct device *dev, size_t priv_size,
2301 				      enum ath12k_bus bus)
2302 {
2303 	struct ath12k_base *ab;
2304 
2305 	ab = kzalloc(sizeof(*ab) + priv_size, GFP_KERNEL);
2306 	if (!ab)
2307 		return NULL;
2308 
2309 	init_completion(&ab->driver_recovery);
2310 
2311 	ab->workqueue = create_singlethread_workqueue("ath12k_wq");
2312 	if (!ab->workqueue)
2313 		goto err_sc_free;
2314 
2315 	ab->workqueue_aux = create_singlethread_workqueue("ath12k_aux_wq");
2316 	if (!ab->workqueue_aux)
2317 		goto err_free_wq;
2318 
2319 	if (ath12k_wmi_alloc() < 0)
2320 		goto err_free_wq_aux;
2321 
2322 	mutex_init(&ab->core_lock);
2323 	spin_lock_init(&ab->base_lock);
2324 	init_completion(&ab->reset_complete);
2325 
2326 	init_waitqueue_head(&ab->peer_mapping_wq);
2327 	init_waitqueue_head(&ab->wmi_ab.tx_credits_wq);
2328 	INIT_WORK(&ab->restart_work, ath12k_core_restart);
2329 	INIT_WORK(&ab->reset_work, ath12k_core_reset);
2330 	INIT_WORK(&ab->rfkill_work, ath12k_rfkill_work);
2331 	INIT_WORK(&ab->dump_work, ath12k_coredump_upload);
2332 	INIT_WORK(&ab->update_11d_work, ath12k_update_11d);
2333 
2334 	timer_setup(&ab->rx_replenish_retry, ath12k_ce_rx_replenish_retry, 0);
2335 	init_completion(&ab->htc_suspend);
2336 	init_completion(&ab->restart_completed);
2337 	init_completion(&ab->wow.wakeup_completed);
2338 
2339 	ab->dev = dev;
2340 	ab->hif.bus = bus;
2341 	ab->qmi.num_radios = U8_MAX;
2342 	ab->single_chip_mlo_support = false;
2343 
2344 	/* Device index used to identify the devices in a group.
2345 	 *
2346 	 * In Intra-device MLO, only one device present in a group,
2347 	 * so it is always zero.
2348 	 *
2349 	 * In Inter-device MLO, Multiple device present in a group,
2350 	 * expect non-zero value.
2351 	 */
2352 	ab->device_id = 0;
2353 
2354 	return ab;
2355 
2356 err_free_wq_aux:
2357 	destroy_workqueue(ab->workqueue_aux);
2358 err_free_wq:
2359 	destroy_workqueue(ab->workqueue);
2360 err_sc_free:
2361 	kfree(ab);
2362 	return NULL;
2363 }
2364 
2365 MODULE_DESCRIPTION("Driver support for Qualcomm Technologies WLAN devices");
2366 MODULE_LICENSE("Dual BSD/GPL");
2367