blob: 71ab110a4c4d2486e607d62f9fcf00151e1ea303 [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 Kaziorf2708be2014-05-16 17:15:39 +03001275 if (WARN_ON(!ar_pci->started))
1276 return;
1277
Michal Kazior5d1aa942013-11-25 14:06:24 +01001278 ret = ath10k_ce_disable_interrupts(ar);
1279 if (ret)
1280 ath10k_warn("failed to disable CE interrupts: %d\n", ret);
Michal Kazior32270b62013-08-02 09:15:47 +02001281
Michal Kazior5d1aa942013-11-25 14:06:24 +01001282 ath10k_pci_free_irq(ar);
1283 ath10k_pci_kill_tasklet(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001284
Michal Kaziorab977bd2013-11-25 14:06:26 +01001285 ret = ath10k_pci_request_early_irq(ar);
1286 if (ret)
1287 ath10k_warn("failed to re-enable early irq: %d\n", ret);
1288
Kalle Valo5e3dd152013-06-12 20:52:10 +03001289 /* At this point, asynchronous threads are stopped, the target should
1290 * not DMA nor interrupt. We process the leftovers and then free
1291 * everything else up. */
1292
Kalle Valo5e3dd152013-06-12 20:52:10 +03001293 ath10k_pci_buffer_cleanup(ar);
Michal Kazior32270b62013-08-02 09:15:47 +02001294
Michal Kazior6a42a472013-11-08 08:01:35 +01001295 /* Make the sure the device won't access any structures on the host by
1296 * resetting it. The device was fed with PCI CE ringbuffer
1297 * configuration during init. If ringbuffers are freed and the device
1298 * were to access them this could lead to memory corruption on the
1299 * host. */
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001300 ath10k_pci_warm_reset(ar);
Michal Kazior6a42a472013-11-08 08:01:35 +01001301
Michal Kazior32270b62013-08-02 09:15:47 +02001302 ar_pci->started = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001303}
1304
1305static int ath10k_pci_hif_exchange_bmi_msg(struct ath10k *ar,
1306 void *req, u32 req_len,
1307 void *resp, u32 *resp_len)
1308{
1309 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2aa39112013-08-27 13:08:02 +02001310 struct ath10k_pci_pipe *pci_tx = &ar_pci->pipe_info[BMI_CE_NUM_TO_TARG];
1311 struct ath10k_pci_pipe *pci_rx = &ar_pci->pipe_info[BMI_CE_NUM_TO_HOST];
1312 struct ath10k_ce_pipe *ce_tx = pci_tx->ce_hdl;
1313 struct ath10k_ce_pipe *ce_rx = pci_rx->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001314 dma_addr_t req_paddr = 0;
1315 dma_addr_t resp_paddr = 0;
1316 struct bmi_xfer xfer = {};
1317 void *treq, *tresp = NULL;
1318 int ret = 0;
1319
Michal Kazior85622cd2013-11-25 14:06:22 +01001320 might_sleep();
1321
Kalle Valo5e3dd152013-06-12 20:52:10 +03001322 if (resp && !resp_len)
1323 return -EINVAL;
1324
1325 if (resp && resp_len && *resp_len == 0)
1326 return -EINVAL;
1327
1328 treq = kmemdup(req, req_len, GFP_KERNEL);
1329 if (!treq)
1330 return -ENOMEM;
1331
1332 req_paddr = dma_map_single(ar->dev, treq, req_len, DMA_TO_DEVICE);
1333 ret = dma_mapping_error(ar->dev, req_paddr);
1334 if (ret)
1335 goto err_dma;
1336
1337 if (resp && resp_len) {
1338 tresp = kzalloc(*resp_len, GFP_KERNEL);
1339 if (!tresp) {
1340 ret = -ENOMEM;
1341 goto err_req;
1342 }
1343
1344 resp_paddr = dma_map_single(ar->dev, tresp, *resp_len,
1345 DMA_FROM_DEVICE);
1346 ret = dma_mapping_error(ar->dev, resp_paddr);
1347 if (ret)
1348 goto err_req;
1349
1350 xfer.wait_for_resp = true;
1351 xfer.resp_len = 0;
1352
1353 ath10k_ce_recv_buf_enqueue(ce_rx, &xfer, resp_paddr);
1354 }
1355
1356 init_completion(&xfer.done);
1357
1358 ret = ath10k_ce_send(ce_tx, &xfer, req_paddr, req_len, -1, 0);
1359 if (ret)
1360 goto err_resp;
1361
Michal Kazior85622cd2013-11-25 14:06:22 +01001362 ret = ath10k_pci_bmi_wait(ce_tx, ce_rx, &xfer);
1363 if (ret) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001364 u32 unused_buffer;
1365 unsigned int unused_nbytes;
1366 unsigned int unused_id;
1367
Kalle Valo5e3dd152013-06-12 20:52:10 +03001368 ath10k_ce_cancel_send_next(ce_tx, NULL, &unused_buffer,
1369 &unused_nbytes, &unused_id);
1370 } else {
1371 /* non-zero means we did not time out */
1372 ret = 0;
1373 }
1374
1375err_resp:
1376 if (resp) {
1377 u32 unused_buffer;
1378
1379 ath10k_ce_revoke_recv_next(ce_rx, NULL, &unused_buffer);
1380 dma_unmap_single(ar->dev, resp_paddr,
1381 *resp_len, DMA_FROM_DEVICE);
1382 }
1383err_req:
1384 dma_unmap_single(ar->dev, req_paddr, req_len, DMA_TO_DEVICE);
1385
1386 if (ret == 0 && resp_len) {
1387 *resp_len = min(*resp_len, xfer.resp_len);
1388 memcpy(resp, tresp, xfer.resp_len);
1389 }
1390err_dma:
1391 kfree(treq);
1392 kfree(tresp);
1393
1394 return ret;
1395}
1396
Michal Kazior5440ce22013-09-03 15:09:58 +02001397static void ath10k_pci_bmi_send_done(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001398{
Michal Kazior5440ce22013-09-03 15:09:58 +02001399 struct bmi_xfer *xfer;
1400 u32 ce_data;
1401 unsigned int nbytes;
1402 unsigned int transfer_id;
1403
1404 if (ath10k_ce_completed_send_next(ce_state, (void **)&xfer, &ce_data,
1405 &nbytes, &transfer_id))
1406 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001407
1408 if (xfer->wait_for_resp)
1409 return;
1410
1411 complete(&xfer->done);
1412}
1413
Michal Kazior5440ce22013-09-03 15:09:58 +02001414static void ath10k_pci_bmi_recv_data(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001415{
Michal Kazior5440ce22013-09-03 15:09:58 +02001416 struct bmi_xfer *xfer;
1417 u32 ce_data;
1418 unsigned int nbytes;
1419 unsigned int transfer_id;
1420 unsigned int flags;
1421
1422 if (ath10k_ce_completed_recv_next(ce_state, (void **)&xfer, &ce_data,
1423 &nbytes, &transfer_id, &flags))
1424 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001425
1426 if (!xfer->wait_for_resp) {
1427 ath10k_warn("unexpected: BMI data received; ignoring\n");
1428 return;
1429 }
1430
1431 xfer->resp_len = nbytes;
1432 complete(&xfer->done);
1433}
1434
Michal Kazior85622cd2013-11-25 14:06:22 +01001435static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
1436 struct ath10k_ce_pipe *rx_pipe,
1437 struct bmi_xfer *xfer)
1438{
1439 unsigned long timeout = jiffies + BMI_COMMUNICATION_TIMEOUT_HZ;
1440
1441 while (time_before_eq(jiffies, timeout)) {
1442 ath10k_pci_bmi_send_done(tx_pipe);
1443 ath10k_pci_bmi_recv_data(rx_pipe);
1444
1445 if (completion_done(&xfer->done))
1446 return 0;
1447
1448 schedule();
1449 }
1450
1451 return -ETIMEDOUT;
1452}
1453
Kalle Valo5e3dd152013-06-12 20:52:10 +03001454/*
1455 * Map from service/endpoint to Copy Engine.
1456 * This table is derived from the CE_PCI TABLE, above.
1457 * It is passed to the Target at startup for use by firmware.
1458 */
1459static const struct service_to_pipe target_service_to_ce_map_wlan[] = {
1460 {
1461 ATH10K_HTC_SVC_ID_WMI_DATA_VO,
1462 PIPEDIR_OUT, /* out = UL = host -> target */
1463 3,
1464 },
1465 {
1466 ATH10K_HTC_SVC_ID_WMI_DATA_VO,
1467 PIPEDIR_IN, /* in = DL = target -> host */
1468 2,
1469 },
1470 {
1471 ATH10K_HTC_SVC_ID_WMI_DATA_BK,
1472 PIPEDIR_OUT, /* out = UL = host -> target */
1473 3,
1474 },
1475 {
1476 ATH10K_HTC_SVC_ID_WMI_DATA_BK,
1477 PIPEDIR_IN, /* in = DL = target -> host */
1478 2,
1479 },
1480 {
1481 ATH10K_HTC_SVC_ID_WMI_DATA_BE,
1482 PIPEDIR_OUT, /* out = UL = host -> target */
1483 3,
1484 },
1485 {
1486 ATH10K_HTC_SVC_ID_WMI_DATA_BE,
1487 PIPEDIR_IN, /* in = DL = target -> host */
1488 2,
1489 },
1490 {
1491 ATH10K_HTC_SVC_ID_WMI_DATA_VI,
1492 PIPEDIR_OUT, /* out = UL = host -> target */
1493 3,
1494 },
1495 {
1496 ATH10K_HTC_SVC_ID_WMI_DATA_VI,
1497 PIPEDIR_IN, /* in = DL = target -> host */
1498 2,
1499 },
1500 {
1501 ATH10K_HTC_SVC_ID_WMI_CONTROL,
1502 PIPEDIR_OUT, /* out = UL = host -> target */
1503 3,
1504 },
1505 {
1506 ATH10K_HTC_SVC_ID_WMI_CONTROL,
1507 PIPEDIR_IN, /* in = DL = target -> host */
1508 2,
1509 },
1510 {
1511 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1512 PIPEDIR_OUT, /* out = UL = host -> target */
1513 0, /* could be moved to 3 (share with WMI) */
1514 },
1515 {
1516 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1517 PIPEDIR_IN, /* in = DL = target -> host */
1518 1,
1519 },
1520 {
1521 ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS, /* not currently used */
1522 PIPEDIR_OUT, /* out = UL = host -> target */
1523 0,
1524 },
1525 {
1526 ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS, /* not currently used */
1527 PIPEDIR_IN, /* in = DL = target -> host */
1528 1,
1529 },
1530 {
1531 ATH10K_HTC_SVC_ID_HTT_DATA_MSG,
1532 PIPEDIR_OUT, /* out = UL = host -> target */
1533 4,
1534 },
1535 {
1536 ATH10K_HTC_SVC_ID_HTT_DATA_MSG,
1537 PIPEDIR_IN, /* in = DL = target -> host */
1538 1,
1539 },
1540
1541 /* (Additions here) */
1542
1543 { /* Must be last */
1544 0,
1545 0,
1546 0,
1547 },
1548};
1549
1550/*
1551 * Send an interrupt to the device to wake up the Target CPU
1552 * so it has an opportunity to notice any changed state.
1553 */
1554static int ath10k_pci_wake_target_cpu(struct ath10k *ar)
1555{
1556 int ret;
1557 u32 core_ctrl;
1558
1559 ret = ath10k_pci_diag_read_access(ar, SOC_CORE_BASE_ADDRESS |
1560 CORE_CTRL_ADDRESS,
1561 &core_ctrl);
1562 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001563 ath10k_warn("failed to read core_ctrl: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001564 return ret;
1565 }
1566
1567 /* A_INUM_FIRMWARE interrupt to Target CPU */
1568 core_ctrl |= CORE_CTRL_CPU_INTR_MASK;
1569
1570 ret = ath10k_pci_diag_write_access(ar, SOC_CORE_BASE_ADDRESS |
1571 CORE_CTRL_ADDRESS,
1572 core_ctrl);
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001573 if (ret) {
1574 ath10k_warn("failed to set target CPU interrupt mask: %d\n",
1575 ret);
1576 return ret;
1577 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001578
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001579 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001580}
1581
1582static int ath10k_pci_init_config(struct ath10k *ar)
1583{
1584 u32 interconnect_targ_addr;
1585 u32 pcie_state_targ_addr = 0;
1586 u32 pipe_cfg_targ_addr = 0;
1587 u32 svc_to_pipe_map = 0;
1588 u32 pcie_config_flags = 0;
1589 u32 ealloc_value;
1590 u32 ealloc_targ_addr;
1591 u32 flag2_value;
1592 u32 flag2_targ_addr;
1593 int ret = 0;
1594
1595 /* Download to Target the CE Config and the service-to-CE map */
1596 interconnect_targ_addr =
1597 host_interest_item_address(HI_ITEM(hi_interconnect_state));
1598
1599 /* Supply Target-side CE configuration */
1600 ret = ath10k_pci_diag_read_access(ar, interconnect_targ_addr,
1601 &pcie_state_targ_addr);
1602 if (ret != 0) {
1603 ath10k_err("Failed to get pcie state addr: %d\n", ret);
1604 return ret;
1605 }
1606
1607 if (pcie_state_targ_addr == 0) {
1608 ret = -EIO;
1609 ath10k_err("Invalid pcie state addr\n");
1610 return ret;
1611 }
1612
1613 ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
1614 offsetof(struct pcie_state,
1615 pipe_cfg_addr),
1616 &pipe_cfg_targ_addr);
1617 if (ret != 0) {
1618 ath10k_err("Failed to get pipe cfg addr: %d\n", ret);
1619 return ret;
1620 }
1621
1622 if (pipe_cfg_targ_addr == 0) {
1623 ret = -EIO;
1624 ath10k_err("Invalid pipe cfg addr\n");
1625 return ret;
1626 }
1627
1628 ret = ath10k_pci_diag_write_mem(ar, pipe_cfg_targ_addr,
1629 target_ce_config_wlan,
1630 sizeof(target_ce_config_wlan));
1631
1632 if (ret != 0) {
1633 ath10k_err("Failed to write pipe cfg: %d\n", ret);
1634 return ret;
1635 }
1636
1637 ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
1638 offsetof(struct pcie_state,
1639 svc_to_pipe_map),
1640 &svc_to_pipe_map);
1641 if (ret != 0) {
1642 ath10k_err("Failed to get svc/pipe map: %d\n", ret);
1643 return ret;
1644 }
1645
1646 if (svc_to_pipe_map == 0) {
1647 ret = -EIO;
1648 ath10k_err("Invalid svc_to_pipe map\n");
1649 return ret;
1650 }
1651
1652 ret = ath10k_pci_diag_write_mem(ar, svc_to_pipe_map,
1653 target_service_to_ce_map_wlan,
1654 sizeof(target_service_to_ce_map_wlan));
1655 if (ret != 0) {
1656 ath10k_err("Failed to write svc/pipe map: %d\n", ret);
1657 return ret;
1658 }
1659
1660 ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
1661 offsetof(struct pcie_state,
1662 config_flags),
1663 &pcie_config_flags);
1664 if (ret != 0) {
1665 ath10k_err("Failed to get pcie config_flags: %d\n", ret);
1666 return ret;
1667 }
1668
1669 pcie_config_flags &= ~PCIE_CONFIG_FLAG_ENABLE_L1;
1670
1671 ret = ath10k_pci_diag_write_mem(ar, pcie_state_targ_addr +
1672 offsetof(struct pcie_state, config_flags),
1673 &pcie_config_flags,
1674 sizeof(pcie_config_flags));
1675 if (ret != 0) {
1676 ath10k_err("Failed to write pcie config_flags: %d\n", ret);
1677 return ret;
1678 }
1679
1680 /* configure early allocation */
1681 ealloc_targ_addr = host_interest_item_address(HI_ITEM(hi_early_alloc));
1682
1683 ret = ath10k_pci_diag_read_access(ar, ealloc_targ_addr, &ealloc_value);
1684 if (ret != 0) {
1685 ath10k_err("Faile to get early alloc val: %d\n", ret);
1686 return ret;
1687 }
1688
1689 /* first bank is switched to IRAM */
1690 ealloc_value |= ((HI_EARLY_ALLOC_MAGIC << HI_EARLY_ALLOC_MAGIC_SHIFT) &
1691 HI_EARLY_ALLOC_MAGIC_MASK);
1692 ealloc_value |= ((1 << HI_EARLY_ALLOC_IRAM_BANKS_SHIFT) &
1693 HI_EARLY_ALLOC_IRAM_BANKS_MASK);
1694
1695 ret = ath10k_pci_diag_write_access(ar, ealloc_targ_addr, ealloc_value);
1696 if (ret != 0) {
1697 ath10k_err("Failed to set early alloc val: %d\n", ret);
1698 return ret;
1699 }
1700
1701 /* Tell Target to proceed with initialization */
1702 flag2_targ_addr = host_interest_item_address(HI_ITEM(hi_option_flag2));
1703
1704 ret = ath10k_pci_diag_read_access(ar, flag2_targ_addr, &flag2_value);
1705 if (ret != 0) {
1706 ath10k_err("Failed to get option val: %d\n", ret);
1707 return ret;
1708 }
1709
1710 flag2_value |= HI_OPTION_EARLY_CFG_DONE;
1711
1712 ret = ath10k_pci_diag_write_access(ar, flag2_targ_addr, flag2_value);
1713 if (ret != 0) {
1714 ath10k_err("Failed to set option val: %d\n", ret);
1715 return ret;
1716 }
1717
1718 return 0;
1719}
1720
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001721static int ath10k_pci_alloc_ce(struct ath10k *ar)
1722{
1723 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001724
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001725 for (i = 0; i < CE_COUNT; i++) {
1726 ret = ath10k_ce_alloc_pipe(ar, i, &host_ce_config_wlan[i]);
1727 if (ret) {
1728 ath10k_err("failed to allocate copy engine pipe %d: %d\n",
1729 i, ret);
1730 return ret;
1731 }
1732 }
1733
1734 return 0;
1735}
1736
1737static void ath10k_pci_free_ce(struct ath10k *ar)
1738{
1739 int i;
1740
1741 for (i = 0; i < CE_COUNT; i++)
1742 ath10k_ce_free_pipe(ar, i);
1743}
Kalle Valo5e3dd152013-06-12 20:52:10 +03001744
1745static int ath10k_pci_ce_init(struct ath10k *ar)
1746{
1747 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +02001748 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001749 const struct ce_attr *attr;
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001750 int pipe_num, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001751
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001752 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001753 pipe_info = &ar_pci->pipe_info[pipe_num];
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001754 pipe_info->ce_hdl = &ar_pci->ce_states[pipe_num];
Kalle Valo5e3dd152013-06-12 20:52:10 +03001755 pipe_info->pipe_num = pipe_num;
1756 pipe_info->hif_ce_state = ar;
1757 attr = &host_ce_config_wlan[pipe_num];
1758
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001759 ret = ath10k_ce_init_pipe(ar, pipe_num, attr);
1760 if (ret) {
1761 ath10k_err("failed to initialize copy engine pipe %d: %d\n",
1762 pipe_num, ret);
1763 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001764 }
1765
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001766 if (pipe_num == CE_COUNT - 1) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001767 /*
1768 * Reserve the ultimate CE for
1769 * diagnostic Window support
1770 */
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001771 ar_pci->ce_diag = pipe_info->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001772 continue;
1773 }
1774
1775 pipe_info->buf_sz = (size_t) (attr->src_sz_max);
1776 }
1777
Kalle Valo5e3dd152013-06-12 20:52:10 +03001778 return 0;
1779}
1780
1781static void ath10k_pci_fw_interrupt_handler(struct ath10k *ar)
1782{
1783 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valob39712c2014-03-28 09:32:46 +02001784 u32 fw_indicator;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001785
1786 ath10k_pci_wake(ar);
1787
Kalle Valob39712c2014-03-28 09:32:46 +02001788 fw_indicator = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001789
1790 if (fw_indicator & FW_IND_EVENT_PENDING) {
1791 /* ACK: clear Target-side pending event */
Kalle Valob39712c2014-03-28 09:32:46 +02001792 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001793 fw_indicator & ~FW_IND_EVENT_PENDING);
1794
1795 if (ar_pci->started) {
1796 ath10k_pci_hif_dump_area(ar);
1797 } else {
1798 /*
1799 * Probable Target failure before we're prepared
1800 * to handle it. Generally unexpected.
1801 */
1802 ath10k_warn("early firmware event indicated\n");
1803 }
1804 }
1805
1806 ath10k_pci_sleep(ar);
1807}
1808
Michal Kaziorde013572014-05-14 16:56:16 +03001809/* this function effectively clears target memory controller assert line */
1810static void ath10k_pci_warm_reset_si0(struct ath10k *ar)
1811{
1812 u32 val;
1813
1814 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1815 ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
1816 val | SOC_RESET_CONTROL_SI0_RST_MASK);
1817 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1818
1819 msleep(10);
1820
1821 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1822 ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
1823 val & ~SOC_RESET_CONTROL_SI0_RST_MASK);
1824 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1825
1826 msleep(10);
1827}
1828
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001829static int ath10k_pci_warm_reset(struct ath10k *ar)
1830{
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001831 int ret = 0;
1832 u32 val;
1833
Kalle Valo50f87a62014-03-28 09:32:52 +02001834 ath10k_dbg(ATH10K_DBG_BOOT, "boot warm reset\n");
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001835
1836 ret = ath10k_do_pci_wake(ar);
1837 if (ret) {
1838 ath10k_err("failed to wake up target: %d\n", ret);
1839 return ret;
1840 }
1841
1842 /* debug */
1843 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1844 PCIE_INTR_CAUSE_ADDRESS);
1845 ath10k_dbg(ATH10K_DBG_BOOT, "boot host cpu intr cause: 0x%08x\n", val);
1846
1847 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1848 CPU_INTR_ADDRESS);
1849 ath10k_dbg(ATH10K_DBG_BOOT, "boot target cpu intr cause: 0x%08x\n",
1850 val);
1851
1852 /* disable pending irqs */
1853 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
1854 PCIE_INTR_ENABLE_ADDRESS, 0);
1855
1856 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
1857 PCIE_INTR_CLR_ADDRESS, ~0);
1858
1859 msleep(100);
1860
1861 /* clear fw indicator */
Kalle Valob39712c2014-03-28 09:32:46 +02001862 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, 0);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001863
1864 /* clear target LF timer interrupts */
1865 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1866 SOC_LF_TIMER_CONTROL0_ADDRESS);
1867 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS +
1868 SOC_LF_TIMER_CONTROL0_ADDRESS,
1869 val & ~SOC_LF_TIMER_CONTROL0_ENABLE_MASK);
1870
1871 /* reset CE */
1872 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1873 SOC_RESET_CONTROL_ADDRESS);
1874 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1875 val | SOC_RESET_CONTROL_CE_RST_MASK);
1876 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1877 SOC_RESET_CONTROL_ADDRESS);
1878 msleep(10);
1879
1880 /* unreset CE */
1881 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1882 val & ~SOC_RESET_CONTROL_CE_RST_MASK);
1883 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1884 SOC_RESET_CONTROL_ADDRESS);
1885 msleep(10);
1886
Michal Kaziorde013572014-05-14 16:56:16 +03001887 ath10k_pci_warm_reset_si0(ar);
1888
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001889 /* debug */
1890 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1891 PCIE_INTR_CAUSE_ADDRESS);
1892 ath10k_dbg(ATH10K_DBG_BOOT, "boot host cpu intr cause: 0x%08x\n", val);
1893
1894 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1895 CPU_INTR_ADDRESS);
1896 ath10k_dbg(ATH10K_DBG_BOOT, "boot target cpu intr cause: 0x%08x\n",
1897 val);
1898
1899 /* CPU warm reset */
1900 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1901 SOC_RESET_CONTROL_ADDRESS);
1902 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1903 val | SOC_RESET_CONTROL_CPU_WARM_RST_MASK);
1904
1905 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1906 SOC_RESET_CONTROL_ADDRESS);
1907 ath10k_dbg(ATH10K_DBG_BOOT, "boot target reset state: 0x%08x\n", val);
1908
1909 msleep(100);
1910
1911 ath10k_dbg(ATH10K_DBG_BOOT, "boot warm reset complete\n");
1912
1913 ath10k_do_pci_sleep(ar);
1914 return ret;
1915}
1916
1917static int __ath10k_pci_hif_power_up(struct ath10k *ar, bool cold_reset)
Michal Kazior8c5c5362013-07-16 09:38:50 +02001918{
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02001919 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo95cbb6a2013-11-20 10:00:35 +02001920 const char *irq_mode;
Michal Kazior8c5c5362013-07-16 09:38:50 +02001921 int ret;
1922
1923 /*
1924 * Bring the target up cleanly.
1925 *
1926 * The target may be in an undefined state with an AUX-powered Target
1927 * and a Host in WoW mode. If the Host crashes, loses power, or is
1928 * restarted (without unloading the driver) then the Target is left
1929 * (aux) powered and running. On a subsequent driver load, the Target
1930 * is in an unexpected state. We try to catch that here in order to
1931 * reset the Target and retry the probe.
1932 */
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001933 if (cold_reset)
1934 ret = ath10k_pci_cold_reset(ar);
1935 else
1936 ret = ath10k_pci_warm_reset(ar);
1937
Michal Kazior5b2589f2013-11-08 08:01:30 +01001938 if (ret) {
1939 ath10k_err("failed to reset target: %d\n", ret);
Michal Kazior98563d52013-11-08 08:01:33 +01001940 goto err;
Michal Kazior5b2589f2013-11-08 08:01:30 +01001941 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02001942
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02001943 if (!test_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE, ar_pci->features))
Michal Kazior8c5c5362013-07-16 09:38:50 +02001944 /* Force AWAKE forever */
Michal Kazior8c5c5362013-07-16 09:38:50 +02001945 ath10k_do_pci_wake(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001946
1947 ret = ath10k_pci_ce_init(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001948 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001949 ath10k_err("failed to initialize CE: %d\n", ret);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001950 goto err_ps;
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001951 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02001952
Michal Kazior98563d52013-11-08 08:01:33 +01001953 ret = ath10k_ce_disable_interrupts(ar);
1954 if (ret) {
1955 ath10k_err("failed to disable CE interrupts: %d\n", ret);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001956 goto err_ce;
1957 }
1958
Michal Kaziorfc15ca12013-11-25 14:06:21 +01001959 ret = ath10k_pci_init_irq(ar);
Michal Kazior98563d52013-11-08 08:01:33 +01001960 if (ret) {
Michal Kaziorfc15ca12013-11-25 14:06:21 +01001961 ath10k_err("failed to init irqs: %d\n", ret);
Michal Kazior98563d52013-11-08 08:01:33 +01001962 goto err_ce;
1963 }
1964
Michal Kaziorab977bd2013-11-25 14:06:26 +01001965 ret = ath10k_pci_request_early_irq(ar);
1966 if (ret) {
1967 ath10k_err("failed to request early irq: %d\n", ret);
1968 goto err_deinit_irq;
1969 }
1970
Michal Kazior98563d52013-11-08 08:01:33 +01001971 ret = ath10k_pci_wait_for_target_init(ar);
1972 if (ret) {
1973 ath10k_err("failed to wait for target to init: %d\n", ret);
Michal Kaziorab977bd2013-11-25 14:06:26 +01001974 goto err_free_early_irq;
Michal Kazior98563d52013-11-08 08:01:33 +01001975 }
1976
1977 ret = ath10k_pci_init_config(ar);
1978 if (ret) {
1979 ath10k_err("failed to setup init config: %d\n", ret);
Michal Kaziorab977bd2013-11-25 14:06:26 +01001980 goto err_free_early_irq;
Michal Kazior98563d52013-11-08 08:01:33 +01001981 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02001982
1983 ret = ath10k_pci_wake_target_cpu(ar);
1984 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001985 ath10k_err("could not wake up target CPU: %d\n", ret);
Michal Kaziorab977bd2013-11-25 14:06:26 +01001986 goto err_free_early_irq;
Michal Kazior8c5c5362013-07-16 09:38:50 +02001987 }
1988
Kalle Valo95cbb6a2013-11-20 10:00:35 +02001989 if (ar_pci->num_msi_intrs > 1)
1990 irq_mode = "MSI-X";
1991 else if (ar_pci->num_msi_intrs == 1)
1992 irq_mode = "MSI";
1993 else
1994 irq_mode = "legacy";
1995
Kalle Valo650b91f2013-11-20 10:00:49 +02001996 if (!test_bit(ATH10K_FLAG_FIRST_BOOT_DONE, &ar->dev_flags))
Kalle Valo78a9cb42014-03-28 09:32:58 +02001997 ath10k_info("pci irq %s irq_mode %d reset_mode %d\n",
1998 irq_mode, ath10k_pci_irq_mode,
1999 ath10k_pci_reset_mode);
Kalle Valo95cbb6a2013-11-20 10:00:35 +02002000
Michal Kazior8c5c5362013-07-16 09:38:50 +02002001 return 0;
2002
Michal Kaziorab977bd2013-11-25 14:06:26 +01002003err_free_early_irq:
2004 ath10k_pci_free_early_irq(ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002005err_deinit_irq:
2006 ath10k_pci_deinit_irq(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02002007err_ce:
2008 ath10k_pci_ce_deinit(ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002009 ath10k_pci_warm_reset(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02002010err_ps:
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002011 if (!test_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE, ar_pci->features))
Michal Kazior8c5c5362013-07-16 09:38:50 +02002012 ath10k_do_pci_sleep(ar);
2013err:
2014 return ret;
2015}
2016
Michal Kazior61c95ce2014-05-14 16:56:16 +03002017static int ath10k_pci_hif_power_up_warm(struct ath10k *ar)
2018{
2019 int i, ret;
2020
2021 /*
2022 * Sometime warm reset succeeds after retries.
2023 *
2024 * FIXME: It might be possible to tune ath10k_pci_warm_reset() to work
2025 * at first try.
2026 */
2027 for (i = 0; i < ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS; i++) {
2028 ret = __ath10k_pci_hif_power_up(ar, false);
2029 if (ret == 0)
2030 break;
2031
2032 ath10k_warn("failed to warm reset (attempt %d out of %d): %d\n",
2033 i + 1, ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS, ret);
2034 }
2035
2036 return ret;
2037}
2038
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002039static int ath10k_pci_hif_power_up(struct ath10k *ar)
2040{
2041 int ret;
2042
Kalle Valo50f87a62014-03-28 09:32:52 +02002043 ath10k_dbg(ATH10K_DBG_BOOT, "boot hif power up\n");
2044
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002045 /*
2046 * Hardware CUS232 version 2 has some issues with cold reset and the
2047 * preferred (and safer) way to perform a device reset is through a
2048 * warm reset.
2049 *
Michal Kazior61c95ce2014-05-14 16:56:16 +03002050 * Warm reset doesn't always work though so fall back to cold reset may
2051 * be necessary.
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002052 */
Michal Kazior61c95ce2014-05-14 16:56:16 +03002053 ret = ath10k_pci_hif_power_up_warm(ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002054 if (ret) {
Kalle Valo35098462014-03-28 09:32:27 +02002055 ath10k_warn("failed to power up target using warm reset: %d\n",
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002056 ret);
2057
Kalle Valo35098462014-03-28 09:32:27 +02002058 if (ath10k_pci_reset_mode == ATH10K_PCI_RESET_WARM_ONLY)
2059 return ret;
2060
2061 ath10k_warn("trying cold reset\n");
2062
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002063 ret = __ath10k_pci_hif_power_up(ar, true);
2064 if (ret) {
2065 ath10k_err("failed to power up target using cold reset too (%d)\n",
2066 ret);
2067 return ret;
2068 }
2069 }
2070
2071 return 0;
2072}
2073
Michal Kazior8c5c5362013-07-16 09:38:50 +02002074static void ath10k_pci_hif_power_down(struct ath10k *ar)
2075{
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002076 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2077
Kalle Valo50f87a62014-03-28 09:32:52 +02002078 ath10k_dbg(ATH10K_DBG_BOOT, "boot hif power down\n");
2079
Michal Kaziorab977bd2013-11-25 14:06:26 +01002080 ath10k_pci_free_early_irq(ar);
2081 ath10k_pci_kill_tasklet(ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002082 ath10k_pci_deinit_irq(ar);
Michal Kaziordf5e8522014-03-28 10:02:45 +02002083 ath10k_pci_ce_deinit(ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002084 ath10k_pci_warm_reset(ar);
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002085
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002086 if (!test_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE, ar_pci->features))
Michal Kazior8c5c5362013-07-16 09:38:50 +02002087 ath10k_do_pci_sleep(ar);
2088}
2089
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002090#ifdef CONFIG_PM
2091
2092#define ATH10K_PCI_PM_CONTROL 0x44
2093
2094static int ath10k_pci_hif_suspend(struct ath10k *ar)
2095{
2096 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2097 struct pci_dev *pdev = ar_pci->pdev;
2098 u32 val;
2099
2100 pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
2101
2102 if ((val & 0x000000ff) != 0x3) {
2103 pci_save_state(pdev);
2104 pci_disable_device(pdev);
2105 pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
2106 (val & 0xffffff00) | 0x03);
2107 }
2108
2109 return 0;
2110}
2111
2112static int ath10k_pci_hif_resume(struct ath10k *ar)
2113{
2114 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2115 struct pci_dev *pdev = ar_pci->pdev;
2116 u32 val;
2117
2118 pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
2119
2120 if ((val & 0x000000ff) != 0) {
2121 pci_restore_state(pdev);
2122 pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
2123 val & 0xffffff00);
2124 /*
2125 * Suspend/Resume resets the PCI configuration space,
2126 * so we have to re-disable the RETRY_TIMEOUT register (0x41)
2127 * to keep PCI Tx retries from interfering with C3 CPU state
2128 */
2129 pci_read_config_dword(pdev, 0x40, &val);
2130
2131 if ((val & 0x0000ff00) != 0)
2132 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
2133 }
2134
2135 return 0;
2136}
2137#endif
2138
Kalle Valo5e3dd152013-06-12 20:52:10 +03002139static const struct ath10k_hif_ops ath10k_pci_hif_ops = {
Michal Kazior726346f2014-02-27 18:50:04 +02002140 .tx_sg = ath10k_pci_hif_tx_sg,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002141 .exchange_bmi_msg = ath10k_pci_hif_exchange_bmi_msg,
2142 .start = ath10k_pci_hif_start,
2143 .stop = ath10k_pci_hif_stop,
2144 .map_service_to_pipe = ath10k_pci_hif_map_service_to_pipe,
2145 .get_default_pipe = ath10k_pci_hif_get_default_pipe,
2146 .send_complete_check = ath10k_pci_hif_send_complete_check,
Michal Kaziore799bbf2013-07-05 16:15:12 +03002147 .set_callbacks = ath10k_pci_hif_set_callbacks,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002148 .get_free_queue_number = ath10k_pci_hif_get_free_queue_number,
Michal Kazior8c5c5362013-07-16 09:38:50 +02002149 .power_up = ath10k_pci_hif_power_up,
2150 .power_down = ath10k_pci_hif_power_down,
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002151#ifdef CONFIG_PM
2152 .suspend = ath10k_pci_hif_suspend,
2153 .resume = ath10k_pci_hif_resume,
2154#endif
Kalle Valo5e3dd152013-06-12 20:52:10 +03002155};
2156
2157static void ath10k_pci_ce_tasklet(unsigned long ptr)
2158{
Michal Kazior87263e52013-08-27 13:08:01 +02002159 struct ath10k_pci_pipe *pipe = (struct ath10k_pci_pipe *)ptr;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002160 struct ath10k_pci *ar_pci = pipe->ar_pci;
2161
2162 ath10k_ce_per_engine_service(ar_pci->ar, pipe->pipe_num);
2163}
2164
2165static void ath10k_msi_err_tasklet(unsigned long data)
2166{
2167 struct ath10k *ar = (struct ath10k *)data;
2168
2169 ath10k_pci_fw_interrupt_handler(ar);
2170}
2171
2172/*
2173 * Handler for a per-engine interrupt on a PARTICULAR CE.
2174 * This is used in cases where each CE has a private MSI interrupt.
2175 */
2176static irqreturn_t ath10k_pci_per_engine_handler(int irq, void *arg)
2177{
2178 struct ath10k *ar = arg;
2179 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2180 int ce_id = irq - ar_pci->pdev->irq - MSI_ASSIGN_CE_INITIAL;
2181
Dan Carpentere5742672013-06-18 10:28:46 +03002182 if (ce_id < 0 || ce_id >= ARRAY_SIZE(ar_pci->pipe_info)) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03002183 ath10k_warn("unexpected/invalid irq %d ce_id %d\n", irq, ce_id);
2184 return IRQ_HANDLED;
2185 }
2186
2187 /*
2188 * NOTE: We are able to derive ce_id from irq because we
2189 * use a one-to-one mapping for CE's 0..5.
2190 * CE's 6 & 7 do not use interrupts at all.
2191 *
2192 * This mapping must be kept in sync with the mapping
2193 * used by firmware.
2194 */
2195 tasklet_schedule(&ar_pci->pipe_info[ce_id].intr);
2196 return IRQ_HANDLED;
2197}
2198
2199static irqreturn_t ath10k_pci_msi_fw_handler(int irq, void *arg)
2200{
2201 struct ath10k *ar = arg;
2202 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2203
2204 tasklet_schedule(&ar_pci->msi_fw_err);
2205 return IRQ_HANDLED;
2206}
2207
2208/*
2209 * Top-level interrupt handler for all PCI interrupts from a Target.
2210 * When a block of MSI interrupts is allocated, this top-level handler
2211 * is not used; instead, we directly call the correct sub-handler.
2212 */
2213static irqreturn_t ath10k_pci_interrupt_handler(int irq, void *arg)
2214{
2215 struct ath10k *ar = arg;
2216 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2217
2218 if (ar_pci->num_msi_intrs == 0) {
Michal Kaziore5398872013-11-25 14:06:20 +01002219 if (!ath10k_pci_irq_pending(ar))
2220 return IRQ_NONE;
2221
Michal Kazior26852182013-11-25 14:06:25 +01002222 ath10k_pci_disable_and_clear_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002223 }
2224
2225 tasklet_schedule(&ar_pci->intr_tq);
2226
2227 return IRQ_HANDLED;
2228}
2229
Michal Kaziorab977bd2013-11-25 14:06:26 +01002230static void ath10k_pci_early_irq_tasklet(unsigned long data)
2231{
2232 struct ath10k *ar = (struct ath10k *)data;
Michal Kaziorab977bd2013-11-25 14:06:26 +01002233 u32 fw_ind;
2234 int ret;
2235
2236 ret = ath10k_pci_wake(ar);
2237 if (ret) {
2238 ath10k_warn("failed to wake target in early irq tasklet: %d\n",
2239 ret);
2240 return;
2241 }
2242
Kalle Valob39712c2014-03-28 09:32:46 +02002243 fw_ind = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
Michal Kaziorab977bd2013-11-25 14:06:26 +01002244 if (fw_ind & FW_IND_EVENT_PENDING) {
Kalle Valob39712c2014-03-28 09:32:46 +02002245 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS,
Michal Kaziorab977bd2013-11-25 14:06:26 +01002246 fw_ind & ~FW_IND_EVENT_PENDING);
Michal Kazior1a4ab282014-05-14 16:56:16 +03002247 ath10k_pci_hif_dump_area(ar);
Michal Kaziorab977bd2013-11-25 14:06:26 +01002248 }
2249
2250 ath10k_pci_sleep(ar);
2251 ath10k_pci_enable_legacy_irq(ar);
2252}
2253
Kalle Valo5e3dd152013-06-12 20:52:10 +03002254static void ath10k_pci_tasklet(unsigned long data)
2255{
2256 struct ath10k *ar = (struct ath10k *)data;
2257 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2258
2259 ath10k_pci_fw_interrupt_handler(ar); /* FIXME: Handle FW error */
2260 ath10k_ce_per_engine_service_any(ar);
2261
Michal Kazior26852182013-11-25 14:06:25 +01002262 /* Re-enable legacy irq that was disabled in the irq handler */
2263 if (ar_pci->num_msi_intrs == 0)
2264 ath10k_pci_enable_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002265}
2266
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002267static int ath10k_pci_request_irq_msix(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002268{
2269 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002270 int ret, i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002271
2272 ret = request_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW,
2273 ath10k_pci_msi_fw_handler,
2274 IRQF_SHARED, "ath10k_pci", ar);
Michal Kazior591ecdb2013-07-31 10:55:15 +02002275 if (ret) {
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002276 ath10k_warn("failed to request MSI-X fw irq %d: %d\n",
Michal Kazior591ecdb2013-07-31 10:55:15 +02002277 ar_pci->pdev->irq + MSI_ASSIGN_FW, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002278 return ret;
Michal Kazior591ecdb2013-07-31 10:55:15 +02002279 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002280
2281 for (i = MSI_ASSIGN_CE_INITIAL; i <= MSI_ASSIGN_CE_MAX; i++) {
2282 ret = request_irq(ar_pci->pdev->irq + i,
2283 ath10k_pci_per_engine_handler,
2284 IRQF_SHARED, "ath10k_pci", ar);
2285 if (ret) {
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002286 ath10k_warn("failed to request MSI-X ce irq %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002287 ar_pci->pdev->irq + i, ret);
2288
Michal Kazior87b14232013-06-26 08:50:50 +02002289 for (i--; i >= MSI_ASSIGN_CE_INITIAL; i--)
2290 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002291
Michal Kazior87b14232013-06-26 08:50:50 +02002292 free_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002293 return ret;
2294 }
2295 }
2296
Kalle Valo5e3dd152013-06-12 20:52:10 +03002297 return 0;
2298}
2299
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002300static int ath10k_pci_request_irq_msi(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002301{
2302 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2303 int ret;
2304
2305 ret = request_irq(ar_pci->pdev->irq,
2306 ath10k_pci_interrupt_handler,
2307 IRQF_SHARED, "ath10k_pci", ar);
Kalle Valof3782742013-10-17 11:36:15 +03002308 if (ret) {
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002309 ath10k_warn("failed to request MSI irq %d: %d\n",
2310 ar_pci->pdev->irq, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002311 return ret;
Kalle Valof3782742013-10-17 11:36:15 +03002312 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002313
Kalle Valo5e3dd152013-06-12 20:52:10 +03002314 return 0;
2315}
2316
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002317static int ath10k_pci_request_irq_legacy(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002318{
2319 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002320 int ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002321
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002322 ret = request_irq(ar_pci->pdev->irq,
2323 ath10k_pci_interrupt_handler,
2324 IRQF_SHARED, "ath10k_pci", ar);
Kalle Valof3782742013-10-17 11:36:15 +03002325 if (ret) {
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002326 ath10k_warn("failed to request legacy irq %d: %d\n",
2327 ar_pci->pdev->irq, ret);
Kalle Valof3782742013-10-17 11:36:15 +03002328 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002329 }
2330
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002331 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002332}
2333
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002334static int ath10k_pci_request_irq(struct ath10k *ar)
2335{
2336 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2337
2338 switch (ar_pci->num_msi_intrs) {
2339 case 0:
2340 return ath10k_pci_request_irq_legacy(ar);
2341 case 1:
2342 return ath10k_pci_request_irq_msi(ar);
2343 case MSI_NUM_REQUEST:
2344 return ath10k_pci_request_irq_msix(ar);
2345 }
2346
2347 ath10k_warn("unknown irq configuration upon request\n");
2348 return -EINVAL;
2349}
2350
2351static void ath10k_pci_free_irq(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002352{
2353 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2354 int i;
2355
2356 /* There's at least one interrupt irregardless whether its legacy INTR
2357 * or MSI or MSI-X */
2358 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
2359 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002360}
2361
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002362static void ath10k_pci_init_irq_tasklets(struct ath10k *ar)
2363{
2364 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2365 int i;
2366
2367 tasklet_init(&ar_pci->intr_tq, ath10k_pci_tasklet, (unsigned long)ar);
2368 tasklet_init(&ar_pci->msi_fw_err, ath10k_msi_err_tasklet,
2369 (unsigned long)ar);
Michal Kaziorab977bd2013-11-25 14:06:26 +01002370 tasklet_init(&ar_pci->early_irq_tasklet, ath10k_pci_early_irq_tasklet,
2371 (unsigned long)ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002372
2373 for (i = 0; i < CE_COUNT; i++) {
2374 ar_pci->pipe_info[i].ar_pci = ar_pci;
2375 tasklet_init(&ar_pci->pipe_info[i].intr, ath10k_pci_ce_tasklet,
2376 (unsigned long)&ar_pci->pipe_info[i]);
2377 }
2378}
2379
2380static int ath10k_pci_init_irq(struct ath10k *ar)
2381{
2382 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002383 bool msix_supported = test_bit(ATH10K_PCI_FEATURE_MSI_X,
2384 ar_pci->features);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002385 int ret;
2386
2387 ath10k_pci_init_irq_tasklets(ar);
2388
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002389 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_AUTO &&
2390 !test_bit(ATH10K_FLAG_FIRST_BOOT_DONE, &ar->dev_flags))
2391 ath10k_info("limiting irq mode to: %d\n", ath10k_pci_irq_mode);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002392
2393 /* Try MSI-X */
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002394 if (ath10k_pci_irq_mode == ATH10K_PCI_IRQ_AUTO && msix_supported) {
2395 ar_pci->num_msi_intrs = MSI_NUM_REQUEST;
Alexander Gordeev5ad68672014-02-13 17:50:02 +02002396 ret = pci_enable_msi_range(ar_pci->pdev, ar_pci->num_msi_intrs,
2397 ar_pci->num_msi_intrs);
2398 if (ret > 0)
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002399 return 0;
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002400
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002401 /* fall-through */
2402 }
2403
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002404 /* Try MSI */
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002405 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_LEGACY) {
2406 ar_pci->num_msi_intrs = 1;
2407 ret = pci_enable_msi(ar_pci->pdev);
2408 if (ret == 0)
2409 return 0;
2410
2411 /* fall-through */
2412 }
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002413
2414 /* Try legacy irq
2415 *
2416 * A potential race occurs here: The CORE_BASE write
2417 * depends on target correctly decoding AXI address but
2418 * host won't know when target writes BAR to CORE_CTRL.
2419 * This write might get lost if target has NOT written BAR.
2420 * For now, fix the race by repeating the write in below
2421 * synchronization checking. */
2422 ar_pci->num_msi_intrs = 0;
2423
2424 ret = ath10k_pci_wake(ar);
2425 if (ret) {
2426 ath10k_warn("failed to wake target: %d\n", ret);
2427 return ret;
2428 }
2429
2430 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2431 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
2432 ath10k_pci_sleep(ar);
2433
2434 return 0;
2435}
2436
2437static int ath10k_pci_deinit_irq_legacy(struct ath10k *ar)
2438{
2439 int ret;
2440
2441 ret = ath10k_pci_wake(ar);
2442 if (ret) {
2443 ath10k_warn("failed to wake target: %d\n", ret);
2444 return ret;
2445 }
2446
2447 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2448 0);
2449 ath10k_pci_sleep(ar);
2450
2451 return 0;
2452}
2453
2454static int ath10k_pci_deinit_irq(struct ath10k *ar)
2455{
2456 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2457
2458 switch (ar_pci->num_msi_intrs) {
2459 case 0:
2460 return ath10k_pci_deinit_irq_legacy(ar);
2461 case 1:
2462 /* fall-through */
2463 case MSI_NUM_REQUEST:
2464 pci_disable_msi(ar_pci->pdev);
2465 return 0;
Alexander Gordeevbb8b6212014-02-13 17:50:01 +02002466 default:
2467 pci_disable_msi(ar_pci->pdev);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002468 }
2469
2470 ath10k_warn("unknown irq configuration upon deinit\n");
2471 return -EINVAL;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002472}
2473
Michal Kaziord7fb47f2013-11-08 08:01:26 +01002474static int ath10k_pci_wait_for_target_init(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002475{
2476 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo0399eca2014-03-28 09:32:21 +02002477 unsigned long timeout;
Kalle Valof3782742013-10-17 11:36:15 +03002478 int ret;
Kalle Valo0399eca2014-03-28 09:32:21 +02002479 u32 val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002480
Kalle Valo50f87a62014-03-28 09:32:52 +02002481 ath10k_dbg(ATH10K_DBG_BOOT, "boot waiting target to initialise\n");
2482
Michal Kazior98563d52013-11-08 08:01:33 +01002483 ret = ath10k_pci_wake(ar);
Kalle Valof3782742013-10-17 11:36:15 +03002484 if (ret) {
Kalle Valo0399eca2014-03-28 09:32:21 +02002485 ath10k_err("failed to wake up target for init: %d\n", ret);
Kalle Valof3782742013-10-17 11:36:15 +03002486 return ret;
2487 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002488
Kalle Valo0399eca2014-03-28 09:32:21 +02002489 timeout = jiffies + msecs_to_jiffies(ATH10K_PCI_TARGET_WAIT);
2490
2491 do {
2492 val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
2493
Kalle Valo50f87a62014-03-28 09:32:52 +02002494 ath10k_dbg(ATH10K_DBG_BOOT, "boot target indicator %x\n", val);
2495
Kalle Valo0399eca2014-03-28 09:32:21 +02002496 /* target should never return this */
2497 if (val == 0xffffffff)
2498 continue;
2499
Michal Kazior7710cd22014-04-23 19:30:04 +03002500 /* the device has crashed so don't bother trying anymore */
2501 if (val & FW_IND_EVENT_PENDING)
2502 break;
2503
Kalle Valo0399eca2014-03-28 09:32:21 +02002504 if (val & FW_IND_INITIALIZED)
2505 break;
2506
Kalle Valo5e3dd152013-06-12 20:52:10 +03002507 if (ar_pci->num_msi_intrs == 0)
2508 /* Fix potential race by repeating CORE_BASE writes */
Kalle Valo0399eca2014-03-28 09:32:21 +02002509 ath10k_pci_soc_write32(ar, PCIE_INTR_ENABLE_ADDRESS,
2510 PCIE_INTR_FIRMWARE_MASK |
2511 PCIE_INTR_CE_MASK_ALL);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002512
Kalle Valo0399eca2014-03-28 09:32:21 +02002513 mdelay(10);
2514 } while (time_before(jiffies, timeout));
2515
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002516 if (val == 0xffffffff) {
2517 ath10k_err("failed to read device register, device is gone\n");
2518 ret = -EIO;
2519 goto out;
2520 }
2521
Michal Kazior7710cd22014-04-23 19:30:04 +03002522 if (val & FW_IND_EVENT_PENDING) {
2523 ath10k_warn("device has crashed during init\n");
Michal Kazior1a4ab282014-05-14 16:56:16 +03002524 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS,
2525 val & ~FW_IND_EVENT_PENDING);
2526 ath10k_pci_hif_dump_area(ar);
Michal Kazior7710cd22014-04-23 19:30:04 +03002527 ret = -ECOMM;
2528 goto out;
2529 }
2530
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002531 if (!(val & FW_IND_INITIALIZED)) {
Kalle Valo0399eca2014-03-28 09:32:21 +02002532 ath10k_err("failed to receive initialized event from target: %08x\n",
2533 val);
2534 ret = -ETIMEDOUT;
Michal Kazior5b2589f2013-11-08 08:01:30 +01002535 goto out;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002536 }
2537
Kalle Valo50f87a62014-03-28 09:32:52 +02002538 ath10k_dbg(ATH10K_DBG_BOOT, "boot target initialised\n");
2539
Michal Kazior5b2589f2013-11-08 08:01:30 +01002540out:
Michal Kazior98563d52013-11-08 08:01:33 +01002541 ath10k_pci_sleep(ar);
Michal Kazior5b2589f2013-11-08 08:01:30 +01002542 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002543}
2544
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002545static int ath10k_pci_cold_reset(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002546{
Michal Kazior5b2589f2013-11-08 08:01:30 +01002547 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002548 u32 val;
2549
Kalle Valo50f87a62014-03-28 09:32:52 +02002550 ath10k_dbg(ATH10K_DBG_BOOT, "boot cold reset\n");
2551
Michal Kazior5b2589f2013-11-08 08:01:30 +01002552 ret = ath10k_do_pci_wake(ar);
2553 if (ret) {
2554 ath10k_err("failed to wake up target: %d\n",
2555 ret);
2556 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002557 }
2558
2559 /* Put Target, including PCIe, into RESET. */
Kalle Valoe479ed42013-09-01 10:01:53 +03002560 val = ath10k_pci_reg_read32(ar, SOC_GLOBAL_RESET_ADDRESS);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002561 val |= 1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002562 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002563
2564 for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
Kalle Valoe479ed42013-09-01 10:01:53 +03002565 if (ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03002566 RTC_STATE_COLD_RESET_MASK)
2567 break;
2568 msleep(1);
2569 }
2570
2571 /* Pull Target, including PCIe, out of RESET. */
2572 val &= ~1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002573 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002574
2575 for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
Kalle Valoe479ed42013-09-01 10:01:53 +03002576 if (!(ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03002577 RTC_STATE_COLD_RESET_MASK))
2578 break;
2579 msleep(1);
2580 }
2581
Michal Kazior5b2589f2013-11-08 08:01:30 +01002582 ath10k_do_pci_sleep(ar);
Kalle Valo50f87a62014-03-28 09:32:52 +02002583
2584 ath10k_dbg(ATH10K_DBG_BOOT, "boot cold reset complete\n");
2585
Michal Kazior5b2589f2013-11-08 08:01:30 +01002586 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002587}
2588
2589static void ath10k_pci_dump_features(struct ath10k_pci *ar_pci)
2590{
2591 int i;
2592
2593 for (i = 0; i < ATH10K_PCI_FEATURE_COUNT; i++) {
2594 if (!test_bit(i, ar_pci->features))
2595 continue;
2596
2597 switch (i) {
2598 case ATH10K_PCI_FEATURE_MSI_X:
Kalle Valo24cfade2013-09-08 17:55:50 +03002599 ath10k_dbg(ATH10K_DBG_BOOT, "device supports MSI-X\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002600 break;
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002601 case ATH10K_PCI_FEATURE_SOC_POWER_SAVE:
Kalle Valo24cfade2013-09-08 17:55:50 +03002602 ath10k_dbg(ATH10K_DBG_BOOT, "QCA98XX SoC power save enabled\n");
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002603 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002604 }
2605 }
2606}
2607
2608static int ath10k_pci_probe(struct pci_dev *pdev,
2609 const struct pci_device_id *pci_dev)
2610{
2611 void __iomem *mem;
2612 int ret = 0;
2613 struct ath10k *ar;
2614 struct ath10k_pci *ar_pci;
Kalle Valoe01ae682013-09-01 11:22:14 +03002615 u32 lcr_val, chip_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002616
Kalle Valo50f87a62014-03-28 09:32:52 +02002617 ath10k_dbg(ATH10K_DBG_PCI, "pci probe\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002618
2619 ar_pci = kzalloc(sizeof(*ar_pci), GFP_KERNEL);
2620 if (ar_pci == NULL)
2621 return -ENOMEM;
2622
2623 ar_pci->pdev = pdev;
2624 ar_pci->dev = &pdev->dev;
2625
2626 switch (pci_dev->device) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03002627 case QCA988X_2_0_DEVICE_ID:
2628 set_bit(ATH10K_PCI_FEATURE_MSI_X, ar_pci->features);
2629 break;
2630 default:
2631 ret = -ENODEV;
Masanari Iida6d3be302013-09-30 23:19:09 +09002632 ath10k_err("Unknown device ID: %d\n", pci_dev->device);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002633 goto err_ar_pci;
2634 }
2635
Kalle Valoe42c1fb2014-03-28 09:32:33 +02002636 if (ath10k_pci_target_ps)
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002637 set_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE, ar_pci->features);
2638
Kalle Valo5e3dd152013-06-12 20:52:10 +03002639 ath10k_pci_dump_features(ar_pci);
2640
Michal Kazior3a0861f2013-07-05 16:15:06 +03002641 ar = ath10k_core_create(ar_pci, ar_pci->dev, &ath10k_pci_hif_ops);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002642 if (!ar) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002643 ath10k_err("failed to create driver core\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002644 ret = -EINVAL;
2645 goto err_ar_pci;
2646 }
2647
Kalle Valo5e3dd152013-06-12 20:52:10 +03002648 ar_pci->ar = ar;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002649 atomic_set(&ar_pci->keep_awake_count, 0);
2650
2651 pci_set_drvdata(pdev, ar);
2652
Kalle Valo5e3dd152013-06-12 20:52:10 +03002653 ret = pci_enable_device(pdev);
2654 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002655 ath10k_err("failed to enable PCI device: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002656 goto err_ar;
2657 }
2658
2659 /* Request MMIO resources */
2660 ret = pci_request_region(pdev, BAR_NUM, "ath");
2661 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002662 ath10k_err("failed to request MMIO region: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002663 goto err_device;
2664 }
2665
2666 /*
2667 * Target structures have a limit of 32 bit DMA pointers.
2668 * DMA pointers can be wider than 32 bits by default on some systems.
2669 */
2670 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2671 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002672 ath10k_err("failed to set DMA mask to 32-bit: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002673 goto err_region;
2674 }
2675
2676 ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2677 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002678 ath10k_err("failed to set consistent DMA mask to 32-bit\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002679 goto err_region;
2680 }
2681
2682 /* Set bus master bit in PCI_COMMAND to enable DMA */
2683 pci_set_master(pdev);
2684
2685 /*
2686 * Temporary FIX: disable ASPM
2687 * Will be removed after the OTP is programmed
2688 */
2689 pci_read_config_dword(pdev, 0x80, &lcr_val);
2690 pci_write_config_dword(pdev, 0x80, (lcr_val & 0xffffff00));
2691
2692 /* Arrange for access to Target SoC registers. */
2693 mem = pci_iomap(pdev, BAR_NUM, 0);
2694 if (!mem) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002695 ath10k_err("failed to perform IOMAP for BAR%d\n", BAR_NUM);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002696 ret = -EIO;
2697 goto err_master;
2698 }
2699
2700 ar_pci->mem = mem;
2701
2702 spin_lock_init(&ar_pci->ce_lock);
2703
Kalle Valoe01ae682013-09-01 11:22:14 +03002704 ret = ath10k_do_pci_wake(ar);
2705 if (ret) {
2706 ath10k_err("Failed to get chip id: %d\n", ret);
Wei Yongjun12eb0872013-10-30 13:24:39 +08002707 goto err_iomap;
Kalle Valoe01ae682013-09-01 11:22:14 +03002708 }
2709
Kalle Valo233eb972013-10-16 16:46:11 +03002710 chip_id = ath10k_pci_soc_read32(ar, SOC_CHIP_ID_ADDRESS);
Kalle Valoe01ae682013-09-01 11:22:14 +03002711
2712 ath10k_do_pci_sleep(ar);
2713
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002714 ret = ath10k_pci_alloc_ce(ar);
2715 if (ret) {
2716 ath10k_err("failed to allocate copy engine pipes: %d\n", ret);
2717 goto err_iomap;
2718 }
2719
Kalle Valo24cfade2013-09-08 17:55:50 +03002720 ath10k_dbg(ATH10K_DBG_BOOT, "boot pci_mem 0x%p\n", ar_pci->mem);
2721
Kalle Valoe01ae682013-09-01 11:22:14 +03002722 ret = ath10k_core_register(ar, chip_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002723 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002724 ath10k_err("failed to register driver core: %d\n", ret);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002725 goto err_free_ce;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002726 }
2727
2728 return 0;
2729
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002730err_free_ce:
2731 ath10k_pci_free_ce(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002732err_iomap:
2733 pci_iounmap(pdev, mem);
2734err_master:
2735 pci_clear_master(pdev);
2736err_region:
2737 pci_release_region(pdev, BAR_NUM);
2738err_device:
2739 pci_disable_device(pdev);
2740err_ar:
Kalle Valo5e3dd152013-06-12 20:52:10 +03002741 ath10k_core_destroy(ar);
2742err_ar_pci:
2743 /* call HIF PCI free here */
2744 kfree(ar_pci);
2745
2746 return ret;
2747}
2748
2749static void ath10k_pci_remove(struct pci_dev *pdev)
2750{
2751 struct ath10k *ar = pci_get_drvdata(pdev);
2752 struct ath10k_pci *ar_pci;
2753
Kalle Valo50f87a62014-03-28 09:32:52 +02002754 ath10k_dbg(ATH10K_DBG_PCI, "pci remove\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002755
2756 if (!ar)
2757 return;
2758
2759 ar_pci = ath10k_pci_priv(ar);
2760
2761 if (!ar_pci)
2762 return;
2763
Kalle Valo5e3dd152013-06-12 20:52:10 +03002764 ath10k_core_unregister(ar);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002765 ath10k_pci_free_ce(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002766
Kalle Valo5e3dd152013-06-12 20:52:10 +03002767 pci_iounmap(pdev, ar_pci->mem);
2768 pci_release_region(pdev, BAR_NUM);
2769 pci_clear_master(pdev);
2770 pci_disable_device(pdev);
2771
2772 ath10k_core_destroy(ar);
2773 kfree(ar_pci);
2774}
2775
Kalle Valo5e3dd152013-06-12 20:52:10 +03002776MODULE_DEVICE_TABLE(pci, ath10k_pci_id_table);
2777
2778static struct pci_driver ath10k_pci_driver = {
2779 .name = "ath10k_pci",
2780 .id_table = ath10k_pci_id_table,
2781 .probe = ath10k_pci_probe,
2782 .remove = ath10k_pci_remove,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002783};
2784
2785static int __init ath10k_pci_init(void)
2786{
2787 int ret;
2788
2789 ret = pci_register_driver(&ath10k_pci_driver);
2790 if (ret)
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002791 ath10k_err("failed to register PCI driver: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002792
2793 return ret;
2794}
2795module_init(ath10k_pci_init);
2796
2797static void __exit ath10k_pci_exit(void)
2798{
2799 pci_unregister_driver(&ath10k_pci_driver);
2800}
2801
2802module_exit(ath10k_pci_exit);
2803
2804MODULE_AUTHOR("Qualcomm Atheros");
2805MODULE_DESCRIPTION("Driver support for Atheros QCA988X PCIe devices");
2806MODULE_LICENSE("Dual BSD/GPL");
Kalle Valo929417c2014-03-28 09:32:39 +02002807MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_FW_2_FILE);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002808MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_BOARD_DATA_FILE);