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 enum ath10k_pci_reset_mode {
43 ATH10K_PCI_RESET_AUTO = 0,
44 ATH10K_PCI_RESET_WARM_ONLY = 1,
47 static unsigned int ath10k_pci_irq_mode = ATH10K_PCI_IRQ_AUTO;
48 static unsigned int ath10k_pci_reset_mode = ATH10K_PCI_RESET_AUTO;
50 module_param_named(irq_mode, ath10k_pci_irq_mode, uint, 0644);
51 MODULE_PARM_DESC(irq_mode, "0: auto, 1: legacy, 2: msi (default: 0)");
53 module_param_named(reset_mode, ath10k_pci_reset_mode, uint, 0644);
54 MODULE_PARM_DESC(reset_mode, "0: auto, 1: warm only (default: 0)");
56 /* how long wait to wait for target to initialise, in ms */
57 #define ATH10K_PCI_TARGET_WAIT 3000
58 #define ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS 3
60 #define QCA988X_2_0_DEVICE_ID (0x003c)
61 #define QCA6174_2_1_DEVICE_ID (0x003e)
63 static const struct pci_device_id ath10k_pci_id_table[] = {
64 { PCI_VDEVICE(ATHEROS, QCA988X_2_0_DEVICE_ID) }, /* PCI-E QCA988X V2 */
65 { PCI_VDEVICE(ATHEROS, QCA6174_2_1_DEVICE_ID) }, /* PCI-E QCA6174 V2.1 */
69 static const struct ath10k_pci_supp_chip ath10k_pci_supp_chips[] = {
70 /* QCA988X pre 2.0 chips are not supported because they need some nasty
71 * hacks. ath10k doesn't have them and these devices crash horribly
74 { QCA988X_2_0_DEVICE_ID, QCA988X_HW_2_0_CHIP_ID_REV },
75 { QCA6174_2_1_DEVICE_ID, QCA6174_HW_2_1_CHIP_ID_REV },
76 { QCA6174_2_1_DEVICE_ID, QCA6174_HW_2_2_CHIP_ID_REV },
77 { QCA6174_2_1_DEVICE_ID, QCA6174_HW_3_0_CHIP_ID_REV },
78 { QCA6174_2_1_DEVICE_ID, QCA6174_HW_3_1_CHIP_ID_REV },
79 { QCA6174_2_1_DEVICE_ID, QCA6174_HW_3_2_CHIP_ID_REV },
82 static void ath10k_pci_buffer_cleanup(struct ath10k *ar);
83 static int ath10k_pci_cold_reset(struct ath10k *ar);
84 static int ath10k_pci_warm_reset(struct ath10k *ar);
85 static int ath10k_pci_wait_for_target_init(struct ath10k *ar);
86 static int ath10k_pci_init_irq(struct ath10k *ar);
87 static int ath10k_pci_deinit_irq(struct ath10k *ar);
88 static int ath10k_pci_request_irq(struct ath10k *ar);
89 static void ath10k_pci_free_irq(struct ath10k *ar);
90 static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
91 struct ath10k_ce_pipe *rx_pipe,
92 struct bmi_xfer *xfer);
94 static const struct ce_attr host_ce_config_wlan[] = {
95 /* CE0: host->target HTC control and raw streams */
97 .flags = CE_ATTR_FLAGS,
103 /* CE1: target->host HTT + HTC control */
105 .flags = CE_ATTR_FLAGS,
108 .dest_nentries = 512,
111 /* CE2: target->host WMI */
113 .flags = CE_ATTR_FLAGS,
116 .dest_nentries = 128,
119 /* CE3: host->target WMI */
121 .flags = CE_ATTR_FLAGS,
127 /* CE4: host->target HTT */
129 .flags = CE_ATTR_FLAGS | CE_ATTR_DIS_INTR,
130 .src_nentries = CE_HTT_H2T_MSG_SRC_NENTRIES,
137 .flags = CE_ATTR_FLAGS,
143 /* CE6: target autonomous hif_memcpy */
145 .flags = CE_ATTR_FLAGS,
151 /* CE7: ce_diag, the Diagnostic Window */
153 .flags = CE_ATTR_FLAGS,
155 .src_sz_max = DIAG_TRANSFER_LIMIT,
160 /* Target firmware's Copy Engine configuration. */
161 static const struct ce_pipe_config target_ce_config_wlan[] = {
162 /* CE0: host->target HTC control and raw streams */
164 .pipenum = __cpu_to_le32(0),
165 .pipedir = __cpu_to_le32(PIPEDIR_OUT),
166 .nentries = __cpu_to_le32(32),
167 .nbytes_max = __cpu_to_le32(256),
168 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
169 .reserved = __cpu_to_le32(0),
172 /* CE1: target->host HTT + HTC control */
174 .pipenum = __cpu_to_le32(1),
175 .pipedir = __cpu_to_le32(PIPEDIR_IN),
176 .nentries = __cpu_to_le32(32),
177 .nbytes_max = __cpu_to_le32(2048),
178 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
179 .reserved = __cpu_to_le32(0),
182 /* CE2: target->host WMI */
184 .pipenum = __cpu_to_le32(2),
185 .pipedir = __cpu_to_le32(PIPEDIR_IN),
186 .nentries = __cpu_to_le32(64),
187 .nbytes_max = __cpu_to_le32(2048),
188 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
189 .reserved = __cpu_to_le32(0),
192 /* CE3: host->target WMI */
194 .pipenum = __cpu_to_le32(3),
195 .pipedir = __cpu_to_le32(PIPEDIR_OUT),
196 .nentries = __cpu_to_le32(32),
197 .nbytes_max = __cpu_to_le32(2048),
198 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
199 .reserved = __cpu_to_le32(0),
202 /* CE4: host->target HTT */
204 .pipenum = __cpu_to_le32(4),
205 .pipedir = __cpu_to_le32(PIPEDIR_OUT),
206 .nentries = __cpu_to_le32(256),
207 .nbytes_max = __cpu_to_le32(256),
208 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
209 .reserved = __cpu_to_le32(0),
212 /* NB: 50% of src nentries, since tx has 2 frags */
216 .pipenum = __cpu_to_le32(5),
217 .pipedir = __cpu_to_le32(PIPEDIR_OUT),
218 .nentries = __cpu_to_le32(32),
219 .nbytes_max = __cpu_to_le32(2048),
220 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
221 .reserved = __cpu_to_le32(0),
224 /* CE6: Reserved for target autonomous hif_memcpy */
226 .pipenum = __cpu_to_le32(6),
227 .pipedir = __cpu_to_le32(PIPEDIR_INOUT),
228 .nentries = __cpu_to_le32(32),
229 .nbytes_max = __cpu_to_le32(4096),
230 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
231 .reserved = __cpu_to_le32(0),
234 /* CE7 used only by Host */
238 * Map from service/endpoint to Copy Engine.
239 * This table is derived from the CE_PCI TABLE, above.
240 * It is passed to the Target at startup for use by firmware.
242 static const struct service_to_pipe target_service_to_ce_map_wlan[] = {
244 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VO),
245 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
249 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VO),
250 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
254 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BK),
255 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
259 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BK),
260 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
264 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BE),
265 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
269 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BE),
270 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
274 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VI),
275 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
279 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VI),
280 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
284 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_CONTROL),
285 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
289 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_CONTROL),
290 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
294 __cpu_to_le32(ATH10K_HTC_SVC_ID_RSVD_CTRL),
295 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
299 __cpu_to_le32(ATH10K_HTC_SVC_ID_RSVD_CTRL),
300 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
304 __cpu_to_le32(ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS),
305 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
309 __cpu_to_le32(ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS),
310 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
314 __cpu_to_le32(ATH10K_HTC_SVC_ID_HTT_DATA_MSG),
315 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
319 __cpu_to_le32(ATH10K_HTC_SVC_ID_HTT_DATA_MSG),
320 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
324 /* (Additions here) */
333 static bool ath10k_pci_is_awake(struct ath10k *ar)
335 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
336 u32 val = ioread32(ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
339 return RTC_STATE_V_GET(val) == RTC_STATE_V_ON;
342 static void __ath10k_pci_wake(struct ath10k *ar)
344 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
346 lockdep_assert_held(&ar_pci->ps_lock);
348 ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps wake reg refcount %lu awake %d\n",
349 ar_pci->ps_wake_refcount, ar_pci->ps_awake);
351 iowrite32(PCIE_SOC_WAKE_V_MASK,
352 ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
353 PCIE_SOC_WAKE_ADDRESS);
356 static void __ath10k_pci_sleep(struct ath10k *ar)
358 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
360 lockdep_assert_held(&ar_pci->ps_lock);
362 ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps sleep reg refcount %lu awake %d\n",
363 ar_pci->ps_wake_refcount, ar_pci->ps_awake);
365 iowrite32(PCIE_SOC_WAKE_RESET,
366 ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
367 PCIE_SOC_WAKE_ADDRESS);
368 ar_pci->ps_awake = false;
371 static int ath10k_pci_wake_wait(struct ath10k *ar)
376 while (tot_delay < PCIE_WAKE_TIMEOUT) {
377 if (ath10k_pci_is_awake(ar))
381 tot_delay += curr_delay;
390 static int ath10k_pci_wake(struct ath10k *ar)
392 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
396 spin_lock_irqsave(&ar_pci->ps_lock, flags);
398 ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps wake refcount %lu awake %d\n",
399 ar_pci->ps_wake_refcount, ar_pci->ps_awake);
401 /* This function can be called very frequently. To avoid excessive
402 * CPU stalls for MMIO reads use a cache var to hold the device state.
404 if (!ar_pci->ps_awake) {
405 __ath10k_pci_wake(ar);
407 ret = ath10k_pci_wake_wait(ar);
409 ar_pci->ps_awake = true;
413 ar_pci->ps_wake_refcount++;
414 WARN_ON(ar_pci->ps_wake_refcount == 0);
417 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
422 static void ath10k_pci_sleep(struct ath10k *ar)
424 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
427 spin_lock_irqsave(&ar_pci->ps_lock, flags);
429 ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps sleep refcount %lu awake %d\n",
430 ar_pci->ps_wake_refcount, ar_pci->ps_awake);
432 if (WARN_ON(ar_pci->ps_wake_refcount == 0))
435 ar_pci->ps_wake_refcount--;
437 mod_timer(&ar_pci->ps_timer, jiffies +
438 msecs_to_jiffies(ATH10K_PCI_SLEEP_GRACE_PERIOD_MSEC));
441 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
444 static void ath10k_pci_ps_timer(unsigned long ptr)
446 struct ath10k *ar = (void *)ptr;
447 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
450 spin_lock_irqsave(&ar_pci->ps_lock, flags);
452 ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps timer refcount %lu awake %d\n",
453 ar_pci->ps_wake_refcount, ar_pci->ps_awake);
455 if (ar_pci->ps_wake_refcount > 0)
458 __ath10k_pci_sleep(ar);
461 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
464 static void ath10k_pci_sleep_sync(struct ath10k *ar)
466 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
469 del_timer_sync(&ar_pci->ps_timer);
471 spin_lock_irqsave(&ar_pci->ps_lock, flags);
472 WARN_ON(ar_pci->ps_wake_refcount > 0);
473 __ath10k_pci_sleep(ar);
474 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
477 void ath10k_pci_write32(struct ath10k *ar, u32 offset, u32 value)
479 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
482 ret = ath10k_pci_wake(ar);
484 ath10k_warn(ar, "failed to wake target for write32 of 0x%08x at 0x%08x: %d\n",
489 iowrite32(value, ar_pci->mem + offset);
490 ath10k_pci_sleep(ar);
493 u32 ath10k_pci_read32(struct ath10k *ar, u32 offset)
495 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
499 ret = ath10k_pci_wake(ar);
501 ath10k_warn(ar, "failed to wake target for read32 at 0x%08x: %d\n",
506 val = ioread32(ar_pci->mem + offset);
507 ath10k_pci_sleep(ar);
512 u32 ath10k_pci_soc_read32(struct ath10k *ar, u32 addr)
514 return ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS + addr);
517 void ath10k_pci_soc_write32(struct ath10k *ar, u32 addr, u32 val)
519 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + addr, val);
522 u32 ath10k_pci_reg_read32(struct ath10k *ar, u32 addr)
524 return ath10k_pci_read32(ar, PCIE_LOCAL_BASE_ADDRESS + addr);
527 void ath10k_pci_reg_write32(struct ath10k *ar, u32 addr, u32 val)
529 ath10k_pci_write32(ar, PCIE_LOCAL_BASE_ADDRESS + addr, val);
532 static bool ath10k_pci_irq_pending(struct ath10k *ar)
536 /* Check if the shared legacy irq is for us */
537 cause = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
538 PCIE_INTR_CAUSE_ADDRESS);
539 if (cause & (PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL))
545 static void ath10k_pci_disable_and_clear_legacy_irq(struct ath10k *ar)
547 /* IMPORTANT: INTR_CLR register has to be set after
548 * INTR_ENABLE is set to 0, otherwise interrupt can not be
550 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
552 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_CLR_ADDRESS,
553 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
555 /* IMPORTANT: this extra read transaction is required to
556 * flush the posted write buffer. */
557 (void)ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
558 PCIE_INTR_ENABLE_ADDRESS);
561 static void ath10k_pci_enable_legacy_irq(struct ath10k *ar)
563 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
564 PCIE_INTR_ENABLE_ADDRESS,
565 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
567 /* IMPORTANT: this extra read transaction is required to
568 * flush the posted write buffer. */
569 (void)ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
570 PCIE_INTR_ENABLE_ADDRESS);
573 static inline const char *ath10k_pci_get_irq_method(struct ath10k *ar)
575 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
577 if (ar_pci->num_msi_intrs > 1)
580 if (ar_pci->num_msi_intrs == 1)
586 static int __ath10k_pci_rx_post_buf(struct ath10k_pci_pipe *pipe)
588 struct ath10k *ar = pipe->hif_ce_state;
589 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
590 struct ath10k_ce_pipe *ce_pipe = pipe->ce_hdl;
595 lockdep_assert_held(&ar_pci->ce_lock);
597 skb = dev_alloc_skb(pipe->buf_sz);
601 WARN_ONCE((unsigned long)skb->data & 3, "unaligned skb");
603 paddr = dma_map_single(ar->dev, skb->data,
604 skb->len + skb_tailroom(skb),
606 if (unlikely(dma_mapping_error(ar->dev, paddr))) {
607 ath10k_warn(ar, "failed to dma map pci rx buf\n");
608 dev_kfree_skb_any(skb);
612 ATH10K_SKB_RXCB(skb)->paddr = paddr;
614 ret = __ath10k_ce_rx_post_buf(ce_pipe, skb, paddr);
616 ath10k_warn(ar, "failed to post pci rx buf: %d\n", ret);
617 dma_unmap_single(ar->dev, paddr, skb->len + skb_tailroom(skb),
619 dev_kfree_skb_any(skb);
626 static void __ath10k_pci_rx_post_pipe(struct ath10k_pci_pipe *pipe)
628 struct ath10k *ar = pipe->hif_ce_state;
629 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
630 struct ath10k_ce_pipe *ce_pipe = pipe->ce_hdl;
633 lockdep_assert_held(&ar_pci->ce_lock);
635 if (pipe->buf_sz == 0)
638 if (!ce_pipe->dest_ring)
641 num = __ath10k_ce_rx_num_free_bufs(ce_pipe);
643 ret = __ath10k_pci_rx_post_buf(pipe);
645 ath10k_warn(ar, "failed to post pci rx buf: %d\n", ret);
646 mod_timer(&ar_pci->rx_post_retry, jiffies +
647 ATH10K_PCI_RX_POST_RETRY_MS);
653 static void ath10k_pci_rx_post_pipe(struct ath10k_pci_pipe *pipe)
655 struct ath10k *ar = pipe->hif_ce_state;
656 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
658 spin_lock_bh(&ar_pci->ce_lock);
659 __ath10k_pci_rx_post_pipe(pipe);
660 spin_unlock_bh(&ar_pci->ce_lock);
663 static void ath10k_pci_rx_post(struct ath10k *ar)
665 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
668 spin_lock_bh(&ar_pci->ce_lock);
669 for (i = 0; i < CE_COUNT; i++)
670 __ath10k_pci_rx_post_pipe(&ar_pci->pipe_info[i]);
671 spin_unlock_bh(&ar_pci->ce_lock);
674 static void ath10k_pci_rx_replenish_retry(unsigned long ptr)
676 struct ath10k *ar = (void *)ptr;
678 ath10k_pci_rx_post(ar);
682 * Diagnostic read/write access is provided for startup/config/debug usage.
683 * Caller must guarantee proper alignment, when applicable, and single user
686 static int ath10k_pci_diag_read_mem(struct ath10k *ar, u32 address, void *data,
689 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
692 unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
695 struct ath10k_ce_pipe *ce_diag;
696 /* Host buffer address in CE space */
698 dma_addr_t ce_data_base = 0;
699 void *data_buf = NULL;
702 spin_lock_bh(&ar_pci->ce_lock);
704 ce_diag = ar_pci->ce_diag;
707 * Allocate a temporary bounce buffer to hold caller's data
708 * to be DMA'ed from Target. This guarantees
709 * 1) 4-byte alignment
710 * 2) Buffer in DMA-able space
712 orig_nbytes = nbytes;
713 data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
722 memset(data_buf, 0, orig_nbytes);
724 remaining_bytes = orig_nbytes;
725 ce_data = ce_data_base;
726 while (remaining_bytes) {
727 nbytes = min_t(unsigned int, remaining_bytes,
728 DIAG_TRANSFER_LIMIT);
730 ret = __ath10k_ce_rx_post_buf(ce_diag, NULL, ce_data);
734 /* Request CE to send from Target(!) address to Host buffer */
736 * The address supplied by the caller is in the
737 * Target CPU virtual address space.
739 * In order to use this address with the diagnostic CE,
740 * convert it from Target CPU virtual address space
741 * to CE address space
743 address = TARG_CPU_SPACE_TO_CE_SPACE(ar, ar_pci->mem,
746 ret = ath10k_ce_send_nolock(ce_diag, NULL, (u32)address, nbytes, 0,
752 while (ath10k_ce_completed_send_next_nolock(ce_diag, NULL, &buf,
756 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
762 if (nbytes != completed_nbytes) {
767 if (buf != (u32)address) {
773 while (ath10k_ce_completed_recv_next_nolock(ce_diag, NULL, &buf,
778 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
784 if (nbytes != completed_nbytes) {
789 if (buf != ce_data) {
794 remaining_bytes -= nbytes;
801 memcpy(data, data_buf, orig_nbytes);
803 ath10k_warn(ar, "failed to read diag value at 0x%x: %d\n",
807 dma_free_coherent(ar->dev, orig_nbytes, data_buf,
810 spin_unlock_bh(&ar_pci->ce_lock);
815 static int ath10k_pci_diag_read32(struct ath10k *ar, u32 address, u32 *value)
820 ret = ath10k_pci_diag_read_mem(ar, address, &val, sizeof(val));
821 *value = __le32_to_cpu(val);
826 static int __ath10k_pci_diag_read_hi(struct ath10k *ar, void *dest,
832 host_addr = host_interest_item_address(src);
834 ret = ath10k_pci_diag_read32(ar, host_addr, &addr);
836 ath10k_warn(ar, "failed to get memcpy hi address for firmware address %d: %d\n",
841 ret = ath10k_pci_diag_read_mem(ar, addr, dest, len);
843 ath10k_warn(ar, "failed to memcpy firmware memory from %d (%d B): %d\n",
851 #define ath10k_pci_diag_read_hi(ar, dest, src, len) \
852 __ath10k_pci_diag_read_hi(ar, dest, HI_ITEM(src), len)
854 static int ath10k_pci_diag_write_mem(struct ath10k *ar, u32 address,
855 const void *data, int nbytes)
857 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
860 unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
863 struct ath10k_ce_pipe *ce_diag;
864 void *data_buf = NULL;
865 u32 ce_data; /* Host buffer address in CE space */
866 dma_addr_t ce_data_base = 0;
869 spin_lock_bh(&ar_pci->ce_lock);
871 ce_diag = ar_pci->ce_diag;
874 * Allocate a temporary bounce buffer to hold caller's data
875 * to be DMA'ed to Target. This guarantees
876 * 1) 4-byte alignment
877 * 2) Buffer in DMA-able space
879 orig_nbytes = nbytes;
880 data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
889 /* Copy caller's data to allocated DMA buf */
890 memcpy(data_buf, data, orig_nbytes);
893 * The address supplied by the caller is in the
894 * Target CPU virtual address space.
896 * In order to use this address with the diagnostic CE,
898 * Target CPU virtual address space
902 address = TARG_CPU_SPACE_TO_CE_SPACE(ar, ar_pci->mem, address);
904 remaining_bytes = orig_nbytes;
905 ce_data = ce_data_base;
906 while (remaining_bytes) {
907 /* FIXME: check cast */
908 nbytes = min_t(int, remaining_bytes, DIAG_TRANSFER_LIMIT);
910 /* Set up to receive directly into Target(!) address */
911 ret = __ath10k_ce_rx_post_buf(ce_diag, NULL, address);
916 * Request CE to send caller-supplied data that
917 * was copied to bounce buffer to Target(!) address.
919 ret = ath10k_ce_send_nolock(ce_diag, NULL, (u32)ce_data,
925 while (ath10k_ce_completed_send_next_nolock(ce_diag, NULL, &buf,
930 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
936 if (nbytes != completed_nbytes) {
941 if (buf != ce_data) {
947 while (ath10k_ce_completed_recv_next_nolock(ce_diag, NULL, &buf,
952 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
958 if (nbytes != completed_nbytes) {
963 if (buf != address) {
968 remaining_bytes -= nbytes;
975 dma_free_coherent(ar->dev, orig_nbytes, data_buf,
980 ath10k_warn(ar, "failed to write diag value at 0x%x: %d\n",
983 spin_unlock_bh(&ar_pci->ce_lock);
988 static int ath10k_pci_diag_write32(struct ath10k *ar, u32 address, u32 value)
990 __le32 val = __cpu_to_le32(value);
992 return ath10k_pci_diag_write_mem(ar, address, &val, sizeof(val));
995 /* Called by lower (CE) layer when a send to Target completes. */
996 static void ath10k_pci_ce_send_done(struct ath10k_ce_pipe *ce_state)
998 struct ath10k *ar = ce_state->ar;
999 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1000 struct ath10k_hif_cb *cb = &ar_pci->msg_callbacks_current;
1001 struct sk_buff_head list;
1002 struct sk_buff *skb;
1004 unsigned int nbytes;
1005 unsigned int transfer_id;
1007 __skb_queue_head_init(&list);
1008 while (ath10k_ce_completed_send_next(ce_state, (void **)&skb, &ce_data,
1009 &nbytes, &transfer_id) == 0) {
1010 /* no need to call tx completion for NULL pointers */
1014 __skb_queue_tail(&list, skb);
1017 while ((skb = __skb_dequeue(&list)))
1018 cb->tx_completion(ar, skb);
1021 /* Called by lower (CE) layer when data is received from the Target. */
1022 static void ath10k_pci_ce_recv_data(struct ath10k_ce_pipe *ce_state)
1024 struct ath10k *ar = ce_state->ar;
1025 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1026 struct ath10k_pci_pipe *pipe_info = &ar_pci->pipe_info[ce_state->id];
1027 struct ath10k_hif_cb *cb = &ar_pci->msg_callbacks_current;
1028 struct sk_buff *skb;
1029 struct sk_buff_head list;
1030 void *transfer_context;
1032 unsigned int nbytes, max_nbytes;
1033 unsigned int transfer_id;
1036 __skb_queue_head_init(&list);
1037 while (ath10k_ce_completed_recv_next(ce_state, &transfer_context,
1038 &ce_data, &nbytes, &transfer_id,
1040 skb = transfer_context;
1041 max_nbytes = skb->len + skb_tailroom(skb);
1042 dma_unmap_single(ar->dev, ATH10K_SKB_RXCB(skb)->paddr,
1043 max_nbytes, DMA_FROM_DEVICE);
1045 if (unlikely(max_nbytes < nbytes)) {
1046 ath10k_warn(ar, "rxed more than expected (nbytes %d, max %d)",
1047 nbytes, max_nbytes);
1048 dev_kfree_skb_any(skb);
1052 skb_put(skb, nbytes);
1053 __skb_queue_tail(&list, skb);
1056 while ((skb = __skb_dequeue(&list))) {
1057 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci rx ce pipe %d len %d\n",
1058 ce_state->id, skb->len);
1059 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci rx: ",
1060 skb->data, skb->len);
1062 cb->rx_completion(ar, skb);
1065 ath10k_pci_rx_post_pipe(pipe_info);
1068 static int ath10k_pci_hif_tx_sg(struct ath10k *ar, u8 pipe_id,
1069 struct ath10k_hif_sg_item *items, int n_items)
1071 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1072 struct ath10k_pci_pipe *pci_pipe = &ar_pci->pipe_info[pipe_id];
1073 struct ath10k_ce_pipe *ce_pipe = pci_pipe->ce_hdl;
1074 struct ath10k_ce_ring *src_ring = ce_pipe->src_ring;
1075 unsigned int nentries_mask;
1076 unsigned int sw_index;
1077 unsigned int write_index;
1080 spin_lock_bh(&ar_pci->ce_lock);
1082 nentries_mask = src_ring->nentries_mask;
1083 sw_index = src_ring->sw_index;
1084 write_index = src_ring->write_index;
1086 if (unlikely(CE_RING_DELTA(nentries_mask,
1087 write_index, sw_index - 1) < n_items)) {
1092 for (i = 0; i < n_items - 1; i++) {
1093 ath10k_dbg(ar, ATH10K_DBG_PCI,
1094 "pci tx item %d paddr 0x%08x len %d n_items %d\n",
1095 i, items[i].paddr, items[i].len, n_items);
1096 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci tx data: ",
1097 items[i].vaddr, items[i].len);
1099 err = ath10k_ce_send_nolock(ce_pipe,
1100 items[i].transfer_context,
1103 items[i].transfer_id,
1104 CE_SEND_FLAG_GATHER);
1109 /* `i` is equal to `n_items -1` after for() */
1111 ath10k_dbg(ar, ATH10K_DBG_PCI,
1112 "pci tx item %d paddr 0x%08x len %d n_items %d\n",
1113 i, items[i].paddr, items[i].len, n_items);
1114 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci tx data: ",
1115 items[i].vaddr, items[i].len);
1117 err = ath10k_ce_send_nolock(ce_pipe,
1118 items[i].transfer_context,
1121 items[i].transfer_id,
1126 spin_unlock_bh(&ar_pci->ce_lock);
1131 __ath10k_ce_send_revert(ce_pipe);
1133 spin_unlock_bh(&ar_pci->ce_lock);
1137 static int ath10k_pci_hif_diag_read(struct ath10k *ar, u32 address, void *buf,
1140 return ath10k_pci_diag_read_mem(ar, address, buf, buf_len);
1143 static u16 ath10k_pci_hif_get_free_queue_number(struct ath10k *ar, u8 pipe)
1145 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1147 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif get free queue number\n");
1149 return ath10k_ce_num_free_src_entries(ar_pci->pipe_info[pipe].ce_hdl);
1152 static void ath10k_pci_dump_registers(struct ath10k *ar,
1153 struct ath10k_fw_crash_data *crash_data)
1155 __le32 reg_dump_values[REG_DUMP_COUNT_QCA988X] = {};
1158 lockdep_assert_held(&ar->data_lock);
1160 ret = ath10k_pci_diag_read_hi(ar, ®_dump_values[0],
1162 REG_DUMP_COUNT_QCA988X * sizeof(__le32));
1164 ath10k_err(ar, "failed to read firmware dump area: %d\n", ret);
1168 BUILD_BUG_ON(REG_DUMP_COUNT_QCA988X % 4);
1170 ath10k_err(ar, "firmware register dump:\n");
1171 for (i = 0; i < REG_DUMP_COUNT_QCA988X; i += 4)
1172 ath10k_err(ar, "[%02d]: 0x%08X 0x%08X 0x%08X 0x%08X\n",
1174 __le32_to_cpu(reg_dump_values[i]),
1175 __le32_to_cpu(reg_dump_values[i + 1]),
1176 __le32_to_cpu(reg_dump_values[i + 2]),
1177 __le32_to_cpu(reg_dump_values[i + 3]));
1182 for (i = 0; i < REG_DUMP_COUNT_QCA988X; i++)
1183 crash_data->registers[i] = reg_dump_values[i];
1186 static void ath10k_pci_fw_crashed_dump(struct ath10k *ar)
1188 struct ath10k_fw_crash_data *crash_data;
1191 spin_lock_bh(&ar->data_lock);
1193 ar->stats.fw_crash_counter++;
1195 crash_data = ath10k_debug_get_new_fw_crash_data(ar);
1198 scnprintf(uuid, sizeof(uuid), "%pUl", &crash_data->uuid);
1200 scnprintf(uuid, sizeof(uuid), "n/a");
1202 ath10k_err(ar, "firmware crashed! (uuid %s)\n", uuid);
1203 ath10k_print_driver_info(ar);
1204 ath10k_pci_dump_registers(ar, crash_data);
1206 spin_unlock_bh(&ar->data_lock);
1208 queue_work(ar->workqueue, &ar->restart_work);
1211 static void ath10k_pci_hif_send_complete_check(struct ath10k *ar, u8 pipe,
1214 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif send complete check\n");
1219 * Decide whether to actually poll for completions, or just
1220 * wait for a later chance.
1221 * If there seem to be plenty of resources left, then just wait
1222 * since checking involves reading a CE register, which is a
1223 * relatively expensive operation.
1225 resources = ath10k_pci_hif_get_free_queue_number(ar, pipe);
1228 * If at least 50% of the total resources are still available,
1229 * don't bother checking again yet.
1231 if (resources > (host_ce_config_wlan[pipe].src_nentries >> 1))
1234 ath10k_ce_per_engine_service(ar, pipe);
1237 static void ath10k_pci_hif_set_callbacks(struct ath10k *ar,
1238 struct ath10k_hif_cb *callbacks)
1240 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1242 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif set callbacks\n");
1244 memcpy(&ar_pci->msg_callbacks_current, callbacks,
1245 sizeof(ar_pci->msg_callbacks_current));
1248 static void ath10k_pci_kill_tasklet(struct ath10k *ar)
1250 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1253 tasklet_kill(&ar_pci->intr_tq);
1254 tasklet_kill(&ar_pci->msi_fw_err);
1256 for (i = 0; i < CE_COUNT; i++)
1257 tasklet_kill(&ar_pci->pipe_info[i].intr);
1259 del_timer_sync(&ar_pci->rx_post_retry);
1262 static int ath10k_pci_hif_map_service_to_pipe(struct ath10k *ar,
1263 u16 service_id, u8 *ul_pipe,
1264 u8 *dl_pipe, int *ul_is_polled,
1267 const struct service_to_pipe *entry;
1268 bool ul_set = false, dl_set = false;
1271 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif map service\n");
1273 /* polling for received messages not supported */
1276 for (i = 0; i < ARRAY_SIZE(target_service_to_ce_map_wlan); i++) {
1277 entry = &target_service_to_ce_map_wlan[i];
1279 if (__le32_to_cpu(entry->service_id) != service_id)
1282 switch (__le32_to_cpu(entry->pipedir)) {
1287 *dl_pipe = __le32_to_cpu(entry->pipenum);
1292 *ul_pipe = __le32_to_cpu(entry->pipenum);
1298 *dl_pipe = __le32_to_cpu(entry->pipenum);
1299 *ul_pipe = __le32_to_cpu(entry->pipenum);
1306 if (WARN_ON(!ul_set || !dl_set))
1310 (host_ce_config_wlan[*ul_pipe].flags & CE_ATTR_DIS_INTR) != 0;
1315 static void ath10k_pci_hif_get_default_pipe(struct ath10k *ar,
1316 u8 *ul_pipe, u8 *dl_pipe)
1318 int ul_is_polled, dl_is_polled;
1320 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif get default pipe\n");
1322 (void)ath10k_pci_hif_map_service_to_pipe(ar,
1323 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1330 static void ath10k_pci_irq_msi_fw_mask(struct ath10k *ar)
1334 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS + CORE_CTRL_ADDRESS);
1335 val &= ~CORE_CTRL_PCIE_REG_31_MASK;
1337 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + CORE_CTRL_ADDRESS, val);
1340 static void ath10k_pci_irq_msi_fw_unmask(struct ath10k *ar)
1344 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS + CORE_CTRL_ADDRESS);
1345 val |= CORE_CTRL_PCIE_REG_31_MASK;
1347 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + CORE_CTRL_ADDRESS, val);
1350 static void ath10k_pci_irq_disable(struct ath10k *ar)
1352 ath10k_ce_disable_interrupts(ar);
1353 ath10k_pci_disable_and_clear_legacy_irq(ar);
1354 ath10k_pci_irq_msi_fw_mask(ar);
1357 static void ath10k_pci_irq_sync(struct ath10k *ar)
1359 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1362 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
1363 synchronize_irq(ar_pci->pdev->irq + i);
1366 static void ath10k_pci_irq_enable(struct ath10k *ar)
1368 ath10k_ce_enable_interrupts(ar);
1369 ath10k_pci_enable_legacy_irq(ar);
1370 ath10k_pci_irq_msi_fw_unmask(ar);
1373 static int ath10k_pci_hif_start(struct ath10k *ar)
1375 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1376 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif start\n");
1378 ath10k_pci_irq_enable(ar);
1379 ath10k_pci_rx_post(ar);
1381 pcie_capability_write_word(ar_pci->pdev, PCI_EXP_LNKCTL,
1387 static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe *pci_pipe)
1390 struct ath10k_ce_pipe *ce_pipe;
1391 struct ath10k_ce_ring *ce_ring;
1392 struct sk_buff *skb;
1395 ar = pci_pipe->hif_ce_state;
1396 ce_pipe = pci_pipe->ce_hdl;
1397 ce_ring = ce_pipe->dest_ring;
1402 if (!pci_pipe->buf_sz)
1405 for (i = 0; i < ce_ring->nentries; i++) {
1406 skb = ce_ring->per_transfer_context[i];
1410 ce_ring->per_transfer_context[i] = NULL;
1412 dma_unmap_single(ar->dev, ATH10K_SKB_RXCB(skb)->paddr,
1413 skb->len + skb_tailroom(skb),
1415 dev_kfree_skb_any(skb);
1419 static void ath10k_pci_tx_pipe_cleanup(struct ath10k_pci_pipe *pci_pipe)
1422 struct ath10k_pci *ar_pci;
1423 struct ath10k_ce_pipe *ce_pipe;
1424 struct ath10k_ce_ring *ce_ring;
1425 struct ce_desc *ce_desc;
1426 struct sk_buff *skb;
1430 ar = pci_pipe->hif_ce_state;
1431 ar_pci = ath10k_pci_priv(ar);
1432 ce_pipe = pci_pipe->ce_hdl;
1433 ce_ring = ce_pipe->src_ring;
1438 if (!pci_pipe->buf_sz)
1441 ce_desc = ce_ring->shadow_base;
1442 if (WARN_ON(!ce_desc))
1445 for (i = 0; i < ce_ring->nentries; i++) {
1446 skb = ce_ring->per_transfer_context[i];
1450 ce_ring->per_transfer_context[i] = NULL;
1451 id = MS(__le16_to_cpu(ce_desc[i].flags),
1452 CE_DESC_FLAGS_META_DATA);
1454 ar_pci->msg_callbacks_current.tx_completion(ar, skb);
1459 * Cleanup residual buffers for device shutdown:
1460 * buffers that were enqueued for receive
1461 * buffers that were to be sent
1462 * Note: Buffers that had completed but which were
1463 * not yet processed are on a completion queue. They
1464 * are handled when the completion thread shuts down.
1466 static void ath10k_pci_buffer_cleanup(struct ath10k *ar)
1468 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1471 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
1472 struct ath10k_pci_pipe *pipe_info;
1474 pipe_info = &ar_pci->pipe_info[pipe_num];
1475 ath10k_pci_rx_pipe_cleanup(pipe_info);
1476 ath10k_pci_tx_pipe_cleanup(pipe_info);
1480 static void ath10k_pci_ce_deinit(struct ath10k *ar)
1484 for (i = 0; i < CE_COUNT; i++)
1485 ath10k_ce_deinit_pipe(ar, i);
1488 static void ath10k_pci_flush(struct ath10k *ar)
1490 ath10k_pci_kill_tasklet(ar);
1491 ath10k_pci_buffer_cleanup(ar);
1494 static void ath10k_pci_hif_stop(struct ath10k *ar)
1496 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1497 unsigned long flags;
1499 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif stop\n");
1501 /* Most likely the device has HTT Rx ring configured. The only way to
1502 * prevent the device from accessing (and possible corrupting) host
1503 * memory is to reset the chip now.
1505 * There's also no known way of masking MSI interrupts on the device.
1506 * For ranged MSI the CE-related interrupts can be masked. However
1507 * regardless how many MSI interrupts are assigned the first one
1508 * is always used for firmware indications (crashes) and cannot be
1509 * masked. To prevent the device from asserting the interrupt reset it
1510 * before proceeding with cleanup.
1512 ath10k_pci_warm_reset(ar);
1514 ath10k_pci_irq_disable(ar);
1515 ath10k_pci_irq_sync(ar);
1516 ath10k_pci_flush(ar);
1518 spin_lock_irqsave(&ar_pci->ps_lock, flags);
1519 WARN_ON(ar_pci->ps_wake_refcount > 0);
1520 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
1523 static int ath10k_pci_hif_exchange_bmi_msg(struct ath10k *ar,
1524 void *req, u32 req_len,
1525 void *resp, u32 *resp_len)
1527 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1528 struct ath10k_pci_pipe *pci_tx = &ar_pci->pipe_info[BMI_CE_NUM_TO_TARG];
1529 struct ath10k_pci_pipe *pci_rx = &ar_pci->pipe_info[BMI_CE_NUM_TO_HOST];
1530 struct ath10k_ce_pipe *ce_tx = pci_tx->ce_hdl;
1531 struct ath10k_ce_pipe *ce_rx = pci_rx->ce_hdl;
1532 dma_addr_t req_paddr = 0;
1533 dma_addr_t resp_paddr = 0;
1534 struct bmi_xfer xfer = {};
1535 void *treq, *tresp = NULL;
1540 if (resp && !resp_len)
1543 if (resp && resp_len && *resp_len == 0)
1546 treq = kmemdup(req, req_len, GFP_KERNEL);
1550 req_paddr = dma_map_single(ar->dev, treq, req_len, DMA_TO_DEVICE);
1551 ret = dma_mapping_error(ar->dev, req_paddr);
1555 if (resp && resp_len) {
1556 tresp = kzalloc(*resp_len, GFP_KERNEL);
1562 resp_paddr = dma_map_single(ar->dev, tresp, *resp_len,
1564 ret = dma_mapping_error(ar->dev, resp_paddr);
1568 xfer.wait_for_resp = true;
1571 ath10k_ce_rx_post_buf(ce_rx, &xfer, resp_paddr);
1574 ret = ath10k_ce_send(ce_tx, &xfer, req_paddr, req_len, -1, 0);
1578 ret = ath10k_pci_bmi_wait(ce_tx, ce_rx, &xfer);
1581 unsigned int unused_nbytes;
1582 unsigned int unused_id;
1584 ath10k_ce_cancel_send_next(ce_tx, NULL, &unused_buffer,
1585 &unused_nbytes, &unused_id);
1587 /* non-zero means we did not time out */
1595 ath10k_ce_revoke_recv_next(ce_rx, NULL, &unused_buffer);
1596 dma_unmap_single(ar->dev, resp_paddr,
1597 *resp_len, DMA_FROM_DEVICE);
1600 dma_unmap_single(ar->dev, req_paddr, req_len, DMA_TO_DEVICE);
1602 if (ret == 0 && resp_len) {
1603 *resp_len = min(*resp_len, xfer.resp_len);
1604 memcpy(resp, tresp, xfer.resp_len);
1613 static void ath10k_pci_bmi_send_done(struct ath10k_ce_pipe *ce_state)
1615 struct bmi_xfer *xfer;
1617 unsigned int nbytes;
1618 unsigned int transfer_id;
1620 if (ath10k_ce_completed_send_next(ce_state, (void **)&xfer, &ce_data,
1621 &nbytes, &transfer_id))
1624 xfer->tx_done = true;
1627 static void ath10k_pci_bmi_recv_data(struct ath10k_ce_pipe *ce_state)
1629 struct ath10k *ar = ce_state->ar;
1630 struct bmi_xfer *xfer;
1632 unsigned int nbytes;
1633 unsigned int transfer_id;
1636 if (ath10k_ce_completed_recv_next(ce_state, (void **)&xfer, &ce_data,
1637 &nbytes, &transfer_id, &flags))
1640 if (WARN_ON_ONCE(!xfer))
1643 if (!xfer->wait_for_resp) {
1644 ath10k_warn(ar, "unexpected: BMI data received; ignoring\n");
1648 xfer->resp_len = nbytes;
1649 xfer->rx_done = true;
1652 static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
1653 struct ath10k_ce_pipe *rx_pipe,
1654 struct bmi_xfer *xfer)
1656 unsigned long timeout = jiffies + BMI_COMMUNICATION_TIMEOUT_HZ;
1658 while (time_before_eq(jiffies, timeout)) {
1659 ath10k_pci_bmi_send_done(tx_pipe);
1660 ath10k_pci_bmi_recv_data(rx_pipe);
1662 if (xfer->tx_done && (xfer->rx_done == xfer->wait_for_resp))
1672 * Send an interrupt to the device to wake up the Target CPU
1673 * so it has an opportunity to notice any changed state.
1675 static int ath10k_pci_wake_target_cpu(struct ath10k *ar)
1679 addr = SOC_CORE_BASE_ADDRESS | CORE_CTRL_ADDRESS;
1680 val = ath10k_pci_read32(ar, addr);
1681 val |= CORE_CTRL_CPU_INTR_MASK;
1682 ath10k_pci_write32(ar, addr, val);
1687 static int ath10k_pci_get_num_banks(struct ath10k *ar)
1689 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1691 switch (ar_pci->pdev->device) {
1692 case QCA988X_2_0_DEVICE_ID:
1694 case QCA6174_2_1_DEVICE_ID:
1695 switch (MS(ar->chip_id, SOC_CHIP_ID_REV)) {
1696 case QCA6174_HW_1_0_CHIP_ID_REV:
1697 case QCA6174_HW_1_1_CHIP_ID_REV:
1698 case QCA6174_HW_2_1_CHIP_ID_REV:
1699 case QCA6174_HW_2_2_CHIP_ID_REV:
1701 case QCA6174_HW_1_3_CHIP_ID_REV:
1703 case QCA6174_HW_3_0_CHIP_ID_REV:
1704 case QCA6174_HW_3_1_CHIP_ID_REV:
1705 case QCA6174_HW_3_2_CHIP_ID_REV:
1711 ath10k_warn(ar, "unknown number of banks, assuming 1\n");
1715 static int ath10k_pci_init_config(struct ath10k *ar)
1717 u32 interconnect_targ_addr;
1718 u32 pcie_state_targ_addr = 0;
1719 u32 pipe_cfg_targ_addr = 0;
1720 u32 svc_to_pipe_map = 0;
1721 u32 pcie_config_flags = 0;
1723 u32 ealloc_targ_addr;
1725 u32 flag2_targ_addr;
1728 /* Download to Target the CE Config and the service-to-CE map */
1729 interconnect_targ_addr =
1730 host_interest_item_address(HI_ITEM(hi_interconnect_state));
1732 /* Supply Target-side CE configuration */
1733 ret = ath10k_pci_diag_read32(ar, interconnect_targ_addr,
1734 &pcie_state_targ_addr);
1736 ath10k_err(ar, "Failed to get pcie state addr: %d\n", ret);
1740 if (pcie_state_targ_addr == 0) {
1742 ath10k_err(ar, "Invalid pcie state addr\n");
1746 ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
1747 offsetof(struct pcie_state,
1749 &pipe_cfg_targ_addr);
1751 ath10k_err(ar, "Failed to get pipe cfg addr: %d\n", ret);
1755 if (pipe_cfg_targ_addr == 0) {
1757 ath10k_err(ar, "Invalid pipe cfg addr\n");
1761 ret = ath10k_pci_diag_write_mem(ar, pipe_cfg_targ_addr,
1762 target_ce_config_wlan,
1763 sizeof(target_ce_config_wlan));
1766 ath10k_err(ar, "Failed to write pipe cfg: %d\n", ret);
1770 ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
1771 offsetof(struct pcie_state,
1775 ath10k_err(ar, "Failed to get svc/pipe map: %d\n", ret);
1779 if (svc_to_pipe_map == 0) {
1781 ath10k_err(ar, "Invalid svc_to_pipe map\n");
1785 ret = ath10k_pci_diag_write_mem(ar, svc_to_pipe_map,
1786 target_service_to_ce_map_wlan,
1787 sizeof(target_service_to_ce_map_wlan));
1789 ath10k_err(ar, "Failed to write svc/pipe map: %d\n", ret);
1793 ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
1794 offsetof(struct pcie_state,
1796 &pcie_config_flags);
1798 ath10k_err(ar, "Failed to get pcie config_flags: %d\n", ret);
1802 pcie_config_flags &= ~PCIE_CONFIG_FLAG_ENABLE_L1;
1804 ret = ath10k_pci_diag_write32(ar, (pcie_state_targ_addr +
1805 offsetof(struct pcie_state,
1809 ath10k_err(ar, "Failed to write pcie config_flags: %d\n", ret);
1813 /* configure early allocation */
1814 ealloc_targ_addr = host_interest_item_address(HI_ITEM(hi_early_alloc));
1816 ret = ath10k_pci_diag_read32(ar, ealloc_targ_addr, &ealloc_value);
1818 ath10k_err(ar, "Faile to get early alloc val: %d\n", ret);
1822 /* first bank is switched to IRAM */
1823 ealloc_value |= ((HI_EARLY_ALLOC_MAGIC << HI_EARLY_ALLOC_MAGIC_SHIFT) &
1824 HI_EARLY_ALLOC_MAGIC_MASK);
1825 ealloc_value |= ((ath10k_pci_get_num_banks(ar) <<
1826 HI_EARLY_ALLOC_IRAM_BANKS_SHIFT) &
1827 HI_EARLY_ALLOC_IRAM_BANKS_MASK);
1829 ret = ath10k_pci_diag_write32(ar, ealloc_targ_addr, ealloc_value);
1831 ath10k_err(ar, "Failed to set early alloc val: %d\n", ret);
1835 /* Tell Target to proceed with initialization */
1836 flag2_targ_addr = host_interest_item_address(HI_ITEM(hi_option_flag2));
1838 ret = ath10k_pci_diag_read32(ar, flag2_targ_addr, &flag2_value);
1840 ath10k_err(ar, "Failed to get option val: %d\n", ret);
1844 flag2_value |= HI_OPTION_EARLY_CFG_DONE;
1846 ret = ath10k_pci_diag_write32(ar, flag2_targ_addr, flag2_value);
1848 ath10k_err(ar, "Failed to set option val: %d\n", ret);
1855 static int ath10k_pci_alloc_pipes(struct ath10k *ar)
1857 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1858 struct ath10k_pci_pipe *pipe;
1861 for (i = 0; i < CE_COUNT; i++) {
1862 pipe = &ar_pci->pipe_info[i];
1863 pipe->ce_hdl = &ar_pci->ce_states[i];
1865 pipe->hif_ce_state = ar;
1867 ret = ath10k_ce_alloc_pipe(ar, i, &host_ce_config_wlan[i],
1868 ath10k_pci_ce_send_done,
1869 ath10k_pci_ce_recv_data);
1871 ath10k_err(ar, "failed to allocate copy engine pipe %d: %d\n",
1876 /* Last CE is Diagnostic Window */
1877 if (i == CE_COUNT - 1) {
1878 ar_pci->ce_diag = pipe->ce_hdl;
1882 pipe->buf_sz = (size_t)(host_ce_config_wlan[i].src_sz_max);
1888 static void ath10k_pci_free_pipes(struct ath10k *ar)
1892 for (i = 0; i < CE_COUNT; i++)
1893 ath10k_ce_free_pipe(ar, i);
1896 static int ath10k_pci_init_pipes(struct ath10k *ar)
1900 for (i = 0; i < CE_COUNT; i++) {
1901 ret = ath10k_ce_init_pipe(ar, i, &host_ce_config_wlan[i]);
1903 ath10k_err(ar, "failed to initialize copy engine pipe %d: %d\n",
1912 static bool ath10k_pci_has_fw_crashed(struct ath10k *ar)
1914 return ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS) &
1915 FW_IND_EVENT_PENDING;
1918 static void ath10k_pci_fw_crashed_clear(struct ath10k *ar)
1922 val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
1923 val &= ~FW_IND_EVENT_PENDING;
1924 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, val);
1927 /* this function effectively clears target memory controller assert line */
1928 static void ath10k_pci_warm_reset_si0(struct ath10k *ar)
1932 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1933 ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
1934 val | SOC_RESET_CONTROL_SI0_RST_MASK);
1935 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1939 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1940 ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
1941 val & ~SOC_RESET_CONTROL_SI0_RST_MASK);
1942 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1947 static void ath10k_pci_warm_reset_cpu(struct ath10k *ar)
1951 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, 0);
1953 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1954 SOC_RESET_CONTROL_ADDRESS);
1955 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1956 val | SOC_RESET_CONTROL_CPU_WARM_RST_MASK);
1959 static void ath10k_pci_warm_reset_ce(struct ath10k *ar)
1963 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1964 SOC_RESET_CONTROL_ADDRESS);
1966 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1967 val | SOC_RESET_CONTROL_CE_RST_MASK);
1969 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1970 val & ~SOC_RESET_CONTROL_CE_RST_MASK);
1973 static void ath10k_pci_warm_reset_clear_lf(struct ath10k *ar)
1977 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1978 SOC_LF_TIMER_CONTROL0_ADDRESS);
1979 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS +
1980 SOC_LF_TIMER_CONTROL0_ADDRESS,
1981 val & ~SOC_LF_TIMER_CONTROL0_ENABLE_MASK);
1984 static int ath10k_pci_warm_reset(struct ath10k *ar)
1988 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot warm reset\n");
1990 spin_lock_bh(&ar->data_lock);
1991 ar->stats.fw_warm_reset_counter++;
1992 spin_unlock_bh(&ar->data_lock);
1994 ath10k_pci_irq_disable(ar);
1996 /* Make sure the target CPU is not doing anything dangerous, e.g. if it
1997 * were to access copy engine while host performs copy engine reset
1998 * then it is possible for the device to confuse pci-e controller to
1999 * the point of bringing host system to a complete stop (i.e. hang).
2001 ath10k_pci_warm_reset_si0(ar);
2002 ath10k_pci_warm_reset_cpu(ar);
2003 ath10k_pci_init_pipes(ar);
2004 ath10k_pci_wait_for_target_init(ar);
2006 ath10k_pci_warm_reset_clear_lf(ar);
2007 ath10k_pci_warm_reset_ce(ar);
2008 ath10k_pci_warm_reset_cpu(ar);
2009 ath10k_pci_init_pipes(ar);
2011 ret = ath10k_pci_wait_for_target_init(ar);
2013 ath10k_warn(ar, "failed to wait for target init: %d\n", ret);
2017 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot warm reset complete\n");
2022 static int ath10k_pci_qca988x_chip_reset(struct ath10k *ar)
2027 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot 988x chip reset\n");
2029 /* Some hardware revisions (e.g. CUS223v2) has issues with cold reset.
2030 * It is thus preferred to use warm reset which is safer but may not be
2031 * able to recover the device from all possible fail scenarios.
2033 * Warm reset doesn't always work on first try so attempt it a few
2034 * times before giving up.
2036 for (i = 0; i < ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS; i++) {
2037 ret = ath10k_pci_warm_reset(ar);
2039 ath10k_warn(ar, "failed to warm reset attempt %d of %d: %d\n",
2040 i + 1, ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS,
2045 /* FIXME: Sometimes copy engine doesn't recover after warm
2046 * reset. In most cases this needs cold reset. In some of these
2047 * cases the device is in such a state that a cold reset may
2050 * Reading any host interest register via copy engine is
2051 * sufficient to verify if device is capable of booting
2054 ret = ath10k_pci_init_pipes(ar);
2056 ath10k_warn(ar, "failed to init copy engine: %d\n",
2061 ret = ath10k_pci_diag_read32(ar, QCA988X_HOST_INTEREST_ADDRESS,
2064 ath10k_warn(ar, "failed to poke copy engine: %d\n",
2069 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot chip reset complete (warm)\n");
2073 if (ath10k_pci_reset_mode == ATH10K_PCI_RESET_WARM_ONLY) {
2074 ath10k_warn(ar, "refusing cold reset as requested\n");
2078 ret = ath10k_pci_cold_reset(ar);
2080 ath10k_warn(ar, "failed to cold reset: %d\n", ret);
2084 ret = ath10k_pci_wait_for_target_init(ar);
2086 ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
2091 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca988x chip reset complete (cold)\n");
2096 static int ath10k_pci_qca6174_chip_reset(struct ath10k *ar)
2100 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca6174 chip reset\n");
2102 /* FIXME: QCA6174 requires cold + warm reset to work. */
2104 ret = ath10k_pci_cold_reset(ar);
2106 ath10k_warn(ar, "failed to cold reset: %d\n", ret);
2110 ret = ath10k_pci_wait_for_target_init(ar);
2112 ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
2117 ret = ath10k_pci_warm_reset(ar);
2119 ath10k_warn(ar, "failed to warm reset: %d\n", ret);
2123 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca6174 chip reset complete (cold)\n");
2128 static int ath10k_pci_chip_reset(struct ath10k *ar)
2130 if (QCA_REV_988X(ar))
2131 return ath10k_pci_qca988x_chip_reset(ar);
2132 else if (QCA_REV_6174(ar))
2133 return ath10k_pci_qca6174_chip_reset(ar);
2138 static int ath10k_pci_hif_power_up(struct ath10k *ar)
2140 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2143 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif power up\n");
2145 pcie_capability_read_word(ar_pci->pdev, PCI_EXP_LNKCTL,
2147 pcie_capability_write_word(ar_pci->pdev, PCI_EXP_LNKCTL,
2148 ar_pci->link_ctl & ~PCI_EXP_LNKCTL_ASPMC);
2151 * Bring the target up cleanly.
2153 * The target may be in an undefined state with an AUX-powered Target
2154 * and a Host in WoW mode. If the Host crashes, loses power, or is
2155 * restarted (without unloading the driver) then the Target is left
2156 * (aux) powered and running. On a subsequent driver load, the Target
2157 * is in an unexpected state. We try to catch that here in order to
2158 * reset the Target and retry the probe.
2160 ret = ath10k_pci_chip_reset(ar);
2162 if (ath10k_pci_has_fw_crashed(ar)) {
2163 ath10k_warn(ar, "firmware crashed during chip reset\n");
2164 ath10k_pci_fw_crashed_clear(ar);
2165 ath10k_pci_fw_crashed_dump(ar);
2168 ath10k_err(ar, "failed to reset chip: %d\n", ret);
2172 ret = ath10k_pci_init_pipes(ar);
2174 ath10k_err(ar, "failed to initialize CE: %d\n", ret);
2178 ret = ath10k_pci_init_config(ar);
2180 ath10k_err(ar, "failed to setup init config: %d\n", ret);
2184 ret = ath10k_pci_wake_target_cpu(ar);
2186 ath10k_err(ar, "could not wake up target CPU: %d\n", ret);
2193 ath10k_pci_ce_deinit(ar);
2199 static void ath10k_pci_hif_power_down(struct ath10k *ar)
2201 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif power down\n");
2203 /* Currently hif_power_up performs effectively a reset and hif_stop
2204 * resets the chip as well so there's no point in resetting here.
2210 static int ath10k_pci_hif_suspend(struct ath10k *ar)
2212 /* The grace timer can still be counting down and ar->ps_awake be true.
2213 * It is known that the device may be asleep after resuming regardless
2214 * of the SoC powersave state before suspending. Hence make sure the
2215 * device is asleep before proceeding.
2217 ath10k_pci_sleep_sync(ar);
2222 static int ath10k_pci_hif_resume(struct ath10k *ar)
2224 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2225 struct pci_dev *pdev = ar_pci->pdev;
2228 /* Suspend/Resume resets the PCI configuration space, so we have to
2229 * re-disable the RETRY_TIMEOUT register (0x41) to keep PCI Tx retries
2230 * from interfering with C3 CPU state. pci_restore_state won't help
2231 * here since it only restores the first 64 bytes pci config header.
2233 pci_read_config_dword(pdev, 0x40, &val);
2234 if ((val & 0x0000ff00) != 0)
2235 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
2241 static const struct ath10k_hif_ops ath10k_pci_hif_ops = {
2242 .tx_sg = ath10k_pci_hif_tx_sg,
2243 .diag_read = ath10k_pci_hif_diag_read,
2244 .diag_write = ath10k_pci_diag_write_mem,
2245 .exchange_bmi_msg = ath10k_pci_hif_exchange_bmi_msg,
2246 .start = ath10k_pci_hif_start,
2247 .stop = ath10k_pci_hif_stop,
2248 .map_service_to_pipe = ath10k_pci_hif_map_service_to_pipe,
2249 .get_default_pipe = ath10k_pci_hif_get_default_pipe,
2250 .send_complete_check = ath10k_pci_hif_send_complete_check,
2251 .set_callbacks = ath10k_pci_hif_set_callbacks,
2252 .get_free_queue_number = ath10k_pci_hif_get_free_queue_number,
2253 .power_up = ath10k_pci_hif_power_up,
2254 .power_down = ath10k_pci_hif_power_down,
2255 .read32 = ath10k_pci_read32,
2256 .write32 = ath10k_pci_write32,
2258 .suspend = ath10k_pci_hif_suspend,
2259 .resume = ath10k_pci_hif_resume,
2263 static void ath10k_pci_ce_tasklet(unsigned long ptr)
2265 struct ath10k_pci_pipe *pipe = (struct ath10k_pci_pipe *)ptr;
2266 struct ath10k_pci *ar_pci = pipe->ar_pci;
2268 ath10k_ce_per_engine_service(ar_pci->ar, pipe->pipe_num);
2271 static void ath10k_msi_err_tasklet(unsigned long data)
2273 struct ath10k *ar = (struct ath10k *)data;
2275 if (!ath10k_pci_has_fw_crashed(ar)) {
2276 ath10k_warn(ar, "received unsolicited fw crash interrupt\n");
2280 ath10k_pci_irq_disable(ar);
2281 ath10k_pci_fw_crashed_clear(ar);
2282 ath10k_pci_fw_crashed_dump(ar);
2286 * Handler for a per-engine interrupt on a PARTICULAR CE.
2287 * This is used in cases where each CE has a private MSI interrupt.
2289 static irqreturn_t ath10k_pci_per_engine_handler(int irq, void *arg)
2291 struct ath10k *ar = arg;
2292 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2293 int ce_id = irq - ar_pci->pdev->irq - MSI_ASSIGN_CE_INITIAL;
2295 if (ce_id < 0 || ce_id >= ARRAY_SIZE(ar_pci->pipe_info)) {
2296 ath10k_warn(ar, "unexpected/invalid irq %d ce_id %d\n", irq,
2302 * NOTE: We are able to derive ce_id from irq because we
2303 * use a one-to-one mapping for CE's 0..5.
2304 * CE's 6 & 7 do not use interrupts at all.
2306 * This mapping must be kept in sync with the mapping
2309 tasklet_schedule(&ar_pci->pipe_info[ce_id].intr);
2313 static irqreturn_t ath10k_pci_msi_fw_handler(int irq, void *arg)
2315 struct ath10k *ar = arg;
2316 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2318 tasklet_schedule(&ar_pci->msi_fw_err);
2323 * Top-level interrupt handler for all PCI interrupts from a Target.
2324 * When a block of MSI interrupts is allocated, this top-level handler
2325 * is not used; instead, we directly call the correct sub-handler.
2327 static irqreturn_t ath10k_pci_interrupt_handler(int irq, void *arg)
2329 struct ath10k *ar = arg;
2330 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2332 if (ar_pci->num_msi_intrs == 0) {
2333 if (!ath10k_pci_irq_pending(ar))
2336 ath10k_pci_disable_and_clear_legacy_irq(ar);
2339 tasklet_schedule(&ar_pci->intr_tq);
2344 static void ath10k_pci_tasklet(unsigned long data)
2346 struct ath10k *ar = (struct ath10k *)data;
2347 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2349 if (ath10k_pci_has_fw_crashed(ar)) {
2350 ath10k_pci_irq_disable(ar);
2351 ath10k_pci_fw_crashed_clear(ar);
2352 ath10k_pci_fw_crashed_dump(ar);
2356 ath10k_ce_per_engine_service_any(ar);
2358 /* Re-enable legacy irq that was disabled in the irq handler */
2359 if (ar_pci->num_msi_intrs == 0)
2360 ath10k_pci_enable_legacy_irq(ar);
2363 static int ath10k_pci_request_irq_msix(struct ath10k *ar)
2365 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2368 ret = request_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW,
2369 ath10k_pci_msi_fw_handler,
2370 IRQF_SHARED, "ath10k_pci", ar);
2372 ath10k_warn(ar, "failed to request MSI-X fw irq %d: %d\n",
2373 ar_pci->pdev->irq + MSI_ASSIGN_FW, ret);
2377 for (i = MSI_ASSIGN_CE_INITIAL; i <= MSI_ASSIGN_CE_MAX; i++) {
2378 ret = request_irq(ar_pci->pdev->irq + i,
2379 ath10k_pci_per_engine_handler,
2380 IRQF_SHARED, "ath10k_pci", ar);
2382 ath10k_warn(ar, "failed to request MSI-X ce irq %d: %d\n",
2383 ar_pci->pdev->irq + i, ret);
2385 for (i--; i >= MSI_ASSIGN_CE_INITIAL; i--)
2386 free_irq(ar_pci->pdev->irq + i, ar);
2388 free_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW, ar);
2396 static int ath10k_pci_request_irq_msi(struct ath10k *ar)
2398 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2401 ret = request_irq(ar_pci->pdev->irq,
2402 ath10k_pci_interrupt_handler,
2403 IRQF_SHARED, "ath10k_pci", ar);
2405 ath10k_warn(ar, "failed to request MSI irq %d: %d\n",
2406 ar_pci->pdev->irq, ret);
2413 static int ath10k_pci_request_irq_legacy(struct ath10k *ar)
2415 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2418 ret = request_irq(ar_pci->pdev->irq,
2419 ath10k_pci_interrupt_handler,
2420 IRQF_SHARED, "ath10k_pci", ar);
2422 ath10k_warn(ar, "failed to request legacy irq %d: %d\n",
2423 ar_pci->pdev->irq, ret);
2430 static int ath10k_pci_request_irq(struct ath10k *ar)
2432 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2434 switch (ar_pci->num_msi_intrs) {
2436 return ath10k_pci_request_irq_legacy(ar);
2438 return ath10k_pci_request_irq_msi(ar);
2439 case MSI_NUM_REQUEST:
2440 return ath10k_pci_request_irq_msix(ar);
2443 ath10k_warn(ar, "unknown irq configuration upon request\n");
2447 static void ath10k_pci_free_irq(struct ath10k *ar)
2449 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2452 /* There's at least one interrupt irregardless whether its legacy INTR
2453 * or MSI or MSI-X */
2454 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
2455 free_irq(ar_pci->pdev->irq + i, ar);
2458 static void ath10k_pci_init_irq_tasklets(struct ath10k *ar)
2460 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2463 tasklet_init(&ar_pci->intr_tq, ath10k_pci_tasklet, (unsigned long)ar);
2464 tasklet_init(&ar_pci->msi_fw_err, ath10k_msi_err_tasklet,
2467 for (i = 0; i < CE_COUNT; i++) {
2468 ar_pci->pipe_info[i].ar_pci = ar_pci;
2469 tasklet_init(&ar_pci->pipe_info[i].intr, ath10k_pci_ce_tasklet,
2470 (unsigned long)&ar_pci->pipe_info[i]);
2474 static int ath10k_pci_init_irq(struct ath10k *ar)
2476 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2479 ath10k_pci_init_irq_tasklets(ar);
2481 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_AUTO)
2482 ath10k_info(ar, "limiting irq mode to: %d\n",
2483 ath10k_pci_irq_mode);
2486 if (ath10k_pci_irq_mode == ATH10K_PCI_IRQ_AUTO) {
2487 ar_pci->num_msi_intrs = MSI_NUM_REQUEST;
2488 ret = pci_enable_msi_range(ar_pci->pdev, ar_pci->num_msi_intrs,
2489 ar_pci->num_msi_intrs);
2497 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_LEGACY) {
2498 ar_pci->num_msi_intrs = 1;
2499 ret = pci_enable_msi(ar_pci->pdev);
2508 * A potential race occurs here: The CORE_BASE write
2509 * depends on target correctly decoding AXI address but
2510 * host won't know when target writes BAR to CORE_CTRL.
2511 * This write might get lost if target has NOT written BAR.
2512 * For now, fix the race by repeating the write in below
2513 * synchronization checking. */
2514 ar_pci->num_msi_intrs = 0;
2516 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2517 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
2522 static void ath10k_pci_deinit_irq_legacy(struct ath10k *ar)
2524 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2528 static int ath10k_pci_deinit_irq(struct ath10k *ar)
2530 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2532 switch (ar_pci->num_msi_intrs) {
2534 ath10k_pci_deinit_irq_legacy(ar);
2538 case MSI_NUM_REQUEST:
2539 pci_disable_msi(ar_pci->pdev);
2542 pci_disable_msi(ar_pci->pdev);
2545 ath10k_warn(ar, "unknown irq configuration upon deinit\n");
2549 static int ath10k_pci_wait_for_target_init(struct ath10k *ar)
2551 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2552 unsigned long timeout;
2555 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot waiting target to initialise\n");
2557 timeout = jiffies + msecs_to_jiffies(ATH10K_PCI_TARGET_WAIT);
2560 val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
2562 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target indicator %x\n",
2565 /* target should never return this */
2566 if (val == 0xffffffff)
2569 /* the device has crashed so don't bother trying anymore */
2570 if (val & FW_IND_EVENT_PENDING)
2573 if (val & FW_IND_INITIALIZED)
2576 if (ar_pci->num_msi_intrs == 0)
2577 /* Fix potential race by repeating CORE_BASE writes */
2578 ath10k_pci_enable_legacy_irq(ar);
2581 } while (time_before(jiffies, timeout));
2583 ath10k_pci_disable_and_clear_legacy_irq(ar);
2584 ath10k_pci_irq_msi_fw_mask(ar);
2586 if (val == 0xffffffff) {
2587 ath10k_err(ar, "failed to read device register, device is gone\n");
2591 if (val & FW_IND_EVENT_PENDING) {
2592 ath10k_warn(ar, "device has crashed during init\n");
2596 if (!(val & FW_IND_INITIALIZED)) {
2597 ath10k_err(ar, "failed to receive initialized event from target: %08x\n",
2602 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target initialised\n");
2606 static int ath10k_pci_cold_reset(struct ath10k *ar)
2611 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cold reset\n");
2613 spin_lock_bh(&ar->data_lock);
2615 ar->stats.fw_cold_reset_counter++;
2617 spin_unlock_bh(&ar->data_lock);
2619 /* Put Target, including PCIe, into RESET. */
2620 val = ath10k_pci_reg_read32(ar, SOC_GLOBAL_RESET_ADDRESS);
2622 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
2624 for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
2625 if (ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
2626 RTC_STATE_COLD_RESET_MASK)
2631 /* Pull Target, including PCIe, out of RESET. */
2633 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
2635 for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
2636 if (!(ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
2637 RTC_STATE_COLD_RESET_MASK))
2642 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cold reset complete\n");
2647 static int ath10k_pci_claim(struct ath10k *ar)
2649 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2650 struct pci_dev *pdev = ar_pci->pdev;
2653 pci_set_drvdata(pdev, ar);
2655 ret = pci_enable_device(pdev);
2657 ath10k_err(ar, "failed to enable pci device: %d\n", ret);
2661 ret = pci_request_region(pdev, BAR_NUM, "ath");
2663 ath10k_err(ar, "failed to request region BAR%d: %d\n", BAR_NUM,
2668 /* Target expects 32 bit DMA. Enforce it. */
2669 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2671 ath10k_err(ar, "failed to set dma mask to 32-bit: %d\n", ret);
2675 ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2677 ath10k_err(ar, "failed to set consistent dma mask to 32-bit: %d\n",
2682 pci_set_master(pdev);
2684 /* Arrange for access to Target SoC registers. */
2685 ar_pci->mem = pci_iomap(pdev, BAR_NUM, 0);
2687 ath10k_err(ar, "failed to iomap BAR%d\n", BAR_NUM);
2692 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot pci_mem 0x%p\n", ar_pci->mem);
2696 pci_clear_master(pdev);
2699 pci_release_region(pdev, BAR_NUM);
2702 pci_disable_device(pdev);
2707 static void ath10k_pci_release(struct ath10k *ar)
2709 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2710 struct pci_dev *pdev = ar_pci->pdev;
2712 pci_iounmap(pdev, ar_pci->mem);
2713 pci_release_region(pdev, BAR_NUM);
2714 pci_clear_master(pdev);
2715 pci_disable_device(pdev);
2718 static bool ath10k_pci_chip_is_supported(u32 dev_id, u32 chip_id)
2720 const struct ath10k_pci_supp_chip *supp_chip;
2722 u32 rev_id = MS(chip_id, SOC_CHIP_ID_REV);
2724 for (i = 0; i < ARRAY_SIZE(ath10k_pci_supp_chips); i++) {
2725 supp_chip = &ath10k_pci_supp_chips[i];
2727 if (supp_chip->dev_id == dev_id &&
2728 supp_chip->rev_id == rev_id)
2735 static int ath10k_pci_probe(struct pci_dev *pdev,
2736 const struct pci_device_id *pci_dev)
2740 struct ath10k_pci *ar_pci;
2741 enum ath10k_hw_rev hw_rev;
2744 switch (pci_dev->device) {
2745 case QCA988X_2_0_DEVICE_ID:
2746 hw_rev = ATH10K_HW_QCA988X;
2748 case QCA6174_2_1_DEVICE_ID:
2749 hw_rev = ATH10K_HW_QCA6174;
2756 ar = ath10k_core_create(sizeof(*ar_pci), &pdev->dev, ATH10K_BUS_PCI,
2757 hw_rev, &ath10k_pci_hif_ops);
2759 dev_err(&pdev->dev, "failed to allocate core\n");
2763 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci probe\n");
2765 ar_pci = ath10k_pci_priv(ar);
2766 ar_pci->pdev = pdev;
2767 ar_pci->dev = &pdev->dev;
2770 if (pdev->subsystem_vendor || pdev->subsystem_device)
2771 scnprintf(ar->spec_board_id, sizeof(ar->spec_board_id),
2772 "%04x:%04x:%04x:%04x",
2773 pdev->vendor, pdev->device,
2774 pdev->subsystem_vendor, pdev->subsystem_device);
2776 spin_lock_init(&ar_pci->ce_lock);
2777 spin_lock_init(&ar_pci->ps_lock);
2779 setup_timer(&ar_pci->rx_post_retry, ath10k_pci_rx_replenish_retry,
2781 setup_timer(&ar_pci->ps_timer, ath10k_pci_ps_timer,
2784 ret = ath10k_pci_claim(ar);
2786 ath10k_err(ar, "failed to claim device: %d\n", ret);
2787 goto err_core_destroy;
2790 ret = ath10k_pci_alloc_pipes(ar);
2792 ath10k_err(ar, "failed to allocate copy engine pipes: %d\n",
2797 ath10k_pci_ce_deinit(ar);
2798 ath10k_pci_irq_disable(ar);
2800 ret = ath10k_pci_init_irq(ar);
2802 ath10k_err(ar, "failed to init irqs: %d\n", ret);
2803 goto err_free_pipes;
2806 ath10k_info(ar, "pci irq %s interrupts %d irq_mode %d reset_mode %d\n",
2807 ath10k_pci_get_irq_method(ar), ar_pci->num_msi_intrs,
2808 ath10k_pci_irq_mode, ath10k_pci_reset_mode);
2810 ret = ath10k_pci_request_irq(ar);
2812 ath10k_warn(ar, "failed to request irqs: %d\n", ret);
2813 goto err_deinit_irq;
2816 ret = ath10k_pci_chip_reset(ar);
2818 ath10k_err(ar, "failed to reset chip: %d\n", ret);
2822 chip_id = ath10k_pci_soc_read32(ar, SOC_CHIP_ID_ADDRESS);
2823 if (chip_id == 0xffffffff) {
2824 ath10k_err(ar, "failed to get chip id\n");
2828 if (!ath10k_pci_chip_is_supported(pdev->device, chip_id)) {
2829 ath10k_err(ar, "device %04x with chip_id %08x isn't supported\n",
2830 pdev->device, chip_id);
2834 ret = ath10k_core_register(ar, chip_id);
2836 ath10k_err(ar, "failed to register driver core: %d\n", ret);
2843 ath10k_pci_free_irq(ar);
2844 ath10k_pci_kill_tasklet(ar);
2847 ath10k_pci_deinit_irq(ar);
2850 ath10k_pci_free_pipes(ar);
2853 ath10k_pci_release(ar);
2856 ath10k_core_destroy(ar);
2861 static void ath10k_pci_remove(struct pci_dev *pdev)
2863 struct ath10k *ar = pci_get_drvdata(pdev);
2864 struct ath10k_pci *ar_pci;
2866 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci remove\n");
2871 ar_pci = ath10k_pci_priv(ar);
2876 ath10k_core_unregister(ar);
2877 ath10k_pci_free_irq(ar);
2878 ath10k_pci_kill_tasklet(ar);
2879 ath10k_pci_deinit_irq(ar);
2880 ath10k_pci_ce_deinit(ar);
2881 ath10k_pci_free_pipes(ar);
2882 ath10k_pci_sleep_sync(ar);
2883 ath10k_pci_release(ar);
2884 ath10k_core_destroy(ar);
2887 MODULE_DEVICE_TABLE(pci, ath10k_pci_id_table);
2889 static struct pci_driver ath10k_pci_driver = {
2890 .name = "ath10k_pci",
2891 .id_table = ath10k_pci_id_table,
2892 .probe = ath10k_pci_probe,
2893 .remove = ath10k_pci_remove,
2896 static int __init ath10k_pci_init(void)
2900 ret = pci_register_driver(&ath10k_pci_driver);
2902 printk(KERN_ERR "failed to register ath10k pci driver: %d\n",
2907 module_init(ath10k_pci_init);
2909 static void __exit ath10k_pci_exit(void)
2911 pci_unregister_driver(&ath10k_pci_driver);
2914 module_exit(ath10k_pci_exit);
2916 MODULE_AUTHOR("Qualcomm Atheros");
2917 MODULE_DESCRIPTION("Driver support for Atheros QCA988X PCIe devices");
2918 MODULE_LICENSE("Dual BSD/GPL");
2920 /* QCA988x 2.0 firmware files */
2921 MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_FW_FILE);
2922 MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API2_FILE);
2923 MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API3_FILE);
2924 MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API4_FILE);
2925 MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API5_FILE);
2926 MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_BOARD_DATA_FILE);
2928 /* QCA6174 2.1 firmware files */
2929 MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" ATH10K_FW_API4_FILE);
2930 MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" QCA6174_HW_2_1_BOARD_DATA_FILE);
2932 /* QCA6174 3.1 firmware files */
2933 MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" ATH10K_FW_API4_FILE);
2934 MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" QCA6174_HW_3_0_BOARD_DATA_FILE);