blob: 08e88a3758c168925cb3f361e6144d8563d40458 [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";
365 else if (ar_pci->num_msi_intrs == 1)
366 return "msi";
367 else
368 return "legacy";
Michal Kaziorab977bd2013-11-25 14:06:26 +0100369}
370
Michal Kazior728f95e2014-08-22 14:33:14 +0200371static int __ath10k_pci_rx_post_buf(struct ath10k_pci_pipe *pipe)
Michal Kaziorab977bd2013-11-25 14:06:26 +0100372{
Michal Kazior728f95e2014-08-22 14:33:14 +0200373 struct ath10k *ar = pipe->hif_ce_state;
Michal Kaziorab977bd2013-11-25 14:06:26 +0100374 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior728f95e2014-08-22 14:33:14 +0200375 struct ath10k_ce_pipe *ce_pipe = pipe->ce_hdl;
376 struct sk_buff *skb;
377 dma_addr_t paddr;
Michal Kaziorab977bd2013-11-25 14:06:26 +0100378 int ret;
379
Michal Kazior728f95e2014-08-22 14:33:14 +0200380 lockdep_assert_held(&ar_pci->ce_lock);
381
382 skb = dev_alloc_skb(pipe->buf_sz);
383 if (!skb)
384 return -ENOMEM;
385
386 WARN_ONCE((unsigned long)skb->data & 3, "unaligned skb");
387
388 paddr = dma_map_single(ar->dev, skb->data,
389 skb->len + skb_tailroom(skb),
390 DMA_FROM_DEVICE);
391 if (unlikely(dma_mapping_error(ar->dev, paddr))) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200392 ath10k_warn(ar, "failed to dma map pci rx buf\n");
Michal Kazior728f95e2014-08-22 14:33:14 +0200393 dev_kfree_skb_any(skb);
394 return -EIO;
395 }
396
397 ATH10K_SKB_CB(skb)->paddr = paddr;
398
399 ret = __ath10k_ce_rx_post_buf(ce_pipe, skb, paddr);
Michal Kaziorab977bd2013-11-25 14:06:26 +0100400 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200401 ath10k_warn(ar, "failed to post pci rx buf: %d\n", ret);
Michal Kazior728f95e2014-08-22 14:33:14 +0200402 dma_unmap_single(ar->dev, paddr, skb->len + skb_tailroom(skb),
403 DMA_FROM_DEVICE);
404 dev_kfree_skb_any(skb);
Michal Kaziorab977bd2013-11-25 14:06:26 +0100405 return ret;
406 }
407
408 return 0;
409}
410
Michal Kazior728f95e2014-08-22 14:33:14 +0200411static void __ath10k_pci_rx_post_pipe(struct ath10k_pci_pipe *pipe)
Michal Kaziorab977bd2013-11-25 14:06:26 +0100412{
Michal Kazior728f95e2014-08-22 14:33:14 +0200413 struct ath10k *ar = pipe->hif_ce_state;
414 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
415 struct ath10k_ce_pipe *ce_pipe = pipe->ce_hdl;
416 int ret, num;
417
418 lockdep_assert_held(&ar_pci->ce_lock);
419
420 if (pipe->buf_sz == 0)
421 return;
422
423 if (!ce_pipe->dest_ring)
424 return;
425
426 num = __ath10k_ce_rx_num_free_bufs(ce_pipe);
427 while (num--) {
428 ret = __ath10k_pci_rx_post_buf(pipe);
429 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200430 ath10k_warn(ar, "failed to post pci rx buf: %d\n", ret);
Michal Kazior728f95e2014-08-22 14:33:14 +0200431 mod_timer(&ar_pci->rx_post_retry, jiffies +
432 ATH10K_PCI_RX_POST_RETRY_MS);
433 break;
434 }
435 }
436}
437
438static void ath10k_pci_rx_post_pipe(struct ath10k_pci_pipe *pipe)
439{
440 struct ath10k *ar = pipe->hif_ce_state;
441 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
442
443 spin_lock_bh(&ar_pci->ce_lock);
444 __ath10k_pci_rx_post_pipe(pipe);
445 spin_unlock_bh(&ar_pci->ce_lock);
446}
447
448static void ath10k_pci_rx_post(struct ath10k *ar)
449{
450 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
451 int i;
452
453 spin_lock_bh(&ar_pci->ce_lock);
454 for (i = 0; i < CE_COUNT; i++)
455 __ath10k_pci_rx_post_pipe(&ar_pci->pipe_info[i]);
456 spin_unlock_bh(&ar_pci->ce_lock);
457}
458
459static void ath10k_pci_rx_replenish_retry(unsigned long ptr)
460{
461 struct ath10k *ar = (void *)ptr;
462
463 ath10k_pci_rx_post(ar);
Michal Kaziorab977bd2013-11-25 14:06:26 +0100464}
465
Kalle Valo5e3dd152013-06-12 20:52:10 +0300466/*
467 * Diagnostic read/write access is provided for startup/config/debug usage.
468 * Caller must guarantee proper alignment, when applicable, and single user
469 * at any moment.
470 */
471static int ath10k_pci_diag_read_mem(struct ath10k *ar, u32 address, void *data,
472 int nbytes)
473{
474 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
475 int ret = 0;
476 u32 buf;
477 unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
478 unsigned int id;
479 unsigned int flags;
Michal Kazior2aa39112013-08-27 13:08:02 +0200480 struct ath10k_ce_pipe *ce_diag;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300481 /* Host buffer address in CE space */
482 u32 ce_data;
483 dma_addr_t ce_data_base = 0;
484 void *data_buf = NULL;
485 int i;
486
Kalle Valo5e3dd152013-06-12 20:52:10 +0300487 ce_diag = ar_pci->ce_diag;
488
489 /*
490 * Allocate a temporary bounce buffer to hold caller's data
491 * to be DMA'ed from Target. This guarantees
492 * 1) 4-byte alignment
493 * 2) Buffer in DMA-able space
494 */
495 orig_nbytes = nbytes;
Michal Kazior68c03242014-03-28 10:02:35 +0200496 data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
497 orig_nbytes,
498 &ce_data_base,
499 GFP_ATOMIC);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300500
501 if (!data_buf) {
502 ret = -ENOMEM;
503 goto done;
504 }
505 memset(data_buf, 0, orig_nbytes);
506
507 remaining_bytes = orig_nbytes;
508 ce_data = ce_data_base;
509 while (remaining_bytes) {
510 nbytes = min_t(unsigned int, remaining_bytes,
511 DIAG_TRANSFER_LIMIT);
512
Michal Kazior728f95e2014-08-22 14:33:14 +0200513 ret = ath10k_ce_rx_post_buf(ce_diag, NULL, ce_data);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300514 if (ret != 0)
515 goto done;
516
517 /* Request CE to send from Target(!) address to Host buffer */
518 /*
519 * The address supplied by the caller is in the
520 * Target CPU virtual address space.
521 *
522 * In order to use this address with the diagnostic CE,
523 * convert it from Target CPU virtual address space
524 * to CE address space
525 */
Kalle Valo5e3dd152013-06-12 20:52:10 +0300526 address = TARG_CPU_SPACE_TO_CE_SPACE(ar, ar_pci->mem,
527 address);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300528
529 ret = ath10k_ce_send(ce_diag, NULL, (u32)address, nbytes, 0,
Kalle Valo5b07e072014-09-14 12:50:06 +0300530 0);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300531 if (ret)
532 goto done;
533
534 i = 0;
535 while (ath10k_ce_completed_send_next(ce_diag, NULL, &buf,
536 &completed_nbytes,
537 &id) != 0) {
538 mdelay(1);
539 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
540 ret = -EBUSY;
541 goto done;
542 }
543 }
544
545 if (nbytes != completed_nbytes) {
546 ret = -EIO;
547 goto done;
548 }
549
Kalle Valocfbc06a2014-09-14 12:50:23 +0300550 if (buf != (u32)address) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300551 ret = -EIO;
552 goto done;
553 }
554
555 i = 0;
556 while (ath10k_ce_completed_recv_next(ce_diag, NULL, &buf,
557 &completed_nbytes,
558 &id, &flags) != 0) {
559 mdelay(1);
560
561 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
562 ret = -EBUSY;
563 goto done;
564 }
565 }
566
567 if (nbytes != completed_nbytes) {
568 ret = -EIO;
569 goto done;
570 }
571
572 if (buf != ce_data) {
573 ret = -EIO;
574 goto done;
575 }
576
577 remaining_bytes -= nbytes;
578 address += nbytes;
579 ce_data += nbytes;
580 }
581
582done:
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300583 if (ret == 0)
584 memcpy(data, data_buf, orig_nbytes);
585 else
Michal Kazior7aa7a722014-08-25 12:09:38 +0200586 ath10k_warn(ar, "failed to read diag value at 0x%x: %d\n",
Kalle Valo50f87a62014-03-28 09:32:52 +0200587 address, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300588
589 if (data_buf)
Michal Kazior68c03242014-03-28 10:02:35 +0200590 dma_free_coherent(ar->dev, orig_nbytes, data_buf,
591 ce_data_base);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300592
593 return ret;
594}
595
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300596static int ath10k_pci_diag_read32(struct ath10k *ar, u32 address, u32 *value)
597{
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300598 __le32 val = 0;
599 int ret;
600
601 ret = ath10k_pci_diag_read_mem(ar, address, &val, sizeof(val));
602 *value = __le32_to_cpu(val);
603
604 return ret;
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300605}
606
607static int __ath10k_pci_diag_read_hi(struct ath10k *ar, void *dest,
608 u32 src, u32 len)
609{
610 u32 host_addr, addr;
611 int ret;
612
613 host_addr = host_interest_item_address(src);
614
615 ret = ath10k_pci_diag_read32(ar, host_addr, &addr);
616 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200617 ath10k_warn(ar, "failed to get memcpy hi address for firmware address %d: %d\n",
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300618 src, ret);
619 return ret;
620 }
621
622 ret = ath10k_pci_diag_read_mem(ar, addr, dest, len);
623 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200624 ath10k_warn(ar, "failed to memcpy firmware memory from %d (%d B): %d\n",
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300625 addr, len, ret);
626 return ret;
627 }
628
629 return 0;
630}
631
632#define ath10k_pci_diag_read_hi(ar, dest, src, len) \
633 __ath10k_pci_diag_read_hi(ar, dest, HI_ITEM(src), len);
634
Kalle Valo5e3dd152013-06-12 20:52:10 +0300635static int ath10k_pci_diag_write_mem(struct ath10k *ar, u32 address,
636 const void *data, int nbytes)
637{
638 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
639 int ret = 0;
640 u32 buf;
641 unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
642 unsigned int id;
643 unsigned int flags;
Michal Kazior2aa39112013-08-27 13:08:02 +0200644 struct ath10k_ce_pipe *ce_diag;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300645 void *data_buf = NULL;
646 u32 ce_data; /* Host buffer address in CE space */
647 dma_addr_t ce_data_base = 0;
648 int i;
649
650 ce_diag = ar_pci->ce_diag;
651
652 /*
653 * Allocate a temporary bounce buffer to hold caller's data
654 * to be DMA'ed to Target. This guarantees
655 * 1) 4-byte alignment
656 * 2) Buffer in DMA-able space
657 */
658 orig_nbytes = nbytes;
Michal Kazior68c03242014-03-28 10:02:35 +0200659 data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
660 orig_nbytes,
661 &ce_data_base,
662 GFP_ATOMIC);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300663 if (!data_buf) {
664 ret = -ENOMEM;
665 goto done;
666 }
667
668 /* Copy caller's data to allocated DMA buf */
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300669 memcpy(data_buf, data, orig_nbytes);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300670
671 /*
672 * The address supplied by the caller is in the
673 * Target CPU virtual address space.
674 *
675 * In order to use this address with the diagnostic CE,
676 * convert it from
677 * Target CPU virtual address space
678 * to
679 * CE address space
680 */
Kalle Valo5e3dd152013-06-12 20:52:10 +0300681 address = TARG_CPU_SPACE_TO_CE_SPACE(ar, ar_pci->mem, address);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300682
683 remaining_bytes = orig_nbytes;
684 ce_data = ce_data_base;
685 while (remaining_bytes) {
686 /* FIXME: check cast */
687 nbytes = min_t(int, remaining_bytes, DIAG_TRANSFER_LIMIT);
688
689 /* Set up to receive directly into Target(!) address */
Michal Kazior728f95e2014-08-22 14:33:14 +0200690 ret = ath10k_ce_rx_post_buf(ce_diag, NULL, address);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300691 if (ret != 0)
692 goto done;
693
694 /*
695 * Request CE to send caller-supplied data that
696 * was copied to bounce buffer to Target(!) address.
697 */
Kalle Valocfbc06a2014-09-14 12:50:23 +0300698 ret = ath10k_ce_send(ce_diag, NULL, (u32)ce_data,
Kalle Valo5e3dd152013-06-12 20:52:10 +0300699 nbytes, 0, 0);
700 if (ret != 0)
701 goto done;
702
703 i = 0;
704 while (ath10k_ce_completed_send_next(ce_diag, NULL, &buf,
705 &completed_nbytes,
706 &id) != 0) {
707 mdelay(1);
708
709 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
710 ret = -EBUSY;
711 goto done;
712 }
713 }
714
715 if (nbytes != completed_nbytes) {
716 ret = -EIO;
717 goto done;
718 }
719
720 if (buf != ce_data) {
721 ret = -EIO;
722 goto done;
723 }
724
725 i = 0;
726 while (ath10k_ce_completed_recv_next(ce_diag, NULL, &buf,
727 &completed_nbytes,
728 &id, &flags) != 0) {
729 mdelay(1);
730
731 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
732 ret = -EBUSY;
733 goto done;
734 }
735 }
736
737 if (nbytes != completed_nbytes) {
738 ret = -EIO;
739 goto done;
740 }
741
742 if (buf != address) {
743 ret = -EIO;
744 goto done;
745 }
746
747 remaining_bytes -= nbytes;
748 address += nbytes;
749 ce_data += nbytes;
750 }
751
752done:
753 if (data_buf) {
Michal Kazior68c03242014-03-28 10:02:35 +0200754 dma_free_coherent(ar->dev, orig_nbytes, data_buf,
755 ce_data_base);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300756 }
757
758 if (ret != 0)
Michal Kazior7aa7a722014-08-25 12:09:38 +0200759 ath10k_warn(ar, "failed to write diag value at 0x%x: %d\n",
Kalle Valo50f87a62014-03-28 09:32:52 +0200760 address, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300761
762 return ret;
763}
764
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300765static int ath10k_pci_diag_write32(struct ath10k *ar, u32 address, u32 value)
766{
767 __le32 val = __cpu_to_le32(value);
768
769 return ath10k_pci_diag_write_mem(ar, address, &val, sizeof(val));
770}
771
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200772static bool ath10k_pci_is_awake(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300773{
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200774 u32 val = ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS);
775
776 return RTC_STATE_V_GET(val) == RTC_STATE_V_ON;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300777}
778
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200779static int ath10k_pci_wake_wait(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300780{
Kalle Valo5e3dd152013-06-12 20:52:10 +0300781 int tot_delay = 0;
782 int curr_delay = 5;
783
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200784 while (tot_delay < PCIE_WAKE_TIMEOUT) {
785 if (ath10k_pci_is_awake(ar))
Kalle Valo3aebe542013-09-01 10:02:07 +0300786 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300787
788 udelay(curr_delay);
789 tot_delay += curr_delay;
790
791 if (curr_delay < 50)
792 curr_delay += 5;
793 }
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200794
795 return -ETIMEDOUT;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300796}
797
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200798static int ath10k_pci_wake(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300799{
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200800 ath10k_pci_reg_write32(ar, PCIE_SOC_WAKE_ADDRESS,
801 PCIE_SOC_WAKE_V_MASK);
802 return ath10k_pci_wake_wait(ar);
803}
Kalle Valo5e3dd152013-06-12 20:52:10 +0300804
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200805static void ath10k_pci_sleep(struct ath10k *ar)
806{
807 ath10k_pci_reg_write32(ar, PCIE_SOC_WAKE_ADDRESS,
808 PCIE_SOC_WAKE_RESET);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300809}
810
Kalle Valo5e3dd152013-06-12 20:52:10 +0300811/* Called by lower (CE) layer when a send to Target completes. */
Michal Kazior5440ce22013-09-03 15:09:58 +0200812static void ath10k_pci_ce_send_done(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300813{
814 struct ath10k *ar = ce_state->ar;
815 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2f5280d2014-02-27 18:50:05 +0200816 struct ath10k_hif_cb *cb = &ar_pci->msg_callbacks_current;
Michal Kazior5440ce22013-09-03 15:09:58 +0200817 void *transfer_context;
818 u32 ce_data;
819 unsigned int nbytes;
820 unsigned int transfer_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300821
Michal Kazior5440ce22013-09-03 15:09:58 +0200822 while (ath10k_ce_completed_send_next(ce_state, &transfer_context,
823 &ce_data, &nbytes,
824 &transfer_id) == 0) {
Michal Kaziora16942e2014-02-27 18:50:04 +0200825 /* no need to call tx completion for NULL pointers */
Michal Kazior726346f2014-02-27 18:50:04 +0200826 if (transfer_context == NULL)
827 continue;
828
Michal Kazior2f5280d2014-02-27 18:50:05 +0200829 cb->tx_completion(ar, transfer_context, transfer_id);
Michal Kazior5440ce22013-09-03 15:09:58 +0200830 }
Kalle Valo5e3dd152013-06-12 20:52:10 +0300831}
832
833/* Called by lower (CE) layer when data is received from the Target. */
Michal Kazior5440ce22013-09-03 15:09:58 +0200834static void ath10k_pci_ce_recv_data(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300835{
836 struct ath10k *ar = ce_state->ar;
837 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +0200838 struct ath10k_pci_pipe *pipe_info = &ar_pci->pipe_info[ce_state->id];
Michal Kazior2f5280d2014-02-27 18:50:05 +0200839 struct ath10k_hif_cb *cb = &ar_pci->msg_callbacks_current;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300840 struct sk_buff *skb;
Michal Kazior5440ce22013-09-03 15:09:58 +0200841 void *transfer_context;
842 u32 ce_data;
Michal Kazior2f5280d2014-02-27 18:50:05 +0200843 unsigned int nbytes, max_nbytes;
Michal Kazior5440ce22013-09-03 15:09:58 +0200844 unsigned int transfer_id;
845 unsigned int flags;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300846
Michal Kazior5440ce22013-09-03 15:09:58 +0200847 while (ath10k_ce_completed_recv_next(ce_state, &transfer_context,
848 &ce_data, &nbytes, &transfer_id,
849 &flags) == 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300850 skb = transfer_context;
Michal Kazior2f5280d2014-02-27 18:50:05 +0200851 max_nbytes = skb->len + skb_tailroom(skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300852 dma_unmap_single(ar->dev, ATH10K_SKB_CB(skb)->paddr,
Michal Kazior2f5280d2014-02-27 18:50:05 +0200853 max_nbytes, DMA_FROM_DEVICE);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300854
Michal Kazior2f5280d2014-02-27 18:50:05 +0200855 if (unlikely(max_nbytes < nbytes)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200856 ath10k_warn(ar, "rxed more than expected (nbytes %d, max %d)",
Michal Kazior2f5280d2014-02-27 18:50:05 +0200857 nbytes, max_nbytes);
858 dev_kfree_skb_any(skb);
859 continue;
860 }
861
862 skb_put(skb, nbytes);
863 cb->rx_completion(ar, skb, pipe_info->pipe_num);
864 }
Michal Kaziorc29a3802014-07-21 21:03:10 +0300865
Michal Kazior728f95e2014-08-22 14:33:14 +0200866 ath10k_pci_rx_post_pipe(pipe_info);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300867}
868
Michal Kazior726346f2014-02-27 18:50:04 +0200869static int ath10k_pci_hif_tx_sg(struct ath10k *ar, u8 pipe_id,
870 struct ath10k_hif_sg_item *items, int n_items)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300871{
Kalle Valo5e3dd152013-06-12 20:52:10 +0300872 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior726346f2014-02-27 18:50:04 +0200873 struct ath10k_pci_pipe *pci_pipe = &ar_pci->pipe_info[pipe_id];
874 struct ath10k_ce_pipe *ce_pipe = pci_pipe->ce_hdl;
875 struct ath10k_ce_ring *src_ring = ce_pipe->src_ring;
Michal Kazior7147a132014-05-26 12:02:58 +0200876 unsigned int nentries_mask;
877 unsigned int sw_index;
878 unsigned int write_index;
Michal Kazior08b8aa02014-05-26 12:02:59 +0200879 int err, i = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300880
Michal Kazior726346f2014-02-27 18:50:04 +0200881 spin_lock_bh(&ar_pci->ce_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300882
Michal Kazior7147a132014-05-26 12:02:58 +0200883 nentries_mask = src_ring->nentries_mask;
884 sw_index = src_ring->sw_index;
885 write_index = src_ring->write_index;
886
Michal Kazior726346f2014-02-27 18:50:04 +0200887 if (unlikely(CE_RING_DELTA(nentries_mask,
888 write_index, sw_index - 1) < n_items)) {
889 err = -ENOBUFS;
Michal Kazior08b8aa02014-05-26 12:02:59 +0200890 goto err;
Michal Kazior726346f2014-02-27 18:50:04 +0200891 }
892
893 for (i = 0; i < n_items - 1; i++) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200894 ath10k_dbg(ar, ATH10K_DBG_PCI,
Michal Kazior726346f2014-02-27 18:50:04 +0200895 "pci tx item %d paddr 0x%08x len %d n_items %d\n",
896 i, items[i].paddr, items[i].len, n_items);
Michal Kazior7aa7a722014-08-25 12:09:38 +0200897 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci tx data: ",
Michal Kazior726346f2014-02-27 18:50:04 +0200898 items[i].vaddr, items[i].len);
899
900 err = ath10k_ce_send_nolock(ce_pipe,
901 items[i].transfer_context,
902 items[i].paddr,
903 items[i].len,
904 items[i].transfer_id,
905 CE_SEND_FLAG_GATHER);
906 if (err)
Michal Kazior08b8aa02014-05-26 12:02:59 +0200907 goto err;
Michal Kazior726346f2014-02-27 18:50:04 +0200908 }
909
910 /* `i` is equal to `n_items -1` after for() */
Kalle Valo5e3dd152013-06-12 20:52:10 +0300911
Michal Kazior7aa7a722014-08-25 12:09:38 +0200912 ath10k_dbg(ar, ATH10K_DBG_PCI,
Michal Kazior726346f2014-02-27 18:50:04 +0200913 "pci tx item %d paddr 0x%08x len %d n_items %d\n",
914 i, items[i].paddr, items[i].len, n_items);
Michal Kazior7aa7a722014-08-25 12:09:38 +0200915 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci tx data: ",
Michal Kazior726346f2014-02-27 18:50:04 +0200916 items[i].vaddr, items[i].len);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300917
Michal Kazior726346f2014-02-27 18:50:04 +0200918 err = ath10k_ce_send_nolock(ce_pipe,
919 items[i].transfer_context,
920 items[i].paddr,
921 items[i].len,
922 items[i].transfer_id,
923 0);
924 if (err)
Michal Kazior08b8aa02014-05-26 12:02:59 +0200925 goto err;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300926
Michal Kazior08b8aa02014-05-26 12:02:59 +0200927 spin_unlock_bh(&ar_pci->ce_lock);
928 return 0;
929
930err:
931 for (; i > 0; i--)
932 __ath10k_ce_send_revert(ce_pipe);
933
Michal Kazior726346f2014-02-27 18:50:04 +0200934 spin_unlock_bh(&ar_pci->ce_lock);
935 return err;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300936}
937
938static u16 ath10k_pci_hif_get_free_queue_number(struct ath10k *ar, u8 pipe)
939{
940 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo50f87a62014-03-28 09:32:52 +0200941
Michal Kazior7aa7a722014-08-25 12:09:38 +0200942 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif get free queue number\n");
Kalle Valo50f87a62014-03-28 09:32:52 +0200943
Michal Kazior3efcb3b2013-10-02 11:03:41 +0200944 return ath10k_ce_num_free_src_entries(ar_pci->pipe_info[pipe].ce_hdl);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300945}
946
Ben Greear384914b2014-08-25 08:37:32 +0300947static void ath10k_pci_dump_registers(struct ath10k *ar,
948 struct ath10k_fw_crash_data *crash_data)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300949{
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300950 __le32 reg_dump_values[REG_DUMP_COUNT_QCA988X] = {};
951 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300952
Ben Greear384914b2014-08-25 08:37:32 +0300953 lockdep_assert_held(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300954
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300955 ret = ath10k_pci_diag_read_hi(ar, &reg_dump_values[0],
956 hi_failure_state,
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300957 REG_DUMP_COUNT_QCA988X * sizeof(__le32));
Michal Kazior1d2b48d2013-11-08 08:01:34 +0100958 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200959 ath10k_err(ar, "failed to read firmware dump area: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300960 return;
961 }
962
963 BUILD_BUG_ON(REG_DUMP_COUNT_QCA988X % 4);
964
Michal Kazior7aa7a722014-08-25 12:09:38 +0200965 ath10k_err(ar, "firmware register dump:\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +0300966 for (i = 0; i < REG_DUMP_COUNT_QCA988X; i += 4)
Michal Kazior7aa7a722014-08-25 12:09:38 +0200967 ath10k_err(ar, "[%02d]: 0x%08X 0x%08X 0x%08X 0x%08X\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +0300968 i,
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300969 __le32_to_cpu(reg_dump_values[i]),
970 __le32_to_cpu(reg_dump_values[i + 1]),
971 __le32_to_cpu(reg_dump_values[i + 2]),
972 __le32_to_cpu(reg_dump_values[i + 3]));
Michal Kazioraffd3212013-07-16 09:54:35 +0200973
Michal Kazior1bbb1192014-08-25 12:13:14 +0200974 if (!crash_data)
975 return;
976
Ben Greear384914b2014-08-25 08:37:32 +0300977 for (i = 0; i < REG_DUMP_COUNT_QCA988X; i++)
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300978 crash_data->registers[i] = reg_dump_values[i];
Ben Greear384914b2014-08-25 08:37:32 +0300979}
980
Kalle Valo0e9848c2014-08-25 08:37:37 +0300981static void ath10k_pci_fw_crashed_dump(struct ath10k *ar)
Ben Greear384914b2014-08-25 08:37:32 +0300982{
983 struct ath10k_fw_crash_data *crash_data;
984 char uuid[50];
985
986 spin_lock_bh(&ar->data_lock);
987
988 crash_data = ath10k_debug_get_new_fw_crash_data(ar);
989
990 if (crash_data)
991 scnprintf(uuid, sizeof(uuid), "%pUl", &crash_data->uuid);
992 else
993 scnprintf(uuid, sizeof(uuid), "n/a");
994
Michal Kazior7aa7a722014-08-25 12:09:38 +0200995 ath10k_err(ar, "firmware crashed! (uuid %s)\n", uuid);
Kalle Valo8a0c7972014-08-25 08:37:45 +0300996 ath10k_print_driver_info(ar);
Ben Greear384914b2014-08-25 08:37:32 +0300997 ath10k_pci_dump_registers(ar, crash_data);
998
Ben Greear384914b2014-08-25 08:37:32 +0300999 spin_unlock_bh(&ar->data_lock);
Michal Kazioraffd3212013-07-16 09:54:35 +02001000
Michal Kazior5e90de82013-10-16 16:46:05 +03001001 queue_work(ar->workqueue, &ar->restart_work);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001002}
1003
1004static void ath10k_pci_hif_send_complete_check(struct ath10k *ar, u8 pipe,
1005 int force)
1006{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001007 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif send complete check\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001008
Kalle Valo5e3dd152013-06-12 20:52:10 +03001009 if (!force) {
1010 int resources;
1011 /*
1012 * Decide whether to actually poll for completions, or just
1013 * wait for a later chance.
1014 * If there seem to be plenty of resources left, then just wait
1015 * since checking involves reading a CE register, which is a
1016 * relatively expensive operation.
1017 */
1018 resources = ath10k_pci_hif_get_free_queue_number(ar, pipe);
1019
1020 /*
1021 * If at least 50% of the total resources are still available,
1022 * don't bother checking again yet.
1023 */
1024 if (resources > (host_ce_config_wlan[pipe].src_nentries >> 1))
1025 return;
1026 }
1027 ath10k_ce_per_engine_service(ar, pipe);
1028}
1029
Michal Kaziore799bbf2013-07-05 16:15:12 +03001030static void ath10k_pci_hif_set_callbacks(struct ath10k *ar,
1031 struct ath10k_hif_cb *callbacks)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001032{
1033 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1034
Michal Kazior7aa7a722014-08-25 12:09:38 +02001035 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif set callbacks\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001036
1037 memcpy(&ar_pci->msg_callbacks_current, callbacks,
1038 sizeof(ar_pci->msg_callbacks_current));
1039}
1040
Michal Kazior96a9d0d2013-11-08 08:01:25 +01001041static void ath10k_pci_kill_tasklet(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001042{
1043 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001044 int i;
1045
Kalle Valo5e3dd152013-06-12 20:52:10 +03001046 tasklet_kill(&ar_pci->intr_tq);
Michal Kazior103d4f52013-11-08 08:01:24 +01001047 tasklet_kill(&ar_pci->msi_fw_err);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001048
1049 for (i = 0; i < CE_COUNT; i++)
1050 tasklet_kill(&ar_pci->pipe_info[i].intr);
Michal Kazior728f95e2014-08-22 14:33:14 +02001051
1052 del_timer_sync(&ar_pci->rx_post_retry);
Michal Kazior96a9d0d2013-11-08 08:01:25 +01001053}
1054
Kalle Valo5e3dd152013-06-12 20:52:10 +03001055static int ath10k_pci_hif_map_service_to_pipe(struct ath10k *ar,
1056 u16 service_id, u8 *ul_pipe,
1057 u8 *dl_pipe, int *ul_is_polled,
1058 int *dl_is_polled)
1059{
Michal Kazior7c6aa252014-08-26 19:14:03 +03001060 const struct service_to_pipe *entry;
1061 bool ul_set = false, dl_set = false;
1062 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001063
Michal Kazior7aa7a722014-08-25 12:09:38 +02001064 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif map service\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001065
Kalle Valo5e3dd152013-06-12 20:52:10 +03001066 /* polling for received messages not supported */
1067 *dl_is_polled = 0;
1068
Michal Kazior7c6aa252014-08-26 19:14:03 +03001069 for (i = 0; i < ARRAY_SIZE(target_service_to_ce_map_wlan); i++) {
1070 entry = &target_service_to_ce_map_wlan[i];
Kalle Valo5e3dd152013-06-12 20:52:10 +03001071
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001072 if (__le32_to_cpu(entry->service_id) != service_id)
Michal Kazior7c6aa252014-08-26 19:14:03 +03001073 continue;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001074
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001075 switch (__le32_to_cpu(entry->pipedir)) {
Michal Kazior7c6aa252014-08-26 19:14:03 +03001076 case PIPEDIR_NONE:
1077 break;
1078 case PIPEDIR_IN:
1079 WARN_ON(dl_set);
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001080 *dl_pipe = __le32_to_cpu(entry->pipenum);
Michal Kazior7c6aa252014-08-26 19:14:03 +03001081 dl_set = true;
1082 break;
1083 case PIPEDIR_OUT:
1084 WARN_ON(ul_set);
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001085 *ul_pipe = __le32_to_cpu(entry->pipenum);
Michal Kazior7c6aa252014-08-26 19:14:03 +03001086 ul_set = true;
1087 break;
1088 case PIPEDIR_INOUT:
1089 WARN_ON(dl_set);
1090 WARN_ON(ul_set);
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001091 *dl_pipe = __le32_to_cpu(entry->pipenum);
1092 *ul_pipe = __le32_to_cpu(entry->pipenum);
Michal Kazior7c6aa252014-08-26 19:14:03 +03001093 dl_set = true;
1094 ul_set = true;
1095 break;
1096 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001097 }
Michal Kazior7c6aa252014-08-26 19:14:03 +03001098
1099 if (WARN_ON(!ul_set || !dl_set))
1100 return -ENOENT;
1101
Kalle Valo5e3dd152013-06-12 20:52:10 +03001102 *ul_is_polled =
1103 (host_ce_config_wlan[*ul_pipe].flags & CE_ATTR_DIS_INTR) != 0;
1104
Michal Kazior7c6aa252014-08-26 19:14:03 +03001105 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001106}
1107
1108static void ath10k_pci_hif_get_default_pipe(struct ath10k *ar,
Kalle Valo5b07e072014-09-14 12:50:06 +03001109 u8 *ul_pipe, u8 *dl_pipe)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001110{
1111 int ul_is_polled, dl_is_polled;
1112
Michal Kazior7aa7a722014-08-25 12:09:38 +02001113 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif get default pipe\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001114
Kalle Valo5e3dd152013-06-12 20:52:10 +03001115 (void)ath10k_pci_hif_map_service_to_pipe(ar,
1116 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1117 ul_pipe,
1118 dl_pipe,
1119 &ul_is_polled,
1120 &dl_is_polled);
1121}
1122
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001123static void ath10k_pci_irq_disable(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001124{
Kalle Valo5e3dd152013-06-12 20:52:10 +03001125 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001126 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001127
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001128 ath10k_ce_disable_interrupts(ar);
Michal Kaziore75db4e2014-08-28 22:14:16 +03001129 ath10k_pci_disable_and_clear_legacy_irq(ar);
1130 /* FIXME: How to mask all MSI interrupts? */
Kalle Valo5e3dd152013-06-12 20:52:10 +03001131
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001132 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
1133 synchronize_irq(ar_pci->pdev->irq + i);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001134}
1135
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001136static void ath10k_pci_irq_enable(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001137{
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001138 ath10k_ce_enable_interrupts(ar);
Michal Kaziore75db4e2014-08-28 22:14:16 +03001139 ath10k_pci_enable_legacy_irq(ar);
1140 /* FIXME: How to unmask all MSI interrupts? */
Kalle Valo5e3dd152013-06-12 20:52:10 +03001141}
1142
1143static int ath10k_pci_hif_start(struct ath10k *ar)
1144{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001145 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif start\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001146
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001147 ath10k_pci_irq_enable(ar);
Michal Kazior728f95e2014-08-22 14:33:14 +02001148 ath10k_pci_rx_post(ar);
Kalle Valo50f87a62014-03-28 09:32:52 +02001149
Kalle Valo5e3dd152013-06-12 20:52:10 +03001150 return 0;
1151}
1152
Michal Kazior87263e52013-08-27 13:08:01 +02001153static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe *pipe_info)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001154{
1155 struct ath10k *ar;
1156 struct ath10k_pci *ar_pci;
Michal Kazior2aa39112013-08-27 13:08:02 +02001157 struct ath10k_ce_pipe *ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001158 u32 buf_sz;
1159 struct sk_buff *netbuf;
1160 u32 ce_data;
1161
1162 buf_sz = pipe_info->buf_sz;
1163
1164 /* Unused Copy Engine */
1165 if (buf_sz == 0)
1166 return;
1167
1168 ar = pipe_info->hif_ce_state;
1169 ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001170 ce_hdl = pipe_info->ce_hdl;
1171
1172 while (ath10k_ce_revoke_recv_next(ce_hdl, (void **)&netbuf,
1173 &ce_data) == 0) {
1174 dma_unmap_single(ar->dev, ATH10K_SKB_CB(netbuf)->paddr,
1175 netbuf->len + skb_tailroom(netbuf),
1176 DMA_FROM_DEVICE);
1177 dev_kfree_skb_any(netbuf);
1178 }
1179}
1180
Michal Kazior87263e52013-08-27 13:08:01 +02001181static void ath10k_pci_tx_pipe_cleanup(struct ath10k_pci_pipe *pipe_info)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001182{
1183 struct ath10k *ar;
1184 struct ath10k_pci *ar_pci;
Michal Kazior2aa39112013-08-27 13:08:02 +02001185 struct ath10k_ce_pipe *ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001186 struct sk_buff *netbuf;
1187 u32 ce_data;
1188 unsigned int nbytes;
1189 unsigned int id;
1190 u32 buf_sz;
1191
1192 buf_sz = pipe_info->buf_sz;
1193
1194 /* Unused Copy Engine */
1195 if (buf_sz == 0)
1196 return;
1197
1198 ar = pipe_info->hif_ce_state;
1199 ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001200 ce_hdl = pipe_info->ce_hdl;
1201
1202 while (ath10k_ce_cancel_send_next(ce_hdl, (void **)&netbuf,
1203 &ce_data, &nbytes, &id) == 0) {
Michal Kaziora16942e2014-02-27 18:50:04 +02001204 /* no need to call tx completion for NULL pointers */
1205 if (!netbuf)
Michal Kazior2415fc12013-11-08 08:01:32 +01001206 continue;
Michal Kazior2415fc12013-11-08 08:01:32 +01001207
Kalle Valoe9bb0aa2013-09-08 18:36:11 +03001208 ar_pci->msg_callbacks_current.tx_completion(ar,
1209 netbuf,
1210 id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001211 }
1212}
1213
1214/*
1215 * Cleanup residual buffers for device shutdown:
1216 * buffers that were enqueued for receive
1217 * buffers that were to be sent
1218 * Note: Buffers that had completed but which were
1219 * not yet processed are on a completion queue. They
1220 * are handled when the completion thread shuts down.
1221 */
1222static void ath10k_pci_buffer_cleanup(struct ath10k *ar)
1223{
1224 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1225 int pipe_num;
1226
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001227 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Michal Kazior87263e52013-08-27 13:08:01 +02001228 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001229
1230 pipe_info = &ar_pci->pipe_info[pipe_num];
1231 ath10k_pci_rx_pipe_cleanup(pipe_info);
1232 ath10k_pci_tx_pipe_cleanup(pipe_info);
1233 }
1234}
1235
1236static void ath10k_pci_ce_deinit(struct ath10k *ar)
1237{
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001238 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001239
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001240 for (i = 0; i < CE_COUNT; i++)
1241 ath10k_ce_deinit_pipe(ar, i);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001242}
1243
Michal Kazior728f95e2014-08-22 14:33:14 +02001244static void ath10k_pci_flush(struct ath10k *ar)
1245{
1246 ath10k_pci_kill_tasklet(ar);
1247 ath10k_pci_buffer_cleanup(ar);
1248}
1249
Kalle Valo5e3dd152013-06-12 20:52:10 +03001250static void ath10k_pci_hif_stop(struct ath10k *ar)
1251{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001252 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif stop\n");
Michal Kazior32270b62013-08-02 09:15:47 +02001253
Michal Kazior10d23db2014-08-22 14:33:15 +02001254 /* Most likely the device has HTT Rx ring configured. The only way to
1255 * prevent the device from accessing (and possible corrupting) host
1256 * memory is to reset the chip now.
Michal Kaziore75db4e2014-08-28 22:14:16 +03001257 *
1258 * There's also no known way of masking MSI interrupts on the device.
1259 * For ranged MSI the CE-related interrupts can be masked. However
1260 * regardless how many MSI interrupts are assigned the first one
1261 * is always used for firmware indications (crashes) and cannot be
1262 * masked. To prevent the device from asserting the interrupt reset it
1263 * before proceeding with cleanup.
Michal Kazior10d23db2014-08-22 14:33:15 +02001264 */
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001265 ath10k_pci_warm_reset(ar);
Michal Kaziore75db4e2014-08-28 22:14:16 +03001266
1267 ath10k_pci_irq_disable(ar);
1268 ath10k_pci_flush(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001269}
1270
1271static int ath10k_pci_hif_exchange_bmi_msg(struct ath10k *ar,
1272 void *req, u32 req_len,
1273 void *resp, u32 *resp_len)
1274{
1275 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2aa39112013-08-27 13:08:02 +02001276 struct ath10k_pci_pipe *pci_tx = &ar_pci->pipe_info[BMI_CE_NUM_TO_TARG];
1277 struct ath10k_pci_pipe *pci_rx = &ar_pci->pipe_info[BMI_CE_NUM_TO_HOST];
1278 struct ath10k_ce_pipe *ce_tx = pci_tx->ce_hdl;
1279 struct ath10k_ce_pipe *ce_rx = pci_rx->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001280 dma_addr_t req_paddr = 0;
1281 dma_addr_t resp_paddr = 0;
1282 struct bmi_xfer xfer = {};
1283 void *treq, *tresp = NULL;
1284 int ret = 0;
1285
Michal Kazior85622cd2013-11-25 14:06:22 +01001286 might_sleep();
1287
Kalle Valo5e3dd152013-06-12 20:52:10 +03001288 if (resp && !resp_len)
1289 return -EINVAL;
1290
1291 if (resp && resp_len && *resp_len == 0)
1292 return -EINVAL;
1293
1294 treq = kmemdup(req, req_len, GFP_KERNEL);
1295 if (!treq)
1296 return -ENOMEM;
1297
1298 req_paddr = dma_map_single(ar->dev, treq, req_len, DMA_TO_DEVICE);
1299 ret = dma_mapping_error(ar->dev, req_paddr);
1300 if (ret)
1301 goto err_dma;
1302
1303 if (resp && resp_len) {
1304 tresp = kzalloc(*resp_len, GFP_KERNEL);
1305 if (!tresp) {
1306 ret = -ENOMEM;
1307 goto err_req;
1308 }
1309
1310 resp_paddr = dma_map_single(ar->dev, tresp, *resp_len,
1311 DMA_FROM_DEVICE);
1312 ret = dma_mapping_error(ar->dev, resp_paddr);
1313 if (ret)
1314 goto err_req;
1315
1316 xfer.wait_for_resp = true;
1317 xfer.resp_len = 0;
1318
Michal Kazior728f95e2014-08-22 14:33:14 +02001319 ath10k_ce_rx_post_buf(ce_rx, &xfer, resp_paddr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001320 }
1321
Kalle Valo5e3dd152013-06-12 20:52:10 +03001322 ret = ath10k_ce_send(ce_tx, &xfer, req_paddr, req_len, -1, 0);
1323 if (ret)
1324 goto err_resp;
1325
Michal Kazior85622cd2013-11-25 14:06:22 +01001326 ret = ath10k_pci_bmi_wait(ce_tx, ce_rx, &xfer);
1327 if (ret) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001328 u32 unused_buffer;
1329 unsigned int unused_nbytes;
1330 unsigned int unused_id;
1331
Kalle Valo5e3dd152013-06-12 20:52:10 +03001332 ath10k_ce_cancel_send_next(ce_tx, NULL, &unused_buffer,
1333 &unused_nbytes, &unused_id);
1334 } else {
1335 /* non-zero means we did not time out */
1336 ret = 0;
1337 }
1338
1339err_resp:
1340 if (resp) {
1341 u32 unused_buffer;
1342
1343 ath10k_ce_revoke_recv_next(ce_rx, NULL, &unused_buffer);
1344 dma_unmap_single(ar->dev, resp_paddr,
1345 *resp_len, DMA_FROM_DEVICE);
1346 }
1347err_req:
1348 dma_unmap_single(ar->dev, req_paddr, req_len, DMA_TO_DEVICE);
1349
1350 if (ret == 0 && resp_len) {
1351 *resp_len = min(*resp_len, xfer.resp_len);
1352 memcpy(resp, tresp, xfer.resp_len);
1353 }
1354err_dma:
1355 kfree(treq);
1356 kfree(tresp);
1357
1358 return ret;
1359}
1360
Michal Kazior5440ce22013-09-03 15:09:58 +02001361static void ath10k_pci_bmi_send_done(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001362{
Michal Kazior5440ce22013-09-03 15:09:58 +02001363 struct bmi_xfer *xfer;
1364 u32 ce_data;
1365 unsigned int nbytes;
1366 unsigned int transfer_id;
1367
1368 if (ath10k_ce_completed_send_next(ce_state, (void **)&xfer, &ce_data,
1369 &nbytes, &transfer_id))
1370 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001371
Michal Kazior2374b182014-07-14 16:25:25 +03001372 xfer->tx_done = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001373}
1374
Michal Kazior5440ce22013-09-03 15:09:58 +02001375static void ath10k_pci_bmi_recv_data(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001376{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001377 struct ath10k *ar = ce_state->ar;
Michal Kazior5440ce22013-09-03 15:09:58 +02001378 struct bmi_xfer *xfer;
1379 u32 ce_data;
1380 unsigned int nbytes;
1381 unsigned int transfer_id;
1382 unsigned int flags;
1383
1384 if (ath10k_ce_completed_recv_next(ce_state, (void **)&xfer, &ce_data,
1385 &nbytes, &transfer_id, &flags))
1386 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001387
1388 if (!xfer->wait_for_resp) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001389 ath10k_warn(ar, "unexpected: BMI data received; ignoring\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001390 return;
1391 }
1392
1393 xfer->resp_len = nbytes;
Michal Kazior2374b182014-07-14 16:25:25 +03001394 xfer->rx_done = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001395}
1396
Michal Kazior85622cd2013-11-25 14:06:22 +01001397static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
1398 struct ath10k_ce_pipe *rx_pipe,
1399 struct bmi_xfer *xfer)
1400{
1401 unsigned long timeout = jiffies + BMI_COMMUNICATION_TIMEOUT_HZ;
1402
1403 while (time_before_eq(jiffies, timeout)) {
1404 ath10k_pci_bmi_send_done(tx_pipe);
1405 ath10k_pci_bmi_recv_data(rx_pipe);
1406
Michal Kazior2374b182014-07-14 16:25:25 +03001407 if (xfer->tx_done && (xfer->rx_done == xfer->wait_for_resp))
Michal Kazior85622cd2013-11-25 14:06:22 +01001408 return 0;
1409
1410 schedule();
1411 }
1412
1413 return -ETIMEDOUT;
1414}
1415
Kalle Valo5e3dd152013-06-12 20:52:10 +03001416/*
Kalle Valo5e3dd152013-06-12 20:52:10 +03001417 * Send an interrupt to the device to wake up the Target CPU
1418 * so it has an opportunity to notice any changed state.
1419 */
1420static int ath10k_pci_wake_target_cpu(struct ath10k *ar)
1421{
Michal Kazior9e264942014-09-02 11:00:21 +03001422 u32 addr, val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001423
Michal Kazior9e264942014-09-02 11:00:21 +03001424 addr = SOC_CORE_BASE_ADDRESS | CORE_CTRL_ADDRESS;
1425 val = ath10k_pci_read32(ar, addr);
1426 val |= CORE_CTRL_CPU_INTR_MASK;
1427 ath10k_pci_write32(ar, addr, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001428
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001429 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001430}
1431
1432static int ath10k_pci_init_config(struct ath10k *ar)
1433{
1434 u32 interconnect_targ_addr;
1435 u32 pcie_state_targ_addr = 0;
1436 u32 pipe_cfg_targ_addr = 0;
1437 u32 svc_to_pipe_map = 0;
1438 u32 pcie_config_flags = 0;
1439 u32 ealloc_value;
1440 u32 ealloc_targ_addr;
1441 u32 flag2_value;
1442 u32 flag2_targ_addr;
1443 int ret = 0;
1444
1445 /* Download to Target the CE Config and the service-to-CE map */
1446 interconnect_targ_addr =
1447 host_interest_item_address(HI_ITEM(hi_interconnect_state));
1448
1449 /* Supply Target-side CE configuration */
Michal Kazior9e264942014-09-02 11:00:21 +03001450 ret = ath10k_pci_diag_read32(ar, interconnect_targ_addr,
1451 &pcie_state_targ_addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001452 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001453 ath10k_err(ar, "Failed to get pcie state addr: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001454 return ret;
1455 }
1456
1457 if (pcie_state_targ_addr == 0) {
1458 ret = -EIO;
Michal Kazior7aa7a722014-08-25 12:09:38 +02001459 ath10k_err(ar, "Invalid pcie state addr\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001460 return ret;
1461 }
1462
Michal Kazior9e264942014-09-02 11:00:21 +03001463 ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
Kalle Valo5e3dd152013-06-12 20:52:10 +03001464 offsetof(struct pcie_state,
Michal Kazior9e264942014-09-02 11:00:21 +03001465 pipe_cfg_addr)),
1466 &pipe_cfg_targ_addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001467 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001468 ath10k_err(ar, "Failed to get pipe cfg addr: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001469 return ret;
1470 }
1471
1472 if (pipe_cfg_targ_addr == 0) {
1473 ret = -EIO;
Michal Kazior7aa7a722014-08-25 12:09:38 +02001474 ath10k_err(ar, "Invalid pipe cfg addr\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001475 return ret;
1476 }
1477
1478 ret = ath10k_pci_diag_write_mem(ar, pipe_cfg_targ_addr,
Kalle Valo5b07e072014-09-14 12:50:06 +03001479 target_ce_config_wlan,
1480 sizeof(target_ce_config_wlan));
Kalle Valo5e3dd152013-06-12 20:52:10 +03001481
1482 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001483 ath10k_err(ar, "Failed to write pipe cfg: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001484 return ret;
1485 }
1486
Michal Kazior9e264942014-09-02 11:00:21 +03001487 ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
Kalle Valo5e3dd152013-06-12 20:52:10 +03001488 offsetof(struct pcie_state,
Michal Kazior9e264942014-09-02 11:00:21 +03001489 svc_to_pipe_map)),
1490 &svc_to_pipe_map);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001491 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001492 ath10k_err(ar, "Failed to get svc/pipe map: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001493 return ret;
1494 }
1495
1496 if (svc_to_pipe_map == 0) {
1497 ret = -EIO;
Michal Kazior7aa7a722014-08-25 12:09:38 +02001498 ath10k_err(ar, "Invalid svc_to_pipe map\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001499 return ret;
1500 }
1501
1502 ret = ath10k_pci_diag_write_mem(ar, svc_to_pipe_map,
Kalle Valo5b07e072014-09-14 12:50:06 +03001503 target_service_to_ce_map_wlan,
1504 sizeof(target_service_to_ce_map_wlan));
Kalle Valo5e3dd152013-06-12 20:52:10 +03001505 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001506 ath10k_err(ar, "Failed to write svc/pipe map: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001507 return ret;
1508 }
1509
Michal Kazior9e264942014-09-02 11:00:21 +03001510 ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
Kalle Valo5e3dd152013-06-12 20:52:10 +03001511 offsetof(struct pcie_state,
Michal Kazior9e264942014-09-02 11:00:21 +03001512 config_flags)),
1513 &pcie_config_flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001514 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001515 ath10k_err(ar, "Failed to get pcie config_flags: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001516 return ret;
1517 }
1518
1519 pcie_config_flags &= ~PCIE_CONFIG_FLAG_ENABLE_L1;
1520
Michal Kazior9e264942014-09-02 11:00:21 +03001521 ret = ath10k_pci_diag_write32(ar, (pcie_state_targ_addr +
1522 offsetof(struct pcie_state,
1523 config_flags)),
1524 pcie_config_flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001525 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001526 ath10k_err(ar, "Failed to write pcie config_flags: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001527 return ret;
1528 }
1529
1530 /* configure early allocation */
1531 ealloc_targ_addr = host_interest_item_address(HI_ITEM(hi_early_alloc));
1532
Michal Kazior9e264942014-09-02 11:00:21 +03001533 ret = ath10k_pci_diag_read32(ar, ealloc_targ_addr, &ealloc_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001534 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001535 ath10k_err(ar, "Faile to get early alloc val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001536 return ret;
1537 }
1538
1539 /* first bank is switched to IRAM */
1540 ealloc_value |= ((HI_EARLY_ALLOC_MAGIC << HI_EARLY_ALLOC_MAGIC_SHIFT) &
1541 HI_EARLY_ALLOC_MAGIC_MASK);
1542 ealloc_value |= ((1 << HI_EARLY_ALLOC_IRAM_BANKS_SHIFT) &
1543 HI_EARLY_ALLOC_IRAM_BANKS_MASK);
1544
Michal Kazior9e264942014-09-02 11:00:21 +03001545 ret = ath10k_pci_diag_write32(ar, ealloc_targ_addr, ealloc_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001546 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001547 ath10k_err(ar, "Failed to set early alloc val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001548 return ret;
1549 }
1550
1551 /* Tell Target to proceed with initialization */
1552 flag2_targ_addr = host_interest_item_address(HI_ITEM(hi_option_flag2));
1553
Michal Kazior9e264942014-09-02 11:00:21 +03001554 ret = ath10k_pci_diag_read32(ar, flag2_targ_addr, &flag2_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001555 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001556 ath10k_err(ar, "Failed to get option val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001557 return ret;
1558 }
1559
1560 flag2_value |= HI_OPTION_EARLY_CFG_DONE;
1561
Michal Kazior9e264942014-09-02 11:00:21 +03001562 ret = ath10k_pci_diag_write32(ar, flag2_targ_addr, flag2_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001563 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001564 ath10k_err(ar, "Failed to set option val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001565 return ret;
1566 }
1567
1568 return 0;
1569}
1570
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001571static int ath10k_pci_alloc_ce(struct ath10k *ar)
1572{
1573 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001574
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001575 for (i = 0; i < CE_COUNT; i++) {
1576 ret = ath10k_ce_alloc_pipe(ar, i, &host_ce_config_wlan[i]);
1577 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001578 ath10k_err(ar, "failed to allocate copy engine pipe %d: %d\n",
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001579 i, ret);
1580 return ret;
1581 }
1582 }
1583
1584 return 0;
1585}
1586
1587static void ath10k_pci_free_ce(struct ath10k *ar)
1588{
1589 int i;
1590
1591 for (i = 0; i < CE_COUNT; i++)
1592 ath10k_ce_free_pipe(ar, i);
1593}
Kalle Valo5e3dd152013-06-12 20:52:10 +03001594
1595static int ath10k_pci_ce_init(struct ath10k *ar)
1596{
1597 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +02001598 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001599 const struct ce_attr *attr;
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001600 int pipe_num, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001601
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001602 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001603 pipe_info = &ar_pci->pipe_info[pipe_num];
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001604 pipe_info->ce_hdl = &ar_pci->ce_states[pipe_num];
Kalle Valo5e3dd152013-06-12 20:52:10 +03001605 pipe_info->pipe_num = pipe_num;
1606 pipe_info->hif_ce_state = ar;
1607 attr = &host_ce_config_wlan[pipe_num];
1608
Michal Kazior145cc122014-08-22 14:23:32 +02001609 ret = ath10k_ce_init_pipe(ar, pipe_num, attr,
1610 ath10k_pci_ce_send_done,
1611 ath10k_pci_ce_recv_data);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001612 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001613 ath10k_err(ar, "failed to initialize copy engine pipe %d: %d\n",
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001614 pipe_num, ret);
1615 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001616 }
1617
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001618 if (pipe_num == CE_COUNT - 1) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001619 /*
1620 * Reserve the ultimate CE for
1621 * diagnostic Window support
1622 */
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001623 ar_pci->ce_diag = pipe_info->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001624 continue;
1625 }
1626
Kalle Valocfbc06a2014-09-14 12:50:23 +03001627 pipe_info->buf_sz = (size_t)(attr->src_sz_max);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001628 }
1629
Kalle Valo5e3dd152013-06-12 20:52:10 +03001630 return 0;
1631}
1632
Michal Kazior5c771e72014-08-22 14:23:34 +02001633static bool ath10k_pci_has_fw_crashed(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001634{
Michal Kazior5c771e72014-08-22 14:23:34 +02001635 return ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS) &
1636 FW_IND_EVENT_PENDING;
1637}
Kalle Valo5e3dd152013-06-12 20:52:10 +03001638
Michal Kazior5c771e72014-08-22 14:23:34 +02001639static void ath10k_pci_fw_crashed_clear(struct ath10k *ar)
1640{
1641 u32 val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001642
Michal Kazior5c771e72014-08-22 14:23:34 +02001643 val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
1644 val &= ~FW_IND_EVENT_PENDING;
1645 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001646}
1647
Michal Kaziorde013572014-05-14 16:56:16 +03001648/* this function effectively clears target memory controller assert line */
1649static void ath10k_pci_warm_reset_si0(struct ath10k *ar)
1650{
1651 u32 val;
1652
1653 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1654 ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
1655 val | SOC_RESET_CONTROL_SI0_RST_MASK);
1656 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1657
1658 msleep(10);
1659
1660 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1661 ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
1662 val & ~SOC_RESET_CONTROL_SI0_RST_MASK);
1663 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1664
1665 msleep(10);
1666}
1667
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001668static int ath10k_pci_warm_reset(struct ath10k *ar)
1669{
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001670 u32 val;
1671
Michal Kazior7aa7a722014-08-25 12:09:38 +02001672 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot warm reset\n");
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001673
1674 /* debug */
1675 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1676 PCIE_INTR_CAUSE_ADDRESS);
Michal Kazior7aa7a722014-08-25 12:09:38 +02001677 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot host cpu intr cause: 0x%08x\n",
1678 val);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001679
1680 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1681 CPU_INTR_ADDRESS);
Michal Kazior7aa7a722014-08-25 12:09:38 +02001682 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target cpu intr cause: 0x%08x\n",
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001683 val);
1684
1685 /* disable pending irqs */
1686 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
1687 PCIE_INTR_ENABLE_ADDRESS, 0);
1688
1689 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
1690 PCIE_INTR_CLR_ADDRESS, ~0);
1691
1692 msleep(100);
1693
1694 /* clear fw indicator */
Kalle Valob39712c2014-03-28 09:32:46 +02001695 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, 0);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001696
1697 /* clear target LF timer interrupts */
1698 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1699 SOC_LF_TIMER_CONTROL0_ADDRESS);
1700 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS +
1701 SOC_LF_TIMER_CONTROL0_ADDRESS,
1702 val & ~SOC_LF_TIMER_CONTROL0_ENABLE_MASK);
1703
1704 /* reset CE */
1705 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1706 SOC_RESET_CONTROL_ADDRESS);
1707 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1708 val | SOC_RESET_CONTROL_CE_RST_MASK);
1709 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1710 SOC_RESET_CONTROL_ADDRESS);
1711 msleep(10);
1712
1713 /* unreset CE */
1714 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1715 val & ~SOC_RESET_CONTROL_CE_RST_MASK);
1716 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1717 SOC_RESET_CONTROL_ADDRESS);
1718 msleep(10);
1719
Michal Kaziorde013572014-05-14 16:56:16 +03001720 ath10k_pci_warm_reset_si0(ar);
1721
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001722 /* debug */
1723 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1724 PCIE_INTR_CAUSE_ADDRESS);
Michal Kazior7aa7a722014-08-25 12:09:38 +02001725 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot host cpu intr cause: 0x%08x\n",
1726 val);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001727
1728 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1729 CPU_INTR_ADDRESS);
Michal Kazior7aa7a722014-08-25 12:09:38 +02001730 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target cpu intr cause: 0x%08x\n",
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001731 val);
1732
1733 /* CPU warm reset */
1734 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1735 SOC_RESET_CONTROL_ADDRESS);
1736 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1737 val | SOC_RESET_CONTROL_CPU_WARM_RST_MASK);
1738
1739 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1740 SOC_RESET_CONTROL_ADDRESS);
Michal Kazior7aa7a722014-08-25 12:09:38 +02001741 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target reset state: 0x%08x\n",
1742 val);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001743
1744 msleep(100);
1745
Michal Kazior7aa7a722014-08-25 12:09:38 +02001746 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot warm reset complete\n");
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001747
Michal Kaziorc0c378f2014-08-07 11:03:28 +02001748 return 0;
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001749}
1750
1751static int __ath10k_pci_hif_power_up(struct ath10k *ar, bool cold_reset)
Michal Kazior8c5c5362013-07-16 09:38:50 +02001752{
1753 int ret;
1754
1755 /*
1756 * Bring the target up cleanly.
1757 *
1758 * The target may be in an undefined state with an AUX-powered Target
1759 * and a Host in WoW mode. If the Host crashes, loses power, or is
1760 * restarted (without unloading the driver) then the Target is left
1761 * (aux) powered and running. On a subsequent driver load, the Target
1762 * is in an unexpected state. We try to catch that here in order to
1763 * reset the Target and retry the probe.
1764 */
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001765 if (cold_reset)
1766 ret = ath10k_pci_cold_reset(ar);
1767 else
1768 ret = ath10k_pci_warm_reset(ar);
1769
Michal Kazior5b2589f2013-11-08 08:01:30 +01001770 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001771 ath10k_err(ar, "failed to reset target: %d\n", ret);
Michal Kazior98563d52013-11-08 08:01:33 +01001772 goto err;
Michal Kazior5b2589f2013-11-08 08:01:30 +01001773 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02001774
Michal Kazior8c5c5362013-07-16 09:38:50 +02001775 ret = ath10k_pci_ce_init(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001776 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001777 ath10k_err(ar, "failed to initialize CE: %d\n", ret);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02001778 goto err;
Michal Kaziorab977bd2013-11-25 14:06:26 +01001779 }
1780
Michal Kazior98563d52013-11-08 08:01:33 +01001781 ret = ath10k_pci_wait_for_target_init(ar);
1782 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001783 ath10k_err(ar, "failed to wait for target to init: %d\n", ret);
Michal Kazior5c771e72014-08-22 14:23:34 +02001784 goto err_ce;
Michal Kazior98563d52013-11-08 08:01:33 +01001785 }
1786
1787 ret = ath10k_pci_init_config(ar);
1788 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001789 ath10k_err(ar, "failed to setup init config: %d\n", ret);
Michal Kazior5c771e72014-08-22 14:23:34 +02001790 goto err_ce;
Michal Kazior98563d52013-11-08 08:01:33 +01001791 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02001792
1793 ret = ath10k_pci_wake_target_cpu(ar);
1794 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001795 ath10k_err(ar, "could not wake up target CPU: %d\n", ret);
Michal Kazior5c771e72014-08-22 14:23:34 +02001796 goto err_ce;
Michal Kazior8c5c5362013-07-16 09:38:50 +02001797 }
1798
1799 return 0;
1800
1801err_ce:
1802 ath10k_pci_ce_deinit(ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001803 ath10k_pci_warm_reset(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001804err:
1805 return ret;
1806}
1807
Michal Kazior61c95ce2014-05-14 16:56:16 +03001808static int ath10k_pci_hif_power_up_warm(struct ath10k *ar)
1809{
1810 int i, ret;
1811
1812 /*
1813 * Sometime warm reset succeeds after retries.
1814 *
1815 * FIXME: It might be possible to tune ath10k_pci_warm_reset() to work
1816 * at first try.
1817 */
1818 for (i = 0; i < ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS; i++) {
1819 ret = __ath10k_pci_hif_power_up(ar, false);
1820 if (ret == 0)
1821 break;
1822
Michal Kazior7aa7a722014-08-25 12:09:38 +02001823 ath10k_warn(ar, "failed to warm reset (attempt %d out of %d): %d\n",
Michal Kazior61c95ce2014-05-14 16:56:16 +03001824 i + 1, ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS, ret);
1825 }
1826
1827 return ret;
1828}
1829
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001830static int ath10k_pci_hif_power_up(struct ath10k *ar)
1831{
1832 int ret;
1833
Michal Kazior7aa7a722014-08-25 12:09:38 +02001834 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif power up\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001835
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001836 /*
1837 * Hardware CUS232 version 2 has some issues with cold reset and the
1838 * preferred (and safer) way to perform a device reset is through a
1839 * warm reset.
1840 *
Michal Kazior61c95ce2014-05-14 16:56:16 +03001841 * Warm reset doesn't always work though so fall back to cold reset may
1842 * be necessary.
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001843 */
Michal Kazior61c95ce2014-05-14 16:56:16 +03001844 ret = ath10k_pci_hif_power_up_warm(ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001845 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001846 ath10k_warn(ar, "failed to power up target using warm reset: %d\n",
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001847 ret);
1848
Kalle Valo35098462014-03-28 09:32:27 +02001849 if (ath10k_pci_reset_mode == ATH10K_PCI_RESET_WARM_ONLY)
1850 return ret;
1851
Michal Kazior7aa7a722014-08-25 12:09:38 +02001852 ath10k_warn(ar, "trying cold reset\n");
Kalle Valo35098462014-03-28 09:32:27 +02001853
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001854 ret = __ath10k_pci_hif_power_up(ar, true);
1855 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001856 ath10k_err(ar, "failed to power up target using cold reset too (%d)\n",
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001857 ret);
1858 return ret;
1859 }
1860 }
1861
1862 return 0;
1863}
1864
Michal Kazior8c5c5362013-07-16 09:38:50 +02001865static void ath10k_pci_hif_power_down(struct ath10k *ar)
1866{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001867 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif power down\n");
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02001868
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001869 ath10k_pci_warm_reset(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001870}
1871
Michal Kazior8cd13ca2013-07-16 09:38:54 +02001872#ifdef CONFIG_PM
1873
1874#define ATH10K_PCI_PM_CONTROL 0x44
1875
1876static int ath10k_pci_hif_suspend(struct ath10k *ar)
1877{
1878 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1879 struct pci_dev *pdev = ar_pci->pdev;
1880 u32 val;
1881
1882 pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
1883
1884 if ((val & 0x000000ff) != 0x3) {
1885 pci_save_state(pdev);
1886 pci_disable_device(pdev);
1887 pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
1888 (val & 0xffffff00) | 0x03);
1889 }
1890
1891 return 0;
1892}
1893
1894static int ath10k_pci_hif_resume(struct ath10k *ar)
1895{
1896 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1897 struct pci_dev *pdev = ar_pci->pdev;
1898 u32 val;
1899
1900 pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
1901
1902 if ((val & 0x000000ff) != 0) {
1903 pci_restore_state(pdev);
1904 pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
1905 val & 0xffffff00);
1906 /*
1907 * Suspend/Resume resets the PCI configuration space,
1908 * so we have to re-disable the RETRY_TIMEOUT register (0x41)
1909 * to keep PCI Tx retries from interfering with C3 CPU state
1910 */
1911 pci_read_config_dword(pdev, 0x40, &val);
1912
1913 if ((val & 0x0000ff00) != 0)
1914 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
1915 }
1916
1917 return 0;
1918}
1919#endif
1920
Kalle Valo5e3dd152013-06-12 20:52:10 +03001921static const struct ath10k_hif_ops ath10k_pci_hif_ops = {
Michal Kazior726346f2014-02-27 18:50:04 +02001922 .tx_sg = ath10k_pci_hif_tx_sg,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001923 .exchange_bmi_msg = ath10k_pci_hif_exchange_bmi_msg,
1924 .start = ath10k_pci_hif_start,
1925 .stop = ath10k_pci_hif_stop,
1926 .map_service_to_pipe = ath10k_pci_hif_map_service_to_pipe,
1927 .get_default_pipe = ath10k_pci_hif_get_default_pipe,
1928 .send_complete_check = ath10k_pci_hif_send_complete_check,
Michal Kaziore799bbf2013-07-05 16:15:12 +03001929 .set_callbacks = ath10k_pci_hif_set_callbacks,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001930 .get_free_queue_number = ath10k_pci_hif_get_free_queue_number,
Michal Kazior8c5c5362013-07-16 09:38:50 +02001931 .power_up = ath10k_pci_hif_power_up,
1932 .power_down = ath10k_pci_hif_power_down,
Michal Kazior8cd13ca2013-07-16 09:38:54 +02001933#ifdef CONFIG_PM
1934 .suspend = ath10k_pci_hif_suspend,
1935 .resume = ath10k_pci_hif_resume,
1936#endif
Kalle Valo5e3dd152013-06-12 20:52:10 +03001937};
1938
1939static void ath10k_pci_ce_tasklet(unsigned long ptr)
1940{
Michal Kazior87263e52013-08-27 13:08:01 +02001941 struct ath10k_pci_pipe *pipe = (struct ath10k_pci_pipe *)ptr;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001942 struct ath10k_pci *ar_pci = pipe->ar_pci;
1943
1944 ath10k_ce_per_engine_service(ar_pci->ar, pipe->pipe_num);
1945}
1946
1947static void ath10k_msi_err_tasklet(unsigned long data)
1948{
1949 struct ath10k *ar = (struct ath10k *)data;
1950
Michal Kazior5c771e72014-08-22 14:23:34 +02001951 if (!ath10k_pci_has_fw_crashed(ar)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001952 ath10k_warn(ar, "received unsolicited fw crash interrupt\n");
Michal Kazior5c771e72014-08-22 14:23:34 +02001953 return;
1954 }
1955
1956 ath10k_pci_fw_crashed_clear(ar);
1957 ath10k_pci_fw_crashed_dump(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001958}
1959
1960/*
1961 * Handler for a per-engine interrupt on a PARTICULAR CE.
1962 * This is used in cases where each CE has a private MSI interrupt.
1963 */
1964static irqreturn_t ath10k_pci_per_engine_handler(int irq, void *arg)
1965{
1966 struct ath10k *ar = arg;
1967 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1968 int ce_id = irq - ar_pci->pdev->irq - MSI_ASSIGN_CE_INITIAL;
1969
Dan Carpentere5742672013-06-18 10:28:46 +03001970 if (ce_id < 0 || ce_id >= ARRAY_SIZE(ar_pci->pipe_info)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001971 ath10k_warn(ar, "unexpected/invalid irq %d ce_id %d\n", irq,
1972 ce_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001973 return IRQ_HANDLED;
1974 }
1975
1976 /*
1977 * NOTE: We are able to derive ce_id from irq because we
1978 * use a one-to-one mapping for CE's 0..5.
1979 * CE's 6 & 7 do not use interrupts at all.
1980 *
1981 * This mapping must be kept in sync with the mapping
1982 * used by firmware.
1983 */
1984 tasklet_schedule(&ar_pci->pipe_info[ce_id].intr);
1985 return IRQ_HANDLED;
1986}
1987
1988static irqreturn_t ath10k_pci_msi_fw_handler(int irq, void *arg)
1989{
1990 struct ath10k *ar = arg;
1991 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1992
1993 tasklet_schedule(&ar_pci->msi_fw_err);
1994 return IRQ_HANDLED;
1995}
1996
1997/*
1998 * Top-level interrupt handler for all PCI interrupts from a Target.
1999 * When a block of MSI interrupts is allocated, this top-level handler
2000 * is not used; instead, we directly call the correct sub-handler.
2001 */
2002static irqreturn_t ath10k_pci_interrupt_handler(int irq, void *arg)
2003{
2004 struct ath10k *ar = arg;
2005 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2006
2007 if (ar_pci->num_msi_intrs == 0) {
Michal Kaziore5398872013-11-25 14:06:20 +01002008 if (!ath10k_pci_irq_pending(ar))
2009 return IRQ_NONE;
2010
Michal Kazior26852182013-11-25 14:06:25 +01002011 ath10k_pci_disable_and_clear_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002012 }
2013
2014 tasklet_schedule(&ar_pci->intr_tq);
2015
2016 return IRQ_HANDLED;
2017}
2018
2019static void ath10k_pci_tasklet(unsigned long data)
2020{
2021 struct ath10k *ar = (struct ath10k *)data;
2022 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2023
Michal Kazior5c771e72014-08-22 14:23:34 +02002024 if (ath10k_pci_has_fw_crashed(ar)) {
2025 ath10k_pci_fw_crashed_clear(ar);
2026 ath10k_pci_fw_crashed_dump(ar);
2027 return;
2028 }
2029
Kalle Valo5e3dd152013-06-12 20:52:10 +03002030 ath10k_ce_per_engine_service_any(ar);
2031
Michal Kazior26852182013-11-25 14:06:25 +01002032 /* Re-enable legacy irq that was disabled in the irq handler */
2033 if (ar_pci->num_msi_intrs == 0)
2034 ath10k_pci_enable_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002035}
2036
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002037static int ath10k_pci_request_irq_msix(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002038{
2039 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002040 int ret, i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002041
2042 ret = request_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW,
2043 ath10k_pci_msi_fw_handler,
2044 IRQF_SHARED, "ath10k_pci", ar);
Michal Kazior591ecdb2013-07-31 10:55:15 +02002045 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002046 ath10k_warn(ar, "failed to request MSI-X fw irq %d: %d\n",
Michal Kazior591ecdb2013-07-31 10:55:15 +02002047 ar_pci->pdev->irq + MSI_ASSIGN_FW, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002048 return ret;
Michal Kazior591ecdb2013-07-31 10:55:15 +02002049 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002050
2051 for (i = MSI_ASSIGN_CE_INITIAL; i <= MSI_ASSIGN_CE_MAX; i++) {
2052 ret = request_irq(ar_pci->pdev->irq + i,
2053 ath10k_pci_per_engine_handler,
2054 IRQF_SHARED, "ath10k_pci", ar);
2055 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002056 ath10k_warn(ar, "failed to request MSI-X ce irq %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002057 ar_pci->pdev->irq + i, ret);
2058
Michal Kazior87b14232013-06-26 08:50:50 +02002059 for (i--; i >= MSI_ASSIGN_CE_INITIAL; i--)
2060 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002061
Michal Kazior87b14232013-06-26 08:50:50 +02002062 free_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002063 return ret;
2064 }
2065 }
2066
Kalle Valo5e3dd152013-06-12 20:52:10 +03002067 return 0;
2068}
2069
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002070static int ath10k_pci_request_irq_msi(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002071{
2072 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2073 int ret;
2074
2075 ret = request_irq(ar_pci->pdev->irq,
2076 ath10k_pci_interrupt_handler,
2077 IRQF_SHARED, "ath10k_pci", ar);
Kalle Valof3782742013-10-17 11:36:15 +03002078 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002079 ath10k_warn(ar, "failed to request MSI irq %d: %d\n",
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002080 ar_pci->pdev->irq, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002081 return ret;
Kalle Valof3782742013-10-17 11:36:15 +03002082 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002083
Kalle Valo5e3dd152013-06-12 20:52:10 +03002084 return 0;
2085}
2086
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002087static int ath10k_pci_request_irq_legacy(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002088{
2089 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002090 int ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002091
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002092 ret = request_irq(ar_pci->pdev->irq,
2093 ath10k_pci_interrupt_handler,
2094 IRQF_SHARED, "ath10k_pci", ar);
Kalle Valof3782742013-10-17 11:36:15 +03002095 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002096 ath10k_warn(ar, "failed to request legacy irq %d: %d\n",
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002097 ar_pci->pdev->irq, ret);
Kalle Valof3782742013-10-17 11:36:15 +03002098 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002099 }
2100
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002101 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002102}
2103
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002104static int ath10k_pci_request_irq(struct ath10k *ar)
2105{
2106 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2107
2108 switch (ar_pci->num_msi_intrs) {
2109 case 0:
2110 return ath10k_pci_request_irq_legacy(ar);
2111 case 1:
2112 return ath10k_pci_request_irq_msi(ar);
2113 case MSI_NUM_REQUEST:
2114 return ath10k_pci_request_irq_msix(ar);
2115 }
2116
Michal Kazior7aa7a722014-08-25 12:09:38 +02002117 ath10k_warn(ar, "unknown irq configuration upon request\n");
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002118 return -EINVAL;
2119}
2120
2121static void ath10k_pci_free_irq(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002122{
2123 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2124 int i;
2125
2126 /* There's at least one interrupt irregardless whether its legacy INTR
2127 * or MSI or MSI-X */
2128 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
2129 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002130}
2131
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002132static void ath10k_pci_init_irq_tasklets(struct ath10k *ar)
2133{
2134 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2135 int i;
2136
2137 tasklet_init(&ar_pci->intr_tq, ath10k_pci_tasklet, (unsigned long)ar);
2138 tasklet_init(&ar_pci->msi_fw_err, ath10k_msi_err_tasklet,
2139 (unsigned long)ar);
2140
2141 for (i = 0; i < CE_COUNT; i++) {
2142 ar_pci->pipe_info[i].ar_pci = ar_pci;
2143 tasklet_init(&ar_pci->pipe_info[i].intr, ath10k_pci_ce_tasklet,
2144 (unsigned long)&ar_pci->pipe_info[i]);
2145 }
2146}
2147
2148static int ath10k_pci_init_irq(struct ath10k *ar)
2149{
2150 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2151 int ret;
2152
2153 ath10k_pci_init_irq_tasklets(ar);
2154
Michal Kazior403d6272014-08-22 14:23:31 +02002155 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_AUTO)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002156 ath10k_info(ar, "limiting irq mode to: %d\n",
2157 ath10k_pci_irq_mode);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002158
2159 /* Try MSI-X */
Michal Kazior0edf2572014-08-07 11:03:29 +02002160 if (ath10k_pci_irq_mode == ATH10K_PCI_IRQ_AUTO) {
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002161 ar_pci->num_msi_intrs = MSI_NUM_REQUEST;
Alexander Gordeev5ad68672014-02-13 17:50:02 +02002162 ret = pci_enable_msi_range(ar_pci->pdev, ar_pci->num_msi_intrs,
Kalle Valo5b07e072014-09-14 12:50:06 +03002163 ar_pci->num_msi_intrs);
Alexander Gordeev5ad68672014-02-13 17:50:02 +02002164 if (ret > 0)
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002165 return 0;
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002166
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002167 /* fall-through */
2168 }
2169
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002170 /* Try MSI */
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002171 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_LEGACY) {
2172 ar_pci->num_msi_intrs = 1;
2173 ret = pci_enable_msi(ar_pci->pdev);
2174 if (ret == 0)
2175 return 0;
2176
2177 /* fall-through */
2178 }
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002179
2180 /* Try legacy irq
2181 *
2182 * A potential race occurs here: The CORE_BASE write
2183 * depends on target correctly decoding AXI address but
2184 * host won't know when target writes BAR to CORE_CTRL.
2185 * This write might get lost if target has NOT written BAR.
2186 * For now, fix the race by repeating the write in below
2187 * synchronization checking. */
2188 ar_pci->num_msi_intrs = 0;
2189
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002190 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2191 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002192
2193 return 0;
2194}
2195
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002196static void ath10k_pci_deinit_irq_legacy(struct ath10k *ar)
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002197{
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002198 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2199 0);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002200}
2201
2202static int ath10k_pci_deinit_irq(struct ath10k *ar)
2203{
2204 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2205
2206 switch (ar_pci->num_msi_intrs) {
2207 case 0:
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002208 ath10k_pci_deinit_irq_legacy(ar);
2209 return 0;
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002210 case 1:
2211 /* fall-through */
2212 case MSI_NUM_REQUEST:
2213 pci_disable_msi(ar_pci->pdev);
2214 return 0;
Alexander Gordeevbb8b6212014-02-13 17:50:01 +02002215 default:
2216 pci_disable_msi(ar_pci->pdev);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002217 }
2218
Michal Kazior7aa7a722014-08-25 12:09:38 +02002219 ath10k_warn(ar, "unknown irq configuration upon deinit\n");
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002220 return -EINVAL;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002221}
2222
Michal Kaziord7fb47f2013-11-08 08:01:26 +01002223static int ath10k_pci_wait_for_target_init(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002224{
2225 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo0399eca2014-03-28 09:32:21 +02002226 unsigned long timeout;
Kalle Valo0399eca2014-03-28 09:32:21 +02002227 u32 val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002228
Michal Kazior7aa7a722014-08-25 12:09:38 +02002229 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot waiting target to initialise\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002230
Kalle Valo0399eca2014-03-28 09:32:21 +02002231 timeout = jiffies + msecs_to_jiffies(ATH10K_PCI_TARGET_WAIT);
2232
2233 do {
2234 val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
2235
Michal Kazior7aa7a722014-08-25 12:09:38 +02002236 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target indicator %x\n",
2237 val);
Kalle Valo50f87a62014-03-28 09:32:52 +02002238
Kalle Valo0399eca2014-03-28 09:32:21 +02002239 /* target should never return this */
2240 if (val == 0xffffffff)
2241 continue;
2242
Michal Kazior7710cd22014-04-23 19:30:04 +03002243 /* the device has crashed so don't bother trying anymore */
2244 if (val & FW_IND_EVENT_PENDING)
2245 break;
2246
Kalle Valo0399eca2014-03-28 09:32:21 +02002247 if (val & FW_IND_INITIALIZED)
2248 break;
2249
Kalle Valo5e3dd152013-06-12 20:52:10 +03002250 if (ar_pci->num_msi_intrs == 0)
2251 /* Fix potential race by repeating CORE_BASE writes */
Michal Kaziorc947a9e2014-08-22 14:23:30 +02002252 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
2253 PCIE_INTR_ENABLE_ADDRESS,
2254 PCIE_INTR_FIRMWARE_MASK |
2255 PCIE_INTR_CE_MASK_ALL);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002256
Kalle Valo0399eca2014-03-28 09:32:21 +02002257 mdelay(10);
2258 } while (time_before(jiffies, timeout));
2259
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002260 if (val == 0xffffffff) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002261 ath10k_err(ar, "failed to read device register, device is gone\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002262 return -EIO;
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002263 }
2264
Michal Kazior7710cd22014-04-23 19:30:04 +03002265 if (val & FW_IND_EVENT_PENDING) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002266 ath10k_warn(ar, "device has crashed during init\n");
Michal Kazior5c771e72014-08-22 14:23:34 +02002267 ath10k_pci_fw_crashed_clear(ar);
Kalle Valo0e9848c2014-08-25 08:37:37 +03002268 ath10k_pci_fw_crashed_dump(ar);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002269 return -ECOMM;
Michal Kazior7710cd22014-04-23 19:30:04 +03002270 }
2271
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002272 if (!(val & FW_IND_INITIALIZED)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002273 ath10k_err(ar, "failed to receive initialized event from target: %08x\n",
Kalle Valo0399eca2014-03-28 09:32:21 +02002274 val);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002275 return -ETIMEDOUT;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002276 }
2277
Michal Kazior7aa7a722014-08-25 12:09:38 +02002278 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target initialised\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002279 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002280}
2281
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002282static int ath10k_pci_cold_reset(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002283{
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002284 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002285 u32 val;
2286
Michal Kazior7aa7a722014-08-25 12:09:38 +02002287 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cold reset\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002288
2289 /* Put Target, including PCIe, into RESET. */
Kalle Valoe479ed42013-09-01 10:01:53 +03002290 val = ath10k_pci_reg_read32(ar, SOC_GLOBAL_RESET_ADDRESS);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002291 val |= 1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002292 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002293
2294 for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
Kalle Valoe479ed42013-09-01 10:01:53 +03002295 if (ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03002296 RTC_STATE_COLD_RESET_MASK)
2297 break;
2298 msleep(1);
2299 }
2300
2301 /* Pull Target, including PCIe, out of RESET. */
2302 val &= ~1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002303 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002304
2305 for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
Kalle Valoe479ed42013-09-01 10:01:53 +03002306 if (!(ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03002307 RTC_STATE_COLD_RESET_MASK))
2308 break;
2309 msleep(1);
2310 }
2311
Michal Kazior7aa7a722014-08-25 12:09:38 +02002312 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cold reset complete\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02002313
Michal Kazior5b2589f2013-11-08 08:01:30 +01002314 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002315}
2316
Michal Kazior2986e3e2014-08-07 11:03:30 +02002317static int ath10k_pci_claim(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002318{
Michal Kazior2986e3e2014-08-07 11:03:30 +02002319 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2320 struct pci_dev *pdev = ar_pci->pdev;
2321 u32 lcr_val;
2322 int ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002323
2324 pci_set_drvdata(pdev, ar);
2325
Kalle Valo5e3dd152013-06-12 20:52:10 +03002326 ret = pci_enable_device(pdev);
2327 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002328 ath10k_err(ar, "failed to enable pci device: %d\n", ret);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002329 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002330 }
2331
Kalle Valo5e3dd152013-06-12 20:52:10 +03002332 ret = pci_request_region(pdev, BAR_NUM, "ath");
2333 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002334 ath10k_err(ar, "failed to request region BAR%d: %d\n", BAR_NUM,
Michal Kazior2986e3e2014-08-07 11:03:30 +02002335 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002336 goto err_device;
2337 }
2338
Michal Kazior2986e3e2014-08-07 11:03:30 +02002339 /* Target expects 32 bit DMA. Enforce it. */
Kalle Valo5e3dd152013-06-12 20:52:10 +03002340 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2341 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002342 ath10k_err(ar, "failed to set dma mask to 32-bit: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002343 goto err_region;
2344 }
2345
2346 ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2347 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002348 ath10k_err(ar, "failed to set consistent dma mask to 32-bit: %d\n",
Michal Kazior2986e3e2014-08-07 11:03:30 +02002349 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002350 goto err_region;
2351 }
2352
Kalle Valo5e3dd152013-06-12 20:52:10 +03002353 pci_set_master(pdev);
2354
Michal Kazior2986e3e2014-08-07 11:03:30 +02002355 /* Workaround: Disable ASPM */
Kalle Valo5e3dd152013-06-12 20:52:10 +03002356 pci_read_config_dword(pdev, 0x80, &lcr_val);
2357 pci_write_config_dword(pdev, 0x80, (lcr_val & 0xffffff00));
2358
2359 /* Arrange for access to Target SoC registers. */
Michal Kazior2986e3e2014-08-07 11:03:30 +02002360 ar_pci->mem = pci_iomap(pdev, BAR_NUM, 0);
2361 if (!ar_pci->mem) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002362 ath10k_err(ar, "failed to iomap BAR%d\n", BAR_NUM);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002363 ret = -EIO;
2364 goto err_master;
2365 }
2366
Michal Kazior7aa7a722014-08-25 12:09:38 +02002367 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot pci_mem 0x%p\n", ar_pci->mem);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002368 return 0;
2369
2370err_master:
2371 pci_clear_master(pdev);
2372
2373err_region:
2374 pci_release_region(pdev, BAR_NUM);
2375
2376err_device:
2377 pci_disable_device(pdev);
2378
2379 return ret;
2380}
2381
2382static void ath10k_pci_release(struct ath10k *ar)
2383{
2384 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2385 struct pci_dev *pdev = ar_pci->pdev;
2386
2387 pci_iounmap(pdev, ar_pci->mem);
2388 pci_release_region(pdev, BAR_NUM);
2389 pci_clear_master(pdev);
2390 pci_disable_device(pdev);
2391}
2392
Kalle Valo5e3dd152013-06-12 20:52:10 +03002393static int ath10k_pci_probe(struct pci_dev *pdev,
2394 const struct pci_device_id *pci_dev)
2395{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002396 int ret = 0;
2397 struct ath10k *ar;
2398 struct ath10k_pci *ar_pci;
Michal Kazior2986e3e2014-08-07 11:03:30 +02002399 u32 chip_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002400
Michal Kaziore7b54192014-08-07 11:03:27 +02002401 ar = ath10k_core_create(sizeof(*ar_pci), &pdev->dev,
2402 &ath10k_pci_hif_ops);
2403 if (!ar) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002404 dev_err(&pdev->dev, "failed to allocate core\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002405 return -ENOMEM;
Michal Kaziore7b54192014-08-07 11:03:27 +02002406 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002407
Michal Kazior7aa7a722014-08-25 12:09:38 +02002408 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci probe\n");
2409
Michal Kaziore7b54192014-08-07 11:03:27 +02002410 ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002411 ar_pci->pdev = pdev;
2412 ar_pci->dev = &pdev->dev;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002413 ar_pci->ar = ar;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002414
2415 spin_lock_init(&ar_pci->ce_lock);
Michal Kazior728f95e2014-08-22 14:33:14 +02002416 setup_timer(&ar_pci->rx_post_retry, ath10k_pci_rx_replenish_retry,
2417 (unsigned long)ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002418
Michal Kazior2986e3e2014-08-07 11:03:30 +02002419 ret = ath10k_pci_claim(ar);
Kalle Valoe01ae682013-09-01 11:22:14 +03002420 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002421 ath10k_err(ar, "failed to claim device: %d\n", ret);
Michal Kaziore7b54192014-08-07 11:03:27 +02002422 goto err_core_destroy;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002423 }
2424
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002425 ret = ath10k_pci_wake(ar);
Kalle Valoe01ae682013-09-01 11:22:14 +03002426 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002427 ath10k_err(ar, "failed to wake up: %d\n", ret);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002428 goto err_release;
Kalle Valoe01ae682013-09-01 11:22:14 +03002429 }
2430
Kalle Valo233eb972013-10-16 16:46:11 +03002431 chip_id = ath10k_pci_soc_read32(ar, SOC_CHIP_ID_ADDRESS);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002432 if (chip_id == 0xffffffff) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002433 ath10k_err(ar, "failed to get chip id\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002434 goto err_sleep;
2435 }
Kalle Valoe01ae682013-09-01 11:22:14 +03002436
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002437 ret = ath10k_pci_alloc_ce(ar);
2438 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002439 ath10k_err(ar, "failed to allocate copy engine pipes: %d\n",
2440 ret);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002441 goto err_sleep;
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002442 }
2443
Michal Kazior403d6272014-08-22 14:23:31 +02002444 ath10k_pci_ce_deinit(ar);
2445
2446 ret = ath10k_ce_disable_interrupts(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002447 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002448 ath10k_err(ar, "failed to disable copy engine interrupts: %d\n",
Michal Kazior403d6272014-08-22 14:23:31 +02002449 ret);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002450 goto err_free_ce;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002451 }
2452
Michal Kazior5c771e72014-08-22 14:23:34 +02002453 /* Workaround: There's no known way to mask all possible interrupts via
2454 * device CSR. The only way to make sure device doesn't assert
2455 * interrupts is to reset it. Interrupts are then disabled on host
2456 * after handlers are registered.
2457 */
2458 ath10k_pci_warm_reset(ar);
2459
Michal Kazior403d6272014-08-22 14:23:31 +02002460 ret = ath10k_pci_init_irq(ar);
2461 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002462 ath10k_err(ar, "failed to init irqs: %d\n", ret);
Michal Kazior403d6272014-08-22 14:23:31 +02002463 goto err_free_ce;
2464 }
2465
Michal Kazior7aa7a722014-08-25 12:09:38 +02002466 ath10k_info(ar, "pci irq %s interrupts %d irq_mode %d reset_mode %d\n",
Michal Kazior403d6272014-08-22 14:23:31 +02002467 ath10k_pci_get_irq_method(ar), ar_pci->num_msi_intrs,
2468 ath10k_pci_irq_mode, ath10k_pci_reset_mode);
2469
Michal Kazior5c771e72014-08-22 14:23:34 +02002470 ret = ath10k_pci_request_irq(ar);
Michal Kazior403d6272014-08-22 14:23:31 +02002471 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002472 ath10k_warn(ar, "failed to request irqs: %d\n", ret);
Michal Kazior403d6272014-08-22 14:23:31 +02002473 goto err_deinit_irq;
2474 }
2475
Michal Kazior5c771e72014-08-22 14:23:34 +02002476 /* This shouldn't race as the device has been reset above. */
2477 ath10k_pci_irq_disable(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002478
2479 ret = ath10k_core_register(ar, chip_id);
2480 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002481 ath10k_err(ar, "failed to register driver core: %d\n", ret);
Michal Kazior5c771e72014-08-22 14:23:34 +02002482 goto err_free_irq;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002483 }
2484
2485 return 0;
2486
Michal Kazior5c771e72014-08-22 14:23:34 +02002487err_free_irq:
2488 ath10k_pci_free_irq(ar);
Michal Kazior21396272014-08-28 10:24:40 +02002489 ath10k_pci_kill_tasklet(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02002490
Michal Kazior403d6272014-08-22 14:23:31 +02002491err_deinit_irq:
2492 ath10k_pci_deinit_irq(ar);
2493
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002494err_free_ce:
2495 ath10k_pci_free_ce(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002496
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002497err_sleep:
2498 ath10k_pci_sleep(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002499
2500err_release:
2501 ath10k_pci_release(ar);
2502
Michal Kaziore7b54192014-08-07 11:03:27 +02002503err_core_destroy:
Kalle Valo5e3dd152013-06-12 20:52:10 +03002504 ath10k_core_destroy(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002505
2506 return ret;
2507}
2508
2509static void ath10k_pci_remove(struct pci_dev *pdev)
2510{
2511 struct ath10k *ar = pci_get_drvdata(pdev);
2512 struct ath10k_pci *ar_pci;
2513
Michal Kazior7aa7a722014-08-25 12:09:38 +02002514 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci remove\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002515
2516 if (!ar)
2517 return;
2518
2519 ar_pci = ath10k_pci_priv(ar);
2520
2521 if (!ar_pci)
2522 return;
2523
Kalle Valo5e3dd152013-06-12 20:52:10 +03002524 ath10k_core_unregister(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02002525 ath10k_pci_free_irq(ar);
Michal Kazior21396272014-08-28 10:24:40 +02002526 ath10k_pci_kill_tasklet(ar);
Michal Kazior403d6272014-08-22 14:23:31 +02002527 ath10k_pci_deinit_irq(ar);
2528 ath10k_pci_ce_deinit(ar);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002529 ath10k_pci_free_ce(ar);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002530 ath10k_pci_sleep(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002531 ath10k_pci_release(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002532 ath10k_core_destroy(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002533}
2534
Kalle Valo5e3dd152013-06-12 20:52:10 +03002535MODULE_DEVICE_TABLE(pci, ath10k_pci_id_table);
2536
2537static struct pci_driver ath10k_pci_driver = {
2538 .name = "ath10k_pci",
2539 .id_table = ath10k_pci_id_table,
2540 .probe = ath10k_pci_probe,
2541 .remove = ath10k_pci_remove,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002542};
2543
2544static int __init ath10k_pci_init(void)
2545{
2546 int ret;
2547
2548 ret = pci_register_driver(&ath10k_pci_driver);
2549 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002550 printk(KERN_ERR "failed to register ath10k pci driver: %d\n",
2551 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002552
2553 return ret;
2554}
2555module_init(ath10k_pci_init);
2556
2557static void __exit ath10k_pci_exit(void)
2558{
2559 pci_unregister_driver(&ath10k_pci_driver);
2560}
2561
2562module_exit(ath10k_pci_exit);
2563
2564MODULE_AUTHOR("Qualcomm Atheros");
2565MODULE_DESCRIPTION("Driver support for Atheros QCA988X PCIe devices");
2566MODULE_LICENSE("Dual BSD/GPL");
Michal Kazior24c88f72014-07-25 13:32:17 +02002567MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_FW_3_FILE);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002568MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_BOARD_DATA_FILE);