blob: 6224952ba523574845e222dee977325fe6bf0afa [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
Michal Kaziorcfe9c452013-11-25 14:06:27 +010047static unsigned int ath10k_pci_irq_mode = ATH10K_PCI_IRQ_AUTO;
Kalle Valo35098462014-03-28 09:32:27 +020048static unsigned int ath10k_pci_reset_mode = ATH10K_PCI_RESET_AUTO;
Michal Kaziorcfe9c452013-11-25 14:06:27 +010049
Michal Kaziorcfe9c452013-11-25 14:06:27 +010050module_param_named(irq_mode, ath10k_pci_irq_mode, uint, 0644);
51MODULE_PARM_DESC(irq_mode, "0: auto, 1: legacy, 2: msi (default: 0)");
52
Kalle Valo35098462014-03-28 09:32:27 +020053module_param_named(reset_mode, ath10k_pci_reset_mode, uint, 0644);
54MODULE_PARM_DESC(reset_mode, "0: auto, 1: warm only (default: 0)");
55
Kalle Valo0399eca2014-03-28 09:32:21 +020056/* how long wait to wait for target to initialise, in ms */
57#define ATH10K_PCI_TARGET_WAIT 3000
Michal Kazior61c95ce2014-05-14 16:56:16 +030058#define ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS 3
Kalle Valo0399eca2014-03-28 09:32:21 +020059
Kalle Valo5e3dd152013-06-12 20:52:10 +030060#define QCA988X_2_0_DEVICE_ID (0x003c)
61
62static DEFINE_PCI_DEVICE_TABLE(ath10k_pci_id_table) = {
Kalle Valo5e3dd152013-06-12 20:52:10 +030063 { PCI_VDEVICE(ATHEROS, QCA988X_2_0_DEVICE_ID) }, /* PCI-E QCA988X V2 */
64 {0}
65};
66
67static int ath10k_pci_diag_read_access(struct ath10k *ar, u32 address,
68 u32 *data);
69
Kalle Valo5e3dd152013-06-12 20:52:10 +030070static int ath10k_pci_post_rx(struct ath10k *ar);
Michal Kazior87263e52013-08-27 13:08:01 +020071static int ath10k_pci_post_rx_pipe(struct ath10k_pci_pipe *pipe_info,
Kalle Valo5e3dd152013-06-12 20:52:10 +030072 int num);
Michal Kazior87263e52013-08-27 13:08:01 +020073static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe *pipe_info);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +010074static int ath10k_pci_cold_reset(struct ath10k *ar);
75static int ath10k_pci_warm_reset(struct ath10k *ar);
Michal Kaziord7fb47f2013-11-08 08:01:26 +010076static int ath10k_pci_wait_for_target_init(struct ath10k *ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +010077static int ath10k_pci_init_irq(struct ath10k *ar);
78static int ath10k_pci_deinit_irq(struct ath10k *ar);
79static int ath10k_pci_request_irq(struct ath10k *ar);
80static void ath10k_pci_free_irq(struct ath10k *ar);
Michal Kazior85622cd2013-11-25 14:06:22 +010081static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
82 struct ath10k_ce_pipe *rx_pipe,
83 struct bmi_xfer *xfer);
Kalle Valo5e3dd152013-06-12 20:52:10 +030084
85static const struct ce_attr host_ce_config_wlan[] = {
Kalle Valo48e9c222013-09-01 10:01:32 +030086 /* CE0: host->target HTC control and raw streams */
87 {
88 .flags = CE_ATTR_FLAGS,
89 .src_nentries = 16,
90 .src_sz_max = 256,
91 .dest_nentries = 0,
92 },
93
94 /* CE1: target->host HTT + HTC control */
95 {
96 .flags = CE_ATTR_FLAGS,
97 .src_nentries = 0,
98 .src_sz_max = 512,
99 .dest_nentries = 512,
100 },
101
102 /* CE2: target->host WMI */
103 {
104 .flags = CE_ATTR_FLAGS,
105 .src_nentries = 0,
106 .src_sz_max = 2048,
107 .dest_nentries = 32,
108 },
109
110 /* CE3: host->target WMI */
111 {
112 .flags = CE_ATTR_FLAGS,
113 .src_nentries = 32,
114 .src_sz_max = 2048,
115 .dest_nentries = 0,
116 },
117
118 /* CE4: host->target HTT */
119 {
120 .flags = CE_ATTR_FLAGS | CE_ATTR_DIS_INTR,
121 .src_nentries = CE_HTT_H2T_MSG_SRC_NENTRIES,
122 .src_sz_max = 256,
123 .dest_nentries = 0,
124 },
125
126 /* CE5: unused */
127 {
128 .flags = CE_ATTR_FLAGS,
129 .src_nentries = 0,
130 .src_sz_max = 0,
131 .dest_nentries = 0,
132 },
133
134 /* CE6: target autonomous hif_memcpy */
135 {
136 .flags = CE_ATTR_FLAGS,
137 .src_nentries = 0,
138 .src_sz_max = 0,
139 .dest_nentries = 0,
140 },
141
142 /* CE7: ce_diag, the Diagnostic Window */
143 {
144 .flags = CE_ATTR_FLAGS,
145 .src_nentries = 2,
146 .src_sz_max = DIAG_TRANSFER_LIMIT,
147 .dest_nentries = 2,
148 },
Kalle Valo5e3dd152013-06-12 20:52:10 +0300149};
150
151/* Target firmware's Copy Engine configuration. */
152static const struct ce_pipe_config target_ce_config_wlan[] = {
Kalle Valod88effb2013-09-01 10:01:39 +0300153 /* CE0: host->target HTC control and raw streams */
154 {
155 .pipenum = 0,
156 .pipedir = PIPEDIR_OUT,
157 .nentries = 32,
158 .nbytes_max = 256,
159 .flags = CE_ATTR_FLAGS,
160 .reserved = 0,
161 },
162
163 /* CE1: target->host HTT + HTC control */
164 {
165 .pipenum = 1,
166 .pipedir = PIPEDIR_IN,
167 .nentries = 32,
168 .nbytes_max = 512,
169 .flags = CE_ATTR_FLAGS,
170 .reserved = 0,
171 },
172
173 /* CE2: target->host WMI */
174 {
175 .pipenum = 2,
176 .pipedir = PIPEDIR_IN,
177 .nentries = 32,
178 .nbytes_max = 2048,
179 .flags = CE_ATTR_FLAGS,
180 .reserved = 0,
181 },
182
183 /* CE3: host->target WMI */
184 {
185 .pipenum = 3,
186 .pipedir = PIPEDIR_OUT,
187 .nentries = 32,
188 .nbytes_max = 2048,
189 .flags = CE_ATTR_FLAGS,
190 .reserved = 0,
191 },
192
193 /* CE4: host->target HTT */
194 {
195 .pipenum = 4,
196 .pipedir = PIPEDIR_OUT,
197 .nentries = 256,
198 .nbytes_max = 256,
199 .flags = CE_ATTR_FLAGS,
200 .reserved = 0,
201 },
202
Kalle Valo5e3dd152013-06-12 20:52:10 +0300203 /* NB: 50% of src nentries, since tx has 2 frags */
Kalle Valod88effb2013-09-01 10:01:39 +0300204
205 /* CE5: unused */
206 {
207 .pipenum = 5,
208 .pipedir = PIPEDIR_OUT,
209 .nentries = 32,
210 .nbytes_max = 2048,
211 .flags = CE_ATTR_FLAGS,
212 .reserved = 0,
213 },
214
215 /* CE6: Reserved for target autonomous hif_memcpy */
216 {
217 .pipenum = 6,
218 .pipedir = PIPEDIR_INOUT,
219 .nentries = 32,
220 .nbytes_max = 4096,
221 .flags = CE_ATTR_FLAGS,
222 .reserved = 0,
223 },
224
Kalle Valo5e3dd152013-06-12 20:52:10 +0300225 /* CE7 used only by Host */
226};
227
Michal Kaziore5398872013-11-25 14:06:20 +0100228static bool ath10k_pci_irq_pending(struct ath10k *ar)
229{
230 u32 cause;
231
232 /* Check if the shared legacy irq is for us */
233 cause = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
234 PCIE_INTR_CAUSE_ADDRESS);
235 if (cause & (PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL))
236 return true;
237
238 return false;
239}
240
Michal Kazior26852182013-11-25 14:06:25 +0100241static void ath10k_pci_disable_and_clear_legacy_irq(struct ath10k *ar)
242{
243 /* IMPORTANT: INTR_CLR register has to be set after
244 * INTR_ENABLE is set to 0, otherwise interrupt can not be
245 * really cleared. */
246 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
247 0);
248 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_CLR_ADDRESS,
249 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
250
251 /* IMPORTANT: this extra read transaction is required to
252 * flush the posted write buffer. */
253 (void) ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
254 PCIE_INTR_ENABLE_ADDRESS);
255}
256
257static void ath10k_pci_enable_legacy_irq(struct ath10k *ar)
258{
259 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
260 PCIE_INTR_ENABLE_ADDRESS,
261 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
262
263 /* IMPORTANT: this extra read transaction is required to
264 * flush the posted write buffer. */
265 (void) ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
266 PCIE_INTR_ENABLE_ADDRESS);
267}
268
Michal Kaziorab977bd2013-11-25 14:06:26 +0100269static irqreturn_t ath10k_pci_early_irq_handler(int irq, void *arg)
270{
271 struct ath10k *ar = arg;
272 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
273
274 if (ar_pci->num_msi_intrs == 0) {
275 if (!ath10k_pci_irq_pending(ar))
276 return IRQ_NONE;
277
278 ath10k_pci_disable_and_clear_legacy_irq(ar);
279 }
280
281 tasklet_schedule(&ar_pci->early_irq_tasklet);
282
283 return IRQ_HANDLED;
284}
285
286static int ath10k_pci_request_early_irq(struct ath10k *ar)
287{
288 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
289 int ret;
290
291 /* Regardless whether MSI-X/MSI/legacy irqs have been set up the first
292 * interrupt from irq vector is triggered in all cases for FW
293 * indication/errors */
294 ret = request_irq(ar_pci->pdev->irq, ath10k_pci_early_irq_handler,
295 IRQF_SHARED, "ath10k_pci (early)", ar);
296 if (ret) {
297 ath10k_warn("failed to request early irq: %d\n", ret);
298 return ret;
299 }
300
301 return 0;
302}
303
304static void ath10k_pci_free_early_irq(struct ath10k *ar)
305{
306 free_irq(ath10k_pci_priv(ar)->pdev->irq, ar);
307}
308
Michal Kazior403d6272014-08-22 14:23:31 +0200309static inline const char *ath10k_pci_get_irq_method(struct ath10k *ar)
310{
311 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
312
313 if (ar_pci->num_msi_intrs > 1)
314 return "msi-x";
315 else if (ar_pci->num_msi_intrs == 1)
316 return "msi";
317 else
318 return "legacy";
319}
320
Kalle Valo5e3dd152013-06-12 20:52:10 +0300321/*
322 * Diagnostic read/write access is provided for startup/config/debug usage.
323 * Caller must guarantee proper alignment, when applicable, and single user
324 * at any moment.
325 */
326static int ath10k_pci_diag_read_mem(struct ath10k *ar, u32 address, void *data,
327 int nbytes)
328{
329 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
330 int ret = 0;
331 u32 buf;
332 unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
333 unsigned int id;
334 unsigned int flags;
Michal Kazior2aa39112013-08-27 13:08:02 +0200335 struct ath10k_ce_pipe *ce_diag;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300336 /* Host buffer address in CE space */
337 u32 ce_data;
338 dma_addr_t ce_data_base = 0;
339 void *data_buf = NULL;
340 int i;
341
342 /*
343 * This code cannot handle reads to non-memory space. Redirect to the
344 * register read fn but preserve the multi word read capability of
345 * this fn
346 */
347 if (address < DRAM_BASE_ADDRESS) {
348 if (!IS_ALIGNED(address, 4) ||
349 !IS_ALIGNED((unsigned long)data, 4))
350 return -EIO;
351
352 while ((nbytes >= 4) && ((ret = ath10k_pci_diag_read_access(
353 ar, address, (u32 *)data)) == 0)) {
354 nbytes -= sizeof(u32);
355 address += sizeof(u32);
356 data += sizeof(u32);
357 }
358 return ret;
359 }
360
361 ce_diag = ar_pci->ce_diag;
362
363 /*
364 * Allocate a temporary bounce buffer to hold caller's data
365 * to be DMA'ed from Target. This guarantees
366 * 1) 4-byte alignment
367 * 2) Buffer in DMA-able space
368 */
369 orig_nbytes = nbytes;
Michal Kazior68c03242014-03-28 10:02:35 +0200370 data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
371 orig_nbytes,
372 &ce_data_base,
373 GFP_ATOMIC);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300374
375 if (!data_buf) {
376 ret = -ENOMEM;
377 goto done;
378 }
379 memset(data_buf, 0, orig_nbytes);
380
381 remaining_bytes = orig_nbytes;
382 ce_data = ce_data_base;
383 while (remaining_bytes) {
384 nbytes = min_t(unsigned int, remaining_bytes,
385 DIAG_TRANSFER_LIMIT);
386
387 ret = ath10k_ce_recv_buf_enqueue(ce_diag, NULL, ce_data);
388 if (ret != 0)
389 goto done;
390
391 /* Request CE to send from Target(!) address to Host buffer */
392 /*
393 * The address supplied by the caller is in the
394 * Target CPU virtual address space.
395 *
396 * In order to use this address with the diagnostic CE,
397 * convert it from Target CPU virtual address space
398 * to CE address space
399 */
Kalle Valo5e3dd152013-06-12 20:52:10 +0300400 address = TARG_CPU_SPACE_TO_CE_SPACE(ar, ar_pci->mem,
401 address);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300402
403 ret = ath10k_ce_send(ce_diag, NULL, (u32)address, nbytes, 0,
404 0);
405 if (ret)
406 goto done;
407
408 i = 0;
409 while (ath10k_ce_completed_send_next(ce_diag, NULL, &buf,
410 &completed_nbytes,
411 &id) != 0) {
412 mdelay(1);
413 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
414 ret = -EBUSY;
415 goto done;
416 }
417 }
418
419 if (nbytes != completed_nbytes) {
420 ret = -EIO;
421 goto done;
422 }
423
424 if (buf != (u32) address) {
425 ret = -EIO;
426 goto done;
427 }
428
429 i = 0;
430 while (ath10k_ce_completed_recv_next(ce_diag, NULL, &buf,
431 &completed_nbytes,
432 &id, &flags) != 0) {
433 mdelay(1);
434
435 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
436 ret = -EBUSY;
437 goto done;
438 }
439 }
440
441 if (nbytes != completed_nbytes) {
442 ret = -EIO;
443 goto done;
444 }
445
446 if (buf != ce_data) {
447 ret = -EIO;
448 goto done;
449 }
450
451 remaining_bytes -= nbytes;
452 address += nbytes;
453 ce_data += nbytes;
454 }
455
456done:
457 if (ret == 0) {
458 /* Copy data from allocated DMA buf to caller's buf */
459 WARN_ON_ONCE(orig_nbytes & 3);
460 for (i = 0; i < orig_nbytes / sizeof(__le32); i++) {
461 ((u32 *)data)[i] =
462 __le32_to_cpu(((__le32 *)data_buf)[i]);
463 }
464 } else
Kalle Valo50f87a62014-03-28 09:32:52 +0200465 ath10k_warn("failed to read diag value at 0x%x: %d\n",
466 address, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300467
468 if (data_buf)
Michal Kazior68c03242014-03-28 10:02:35 +0200469 dma_free_coherent(ar->dev, orig_nbytes, data_buf,
470 ce_data_base);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300471
472 return ret;
473}
474
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300475static int ath10k_pci_diag_read32(struct ath10k *ar, u32 address, u32 *value)
476{
477 return ath10k_pci_diag_read_mem(ar, address, value, sizeof(u32));
478}
479
480static int __ath10k_pci_diag_read_hi(struct ath10k *ar, void *dest,
481 u32 src, u32 len)
482{
483 u32 host_addr, addr;
484 int ret;
485
486 host_addr = host_interest_item_address(src);
487
488 ret = ath10k_pci_diag_read32(ar, host_addr, &addr);
489 if (ret != 0) {
490 ath10k_warn("failed to get memcpy hi address for firmware address %d: %d\n",
491 src, ret);
492 return ret;
493 }
494
495 ret = ath10k_pci_diag_read_mem(ar, addr, dest, len);
496 if (ret != 0) {
497 ath10k_warn("failed to memcpy firmware memory from %d (%d B): %d\n",
498 addr, len, ret);
499 return ret;
500 }
501
502 return 0;
503}
504
505#define ath10k_pci_diag_read_hi(ar, dest, src, len) \
506 __ath10k_pci_diag_read_hi(ar, dest, HI_ITEM(src), len);
507
Kalle Valo5e3dd152013-06-12 20:52:10 +0300508/* Read 4-byte aligned data from Target memory or register */
509static int ath10k_pci_diag_read_access(struct ath10k *ar, u32 address,
510 u32 *data)
511{
512 /* Assume range doesn't cross this boundary */
513 if (address >= DRAM_BASE_ADDRESS)
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300514 return ath10k_pci_diag_read32(ar, address, data);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300515
Kalle Valo5e3dd152013-06-12 20:52:10 +0300516 *data = ath10k_pci_read32(ar, address);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300517 return 0;
518}
519
520static int ath10k_pci_diag_write_mem(struct ath10k *ar, u32 address,
521 const void *data, int nbytes)
522{
523 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
524 int ret = 0;
525 u32 buf;
526 unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
527 unsigned int id;
528 unsigned int flags;
Michal Kazior2aa39112013-08-27 13:08:02 +0200529 struct ath10k_ce_pipe *ce_diag;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300530 void *data_buf = NULL;
531 u32 ce_data; /* Host buffer address in CE space */
532 dma_addr_t ce_data_base = 0;
533 int i;
534
535 ce_diag = ar_pci->ce_diag;
536
537 /*
538 * Allocate a temporary bounce buffer to hold caller's data
539 * to be DMA'ed to Target. This guarantees
540 * 1) 4-byte alignment
541 * 2) Buffer in DMA-able space
542 */
543 orig_nbytes = nbytes;
Michal Kazior68c03242014-03-28 10:02:35 +0200544 data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
545 orig_nbytes,
546 &ce_data_base,
547 GFP_ATOMIC);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300548 if (!data_buf) {
549 ret = -ENOMEM;
550 goto done;
551 }
552
553 /* Copy caller's data to allocated DMA buf */
554 WARN_ON_ONCE(orig_nbytes & 3);
555 for (i = 0; i < orig_nbytes / sizeof(__le32); i++)
556 ((__le32 *)data_buf)[i] = __cpu_to_le32(((u32 *)data)[i]);
557
558 /*
559 * The address supplied by the caller is in the
560 * Target CPU virtual address space.
561 *
562 * In order to use this address with the diagnostic CE,
563 * convert it from
564 * Target CPU virtual address space
565 * to
566 * CE address space
567 */
Kalle Valo5e3dd152013-06-12 20:52:10 +0300568 address = TARG_CPU_SPACE_TO_CE_SPACE(ar, ar_pci->mem, address);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300569
570 remaining_bytes = orig_nbytes;
571 ce_data = ce_data_base;
572 while (remaining_bytes) {
573 /* FIXME: check cast */
574 nbytes = min_t(int, remaining_bytes, DIAG_TRANSFER_LIMIT);
575
576 /* Set up to receive directly into Target(!) address */
577 ret = ath10k_ce_recv_buf_enqueue(ce_diag, NULL, address);
578 if (ret != 0)
579 goto done;
580
581 /*
582 * Request CE to send caller-supplied data that
583 * was copied to bounce buffer to Target(!) address.
584 */
585 ret = ath10k_ce_send(ce_diag, NULL, (u32) ce_data,
586 nbytes, 0, 0);
587 if (ret != 0)
588 goto done;
589
590 i = 0;
591 while (ath10k_ce_completed_send_next(ce_diag, NULL, &buf,
592 &completed_nbytes,
593 &id) != 0) {
594 mdelay(1);
595
596 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
597 ret = -EBUSY;
598 goto done;
599 }
600 }
601
602 if (nbytes != completed_nbytes) {
603 ret = -EIO;
604 goto done;
605 }
606
607 if (buf != ce_data) {
608 ret = -EIO;
609 goto done;
610 }
611
612 i = 0;
613 while (ath10k_ce_completed_recv_next(ce_diag, NULL, &buf,
614 &completed_nbytes,
615 &id, &flags) != 0) {
616 mdelay(1);
617
618 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
619 ret = -EBUSY;
620 goto done;
621 }
622 }
623
624 if (nbytes != completed_nbytes) {
625 ret = -EIO;
626 goto done;
627 }
628
629 if (buf != address) {
630 ret = -EIO;
631 goto done;
632 }
633
634 remaining_bytes -= nbytes;
635 address += nbytes;
636 ce_data += nbytes;
637 }
638
639done:
640 if (data_buf) {
Michal Kazior68c03242014-03-28 10:02:35 +0200641 dma_free_coherent(ar->dev, orig_nbytes, data_buf,
642 ce_data_base);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300643 }
644
645 if (ret != 0)
Kalle Valo50f87a62014-03-28 09:32:52 +0200646 ath10k_warn("failed to write diag value at 0x%x: %d\n",
647 address, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300648
649 return ret;
650}
651
652/* Write 4B data to Target memory or register */
653static int ath10k_pci_diag_write_access(struct ath10k *ar, u32 address,
654 u32 data)
655{
656 /* Assume range doesn't cross this boundary */
657 if (address >= DRAM_BASE_ADDRESS)
658 return ath10k_pci_diag_write_mem(ar, address, &data,
659 sizeof(u32));
660
Kalle Valo5e3dd152013-06-12 20:52:10 +0300661 ath10k_pci_write32(ar, address, data);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300662 return 0;
663}
664
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200665static bool ath10k_pci_is_awake(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300666{
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200667 u32 val = ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS);
668
669 return RTC_STATE_V_GET(val) == RTC_STATE_V_ON;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300670}
671
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200672static int ath10k_pci_wake_wait(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300673{
Kalle Valo5e3dd152013-06-12 20:52:10 +0300674 int tot_delay = 0;
675 int curr_delay = 5;
676
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200677 while (tot_delay < PCIE_WAKE_TIMEOUT) {
678 if (ath10k_pci_is_awake(ar))
Kalle Valo3aebe542013-09-01 10:02:07 +0300679 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300680
681 udelay(curr_delay);
682 tot_delay += curr_delay;
683
684 if (curr_delay < 50)
685 curr_delay += 5;
686 }
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200687
688 return -ETIMEDOUT;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300689}
690
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200691static int ath10k_pci_wake(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300692{
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200693 ath10k_pci_reg_write32(ar, PCIE_SOC_WAKE_ADDRESS,
694 PCIE_SOC_WAKE_V_MASK);
695 return ath10k_pci_wake_wait(ar);
696}
Kalle Valo5e3dd152013-06-12 20:52:10 +0300697
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200698static void ath10k_pci_sleep(struct ath10k *ar)
699{
700 ath10k_pci_reg_write32(ar, PCIE_SOC_WAKE_ADDRESS,
701 PCIE_SOC_WAKE_RESET);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300702}
703
Kalle Valo5e3dd152013-06-12 20:52:10 +0300704/* Called by lower (CE) layer when a send to Target completes. */
Michal Kazior5440ce22013-09-03 15:09:58 +0200705static void ath10k_pci_ce_send_done(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300706{
707 struct ath10k *ar = ce_state->ar;
708 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2f5280d2014-02-27 18:50:05 +0200709 struct ath10k_hif_cb *cb = &ar_pci->msg_callbacks_current;
Michal Kazior5440ce22013-09-03 15:09:58 +0200710 void *transfer_context;
711 u32 ce_data;
712 unsigned int nbytes;
713 unsigned int transfer_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300714
Michal Kazior5440ce22013-09-03 15:09:58 +0200715 while (ath10k_ce_completed_send_next(ce_state, &transfer_context,
716 &ce_data, &nbytes,
717 &transfer_id) == 0) {
Michal Kaziora16942e2014-02-27 18:50:04 +0200718 /* no need to call tx completion for NULL pointers */
Michal Kazior726346f2014-02-27 18:50:04 +0200719 if (transfer_context == NULL)
720 continue;
721
Michal Kazior2f5280d2014-02-27 18:50:05 +0200722 cb->tx_completion(ar, transfer_context, transfer_id);
Michal Kazior5440ce22013-09-03 15:09:58 +0200723 }
Kalle Valo5e3dd152013-06-12 20:52:10 +0300724}
725
726/* Called by lower (CE) layer when data is received from the Target. */
Michal Kazior5440ce22013-09-03 15:09:58 +0200727static void ath10k_pci_ce_recv_data(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300728{
729 struct ath10k *ar = ce_state->ar;
730 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +0200731 struct ath10k_pci_pipe *pipe_info = &ar_pci->pipe_info[ce_state->id];
Michal Kazior2f5280d2014-02-27 18:50:05 +0200732 struct ath10k_hif_cb *cb = &ar_pci->msg_callbacks_current;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300733 struct sk_buff *skb;
Michal Kazior5440ce22013-09-03 15:09:58 +0200734 void *transfer_context;
735 u32 ce_data;
Michal Kazior2f5280d2014-02-27 18:50:05 +0200736 unsigned int nbytes, max_nbytes;
Michal Kazior5440ce22013-09-03 15:09:58 +0200737 unsigned int transfer_id;
738 unsigned int flags;
Michal Kaziorc29a3802014-07-21 21:03:10 +0300739 int err, num_replenish = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300740
Michal Kazior5440ce22013-09-03 15:09:58 +0200741 while (ath10k_ce_completed_recv_next(ce_state, &transfer_context,
742 &ce_data, &nbytes, &transfer_id,
743 &flags) == 0) {
Michal Kaziorc29a3802014-07-21 21:03:10 +0300744 num_replenish++;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300745 skb = transfer_context;
Michal Kazior2f5280d2014-02-27 18:50:05 +0200746 max_nbytes = skb->len + skb_tailroom(skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300747 dma_unmap_single(ar->dev, ATH10K_SKB_CB(skb)->paddr,
Michal Kazior2f5280d2014-02-27 18:50:05 +0200748 max_nbytes, DMA_FROM_DEVICE);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300749
Michal Kazior2f5280d2014-02-27 18:50:05 +0200750 if (unlikely(max_nbytes < nbytes)) {
751 ath10k_warn("rxed more than expected (nbytes %d, max %d)",
752 nbytes, max_nbytes);
753 dev_kfree_skb_any(skb);
754 continue;
755 }
756
757 skb_put(skb, nbytes);
758 cb->rx_completion(ar, skb, pipe_info->pipe_num);
759 }
Michal Kaziorc29a3802014-07-21 21:03:10 +0300760
761 err = ath10k_pci_post_rx_pipe(pipe_info, num_replenish);
762 if (unlikely(err)) {
763 /* FIXME: retry */
764 ath10k_warn("failed to replenish CE rx ring %d (%d bufs): %d\n",
765 pipe_info->pipe_num, num_replenish, err);
766 }
Kalle Valo5e3dd152013-06-12 20:52:10 +0300767}
768
Michal Kazior726346f2014-02-27 18:50:04 +0200769static int ath10k_pci_hif_tx_sg(struct ath10k *ar, u8 pipe_id,
770 struct ath10k_hif_sg_item *items, int n_items)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300771{
Kalle Valo5e3dd152013-06-12 20:52:10 +0300772 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior726346f2014-02-27 18:50:04 +0200773 struct ath10k_pci_pipe *pci_pipe = &ar_pci->pipe_info[pipe_id];
774 struct ath10k_ce_pipe *ce_pipe = pci_pipe->ce_hdl;
775 struct ath10k_ce_ring *src_ring = ce_pipe->src_ring;
Michal Kazior7147a132014-05-26 12:02:58 +0200776 unsigned int nentries_mask;
777 unsigned int sw_index;
778 unsigned int write_index;
Michal Kazior08b8aa02014-05-26 12:02:59 +0200779 int err, i = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300780
Michal Kazior726346f2014-02-27 18:50:04 +0200781 spin_lock_bh(&ar_pci->ce_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300782
Michal Kazior7147a132014-05-26 12:02:58 +0200783 nentries_mask = src_ring->nentries_mask;
784 sw_index = src_ring->sw_index;
785 write_index = src_ring->write_index;
786
Michal Kazior726346f2014-02-27 18:50:04 +0200787 if (unlikely(CE_RING_DELTA(nentries_mask,
788 write_index, sw_index - 1) < n_items)) {
789 err = -ENOBUFS;
Michal Kazior08b8aa02014-05-26 12:02:59 +0200790 goto err;
Michal Kazior726346f2014-02-27 18:50:04 +0200791 }
792
793 for (i = 0; i < n_items - 1; i++) {
794 ath10k_dbg(ATH10K_DBG_PCI,
795 "pci tx item %d paddr 0x%08x len %d n_items %d\n",
796 i, items[i].paddr, items[i].len, n_items);
797 ath10k_dbg_dump(ATH10K_DBG_PCI_DUMP, NULL, "item data: ",
798 items[i].vaddr, items[i].len);
799
800 err = ath10k_ce_send_nolock(ce_pipe,
801 items[i].transfer_context,
802 items[i].paddr,
803 items[i].len,
804 items[i].transfer_id,
805 CE_SEND_FLAG_GATHER);
806 if (err)
Michal Kazior08b8aa02014-05-26 12:02:59 +0200807 goto err;
Michal Kazior726346f2014-02-27 18:50:04 +0200808 }
809
810 /* `i` is equal to `n_items -1` after for() */
Kalle Valo5e3dd152013-06-12 20:52:10 +0300811
812 ath10k_dbg(ATH10K_DBG_PCI,
Michal Kazior726346f2014-02-27 18:50:04 +0200813 "pci tx item %d paddr 0x%08x len %d n_items %d\n",
814 i, items[i].paddr, items[i].len, n_items);
815 ath10k_dbg_dump(ATH10K_DBG_PCI_DUMP, NULL, "item data: ",
816 items[i].vaddr, items[i].len);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300817
Michal Kazior726346f2014-02-27 18:50:04 +0200818 err = ath10k_ce_send_nolock(ce_pipe,
819 items[i].transfer_context,
820 items[i].paddr,
821 items[i].len,
822 items[i].transfer_id,
823 0);
824 if (err)
Michal Kazior08b8aa02014-05-26 12:02:59 +0200825 goto err;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300826
Michal Kazior08b8aa02014-05-26 12:02:59 +0200827 spin_unlock_bh(&ar_pci->ce_lock);
828 return 0;
829
830err:
831 for (; i > 0; i--)
832 __ath10k_ce_send_revert(ce_pipe);
833
Michal Kazior726346f2014-02-27 18:50:04 +0200834 spin_unlock_bh(&ar_pci->ce_lock);
835 return err;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300836}
837
838static u16 ath10k_pci_hif_get_free_queue_number(struct ath10k *ar, u8 pipe)
839{
840 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo50f87a62014-03-28 09:32:52 +0200841
842 ath10k_dbg(ATH10K_DBG_PCI, "pci hif get free queue number\n");
843
Michal Kazior3efcb3b2013-10-02 11:03:41 +0200844 return ath10k_ce_num_free_src_entries(ar_pci->pipe_info[pipe].ce_hdl);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300845}
846
Ben Greear384914b2014-08-25 08:37:32 +0300847static void ath10k_pci_dump_registers(struct ath10k *ar,
848 struct ath10k_fw_crash_data *crash_data)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300849{
Ben Greear384914b2014-08-25 08:37:32 +0300850 u32 i, reg_dump_values[REG_DUMP_COUNT_QCA988X] = {};
Kalle Valo5e3dd152013-06-12 20:52:10 +0300851 int ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300852
Ben Greear384914b2014-08-25 08:37:32 +0300853 lockdep_assert_held(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300854
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300855 ret = ath10k_pci_diag_read_hi(ar, &reg_dump_values[0],
856 hi_failure_state,
857 REG_DUMP_COUNT_QCA988X * sizeof(u32));
Michal Kazior1d2b48d2013-11-08 08:01:34 +0100858 if (ret) {
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300859 ath10k_err("failed to read firmware dump area: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300860 return;
861 }
862
863 BUILD_BUG_ON(REG_DUMP_COUNT_QCA988X % 4);
864
Kalle Valo0e9848c2014-08-25 08:37:37 +0300865 ath10k_err("firmware register dump:\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +0300866 for (i = 0; i < REG_DUMP_COUNT_QCA988X; i += 4)
867 ath10k_err("[%02d]: 0x%08X 0x%08X 0x%08X 0x%08X\n",
868 i,
869 reg_dump_values[i],
870 reg_dump_values[i + 1],
871 reg_dump_values[i + 2],
872 reg_dump_values[i + 3]);
Michal Kazioraffd3212013-07-16 09:54:35 +0200873
Ben Greear384914b2014-08-25 08:37:32 +0300874 /* crash_data is in little endian */
875 for (i = 0; i < REG_DUMP_COUNT_QCA988X; i++)
876 crash_data->registers[i] = cpu_to_le32(reg_dump_values[i]);
877}
878
Kalle Valo0e9848c2014-08-25 08:37:37 +0300879static void ath10k_pci_fw_crashed_dump(struct ath10k *ar)
Ben Greear384914b2014-08-25 08:37:32 +0300880{
881 struct ath10k_fw_crash_data *crash_data;
882 char uuid[50];
883
884 spin_lock_bh(&ar->data_lock);
885
886 crash_data = ath10k_debug_get_new_fw_crash_data(ar);
887
888 if (crash_data)
889 scnprintf(uuid, sizeof(uuid), "%pUl", &crash_data->uuid);
890 else
891 scnprintf(uuid, sizeof(uuid), "n/a");
892
893 ath10k_err("firmware crashed! (uuid %s)\n", uuid);
Kalle Valo8a0c7972014-08-25 08:37:45 +0300894 ath10k_print_driver_info(ar);
Ben Greear384914b2014-08-25 08:37:32 +0300895
896 if (!crash_data)
897 goto exit;
898
899 ath10k_pci_dump_registers(ar, crash_data);
900
901exit:
902 spin_unlock_bh(&ar->data_lock);
903
Michal Kazior5e90de82013-10-16 16:46:05 +0300904 queue_work(ar->workqueue, &ar->restart_work);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300905}
906
907static void ath10k_pci_hif_send_complete_check(struct ath10k *ar, u8 pipe,
908 int force)
909{
Kalle Valo50f87a62014-03-28 09:32:52 +0200910 ath10k_dbg(ATH10K_DBG_PCI, "pci hif send complete check\n");
911
Kalle Valo5e3dd152013-06-12 20:52:10 +0300912 if (!force) {
913 int resources;
914 /*
915 * Decide whether to actually poll for completions, or just
916 * wait for a later chance.
917 * If there seem to be plenty of resources left, then just wait
918 * since checking involves reading a CE register, which is a
919 * relatively expensive operation.
920 */
921 resources = ath10k_pci_hif_get_free_queue_number(ar, pipe);
922
923 /*
924 * If at least 50% of the total resources are still available,
925 * don't bother checking again yet.
926 */
927 if (resources > (host_ce_config_wlan[pipe].src_nentries >> 1))
928 return;
929 }
930 ath10k_ce_per_engine_service(ar, pipe);
931}
932
Michal Kaziore799bbf2013-07-05 16:15:12 +0300933static void ath10k_pci_hif_set_callbacks(struct ath10k *ar,
934 struct ath10k_hif_cb *callbacks)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300935{
936 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
937
Kalle Valo50f87a62014-03-28 09:32:52 +0200938 ath10k_dbg(ATH10K_DBG_PCI, "pci hif set callbacks\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +0300939
940 memcpy(&ar_pci->msg_callbacks_current, callbacks,
941 sizeof(ar_pci->msg_callbacks_current));
942}
943
Michal Kazior96a9d0d2013-11-08 08:01:25 +0100944static void ath10k_pci_kill_tasklet(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300945{
946 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300947 int i;
948
Kalle Valo5e3dd152013-06-12 20:52:10 +0300949 tasklet_kill(&ar_pci->intr_tq);
Michal Kazior103d4f52013-11-08 08:01:24 +0100950 tasklet_kill(&ar_pci->msi_fw_err);
Michal Kaziorab977bd2013-11-25 14:06:26 +0100951 tasklet_kill(&ar_pci->early_irq_tasklet);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300952
953 for (i = 0; i < CE_COUNT; i++)
954 tasklet_kill(&ar_pci->pipe_info[i].intr);
Michal Kazior96a9d0d2013-11-08 08:01:25 +0100955}
956
Kalle Valo5e3dd152013-06-12 20:52:10 +0300957/* TODO - temporary mapping while we have too few CE's */
958static int ath10k_pci_hif_map_service_to_pipe(struct ath10k *ar,
959 u16 service_id, u8 *ul_pipe,
960 u8 *dl_pipe, int *ul_is_polled,
961 int *dl_is_polled)
962{
963 int ret = 0;
964
Kalle Valo50f87a62014-03-28 09:32:52 +0200965 ath10k_dbg(ATH10K_DBG_PCI, "pci hif map service\n");
966
Kalle Valo5e3dd152013-06-12 20:52:10 +0300967 /* polling for received messages not supported */
968 *dl_is_polled = 0;
969
970 switch (service_id) {
971 case ATH10K_HTC_SVC_ID_HTT_DATA_MSG:
972 /*
973 * Host->target HTT gets its own pipe, so it can be polled
974 * while other pipes are interrupt driven.
975 */
976 *ul_pipe = 4;
977 /*
978 * Use the same target->host pipe for HTC ctrl, HTC raw
979 * streams, and HTT.
980 */
981 *dl_pipe = 1;
982 break;
983
984 case ATH10K_HTC_SVC_ID_RSVD_CTRL:
985 case ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS:
986 /*
987 * Note: HTC_RAW_STREAMS_SVC is currently unused, and
988 * HTC_CTRL_RSVD_SVC could share the same pipe as the
989 * WMI services. So, if another CE is needed, change
990 * this to *ul_pipe = 3, which frees up CE 0.
991 */
992 /* *ul_pipe = 3; */
993 *ul_pipe = 0;
994 *dl_pipe = 1;
995 break;
996
997 case ATH10K_HTC_SVC_ID_WMI_DATA_BK:
998 case ATH10K_HTC_SVC_ID_WMI_DATA_BE:
999 case ATH10K_HTC_SVC_ID_WMI_DATA_VI:
1000 case ATH10K_HTC_SVC_ID_WMI_DATA_VO:
1001
1002 case ATH10K_HTC_SVC_ID_WMI_CONTROL:
1003 *ul_pipe = 3;
1004 *dl_pipe = 2;
1005 break;
1006
1007 /* pipe 5 unused */
1008 /* pipe 6 reserved */
1009 /* pipe 7 reserved */
1010
1011 default:
1012 ret = -1;
1013 break;
1014 }
1015 *ul_is_polled =
1016 (host_ce_config_wlan[*ul_pipe].flags & CE_ATTR_DIS_INTR) != 0;
1017
1018 return ret;
1019}
1020
1021static void ath10k_pci_hif_get_default_pipe(struct ath10k *ar,
1022 u8 *ul_pipe, u8 *dl_pipe)
1023{
1024 int ul_is_polled, dl_is_polled;
1025
Kalle Valo50f87a62014-03-28 09:32:52 +02001026 ath10k_dbg(ATH10K_DBG_PCI, "pci hif get default pipe\n");
1027
Kalle Valo5e3dd152013-06-12 20:52:10 +03001028 (void)ath10k_pci_hif_map_service_to_pipe(ar,
1029 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1030 ul_pipe,
1031 dl_pipe,
1032 &ul_is_polled,
1033 &dl_is_polled);
1034}
1035
Michal Kazior87263e52013-08-27 13:08:01 +02001036static int ath10k_pci_post_rx_pipe(struct ath10k_pci_pipe *pipe_info,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001037 int num)
1038{
1039 struct ath10k *ar = pipe_info->hif_ce_state;
1040 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2aa39112013-08-27 13:08:02 +02001041 struct ath10k_ce_pipe *ce_state = pipe_info->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001042 struct sk_buff *skb;
1043 dma_addr_t ce_data;
1044 int i, ret = 0;
1045
1046 if (pipe_info->buf_sz == 0)
1047 return 0;
1048
1049 for (i = 0; i < num; i++) {
1050 skb = dev_alloc_skb(pipe_info->buf_sz);
1051 if (!skb) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001052 ath10k_warn("failed to allocate skbuff for pipe %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001053 num);
1054 ret = -ENOMEM;
1055 goto err;
1056 }
1057
1058 WARN_ONCE((unsigned long)skb->data & 3, "unaligned skb");
1059
1060 ce_data = dma_map_single(ar->dev, skb->data,
1061 skb->len + skb_tailroom(skb),
1062 DMA_FROM_DEVICE);
1063
1064 if (unlikely(dma_mapping_error(ar->dev, ce_data))) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001065 ath10k_warn("failed to DMA map sk_buff\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001066 dev_kfree_skb_any(skb);
1067 ret = -EIO;
1068 goto err;
1069 }
1070
1071 ATH10K_SKB_CB(skb)->paddr = ce_data;
1072
1073 pci_dma_sync_single_for_device(ar_pci->pdev, ce_data,
1074 pipe_info->buf_sz,
1075 PCI_DMA_FROMDEVICE);
1076
1077 ret = ath10k_ce_recv_buf_enqueue(ce_state, (void *)skb,
1078 ce_data);
1079 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001080 ath10k_warn("failed to enqueue to pipe %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001081 num, ret);
1082 goto err;
1083 }
1084 }
1085
1086 return ret;
1087
1088err:
1089 ath10k_pci_rx_pipe_cleanup(pipe_info);
1090 return ret;
1091}
1092
1093static int ath10k_pci_post_rx(struct ath10k *ar)
1094{
1095 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +02001096 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001097 const struct ce_attr *attr;
1098 int pipe_num, ret = 0;
1099
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001100 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001101 pipe_info = &ar_pci->pipe_info[pipe_num];
1102 attr = &host_ce_config_wlan[pipe_num];
1103
1104 if (attr->dest_nentries == 0)
1105 continue;
1106
1107 ret = ath10k_pci_post_rx_pipe(pipe_info,
1108 attr->dest_nentries - 1);
1109 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001110 ath10k_warn("failed to post RX buffer for pipe %d: %d\n",
1111 pipe_num, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001112
1113 for (; pipe_num >= 0; pipe_num--) {
1114 pipe_info = &ar_pci->pipe_info[pipe_num];
1115 ath10k_pci_rx_pipe_cleanup(pipe_info);
1116 }
1117 return ret;
1118 }
1119 }
1120
1121 return 0;
1122}
1123
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001124static void ath10k_pci_irq_disable(struct ath10k *ar)
1125{
1126 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1127 int i;
1128
1129 ath10k_ce_disable_interrupts(ar);
1130
1131 /* Regardless how many interrupts were assigned for MSI the first one
1132 * is always used for firmware indications (crashes). There's no way to
1133 * mask the irq in the device so call disable_irq(). Legacy (shared)
1134 * interrupts can be masked on the device though.
1135 */
1136 if (ar_pci->num_msi_intrs > 0)
1137 disable_irq(ar_pci->pdev->irq);
1138 else
1139 ath10k_pci_disable_and_clear_legacy_irq(ar);
1140
1141 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
1142 synchronize_irq(ar_pci->pdev->irq + i);
1143}
1144
1145static void ath10k_pci_irq_enable(struct ath10k *ar)
1146{
1147 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1148
1149 ath10k_ce_enable_interrupts(ar);
1150
1151 /* See comment in ath10k_pci_irq_disable() */
1152 if (ar_pci->num_msi_intrs > 0)
1153 enable_irq(ar_pci->pdev->irq);
1154 else
1155 ath10k_pci_enable_legacy_irq(ar);
1156}
1157
Kalle Valo5e3dd152013-06-12 20:52:10 +03001158static int ath10k_pci_hif_start(struct ath10k *ar)
1159{
1160 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorab977bd2013-11-25 14:06:26 +01001161 int ret, ret_early;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001162
Kalle Valo50f87a62014-03-28 09:32:52 +02001163 ath10k_dbg(ATH10K_DBG_BOOT, "boot hif start\n");
1164
Michal Kaziorab977bd2013-11-25 14:06:26 +01001165 ath10k_pci_free_early_irq(ar);
1166 ath10k_pci_kill_tasklet(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001167
Michal Kazior5d1aa942013-11-25 14:06:24 +01001168 ret = ath10k_pci_request_irq(ar);
1169 if (ret) {
1170 ath10k_warn("failed to post RX buffers for all pipes: %d\n",
1171 ret);
Michal Kazior2f5280d2014-02-27 18:50:05 +02001172 goto err_early_irq;
Michal Kazior5d1aa942013-11-25 14:06:24 +01001173 }
1174
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001175 ath10k_pci_irq_enable(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001176
1177 /* Post buffers once to start things off. */
1178 ret = ath10k_pci_post_rx(ar);
1179 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001180 ath10k_warn("failed to post RX buffers for all pipes: %d\n",
1181 ret);
Michal Kazior5d1aa942013-11-25 14:06:24 +01001182 goto err_stop;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001183 }
1184
1185 ar_pci->started = 1;
1186 return 0;
Michal Kaziorc80de122013-11-25 14:06:23 +01001187
Michal Kazior5d1aa942013-11-25 14:06:24 +01001188err_stop:
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001189 ath10k_pci_irq_disable(ar);
Michal Kazior5d1aa942013-11-25 14:06:24 +01001190 ath10k_pci_free_irq(ar);
1191 ath10k_pci_kill_tasklet(ar);
Michal Kaziorab977bd2013-11-25 14:06:26 +01001192err_early_irq:
1193 /* Though there should be no interrupts (device was reset)
1194 * power_down() expects the early IRQ to be installed as per the
1195 * driver lifecycle. */
1196 ret_early = ath10k_pci_request_early_irq(ar);
1197 if (ret_early)
1198 ath10k_warn("failed to re-enable early irq: %d\n", ret_early);
1199
Michal Kaziorc80de122013-11-25 14:06:23 +01001200 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001201}
1202
Michal Kazior87263e52013-08-27 13:08:01 +02001203static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe *pipe_info)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001204{
1205 struct ath10k *ar;
1206 struct ath10k_pci *ar_pci;
Michal Kazior2aa39112013-08-27 13:08:02 +02001207 struct ath10k_ce_pipe *ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001208 u32 buf_sz;
1209 struct sk_buff *netbuf;
1210 u32 ce_data;
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_revoke_recv_next(ce_hdl, (void **)&netbuf,
1227 &ce_data) == 0) {
1228 dma_unmap_single(ar->dev, ATH10K_SKB_CB(netbuf)->paddr,
1229 netbuf->len + skb_tailroom(netbuf),
1230 DMA_FROM_DEVICE);
1231 dev_kfree_skb_any(netbuf);
1232 }
1233}
1234
Michal Kazior87263e52013-08-27 13:08:01 +02001235static void ath10k_pci_tx_pipe_cleanup(struct ath10k_pci_pipe *pipe_info)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001236{
1237 struct ath10k *ar;
1238 struct ath10k_pci *ar_pci;
Michal Kazior2aa39112013-08-27 13:08:02 +02001239 struct ath10k_ce_pipe *ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001240 struct sk_buff *netbuf;
1241 u32 ce_data;
1242 unsigned int nbytes;
1243 unsigned int id;
1244 u32 buf_sz;
1245
1246 buf_sz = pipe_info->buf_sz;
1247
1248 /* Unused Copy Engine */
1249 if (buf_sz == 0)
1250 return;
1251
1252 ar = pipe_info->hif_ce_state;
1253 ar_pci = ath10k_pci_priv(ar);
1254
1255 if (!ar_pci->started)
1256 return;
1257
1258 ce_hdl = pipe_info->ce_hdl;
1259
1260 while (ath10k_ce_cancel_send_next(ce_hdl, (void **)&netbuf,
1261 &ce_data, &nbytes, &id) == 0) {
Michal Kaziora16942e2014-02-27 18:50:04 +02001262 /* no need to call tx completion for NULL pointers */
1263 if (!netbuf)
Michal Kazior2415fc12013-11-08 08:01:32 +01001264 continue;
Michal Kazior2415fc12013-11-08 08:01:32 +01001265
Kalle Valoe9bb0aa2013-09-08 18:36:11 +03001266 ar_pci->msg_callbacks_current.tx_completion(ar,
1267 netbuf,
1268 id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001269 }
1270}
1271
1272/*
1273 * Cleanup residual buffers for device shutdown:
1274 * buffers that were enqueued for receive
1275 * buffers that were to be sent
1276 * Note: Buffers that had completed but which were
1277 * not yet processed are on a completion queue. They
1278 * are handled when the completion thread shuts down.
1279 */
1280static void ath10k_pci_buffer_cleanup(struct ath10k *ar)
1281{
1282 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1283 int pipe_num;
1284
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001285 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Michal Kazior87263e52013-08-27 13:08:01 +02001286 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001287
1288 pipe_info = &ar_pci->pipe_info[pipe_num];
1289 ath10k_pci_rx_pipe_cleanup(pipe_info);
1290 ath10k_pci_tx_pipe_cleanup(pipe_info);
1291 }
1292}
1293
1294static void ath10k_pci_ce_deinit(struct ath10k *ar)
1295{
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001296 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001297
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001298 for (i = 0; i < CE_COUNT; i++)
1299 ath10k_ce_deinit_pipe(ar, i);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001300}
1301
1302static void ath10k_pci_hif_stop(struct ath10k *ar)
1303{
Michal Kazior32270b62013-08-02 09:15:47 +02001304 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior5d1aa942013-11-25 14:06:24 +01001305 int ret;
Michal Kazior32270b62013-08-02 09:15:47 +02001306
Kalle Valo50f87a62014-03-28 09:32:52 +02001307 ath10k_dbg(ATH10K_DBG_BOOT, "boot hif stop\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001308
Michal Kaziorf2708be2014-05-16 17:15:39 +03001309 if (WARN_ON(!ar_pci->started))
1310 return;
1311
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001312 ath10k_pci_irq_disable(ar);
Michal Kazior5d1aa942013-11-25 14:06:24 +01001313 ath10k_pci_free_irq(ar);
1314 ath10k_pci_kill_tasklet(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001315
Michal Kaziorab977bd2013-11-25 14:06:26 +01001316 ret = ath10k_pci_request_early_irq(ar);
1317 if (ret)
1318 ath10k_warn("failed to re-enable early irq: %d\n", ret);
1319
Kalle Valo5e3dd152013-06-12 20:52:10 +03001320 /* At this point, asynchronous threads are stopped, the target should
1321 * not DMA nor interrupt. We process the leftovers and then free
1322 * everything else up. */
1323
Kalle Valo5e3dd152013-06-12 20:52:10 +03001324 ath10k_pci_buffer_cleanup(ar);
Michal Kazior32270b62013-08-02 09:15:47 +02001325
Michal Kazior6a42a472013-11-08 08:01:35 +01001326 /* Make the sure the device won't access any structures on the host by
1327 * resetting it. The device was fed with PCI CE ringbuffer
1328 * configuration during init. If ringbuffers are freed and the device
1329 * were to access them this could lead to memory corruption on the
1330 * host. */
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001331 ath10k_pci_warm_reset(ar);
Michal Kazior6a42a472013-11-08 08:01:35 +01001332
Michal Kazior32270b62013-08-02 09:15:47 +02001333 ar_pci->started = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001334}
1335
1336static int ath10k_pci_hif_exchange_bmi_msg(struct ath10k *ar,
1337 void *req, u32 req_len,
1338 void *resp, u32 *resp_len)
1339{
1340 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2aa39112013-08-27 13:08:02 +02001341 struct ath10k_pci_pipe *pci_tx = &ar_pci->pipe_info[BMI_CE_NUM_TO_TARG];
1342 struct ath10k_pci_pipe *pci_rx = &ar_pci->pipe_info[BMI_CE_NUM_TO_HOST];
1343 struct ath10k_ce_pipe *ce_tx = pci_tx->ce_hdl;
1344 struct ath10k_ce_pipe *ce_rx = pci_rx->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001345 dma_addr_t req_paddr = 0;
1346 dma_addr_t resp_paddr = 0;
1347 struct bmi_xfer xfer = {};
1348 void *treq, *tresp = NULL;
1349 int ret = 0;
1350
Michal Kazior85622cd2013-11-25 14:06:22 +01001351 might_sleep();
1352
Kalle Valo5e3dd152013-06-12 20:52:10 +03001353 if (resp && !resp_len)
1354 return -EINVAL;
1355
1356 if (resp && resp_len && *resp_len == 0)
1357 return -EINVAL;
1358
1359 treq = kmemdup(req, req_len, GFP_KERNEL);
1360 if (!treq)
1361 return -ENOMEM;
1362
1363 req_paddr = dma_map_single(ar->dev, treq, req_len, DMA_TO_DEVICE);
1364 ret = dma_mapping_error(ar->dev, req_paddr);
1365 if (ret)
1366 goto err_dma;
1367
1368 if (resp && resp_len) {
1369 tresp = kzalloc(*resp_len, GFP_KERNEL);
1370 if (!tresp) {
1371 ret = -ENOMEM;
1372 goto err_req;
1373 }
1374
1375 resp_paddr = dma_map_single(ar->dev, tresp, *resp_len,
1376 DMA_FROM_DEVICE);
1377 ret = dma_mapping_error(ar->dev, resp_paddr);
1378 if (ret)
1379 goto err_req;
1380
1381 xfer.wait_for_resp = true;
1382 xfer.resp_len = 0;
1383
1384 ath10k_ce_recv_buf_enqueue(ce_rx, &xfer, resp_paddr);
1385 }
1386
Kalle Valo5e3dd152013-06-12 20:52:10 +03001387 ret = ath10k_ce_send(ce_tx, &xfer, req_paddr, req_len, -1, 0);
1388 if (ret)
1389 goto err_resp;
1390
Michal Kazior85622cd2013-11-25 14:06:22 +01001391 ret = ath10k_pci_bmi_wait(ce_tx, ce_rx, &xfer);
1392 if (ret) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001393 u32 unused_buffer;
1394 unsigned int unused_nbytes;
1395 unsigned int unused_id;
1396
Kalle Valo5e3dd152013-06-12 20:52:10 +03001397 ath10k_ce_cancel_send_next(ce_tx, NULL, &unused_buffer,
1398 &unused_nbytes, &unused_id);
1399 } else {
1400 /* non-zero means we did not time out */
1401 ret = 0;
1402 }
1403
1404err_resp:
1405 if (resp) {
1406 u32 unused_buffer;
1407
1408 ath10k_ce_revoke_recv_next(ce_rx, NULL, &unused_buffer);
1409 dma_unmap_single(ar->dev, resp_paddr,
1410 *resp_len, DMA_FROM_DEVICE);
1411 }
1412err_req:
1413 dma_unmap_single(ar->dev, req_paddr, req_len, DMA_TO_DEVICE);
1414
1415 if (ret == 0 && resp_len) {
1416 *resp_len = min(*resp_len, xfer.resp_len);
1417 memcpy(resp, tresp, xfer.resp_len);
1418 }
1419err_dma:
1420 kfree(treq);
1421 kfree(tresp);
1422
1423 return ret;
1424}
1425
Michal Kazior5440ce22013-09-03 15:09:58 +02001426static void ath10k_pci_bmi_send_done(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001427{
Michal Kazior5440ce22013-09-03 15:09:58 +02001428 struct bmi_xfer *xfer;
1429 u32 ce_data;
1430 unsigned int nbytes;
1431 unsigned int transfer_id;
1432
1433 if (ath10k_ce_completed_send_next(ce_state, (void **)&xfer, &ce_data,
1434 &nbytes, &transfer_id))
1435 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001436
Michal Kazior2374b182014-07-14 16:25:25 +03001437 xfer->tx_done = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001438}
1439
Michal Kazior5440ce22013-09-03 15:09:58 +02001440static void ath10k_pci_bmi_recv_data(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001441{
Michal Kazior5440ce22013-09-03 15:09:58 +02001442 struct bmi_xfer *xfer;
1443 u32 ce_data;
1444 unsigned int nbytes;
1445 unsigned int transfer_id;
1446 unsigned int flags;
1447
1448 if (ath10k_ce_completed_recv_next(ce_state, (void **)&xfer, &ce_data,
1449 &nbytes, &transfer_id, &flags))
1450 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001451
1452 if (!xfer->wait_for_resp) {
1453 ath10k_warn("unexpected: BMI data received; ignoring\n");
1454 return;
1455 }
1456
1457 xfer->resp_len = nbytes;
Michal Kazior2374b182014-07-14 16:25:25 +03001458 xfer->rx_done = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001459}
1460
Michal Kazior85622cd2013-11-25 14:06:22 +01001461static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
1462 struct ath10k_ce_pipe *rx_pipe,
1463 struct bmi_xfer *xfer)
1464{
1465 unsigned long timeout = jiffies + BMI_COMMUNICATION_TIMEOUT_HZ;
1466
1467 while (time_before_eq(jiffies, timeout)) {
1468 ath10k_pci_bmi_send_done(tx_pipe);
1469 ath10k_pci_bmi_recv_data(rx_pipe);
1470
Michal Kazior2374b182014-07-14 16:25:25 +03001471 if (xfer->tx_done && (xfer->rx_done == xfer->wait_for_resp))
Michal Kazior85622cd2013-11-25 14:06:22 +01001472 return 0;
1473
1474 schedule();
1475 }
1476
1477 return -ETIMEDOUT;
1478}
1479
Kalle Valo5e3dd152013-06-12 20:52:10 +03001480/*
1481 * Map from service/endpoint to Copy Engine.
1482 * This table is derived from the CE_PCI TABLE, above.
1483 * It is passed to the Target at startup for use by firmware.
1484 */
1485static const struct service_to_pipe target_service_to_ce_map_wlan[] = {
1486 {
1487 ATH10K_HTC_SVC_ID_WMI_DATA_VO,
1488 PIPEDIR_OUT, /* out = UL = host -> target */
1489 3,
1490 },
1491 {
1492 ATH10K_HTC_SVC_ID_WMI_DATA_VO,
1493 PIPEDIR_IN, /* in = DL = target -> host */
1494 2,
1495 },
1496 {
1497 ATH10K_HTC_SVC_ID_WMI_DATA_BK,
1498 PIPEDIR_OUT, /* out = UL = host -> target */
1499 3,
1500 },
1501 {
1502 ATH10K_HTC_SVC_ID_WMI_DATA_BK,
1503 PIPEDIR_IN, /* in = DL = target -> host */
1504 2,
1505 },
1506 {
1507 ATH10K_HTC_SVC_ID_WMI_DATA_BE,
1508 PIPEDIR_OUT, /* out = UL = host -> target */
1509 3,
1510 },
1511 {
1512 ATH10K_HTC_SVC_ID_WMI_DATA_BE,
1513 PIPEDIR_IN, /* in = DL = target -> host */
1514 2,
1515 },
1516 {
1517 ATH10K_HTC_SVC_ID_WMI_DATA_VI,
1518 PIPEDIR_OUT, /* out = UL = host -> target */
1519 3,
1520 },
1521 {
1522 ATH10K_HTC_SVC_ID_WMI_DATA_VI,
1523 PIPEDIR_IN, /* in = DL = target -> host */
1524 2,
1525 },
1526 {
1527 ATH10K_HTC_SVC_ID_WMI_CONTROL,
1528 PIPEDIR_OUT, /* out = UL = host -> target */
1529 3,
1530 },
1531 {
1532 ATH10K_HTC_SVC_ID_WMI_CONTROL,
1533 PIPEDIR_IN, /* in = DL = target -> host */
1534 2,
1535 },
1536 {
1537 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1538 PIPEDIR_OUT, /* out = UL = host -> target */
1539 0, /* could be moved to 3 (share with WMI) */
1540 },
1541 {
1542 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1543 PIPEDIR_IN, /* in = DL = target -> host */
1544 1,
1545 },
1546 {
1547 ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS, /* not currently used */
1548 PIPEDIR_OUT, /* out = UL = host -> target */
1549 0,
1550 },
1551 {
1552 ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS, /* not currently used */
1553 PIPEDIR_IN, /* in = DL = target -> host */
1554 1,
1555 },
1556 {
1557 ATH10K_HTC_SVC_ID_HTT_DATA_MSG,
1558 PIPEDIR_OUT, /* out = UL = host -> target */
1559 4,
1560 },
1561 {
1562 ATH10K_HTC_SVC_ID_HTT_DATA_MSG,
1563 PIPEDIR_IN, /* in = DL = target -> host */
1564 1,
1565 },
1566
1567 /* (Additions here) */
1568
1569 { /* Must be last */
1570 0,
1571 0,
1572 0,
1573 },
1574};
1575
1576/*
1577 * Send an interrupt to the device to wake up the Target CPU
1578 * so it has an opportunity to notice any changed state.
1579 */
1580static int ath10k_pci_wake_target_cpu(struct ath10k *ar)
1581{
1582 int ret;
1583 u32 core_ctrl;
1584
1585 ret = ath10k_pci_diag_read_access(ar, SOC_CORE_BASE_ADDRESS |
1586 CORE_CTRL_ADDRESS,
1587 &core_ctrl);
1588 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001589 ath10k_warn("failed to read core_ctrl: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001590 return ret;
1591 }
1592
1593 /* A_INUM_FIRMWARE interrupt to Target CPU */
1594 core_ctrl |= CORE_CTRL_CPU_INTR_MASK;
1595
1596 ret = ath10k_pci_diag_write_access(ar, SOC_CORE_BASE_ADDRESS |
1597 CORE_CTRL_ADDRESS,
1598 core_ctrl);
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001599 if (ret) {
1600 ath10k_warn("failed to set target CPU interrupt mask: %d\n",
1601 ret);
1602 return ret;
1603 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001604
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001605 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001606}
1607
1608static int ath10k_pci_init_config(struct ath10k *ar)
1609{
1610 u32 interconnect_targ_addr;
1611 u32 pcie_state_targ_addr = 0;
1612 u32 pipe_cfg_targ_addr = 0;
1613 u32 svc_to_pipe_map = 0;
1614 u32 pcie_config_flags = 0;
1615 u32 ealloc_value;
1616 u32 ealloc_targ_addr;
1617 u32 flag2_value;
1618 u32 flag2_targ_addr;
1619 int ret = 0;
1620
1621 /* Download to Target the CE Config and the service-to-CE map */
1622 interconnect_targ_addr =
1623 host_interest_item_address(HI_ITEM(hi_interconnect_state));
1624
1625 /* Supply Target-side CE configuration */
1626 ret = ath10k_pci_diag_read_access(ar, interconnect_targ_addr,
1627 &pcie_state_targ_addr);
1628 if (ret != 0) {
1629 ath10k_err("Failed to get pcie state addr: %d\n", ret);
1630 return ret;
1631 }
1632
1633 if (pcie_state_targ_addr == 0) {
1634 ret = -EIO;
1635 ath10k_err("Invalid pcie state addr\n");
1636 return ret;
1637 }
1638
1639 ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
1640 offsetof(struct pcie_state,
1641 pipe_cfg_addr),
1642 &pipe_cfg_targ_addr);
1643 if (ret != 0) {
1644 ath10k_err("Failed to get pipe cfg addr: %d\n", ret);
1645 return ret;
1646 }
1647
1648 if (pipe_cfg_targ_addr == 0) {
1649 ret = -EIO;
1650 ath10k_err("Invalid pipe cfg addr\n");
1651 return ret;
1652 }
1653
1654 ret = ath10k_pci_diag_write_mem(ar, pipe_cfg_targ_addr,
1655 target_ce_config_wlan,
1656 sizeof(target_ce_config_wlan));
1657
1658 if (ret != 0) {
1659 ath10k_err("Failed to write pipe cfg: %d\n", ret);
1660 return ret;
1661 }
1662
1663 ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
1664 offsetof(struct pcie_state,
1665 svc_to_pipe_map),
1666 &svc_to_pipe_map);
1667 if (ret != 0) {
1668 ath10k_err("Failed to get svc/pipe map: %d\n", ret);
1669 return ret;
1670 }
1671
1672 if (svc_to_pipe_map == 0) {
1673 ret = -EIO;
1674 ath10k_err("Invalid svc_to_pipe map\n");
1675 return ret;
1676 }
1677
1678 ret = ath10k_pci_diag_write_mem(ar, svc_to_pipe_map,
1679 target_service_to_ce_map_wlan,
1680 sizeof(target_service_to_ce_map_wlan));
1681 if (ret != 0) {
1682 ath10k_err("Failed to write svc/pipe map: %d\n", ret);
1683 return ret;
1684 }
1685
1686 ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
1687 offsetof(struct pcie_state,
1688 config_flags),
1689 &pcie_config_flags);
1690 if (ret != 0) {
1691 ath10k_err("Failed to get pcie config_flags: %d\n", ret);
1692 return ret;
1693 }
1694
1695 pcie_config_flags &= ~PCIE_CONFIG_FLAG_ENABLE_L1;
1696
1697 ret = ath10k_pci_diag_write_mem(ar, pcie_state_targ_addr +
1698 offsetof(struct pcie_state, config_flags),
1699 &pcie_config_flags,
1700 sizeof(pcie_config_flags));
1701 if (ret != 0) {
1702 ath10k_err("Failed to write pcie config_flags: %d\n", ret);
1703 return ret;
1704 }
1705
1706 /* configure early allocation */
1707 ealloc_targ_addr = host_interest_item_address(HI_ITEM(hi_early_alloc));
1708
1709 ret = ath10k_pci_diag_read_access(ar, ealloc_targ_addr, &ealloc_value);
1710 if (ret != 0) {
1711 ath10k_err("Faile to get early alloc val: %d\n", ret);
1712 return ret;
1713 }
1714
1715 /* first bank is switched to IRAM */
1716 ealloc_value |= ((HI_EARLY_ALLOC_MAGIC << HI_EARLY_ALLOC_MAGIC_SHIFT) &
1717 HI_EARLY_ALLOC_MAGIC_MASK);
1718 ealloc_value |= ((1 << HI_EARLY_ALLOC_IRAM_BANKS_SHIFT) &
1719 HI_EARLY_ALLOC_IRAM_BANKS_MASK);
1720
1721 ret = ath10k_pci_diag_write_access(ar, ealloc_targ_addr, ealloc_value);
1722 if (ret != 0) {
1723 ath10k_err("Failed to set early alloc val: %d\n", ret);
1724 return ret;
1725 }
1726
1727 /* Tell Target to proceed with initialization */
1728 flag2_targ_addr = host_interest_item_address(HI_ITEM(hi_option_flag2));
1729
1730 ret = ath10k_pci_diag_read_access(ar, flag2_targ_addr, &flag2_value);
1731 if (ret != 0) {
1732 ath10k_err("Failed to get option val: %d\n", ret);
1733 return ret;
1734 }
1735
1736 flag2_value |= HI_OPTION_EARLY_CFG_DONE;
1737
1738 ret = ath10k_pci_diag_write_access(ar, flag2_targ_addr, flag2_value);
1739 if (ret != 0) {
1740 ath10k_err("Failed to set option val: %d\n", ret);
1741 return ret;
1742 }
1743
1744 return 0;
1745}
1746
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001747static int ath10k_pci_alloc_ce(struct ath10k *ar)
1748{
1749 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001750
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001751 for (i = 0; i < CE_COUNT; i++) {
1752 ret = ath10k_ce_alloc_pipe(ar, i, &host_ce_config_wlan[i]);
1753 if (ret) {
1754 ath10k_err("failed to allocate copy engine pipe %d: %d\n",
1755 i, ret);
1756 return ret;
1757 }
1758 }
1759
1760 return 0;
1761}
1762
1763static void ath10k_pci_free_ce(struct ath10k *ar)
1764{
1765 int i;
1766
1767 for (i = 0; i < CE_COUNT; i++)
1768 ath10k_ce_free_pipe(ar, i);
1769}
Kalle Valo5e3dd152013-06-12 20:52:10 +03001770
1771static int ath10k_pci_ce_init(struct ath10k *ar)
1772{
1773 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +02001774 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001775 const struct ce_attr *attr;
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001776 int pipe_num, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001777
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001778 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001779 pipe_info = &ar_pci->pipe_info[pipe_num];
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001780 pipe_info->ce_hdl = &ar_pci->ce_states[pipe_num];
Kalle Valo5e3dd152013-06-12 20:52:10 +03001781 pipe_info->pipe_num = pipe_num;
1782 pipe_info->hif_ce_state = ar;
1783 attr = &host_ce_config_wlan[pipe_num];
1784
Michal Kazior145cc122014-08-22 14:23:32 +02001785 ret = ath10k_ce_init_pipe(ar, pipe_num, attr,
1786 ath10k_pci_ce_send_done,
1787 ath10k_pci_ce_recv_data);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001788 if (ret) {
1789 ath10k_err("failed to initialize copy engine pipe %d: %d\n",
1790 pipe_num, ret);
1791 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001792 }
1793
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001794 if (pipe_num == CE_COUNT - 1) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001795 /*
1796 * Reserve the ultimate CE for
1797 * diagnostic Window support
1798 */
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001799 ar_pci->ce_diag = pipe_info->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001800 continue;
1801 }
1802
1803 pipe_info->buf_sz = (size_t) (attr->src_sz_max);
1804 }
1805
Kalle Valo5e3dd152013-06-12 20:52:10 +03001806 return 0;
1807}
1808
1809static void ath10k_pci_fw_interrupt_handler(struct ath10k *ar)
1810{
1811 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valob39712c2014-03-28 09:32:46 +02001812 u32 fw_indicator;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001813
Kalle Valob39712c2014-03-28 09:32:46 +02001814 fw_indicator = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001815
1816 if (fw_indicator & FW_IND_EVENT_PENDING) {
1817 /* ACK: clear Target-side pending event */
Kalle Valob39712c2014-03-28 09:32:46 +02001818 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001819 fw_indicator & ~FW_IND_EVENT_PENDING);
1820
1821 if (ar_pci->started) {
Kalle Valo0e9848c2014-08-25 08:37:37 +03001822 ath10k_pci_fw_crashed_dump(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001823 } else {
1824 /*
1825 * Probable Target failure before we're prepared
1826 * to handle it. Generally unexpected.
1827 */
1828 ath10k_warn("early firmware event indicated\n");
1829 }
1830 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001831}
1832
Michal Kaziorde013572014-05-14 16:56:16 +03001833/* this function effectively clears target memory controller assert line */
1834static void ath10k_pci_warm_reset_si0(struct ath10k *ar)
1835{
1836 u32 val;
1837
1838 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1839 ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
1840 val | SOC_RESET_CONTROL_SI0_RST_MASK);
1841 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1842
1843 msleep(10);
1844
1845 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1846 ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
1847 val & ~SOC_RESET_CONTROL_SI0_RST_MASK);
1848 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1849
1850 msleep(10);
1851}
1852
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001853static int ath10k_pci_warm_reset(struct ath10k *ar)
1854{
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001855 u32 val;
1856
Kalle Valo50f87a62014-03-28 09:32:52 +02001857 ath10k_dbg(ATH10K_DBG_BOOT, "boot warm reset\n");
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001858
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001859 /* debug */
1860 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1861 PCIE_INTR_CAUSE_ADDRESS);
1862 ath10k_dbg(ATH10K_DBG_BOOT, "boot host cpu intr cause: 0x%08x\n", val);
1863
1864 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1865 CPU_INTR_ADDRESS);
1866 ath10k_dbg(ATH10K_DBG_BOOT, "boot target cpu intr cause: 0x%08x\n",
1867 val);
1868
1869 /* disable pending irqs */
1870 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
1871 PCIE_INTR_ENABLE_ADDRESS, 0);
1872
1873 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
1874 PCIE_INTR_CLR_ADDRESS, ~0);
1875
1876 msleep(100);
1877
1878 /* clear fw indicator */
Kalle Valob39712c2014-03-28 09:32:46 +02001879 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, 0);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001880
1881 /* clear target LF timer interrupts */
1882 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1883 SOC_LF_TIMER_CONTROL0_ADDRESS);
1884 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS +
1885 SOC_LF_TIMER_CONTROL0_ADDRESS,
1886 val & ~SOC_LF_TIMER_CONTROL0_ENABLE_MASK);
1887
1888 /* reset CE */
1889 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1890 SOC_RESET_CONTROL_ADDRESS);
1891 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1892 val | SOC_RESET_CONTROL_CE_RST_MASK);
1893 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1894 SOC_RESET_CONTROL_ADDRESS);
1895 msleep(10);
1896
1897 /* unreset CE */
1898 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1899 val & ~SOC_RESET_CONTROL_CE_RST_MASK);
1900 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1901 SOC_RESET_CONTROL_ADDRESS);
1902 msleep(10);
1903
Michal Kaziorde013572014-05-14 16:56:16 +03001904 ath10k_pci_warm_reset_si0(ar);
1905
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001906 /* debug */
1907 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1908 PCIE_INTR_CAUSE_ADDRESS);
1909 ath10k_dbg(ATH10K_DBG_BOOT, "boot host cpu intr cause: 0x%08x\n", val);
1910
1911 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1912 CPU_INTR_ADDRESS);
1913 ath10k_dbg(ATH10K_DBG_BOOT, "boot target cpu intr cause: 0x%08x\n",
1914 val);
1915
1916 /* CPU warm reset */
1917 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1918 SOC_RESET_CONTROL_ADDRESS);
1919 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1920 val | SOC_RESET_CONTROL_CPU_WARM_RST_MASK);
1921
1922 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1923 SOC_RESET_CONTROL_ADDRESS);
1924 ath10k_dbg(ATH10K_DBG_BOOT, "boot target reset state: 0x%08x\n", val);
1925
1926 msleep(100);
1927
1928 ath10k_dbg(ATH10K_DBG_BOOT, "boot warm reset complete\n");
1929
Michal Kaziorc0c378f2014-08-07 11:03:28 +02001930 return 0;
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001931}
1932
1933static int __ath10k_pci_hif_power_up(struct ath10k *ar, bool cold_reset)
Michal Kazior8c5c5362013-07-16 09:38:50 +02001934{
1935 int ret;
1936
1937 /*
1938 * Bring the target up cleanly.
1939 *
1940 * The target may be in an undefined state with an AUX-powered Target
1941 * and a Host in WoW mode. If the Host crashes, loses power, or is
1942 * restarted (without unloading the driver) then the Target is left
1943 * (aux) powered and running. On a subsequent driver load, the Target
1944 * is in an unexpected state. We try to catch that here in order to
1945 * reset the Target and retry the probe.
1946 */
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001947 if (cold_reset)
1948 ret = ath10k_pci_cold_reset(ar);
1949 else
1950 ret = ath10k_pci_warm_reset(ar);
1951
Michal Kazior5b2589f2013-11-08 08:01:30 +01001952 if (ret) {
1953 ath10k_err("failed to reset target: %d\n", ret);
Michal Kazior98563d52013-11-08 08:01:33 +01001954 goto err;
Michal Kazior5b2589f2013-11-08 08:01:30 +01001955 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02001956
Michal Kazior8c5c5362013-07-16 09:38:50 +02001957 ret = ath10k_pci_ce_init(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001958 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001959 ath10k_err("failed to initialize CE: %d\n", ret);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02001960 goto err;
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001961 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02001962
Michal Kaziorab977bd2013-11-25 14:06:26 +01001963 ret = ath10k_pci_request_early_irq(ar);
1964 if (ret) {
1965 ath10k_err("failed to request early irq: %d\n", ret);
Michal Kazior403d6272014-08-22 14:23:31 +02001966 goto err_ce;
Michal Kaziorab977bd2013-11-25 14:06:26 +01001967 }
1968
Michal Kazior98563d52013-11-08 08:01:33 +01001969 ret = ath10k_pci_wait_for_target_init(ar);
1970 if (ret) {
1971 ath10k_err("failed to wait for target to init: %d\n", ret);
Michal Kaziorab977bd2013-11-25 14:06:26 +01001972 goto err_free_early_irq;
Michal Kazior98563d52013-11-08 08:01:33 +01001973 }
1974
1975 ret = ath10k_pci_init_config(ar);
1976 if (ret) {
1977 ath10k_err("failed to setup init config: %d\n", ret);
Michal Kaziorab977bd2013-11-25 14:06:26 +01001978 goto err_free_early_irq;
Michal Kazior98563d52013-11-08 08:01:33 +01001979 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02001980
1981 ret = ath10k_pci_wake_target_cpu(ar);
1982 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001983 ath10k_err("could not wake up target CPU: %d\n", ret);
Michal Kaziorab977bd2013-11-25 14:06:26 +01001984 goto err_free_early_irq;
Michal Kazior8c5c5362013-07-16 09:38:50 +02001985 }
1986
1987 return 0;
1988
Michal Kaziorab977bd2013-11-25 14:06:26 +01001989err_free_early_irq:
1990 ath10k_pci_free_early_irq(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001991err_ce:
1992 ath10k_pci_ce_deinit(ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001993 ath10k_pci_warm_reset(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001994err:
1995 return ret;
1996}
1997
Michal Kazior61c95ce2014-05-14 16:56:16 +03001998static int ath10k_pci_hif_power_up_warm(struct ath10k *ar)
1999{
2000 int i, ret;
2001
2002 /*
2003 * Sometime warm reset succeeds after retries.
2004 *
2005 * FIXME: It might be possible to tune ath10k_pci_warm_reset() to work
2006 * at first try.
2007 */
2008 for (i = 0; i < ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS; i++) {
2009 ret = __ath10k_pci_hif_power_up(ar, false);
2010 if (ret == 0)
2011 break;
2012
2013 ath10k_warn("failed to warm reset (attempt %d out of %d): %d\n",
2014 i + 1, ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS, ret);
2015 }
2016
2017 return ret;
2018}
2019
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002020static int ath10k_pci_hif_power_up(struct ath10k *ar)
2021{
2022 int ret;
2023
Kalle Valo50f87a62014-03-28 09:32:52 +02002024 ath10k_dbg(ATH10K_DBG_BOOT, "boot hif power up\n");
2025
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002026 /*
2027 * Hardware CUS232 version 2 has some issues with cold reset and the
2028 * preferred (and safer) way to perform a device reset is through a
2029 * warm reset.
2030 *
Michal Kazior61c95ce2014-05-14 16:56:16 +03002031 * Warm reset doesn't always work though so fall back to cold reset may
2032 * be necessary.
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002033 */
Michal Kazior61c95ce2014-05-14 16:56:16 +03002034 ret = ath10k_pci_hif_power_up_warm(ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002035 if (ret) {
Kalle Valo35098462014-03-28 09:32:27 +02002036 ath10k_warn("failed to power up target using warm reset: %d\n",
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002037 ret);
2038
Kalle Valo35098462014-03-28 09:32:27 +02002039 if (ath10k_pci_reset_mode == ATH10K_PCI_RESET_WARM_ONLY)
2040 return ret;
2041
2042 ath10k_warn("trying cold reset\n");
2043
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002044 ret = __ath10k_pci_hif_power_up(ar, true);
2045 if (ret) {
2046 ath10k_err("failed to power up target using cold reset too (%d)\n",
2047 ret);
2048 return ret;
2049 }
2050 }
2051
2052 return 0;
2053}
2054
Michal Kazior8c5c5362013-07-16 09:38:50 +02002055static void ath10k_pci_hif_power_down(struct ath10k *ar)
2056{
Kalle Valo50f87a62014-03-28 09:32:52 +02002057 ath10k_dbg(ATH10K_DBG_BOOT, "boot hif power down\n");
2058
Michal Kaziorab977bd2013-11-25 14:06:26 +01002059 ath10k_pci_free_early_irq(ar);
2060 ath10k_pci_kill_tasklet(ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002061 ath10k_pci_warm_reset(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02002062}
2063
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002064#ifdef CONFIG_PM
2065
2066#define ATH10K_PCI_PM_CONTROL 0x44
2067
2068static int ath10k_pci_hif_suspend(struct ath10k *ar)
2069{
2070 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2071 struct pci_dev *pdev = ar_pci->pdev;
2072 u32 val;
2073
2074 pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
2075
2076 if ((val & 0x000000ff) != 0x3) {
2077 pci_save_state(pdev);
2078 pci_disable_device(pdev);
2079 pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
2080 (val & 0xffffff00) | 0x03);
2081 }
2082
2083 return 0;
2084}
2085
2086static int ath10k_pci_hif_resume(struct ath10k *ar)
2087{
2088 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2089 struct pci_dev *pdev = ar_pci->pdev;
2090 u32 val;
2091
2092 pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
2093
2094 if ((val & 0x000000ff) != 0) {
2095 pci_restore_state(pdev);
2096 pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
2097 val & 0xffffff00);
2098 /*
2099 * Suspend/Resume resets the PCI configuration space,
2100 * so we have to re-disable the RETRY_TIMEOUT register (0x41)
2101 * to keep PCI Tx retries from interfering with C3 CPU state
2102 */
2103 pci_read_config_dword(pdev, 0x40, &val);
2104
2105 if ((val & 0x0000ff00) != 0)
2106 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
2107 }
2108
2109 return 0;
2110}
2111#endif
2112
Kalle Valo5e3dd152013-06-12 20:52:10 +03002113static const struct ath10k_hif_ops ath10k_pci_hif_ops = {
Michal Kazior726346f2014-02-27 18:50:04 +02002114 .tx_sg = ath10k_pci_hif_tx_sg,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002115 .exchange_bmi_msg = ath10k_pci_hif_exchange_bmi_msg,
2116 .start = ath10k_pci_hif_start,
2117 .stop = ath10k_pci_hif_stop,
2118 .map_service_to_pipe = ath10k_pci_hif_map_service_to_pipe,
2119 .get_default_pipe = ath10k_pci_hif_get_default_pipe,
2120 .send_complete_check = ath10k_pci_hif_send_complete_check,
Michal Kaziore799bbf2013-07-05 16:15:12 +03002121 .set_callbacks = ath10k_pci_hif_set_callbacks,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002122 .get_free_queue_number = ath10k_pci_hif_get_free_queue_number,
Michal Kazior8c5c5362013-07-16 09:38:50 +02002123 .power_up = ath10k_pci_hif_power_up,
2124 .power_down = ath10k_pci_hif_power_down,
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002125#ifdef CONFIG_PM
2126 .suspend = ath10k_pci_hif_suspend,
2127 .resume = ath10k_pci_hif_resume,
2128#endif
Kalle Valo5e3dd152013-06-12 20:52:10 +03002129};
2130
2131static void ath10k_pci_ce_tasklet(unsigned long ptr)
2132{
Michal Kazior87263e52013-08-27 13:08:01 +02002133 struct ath10k_pci_pipe *pipe = (struct ath10k_pci_pipe *)ptr;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002134 struct ath10k_pci *ar_pci = pipe->ar_pci;
2135
2136 ath10k_ce_per_engine_service(ar_pci->ar, pipe->pipe_num);
2137}
2138
2139static void ath10k_msi_err_tasklet(unsigned long data)
2140{
2141 struct ath10k *ar = (struct ath10k *)data;
2142
2143 ath10k_pci_fw_interrupt_handler(ar);
2144}
2145
2146/*
2147 * Handler for a per-engine interrupt on a PARTICULAR CE.
2148 * This is used in cases where each CE has a private MSI interrupt.
2149 */
2150static irqreturn_t ath10k_pci_per_engine_handler(int irq, void *arg)
2151{
2152 struct ath10k *ar = arg;
2153 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2154 int ce_id = irq - ar_pci->pdev->irq - MSI_ASSIGN_CE_INITIAL;
2155
Dan Carpentere5742672013-06-18 10:28:46 +03002156 if (ce_id < 0 || ce_id >= ARRAY_SIZE(ar_pci->pipe_info)) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03002157 ath10k_warn("unexpected/invalid irq %d ce_id %d\n", irq, ce_id);
2158 return IRQ_HANDLED;
2159 }
2160
2161 /*
2162 * NOTE: We are able to derive ce_id from irq because we
2163 * use a one-to-one mapping for CE's 0..5.
2164 * CE's 6 & 7 do not use interrupts at all.
2165 *
2166 * This mapping must be kept in sync with the mapping
2167 * used by firmware.
2168 */
2169 tasklet_schedule(&ar_pci->pipe_info[ce_id].intr);
2170 return IRQ_HANDLED;
2171}
2172
2173static irqreturn_t ath10k_pci_msi_fw_handler(int irq, void *arg)
2174{
2175 struct ath10k *ar = arg;
2176 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2177
2178 tasklet_schedule(&ar_pci->msi_fw_err);
2179 return IRQ_HANDLED;
2180}
2181
2182/*
2183 * Top-level interrupt handler for all PCI interrupts from a Target.
2184 * When a block of MSI interrupts is allocated, this top-level handler
2185 * is not used; instead, we directly call the correct sub-handler.
2186 */
2187static irqreturn_t ath10k_pci_interrupt_handler(int irq, void *arg)
2188{
2189 struct ath10k *ar = arg;
2190 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2191
2192 if (ar_pci->num_msi_intrs == 0) {
Michal Kaziore5398872013-11-25 14:06:20 +01002193 if (!ath10k_pci_irq_pending(ar))
2194 return IRQ_NONE;
2195
Michal Kazior26852182013-11-25 14:06:25 +01002196 ath10k_pci_disable_and_clear_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002197 }
2198
2199 tasklet_schedule(&ar_pci->intr_tq);
2200
2201 return IRQ_HANDLED;
2202}
2203
Michal Kaziorab977bd2013-11-25 14:06:26 +01002204static void ath10k_pci_early_irq_tasklet(unsigned long data)
2205{
2206 struct ath10k *ar = (struct ath10k *)data;
Michal Kaziorab977bd2013-11-25 14:06:26 +01002207 u32 fw_ind;
Michal Kaziorab977bd2013-11-25 14:06:26 +01002208
Kalle Valob39712c2014-03-28 09:32:46 +02002209 fw_ind = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
Michal Kaziorab977bd2013-11-25 14:06:26 +01002210 if (fw_ind & FW_IND_EVENT_PENDING) {
Kalle Valob39712c2014-03-28 09:32:46 +02002211 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS,
Michal Kaziorab977bd2013-11-25 14:06:26 +01002212 fw_ind & ~FW_IND_EVENT_PENDING);
Kalle Valo0e9848c2014-08-25 08:37:37 +03002213 ath10k_pci_fw_crashed_dump(ar);
Michal Kaziorab977bd2013-11-25 14:06:26 +01002214 }
2215
Michal Kaziorab977bd2013-11-25 14:06:26 +01002216 ath10k_pci_enable_legacy_irq(ar);
2217}
2218
Kalle Valo5e3dd152013-06-12 20:52:10 +03002219static void ath10k_pci_tasklet(unsigned long data)
2220{
2221 struct ath10k *ar = (struct ath10k *)data;
2222 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2223
2224 ath10k_pci_fw_interrupt_handler(ar); /* FIXME: Handle FW error */
2225 ath10k_ce_per_engine_service_any(ar);
2226
Michal Kazior26852182013-11-25 14:06:25 +01002227 /* Re-enable legacy irq that was disabled in the irq handler */
2228 if (ar_pci->num_msi_intrs == 0)
2229 ath10k_pci_enable_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002230}
2231
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002232static int ath10k_pci_request_irq_msix(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002233{
2234 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002235 int ret, i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002236
2237 ret = request_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW,
2238 ath10k_pci_msi_fw_handler,
2239 IRQF_SHARED, "ath10k_pci", ar);
Michal Kazior591ecdb2013-07-31 10:55:15 +02002240 if (ret) {
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002241 ath10k_warn("failed to request MSI-X fw irq %d: %d\n",
Michal Kazior591ecdb2013-07-31 10:55:15 +02002242 ar_pci->pdev->irq + MSI_ASSIGN_FW, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002243 return ret;
Michal Kazior591ecdb2013-07-31 10:55:15 +02002244 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002245
2246 for (i = MSI_ASSIGN_CE_INITIAL; i <= MSI_ASSIGN_CE_MAX; i++) {
2247 ret = request_irq(ar_pci->pdev->irq + i,
2248 ath10k_pci_per_engine_handler,
2249 IRQF_SHARED, "ath10k_pci", ar);
2250 if (ret) {
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002251 ath10k_warn("failed to request MSI-X ce irq %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002252 ar_pci->pdev->irq + i, ret);
2253
Michal Kazior87b14232013-06-26 08:50:50 +02002254 for (i--; i >= MSI_ASSIGN_CE_INITIAL; i--)
2255 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002256
Michal Kazior87b14232013-06-26 08:50:50 +02002257 free_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002258 return ret;
2259 }
2260 }
2261
Kalle Valo5e3dd152013-06-12 20:52:10 +03002262 return 0;
2263}
2264
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002265static int ath10k_pci_request_irq_msi(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002266{
2267 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2268 int ret;
2269
2270 ret = request_irq(ar_pci->pdev->irq,
2271 ath10k_pci_interrupt_handler,
2272 IRQF_SHARED, "ath10k_pci", ar);
Kalle Valof3782742013-10-17 11:36:15 +03002273 if (ret) {
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002274 ath10k_warn("failed to request MSI irq %d: %d\n",
2275 ar_pci->pdev->irq, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002276 return ret;
Kalle Valof3782742013-10-17 11:36:15 +03002277 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002278
Kalle Valo5e3dd152013-06-12 20:52:10 +03002279 return 0;
2280}
2281
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002282static int ath10k_pci_request_irq_legacy(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002283{
2284 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002285 int ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002286
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002287 ret = request_irq(ar_pci->pdev->irq,
2288 ath10k_pci_interrupt_handler,
2289 IRQF_SHARED, "ath10k_pci", ar);
Kalle Valof3782742013-10-17 11:36:15 +03002290 if (ret) {
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002291 ath10k_warn("failed to request legacy irq %d: %d\n",
2292 ar_pci->pdev->irq, ret);
Kalle Valof3782742013-10-17 11:36:15 +03002293 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002294 }
2295
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002296 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002297}
2298
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002299static int ath10k_pci_request_irq(struct ath10k *ar)
2300{
2301 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2302
2303 switch (ar_pci->num_msi_intrs) {
2304 case 0:
2305 return ath10k_pci_request_irq_legacy(ar);
2306 case 1:
2307 return ath10k_pci_request_irq_msi(ar);
2308 case MSI_NUM_REQUEST:
2309 return ath10k_pci_request_irq_msix(ar);
2310 }
2311
2312 ath10k_warn("unknown irq configuration upon request\n");
2313 return -EINVAL;
2314}
2315
2316static void ath10k_pci_free_irq(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002317{
2318 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2319 int i;
2320
2321 /* There's at least one interrupt irregardless whether its legacy INTR
2322 * or MSI or MSI-X */
2323 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
2324 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002325}
2326
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002327static void ath10k_pci_init_irq_tasklets(struct ath10k *ar)
2328{
2329 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2330 int i;
2331
2332 tasklet_init(&ar_pci->intr_tq, ath10k_pci_tasklet, (unsigned long)ar);
2333 tasklet_init(&ar_pci->msi_fw_err, ath10k_msi_err_tasklet,
2334 (unsigned long)ar);
Michal Kaziorab977bd2013-11-25 14:06:26 +01002335 tasklet_init(&ar_pci->early_irq_tasklet, ath10k_pci_early_irq_tasklet,
2336 (unsigned long)ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002337
2338 for (i = 0; i < CE_COUNT; i++) {
2339 ar_pci->pipe_info[i].ar_pci = ar_pci;
2340 tasklet_init(&ar_pci->pipe_info[i].intr, ath10k_pci_ce_tasklet,
2341 (unsigned long)&ar_pci->pipe_info[i]);
2342 }
2343}
2344
2345static int ath10k_pci_init_irq(struct ath10k *ar)
2346{
2347 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2348 int ret;
2349
2350 ath10k_pci_init_irq_tasklets(ar);
2351
Michal Kazior403d6272014-08-22 14:23:31 +02002352 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_AUTO)
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002353 ath10k_info("limiting irq mode to: %d\n", ath10k_pci_irq_mode);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002354
2355 /* Try MSI-X */
Michal Kazior0edf2572014-08-07 11:03:29 +02002356 if (ath10k_pci_irq_mode == ATH10K_PCI_IRQ_AUTO) {
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002357 ar_pci->num_msi_intrs = MSI_NUM_REQUEST;
Alexander Gordeev5ad68672014-02-13 17:50:02 +02002358 ret = pci_enable_msi_range(ar_pci->pdev, ar_pci->num_msi_intrs,
2359 ar_pci->num_msi_intrs);
2360 if (ret > 0)
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002361 return 0;
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002362
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002363 /* fall-through */
2364 }
2365
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002366 /* Try MSI */
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002367 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_LEGACY) {
2368 ar_pci->num_msi_intrs = 1;
2369 ret = pci_enable_msi(ar_pci->pdev);
2370 if (ret == 0)
2371 return 0;
2372
2373 /* fall-through */
2374 }
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002375
2376 /* Try legacy irq
2377 *
2378 * A potential race occurs here: The CORE_BASE write
2379 * depends on target correctly decoding AXI address but
2380 * host won't know when target writes BAR to CORE_CTRL.
2381 * This write might get lost if target has NOT written BAR.
2382 * For now, fix the race by repeating the write in below
2383 * synchronization checking. */
2384 ar_pci->num_msi_intrs = 0;
2385
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002386 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2387 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002388
2389 return 0;
2390}
2391
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002392static void ath10k_pci_deinit_irq_legacy(struct ath10k *ar)
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002393{
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002394 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2395 0);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002396}
2397
2398static int ath10k_pci_deinit_irq(struct ath10k *ar)
2399{
2400 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2401
2402 switch (ar_pci->num_msi_intrs) {
2403 case 0:
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002404 ath10k_pci_deinit_irq_legacy(ar);
2405 return 0;
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002406 case 1:
2407 /* fall-through */
2408 case MSI_NUM_REQUEST:
2409 pci_disable_msi(ar_pci->pdev);
2410 return 0;
Alexander Gordeevbb8b6212014-02-13 17:50:01 +02002411 default:
2412 pci_disable_msi(ar_pci->pdev);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002413 }
2414
2415 ath10k_warn("unknown irq configuration upon deinit\n");
2416 return -EINVAL;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002417}
2418
Michal Kaziord7fb47f2013-11-08 08:01:26 +01002419static int ath10k_pci_wait_for_target_init(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002420{
2421 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo0399eca2014-03-28 09:32:21 +02002422 unsigned long timeout;
Kalle Valo0399eca2014-03-28 09:32:21 +02002423 u32 val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002424
Kalle Valo50f87a62014-03-28 09:32:52 +02002425 ath10k_dbg(ATH10K_DBG_BOOT, "boot waiting target to initialise\n");
2426
Kalle Valo0399eca2014-03-28 09:32:21 +02002427 timeout = jiffies + msecs_to_jiffies(ATH10K_PCI_TARGET_WAIT);
2428
2429 do {
2430 val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
2431
Kalle Valo50f87a62014-03-28 09:32:52 +02002432 ath10k_dbg(ATH10K_DBG_BOOT, "boot target indicator %x\n", val);
2433
Kalle Valo0399eca2014-03-28 09:32:21 +02002434 /* target should never return this */
2435 if (val == 0xffffffff)
2436 continue;
2437
Michal Kazior7710cd22014-04-23 19:30:04 +03002438 /* the device has crashed so don't bother trying anymore */
2439 if (val & FW_IND_EVENT_PENDING)
2440 break;
2441
Kalle Valo0399eca2014-03-28 09:32:21 +02002442 if (val & FW_IND_INITIALIZED)
2443 break;
2444
Kalle Valo5e3dd152013-06-12 20:52:10 +03002445 if (ar_pci->num_msi_intrs == 0)
2446 /* Fix potential race by repeating CORE_BASE writes */
Michal Kaziorc947a9e2014-08-22 14:23:30 +02002447 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
2448 PCIE_INTR_ENABLE_ADDRESS,
2449 PCIE_INTR_FIRMWARE_MASK |
2450 PCIE_INTR_CE_MASK_ALL);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002451
Kalle Valo0399eca2014-03-28 09:32:21 +02002452 mdelay(10);
2453 } while (time_before(jiffies, timeout));
2454
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002455 if (val == 0xffffffff) {
2456 ath10k_err("failed to read device register, device is gone\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002457 return -EIO;
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002458 }
2459
Michal Kazior7710cd22014-04-23 19:30:04 +03002460 if (val & FW_IND_EVENT_PENDING) {
2461 ath10k_warn("device has crashed during init\n");
Michal Kazior1a4ab282014-05-14 16:56:16 +03002462 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS,
2463 val & ~FW_IND_EVENT_PENDING);
Kalle Valo0e9848c2014-08-25 08:37:37 +03002464 ath10k_pci_fw_crashed_dump(ar);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002465 return -ECOMM;
Michal Kazior7710cd22014-04-23 19:30:04 +03002466 }
2467
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002468 if (!(val & FW_IND_INITIALIZED)) {
Kalle Valo0399eca2014-03-28 09:32:21 +02002469 ath10k_err("failed to receive initialized event from target: %08x\n",
2470 val);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002471 return -ETIMEDOUT;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002472 }
2473
Kalle Valo50f87a62014-03-28 09:32:52 +02002474 ath10k_dbg(ATH10K_DBG_BOOT, "boot target initialised\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002475 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002476}
2477
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002478static int ath10k_pci_cold_reset(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002479{
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002480 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002481 u32 val;
2482
Kalle Valo50f87a62014-03-28 09:32:52 +02002483 ath10k_dbg(ATH10K_DBG_BOOT, "boot cold reset\n");
2484
Kalle Valo5e3dd152013-06-12 20:52:10 +03002485 /* Put Target, including PCIe, into RESET. */
Kalle Valoe479ed42013-09-01 10:01:53 +03002486 val = ath10k_pci_reg_read32(ar, SOC_GLOBAL_RESET_ADDRESS);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002487 val |= 1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002488 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002489
2490 for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
Kalle Valoe479ed42013-09-01 10:01:53 +03002491 if (ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03002492 RTC_STATE_COLD_RESET_MASK)
2493 break;
2494 msleep(1);
2495 }
2496
2497 /* Pull Target, including PCIe, out of RESET. */
2498 val &= ~1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002499 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002500
2501 for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
Kalle Valoe479ed42013-09-01 10:01:53 +03002502 if (!(ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03002503 RTC_STATE_COLD_RESET_MASK))
2504 break;
2505 msleep(1);
2506 }
2507
Kalle Valo50f87a62014-03-28 09:32:52 +02002508 ath10k_dbg(ATH10K_DBG_BOOT, "boot cold reset complete\n");
2509
Michal Kazior5b2589f2013-11-08 08:01:30 +01002510 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002511}
2512
Michal Kazior2986e3e2014-08-07 11:03:30 +02002513static int ath10k_pci_claim(struct ath10k *ar)
2514{
2515 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2516 struct pci_dev *pdev = ar_pci->pdev;
2517 u32 lcr_val;
2518 int ret;
2519
2520 pci_set_drvdata(pdev, ar);
2521
2522 ret = pci_enable_device(pdev);
2523 if (ret) {
2524 ath10k_err("failed to enable pci device: %d\n", ret);
2525 return ret;
2526 }
2527
2528 ret = pci_request_region(pdev, BAR_NUM, "ath");
2529 if (ret) {
2530 ath10k_err("failed to request region BAR%d: %d\n", BAR_NUM,
2531 ret);
2532 goto err_device;
2533 }
2534
2535 /* Target expects 32 bit DMA. Enforce it. */
2536 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2537 if (ret) {
2538 ath10k_err("failed to set dma mask to 32-bit: %d\n", ret);
2539 goto err_region;
2540 }
2541
2542 ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2543 if (ret) {
2544 ath10k_err("failed to set consistent dma mask to 32-bit: %d\n",
2545 ret);
2546 goto err_region;
2547 }
2548
2549 pci_set_master(pdev);
2550
2551 /* Workaround: Disable ASPM */
2552 pci_read_config_dword(pdev, 0x80, &lcr_val);
2553 pci_write_config_dword(pdev, 0x80, (lcr_val & 0xffffff00));
2554
2555 /* Arrange for access to Target SoC registers. */
2556 ar_pci->mem = pci_iomap(pdev, BAR_NUM, 0);
2557 if (!ar_pci->mem) {
2558 ath10k_err("failed to iomap BAR%d\n", BAR_NUM);
2559 ret = -EIO;
2560 goto err_master;
2561 }
2562
2563 ath10k_dbg(ATH10K_DBG_BOOT, "boot pci_mem 0x%p\n", ar_pci->mem);
2564 return 0;
2565
2566err_master:
2567 pci_clear_master(pdev);
2568
2569err_region:
2570 pci_release_region(pdev, BAR_NUM);
2571
2572err_device:
2573 pci_disable_device(pdev);
2574
2575 return ret;
2576}
2577
2578static void ath10k_pci_release(struct ath10k *ar)
2579{
2580 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2581 struct pci_dev *pdev = ar_pci->pdev;
2582
2583 pci_iounmap(pdev, ar_pci->mem);
2584 pci_release_region(pdev, BAR_NUM);
2585 pci_clear_master(pdev);
2586 pci_disable_device(pdev);
2587}
2588
Kalle Valo5e3dd152013-06-12 20:52:10 +03002589static int ath10k_pci_probe(struct pci_dev *pdev,
2590 const struct pci_device_id *pci_dev)
2591{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002592 int ret = 0;
2593 struct ath10k *ar;
2594 struct ath10k_pci *ar_pci;
Michal Kazior2986e3e2014-08-07 11:03:30 +02002595 u32 chip_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002596
Kalle Valo50f87a62014-03-28 09:32:52 +02002597 ath10k_dbg(ATH10K_DBG_PCI, "pci probe\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002598
Michal Kaziore7b54192014-08-07 11:03:27 +02002599 ar = ath10k_core_create(sizeof(*ar_pci), &pdev->dev,
2600 &ath10k_pci_hif_ops);
2601 if (!ar) {
2602 ath10k_err("failed to allocate core\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002603 return -ENOMEM;
Michal Kaziore7b54192014-08-07 11:03:27 +02002604 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002605
Michal Kaziore7b54192014-08-07 11:03:27 +02002606 ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002607 ar_pci->pdev = pdev;
2608 ar_pci->dev = &pdev->dev;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002609 ar_pci->ar = ar;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002610
Michal Kazior2986e3e2014-08-07 11:03:30 +02002611 spin_lock_init(&ar_pci->ce_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002612
Michal Kazior2986e3e2014-08-07 11:03:30 +02002613 ret = ath10k_pci_claim(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002614 if (ret) {
Michal Kazior2986e3e2014-08-07 11:03:30 +02002615 ath10k_err("failed to claim device: %d\n", ret);
Michal Kaziore7b54192014-08-07 11:03:27 +02002616 goto err_core_destroy;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002617 }
2618
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002619 ret = ath10k_pci_wake(ar);
Kalle Valoe01ae682013-09-01 11:22:14 +03002620 if (ret) {
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002621 ath10k_err("failed to wake up: %d\n", ret);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002622 goto err_release;
Kalle Valoe01ae682013-09-01 11:22:14 +03002623 }
2624
Kalle Valo233eb972013-10-16 16:46:11 +03002625 chip_id = ath10k_pci_soc_read32(ar, SOC_CHIP_ID_ADDRESS);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002626 if (chip_id == 0xffffffff) {
2627 ath10k_err("failed to get chip id\n");
2628 goto err_sleep;
2629 }
Kalle Valoe01ae682013-09-01 11:22:14 +03002630
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002631 ret = ath10k_pci_alloc_ce(ar);
2632 if (ret) {
2633 ath10k_err("failed to allocate copy engine pipes: %d\n", ret);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002634 goto err_sleep;
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002635 }
2636
Michal Kazior403d6272014-08-22 14:23:31 +02002637 ath10k_pci_ce_deinit(ar);
2638
2639 ret = ath10k_ce_disable_interrupts(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002640 if (ret) {
Michal Kazior403d6272014-08-22 14:23:31 +02002641 ath10k_err("failed to disable copy engine interrupts: %d\n",
2642 ret);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002643 goto err_free_ce;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002644 }
2645
Michal Kazior403d6272014-08-22 14:23:31 +02002646 ret = ath10k_pci_init_irq(ar);
2647 if (ret) {
2648 ath10k_err("failed to init irqs: %d\n", ret);
2649 goto err_free_ce;
2650 }
2651
2652 ath10k_info("pci irq %s interrupts %d irq_mode %d reset_mode %d\n",
2653 ath10k_pci_get_irq_method(ar), ar_pci->num_msi_intrs,
2654 ath10k_pci_irq_mode, ath10k_pci_reset_mode);
2655
2656 ret = ath10k_core_register(ar, chip_id);
2657 if (ret) {
2658 ath10k_err("failed to register driver core: %d\n", ret);
2659 goto err_deinit_irq;
2660 }
2661
Kalle Valo5e3dd152013-06-12 20:52:10 +03002662 return 0;
2663
Michal Kazior403d6272014-08-22 14:23:31 +02002664err_deinit_irq:
2665 ath10k_pci_deinit_irq(ar);
2666
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002667err_free_ce:
2668 ath10k_pci_free_ce(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002669
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002670err_sleep:
2671 ath10k_pci_sleep(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002672
2673err_release:
2674 ath10k_pci_release(ar);
2675
Michal Kaziore7b54192014-08-07 11:03:27 +02002676err_core_destroy:
Kalle Valo5e3dd152013-06-12 20:52:10 +03002677 ath10k_core_destroy(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002678
2679 return ret;
2680}
2681
2682static void ath10k_pci_remove(struct pci_dev *pdev)
2683{
2684 struct ath10k *ar = pci_get_drvdata(pdev);
2685 struct ath10k_pci *ar_pci;
2686
Kalle Valo50f87a62014-03-28 09:32:52 +02002687 ath10k_dbg(ATH10K_DBG_PCI, "pci remove\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002688
2689 if (!ar)
2690 return;
2691
2692 ar_pci = ath10k_pci_priv(ar);
2693
2694 if (!ar_pci)
2695 return;
2696
Kalle Valo5e3dd152013-06-12 20:52:10 +03002697 ath10k_core_unregister(ar);
Michal Kazior403d6272014-08-22 14:23:31 +02002698 ath10k_pci_deinit_irq(ar);
2699 ath10k_pci_ce_deinit(ar);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002700 ath10k_pci_free_ce(ar);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002701 ath10k_pci_sleep(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002702 ath10k_pci_release(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002703 ath10k_core_destroy(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002704}
2705
Kalle Valo5e3dd152013-06-12 20:52:10 +03002706MODULE_DEVICE_TABLE(pci, ath10k_pci_id_table);
2707
2708static struct pci_driver ath10k_pci_driver = {
2709 .name = "ath10k_pci",
2710 .id_table = ath10k_pci_id_table,
2711 .probe = ath10k_pci_probe,
2712 .remove = ath10k_pci_remove,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002713};
2714
2715static int __init ath10k_pci_init(void)
2716{
2717 int ret;
2718
2719 ret = pci_register_driver(&ath10k_pci_driver);
2720 if (ret)
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002721 ath10k_err("failed to register PCI driver: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002722
2723 return ret;
2724}
2725module_init(ath10k_pci_init);
2726
2727static void __exit ath10k_pci_exit(void)
2728{
2729 pci_unregister_driver(&ath10k_pci_driver);
2730}
2731
2732module_exit(ath10k_pci_exit);
2733
2734MODULE_AUTHOR("Qualcomm Atheros");
2735MODULE_DESCRIPTION("Driver support for Atheros QCA988X PCIe devices");
2736MODULE_LICENSE("Dual BSD/GPL");
Michal Kazior24c88f72014-07-25 13:32:17 +02002737MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_FW_3_FILE);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002738MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_BOARD_DATA_FILE);