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