blob: 43d63677b1332f75db614ab274ed316daa7ba7e2 [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 Valo0399eca2014-03-28 09:32:21 +020051/* how long wait to wait for target to initialise, in ms */
52#define ATH10K_PCI_TARGET_WAIT 3000
53
Kalle Valo5e3dd152013-06-12 20:52:10 +030054#define QCA988X_2_0_DEVICE_ID (0x003c)
55
56static DEFINE_PCI_DEVICE_TABLE(ath10k_pci_id_table) = {
Kalle Valo5e3dd152013-06-12 20:52:10 +030057 { PCI_VDEVICE(ATHEROS, QCA988X_2_0_DEVICE_ID) }, /* PCI-E QCA988X V2 */
58 {0}
59};
60
61static int ath10k_pci_diag_read_access(struct ath10k *ar, u32 address,
62 u32 *data);
63
Kalle Valo5e3dd152013-06-12 20:52:10 +030064static int ath10k_pci_post_rx(struct ath10k *ar);
Michal Kazior87263e52013-08-27 13:08:01 +020065static int ath10k_pci_post_rx_pipe(struct ath10k_pci_pipe *pipe_info,
Kalle Valo5e3dd152013-06-12 20:52:10 +030066 int num);
Michal Kazior87263e52013-08-27 13:08:01 +020067static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe *pipe_info);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +010068static int ath10k_pci_cold_reset(struct ath10k *ar);
69static int ath10k_pci_warm_reset(struct ath10k *ar);
Michal Kaziord7fb47f2013-11-08 08:01:26 +010070static int ath10k_pci_wait_for_target_init(struct ath10k *ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +010071static int ath10k_pci_init_irq(struct ath10k *ar);
72static int ath10k_pci_deinit_irq(struct ath10k *ar);
73static int ath10k_pci_request_irq(struct ath10k *ar);
74static void ath10k_pci_free_irq(struct ath10k *ar);
Michal Kazior85622cd2013-11-25 14:06:22 +010075static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
76 struct ath10k_ce_pipe *rx_pipe,
77 struct bmi_xfer *xfer);
Kalle Valo5e3dd152013-06-12 20:52:10 +030078
79static const struct ce_attr host_ce_config_wlan[] = {
Kalle Valo48e9c222013-09-01 10:01:32 +030080 /* CE0: host->target HTC control and raw streams */
81 {
82 .flags = CE_ATTR_FLAGS,
83 .src_nentries = 16,
84 .src_sz_max = 256,
85 .dest_nentries = 0,
86 },
87
88 /* CE1: target->host HTT + HTC control */
89 {
90 .flags = CE_ATTR_FLAGS,
91 .src_nentries = 0,
92 .src_sz_max = 512,
93 .dest_nentries = 512,
94 },
95
96 /* CE2: target->host WMI */
97 {
98 .flags = CE_ATTR_FLAGS,
99 .src_nentries = 0,
100 .src_sz_max = 2048,
101 .dest_nentries = 32,
102 },
103
104 /* CE3: host->target WMI */
105 {
106 .flags = CE_ATTR_FLAGS,
107 .src_nentries = 32,
108 .src_sz_max = 2048,
109 .dest_nentries = 0,
110 },
111
112 /* CE4: host->target HTT */
113 {
114 .flags = CE_ATTR_FLAGS | CE_ATTR_DIS_INTR,
115 .src_nentries = CE_HTT_H2T_MSG_SRC_NENTRIES,
116 .src_sz_max = 256,
117 .dest_nentries = 0,
118 },
119
120 /* CE5: unused */
121 {
122 .flags = CE_ATTR_FLAGS,
123 .src_nentries = 0,
124 .src_sz_max = 0,
125 .dest_nentries = 0,
126 },
127
128 /* CE6: target autonomous hif_memcpy */
129 {
130 .flags = CE_ATTR_FLAGS,
131 .src_nentries = 0,
132 .src_sz_max = 0,
133 .dest_nentries = 0,
134 },
135
136 /* CE7: ce_diag, the Diagnostic Window */
137 {
138 .flags = CE_ATTR_FLAGS,
139 .src_nentries = 2,
140 .src_sz_max = DIAG_TRANSFER_LIMIT,
141 .dest_nentries = 2,
142 },
Kalle Valo5e3dd152013-06-12 20:52:10 +0300143};
144
145/* Target firmware's Copy Engine configuration. */
146static const struct ce_pipe_config target_ce_config_wlan[] = {
Kalle Valod88effb2013-09-01 10:01:39 +0300147 /* CE0: host->target HTC control and raw streams */
148 {
149 .pipenum = 0,
150 .pipedir = PIPEDIR_OUT,
151 .nentries = 32,
152 .nbytes_max = 256,
153 .flags = CE_ATTR_FLAGS,
154 .reserved = 0,
155 },
156
157 /* CE1: target->host HTT + HTC control */
158 {
159 .pipenum = 1,
160 .pipedir = PIPEDIR_IN,
161 .nentries = 32,
162 .nbytes_max = 512,
163 .flags = CE_ATTR_FLAGS,
164 .reserved = 0,
165 },
166
167 /* CE2: target->host WMI */
168 {
169 .pipenum = 2,
170 .pipedir = PIPEDIR_IN,
171 .nentries = 32,
172 .nbytes_max = 2048,
173 .flags = CE_ATTR_FLAGS,
174 .reserved = 0,
175 },
176
177 /* CE3: host->target WMI */
178 {
179 .pipenum = 3,
180 .pipedir = PIPEDIR_OUT,
181 .nentries = 32,
182 .nbytes_max = 2048,
183 .flags = CE_ATTR_FLAGS,
184 .reserved = 0,
185 },
186
187 /* CE4: host->target HTT */
188 {
189 .pipenum = 4,
190 .pipedir = PIPEDIR_OUT,
191 .nentries = 256,
192 .nbytes_max = 256,
193 .flags = CE_ATTR_FLAGS,
194 .reserved = 0,
195 },
196
Kalle Valo5e3dd152013-06-12 20:52:10 +0300197 /* NB: 50% of src nentries, since tx has 2 frags */
Kalle Valod88effb2013-09-01 10:01:39 +0300198
199 /* CE5: unused */
200 {
201 .pipenum = 5,
202 .pipedir = PIPEDIR_OUT,
203 .nentries = 32,
204 .nbytes_max = 2048,
205 .flags = CE_ATTR_FLAGS,
206 .reserved = 0,
207 },
208
209 /* CE6: Reserved for target autonomous hif_memcpy */
210 {
211 .pipenum = 6,
212 .pipedir = PIPEDIR_INOUT,
213 .nentries = 32,
214 .nbytes_max = 4096,
215 .flags = CE_ATTR_FLAGS,
216 .reserved = 0,
217 },
218
Kalle Valo5e3dd152013-06-12 20:52:10 +0300219 /* CE7 used only by Host */
220};
221
Michal Kaziore5398872013-11-25 14:06:20 +0100222static bool ath10k_pci_irq_pending(struct ath10k *ar)
223{
224 u32 cause;
225
226 /* Check if the shared legacy irq is for us */
227 cause = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
228 PCIE_INTR_CAUSE_ADDRESS);
229 if (cause & (PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL))
230 return true;
231
232 return false;
233}
234
Michal Kazior26852182013-11-25 14:06:25 +0100235static void ath10k_pci_disable_and_clear_legacy_irq(struct ath10k *ar)
236{
237 /* IMPORTANT: INTR_CLR register has to be set after
238 * INTR_ENABLE is set to 0, otherwise interrupt can not be
239 * really cleared. */
240 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
241 0);
242 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_CLR_ADDRESS,
243 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
244
245 /* IMPORTANT: this extra read transaction is required to
246 * flush the posted write buffer. */
247 (void) ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
248 PCIE_INTR_ENABLE_ADDRESS);
249}
250
251static void ath10k_pci_enable_legacy_irq(struct ath10k *ar)
252{
253 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
254 PCIE_INTR_ENABLE_ADDRESS,
255 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
256
257 /* IMPORTANT: this extra read transaction is required to
258 * flush the posted write buffer. */
259 (void) ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
260 PCIE_INTR_ENABLE_ADDRESS);
261}
262
Michal Kaziorab977bd2013-11-25 14:06:26 +0100263static irqreturn_t ath10k_pci_early_irq_handler(int irq, void *arg)
264{
265 struct ath10k *ar = arg;
266 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
267
268 if (ar_pci->num_msi_intrs == 0) {
269 if (!ath10k_pci_irq_pending(ar))
270 return IRQ_NONE;
271
272 ath10k_pci_disable_and_clear_legacy_irq(ar);
273 }
274
275 tasklet_schedule(&ar_pci->early_irq_tasklet);
276
277 return IRQ_HANDLED;
278}
279
280static int ath10k_pci_request_early_irq(struct ath10k *ar)
281{
282 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
283 int ret;
284
285 /* Regardless whether MSI-X/MSI/legacy irqs have been set up the first
286 * interrupt from irq vector is triggered in all cases for FW
287 * indication/errors */
288 ret = request_irq(ar_pci->pdev->irq, ath10k_pci_early_irq_handler,
289 IRQF_SHARED, "ath10k_pci (early)", ar);
290 if (ret) {
291 ath10k_warn("failed to request early irq: %d\n", ret);
292 return ret;
293 }
294
295 return 0;
296}
297
298static void ath10k_pci_free_early_irq(struct ath10k *ar)
299{
300 free_irq(ath10k_pci_priv(ar)->pdev->irq, ar);
301}
302
Kalle Valo5e3dd152013-06-12 20:52:10 +0300303/*
304 * Diagnostic read/write access is provided for startup/config/debug usage.
305 * Caller must guarantee proper alignment, when applicable, and single user
306 * at any moment.
307 */
308static int ath10k_pci_diag_read_mem(struct ath10k *ar, u32 address, void *data,
309 int nbytes)
310{
311 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
312 int ret = 0;
313 u32 buf;
314 unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
315 unsigned int id;
316 unsigned int flags;
Michal Kazior2aa39112013-08-27 13:08:02 +0200317 struct ath10k_ce_pipe *ce_diag;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300318 /* Host buffer address in CE space */
319 u32 ce_data;
320 dma_addr_t ce_data_base = 0;
321 void *data_buf = NULL;
322 int i;
323
324 /*
325 * This code cannot handle reads to non-memory space. Redirect to the
326 * register read fn but preserve the multi word read capability of
327 * this fn
328 */
329 if (address < DRAM_BASE_ADDRESS) {
330 if (!IS_ALIGNED(address, 4) ||
331 !IS_ALIGNED((unsigned long)data, 4))
332 return -EIO;
333
334 while ((nbytes >= 4) && ((ret = ath10k_pci_diag_read_access(
335 ar, address, (u32 *)data)) == 0)) {
336 nbytes -= sizeof(u32);
337 address += sizeof(u32);
338 data += sizeof(u32);
339 }
340 return ret;
341 }
342
343 ce_diag = ar_pci->ce_diag;
344
345 /*
346 * Allocate a temporary bounce buffer to hold caller's data
347 * to be DMA'ed from Target. This guarantees
348 * 1) 4-byte alignment
349 * 2) Buffer in DMA-able space
350 */
351 orig_nbytes = nbytes;
352 data_buf = (unsigned char *)pci_alloc_consistent(ar_pci->pdev,
353 orig_nbytes,
354 &ce_data_base);
355
356 if (!data_buf) {
357 ret = -ENOMEM;
358 goto done;
359 }
360 memset(data_buf, 0, orig_nbytes);
361
362 remaining_bytes = orig_nbytes;
363 ce_data = ce_data_base;
364 while (remaining_bytes) {
365 nbytes = min_t(unsigned int, remaining_bytes,
366 DIAG_TRANSFER_LIMIT);
367
368 ret = ath10k_ce_recv_buf_enqueue(ce_diag, NULL, ce_data);
369 if (ret != 0)
370 goto done;
371
372 /* Request CE to send from Target(!) address to Host buffer */
373 /*
374 * The address supplied by the caller is in the
375 * Target CPU virtual address space.
376 *
377 * In order to use this address with the diagnostic CE,
378 * convert it from Target CPU virtual address space
379 * to CE address space
380 */
381 ath10k_pci_wake(ar);
382 address = TARG_CPU_SPACE_TO_CE_SPACE(ar, ar_pci->mem,
383 address);
384 ath10k_pci_sleep(ar);
385
386 ret = ath10k_ce_send(ce_diag, NULL, (u32)address, nbytes, 0,
387 0);
388 if (ret)
389 goto done;
390
391 i = 0;
392 while (ath10k_ce_completed_send_next(ce_diag, NULL, &buf,
393 &completed_nbytes,
394 &id) != 0) {
395 mdelay(1);
396 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
397 ret = -EBUSY;
398 goto done;
399 }
400 }
401
402 if (nbytes != completed_nbytes) {
403 ret = -EIO;
404 goto done;
405 }
406
407 if (buf != (u32) address) {
408 ret = -EIO;
409 goto done;
410 }
411
412 i = 0;
413 while (ath10k_ce_completed_recv_next(ce_diag, NULL, &buf,
414 &completed_nbytes,
415 &id, &flags) != 0) {
416 mdelay(1);
417
418 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
419 ret = -EBUSY;
420 goto done;
421 }
422 }
423
424 if (nbytes != completed_nbytes) {
425 ret = -EIO;
426 goto done;
427 }
428
429 if (buf != ce_data) {
430 ret = -EIO;
431 goto done;
432 }
433
434 remaining_bytes -= nbytes;
435 address += nbytes;
436 ce_data += nbytes;
437 }
438
439done:
440 if (ret == 0) {
441 /* Copy data from allocated DMA buf to caller's buf */
442 WARN_ON_ONCE(orig_nbytes & 3);
443 for (i = 0; i < orig_nbytes / sizeof(__le32); i++) {
444 ((u32 *)data)[i] =
445 __le32_to_cpu(((__le32 *)data_buf)[i]);
446 }
447 } else
448 ath10k_dbg(ATH10K_DBG_PCI, "%s failure (0x%x)\n",
449 __func__, address);
450
451 if (data_buf)
452 pci_free_consistent(ar_pci->pdev, orig_nbytes,
453 data_buf, ce_data_base);
454
455 return ret;
456}
457
458/* Read 4-byte aligned data from Target memory or register */
459static int ath10k_pci_diag_read_access(struct ath10k *ar, u32 address,
460 u32 *data)
461{
462 /* Assume range doesn't cross this boundary */
463 if (address >= DRAM_BASE_ADDRESS)
464 return ath10k_pci_diag_read_mem(ar, address, data, sizeof(u32));
465
466 ath10k_pci_wake(ar);
467 *data = ath10k_pci_read32(ar, address);
468 ath10k_pci_sleep(ar);
469 return 0;
470}
471
472static int ath10k_pci_diag_write_mem(struct ath10k *ar, u32 address,
473 const void *data, int nbytes)
474{
475 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
476 int ret = 0;
477 u32 buf;
478 unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
479 unsigned int id;
480 unsigned int flags;
Michal Kazior2aa39112013-08-27 13:08:02 +0200481 struct ath10k_ce_pipe *ce_diag;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300482 void *data_buf = NULL;
483 u32 ce_data; /* Host buffer address in CE space */
484 dma_addr_t ce_data_base = 0;
485 int i;
486
487 ce_diag = ar_pci->ce_diag;
488
489 /*
490 * Allocate a temporary bounce buffer to hold caller's data
491 * to be DMA'ed to Target. This guarantees
492 * 1) 4-byte alignment
493 * 2) Buffer in DMA-able space
494 */
495 orig_nbytes = nbytes;
496 data_buf = (unsigned char *)pci_alloc_consistent(ar_pci->pdev,
497 orig_nbytes,
498 &ce_data_base);
499 if (!data_buf) {
500 ret = -ENOMEM;
501 goto done;
502 }
503
504 /* Copy caller's data to allocated DMA buf */
505 WARN_ON_ONCE(orig_nbytes & 3);
506 for (i = 0; i < orig_nbytes / sizeof(__le32); i++)
507 ((__le32 *)data_buf)[i] = __cpu_to_le32(((u32 *)data)[i]);
508
509 /*
510 * The address supplied by the caller is in the
511 * Target CPU virtual address space.
512 *
513 * In order to use this address with the diagnostic CE,
514 * convert it from
515 * Target CPU virtual address space
516 * to
517 * CE address space
518 */
519 ath10k_pci_wake(ar);
520 address = TARG_CPU_SPACE_TO_CE_SPACE(ar, ar_pci->mem, address);
521 ath10k_pci_sleep(ar);
522
523 remaining_bytes = orig_nbytes;
524 ce_data = ce_data_base;
525 while (remaining_bytes) {
526 /* FIXME: check cast */
527 nbytes = min_t(int, remaining_bytes, DIAG_TRANSFER_LIMIT);
528
529 /* Set up to receive directly into Target(!) address */
530 ret = ath10k_ce_recv_buf_enqueue(ce_diag, NULL, address);
531 if (ret != 0)
532 goto done;
533
534 /*
535 * Request CE to send caller-supplied data that
536 * was copied to bounce buffer to Target(!) address.
537 */
538 ret = ath10k_ce_send(ce_diag, NULL, (u32) ce_data,
539 nbytes, 0, 0);
540 if (ret != 0)
541 goto done;
542
543 i = 0;
544 while (ath10k_ce_completed_send_next(ce_diag, NULL, &buf,
545 &completed_nbytes,
546 &id) != 0) {
547 mdelay(1);
548
549 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
550 ret = -EBUSY;
551 goto done;
552 }
553 }
554
555 if (nbytes != completed_nbytes) {
556 ret = -EIO;
557 goto done;
558 }
559
560 if (buf != ce_data) {
561 ret = -EIO;
562 goto done;
563 }
564
565 i = 0;
566 while (ath10k_ce_completed_recv_next(ce_diag, NULL, &buf,
567 &completed_nbytes,
568 &id, &flags) != 0) {
569 mdelay(1);
570
571 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
572 ret = -EBUSY;
573 goto done;
574 }
575 }
576
577 if (nbytes != completed_nbytes) {
578 ret = -EIO;
579 goto done;
580 }
581
582 if (buf != address) {
583 ret = -EIO;
584 goto done;
585 }
586
587 remaining_bytes -= nbytes;
588 address += nbytes;
589 ce_data += nbytes;
590 }
591
592done:
593 if (data_buf) {
594 pci_free_consistent(ar_pci->pdev, orig_nbytes, data_buf,
595 ce_data_base);
596 }
597
598 if (ret != 0)
599 ath10k_dbg(ATH10K_DBG_PCI, "%s failure (0x%x)\n", __func__,
600 address);
601
602 return ret;
603}
604
605/* Write 4B data to Target memory or register */
606static int ath10k_pci_diag_write_access(struct ath10k *ar, u32 address,
607 u32 data)
608{
609 /* Assume range doesn't cross this boundary */
610 if (address >= DRAM_BASE_ADDRESS)
611 return ath10k_pci_diag_write_mem(ar, address, &data,
612 sizeof(u32));
613
614 ath10k_pci_wake(ar);
615 ath10k_pci_write32(ar, address, data);
616 ath10k_pci_sleep(ar);
617 return 0;
618}
619
620static bool ath10k_pci_target_is_awake(struct ath10k *ar)
621{
622 void __iomem *mem = ath10k_pci_priv(ar)->mem;
623 u32 val;
624 val = ioread32(mem + PCIE_LOCAL_BASE_ADDRESS +
625 RTC_STATE_ADDRESS);
626 return (RTC_STATE_V_GET(val) == RTC_STATE_V_ON);
627}
628
Kalle Valo3aebe542013-09-01 10:02:07 +0300629int ath10k_do_pci_wake(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300630{
631 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
632 void __iomem *pci_addr = ar_pci->mem;
633 int tot_delay = 0;
634 int curr_delay = 5;
635
636 if (atomic_read(&ar_pci->keep_awake_count) == 0) {
637 /* Force AWAKE */
638 iowrite32(PCIE_SOC_WAKE_V_MASK,
639 pci_addr + PCIE_LOCAL_BASE_ADDRESS +
640 PCIE_SOC_WAKE_ADDRESS);
641 }
642 atomic_inc(&ar_pci->keep_awake_count);
643
644 if (ar_pci->verified_awake)
Kalle Valo3aebe542013-09-01 10:02:07 +0300645 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300646
647 for (;;) {
648 if (ath10k_pci_target_is_awake(ar)) {
649 ar_pci->verified_awake = true;
Kalle Valo3aebe542013-09-01 10:02:07 +0300650 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300651 }
652
653 if (tot_delay > PCIE_WAKE_TIMEOUT) {
Kalle Valo3aebe542013-09-01 10:02:07 +0300654 ath10k_warn("target took longer %d us to wake up (awake count %d)\n",
655 PCIE_WAKE_TIMEOUT,
Kalle Valo5e3dd152013-06-12 20:52:10 +0300656 atomic_read(&ar_pci->keep_awake_count));
Kalle Valo3aebe542013-09-01 10:02:07 +0300657 return -ETIMEDOUT;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300658 }
659
660 udelay(curr_delay);
661 tot_delay += curr_delay;
662
663 if (curr_delay < 50)
664 curr_delay += 5;
665 }
666}
667
668void ath10k_do_pci_sleep(struct ath10k *ar)
669{
670 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
671 void __iomem *pci_addr = ar_pci->mem;
672
673 if (atomic_dec_and_test(&ar_pci->keep_awake_count)) {
674 /* Allow sleep */
675 ar_pci->verified_awake = false;
676 iowrite32(PCIE_SOC_WAKE_RESET,
677 pci_addr + PCIE_LOCAL_BASE_ADDRESS +
678 PCIE_SOC_WAKE_ADDRESS);
679 }
680}
681
Kalle Valo5e3dd152013-06-12 20:52:10 +0300682/* Called by lower (CE) layer when a send to Target completes. */
Michal Kazior5440ce22013-09-03 15:09:58 +0200683static void ath10k_pci_ce_send_done(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300684{
685 struct ath10k *ar = ce_state->ar;
686 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2f5280d2014-02-27 18:50:05 +0200687 struct ath10k_hif_cb *cb = &ar_pci->msg_callbacks_current;
Michal Kazior5440ce22013-09-03 15:09:58 +0200688 void *transfer_context;
689 u32 ce_data;
690 unsigned int nbytes;
691 unsigned int transfer_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300692
Michal Kazior5440ce22013-09-03 15:09:58 +0200693 while (ath10k_ce_completed_send_next(ce_state, &transfer_context,
694 &ce_data, &nbytes,
695 &transfer_id) == 0) {
Michal Kaziora16942e2014-02-27 18:50:04 +0200696 /* no need to call tx completion for NULL pointers */
Michal Kazior726346f2014-02-27 18:50:04 +0200697 if (transfer_context == NULL)
698 continue;
699
Michal Kazior2f5280d2014-02-27 18:50:05 +0200700 cb->tx_completion(ar, transfer_context, transfer_id);
Michal Kazior5440ce22013-09-03 15:09:58 +0200701 }
Kalle Valo5e3dd152013-06-12 20:52:10 +0300702}
703
704/* Called by lower (CE) layer when data is received from the Target. */
Michal Kazior5440ce22013-09-03 15:09:58 +0200705static void ath10k_pci_ce_recv_data(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300706{
707 struct ath10k *ar = ce_state->ar;
708 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +0200709 struct ath10k_pci_pipe *pipe_info = &ar_pci->pipe_info[ce_state->id];
Michal Kazior2f5280d2014-02-27 18:50:05 +0200710 struct ath10k_hif_cb *cb = &ar_pci->msg_callbacks_current;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300711 struct sk_buff *skb;
Michal Kazior5440ce22013-09-03 15:09:58 +0200712 void *transfer_context;
713 u32 ce_data;
Michal Kazior2f5280d2014-02-27 18:50:05 +0200714 unsigned int nbytes, max_nbytes;
Michal Kazior5440ce22013-09-03 15:09:58 +0200715 unsigned int transfer_id;
716 unsigned int flags;
Michal Kazior2f5280d2014-02-27 18:50:05 +0200717 int err;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300718
Michal Kazior5440ce22013-09-03 15:09:58 +0200719 while (ath10k_ce_completed_recv_next(ce_state, &transfer_context,
720 &ce_data, &nbytes, &transfer_id,
721 &flags) == 0) {
Michal Kazior2f5280d2014-02-27 18:50:05 +0200722 err = ath10k_pci_post_rx_pipe(pipe_info, 1);
723 if (unlikely(err)) {
724 /* FIXME: retry */
725 ath10k_warn("failed to replenish CE rx ring %d: %d\n",
726 pipe_info->pipe_num, err);
727 }
Kalle Valo5e3dd152013-06-12 20:52:10 +0300728
729 skb = transfer_context;
Michal Kazior2f5280d2014-02-27 18:50:05 +0200730 max_nbytes = skb->len + skb_tailroom(skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300731 dma_unmap_single(ar->dev, ATH10K_SKB_CB(skb)->paddr,
Michal Kazior2f5280d2014-02-27 18:50:05 +0200732 max_nbytes, DMA_FROM_DEVICE);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300733
Michal Kazior2f5280d2014-02-27 18:50:05 +0200734 if (unlikely(max_nbytes < nbytes)) {
735 ath10k_warn("rxed more than expected (nbytes %d, max %d)",
736 nbytes, max_nbytes);
737 dev_kfree_skb_any(skb);
738 continue;
739 }
740
741 skb_put(skb, nbytes);
742 cb->rx_completion(ar, skb, pipe_info->pipe_num);
743 }
Kalle Valo5e3dd152013-06-12 20:52:10 +0300744}
745
Michal Kazior726346f2014-02-27 18:50:04 +0200746static int ath10k_pci_hif_tx_sg(struct ath10k *ar, u8 pipe_id,
747 struct ath10k_hif_sg_item *items, int n_items)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300748{
Kalle Valo5e3dd152013-06-12 20:52:10 +0300749 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior726346f2014-02-27 18:50:04 +0200750 struct ath10k_pci_pipe *pci_pipe = &ar_pci->pipe_info[pipe_id];
751 struct ath10k_ce_pipe *ce_pipe = pci_pipe->ce_hdl;
752 struct ath10k_ce_ring *src_ring = ce_pipe->src_ring;
753 unsigned int nentries_mask = src_ring->nentries_mask;
754 unsigned int sw_index = src_ring->sw_index;
755 unsigned int write_index = src_ring->write_index;
756 int err, i;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300757
Michal Kazior726346f2014-02-27 18:50:04 +0200758 spin_lock_bh(&ar_pci->ce_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300759
Michal Kazior726346f2014-02-27 18:50:04 +0200760 if (unlikely(CE_RING_DELTA(nentries_mask,
761 write_index, sw_index - 1) < n_items)) {
762 err = -ENOBUFS;
763 goto unlock;
764 }
765
766 for (i = 0; i < n_items - 1; i++) {
767 ath10k_dbg(ATH10K_DBG_PCI,
768 "pci tx item %d paddr 0x%08x len %d n_items %d\n",
769 i, items[i].paddr, items[i].len, n_items);
770 ath10k_dbg_dump(ATH10K_DBG_PCI_DUMP, NULL, "item data: ",
771 items[i].vaddr, items[i].len);
772
773 err = ath10k_ce_send_nolock(ce_pipe,
774 items[i].transfer_context,
775 items[i].paddr,
776 items[i].len,
777 items[i].transfer_id,
778 CE_SEND_FLAG_GATHER);
779 if (err)
780 goto unlock;
781 }
782
783 /* `i` is equal to `n_items -1` after for() */
Kalle Valo5e3dd152013-06-12 20:52:10 +0300784
785 ath10k_dbg(ATH10K_DBG_PCI,
Michal Kazior726346f2014-02-27 18:50:04 +0200786 "pci tx item %d paddr 0x%08x len %d n_items %d\n",
787 i, items[i].paddr, items[i].len, n_items);
788 ath10k_dbg_dump(ATH10K_DBG_PCI_DUMP, NULL, "item data: ",
789 items[i].vaddr, items[i].len);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300790
Michal Kazior726346f2014-02-27 18:50:04 +0200791 err = ath10k_ce_send_nolock(ce_pipe,
792 items[i].transfer_context,
793 items[i].paddr,
794 items[i].len,
795 items[i].transfer_id,
796 0);
797 if (err)
798 goto unlock;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300799
Michal Kazior726346f2014-02-27 18:50:04 +0200800 err = 0;
801unlock:
802 spin_unlock_bh(&ar_pci->ce_lock);
803 return err;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300804}
805
806static u16 ath10k_pci_hif_get_free_queue_number(struct ath10k *ar, u8 pipe)
807{
808 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior3efcb3b2013-10-02 11:03:41 +0200809 return ath10k_ce_num_free_src_entries(ar_pci->pipe_info[pipe].ce_hdl);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300810}
811
812static void ath10k_pci_hif_dump_area(struct ath10k *ar)
813{
814 u32 reg_dump_area = 0;
815 u32 reg_dump_values[REG_DUMP_COUNT_QCA988X] = {};
816 u32 host_addr;
817 int ret;
818 u32 i;
819
820 ath10k_err("firmware crashed!\n");
821 ath10k_err("hardware name %s version 0x%x\n",
822 ar->hw_params.name, ar->target_version);
Chun-Yeow Yeoh5ba88b32014-01-21 17:21:21 +0800823 ath10k_err("firmware version: %s\n", ar->hw->wiphy->fw_version);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300824
825 host_addr = host_interest_item_address(HI_ITEM(hi_failure_state));
Michal Kazior1d2b48d2013-11-08 08:01:34 +0100826 ret = ath10k_pci_diag_read_mem(ar, host_addr,
827 &reg_dump_area, sizeof(u32));
828 if (ret) {
829 ath10k_err("failed to read FW dump area address: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300830 return;
831 }
832
833 ath10k_err("target register Dump Location: 0x%08X\n", reg_dump_area);
834
835 ret = ath10k_pci_diag_read_mem(ar, reg_dump_area,
836 &reg_dump_values[0],
837 REG_DUMP_COUNT_QCA988X * sizeof(u32));
838 if (ret != 0) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +0100839 ath10k_err("failed to read FW dump area: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300840 return;
841 }
842
843 BUILD_BUG_ON(REG_DUMP_COUNT_QCA988X % 4);
844
845 ath10k_err("target Register Dump\n");
846 for (i = 0; i < REG_DUMP_COUNT_QCA988X; i += 4)
847 ath10k_err("[%02d]: 0x%08X 0x%08X 0x%08X 0x%08X\n",
848 i,
849 reg_dump_values[i],
850 reg_dump_values[i + 1],
851 reg_dump_values[i + 2],
852 reg_dump_values[i + 3]);
Michal Kazioraffd3212013-07-16 09:54:35 +0200853
Michal Kazior5e90de82013-10-16 16:46:05 +0300854 queue_work(ar->workqueue, &ar->restart_work);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300855}
856
857static void ath10k_pci_hif_send_complete_check(struct ath10k *ar, u8 pipe,
858 int force)
859{
860 if (!force) {
861 int resources;
862 /*
863 * Decide whether to actually poll for completions, or just
864 * wait for a later chance.
865 * If there seem to be plenty of resources left, then just wait
866 * since checking involves reading a CE register, which is a
867 * relatively expensive operation.
868 */
869 resources = ath10k_pci_hif_get_free_queue_number(ar, pipe);
870
871 /*
872 * If at least 50% of the total resources are still available,
873 * don't bother checking again yet.
874 */
875 if (resources > (host_ce_config_wlan[pipe].src_nentries >> 1))
876 return;
877 }
878 ath10k_ce_per_engine_service(ar, pipe);
879}
880
Michal Kaziore799bbf2013-07-05 16:15:12 +0300881static void ath10k_pci_hif_set_callbacks(struct ath10k *ar,
882 struct ath10k_hif_cb *callbacks)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300883{
884 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
885
886 ath10k_dbg(ATH10K_DBG_PCI, "%s\n", __func__);
887
888 memcpy(&ar_pci->msg_callbacks_current, callbacks,
889 sizeof(ar_pci->msg_callbacks_current));
890}
891
Michal Kaziorc80de122013-11-25 14:06:23 +0100892static int ath10k_pci_setup_ce_irq(struct ath10k *ar)
893{
894 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
895 const struct ce_attr *attr;
896 struct ath10k_pci_pipe *pipe_info;
897 int pipe_num, disable_interrupts;
898
899 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
900 pipe_info = &ar_pci->pipe_info[pipe_num];
901
902 /* Handle Diagnostic CE specially */
903 if (pipe_info->ce_hdl == ar_pci->ce_diag)
904 continue;
905
906 attr = &host_ce_config_wlan[pipe_num];
907
908 if (attr->src_nentries) {
909 disable_interrupts = attr->flags & CE_ATTR_DIS_INTR;
910 ath10k_ce_send_cb_register(pipe_info->ce_hdl,
911 ath10k_pci_ce_send_done,
912 disable_interrupts);
913 }
914
915 if (attr->dest_nentries)
916 ath10k_ce_recv_cb_register(pipe_info->ce_hdl,
917 ath10k_pci_ce_recv_data);
918 }
919
920 return 0;
921}
922
Michal Kazior96a9d0d2013-11-08 08:01:25 +0100923static void ath10k_pci_kill_tasklet(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300924{
925 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300926 int i;
927
Kalle Valo5e3dd152013-06-12 20:52:10 +0300928 tasklet_kill(&ar_pci->intr_tq);
Michal Kazior103d4f52013-11-08 08:01:24 +0100929 tasklet_kill(&ar_pci->msi_fw_err);
Michal Kaziorab977bd2013-11-25 14:06:26 +0100930 tasklet_kill(&ar_pci->early_irq_tasklet);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300931
932 for (i = 0; i < CE_COUNT; i++)
933 tasklet_kill(&ar_pci->pipe_info[i].intr);
Michal Kazior96a9d0d2013-11-08 08:01:25 +0100934}
935
Kalle Valo5e3dd152013-06-12 20:52:10 +0300936/* TODO - temporary mapping while we have too few CE's */
937static int ath10k_pci_hif_map_service_to_pipe(struct ath10k *ar,
938 u16 service_id, u8 *ul_pipe,
939 u8 *dl_pipe, int *ul_is_polled,
940 int *dl_is_polled)
941{
942 int ret = 0;
943
944 /* polling for received messages not supported */
945 *dl_is_polled = 0;
946
947 switch (service_id) {
948 case ATH10K_HTC_SVC_ID_HTT_DATA_MSG:
949 /*
950 * Host->target HTT gets its own pipe, so it can be polled
951 * while other pipes are interrupt driven.
952 */
953 *ul_pipe = 4;
954 /*
955 * Use the same target->host pipe for HTC ctrl, HTC raw
956 * streams, and HTT.
957 */
958 *dl_pipe = 1;
959 break;
960
961 case ATH10K_HTC_SVC_ID_RSVD_CTRL:
962 case ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS:
963 /*
964 * Note: HTC_RAW_STREAMS_SVC is currently unused, and
965 * HTC_CTRL_RSVD_SVC could share the same pipe as the
966 * WMI services. So, if another CE is needed, change
967 * this to *ul_pipe = 3, which frees up CE 0.
968 */
969 /* *ul_pipe = 3; */
970 *ul_pipe = 0;
971 *dl_pipe = 1;
972 break;
973
974 case ATH10K_HTC_SVC_ID_WMI_DATA_BK:
975 case ATH10K_HTC_SVC_ID_WMI_DATA_BE:
976 case ATH10K_HTC_SVC_ID_WMI_DATA_VI:
977 case ATH10K_HTC_SVC_ID_WMI_DATA_VO:
978
979 case ATH10K_HTC_SVC_ID_WMI_CONTROL:
980 *ul_pipe = 3;
981 *dl_pipe = 2;
982 break;
983
984 /* pipe 5 unused */
985 /* pipe 6 reserved */
986 /* pipe 7 reserved */
987
988 default:
989 ret = -1;
990 break;
991 }
992 *ul_is_polled =
993 (host_ce_config_wlan[*ul_pipe].flags & CE_ATTR_DIS_INTR) != 0;
994
995 return ret;
996}
997
998static void ath10k_pci_hif_get_default_pipe(struct ath10k *ar,
999 u8 *ul_pipe, u8 *dl_pipe)
1000{
1001 int ul_is_polled, dl_is_polled;
1002
1003 (void)ath10k_pci_hif_map_service_to_pipe(ar,
1004 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1005 ul_pipe,
1006 dl_pipe,
1007 &ul_is_polled,
1008 &dl_is_polled);
1009}
1010
Michal Kazior87263e52013-08-27 13:08:01 +02001011static int ath10k_pci_post_rx_pipe(struct ath10k_pci_pipe *pipe_info,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001012 int num)
1013{
1014 struct ath10k *ar = pipe_info->hif_ce_state;
1015 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2aa39112013-08-27 13:08:02 +02001016 struct ath10k_ce_pipe *ce_state = pipe_info->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001017 struct sk_buff *skb;
1018 dma_addr_t ce_data;
1019 int i, ret = 0;
1020
1021 if (pipe_info->buf_sz == 0)
1022 return 0;
1023
1024 for (i = 0; i < num; i++) {
1025 skb = dev_alloc_skb(pipe_info->buf_sz);
1026 if (!skb) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001027 ath10k_warn("failed to allocate skbuff for pipe %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001028 num);
1029 ret = -ENOMEM;
1030 goto err;
1031 }
1032
1033 WARN_ONCE((unsigned long)skb->data & 3, "unaligned skb");
1034
1035 ce_data = dma_map_single(ar->dev, skb->data,
1036 skb->len + skb_tailroom(skb),
1037 DMA_FROM_DEVICE);
1038
1039 if (unlikely(dma_mapping_error(ar->dev, ce_data))) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001040 ath10k_warn("failed to DMA map sk_buff\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001041 dev_kfree_skb_any(skb);
1042 ret = -EIO;
1043 goto err;
1044 }
1045
1046 ATH10K_SKB_CB(skb)->paddr = ce_data;
1047
1048 pci_dma_sync_single_for_device(ar_pci->pdev, ce_data,
1049 pipe_info->buf_sz,
1050 PCI_DMA_FROMDEVICE);
1051
1052 ret = ath10k_ce_recv_buf_enqueue(ce_state, (void *)skb,
1053 ce_data);
1054 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001055 ath10k_warn("failed to enqueue to pipe %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001056 num, ret);
1057 goto err;
1058 }
1059 }
1060
1061 return ret;
1062
1063err:
1064 ath10k_pci_rx_pipe_cleanup(pipe_info);
1065 return ret;
1066}
1067
1068static int ath10k_pci_post_rx(struct ath10k *ar)
1069{
1070 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +02001071 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001072 const struct ce_attr *attr;
1073 int pipe_num, ret = 0;
1074
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001075 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001076 pipe_info = &ar_pci->pipe_info[pipe_num];
1077 attr = &host_ce_config_wlan[pipe_num];
1078
1079 if (attr->dest_nentries == 0)
1080 continue;
1081
1082 ret = ath10k_pci_post_rx_pipe(pipe_info,
1083 attr->dest_nentries - 1);
1084 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001085 ath10k_warn("failed to post RX buffer for pipe %d: %d\n",
1086 pipe_num, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001087
1088 for (; pipe_num >= 0; pipe_num--) {
1089 pipe_info = &ar_pci->pipe_info[pipe_num];
1090 ath10k_pci_rx_pipe_cleanup(pipe_info);
1091 }
1092 return ret;
1093 }
1094 }
1095
1096 return 0;
1097}
1098
1099static int ath10k_pci_hif_start(struct ath10k *ar)
1100{
1101 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorab977bd2013-11-25 14:06:26 +01001102 int ret, ret_early;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001103
Michal Kaziorab977bd2013-11-25 14:06:26 +01001104 ath10k_pci_free_early_irq(ar);
1105 ath10k_pci_kill_tasklet(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001106
Michal Kazior5d1aa942013-11-25 14:06:24 +01001107 ret = ath10k_pci_request_irq(ar);
1108 if (ret) {
1109 ath10k_warn("failed to post RX buffers for all pipes: %d\n",
1110 ret);
Michal Kazior2f5280d2014-02-27 18:50:05 +02001111 goto err_early_irq;
Michal Kazior5d1aa942013-11-25 14:06:24 +01001112 }
1113
Michal Kaziorc80de122013-11-25 14:06:23 +01001114 ret = ath10k_pci_setup_ce_irq(ar);
1115 if (ret) {
1116 ath10k_warn("failed to setup CE interrupts: %d\n", ret);
Michal Kazior5d1aa942013-11-25 14:06:24 +01001117 goto err_stop;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001118 }
1119
1120 /* Post buffers once to start things off. */
1121 ret = ath10k_pci_post_rx(ar);
1122 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001123 ath10k_warn("failed to post RX buffers for all pipes: %d\n",
1124 ret);
Michal Kazior5d1aa942013-11-25 14:06:24 +01001125 goto err_stop;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001126 }
1127
1128 ar_pci->started = 1;
1129 return 0;
Michal Kaziorc80de122013-11-25 14:06:23 +01001130
Michal Kazior5d1aa942013-11-25 14:06:24 +01001131err_stop:
1132 ath10k_ce_disable_interrupts(ar);
1133 ath10k_pci_free_irq(ar);
1134 ath10k_pci_kill_tasklet(ar);
Michal Kaziorab977bd2013-11-25 14:06:26 +01001135err_early_irq:
1136 /* Though there should be no interrupts (device was reset)
1137 * power_down() expects the early IRQ to be installed as per the
1138 * driver lifecycle. */
1139 ret_early = ath10k_pci_request_early_irq(ar);
1140 if (ret_early)
1141 ath10k_warn("failed to re-enable early irq: %d\n", ret_early);
1142
Michal Kaziorc80de122013-11-25 14:06:23 +01001143 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001144}
1145
Michal Kazior87263e52013-08-27 13:08:01 +02001146static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe *pipe_info)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001147{
1148 struct ath10k *ar;
1149 struct ath10k_pci *ar_pci;
Michal Kazior2aa39112013-08-27 13:08:02 +02001150 struct ath10k_ce_pipe *ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001151 u32 buf_sz;
1152 struct sk_buff *netbuf;
1153 u32 ce_data;
1154
1155 buf_sz = pipe_info->buf_sz;
1156
1157 /* Unused Copy Engine */
1158 if (buf_sz == 0)
1159 return;
1160
1161 ar = pipe_info->hif_ce_state;
1162 ar_pci = ath10k_pci_priv(ar);
1163
1164 if (!ar_pci->started)
1165 return;
1166
1167 ce_hdl = pipe_info->ce_hdl;
1168
1169 while (ath10k_ce_revoke_recv_next(ce_hdl, (void **)&netbuf,
1170 &ce_data) == 0) {
1171 dma_unmap_single(ar->dev, ATH10K_SKB_CB(netbuf)->paddr,
1172 netbuf->len + skb_tailroom(netbuf),
1173 DMA_FROM_DEVICE);
1174 dev_kfree_skb_any(netbuf);
1175 }
1176}
1177
Michal Kazior87263e52013-08-27 13:08:01 +02001178static void ath10k_pci_tx_pipe_cleanup(struct ath10k_pci_pipe *pipe_info)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001179{
1180 struct ath10k *ar;
1181 struct ath10k_pci *ar_pci;
Michal Kazior2aa39112013-08-27 13:08:02 +02001182 struct ath10k_ce_pipe *ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001183 struct sk_buff *netbuf;
1184 u32 ce_data;
1185 unsigned int nbytes;
1186 unsigned int id;
1187 u32 buf_sz;
1188
1189 buf_sz = pipe_info->buf_sz;
1190
1191 /* Unused Copy Engine */
1192 if (buf_sz == 0)
1193 return;
1194
1195 ar = pipe_info->hif_ce_state;
1196 ar_pci = ath10k_pci_priv(ar);
1197
1198 if (!ar_pci->started)
1199 return;
1200
1201 ce_hdl = pipe_info->ce_hdl;
1202
1203 while (ath10k_ce_cancel_send_next(ce_hdl, (void **)&netbuf,
1204 &ce_data, &nbytes, &id) == 0) {
Michal Kaziora16942e2014-02-27 18:50:04 +02001205 /* no need to call tx completion for NULL pointers */
1206 if (!netbuf)
Michal Kazior2415fc12013-11-08 08:01:32 +01001207 continue;
Michal Kazior2415fc12013-11-08 08:01:32 +01001208
Kalle Valoe9bb0aa2013-09-08 18:36:11 +03001209 ar_pci->msg_callbacks_current.tx_completion(ar,
1210 netbuf,
1211 id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001212 }
1213}
1214
1215/*
1216 * Cleanup residual buffers for device shutdown:
1217 * buffers that were enqueued for receive
1218 * buffers that were to be sent
1219 * Note: Buffers that had completed but which were
1220 * not yet processed are on a completion queue. They
1221 * are handled when the completion thread shuts down.
1222 */
1223static void ath10k_pci_buffer_cleanup(struct ath10k *ar)
1224{
1225 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1226 int pipe_num;
1227
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001228 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Michal Kazior87263e52013-08-27 13:08:01 +02001229 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001230
1231 pipe_info = &ar_pci->pipe_info[pipe_num];
1232 ath10k_pci_rx_pipe_cleanup(pipe_info);
1233 ath10k_pci_tx_pipe_cleanup(pipe_info);
1234 }
1235}
1236
1237static void ath10k_pci_ce_deinit(struct ath10k *ar)
1238{
1239 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +02001240 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001241 int pipe_num;
1242
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001243 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001244 pipe_info = &ar_pci->pipe_info[pipe_num];
1245 if (pipe_info->ce_hdl) {
1246 ath10k_ce_deinit(pipe_info->ce_hdl);
1247 pipe_info->ce_hdl = NULL;
1248 pipe_info->buf_sz = 0;
1249 }
1250 }
1251}
1252
1253static void ath10k_pci_hif_stop(struct ath10k *ar)
1254{
Michal Kazior32270b62013-08-02 09:15:47 +02001255 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior5d1aa942013-11-25 14:06:24 +01001256 int ret;
Michal Kazior32270b62013-08-02 09:15:47 +02001257
Kalle Valo5e3dd152013-06-12 20:52:10 +03001258 ath10k_dbg(ATH10K_DBG_PCI, "%s\n", __func__);
1259
Michal Kazior5d1aa942013-11-25 14:06:24 +01001260 ret = ath10k_ce_disable_interrupts(ar);
1261 if (ret)
1262 ath10k_warn("failed to disable CE interrupts: %d\n", ret);
Michal Kazior32270b62013-08-02 09:15:47 +02001263
Michal Kazior5d1aa942013-11-25 14:06:24 +01001264 ath10k_pci_free_irq(ar);
1265 ath10k_pci_kill_tasklet(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001266
Michal Kaziorab977bd2013-11-25 14:06:26 +01001267 ret = ath10k_pci_request_early_irq(ar);
1268 if (ret)
1269 ath10k_warn("failed to re-enable early irq: %d\n", ret);
1270
Kalle Valo5e3dd152013-06-12 20:52:10 +03001271 /* At this point, asynchronous threads are stopped, the target should
1272 * not DMA nor interrupt. We process the leftovers and then free
1273 * everything else up. */
1274
Kalle Valo5e3dd152013-06-12 20:52:10 +03001275 ath10k_pci_buffer_cleanup(ar);
Michal Kazior32270b62013-08-02 09:15:47 +02001276
Michal Kazior6a42a472013-11-08 08:01:35 +01001277 /* Make the sure the device won't access any structures on the host by
1278 * resetting it. The device was fed with PCI CE ringbuffer
1279 * configuration during init. If ringbuffers are freed and the device
1280 * were to access them this could lead to memory corruption on the
1281 * host. */
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001282 ath10k_pci_warm_reset(ar);
Michal Kazior6a42a472013-11-08 08:01:35 +01001283
Michal Kazior32270b62013-08-02 09:15:47 +02001284 ar_pci->started = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001285}
1286
1287static int ath10k_pci_hif_exchange_bmi_msg(struct ath10k *ar,
1288 void *req, u32 req_len,
1289 void *resp, u32 *resp_len)
1290{
1291 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2aa39112013-08-27 13:08:02 +02001292 struct ath10k_pci_pipe *pci_tx = &ar_pci->pipe_info[BMI_CE_NUM_TO_TARG];
1293 struct ath10k_pci_pipe *pci_rx = &ar_pci->pipe_info[BMI_CE_NUM_TO_HOST];
1294 struct ath10k_ce_pipe *ce_tx = pci_tx->ce_hdl;
1295 struct ath10k_ce_pipe *ce_rx = pci_rx->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001296 dma_addr_t req_paddr = 0;
1297 dma_addr_t resp_paddr = 0;
1298 struct bmi_xfer xfer = {};
1299 void *treq, *tresp = NULL;
1300 int ret = 0;
1301
Michal Kazior85622cd2013-11-25 14:06:22 +01001302 might_sleep();
1303
Kalle Valo5e3dd152013-06-12 20:52:10 +03001304 if (resp && !resp_len)
1305 return -EINVAL;
1306
1307 if (resp && resp_len && *resp_len == 0)
1308 return -EINVAL;
1309
1310 treq = kmemdup(req, req_len, GFP_KERNEL);
1311 if (!treq)
1312 return -ENOMEM;
1313
1314 req_paddr = dma_map_single(ar->dev, treq, req_len, DMA_TO_DEVICE);
1315 ret = dma_mapping_error(ar->dev, req_paddr);
1316 if (ret)
1317 goto err_dma;
1318
1319 if (resp && resp_len) {
1320 tresp = kzalloc(*resp_len, GFP_KERNEL);
1321 if (!tresp) {
1322 ret = -ENOMEM;
1323 goto err_req;
1324 }
1325
1326 resp_paddr = dma_map_single(ar->dev, tresp, *resp_len,
1327 DMA_FROM_DEVICE);
1328 ret = dma_mapping_error(ar->dev, resp_paddr);
1329 if (ret)
1330 goto err_req;
1331
1332 xfer.wait_for_resp = true;
1333 xfer.resp_len = 0;
1334
1335 ath10k_ce_recv_buf_enqueue(ce_rx, &xfer, resp_paddr);
1336 }
1337
1338 init_completion(&xfer.done);
1339
1340 ret = ath10k_ce_send(ce_tx, &xfer, req_paddr, req_len, -1, 0);
1341 if (ret)
1342 goto err_resp;
1343
Michal Kazior85622cd2013-11-25 14:06:22 +01001344 ret = ath10k_pci_bmi_wait(ce_tx, ce_rx, &xfer);
1345 if (ret) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001346 u32 unused_buffer;
1347 unsigned int unused_nbytes;
1348 unsigned int unused_id;
1349
Kalle Valo5e3dd152013-06-12 20:52:10 +03001350 ath10k_ce_cancel_send_next(ce_tx, NULL, &unused_buffer,
1351 &unused_nbytes, &unused_id);
1352 } else {
1353 /* non-zero means we did not time out */
1354 ret = 0;
1355 }
1356
1357err_resp:
1358 if (resp) {
1359 u32 unused_buffer;
1360
1361 ath10k_ce_revoke_recv_next(ce_rx, NULL, &unused_buffer);
1362 dma_unmap_single(ar->dev, resp_paddr,
1363 *resp_len, DMA_FROM_DEVICE);
1364 }
1365err_req:
1366 dma_unmap_single(ar->dev, req_paddr, req_len, DMA_TO_DEVICE);
1367
1368 if (ret == 0 && resp_len) {
1369 *resp_len = min(*resp_len, xfer.resp_len);
1370 memcpy(resp, tresp, xfer.resp_len);
1371 }
1372err_dma:
1373 kfree(treq);
1374 kfree(tresp);
1375
1376 return ret;
1377}
1378
Michal Kazior5440ce22013-09-03 15:09:58 +02001379static void ath10k_pci_bmi_send_done(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001380{
Michal Kazior5440ce22013-09-03 15:09:58 +02001381 struct bmi_xfer *xfer;
1382 u32 ce_data;
1383 unsigned int nbytes;
1384 unsigned int transfer_id;
1385
1386 if (ath10k_ce_completed_send_next(ce_state, (void **)&xfer, &ce_data,
1387 &nbytes, &transfer_id))
1388 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001389
1390 if (xfer->wait_for_resp)
1391 return;
1392
1393 complete(&xfer->done);
1394}
1395
Michal Kazior5440ce22013-09-03 15:09:58 +02001396static void ath10k_pci_bmi_recv_data(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001397{
Michal Kazior5440ce22013-09-03 15:09:58 +02001398 struct bmi_xfer *xfer;
1399 u32 ce_data;
1400 unsigned int nbytes;
1401 unsigned int transfer_id;
1402 unsigned int flags;
1403
1404 if (ath10k_ce_completed_recv_next(ce_state, (void **)&xfer, &ce_data,
1405 &nbytes, &transfer_id, &flags))
1406 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001407
1408 if (!xfer->wait_for_resp) {
1409 ath10k_warn("unexpected: BMI data received; ignoring\n");
1410 return;
1411 }
1412
1413 xfer->resp_len = nbytes;
1414 complete(&xfer->done);
1415}
1416
Michal Kazior85622cd2013-11-25 14:06:22 +01001417static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
1418 struct ath10k_ce_pipe *rx_pipe,
1419 struct bmi_xfer *xfer)
1420{
1421 unsigned long timeout = jiffies + BMI_COMMUNICATION_TIMEOUT_HZ;
1422
1423 while (time_before_eq(jiffies, timeout)) {
1424 ath10k_pci_bmi_send_done(tx_pipe);
1425 ath10k_pci_bmi_recv_data(rx_pipe);
1426
1427 if (completion_done(&xfer->done))
1428 return 0;
1429
1430 schedule();
1431 }
1432
1433 return -ETIMEDOUT;
1434}
1435
Kalle Valo5e3dd152013-06-12 20:52:10 +03001436/*
1437 * Map from service/endpoint to Copy Engine.
1438 * This table is derived from the CE_PCI TABLE, above.
1439 * It is passed to the Target at startup for use by firmware.
1440 */
1441static const struct service_to_pipe target_service_to_ce_map_wlan[] = {
1442 {
1443 ATH10K_HTC_SVC_ID_WMI_DATA_VO,
1444 PIPEDIR_OUT, /* out = UL = host -> target */
1445 3,
1446 },
1447 {
1448 ATH10K_HTC_SVC_ID_WMI_DATA_VO,
1449 PIPEDIR_IN, /* in = DL = target -> host */
1450 2,
1451 },
1452 {
1453 ATH10K_HTC_SVC_ID_WMI_DATA_BK,
1454 PIPEDIR_OUT, /* out = UL = host -> target */
1455 3,
1456 },
1457 {
1458 ATH10K_HTC_SVC_ID_WMI_DATA_BK,
1459 PIPEDIR_IN, /* in = DL = target -> host */
1460 2,
1461 },
1462 {
1463 ATH10K_HTC_SVC_ID_WMI_DATA_BE,
1464 PIPEDIR_OUT, /* out = UL = host -> target */
1465 3,
1466 },
1467 {
1468 ATH10K_HTC_SVC_ID_WMI_DATA_BE,
1469 PIPEDIR_IN, /* in = DL = target -> host */
1470 2,
1471 },
1472 {
1473 ATH10K_HTC_SVC_ID_WMI_DATA_VI,
1474 PIPEDIR_OUT, /* out = UL = host -> target */
1475 3,
1476 },
1477 {
1478 ATH10K_HTC_SVC_ID_WMI_DATA_VI,
1479 PIPEDIR_IN, /* in = DL = target -> host */
1480 2,
1481 },
1482 {
1483 ATH10K_HTC_SVC_ID_WMI_CONTROL,
1484 PIPEDIR_OUT, /* out = UL = host -> target */
1485 3,
1486 },
1487 {
1488 ATH10K_HTC_SVC_ID_WMI_CONTROL,
1489 PIPEDIR_IN, /* in = DL = target -> host */
1490 2,
1491 },
1492 {
1493 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1494 PIPEDIR_OUT, /* out = UL = host -> target */
1495 0, /* could be moved to 3 (share with WMI) */
1496 },
1497 {
1498 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1499 PIPEDIR_IN, /* in = DL = target -> host */
1500 1,
1501 },
1502 {
1503 ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS, /* not currently used */
1504 PIPEDIR_OUT, /* out = UL = host -> target */
1505 0,
1506 },
1507 {
1508 ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS, /* not currently used */
1509 PIPEDIR_IN, /* in = DL = target -> host */
1510 1,
1511 },
1512 {
1513 ATH10K_HTC_SVC_ID_HTT_DATA_MSG,
1514 PIPEDIR_OUT, /* out = UL = host -> target */
1515 4,
1516 },
1517 {
1518 ATH10K_HTC_SVC_ID_HTT_DATA_MSG,
1519 PIPEDIR_IN, /* in = DL = target -> host */
1520 1,
1521 },
1522
1523 /* (Additions here) */
1524
1525 { /* Must be last */
1526 0,
1527 0,
1528 0,
1529 },
1530};
1531
1532/*
1533 * Send an interrupt to the device to wake up the Target CPU
1534 * so it has an opportunity to notice any changed state.
1535 */
1536static int ath10k_pci_wake_target_cpu(struct ath10k *ar)
1537{
1538 int ret;
1539 u32 core_ctrl;
1540
1541 ret = ath10k_pci_diag_read_access(ar, SOC_CORE_BASE_ADDRESS |
1542 CORE_CTRL_ADDRESS,
1543 &core_ctrl);
1544 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001545 ath10k_warn("failed to read core_ctrl: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001546 return ret;
1547 }
1548
1549 /* A_INUM_FIRMWARE interrupt to Target CPU */
1550 core_ctrl |= CORE_CTRL_CPU_INTR_MASK;
1551
1552 ret = ath10k_pci_diag_write_access(ar, SOC_CORE_BASE_ADDRESS |
1553 CORE_CTRL_ADDRESS,
1554 core_ctrl);
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001555 if (ret) {
1556 ath10k_warn("failed to set target CPU interrupt mask: %d\n",
1557 ret);
1558 return ret;
1559 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001560
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001561 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001562}
1563
1564static int ath10k_pci_init_config(struct ath10k *ar)
1565{
1566 u32 interconnect_targ_addr;
1567 u32 pcie_state_targ_addr = 0;
1568 u32 pipe_cfg_targ_addr = 0;
1569 u32 svc_to_pipe_map = 0;
1570 u32 pcie_config_flags = 0;
1571 u32 ealloc_value;
1572 u32 ealloc_targ_addr;
1573 u32 flag2_value;
1574 u32 flag2_targ_addr;
1575 int ret = 0;
1576
1577 /* Download to Target the CE Config and the service-to-CE map */
1578 interconnect_targ_addr =
1579 host_interest_item_address(HI_ITEM(hi_interconnect_state));
1580
1581 /* Supply Target-side CE configuration */
1582 ret = ath10k_pci_diag_read_access(ar, interconnect_targ_addr,
1583 &pcie_state_targ_addr);
1584 if (ret != 0) {
1585 ath10k_err("Failed to get pcie state addr: %d\n", ret);
1586 return ret;
1587 }
1588
1589 if (pcie_state_targ_addr == 0) {
1590 ret = -EIO;
1591 ath10k_err("Invalid pcie state addr\n");
1592 return ret;
1593 }
1594
1595 ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
1596 offsetof(struct pcie_state,
1597 pipe_cfg_addr),
1598 &pipe_cfg_targ_addr);
1599 if (ret != 0) {
1600 ath10k_err("Failed to get pipe cfg addr: %d\n", ret);
1601 return ret;
1602 }
1603
1604 if (pipe_cfg_targ_addr == 0) {
1605 ret = -EIO;
1606 ath10k_err("Invalid pipe cfg addr\n");
1607 return ret;
1608 }
1609
1610 ret = ath10k_pci_diag_write_mem(ar, pipe_cfg_targ_addr,
1611 target_ce_config_wlan,
1612 sizeof(target_ce_config_wlan));
1613
1614 if (ret != 0) {
1615 ath10k_err("Failed to write pipe cfg: %d\n", ret);
1616 return ret;
1617 }
1618
1619 ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
1620 offsetof(struct pcie_state,
1621 svc_to_pipe_map),
1622 &svc_to_pipe_map);
1623 if (ret != 0) {
1624 ath10k_err("Failed to get svc/pipe map: %d\n", ret);
1625 return ret;
1626 }
1627
1628 if (svc_to_pipe_map == 0) {
1629 ret = -EIO;
1630 ath10k_err("Invalid svc_to_pipe map\n");
1631 return ret;
1632 }
1633
1634 ret = ath10k_pci_diag_write_mem(ar, svc_to_pipe_map,
1635 target_service_to_ce_map_wlan,
1636 sizeof(target_service_to_ce_map_wlan));
1637 if (ret != 0) {
1638 ath10k_err("Failed to write svc/pipe map: %d\n", ret);
1639 return ret;
1640 }
1641
1642 ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
1643 offsetof(struct pcie_state,
1644 config_flags),
1645 &pcie_config_flags);
1646 if (ret != 0) {
1647 ath10k_err("Failed to get pcie config_flags: %d\n", ret);
1648 return ret;
1649 }
1650
1651 pcie_config_flags &= ~PCIE_CONFIG_FLAG_ENABLE_L1;
1652
1653 ret = ath10k_pci_diag_write_mem(ar, pcie_state_targ_addr +
1654 offsetof(struct pcie_state, config_flags),
1655 &pcie_config_flags,
1656 sizeof(pcie_config_flags));
1657 if (ret != 0) {
1658 ath10k_err("Failed to write pcie config_flags: %d\n", ret);
1659 return ret;
1660 }
1661
1662 /* configure early allocation */
1663 ealloc_targ_addr = host_interest_item_address(HI_ITEM(hi_early_alloc));
1664
1665 ret = ath10k_pci_diag_read_access(ar, ealloc_targ_addr, &ealloc_value);
1666 if (ret != 0) {
1667 ath10k_err("Faile to get early alloc val: %d\n", ret);
1668 return ret;
1669 }
1670
1671 /* first bank is switched to IRAM */
1672 ealloc_value |= ((HI_EARLY_ALLOC_MAGIC << HI_EARLY_ALLOC_MAGIC_SHIFT) &
1673 HI_EARLY_ALLOC_MAGIC_MASK);
1674 ealloc_value |= ((1 << HI_EARLY_ALLOC_IRAM_BANKS_SHIFT) &
1675 HI_EARLY_ALLOC_IRAM_BANKS_MASK);
1676
1677 ret = ath10k_pci_diag_write_access(ar, ealloc_targ_addr, ealloc_value);
1678 if (ret != 0) {
1679 ath10k_err("Failed to set early alloc val: %d\n", ret);
1680 return ret;
1681 }
1682
1683 /* Tell Target to proceed with initialization */
1684 flag2_targ_addr = host_interest_item_address(HI_ITEM(hi_option_flag2));
1685
1686 ret = ath10k_pci_diag_read_access(ar, flag2_targ_addr, &flag2_value);
1687 if (ret != 0) {
1688 ath10k_err("Failed to get option val: %d\n", ret);
1689 return ret;
1690 }
1691
1692 flag2_value |= HI_OPTION_EARLY_CFG_DONE;
1693
1694 ret = ath10k_pci_diag_write_access(ar, flag2_targ_addr, flag2_value);
1695 if (ret != 0) {
1696 ath10k_err("Failed to set option val: %d\n", ret);
1697 return ret;
1698 }
1699
1700 return 0;
1701}
1702
1703
1704
1705static int ath10k_pci_ce_init(struct ath10k *ar)
1706{
1707 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +02001708 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001709 const struct ce_attr *attr;
1710 int pipe_num;
1711
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001712 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001713 pipe_info = &ar_pci->pipe_info[pipe_num];
1714 pipe_info->pipe_num = pipe_num;
1715 pipe_info->hif_ce_state = ar;
1716 attr = &host_ce_config_wlan[pipe_num];
1717
1718 pipe_info->ce_hdl = ath10k_ce_init(ar, pipe_num, attr);
1719 if (pipe_info->ce_hdl == NULL) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001720 ath10k_err("failed to initialize CE for pipe: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001721 pipe_num);
1722
1723 /* It is safe to call it here. It checks if ce_hdl is
1724 * valid for each pipe */
1725 ath10k_pci_ce_deinit(ar);
1726 return -1;
1727 }
1728
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001729 if (pipe_num == CE_COUNT - 1) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001730 /*
1731 * Reserve the ultimate CE for
1732 * diagnostic Window support
1733 */
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001734 ar_pci->ce_diag = pipe_info->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001735 continue;
1736 }
1737
1738 pipe_info->buf_sz = (size_t) (attr->src_sz_max);
1739 }
1740
Kalle Valo5e3dd152013-06-12 20:52:10 +03001741 return 0;
1742}
1743
1744static void ath10k_pci_fw_interrupt_handler(struct ath10k *ar)
1745{
1746 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1747 u32 fw_indicator_address, fw_indicator;
1748
1749 ath10k_pci_wake(ar);
1750
1751 fw_indicator_address = ar_pci->fw_indicator_address;
1752 fw_indicator = ath10k_pci_read32(ar, fw_indicator_address);
1753
1754 if (fw_indicator & FW_IND_EVENT_PENDING) {
1755 /* ACK: clear Target-side pending event */
1756 ath10k_pci_write32(ar, fw_indicator_address,
1757 fw_indicator & ~FW_IND_EVENT_PENDING);
1758
1759 if (ar_pci->started) {
1760 ath10k_pci_hif_dump_area(ar);
1761 } else {
1762 /*
1763 * Probable Target failure before we're prepared
1764 * to handle it. Generally unexpected.
1765 */
1766 ath10k_warn("early firmware event indicated\n");
1767 }
1768 }
1769
1770 ath10k_pci_sleep(ar);
1771}
1772
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001773static int ath10k_pci_warm_reset(struct ath10k *ar)
1774{
1775 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1776 int ret = 0;
1777 u32 val;
1778
1779 ath10k_dbg(ATH10K_DBG_BOOT, "boot performing warm chip reset\n");
1780
1781 ret = ath10k_do_pci_wake(ar);
1782 if (ret) {
1783 ath10k_err("failed to wake up target: %d\n", ret);
1784 return ret;
1785 }
1786
1787 /* debug */
1788 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1789 PCIE_INTR_CAUSE_ADDRESS);
1790 ath10k_dbg(ATH10K_DBG_BOOT, "boot host cpu intr cause: 0x%08x\n", val);
1791
1792 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1793 CPU_INTR_ADDRESS);
1794 ath10k_dbg(ATH10K_DBG_BOOT, "boot target cpu intr cause: 0x%08x\n",
1795 val);
1796
1797 /* disable pending irqs */
1798 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
1799 PCIE_INTR_ENABLE_ADDRESS, 0);
1800
1801 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
1802 PCIE_INTR_CLR_ADDRESS, ~0);
1803
1804 msleep(100);
1805
1806 /* clear fw indicator */
1807 ath10k_pci_write32(ar, ar_pci->fw_indicator_address, 0);
1808
1809 /* clear target LF timer interrupts */
1810 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1811 SOC_LF_TIMER_CONTROL0_ADDRESS);
1812 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS +
1813 SOC_LF_TIMER_CONTROL0_ADDRESS,
1814 val & ~SOC_LF_TIMER_CONTROL0_ENABLE_MASK);
1815
1816 /* reset CE */
1817 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1818 SOC_RESET_CONTROL_ADDRESS);
1819 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1820 val | SOC_RESET_CONTROL_CE_RST_MASK);
1821 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1822 SOC_RESET_CONTROL_ADDRESS);
1823 msleep(10);
1824
1825 /* unreset CE */
1826 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1827 val & ~SOC_RESET_CONTROL_CE_RST_MASK);
1828 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1829 SOC_RESET_CONTROL_ADDRESS);
1830 msleep(10);
1831
1832 /* debug */
1833 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1834 PCIE_INTR_CAUSE_ADDRESS);
1835 ath10k_dbg(ATH10K_DBG_BOOT, "boot host cpu intr cause: 0x%08x\n", val);
1836
1837 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1838 CPU_INTR_ADDRESS);
1839 ath10k_dbg(ATH10K_DBG_BOOT, "boot target cpu intr cause: 0x%08x\n",
1840 val);
1841
1842 /* CPU warm reset */
1843 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1844 SOC_RESET_CONTROL_ADDRESS);
1845 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1846 val | SOC_RESET_CONTROL_CPU_WARM_RST_MASK);
1847
1848 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1849 SOC_RESET_CONTROL_ADDRESS);
1850 ath10k_dbg(ATH10K_DBG_BOOT, "boot target reset state: 0x%08x\n", val);
1851
1852 msleep(100);
1853
1854 ath10k_dbg(ATH10K_DBG_BOOT, "boot warm reset complete\n");
1855
1856 ath10k_do_pci_sleep(ar);
1857 return ret;
1858}
1859
1860static int __ath10k_pci_hif_power_up(struct ath10k *ar, bool cold_reset)
Michal Kazior8c5c5362013-07-16 09:38:50 +02001861{
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02001862 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo95cbb6a2013-11-20 10:00:35 +02001863 const char *irq_mode;
Michal Kazior8c5c5362013-07-16 09:38:50 +02001864 int ret;
1865
1866 /*
1867 * Bring the target up cleanly.
1868 *
1869 * The target may be in an undefined state with an AUX-powered Target
1870 * and a Host in WoW mode. If the Host crashes, loses power, or is
1871 * restarted (without unloading the driver) then the Target is left
1872 * (aux) powered and running. On a subsequent driver load, the Target
1873 * is in an unexpected state. We try to catch that here in order to
1874 * reset the Target and retry the probe.
1875 */
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001876 if (cold_reset)
1877 ret = ath10k_pci_cold_reset(ar);
1878 else
1879 ret = ath10k_pci_warm_reset(ar);
1880
Michal Kazior5b2589f2013-11-08 08:01:30 +01001881 if (ret) {
1882 ath10k_err("failed to reset target: %d\n", ret);
Michal Kazior98563d52013-11-08 08:01:33 +01001883 goto err;
Michal Kazior5b2589f2013-11-08 08:01:30 +01001884 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02001885
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02001886 if (!test_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE, ar_pci->features))
Michal Kazior8c5c5362013-07-16 09:38:50 +02001887 /* Force AWAKE forever */
Michal Kazior8c5c5362013-07-16 09:38:50 +02001888 ath10k_do_pci_wake(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001889
1890 ret = ath10k_pci_ce_init(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001891 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001892 ath10k_err("failed to initialize CE: %d\n", ret);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001893 goto err_ps;
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001894 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02001895
Michal Kazior98563d52013-11-08 08:01:33 +01001896 ret = ath10k_ce_disable_interrupts(ar);
1897 if (ret) {
1898 ath10k_err("failed to disable CE interrupts: %d\n", ret);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001899 goto err_ce;
1900 }
1901
Michal Kaziorfc15ca12013-11-25 14:06:21 +01001902 ret = ath10k_pci_init_irq(ar);
Michal Kazior98563d52013-11-08 08:01:33 +01001903 if (ret) {
Michal Kaziorfc15ca12013-11-25 14:06:21 +01001904 ath10k_err("failed to init irqs: %d\n", ret);
Michal Kazior98563d52013-11-08 08:01:33 +01001905 goto err_ce;
1906 }
1907
Michal Kaziorab977bd2013-11-25 14:06:26 +01001908 ret = ath10k_pci_request_early_irq(ar);
1909 if (ret) {
1910 ath10k_err("failed to request early irq: %d\n", ret);
1911 goto err_deinit_irq;
1912 }
1913
Michal Kazior98563d52013-11-08 08:01:33 +01001914 ret = ath10k_pci_wait_for_target_init(ar);
1915 if (ret) {
1916 ath10k_err("failed to wait for target to init: %d\n", ret);
Michal Kaziorab977bd2013-11-25 14:06:26 +01001917 goto err_free_early_irq;
Michal Kazior98563d52013-11-08 08:01:33 +01001918 }
1919
1920 ret = ath10k_pci_init_config(ar);
1921 if (ret) {
1922 ath10k_err("failed to setup init config: %d\n", ret);
Michal Kaziorab977bd2013-11-25 14:06:26 +01001923 goto err_free_early_irq;
Michal Kazior98563d52013-11-08 08:01:33 +01001924 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02001925
1926 ret = ath10k_pci_wake_target_cpu(ar);
1927 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001928 ath10k_err("could not wake up target CPU: %d\n", ret);
Michal Kaziorab977bd2013-11-25 14:06:26 +01001929 goto err_free_early_irq;
Michal Kazior8c5c5362013-07-16 09:38:50 +02001930 }
1931
Kalle Valo95cbb6a2013-11-20 10:00:35 +02001932 if (ar_pci->num_msi_intrs > 1)
1933 irq_mode = "MSI-X";
1934 else if (ar_pci->num_msi_intrs == 1)
1935 irq_mode = "MSI";
1936 else
1937 irq_mode = "legacy";
1938
Kalle Valo650b91f2013-11-20 10:00:49 +02001939 if (!test_bit(ATH10K_FLAG_FIRST_BOOT_DONE, &ar->dev_flags))
1940 ath10k_info("pci irq %s\n", irq_mode);
Kalle Valo95cbb6a2013-11-20 10:00:35 +02001941
Michal Kazior8c5c5362013-07-16 09:38:50 +02001942 return 0;
1943
Michal Kaziorab977bd2013-11-25 14:06:26 +01001944err_free_early_irq:
1945 ath10k_pci_free_early_irq(ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01001946err_deinit_irq:
1947 ath10k_pci_deinit_irq(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001948err_ce:
1949 ath10k_pci_ce_deinit(ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001950 ath10k_pci_warm_reset(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001951err_ps:
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02001952 if (!test_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE, ar_pci->features))
Michal Kazior8c5c5362013-07-16 09:38:50 +02001953 ath10k_do_pci_sleep(ar);
1954err:
1955 return ret;
1956}
1957
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001958static int ath10k_pci_hif_power_up(struct ath10k *ar)
1959{
1960 int ret;
1961
1962 /*
1963 * Hardware CUS232 version 2 has some issues with cold reset and the
1964 * preferred (and safer) way to perform a device reset is through a
1965 * warm reset.
1966 *
1967 * Warm reset doesn't always work though (notably after a firmware
1968 * crash) so fall back to cold reset if necessary.
1969 */
1970 ret = __ath10k_pci_hif_power_up(ar, false);
1971 if (ret) {
1972 ath10k_warn("failed to power up target using warm reset (%d), trying cold reset\n",
1973 ret);
1974
1975 ret = __ath10k_pci_hif_power_up(ar, true);
1976 if (ret) {
1977 ath10k_err("failed to power up target using cold reset too (%d)\n",
1978 ret);
1979 return ret;
1980 }
1981 }
1982
1983 return 0;
1984}
1985
Michal Kazior8c5c5362013-07-16 09:38:50 +02001986static void ath10k_pci_hif_power_down(struct ath10k *ar)
1987{
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02001988 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1989
Michal Kaziorab977bd2013-11-25 14:06:26 +01001990 ath10k_pci_free_early_irq(ar);
1991 ath10k_pci_kill_tasklet(ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01001992 ath10k_pci_deinit_irq(ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001993 ath10k_pci_warm_reset(ar);
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02001994
Michal Kazior8c5c5362013-07-16 09:38:50 +02001995 ath10k_pci_ce_deinit(ar);
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02001996 if (!test_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE, ar_pci->features))
Michal Kazior8c5c5362013-07-16 09:38:50 +02001997 ath10k_do_pci_sleep(ar);
1998}
1999
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002000#ifdef CONFIG_PM
2001
2002#define ATH10K_PCI_PM_CONTROL 0x44
2003
2004static int ath10k_pci_hif_suspend(struct ath10k *ar)
2005{
2006 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2007 struct pci_dev *pdev = ar_pci->pdev;
2008 u32 val;
2009
2010 pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
2011
2012 if ((val & 0x000000ff) != 0x3) {
2013 pci_save_state(pdev);
2014 pci_disable_device(pdev);
2015 pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
2016 (val & 0xffffff00) | 0x03);
2017 }
2018
2019 return 0;
2020}
2021
2022static int ath10k_pci_hif_resume(struct ath10k *ar)
2023{
2024 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2025 struct pci_dev *pdev = ar_pci->pdev;
2026 u32 val;
2027
2028 pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
2029
2030 if ((val & 0x000000ff) != 0) {
2031 pci_restore_state(pdev);
2032 pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
2033 val & 0xffffff00);
2034 /*
2035 * Suspend/Resume resets the PCI configuration space,
2036 * so we have to re-disable the RETRY_TIMEOUT register (0x41)
2037 * to keep PCI Tx retries from interfering with C3 CPU state
2038 */
2039 pci_read_config_dword(pdev, 0x40, &val);
2040
2041 if ((val & 0x0000ff00) != 0)
2042 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
2043 }
2044
2045 return 0;
2046}
2047#endif
2048
Kalle Valo5e3dd152013-06-12 20:52:10 +03002049static const struct ath10k_hif_ops ath10k_pci_hif_ops = {
Michal Kazior726346f2014-02-27 18:50:04 +02002050 .tx_sg = ath10k_pci_hif_tx_sg,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002051 .exchange_bmi_msg = ath10k_pci_hif_exchange_bmi_msg,
2052 .start = ath10k_pci_hif_start,
2053 .stop = ath10k_pci_hif_stop,
2054 .map_service_to_pipe = ath10k_pci_hif_map_service_to_pipe,
2055 .get_default_pipe = ath10k_pci_hif_get_default_pipe,
2056 .send_complete_check = ath10k_pci_hif_send_complete_check,
Michal Kaziore799bbf2013-07-05 16:15:12 +03002057 .set_callbacks = ath10k_pci_hif_set_callbacks,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002058 .get_free_queue_number = ath10k_pci_hif_get_free_queue_number,
Michal Kazior8c5c5362013-07-16 09:38:50 +02002059 .power_up = ath10k_pci_hif_power_up,
2060 .power_down = ath10k_pci_hif_power_down,
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002061#ifdef CONFIG_PM
2062 .suspend = ath10k_pci_hif_suspend,
2063 .resume = ath10k_pci_hif_resume,
2064#endif
Kalle Valo5e3dd152013-06-12 20:52:10 +03002065};
2066
2067static void ath10k_pci_ce_tasklet(unsigned long ptr)
2068{
Michal Kazior87263e52013-08-27 13:08:01 +02002069 struct ath10k_pci_pipe *pipe = (struct ath10k_pci_pipe *)ptr;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002070 struct ath10k_pci *ar_pci = pipe->ar_pci;
2071
2072 ath10k_ce_per_engine_service(ar_pci->ar, pipe->pipe_num);
2073}
2074
2075static void ath10k_msi_err_tasklet(unsigned long data)
2076{
2077 struct ath10k *ar = (struct ath10k *)data;
2078
2079 ath10k_pci_fw_interrupt_handler(ar);
2080}
2081
2082/*
2083 * Handler for a per-engine interrupt on a PARTICULAR CE.
2084 * This is used in cases where each CE has a private MSI interrupt.
2085 */
2086static irqreturn_t ath10k_pci_per_engine_handler(int irq, void *arg)
2087{
2088 struct ath10k *ar = arg;
2089 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2090 int ce_id = irq - ar_pci->pdev->irq - MSI_ASSIGN_CE_INITIAL;
2091
Dan Carpentere5742672013-06-18 10:28:46 +03002092 if (ce_id < 0 || ce_id >= ARRAY_SIZE(ar_pci->pipe_info)) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03002093 ath10k_warn("unexpected/invalid irq %d ce_id %d\n", irq, ce_id);
2094 return IRQ_HANDLED;
2095 }
2096
2097 /*
2098 * NOTE: We are able to derive ce_id from irq because we
2099 * use a one-to-one mapping for CE's 0..5.
2100 * CE's 6 & 7 do not use interrupts at all.
2101 *
2102 * This mapping must be kept in sync with the mapping
2103 * used by firmware.
2104 */
2105 tasklet_schedule(&ar_pci->pipe_info[ce_id].intr);
2106 return IRQ_HANDLED;
2107}
2108
2109static irqreturn_t ath10k_pci_msi_fw_handler(int irq, void *arg)
2110{
2111 struct ath10k *ar = arg;
2112 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2113
2114 tasklet_schedule(&ar_pci->msi_fw_err);
2115 return IRQ_HANDLED;
2116}
2117
2118/*
2119 * Top-level interrupt handler for all PCI interrupts from a Target.
2120 * When a block of MSI interrupts is allocated, this top-level handler
2121 * is not used; instead, we directly call the correct sub-handler.
2122 */
2123static irqreturn_t ath10k_pci_interrupt_handler(int irq, void *arg)
2124{
2125 struct ath10k *ar = arg;
2126 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2127
2128 if (ar_pci->num_msi_intrs == 0) {
Michal Kaziore5398872013-11-25 14:06:20 +01002129 if (!ath10k_pci_irq_pending(ar))
2130 return IRQ_NONE;
2131
Michal Kazior26852182013-11-25 14:06:25 +01002132 ath10k_pci_disable_and_clear_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002133 }
2134
2135 tasklet_schedule(&ar_pci->intr_tq);
2136
2137 return IRQ_HANDLED;
2138}
2139
Michal Kaziorab977bd2013-11-25 14:06:26 +01002140static void ath10k_pci_early_irq_tasklet(unsigned long data)
2141{
2142 struct ath10k *ar = (struct ath10k *)data;
2143 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2144 u32 fw_ind;
2145 int ret;
2146
2147 ret = ath10k_pci_wake(ar);
2148 if (ret) {
2149 ath10k_warn("failed to wake target in early irq tasklet: %d\n",
2150 ret);
2151 return;
2152 }
2153
2154 fw_ind = ath10k_pci_read32(ar, ar_pci->fw_indicator_address);
2155 if (fw_ind & FW_IND_EVENT_PENDING) {
2156 ath10k_pci_write32(ar, ar_pci->fw_indicator_address,
2157 fw_ind & ~FW_IND_EVENT_PENDING);
2158
2159 /* Some structures are unavailable during early boot or at
2160 * driver teardown so just print that the device has crashed. */
2161 ath10k_warn("device crashed - no diagnostics available\n");
2162 }
2163
2164 ath10k_pci_sleep(ar);
2165 ath10k_pci_enable_legacy_irq(ar);
2166}
2167
Kalle Valo5e3dd152013-06-12 20:52:10 +03002168static void ath10k_pci_tasklet(unsigned long data)
2169{
2170 struct ath10k *ar = (struct ath10k *)data;
2171 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2172
2173 ath10k_pci_fw_interrupt_handler(ar); /* FIXME: Handle FW error */
2174 ath10k_ce_per_engine_service_any(ar);
2175
Michal Kazior26852182013-11-25 14:06:25 +01002176 /* Re-enable legacy irq that was disabled in the irq handler */
2177 if (ar_pci->num_msi_intrs == 0)
2178 ath10k_pci_enable_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002179}
2180
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002181static int ath10k_pci_request_irq_msix(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002182{
2183 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002184 int ret, i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002185
2186 ret = request_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW,
2187 ath10k_pci_msi_fw_handler,
2188 IRQF_SHARED, "ath10k_pci", ar);
Michal Kazior591ecdb2013-07-31 10:55:15 +02002189 if (ret) {
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002190 ath10k_warn("failed to request MSI-X fw irq %d: %d\n",
Michal Kazior591ecdb2013-07-31 10:55:15 +02002191 ar_pci->pdev->irq + MSI_ASSIGN_FW, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002192 return ret;
Michal Kazior591ecdb2013-07-31 10:55:15 +02002193 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002194
2195 for (i = MSI_ASSIGN_CE_INITIAL; i <= MSI_ASSIGN_CE_MAX; i++) {
2196 ret = request_irq(ar_pci->pdev->irq + i,
2197 ath10k_pci_per_engine_handler,
2198 IRQF_SHARED, "ath10k_pci", ar);
2199 if (ret) {
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002200 ath10k_warn("failed to request MSI-X ce irq %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002201 ar_pci->pdev->irq + i, ret);
2202
Michal Kazior87b14232013-06-26 08:50:50 +02002203 for (i--; i >= MSI_ASSIGN_CE_INITIAL; i--)
2204 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002205
Michal Kazior87b14232013-06-26 08:50:50 +02002206 free_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002207 return ret;
2208 }
2209 }
2210
Kalle Valo5e3dd152013-06-12 20:52:10 +03002211 return 0;
2212}
2213
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002214static int ath10k_pci_request_irq_msi(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002215{
2216 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2217 int ret;
2218
2219 ret = request_irq(ar_pci->pdev->irq,
2220 ath10k_pci_interrupt_handler,
2221 IRQF_SHARED, "ath10k_pci", ar);
Kalle Valof3782742013-10-17 11:36:15 +03002222 if (ret) {
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002223 ath10k_warn("failed to request MSI irq %d: %d\n",
2224 ar_pci->pdev->irq, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002225 return ret;
Kalle Valof3782742013-10-17 11:36:15 +03002226 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002227
Kalle Valo5e3dd152013-06-12 20:52:10 +03002228 return 0;
2229}
2230
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002231static int ath10k_pci_request_irq_legacy(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002232{
2233 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002234 int ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002235
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002236 ret = request_irq(ar_pci->pdev->irq,
2237 ath10k_pci_interrupt_handler,
2238 IRQF_SHARED, "ath10k_pci", ar);
Kalle Valof3782742013-10-17 11:36:15 +03002239 if (ret) {
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002240 ath10k_warn("failed to request legacy irq %d: %d\n",
2241 ar_pci->pdev->irq, ret);
Kalle Valof3782742013-10-17 11:36:15 +03002242 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002243 }
2244
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002245 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002246}
2247
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002248static int ath10k_pci_request_irq(struct ath10k *ar)
2249{
2250 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2251
2252 switch (ar_pci->num_msi_intrs) {
2253 case 0:
2254 return ath10k_pci_request_irq_legacy(ar);
2255 case 1:
2256 return ath10k_pci_request_irq_msi(ar);
2257 case MSI_NUM_REQUEST:
2258 return ath10k_pci_request_irq_msix(ar);
2259 }
2260
2261 ath10k_warn("unknown irq configuration upon request\n");
2262 return -EINVAL;
2263}
2264
2265static void ath10k_pci_free_irq(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002266{
2267 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2268 int i;
2269
2270 /* There's at least one interrupt irregardless whether its legacy INTR
2271 * or MSI or MSI-X */
2272 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
2273 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002274}
2275
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002276static void ath10k_pci_init_irq_tasklets(struct ath10k *ar)
2277{
2278 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2279 int i;
2280
2281 tasklet_init(&ar_pci->intr_tq, ath10k_pci_tasklet, (unsigned long)ar);
2282 tasklet_init(&ar_pci->msi_fw_err, ath10k_msi_err_tasklet,
2283 (unsigned long)ar);
Michal Kaziorab977bd2013-11-25 14:06:26 +01002284 tasklet_init(&ar_pci->early_irq_tasklet, ath10k_pci_early_irq_tasklet,
2285 (unsigned long)ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002286
2287 for (i = 0; i < CE_COUNT; i++) {
2288 ar_pci->pipe_info[i].ar_pci = ar_pci;
2289 tasklet_init(&ar_pci->pipe_info[i].intr, ath10k_pci_ce_tasklet,
2290 (unsigned long)&ar_pci->pipe_info[i]);
2291 }
2292}
2293
2294static int ath10k_pci_init_irq(struct ath10k *ar)
2295{
2296 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002297 bool msix_supported = test_bit(ATH10K_PCI_FEATURE_MSI_X,
2298 ar_pci->features);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002299 int ret;
2300
2301 ath10k_pci_init_irq_tasklets(ar);
2302
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002303 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_AUTO &&
2304 !test_bit(ATH10K_FLAG_FIRST_BOOT_DONE, &ar->dev_flags))
2305 ath10k_info("limiting irq mode to: %d\n", ath10k_pci_irq_mode);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002306
2307 /* Try MSI-X */
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002308 if (ath10k_pci_irq_mode == ATH10K_PCI_IRQ_AUTO && msix_supported) {
2309 ar_pci->num_msi_intrs = MSI_NUM_REQUEST;
Alexander Gordeev5ad68672014-02-13 17:50:02 +02002310 ret = pci_enable_msi_range(ar_pci->pdev, ar_pci->num_msi_intrs,
2311 ar_pci->num_msi_intrs);
2312 if (ret > 0)
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002313 return 0;
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002314
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002315 /* fall-through */
2316 }
2317
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002318 /* Try MSI */
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002319 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_LEGACY) {
2320 ar_pci->num_msi_intrs = 1;
2321 ret = pci_enable_msi(ar_pci->pdev);
2322 if (ret == 0)
2323 return 0;
2324
2325 /* fall-through */
2326 }
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002327
2328 /* Try legacy irq
2329 *
2330 * A potential race occurs here: The CORE_BASE write
2331 * depends on target correctly decoding AXI address but
2332 * host won't know when target writes BAR to CORE_CTRL.
2333 * This write might get lost if target has NOT written BAR.
2334 * For now, fix the race by repeating the write in below
2335 * synchronization checking. */
2336 ar_pci->num_msi_intrs = 0;
2337
2338 ret = ath10k_pci_wake(ar);
2339 if (ret) {
2340 ath10k_warn("failed to wake target: %d\n", ret);
2341 return ret;
2342 }
2343
2344 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2345 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
2346 ath10k_pci_sleep(ar);
2347
2348 return 0;
2349}
2350
2351static int ath10k_pci_deinit_irq_legacy(struct ath10k *ar)
2352{
2353 int ret;
2354
2355 ret = ath10k_pci_wake(ar);
2356 if (ret) {
2357 ath10k_warn("failed to wake target: %d\n", ret);
2358 return ret;
2359 }
2360
2361 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2362 0);
2363 ath10k_pci_sleep(ar);
2364
2365 return 0;
2366}
2367
2368static int ath10k_pci_deinit_irq(struct ath10k *ar)
2369{
2370 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2371
2372 switch (ar_pci->num_msi_intrs) {
2373 case 0:
2374 return ath10k_pci_deinit_irq_legacy(ar);
2375 case 1:
2376 /* fall-through */
2377 case MSI_NUM_REQUEST:
2378 pci_disable_msi(ar_pci->pdev);
2379 return 0;
Alexander Gordeevbb8b6212014-02-13 17:50:01 +02002380 default:
2381 pci_disable_msi(ar_pci->pdev);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002382 }
2383
2384 ath10k_warn("unknown irq configuration upon deinit\n");
2385 return -EINVAL;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002386}
2387
Michal Kaziord7fb47f2013-11-08 08:01:26 +01002388static int ath10k_pci_wait_for_target_init(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002389{
2390 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo0399eca2014-03-28 09:32:21 +02002391 unsigned long timeout;
Kalle Valof3782742013-10-17 11:36:15 +03002392 int ret;
Kalle Valo0399eca2014-03-28 09:32:21 +02002393 u32 val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002394
Michal Kazior98563d52013-11-08 08:01:33 +01002395 ret = ath10k_pci_wake(ar);
Kalle Valof3782742013-10-17 11:36:15 +03002396 if (ret) {
Kalle Valo0399eca2014-03-28 09:32:21 +02002397 ath10k_err("failed to wake up target for init: %d\n", ret);
Kalle Valof3782742013-10-17 11:36:15 +03002398 return ret;
2399 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002400
Kalle Valo0399eca2014-03-28 09:32:21 +02002401 timeout = jiffies + msecs_to_jiffies(ATH10K_PCI_TARGET_WAIT);
2402
2403 do {
2404 val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
2405
2406 /* target should never return this */
2407 if (val == 0xffffffff)
2408 continue;
2409
2410 if (val & FW_IND_INITIALIZED)
2411 break;
2412
Kalle Valo5e3dd152013-06-12 20:52:10 +03002413 if (ar_pci->num_msi_intrs == 0)
2414 /* Fix potential race by repeating CORE_BASE writes */
Kalle Valo0399eca2014-03-28 09:32:21 +02002415 ath10k_pci_soc_write32(ar, PCIE_INTR_ENABLE_ADDRESS,
2416 PCIE_INTR_FIRMWARE_MASK |
2417 PCIE_INTR_CE_MASK_ALL);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002418
Kalle Valo0399eca2014-03-28 09:32:21 +02002419 mdelay(10);
2420 } while (time_before(jiffies, timeout));
2421
2422 if (val == 0xffffffff || !(val & FW_IND_INITIALIZED)) {
2423 ath10k_err("failed to receive initialized event from target: %08x\n",
2424 val);
2425 ret = -ETIMEDOUT;
Michal Kazior5b2589f2013-11-08 08:01:30 +01002426 goto out;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002427 }
2428
Michal Kazior5b2589f2013-11-08 08:01:30 +01002429out:
Michal Kazior98563d52013-11-08 08:01:33 +01002430 ath10k_pci_sleep(ar);
Michal Kazior5b2589f2013-11-08 08:01:30 +01002431 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002432}
2433
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002434static int ath10k_pci_cold_reset(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002435{
Michal Kazior5b2589f2013-11-08 08:01:30 +01002436 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002437 u32 val;
2438
Michal Kazior5b2589f2013-11-08 08:01:30 +01002439 ret = ath10k_do_pci_wake(ar);
2440 if (ret) {
2441 ath10k_err("failed to wake up target: %d\n",
2442 ret);
2443 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002444 }
2445
2446 /* Put Target, including PCIe, into RESET. */
Kalle Valoe479ed42013-09-01 10:01:53 +03002447 val = ath10k_pci_reg_read32(ar, SOC_GLOBAL_RESET_ADDRESS);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002448 val |= 1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002449 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002450
2451 for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
Kalle Valoe479ed42013-09-01 10:01:53 +03002452 if (ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03002453 RTC_STATE_COLD_RESET_MASK)
2454 break;
2455 msleep(1);
2456 }
2457
2458 /* Pull Target, including PCIe, out of RESET. */
2459 val &= ~1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002460 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002461
2462 for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
Kalle Valoe479ed42013-09-01 10:01:53 +03002463 if (!(ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03002464 RTC_STATE_COLD_RESET_MASK))
2465 break;
2466 msleep(1);
2467 }
2468
Michal Kazior5b2589f2013-11-08 08:01:30 +01002469 ath10k_do_pci_sleep(ar);
2470 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002471}
2472
2473static void ath10k_pci_dump_features(struct ath10k_pci *ar_pci)
2474{
2475 int i;
2476
2477 for (i = 0; i < ATH10K_PCI_FEATURE_COUNT; i++) {
2478 if (!test_bit(i, ar_pci->features))
2479 continue;
2480
2481 switch (i) {
2482 case ATH10K_PCI_FEATURE_MSI_X:
Kalle Valo24cfade2013-09-08 17:55:50 +03002483 ath10k_dbg(ATH10K_DBG_BOOT, "device supports MSI-X\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002484 break;
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002485 case ATH10K_PCI_FEATURE_SOC_POWER_SAVE:
Kalle Valo24cfade2013-09-08 17:55:50 +03002486 ath10k_dbg(ATH10K_DBG_BOOT, "QCA98XX SoC power save enabled\n");
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002487 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002488 }
2489 }
2490}
2491
2492static int ath10k_pci_probe(struct pci_dev *pdev,
2493 const struct pci_device_id *pci_dev)
2494{
2495 void __iomem *mem;
2496 int ret = 0;
2497 struct ath10k *ar;
2498 struct ath10k_pci *ar_pci;
Kalle Valoe01ae682013-09-01 11:22:14 +03002499 u32 lcr_val, chip_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002500
2501 ath10k_dbg(ATH10K_DBG_PCI, "%s\n", __func__);
2502
2503 ar_pci = kzalloc(sizeof(*ar_pci), GFP_KERNEL);
2504 if (ar_pci == NULL)
2505 return -ENOMEM;
2506
2507 ar_pci->pdev = pdev;
2508 ar_pci->dev = &pdev->dev;
2509
2510 switch (pci_dev->device) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03002511 case QCA988X_2_0_DEVICE_ID:
2512 set_bit(ATH10K_PCI_FEATURE_MSI_X, ar_pci->features);
2513 break;
2514 default:
2515 ret = -ENODEV;
Masanari Iida6d3be302013-09-30 23:19:09 +09002516 ath10k_err("Unknown device ID: %d\n", pci_dev->device);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002517 goto err_ar_pci;
2518 }
2519
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002520 if (ath10k_target_ps)
2521 set_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE, ar_pci->features);
2522
Kalle Valo5e3dd152013-06-12 20:52:10 +03002523 ath10k_pci_dump_features(ar_pci);
2524
Michal Kazior3a0861f2013-07-05 16:15:06 +03002525 ar = ath10k_core_create(ar_pci, ar_pci->dev, &ath10k_pci_hif_ops);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002526 if (!ar) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002527 ath10k_err("failed to create driver core\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002528 ret = -EINVAL;
2529 goto err_ar_pci;
2530 }
2531
Kalle Valo5e3dd152013-06-12 20:52:10 +03002532 ar_pci->ar = ar;
2533 ar_pci->fw_indicator_address = FW_INDICATOR_ADDRESS;
2534 atomic_set(&ar_pci->keep_awake_count, 0);
2535
2536 pci_set_drvdata(pdev, ar);
2537
2538 /*
2539 * Without any knowledge of the Host, the Target may have been reset or
2540 * power cycled and its Config Space may no longer reflect the PCI
2541 * address space that was assigned earlier by the PCI infrastructure.
2542 * Refresh it now.
2543 */
2544 ret = pci_assign_resource(pdev, BAR_NUM);
2545 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002546 ath10k_err("failed to assign PCI space: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002547 goto err_ar;
2548 }
2549
2550 ret = pci_enable_device(pdev);
2551 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002552 ath10k_err("failed to enable PCI device: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002553 goto err_ar;
2554 }
2555
2556 /* Request MMIO resources */
2557 ret = pci_request_region(pdev, BAR_NUM, "ath");
2558 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002559 ath10k_err("failed to request MMIO region: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002560 goto err_device;
2561 }
2562
2563 /*
2564 * Target structures have a limit of 32 bit DMA pointers.
2565 * DMA pointers can be wider than 32 bits by default on some systems.
2566 */
2567 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2568 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002569 ath10k_err("failed to set DMA mask to 32-bit: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002570 goto err_region;
2571 }
2572
2573 ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2574 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002575 ath10k_err("failed to set consistent DMA mask to 32-bit\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002576 goto err_region;
2577 }
2578
2579 /* Set bus master bit in PCI_COMMAND to enable DMA */
2580 pci_set_master(pdev);
2581
2582 /*
2583 * Temporary FIX: disable ASPM
2584 * Will be removed after the OTP is programmed
2585 */
2586 pci_read_config_dword(pdev, 0x80, &lcr_val);
2587 pci_write_config_dword(pdev, 0x80, (lcr_val & 0xffffff00));
2588
2589 /* Arrange for access to Target SoC registers. */
2590 mem = pci_iomap(pdev, BAR_NUM, 0);
2591 if (!mem) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002592 ath10k_err("failed to perform IOMAP for BAR%d\n", BAR_NUM);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002593 ret = -EIO;
2594 goto err_master;
2595 }
2596
2597 ar_pci->mem = mem;
2598
2599 spin_lock_init(&ar_pci->ce_lock);
2600
Kalle Valoe01ae682013-09-01 11:22:14 +03002601 ret = ath10k_do_pci_wake(ar);
2602 if (ret) {
2603 ath10k_err("Failed to get chip id: %d\n", ret);
Wei Yongjun12eb0872013-10-30 13:24:39 +08002604 goto err_iomap;
Kalle Valoe01ae682013-09-01 11:22:14 +03002605 }
2606
Kalle Valo233eb972013-10-16 16:46:11 +03002607 chip_id = ath10k_pci_soc_read32(ar, SOC_CHIP_ID_ADDRESS);
Kalle Valoe01ae682013-09-01 11:22:14 +03002608
2609 ath10k_do_pci_sleep(ar);
2610
Kalle Valo24cfade2013-09-08 17:55:50 +03002611 ath10k_dbg(ATH10K_DBG_BOOT, "boot pci_mem 0x%p\n", ar_pci->mem);
2612
Kalle Valoe01ae682013-09-01 11:22:14 +03002613 ret = ath10k_core_register(ar, chip_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002614 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002615 ath10k_err("failed to register driver core: %d\n", ret);
Michal Kazior32270b62013-08-02 09:15:47 +02002616 goto err_iomap;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002617 }
2618
2619 return 0;
2620
Kalle Valo5e3dd152013-06-12 20:52:10 +03002621err_iomap:
2622 pci_iounmap(pdev, mem);
2623err_master:
2624 pci_clear_master(pdev);
2625err_region:
2626 pci_release_region(pdev, BAR_NUM);
2627err_device:
2628 pci_disable_device(pdev);
2629err_ar:
Kalle Valo5e3dd152013-06-12 20:52:10 +03002630 ath10k_core_destroy(ar);
2631err_ar_pci:
2632 /* call HIF PCI free here */
2633 kfree(ar_pci);
2634
2635 return ret;
2636}
2637
2638static void ath10k_pci_remove(struct pci_dev *pdev)
2639{
2640 struct ath10k *ar = pci_get_drvdata(pdev);
2641 struct ath10k_pci *ar_pci;
2642
2643 ath10k_dbg(ATH10K_DBG_PCI, "%s\n", __func__);
2644
2645 if (!ar)
2646 return;
2647
2648 ar_pci = ath10k_pci_priv(ar);
2649
2650 if (!ar_pci)
2651 return;
2652
2653 tasklet_kill(&ar_pci->msi_fw_err);
2654
2655 ath10k_core_unregister(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002656
Kalle Valo5e3dd152013-06-12 20:52:10 +03002657 pci_iounmap(pdev, ar_pci->mem);
2658 pci_release_region(pdev, BAR_NUM);
2659 pci_clear_master(pdev);
2660 pci_disable_device(pdev);
2661
2662 ath10k_core_destroy(ar);
2663 kfree(ar_pci);
2664}
2665
Kalle Valo5e3dd152013-06-12 20:52:10 +03002666MODULE_DEVICE_TABLE(pci, ath10k_pci_id_table);
2667
2668static struct pci_driver ath10k_pci_driver = {
2669 .name = "ath10k_pci",
2670 .id_table = ath10k_pci_id_table,
2671 .probe = ath10k_pci_probe,
2672 .remove = ath10k_pci_remove,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002673};
2674
2675static int __init ath10k_pci_init(void)
2676{
2677 int ret;
2678
2679 ret = pci_register_driver(&ath10k_pci_driver);
2680 if (ret)
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002681 ath10k_err("failed to register PCI driver: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002682
2683 return ret;
2684}
2685module_init(ath10k_pci_init);
2686
2687static void __exit ath10k_pci_exit(void)
2688{
2689 pci_unregister_driver(&ath10k_pci_driver);
2690}
2691
2692module_exit(ath10k_pci_exit);
2693
2694MODULE_AUTHOR("Qualcomm Atheros");
2695MODULE_DESCRIPTION("Driver support for Atheros QCA988X PCIe devices");
2696MODULE_LICENSE("Dual BSD/GPL");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002697MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_FW_FILE);
2698MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_OTP_FILE);
2699MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_BOARD_DATA_FILE);