blob: 7d72b9cfe0bed2b3fbdc2609343d241c1b5ba219 [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;
765 unsigned int nentries_mask = src_ring->nentries_mask;
766 unsigned int sw_index = src_ring->sw_index;
767 unsigned int write_index = src_ring->write_index;
768 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 Kazior726346f2014-02-27 18:50:04 +0200772 if (unlikely(CE_RING_DELTA(nentries_mask,
773 write_index, sw_index - 1) < n_items)) {
774 err = -ENOBUFS;
775 goto unlock;
776 }
777
778 for (i = 0; i < n_items - 1; i++) {
779 ath10k_dbg(ATH10K_DBG_PCI,
780 "pci tx item %d paddr 0x%08x len %d n_items %d\n",
781 i, items[i].paddr, items[i].len, n_items);
782 ath10k_dbg_dump(ATH10K_DBG_PCI_DUMP, NULL, "item data: ",
783 items[i].vaddr, items[i].len);
784
785 err = ath10k_ce_send_nolock(ce_pipe,
786 items[i].transfer_context,
787 items[i].paddr,
788 items[i].len,
789 items[i].transfer_id,
790 CE_SEND_FLAG_GATHER);
791 if (err)
792 goto unlock;
793 }
794
795 /* `i` is equal to `n_items -1` after for() */
Kalle Valo5e3dd152013-06-12 20:52:10 +0300796
797 ath10k_dbg(ATH10K_DBG_PCI,
Michal Kazior726346f2014-02-27 18:50:04 +0200798 "pci tx item %d paddr 0x%08x len %d n_items %d\n",
799 i, items[i].paddr, items[i].len, n_items);
800 ath10k_dbg_dump(ATH10K_DBG_PCI_DUMP, NULL, "item data: ",
801 items[i].vaddr, items[i].len);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300802
Michal Kazior726346f2014-02-27 18:50:04 +0200803 err = ath10k_ce_send_nolock(ce_pipe,
804 items[i].transfer_context,
805 items[i].paddr,
806 items[i].len,
807 items[i].transfer_id,
808 0);
809 if (err)
810 goto unlock;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300811
Michal Kazior726346f2014-02-27 18:50:04 +0200812 err = 0;
813unlock:
814 spin_unlock_bh(&ar_pci->ce_lock);
815 return err;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300816}
817
818static u16 ath10k_pci_hif_get_free_queue_number(struct ath10k *ar, u8 pipe)
819{
820 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo50f87a62014-03-28 09:32:52 +0200821
822 ath10k_dbg(ATH10K_DBG_PCI, "pci hif get free queue number\n");
823
Michal Kazior3efcb3b2013-10-02 11:03:41 +0200824 return ath10k_ce_num_free_src_entries(ar_pci->pipe_info[pipe].ce_hdl);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300825}
826
827static void ath10k_pci_hif_dump_area(struct ath10k *ar)
828{
829 u32 reg_dump_area = 0;
830 u32 reg_dump_values[REG_DUMP_COUNT_QCA988X] = {};
831 u32 host_addr;
832 int ret;
833 u32 i;
834
835 ath10k_err("firmware crashed!\n");
836 ath10k_err("hardware name %s version 0x%x\n",
837 ar->hw_params.name, ar->target_version);
Chun-Yeow Yeoh5ba88b32014-01-21 17:21:21 +0800838 ath10k_err("firmware version: %s\n", ar->hw->wiphy->fw_version);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300839
840 host_addr = host_interest_item_address(HI_ITEM(hi_failure_state));
Michal Kazior1d2b48d2013-11-08 08:01:34 +0100841 ret = ath10k_pci_diag_read_mem(ar, host_addr,
842 &reg_dump_area, sizeof(u32));
843 if (ret) {
844 ath10k_err("failed to read FW dump area address: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300845 return;
846 }
847
848 ath10k_err("target register Dump Location: 0x%08X\n", reg_dump_area);
849
850 ret = ath10k_pci_diag_read_mem(ar, reg_dump_area,
851 &reg_dump_values[0],
852 REG_DUMP_COUNT_QCA988X * sizeof(u32));
853 if (ret != 0) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +0100854 ath10k_err("failed to read FW dump area: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300855 return;
856 }
857
858 BUILD_BUG_ON(REG_DUMP_COUNT_QCA988X % 4);
859
860 ath10k_err("target Register Dump\n");
861 for (i = 0; i < REG_DUMP_COUNT_QCA988X; i += 4)
862 ath10k_err("[%02d]: 0x%08X 0x%08X 0x%08X 0x%08X\n",
863 i,
864 reg_dump_values[i],
865 reg_dump_values[i + 1],
866 reg_dump_values[i + 2],
867 reg_dump_values[i + 3]);
Michal Kazioraffd3212013-07-16 09:54:35 +0200868
Michal Kazior5e90de82013-10-16 16:46:05 +0300869 queue_work(ar->workqueue, &ar->restart_work);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300870}
871
872static void ath10k_pci_hif_send_complete_check(struct ath10k *ar, u8 pipe,
873 int force)
874{
Kalle Valo50f87a62014-03-28 09:32:52 +0200875 ath10k_dbg(ATH10K_DBG_PCI, "pci hif send complete check\n");
876
Kalle Valo5e3dd152013-06-12 20:52:10 +0300877 if (!force) {
878 int resources;
879 /*
880 * Decide whether to actually poll for completions, or just
881 * wait for a later chance.
882 * If there seem to be plenty of resources left, then just wait
883 * since checking involves reading a CE register, which is a
884 * relatively expensive operation.
885 */
886 resources = ath10k_pci_hif_get_free_queue_number(ar, pipe);
887
888 /*
889 * If at least 50% of the total resources are still available,
890 * don't bother checking again yet.
891 */
892 if (resources > (host_ce_config_wlan[pipe].src_nentries >> 1))
893 return;
894 }
895 ath10k_ce_per_engine_service(ar, pipe);
896}
897
Michal Kaziore799bbf2013-07-05 16:15:12 +0300898static void ath10k_pci_hif_set_callbacks(struct ath10k *ar,
899 struct ath10k_hif_cb *callbacks)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300900{
901 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
902
Kalle Valo50f87a62014-03-28 09:32:52 +0200903 ath10k_dbg(ATH10K_DBG_PCI, "pci hif set callbacks\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +0300904
905 memcpy(&ar_pci->msg_callbacks_current, callbacks,
906 sizeof(ar_pci->msg_callbacks_current));
907}
908
Michal Kaziorc80de122013-11-25 14:06:23 +0100909static int ath10k_pci_setup_ce_irq(struct ath10k *ar)
910{
911 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
912 const struct ce_attr *attr;
913 struct ath10k_pci_pipe *pipe_info;
914 int pipe_num, disable_interrupts;
915
916 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
917 pipe_info = &ar_pci->pipe_info[pipe_num];
918
919 /* Handle Diagnostic CE specially */
920 if (pipe_info->ce_hdl == ar_pci->ce_diag)
921 continue;
922
923 attr = &host_ce_config_wlan[pipe_num];
924
925 if (attr->src_nentries) {
926 disable_interrupts = attr->flags & CE_ATTR_DIS_INTR;
927 ath10k_ce_send_cb_register(pipe_info->ce_hdl,
928 ath10k_pci_ce_send_done,
929 disable_interrupts);
930 }
931
932 if (attr->dest_nentries)
933 ath10k_ce_recv_cb_register(pipe_info->ce_hdl,
934 ath10k_pci_ce_recv_data);
935 }
936
937 return 0;
938}
939
Michal Kazior96a9d0d2013-11-08 08:01:25 +0100940static void ath10k_pci_kill_tasklet(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300941{
942 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300943 int i;
944
Kalle Valo5e3dd152013-06-12 20:52:10 +0300945 tasklet_kill(&ar_pci->intr_tq);
Michal Kazior103d4f52013-11-08 08:01:24 +0100946 tasklet_kill(&ar_pci->msi_fw_err);
Michal Kaziorab977bd2013-11-25 14:06:26 +0100947 tasklet_kill(&ar_pci->early_irq_tasklet);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300948
949 for (i = 0; i < CE_COUNT; i++)
950 tasklet_kill(&ar_pci->pipe_info[i].intr);
Michal Kazior96a9d0d2013-11-08 08:01:25 +0100951}
952
Kalle Valo5e3dd152013-06-12 20:52:10 +0300953/* TODO - temporary mapping while we have too few CE's */
954static int ath10k_pci_hif_map_service_to_pipe(struct ath10k *ar,
955 u16 service_id, u8 *ul_pipe,
956 u8 *dl_pipe, int *ul_is_polled,
957 int *dl_is_polled)
958{
959 int ret = 0;
960
Kalle Valo50f87a62014-03-28 09:32:52 +0200961 ath10k_dbg(ATH10K_DBG_PCI, "pci hif map service\n");
962
Kalle Valo5e3dd152013-06-12 20:52:10 +0300963 /* polling for received messages not supported */
964 *dl_is_polled = 0;
965
966 switch (service_id) {
967 case ATH10K_HTC_SVC_ID_HTT_DATA_MSG:
968 /*
969 * Host->target HTT gets its own pipe, so it can be polled
970 * while other pipes are interrupt driven.
971 */
972 *ul_pipe = 4;
973 /*
974 * Use the same target->host pipe for HTC ctrl, HTC raw
975 * streams, and HTT.
976 */
977 *dl_pipe = 1;
978 break;
979
980 case ATH10K_HTC_SVC_ID_RSVD_CTRL:
981 case ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS:
982 /*
983 * Note: HTC_RAW_STREAMS_SVC is currently unused, and
984 * HTC_CTRL_RSVD_SVC could share the same pipe as the
985 * WMI services. So, if another CE is needed, change
986 * this to *ul_pipe = 3, which frees up CE 0.
987 */
988 /* *ul_pipe = 3; */
989 *ul_pipe = 0;
990 *dl_pipe = 1;
991 break;
992
993 case ATH10K_HTC_SVC_ID_WMI_DATA_BK:
994 case ATH10K_HTC_SVC_ID_WMI_DATA_BE:
995 case ATH10K_HTC_SVC_ID_WMI_DATA_VI:
996 case ATH10K_HTC_SVC_ID_WMI_DATA_VO:
997
998 case ATH10K_HTC_SVC_ID_WMI_CONTROL:
999 *ul_pipe = 3;
1000 *dl_pipe = 2;
1001 break;
1002
1003 /* pipe 5 unused */
1004 /* pipe 6 reserved */
1005 /* pipe 7 reserved */
1006
1007 default:
1008 ret = -1;
1009 break;
1010 }
1011 *ul_is_polled =
1012 (host_ce_config_wlan[*ul_pipe].flags & CE_ATTR_DIS_INTR) != 0;
1013
1014 return ret;
1015}
1016
1017static void ath10k_pci_hif_get_default_pipe(struct ath10k *ar,
1018 u8 *ul_pipe, u8 *dl_pipe)
1019{
1020 int ul_is_polled, dl_is_polled;
1021
Kalle Valo50f87a62014-03-28 09:32:52 +02001022 ath10k_dbg(ATH10K_DBG_PCI, "pci hif get default pipe\n");
1023
Kalle Valo5e3dd152013-06-12 20:52:10 +03001024 (void)ath10k_pci_hif_map_service_to_pipe(ar,
1025 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1026 ul_pipe,
1027 dl_pipe,
1028 &ul_is_polled,
1029 &dl_is_polled);
1030}
1031
Michal Kazior87263e52013-08-27 13:08:01 +02001032static int ath10k_pci_post_rx_pipe(struct ath10k_pci_pipe *pipe_info,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001033 int num)
1034{
1035 struct ath10k *ar = pipe_info->hif_ce_state;
1036 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2aa39112013-08-27 13:08:02 +02001037 struct ath10k_ce_pipe *ce_state = pipe_info->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001038 struct sk_buff *skb;
1039 dma_addr_t ce_data;
1040 int i, ret = 0;
1041
1042 if (pipe_info->buf_sz == 0)
1043 return 0;
1044
1045 for (i = 0; i < num; i++) {
1046 skb = dev_alloc_skb(pipe_info->buf_sz);
1047 if (!skb) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001048 ath10k_warn("failed to allocate skbuff for pipe %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001049 num);
1050 ret = -ENOMEM;
1051 goto err;
1052 }
1053
1054 WARN_ONCE((unsigned long)skb->data & 3, "unaligned skb");
1055
1056 ce_data = dma_map_single(ar->dev, skb->data,
1057 skb->len + skb_tailroom(skb),
1058 DMA_FROM_DEVICE);
1059
1060 if (unlikely(dma_mapping_error(ar->dev, ce_data))) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001061 ath10k_warn("failed to DMA map sk_buff\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001062 dev_kfree_skb_any(skb);
1063 ret = -EIO;
1064 goto err;
1065 }
1066
1067 ATH10K_SKB_CB(skb)->paddr = ce_data;
1068
1069 pci_dma_sync_single_for_device(ar_pci->pdev, ce_data,
1070 pipe_info->buf_sz,
1071 PCI_DMA_FROMDEVICE);
1072
1073 ret = ath10k_ce_recv_buf_enqueue(ce_state, (void *)skb,
1074 ce_data);
1075 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001076 ath10k_warn("failed to enqueue to pipe %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001077 num, ret);
1078 goto err;
1079 }
1080 }
1081
1082 return ret;
1083
1084err:
1085 ath10k_pci_rx_pipe_cleanup(pipe_info);
1086 return ret;
1087}
1088
1089static int ath10k_pci_post_rx(struct ath10k *ar)
1090{
1091 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +02001092 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001093 const struct ce_attr *attr;
1094 int pipe_num, ret = 0;
1095
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001096 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001097 pipe_info = &ar_pci->pipe_info[pipe_num];
1098 attr = &host_ce_config_wlan[pipe_num];
1099
1100 if (attr->dest_nentries == 0)
1101 continue;
1102
1103 ret = ath10k_pci_post_rx_pipe(pipe_info,
1104 attr->dest_nentries - 1);
1105 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001106 ath10k_warn("failed to post RX buffer for pipe %d: %d\n",
1107 pipe_num, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001108
1109 for (; pipe_num >= 0; pipe_num--) {
1110 pipe_info = &ar_pci->pipe_info[pipe_num];
1111 ath10k_pci_rx_pipe_cleanup(pipe_info);
1112 }
1113 return ret;
1114 }
1115 }
1116
1117 return 0;
1118}
1119
1120static int ath10k_pci_hif_start(struct ath10k *ar)
1121{
1122 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorab977bd2013-11-25 14:06:26 +01001123 int ret, ret_early;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001124
Kalle Valo50f87a62014-03-28 09:32:52 +02001125 ath10k_dbg(ATH10K_DBG_BOOT, "boot hif start\n");
1126
Michal Kaziorab977bd2013-11-25 14:06:26 +01001127 ath10k_pci_free_early_irq(ar);
1128 ath10k_pci_kill_tasklet(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001129
Michal Kazior5d1aa942013-11-25 14:06:24 +01001130 ret = ath10k_pci_request_irq(ar);
1131 if (ret) {
1132 ath10k_warn("failed to post RX buffers for all pipes: %d\n",
1133 ret);
Michal Kazior2f5280d2014-02-27 18:50:05 +02001134 goto err_early_irq;
Michal Kazior5d1aa942013-11-25 14:06:24 +01001135 }
1136
Michal Kaziorc80de122013-11-25 14:06:23 +01001137 ret = ath10k_pci_setup_ce_irq(ar);
1138 if (ret) {
1139 ath10k_warn("failed to setup CE interrupts: %d\n", ret);
Michal Kazior5d1aa942013-11-25 14:06:24 +01001140 goto err_stop;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001141 }
1142
1143 /* Post buffers once to start things off. */
1144 ret = ath10k_pci_post_rx(ar);
1145 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001146 ath10k_warn("failed to post RX buffers for all pipes: %d\n",
1147 ret);
Michal Kazior5d1aa942013-11-25 14:06:24 +01001148 goto err_stop;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001149 }
1150
1151 ar_pci->started = 1;
1152 return 0;
Michal Kaziorc80de122013-11-25 14:06:23 +01001153
Michal Kazior5d1aa942013-11-25 14:06:24 +01001154err_stop:
1155 ath10k_ce_disable_interrupts(ar);
1156 ath10k_pci_free_irq(ar);
1157 ath10k_pci_kill_tasklet(ar);
Michal Kaziorab977bd2013-11-25 14:06:26 +01001158err_early_irq:
1159 /* Though there should be no interrupts (device was reset)
1160 * power_down() expects the early IRQ to be installed as per the
1161 * driver lifecycle. */
1162 ret_early = ath10k_pci_request_early_irq(ar);
1163 if (ret_early)
1164 ath10k_warn("failed to re-enable early irq: %d\n", ret_early);
1165
Michal Kaziorc80de122013-11-25 14:06:23 +01001166 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001167}
1168
Michal Kazior87263e52013-08-27 13:08:01 +02001169static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe *pipe_info)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001170{
1171 struct ath10k *ar;
1172 struct ath10k_pci *ar_pci;
Michal Kazior2aa39112013-08-27 13:08:02 +02001173 struct ath10k_ce_pipe *ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001174 u32 buf_sz;
1175 struct sk_buff *netbuf;
1176 u32 ce_data;
1177
1178 buf_sz = pipe_info->buf_sz;
1179
1180 /* Unused Copy Engine */
1181 if (buf_sz == 0)
1182 return;
1183
1184 ar = pipe_info->hif_ce_state;
1185 ar_pci = ath10k_pci_priv(ar);
1186
1187 if (!ar_pci->started)
1188 return;
1189
1190 ce_hdl = pipe_info->ce_hdl;
1191
1192 while (ath10k_ce_revoke_recv_next(ce_hdl, (void **)&netbuf,
1193 &ce_data) == 0) {
1194 dma_unmap_single(ar->dev, ATH10K_SKB_CB(netbuf)->paddr,
1195 netbuf->len + skb_tailroom(netbuf),
1196 DMA_FROM_DEVICE);
1197 dev_kfree_skb_any(netbuf);
1198 }
1199}
1200
Michal Kazior87263e52013-08-27 13:08:01 +02001201static void ath10k_pci_tx_pipe_cleanup(struct ath10k_pci_pipe *pipe_info)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001202{
1203 struct ath10k *ar;
1204 struct ath10k_pci *ar_pci;
Michal Kazior2aa39112013-08-27 13:08:02 +02001205 struct ath10k_ce_pipe *ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001206 struct sk_buff *netbuf;
1207 u32 ce_data;
1208 unsigned int nbytes;
1209 unsigned int id;
1210 u32 buf_sz;
1211
1212 buf_sz = pipe_info->buf_sz;
1213
1214 /* Unused Copy Engine */
1215 if (buf_sz == 0)
1216 return;
1217
1218 ar = pipe_info->hif_ce_state;
1219 ar_pci = ath10k_pci_priv(ar);
1220
1221 if (!ar_pci->started)
1222 return;
1223
1224 ce_hdl = pipe_info->ce_hdl;
1225
1226 while (ath10k_ce_cancel_send_next(ce_hdl, (void **)&netbuf,
1227 &ce_data, &nbytes, &id) == 0) {
Michal Kaziora16942e2014-02-27 18:50:04 +02001228 /* no need to call tx completion for NULL pointers */
1229 if (!netbuf)
Michal Kazior2415fc12013-11-08 08:01:32 +01001230 continue;
Michal Kazior2415fc12013-11-08 08:01:32 +01001231
Kalle Valoe9bb0aa2013-09-08 18:36:11 +03001232 ar_pci->msg_callbacks_current.tx_completion(ar,
1233 netbuf,
1234 id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001235 }
1236}
1237
1238/*
1239 * Cleanup residual buffers for device shutdown:
1240 * buffers that were enqueued for receive
1241 * buffers that were to be sent
1242 * Note: Buffers that had completed but which were
1243 * not yet processed are on a completion queue. They
1244 * are handled when the completion thread shuts down.
1245 */
1246static void ath10k_pci_buffer_cleanup(struct ath10k *ar)
1247{
1248 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1249 int pipe_num;
1250
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001251 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Michal Kazior87263e52013-08-27 13:08:01 +02001252 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001253
1254 pipe_info = &ar_pci->pipe_info[pipe_num];
1255 ath10k_pci_rx_pipe_cleanup(pipe_info);
1256 ath10k_pci_tx_pipe_cleanup(pipe_info);
1257 }
1258}
1259
1260static void ath10k_pci_ce_deinit(struct ath10k *ar)
1261{
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001262 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001263
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001264 for (i = 0; i < CE_COUNT; i++)
1265 ath10k_ce_deinit_pipe(ar, i);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001266}
1267
1268static void ath10k_pci_hif_stop(struct ath10k *ar)
1269{
Michal Kazior32270b62013-08-02 09:15:47 +02001270 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior5d1aa942013-11-25 14:06:24 +01001271 int ret;
Michal Kazior32270b62013-08-02 09:15:47 +02001272
Kalle Valo50f87a62014-03-28 09:32:52 +02001273 ath10k_dbg(ATH10K_DBG_BOOT, "boot hif stop\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001274
Michal Kazior5d1aa942013-11-25 14:06:24 +01001275 ret = ath10k_ce_disable_interrupts(ar);
1276 if (ret)
1277 ath10k_warn("failed to disable CE interrupts: %d\n", ret);
Michal Kazior32270b62013-08-02 09:15:47 +02001278
Michal Kazior5d1aa942013-11-25 14:06:24 +01001279 ath10k_pci_free_irq(ar);
1280 ath10k_pci_kill_tasklet(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001281
Michal Kaziorab977bd2013-11-25 14:06:26 +01001282 ret = ath10k_pci_request_early_irq(ar);
1283 if (ret)
1284 ath10k_warn("failed to re-enable early irq: %d\n", ret);
1285
Kalle Valo5e3dd152013-06-12 20:52:10 +03001286 /* At this point, asynchronous threads are stopped, the target should
1287 * not DMA nor interrupt. We process the leftovers and then free
1288 * everything else up. */
1289
Kalle Valo5e3dd152013-06-12 20:52:10 +03001290 ath10k_pci_buffer_cleanup(ar);
Michal Kazior32270b62013-08-02 09:15:47 +02001291
Michal Kazior6a42a472013-11-08 08:01:35 +01001292 /* Make the sure the device won't access any structures on the host by
1293 * resetting it. The device was fed with PCI CE ringbuffer
1294 * configuration during init. If ringbuffers are freed and the device
1295 * were to access them this could lead to memory corruption on the
1296 * host. */
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001297 ath10k_pci_warm_reset(ar);
Michal Kazior6a42a472013-11-08 08:01:35 +01001298
Michal Kazior32270b62013-08-02 09:15:47 +02001299 ar_pci->started = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001300}
1301
1302static int ath10k_pci_hif_exchange_bmi_msg(struct ath10k *ar,
1303 void *req, u32 req_len,
1304 void *resp, u32 *resp_len)
1305{
1306 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2aa39112013-08-27 13:08:02 +02001307 struct ath10k_pci_pipe *pci_tx = &ar_pci->pipe_info[BMI_CE_NUM_TO_TARG];
1308 struct ath10k_pci_pipe *pci_rx = &ar_pci->pipe_info[BMI_CE_NUM_TO_HOST];
1309 struct ath10k_ce_pipe *ce_tx = pci_tx->ce_hdl;
1310 struct ath10k_ce_pipe *ce_rx = pci_rx->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001311 dma_addr_t req_paddr = 0;
1312 dma_addr_t resp_paddr = 0;
1313 struct bmi_xfer xfer = {};
1314 void *treq, *tresp = NULL;
1315 int ret = 0;
1316
Michal Kazior85622cd2013-11-25 14:06:22 +01001317 might_sleep();
1318
Kalle Valo5e3dd152013-06-12 20:52:10 +03001319 if (resp && !resp_len)
1320 return -EINVAL;
1321
1322 if (resp && resp_len && *resp_len == 0)
1323 return -EINVAL;
1324
1325 treq = kmemdup(req, req_len, GFP_KERNEL);
1326 if (!treq)
1327 return -ENOMEM;
1328
1329 req_paddr = dma_map_single(ar->dev, treq, req_len, DMA_TO_DEVICE);
1330 ret = dma_mapping_error(ar->dev, req_paddr);
1331 if (ret)
1332 goto err_dma;
1333
1334 if (resp && resp_len) {
1335 tresp = kzalloc(*resp_len, GFP_KERNEL);
1336 if (!tresp) {
1337 ret = -ENOMEM;
1338 goto err_req;
1339 }
1340
1341 resp_paddr = dma_map_single(ar->dev, tresp, *resp_len,
1342 DMA_FROM_DEVICE);
1343 ret = dma_mapping_error(ar->dev, resp_paddr);
1344 if (ret)
1345 goto err_req;
1346
1347 xfer.wait_for_resp = true;
1348 xfer.resp_len = 0;
1349
1350 ath10k_ce_recv_buf_enqueue(ce_rx, &xfer, resp_paddr);
1351 }
1352
1353 init_completion(&xfer.done);
1354
1355 ret = ath10k_ce_send(ce_tx, &xfer, req_paddr, req_len, -1, 0);
1356 if (ret)
1357 goto err_resp;
1358
Michal Kazior85622cd2013-11-25 14:06:22 +01001359 ret = ath10k_pci_bmi_wait(ce_tx, ce_rx, &xfer);
1360 if (ret) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001361 u32 unused_buffer;
1362 unsigned int unused_nbytes;
1363 unsigned int unused_id;
1364
Kalle Valo5e3dd152013-06-12 20:52:10 +03001365 ath10k_ce_cancel_send_next(ce_tx, NULL, &unused_buffer,
1366 &unused_nbytes, &unused_id);
1367 } else {
1368 /* non-zero means we did not time out */
1369 ret = 0;
1370 }
1371
1372err_resp:
1373 if (resp) {
1374 u32 unused_buffer;
1375
1376 ath10k_ce_revoke_recv_next(ce_rx, NULL, &unused_buffer);
1377 dma_unmap_single(ar->dev, resp_paddr,
1378 *resp_len, DMA_FROM_DEVICE);
1379 }
1380err_req:
1381 dma_unmap_single(ar->dev, req_paddr, req_len, DMA_TO_DEVICE);
1382
1383 if (ret == 0 && resp_len) {
1384 *resp_len = min(*resp_len, xfer.resp_len);
1385 memcpy(resp, tresp, xfer.resp_len);
1386 }
1387err_dma:
1388 kfree(treq);
1389 kfree(tresp);
1390
1391 return ret;
1392}
1393
Michal Kazior5440ce22013-09-03 15:09:58 +02001394static void ath10k_pci_bmi_send_done(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001395{
Michal Kazior5440ce22013-09-03 15:09:58 +02001396 struct bmi_xfer *xfer;
1397 u32 ce_data;
1398 unsigned int nbytes;
1399 unsigned int transfer_id;
1400
1401 if (ath10k_ce_completed_send_next(ce_state, (void **)&xfer, &ce_data,
1402 &nbytes, &transfer_id))
1403 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001404
1405 if (xfer->wait_for_resp)
1406 return;
1407
1408 complete(&xfer->done);
1409}
1410
Michal Kazior5440ce22013-09-03 15:09:58 +02001411static void ath10k_pci_bmi_recv_data(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001412{
Michal Kazior5440ce22013-09-03 15:09:58 +02001413 struct bmi_xfer *xfer;
1414 u32 ce_data;
1415 unsigned int nbytes;
1416 unsigned int transfer_id;
1417 unsigned int flags;
1418
1419 if (ath10k_ce_completed_recv_next(ce_state, (void **)&xfer, &ce_data,
1420 &nbytes, &transfer_id, &flags))
1421 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001422
1423 if (!xfer->wait_for_resp) {
1424 ath10k_warn("unexpected: BMI data received; ignoring\n");
1425 return;
1426 }
1427
1428 xfer->resp_len = nbytes;
1429 complete(&xfer->done);
1430}
1431
Michal Kazior85622cd2013-11-25 14:06:22 +01001432static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
1433 struct ath10k_ce_pipe *rx_pipe,
1434 struct bmi_xfer *xfer)
1435{
1436 unsigned long timeout = jiffies + BMI_COMMUNICATION_TIMEOUT_HZ;
1437
1438 while (time_before_eq(jiffies, timeout)) {
1439 ath10k_pci_bmi_send_done(tx_pipe);
1440 ath10k_pci_bmi_recv_data(rx_pipe);
1441
1442 if (completion_done(&xfer->done))
1443 return 0;
1444
1445 schedule();
1446 }
1447
1448 return -ETIMEDOUT;
1449}
1450
Kalle Valo5e3dd152013-06-12 20:52:10 +03001451/*
1452 * Map from service/endpoint to Copy Engine.
1453 * This table is derived from the CE_PCI TABLE, above.
1454 * It is passed to the Target at startup for use by firmware.
1455 */
1456static const struct service_to_pipe target_service_to_ce_map_wlan[] = {
1457 {
1458 ATH10K_HTC_SVC_ID_WMI_DATA_VO,
1459 PIPEDIR_OUT, /* out = UL = host -> target */
1460 3,
1461 },
1462 {
1463 ATH10K_HTC_SVC_ID_WMI_DATA_VO,
1464 PIPEDIR_IN, /* in = DL = target -> host */
1465 2,
1466 },
1467 {
1468 ATH10K_HTC_SVC_ID_WMI_DATA_BK,
1469 PIPEDIR_OUT, /* out = UL = host -> target */
1470 3,
1471 },
1472 {
1473 ATH10K_HTC_SVC_ID_WMI_DATA_BK,
1474 PIPEDIR_IN, /* in = DL = target -> host */
1475 2,
1476 },
1477 {
1478 ATH10K_HTC_SVC_ID_WMI_DATA_BE,
1479 PIPEDIR_OUT, /* out = UL = host -> target */
1480 3,
1481 },
1482 {
1483 ATH10K_HTC_SVC_ID_WMI_DATA_BE,
1484 PIPEDIR_IN, /* in = DL = target -> host */
1485 2,
1486 },
1487 {
1488 ATH10K_HTC_SVC_ID_WMI_DATA_VI,
1489 PIPEDIR_OUT, /* out = UL = host -> target */
1490 3,
1491 },
1492 {
1493 ATH10K_HTC_SVC_ID_WMI_DATA_VI,
1494 PIPEDIR_IN, /* in = DL = target -> host */
1495 2,
1496 },
1497 {
1498 ATH10K_HTC_SVC_ID_WMI_CONTROL,
1499 PIPEDIR_OUT, /* out = UL = host -> target */
1500 3,
1501 },
1502 {
1503 ATH10K_HTC_SVC_ID_WMI_CONTROL,
1504 PIPEDIR_IN, /* in = DL = target -> host */
1505 2,
1506 },
1507 {
1508 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1509 PIPEDIR_OUT, /* out = UL = host -> target */
1510 0, /* could be moved to 3 (share with WMI) */
1511 },
1512 {
1513 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1514 PIPEDIR_IN, /* in = DL = target -> host */
1515 1,
1516 },
1517 {
1518 ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS, /* not currently used */
1519 PIPEDIR_OUT, /* out = UL = host -> target */
1520 0,
1521 },
1522 {
1523 ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS, /* not currently used */
1524 PIPEDIR_IN, /* in = DL = target -> host */
1525 1,
1526 },
1527 {
1528 ATH10K_HTC_SVC_ID_HTT_DATA_MSG,
1529 PIPEDIR_OUT, /* out = UL = host -> target */
1530 4,
1531 },
1532 {
1533 ATH10K_HTC_SVC_ID_HTT_DATA_MSG,
1534 PIPEDIR_IN, /* in = DL = target -> host */
1535 1,
1536 },
1537
1538 /* (Additions here) */
1539
1540 { /* Must be last */
1541 0,
1542 0,
1543 0,
1544 },
1545};
1546
1547/*
1548 * Send an interrupt to the device to wake up the Target CPU
1549 * so it has an opportunity to notice any changed state.
1550 */
1551static int ath10k_pci_wake_target_cpu(struct ath10k *ar)
1552{
1553 int ret;
1554 u32 core_ctrl;
1555
1556 ret = ath10k_pci_diag_read_access(ar, SOC_CORE_BASE_ADDRESS |
1557 CORE_CTRL_ADDRESS,
1558 &core_ctrl);
1559 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001560 ath10k_warn("failed to read core_ctrl: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001561 return ret;
1562 }
1563
1564 /* A_INUM_FIRMWARE interrupt to Target CPU */
1565 core_ctrl |= CORE_CTRL_CPU_INTR_MASK;
1566
1567 ret = ath10k_pci_diag_write_access(ar, SOC_CORE_BASE_ADDRESS |
1568 CORE_CTRL_ADDRESS,
1569 core_ctrl);
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001570 if (ret) {
1571 ath10k_warn("failed to set target CPU interrupt mask: %d\n",
1572 ret);
1573 return ret;
1574 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001575
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001576 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001577}
1578
1579static int ath10k_pci_init_config(struct ath10k *ar)
1580{
1581 u32 interconnect_targ_addr;
1582 u32 pcie_state_targ_addr = 0;
1583 u32 pipe_cfg_targ_addr = 0;
1584 u32 svc_to_pipe_map = 0;
1585 u32 pcie_config_flags = 0;
1586 u32 ealloc_value;
1587 u32 ealloc_targ_addr;
1588 u32 flag2_value;
1589 u32 flag2_targ_addr;
1590 int ret = 0;
1591
1592 /* Download to Target the CE Config and the service-to-CE map */
1593 interconnect_targ_addr =
1594 host_interest_item_address(HI_ITEM(hi_interconnect_state));
1595
1596 /* Supply Target-side CE configuration */
1597 ret = ath10k_pci_diag_read_access(ar, interconnect_targ_addr,
1598 &pcie_state_targ_addr);
1599 if (ret != 0) {
1600 ath10k_err("Failed to get pcie state addr: %d\n", ret);
1601 return ret;
1602 }
1603
1604 if (pcie_state_targ_addr == 0) {
1605 ret = -EIO;
1606 ath10k_err("Invalid pcie state addr\n");
1607 return ret;
1608 }
1609
1610 ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
1611 offsetof(struct pcie_state,
1612 pipe_cfg_addr),
1613 &pipe_cfg_targ_addr);
1614 if (ret != 0) {
1615 ath10k_err("Failed to get pipe cfg addr: %d\n", ret);
1616 return ret;
1617 }
1618
1619 if (pipe_cfg_targ_addr == 0) {
1620 ret = -EIO;
1621 ath10k_err("Invalid pipe cfg addr\n");
1622 return ret;
1623 }
1624
1625 ret = ath10k_pci_diag_write_mem(ar, pipe_cfg_targ_addr,
1626 target_ce_config_wlan,
1627 sizeof(target_ce_config_wlan));
1628
1629 if (ret != 0) {
1630 ath10k_err("Failed to write pipe cfg: %d\n", ret);
1631 return ret;
1632 }
1633
1634 ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
1635 offsetof(struct pcie_state,
1636 svc_to_pipe_map),
1637 &svc_to_pipe_map);
1638 if (ret != 0) {
1639 ath10k_err("Failed to get svc/pipe map: %d\n", ret);
1640 return ret;
1641 }
1642
1643 if (svc_to_pipe_map == 0) {
1644 ret = -EIO;
1645 ath10k_err("Invalid svc_to_pipe map\n");
1646 return ret;
1647 }
1648
1649 ret = ath10k_pci_diag_write_mem(ar, svc_to_pipe_map,
1650 target_service_to_ce_map_wlan,
1651 sizeof(target_service_to_ce_map_wlan));
1652 if (ret != 0) {
1653 ath10k_err("Failed to write svc/pipe map: %d\n", ret);
1654 return ret;
1655 }
1656
1657 ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
1658 offsetof(struct pcie_state,
1659 config_flags),
1660 &pcie_config_flags);
1661 if (ret != 0) {
1662 ath10k_err("Failed to get pcie config_flags: %d\n", ret);
1663 return ret;
1664 }
1665
1666 pcie_config_flags &= ~PCIE_CONFIG_FLAG_ENABLE_L1;
1667
1668 ret = ath10k_pci_diag_write_mem(ar, pcie_state_targ_addr +
1669 offsetof(struct pcie_state, config_flags),
1670 &pcie_config_flags,
1671 sizeof(pcie_config_flags));
1672 if (ret != 0) {
1673 ath10k_err("Failed to write pcie config_flags: %d\n", ret);
1674 return ret;
1675 }
1676
1677 /* configure early allocation */
1678 ealloc_targ_addr = host_interest_item_address(HI_ITEM(hi_early_alloc));
1679
1680 ret = ath10k_pci_diag_read_access(ar, ealloc_targ_addr, &ealloc_value);
1681 if (ret != 0) {
1682 ath10k_err("Faile to get early alloc val: %d\n", ret);
1683 return ret;
1684 }
1685
1686 /* first bank is switched to IRAM */
1687 ealloc_value |= ((HI_EARLY_ALLOC_MAGIC << HI_EARLY_ALLOC_MAGIC_SHIFT) &
1688 HI_EARLY_ALLOC_MAGIC_MASK);
1689 ealloc_value |= ((1 << HI_EARLY_ALLOC_IRAM_BANKS_SHIFT) &
1690 HI_EARLY_ALLOC_IRAM_BANKS_MASK);
1691
1692 ret = ath10k_pci_diag_write_access(ar, ealloc_targ_addr, ealloc_value);
1693 if (ret != 0) {
1694 ath10k_err("Failed to set early alloc val: %d\n", ret);
1695 return ret;
1696 }
1697
1698 /* Tell Target to proceed with initialization */
1699 flag2_targ_addr = host_interest_item_address(HI_ITEM(hi_option_flag2));
1700
1701 ret = ath10k_pci_diag_read_access(ar, flag2_targ_addr, &flag2_value);
1702 if (ret != 0) {
1703 ath10k_err("Failed to get option val: %d\n", ret);
1704 return ret;
1705 }
1706
1707 flag2_value |= HI_OPTION_EARLY_CFG_DONE;
1708
1709 ret = ath10k_pci_diag_write_access(ar, flag2_targ_addr, flag2_value);
1710 if (ret != 0) {
1711 ath10k_err("Failed to set option val: %d\n", ret);
1712 return ret;
1713 }
1714
1715 return 0;
1716}
1717
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001718static int ath10k_pci_alloc_ce(struct ath10k *ar)
1719{
1720 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001721
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001722 for (i = 0; i < CE_COUNT; i++) {
1723 ret = ath10k_ce_alloc_pipe(ar, i, &host_ce_config_wlan[i]);
1724 if (ret) {
1725 ath10k_err("failed to allocate copy engine pipe %d: %d\n",
1726 i, ret);
1727 return ret;
1728 }
1729 }
1730
1731 return 0;
1732}
1733
1734static void ath10k_pci_free_ce(struct ath10k *ar)
1735{
1736 int i;
1737
1738 for (i = 0; i < CE_COUNT; i++)
1739 ath10k_ce_free_pipe(ar, i);
1740}
Kalle Valo5e3dd152013-06-12 20:52:10 +03001741
1742static int ath10k_pci_ce_init(struct ath10k *ar)
1743{
1744 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +02001745 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001746 const struct ce_attr *attr;
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001747 int pipe_num, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001748
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001749 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001750 pipe_info = &ar_pci->pipe_info[pipe_num];
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001751 pipe_info->ce_hdl = &ar_pci->ce_states[pipe_num];
Kalle Valo5e3dd152013-06-12 20:52:10 +03001752 pipe_info->pipe_num = pipe_num;
1753 pipe_info->hif_ce_state = ar;
1754 attr = &host_ce_config_wlan[pipe_num];
1755
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001756 ret = ath10k_ce_init_pipe(ar, pipe_num, attr);
1757 if (ret) {
1758 ath10k_err("failed to initialize copy engine pipe %d: %d\n",
1759 pipe_num, ret);
1760 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001761 }
1762
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001763 if (pipe_num == CE_COUNT - 1) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001764 /*
1765 * Reserve the ultimate CE for
1766 * diagnostic Window support
1767 */
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001768 ar_pci->ce_diag = pipe_info->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001769 continue;
1770 }
1771
1772 pipe_info->buf_sz = (size_t) (attr->src_sz_max);
1773 }
1774
Kalle Valo5e3dd152013-06-12 20:52:10 +03001775 return 0;
1776}
1777
1778static void ath10k_pci_fw_interrupt_handler(struct ath10k *ar)
1779{
1780 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valob39712c2014-03-28 09:32:46 +02001781 u32 fw_indicator;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001782
1783 ath10k_pci_wake(ar);
1784
Kalle Valob39712c2014-03-28 09:32:46 +02001785 fw_indicator = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001786
1787 if (fw_indicator & FW_IND_EVENT_PENDING) {
1788 /* ACK: clear Target-side pending event */
Kalle Valob39712c2014-03-28 09:32:46 +02001789 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001790 fw_indicator & ~FW_IND_EVENT_PENDING);
1791
1792 if (ar_pci->started) {
1793 ath10k_pci_hif_dump_area(ar);
1794 } else {
1795 /*
1796 * Probable Target failure before we're prepared
1797 * to handle it. Generally unexpected.
1798 */
1799 ath10k_warn("early firmware event indicated\n");
1800 }
1801 }
1802
1803 ath10k_pci_sleep(ar);
1804}
1805
Michal Kaziorde013572014-05-14 16:56:16 +03001806/* this function effectively clears target memory controller assert line */
1807static void ath10k_pci_warm_reset_si0(struct ath10k *ar)
1808{
1809 u32 val;
1810
1811 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1812 ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
1813 val | SOC_RESET_CONTROL_SI0_RST_MASK);
1814 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1815
1816 msleep(10);
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
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001826static int ath10k_pci_warm_reset(struct ath10k *ar)
1827{
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001828 int ret = 0;
1829 u32 val;
1830
Kalle Valo50f87a62014-03-28 09:32:52 +02001831 ath10k_dbg(ATH10K_DBG_BOOT, "boot warm reset\n");
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001832
1833 ret = ath10k_do_pci_wake(ar);
1834 if (ret) {
1835 ath10k_err("failed to wake up target: %d\n", ret);
1836 return ret;
1837 }
1838
1839 /* debug */
1840 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1841 PCIE_INTR_CAUSE_ADDRESS);
1842 ath10k_dbg(ATH10K_DBG_BOOT, "boot host cpu intr cause: 0x%08x\n", val);
1843
1844 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1845 CPU_INTR_ADDRESS);
1846 ath10k_dbg(ATH10K_DBG_BOOT, "boot target cpu intr cause: 0x%08x\n",
1847 val);
1848
1849 /* disable pending irqs */
1850 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
1851 PCIE_INTR_ENABLE_ADDRESS, 0);
1852
1853 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
1854 PCIE_INTR_CLR_ADDRESS, ~0);
1855
1856 msleep(100);
1857
1858 /* clear fw indicator */
Kalle Valob39712c2014-03-28 09:32:46 +02001859 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, 0);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001860
1861 /* clear target LF timer interrupts */
1862 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1863 SOC_LF_TIMER_CONTROL0_ADDRESS);
1864 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS +
1865 SOC_LF_TIMER_CONTROL0_ADDRESS,
1866 val & ~SOC_LF_TIMER_CONTROL0_ENABLE_MASK);
1867
1868 /* reset CE */
1869 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1870 SOC_RESET_CONTROL_ADDRESS);
1871 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1872 val | SOC_RESET_CONTROL_CE_RST_MASK);
1873 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1874 SOC_RESET_CONTROL_ADDRESS);
1875 msleep(10);
1876
1877 /* unreset CE */
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
Michal Kaziorde013572014-05-14 16:56:16 +03001884 ath10k_pci_warm_reset_si0(ar);
1885
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001886 /* debug */
1887 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1888 PCIE_INTR_CAUSE_ADDRESS);
1889 ath10k_dbg(ATH10K_DBG_BOOT, "boot host cpu intr cause: 0x%08x\n", val);
1890
1891 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1892 CPU_INTR_ADDRESS);
1893 ath10k_dbg(ATH10K_DBG_BOOT, "boot target cpu intr cause: 0x%08x\n",
1894 val);
1895
1896 /* CPU warm reset */
1897 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1898 SOC_RESET_CONTROL_ADDRESS);
1899 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1900 val | SOC_RESET_CONTROL_CPU_WARM_RST_MASK);
1901
1902 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1903 SOC_RESET_CONTROL_ADDRESS);
1904 ath10k_dbg(ATH10K_DBG_BOOT, "boot target reset state: 0x%08x\n", val);
1905
1906 msleep(100);
1907
1908 ath10k_dbg(ATH10K_DBG_BOOT, "boot warm reset complete\n");
1909
1910 ath10k_do_pci_sleep(ar);
1911 return ret;
1912}
1913
1914static int __ath10k_pci_hif_power_up(struct ath10k *ar, bool cold_reset)
Michal Kazior8c5c5362013-07-16 09:38:50 +02001915{
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02001916 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo95cbb6a2013-11-20 10:00:35 +02001917 const char *irq_mode;
Michal Kazior8c5c5362013-07-16 09:38:50 +02001918 int ret;
1919
1920 /*
1921 * Bring the target up cleanly.
1922 *
1923 * The target may be in an undefined state with an AUX-powered Target
1924 * and a Host in WoW mode. If the Host crashes, loses power, or is
1925 * restarted (without unloading the driver) then the Target is left
1926 * (aux) powered and running. On a subsequent driver load, the Target
1927 * is in an unexpected state. We try to catch that here in order to
1928 * reset the Target and retry the probe.
1929 */
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001930 if (cold_reset)
1931 ret = ath10k_pci_cold_reset(ar);
1932 else
1933 ret = ath10k_pci_warm_reset(ar);
1934
Michal Kazior5b2589f2013-11-08 08:01:30 +01001935 if (ret) {
1936 ath10k_err("failed to reset target: %d\n", ret);
Michal Kazior98563d52013-11-08 08:01:33 +01001937 goto err;
Michal Kazior5b2589f2013-11-08 08:01:30 +01001938 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02001939
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02001940 if (!test_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE, ar_pci->features))
Michal Kazior8c5c5362013-07-16 09:38:50 +02001941 /* Force AWAKE forever */
Michal Kazior8c5c5362013-07-16 09:38:50 +02001942 ath10k_do_pci_wake(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001943
1944 ret = ath10k_pci_ce_init(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001945 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001946 ath10k_err("failed to initialize CE: %d\n", ret);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001947 goto err_ps;
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001948 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02001949
Michal Kazior98563d52013-11-08 08:01:33 +01001950 ret = ath10k_ce_disable_interrupts(ar);
1951 if (ret) {
1952 ath10k_err("failed to disable CE interrupts: %d\n", ret);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001953 goto err_ce;
1954 }
1955
Michal Kaziorfc15ca12013-11-25 14:06:21 +01001956 ret = ath10k_pci_init_irq(ar);
Michal Kazior98563d52013-11-08 08:01:33 +01001957 if (ret) {
Michal Kaziorfc15ca12013-11-25 14:06:21 +01001958 ath10k_err("failed to init irqs: %d\n", ret);
Michal Kazior98563d52013-11-08 08:01:33 +01001959 goto err_ce;
1960 }
1961
Michal Kaziorab977bd2013-11-25 14:06:26 +01001962 ret = ath10k_pci_request_early_irq(ar);
1963 if (ret) {
1964 ath10k_err("failed to request early irq: %d\n", ret);
1965 goto err_deinit_irq;
1966 }
1967
Michal Kazior98563d52013-11-08 08:01:33 +01001968 ret = ath10k_pci_wait_for_target_init(ar);
1969 if (ret) {
1970 ath10k_err("failed to wait for target to init: %d\n", ret);
Michal Kaziorab977bd2013-11-25 14:06:26 +01001971 goto err_free_early_irq;
Michal Kazior98563d52013-11-08 08:01:33 +01001972 }
1973
1974 ret = ath10k_pci_init_config(ar);
1975 if (ret) {
1976 ath10k_err("failed to setup init config: %d\n", ret);
Michal Kaziorab977bd2013-11-25 14:06:26 +01001977 goto err_free_early_irq;
Michal Kazior98563d52013-11-08 08:01:33 +01001978 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02001979
1980 ret = ath10k_pci_wake_target_cpu(ar);
1981 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001982 ath10k_err("could not wake up target CPU: %d\n", ret);
Michal Kaziorab977bd2013-11-25 14:06:26 +01001983 goto err_free_early_irq;
Michal Kazior8c5c5362013-07-16 09:38:50 +02001984 }
1985
Kalle Valo95cbb6a2013-11-20 10:00:35 +02001986 if (ar_pci->num_msi_intrs > 1)
1987 irq_mode = "MSI-X";
1988 else if (ar_pci->num_msi_intrs == 1)
1989 irq_mode = "MSI";
1990 else
1991 irq_mode = "legacy";
1992
Kalle Valo650b91f2013-11-20 10:00:49 +02001993 if (!test_bit(ATH10K_FLAG_FIRST_BOOT_DONE, &ar->dev_flags))
Kalle Valo78a9cb42014-03-28 09:32:58 +02001994 ath10k_info("pci irq %s irq_mode %d reset_mode %d\n",
1995 irq_mode, ath10k_pci_irq_mode,
1996 ath10k_pci_reset_mode);
Kalle Valo95cbb6a2013-11-20 10:00:35 +02001997
Michal Kazior8c5c5362013-07-16 09:38:50 +02001998 return 0;
1999
Michal Kaziorab977bd2013-11-25 14:06:26 +01002000err_free_early_irq:
2001 ath10k_pci_free_early_irq(ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002002err_deinit_irq:
2003 ath10k_pci_deinit_irq(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02002004err_ce:
2005 ath10k_pci_ce_deinit(ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002006 ath10k_pci_warm_reset(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02002007err_ps:
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002008 if (!test_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE, ar_pci->features))
Michal Kazior8c5c5362013-07-16 09:38:50 +02002009 ath10k_do_pci_sleep(ar);
2010err:
2011 return ret;
2012}
2013
Michal Kazior61c95ce2014-05-14 16:56:16 +03002014static int ath10k_pci_hif_power_up_warm(struct ath10k *ar)
2015{
2016 int i, ret;
2017
2018 /*
2019 * Sometime warm reset succeeds after retries.
2020 *
2021 * FIXME: It might be possible to tune ath10k_pci_warm_reset() to work
2022 * at first try.
2023 */
2024 for (i = 0; i < ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS; i++) {
2025 ret = __ath10k_pci_hif_power_up(ar, false);
2026 if (ret == 0)
2027 break;
2028
2029 ath10k_warn("failed to warm reset (attempt %d out of %d): %d\n",
2030 i + 1, ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS, ret);
2031 }
2032
2033 return ret;
2034}
2035
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002036static int ath10k_pci_hif_power_up(struct ath10k *ar)
2037{
2038 int ret;
2039
Kalle Valo50f87a62014-03-28 09:32:52 +02002040 ath10k_dbg(ATH10K_DBG_BOOT, "boot hif power up\n");
2041
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002042 /*
2043 * Hardware CUS232 version 2 has some issues with cold reset and the
2044 * preferred (and safer) way to perform a device reset is through a
2045 * warm reset.
2046 *
Michal Kazior61c95ce2014-05-14 16:56:16 +03002047 * Warm reset doesn't always work though so fall back to cold reset may
2048 * be necessary.
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002049 */
Michal Kazior61c95ce2014-05-14 16:56:16 +03002050 ret = ath10k_pci_hif_power_up_warm(ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002051 if (ret) {
Kalle Valo35098462014-03-28 09:32:27 +02002052 ath10k_warn("failed to power up target using warm reset: %d\n",
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002053 ret);
2054
Kalle Valo35098462014-03-28 09:32:27 +02002055 if (ath10k_pci_reset_mode == ATH10K_PCI_RESET_WARM_ONLY)
2056 return ret;
2057
2058 ath10k_warn("trying cold reset\n");
2059
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002060 ret = __ath10k_pci_hif_power_up(ar, true);
2061 if (ret) {
2062 ath10k_err("failed to power up target using cold reset too (%d)\n",
2063 ret);
2064 return ret;
2065 }
2066 }
2067
2068 return 0;
2069}
2070
Michal Kazior8c5c5362013-07-16 09:38:50 +02002071static void ath10k_pci_hif_power_down(struct ath10k *ar)
2072{
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002073 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2074
Kalle Valo50f87a62014-03-28 09:32:52 +02002075 ath10k_dbg(ATH10K_DBG_BOOT, "boot hif power down\n");
2076
Michal Kaziorab977bd2013-11-25 14:06:26 +01002077 ath10k_pci_free_early_irq(ar);
2078 ath10k_pci_kill_tasklet(ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002079 ath10k_pci_deinit_irq(ar);
Michal Kaziordf5e8522014-03-28 10:02:45 +02002080 ath10k_pci_ce_deinit(ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002081 ath10k_pci_warm_reset(ar);
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002082
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002083 if (!test_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE, ar_pci->features))
Michal Kazior8c5c5362013-07-16 09:38:50 +02002084 ath10k_do_pci_sleep(ar);
2085}
2086
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002087#ifdef CONFIG_PM
2088
2089#define ATH10K_PCI_PM_CONTROL 0x44
2090
2091static int ath10k_pci_hif_suspend(struct ath10k *ar)
2092{
2093 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2094 struct pci_dev *pdev = ar_pci->pdev;
2095 u32 val;
2096
2097 pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
2098
2099 if ((val & 0x000000ff) != 0x3) {
2100 pci_save_state(pdev);
2101 pci_disable_device(pdev);
2102 pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
2103 (val & 0xffffff00) | 0x03);
2104 }
2105
2106 return 0;
2107}
2108
2109static int ath10k_pci_hif_resume(struct ath10k *ar)
2110{
2111 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2112 struct pci_dev *pdev = ar_pci->pdev;
2113 u32 val;
2114
2115 pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
2116
2117 if ((val & 0x000000ff) != 0) {
2118 pci_restore_state(pdev);
2119 pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
2120 val & 0xffffff00);
2121 /*
2122 * Suspend/Resume resets the PCI configuration space,
2123 * so we have to re-disable the RETRY_TIMEOUT register (0x41)
2124 * to keep PCI Tx retries from interfering with C3 CPU state
2125 */
2126 pci_read_config_dword(pdev, 0x40, &val);
2127
2128 if ((val & 0x0000ff00) != 0)
2129 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
2130 }
2131
2132 return 0;
2133}
2134#endif
2135
Kalle Valo5e3dd152013-06-12 20:52:10 +03002136static const struct ath10k_hif_ops ath10k_pci_hif_ops = {
Michal Kazior726346f2014-02-27 18:50:04 +02002137 .tx_sg = ath10k_pci_hif_tx_sg,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002138 .exchange_bmi_msg = ath10k_pci_hif_exchange_bmi_msg,
2139 .start = ath10k_pci_hif_start,
2140 .stop = ath10k_pci_hif_stop,
2141 .map_service_to_pipe = ath10k_pci_hif_map_service_to_pipe,
2142 .get_default_pipe = ath10k_pci_hif_get_default_pipe,
2143 .send_complete_check = ath10k_pci_hif_send_complete_check,
Michal Kaziore799bbf2013-07-05 16:15:12 +03002144 .set_callbacks = ath10k_pci_hif_set_callbacks,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002145 .get_free_queue_number = ath10k_pci_hif_get_free_queue_number,
Michal Kazior8c5c5362013-07-16 09:38:50 +02002146 .power_up = ath10k_pci_hif_power_up,
2147 .power_down = ath10k_pci_hif_power_down,
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002148#ifdef CONFIG_PM
2149 .suspend = ath10k_pci_hif_suspend,
2150 .resume = ath10k_pci_hif_resume,
2151#endif
Kalle Valo5e3dd152013-06-12 20:52:10 +03002152};
2153
2154static void ath10k_pci_ce_tasklet(unsigned long ptr)
2155{
Michal Kazior87263e52013-08-27 13:08:01 +02002156 struct ath10k_pci_pipe *pipe = (struct ath10k_pci_pipe *)ptr;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002157 struct ath10k_pci *ar_pci = pipe->ar_pci;
2158
2159 ath10k_ce_per_engine_service(ar_pci->ar, pipe->pipe_num);
2160}
2161
2162static void ath10k_msi_err_tasklet(unsigned long data)
2163{
2164 struct ath10k *ar = (struct ath10k *)data;
2165
2166 ath10k_pci_fw_interrupt_handler(ar);
2167}
2168
2169/*
2170 * Handler for a per-engine interrupt on a PARTICULAR CE.
2171 * This is used in cases where each CE has a private MSI interrupt.
2172 */
2173static irqreturn_t ath10k_pci_per_engine_handler(int irq, void *arg)
2174{
2175 struct ath10k *ar = arg;
2176 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2177 int ce_id = irq - ar_pci->pdev->irq - MSI_ASSIGN_CE_INITIAL;
2178
Dan Carpentere5742672013-06-18 10:28:46 +03002179 if (ce_id < 0 || ce_id >= ARRAY_SIZE(ar_pci->pipe_info)) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03002180 ath10k_warn("unexpected/invalid irq %d ce_id %d\n", irq, ce_id);
2181 return IRQ_HANDLED;
2182 }
2183
2184 /*
2185 * NOTE: We are able to derive ce_id from irq because we
2186 * use a one-to-one mapping for CE's 0..5.
2187 * CE's 6 & 7 do not use interrupts at all.
2188 *
2189 * This mapping must be kept in sync with the mapping
2190 * used by firmware.
2191 */
2192 tasklet_schedule(&ar_pci->pipe_info[ce_id].intr);
2193 return IRQ_HANDLED;
2194}
2195
2196static irqreturn_t ath10k_pci_msi_fw_handler(int irq, void *arg)
2197{
2198 struct ath10k *ar = arg;
2199 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2200
2201 tasklet_schedule(&ar_pci->msi_fw_err);
2202 return IRQ_HANDLED;
2203}
2204
2205/*
2206 * Top-level interrupt handler for all PCI interrupts from a Target.
2207 * When a block of MSI interrupts is allocated, this top-level handler
2208 * is not used; instead, we directly call the correct sub-handler.
2209 */
2210static irqreturn_t ath10k_pci_interrupt_handler(int irq, void *arg)
2211{
2212 struct ath10k *ar = arg;
2213 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2214
2215 if (ar_pci->num_msi_intrs == 0) {
Michal Kaziore5398872013-11-25 14:06:20 +01002216 if (!ath10k_pci_irq_pending(ar))
2217 return IRQ_NONE;
2218
Michal Kazior26852182013-11-25 14:06:25 +01002219 ath10k_pci_disable_and_clear_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002220 }
2221
2222 tasklet_schedule(&ar_pci->intr_tq);
2223
2224 return IRQ_HANDLED;
2225}
2226
Michal Kaziorab977bd2013-11-25 14:06:26 +01002227static void ath10k_pci_early_irq_tasklet(unsigned long data)
2228{
2229 struct ath10k *ar = (struct ath10k *)data;
Michal Kaziorab977bd2013-11-25 14:06:26 +01002230 u32 fw_ind;
2231 int ret;
2232
2233 ret = ath10k_pci_wake(ar);
2234 if (ret) {
2235 ath10k_warn("failed to wake target in early irq tasklet: %d\n",
2236 ret);
2237 return;
2238 }
2239
Kalle Valob39712c2014-03-28 09:32:46 +02002240 fw_ind = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
Michal Kaziorab977bd2013-11-25 14:06:26 +01002241 if (fw_ind & FW_IND_EVENT_PENDING) {
Kalle Valob39712c2014-03-28 09:32:46 +02002242 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS,
Michal Kaziorab977bd2013-11-25 14:06:26 +01002243 fw_ind & ~FW_IND_EVENT_PENDING);
Michal Kazior1a4ab282014-05-14 16:56:16 +03002244 ath10k_pci_hif_dump_area(ar);
Michal Kaziorab977bd2013-11-25 14:06:26 +01002245 }
2246
2247 ath10k_pci_sleep(ar);
2248 ath10k_pci_enable_legacy_irq(ar);
2249}
2250
Kalle Valo5e3dd152013-06-12 20:52:10 +03002251static void ath10k_pci_tasklet(unsigned long data)
2252{
2253 struct ath10k *ar = (struct ath10k *)data;
2254 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2255
2256 ath10k_pci_fw_interrupt_handler(ar); /* FIXME: Handle FW error */
2257 ath10k_ce_per_engine_service_any(ar);
2258
Michal Kazior26852182013-11-25 14:06:25 +01002259 /* Re-enable legacy irq that was disabled in the irq handler */
2260 if (ar_pci->num_msi_intrs == 0)
2261 ath10k_pci_enable_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002262}
2263
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002264static int ath10k_pci_request_irq_msix(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002265{
2266 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002267 int ret, i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002268
2269 ret = request_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW,
2270 ath10k_pci_msi_fw_handler,
2271 IRQF_SHARED, "ath10k_pci", ar);
Michal Kazior591ecdb2013-07-31 10:55:15 +02002272 if (ret) {
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002273 ath10k_warn("failed to request MSI-X fw irq %d: %d\n",
Michal Kazior591ecdb2013-07-31 10:55:15 +02002274 ar_pci->pdev->irq + MSI_ASSIGN_FW, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002275 return ret;
Michal Kazior591ecdb2013-07-31 10:55:15 +02002276 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002277
2278 for (i = MSI_ASSIGN_CE_INITIAL; i <= MSI_ASSIGN_CE_MAX; i++) {
2279 ret = request_irq(ar_pci->pdev->irq + i,
2280 ath10k_pci_per_engine_handler,
2281 IRQF_SHARED, "ath10k_pci", ar);
2282 if (ret) {
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002283 ath10k_warn("failed to request MSI-X ce irq %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002284 ar_pci->pdev->irq + i, ret);
2285
Michal Kazior87b14232013-06-26 08:50:50 +02002286 for (i--; i >= MSI_ASSIGN_CE_INITIAL; i--)
2287 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002288
Michal Kazior87b14232013-06-26 08:50:50 +02002289 free_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002290 return ret;
2291 }
2292 }
2293
Kalle Valo5e3dd152013-06-12 20:52:10 +03002294 return 0;
2295}
2296
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002297static int ath10k_pci_request_irq_msi(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002298{
2299 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2300 int ret;
2301
2302 ret = request_irq(ar_pci->pdev->irq,
2303 ath10k_pci_interrupt_handler,
2304 IRQF_SHARED, "ath10k_pci", ar);
Kalle Valof3782742013-10-17 11:36:15 +03002305 if (ret) {
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002306 ath10k_warn("failed to request MSI irq %d: %d\n",
2307 ar_pci->pdev->irq, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002308 return ret;
Kalle Valof3782742013-10-17 11:36:15 +03002309 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002310
Kalle Valo5e3dd152013-06-12 20:52:10 +03002311 return 0;
2312}
2313
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002314static int ath10k_pci_request_irq_legacy(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002315{
2316 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002317 int ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002318
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002319 ret = request_irq(ar_pci->pdev->irq,
2320 ath10k_pci_interrupt_handler,
2321 IRQF_SHARED, "ath10k_pci", ar);
Kalle Valof3782742013-10-17 11:36:15 +03002322 if (ret) {
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002323 ath10k_warn("failed to request legacy irq %d: %d\n",
2324 ar_pci->pdev->irq, ret);
Kalle Valof3782742013-10-17 11:36:15 +03002325 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002326 }
2327
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002328 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002329}
2330
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002331static int ath10k_pci_request_irq(struct ath10k *ar)
2332{
2333 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2334
2335 switch (ar_pci->num_msi_intrs) {
2336 case 0:
2337 return ath10k_pci_request_irq_legacy(ar);
2338 case 1:
2339 return ath10k_pci_request_irq_msi(ar);
2340 case MSI_NUM_REQUEST:
2341 return ath10k_pci_request_irq_msix(ar);
2342 }
2343
2344 ath10k_warn("unknown irq configuration upon request\n");
2345 return -EINVAL;
2346}
2347
2348static void ath10k_pci_free_irq(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002349{
2350 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2351 int i;
2352
2353 /* There's at least one interrupt irregardless whether its legacy INTR
2354 * or MSI or MSI-X */
2355 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
2356 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002357}
2358
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002359static void ath10k_pci_init_irq_tasklets(struct ath10k *ar)
2360{
2361 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2362 int i;
2363
2364 tasklet_init(&ar_pci->intr_tq, ath10k_pci_tasklet, (unsigned long)ar);
2365 tasklet_init(&ar_pci->msi_fw_err, ath10k_msi_err_tasklet,
2366 (unsigned long)ar);
Michal Kaziorab977bd2013-11-25 14:06:26 +01002367 tasklet_init(&ar_pci->early_irq_tasklet, ath10k_pci_early_irq_tasklet,
2368 (unsigned long)ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002369
2370 for (i = 0; i < CE_COUNT; i++) {
2371 ar_pci->pipe_info[i].ar_pci = ar_pci;
2372 tasklet_init(&ar_pci->pipe_info[i].intr, ath10k_pci_ce_tasklet,
2373 (unsigned long)&ar_pci->pipe_info[i]);
2374 }
2375}
2376
2377static int ath10k_pci_init_irq(struct ath10k *ar)
2378{
2379 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002380 bool msix_supported = test_bit(ATH10K_PCI_FEATURE_MSI_X,
2381 ar_pci->features);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002382 int ret;
2383
2384 ath10k_pci_init_irq_tasklets(ar);
2385
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002386 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_AUTO &&
2387 !test_bit(ATH10K_FLAG_FIRST_BOOT_DONE, &ar->dev_flags))
2388 ath10k_info("limiting irq mode to: %d\n", ath10k_pci_irq_mode);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002389
2390 /* Try MSI-X */
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002391 if (ath10k_pci_irq_mode == ATH10K_PCI_IRQ_AUTO && msix_supported) {
2392 ar_pci->num_msi_intrs = MSI_NUM_REQUEST;
Alexander Gordeev5ad68672014-02-13 17:50:02 +02002393 ret = pci_enable_msi_range(ar_pci->pdev, ar_pci->num_msi_intrs,
2394 ar_pci->num_msi_intrs);
2395 if (ret > 0)
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002396 return 0;
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002397
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002398 /* fall-through */
2399 }
2400
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002401 /* Try MSI */
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002402 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_LEGACY) {
2403 ar_pci->num_msi_intrs = 1;
2404 ret = pci_enable_msi(ar_pci->pdev);
2405 if (ret == 0)
2406 return 0;
2407
2408 /* fall-through */
2409 }
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002410
2411 /* Try legacy irq
2412 *
2413 * A potential race occurs here: The CORE_BASE write
2414 * depends on target correctly decoding AXI address but
2415 * host won't know when target writes BAR to CORE_CTRL.
2416 * This write might get lost if target has NOT written BAR.
2417 * For now, fix the race by repeating the write in below
2418 * synchronization checking. */
2419 ar_pci->num_msi_intrs = 0;
2420
2421 ret = ath10k_pci_wake(ar);
2422 if (ret) {
2423 ath10k_warn("failed to wake target: %d\n", ret);
2424 return ret;
2425 }
2426
2427 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2428 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
2429 ath10k_pci_sleep(ar);
2430
2431 return 0;
2432}
2433
2434static int ath10k_pci_deinit_irq_legacy(struct ath10k *ar)
2435{
2436 int ret;
2437
2438 ret = ath10k_pci_wake(ar);
2439 if (ret) {
2440 ath10k_warn("failed to wake target: %d\n", ret);
2441 return ret;
2442 }
2443
2444 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2445 0);
2446 ath10k_pci_sleep(ar);
2447
2448 return 0;
2449}
2450
2451static int ath10k_pci_deinit_irq(struct ath10k *ar)
2452{
2453 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2454
2455 switch (ar_pci->num_msi_intrs) {
2456 case 0:
2457 return ath10k_pci_deinit_irq_legacy(ar);
2458 case 1:
2459 /* fall-through */
2460 case MSI_NUM_REQUEST:
2461 pci_disable_msi(ar_pci->pdev);
2462 return 0;
Alexander Gordeevbb8b6212014-02-13 17:50:01 +02002463 default:
2464 pci_disable_msi(ar_pci->pdev);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002465 }
2466
2467 ath10k_warn("unknown irq configuration upon deinit\n");
2468 return -EINVAL;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002469}
2470
Michal Kaziord7fb47f2013-11-08 08:01:26 +01002471static int ath10k_pci_wait_for_target_init(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002472{
2473 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo0399eca2014-03-28 09:32:21 +02002474 unsigned long timeout;
Kalle Valof3782742013-10-17 11:36:15 +03002475 int ret;
Kalle Valo0399eca2014-03-28 09:32:21 +02002476 u32 val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002477
Kalle Valo50f87a62014-03-28 09:32:52 +02002478 ath10k_dbg(ATH10K_DBG_BOOT, "boot waiting target to initialise\n");
2479
Michal Kazior98563d52013-11-08 08:01:33 +01002480 ret = ath10k_pci_wake(ar);
Kalle Valof3782742013-10-17 11:36:15 +03002481 if (ret) {
Kalle Valo0399eca2014-03-28 09:32:21 +02002482 ath10k_err("failed to wake up target for init: %d\n", ret);
Kalle Valof3782742013-10-17 11:36:15 +03002483 return ret;
2484 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002485
Kalle Valo0399eca2014-03-28 09:32:21 +02002486 timeout = jiffies + msecs_to_jiffies(ATH10K_PCI_TARGET_WAIT);
2487
2488 do {
2489 val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
2490
Kalle Valo50f87a62014-03-28 09:32:52 +02002491 ath10k_dbg(ATH10K_DBG_BOOT, "boot target indicator %x\n", val);
2492
Kalle Valo0399eca2014-03-28 09:32:21 +02002493 /* target should never return this */
2494 if (val == 0xffffffff)
2495 continue;
2496
Michal Kazior7710cd22014-04-23 19:30:04 +03002497 /* the device has crashed so don't bother trying anymore */
2498 if (val & FW_IND_EVENT_PENDING)
2499 break;
2500
Kalle Valo0399eca2014-03-28 09:32:21 +02002501 if (val & FW_IND_INITIALIZED)
2502 break;
2503
Kalle Valo5e3dd152013-06-12 20:52:10 +03002504 if (ar_pci->num_msi_intrs == 0)
2505 /* Fix potential race by repeating CORE_BASE writes */
Kalle Valo0399eca2014-03-28 09:32:21 +02002506 ath10k_pci_soc_write32(ar, PCIE_INTR_ENABLE_ADDRESS,
2507 PCIE_INTR_FIRMWARE_MASK |
2508 PCIE_INTR_CE_MASK_ALL);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002509
Kalle Valo0399eca2014-03-28 09:32:21 +02002510 mdelay(10);
2511 } while (time_before(jiffies, timeout));
2512
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002513 if (val == 0xffffffff) {
2514 ath10k_err("failed to read device register, device is gone\n");
2515 ret = -EIO;
2516 goto out;
2517 }
2518
Michal Kazior7710cd22014-04-23 19:30:04 +03002519 if (val & FW_IND_EVENT_PENDING) {
2520 ath10k_warn("device has crashed during init\n");
Michal Kazior1a4ab282014-05-14 16:56:16 +03002521 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS,
2522 val & ~FW_IND_EVENT_PENDING);
2523 ath10k_pci_hif_dump_area(ar);
Michal Kazior7710cd22014-04-23 19:30:04 +03002524 ret = -ECOMM;
2525 goto out;
2526 }
2527
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002528 if (!(val & FW_IND_INITIALIZED)) {
Kalle Valo0399eca2014-03-28 09:32:21 +02002529 ath10k_err("failed to receive initialized event from target: %08x\n",
2530 val);
2531 ret = -ETIMEDOUT;
Michal Kazior5b2589f2013-11-08 08:01:30 +01002532 goto out;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002533 }
2534
Kalle Valo50f87a62014-03-28 09:32:52 +02002535 ath10k_dbg(ATH10K_DBG_BOOT, "boot target initialised\n");
2536
Michal Kazior5b2589f2013-11-08 08:01:30 +01002537out:
Michal Kazior98563d52013-11-08 08:01:33 +01002538 ath10k_pci_sleep(ar);
Michal Kazior5b2589f2013-11-08 08:01:30 +01002539 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002540}
2541
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002542static int ath10k_pci_cold_reset(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002543{
Michal Kazior5b2589f2013-11-08 08:01:30 +01002544 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002545 u32 val;
2546
Kalle Valo50f87a62014-03-28 09:32:52 +02002547 ath10k_dbg(ATH10K_DBG_BOOT, "boot cold reset\n");
2548
Michal Kazior5b2589f2013-11-08 08:01:30 +01002549 ret = ath10k_do_pci_wake(ar);
2550 if (ret) {
2551 ath10k_err("failed to wake up target: %d\n",
2552 ret);
2553 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002554 }
2555
2556 /* Put Target, including PCIe, into RESET. */
Kalle Valoe479ed42013-09-01 10:01:53 +03002557 val = ath10k_pci_reg_read32(ar, SOC_GLOBAL_RESET_ADDRESS);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002558 val |= 1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002559 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002560
2561 for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
Kalle Valoe479ed42013-09-01 10:01:53 +03002562 if (ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03002563 RTC_STATE_COLD_RESET_MASK)
2564 break;
2565 msleep(1);
2566 }
2567
2568 /* Pull Target, including PCIe, out of RESET. */
2569 val &= ~1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002570 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002571
2572 for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
Kalle Valoe479ed42013-09-01 10:01:53 +03002573 if (!(ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03002574 RTC_STATE_COLD_RESET_MASK))
2575 break;
2576 msleep(1);
2577 }
2578
Michal Kazior5b2589f2013-11-08 08:01:30 +01002579 ath10k_do_pci_sleep(ar);
Kalle Valo50f87a62014-03-28 09:32:52 +02002580
2581 ath10k_dbg(ATH10K_DBG_BOOT, "boot cold reset complete\n");
2582
Michal Kazior5b2589f2013-11-08 08:01:30 +01002583 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002584}
2585
2586static void ath10k_pci_dump_features(struct ath10k_pci *ar_pci)
2587{
2588 int i;
2589
2590 for (i = 0; i < ATH10K_PCI_FEATURE_COUNT; i++) {
2591 if (!test_bit(i, ar_pci->features))
2592 continue;
2593
2594 switch (i) {
2595 case ATH10K_PCI_FEATURE_MSI_X:
Kalle Valo24cfade2013-09-08 17:55:50 +03002596 ath10k_dbg(ATH10K_DBG_BOOT, "device supports MSI-X\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002597 break;
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002598 case ATH10K_PCI_FEATURE_SOC_POWER_SAVE:
Kalle Valo24cfade2013-09-08 17:55:50 +03002599 ath10k_dbg(ATH10K_DBG_BOOT, "QCA98XX SoC power save enabled\n");
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002600 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002601 }
2602 }
2603}
2604
2605static int ath10k_pci_probe(struct pci_dev *pdev,
2606 const struct pci_device_id *pci_dev)
2607{
2608 void __iomem *mem;
2609 int ret = 0;
2610 struct ath10k *ar;
2611 struct ath10k_pci *ar_pci;
Kalle Valoe01ae682013-09-01 11:22:14 +03002612 u32 lcr_val, chip_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002613
Kalle Valo50f87a62014-03-28 09:32:52 +02002614 ath10k_dbg(ATH10K_DBG_PCI, "pci probe\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002615
2616 ar_pci = kzalloc(sizeof(*ar_pci), GFP_KERNEL);
2617 if (ar_pci == NULL)
2618 return -ENOMEM;
2619
2620 ar_pci->pdev = pdev;
2621 ar_pci->dev = &pdev->dev;
2622
2623 switch (pci_dev->device) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03002624 case QCA988X_2_0_DEVICE_ID:
2625 set_bit(ATH10K_PCI_FEATURE_MSI_X, ar_pci->features);
2626 break;
2627 default:
2628 ret = -ENODEV;
Masanari Iida6d3be302013-09-30 23:19:09 +09002629 ath10k_err("Unknown device ID: %d\n", pci_dev->device);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002630 goto err_ar_pci;
2631 }
2632
Kalle Valoe42c1fb2014-03-28 09:32:33 +02002633 if (ath10k_pci_target_ps)
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002634 set_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE, ar_pci->features);
2635
Kalle Valo5e3dd152013-06-12 20:52:10 +03002636 ath10k_pci_dump_features(ar_pci);
2637
Michal Kazior3a0861f2013-07-05 16:15:06 +03002638 ar = ath10k_core_create(ar_pci, ar_pci->dev, &ath10k_pci_hif_ops);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002639 if (!ar) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002640 ath10k_err("failed to create driver core\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002641 ret = -EINVAL;
2642 goto err_ar_pci;
2643 }
2644
Kalle Valo5e3dd152013-06-12 20:52:10 +03002645 ar_pci->ar = ar;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002646 atomic_set(&ar_pci->keep_awake_count, 0);
2647
2648 pci_set_drvdata(pdev, ar);
2649
Kalle Valo5e3dd152013-06-12 20:52:10 +03002650 ret = pci_enable_device(pdev);
2651 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002652 ath10k_err("failed to enable PCI device: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002653 goto err_ar;
2654 }
2655
2656 /* Request MMIO resources */
2657 ret = pci_request_region(pdev, BAR_NUM, "ath");
2658 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002659 ath10k_err("failed to request MMIO region: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002660 goto err_device;
2661 }
2662
2663 /*
2664 * Target structures have a limit of 32 bit DMA pointers.
2665 * DMA pointers can be wider than 32 bits by default on some systems.
2666 */
2667 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2668 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002669 ath10k_err("failed to set DMA mask to 32-bit: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002670 goto err_region;
2671 }
2672
2673 ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2674 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002675 ath10k_err("failed to set consistent DMA mask to 32-bit\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002676 goto err_region;
2677 }
2678
2679 /* Set bus master bit in PCI_COMMAND to enable DMA */
2680 pci_set_master(pdev);
2681
2682 /*
2683 * Temporary FIX: disable ASPM
2684 * Will be removed after the OTP is programmed
2685 */
2686 pci_read_config_dword(pdev, 0x80, &lcr_val);
2687 pci_write_config_dword(pdev, 0x80, (lcr_val & 0xffffff00));
2688
2689 /* Arrange for access to Target SoC registers. */
2690 mem = pci_iomap(pdev, BAR_NUM, 0);
2691 if (!mem) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002692 ath10k_err("failed to perform IOMAP for BAR%d\n", BAR_NUM);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002693 ret = -EIO;
2694 goto err_master;
2695 }
2696
2697 ar_pci->mem = mem;
2698
2699 spin_lock_init(&ar_pci->ce_lock);
2700
Kalle Valoe01ae682013-09-01 11:22:14 +03002701 ret = ath10k_do_pci_wake(ar);
2702 if (ret) {
2703 ath10k_err("Failed to get chip id: %d\n", ret);
Wei Yongjun12eb0872013-10-30 13:24:39 +08002704 goto err_iomap;
Kalle Valoe01ae682013-09-01 11:22:14 +03002705 }
2706
Kalle Valo233eb972013-10-16 16:46:11 +03002707 chip_id = ath10k_pci_soc_read32(ar, SOC_CHIP_ID_ADDRESS);
Kalle Valoe01ae682013-09-01 11:22:14 +03002708
2709 ath10k_do_pci_sleep(ar);
2710
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002711 ret = ath10k_pci_alloc_ce(ar);
2712 if (ret) {
2713 ath10k_err("failed to allocate copy engine pipes: %d\n", ret);
2714 goto err_iomap;
2715 }
2716
Kalle Valo24cfade2013-09-08 17:55:50 +03002717 ath10k_dbg(ATH10K_DBG_BOOT, "boot pci_mem 0x%p\n", ar_pci->mem);
2718
Kalle Valoe01ae682013-09-01 11:22:14 +03002719 ret = ath10k_core_register(ar, chip_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002720 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002721 ath10k_err("failed to register driver core: %d\n", ret);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002722 goto err_free_ce;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002723 }
2724
2725 return 0;
2726
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002727err_free_ce:
2728 ath10k_pci_free_ce(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002729err_iomap:
2730 pci_iounmap(pdev, mem);
2731err_master:
2732 pci_clear_master(pdev);
2733err_region:
2734 pci_release_region(pdev, BAR_NUM);
2735err_device:
2736 pci_disable_device(pdev);
2737err_ar:
Kalle Valo5e3dd152013-06-12 20:52:10 +03002738 ath10k_core_destroy(ar);
2739err_ar_pci:
2740 /* call HIF PCI free here */
2741 kfree(ar_pci);
2742
2743 return ret;
2744}
2745
2746static void ath10k_pci_remove(struct pci_dev *pdev)
2747{
2748 struct ath10k *ar = pci_get_drvdata(pdev);
2749 struct ath10k_pci *ar_pci;
2750
Kalle Valo50f87a62014-03-28 09:32:52 +02002751 ath10k_dbg(ATH10K_DBG_PCI, "pci remove\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002752
2753 if (!ar)
2754 return;
2755
2756 ar_pci = ath10k_pci_priv(ar);
2757
2758 if (!ar_pci)
2759 return;
2760
2761 tasklet_kill(&ar_pci->msi_fw_err);
2762
2763 ath10k_core_unregister(ar);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002764 ath10k_pci_free_ce(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002765
Kalle Valo5e3dd152013-06-12 20:52:10 +03002766 pci_iounmap(pdev, ar_pci->mem);
2767 pci_release_region(pdev, BAR_NUM);
2768 pci_clear_master(pdev);
2769 pci_disable_device(pdev);
2770
2771 ath10k_core_destroy(ar);
2772 kfree(ar_pci);
2773}
2774
Kalle Valo5e3dd152013-06-12 20:52:10 +03002775MODULE_DEVICE_TABLE(pci, ath10k_pci_id_table);
2776
2777static struct pci_driver ath10k_pci_driver = {
2778 .name = "ath10k_pci",
2779 .id_table = ath10k_pci_id_table,
2780 .probe = ath10k_pci_probe,
2781 .remove = ath10k_pci_remove,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002782};
2783
2784static int __init ath10k_pci_init(void)
2785{
2786 int ret;
2787
2788 ret = pci_register_driver(&ath10k_pci_driver);
2789 if (ret)
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002790 ath10k_err("failed to register PCI driver: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002791
2792 return ret;
2793}
2794module_init(ath10k_pci_init);
2795
2796static void __exit ath10k_pci_exit(void)
2797{
2798 pci_unregister_driver(&ath10k_pci_driver);
2799}
2800
2801module_exit(ath10k_pci_exit);
2802
2803MODULE_AUTHOR("Qualcomm Atheros");
2804MODULE_DESCRIPTION("Driver support for Atheros QCA988X PCIe devices");
2805MODULE_LICENSE("Dual BSD/GPL");
Kalle Valo929417c2014-03-28 09:32:39 +02002806MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_FW_2_FILE);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002807MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_BOARD_DATA_FILE);