1 // SPDX-License-Identifier: BSD-3-Clause-Clear
2 /*
3 * Copyright (c) 2018-2019 The Linux Foundation. All rights reserved.
4 */
5
6 #include <linux/module.h>
7 #include <linux/slab.h>
8 #include <linux/remoteproc.h>
9 #include <linux/firmware.h>
10 #include "core.h"
11 #include "dp_tx.h"
12 #include "dp_rx.h"
13 #include "debug.h"
14 #include "hif.h"
15
16 unsigned int ath11k_debug_mask;
17 EXPORT_SYMBOL(ath11k_debug_mask);
18 module_param_named(debug_mask, ath11k_debug_mask, uint, 0644);
19 MODULE_PARM_DESC(debug_mask, "Debugging mask");
20
21 static unsigned int ath11k_crypto_mode;
22 module_param_named(crypto_mode, ath11k_crypto_mode, uint, 0644);
23 MODULE_PARM_DESC(crypto_mode, "crypto mode: 0-hardware, 1-software");
24
25 /* frame mode values are mapped as per enum ath11k_hw_txrx_mode */
26 unsigned int ath11k_frame_mode = ATH11K_HW_TXRX_NATIVE_WIFI;
27 module_param_named(frame_mode, ath11k_frame_mode, uint, 0644);
28 MODULE_PARM_DESC(frame_mode,
29 "Datapath frame mode (0: raw, 1: native wifi (default), 2: ethernet)");
30
31 static const struct ath11k_hw_params ath11k_hw_params[] = {
32 {
33 .hw_rev = ATH11K_HW_IPQ8074,
34 .name = "ipq8074 hw2.0",
35 .fw = {
36 .dir = "IPQ8074/hw2.0",
37 .board_size = 256 * 1024,
38 .cal_size = 256 * 1024,
39 },
40 .max_radios = 3,
41 .bdf_addr = 0x4B0C0000,
42 .hw_ops = &ipq8074_ops,
43 .ring_mask = &ath11k_hw_ring_mask_ipq8074,
44 .internal_sleep_clock = false,
45 .regs = &ipq8074_regs,
46 .host_ce_config = ath11k_host_ce_config_ipq8074,
47 .ce_count = 12,
48 .target_ce_config = ath11k_target_ce_config_wlan_ipq8074,
49 .target_ce_count = 11,
50 .svc_to_ce_map = ath11k_target_service_to_ce_map_wlan_ipq8074,
51 .svc_to_ce_map_len = 21,
52 .single_pdev_only = false,
53 .needs_band_to_mac = true,
54 .rxdma1_enable = true,
55 .num_rxmda_per_pdev = 1,
56 .rx_mac_buf_ring = false,
57 .vdev_start_delay = false,
58 .htt_peer_map_v2 = true,
59 .tcl_0_only = false,
60 .spectral_fft_sz = 2,
61
62 .interface_modes = BIT(NL80211_IFTYPE_STATION) |
63 BIT(NL80211_IFTYPE_AP) |
64 BIT(NL80211_IFTYPE_MESH_POINT),
65 .supports_monitor = true,
66 .supports_shadow_regs = false,
67 .idle_ps = false,
68 },
69 {
70 .hw_rev = ATH11K_HW_IPQ6018_HW10,
71 .name = "ipq6018 hw1.0",
72 .fw = {
73 .dir = "IPQ6018/hw1.0",
74 .board_size = 256 * 1024,
75 .cal_size = 256 * 1024,
76 },
77 .max_radios = 2,
78 .bdf_addr = 0x4ABC0000,
79 .hw_ops = &ipq6018_ops,
80 .ring_mask = &ath11k_hw_ring_mask_ipq8074,
81 .internal_sleep_clock = false,
82 .regs = &ipq8074_regs,
83 .host_ce_config = ath11k_host_ce_config_ipq8074,
84 .ce_count = 12,
85 .target_ce_config = ath11k_target_ce_config_wlan_ipq8074,
86 .target_ce_count = 11,
87 .svc_to_ce_map = ath11k_target_service_to_ce_map_wlan_ipq6018,
88 .svc_to_ce_map_len = 19,
89 .single_pdev_only = false,
90 .needs_band_to_mac = true,
91 .rxdma1_enable = true,
92 .num_rxmda_per_pdev = 1,
93 .rx_mac_buf_ring = false,
94 .vdev_start_delay = false,
95 .htt_peer_map_v2 = true,
96 .tcl_0_only = false,
97 .spectral_fft_sz = 4,
98
99 .interface_modes = BIT(NL80211_IFTYPE_STATION) |
100 BIT(NL80211_IFTYPE_AP) |
101 BIT(NL80211_IFTYPE_MESH_POINT),
102 .supports_monitor = true,
103 .supports_shadow_regs = false,
104 .idle_ps = false,
105 },
106 {
107 .name = "qca6390 hw2.0",
108 .hw_rev = ATH11K_HW_QCA6390_HW20,
109 .fw = {
110 .dir = "QCA6390/hw2.0",
111 .board_size = 256 * 1024,
112 .cal_size = 256 * 1024,
113 },
114 .max_radios = 3,
115 .bdf_addr = 0x4B0C0000,
116 .hw_ops = &qca6390_ops,
117 .ring_mask = &ath11k_hw_ring_mask_qca6390,
118 .internal_sleep_clock = true,
119 .regs = &qca6390_regs,
120 .host_ce_config = ath11k_host_ce_config_qca6390,
121 .ce_count = 9,
122 .target_ce_config = ath11k_target_ce_config_wlan_qca6390,
123 .target_ce_count = 9,
124 .svc_to_ce_map = ath11k_target_service_to_ce_map_wlan_qca6390,
125 .svc_to_ce_map_len = 14,
126 .single_pdev_only = true,
127 .needs_band_to_mac = false,
128 .rxdma1_enable = false,
129 .num_rxmda_per_pdev = 2,
130 .rx_mac_buf_ring = true,
131 .vdev_start_delay = true,
132 .htt_peer_map_v2 = false,
133 .tcl_0_only = true,
134 .spectral_fft_sz = 0,
135
136 .interface_modes = BIT(NL80211_IFTYPE_STATION) |
137 BIT(NL80211_IFTYPE_AP),
138 .supports_monitor = false,
139 .supports_shadow_regs = true,
140 .idle_ps = true,
141 },
142 };
143
ath11k_core_create_board_name(struct ath11k_base * ab,char * name,size_t name_len)144 static int ath11k_core_create_board_name(struct ath11k_base *ab, char *name,
145 size_t name_len)
146 {
147 scnprintf(name, name_len,
148 "bus=%s,qmi-chip-id=%d,qmi-board-id=%d",
149 ath11k_bus_str(ab->hif.bus),
150 ab->qmi.target.chip_id,
151 ab->qmi.target.board_id);
152
153 ath11k_dbg(ab, ATH11K_DBG_BOOT, "boot using board name '%s'\n", name);
154
155 return 0;
156 }
157
ath11k_core_firmware_request(struct ath11k_base * ab,const char * file)158 const struct firmware *ath11k_core_firmware_request(struct ath11k_base *ab,
159 const char *file)
160 {
161 const struct firmware *fw;
162 char path[100];
163 int ret;
164
165 if (file == NULL)
166 return ERR_PTR(-ENOENT);
167
168 ath11k_core_create_firmware_path(ab, file, path, sizeof(path));
169
170 ret = firmware_request_nowarn(&fw, path, ab->dev);
171 if (ret)
172 return ERR_PTR(ret);
173
174 ath11k_dbg(ab, ATH11K_DBG_BOOT, "boot firmware request %s size %zu\n",
175 path, fw->size);
176
177 return fw;
178 }
179
ath11k_core_free_bdf(struct ath11k_base * ab,struct ath11k_board_data * bd)180 void ath11k_core_free_bdf(struct ath11k_base *ab, struct ath11k_board_data *bd)
181 {
182 if (!IS_ERR(bd->fw))
183 release_firmware(bd->fw);
184
185 memset(bd, 0, sizeof(*bd));
186 }
187
ath11k_core_parse_bd_ie_board(struct ath11k_base * ab,struct ath11k_board_data * bd,const void * buf,size_t buf_len,const char * boardname,int bd_ie_type)188 static int ath11k_core_parse_bd_ie_board(struct ath11k_base *ab,
189 struct ath11k_board_data *bd,
190 const void *buf, size_t buf_len,
191 const char *boardname,
192 int bd_ie_type)
193 {
194 const struct ath11k_fw_ie *hdr;
195 bool name_match_found;
196 int ret, board_ie_id;
197 size_t board_ie_len;
198 const void *board_ie_data;
199
200 name_match_found = false;
201
202 /* go through ATH11K_BD_IE_BOARD_ elements */
203 while (buf_len > sizeof(struct ath11k_fw_ie)) {
204 hdr = buf;
205 board_ie_id = le32_to_cpu(hdr->id);
206 board_ie_len = le32_to_cpu(hdr->len);
207 board_ie_data = hdr->data;
208
209 buf_len -= sizeof(*hdr);
210 buf += sizeof(*hdr);
211
212 if (buf_len < ALIGN(board_ie_len, 4)) {
213 ath11k_err(ab, "invalid ATH11K_BD_IE_BOARD length: %zu < %zu\n",
214 buf_len, ALIGN(board_ie_len, 4));
215 ret = -EINVAL;
216 goto out;
217 }
218
219 switch (board_ie_id) {
220 case ATH11K_BD_IE_BOARD_NAME:
221 ath11k_dbg_dump(ab, ATH11K_DBG_BOOT, "board name", "",
222 board_ie_data, board_ie_len);
223
224 if (board_ie_len != strlen(boardname))
225 break;
226
227 ret = memcmp(board_ie_data, boardname, strlen(boardname));
228 if (ret)
229 break;
230
231 name_match_found = true;
232 ath11k_dbg(ab, ATH11K_DBG_BOOT,
233 "boot found match for name '%s'",
234 boardname);
235 break;
236 case ATH11K_BD_IE_BOARD_DATA:
237 if (!name_match_found)
238 /* no match found */
239 break;
240
241 ath11k_dbg(ab, ATH11K_DBG_BOOT,
242 "boot found board data for '%s'", boardname);
243
244 bd->data = board_ie_data;
245 bd->len = board_ie_len;
246
247 ret = 0;
248 goto out;
249 default:
250 ath11k_warn(ab, "unknown ATH11K_BD_IE_BOARD found: %d\n",
251 board_ie_id);
252 break;
253 }
254
255 /* jump over the padding */
256 board_ie_len = ALIGN(board_ie_len, 4);
257
258 buf_len -= board_ie_len;
259 buf += board_ie_len;
260 }
261
262 /* no match found */
263 ret = -ENOENT;
264
265 out:
266 return ret;
267 }
268
ath11k_core_fetch_board_data_api_n(struct ath11k_base * ab,struct ath11k_board_data * bd,const char * boardname)269 static int ath11k_core_fetch_board_data_api_n(struct ath11k_base *ab,
270 struct ath11k_board_data *bd,
271 const char *boardname)
272 {
273 size_t len, magic_len;
274 const u8 *data;
275 char *filename, filepath[100];
276 size_t ie_len;
277 struct ath11k_fw_ie *hdr;
278 int ret, ie_id;
279
280 filename = ATH11K_BOARD_API2_FILE;
281
282 if (!bd->fw)
283 bd->fw = ath11k_core_firmware_request(ab, filename);
284
285 if (IS_ERR(bd->fw))
286 return PTR_ERR(bd->fw);
287
288 data = bd->fw->data;
289 len = bd->fw->size;
290
291 ath11k_core_create_firmware_path(ab, filename,
292 filepath, sizeof(filepath));
293
294 /* magic has extra null byte padded */
295 magic_len = strlen(ATH11K_BOARD_MAGIC) + 1;
296 if (len < magic_len) {
297 ath11k_err(ab, "failed to find magic value in %s, file too short: %zu\n",
298 filepath, len);
299 ret = -EINVAL;
300 goto err;
301 }
302
303 if (memcmp(data, ATH11K_BOARD_MAGIC, magic_len)) {
304 ath11k_err(ab, "found invalid board magic\n");
305 ret = -EINVAL;
306 goto err;
307 }
308
309 /* magic is padded to 4 bytes */
310 magic_len = ALIGN(magic_len, 4);
311 if (len < magic_len) {
312 ath11k_err(ab, "failed: %s too small to contain board data, len: %zu\n",
313 filepath, len);
314 ret = -EINVAL;
315 goto err;
316 }
317
318 data += magic_len;
319 len -= magic_len;
320
321 while (len > sizeof(struct ath11k_fw_ie)) {
322 hdr = (struct ath11k_fw_ie *)data;
323 ie_id = le32_to_cpu(hdr->id);
324 ie_len = le32_to_cpu(hdr->len);
325
326 len -= sizeof(*hdr);
327 data = hdr->data;
328
329 if (len < ALIGN(ie_len, 4)) {
330 ath11k_err(ab, "invalid length for board ie_id %d ie_len %zu len %zu\n",
331 ie_id, ie_len, len);
332 ret = -EINVAL;
333 goto err;
334 }
335
336 switch (ie_id) {
337 case ATH11K_BD_IE_BOARD:
338 ret = ath11k_core_parse_bd_ie_board(ab, bd, data,
339 ie_len,
340 boardname,
341 ATH11K_BD_IE_BOARD);
342 if (ret == -ENOENT)
343 /* no match found, continue */
344 break;
345 else if (ret)
346 /* there was an error, bail out */
347 goto err;
348 /* either found or error, so stop searching */
349 goto out;
350 }
351
352 /* jump over the padding */
353 ie_len = ALIGN(ie_len, 4);
354
355 len -= ie_len;
356 data += ie_len;
357 }
358
359 out:
360 if (!bd->data || !bd->len) {
361 ath11k_err(ab,
362 "failed to fetch board data for %s from %s\n",
363 boardname, filepath);
364 ret = -ENODATA;
365 goto err;
366 }
367
368 return 0;
369
370 err:
371 ath11k_core_free_bdf(ab, bd);
372 return ret;
373 }
374
ath11k_core_fetch_board_data_api_1(struct ath11k_base * ab,struct ath11k_board_data * bd)375 static int ath11k_core_fetch_board_data_api_1(struct ath11k_base *ab,
376 struct ath11k_board_data *bd)
377 {
378 bd->fw = ath11k_core_firmware_request(ab, ATH11K_DEFAULT_BOARD_FILE);
379 if (IS_ERR(bd->fw))
380 return PTR_ERR(bd->fw);
381
382 bd->data = bd->fw->data;
383 bd->len = bd->fw->size;
384
385 return 0;
386 }
387
388 #define BOARD_NAME_SIZE 100
ath11k_core_fetch_bdf(struct ath11k_base * ab,struct ath11k_board_data * bd)389 int ath11k_core_fetch_bdf(struct ath11k_base *ab, struct ath11k_board_data *bd)
390 {
391 char boardname[BOARD_NAME_SIZE];
392 int ret;
393
394 ret = ath11k_core_create_board_name(ab, boardname, BOARD_NAME_SIZE);
395 if (ret) {
396 ath11k_err(ab, "failed to create board name: %d", ret);
397 return ret;
398 }
399
400 ab->bd_api = 2;
401 ret = ath11k_core_fetch_board_data_api_n(ab, bd, boardname);
402 if (!ret)
403 goto success;
404
405 ab->bd_api = 1;
406 ret = ath11k_core_fetch_board_data_api_1(ab, bd);
407 if (ret) {
408 ath11k_err(ab, "failed to fetch board-2.bin or board.bin from %s\n",
409 ab->hw_params.fw.dir);
410 return ret;
411 }
412
413 success:
414 ath11k_dbg(ab, ATH11K_DBG_BOOT, "using board api %d\n", ab->bd_api);
415 return 0;
416 }
417
ath11k_core_stop(struct ath11k_base * ab)418 static void ath11k_core_stop(struct ath11k_base *ab)
419 {
420 if (!test_bit(ATH11K_FLAG_CRASH_FLUSH, &ab->dev_flags))
421 ath11k_qmi_firmware_stop(ab);
422
423 ath11k_hif_stop(ab);
424 ath11k_wmi_detach(ab);
425 ath11k_dp_pdev_reo_cleanup(ab);
426
427 /* De-Init of components as needed */
428 }
429
ath11k_core_soc_create(struct ath11k_base * ab)430 static int ath11k_core_soc_create(struct ath11k_base *ab)
431 {
432 int ret;
433
434 ret = ath11k_qmi_init_service(ab);
435 if (ret) {
436 ath11k_err(ab, "failed to initialize qmi :%d\n", ret);
437 return ret;
438 }
439
440 ret = ath11k_debugfs_soc_create(ab);
441 if (ret) {
442 ath11k_err(ab, "failed to create ath11k debugfs\n");
443 goto err_qmi_deinit;
444 }
445
446 ret = ath11k_hif_power_up(ab);
447 if (ret) {
448 ath11k_err(ab, "failed to power up :%d\n", ret);
449 goto err_debugfs_reg;
450 }
451
452 return 0;
453
454 err_debugfs_reg:
455 ath11k_debugfs_soc_destroy(ab);
456 err_qmi_deinit:
457 ath11k_qmi_deinit_service(ab);
458 return ret;
459 }
460
ath11k_core_soc_destroy(struct ath11k_base * ab)461 static void ath11k_core_soc_destroy(struct ath11k_base *ab)
462 {
463 ath11k_debugfs_soc_destroy(ab);
464 ath11k_dp_free(ab);
465 ath11k_reg_free(ab);
466 ath11k_qmi_deinit_service(ab);
467 }
468
ath11k_core_pdev_create(struct ath11k_base * ab)469 static int ath11k_core_pdev_create(struct ath11k_base *ab)
470 {
471 int ret;
472
473 ret = ath11k_debugfs_pdev_create(ab);
474 if (ret) {
475 ath11k_err(ab, "failed to create core pdev debugfs: %d\n", ret);
476 return ret;
477 }
478
479 ret = ath11k_dp_pdev_alloc(ab);
480 if (ret) {
481 ath11k_err(ab, "failed to attach DP pdev: %d\n", ret);
482 goto err_pdev_debug;
483 }
484
485 ret = ath11k_mac_register(ab);
486 if (ret) {
487 ath11k_err(ab, "failed register the radio with mac80211: %d\n", ret);
488 goto err_dp_pdev_free;
489 }
490
491 ret = ath11k_thermal_register(ab);
492 if (ret) {
493 ath11k_err(ab, "could not register thermal device: %d\n",
494 ret);
495 goto err_mac_unregister;
496 }
497
498 ret = ath11k_spectral_init(ab);
499 if (ret) {
500 ath11k_err(ab, "failed to init spectral %d\n", ret);
501 goto err_thermal_unregister;
502 }
503
504 return 0;
505
506 err_thermal_unregister:
507 ath11k_thermal_unregister(ab);
508 err_mac_unregister:
509 ath11k_mac_unregister(ab);
510 err_dp_pdev_free:
511 ath11k_dp_pdev_free(ab);
512 err_pdev_debug:
513 ath11k_debugfs_pdev_destroy(ab);
514
515 return ret;
516 }
517
ath11k_core_pdev_destroy(struct ath11k_base * ab)518 static void ath11k_core_pdev_destroy(struct ath11k_base *ab)
519 {
520 ath11k_spectral_deinit(ab);
521 ath11k_thermal_unregister(ab);
522 ath11k_mac_unregister(ab);
523 ath11k_hif_irq_disable(ab);
524 ath11k_dp_pdev_free(ab);
525 ath11k_debugfs_pdev_destroy(ab);
526 }
527
ath11k_core_start(struct ath11k_base * ab,enum ath11k_firmware_mode mode)528 static int ath11k_core_start(struct ath11k_base *ab,
529 enum ath11k_firmware_mode mode)
530 {
531 int ret;
532
533 ret = ath11k_qmi_firmware_start(ab, mode);
534 if (ret) {
535 ath11k_err(ab, "failed to attach wmi: %d\n", ret);
536 return ret;
537 }
538
539 ret = ath11k_wmi_attach(ab);
540 if (ret) {
541 ath11k_err(ab, "failed to attach wmi: %d\n", ret);
542 goto err_firmware_stop;
543 }
544
545 ret = ath11k_htc_init(ab);
546 if (ret) {
547 ath11k_err(ab, "failed to init htc: %d\n", ret);
548 goto err_wmi_detach;
549 }
550
551 ret = ath11k_hif_start(ab);
552 if (ret) {
553 ath11k_err(ab, "failed to start HIF: %d\n", ret);
554 goto err_wmi_detach;
555 }
556
557 ret = ath11k_htc_wait_target(&ab->htc);
558 if (ret) {
559 ath11k_err(ab, "failed to connect to HTC: %d\n", ret);
560 goto err_hif_stop;
561 }
562
563 ret = ath11k_dp_htt_connect(&ab->dp);
564 if (ret) {
565 ath11k_err(ab, "failed to connect to HTT: %d\n", ret);
566 goto err_hif_stop;
567 }
568
569 ret = ath11k_wmi_connect(ab);
570 if (ret) {
571 ath11k_err(ab, "failed to connect wmi: %d\n", ret);
572 goto err_hif_stop;
573 }
574
575 ret = ath11k_htc_start(&ab->htc);
576 if (ret) {
577 ath11k_err(ab, "failed to start HTC: %d\n", ret);
578 goto err_hif_stop;
579 }
580
581 ret = ath11k_wmi_wait_for_service_ready(ab);
582 if (ret) {
583 ath11k_err(ab, "failed to receive wmi service ready event: %d\n",
584 ret);
585 goto err_hif_stop;
586 }
587
588 ret = ath11k_mac_allocate(ab);
589 if (ret) {
590 ath11k_err(ab, "failed to create new hw device with mac80211 :%d\n",
591 ret);
592 goto err_hif_stop;
593 }
594
595 ath11k_dp_pdev_pre_alloc(ab);
596
597 ret = ath11k_dp_pdev_reo_setup(ab);
598 if (ret) {
599 ath11k_err(ab, "failed to initialize reo destination rings: %d\n", ret);
600 goto err_mac_destroy;
601 }
602
603 ret = ath11k_wmi_cmd_init(ab);
604 if (ret) {
605 ath11k_err(ab, "failed to send wmi init cmd: %d\n", ret);
606 goto err_reo_cleanup;
607 }
608
609 ret = ath11k_wmi_wait_for_unified_ready(ab);
610 if (ret) {
611 ath11k_err(ab, "failed to receive wmi unified ready event: %d\n",
612 ret);
613 goto err_reo_cleanup;
614 }
615
616 ret = ath11k_dp_tx_htt_h2t_ver_req_msg(ab);
617 if (ret) {
618 ath11k_err(ab, "failed to send htt version request message: %d\n",
619 ret);
620 goto err_reo_cleanup;
621 }
622
623 return 0;
624
625 err_reo_cleanup:
626 ath11k_dp_pdev_reo_cleanup(ab);
627 err_mac_destroy:
628 ath11k_mac_destroy(ab);
629 err_hif_stop:
630 ath11k_hif_stop(ab);
631 err_wmi_detach:
632 ath11k_wmi_detach(ab);
633 err_firmware_stop:
634 ath11k_qmi_firmware_stop(ab);
635
636 return ret;
637 }
638
ath11k_core_qmi_firmware_ready(struct ath11k_base * ab)639 int ath11k_core_qmi_firmware_ready(struct ath11k_base *ab)
640 {
641 int ret;
642
643 ret = ath11k_ce_init_pipes(ab);
644 if (ret) {
645 ath11k_err(ab, "failed to initialize CE: %d\n", ret);
646 return ret;
647 }
648
649 ret = ath11k_dp_alloc(ab);
650 if (ret) {
651 ath11k_err(ab, "failed to init DP: %d\n", ret);
652 return ret;
653 }
654
655 switch (ath11k_crypto_mode) {
656 case ATH11K_CRYPT_MODE_SW:
657 set_bit(ATH11K_FLAG_HW_CRYPTO_DISABLED, &ab->dev_flags);
658 set_bit(ATH11K_FLAG_RAW_MODE, &ab->dev_flags);
659 break;
660 case ATH11K_CRYPT_MODE_HW:
661 clear_bit(ATH11K_FLAG_HW_CRYPTO_DISABLED, &ab->dev_flags);
662 clear_bit(ATH11K_FLAG_RAW_MODE, &ab->dev_flags);
663 break;
664 default:
665 ath11k_info(ab, "invalid crypto_mode: %d\n", ath11k_crypto_mode);
666 return -EINVAL;
667 }
668
669 if (ath11k_frame_mode == ATH11K_HW_TXRX_RAW)
670 set_bit(ATH11K_FLAG_RAW_MODE, &ab->dev_flags);
671
672 mutex_lock(&ab->core_lock);
673 ret = ath11k_core_start(ab, ATH11K_FIRMWARE_MODE_NORMAL);
674 if (ret) {
675 ath11k_err(ab, "failed to start core: %d\n", ret);
676 goto err_dp_free;
677 }
678
679 ret = ath11k_core_pdev_create(ab);
680 if (ret) {
681 ath11k_err(ab, "failed to create pdev core: %d\n", ret);
682 goto err_core_stop;
683 }
684 ath11k_hif_irq_enable(ab);
685 mutex_unlock(&ab->core_lock);
686
687 return 0;
688
689 err_core_stop:
690 ath11k_core_stop(ab);
691 ath11k_mac_destroy(ab);
692 err_dp_free:
693 ath11k_dp_free(ab);
694 mutex_unlock(&ab->core_lock);
695 return ret;
696 }
697
ath11k_core_reconfigure_on_crash(struct ath11k_base * ab)698 static int ath11k_core_reconfigure_on_crash(struct ath11k_base *ab)
699 {
700 int ret;
701
702 mutex_lock(&ab->core_lock);
703 ath11k_thermal_unregister(ab);
704 ath11k_hif_irq_disable(ab);
705 ath11k_dp_pdev_free(ab);
706 ath11k_spectral_deinit(ab);
707 ath11k_hif_stop(ab);
708 ath11k_wmi_detach(ab);
709 ath11k_dp_pdev_reo_cleanup(ab);
710 mutex_unlock(&ab->core_lock);
711
712 ath11k_dp_free(ab);
713 ath11k_hal_srng_deinit(ab);
714
715 ab->free_vdev_map = (1LL << (ab->num_radios * TARGET_NUM_VDEVS)) - 1;
716
717 ret = ath11k_hal_srng_init(ab);
718 if (ret)
719 return ret;
720
721 clear_bit(ATH11K_FLAG_CRASH_FLUSH, &ab->dev_flags);
722
723 ret = ath11k_core_qmi_firmware_ready(ab);
724 if (ret)
725 goto err_hal_srng_deinit;
726
727 clear_bit(ATH11K_FLAG_RECOVERY, &ab->dev_flags);
728
729 return 0;
730
731 err_hal_srng_deinit:
732 ath11k_hal_srng_deinit(ab);
733 return ret;
734 }
735
ath11k_core_halt(struct ath11k * ar)736 void ath11k_core_halt(struct ath11k *ar)
737 {
738 struct ath11k_base *ab = ar->ab;
739
740 lockdep_assert_held(&ar->conf_mutex);
741
742 ar->num_created_vdevs = 0;
743 ar->allocated_vdev_map = 0;
744
745 ath11k_mac_scan_finish(ar);
746 ath11k_mac_peer_cleanup_all(ar);
747 cancel_delayed_work_sync(&ar->scan.timeout);
748 cancel_work_sync(&ar->regd_update_work);
749
750 rcu_assign_pointer(ab->pdevs_active[ar->pdev_idx], NULL);
751 synchronize_rcu();
752 INIT_LIST_HEAD(&ar->arvifs);
753 idr_init(&ar->txmgmt_idr);
754 }
755
ath11k_core_restart(struct work_struct * work)756 static void ath11k_core_restart(struct work_struct *work)
757 {
758 struct ath11k_base *ab = container_of(work, struct ath11k_base, restart_work);
759 struct ath11k *ar;
760 struct ath11k_pdev *pdev;
761 int i, ret = 0;
762
763 spin_lock_bh(&ab->base_lock);
764 ab->stats.fw_crash_counter++;
765 spin_unlock_bh(&ab->base_lock);
766
767 for (i = 0; i < ab->num_radios; i++) {
768 pdev = &ab->pdevs[i];
769 ar = pdev->ar;
770 if (!ar || ar->state == ATH11K_STATE_OFF)
771 continue;
772
773 ieee80211_stop_queues(ar->hw);
774 ath11k_mac_drain_tx(ar);
775 complete(&ar->scan.started);
776 complete(&ar->scan.completed);
777 complete(&ar->peer_assoc_done);
778 complete(&ar->peer_delete_done);
779 complete(&ar->install_key_done);
780 complete(&ar->vdev_setup_done);
781 complete(&ar->bss_survey_done);
782 complete(&ar->thermal.wmi_sync);
783
784 wake_up(&ar->dp.tx_empty_waitq);
785 idr_for_each(&ar->txmgmt_idr,
786 ath11k_mac_tx_mgmt_pending_free, ar);
787 idr_destroy(&ar->txmgmt_idr);
788 }
789
790 wake_up(&ab->wmi_ab.tx_credits_wq);
791 wake_up(&ab->peer_mapping_wq);
792
793 ret = ath11k_core_reconfigure_on_crash(ab);
794 if (ret) {
795 ath11k_err(ab, "failed to reconfigure driver on crash recovery\n");
796 return;
797 }
798
799 for (i = 0; i < ab->num_radios; i++) {
800 pdev = &ab->pdevs[i];
801 ar = pdev->ar;
802 if (!ar || ar->state == ATH11K_STATE_OFF)
803 continue;
804
805 mutex_lock(&ar->conf_mutex);
806
807 switch (ar->state) {
808 case ATH11K_STATE_ON:
809 ar->state = ATH11K_STATE_RESTARTING;
810 ath11k_core_halt(ar);
811 ieee80211_restart_hw(ar->hw);
812 break;
813 case ATH11K_STATE_OFF:
814 ath11k_warn(ab,
815 "cannot restart radio %d that hasn't been started\n",
816 i);
817 break;
818 case ATH11K_STATE_RESTARTING:
819 break;
820 case ATH11K_STATE_RESTARTED:
821 ar->state = ATH11K_STATE_WEDGED;
822 fallthrough;
823 case ATH11K_STATE_WEDGED:
824 ath11k_warn(ab,
825 "device is wedged, will not restart radio %d\n", i);
826 break;
827 }
828 mutex_unlock(&ar->conf_mutex);
829 }
830 complete(&ab->driver_recovery);
831 }
832
ath11k_init_hw_params(struct ath11k_base * ab)833 static int ath11k_init_hw_params(struct ath11k_base *ab)
834 {
835 const struct ath11k_hw_params *hw_params = NULL;
836 int i;
837
838 for (i = 0; i < ARRAY_SIZE(ath11k_hw_params); i++) {
839 hw_params = &ath11k_hw_params[i];
840
841 if (hw_params->hw_rev == ab->hw_rev)
842 break;
843 }
844
845 if (i == ARRAY_SIZE(ath11k_hw_params)) {
846 ath11k_err(ab, "Unsupported hardware version: 0x%x\n", ab->hw_rev);
847 return -EINVAL;
848 }
849
850 ab->hw_params = *hw_params;
851
852 ath11k_dbg(ab, ATH11K_DBG_BOOT, "Hardware name %s\n", ab->hw_params.name);
853
854 return 0;
855 }
856
ath11k_core_pre_init(struct ath11k_base * ab)857 int ath11k_core_pre_init(struct ath11k_base *ab)
858 {
859 int ret;
860
861 ret = ath11k_init_hw_params(ab);
862 if (ret) {
863 ath11k_err(ab, "failed to get hw params: %d\n", ret);
864 return ret;
865 }
866
867 return 0;
868 }
869 EXPORT_SYMBOL(ath11k_core_pre_init);
870
ath11k_core_init(struct ath11k_base * ab)871 int ath11k_core_init(struct ath11k_base *ab)
872 {
873 int ret;
874
875 ret = ath11k_core_soc_create(ab);
876 if (ret) {
877 ath11k_err(ab, "failed to create soc core: %d\n", ret);
878 return ret;
879 }
880
881 return 0;
882 }
883 EXPORT_SYMBOL(ath11k_core_init);
884
ath11k_core_deinit(struct ath11k_base * ab)885 void ath11k_core_deinit(struct ath11k_base *ab)
886 {
887 mutex_lock(&ab->core_lock);
888
889 ath11k_core_pdev_destroy(ab);
890 ath11k_core_stop(ab);
891
892 mutex_unlock(&ab->core_lock);
893
894 ath11k_hif_power_down(ab);
895 ath11k_mac_destroy(ab);
896 ath11k_core_soc_destroy(ab);
897 }
898 EXPORT_SYMBOL(ath11k_core_deinit);
899
ath11k_core_free(struct ath11k_base * ab)900 void ath11k_core_free(struct ath11k_base *ab)
901 {
902 kfree(ab);
903 }
904 EXPORT_SYMBOL(ath11k_core_free);
905
ath11k_core_alloc(struct device * dev,size_t priv_size,enum ath11k_bus bus,const struct ath11k_bus_params * bus_params)906 struct ath11k_base *ath11k_core_alloc(struct device *dev, size_t priv_size,
907 enum ath11k_bus bus,
908 const struct ath11k_bus_params *bus_params)
909 {
910 struct ath11k_base *ab;
911
912 ab = kzalloc(sizeof(*ab) + priv_size, GFP_KERNEL);
913 if (!ab)
914 return NULL;
915
916 init_completion(&ab->driver_recovery);
917
918 ab->workqueue = create_singlethread_workqueue("ath11k_wq");
919 if (!ab->workqueue)
920 goto err_sc_free;
921
922 mutex_init(&ab->core_lock);
923 spin_lock_init(&ab->base_lock);
924
925 INIT_LIST_HEAD(&ab->peers);
926 init_waitqueue_head(&ab->peer_mapping_wq);
927 init_waitqueue_head(&ab->wmi_ab.tx_credits_wq);
928 INIT_WORK(&ab->restart_work, ath11k_core_restart);
929 timer_setup(&ab->rx_replenish_retry, ath11k_ce_rx_replenish_retry, 0);
930 ab->dev = dev;
931 ab->bus_params = *bus_params;
932 ab->hif.bus = bus;
933
934 return ab;
935
936 err_sc_free:
937 kfree(ab);
938 return NULL;
939 }
940 EXPORT_SYMBOL(ath11k_core_alloc);
941
942 MODULE_DESCRIPTION("Core module for Qualcomm Atheros 802.11ax wireless LAN cards.");
943 MODULE_LICENSE("Dual BSD/GPL");
944