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)
62 static DEFINE_PCI_DEVICE_TABLE(ath10k_pci_id_table) = {
63 { PCI_VDEVICE(ATHEROS, QCA988X_2_0_DEVICE_ID) }, /* PCI-E QCA988X V2 */
67 static int ath10k_pci_diag_read_access(struct ath10k *ar, u32 address,
70 static int ath10k_pci_post_rx(struct ath10k *ar);
71 static int ath10k_pci_post_rx_pipe(struct ath10k_pci_pipe *pipe_info,
73 static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe *pipe_info);
74 static int ath10k_pci_cold_reset(struct ath10k *ar);
75 static int ath10k_pci_warm_reset(struct ath10k *ar);
76 static int ath10k_pci_wait_for_target_init(struct ath10k *ar);
77 static int ath10k_pci_init_irq(struct ath10k *ar);
78 static int ath10k_pci_deinit_irq(struct ath10k *ar);
79 static int ath10k_pci_request_irq(struct ath10k *ar);
80 static void ath10k_pci_free_irq(struct ath10k *ar);
81 static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
82 struct ath10k_ce_pipe *rx_pipe,
83 struct bmi_xfer *xfer);
85 static const struct ce_attr host_ce_config_wlan[] = {
86 /* CE0: host->target HTC control and raw streams */
88 .flags = CE_ATTR_FLAGS,
94 /* CE1: target->host HTT + HTC control */
96 .flags = CE_ATTR_FLAGS,
102 /* CE2: target->host WMI */
104 .flags = CE_ATTR_FLAGS,
110 /* CE3: host->target WMI */
112 .flags = CE_ATTR_FLAGS,
118 /* CE4: host->target HTT */
120 .flags = CE_ATTR_FLAGS | CE_ATTR_DIS_INTR,
121 .src_nentries = CE_HTT_H2T_MSG_SRC_NENTRIES,
128 .flags = CE_ATTR_FLAGS,
134 /* CE6: target autonomous hif_memcpy */
136 .flags = CE_ATTR_FLAGS,
142 /* CE7: ce_diag, the Diagnostic Window */
144 .flags = CE_ATTR_FLAGS,
146 .src_sz_max = DIAG_TRANSFER_LIMIT,
151 /* Target firmware's Copy Engine configuration. */
152 static const struct ce_pipe_config target_ce_config_wlan[] = {
153 /* CE0: host->target HTC control and raw streams */
156 .pipedir = PIPEDIR_OUT,
159 .flags = CE_ATTR_FLAGS,
163 /* CE1: target->host HTT + HTC control */
166 .pipedir = PIPEDIR_IN,
169 .flags = CE_ATTR_FLAGS,
173 /* CE2: target->host WMI */
176 .pipedir = PIPEDIR_IN,
179 .flags = CE_ATTR_FLAGS,
183 /* CE3: host->target WMI */
186 .pipedir = PIPEDIR_OUT,
189 .flags = CE_ATTR_FLAGS,
193 /* CE4: host->target HTT */
196 .pipedir = PIPEDIR_OUT,
199 .flags = CE_ATTR_FLAGS,
203 /* NB: 50% of src nentries, since tx has 2 frags */
208 .pipedir = PIPEDIR_OUT,
211 .flags = CE_ATTR_FLAGS,
215 /* CE6: Reserved for target autonomous hif_memcpy */
218 .pipedir = PIPEDIR_INOUT,
221 .flags = CE_ATTR_FLAGS,
225 /* CE7 used only by Host */
228 static bool ath10k_pci_irq_pending(struct ath10k *ar)
232 /* Check if the shared legacy irq is for us */
233 cause = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
234 PCIE_INTR_CAUSE_ADDRESS);
235 if (cause & (PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL))
241 static void ath10k_pci_disable_and_clear_legacy_irq(struct ath10k *ar)
243 /* IMPORTANT: INTR_CLR register has to be set after
244 * INTR_ENABLE is set to 0, otherwise interrupt can not be
246 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
248 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_CLR_ADDRESS,
249 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
251 /* IMPORTANT: this extra read transaction is required to
252 * flush the posted write buffer. */
253 (void) ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
254 PCIE_INTR_ENABLE_ADDRESS);
257 static void ath10k_pci_enable_legacy_irq(struct ath10k *ar)
259 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
260 PCIE_INTR_ENABLE_ADDRESS,
261 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
263 /* IMPORTANT: this extra read transaction is required to
264 * flush the posted write buffer. */
265 (void) ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
266 PCIE_INTR_ENABLE_ADDRESS);
269 static irqreturn_t ath10k_pci_early_irq_handler(int irq, void *arg)
271 struct ath10k *ar = arg;
272 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
274 if (ar_pci->num_msi_intrs == 0) {
275 if (!ath10k_pci_irq_pending(ar))
278 ath10k_pci_disable_and_clear_legacy_irq(ar);
281 tasklet_schedule(&ar_pci->early_irq_tasklet);
286 static int ath10k_pci_request_early_irq(struct ath10k *ar)
288 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
291 /* Regardless whether MSI-X/MSI/legacy irqs have been set up the first
292 * interrupt from irq vector is triggered in all cases for FW
293 * indication/errors */
294 ret = request_irq(ar_pci->pdev->irq, ath10k_pci_early_irq_handler,
295 IRQF_SHARED, "ath10k_pci (early)", ar);
297 ath10k_warn("failed to request early irq: %d\n", ret);
304 static void ath10k_pci_free_early_irq(struct ath10k *ar)
306 free_irq(ath10k_pci_priv(ar)->pdev->irq, ar);
309 static inline const char *ath10k_pci_get_irq_method(struct ath10k *ar)
311 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
313 if (ar_pci->num_msi_intrs > 1)
315 else if (ar_pci->num_msi_intrs == 1)
322 * Diagnostic read/write access is provided for startup/config/debug usage.
323 * Caller must guarantee proper alignment, when applicable, and single user
326 static int ath10k_pci_diag_read_mem(struct ath10k *ar, u32 address, void *data,
329 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
332 unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
335 struct ath10k_ce_pipe *ce_diag;
336 /* Host buffer address in CE space */
338 dma_addr_t ce_data_base = 0;
339 void *data_buf = NULL;
343 * This code cannot handle reads to non-memory space. Redirect to the
344 * register read fn but preserve the multi word read capability of
347 if (address < DRAM_BASE_ADDRESS) {
348 if (!IS_ALIGNED(address, 4) ||
349 !IS_ALIGNED((unsigned long)data, 4))
352 while ((nbytes >= 4) && ((ret = ath10k_pci_diag_read_access(
353 ar, address, (u32 *)data)) == 0)) {
354 nbytes -= sizeof(u32);
355 address += sizeof(u32);
361 ce_diag = ar_pci->ce_diag;
364 * Allocate a temporary bounce buffer to hold caller's data
365 * to be DMA'ed from Target. This guarantees
366 * 1) 4-byte alignment
367 * 2) Buffer in DMA-able space
369 orig_nbytes = nbytes;
370 data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
379 memset(data_buf, 0, orig_nbytes);
381 remaining_bytes = orig_nbytes;
382 ce_data = ce_data_base;
383 while (remaining_bytes) {
384 nbytes = min_t(unsigned int, remaining_bytes,
385 DIAG_TRANSFER_LIMIT);
387 ret = ath10k_ce_recv_buf_enqueue(ce_diag, NULL, ce_data);
391 /* Request CE to send from Target(!) address to Host buffer */
393 * The address supplied by the caller is in the
394 * Target CPU virtual address space.
396 * In order to use this address with the diagnostic CE,
397 * convert it from Target CPU virtual address space
398 * to CE address space
400 address = TARG_CPU_SPACE_TO_CE_SPACE(ar, ar_pci->mem,
403 ret = ath10k_ce_send(ce_diag, NULL, (u32)address, nbytes, 0,
409 while (ath10k_ce_completed_send_next(ce_diag, NULL, &buf,
413 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
419 if (nbytes != completed_nbytes) {
424 if (buf != (u32) address) {
430 while (ath10k_ce_completed_recv_next(ce_diag, NULL, &buf,
435 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
441 if (nbytes != completed_nbytes) {
446 if (buf != ce_data) {
451 remaining_bytes -= nbytes;
458 /* Copy data from allocated DMA buf to caller's buf */
459 WARN_ON_ONCE(orig_nbytes & 3);
460 for (i = 0; i < orig_nbytes / sizeof(__le32); i++) {
462 __le32_to_cpu(((__le32 *)data_buf)[i]);
465 ath10k_warn("failed to read diag value at 0x%x: %d\n",
469 dma_free_coherent(ar->dev, orig_nbytes, data_buf,
475 static int ath10k_pci_diag_read32(struct ath10k *ar, u32 address, u32 *value)
477 return ath10k_pci_diag_read_mem(ar, address, value, sizeof(u32));
480 static int __ath10k_pci_diag_read_hi(struct ath10k *ar, void *dest,
486 host_addr = host_interest_item_address(src);
488 ret = ath10k_pci_diag_read32(ar, host_addr, &addr);
490 ath10k_warn("failed to get memcpy hi address for firmware address %d: %d\n",
495 ret = ath10k_pci_diag_read_mem(ar, addr, dest, len);
497 ath10k_warn("failed to memcpy firmware memory from %d (%d B): %d\n",
505 #define ath10k_pci_diag_read_hi(ar, dest, src, len) \
506 __ath10k_pci_diag_read_hi(ar, dest, HI_ITEM(src), len);
508 /* Read 4-byte aligned data from Target memory or register */
509 static int ath10k_pci_diag_read_access(struct ath10k *ar, u32 address,
512 /* Assume range doesn't cross this boundary */
513 if (address >= DRAM_BASE_ADDRESS)
514 return ath10k_pci_diag_read32(ar, address, data);
516 *data = ath10k_pci_read32(ar, address);
520 static int ath10k_pci_diag_write_mem(struct ath10k *ar, u32 address,
521 const void *data, int nbytes)
523 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
526 unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
529 struct ath10k_ce_pipe *ce_diag;
530 void *data_buf = NULL;
531 u32 ce_data; /* Host buffer address in CE space */
532 dma_addr_t ce_data_base = 0;
535 ce_diag = ar_pci->ce_diag;
538 * Allocate a temporary bounce buffer to hold caller's data
539 * to be DMA'ed to Target. This guarantees
540 * 1) 4-byte alignment
541 * 2) Buffer in DMA-able space
543 orig_nbytes = nbytes;
544 data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
553 /* Copy caller's data to allocated DMA buf */
554 WARN_ON_ONCE(orig_nbytes & 3);
555 for (i = 0; i < orig_nbytes / sizeof(__le32); i++)
556 ((__le32 *)data_buf)[i] = __cpu_to_le32(((u32 *)data)[i]);
559 * The address supplied by the caller is in the
560 * Target CPU virtual address space.
562 * In order to use this address with the diagnostic CE,
564 * Target CPU virtual address space
568 address = TARG_CPU_SPACE_TO_CE_SPACE(ar, ar_pci->mem, address);
570 remaining_bytes = orig_nbytes;
571 ce_data = ce_data_base;
572 while (remaining_bytes) {
573 /* FIXME: check cast */
574 nbytes = min_t(int, remaining_bytes, DIAG_TRANSFER_LIMIT);
576 /* Set up to receive directly into Target(!) address */
577 ret = ath10k_ce_recv_buf_enqueue(ce_diag, NULL, address);
582 * Request CE to send caller-supplied data that
583 * was copied to bounce buffer to Target(!) address.
585 ret = ath10k_ce_send(ce_diag, NULL, (u32) ce_data,
591 while (ath10k_ce_completed_send_next(ce_diag, NULL, &buf,
596 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
602 if (nbytes != completed_nbytes) {
607 if (buf != ce_data) {
613 while (ath10k_ce_completed_recv_next(ce_diag, NULL, &buf,
618 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
624 if (nbytes != completed_nbytes) {
629 if (buf != address) {
634 remaining_bytes -= nbytes;
641 dma_free_coherent(ar->dev, orig_nbytes, data_buf,
646 ath10k_warn("failed to write diag value at 0x%x: %d\n",
652 /* Write 4B data to Target memory or register */
653 static int ath10k_pci_diag_write_access(struct ath10k *ar, u32 address,
656 /* Assume range doesn't cross this boundary */
657 if (address >= DRAM_BASE_ADDRESS)
658 return ath10k_pci_diag_write_mem(ar, address, &data,
661 ath10k_pci_write32(ar, address, data);
665 static bool ath10k_pci_is_awake(struct ath10k *ar)
667 u32 val = ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS);
669 return RTC_STATE_V_GET(val) == RTC_STATE_V_ON;
672 static int ath10k_pci_wake_wait(struct ath10k *ar)
677 while (tot_delay < PCIE_WAKE_TIMEOUT) {
678 if (ath10k_pci_is_awake(ar))
682 tot_delay += curr_delay;
691 static int ath10k_pci_wake(struct ath10k *ar)
693 ath10k_pci_reg_write32(ar, PCIE_SOC_WAKE_ADDRESS,
694 PCIE_SOC_WAKE_V_MASK);
695 return ath10k_pci_wake_wait(ar);
698 static void ath10k_pci_sleep(struct ath10k *ar)
700 ath10k_pci_reg_write32(ar, PCIE_SOC_WAKE_ADDRESS,
701 PCIE_SOC_WAKE_RESET);
704 /* Called by lower (CE) layer when a send to Target completes. */
705 static void ath10k_pci_ce_send_done(struct ath10k_ce_pipe *ce_state)
707 struct ath10k *ar = ce_state->ar;
708 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
709 struct ath10k_hif_cb *cb = &ar_pci->msg_callbacks_current;
710 void *transfer_context;
713 unsigned int transfer_id;
715 while (ath10k_ce_completed_send_next(ce_state, &transfer_context,
717 &transfer_id) == 0) {
718 /* no need to call tx completion for NULL pointers */
719 if (transfer_context == NULL)
722 cb->tx_completion(ar, transfer_context, transfer_id);
726 /* Called by lower (CE) layer when data is received from the Target. */
727 static void ath10k_pci_ce_recv_data(struct ath10k_ce_pipe *ce_state)
729 struct ath10k *ar = ce_state->ar;
730 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
731 struct ath10k_pci_pipe *pipe_info = &ar_pci->pipe_info[ce_state->id];
732 struct ath10k_hif_cb *cb = &ar_pci->msg_callbacks_current;
734 void *transfer_context;
736 unsigned int nbytes, max_nbytes;
737 unsigned int transfer_id;
739 int err, num_replenish = 0;
741 while (ath10k_ce_completed_recv_next(ce_state, &transfer_context,
742 &ce_data, &nbytes, &transfer_id,
745 skb = transfer_context;
746 max_nbytes = skb->len + skb_tailroom(skb);
747 dma_unmap_single(ar->dev, ATH10K_SKB_CB(skb)->paddr,
748 max_nbytes, DMA_FROM_DEVICE);
750 if (unlikely(max_nbytes < nbytes)) {
751 ath10k_warn("rxed more than expected (nbytes %d, max %d)",
753 dev_kfree_skb_any(skb);
757 skb_put(skb, nbytes);
758 cb->rx_completion(ar, skb, pipe_info->pipe_num);
761 err = ath10k_pci_post_rx_pipe(pipe_info, num_replenish);
764 ath10k_warn("failed to replenish CE rx ring %d (%d bufs): %d\n",
765 pipe_info->pipe_num, num_replenish, err);
769 static int ath10k_pci_hif_tx_sg(struct ath10k *ar, u8 pipe_id,
770 struct ath10k_hif_sg_item *items, int n_items)
772 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
773 struct ath10k_pci_pipe *pci_pipe = &ar_pci->pipe_info[pipe_id];
774 struct ath10k_ce_pipe *ce_pipe = pci_pipe->ce_hdl;
775 struct ath10k_ce_ring *src_ring = ce_pipe->src_ring;
776 unsigned int nentries_mask;
777 unsigned int sw_index;
778 unsigned int write_index;
781 spin_lock_bh(&ar_pci->ce_lock);
783 nentries_mask = src_ring->nentries_mask;
784 sw_index = src_ring->sw_index;
785 write_index = src_ring->write_index;
787 if (unlikely(CE_RING_DELTA(nentries_mask,
788 write_index, sw_index - 1) < n_items)) {
793 for (i = 0; i < n_items - 1; i++) {
794 ath10k_dbg(ATH10K_DBG_PCI,
795 "pci tx item %d paddr 0x%08x len %d n_items %d\n",
796 i, items[i].paddr, items[i].len, n_items);
797 ath10k_dbg_dump(ATH10K_DBG_PCI_DUMP, NULL, "item data: ",
798 items[i].vaddr, items[i].len);
800 err = ath10k_ce_send_nolock(ce_pipe,
801 items[i].transfer_context,
804 items[i].transfer_id,
805 CE_SEND_FLAG_GATHER);
810 /* `i` is equal to `n_items -1` after for() */
812 ath10k_dbg(ATH10K_DBG_PCI,
813 "pci tx item %d paddr 0x%08x len %d n_items %d\n",
814 i, items[i].paddr, items[i].len, n_items);
815 ath10k_dbg_dump(ATH10K_DBG_PCI_DUMP, NULL, "item data: ",
816 items[i].vaddr, items[i].len);
818 err = ath10k_ce_send_nolock(ce_pipe,
819 items[i].transfer_context,
822 items[i].transfer_id,
827 spin_unlock_bh(&ar_pci->ce_lock);
832 __ath10k_ce_send_revert(ce_pipe);
834 spin_unlock_bh(&ar_pci->ce_lock);
838 static u16 ath10k_pci_hif_get_free_queue_number(struct ath10k *ar, u8 pipe)
840 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
842 ath10k_dbg(ATH10K_DBG_PCI, "pci hif get free queue number\n");
844 return ath10k_ce_num_free_src_entries(ar_pci->pipe_info[pipe].ce_hdl);
847 static void ath10k_pci_dump_registers(struct ath10k *ar,
848 struct ath10k_fw_crash_data *crash_data)
850 u32 i, reg_dump_values[REG_DUMP_COUNT_QCA988X] = {};
853 lockdep_assert_held(&ar->data_lock);
855 ret = ath10k_pci_diag_read_hi(ar, ®_dump_values[0],
857 REG_DUMP_COUNT_QCA988X * sizeof(u32));
859 ath10k_err("failed to read firmware dump area: %d\n", ret);
863 BUILD_BUG_ON(REG_DUMP_COUNT_QCA988X % 4);
865 ath10k_err("firmware register dump:\n");
866 for (i = 0; i < REG_DUMP_COUNT_QCA988X; i += 4)
867 ath10k_err("[%02d]: 0x%08X 0x%08X 0x%08X 0x%08X\n",
870 reg_dump_values[i + 1],
871 reg_dump_values[i + 2],
872 reg_dump_values[i + 3]);
874 /* crash_data is in little endian */
875 for (i = 0; i < REG_DUMP_COUNT_QCA988X; i++)
876 crash_data->registers[i] = cpu_to_le32(reg_dump_values[i]);
879 static void ath10k_pci_fw_crashed_dump(struct ath10k *ar)
881 struct ath10k_fw_crash_data *crash_data;
884 spin_lock_bh(&ar->data_lock);
886 crash_data = ath10k_debug_get_new_fw_crash_data(ar);
889 scnprintf(uuid, sizeof(uuid), "%pUl", &crash_data->uuid);
891 scnprintf(uuid, sizeof(uuid), "n/a");
893 ath10k_err("firmware crashed! (uuid %s)\n", uuid);
894 ath10k_print_driver_info(ar);
899 ath10k_pci_dump_registers(ar, crash_data);
902 spin_unlock_bh(&ar->data_lock);
904 queue_work(ar->workqueue, &ar->restart_work);
907 static void ath10k_pci_hif_send_complete_check(struct ath10k *ar, u8 pipe,
910 ath10k_dbg(ATH10K_DBG_PCI, "pci hif send complete check\n");
915 * Decide whether to actually poll for completions, or just
916 * wait for a later chance.
917 * If there seem to be plenty of resources left, then just wait
918 * since checking involves reading a CE register, which is a
919 * relatively expensive operation.
921 resources = ath10k_pci_hif_get_free_queue_number(ar, pipe);
924 * If at least 50% of the total resources are still available,
925 * don't bother checking again yet.
927 if (resources > (host_ce_config_wlan[pipe].src_nentries >> 1))
930 ath10k_ce_per_engine_service(ar, pipe);
933 static void ath10k_pci_hif_set_callbacks(struct ath10k *ar,
934 struct ath10k_hif_cb *callbacks)
936 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
938 ath10k_dbg(ATH10K_DBG_PCI, "pci hif set callbacks\n");
940 memcpy(&ar_pci->msg_callbacks_current, callbacks,
941 sizeof(ar_pci->msg_callbacks_current));
944 static void ath10k_pci_kill_tasklet(struct ath10k *ar)
946 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
949 tasklet_kill(&ar_pci->intr_tq);
950 tasklet_kill(&ar_pci->msi_fw_err);
951 tasklet_kill(&ar_pci->early_irq_tasklet);
953 for (i = 0; i < CE_COUNT; i++)
954 tasklet_kill(&ar_pci->pipe_info[i].intr);
957 /* TODO - temporary mapping while we have too few CE's */
958 static int ath10k_pci_hif_map_service_to_pipe(struct ath10k *ar,
959 u16 service_id, u8 *ul_pipe,
960 u8 *dl_pipe, int *ul_is_polled,
965 ath10k_dbg(ATH10K_DBG_PCI, "pci hif map service\n");
967 /* polling for received messages not supported */
970 switch (service_id) {
971 case ATH10K_HTC_SVC_ID_HTT_DATA_MSG:
973 * Host->target HTT gets its own pipe, so it can be polled
974 * while other pipes are interrupt driven.
978 * Use the same target->host pipe for HTC ctrl, HTC raw
984 case ATH10K_HTC_SVC_ID_RSVD_CTRL:
985 case ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS:
987 * Note: HTC_RAW_STREAMS_SVC is currently unused, and
988 * HTC_CTRL_RSVD_SVC could share the same pipe as the
989 * WMI services. So, if another CE is needed, change
990 * this to *ul_pipe = 3, which frees up CE 0.
997 case ATH10K_HTC_SVC_ID_WMI_DATA_BK:
998 case ATH10K_HTC_SVC_ID_WMI_DATA_BE:
999 case ATH10K_HTC_SVC_ID_WMI_DATA_VI:
1000 case ATH10K_HTC_SVC_ID_WMI_DATA_VO:
1002 case ATH10K_HTC_SVC_ID_WMI_CONTROL:
1008 /* pipe 6 reserved */
1009 /* pipe 7 reserved */
1016 (host_ce_config_wlan[*ul_pipe].flags & CE_ATTR_DIS_INTR) != 0;
1021 static void ath10k_pci_hif_get_default_pipe(struct ath10k *ar,
1022 u8 *ul_pipe, u8 *dl_pipe)
1024 int ul_is_polled, dl_is_polled;
1026 ath10k_dbg(ATH10K_DBG_PCI, "pci hif get default pipe\n");
1028 (void)ath10k_pci_hif_map_service_to_pipe(ar,
1029 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1036 static int ath10k_pci_post_rx_pipe(struct ath10k_pci_pipe *pipe_info,
1039 struct ath10k *ar = pipe_info->hif_ce_state;
1040 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1041 struct ath10k_ce_pipe *ce_state = pipe_info->ce_hdl;
1042 struct sk_buff *skb;
1046 if (pipe_info->buf_sz == 0)
1049 for (i = 0; i < num; i++) {
1050 skb = dev_alloc_skb(pipe_info->buf_sz);
1052 ath10k_warn("failed to allocate skbuff for pipe %d\n",
1058 WARN_ONCE((unsigned long)skb->data & 3, "unaligned skb");
1060 ce_data = dma_map_single(ar->dev, skb->data,
1061 skb->len + skb_tailroom(skb),
1064 if (unlikely(dma_mapping_error(ar->dev, ce_data))) {
1065 ath10k_warn("failed to DMA map sk_buff\n");
1066 dev_kfree_skb_any(skb);
1071 ATH10K_SKB_CB(skb)->paddr = ce_data;
1073 pci_dma_sync_single_for_device(ar_pci->pdev, ce_data,
1075 PCI_DMA_FROMDEVICE);
1077 ret = ath10k_ce_recv_buf_enqueue(ce_state, (void *)skb,
1080 ath10k_warn("failed to enqueue to pipe %d: %d\n",
1089 ath10k_pci_rx_pipe_cleanup(pipe_info);
1093 static int ath10k_pci_post_rx(struct ath10k *ar)
1095 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1096 struct ath10k_pci_pipe *pipe_info;
1097 const struct ce_attr *attr;
1098 int pipe_num, ret = 0;
1100 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
1101 pipe_info = &ar_pci->pipe_info[pipe_num];
1102 attr = &host_ce_config_wlan[pipe_num];
1104 if (attr->dest_nentries == 0)
1107 ret = ath10k_pci_post_rx_pipe(pipe_info,
1108 attr->dest_nentries - 1);
1110 ath10k_warn("failed to post RX buffer for pipe %d: %d\n",
1113 for (; pipe_num >= 0; pipe_num--) {
1114 pipe_info = &ar_pci->pipe_info[pipe_num];
1115 ath10k_pci_rx_pipe_cleanup(pipe_info);
1124 static void ath10k_pci_irq_disable(struct ath10k *ar)
1126 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1129 ath10k_ce_disable_interrupts(ar);
1131 /* Regardless how many interrupts were assigned for MSI the first one
1132 * is always used for firmware indications (crashes). There's no way to
1133 * mask the irq in the device so call disable_irq(). Legacy (shared)
1134 * interrupts can be masked on the device though.
1136 if (ar_pci->num_msi_intrs > 0)
1137 disable_irq(ar_pci->pdev->irq);
1139 ath10k_pci_disable_and_clear_legacy_irq(ar);
1141 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
1142 synchronize_irq(ar_pci->pdev->irq + i);
1145 static void ath10k_pci_irq_enable(struct ath10k *ar)
1147 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1149 ath10k_ce_enable_interrupts(ar);
1151 /* See comment in ath10k_pci_irq_disable() */
1152 if (ar_pci->num_msi_intrs > 0)
1153 enable_irq(ar_pci->pdev->irq);
1155 ath10k_pci_enable_legacy_irq(ar);
1158 static int ath10k_pci_hif_start(struct ath10k *ar)
1160 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1163 ath10k_dbg(ATH10K_DBG_BOOT, "boot hif start\n");
1165 ath10k_pci_free_early_irq(ar);
1166 ath10k_pci_kill_tasklet(ar);
1168 ret = ath10k_pci_request_irq(ar);
1170 ath10k_warn("failed to post RX buffers for all pipes: %d\n",
1175 ath10k_pci_irq_enable(ar);
1177 /* Post buffers once to start things off. */
1178 ret = ath10k_pci_post_rx(ar);
1180 ath10k_warn("failed to post RX buffers for all pipes: %d\n",
1185 ar_pci->started = 1;
1189 ath10k_pci_irq_disable(ar);
1190 ath10k_pci_free_irq(ar);
1191 ath10k_pci_kill_tasklet(ar);
1193 /* Though there should be no interrupts (device was reset)
1194 * power_down() expects the early IRQ to be installed as per the
1195 * driver lifecycle. */
1196 ret_early = ath10k_pci_request_early_irq(ar);
1198 ath10k_warn("failed to re-enable early irq: %d\n", ret_early);
1203 static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe *pipe_info)
1206 struct ath10k_pci *ar_pci;
1207 struct ath10k_ce_pipe *ce_hdl;
1209 struct sk_buff *netbuf;
1212 buf_sz = pipe_info->buf_sz;
1214 /* Unused Copy Engine */
1218 ar = pipe_info->hif_ce_state;
1219 ar_pci = ath10k_pci_priv(ar);
1221 if (!ar_pci->started)
1224 ce_hdl = pipe_info->ce_hdl;
1226 while (ath10k_ce_revoke_recv_next(ce_hdl, (void **)&netbuf,
1228 dma_unmap_single(ar->dev, ATH10K_SKB_CB(netbuf)->paddr,
1229 netbuf->len + skb_tailroom(netbuf),
1231 dev_kfree_skb_any(netbuf);
1235 static void ath10k_pci_tx_pipe_cleanup(struct ath10k_pci_pipe *pipe_info)
1238 struct ath10k_pci *ar_pci;
1239 struct ath10k_ce_pipe *ce_hdl;
1240 struct sk_buff *netbuf;
1242 unsigned int nbytes;
1246 buf_sz = pipe_info->buf_sz;
1248 /* Unused Copy Engine */
1252 ar = pipe_info->hif_ce_state;
1253 ar_pci = ath10k_pci_priv(ar);
1255 if (!ar_pci->started)
1258 ce_hdl = pipe_info->ce_hdl;
1260 while (ath10k_ce_cancel_send_next(ce_hdl, (void **)&netbuf,
1261 &ce_data, &nbytes, &id) == 0) {
1262 /* no need to call tx completion for NULL pointers */
1266 ar_pci->msg_callbacks_current.tx_completion(ar,
1273 * Cleanup residual buffers for device shutdown:
1274 * buffers that were enqueued for receive
1275 * buffers that were to be sent
1276 * Note: Buffers that had completed but which were
1277 * not yet processed are on a completion queue. They
1278 * are handled when the completion thread shuts down.
1280 static void ath10k_pci_buffer_cleanup(struct ath10k *ar)
1282 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1285 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
1286 struct ath10k_pci_pipe *pipe_info;
1288 pipe_info = &ar_pci->pipe_info[pipe_num];
1289 ath10k_pci_rx_pipe_cleanup(pipe_info);
1290 ath10k_pci_tx_pipe_cleanup(pipe_info);
1294 static void ath10k_pci_ce_deinit(struct ath10k *ar)
1298 for (i = 0; i < CE_COUNT; i++)
1299 ath10k_ce_deinit_pipe(ar, i);
1302 static void ath10k_pci_hif_stop(struct ath10k *ar)
1304 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1307 ath10k_dbg(ATH10K_DBG_BOOT, "boot hif stop\n");
1309 if (WARN_ON(!ar_pci->started))
1312 ath10k_pci_irq_disable(ar);
1313 ath10k_pci_free_irq(ar);
1314 ath10k_pci_kill_tasklet(ar);
1316 ret = ath10k_pci_request_early_irq(ar);
1318 ath10k_warn("failed to re-enable early irq: %d\n", ret);
1320 /* At this point, asynchronous threads are stopped, the target should
1321 * not DMA nor interrupt. We process the leftovers and then free
1322 * everything else up. */
1324 ath10k_pci_buffer_cleanup(ar);
1326 /* Make the sure the device won't access any structures on the host by
1327 * resetting it. The device was fed with PCI CE ringbuffer
1328 * configuration during init. If ringbuffers are freed and the device
1329 * were to access them this could lead to memory corruption on the
1331 ath10k_pci_warm_reset(ar);
1333 ar_pci->started = 0;
1336 static int ath10k_pci_hif_exchange_bmi_msg(struct ath10k *ar,
1337 void *req, u32 req_len,
1338 void *resp, u32 *resp_len)
1340 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1341 struct ath10k_pci_pipe *pci_tx = &ar_pci->pipe_info[BMI_CE_NUM_TO_TARG];
1342 struct ath10k_pci_pipe *pci_rx = &ar_pci->pipe_info[BMI_CE_NUM_TO_HOST];
1343 struct ath10k_ce_pipe *ce_tx = pci_tx->ce_hdl;
1344 struct ath10k_ce_pipe *ce_rx = pci_rx->ce_hdl;
1345 dma_addr_t req_paddr = 0;
1346 dma_addr_t resp_paddr = 0;
1347 struct bmi_xfer xfer = {};
1348 void *treq, *tresp = NULL;
1353 if (resp && !resp_len)
1356 if (resp && resp_len && *resp_len == 0)
1359 treq = kmemdup(req, req_len, GFP_KERNEL);
1363 req_paddr = dma_map_single(ar->dev, treq, req_len, DMA_TO_DEVICE);
1364 ret = dma_mapping_error(ar->dev, req_paddr);
1368 if (resp && resp_len) {
1369 tresp = kzalloc(*resp_len, GFP_KERNEL);
1375 resp_paddr = dma_map_single(ar->dev, tresp, *resp_len,
1377 ret = dma_mapping_error(ar->dev, resp_paddr);
1381 xfer.wait_for_resp = true;
1384 ath10k_ce_recv_buf_enqueue(ce_rx, &xfer, resp_paddr);
1387 ret = ath10k_ce_send(ce_tx, &xfer, req_paddr, req_len, -1, 0);
1391 ret = ath10k_pci_bmi_wait(ce_tx, ce_rx, &xfer);
1394 unsigned int unused_nbytes;
1395 unsigned int unused_id;
1397 ath10k_ce_cancel_send_next(ce_tx, NULL, &unused_buffer,
1398 &unused_nbytes, &unused_id);
1400 /* non-zero means we did not time out */
1408 ath10k_ce_revoke_recv_next(ce_rx, NULL, &unused_buffer);
1409 dma_unmap_single(ar->dev, resp_paddr,
1410 *resp_len, DMA_FROM_DEVICE);
1413 dma_unmap_single(ar->dev, req_paddr, req_len, DMA_TO_DEVICE);
1415 if (ret == 0 && resp_len) {
1416 *resp_len = min(*resp_len, xfer.resp_len);
1417 memcpy(resp, tresp, xfer.resp_len);
1426 static void ath10k_pci_bmi_send_done(struct ath10k_ce_pipe *ce_state)
1428 struct bmi_xfer *xfer;
1430 unsigned int nbytes;
1431 unsigned int transfer_id;
1433 if (ath10k_ce_completed_send_next(ce_state, (void **)&xfer, &ce_data,
1434 &nbytes, &transfer_id))
1437 xfer->tx_done = true;
1440 static void ath10k_pci_bmi_recv_data(struct ath10k_ce_pipe *ce_state)
1442 struct bmi_xfer *xfer;
1444 unsigned int nbytes;
1445 unsigned int transfer_id;
1448 if (ath10k_ce_completed_recv_next(ce_state, (void **)&xfer, &ce_data,
1449 &nbytes, &transfer_id, &flags))
1452 if (!xfer->wait_for_resp) {
1453 ath10k_warn("unexpected: BMI data received; ignoring\n");
1457 xfer->resp_len = nbytes;
1458 xfer->rx_done = true;
1461 static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
1462 struct ath10k_ce_pipe *rx_pipe,
1463 struct bmi_xfer *xfer)
1465 unsigned long timeout = jiffies + BMI_COMMUNICATION_TIMEOUT_HZ;
1467 while (time_before_eq(jiffies, timeout)) {
1468 ath10k_pci_bmi_send_done(tx_pipe);
1469 ath10k_pci_bmi_recv_data(rx_pipe);
1471 if (xfer->tx_done && (xfer->rx_done == xfer->wait_for_resp))
1481 * Map from service/endpoint to Copy Engine.
1482 * This table is derived from the CE_PCI TABLE, above.
1483 * It is passed to the Target at startup for use by firmware.
1485 static const struct service_to_pipe target_service_to_ce_map_wlan[] = {
1487 ATH10K_HTC_SVC_ID_WMI_DATA_VO,
1488 PIPEDIR_OUT, /* out = UL = host -> target */
1492 ATH10K_HTC_SVC_ID_WMI_DATA_VO,
1493 PIPEDIR_IN, /* in = DL = target -> host */
1497 ATH10K_HTC_SVC_ID_WMI_DATA_BK,
1498 PIPEDIR_OUT, /* out = UL = host -> target */
1502 ATH10K_HTC_SVC_ID_WMI_DATA_BK,
1503 PIPEDIR_IN, /* in = DL = target -> host */
1507 ATH10K_HTC_SVC_ID_WMI_DATA_BE,
1508 PIPEDIR_OUT, /* out = UL = host -> target */
1512 ATH10K_HTC_SVC_ID_WMI_DATA_BE,
1513 PIPEDIR_IN, /* in = DL = target -> host */
1517 ATH10K_HTC_SVC_ID_WMI_DATA_VI,
1518 PIPEDIR_OUT, /* out = UL = host -> target */
1522 ATH10K_HTC_SVC_ID_WMI_DATA_VI,
1523 PIPEDIR_IN, /* in = DL = target -> host */
1527 ATH10K_HTC_SVC_ID_WMI_CONTROL,
1528 PIPEDIR_OUT, /* out = UL = host -> target */
1532 ATH10K_HTC_SVC_ID_WMI_CONTROL,
1533 PIPEDIR_IN, /* in = DL = target -> host */
1537 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1538 PIPEDIR_OUT, /* out = UL = host -> target */
1539 0, /* could be moved to 3 (share with WMI) */
1542 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1543 PIPEDIR_IN, /* in = DL = target -> host */
1547 ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS, /* not currently used */
1548 PIPEDIR_OUT, /* out = UL = host -> target */
1552 ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS, /* not currently used */
1553 PIPEDIR_IN, /* in = DL = target -> host */
1557 ATH10K_HTC_SVC_ID_HTT_DATA_MSG,
1558 PIPEDIR_OUT, /* out = UL = host -> target */
1562 ATH10K_HTC_SVC_ID_HTT_DATA_MSG,
1563 PIPEDIR_IN, /* in = DL = target -> host */
1567 /* (Additions here) */
1569 { /* Must be last */
1577 * Send an interrupt to the device to wake up the Target CPU
1578 * so it has an opportunity to notice any changed state.
1580 static int ath10k_pci_wake_target_cpu(struct ath10k *ar)
1585 ret = ath10k_pci_diag_read_access(ar, SOC_CORE_BASE_ADDRESS |
1589 ath10k_warn("failed to read core_ctrl: %d\n", ret);
1593 /* A_INUM_FIRMWARE interrupt to Target CPU */
1594 core_ctrl |= CORE_CTRL_CPU_INTR_MASK;
1596 ret = ath10k_pci_diag_write_access(ar, SOC_CORE_BASE_ADDRESS |
1600 ath10k_warn("failed to set target CPU interrupt mask: %d\n",
1608 static int ath10k_pci_init_config(struct ath10k *ar)
1610 u32 interconnect_targ_addr;
1611 u32 pcie_state_targ_addr = 0;
1612 u32 pipe_cfg_targ_addr = 0;
1613 u32 svc_to_pipe_map = 0;
1614 u32 pcie_config_flags = 0;
1616 u32 ealloc_targ_addr;
1618 u32 flag2_targ_addr;
1621 /* Download to Target the CE Config and the service-to-CE map */
1622 interconnect_targ_addr =
1623 host_interest_item_address(HI_ITEM(hi_interconnect_state));
1625 /* Supply Target-side CE configuration */
1626 ret = ath10k_pci_diag_read_access(ar, interconnect_targ_addr,
1627 &pcie_state_targ_addr);
1629 ath10k_err("Failed to get pcie state addr: %d\n", ret);
1633 if (pcie_state_targ_addr == 0) {
1635 ath10k_err("Invalid pcie state addr\n");
1639 ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
1640 offsetof(struct pcie_state,
1642 &pipe_cfg_targ_addr);
1644 ath10k_err("Failed to get pipe cfg addr: %d\n", ret);
1648 if (pipe_cfg_targ_addr == 0) {
1650 ath10k_err("Invalid pipe cfg addr\n");
1654 ret = ath10k_pci_diag_write_mem(ar, pipe_cfg_targ_addr,
1655 target_ce_config_wlan,
1656 sizeof(target_ce_config_wlan));
1659 ath10k_err("Failed to write pipe cfg: %d\n", ret);
1663 ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
1664 offsetof(struct pcie_state,
1668 ath10k_err("Failed to get svc/pipe map: %d\n", ret);
1672 if (svc_to_pipe_map == 0) {
1674 ath10k_err("Invalid svc_to_pipe map\n");
1678 ret = ath10k_pci_diag_write_mem(ar, svc_to_pipe_map,
1679 target_service_to_ce_map_wlan,
1680 sizeof(target_service_to_ce_map_wlan));
1682 ath10k_err("Failed to write svc/pipe map: %d\n", ret);
1686 ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
1687 offsetof(struct pcie_state,
1689 &pcie_config_flags);
1691 ath10k_err("Failed to get pcie config_flags: %d\n", ret);
1695 pcie_config_flags &= ~PCIE_CONFIG_FLAG_ENABLE_L1;
1697 ret = ath10k_pci_diag_write_mem(ar, pcie_state_targ_addr +
1698 offsetof(struct pcie_state, config_flags),
1700 sizeof(pcie_config_flags));
1702 ath10k_err("Failed to write pcie config_flags: %d\n", ret);
1706 /* configure early allocation */
1707 ealloc_targ_addr = host_interest_item_address(HI_ITEM(hi_early_alloc));
1709 ret = ath10k_pci_diag_read_access(ar, ealloc_targ_addr, &ealloc_value);
1711 ath10k_err("Faile to get early alloc val: %d\n", ret);
1715 /* first bank is switched to IRAM */
1716 ealloc_value |= ((HI_EARLY_ALLOC_MAGIC << HI_EARLY_ALLOC_MAGIC_SHIFT) &
1717 HI_EARLY_ALLOC_MAGIC_MASK);
1718 ealloc_value |= ((1 << HI_EARLY_ALLOC_IRAM_BANKS_SHIFT) &
1719 HI_EARLY_ALLOC_IRAM_BANKS_MASK);
1721 ret = ath10k_pci_diag_write_access(ar, ealloc_targ_addr, ealloc_value);
1723 ath10k_err("Failed to set early alloc val: %d\n", ret);
1727 /* Tell Target to proceed with initialization */
1728 flag2_targ_addr = host_interest_item_address(HI_ITEM(hi_option_flag2));
1730 ret = ath10k_pci_diag_read_access(ar, flag2_targ_addr, &flag2_value);
1732 ath10k_err("Failed to get option val: %d\n", ret);
1736 flag2_value |= HI_OPTION_EARLY_CFG_DONE;
1738 ret = ath10k_pci_diag_write_access(ar, flag2_targ_addr, flag2_value);
1740 ath10k_err("Failed to set option val: %d\n", ret);
1747 static int ath10k_pci_alloc_ce(struct ath10k *ar)
1751 for (i = 0; i < CE_COUNT; i++) {
1752 ret = ath10k_ce_alloc_pipe(ar, i, &host_ce_config_wlan[i]);
1754 ath10k_err("failed to allocate copy engine pipe %d: %d\n",
1763 static void ath10k_pci_free_ce(struct ath10k *ar)
1767 for (i = 0; i < CE_COUNT; i++)
1768 ath10k_ce_free_pipe(ar, i);
1771 static int ath10k_pci_ce_init(struct ath10k *ar)
1773 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1774 struct ath10k_pci_pipe *pipe_info;
1775 const struct ce_attr *attr;
1778 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
1779 pipe_info = &ar_pci->pipe_info[pipe_num];
1780 pipe_info->ce_hdl = &ar_pci->ce_states[pipe_num];
1781 pipe_info->pipe_num = pipe_num;
1782 pipe_info->hif_ce_state = ar;
1783 attr = &host_ce_config_wlan[pipe_num];
1785 ret = ath10k_ce_init_pipe(ar, pipe_num, attr,
1786 ath10k_pci_ce_send_done,
1787 ath10k_pci_ce_recv_data);
1789 ath10k_err("failed to initialize copy engine pipe %d: %d\n",
1794 if (pipe_num == CE_COUNT - 1) {
1796 * Reserve the ultimate CE for
1797 * diagnostic Window support
1799 ar_pci->ce_diag = pipe_info->ce_hdl;
1803 pipe_info->buf_sz = (size_t) (attr->src_sz_max);
1809 static void ath10k_pci_fw_interrupt_handler(struct ath10k *ar)
1811 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1814 fw_indicator = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
1816 if (fw_indicator & FW_IND_EVENT_PENDING) {
1817 /* ACK: clear Target-side pending event */
1818 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS,
1819 fw_indicator & ~FW_IND_EVENT_PENDING);
1821 if (ar_pci->started) {
1822 ath10k_pci_fw_crashed_dump(ar);
1825 * Probable Target failure before we're prepared
1826 * to handle it. Generally unexpected.
1828 ath10k_warn("early firmware event indicated\n");
1833 /* this function effectively clears target memory controller assert line */
1834 static void ath10k_pci_warm_reset_si0(struct ath10k *ar)
1838 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1839 ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
1840 val | SOC_RESET_CONTROL_SI0_RST_MASK);
1841 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1845 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1846 ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
1847 val & ~SOC_RESET_CONTROL_SI0_RST_MASK);
1848 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1853 static int ath10k_pci_warm_reset(struct ath10k *ar)
1857 ath10k_dbg(ATH10K_DBG_BOOT, "boot warm reset\n");
1860 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1861 PCIE_INTR_CAUSE_ADDRESS);
1862 ath10k_dbg(ATH10K_DBG_BOOT, "boot host cpu intr cause: 0x%08x\n", val);
1864 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1866 ath10k_dbg(ATH10K_DBG_BOOT, "boot target cpu intr cause: 0x%08x\n",
1869 /* disable pending irqs */
1870 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
1871 PCIE_INTR_ENABLE_ADDRESS, 0);
1873 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
1874 PCIE_INTR_CLR_ADDRESS, ~0);
1878 /* clear fw indicator */
1879 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, 0);
1881 /* clear target LF timer interrupts */
1882 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1883 SOC_LF_TIMER_CONTROL0_ADDRESS);
1884 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS +
1885 SOC_LF_TIMER_CONTROL0_ADDRESS,
1886 val & ~SOC_LF_TIMER_CONTROL0_ENABLE_MASK);
1889 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1890 SOC_RESET_CONTROL_ADDRESS);
1891 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1892 val | SOC_RESET_CONTROL_CE_RST_MASK);
1893 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1894 SOC_RESET_CONTROL_ADDRESS);
1898 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1899 val & ~SOC_RESET_CONTROL_CE_RST_MASK);
1900 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1901 SOC_RESET_CONTROL_ADDRESS);
1904 ath10k_pci_warm_reset_si0(ar);
1907 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1908 PCIE_INTR_CAUSE_ADDRESS);
1909 ath10k_dbg(ATH10K_DBG_BOOT, "boot host cpu intr cause: 0x%08x\n", val);
1911 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1913 ath10k_dbg(ATH10K_DBG_BOOT, "boot target cpu intr cause: 0x%08x\n",
1916 /* CPU warm reset */
1917 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1918 SOC_RESET_CONTROL_ADDRESS);
1919 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1920 val | SOC_RESET_CONTROL_CPU_WARM_RST_MASK);
1922 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1923 SOC_RESET_CONTROL_ADDRESS);
1924 ath10k_dbg(ATH10K_DBG_BOOT, "boot target reset state: 0x%08x\n", val);
1928 ath10k_dbg(ATH10K_DBG_BOOT, "boot warm reset complete\n");
1933 static int __ath10k_pci_hif_power_up(struct ath10k *ar, bool cold_reset)
1938 * Bring the target up cleanly.
1940 * The target may be in an undefined state with an AUX-powered Target
1941 * and a Host in WoW mode. If the Host crashes, loses power, or is
1942 * restarted (without unloading the driver) then the Target is left
1943 * (aux) powered and running. On a subsequent driver load, the Target
1944 * is in an unexpected state. We try to catch that here in order to
1945 * reset the Target and retry the probe.
1948 ret = ath10k_pci_cold_reset(ar);
1950 ret = ath10k_pci_warm_reset(ar);
1953 ath10k_err("failed to reset target: %d\n", ret);
1957 ret = ath10k_pci_ce_init(ar);
1959 ath10k_err("failed to initialize CE: %d\n", ret);
1963 ret = ath10k_pci_request_early_irq(ar);
1965 ath10k_err("failed to request early irq: %d\n", ret);
1969 ret = ath10k_pci_wait_for_target_init(ar);
1971 ath10k_err("failed to wait for target to init: %d\n", ret);
1972 goto err_free_early_irq;
1975 ret = ath10k_pci_init_config(ar);
1977 ath10k_err("failed to setup init config: %d\n", ret);
1978 goto err_free_early_irq;
1981 ret = ath10k_pci_wake_target_cpu(ar);
1983 ath10k_err("could not wake up target CPU: %d\n", ret);
1984 goto err_free_early_irq;
1990 ath10k_pci_free_early_irq(ar);
1992 ath10k_pci_ce_deinit(ar);
1993 ath10k_pci_warm_reset(ar);
1998 static int ath10k_pci_hif_power_up_warm(struct ath10k *ar)
2003 * Sometime warm reset succeeds after retries.
2005 * FIXME: It might be possible to tune ath10k_pci_warm_reset() to work
2008 for (i = 0; i < ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS; i++) {
2009 ret = __ath10k_pci_hif_power_up(ar, false);
2013 ath10k_warn("failed to warm reset (attempt %d out of %d): %d\n",
2014 i + 1, ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS, ret);
2020 static int ath10k_pci_hif_power_up(struct ath10k *ar)
2024 ath10k_dbg(ATH10K_DBG_BOOT, "boot hif power up\n");
2027 * Hardware CUS232 version 2 has some issues with cold reset and the
2028 * preferred (and safer) way to perform a device reset is through a
2031 * Warm reset doesn't always work though so fall back to cold reset may
2034 ret = ath10k_pci_hif_power_up_warm(ar);
2036 ath10k_warn("failed to power up target using warm reset: %d\n",
2039 if (ath10k_pci_reset_mode == ATH10K_PCI_RESET_WARM_ONLY)
2042 ath10k_warn("trying cold reset\n");
2044 ret = __ath10k_pci_hif_power_up(ar, true);
2046 ath10k_err("failed to power up target using cold reset too (%d)\n",
2055 static void ath10k_pci_hif_power_down(struct ath10k *ar)
2057 ath10k_dbg(ATH10K_DBG_BOOT, "boot hif power down\n");
2059 ath10k_pci_free_early_irq(ar);
2060 ath10k_pci_kill_tasklet(ar);
2061 ath10k_pci_warm_reset(ar);
2066 #define ATH10K_PCI_PM_CONTROL 0x44
2068 static int ath10k_pci_hif_suspend(struct ath10k *ar)
2070 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2071 struct pci_dev *pdev = ar_pci->pdev;
2074 pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
2076 if ((val & 0x000000ff) != 0x3) {
2077 pci_save_state(pdev);
2078 pci_disable_device(pdev);
2079 pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
2080 (val & 0xffffff00) | 0x03);
2086 static int ath10k_pci_hif_resume(struct ath10k *ar)
2088 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2089 struct pci_dev *pdev = ar_pci->pdev;
2092 pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
2094 if ((val & 0x000000ff) != 0) {
2095 pci_restore_state(pdev);
2096 pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
2099 * Suspend/Resume resets the PCI configuration space,
2100 * so we have to re-disable the RETRY_TIMEOUT register (0x41)
2101 * to keep PCI Tx retries from interfering with C3 CPU state
2103 pci_read_config_dword(pdev, 0x40, &val);
2105 if ((val & 0x0000ff00) != 0)
2106 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
2113 static const struct ath10k_hif_ops ath10k_pci_hif_ops = {
2114 .tx_sg = ath10k_pci_hif_tx_sg,
2115 .exchange_bmi_msg = ath10k_pci_hif_exchange_bmi_msg,
2116 .start = ath10k_pci_hif_start,
2117 .stop = ath10k_pci_hif_stop,
2118 .map_service_to_pipe = ath10k_pci_hif_map_service_to_pipe,
2119 .get_default_pipe = ath10k_pci_hif_get_default_pipe,
2120 .send_complete_check = ath10k_pci_hif_send_complete_check,
2121 .set_callbacks = ath10k_pci_hif_set_callbacks,
2122 .get_free_queue_number = ath10k_pci_hif_get_free_queue_number,
2123 .power_up = ath10k_pci_hif_power_up,
2124 .power_down = ath10k_pci_hif_power_down,
2126 .suspend = ath10k_pci_hif_suspend,
2127 .resume = ath10k_pci_hif_resume,
2131 static void ath10k_pci_ce_tasklet(unsigned long ptr)
2133 struct ath10k_pci_pipe *pipe = (struct ath10k_pci_pipe *)ptr;
2134 struct ath10k_pci *ar_pci = pipe->ar_pci;
2136 ath10k_ce_per_engine_service(ar_pci->ar, pipe->pipe_num);
2139 static void ath10k_msi_err_tasklet(unsigned long data)
2141 struct ath10k *ar = (struct ath10k *)data;
2143 ath10k_pci_fw_interrupt_handler(ar);
2147 * Handler for a per-engine interrupt on a PARTICULAR CE.
2148 * This is used in cases where each CE has a private MSI interrupt.
2150 static irqreturn_t ath10k_pci_per_engine_handler(int irq, void *arg)
2152 struct ath10k *ar = arg;
2153 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2154 int ce_id = irq - ar_pci->pdev->irq - MSI_ASSIGN_CE_INITIAL;
2156 if (ce_id < 0 || ce_id >= ARRAY_SIZE(ar_pci->pipe_info)) {
2157 ath10k_warn("unexpected/invalid irq %d ce_id %d\n", irq, ce_id);
2162 * NOTE: We are able to derive ce_id from irq because we
2163 * use a one-to-one mapping for CE's 0..5.
2164 * CE's 6 & 7 do not use interrupts at all.
2166 * This mapping must be kept in sync with the mapping
2169 tasklet_schedule(&ar_pci->pipe_info[ce_id].intr);
2173 static irqreturn_t ath10k_pci_msi_fw_handler(int irq, void *arg)
2175 struct ath10k *ar = arg;
2176 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2178 tasklet_schedule(&ar_pci->msi_fw_err);
2183 * Top-level interrupt handler for all PCI interrupts from a Target.
2184 * When a block of MSI interrupts is allocated, this top-level handler
2185 * is not used; instead, we directly call the correct sub-handler.
2187 static irqreturn_t ath10k_pci_interrupt_handler(int irq, void *arg)
2189 struct ath10k *ar = arg;
2190 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2192 if (ar_pci->num_msi_intrs == 0) {
2193 if (!ath10k_pci_irq_pending(ar))
2196 ath10k_pci_disable_and_clear_legacy_irq(ar);
2199 tasklet_schedule(&ar_pci->intr_tq);
2204 static void ath10k_pci_early_irq_tasklet(unsigned long data)
2206 struct ath10k *ar = (struct ath10k *)data;
2209 fw_ind = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
2210 if (fw_ind & FW_IND_EVENT_PENDING) {
2211 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS,
2212 fw_ind & ~FW_IND_EVENT_PENDING);
2213 ath10k_pci_fw_crashed_dump(ar);
2216 ath10k_pci_enable_legacy_irq(ar);
2219 static void ath10k_pci_tasklet(unsigned long data)
2221 struct ath10k *ar = (struct ath10k *)data;
2222 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2224 ath10k_pci_fw_interrupt_handler(ar); /* FIXME: Handle FW error */
2225 ath10k_ce_per_engine_service_any(ar);
2227 /* Re-enable legacy irq that was disabled in the irq handler */
2228 if (ar_pci->num_msi_intrs == 0)
2229 ath10k_pci_enable_legacy_irq(ar);
2232 static int ath10k_pci_request_irq_msix(struct ath10k *ar)
2234 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2237 ret = request_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW,
2238 ath10k_pci_msi_fw_handler,
2239 IRQF_SHARED, "ath10k_pci", ar);
2241 ath10k_warn("failed to request MSI-X fw irq %d: %d\n",
2242 ar_pci->pdev->irq + MSI_ASSIGN_FW, ret);
2246 for (i = MSI_ASSIGN_CE_INITIAL; i <= MSI_ASSIGN_CE_MAX; i++) {
2247 ret = request_irq(ar_pci->pdev->irq + i,
2248 ath10k_pci_per_engine_handler,
2249 IRQF_SHARED, "ath10k_pci", ar);
2251 ath10k_warn("failed to request MSI-X ce irq %d: %d\n",
2252 ar_pci->pdev->irq + i, ret);
2254 for (i--; i >= MSI_ASSIGN_CE_INITIAL; i--)
2255 free_irq(ar_pci->pdev->irq + i, ar);
2257 free_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW, ar);
2265 static int ath10k_pci_request_irq_msi(struct ath10k *ar)
2267 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2270 ret = request_irq(ar_pci->pdev->irq,
2271 ath10k_pci_interrupt_handler,
2272 IRQF_SHARED, "ath10k_pci", ar);
2274 ath10k_warn("failed to request MSI irq %d: %d\n",
2275 ar_pci->pdev->irq, ret);
2282 static int ath10k_pci_request_irq_legacy(struct ath10k *ar)
2284 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2287 ret = request_irq(ar_pci->pdev->irq,
2288 ath10k_pci_interrupt_handler,
2289 IRQF_SHARED, "ath10k_pci", ar);
2291 ath10k_warn("failed to request legacy irq %d: %d\n",
2292 ar_pci->pdev->irq, ret);
2299 static int ath10k_pci_request_irq(struct ath10k *ar)
2301 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2303 switch (ar_pci->num_msi_intrs) {
2305 return ath10k_pci_request_irq_legacy(ar);
2307 return ath10k_pci_request_irq_msi(ar);
2308 case MSI_NUM_REQUEST:
2309 return ath10k_pci_request_irq_msix(ar);
2312 ath10k_warn("unknown irq configuration upon request\n");
2316 static void ath10k_pci_free_irq(struct ath10k *ar)
2318 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2321 /* There's at least one interrupt irregardless whether its legacy INTR
2322 * or MSI or MSI-X */
2323 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
2324 free_irq(ar_pci->pdev->irq + i, ar);
2327 static void ath10k_pci_init_irq_tasklets(struct ath10k *ar)
2329 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2332 tasklet_init(&ar_pci->intr_tq, ath10k_pci_tasklet, (unsigned long)ar);
2333 tasklet_init(&ar_pci->msi_fw_err, ath10k_msi_err_tasklet,
2335 tasklet_init(&ar_pci->early_irq_tasklet, ath10k_pci_early_irq_tasklet,
2338 for (i = 0; i < CE_COUNT; i++) {
2339 ar_pci->pipe_info[i].ar_pci = ar_pci;
2340 tasklet_init(&ar_pci->pipe_info[i].intr, ath10k_pci_ce_tasklet,
2341 (unsigned long)&ar_pci->pipe_info[i]);
2345 static int ath10k_pci_init_irq(struct ath10k *ar)
2347 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2350 ath10k_pci_init_irq_tasklets(ar);
2352 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_AUTO)
2353 ath10k_info("limiting irq mode to: %d\n", ath10k_pci_irq_mode);
2356 if (ath10k_pci_irq_mode == ATH10K_PCI_IRQ_AUTO) {
2357 ar_pci->num_msi_intrs = MSI_NUM_REQUEST;
2358 ret = pci_enable_msi_range(ar_pci->pdev, ar_pci->num_msi_intrs,
2359 ar_pci->num_msi_intrs);
2367 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_LEGACY) {
2368 ar_pci->num_msi_intrs = 1;
2369 ret = pci_enable_msi(ar_pci->pdev);
2378 * A potential race occurs here: The CORE_BASE write
2379 * depends on target correctly decoding AXI address but
2380 * host won't know when target writes BAR to CORE_CTRL.
2381 * This write might get lost if target has NOT written BAR.
2382 * For now, fix the race by repeating the write in below
2383 * synchronization checking. */
2384 ar_pci->num_msi_intrs = 0;
2386 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2387 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
2392 static void ath10k_pci_deinit_irq_legacy(struct ath10k *ar)
2394 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2398 static int ath10k_pci_deinit_irq(struct ath10k *ar)
2400 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2402 switch (ar_pci->num_msi_intrs) {
2404 ath10k_pci_deinit_irq_legacy(ar);
2408 case MSI_NUM_REQUEST:
2409 pci_disable_msi(ar_pci->pdev);
2412 pci_disable_msi(ar_pci->pdev);
2415 ath10k_warn("unknown irq configuration upon deinit\n");
2419 static int ath10k_pci_wait_for_target_init(struct ath10k *ar)
2421 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2422 unsigned long timeout;
2425 ath10k_dbg(ATH10K_DBG_BOOT, "boot waiting target to initialise\n");
2427 timeout = jiffies + msecs_to_jiffies(ATH10K_PCI_TARGET_WAIT);
2430 val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
2432 ath10k_dbg(ATH10K_DBG_BOOT, "boot target indicator %x\n", val);
2434 /* target should never return this */
2435 if (val == 0xffffffff)
2438 /* the device has crashed so don't bother trying anymore */
2439 if (val & FW_IND_EVENT_PENDING)
2442 if (val & FW_IND_INITIALIZED)
2445 if (ar_pci->num_msi_intrs == 0)
2446 /* Fix potential race by repeating CORE_BASE writes */
2447 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
2448 PCIE_INTR_ENABLE_ADDRESS,
2449 PCIE_INTR_FIRMWARE_MASK |
2450 PCIE_INTR_CE_MASK_ALL);
2453 } while (time_before(jiffies, timeout));
2455 if (val == 0xffffffff) {
2456 ath10k_err("failed to read device register, device is gone\n");
2460 if (val & FW_IND_EVENT_PENDING) {
2461 ath10k_warn("device has crashed during init\n");
2462 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS,
2463 val & ~FW_IND_EVENT_PENDING);
2464 ath10k_pci_fw_crashed_dump(ar);
2468 if (!(val & FW_IND_INITIALIZED)) {
2469 ath10k_err("failed to receive initialized event from target: %08x\n",
2474 ath10k_dbg(ATH10K_DBG_BOOT, "boot target initialised\n");
2478 static int ath10k_pci_cold_reset(struct ath10k *ar)
2483 ath10k_dbg(ATH10K_DBG_BOOT, "boot cold reset\n");
2485 /* Put Target, including PCIe, into RESET. */
2486 val = ath10k_pci_reg_read32(ar, SOC_GLOBAL_RESET_ADDRESS);
2488 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
2490 for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
2491 if (ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
2492 RTC_STATE_COLD_RESET_MASK)
2497 /* Pull Target, including PCIe, out of RESET. */
2499 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
2501 for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
2502 if (!(ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
2503 RTC_STATE_COLD_RESET_MASK))
2508 ath10k_dbg(ATH10K_DBG_BOOT, "boot cold reset complete\n");
2513 static int ath10k_pci_claim(struct ath10k *ar)
2515 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2516 struct pci_dev *pdev = ar_pci->pdev;
2520 pci_set_drvdata(pdev, ar);
2522 ret = pci_enable_device(pdev);
2524 ath10k_err("failed to enable pci device: %d\n", ret);
2528 ret = pci_request_region(pdev, BAR_NUM, "ath");
2530 ath10k_err("failed to request region BAR%d: %d\n", BAR_NUM,
2535 /* Target expects 32 bit DMA. Enforce it. */
2536 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2538 ath10k_err("failed to set dma mask to 32-bit: %d\n", ret);
2542 ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2544 ath10k_err("failed to set consistent dma mask to 32-bit: %d\n",
2549 pci_set_master(pdev);
2551 /* Workaround: Disable ASPM */
2552 pci_read_config_dword(pdev, 0x80, &lcr_val);
2553 pci_write_config_dword(pdev, 0x80, (lcr_val & 0xffffff00));
2555 /* Arrange for access to Target SoC registers. */
2556 ar_pci->mem = pci_iomap(pdev, BAR_NUM, 0);
2558 ath10k_err("failed to iomap BAR%d\n", BAR_NUM);
2563 ath10k_dbg(ATH10K_DBG_BOOT, "boot pci_mem 0x%p\n", ar_pci->mem);
2567 pci_clear_master(pdev);
2570 pci_release_region(pdev, BAR_NUM);
2573 pci_disable_device(pdev);
2578 static void ath10k_pci_release(struct ath10k *ar)
2580 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2581 struct pci_dev *pdev = ar_pci->pdev;
2583 pci_iounmap(pdev, ar_pci->mem);
2584 pci_release_region(pdev, BAR_NUM);
2585 pci_clear_master(pdev);
2586 pci_disable_device(pdev);
2589 static int ath10k_pci_probe(struct pci_dev *pdev,
2590 const struct pci_device_id *pci_dev)
2594 struct ath10k_pci *ar_pci;
2597 ath10k_dbg(ATH10K_DBG_PCI, "pci probe\n");
2599 ar = ath10k_core_create(sizeof(*ar_pci), &pdev->dev,
2600 &ath10k_pci_hif_ops);
2602 ath10k_err("failed to allocate core\n");
2606 ar_pci = ath10k_pci_priv(ar);
2607 ar_pci->pdev = pdev;
2608 ar_pci->dev = &pdev->dev;
2611 spin_lock_init(&ar_pci->ce_lock);
2613 ret = ath10k_pci_claim(ar);
2615 ath10k_err("failed to claim device: %d\n", ret);
2616 goto err_core_destroy;
2619 ret = ath10k_pci_wake(ar);
2621 ath10k_err("failed to wake up: %d\n", ret);
2625 chip_id = ath10k_pci_soc_read32(ar, SOC_CHIP_ID_ADDRESS);
2626 if (chip_id == 0xffffffff) {
2627 ath10k_err("failed to get chip id\n");
2631 ret = ath10k_pci_alloc_ce(ar);
2633 ath10k_err("failed to allocate copy engine pipes: %d\n", ret);
2637 ath10k_pci_ce_deinit(ar);
2639 ret = ath10k_ce_disable_interrupts(ar);
2641 ath10k_err("failed to disable copy engine interrupts: %d\n",
2646 ret = ath10k_pci_init_irq(ar);
2648 ath10k_err("failed to init irqs: %d\n", ret);
2652 ath10k_info("pci irq %s interrupts %d irq_mode %d reset_mode %d\n",
2653 ath10k_pci_get_irq_method(ar), ar_pci->num_msi_intrs,
2654 ath10k_pci_irq_mode, ath10k_pci_reset_mode);
2656 ret = ath10k_core_register(ar, chip_id);
2658 ath10k_err("failed to register driver core: %d\n", ret);
2659 goto err_deinit_irq;
2665 ath10k_pci_deinit_irq(ar);
2668 ath10k_pci_free_ce(ar);
2671 ath10k_pci_sleep(ar);
2674 ath10k_pci_release(ar);
2677 ath10k_core_destroy(ar);
2682 static void ath10k_pci_remove(struct pci_dev *pdev)
2684 struct ath10k *ar = pci_get_drvdata(pdev);
2685 struct ath10k_pci *ar_pci;
2687 ath10k_dbg(ATH10K_DBG_PCI, "pci remove\n");
2692 ar_pci = ath10k_pci_priv(ar);
2697 ath10k_core_unregister(ar);
2698 ath10k_pci_deinit_irq(ar);
2699 ath10k_pci_ce_deinit(ar);
2700 ath10k_pci_free_ce(ar);
2701 ath10k_pci_sleep(ar);
2702 ath10k_pci_release(ar);
2703 ath10k_core_destroy(ar);
2706 MODULE_DEVICE_TABLE(pci, ath10k_pci_id_table);
2708 static struct pci_driver ath10k_pci_driver = {
2709 .name = "ath10k_pci",
2710 .id_table = ath10k_pci_id_table,
2711 .probe = ath10k_pci_probe,
2712 .remove = ath10k_pci_remove,
2715 static int __init ath10k_pci_init(void)
2719 ret = pci_register_driver(&ath10k_pci_driver);
2721 ath10k_err("failed to register PCI driver: %d\n", ret);
2725 module_init(ath10k_pci_init);
2727 static void __exit ath10k_pci_exit(void)
2729 pci_unregister_driver(&ath10k_pci_driver);
2732 module_exit(ath10k_pci_exit);
2734 MODULE_AUTHOR("Qualcomm Atheros");
2735 MODULE_DESCRIPTION("Driver support for Atheros QCA988X PCIe devices");
2736 MODULE_LICENSE("Dual BSD/GPL");
2737 MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_FW_3_FILE);
2738 MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_BOARD_DATA_FILE);