blob: 3b40a86304bfe349654ce15359b4b0690d8b04ee [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 Kazior7505f7c2014-12-02 10:55:54 +020067static const struct ath10k_pci_supp_chip ath10k_pci_supp_chips[] = {
68 /* QCA988X pre 2.0 chips are not supported because they need some nasty
69 * hacks. ath10k doesn't have them and these devices crash horribly
70 * because of that.
71 */
72 { QCA988X_2_0_DEVICE_ID, QCA988X_HW_2_0_CHIP_ID_REV },
73};
74
Michal Kazior728f95e2014-08-22 14:33:14 +020075static void ath10k_pci_buffer_cleanup(struct ath10k *ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +010076static int ath10k_pci_cold_reset(struct ath10k *ar);
77static int ath10k_pci_warm_reset(struct ath10k *ar);
Michal Kaziord7fb47f2013-11-08 08:01:26 +010078static int ath10k_pci_wait_for_target_init(struct ath10k *ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +010079static int ath10k_pci_init_irq(struct ath10k *ar);
80static int ath10k_pci_deinit_irq(struct ath10k *ar);
81static int ath10k_pci_request_irq(struct ath10k *ar);
82static void ath10k_pci_free_irq(struct ath10k *ar);
Michal Kazior85622cd2013-11-25 14:06:22 +010083static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
84 struct ath10k_ce_pipe *rx_pipe,
85 struct bmi_xfer *xfer);
Kalle Valo5e3dd152013-06-12 20:52:10 +030086
87static const struct ce_attr host_ce_config_wlan[] = {
Kalle Valo48e9c222013-09-01 10:01:32 +030088 /* CE0: host->target HTC control and raw streams */
89 {
90 .flags = CE_ATTR_FLAGS,
91 .src_nentries = 16,
92 .src_sz_max = 256,
93 .dest_nentries = 0,
94 },
95
96 /* CE1: target->host HTT + HTC control */
97 {
98 .flags = CE_ATTR_FLAGS,
99 .src_nentries = 0,
100 .src_sz_max = 512,
101 .dest_nentries = 512,
102 },
103
104 /* CE2: target->host WMI */
105 {
106 .flags = CE_ATTR_FLAGS,
107 .src_nentries = 0,
108 .src_sz_max = 2048,
109 .dest_nentries = 32,
110 },
111
112 /* CE3: host->target WMI */
113 {
114 .flags = CE_ATTR_FLAGS,
115 .src_nentries = 32,
116 .src_sz_max = 2048,
117 .dest_nentries = 0,
118 },
119
120 /* CE4: host->target HTT */
121 {
122 .flags = CE_ATTR_FLAGS | CE_ATTR_DIS_INTR,
123 .src_nentries = CE_HTT_H2T_MSG_SRC_NENTRIES,
124 .src_sz_max = 256,
125 .dest_nentries = 0,
126 },
127
128 /* CE5: unused */
129 {
130 .flags = CE_ATTR_FLAGS,
131 .src_nentries = 0,
132 .src_sz_max = 0,
133 .dest_nentries = 0,
134 },
135
136 /* CE6: target autonomous hif_memcpy */
137 {
138 .flags = CE_ATTR_FLAGS,
139 .src_nentries = 0,
140 .src_sz_max = 0,
141 .dest_nentries = 0,
142 },
143
144 /* CE7: ce_diag, the Diagnostic Window */
145 {
146 .flags = CE_ATTR_FLAGS,
147 .src_nentries = 2,
148 .src_sz_max = DIAG_TRANSFER_LIMIT,
149 .dest_nentries = 2,
150 },
Kalle Valo5e3dd152013-06-12 20:52:10 +0300151};
152
153/* Target firmware's Copy Engine configuration. */
154static const struct ce_pipe_config target_ce_config_wlan[] = {
Kalle Valod88effb2013-09-01 10:01:39 +0300155 /* CE0: host->target HTC control and raw streams */
156 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300157 .pipenum = __cpu_to_le32(0),
158 .pipedir = __cpu_to_le32(PIPEDIR_OUT),
159 .nentries = __cpu_to_le32(32),
160 .nbytes_max = __cpu_to_le32(256),
161 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
162 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300163 },
164
165 /* CE1: target->host HTT + HTC control */
166 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300167 .pipenum = __cpu_to_le32(1),
168 .pipedir = __cpu_to_le32(PIPEDIR_IN),
169 .nentries = __cpu_to_le32(32),
170 .nbytes_max = __cpu_to_le32(512),
171 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
172 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300173 },
174
175 /* CE2: target->host WMI */
176 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300177 .pipenum = __cpu_to_le32(2),
178 .pipedir = __cpu_to_le32(PIPEDIR_IN),
179 .nentries = __cpu_to_le32(32),
180 .nbytes_max = __cpu_to_le32(2048),
181 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
182 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300183 },
184
185 /* CE3: host->target WMI */
186 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300187 .pipenum = __cpu_to_le32(3),
188 .pipedir = __cpu_to_le32(PIPEDIR_OUT),
189 .nentries = __cpu_to_le32(32),
190 .nbytes_max = __cpu_to_le32(2048),
191 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
192 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300193 },
194
195 /* CE4: host->target HTT */
196 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300197 .pipenum = __cpu_to_le32(4),
198 .pipedir = __cpu_to_le32(PIPEDIR_OUT),
199 .nentries = __cpu_to_le32(256),
200 .nbytes_max = __cpu_to_le32(256),
201 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
202 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300203 },
204
Kalle Valo5e3dd152013-06-12 20:52:10 +0300205 /* NB: 50% of src nentries, since tx has 2 frags */
Kalle Valod88effb2013-09-01 10:01:39 +0300206
207 /* CE5: unused */
208 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300209 .pipenum = __cpu_to_le32(5),
210 .pipedir = __cpu_to_le32(PIPEDIR_OUT),
211 .nentries = __cpu_to_le32(32),
212 .nbytes_max = __cpu_to_le32(2048),
213 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
214 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300215 },
216
217 /* CE6: Reserved for target autonomous hif_memcpy */
218 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300219 .pipenum = __cpu_to_le32(6),
220 .pipedir = __cpu_to_le32(PIPEDIR_INOUT),
221 .nentries = __cpu_to_le32(32),
222 .nbytes_max = __cpu_to_le32(4096),
223 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
224 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300225 },
226
Kalle Valo5e3dd152013-06-12 20:52:10 +0300227 /* CE7 used only by Host */
228};
229
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300230/*
231 * Map from service/endpoint to Copy Engine.
232 * This table is derived from the CE_PCI TABLE, above.
233 * It is passed to the Target at startup for use by firmware.
234 */
235static const struct service_to_pipe target_service_to_ce_map_wlan[] = {
236 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300237 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VO),
238 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
239 __cpu_to_le32(3),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300240 },
241 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300242 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VO),
243 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
244 __cpu_to_le32(2),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300245 },
246 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300247 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BK),
248 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
249 __cpu_to_le32(3),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300250 },
251 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300252 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BK),
253 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
254 __cpu_to_le32(2),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300255 },
256 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300257 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BE),
258 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
259 __cpu_to_le32(3),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300260 },
261 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300262 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BE),
263 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
264 __cpu_to_le32(2),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300265 },
266 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300267 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VI),
268 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
269 __cpu_to_le32(3),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300270 },
271 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300272 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VI),
273 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
274 __cpu_to_le32(2),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300275 },
276 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300277 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_CONTROL),
278 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
279 __cpu_to_le32(3),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300280 },
281 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300282 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_CONTROL),
283 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
284 __cpu_to_le32(2),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300285 },
286 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300287 __cpu_to_le32(ATH10K_HTC_SVC_ID_RSVD_CTRL),
288 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
289 __cpu_to_le32(0),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300290 },
291 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300292 __cpu_to_le32(ATH10K_HTC_SVC_ID_RSVD_CTRL),
293 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
294 __cpu_to_le32(1),
295 },
296 { /* not used */
297 __cpu_to_le32(ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS),
298 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
299 __cpu_to_le32(0),
300 },
301 { /* not used */
302 __cpu_to_le32(ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS),
303 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
304 __cpu_to_le32(1),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300305 },
306 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300307 __cpu_to_le32(ATH10K_HTC_SVC_ID_HTT_DATA_MSG),
308 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
309 __cpu_to_le32(4),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300310 },
311 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300312 __cpu_to_le32(ATH10K_HTC_SVC_ID_HTT_DATA_MSG),
313 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
314 __cpu_to_le32(1),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300315 },
316
317 /* (Additions here) */
318
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300319 { /* must be last */
320 __cpu_to_le32(0),
321 __cpu_to_le32(0),
322 __cpu_to_le32(0),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300323 },
324};
325
Michal Kaziore5398872013-11-25 14:06:20 +0100326static bool ath10k_pci_irq_pending(struct ath10k *ar)
327{
328 u32 cause;
329
330 /* Check if the shared legacy irq is for us */
331 cause = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
332 PCIE_INTR_CAUSE_ADDRESS);
333 if (cause & (PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL))
334 return true;
335
336 return false;
337}
338
Michal Kazior26852182013-11-25 14:06:25 +0100339static void ath10k_pci_disable_and_clear_legacy_irq(struct ath10k *ar)
340{
341 /* IMPORTANT: INTR_CLR register has to be set after
342 * INTR_ENABLE is set to 0, otherwise interrupt can not be
343 * really cleared. */
344 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
345 0);
346 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_CLR_ADDRESS,
347 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
348
349 /* IMPORTANT: this extra read transaction is required to
350 * flush the posted write buffer. */
Kalle Valocfbc06a2014-09-14 12:50:23 +0300351 (void)ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
352 PCIE_INTR_ENABLE_ADDRESS);
Michal Kazior26852182013-11-25 14:06:25 +0100353}
354
355static void ath10k_pci_enable_legacy_irq(struct ath10k *ar)
356{
357 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
358 PCIE_INTR_ENABLE_ADDRESS,
359 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
360
361 /* IMPORTANT: this extra read transaction is required to
362 * flush the posted write buffer. */
Kalle Valocfbc06a2014-09-14 12:50:23 +0300363 (void)ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
364 PCIE_INTR_ENABLE_ADDRESS);
Michal Kazior26852182013-11-25 14:06:25 +0100365}
366
Michal Kazior403d6272014-08-22 14:23:31 +0200367static inline const char *ath10k_pci_get_irq_method(struct ath10k *ar)
Michal Kaziorab977bd2013-11-25 14:06:26 +0100368{
Michal Kaziorab977bd2013-11-25 14:06:26 +0100369 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
370
Michal Kazior403d6272014-08-22 14:23:31 +0200371 if (ar_pci->num_msi_intrs > 1)
372 return "msi-x";
Kalle Valod8bb26b2014-09-14 12:50:33 +0300373
374 if (ar_pci->num_msi_intrs == 1)
Michal Kazior403d6272014-08-22 14:23:31 +0200375 return "msi";
Kalle Valod8bb26b2014-09-14 12:50:33 +0300376
377 return "legacy";
Michal Kaziorab977bd2013-11-25 14:06:26 +0100378}
379
Michal Kazior728f95e2014-08-22 14:33:14 +0200380static int __ath10k_pci_rx_post_buf(struct ath10k_pci_pipe *pipe)
Michal Kaziorab977bd2013-11-25 14:06:26 +0100381{
Michal Kazior728f95e2014-08-22 14:33:14 +0200382 struct ath10k *ar = pipe->hif_ce_state;
Michal Kaziorab977bd2013-11-25 14:06:26 +0100383 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior728f95e2014-08-22 14:33:14 +0200384 struct ath10k_ce_pipe *ce_pipe = pipe->ce_hdl;
385 struct sk_buff *skb;
386 dma_addr_t paddr;
Michal Kaziorab977bd2013-11-25 14:06:26 +0100387 int ret;
388
Michal Kazior728f95e2014-08-22 14:33:14 +0200389 lockdep_assert_held(&ar_pci->ce_lock);
390
391 skb = dev_alloc_skb(pipe->buf_sz);
392 if (!skb)
393 return -ENOMEM;
394
395 WARN_ONCE((unsigned long)skb->data & 3, "unaligned skb");
396
397 paddr = dma_map_single(ar->dev, skb->data,
398 skb->len + skb_tailroom(skb),
399 DMA_FROM_DEVICE);
400 if (unlikely(dma_mapping_error(ar->dev, paddr))) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200401 ath10k_warn(ar, "failed to dma map pci rx buf\n");
Michal Kazior728f95e2014-08-22 14:33:14 +0200402 dev_kfree_skb_any(skb);
403 return -EIO;
404 }
405
406 ATH10K_SKB_CB(skb)->paddr = paddr;
407
408 ret = __ath10k_ce_rx_post_buf(ce_pipe, skb, paddr);
Michal Kaziorab977bd2013-11-25 14:06:26 +0100409 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200410 ath10k_warn(ar, "failed to post pci rx buf: %d\n", ret);
Michal Kazior728f95e2014-08-22 14:33:14 +0200411 dma_unmap_single(ar->dev, paddr, skb->len + skb_tailroom(skb),
412 DMA_FROM_DEVICE);
413 dev_kfree_skb_any(skb);
Michal Kaziorab977bd2013-11-25 14:06:26 +0100414 return ret;
415 }
416
417 return 0;
418}
419
Michal Kazior728f95e2014-08-22 14:33:14 +0200420static void __ath10k_pci_rx_post_pipe(struct ath10k_pci_pipe *pipe)
Michal Kaziorab977bd2013-11-25 14:06:26 +0100421{
Michal Kazior728f95e2014-08-22 14:33:14 +0200422 struct ath10k *ar = pipe->hif_ce_state;
423 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
424 struct ath10k_ce_pipe *ce_pipe = pipe->ce_hdl;
425 int ret, num;
426
427 lockdep_assert_held(&ar_pci->ce_lock);
428
429 if (pipe->buf_sz == 0)
430 return;
431
432 if (!ce_pipe->dest_ring)
433 return;
434
435 num = __ath10k_ce_rx_num_free_bufs(ce_pipe);
436 while (num--) {
437 ret = __ath10k_pci_rx_post_buf(pipe);
438 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200439 ath10k_warn(ar, "failed to post pci rx buf: %d\n", ret);
Michal Kazior728f95e2014-08-22 14:33:14 +0200440 mod_timer(&ar_pci->rx_post_retry, jiffies +
441 ATH10K_PCI_RX_POST_RETRY_MS);
442 break;
443 }
444 }
445}
446
447static void ath10k_pci_rx_post_pipe(struct ath10k_pci_pipe *pipe)
448{
449 struct ath10k *ar = pipe->hif_ce_state;
450 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
451
452 spin_lock_bh(&ar_pci->ce_lock);
453 __ath10k_pci_rx_post_pipe(pipe);
454 spin_unlock_bh(&ar_pci->ce_lock);
455}
456
457static void ath10k_pci_rx_post(struct ath10k *ar)
458{
459 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
460 int i;
461
462 spin_lock_bh(&ar_pci->ce_lock);
463 for (i = 0; i < CE_COUNT; i++)
464 __ath10k_pci_rx_post_pipe(&ar_pci->pipe_info[i]);
465 spin_unlock_bh(&ar_pci->ce_lock);
466}
467
468static void ath10k_pci_rx_replenish_retry(unsigned long ptr)
469{
470 struct ath10k *ar = (void *)ptr;
471
472 ath10k_pci_rx_post(ar);
Michal Kaziorab977bd2013-11-25 14:06:26 +0100473}
474
Kalle Valo5e3dd152013-06-12 20:52:10 +0300475/*
476 * Diagnostic read/write access is provided for startup/config/debug usage.
477 * Caller must guarantee proper alignment, when applicable, and single user
478 * at any moment.
479 */
480static int ath10k_pci_diag_read_mem(struct ath10k *ar, u32 address, void *data,
481 int nbytes)
482{
483 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
484 int ret = 0;
485 u32 buf;
486 unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
487 unsigned int id;
488 unsigned int flags;
Michal Kazior2aa39112013-08-27 13:08:02 +0200489 struct ath10k_ce_pipe *ce_diag;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300490 /* Host buffer address in CE space */
491 u32 ce_data;
492 dma_addr_t ce_data_base = 0;
493 void *data_buf = NULL;
494 int i;
495
Kalle Valoeef25402014-09-24 14:16:52 +0300496 spin_lock_bh(&ar_pci->ce_lock);
497
Kalle Valo5e3dd152013-06-12 20:52:10 +0300498 ce_diag = ar_pci->ce_diag;
499
500 /*
501 * Allocate a temporary bounce buffer to hold caller's data
502 * to be DMA'ed from Target. This guarantees
503 * 1) 4-byte alignment
504 * 2) Buffer in DMA-able space
505 */
506 orig_nbytes = nbytes;
Michal Kazior68c03242014-03-28 10:02:35 +0200507 data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
508 orig_nbytes,
509 &ce_data_base,
510 GFP_ATOMIC);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300511
512 if (!data_buf) {
513 ret = -ENOMEM;
514 goto done;
515 }
516 memset(data_buf, 0, orig_nbytes);
517
518 remaining_bytes = orig_nbytes;
519 ce_data = ce_data_base;
520 while (remaining_bytes) {
521 nbytes = min_t(unsigned int, remaining_bytes,
522 DIAG_TRANSFER_LIMIT);
523
Kalle Valoeef25402014-09-24 14:16:52 +0300524 ret = __ath10k_ce_rx_post_buf(ce_diag, NULL, ce_data);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300525 if (ret != 0)
526 goto done;
527
528 /* Request CE to send from Target(!) address to Host buffer */
529 /*
530 * The address supplied by the caller is in the
531 * Target CPU virtual address space.
532 *
533 * In order to use this address with the diagnostic CE,
534 * convert it from Target CPU virtual address space
535 * to CE address space
536 */
Kalle Valo5e3dd152013-06-12 20:52:10 +0300537 address = TARG_CPU_SPACE_TO_CE_SPACE(ar, ar_pci->mem,
538 address);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300539
Kalle Valoeef25402014-09-24 14:16:52 +0300540 ret = ath10k_ce_send_nolock(ce_diag, NULL, (u32)address, nbytes, 0,
541 0);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300542 if (ret)
543 goto done;
544
545 i = 0;
Kalle Valoeef25402014-09-24 14:16:52 +0300546 while (ath10k_ce_completed_send_next_nolock(ce_diag, NULL, &buf,
547 &completed_nbytes,
548 &id) != 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300549 mdelay(1);
550 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
551 ret = -EBUSY;
552 goto done;
553 }
554 }
555
556 if (nbytes != completed_nbytes) {
557 ret = -EIO;
558 goto done;
559 }
560
Kalle Valocfbc06a2014-09-14 12:50:23 +0300561 if (buf != (u32)address) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300562 ret = -EIO;
563 goto done;
564 }
565
566 i = 0;
Kalle Valoeef25402014-09-24 14:16:52 +0300567 while (ath10k_ce_completed_recv_next_nolock(ce_diag, NULL, &buf,
568 &completed_nbytes,
569 &id, &flags) != 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300570 mdelay(1);
571
572 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
573 ret = -EBUSY;
574 goto done;
575 }
576 }
577
578 if (nbytes != completed_nbytes) {
579 ret = -EIO;
580 goto done;
581 }
582
583 if (buf != ce_data) {
584 ret = -EIO;
585 goto done;
586 }
587
588 remaining_bytes -= nbytes;
589 address += nbytes;
590 ce_data += nbytes;
591 }
592
593done:
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300594 if (ret == 0)
595 memcpy(data, data_buf, orig_nbytes);
596 else
Michal Kazior7aa7a722014-08-25 12:09:38 +0200597 ath10k_warn(ar, "failed to read diag value at 0x%x: %d\n",
Kalle Valo50f87a62014-03-28 09:32:52 +0200598 address, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300599
600 if (data_buf)
Michal Kazior68c03242014-03-28 10:02:35 +0200601 dma_free_coherent(ar->dev, orig_nbytes, data_buf,
602 ce_data_base);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300603
Kalle Valoeef25402014-09-24 14:16:52 +0300604 spin_unlock_bh(&ar_pci->ce_lock);
605
Kalle Valo5e3dd152013-06-12 20:52:10 +0300606 return ret;
607}
608
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300609static int ath10k_pci_diag_read32(struct ath10k *ar, u32 address, u32 *value)
610{
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300611 __le32 val = 0;
612 int ret;
613
614 ret = ath10k_pci_diag_read_mem(ar, address, &val, sizeof(val));
615 *value = __le32_to_cpu(val);
616
617 return ret;
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300618}
619
620static int __ath10k_pci_diag_read_hi(struct ath10k *ar, void *dest,
621 u32 src, u32 len)
622{
623 u32 host_addr, addr;
624 int ret;
625
626 host_addr = host_interest_item_address(src);
627
628 ret = ath10k_pci_diag_read32(ar, host_addr, &addr);
629 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200630 ath10k_warn(ar, "failed to get memcpy hi address for firmware address %d: %d\n",
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300631 src, ret);
632 return ret;
633 }
634
635 ret = ath10k_pci_diag_read_mem(ar, addr, dest, len);
636 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200637 ath10k_warn(ar, "failed to memcpy firmware memory from %d (%d B): %d\n",
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300638 addr, len, ret);
639 return ret;
640 }
641
642 return 0;
643}
644
645#define ath10k_pci_diag_read_hi(ar, dest, src, len) \
Kalle Valo8cc7f262014-09-14 12:50:39 +0300646 __ath10k_pci_diag_read_hi(ar, dest, HI_ITEM(src), len)
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300647
Kalle Valo5e3dd152013-06-12 20:52:10 +0300648static int ath10k_pci_diag_write_mem(struct ath10k *ar, u32 address,
649 const void *data, int nbytes)
650{
651 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
652 int ret = 0;
653 u32 buf;
654 unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
655 unsigned int id;
656 unsigned int flags;
Michal Kazior2aa39112013-08-27 13:08:02 +0200657 struct ath10k_ce_pipe *ce_diag;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300658 void *data_buf = NULL;
659 u32 ce_data; /* Host buffer address in CE space */
660 dma_addr_t ce_data_base = 0;
661 int i;
662
Kalle Valoeef25402014-09-24 14:16:52 +0300663 spin_lock_bh(&ar_pci->ce_lock);
664
Kalle Valo5e3dd152013-06-12 20:52:10 +0300665 ce_diag = ar_pci->ce_diag;
666
667 /*
668 * Allocate a temporary bounce buffer to hold caller's data
669 * to be DMA'ed to Target. This guarantees
670 * 1) 4-byte alignment
671 * 2) Buffer in DMA-able space
672 */
673 orig_nbytes = nbytes;
Michal Kazior68c03242014-03-28 10:02:35 +0200674 data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
675 orig_nbytes,
676 &ce_data_base,
677 GFP_ATOMIC);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300678 if (!data_buf) {
679 ret = -ENOMEM;
680 goto done;
681 }
682
683 /* Copy caller's data to allocated DMA buf */
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300684 memcpy(data_buf, data, orig_nbytes);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300685
686 /*
687 * The address supplied by the caller is in the
688 * Target CPU virtual address space.
689 *
690 * In order to use this address with the diagnostic CE,
691 * convert it from
692 * Target CPU virtual address space
693 * to
694 * CE address space
695 */
Kalle Valo5e3dd152013-06-12 20:52:10 +0300696 address = TARG_CPU_SPACE_TO_CE_SPACE(ar, ar_pci->mem, address);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300697
698 remaining_bytes = orig_nbytes;
699 ce_data = ce_data_base;
700 while (remaining_bytes) {
701 /* FIXME: check cast */
702 nbytes = min_t(int, remaining_bytes, DIAG_TRANSFER_LIMIT);
703
704 /* Set up to receive directly into Target(!) address */
Kalle Valoeef25402014-09-24 14:16:52 +0300705 ret = __ath10k_ce_rx_post_buf(ce_diag, NULL, address);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300706 if (ret != 0)
707 goto done;
708
709 /*
710 * Request CE to send caller-supplied data that
711 * was copied to bounce buffer to Target(!) address.
712 */
Kalle Valoeef25402014-09-24 14:16:52 +0300713 ret = ath10k_ce_send_nolock(ce_diag, NULL, (u32)ce_data,
714 nbytes, 0, 0);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300715 if (ret != 0)
716 goto done;
717
718 i = 0;
Kalle Valoeef25402014-09-24 14:16:52 +0300719 while (ath10k_ce_completed_send_next_nolock(ce_diag, NULL, &buf,
720 &completed_nbytes,
721 &id) != 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300722 mdelay(1);
723
724 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
725 ret = -EBUSY;
726 goto done;
727 }
728 }
729
730 if (nbytes != completed_nbytes) {
731 ret = -EIO;
732 goto done;
733 }
734
735 if (buf != ce_data) {
736 ret = -EIO;
737 goto done;
738 }
739
740 i = 0;
Kalle Valoeef25402014-09-24 14:16:52 +0300741 while (ath10k_ce_completed_recv_next_nolock(ce_diag, NULL, &buf,
742 &completed_nbytes,
743 &id, &flags) != 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300744 mdelay(1);
745
746 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
747 ret = -EBUSY;
748 goto done;
749 }
750 }
751
752 if (nbytes != completed_nbytes) {
753 ret = -EIO;
754 goto done;
755 }
756
757 if (buf != address) {
758 ret = -EIO;
759 goto done;
760 }
761
762 remaining_bytes -= nbytes;
763 address += nbytes;
764 ce_data += nbytes;
765 }
766
767done:
768 if (data_buf) {
Michal Kazior68c03242014-03-28 10:02:35 +0200769 dma_free_coherent(ar->dev, orig_nbytes, data_buf,
770 ce_data_base);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300771 }
772
773 if (ret != 0)
Michal Kazior7aa7a722014-08-25 12:09:38 +0200774 ath10k_warn(ar, "failed to write diag value at 0x%x: %d\n",
Kalle Valo50f87a62014-03-28 09:32:52 +0200775 address, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300776
Kalle Valoeef25402014-09-24 14:16:52 +0300777 spin_unlock_bh(&ar_pci->ce_lock);
778
Kalle Valo5e3dd152013-06-12 20:52:10 +0300779 return ret;
780}
781
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300782static int ath10k_pci_diag_write32(struct ath10k *ar, u32 address, u32 value)
783{
784 __le32 val = __cpu_to_le32(value);
785
786 return ath10k_pci_diag_write_mem(ar, address, &val, sizeof(val));
787}
788
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200789static bool ath10k_pci_is_awake(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300790{
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200791 u32 val = ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS);
792
793 return RTC_STATE_V_GET(val) == RTC_STATE_V_ON;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300794}
795
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200796static int ath10k_pci_wake_wait(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300797{
Kalle Valo5e3dd152013-06-12 20:52:10 +0300798 int tot_delay = 0;
799 int curr_delay = 5;
800
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200801 while (tot_delay < PCIE_WAKE_TIMEOUT) {
802 if (ath10k_pci_is_awake(ar))
Kalle Valo3aebe542013-09-01 10:02:07 +0300803 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300804
805 udelay(curr_delay);
806 tot_delay += curr_delay;
807
808 if (curr_delay < 50)
809 curr_delay += 5;
810 }
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200811
812 return -ETIMEDOUT;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300813}
814
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200815static int ath10k_pci_wake(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300816{
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200817 ath10k_pci_reg_write32(ar, PCIE_SOC_WAKE_ADDRESS,
818 PCIE_SOC_WAKE_V_MASK);
819 return ath10k_pci_wake_wait(ar);
820}
Kalle Valo5e3dd152013-06-12 20:52:10 +0300821
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200822static void ath10k_pci_sleep(struct ath10k *ar)
823{
824 ath10k_pci_reg_write32(ar, PCIE_SOC_WAKE_ADDRESS,
825 PCIE_SOC_WAKE_RESET);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300826}
827
Kalle Valo5e3dd152013-06-12 20:52:10 +0300828/* Called by lower (CE) layer when a send to Target completes. */
Michal Kazior5440ce22013-09-03 15:09:58 +0200829static void ath10k_pci_ce_send_done(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300830{
831 struct ath10k *ar = ce_state->ar;
832 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2f5280d2014-02-27 18:50:05 +0200833 struct ath10k_hif_cb *cb = &ar_pci->msg_callbacks_current;
Michal Kazior1cb86d42014-11-27 11:09:38 +0100834 struct sk_buff_head list;
835 struct sk_buff *skb;
Michal Kazior5440ce22013-09-03 15:09:58 +0200836 u32 ce_data;
837 unsigned int nbytes;
838 unsigned int transfer_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300839
Michal Kazior1cb86d42014-11-27 11:09:38 +0100840 __skb_queue_head_init(&list);
841 while (ath10k_ce_completed_send_next(ce_state, (void **)&skb, &ce_data,
842 &nbytes, &transfer_id) == 0) {
Michal Kaziora16942e2014-02-27 18:50:04 +0200843 /* no need to call tx completion for NULL pointers */
Michal Kazior1cb86d42014-11-27 11:09:38 +0100844 if (skb == NULL)
Michal Kazior726346f2014-02-27 18:50:04 +0200845 continue;
846
Michal Kazior1cb86d42014-11-27 11:09:38 +0100847 __skb_queue_tail(&list, skb);
Michal Kazior5440ce22013-09-03 15:09:58 +0200848 }
Michal Kazior1cb86d42014-11-27 11:09:38 +0100849
850 while ((skb = __skb_dequeue(&list)))
851 cb->tx_completion(ar, skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300852}
853
854/* Called by lower (CE) layer when data is received from the Target. */
Michal Kazior5440ce22013-09-03 15:09:58 +0200855static void ath10k_pci_ce_recv_data(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300856{
857 struct ath10k *ar = ce_state->ar;
858 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +0200859 struct ath10k_pci_pipe *pipe_info = &ar_pci->pipe_info[ce_state->id];
Michal Kazior2f5280d2014-02-27 18:50:05 +0200860 struct ath10k_hif_cb *cb = &ar_pci->msg_callbacks_current;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300861 struct sk_buff *skb;
Michal Kazior1cb86d42014-11-27 11:09:38 +0100862 struct sk_buff_head list;
Michal Kazior5440ce22013-09-03 15:09:58 +0200863 void *transfer_context;
864 u32 ce_data;
Michal Kazior2f5280d2014-02-27 18:50:05 +0200865 unsigned int nbytes, max_nbytes;
Michal Kazior5440ce22013-09-03 15:09:58 +0200866 unsigned int transfer_id;
867 unsigned int flags;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300868
Michal Kazior1cb86d42014-11-27 11:09:38 +0100869 __skb_queue_head_init(&list);
Michal Kazior5440ce22013-09-03 15:09:58 +0200870 while (ath10k_ce_completed_recv_next(ce_state, &transfer_context,
871 &ce_data, &nbytes, &transfer_id,
872 &flags) == 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300873 skb = transfer_context;
Michal Kazior2f5280d2014-02-27 18:50:05 +0200874 max_nbytes = skb->len + skb_tailroom(skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300875 dma_unmap_single(ar->dev, ATH10K_SKB_CB(skb)->paddr,
Michal Kazior2f5280d2014-02-27 18:50:05 +0200876 max_nbytes, DMA_FROM_DEVICE);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300877
Michal Kazior2f5280d2014-02-27 18:50:05 +0200878 if (unlikely(max_nbytes < nbytes)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200879 ath10k_warn(ar, "rxed more than expected (nbytes %d, max %d)",
Michal Kazior2f5280d2014-02-27 18:50:05 +0200880 nbytes, max_nbytes);
881 dev_kfree_skb_any(skb);
882 continue;
883 }
884
885 skb_put(skb, nbytes);
Michal Kazior1cb86d42014-11-27 11:09:38 +0100886 __skb_queue_tail(&list, skb);
887 }
Michal Kaziora360e542014-09-23 10:22:54 +0200888
Michal Kazior1cb86d42014-11-27 11:09:38 +0100889 while ((skb = __skb_dequeue(&list))) {
Michal Kaziora360e542014-09-23 10:22:54 +0200890 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci rx ce pipe %d len %d\n",
891 ce_state->id, skb->len);
892 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci rx: ",
893 skb->data, skb->len);
894
Michal Kazior5f07ea42014-11-27 11:09:36 +0100895 cb->rx_completion(ar, skb);
Michal Kazior2f5280d2014-02-27 18:50:05 +0200896 }
Michal Kaziorc29a3802014-07-21 21:03:10 +0300897
Michal Kazior728f95e2014-08-22 14:33:14 +0200898 ath10k_pci_rx_post_pipe(pipe_info);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300899}
900
Michal Kazior726346f2014-02-27 18:50:04 +0200901static int ath10k_pci_hif_tx_sg(struct ath10k *ar, u8 pipe_id,
902 struct ath10k_hif_sg_item *items, int n_items)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300903{
Kalle Valo5e3dd152013-06-12 20:52:10 +0300904 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior726346f2014-02-27 18:50:04 +0200905 struct ath10k_pci_pipe *pci_pipe = &ar_pci->pipe_info[pipe_id];
906 struct ath10k_ce_pipe *ce_pipe = pci_pipe->ce_hdl;
907 struct ath10k_ce_ring *src_ring = ce_pipe->src_ring;
Michal Kazior7147a132014-05-26 12:02:58 +0200908 unsigned int nentries_mask;
909 unsigned int sw_index;
910 unsigned int write_index;
Michal Kazior08b8aa02014-05-26 12:02:59 +0200911 int err, i = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300912
Michal Kazior726346f2014-02-27 18:50:04 +0200913 spin_lock_bh(&ar_pci->ce_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300914
Michal Kazior7147a132014-05-26 12:02:58 +0200915 nentries_mask = src_ring->nentries_mask;
916 sw_index = src_ring->sw_index;
917 write_index = src_ring->write_index;
918
Michal Kazior726346f2014-02-27 18:50:04 +0200919 if (unlikely(CE_RING_DELTA(nentries_mask,
920 write_index, sw_index - 1) < n_items)) {
921 err = -ENOBUFS;
Michal Kazior08b8aa02014-05-26 12:02:59 +0200922 goto err;
Michal Kazior726346f2014-02-27 18:50:04 +0200923 }
924
925 for (i = 0; i < n_items - 1; i++) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200926 ath10k_dbg(ar, ATH10K_DBG_PCI,
Michal Kazior726346f2014-02-27 18:50:04 +0200927 "pci tx item %d paddr 0x%08x len %d n_items %d\n",
928 i, items[i].paddr, items[i].len, n_items);
Michal Kazior7aa7a722014-08-25 12:09:38 +0200929 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci tx data: ",
Michal Kazior726346f2014-02-27 18:50:04 +0200930 items[i].vaddr, items[i].len);
931
932 err = ath10k_ce_send_nolock(ce_pipe,
933 items[i].transfer_context,
934 items[i].paddr,
935 items[i].len,
936 items[i].transfer_id,
937 CE_SEND_FLAG_GATHER);
938 if (err)
Michal Kazior08b8aa02014-05-26 12:02:59 +0200939 goto err;
Michal Kazior726346f2014-02-27 18:50:04 +0200940 }
941
942 /* `i` is equal to `n_items -1` after for() */
Kalle Valo5e3dd152013-06-12 20:52:10 +0300943
Michal Kazior7aa7a722014-08-25 12:09:38 +0200944 ath10k_dbg(ar, ATH10K_DBG_PCI,
Michal Kazior726346f2014-02-27 18:50:04 +0200945 "pci tx item %d paddr 0x%08x len %d n_items %d\n",
946 i, items[i].paddr, items[i].len, n_items);
Michal Kazior7aa7a722014-08-25 12:09:38 +0200947 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci tx data: ",
Michal Kazior726346f2014-02-27 18:50:04 +0200948 items[i].vaddr, items[i].len);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300949
Michal Kazior726346f2014-02-27 18:50:04 +0200950 err = ath10k_ce_send_nolock(ce_pipe,
951 items[i].transfer_context,
952 items[i].paddr,
953 items[i].len,
954 items[i].transfer_id,
955 0);
956 if (err)
Michal Kazior08b8aa02014-05-26 12:02:59 +0200957 goto err;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300958
Michal Kazior08b8aa02014-05-26 12:02:59 +0200959 spin_unlock_bh(&ar_pci->ce_lock);
960 return 0;
961
962err:
963 for (; i > 0; i--)
964 __ath10k_ce_send_revert(ce_pipe);
965
Michal Kazior726346f2014-02-27 18:50:04 +0200966 spin_unlock_bh(&ar_pci->ce_lock);
967 return err;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300968}
969
Kalle Valoeef25402014-09-24 14:16:52 +0300970static int ath10k_pci_hif_diag_read(struct ath10k *ar, u32 address, void *buf,
971 size_t buf_len)
972{
973 return ath10k_pci_diag_read_mem(ar, address, buf, buf_len);
974}
975
Kalle Valo5e3dd152013-06-12 20:52:10 +0300976static u16 ath10k_pci_hif_get_free_queue_number(struct ath10k *ar, u8 pipe)
977{
978 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo50f87a62014-03-28 09:32:52 +0200979
Michal Kazior7aa7a722014-08-25 12:09:38 +0200980 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif get free queue number\n");
Kalle Valo50f87a62014-03-28 09:32:52 +0200981
Michal Kazior3efcb3b2013-10-02 11:03:41 +0200982 return ath10k_ce_num_free_src_entries(ar_pci->pipe_info[pipe].ce_hdl);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300983}
984
Ben Greear384914b2014-08-25 08:37:32 +0300985static void ath10k_pci_dump_registers(struct ath10k *ar,
986 struct ath10k_fw_crash_data *crash_data)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300987{
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300988 __le32 reg_dump_values[REG_DUMP_COUNT_QCA988X] = {};
989 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300990
Ben Greear384914b2014-08-25 08:37:32 +0300991 lockdep_assert_held(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300992
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300993 ret = ath10k_pci_diag_read_hi(ar, &reg_dump_values[0],
994 hi_failure_state,
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300995 REG_DUMP_COUNT_QCA988X * sizeof(__le32));
Michal Kazior1d2b48d2013-11-08 08:01:34 +0100996 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200997 ath10k_err(ar, "failed to read firmware dump area: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300998 return;
999 }
1000
1001 BUILD_BUG_ON(REG_DUMP_COUNT_QCA988X % 4);
1002
Michal Kazior7aa7a722014-08-25 12:09:38 +02001003 ath10k_err(ar, "firmware register dump:\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001004 for (i = 0; i < REG_DUMP_COUNT_QCA988X; i += 4)
Michal Kazior7aa7a722014-08-25 12:09:38 +02001005 ath10k_err(ar, "[%02d]: 0x%08X 0x%08X 0x%08X 0x%08X\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001006 i,
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001007 __le32_to_cpu(reg_dump_values[i]),
1008 __le32_to_cpu(reg_dump_values[i + 1]),
1009 __le32_to_cpu(reg_dump_values[i + 2]),
1010 __le32_to_cpu(reg_dump_values[i + 3]));
Michal Kazioraffd3212013-07-16 09:54:35 +02001011
Michal Kazior1bbb1192014-08-25 12:13:14 +02001012 if (!crash_data)
1013 return;
1014
Ben Greear384914b2014-08-25 08:37:32 +03001015 for (i = 0; i < REG_DUMP_COUNT_QCA988X; i++)
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001016 crash_data->registers[i] = reg_dump_values[i];
Ben Greear384914b2014-08-25 08:37:32 +03001017}
1018
Kalle Valo0e9848c2014-08-25 08:37:37 +03001019static void ath10k_pci_fw_crashed_dump(struct ath10k *ar)
Ben Greear384914b2014-08-25 08:37:32 +03001020{
1021 struct ath10k_fw_crash_data *crash_data;
1022 char uuid[50];
1023
1024 spin_lock_bh(&ar->data_lock);
1025
Ben Greearf51dbe72014-09-29 14:41:46 +03001026 ar->stats.fw_crash_counter++;
1027
Ben Greear384914b2014-08-25 08:37:32 +03001028 crash_data = ath10k_debug_get_new_fw_crash_data(ar);
1029
1030 if (crash_data)
1031 scnprintf(uuid, sizeof(uuid), "%pUl", &crash_data->uuid);
1032 else
1033 scnprintf(uuid, sizeof(uuid), "n/a");
1034
Michal Kazior7aa7a722014-08-25 12:09:38 +02001035 ath10k_err(ar, "firmware crashed! (uuid %s)\n", uuid);
Kalle Valo8a0c7972014-08-25 08:37:45 +03001036 ath10k_print_driver_info(ar);
Ben Greear384914b2014-08-25 08:37:32 +03001037 ath10k_pci_dump_registers(ar, crash_data);
1038
Ben Greear384914b2014-08-25 08:37:32 +03001039 spin_unlock_bh(&ar->data_lock);
Michal Kazioraffd3212013-07-16 09:54:35 +02001040
Michal Kazior5e90de82013-10-16 16:46:05 +03001041 queue_work(ar->workqueue, &ar->restart_work);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001042}
1043
1044static void ath10k_pci_hif_send_complete_check(struct ath10k *ar, u8 pipe,
1045 int force)
1046{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001047 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif send complete check\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001048
Kalle Valo5e3dd152013-06-12 20:52:10 +03001049 if (!force) {
1050 int resources;
1051 /*
1052 * Decide whether to actually poll for completions, or just
1053 * wait for a later chance.
1054 * If there seem to be plenty of resources left, then just wait
1055 * since checking involves reading a CE register, which is a
1056 * relatively expensive operation.
1057 */
1058 resources = ath10k_pci_hif_get_free_queue_number(ar, pipe);
1059
1060 /*
1061 * If at least 50% of the total resources are still available,
1062 * don't bother checking again yet.
1063 */
1064 if (resources > (host_ce_config_wlan[pipe].src_nentries >> 1))
1065 return;
1066 }
1067 ath10k_ce_per_engine_service(ar, pipe);
1068}
1069
Michal Kaziore799bbf2013-07-05 16:15:12 +03001070static void ath10k_pci_hif_set_callbacks(struct ath10k *ar,
1071 struct ath10k_hif_cb *callbacks)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001072{
1073 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1074
Michal Kazior7aa7a722014-08-25 12:09:38 +02001075 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif set callbacks\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001076
1077 memcpy(&ar_pci->msg_callbacks_current, callbacks,
1078 sizeof(ar_pci->msg_callbacks_current));
1079}
1080
Michal Kazior96a9d0d2013-11-08 08:01:25 +01001081static void ath10k_pci_kill_tasklet(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001082{
1083 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001084 int i;
1085
Kalle Valo5e3dd152013-06-12 20:52:10 +03001086 tasklet_kill(&ar_pci->intr_tq);
Michal Kazior103d4f52013-11-08 08:01:24 +01001087 tasklet_kill(&ar_pci->msi_fw_err);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001088
1089 for (i = 0; i < CE_COUNT; i++)
1090 tasklet_kill(&ar_pci->pipe_info[i].intr);
Michal Kazior728f95e2014-08-22 14:33:14 +02001091
1092 del_timer_sync(&ar_pci->rx_post_retry);
Michal Kazior96a9d0d2013-11-08 08:01:25 +01001093}
1094
Kalle Valo5e3dd152013-06-12 20:52:10 +03001095static int ath10k_pci_hif_map_service_to_pipe(struct ath10k *ar,
1096 u16 service_id, u8 *ul_pipe,
1097 u8 *dl_pipe, int *ul_is_polled,
1098 int *dl_is_polled)
1099{
Michal Kazior7c6aa252014-08-26 19:14:03 +03001100 const struct service_to_pipe *entry;
1101 bool ul_set = false, dl_set = false;
1102 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001103
Michal Kazior7aa7a722014-08-25 12:09:38 +02001104 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif map service\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001105
Kalle Valo5e3dd152013-06-12 20:52:10 +03001106 /* polling for received messages not supported */
1107 *dl_is_polled = 0;
1108
Michal Kazior7c6aa252014-08-26 19:14:03 +03001109 for (i = 0; i < ARRAY_SIZE(target_service_to_ce_map_wlan); i++) {
1110 entry = &target_service_to_ce_map_wlan[i];
Kalle Valo5e3dd152013-06-12 20:52:10 +03001111
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001112 if (__le32_to_cpu(entry->service_id) != service_id)
Michal Kazior7c6aa252014-08-26 19:14:03 +03001113 continue;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001114
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001115 switch (__le32_to_cpu(entry->pipedir)) {
Michal Kazior7c6aa252014-08-26 19:14:03 +03001116 case PIPEDIR_NONE:
1117 break;
1118 case PIPEDIR_IN:
1119 WARN_ON(dl_set);
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001120 *dl_pipe = __le32_to_cpu(entry->pipenum);
Michal Kazior7c6aa252014-08-26 19:14:03 +03001121 dl_set = true;
1122 break;
1123 case PIPEDIR_OUT:
1124 WARN_ON(ul_set);
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001125 *ul_pipe = __le32_to_cpu(entry->pipenum);
Michal Kazior7c6aa252014-08-26 19:14:03 +03001126 ul_set = true;
1127 break;
1128 case PIPEDIR_INOUT:
1129 WARN_ON(dl_set);
1130 WARN_ON(ul_set);
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001131 *dl_pipe = __le32_to_cpu(entry->pipenum);
1132 *ul_pipe = __le32_to_cpu(entry->pipenum);
Michal Kazior7c6aa252014-08-26 19:14:03 +03001133 dl_set = true;
1134 ul_set = true;
1135 break;
1136 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001137 }
Michal Kazior7c6aa252014-08-26 19:14:03 +03001138
1139 if (WARN_ON(!ul_set || !dl_set))
1140 return -ENOENT;
1141
Kalle Valo5e3dd152013-06-12 20:52:10 +03001142 *ul_is_polled =
1143 (host_ce_config_wlan[*ul_pipe].flags & CE_ATTR_DIS_INTR) != 0;
1144
Michal Kazior7c6aa252014-08-26 19:14:03 +03001145 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001146}
1147
1148static void ath10k_pci_hif_get_default_pipe(struct ath10k *ar,
Kalle Valo5b07e072014-09-14 12:50:06 +03001149 u8 *ul_pipe, u8 *dl_pipe)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001150{
1151 int ul_is_polled, dl_is_polled;
1152
Michal Kazior7aa7a722014-08-25 12:09:38 +02001153 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif get default pipe\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001154
Kalle Valo5e3dd152013-06-12 20:52:10 +03001155 (void)ath10k_pci_hif_map_service_to_pipe(ar,
1156 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1157 ul_pipe,
1158 dl_pipe,
1159 &ul_is_polled,
1160 &dl_is_polled);
1161}
1162
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001163static void ath10k_pci_irq_msi_fw_mask(struct ath10k *ar)
1164{
1165 u32 val;
1166
1167 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS + CORE_CTRL_ADDRESS);
1168 val &= ~CORE_CTRL_PCIE_REG_31_MASK;
1169
1170 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + CORE_CTRL_ADDRESS, val);
1171}
1172
1173static void ath10k_pci_irq_msi_fw_unmask(struct ath10k *ar)
1174{
1175 u32 val;
1176
1177 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS + CORE_CTRL_ADDRESS);
1178 val |= CORE_CTRL_PCIE_REG_31_MASK;
1179
1180 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + CORE_CTRL_ADDRESS, val);
1181}
1182
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001183static void ath10k_pci_irq_disable(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001184{
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001185 ath10k_ce_disable_interrupts(ar);
1186 ath10k_pci_disable_and_clear_legacy_irq(ar);
1187 ath10k_pci_irq_msi_fw_mask(ar);
1188}
1189
1190static void ath10k_pci_irq_sync(struct ath10k *ar)
1191{
Kalle Valo5e3dd152013-06-12 20:52:10 +03001192 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001193 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001194
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001195 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
1196 synchronize_irq(ar_pci->pdev->irq + i);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001197}
1198
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001199static void ath10k_pci_irq_enable(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001200{
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001201 ath10k_ce_enable_interrupts(ar);
Michal Kaziore75db4e2014-08-28 22:14:16 +03001202 ath10k_pci_enable_legacy_irq(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001203 ath10k_pci_irq_msi_fw_unmask(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001204}
1205
1206static int ath10k_pci_hif_start(struct ath10k *ar)
1207{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001208 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif start\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001209
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001210 ath10k_pci_irq_enable(ar);
Michal Kazior728f95e2014-08-22 14:33:14 +02001211 ath10k_pci_rx_post(ar);
Kalle Valo50f87a62014-03-28 09:32:52 +02001212
Kalle Valo5e3dd152013-06-12 20:52:10 +03001213 return 0;
1214}
1215
Michal Kazior099ac7c2014-10-28 10:32:05 +01001216static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe *pci_pipe)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001217{
1218 struct ath10k *ar;
Michal Kazior099ac7c2014-10-28 10:32:05 +01001219 struct ath10k_ce_pipe *ce_pipe;
1220 struct ath10k_ce_ring *ce_ring;
1221 struct sk_buff *skb;
1222 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001223
Michal Kazior099ac7c2014-10-28 10:32:05 +01001224 ar = pci_pipe->hif_ce_state;
1225 ce_pipe = pci_pipe->ce_hdl;
1226 ce_ring = ce_pipe->dest_ring;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001227
Michal Kazior099ac7c2014-10-28 10:32:05 +01001228 if (!ce_ring)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001229 return;
1230
Michal Kazior099ac7c2014-10-28 10:32:05 +01001231 if (!pci_pipe->buf_sz)
1232 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001233
Michal Kazior099ac7c2014-10-28 10:32:05 +01001234 for (i = 0; i < ce_ring->nentries; i++) {
1235 skb = ce_ring->per_transfer_context[i];
1236 if (!skb)
1237 continue;
1238
1239 ce_ring->per_transfer_context[i] = NULL;
1240
1241 dma_unmap_single(ar->dev, ATH10K_SKB_CB(skb)->paddr,
1242 skb->len + skb_tailroom(skb),
Kalle Valo5e3dd152013-06-12 20:52:10 +03001243 DMA_FROM_DEVICE);
Michal Kazior099ac7c2014-10-28 10:32:05 +01001244 dev_kfree_skb_any(skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001245 }
1246}
1247
Michal Kazior099ac7c2014-10-28 10:32:05 +01001248static void ath10k_pci_tx_pipe_cleanup(struct ath10k_pci_pipe *pci_pipe)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001249{
1250 struct ath10k *ar;
1251 struct ath10k_pci *ar_pci;
Michal Kazior099ac7c2014-10-28 10:32:05 +01001252 struct ath10k_ce_pipe *ce_pipe;
1253 struct ath10k_ce_ring *ce_ring;
1254 struct ce_desc *ce_desc;
1255 struct sk_buff *skb;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001256 unsigned int id;
Michal Kazior099ac7c2014-10-28 10:32:05 +01001257 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001258
Michal Kazior099ac7c2014-10-28 10:32:05 +01001259 ar = pci_pipe->hif_ce_state;
1260 ar_pci = ath10k_pci_priv(ar);
1261 ce_pipe = pci_pipe->ce_hdl;
1262 ce_ring = ce_pipe->src_ring;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001263
Michal Kazior099ac7c2014-10-28 10:32:05 +01001264 if (!ce_ring)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001265 return;
1266
Michal Kazior099ac7c2014-10-28 10:32:05 +01001267 if (!pci_pipe->buf_sz)
1268 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001269
Michal Kazior099ac7c2014-10-28 10:32:05 +01001270 ce_desc = ce_ring->shadow_base;
1271 if (WARN_ON(!ce_desc))
1272 return;
1273
1274 for (i = 0; i < ce_ring->nentries; i++) {
1275 skb = ce_ring->per_transfer_context[i];
1276 if (!skb)
Michal Kazior2415fc12013-11-08 08:01:32 +01001277 continue;
Michal Kazior2415fc12013-11-08 08:01:32 +01001278
Michal Kazior099ac7c2014-10-28 10:32:05 +01001279 ce_ring->per_transfer_context[i] = NULL;
1280 id = MS(__le16_to_cpu(ce_desc[i].flags),
1281 CE_DESC_FLAGS_META_DATA);
1282
Michal Kaziord84a5122014-11-27 11:09:37 +01001283 ar_pci->msg_callbacks_current.tx_completion(ar, skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001284 }
1285}
1286
1287/*
1288 * Cleanup residual buffers for device shutdown:
1289 * buffers that were enqueued for receive
1290 * buffers that were to be sent
1291 * Note: Buffers that had completed but which were
1292 * not yet processed are on a completion queue. They
1293 * are handled when the completion thread shuts down.
1294 */
1295static void ath10k_pci_buffer_cleanup(struct ath10k *ar)
1296{
1297 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1298 int pipe_num;
1299
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001300 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Michal Kazior87263e52013-08-27 13:08:01 +02001301 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001302
1303 pipe_info = &ar_pci->pipe_info[pipe_num];
1304 ath10k_pci_rx_pipe_cleanup(pipe_info);
1305 ath10k_pci_tx_pipe_cleanup(pipe_info);
1306 }
1307}
1308
1309static void ath10k_pci_ce_deinit(struct ath10k *ar)
1310{
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001311 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001312
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001313 for (i = 0; i < CE_COUNT; i++)
1314 ath10k_ce_deinit_pipe(ar, i);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001315}
1316
Michal Kazior728f95e2014-08-22 14:33:14 +02001317static void ath10k_pci_flush(struct ath10k *ar)
1318{
1319 ath10k_pci_kill_tasklet(ar);
1320 ath10k_pci_buffer_cleanup(ar);
1321}
1322
Kalle Valo5e3dd152013-06-12 20:52:10 +03001323static void ath10k_pci_hif_stop(struct ath10k *ar)
1324{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001325 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif stop\n");
Michal Kazior32270b62013-08-02 09:15:47 +02001326
Michal Kazior10d23db2014-08-22 14:33:15 +02001327 /* Most likely the device has HTT Rx ring configured. The only way to
1328 * prevent the device from accessing (and possible corrupting) host
1329 * memory is to reset the chip now.
Michal Kaziore75db4e2014-08-28 22:14:16 +03001330 *
1331 * There's also no known way of masking MSI interrupts on the device.
1332 * For ranged MSI the CE-related interrupts can be masked. However
1333 * regardless how many MSI interrupts are assigned the first one
1334 * is always used for firmware indications (crashes) and cannot be
1335 * masked. To prevent the device from asserting the interrupt reset it
1336 * before proceeding with cleanup.
Michal Kazior10d23db2014-08-22 14:33:15 +02001337 */
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001338 ath10k_pci_warm_reset(ar);
Michal Kaziore75db4e2014-08-28 22:14:16 +03001339
1340 ath10k_pci_irq_disable(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001341 ath10k_pci_irq_sync(ar);
Michal Kaziore75db4e2014-08-28 22:14:16 +03001342 ath10k_pci_flush(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001343}
1344
1345static int ath10k_pci_hif_exchange_bmi_msg(struct ath10k *ar,
1346 void *req, u32 req_len,
1347 void *resp, u32 *resp_len)
1348{
1349 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2aa39112013-08-27 13:08:02 +02001350 struct ath10k_pci_pipe *pci_tx = &ar_pci->pipe_info[BMI_CE_NUM_TO_TARG];
1351 struct ath10k_pci_pipe *pci_rx = &ar_pci->pipe_info[BMI_CE_NUM_TO_HOST];
1352 struct ath10k_ce_pipe *ce_tx = pci_tx->ce_hdl;
1353 struct ath10k_ce_pipe *ce_rx = pci_rx->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001354 dma_addr_t req_paddr = 0;
1355 dma_addr_t resp_paddr = 0;
1356 struct bmi_xfer xfer = {};
1357 void *treq, *tresp = NULL;
1358 int ret = 0;
1359
Michal Kazior85622cd2013-11-25 14:06:22 +01001360 might_sleep();
1361
Kalle Valo5e3dd152013-06-12 20:52:10 +03001362 if (resp && !resp_len)
1363 return -EINVAL;
1364
1365 if (resp && resp_len && *resp_len == 0)
1366 return -EINVAL;
1367
1368 treq = kmemdup(req, req_len, GFP_KERNEL);
1369 if (!treq)
1370 return -ENOMEM;
1371
1372 req_paddr = dma_map_single(ar->dev, treq, req_len, DMA_TO_DEVICE);
1373 ret = dma_mapping_error(ar->dev, req_paddr);
1374 if (ret)
1375 goto err_dma;
1376
1377 if (resp && resp_len) {
1378 tresp = kzalloc(*resp_len, GFP_KERNEL);
1379 if (!tresp) {
1380 ret = -ENOMEM;
1381 goto err_req;
1382 }
1383
1384 resp_paddr = dma_map_single(ar->dev, tresp, *resp_len,
1385 DMA_FROM_DEVICE);
1386 ret = dma_mapping_error(ar->dev, resp_paddr);
1387 if (ret)
1388 goto err_req;
1389
1390 xfer.wait_for_resp = true;
1391 xfer.resp_len = 0;
1392
Michal Kazior728f95e2014-08-22 14:33:14 +02001393 ath10k_ce_rx_post_buf(ce_rx, &xfer, resp_paddr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001394 }
1395
Kalle Valo5e3dd152013-06-12 20:52:10 +03001396 ret = ath10k_ce_send(ce_tx, &xfer, req_paddr, req_len, -1, 0);
1397 if (ret)
1398 goto err_resp;
1399
Michal Kazior85622cd2013-11-25 14:06:22 +01001400 ret = ath10k_pci_bmi_wait(ce_tx, ce_rx, &xfer);
1401 if (ret) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001402 u32 unused_buffer;
1403 unsigned int unused_nbytes;
1404 unsigned int unused_id;
1405
Kalle Valo5e3dd152013-06-12 20:52:10 +03001406 ath10k_ce_cancel_send_next(ce_tx, NULL, &unused_buffer,
1407 &unused_nbytes, &unused_id);
1408 } else {
1409 /* non-zero means we did not time out */
1410 ret = 0;
1411 }
1412
1413err_resp:
1414 if (resp) {
1415 u32 unused_buffer;
1416
1417 ath10k_ce_revoke_recv_next(ce_rx, NULL, &unused_buffer);
1418 dma_unmap_single(ar->dev, resp_paddr,
1419 *resp_len, DMA_FROM_DEVICE);
1420 }
1421err_req:
1422 dma_unmap_single(ar->dev, req_paddr, req_len, DMA_TO_DEVICE);
1423
1424 if (ret == 0 && resp_len) {
1425 *resp_len = min(*resp_len, xfer.resp_len);
1426 memcpy(resp, tresp, xfer.resp_len);
1427 }
1428err_dma:
1429 kfree(treq);
1430 kfree(tresp);
1431
1432 return ret;
1433}
1434
Michal Kazior5440ce22013-09-03 15:09:58 +02001435static void ath10k_pci_bmi_send_done(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001436{
Michal Kazior5440ce22013-09-03 15:09:58 +02001437 struct bmi_xfer *xfer;
1438 u32 ce_data;
1439 unsigned int nbytes;
1440 unsigned int transfer_id;
1441
1442 if (ath10k_ce_completed_send_next(ce_state, (void **)&xfer, &ce_data,
1443 &nbytes, &transfer_id))
1444 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001445
Michal Kazior2374b182014-07-14 16:25:25 +03001446 xfer->tx_done = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001447}
1448
Michal Kazior5440ce22013-09-03 15:09:58 +02001449static void ath10k_pci_bmi_recv_data(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001450{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001451 struct ath10k *ar = ce_state->ar;
Michal Kazior5440ce22013-09-03 15:09:58 +02001452 struct bmi_xfer *xfer;
1453 u32 ce_data;
1454 unsigned int nbytes;
1455 unsigned int transfer_id;
1456 unsigned int flags;
1457
1458 if (ath10k_ce_completed_recv_next(ce_state, (void **)&xfer, &ce_data,
1459 &nbytes, &transfer_id, &flags))
1460 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001461
Michal Kazior04ed9df2014-10-28 10:34:36 +01001462 if (WARN_ON_ONCE(!xfer))
1463 return;
1464
Kalle Valo5e3dd152013-06-12 20:52:10 +03001465 if (!xfer->wait_for_resp) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001466 ath10k_warn(ar, "unexpected: BMI data received; ignoring\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001467 return;
1468 }
1469
1470 xfer->resp_len = nbytes;
Michal Kazior2374b182014-07-14 16:25:25 +03001471 xfer->rx_done = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001472}
1473
Michal Kazior85622cd2013-11-25 14:06:22 +01001474static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
1475 struct ath10k_ce_pipe *rx_pipe,
1476 struct bmi_xfer *xfer)
1477{
1478 unsigned long timeout = jiffies + BMI_COMMUNICATION_TIMEOUT_HZ;
1479
1480 while (time_before_eq(jiffies, timeout)) {
1481 ath10k_pci_bmi_send_done(tx_pipe);
1482 ath10k_pci_bmi_recv_data(rx_pipe);
1483
Michal Kazior2374b182014-07-14 16:25:25 +03001484 if (xfer->tx_done && (xfer->rx_done == xfer->wait_for_resp))
Michal Kazior85622cd2013-11-25 14:06:22 +01001485 return 0;
1486
1487 schedule();
1488 }
1489
1490 return -ETIMEDOUT;
1491}
1492
Kalle Valo5e3dd152013-06-12 20:52:10 +03001493/*
Kalle Valo5e3dd152013-06-12 20:52:10 +03001494 * Send an interrupt to the device to wake up the Target CPU
1495 * so it has an opportunity to notice any changed state.
1496 */
1497static int ath10k_pci_wake_target_cpu(struct ath10k *ar)
1498{
Michal Kazior9e264942014-09-02 11:00:21 +03001499 u32 addr, val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001500
Michal Kazior9e264942014-09-02 11:00:21 +03001501 addr = SOC_CORE_BASE_ADDRESS | CORE_CTRL_ADDRESS;
1502 val = ath10k_pci_read32(ar, addr);
1503 val |= CORE_CTRL_CPU_INTR_MASK;
1504 ath10k_pci_write32(ar, addr, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001505
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001506 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001507}
1508
1509static int ath10k_pci_init_config(struct ath10k *ar)
1510{
1511 u32 interconnect_targ_addr;
1512 u32 pcie_state_targ_addr = 0;
1513 u32 pipe_cfg_targ_addr = 0;
1514 u32 svc_to_pipe_map = 0;
1515 u32 pcie_config_flags = 0;
1516 u32 ealloc_value;
1517 u32 ealloc_targ_addr;
1518 u32 flag2_value;
1519 u32 flag2_targ_addr;
1520 int ret = 0;
1521
1522 /* Download to Target the CE Config and the service-to-CE map */
1523 interconnect_targ_addr =
1524 host_interest_item_address(HI_ITEM(hi_interconnect_state));
1525
1526 /* Supply Target-side CE configuration */
Michal Kazior9e264942014-09-02 11:00:21 +03001527 ret = ath10k_pci_diag_read32(ar, interconnect_targ_addr,
1528 &pcie_state_targ_addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001529 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001530 ath10k_err(ar, "Failed to get pcie state addr: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001531 return ret;
1532 }
1533
1534 if (pcie_state_targ_addr == 0) {
1535 ret = -EIO;
Michal Kazior7aa7a722014-08-25 12:09:38 +02001536 ath10k_err(ar, "Invalid pcie state addr\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001537 return ret;
1538 }
1539
Michal Kazior9e264942014-09-02 11:00:21 +03001540 ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
Kalle Valo5e3dd152013-06-12 20:52:10 +03001541 offsetof(struct pcie_state,
Michal Kazior9e264942014-09-02 11:00:21 +03001542 pipe_cfg_addr)),
1543 &pipe_cfg_targ_addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001544 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001545 ath10k_err(ar, "Failed to get pipe cfg addr: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001546 return ret;
1547 }
1548
1549 if (pipe_cfg_targ_addr == 0) {
1550 ret = -EIO;
Michal Kazior7aa7a722014-08-25 12:09:38 +02001551 ath10k_err(ar, "Invalid pipe cfg addr\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001552 return ret;
1553 }
1554
1555 ret = ath10k_pci_diag_write_mem(ar, pipe_cfg_targ_addr,
Kalle Valo5b07e072014-09-14 12:50:06 +03001556 target_ce_config_wlan,
1557 sizeof(target_ce_config_wlan));
Kalle Valo5e3dd152013-06-12 20:52:10 +03001558
1559 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001560 ath10k_err(ar, "Failed to write pipe cfg: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001561 return ret;
1562 }
1563
Michal Kazior9e264942014-09-02 11:00:21 +03001564 ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
Kalle Valo5e3dd152013-06-12 20:52:10 +03001565 offsetof(struct pcie_state,
Michal Kazior9e264942014-09-02 11:00:21 +03001566 svc_to_pipe_map)),
1567 &svc_to_pipe_map);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001568 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001569 ath10k_err(ar, "Failed to get svc/pipe map: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001570 return ret;
1571 }
1572
1573 if (svc_to_pipe_map == 0) {
1574 ret = -EIO;
Michal Kazior7aa7a722014-08-25 12:09:38 +02001575 ath10k_err(ar, "Invalid svc_to_pipe map\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001576 return ret;
1577 }
1578
1579 ret = ath10k_pci_diag_write_mem(ar, svc_to_pipe_map,
Kalle Valo5b07e072014-09-14 12:50:06 +03001580 target_service_to_ce_map_wlan,
1581 sizeof(target_service_to_ce_map_wlan));
Kalle Valo5e3dd152013-06-12 20:52:10 +03001582 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001583 ath10k_err(ar, "Failed to write svc/pipe map: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001584 return ret;
1585 }
1586
Michal Kazior9e264942014-09-02 11:00:21 +03001587 ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
Kalle Valo5e3dd152013-06-12 20:52:10 +03001588 offsetof(struct pcie_state,
Michal Kazior9e264942014-09-02 11:00:21 +03001589 config_flags)),
1590 &pcie_config_flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001591 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001592 ath10k_err(ar, "Failed to get pcie config_flags: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001593 return ret;
1594 }
1595
1596 pcie_config_flags &= ~PCIE_CONFIG_FLAG_ENABLE_L1;
1597
Michal Kazior9e264942014-09-02 11:00:21 +03001598 ret = ath10k_pci_diag_write32(ar, (pcie_state_targ_addr +
1599 offsetof(struct pcie_state,
1600 config_flags)),
1601 pcie_config_flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001602 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001603 ath10k_err(ar, "Failed to write pcie config_flags: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001604 return ret;
1605 }
1606
1607 /* configure early allocation */
1608 ealloc_targ_addr = host_interest_item_address(HI_ITEM(hi_early_alloc));
1609
Michal Kazior9e264942014-09-02 11:00:21 +03001610 ret = ath10k_pci_diag_read32(ar, ealloc_targ_addr, &ealloc_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001611 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001612 ath10k_err(ar, "Faile to get early alloc val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001613 return ret;
1614 }
1615
1616 /* first bank is switched to IRAM */
1617 ealloc_value |= ((HI_EARLY_ALLOC_MAGIC << HI_EARLY_ALLOC_MAGIC_SHIFT) &
1618 HI_EARLY_ALLOC_MAGIC_MASK);
1619 ealloc_value |= ((1 << HI_EARLY_ALLOC_IRAM_BANKS_SHIFT) &
1620 HI_EARLY_ALLOC_IRAM_BANKS_MASK);
1621
Michal Kazior9e264942014-09-02 11:00:21 +03001622 ret = ath10k_pci_diag_write32(ar, ealloc_targ_addr, ealloc_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001623 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001624 ath10k_err(ar, "Failed to set early alloc val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001625 return ret;
1626 }
1627
1628 /* Tell Target to proceed with initialization */
1629 flag2_targ_addr = host_interest_item_address(HI_ITEM(hi_option_flag2));
1630
Michal Kazior9e264942014-09-02 11:00:21 +03001631 ret = ath10k_pci_diag_read32(ar, flag2_targ_addr, &flag2_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001632 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001633 ath10k_err(ar, "Failed to get option val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001634 return ret;
1635 }
1636
1637 flag2_value |= HI_OPTION_EARLY_CFG_DONE;
1638
Michal Kazior9e264942014-09-02 11:00:21 +03001639 ret = ath10k_pci_diag_write32(ar, flag2_targ_addr, flag2_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001640 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001641 ath10k_err(ar, "Failed to set option val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001642 return ret;
1643 }
1644
1645 return 0;
1646}
1647
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001648static int ath10k_pci_alloc_pipes(struct ath10k *ar)
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001649{
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001650 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1651 struct ath10k_pci_pipe *pipe;
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001652 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001653
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001654 for (i = 0; i < CE_COUNT; i++) {
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001655 pipe = &ar_pci->pipe_info[i];
1656 pipe->ce_hdl = &ar_pci->ce_states[i];
1657 pipe->pipe_num = i;
1658 pipe->hif_ce_state = ar;
1659
1660 ret = ath10k_ce_alloc_pipe(ar, i, &host_ce_config_wlan[i],
1661 ath10k_pci_ce_send_done,
1662 ath10k_pci_ce_recv_data);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001663 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001664 ath10k_err(ar, "failed to allocate copy engine pipe %d: %d\n",
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001665 i, ret);
1666 return ret;
1667 }
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001668
1669 /* Last CE is Diagnostic Window */
1670 if (i == CE_COUNT - 1) {
1671 ar_pci->ce_diag = pipe->ce_hdl;
1672 continue;
1673 }
1674
1675 pipe->buf_sz = (size_t)(host_ce_config_wlan[i].src_sz_max);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001676 }
1677
1678 return 0;
1679}
1680
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001681static void ath10k_pci_free_pipes(struct ath10k *ar)
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001682{
1683 int i;
1684
1685 for (i = 0; i < CE_COUNT; i++)
1686 ath10k_ce_free_pipe(ar, i);
1687}
Kalle Valo5e3dd152013-06-12 20:52:10 +03001688
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001689static int ath10k_pci_init_pipes(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001690{
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001691 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001692
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001693 for (i = 0; i < CE_COUNT; i++) {
1694 ret = ath10k_ce_init_pipe(ar, i, &host_ce_config_wlan[i]);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001695 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001696 ath10k_err(ar, "failed to initialize copy engine pipe %d: %d\n",
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001697 i, ret);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001698 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001699 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001700 }
1701
Kalle Valo5e3dd152013-06-12 20:52:10 +03001702 return 0;
1703}
1704
Michal Kazior5c771e72014-08-22 14:23:34 +02001705static bool ath10k_pci_has_fw_crashed(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001706{
Michal Kazior5c771e72014-08-22 14:23:34 +02001707 return ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS) &
1708 FW_IND_EVENT_PENDING;
1709}
Kalle Valo5e3dd152013-06-12 20:52:10 +03001710
Michal Kazior5c771e72014-08-22 14:23:34 +02001711static void ath10k_pci_fw_crashed_clear(struct ath10k *ar)
1712{
1713 u32 val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001714
Michal Kazior5c771e72014-08-22 14:23:34 +02001715 val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
1716 val &= ~FW_IND_EVENT_PENDING;
1717 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001718}
1719
Michal Kaziorde013572014-05-14 16:56:16 +03001720/* this function effectively clears target memory controller assert line */
1721static void ath10k_pci_warm_reset_si0(struct ath10k *ar)
1722{
1723 u32 val;
1724
1725 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1726 ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
1727 val | SOC_RESET_CONTROL_SI0_RST_MASK);
1728 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1729
1730 msleep(10);
1731
1732 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1733 ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
1734 val & ~SOC_RESET_CONTROL_SI0_RST_MASK);
1735 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1736
1737 msleep(10);
1738}
1739
Michal Kazior61c16482014-10-28 10:32:06 +01001740static void ath10k_pci_warm_reset_cpu(struct ath10k *ar)
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001741{
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001742 u32 val;
1743
Kalle Valob39712c2014-03-28 09:32:46 +02001744 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, 0);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001745
Michal Kazior61c16482014-10-28 10:32:06 +01001746 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1747 SOC_RESET_CONTROL_ADDRESS);
1748 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1749 val | SOC_RESET_CONTROL_CPU_WARM_RST_MASK);
1750}
1751
1752static void ath10k_pci_warm_reset_ce(struct ath10k *ar)
1753{
1754 u32 val;
1755
1756 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1757 SOC_RESET_CONTROL_ADDRESS);
1758
1759 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1760 val | SOC_RESET_CONTROL_CE_RST_MASK);
1761 msleep(10);
1762 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1763 val & ~SOC_RESET_CONTROL_CE_RST_MASK);
1764}
1765
1766static void ath10k_pci_warm_reset_clear_lf(struct ath10k *ar)
1767{
1768 u32 val;
1769
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001770 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1771 SOC_LF_TIMER_CONTROL0_ADDRESS);
1772 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS +
1773 SOC_LF_TIMER_CONTROL0_ADDRESS,
1774 val & ~SOC_LF_TIMER_CONTROL0_ENABLE_MASK);
Michal Kazior61c16482014-10-28 10:32:06 +01001775}
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001776
Michal Kazior61c16482014-10-28 10:32:06 +01001777static int ath10k_pci_warm_reset(struct ath10k *ar)
1778{
1779 int ret;
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001780
Michal Kazior61c16482014-10-28 10:32:06 +01001781 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot warm reset\n");
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001782
Michal Kazior61c16482014-10-28 10:32:06 +01001783 spin_lock_bh(&ar->data_lock);
1784 ar->stats.fw_warm_reset_counter++;
1785 spin_unlock_bh(&ar->data_lock);
1786
1787 ath10k_pci_irq_disable(ar);
1788
1789 /* Make sure the target CPU is not doing anything dangerous, e.g. if it
1790 * were to access copy engine while host performs copy engine reset
1791 * then it is possible for the device to confuse pci-e controller to
1792 * the point of bringing host system to a complete stop (i.e. hang).
1793 */
Michal Kaziorde013572014-05-14 16:56:16 +03001794 ath10k_pci_warm_reset_si0(ar);
Michal Kazior61c16482014-10-28 10:32:06 +01001795 ath10k_pci_warm_reset_cpu(ar);
1796 ath10k_pci_init_pipes(ar);
1797 ath10k_pci_wait_for_target_init(ar);
Michal Kaziorde013572014-05-14 16:56:16 +03001798
Michal Kazior61c16482014-10-28 10:32:06 +01001799 ath10k_pci_warm_reset_clear_lf(ar);
1800 ath10k_pci_warm_reset_ce(ar);
1801 ath10k_pci_warm_reset_cpu(ar);
1802 ath10k_pci_init_pipes(ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001803
Michal Kazior61c16482014-10-28 10:32:06 +01001804 ret = ath10k_pci_wait_for_target_init(ar);
1805 if (ret) {
1806 ath10k_warn(ar, "failed to wait for target init: %d\n", ret);
1807 return ret;
1808 }
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001809
Michal Kazior7aa7a722014-08-25 12:09:38 +02001810 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot warm reset complete\n");
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001811
Michal Kaziorc0c378f2014-08-07 11:03:28 +02001812 return 0;
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001813}
1814
Michal Kazior0bc14d02014-10-28 10:32:07 +01001815static int ath10k_pci_chip_reset(struct ath10k *ar)
1816{
1817 int i, ret;
1818 u32 val;
1819
1820 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot chip reset\n");
1821
1822 /* Some hardware revisions (e.g. CUS223v2) has issues with cold reset.
1823 * It is thus preferred to use warm reset which is safer but may not be
1824 * able to recover the device from all possible fail scenarios.
1825 *
1826 * Warm reset doesn't always work on first try so attempt it a few
1827 * times before giving up.
1828 */
1829 for (i = 0; i < ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS; i++) {
1830 ret = ath10k_pci_warm_reset(ar);
1831 if (ret) {
1832 ath10k_warn(ar, "failed to warm reset attempt %d of %d: %d\n",
1833 i + 1, ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS,
1834 ret);
1835 continue;
1836 }
1837
1838 /* FIXME: Sometimes copy engine doesn't recover after warm
1839 * reset. In most cases this needs cold reset. In some of these
1840 * cases the device is in such a state that a cold reset may
1841 * lock up the host.
1842 *
1843 * Reading any host interest register via copy engine is
1844 * sufficient to verify if device is capable of booting
1845 * firmware blob.
1846 */
1847 ret = ath10k_pci_init_pipes(ar);
1848 if (ret) {
1849 ath10k_warn(ar, "failed to init copy engine: %d\n",
1850 ret);
1851 continue;
1852 }
1853
1854 ret = ath10k_pci_diag_read32(ar, QCA988X_HOST_INTEREST_ADDRESS,
1855 &val);
1856 if (ret) {
1857 ath10k_warn(ar, "failed to poke copy engine: %d\n",
1858 ret);
1859 continue;
1860 }
1861
1862 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot chip reset complete (warm)\n");
1863 return 0;
1864 }
1865
1866 if (ath10k_pci_reset_mode == ATH10K_PCI_RESET_WARM_ONLY) {
1867 ath10k_warn(ar, "refusing cold reset as requested\n");
1868 return -EPERM;
1869 }
1870
1871 ret = ath10k_pci_cold_reset(ar);
1872 if (ret) {
1873 ath10k_warn(ar, "failed to cold reset: %d\n", ret);
1874 return ret;
1875 }
1876
1877 ret = ath10k_pci_wait_for_target_init(ar);
1878 if (ret) {
1879 ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
1880 ret);
1881 return ret;
1882 }
1883
1884 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot chip reset complete (cold)\n");
1885
1886 return 0;
1887}
1888
1889static int ath10k_pci_hif_power_up(struct ath10k *ar)
Michal Kazior8c5c5362013-07-16 09:38:50 +02001890{
1891 int ret;
1892
Michal Kazior0bc14d02014-10-28 10:32:07 +01001893 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif power up\n");
1894
Bartosz Markowski707b1bbd2014-10-31 09:03:43 +01001895 ret = ath10k_pci_wake(ar);
1896 if (ret) {
1897 ath10k_err(ar, "failed to wake up target: %d\n", ret);
1898 return ret;
1899 }
1900
Michal Kazior8c5c5362013-07-16 09:38:50 +02001901 /*
1902 * Bring the target up cleanly.
1903 *
1904 * The target may be in an undefined state with an AUX-powered Target
1905 * and a Host in WoW mode. If the Host crashes, loses power, or is
1906 * restarted (without unloading the driver) then the Target is left
1907 * (aux) powered and running. On a subsequent driver load, the Target
1908 * is in an unexpected state. We try to catch that here in order to
1909 * reset the Target and retry the probe.
1910 */
Michal Kazior0bc14d02014-10-28 10:32:07 +01001911 ret = ath10k_pci_chip_reset(ar);
Michal Kazior5b2589f2013-11-08 08:01:30 +01001912 if (ret) {
Michal Kaziora2fa8802015-01-12 15:29:37 +01001913 if (ath10k_pci_has_fw_crashed(ar)) {
1914 ath10k_warn(ar, "firmware crashed during chip reset\n");
1915 ath10k_pci_fw_crashed_clear(ar);
1916 ath10k_pci_fw_crashed_dump(ar);
1917 }
1918
Michal Kazior0bc14d02014-10-28 10:32:07 +01001919 ath10k_err(ar, "failed to reset chip: %d\n", ret);
Bartosz Markowski707b1bbd2014-10-31 09:03:43 +01001920 goto err_sleep;
Michal Kazior5b2589f2013-11-08 08:01:30 +01001921 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02001922
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001923 ret = ath10k_pci_init_pipes(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001924 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001925 ath10k_err(ar, "failed to initialize CE: %d\n", ret);
Bartosz Markowski707b1bbd2014-10-31 09:03:43 +01001926 goto err_sleep;
Michal Kaziorab977bd2013-11-25 14:06:26 +01001927 }
1928
Michal Kazior98563d52013-11-08 08:01:33 +01001929 ret = ath10k_pci_init_config(ar);
1930 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001931 ath10k_err(ar, "failed to setup init config: %d\n", ret);
Michal Kazior5c771e72014-08-22 14:23:34 +02001932 goto err_ce;
Michal Kazior98563d52013-11-08 08:01:33 +01001933 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02001934
1935 ret = ath10k_pci_wake_target_cpu(ar);
1936 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001937 ath10k_err(ar, "could not wake up target CPU: %d\n", ret);
Michal Kazior5c771e72014-08-22 14:23:34 +02001938 goto err_ce;
Michal Kazior8c5c5362013-07-16 09:38:50 +02001939 }
1940
1941 return 0;
1942
1943err_ce:
1944 ath10k_pci_ce_deinit(ar);
Michal Kazior0bc14d02014-10-28 10:32:07 +01001945
Bartosz Markowski707b1bbd2014-10-31 09:03:43 +01001946err_sleep:
1947 ath10k_pci_sleep(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001948 return ret;
1949}
1950
1951static void ath10k_pci_hif_power_down(struct ath10k *ar)
1952{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001953 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif power down\n");
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02001954
Michal Kaziorc011b282014-10-28 10:32:08 +01001955 /* Currently hif_power_up performs effectively a reset and hif_stop
1956 * resets the chip as well so there's no point in resetting here.
1957 */
Bartosz Markowski707b1bbd2014-10-31 09:03:43 +01001958
1959 ath10k_pci_sleep(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001960}
1961
Michal Kazior8cd13ca2013-07-16 09:38:54 +02001962#ifdef CONFIG_PM
1963
1964#define ATH10K_PCI_PM_CONTROL 0x44
1965
1966static int ath10k_pci_hif_suspend(struct ath10k *ar)
1967{
1968 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1969 struct pci_dev *pdev = ar_pci->pdev;
1970 u32 val;
1971
1972 pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
1973
1974 if ((val & 0x000000ff) != 0x3) {
1975 pci_save_state(pdev);
1976 pci_disable_device(pdev);
1977 pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
1978 (val & 0xffffff00) | 0x03);
1979 }
1980
1981 return 0;
1982}
1983
1984static int ath10k_pci_hif_resume(struct ath10k *ar)
1985{
1986 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1987 struct pci_dev *pdev = ar_pci->pdev;
1988 u32 val;
1989
1990 pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
1991
1992 if ((val & 0x000000ff) != 0) {
1993 pci_restore_state(pdev);
1994 pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
1995 val & 0xffffff00);
1996 /*
1997 * Suspend/Resume resets the PCI configuration space,
1998 * so we have to re-disable the RETRY_TIMEOUT register (0x41)
1999 * to keep PCI Tx retries from interfering with C3 CPU state
2000 */
2001 pci_read_config_dword(pdev, 0x40, &val);
2002
2003 if ((val & 0x0000ff00) != 0)
2004 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
2005 }
2006
2007 return 0;
2008}
2009#endif
2010
Kalle Valo5e3dd152013-06-12 20:52:10 +03002011static const struct ath10k_hif_ops ath10k_pci_hif_ops = {
Michal Kazior726346f2014-02-27 18:50:04 +02002012 .tx_sg = ath10k_pci_hif_tx_sg,
Kalle Valoeef25402014-09-24 14:16:52 +03002013 .diag_read = ath10k_pci_hif_diag_read,
Yanbo Li9f65ad22014-11-25 12:24:48 +02002014 .diag_write = ath10k_pci_diag_write_mem,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002015 .exchange_bmi_msg = ath10k_pci_hif_exchange_bmi_msg,
2016 .start = ath10k_pci_hif_start,
2017 .stop = ath10k_pci_hif_stop,
2018 .map_service_to_pipe = ath10k_pci_hif_map_service_to_pipe,
2019 .get_default_pipe = ath10k_pci_hif_get_default_pipe,
2020 .send_complete_check = ath10k_pci_hif_send_complete_check,
Michal Kaziore799bbf2013-07-05 16:15:12 +03002021 .set_callbacks = ath10k_pci_hif_set_callbacks,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002022 .get_free_queue_number = ath10k_pci_hif_get_free_queue_number,
Michal Kazior8c5c5362013-07-16 09:38:50 +02002023 .power_up = ath10k_pci_hif_power_up,
2024 .power_down = ath10k_pci_hif_power_down,
Yanbo Li077a3802014-11-25 12:24:33 +02002025 .read32 = ath10k_pci_read32,
2026 .write32 = ath10k_pci_write32,
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002027#ifdef CONFIG_PM
2028 .suspend = ath10k_pci_hif_suspend,
2029 .resume = ath10k_pci_hif_resume,
2030#endif
Kalle Valo5e3dd152013-06-12 20:52:10 +03002031};
2032
2033static void ath10k_pci_ce_tasklet(unsigned long ptr)
2034{
Michal Kazior87263e52013-08-27 13:08:01 +02002035 struct ath10k_pci_pipe *pipe = (struct ath10k_pci_pipe *)ptr;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002036 struct ath10k_pci *ar_pci = pipe->ar_pci;
2037
2038 ath10k_ce_per_engine_service(ar_pci->ar, pipe->pipe_num);
2039}
2040
2041static void ath10k_msi_err_tasklet(unsigned long data)
2042{
2043 struct ath10k *ar = (struct ath10k *)data;
2044
Michal Kazior5c771e72014-08-22 14:23:34 +02002045 if (!ath10k_pci_has_fw_crashed(ar)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002046 ath10k_warn(ar, "received unsolicited fw crash interrupt\n");
Michal Kazior5c771e72014-08-22 14:23:34 +02002047 return;
2048 }
2049
2050 ath10k_pci_fw_crashed_clear(ar);
2051 ath10k_pci_fw_crashed_dump(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002052}
2053
2054/*
2055 * Handler for a per-engine interrupt on a PARTICULAR CE.
2056 * This is used in cases where each CE has a private MSI interrupt.
2057 */
2058static irqreturn_t ath10k_pci_per_engine_handler(int irq, void *arg)
2059{
2060 struct ath10k *ar = arg;
2061 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2062 int ce_id = irq - ar_pci->pdev->irq - MSI_ASSIGN_CE_INITIAL;
2063
Dan Carpentere5742672013-06-18 10:28:46 +03002064 if (ce_id < 0 || ce_id >= ARRAY_SIZE(ar_pci->pipe_info)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002065 ath10k_warn(ar, "unexpected/invalid irq %d ce_id %d\n", irq,
2066 ce_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002067 return IRQ_HANDLED;
2068 }
2069
2070 /*
2071 * NOTE: We are able to derive ce_id from irq because we
2072 * use a one-to-one mapping for CE's 0..5.
2073 * CE's 6 & 7 do not use interrupts at all.
2074 *
2075 * This mapping must be kept in sync with the mapping
2076 * used by firmware.
2077 */
2078 tasklet_schedule(&ar_pci->pipe_info[ce_id].intr);
2079 return IRQ_HANDLED;
2080}
2081
2082static irqreturn_t ath10k_pci_msi_fw_handler(int irq, void *arg)
2083{
2084 struct ath10k *ar = arg;
2085 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2086
2087 tasklet_schedule(&ar_pci->msi_fw_err);
2088 return IRQ_HANDLED;
2089}
2090
2091/*
2092 * Top-level interrupt handler for all PCI interrupts from a Target.
2093 * When a block of MSI interrupts is allocated, this top-level handler
2094 * is not used; instead, we directly call the correct sub-handler.
2095 */
2096static irqreturn_t ath10k_pci_interrupt_handler(int irq, void *arg)
2097{
2098 struct ath10k *ar = arg;
2099 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2100
2101 if (ar_pci->num_msi_intrs == 0) {
Michal Kaziore5398872013-11-25 14:06:20 +01002102 if (!ath10k_pci_irq_pending(ar))
2103 return IRQ_NONE;
2104
Michal Kazior26852182013-11-25 14:06:25 +01002105 ath10k_pci_disable_and_clear_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002106 }
2107
2108 tasklet_schedule(&ar_pci->intr_tq);
2109
2110 return IRQ_HANDLED;
2111}
2112
2113static void ath10k_pci_tasklet(unsigned long data)
2114{
2115 struct ath10k *ar = (struct ath10k *)data;
2116 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2117
Michal Kazior5c771e72014-08-22 14:23:34 +02002118 if (ath10k_pci_has_fw_crashed(ar)) {
2119 ath10k_pci_fw_crashed_clear(ar);
2120 ath10k_pci_fw_crashed_dump(ar);
2121 return;
2122 }
2123
Kalle Valo5e3dd152013-06-12 20:52:10 +03002124 ath10k_ce_per_engine_service_any(ar);
2125
Michal Kazior26852182013-11-25 14:06:25 +01002126 /* Re-enable legacy irq that was disabled in the irq handler */
2127 if (ar_pci->num_msi_intrs == 0)
2128 ath10k_pci_enable_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002129}
2130
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002131static int ath10k_pci_request_irq_msix(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002132{
2133 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002134 int ret, i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002135
2136 ret = request_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW,
2137 ath10k_pci_msi_fw_handler,
2138 IRQF_SHARED, "ath10k_pci", ar);
Michal Kazior591ecdb2013-07-31 10:55:15 +02002139 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002140 ath10k_warn(ar, "failed to request MSI-X fw irq %d: %d\n",
Michal Kazior591ecdb2013-07-31 10:55:15 +02002141 ar_pci->pdev->irq + MSI_ASSIGN_FW, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002142 return ret;
Michal Kazior591ecdb2013-07-31 10:55:15 +02002143 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002144
2145 for (i = MSI_ASSIGN_CE_INITIAL; i <= MSI_ASSIGN_CE_MAX; i++) {
2146 ret = request_irq(ar_pci->pdev->irq + i,
2147 ath10k_pci_per_engine_handler,
2148 IRQF_SHARED, "ath10k_pci", ar);
2149 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002150 ath10k_warn(ar, "failed to request MSI-X ce irq %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002151 ar_pci->pdev->irq + i, ret);
2152
Michal Kazior87b14232013-06-26 08:50:50 +02002153 for (i--; i >= MSI_ASSIGN_CE_INITIAL; i--)
2154 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002155
Michal Kazior87b14232013-06-26 08:50:50 +02002156 free_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002157 return ret;
2158 }
2159 }
2160
Kalle Valo5e3dd152013-06-12 20:52:10 +03002161 return 0;
2162}
2163
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002164static int ath10k_pci_request_irq_msi(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002165{
2166 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2167 int ret;
2168
2169 ret = request_irq(ar_pci->pdev->irq,
2170 ath10k_pci_interrupt_handler,
2171 IRQF_SHARED, "ath10k_pci", ar);
Kalle Valof3782742013-10-17 11:36:15 +03002172 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002173 ath10k_warn(ar, "failed to request MSI irq %d: %d\n",
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002174 ar_pci->pdev->irq, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002175 return ret;
Kalle Valof3782742013-10-17 11:36:15 +03002176 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002177
Kalle Valo5e3dd152013-06-12 20:52:10 +03002178 return 0;
2179}
2180
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002181static int ath10k_pci_request_irq_legacy(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002182{
2183 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002184 int ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002185
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002186 ret = request_irq(ar_pci->pdev->irq,
2187 ath10k_pci_interrupt_handler,
2188 IRQF_SHARED, "ath10k_pci", ar);
Kalle Valof3782742013-10-17 11:36:15 +03002189 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002190 ath10k_warn(ar, "failed to request legacy irq %d: %d\n",
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002191 ar_pci->pdev->irq, ret);
Kalle Valof3782742013-10-17 11:36:15 +03002192 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002193 }
2194
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002195 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002196}
2197
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002198static int ath10k_pci_request_irq(struct ath10k *ar)
2199{
2200 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2201
2202 switch (ar_pci->num_msi_intrs) {
2203 case 0:
2204 return ath10k_pci_request_irq_legacy(ar);
2205 case 1:
2206 return ath10k_pci_request_irq_msi(ar);
2207 case MSI_NUM_REQUEST:
2208 return ath10k_pci_request_irq_msix(ar);
2209 }
2210
Michal Kazior7aa7a722014-08-25 12:09:38 +02002211 ath10k_warn(ar, "unknown irq configuration upon request\n");
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002212 return -EINVAL;
2213}
2214
2215static void ath10k_pci_free_irq(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002216{
2217 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2218 int i;
2219
2220 /* There's at least one interrupt irregardless whether its legacy INTR
2221 * or MSI or MSI-X */
2222 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
2223 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002224}
2225
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002226static void ath10k_pci_init_irq_tasklets(struct ath10k *ar)
2227{
2228 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2229 int i;
2230
2231 tasklet_init(&ar_pci->intr_tq, ath10k_pci_tasklet, (unsigned long)ar);
2232 tasklet_init(&ar_pci->msi_fw_err, ath10k_msi_err_tasklet,
2233 (unsigned long)ar);
2234
2235 for (i = 0; i < CE_COUNT; i++) {
2236 ar_pci->pipe_info[i].ar_pci = ar_pci;
2237 tasklet_init(&ar_pci->pipe_info[i].intr, ath10k_pci_ce_tasklet,
2238 (unsigned long)&ar_pci->pipe_info[i]);
2239 }
2240}
2241
2242static int ath10k_pci_init_irq(struct ath10k *ar)
2243{
2244 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2245 int ret;
2246
2247 ath10k_pci_init_irq_tasklets(ar);
2248
Michal Kazior403d6272014-08-22 14:23:31 +02002249 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_AUTO)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002250 ath10k_info(ar, "limiting irq mode to: %d\n",
2251 ath10k_pci_irq_mode);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002252
2253 /* Try MSI-X */
Michal Kazior0edf2572014-08-07 11:03:29 +02002254 if (ath10k_pci_irq_mode == ATH10K_PCI_IRQ_AUTO) {
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002255 ar_pci->num_msi_intrs = MSI_NUM_REQUEST;
Alexander Gordeev5ad68672014-02-13 17:50:02 +02002256 ret = pci_enable_msi_range(ar_pci->pdev, ar_pci->num_msi_intrs,
Kalle Valo5b07e072014-09-14 12:50:06 +03002257 ar_pci->num_msi_intrs);
Alexander Gordeev5ad68672014-02-13 17:50:02 +02002258 if (ret > 0)
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002259 return 0;
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002260
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002261 /* fall-through */
2262 }
2263
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002264 /* Try MSI */
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002265 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_LEGACY) {
2266 ar_pci->num_msi_intrs = 1;
2267 ret = pci_enable_msi(ar_pci->pdev);
2268 if (ret == 0)
2269 return 0;
2270
2271 /* fall-through */
2272 }
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002273
2274 /* Try legacy irq
2275 *
2276 * A potential race occurs here: The CORE_BASE write
2277 * depends on target correctly decoding AXI address but
2278 * host won't know when target writes BAR to CORE_CTRL.
2279 * This write might get lost if target has NOT written BAR.
2280 * For now, fix the race by repeating the write in below
2281 * synchronization checking. */
2282 ar_pci->num_msi_intrs = 0;
2283
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002284 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2285 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002286
2287 return 0;
2288}
2289
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002290static void ath10k_pci_deinit_irq_legacy(struct ath10k *ar)
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002291{
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002292 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2293 0);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002294}
2295
2296static int ath10k_pci_deinit_irq(struct ath10k *ar)
2297{
2298 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2299
2300 switch (ar_pci->num_msi_intrs) {
2301 case 0:
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002302 ath10k_pci_deinit_irq_legacy(ar);
2303 return 0;
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002304 case 1:
2305 /* fall-through */
2306 case MSI_NUM_REQUEST:
2307 pci_disable_msi(ar_pci->pdev);
2308 return 0;
Alexander Gordeevbb8b6212014-02-13 17:50:01 +02002309 default:
2310 pci_disable_msi(ar_pci->pdev);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002311 }
2312
Michal Kazior7aa7a722014-08-25 12:09:38 +02002313 ath10k_warn(ar, "unknown irq configuration upon deinit\n");
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002314 return -EINVAL;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002315}
2316
Michal Kaziord7fb47f2013-11-08 08:01:26 +01002317static int ath10k_pci_wait_for_target_init(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002318{
2319 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo0399eca2014-03-28 09:32:21 +02002320 unsigned long timeout;
Kalle Valo0399eca2014-03-28 09:32:21 +02002321 u32 val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002322
Michal Kazior7aa7a722014-08-25 12:09:38 +02002323 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot waiting target to initialise\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002324
Kalle Valo0399eca2014-03-28 09:32:21 +02002325 timeout = jiffies + msecs_to_jiffies(ATH10K_PCI_TARGET_WAIT);
2326
2327 do {
2328 val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
2329
Michal Kazior7aa7a722014-08-25 12:09:38 +02002330 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target indicator %x\n",
2331 val);
Kalle Valo50f87a62014-03-28 09:32:52 +02002332
Kalle Valo0399eca2014-03-28 09:32:21 +02002333 /* target should never return this */
2334 if (val == 0xffffffff)
2335 continue;
2336
Michal Kazior7710cd22014-04-23 19:30:04 +03002337 /* the device has crashed so don't bother trying anymore */
2338 if (val & FW_IND_EVENT_PENDING)
2339 break;
2340
Kalle Valo0399eca2014-03-28 09:32:21 +02002341 if (val & FW_IND_INITIALIZED)
2342 break;
2343
Kalle Valo5e3dd152013-06-12 20:52:10 +03002344 if (ar_pci->num_msi_intrs == 0)
2345 /* Fix potential race by repeating CORE_BASE writes */
Michal Kaziora4282492014-10-20 14:14:37 +02002346 ath10k_pci_enable_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002347
Kalle Valo0399eca2014-03-28 09:32:21 +02002348 mdelay(10);
2349 } while (time_before(jiffies, timeout));
2350
Michal Kaziora4282492014-10-20 14:14:37 +02002351 ath10k_pci_disable_and_clear_legacy_irq(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02002352 ath10k_pci_irq_msi_fw_mask(ar);
Michal Kaziora4282492014-10-20 14:14:37 +02002353
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002354 if (val == 0xffffffff) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002355 ath10k_err(ar, "failed to read device register, device is gone\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002356 return -EIO;
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002357 }
2358
Michal Kazior7710cd22014-04-23 19:30:04 +03002359 if (val & FW_IND_EVENT_PENDING) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002360 ath10k_warn(ar, "device has crashed during init\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002361 return -ECOMM;
Michal Kazior7710cd22014-04-23 19:30:04 +03002362 }
2363
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002364 if (!(val & FW_IND_INITIALIZED)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002365 ath10k_err(ar, "failed to receive initialized event from target: %08x\n",
Kalle Valo0399eca2014-03-28 09:32:21 +02002366 val);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002367 return -ETIMEDOUT;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002368 }
2369
Michal Kazior7aa7a722014-08-25 12:09:38 +02002370 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target initialised\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002371 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002372}
2373
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002374static int ath10k_pci_cold_reset(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002375{
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002376 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002377 u32 val;
2378
Michal Kazior7aa7a722014-08-25 12:09:38 +02002379 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cold reset\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002380
Ben Greearf51dbe72014-09-29 14:41:46 +03002381 spin_lock_bh(&ar->data_lock);
2382
2383 ar->stats.fw_cold_reset_counter++;
2384
2385 spin_unlock_bh(&ar->data_lock);
2386
Kalle Valo5e3dd152013-06-12 20:52:10 +03002387 /* Put Target, including PCIe, into RESET. */
Kalle Valoe479ed42013-09-01 10:01:53 +03002388 val = ath10k_pci_reg_read32(ar, SOC_GLOBAL_RESET_ADDRESS);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002389 val |= 1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002390 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002391
2392 for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
Kalle Valoe479ed42013-09-01 10:01:53 +03002393 if (ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03002394 RTC_STATE_COLD_RESET_MASK)
2395 break;
2396 msleep(1);
2397 }
2398
2399 /* Pull Target, including PCIe, out of RESET. */
2400 val &= ~1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002401 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002402
2403 for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
Kalle Valoe479ed42013-09-01 10:01:53 +03002404 if (!(ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03002405 RTC_STATE_COLD_RESET_MASK))
2406 break;
2407 msleep(1);
2408 }
2409
Michal Kazior7aa7a722014-08-25 12:09:38 +02002410 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cold reset complete\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02002411
Michal Kazior5b2589f2013-11-08 08:01:30 +01002412 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002413}
2414
Michal Kazior2986e3e2014-08-07 11:03:30 +02002415static int ath10k_pci_claim(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002416{
Michal Kazior2986e3e2014-08-07 11:03:30 +02002417 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2418 struct pci_dev *pdev = ar_pci->pdev;
2419 u32 lcr_val;
2420 int ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002421
2422 pci_set_drvdata(pdev, ar);
2423
Kalle Valo5e3dd152013-06-12 20:52:10 +03002424 ret = pci_enable_device(pdev);
2425 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002426 ath10k_err(ar, "failed to enable pci device: %d\n", ret);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002427 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002428 }
2429
Kalle Valo5e3dd152013-06-12 20:52:10 +03002430 ret = pci_request_region(pdev, BAR_NUM, "ath");
2431 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002432 ath10k_err(ar, "failed to request region BAR%d: %d\n", BAR_NUM,
Michal Kazior2986e3e2014-08-07 11:03:30 +02002433 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002434 goto err_device;
2435 }
2436
Michal Kazior2986e3e2014-08-07 11:03:30 +02002437 /* Target expects 32 bit DMA. Enforce it. */
Kalle Valo5e3dd152013-06-12 20:52:10 +03002438 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2439 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002440 ath10k_err(ar, "failed to set dma mask to 32-bit: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002441 goto err_region;
2442 }
2443
2444 ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2445 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002446 ath10k_err(ar, "failed to set consistent dma mask to 32-bit: %d\n",
Michal Kazior2986e3e2014-08-07 11:03:30 +02002447 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002448 goto err_region;
2449 }
2450
Kalle Valo5e3dd152013-06-12 20:52:10 +03002451 pci_set_master(pdev);
2452
Michal Kazior2986e3e2014-08-07 11:03:30 +02002453 /* Workaround: Disable ASPM */
Kalle Valo5e3dd152013-06-12 20:52:10 +03002454 pci_read_config_dword(pdev, 0x80, &lcr_val);
2455 pci_write_config_dword(pdev, 0x80, (lcr_val & 0xffffff00));
2456
2457 /* Arrange for access to Target SoC registers. */
Michal Kazior2986e3e2014-08-07 11:03:30 +02002458 ar_pci->mem = pci_iomap(pdev, BAR_NUM, 0);
2459 if (!ar_pci->mem) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002460 ath10k_err(ar, "failed to iomap BAR%d\n", BAR_NUM);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002461 ret = -EIO;
2462 goto err_master;
2463 }
2464
Michal Kazior7aa7a722014-08-25 12:09:38 +02002465 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot pci_mem 0x%p\n", ar_pci->mem);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002466 return 0;
2467
2468err_master:
2469 pci_clear_master(pdev);
2470
2471err_region:
2472 pci_release_region(pdev, BAR_NUM);
2473
2474err_device:
2475 pci_disable_device(pdev);
2476
2477 return ret;
2478}
2479
2480static void ath10k_pci_release(struct ath10k *ar)
2481{
2482 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2483 struct pci_dev *pdev = ar_pci->pdev;
2484
2485 pci_iounmap(pdev, ar_pci->mem);
2486 pci_release_region(pdev, BAR_NUM);
2487 pci_clear_master(pdev);
2488 pci_disable_device(pdev);
2489}
2490
Michal Kazior7505f7c2014-12-02 10:55:54 +02002491static bool ath10k_pci_chip_is_supported(u32 dev_id, u32 chip_id)
2492{
2493 const struct ath10k_pci_supp_chip *supp_chip;
2494 int i;
2495 u32 rev_id = MS(chip_id, SOC_CHIP_ID_REV);
2496
2497 for (i = 0; i < ARRAY_SIZE(ath10k_pci_supp_chips); i++) {
2498 supp_chip = &ath10k_pci_supp_chips[i];
2499
2500 if (supp_chip->dev_id == dev_id &&
2501 supp_chip->rev_id == rev_id)
2502 return true;
2503 }
2504
2505 return false;
2506}
2507
Kalle Valo5e3dd152013-06-12 20:52:10 +03002508static int ath10k_pci_probe(struct pci_dev *pdev,
2509 const struct pci_device_id *pci_dev)
2510{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002511 int ret = 0;
2512 struct ath10k *ar;
2513 struct ath10k_pci *ar_pci;
Michal Kazior2986e3e2014-08-07 11:03:30 +02002514 u32 chip_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002515
Michal Kaziore7b54192014-08-07 11:03:27 +02002516 ar = ath10k_core_create(sizeof(*ar_pci), &pdev->dev,
Kalle Valoe07db352014-10-13 09:40:47 +03002517 ATH10K_BUS_PCI,
Michal Kaziore7b54192014-08-07 11:03:27 +02002518 &ath10k_pci_hif_ops);
2519 if (!ar) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002520 dev_err(&pdev->dev, "failed to allocate core\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002521 return -ENOMEM;
Michal Kaziore7b54192014-08-07 11:03:27 +02002522 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002523
Michal Kazior7aa7a722014-08-25 12:09:38 +02002524 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci probe\n");
2525
Michal Kaziore7b54192014-08-07 11:03:27 +02002526 ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002527 ar_pci->pdev = pdev;
2528 ar_pci->dev = &pdev->dev;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002529 ar_pci->ar = ar;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002530
2531 spin_lock_init(&ar_pci->ce_lock);
Michal Kazior728f95e2014-08-22 14:33:14 +02002532 setup_timer(&ar_pci->rx_post_retry, ath10k_pci_rx_replenish_retry,
2533 (unsigned long)ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002534
Michal Kazior2986e3e2014-08-07 11:03:30 +02002535 ret = ath10k_pci_claim(ar);
Kalle Valoe01ae682013-09-01 11:22:14 +03002536 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002537 ath10k_err(ar, "failed to claim device: %d\n", ret);
Michal Kaziore7b54192014-08-07 11:03:27 +02002538 goto err_core_destroy;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002539 }
2540
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002541 ret = ath10k_pci_wake(ar);
Kalle Valoe01ae682013-09-01 11:22:14 +03002542 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002543 ath10k_err(ar, "failed to wake up: %d\n", ret);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002544 goto err_release;
Kalle Valoe01ae682013-09-01 11:22:14 +03002545 }
2546
Kalle Valo233eb972013-10-16 16:46:11 +03002547 chip_id = ath10k_pci_soc_read32(ar, SOC_CHIP_ID_ADDRESS);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002548 if (chip_id == 0xffffffff) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002549 ath10k_err(ar, "failed to get chip id\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002550 goto err_sleep;
2551 }
Kalle Valoe01ae682013-09-01 11:22:14 +03002552
Michal Kazior7505f7c2014-12-02 10:55:54 +02002553 if (!ath10k_pci_chip_is_supported(pdev->device, chip_id)) {
2554 ath10k_err(ar, "device %04x with chip_id %08x isn't supported\n",
2555 pdev->device, chip_id);
2556 goto err_sleep;
2557 }
2558
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002559 ret = ath10k_pci_alloc_pipes(ar);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002560 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002561 ath10k_err(ar, "failed to allocate copy engine pipes: %d\n",
2562 ret);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002563 goto err_sleep;
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002564 }
2565
Michal Kazior403d6272014-08-22 14:23:31 +02002566 ath10k_pci_ce_deinit(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02002567 ath10k_pci_irq_disable(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02002568
Michal Kazior403d6272014-08-22 14:23:31 +02002569 ret = ath10k_pci_init_irq(ar);
2570 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002571 ath10k_err(ar, "failed to init irqs: %d\n", ret);
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002572 goto err_free_pipes;
Michal Kazior403d6272014-08-22 14:23:31 +02002573 }
2574
Michal Kazior7aa7a722014-08-25 12:09:38 +02002575 ath10k_info(ar, "pci irq %s interrupts %d irq_mode %d reset_mode %d\n",
Michal Kazior403d6272014-08-22 14:23:31 +02002576 ath10k_pci_get_irq_method(ar), ar_pci->num_msi_intrs,
2577 ath10k_pci_irq_mode, ath10k_pci_reset_mode);
2578
Michal Kazior5c771e72014-08-22 14:23:34 +02002579 ret = ath10k_pci_request_irq(ar);
Michal Kazior403d6272014-08-22 14:23:31 +02002580 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002581 ath10k_warn(ar, "failed to request irqs: %d\n", ret);
Michal Kazior403d6272014-08-22 14:23:31 +02002582 goto err_deinit_irq;
2583 }
2584
Bartosz Markowski707b1bbd2014-10-31 09:03:43 +01002585 ath10k_pci_sleep(ar);
2586
Kalle Valoe01ae682013-09-01 11:22:14 +03002587 ret = ath10k_core_register(ar, chip_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002588 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002589 ath10k_err(ar, "failed to register driver core: %d\n", ret);
Michal Kazior5c771e72014-08-22 14:23:34 +02002590 goto err_free_irq;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002591 }
2592
2593 return 0;
2594
Michal Kazior5c771e72014-08-22 14:23:34 +02002595err_free_irq:
2596 ath10k_pci_free_irq(ar);
Michal Kazior21396272014-08-28 10:24:40 +02002597 ath10k_pci_kill_tasklet(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02002598
Michal Kazior403d6272014-08-22 14:23:31 +02002599err_deinit_irq:
2600 ath10k_pci_deinit_irq(ar);
2601
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002602err_free_pipes:
2603 ath10k_pci_free_pipes(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002604
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002605err_sleep:
2606 ath10k_pci_sleep(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002607
2608err_release:
2609 ath10k_pci_release(ar);
2610
Michal Kaziore7b54192014-08-07 11:03:27 +02002611err_core_destroy:
Kalle Valo5e3dd152013-06-12 20:52:10 +03002612 ath10k_core_destroy(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002613
2614 return ret;
2615}
2616
2617static void ath10k_pci_remove(struct pci_dev *pdev)
2618{
2619 struct ath10k *ar = pci_get_drvdata(pdev);
2620 struct ath10k_pci *ar_pci;
2621
Michal Kazior7aa7a722014-08-25 12:09:38 +02002622 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci remove\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002623
2624 if (!ar)
2625 return;
2626
2627 ar_pci = ath10k_pci_priv(ar);
2628
2629 if (!ar_pci)
2630 return;
2631
Kalle Valo5e3dd152013-06-12 20:52:10 +03002632 ath10k_core_unregister(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02002633 ath10k_pci_free_irq(ar);
Michal Kazior21396272014-08-28 10:24:40 +02002634 ath10k_pci_kill_tasklet(ar);
Michal Kazior403d6272014-08-22 14:23:31 +02002635 ath10k_pci_deinit_irq(ar);
2636 ath10k_pci_ce_deinit(ar);
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002637 ath10k_pci_free_pipes(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002638 ath10k_pci_release(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002639 ath10k_core_destroy(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002640}
2641
Kalle Valo5e3dd152013-06-12 20:52:10 +03002642MODULE_DEVICE_TABLE(pci, ath10k_pci_id_table);
2643
2644static struct pci_driver ath10k_pci_driver = {
2645 .name = "ath10k_pci",
2646 .id_table = ath10k_pci_id_table,
2647 .probe = ath10k_pci_probe,
2648 .remove = ath10k_pci_remove,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002649};
2650
2651static int __init ath10k_pci_init(void)
2652{
2653 int ret;
2654
2655 ret = pci_register_driver(&ath10k_pci_driver);
2656 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002657 printk(KERN_ERR "failed to register ath10k pci driver: %d\n",
2658 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002659
2660 return ret;
2661}
2662module_init(ath10k_pci_init);
2663
2664static void __exit ath10k_pci_exit(void)
2665{
2666 pci_unregister_driver(&ath10k_pci_driver);
2667}
2668
2669module_exit(ath10k_pci_exit);
2670
2671MODULE_AUTHOR("Qualcomm Atheros");
2672MODULE_DESCRIPTION("Driver support for Atheros QCA988X PCIe devices");
2673MODULE_LICENSE("Dual BSD/GPL");
Bartosz Markowski8026cae2014-10-06 14:16:41 +02002674MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_FW_FILE);
2675MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API2_FILE);
2676MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API3_FILE);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002677MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_BOARD_DATA_FILE);