1 // SPDX-License-Identifier: BSD-3-Clause-Clear
3 * Copyright (c) 2018-2019 The Linux Foundation. All rights reserved.
6 #include <linux/module.h>
7 #include <linux/slab.h>
8 #include <linux/remoteproc.h>
9 #include <linux/firmware.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
= {
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
)
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
,
48 /* Note: bus is fixed to ahb. When other bus type supported,
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
);
61 static const struct firmware
*ath11k_fetch_fw_file(struct ath11k_base
*ab
,
66 const struct firmware
*fw
;
70 return ERR_PTR(-ENOENT
);
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",
82 ath11k_warn(ab
, "Downloading BDF: %s, size: %zu\n",
88 void ath11k_core_free_bdf(struct ath11k_base
*ab
, struct ath11k_board_data
*bd
)
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
,
102 const struct ath11k_fw_ie
*hdr
;
103 bool name_match_found
;
104 int ret
, board_ie_id
;
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
)) {
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
);
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));
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
))
135 ret
= memcmp(board_ie_data
, boardname
, strlen(boardname
));
139 name_match_found
= true;
140 ath11k_dbg(ab
, ATH11K_DBG_BOOT
,
141 "boot found match for name '%s'",
144 case ATH11K_BD_IE_BOARD_DATA
:
145 if (!name_match_found
)
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
;
158 ath11k_warn(ab
, "unknown ATH11K_BD_IE_BOARD found: %d\n",
163 /* jump over the padding */
164 board_ie_len
= ALIGN(board_ie_len
, 4);
166 buf_len
-= board_ie_len
;
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
;
183 char *filename
= ATH11K_BOARD_API2_FILE
;
185 struct ath11k_fw_ie
*hdr
;
189 bd
->fw
= ath11k_fetch_fw_file(ab
,
190 ab
->hw_params
.fw
.dir
,
193 return PTR_ERR(bd
->fw
);
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
);
207 if (memcmp(data
, ATH11K_BOARD_MAGIC
, magic_len
)) {
208 ath11k_err(ab
, "found invalid board magic\n");
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
);
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
);
233 if (len
< ALIGN(ie_len
, 4)) {
234 ath11k_err(ab
, "invalid length for board ie_id %d ie_len %zu len %zu\n",
240 case ATH11K_BD_IE_BOARD
:
241 ret
= ath11k_core_parse_bd_ie_board(ab
, bd
, data
,
246 /* no match found, continue */
249 /* there was an error, bail out */
251 /* either found or error, so stop searching */
255 /* jump over the padding */
256 ie_len
= ALIGN(ie_len
, 4);
263 if (!bd
->data
|| !bd
->len
) {
265 "failed to fetch board data for %s from %s/%s\n",
266 boardname
, ab
->hw_params
.fw
.dir
, filename
);
274 ath11k_core_free_bdf(ab
, bd
);
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
);
285 return PTR_ERR(bd
->fw
);
287 bd
->data
= bd
->fw
->data
;
288 bd
->len
= bd
->fw
->size
;
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
];
299 ret
= ath11k_core_create_board_name(ab
, boardname
, BOARD_NAME_SIZE
);
301 ath11k_err(ab
, "failed to create board name: %d", ret
);
306 ret
= ath11k_core_fetch_board_data_api_n(ab
, bd
, boardname
);
311 ret
= ath11k_core_fetch_board_data_api_1(ab
, bd
);
313 ath11k_err(ab
, "failed to fetch board-2.bin or board.bin from %s\n",
314 ab
->hw_params
.fw
.dir
);
319 ath11k_dbg(ab
, ATH11K_DBG_BOOT
, "using board api %d\n", ab
->bd_api
);
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
);
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
)
338 ret
= ath11k_qmi_init_service(ab
);
340 ath11k_err(ab
, "failed to initialize qmi :%d\n", ret
);
344 ret
= ath11k_debug_soc_create(ab
);
346 ath11k_err(ab
, "failed to create ath11k debugfs\n");
350 ret
= ath11k_ahb_power_up(ab
);
352 ath11k_err(ab
, "failed to power up :%d\n", ret
);
353 goto err_debugfs_reg
;
359 ath11k_debug_soc_destroy(ab
);
361 ath11k_qmi_deinit_service(ab
);
365 static void ath11k_core_soc_destroy(struct ath11k_base
*ab
)
367 ath11k_debug_soc_destroy(ab
);
370 ath11k_qmi_deinit_service(ab
);
373 static int ath11k_core_pdev_create(struct ath11k_base
*ab
)
377 ret
= ath11k_debug_pdev_create(ab
);
379 ath11k_err(ab
, "failed to create core pdev debugfs: %d\n", ret
);
383 ret
= ath11k_mac_register(ab
);
385 ath11k_err(ab
, "failed register the radio with mac80211: %d\n", ret
);
389 ret
= ath11k_dp_pdev_alloc(ab
);
391 ath11k_err(ab
, "failed to attach DP pdev: %d\n", ret
);
392 goto err_mac_unregister
;
398 ath11k_mac_unregister(ab
);
401 ath11k_debug_pdev_destroy(ab
);
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
)
419 ret
= ath11k_qmi_firmware_start(ab
, mode
);
421 ath11k_err(ab
, "failed to attach wmi: %d\n", ret
);
425 ret
= ath11k_wmi_attach(ab
);
427 ath11k_err(ab
, "failed to attach wmi: %d\n", ret
);
428 goto err_firmware_stop
;
431 ret
= ath11k_htc_init(ab
);
433 ath11k_err(ab
, "failed to init htc: %d\n", ret
);
437 ret
= ath11k_ahb_start(ab
);
439 ath11k_err(ab
, "failed to start HIF: %d\n", ret
);
443 ret
= ath11k_htc_wait_target(&ab
->htc
);
445 ath11k_err(ab
, "failed to connect to HTC: %d\n", ret
);
449 ret
= ath11k_dp_htt_connect(&ab
->dp
);
451 ath11k_err(ab
, "failed to connect to HTT: %d\n", ret
);
455 ret
= ath11k_wmi_connect(ab
);
457 ath11k_err(ab
, "failed to connect wmi: %d\n", ret
);
461 ret
= ath11k_htc_start(&ab
->htc
);
463 ath11k_err(ab
, "failed to start HTC: %d\n", ret
);
467 ret
= ath11k_wmi_wait_for_service_ready(ab
);
469 ath11k_err(ab
, "failed to receive wmi service ready event: %d\n",
474 ret
= ath11k_mac_allocate(ab
);
476 ath11k_err(ab
, "failed to create new hw device with mac80211 :%d\n",
481 ath11k_dp_pdev_pre_alloc(ab
);
483 ret
= ath11k_dp_pdev_reo_setup(ab
);
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
);
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
);
497 ath11k_err(ab
, "failed to receive wmi unified ready event: %d\n",
499 goto err_reo_cleanup
;
502 ret
= ath11k_dp_tx_htt_h2t_ver_req_msg(ab
);
504 ath11k_err(ab
, "failed to send htt version request message: %d\n",
506 goto err_reo_cleanup
;
512 ath11k_dp_pdev_reo_cleanup(ab
);
514 ath11k_mac_destroy(ab
);
518 ath11k_wmi_detach(ab
);
520 ath11k_qmi_firmware_stop(ab
);
525 int ath11k_core_qmi_firmware_ready(struct ath11k_base
*ab
)
529 ret
= ath11k_ce_init_pipes(ab
);
531 ath11k_err(ab
, "failed to initialize CE: %d\n", ret
);
535 ret
= ath11k_dp_alloc(ab
);
537 ath11k_err(ab
, "failed to init DP: %d\n", ret
);
541 mutex_lock(&ab
->core_lock
);
542 ret
= ath11k_core_start(ab
, ATH11K_FIRMWARE_MODE_NORMAL
);
544 ath11k_err(ab
, "failed to start core: %d\n", ret
);
548 ret
= ath11k_core_pdev_create(ab
);
550 ath11k_err(ab
, "failed to create pdev core: %d\n", ret
);
553 ath11k_ahb_ext_irq_enable(ab
);
554 mutex_unlock(&ab
->core_lock
);
559 ath11k_core_stop(ab
);
560 ath11k_mac_destroy(ab
);
563 mutex_unlock(&ab
->core_lock
);
567 static int ath11k_core_reconfigure_on_crash(struct ath11k_base
*ab
)
571 mutex_lock(&ab
->core_lock
);
572 ath11k_ahb_ext_irq_disable(ab
);
573 ath11k_dp_pdev_free(ab
);
575 ath11k_wmi_detach(ab
);
576 ath11k_dp_pdev_reo_cleanup(ab
);
577 mutex_unlock(&ab
->core_lock
);
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
);
588 clear_bit(ATH11K_FLAG_CRASH_FLUSH
, &ab
->dev_flags
);
590 ret
= ath11k_core_qmi_firmware_ready(ab
);
592 goto err_hal_srng_deinit
;
594 clear_bit(ATH11K_FLAG_RECOVERY
, &ab
->dev_flags
);
599 ath11k_hal_srng_deinit(ab
);
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
);
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
);
626 struct ath11k_pdev
*pdev
;
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
];
636 if (!ar
|| ar
->state
== ATH11K_STATE_OFF
)
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
);
659 ath11k_err(ab
, "failed to reconfigure driver on crash recovery\n");
663 for (i
= 0; i
< ab
->num_radios
; i
++) {
664 pdev
= &ab
->pdevs
[i
];
666 if (!ar
|| ar
->state
== ATH11K_STATE_OFF
)
669 mutex_lock(&ar
->conf_mutex
);
672 case ATH11K_STATE_ON
:
673 ar
->state
= ATH11K_STATE_RESTARTING
;
674 ath11k_core_halt(ar
);
675 ieee80211_restart_hw(ar
->hw
);
677 case ATH11K_STATE_OFF
:
679 "cannot restart radio %d that hasn't been started\n",
682 case ATH11K_STATE_RESTARTING
:
684 case ATH11K_STATE_RESTARTED
:
685 ar
->state
= ATH11K_STATE_WEDGED
;
687 case ATH11K_STATE_WEDGED
:
689 "device is wedged, will not restart radio %d\n", i
);
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
;
704 if (of_property_read_u32(dev
->of_node
, "qcom,rproc", &rproc_phandle
)) {
705 ath11k_err(ab
, "failed to get q6_rproc handle\n");
709 prproc
= rproc_get_by_phandle(rproc_phandle
);
711 ath11k_err(ab
, "failed to get rproc\n");
714 ab
->tgt_rproc
= prproc
;
715 ab
->hw_params
= ath11k_hw_params
;
717 ret
= ath11k_core_soc_create(ab
);
719 ath11k_err(ab
, "failed to create soc core: %d\n", ret
);
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
)
745 struct ath11k_base
*ath11k_core_alloc(struct device
*dev
)
747 struct ath11k_base
*ab
;
749 ab
= kzalloc(sizeof(*ab
), GFP_KERNEL
);
753 init_completion(&ab
->driver_recovery
);
755 ab
->workqueue
= create_singlethread_workqueue("ath11k_wq");
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);
776 static int __init
ath11k_init(void)
780 ret
= ath11k_ahb_init();
782 printk(KERN_ERR
"failed to register ath11k ahb driver: %d\n",
786 module_init(ath11k_init
);
788 static void __exit
ath11k_exit(void)
792 module_exit(ath11k_exit
);
794 MODULE_DESCRIPTION("Driver support for Qualcomm Technologies 802.11ax wireless chip");
795 MODULE_LICENSE("Dual BSD/GPL");