blob: d97397563944d5f51214f7ae90e5e3c9668d6727 [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
Bartosz Markowski8cc8df92013-08-02 09:58:49 +020042static unsigned int ath10k_target_ps;
Michal Kaziorcfe9c452013-11-25 14:06:27 +010043static unsigned int ath10k_pci_irq_mode = ATH10K_PCI_IRQ_AUTO;
44
Kalle Valo5e3dd152013-06-12 20:52:10 +030045module_param(ath10k_target_ps, uint, 0644);
46MODULE_PARM_DESC(ath10k_target_ps, "Enable ath10k Target (SoC) PS option");
47
Michal Kaziorcfe9c452013-11-25 14:06:27 +010048module_param_named(irq_mode, ath10k_pci_irq_mode, uint, 0644);
49MODULE_PARM_DESC(irq_mode, "0: auto, 1: legacy, 2: msi (default: 0)");
50
Kalle Valo5e3dd152013-06-12 20:52:10 +030051#define QCA988X_2_0_DEVICE_ID (0x003c)
52
53static DEFINE_PCI_DEVICE_TABLE(ath10k_pci_id_table) = {
Kalle Valo5e3dd152013-06-12 20:52:10 +030054 { PCI_VDEVICE(ATHEROS, QCA988X_2_0_DEVICE_ID) }, /* PCI-E QCA988X V2 */
55 {0}
56};
57
58static int ath10k_pci_diag_read_access(struct ath10k *ar, u32 address,
59 u32 *data);
60
61static void ath10k_pci_process_ce(struct ath10k *ar);
62static int ath10k_pci_post_rx(struct ath10k *ar);
Michal Kazior87263e52013-08-27 13:08:01 +020063static int ath10k_pci_post_rx_pipe(struct ath10k_pci_pipe *pipe_info,
Kalle Valo5e3dd152013-06-12 20:52:10 +030064 int num);
Michal Kazior87263e52013-08-27 13:08:01 +020065static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe *pipe_info);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +010066static int ath10k_pci_cold_reset(struct ath10k *ar);
67static int ath10k_pci_warm_reset(struct ath10k *ar);
Michal Kaziord7fb47f2013-11-08 08:01:26 +010068static int ath10k_pci_wait_for_target_init(struct ath10k *ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +010069static int ath10k_pci_init_irq(struct ath10k *ar);
70static int ath10k_pci_deinit_irq(struct ath10k *ar);
71static int ath10k_pci_request_irq(struct ath10k *ar);
72static void ath10k_pci_free_irq(struct ath10k *ar);
Michal Kazior85622cd2013-11-25 14:06:22 +010073static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
74 struct ath10k_ce_pipe *rx_pipe,
75 struct bmi_xfer *xfer);
Michal Kaziorc80de122013-11-25 14:06:23 +010076static void ath10k_pci_cleanup_ce(struct ath10k *ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +030077
78static const struct ce_attr host_ce_config_wlan[] = {
Kalle Valo48e9c222013-09-01 10:01:32 +030079 /* CE0: host->target HTC control and raw streams */
80 {
81 .flags = CE_ATTR_FLAGS,
82 .src_nentries = 16,
83 .src_sz_max = 256,
84 .dest_nentries = 0,
85 },
86
87 /* CE1: target->host HTT + HTC control */
88 {
89 .flags = CE_ATTR_FLAGS,
90 .src_nentries = 0,
91 .src_sz_max = 512,
92 .dest_nentries = 512,
93 },
94
95 /* CE2: target->host WMI */
96 {
97 .flags = CE_ATTR_FLAGS,
98 .src_nentries = 0,
99 .src_sz_max = 2048,
100 .dest_nentries = 32,
101 },
102
103 /* CE3: host->target WMI */
104 {
105 .flags = CE_ATTR_FLAGS,
106 .src_nentries = 32,
107 .src_sz_max = 2048,
108 .dest_nentries = 0,
109 },
110
111 /* CE4: host->target HTT */
112 {
113 .flags = CE_ATTR_FLAGS | CE_ATTR_DIS_INTR,
114 .src_nentries = CE_HTT_H2T_MSG_SRC_NENTRIES,
115 .src_sz_max = 256,
116 .dest_nentries = 0,
117 },
118
119 /* CE5: unused */
120 {
121 .flags = CE_ATTR_FLAGS,
122 .src_nentries = 0,
123 .src_sz_max = 0,
124 .dest_nentries = 0,
125 },
126
127 /* CE6: target autonomous hif_memcpy */
128 {
129 .flags = CE_ATTR_FLAGS,
130 .src_nentries = 0,
131 .src_sz_max = 0,
132 .dest_nentries = 0,
133 },
134
135 /* CE7: ce_diag, the Diagnostic Window */
136 {
137 .flags = CE_ATTR_FLAGS,
138 .src_nentries = 2,
139 .src_sz_max = DIAG_TRANSFER_LIMIT,
140 .dest_nentries = 2,
141 },
Kalle Valo5e3dd152013-06-12 20:52:10 +0300142};
143
144/* Target firmware's Copy Engine configuration. */
145static const struct ce_pipe_config target_ce_config_wlan[] = {
Kalle Valod88effb2013-09-01 10:01:39 +0300146 /* CE0: host->target HTC control and raw streams */
147 {
148 .pipenum = 0,
149 .pipedir = PIPEDIR_OUT,
150 .nentries = 32,
151 .nbytes_max = 256,
152 .flags = CE_ATTR_FLAGS,
153 .reserved = 0,
154 },
155
156 /* CE1: target->host HTT + HTC control */
157 {
158 .pipenum = 1,
159 .pipedir = PIPEDIR_IN,
160 .nentries = 32,
161 .nbytes_max = 512,
162 .flags = CE_ATTR_FLAGS,
163 .reserved = 0,
164 },
165
166 /* CE2: target->host WMI */
167 {
168 .pipenum = 2,
169 .pipedir = PIPEDIR_IN,
170 .nentries = 32,
171 .nbytes_max = 2048,
172 .flags = CE_ATTR_FLAGS,
173 .reserved = 0,
174 },
175
176 /* CE3: host->target WMI */
177 {
178 .pipenum = 3,
179 .pipedir = PIPEDIR_OUT,
180 .nentries = 32,
181 .nbytes_max = 2048,
182 .flags = CE_ATTR_FLAGS,
183 .reserved = 0,
184 },
185
186 /* CE4: host->target HTT */
187 {
188 .pipenum = 4,
189 .pipedir = PIPEDIR_OUT,
190 .nentries = 256,
191 .nbytes_max = 256,
192 .flags = CE_ATTR_FLAGS,
193 .reserved = 0,
194 },
195
Kalle Valo5e3dd152013-06-12 20:52:10 +0300196 /* NB: 50% of src nentries, since tx has 2 frags */
Kalle Valod88effb2013-09-01 10:01:39 +0300197
198 /* CE5: unused */
199 {
200 .pipenum = 5,
201 .pipedir = PIPEDIR_OUT,
202 .nentries = 32,
203 .nbytes_max = 2048,
204 .flags = CE_ATTR_FLAGS,
205 .reserved = 0,
206 },
207
208 /* CE6: Reserved for target autonomous hif_memcpy */
209 {
210 .pipenum = 6,
211 .pipedir = PIPEDIR_INOUT,
212 .nentries = 32,
213 .nbytes_max = 4096,
214 .flags = CE_ATTR_FLAGS,
215 .reserved = 0,
216 },
217
Kalle Valo5e3dd152013-06-12 20:52:10 +0300218 /* CE7 used only by Host */
219};
220
Michal Kaziore5398872013-11-25 14:06:20 +0100221static bool ath10k_pci_irq_pending(struct ath10k *ar)
222{
223 u32 cause;
224
225 /* Check if the shared legacy irq is for us */
226 cause = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
227 PCIE_INTR_CAUSE_ADDRESS);
228 if (cause & (PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL))
229 return true;
230
231 return false;
232}
233
Michal Kazior26852182013-11-25 14:06:25 +0100234static void ath10k_pci_disable_and_clear_legacy_irq(struct ath10k *ar)
235{
236 /* IMPORTANT: INTR_CLR register has to be set after
237 * INTR_ENABLE is set to 0, otherwise interrupt can not be
238 * really cleared. */
239 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
240 0);
241 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_CLR_ADDRESS,
242 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
243
244 /* IMPORTANT: this extra read transaction is required to
245 * flush the posted write buffer. */
246 (void) ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
247 PCIE_INTR_ENABLE_ADDRESS);
248}
249
250static void ath10k_pci_enable_legacy_irq(struct ath10k *ar)
251{
252 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
253 PCIE_INTR_ENABLE_ADDRESS,
254 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
255
256 /* IMPORTANT: this extra read transaction is required to
257 * flush the posted write buffer. */
258 (void) ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
259 PCIE_INTR_ENABLE_ADDRESS);
260}
261
Michal Kaziorab977bd2013-11-25 14:06:26 +0100262static irqreturn_t ath10k_pci_early_irq_handler(int irq, void *arg)
263{
264 struct ath10k *ar = arg;
265 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
266
267 if (ar_pci->num_msi_intrs == 0) {
268 if (!ath10k_pci_irq_pending(ar))
269 return IRQ_NONE;
270
271 ath10k_pci_disable_and_clear_legacy_irq(ar);
272 }
273
274 tasklet_schedule(&ar_pci->early_irq_tasklet);
275
276 return IRQ_HANDLED;
277}
278
279static int ath10k_pci_request_early_irq(struct ath10k *ar)
280{
281 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
282 int ret;
283
284 /* Regardless whether MSI-X/MSI/legacy irqs have been set up the first
285 * interrupt from irq vector is triggered in all cases for FW
286 * indication/errors */
287 ret = request_irq(ar_pci->pdev->irq, ath10k_pci_early_irq_handler,
288 IRQF_SHARED, "ath10k_pci (early)", ar);
289 if (ret) {
290 ath10k_warn("failed to request early irq: %d\n", ret);
291 return ret;
292 }
293
294 return 0;
295}
296
297static void ath10k_pci_free_early_irq(struct ath10k *ar)
298{
299 free_irq(ath10k_pci_priv(ar)->pdev->irq, ar);
300}
301
Kalle Valo5e3dd152013-06-12 20:52:10 +0300302/*
303 * Diagnostic read/write access is provided for startup/config/debug usage.
304 * Caller must guarantee proper alignment, when applicable, and single user
305 * at any moment.
306 */
307static int ath10k_pci_diag_read_mem(struct ath10k *ar, u32 address, void *data,
308 int nbytes)
309{
310 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
311 int ret = 0;
312 u32 buf;
313 unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
314 unsigned int id;
315 unsigned int flags;
Michal Kazior2aa39112013-08-27 13:08:02 +0200316 struct ath10k_ce_pipe *ce_diag;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300317 /* Host buffer address in CE space */
318 u32 ce_data;
319 dma_addr_t ce_data_base = 0;
320 void *data_buf = NULL;
321 int i;
322
323 /*
324 * This code cannot handle reads to non-memory space. Redirect to the
325 * register read fn but preserve the multi word read capability of
326 * this fn
327 */
328 if (address < DRAM_BASE_ADDRESS) {
329 if (!IS_ALIGNED(address, 4) ||
330 !IS_ALIGNED((unsigned long)data, 4))
331 return -EIO;
332
333 while ((nbytes >= 4) && ((ret = ath10k_pci_diag_read_access(
334 ar, address, (u32 *)data)) == 0)) {
335 nbytes -= sizeof(u32);
336 address += sizeof(u32);
337 data += sizeof(u32);
338 }
339 return ret;
340 }
341
342 ce_diag = ar_pci->ce_diag;
343
344 /*
345 * Allocate a temporary bounce buffer to hold caller's data
346 * to be DMA'ed from Target. This guarantees
347 * 1) 4-byte alignment
348 * 2) Buffer in DMA-able space
349 */
350 orig_nbytes = nbytes;
351 data_buf = (unsigned char *)pci_alloc_consistent(ar_pci->pdev,
352 orig_nbytes,
353 &ce_data_base);
354
355 if (!data_buf) {
356 ret = -ENOMEM;
357 goto done;
358 }
359 memset(data_buf, 0, orig_nbytes);
360
361 remaining_bytes = orig_nbytes;
362 ce_data = ce_data_base;
363 while (remaining_bytes) {
364 nbytes = min_t(unsigned int, remaining_bytes,
365 DIAG_TRANSFER_LIMIT);
366
367 ret = ath10k_ce_recv_buf_enqueue(ce_diag, NULL, ce_data);
368 if (ret != 0)
369 goto done;
370
371 /* Request CE to send from Target(!) address to Host buffer */
372 /*
373 * The address supplied by the caller is in the
374 * Target CPU virtual address space.
375 *
376 * In order to use this address with the diagnostic CE,
377 * convert it from Target CPU virtual address space
378 * to CE address space
379 */
380 ath10k_pci_wake(ar);
381 address = TARG_CPU_SPACE_TO_CE_SPACE(ar, ar_pci->mem,
382 address);
383 ath10k_pci_sleep(ar);
384
385 ret = ath10k_ce_send(ce_diag, NULL, (u32)address, nbytes, 0,
386 0);
387 if (ret)
388 goto done;
389
390 i = 0;
391 while (ath10k_ce_completed_send_next(ce_diag, NULL, &buf,
392 &completed_nbytes,
393 &id) != 0) {
394 mdelay(1);
395 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
396 ret = -EBUSY;
397 goto done;
398 }
399 }
400
401 if (nbytes != completed_nbytes) {
402 ret = -EIO;
403 goto done;
404 }
405
406 if (buf != (u32) address) {
407 ret = -EIO;
408 goto done;
409 }
410
411 i = 0;
412 while (ath10k_ce_completed_recv_next(ce_diag, NULL, &buf,
413 &completed_nbytes,
414 &id, &flags) != 0) {
415 mdelay(1);
416
417 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
418 ret = -EBUSY;
419 goto done;
420 }
421 }
422
423 if (nbytes != completed_nbytes) {
424 ret = -EIO;
425 goto done;
426 }
427
428 if (buf != ce_data) {
429 ret = -EIO;
430 goto done;
431 }
432
433 remaining_bytes -= nbytes;
434 address += nbytes;
435 ce_data += nbytes;
436 }
437
438done:
439 if (ret == 0) {
440 /* Copy data from allocated DMA buf to caller's buf */
441 WARN_ON_ONCE(orig_nbytes & 3);
442 for (i = 0; i < orig_nbytes / sizeof(__le32); i++) {
443 ((u32 *)data)[i] =
444 __le32_to_cpu(((__le32 *)data_buf)[i]);
445 }
446 } else
447 ath10k_dbg(ATH10K_DBG_PCI, "%s failure (0x%x)\n",
448 __func__, address);
449
450 if (data_buf)
451 pci_free_consistent(ar_pci->pdev, orig_nbytes,
452 data_buf, ce_data_base);
453
454 return ret;
455}
456
457/* Read 4-byte aligned data from Target memory or register */
458static int ath10k_pci_diag_read_access(struct ath10k *ar, u32 address,
459 u32 *data)
460{
461 /* Assume range doesn't cross this boundary */
462 if (address >= DRAM_BASE_ADDRESS)
463 return ath10k_pci_diag_read_mem(ar, address, data, sizeof(u32));
464
465 ath10k_pci_wake(ar);
466 *data = ath10k_pci_read32(ar, address);
467 ath10k_pci_sleep(ar);
468 return 0;
469}
470
471static int ath10k_pci_diag_write_mem(struct ath10k *ar, u32 address,
472 const void *data, int nbytes)
473{
474 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
475 int ret = 0;
476 u32 buf;
477 unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
478 unsigned int id;
479 unsigned int flags;
Michal Kazior2aa39112013-08-27 13:08:02 +0200480 struct ath10k_ce_pipe *ce_diag;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300481 void *data_buf = NULL;
482 u32 ce_data; /* Host buffer address in CE space */
483 dma_addr_t ce_data_base = 0;
484 int i;
485
486 ce_diag = ar_pci->ce_diag;
487
488 /*
489 * Allocate a temporary bounce buffer to hold caller's data
490 * to be DMA'ed to Target. This guarantees
491 * 1) 4-byte alignment
492 * 2) Buffer in DMA-able space
493 */
494 orig_nbytes = nbytes;
495 data_buf = (unsigned char *)pci_alloc_consistent(ar_pci->pdev,
496 orig_nbytes,
497 &ce_data_base);
498 if (!data_buf) {
499 ret = -ENOMEM;
500 goto done;
501 }
502
503 /* Copy caller's data to allocated DMA buf */
504 WARN_ON_ONCE(orig_nbytes & 3);
505 for (i = 0; i < orig_nbytes / sizeof(__le32); i++)
506 ((__le32 *)data_buf)[i] = __cpu_to_le32(((u32 *)data)[i]);
507
508 /*
509 * The address supplied by the caller is in the
510 * Target CPU virtual address space.
511 *
512 * In order to use this address with the diagnostic CE,
513 * convert it from
514 * Target CPU virtual address space
515 * to
516 * CE address space
517 */
518 ath10k_pci_wake(ar);
519 address = TARG_CPU_SPACE_TO_CE_SPACE(ar, ar_pci->mem, address);
520 ath10k_pci_sleep(ar);
521
522 remaining_bytes = orig_nbytes;
523 ce_data = ce_data_base;
524 while (remaining_bytes) {
525 /* FIXME: check cast */
526 nbytes = min_t(int, remaining_bytes, DIAG_TRANSFER_LIMIT);
527
528 /* Set up to receive directly into Target(!) address */
529 ret = ath10k_ce_recv_buf_enqueue(ce_diag, NULL, address);
530 if (ret != 0)
531 goto done;
532
533 /*
534 * Request CE to send caller-supplied data that
535 * was copied to bounce buffer to Target(!) address.
536 */
537 ret = ath10k_ce_send(ce_diag, NULL, (u32) ce_data,
538 nbytes, 0, 0);
539 if (ret != 0)
540 goto done;
541
542 i = 0;
543 while (ath10k_ce_completed_send_next(ce_diag, NULL, &buf,
544 &completed_nbytes,
545 &id) != 0) {
546 mdelay(1);
547
548 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
549 ret = -EBUSY;
550 goto done;
551 }
552 }
553
554 if (nbytes != completed_nbytes) {
555 ret = -EIO;
556 goto done;
557 }
558
559 if (buf != ce_data) {
560 ret = -EIO;
561 goto done;
562 }
563
564 i = 0;
565 while (ath10k_ce_completed_recv_next(ce_diag, NULL, &buf,
566 &completed_nbytes,
567 &id, &flags) != 0) {
568 mdelay(1);
569
570 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
571 ret = -EBUSY;
572 goto done;
573 }
574 }
575
576 if (nbytes != completed_nbytes) {
577 ret = -EIO;
578 goto done;
579 }
580
581 if (buf != address) {
582 ret = -EIO;
583 goto done;
584 }
585
586 remaining_bytes -= nbytes;
587 address += nbytes;
588 ce_data += nbytes;
589 }
590
591done:
592 if (data_buf) {
593 pci_free_consistent(ar_pci->pdev, orig_nbytes, data_buf,
594 ce_data_base);
595 }
596
597 if (ret != 0)
598 ath10k_dbg(ATH10K_DBG_PCI, "%s failure (0x%x)\n", __func__,
599 address);
600
601 return ret;
602}
603
604/* Write 4B data to Target memory or register */
605static int ath10k_pci_diag_write_access(struct ath10k *ar, u32 address,
606 u32 data)
607{
608 /* Assume range doesn't cross this boundary */
609 if (address >= DRAM_BASE_ADDRESS)
610 return ath10k_pci_diag_write_mem(ar, address, &data,
611 sizeof(u32));
612
613 ath10k_pci_wake(ar);
614 ath10k_pci_write32(ar, address, data);
615 ath10k_pci_sleep(ar);
616 return 0;
617}
618
619static bool ath10k_pci_target_is_awake(struct ath10k *ar)
620{
621 void __iomem *mem = ath10k_pci_priv(ar)->mem;
622 u32 val;
623 val = ioread32(mem + PCIE_LOCAL_BASE_ADDRESS +
624 RTC_STATE_ADDRESS);
625 return (RTC_STATE_V_GET(val) == RTC_STATE_V_ON);
626}
627
Kalle Valo3aebe542013-09-01 10:02:07 +0300628int ath10k_do_pci_wake(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300629{
630 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
631 void __iomem *pci_addr = ar_pci->mem;
632 int tot_delay = 0;
633 int curr_delay = 5;
634
635 if (atomic_read(&ar_pci->keep_awake_count) == 0) {
636 /* Force AWAKE */
637 iowrite32(PCIE_SOC_WAKE_V_MASK,
638 pci_addr + PCIE_LOCAL_BASE_ADDRESS +
639 PCIE_SOC_WAKE_ADDRESS);
640 }
641 atomic_inc(&ar_pci->keep_awake_count);
642
643 if (ar_pci->verified_awake)
Kalle Valo3aebe542013-09-01 10:02:07 +0300644 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300645
646 for (;;) {
647 if (ath10k_pci_target_is_awake(ar)) {
648 ar_pci->verified_awake = true;
Kalle Valo3aebe542013-09-01 10:02:07 +0300649 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300650 }
651
652 if (tot_delay > PCIE_WAKE_TIMEOUT) {
Kalle Valo3aebe542013-09-01 10:02:07 +0300653 ath10k_warn("target took longer %d us to wake up (awake count %d)\n",
654 PCIE_WAKE_TIMEOUT,
Kalle Valo5e3dd152013-06-12 20:52:10 +0300655 atomic_read(&ar_pci->keep_awake_count));
Kalle Valo3aebe542013-09-01 10:02:07 +0300656 return -ETIMEDOUT;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300657 }
658
659 udelay(curr_delay);
660 tot_delay += curr_delay;
661
662 if (curr_delay < 50)
663 curr_delay += 5;
664 }
665}
666
667void ath10k_do_pci_sleep(struct ath10k *ar)
668{
669 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
670 void __iomem *pci_addr = ar_pci->mem;
671
672 if (atomic_dec_and_test(&ar_pci->keep_awake_count)) {
673 /* Allow sleep */
674 ar_pci->verified_awake = false;
675 iowrite32(PCIE_SOC_WAKE_RESET,
676 pci_addr + PCIE_LOCAL_BASE_ADDRESS +
677 PCIE_SOC_WAKE_ADDRESS);
678 }
679}
680
681/*
682 * FIXME: Handle OOM properly.
683 */
684static inline
Michal Kazior87263e52013-08-27 13:08:01 +0200685struct ath10k_pci_compl *get_free_compl(struct ath10k_pci_pipe *pipe_info)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300686{
687 struct ath10k_pci_compl *compl = NULL;
688
689 spin_lock_bh(&pipe_info->pipe_lock);
690 if (list_empty(&pipe_info->compl_free)) {
691 ath10k_warn("Completion buffers are full\n");
692 goto exit;
693 }
694 compl = list_first_entry(&pipe_info->compl_free,
695 struct ath10k_pci_compl, list);
696 list_del(&compl->list);
697exit:
698 spin_unlock_bh(&pipe_info->pipe_lock);
699 return compl;
700}
701
702/* Called by lower (CE) layer when a send to Target completes. */
Michal Kazior5440ce22013-09-03 15:09:58 +0200703static void ath10k_pci_ce_send_done(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300704{
705 struct ath10k *ar = ce_state->ar;
706 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +0200707 struct ath10k_pci_pipe *pipe_info = &ar_pci->pipe_info[ce_state->id];
Kalle Valo5e3dd152013-06-12 20:52:10 +0300708 struct ath10k_pci_compl *compl;
Michal Kazior5440ce22013-09-03 15:09:58 +0200709 void *transfer_context;
710 u32 ce_data;
711 unsigned int nbytes;
712 unsigned int transfer_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300713
Michal Kazior5440ce22013-09-03 15:09:58 +0200714 while (ath10k_ce_completed_send_next(ce_state, &transfer_context,
715 &ce_data, &nbytes,
716 &transfer_id) == 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300717 compl = get_free_compl(pipe_info);
718 if (!compl)
719 break;
720
Michal Kaziorf9d8fec2013-08-13 07:54:56 +0200721 compl->state = ATH10K_PCI_COMPL_SEND;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300722 compl->ce_state = ce_state;
723 compl->pipe_info = pipe_info;
Kalle Valoaa5c1db2013-09-01 10:01:46 +0300724 compl->skb = transfer_context;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300725 compl->nbytes = nbytes;
726 compl->transfer_id = transfer_id;
727 compl->flags = 0;
728
729 /*
730 * Add the completion to the processing queue.
731 */
732 spin_lock_bh(&ar_pci->compl_lock);
733 list_add_tail(&compl->list, &ar_pci->compl_process);
734 spin_unlock_bh(&ar_pci->compl_lock);
Michal Kazior5440ce22013-09-03 15:09:58 +0200735 }
Kalle Valo5e3dd152013-06-12 20:52:10 +0300736
737 ath10k_pci_process_ce(ar);
738}
739
740/* Called by lower (CE) layer when data is received from the Target. */
Michal Kazior5440ce22013-09-03 15:09:58 +0200741static void ath10k_pci_ce_recv_data(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300742{
743 struct ath10k *ar = ce_state->ar;
744 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +0200745 struct ath10k_pci_pipe *pipe_info = &ar_pci->pipe_info[ce_state->id];
Kalle Valo5e3dd152013-06-12 20:52:10 +0300746 struct ath10k_pci_compl *compl;
747 struct sk_buff *skb;
Michal Kazior5440ce22013-09-03 15:09:58 +0200748 void *transfer_context;
749 u32 ce_data;
750 unsigned int nbytes;
751 unsigned int transfer_id;
752 unsigned int flags;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300753
Michal Kazior5440ce22013-09-03 15:09:58 +0200754 while (ath10k_ce_completed_recv_next(ce_state, &transfer_context,
755 &ce_data, &nbytes, &transfer_id,
756 &flags) == 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300757 compl = get_free_compl(pipe_info);
758 if (!compl)
759 break;
760
Michal Kaziorf9d8fec2013-08-13 07:54:56 +0200761 compl->state = ATH10K_PCI_COMPL_RECV;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300762 compl->ce_state = ce_state;
763 compl->pipe_info = pipe_info;
Kalle Valoaa5c1db2013-09-01 10:01:46 +0300764 compl->skb = transfer_context;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300765 compl->nbytes = nbytes;
766 compl->transfer_id = transfer_id;
767 compl->flags = flags;
768
769 skb = transfer_context;
770 dma_unmap_single(ar->dev, ATH10K_SKB_CB(skb)->paddr,
771 skb->len + skb_tailroom(skb),
772 DMA_FROM_DEVICE);
773 /*
774 * Add the completion to the processing queue.
775 */
776 spin_lock_bh(&ar_pci->compl_lock);
777 list_add_tail(&compl->list, &ar_pci->compl_process);
778 spin_unlock_bh(&ar_pci->compl_lock);
Michal Kazior5440ce22013-09-03 15:09:58 +0200779 }
Kalle Valo5e3dd152013-06-12 20:52:10 +0300780
781 ath10k_pci_process_ce(ar);
782}
783
784/* Send the first nbytes bytes of the buffer */
785static int ath10k_pci_hif_send_head(struct ath10k *ar, u8 pipe_id,
786 unsigned int transfer_id,
787 unsigned int bytes, struct sk_buff *nbuf)
788{
789 struct ath10k_skb_cb *skb_cb = ATH10K_SKB_CB(nbuf);
790 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +0200791 struct ath10k_pci_pipe *pipe_info = &(ar_pci->pipe_info[pipe_id]);
Michal Kazior2aa39112013-08-27 13:08:02 +0200792 struct ath10k_ce_pipe *ce_hdl = pipe_info->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300793 unsigned int len;
794 u32 flags = 0;
795 int ret;
796
Kalle Valo5e3dd152013-06-12 20:52:10 +0300797 len = min(bytes, nbuf->len);
798 bytes -= len;
799
800 if (len & 3)
801 ath10k_warn("skb not aligned to 4-byte boundary (%d)\n", len);
802
803 ath10k_dbg(ATH10K_DBG_PCI,
804 "pci send data vaddr %p paddr 0x%llx len %d as %d bytes\n",
805 nbuf->data, (unsigned long long) skb_cb->paddr,
806 nbuf->len, len);
807 ath10k_dbg_dump(ATH10K_DBG_PCI_DUMP, NULL,
808 "ath10k tx: data: ",
809 nbuf->data, nbuf->len);
810
Michal Kazior2e761b52013-10-02 11:03:40 +0200811 ret = ath10k_ce_send(ce_hdl, nbuf, skb_cb->paddr, len, transfer_id,
812 flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300813 if (ret)
Michal Kazior1d2b48d2013-11-08 08:01:34 +0100814 ath10k_warn("failed to send sk_buff to CE: %p\n", nbuf);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300815
816 return ret;
817}
818
819static u16 ath10k_pci_hif_get_free_queue_number(struct ath10k *ar, u8 pipe)
820{
821 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior3efcb3b2013-10-02 11:03:41 +0200822 return ath10k_ce_num_free_src_entries(ar_pci->pipe_info[pipe].ce_hdl);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300823}
824
825static void ath10k_pci_hif_dump_area(struct ath10k *ar)
826{
827 u32 reg_dump_area = 0;
828 u32 reg_dump_values[REG_DUMP_COUNT_QCA988X] = {};
829 u32 host_addr;
830 int ret;
831 u32 i;
832
833 ath10k_err("firmware crashed!\n");
834 ath10k_err("hardware name %s version 0x%x\n",
835 ar->hw_params.name, ar->target_version);
Chun-Yeow Yeoh5ba88b32014-01-21 17:21:21 +0800836 ath10k_err("firmware version: %s\n", ar->hw->wiphy->fw_version);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300837
838 host_addr = host_interest_item_address(HI_ITEM(hi_failure_state));
Michal Kazior1d2b48d2013-11-08 08:01:34 +0100839 ret = ath10k_pci_diag_read_mem(ar, host_addr,
840 &reg_dump_area, sizeof(u32));
841 if (ret) {
842 ath10k_err("failed to read FW dump area address: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300843 return;
844 }
845
846 ath10k_err("target register Dump Location: 0x%08X\n", reg_dump_area);
847
848 ret = ath10k_pci_diag_read_mem(ar, reg_dump_area,
849 &reg_dump_values[0],
850 REG_DUMP_COUNT_QCA988X * sizeof(u32));
851 if (ret != 0) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +0100852 ath10k_err("failed to read FW dump area: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300853 return;
854 }
855
856 BUILD_BUG_ON(REG_DUMP_COUNT_QCA988X % 4);
857
858 ath10k_err("target Register Dump\n");
859 for (i = 0; i < REG_DUMP_COUNT_QCA988X; i += 4)
860 ath10k_err("[%02d]: 0x%08X 0x%08X 0x%08X 0x%08X\n",
861 i,
862 reg_dump_values[i],
863 reg_dump_values[i + 1],
864 reg_dump_values[i + 2],
865 reg_dump_values[i + 3]);
Michal Kazioraffd3212013-07-16 09:54:35 +0200866
Michal Kazior5e90de82013-10-16 16:46:05 +0300867 queue_work(ar->workqueue, &ar->restart_work);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300868}
869
870static void ath10k_pci_hif_send_complete_check(struct ath10k *ar, u8 pipe,
871 int force)
872{
873 if (!force) {
874 int resources;
875 /*
876 * Decide whether to actually poll for completions, or just
877 * wait for a later chance.
878 * If there seem to be plenty of resources left, then just wait
879 * since checking involves reading a CE register, which is a
880 * relatively expensive operation.
881 */
882 resources = ath10k_pci_hif_get_free_queue_number(ar, pipe);
883
884 /*
885 * If at least 50% of the total resources are still available,
886 * don't bother checking again yet.
887 */
888 if (resources > (host_ce_config_wlan[pipe].src_nentries >> 1))
889 return;
890 }
891 ath10k_ce_per_engine_service(ar, pipe);
892}
893
Michal Kaziore799bbf2013-07-05 16:15:12 +0300894static void ath10k_pci_hif_set_callbacks(struct ath10k *ar,
895 struct ath10k_hif_cb *callbacks)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300896{
897 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
898
899 ath10k_dbg(ATH10K_DBG_PCI, "%s\n", __func__);
900
901 memcpy(&ar_pci->msg_callbacks_current, callbacks,
902 sizeof(ar_pci->msg_callbacks_current));
903}
904
Michal Kaziorc80de122013-11-25 14:06:23 +0100905static int ath10k_pci_alloc_compl(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300906{
907 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300908 const struct ce_attr *attr;
Michal Kazior87263e52013-08-27 13:08:01 +0200909 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300910 struct ath10k_pci_compl *compl;
Michal Kaziorc80de122013-11-25 14:06:23 +0100911 int i, pipe_num, completions;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300912
913 spin_lock_init(&ar_pci->compl_lock);
914 INIT_LIST_HEAD(&ar_pci->compl_process);
915
Michal Kaziorfad6ed72013-11-08 08:01:23 +0100916 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300917 pipe_info = &ar_pci->pipe_info[pipe_num];
918
919 spin_lock_init(&pipe_info->pipe_lock);
920 INIT_LIST_HEAD(&pipe_info->compl_free);
921
922 /* Handle Diagnostic CE specially */
Michal Kaziorc80de122013-11-25 14:06:23 +0100923 if (pipe_info->ce_hdl == ar_pci->ce_diag)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300924 continue;
925
926 attr = &host_ce_config_wlan[pipe_num];
927 completions = 0;
928
Michal Kaziorc80de122013-11-25 14:06:23 +0100929 if (attr->src_nentries)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300930 completions += attr->src_nentries;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300931
Michal Kaziorc80de122013-11-25 14:06:23 +0100932 if (attr->dest_nentries)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300933 completions += attr->dest_nentries;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300934
935 for (i = 0; i < completions; i++) {
Michal Kaziorffe5daa2013-08-13 07:54:55 +0200936 compl = kmalloc(sizeof(*compl), GFP_KERNEL);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300937 if (!compl) {
938 ath10k_warn("No memory for completion state\n");
Michal Kaziorc80de122013-11-25 14:06:23 +0100939 ath10k_pci_cleanup_ce(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300940 return -ENOMEM;
941 }
942
Michal Kaziorf9d8fec2013-08-13 07:54:56 +0200943 compl->state = ATH10K_PCI_COMPL_FREE;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300944 list_add_tail(&compl->list, &pipe_info->compl_free);
945 }
946 }
947
948 return 0;
949}
950
Michal Kaziorc80de122013-11-25 14:06:23 +0100951static int ath10k_pci_setup_ce_irq(struct ath10k *ar)
952{
953 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
954 const struct ce_attr *attr;
955 struct ath10k_pci_pipe *pipe_info;
956 int pipe_num, disable_interrupts;
957
958 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
959 pipe_info = &ar_pci->pipe_info[pipe_num];
960
961 /* Handle Diagnostic CE specially */
962 if (pipe_info->ce_hdl == ar_pci->ce_diag)
963 continue;
964
965 attr = &host_ce_config_wlan[pipe_num];
966
967 if (attr->src_nentries) {
968 disable_interrupts = attr->flags & CE_ATTR_DIS_INTR;
969 ath10k_ce_send_cb_register(pipe_info->ce_hdl,
970 ath10k_pci_ce_send_done,
971 disable_interrupts);
972 }
973
974 if (attr->dest_nentries)
975 ath10k_ce_recv_cb_register(pipe_info->ce_hdl,
976 ath10k_pci_ce_recv_data);
977 }
978
979 return 0;
980}
981
Michal Kazior96a9d0d2013-11-08 08:01:25 +0100982static void ath10k_pci_kill_tasklet(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300983{
984 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300985 int i;
986
Kalle Valo5e3dd152013-06-12 20:52:10 +0300987 tasklet_kill(&ar_pci->intr_tq);
Michal Kazior103d4f52013-11-08 08:01:24 +0100988 tasklet_kill(&ar_pci->msi_fw_err);
Michal Kaziorab977bd2013-11-25 14:06:26 +0100989 tasklet_kill(&ar_pci->early_irq_tasklet);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300990
991 for (i = 0; i < CE_COUNT; i++)
992 tasklet_kill(&ar_pci->pipe_info[i].intr);
Michal Kazior96a9d0d2013-11-08 08:01:25 +0100993}
994
Kalle Valo5e3dd152013-06-12 20:52:10 +0300995static void ath10k_pci_cleanup_ce(struct ath10k *ar)
996{
997 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
998 struct ath10k_pci_compl *compl, *tmp;
Michal Kazior87263e52013-08-27 13:08:01 +0200999 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001000 struct sk_buff *netbuf;
1001 int pipe_num;
1002
1003 /* Free pending completions. */
1004 spin_lock_bh(&ar_pci->compl_lock);
1005 if (!list_empty(&ar_pci->compl_process))
1006 ath10k_warn("pending completions still present! possible memory leaks.\n");
1007
1008 list_for_each_entry_safe(compl, tmp, &ar_pci->compl_process, list) {
1009 list_del(&compl->list);
Kalle Valoaa5c1db2013-09-01 10:01:46 +03001010 netbuf = compl->skb;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001011 dev_kfree_skb_any(netbuf);
1012 kfree(compl);
1013 }
1014 spin_unlock_bh(&ar_pci->compl_lock);
1015
1016 /* Free unused completions for each pipe. */
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001017 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001018 pipe_info = &ar_pci->pipe_info[pipe_num];
1019
1020 spin_lock_bh(&pipe_info->pipe_lock);
1021 list_for_each_entry_safe(compl, tmp,
1022 &pipe_info->compl_free, list) {
1023 list_del(&compl->list);
1024 kfree(compl);
1025 }
1026 spin_unlock_bh(&pipe_info->pipe_lock);
1027 }
1028}
1029
1030static void ath10k_pci_process_ce(struct ath10k *ar)
1031{
1032 struct ath10k_pci *ar_pci = ar->hif.priv;
1033 struct ath10k_hif_cb *cb = &ar_pci->msg_callbacks_current;
1034 struct ath10k_pci_compl *compl;
1035 struct sk_buff *skb;
1036 unsigned int nbytes;
1037 int ret, send_done = 0;
1038
1039 /* Upper layers aren't ready to handle tx/rx completions in parallel so
1040 * we must serialize all completion processing. */
1041
1042 spin_lock_bh(&ar_pci->compl_lock);
1043 if (ar_pci->compl_processing) {
1044 spin_unlock_bh(&ar_pci->compl_lock);
1045 return;
1046 }
1047 ar_pci->compl_processing = true;
1048 spin_unlock_bh(&ar_pci->compl_lock);
1049
1050 for (;;) {
1051 spin_lock_bh(&ar_pci->compl_lock);
1052 if (list_empty(&ar_pci->compl_process)) {
1053 spin_unlock_bh(&ar_pci->compl_lock);
1054 break;
1055 }
1056 compl = list_first_entry(&ar_pci->compl_process,
1057 struct ath10k_pci_compl, list);
1058 list_del(&compl->list);
1059 spin_unlock_bh(&ar_pci->compl_lock);
1060
Michal Kaziorf9d8fec2013-08-13 07:54:56 +02001061 switch (compl->state) {
1062 case ATH10K_PCI_COMPL_SEND:
Kalle Valo5e3dd152013-06-12 20:52:10 +03001063 cb->tx_completion(ar,
Kalle Valoaa5c1db2013-09-01 10:01:46 +03001064 compl->skb,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001065 compl->transfer_id);
1066 send_done = 1;
Michal Kaziorf9d8fec2013-08-13 07:54:56 +02001067 break;
1068 case ATH10K_PCI_COMPL_RECV:
Kalle Valo5e3dd152013-06-12 20:52:10 +03001069 ret = ath10k_pci_post_rx_pipe(compl->pipe_info, 1);
1070 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001071 ath10k_warn("failed to post RX buffer for pipe %d: %d\n",
1072 compl->pipe_info->pipe_num, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001073 break;
1074 }
1075
Kalle Valoaa5c1db2013-09-01 10:01:46 +03001076 skb = compl->skb;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001077 nbytes = compl->nbytes;
1078
1079 ath10k_dbg(ATH10K_DBG_PCI,
1080 "ath10k_pci_ce_recv_data netbuf=%p nbytes=%d\n",
1081 skb, nbytes);
1082 ath10k_dbg_dump(ATH10K_DBG_PCI_DUMP, NULL,
1083 "ath10k rx: ", skb->data, nbytes);
1084
1085 if (skb->len + skb_tailroom(skb) >= nbytes) {
1086 skb_trim(skb, 0);
1087 skb_put(skb, nbytes);
1088 cb->rx_completion(ar, skb,
1089 compl->pipe_info->pipe_num);
1090 } else {
1091 ath10k_warn("rxed more than expected (nbytes %d, max %d)",
1092 nbytes,
1093 skb->len + skb_tailroom(skb));
1094 }
Michal Kaziorf9d8fec2013-08-13 07:54:56 +02001095 break;
1096 case ATH10K_PCI_COMPL_FREE:
1097 ath10k_warn("free completion cannot be processed\n");
1098 break;
1099 default:
1100 ath10k_warn("invalid completion state (%d)\n",
1101 compl->state);
1102 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001103 }
1104
Michal Kaziorf9d8fec2013-08-13 07:54:56 +02001105 compl->state = ATH10K_PCI_COMPL_FREE;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001106
1107 /*
1108 * Add completion back to the pipe's free list.
1109 */
1110 spin_lock_bh(&compl->pipe_info->pipe_lock);
1111 list_add_tail(&compl->list, &compl->pipe_info->compl_free);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001112 spin_unlock_bh(&compl->pipe_info->pipe_lock);
1113 }
1114
1115 spin_lock_bh(&ar_pci->compl_lock);
1116 ar_pci->compl_processing = false;
1117 spin_unlock_bh(&ar_pci->compl_lock);
1118}
1119
1120/* TODO - temporary mapping while we have too few CE's */
1121static int ath10k_pci_hif_map_service_to_pipe(struct ath10k *ar,
1122 u16 service_id, u8 *ul_pipe,
1123 u8 *dl_pipe, int *ul_is_polled,
1124 int *dl_is_polled)
1125{
1126 int ret = 0;
1127
1128 /* polling for received messages not supported */
1129 *dl_is_polled = 0;
1130
1131 switch (service_id) {
1132 case ATH10K_HTC_SVC_ID_HTT_DATA_MSG:
1133 /*
1134 * Host->target HTT gets its own pipe, so it can be polled
1135 * while other pipes are interrupt driven.
1136 */
1137 *ul_pipe = 4;
1138 /*
1139 * Use the same target->host pipe for HTC ctrl, HTC raw
1140 * streams, and HTT.
1141 */
1142 *dl_pipe = 1;
1143 break;
1144
1145 case ATH10K_HTC_SVC_ID_RSVD_CTRL:
1146 case ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS:
1147 /*
1148 * Note: HTC_RAW_STREAMS_SVC is currently unused, and
1149 * HTC_CTRL_RSVD_SVC could share the same pipe as the
1150 * WMI services. So, if another CE is needed, change
1151 * this to *ul_pipe = 3, which frees up CE 0.
1152 */
1153 /* *ul_pipe = 3; */
1154 *ul_pipe = 0;
1155 *dl_pipe = 1;
1156 break;
1157
1158 case ATH10K_HTC_SVC_ID_WMI_DATA_BK:
1159 case ATH10K_HTC_SVC_ID_WMI_DATA_BE:
1160 case ATH10K_HTC_SVC_ID_WMI_DATA_VI:
1161 case ATH10K_HTC_SVC_ID_WMI_DATA_VO:
1162
1163 case ATH10K_HTC_SVC_ID_WMI_CONTROL:
1164 *ul_pipe = 3;
1165 *dl_pipe = 2;
1166 break;
1167
1168 /* pipe 5 unused */
1169 /* pipe 6 reserved */
1170 /* pipe 7 reserved */
1171
1172 default:
1173 ret = -1;
1174 break;
1175 }
1176 *ul_is_polled =
1177 (host_ce_config_wlan[*ul_pipe].flags & CE_ATTR_DIS_INTR) != 0;
1178
1179 return ret;
1180}
1181
1182static void ath10k_pci_hif_get_default_pipe(struct ath10k *ar,
1183 u8 *ul_pipe, u8 *dl_pipe)
1184{
1185 int ul_is_polled, dl_is_polled;
1186
1187 (void)ath10k_pci_hif_map_service_to_pipe(ar,
1188 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1189 ul_pipe,
1190 dl_pipe,
1191 &ul_is_polled,
1192 &dl_is_polled);
1193}
1194
Michal Kazior87263e52013-08-27 13:08:01 +02001195static int ath10k_pci_post_rx_pipe(struct ath10k_pci_pipe *pipe_info,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001196 int num)
1197{
1198 struct ath10k *ar = pipe_info->hif_ce_state;
1199 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2aa39112013-08-27 13:08:02 +02001200 struct ath10k_ce_pipe *ce_state = pipe_info->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001201 struct sk_buff *skb;
1202 dma_addr_t ce_data;
1203 int i, ret = 0;
1204
1205 if (pipe_info->buf_sz == 0)
1206 return 0;
1207
1208 for (i = 0; i < num; i++) {
1209 skb = dev_alloc_skb(pipe_info->buf_sz);
1210 if (!skb) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001211 ath10k_warn("failed to allocate skbuff for pipe %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001212 num);
1213 ret = -ENOMEM;
1214 goto err;
1215 }
1216
1217 WARN_ONCE((unsigned long)skb->data & 3, "unaligned skb");
1218
1219 ce_data = dma_map_single(ar->dev, skb->data,
1220 skb->len + skb_tailroom(skb),
1221 DMA_FROM_DEVICE);
1222
1223 if (unlikely(dma_mapping_error(ar->dev, ce_data))) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001224 ath10k_warn("failed to DMA map sk_buff\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001225 dev_kfree_skb_any(skb);
1226 ret = -EIO;
1227 goto err;
1228 }
1229
1230 ATH10K_SKB_CB(skb)->paddr = ce_data;
1231
1232 pci_dma_sync_single_for_device(ar_pci->pdev, ce_data,
1233 pipe_info->buf_sz,
1234 PCI_DMA_FROMDEVICE);
1235
1236 ret = ath10k_ce_recv_buf_enqueue(ce_state, (void *)skb,
1237 ce_data);
1238 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001239 ath10k_warn("failed to enqueue to pipe %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001240 num, ret);
1241 goto err;
1242 }
1243 }
1244
1245 return ret;
1246
1247err:
1248 ath10k_pci_rx_pipe_cleanup(pipe_info);
1249 return ret;
1250}
1251
1252static int ath10k_pci_post_rx(struct ath10k *ar)
1253{
1254 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +02001255 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001256 const struct ce_attr *attr;
1257 int pipe_num, ret = 0;
1258
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001259 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001260 pipe_info = &ar_pci->pipe_info[pipe_num];
1261 attr = &host_ce_config_wlan[pipe_num];
1262
1263 if (attr->dest_nentries == 0)
1264 continue;
1265
1266 ret = ath10k_pci_post_rx_pipe(pipe_info,
1267 attr->dest_nentries - 1);
1268 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001269 ath10k_warn("failed to post RX buffer for pipe %d: %d\n",
1270 pipe_num, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001271
1272 for (; pipe_num >= 0; pipe_num--) {
1273 pipe_info = &ar_pci->pipe_info[pipe_num];
1274 ath10k_pci_rx_pipe_cleanup(pipe_info);
1275 }
1276 return ret;
1277 }
1278 }
1279
1280 return 0;
1281}
1282
1283static int ath10k_pci_hif_start(struct ath10k *ar)
1284{
1285 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorab977bd2013-11-25 14:06:26 +01001286 int ret, ret_early;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001287
Michal Kaziorab977bd2013-11-25 14:06:26 +01001288 ath10k_pci_free_early_irq(ar);
1289 ath10k_pci_kill_tasklet(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001290
Michal Kaziorc80de122013-11-25 14:06:23 +01001291 ret = ath10k_pci_alloc_compl(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001292 if (ret) {
Michal Kaziorc80de122013-11-25 14:06:23 +01001293 ath10k_warn("failed to allocate CE completions: %d\n", ret);
Michal Kaziorab977bd2013-11-25 14:06:26 +01001294 goto err_early_irq;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001295 }
1296
Michal Kazior5d1aa942013-11-25 14:06:24 +01001297 ret = ath10k_pci_request_irq(ar);
1298 if (ret) {
1299 ath10k_warn("failed to post RX buffers for all pipes: %d\n",
1300 ret);
1301 goto err_free_compl;
1302 }
1303
Michal Kaziorc80de122013-11-25 14:06:23 +01001304 ret = ath10k_pci_setup_ce_irq(ar);
1305 if (ret) {
1306 ath10k_warn("failed to setup CE interrupts: %d\n", ret);
Michal Kazior5d1aa942013-11-25 14:06:24 +01001307 goto err_stop;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001308 }
1309
1310 /* Post buffers once to start things off. */
1311 ret = ath10k_pci_post_rx(ar);
1312 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001313 ath10k_warn("failed to post RX buffers for all pipes: %d\n",
1314 ret);
Michal Kazior5d1aa942013-11-25 14:06:24 +01001315 goto err_stop;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001316 }
1317
1318 ar_pci->started = 1;
1319 return 0;
Michal Kaziorc80de122013-11-25 14:06:23 +01001320
Michal Kazior5d1aa942013-11-25 14:06:24 +01001321err_stop:
1322 ath10k_ce_disable_interrupts(ar);
1323 ath10k_pci_free_irq(ar);
1324 ath10k_pci_kill_tasklet(ar);
Michal Kaziorc80de122013-11-25 14:06:23 +01001325 ath10k_pci_process_ce(ar);
1326err_free_compl:
1327 ath10k_pci_cleanup_ce(ar);
Michal Kaziorab977bd2013-11-25 14:06:26 +01001328err_early_irq:
1329 /* Though there should be no interrupts (device was reset)
1330 * power_down() expects the early IRQ to be installed as per the
1331 * driver lifecycle. */
1332 ret_early = ath10k_pci_request_early_irq(ar);
1333 if (ret_early)
1334 ath10k_warn("failed to re-enable early irq: %d\n", ret_early);
1335
Michal Kaziorc80de122013-11-25 14:06:23 +01001336 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001337}
1338
Michal Kazior87263e52013-08-27 13:08:01 +02001339static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe *pipe_info)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001340{
1341 struct ath10k *ar;
1342 struct ath10k_pci *ar_pci;
Michal Kazior2aa39112013-08-27 13:08:02 +02001343 struct ath10k_ce_pipe *ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001344 u32 buf_sz;
1345 struct sk_buff *netbuf;
1346 u32 ce_data;
1347
1348 buf_sz = pipe_info->buf_sz;
1349
1350 /* Unused Copy Engine */
1351 if (buf_sz == 0)
1352 return;
1353
1354 ar = pipe_info->hif_ce_state;
1355 ar_pci = ath10k_pci_priv(ar);
1356
1357 if (!ar_pci->started)
1358 return;
1359
1360 ce_hdl = pipe_info->ce_hdl;
1361
1362 while (ath10k_ce_revoke_recv_next(ce_hdl, (void **)&netbuf,
1363 &ce_data) == 0) {
1364 dma_unmap_single(ar->dev, ATH10K_SKB_CB(netbuf)->paddr,
1365 netbuf->len + skb_tailroom(netbuf),
1366 DMA_FROM_DEVICE);
1367 dev_kfree_skb_any(netbuf);
1368 }
1369}
1370
Michal Kazior87263e52013-08-27 13:08:01 +02001371static void ath10k_pci_tx_pipe_cleanup(struct ath10k_pci_pipe *pipe_info)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001372{
1373 struct ath10k *ar;
1374 struct ath10k_pci *ar_pci;
Michal Kazior2aa39112013-08-27 13:08:02 +02001375 struct ath10k_ce_pipe *ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001376 struct sk_buff *netbuf;
1377 u32 ce_data;
1378 unsigned int nbytes;
1379 unsigned int id;
1380 u32 buf_sz;
1381
1382 buf_sz = pipe_info->buf_sz;
1383
1384 /* Unused Copy Engine */
1385 if (buf_sz == 0)
1386 return;
1387
1388 ar = pipe_info->hif_ce_state;
1389 ar_pci = ath10k_pci_priv(ar);
1390
1391 if (!ar_pci->started)
1392 return;
1393
1394 ce_hdl = pipe_info->ce_hdl;
1395
1396 while (ath10k_ce_cancel_send_next(ce_hdl, (void **)&netbuf,
1397 &ce_data, &nbytes, &id) == 0) {
Kalle Valoe9bb0aa2013-09-08 18:36:11 +03001398 /*
1399 * Indicate the completion to higer layer to free
1400 * the buffer
1401 */
Michal Kazior2415fc12013-11-08 08:01:32 +01001402
1403 if (!netbuf) {
1404 ath10k_warn("invalid sk_buff on CE %d - NULL pointer. firmware crashed?\n",
1405 ce_hdl->id);
1406 continue;
1407 }
1408
Kalle Valoe9bb0aa2013-09-08 18:36:11 +03001409 ar_pci->msg_callbacks_current.tx_completion(ar,
1410 netbuf,
1411 id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001412 }
1413}
1414
1415/*
1416 * Cleanup residual buffers for device shutdown:
1417 * buffers that were enqueued for receive
1418 * buffers that were to be sent
1419 * Note: Buffers that had completed but which were
1420 * not yet processed are on a completion queue. They
1421 * are handled when the completion thread shuts down.
1422 */
1423static void ath10k_pci_buffer_cleanup(struct ath10k *ar)
1424{
1425 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1426 int pipe_num;
1427
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001428 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Michal Kazior87263e52013-08-27 13:08:01 +02001429 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001430
1431 pipe_info = &ar_pci->pipe_info[pipe_num];
1432 ath10k_pci_rx_pipe_cleanup(pipe_info);
1433 ath10k_pci_tx_pipe_cleanup(pipe_info);
1434 }
1435}
1436
1437static void ath10k_pci_ce_deinit(struct ath10k *ar)
1438{
1439 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +02001440 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001441 int pipe_num;
1442
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001443 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001444 pipe_info = &ar_pci->pipe_info[pipe_num];
1445 if (pipe_info->ce_hdl) {
1446 ath10k_ce_deinit(pipe_info->ce_hdl);
1447 pipe_info->ce_hdl = NULL;
1448 pipe_info->buf_sz = 0;
1449 }
1450 }
1451}
1452
1453static void ath10k_pci_hif_stop(struct ath10k *ar)
1454{
Michal Kazior32270b62013-08-02 09:15:47 +02001455 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior5d1aa942013-11-25 14:06:24 +01001456 int ret;
Michal Kazior32270b62013-08-02 09:15:47 +02001457
Kalle Valo5e3dd152013-06-12 20:52:10 +03001458 ath10k_dbg(ATH10K_DBG_PCI, "%s\n", __func__);
1459
Michal Kazior5d1aa942013-11-25 14:06:24 +01001460 ret = ath10k_ce_disable_interrupts(ar);
1461 if (ret)
1462 ath10k_warn("failed to disable CE interrupts: %d\n", ret);
Michal Kazior32270b62013-08-02 09:15:47 +02001463
Michal Kazior5d1aa942013-11-25 14:06:24 +01001464 ath10k_pci_free_irq(ar);
1465 ath10k_pci_kill_tasklet(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001466
Michal Kaziorab977bd2013-11-25 14:06:26 +01001467 ret = ath10k_pci_request_early_irq(ar);
1468 if (ret)
1469 ath10k_warn("failed to re-enable early irq: %d\n", ret);
1470
Kalle Valo5e3dd152013-06-12 20:52:10 +03001471 /* At this point, asynchronous threads are stopped, the target should
1472 * not DMA nor interrupt. We process the leftovers and then free
1473 * everything else up. */
1474
1475 ath10k_pci_process_ce(ar);
1476 ath10k_pci_cleanup_ce(ar);
1477 ath10k_pci_buffer_cleanup(ar);
Michal Kazior32270b62013-08-02 09:15:47 +02001478
Michal Kazior6a42a472013-11-08 08:01:35 +01001479 /* Make the sure the device won't access any structures on the host by
1480 * resetting it. The device was fed with PCI CE ringbuffer
1481 * configuration during init. If ringbuffers are freed and the device
1482 * were to access them this could lead to memory corruption on the
1483 * host. */
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001484 ath10k_pci_warm_reset(ar);
Michal Kazior6a42a472013-11-08 08:01:35 +01001485
Michal Kazior32270b62013-08-02 09:15:47 +02001486 ar_pci->started = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001487}
1488
1489static int ath10k_pci_hif_exchange_bmi_msg(struct ath10k *ar,
1490 void *req, u32 req_len,
1491 void *resp, u32 *resp_len)
1492{
1493 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2aa39112013-08-27 13:08:02 +02001494 struct ath10k_pci_pipe *pci_tx = &ar_pci->pipe_info[BMI_CE_NUM_TO_TARG];
1495 struct ath10k_pci_pipe *pci_rx = &ar_pci->pipe_info[BMI_CE_NUM_TO_HOST];
1496 struct ath10k_ce_pipe *ce_tx = pci_tx->ce_hdl;
1497 struct ath10k_ce_pipe *ce_rx = pci_rx->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001498 dma_addr_t req_paddr = 0;
1499 dma_addr_t resp_paddr = 0;
1500 struct bmi_xfer xfer = {};
1501 void *treq, *tresp = NULL;
1502 int ret = 0;
1503
Michal Kazior85622cd2013-11-25 14:06:22 +01001504 might_sleep();
1505
Kalle Valo5e3dd152013-06-12 20:52:10 +03001506 if (resp && !resp_len)
1507 return -EINVAL;
1508
1509 if (resp && resp_len && *resp_len == 0)
1510 return -EINVAL;
1511
1512 treq = kmemdup(req, req_len, GFP_KERNEL);
1513 if (!treq)
1514 return -ENOMEM;
1515
1516 req_paddr = dma_map_single(ar->dev, treq, req_len, DMA_TO_DEVICE);
1517 ret = dma_mapping_error(ar->dev, req_paddr);
1518 if (ret)
1519 goto err_dma;
1520
1521 if (resp && resp_len) {
1522 tresp = kzalloc(*resp_len, GFP_KERNEL);
1523 if (!tresp) {
1524 ret = -ENOMEM;
1525 goto err_req;
1526 }
1527
1528 resp_paddr = dma_map_single(ar->dev, tresp, *resp_len,
1529 DMA_FROM_DEVICE);
1530 ret = dma_mapping_error(ar->dev, resp_paddr);
1531 if (ret)
1532 goto err_req;
1533
1534 xfer.wait_for_resp = true;
1535 xfer.resp_len = 0;
1536
1537 ath10k_ce_recv_buf_enqueue(ce_rx, &xfer, resp_paddr);
1538 }
1539
1540 init_completion(&xfer.done);
1541
1542 ret = ath10k_ce_send(ce_tx, &xfer, req_paddr, req_len, -1, 0);
1543 if (ret)
1544 goto err_resp;
1545
Michal Kazior85622cd2013-11-25 14:06:22 +01001546 ret = ath10k_pci_bmi_wait(ce_tx, ce_rx, &xfer);
1547 if (ret) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001548 u32 unused_buffer;
1549 unsigned int unused_nbytes;
1550 unsigned int unused_id;
1551
Kalle Valo5e3dd152013-06-12 20:52:10 +03001552 ath10k_ce_cancel_send_next(ce_tx, NULL, &unused_buffer,
1553 &unused_nbytes, &unused_id);
1554 } else {
1555 /* non-zero means we did not time out */
1556 ret = 0;
1557 }
1558
1559err_resp:
1560 if (resp) {
1561 u32 unused_buffer;
1562
1563 ath10k_ce_revoke_recv_next(ce_rx, NULL, &unused_buffer);
1564 dma_unmap_single(ar->dev, resp_paddr,
1565 *resp_len, DMA_FROM_DEVICE);
1566 }
1567err_req:
1568 dma_unmap_single(ar->dev, req_paddr, req_len, DMA_TO_DEVICE);
1569
1570 if (ret == 0 && resp_len) {
1571 *resp_len = min(*resp_len, xfer.resp_len);
1572 memcpy(resp, tresp, xfer.resp_len);
1573 }
1574err_dma:
1575 kfree(treq);
1576 kfree(tresp);
1577
1578 return ret;
1579}
1580
Michal Kazior5440ce22013-09-03 15:09:58 +02001581static void ath10k_pci_bmi_send_done(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001582{
Michal Kazior5440ce22013-09-03 15:09:58 +02001583 struct bmi_xfer *xfer;
1584 u32 ce_data;
1585 unsigned int nbytes;
1586 unsigned int transfer_id;
1587
1588 if (ath10k_ce_completed_send_next(ce_state, (void **)&xfer, &ce_data,
1589 &nbytes, &transfer_id))
1590 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001591
1592 if (xfer->wait_for_resp)
1593 return;
1594
1595 complete(&xfer->done);
1596}
1597
Michal Kazior5440ce22013-09-03 15:09:58 +02001598static void ath10k_pci_bmi_recv_data(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001599{
Michal Kazior5440ce22013-09-03 15:09:58 +02001600 struct bmi_xfer *xfer;
1601 u32 ce_data;
1602 unsigned int nbytes;
1603 unsigned int transfer_id;
1604 unsigned int flags;
1605
1606 if (ath10k_ce_completed_recv_next(ce_state, (void **)&xfer, &ce_data,
1607 &nbytes, &transfer_id, &flags))
1608 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001609
1610 if (!xfer->wait_for_resp) {
1611 ath10k_warn("unexpected: BMI data received; ignoring\n");
1612 return;
1613 }
1614
1615 xfer->resp_len = nbytes;
1616 complete(&xfer->done);
1617}
1618
Michal Kazior85622cd2013-11-25 14:06:22 +01001619static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
1620 struct ath10k_ce_pipe *rx_pipe,
1621 struct bmi_xfer *xfer)
1622{
1623 unsigned long timeout = jiffies + BMI_COMMUNICATION_TIMEOUT_HZ;
1624
1625 while (time_before_eq(jiffies, timeout)) {
1626 ath10k_pci_bmi_send_done(tx_pipe);
1627 ath10k_pci_bmi_recv_data(rx_pipe);
1628
1629 if (completion_done(&xfer->done))
1630 return 0;
1631
1632 schedule();
1633 }
1634
1635 return -ETIMEDOUT;
1636}
1637
Kalle Valo5e3dd152013-06-12 20:52:10 +03001638/*
1639 * Map from service/endpoint to Copy Engine.
1640 * This table is derived from the CE_PCI TABLE, above.
1641 * It is passed to the Target at startup for use by firmware.
1642 */
1643static const struct service_to_pipe target_service_to_ce_map_wlan[] = {
1644 {
1645 ATH10K_HTC_SVC_ID_WMI_DATA_VO,
1646 PIPEDIR_OUT, /* out = UL = host -> target */
1647 3,
1648 },
1649 {
1650 ATH10K_HTC_SVC_ID_WMI_DATA_VO,
1651 PIPEDIR_IN, /* in = DL = target -> host */
1652 2,
1653 },
1654 {
1655 ATH10K_HTC_SVC_ID_WMI_DATA_BK,
1656 PIPEDIR_OUT, /* out = UL = host -> target */
1657 3,
1658 },
1659 {
1660 ATH10K_HTC_SVC_ID_WMI_DATA_BK,
1661 PIPEDIR_IN, /* in = DL = target -> host */
1662 2,
1663 },
1664 {
1665 ATH10K_HTC_SVC_ID_WMI_DATA_BE,
1666 PIPEDIR_OUT, /* out = UL = host -> target */
1667 3,
1668 },
1669 {
1670 ATH10K_HTC_SVC_ID_WMI_DATA_BE,
1671 PIPEDIR_IN, /* in = DL = target -> host */
1672 2,
1673 },
1674 {
1675 ATH10K_HTC_SVC_ID_WMI_DATA_VI,
1676 PIPEDIR_OUT, /* out = UL = host -> target */
1677 3,
1678 },
1679 {
1680 ATH10K_HTC_SVC_ID_WMI_DATA_VI,
1681 PIPEDIR_IN, /* in = DL = target -> host */
1682 2,
1683 },
1684 {
1685 ATH10K_HTC_SVC_ID_WMI_CONTROL,
1686 PIPEDIR_OUT, /* out = UL = host -> target */
1687 3,
1688 },
1689 {
1690 ATH10K_HTC_SVC_ID_WMI_CONTROL,
1691 PIPEDIR_IN, /* in = DL = target -> host */
1692 2,
1693 },
1694 {
1695 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1696 PIPEDIR_OUT, /* out = UL = host -> target */
1697 0, /* could be moved to 3 (share with WMI) */
1698 },
1699 {
1700 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1701 PIPEDIR_IN, /* in = DL = target -> host */
1702 1,
1703 },
1704 {
1705 ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS, /* not currently used */
1706 PIPEDIR_OUT, /* out = UL = host -> target */
1707 0,
1708 },
1709 {
1710 ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS, /* not currently used */
1711 PIPEDIR_IN, /* in = DL = target -> host */
1712 1,
1713 },
1714 {
1715 ATH10K_HTC_SVC_ID_HTT_DATA_MSG,
1716 PIPEDIR_OUT, /* out = UL = host -> target */
1717 4,
1718 },
1719 {
1720 ATH10K_HTC_SVC_ID_HTT_DATA_MSG,
1721 PIPEDIR_IN, /* in = DL = target -> host */
1722 1,
1723 },
1724
1725 /* (Additions here) */
1726
1727 { /* Must be last */
1728 0,
1729 0,
1730 0,
1731 },
1732};
1733
1734/*
1735 * Send an interrupt to the device to wake up the Target CPU
1736 * so it has an opportunity to notice any changed state.
1737 */
1738static int ath10k_pci_wake_target_cpu(struct ath10k *ar)
1739{
1740 int ret;
1741 u32 core_ctrl;
1742
1743 ret = ath10k_pci_diag_read_access(ar, SOC_CORE_BASE_ADDRESS |
1744 CORE_CTRL_ADDRESS,
1745 &core_ctrl);
1746 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001747 ath10k_warn("failed to read core_ctrl: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001748 return ret;
1749 }
1750
1751 /* A_INUM_FIRMWARE interrupt to Target CPU */
1752 core_ctrl |= CORE_CTRL_CPU_INTR_MASK;
1753
1754 ret = ath10k_pci_diag_write_access(ar, SOC_CORE_BASE_ADDRESS |
1755 CORE_CTRL_ADDRESS,
1756 core_ctrl);
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001757 if (ret) {
1758 ath10k_warn("failed to set target CPU interrupt mask: %d\n",
1759 ret);
1760 return ret;
1761 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001762
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001763 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001764}
1765
1766static int ath10k_pci_init_config(struct ath10k *ar)
1767{
1768 u32 interconnect_targ_addr;
1769 u32 pcie_state_targ_addr = 0;
1770 u32 pipe_cfg_targ_addr = 0;
1771 u32 svc_to_pipe_map = 0;
1772 u32 pcie_config_flags = 0;
1773 u32 ealloc_value;
1774 u32 ealloc_targ_addr;
1775 u32 flag2_value;
1776 u32 flag2_targ_addr;
1777 int ret = 0;
1778
1779 /* Download to Target the CE Config and the service-to-CE map */
1780 interconnect_targ_addr =
1781 host_interest_item_address(HI_ITEM(hi_interconnect_state));
1782
1783 /* Supply Target-side CE configuration */
1784 ret = ath10k_pci_diag_read_access(ar, interconnect_targ_addr,
1785 &pcie_state_targ_addr);
1786 if (ret != 0) {
1787 ath10k_err("Failed to get pcie state addr: %d\n", ret);
1788 return ret;
1789 }
1790
1791 if (pcie_state_targ_addr == 0) {
1792 ret = -EIO;
1793 ath10k_err("Invalid pcie state addr\n");
1794 return ret;
1795 }
1796
1797 ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
1798 offsetof(struct pcie_state,
1799 pipe_cfg_addr),
1800 &pipe_cfg_targ_addr);
1801 if (ret != 0) {
1802 ath10k_err("Failed to get pipe cfg addr: %d\n", ret);
1803 return ret;
1804 }
1805
1806 if (pipe_cfg_targ_addr == 0) {
1807 ret = -EIO;
1808 ath10k_err("Invalid pipe cfg addr\n");
1809 return ret;
1810 }
1811
1812 ret = ath10k_pci_diag_write_mem(ar, pipe_cfg_targ_addr,
1813 target_ce_config_wlan,
1814 sizeof(target_ce_config_wlan));
1815
1816 if (ret != 0) {
1817 ath10k_err("Failed to write pipe cfg: %d\n", ret);
1818 return ret;
1819 }
1820
1821 ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
1822 offsetof(struct pcie_state,
1823 svc_to_pipe_map),
1824 &svc_to_pipe_map);
1825 if (ret != 0) {
1826 ath10k_err("Failed to get svc/pipe map: %d\n", ret);
1827 return ret;
1828 }
1829
1830 if (svc_to_pipe_map == 0) {
1831 ret = -EIO;
1832 ath10k_err("Invalid svc_to_pipe map\n");
1833 return ret;
1834 }
1835
1836 ret = ath10k_pci_diag_write_mem(ar, svc_to_pipe_map,
1837 target_service_to_ce_map_wlan,
1838 sizeof(target_service_to_ce_map_wlan));
1839 if (ret != 0) {
1840 ath10k_err("Failed to write svc/pipe map: %d\n", ret);
1841 return ret;
1842 }
1843
1844 ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
1845 offsetof(struct pcie_state,
1846 config_flags),
1847 &pcie_config_flags);
1848 if (ret != 0) {
1849 ath10k_err("Failed to get pcie config_flags: %d\n", ret);
1850 return ret;
1851 }
1852
1853 pcie_config_flags &= ~PCIE_CONFIG_FLAG_ENABLE_L1;
1854
1855 ret = ath10k_pci_diag_write_mem(ar, pcie_state_targ_addr +
1856 offsetof(struct pcie_state, config_flags),
1857 &pcie_config_flags,
1858 sizeof(pcie_config_flags));
1859 if (ret != 0) {
1860 ath10k_err("Failed to write pcie config_flags: %d\n", ret);
1861 return ret;
1862 }
1863
1864 /* configure early allocation */
1865 ealloc_targ_addr = host_interest_item_address(HI_ITEM(hi_early_alloc));
1866
1867 ret = ath10k_pci_diag_read_access(ar, ealloc_targ_addr, &ealloc_value);
1868 if (ret != 0) {
1869 ath10k_err("Faile to get early alloc val: %d\n", ret);
1870 return ret;
1871 }
1872
1873 /* first bank is switched to IRAM */
1874 ealloc_value |= ((HI_EARLY_ALLOC_MAGIC << HI_EARLY_ALLOC_MAGIC_SHIFT) &
1875 HI_EARLY_ALLOC_MAGIC_MASK);
1876 ealloc_value |= ((1 << HI_EARLY_ALLOC_IRAM_BANKS_SHIFT) &
1877 HI_EARLY_ALLOC_IRAM_BANKS_MASK);
1878
1879 ret = ath10k_pci_diag_write_access(ar, ealloc_targ_addr, ealloc_value);
1880 if (ret != 0) {
1881 ath10k_err("Failed to set early alloc val: %d\n", ret);
1882 return ret;
1883 }
1884
1885 /* Tell Target to proceed with initialization */
1886 flag2_targ_addr = host_interest_item_address(HI_ITEM(hi_option_flag2));
1887
1888 ret = ath10k_pci_diag_read_access(ar, flag2_targ_addr, &flag2_value);
1889 if (ret != 0) {
1890 ath10k_err("Failed to get option val: %d\n", ret);
1891 return ret;
1892 }
1893
1894 flag2_value |= HI_OPTION_EARLY_CFG_DONE;
1895
1896 ret = ath10k_pci_diag_write_access(ar, flag2_targ_addr, flag2_value);
1897 if (ret != 0) {
1898 ath10k_err("Failed to set option val: %d\n", ret);
1899 return ret;
1900 }
1901
1902 return 0;
1903}
1904
1905
1906
1907static int ath10k_pci_ce_init(struct ath10k *ar)
1908{
1909 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +02001910 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001911 const struct ce_attr *attr;
1912 int pipe_num;
1913
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001914 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001915 pipe_info = &ar_pci->pipe_info[pipe_num];
1916 pipe_info->pipe_num = pipe_num;
1917 pipe_info->hif_ce_state = ar;
1918 attr = &host_ce_config_wlan[pipe_num];
1919
1920 pipe_info->ce_hdl = ath10k_ce_init(ar, pipe_num, attr);
1921 if (pipe_info->ce_hdl == NULL) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001922 ath10k_err("failed to initialize CE for pipe: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001923 pipe_num);
1924
1925 /* It is safe to call it here. It checks if ce_hdl is
1926 * valid for each pipe */
1927 ath10k_pci_ce_deinit(ar);
1928 return -1;
1929 }
1930
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001931 if (pipe_num == CE_COUNT - 1) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001932 /*
1933 * Reserve the ultimate CE for
1934 * diagnostic Window support
1935 */
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001936 ar_pci->ce_diag = pipe_info->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001937 continue;
1938 }
1939
1940 pipe_info->buf_sz = (size_t) (attr->src_sz_max);
1941 }
1942
Kalle Valo5e3dd152013-06-12 20:52:10 +03001943 return 0;
1944}
1945
1946static void ath10k_pci_fw_interrupt_handler(struct ath10k *ar)
1947{
1948 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1949 u32 fw_indicator_address, fw_indicator;
1950
1951 ath10k_pci_wake(ar);
1952
1953 fw_indicator_address = ar_pci->fw_indicator_address;
1954 fw_indicator = ath10k_pci_read32(ar, fw_indicator_address);
1955
1956 if (fw_indicator & FW_IND_EVENT_PENDING) {
1957 /* ACK: clear Target-side pending event */
1958 ath10k_pci_write32(ar, fw_indicator_address,
1959 fw_indicator & ~FW_IND_EVENT_PENDING);
1960
1961 if (ar_pci->started) {
1962 ath10k_pci_hif_dump_area(ar);
1963 } else {
1964 /*
1965 * Probable Target failure before we're prepared
1966 * to handle it. Generally unexpected.
1967 */
1968 ath10k_warn("early firmware event indicated\n");
1969 }
1970 }
1971
1972 ath10k_pci_sleep(ar);
1973}
1974
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001975static int ath10k_pci_warm_reset(struct ath10k *ar)
1976{
1977 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1978 int ret = 0;
1979 u32 val;
1980
1981 ath10k_dbg(ATH10K_DBG_BOOT, "boot performing warm chip reset\n");
1982
1983 ret = ath10k_do_pci_wake(ar);
1984 if (ret) {
1985 ath10k_err("failed to wake up target: %d\n", ret);
1986 return ret;
1987 }
1988
1989 /* debug */
1990 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1991 PCIE_INTR_CAUSE_ADDRESS);
1992 ath10k_dbg(ATH10K_DBG_BOOT, "boot host cpu intr cause: 0x%08x\n", val);
1993
1994 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1995 CPU_INTR_ADDRESS);
1996 ath10k_dbg(ATH10K_DBG_BOOT, "boot target cpu intr cause: 0x%08x\n",
1997 val);
1998
1999 /* disable pending irqs */
2000 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
2001 PCIE_INTR_ENABLE_ADDRESS, 0);
2002
2003 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
2004 PCIE_INTR_CLR_ADDRESS, ~0);
2005
2006 msleep(100);
2007
2008 /* clear fw indicator */
2009 ath10k_pci_write32(ar, ar_pci->fw_indicator_address, 0);
2010
2011 /* clear target LF timer interrupts */
2012 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
2013 SOC_LF_TIMER_CONTROL0_ADDRESS);
2014 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS +
2015 SOC_LF_TIMER_CONTROL0_ADDRESS,
2016 val & ~SOC_LF_TIMER_CONTROL0_ENABLE_MASK);
2017
2018 /* reset CE */
2019 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
2020 SOC_RESET_CONTROL_ADDRESS);
2021 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
2022 val | SOC_RESET_CONTROL_CE_RST_MASK);
2023 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
2024 SOC_RESET_CONTROL_ADDRESS);
2025 msleep(10);
2026
2027 /* unreset CE */
2028 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
2029 val & ~SOC_RESET_CONTROL_CE_RST_MASK);
2030 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
2031 SOC_RESET_CONTROL_ADDRESS);
2032 msleep(10);
2033
2034 /* debug */
2035 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
2036 PCIE_INTR_CAUSE_ADDRESS);
2037 ath10k_dbg(ATH10K_DBG_BOOT, "boot host cpu intr cause: 0x%08x\n", val);
2038
2039 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
2040 CPU_INTR_ADDRESS);
2041 ath10k_dbg(ATH10K_DBG_BOOT, "boot target cpu intr cause: 0x%08x\n",
2042 val);
2043
2044 /* CPU warm reset */
2045 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
2046 SOC_RESET_CONTROL_ADDRESS);
2047 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
2048 val | SOC_RESET_CONTROL_CPU_WARM_RST_MASK);
2049
2050 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
2051 SOC_RESET_CONTROL_ADDRESS);
2052 ath10k_dbg(ATH10K_DBG_BOOT, "boot target reset state: 0x%08x\n", val);
2053
2054 msleep(100);
2055
2056 ath10k_dbg(ATH10K_DBG_BOOT, "boot warm reset complete\n");
2057
2058 ath10k_do_pci_sleep(ar);
2059 return ret;
2060}
2061
2062static int __ath10k_pci_hif_power_up(struct ath10k *ar, bool cold_reset)
Michal Kazior8c5c5362013-07-16 09:38:50 +02002063{
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002064 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo95cbb6a2013-11-20 10:00:35 +02002065 const char *irq_mode;
Michal Kazior8c5c5362013-07-16 09:38:50 +02002066 int ret;
2067
2068 /*
2069 * Bring the target up cleanly.
2070 *
2071 * The target may be in an undefined state with an AUX-powered Target
2072 * and a Host in WoW mode. If the Host crashes, loses power, or is
2073 * restarted (without unloading the driver) then the Target is left
2074 * (aux) powered and running. On a subsequent driver load, the Target
2075 * is in an unexpected state. We try to catch that here in order to
2076 * reset the Target and retry the probe.
2077 */
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002078 if (cold_reset)
2079 ret = ath10k_pci_cold_reset(ar);
2080 else
2081 ret = ath10k_pci_warm_reset(ar);
2082
Michal Kazior5b2589f2013-11-08 08:01:30 +01002083 if (ret) {
2084 ath10k_err("failed to reset target: %d\n", ret);
Michal Kazior98563d52013-11-08 08:01:33 +01002085 goto err;
Michal Kazior5b2589f2013-11-08 08:01:30 +01002086 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02002087
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002088 if (!test_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE, ar_pci->features))
Michal Kazior8c5c5362013-07-16 09:38:50 +02002089 /* Force AWAKE forever */
Michal Kazior8c5c5362013-07-16 09:38:50 +02002090 ath10k_do_pci_wake(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02002091
2092 ret = ath10k_pci_ce_init(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02002093 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002094 ath10k_err("failed to initialize CE: %d\n", ret);
Michal Kazior8c5c5362013-07-16 09:38:50 +02002095 goto err_ps;
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002096 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02002097
Michal Kazior98563d52013-11-08 08:01:33 +01002098 ret = ath10k_ce_disable_interrupts(ar);
2099 if (ret) {
2100 ath10k_err("failed to disable CE interrupts: %d\n", ret);
Michal Kazior8c5c5362013-07-16 09:38:50 +02002101 goto err_ce;
2102 }
2103
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002104 ret = ath10k_pci_init_irq(ar);
Michal Kazior98563d52013-11-08 08:01:33 +01002105 if (ret) {
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002106 ath10k_err("failed to init irqs: %d\n", ret);
Michal Kazior98563d52013-11-08 08:01:33 +01002107 goto err_ce;
2108 }
2109
Michal Kaziorab977bd2013-11-25 14:06:26 +01002110 ret = ath10k_pci_request_early_irq(ar);
2111 if (ret) {
2112 ath10k_err("failed to request early irq: %d\n", ret);
2113 goto err_deinit_irq;
2114 }
2115
Michal Kazior98563d52013-11-08 08:01:33 +01002116 ret = ath10k_pci_wait_for_target_init(ar);
2117 if (ret) {
2118 ath10k_err("failed to wait for target to init: %d\n", ret);
Michal Kaziorab977bd2013-11-25 14:06:26 +01002119 goto err_free_early_irq;
Michal Kazior98563d52013-11-08 08:01:33 +01002120 }
2121
2122 ret = ath10k_pci_init_config(ar);
2123 if (ret) {
2124 ath10k_err("failed to setup init config: %d\n", ret);
Michal Kaziorab977bd2013-11-25 14:06:26 +01002125 goto err_free_early_irq;
Michal Kazior98563d52013-11-08 08:01:33 +01002126 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02002127
2128 ret = ath10k_pci_wake_target_cpu(ar);
2129 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002130 ath10k_err("could not wake up target CPU: %d\n", ret);
Michal Kaziorab977bd2013-11-25 14:06:26 +01002131 goto err_free_early_irq;
Michal Kazior8c5c5362013-07-16 09:38:50 +02002132 }
2133
Kalle Valo95cbb6a2013-11-20 10:00:35 +02002134 if (ar_pci->num_msi_intrs > 1)
2135 irq_mode = "MSI-X";
2136 else if (ar_pci->num_msi_intrs == 1)
2137 irq_mode = "MSI";
2138 else
2139 irq_mode = "legacy";
2140
Kalle Valo650b91f2013-11-20 10:00:49 +02002141 if (!test_bit(ATH10K_FLAG_FIRST_BOOT_DONE, &ar->dev_flags))
2142 ath10k_info("pci irq %s\n", irq_mode);
Kalle Valo95cbb6a2013-11-20 10:00:35 +02002143
Michal Kazior8c5c5362013-07-16 09:38:50 +02002144 return 0;
2145
Michal Kaziorab977bd2013-11-25 14:06:26 +01002146err_free_early_irq:
2147 ath10k_pci_free_early_irq(ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002148err_deinit_irq:
2149 ath10k_pci_deinit_irq(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02002150err_ce:
2151 ath10k_pci_ce_deinit(ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002152 ath10k_pci_warm_reset(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02002153err_ps:
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002154 if (!test_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE, ar_pci->features))
Michal Kazior8c5c5362013-07-16 09:38:50 +02002155 ath10k_do_pci_sleep(ar);
2156err:
2157 return ret;
2158}
2159
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002160static int ath10k_pci_hif_power_up(struct ath10k *ar)
2161{
2162 int ret;
2163
2164 /*
2165 * Hardware CUS232 version 2 has some issues with cold reset and the
2166 * preferred (and safer) way to perform a device reset is through a
2167 * warm reset.
2168 *
2169 * Warm reset doesn't always work though (notably after a firmware
2170 * crash) so fall back to cold reset if necessary.
2171 */
2172 ret = __ath10k_pci_hif_power_up(ar, false);
2173 if (ret) {
2174 ath10k_warn("failed to power up target using warm reset (%d), trying cold reset\n",
2175 ret);
2176
2177 ret = __ath10k_pci_hif_power_up(ar, true);
2178 if (ret) {
2179 ath10k_err("failed to power up target using cold reset too (%d)\n",
2180 ret);
2181 return ret;
2182 }
2183 }
2184
2185 return 0;
2186}
2187
Michal Kazior8c5c5362013-07-16 09:38:50 +02002188static void ath10k_pci_hif_power_down(struct ath10k *ar)
2189{
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002190 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2191
Michal Kaziorab977bd2013-11-25 14:06:26 +01002192 ath10k_pci_free_early_irq(ar);
2193 ath10k_pci_kill_tasklet(ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002194 ath10k_pci_deinit_irq(ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002195 ath10k_pci_warm_reset(ar);
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002196
Michal Kazior8c5c5362013-07-16 09:38:50 +02002197 ath10k_pci_ce_deinit(ar);
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002198 if (!test_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE, ar_pci->features))
Michal Kazior8c5c5362013-07-16 09:38:50 +02002199 ath10k_do_pci_sleep(ar);
2200}
2201
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002202#ifdef CONFIG_PM
2203
2204#define ATH10K_PCI_PM_CONTROL 0x44
2205
2206static int ath10k_pci_hif_suspend(struct ath10k *ar)
2207{
2208 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2209 struct pci_dev *pdev = ar_pci->pdev;
2210 u32 val;
2211
2212 pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
2213
2214 if ((val & 0x000000ff) != 0x3) {
2215 pci_save_state(pdev);
2216 pci_disable_device(pdev);
2217 pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
2218 (val & 0xffffff00) | 0x03);
2219 }
2220
2221 return 0;
2222}
2223
2224static int ath10k_pci_hif_resume(struct ath10k *ar)
2225{
2226 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2227 struct pci_dev *pdev = ar_pci->pdev;
2228 u32 val;
2229
2230 pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
2231
2232 if ((val & 0x000000ff) != 0) {
2233 pci_restore_state(pdev);
2234 pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
2235 val & 0xffffff00);
2236 /*
2237 * Suspend/Resume resets the PCI configuration space,
2238 * so we have to re-disable the RETRY_TIMEOUT register (0x41)
2239 * to keep PCI Tx retries from interfering with C3 CPU state
2240 */
2241 pci_read_config_dword(pdev, 0x40, &val);
2242
2243 if ((val & 0x0000ff00) != 0)
2244 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
2245 }
2246
2247 return 0;
2248}
2249#endif
2250
Kalle Valo5e3dd152013-06-12 20:52:10 +03002251static const struct ath10k_hif_ops ath10k_pci_hif_ops = {
2252 .send_head = ath10k_pci_hif_send_head,
2253 .exchange_bmi_msg = ath10k_pci_hif_exchange_bmi_msg,
2254 .start = ath10k_pci_hif_start,
2255 .stop = ath10k_pci_hif_stop,
2256 .map_service_to_pipe = ath10k_pci_hif_map_service_to_pipe,
2257 .get_default_pipe = ath10k_pci_hif_get_default_pipe,
2258 .send_complete_check = ath10k_pci_hif_send_complete_check,
Michal Kaziore799bbf2013-07-05 16:15:12 +03002259 .set_callbacks = ath10k_pci_hif_set_callbacks,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002260 .get_free_queue_number = ath10k_pci_hif_get_free_queue_number,
Michal Kazior8c5c5362013-07-16 09:38:50 +02002261 .power_up = ath10k_pci_hif_power_up,
2262 .power_down = ath10k_pci_hif_power_down,
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002263#ifdef CONFIG_PM
2264 .suspend = ath10k_pci_hif_suspend,
2265 .resume = ath10k_pci_hif_resume,
2266#endif
Kalle Valo5e3dd152013-06-12 20:52:10 +03002267};
2268
2269static void ath10k_pci_ce_tasklet(unsigned long ptr)
2270{
Michal Kazior87263e52013-08-27 13:08:01 +02002271 struct ath10k_pci_pipe *pipe = (struct ath10k_pci_pipe *)ptr;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002272 struct ath10k_pci *ar_pci = pipe->ar_pci;
2273
2274 ath10k_ce_per_engine_service(ar_pci->ar, pipe->pipe_num);
2275}
2276
2277static void ath10k_msi_err_tasklet(unsigned long data)
2278{
2279 struct ath10k *ar = (struct ath10k *)data;
2280
2281 ath10k_pci_fw_interrupt_handler(ar);
2282}
2283
2284/*
2285 * Handler for a per-engine interrupt on a PARTICULAR CE.
2286 * This is used in cases where each CE has a private MSI interrupt.
2287 */
2288static irqreturn_t ath10k_pci_per_engine_handler(int irq, void *arg)
2289{
2290 struct ath10k *ar = arg;
2291 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2292 int ce_id = irq - ar_pci->pdev->irq - MSI_ASSIGN_CE_INITIAL;
2293
Dan Carpentere5742672013-06-18 10:28:46 +03002294 if (ce_id < 0 || ce_id >= ARRAY_SIZE(ar_pci->pipe_info)) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03002295 ath10k_warn("unexpected/invalid irq %d ce_id %d\n", irq, ce_id);
2296 return IRQ_HANDLED;
2297 }
2298
2299 /*
2300 * NOTE: We are able to derive ce_id from irq because we
2301 * use a one-to-one mapping for CE's 0..5.
2302 * CE's 6 & 7 do not use interrupts at all.
2303 *
2304 * This mapping must be kept in sync with the mapping
2305 * used by firmware.
2306 */
2307 tasklet_schedule(&ar_pci->pipe_info[ce_id].intr);
2308 return IRQ_HANDLED;
2309}
2310
2311static irqreturn_t ath10k_pci_msi_fw_handler(int irq, void *arg)
2312{
2313 struct ath10k *ar = arg;
2314 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2315
2316 tasklet_schedule(&ar_pci->msi_fw_err);
2317 return IRQ_HANDLED;
2318}
2319
2320/*
2321 * Top-level interrupt handler for all PCI interrupts from a Target.
2322 * When a block of MSI interrupts is allocated, this top-level handler
2323 * is not used; instead, we directly call the correct sub-handler.
2324 */
2325static irqreturn_t ath10k_pci_interrupt_handler(int irq, void *arg)
2326{
2327 struct ath10k *ar = arg;
2328 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2329
2330 if (ar_pci->num_msi_intrs == 0) {
Michal Kaziore5398872013-11-25 14:06:20 +01002331 if (!ath10k_pci_irq_pending(ar))
2332 return IRQ_NONE;
2333
Michal Kazior26852182013-11-25 14:06:25 +01002334 ath10k_pci_disable_and_clear_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002335 }
2336
2337 tasklet_schedule(&ar_pci->intr_tq);
2338
2339 return IRQ_HANDLED;
2340}
2341
Michal Kaziorab977bd2013-11-25 14:06:26 +01002342static void ath10k_pci_early_irq_tasklet(unsigned long data)
2343{
2344 struct ath10k *ar = (struct ath10k *)data;
2345 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2346 u32 fw_ind;
2347 int ret;
2348
2349 ret = ath10k_pci_wake(ar);
2350 if (ret) {
2351 ath10k_warn("failed to wake target in early irq tasklet: %d\n",
2352 ret);
2353 return;
2354 }
2355
2356 fw_ind = ath10k_pci_read32(ar, ar_pci->fw_indicator_address);
2357 if (fw_ind & FW_IND_EVENT_PENDING) {
2358 ath10k_pci_write32(ar, ar_pci->fw_indicator_address,
2359 fw_ind & ~FW_IND_EVENT_PENDING);
2360
2361 /* Some structures are unavailable during early boot or at
2362 * driver teardown so just print that the device has crashed. */
2363 ath10k_warn("device crashed - no diagnostics available\n");
2364 }
2365
2366 ath10k_pci_sleep(ar);
2367 ath10k_pci_enable_legacy_irq(ar);
2368}
2369
Kalle Valo5e3dd152013-06-12 20:52:10 +03002370static void ath10k_pci_tasklet(unsigned long data)
2371{
2372 struct ath10k *ar = (struct ath10k *)data;
2373 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2374
2375 ath10k_pci_fw_interrupt_handler(ar); /* FIXME: Handle FW error */
2376 ath10k_ce_per_engine_service_any(ar);
2377
Michal Kazior26852182013-11-25 14:06:25 +01002378 /* Re-enable legacy irq that was disabled in the irq handler */
2379 if (ar_pci->num_msi_intrs == 0)
2380 ath10k_pci_enable_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002381}
2382
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002383static int ath10k_pci_request_irq_msix(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002384{
2385 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002386 int ret, i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002387
2388 ret = request_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW,
2389 ath10k_pci_msi_fw_handler,
2390 IRQF_SHARED, "ath10k_pci", ar);
Michal Kazior591ecdb2013-07-31 10:55:15 +02002391 if (ret) {
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002392 ath10k_warn("failed to request MSI-X fw irq %d: %d\n",
Michal Kazior591ecdb2013-07-31 10:55:15 +02002393 ar_pci->pdev->irq + MSI_ASSIGN_FW, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002394 return ret;
Michal Kazior591ecdb2013-07-31 10:55:15 +02002395 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002396
2397 for (i = MSI_ASSIGN_CE_INITIAL; i <= MSI_ASSIGN_CE_MAX; i++) {
2398 ret = request_irq(ar_pci->pdev->irq + i,
2399 ath10k_pci_per_engine_handler,
2400 IRQF_SHARED, "ath10k_pci", ar);
2401 if (ret) {
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002402 ath10k_warn("failed to request MSI-X ce irq %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002403 ar_pci->pdev->irq + i, ret);
2404
Michal Kazior87b14232013-06-26 08:50:50 +02002405 for (i--; i >= MSI_ASSIGN_CE_INITIAL; i--)
2406 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002407
Michal Kazior87b14232013-06-26 08:50:50 +02002408 free_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002409 return ret;
2410 }
2411 }
2412
Kalle Valo5e3dd152013-06-12 20:52:10 +03002413 return 0;
2414}
2415
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002416static int ath10k_pci_request_irq_msi(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002417{
2418 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2419 int ret;
2420
2421 ret = request_irq(ar_pci->pdev->irq,
2422 ath10k_pci_interrupt_handler,
2423 IRQF_SHARED, "ath10k_pci", ar);
Kalle Valof3782742013-10-17 11:36:15 +03002424 if (ret) {
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002425 ath10k_warn("failed to request MSI irq %d: %d\n",
2426 ar_pci->pdev->irq, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002427 return ret;
Kalle Valof3782742013-10-17 11:36:15 +03002428 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002429
Kalle Valo5e3dd152013-06-12 20:52:10 +03002430 return 0;
2431}
2432
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002433static int ath10k_pci_request_irq_legacy(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002434{
2435 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002436 int ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002437
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002438 ret = request_irq(ar_pci->pdev->irq,
2439 ath10k_pci_interrupt_handler,
2440 IRQF_SHARED, "ath10k_pci", ar);
Kalle Valof3782742013-10-17 11:36:15 +03002441 if (ret) {
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002442 ath10k_warn("failed to request legacy irq %d: %d\n",
2443 ar_pci->pdev->irq, ret);
Kalle Valof3782742013-10-17 11:36:15 +03002444 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002445 }
2446
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002447 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002448}
2449
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002450static int ath10k_pci_request_irq(struct ath10k *ar)
2451{
2452 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2453
2454 switch (ar_pci->num_msi_intrs) {
2455 case 0:
2456 return ath10k_pci_request_irq_legacy(ar);
2457 case 1:
2458 return ath10k_pci_request_irq_msi(ar);
2459 case MSI_NUM_REQUEST:
2460 return ath10k_pci_request_irq_msix(ar);
2461 }
2462
2463 ath10k_warn("unknown irq configuration upon request\n");
2464 return -EINVAL;
2465}
2466
2467static void ath10k_pci_free_irq(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002468{
2469 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2470 int i;
2471
2472 /* There's at least one interrupt irregardless whether its legacy INTR
2473 * or MSI or MSI-X */
2474 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
2475 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002476}
2477
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002478static void ath10k_pci_init_irq_tasklets(struct ath10k *ar)
2479{
2480 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2481 int i;
2482
2483 tasklet_init(&ar_pci->intr_tq, ath10k_pci_tasklet, (unsigned long)ar);
2484 tasklet_init(&ar_pci->msi_fw_err, ath10k_msi_err_tasklet,
2485 (unsigned long)ar);
Michal Kaziorab977bd2013-11-25 14:06:26 +01002486 tasklet_init(&ar_pci->early_irq_tasklet, ath10k_pci_early_irq_tasklet,
2487 (unsigned long)ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002488
2489 for (i = 0; i < CE_COUNT; i++) {
2490 ar_pci->pipe_info[i].ar_pci = ar_pci;
2491 tasklet_init(&ar_pci->pipe_info[i].intr, ath10k_pci_ce_tasklet,
2492 (unsigned long)&ar_pci->pipe_info[i]);
2493 }
2494}
2495
2496static int ath10k_pci_init_irq(struct ath10k *ar)
2497{
2498 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002499 bool msix_supported = test_bit(ATH10K_PCI_FEATURE_MSI_X,
2500 ar_pci->features);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002501 int ret;
2502
2503 ath10k_pci_init_irq_tasklets(ar);
2504
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002505 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_AUTO &&
2506 !test_bit(ATH10K_FLAG_FIRST_BOOT_DONE, &ar->dev_flags))
2507 ath10k_info("limiting irq mode to: %d\n", ath10k_pci_irq_mode);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002508
2509 /* Try MSI-X */
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002510 if (ath10k_pci_irq_mode == ATH10K_PCI_IRQ_AUTO && msix_supported) {
2511 ar_pci->num_msi_intrs = MSI_NUM_REQUEST;
Alexander Gordeev5ad68672014-02-13 17:50:02 +02002512 ret = pci_enable_msi_range(ar_pci->pdev, ar_pci->num_msi_intrs,
2513 ar_pci->num_msi_intrs);
2514 if (ret > 0)
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002515 return 0;
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002516
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002517 /* fall-through */
2518 }
2519
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002520 /* Try MSI */
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002521 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_LEGACY) {
2522 ar_pci->num_msi_intrs = 1;
2523 ret = pci_enable_msi(ar_pci->pdev);
2524 if (ret == 0)
2525 return 0;
2526
2527 /* fall-through */
2528 }
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002529
2530 /* Try legacy irq
2531 *
2532 * A potential race occurs here: The CORE_BASE write
2533 * depends on target correctly decoding AXI address but
2534 * host won't know when target writes BAR to CORE_CTRL.
2535 * This write might get lost if target has NOT written BAR.
2536 * For now, fix the race by repeating the write in below
2537 * synchronization checking. */
2538 ar_pci->num_msi_intrs = 0;
2539
2540 ret = ath10k_pci_wake(ar);
2541 if (ret) {
2542 ath10k_warn("failed to wake target: %d\n", ret);
2543 return ret;
2544 }
2545
2546 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2547 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
2548 ath10k_pci_sleep(ar);
2549
2550 return 0;
2551}
2552
2553static int ath10k_pci_deinit_irq_legacy(struct ath10k *ar)
2554{
2555 int ret;
2556
2557 ret = ath10k_pci_wake(ar);
2558 if (ret) {
2559 ath10k_warn("failed to wake target: %d\n", ret);
2560 return ret;
2561 }
2562
2563 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2564 0);
2565 ath10k_pci_sleep(ar);
2566
2567 return 0;
2568}
2569
2570static int ath10k_pci_deinit_irq(struct ath10k *ar)
2571{
2572 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2573
2574 switch (ar_pci->num_msi_intrs) {
2575 case 0:
2576 return ath10k_pci_deinit_irq_legacy(ar);
2577 case 1:
2578 /* fall-through */
2579 case MSI_NUM_REQUEST:
2580 pci_disable_msi(ar_pci->pdev);
2581 return 0;
Alexander Gordeevbb8b6212014-02-13 17:50:01 +02002582 default:
2583 pci_disable_msi(ar_pci->pdev);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002584 }
2585
2586 ath10k_warn("unknown irq configuration upon deinit\n");
2587 return -EINVAL;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002588}
2589
Michal Kaziord7fb47f2013-11-08 08:01:26 +01002590static int ath10k_pci_wait_for_target_init(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002591{
2592 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2593 int wait_limit = 300; /* 3 sec */
Kalle Valof3782742013-10-17 11:36:15 +03002594 int ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002595
Michal Kazior98563d52013-11-08 08:01:33 +01002596 ret = ath10k_pci_wake(ar);
Kalle Valof3782742013-10-17 11:36:15 +03002597 if (ret) {
Michal Kazior5b2589f2013-11-08 08:01:30 +01002598 ath10k_err("failed to wake up target: %d\n", ret);
Kalle Valof3782742013-10-17 11:36:15 +03002599 return ret;
2600 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002601
2602 while (wait_limit-- &&
2603 !(ioread32(ar_pci->mem + FW_INDICATOR_ADDRESS) &
2604 FW_IND_INITIALIZED)) {
2605 if (ar_pci->num_msi_intrs == 0)
2606 /* Fix potential race by repeating CORE_BASE writes */
2607 iowrite32(PCIE_INTR_FIRMWARE_MASK |
2608 PCIE_INTR_CE_MASK_ALL,
2609 ar_pci->mem + (SOC_CORE_BASE_ADDRESS |
2610 PCIE_INTR_ENABLE_ADDRESS));
2611 mdelay(10);
2612 }
2613
2614 if (wait_limit < 0) {
Michal Kazior5b2589f2013-11-08 08:01:30 +01002615 ath10k_err("target stalled\n");
2616 ret = -EIO;
2617 goto out;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002618 }
2619
Michal Kazior5b2589f2013-11-08 08:01:30 +01002620out:
Michal Kazior98563d52013-11-08 08:01:33 +01002621 ath10k_pci_sleep(ar);
Michal Kazior5b2589f2013-11-08 08:01:30 +01002622 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002623}
2624
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002625static int ath10k_pci_cold_reset(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002626{
Michal Kazior5b2589f2013-11-08 08:01:30 +01002627 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002628 u32 val;
2629
Michal Kazior5b2589f2013-11-08 08:01:30 +01002630 ret = ath10k_do_pci_wake(ar);
2631 if (ret) {
2632 ath10k_err("failed to wake up target: %d\n",
2633 ret);
2634 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002635 }
2636
2637 /* Put Target, including PCIe, into RESET. */
Kalle Valoe479ed42013-09-01 10:01:53 +03002638 val = ath10k_pci_reg_read32(ar, SOC_GLOBAL_RESET_ADDRESS);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002639 val |= 1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002640 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002641
2642 for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
Kalle Valoe479ed42013-09-01 10:01:53 +03002643 if (ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03002644 RTC_STATE_COLD_RESET_MASK)
2645 break;
2646 msleep(1);
2647 }
2648
2649 /* Pull Target, including PCIe, out of RESET. */
2650 val &= ~1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002651 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002652
2653 for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
Kalle Valoe479ed42013-09-01 10:01:53 +03002654 if (!(ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03002655 RTC_STATE_COLD_RESET_MASK))
2656 break;
2657 msleep(1);
2658 }
2659
Michal Kazior5b2589f2013-11-08 08:01:30 +01002660 ath10k_do_pci_sleep(ar);
2661 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002662}
2663
2664static void ath10k_pci_dump_features(struct ath10k_pci *ar_pci)
2665{
2666 int i;
2667
2668 for (i = 0; i < ATH10K_PCI_FEATURE_COUNT; i++) {
2669 if (!test_bit(i, ar_pci->features))
2670 continue;
2671
2672 switch (i) {
2673 case ATH10K_PCI_FEATURE_MSI_X:
Kalle Valo24cfade2013-09-08 17:55:50 +03002674 ath10k_dbg(ATH10K_DBG_BOOT, "device supports MSI-X\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002675 break;
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002676 case ATH10K_PCI_FEATURE_SOC_POWER_SAVE:
Kalle Valo24cfade2013-09-08 17:55:50 +03002677 ath10k_dbg(ATH10K_DBG_BOOT, "QCA98XX SoC power save enabled\n");
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002678 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002679 }
2680 }
2681}
2682
2683static int ath10k_pci_probe(struct pci_dev *pdev,
2684 const struct pci_device_id *pci_dev)
2685{
2686 void __iomem *mem;
2687 int ret = 0;
2688 struct ath10k *ar;
2689 struct ath10k_pci *ar_pci;
Kalle Valoe01ae682013-09-01 11:22:14 +03002690 u32 lcr_val, chip_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002691
2692 ath10k_dbg(ATH10K_DBG_PCI, "%s\n", __func__);
2693
2694 ar_pci = kzalloc(sizeof(*ar_pci), GFP_KERNEL);
2695 if (ar_pci == NULL)
2696 return -ENOMEM;
2697
2698 ar_pci->pdev = pdev;
2699 ar_pci->dev = &pdev->dev;
2700
2701 switch (pci_dev->device) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03002702 case QCA988X_2_0_DEVICE_ID:
2703 set_bit(ATH10K_PCI_FEATURE_MSI_X, ar_pci->features);
2704 break;
2705 default:
2706 ret = -ENODEV;
Masanari Iida6d3be302013-09-30 23:19:09 +09002707 ath10k_err("Unknown device ID: %d\n", pci_dev->device);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002708 goto err_ar_pci;
2709 }
2710
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002711 if (ath10k_target_ps)
2712 set_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE, ar_pci->features);
2713
Kalle Valo5e3dd152013-06-12 20:52:10 +03002714 ath10k_pci_dump_features(ar_pci);
2715
Michal Kazior3a0861f2013-07-05 16:15:06 +03002716 ar = ath10k_core_create(ar_pci, ar_pci->dev, &ath10k_pci_hif_ops);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002717 if (!ar) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002718 ath10k_err("failed to create driver core\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002719 ret = -EINVAL;
2720 goto err_ar_pci;
2721 }
2722
Kalle Valo5e3dd152013-06-12 20:52:10 +03002723 ar_pci->ar = ar;
2724 ar_pci->fw_indicator_address = FW_INDICATOR_ADDRESS;
2725 atomic_set(&ar_pci->keep_awake_count, 0);
2726
2727 pci_set_drvdata(pdev, ar);
2728
2729 /*
2730 * Without any knowledge of the Host, the Target may have been reset or
2731 * power cycled and its Config Space may no longer reflect the PCI
2732 * address space that was assigned earlier by the PCI infrastructure.
2733 * Refresh it now.
2734 */
2735 ret = pci_assign_resource(pdev, BAR_NUM);
2736 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002737 ath10k_err("failed to assign PCI space: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002738 goto err_ar;
2739 }
2740
2741 ret = pci_enable_device(pdev);
2742 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002743 ath10k_err("failed to enable PCI device: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002744 goto err_ar;
2745 }
2746
2747 /* Request MMIO resources */
2748 ret = pci_request_region(pdev, BAR_NUM, "ath");
2749 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002750 ath10k_err("failed to request MMIO region: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002751 goto err_device;
2752 }
2753
2754 /*
2755 * Target structures have a limit of 32 bit DMA pointers.
2756 * DMA pointers can be wider than 32 bits by default on some systems.
2757 */
2758 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2759 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002760 ath10k_err("failed to set DMA mask to 32-bit: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002761 goto err_region;
2762 }
2763
2764 ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2765 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002766 ath10k_err("failed to set consistent DMA mask to 32-bit\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002767 goto err_region;
2768 }
2769
2770 /* Set bus master bit in PCI_COMMAND to enable DMA */
2771 pci_set_master(pdev);
2772
2773 /*
2774 * Temporary FIX: disable ASPM
2775 * Will be removed after the OTP is programmed
2776 */
2777 pci_read_config_dword(pdev, 0x80, &lcr_val);
2778 pci_write_config_dword(pdev, 0x80, (lcr_val & 0xffffff00));
2779
2780 /* Arrange for access to Target SoC registers. */
2781 mem = pci_iomap(pdev, BAR_NUM, 0);
2782 if (!mem) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002783 ath10k_err("failed to perform IOMAP for BAR%d\n", BAR_NUM);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002784 ret = -EIO;
2785 goto err_master;
2786 }
2787
2788 ar_pci->mem = mem;
2789
2790 spin_lock_init(&ar_pci->ce_lock);
2791
Kalle Valoe01ae682013-09-01 11:22:14 +03002792 ret = ath10k_do_pci_wake(ar);
2793 if (ret) {
2794 ath10k_err("Failed to get chip id: %d\n", ret);
Wei Yongjun12eb0872013-10-30 13:24:39 +08002795 goto err_iomap;
Kalle Valoe01ae682013-09-01 11:22:14 +03002796 }
2797
Kalle Valo233eb972013-10-16 16:46:11 +03002798 chip_id = ath10k_pci_soc_read32(ar, SOC_CHIP_ID_ADDRESS);
Kalle Valoe01ae682013-09-01 11:22:14 +03002799
2800 ath10k_do_pci_sleep(ar);
2801
Kalle Valo24cfade2013-09-08 17:55:50 +03002802 ath10k_dbg(ATH10K_DBG_BOOT, "boot pci_mem 0x%p\n", ar_pci->mem);
2803
Kalle Valoe01ae682013-09-01 11:22:14 +03002804 ret = ath10k_core_register(ar, chip_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002805 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002806 ath10k_err("failed to register driver core: %d\n", ret);
Michal Kazior32270b62013-08-02 09:15:47 +02002807 goto err_iomap;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002808 }
2809
2810 return 0;
2811
Kalle Valo5e3dd152013-06-12 20:52:10 +03002812err_iomap:
2813 pci_iounmap(pdev, mem);
2814err_master:
2815 pci_clear_master(pdev);
2816err_region:
2817 pci_release_region(pdev, BAR_NUM);
2818err_device:
2819 pci_disable_device(pdev);
2820err_ar:
Kalle Valo5e3dd152013-06-12 20:52:10 +03002821 ath10k_core_destroy(ar);
2822err_ar_pci:
2823 /* call HIF PCI free here */
2824 kfree(ar_pci);
2825
2826 return ret;
2827}
2828
2829static void ath10k_pci_remove(struct pci_dev *pdev)
2830{
2831 struct ath10k *ar = pci_get_drvdata(pdev);
2832 struct ath10k_pci *ar_pci;
2833
2834 ath10k_dbg(ATH10K_DBG_PCI, "%s\n", __func__);
2835
2836 if (!ar)
2837 return;
2838
2839 ar_pci = ath10k_pci_priv(ar);
2840
2841 if (!ar_pci)
2842 return;
2843
2844 tasklet_kill(&ar_pci->msi_fw_err);
2845
2846 ath10k_core_unregister(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002847
Kalle Valo5e3dd152013-06-12 20:52:10 +03002848 pci_iounmap(pdev, ar_pci->mem);
2849 pci_release_region(pdev, BAR_NUM);
2850 pci_clear_master(pdev);
2851 pci_disable_device(pdev);
2852
2853 ath10k_core_destroy(ar);
2854 kfree(ar_pci);
2855}
2856
Kalle Valo5e3dd152013-06-12 20:52:10 +03002857MODULE_DEVICE_TABLE(pci, ath10k_pci_id_table);
2858
2859static struct pci_driver ath10k_pci_driver = {
2860 .name = "ath10k_pci",
2861 .id_table = ath10k_pci_id_table,
2862 .probe = ath10k_pci_probe,
2863 .remove = ath10k_pci_remove,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002864};
2865
2866static int __init ath10k_pci_init(void)
2867{
2868 int ret;
2869
2870 ret = pci_register_driver(&ath10k_pci_driver);
2871 if (ret)
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002872 ath10k_err("failed to register PCI driver: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002873
2874 return ret;
2875}
2876module_init(ath10k_pci_init);
2877
2878static void __exit ath10k_pci_exit(void)
2879{
2880 pci_unregister_driver(&ath10k_pci_driver);
2881}
2882
2883module_exit(ath10k_pci_exit);
2884
2885MODULE_AUTHOR("Qualcomm Atheros");
2886MODULE_DESCRIPTION("Driver support for Atheros QCA988X PCIe devices");
2887MODULE_LICENSE("Dual BSD/GPL");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002888MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_FW_FILE);
2889MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_OTP_FILE);
2890MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_BOARD_DATA_FILE);