blob: 63f374ed6f6ab3cdd22cb429f9a700a3960cc456 [file] [log] [blame]
Kalle Valo5e3dd152013-06-12 20:52:10 +03001/*
2 * Copyright (c) 2005-2011 Atheros Communications Inc.
3 * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
4 *
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.
8 *
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.
16 */
17
18#include <linux/pci.h>
19#include <linux/module.h>
20#include <linux/interrupt.h>
21#include <linux/spinlock.h>
Kalle Valo650b91f2013-11-20 10:00:49 +020022#include <linux/bitops.h>
Kalle Valo5e3dd152013-06-12 20:52:10 +030023
24#include "core.h"
25#include "debug.h"
26
27#include "targaddrs.h"
28#include "bmi.h"
29
30#include "hif.h"
31#include "htc.h"
32
33#include "ce.h"
34#include "pci.h"
35
Michal Kaziorcfe9c452013-11-25 14:06:27 +010036enum ath10k_pci_irq_mode {
37 ATH10K_PCI_IRQ_AUTO = 0,
38 ATH10K_PCI_IRQ_LEGACY = 1,
39 ATH10K_PCI_IRQ_MSI = 2,
40};
41
Kalle Valo35098462014-03-28 09:32:27 +020042enum ath10k_pci_reset_mode {
43 ATH10K_PCI_RESET_AUTO = 0,
44 ATH10K_PCI_RESET_WARM_ONLY = 1,
45};
46
Michal Kaziorcfe9c452013-11-25 14:06:27 +010047static unsigned int ath10k_pci_irq_mode = ATH10K_PCI_IRQ_AUTO;
Kalle Valo35098462014-03-28 09:32:27 +020048static unsigned int ath10k_pci_reset_mode = ATH10K_PCI_RESET_AUTO;
Michal Kaziorcfe9c452013-11-25 14:06:27 +010049
Michal Kaziorcfe9c452013-11-25 14:06:27 +010050module_param_named(irq_mode, ath10k_pci_irq_mode, uint, 0644);
51MODULE_PARM_DESC(irq_mode, "0: auto, 1: legacy, 2: msi (default: 0)");
52
Kalle Valo35098462014-03-28 09:32:27 +020053module_param_named(reset_mode, ath10k_pci_reset_mode, uint, 0644);
54MODULE_PARM_DESC(reset_mode, "0: auto, 1: warm only (default: 0)");
55
Kalle Valo0399eca2014-03-28 09:32:21 +020056/* how long wait to wait for target to initialise, in ms */
57#define ATH10K_PCI_TARGET_WAIT 3000
Michal Kazior61c95ce2014-05-14 16:56:16 +030058#define ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS 3
Kalle Valo0399eca2014-03-28 09:32:21 +020059
Kalle Valo5e3dd152013-06-12 20:52:10 +030060#define QCA988X_2_0_DEVICE_ID (0x003c)
61
Benoit Taine9baa3c32014-08-08 15:56:03 +020062static const struct pci_device_id ath10k_pci_id_table[] = {
Kalle Valo5e3dd152013-06-12 20:52:10 +030063 { PCI_VDEVICE(ATHEROS, QCA988X_2_0_DEVICE_ID) }, /* PCI-E QCA988X V2 */
64 {0}
65};
66
Michal Kazior728f95e2014-08-22 14:33:14 +020067static void ath10k_pci_buffer_cleanup(struct ath10k *ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +010068static int ath10k_pci_cold_reset(struct ath10k *ar);
69static int ath10k_pci_warm_reset(struct ath10k *ar);
Michal Kaziord7fb47f2013-11-08 08:01:26 +010070static int ath10k_pci_wait_for_target_init(struct ath10k *ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +010071static int ath10k_pci_init_irq(struct ath10k *ar);
72static int ath10k_pci_deinit_irq(struct ath10k *ar);
73static int ath10k_pci_request_irq(struct ath10k *ar);
74static void ath10k_pci_free_irq(struct ath10k *ar);
Michal Kazior85622cd2013-11-25 14:06:22 +010075static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
76 struct ath10k_ce_pipe *rx_pipe,
77 struct bmi_xfer *xfer);
Kalle Valo5e3dd152013-06-12 20:52:10 +030078
79static const struct ce_attr host_ce_config_wlan[] = {
Kalle Valo48e9c222013-09-01 10:01:32 +030080 /* CE0: host->target HTC control and raw streams */
81 {
82 .flags = CE_ATTR_FLAGS,
83 .src_nentries = 16,
84 .src_sz_max = 256,
85 .dest_nentries = 0,
86 },
87
88 /* CE1: target->host HTT + HTC control */
89 {
90 .flags = CE_ATTR_FLAGS,
91 .src_nentries = 0,
92 .src_sz_max = 512,
93 .dest_nentries = 512,
94 },
95
96 /* CE2: target->host WMI */
97 {
98 .flags = CE_ATTR_FLAGS,
99 .src_nentries = 0,
100 .src_sz_max = 2048,
101 .dest_nentries = 32,
102 },
103
104 /* CE3: host->target WMI */
105 {
106 .flags = CE_ATTR_FLAGS,
107 .src_nentries = 32,
108 .src_sz_max = 2048,
109 .dest_nentries = 0,
110 },
111
112 /* CE4: host->target HTT */
113 {
114 .flags = CE_ATTR_FLAGS | CE_ATTR_DIS_INTR,
115 .src_nentries = CE_HTT_H2T_MSG_SRC_NENTRIES,
116 .src_sz_max = 256,
117 .dest_nentries = 0,
118 },
119
120 /* CE5: unused */
121 {
122 .flags = CE_ATTR_FLAGS,
123 .src_nentries = 0,
124 .src_sz_max = 0,
125 .dest_nentries = 0,
126 },
127
128 /* CE6: target autonomous hif_memcpy */
129 {
130 .flags = CE_ATTR_FLAGS,
131 .src_nentries = 0,
132 .src_sz_max = 0,
133 .dest_nentries = 0,
134 },
135
136 /* CE7: ce_diag, the Diagnostic Window */
137 {
138 .flags = CE_ATTR_FLAGS,
139 .src_nentries = 2,
140 .src_sz_max = DIAG_TRANSFER_LIMIT,
141 .dest_nentries = 2,
142 },
Kalle Valo5e3dd152013-06-12 20:52:10 +0300143};
144
145/* Target firmware's Copy Engine configuration. */
146static const struct ce_pipe_config target_ce_config_wlan[] = {
Kalle Valod88effb2013-09-01 10:01:39 +0300147 /* CE0: host->target HTC control and raw streams */
148 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300149 .pipenum = __cpu_to_le32(0),
150 .pipedir = __cpu_to_le32(PIPEDIR_OUT),
151 .nentries = __cpu_to_le32(32),
152 .nbytes_max = __cpu_to_le32(256),
153 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
154 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300155 },
156
157 /* CE1: target->host HTT + HTC control */
158 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300159 .pipenum = __cpu_to_le32(1),
160 .pipedir = __cpu_to_le32(PIPEDIR_IN),
161 .nentries = __cpu_to_le32(32),
162 .nbytes_max = __cpu_to_le32(512),
163 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
164 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300165 },
166
167 /* CE2: target->host WMI */
168 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300169 .pipenum = __cpu_to_le32(2),
170 .pipedir = __cpu_to_le32(PIPEDIR_IN),
171 .nentries = __cpu_to_le32(32),
172 .nbytes_max = __cpu_to_le32(2048),
173 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
174 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300175 },
176
177 /* CE3: host->target WMI */
178 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300179 .pipenum = __cpu_to_le32(3),
180 .pipedir = __cpu_to_le32(PIPEDIR_OUT),
181 .nentries = __cpu_to_le32(32),
182 .nbytes_max = __cpu_to_le32(2048),
183 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
184 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300185 },
186
187 /* CE4: host->target HTT */
188 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300189 .pipenum = __cpu_to_le32(4),
190 .pipedir = __cpu_to_le32(PIPEDIR_OUT),
191 .nentries = __cpu_to_le32(256),
192 .nbytes_max = __cpu_to_le32(256),
193 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
194 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300195 },
196
Kalle Valo5e3dd152013-06-12 20:52:10 +0300197 /* NB: 50% of src nentries, since tx has 2 frags */
Kalle Valod88effb2013-09-01 10:01:39 +0300198
199 /* CE5: unused */
200 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300201 .pipenum = __cpu_to_le32(5),
202 .pipedir = __cpu_to_le32(PIPEDIR_OUT),
203 .nentries = __cpu_to_le32(32),
204 .nbytes_max = __cpu_to_le32(2048),
205 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
206 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300207 },
208
209 /* CE6: Reserved for target autonomous hif_memcpy */
210 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300211 .pipenum = __cpu_to_le32(6),
212 .pipedir = __cpu_to_le32(PIPEDIR_INOUT),
213 .nentries = __cpu_to_le32(32),
214 .nbytes_max = __cpu_to_le32(4096),
215 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
216 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300217 },
218
Kalle Valo5e3dd152013-06-12 20:52:10 +0300219 /* CE7 used only by Host */
220};
221
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300222/*
223 * Map from service/endpoint to Copy Engine.
224 * This table is derived from the CE_PCI TABLE, above.
225 * It is passed to the Target at startup for use by firmware.
226 */
227static const struct service_to_pipe target_service_to_ce_map_wlan[] = {
228 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300229 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VO),
230 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
231 __cpu_to_le32(3),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300232 },
233 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300234 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VO),
235 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
236 __cpu_to_le32(2),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300237 },
238 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300239 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BK),
240 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
241 __cpu_to_le32(3),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300242 },
243 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300244 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BK),
245 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
246 __cpu_to_le32(2),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300247 },
248 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300249 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BE),
250 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
251 __cpu_to_le32(3),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300252 },
253 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300254 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BE),
255 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
256 __cpu_to_le32(2),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300257 },
258 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300259 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VI),
260 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
261 __cpu_to_le32(3),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300262 },
263 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300264 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VI),
265 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
266 __cpu_to_le32(2),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300267 },
268 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300269 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_CONTROL),
270 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
271 __cpu_to_le32(3),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300272 },
273 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300274 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_CONTROL),
275 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
276 __cpu_to_le32(2),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300277 },
278 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300279 __cpu_to_le32(ATH10K_HTC_SVC_ID_RSVD_CTRL),
280 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
281 __cpu_to_le32(0),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300282 },
283 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300284 __cpu_to_le32(ATH10K_HTC_SVC_ID_RSVD_CTRL),
285 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
286 __cpu_to_le32(1),
287 },
288 { /* not used */
289 __cpu_to_le32(ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS),
290 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
291 __cpu_to_le32(0),
292 },
293 { /* not used */
294 __cpu_to_le32(ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS),
295 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
296 __cpu_to_le32(1),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300297 },
298 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300299 __cpu_to_le32(ATH10K_HTC_SVC_ID_HTT_DATA_MSG),
300 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
301 __cpu_to_le32(4),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300302 },
303 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300304 __cpu_to_le32(ATH10K_HTC_SVC_ID_HTT_DATA_MSG),
305 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
306 __cpu_to_le32(1),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300307 },
308
309 /* (Additions here) */
310
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300311 { /* must be last */
312 __cpu_to_le32(0),
313 __cpu_to_le32(0),
314 __cpu_to_le32(0),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300315 },
316};
317
Michal Kaziore5398872013-11-25 14:06:20 +0100318static bool ath10k_pci_irq_pending(struct ath10k *ar)
319{
320 u32 cause;
321
322 /* Check if the shared legacy irq is for us */
323 cause = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
324 PCIE_INTR_CAUSE_ADDRESS);
325 if (cause & (PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL))
326 return true;
327
328 return false;
329}
330
Michal Kazior26852182013-11-25 14:06:25 +0100331static void ath10k_pci_disable_and_clear_legacy_irq(struct ath10k *ar)
332{
333 /* IMPORTANT: INTR_CLR register has to be set after
334 * INTR_ENABLE is set to 0, otherwise interrupt can not be
335 * really cleared. */
336 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
337 0);
338 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_CLR_ADDRESS,
339 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
340
341 /* IMPORTANT: this extra read transaction is required to
342 * flush the posted write buffer. */
Kalle Valocfbc06a2014-09-14 12:50:23 +0300343 (void)ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
344 PCIE_INTR_ENABLE_ADDRESS);
Michal Kazior26852182013-11-25 14:06:25 +0100345}
346
347static void ath10k_pci_enable_legacy_irq(struct ath10k *ar)
348{
349 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
350 PCIE_INTR_ENABLE_ADDRESS,
351 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
352
353 /* IMPORTANT: this extra read transaction is required to
354 * flush the posted write buffer. */
Kalle Valocfbc06a2014-09-14 12:50:23 +0300355 (void)ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
356 PCIE_INTR_ENABLE_ADDRESS);
Michal Kazior26852182013-11-25 14:06:25 +0100357}
358
Michal Kazior403d6272014-08-22 14:23:31 +0200359static inline const char *ath10k_pci_get_irq_method(struct ath10k *ar)
Michal Kaziorab977bd2013-11-25 14:06:26 +0100360{
Michal Kaziorab977bd2013-11-25 14:06:26 +0100361 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
362
Michal Kazior403d6272014-08-22 14:23:31 +0200363 if (ar_pci->num_msi_intrs > 1)
364 return "msi-x";
Kalle Valod8bb26b2014-09-14 12:50:33 +0300365
366 if (ar_pci->num_msi_intrs == 1)
Michal Kazior403d6272014-08-22 14:23:31 +0200367 return "msi";
Kalle Valod8bb26b2014-09-14 12:50:33 +0300368
369 return "legacy";
Michal Kaziorab977bd2013-11-25 14:06:26 +0100370}
371
Michal Kazior728f95e2014-08-22 14:33:14 +0200372static int __ath10k_pci_rx_post_buf(struct ath10k_pci_pipe *pipe)
Michal Kaziorab977bd2013-11-25 14:06:26 +0100373{
Michal Kazior728f95e2014-08-22 14:33:14 +0200374 struct ath10k *ar = pipe->hif_ce_state;
Michal Kaziorab977bd2013-11-25 14:06:26 +0100375 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior728f95e2014-08-22 14:33:14 +0200376 struct ath10k_ce_pipe *ce_pipe = pipe->ce_hdl;
377 struct sk_buff *skb;
378 dma_addr_t paddr;
Michal Kaziorab977bd2013-11-25 14:06:26 +0100379 int ret;
380
Michal Kazior728f95e2014-08-22 14:33:14 +0200381 lockdep_assert_held(&ar_pci->ce_lock);
382
383 skb = dev_alloc_skb(pipe->buf_sz);
384 if (!skb)
385 return -ENOMEM;
386
387 WARN_ONCE((unsigned long)skb->data & 3, "unaligned skb");
388
389 paddr = dma_map_single(ar->dev, skb->data,
390 skb->len + skb_tailroom(skb),
391 DMA_FROM_DEVICE);
392 if (unlikely(dma_mapping_error(ar->dev, paddr))) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200393 ath10k_warn(ar, "failed to dma map pci rx buf\n");
Michal Kazior728f95e2014-08-22 14:33:14 +0200394 dev_kfree_skb_any(skb);
395 return -EIO;
396 }
397
398 ATH10K_SKB_CB(skb)->paddr = paddr;
399
400 ret = __ath10k_ce_rx_post_buf(ce_pipe, skb, paddr);
Michal Kaziorab977bd2013-11-25 14:06:26 +0100401 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200402 ath10k_warn(ar, "failed to post pci rx buf: %d\n", ret);
Michal Kazior728f95e2014-08-22 14:33:14 +0200403 dma_unmap_single(ar->dev, paddr, skb->len + skb_tailroom(skb),
404 DMA_FROM_DEVICE);
405 dev_kfree_skb_any(skb);
Michal Kaziorab977bd2013-11-25 14:06:26 +0100406 return ret;
407 }
408
409 return 0;
410}
411
Michal Kazior728f95e2014-08-22 14:33:14 +0200412static void __ath10k_pci_rx_post_pipe(struct ath10k_pci_pipe *pipe)
Michal Kaziorab977bd2013-11-25 14:06:26 +0100413{
Michal Kazior728f95e2014-08-22 14:33:14 +0200414 struct ath10k *ar = pipe->hif_ce_state;
415 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
416 struct ath10k_ce_pipe *ce_pipe = pipe->ce_hdl;
417 int ret, num;
418
419 lockdep_assert_held(&ar_pci->ce_lock);
420
421 if (pipe->buf_sz == 0)
422 return;
423
424 if (!ce_pipe->dest_ring)
425 return;
426
427 num = __ath10k_ce_rx_num_free_bufs(ce_pipe);
428 while (num--) {
429 ret = __ath10k_pci_rx_post_buf(pipe);
430 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200431 ath10k_warn(ar, "failed to post pci rx buf: %d\n", ret);
Michal Kazior728f95e2014-08-22 14:33:14 +0200432 mod_timer(&ar_pci->rx_post_retry, jiffies +
433 ATH10K_PCI_RX_POST_RETRY_MS);
434 break;
435 }
436 }
437}
438
439static void ath10k_pci_rx_post_pipe(struct ath10k_pci_pipe *pipe)
440{
441 struct ath10k *ar = pipe->hif_ce_state;
442 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
443
444 spin_lock_bh(&ar_pci->ce_lock);
445 __ath10k_pci_rx_post_pipe(pipe);
446 spin_unlock_bh(&ar_pci->ce_lock);
447}
448
449static void ath10k_pci_rx_post(struct ath10k *ar)
450{
451 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
452 int i;
453
454 spin_lock_bh(&ar_pci->ce_lock);
455 for (i = 0; i < CE_COUNT; i++)
456 __ath10k_pci_rx_post_pipe(&ar_pci->pipe_info[i]);
457 spin_unlock_bh(&ar_pci->ce_lock);
458}
459
460static void ath10k_pci_rx_replenish_retry(unsigned long ptr)
461{
462 struct ath10k *ar = (void *)ptr;
463
464 ath10k_pci_rx_post(ar);
Michal Kaziorab977bd2013-11-25 14:06:26 +0100465}
466
Kalle Valo5e3dd152013-06-12 20:52:10 +0300467/*
468 * Diagnostic read/write access is provided for startup/config/debug usage.
469 * Caller must guarantee proper alignment, when applicable, and single user
470 * at any moment.
471 */
472static int ath10k_pci_diag_read_mem(struct ath10k *ar, u32 address, void *data,
473 int nbytes)
474{
475 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
476 int ret = 0;
477 u32 buf;
478 unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
479 unsigned int id;
480 unsigned int flags;
Michal Kazior2aa39112013-08-27 13:08:02 +0200481 struct ath10k_ce_pipe *ce_diag;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300482 /* Host buffer address in CE space */
483 u32 ce_data;
484 dma_addr_t ce_data_base = 0;
485 void *data_buf = NULL;
486 int i;
487
Kalle Valoeef25402014-09-24 14:16:52 +0300488 spin_lock_bh(&ar_pci->ce_lock);
489
Kalle Valo5e3dd152013-06-12 20:52:10 +0300490 ce_diag = ar_pci->ce_diag;
491
492 /*
493 * Allocate a temporary bounce buffer to hold caller's data
494 * to be DMA'ed from Target. This guarantees
495 * 1) 4-byte alignment
496 * 2) Buffer in DMA-able space
497 */
498 orig_nbytes = nbytes;
Michal Kazior68c03242014-03-28 10:02:35 +0200499 data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
500 orig_nbytes,
501 &ce_data_base,
502 GFP_ATOMIC);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300503
504 if (!data_buf) {
505 ret = -ENOMEM;
506 goto done;
507 }
508 memset(data_buf, 0, orig_nbytes);
509
510 remaining_bytes = orig_nbytes;
511 ce_data = ce_data_base;
512 while (remaining_bytes) {
513 nbytes = min_t(unsigned int, remaining_bytes,
514 DIAG_TRANSFER_LIMIT);
515
Kalle Valoeef25402014-09-24 14:16:52 +0300516 ret = __ath10k_ce_rx_post_buf(ce_diag, NULL, ce_data);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300517 if (ret != 0)
518 goto done;
519
520 /* Request CE to send from Target(!) address to Host buffer */
521 /*
522 * The address supplied by the caller is in the
523 * Target CPU virtual address space.
524 *
525 * In order to use this address with the diagnostic CE,
526 * convert it from Target CPU virtual address space
527 * to CE address space
528 */
Kalle Valo5e3dd152013-06-12 20:52:10 +0300529 address = TARG_CPU_SPACE_TO_CE_SPACE(ar, ar_pci->mem,
530 address);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300531
Kalle Valoeef25402014-09-24 14:16:52 +0300532 ret = ath10k_ce_send_nolock(ce_diag, NULL, (u32)address, nbytes, 0,
533 0);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300534 if (ret)
535 goto done;
536
537 i = 0;
Kalle Valoeef25402014-09-24 14:16:52 +0300538 while (ath10k_ce_completed_send_next_nolock(ce_diag, NULL, &buf,
539 &completed_nbytes,
540 &id) != 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300541 mdelay(1);
542 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
543 ret = -EBUSY;
544 goto done;
545 }
546 }
547
548 if (nbytes != completed_nbytes) {
549 ret = -EIO;
550 goto done;
551 }
552
Kalle Valocfbc06a2014-09-14 12:50:23 +0300553 if (buf != (u32)address) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300554 ret = -EIO;
555 goto done;
556 }
557
558 i = 0;
Kalle Valoeef25402014-09-24 14:16:52 +0300559 while (ath10k_ce_completed_recv_next_nolock(ce_diag, NULL, &buf,
560 &completed_nbytes,
561 &id, &flags) != 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300562 mdelay(1);
563
564 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
565 ret = -EBUSY;
566 goto done;
567 }
568 }
569
570 if (nbytes != completed_nbytes) {
571 ret = -EIO;
572 goto done;
573 }
574
575 if (buf != ce_data) {
576 ret = -EIO;
577 goto done;
578 }
579
580 remaining_bytes -= nbytes;
581 address += nbytes;
582 ce_data += nbytes;
583 }
584
585done:
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300586 if (ret == 0)
587 memcpy(data, data_buf, orig_nbytes);
588 else
Michal Kazior7aa7a722014-08-25 12:09:38 +0200589 ath10k_warn(ar, "failed to read diag value at 0x%x: %d\n",
Kalle Valo50f87a62014-03-28 09:32:52 +0200590 address, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300591
592 if (data_buf)
Michal Kazior68c03242014-03-28 10:02:35 +0200593 dma_free_coherent(ar->dev, orig_nbytes, data_buf,
594 ce_data_base);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300595
Kalle Valoeef25402014-09-24 14:16:52 +0300596 spin_unlock_bh(&ar_pci->ce_lock);
597
Kalle Valo5e3dd152013-06-12 20:52:10 +0300598 return ret;
599}
600
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300601static int ath10k_pci_diag_read32(struct ath10k *ar, u32 address, u32 *value)
602{
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300603 __le32 val = 0;
604 int ret;
605
606 ret = ath10k_pci_diag_read_mem(ar, address, &val, sizeof(val));
607 *value = __le32_to_cpu(val);
608
609 return ret;
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300610}
611
612static int __ath10k_pci_diag_read_hi(struct ath10k *ar, void *dest,
613 u32 src, u32 len)
614{
615 u32 host_addr, addr;
616 int ret;
617
618 host_addr = host_interest_item_address(src);
619
620 ret = ath10k_pci_diag_read32(ar, host_addr, &addr);
621 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200622 ath10k_warn(ar, "failed to get memcpy hi address for firmware address %d: %d\n",
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300623 src, ret);
624 return ret;
625 }
626
627 ret = ath10k_pci_diag_read_mem(ar, addr, dest, len);
628 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200629 ath10k_warn(ar, "failed to memcpy firmware memory from %d (%d B): %d\n",
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300630 addr, len, ret);
631 return ret;
632 }
633
634 return 0;
635}
636
637#define ath10k_pci_diag_read_hi(ar, dest, src, len) \
Kalle Valo8cc7f262014-09-14 12:50:39 +0300638 __ath10k_pci_diag_read_hi(ar, dest, HI_ITEM(src), len)
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300639
Kalle Valo5e3dd152013-06-12 20:52:10 +0300640static int ath10k_pci_diag_write_mem(struct ath10k *ar, u32 address,
641 const void *data, int nbytes)
642{
643 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
644 int ret = 0;
645 u32 buf;
646 unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
647 unsigned int id;
648 unsigned int flags;
Michal Kazior2aa39112013-08-27 13:08:02 +0200649 struct ath10k_ce_pipe *ce_diag;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300650 void *data_buf = NULL;
651 u32 ce_data; /* Host buffer address in CE space */
652 dma_addr_t ce_data_base = 0;
653 int i;
654
Kalle Valoeef25402014-09-24 14:16:52 +0300655 spin_lock_bh(&ar_pci->ce_lock);
656
Kalle Valo5e3dd152013-06-12 20:52:10 +0300657 ce_diag = ar_pci->ce_diag;
658
659 /*
660 * Allocate a temporary bounce buffer to hold caller's data
661 * to be DMA'ed to Target. This guarantees
662 * 1) 4-byte alignment
663 * 2) Buffer in DMA-able space
664 */
665 orig_nbytes = nbytes;
Michal Kazior68c03242014-03-28 10:02:35 +0200666 data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
667 orig_nbytes,
668 &ce_data_base,
669 GFP_ATOMIC);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300670 if (!data_buf) {
671 ret = -ENOMEM;
672 goto done;
673 }
674
675 /* Copy caller's data to allocated DMA buf */
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300676 memcpy(data_buf, data, orig_nbytes);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300677
678 /*
679 * The address supplied by the caller is in the
680 * Target CPU virtual address space.
681 *
682 * In order to use this address with the diagnostic CE,
683 * convert it from
684 * Target CPU virtual address space
685 * to
686 * CE address space
687 */
Kalle Valo5e3dd152013-06-12 20:52:10 +0300688 address = TARG_CPU_SPACE_TO_CE_SPACE(ar, ar_pci->mem, address);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300689
690 remaining_bytes = orig_nbytes;
691 ce_data = ce_data_base;
692 while (remaining_bytes) {
693 /* FIXME: check cast */
694 nbytes = min_t(int, remaining_bytes, DIAG_TRANSFER_LIMIT);
695
696 /* Set up to receive directly into Target(!) address */
Kalle Valoeef25402014-09-24 14:16:52 +0300697 ret = __ath10k_ce_rx_post_buf(ce_diag, NULL, address);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300698 if (ret != 0)
699 goto done;
700
701 /*
702 * Request CE to send caller-supplied data that
703 * was copied to bounce buffer to Target(!) address.
704 */
Kalle Valoeef25402014-09-24 14:16:52 +0300705 ret = ath10k_ce_send_nolock(ce_diag, NULL, (u32)ce_data,
706 nbytes, 0, 0);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300707 if (ret != 0)
708 goto done;
709
710 i = 0;
Kalle Valoeef25402014-09-24 14:16:52 +0300711 while (ath10k_ce_completed_send_next_nolock(ce_diag, NULL, &buf,
712 &completed_nbytes,
713 &id) != 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300714 mdelay(1);
715
716 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
717 ret = -EBUSY;
718 goto done;
719 }
720 }
721
722 if (nbytes != completed_nbytes) {
723 ret = -EIO;
724 goto done;
725 }
726
727 if (buf != ce_data) {
728 ret = -EIO;
729 goto done;
730 }
731
732 i = 0;
Kalle Valoeef25402014-09-24 14:16:52 +0300733 while (ath10k_ce_completed_recv_next_nolock(ce_diag, NULL, &buf,
734 &completed_nbytes,
735 &id, &flags) != 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300736 mdelay(1);
737
738 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
739 ret = -EBUSY;
740 goto done;
741 }
742 }
743
744 if (nbytes != completed_nbytes) {
745 ret = -EIO;
746 goto done;
747 }
748
749 if (buf != address) {
750 ret = -EIO;
751 goto done;
752 }
753
754 remaining_bytes -= nbytes;
755 address += nbytes;
756 ce_data += nbytes;
757 }
758
759done:
760 if (data_buf) {
Michal Kazior68c03242014-03-28 10:02:35 +0200761 dma_free_coherent(ar->dev, orig_nbytes, data_buf,
762 ce_data_base);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300763 }
764
765 if (ret != 0)
Michal Kazior7aa7a722014-08-25 12:09:38 +0200766 ath10k_warn(ar, "failed to write diag value at 0x%x: %d\n",
Kalle Valo50f87a62014-03-28 09:32:52 +0200767 address, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300768
Kalle Valoeef25402014-09-24 14:16:52 +0300769 spin_unlock_bh(&ar_pci->ce_lock);
770
Kalle Valo5e3dd152013-06-12 20:52:10 +0300771 return ret;
772}
773
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300774static int ath10k_pci_diag_write32(struct ath10k *ar, u32 address, u32 value)
775{
776 __le32 val = __cpu_to_le32(value);
777
778 return ath10k_pci_diag_write_mem(ar, address, &val, sizeof(val));
779}
780
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200781static bool ath10k_pci_is_awake(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300782{
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200783 u32 val = ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS);
784
785 return RTC_STATE_V_GET(val) == RTC_STATE_V_ON;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300786}
787
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200788static int ath10k_pci_wake_wait(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300789{
Kalle Valo5e3dd152013-06-12 20:52:10 +0300790 int tot_delay = 0;
791 int curr_delay = 5;
792
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200793 while (tot_delay < PCIE_WAKE_TIMEOUT) {
794 if (ath10k_pci_is_awake(ar))
Kalle Valo3aebe542013-09-01 10:02:07 +0300795 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300796
797 udelay(curr_delay);
798 tot_delay += curr_delay;
799
800 if (curr_delay < 50)
801 curr_delay += 5;
802 }
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200803
804 return -ETIMEDOUT;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300805}
806
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200807static int ath10k_pci_wake(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300808{
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200809 ath10k_pci_reg_write32(ar, PCIE_SOC_WAKE_ADDRESS,
810 PCIE_SOC_WAKE_V_MASK);
811 return ath10k_pci_wake_wait(ar);
812}
Kalle Valo5e3dd152013-06-12 20:52:10 +0300813
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200814static void ath10k_pci_sleep(struct ath10k *ar)
815{
816 ath10k_pci_reg_write32(ar, PCIE_SOC_WAKE_ADDRESS,
817 PCIE_SOC_WAKE_RESET);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300818}
819
Kalle Valo5e3dd152013-06-12 20:52:10 +0300820/* Called by lower (CE) layer when a send to Target completes. */
Michal Kazior5440ce22013-09-03 15:09:58 +0200821static void ath10k_pci_ce_send_done(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300822{
823 struct ath10k *ar = ce_state->ar;
824 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2f5280d2014-02-27 18:50:05 +0200825 struct ath10k_hif_cb *cb = &ar_pci->msg_callbacks_current;
Michal Kazior5440ce22013-09-03 15:09:58 +0200826 void *transfer_context;
827 u32 ce_data;
828 unsigned int nbytes;
829 unsigned int transfer_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300830
Michal Kazior5440ce22013-09-03 15:09:58 +0200831 while (ath10k_ce_completed_send_next(ce_state, &transfer_context,
832 &ce_data, &nbytes,
833 &transfer_id) == 0) {
Michal Kaziora16942e2014-02-27 18:50:04 +0200834 /* no need to call tx completion for NULL pointers */
Michal Kazior726346f2014-02-27 18:50:04 +0200835 if (transfer_context == NULL)
836 continue;
837
Michal Kazior2f5280d2014-02-27 18:50:05 +0200838 cb->tx_completion(ar, transfer_context, transfer_id);
Michal Kazior5440ce22013-09-03 15:09:58 +0200839 }
Kalle Valo5e3dd152013-06-12 20:52:10 +0300840}
841
842/* Called by lower (CE) layer when data is received from the Target. */
Michal Kazior5440ce22013-09-03 15:09:58 +0200843static void ath10k_pci_ce_recv_data(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300844{
845 struct ath10k *ar = ce_state->ar;
846 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +0200847 struct ath10k_pci_pipe *pipe_info = &ar_pci->pipe_info[ce_state->id];
Michal Kazior2f5280d2014-02-27 18:50:05 +0200848 struct ath10k_hif_cb *cb = &ar_pci->msg_callbacks_current;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300849 struct sk_buff *skb;
Michal Kazior5440ce22013-09-03 15:09:58 +0200850 void *transfer_context;
851 u32 ce_data;
Michal Kazior2f5280d2014-02-27 18:50:05 +0200852 unsigned int nbytes, max_nbytes;
Michal Kazior5440ce22013-09-03 15:09:58 +0200853 unsigned int transfer_id;
854 unsigned int flags;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300855
Michal Kazior5440ce22013-09-03 15:09:58 +0200856 while (ath10k_ce_completed_recv_next(ce_state, &transfer_context,
857 &ce_data, &nbytes, &transfer_id,
858 &flags) == 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300859 skb = transfer_context;
Michal Kazior2f5280d2014-02-27 18:50:05 +0200860 max_nbytes = skb->len + skb_tailroom(skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300861 dma_unmap_single(ar->dev, ATH10K_SKB_CB(skb)->paddr,
Michal Kazior2f5280d2014-02-27 18:50:05 +0200862 max_nbytes, DMA_FROM_DEVICE);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300863
Michal Kazior2f5280d2014-02-27 18:50:05 +0200864 if (unlikely(max_nbytes < nbytes)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200865 ath10k_warn(ar, "rxed more than expected (nbytes %d, max %d)",
Michal Kazior2f5280d2014-02-27 18:50:05 +0200866 nbytes, max_nbytes);
867 dev_kfree_skb_any(skb);
868 continue;
869 }
870
871 skb_put(skb, nbytes);
Michal Kaziora360e542014-09-23 10:22:54 +0200872
873 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci rx ce pipe %d len %d\n",
874 ce_state->id, skb->len);
875 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci rx: ",
876 skb->data, skb->len);
877
Michal Kazior2f5280d2014-02-27 18:50:05 +0200878 cb->rx_completion(ar, skb, pipe_info->pipe_num);
879 }
Michal Kaziorc29a3802014-07-21 21:03:10 +0300880
Michal Kazior728f95e2014-08-22 14:33:14 +0200881 ath10k_pci_rx_post_pipe(pipe_info);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300882}
883
Michal Kazior726346f2014-02-27 18:50:04 +0200884static int ath10k_pci_hif_tx_sg(struct ath10k *ar, u8 pipe_id,
885 struct ath10k_hif_sg_item *items, int n_items)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300886{
Kalle Valo5e3dd152013-06-12 20:52:10 +0300887 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior726346f2014-02-27 18:50:04 +0200888 struct ath10k_pci_pipe *pci_pipe = &ar_pci->pipe_info[pipe_id];
889 struct ath10k_ce_pipe *ce_pipe = pci_pipe->ce_hdl;
890 struct ath10k_ce_ring *src_ring = ce_pipe->src_ring;
Michal Kazior7147a132014-05-26 12:02:58 +0200891 unsigned int nentries_mask;
892 unsigned int sw_index;
893 unsigned int write_index;
Michal Kazior08b8aa02014-05-26 12:02:59 +0200894 int err, i = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300895
Michal Kazior726346f2014-02-27 18:50:04 +0200896 spin_lock_bh(&ar_pci->ce_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300897
Michal Kazior7147a132014-05-26 12:02:58 +0200898 nentries_mask = src_ring->nentries_mask;
899 sw_index = src_ring->sw_index;
900 write_index = src_ring->write_index;
901
Michal Kazior726346f2014-02-27 18:50:04 +0200902 if (unlikely(CE_RING_DELTA(nentries_mask,
903 write_index, sw_index - 1) < n_items)) {
904 err = -ENOBUFS;
Michal Kazior08b8aa02014-05-26 12:02:59 +0200905 goto err;
Michal Kazior726346f2014-02-27 18:50:04 +0200906 }
907
908 for (i = 0; i < n_items - 1; i++) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200909 ath10k_dbg(ar, ATH10K_DBG_PCI,
Michal Kazior726346f2014-02-27 18:50:04 +0200910 "pci tx item %d paddr 0x%08x len %d n_items %d\n",
911 i, items[i].paddr, items[i].len, n_items);
Michal Kazior7aa7a722014-08-25 12:09:38 +0200912 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci tx data: ",
Michal Kazior726346f2014-02-27 18:50:04 +0200913 items[i].vaddr, items[i].len);
914
915 err = ath10k_ce_send_nolock(ce_pipe,
916 items[i].transfer_context,
917 items[i].paddr,
918 items[i].len,
919 items[i].transfer_id,
920 CE_SEND_FLAG_GATHER);
921 if (err)
Michal Kazior08b8aa02014-05-26 12:02:59 +0200922 goto err;
Michal Kazior726346f2014-02-27 18:50:04 +0200923 }
924
925 /* `i` is equal to `n_items -1` after for() */
Kalle Valo5e3dd152013-06-12 20:52:10 +0300926
Michal Kazior7aa7a722014-08-25 12:09:38 +0200927 ath10k_dbg(ar, ATH10K_DBG_PCI,
Michal Kazior726346f2014-02-27 18:50:04 +0200928 "pci tx item %d paddr 0x%08x len %d n_items %d\n",
929 i, items[i].paddr, items[i].len, n_items);
Michal Kazior7aa7a722014-08-25 12:09:38 +0200930 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci tx data: ",
Michal Kazior726346f2014-02-27 18:50:04 +0200931 items[i].vaddr, items[i].len);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300932
Michal Kazior726346f2014-02-27 18:50:04 +0200933 err = ath10k_ce_send_nolock(ce_pipe,
934 items[i].transfer_context,
935 items[i].paddr,
936 items[i].len,
937 items[i].transfer_id,
938 0);
939 if (err)
Michal Kazior08b8aa02014-05-26 12:02:59 +0200940 goto err;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300941
Michal Kazior08b8aa02014-05-26 12:02:59 +0200942 spin_unlock_bh(&ar_pci->ce_lock);
943 return 0;
944
945err:
946 for (; i > 0; i--)
947 __ath10k_ce_send_revert(ce_pipe);
948
Michal Kazior726346f2014-02-27 18:50:04 +0200949 spin_unlock_bh(&ar_pci->ce_lock);
950 return err;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300951}
952
Kalle Valoeef25402014-09-24 14:16:52 +0300953static int ath10k_pci_hif_diag_read(struct ath10k *ar, u32 address, void *buf,
954 size_t buf_len)
955{
956 return ath10k_pci_diag_read_mem(ar, address, buf, buf_len);
957}
958
Kalle Valo5e3dd152013-06-12 20:52:10 +0300959static u16 ath10k_pci_hif_get_free_queue_number(struct ath10k *ar, u8 pipe)
960{
961 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo50f87a62014-03-28 09:32:52 +0200962
Michal Kazior7aa7a722014-08-25 12:09:38 +0200963 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif get free queue number\n");
Kalle Valo50f87a62014-03-28 09:32:52 +0200964
Michal Kazior3efcb3b2013-10-02 11:03:41 +0200965 return ath10k_ce_num_free_src_entries(ar_pci->pipe_info[pipe].ce_hdl);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300966}
967
Ben Greear384914b2014-08-25 08:37:32 +0300968static void ath10k_pci_dump_registers(struct ath10k *ar,
969 struct ath10k_fw_crash_data *crash_data)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300970{
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300971 __le32 reg_dump_values[REG_DUMP_COUNT_QCA988X] = {};
972 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300973
Ben Greear384914b2014-08-25 08:37:32 +0300974 lockdep_assert_held(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300975
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300976 ret = ath10k_pci_diag_read_hi(ar, &reg_dump_values[0],
977 hi_failure_state,
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300978 REG_DUMP_COUNT_QCA988X * sizeof(__le32));
Michal Kazior1d2b48d2013-11-08 08:01:34 +0100979 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200980 ath10k_err(ar, "failed to read firmware dump area: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300981 return;
982 }
983
984 BUILD_BUG_ON(REG_DUMP_COUNT_QCA988X % 4);
985
Michal Kazior7aa7a722014-08-25 12:09:38 +0200986 ath10k_err(ar, "firmware register dump:\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +0300987 for (i = 0; i < REG_DUMP_COUNT_QCA988X; i += 4)
Michal Kazior7aa7a722014-08-25 12:09:38 +0200988 ath10k_err(ar, "[%02d]: 0x%08X 0x%08X 0x%08X 0x%08X\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +0300989 i,
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300990 __le32_to_cpu(reg_dump_values[i]),
991 __le32_to_cpu(reg_dump_values[i + 1]),
992 __le32_to_cpu(reg_dump_values[i + 2]),
993 __le32_to_cpu(reg_dump_values[i + 3]));
Michal Kazioraffd3212013-07-16 09:54:35 +0200994
Michal Kazior1bbb1192014-08-25 12:13:14 +0200995 if (!crash_data)
996 return;
997
Ben Greear384914b2014-08-25 08:37:32 +0300998 for (i = 0; i < REG_DUMP_COUNT_QCA988X; i++)
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300999 crash_data->registers[i] = reg_dump_values[i];
Ben Greear384914b2014-08-25 08:37:32 +03001000}
1001
Kalle Valo0e9848c2014-08-25 08:37:37 +03001002static void ath10k_pci_fw_crashed_dump(struct ath10k *ar)
Ben Greear384914b2014-08-25 08:37:32 +03001003{
1004 struct ath10k_fw_crash_data *crash_data;
1005 char uuid[50];
1006
1007 spin_lock_bh(&ar->data_lock);
1008
Ben Greearf51dbe72014-09-29 14:41:46 +03001009 ar->stats.fw_crash_counter++;
1010
Ben Greear384914b2014-08-25 08:37:32 +03001011 crash_data = ath10k_debug_get_new_fw_crash_data(ar);
1012
1013 if (crash_data)
1014 scnprintf(uuid, sizeof(uuid), "%pUl", &crash_data->uuid);
1015 else
1016 scnprintf(uuid, sizeof(uuid), "n/a");
1017
Michal Kazior7aa7a722014-08-25 12:09:38 +02001018 ath10k_err(ar, "firmware crashed! (uuid %s)\n", uuid);
Kalle Valo8a0c7972014-08-25 08:37:45 +03001019 ath10k_print_driver_info(ar);
Ben Greear384914b2014-08-25 08:37:32 +03001020 ath10k_pci_dump_registers(ar, crash_data);
1021
Ben Greear384914b2014-08-25 08:37:32 +03001022 spin_unlock_bh(&ar->data_lock);
Michal Kazioraffd3212013-07-16 09:54:35 +02001023
Michal Kazior5e90de82013-10-16 16:46:05 +03001024 queue_work(ar->workqueue, &ar->restart_work);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001025}
1026
1027static void ath10k_pci_hif_send_complete_check(struct ath10k *ar, u8 pipe,
1028 int force)
1029{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001030 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif send complete check\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001031
Kalle Valo5e3dd152013-06-12 20:52:10 +03001032 if (!force) {
1033 int resources;
1034 /*
1035 * Decide whether to actually poll for completions, or just
1036 * wait for a later chance.
1037 * If there seem to be plenty of resources left, then just wait
1038 * since checking involves reading a CE register, which is a
1039 * relatively expensive operation.
1040 */
1041 resources = ath10k_pci_hif_get_free_queue_number(ar, pipe);
1042
1043 /*
1044 * If at least 50% of the total resources are still available,
1045 * don't bother checking again yet.
1046 */
1047 if (resources > (host_ce_config_wlan[pipe].src_nentries >> 1))
1048 return;
1049 }
1050 ath10k_ce_per_engine_service(ar, pipe);
1051}
1052
Michal Kaziore799bbf2013-07-05 16:15:12 +03001053static void ath10k_pci_hif_set_callbacks(struct ath10k *ar,
1054 struct ath10k_hif_cb *callbacks)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001055{
1056 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1057
Michal Kazior7aa7a722014-08-25 12:09:38 +02001058 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif set callbacks\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001059
1060 memcpy(&ar_pci->msg_callbacks_current, callbacks,
1061 sizeof(ar_pci->msg_callbacks_current));
1062}
1063
Michal Kazior96a9d0d2013-11-08 08:01:25 +01001064static void ath10k_pci_kill_tasklet(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001065{
1066 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001067 int i;
1068
Kalle Valo5e3dd152013-06-12 20:52:10 +03001069 tasklet_kill(&ar_pci->intr_tq);
Michal Kazior103d4f52013-11-08 08:01:24 +01001070 tasklet_kill(&ar_pci->msi_fw_err);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001071
1072 for (i = 0; i < CE_COUNT; i++)
1073 tasklet_kill(&ar_pci->pipe_info[i].intr);
Michal Kazior728f95e2014-08-22 14:33:14 +02001074
1075 del_timer_sync(&ar_pci->rx_post_retry);
Michal Kazior96a9d0d2013-11-08 08:01:25 +01001076}
1077
Kalle Valo5e3dd152013-06-12 20:52:10 +03001078static int ath10k_pci_hif_map_service_to_pipe(struct ath10k *ar,
1079 u16 service_id, u8 *ul_pipe,
1080 u8 *dl_pipe, int *ul_is_polled,
1081 int *dl_is_polled)
1082{
Michal Kazior7c6aa252014-08-26 19:14:03 +03001083 const struct service_to_pipe *entry;
1084 bool ul_set = false, dl_set = false;
1085 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001086
Michal Kazior7aa7a722014-08-25 12:09:38 +02001087 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif map service\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001088
Kalle Valo5e3dd152013-06-12 20:52:10 +03001089 /* polling for received messages not supported */
1090 *dl_is_polled = 0;
1091
Michal Kazior7c6aa252014-08-26 19:14:03 +03001092 for (i = 0; i < ARRAY_SIZE(target_service_to_ce_map_wlan); i++) {
1093 entry = &target_service_to_ce_map_wlan[i];
Kalle Valo5e3dd152013-06-12 20:52:10 +03001094
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001095 if (__le32_to_cpu(entry->service_id) != service_id)
Michal Kazior7c6aa252014-08-26 19:14:03 +03001096 continue;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001097
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001098 switch (__le32_to_cpu(entry->pipedir)) {
Michal Kazior7c6aa252014-08-26 19:14:03 +03001099 case PIPEDIR_NONE:
1100 break;
1101 case PIPEDIR_IN:
1102 WARN_ON(dl_set);
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001103 *dl_pipe = __le32_to_cpu(entry->pipenum);
Michal Kazior7c6aa252014-08-26 19:14:03 +03001104 dl_set = true;
1105 break;
1106 case PIPEDIR_OUT:
1107 WARN_ON(ul_set);
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001108 *ul_pipe = __le32_to_cpu(entry->pipenum);
Michal Kazior7c6aa252014-08-26 19:14:03 +03001109 ul_set = true;
1110 break;
1111 case PIPEDIR_INOUT:
1112 WARN_ON(dl_set);
1113 WARN_ON(ul_set);
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001114 *dl_pipe = __le32_to_cpu(entry->pipenum);
1115 *ul_pipe = __le32_to_cpu(entry->pipenum);
Michal Kazior7c6aa252014-08-26 19:14:03 +03001116 dl_set = true;
1117 ul_set = true;
1118 break;
1119 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001120 }
Michal Kazior7c6aa252014-08-26 19:14:03 +03001121
1122 if (WARN_ON(!ul_set || !dl_set))
1123 return -ENOENT;
1124
Kalle Valo5e3dd152013-06-12 20:52:10 +03001125 *ul_is_polled =
1126 (host_ce_config_wlan[*ul_pipe].flags & CE_ATTR_DIS_INTR) != 0;
1127
Michal Kazior7c6aa252014-08-26 19:14:03 +03001128 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001129}
1130
1131static void ath10k_pci_hif_get_default_pipe(struct ath10k *ar,
Kalle Valo5b07e072014-09-14 12:50:06 +03001132 u8 *ul_pipe, u8 *dl_pipe)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001133{
1134 int ul_is_polled, dl_is_polled;
1135
Michal Kazior7aa7a722014-08-25 12:09:38 +02001136 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif get default pipe\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001137
Kalle Valo5e3dd152013-06-12 20:52:10 +03001138 (void)ath10k_pci_hif_map_service_to_pipe(ar,
1139 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1140 ul_pipe,
1141 dl_pipe,
1142 &ul_is_polled,
1143 &dl_is_polled);
1144}
1145
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001146static void ath10k_pci_irq_msi_fw_mask(struct ath10k *ar)
1147{
1148 u32 val;
1149
1150 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS + CORE_CTRL_ADDRESS);
1151 val &= ~CORE_CTRL_PCIE_REG_31_MASK;
1152
1153 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + CORE_CTRL_ADDRESS, val);
1154}
1155
1156static void ath10k_pci_irq_msi_fw_unmask(struct ath10k *ar)
1157{
1158 u32 val;
1159
1160 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS + CORE_CTRL_ADDRESS);
1161 val |= CORE_CTRL_PCIE_REG_31_MASK;
1162
1163 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + CORE_CTRL_ADDRESS, val);
1164}
1165
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001166static void ath10k_pci_irq_disable(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001167{
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001168 ath10k_ce_disable_interrupts(ar);
1169 ath10k_pci_disable_and_clear_legacy_irq(ar);
1170 ath10k_pci_irq_msi_fw_mask(ar);
1171}
1172
1173static void ath10k_pci_irq_sync(struct ath10k *ar)
1174{
Kalle Valo5e3dd152013-06-12 20:52:10 +03001175 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001176 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001177
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001178 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
1179 synchronize_irq(ar_pci->pdev->irq + i);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001180}
1181
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001182static void ath10k_pci_irq_enable(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001183{
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001184 ath10k_ce_enable_interrupts(ar);
Michal Kaziore75db4e2014-08-28 22:14:16 +03001185 ath10k_pci_enable_legacy_irq(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001186 ath10k_pci_irq_msi_fw_unmask(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001187}
1188
1189static int ath10k_pci_hif_start(struct ath10k *ar)
1190{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001191 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif start\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001192
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001193 ath10k_pci_irq_enable(ar);
Michal Kazior728f95e2014-08-22 14:33:14 +02001194 ath10k_pci_rx_post(ar);
Kalle Valo50f87a62014-03-28 09:32:52 +02001195
Kalle Valo5e3dd152013-06-12 20:52:10 +03001196 return 0;
1197}
1198
Michal Kazior099ac7c2014-10-28 10:32:05 +01001199static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe *pci_pipe)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001200{
1201 struct ath10k *ar;
Michal Kazior099ac7c2014-10-28 10:32:05 +01001202 struct ath10k_ce_pipe *ce_pipe;
1203 struct ath10k_ce_ring *ce_ring;
1204 struct sk_buff *skb;
1205 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001206
Michal Kazior099ac7c2014-10-28 10:32:05 +01001207 ar = pci_pipe->hif_ce_state;
1208 ce_pipe = pci_pipe->ce_hdl;
1209 ce_ring = ce_pipe->dest_ring;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001210
Michal Kazior099ac7c2014-10-28 10:32:05 +01001211 if (!ce_ring)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001212 return;
1213
Michal Kazior099ac7c2014-10-28 10:32:05 +01001214 if (!pci_pipe->buf_sz)
1215 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001216
Michal Kazior099ac7c2014-10-28 10:32:05 +01001217 for (i = 0; i < ce_ring->nentries; i++) {
1218 skb = ce_ring->per_transfer_context[i];
1219 if (!skb)
1220 continue;
1221
1222 ce_ring->per_transfer_context[i] = NULL;
1223
1224 dma_unmap_single(ar->dev, ATH10K_SKB_CB(skb)->paddr,
1225 skb->len + skb_tailroom(skb),
Kalle Valo5e3dd152013-06-12 20:52:10 +03001226 DMA_FROM_DEVICE);
Michal Kazior099ac7c2014-10-28 10:32:05 +01001227 dev_kfree_skb_any(skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001228 }
1229}
1230
Michal Kazior099ac7c2014-10-28 10:32:05 +01001231static void ath10k_pci_tx_pipe_cleanup(struct ath10k_pci_pipe *pci_pipe)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001232{
1233 struct ath10k *ar;
1234 struct ath10k_pci *ar_pci;
Michal Kazior099ac7c2014-10-28 10:32:05 +01001235 struct ath10k_ce_pipe *ce_pipe;
1236 struct ath10k_ce_ring *ce_ring;
1237 struct ce_desc *ce_desc;
1238 struct sk_buff *skb;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001239 unsigned int id;
Michal Kazior099ac7c2014-10-28 10:32:05 +01001240 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001241
Michal Kazior099ac7c2014-10-28 10:32:05 +01001242 ar = pci_pipe->hif_ce_state;
1243 ar_pci = ath10k_pci_priv(ar);
1244 ce_pipe = pci_pipe->ce_hdl;
1245 ce_ring = ce_pipe->src_ring;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001246
Michal Kazior099ac7c2014-10-28 10:32:05 +01001247 if (!ce_ring)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001248 return;
1249
Michal Kazior099ac7c2014-10-28 10:32:05 +01001250 if (!pci_pipe->buf_sz)
1251 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001252
Michal Kazior099ac7c2014-10-28 10:32:05 +01001253 ce_desc = ce_ring->shadow_base;
1254 if (WARN_ON(!ce_desc))
1255 return;
1256
1257 for (i = 0; i < ce_ring->nentries; i++) {
1258 skb = ce_ring->per_transfer_context[i];
1259 if (!skb)
Michal Kazior2415fc12013-11-08 08:01:32 +01001260 continue;
Michal Kazior2415fc12013-11-08 08:01:32 +01001261
Michal Kazior099ac7c2014-10-28 10:32:05 +01001262 ce_ring->per_transfer_context[i] = NULL;
1263 id = MS(__le16_to_cpu(ce_desc[i].flags),
1264 CE_DESC_FLAGS_META_DATA);
1265
1266 ar_pci->msg_callbacks_current.tx_completion(ar, skb, id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001267 }
1268}
1269
1270/*
1271 * Cleanup residual buffers for device shutdown:
1272 * buffers that were enqueued for receive
1273 * buffers that were to be sent
1274 * Note: Buffers that had completed but which were
1275 * not yet processed are on a completion queue. They
1276 * are handled when the completion thread shuts down.
1277 */
1278static void ath10k_pci_buffer_cleanup(struct ath10k *ar)
1279{
1280 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1281 int pipe_num;
1282
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001283 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Michal Kazior87263e52013-08-27 13:08:01 +02001284 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001285
1286 pipe_info = &ar_pci->pipe_info[pipe_num];
1287 ath10k_pci_rx_pipe_cleanup(pipe_info);
1288 ath10k_pci_tx_pipe_cleanup(pipe_info);
1289 }
1290}
1291
1292static void ath10k_pci_ce_deinit(struct ath10k *ar)
1293{
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001294 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001295
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001296 for (i = 0; i < CE_COUNT; i++)
1297 ath10k_ce_deinit_pipe(ar, i);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001298}
1299
Michal Kazior728f95e2014-08-22 14:33:14 +02001300static void ath10k_pci_flush(struct ath10k *ar)
1301{
1302 ath10k_pci_kill_tasklet(ar);
1303 ath10k_pci_buffer_cleanup(ar);
1304}
1305
Kalle Valo5e3dd152013-06-12 20:52:10 +03001306static void ath10k_pci_hif_stop(struct ath10k *ar)
1307{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001308 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif stop\n");
Michal Kazior32270b62013-08-02 09:15:47 +02001309
Michal Kazior10d23db2014-08-22 14:33:15 +02001310 /* Most likely the device has HTT Rx ring configured. The only way to
1311 * prevent the device from accessing (and possible corrupting) host
1312 * memory is to reset the chip now.
Michal Kaziore75db4e2014-08-28 22:14:16 +03001313 *
1314 * There's also no known way of masking MSI interrupts on the device.
1315 * For ranged MSI the CE-related interrupts can be masked. However
1316 * regardless how many MSI interrupts are assigned the first one
1317 * is always used for firmware indications (crashes) and cannot be
1318 * masked. To prevent the device from asserting the interrupt reset it
1319 * before proceeding with cleanup.
Michal Kazior10d23db2014-08-22 14:33:15 +02001320 */
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001321 ath10k_pci_warm_reset(ar);
Michal Kaziore75db4e2014-08-28 22:14:16 +03001322
1323 ath10k_pci_irq_disable(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001324 ath10k_pci_irq_sync(ar);
Michal Kaziore75db4e2014-08-28 22:14:16 +03001325 ath10k_pci_flush(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001326}
1327
1328static int ath10k_pci_hif_exchange_bmi_msg(struct ath10k *ar,
1329 void *req, u32 req_len,
1330 void *resp, u32 *resp_len)
1331{
1332 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2aa39112013-08-27 13:08:02 +02001333 struct ath10k_pci_pipe *pci_tx = &ar_pci->pipe_info[BMI_CE_NUM_TO_TARG];
1334 struct ath10k_pci_pipe *pci_rx = &ar_pci->pipe_info[BMI_CE_NUM_TO_HOST];
1335 struct ath10k_ce_pipe *ce_tx = pci_tx->ce_hdl;
1336 struct ath10k_ce_pipe *ce_rx = pci_rx->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001337 dma_addr_t req_paddr = 0;
1338 dma_addr_t resp_paddr = 0;
1339 struct bmi_xfer xfer = {};
1340 void *treq, *tresp = NULL;
1341 int ret = 0;
1342
Michal Kazior85622cd2013-11-25 14:06:22 +01001343 might_sleep();
1344
Kalle Valo5e3dd152013-06-12 20:52:10 +03001345 if (resp && !resp_len)
1346 return -EINVAL;
1347
1348 if (resp && resp_len && *resp_len == 0)
1349 return -EINVAL;
1350
1351 treq = kmemdup(req, req_len, GFP_KERNEL);
1352 if (!treq)
1353 return -ENOMEM;
1354
1355 req_paddr = dma_map_single(ar->dev, treq, req_len, DMA_TO_DEVICE);
1356 ret = dma_mapping_error(ar->dev, req_paddr);
1357 if (ret)
1358 goto err_dma;
1359
1360 if (resp && resp_len) {
1361 tresp = kzalloc(*resp_len, GFP_KERNEL);
1362 if (!tresp) {
1363 ret = -ENOMEM;
1364 goto err_req;
1365 }
1366
1367 resp_paddr = dma_map_single(ar->dev, tresp, *resp_len,
1368 DMA_FROM_DEVICE);
1369 ret = dma_mapping_error(ar->dev, resp_paddr);
1370 if (ret)
1371 goto err_req;
1372
1373 xfer.wait_for_resp = true;
1374 xfer.resp_len = 0;
1375
Michal Kazior728f95e2014-08-22 14:33:14 +02001376 ath10k_ce_rx_post_buf(ce_rx, &xfer, resp_paddr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001377 }
1378
Kalle Valo5e3dd152013-06-12 20:52:10 +03001379 ret = ath10k_ce_send(ce_tx, &xfer, req_paddr, req_len, -1, 0);
1380 if (ret)
1381 goto err_resp;
1382
Michal Kazior85622cd2013-11-25 14:06:22 +01001383 ret = ath10k_pci_bmi_wait(ce_tx, ce_rx, &xfer);
1384 if (ret) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001385 u32 unused_buffer;
1386 unsigned int unused_nbytes;
1387 unsigned int unused_id;
1388
Kalle Valo5e3dd152013-06-12 20:52:10 +03001389 ath10k_ce_cancel_send_next(ce_tx, NULL, &unused_buffer,
1390 &unused_nbytes, &unused_id);
1391 } else {
1392 /* non-zero means we did not time out */
1393 ret = 0;
1394 }
1395
1396err_resp:
1397 if (resp) {
1398 u32 unused_buffer;
1399
1400 ath10k_ce_revoke_recv_next(ce_rx, NULL, &unused_buffer);
1401 dma_unmap_single(ar->dev, resp_paddr,
1402 *resp_len, DMA_FROM_DEVICE);
1403 }
1404err_req:
1405 dma_unmap_single(ar->dev, req_paddr, req_len, DMA_TO_DEVICE);
1406
1407 if (ret == 0 && resp_len) {
1408 *resp_len = min(*resp_len, xfer.resp_len);
1409 memcpy(resp, tresp, xfer.resp_len);
1410 }
1411err_dma:
1412 kfree(treq);
1413 kfree(tresp);
1414
1415 return ret;
1416}
1417
Michal Kazior5440ce22013-09-03 15:09:58 +02001418static void ath10k_pci_bmi_send_done(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001419{
Michal Kazior5440ce22013-09-03 15:09:58 +02001420 struct bmi_xfer *xfer;
1421 u32 ce_data;
1422 unsigned int nbytes;
1423 unsigned int transfer_id;
1424
1425 if (ath10k_ce_completed_send_next(ce_state, (void **)&xfer, &ce_data,
1426 &nbytes, &transfer_id))
1427 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001428
Michal Kazior2374b182014-07-14 16:25:25 +03001429 xfer->tx_done = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001430}
1431
Michal Kazior5440ce22013-09-03 15:09:58 +02001432static void ath10k_pci_bmi_recv_data(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001433{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001434 struct ath10k *ar = ce_state->ar;
Michal Kazior5440ce22013-09-03 15:09:58 +02001435 struct bmi_xfer *xfer;
1436 u32 ce_data;
1437 unsigned int nbytes;
1438 unsigned int transfer_id;
1439 unsigned int flags;
1440
1441 if (ath10k_ce_completed_recv_next(ce_state, (void **)&xfer, &ce_data,
1442 &nbytes, &transfer_id, &flags))
1443 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001444
1445 if (!xfer->wait_for_resp) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001446 ath10k_warn(ar, "unexpected: BMI data received; ignoring\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001447 return;
1448 }
1449
1450 xfer->resp_len = nbytes;
Michal Kazior2374b182014-07-14 16:25:25 +03001451 xfer->rx_done = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001452}
1453
Michal Kazior85622cd2013-11-25 14:06:22 +01001454static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
1455 struct ath10k_ce_pipe *rx_pipe,
1456 struct bmi_xfer *xfer)
1457{
1458 unsigned long timeout = jiffies + BMI_COMMUNICATION_TIMEOUT_HZ;
1459
1460 while (time_before_eq(jiffies, timeout)) {
1461 ath10k_pci_bmi_send_done(tx_pipe);
1462 ath10k_pci_bmi_recv_data(rx_pipe);
1463
Michal Kazior2374b182014-07-14 16:25:25 +03001464 if (xfer->tx_done && (xfer->rx_done == xfer->wait_for_resp))
Michal Kazior85622cd2013-11-25 14:06:22 +01001465 return 0;
1466
1467 schedule();
1468 }
1469
1470 return -ETIMEDOUT;
1471}
1472
Kalle Valo5e3dd152013-06-12 20:52:10 +03001473/*
Kalle Valo5e3dd152013-06-12 20:52:10 +03001474 * Send an interrupt to the device to wake up the Target CPU
1475 * so it has an opportunity to notice any changed state.
1476 */
1477static int ath10k_pci_wake_target_cpu(struct ath10k *ar)
1478{
Michal Kazior9e264942014-09-02 11:00:21 +03001479 u32 addr, val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001480
Michal Kazior9e264942014-09-02 11:00:21 +03001481 addr = SOC_CORE_BASE_ADDRESS | CORE_CTRL_ADDRESS;
1482 val = ath10k_pci_read32(ar, addr);
1483 val |= CORE_CTRL_CPU_INTR_MASK;
1484 ath10k_pci_write32(ar, addr, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001485
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001486 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001487}
1488
1489static int ath10k_pci_init_config(struct ath10k *ar)
1490{
1491 u32 interconnect_targ_addr;
1492 u32 pcie_state_targ_addr = 0;
1493 u32 pipe_cfg_targ_addr = 0;
1494 u32 svc_to_pipe_map = 0;
1495 u32 pcie_config_flags = 0;
1496 u32 ealloc_value;
1497 u32 ealloc_targ_addr;
1498 u32 flag2_value;
1499 u32 flag2_targ_addr;
1500 int ret = 0;
1501
1502 /* Download to Target the CE Config and the service-to-CE map */
1503 interconnect_targ_addr =
1504 host_interest_item_address(HI_ITEM(hi_interconnect_state));
1505
1506 /* Supply Target-side CE configuration */
Michal Kazior9e264942014-09-02 11:00:21 +03001507 ret = ath10k_pci_diag_read32(ar, interconnect_targ_addr,
1508 &pcie_state_targ_addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001509 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001510 ath10k_err(ar, "Failed to get pcie state addr: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001511 return ret;
1512 }
1513
1514 if (pcie_state_targ_addr == 0) {
1515 ret = -EIO;
Michal Kazior7aa7a722014-08-25 12:09:38 +02001516 ath10k_err(ar, "Invalid pcie state addr\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001517 return ret;
1518 }
1519
Michal Kazior9e264942014-09-02 11:00:21 +03001520 ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
Kalle Valo5e3dd152013-06-12 20:52:10 +03001521 offsetof(struct pcie_state,
Michal Kazior9e264942014-09-02 11:00:21 +03001522 pipe_cfg_addr)),
1523 &pipe_cfg_targ_addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001524 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001525 ath10k_err(ar, "Failed to get pipe cfg addr: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001526 return ret;
1527 }
1528
1529 if (pipe_cfg_targ_addr == 0) {
1530 ret = -EIO;
Michal Kazior7aa7a722014-08-25 12:09:38 +02001531 ath10k_err(ar, "Invalid pipe cfg addr\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001532 return ret;
1533 }
1534
1535 ret = ath10k_pci_diag_write_mem(ar, pipe_cfg_targ_addr,
Kalle Valo5b07e072014-09-14 12:50:06 +03001536 target_ce_config_wlan,
1537 sizeof(target_ce_config_wlan));
Kalle Valo5e3dd152013-06-12 20:52:10 +03001538
1539 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001540 ath10k_err(ar, "Failed to write pipe cfg: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001541 return ret;
1542 }
1543
Michal Kazior9e264942014-09-02 11:00:21 +03001544 ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
Kalle Valo5e3dd152013-06-12 20:52:10 +03001545 offsetof(struct pcie_state,
Michal Kazior9e264942014-09-02 11:00:21 +03001546 svc_to_pipe_map)),
1547 &svc_to_pipe_map);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001548 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001549 ath10k_err(ar, "Failed to get svc/pipe map: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001550 return ret;
1551 }
1552
1553 if (svc_to_pipe_map == 0) {
1554 ret = -EIO;
Michal Kazior7aa7a722014-08-25 12:09:38 +02001555 ath10k_err(ar, "Invalid svc_to_pipe map\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001556 return ret;
1557 }
1558
1559 ret = ath10k_pci_diag_write_mem(ar, svc_to_pipe_map,
Kalle Valo5b07e072014-09-14 12:50:06 +03001560 target_service_to_ce_map_wlan,
1561 sizeof(target_service_to_ce_map_wlan));
Kalle Valo5e3dd152013-06-12 20:52:10 +03001562 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001563 ath10k_err(ar, "Failed to write svc/pipe map: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001564 return ret;
1565 }
1566
Michal Kazior9e264942014-09-02 11:00:21 +03001567 ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
Kalle Valo5e3dd152013-06-12 20:52:10 +03001568 offsetof(struct pcie_state,
Michal Kazior9e264942014-09-02 11:00:21 +03001569 config_flags)),
1570 &pcie_config_flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001571 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001572 ath10k_err(ar, "Failed to get pcie config_flags: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001573 return ret;
1574 }
1575
1576 pcie_config_flags &= ~PCIE_CONFIG_FLAG_ENABLE_L1;
1577
Michal Kazior9e264942014-09-02 11:00:21 +03001578 ret = ath10k_pci_diag_write32(ar, (pcie_state_targ_addr +
1579 offsetof(struct pcie_state,
1580 config_flags)),
1581 pcie_config_flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001582 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001583 ath10k_err(ar, "Failed to write pcie config_flags: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001584 return ret;
1585 }
1586
1587 /* configure early allocation */
1588 ealloc_targ_addr = host_interest_item_address(HI_ITEM(hi_early_alloc));
1589
Michal Kazior9e264942014-09-02 11:00:21 +03001590 ret = ath10k_pci_diag_read32(ar, ealloc_targ_addr, &ealloc_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001591 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001592 ath10k_err(ar, "Faile to get early alloc val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001593 return ret;
1594 }
1595
1596 /* first bank is switched to IRAM */
1597 ealloc_value |= ((HI_EARLY_ALLOC_MAGIC << HI_EARLY_ALLOC_MAGIC_SHIFT) &
1598 HI_EARLY_ALLOC_MAGIC_MASK);
1599 ealloc_value |= ((1 << HI_EARLY_ALLOC_IRAM_BANKS_SHIFT) &
1600 HI_EARLY_ALLOC_IRAM_BANKS_MASK);
1601
Michal Kazior9e264942014-09-02 11:00:21 +03001602 ret = ath10k_pci_diag_write32(ar, ealloc_targ_addr, ealloc_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001603 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001604 ath10k_err(ar, "Failed to set early alloc val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001605 return ret;
1606 }
1607
1608 /* Tell Target to proceed with initialization */
1609 flag2_targ_addr = host_interest_item_address(HI_ITEM(hi_option_flag2));
1610
Michal Kazior9e264942014-09-02 11:00:21 +03001611 ret = ath10k_pci_diag_read32(ar, flag2_targ_addr, &flag2_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001612 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001613 ath10k_err(ar, "Failed to get option val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001614 return ret;
1615 }
1616
1617 flag2_value |= HI_OPTION_EARLY_CFG_DONE;
1618
Michal Kazior9e264942014-09-02 11:00:21 +03001619 ret = ath10k_pci_diag_write32(ar, flag2_targ_addr, flag2_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001620 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001621 ath10k_err(ar, "Failed to set option val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001622 return ret;
1623 }
1624
1625 return 0;
1626}
1627
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001628static int ath10k_pci_alloc_pipes(struct ath10k *ar)
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001629{
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001630 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1631 struct ath10k_pci_pipe *pipe;
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001632 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001633
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001634 for (i = 0; i < CE_COUNT; i++) {
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001635 pipe = &ar_pci->pipe_info[i];
1636 pipe->ce_hdl = &ar_pci->ce_states[i];
1637 pipe->pipe_num = i;
1638 pipe->hif_ce_state = ar;
1639
1640 ret = ath10k_ce_alloc_pipe(ar, i, &host_ce_config_wlan[i],
1641 ath10k_pci_ce_send_done,
1642 ath10k_pci_ce_recv_data);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001643 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001644 ath10k_err(ar, "failed to allocate copy engine pipe %d: %d\n",
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001645 i, ret);
1646 return ret;
1647 }
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001648
1649 /* Last CE is Diagnostic Window */
1650 if (i == CE_COUNT - 1) {
1651 ar_pci->ce_diag = pipe->ce_hdl;
1652 continue;
1653 }
1654
1655 pipe->buf_sz = (size_t)(host_ce_config_wlan[i].src_sz_max);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001656 }
1657
1658 return 0;
1659}
1660
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001661static void ath10k_pci_free_pipes(struct ath10k *ar)
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001662{
1663 int i;
1664
1665 for (i = 0; i < CE_COUNT; i++)
1666 ath10k_ce_free_pipe(ar, i);
1667}
Kalle Valo5e3dd152013-06-12 20:52:10 +03001668
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001669static int ath10k_pci_init_pipes(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001670{
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001671 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001672
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001673 for (i = 0; i < CE_COUNT; i++) {
1674 ret = ath10k_ce_init_pipe(ar, i, &host_ce_config_wlan[i]);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001675 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001676 ath10k_err(ar, "failed to initialize copy engine pipe %d: %d\n",
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001677 i, ret);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001678 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001679 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001680 }
1681
Kalle Valo5e3dd152013-06-12 20:52:10 +03001682 return 0;
1683}
1684
Michal Kazior5c771e72014-08-22 14:23:34 +02001685static bool ath10k_pci_has_fw_crashed(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001686{
Michal Kazior5c771e72014-08-22 14:23:34 +02001687 return ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS) &
1688 FW_IND_EVENT_PENDING;
1689}
Kalle Valo5e3dd152013-06-12 20:52:10 +03001690
Michal Kazior5c771e72014-08-22 14:23:34 +02001691static void ath10k_pci_fw_crashed_clear(struct ath10k *ar)
1692{
1693 u32 val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001694
Michal Kazior5c771e72014-08-22 14:23:34 +02001695 val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
1696 val &= ~FW_IND_EVENT_PENDING;
1697 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001698}
1699
Michal Kaziorde013572014-05-14 16:56:16 +03001700/* this function effectively clears target memory controller assert line */
1701static void ath10k_pci_warm_reset_si0(struct ath10k *ar)
1702{
1703 u32 val;
1704
1705 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1706 ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
1707 val | SOC_RESET_CONTROL_SI0_RST_MASK);
1708 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1709
1710 msleep(10);
1711
1712 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1713 ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
1714 val & ~SOC_RESET_CONTROL_SI0_RST_MASK);
1715 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1716
1717 msleep(10);
1718}
1719
Michal Kazior61c16482014-10-28 10:32:06 +01001720static void ath10k_pci_warm_reset_cpu(struct ath10k *ar)
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001721{
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001722 u32 val;
1723
Kalle Valob39712c2014-03-28 09:32:46 +02001724 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, 0);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001725
Michal Kazior61c16482014-10-28 10:32:06 +01001726 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1727 SOC_RESET_CONTROL_ADDRESS);
1728 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1729 val | SOC_RESET_CONTROL_CPU_WARM_RST_MASK);
1730}
1731
1732static void ath10k_pci_warm_reset_ce(struct ath10k *ar)
1733{
1734 u32 val;
1735
1736 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1737 SOC_RESET_CONTROL_ADDRESS);
1738
1739 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1740 val | SOC_RESET_CONTROL_CE_RST_MASK);
1741 msleep(10);
1742 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1743 val & ~SOC_RESET_CONTROL_CE_RST_MASK);
1744}
1745
1746static void ath10k_pci_warm_reset_clear_lf(struct ath10k *ar)
1747{
1748 u32 val;
1749
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001750 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1751 SOC_LF_TIMER_CONTROL0_ADDRESS);
1752 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS +
1753 SOC_LF_TIMER_CONTROL0_ADDRESS,
1754 val & ~SOC_LF_TIMER_CONTROL0_ENABLE_MASK);
Michal Kazior61c16482014-10-28 10:32:06 +01001755}
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001756
Michal Kazior61c16482014-10-28 10:32:06 +01001757static int ath10k_pci_warm_reset(struct ath10k *ar)
1758{
1759 int ret;
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001760
Michal Kazior61c16482014-10-28 10:32:06 +01001761 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot warm reset\n");
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001762
Michal Kazior61c16482014-10-28 10:32:06 +01001763 spin_lock_bh(&ar->data_lock);
1764 ar->stats.fw_warm_reset_counter++;
1765 spin_unlock_bh(&ar->data_lock);
1766
1767 ath10k_pci_irq_disable(ar);
1768
1769 /* Make sure the target CPU is not doing anything dangerous, e.g. if it
1770 * were to access copy engine while host performs copy engine reset
1771 * then it is possible for the device to confuse pci-e controller to
1772 * the point of bringing host system to a complete stop (i.e. hang).
1773 */
Michal Kaziorde013572014-05-14 16:56:16 +03001774 ath10k_pci_warm_reset_si0(ar);
Michal Kazior61c16482014-10-28 10:32:06 +01001775 ath10k_pci_warm_reset_cpu(ar);
1776 ath10k_pci_init_pipes(ar);
1777 ath10k_pci_wait_for_target_init(ar);
Michal Kaziorde013572014-05-14 16:56:16 +03001778
Michal Kazior61c16482014-10-28 10:32:06 +01001779 ath10k_pci_warm_reset_clear_lf(ar);
1780 ath10k_pci_warm_reset_ce(ar);
1781 ath10k_pci_warm_reset_cpu(ar);
1782 ath10k_pci_init_pipes(ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001783
Michal Kazior61c16482014-10-28 10:32:06 +01001784 ret = ath10k_pci_wait_for_target_init(ar);
1785 if (ret) {
1786 ath10k_warn(ar, "failed to wait for target init: %d\n", ret);
1787 return ret;
1788 }
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001789
Michal Kazior7aa7a722014-08-25 12:09:38 +02001790 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot warm reset complete\n");
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001791
Michal Kaziorc0c378f2014-08-07 11:03:28 +02001792 return 0;
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001793}
1794
Michal Kazior0bc14d02014-10-28 10:32:07 +01001795static int ath10k_pci_chip_reset(struct ath10k *ar)
1796{
1797 int i, ret;
1798 u32 val;
1799
1800 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot chip reset\n");
1801
1802 /* Some hardware revisions (e.g. CUS223v2) has issues with cold reset.
1803 * It is thus preferred to use warm reset which is safer but may not be
1804 * able to recover the device from all possible fail scenarios.
1805 *
1806 * Warm reset doesn't always work on first try so attempt it a few
1807 * times before giving up.
1808 */
1809 for (i = 0; i < ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS; i++) {
1810 ret = ath10k_pci_warm_reset(ar);
1811 if (ret) {
1812 ath10k_warn(ar, "failed to warm reset attempt %d of %d: %d\n",
1813 i + 1, ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS,
1814 ret);
1815 continue;
1816 }
1817
1818 /* FIXME: Sometimes copy engine doesn't recover after warm
1819 * reset. In most cases this needs cold reset. In some of these
1820 * cases the device is in such a state that a cold reset may
1821 * lock up the host.
1822 *
1823 * Reading any host interest register via copy engine is
1824 * sufficient to verify if device is capable of booting
1825 * firmware blob.
1826 */
1827 ret = ath10k_pci_init_pipes(ar);
1828 if (ret) {
1829 ath10k_warn(ar, "failed to init copy engine: %d\n",
1830 ret);
1831 continue;
1832 }
1833
1834 ret = ath10k_pci_diag_read32(ar, QCA988X_HOST_INTEREST_ADDRESS,
1835 &val);
1836 if (ret) {
1837 ath10k_warn(ar, "failed to poke copy engine: %d\n",
1838 ret);
1839 continue;
1840 }
1841
1842 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot chip reset complete (warm)\n");
1843 return 0;
1844 }
1845
1846 if (ath10k_pci_reset_mode == ATH10K_PCI_RESET_WARM_ONLY) {
1847 ath10k_warn(ar, "refusing cold reset as requested\n");
1848 return -EPERM;
1849 }
1850
1851 ret = ath10k_pci_cold_reset(ar);
1852 if (ret) {
1853 ath10k_warn(ar, "failed to cold reset: %d\n", ret);
1854 return ret;
1855 }
1856
1857 ret = ath10k_pci_wait_for_target_init(ar);
1858 if (ret) {
1859 ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
1860 ret);
1861 return ret;
1862 }
1863
1864 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot chip reset complete (cold)\n");
1865
1866 return 0;
1867}
1868
1869static int ath10k_pci_hif_power_up(struct ath10k *ar)
Michal Kazior8c5c5362013-07-16 09:38:50 +02001870{
1871 int ret;
1872
Michal Kazior0bc14d02014-10-28 10:32:07 +01001873 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif power up\n");
1874
Michal Kazior8c5c5362013-07-16 09:38:50 +02001875 /*
1876 * Bring the target up cleanly.
1877 *
1878 * The target may be in an undefined state with an AUX-powered Target
1879 * and a Host in WoW mode. If the Host crashes, loses power, or is
1880 * restarted (without unloading the driver) then the Target is left
1881 * (aux) powered and running. On a subsequent driver load, the Target
1882 * is in an unexpected state. We try to catch that here in order to
1883 * reset the Target and retry the probe.
1884 */
Michal Kazior0bc14d02014-10-28 10:32:07 +01001885 ret = ath10k_pci_chip_reset(ar);
Michal Kazior5b2589f2013-11-08 08:01:30 +01001886 if (ret) {
Michal Kazior0bc14d02014-10-28 10:32:07 +01001887 ath10k_err(ar, "failed to reset chip: %d\n", ret);
Michal Kazior98563d52013-11-08 08:01:33 +01001888 goto err;
Michal Kazior5b2589f2013-11-08 08:01:30 +01001889 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02001890
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001891 ret = ath10k_pci_init_pipes(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001892 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001893 ath10k_err(ar, "failed to initialize CE: %d\n", ret);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02001894 goto err;
Michal Kaziorab977bd2013-11-25 14:06:26 +01001895 }
1896
Michal Kazior98563d52013-11-08 08:01:33 +01001897 ret = ath10k_pci_init_config(ar);
1898 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001899 ath10k_err(ar, "failed to setup init config: %d\n", ret);
Michal Kazior5c771e72014-08-22 14:23:34 +02001900 goto err_ce;
Michal Kazior98563d52013-11-08 08:01:33 +01001901 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02001902
1903 ret = ath10k_pci_wake_target_cpu(ar);
1904 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001905 ath10k_err(ar, "could not wake up target CPU: %d\n", ret);
Michal Kazior5c771e72014-08-22 14:23:34 +02001906 goto err_ce;
Michal Kazior8c5c5362013-07-16 09:38:50 +02001907 }
1908
1909 return 0;
1910
1911err_ce:
1912 ath10k_pci_ce_deinit(ar);
Michal Kazior0bc14d02014-10-28 10:32:07 +01001913
Michal Kazior8c5c5362013-07-16 09:38:50 +02001914err:
1915 return ret;
1916}
1917
1918static void ath10k_pci_hif_power_down(struct ath10k *ar)
1919{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001920 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif power down\n");
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02001921
Michal Kaziorc011b282014-10-28 10:32:08 +01001922 /* Currently hif_power_up performs effectively a reset and hif_stop
1923 * resets the chip as well so there's no point in resetting here.
1924 */
Michal Kazior8c5c5362013-07-16 09:38:50 +02001925}
1926
Michal Kazior8cd13ca2013-07-16 09:38:54 +02001927#ifdef CONFIG_PM
1928
1929#define ATH10K_PCI_PM_CONTROL 0x44
1930
1931static int ath10k_pci_hif_suspend(struct ath10k *ar)
1932{
1933 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1934 struct pci_dev *pdev = ar_pci->pdev;
1935 u32 val;
1936
1937 pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
1938
1939 if ((val & 0x000000ff) != 0x3) {
1940 pci_save_state(pdev);
1941 pci_disable_device(pdev);
1942 pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
1943 (val & 0xffffff00) | 0x03);
1944 }
1945
1946 return 0;
1947}
1948
1949static int ath10k_pci_hif_resume(struct ath10k *ar)
1950{
1951 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1952 struct pci_dev *pdev = ar_pci->pdev;
1953 u32 val;
1954
1955 pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
1956
1957 if ((val & 0x000000ff) != 0) {
1958 pci_restore_state(pdev);
1959 pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
1960 val & 0xffffff00);
1961 /*
1962 * Suspend/Resume resets the PCI configuration space,
1963 * so we have to re-disable the RETRY_TIMEOUT register (0x41)
1964 * to keep PCI Tx retries from interfering with C3 CPU state
1965 */
1966 pci_read_config_dword(pdev, 0x40, &val);
1967
1968 if ((val & 0x0000ff00) != 0)
1969 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
1970 }
1971
1972 return 0;
1973}
1974#endif
1975
Kalle Valo5e3dd152013-06-12 20:52:10 +03001976static const struct ath10k_hif_ops ath10k_pci_hif_ops = {
Michal Kazior726346f2014-02-27 18:50:04 +02001977 .tx_sg = ath10k_pci_hif_tx_sg,
Kalle Valoeef25402014-09-24 14:16:52 +03001978 .diag_read = ath10k_pci_hif_diag_read,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001979 .exchange_bmi_msg = ath10k_pci_hif_exchange_bmi_msg,
1980 .start = ath10k_pci_hif_start,
1981 .stop = ath10k_pci_hif_stop,
1982 .map_service_to_pipe = ath10k_pci_hif_map_service_to_pipe,
1983 .get_default_pipe = ath10k_pci_hif_get_default_pipe,
1984 .send_complete_check = ath10k_pci_hif_send_complete_check,
Michal Kaziore799bbf2013-07-05 16:15:12 +03001985 .set_callbacks = ath10k_pci_hif_set_callbacks,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001986 .get_free_queue_number = ath10k_pci_hif_get_free_queue_number,
Michal Kazior8c5c5362013-07-16 09:38:50 +02001987 .power_up = ath10k_pci_hif_power_up,
1988 .power_down = ath10k_pci_hif_power_down,
Michal Kazior8cd13ca2013-07-16 09:38:54 +02001989#ifdef CONFIG_PM
1990 .suspend = ath10k_pci_hif_suspend,
1991 .resume = ath10k_pci_hif_resume,
1992#endif
Kalle Valo5e3dd152013-06-12 20:52:10 +03001993};
1994
1995static void ath10k_pci_ce_tasklet(unsigned long ptr)
1996{
Michal Kazior87263e52013-08-27 13:08:01 +02001997 struct ath10k_pci_pipe *pipe = (struct ath10k_pci_pipe *)ptr;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001998 struct ath10k_pci *ar_pci = pipe->ar_pci;
1999
2000 ath10k_ce_per_engine_service(ar_pci->ar, pipe->pipe_num);
2001}
2002
2003static void ath10k_msi_err_tasklet(unsigned long data)
2004{
2005 struct ath10k *ar = (struct ath10k *)data;
2006
Michal Kazior5c771e72014-08-22 14:23:34 +02002007 if (!ath10k_pci_has_fw_crashed(ar)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002008 ath10k_warn(ar, "received unsolicited fw crash interrupt\n");
Michal Kazior5c771e72014-08-22 14:23:34 +02002009 return;
2010 }
2011
2012 ath10k_pci_fw_crashed_clear(ar);
2013 ath10k_pci_fw_crashed_dump(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002014}
2015
2016/*
2017 * Handler for a per-engine interrupt on a PARTICULAR CE.
2018 * This is used in cases where each CE has a private MSI interrupt.
2019 */
2020static irqreturn_t ath10k_pci_per_engine_handler(int irq, void *arg)
2021{
2022 struct ath10k *ar = arg;
2023 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2024 int ce_id = irq - ar_pci->pdev->irq - MSI_ASSIGN_CE_INITIAL;
2025
Dan Carpentere5742672013-06-18 10:28:46 +03002026 if (ce_id < 0 || ce_id >= ARRAY_SIZE(ar_pci->pipe_info)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002027 ath10k_warn(ar, "unexpected/invalid irq %d ce_id %d\n", irq,
2028 ce_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002029 return IRQ_HANDLED;
2030 }
2031
2032 /*
2033 * NOTE: We are able to derive ce_id from irq because we
2034 * use a one-to-one mapping for CE's 0..5.
2035 * CE's 6 & 7 do not use interrupts at all.
2036 *
2037 * This mapping must be kept in sync with the mapping
2038 * used by firmware.
2039 */
2040 tasklet_schedule(&ar_pci->pipe_info[ce_id].intr);
2041 return IRQ_HANDLED;
2042}
2043
2044static irqreturn_t ath10k_pci_msi_fw_handler(int irq, void *arg)
2045{
2046 struct ath10k *ar = arg;
2047 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2048
2049 tasklet_schedule(&ar_pci->msi_fw_err);
2050 return IRQ_HANDLED;
2051}
2052
2053/*
2054 * Top-level interrupt handler for all PCI interrupts from a Target.
2055 * When a block of MSI interrupts is allocated, this top-level handler
2056 * is not used; instead, we directly call the correct sub-handler.
2057 */
2058static irqreturn_t ath10k_pci_interrupt_handler(int irq, void *arg)
2059{
2060 struct ath10k *ar = arg;
2061 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2062
2063 if (ar_pci->num_msi_intrs == 0) {
Michal Kaziore5398872013-11-25 14:06:20 +01002064 if (!ath10k_pci_irq_pending(ar))
2065 return IRQ_NONE;
2066
Michal Kazior26852182013-11-25 14:06:25 +01002067 ath10k_pci_disable_and_clear_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002068 }
2069
2070 tasklet_schedule(&ar_pci->intr_tq);
2071
2072 return IRQ_HANDLED;
2073}
2074
2075static void ath10k_pci_tasklet(unsigned long data)
2076{
2077 struct ath10k *ar = (struct ath10k *)data;
2078 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2079
Michal Kazior5c771e72014-08-22 14:23:34 +02002080 if (ath10k_pci_has_fw_crashed(ar)) {
2081 ath10k_pci_fw_crashed_clear(ar);
2082 ath10k_pci_fw_crashed_dump(ar);
2083 return;
2084 }
2085
Kalle Valo5e3dd152013-06-12 20:52:10 +03002086 ath10k_ce_per_engine_service_any(ar);
2087
Michal Kazior26852182013-11-25 14:06:25 +01002088 /* Re-enable legacy irq that was disabled in the irq handler */
2089 if (ar_pci->num_msi_intrs == 0)
2090 ath10k_pci_enable_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002091}
2092
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002093static int ath10k_pci_request_irq_msix(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002094{
2095 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002096 int ret, i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002097
2098 ret = request_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW,
2099 ath10k_pci_msi_fw_handler,
2100 IRQF_SHARED, "ath10k_pci", ar);
Michal Kazior591ecdb2013-07-31 10:55:15 +02002101 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002102 ath10k_warn(ar, "failed to request MSI-X fw irq %d: %d\n",
Michal Kazior591ecdb2013-07-31 10:55:15 +02002103 ar_pci->pdev->irq + MSI_ASSIGN_FW, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002104 return ret;
Michal Kazior591ecdb2013-07-31 10:55:15 +02002105 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002106
2107 for (i = MSI_ASSIGN_CE_INITIAL; i <= MSI_ASSIGN_CE_MAX; i++) {
2108 ret = request_irq(ar_pci->pdev->irq + i,
2109 ath10k_pci_per_engine_handler,
2110 IRQF_SHARED, "ath10k_pci", ar);
2111 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002112 ath10k_warn(ar, "failed to request MSI-X ce irq %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002113 ar_pci->pdev->irq + i, ret);
2114
Michal Kazior87b14232013-06-26 08:50:50 +02002115 for (i--; i >= MSI_ASSIGN_CE_INITIAL; i--)
2116 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002117
Michal Kazior87b14232013-06-26 08:50:50 +02002118 free_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002119 return ret;
2120 }
2121 }
2122
Kalle Valo5e3dd152013-06-12 20:52:10 +03002123 return 0;
2124}
2125
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002126static int ath10k_pci_request_irq_msi(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002127{
2128 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2129 int ret;
2130
2131 ret = request_irq(ar_pci->pdev->irq,
2132 ath10k_pci_interrupt_handler,
2133 IRQF_SHARED, "ath10k_pci", ar);
Kalle Valof3782742013-10-17 11:36:15 +03002134 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002135 ath10k_warn(ar, "failed to request MSI irq %d: %d\n",
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002136 ar_pci->pdev->irq, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002137 return ret;
Kalle Valof3782742013-10-17 11:36:15 +03002138 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002139
Kalle Valo5e3dd152013-06-12 20:52:10 +03002140 return 0;
2141}
2142
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002143static int ath10k_pci_request_irq_legacy(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002144{
2145 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002146 int ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002147
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002148 ret = request_irq(ar_pci->pdev->irq,
2149 ath10k_pci_interrupt_handler,
2150 IRQF_SHARED, "ath10k_pci", ar);
Kalle Valof3782742013-10-17 11:36:15 +03002151 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002152 ath10k_warn(ar, "failed to request legacy irq %d: %d\n",
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002153 ar_pci->pdev->irq, ret);
Kalle Valof3782742013-10-17 11:36:15 +03002154 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002155 }
2156
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002157 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002158}
2159
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002160static int ath10k_pci_request_irq(struct ath10k *ar)
2161{
2162 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2163
2164 switch (ar_pci->num_msi_intrs) {
2165 case 0:
2166 return ath10k_pci_request_irq_legacy(ar);
2167 case 1:
2168 return ath10k_pci_request_irq_msi(ar);
2169 case MSI_NUM_REQUEST:
2170 return ath10k_pci_request_irq_msix(ar);
2171 }
2172
Michal Kazior7aa7a722014-08-25 12:09:38 +02002173 ath10k_warn(ar, "unknown irq configuration upon request\n");
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002174 return -EINVAL;
2175}
2176
2177static void ath10k_pci_free_irq(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002178{
2179 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2180 int i;
2181
2182 /* There's at least one interrupt irregardless whether its legacy INTR
2183 * or MSI or MSI-X */
2184 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
2185 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002186}
2187
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002188static void ath10k_pci_init_irq_tasklets(struct ath10k *ar)
2189{
2190 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2191 int i;
2192
2193 tasklet_init(&ar_pci->intr_tq, ath10k_pci_tasklet, (unsigned long)ar);
2194 tasklet_init(&ar_pci->msi_fw_err, ath10k_msi_err_tasklet,
2195 (unsigned long)ar);
2196
2197 for (i = 0; i < CE_COUNT; i++) {
2198 ar_pci->pipe_info[i].ar_pci = ar_pci;
2199 tasklet_init(&ar_pci->pipe_info[i].intr, ath10k_pci_ce_tasklet,
2200 (unsigned long)&ar_pci->pipe_info[i]);
2201 }
2202}
2203
2204static int ath10k_pci_init_irq(struct ath10k *ar)
2205{
2206 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2207 int ret;
2208
2209 ath10k_pci_init_irq_tasklets(ar);
2210
Michal Kazior403d6272014-08-22 14:23:31 +02002211 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_AUTO)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002212 ath10k_info(ar, "limiting irq mode to: %d\n",
2213 ath10k_pci_irq_mode);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002214
2215 /* Try MSI-X */
Michal Kazior0edf2572014-08-07 11:03:29 +02002216 if (ath10k_pci_irq_mode == ATH10K_PCI_IRQ_AUTO) {
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002217 ar_pci->num_msi_intrs = MSI_NUM_REQUEST;
Alexander Gordeev5ad68672014-02-13 17:50:02 +02002218 ret = pci_enable_msi_range(ar_pci->pdev, ar_pci->num_msi_intrs,
Kalle Valo5b07e072014-09-14 12:50:06 +03002219 ar_pci->num_msi_intrs);
Alexander Gordeev5ad68672014-02-13 17:50:02 +02002220 if (ret > 0)
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002221 return 0;
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002222
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002223 /* fall-through */
2224 }
2225
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002226 /* Try MSI */
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002227 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_LEGACY) {
2228 ar_pci->num_msi_intrs = 1;
2229 ret = pci_enable_msi(ar_pci->pdev);
2230 if (ret == 0)
2231 return 0;
2232
2233 /* fall-through */
2234 }
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002235
2236 /* Try legacy irq
2237 *
2238 * A potential race occurs here: The CORE_BASE write
2239 * depends on target correctly decoding AXI address but
2240 * host won't know when target writes BAR to CORE_CTRL.
2241 * This write might get lost if target has NOT written BAR.
2242 * For now, fix the race by repeating the write in below
2243 * synchronization checking. */
2244 ar_pci->num_msi_intrs = 0;
2245
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002246 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2247 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002248
2249 return 0;
2250}
2251
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002252static void ath10k_pci_deinit_irq_legacy(struct ath10k *ar)
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002253{
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002254 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2255 0);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002256}
2257
2258static int ath10k_pci_deinit_irq(struct ath10k *ar)
2259{
2260 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2261
2262 switch (ar_pci->num_msi_intrs) {
2263 case 0:
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002264 ath10k_pci_deinit_irq_legacy(ar);
2265 return 0;
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002266 case 1:
2267 /* fall-through */
2268 case MSI_NUM_REQUEST:
2269 pci_disable_msi(ar_pci->pdev);
2270 return 0;
Alexander Gordeevbb8b6212014-02-13 17:50:01 +02002271 default:
2272 pci_disable_msi(ar_pci->pdev);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002273 }
2274
Michal Kazior7aa7a722014-08-25 12:09:38 +02002275 ath10k_warn(ar, "unknown irq configuration upon deinit\n");
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002276 return -EINVAL;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002277}
2278
Michal Kaziord7fb47f2013-11-08 08:01:26 +01002279static int ath10k_pci_wait_for_target_init(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002280{
2281 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo0399eca2014-03-28 09:32:21 +02002282 unsigned long timeout;
Kalle Valo0399eca2014-03-28 09:32:21 +02002283 u32 val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002284
Michal Kazior7aa7a722014-08-25 12:09:38 +02002285 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot waiting target to initialise\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002286
Kalle Valo0399eca2014-03-28 09:32:21 +02002287 timeout = jiffies + msecs_to_jiffies(ATH10K_PCI_TARGET_WAIT);
2288
2289 do {
2290 val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
2291
Michal Kazior7aa7a722014-08-25 12:09:38 +02002292 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target indicator %x\n",
2293 val);
Kalle Valo50f87a62014-03-28 09:32:52 +02002294
Kalle Valo0399eca2014-03-28 09:32:21 +02002295 /* target should never return this */
2296 if (val == 0xffffffff)
2297 continue;
2298
Michal Kazior7710cd22014-04-23 19:30:04 +03002299 /* the device has crashed so don't bother trying anymore */
2300 if (val & FW_IND_EVENT_PENDING)
2301 break;
2302
Kalle Valo0399eca2014-03-28 09:32:21 +02002303 if (val & FW_IND_INITIALIZED)
2304 break;
2305
Kalle Valo5e3dd152013-06-12 20:52:10 +03002306 if (ar_pci->num_msi_intrs == 0)
2307 /* Fix potential race by repeating CORE_BASE writes */
Michal Kaziora4282492014-10-20 14:14:37 +02002308 ath10k_pci_enable_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002309
Kalle Valo0399eca2014-03-28 09:32:21 +02002310 mdelay(10);
2311 } while (time_before(jiffies, timeout));
2312
Michal Kaziora4282492014-10-20 14:14:37 +02002313 ath10k_pci_disable_and_clear_legacy_irq(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02002314 ath10k_pci_irq_msi_fw_mask(ar);
Michal Kaziora4282492014-10-20 14:14:37 +02002315
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002316 if (val == 0xffffffff) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002317 ath10k_err(ar, "failed to read device register, device is gone\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002318 return -EIO;
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002319 }
2320
Michal Kazior7710cd22014-04-23 19:30:04 +03002321 if (val & FW_IND_EVENT_PENDING) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002322 ath10k_warn(ar, "device has crashed during init\n");
Michal Kazior5c771e72014-08-22 14:23:34 +02002323 ath10k_pci_fw_crashed_clear(ar);
Kalle Valo0e9848c2014-08-25 08:37:37 +03002324 ath10k_pci_fw_crashed_dump(ar);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002325 return -ECOMM;
Michal Kazior7710cd22014-04-23 19:30:04 +03002326 }
2327
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002328 if (!(val & FW_IND_INITIALIZED)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002329 ath10k_err(ar, "failed to receive initialized event from target: %08x\n",
Kalle Valo0399eca2014-03-28 09:32:21 +02002330 val);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002331 return -ETIMEDOUT;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002332 }
2333
Michal Kazior7aa7a722014-08-25 12:09:38 +02002334 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target initialised\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002335 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002336}
2337
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002338static int ath10k_pci_cold_reset(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002339{
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002340 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002341 u32 val;
2342
Michal Kazior7aa7a722014-08-25 12:09:38 +02002343 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cold reset\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002344
Ben Greearf51dbe72014-09-29 14:41:46 +03002345 spin_lock_bh(&ar->data_lock);
2346
2347 ar->stats.fw_cold_reset_counter++;
2348
2349 spin_unlock_bh(&ar->data_lock);
2350
Kalle Valo5e3dd152013-06-12 20:52:10 +03002351 /* Put Target, including PCIe, into RESET. */
Kalle Valoe479ed42013-09-01 10:01:53 +03002352 val = ath10k_pci_reg_read32(ar, SOC_GLOBAL_RESET_ADDRESS);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002353 val |= 1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002354 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002355
2356 for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
Kalle Valoe479ed42013-09-01 10:01:53 +03002357 if (ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03002358 RTC_STATE_COLD_RESET_MASK)
2359 break;
2360 msleep(1);
2361 }
2362
2363 /* Pull Target, including PCIe, out of RESET. */
2364 val &= ~1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002365 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002366
2367 for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
Kalle Valoe479ed42013-09-01 10:01:53 +03002368 if (!(ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03002369 RTC_STATE_COLD_RESET_MASK))
2370 break;
2371 msleep(1);
2372 }
2373
Michal Kazior7aa7a722014-08-25 12:09:38 +02002374 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cold reset complete\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02002375
Michal Kazior5b2589f2013-11-08 08:01:30 +01002376 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002377}
2378
Michal Kazior2986e3e2014-08-07 11:03:30 +02002379static int ath10k_pci_claim(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002380{
Michal Kazior2986e3e2014-08-07 11:03:30 +02002381 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2382 struct pci_dev *pdev = ar_pci->pdev;
2383 u32 lcr_val;
2384 int ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002385
2386 pci_set_drvdata(pdev, ar);
2387
Kalle Valo5e3dd152013-06-12 20:52:10 +03002388 ret = pci_enable_device(pdev);
2389 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002390 ath10k_err(ar, "failed to enable pci device: %d\n", ret);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002391 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002392 }
2393
Kalle Valo5e3dd152013-06-12 20:52:10 +03002394 ret = pci_request_region(pdev, BAR_NUM, "ath");
2395 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002396 ath10k_err(ar, "failed to request region BAR%d: %d\n", BAR_NUM,
Michal Kazior2986e3e2014-08-07 11:03:30 +02002397 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002398 goto err_device;
2399 }
2400
Michal Kazior2986e3e2014-08-07 11:03:30 +02002401 /* Target expects 32 bit DMA. Enforce it. */
Kalle Valo5e3dd152013-06-12 20:52:10 +03002402 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2403 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002404 ath10k_err(ar, "failed to set dma mask to 32-bit: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002405 goto err_region;
2406 }
2407
2408 ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2409 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002410 ath10k_err(ar, "failed to set consistent dma mask to 32-bit: %d\n",
Michal Kazior2986e3e2014-08-07 11:03:30 +02002411 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002412 goto err_region;
2413 }
2414
Kalle Valo5e3dd152013-06-12 20:52:10 +03002415 pci_set_master(pdev);
2416
Michal Kazior2986e3e2014-08-07 11:03:30 +02002417 /* Workaround: Disable ASPM */
Kalle Valo5e3dd152013-06-12 20:52:10 +03002418 pci_read_config_dword(pdev, 0x80, &lcr_val);
2419 pci_write_config_dword(pdev, 0x80, (lcr_val & 0xffffff00));
2420
2421 /* Arrange for access to Target SoC registers. */
Michal Kazior2986e3e2014-08-07 11:03:30 +02002422 ar_pci->mem = pci_iomap(pdev, BAR_NUM, 0);
2423 if (!ar_pci->mem) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002424 ath10k_err(ar, "failed to iomap BAR%d\n", BAR_NUM);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002425 ret = -EIO;
2426 goto err_master;
2427 }
2428
Michal Kazior7aa7a722014-08-25 12:09:38 +02002429 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot pci_mem 0x%p\n", ar_pci->mem);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002430 return 0;
2431
2432err_master:
2433 pci_clear_master(pdev);
2434
2435err_region:
2436 pci_release_region(pdev, BAR_NUM);
2437
2438err_device:
2439 pci_disable_device(pdev);
2440
2441 return ret;
2442}
2443
2444static void ath10k_pci_release(struct ath10k *ar)
2445{
2446 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2447 struct pci_dev *pdev = ar_pci->pdev;
2448
2449 pci_iounmap(pdev, ar_pci->mem);
2450 pci_release_region(pdev, BAR_NUM);
2451 pci_clear_master(pdev);
2452 pci_disable_device(pdev);
2453}
2454
Kalle Valo5e3dd152013-06-12 20:52:10 +03002455static int ath10k_pci_probe(struct pci_dev *pdev,
2456 const struct pci_device_id *pci_dev)
2457{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002458 int ret = 0;
2459 struct ath10k *ar;
2460 struct ath10k_pci *ar_pci;
Michal Kazior2986e3e2014-08-07 11:03:30 +02002461 u32 chip_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002462
Michal Kaziore7b54192014-08-07 11:03:27 +02002463 ar = ath10k_core_create(sizeof(*ar_pci), &pdev->dev,
Kalle Valoe07db352014-10-13 09:40:47 +03002464 ATH10K_BUS_PCI,
Michal Kaziore7b54192014-08-07 11:03:27 +02002465 &ath10k_pci_hif_ops);
2466 if (!ar) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002467 dev_err(&pdev->dev, "failed to allocate core\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002468 return -ENOMEM;
Michal Kaziore7b54192014-08-07 11:03:27 +02002469 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002470
Michal Kazior7aa7a722014-08-25 12:09:38 +02002471 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci probe\n");
2472
Michal Kaziore7b54192014-08-07 11:03:27 +02002473 ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002474 ar_pci->pdev = pdev;
2475 ar_pci->dev = &pdev->dev;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002476 ar_pci->ar = ar;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002477
2478 spin_lock_init(&ar_pci->ce_lock);
Michal Kazior728f95e2014-08-22 14:33:14 +02002479 setup_timer(&ar_pci->rx_post_retry, ath10k_pci_rx_replenish_retry,
2480 (unsigned long)ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002481
Michal Kazior2986e3e2014-08-07 11:03:30 +02002482 ret = ath10k_pci_claim(ar);
Kalle Valoe01ae682013-09-01 11:22:14 +03002483 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002484 ath10k_err(ar, "failed to claim device: %d\n", ret);
Michal Kaziore7b54192014-08-07 11:03:27 +02002485 goto err_core_destroy;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002486 }
2487
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002488 ret = ath10k_pci_wake(ar);
Kalle Valoe01ae682013-09-01 11:22:14 +03002489 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002490 ath10k_err(ar, "failed to wake up: %d\n", ret);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002491 goto err_release;
Kalle Valoe01ae682013-09-01 11:22:14 +03002492 }
2493
Kalle Valo233eb972013-10-16 16:46:11 +03002494 chip_id = ath10k_pci_soc_read32(ar, SOC_CHIP_ID_ADDRESS);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002495 if (chip_id == 0xffffffff) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002496 ath10k_err(ar, "failed to get chip id\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002497 goto err_sleep;
2498 }
Kalle Valoe01ae682013-09-01 11:22:14 +03002499
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002500 ret = ath10k_pci_alloc_pipes(ar);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002501 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002502 ath10k_err(ar, "failed to allocate copy engine pipes: %d\n",
2503 ret);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002504 goto err_sleep;
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002505 }
2506
Michal Kazior403d6272014-08-22 14:23:31 +02002507 ath10k_pci_ce_deinit(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02002508 ath10k_pci_irq_disable(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02002509
Michal Kazior403d6272014-08-22 14:23:31 +02002510 ret = ath10k_pci_init_irq(ar);
2511 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002512 ath10k_err(ar, "failed to init irqs: %d\n", ret);
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002513 goto err_free_pipes;
Michal Kazior403d6272014-08-22 14:23:31 +02002514 }
2515
Michal Kazior7aa7a722014-08-25 12:09:38 +02002516 ath10k_info(ar, "pci irq %s interrupts %d irq_mode %d reset_mode %d\n",
Michal Kazior403d6272014-08-22 14:23:31 +02002517 ath10k_pci_get_irq_method(ar), ar_pci->num_msi_intrs,
2518 ath10k_pci_irq_mode, ath10k_pci_reset_mode);
2519
Michal Kazior5c771e72014-08-22 14:23:34 +02002520 ret = ath10k_pci_request_irq(ar);
Michal Kazior403d6272014-08-22 14:23:31 +02002521 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002522 ath10k_warn(ar, "failed to request irqs: %d\n", ret);
Michal Kazior403d6272014-08-22 14:23:31 +02002523 goto err_deinit_irq;
2524 }
2525
Kalle Valoe01ae682013-09-01 11:22:14 +03002526 ret = ath10k_core_register(ar, chip_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002527 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002528 ath10k_err(ar, "failed to register driver core: %d\n", ret);
Michal Kazior5c771e72014-08-22 14:23:34 +02002529 goto err_free_irq;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002530 }
2531
2532 return 0;
2533
Michal Kazior5c771e72014-08-22 14:23:34 +02002534err_free_irq:
2535 ath10k_pci_free_irq(ar);
Michal Kazior21396272014-08-28 10:24:40 +02002536 ath10k_pci_kill_tasklet(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02002537
Michal Kazior403d6272014-08-22 14:23:31 +02002538err_deinit_irq:
2539 ath10k_pci_deinit_irq(ar);
2540
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002541err_free_pipes:
2542 ath10k_pci_free_pipes(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002543
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002544err_sleep:
2545 ath10k_pci_sleep(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002546
2547err_release:
2548 ath10k_pci_release(ar);
2549
Michal Kaziore7b54192014-08-07 11:03:27 +02002550err_core_destroy:
Kalle Valo5e3dd152013-06-12 20:52:10 +03002551 ath10k_core_destroy(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002552
2553 return ret;
2554}
2555
2556static void ath10k_pci_remove(struct pci_dev *pdev)
2557{
2558 struct ath10k *ar = pci_get_drvdata(pdev);
2559 struct ath10k_pci *ar_pci;
2560
Michal Kazior7aa7a722014-08-25 12:09:38 +02002561 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci remove\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002562
2563 if (!ar)
2564 return;
2565
2566 ar_pci = ath10k_pci_priv(ar);
2567
2568 if (!ar_pci)
2569 return;
2570
Kalle Valo5e3dd152013-06-12 20:52:10 +03002571 ath10k_core_unregister(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02002572 ath10k_pci_free_irq(ar);
Michal Kazior21396272014-08-28 10:24:40 +02002573 ath10k_pci_kill_tasklet(ar);
Michal Kazior403d6272014-08-22 14:23:31 +02002574 ath10k_pci_deinit_irq(ar);
2575 ath10k_pci_ce_deinit(ar);
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002576 ath10k_pci_free_pipes(ar);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002577 ath10k_pci_sleep(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002578 ath10k_pci_release(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002579 ath10k_core_destroy(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002580}
2581
Kalle Valo5e3dd152013-06-12 20:52:10 +03002582MODULE_DEVICE_TABLE(pci, ath10k_pci_id_table);
2583
2584static struct pci_driver ath10k_pci_driver = {
2585 .name = "ath10k_pci",
2586 .id_table = ath10k_pci_id_table,
2587 .probe = ath10k_pci_probe,
2588 .remove = ath10k_pci_remove,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002589};
2590
2591static int __init ath10k_pci_init(void)
2592{
2593 int ret;
2594
2595 ret = pci_register_driver(&ath10k_pci_driver);
2596 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002597 printk(KERN_ERR "failed to register ath10k pci driver: %d\n",
2598 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002599
2600 return ret;
2601}
2602module_init(ath10k_pci_init);
2603
2604static void __exit ath10k_pci_exit(void)
2605{
2606 pci_unregister_driver(&ath10k_pci_driver);
2607}
2608
2609module_exit(ath10k_pci_exit);
2610
2611MODULE_AUTHOR("Qualcomm Atheros");
2612MODULE_DESCRIPTION("Driver support for Atheros QCA988X PCIe devices");
2613MODULE_LICENSE("Dual BSD/GPL");
Bartosz Markowski8026cae2014-10-06 14:16:41 +02002614MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_FW_FILE);
2615MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API2_FILE);
2616MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API3_FILE);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002617MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_BOARD_DATA_FILE);