2 * Copyright (c) 2005-2011 Atheros Communications Inc.
3 * Copyright (c) 2011-2017 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>
28 #include "targaddrs.h"
37 enum ath10k_pci_reset_mode {
38 ATH10K_PCI_RESET_AUTO = 0,
39 ATH10K_PCI_RESET_WARM_ONLY = 1,
42 static unsigned int ath10k_pci_irq_mode = ATH10K_PCI_IRQ_AUTO;
43 static unsigned int ath10k_pci_reset_mode = ATH10K_PCI_RESET_AUTO;
45 module_param_named(irq_mode, ath10k_pci_irq_mode, uint, 0644);
46 MODULE_PARM_DESC(irq_mode, "0: auto, 1: legacy, 2: msi (default: 0)");
48 module_param_named(reset_mode, ath10k_pci_reset_mode, uint, 0644);
49 MODULE_PARM_DESC(reset_mode, "0: auto, 1: warm only (default: 0)");
51 /* how long wait to wait for target to initialise, in ms */
52 #define ATH10K_PCI_TARGET_WAIT 3000
53 #define ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS 3
55 /* Maximum number of bytes that can be handled atomically by
56 * diag read and write.
58 #define ATH10K_DIAG_TRANSFER_LIMIT 0x5000
60 static const struct pci_device_id ath10k_pci_id_table[] = {
61 { PCI_VDEVICE(ATHEROS, QCA988X_2_0_DEVICE_ID) }, /* PCI-E QCA988X V2 */
62 { PCI_VDEVICE(ATHEROS, QCA6164_2_1_DEVICE_ID) }, /* PCI-E QCA6164 V2.1 */
63 { PCI_VDEVICE(ATHEROS, QCA6174_2_1_DEVICE_ID) }, /* PCI-E QCA6174 V2.1 */
64 { PCI_VDEVICE(ATHEROS, QCA99X0_2_0_DEVICE_ID) }, /* PCI-E QCA99X0 V2 */
65 { PCI_VDEVICE(ATHEROS, QCA9888_2_0_DEVICE_ID) }, /* PCI-E QCA9888 V2 */
66 { PCI_VDEVICE(ATHEROS, QCA9984_1_0_DEVICE_ID) }, /* PCI-E QCA9984 V1 */
67 { PCI_VDEVICE(ATHEROS, QCA9377_1_0_DEVICE_ID) }, /* PCI-E QCA9377 V1 */
68 { PCI_VDEVICE(ATHEROS, QCA9887_1_0_DEVICE_ID) }, /* PCI-E QCA9887 */
72 static const struct ath10k_pci_supp_chip ath10k_pci_supp_chips[] = {
73 /* QCA988X pre 2.0 chips are not supported because they need some nasty
74 * hacks. ath10k doesn't have them and these devices crash horribly
77 { QCA988X_2_0_DEVICE_ID, QCA988X_HW_2_0_CHIP_ID_REV },
79 { QCA6164_2_1_DEVICE_ID, QCA6174_HW_2_1_CHIP_ID_REV },
80 { QCA6164_2_1_DEVICE_ID, QCA6174_HW_2_2_CHIP_ID_REV },
81 { QCA6164_2_1_DEVICE_ID, QCA6174_HW_3_0_CHIP_ID_REV },
82 { QCA6164_2_1_DEVICE_ID, QCA6174_HW_3_1_CHIP_ID_REV },
83 { QCA6164_2_1_DEVICE_ID, QCA6174_HW_3_2_CHIP_ID_REV },
85 { QCA6174_2_1_DEVICE_ID, QCA6174_HW_2_1_CHIP_ID_REV },
86 { QCA6174_2_1_DEVICE_ID, QCA6174_HW_2_2_CHIP_ID_REV },
87 { QCA6174_2_1_DEVICE_ID, QCA6174_HW_3_0_CHIP_ID_REV },
88 { QCA6174_2_1_DEVICE_ID, QCA6174_HW_3_1_CHIP_ID_REV },
89 { QCA6174_2_1_DEVICE_ID, QCA6174_HW_3_2_CHIP_ID_REV },
91 { QCA99X0_2_0_DEVICE_ID, QCA99X0_HW_2_0_CHIP_ID_REV },
93 { QCA9984_1_0_DEVICE_ID, QCA9984_HW_1_0_CHIP_ID_REV },
95 { QCA9888_2_0_DEVICE_ID, QCA9888_HW_2_0_CHIP_ID_REV },
97 { QCA9377_1_0_DEVICE_ID, QCA9377_HW_1_0_CHIP_ID_REV },
98 { QCA9377_1_0_DEVICE_ID, QCA9377_HW_1_1_CHIP_ID_REV },
100 { QCA9887_1_0_DEVICE_ID, QCA9887_HW_1_0_CHIP_ID_REV },
103 static void ath10k_pci_buffer_cleanup(struct ath10k *ar);
104 static int ath10k_pci_cold_reset(struct ath10k *ar);
105 static int ath10k_pci_safe_chip_reset(struct ath10k *ar);
106 static int ath10k_pci_init_irq(struct ath10k *ar);
107 static int ath10k_pci_deinit_irq(struct ath10k *ar);
108 static int ath10k_pci_request_irq(struct ath10k *ar);
109 static void ath10k_pci_free_irq(struct ath10k *ar);
110 static int ath10k_pci_bmi_wait(struct ath10k *ar,
111 struct ath10k_ce_pipe *tx_pipe,
112 struct ath10k_ce_pipe *rx_pipe,
113 struct bmi_xfer *xfer);
114 static int ath10k_pci_qca99x0_chip_reset(struct ath10k *ar);
115 static void ath10k_pci_htc_tx_cb(struct ath10k_ce_pipe *ce_state);
116 static void ath10k_pci_htc_rx_cb(struct ath10k_ce_pipe *ce_state);
117 static void ath10k_pci_htt_tx_cb(struct ath10k_ce_pipe *ce_state);
118 static void ath10k_pci_htt_rx_cb(struct ath10k_ce_pipe *ce_state);
119 static void ath10k_pci_htt_htc_rx_cb(struct ath10k_ce_pipe *ce_state);
120 static void ath10k_pci_pktlog_rx_cb(struct ath10k_ce_pipe *ce_state);
122 static struct ce_attr host_ce_config_wlan[] = {
123 /* CE0: host->target HTC control and raw streams */
125 .flags = CE_ATTR_FLAGS,
129 .send_cb = ath10k_pci_htc_tx_cb,
132 /* CE1: target->host HTT + HTC control */
134 .flags = CE_ATTR_FLAGS,
137 .dest_nentries = 512,
138 .recv_cb = ath10k_pci_htt_htc_rx_cb,
141 /* CE2: target->host WMI */
143 .flags = CE_ATTR_FLAGS,
146 .dest_nentries = 128,
147 .recv_cb = ath10k_pci_htc_rx_cb,
150 /* CE3: host->target WMI */
152 .flags = CE_ATTR_FLAGS,
156 .send_cb = ath10k_pci_htc_tx_cb,
159 /* CE4: host->target HTT */
161 .flags = CE_ATTR_FLAGS | CE_ATTR_DIS_INTR,
162 .src_nentries = CE_HTT_H2T_MSG_SRC_NENTRIES,
165 .send_cb = ath10k_pci_htt_tx_cb,
168 /* CE5: target->host HTT (HIF->HTT) */
170 .flags = CE_ATTR_FLAGS,
173 .dest_nentries = 512,
174 .recv_cb = ath10k_pci_htt_rx_cb,
177 /* CE6: target autonomous hif_memcpy */
179 .flags = CE_ATTR_FLAGS,
185 /* CE7: ce_diag, the Diagnostic Window */
187 .flags = CE_ATTR_FLAGS,
189 .src_sz_max = DIAG_TRANSFER_LIMIT,
193 /* CE8: target->host pktlog */
195 .flags = CE_ATTR_FLAGS,
198 .dest_nentries = 128,
199 .recv_cb = ath10k_pci_pktlog_rx_cb,
202 /* CE9 target autonomous qcache memcpy */
204 .flags = CE_ATTR_FLAGS,
210 /* CE10: target autonomous hif memcpy */
212 .flags = CE_ATTR_FLAGS,
218 /* CE11: target autonomous hif memcpy */
220 .flags = CE_ATTR_FLAGS,
227 /* Target firmware's Copy Engine configuration. */
228 static struct ce_pipe_config target_ce_config_wlan[] = {
229 /* CE0: host->target HTC control and raw streams */
231 .pipenum = __cpu_to_le32(0),
232 .pipedir = __cpu_to_le32(PIPEDIR_OUT),
233 .nentries = __cpu_to_le32(32),
234 .nbytes_max = __cpu_to_le32(256),
235 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
236 .reserved = __cpu_to_le32(0),
239 /* CE1: target->host HTT + HTC control */
241 .pipenum = __cpu_to_le32(1),
242 .pipedir = __cpu_to_le32(PIPEDIR_IN),
243 .nentries = __cpu_to_le32(32),
244 .nbytes_max = __cpu_to_le32(2048),
245 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
246 .reserved = __cpu_to_le32(0),
249 /* CE2: target->host WMI */
251 .pipenum = __cpu_to_le32(2),
252 .pipedir = __cpu_to_le32(PIPEDIR_IN),
253 .nentries = __cpu_to_le32(64),
254 .nbytes_max = __cpu_to_le32(2048),
255 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
256 .reserved = __cpu_to_le32(0),
259 /* CE3: host->target WMI */
261 .pipenum = __cpu_to_le32(3),
262 .pipedir = __cpu_to_le32(PIPEDIR_OUT),
263 .nentries = __cpu_to_le32(32),
264 .nbytes_max = __cpu_to_le32(2048),
265 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
266 .reserved = __cpu_to_le32(0),
269 /* CE4: host->target HTT */
271 .pipenum = __cpu_to_le32(4),
272 .pipedir = __cpu_to_le32(PIPEDIR_OUT),
273 .nentries = __cpu_to_le32(256),
274 .nbytes_max = __cpu_to_le32(256),
275 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
276 .reserved = __cpu_to_le32(0),
279 /* NB: 50% of src nentries, since tx has 2 frags */
281 /* CE5: target->host HTT (HIF->HTT) */
283 .pipenum = __cpu_to_le32(5),
284 .pipedir = __cpu_to_le32(PIPEDIR_IN),
285 .nentries = __cpu_to_le32(32),
286 .nbytes_max = __cpu_to_le32(512),
287 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
288 .reserved = __cpu_to_le32(0),
291 /* CE6: Reserved for target autonomous hif_memcpy */
293 .pipenum = __cpu_to_le32(6),
294 .pipedir = __cpu_to_le32(PIPEDIR_INOUT),
295 .nentries = __cpu_to_le32(32),
296 .nbytes_max = __cpu_to_le32(4096),
297 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
298 .reserved = __cpu_to_le32(0),
301 /* CE7 used only by Host */
303 .pipenum = __cpu_to_le32(7),
304 .pipedir = __cpu_to_le32(PIPEDIR_INOUT),
305 .nentries = __cpu_to_le32(0),
306 .nbytes_max = __cpu_to_le32(0),
307 .flags = __cpu_to_le32(0),
308 .reserved = __cpu_to_le32(0),
311 /* CE8 target->host packtlog */
313 .pipenum = __cpu_to_le32(8),
314 .pipedir = __cpu_to_le32(PIPEDIR_IN),
315 .nentries = __cpu_to_le32(64),
316 .nbytes_max = __cpu_to_le32(2048),
317 .flags = __cpu_to_le32(CE_ATTR_FLAGS | CE_ATTR_DIS_INTR),
318 .reserved = __cpu_to_le32(0),
321 /* CE9 target autonomous qcache memcpy */
323 .pipenum = __cpu_to_le32(9),
324 .pipedir = __cpu_to_le32(PIPEDIR_INOUT),
325 .nentries = __cpu_to_le32(32),
326 .nbytes_max = __cpu_to_le32(2048),
327 .flags = __cpu_to_le32(CE_ATTR_FLAGS | CE_ATTR_DIS_INTR),
328 .reserved = __cpu_to_le32(0),
331 /* It not necessary to send target wlan configuration for CE10 & CE11
332 * as these CEs are not actively used in target.
337 * Map from service/endpoint to Copy Engine.
338 * This table is derived from the CE_PCI TABLE, above.
339 * It is passed to the Target at startup for use by firmware.
341 static struct service_to_pipe target_service_to_ce_map_wlan[] = {
343 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VO),
344 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
348 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VO),
349 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
353 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BK),
354 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
358 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BK),
359 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
363 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BE),
364 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
368 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BE),
369 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
373 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VI),
374 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
378 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VI),
379 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
383 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_CONTROL),
384 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
388 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_CONTROL),
389 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
393 __cpu_to_le32(ATH10K_HTC_SVC_ID_RSVD_CTRL),
394 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
398 __cpu_to_le32(ATH10K_HTC_SVC_ID_RSVD_CTRL),
399 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
403 __cpu_to_le32(ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS),
404 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
408 __cpu_to_le32(ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS),
409 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
413 __cpu_to_le32(ATH10K_HTC_SVC_ID_HTT_DATA_MSG),
414 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
418 __cpu_to_le32(ATH10K_HTC_SVC_ID_HTT_DATA_MSG),
419 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
423 /* (Additions here) */
432 static bool ath10k_pci_is_awake(struct ath10k *ar)
434 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
435 u32 val = ioread32(ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
438 return RTC_STATE_V_GET(val) == RTC_STATE_V_ON;
441 static void __ath10k_pci_wake(struct ath10k *ar)
443 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
445 lockdep_assert_held(&ar_pci->ps_lock);
447 ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps wake reg refcount %lu awake %d\n",
448 ar_pci->ps_wake_refcount, ar_pci->ps_awake);
450 iowrite32(PCIE_SOC_WAKE_V_MASK,
451 ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
452 PCIE_SOC_WAKE_ADDRESS);
455 static void __ath10k_pci_sleep(struct ath10k *ar)
457 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
459 lockdep_assert_held(&ar_pci->ps_lock);
461 ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps sleep reg refcount %lu awake %d\n",
462 ar_pci->ps_wake_refcount, ar_pci->ps_awake);
464 iowrite32(PCIE_SOC_WAKE_RESET,
465 ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
466 PCIE_SOC_WAKE_ADDRESS);
467 ar_pci->ps_awake = false;
470 static int ath10k_pci_wake_wait(struct ath10k *ar)
475 while (tot_delay < PCIE_WAKE_TIMEOUT) {
476 if (ath10k_pci_is_awake(ar)) {
477 if (tot_delay > PCIE_WAKE_LATE_US)
478 ath10k_warn(ar, "device wakeup took %d ms which is unusually long, otherwise it works normally.\n",
484 tot_delay += curr_delay;
493 static int ath10k_pci_force_wake(struct ath10k *ar)
495 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
502 spin_lock_irqsave(&ar_pci->ps_lock, flags);
504 if (!ar_pci->ps_awake) {
505 iowrite32(PCIE_SOC_WAKE_V_MASK,
506 ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
507 PCIE_SOC_WAKE_ADDRESS);
509 ret = ath10k_pci_wake_wait(ar);
511 ar_pci->ps_awake = true;
514 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
519 static void ath10k_pci_force_sleep(struct ath10k *ar)
521 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
524 spin_lock_irqsave(&ar_pci->ps_lock, flags);
526 iowrite32(PCIE_SOC_WAKE_RESET,
527 ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
528 PCIE_SOC_WAKE_ADDRESS);
529 ar_pci->ps_awake = false;
531 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
534 static int ath10k_pci_wake(struct ath10k *ar)
536 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
540 if (ar_pci->pci_ps == 0)
543 spin_lock_irqsave(&ar_pci->ps_lock, flags);
545 ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps wake refcount %lu awake %d\n",
546 ar_pci->ps_wake_refcount, ar_pci->ps_awake);
548 /* This function can be called very frequently. To avoid excessive
549 * CPU stalls for MMIO reads use a cache var to hold the device state.
551 if (!ar_pci->ps_awake) {
552 __ath10k_pci_wake(ar);
554 ret = ath10k_pci_wake_wait(ar);
556 ar_pci->ps_awake = true;
560 ar_pci->ps_wake_refcount++;
561 WARN_ON(ar_pci->ps_wake_refcount == 0);
564 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
569 static void ath10k_pci_sleep(struct ath10k *ar)
571 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
574 if (ar_pci->pci_ps == 0)
577 spin_lock_irqsave(&ar_pci->ps_lock, flags);
579 ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps sleep refcount %lu awake %d\n",
580 ar_pci->ps_wake_refcount, ar_pci->ps_awake);
582 if (WARN_ON(ar_pci->ps_wake_refcount == 0))
585 ar_pci->ps_wake_refcount--;
587 mod_timer(&ar_pci->ps_timer, jiffies +
588 msecs_to_jiffies(ATH10K_PCI_SLEEP_GRACE_PERIOD_MSEC));
591 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
594 static void ath10k_pci_ps_timer(struct timer_list *t)
596 struct ath10k_pci *ar_pci = from_timer(ar_pci, t, ps_timer);
597 struct ath10k *ar = ar_pci->ar;
600 spin_lock_irqsave(&ar_pci->ps_lock, flags);
602 ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps timer refcount %lu awake %d\n",
603 ar_pci->ps_wake_refcount, ar_pci->ps_awake);
605 if (ar_pci->ps_wake_refcount > 0)
608 __ath10k_pci_sleep(ar);
611 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
614 static void ath10k_pci_sleep_sync(struct ath10k *ar)
616 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
619 if (ar_pci->pci_ps == 0) {
620 ath10k_pci_force_sleep(ar);
624 del_timer_sync(&ar_pci->ps_timer);
626 spin_lock_irqsave(&ar_pci->ps_lock, flags);
627 WARN_ON(ar_pci->ps_wake_refcount > 0);
628 __ath10k_pci_sleep(ar);
629 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
632 static void ath10k_bus_pci_write32(struct ath10k *ar, u32 offset, u32 value)
634 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
637 if (unlikely(offset + sizeof(value) > ar_pci->mem_len)) {
638 ath10k_warn(ar, "refusing to write mmio out of bounds at 0x%08x - 0x%08zx (max 0x%08zx)\n",
639 offset, offset + sizeof(value), ar_pci->mem_len);
643 ret = ath10k_pci_wake(ar);
645 ath10k_warn(ar, "failed to wake target for write32 of 0x%08x at 0x%08x: %d\n",
650 iowrite32(value, ar_pci->mem + offset);
651 ath10k_pci_sleep(ar);
654 static u32 ath10k_bus_pci_read32(struct ath10k *ar, u32 offset)
656 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
660 if (unlikely(offset + sizeof(val) > ar_pci->mem_len)) {
661 ath10k_warn(ar, "refusing to read mmio out of bounds at 0x%08x - 0x%08zx (max 0x%08zx)\n",
662 offset, offset + sizeof(val), ar_pci->mem_len);
666 ret = ath10k_pci_wake(ar);
668 ath10k_warn(ar, "failed to wake target for read32 at 0x%08x: %d\n",
673 val = ioread32(ar_pci->mem + offset);
674 ath10k_pci_sleep(ar);
679 inline void ath10k_pci_write32(struct ath10k *ar, u32 offset, u32 value)
681 struct ath10k_ce *ce = ath10k_ce_priv(ar);
683 ce->bus_ops->write32(ar, offset, value);
686 inline u32 ath10k_pci_read32(struct ath10k *ar, u32 offset)
688 struct ath10k_ce *ce = ath10k_ce_priv(ar);
690 return ce->bus_ops->read32(ar, offset);
693 u32 ath10k_pci_soc_read32(struct ath10k *ar, u32 addr)
695 return ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS + addr);
698 void ath10k_pci_soc_write32(struct ath10k *ar, u32 addr, u32 val)
700 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + addr, val);
703 u32 ath10k_pci_reg_read32(struct ath10k *ar, u32 addr)
705 return ath10k_pci_read32(ar, PCIE_LOCAL_BASE_ADDRESS + addr);
708 void ath10k_pci_reg_write32(struct ath10k *ar, u32 addr, u32 val)
710 ath10k_pci_write32(ar, PCIE_LOCAL_BASE_ADDRESS + addr, val);
713 bool ath10k_pci_irq_pending(struct ath10k *ar)
717 /* Check if the shared legacy irq is for us */
718 cause = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
719 PCIE_INTR_CAUSE_ADDRESS);
720 if (cause & (PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL))
726 void ath10k_pci_disable_and_clear_legacy_irq(struct ath10k *ar)
728 /* IMPORTANT: INTR_CLR register has to be set after
729 * INTR_ENABLE is set to 0, otherwise interrupt can not be
732 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
734 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_CLR_ADDRESS,
735 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
737 /* IMPORTANT: this extra read transaction is required to
738 * flush the posted write buffer.
740 (void)ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
741 PCIE_INTR_ENABLE_ADDRESS);
744 void ath10k_pci_enable_legacy_irq(struct ath10k *ar)
746 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
747 PCIE_INTR_ENABLE_ADDRESS,
748 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
750 /* IMPORTANT: this extra read transaction is required to
751 * flush the posted write buffer.
753 (void)ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
754 PCIE_INTR_ENABLE_ADDRESS);
757 static inline const char *ath10k_pci_get_irq_method(struct ath10k *ar)
759 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
761 if (ar_pci->oper_irq_mode == ATH10K_PCI_IRQ_MSI)
767 static int __ath10k_pci_rx_post_buf(struct ath10k_pci_pipe *pipe)
769 struct ath10k *ar = pipe->hif_ce_state;
770 struct ath10k_ce *ce = ath10k_ce_priv(ar);
771 struct ath10k_ce_pipe *ce_pipe = pipe->ce_hdl;
776 skb = dev_alloc_skb(pipe->buf_sz);
780 WARN_ONCE((unsigned long)skb->data & 3, "unaligned skb");
782 paddr = dma_map_single(ar->dev, skb->data,
783 skb->len + skb_tailroom(skb),
785 if (unlikely(dma_mapping_error(ar->dev, paddr))) {
786 ath10k_warn(ar, "failed to dma map pci rx buf\n");
787 dev_kfree_skb_any(skb);
791 ATH10K_SKB_RXCB(skb)->paddr = paddr;
793 spin_lock_bh(&ce->ce_lock);
794 ret = ce_pipe->ops->ce_rx_post_buf(ce_pipe, skb, paddr);
795 spin_unlock_bh(&ce->ce_lock);
797 dma_unmap_single(ar->dev, paddr, skb->len + skb_tailroom(skb),
799 dev_kfree_skb_any(skb);
806 static void ath10k_pci_rx_post_pipe(struct ath10k_pci_pipe *pipe)
808 struct ath10k *ar = pipe->hif_ce_state;
809 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
810 struct ath10k_ce *ce = ath10k_ce_priv(ar);
811 struct ath10k_ce_pipe *ce_pipe = pipe->ce_hdl;
814 if (pipe->buf_sz == 0)
817 if (!ce_pipe->dest_ring)
820 spin_lock_bh(&ce->ce_lock);
821 num = __ath10k_ce_rx_num_free_bufs(ce_pipe);
822 spin_unlock_bh(&ce->ce_lock);
825 ret = __ath10k_pci_rx_post_buf(pipe);
829 ath10k_warn(ar, "failed to post pci rx buf: %d\n", ret);
830 mod_timer(&ar_pci->rx_post_retry, jiffies +
831 ATH10K_PCI_RX_POST_RETRY_MS);
838 void ath10k_pci_rx_post(struct ath10k *ar)
840 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
843 for (i = 0; i < CE_COUNT; i++)
844 ath10k_pci_rx_post_pipe(&ar_pci->pipe_info[i]);
847 void ath10k_pci_rx_replenish_retry(struct timer_list *t)
849 struct ath10k_pci *ar_pci = from_timer(ar_pci, t, rx_post_retry);
850 struct ath10k *ar = ar_pci->ar;
852 ath10k_pci_rx_post(ar);
855 static u32 ath10k_pci_qca988x_targ_cpu_to_ce_addr(struct ath10k *ar, u32 addr)
857 u32 val = 0, region = addr & 0xfffff;
859 val = (ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS + CORE_CTRL_ADDRESS)
861 val |= 0x100000 | region;
865 static u32 ath10k_pci_qca99x0_targ_cpu_to_ce_addr(struct ath10k *ar, u32 addr)
867 u32 val = 0, region = addr & 0xfffff;
869 val = ath10k_pci_read32(ar, PCIE_BAR_REG_ADDRESS);
870 val |= 0x100000 | region;
874 static u32 ath10k_pci_targ_cpu_to_ce_addr(struct ath10k *ar, u32 addr)
876 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
878 if (WARN_ON_ONCE(!ar_pci->targ_cpu_to_ce_addr))
881 return ar_pci->targ_cpu_to_ce_addr(ar, addr);
885 * Diagnostic read/write access is provided for startup/config/debug usage.
886 * Caller must guarantee proper alignment, when applicable, and single user
889 static int ath10k_pci_diag_read_mem(struct ath10k *ar, u32 address, void *data,
892 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
893 struct ath10k_ce *ce = ath10k_ce_priv(ar);
896 unsigned int completed_nbytes, alloc_nbytes, remaining_bytes;
897 struct ath10k_ce_pipe *ce_diag;
898 /* Host buffer address in CE space */
900 dma_addr_t ce_data_base = 0;
901 void *data_buf = NULL;
904 spin_lock_bh(&ce->ce_lock);
906 ce_diag = ar_pci->ce_diag;
909 * Allocate a temporary bounce buffer to hold caller's data
910 * to be DMA'ed from Target. This guarantees
911 * 1) 4-byte alignment
912 * 2) Buffer in DMA-able space
914 alloc_nbytes = min_t(unsigned int, nbytes, DIAG_TRANSFER_LIMIT);
916 data_buf = (unsigned char *)dma_zalloc_coherent(ar->dev,
926 remaining_bytes = nbytes;
927 ce_data = ce_data_base;
928 while (remaining_bytes) {
929 nbytes = min_t(unsigned int, remaining_bytes,
930 DIAG_TRANSFER_LIMIT);
932 ret = ce_diag->ops->ce_rx_post_buf(ce_diag, &ce_data, ce_data);
936 /* Request CE to send from Target(!) address to Host buffer */
938 * The address supplied by the caller is in the
939 * Target CPU virtual address space.
941 * In order to use this address with the diagnostic CE,
942 * convert it from Target CPU virtual address space
943 * to CE address space
945 address = ath10k_pci_targ_cpu_to_ce_addr(ar, address);
947 ret = ath10k_ce_send_nolock(ce_diag, NULL, (u32)address, nbytes, 0,
953 while (ath10k_ce_completed_send_next_nolock(ce_diag,
956 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
963 while (ath10k_ce_completed_recv_next_nolock(ce_diag,
969 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
975 if (nbytes != completed_nbytes) {
980 if (*buf != ce_data) {
985 remaining_bytes -= nbytes;
986 memcpy(data, data_buf, nbytes);
995 dma_free_coherent(ar->dev, alloc_nbytes, data_buf,
998 spin_unlock_bh(&ce->ce_lock);
1003 static int ath10k_pci_diag_read32(struct ath10k *ar, u32 address, u32 *value)
1008 ret = ath10k_pci_diag_read_mem(ar, address, &val, sizeof(val));
1009 *value = __le32_to_cpu(val);
1014 static int __ath10k_pci_diag_read_hi(struct ath10k *ar, void *dest,
1017 u32 host_addr, addr;
1020 host_addr = host_interest_item_address(src);
1022 ret = ath10k_pci_diag_read32(ar, host_addr, &addr);
1024 ath10k_warn(ar, "failed to get memcpy hi address for firmware address %d: %d\n",
1029 ret = ath10k_pci_diag_read_mem(ar, addr, dest, len);
1031 ath10k_warn(ar, "failed to memcpy firmware memory from %d (%d B): %d\n",
1039 #define ath10k_pci_diag_read_hi(ar, dest, src, len) \
1040 __ath10k_pci_diag_read_hi(ar, dest, HI_ITEM(src), len)
1042 int ath10k_pci_diag_write_mem(struct ath10k *ar, u32 address,
1043 const void *data, int nbytes)
1045 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1046 struct ath10k_ce *ce = ath10k_ce_priv(ar);
1049 unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
1050 struct ath10k_ce_pipe *ce_diag;
1051 void *data_buf = NULL;
1052 u32 ce_data; /* Host buffer address in CE space */
1053 dma_addr_t ce_data_base = 0;
1056 spin_lock_bh(&ce->ce_lock);
1058 ce_diag = ar_pci->ce_diag;
1061 * Allocate a temporary bounce buffer to hold caller's data
1062 * to be DMA'ed to Target. This guarantees
1063 * 1) 4-byte alignment
1064 * 2) Buffer in DMA-able space
1066 orig_nbytes = nbytes;
1067 data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
1076 /* Copy caller's data to allocated DMA buf */
1077 memcpy(data_buf, data, orig_nbytes);
1080 * The address supplied by the caller is in the
1081 * Target CPU virtual address space.
1083 * In order to use this address with the diagnostic CE,
1085 * Target CPU virtual address space
1089 address = ath10k_pci_targ_cpu_to_ce_addr(ar, address);
1091 remaining_bytes = orig_nbytes;
1092 ce_data = ce_data_base;
1093 while (remaining_bytes) {
1094 /* FIXME: check cast */
1095 nbytes = min_t(int, remaining_bytes, DIAG_TRANSFER_LIMIT);
1097 /* Set up to receive directly into Target(!) address */
1098 ret = ce_diag->ops->ce_rx_post_buf(ce_diag, &address, address);
1103 * Request CE to send caller-supplied data that
1104 * was copied to bounce buffer to Target(!) address.
1106 ret = ath10k_ce_send_nolock(ce_diag, NULL, (u32)ce_data,
1112 while (ath10k_ce_completed_send_next_nolock(ce_diag,
1116 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
1123 while (ath10k_ce_completed_recv_next_nolock(ce_diag,
1129 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
1135 if (nbytes != completed_nbytes) {
1140 if (*buf != address) {
1145 remaining_bytes -= nbytes;
1152 dma_free_coherent(ar->dev, orig_nbytes, data_buf,
1157 ath10k_warn(ar, "failed to write diag value at 0x%x: %d\n",
1160 spin_unlock_bh(&ce->ce_lock);
1165 static int ath10k_pci_diag_write32(struct ath10k *ar, u32 address, u32 value)
1167 __le32 val = __cpu_to_le32(value);
1169 return ath10k_pci_diag_write_mem(ar, address, &val, sizeof(val));
1172 /* Called by lower (CE) layer when a send to Target completes. */
1173 static void ath10k_pci_htc_tx_cb(struct ath10k_ce_pipe *ce_state)
1175 struct ath10k *ar = ce_state->ar;
1176 struct sk_buff_head list;
1177 struct sk_buff *skb;
1179 __skb_queue_head_init(&list);
1180 while (ath10k_ce_completed_send_next(ce_state, (void **)&skb) == 0) {
1181 /* no need to call tx completion for NULL pointers */
1185 __skb_queue_tail(&list, skb);
1188 while ((skb = __skb_dequeue(&list)))
1189 ath10k_htc_tx_completion_handler(ar, skb);
1192 static void ath10k_pci_process_rx_cb(struct ath10k_ce_pipe *ce_state,
1193 void (*callback)(struct ath10k *ar,
1194 struct sk_buff *skb))
1196 struct ath10k *ar = ce_state->ar;
1197 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1198 struct ath10k_pci_pipe *pipe_info = &ar_pci->pipe_info[ce_state->id];
1199 struct sk_buff *skb;
1200 struct sk_buff_head list;
1201 void *transfer_context;
1202 unsigned int nbytes, max_nbytes;
1204 __skb_queue_head_init(&list);
1205 while (ath10k_ce_completed_recv_next(ce_state, &transfer_context,
1207 skb = transfer_context;
1208 max_nbytes = skb->len + skb_tailroom(skb);
1209 dma_unmap_single(ar->dev, ATH10K_SKB_RXCB(skb)->paddr,
1210 max_nbytes, DMA_FROM_DEVICE);
1212 if (unlikely(max_nbytes < nbytes)) {
1213 ath10k_warn(ar, "rxed more than expected (nbytes %d, max %d)",
1214 nbytes, max_nbytes);
1215 dev_kfree_skb_any(skb);
1219 skb_put(skb, nbytes);
1220 __skb_queue_tail(&list, skb);
1223 while ((skb = __skb_dequeue(&list))) {
1224 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci rx ce pipe %d len %d\n",
1225 ce_state->id, skb->len);
1226 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci rx: ",
1227 skb->data, skb->len);
1232 ath10k_pci_rx_post_pipe(pipe_info);
1235 static void ath10k_pci_process_htt_rx_cb(struct ath10k_ce_pipe *ce_state,
1236 void (*callback)(struct ath10k *ar,
1237 struct sk_buff *skb))
1239 struct ath10k *ar = ce_state->ar;
1240 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1241 struct ath10k_pci_pipe *pipe_info = &ar_pci->pipe_info[ce_state->id];
1242 struct ath10k_ce_pipe *ce_pipe = pipe_info->ce_hdl;
1243 struct sk_buff *skb;
1244 struct sk_buff_head list;
1245 void *transfer_context;
1246 unsigned int nbytes, max_nbytes, nentries;
1249 /* No need to aquire ce_lock for CE5, since this is the only place CE5
1250 * is processed other than init and deinit. Before releasing CE5
1251 * buffers, interrupts are disabled. Thus CE5 access is serialized.
1253 __skb_queue_head_init(&list);
1254 while (ath10k_ce_completed_recv_next_nolock(ce_state, &transfer_context,
1256 skb = transfer_context;
1257 max_nbytes = skb->len + skb_tailroom(skb);
1259 if (unlikely(max_nbytes < nbytes)) {
1260 ath10k_warn(ar, "rxed more than expected (nbytes %d, max %d)",
1261 nbytes, max_nbytes);
1265 dma_sync_single_for_cpu(ar->dev, ATH10K_SKB_RXCB(skb)->paddr,
1266 max_nbytes, DMA_FROM_DEVICE);
1267 skb_put(skb, nbytes);
1268 __skb_queue_tail(&list, skb);
1271 nentries = skb_queue_len(&list);
1272 while ((skb = __skb_dequeue(&list))) {
1273 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci rx ce pipe %d len %d\n",
1274 ce_state->id, skb->len);
1275 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci rx: ",
1276 skb->data, skb->len);
1278 orig_len = skb->len;
1280 skb_push(skb, orig_len - skb->len);
1281 skb_reset_tail_pointer(skb);
1284 /*let device gain the buffer again*/
1285 dma_sync_single_for_device(ar->dev, ATH10K_SKB_RXCB(skb)->paddr,
1286 skb->len + skb_tailroom(skb),
1289 ath10k_ce_rx_update_write_idx(ce_pipe, nentries);
1292 /* Called by lower (CE) layer when data is received from the Target. */
1293 static void ath10k_pci_htc_rx_cb(struct ath10k_ce_pipe *ce_state)
1295 ath10k_pci_process_rx_cb(ce_state, ath10k_htc_rx_completion_handler);
1298 static void ath10k_pci_htt_htc_rx_cb(struct ath10k_ce_pipe *ce_state)
1300 /* CE4 polling needs to be done whenever CE pipe which transports
1301 * HTT Rx (target->host) is processed.
1303 ath10k_ce_per_engine_service(ce_state->ar, 4);
1305 ath10k_pci_process_rx_cb(ce_state, ath10k_htc_rx_completion_handler);
1308 /* Called by lower (CE) layer when data is received from the Target.
1309 * Only 10.4 firmware uses separate CE to transfer pktlog data.
1311 static void ath10k_pci_pktlog_rx_cb(struct ath10k_ce_pipe *ce_state)
1313 ath10k_pci_process_rx_cb(ce_state,
1314 ath10k_htt_rx_pktlog_completion_handler);
1317 /* Called by lower (CE) layer when a send to HTT Target completes. */
1318 static void ath10k_pci_htt_tx_cb(struct ath10k_ce_pipe *ce_state)
1320 struct ath10k *ar = ce_state->ar;
1321 struct sk_buff *skb;
1323 while (ath10k_ce_completed_send_next(ce_state, (void **)&skb) == 0) {
1324 /* no need to call tx completion for NULL pointers */
1328 dma_unmap_single(ar->dev, ATH10K_SKB_CB(skb)->paddr,
1329 skb->len, DMA_TO_DEVICE);
1330 ath10k_htt_hif_tx_complete(ar, skb);
1334 static void ath10k_pci_htt_rx_deliver(struct ath10k *ar, struct sk_buff *skb)
1336 skb_pull(skb, sizeof(struct ath10k_htc_hdr));
1337 ath10k_htt_t2h_msg_handler(ar, skb);
1340 /* Called by lower (CE) layer when HTT data is received from the Target. */
1341 static void ath10k_pci_htt_rx_cb(struct ath10k_ce_pipe *ce_state)
1343 /* CE4 polling needs to be done whenever CE pipe which transports
1344 * HTT Rx (target->host) is processed.
1346 ath10k_ce_per_engine_service(ce_state->ar, 4);
1348 ath10k_pci_process_htt_rx_cb(ce_state, ath10k_pci_htt_rx_deliver);
1351 int ath10k_pci_hif_tx_sg(struct ath10k *ar, u8 pipe_id,
1352 struct ath10k_hif_sg_item *items, int n_items)
1354 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1355 struct ath10k_ce *ce = ath10k_ce_priv(ar);
1356 struct ath10k_pci_pipe *pci_pipe = &ar_pci->pipe_info[pipe_id];
1357 struct ath10k_ce_pipe *ce_pipe = pci_pipe->ce_hdl;
1358 struct ath10k_ce_ring *src_ring = ce_pipe->src_ring;
1359 unsigned int nentries_mask;
1360 unsigned int sw_index;
1361 unsigned int write_index;
1364 spin_lock_bh(&ce->ce_lock);
1366 nentries_mask = src_ring->nentries_mask;
1367 sw_index = src_ring->sw_index;
1368 write_index = src_ring->write_index;
1370 if (unlikely(CE_RING_DELTA(nentries_mask,
1371 write_index, sw_index - 1) < n_items)) {
1376 for (i = 0; i < n_items - 1; i++) {
1377 ath10k_dbg(ar, ATH10K_DBG_PCI,
1378 "pci tx item %d paddr 0x%08x len %d n_items %d\n",
1379 i, items[i].paddr, items[i].len, n_items);
1380 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci tx data: ",
1381 items[i].vaddr, items[i].len);
1383 err = ath10k_ce_send_nolock(ce_pipe,
1384 items[i].transfer_context,
1387 items[i].transfer_id,
1388 CE_SEND_FLAG_GATHER);
1393 /* `i` is equal to `n_items -1` after for() */
1395 ath10k_dbg(ar, ATH10K_DBG_PCI,
1396 "pci tx item %d paddr 0x%08x len %d n_items %d\n",
1397 i, items[i].paddr, items[i].len, n_items);
1398 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci tx data: ",
1399 items[i].vaddr, items[i].len);
1401 err = ath10k_ce_send_nolock(ce_pipe,
1402 items[i].transfer_context,
1405 items[i].transfer_id,
1410 spin_unlock_bh(&ce->ce_lock);
1415 __ath10k_ce_send_revert(ce_pipe);
1417 spin_unlock_bh(&ce->ce_lock);
1421 int ath10k_pci_hif_diag_read(struct ath10k *ar, u32 address, void *buf,
1424 return ath10k_pci_diag_read_mem(ar, address, buf, buf_len);
1427 u16 ath10k_pci_hif_get_free_queue_number(struct ath10k *ar, u8 pipe)
1429 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1431 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif get free queue number\n");
1433 return ath10k_ce_num_free_src_entries(ar_pci->pipe_info[pipe].ce_hdl);
1436 static void ath10k_pci_dump_registers(struct ath10k *ar,
1437 struct ath10k_fw_crash_data *crash_data)
1439 __le32 reg_dump_values[REG_DUMP_COUNT_QCA988X] = {};
1442 lockdep_assert_held(&ar->data_lock);
1444 ret = ath10k_pci_diag_read_hi(ar, ®_dump_values[0],
1446 REG_DUMP_COUNT_QCA988X * sizeof(__le32));
1448 ath10k_err(ar, "failed to read firmware dump area: %d\n", ret);
1452 BUILD_BUG_ON(REG_DUMP_COUNT_QCA988X % 4);
1454 ath10k_err(ar, "firmware register dump:\n");
1455 for (i = 0; i < REG_DUMP_COUNT_QCA988X; i += 4)
1456 ath10k_err(ar, "[%02d]: 0x%08X 0x%08X 0x%08X 0x%08X\n",
1458 __le32_to_cpu(reg_dump_values[i]),
1459 __le32_to_cpu(reg_dump_values[i + 1]),
1460 __le32_to_cpu(reg_dump_values[i + 2]),
1461 __le32_to_cpu(reg_dump_values[i + 3]));
1466 for (i = 0; i < REG_DUMP_COUNT_QCA988X; i++)
1467 crash_data->registers[i] = reg_dump_values[i];
1470 static int ath10k_pci_dump_memory_section(struct ath10k *ar,
1471 const struct ath10k_mem_region *mem_region,
1472 u8 *buf, size_t buf_len)
1474 const struct ath10k_mem_section *cur_section, *next_section;
1475 unsigned int count, section_size, skip_size;
1478 if (!mem_region || !buf)
1481 cur_section = &mem_region->section_table.sections[0];
1483 if (mem_region->start > cur_section->start) {
1484 ath10k_warn(ar, "incorrect memdump region 0x%x with section start address 0x%x.\n",
1485 mem_region->start, cur_section->start);
1489 skip_size = cur_section->start - mem_region->start;
1491 /* fill the gap between the first register section and register
1494 for (i = 0; i < skip_size; i++) {
1495 *buf = ATH10K_MAGIC_NOT_COPIED;
1501 for (i = 0; cur_section != NULL; i++) {
1502 section_size = cur_section->end - cur_section->start;
1504 if (section_size <= 0) {
1505 ath10k_warn(ar, "incorrect ramdump format with start address 0x%x and stop address 0x%x\n",
1511 if ((i + 1) == mem_region->section_table.size) {
1513 next_section = NULL;
1516 next_section = cur_section + 1;
1518 if (cur_section->end > next_section->start) {
1519 ath10k_warn(ar, "next ramdump section 0x%x is smaller than current end address 0x%x\n",
1520 next_section->start,
1525 skip_size = next_section->start - cur_section->end;
1528 if (buf_len < (skip_size + section_size)) {
1529 ath10k_warn(ar, "ramdump buffer is too small: %zu\n", buf_len);
1533 buf_len -= skip_size + section_size;
1535 /* read section to dest memory */
1536 ret = ath10k_pci_diag_read_mem(ar, cur_section->start,
1539 ath10k_warn(ar, "failed to read ramdump from section 0x%x: %d\n",
1540 cur_section->start, ret);
1544 buf += section_size;
1545 count += section_size;
1547 /* fill in the gap between this section and the next */
1548 for (j = 0; j < skip_size; j++) {
1549 *buf = ATH10K_MAGIC_NOT_COPIED;
1556 /* this was the last section */
1559 cur_section = next_section;
1565 static int ath10k_pci_set_ram_config(struct ath10k *ar, u32 config)
1569 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
1570 FW_RAM_CONFIG_ADDRESS, config);
1572 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1573 FW_RAM_CONFIG_ADDRESS);
1574 if (val != config) {
1575 ath10k_warn(ar, "failed to set RAM config from 0x%x to 0x%x\n",
1583 static void ath10k_pci_dump_memory(struct ath10k *ar,
1584 struct ath10k_fw_crash_data *crash_data)
1586 const struct ath10k_hw_mem_layout *mem_layout;
1587 const struct ath10k_mem_region *current_region;
1588 struct ath10k_dump_ram_data_hdr *hdr;
1594 lockdep_assert_held(&ar->data_lock);
1599 mem_layout = ath10k_coredump_get_mem_layout(ar);
1603 current_region = &mem_layout->region_table.regions[0];
1605 buf = crash_data->ramdump_buf;
1606 buf_len = crash_data->ramdump_buf_len;
1608 memset(buf, 0, buf_len);
1610 for (i = 0; i < mem_layout->region_table.size; i++) {
1613 if (current_region->len > buf_len) {
1614 ath10k_warn(ar, "memory region %s size %d is larger that remaining ramdump buffer size %zu\n",
1615 current_region->name,
1616 current_region->len,
1621 /* To get IRAM dump, the host driver needs to switch target
1622 * ram config from DRAM to IRAM.
1624 if (current_region->type == ATH10K_MEM_REGION_TYPE_IRAM1 ||
1625 current_region->type == ATH10K_MEM_REGION_TYPE_IRAM2) {
1626 shift = current_region->start >> 20;
1628 ret = ath10k_pci_set_ram_config(ar, shift);
1630 ath10k_warn(ar, "failed to switch ram config to IRAM for section %s: %d\n",
1631 current_region->name, ret);
1636 /* Reserve space for the header. */
1638 buf += sizeof(*hdr);
1639 buf_len -= sizeof(*hdr);
1641 if (current_region->section_table.size > 0) {
1642 /* Copy each section individually. */
1643 count = ath10k_pci_dump_memory_section(ar,
1646 current_region->len);
1648 /* No individiual memory sections defined so we can
1649 * copy the entire memory region.
1651 ret = ath10k_pci_diag_read_mem(ar,
1652 current_region->start,
1654 current_region->len);
1656 ath10k_warn(ar, "failed to copy ramdump region %s: %d\n",
1657 current_region->name, ret);
1661 count = current_region->len;
1664 hdr->region_type = cpu_to_le32(current_region->type);
1665 hdr->start = cpu_to_le32(current_region->start);
1666 hdr->length = cpu_to_le32(count);
1669 /* Note: the header remains, just with zero length. */
1679 static void ath10k_pci_fw_crashed_dump(struct ath10k *ar)
1681 struct ath10k_fw_crash_data *crash_data;
1682 char guid[UUID_STRING_LEN + 1];
1684 spin_lock_bh(&ar->data_lock);
1686 ar->stats.fw_crash_counter++;
1688 crash_data = ath10k_coredump_new(ar);
1691 scnprintf(guid, sizeof(guid), "%pUl", &crash_data->guid);
1693 scnprintf(guid, sizeof(guid), "n/a");
1695 ath10k_err(ar, "firmware crashed! (guid %s)\n", guid);
1696 ath10k_print_driver_info(ar);
1697 ath10k_pci_dump_registers(ar, crash_data);
1698 ath10k_ce_dump_registers(ar, crash_data);
1699 ath10k_pci_dump_memory(ar, crash_data);
1701 spin_unlock_bh(&ar->data_lock);
1703 queue_work(ar->workqueue, &ar->restart_work);
1706 void ath10k_pci_hif_send_complete_check(struct ath10k *ar, u8 pipe,
1709 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif send complete check\n");
1714 * Decide whether to actually poll for completions, or just
1715 * wait for a later chance.
1716 * If there seem to be plenty of resources left, then just wait
1717 * since checking involves reading a CE register, which is a
1718 * relatively expensive operation.
1720 resources = ath10k_pci_hif_get_free_queue_number(ar, pipe);
1723 * If at least 50% of the total resources are still available,
1724 * don't bother checking again yet.
1726 if (resources > (host_ce_config_wlan[pipe].src_nentries >> 1))
1729 ath10k_ce_per_engine_service(ar, pipe);
1732 static void ath10k_pci_rx_retry_sync(struct ath10k *ar)
1734 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1736 del_timer_sync(&ar_pci->rx_post_retry);
1739 int ath10k_pci_hif_map_service_to_pipe(struct ath10k *ar, u16 service_id,
1740 u8 *ul_pipe, u8 *dl_pipe)
1742 const struct service_to_pipe *entry;
1743 bool ul_set = false, dl_set = false;
1746 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif map service\n");
1748 for (i = 0; i < ARRAY_SIZE(target_service_to_ce_map_wlan); i++) {
1749 entry = &target_service_to_ce_map_wlan[i];
1751 if (__le32_to_cpu(entry->service_id) != service_id)
1754 switch (__le32_to_cpu(entry->pipedir)) {
1759 *dl_pipe = __le32_to_cpu(entry->pipenum);
1764 *ul_pipe = __le32_to_cpu(entry->pipenum);
1770 *dl_pipe = __le32_to_cpu(entry->pipenum);
1771 *ul_pipe = __le32_to_cpu(entry->pipenum);
1778 if (WARN_ON(!ul_set || !dl_set))
1784 void ath10k_pci_hif_get_default_pipe(struct ath10k *ar,
1785 u8 *ul_pipe, u8 *dl_pipe)
1787 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif get default pipe\n");
1789 (void)ath10k_pci_hif_map_service_to_pipe(ar,
1790 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1794 void ath10k_pci_irq_msi_fw_mask(struct ath10k *ar)
1798 switch (ar->hw_rev) {
1799 case ATH10K_HW_QCA988X:
1800 case ATH10K_HW_QCA9887:
1801 case ATH10K_HW_QCA6174:
1802 case ATH10K_HW_QCA9377:
1803 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1805 val &= ~CORE_CTRL_PCIE_REG_31_MASK;
1806 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
1807 CORE_CTRL_ADDRESS, val);
1809 case ATH10K_HW_QCA99X0:
1810 case ATH10K_HW_QCA9984:
1811 case ATH10K_HW_QCA9888:
1812 case ATH10K_HW_QCA4019:
1813 /* TODO: Find appropriate register configuration for QCA99X0
1817 case ATH10K_HW_WCN3990:
1822 static void ath10k_pci_irq_msi_fw_unmask(struct ath10k *ar)
1826 switch (ar->hw_rev) {
1827 case ATH10K_HW_QCA988X:
1828 case ATH10K_HW_QCA9887:
1829 case ATH10K_HW_QCA6174:
1830 case ATH10K_HW_QCA9377:
1831 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1833 val |= CORE_CTRL_PCIE_REG_31_MASK;
1834 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
1835 CORE_CTRL_ADDRESS, val);
1837 case ATH10K_HW_QCA99X0:
1838 case ATH10K_HW_QCA9984:
1839 case ATH10K_HW_QCA9888:
1840 case ATH10K_HW_QCA4019:
1841 /* TODO: Find appropriate register configuration for QCA99X0
1842 * to unmask irq/MSI.
1845 case ATH10K_HW_WCN3990:
1850 static void ath10k_pci_irq_disable(struct ath10k *ar)
1852 ath10k_ce_disable_interrupts(ar);
1853 ath10k_pci_disable_and_clear_legacy_irq(ar);
1854 ath10k_pci_irq_msi_fw_mask(ar);
1857 static void ath10k_pci_irq_sync(struct ath10k *ar)
1859 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1861 synchronize_irq(ar_pci->pdev->irq);
1864 static void ath10k_pci_irq_enable(struct ath10k *ar)
1866 ath10k_ce_enable_interrupts(ar);
1867 ath10k_pci_enable_legacy_irq(ar);
1868 ath10k_pci_irq_msi_fw_unmask(ar);
1871 static int ath10k_pci_hif_start(struct ath10k *ar)
1873 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1875 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif start\n");
1877 napi_enable(&ar->napi);
1879 ath10k_pci_irq_enable(ar);
1880 ath10k_pci_rx_post(ar);
1882 pcie_capability_write_word(ar_pci->pdev, PCI_EXP_LNKCTL,
1888 static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe *pci_pipe)
1891 struct ath10k_ce_pipe *ce_pipe;
1892 struct ath10k_ce_ring *ce_ring;
1893 struct sk_buff *skb;
1896 ar = pci_pipe->hif_ce_state;
1897 ce_pipe = pci_pipe->ce_hdl;
1898 ce_ring = ce_pipe->dest_ring;
1903 if (!pci_pipe->buf_sz)
1906 for (i = 0; i < ce_ring->nentries; i++) {
1907 skb = ce_ring->per_transfer_context[i];
1911 ce_ring->per_transfer_context[i] = NULL;
1913 dma_unmap_single(ar->dev, ATH10K_SKB_RXCB(skb)->paddr,
1914 skb->len + skb_tailroom(skb),
1916 dev_kfree_skb_any(skb);
1920 static void ath10k_pci_tx_pipe_cleanup(struct ath10k_pci_pipe *pci_pipe)
1923 struct ath10k_ce_pipe *ce_pipe;
1924 struct ath10k_ce_ring *ce_ring;
1925 struct sk_buff *skb;
1928 ar = pci_pipe->hif_ce_state;
1929 ce_pipe = pci_pipe->ce_hdl;
1930 ce_ring = ce_pipe->src_ring;
1935 if (!pci_pipe->buf_sz)
1938 for (i = 0; i < ce_ring->nentries; i++) {
1939 skb = ce_ring->per_transfer_context[i];
1943 ce_ring->per_transfer_context[i] = NULL;
1945 ath10k_htc_tx_completion_handler(ar, skb);
1950 * Cleanup residual buffers for device shutdown:
1951 * buffers that were enqueued for receive
1952 * buffers that were to be sent
1953 * Note: Buffers that had completed but which were
1954 * not yet processed are on a completion queue. They
1955 * are handled when the completion thread shuts down.
1957 static void ath10k_pci_buffer_cleanup(struct ath10k *ar)
1959 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1962 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
1963 struct ath10k_pci_pipe *pipe_info;
1965 pipe_info = &ar_pci->pipe_info[pipe_num];
1966 ath10k_pci_rx_pipe_cleanup(pipe_info);
1967 ath10k_pci_tx_pipe_cleanup(pipe_info);
1971 void ath10k_pci_ce_deinit(struct ath10k *ar)
1975 for (i = 0; i < CE_COUNT; i++)
1976 ath10k_ce_deinit_pipe(ar, i);
1979 void ath10k_pci_flush(struct ath10k *ar)
1981 ath10k_pci_rx_retry_sync(ar);
1982 ath10k_pci_buffer_cleanup(ar);
1985 static void ath10k_pci_hif_stop(struct ath10k *ar)
1987 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1988 unsigned long flags;
1990 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif stop\n");
1992 /* Most likely the device has HTT Rx ring configured. The only way to
1993 * prevent the device from accessing (and possible corrupting) host
1994 * memory is to reset the chip now.
1996 * There's also no known way of masking MSI interrupts on the device.
1997 * For ranged MSI the CE-related interrupts can be masked. However
1998 * regardless how many MSI interrupts are assigned the first one
1999 * is always used for firmware indications (crashes) and cannot be
2000 * masked. To prevent the device from asserting the interrupt reset it
2001 * before proceeding with cleanup.
2003 ath10k_pci_safe_chip_reset(ar);
2005 ath10k_pci_irq_disable(ar);
2006 ath10k_pci_irq_sync(ar);
2007 ath10k_pci_flush(ar);
2008 napi_synchronize(&ar->napi);
2009 napi_disable(&ar->napi);
2011 spin_lock_irqsave(&ar_pci->ps_lock, flags);
2012 WARN_ON(ar_pci->ps_wake_refcount > 0);
2013 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
2016 int ath10k_pci_hif_exchange_bmi_msg(struct ath10k *ar,
2017 void *req, u32 req_len,
2018 void *resp, u32 *resp_len)
2020 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2021 struct ath10k_pci_pipe *pci_tx = &ar_pci->pipe_info[BMI_CE_NUM_TO_TARG];
2022 struct ath10k_pci_pipe *pci_rx = &ar_pci->pipe_info[BMI_CE_NUM_TO_HOST];
2023 struct ath10k_ce_pipe *ce_tx = pci_tx->ce_hdl;
2024 struct ath10k_ce_pipe *ce_rx = pci_rx->ce_hdl;
2025 dma_addr_t req_paddr = 0;
2026 dma_addr_t resp_paddr = 0;
2027 struct bmi_xfer xfer = {};
2028 void *treq, *tresp = NULL;
2033 if (resp && !resp_len)
2036 if (resp && resp_len && *resp_len == 0)
2039 treq = kmemdup(req, req_len, GFP_KERNEL);
2043 req_paddr = dma_map_single(ar->dev, treq, req_len, DMA_TO_DEVICE);
2044 ret = dma_mapping_error(ar->dev, req_paddr);
2050 if (resp && resp_len) {
2051 tresp = kzalloc(*resp_len, GFP_KERNEL);
2057 resp_paddr = dma_map_single(ar->dev, tresp, *resp_len,
2059 ret = dma_mapping_error(ar->dev, resp_paddr);
2065 xfer.wait_for_resp = true;
2068 ath10k_ce_rx_post_buf(ce_rx, &xfer, resp_paddr);
2071 ret = ath10k_ce_send(ce_tx, &xfer, req_paddr, req_len, -1, 0);
2075 ret = ath10k_pci_bmi_wait(ar, ce_tx, ce_rx, &xfer);
2077 dma_addr_t unused_buffer;
2078 unsigned int unused_nbytes;
2079 unsigned int unused_id;
2081 ath10k_ce_cancel_send_next(ce_tx, NULL, &unused_buffer,
2082 &unused_nbytes, &unused_id);
2084 /* non-zero means we did not time out */
2090 dma_addr_t unused_buffer;
2092 ath10k_ce_revoke_recv_next(ce_rx, NULL, &unused_buffer);
2093 dma_unmap_single(ar->dev, resp_paddr,
2094 *resp_len, DMA_FROM_DEVICE);
2097 dma_unmap_single(ar->dev, req_paddr, req_len, DMA_TO_DEVICE);
2099 if (ret == 0 && resp_len) {
2100 *resp_len = min(*resp_len, xfer.resp_len);
2101 memcpy(resp, tresp, xfer.resp_len);
2110 static void ath10k_pci_bmi_send_done(struct ath10k_ce_pipe *ce_state)
2112 struct bmi_xfer *xfer;
2114 if (ath10k_ce_completed_send_next(ce_state, (void **)&xfer))
2117 xfer->tx_done = true;
2120 static void ath10k_pci_bmi_recv_data(struct ath10k_ce_pipe *ce_state)
2122 struct ath10k *ar = ce_state->ar;
2123 struct bmi_xfer *xfer;
2124 unsigned int nbytes;
2126 if (ath10k_ce_completed_recv_next(ce_state, (void **)&xfer,
2130 if (WARN_ON_ONCE(!xfer))
2133 if (!xfer->wait_for_resp) {
2134 ath10k_warn(ar, "unexpected: BMI data received; ignoring\n");
2138 xfer->resp_len = nbytes;
2139 xfer->rx_done = true;
2142 static int ath10k_pci_bmi_wait(struct ath10k *ar,
2143 struct ath10k_ce_pipe *tx_pipe,
2144 struct ath10k_ce_pipe *rx_pipe,
2145 struct bmi_xfer *xfer)
2147 unsigned long timeout = jiffies + BMI_COMMUNICATION_TIMEOUT_HZ;
2148 unsigned long started = jiffies;
2152 while (time_before_eq(jiffies, timeout)) {
2153 ath10k_pci_bmi_send_done(tx_pipe);
2154 ath10k_pci_bmi_recv_data(rx_pipe);
2156 if (xfer->tx_done && (xfer->rx_done == xfer->wait_for_resp)) {
2167 dur = jiffies - started;
2169 ath10k_dbg(ar, ATH10K_DBG_BMI,
2170 "bmi cmd took %lu jiffies hz %d ret %d\n",
2176 * Send an interrupt to the device to wake up the Target CPU
2177 * so it has an opportunity to notice any changed state.
2179 static int ath10k_pci_wake_target_cpu(struct ath10k *ar)
2183 addr = SOC_CORE_BASE_ADDRESS + CORE_CTRL_ADDRESS;
2184 val = ath10k_pci_read32(ar, addr);
2185 val |= CORE_CTRL_CPU_INTR_MASK;
2186 ath10k_pci_write32(ar, addr, val);
2191 static int ath10k_pci_get_num_banks(struct ath10k *ar)
2193 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2195 switch (ar_pci->pdev->device) {
2196 case QCA988X_2_0_DEVICE_ID:
2197 case QCA99X0_2_0_DEVICE_ID:
2198 case QCA9888_2_0_DEVICE_ID:
2199 case QCA9984_1_0_DEVICE_ID:
2200 case QCA9887_1_0_DEVICE_ID:
2202 case QCA6164_2_1_DEVICE_ID:
2203 case QCA6174_2_1_DEVICE_ID:
2204 switch (MS(ar->chip_id, SOC_CHIP_ID_REV)) {
2205 case QCA6174_HW_1_0_CHIP_ID_REV:
2206 case QCA6174_HW_1_1_CHIP_ID_REV:
2207 case QCA6174_HW_2_1_CHIP_ID_REV:
2208 case QCA6174_HW_2_2_CHIP_ID_REV:
2210 case QCA6174_HW_1_3_CHIP_ID_REV:
2212 case QCA6174_HW_3_0_CHIP_ID_REV:
2213 case QCA6174_HW_3_1_CHIP_ID_REV:
2214 case QCA6174_HW_3_2_CHIP_ID_REV:
2218 case QCA9377_1_0_DEVICE_ID:
2222 ath10k_warn(ar, "unknown number of banks, assuming 1\n");
2226 static int ath10k_bus_get_num_banks(struct ath10k *ar)
2228 struct ath10k_ce *ce = ath10k_ce_priv(ar);
2230 return ce->bus_ops->get_num_banks(ar);
2233 int ath10k_pci_init_config(struct ath10k *ar)
2235 u32 interconnect_targ_addr;
2236 u32 pcie_state_targ_addr = 0;
2237 u32 pipe_cfg_targ_addr = 0;
2238 u32 svc_to_pipe_map = 0;
2239 u32 pcie_config_flags = 0;
2241 u32 ealloc_targ_addr;
2243 u32 flag2_targ_addr;
2246 /* Download to Target the CE Config and the service-to-CE map */
2247 interconnect_targ_addr =
2248 host_interest_item_address(HI_ITEM(hi_interconnect_state));
2250 /* Supply Target-side CE configuration */
2251 ret = ath10k_pci_diag_read32(ar, interconnect_targ_addr,
2252 &pcie_state_targ_addr);
2254 ath10k_err(ar, "Failed to get pcie state addr: %d\n", ret);
2258 if (pcie_state_targ_addr == 0) {
2260 ath10k_err(ar, "Invalid pcie state addr\n");
2264 ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
2265 offsetof(struct pcie_state,
2267 &pipe_cfg_targ_addr);
2269 ath10k_err(ar, "Failed to get pipe cfg addr: %d\n", ret);
2273 if (pipe_cfg_targ_addr == 0) {
2275 ath10k_err(ar, "Invalid pipe cfg addr\n");
2279 ret = ath10k_pci_diag_write_mem(ar, pipe_cfg_targ_addr,
2280 target_ce_config_wlan,
2281 sizeof(struct ce_pipe_config) *
2282 NUM_TARGET_CE_CONFIG_WLAN);
2285 ath10k_err(ar, "Failed to write pipe cfg: %d\n", ret);
2289 ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
2290 offsetof(struct pcie_state,
2294 ath10k_err(ar, "Failed to get svc/pipe map: %d\n", ret);
2298 if (svc_to_pipe_map == 0) {
2300 ath10k_err(ar, "Invalid svc_to_pipe map\n");
2304 ret = ath10k_pci_diag_write_mem(ar, svc_to_pipe_map,
2305 target_service_to_ce_map_wlan,
2306 sizeof(target_service_to_ce_map_wlan));
2308 ath10k_err(ar, "Failed to write svc/pipe map: %d\n", ret);
2312 ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
2313 offsetof(struct pcie_state,
2315 &pcie_config_flags);
2317 ath10k_err(ar, "Failed to get pcie config_flags: %d\n", ret);
2321 pcie_config_flags &= ~PCIE_CONFIG_FLAG_ENABLE_L1;
2323 ret = ath10k_pci_diag_write32(ar, (pcie_state_targ_addr +
2324 offsetof(struct pcie_state,
2328 ath10k_err(ar, "Failed to write pcie config_flags: %d\n", ret);
2332 /* configure early allocation */
2333 ealloc_targ_addr = host_interest_item_address(HI_ITEM(hi_early_alloc));
2335 ret = ath10k_pci_diag_read32(ar, ealloc_targ_addr, &ealloc_value);
2337 ath10k_err(ar, "Failed to get early alloc val: %d\n", ret);
2341 /* first bank is switched to IRAM */
2342 ealloc_value |= ((HI_EARLY_ALLOC_MAGIC << HI_EARLY_ALLOC_MAGIC_SHIFT) &
2343 HI_EARLY_ALLOC_MAGIC_MASK);
2344 ealloc_value |= ((ath10k_bus_get_num_banks(ar) <<
2345 HI_EARLY_ALLOC_IRAM_BANKS_SHIFT) &
2346 HI_EARLY_ALLOC_IRAM_BANKS_MASK);
2348 ret = ath10k_pci_diag_write32(ar, ealloc_targ_addr, ealloc_value);
2350 ath10k_err(ar, "Failed to set early alloc val: %d\n", ret);
2354 /* Tell Target to proceed with initialization */
2355 flag2_targ_addr = host_interest_item_address(HI_ITEM(hi_option_flag2));
2357 ret = ath10k_pci_diag_read32(ar, flag2_targ_addr, &flag2_value);
2359 ath10k_err(ar, "Failed to get option val: %d\n", ret);
2363 flag2_value |= HI_OPTION_EARLY_CFG_DONE;
2365 ret = ath10k_pci_diag_write32(ar, flag2_targ_addr, flag2_value);
2367 ath10k_err(ar, "Failed to set option val: %d\n", ret);
2374 static void ath10k_pci_override_ce_config(struct ath10k *ar)
2376 struct ce_attr *attr;
2377 struct ce_pipe_config *config;
2379 /* For QCA6174 we're overriding the Copy Engine 5 configuration,
2380 * since it is currently used for other feature.
2383 /* Override Host's Copy Engine 5 configuration */
2384 attr = &host_ce_config_wlan[5];
2385 attr->src_sz_max = 0;
2386 attr->dest_nentries = 0;
2388 /* Override Target firmware's Copy Engine configuration */
2389 config = &target_ce_config_wlan[5];
2390 config->pipedir = __cpu_to_le32(PIPEDIR_OUT);
2391 config->nbytes_max = __cpu_to_le32(2048);
2393 /* Map from service/endpoint to Copy Engine */
2394 target_service_to_ce_map_wlan[15].pipenum = __cpu_to_le32(1);
2397 int ath10k_pci_alloc_pipes(struct ath10k *ar)
2399 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2400 struct ath10k_pci_pipe *pipe;
2401 struct ath10k_ce *ce = ath10k_ce_priv(ar);
2404 for (i = 0; i < CE_COUNT; i++) {
2405 pipe = &ar_pci->pipe_info[i];
2406 pipe->ce_hdl = &ce->ce_states[i];
2408 pipe->hif_ce_state = ar;
2410 ret = ath10k_ce_alloc_pipe(ar, i, &host_ce_config_wlan[i]);
2412 ath10k_err(ar, "failed to allocate copy engine pipe %d: %d\n",
2417 /* Last CE is Diagnostic Window */
2418 if (i == CE_DIAG_PIPE) {
2419 ar_pci->ce_diag = pipe->ce_hdl;
2423 pipe->buf_sz = (size_t)(host_ce_config_wlan[i].src_sz_max);
2429 void ath10k_pci_free_pipes(struct ath10k *ar)
2433 for (i = 0; i < CE_COUNT; i++)
2434 ath10k_ce_free_pipe(ar, i);
2437 int ath10k_pci_init_pipes(struct ath10k *ar)
2441 for (i = 0; i < CE_COUNT; i++) {
2442 ret = ath10k_ce_init_pipe(ar, i, &host_ce_config_wlan[i]);
2444 ath10k_err(ar, "failed to initialize copy engine pipe %d: %d\n",
2453 static bool ath10k_pci_has_fw_crashed(struct ath10k *ar)
2455 return ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS) &
2456 FW_IND_EVENT_PENDING;
2459 static void ath10k_pci_fw_crashed_clear(struct ath10k *ar)
2463 val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
2464 val &= ~FW_IND_EVENT_PENDING;
2465 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, val);
2468 static bool ath10k_pci_has_device_gone(struct ath10k *ar)
2472 val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
2473 return (val == 0xffffffff);
2476 /* this function effectively clears target memory controller assert line */
2477 static void ath10k_pci_warm_reset_si0(struct ath10k *ar)
2481 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
2482 ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
2483 val | SOC_RESET_CONTROL_SI0_RST_MASK);
2484 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
2488 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
2489 ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
2490 val & ~SOC_RESET_CONTROL_SI0_RST_MASK);
2491 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
2496 static void ath10k_pci_warm_reset_cpu(struct ath10k *ar)
2500 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, 0);
2502 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
2503 SOC_RESET_CONTROL_ADDRESS);
2504 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
2505 val | SOC_RESET_CONTROL_CPU_WARM_RST_MASK);
2508 static void ath10k_pci_warm_reset_ce(struct ath10k *ar)
2512 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
2513 SOC_RESET_CONTROL_ADDRESS);
2515 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
2516 val | SOC_RESET_CONTROL_CE_RST_MASK);
2518 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
2519 val & ~SOC_RESET_CONTROL_CE_RST_MASK);
2522 static void ath10k_pci_warm_reset_clear_lf(struct ath10k *ar)
2526 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
2527 SOC_LF_TIMER_CONTROL0_ADDRESS);
2528 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS +
2529 SOC_LF_TIMER_CONTROL0_ADDRESS,
2530 val & ~SOC_LF_TIMER_CONTROL0_ENABLE_MASK);
2533 static int ath10k_pci_warm_reset(struct ath10k *ar)
2537 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot warm reset\n");
2539 spin_lock_bh(&ar->data_lock);
2540 ar->stats.fw_warm_reset_counter++;
2541 spin_unlock_bh(&ar->data_lock);
2543 ath10k_pci_irq_disable(ar);
2545 /* Make sure the target CPU is not doing anything dangerous, e.g. if it
2546 * were to access copy engine while host performs copy engine reset
2547 * then it is possible for the device to confuse pci-e controller to
2548 * the point of bringing host system to a complete stop (i.e. hang).
2550 ath10k_pci_warm_reset_si0(ar);
2551 ath10k_pci_warm_reset_cpu(ar);
2552 ath10k_pci_init_pipes(ar);
2553 ath10k_pci_wait_for_target_init(ar);
2555 ath10k_pci_warm_reset_clear_lf(ar);
2556 ath10k_pci_warm_reset_ce(ar);
2557 ath10k_pci_warm_reset_cpu(ar);
2558 ath10k_pci_init_pipes(ar);
2560 ret = ath10k_pci_wait_for_target_init(ar);
2562 ath10k_warn(ar, "failed to wait for target init: %d\n", ret);
2566 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot warm reset complete\n");
2571 static int ath10k_pci_qca99x0_soft_chip_reset(struct ath10k *ar)
2573 ath10k_pci_irq_disable(ar);
2574 return ath10k_pci_qca99x0_chip_reset(ar);
2577 static int ath10k_pci_safe_chip_reset(struct ath10k *ar)
2579 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2581 if (!ar_pci->pci_soft_reset)
2584 return ar_pci->pci_soft_reset(ar);
2587 static int ath10k_pci_qca988x_chip_reset(struct ath10k *ar)
2592 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot 988x chip reset\n");
2594 /* Some hardware revisions (e.g. CUS223v2) has issues with cold reset.
2595 * It is thus preferred to use warm reset which is safer but may not be
2596 * able to recover the device from all possible fail scenarios.
2598 * Warm reset doesn't always work on first try so attempt it a few
2599 * times before giving up.
2601 for (i = 0; i < ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS; i++) {
2602 ret = ath10k_pci_warm_reset(ar);
2604 ath10k_warn(ar, "failed to warm reset attempt %d of %d: %d\n",
2605 i + 1, ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS,
2610 /* FIXME: Sometimes copy engine doesn't recover after warm
2611 * reset. In most cases this needs cold reset. In some of these
2612 * cases the device is in such a state that a cold reset may
2615 * Reading any host interest register via copy engine is
2616 * sufficient to verify if device is capable of booting
2619 ret = ath10k_pci_init_pipes(ar);
2621 ath10k_warn(ar, "failed to init copy engine: %d\n",
2626 ret = ath10k_pci_diag_read32(ar, QCA988X_HOST_INTEREST_ADDRESS,
2629 ath10k_warn(ar, "failed to poke copy engine: %d\n",
2634 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot chip reset complete (warm)\n");
2638 if (ath10k_pci_reset_mode == ATH10K_PCI_RESET_WARM_ONLY) {
2639 ath10k_warn(ar, "refusing cold reset as requested\n");
2643 ret = ath10k_pci_cold_reset(ar);
2645 ath10k_warn(ar, "failed to cold reset: %d\n", ret);
2649 ret = ath10k_pci_wait_for_target_init(ar);
2651 ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
2656 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca988x chip reset complete (cold)\n");
2661 static int ath10k_pci_qca6174_chip_reset(struct ath10k *ar)
2665 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca6174 chip reset\n");
2667 /* FIXME: QCA6174 requires cold + warm reset to work. */
2669 ret = ath10k_pci_cold_reset(ar);
2671 ath10k_warn(ar, "failed to cold reset: %d\n", ret);
2675 ret = ath10k_pci_wait_for_target_init(ar);
2677 ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
2682 ret = ath10k_pci_warm_reset(ar);
2684 ath10k_warn(ar, "failed to warm reset: %d\n", ret);
2688 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca6174 chip reset complete (cold)\n");
2693 static int ath10k_pci_qca99x0_chip_reset(struct ath10k *ar)
2697 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca99x0 chip reset\n");
2699 ret = ath10k_pci_cold_reset(ar);
2701 ath10k_warn(ar, "failed to cold reset: %d\n", ret);
2705 ret = ath10k_pci_wait_for_target_init(ar);
2707 ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
2712 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca99x0 chip reset complete (cold)\n");
2717 static int ath10k_pci_chip_reset(struct ath10k *ar)
2719 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2721 if (WARN_ON(!ar_pci->pci_hard_reset))
2724 return ar_pci->pci_hard_reset(ar);
2727 static int ath10k_pci_hif_power_up(struct ath10k *ar)
2729 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2732 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif power up\n");
2734 pcie_capability_read_word(ar_pci->pdev, PCI_EXP_LNKCTL,
2736 pcie_capability_write_word(ar_pci->pdev, PCI_EXP_LNKCTL,
2737 ar_pci->link_ctl & ~PCI_EXP_LNKCTL_ASPMC);
2740 * Bring the target up cleanly.
2742 * The target may be in an undefined state with an AUX-powered Target
2743 * and a Host in WoW mode. If the Host crashes, loses power, or is
2744 * restarted (without unloading the driver) then the Target is left
2745 * (aux) powered and running. On a subsequent driver load, the Target
2746 * is in an unexpected state. We try to catch that here in order to
2747 * reset the Target and retry the probe.
2749 ret = ath10k_pci_chip_reset(ar);
2751 if (ath10k_pci_has_fw_crashed(ar)) {
2752 ath10k_warn(ar, "firmware crashed during chip reset\n");
2753 ath10k_pci_fw_crashed_clear(ar);
2754 ath10k_pci_fw_crashed_dump(ar);
2757 ath10k_err(ar, "failed to reset chip: %d\n", ret);
2761 ret = ath10k_pci_init_pipes(ar);
2763 ath10k_err(ar, "failed to initialize CE: %d\n", ret);
2767 ret = ath10k_pci_init_config(ar);
2769 ath10k_err(ar, "failed to setup init config: %d\n", ret);
2773 ret = ath10k_pci_wake_target_cpu(ar);
2775 ath10k_err(ar, "could not wake up target CPU: %d\n", ret);
2782 ath10k_pci_ce_deinit(ar);
2788 void ath10k_pci_hif_power_down(struct ath10k *ar)
2790 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif power down\n");
2792 /* Currently hif_power_up performs effectively a reset and hif_stop
2793 * resets the chip as well so there's no point in resetting here.
2797 static int ath10k_pci_hif_suspend(struct ath10k *ar)
2799 /* Nothing to do; the important stuff is in the driver suspend. */
2803 static int ath10k_pci_suspend(struct ath10k *ar)
2805 /* The grace timer can still be counting down and ar->ps_awake be true.
2806 * It is known that the device may be asleep after resuming regardless
2807 * of the SoC powersave state before suspending. Hence make sure the
2808 * device is asleep before proceeding.
2810 ath10k_pci_sleep_sync(ar);
2815 static int ath10k_pci_hif_resume(struct ath10k *ar)
2817 /* Nothing to do; the important stuff is in the driver resume. */
2821 static int ath10k_pci_resume(struct ath10k *ar)
2823 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2824 struct pci_dev *pdev = ar_pci->pdev;
2828 ret = ath10k_pci_force_wake(ar);
2830 ath10k_err(ar, "failed to wake up target: %d\n", ret);
2834 /* Suspend/Resume resets the PCI configuration space, so we have to
2835 * re-disable the RETRY_TIMEOUT register (0x41) to keep PCI Tx retries
2836 * from interfering with C3 CPU state. pci_restore_state won't help
2837 * here since it only restores the first 64 bytes pci config header.
2839 pci_read_config_dword(pdev, 0x40, &val);
2840 if ((val & 0x0000ff00) != 0)
2841 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
2846 static bool ath10k_pci_validate_cal(void *data, size_t size)
2848 __le16 *cal_words = data;
2855 for (i = 0; i < size / 2; i++)
2856 checksum ^= le16_to_cpu(cal_words[i]);
2858 return checksum == 0xffff;
2861 static void ath10k_pci_enable_eeprom(struct ath10k *ar)
2863 /* Enable SI clock */
2864 ath10k_pci_soc_write32(ar, CLOCK_CONTROL_OFFSET, 0x0);
2866 /* Configure GPIOs for I2C operation */
2867 ath10k_pci_write32(ar,
2868 GPIO_BASE_ADDRESS + GPIO_PIN0_OFFSET +
2869 4 * QCA9887_1_0_I2C_SDA_GPIO_PIN,
2870 SM(QCA9887_1_0_I2C_SDA_PIN_CONFIG,
2872 SM(1, GPIO_PIN0_PAD_PULL));
2874 ath10k_pci_write32(ar,
2875 GPIO_BASE_ADDRESS + GPIO_PIN0_OFFSET +
2876 4 * QCA9887_1_0_SI_CLK_GPIO_PIN,
2877 SM(QCA9887_1_0_SI_CLK_PIN_CONFIG, GPIO_PIN0_CONFIG) |
2878 SM(1, GPIO_PIN0_PAD_PULL));
2880 ath10k_pci_write32(ar,
2882 QCA9887_1_0_GPIO_ENABLE_W1TS_LOW_ADDRESS,
2883 1u << QCA9887_1_0_SI_CLK_GPIO_PIN);
2885 /* In Swift ASIC - EEPROM clock will be (110MHz/512) = 214KHz */
2886 ath10k_pci_write32(ar,
2887 SI_BASE_ADDRESS + SI_CONFIG_OFFSET,
2888 SM(1, SI_CONFIG_ERR_INT) |
2889 SM(1, SI_CONFIG_BIDIR_OD_DATA) |
2890 SM(1, SI_CONFIG_I2C) |
2891 SM(1, SI_CONFIG_POS_SAMPLE) |
2892 SM(1, SI_CONFIG_INACTIVE_DATA) |
2893 SM(1, SI_CONFIG_INACTIVE_CLK) |
2894 SM(8, SI_CONFIG_DIVIDER));
2897 static int ath10k_pci_read_eeprom(struct ath10k *ar, u16 addr, u8 *out)
2902 /* set device select byte and for the read operation */
2903 reg = QCA9887_EEPROM_SELECT_READ |
2904 SM(addr, QCA9887_EEPROM_ADDR_LO) |
2905 SM(addr >> 8, QCA9887_EEPROM_ADDR_HI);
2906 ath10k_pci_write32(ar, SI_BASE_ADDRESS + SI_TX_DATA0_OFFSET, reg);
2908 /* write transmit data, transfer length, and START bit */
2909 ath10k_pci_write32(ar, SI_BASE_ADDRESS + SI_CS_OFFSET,
2910 SM(1, SI_CS_START) | SM(1, SI_CS_RX_CNT) |
2911 SM(4, SI_CS_TX_CNT));
2913 /* wait max 1 sec */
2914 wait_limit = 100000;
2916 /* wait for SI_CS_DONE_INT */
2918 reg = ath10k_pci_read32(ar, SI_BASE_ADDRESS + SI_CS_OFFSET);
2919 if (MS(reg, SI_CS_DONE_INT))
2924 } while (wait_limit > 0);
2926 if (!MS(reg, SI_CS_DONE_INT)) {
2927 ath10k_err(ar, "timeout while reading device EEPROM at %04x\n",
2932 /* clear SI_CS_DONE_INT */
2933 ath10k_pci_write32(ar, SI_BASE_ADDRESS + SI_CS_OFFSET, reg);
2935 if (MS(reg, SI_CS_DONE_ERR)) {
2936 ath10k_err(ar, "failed to read device EEPROM at %04x\n", addr);
2940 /* extract receive data */
2941 reg = ath10k_pci_read32(ar, SI_BASE_ADDRESS + SI_RX_DATA0_OFFSET);
2947 static int ath10k_pci_hif_fetch_cal_eeprom(struct ath10k *ar, void **data,
2954 if (!QCA_REV_9887(ar))
2957 calsize = ar->hw_params.cal_data_len;
2958 caldata = kmalloc(calsize, GFP_KERNEL);
2962 ath10k_pci_enable_eeprom(ar);
2964 for (i = 0; i < calsize; i++) {
2965 ret = ath10k_pci_read_eeprom(ar, i, &caldata[i]);
2970 if (!ath10k_pci_validate_cal(caldata, calsize))
2974 *data_len = calsize;
2984 static const struct ath10k_hif_ops ath10k_pci_hif_ops = {
2985 .tx_sg = ath10k_pci_hif_tx_sg,
2986 .diag_read = ath10k_pci_hif_diag_read,
2987 .diag_write = ath10k_pci_diag_write_mem,
2988 .exchange_bmi_msg = ath10k_pci_hif_exchange_bmi_msg,
2989 .start = ath10k_pci_hif_start,
2990 .stop = ath10k_pci_hif_stop,
2991 .map_service_to_pipe = ath10k_pci_hif_map_service_to_pipe,
2992 .get_default_pipe = ath10k_pci_hif_get_default_pipe,
2993 .send_complete_check = ath10k_pci_hif_send_complete_check,
2994 .get_free_queue_number = ath10k_pci_hif_get_free_queue_number,
2995 .power_up = ath10k_pci_hif_power_up,
2996 .power_down = ath10k_pci_hif_power_down,
2997 .read32 = ath10k_pci_read32,
2998 .write32 = ath10k_pci_write32,
2999 .suspend = ath10k_pci_hif_suspend,
3000 .resume = ath10k_pci_hif_resume,
3001 .fetch_cal_eeprom = ath10k_pci_hif_fetch_cal_eeprom,
3005 * Top-level interrupt handler for all PCI interrupts from a Target.
3006 * When a block of MSI interrupts is allocated, this top-level handler
3007 * is not used; instead, we directly call the correct sub-handler.
3009 static irqreturn_t ath10k_pci_interrupt_handler(int irq, void *arg)
3011 struct ath10k *ar = arg;
3012 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
3015 if (ath10k_pci_has_device_gone(ar))
3018 ret = ath10k_pci_force_wake(ar);
3020 ath10k_warn(ar, "failed to wake device up on irq: %d\n", ret);
3024 if ((ar_pci->oper_irq_mode == ATH10K_PCI_IRQ_LEGACY) &&
3025 !ath10k_pci_irq_pending(ar))
3028 ath10k_pci_disable_and_clear_legacy_irq(ar);
3029 ath10k_pci_irq_msi_fw_mask(ar);
3030 napi_schedule(&ar->napi);
3035 static int ath10k_pci_napi_poll(struct napi_struct *ctx, int budget)
3037 struct ath10k *ar = container_of(ctx, struct ath10k, napi);
3040 if (ath10k_pci_has_fw_crashed(ar)) {
3041 ath10k_pci_fw_crashed_clear(ar);
3042 ath10k_pci_fw_crashed_dump(ar);
3047 ath10k_ce_per_engine_service_any(ar);
3049 done = ath10k_htt_txrx_compl_task(ar, budget);
3051 if (done < budget) {
3052 napi_complete_done(ctx, done);
3053 /* In case of MSI, it is possible that interrupts are received
3054 * while NAPI poll is inprogress. So pending interrupts that are
3055 * received after processing all copy engine pipes by NAPI poll
3056 * will not be handled again. This is causing failure to
3057 * complete boot sequence in x86 platform. So before enabling
3058 * interrupts safer to check for pending interrupts for
3059 * immediate servicing.
3061 if (ath10k_ce_interrupt_summary(ar)) {
3062 napi_reschedule(ctx);
3065 ath10k_pci_enable_legacy_irq(ar);
3066 ath10k_pci_irq_msi_fw_unmask(ar);
3073 static int ath10k_pci_request_irq_msi(struct ath10k *ar)
3075 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
3078 ret = request_irq(ar_pci->pdev->irq,
3079 ath10k_pci_interrupt_handler,
3080 IRQF_SHARED, "ath10k_pci", ar);
3082 ath10k_warn(ar, "failed to request MSI irq %d: %d\n",
3083 ar_pci->pdev->irq, ret);
3090 static int ath10k_pci_request_irq_legacy(struct ath10k *ar)
3092 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
3095 ret = request_irq(ar_pci->pdev->irq,
3096 ath10k_pci_interrupt_handler,
3097 IRQF_SHARED, "ath10k_pci", ar);
3099 ath10k_warn(ar, "failed to request legacy irq %d: %d\n",
3100 ar_pci->pdev->irq, ret);
3107 static int ath10k_pci_request_irq(struct ath10k *ar)
3109 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
3111 switch (ar_pci->oper_irq_mode) {
3112 case ATH10K_PCI_IRQ_LEGACY:
3113 return ath10k_pci_request_irq_legacy(ar);
3114 case ATH10K_PCI_IRQ_MSI:
3115 return ath10k_pci_request_irq_msi(ar);
3121 static void ath10k_pci_free_irq(struct ath10k *ar)
3123 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
3125 free_irq(ar_pci->pdev->irq, ar);
3128 void ath10k_pci_init_napi(struct ath10k *ar)
3130 netif_napi_add(&ar->napi_dev, &ar->napi, ath10k_pci_napi_poll,
3131 ATH10K_NAPI_BUDGET);
3134 static int ath10k_pci_init_irq(struct ath10k *ar)
3136 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
3139 ath10k_pci_init_napi(ar);
3141 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_AUTO)
3142 ath10k_info(ar, "limiting irq mode to: %d\n",
3143 ath10k_pci_irq_mode);
3146 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_LEGACY) {
3147 ar_pci->oper_irq_mode = ATH10K_PCI_IRQ_MSI;
3148 ret = pci_enable_msi(ar_pci->pdev);
3157 * A potential race occurs here: The CORE_BASE write
3158 * depends on target correctly decoding AXI address but
3159 * host won't know when target writes BAR to CORE_CTRL.
3160 * This write might get lost if target has NOT written BAR.
3161 * For now, fix the race by repeating the write in below
3162 * synchronization checking.
3164 ar_pci->oper_irq_mode = ATH10K_PCI_IRQ_LEGACY;
3166 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
3167 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
3172 static void ath10k_pci_deinit_irq_legacy(struct ath10k *ar)
3174 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
3178 static int ath10k_pci_deinit_irq(struct ath10k *ar)
3180 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
3182 switch (ar_pci->oper_irq_mode) {
3183 case ATH10K_PCI_IRQ_LEGACY:
3184 ath10k_pci_deinit_irq_legacy(ar);
3187 pci_disable_msi(ar_pci->pdev);
3194 int ath10k_pci_wait_for_target_init(struct ath10k *ar)
3196 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
3197 unsigned long timeout;
3200 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot waiting target to initialise\n");
3202 timeout = jiffies + msecs_to_jiffies(ATH10K_PCI_TARGET_WAIT);
3205 val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
3207 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target indicator %x\n",
3210 /* target should never return this */
3211 if (val == 0xffffffff)
3214 /* the device has crashed so don't bother trying anymore */
3215 if (val & FW_IND_EVENT_PENDING)
3218 if (val & FW_IND_INITIALIZED)
3221 if (ar_pci->oper_irq_mode == ATH10K_PCI_IRQ_LEGACY)
3222 /* Fix potential race by repeating CORE_BASE writes */
3223 ath10k_pci_enable_legacy_irq(ar);
3226 } while (time_before(jiffies, timeout));
3228 ath10k_pci_disable_and_clear_legacy_irq(ar);
3229 ath10k_pci_irq_msi_fw_mask(ar);
3231 if (val == 0xffffffff) {
3232 ath10k_err(ar, "failed to read device register, device is gone\n");
3236 if (val & FW_IND_EVENT_PENDING) {
3237 ath10k_warn(ar, "device has crashed during init\n");
3241 if (!(val & FW_IND_INITIALIZED)) {
3242 ath10k_err(ar, "failed to receive initialized event from target: %08x\n",
3247 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target initialised\n");
3251 static int ath10k_pci_cold_reset(struct ath10k *ar)
3255 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cold reset\n");
3257 spin_lock_bh(&ar->data_lock);
3259 ar->stats.fw_cold_reset_counter++;
3261 spin_unlock_bh(&ar->data_lock);
3263 /* Put Target, including PCIe, into RESET. */
3264 val = ath10k_pci_reg_read32(ar, SOC_GLOBAL_RESET_ADDRESS);