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