blob: 144eb8a30e2d41212c0213bfe48f7ff1eacfa32e [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
62static DEFINE_PCI_DEVICE_TABLE(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
67static int ath10k_pci_diag_read_access(struct ath10k *ar, u32 address,
68 u32 *data);
69
Michal Kazior728f95e2014-08-22 14:33:14 +020070static void ath10k_pci_buffer_cleanup(struct ath10k *ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +010071static int ath10k_pci_cold_reset(struct ath10k *ar);
72static int ath10k_pci_warm_reset(struct ath10k *ar);
Michal Kaziord7fb47f2013-11-08 08:01:26 +010073static int ath10k_pci_wait_for_target_init(struct ath10k *ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +010074static int ath10k_pci_init_irq(struct ath10k *ar);
75static int ath10k_pci_deinit_irq(struct ath10k *ar);
76static int ath10k_pci_request_irq(struct ath10k *ar);
77static void ath10k_pci_free_irq(struct ath10k *ar);
Michal Kazior85622cd2013-11-25 14:06:22 +010078static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
79 struct ath10k_ce_pipe *rx_pipe,
80 struct bmi_xfer *xfer);
Kalle Valo5e3dd152013-06-12 20:52:10 +030081
82static const struct ce_attr host_ce_config_wlan[] = {
Kalle Valo48e9c222013-09-01 10:01:32 +030083 /* CE0: host->target HTC control and raw streams */
84 {
85 .flags = CE_ATTR_FLAGS,
86 .src_nentries = 16,
87 .src_sz_max = 256,
88 .dest_nentries = 0,
89 },
90
91 /* CE1: target->host HTT + HTC control */
92 {
93 .flags = CE_ATTR_FLAGS,
94 .src_nentries = 0,
95 .src_sz_max = 512,
96 .dest_nentries = 512,
97 },
98
99 /* CE2: target->host WMI */
100 {
101 .flags = CE_ATTR_FLAGS,
102 .src_nentries = 0,
103 .src_sz_max = 2048,
104 .dest_nentries = 32,
105 },
106
107 /* CE3: host->target WMI */
108 {
109 .flags = CE_ATTR_FLAGS,
110 .src_nentries = 32,
111 .src_sz_max = 2048,
112 .dest_nentries = 0,
113 },
114
115 /* CE4: host->target HTT */
116 {
117 .flags = CE_ATTR_FLAGS | CE_ATTR_DIS_INTR,
118 .src_nentries = CE_HTT_H2T_MSG_SRC_NENTRIES,
119 .src_sz_max = 256,
120 .dest_nentries = 0,
121 },
122
123 /* CE5: unused */
124 {
125 .flags = CE_ATTR_FLAGS,
126 .src_nentries = 0,
127 .src_sz_max = 0,
128 .dest_nentries = 0,
129 },
130
131 /* CE6: target autonomous hif_memcpy */
132 {
133 .flags = CE_ATTR_FLAGS,
134 .src_nentries = 0,
135 .src_sz_max = 0,
136 .dest_nentries = 0,
137 },
138
139 /* CE7: ce_diag, the Diagnostic Window */
140 {
141 .flags = CE_ATTR_FLAGS,
142 .src_nentries = 2,
143 .src_sz_max = DIAG_TRANSFER_LIMIT,
144 .dest_nentries = 2,
145 },
Kalle Valo5e3dd152013-06-12 20:52:10 +0300146};
147
148/* Target firmware's Copy Engine configuration. */
149static const struct ce_pipe_config target_ce_config_wlan[] = {
Kalle Valod88effb2013-09-01 10:01:39 +0300150 /* CE0: host->target HTC control and raw streams */
151 {
152 .pipenum = 0,
153 .pipedir = PIPEDIR_OUT,
154 .nentries = 32,
155 .nbytes_max = 256,
156 .flags = CE_ATTR_FLAGS,
157 .reserved = 0,
158 },
159
160 /* CE1: target->host HTT + HTC control */
161 {
162 .pipenum = 1,
163 .pipedir = PIPEDIR_IN,
164 .nentries = 32,
165 .nbytes_max = 512,
166 .flags = CE_ATTR_FLAGS,
167 .reserved = 0,
168 },
169
170 /* CE2: target->host WMI */
171 {
172 .pipenum = 2,
173 .pipedir = PIPEDIR_IN,
174 .nentries = 32,
175 .nbytes_max = 2048,
176 .flags = CE_ATTR_FLAGS,
177 .reserved = 0,
178 },
179
180 /* CE3: host->target WMI */
181 {
182 .pipenum = 3,
183 .pipedir = PIPEDIR_OUT,
184 .nentries = 32,
185 .nbytes_max = 2048,
186 .flags = CE_ATTR_FLAGS,
187 .reserved = 0,
188 },
189
190 /* CE4: host->target HTT */
191 {
192 .pipenum = 4,
193 .pipedir = PIPEDIR_OUT,
194 .nentries = 256,
195 .nbytes_max = 256,
196 .flags = CE_ATTR_FLAGS,
197 .reserved = 0,
198 },
199
Kalle Valo5e3dd152013-06-12 20:52:10 +0300200 /* NB: 50% of src nentries, since tx has 2 frags */
Kalle Valod88effb2013-09-01 10:01:39 +0300201
202 /* CE5: unused */
203 {
204 .pipenum = 5,
205 .pipedir = PIPEDIR_OUT,
206 .nentries = 32,
207 .nbytes_max = 2048,
208 .flags = CE_ATTR_FLAGS,
209 .reserved = 0,
210 },
211
212 /* CE6: Reserved for target autonomous hif_memcpy */
213 {
214 .pipenum = 6,
215 .pipedir = PIPEDIR_INOUT,
216 .nentries = 32,
217 .nbytes_max = 4096,
218 .flags = CE_ATTR_FLAGS,
219 .reserved = 0,
220 },
221
Kalle Valo5e3dd152013-06-12 20:52:10 +0300222 /* CE7 used only by Host */
223};
224
Michal Kaziore5398872013-11-25 14:06:20 +0100225static bool ath10k_pci_irq_pending(struct ath10k *ar)
226{
227 u32 cause;
228
229 /* Check if the shared legacy irq is for us */
230 cause = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
231 PCIE_INTR_CAUSE_ADDRESS);
232 if (cause & (PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL))
233 return true;
234
235 return false;
236}
237
Michal Kazior26852182013-11-25 14:06:25 +0100238static void ath10k_pci_disable_and_clear_legacy_irq(struct ath10k *ar)
239{
240 /* IMPORTANT: INTR_CLR register has to be set after
241 * INTR_ENABLE is set to 0, otherwise interrupt can not be
242 * really cleared. */
243 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
244 0);
245 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_CLR_ADDRESS,
246 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
247
248 /* IMPORTANT: this extra read transaction is required to
249 * flush the posted write buffer. */
250 (void) ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
251 PCIE_INTR_ENABLE_ADDRESS);
252}
253
254static void ath10k_pci_enable_legacy_irq(struct ath10k *ar)
255{
256 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
257 PCIE_INTR_ENABLE_ADDRESS,
258 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
259
260 /* IMPORTANT: this extra read transaction is required to
261 * flush the posted write buffer. */
262 (void) ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
263 PCIE_INTR_ENABLE_ADDRESS);
264}
265
Michal Kazior403d6272014-08-22 14:23:31 +0200266static inline const char *ath10k_pci_get_irq_method(struct ath10k *ar)
267{
268 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
269
270 if (ar_pci->num_msi_intrs > 1)
271 return "msi-x";
272 else if (ar_pci->num_msi_intrs == 1)
273 return "msi";
274 else
275 return "legacy";
276}
277
Michal Kazior728f95e2014-08-22 14:33:14 +0200278static int __ath10k_pci_rx_post_buf(struct ath10k_pci_pipe *pipe)
279{
280 struct ath10k *ar = pipe->hif_ce_state;
281 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
282 struct ath10k_ce_pipe *ce_pipe = pipe->ce_hdl;
283 struct sk_buff *skb;
284 dma_addr_t paddr;
285 int ret;
286
287 lockdep_assert_held(&ar_pci->ce_lock);
288
289 skb = dev_alloc_skb(pipe->buf_sz);
290 if (!skb)
291 return -ENOMEM;
292
293 WARN_ONCE((unsigned long)skb->data & 3, "unaligned skb");
294
295 paddr = dma_map_single(ar->dev, skb->data,
296 skb->len + skb_tailroom(skb),
297 DMA_FROM_DEVICE);
298 if (unlikely(dma_mapping_error(ar->dev, paddr))) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200299 ath10k_warn(ar, "failed to dma map pci rx buf\n");
Michal Kazior728f95e2014-08-22 14:33:14 +0200300 dev_kfree_skb_any(skb);
301 return -EIO;
302 }
303
304 ATH10K_SKB_CB(skb)->paddr = paddr;
305
306 ret = __ath10k_ce_rx_post_buf(ce_pipe, skb, paddr);
307 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200308 ath10k_warn(ar, "failed to post pci rx buf: %d\n", ret);
Michal Kazior728f95e2014-08-22 14:33:14 +0200309 dma_unmap_single(ar->dev, paddr, skb->len + skb_tailroom(skb),
310 DMA_FROM_DEVICE);
311 dev_kfree_skb_any(skb);
312 return ret;
313 }
314
315 return 0;
316}
317
318static void __ath10k_pci_rx_post_pipe(struct ath10k_pci_pipe *pipe)
319{
320 struct ath10k *ar = pipe->hif_ce_state;
321 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
322 struct ath10k_ce_pipe *ce_pipe = pipe->ce_hdl;
323 int ret, num;
324
325 lockdep_assert_held(&ar_pci->ce_lock);
326
327 if (pipe->buf_sz == 0)
328 return;
329
330 if (!ce_pipe->dest_ring)
331 return;
332
333 num = __ath10k_ce_rx_num_free_bufs(ce_pipe);
334 while (num--) {
335 ret = __ath10k_pci_rx_post_buf(pipe);
336 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200337 ath10k_warn(ar, "failed to post pci rx buf: %d\n", ret);
Michal Kazior728f95e2014-08-22 14:33:14 +0200338 mod_timer(&ar_pci->rx_post_retry, jiffies +
339 ATH10K_PCI_RX_POST_RETRY_MS);
340 break;
341 }
342 }
343}
344
345static void ath10k_pci_rx_post_pipe(struct ath10k_pci_pipe *pipe)
346{
347 struct ath10k *ar = pipe->hif_ce_state;
348 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
349
350 spin_lock_bh(&ar_pci->ce_lock);
351 __ath10k_pci_rx_post_pipe(pipe);
352 spin_unlock_bh(&ar_pci->ce_lock);
353}
354
355static void ath10k_pci_rx_post(struct ath10k *ar)
356{
357 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
358 int i;
359
360 spin_lock_bh(&ar_pci->ce_lock);
361 for (i = 0; i < CE_COUNT; i++)
362 __ath10k_pci_rx_post_pipe(&ar_pci->pipe_info[i]);
363 spin_unlock_bh(&ar_pci->ce_lock);
364}
365
366static void ath10k_pci_rx_replenish_retry(unsigned long ptr)
367{
368 struct ath10k *ar = (void *)ptr;
369
370 ath10k_pci_rx_post(ar);
371}
372
Kalle Valo5e3dd152013-06-12 20:52:10 +0300373/*
374 * Diagnostic read/write access is provided for startup/config/debug usage.
375 * Caller must guarantee proper alignment, when applicable, and single user
376 * at any moment.
377 */
378static int ath10k_pci_diag_read_mem(struct ath10k *ar, u32 address, void *data,
379 int nbytes)
380{
381 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
382 int ret = 0;
383 u32 buf;
384 unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
385 unsigned int id;
386 unsigned int flags;
Michal Kazior2aa39112013-08-27 13:08:02 +0200387 struct ath10k_ce_pipe *ce_diag;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300388 /* Host buffer address in CE space */
389 u32 ce_data;
390 dma_addr_t ce_data_base = 0;
391 void *data_buf = NULL;
392 int i;
393
394 /*
395 * This code cannot handle reads to non-memory space. Redirect to the
396 * register read fn but preserve the multi word read capability of
397 * this fn
398 */
399 if (address < DRAM_BASE_ADDRESS) {
400 if (!IS_ALIGNED(address, 4) ||
401 !IS_ALIGNED((unsigned long)data, 4))
402 return -EIO;
403
404 while ((nbytes >= 4) && ((ret = ath10k_pci_diag_read_access(
405 ar, address, (u32 *)data)) == 0)) {
406 nbytes -= sizeof(u32);
407 address += sizeof(u32);
408 data += sizeof(u32);
409 }
410 return ret;
411 }
412
413 ce_diag = ar_pci->ce_diag;
414
415 /*
416 * Allocate a temporary bounce buffer to hold caller's data
417 * to be DMA'ed from Target. This guarantees
418 * 1) 4-byte alignment
419 * 2) Buffer in DMA-able space
420 */
421 orig_nbytes = nbytes;
Michal Kazior68c03242014-03-28 10:02:35 +0200422 data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
423 orig_nbytes,
424 &ce_data_base,
425 GFP_ATOMIC);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300426
427 if (!data_buf) {
428 ret = -ENOMEM;
429 goto done;
430 }
431 memset(data_buf, 0, orig_nbytes);
432
433 remaining_bytes = orig_nbytes;
434 ce_data = ce_data_base;
435 while (remaining_bytes) {
436 nbytes = min_t(unsigned int, remaining_bytes,
437 DIAG_TRANSFER_LIMIT);
438
Michal Kazior728f95e2014-08-22 14:33:14 +0200439 ret = ath10k_ce_rx_post_buf(ce_diag, NULL, ce_data);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300440 if (ret != 0)
441 goto done;
442
443 /* Request CE to send from Target(!) address to Host buffer */
444 /*
445 * The address supplied by the caller is in the
446 * Target CPU virtual address space.
447 *
448 * In order to use this address with the diagnostic CE,
449 * convert it from Target CPU virtual address space
450 * to CE address space
451 */
Kalle Valo5e3dd152013-06-12 20:52:10 +0300452 address = TARG_CPU_SPACE_TO_CE_SPACE(ar, ar_pci->mem,
453 address);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300454
455 ret = ath10k_ce_send(ce_diag, NULL, (u32)address, nbytes, 0,
456 0);
457 if (ret)
458 goto done;
459
460 i = 0;
461 while (ath10k_ce_completed_send_next(ce_diag, NULL, &buf,
462 &completed_nbytes,
463 &id) != 0) {
464 mdelay(1);
465 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
466 ret = -EBUSY;
467 goto done;
468 }
469 }
470
471 if (nbytes != completed_nbytes) {
472 ret = -EIO;
473 goto done;
474 }
475
476 if (buf != (u32) address) {
477 ret = -EIO;
478 goto done;
479 }
480
481 i = 0;
482 while (ath10k_ce_completed_recv_next(ce_diag, NULL, &buf,
483 &completed_nbytes,
484 &id, &flags) != 0) {
485 mdelay(1);
486
487 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
488 ret = -EBUSY;
489 goto done;
490 }
491 }
492
493 if (nbytes != completed_nbytes) {
494 ret = -EIO;
495 goto done;
496 }
497
498 if (buf != ce_data) {
499 ret = -EIO;
500 goto done;
501 }
502
503 remaining_bytes -= nbytes;
504 address += nbytes;
505 ce_data += nbytes;
506 }
507
508done:
509 if (ret == 0) {
510 /* Copy data from allocated DMA buf to caller's buf */
511 WARN_ON_ONCE(orig_nbytes & 3);
512 for (i = 0; i < orig_nbytes / sizeof(__le32); i++) {
513 ((u32 *)data)[i] =
514 __le32_to_cpu(((__le32 *)data_buf)[i]);
515 }
516 } else
Michal Kazior7aa7a722014-08-25 12:09:38 +0200517 ath10k_warn(ar, "failed to read diag value at 0x%x: %d\n",
Kalle Valo50f87a62014-03-28 09:32:52 +0200518 address, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300519
520 if (data_buf)
Michal Kazior68c03242014-03-28 10:02:35 +0200521 dma_free_coherent(ar->dev, orig_nbytes, data_buf,
522 ce_data_base);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300523
524 return ret;
525}
526
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300527static int ath10k_pci_diag_read32(struct ath10k *ar, u32 address, u32 *value)
528{
529 return ath10k_pci_diag_read_mem(ar, address, value, sizeof(u32));
530}
531
532static int __ath10k_pci_diag_read_hi(struct ath10k *ar, void *dest,
533 u32 src, u32 len)
534{
535 u32 host_addr, addr;
536 int ret;
537
538 host_addr = host_interest_item_address(src);
539
540 ret = ath10k_pci_diag_read32(ar, host_addr, &addr);
541 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200542 ath10k_warn(ar, "failed to get memcpy hi address for firmware address %d: %d\n",
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300543 src, ret);
544 return ret;
545 }
546
547 ret = ath10k_pci_diag_read_mem(ar, addr, dest, len);
548 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200549 ath10k_warn(ar, "failed to memcpy firmware memory from %d (%d B): %d\n",
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300550 addr, len, ret);
551 return ret;
552 }
553
554 return 0;
555}
556
557#define ath10k_pci_diag_read_hi(ar, dest, src, len) \
558 __ath10k_pci_diag_read_hi(ar, dest, HI_ITEM(src), len);
559
Kalle Valo5e3dd152013-06-12 20:52:10 +0300560/* Read 4-byte aligned data from Target memory or register */
561static int ath10k_pci_diag_read_access(struct ath10k *ar, u32 address,
562 u32 *data)
563{
564 /* Assume range doesn't cross this boundary */
565 if (address >= DRAM_BASE_ADDRESS)
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300566 return ath10k_pci_diag_read32(ar, address, data);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300567
Kalle Valo5e3dd152013-06-12 20:52:10 +0300568 *data = ath10k_pci_read32(ar, address);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300569 return 0;
570}
571
572static int ath10k_pci_diag_write_mem(struct ath10k *ar, u32 address,
573 const void *data, int nbytes)
574{
575 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
576 int ret = 0;
577 u32 buf;
578 unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
579 unsigned int id;
580 unsigned int flags;
Michal Kazior2aa39112013-08-27 13:08:02 +0200581 struct ath10k_ce_pipe *ce_diag;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300582 void *data_buf = NULL;
583 u32 ce_data; /* Host buffer address in CE space */
584 dma_addr_t ce_data_base = 0;
585 int i;
586
587 ce_diag = ar_pci->ce_diag;
588
589 /*
590 * Allocate a temporary bounce buffer to hold caller's data
591 * to be DMA'ed to Target. This guarantees
592 * 1) 4-byte alignment
593 * 2) Buffer in DMA-able space
594 */
595 orig_nbytes = nbytes;
Michal Kazior68c03242014-03-28 10:02:35 +0200596 data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
597 orig_nbytes,
598 &ce_data_base,
599 GFP_ATOMIC);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300600 if (!data_buf) {
601 ret = -ENOMEM;
602 goto done;
603 }
604
605 /* Copy caller's data to allocated DMA buf */
606 WARN_ON_ONCE(orig_nbytes & 3);
607 for (i = 0; i < orig_nbytes / sizeof(__le32); i++)
608 ((__le32 *)data_buf)[i] = __cpu_to_le32(((u32 *)data)[i]);
609
610 /*
611 * The address supplied by the caller is in the
612 * Target CPU virtual address space.
613 *
614 * In order to use this address with the diagnostic CE,
615 * convert it from
616 * Target CPU virtual address space
617 * to
618 * CE address space
619 */
Kalle Valo5e3dd152013-06-12 20:52:10 +0300620 address = TARG_CPU_SPACE_TO_CE_SPACE(ar, ar_pci->mem, address);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300621
622 remaining_bytes = orig_nbytes;
623 ce_data = ce_data_base;
624 while (remaining_bytes) {
625 /* FIXME: check cast */
626 nbytes = min_t(int, remaining_bytes, DIAG_TRANSFER_LIMIT);
627
628 /* Set up to receive directly into Target(!) address */
Michal Kazior728f95e2014-08-22 14:33:14 +0200629 ret = ath10k_ce_rx_post_buf(ce_diag, NULL, address);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300630 if (ret != 0)
631 goto done;
632
633 /*
634 * Request CE to send caller-supplied data that
635 * was copied to bounce buffer to Target(!) address.
636 */
637 ret = ath10k_ce_send(ce_diag, NULL, (u32) ce_data,
638 nbytes, 0, 0);
639 if (ret != 0)
640 goto done;
641
642 i = 0;
643 while (ath10k_ce_completed_send_next(ce_diag, NULL, &buf,
644 &completed_nbytes,
645 &id) != 0) {
646 mdelay(1);
647
648 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
649 ret = -EBUSY;
650 goto done;
651 }
652 }
653
654 if (nbytes != completed_nbytes) {
655 ret = -EIO;
656 goto done;
657 }
658
659 if (buf != ce_data) {
660 ret = -EIO;
661 goto done;
662 }
663
664 i = 0;
665 while (ath10k_ce_completed_recv_next(ce_diag, NULL, &buf,
666 &completed_nbytes,
667 &id, &flags) != 0) {
668 mdelay(1);
669
670 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
671 ret = -EBUSY;
672 goto done;
673 }
674 }
675
676 if (nbytes != completed_nbytes) {
677 ret = -EIO;
678 goto done;
679 }
680
681 if (buf != address) {
682 ret = -EIO;
683 goto done;
684 }
685
686 remaining_bytes -= nbytes;
687 address += nbytes;
688 ce_data += nbytes;
689 }
690
691done:
692 if (data_buf) {
Michal Kazior68c03242014-03-28 10:02:35 +0200693 dma_free_coherent(ar->dev, orig_nbytes, data_buf,
694 ce_data_base);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300695 }
696
697 if (ret != 0)
Michal Kazior7aa7a722014-08-25 12:09:38 +0200698 ath10k_warn(ar, "failed to write diag value at 0x%x: %d\n",
Kalle Valo50f87a62014-03-28 09:32:52 +0200699 address, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300700
701 return ret;
702}
703
704/* Write 4B data to Target memory or register */
705static int ath10k_pci_diag_write_access(struct ath10k *ar, u32 address,
706 u32 data)
707{
708 /* Assume range doesn't cross this boundary */
709 if (address >= DRAM_BASE_ADDRESS)
710 return ath10k_pci_diag_write_mem(ar, address, &data,
711 sizeof(u32));
712
Kalle Valo5e3dd152013-06-12 20:52:10 +0300713 ath10k_pci_write32(ar, address, data);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300714 return 0;
715}
716
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200717static bool ath10k_pci_is_awake(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300718{
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200719 u32 val = ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS);
720
721 return RTC_STATE_V_GET(val) == RTC_STATE_V_ON;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300722}
723
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200724static int ath10k_pci_wake_wait(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300725{
Kalle Valo5e3dd152013-06-12 20:52:10 +0300726 int tot_delay = 0;
727 int curr_delay = 5;
728
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200729 while (tot_delay < PCIE_WAKE_TIMEOUT) {
730 if (ath10k_pci_is_awake(ar))
Kalle Valo3aebe542013-09-01 10:02:07 +0300731 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300732
733 udelay(curr_delay);
734 tot_delay += curr_delay;
735
736 if (curr_delay < 50)
737 curr_delay += 5;
738 }
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200739
740 return -ETIMEDOUT;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300741}
742
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200743static int ath10k_pci_wake(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300744{
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200745 ath10k_pci_reg_write32(ar, PCIE_SOC_WAKE_ADDRESS,
746 PCIE_SOC_WAKE_V_MASK);
747 return ath10k_pci_wake_wait(ar);
748}
Kalle Valo5e3dd152013-06-12 20:52:10 +0300749
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200750static void ath10k_pci_sleep(struct ath10k *ar)
751{
752 ath10k_pci_reg_write32(ar, PCIE_SOC_WAKE_ADDRESS,
753 PCIE_SOC_WAKE_RESET);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300754}
755
Kalle Valo5e3dd152013-06-12 20:52:10 +0300756/* Called by lower (CE) layer when a send to Target completes. */
Michal Kazior5440ce22013-09-03 15:09:58 +0200757static void ath10k_pci_ce_send_done(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300758{
759 struct ath10k *ar = ce_state->ar;
760 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2f5280d2014-02-27 18:50:05 +0200761 struct ath10k_hif_cb *cb = &ar_pci->msg_callbacks_current;
Michal Kazior5440ce22013-09-03 15:09:58 +0200762 void *transfer_context;
763 u32 ce_data;
764 unsigned int nbytes;
765 unsigned int transfer_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300766
Michal Kazior5440ce22013-09-03 15:09:58 +0200767 while (ath10k_ce_completed_send_next(ce_state, &transfer_context,
768 &ce_data, &nbytes,
769 &transfer_id) == 0) {
Michal Kaziora16942e2014-02-27 18:50:04 +0200770 /* no need to call tx completion for NULL pointers */
Michal Kazior726346f2014-02-27 18:50:04 +0200771 if (transfer_context == NULL)
772 continue;
773
Michal Kazior2f5280d2014-02-27 18:50:05 +0200774 cb->tx_completion(ar, transfer_context, transfer_id);
Michal Kazior5440ce22013-09-03 15:09:58 +0200775 }
Kalle Valo5e3dd152013-06-12 20:52:10 +0300776}
777
778/* Called by lower (CE) layer when data is received from the Target. */
Michal Kazior5440ce22013-09-03 15:09:58 +0200779static void ath10k_pci_ce_recv_data(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300780{
781 struct ath10k *ar = ce_state->ar;
782 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +0200783 struct ath10k_pci_pipe *pipe_info = &ar_pci->pipe_info[ce_state->id];
Michal Kazior2f5280d2014-02-27 18:50:05 +0200784 struct ath10k_hif_cb *cb = &ar_pci->msg_callbacks_current;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300785 struct sk_buff *skb;
Michal Kazior5440ce22013-09-03 15:09:58 +0200786 void *transfer_context;
787 u32 ce_data;
Michal Kazior2f5280d2014-02-27 18:50:05 +0200788 unsigned int nbytes, max_nbytes;
Michal Kazior5440ce22013-09-03 15:09:58 +0200789 unsigned int transfer_id;
790 unsigned int flags;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300791
Michal Kazior5440ce22013-09-03 15:09:58 +0200792 while (ath10k_ce_completed_recv_next(ce_state, &transfer_context,
793 &ce_data, &nbytes, &transfer_id,
794 &flags) == 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300795 skb = transfer_context;
Michal Kazior2f5280d2014-02-27 18:50:05 +0200796 max_nbytes = skb->len + skb_tailroom(skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300797 dma_unmap_single(ar->dev, ATH10K_SKB_CB(skb)->paddr,
Michal Kazior2f5280d2014-02-27 18:50:05 +0200798 max_nbytes, DMA_FROM_DEVICE);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300799
Michal Kazior2f5280d2014-02-27 18:50:05 +0200800 if (unlikely(max_nbytes < nbytes)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200801 ath10k_warn(ar, "rxed more than expected (nbytes %d, max %d)",
Michal Kazior2f5280d2014-02-27 18:50:05 +0200802 nbytes, max_nbytes);
803 dev_kfree_skb_any(skb);
804 continue;
805 }
806
807 skb_put(skb, nbytes);
808 cb->rx_completion(ar, skb, pipe_info->pipe_num);
809 }
Michal Kaziorc29a3802014-07-21 21:03:10 +0300810
Michal Kazior728f95e2014-08-22 14:33:14 +0200811 ath10k_pci_rx_post_pipe(pipe_info);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300812}
813
Michal Kazior726346f2014-02-27 18:50:04 +0200814static int ath10k_pci_hif_tx_sg(struct ath10k *ar, u8 pipe_id,
815 struct ath10k_hif_sg_item *items, int n_items)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300816{
Kalle Valo5e3dd152013-06-12 20:52:10 +0300817 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior726346f2014-02-27 18:50:04 +0200818 struct ath10k_pci_pipe *pci_pipe = &ar_pci->pipe_info[pipe_id];
819 struct ath10k_ce_pipe *ce_pipe = pci_pipe->ce_hdl;
820 struct ath10k_ce_ring *src_ring = ce_pipe->src_ring;
Michal Kazior7147a132014-05-26 12:02:58 +0200821 unsigned int nentries_mask;
822 unsigned int sw_index;
823 unsigned int write_index;
Michal Kazior08b8aa02014-05-26 12:02:59 +0200824 int err, i = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300825
Michal Kazior726346f2014-02-27 18:50:04 +0200826 spin_lock_bh(&ar_pci->ce_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300827
Michal Kazior7147a132014-05-26 12:02:58 +0200828 nentries_mask = src_ring->nentries_mask;
829 sw_index = src_ring->sw_index;
830 write_index = src_ring->write_index;
831
Michal Kazior726346f2014-02-27 18:50:04 +0200832 if (unlikely(CE_RING_DELTA(nentries_mask,
833 write_index, sw_index - 1) < n_items)) {
834 err = -ENOBUFS;
Michal Kazior08b8aa02014-05-26 12:02:59 +0200835 goto err;
Michal Kazior726346f2014-02-27 18:50:04 +0200836 }
837
838 for (i = 0; i < n_items - 1; i++) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200839 ath10k_dbg(ar, ATH10K_DBG_PCI,
Michal Kazior726346f2014-02-27 18:50:04 +0200840 "pci tx item %d paddr 0x%08x len %d n_items %d\n",
841 i, items[i].paddr, items[i].len, n_items);
Michal Kazior7aa7a722014-08-25 12:09:38 +0200842 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci tx data: ",
Michal Kazior726346f2014-02-27 18:50:04 +0200843 items[i].vaddr, items[i].len);
844
845 err = ath10k_ce_send_nolock(ce_pipe,
846 items[i].transfer_context,
847 items[i].paddr,
848 items[i].len,
849 items[i].transfer_id,
850 CE_SEND_FLAG_GATHER);
851 if (err)
Michal Kazior08b8aa02014-05-26 12:02:59 +0200852 goto err;
Michal Kazior726346f2014-02-27 18:50:04 +0200853 }
854
855 /* `i` is equal to `n_items -1` after for() */
Kalle Valo5e3dd152013-06-12 20:52:10 +0300856
Michal Kazior7aa7a722014-08-25 12:09:38 +0200857 ath10k_dbg(ar, ATH10K_DBG_PCI,
Michal Kazior726346f2014-02-27 18:50:04 +0200858 "pci tx item %d paddr 0x%08x len %d n_items %d\n",
859 i, items[i].paddr, items[i].len, n_items);
Michal Kazior7aa7a722014-08-25 12:09:38 +0200860 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci tx data: ",
Michal Kazior726346f2014-02-27 18:50:04 +0200861 items[i].vaddr, items[i].len);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300862
Michal Kazior726346f2014-02-27 18:50:04 +0200863 err = ath10k_ce_send_nolock(ce_pipe,
864 items[i].transfer_context,
865 items[i].paddr,
866 items[i].len,
867 items[i].transfer_id,
868 0);
869 if (err)
Michal Kazior08b8aa02014-05-26 12:02:59 +0200870 goto err;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300871
Michal Kazior08b8aa02014-05-26 12:02:59 +0200872 spin_unlock_bh(&ar_pci->ce_lock);
873 return 0;
874
875err:
876 for (; i > 0; i--)
877 __ath10k_ce_send_revert(ce_pipe);
878
Michal Kazior726346f2014-02-27 18:50:04 +0200879 spin_unlock_bh(&ar_pci->ce_lock);
880 return err;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300881}
882
883static u16 ath10k_pci_hif_get_free_queue_number(struct ath10k *ar, u8 pipe)
884{
885 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo50f87a62014-03-28 09:32:52 +0200886
Michal Kazior7aa7a722014-08-25 12:09:38 +0200887 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif get free queue number\n");
Kalle Valo50f87a62014-03-28 09:32:52 +0200888
Michal Kazior3efcb3b2013-10-02 11:03:41 +0200889 return ath10k_ce_num_free_src_entries(ar_pci->pipe_info[pipe].ce_hdl);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300890}
891
Ben Greear384914b2014-08-25 08:37:32 +0300892static void ath10k_pci_dump_registers(struct ath10k *ar,
893 struct ath10k_fw_crash_data *crash_data)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300894{
Ben Greear384914b2014-08-25 08:37:32 +0300895 u32 i, reg_dump_values[REG_DUMP_COUNT_QCA988X] = {};
Kalle Valo5e3dd152013-06-12 20:52:10 +0300896 int ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300897
Ben Greear384914b2014-08-25 08:37:32 +0300898 lockdep_assert_held(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300899
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300900 ret = ath10k_pci_diag_read_hi(ar, &reg_dump_values[0],
901 hi_failure_state,
902 REG_DUMP_COUNT_QCA988X * sizeof(u32));
Michal Kazior1d2b48d2013-11-08 08:01:34 +0100903 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200904 ath10k_err(ar, "failed to read firmware dump area: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300905 return;
906 }
907
908 BUILD_BUG_ON(REG_DUMP_COUNT_QCA988X % 4);
909
Michal Kazior7aa7a722014-08-25 12:09:38 +0200910 ath10k_err(ar, "firmware register dump:\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +0300911 for (i = 0; i < REG_DUMP_COUNT_QCA988X; i += 4)
Michal Kazior7aa7a722014-08-25 12:09:38 +0200912 ath10k_err(ar, "[%02d]: 0x%08X 0x%08X 0x%08X 0x%08X\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +0300913 i,
914 reg_dump_values[i],
915 reg_dump_values[i + 1],
916 reg_dump_values[i + 2],
917 reg_dump_values[i + 3]);
Michal Kazioraffd3212013-07-16 09:54:35 +0200918
Michal Kazior1bbb1192014-08-25 12:13:14 +0200919 if (!crash_data)
920 return;
921
Ben Greear384914b2014-08-25 08:37:32 +0300922 /* crash_data is in little endian */
923 for (i = 0; i < REG_DUMP_COUNT_QCA988X; i++)
924 crash_data->registers[i] = cpu_to_le32(reg_dump_values[i]);
925}
926
Kalle Valo0e9848c2014-08-25 08:37:37 +0300927static void ath10k_pci_fw_crashed_dump(struct ath10k *ar)
Ben Greear384914b2014-08-25 08:37:32 +0300928{
929 struct ath10k_fw_crash_data *crash_data;
930 char uuid[50];
931
932 spin_lock_bh(&ar->data_lock);
933
934 crash_data = ath10k_debug_get_new_fw_crash_data(ar);
935
936 if (crash_data)
937 scnprintf(uuid, sizeof(uuid), "%pUl", &crash_data->uuid);
938 else
939 scnprintf(uuid, sizeof(uuid), "n/a");
940
Michal Kazior7aa7a722014-08-25 12:09:38 +0200941 ath10k_err(ar, "firmware crashed! (uuid %s)\n", uuid);
Kalle Valo8a0c7972014-08-25 08:37:45 +0300942 ath10k_print_driver_info(ar);
Ben Greear384914b2014-08-25 08:37:32 +0300943 ath10k_pci_dump_registers(ar, crash_data);
944
Ben Greear384914b2014-08-25 08:37:32 +0300945 spin_unlock_bh(&ar->data_lock);
946
Michal Kazior5e90de82013-10-16 16:46:05 +0300947 queue_work(ar->workqueue, &ar->restart_work);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300948}
949
950static void ath10k_pci_hif_send_complete_check(struct ath10k *ar, u8 pipe,
951 int force)
952{
Michal Kazior7aa7a722014-08-25 12:09:38 +0200953 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif send complete check\n");
Kalle Valo50f87a62014-03-28 09:32:52 +0200954
Kalle Valo5e3dd152013-06-12 20:52:10 +0300955 if (!force) {
956 int resources;
957 /*
958 * Decide whether to actually poll for completions, or just
959 * wait for a later chance.
960 * If there seem to be plenty of resources left, then just wait
961 * since checking involves reading a CE register, which is a
962 * relatively expensive operation.
963 */
964 resources = ath10k_pci_hif_get_free_queue_number(ar, pipe);
965
966 /*
967 * If at least 50% of the total resources are still available,
968 * don't bother checking again yet.
969 */
970 if (resources > (host_ce_config_wlan[pipe].src_nentries >> 1))
971 return;
972 }
973 ath10k_ce_per_engine_service(ar, pipe);
974}
975
Michal Kaziore799bbf2013-07-05 16:15:12 +0300976static void ath10k_pci_hif_set_callbacks(struct ath10k *ar,
977 struct ath10k_hif_cb *callbacks)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300978{
979 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
980
Michal Kazior7aa7a722014-08-25 12:09:38 +0200981 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif set callbacks\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +0300982
983 memcpy(&ar_pci->msg_callbacks_current, callbacks,
984 sizeof(ar_pci->msg_callbacks_current));
985}
986
Michal Kazior96a9d0d2013-11-08 08:01:25 +0100987static void ath10k_pci_kill_tasklet(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300988{
989 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300990 int i;
991
Kalle Valo5e3dd152013-06-12 20:52:10 +0300992 tasklet_kill(&ar_pci->intr_tq);
Michal Kazior103d4f52013-11-08 08:01:24 +0100993 tasklet_kill(&ar_pci->msi_fw_err);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300994
995 for (i = 0; i < CE_COUNT; i++)
996 tasklet_kill(&ar_pci->pipe_info[i].intr);
Michal Kazior728f95e2014-08-22 14:33:14 +0200997
998 del_timer_sync(&ar_pci->rx_post_retry);
Michal Kazior96a9d0d2013-11-08 08:01:25 +0100999}
1000
Kalle Valo5e3dd152013-06-12 20:52:10 +03001001/* TODO - temporary mapping while we have too few CE's */
1002static int ath10k_pci_hif_map_service_to_pipe(struct ath10k *ar,
1003 u16 service_id, u8 *ul_pipe,
1004 u8 *dl_pipe, int *ul_is_polled,
1005 int *dl_is_polled)
1006{
1007 int ret = 0;
1008
Michal Kazior7aa7a722014-08-25 12:09:38 +02001009 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif map service\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001010
Kalle Valo5e3dd152013-06-12 20:52:10 +03001011 /* polling for received messages not supported */
1012 *dl_is_polled = 0;
1013
1014 switch (service_id) {
1015 case ATH10K_HTC_SVC_ID_HTT_DATA_MSG:
1016 /*
1017 * Host->target HTT gets its own pipe, so it can be polled
1018 * while other pipes are interrupt driven.
1019 */
1020 *ul_pipe = 4;
1021 /*
1022 * Use the same target->host pipe for HTC ctrl, HTC raw
1023 * streams, and HTT.
1024 */
1025 *dl_pipe = 1;
1026 break;
1027
1028 case ATH10K_HTC_SVC_ID_RSVD_CTRL:
1029 case ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS:
1030 /*
1031 * Note: HTC_RAW_STREAMS_SVC is currently unused, and
1032 * HTC_CTRL_RSVD_SVC could share the same pipe as the
1033 * WMI services. So, if another CE is needed, change
1034 * this to *ul_pipe = 3, which frees up CE 0.
1035 */
1036 /* *ul_pipe = 3; */
1037 *ul_pipe = 0;
1038 *dl_pipe = 1;
1039 break;
1040
1041 case ATH10K_HTC_SVC_ID_WMI_DATA_BK:
1042 case ATH10K_HTC_SVC_ID_WMI_DATA_BE:
1043 case ATH10K_HTC_SVC_ID_WMI_DATA_VI:
1044 case ATH10K_HTC_SVC_ID_WMI_DATA_VO:
1045
1046 case ATH10K_HTC_SVC_ID_WMI_CONTROL:
1047 *ul_pipe = 3;
1048 *dl_pipe = 2;
1049 break;
1050
1051 /* pipe 5 unused */
1052 /* pipe 6 reserved */
1053 /* pipe 7 reserved */
1054
1055 default:
1056 ret = -1;
1057 break;
1058 }
1059 *ul_is_polled =
1060 (host_ce_config_wlan[*ul_pipe].flags & CE_ATTR_DIS_INTR) != 0;
1061
1062 return ret;
1063}
1064
1065static void ath10k_pci_hif_get_default_pipe(struct ath10k *ar,
1066 u8 *ul_pipe, u8 *dl_pipe)
1067{
1068 int ul_is_polled, dl_is_polled;
1069
Michal Kazior7aa7a722014-08-25 12:09:38 +02001070 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif get default pipe\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001071
Kalle Valo5e3dd152013-06-12 20:52:10 +03001072 (void)ath10k_pci_hif_map_service_to_pipe(ar,
1073 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1074 ul_pipe,
1075 dl_pipe,
1076 &ul_is_polled,
1077 &dl_is_polled);
1078}
1079
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001080static void ath10k_pci_irq_disable(struct ath10k *ar)
1081{
1082 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1083 int i;
1084
1085 ath10k_ce_disable_interrupts(ar);
1086
1087 /* Regardless how many interrupts were assigned for MSI the first one
1088 * is always used for firmware indications (crashes). There's no way to
1089 * mask the irq in the device so call disable_irq(). Legacy (shared)
1090 * interrupts can be masked on the device though.
1091 */
1092 if (ar_pci->num_msi_intrs > 0)
1093 disable_irq(ar_pci->pdev->irq);
1094 else
1095 ath10k_pci_disable_and_clear_legacy_irq(ar);
1096
1097 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
1098 synchronize_irq(ar_pci->pdev->irq + i);
1099}
1100
1101static void ath10k_pci_irq_enable(struct ath10k *ar)
1102{
1103 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1104
1105 ath10k_ce_enable_interrupts(ar);
1106
1107 /* See comment in ath10k_pci_irq_disable() */
1108 if (ar_pci->num_msi_intrs > 0)
1109 enable_irq(ar_pci->pdev->irq);
1110 else
1111 ath10k_pci_enable_legacy_irq(ar);
1112}
1113
Kalle Valo5e3dd152013-06-12 20:52:10 +03001114static int ath10k_pci_hif_start(struct ath10k *ar)
1115{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001116 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif start\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001117
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001118 ath10k_pci_irq_enable(ar);
Michal Kazior728f95e2014-08-22 14:33:14 +02001119 ath10k_pci_rx_post(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001120
Kalle Valo5e3dd152013-06-12 20:52:10 +03001121 return 0;
1122}
1123
Michal Kazior87263e52013-08-27 13:08:01 +02001124static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe *pipe_info)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001125{
1126 struct ath10k *ar;
1127 struct ath10k_pci *ar_pci;
Michal Kazior2aa39112013-08-27 13:08:02 +02001128 struct ath10k_ce_pipe *ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001129 u32 buf_sz;
1130 struct sk_buff *netbuf;
1131 u32 ce_data;
1132
1133 buf_sz = pipe_info->buf_sz;
1134
1135 /* Unused Copy Engine */
1136 if (buf_sz == 0)
1137 return;
1138
1139 ar = pipe_info->hif_ce_state;
1140 ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001141 ce_hdl = pipe_info->ce_hdl;
1142
1143 while (ath10k_ce_revoke_recv_next(ce_hdl, (void **)&netbuf,
1144 &ce_data) == 0) {
1145 dma_unmap_single(ar->dev, ATH10K_SKB_CB(netbuf)->paddr,
1146 netbuf->len + skb_tailroom(netbuf),
1147 DMA_FROM_DEVICE);
1148 dev_kfree_skb_any(netbuf);
1149 }
1150}
1151
Michal Kazior87263e52013-08-27 13:08:01 +02001152static void ath10k_pci_tx_pipe_cleanup(struct ath10k_pci_pipe *pipe_info)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001153{
1154 struct ath10k *ar;
1155 struct ath10k_pci *ar_pci;
Michal Kazior2aa39112013-08-27 13:08:02 +02001156 struct ath10k_ce_pipe *ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001157 struct sk_buff *netbuf;
1158 u32 ce_data;
1159 unsigned int nbytes;
1160 unsigned int id;
1161 u32 buf_sz;
1162
1163 buf_sz = pipe_info->buf_sz;
1164
1165 /* Unused Copy Engine */
1166 if (buf_sz == 0)
1167 return;
1168
1169 ar = pipe_info->hif_ce_state;
1170 ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001171 ce_hdl = pipe_info->ce_hdl;
1172
1173 while (ath10k_ce_cancel_send_next(ce_hdl, (void **)&netbuf,
1174 &ce_data, &nbytes, &id) == 0) {
Michal Kaziora16942e2014-02-27 18:50:04 +02001175 /* no need to call tx completion for NULL pointers */
1176 if (!netbuf)
Michal Kazior2415fc12013-11-08 08:01:32 +01001177 continue;
Michal Kazior2415fc12013-11-08 08:01:32 +01001178
Kalle Valoe9bb0aa2013-09-08 18:36:11 +03001179 ar_pci->msg_callbacks_current.tx_completion(ar,
1180 netbuf,
1181 id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001182 }
1183}
1184
1185/*
1186 * Cleanup residual buffers for device shutdown:
1187 * buffers that were enqueued for receive
1188 * buffers that were to be sent
1189 * Note: Buffers that had completed but which were
1190 * not yet processed are on a completion queue. They
1191 * are handled when the completion thread shuts down.
1192 */
1193static void ath10k_pci_buffer_cleanup(struct ath10k *ar)
1194{
1195 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1196 int pipe_num;
1197
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001198 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Michal Kazior87263e52013-08-27 13:08:01 +02001199 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001200
1201 pipe_info = &ar_pci->pipe_info[pipe_num];
1202 ath10k_pci_rx_pipe_cleanup(pipe_info);
1203 ath10k_pci_tx_pipe_cleanup(pipe_info);
1204 }
1205}
1206
1207static void ath10k_pci_ce_deinit(struct ath10k *ar)
1208{
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001209 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001210
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001211 for (i = 0; i < CE_COUNT; i++)
1212 ath10k_ce_deinit_pipe(ar, i);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001213}
1214
Michal Kazior728f95e2014-08-22 14:33:14 +02001215static void ath10k_pci_flush(struct ath10k *ar)
1216{
1217 ath10k_pci_kill_tasklet(ar);
1218 ath10k_pci_buffer_cleanup(ar);
1219}
1220
Kalle Valo5e3dd152013-06-12 20:52:10 +03001221static void ath10k_pci_hif_stop(struct ath10k *ar)
1222{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001223 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif stop\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001224
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001225 ath10k_pci_irq_disable(ar);
Michal Kazior728f95e2014-08-22 14:33:14 +02001226 ath10k_pci_flush(ar);
Michal Kazior32270b62013-08-02 09:15:47 +02001227
Michal Kazior10d23db2014-08-22 14:33:15 +02001228 /* Most likely the device has HTT Rx ring configured. The only way to
1229 * prevent the device from accessing (and possible corrupting) host
1230 * memory is to reset the chip now.
1231 */
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001232 ath10k_pci_warm_reset(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001233}
1234
1235static int ath10k_pci_hif_exchange_bmi_msg(struct ath10k *ar,
1236 void *req, u32 req_len,
1237 void *resp, u32 *resp_len)
1238{
1239 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2aa39112013-08-27 13:08:02 +02001240 struct ath10k_pci_pipe *pci_tx = &ar_pci->pipe_info[BMI_CE_NUM_TO_TARG];
1241 struct ath10k_pci_pipe *pci_rx = &ar_pci->pipe_info[BMI_CE_NUM_TO_HOST];
1242 struct ath10k_ce_pipe *ce_tx = pci_tx->ce_hdl;
1243 struct ath10k_ce_pipe *ce_rx = pci_rx->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001244 dma_addr_t req_paddr = 0;
1245 dma_addr_t resp_paddr = 0;
1246 struct bmi_xfer xfer = {};
1247 void *treq, *tresp = NULL;
1248 int ret = 0;
1249
Michal Kazior85622cd2013-11-25 14:06:22 +01001250 might_sleep();
1251
Kalle Valo5e3dd152013-06-12 20:52:10 +03001252 if (resp && !resp_len)
1253 return -EINVAL;
1254
1255 if (resp && resp_len && *resp_len == 0)
1256 return -EINVAL;
1257
1258 treq = kmemdup(req, req_len, GFP_KERNEL);
1259 if (!treq)
1260 return -ENOMEM;
1261
1262 req_paddr = dma_map_single(ar->dev, treq, req_len, DMA_TO_DEVICE);
1263 ret = dma_mapping_error(ar->dev, req_paddr);
1264 if (ret)
1265 goto err_dma;
1266
1267 if (resp && resp_len) {
1268 tresp = kzalloc(*resp_len, GFP_KERNEL);
1269 if (!tresp) {
1270 ret = -ENOMEM;
1271 goto err_req;
1272 }
1273
1274 resp_paddr = dma_map_single(ar->dev, tresp, *resp_len,
1275 DMA_FROM_DEVICE);
1276 ret = dma_mapping_error(ar->dev, resp_paddr);
1277 if (ret)
1278 goto err_req;
1279
1280 xfer.wait_for_resp = true;
1281 xfer.resp_len = 0;
1282
Michal Kazior728f95e2014-08-22 14:33:14 +02001283 ath10k_ce_rx_post_buf(ce_rx, &xfer, resp_paddr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001284 }
1285
Kalle Valo5e3dd152013-06-12 20:52:10 +03001286 ret = ath10k_ce_send(ce_tx, &xfer, req_paddr, req_len, -1, 0);
1287 if (ret)
1288 goto err_resp;
1289
Michal Kazior85622cd2013-11-25 14:06:22 +01001290 ret = ath10k_pci_bmi_wait(ce_tx, ce_rx, &xfer);
1291 if (ret) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001292 u32 unused_buffer;
1293 unsigned int unused_nbytes;
1294 unsigned int unused_id;
1295
Kalle Valo5e3dd152013-06-12 20:52:10 +03001296 ath10k_ce_cancel_send_next(ce_tx, NULL, &unused_buffer,
1297 &unused_nbytes, &unused_id);
1298 } else {
1299 /* non-zero means we did not time out */
1300 ret = 0;
1301 }
1302
1303err_resp:
1304 if (resp) {
1305 u32 unused_buffer;
1306
1307 ath10k_ce_revoke_recv_next(ce_rx, NULL, &unused_buffer);
1308 dma_unmap_single(ar->dev, resp_paddr,
1309 *resp_len, DMA_FROM_DEVICE);
1310 }
1311err_req:
1312 dma_unmap_single(ar->dev, req_paddr, req_len, DMA_TO_DEVICE);
1313
1314 if (ret == 0 && resp_len) {
1315 *resp_len = min(*resp_len, xfer.resp_len);
1316 memcpy(resp, tresp, xfer.resp_len);
1317 }
1318err_dma:
1319 kfree(treq);
1320 kfree(tresp);
1321
1322 return ret;
1323}
1324
Michal Kazior5440ce22013-09-03 15:09:58 +02001325static void ath10k_pci_bmi_send_done(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001326{
Michal Kazior5440ce22013-09-03 15:09:58 +02001327 struct bmi_xfer *xfer;
1328 u32 ce_data;
1329 unsigned int nbytes;
1330 unsigned int transfer_id;
1331
1332 if (ath10k_ce_completed_send_next(ce_state, (void **)&xfer, &ce_data,
1333 &nbytes, &transfer_id))
1334 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001335
Michal Kazior2374b182014-07-14 16:25:25 +03001336 xfer->tx_done = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001337}
1338
Michal Kazior5440ce22013-09-03 15:09:58 +02001339static void ath10k_pci_bmi_recv_data(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001340{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001341 struct ath10k *ar = ce_state->ar;
Michal Kazior5440ce22013-09-03 15:09:58 +02001342 struct bmi_xfer *xfer;
1343 u32 ce_data;
1344 unsigned int nbytes;
1345 unsigned int transfer_id;
1346 unsigned int flags;
1347
1348 if (ath10k_ce_completed_recv_next(ce_state, (void **)&xfer, &ce_data,
1349 &nbytes, &transfer_id, &flags))
1350 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001351
1352 if (!xfer->wait_for_resp) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001353 ath10k_warn(ar, "unexpected: BMI data received; ignoring\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001354 return;
1355 }
1356
1357 xfer->resp_len = nbytes;
Michal Kazior2374b182014-07-14 16:25:25 +03001358 xfer->rx_done = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001359}
1360
Michal Kazior85622cd2013-11-25 14:06:22 +01001361static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
1362 struct ath10k_ce_pipe *rx_pipe,
1363 struct bmi_xfer *xfer)
1364{
1365 unsigned long timeout = jiffies + BMI_COMMUNICATION_TIMEOUT_HZ;
1366
1367 while (time_before_eq(jiffies, timeout)) {
1368 ath10k_pci_bmi_send_done(tx_pipe);
1369 ath10k_pci_bmi_recv_data(rx_pipe);
1370
Michal Kazior2374b182014-07-14 16:25:25 +03001371 if (xfer->tx_done && (xfer->rx_done == xfer->wait_for_resp))
Michal Kazior85622cd2013-11-25 14:06:22 +01001372 return 0;
1373
1374 schedule();
1375 }
1376
1377 return -ETIMEDOUT;
1378}
1379
Kalle Valo5e3dd152013-06-12 20:52:10 +03001380/*
1381 * Map from service/endpoint to Copy Engine.
1382 * This table is derived from the CE_PCI TABLE, above.
1383 * It is passed to the Target at startup for use by firmware.
1384 */
1385static const struct service_to_pipe target_service_to_ce_map_wlan[] = {
1386 {
1387 ATH10K_HTC_SVC_ID_WMI_DATA_VO,
1388 PIPEDIR_OUT, /* out = UL = host -> target */
1389 3,
1390 },
1391 {
1392 ATH10K_HTC_SVC_ID_WMI_DATA_VO,
1393 PIPEDIR_IN, /* in = DL = target -> host */
1394 2,
1395 },
1396 {
1397 ATH10K_HTC_SVC_ID_WMI_DATA_BK,
1398 PIPEDIR_OUT, /* out = UL = host -> target */
1399 3,
1400 },
1401 {
1402 ATH10K_HTC_SVC_ID_WMI_DATA_BK,
1403 PIPEDIR_IN, /* in = DL = target -> host */
1404 2,
1405 },
1406 {
1407 ATH10K_HTC_SVC_ID_WMI_DATA_BE,
1408 PIPEDIR_OUT, /* out = UL = host -> target */
1409 3,
1410 },
1411 {
1412 ATH10K_HTC_SVC_ID_WMI_DATA_BE,
1413 PIPEDIR_IN, /* in = DL = target -> host */
1414 2,
1415 },
1416 {
1417 ATH10K_HTC_SVC_ID_WMI_DATA_VI,
1418 PIPEDIR_OUT, /* out = UL = host -> target */
1419 3,
1420 },
1421 {
1422 ATH10K_HTC_SVC_ID_WMI_DATA_VI,
1423 PIPEDIR_IN, /* in = DL = target -> host */
1424 2,
1425 },
1426 {
1427 ATH10K_HTC_SVC_ID_WMI_CONTROL,
1428 PIPEDIR_OUT, /* out = UL = host -> target */
1429 3,
1430 },
1431 {
1432 ATH10K_HTC_SVC_ID_WMI_CONTROL,
1433 PIPEDIR_IN, /* in = DL = target -> host */
1434 2,
1435 },
1436 {
1437 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1438 PIPEDIR_OUT, /* out = UL = host -> target */
1439 0, /* could be moved to 3 (share with WMI) */
1440 },
1441 {
1442 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1443 PIPEDIR_IN, /* in = DL = target -> host */
1444 1,
1445 },
1446 {
1447 ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS, /* not currently used */
1448 PIPEDIR_OUT, /* out = UL = host -> target */
1449 0,
1450 },
1451 {
1452 ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS, /* not currently used */
1453 PIPEDIR_IN, /* in = DL = target -> host */
1454 1,
1455 },
1456 {
1457 ATH10K_HTC_SVC_ID_HTT_DATA_MSG,
1458 PIPEDIR_OUT, /* out = UL = host -> target */
1459 4,
1460 },
1461 {
1462 ATH10K_HTC_SVC_ID_HTT_DATA_MSG,
1463 PIPEDIR_IN, /* in = DL = target -> host */
1464 1,
1465 },
1466
1467 /* (Additions here) */
1468
1469 { /* Must be last */
1470 0,
1471 0,
1472 0,
1473 },
1474};
1475
1476/*
1477 * Send an interrupt to the device to wake up the Target CPU
1478 * so it has an opportunity to notice any changed state.
1479 */
1480static int ath10k_pci_wake_target_cpu(struct ath10k *ar)
1481{
1482 int ret;
1483 u32 core_ctrl;
1484
1485 ret = ath10k_pci_diag_read_access(ar, SOC_CORE_BASE_ADDRESS |
1486 CORE_CTRL_ADDRESS,
1487 &core_ctrl);
1488 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001489 ath10k_warn(ar, "failed to read core_ctrl: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001490 return ret;
1491 }
1492
1493 /* A_INUM_FIRMWARE interrupt to Target CPU */
1494 core_ctrl |= CORE_CTRL_CPU_INTR_MASK;
1495
1496 ret = ath10k_pci_diag_write_access(ar, SOC_CORE_BASE_ADDRESS |
1497 CORE_CTRL_ADDRESS,
1498 core_ctrl);
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001499 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001500 ath10k_warn(ar, "failed to set target CPU interrupt mask: %d\n",
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001501 ret);
1502 return ret;
1503 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001504
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001505 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001506}
1507
1508static int ath10k_pci_init_config(struct ath10k *ar)
1509{
1510 u32 interconnect_targ_addr;
1511 u32 pcie_state_targ_addr = 0;
1512 u32 pipe_cfg_targ_addr = 0;
1513 u32 svc_to_pipe_map = 0;
1514 u32 pcie_config_flags = 0;
1515 u32 ealloc_value;
1516 u32 ealloc_targ_addr;
1517 u32 flag2_value;
1518 u32 flag2_targ_addr;
1519 int ret = 0;
1520
1521 /* Download to Target the CE Config and the service-to-CE map */
1522 interconnect_targ_addr =
1523 host_interest_item_address(HI_ITEM(hi_interconnect_state));
1524
1525 /* Supply Target-side CE configuration */
1526 ret = ath10k_pci_diag_read_access(ar, interconnect_targ_addr,
1527 &pcie_state_targ_addr);
1528 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001529 ath10k_err(ar, "Failed to get pcie state addr: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001530 return ret;
1531 }
1532
1533 if (pcie_state_targ_addr == 0) {
1534 ret = -EIO;
Michal Kazior7aa7a722014-08-25 12:09:38 +02001535 ath10k_err(ar, "Invalid pcie state addr\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001536 return ret;
1537 }
1538
1539 ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
1540 offsetof(struct pcie_state,
1541 pipe_cfg_addr),
1542 &pipe_cfg_targ_addr);
1543 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001544 ath10k_err(ar, "Failed to get pipe cfg addr: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001545 return ret;
1546 }
1547
1548 if (pipe_cfg_targ_addr == 0) {
1549 ret = -EIO;
Michal Kazior7aa7a722014-08-25 12:09:38 +02001550 ath10k_err(ar, "Invalid pipe cfg addr\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001551 return ret;
1552 }
1553
1554 ret = ath10k_pci_diag_write_mem(ar, pipe_cfg_targ_addr,
1555 target_ce_config_wlan,
1556 sizeof(target_ce_config_wlan));
1557
1558 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001559 ath10k_err(ar, "Failed to write pipe cfg: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001560 return ret;
1561 }
1562
1563 ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
1564 offsetof(struct pcie_state,
1565 svc_to_pipe_map),
1566 &svc_to_pipe_map);
1567 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001568 ath10k_err(ar, "Failed to get svc/pipe map: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001569 return ret;
1570 }
1571
1572 if (svc_to_pipe_map == 0) {
1573 ret = -EIO;
Michal Kazior7aa7a722014-08-25 12:09:38 +02001574 ath10k_err(ar, "Invalid svc_to_pipe map\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001575 return ret;
1576 }
1577
1578 ret = ath10k_pci_diag_write_mem(ar, svc_to_pipe_map,
1579 target_service_to_ce_map_wlan,
1580 sizeof(target_service_to_ce_map_wlan));
1581 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001582 ath10k_err(ar, "Failed to write svc/pipe map: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001583 return ret;
1584 }
1585
1586 ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
1587 offsetof(struct pcie_state,
1588 config_flags),
1589 &pcie_config_flags);
1590 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001591 ath10k_err(ar, "Failed to get pcie config_flags: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001592 return ret;
1593 }
1594
1595 pcie_config_flags &= ~PCIE_CONFIG_FLAG_ENABLE_L1;
1596
1597 ret = ath10k_pci_diag_write_mem(ar, pcie_state_targ_addr +
1598 offsetof(struct pcie_state, config_flags),
1599 &pcie_config_flags,
1600 sizeof(pcie_config_flags));
1601 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001602 ath10k_err(ar, "Failed to write pcie config_flags: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001603 return ret;
1604 }
1605
1606 /* configure early allocation */
1607 ealloc_targ_addr = host_interest_item_address(HI_ITEM(hi_early_alloc));
1608
1609 ret = ath10k_pci_diag_read_access(ar, ealloc_targ_addr, &ealloc_value);
1610 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001611 ath10k_err(ar, "Faile to get early alloc val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001612 return ret;
1613 }
1614
1615 /* first bank is switched to IRAM */
1616 ealloc_value |= ((HI_EARLY_ALLOC_MAGIC << HI_EARLY_ALLOC_MAGIC_SHIFT) &
1617 HI_EARLY_ALLOC_MAGIC_MASK);
1618 ealloc_value |= ((1 << HI_EARLY_ALLOC_IRAM_BANKS_SHIFT) &
1619 HI_EARLY_ALLOC_IRAM_BANKS_MASK);
1620
1621 ret = ath10k_pci_diag_write_access(ar, ealloc_targ_addr, ealloc_value);
1622 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001623 ath10k_err(ar, "Failed to set early alloc val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001624 return ret;
1625 }
1626
1627 /* Tell Target to proceed with initialization */
1628 flag2_targ_addr = host_interest_item_address(HI_ITEM(hi_option_flag2));
1629
1630 ret = ath10k_pci_diag_read_access(ar, flag2_targ_addr, &flag2_value);
1631 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001632 ath10k_err(ar, "Failed to get option val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001633 return ret;
1634 }
1635
1636 flag2_value |= HI_OPTION_EARLY_CFG_DONE;
1637
1638 ret = ath10k_pci_diag_write_access(ar, flag2_targ_addr, flag2_value);
1639 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001640 ath10k_err(ar, "Failed to set option val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001641 return ret;
1642 }
1643
1644 return 0;
1645}
1646
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001647static int ath10k_pci_alloc_ce(struct ath10k *ar)
1648{
1649 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001650
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001651 for (i = 0; i < CE_COUNT; i++) {
1652 ret = ath10k_ce_alloc_pipe(ar, i, &host_ce_config_wlan[i]);
1653 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001654 ath10k_err(ar, "failed to allocate copy engine pipe %d: %d\n",
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001655 i, ret);
1656 return ret;
1657 }
1658 }
1659
1660 return 0;
1661}
1662
1663static void ath10k_pci_free_ce(struct ath10k *ar)
1664{
1665 int i;
1666
1667 for (i = 0; i < CE_COUNT; i++)
1668 ath10k_ce_free_pipe(ar, i);
1669}
Kalle Valo5e3dd152013-06-12 20:52:10 +03001670
1671static int ath10k_pci_ce_init(struct ath10k *ar)
1672{
1673 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +02001674 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001675 const struct ce_attr *attr;
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001676 int pipe_num, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001677
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001678 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001679 pipe_info = &ar_pci->pipe_info[pipe_num];
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001680 pipe_info->ce_hdl = &ar_pci->ce_states[pipe_num];
Kalle Valo5e3dd152013-06-12 20:52:10 +03001681 pipe_info->pipe_num = pipe_num;
1682 pipe_info->hif_ce_state = ar;
1683 attr = &host_ce_config_wlan[pipe_num];
1684
Michal Kazior145cc122014-08-22 14:23:32 +02001685 ret = ath10k_ce_init_pipe(ar, pipe_num, attr,
1686 ath10k_pci_ce_send_done,
1687 ath10k_pci_ce_recv_data);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001688 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001689 ath10k_err(ar, "failed to initialize copy engine pipe %d: %d\n",
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001690 pipe_num, ret);
1691 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001692 }
1693
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001694 if (pipe_num == CE_COUNT - 1) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001695 /*
1696 * Reserve the ultimate CE for
1697 * diagnostic Window support
1698 */
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001699 ar_pci->ce_diag = pipe_info->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001700 continue;
1701 }
1702
1703 pipe_info->buf_sz = (size_t) (attr->src_sz_max);
1704 }
1705
Kalle Valo5e3dd152013-06-12 20:52:10 +03001706 return 0;
1707}
1708
Michal Kazior5c771e72014-08-22 14:23:34 +02001709static bool ath10k_pci_has_fw_crashed(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001710{
Michal Kazior5c771e72014-08-22 14:23:34 +02001711 return ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS) &
1712 FW_IND_EVENT_PENDING;
1713}
Kalle Valo5e3dd152013-06-12 20:52:10 +03001714
Michal Kazior5c771e72014-08-22 14:23:34 +02001715static void ath10k_pci_fw_crashed_clear(struct ath10k *ar)
1716{
1717 u32 val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001718
Michal Kazior5c771e72014-08-22 14:23:34 +02001719 val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
1720 val &= ~FW_IND_EVENT_PENDING;
1721 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001722}
1723
Michal Kaziorde013572014-05-14 16:56:16 +03001724/* this function effectively clears target memory controller assert line */
1725static void ath10k_pci_warm_reset_si0(struct ath10k *ar)
1726{
1727 u32 val;
1728
1729 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1730 ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
1731 val | SOC_RESET_CONTROL_SI0_RST_MASK);
1732 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1733
1734 msleep(10);
1735
1736 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1737 ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
1738 val & ~SOC_RESET_CONTROL_SI0_RST_MASK);
1739 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1740
1741 msleep(10);
1742}
1743
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001744static int ath10k_pci_warm_reset(struct ath10k *ar)
1745{
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001746 u32 val;
1747
Michal Kazior7aa7a722014-08-25 12:09:38 +02001748 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot warm reset\n");
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001749
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001750 /* debug */
1751 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1752 PCIE_INTR_CAUSE_ADDRESS);
Michal Kazior7aa7a722014-08-25 12:09:38 +02001753 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot host cpu intr cause: 0x%08x\n",
1754 val);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001755
1756 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1757 CPU_INTR_ADDRESS);
Michal Kazior7aa7a722014-08-25 12:09:38 +02001758 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target cpu intr cause: 0x%08x\n",
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001759 val);
1760
1761 /* disable pending irqs */
1762 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
1763 PCIE_INTR_ENABLE_ADDRESS, 0);
1764
1765 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
1766 PCIE_INTR_CLR_ADDRESS, ~0);
1767
1768 msleep(100);
1769
1770 /* clear fw indicator */
Kalle Valob39712c2014-03-28 09:32:46 +02001771 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, 0);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001772
1773 /* clear target LF timer interrupts */
1774 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1775 SOC_LF_TIMER_CONTROL0_ADDRESS);
1776 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS +
1777 SOC_LF_TIMER_CONTROL0_ADDRESS,
1778 val & ~SOC_LF_TIMER_CONTROL0_ENABLE_MASK);
1779
1780 /* reset CE */
1781 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1782 SOC_RESET_CONTROL_ADDRESS);
1783 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1784 val | SOC_RESET_CONTROL_CE_RST_MASK);
1785 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1786 SOC_RESET_CONTROL_ADDRESS);
1787 msleep(10);
1788
1789 /* unreset CE */
1790 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1791 val & ~SOC_RESET_CONTROL_CE_RST_MASK);
1792 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1793 SOC_RESET_CONTROL_ADDRESS);
1794 msleep(10);
1795
Michal Kaziorde013572014-05-14 16:56:16 +03001796 ath10k_pci_warm_reset_si0(ar);
1797
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001798 /* debug */
1799 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1800 PCIE_INTR_CAUSE_ADDRESS);
Michal Kazior7aa7a722014-08-25 12:09:38 +02001801 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot host cpu intr cause: 0x%08x\n",
1802 val);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001803
1804 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1805 CPU_INTR_ADDRESS);
Michal Kazior7aa7a722014-08-25 12:09:38 +02001806 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target cpu intr cause: 0x%08x\n",
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001807 val);
1808
1809 /* CPU warm reset */
1810 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1811 SOC_RESET_CONTROL_ADDRESS);
1812 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1813 val | SOC_RESET_CONTROL_CPU_WARM_RST_MASK);
1814
1815 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1816 SOC_RESET_CONTROL_ADDRESS);
Michal Kazior7aa7a722014-08-25 12:09:38 +02001817 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target reset state: 0x%08x\n",
1818 val);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001819
1820 msleep(100);
1821
Michal Kazior7aa7a722014-08-25 12:09:38 +02001822 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot warm reset complete\n");
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001823
Michal Kaziorc0c378f2014-08-07 11:03:28 +02001824 return 0;
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001825}
1826
1827static int __ath10k_pci_hif_power_up(struct ath10k *ar, bool cold_reset)
Michal Kazior8c5c5362013-07-16 09:38:50 +02001828{
1829 int ret;
1830
1831 /*
1832 * Bring the target up cleanly.
1833 *
1834 * The target may be in an undefined state with an AUX-powered Target
1835 * and a Host in WoW mode. If the Host crashes, loses power, or is
1836 * restarted (without unloading the driver) then the Target is left
1837 * (aux) powered and running. On a subsequent driver load, the Target
1838 * is in an unexpected state. We try to catch that here in order to
1839 * reset the Target and retry the probe.
1840 */
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001841 if (cold_reset)
1842 ret = ath10k_pci_cold_reset(ar);
1843 else
1844 ret = ath10k_pci_warm_reset(ar);
1845
Michal Kazior5b2589f2013-11-08 08:01:30 +01001846 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001847 ath10k_err(ar, "failed to reset target: %d\n", ret);
Michal Kazior98563d52013-11-08 08:01:33 +01001848 goto err;
Michal Kazior5b2589f2013-11-08 08:01:30 +01001849 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02001850
Michal Kazior8c5c5362013-07-16 09:38:50 +02001851 ret = ath10k_pci_ce_init(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001852 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001853 ath10k_err(ar, "failed to initialize CE: %d\n", ret);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02001854 goto err;
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001855 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02001856
Michal Kazior98563d52013-11-08 08:01:33 +01001857 ret = ath10k_pci_wait_for_target_init(ar);
1858 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001859 ath10k_err(ar, "failed to wait for target to init: %d\n", ret);
Michal Kazior5c771e72014-08-22 14:23:34 +02001860 goto err_ce;
Michal Kazior98563d52013-11-08 08:01:33 +01001861 }
1862
1863 ret = ath10k_pci_init_config(ar);
1864 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001865 ath10k_err(ar, "failed to setup init config: %d\n", ret);
Michal Kazior5c771e72014-08-22 14:23:34 +02001866 goto err_ce;
Michal Kazior98563d52013-11-08 08:01:33 +01001867 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02001868
1869 ret = ath10k_pci_wake_target_cpu(ar);
1870 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001871 ath10k_err(ar, "could not wake up target CPU: %d\n", ret);
Michal Kazior5c771e72014-08-22 14:23:34 +02001872 goto err_ce;
Michal Kazior8c5c5362013-07-16 09:38:50 +02001873 }
1874
1875 return 0;
1876
1877err_ce:
1878 ath10k_pci_ce_deinit(ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001879 ath10k_pci_warm_reset(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001880err:
1881 return ret;
1882}
1883
Michal Kazior61c95ce2014-05-14 16:56:16 +03001884static int ath10k_pci_hif_power_up_warm(struct ath10k *ar)
1885{
1886 int i, ret;
1887
1888 /*
1889 * Sometime warm reset succeeds after retries.
1890 *
1891 * FIXME: It might be possible to tune ath10k_pci_warm_reset() to work
1892 * at first try.
1893 */
1894 for (i = 0; i < ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS; i++) {
1895 ret = __ath10k_pci_hif_power_up(ar, false);
1896 if (ret == 0)
1897 break;
1898
Michal Kazior7aa7a722014-08-25 12:09:38 +02001899 ath10k_warn(ar, "failed to warm reset (attempt %d out of %d): %d\n",
Michal Kazior61c95ce2014-05-14 16:56:16 +03001900 i + 1, ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS, ret);
1901 }
1902
1903 return ret;
1904}
1905
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001906static int ath10k_pci_hif_power_up(struct ath10k *ar)
1907{
1908 int ret;
1909
Michal Kazior7aa7a722014-08-25 12:09:38 +02001910 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif power up\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001911
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001912 /*
1913 * Hardware CUS232 version 2 has some issues with cold reset and the
1914 * preferred (and safer) way to perform a device reset is through a
1915 * warm reset.
1916 *
Michal Kazior61c95ce2014-05-14 16:56:16 +03001917 * Warm reset doesn't always work though so fall back to cold reset may
1918 * be necessary.
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001919 */
Michal Kazior61c95ce2014-05-14 16:56:16 +03001920 ret = ath10k_pci_hif_power_up_warm(ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001921 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001922 ath10k_warn(ar, "failed to power up target using warm reset: %d\n",
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001923 ret);
1924
Kalle Valo35098462014-03-28 09:32:27 +02001925 if (ath10k_pci_reset_mode == ATH10K_PCI_RESET_WARM_ONLY)
1926 return ret;
1927
Michal Kazior7aa7a722014-08-25 12:09:38 +02001928 ath10k_warn(ar, "trying cold reset\n");
Kalle Valo35098462014-03-28 09:32:27 +02001929
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001930 ret = __ath10k_pci_hif_power_up(ar, true);
1931 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001932 ath10k_err(ar, "failed to power up target using cold reset too (%d)\n",
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001933 ret);
1934 return ret;
1935 }
1936 }
1937
1938 return 0;
1939}
1940
Michal Kazior8c5c5362013-07-16 09:38:50 +02001941static void ath10k_pci_hif_power_down(struct ath10k *ar)
1942{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001943 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif power down\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001944
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001945 ath10k_pci_warm_reset(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001946}
1947
Michal Kazior8cd13ca2013-07-16 09:38:54 +02001948#ifdef CONFIG_PM
1949
1950#define ATH10K_PCI_PM_CONTROL 0x44
1951
1952static int ath10k_pci_hif_suspend(struct ath10k *ar)
1953{
1954 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1955 struct pci_dev *pdev = ar_pci->pdev;
1956 u32 val;
1957
1958 pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
1959
1960 if ((val & 0x000000ff) != 0x3) {
1961 pci_save_state(pdev);
1962 pci_disable_device(pdev);
1963 pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
1964 (val & 0xffffff00) | 0x03);
1965 }
1966
1967 return 0;
1968}
1969
1970static int ath10k_pci_hif_resume(struct ath10k *ar)
1971{
1972 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1973 struct pci_dev *pdev = ar_pci->pdev;
1974 u32 val;
1975
1976 pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
1977
1978 if ((val & 0x000000ff) != 0) {
1979 pci_restore_state(pdev);
1980 pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
1981 val & 0xffffff00);
1982 /*
1983 * Suspend/Resume resets the PCI configuration space,
1984 * so we have to re-disable the RETRY_TIMEOUT register (0x41)
1985 * to keep PCI Tx retries from interfering with C3 CPU state
1986 */
1987 pci_read_config_dword(pdev, 0x40, &val);
1988
1989 if ((val & 0x0000ff00) != 0)
1990 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
1991 }
1992
1993 return 0;
1994}
1995#endif
1996
Kalle Valo5e3dd152013-06-12 20:52:10 +03001997static const struct ath10k_hif_ops ath10k_pci_hif_ops = {
Michal Kazior726346f2014-02-27 18:50:04 +02001998 .tx_sg = ath10k_pci_hif_tx_sg,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001999 .exchange_bmi_msg = ath10k_pci_hif_exchange_bmi_msg,
2000 .start = ath10k_pci_hif_start,
2001 .stop = ath10k_pci_hif_stop,
2002 .map_service_to_pipe = ath10k_pci_hif_map_service_to_pipe,
2003 .get_default_pipe = ath10k_pci_hif_get_default_pipe,
2004 .send_complete_check = ath10k_pci_hif_send_complete_check,
Michal Kaziore799bbf2013-07-05 16:15:12 +03002005 .set_callbacks = ath10k_pci_hif_set_callbacks,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002006 .get_free_queue_number = ath10k_pci_hif_get_free_queue_number,
Michal Kazior8c5c5362013-07-16 09:38:50 +02002007 .power_up = ath10k_pci_hif_power_up,
2008 .power_down = ath10k_pci_hif_power_down,
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002009#ifdef CONFIG_PM
2010 .suspend = ath10k_pci_hif_suspend,
2011 .resume = ath10k_pci_hif_resume,
2012#endif
Kalle Valo5e3dd152013-06-12 20:52:10 +03002013};
2014
2015static void ath10k_pci_ce_tasklet(unsigned long ptr)
2016{
Michal Kazior87263e52013-08-27 13:08:01 +02002017 struct ath10k_pci_pipe *pipe = (struct ath10k_pci_pipe *)ptr;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002018 struct ath10k_pci *ar_pci = pipe->ar_pci;
2019
2020 ath10k_ce_per_engine_service(ar_pci->ar, pipe->pipe_num);
2021}
2022
2023static void ath10k_msi_err_tasklet(unsigned long data)
2024{
2025 struct ath10k *ar = (struct ath10k *)data;
2026
Michal Kazior5c771e72014-08-22 14:23:34 +02002027 if (!ath10k_pci_has_fw_crashed(ar)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002028 ath10k_warn(ar, "received unsolicited fw crash interrupt\n");
Michal Kazior5c771e72014-08-22 14:23:34 +02002029 return;
2030 }
2031
2032 ath10k_pci_fw_crashed_clear(ar);
2033 ath10k_pci_fw_crashed_dump(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002034}
2035
2036/*
2037 * Handler for a per-engine interrupt on a PARTICULAR CE.
2038 * This is used in cases where each CE has a private MSI interrupt.
2039 */
2040static irqreturn_t ath10k_pci_per_engine_handler(int irq, void *arg)
2041{
2042 struct ath10k *ar = arg;
2043 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2044 int ce_id = irq - ar_pci->pdev->irq - MSI_ASSIGN_CE_INITIAL;
2045
Dan Carpentere5742672013-06-18 10:28:46 +03002046 if (ce_id < 0 || ce_id >= ARRAY_SIZE(ar_pci->pipe_info)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002047 ath10k_warn(ar, "unexpected/invalid irq %d ce_id %d\n", irq,
2048 ce_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002049 return IRQ_HANDLED;
2050 }
2051
2052 /*
2053 * NOTE: We are able to derive ce_id from irq because we
2054 * use a one-to-one mapping for CE's 0..5.
2055 * CE's 6 & 7 do not use interrupts at all.
2056 *
2057 * This mapping must be kept in sync with the mapping
2058 * used by firmware.
2059 */
2060 tasklet_schedule(&ar_pci->pipe_info[ce_id].intr);
2061 return IRQ_HANDLED;
2062}
2063
2064static irqreturn_t ath10k_pci_msi_fw_handler(int irq, void *arg)
2065{
2066 struct ath10k *ar = arg;
2067 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2068
2069 tasklet_schedule(&ar_pci->msi_fw_err);
2070 return IRQ_HANDLED;
2071}
2072
2073/*
2074 * Top-level interrupt handler for all PCI interrupts from a Target.
2075 * When a block of MSI interrupts is allocated, this top-level handler
2076 * is not used; instead, we directly call the correct sub-handler.
2077 */
2078static irqreturn_t ath10k_pci_interrupt_handler(int irq, void *arg)
2079{
2080 struct ath10k *ar = arg;
2081 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2082
2083 if (ar_pci->num_msi_intrs == 0) {
Michal Kaziore5398872013-11-25 14:06:20 +01002084 if (!ath10k_pci_irq_pending(ar))
2085 return IRQ_NONE;
2086
Michal Kazior26852182013-11-25 14:06:25 +01002087 ath10k_pci_disable_and_clear_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002088 }
2089
2090 tasklet_schedule(&ar_pci->intr_tq);
2091
2092 return IRQ_HANDLED;
2093}
2094
2095static void ath10k_pci_tasklet(unsigned long data)
2096{
2097 struct ath10k *ar = (struct ath10k *)data;
2098 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2099
Michal Kazior5c771e72014-08-22 14:23:34 +02002100 if (ath10k_pci_has_fw_crashed(ar)) {
2101 ath10k_pci_fw_crashed_clear(ar);
2102 ath10k_pci_fw_crashed_dump(ar);
2103 return;
2104 }
2105
Kalle Valo5e3dd152013-06-12 20:52:10 +03002106 ath10k_ce_per_engine_service_any(ar);
2107
Michal Kazior26852182013-11-25 14:06:25 +01002108 /* Re-enable legacy irq that was disabled in the irq handler */
2109 if (ar_pci->num_msi_intrs == 0)
2110 ath10k_pci_enable_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002111}
2112
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002113static int ath10k_pci_request_irq_msix(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002114{
2115 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002116 int ret, i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002117
2118 ret = request_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW,
2119 ath10k_pci_msi_fw_handler,
2120 IRQF_SHARED, "ath10k_pci", ar);
Michal Kazior591ecdb2013-07-31 10:55:15 +02002121 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002122 ath10k_warn(ar, "failed to request MSI-X fw irq %d: %d\n",
Michal Kazior591ecdb2013-07-31 10:55:15 +02002123 ar_pci->pdev->irq + MSI_ASSIGN_FW, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002124 return ret;
Michal Kazior591ecdb2013-07-31 10:55:15 +02002125 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002126
2127 for (i = MSI_ASSIGN_CE_INITIAL; i <= MSI_ASSIGN_CE_MAX; i++) {
2128 ret = request_irq(ar_pci->pdev->irq + i,
2129 ath10k_pci_per_engine_handler,
2130 IRQF_SHARED, "ath10k_pci", ar);
2131 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002132 ath10k_warn(ar, "failed to request MSI-X ce irq %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002133 ar_pci->pdev->irq + i, ret);
2134
Michal Kazior87b14232013-06-26 08:50:50 +02002135 for (i--; i >= MSI_ASSIGN_CE_INITIAL; i--)
2136 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002137
Michal Kazior87b14232013-06-26 08:50:50 +02002138 free_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002139 return ret;
2140 }
2141 }
2142
Kalle Valo5e3dd152013-06-12 20:52:10 +03002143 return 0;
2144}
2145
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002146static int ath10k_pci_request_irq_msi(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002147{
2148 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2149 int ret;
2150
2151 ret = request_irq(ar_pci->pdev->irq,
2152 ath10k_pci_interrupt_handler,
2153 IRQF_SHARED, "ath10k_pci", ar);
Kalle Valof3782742013-10-17 11:36:15 +03002154 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002155 ath10k_warn(ar, "failed to request MSI irq %d: %d\n",
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002156 ar_pci->pdev->irq, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002157 return ret;
Kalle Valof3782742013-10-17 11:36:15 +03002158 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002159
Kalle Valo5e3dd152013-06-12 20:52:10 +03002160 return 0;
2161}
2162
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002163static int ath10k_pci_request_irq_legacy(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002164{
2165 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002166 int ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002167
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002168 ret = request_irq(ar_pci->pdev->irq,
2169 ath10k_pci_interrupt_handler,
2170 IRQF_SHARED, "ath10k_pci", ar);
Kalle Valof3782742013-10-17 11:36:15 +03002171 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002172 ath10k_warn(ar, "failed to request legacy irq %d: %d\n",
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002173 ar_pci->pdev->irq, ret);
Kalle Valof3782742013-10-17 11:36:15 +03002174 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002175 }
2176
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002177 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002178}
2179
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002180static int ath10k_pci_request_irq(struct ath10k *ar)
2181{
2182 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2183
2184 switch (ar_pci->num_msi_intrs) {
2185 case 0:
2186 return ath10k_pci_request_irq_legacy(ar);
2187 case 1:
2188 return ath10k_pci_request_irq_msi(ar);
2189 case MSI_NUM_REQUEST:
2190 return ath10k_pci_request_irq_msix(ar);
2191 }
2192
Michal Kazior7aa7a722014-08-25 12:09:38 +02002193 ath10k_warn(ar, "unknown irq configuration upon request\n");
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002194 return -EINVAL;
2195}
2196
2197static void ath10k_pci_free_irq(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002198{
2199 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2200 int i;
2201
2202 /* There's at least one interrupt irregardless whether its legacy INTR
2203 * or MSI or MSI-X */
2204 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
2205 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002206}
2207
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002208static void ath10k_pci_init_irq_tasklets(struct ath10k *ar)
2209{
2210 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2211 int i;
2212
2213 tasklet_init(&ar_pci->intr_tq, ath10k_pci_tasklet, (unsigned long)ar);
2214 tasklet_init(&ar_pci->msi_fw_err, ath10k_msi_err_tasklet,
2215 (unsigned long)ar);
2216
2217 for (i = 0; i < CE_COUNT; i++) {
2218 ar_pci->pipe_info[i].ar_pci = ar_pci;
2219 tasklet_init(&ar_pci->pipe_info[i].intr, ath10k_pci_ce_tasklet,
2220 (unsigned long)&ar_pci->pipe_info[i]);
2221 }
2222}
2223
2224static int ath10k_pci_init_irq(struct ath10k *ar)
2225{
2226 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2227 int ret;
2228
2229 ath10k_pci_init_irq_tasklets(ar);
2230
Michal Kazior403d6272014-08-22 14:23:31 +02002231 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_AUTO)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002232 ath10k_info(ar, "limiting irq mode to: %d\n",
2233 ath10k_pci_irq_mode);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002234
2235 /* Try MSI-X */
Michal Kazior0edf2572014-08-07 11:03:29 +02002236 if (ath10k_pci_irq_mode == ATH10K_PCI_IRQ_AUTO) {
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002237 ar_pci->num_msi_intrs = MSI_NUM_REQUEST;
Alexander Gordeev5ad68672014-02-13 17:50:02 +02002238 ret = pci_enable_msi_range(ar_pci->pdev, ar_pci->num_msi_intrs,
2239 ar_pci->num_msi_intrs);
2240 if (ret > 0)
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002241 return 0;
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002242
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002243 /* fall-through */
2244 }
2245
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002246 /* Try MSI */
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002247 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_LEGACY) {
2248 ar_pci->num_msi_intrs = 1;
2249 ret = pci_enable_msi(ar_pci->pdev);
2250 if (ret == 0)
2251 return 0;
2252
2253 /* fall-through */
2254 }
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002255
2256 /* Try legacy irq
2257 *
2258 * A potential race occurs here: The CORE_BASE write
2259 * depends on target correctly decoding AXI address but
2260 * host won't know when target writes BAR to CORE_CTRL.
2261 * This write might get lost if target has NOT written BAR.
2262 * For now, fix the race by repeating the write in below
2263 * synchronization checking. */
2264 ar_pci->num_msi_intrs = 0;
2265
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002266 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2267 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002268
2269 return 0;
2270}
2271
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002272static void ath10k_pci_deinit_irq_legacy(struct ath10k *ar)
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002273{
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002274 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2275 0);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002276}
2277
2278static int ath10k_pci_deinit_irq(struct ath10k *ar)
2279{
2280 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2281
2282 switch (ar_pci->num_msi_intrs) {
2283 case 0:
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002284 ath10k_pci_deinit_irq_legacy(ar);
2285 return 0;
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002286 case 1:
2287 /* fall-through */
2288 case MSI_NUM_REQUEST:
2289 pci_disable_msi(ar_pci->pdev);
2290 return 0;
Alexander Gordeevbb8b6212014-02-13 17:50:01 +02002291 default:
2292 pci_disable_msi(ar_pci->pdev);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002293 }
2294
Michal Kazior7aa7a722014-08-25 12:09:38 +02002295 ath10k_warn(ar, "unknown irq configuration upon deinit\n");
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002296 return -EINVAL;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002297}
2298
Michal Kaziord7fb47f2013-11-08 08:01:26 +01002299static int ath10k_pci_wait_for_target_init(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002300{
2301 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo0399eca2014-03-28 09:32:21 +02002302 unsigned long timeout;
Kalle Valo0399eca2014-03-28 09:32:21 +02002303 u32 val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002304
Michal Kazior7aa7a722014-08-25 12:09:38 +02002305 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot waiting target to initialise\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02002306
Kalle Valo0399eca2014-03-28 09:32:21 +02002307 timeout = jiffies + msecs_to_jiffies(ATH10K_PCI_TARGET_WAIT);
2308
2309 do {
2310 val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
2311
Michal Kazior7aa7a722014-08-25 12:09:38 +02002312 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target indicator %x\n",
2313 val);
Kalle Valo50f87a62014-03-28 09:32:52 +02002314
Kalle Valo0399eca2014-03-28 09:32:21 +02002315 /* target should never return this */
2316 if (val == 0xffffffff)
2317 continue;
2318
Michal Kazior7710cd22014-04-23 19:30:04 +03002319 /* the device has crashed so don't bother trying anymore */
2320 if (val & FW_IND_EVENT_PENDING)
2321 break;
2322
Kalle Valo0399eca2014-03-28 09:32:21 +02002323 if (val & FW_IND_INITIALIZED)
2324 break;
2325
Kalle Valo5e3dd152013-06-12 20:52:10 +03002326 if (ar_pci->num_msi_intrs == 0)
2327 /* Fix potential race by repeating CORE_BASE writes */
Michal Kaziorc947a9e2014-08-22 14:23:30 +02002328 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
2329 PCIE_INTR_ENABLE_ADDRESS,
2330 PCIE_INTR_FIRMWARE_MASK |
2331 PCIE_INTR_CE_MASK_ALL);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002332
Kalle Valo0399eca2014-03-28 09:32:21 +02002333 mdelay(10);
2334 } while (time_before(jiffies, timeout));
2335
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002336 if (val == 0xffffffff) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002337 ath10k_err(ar, "failed to read device register, device is gone\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002338 return -EIO;
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002339 }
2340
Michal Kazior7710cd22014-04-23 19:30:04 +03002341 if (val & FW_IND_EVENT_PENDING) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002342 ath10k_warn(ar, "device has crashed during init\n");
Michal Kazior5c771e72014-08-22 14:23:34 +02002343 ath10k_pci_fw_crashed_clear(ar);
Kalle Valo0e9848c2014-08-25 08:37:37 +03002344 ath10k_pci_fw_crashed_dump(ar);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002345 return -ECOMM;
Michal Kazior7710cd22014-04-23 19:30:04 +03002346 }
2347
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002348 if (!(val & FW_IND_INITIALIZED)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002349 ath10k_err(ar, "failed to receive initialized event from target: %08x\n",
Kalle Valo0399eca2014-03-28 09:32:21 +02002350 val);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002351 return -ETIMEDOUT;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002352 }
2353
Michal Kazior7aa7a722014-08-25 12:09:38 +02002354 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target initialised\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002355 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002356}
2357
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002358static int ath10k_pci_cold_reset(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002359{
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002360 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002361 u32 val;
2362
Michal Kazior7aa7a722014-08-25 12:09:38 +02002363 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cold reset\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02002364
Kalle Valo5e3dd152013-06-12 20:52:10 +03002365 /* Put Target, including PCIe, into RESET. */
Kalle Valoe479ed42013-09-01 10:01:53 +03002366 val = ath10k_pci_reg_read32(ar, SOC_GLOBAL_RESET_ADDRESS);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002367 val |= 1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002368 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002369
2370 for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
Kalle Valoe479ed42013-09-01 10:01:53 +03002371 if (ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03002372 RTC_STATE_COLD_RESET_MASK)
2373 break;
2374 msleep(1);
2375 }
2376
2377 /* Pull Target, including PCIe, out of RESET. */
2378 val &= ~1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002379 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002380
2381 for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
Kalle Valoe479ed42013-09-01 10:01:53 +03002382 if (!(ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03002383 RTC_STATE_COLD_RESET_MASK))
2384 break;
2385 msleep(1);
2386 }
2387
Michal Kazior7aa7a722014-08-25 12:09:38 +02002388 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cold reset complete\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02002389
Michal Kazior5b2589f2013-11-08 08:01:30 +01002390 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002391}
2392
Michal Kazior2986e3e2014-08-07 11:03:30 +02002393static int ath10k_pci_claim(struct ath10k *ar)
2394{
2395 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2396 struct pci_dev *pdev = ar_pci->pdev;
2397 u32 lcr_val;
2398 int ret;
2399
2400 pci_set_drvdata(pdev, ar);
2401
2402 ret = pci_enable_device(pdev);
2403 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002404 ath10k_err(ar, "failed to enable pci device: %d\n", ret);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002405 return ret;
2406 }
2407
2408 ret = pci_request_region(pdev, BAR_NUM, "ath");
2409 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002410 ath10k_err(ar, "failed to request region BAR%d: %d\n", BAR_NUM,
Michal Kazior2986e3e2014-08-07 11:03:30 +02002411 ret);
2412 goto err_device;
2413 }
2414
2415 /* Target expects 32 bit DMA. Enforce it. */
2416 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2417 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002418 ath10k_err(ar, "failed to set dma mask to 32-bit: %d\n", ret);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002419 goto err_region;
2420 }
2421
2422 ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2423 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002424 ath10k_err(ar, "failed to set consistent dma mask to 32-bit: %d\n",
Michal Kazior2986e3e2014-08-07 11:03:30 +02002425 ret);
2426 goto err_region;
2427 }
2428
2429 pci_set_master(pdev);
2430
2431 /* Workaround: Disable ASPM */
2432 pci_read_config_dword(pdev, 0x80, &lcr_val);
2433 pci_write_config_dword(pdev, 0x80, (lcr_val & 0xffffff00));
2434
2435 /* Arrange for access to Target SoC registers. */
2436 ar_pci->mem = pci_iomap(pdev, BAR_NUM, 0);
2437 if (!ar_pci->mem) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002438 ath10k_err(ar, "failed to iomap BAR%d\n", BAR_NUM);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002439 ret = -EIO;
2440 goto err_master;
2441 }
2442
Michal Kazior7aa7a722014-08-25 12:09:38 +02002443 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot pci_mem 0x%p\n", ar_pci->mem);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002444 return 0;
2445
2446err_master:
2447 pci_clear_master(pdev);
2448
2449err_region:
2450 pci_release_region(pdev, BAR_NUM);
2451
2452err_device:
2453 pci_disable_device(pdev);
2454
2455 return ret;
2456}
2457
2458static void ath10k_pci_release(struct ath10k *ar)
2459{
2460 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2461 struct pci_dev *pdev = ar_pci->pdev;
2462
2463 pci_iounmap(pdev, ar_pci->mem);
2464 pci_release_region(pdev, BAR_NUM);
2465 pci_clear_master(pdev);
2466 pci_disable_device(pdev);
2467}
2468
Kalle Valo5e3dd152013-06-12 20:52:10 +03002469static int ath10k_pci_probe(struct pci_dev *pdev,
2470 const struct pci_device_id *pci_dev)
2471{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002472 int ret = 0;
2473 struct ath10k *ar;
2474 struct ath10k_pci *ar_pci;
Michal Kazior2986e3e2014-08-07 11:03:30 +02002475 u32 chip_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002476
Michal Kaziore7b54192014-08-07 11:03:27 +02002477 ar = ath10k_core_create(sizeof(*ar_pci), &pdev->dev,
2478 &ath10k_pci_hif_ops);
2479 if (!ar) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002480 dev_err(&pdev->dev, "failed to allocate core\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002481 return -ENOMEM;
Michal Kaziore7b54192014-08-07 11:03:27 +02002482 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002483
Michal Kazior7aa7a722014-08-25 12:09:38 +02002484 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci probe\n");
2485
Michal Kaziore7b54192014-08-07 11:03:27 +02002486 ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002487 ar_pci->pdev = pdev;
2488 ar_pci->dev = &pdev->dev;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002489 ar_pci->ar = ar;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002490
Michal Kazior2986e3e2014-08-07 11:03:30 +02002491 spin_lock_init(&ar_pci->ce_lock);
Michal Kazior728f95e2014-08-22 14:33:14 +02002492 setup_timer(&ar_pci->rx_post_retry, ath10k_pci_rx_replenish_retry,
2493 (unsigned long)ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002494
Michal Kazior2986e3e2014-08-07 11:03:30 +02002495 ret = ath10k_pci_claim(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002496 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002497 ath10k_err(ar, "failed to claim device: %d\n", ret);
Michal Kaziore7b54192014-08-07 11:03:27 +02002498 goto err_core_destroy;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002499 }
2500
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002501 ret = ath10k_pci_wake(ar);
Kalle Valoe01ae682013-09-01 11:22:14 +03002502 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002503 ath10k_err(ar, "failed to wake up: %d\n", ret);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002504 goto err_release;
Kalle Valoe01ae682013-09-01 11:22:14 +03002505 }
2506
Kalle Valo233eb972013-10-16 16:46:11 +03002507 chip_id = ath10k_pci_soc_read32(ar, SOC_CHIP_ID_ADDRESS);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002508 if (chip_id == 0xffffffff) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002509 ath10k_err(ar, "failed to get chip id\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002510 goto err_sleep;
2511 }
Kalle Valoe01ae682013-09-01 11:22:14 +03002512
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002513 ret = ath10k_pci_alloc_ce(ar);
2514 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002515 ath10k_err(ar, "failed to allocate copy engine pipes: %d\n",
2516 ret);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002517 goto err_sleep;
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002518 }
2519
Michal Kazior403d6272014-08-22 14:23:31 +02002520 ath10k_pci_ce_deinit(ar);
2521
2522 ret = ath10k_ce_disable_interrupts(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002523 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002524 ath10k_err(ar, "failed to disable copy engine interrupts: %d\n",
Michal Kazior403d6272014-08-22 14:23:31 +02002525 ret);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002526 goto err_free_ce;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002527 }
2528
Michal Kazior5c771e72014-08-22 14:23:34 +02002529 /* Workaround: There's no known way to mask all possible interrupts via
2530 * device CSR. The only way to make sure device doesn't assert
2531 * interrupts is to reset it. Interrupts are then disabled on host
2532 * after handlers are registered.
2533 */
2534 ath10k_pci_warm_reset(ar);
2535
Michal Kazior403d6272014-08-22 14:23:31 +02002536 ret = ath10k_pci_init_irq(ar);
2537 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002538 ath10k_err(ar, "failed to init irqs: %d\n", ret);
Michal Kazior403d6272014-08-22 14:23:31 +02002539 goto err_free_ce;
2540 }
2541
Michal Kazior7aa7a722014-08-25 12:09:38 +02002542 ath10k_info(ar, "pci irq %s interrupts %d irq_mode %d reset_mode %d\n",
Michal Kazior403d6272014-08-22 14:23:31 +02002543 ath10k_pci_get_irq_method(ar), ar_pci->num_msi_intrs,
2544 ath10k_pci_irq_mode, ath10k_pci_reset_mode);
2545
Michal Kazior5c771e72014-08-22 14:23:34 +02002546 ret = ath10k_pci_request_irq(ar);
Michal Kazior403d6272014-08-22 14:23:31 +02002547 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002548 ath10k_warn(ar, "failed to request irqs: %d\n", ret);
Michal Kazior403d6272014-08-22 14:23:31 +02002549 goto err_deinit_irq;
2550 }
2551
Michal Kazior5c771e72014-08-22 14:23:34 +02002552 /* This shouldn't race as the device has been reset above. */
2553 ath10k_pci_irq_disable(ar);
2554
2555 ret = ath10k_core_register(ar, chip_id);
2556 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002557 ath10k_err(ar, "failed to register driver core: %d\n", ret);
Michal Kazior5c771e72014-08-22 14:23:34 +02002558 goto err_free_irq;
2559 }
2560
Kalle Valo5e3dd152013-06-12 20:52:10 +03002561 return 0;
2562
Michal Kazior5c771e72014-08-22 14:23:34 +02002563err_free_irq:
2564 ath10k_pci_free_irq(ar);
2565
Michal Kazior403d6272014-08-22 14:23:31 +02002566err_deinit_irq:
2567 ath10k_pci_deinit_irq(ar);
2568
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002569err_free_ce:
2570 ath10k_pci_free_ce(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002571
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002572err_sleep:
2573 ath10k_pci_sleep(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002574
2575err_release:
2576 ath10k_pci_release(ar);
2577
Michal Kaziore7b54192014-08-07 11:03:27 +02002578err_core_destroy:
Kalle Valo5e3dd152013-06-12 20:52:10 +03002579 ath10k_core_destroy(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002580
2581 return ret;
2582}
2583
2584static void ath10k_pci_remove(struct pci_dev *pdev)
2585{
2586 struct ath10k *ar = pci_get_drvdata(pdev);
2587 struct ath10k_pci *ar_pci;
2588
Michal Kazior7aa7a722014-08-25 12:09:38 +02002589 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci remove\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002590
2591 if (!ar)
2592 return;
2593
2594 ar_pci = ath10k_pci_priv(ar);
2595
2596 if (!ar_pci)
2597 return;
2598
Kalle Valo5e3dd152013-06-12 20:52:10 +03002599 ath10k_core_unregister(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02002600 ath10k_pci_free_irq(ar);
Michal Kazior403d6272014-08-22 14:23:31 +02002601 ath10k_pci_deinit_irq(ar);
2602 ath10k_pci_ce_deinit(ar);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002603 ath10k_pci_free_ce(ar);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002604 ath10k_pci_sleep(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002605 ath10k_pci_release(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002606 ath10k_core_destroy(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002607}
2608
Kalle Valo5e3dd152013-06-12 20:52:10 +03002609MODULE_DEVICE_TABLE(pci, ath10k_pci_id_table);
2610
2611static struct pci_driver ath10k_pci_driver = {
2612 .name = "ath10k_pci",
2613 .id_table = ath10k_pci_id_table,
2614 .probe = ath10k_pci_probe,
2615 .remove = ath10k_pci_remove,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002616};
2617
2618static int __init ath10k_pci_init(void)
2619{
2620 int ret;
2621
2622 ret = pci_register_driver(&ath10k_pci_driver);
2623 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002624 printk(KERN_ERR "failed to register ath10k pci driver: %d\n",
2625 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002626
2627 return ret;
2628}
2629module_init(ath10k_pci_init);
2630
2631static void __exit ath10k_pci_exit(void)
2632{
2633 pci_unregister_driver(&ath10k_pci_driver);
2634}
2635
2636module_exit(ath10k_pci_exit);
2637
2638MODULE_AUTHOR("Qualcomm Atheros");
2639MODULE_DESCRIPTION("Driver support for Atheros QCA988X PCIe devices");
2640MODULE_LICENSE("Dual BSD/GPL");
Michal Kazior24c88f72014-07-25 13:32:17 +02002641MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_FW_3_FILE);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002642MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_BOARD_DATA_FILE);