2 * Copyright (c) 2005-2011 Atheros Communications Inc.
3 * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <linux/pci.h>
19 #include <linux/module.h>
20 #include <linux/interrupt.h>
21 #include <linux/spinlock.h>
22 #include <linux/bitops.h>
27 #include "targaddrs.h"
36 enum ath10k_pci_irq_mode
{
37 ATH10K_PCI_IRQ_AUTO
= 0,
38 ATH10K_PCI_IRQ_LEGACY
= 1,
39 ATH10K_PCI_IRQ_MSI
= 2,
42 static unsigned int ath10k_target_ps
;
43 static unsigned int ath10k_pci_irq_mode
= ATH10K_PCI_IRQ_AUTO
;
45 module_param(ath10k_target_ps
, uint
, 0644);
46 MODULE_PARM_DESC(ath10k_target_ps
, "Enable ath10k Target (SoC) PS option");
48 module_param_named(irq_mode
, ath10k_pci_irq_mode
, uint
, 0644);
49 MODULE_PARM_DESC(irq_mode
, "0: auto, 1: legacy, 2: msi (default: 0)");
51 #define QCA988X_2_0_DEVICE_ID (0x003c)
53 static DEFINE_PCI_DEVICE_TABLE(ath10k_pci_id_table
) = {
54 { PCI_VDEVICE(ATHEROS
, QCA988X_2_0_DEVICE_ID
) }, /* PCI-E QCA988X V2 */
58 static int ath10k_pci_diag_read_access(struct ath10k
*ar
, u32 address
,
61 static void ath10k_pci_process_ce(struct ath10k
*ar
);
62 static int ath10k_pci_post_rx(struct ath10k
*ar
);
63 static int ath10k_pci_post_rx_pipe(struct ath10k_pci_pipe
*pipe_info
,
65 static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe
*pipe_info
);
66 static int ath10k_pci_cold_reset(struct ath10k
*ar
);
67 static int ath10k_pci_warm_reset(struct ath10k
*ar
);
68 static int ath10k_pci_wait_for_target_init(struct ath10k
*ar
);
69 static int ath10k_pci_init_irq(struct ath10k
*ar
);
70 static int ath10k_pci_deinit_irq(struct ath10k
*ar
);
71 static int ath10k_pci_request_irq(struct ath10k
*ar
);
72 static void ath10k_pci_free_irq(struct ath10k
*ar
);
73 static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe
*tx_pipe
,
74 struct ath10k_ce_pipe
*rx_pipe
,
75 struct bmi_xfer
*xfer
);
76 static void ath10k_pci_cleanup_ce(struct ath10k
*ar
);
78 static const struct ce_attr host_ce_config_wlan
[] = {
79 /* CE0: host->target HTC control and raw streams */
81 .flags
= CE_ATTR_FLAGS
,
87 /* CE1: target->host HTT + HTC control */
89 .flags
= CE_ATTR_FLAGS
,
95 /* CE2: target->host WMI */
97 .flags
= CE_ATTR_FLAGS
,
103 /* CE3: host->target WMI */
105 .flags
= CE_ATTR_FLAGS
,
111 /* CE4: host->target HTT */
113 .flags
= CE_ATTR_FLAGS
| CE_ATTR_DIS_INTR
,
114 .src_nentries
= CE_HTT_H2T_MSG_SRC_NENTRIES
,
121 .flags
= CE_ATTR_FLAGS
,
127 /* CE6: target autonomous hif_memcpy */
129 .flags
= CE_ATTR_FLAGS
,
135 /* CE7: ce_diag, the Diagnostic Window */
137 .flags
= CE_ATTR_FLAGS
,
139 .src_sz_max
= DIAG_TRANSFER_LIMIT
,
144 /* Target firmware's Copy Engine configuration. */
145 static const struct ce_pipe_config target_ce_config_wlan
[] = {
146 /* CE0: host->target HTC control and raw streams */
149 .pipedir
= PIPEDIR_OUT
,
152 .flags
= CE_ATTR_FLAGS
,
156 /* CE1: target->host HTT + HTC control */
159 .pipedir
= PIPEDIR_IN
,
162 .flags
= CE_ATTR_FLAGS
,
166 /* CE2: target->host WMI */
169 .pipedir
= PIPEDIR_IN
,
172 .flags
= CE_ATTR_FLAGS
,
176 /* CE3: host->target WMI */
179 .pipedir
= PIPEDIR_OUT
,
182 .flags
= CE_ATTR_FLAGS
,
186 /* CE4: host->target HTT */
189 .pipedir
= PIPEDIR_OUT
,
192 .flags
= CE_ATTR_FLAGS
,
196 /* NB: 50% of src nentries, since tx has 2 frags */
201 .pipedir
= PIPEDIR_OUT
,
204 .flags
= CE_ATTR_FLAGS
,
208 /* CE6: Reserved for target autonomous hif_memcpy */
211 .pipedir
= PIPEDIR_INOUT
,
214 .flags
= CE_ATTR_FLAGS
,
218 /* CE7 used only by Host */
221 static bool ath10k_pci_irq_pending(struct ath10k
*ar
)
225 /* Check if the shared legacy irq is for us */
226 cause
= ath10k_pci_read32(ar
, SOC_CORE_BASE_ADDRESS
+
227 PCIE_INTR_CAUSE_ADDRESS
);
228 if (cause
& (PCIE_INTR_FIRMWARE_MASK
| PCIE_INTR_CE_MASK_ALL
))
234 static void ath10k_pci_disable_and_clear_legacy_irq(struct ath10k
*ar
)
236 /* IMPORTANT: INTR_CLR register has to be set after
237 * INTR_ENABLE is set to 0, otherwise interrupt can not be
239 ath10k_pci_write32(ar
, SOC_CORE_BASE_ADDRESS
+ PCIE_INTR_ENABLE_ADDRESS
,
241 ath10k_pci_write32(ar
, SOC_CORE_BASE_ADDRESS
+ PCIE_INTR_CLR_ADDRESS
,
242 PCIE_INTR_FIRMWARE_MASK
| PCIE_INTR_CE_MASK_ALL
);
244 /* IMPORTANT: this extra read transaction is required to
245 * flush the posted write buffer. */
246 (void) ath10k_pci_read32(ar
, SOC_CORE_BASE_ADDRESS
+
247 PCIE_INTR_ENABLE_ADDRESS
);
250 static void ath10k_pci_enable_legacy_irq(struct ath10k
*ar
)
252 ath10k_pci_write32(ar
, SOC_CORE_BASE_ADDRESS
+
253 PCIE_INTR_ENABLE_ADDRESS
,
254 PCIE_INTR_FIRMWARE_MASK
| PCIE_INTR_CE_MASK_ALL
);
256 /* IMPORTANT: this extra read transaction is required to
257 * flush the posted write buffer. */
258 (void) ath10k_pci_read32(ar
, SOC_CORE_BASE_ADDRESS
+
259 PCIE_INTR_ENABLE_ADDRESS
);
262 static irqreturn_t
ath10k_pci_early_irq_handler(int irq
, void *arg
)
264 struct ath10k
*ar
= arg
;
265 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
267 if (ar_pci
->num_msi_intrs
== 0) {
268 if (!ath10k_pci_irq_pending(ar
))
271 ath10k_pci_disable_and_clear_legacy_irq(ar
);
274 tasklet_schedule(&ar_pci
->early_irq_tasklet
);
279 static int ath10k_pci_request_early_irq(struct ath10k
*ar
)
281 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
284 /* Regardless whether MSI-X/MSI/legacy irqs have been set up the first
285 * interrupt from irq vector is triggered in all cases for FW
286 * indication/errors */
287 ret
= request_irq(ar_pci
->pdev
->irq
, ath10k_pci_early_irq_handler
,
288 IRQF_SHARED
, "ath10k_pci (early)", ar
);
290 ath10k_warn("failed to request early irq: %d\n", ret
);
297 static void ath10k_pci_free_early_irq(struct ath10k
*ar
)
299 free_irq(ath10k_pci_priv(ar
)->pdev
->irq
, ar
);
303 * Diagnostic read/write access is provided for startup/config/debug usage.
304 * Caller must guarantee proper alignment, when applicable, and single user
307 static int ath10k_pci_diag_read_mem(struct ath10k
*ar
, u32 address
, void *data
,
310 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
313 unsigned int completed_nbytes
, orig_nbytes
, remaining_bytes
;
316 struct ath10k_ce_pipe
*ce_diag
;
317 /* Host buffer address in CE space */
319 dma_addr_t ce_data_base
= 0;
320 void *data_buf
= NULL
;
324 * This code cannot handle reads to non-memory space. Redirect to the
325 * register read fn but preserve the multi word read capability of
328 if (address
< DRAM_BASE_ADDRESS
) {
329 if (!IS_ALIGNED(address
, 4) ||
330 !IS_ALIGNED((unsigned long)data
, 4))
333 while ((nbytes
>= 4) && ((ret
= ath10k_pci_diag_read_access(
334 ar
, address
, (u32
*)data
)) == 0)) {
335 nbytes
-= sizeof(u32
);
336 address
+= sizeof(u32
);
342 ce_diag
= ar_pci
->ce_diag
;
345 * Allocate a temporary bounce buffer to hold caller's data
346 * to be DMA'ed from Target. This guarantees
347 * 1) 4-byte alignment
348 * 2) Buffer in DMA-able space
350 orig_nbytes
= nbytes
;
351 data_buf
= (unsigned char *)pci_alloc_consistent(ar_pci
->pdev
,
359 memset(data_buf
, 0, orig_nbytes
);
361 remaining_bytes
= orig_nbytes
;
362 ce_data
= ce_data_base
;
363 while (remaining_bytes
) {
364 nbytes
= min_t(unsigned int, remaining_bytes
,
365 DIAG_TRANSFER_LIMIT
);
367 ret
= ath10k_ce_recv_buf_enqueue(ce_diag
, NULL
, ce_data
);
371 /* Request CE to send from Target(!) address to Host buffer */
373 * The address supplied by the caller is in the
374 * Target CPU virtual address space.
376 * In order to use this address with the diagnostic CE,
377 * convert it from Target CPU virtual address space
378 * to CE address space
381 address
= TARG_CPU_SPACE_TO_CE_SPACE(ar
, ar_pci
->mem
,
383 ath10k_pci_sleep(ar
);
385 ret
= ath10k_ce_send(ce_diag
, NULL
, (u32
)address
, nbytes
, 0,
391 while (ath10k_ce_completed_send_next(ce_diag
, NULL
, &buf
,
395 if (i
++ > DIAG_ACCESS_CE_TIMEOUT_MS
) {
401 if (nbytes
!= completed_nbytes
) {
406 if (buf
!= (u32
) address
) {
412 while (ath10k_ce_completed_recv_next(ce_diag
, NULL
, &buf
,
417 if (i
++ > DIAG_ACCESS_CE_TIMEOUT_MS
) {
423 if (nbytes
!= completed_nbytes
) {
428 if (buf
!= ce_data
) {
433 remaining_bytes
-= nbytes
;
440 /* Copy data from allocated DMA buf to caller's buf */
441 WARN_ON_ONCE(orig_nbytes
& 3);
442 for (i
= 0; i
< orig_nbytes
/ sizeof(__le32
); i
++) {
444 __le32_to_cpu(((__le32
*)data_buf
)[i
]);
447 ath10k_dbg(ATH10K_DBG_PCI
, "%s failure (0x%x)\n",
451 pci_free_consistent(ar_pci
->pdev
, orig_nbytes
,
452 data_buf
, ce_data_base
);
457 /* Read 4-byte aligned data from Target memory or register */
458 static int ath10k_pci_diag_read_access(struct ath10k
*ar
, u32 address
,
461 /* Assume range doesn't cross this boundary */
462 if (address
>= DRAM_BASE_ADDRESS
)
463 return ath10k_pci_diag_read_mem(ar
, address
, data
, sizeof(u32
));
466 *data
= ath10k_pci_read32(ar
, address
);
467 ath10k_pci_sleep(ar
);
471 static int ath10k_pci_diag_write_mem(struct ath10k
*ar
, u32 address
,
472 const void *data
, int nbytes
)
474 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
477 unsigned int completed_nbytes
, orig_nbytes
, remaining_bytes
;
480 struct ath10k_ce_pipe
*ce_diag
;
481 void *data_buf
= NULL
;
482 u32 ce_data
; /* Host buffer address in CE space */
483 dma_addr_t ce_data_base
= 0;
486 ce_diag
= ar_pci
->ce_diag
;
489 * Allocate a temporary bounce buffer to hold caller's data
490 * to be DMA'ed to Target. This guarantees
491 * 1) 4-byte alignment
492 * 2) Buffer in DMA-able space
494 orig_nbytes
= nbytes
;
495 data_buf
= (unsigned char *)pci_alloc_consistent(ar_pci
->pdev
,
503 /* Copy caller's data to allocated DMA buf */
504 WARN_ON_ONCE(orig_nbytes
& 3);
505 for (i
= 0; i
< orig_nbytes
/ sizeof(__le32
); i
++)
506 ((__le32
*)data_buf
)[i
] = __cpu_to_le32(((u32
*)data
)[i
]);
509 * The address supplied by the caller is in the
510 * Target CPU virtual address space.
512 * In order to use this address with the diagnostic CE,
514 * Target CPU virtual address space
519 address
= TARG_CPU_SPACE_TO_CE_SPACE(ar
, ar_pci
->mem
, address
);
520 ath10k_pci_sleep(ar
);
522 remaining_bytes
= orig_nbytes
;
523 ce_data
= ce_data_base
;
524 while (remaining_bytes
) {
525 /* FIXME: check cast */
526 nbytes
= min_t(int, remaining_bytes
, DIAG_TRANSFER_LIMIT
);
528 /* Set up to receive directly into Target(!) address */
529 ret
= ath10k_ce_recv_buf_enqueue(ce_diag
, NULL
, address
);
534 * Request CE to send caller-supplied data that
535 * was copied to bounce buffer to Target(!) address.
537 ret
= ath10k_ce_send(ce_diag
, NULL
, (u32
) ce_data
,
543 while (ath10k_ce_completed_send_next(ce_diag
, NULL
, &buf
,
548 if (i
++ > DIAG_ACCESS_CE_TIMEOUT_MS
) {
554 if (nbytes
!= completed_nbytes
) {
559 if (buf
!= ce_data
) {
565 while (ath10k_ce_completed_recv_next(ce_diag
, NULL
, &buf
,
570 if (i
++ > DIAG_ACCESS_CE_TIMEOUT_MS
) {
576 if (nbytes
!= completed_nbytes
) {
581 if (buf
!= address
) {
586 remaining_bytes
-= nbytes
;
593 pci_free_consistent(ar_pci
->pdev
, orig_nbytes
, data_buf
,
598 ath10k_dbg(ATH10K_DBG_PCI
, "%s failure (0x%x)\n", __func__
,
604 /* Write 4B data to Target memory or register */
605 static int ath10k_pci_diag_write_access(struct ath10k
*ar
, u32 address
,
608 /* Assume range doesn't cross this boundary */
609 if (address
>= DRAM_BASE_ADDRESS
)
610 return ath10k_pci_diag_write_mem(ar
, address
, &data
,
614 ath10k_pci_write32(ar
, address
, data
);
615 ath10k_pci_sleep(ar
);
619 static bool ath10k_pci_target_is_awake(struct ath10k
*ar
)
621 void __iomem
*mem
= ath10k_pci_priv(ar
)->mem
;
623 val
= ioread32(mem
+ PCIE_LOCAL_BASE_ADDRESS
+
625 return (RTC_STATE_V_GET(val
) == RTC_STATE_V_ON
);
628 int ath10k_do_pci_wake(struct ath10k
*ar
)
630 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
631 void __iomem
*pci_addr
= ar_pci
->mem
;
635 if (atomic_read(&ar_pci
->keep_awake_count
) == 0) {
637 iowrite32(PCIE_SOC_WAKE_V_MASK
,
638 pci_addr
+ PCIE_LOCAL_BASE_ADDRESS
+
639 PCIE_SOC_WAKE_ADDRESS
);
641 atomic_inc(&ar_pci
->keep_awake_count
);
643 if (ar_pci
->verified_awake
)
647 if (ath10k_pci_target_is_awake(ar
)) {
648 ar_pci
->verified_awake
= true;
652 if (tot_delay
> PCIE_WAKE_TIMEOUT
) {
653 ath10k_warn("target took longer %d us to wake up (awake count %d)\n",
655 atomic_read(&ar_pci
->keep_awake_count
));
660 tot_delay
+= curr_delay
;
667 void ath10k_do_pci_sleep(struct ath10k
*ar
)
669 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
670 void __iomem
*pci_addr
= ar_pci
->mem
;
672 if (atomic_dec_and_test(&ar_pci
->keep_awake_count
)) {
674 ar_pci
->verified_awake
= false;
675 iowrite32(PCIE_SOC_WAKE_RESET
,
676 pci_addr
+ PCIE_LOCAL_BASE_ADDRESS
+
677 PCIE_SOC_WAKE_ADDRESS
);
682 * FIXME: Handle OOM properly.
685 struct ath10k_pci_compl
*get_free_compl(struct ath10k_pci_pipe
*pipe_info
)
687 struct ath10k_pci_compl
*compl = NULL
;
689 spin_lock_bh(&pipe_info
->pipe_lock
);
690 if (list_empty(&pipe_info
->compl_free
)) {
691 ath10k_warn("Completion buffers are full\n");
694 compl = list_first_entry(&pipe_info
->compl_free
,
695 struct ath10k_pci_compl
, list
);
696 list_del(&compl->list
);
698 spin_unlock_bh(&pipe_info
->pipe_lock
);
702 /* Called by lower (CE) layer when a send to Target completes. */
703 static void ath10k_pci_ce_send_done(struct ath10k_ce_pipe
*ce_state
)
705 struct ath10k
*ar
= ce_state
->ar
;
706 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
707 struct ath10k_pci_pipe
*pipe_info
= &ar_pci
->pipe_info
[ce_state
->id
];
708 struct ath10k_pci_compl
*compl;
709 void *transfer_context
;
712 unsigned int transfer_id
;
714 while (ath10k_ce_completed_send_next(ce_state
, &transfer_context
,
716 &transfer_id
) == 0) {
717 if (transfer_context
== NULL
)
720 compl = get_free_compl(pipe_info
);
724 compl->state
= ATH10K_PCI_COMPL_SEND
;
725 compl->ce_state
= ce_state
;
726 compl->pipe_info
= pipe_info
;
727 compl->skb
= transfer_context
;
728 compl->nbytes
= nbytes
;
729 compl->transfer_id
= transfer_id
;
733 * Add the completion to the processing queue.
735 spin_lock_bh(&ar_pci
->compl_lock
);
736 list_add_tail(&compl->list
, &ar_pci
->compl_process
);
737 spin_unlock_bh(&ar_pci
->compl_lock
);
740 ath10k_pci_process_ce(ar
);
743 /* Called by lower (CE) layer when data is received from the Target. */
744 static void ath10k_pci_ce_recv_data(struct ath10k_ce_pipe
*ce_state
)
746 struct ath10k
*ar
= ce_state
->ar
;
747 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
748 struct ath10k_pci_pipe
*pipe_info
= &ar_pci
->pipe_info
[ce_state
->id
];
749 struct ath10k_pci_compl
*compl;
751 void *transfer_context
;
754 unsigned int transfer_id
;
757 while (ath10k_ce_completed_recv_next(ce_state
, &transfer_context
,
758 &ce_data
, &nbytes
, &transfer_id
,
760 compl = get_free_compl(pipe_info
);
764 compl->state
= ATH10K_PCI_COMPL_RECV
;
765 compl->ce_state
= ce_state
;
766 compl->pipe_info
= pipe_info
;
767 compl->skb
= transfer_context
;
768 compl->nbytes
= nbytes
;
769 compl->transfer_id
= transfer_id
;
770 compl->flags
= flags
;
772 skb
= transfer_context
;
773 dma_unmap_single(ar
->dev
, ATH10K_SKB_CB(skb
)->paddr
,
774 skb
->len
+ skb_tailroom(skb
),
777 * Add the completion to the processing queue.
779 spin_lock_bh(&ar_pci
->compl_lock
);
780 list_add_tail(&compl->list
, &ar_pci
->compl_process
);
781 spin_unlock_bh(&ar_pci
->compl_lock
);
784 ath10k_pci_process_ce(ar
);
787 static int ath10k_pci_hif_tx_sg(struct ath10k
*ar
, u8 pipe_id
,
788 struct ath10k_hif_sg_item
*items
, int n_items
)
790 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
791 struct ath10k_pci_pipe
*pci_pipe
= &ar_pci
->pipe_info
[pipe_id
];
792 struct ath10k_ce_pipe
*ce_pipe
= pci_pipe
->ce_hdl
;
793 struct ath10k_ce_ring
*src_ring
= ce_pipe
->src_ring
;
794 unsigned int nentries_mask
= src_ring
->nentries_mask
;
795 unsigned int sw_index
= src_ring
->sw_index
;
796 unsigned int write_index
= src_ring
->write_index
;
799 spin_lock_bh(&ar_pci
->ce_lock
);
801 if (unlikely(CE_RING_DELTA(nentries_mask
,
802 write_index
, sw_index
- 1) < n_items
)) {
807 for (i
= 0; i
< n_items
- 1; i
++) {
808 ath10k_dbg(ATH10K_DBG_PCI
,
809 "pci tx item %d paddr 0x%08x len %d n_items %d\n",
810 i
, items
[i
].paddr
, items
[i
].len
, n_items
);
811 ath10k_dbg_dump(ATH10K_DBG_PCI_DUMP
, NULL
, "item data: ",
812 items
[i
].vaddr
, items
[i
].len
);
814 err
= ath10k_ce_send_nolock(ce_pipe
,
815 items
[i
].transfer_context
,
818 items
[i
].transfer_id
,
819 CE_SEND_FLAG_GATHER
);
824 /* `i` is equal to `n_items -1` after for() */
826 ath10k_dbg(ATH10K_DBG_PCI
,
827 "pci tx item %d paddr 0x%08x len %d n_items %d\n",
828 i
, items
[i
].paddr
, items
[i
].len
, n_items
);
829 ath10k_dbg_dump(ATH10K_DBG_PCI_DUMP
, NULL
, "item data: ",
830 items
[i
].vaddr
, items
[i
].len
);
832 err
= ath10k_ce_send_nolock(ce_pipe
,
833 items
[i
].transfer_context
,
836 items
[i
].transfer_id
,
843 spin_unlock_bh(&ar_pci
->ce_lock
);
847 static u16
ath10k_pci_hif_get_free_queue_number(struct ath10k
*ar
, u8 pipe
)
849 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
850 return ath10k_ce_num_free_src_entries(ar_pci
->pipe_info
[pipe
].ce_hdl
);
853 static void ath10k_pci_hif_dump_area(struct ath10k
*ar
)
855 u32 reg_dump_area
= 0;
856 u32 reg_dump_values
[REG_DUMP_COUNT_QCA988X
] = {};
861 ath10k_err("firmware crashed!\n");
862 ath10k_err("hardware name %s version 0x%x\n",
863 ar
->hw_params
.name
, ar
->target_version
);
864 ath10k_err("firmware version: %s\n", ar
->hw
->wiphy
->fw_version
);
866 host_addr
= host_interest_item_address(HI_ITEM(hi_failure_state
));
867 ret
= ath10k_pci_diag_read_mem(ar
, host_addr
,
868 ®_dump_area
, sizeof(u32
));
870 ath10k_err("failed to read FW dump area address: %d\n", ret
);
874 ath10k_err("target register Dump Location: 0x%08X\n", reg_dump_area
);
876 ret
= ath10k_pci_diag_read_mem(ar
, reg_dump_area
,
878 REG_DUMP_COUNT_QCA988X
* sizeof(u32
));
880 ath10k_err("failed to read FW dump area: %d\n", ret
);
884 BUILD_BUG_ON(REG_DUMP_COUNT_QCA988X
% 4);
886 ath10k_err("target Register Dump\n");
887 for (i
= 0; i
< REG_DUMP_COUNT_QCA988X
; i
+= 4)
888 ath10k_err("[%02d]: 0x%08X 0x%08X 0x%08X 0x%08X\n",
891 reg_dump_values
[i
+ 1],
892 reg_dump_values
[i
+ 2],
893 reg_dump_values
[i
+ 3]);
895 queue_work(ar
->workqueue
, &ar
->restart_work
);
898 static void ath10k_pci_hif_send_complete_check(struct ath10k
*ar
, u8 pipe
,
904 * Decide whether to actually poll for completions, or just
905 * wait for a later chance.
906 * If there seem to be plenty of resources left, then just wait
907 * since checking involves reading a CE register, which is a
908 * relatively expensive operation.
910 resources
= ath10k_pci_hif_get_free_queue_number(ar
, pipe
);
913 * If at least 50% of the total resources are still available,
914 * don't bother checking again yet.
916 if (resources
> (host_ce_config_wlan
[pipe
].src_nentries
>> 1))
919 ath10k_ce_per_engine_service(ar
, pipe
);
922 static void ath10k_pci_hif_set_callbacks(struct ath10k
*ar
,
923 struct ath10k_hif_cb
*callbacks
)
925 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
927 ath10k_dbg(ATH10K_DBG_PCI
, "%s\n", __func__
);
929 memcpy(&ar_pci
->msg_callbacks_current
, callbacks
,
930 sizeof(ar_pci
->msg_callbacks_current
));
933 static int ath10k_pci_alloc_compl(struct ath10k
*ar
)
935 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
936 const struct ce_attr
*attr
;
937 struct ath10k_pci_pipe
*pipe_info
;
938 struct ath10k_pci_compl
*compl;
939 int i
, pipe_num
, completions
;
941 spin_lock_init(&ar_pci
->compl_lock
);
942 INIT_LIST_HEAD(&ar_pci
->compl_process
);
944 for (pipe_num
= 0; pipe_num
< CE_COUNT
; pipe_num
++) {
945 pipe_info
= &ar_pci
->pipe_info
[pipe_num
];
947 spin_lock_init(&pipe_info
->pipe_lock
);
948 INIT_LIST_HEAD(&pipe_info
->compl_free
);
950 /* Handle Diagnostic CE specially */
951 if (pipe_info
->ce_hdl
== ar_pci
->ce_diag
)
954 attr
= &host_ce_config_wlan
[pipe_num
];
957 if (attr
->src_nentries
)
958 completions
+= attr
->src_nentries
;
960 if (attr
->dest_nentries
)
961 completions
+= attr
->dest_nentries
;
963 for (i
= 0; i
< completions
; i
++) {
964 compl = kmalloc(sizeof(*compl), GFP_KERNEL
);
966 ath10k_warn("No memory for completion state\n");
967 ath10k_pci_cleanup_ce(ar
);
971 compl->state
= ATH10K_PCI_COMPL_FREE
;
972 list_add_tail(&compl->list
, &pipe_info
->compl_free
);
979 static int ath10k_pci_setup_ce_irq(struct ath10k
*ar
)
981 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
982 const struct ce_attr
*attr
;
983 struct ath10k_pci_pipe
*pipe_info
;
984 int pipe_num
, disable_interrupts
;
986 for (pipe_num
= 0; pipe_num
< CE_COUNT
; pipe_num
++) {
987 pipe_info
= &ar_pci
->pipe_info
[pipe_num
];
989 /* Handle Diagnostic CE specially */
990 if (pipe_info
->ce_hdl
== ar_pci
->ce_diag
)
993 attr
= &host_ce_config_wlan
[pipe_num
];
995 if (attr
->src_nentries
) {
996 disable_interrupts
= attr
->flags
& CE_ATTR_DIS_INTR
;
997 ath10k_ce_send_cb_register(pipe_info
->ce_hdl
,
998 ath10k_pci_ce_send_done
,
1002 if (attr
->dest_nentries
)
1003 ath10k_ce_recv_cb_register(pipe_info
->ce_hdl
,
1004 ath10k_pci_ce_recv_data
);
1010 static void ath10k_pci_kill_tasklet(struct ath10k
*ar
)
1012 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
1015 tasklet_kill(&ar_pci
->intr_tq
);
1016 tasklet_kill(&ar_pci
->msi_fw_err
);
1017 tasklet_kill(&ar_pci
->early_irq_tasklet
);
1019 for (i
= 0; i
< CE_COUNT
; i
++)
1020 tasklet_kill(&ar_pci
->pipe_info
[i
].intr
);
1023 static void ath10k_pci_cleanup_ce(struct ath10k
*ar
)
1025 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
1026 struct ath10k_pci_compl
*compl, *tmp
;
1027 struct ath10k_pci_pipe
*pipe_info
;
1028 struct sk_buff
*netbuf
;
1031 /* Free pending completions. */
1032 spin_lock_bh(&ar_pci
->compl_lock
);
1033 if (!list_empty(&ar_pci
->compl_process
))
1034 ath10k_warn("pending completions still present! possible memory leaks.\n");
1036 list_for_each_entry_safe(compl, tmp
, &ar_pci
->compl_process
, list
) {
1037 list_del(&compl->list
);
1038 netbuf
= compl->skb
;
1039 dev_kfree_skb_any(netbuf
);
1042 spin_unlock_bh(&ar_pci
->compl_lock
);
1044 /* Free unused completions for each pipe. */
1045 for (pipe_num
= 0; pipe_num
< CE_COUNT
; pipe_num
++) {
1046 pipe_info
= &ar_pci
->pipe_info
[pipe_num
];
1048 spin_lock_bh(&pipe_info
->pipe_lock
);
1049 list_for_each_entry_safe(compl, tmp
,
1050 &pipe_info
->compl_free
, list
) {
1051 list_del(&compl->list
);
1054 spin_unlock_bh(&pipe_info
->pipe_lock
);
1058 static void ath10k_pci_process_ce(struct ath10k
*ar
)
1060 struct ath10k_pci
*ar_pci
= ar
->hif
.priv
;
1061 struct ath10k_hif_cb
*cb
= &ar_pci
->msg_callbacks_current
;
1062 struct ath10k_pci_compl
*compl;
1063 struct sk_buff
*skb
;
1064 unsigned int nbytes
;
1065 int ret
, send_done
= 0;
1067 /* Upper layers aren't ready to handle tx/rx completions in parallel so
1068 * we must serialize all completion processing. */
1070 spin_lock_bh(&ar_pci
->compl_lock
);
1071 if (ar_pci
->compl_processing
) {
1072 spin_unlock_bh(&ar_pci
->compl_lock
);
1075 ar_pci
->compl_processing
= true;
1076 spin_unlock_bh(&ar_pci
->compl_lock
);
1079 spin_lock_bh(&ar_pci
->compl_lock
);
1080 if (list_empty(&ar_pci
->compl_process
)) {
1081 spin_unlock_bh(&ar_pci
->compl_lock
);
1084 compl = list_first_entry(&ar_pci
->compl_process
,
1085 struct ath10k_pci_compl
, list
);
1086 list_del(&compl->list
);
1087 spin_unlock_bh(&ar_pci
->compl_lock
);
1089 switch (compl->state
) {
1090 case ATH10K_PCI_COMPL_SEND
:
1091 cb
->tx_completion(ar
,
1093 compl->transfer_id
);
1096 case ATH10K_PCI_COMPL_RECV
:
1097 ret
= ath10k_pci_post_rx_pipe(compl->pipe_info
, 1);
1099 ath10k_warn("failed to post RX buffer for pipe %d: %d\n",
1100 compl->pipe_info
->pipe_num
, ret
);
1105 nbytes
= compl->nbytes
;
1107 ath10k_dbg(ATH10K_DBG_PCI
,
1108 "ath10k_pci_ce_recv_data netbuf=%p nbytes=%d\n",
1110 ath10k_dbg_dump(ATH10K_DBG_PCI_DUMP
, NULL
,
1111 "ath10k rx: ", skb
->data
, nbytes
);
1113 if (skb
->len
+ skb_tailroom(skb
) >= nbytes
) {
1115 skb_put(skb
, nbytes
);
1116 cb
->rx_completion(ar
, skb
,
1117 compl->pipe_info
->pipe_num
);
1119 ath10k_warn("rxed more than expected (nbytes %d, max %d)",
1121 skb
->len
+ skb_tailroom(skb
));
1124 case ATH10K_PCI_COMPL_FREE
:
1125 ath10k_warn("free completion cannot be processed\n");
1128 ath10k_warn("invalid completion state (%d)\n",
1133 compl->state
= ATH10K_PCI_COMPL_FREE
;
1136 * Add completion back to the pipe's free list.
1138 spin_lock_bh(&compl->pipe_info
->pipe_lock
);
1139 list_add_tail(&compl->list
, &compl->pipe_info
->compl_free
);
1140 spin_unlock_bh(&compl->pipe_info
->pipe_lock
);
1143 spin_lock_bh(&ar_pci
->compl_lock
);
1144 ar_pci
->compl_processing
= false;
1145 spin_unlock_bh(&ar_pci
->compl_lock
);
1148 /* TODO - temporary mapping while we have too few CE's */
1149 static int ath10k_pci_hif_map_service_to_pipe(struct ath10k
*ar
,
1150 u16 service_id
, u8
*ul_pipe
,
1151 u8
*dl_pipe
, int *ul_is_polled
,
1156 /* polling for received messages not supported */
1159 switch (service_id
) {
1160 case ATH10K_HTC_SVC_ID_HTT_DATA_MSG
:
1162 * Host->target HTT gets its own pipe, so it can be polled
1163 * while other pipes are interrupt driven.
1167 * Use the same target->host pipe for HTC ctrl, HTC raw
1173 case ATH10K_HTC_SVC_ID_RSVD_CTRL
:
1174 case ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS
:
1176 * Note: HTC_RAW_STREAMS_SVC is currently unused, and
1177 * HTC_CTRL_RSVD_SVC could share the same pipe as the
1178 * WMI services. So, if another CE is needed, change
1179 * this to *ul_pipe = 3, which frees up CE 0.
1186 case ATH10K_HTC_SVC_ID_WMI_DATA_BK
:
1187 case ATH10K_HTC_SVC_ID_WMI_DATA_BE
:
1188 case ATH10K_HTC_SVC_ID_WMI_DATA_VI
:
1189 case ATH10K_HTC_SVC_ID_WMI_DATA_VO
:
1191 case ATH10K_HTC_SVC_ID_WMI_CONTROL
:
1197 /* pipe 6 reserved */
1198 /* pipe 7 reserved */
1205 (host_ce_config_wlan
[*ul_pipe
].flags
& CE_ATTR_DIS_INTR
) != 0;
1210 static void ath10k_pci_hif_get_default_pipe(struct ath10k
*ar
,
1211 u8
*ul_pipe
, u8
*dl_pipe
)
1213 int ul_is_polled
, dl_is_polled
;
1215 (void)ath10k_pci_hif_map_service_to_pipe(ar
,
1216 ATH10K_HTC_SVC_ID_RSVD_CTRL
,
1223 static int ath10k_pci_post_rx_pipe(struct ath10k_pci_pipe
*pipe_info
,
1226 struct ath10k
*ar
= pipe_info
->hif_ce_state
;
1227 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
1228 struct ath10k_ce_pipe
*ce_state
= pipe_info
->ce_hdl
;
1229 struct sk_buff
*skb
;
1233 if (pipe_info
->buf_sz
== 0)
1236 for (i
= 0; i
< num
; i
++) {
1237 skb
= dev_alloc_skb(pipe_info
->buf_sz
);
1239 ath10k_warn("failed to allocate skbuff for pipe %d\n",
1245 WARN_ONCE((unsigned long)skb
->data
& 3, "unaligned skb");
1247 ce_data
= dma_map_single(ar
->dev
, skb
->data
,
1248 skb
->len
+ skb_tailroom(skb
),
1251 if (unlikely(dma_mapping_error(ar
->dev
, ce_data
))) {
1252 ath10k_warn("failed to DMA map sk_buff\n");
1253 dev_kfree_skb_any(skb
);
1258 ATH10K_SKB_CB(skb
)->paddr
= ce_data
;
1260 pci_dma_sync_single_for_device(ar_pci
->pdev
, ce_data
,
1262 PCI_DMA_FROMDEVICE
);
1264 ret
= ath10k_ce_recv_buf_enqueue(ce_state
, (void *)skb
,
1267 ath10k_warn("failed to enqueue to pipe %d: %d\n",
1276 ath10k_pci_rx_pipe_cleanup(pipe_info
);
1280 static int ath10k_pci_post_rx(struct ath10k
*ar
)
1282 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
1283 struct ath10k_pci_pipe
*pipe_info
;
1284 const struct ce_attr
*attr
;
1285 int pipe_num
, ret
= 0;
1287 for (pipe_num
= 0; pipe_num
< CE_COUNT
; pipe_num
++) {
1288 pipe_info
= &ar_pci
->pipe_info
[pipe_num
];
1289 attr
= &host_ce_config_wlan
[pipe_num
];
1291 if (attr
->dest_nentries
== 0)
1294 ret
= ath10k_pci_post_rx_pipe(pipe_info
,
1295 attr
->dest_nentries
- 1);
1297 ath10k_warn("failed to post RX buffer for pipe %d: %d\n",
1300 for (; pipe_num
>= 0; pipe_num
--) {
1301 pipe_info
= &ar_pci
->pipe_info
[pipe_num
];
1302 ath10k_pci_rx_pipe_cleanup(pipe_info
);
1311 static int ath10k_pci_hif_start(struct ath10k
*ar
)
1313 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
1316 ath10k_pci_free_early_irq(ar
);
1317 ath10k_pci_kill_tasklet(ar
);
1319 ret
= ath10k_pci_alloc_compl(ar
);
1321 ath10k_warn("failed to allocate CE completions: %d\n", ret
);
1325 ret
= ath10k_pci_request_irq(ar
);
1327 ath10k_warn("failed to post RX buffers for all pipes: %d\n",
1329 goto err_free_compl
;
1332 ret
= ath10k_pci_setup_ce_irq(ar
);
1334 ath10k_warn("failed to setup CE interrupts: %d\n", ret
);
1338 /* Post buffers once to start things off. */
1339 ret
= ath10k_pci_post_rx(ar
);
1341 ath10k_warn("failed to post RX buffers for all pipes: %d\n",
1346 ar_pci
->started
= 1;
1350 ath10k_ce_disable_interrupts(ar
);
1351 ath10k_pci_free_irq(ar
);
1352 ath10k_pci_kill_tasklet(ar
);
1353 ath10k_pci_process_ce(ar
);
1355 ath10k_pci_cleanup_ce(ar
);
1357 /* Though there should be no interrupts (device was reset)
1358 * power_down() expects the early IRQ to be installed as per the
1359 * driver lifecycle. */
1360 ret_early
= ath10k_pci_request_early_irq(ar
);
1362 ath10k_warn("failed to re-enable early irq: %d\n", ret_early
);
1367 static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe
*pipe_info
)
1370 struct ath10k_pci
*ar_pci
;
1371 struct ath10k_ce_pipe
*ce_hdl
;
1373 struct sk_buff
*netbuf
;
1376 buf_sz
= pipe_info
->buf_sz
;
1378 /* Unused Copy Engine */
1382 ar
= pipe_info
->hif_ce_state
;
1383 ar_pci
= ath10k_pci_priv(ar
);
1385 if (!ar_pci
->started
)
1388 ce_hdl
= pipe_info
->ce_hdl
;
1390 while (ath10k_ce_revoke_recv_next(ce_hdl
, (void **)&netbuf
,
1392 dma_unmap_single(ar
->dev
, ATH10K_SKB_CB(netbuf
)->paddr
,
1393 netbuf
->len
+ skb_tailroom(netbuf
),
1395 dev_kfree_skb_any(netbuf
);
1399 static void ath10k_pci_tx_pipe_cleanup(struct ath10k_pci_pipe
*pipe_info
)
1402 struct ath10k_pci
*ar_pci
;
1403 struct ath10k_ce_pipe
*ce_hdl
;
1404 struct sk_buff
*netbuf
;
1406 unsigned int nbytes
;
1410 buf_sz
= pipe_info
->buf_sz
;
1412 /* Unused Copy Engine */
1416 ar
= pipe_info
->hif_ce_state
;
1417 ar_pci
= ath10k_pci_priv(ar
);
1419 if (!ar_pci
->started
)
1422 ce_hdl
= pipe_info
->ce_hdl
;
1424 while (ath10k_ce_cancel_send_next(ce_hdl
, (void **)&netbuf
,
1425 &ce_data
, &nbytes
, &id
) == 0) {
1427 * Indicate the completion to higer layer to free
1432 ath10k_warn("invalid sk_buff on CE %d - NULL pointer. firmware crashed?\n",
1437 ar_pci
->msg_callbacks_current
.tx_completion(ar
,
1444 * Cleanup residual buffers for device shutdown:
1445 * buffers that were enqueued for receive
1446 * buffers that were to be sent
1447 * Note: Buffers that had completed but which were
1448 * not yet processed are on a completion queue. They
1449 * are handled when the completion thread shuts down.
1451 static void ath10k_pci_buffer_cleanup(struct ath10k
*ar
)
1453 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
1456 for (pipe_num
= 0; pipe_num
< CE_COUNT
; pipe_num
++) {
1457 struct ath10k_pci_pipe
*pipe_info
;
1459 pipe_info
= &ar_pci
->pipe_info
[pipe_num
];
1460 ath10k_pci_rx_pipe_cleanup(pipe_info
);
1461 ath10k_pci_tx_pipe_cleanup(pipe_info
);
1465 static void ath10k_pci_ce_deinit(struct ath10k
*ar
)
1467 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
1468 struct ath10k_pci_pipe
*pipe_info
;
1471 for (pipe_num
= 0; pipe_num
< CE_COUNT
; pipe_num
++) {
1472 pipe_info
= &ar_pci
->pipe_info
[pipe_num
];
1473 if (pipe_info
->ce_hdl
) {
1474 ath10k_ce_deinit(pipe_info
->ce_hdl
);
1475 pipe_info
->ce_hdl
= NULL
;
1476 pipe_info
->buf_sz
= 0;
1481 static void ath10k_pci_hif_stop(struct ath10k
*ar
)
1483 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
1486 ath10k_dbg(ATH10K_DBG_PCI
, "%s\n", __func__
);
1488 ret
= ath10k_ce_disable_interrupts(ar
);
1490 ath10k_warn("failed to disable CE interrupts: %d\n", ret
);
1492 ath10k_pci_free_irq(ar
);
1493 ath10k_pci_kill_tasklet(ar
);
1495 ret
= ath10k_pci_request_early_irq(ar
);
1497 ath10k_warn("failed to re-enable early irq: %d\n", ret
);
1499 /* At this point, asynchronous threads are stopped, the target should
1500 * not DMA nor interrupt. We process the leftovers and then free
1501 * everything else up. */
1503 ath10k_pci_process_ce(ar
);
1504 ath10k_pci_cleanup_ce(ar
);
1505 ath10k_pci_buffer_cleanup(ar
);
1507 /* Make the sure the device won't access any structures on the host by
1508 * resetting it. The device was fed with PCI CE ringbuffer
1509 * configuration during init. If ringbuffers are freed and the device
1510 * were to access them this could lead to memory corruption on the
1512 ath10k_pci_warm_reset(ar
);
1514 ar_pci
->started
= 0;
1517 static int ath10k_pci_hif_exchange_bmi_msg(struct ath10k
*ar
,
1518 void *req
, u32 req_len
,
1519 void *resp
, u32
*resp_len
)
1521 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
1522 struct ath10k_pci_pipe
*pci_tx
= &ar_pci
->pipe_info
[BMI_CE_NUM_TO_TARG
];
1523 struct ath10k_pci_pipe
*pci_rx
= &ar_pci
->pipe_info
[BMI_CE_NUM_TO_HOST
];
1524 struct ath10k_ce_pipe
*ce_tx
= pci_tx
->ce_hdl
;
1525 struct ath10k_ce_pipe
*ce_rx
= pci_rx
->ce_hdl
;
1526 dma_addr_t req_paddr
= 0;
1527 dma_addr_t resp_paddr
= 0;
1528 struct bmi_xfer xfer
= {};
1529 void *treq
, *tresp
= NULL
;
1534 if (resp
&& !resp_len
)
1537 if (resp
&& resp_len
&& *resp_len
== 0)
1540 treq
= kmemdup(req
, req_len
, GFP_KERNEL
);
1544 req_paddr
= dma_map_single(ar
->dev
, treq
, req_len
, DMA_TO_DEVICE
);
1545 ret
= dma_mapping_error(ar
->dev
, req_paddr
);
1549 if (resp
&& resp_len
) {
1550 tresp
= kzalloc(*resp_len
, GFP_KERNEL
);
1556 resp_paddr
= dma_map_single(ar
->dev
, tresp
, *resp_len
,
1558 ret
= dma_mapping_error(ar
->dev
, resp_paddr
);
1562 xfer
.wait_for_resp
= true;
1565 ath10k_ce_recv_buf_enqueue(ce_rx
, &xfer
, resp_paddr
);
1568 init_completion(&xfer
.done
);
1570 ret
= ath10k_ce_send(ce_tx
, &xfer
, req_paddr
, req_len
, -1, 0);
1574 ret
= ath10k_pci_bmi_wait(ce_tx
, ce_rx
, &xfer
);
1577 unsigned int unused_nbytes
;
1578 unsigned int unused_id
;
1580 ath10k_ce_cancel_send_next(ce_tx
, NULL
, &unused_buffer
,
1581 &unused_nbytes
, &unused_id
);
1583 /* non-zero means we did not time out */
1591 ath10k_ce_revoke_recv_next(ce_rx
, NULL
, &unused_buffer
);
1592 dma_unmap_single(ar
->dev
, resp_paddr
,
1593 *resp_len
, DMA_FROM_DEVICE
);
1596 dma_unmap_single(ar
->dev
, req_paddr
, req_len
, DMA_TO_DEVICE
);
1598 if (ret
== 0 && resp_len
) {
1599 *resp_len
= min(*resp_len
, xfer
.resp_len
);
1600 memcpy(resp
, tresp
, xfer
.resp_len
);
1609 static void ath10k_pci_bmi_send_done(struct ath10k_ce_pipe
*ce_state
)
1611 struct bmi_xfer
*xfer
;
1613 unsigned int nbytes
;
1614 unsigned int transfer_id
;
1616 if (ath10k_ce_completed_send_next(ce_state
, (void **)&xfer
, &ce_data
,
1617 &nbytes
, &transfer_id
))
1620 if (xfer
->wait_for_resp
)
1623 complete(&xfer
->done
);
1626 static void ath10k_pci_bmi_recv_data(struct ath10k_ce_pipe
*ce_state
)
1628 struct bmi_xfer
*xfer
;
1630 unsigned int nbytes
;
1631 unsigned int transfer_id
;
1634 if (ath10k_ce_completed_recv_next(ce_state
, (void **)&xfer
, &ce_data
,
1635 &nbytes
, &transfer_id
, &flags
))
1638 if (!xfer
->wait_for_resp
) {
1639 ath10k_warn("unexpected: BMI data received; ignoring\n");
1643 xfer
->resp_len
= nbytes
;
1644 complete(&xfer
->done
);
1647 static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe
*tx_pipe
,
1648 struct ath10k_ce_pipe
*rx_pipe
,
1649 struct bmi_xfer
*xfer
)
1651 unsigned long timeout
= jiffies
+ BMI_COMMUNICATION_TIMEOUT_HZ
;
1653 while (time_before_eq(jiffies
, timeout
)) {
1654 ath10k_pci_bmi_send_done(tx_pipe
);
1655 ath10k_pci_bmi_recv_data(rx_pipe
);
1657 if (completion_done(&xfer
->done
))
1667 * Map from service/endpoint to Copy Engine.
1668 * This table is derived from the CE_PCI TABLE, above.
1669 * It is passed to the Target at startup for use by firmware.
1671 static const struct service_to_pipe target_service_to_ce_map_wlan
[] = {
1673 ATH10K_HTC_SVC_ID_WMI_DATA_VO
,
1674 PIPEDIR_OUT
, /* out = UL = host -> target */
1678 ATH10K_HTC_SVC_ID_WMI_DATA_VO
,
1679 PIPEDIR_IN
, /* in = DL = target -> host */
1683 ATH10K_HTC_SVC_ID_WMI_DATA_BK
,
1684 PIPEDIR_OUT
, /* out = UL = host -> target */
1688 ATH10K_HTC_SVC_ID_WMI_DATA_BK
,
1689 PIPEDIR_IN
, /* in = DL = target -> host */
1693 ATH10K_HTC_SVC_ID_WMI_DATA_BE
,
1694 PIPEDIR_OUT
, /* out = UL = host -> target */
1698 ATH10K_HTC_SVC_ID_WMI_DATA_BE
,
1699 PIPEDIR_IN
, /* in = DL = target -> host */
1703 ATH10K_HTC_SVC_ID_WMI_DATA_VI
,
1704 PIPEDIR_OUT
, /* out = UL = host -> target */
1708 ATH10K_HTC_SVC_ID_WMI_DATA_VI
,
1709 PIPEDIR_IN
, /* in = DL = target -> host */
1713 ATH10K_HTC_SVC_ID_WMI_CONTROL
,
1714 PIPEDIR_OUT
, /* out = UL = host -> target */
1718 ATH10K_HTC_SVC_ID_WMI_CONTROL
,
1719 PIPEDIR_IN
, /* in = DL = target -> host */
1723 ATH10K_HTC_SVC_ID_RSVD_CTRL
,
1724 PIPEDIR_OUT
, /* out = UL = host -> target */
1725 0, /* could be moved to 3 (share with WMI) */
1728 ATH10K_HTC_SVC_ID_RSVD_CTRL
,
1729 PIPEDIR_IN
, /* in = DL = target -> host */
1733 ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS
, /* not currently used */
1734 PIPEDIR_OUT
, /* out = UL = host -> target */
1738 ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS
, /* not currently used */
1739 PIPEDIR_IN
, /* in = DL = target -> host */
1743 ATH10K_HTC_SVC_ID_HTT_DATA_MSG
,
1744 PIPEDIR_OUT
, /* out = UL = host -> target */
1748 ATH10K_HTC_SVC_ID_HTT_DATA_MSG
,
1749 PIPEDIR_IN
, /* in = DL = target -> host */
1753 /* (Additions here) */
1755 { /* Must be last */
1763 * Send an interrupt to the device to wake up the Target CPU
1764 * so it has an opportunity to notice any changed state.
1766 static int ath10k_pci_wake_target_cpu(struct ath10k
*ar
)
1771 ret
= ath10k_pci_diag_read_access(ar
, SOC_CORE_BASE_ADDRESS
|
1775 ath10k_warn("failed to read core_ctrl: %d\n", ret
);
1779 /* A_INUM_FIRMWARE interrupt to Target CPU */
1780 core_ctrl
|= CORE_CTRL_CPU_INTR_MASK
;
1782 ret
= ath10k_pci_diag_write_access(ar
, SOC_CORE_BASE_ADDRESS
|
1786 ath10k_warn("failed to set target CPU interrupt mask: %d\n",
1794 static int ath10k_pci_init_config(struct ath10k
*ar
)
1796 u32 interconnect_targ_addr
;
1797 u32 pcie_state_targ_addr
= 0;
1798 u32 pipe_cfg_targ_addr
= 0;
1799 u32 svc_to_pipe_map
= 0;
1800 u32 pcie_config_flags
= 0;
1802 u32 ealloc_targ_addr
;
1804 u32 flag2_targ_addr
;
1807 /* Download to Target the CE Config and the service-to-CE map */
1808 interconnect_targ_addr
=
1809 host_interest_item_address(HI_ITEM(hi_interconnect_state
));
1811 /* Supply Target-side CE configuration */
1812 ret
= ath10k_pci_diag_read_access(ar
, interconnect_targ_addr
,
1813 &pcie_state_targ_addr
);
1815 ath10k_err("Failed to get pcie state addr: %d\n", ret
);
1819 if (pcie_state_targ_addr
== 0) {
1821 ath10k_err("Invalid pcie state addr\n");
1825 ret
= ath10k_pci_diag_read_access(ar
, pcie_state_targ_addr
+
1826 offsetof(struct pcie_state
,
1828 &pipe_cfg_targ_addr
);
1830 ath10k_err("Failed to get pipe cfg addr: %d\n", ret
);
1834 if (pipe_cfg_targ_addr
== 0) {
1836 ath10k_err("Invalid pipe cfg addr\n");
1840 ret
= ath10k_pci_diag_write_mem(ar
, pipe_cfg_targ_addr
,
1841 target_ce_config_wlan
,
1842 sizeof(target_ce_config_wlan
));
1845 ath10k_err("Failed to write pipe cfg: %d\n", ret
);
1849 ret
= ath10k_pci_diag_read_access(ar
, pcie_state_targ_addr
+
1850 offsetof(struct pcie_state
,
1854 ath10k_err("Failed to get svc/pipe map: %d\n", ret
);
1858 if (svc_to_pipe_map
== 0) {
1860 ath10k_err("Invalid svc_to_pipe map\n");
1864 ret
= ath10k_pci_diag_write_mem(ar
, svc_to_pipe_map
,
1865 target_service_to_ce_map_wlan
,
1866 sizeof(target_service_to_ce_map_wlan
));
1868 ath10k_err("Failed to write svc/pipe map: %d\n", ret
);
1872 ret
= ath10k_pci_diag_read_access(ar
, pcie_state_targ_addr
+
1873 offsetof(struct pcie_state
,
1875 &pcie_config_flags
);
1877 ath10k_err("Failed to get pcie config_flags: %d\n", ret
);
1881 pcie_config_flags
&= ~PCIE_CONFIG_FLAG_ENABLE_L1
;
1883 ret
= ath10k_pci_diag_write_mem(ar
, pcie_state_targ_addr
+
1884 offsetof(struct pcie_state
, config_flags
),
1886 sizeof(pcie_config_flags
));
1888 ath10k_err("Failed to write pcie config_flags: %d\n", ret
);
1892 /* configure early allocation */
1893 ealloc_targ_addr
= host_interest_item_address(HI_ITEM(hi_early_alloc
));
1895 ret
= ath10k_pci_diag_read_access(ar
, ealloc_targ_addr
, &ealloc_value
);
1897 ath10k_err("Faile to get early alloc val: %d\n", ret
);
1901 /* first bank is switched to IRAM */
1902 ealloc_value
|= ((HI_EARLY_ALLOC_MAGIC
<< HI_EARLY_ALLOC_MAGIC_SHIFT
) &
1903 HI_EARLY_ALLOC_MAGIC_MASK
);
1904 ealloc_value
|= ((1 << HI_EARLY_ALLOC_IRAM_BANKS_SHIFT
) &
1905 HI_EARLY_ALLOC_IRAM_BANKS_MASK
);
1907 ret
= ath10k_pci_diag_write_access(ar
, ealloc_targ_addr
, ealloc_value
);
1909 ath10k_err("Failed to set early alloc val: %d\n", ret
);
1913 /* Tell Target to proceed with initialization */
1914 flag2_targ_addr
= host_interest_item_address(HI_ITEM(hi_option_flag2
));
1916 ret
= ath10k_pci_diag_read_access(ar
, flag2_targ_addr
, &flag2_value
);
1918 ath10k_err("Failed to get option val: %d\n", ret
);
1922 flag2_value
|= HI_OPTION_EARLY_CFG_DONE
;
1924 ret
= ath10k_pci_diag_write_access(ar
, flag2_targ_addr
, flag2_value
);
1926 ath10k_err("Failed to set option val: %d\n", ret
);
1935 static int ath10k_pci_ce_init(struct ath10k
*ar
)
1937 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
1938 struct ath10k_pci_pipe
*pipe_info
;
1939 const struct ce_attr
*attr
;
1942 for (pipe_num
= 0; pipe_num
< CE_COUNT
; pipe_num
++) {
1943 pipe_info
= &ar_pci
->pipe_info
[pipe_num
];
1944 pipe_info
->pipe_num
= pipe_num
;
1945 pipe_info
->hif_ce_state
= ar
;
1946 attr
= &host_ce_config_wlan
[pipe_num
];
1948 pipe_info
->ce_hdl
= ath10k_ce_init(ar
, pipe_num
, attr
);
1949 if (pipe_info
->ce_hdl
== NULL
) {
1950 ath10k_err("failed to initialize CE for pipe: %d\n",
1953 /* It is safe to call it here. It checks if ce_hdl is
1954 * valid for each pipe */
1955 ath10k_pci_ce_deinit(ar
);
1959 if (pipe_num
== CE_COUNT
- 1) {
1961 * Reserve the ultimate CE for
1962 * diagnostic Window support
1964 ar_pci
->ce_diag
= pipe_info
->ce_hdl
;
1968 pipe_info
->buf_sz
= (size_t) (attr
->src_sz_max
);
1974 static void ath10k_pci_fw_interrupt_handler(struct ath10k
*ar
)
1976 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
1977 u32 fw_indicator_address
, fw_indicator
;
1979 ath10k_pci_wake(ar
);
1981 fw_indicator_address
= ar_pci
->fw_indicator_address
;
1982 fw_indicator
= ath10k_pci_read32(ar
, fw_indicator_address
);
1984 if (fw_indicator
& FW_IND_EVENT_PENDING
) {
1985 /* ACK: clear Target-side pending event */
1986 ath10k_pci_write32(ar
, fw_indicator_address
,
1987 fw_indicator
& ~FW_IND_EVENT_PENDING
);
1989 if (ar_pci
->started
) {
1990 ath10k_pci_hif_dump_area(ar
);
1993 * Probable Target failure before we're prepared
1994 * to handle it. Generally unexpected.
1996 ath10k_warn("early firmware event indicated\n");
2000 ath10k_pci_sleep(ar
);
2003 static int ath10k_pci_warm_reset(struct ath10k
*ar
)
2005 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
2009 ath10k_dbg(ATH10K_DBG_BOOT
, "boot performing warm chip reset\n");
2011 ret
= ath10k_do_pci_wake(ar
);
2013 ath10k_err("failed to wake up target: %d\n", ret
);
2018 val
= ath10k_pci_read32(ar
, SOC_CORE_BASE_ADDRESS
+
2019 PCIE_INTR_CAUSE_ADDRESS
);
2020 ath10k_dbg(ATH10K_DBG_BOOT
, "boot host cpu intr cause: 0x%08x\n", val
);
2022 val
= ath10k_pci_read32(ar
, SOC_CORE_BASE_ADDRESS
+
2024 ath10k_dbg(ATH10K_DBG_BOOT
, "boot target cpu intr cause: 0x%08x\n",
2027 /* disable pending irqs */
2028 ath10k_pci_write32(ar
, SOC_CORE_BASE_ADDRESS
+
2029 PCIE_INTR_ENABLE_ADDRESS
, 0);
2031 ath10k_pci_write32(ar
, SOC_CORE_BASE_ADDRESS
+
2032 PCIE_INTR_CLR_ADDRESS
, ~0);
2036 /* clear fw indicator */
2037 ath10k_pci_write32(ar
, ar_pci
->fw_indicator_address
, 0);
2039 /* clear target LF timer interrupts */
2040 val
= ath10k_pci_read32(ar
, RTC_SOC_BASE_ADDRESS
+
2041 SOC_LF_TIMER_CONTROL0_ADDRESS
);
2042 ath10k_pci_write32(ar
, RTC_SOC_BASE_ADDRESS
+
2043 SOC_LF_TIMER_CONTROL0_ADDRESS
,
2044 val
& ~SOC_LF_TIMER_CONTROL0_ENABLE_MASK
);
2047 val
= ath10k_pci_read32(ar
, RTC_SOC_BASE_ADDRESS
+
2048 SOC_RESET_CONTROL_ADDRESS
);
2049 ath10k_pci_write32(ar
, RTC_SOC_BASE_ADDRESS
+ SOC_RESET_CONTROL_ADDRESS
,
2050 val
| SOC_RESET_CONTROL_CE_RST_MASK
);
2051 val
= ath10k_pci_read32(ar
, RTC_SOC_BASE_ADDRESS
+
2052 SOC_RESET_CONTROL_ADDRESS
);
2056 ath10k_pci_write32(ar
, RTC_SOC_BASE_ADDRESS
+ SOC_RESET_CONTROL_ADDRESS
,
2057 val
& ~SOC_RESET_CONTROL_CE_RST_MASK
);
2058 val
= ath10k_pci_read32(ar
, RTC_SOC_BASE_ADDRESS
+
2059 SOC_RESET_CONTROL_ADDRESS
);
2063 val
= ath10k_pci_read32(ar
, SOC_CORE_BASE_ADDRESS
+
2064 PCIE_INTR_CAUSE_ADDRESS
);
2065 ath10k_dbg(ATH10K_DBG_BOOT
, "boot host cpu intr cause: 0x%08x\n", val
);
2067 val
= ath10k_pci_read32(ar
, SOC_CORE_BASE_ADDRESS
+
2069 ath10k_dbg(ATH10K_DBG_BOOT
, "boot target cpu intr cause: 0x%08x\n",
2072 /* CPU warm reset */
2073 val
= ath10k_pci_read32(ar
, RTC_SOC_BASE_ADDRESS
+
2074 SOC_RESET_CONTROL_ADDRESS
);
2075 ath10k_pci_write32(ar
, RTC_SOC_BASE_ADDRESS
+ SOC_RESET_CONTROL_ADDRESS
,
2076 val
| SOC_RESET_CONTROL_CPU_WARM_RST_MASK
);
2078 val
= ath10k_pci_read32(ar
, RTC_SOC_BASE_ADDRESS
+
2079 SOC_RESET_CONTROL_ADDRESS
);
2080 ath10k_dbg(ATH10K_DBG_BOOT
, "boot target reset state: 0x%08x\n", val
);
2084 ath10k_dbg(ATH10K_DBG_BOOT
, "boot warm reset complete\n");
2086 ath10k_do_pci_sleep(ar
);
2090 static int __ath10k_pci_hif_power_up(struct ath10k
*ar
, bool cold_reset
)
2092 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
2093 const char *irq_mode
;
2097 * Bring the target up cleanly.
2099 * The target may be in an undefined state with an AUX-powered Target
2100 * and a Host in WoW mode. If the Host crashes, loses power, or is
2101 * restarted (without unloading the driver) then the Target is left
2102 * (aux) powered and running. On a subsequent driver load, the Target
2103 * is in an unexpected state. We try to catch that here in order to
2104 * reset the Target and retry the probe.
2107 ret
= ath10k_pci_cold_reset(ar
);
2109 ret
= ath10k_pci_warm_reset(ar
);
2112 ath10k_err("failed to reset target: %d\n", ret
);
2116 if (!test_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE
, ar_pci
->features
))
2117 /* Force AWAKE forever */
2118 ath10k_do_pci_wake(ar
);
2120 ret
= ath10k_pci_ce_init(ar
);
2122 ath10k_err("failed to initialize CE: %d\n", ret
);
2126 ret
= ath10k_ce_disable_interrupts(ar
);
2128 ath10k_err("failed to disable CE interrupts: %d\n", ret
);
2132 ret
= ath10k_pci_init_irq(ar
);
2134 ath10k_err("failed to init irqs: %d\n", ret
);
2138 ret
= ath10k_pci_request_early_irq(ar
);
2140 ath10k_err("failed to request early irq: %d\n", ret
);
2141 goto err_deinit_irq
;
2144 ret
= ath10k_pci_wait_for_target_init(ar
);
2146 ath10k_err("failed to wait for target to init: %d\n", ret
);
2147 goto err_free_early_irq
;
2150 ret
= ath10k_pci_init_config(ar
);
2152 ath10k_err("failed to setup init config: %d\n", ret
);
2153 goto err_free_early_irq
;
2156 ret
= ath10k_pci_wake_target_cpu(ar
);
2158 ath10k_err("could not wake up target CPU: %d\n", ret
);
2159 goto err_free_early_irq
;
2162 if (ar_pci
->num_msi_intrs
> 1)
2164 else if (ar_pci
->num_msi_intrs
== 1)
2167 irq_mode
= "legacy";
2169 if (!test_bit(ATH10K_FLAG_FIRST_BOOT_DONE
, &ar
->dev_flags
))
2170 ath10k_info("pci irq %s\n", irq_mode
);
2175 ath10k_pci_free_early_irq(ar
);
2177 ath10k_pci_deinit_irq(ar
);
2179 ath10k_pci_ce_deinit(ar
);
2180 ath10k_pci_warm_reset(ar
);
2182 if (!test_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE
, ar_pci
->features
))
2183 ath10k_do_pci_sleep(ar
);
2188 static int ath10k_pci_hif_power_up(struct ath10k
*ar
)
2193 * Hardware CUS232 version 2 has some issues with cold reset and the
2194 * preferred (and safer) way to perform a device reset is through a
2197 * Warm reset doesn't always work though (notably after a firmware
2198 * crash) so fall back to cold reset if necessary.
2200 ret
= __ath10k_pci_hif_power_up(ar
, false);
2202 ath10k_warn("failed to power up target using warm reset (%d), trying cold reset\n",
2205 ret
= __ath10k_pci_hif_power_up(ar
, true);
2207 ath10k_err("failed to power up target using cold reset too (%d)\n",
2216 static void ath10k_pci_hif_power_down(struct ath10k
*ar
)
2218 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
2220 ath10k_pci_free_early_irq(ar
);
2221 ath10k_pci_kill_tasklet(ar
);
2222 ath10k_pci_deinit_irq(ar
);
2223 ath10k_pci_warm_reset(ar
);
2225 ath10k_pci_ce_deinit(ar
);
2226 if (!test_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE
, ar_pci
->features
))
2227 ath10k_do_pci_sleep(ar
);
2232 #define ATH10K_PCI_PM_CONTROL 0x44
2234 static int ath10k_pci_hif_suspend(struct ath10k
*ar
)
2236 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
2237 struct pci_dev
*pdev
= ar_pci
->pdev
;
2240 pci_read_config_dword(pdev
, ATH10K_PCI_PM_CONTROL
, &val
);
2242 if ((val
& 0x000000ff) != 0x3) {
2243 pci_save_state(pdev
);
2244 pci_disable_device(pdev
);
2245 pci_write_config_dword(pdev
, ATH10K_PCI_PM_CONTROL
,
2246 (val
& 0xffffff00) | 0x03);
2252 static int ath10k_pci_hif_resume(struct ath10k
*ar
)
2254 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
2255 struct pci_dev
*pdev
= ar_pci
->pdev
;
2258 pci_read_config_dword(pdev
, ATH10K_PCI_PM_CONTROL
, &val
);
2260 if ((val
& 0x000000ff) != 0) {
2261 pci_restore_state(pdev
);
2262 pci_write_config_dword(pdev
, ATH10K_PCI_PM_CONTROL
,
2265 * Suspend/Resume resets the PCI configuration space,
2266 * so we have to re-disable the RETRY_TIMEOUT register (0x41)
2267 * to keep PCI Tx retries from interfering with C3 CPU state
2269 pci_read_config_dword(pdev
, 0x40, &val
);
2271 if ((val
& 0x0000ff00) != 0)
2272 pci_write_config_dword(pdev
, 0x40, val
& 0xffff00ff);
2279 static const struct ath10k_hif_ops ath10k_pci_hif_ops
= {
2280 .tx_sg
= ath10k_pci_hif_tx_sg
,
2281 .exchange_bmi_msg
= ath10k_pci_hif_exchange_bmi_msg
,
2282 .start
= ath10k_pci_hif_start
,
2283 .stop
= ath10k_pci_hif_stop
,
2284 .map_service_to_pipe
= ath10k_pci_hif_map_service_to_pipe
,
2285 .get_default_pipe
= ath10k_pci_hif_get_default_pipe
,
2286 .send_complete_check
= ath10k_pci_hif_send_complete_check
,
2287 .set_callbacks
= ath10k_pci_hif_set_callbacks
,
2288 .get_free_queue_number
= ath10k_pci_hif_get_free_queue_number
,
2289 .power_up
= ath10k_pci_hif_power_up
,
2290 .power_down
= ath10k_pci_hif_power_down
,
2292 .suspend
= ath10k_pci_hif_suspend
,
2293 .resume
= ath10k_pci_hif_resume
,
2297 static void ath10k_pci_ce_tasklet(unsigned long ptr
)
2299 struct ath10k_pci_pipe
*pipe
= (struct ath10k_pci_pipe
*)ptr
;
2300 struct ath10k_pci
*ar_pci
= pipe
->ar_pci
;
2302 ath10k_ce_per_engine_service(ar_pci
->ar
, pipe
->pipe_num
);
2305 static void ath10k_msi_err_tasklet(unsigned long data
)
2307 struct ath10k
*ar
= (struct ath10k
*)data
;
2309 ath10k_pci_fw_interrupt_handler(ar
);
2313 * Handler for a per-engine interrupt on a PARTICULAR CE.
2314 * This is used in cases where each CE has a private MSI interrupt.
2316 static irqreturn_t
ath10k_pci_per_engine_handler(int irq
, void *arg
)
2318 struct ath10k
*ar
= arg
;
2319 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
2320 int ce_id
= irq
- ar_pci
->pdev
->irq
- MSI_ASSIGN_CE_INITIAL
;
2322 if (ce_id
< 0 || ce_id
>= ARRAY_SIZE(ar_pci
->pipe_info
)) {
2323 ath10k_warn("unexpected/invalid irq %d ce_id %d\n", irq
, ce_id
);
2328 * NOTE: We are able to derive ce_id from irq because we
2329 * use a one-to-one mapping for CE's 0..5.
2330 * CE's 6 & 7 do not use interrupts at all.
2332 * This mapping must be kept in sync with the mapping
2335 tasklet_schedule(&ar_pci
->pipe_info
[ce_id
].intr
);
2339 static irqreturn_t
ath10k_pci_msi_fw_handler(int irq
, void *arg
)
2341 struct ath10k
*ar
= arg
;
2342 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
2344 tasklet_schedule(&ar_pci
->msi_fw_err
);
2349 * Top-level interrupt handler for all PCI interrupts from a Target.
2350 * When a block of MSI interrupts is allocated, this top-level handler
2351 * is not used; instead, we directly call the correct sub-handler.
2353 static irqreturn_t
ath10k_pci_interrupt_handler(int irq
, void *arg
)
2355 struct ath10k
*ar
= arg
;
2356 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
2358 if (ar_pci
->num_msi_intrs
== 0) {
2359 if (!ath10k_pci_irq_pending(ar
))
2362 ath10k_pci_disable_and_clear_legacy_irq(ar
);
2365 tasklet_schedule(&ar_pci
->intr_tq
);
2370 static void ath10k_pci_early_irq_tasklet(unsigned long data
)
2372 struct ath10k
*ar
= (struct ath10k
*)data
;
2373 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
2377 ret
= ath10k_pci_wake(ar
);
2379 ath10k_warn("failed to wake target in early irq tasklet: %d\n",
2384 fw_ind
= ath10k_pci_read32(ar
, ar_pci
->fw_indicator_address
);
2385 if (fw_ind
& FW_IND_EVENT_PENDING
) {
2386 ath10k_pci_write32(ar
, ar_pci
->fw_indicator_address
,
2387 fw_ind
& ~FW_IND_EVENT_PENDING
);
2389 /* Some structures are unavailable during early boot or at
2390 * driver teardown so just print that the device has crashed. */
2391 ath10k_warn("device crashed - no diagnostics available\n");
2394 ath10k_pci_sleep(ar
);
2395 ath10k_pci_enable_legacy_irq(ar
);
2398 static void ath10k_pci_tasklet(unsigned long data
)
2400 struct ath10k
*ar
= (struct ath10k
*)data
;
2401 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
2403 ath10k_pci_fw_interrupt_handler(ar
); /* FIXME: Handle FW error */
2404 ath10k_ce_per_engine_service_any(ar
);
2406 /* Re-enable legacy irq that was disabled in the irq handler */
2407 if (ar_pci
->num_msi_intrs
== 0)
2408 ath10k_pci_enable_legacy_irq(ar
);
2411 static int ath10k_pci_request_irq_msix(struct ath10k
*ar
)
2413 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
2416 ret
= request_irq(ar_pci
->pdev
->irq
+ MSI_ASSIGN_FW
,
2417 ath10k_pci_msi_fw_handler
,
2418 IRQF_SHARED
, "ath10k_pci", ar
);
2420 ath10k_warn("failed to request MSI-X fw irq %d: %d\n",
2421 ar_pci
->pdev
->irq
+ MSI_ASSIGN_FW
, ret
);
2425 for (i
= MSI_ASSIGN_CE_INITIAL
; i
<= MSI_ASSIGN_CE_MAX
; i
++) {
2426 ret
= request_irq(ar_pci
->pdev
->irq
+ i
,
2427 ath10k_pci_per_engine_handler
,
2428 IRQF_SHARED
, "ath10k_pci", ar
);
2430 ath10k_warn("failed to request MSI-X ce irq %d: %d\n",
2431 ar_pci
->pdev
->irq
+ i
, ret
);
2433 for (i
--; i
>= MSI_ASSIGN_CE_INITIAL
; i
--)
2434 free_irq(ar_pci
->pdev
->irq
+ i
, ar
);
2436 free_irq(ar_pci
->pdev
->irq
+ MSI_ASSIGN_FW
, ar
);
2444 static int ath10k_pci_request_irq_msi(struct ath10k
*ar
)
2446 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
2449 ret
= request_irq(ar_pci
->pdev
->irq
,
2450 ath10k_pci_interrupt_handler
,
2451 IRQF_SHARED
, "ath10k_pci", ar
);
2453 ath10k_warn("failed to request MSI irq %d: %d\n",
2454 ar_pci
->pdev
->irq
, ret
);
2461 static int ath10k_pci_request_irq_legacy(struct ath10k
*ar
)
2463 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
2466 ret
= request_irq(ar_pci
->pdev
->irq
,
2467 ath10k_pci_interrupt_handler
,
2468 IRQF_SHARED
, "ath10k_pci", ar
);
2470 ath10k_warn("failed to request legacy irq %d: %d\n",
2471 ar_pci
->pdev
->irq
, ret
);
2478 static int ath10k_pci_request_irq(struct ath10k
*ar
)
2480 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
2482 switch (ar_pci
->num_msi_intrs
) {
2484 return ath10k_pci_request_irq_legacy(ar
);
2486 return ath10k_pci_request_irq_msi(ar
);
2487 case MSI_NUM_REQUEST
:
2488 return ath10k_pci_request_irq_msix(ar
);
2491 ath10k_warn("unknown irq configuration upon request\n");
2495 static void ath10k_pci_free_irq(struct ath10k
*ar
)
2497 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
2500 /* There's at least one interrupt irregardless whether its legacy INTR
2501 * or MSI or MSI-X */
2502 for (i
= 0; i
< max(1, ar_pci
->num_msi_intrs
); i
++)
2503 free_irq(ar_pci
->pdev
->irq
+ i
, ar
);
2506 static void ath10k_pci_init_irq_tasklets(struct ath10k
*ar
)
2508 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
2511 tasklet_init(&ar_pci
->intr_tq
, ath10k_pci_tasklet
, (unsigned long)ar
);
2512 tasklet_init(&ar_pci
->msi_fw_err
, ath10k_msi_err_tasklet
,
2514 tasklet_init(&ar_pci
->early_irq_tasklet
, ath10k_pci_early_irq_tasklet
,
2517 for (i
= 0; i
< CE_COUNT
; i
++) {
2518 ar_pci
->pipe_info
[i
].ar_pci
= ar_pci
;
2519 tasklet_init(&ar_pci
->pipe_info
[i
].intr
, ath10k_pci_ce_tasklet
,
2520 (unsigned long)&ar_pci
->pipe_info
[i
]);
2524 static int ath10k_pci_init_irq(struct ath10k
*ar
)
2526 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
2527 bool msix_supported
= test_bit(ATH10K_PCI_FEATURE_MSI_X
,
2531 ath10k_pci_init_irq_tasklets(ar
);
2533 if (ath10k_pci_irq_mode
!= ATH10K_PCI_IRQ_AUTO
&&
2534 !test_bit(ATH10K_FLAG_FIRST_BOOT_DONE
, &ar
->dev_flags
))
2535 ath10k_info("limiting irq mode to: %d\n", ath10k_pci_irq_mode
);
2538 if (ath10k_pci_irq_mode
== ATH10K_PCI_IRQ_AUTO
&& msix_supported
) {
2539 ar_pci
->num_msi_intrs
= MSI_NUM_REQUEST
;
2540 ret
= pci_enable_msi_range(ar_pci
->pdev
, ar_pci
->num_msi_intrs
,
2541 ar_pci
->num_msi_intrs
);
2549 if (ath10k_pci_irq_mode
!= ATH10K_PCI_IRQ_LEGACY
) {
2550 ar_pci
->num_msi_intrs
= 1;
2551 ret
= pci_enable_msi(ar_pci
->pdev
);
2560 * A potential race occurs here: The CORE_BASE write
2561 * depends on target correctly decoding AXI address but
2562 * host won't know when target writes BAR to CORE_CTRL.
2563 * This write might get lost if target has NOT written BAR.
2564 * For now, fix the race by repeating the write in below
2565 * synchronization checking. */
2566 ar_pci
->num_msi_intrs
= 0;
2568 ret
= ath10k_pci_wake(ar
);
2570 ath10k_warn("failed to wake target: %d\n", ret
);
2574 ath10k_pci_write32(ar
, SOC_CORE_BASE_ADDRESS
+ PCIE_INTR_ENABLE_ADDRESS
,
2575 PCIE_INTR_FIRMWARE_MASK
| PCIE_INTR_CE_MASK_ALL
);
2576 ath10k_pci_sleep(ar
);
2581 static int ath10k_pci_deinit_irq_legacy(struct ath10k
*ar
)
2585 ret
= ath10k_pci_wake(ar
);
2587 ath10k_warn("failed to wake target: %d\n", ret
);
2591 ath10k_pci_write32(ar
, SOC_CORE_BASE_ADDRESS
+ PCIE_INTR_ENABLE_ADDRESS
,
2593 ath10k_pci_sleep(ar
);
2598 static int ath10k_pci_deinit_irq(struct ath10k
*ar
)
2600 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
2602 switch (ar_pci
->num_msi_intrs
) {
2604 return ath10k_pci_deinit_irq_legacy(ar
);
2607 case MSI_NUM_REQUEST
:
2608 pci_disable_msi(ar_pci
->pdev
);
2611 pci_disable_msi(ar_pci
->pdev
);
2614 ath10k_warn("unknown irq configuration upon deinit\n");
2618 static int ath10k_pci_wait_for_target_init(struct ath10k
*ar
)
2620 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
2621 int wait_limit
= 300; /* 3 sec */
2624 ret
= ath10k_pci_wake(ar
);
2626 ath10k_err("failed to wake up target: %d\n", ret
);
2630 while (wait_limit
-- &&
2631 !(ioread32(ar_pci
->mem
+ FW_INDICATOR_ADDRESS
) &
2632 FW_IND_INITIALIZED
)) {
2633 if (ar_pci
->num_msi_intrs
== 0)
2634 /* Fix potential race by repeating CORE_BASE writes */
2635 iowrite32(PCIE_INTR_FIRMWARE_MASK
|
2636 PCIE_INTR_CE_MASK_ALL
,
2637 ar_pci
->mem
+ (SOC_CORE_BASE_ADDRESS
|
2638 PCIE_INTR_ENABLE_ADDRESS
));
2642 if (wait_limit
< 0) {
2643 ath10k_err("target stalled\n");
2649 ath10k_pci_sleep(ar
);
2653 static int ath10k_pci_cold_reset(struct ath10k
*ar
)
2658 ret
= ath10k_do_pci_wake(ar
);
2660 ath10k_err("failed to wake up target: %d\n",
2665 /* Put Target, including PCIe, into RESET. */
2666 val
= ath10k_pci_reg_read32(ar
, SOC_GLOBAL_RESET_ADDRESS
);
2668 ath10k_pci_reg_write32(ar
, SOC_GLOBAL_RESET_ADDRESS
, val
);
2670 for (i
= 0; i
< ATH_PCI_RESET_WAIT_MAX
; i
++) {
2671 if (ath10k_pci_reg_read32(ar
, RTC_STATE_ADDRESS
) &
2672 RTC_STATE_COLD_RESET_MASK
)
2677 /* Pull Target, including PCIe, out of RESET. */
2679 ath10k_pci_reg_write32(ar
, SOC_GLOBAL_RESET_ADDRESS
, val
);
2681 for (i
= 0; i
< ATH_PCI_RESET_WAIT_MAX
; i
++) {
2682 if (!(ath10k_pci_reg_read32(ar
, RTC_STATE_ADDRESS
) &
2683 RTC_STATE_COLD_RESET_MASK
))
2688 ath10k_do_pci_sleep(ar
);
2692 static void ath10k_pci_dump_features(struct ath10k_pci
*ar_pci
)
2696 for (i
= 0; i
< ATH10K_PCI_FEATURE_COUNT
; i
++) {
2697 if (!test_bit(i
, ar_pci
->features
))
2701 case ATH10K_PCI_FEATURE_MSI_X
:
2702 ath10k_dbg(ATH10K_DBG_BOOT
, "device supports MSI-X\n");
2704 case ATH10K_PCI_FEATURE_SOC_POWER_SAVE
:
2705 ath10k_dbg(ATH10K_DBG_BOOT
, "QCA98XX SoC power save enabled\n");
2711 static int ath10k_pci_probe(struct pci_dev
*pdev
,
2712 const struct pci_device_id
*pci_dev
)
2717 struct ath10k_pci
*ar_pci
;
2718 u32 lcr_val
, chip_id
;
2720 ath10k_dbg(ATH10K_DBG_PCI
, "%s\n", __func__
);
2722 ar_pci
= kzalloc(sizeof(*ar_pci
), GFP_KERNEL
);
2726 ar_pci
->pdev
= pdev
;
2727 ar_pci
->dev
= &pdev
->dev
;
2729 switch (pci_dev
->device
) {
2730 case QCA988X_2_0_DEVICE_ID
:
2731 set_bit(ATH10K_PCI_FEATURE_MSI_X
, ar_pci
->features
);
2735 ath10k_err("Unknown device ID: %d\n", pci_dev
->device
);
2739 if (ath10k_target_ps
)
2740 set_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE
, ar_pci
->features
);
2742 ath10k_pci_dump_features(ar_pci
);
2744 ar
= ath10k_core_create(ar_pci
, ar_pci
->dev
, &ath10k_pci_hif_ops
);
2746 ath10k_err("failed to create driver core\n");
2752 ar_pci
->fw_indicator_address
= FW_INDICATOR_ADDRESS
;
2753 atomic_set(&ar_pci
->keep_awake_count
, 0);
2755 pci_set_drvdata(pdev
, ar
);
2758 * Without any knowledge of the Host, the Target may have been reset or
2759 * power cycled and its Config Space may no longer reflect the PCI
2760 * address space that was assigned earlier by the PCI infrastructure.
2763 ret
= pci_assign_resource(pdev
, BAR_NUM
);
2765 ath10k_err("failed to assign PCI space: %d\n", ret
);
2769 ret
= pci_enable_device(pdev
);
2771 ath10k_err("failed to enable PCI device: %d\n", ret
);
2775 /* Request MMIO resources */
2776 ret
= pci_request_region(pdev
, BAR_NUM
, "ath");
2778 ath10k_err("failed to request MMIO region: %d\n", ret
);
2783 * Target structures have a limit of 32 bit DMA pointers.
2784 * DMA pointers can be wider than 32 bits by default on some systems.
2786 ret
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(32));
2788 ath10k_err("failed to set DMA mask to 32-bit: %d\n", ret
);
2792 ret
= pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(32));
2794 ath10k_err("failed to set consistent DMA mask to 32-bit\n");
2798 /* Set bus master bit in PCI_COMMAND to enable DMA */
2799 pci_set_master(pdev
);
2802 * Temporary FIX: disable ASPM
2803 * Will be removed after the OTP is programmed
2805 pci_read_config_dword(pdev
, 0x80, &lcr_val
);
2806 pci_write_config_dword(pdev
, 0x80, (lcr_val
& 0xffffff00));
2808 /* Arrange for access to Target SoC registers. */
2809 mem
= pci_iomap(pdev
, BAR_NUM
, 0);
2811 ath10k_err("failed to perform IOMAP for BAR%d\n", BAR_NUM
);
2818 spin_lock_init(&ar_pci
->ce_lock
);
2820 ret
= ath10k_do_pci_wake(ar
);
2822 ath10k_err("Failed to get chip id: %d\n", ret
);
2826 chip_id
= ath10k_pci_soc_read32(ar
, SOC_CHIP_ID_ADDRESS
);
2828 ath10k_do_pci_sleep(ar
);
2830 ath10k_dbg(ATH10K_DBG_BOOT
, "boot pci_mem 0x%p\n", ar_pci
->mem
);
2832 ret
= ath10k_core_register(ar
, chip_id
);
2834 ath10k_err("failed to register driver core: %d\n", ret
);
2841 pci_iounmap(pdev
, mem
);
2843 pci_clear_master(pdev
);
2845 pci_release_region(pdev
, BAR_NUM
);
2847 pci_disable_device(pdev
);
2849 ath10k_core_destroy(ar
);
2851 /* call HIF PCI free here */
2857 static void ath10k_pci_remove(struct pci_dev
*pdev
)
2859 struct ath10k
*ar
= pci_get_drvdata(pdev
);
2860 struct ath10k_pci
*ar_pci
;
2862 ath10k_dbg(ATH10K_DBG_PCI
, "%s\n", __func__
);
2867 ar_pci
= ath10k_pci_priv(ar
);
2872 tasklet_kill(&ar_pci
->msi_fw_err
);
2874 ath10k_core_unregister(ar
);
2876 pci_iounmap(pdev
, ar_pci
->mem
);
2877 pci_release_region(pdev
, BAR_NUM
);
2878 pci_clear_master(pdev
);
2879 pci_disable_device(pdev
);
2881 ath10k_core_destroy(ar
);
2885 MODULE_DEVICE_TABLE(pci
, ath10k_pci_id_table
);
2887 static struct pci_driver ath10k_pci_driver
= {
2888 .name
= "ath10k_pci",
2889 .id_table
= ath10k_pci_id_table
,
2890 .probe
= ath10k_pci_probe
,
2891 .remove
= ath10k_pci_remove
,
2894 static int __init
ath10k_pci_init(void)
2898 ret
= pci_register_driver(&ath10k_pci_driver
);
2900 ath10k_err("failed to register PCI driver: %d\n", ret
);
2904 module_init(ath10k_pci_init
);
2906 static void __exit
ath10k_pci_exit(void)
2908 pci_unregister_driver(&ath10k_pci_driver
);
2911 module_exit(ath10k_pci_exit
);
2913 MODULE_AUTHOR("Qualcomm Atheros");
2914 MODULE_DESCRIPTION("Driver support for Atheros QCA988X PCIe devices");
2915 MODULE_LICENSE("Dual BSD/GPL");
2916 MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR
"/" QCA988X_HW_2_0_FW_FILE
);
2917 MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR
"/" QCA988X_HW_2_0_OTP_FILE
);
2918 MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR
"/" QCA988X_HW_2_0_BOARD_DATA_FILE
);