blob: b1eb9153c7f48e35f5f49605b0268380642bb17f [file] [log] [blame]
Kalle Valo5e3dd152013-06-12 20:52:10 +03001/*
2 * Copyright (c) 2005-2011 Atheros Communications Inc.
3 * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <linux/pci.h>
19#include <linux/module.h>
20#include <linux/interrupt.h>
21#include <linux/spinlock.h>
Kalle Valo650b91f2013-11-20 10:00:49 +020022#include <linux/bitops.h>
Kalle Valo5e3dd152013-06-12 20:52:10 +030023
24#include "core.h"
25#include "debug.h"
26
27#include "targaddrs.h"
28#include "bmi.h"
29
30#include "hif.h"
31#include "htc.h"
32
33#include "ce.h"
34#include "pci.h"
35
Michal Kaziorcfe9c452013-11-25 14:06:27 +010036enum ath10k_pci_irq_mode {
37 ATH10K_PCI_IRQ_AUTO = 0,
38 ATH10K_PCI_IRQ_LEGACY = 1,
39 ATH10K_PCI_IRQ_MSI = 2,
40};
41
Kalle Valo35098462014-03-28 09:32:27 +020042enum ath10k_pci_reset_mode {
43 ATH10K_PCI_RESET_AUTO = 0,
44 ATH10K_PCI_RESET_WARM_ONLY = 1,
45};
46
Kalle Valoe42c1fb2014-03-28 09:32:33 +020047static unsigned int ath10k_pci_target_ps;
Michal Kaziorcfe9c452013-11-25 14:06:27 +010048static unsigned int ath10k_pci_irq_mode = ATH10K_PCI_IRQ_AUTO;
Kalle Valo35098462014-03-28 09:32:27 +020049static unsigned int ath10k_pci_reset_mode = ATH10K_PCI_RESET_AUTO;
Michal Kaziorcfe9c452013-11-25 14:06:27 +010050
Kalle Valoe42c1fb2014-03-28 09:32:33 +020051module_param_named(target_ps, ath10k_pci_target_ps, uint, 0644);
52MODULE_PARM_DESC(target_ps, "Enable ath10k Target (SoC) PS option");
Kalle Valo5e3dd152013-06-12 20:52:10 +030053
Michal Kaziorcfe9c452013-11-25 14:06:27 +010054module_param_named(irq_mode, ath10k_pci_irq_mode, uint, 0644);
55MODULE_PARM_DESC(irq_mode, "0: auto, 1: legacy, 2: msi (default: 0)");
56
Kalle Valo35098462014-03-28 09:32:27 +020057module_param_named(reset_mode, ath10k_pci_reset_mode, uint, 0644);
58MODULE_PARM_DESC(reset_mode, "0: auto, 1: warm only (default: 0)");
59
Kalle Valo0399eca2014-03-28 09:32:21 +020060/* how long wait to wait for target to initialise, in ms */
61#define ATH10K_PCI_TARGET_WAIT 3000
Michal Kazior61c95ce2014-05-14 16:56:16 +030062#define ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS 3
Kalle Valo0399eca2014-03-28 09:32:21 +020063
Kalle Valo5e3dd152013-06-12 20:52:10 +030064#define QCA988X_2_0_DEVICE_ID (0x003c)
65
66static DEFINE_PCI_DEVICE_TABLE(ath10k_pci_id_table) = {
Kalle Valo5e3dd152013-06-12 20:52:10 +030067 { PCI_VDEVICE(ATHEROS, QCA988X_2_0_DEVICE_ID) }, /* PCI-E QCA988X V2 */
68 {0}
69};
70
71static int ath10k_pci_diag_read_access(struct ath10k *ar, u32 address,
72 u32 *data);
73
Kalle Valo5e3dd152013-06-12 20:52:10 +030074static int ath10k_pci_post_rx(struct ath10k *ar);
Michal Kazior87263e52013-08-27 13:08:01 +020075static int ath10k_pci_post_rx_pipe(struct ath10k_pci_pipe *pipe_info,
Kalle Valo5e3dd152013-06-12 20:52:10 +030076 int num);
Michal Kazior87263e52013-08-27 13:08:01 +020077static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe *pipe_info);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +010078static int ath10k_pci_cold_reset(struct ath10k *ar);
79static int ath10k_pci_warm_reset(struct ath10k *ar);
Michal Kaziord7fb47f2013-11-08 08:01:26 +010080static int ath10k_pci_wait_for_target_init(struct ath10k *ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +010081static int ath10k_pci_init_irq(struct ath10k *ar);
82static int ath10k_pci_deinit_irq(struct ath10k *ar);
83static int ath10k_pci_request_irq(struct ath10k *ar);
84static void ath10k_pci_free_irq(struct ath10k *ar);
Michal Kazior85622cd2013-11-25 14:06:22 +010085static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
86 struct ath10k_ce_pipe *rx_pipe,
87 struct bmi_xfer *xfer);
Kalle Valo5e3dd152013-06-12 20:52:10 +030088
89static const struct ce_attr host_ce_config_wlan[] = {
Kalle Valo48e9c222013-09-01 10:01:32 +030090 /* CE0: host->target HTC control and raw streams */
91 {
92 .flags = CE_ATTR_FLAGS,
93 .src_nentries = 16,
94 .src_sz_max = 256,
95 .dest_nentries = 0,
96 },
97
98 /* CE1: target->host HTT + HTC control */
99 {
100 .flags = CE_ATTR_FLAGS,
101 .src_nentries = 0,
102 .src_sz_max = 512,
103 .dest_nentries = 512,
104 },
105
106 /* CE2: target->host WMI */
107 {
108 .flags = CE_ATTR_FLAGS,
109 .src_nentries = 0,
110 .src_sz_max = 2048,
111 .dest_nentries = 32,
112 },
113
114 /* CE3: host->target WMI */
115 {
116 .flags = CE_ATTR_FLAGS,
117 .src_nentries = 32,
118 .src_sz_max = 2048,
119 .dest_nentries = 0,
120 },
121
122 /* CE4: host->target HTT */
123 {
124 .flags = CE_ATTR_FLAGS | CE_ATTR_DIS_INTR,
125 .src_nentries = CE_HTT_H2T_MSG_SRC_NENTRIES,
126 .src_sz_max = 256,
127 .dest_nentries = 0,
128 },
129
130 /* CE5: unused */
131 {
132 .flags = CE_ATTR_FLAGS,
133 .src_nentries = 0,
134 .src_sz_max = 0,
135 .dest_nentries = 0,
136 },
137
138 /* CE6: target autonomous hif_memcpy */
139 {
140 .flags = CE_ATTR_FLAGS,
141 .src_nentries = 0,
142 .src_sz_max = 0,
143 .dest_nentries = 0,
144 },
145
146 /* CE7: ce_diag, the Diagnostic Window */
147 {
148 .flags = CE_ATTR_FLAGS,
149 .src_nentries = 2,
150 .src_sz_max = DIAG_TRANSFER_LIMIT,
151 .dest_nentries = 2,
152 },
Kalle Valo5e3dd152013-06-12 20:52:10 +0300153};
154
155/* Target firmware's Copy Engine configuration. */
156static const struct ce_pipe_config target_ce_config_wlan[] = {
Kalle Valod88effb2013-09-01 10:01:39 +0300157 /* CE0: host->target HTC control and raw streams */
158 {
159 .pipenum = 0,
160 .pipedir = PIPEDIR_OUT,
161 .nentries = 32,
162 .nbytes_max = 256,
163 .flags = CE_ATTR_FLAGS,
164 .reserved = 0,
165 },
166
167 /* CE1: target->host HTT + HTC control */
168 {
169 .pipenum = 1,
170 .pipedir = PIPEDIR_IN,
171 .nentries = 32,
172 .nbytes_max = 512,
173 .flags = CE_ATTR_FLAGS,
174 .reserved = 0,
175 },
176
177 /* CE2: target->host WMI */
178 {
179 .pipenum = 2,
180 .pipedir = PIPEDIR_IN,
181 .nentries = 32,
182 .nbytes_max = 2048,
183 .flags = CE_ATTR_FLAGS,
184 .reserved = 0,
185 },
186
187 /* CE3: host->target WMI */
188 {
189 .pipenum = 3,
190 .pipedir = PIPEDIR_OUT,
191 .nentries = 32,
192 .nbytes_max = 2048,
193 .flags = CE_ATTR_FLAGS,
194 .reserved = 0,
195 },
196
197 /* CE4: host->target HTT */
198 {
199 .pipenum = 4,
200 .pipedir = PIPEDIR_OUT,
201 .nentries = 256,
202 .nbytes_max = 256,
203 .flags = CE_ATTR_FLAGS,
204 .reserved = 0,
205 },
206
Kalle Valo5e3dd152013-06-12 20:52:10 +0300207 /* NB: 50% of src nentries, since tx has 2 frags */
Kalle Valod88effb2013-09-01 10:01:39 +0300208
209 /* CE5: unused */
210 {
211 .pipenum = 5,
212 .pipedir = PIPEDIR_OUT,
213 .nentries = 32,
214 .nbytes_max = 2048,
215 .flags = CE_ATTR_FLAGS,
216 .reserved = 0,
217 },
218
219 /* CE6: Reserved for target autonomous hif_memcpy */
220 {
221 .pipenum = 6,
222 .pipedir = PIPEDIR_INOUT,
223 .nentries = 32,
224 .nbytes_max = 4096,
225 .flags = CE_ATTR_FLAGS,
226 .reserved = 0,
227 },
228
Kalle Valo5e3dd152013-06-12 20:52:10 +0300229 /* CE7 used only by Host */
230};
231
Michal Kaziore5398872013-11-25 14:06:20 +0100232static bool ath10k_pci_irq_pending(struct ath10k *ar)
233{
234 u32 cause;
235
236 /* Check if the shared legacy irq is for us */
237 cause = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
238 PCIE_INTR_CAUSE_ADDRESS);
239 if (cause & (PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL))
240 return true;
241
242 return false;
243}
244
Michal Kazior26852182013-11-25 14:06:25 +0100245static void ath10k_pci_disable_and_clear_legacy_irq(struct ath10k *ar)
246{
247 /* IMPORTANT: INTR_CLR register has to be set after
248 * INTR_ENABLE is set to 0, otherwise interrupt can not be
249 * really cleared. */
250 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
251 0);
252 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_CLR_ADDRESS,
253 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
254
255 /* IMPORTANT: this extra read transaction is required to
256 * flush the posted write buffer. */
257 (void) ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
258 PCIE_INTR_ENABLE_ADDRESS);
259}
260
261static void ath10k_pci_enable_legacy_irq(struct ath10k *ar)
262{
263 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
264 PCIE_INTR_ENABLE_ADDRESS,
265 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
266
267 /* IMPORTANT: this extra read transaction is required to
268 * flush the posted write buffer. */
269 (void) ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
270 PCIE_INTR_ENABLE_ADDRESS);
271}
272
Michal Kaziorab977bd2013-11-25 14:06:26 +0100273static irqreturn_t ath10k_pci_early_irq_handler(int irq, void *arg)
274{
275 struct ath10k *ar = arg;
276 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
277
278 if (ar_pci->num_msi_intrs == 0) {
279 if (!ath10k_pci_irq_pending(ar))
280 return IRQ_NONE;
281
282 ath10k_pci_disable_and_clear_legacy_irq(ar);
283 }
284
285 tasklet_schedule(&ar_pci->early_irq_tasklet);
286
287 return IRQ_HANDLED;
288}
289
290static int ath10k_pci_request_early_irq(struct ath10k *ar)
291{
292 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
293 int ret;
294
295 /* Regardless whether MSI-X/MSI/legacy irqs have been set up the first
296 * interrupt from irq vector is triggered in all cases for FW
297 * indication/errors */
298 ret = request_irq(ar_pci->pdev->irq, ath10k_pci_early_irq_handler,
299 IRQF_SHARED, "ath10k_pci (early)", ar);
300 if (ret) {
301 ath10k_warn("failed to request early irq: %d\n", ret);
302 return ret;
303 }
304
305 return 0;
306}
307
308static void ath10k_pci_free_early_irq(struct ath10k *ar)
309{
310 free_irq(ath10k_pci_priv(ar)->pdev->irq, ar);
311}
312
Kalle Valo5e3dd152013-06-12 20:52:10 +0300313/*
314 * Diagnostic read/write access is provided for startup/config/debug usage.
315 * Caller must guarantee proper alignment, when applicable, and single user
316 * at any moment.
317 */
318static int ath10k_pci_diag_read_mem(struct ath10k *ar, u32 address, void *data,
319 int nbytes)
320{
321 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
322 int ret = 0;
323 u32 buf;
324 unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
325 unsigned int id;
326 unsigned int flags;
Michal Kazior2aa39112013-08-27 13:08:02 +0200327 struct ath10k_ce_pipe *ce_diag;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300328 /* Host buffer address in CE space */
329 u32 ce_data;
330 dma_addr_t ce_data_base = 0;
331 void *data_buf = NULL;
332 int i;
333
334 /*
335 * This code cannot handle reads to non-memory space. Redirect to the
336 * register read fn but preserve the multi word read capability of
337 * this fn
338 */
339 if (address < DRAM_BASE_ADDRESS) {
340 if (!IS_ALIGNED(address, 4) ||
341 !IS_ALIGNED((unsigned long)data, 4))
342 return -EIO;
343
344 while ((nbytes >= 4) && ((ret = ath10k_pci_diag_read_access(
345 ar, address, (u32 *)data)) == 0)) {
346 nbytes -= sizeof(u32);
347 address += sizeof(u32);
348 data += sizeof(u32);
349 }
350 return ret;
351 }
352
353 ce_diag = ar_pci->ce_diag;
354
355 /*
356 * Allocate a temporary bounce buffer to hold caller's data
357 * to be DMA'ed from Target. This guarantees
358 * 1) 4-byte alignment
359 * 2) Buffer in DMA-able space
360 */
361 orig_nbytes = nbytes;
Michal Kazior68c03242014-03-28 10:02:35 +0200362 data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
363 orig_nbytes,
364 &ce_data_base,
365 GFP_ATOMIC);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300366
367 if (!data_buf) {
368 ret = -ENOMEM;
369 goto done;
370 }
371 memset(data_buf, 0, orig_nbytes);
372
373 remaining_bytes = orig_nbytes;
374 ce_data = ce_data_base;
375 while (remaining_bytes) {
376 nbytes = min_t(unsigned int, remaining_bytes,
377 DIAG_TRANSFER_LIMIT);
378
379 ret = ath10k_ce_recv_buf_enqueue(ce_diag, NULL, ce_data);
380 if (ret != 0)
381 goto done;
382
383 /* Request CE to send from Target(!) address to Host buffer */
384 /*
385 * The address supplied by the caller is in the
386 * Target CPU virtual address space.
387 *
388 * In order to use this address with the diagnostic CE,
389 * convert it from Target CPU virtual address space
390 * to CE address space
391 */
392 ath10k_pci_wake(ar);
393 address = TARG_CPU_SPACE_TO_CE_SPACE(ar, ar_pci->mem,
394 address);
395 ath10k_pci_sleep(ar);
396
397 ret = ath10k_ce_send(ce_diag, NULL, (u32)address, nbytes, 0,
398 0);
399 if (ret)
400 goto done;
401
402 i = 0;
403 while (ath10k_ce_completed_send_next(ce_diag, NULL, &buf,
404 &completed_nbytes,
405 &id) != 0) {
406 mdelay(1);
407 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
408 ret = -EBUSY;
409 goto done;
410 }
411 }
412
413 if (nbytes != completed_nbytes) {
414 ret = -EIO;
415 goto done;
416 }
417
418 if (buf != (u32) address) {
419 ret = -EIO;
420 goto done;
421 }
422
423 i = 0;
424 while (ath10k_ce_completed_recv_next(ce_diag, NULL, &buf,
425 &completed_nbytes,
426 &id, &flags) != 0) {
427 mdelay(1);
428
429 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
430 ret = -EBUSY;
431 goto done;
432 }
433 }
434
435 if (nbytes != completed_nbytes) {
436 ret = -EIO;
437 goto done;
438 }
439
440 if (buf != ce_data) {
441 ret = -EIO;
442 goto done;
443 }
444
445 remaining_bytes -= nbytes;
446 address += nbytes;
447 ce_data += nbytes;
448 }
449
450done:
451 if (ret == 0) {
452 /* Copy data from allocated DMA buf to caller's buf */
453 WARN_ON_ONCE(orig_nbytes & 3);
454 for (i = 0; i < orig_nbytes / sizeof(__le32); i++) {
455 ((u32 *)data)[i] =
456 __le32_to_cpu(((__le32 *)data_buf)[i]);
457 }
458 } else
Kalle Valo50f87a62014-03-28 09:32:52 +0200459 ath10k_warn("failed to read diag value at 0x%x: %d\n",
460 address, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300461
462 if (data_buf)
Michal Kazior68c03242014-03-28 10:02:35 +0200463 dma_free_coherent(ar->dev, orig_nbytes, data_buf,
464 ce_data_base);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300465
466 return ret;
467}
468
469/* Read 4-byte aligned data from Target memory or register */
470static int ath10k_pci_diag_read_access(struct ath10k *ar, u32 address,
471 u32 *data)
472{
473 /* Assume range doesn't cross this boundary */
474 if (address >= DRAM_BASE_ADDRESS)
475 return ath10k_pci_diag_read_mem(ar, address, data, sizeof(u32));
476
477 ath10k_pci_wake(ar);
478 *data = ath10k_pci_read32(ar, address);
479 ath10k_pci_sleep(ar);
480 return 0;
481}
482
483static int ath10k_pci_diag_write_mem(struct ath10k *ar, u32 address,
484 const void *data, int nbytes)
485{
486 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
487 int ret = 0;
488 u32 buf;
489 unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
490 unsigned int id;
491 unsigned int flags;
Michal Kazior2aa39112013-08-27 13:08:02 +0200492 struct ath10k_ce_pipe *ce_diag;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300493 void *data_buf = NULL;
494 u32 ce_data; /* Host buffer address in CE space */
495 dma_addr_t ce_data_base = 0;
496 int i;
497
498 ce_diag = ar_pci->ce_diag;
499
500 /*
501 * Allocate a temporary bounce buffer to hold caller's data
502 * to be DMA'ed to Target. This guarantees
503 * 1) 4-byte alignment
504 * 2) Buffer in DMA-able space
505 */
506 orig_nbytes = nbytes;
Michal Kazior68c03242014-03-28 10:02:35 +0200507 data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
508 orig_nbytes,
509 &ce_data_base,
510 GFP_ATOMIC);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300511 if (!data_buf) {
512 ret = -ENOMEM;
513 goto done;
514 }
515
516 /* Copy caller's data to allocated DMA buf */
517 WARN_ON_ONCE(orig_nbytes & 3);
518 for (i = 0; i < orig_nbytes / sizeof(__le32); i++)
519 ((__le32 *)data_buf)[i] = __cpu_to_le32(((u32 *)data)[i]);
520
521 /*
522 * The address supplied by the caller is in the
523 * Target CPU virtual address space.
524 *
525 * In order to use this address with the diagnostic CE,
526 * convert it from
527 * Target CPU virtual address space
528 * to
529 * CE address space
530 */
531 ath10k_pci_wake(ar);
532 address = TARG_CPU_SPACE_TO_CE_SPACE(ar, ar_pci->mem, address);
533 ath10k_pci_sleep(ar);
534
535 remaining_bytes = orig_nbytes;
536 ce_data = ce_data_base;
537 while (remaining_bytes) {
538 /* FIXME: check cast */
539 nbytes = min_t(int, remaining_bytes, DIAG_TRANSFER_LIMIT);
540
541 /* Set up to receive directly into Target(!) address */
542 ret = ath10k_ce_recv_buf_enqueue(ce_diag, NULL, address);
543 if (ret != 0)
544 goto done;
545
546 /*
547 * Request CE to send caller-supplied data that
548 * was copied to bounce buffer to Target(!) address.
549 */
550 ret = ath10k_ce_send(ce_diag, NULL, (u32) ce_data,
551 nbytes, 0, 0);
552 if (ret != 0)
553 goto done;
554
555 i = 0;
556 while (ath10k_ce_completed_send_next(ce_diag, NULL, &buf,
557 &completed_nbytes,
558 &id) != 0) {
559 mdelay(1);
560
561 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
562 ret = -EBUSY;
563 goto done;
564 }
565 }
566
567 if (nbytes != completed_nbytes) {
568 ret = -EIO;
569 goto done;
570 }
571
572 if (buf != ce_data) {
573 ret = -EIO;
574 goto done;
575 }
576
577 i = 0;
578 while (ath10k_ce_completed_recv_next(ce_diag, NULL, &buf,
579 &completed_nbytes,
580 &id, &flags) != 0) {
581 mdelay(1);
582
583 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
584 ret = -EBUSY;
585 goto done;
586 }
587 }
588
589 if (nbytes != completed_nbytes) {
590 ret = -EIO;
591 goto done;
592 }
593
594 if (buf != address) {
595 ret = -EIO;
596 goto done;
597 }
598
599 remaining_bytes -= nbytes;
600 address += nbytes;
601 ce_data += nbytes;
602 }
603
604done:
605 if (data_buf) {
Michal Kazior68c03242014-03-28 10:02:35 +0200606 dma_free_coherent(ar->dev, orig_nbytes, data_buf,
607 ce_data_base);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300608 }
609
610 if (ret != 0)
Kalle Valo50f87a62014-03-28 09:32:52 +0200611 ath10k_warn("failed to write diag value at 0x%x: %d\n",
612 address, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300613
614 return ret;
615}
616
617/* Write 4B data to Target memory or register */
618static int ath10k_pci_diag_write_access(struct ath10k *ar, u32 address,
619 u32 data)
620{
621 /* Assume range doesn't cross this boundary */
622 if (address >= DRAM_BASE_ADDRESS)
623 return ath10k_pci_diag_write_mem(ar, address, &data,
624 sizeof(u32));
625
626 ath10k_pci_wake(ar);
627 ath10k_pci_write32(ar, address, data);
628 ath10k_pci_sleep(ar);
629 return 0;
630}
631
632static bool ath10k_pci_target_is_awake(struct ath10k *ar)
633{
634 void __iomem *mem = ath10k_pci_priv(ar)->mem;
635 u32 val;
636 val = ioread32(mem + PCIE_LOCAL_BASE_ADDRESS +
637 RTC_STATE_ADDRESS);
638 return (RTC_STATE_V_GET(val) == RTC_STATE_V_ON);
639}
640
Kalle Valo3aebe542013-09-01 10:02:07 +0300641int ath10k_do_pci_wake(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300642{
643 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
644 void __iomem *pci_addr = ar_pci->mem;
645 int tot_delay = 0;
646 int curr_delay = 5;
647
648 if (atomic_read(&ar_pci->keep_awake_count) == 0) {
649 /* Force AWAKE */
650 iowrite32(PCIE_SOC_WAKE_V_MASK,
651 pci_addr + PCIE_LOCAL_BASE_ADDRESS +
652 PCIE_SOC_WAKE_ADDRESS);
653 }
654 atomic_inc(&ar_pci->keep_awake_count);
655
656 if (ar_pci->verified_awake)
Kalle Valo3aebe542013-09-01 10:02:07 +0300657 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300658
659 for (;;) {
660 if (ath10k_pci_target_is_awake(ar)) {
661 ar_pci->verified_awake = true;
Kalle Valo3aebe542013-09-01 10:02:07 +0300662 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300663 }
664
665 if (tot_delay > PCIE_WAKE_TIMEOUT) {
Kalle Valo3aebe542013-09-01 10:02:07 +0300666 ath10k_warn("target took longer %d us to wake up (awake count %d)\n",
667 PCIE_WAKE_TIMEOUT,
Kalle Valo5e3dd152013-06-12 20:52:10 +0300668 atomic_read(&ar_pci->keep_awake_count));
Kalle Valo3aebe542013-09-01 10:02:07 +0300669 return -ETIMEDOUT;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300670 }
671
672 udelay(curr_delay);
673 tot_delay += curr_delay;
674
675 if (curr_delay < 50)
676 curr_delay += 5;
677 }
678}
679
680void ath10k_do_pci_sleep(struct ath10k *ar)
681{
682 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
683 void __iomem *pci_addr = ar_pci->mem;
684
685 if (atomic_dec_and_test(&ar_pci->keep_awake_count)) {
686 /* Allow sleep */
687 ar_pci->verified_awake = false;
688 iowrite32(PCIE_SOC_WAKE_RESET,
689 pci_addr + PCIE_LOCAL_BASE_ADDRESS +
690 PCIE_SOC_WAKE_ADDRESS);
691 }
692}
693
Kalle Valo5e3dd152013-06-12 20:52:10 +0300694/* Called by lower (CE) layer when a send to Target completes. */
Michal Kazior5440ce22013-09-03 15:09:58 +0200695static void ath10k_pci_ce_send_done(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300696{
697 struct ath10k *ar = ce_state->ar;
698 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2f5280d2014-02-27 18:50:05 +0200699 struct ath10k_hif_cb *cb = &ar_pci->msg_callbacks_current;
Michal Kazior5440ce22013-09-03 15:09:58 +0200700 void *transfer_context;
701 u32 ce_data;
702 unsigned int nbytes;
703 unsigned int transfer_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300704
Michal Kazior5440ce22013-09-03 15:09:58 +0200705 while (ath10k_ce_completed_send_next(ce_state, &transfer_context,
706 &ce_data, &nbytes,
707 &transfer_id) == 0) {
Michal Kaziora16942e2014-02-27 18:50:04 +0200708 /* no need to call tx completion for NULL pointers */
Michal Kazior726346f2014-02-27 18:50:04 +0200709 if (transfer_context == NULL)
710 continue;
711
Michal Kazior2f5280d2014-02-27 18:50:05 +0200712 cb->tx_completion(ar, transfer_context, transfer_id);
Michal Kazior5440ce22013-09-03 15:09:58 +0200713 }
Kalle Valo5e3dd152013-06-12 20:52:10 +0300714}
715
716/* Called by lower (CE) layer when data is received from the Target. */
Michal Kazior5440ce22013-09-03 15:09:58 +0200717static void ath10k_pci_ce_recv_data(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300718{
719 struct ath10k *ar = ce_state->ar;
720 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +0200721 struct ath10k_pci_pipe *pipe_info = &ar_pci->pipe_info[ce_state->id];
Michal Kazior2f5280d2014-02-27 18:50:05 +0200722 struct ath10k_hif_cb *cb = &ar_pci->msg_callbacks_current;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300723 struct sk_buff *skb;
Michal Kazior5440ce22013-09-03 15:09:58 +0200724 void *transfer_context;
725 u32 ce_data;
Michal Kazior2f5280d2014-02-27 18:50:05 +0200726 unsigned int nbytes, max_nbytes;
Michal Kazior5440ce22013-09-03 15:09:58 +0200727 unsigned int transfer_id;
728 unsigned int flags;
Michal Kazior2f5280d2014-02-27 18:50:05 +0200729 int err;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300730
Michal Kazior5440ce22013-09-03 15:09:58 +0200731 while (ath10k_ce_completed_recv_next(ce_state, &transfer_context,
732 &ce_data, &nbytes, &transfer_id,
733 &flags) == 0) {
Michal Kazior2f5280d2014-02-27 18:50:05 +0200734 err = ath10k_pci_post_rx_pipe(pipe_info, 1);
735 if (unlikely(err)) {
736 /* FIXME: retry */
737 ath10k_warn("failed to replenish CE rx ring %d: %d\n",
738 pipe_info->pipe_num, err);
739 }
Kalle Valo5e3dd152013-06-12 20:52:10 +0300740
741 skb = transfer_context;
Michal Kazior2f5280d2014-02-27 18:50:05 +0200742 max_nbytes = skb->len + skb_tailroom(skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300743 dma_unmap_single(ar->dev, ATH10K_SKB_CB(skb)->paddr,
Michal Kazior2f5280d2014-02-27 18:50:05 +0200744 max_nbytes, DMA_FROM_DEVICE);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300745
Michal Kazior2f5280d2014-02-27 18:50:05 +0200746 if (unlikely(max_nbytes < nbytes)) {
747 ath10k_warn("rxed more than expected (nbytes %d, max %d)",
748 nbytes, max_nbytes);
749 dev_kfree_skb_any(skb);
750 continue;
751 }
752
753 skb_put(skb, nbytes);
754 cb->rx_completion(ar, skb, pipe_info->pipe_num);
755 }
Kalle Valo5e3dd152013-06-12 20:52:10 +0300756}
757
Michal Kazior726346f2014-02-27 18:50:04 +0200758static int ath10k_pci_hif_tx_sg(struct ath10k *ar, u8 pipe_id,
759 struct ath10k_hif_sg_item *items, int n_items)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300760{
Kalle Valo5e3dd152013-06-12 20:52:10 +0300761 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior726346f2014-02-27 18:50:04 +0200762 struct ath10k_pci_pipe *pci_pipe = &ar_pci->pipe_info[pipe_id];
763 struct ath10k_ce_pipe *ce_pipe = pci_pipe->ce_hdl;
764 struct ath10k_ce_ring *src_ring = ce_pipe->src_ring;
Michal Kazior7147a132014-05-26 12:02:58 +0200765 unsigned int nentries_mask;
766 unsigned int sw_index;
767 unsigned int write_index;
Michal Kazior726346f2014-02-27 18:50:04 +0200768 int err, i;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300769
Michal Kazior726346f2014-02-27 18:50:04 +0200770 spin_lock_bh(&ar_pci->ce_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300771
Michal Kazior7147a132014-05-26 12:02:58 +0200772 nentries_mask = src_ring->nentries_mask;
773 sw_index = src_ring->sw_index;
774 write_index = src_ring->write_index;
775
Michal Kazior726346f2014-02-27 18:50:04 +0200776 if (unlikely(CE_RING_DELTA(nentries_mask,
777 write_index, sw_index - 1) < n_items)) {
778 err = -ENOBUFS;
779 goto unlock;
780 }
781
782 for (i = 0; i < n_items - 1; i++) {
783 ath10k_dbg(ATH10K_DBG_PCI,
784 "pci tx item %d paddr 0x%08x len %d n_items %d\n",
785 i, items[i].paddr, items[i].len, n_items);
786 ath10k_dbg_dump(ATH10K_DBG_PCI_DUMP, NULL, "item data: ",
787 items[i].vaddr, items[i].len);
788
789 err = ath10k_ce_send_nolock(ce_pipe,
790 items[i].transfer_context,
791 items[i].paddr,
792 items[i].len,
793 items[i].transfer_id,
794 CE_SEND_FLAG_GATHER);
795 if (err)
796 goto unlock;
797 }
798
799 /* `i` is equal to `n_items -1` after for() */
Kalle Valo5e3dd152013-06-12 20:52:10 +0300800
801 ath10k_dbg(ATH10K_DBG_PCI,
Michal Kazior726346f2014-02-27 18:50:04 +0200802 "pci tx item %d paddr 0x%08x len %d n_items %d\n",
803 i, items[i].paddr, items[i].len, n_items);
804 ath10k_dbg_dump(ATH10K_DBG_PCI_DUMP, NULL, "item data: ",
805 items[i].vaddr, items[i].len);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300806
Michal Kazior726346f2014-02-27 18:50:04 +0200807 err = ath10k_ce_send_nolock(ce_pipe,
808 items[i].transfer_context,
809 items[i].paddr,
810 items[i].len,
811 items[i].transfer_id,
812 0);
813 if (err)
814 goto unlock;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300815
Michal Kazior726346f2014-02-27 18:50:04 +0200816 err = 0;
817unlock:
818 spin_unlock_bh(&ar_pci->ce_lock);
819 return err;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300820}
821
822static u16 ath10k_pci_hif_get_free_queue_number(struct ath10k *ar, u8 pipe)
823{
824 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo50f87a62014-03-28 09:32:52 +0200825
826 ath10k_dbg(ATH10K_DBG_PCI, "pci hif get free queue number\n");
827
Michal Kazior3efcb3b2013-10-02 11:03:41 +0200828 return ath10k_ce_num_free_src_entries(ar_pci->pipe_info[pipe].ce_hdl);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300829}
830
831static void ath10k_pci_hif_dump_area(struct ath10k *ar)
832{
833 u32 reg_dump_area = 0;
834 u32 reg_dump_values[REG_DUMP_COUNT_QCA988X] = {};
835 u32 host_addr;
836 int ret;
837 u32 i;
838
839 ath10k_err("firmware crashed!\n");
840 ath10k_err("hardware name %s version 0x%x\n",
841 ar->hw_params.name, ar->target_version);
Chun-Yeow Yeoh5ba88b32014-01-21 17:21:21 +0800842 ath10k_err("firmware version: %s\n", ar->hw->wiphy->fw_version);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300843
844 host_addr = host_interest_item_address(HI_ITEM(hi_failure_state));
Michal Kazior1d2b48d2013-11-08 08:01:34 +0100845 ret = ath10k_pci_diag_read_mem(ar, host_addr,
846 &reg_dump_area, sizeof(u32));
847 if (ret) {
848 ath10k_err("failed to read FW dump area address: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300849 return;
850 }
851
852 ath10k_err("target register Dump Location: 0x%08X\n", reg_dump_area);
853
854 ret = ath10k_pci_diag_read_mem(ar, reg_dump_area,
855 &reg_dump_values[0],
856 REG_DUMP_COUNT_QCA988X * sizeof(u32));
857 if (ret != 0) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +0100858 ath10k_err("failed to read FW dump area: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300859 return;
860 }
861
862 BUILD_BUG_ON(REG_DUMP_COUNT_QCA988X % 4);
863
864 ath10k_err("target Register Dump\n");
865 for (i = 0; i < REG_DUMP_COUNT_QCA988X; i += 4)
866 ath10k_err("[%02d]: 0x%08X 0x%08X 0x%08X 0x%08X\n",
867 i,
868 reg_dump_values[i],
869 reg_dump_values[i + 1],
870 reg_dump_values[i + 2],
871 reg_dump_values[i + 3]);
Michal Kazioraffd3212013-07-16 09:54:35 +0200872
Michal Kazior5e90de82013-10-16 16:46:05 +0300873 queue_work(ar->workqueue, &ar->restart_work);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300874}
875
876static void ath10k_pci_hif_send_complete_check(struct ath10k *ar, u8 pipe,
877 int force)
878{
Kalle Valo50f87a62014-03-28 09:32:52 +0200879 ath10k_dbg(ATH10K_DBG_PCI, "pci hif send complete check\n");
880
Kalle Valo5e3dd152013-06-12 20:52:10 +0300881 if (!force) {
882 int resources;
883 /*
884 * Decide whether to actually poll for completions, or just
885 * wait for a later chance.
886 * If there seem to be plenty of resources left, then just wait
887 * since checking involves reading a CE register, which is a
888 * relatively expensive operation.
889 */
890 resources = ath10k_pci_hif_get_free_queue_number(ar, pipe);
891
892 /*
893 * If at least 50% of the total resources are still available,
894 * don't bother checking again yet.
895 */
896 if (resources > (host_ce_config_wlan[pipe].src_nentries >> 1))
897 return;
898 }
899 ath10k_ce_per_engine_service(ar, pipe);
900}
901
Michal Kaziore799bbf2013-07-05 16:15:12 +0300902static void ath10k_pci_hif_set_callbacks(struct ath10k *ar,
903 struct ath10k_hif_cb *callbacks)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300904{
905 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
906
Kalle Valo50f87a62014-03-28 09:32:52 +0200907 ath10k_dbg(ATH10K_DBG_PCI, "pci hif set callbacks\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +0300908
909 memcpy(&ar_pci->msg_callbacks_current, callbacks,
910 sizeof(ar_pci->msg_callbacks_current));
911}
912
Michal Kaziorc80de122013-11-25 14:06:23 +0100913static int ath10k_pci_setup_ce_irq(struct ath10k *ar)
914{
915 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
916 const struct ce_attr *attr;
917 struct ath10k_pci_pipe *pipe_info;
918 int pipe_num, disable_interrupts;
919
920 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
921 pipe_info = &ar_pci->pipe_info[pipe_num];
922
923 /* Handle Diagnostic CE specially */
924 if (pipe_info->ce_hdl == ar_pci->ce_diag)
925 continue;
926
927 attr = &host_ce_config_wlan[pipe_num];
928
929 if (attr->src_nentries) {
930 disable_interrupts = attr->flags & CE_ATTR_DIS_INTR;
931 ath10k_ce_send_cb_register(pipe_info->ce_hdl,
932 ath10k_pci_ce_send_done,
933 disable_interrupts);
934 }
935
936 if (attr->dest_nentries)
937 ath10k_ce_recv_cb_register(pipe_info->ce_hdl,
938 ath10k_pci_ce_recv_data);
939 }
940
941 return 0;
942}
943
Michal Kazior96a9d0d2013-11-08 08:01:25 +0100944static void ath10k_pci_kill_tasklet(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300945{
946 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300947 int i;
948
Kalle Valo5e3dd152013-06-12 20:52:10 +0300949 tasklet_kill(&ar_pci->intr_tq);
Michal Kazior103d4f52013-11-08 08:01:24 +0100950 tasklet_kill(&ar_pci->msi_fw_err);
Michal Kaziorab977bd2013-11-25 14:06:26 +0100951 tasklet_kill(&ar_pci->early_irq_tasklet);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300952
953 for (i = 0; i < CE_COUNT; i++)
954 tasklet_kill(&ar_pci->pipe_info[i].intr);
Michal Kazior96a9d0d2013-11-08 08:01:25 +0100955}
956
Kalle Valo5e3dd152013-06-12 20:52:10 +0300957/* TODO - temporary mapping while we have too few CE's */
958static int ath10k_pci_hif_map_service_to_pipe(struct ath10k *ar,
959 u16 service_id, u8 *ul_pipe,
960 u8 *dl_pipe, int *ul_is_polled,
961 int *dl_is_polled)
962{
963 int ret = 0;
964
Kalle Valo50f87a62014-03-28 09:32:52 +0200965 ath10k_dbg(ATH10K_DBG_PCI, "pci hif map service\n");
966
Kalle Valo5e3dd152013-06-12 20:52:10 +0300967 /* polling for received messages not supported */
968 *dl_is_polled = 0;
969
970 switch (service_id) {
971 case ATH10K_HTC_SVC_ID_HTT_DATA_MSG:
972 /*
973 * Host->target HTT gets its own pipe, so it can be polled
974 * while other pipes are interrupt driven.
975 */
976 *ul_pipe = 4;
977 /*
978 * Use the same target->host pipe for HTC ctrl, HTC raw
979 * streams, and HTT.
980 */
981 *dl_pipe = 1;
982 break;
983
984 case ATH10K_HTC_SVC_ID_RSVD_CTRL:
985 case ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS:
986 /*
987 * Note: HTC_RAW_STREAMS_SVC is currently unused, and
988 * HTC_CTRL_RSVD_SVC could share the same pipe as the
989 * WMI services. So, if another CE is needed, change
990 * this to *ul_pipe = 3, which frees up CE 0.
991 */
992 /* *ul_pipe = 3; */
993 *ul_pipe = 0;
994 *dl_pipe = 1;
995 break;
996
997 case ATH10K_HTC_SVC_ID_WMI_DATA_BK:
998 case ATH10K_HTC_SVC_ID_WMI_DATA_BE:
999 case ATH10K_HTC_SVC_ID_WMI_DATA_VI:
1000 case ATH10K_HTC_SVC_ID_WMI_DATA_VO:
1001
1002 case ATH10K_HTC_SVC_ID_WMI_CONTROL:
1003 *ul_pipe = 3;
1004 *dl_pipe = 2;
1005 break;
1006
1007 /* pipe 5 unused */
1008 /* pipe 6 reserved */
1009 /* pipe 7 reserved */
1010
1011 default:
1012 ret = -1;
1013 break;
1014 }
1015 *ul_is_polled =
1016 (host_ce_config_wlan[*ul_pipe].flags & CE_ATTR_DIS_INTR) != 0;
1017
1018 return ret;
1019}
1020
1021static void ath10k_pci_hif_get_default_pipe(struct ath10k *ar,
1022 u8 *ul_pipe, u8 *dl_pipe)
1023{
1024 int ul_is_polled, dl_is_polled;
1025
Kalle Valo50f87a62014-03-28 09:32:52 +02001026 ath10k_dbg(ATH10K_DBG_PCI, "pci hif get default pipe\n");
1027
Kalle Valo5e3dd152013-06-12 20:52:10 +03001028 (void)ath10k_pci_hif_map_service_to_pipe(ar,
1029 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1030 ul_pipe,
1031 dl_pipe,
1032 &ul_is_polled,
1033 &dl_is_polled);
1034}
1035
Michal Kazior87263e52013-08-27 13:08:01 +02001036static int ath10k_pci_post_rx_pipe(struct ath10k_pci_pipe *pipe_info,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001037 int num)
1038{
1039 struct ath10k *ar = pipe_info->hif_ce_state;
1040 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2aa39112013-08-27 13:08:02 +02001041 struct ath10k_ce_pipe *ce_state = pipe_info->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001042 struct sk_buff *skb;
1043 dma_addr_t ce_data;
1044 int i, ret = 0;
1045
1046 if (pipe_info->buf_sz == 0)
1047 return 0;
1048
1049 for (i = 0; i < num; i++) {
1050 skb = dev_alloc_skb(pipe_info->buf_sz);
1051 if (!skb) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001052 ath10k_warn("failed to allocate skbuff for pipe %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001053 num);
1054 ret = -ENOMEM;
1055 goto err;
1056 }
1057
1058 WARN_ONCE((unsigned long)skb->data & 3, "unaligned skb");
1059
1060 ce_data = dma_map_single(ar->dev, skb->data,
1061 skb->len + skb_tailroom(skb),
1062 DMA_FROM_DEVICE);
1063
1064 if (unlikely(dma_mapping_error(ar->dev, ce_data))) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001065 ath10k_warn("failed to DMA map sk_buff\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001066 dev_kfree_skb_any(skb);
1067 ret = -EIO;
1068 goto err;
1069 }
1070
1071 ATH10K_SKB_CB(skb)->paddr = ce_data;
1072
1073 pci_dma_sync_single_for_device(ar_pci->pdev, ce_data,
1074 pipe_info->buf_sz,
1075 PCI_DMA_FROMDEVICE);
1076
1077 ret = ath10k_ce_recv_buf_enqueue(ce_state, (void *)skb,
1078 ce_data);
1079 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001080 ath10k_warn("failed to enqueue to pipe %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001081 num, ret);
1082 goto err;
1083 }
1084 }
1085
1086 return ret;
1087
1088err:
1089 ath10k_pci_rx_pipe_cleanup(pipe_info);
1090 return ret;
1091}
1092
1093static int ath10k_pci_post_rx(struct ath10k *ar)
1094{
1095 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +02001096 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001097 const struct ce_attr *attr;
1098 int pipe_num, ret = 0;
1099
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001100 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001101 pipe_info = &ar_pci->pipe_info[pipe_num];
1102 attr = &host_ce_config_wlan[pipe_num];
1103
1104 if (attr->dest_nentries == 0)
1105 continue;
1106
1107 ret = ath10k_pci_post_rx_pipe(pipe_info,
1108 attr->dest_nentries - 1);
1109 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001110 ath10k_warn("failed to post RX buffer for pipe %d: %d\n",
1111 pipe_num, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001112
1113 for (; pipe_num >= 0; pipe_num--) {
1114 pipe_info = &ar_pci->pipe_info[pipe_num];
1115 ath10k_pci_rx_pipe_cleanup(pipe_info);
1116 }
1117 return ret;
1118 }
1119 }
1120
1121 return 0;
1122}
1123
1124static int ath10k_pci_hif_start(struct ath10k *ar)
1125{
1126 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorab977bd2013-11-25 14:06:26 +01001127 int ret, ret_early;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001128
Kalle Valo50f87a62014-03-28 09:32:52 +02001129 ath10k_dbg(ATH10K_DBG_BOOT, "boot hif start\n");
1130
Michal Kaziorab977bd2013-11-25 14:06:26 +01001131 ath10k_pci_free_early_irq(ar);
1132 ath10k_pci_kill_tasklet(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001133
Michal Kazior5d1aa942013-11-25 14:06:24 +01001134 ret = ath10k_pci_request_irq(ar);
1135 if (ret) {
1136 ath10k_warn("failed to post RX buffers for all pipes: %d\n",
1137 ret);
Michal Kazior2f5280d2014-02-27 18:50:05 +02001138 goto err_early_irq;
Michal Kazior5d1aa942013-11-25 14:06:24 +01001139 }
1140
Michal Kaziorc80de122013-11-25 14:06:23 +01001141 ret = ath10k_pci_setup_ce_irq(ar);
1142 if (ret) {
1143 ath10k_warn("failed to setup CE interrupts: %d\n", ret);
Michal Kazior5d1aa942013-11-25 14:06:24 +01001144 goto err_stop;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001145 }
1146
1147 /* Post buffers once to start things off. */
1148 ret = ath10k_pci_post_rx(ar);
1149 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001150 ath10k_warn("failed to post RX buffers for all pipes: %d\n",
1151 ret);
Michal Kazior5d1aa942013-11-25 14:06:24 +01001152 goto err_stop;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001153 }
1154
1155 ar_pci->started = 1;
1156 return 0;
Michal Kaziorc80de122013-11-25 14:06:23 +01001157
Michal Kazior5d1aa942013-11-25 14:06:24 +01001158err_stop:
1159 ath10k_ce_disable_interrupts(ar);
1160 ath10k_pci_free_irq(ar);
1161 ath10k_pci_kill_tasklet(ar);
Michal Kaziorab977bd2013-11-25 14:06:26 +01001162err_early_irq:
1163 /* Though there should be no interrupts (device was reset)
1164 * power_down() expects the early IRQ to be installed as per the
1165 * driver lifecycle. */
1166 ret_early = ath10k_pci_request_early_irq(ar);
1167 if (ret_early)
1168 ath10k_warn("failed to re-enable early irq: %d\n", ret_early);
1169
Michal Kaziorc80de122013-11-25 14:06:23 +01001170 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001171}
1172
Michal Kazior87263e52013-08-27 13:08:01 +02001173static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe *pipe_info)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001174{
1175 struct ath10k *ar;
1176 struct ath10k_pci *ar_pci;
Michal Kazior2aa39112013-08-27 13:08:02 +02001177 struct ath10k_ce_pipe *ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001178 u32 buf_sz;
1179 struct sk_buff *netbuf;
1180 u32 ce_data;
1181
1182 buf_sz = pipe_info->buf_sz;
1183
1184 /* Unused Copy Engine */
1185 if (buf_sz == 0)
1186 return;
1187
1188 ar = pipe_info->hif_ce_state;
1189 ar_pci = ath10k_pci_priv(ar);
1190
1191 if (!ar_pci->started)
1192 return;
1193
1194 ce_hdl = pipe_info->ce_hdl;
1195
1196 while (ath10k_ce_revoke_recv_next(ce_hdl, (void **)&netbuf,
1197 &ce_data) == 0) {
1198 dma_unmap_single(ar->dev, ATH10K_SKB_CB(netbuf)->paddr,
1199 netbuf->len + skb_tailroom(netbuf),
1200 DMA_FROM_DEVICE);
1201 dev_kfree_skb_any(netbuf);
1202 }
1203}
1204
Michal Kazior87263e52013-08-27 13:08:01 +02001205static void ath10k_pci_tx_pipe_cleanup(struct ath10k_pci_pipe *pipe_info)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001206{
1207 struct ath10k *ar;
1208 struct ath10k_pci *ar_pci;
Michal Kazior2aa39112013-08-27 13:08:02 +02001209 struct ath10k_ce_pipe *ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001210 struct sk_buff *netbuf;
1211 u32 ce_data;
1212 unsigned int nbytes;
1213 unsigned int id;
1214 u32 buf_sz;
1215
1216 buf_sz = pipe_info->buf_sz;
1217
1218 /* Unused Copy Engine */
1219 if (buf_sz == 0)
1220 return;
1221
1222 ar = pipe_info->hif_ce_state;
1223 ar_pci = ath10k_pci_priv(ar);
1224
1225 if (!ar_pci->started)
1226 return;
1227
1228 ce_hdl = pipe_info->ce_hdl;
1229
1230 while (ath10k_ce_cancel_send_next(ce_hdl, (void **)&netbuf,
1231 &ce_data, &nbytes, &id) == 0) {
Michal Kaziora16942e2014-02-27 18:50:04 +02001232 /* no need to call tx completion for NULL pointers */
1233 if (!netbuf)
Michal Kazior2415fc12013-11-08 08:01:32 +01001234 continue;
Michal Kazior2415fc12013-11-08 08:01:32 +01001235
Kalle Valoe9bb0aa2013-09-08 18:36:11 +03001236 ar_pci->msg_callbacks_current.tx_completion(ar,
1237 netbuf,
1238 id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001239 }
1240}
1241
1242/*
1243 * Cleanup residual buffers for device shutdown:
1244 * buffers that were enqueued for receive
1245 * buffers that were to be sent
1246 * Note: Buffers that had completed but which were
1247 * not yet processed are on a completion queue. They
1248 * are handled when the completion thread shuts down.
1249 */
1250static void ath10k_pci_buffer_cleanup(struct ath10k *ar)
1251{
1252 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1253 int pipe_num;
1254
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001255 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Michal Kazior87263e52013-08-27 13:08:01 +02001256 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001257
1258 pipe_info = &ar_pci->pipe_info[pipe_num];
1259 ath10k_pci_rx_pipe_cleanup(pipe_info);
1260 ath10k_pci_tx_pipe_cleanup(pipe_info);
1261 }
1262}
1263
1264static void ath10k_pci_ce_deinit(struct ath10k *ar)
1265{
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001266 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001267
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001268 for (i = 0; i < CE_COUNT; i++)
1269 ath10k_ce_deinit_pipe(ar, i);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001270}
1271
1272static void ath10k_pci_hif_stop(struct ath10k *ar)
1273{
Michal Kazior32270b62013-08-02 09:15:47 +02001274 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior5d1aa942013-11-25 14:06:24 +01001275 int ret;
Michal Kazior32270b62013-08-02 09:15:47 +02001276
Kalle Valo50f87a62014-03-28 09:32:52 +02001277 ath10k_dbg(ATH10K_DBG_BOOT, "boot hif stop\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001278
Michal Kaziorf2708be2014-05-16 17:15:39 +03001279 if (WARN_ON(!ar_pci->started))
1280 return;
1281
Michal Kazior5d1aa942013-11-25 14:06:24 +01001282 ret = ath10k_ce_disable_interrupts(ar);
1283 if (ret)
1284 ath10k_warn("failed to disable CE interrupts: %d\n", ret);
Michal Kazior32270b62013-08-02 09:15:47 +02001285
Michal Kazior5d1aa942013-11-25 14:06:24 +01001286 ath10k_pci_free_irq(ar);
1287 ath10k_pci_kill_tasklet(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001288
Michal Kaziorab977bd2013-11-25 14:06:26 +01001289 ret = ath10k_pci_request_early_irq(ar);
1290 if (ret)
1291 ath10k_warn("failed to re-enable early irq: %d\n", ret);
1292
Kalle Valo5e3dd152013-06-12 20:52:10 +03001293 /* At this point, asynchronous threads are stopped, the target should
1294 * not DMA nor interrupt. We process the leftovers and then free
1295 * everything else up. */
1296
Kalle Valo5e3dd152013-06-12 20:52:10 +03001297 ath10k_pci_buffer_cleanup(ar);
Michal Kazior32270b62013-08-02 09:15:47 +02001298
Michal Kazior6a42a472013-11-08 08:01:35 +01001299 /* Make the sure the device won't access any structures on the host by
1300 * resetting it. The device was fed with PCI CE ringbuffer
1301 * configuration during init. If ringbuffers are freed and the device
1302 * were to access them this could lead to memory corruption on the
1303 * host. */
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001304 ath10k_pci_warm_reset(ar);
Michal Kazior6a42a472013-11-08 08:01:35 +01001305
Michal Kazior32270b62013-08-02 09:15:47 +02001306 ar_pci->started = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001307}
1308
1309static int ath10k_pci_hif_exchange_bmi_msg(struct ath10k *ar,
1310 void *req, u32 req_len,
1311 void *resp, u32 *resp_len)
1312{
1313 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2aa39112013-08-27 13:08:02 +02001314 struct ath10k_pci_pipe *pci_tx = &ar_pci->pipe_info[BMI_CE_NUM_TO_TARG];
1315 struct ath10k_pci_pipe *pci_rx = &ar_pci->pipe_info[BMI_CE_NUM_TO_HOST];
1316 struct ath10k_ce_pipe *ce_tx = pci_tx->ce_hdl;
1317 struct ath10k_ce_pipe *ce_rx = pci_rx->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001318 dma_addr_t req_paddr = 0;
1319 dma_addr_t resp_paddr = 0;
1320 struct bmi_xfer xfer = {};
1321 void *treq, *tresp = NULL;
1322 int ret = 0;
1323
Michal Kazior85622cd2013-11-25 14:06:22 +01001324 might_sleep();
1325
Kalle Valo5e3dd152013-06-12 20:52:10 +03001326 if (resp && !resp_len)
1327 return -EINVAL;
1328
1329 if (resp && resp_len && *resp_len == 0)
1330 return -EINVAL;
1331
1332 treq = kmemdup(req, req_len, GFP_KERNEL);
1333 if (!treq)
1334 return -ENOMEM;
1335
1336 req_paddr = dma_map_single(ar->dev, treq, req_len, DMA_TO_DEVICE);
1337 ret = dma_mapping_error(ar->dev, req_paddr);
1338 if (ret)
1339 goto err_dma;
1340
1341 if (resp && resp_len) {
1342 tresp = kzalloc(*resp_len, GFP_KERNEL);
1343 if (!tresp) {
1344 ret = -ENOMEM;
1345 goto err_req;
1346 }
1347
1348 resp_paddr = dma_map_single(ar->dev, tresp, *resp_len,
1349 DMA_FROM_DEVICE);
1350 ret = dma_mapping_error(ar->dev, resp_paddr);
1351 if (ret)
1352 goto err_req;
1353
1354 xfer.wait_for_resp = true;
1355 xfer.resp_len = 0;
1356
1357 ath10k_ce_recv_buf_enqueue(ce_rx, &xfer, resp_paddr);
1358 }
1359
1360 init_completion(&xfer.done);
1361
1362 ret = ath10k_ce_send(ce_tx, &xfer, req_paddr, req_len, -1, 0);
1363 if (ret)
1364 goto err_resp;
1365
Michal Kazior85622cd2013-11-25 14:06:22 +01001366 ret = ath10k_pci_bmi_wait(ce_tx, ce_rx, &xfer);
1367 if (ret) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001368 u32 unused_buffer;
1369 unsigned int unused_nbytes;
1370 unsigned int unused_id;
1371
Kalle Valo5e3dd152013-06-12 20:52:10 +03001372 ath10k_ce_cancel_send_next(ce_tx, NULL, &unused_buffer,
1373 &unused_nbytes, &unused_id);
1374 } else {
1375 /* non-zero means we did not time out */
1376 ret = 0;
1377 }
1378
1379err_resp:
1380 if (resp) {
1381 u32 unused_buffer;
1382
1383 ath10k_ce_revoke_recv_next(ce_rx, NULL, &unused_buffer);
1384 dma_unmap_single(ar->dev, resp_paddr,
1385 *resp_len, DMA_FROM_DEVICE);
1386 }
1387err_req:
1388 dma_unmap_single(ar->dev, req_paddr, req_len, DMA_TO_DEVICE);
1389
1390 if (ret == 0 && resp_len) {
1391 *resp_len = min(*resp_len, xfer.resp_len);
1392 memcpy(resp, tresp, xfer.resp_len);
1393 }
1394err_dma:
1395 kfree(treq);
1396 kfree(tresp);
1397
1398 return ret;
1399}
1400
Michal Kazior5440ce22013-09-03 15:09:58 +02001401static void ath10k_pci_bmi_send_done(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001402{
Michal Kazior5440ce22013-09-03 15:09:58 +02001403 struct bmi_xfer *xfer;
1404 u32 ce_data;
1405 unsigned int nbytes;
1406 unsigned int transfer_id;
1407
1408 if (ath10k_ce_completed_send_next(ce_state, (void **)&xfer, &ce_data,
1409 &nbytes, &transfer_id))
1410 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001411
1412 if (xfer->wait_for_resp)
1413 return;
1414
1415 complete(&xfer->done);
1416}
1417
Michal Kazior5440ce22013-09-03 15:09:58 +02001418static void ath10k_pci_bmi_recv_data(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001419{
Michal Kazior5440ce22013-09-03 15:09:58 +02001420 struct bmi_xfer *xfer;
1421 u32 ce_data;
1422 unsigned int nbytes;
1423 unsigned int transfer_id;
1424 unsigned int flags;
1425
1426 if (ath10k_ce_completed_recv_next(ce_state, (void **)&xfer, &ce_data,
1427 &nbytes, &transfer_id, &flags))
1428 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001429
1430 if (!xfer->wait_for_resp) {
1431 ath10k_warn("unexpected: BMI data received; ignoring\n");
1432 return;
1433 }
1434
1435 xfer->resp_len = nbytes;
1436 complete(&xfer->done);
1437}
1438
Michal Kazior85622cd2013-11-25 14:06:22 +01001439static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
1440 struct ath10k_ce_pipe *rx_pipe,
1441 struct bmi_xfer *xfer)
1442{
1443 unsigned long timeout = jiffies + BMI_COMMUNICATION_TIMEOUT_HZ;
1444
1445 while (time_before_eq(jiffies, timeout)) {
1446 ath10k_pci_bmi_send_done(tx_pipe);
1447 ath10k_pci_bmi_recv_data(rx_pipe);
1448
1449 if (completion_done(&xfer->done))
1450 return 0;
1451
1452 schedule();
1453 }
1454
1455 return -ETIMEDOUT;
1456}
1457
Kalle Valo5e3dd152013-06-12 20:52:10 +03001458/*
1459 * Map from service/endpoint to Copy Engine.
1460 * This table is derived from the CE_PCI TABLE, above.
1461 * It is passed to the Target at startup for use by firmware.
1462 */
1463static const struct service_to_pipe target_service_to_ce_map_wlan[] = {
1464 {
1465 ATH10K_HTC_SVC_ID_WMI_DATA_VO,
1466 PIPEDIR_OUT, /* out = UL = host -> target */
1467 3,
1468 },
1469 {
1470 ATH10K_HTC_SVC_ID_WMI_DATA_VO,
1471 PIPEDIR_IN, /* in = DL = target -> host */
1472 2,
1473 },
1474 {
1475 ATH10K_HTC_SVC_ID_WMI_DATA_BK,
1476 PIPEDIR_OUT, /* out = UL = host -> target */
1477 3,
1478 },
1479 {
1480 ATH10K_HTC_SVC_ID_WMI_DATA_BK,
1481 PIPEDIR_IN, /* in = DL = target -> host */
1482 2,
1483 },
1484 {
1485 ATH10K_HTC_SVC_ID_WMI_DATA_BE,
1486 PIPEDIR_OUT, /* out = UL = host -> target */
1487 3,
1488 },
1489 {
1490 ATH10K_HTC_SVC_ID_WMI_DATA_BE,
1491 PIPEDIR_IN, /* in = DL = target -> host */
1492 2,
1493 },
1494 {
1495 ATH10K_HTC_SVC_ID_WMI_DATA_VI,
1496 PIPEDIR_OUT, /* out = UL = host -> target */
1497 3,
1498 },
1499 {
1500 ATH10K_HTC_SVC_ID_WMI_DATA_VI,
1501 PIPEDIR_IN, /* in = DL = target -> host */
1502 2,
1503 },
1504 {
1505 ATH10K_HTC_SVC_ID_WMI_CONTROL,
1506 PIPEDIR_OUT, /* out = UL = host -> target */
1507 3,
1508 },
1509 {
1510 ATH10K_HTC_SVC_ID_WMI_CONTROL,
1511 PIPEDIR_IN, /* in = DL = target -> host */
1512 2,
1513 },
1514 {
1515 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1516 PIPEDIR_OUT, /* out = UL = host -> target */
1517 0, /* could be moved to 3 (share with WMI) */
1518 },
1519 {
1520 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1521 PIPEDIR_IN, /* in = DL = target -> host */
1522 1,
1523 },
1524 {
1525 ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS, /* not currently used */
1526 PIPEDIR_OUT, /* out = UL = host -> target */
1527 0,
1528 },
1529 {
1530 ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS, /* not currently used */
1531 PIPEDIR_IN, /* in = DL = target -> host */
1532 1,
1533 },
1534 {
1535 ATH10K_HTC_SVC_ID_HTT_DATA_MSG,
1536 PIPEDIR_OUT, /* out = UL = host -> target */
1537 4,
1538 },
1539 {
1540 ATH10K_HTC_SVC_ID_HTT_DATA_MSG,
1541 PIPEDIR_IN, /* in = DL = target -> host */
1542 1,
1543 },
1544
1545 /* (Additions here) */
1546
1547 { /* Must be last */
1548 0,
1549 0,
1550 0,
1551 },
1552};
1553
1554/*
1555 * Send an interrupt to the device to wake up the Target CPU
1556 * so it has an opportunity to notice any changed state.
1557 */
1558static int ath10k_pci_wake_target_cpu(struct ath10k *ar)
1559{
1560 int ret;
1561 u32 core_ctrl;
1562
1563 ret = ath10k_pci_diag_read_access(ar, SOC_CORE_BASE_ADDRESS |
1564 CORE_CTRL_ADDRESS,
1565 &core_ctrl);
1566 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001567 ath10k_warn("failed to read core_ctrl: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001568 return ret;
1569 }
1570
1571 /* A_INUM_FIRMWARE interrupt to Target CPU */
1572 core_ctrl |= CORE_CTRL_CPU_INTR_MASK;
1573
1574 ret = ath10k_pci_diag_write_access(ar, SOC_CORE_BASE_ADDRESS |
1575 CORE_CTRL_ADDRESS,
1576 core_ctrl);
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001577 if (ret) {
1578 ath10k_warn("failed to set target CPU interrupt mask: %d\n",
1579 ret);
1580 return ret;
1581 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001582
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001583 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001584}
1585
1586static int ath10k_pci_init_config(struct ath10k *ar)
1587{
1588 u32 interconnect_targ_addr;
1589 u32 pcie_state_targ_addr = 0;
1590 u32 pipe_cfg_targ_addr = 0;
1591 u32 svc_to_pipe_map = 0;
1592 u32 pcie_config_flags = 0;
1593 u32 ealloc_value;
1594 u32 ealloc_targ_addr;
1595 u32 flag2_value;
1596 u32 flag2_targ_addr;
1597 int ret = 0;
1598
1599 /* Download to Target the CE Config and the service-to-CE map */
1600 interconnect_targ_addr =
1601 host_interest_item_address(HI_ITEM(hi_interconnect_state));
1602
1603 /* Supply Target-side CE configuration */
1604 ret = ath10k_pci_diag_read_access(ar, interconnect_targ_addr,
1605 &pcie_state_targ_addr);
1606 if (ret != 0) {
1607 ath10k_err("Failed to get pcie state addr: %d\n", ret);
1608 return ret;
1609 }
1610
1611 if (pcie_state_targ_addr == 0) {
1612 ret = -EIO;
1613 ath10k_err("Invalid pcie state addr\n");
1614 return ret;
1615 }
1616
1617 ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
1618 offsetof(struct pcie_state,
1619 pipe_cfg_addr),
1620 &pipe_cfg_targ_addr);
1621 if (ret != 0) {
1622 ath10k_err("Failed to get pipe cfg addr: %d\n", ret);
1623 return ret;
1624 }
1625
1626 if (pipe_cfg_targ_addr == 0) {
1627 ret = -EIO;
1628 ath10k_err("Invalid pipe cfg addr\n");
1629 return ret;
1630 }
1631
1632 ret = ath10k_pci_diag_write_mem(ar, pipe_cfg_targ_addr,
1633 target_ce_config_wlan,
1634 sizeof(target_ce_config_wlan));
1635
1636 if (ret != 0) {
1637 ath10k_err("Failed to write pipe cfg: %d\n", ret);
1638 return ret;
1639 }
1640
1641 ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
1642 offsetof(struct pcie_state,
1643 svc_to_pipe_map),
1644 &svc_to_pipe_map);
1645 if (ret != 0) {
1646 ath10k_err("Failed to get svc/pipe map: %d\n", ret);
1647 return ret;
1648 }
1649
1650 if (svc_to_pipe_map == 0) {
1651 ret = -EIO;
1652 ath10k_err("Invalid svc_to_pipe map\n");
1653 return ret;
1654 }
1655
1656 ret = ath10k_pci_diag_write_mem(ar, svc_to_pipe_map,
1657 target_service_to_ce_map_wlan,
1658 sizeof(target_service_to_ce_map_wlan));
1659 if (ret != 0) {
1660 ath10k_err("Failed to write svc/pipe map: %d\n", ret);
1661 return ret;
1662 }
1663
1664 ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
1665 offsetof(struct pcie_state,
1666 config_flags),
1667 &pcie_config_flags);
1668 if (ret != 0) {
1669 ath10k_err("Failed to get pcie config_flags: %d\n", ret);
1670 return ret;
1671 }
1672
1673 pcie_config_flags &= ~PCIE_CONFIG_FLAG_ENABLE_L1;
1674
1675 ret = ath10k_pci_diag_write_mem(ar, pcie_state_targ_addr +
1676 offsetof(struct pcie_state, config_flags),
1677 &pcie_config_flags,
1678 sizeof(pcie_config_flags));
1679 if (ret != 0) {
1680 ath10k_err("Failed to write pcie config_flags: %d\n", ret);
1681 return ret;
1682 }
1683
1684 /* configure early allocation */
1685 ealloc_targ_addr = host_interest_item_address(HI_ITEM(hi_early_alloc));
1686
1687 ret = ath10k_pci_diag_read_access(ar, ealloc_targ_addr, &ealloc_value);
1688 if (ret != 0) {
1689 ath10k_err("Faile to get early alloc val: %d\n", ret);
1690 return ret;
1691 }
1692
1693 /* first bank is switched to IRAM */
1694 ealloc_value |= ((HI_EARLY_ALLOC_MAGIC << HI_EARLY_ALLOC_MAGIC_SHIFT) &
1695 HI_EARLY_ALLOC_MAGIC_MASK);
1696 ealloc_value |= ((1 << HI_EARLY_ALLOC_IRAM_BANKS_SHIFT) &
1697 HI_EARLY_ALLOC_IRAM_BANKS_MASK);
1698
1699 ret = ath10k_pci_diag_write_access(ar, ealloc_targ_addr, ealloc_value);
1700 if (ret != 0) {
1701 ath10k_err("Failed to set early alloc val: %d\n", ret);
1702 return ret;
1703 }
1704
1705 /* Tell Target to proceed with initialization */
1706 flag2_targ_addr = host_interest_item_address(HI_ITEM(hi_option_flag2));
1707
1708 ret = ath10k_pci_diag_read_access(ar, flag2_targ_addr, &flag2_value);
1709 if (ret != 0) {
1710 ath10k_err("Failed to get option val: %d\n", ret);
1711 return ret;
1712 }
1713
1714 flag2_value |= HI_OPTION_EARLY_CFG_DONE;
1715
1716 ret = ath10k_pci_diag_write_access(ar, flag2_targ_addr, flag2_value);
1717 if (ret != 0) {
1718 ath10k_err("Failed to set option val: %d\n", ret);
1719 return ret;
1720 }
1721
1722 return 0;
1723}
1724
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001725static int ath10k_pci_alloc_ce(struct ath10k *ar)
1726{
1727 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001728
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001729 for (i = 0; i < CE_COUNT; i++) {
1730 ret = ath10k_ce_alloc_pipe(ar, i, &host_ce_config_wlan[i]);
1731 if (ret) {
1732 ath10k_err("failed to allocate copy engine pipe %d: %d\n",
1733 i, ret);
1734 return ret;
1735 }
1736 }
1737
1738 return 0;
1739}
1740
1741static void ath10k_pci_free_ce(struct ath10k *ar)
1742{
1743 int i;
1744
1745 for (i = 0; i < CE_COUNT; i++)
1746 ath10k_ce_free_pipe(ar, i);
1747}
Kalle Valo5e3dd152013-06-12 20:52:10 +03001748
1749static int ath10k_pci_ce_init(struct ath10k *ar)
1750{
1751 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +02001752 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001753 const struct ce_attr *attr;
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001754 int pipe_num, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001755
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001756 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001757 pipe_info = &ar_pci->pipe_info[pipe_num];
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001758 pipe_info->ce_hdl = &ar_pci->ce_states[pipe_num];
Kalle Valo5e3dd152013-06-12 20:52:10 +03001759 pipe_info->pipe_num = pipe_num;
1760 pipe_info->hif_ce_state = ar;
1761 attr = &host_ce_config_wlan[pipe_num];
1762
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001763 ret = ath10k_ce_init_pipe(ar, pipe_num, attr);
1764 if (ret) {
1765 ath10k_err("failed to initialize copy engine pipe %d: %d\n",
1766 pipe_num, ret);
1767 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001768 }
1769
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001770 if (pipe_num == CE_COUNT - 1) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001771 /*
1772 * Reserve the ultimate CE for
1773 * diagnostic Window support
1774 */
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001775 ar_pci->ce_diag = pipe_info->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001776 continue;
1777 }
1778
1779 pipe_info->buf_sz = (size_t) (attr->src_sz_max);
1780 }
1781
Kalle Valo5e3dd152013-06-12 20:52:10 +03001782 return 0;
1783}
1784
1785static void ath10k_pci_fw_interrupt_handler(struct ath10k *ar)
1786{
1787 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valob39712c2014-03-28 09:32:46 +02001788 u32 fw_indicator;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001789
1790 ath10k_pci_wake(ar);
1791
Kalle Valob39712c2014-03-28 09:32:46 +02001792 fw_indicator = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001793
1794 if (fw_indicator & FW_IND_EVENT_PENDING) {
1795 /* ACK: clear Target-side pending event */
Kalle Valob39712c2014-03-28 09:32:46 +02001796 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001797 fw_indicator & ~FW_IND_EVENT_PENDING);
1798
1799 if (ar_pci->started) {
1800 ath10k_pci_hif_dump_area(ar);
1801 } else {
1802 /*
1803 * Probable Target failure before we're prepared
1804 * to handle it. Generally unexpected.
1805 */
1806 ath10k_warn("early firmware event indicated\n");
1807 }
1808 }
1809
1810 ath10k_pci_sleep(ar);
1811}
1812
Michal Kaziorde013572014-05-14 16:56:16 +03001813/* this function effectively clears target memory controller assert line */
1814static void ath10k_pci_warm_reset_si0(struct ath10k *ar)
1815{
1816 u32 val;
1817
1818 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1819 ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
1820 val | SOC_RESET_CONTROL_SI0_RST_MASK);
1821 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1822
1823 msleep(10);
1824
1825 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1826 ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
1827 val & ~SOC_RESET_CONTROL_SI0_RST_MASK);
1828 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1829
1830 msleep(10);
1831}
1832
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001833static int ath10k_pci_warm_reset(struct ath10k *ar)
1834{
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001835 int ret = 0;
1836 u32 val;
1837
Kalle Valo50f87a62014-03-28 09:32:52 +02001838 ath10k_dbg(ATH10K_DBG_BOOT, "boot warm reset\n");
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001839
1840 ret = ath10k_do_pci_wake(ar);
1841 if (ret) {
1842 ath10k_err("failed to wake up target: %d\n", ret);
1843 return ret;
1844 }
1845
1846 /* debug */
1847 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1848 PCIE_INTR_CAUSE_ADDRESS);
1849 ath10k_dbg(ATH10K_DBG_BOOT, "boot host cpu intr cause: 0x%08x\n", val);
1850
1851 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1852 CPU_INTR_ADDRESS);
1853 ath10k_dbg(ATH10K_DBG_BOOT, "boot target cpu intr cause: 0x%08x\n",
1854 val);
1855
1856 /* disable pending irqs */
1857 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
1858 PCIE_INTR_ENABLE_ADDRESS, 0);
1859
1860 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
1861 PCIE_INTR_CLR_ADDRESS, ~0);
1862
1863 msleep(100);
1864
1865 /* clear fw indicator */
Kalle Valob39712c2014-03-28 09:32:46 +02001866 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, 0);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001867
1868 /* clear target LF timer interrupts */
1869 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1870 SOC_LF_TIMER_CONTROL0_ADDRESS);
1871 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS +
1872 SOC_LF_TIMER_CONTROL0_ADDRESS,
1873 val & ~SOC_LF_TIMER_CONTROL0_ENABLE_MASK);
1874
1875 /* reset CE */
1876 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1877 SOC_RESET_CONTROL_ADDRESS);
1878 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1879 val | SOC_RESET_CONTROL_CE_RST_MASK);
1880 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1881 SOC_RESET_CONTROL_ADDRESS);
1882 msleep(10);
1883
1884 /* unreset CE */
1885 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1886 val & ~SOC_RESET_CONTROL_CE_RST_MASK);
1887 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1888 SOC_RESET_CONTROL_ADDRESS);
1889 msleep(10);
1890
Michal Kaziorde013572014-05-14 16:56:16 +03001891 ath10k_pci_warm_reset_si0(ar);
1892
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001893 /* debug */
1894 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1895 PCIE_INTR_CAUSE_ADDRESS);
1896 ath10k_dbg(ATH10K_DBG_BOOT, "boot host cpu intr cause: 0x%08x\n", val);
1897
1898 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1899 CPU_INTR_ADDRESS);
1900 ath10k_dbg(ATH10K_DBG_BOOT, "boot target cpu intr cause: 0x%08x\n",
1901 val);
1902
1903 /* CPU warm reset */
1904 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1905 SOC_RESET_CONTROL_ADDRESS);
1906 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1907 val | SOC_RESET_CONTROL_CPU_WARM_RST_MASK);
1908
1909 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1910 SOC_RESET_CONTROL_ADDRESS);
1911 ath10k_dbg(ATH10K_DBG_BOOT, "boot target reset state: 0x%08x\n", val);
1912
1913 msleep(100);
1914
1915 ath10k_dbg(ATH10K_DBG_BOOT, "boot warm reset complete\n");
1916
1917 ath10k_do_pci_sleep(ar);
1918 return ret;
1919}
1920
1921static int __ath10k_pci_hif_power_up(struct ath10k *ar, bool cold_reset)
Michal Kazior8c5c5362013-07-16 09:38:50 +02001922{
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02001923 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo95cbb6a2013-11-20 10:00:35 +02001924 const char *irq_mode;
Michal Kazior8c5c5362013-07-16 09:38:50 +02001925 int ret;
1926
1927 /*
1928 * Bring the target up cleanly.
1929 *
1930 * The target may be in an undefined state with an AUX-powered Target
1931 * and a Host in WoW mode. If the Host crashes, loses power, or is
1932 * restarted (without unloading the driver) then the Target is left
1933 * (aux) powered and running. On a subsequent driver load, the Target
1934 * is in an unexpected state. We try to catch that here in order to
1935 * reset the Target and retry the probe.
1936 */
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001937 if (cold_reset)
1938 ret = ath10k_pci_cold_reset(ar);
1939 else
1940 ret = ath10k_pci_warm_reset(ar);
1941
Michal Kazior5b2589f2013-11-08 08:01:30 +01001942 if (ret) {
1943 ath10k_err("failed to reset target: %d\n", ret);
Michal Kazior98563d52013-11-08 08:01:33 +01001944 goto err;
Michal Kazior5b2589f2013-11-08 08:01:30 +01001945 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02001946
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02001947 if (!test_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE, ar_pci->features))
Michal Kazior8c5c5362013-07-16 09:38:50 +02001948 /* Force AWAKE forever */
Michal Kazior8c5c5362013-07-16 09:38:50 +02001949 ath10k_do_pci_wake(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001950
1951 ret = ath10k_pci_ce_init(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001952 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001953 ath10k_err("failed to initialize CE: %d\n", ret);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001954 goto err_ps;
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001955 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02001956
Michal Kazior98563d52013-11-08 08:01:33 +01001957 ret = ath10k_ce_disable_interrupts(ar);
1958 if (ret) {
1959 ath10k_err("failed to disable CE interrupts: %d\n", ret);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001960 goto err_ce;
1961 }
1962
Michal Kaziorfc15ca12013-11-25 14:06:21 +01001963 ret = ath10k_pci_init_irq(ar);
Michal Kazior98563d52013-11-08 08:01:33 +01001964 if (ret) {
Michal Kaziorfc15ca12013-11-25 14:06:21 +01001965 ath10k_err("failed to init irqs: %d\n", ret);
Michal Kazior98563d52013-11-08 08:01:33 +01001966 goto err_ce;
1967 }
1968
Michal Kaziorab977bd2013-11-25 14:06:26 +01001969 ret = ath10k_pci_request_early_irq(ar);
1970 if (ret) {
1971 ath10k_err("failed to request early irq: %d\n", ret);
1972 goto err_deinit_irq;
1973 }
1974
Michal Kazior98563d52013-11-08 08:01:33 +01001975 ret = ath10k_pci_wait_for_target_init(ar);
1976 if (ret) {
1977 ath10k_err("failed to wait for target to init: %d\n", ret);
Michal Kaziorab977bd2013-11-25 14:06:26 +01001978 goto err_free_early_irq;
Michal Kazior98563d52013-11-08 08:01:33 +01001979 }
1980
1981 ret = ath10k_pci_init_config(ar);
1982 if (ret) {
1983 ath10k_err("failed to setup init config: %d\n", ret);
Michal Kaziorab977bd2013-11-25 14:06:26 +01001984 goto err_free_early_irq;
Michal Kazior98563d52013-11-08 08:01:33 +01001985 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02001986
1987 ret = ath10k_pci_wake_target_cpu(ar);
1988 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001989 ath10k_err("could not wake up target CPU: %d\n", ret);
Michal Kaziorab977bd2013-11-25 14:06:26 +01001990 goto err_free_early_irq;
Michal Kazior8c5c5362013-07-16 09:38:50 +02001991 }
1992
Kalle Valo95cbb6a2013-11-20 10:00:35 +02001993 if (ar_pci->num_msi_intrs > 1)
1994 irq_mode = "MSI-X";
1995 else if (ar_pci->num_msi_intrs == 1)
1996 irq_mode = "MSI";
1997 else
1998 irq_mode = "legacy";
1999
Kalle Valo650b91f2013-11-20 10:00:49 +02002000 if (!test_bit(ATH10K_FLAG_FIRST_BOOT_DONE, &ar->dev_flags))
Kalle Valo78a9cb42014-03-28 09:32:58 +02002001 ath10k_info("pci irq %s irq_mode %d reset_mode %d\n",
2002 irq_mode, ath10k_pci_irq_mode,
2003 ath10k_pci_reset_mode);
Kalle Valo95cbb6a2013-11-20 10:00:35 +02002004
Michal Kazior8c5c5362013-07-16 09:38:50 +02002005 return 0;
2006
Michal Kaziorab977bd2013-11-25 14:06:26 +01002007err_free_early_irq:
2008 ath10k_pci_free_early_irq(ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002009err_deinit_irq:
2010 ath10k_pci_deinit_irq(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02002011err_ce:
2012 ath10k_pci_ce_deinit(ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002013 ath10k_pci_warm_reset(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02002014err_ps:
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002015 if (!test_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE, ar_pci->features))
Michal Kazior8c5c5362013-07-16 09:38:50 +02002016 ath10k_do_pci_sleep(ar);
2017err:
2018 return ret;
2019}
2020
Michal Kazior61c95ce2014-05-14 16:56:16 +03002021static int ath10k_pci_hif_power_up_warm(struct ath10k *ar)
2022{
2023 int i, ret;
2024
2025 /*
2026 * Sometime warm reset succeeds after retries.
2027 *
2028 * FIXME: It might be possible to tune ath10k_pci_warm_reset() to work
2029 * at first try.
2030 */
2031 for (i = 0; i < ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS; i++) {
2032 ret = __ath10k_pci_hif_power_up(ar, false);
2033 if (ret == 0)
2034 break;
2035
2036 ath10k_warn("failed to warm reset (attempt %d out of %d): %d\n",
2037 i + 1, ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS, ret);
2038 }
2039
2040 return ret;
2041}
2042
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002043static int ath10k_pci_hif_power_up(struct ath10k *ar)
2044{
2045 int ret;
2046
Kalle Valo50f87a62014-03-28 09:32:52 +02002047 ath10k_dbg(ATH10K_DBG_BOOT, "boot hif power up\n");
2048
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002049 /*
2050 * Hardware CUS232 version 2 has some issues with cold reset and the
2051 * preferred (and safer) way to perform a device reset is through a
2052 * warm reset.
2053 *
Michal Kazior61c95ce2014-05-14 16:56:16 +03002054 * Warm reset doesn't always work though so fall back to cold reset may
2055 * be necessary.
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002056 */
Michal Kazior61c95ce2014-05-14 16:56:16 +03002057 ret = ath10k_pci_hif_power_up_warm(ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002058 if (ret) {
Kalle Valo35098462014-03-28 09:32:27 +02002059 ath10k_warn("failed to power up target using warm reset: %d\n",
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002060 ret);
2061
Kalle Valo35098462014-03-28 09:32:27 +02002062 if (ath10k_pci_reset_mode == ATH10K_PCI_RESET_WARM_ONLY)
2063 return ret;
2064
2065 ath10k_warn("trying cold reset\n");
2066
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002067 ret = __ath10k_pci_hif_power_up(ar, true);
2068 if (ret) {
2069 ath10k_err("failed to power up target using cold reset too (%d)\n",
2070 ret);
2071 return ret;
2072 }
2073 }
2074
2075 return 0;
2076}
2077
Michal Kazior8c5c5362013-07-16 09:38:50 +02002078static void ath10k_pci_hif_power_down(struct ath10k *ar)
2079{
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002080 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2081
Kalle Valo50f87a62014-03-28 09:32:52 +02002082 ath10k_dbg(ATH10K_DBG_BOOT, "boot hif power down\n");
2083
Michal Kaziorab977bd2013-11-25 14:06:26 +01002084 ath10k_pci_free_early_irq(ar);
2085 ath10k_pci_kill_tasklet(ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002086 ath10k_pci_deinit_irq(ar);
Michal Kaziordf5e8522014-03-28 10:02:45 +02002087 ath10k_pci_ce_deinit(ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002088 ath10k_pci_warm_reset(ar);
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002089
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002090 if (!test_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE, ar_pci->features))
Michal Kazior8c5c5362013-07-16 09:38:50 +02002091 ath10k_do_pci_sleep(ar);
2092}
2093
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002094#ifdef CONFIG_PM
2095
2096#define ATH10K_PCI_PM_CONTROL 0x44
2097
2098static int ath10k_pci_hif_suspend(struct ath10k *ar)
2099{
2100 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2101 struct pci_dev *pdev = ar_pci->pdev;
2102 u32 val;
2103
2104 pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
2105
2106 if ((val & 0x000000ff) != 0x3) {
2107 pci_save_state(pdev);
2108 pci_disable_device(pdev);
2109 pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
2110 (val & 0xffffff00) | 0x03);
2111 }
2112
2113 return 0;
2114}
2115
2116static int ath10k_pci_hif_resume(struct ath10k *ar)
2117{
2118 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2119 struct pci_dev *pdev = ar_pci->pdev;
2120 u32 val;
2121
2122 pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
2123
2124 if ((val & 0x000000ff) != 0) {
2125 pci_restore_state(pdev);
2126 pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
2127 val & 0xffffff00);
2128 /*
2129 * Suspend/Resume resets the PCI configuration space,
2130 * so we have to re-disable the RETRY_TIMEOUT register (0x41)
2131 * to keep PCI Tx retries from interfering with C3 CPU state
2132 */
2133 pci_read_config_dword(pdev, 0x40, &val);
2134
2135 if ((val & 0x0000ff00) != 0)
2136 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
2137 }
2138
2139 return 0;
2140}
2141#endif
2142
Kalle Valo5e3dd152013-06-12 20:52:10 +03002143static const struct ath10k_hif_ops ath10k_pci_hif_ops = {
Michal Kazior726346f2014-02-27 18:50:04 +02002144 .tx_sg = ath10k_pci_hif_tx_sg,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002145 .exchange_bmi_msg = ath10k_pci_hif_exchange_bmi_msg,
2146 .start = ath10k_pci_hif_start,
2147 .stop = ath10k_pci_hif_stop,
2148 .map_service_to_pipe = ath10k_pci_hif_map_service_to_pipe,
2149 .get_default_pipe = ath10k_pci_hif_get_default_pipe,
2150 .send_complete_check = ath10k_pci_hif_send_complete_check,
Michal Kaziore799bbf2013-07-05 16:15:12 +03002151 .set_callbacks = ath10k_pci_hif_set_callbacks,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002152 .get_free_queue_number = ath10k_pci_hif_get_free_queue_number,
Michal Kazior8c5c5362013-07-16 09:38:50 +02002153 .power_up = ath10k_pci_hif_power_up,
2154 .power_down = ath10k_pci_hif_power_down,
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002155#ifdef CONFIG_PM
2156 .suspend = ath10k_pci_hif_suspend,
2157 .resume = ath10k_pci_hif_resume,
2158#endif
Kalle Valo5e3dd152013-06-12 20:52:10 +03002159};
2160
2161static void ath10k_pci_ce_tasklet(unsigned long ptr)
2162{
Michal Kazior87263e52013-08-27 13:08:01 +02002163 struct ath10k_pci_pipe *pipe = (struct ath10k_pci_pipe *)ptr;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002164 struct ath10k_pci *ar_pci = pipe->ar_pci;
2165
2166 ath10k_ce_per_engine_service(ar_pci->ar, pipe->pipe_num);
2167}
2168
2169static void ath10k_msi_err_tasklet(unsigned long data)
2170{
2171 struct ath10k *ar = (struct ath10k *)data;
2172
2173 ath10k_pci_fw_interrupt_handler(ar);
2174}
2175
2176/*
2177 * Handler for a per-engine interrupt on a PARTICULAR CE.
2178 * This is used in cases where each CE has a private MSI interrupt.
2179 */
2180static irqreturn_t ath10k_pci_per_engine_handler(int irq, void *arg)
2181{
2182 struct ath10k *ar = arg;
2183 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2184 int ce_id = irq - ar_pci->pdev->irq - MSI_ASSIGN_CE_INITIAL;
2185
Dan Carpentere5742672013-06-18 10:28:46 +03002186 if (ce_id < 0 || ce_id >= ARRAY_SIZE(ar_pci->pipe_info)) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03002187 ath10k_warn("unexpected/invalid irq %d ce_id %d\n", irq, ce_id);
2188 return IRQ_HANDLED;
2189 }
2190
2191 /*
2192 * NOTE: We are able to derive ce_id from irq because we
2193 * use a one-to-one mapping for CE's 0..5.
2194 * CE's 6 & 7 do not use interrupts at all.
2195 *
2196 * This mapping must be kept in sync with the mapping
2197 * used by firmware.
2198 */
2199 tasklet_schedule(&ar_pci->pipe_info[ce_id].intr);
2200 return IRQ_HANDLED;
2201}
2202
2203static irqreturn_t ath10k_pci_msi_fw_handler(int irq, void *arg)
2204{
2205 struct ath10k *ar = arg;
2206 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2207
2208 tasklet_schedule(&ar_pci->msi_fw_err);
2209 return IRQ_HANDLED;
2210}
2211
2212/*
2213 * Top-level interrupt handler for all PCI interrupts from a Target.
2214 * When a block of MSI interrupts is allocated, this top-level handler
2215 * is not used; instead, we directly call the correct sub-handler.
2216 */
2217static irqreturn_t ath10k_pci_interrupt_handler(int irq, void *arg)
2218{
2219 struct ath10k *ar = arg;
2220 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2221
2222 if (ar_pci->num_msi_intrs == 0) {
Michal Kaziore5398872013-11-25 14:06:20 +01002223 if (!ath10k_pci_irq_pending(ar))
2224 return IRQ_NONE;
2225
Michal Kazior26852182013-11-25 14:06:25 +01002226 ath10k_pci_disable_and_clear_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002227 }
2228
2229 tasklet_schedule(&ar_pci->intr_tq);
2230
2231 return IRQ_HANDLED;
2232}
2233
Michal Kaziorab977bd2013-11-25 14:06:26 +01002234static void ath10k_pci_early_irq_tasklet(unsigned long data)
2235{
2236 struct ath10k *ar = (struct ath10k *)data;
Michal Kaziorab977bd2013-11-25 14:06:26 +01002237 u32 fw_ind;
2238 int ret;
2239
2240 ret = ath10k_pci_wake(ar);
2241 if (ret) {
2242 ath10k_warn("failed to wake target in early irq tasklet: %d\n",
2243 ret);
2244 return;
2245 }
2246
Kalle Valob39712c2014-03-28 09:32:46 +02002247 fw_ind = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
Michal Kaziorab977bd2013-11-25 14:06:26 +01002248 if (fw_ind & FW_IND_EVENT_PENDING) {
Kalle Valob39712c2014-03-28 09:32:46 +02002249 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS,
Michal Kaziorab977bd2013-11-25 14:06:26 +01002250 fw_ind & ~FW_IND_EVENT_PENDING);
Michal Kazior1a4ab282014-05-14 16:56:16 +03002251 ath10k_pci_hif_dump_area(ar);
Michal Kaziorab977bd2013-11-25 14:06:26 +01002252 }
2253
2254 ath10k_pci_sleep(ar);
2255 ath10k_pci_enable_legacy_irq(ar);
2256}
2257
Kalle Valo5e3dd152013-06-12 20:52:10 +03002258static void ath10k_pci_tasklet(unsigned long data)
2259{
2260 struct ath10k *ar = (struct ath10k *)data;
2261 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2262
2263 ath10k_pci_fw_interrupt_handler(ar); /* FIXME: Handle FW error */
2264 ath10k_ce_per_engine_service_any(ar);
2265
Michal Kazior26852182013-11-25 14:06:25 +01002266 /* Re-enable legacy irq that was disabled in the irq handler */
2267 if (ar_pci->num_msi_intrs == 0)
2268 ath10k_pci_enable_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002269}
2270
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002271static int ath10k_pci_request_irq_msix(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002272{
2273 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002274 int ret, i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002275
2276 ret = request_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW,
2277 ath10k_pci_msi_fw_handler,
2278 IRQF_SHARED, "ath10k_pci", ar);
Michal Kazior591ecdb2013-07-31 10:55:15 +02002279 if (ret) {
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002280 ath10k_warn("failed to request MSI-X fw irq %d: %d\n",
Michal Kazior591ecdb2013-07-31 10:55:15 +02002281 ar_pci->pdev->irq + MSI_ASSIGN_FW, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002282 return ret;
Michal Kazior591ecdb2013-07-31 10:55:15 +02002283 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002284
2285 for (i = MSI_ASSIGN_CE_INITIAL; i <= MSI_ASSIGN_CE_MAX; i++) {
2286 ret = request_irq(ar_pci->pdev->irq + i,
2287 ath10k_pci_per_engine_handler,
2288 IRQF_SHARED, "ath10k_pci", ar);
2289 if (ret) {
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002290 ath10k_warn("failed to request MSI-X ce irq %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002291 ar_pci->pdev->irq + i, ret);
2292
Michal Kazior87b14232013-06-26 08:50:50 +02002293 for (i--; i >= MSI_ASSIGN_CE_INITIAL; i--)
2294 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002295
Michal Kazior87b14232013-06-26 08:50:50 +02002296 free_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002297 return ret;
2298 }
2299 }
2300
Kalle Valo5e3dd152013-06-12 20:52:10 +03002301 return 0;
2302}
2303
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002304static int ath10k_pci_request_irq_msi(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002305{
2306 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2307 int ret;
2308
2309 ret = request_irq(ar_pci->pdev->irq,
2310 ath10k_pci_interrupt_handler,
2311 IRQF_SHARED, "ath10k_pci", ar);
Kalle Valof3782742013-10-17 11:36:15 +03002312 if (ret) {
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002313 ath10k_warn("failed to request MSI irq %d: %d\n",
2314 ar_pci->pdev->irq, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002315 return ret;
Kalle Valof3782742013-10-17 11:36:15 +03002316 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002317
Kalle Valo5e3dd152013-06-12 20:52:10 +03002318 return 0;
2319}
2320
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002321static int ath10k_pci_request_irq_legacy(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002322{
2323 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002324 int ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002325
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002326 ret = request_irq(ar_pci->pdev->irq,
2327 ath10k_pci_interrupt_handler,
2328 IRQF_SHARED, "ath10k_pci", ar);
Kalle Valof3782742013-10-17 11:36:15 +03002329 if (ret) {
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002330 ath10k_warn("failed to request legacy irq %d: %d\n",
2331 ar_pci->pdev->irq, ret);
Kalle Valof3782742013-10-17 11:36:15 +03002332 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002333 }
2334
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002335 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002336}
2337
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002338static int ath10k_pci_request_irq(struct ath10k *ar)
2339{
2340 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2341
2342 switch (ar_pci->num_msi_intrs) {
2343 case 0:
2344 return ath10k_pci_request_irq_legacy(ar);
2345 case 1:
2346 return ath10k_pci_request_irq_msi(ar);
2347 case MSI_NUM_REQUEST:
2348 return ath10k_pci_request_irq_msix(ar);
2349 }
2350
2351 ath10k_warn("unknown irq configuration upon request\n");
2352 return -EINVAL;
2353}
2354
2355static void ath10k_pci_free_irq(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002356{
2357 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2358 int i;
2359
2360 /* There's at least one interrupt irregardless whether its legacy INTR
2361 * or MSI or MSI-X */
2362 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
2363 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002364}
2365
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002366static void ath10k_pci_init_irq_tasklets(struct ath10k *ar)
2367{
2368 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2369 int i;
2370
2371 tasklet_init(&ar_pci->intr_tq, ath10k_pci_tasklet, (unsigned long)ar);
2372 tasklet_init(&ar_pci->msi_fw_err, ath10k_msi_err_tasklet,
2373 (unsigned long)ar);
Michal Kaziorab977bd2013-11-25 14:06:26 +01002374 tasklet_init(&ar_pci->early_irq_tasklet, ath10k_pci_early_irq_tasklet,
2375 (unsigned long)ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002376
2377 for (i = 0; i < CE_COUNT; i++) {
2378 ar_pci->pipe_info[i].ar_pci = ar_pci;
2379 tasklet_init(&ar_pci->pipe_info[i].intr, ath10k_pci_ce_tasklet,
2380 (unsigned long)&ar_pci->pipe_info[i]);
2381 }
2382}
2383
2384static int ath10k_pci_init_irq(struct ath10k *ar)
2385{
2386 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002387 bool msix_supported = test_bit(ATH10K_PCI_FEATURE_MSI_X,
2388 ar_pci->features);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002389 int ret;
2390
2391 ath10k_pci_init_irq_tasklets(ar);
2392
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002393 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_AUTO &&
2394 !test_bit(ATH10K_FLAG_FIRST_BOOT_DONE, &ar->dev_flags))
2395 ath10k_info("limiting irq mode to: %d\n", ath10k_pci_irq_mode);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002396
2397 /* Try MSI-X */
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002398 if (ath10k_pci_irq_mode == ATH10K_PCI_IRQ_AUTO && msix_supported) {
2399 ar_pci->num_msi_intrs = MSI_NUM_REQUEST;
Alexander Gordeev5ad68672014-02-13 17:50:02 +02002400 ret = pci_enable_msi_range(ar_pci->pdev, ar_pci->num_msi_intrs,
2401 ar_pci->num_msi_intrs);
2402 if (ret > 0)
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002403 return 0;
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002404
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002405 /* fall-through */
2406 }
2407
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002408 /* Try MSI */
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002409 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_LEGACY) {
2410 ar_pci->num_msi_intrs = 1;
2411 ret = pci_enable_msi(ar_pci->pdev);
2412 if (ret == 0)
2413 return 0;
2414
2415 /* fall-through */
2416 }
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002417
2418 /* Try legacy irq
2419 *
2420 * A potential race occurs here: The CORE_BASE write
2421 * depends on target correctly decoding AXI address but
2422 * host won't know when target writes BAR to CORE_CTRL.
2423 * This write might get lost if target has NOT written BAR.
2424 * For now, fix the race by repeating the write in below
2425 * synchronization checking. */
2426 ar_pci->num_msi_intrs = 0;
2427
2428 ret = ath10k_pci_wake(ar);
2429 if (ret) {
2430 ath10k_warn("failed to wake target: %d\n", ret);
2431 return ret;
2432 }
2433
2434 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2435 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
2436 ath10k_pci_sleep(ar);
2437
2438 return 0;
2439}
2440
2441static int ath10k_pci_deinit_irq_legacy(struct ath10k *ar)
2442{
2443 int ret;
2444
2445 ret = ath10k_pci_wake(ar);
2446 if (ret) {
2447 ath10k_warn("failed to wake target: %d\n", ret);
2448 return ret;
2449 }
2450
2451 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2452 0);
2453 ath10k_pci_sleep(ar);
2454
2455 return 0;
2456}
2457
2458static int ath10k_pci_deinit_irq(struct ath10k *ar)
2459{
2460 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2461
2462 switch (ar_pci->num_msi_intrs) {
2463 case 0:
2464 return ath10k_pci_deinit_irq_legacy(ar);
2465 case 1:
2466 /* fall-through */
2467 case MSI_NUM_REQUEST:
2468 pci_disable_msi(ar_pci->pdev);
2469 return 0;
Alexander Gordeevbb8b6212014-02-13 17:50:01 +02002470 default:
2471 pci_disable_msi(ar_pci->pdev);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002472 }
2473
2474 ath10k_warn("unknown irq configuration upon deinit\n");
2475 return -EINVAL;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002476}
2477
Michal Kaziord7fb47f2013-11-08 08:01:26 +01002478static int ath10k_pci_wait_for_target_init(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002479{
2480 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo0399eca2014-03-28 09:32:21 +02002481 unsigned long timeout;
Kalle Valof3782742013-10-17 11:36:15 +03002482 int ret;
Kalle Valo0399eca2014-03-28 09:32:21 +02002483 u32 val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002484
Kalle Valo50f87a62014-03-28 09:32:52 +02002485 ath10k_dbg(ATH10K_DBG_BOOT, "boot waiting target to initialise\n");
2486
Michal Kazior98563d52013-11-08 08:01:33 +01002487 ret = ath10k_pci_wake(ar);
Kalle Valof3782742013-10-17 11:36:15 +03002488 if (ret) {
Kalle Valo0399eca2014-03-28 09:32:21 +02002489 ath10k_err("failed to wake up target for init: %d\n", ret);
Kalle Valof3782742013-10-17 11:36:15 +03002490 return ret;
2491 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002492
Kalle Valo0399eca2014-03-28 09:32:21 +02002493 timeout = jiffies + msecs_to_jiffies(ATH10K_PCI_TARGET_WAIT);
2494
2495 do {
2496 val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
2497
Kalle Valo50f87a62014-03-28 09:32:52 +02002498 ath10k_dbg(ATH10K_DBG_BOOT, "boot target indicator %x\n", val);
2499
Kalle Valo0399eca2014-03-28 09:32:21 +02002500 /* target should never return this */
2501 if (val == 0xffffffff)
2502 continue;
2503
Michal Kazior7710cd22014-04-23 19:30:04 +03002504 /* the device has crashed so don't bother trying anymore */
2505 if (val & FW_IND_EVENT_PENDING)
2506 break;
2507
Kalle Valo0399eca2014-03-28 09:32:21 +02002508 if (val & FW_IND_INITIALIZED)
2509 break;
2510
Kalle Valo5e3dd152013-06-12 20:52:10 +03002511 if (ar_pci->num_msi_intrs == 0)
2512 /* Fix potential race by repeating CORE_BASE writes */
Kalle Valo0399eca2014-03-28 09:32:21 +02002513 ath10k_pci_soc_write32(ar, PCIE_INTR_ENABLE_ADDRESS,
2514 PCIE_INTR_FIRMWARE_MASK |
2515 PCIE_INTR_CE_MASK_ALL);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002516
Kalle Valo0399eca2014-03-28 09:32:21 +02002517 mdelay(10);
2518 } while (time_before(jiffies, timeout));
2519
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002520 if (val == 0xffffffff) {
2521 ath10k_err("failed to read device register, device is gone\n");
2522 ret = -EIO;
2523 goto out;
2524 }
2525
Michal Kazior7710cd22014-04-23 19:30:04 +03002526 if (val & FW_IND_EVENT_PENDING) {
2527 ath10k_warn("device has crashed during init\n");
Michal Kazior1a4ab282014-05-14 16:56:16 +03002528 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS,
2529 val & ~FW_IND_EVENT_PENDING);
2530 ath10k_pci_hif_dump_area(ar);
Michal Kazior7710cd22014-04-23 19:30:04 +03002531 ret = -ECOMM;
2532 goto out;
2533 }
2534
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002535 if (!(val & FW_IND_INITIALIZED)) {
Kalle Valo0399eca2014-03-28 09:32:21 +02002536 ath10k_err("failed to receive initialized event from target: %08x\n",
2537 val);
2538 ret = -ETIMEDOUT;
Michal Kazior5b2589f2013-11-08 08:01:30 +01002539 goto out;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002540 }
2541
Kalle Valo50f87a62014-03-28 09:32:52 +02002542 ath10k_dbg(ATH10K_DBG_BOOT, "boot target initialised\n");
2543
Michal Kazior5b2589f2013-11-08 08:01:30 +01002544out:
Michal Kazior98563d52013-11-08 08:01:33 +01002545 ath10k_pci_sleep(ar);
Michal Kazior5b2589f2013-11-08 08:01:30 +01002546 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002547}
2548
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002549static int ath10k_pci_cold_reset(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002550{
Michal Kazior5b2589f2013-11-08 08:01:30 +01002551 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002552 u32 val;
2553
Kalle Valo50f87a62014-03-28 09:32:52 +02002554 ath10k_dbg(ATH10K_DBG_BOOT, "boot cold reset\n");
2555
Michal Kazior5b2589f2013-11-08 08:01:30 +01002556 ret = ath10k_do_pci_wake(ar);
2557 if (ret) {
2558 ath10k_err("failed to wake up target: %d\n",
2559 ret);
2560 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002561 }
2562
2563 /* Put Target, including PCIe, into RESET. */
Kalle Valoe479ed42013-09-01 10:01:53 +03002564 val = ath10k_pci_reg_read32(ar, SOC_GLOBAL_RESET_ADDRESS);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002565 val |= 1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002566 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002567
2568 for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
Kalle Valoe479ed42013-09-01 10:01:53 +03002569 if (ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03002570 RTC_STATE_COLD_RESET_MASK)
2571 break;
2572 msleep(1);
2573 }
2574
2575 /* Pull Target, including PCIe, out of RESET. */
2576 val &= ~1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002577 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002578
2579 for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
Kalle Valoe479ed42013-09-01 10:01:53 +03002580 if (!(ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03002581 RTC_STATE_COLD_RESET_MASK))
2582 break;
2583 msleep(1);
2584 }
2585
Michal Kazior5b2589f2013-11-08 08:01:30 +01002586 ath10k_do_pci_sleep(ar);
Kalle Valo50f87a62014-03-28 09:32:52 +02002587
2588 ath10k_dbg(ATH10K_DBG_BOOT, "boot cold reset complete\n");
2589
Michal Kazior5b2589f2013-11-08 08:01:30 +01002590 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002591}
2592
2593static void ath10k_pci_dump_features(struct ath10k_pci *ar_pci)
2594{
2595 int i;
2596
2597 for (i = 0; i < ATH10K_PCI_FEATURE_COUNT; i++) {
2598 if (!test_bit(i, ar_pci->features))
2599 continue;
2600
2601 switch (i) {
2602 case ATH10K_PCI_FEATURE_MSI_X:
Kalle Valo24cfade2013-09-08 17:55:50 +03002603 ath10k_dbg(ATH10K_DBG_BOOT, "device supports MSI-X\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002604 break;
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002605 case ATH10K_PCI_FEATURE_SOC_POWER_SAVE:
Kalle Valo24cfade2013-09-08 17:55:50 +03002606 ath10k_dbg(ATH10K_DBG_BOOT, "QCA98XX SoC power save enabled\n");
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002607 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002608 }
2609 }
2610}
2611
2612static int ath10k_pci_probe(struct pci_dev *pdev,
2613 const struct pci_device_id *pci_dev)
2614{
2615 void __iomem *mem;
2616 int ret = 0;
2617 struct ath10k *ar;
2618 struct ath10k_pci *ar_pci;
Kalle Valoe01ae682013-09-01 11:22:14 +03002619 u32 lcr_val, chip_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002620
Kalle Valo50f87a62014-03-28 09:32:52 +02002621 ath10k_dbg(ATH10K_DBG_PCI, "pci probe\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002622
2623 ar_pci = kzalloc(sizeof(*ar_pci), GFP_KERNEL);
2624 if (ar_pci == NULL)
2625 return -ENOMEM;
2626
2627 ar_pci->pdev = pdev;
2628 ar_pci->dev = &pdev->dev;
2629
2630 switch (pci_dev->device) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03002631 case QCA988X_2_0_DEVICE_ID:
2632 set_bit(ATH10K_PCI_FEATURE_MSI_X, ar_pci->features);
2633 break;
2634 default:
2635 ret = -ENODEV;
Masanari Iida6d3be302013-09-30 23:19:09 +09002636 ath10k_err("Unknown device ID: %d\n", pci_dev->device);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002637 goto err_ar_pci;
2638 }
2639
Kalle Valoe42c1fb2014-03-28 09:32:33 +02002640 if (ath10k_pci_target_ps)
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002641 set_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE, ar_pci->features);
2642
Kalle Valo5e3dd152013-06-12 20:52:10 +03002643 ath10k_pci_dump_features(ar_pci);
2644
Michal Kazior3a0861f2013-07-05 16:15:06 +03002645 ar = ath10k_core_create(ar_pci, ar_pci->dev, &ath10k_pci_hif_ops);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002646 if (!ar) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002647 ath10k_err("failed to create driver core\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002648 ret = -EINVAL;
2649 goto err_ar_pci;
2650 }
2651
Kalle Valo5e3dd152013-06-12 20:52:10 +03002652 ar_pci->ar = ar;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002653 atomic_set(&ar_pci->keep_awake_count, 0);
2654
2655 pci_set_drvdata(pdev, ar);
2656
Kalle Valo5e3dd152013-06-12 20:52:10 +03002657 ret = pci_enable_device(pdev);
2658 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002659 ath10k_err("failed to enable PCI device: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002660 goto err_ar;
2661 }
2662
2663 /* Request MMIO resources */
2664 ret = pci_request_region(pdev, BAR_NUM, "ath");
2665 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002666 ath10k_err("failed to request MMIO region: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002667 goto err_device;
2668 }
2669
2670 /*
2671 * Target structures have a limit of 32 bit DMA pointers.
2672 * DMA pointers can be wider than 32 bits by default on some systems.
2673 */
2674 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2675 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002676 ath10k_err("failed to set DMA mask to 32-bit: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002677 goto err_region;
2678 }
2679
2680 ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2681 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002682 ath10k_err("failed to set consistent DMA mask to 32-bit\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002683 goto err_region;
2684 }
2685
2686 /* Set bus master bit in PCI_COMMAND to enable DMA */
2687 pci_set_master(pdev);
2688
2689 /*
2690 * Temporary FIX: disable ASPM
2691 * Will be removed after the OTP is programmed
2692 */
2693 pci_read_config_dword(pdev, 0x80, &lcr_val);
2694 pci_write_config_dword(pdev, 0x80, (lcr_val & 0xffffff00));
2695
2696 /* Arrange for access to Target SoC registers. */
2697 mem = pci_iomap(pdev, BAR_NUM, 0);
2698 if (!mem) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002699 ath10k_err("failed to perform IOMAP for BAR%d\n", BAR_NUM);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002700 ret = -EIO;
2701 goto err_master;
2702 }
2703
2704 ar_pci->mem = mem;
2705
2706 spin_lock_init(&ar_pci->ce_lock);
2707
Kalle Valoe01ae682013-09-01 11:22:14 +03002708 ret = ath10k_do_pci_wake(ar);
2709 if (ret) {
2710 ath10k_err("Failed to get chip id: %d\n", ret);
Wei Yongjun12eb0872013-10-30 13:24:39 +08002711 goto err_iomap;
Kalle Valoe01ae682013-09-01 11:22:14 +03002712 }
2713
Kalle Valo233eb972013-10-16 16:46:11 +03002714 chip_id = ath10k_pci_soc_read32(ar, SOC_CHIP_ID_ADDRESS);
Kalle Valoe01ae682013-09-01 11:22:14 +03002715
2716 ath10k_do_pci_sleep(ar);
2717
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002718 ret = ath10k_pci_alloc_ce(ar);
2719 if (ret) {
2720 ath10k_err("failed to allocate copy engine pipes: %d\n", ret);
2721 goto err_iomap;
2722 }
2723
Kalle Valo24cfade2013-09-08 17:55:50 +03002724 ath10k_dbg(ATH10K_DBG_BOOT, "boot pci_mem 0x%p\n", ar_pci->mem);
2725
Kalle Valoe01ae682013-09-01 11:22:14 +03002726 ret = ath10k_core_register(ar, chip_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002727 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002728 ath10k_err("failed to register driver core: %d\n", ret);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002729 goto err_free_ce;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002730 }
2731
2732 return 0;
2733
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002734err_free_ce:
2735 ath10k_pci_free_ce(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002736err_iomap:
2737 pci_iounmap(pdev, mem);
2738err_master:
2739 pci_clear_master(pdev);
2740err_region:
2741 pci_release_region(pdev, BAR_NUM);
2742err_device:
2743 pci_disable_device(pdev);
2744err_ar:
Kalle Valo5e3dd152013-06-12 20:52:10 +03002745 ath10k_core_destroy(ar);
2746err_ar_pci:
2747 /* call HIF PCI free here */
2748 kfree(ar_pci);
2749
2750 return ret;
2751}
2752
2753static void ath10k_pci_remove(struct pci_dev *pdev)
2754{
2755 struct ath10k *ar = pci_get_drvdata(pdev);
2756 struct ath10k_pci *ar_pci;
2757
Kalle Valo50f87a62014-03-28 09:32:52 +02002758 ath10k_dbg(ATH10K_DBG_PCI, "pci remove\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002759
2760 if (!ar)
2761 return;
2762
2763 ar_pci = ath10k_pci_priv(ar);
2764
2765 if (!ar_pci)
2766 return;
2767
Kalle Valo5e3dd152013-06-12 20:52:10 +03002768 ath10k_core_unregister(ar);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002769 ath10k_pci_free_ce(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002770
Kalle Valo5e3dd152013-06-12 20:52:10 +03002771 pci_iounmap(pdev, ar_pci->mem);
2772 pci_release_region(pdev, BAR_NUM);
2773 pci_clear_master(pdev);
2774 pci_disable_device(pdev);
2775
2776 ath10k_core_destroy(ar);
2777 kfree(ar_pci);
2778}
2779
Kalle Valo5e3dd152013-06-12 20:52:10 +03002780MODULE_DEVICE_TABLE(pci, ath10k_pci_id_table);
2781
2782static struct pci_driver ath10k_pci_driver = {
2783 .name = "ath10k_pci",
2784 .id_table = ath10k_pci_id_table,
2785 .probe = ath10k_pci_probe,
2786 .remove = ath10k_pci_remove,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002787};
2788
2789static int __init ath10k_pci_init(void)
2790{
2791 int ret;
2792
2793 ret = pci_register_driver(&ath10k_pci_driver);
2794 if (ret)
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002795 ath10k_err("failed to register PCI driver: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002796
2797 return ret;
2798}
2799module_init(ath10k_pci_init);
2800
2801static void __exit ath10k_pci_exit(void)
2802{
2803 pci_unregister_driver(&ath10k_pci_driver);
2804}
2805
2806module_exit(ath10k_pci_exit);
2807
2808MODULE_AUTHOR("Qualcomm Atheros");
2809MODULE_DESCRIPTION("Driver support for Atheros QCA988X PCIe devices");
2810MODULE_LICENSE("Dual BSD/GPL");
Kalle Valo929417c2014-03-28 09:32:39 +02002811MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_FW_2_FILE);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002812MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_BOARD_DATA_FILE);