at commit c03c1a14

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