xref: /linux/drivers/net/wireless/ath/ath12k/core.c (revision ceaebef91d1aff671a512d5f003766ea41152d94)
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 		complete(&ah->peer_ml_id_done);
1549 	}
1550 
1551 	wake_up(&ab->wmi_ab.tx_credits_wq);
1552 	wake_up(&ab->peer_mapping_wq);
1553 }
1554 
1555 static void ath12k_update_11d(struct work_struct *work)
1556 {
1557 	struct ath12k_base *ab = container_of(work, struct ath12k_base, update_11d_work);
1558 	struct ath12k *ar;
1559 	struct ath12k_pdev *pdev;
1560 	struct wmi_set_current_country_arg arg = {};
1561 	int ret, i;
1562 
1563 	spin_lock_bh(&ab->base_lock);
1564 	memcpy(&arg.alpha2, &ab->new_alpha2, 2);
1565 	spin_unlock_bh(&ab->base_lock);
1566 
1567 	ath12k_dbg(ab, ATH12K_DBG_WMI, "update 11d new cc %c%c\n",
1568 		   arg.alpha2[0], arg.alpha2[1]);
1569 
1570 	for (i = 0; i < ab->num_radios; i++) {
1571 		pdev = &ab->pdevs[i];
1572 		ar = pdev->ar;
1573 
1574 		memcpy(&ar->alpha2, &arg.alpha2, 2);
1575 
1576 		reinit_completion(&ar->regd_update_completed);
1577 
1578 		ret = ath12k_wmi_send_set_current_country_cmd(ar, &arg);
1579 		if (ret)
1580 			ath12k_warn(ar->ab,
1581 				    "pdev id %d failed set current country code: %d\n",
1582 				    i, ret);
1583 	}
1584 }
1585 
1586 static void ath12k_core_post_reconfigure_recovery(struct ath12k_base *ab)
1587 {
1588 	struct ath12k_hw_group *ag = ab->ag;
1589 	struct ath12k_hw *ah;
1590 	struct ath12k *ar;
1591 	int i, j;
1592 
1593 	for (i = 0; i < ag->num_hw; i++) {
1594 		ah = ath12k_ag_to_ah(ag, i);
1595 		if (!ah || ah->state == ATH12K_HW_STATE_OFF)
1596 			continue;
1597 
1598 		wiphy_lock(ah->hw->wiphy);
1599 		mutex_lock(&ah->hw_mutex);
1600 
1601 		switch (ah->state) {
1602 		case ATH12K_HW_STATE_ON:
1603 			ah->state = ATH12K_HW_STATE_RESTARTING;
1604 
1605 			for (j = 0; j < ah->num_radio; j++) {
1606 				ar = &ah->radio[j];
1607 				ath12k_core_halt(ar);
1608 			}
1609 
1610 			ath12k_mac_dp_peer_cleanup(ah);
1611 			break;
1612 		case ATH12K_HW_STATE_OFF:
1613 			ath12k_warn(ab,
1614 				    "cannot restart hw %d that hasn't been started\n",
1615 				    i);
1616 			break;
1617 		case ATH12K_HW_STATE_RESTARTING:
1618 			break;
1619 		case ATH12K_HW_STATE_RESTARTED:
1620 			ah->state = ATH12K_HW_STATE_WEDGED;
1621 			fallthrough;
1622 		case ATH12K_HW_STATE_WEDGED:
1623 			ath12k_warn(ab,
1624 				    "device is wedged, will not restart hw %d\n", i);
1625 			break;
1626 		case ATH12K_HW_STATE_TM:
1627 			ath12k_warn(ab, "fw mode reset done radio %d\n", i);
1628 			break;
1629 		}
1630 
1631 		mutex_unlock(&ah->hw_mutex);
1632 		wiphy_unlock(ah->hw->wiphy);
1633 	}
1634 
1635 	complete(&ab->driver_recovery);
1636 }
1637 
1638 static void ath12k_core_restart(struct work_struct *work)
1639 {
1640 	struct ath12k_base *ab = container_of(work, struct ath12k_base, restart_work);
1641 	struct ath12k_hw_group *ag = ab->ag;
1642 	struct ath12k_hw *ah;
1643 	int ret, i;
1644 
1645 	ret = ath12k_core_reconfigure_on_crash(ab);
1646 	if (ret) {
1647 		ath12k_err(ab, "failed to reconfigure driver on crash recovery\n");
1648 		return;
1649 	}
1650 
1651 	if (ab->is_reset) {
1652 		if (!test_bit(ATH12K_FLAG_REGISTERED, &ab->dev_flags)) {
1653 			atomic_dec(&ab->reset_count);
1654 			complete(&ab->reset_complete);
1655 			ab->is_reset = false;
1656 			atomic_set(&ab->fail_cont_count, 0);
1657 			ath12k_dbg(ab, ATH12K_DBG_BOOT, "reset success\n");
1658 		}
1659 
1660 		mutex_lock(&ag->mutex);
1661 
1662 		if (!ath12k_core_hw_group_start_ready(ag)) {
1663 			mutex_unlock(&ag->mutex);
1664 			goto exit_restart;
1665 		}
1666 
1667 		for (i = 0; i < ag->num_hw; i++) {
1668 			ah = ath12k_ag_to_ah(ag, i);
1669 			ieee80211_restart_hw(ah->hw);
1670 		}
1671 
1672 		mutex_unlock(&ag->mutex);
1673 	}
1674 
1675 exit_restart:
1676 	complete(&ab->restart_completed);
1677 }
1678 
1679 static void ath12k_core_reset(struct work_struct *work)
1680 {
1681 	struct ath12k_base *ab = container_of(work, struct ath12k_base, reset_work);
1682 	struct ath12k_hw_group *ag = ab->ag;
1683 	int reset_count, fail_cont_count, i;
1684 	long time_left;
1685 
1686 	if (!(test_bit(ATH12K_FLAG_QMI_FW_READY_COMPLETE, &ab->dev_flags))) {
1687 		ath12k_warn(ab, "ignore reset dev flags 0x%lx\n", ab->dev_flags);
1688 		return;
1689 	}
1690 
1691 	/* Sometimes the recovery will fail and then the next all recovery fail,
1692 	 * this is to avoid infinite recovery since it can not recovery success
1693 	 */
1694 	fail_cont_count = atomic_read(&ab->fail_cont_count);
1695 
1696 	if (fail_cont_count >= ATH12K_RESET_MAX_FAIL_COUNT_FINAL)
1697 		return;
1698 
1699 	if (fail_cont_count >= ATH12K_RESET_MAX_FAIL_COUNT_FIRST &&
1700 	    time_before(jiffies, ab->reset_fail_timeout))
1701 		return;
1702 
1703 	reset_count = atomic_inc_return(&ab->reset_count);
1704 
1705 	if (reset_count > 1) {
1706 		/* Sometimes it happened another reset worker before the previous one
1707 		 * completed, then the second reset worker will destroy the previous one,
1708 		 * thus below is to avoid that.
1709 		 */
1710 		ath12k_warn(ab, "already resetting count %d\n", reset_count);
1711 
1712 		reinit_completion(&ab->reset_complete);
1713 		time_left = wait_for_completion_timeout(&ab->reset_complete,
1714 							ATH12K_RESET_TIMEOUT_HZ);
1715 		if (time_left) {
1716 			ath12k_dbg(ab, ATH12K_DBG_BOOT, "to skip reset\n");
1717 			atomic_dec(&ab->reset_count);
1718 			return;
1719 		}
1720 
1721 		ab->reset_fail_timeout = jiffies + ATH12K_RESET_FAIL_TIMEOUT_HZ;
1722 		/* Record the continuous recovery fail count when recovery failed*/
1723 		fail_cont_count = atomic_inc_return(&ab->fail_cont_count);
1724 	}
1725 
1726 	ath12k_dbg(ab, ATH12K_DBG_BOOT, "reset starting\n");
1727 
1728 	ab->is_reset = true;
1729 	atomic_set(&ab->recovery_count, 0);
1730 
1731 	ath12k_coredump_collect(ab);
1732 	ath12k_core_pre_reconfigure_recovery(ab);
1733 
1734 	ath12k_core_post_reconfigure_recovery(ab);
1735 
1736 	ath12k_dbg(ab, ATH12K_DBG_BOOT, "waiting recovery start...\n");
1737 
1738 	ath12k_hif_irq_disable(ab);
1739 	ath12k_hif_ce_irq_disable(ab);
1740 
1741 	ath12k_hif_power_down(ab, false);
1742 
1743 	/* prepare for power up */
1744 	ab->qmi.num_radios = U8_MAX;
1745 
1746 	mutex_lock(&ag->mutex);
1747 	ath12k_core_to_group_ref_put(ab);
1748 
1749 	if (ag->num_started > 0) {
1750 		ath12k_dbg(ab, ATH12K_DBG_BOOT,
1751 			   "waiting for %d partner device(s) to reset\n",
1752 			   ag->num_started);
1753 		mutex_unlock(&ag->mutex);
1754 		return;
1755 	}
1756 
1757 	/* Prepare MLO global memory region for power up */
1758 	ath12k_qmi_reset_mlo_mem(ag);
1759 
1760 	for (i = 0; i < ag->num_devices; i++) {
1761 		ab = ag->ab[i];
1762 		if (!ab)
1763 			continue;
1764 
1765 		ath12k_hif_power_up(ab);
1766 		ath12k_dbg(ab, ATH12K_DBG_BOOT, "reset started\n");
1767 	}
1768 
1769 	mutex_unlock(&ag->mutex);
1770 }
1771 
1772 enum ath12k_qmi_mem_mode ath12k_core_get_memory_mode(struct ath12k_base *ab)
1773 {
1774 	unsigned long total_ram;
1775 	struct sysinfo si;
1776 
1777 	si_meminfo(&si);
1778 	total_ram = si.totalram * si.mem_unit;
1779 
1780 	if (total_ram < SZ_512M)
1781 		return ATH12K_QMI_MEMORY_MODE_LOW_512_M;
1782 
1783 	return ATH12K_QMI_MEMORY_MODE_DEFAULT;
1784 }
1785 EXPORT_SYMBOL(ath12k_core_get_memory_mode);
1786 
1787 int ath12k_core_pre_init(struct ath12k_base *ab)
1788 {
1789 	const struct ath12k_mem_profile_based_param *param;
1790 
1791 	param = &ath12k_mem_profile_based_param[ab->target_mem_mode];
1792 	ab->profile_param = param;
1793 	ath12k_fw_map(ab);
1794 
1795 	return 0;
1796 }
1797 
1798 static int ath12k_core_panic_handler(struct notifier_block *nb,
1799 				     unsigned long action, void *data)
1800 {
1801 	struct ath12k_base *ab = container_of(nb, struct ath12k_base,
1802 					      panic_nb);
1803 
1804 	return ath12k_hif_panic_handler(ab);
1805 }
1806 
1807 static int ath12k_core_panic_notifier_register(struct ath12k_base *ab)
1808 {
1809 	ab->panic_nb.notifier_call = ath12k_core_panic_handler;
1810 
1811 	return atomic_notifier_chain_register(&panic_notifier_list,
1812 					      &ab->panic_nb);
1813 }
1814 
1815 static void ath12k_core_panic_notifier_unregister(struct ath12k_base *ab)
1816 {
1817 	atomic_notifier_chain_unregister(&panic_notifier_list,
1818 					 &ab->panic_nb);
1819 }
1820 
1821 static inline
1822 bool ath12k_core_hw_group_create_ready(struct ath12k_hw_group *ag)
1823 {
1824 	lockdep_assert_held(&ag->mutex);
1825 
1826 	return (ag->num_probed == ag->num_devices);
1827 }
1828 
1829 static struct ath12k_hw_group *ath12k_core_hw_group_alloc(struct ath12k_base *ab)
1830 {
1831 	struct ath12k_hw_group *ag;
1832 	int count = 0;
1833 
1834 	lockdep_assert_held(&ath12k_hw_group_mutex);
1835 
1836 	list_for_each_entry(ag, &ath12k_hw_group_list, list)
1837 		count++;
1838 
1839 	ag = kzalloc_obj(*ag);
1840 	if (!ag)
1841 		return NULL;
1842 
1843 	ag->id = count;
1844 	list_add(&ag->list, &ath12k_hw_group_list);
1845 	mutex_init(&ag->mutex);
1846 	ag->mlo_capable = false;
1847 
1848 	return ag;
1849 }
1850 
1851 static void ath12k_core_free_wsi_info(struct ath12k_hw_group *ag)
1852 {
1853 	int i;
1854 
1855 	for (i = 0; i < ag->num_devices; i++) {
1856 		of_node_put(ag->wsi_node[i]);
1857 		ag->wsi_node[i] = NULL;
1858 	}
1859 	ag->num_devices = 0;
1860 }
1861 
1862 static void ath12k_core_hw_group_free(struct ath12k_hw_group *ag)
1863 {
1864 	mutex_lock(&ath12k_hw_group_mutex);
1865 
1866 	ath12k_core_free_wsi_info(ag);
1867 	list_del(&ag->list);
1868 	kfree(ag);
1869 
1870 	mutex_unlock(&ath12k_hw_group_mutex);
1871 }
1872 
1873 static struct ath12k_hw_group *ath12k_core_hw_group_find_by_dt(struct ath12k_base *ab)
1874 {
1875 	struct ath12k_hw_group *ag;
1876 	int i;
1877 
1878 	if (!ab->dev->of_node)
1879 		return NULL;
1880 
1881 	list_for_each_entry(ag, &ath12k_hw_group_list, list)
1882 		for (i = 0; i < ag->num_devices; i++)
1883 			if (ag->wsi_node[i] == ab->dev->of_node)
1884 				return ag;
1885 
1886 	return NULL;
1887 }
1888 
1889 static int ath12k_core_get_wsi_info(struct ath12k_hw_group *ag,
1890 				    struct ath12k_base *ab)
1891 {
1892 	struct device_node *next_wsi_dev;
1893 	int device_count = 0, ret = 0;
1894 	struct device_node *wsi_dev;
1895 
1896 	wsi_dev = of_node_get(ab->dev->of_node);
1897 	if (!wsi_dev)
1898 		return -ENODEV;
1899 
1900 	do {
1901 		if (device_count >= ATH12K_MAX_DEVICES) {
1902 			ath12k_warn(ab, "device count in DT %d is more than limit %d\n",
1903 				    device_count, ATH12K_MAX_DEVICES);
1904 			ret = -EINVAL;
1905 			break;
1906 		}
1907 
1908 		ag->wsi_node[device_count++] = of_node_get(wsi_dev);
1909 
1910 		struct device_node *tx_endpoint __free(device_node) =
1911 					of_graph_get_endpoint_by_regs(wsi_dev, 0, -1);
1912 		if (!tx_endpoint) {
1913 			ret = -ENODEV;
1914 			break;
1915 		}
1916 
1917 		struct device_node *next_rx_endpoint __free(device_node) =
1918 					of_graph_get_remote_endpoint(tx_endpoint);
1919 		if (!next_rx_endpoint) {
1920 			ret = -ENODEV;
1921 			break;
1922 		}
1923 
1924 		next_wsi_dev = of_graph_get_port_parent(next_rx_endpoint);
1925 		if (!next_wsi_dev) {
1926 			ret = -ENODEV;
1927 			break;
1928 		}
1929 
1930 		of_node_put(wsi_dev);
1931 		wsi_dev = next_wsi_dev;
1932 	} while (ab->dev->of_node != wsi_dev);
1933 
1934 	if (ret) {
1935 		while (--device_count >= 0) {
1936 			of_node_put(ag->wsi_node[device_count]);
1937 			ag->wsi_node[device_count] = NULL;
1938 		}
1939 
1940 		of_node_put(wsi_dev);
1941 		return ret;
1942 	}
1943 
1944 	of_node_put(wsi_dev);
1945 	ag->num_devices = device_count;
1946 
1947 	return 0;
1948 }
1949 
1950 static int ath12k_core_get_wsi_index(struct ath12k_hw_group *ag,
1951 				     struct ath12k_base *ab)
1952 {
1953 	int i, wsi_controller_index = -1, node_index = -1;
1954 	bool control;
1955 
1956 	for (i = 0; i < ag->num_devices; i++) {
1957 		control = of_property_read_bool(ag->wsi_node[i], "qcom,wsi-controller");
1958 		if (control)
1959 			wsi_controller_index = i;
1960 
1961 		if (ag->wsi_node[i] == ab->dev->of_node)
1962 			node_index = i;
1963 	}
1964 
1965 	if (wsi_controller_index == -1) {
1966 		ath12k_dbg(ab, ATH12K_DBG_BOOT, "wsi controller is not defined in dt");
1967 		return -EINVAL;
1968 	}
1969 
1970 	if (node_index == -1) {
1971 		ath12k_dbg(ab, ATH12K_DBG_BOOT, "unable to get WSI node index");
1972 		return -EINVAL;
1973 	}
1974 
1975 	ab->wsi_info.index = (ag->num_devices + node_index - wsi_controller_index) %
1976 		ag->num_devices;
1977 
1978 	return 0;
1979 }
1980 
1981 static struct ath12k_hw_group *ath12k_core_hw_group_assign(struct ath12k_base *ab)
1982 {
1983 	struct ath12k_wsi_info *wsi = &ab->wsi_info;
1984 	struct ath12k_hw_group *ag;
1985 
1986 	lockdep_assert_held(&ath12k_hw_group_mutex);
1987 
1988 	if (ath12k_ftm_mode)
1989 		goto invalid_group;
1990 
1991 	/* The grouping of multiple devices will be done based on device tree file.
1992 	 * The platforms that do not have any valid group information would have
1993 	 * each device to be part of its own invalid group.
1994 	 *
1995 	 * We use group id ATH12K_INVALID_GROUP_ID for single device group
1996 	 * which didn't have dt entry or wrong dt entry, there could be many
1997 	 * groups with same group id, i.e ATH12K_INVALID_GROUP_ID. So
1998 	 * default group id of ATH12K_INVALID_GROUP_ID combined with
1999 	 * num devices in ath12k_hw_group determines if the group is
2000 	 * multi device or single device group
2001 	 */
2002 
2003 	ag = ath12k_core_hw_group_find_by_dt(ab);
2004 	if (!ag) {
2005 		ag = ath12k_core_hw_group_alloc(ab);
2006 		if (!ag) {
2007 			ath12k_warn(ab, "unable to create new hw group\n");
2008 			return NULL;
2009 		}
2010 
2011 		if (ath12k_core_get_wsi_info(ag, ab) ||
2012 		    ath12k_core_get_wsi_index(ag, ab)) {
2013 			ath12k_dbg(ab, ATH12K_DBG_BOOT,
2014 				   "unable to get wsi info from dt, grouping single device");
2015 			ath12k_core_free_wsi_info(ag);
2016 			ag->id = ATH12K_INVALID_GROUP_ID;
2017 			ag->num_devices = 1;
2018 			wsi->index = 0;
2019 		}
2020 
2021 		goto exit;
2022 	} else if (test_bit(ATH12K_GROUP_FLAG_UNREGISTER, &ag->flags)) {
2023 		ath12k_dbg(ab, ATH12K_DBG_BOOT, "group id %d in unregister state\n",
2024 			   ag->id);
2025 		goto invalid_group;
2026 	} else {
2027 		if (ath12k_core_get_wsi_index(ag, ab))
2028 			goto invalid_group;
2029 		goto exit;
2030 	}
2031 
2032 invalid_group:
2033 	ag = ath12k_core_hw_group_alloc(ab);
2034 	if (!ag) {
2035 		ath12k_warn(ab, "unable to create new hw group\n");
2036 		return NULL;
2037 	}
2038 
2039 	ag->id = ATH12K_INVALID_GROUP_ID;
2040 	ag->num_devices = 1;
2041 	wsi->index = 0;
2042 
2043 	ath12k_dbg(ab, ATH12K_DBG_BOOT, "single device added to hardware group\n");
2044 
2045 exit:
2046 	if (ag->num_probed >= ag->num_devices) {
2047 		ath12k_warn(ab, "unable to add new device to group, max limit reached\n");
2048 		goto invalid_group;
2049 	}
2050 
2051 	ab->device_id = ag->num_probed++;
2052 	ag->ab[ab->device_id] = ab;
2053 	ab->ag = ag;
2054 
2055 	ath12k_dp_cmn_hw_group_assign(ath12k_ab_to_dp(ab), ag);
2056 
2057 	ath12k_dbg(ab, ATH12K_DBG_BOOT, "wsi group-id %d num-devices %d index %d",
2058 		   ag->id, ag->num_devices, wsi->index);
2059 
2060 	return ag;
2061 }
2062 
2063 void ath12k_core_hw_group_unassign(struct ath12k_base *ab)
2064 {
2065 	struct ath12k_hw_group *ag = ath12k_ab_to_ag(ab);
2066 	u8 device_id = ab->device_id;
2067 	int num_probed;
2068 
2069 	if (!ag)
2070 		return;
2071 
2072 	mutex_lock(&ag->mutex);
2073 
2074 	if (WARN_ON(device_id >= ag->num_devices)) {
2075 		mutex_unlock(&ag->mutex);
2076 		return;
2077 	}
2078 
2079 	if (WARN_ON(ag->ab[device_id] != ab)) {
2080 		mutex_unlock(&ag->mutex);
2081 		return;
2082 	}
2083 
2084 	ath12k_dp_cmn_hw_group_unassign(ath12k_ab_to_dp(ab), ag);
2085 
2086 	ag->ab[device_id] = NULL;
2087 	ab->ag = NULL;
2088 	ab->device_id = ATH12K_INVALID_DEVICE_ID;
2089 
2090 	if (ag->num_probed)
2091 		ag->num_probed--;
2092 
2093 	num_probed = ag->num_probed;
2094 
2095 	mutex_unlock(&ag->mutex);
2096 
2097 	if (!num_probed)
2098 		ath12k_core_hw_group_free(ag);
2099 }
2100 
2101 static void ath12k_core_hw_group_destroy(struct ath12k_hw_group *ag)
2102 {
2103 	struct ath12k_base *ab;
2104 	int i;
2105 
2106 	if (WARN_ON(!ag))
2107 		return;
2108 
2109 	for (i = 0; i < ag->num_devices; i++) {
2110 		ab = ag->ab[i];
2111 		if (!ab)
2112 			continue;
2113 
2114 		ath12k_core_soc_destroy(ab);
2115 	}
2116 }
2117 
2118 void ath12k_core_hw_group_cleanup(struct ath12k_hw_group *ag)
2119 {
2120 	struct ath12k_base *ab;
2121 	int i;
2122 
2123 	if (!ag)
2124 		return;
2125 
2126 	mutex_lock(&ag->mutex);
2127 
2128 	if (test_bit(ATH12K_GROUP_FLAG_UNREGISTER, &ag->flags)) {
2129 		mutex_unlock(&ag->mutex);
2130 		return;
2131 	}
2132 
2133 	set_bit(ATH12K_GROUP_FLAG_UNREGISTER, &ag->flags);
2134 
2135 	ath12k_core_hw_group_stop(ag);
2136 
2137 	for (i = 0; i < ag->num_devices; i++) {
2138 		ab = ag->ab[i];
2139 		if (!ab)
2140 			continue;
2141 
2142 		mutex_lock(&ab->core_lock);
2143 		ath12k_core_stop(ab);
2144 		mutex_unlock(&ab->core_lock);
2145 	}
2146 
2147 	mutex_unlock(&ag->mutex);
2148 }
2149 
2150 static int ath12k_core_hw_group_create(struct ath12k_hw_group *ag)
2151 {
2152 	struct ath12k_base *ab;
2153 	int i, ret;
2154 
2155 	lockdep_assert_held(&ag->mutex);
2156 
2157 	for (i = 0; i < ag->num_devices; i++) {
2158 		ab = ag->ab[i];
2159 		if (!ab)
2160 			continue;
2161 
2162 		mutex_lock(&ab->core_lock);
2163 
2164 		ret = ath12k_core_soc_create(ab);
2165 		if (ret) {
2166 			mutex_unlock(&ab->core_lock);
2167 			ath12k_err(ab, "failed to create soc %d core: %d\n", i, ret);
2168 			goto destroy;
2169 		}
2170 
2171 		mutex_unlock(&ab->core_lock);
2172 	}
2173 
2174 	return 0;
2175 
2176 destroy:
2177 	for (i--; i >= 0; i--) {
2178 		ab = ag->ab[i];
2179 		if (!ab)
2180 			continue;
2181 
2182 		mutex_lock(&ab->core_lock);
2183 		ath12k_core_soc_destroy(ab);
2184 		mutex_unlock(&ab->core_lock);
2185 	}
2186 
2187 	return ret;
2188 }
2189 
2190 void ath12k_core_hw_group_set_mlo_capable(struct ath12k_hw_group *ag)
2191 {
2192 	struct ath12k_base *ab;
2193 	int i;
2194 
2195 	if (ath12k_ftm_mode)
2196 		return;
2197 
2198 	lockdep_assert_held(&ag->mutex);
2199 
2200 	if (ag->num_devices == 1) {
2201 		ab = ag->ab[0];
2202 		/* QCN9274 firmware uses firmware IE for MLO advertisement */
2203 		if (ab->fw.fw_features_valid) {
2204 			ag->mlo_capable =
2205 				ath12k_fw_feature_supported(ab, ATH12K_FW_FEATURE_MLO);
2206 			return;
2207 		}
2208 
2209 		/* while WCN7850 firmware uses QMI single_chip_mlo_support bit */
2210 		ag->mlo_capable = ab->single_chip_mlo_support;
2211 		return;
2212 	}
2213 
2214 	ag->mlo_capable = true;
2215 
2216 	for (i = 0; i < ag->num_devices; i++) {
2217 		ab = ag->ab[i];
2218 		if (!ab)
2219 			continue;
2220 
2221 		/* even if 1 device's firmware feature indicates MLO
2222 		 * unsupported, make MLO unsupported for the whole group
2223 		 */
2224 		if (!ath12k_fw_feature_supported(ab, ATH12K_FW_FEATURE_MLO)) {
2225 			ag->mlo_capable = false;
2226 			return;
2227 		}
2228 	}
2229 }
2230 
2231 int ath12k_core_init(struct ath12k_base *ab)
2232 {
2233 	struct ath12k_hw_group *ag;
2234 	int ret;
2235 
2236 	ret = ath12k_core_panic_notifier_register(ab);
2237 	if (ret)
2238 		ath12k_warn(ab, "failed to register panic handler: %d\n", ret);
2239 
2240 	mutex_lock(&ath12k_hw_group_mutex);
2241 
2242 	ag = ath12k_core_hw_group_assign(ab);
2243 	if (!ag) {
2244 		mutex_unlock(&ath12k_hw_group_mutex);
2245 		ath12k_warn(ab, "unable to get hw group\n");
2246 		ret = -ENODEV;
2247 		goto err_unregister_notifier;
2248 	}
2249 
2250 	mutex_unlock(&ath12k_hw_group_mutex);
2251 
2252 	mutex_lock(&ag->mutex);
2253 
2254 	ath12k_dbg(ab, ATH12K_DBG_BOOT, "num devices %d num probed %d\n",
2255 		   ag->num_devices, ag->num_probed);
2256 
2257 	if (ath12k_core_hw_group_create_ready(ag)) {
2258 		ret = ath12k_core_hw_group_create(ag);
2259 		if (ret) {
2260 			mutex_unlock(&ag->mutex);
2261 			ath12k_warn(ab, "unable to create hw group\n");
2262 			goto err_unassign_hw_group;
2263 		}
2264 	}
2265 
2266 	mutex_unlock(&ag->mutex);
2267 
2268 	return 0;
2269 
2270 err_unassign_hw_group:
2271 	ath12k_core_hw_group_unassign(ab);
2272 err_unregister_notifier:
2273 	ath12k_core_panic_notifier_unregister(ab);
2274 
2275 	return ret;
2276 }
2277 
2278 void ath12k_core_deinit(struct ath12k_base *ab)
2279 {
2280 	ath12k_core_hw_group_destroy(ab->ag);
2281 	ath12k_core_hw_group_unassign(ab);
2282 	ath12k_core_panic_notifier_unregister(ab);
2283 }
2284 
2285 void ath12k_core_free(struct ath12k_base *ab)
2286 {
2287 	timer_delete_sync(&ab->rx_replenish_retry);
2288 	ath12k_wmi_free();
2289 	destroy_workqueue(ab->workqueue_aux);
2290 	destroy_workqueue(ab->workqueue);
2291 	kfree(ab);
2292 }
2293 
2294 struct ath12k_base *ath12k_core_alloc(struct device *dev, size_t priv_size,
2295 				      enum ath12k_bus bus)
2296 {
2297 	struct ath12k_base *ab;
2298 
2299 	ab = kzalloc(sizeof(*ab) + priv_size, GFP_KERNEL);
2300 	if (!ab)
2301 		return NULL;
2302 
2303 	init_completion(&ab->driver_recovery);
2304 
2305 	ab->workqueue = create_singlethread_workqueue("ath12k_wq");
2306 	if (!ab->workqueue)
2307 		goto err_sc_free;
2308 
2309 	ab->workqueue_aux = create_singlethread_workqueue("ath12k_aux_wq");
2310 	if (!ab->workqueue_aux)
2311 		goto err_free_wq;
2312 
2313 	if (ath12k_wmi_alloc() < 0)
2314 		goto err_free_wq_aux;
2315 
2316 	mutex_init(&ab->core_lock);
2317 	spin_lock_init(&ab->base_lock);
2318 	init_completion(&ab->reset_complete);
2319 
2320 	init_waitqueue_head(&ab->peer_mapping_wq);
2321 	init_waitqueue_head(&ab->wmi_ab.tx_credits_wq);
2322 	INIT_WORK(&ab->restart_work, ath12k_core_restart);
2323 	INIT_WORK(&ab->reset_work, ath12k_core_reset);
2324 	INIT_WORK(&ab->rfkill_work, ath12k_rfkill_work);
2325 	INIT_WORK(&ab->dump_work, ath12k_coredump_upload);
2326 	INIT_WORK(&ab->update_11d_work, ath12k_update_11d);
2327 
2328 	timer_setup(&ab->rx_replenish_retry, ath12k_ce_rx_replenish_retry, 0);
2329 	init_completion(&ab->htc_suspend);
2330 	init_completion(&ab->restart_completed);
2331 	init_completion(&ab->wow.wakeup_completed);
2332 
2333 	ab->dev = dev;
2334 	ab->hif.bus = bus;
2335 	ab->qmi.num_radios = U8_MAX;
2336 	ab->single_chip_mlo_support = false;
2337 
2338 	/* Device index used to identify the devices in a group.
2339 	 *
2340 	 * In Intra-device MLO, only one device present in a group,
2341 	 * so it is always zero.
2342 	 *
2343 	 * In Inter-device MLO, Multiple device present in a group,
2344 	 * expect non-zero value.
2345 	 */
2346 	ab->device_id = 0;
2347 
2348 	return ab;
2349 
2350 err_free_wq_aux:
2351 	destroy_workqueue(ab->workqueue_aux);
2352 err_free_wq:
2353 	destroy_workqueue(ab->workqueue);
2354 err_sc_free:
2355 	kfree(ab);
2356 	return NULL;
2357 }
2358 
2359 MODULE_DESCRIPTION("Driver support for Qualcomm Technologies WLAN devices");
2360 MODULE_LICENSE("Dual BSD/GPL");
2361