treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / net / wireless / ath / ath11k / core.c
blob9e823056e673547fca7298bea96bf0586205f649
1 // SPDX-License-Identifier: BSD-3-Clause-Clear
2 /*
3 * Copyright (c) 2018-2019 The Linux Foundation. All rights reserved.
4 */
6 #include <linux/module.h>
7 #include <linux/slab.h>
8 #include <linux/remoteproc.h>
9 #include <linux/firmware.h>
10 #include "ahb.h"
11 #include "core.h"
12 #include "dp_tx.h"
13 #include "dp_rx.h"
14 #include "debug.h"
16 unsigned int ath11k_debug_mask;
17 module_param_named(debug_mask, ath11k_debug_mask, uint, 0644);
18 MODULE_PARM_DESC(debug_mask, "Debugging mask");
20 static const struct ath11k_hw_params ath11k_hw_params = {
21 .name = "ipq8074",
22 .fw = {
23 .dir = IPQ8074_FW_DIR,
24 .board_size = IPQ8074_MAX_BOARD_DATA_SZ,
25 .cal_size = IPQ8074_MAX_CAL_DATA_SZ,
29 /* Map from pdev index to hw mac index */
30 u8 ath11k_core_get_hw_mac_id(struct ath11k_base *ab, int pdev_idx)
32 switch (pdev_idx) {
33 case 0:
34 return 0;
35 case 1:
36 return 2;
37 case 2:
38 return 1;
39 default:
40 ath11k_warn(ab, "Invalid pdev idx %d\n", pdev_idx);
41 return ATH11K_INVALID_HW_MAC_ID;
45 static int ath11k_core_create_board_name(struct ath11k_base *ab, char *name,
46 size_t name_len)
48 /* Note: bus is fixed to ahb. When other bus type supported,
49 * make it to dynamic.
51 scnprintf(name, name_len,
52 "bus=ahb,qmi-chip-id=%d,qmi-board-id=%d",
53 ab->qmi.target.chip_id,
54 ab->qmi.target.board_id);
56 ath11k_dbg(ab, ATH11K_DBG_BOOT, "boot using board name '%s'\n", name);
58 return 0;
61 static const struct firmware *ath11k_fetch_fw_file(struct ath11k_base *ab,
62 const char *dir,
63 const char *file)
65 char filename[100];
66 const struct firmware *fw;
67 int ret;
69 if (file == NULL)
70 return ERR_PTR(-ENOENT);
72 if (dir == NULL)
73 dir = ".";
75 snprintf(filename, sizeof(filename), "%s/%s", dir, file);
76 ret = firmware_request_nowarn(&fw, filename, ab->dev);
77 ath11k_dbg(ab, ATH11K_DBG_BOOT, "boot fw request '%s': %d\n",
78 filename, ret);
80 if (ret)
81 return ERR_PTR(ret);
82 ath11k_warn(ab, "Downloading BDF: %s, size: %zu\n",
83 filename, fw->size);
85 return fw;
88 void ath11k_core_free_bdf(struct ath11k_base *ab, struct ath11k_board_data *bd)
90 if (!IS_ERR(bd->fw))
91 release_firmware(bd->fw);
93 memset(bd, 0, sizeof(*bd));
96 static int ath11k_core_parse_bd_ie_board(struct ath11k_base *ab,
97 struct ath11k_board_data *bd,
98 const void *buf, size_t buf_len,
99 const char *boardname,
100 int bd_ie_type)
102 const struct ath11k_fw_ie *hdr;
103 bool name_match_found;
104 int ret, board_ie_id;
105 size_t board_ie_len;
106 const void *board_ie_data;
108 name_match_found = false;
110 /* go through ATH11K_BD_IE_BOARD_ elements */
111 while (buf_len > sizeof(struct ath11k_fw_ie)) {
112 hdr = buf;
113 board_ie_id = le32_to_cpu(hdr->id);
114 board_ie_len = le32_to_cpu(hdr->len);
115 board_ie_data = hdr->data;
117 buf_len -= sizeof(*hdr);
118 buf += sizeof(*hdr);
120 if (buf_len < ALIGN(board_ie_len, 4)) {
121 ath11k_err(ab, "invalid ATH11K_BD_IE_BOARD length: %zu < %zu\n",
122 buf_len, ALIGN(board_ie_len, 4));
123 ret = -EINVAL;
124 goto out;
127 switch (board_ie_id) {
128 case ATH11K_BD_IE_BOARD_NAME:
129 ath11k_dbg_dump(ab, ATH11K_DBG_BOOT, "board name", "",
130 board_ie_data, board_ie_len);
132 if (board_ie_len != strlen(boardname))
133 break;
135 ret = memcmp(board_ie_data, boardname, strlen(boardname));
136 if (ret)
137 break;
139 name_match_found = true;
140 ath11k_dbg(ab, ATH11K_DBG_BOOT,
141 "boot found match for name '%s'",
142 boardname);
143 break;
144 case ATH11K_BD_IE_BOARD_DATA:
145 if (!name_match_found)
146 /* no match found */
147 break;
149 ath11k_dbg(ab, ATH11K_DBG_BOOT,
150 "boot found board data for '%s'", boardname);
152 bd->data = board_ie_data;
153 bd->len = board_ie_len;
155 ret = 0;
156 goto out;
157 default:
158 ath11k_warn(ab, "unknown ATH11K_BD_IE_BOARD found: %d\n",
159 board_ie_id);
160 break;
163 /* jump over the padding */
164 board_ie_len = ALIGN(board_ie_len, 4);
166 buf_len -= board_ie_len;
167 buf += board_ie_len;
170 /* no match found */
171 ret = -ENOENT;
173 out:
174 return ret;
177 static int ath11k_core_fetch_board_data_api_n(struct ath11k_base *ab,
178 struct ath11k_board_data *bd,
179 const char *boardname)
181 size_t len, magic_len;
182 const u8 *data;
183 char *filename = ATH11K_BOARD_API2_FILE;
184 size_t ie_len;
185 struct ath11k_fw_ie *hdr;
186 int ret, ie_id;
188 if (!bd->fw)
189 bd->fw = ath11k_fetch_fw_file(ab,
190 ab->hw_params.fw.dir,
191 filename);
192 if (IS_ERR(bd->fw))
193 return PTR_ERR(bd->fw);
195 data = bd->fw->data;
196 len = bd->fw->size;
198 /* magic has extra null byte padded */
199 magic_len = strlen(ATH11K_BOARD_MAGIC) + 1;
200 if (len < magic_len) {
201 ath11k_err(ab, "failed to find magic value in %s/%s, file too short: %zu\n",
202 ab->hw_params.fw.dir, filename, len);
203 ret = -EINVAL;
204 goto err;
207 if (memcmp(data, ATH11K_BOARD_MAGIC, magic_len)) {
208 ath11k_err(ab, "found invalid board magic\n");
209 ret = -EINVAL;
210 goto err;
213 /* magic is padded to 4 bytes */
214 magic_len = ALIGN(magic_len, 4);
215 if (len < magic_len) {
216 ath11k_err(ab, "failed: %s/%s too small to contain board data, len: %zu\n",
217 ab->hw_params.fw.dir, filename, len);
218 ret = -EINVAL;
219 goto err;
222 data += magic_len;
223 len -= magic_len;
225 while (len > sizeof(struct ath11k_fw_ie)) {
226 hdr = (struct ath11k_fw_ie *)data;
227 ie_id = le32_to_cpu(hdr->id);
228 ie_len = le32_to_cpu(hdr->len);
230 len -= sizeof(*hdr);
231 data = hdr->data;
233 if (len < ALIGN(ie_len, 4)) {
234 ath11k_err(ab, "invalid length for board ie_id %d ie_len %zu len %zu\n",
235 ie_id, ie_len, len);
236 return -EINVAL;
239 switch (ie_id) {
240 case ATH11K_BD_IE_BOARD:
241 ret = ath11k_core_parse_bd_ie_board(ab, bd, data,
242 ie_len,
243 boardname,
244 ATH11K_BD_IE_BOARD);
245 if (ret == -ENOENT)
246 /* no match found, continue */
247 break;
248 else if (ret)
249 /* there was an error, bail out */
250 goto err;
251 /* either found or error, so stop searching */
252 goto out;
255 /* jump over the padding */
256 ie_len = ALIGN(ie_len, 4);
258 len -= ie_len;
259 data += ie_len;
262 out:
263 if (!bd->data || !bd->len) {
264 ath11k_err(ab,
265 "failed to fetch board data for %s from %s/%s\n",
266 boardname, ab->hw_params.fw.dir, filename);
267 ret = -ENODATA;
268 goto err;
271 return 0;
273 err:
274 ath11k_core_free_bdf(ab, bd);
275 return ret;
278 static int ath11k_core_fetch_board_data_api_1(struct ath11k_base *ab,
279 struct ath11k_board_data *bd)
281 bd->fw = ath11k_fetch_fw_file(ab,
282 ab->hw_params.fw.dir,
283 ATH11K_DEFAULT_BOARD_FILE);
284 if (IS_ERR(bd->fw))
285 return PTR_ERR(bd->fw);
287 bd->data = bd->fw->data;
288 bd->len = bd->fw->size;
290 return 0;
293 #define BOARD_NAME_SIZE 100
294 int ath11k_core_fetch_bdf(struct ath11k_base *ab, struct ath11k_board_data *bd)
296 char boardname[BOARD_NAME_SIZE];
297 int ret;
299 ret = ath11k_core_create_board_name(ab, boardname, BOARD_NAME_SIZE);
300 if (ret) {
301 ath11k_err(ab, "failed to create board name: %d", ret);
302 return ret;
305 ab->bd_api = 2;
306 ret = ath11k_core_fetch_board_data_api_n(ab, bd, boardname);
307 if (!ret)
308 goto success;
310 ab->bd_api = 1;
311 ret = ath11k_core_fetch_board_data_api_1(ab, bd);
312 if (ret) {
313 ath11k_err(ab, "failed to fetch board-2.bin or board.bin from %s\n",
314 ab->hw_params.fw.dir);
315 return ret;
318 success:
319 ath11k_dbg(ab, ATH11K_DBG_BOOT, "using board api %d\n", ab->bd_api);
320 return 0;
323 static void ath11k_core_stop(struct ath11k_base *ab)
325 if (!test_bit(ATH11K_FLAG_CRASH_FLUSH, &ab->dev_flags))
326 ath11k_qmi_firmware_stop(ab);
327 ath11k_ahb_stop(ab);
328 ath11k_wmi_detach(ab);
329 ath11k_dp_pdev_reo_cleanup(ab);
331 /* De-Init of components as needed */
334 static int ath11k_core_soc_create(struct ath11k_base *ab)
336 int ret;
338 ret = ath11k_qmi_init_service(ab);
339 if (ret) {
340 ath11k_err(ab, "failed to initialize qmi :%d\n", ret);
341 return ret;
344 ret = ath11k_debug_soc_create(ab);
345 if (ret) {
346 ath11k_err(ab, "failed to create ath11k debugfs\n");
347 goto err_qmi_deinit;
350 ret = ath11k_ahb_power_up(ab);
351 if (ret) {
352 ath11k_err(ab, "failed to power up :%d\n", ret);
353 goto err_debugfs_reg;
356 return 0;
358 err_debugfs_reg:
359 ath11k_debug_soc_destroy(ab);
360 err_qmi_deinit:
361 ath11k_qmi_deinit_service(ab);
362 return ret;
365 static void ath11k_core_soc_destroy(struct ath11k_base *ab)
367 ath11k_debug_soc_destroy(ab);
368 ath11k_dp_free(ab);
369 ath11k_reg_free(ab);
370 ath11k_qmi_deinit_service(ab);
373 static int ath11k_core_pdev_create(struct ath11k_base *ab)
375 int ret;
377 ret = ath11k_debug_pdev_create(ab);
378 if (ret) {
379 ath11k_err(ab, "failed to create core pdev debugfs: %d\n", ret);
380 return ret;
383 ret = ath11k_mac_register(ab);
384 if (ret) {
385 ath11k_err(ab, "failed register the radio with mac80211: %d\n", ret);
386 goto err_pdev_debug;
389 ret = ath11k_dp_pdev_alloc(ab);
390 if (ret) {
391 ath11k_err(ab, "failed to attach DP pdev: %d\n", ret);
392 goto err_mac_unregister;
395 return 0;
397 err_mac_unregister:
398 ath11k_mac_unregister(ab);
400 err_pdev_debug:
401 ath11k_debug_pdev_destroy(ab);
403 return ret;
406 static void ath11k_core_pdev_destroy(struct ath11k_base *ab)
408 ath11k_mac_unregister(ab);
409 ath11k_ahb_ext_irq_disable(ab);
410 ath11k_dp_pdev_free(ab);
411 ath11k_debug_pdev_destroy(ab);
414 static int ath11k_core_start(struct ath11k_base *ab,
415 enum ath11k_firmware_mode mode)
417 int ret;
419 ret = ath11k_qmi_firmware_start(ab, mode);
420 if (ret) {
421 ath11k_err(ab, "failed to attach wmi: %d\n", ret);
422 return ret;
425 ret = ath11k_wmi_attach(ab);
426 if (ret) {
427 ath11k_err(ab, "failed to attach wmi: %d\n", ret);
428 goto err_firmware_stop;
431 ret = ath11k_htc_init(ab);
432 if (ret) {
433 ath11k_err(ab, "failed to init htc: %d\n", ret);
434 goto err_wmi_detach;
437 ret = ath11k_ahb_start(ab);
438 if (ret) {
439 ath11k_err(ab, "failed to start HIF: %d\n", ret);
440 goto err_wmi_detach;
443 ret = ath11k_htc_wait_target(&ab->htc);
444 if (ret) {
445 ath11k_err(ab, "failed to connect to HTC: %d\n", ret);
446 goto err_hif_stop;
449 ret = ath11k_dp_htt_connect(&ab->dp);
450 if (ret) {
451 ath11k_err(ab, "failed to connect to HTT: %d\n", ret);
452 goto err_hif_stop;
455 ret = ath11k_wmi_connect(ab);
456 if (ret) {
457 ath11k_err(ab, "failed to connect wmi: %d\n", ret);
458 goto err_hif_stop;
461 ret = ath11k_htc_start(&ab->htc);
462 if (ret) {
463 ath11k_err(ab, "failed to start HTC: %d\n", ret);
464 goto err_hif_stop;
467 ret = ath11k_wmi_wait_for_service_ready(ab);
468 if (ret) {
469 ath11k_err(ab, "failed to receive wmi service ready event: %d\n",
470 ret);
471 goto err_hif_stop;
474 ret = ath11k_mac_allocate(ab);
475 if (ret) {
476 ath11k_err(ab, "failed to create new hw device with mac80211 :%d\n",
477 ret);
478 goto err_hif_stop;
481 ath11k_dp_pdev_pre_alloc(ab);
483 ret = ath11k_dp_pdev_reo_setup(ab);
484 if (ret) {
485 ath11k_err(ab, "failed to initialize reo destination rings: %d\n", ret);
486 goto err_mac_destroy;
489 ret = ath11k_wmi_cmd_init(ab);
490 if (ret) {
491 ath11k_err(ab, "failed to send wmi init cmd: %d\n", ret);
492 goto err_reo_cleanup;
495 ret = ath11k_wmi_wait_for_unified_ready(ab);
496 if (ret) {
497 ath11k_err(ab, "failed to receive wmi unified ready event: %d\n",
498 ret);
499 goto err_reo_cleanup;
502 ret = ath11k_dp_tx_htt_h2t_ver_req_msg(ab);
503 if (ret) {
504 ath11k_err(ab, "failed to send htt version request message: %d\n",
505 ret);
506 goto err_reo_cleanup;
509 return 0;
511 err_reo_cleanup:
512 ath11k_dp_pdev_reo_cleanup(ab);
513 err_mac_destroy:
514 ath11k_mac_destroy(ab);
515 err_hif_stop:
516 ath11k_ahb_stop(ab);
517 err_wmi_detach:
518 ath11k_wmi_detach(ab);
519 err_firmware_stop:
520 ath11k_qmi_firmware_stop(ab);
522 return ret;
525 int ath11k_core_qmi_firmware_ready(struct ath11k_base *ab)
527 int ret;
529 ret = ath11k_ce_init_pipes(ab);
530 if (ret) {
531 ath11k_err(ab, "failed to initialize CE: %d\n", ret);
532 return ret;
535 ret = ath11k_dp_alloc(ab);
536 if (ret) {
537 ath11k_err(ab, "failed to init DP: %d\n", ret);
538 return ret;
541 mutex_lock(&ab->core_lock);
542 ret = ath11k_core_start(ab, ATH11K_FIRMWARE_MODE_NORMAL);
543 if (ret) {
544 ath11k_err(ab, "failed to start core: %d\n", ret);
545 goto err_dp_free;
548 ret = ath11k_core_pdev_create(ab);
549 if (ret) {
550 ath11k_err(ab, "failed to create pdev core: %d\n", ret);
551 goto err_core_stop;
553 ath11k_ahb_ext_irq_enable(ab);
554 mutex_unlock(&ab->core_lock);
556 return 0;
558 err_core_stop:
559 ath11k_core_stop(ab);
560 ath11k_mac_destroy(ab);
561 err_dp_free:
562 ath11k_dp_free(ab);
563 mutex_unlock(&ab->core_lock);
564 return ret;
567 static int ath11k_core_reconfigure_on_crash(struct ath11k_base *ab)
569 int ret;
571 mutex_lock(&ab->core_lock);
572 ath11k_ahb_ext_irq_disable(ab);
573 ath11k_dp_pdev_free(ab);
574 ath11k_ahb_stop(ab);
575 ath11k_wmi_detach(ab);
576 ath11k_dp_pdev_reo_cleanup(ab);
577 mutex_unlock(&ab->core_lock);
579 ath11k_dp_free(ab);
580 ath11k_hal_srng_deinit(ab);
582 ab->free_vdev_map = (1LL << (ab->num_radios * TARGET_NUM_VDEVS)) - 1;
584 ret = ath11k_hal_srng_init(ab);
585 if (ret)
586 return ret;
588 clear_bit(ATH11K_FLAG_CRASH_FLUSH, &ab->dev_flags);
590 ret = ath11k_core_qmi_firmware_ready(ab);
591 if (ret)
592 goto err_hal_srng_deinit;
594 clear_bit(ATH11K_FLAG_RECOVERY, &ab->dev_flags);
596 return 0;
598 err_hal_srng_deinit:
599 ath11k_hal_srng_deinit(ab);
600 return ret;
603 void ath11k_core_halt(struct ath11k *ar)
605 struct ath11k_base *ab = ar->ab;
607 lockdep_assert_held(&ar->conf_mutex);
609 ar->num_created_vdevs = 0;
611 ath11k_mac_scan_finish(ar);
612 ath11k_mac_peer_cleanup_all(ar);
613 cancel_delayed_work_sync(&ar->scan.timeout);
614 cancel_work_sync(&ar->regd_update_work);
616 rcu_assign_pointer(ab->pdevs_active[ar->pdev_idx], NULL);
617 synchronize_rcu();
618 INIT_LIST_HEAD(&ar->arvifs);
619 idr_init(&ar->txmgmt_idr);
622 static void ath11k_core_restart(struct work_struct *work)
624 struct ath11k_base *ab = container_of(work, struct ath11k_base, restart_work);
625 struct ath11k *ar;
626 struct ath11k_pdev *pdev;
627 int i, ret = 0;
629 spin_lock_bh(&ab->base_lock);
630 ab->stats.fw_crash_counter++;
631 spin_unlock_bh(&ab->base_lock);
633 for (i = 0; i < ab->num_radios; i++) {
634 pdev = &ab->pdevs[i];
635 ar = pdev->ar;
636 if (!ar || ar->state == ATH11K_STATE_OFF)
637 continue;
639 ieee80211_stop_queues(ar->hw);
640 ath11k_mac_drain_tx(ar);
641 complete(&ar->scan.started);
642 complete(&ar->scan.completed);
643 complete(&ar->peer_assoc_done);
644 complete(&ar->install_key_done);
645 complete(&ar->vdev_setup_done);
646 complete(&ar->bss_survey_done);
648 wake_up(&ar->dp.tx_empty_waitq);
649 idr_for_each(&ar->txmgmt_idr,
650 ath11k_mac_tx_mgmt_pending_free, ar);
651 idr_destroy(&ar->txmgmt_idr);
654 wake_up(&ab->wmi_ab.tx_credits_wq);
655 wake_up(&ab->peer_mapping_wq);
657 ret = ath11k_core_reconfigure_on_crash(ab);
658 if (ret) {
659 ath11k_err(ab, "failed to reconfigure driver on crash recovery\n");
660 return;
663 for (i = 0; i < ab->num_radios; i++) {
664 pdev = &ab->pdevs[i];
665 ar = pdev->ar;
666 if (!ar || ar->state == ATH11K_STATE_OFF)
667 continue;
669 mutex_lock(&ar->conf_mutex);
671 switch (ar->state) {
672 case ATH11K_STATE_ON:
673 ar->state = ATH11K_STATE_RESTARTING;
674 ath11k_core_halt(ar);
675 ieee80211_restart_hw(ar->hw);
676 break;
677 case ATH11K_STATE_OFF:
678 ath11k_warn(ab,
679 "cannot restart radio %d that hasn't been started\n",
681 break;
682 case ATH11K_STATE_RESTARTING:
683 break;
684 case ATH11K_STATE_RESTARTED:
685 ar->state = ATH11K_STATE_WEDGED;
686 /* fall through */
687 case ATH11K_STATE_WEDGED:
688 ath11k_warn(ab,
689 "device is wedged, will not restart radio %d\n", i);
690 break;
692 mutex_unlock(&ar->conf_mutex);
694 complete(&ab->driver_recovery);
697 int ath11k_core_init(struct ath11k_base *ab)
699 struct device *dev = ab->dev;
700 struct rproc *prproc;
701 phandle rproc_phandle;
702 int ret;
704 if (of_property_read_u32(dev->of_node, "qcom,rproc", &rproc_phandle)) {
705 ath11k_err(ab, "failed to get q6_rproc handle\n");
706 return -ENOENT;
709 prproc = rproc_get_by_phandle(rproc_phandle);
710 if (!prproc) {
711 ath11k_err(ab, "failed to get rproc\n");
712 return -EINVAL;
714 ab->tgt_rproc = prproc;
715 ab->hw_params = ath11k_hw_params;
717 ret = ath11k_core_soc_create(ab);
718 if (ret) {
719 ath11k_err(ab, "failed to create soc core: %d\n", ret);
720 return ret;
723 return 0;
726 void ath11k_core_deinit(struct ath11k_base *ab)
728 mutex_lock(&ab->core_lock);
730 ath11k_core_pdev_destroy(ab);
731 ath11k_core_stop(ab);
733 mutex_unlock(&ab->core_lock);
735 ath11k_ahb_power_down(ab);
736 ath11k_mac_destroy(ab);
737 ath11k_core_soc_destroy(ab);
740 void ath11k_core_free(struct ath11k_base *ab)
742 kfree(ab);
745 struct ath11k_base *ath11k_core_alloc(struct device *dev)
747 struct ath11k_base *ab;
749 ab = kzalloc(sizeof(*ab), GFP_KERNEL);
750 if (!ab)
751 return NULL;
753 init_completion(&ab->driver_recovery);
755 ab->workqueue = create_singlethread_workqueue("ath11k_wq");
756 if (!ab->workqueue)
757 goto err_sc_free;
759 mutex_init(&ab->core_lock);
760 spin_lock_init(&ab->base_lock);
762 INIT_LIST_HEAD(&ab->peers);
763 init_waitqueue_head(&ab->peer_mapping_wq);
764 init_waitqueue_head(&ab->wmi_ab.tx_credits_wq);
765 INIT_WORK(&ab->restart_work, ath11k_core_restart);
766 timer_setup(&ab->rx_replenish_retry, ath11k_ce_rx_replenish_retry, 0);
767 ab->dev = dev;
769 return ab;
771 err_sc_free:
772 kfree(ab);
773 return NULL;
776 static int __init ath11k_init(void)
778 int ret;
780 ret = ath11k_ahb_init();
781 if (ret)
782 printk(KERN_ERR "failed to register ath11k ahb driver: %d\n",
783 ret);
784 return ret;
786 module_init(ath11k_init);
788 static void __exit ath11k_exit(void)
790 ath11k_ahb_exit();
792 module_exit(ath11k_exit);
794 MODULE_DESCRIPTION("Driver support for Qualcomm Technologies 802.11ax wireless chip");
795 MODULE_LICENSE("Dual BSD/GPL");