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
;
395 ret
= ath11k_thermal_register(ab
);
397 ath11k_err(ab
, "could not register thermal device: %d\n",
399 goto err_dp_pdev_free
;
405 ath11k_dp_pdev_free(ab
);
407 ath11k_mac_unregister(ab
);
409 ath11k_debug_pdev_destroy(ab
);
414 static void ath11k_core_pdev_destroy(struct ath11k_base
*ab
)
416 ath11k_thermal_unregister(ab
);
417 ath11k_mac_unregister(ab
);
418 ath11k_ahb_ext_irq_disable(ab
);
419 ath11k_dp_pdev_free(ab
);
420 ath11k_debug_pdev_destroy(ab
);
423 static int ath11k_core_start(struct ath11k_base
*ab
,
424 enum ath11k_firmware_mode mode
)
428 ret
= ath11k_qmi_firmware_start(ab
, mode
);
430 ath11k_err(ab
, "failed to attach wmi: %d\n", ret
);
434 ret
= ath11k_wmi_attach(ab
);
436 ath11k_err(ab
, "failed to attach wmi: %d\n", ret
);
437 goto err_firmware_stop
;
440 ret
= ath11k_htc_init(ab
);
442 ath11k_err(ab
, "failed to init htc: %d\n", ret
);
446 ret
= ath11k_ahb_start(ab
);
448 ath11k_err(ab
, "failed to start HIF: %d\n", ret
);
452 ret
= ath11k_htc_wait_target(&ab
->htc
);
454 ath11k_err(ab
, "failed to connect to HTC: %d\n", ret
);
458 ret
= ath11k_dp_htt_connect(&ab
->dp
);
460 ath11k_err(ab
, "failed to connect to HTT: %d\n", ret
);
464 ret
= ath11k_wmi_connect(ab
);
466 ath11k_err(ab
, "failed to connect wmi: %d\n", ret
);
470 ret
= ath11k_htc_start(&ab
->htc
);
472 ath11k_err(ab
, "failed to start HTC: %d\n", ret
);
476 ret
= ath11k_wmi_wait_for_service_ready(ab
);
478 ath11k_err(ab
, "failed to receive wmi service ready event: %d\n",
483 ret
= ath11k_mac_allocate(ab
);
485 ath11k_err(ab
, "failed to create new hw device with mac80211 :%d\n",
490 ath11k_dp_pdev_pre_alloc(ab
);
492 ret
= ath11k_dp_pdev_reo_setup(ab
);
494 ath11k_err(ab
, "failed to initialize reo destination rings: %d\n", ret
);
495 goto err_mac_destroy
;
498 ret
= ath11k_wmi_cmd_init(ab
);
500 ath11k_err(ab
, "failed to send wmi init cmd: %d\n", ret
);
501 goto err_reo_cleanup
;
504 ret
= ath11k_wmi_wait_for_unified_ready(ab
);
506 ath11k_err(ab
, "failed to receive wmi unified ready event: %d\n",
508 goto err_reo_cleanup
;
511 ret
= ath11k_dp_tx_htt_h2t_ver_req_msg(ab
);
513 ath11k_err(ab
, "failed to send htt version request message: %d\n",
515 goto err_reo_cleanup
;
521 ath11k_dp_pdev_reo_cleanup(ab
);
523 ath11k_mac_destroy(ab
);
527 ath11k_wmi_detach(ab
);
529 ath11k_qmi_firmware_stop(ab
);
534 int ath11k_core_qmi_firmware_ready(struct ath11k_base
*ab
)
538 ret
= ath11k_ce_init_pipes(ab
);
540 ath11k_err(ab
, "failed to initialize CE: %d\n", ret
);
544 ret
= ath11k_dp_alloc(ab
);
546 ath11k_err(ab
, "failed to init DP: %d\n", ret
);
550 mutex_lock(&ab
->core_lock
);
551 ret
= ath11k_core_start(ab
, ATH11K_FIRMWARE_MODE_NORMAL
);
553 ath11k_err(ab
, "failed to start core: %d\n", ret
);
557 ret
= ath11k_core_pdev_create(ab
);
559 ath11k_err(ab
, "failed to create pdev core: %d\n", ret
);
562 ath11k_ahb_ext_irq_enable(ab
);
563 mutex_unlock(&ab
->core_lock
);
568 ath11k_core_stop(ab
);
569 ath11k_mac_destroy(ab
);
572 mutex_unlock(&ab
->core_lock
);
576 static int ath11k_core_reconfigure_on_crash(struct ath11k_base
*ab
)
580 mutex_lock(&ab
->core_lock
);
581 ath11k_thermal_unregister(ab
);
582 ath11k_ahb_ext_irq_disable(ab
);
583 ath11k_dp_pdev_free(ab
);
585 ath11k_wmi_detach(ab
);
586 ath11k_dp_pdev_reo_cleanup(ab
);
587 mutex_unlock(&ab
->core_lock
);
590 ath11k_hal_srng_deinit(ab
);
592 ab
->free_vdev_map
= (1LL << (ab
->num_radios
* TARGET_NUM_VDEVS
)) - 1;
594 ret
= ath11k_hal_srng_init(ab
);
598 clear_bit(ATH11K_FLAG_CRASH_FLUSH
, &ab
->dev_flags
);
600 ret
= ath11k_core_qmi_firmware_ready(ab
);
602 goto err_hal_srng_deinit
;
604 clear_bit(ATH11K_FLAG_RECOVERY
, &ab
->dev_flags
);
609 ath11k_hal_srng_deinit(ab
);
613 void ath11k_core_halt(struct ath11k
*ar
)
615 struct ath11k_base
*ab
= ar
->ab
;
617 lockdep_assert_held(&ar
->conf_mutex
);
619 ar
->num_created_vdevs
= 0;
620 ar
->allocated_vdev_map
= 0;
622 ath11k_mac_scan_finish(ar
);
623 ath11k_mac_peer_cleanup_all(ar
);
624 cancel_delayed_work_sync(&ar
->scan
.timeout
);
625 cancel_work_sync(&ar
->regd_update_work
);
627 rcu_assign_pointer(ab
->pdevs_active
[ar
->pdev_idx
], NULL
);
629 INIT_LIST_HEAD(&ar
->arvifs
);
630 idr_init(&ar
->txmgmt_idr
);
633 static void ath11k_core_restart(struct work_struct
*work
)
635 struct ath11k_base
*ab
= container_of(work
, struct ath11k_base
, restart_work
);
637 struct ath11k_pdev
*pdev
;
640 spin_lock_bh(&ab
->base_lock
);
641 ab
->stats
.fw_crash_counter
++;
642 spin_unlock_bh(&ab
->base_lock
);
644 for (i
= 0; i
< ab
->num_radios
; i
++) {
645 pdev
= &ab
->pdevs
[i
];
647 if (!ar
|| ar
->state
== ATH11K_STATE_OFF
)
650 ieee80211_stop_queues(ar
->hw
);
651 ath11k_mac_drain_tx(ar
);
652 complete(&ar
->scan
.started
);
653 complete(&ar
->scan
.completed
);
654 complete(&ar
->peer_assoc_done
);
655 complete(&ar
->install_key_done
);
656 complete(&ar
->vdev_setup_done
);
657 complete(&ar
->bss_survey_done
);
658 complete(&ar
->thermal
.wmi_sync
);
660 wake_up(&ar
->dp
.tx_empty_waitq
);
661 idr_for_each(&ar
->txmgmt_idr
,
662 ath11k_mac_tx_mgmt_pending_free
, ar
);
663 idr_destroy(&ar
->txmgmt_idr
);
666 wake_up(&ab
->wmi_ab
.tx_credits_wq
);
667 wake_up(&ab
->peer_mapping_wq
);
669 ret
= ath11k_core_reconfigure_on_crash(ab
);
671 ath11k_err(ab
, "failed to reconfigure driver on crash recovery\n");
675 for (i
= 0; i
< ab
->num_radios
; i
++) {
676 pdev
= &ab
->pdevs
[i
];
678 if (!ar
|| ar
->state
== ATH11K_STATE_OFF
)
681 mutex_lock(&ar
->conf_mutex
);
684 case ATH11K_STATE_ON
:
685 ar
->state
= ATH11K_STATE_RESTARTING
;
686 ath11k_core_halt(ar
);
687 ieee80211_restart_hw(ar
->hw
);
689 case ATH11K_STATE_OFF
:
691 "cannot restart radio %d that hasn't been started\n",
694 case ATH11K_STATE_RESTARTING
:
696 case ATH11K_STATE_RESTARTED
:
697 ar
->state
= ATH11K_STATE_WEDGED
;
699 case ATH11K_STATE_WEDGED
:
701 "device is wedged, will not restart radio %d\n", i
);
704 mutex_unlock(&ar
->conf_mutex
);
706 complete(&ab
->driver_recovery
);
709 int ath11k_core_init(struct ath11k_base
*ab
)
711 struct device
*dev
= ab
->dev
;
712 struct rproc
*prproc
;
713 phandle rproc_phandle
;
716 if (of_property_read_u32(dev
->of_node
, "qcom,rproc", &rproc_phandle
)) {
717 ath11k_err(ab
, "failed to get q6_rproc handle\n");
721 prproc
= rproc_get_by_phandle(rproc_phandle
);
723 ath11k_err(ab
, "failed to get rproc\n");
726 ab
->tgt_rproc
= prproc
;
727 ab
->hw_params
= ath11k_hw_params
;
729 ret
= ath11k_core_soc_create(ab
);
731 ath11k_err(ab
, "failed to create soc core: %d\n", ret
);
738 void ath11k_core_deinit(struct ath11k_base
*ab
)
740 mutex_lock(&ab
->core_lock
);
742 ath11k_core_pdev_destroy(ab
);
743 ath11k_core_stop(ab
);
745 mutex_unlock(&ab
->core_lock
);
747 ath11k_ahb_power_down(ab
);
748 ath11k_mac_destroy(ab
);
749 ath11k_core_soc_destroy(ab
);
752 void ath11k_core_free(struct ath11k_base
*ab
)
757 struct ath11k_base
*ath11k_core_alloc(struct device
*dev
)
759 struct ath11k_base
*ab
;
761 ab
= kzalloc(sizeof(*ab
), GFP_KERNEL
);
765 init_completion(&ab
->driver_recovery
);
767 ab
->workqueue
= create_singlethread_workqueue("ath11k_wq");
771 mutex_init(&ab
->core_lock
);
772 spin_lock_init(&ab
->base_lock
);
774 INIT_LIST_HEAD(&ab
->peers
);
775 init_waitqueue_head(&ab
->peer_mapping_wq
);
776 init_waitqueue_head(&ab
->wmi_ab
.tx_credits_wq
);
777 INIT_WORK(&ab
->restart_work
, ath11k_core_restart
);
778 timer_setup(&ab
->rx_replenish_retry
, ath11k_ce_rx_replenish_retry
, 0);
788 static int __init
ath11k_init(void)
792 ret
= ath11k_ahb_init();
794 printk(KERN_ERR
"failed to register ath11k ahb driver: %d\n",
798 module_init(ath11k_init
);
800 static void __exit
ath11k_exit(void)
804 module_exit(ath11k_exit
);
806 MODULE_DESCRIPTION("Driver support for Qualcomm Technologies 802.11ax wireless chip");
807 MODULE_LICENSE("Dual BSD/GPL");