blob: b4ae4bb3fc09596747603769bd7b0e457b0ab45d [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
Kalle Valo5e3dd152013-06-12 20:52:10 +0300309/*
310 * Diagnostic read/write access is provided for startup/config/debug usage.
311 * Caller must guarantee proper alignment, when applicable, and single user
312 * at any moment.
313 */
314static int ath10k_pci_diag_read_mem(struct ath10k *ar, u32 address, void *data,
315 int nbytes)
316{
317 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
318 int ret = 0;
319 u32 buf;
320 unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
321 unsigned int id;
322 unsigned int flags;
Michal Kazior2aa39112013-08-27 13:08:02 +0200323 struct ath10k_ce_pipe *ce_diag;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300324 /* Host buffer address in CE space */
325 u32 ce_data;
326 dma_addr_t ce_data_base = 0;
327 void *data_buf = NULL;
328 int i;
329
330 /*
331 * This code cannot handle reads to non-memory space. Redirect to the
332 * register read fn but preserve the multi word read capability of
333 * this fn
334 */
335 if (address < DRAM_BASE_ADDRESS) {
336 if (!IS_ALIGNED(address, 4) ||
337 !IS_ALIGNED((unsigned long)data, 4))
338 return -EIO;
339
340 while ((nbytes >= 4) && ((ret = ath10k_pci_diag_read_access(
341 ar, address, (u32 *)data)) == 0)) {
342 nbytes -= sizeof(u32);
343 address += sizeof(u32);
344 data += sizeof(u32);
345 }
346 return ret;
347 }
348
349 ce_diag = ar_pci->ce_diag;
350
351 /*
352 * Allocate a temporary bounce buffer to hold caller's data
353 * to be DMA'ed from Target. This guarantees
354 * 1) 4-byte alignment
355 * 2) Buffer in DMA-able space
356 */
357 orig_nbytes = nbytes;
Michal Kazior68c03242014-03-28 10:02:35 +0200358 data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
359 orig_nbytes,
360 &ce_data_base,
361 GFP_ATOMIC);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300362
363 if (!data_buf) {
364 ret = -ENOMEM;
365 goto done;
366 }
367 memset(data_buf, 0, orig_nbytes);
368
369 remaining_bytes = orig_nbytes;
370 ce_data = ce_data_base;
371 while (remaining_bytes) {
372 nbytes = min_t(unsigned int, remaining_bytes,
373 DIAG_TRANSFER_LIMIT);
374
375 ret = ath10k_ce_recv_buf_enqueue(ce_diag, NULL, ce_data);
376 if (ret != 0)
377 goto done;
378
379 /* Request CE to send from Target(!) address to Host buffer */
380 /*
381 * The address supplied by the caller is in the
382 * Target CPU virtual address space.
383 *
384 * In order to use this address with the diagnostic CE,
385 * convert it from Target CPU virtual address space
386 * to CE address space
387 */
Kalle Valo5e3dd152013-06-12 20:52:10 +0300388 address = TARG_CPU_SPACE_TO_CE_SPACE(ar, ar_pci->mem,
389 address);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300390
391 ret = ath10k_ce_send(ce_diag, NULL, (u32)address, nbytes, 0,
392 0);
393 if (ret)
394 goto done;
395
396 i = 0;
397 while (ath10k_ce_completed_send_next(ce_diag, NULL, &buf,
398 &completed_nbytes,
399 &id) != 0) {
400 mdelay(1);
401 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
402 ret = -EBUSY;
403 goto done;
404 }
405 }
406
407 if (nbytes != completed_nbytes) {
408 ret = -EIO;
409 goto done;
410 }
411
412 if (buf != (u32) address) {
413 ret = -EIO;
414 goto done;
415 }
416
417 i = 0;
418 while (ath10k_ce_completed_recv_next(ce_diag, NULL, &buf,
419 &completed_nbytes,
420 &id, &flags) != 0) {
421 mdelay(1);
422
423 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
424 ret = -EBUSY;
425 goto done;
426 }
427 }
428
429 if (nbytes != completed_nbytes) {
430 ret = -EIO;
431 goto done;
432 }
433
434 if (buf != ce_data) {
435 ret = -EIO;
436 goto done;
437 }
438
439 remaining_bytes -= nbytes;
440 address += nbytes;
441 ce_data += nbytes;
442 }
443
444done:
445 if (ret == 0) {
446 /* Copy data from allocated DMA buf to caller's buf */
447 WARN_ON_ONCE(orig_nbytes & 3);
448 for (i = 0; i < orig_nbytes / sizeof(__le32); i++) {
449 ((u32 *)data)[i] =
450 __le32_to_cpu(((__le32 *)data_buf)[i]);
451 }
452 } else
Kalle Valo50f87a62014-03-28 09:32:52 +0200453 ath10k_warn("failed to read diag value at 0x%x: %d\n",
454 address, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300455
456 if (data_buf)
Michal Kazior68c03242014-03-28 10:02:35 +0200457 dma_free_coherent(ar->dev, orig_nbytes, data_buf,
458 ce_data_base);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300459
460 return ret;
461}
462
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300463static int ath10k_pci_diag_read32(struct ath10k *ar, u32 address, u32 *value)
464{
465 return ath10k_pci_diag_read_mem(ar, address, value, sizeof(u32));
466}
467
468static int __ath10k_pci_diag_read_hi(struct ath10k *ar, void *dest,
469 u32 src, u32 len)
470{
471 u32 host_addr, addr;
472 int ret;
473
474 host_addr = host_interest_item_address(src);
475
476 ret = ath10k_pci_diag_read32(ar, host_addr, &addr);
477 if (ret != 0) {
478 ath10k_warn("failed to get memcpy hi address for firmware address %d: %d\n",
479 src, ret);
480 return ret;
481 }
482
483 ret = ath10k_pci_diag_read_mem(ar, addr, dest, len);
484 if (ret != 0) {
485 ath10k_warn("failed to memcpy firmware memory from %d (%d B): %d\n",
486 addr, len, ret);
487 return ret;
488 }
489
490 return 0;
491}
492
493#define ath10k_pci_diag_read_hi(ar, dest, src, len) \
494 __ath10k_pci_diag_read_hi(ar, dest, HI_ITEM(src), len);
495
Kalle Valo5e3dd152013-06-12 20:52:10 +0300496/* Read 4-byte aligned data from Target memory or register */
497static int ath10k_pci_diag_read_access(struct ath10k *ar, u32 address,
498 u32 *data)
499{
500 /* Assume range doesn't cross this boundary */
501 if (address >= DRAM_BASE_ADDRESS)
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300502 return ath10k_pci_diag_read32(ar, address, data);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300503
Kalle Valo5e3dd152013-06-12 20:52:10 +0300504 *data = ath10k_pci_read32(ar, address);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300505 return 0;
506}
507
508static int ath10k_pci_diag_write_mem(struct ath10k *ar, u32 address,
509 const void *data, int nbytes)
510{
511 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
512 int ret = 0;
513 u32 buf;
514 unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
515 unsigned int id;
516 unsigned int flags;
Michal Kazior2aa39112013-08-27 13:08:02 +0200517 struct ath10k_ce_pipe *ce_diag;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300518 void *data_buf = NULL;
519 u32 ce_data; /* Host buffer address in CE space */
520 dma_addr_t ce_data_base = 0;
521 int i;
522
523 ce_diag = ar_pci->ce_diag;
524
525 /*
526 * Allocate a temporary bounce buffer to hold caller's data
527 * to be DMA'ed to Target. This guarantees
528 * 1) 4-byte alignment
529 * 2) Buffer in DMA-able space
530 */
531 orig_nbytes = nbytes;
Michal Kazior68c03242014-03-28 10:02:35 +0200532 data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
533 orig_nbytes,
534 &ce_data_base,
535 GFP_ATOMIC);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300536 if (!data_buf) {
537 ret = -ENOMEM;
538 goto done;
539 }
540
541 /* Copy caller's data to allocated DMA buf */
542 WARN_ON_ONCE(orig_nbytes & 3);
543 for (i = 0; i < orig_nbytes / sizeof(__le32); i++)
544 ((__le32 *)data_buf)[i] = __cpu_to_le32(((u32 *)data)[i]);
545
546 /*
547 * The address supplied by the caller is in the
548 * Target CPU virtual address space.
549 *
550 * In order to use this address with the diagnostic CE,
551 * convert it from
552 * Target CPU virtual address space
553 * to
554 * CE address space
555 */
Kalle Valo5e3dd152013-06-12 20:52:10 +0300556 address = TARG_CPU_SPACE_TO_CE_SPACE(ar, ar_pci->mem, address);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300557
558 remaining_bytes = orig_nbytes;
559 ce_data = ce_data_base;
560 while (remaining_bytes) {
561 /* FIXME: check cast */
562 nbytes = min_t(int, remaining_bytes, DIAG_TRANSFER_LIMIT);
563
564 /* Set up to receive directly into Target(!) address */
565 ret = ath10k_ce_recv_buf_enqueue(ce_diag, NULL, address);
566 if (ret != 0)
567 goto done;
568
569 /*
570 * Request CE to send caller-supplied data that
571 * was copied to bounce buffer to Target(!) address.
572 */
573 ret = ath10k_ce_send(ce_diag, NULL, (u32) ce_data,
574 nbytes, 0, 0);
575 if (ret != 0)
576 goto done;
577
578 i = 0;
579 while (ath10k_ce_completed_send_next(ce_diag, NULL, &buf,
580 &completed_nbytes,
581 &id) != 0) {
582 mdelay(1);
583
584 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
585 ret = -EBUSY;
586 goto done;
587 }
588 }
589
590 if (nbytes != completed_nbytes) {
591 ret = -EIO;
592 goto done;
593 }
594
595 if (buf != ce_data) {
596 ret = -EIO;
597 goto done;
598 }
599
600 i = 0;
601 while (ath10k_ce_completed_recv_next(ce_diag, NULL, &buf,
602 &completed_nbytes,
603 &id, &flags) != 0) {
604 mdelay(1);
605
606 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
607 ret = -EBUSY;
608 goto done;
609 }
610 }
611
612 if (nbytes != completed_nbytes) {
613 ret = -EIO;
614 goto done;
615 }
616
617 if (buf != address) {
618 ret = -EIO;
619 goto done;
620 }
621
622 remaining_bytes -= nbytes;
623 address += nbytes;
624 ce_data += nbytes;
625 }
626
627done:
628 if (data_buf) {
Michal Kazior68c03242014-03-28 10:02:35 +0200629 dma_free_coherent(ar->dev, orig_nbytes, data_buf,
630 ce_data_base);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300631 }
632
633 if (ret != 0)
Kalle Valo50f87a62014-03-28 09:32:52 +0200634 ath10k_warn("failed to write diag value at 0x%x: %d\n",
635 address, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300636
637 return ret;
638}
639
640/* Write 4B data to Target memory or register */
641static int ath10k_pci_diag_write_access(struct ath10k *ar, u32 address,
642 u32 data)
643{
644 /* Assume range doesn't cross this boundary */
645 if (address >= DRAM_BASE_ADDRESS)
646 return ath10k_pci_diag_write_mem(ar, address, &data,
647 sizeof(u32));
648
Kalle Valo5e3dd152013-06-12 20:52:10 +0300649 ath10k_pci_write32(ar, address, data);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300650 return 0;
651}
652
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200653static bool ath10k_pci_is_awake(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300654{
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200655 u32 val = ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS);
656
657 return RTC_STATE_V_GET(val) == RTC_STATE_V_ON;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300658}
659
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200660static int ath10k_pci_wake_wait(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300661{
Kalle Valo5e3dd152013-06-12 20:52:10 +0300662 int tot_delay = 0;
663 int curr_delay = 5;
664
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200665 while (tot_delay < PCIE_WAKE_TIMEOUT) {
666 if (ath10k_pci_is_awake(ar))
Kalle Valo3aebe542013-09-01 10:02:07 +0300667 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300668
669 udelay(curr_delay);
670 tot_delay += curr_delay;
671
672 if (curr_delay < 50)
673 curr_delay += 5;
674 }
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200675
676 return -ETIMEDOUT;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300677}
678
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200679static int ath10k_pci_wake(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300680{
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200681 ath10k_pci_reg_write32(ar, PCIE_SOC_WAKE_ADDRESS,
682 PCIE_SOC_WAKE_V_MASK);
683 return ath10k_pci_wake_wait(ar);
684}
Kalle Valo5e3dd152013-06-12 20:52:10 +0300685
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200686static void ath10k_pci_sleep(struct ath10k *ar)
687{
688 ath10k_pci_reg_write32(ar, PCIE_SOC_WAKE_ADDRESS,
689 PCIE_SOC_WAKE_RESET);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300690}
691
Kalle Valo5e3dd152013-06-12 20:52:10 +0300692/* Called by lower (CE) layer when a send to Target completes. */
Michal Kazior5440ce22013-09-03 15:09:58 +0200693static void ath10k_pci_ce_send_done(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300694{
695 struct ath10k *ar = ce_state->ar;
696 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2f5280d2014-02-27 18:50:05 +0200697 struct ath10k_hif_cb *cb = &ar_pci->msg_callbacks_current;
Michal Kazior5440ce22013-09-03 15:09:58 +0200698 void *transfer_context;
699 u32 ce_data;
700 unsigned int nbytes;
701 unsigned int transfer_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300702
Michal Kazior5440ce22013-09-03 15:09:58 +0200703 while (ath10k_ce_completed_send_next(ce_state, &transfer_context,
704 &ce_data, &nbytes,
705 &transfer_id) == 0) {
Michal Kaziora16942e2014-02-27 18:50:04 +0200706 /* no need to call tx completion for NULL pointers */
Michal Kazior726346f2014-02-27 18:50:04 +0200707 if (transfer_context == NULL)
708 continue;
709
Michal Kazior2f5280d2014-02-27 18:50:05 +0200710 cb->tx_completion(ar, transfer_context, transfer_id);
Michal Kazior5440ce22013-09-03 15:09:58 +0200711 }
Kalle Valo5e3dd152013-06-12 20:52:10 +0300712}
713
714/* Called by lower (CE) layer when data is received from the Target. */
Michal Kazior5440ce22013-09-03 15:09:58 +0200715static void ath10k_pci_ce_recv_data(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300716{
717 struct ath10k *ar = ce_state->ar;
718 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +0200719 struct ath10k_pci_pipe *pipe_info = &ar_pci->pipe_info[ce_state->id];
Michal Kazior2f5280d2014-02-27 18:50:05 +0200720 struct ath10k_hif_cb *cb = &ar_pci->msg_callbacks_current;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300721 struct sk_buff *skb;
Michal Kazior5440ce22013-09-03 15:09:58 +0200722 void *transfer_context;
723 u32 ce_data;
Michal Kazior2f5280d2014-02-27 18:50:05 +0200724 unsigned int nbytes, max_nbytes;
Michal Kazior5440ce22013-09-03 15:09:58 +0200725 unsigned int transfer_id;
726 unsigned int flags;
Michal Kaziorc29a3802014-07-21 21:03:10 +0300727 int err, num_replenish = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300728
Michal Kazior5440ce22013-09-03 15:09:58 +0200729 while (ath10k_ce_completed_recv_next(ce_state, &transfer_context,
730 &ce_data, &nbytes, &transfer_id,
731 &flags) == 0) {
Michal Kaziorc29a3802014-07-21 21:03:10 +0300732 num_replenish++;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300733 skb = transfer_context;
Michal Kazior2f5280d2014-02-27 18:50:05 +0200734 max_nbytes = skb->len + skb_tailroom(skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300735 dma_unmap_single(ar->dev, ATH10K_SKB_CB(skb)->paddr,
Michal Kazior2f5280d2014-02-27 18:50:05 +0200736 max_nbytes, DMA_FROM_DEVICE);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300737
Michal Kazior2f5280d2014-02-27 18:50:05 +0200738 if (unlikely(max_nbytes < nbytes)) {
739 ath10k_warn("rxed more than expected (nbytes %d, max %d)",
740 nbytes, max_nbytes);
741 dev_kfree_skb_any(skb);
742 continue;
743 }
744
745 skb_put(skb, nbytes);
746 cb->rx_completion(ar, skb, pipe_info->pipe_num);
747 }
Michal Kaziorc29a3802014-07-21 21:03:10 +0300748
749 err = ath10k_pci_post_rx_pipe(pipe_info, num_replenish);
750 if (unlikely(err)) {
751 /* FIXME: retry */
752 ath10k_warn("failed to replenish CE rx ring %d (%d bufs): %d\n",
753 pipe_info->pipe_num, num_replenish, err);
754 }
Kalle Valo5e3dd152013-06-12 20:52:10 +0300755}
756
Michal Kazior726346f2014-02-27 18:50:04 +0200757static int ath10k_pci_hif_tx_sg(struct ath10k *ar, u8 pipe_id,
758 struct ath10k_hif_sg_item *items, int n_items)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300759{
Kalle Valo5e3dd152013-06-12 20:52:10 +0300760 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior726346f2014-02-27 18:50:04 +0200761 struct ath10k_pci_pipe *pci_pipe = &ar_pci->pipe_info[pipe_id];
762 struct ath10k_ce_pipe *ce_pipe = pci_pipe->ce_hdl;
763 struct ath10k_ce_ring *src_ring = ce_pipe->src_ring;
Michal Kazior7147a132014-05-26 12:02:58 +0200764 unsigned int nentries_mask;
765 unsigned int sw_index;
766 unsigned int write_index;
Michal Kazior08b8aa02014-05-26 12:02:59 +0200767 int err, i = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300768
Michal Kazior726346f2014-02-27 18:50:04 +0200769 spin_lock_bh(&ar_pci->ce_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300770
Michal Kazior7147a132014-05-26 12:02:58 +0200771 nentries_mask = src_ring->nentries_mask;
772 sw_index = src_ring->sw_index;
773 write_index = src_ring->write_index;
774
Michal Kazior726346f2014-02-27 18:50:04 +0200775 if (unlikely(CE_RING_DELTA(nentries_mask,
776 write_index, sw_index - 1) < n_items)) {
777 err = -ENOBUFS;
Michal Kazior08b8aa02014-05-26 12:02:59 +0200778 goto err;
Michal Kazior726346f2014-02-27 18:50:04 +0200779 }
780
781 for (i = 0; i < n_items - 1; i++) {
782 ath10k_dbg(ATH10K_DBG_PCI,
783 "pci tx item %d paddr 0x%08x len %d n_items %d\n",
784 i, items[i].paddr, items[i].len, n_items);
785 ath10k_dbg_dump(ATH10K_DBG_PCI_DUMP, NULL, "item data: ",
786 items[i].vaddr, items[i].len);
787
788 err = ath10k_ce_send_nolock(ce_pipe,
789 items[i].transfer_context,
790 items[i].paddr,
791 items[i].len,
792 items[i].transfer_id,
793 CE_SEND_FLAG_GATHER);
794 if (err)
Michal Kazior08b8aa02014-05-26 12:02:59 +0200795 goto err;
Michal Kazior726346f2014-02-27 18:50:04 +0200796 }
797
798 /* `i` is equal to `n_items -1` after for() */
Kalle Valo5e3dd152013-06-12 20:52:10 +0300799
800 ath10k_dbg(ATH10K_DBG_PCI,
Michal Kazior726346f2014-02-27 18:50:04 +0200801 "pci tx item %d paddr 0x%08x len %d n_items %d\n",
802 i, items[i].paddr, items[i].len, n_items);
803 ath10k_dbg_dump(ATH10K_DBG_PCI_DUMP, NULL, "item data: ",
804 items[i].vaddr, items[i].len);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300805
Michal Kazior726346f2014-02-27 18:50:04 +0200806 err = ath10k_ce_send_nolock(ce_pipe,
807 items[i].transfer_context,
808 items[i].paddr,
809 items[i].len,
810 items[i].transfer_id,
811 0);
812 if (err)
Michal Kazior08b8aa02014-05-26 12:02:59 +0200813 goto err;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300814
Michal Kazior08b8aa02014-05-26 12:02:59 +0200815 spin_unlock_bh(&ar_pci->ce_lock);
816 return 0;
817
818err:
819 for (; i > 0; i--)
820 __ath10k_ce_send_revert(ce_pipe);
821
Michal Kazior726346f2014-02-27 18:50:04 +0200822 spin_unlock_bh(&ar_pci->ce_lock);
823 return err;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300824}
825
826static u16 ath10k_pci_hif_get_free_queue_number(struct ath10k *ar, u8 pipe)
827{
828 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo50f87a62014-03-28 09:32:52 +0200829
830 ath10k_dbg(ATH10K_DBG_PCI, "pci hif get free queue number\n");
831
Michal Kazior3efcb3b2013-10-02 11:03:41 +0200832 return ath10k_ce_num_free_src_entries(ar_pci->pipe_info[pipe].ce_hdl);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300833}
834
835static void ath10k_pci_hif_dump_area(struct ath10k *ar)
836{
Kalle Valo5e3dd152013-06-12 20:52:10 +0300837 u32 reg_dump_values[REG_DUMP_COUNT_QCA988X] = {};
Kalle Valo5e3dd152013-06-12 20:52:10 +0300838 int ret;
839 u32 i;
840
841 ath10k_err("firmware crashed!\n");
842 ath10k_err("hardware name %s version 0x%x\n",
843 ar->hw_params.name, ar->target_version);
Chun-Yeow Yeoh5ba88b32014-01-21 17:21:21 +0800844 ath10k_err("firmware version: %s\n", ar->hw->wiphy->fw_version);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300845
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300846 ret = ath10k_pci_diag_read_hi(ar, &reg_dump_values[0],
847 hi_failure_state,
848 REG_DUMP_COUNT_QCA988X * sizeof(u32));
Michal Kazior1d2b48d2013-11-08 08:01:34 +0100849 if (ret) {
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300850 ath10k_err("failed to read firmware dump area: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300851 return;
852 }
853
854 BUILD_BUG_ON(REG_DUMP_COUNT_QCA988X % 4);
855
856 ath10k_err("target Register Dump\n");
857 for (i = 0; i < REG_DUMP_COUNT_QCA988X; i += 4)
858 ath10k_err("[%02d]: 0x%08X 0x%08X 0x%08X 0x%08X\n",
859 i,
860 reg_dump_values[i],
861 reg_dump_values[i + 1],
862 reg_dump_values[i + 2],
863 reg_dump_values[i + 3]);
Michal Kazioraffd3212013-07-16 09:54:35 +0200864
Michal Kazior5e90de82013-10-16 16:46:05 +0300865 queue_work(ar->workqueue, &ar->restart_work);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300866}
867
868static void ath10k_pci_hif_send_complete_check(struct ath10k *ar, u8 pipe,
869 int force)
870{
Kalle Valo50f87a62014-03-28 09:32:52 +0200871 ath10k_dbg(ATH10K_DBG_PCI, "pci hif send complete check\n");
872
Kalle Valo5e3dd152013-06-12 20:52:10 +0300873 if (!force) {
874 int resources;
875 /*
876 * Decide whether to actually poll for completions, or just
877 * wait for a later chance.
878 * If there seem to be plenty of resources left, then just wait
879 * since checking involves reading a CE register, which is a
880 * relatively expensive operation.
881 */
882 resources = ath10k_pci_hif_get_free_queue_number(ar, pipe);
883
884 /*
885 * If at least 50% of the total resources are still available,
886 * don't bother checking again yet.
887 */
888 if (resources > (host_ce_config_wlan[pipe].src_nentries >> 1))
889 return;
890 }
891 ath10k_ce_per_engine_service(ar, pipe);
892}
893
Michal Kaziore799bbf2013-07-05 16:15:12 +0300894static void ath10k_pci_hif_set_callbacks(struct ath10k *ar,
895 struct ath10k_hif_cb *callbacks)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300896{
897 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
898
Kalle Valo50f87a62014-03-28 09:32:52 +0200899 ath10k_dbg(ATH10K_DBG_PCI, "pci hif set callbacks\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +0300900
901 memcpy(&ar_pci->msg_callbacks_current, callbacks,
902 sizeof(ar_pci->msg_callbacks_current));
903}
904
Michal Kaziorc80de122013-11-25 14:06:23 +0100905static int ath10k_pci_setup_ce_irq(struct ath10k *ar)
906{
907 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
908 const struct ce_attr *attr;
909 struct ath10k_pci_pipe *pipe_info;
910 int pipe_num, disable_interrupts;
911
912 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
913 pipe_info = &ar_pci->pipe_info[pipe_num];
914
915 /* Handle Diagnostic CE specially */
916 if (pipe_info->ce_hdl == ar_pci->ce_diag)
917 continue;
918
919 attr = &host_ce_config_wlan[pipe_num];
920
921 if (attr->src_nentries) {
922 disable_interrupts = attr->flags & CE_ATTR_DIS_INTR;
923 ath10k_ce_send_cb_register(pipe_info->ce_hdl,
924 ath10k_pci_ce_send_done,
925 disable_interrupts);
926 }
927
928 if (attr->dest_nentries)
929 ath10k_ce_recv_cb_register(pipe_info->ce_hdl,
930 ath10k_pci_ce_recv_data);
931 }
932
933 return 0;
934}
935
Michal Kazior96a9d0d2013-11-08 08:01:25 +0100936static void ath10k_pci_kill_tasklet(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300937{
938 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300939 int i;
940
Kalle Valo5e3dd152013-06-12 20:52:10 +0300941 tasklet_kill(&ar_pci->intr_tq);
Michal Kazior103d4f52013-11-08 08:01:24 +0100942 tasklet_kill(&ar_pci->msi_fw_err);
Michal Kaziorab977bd2013-11-25 14:06:26 +0100943 tasklet_kill(&ar_pci->early_irq_tasklet);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300944
945 for (i = 0; i < CE_COUNT; i++)
946 tasklet_kill(&ar_pci->pipe_info[i].intr);
Michal Kazior96a9d0d2013-11-08 08:01:25 +0100947}
948
Kalle Valo5e3dd152013-06-12 20:52:10 +0300949/* TODO - temporary mapping while we have too few CE's */
950static int ath10k_pci_hif_map_service_to_pipe(struct ath10k *ar,
951 u16 service_id, u8 *ul_pipe,
952 u8 *dl_pipe, int *ul_is_polled,
953 int *dl_is_polled)
954{
955 int ret = 0;
956
Kalle Valo50f87a62014-03-28 09:32:52 +0200957 ath10k_dbg(ATH10K_DBG_PCI, "pci hif map service\n");
958
Kalle Valo5e3dd152013-06-12 20:52:10 +0300959 /* polling for received messages not supported */
960 *dl_is_polled = 0;
961
962 switch (service_id) {
963 case ATH10K_HTC_SVC_ID_HTT_DATA_MSG:
964 /*
965 * Host->target HTT gets its own pipe, so it can be polled
966 * while other pipes are interrupt driven.
967 */
968 *ul_pipe = 4;
969 /*
970 * Use the same target->host pipe for HTC ctrl, HTC raw
971 * streams, and HTT.
972 */
973 *dl_pipe = 1;
974 break;
975
976 case ATH10K_HTC_SVC_ID_RSVD_CTRL:
977 case ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS:
978 /*
979 * Note: HTC_RAW_STREAMS_SVC is currently unused, and
980 * HTC_CTRL_RSVD_SVC could share the same pipe as the
981 * WMI services. So, if another CE is needed, change
982 * this to *ul_pipe = 3, which frees up CE 0.
983 */
984 /* *ul_pipe = 3; */
985 *ul_pipe = 0;
986 *dl_pipe = 1;
987 break;
988
989 case ATH10K_HTC_SVC_ID_WMI_DATA_BK:
990 case ATH10K_HTC_SVC_ID_WMI_DATA_BE:
991 case ATH10K_HTC_SVC_ID_WMI_DATA_VI:
992 case ATH10K_HTC_SVC_ID_WMI_DATA_VO:
993
994 case ATH10K_HTC_SVC_ID_WMI_CONTROL:
995 *ul_pipe = 3;
996 *dl_pipe = 2;
997 break;
998
999 /* pipe 5 unused */
1000 /* pipe 6 reserved */
1001 /* pipe 7 reserved */
1002
1003 default:
1004 ret = -1;
1005 break;
1006 }
1007 *ul_is_polled =
1008 (host_ce_config_wlan[*ul_pipe].flags & CE_ATTR_DIS_INTR) != 0;
1009
1010 return ret;
1011}
1012
1013static void ath10k_pci_hif_get_default_pipe(struct ath10k *ar,
1014 u8 *ul_pipe, u8 *dl_pipe)
1015{
1016 int ul_is_polled, dl_is_polled;
1017
Kalle Valo50f87a62014-03-28 09:32:52 +02001018 ath10k_dbg(ATH10K_DBG_PCI, "pci hif get default pipe\n");
1019
Kalle Valo5e3dd152013-06-12 20:52:10 +03001020 (void)ath10k_pci_hif_map_service_to_pipe(ar,
1021 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1022 ul_pipe,
1023 dl_pipe,
1024 &ul_is_polled,
1025 &dl_is_polled);
1026}
1027
Michal Kazior87263e52013-08-27 13:08:01 +02001028static int ath10k_pci_post_rx_pipe(struct ath10k_pci_pipe *pipe_info,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001029 int num)
1030{
1031 struct ath10k *ar = pipe_info->hif_ce_state;
1032 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2aa39112013-08-27 13:08:02 +02001033 struct ath10k_ce_pipe *ce_state = pipe_info->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001034 struct sk_buff *skb;
1035 dma_addr_t ce_data;
1036 int i, ret = 0;
1037
1038 if (pipe_info->buf_sz == 0)
1039 return 0;
1040
1041 for (i = 0; i < num; i++) {
1042 skb = dev_alloc_skb(pipe_info->buf_sz);
1043 if (!skb) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001044 ath10k_warn("failed to allocate skbuff for pipe %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001045 num);
1046 ret = -ENOMEM;
1047 goto err;
1048 }
1049
1050 WARN_ONCE((unsigned long)skb->data & 3, "unaligned skb");
1051
1052 ce_data = dma_map_single(ar->dev, skb->data,
1053 skb->len + skb_tailroom(skb),
1054 DMA_FROM_DEVICE);
1055
1056 if (unlikely(dma_mapping_error(ar->dev, ce_data))) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001057 ath10k_warn("failed to DMA map sk_buff\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001058 dev_kfree_skb_any(skb);
1059 ret = -EIO;
1060 goto err;
1061 }
1062
1063 ATH10K_SKB_CB(skb)->paddr = ce_data;
1064
1065 pci_dma_sync_single_for_device(ar_pci->pdev, ce_data,
1066 pipe_info->buf_sz,
1067 PCI_DMA_FROMDEVICE);
1068
1069 ret = ath10k_ce_recv_buf_enqueue(ce_state, (void *)skb,
1070 ce_data);
1071 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001072 ath10k_warn("failed to enqueue to pipe %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001073 num, ret);
1074 goto err;
1075 }
1076 }
1077
1078 return ret;
1079
1080err:
1081 ath10k_pci_rx_pipe_cleanup(pipe_info);
1082 return ret;
1083}
1084
1085static int ath10k_pci_post_rx(struct ath10k *ar)
1086{
1087 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +02001088 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001089 const struct ce_attr *attr;
1090 int pipe_num, ret = 0;
1091
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001092 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001093 pipe_info = &ar_pci->pipe_info[pipe_num];
1094 attr = &host_ce_config_wlan[pipe_num];
1095
1096 if (attr->dest_nentries == 0)
1097 continue;
1098
1099 ret = ath10k_pci_post_rx_pipe(pipe_info,
1100 attr->dest_nentries - 1);
1101 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001102 ath10k_warn("failed to post RX buffer for pipe %d: %d\n",
1103 pipe_num, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001104
1105 for (; pipe_num >= 0; pipe_num--) {
1106 pipe_info = &ar_pci->pipe_info[pipe_num];
1107 ath10k_pci_rx_pipe_cleanup(pipe_info);
1108 }
1109 return ret;
1110 }
1111 }
1112
1113 return 0;
1114}
1115
1116static int ath10k_pci_hif_start(struct ath10k *ar)
1117{
1118 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorab977bd2013-11-25 14:06:26 +01001119 int ret, ret_early;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001120
Kalle Valo50f87a62014-03-28 09:32:52 +02001121 ath10k_dbg(ATH10K_DBG_BOOT, "boot hif start\n");
1122
Michal Kaziorab977bd2013-11-25 14:06:26 +01001123 ath10k_pci_free_early_irq(ar);
1124 ath10k_pci_kill_tasklet(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001125
Michal Kazior5d1aa942013-11-25 14:06:24 +01001126 ret = ath10k_pci_request_irq(ar);
1127 if (ret) {
1128 ath10k_warn("failed to post RX buffers for all pipes: %d\n",
1129 ret);
Michal Kazior2f5280d2014-02-27 18:50:05 +02001130 goto err_early_irq;
Michal Kazior5d1aa942013-11-25 14:06:24 +01001131 }
1132
Michal Kaziorc80de122013-11-25 14:06:23 +01001133 ret = ath10k_pci_setup_ce_irq(ar);
1134 if (ret) {
1135 ath10k_warn("failed to setup CE interrupts: %d\n", ret);
Michal Kazior5d1aa942013-11-25 14:06:24 +01001136 goto err_stop;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001137 }
1138
1139 /* Post buffers once to start things off. */
1140 ret = ath10k_pci_post_rx(ar);
1141 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001142 ath10k_warn("failed to post RX buffers for all pipes: %d\n",
1143 ret);
Michal Kazior5d1aa942013-11-25 14:06:24 +01001144 goto err_stop;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001145 }
1146
1147 ar_pci->started = 1;
1148 return 0;
Michal Kaziorc80de122013-11-25 14:06:23 +01001149
Michal Kazior5d1aa942013-11-25 14:06:24 +01001150err_stop:
1151 ath10k_ce_disable_interrupts(ar);
1152 ath10k_pci_free_irq(ar);
1153 ath10k_pci_kill_tasklet(ar);
Michal Kaziorab977bd2013-11-25 14:06:26 +01001154err_early_irq:
1155 /* Though there should be no interrupts (device was reset)
1156 * power_down() expects the early IRQ to be installed as per the
1157 * driver lifecycle. */
1158 ret_early = ath10k_pci_request_early_irq(ar);
1159 if (ret_early)
1160 ath10k_warn("failed to re-enable early irq: %d\n", ret_early);
1161
Michal Kaziorc80de122013-11-25 14:06:23 +01001162 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001163}
1164
Michal Kazior87263e52013-08-27 13:08:01 +02001165static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe *pipe_info)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001166{
1167 struct ath10k *ar;
1168 struct ath10k_pci *ar_pci;
Michal Kazior2aa39112013-08-27 13:08:02 +02001169 struct ath10k_ce_pipe *ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001170 u32 buf_sz;
1171 struct sk_buff *netbuf;
1172 u32 ce_data;
1173
1174 buf_sz = pipe_info->buf_sz;
1175
1176 /* Unused Copy Engine */
1177 if (buf_sz == 0)
1178 return;
1179
1180 ar = pipe_info->hif_ce_state;
1181 ar_pci = ath10k_pci_priv(ar);
1182
1183 if (!ar_pci->started)
1184 return;
1185
1186 ce_hdl = pipe_info->ce_hdl;
1187
1188 while (ath10k_ce_revoke_recv_next(ce_hdl, (void **)&netbuf,
1189 &ce_data) == 0) {
1190 dma_unmap_single(ar->dev, ATH10K_SKB_CB(netbuf)->paddr,
1191 netbuf->len + skb_tailroom(netbuf),
1192 DMA_FROM_DEVICE);
1193 dev_kfree_skb_any(netbuf);
1194 }
1195}
1196
Michal Kazior87263e52013-08-27 13:08:01 +02001197static void ath10k_pci_tx_pipe_cleanup(struct ath10k_pci_pipe *pipe_info)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001198{
1199 struct ath10k *ar;
1200 struct ath10k_pci *ar_pci;
Michal Kazior2aa39112013-08-27 13:08:02 +02001201 struct ath10k_ce_pipe *ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001202 struct sk_buff *netbuf;
1203 u32 ce_data;
1204 unsigned int nbytes;
1205 unsigned int id;
1206 u32 buf_sz;
1207
1208 buf_sz = pipe_info->buf_sz;
1209
1210 /* Unused Copy Engine */
1211 if (buf_sz == 0)
1212 return;
1213
1214 ar = pipe_info->hif_ce_state;
1215 ar_pci = ath10k_pci_priv(ar);
1216
1217 if (!ar_pci->started)
1218 return;
1219
1220 ce_hdl = pipe_info->ce_hdl;
1221
1222 while (ath10k_ce_cancel_send_next(ce_hdl, (void **)&netbuf,
1223 &ce_data, &nbytes, &id) == 0) {
Michal Kaziora16942e2014-02-27 18:50:04 +02001224 /* no need to call tx completion for NULL pointers */
1225 if (!netbuf)
Michal Kazior2415fc12013-11-08 08:01:32 +01001226 continue;
Michal Kazior2415fc12013-11-08 08:01:32 +01001227
Kalle Valoe9bb0aa2013-09-08 18:36:11 +03001228 ar_pci->msg_callbacks_current.tx_completion(ar,
1229 netbuf,
1230 id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001231 }
1232}
1233
1234/*
1235 * Cleanup residual buffers for device shutdown:
1236 * buffers that were enqueued for receive
1237 * buffers that were to be sent
1238 * Note: Buffers that had completed but which were
1239 * not yet processed are on a completion queue. They
1240 * are handled when the completion thread shuts down.
1241 */
1242static void ath10k_pci_buffer_cleanup(struct ath10k *ar)
1243{
1244 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1245 int pipe_num;
1246
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001247 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Michal Kazior87263e52013-08-27 13:08:01 +02001248 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001249
1250 pipe_info = &ar_pci->pipe_info[pipe_num];
1251 ath10k_pci_rx_pipe_cleanup(pipe_info);
1252 ath10k_pci_tx_pipe_cleanup(pipe_info);
1253 }
1254}
1255
1256static void ath10k_pci_ce_deinit(struct ath10k *ar)
1257{
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001258 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001259
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001260 for (i = 0; i < CE_COUNT; i++)
1261 ath10k_ce_deinit_pipe(ar, i);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001262}
1263
1264static void ath10k_pci_hif_stop(struct ath10k *ar)
1265{
Michal Kazior32270b62013-08-02 09:15:47 +02001266 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior5d1aa942013-11-25 14:06:24 +01001267 int ret;
Michal Kazior32270b62013-08-02 09:15:47 +02001268
Kalle Valo50f87a62014-03-28 09:32:52 +02001269 ath10k_dbg(ATH10K_DBG_BOOT, "boot hif stop\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001270
Michal Kaziorf2708be2014-05-16 17:15:39 +03001271 if (WARN_ON(!ar_pci->started))
1272 return;
1273
Michal Kazior5d1aa942013-11-25 14:06:24 +01001274 ret = ath10k_ce_disable_interrupts(ar);
1275 if (ret)
1276 ath10k_warn("failed to disable CE interrupts: %d\n", ret);
Michal Kazior32270b62013-08-02 09:15:47 +02001277
Michal Kazior5d1aa942013-11-25 14:06:24 +01001278 ath10k_pci_free_irq(ar);
1279 ath10k_pci_kill_tasklet(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001280
Michal Kaziorab977bd2013-11-25 14:06:26 +01001281 ret = ath10k_pci_request_early_irq(ar);
1282 if (ret)
1283 ath10k_warn("failed to re-enable early irq: %d\n", ret);
1284
Kalle Valo5e3dd152013-06-12 20:52:10 +03001285 /* At this point, asynchronous threads are stopped, the target should
1286 * not DMA nor interrupt. We process the leftovers and then free
1287 * everything else up. */
1288
Kalle Valo5e3dd152013-06-12 20:52:10 +03001289 ath10k_pci_buffer_cleanup(ar);
Michal Kazior32270b62013-08-02 09:15:47 +02001290
Michal Kazior6a42a472013-11-08 08:01:35 +01001291 /* Make the sure the device won't access any structures on the host by
1292 * resetting it. The device was fed with PCI CE ringbuffer
1293 * configuration during init. If ringbuffers are freed and the device
1294 * were to access them this could lead to memory corruption on the
1295 * host. */
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001296 ath10k_pci_warm_reset(ar);
Michal Kazior6a42a472013-11-08 08:01:35 +01001297
Michal Kazior32270b62013-08-02 09:15:47 +02001298 ar_pci->started = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001299}
1300
1301static int ath10k_pci_hif_exchange_bmi_msg(struct ath10k *ar,
1302 void *req, u32 req_len,
1303 void *resp, u32 *resp_len)
1304{
1305 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2aa39112013-08-27 13:08:02 +02001306 struct ath10k_pci_pipe *pci_tx = &ar_pci->pipe_info[BMI_CE_NUM_TO_TARG];
1307 struct ath10k_pci_pipe *pci_rx = &ar_pci->pipe_info[BMI_CE_NUM_TO_HOST];
1308 struct ath10k_ce_pipe *ce_tx = pci_tx->ce_hdl;
1309 struct ath10k_ce_pipe *ce_rx = pci_rx->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001310 dma_addr_t req_paddr = 0;
1311 dma_addr_t resp_paddr = 0;
1312 struct bmi_xfer xfer = {};
1313 void *treq, *tresp = NULL;
1314 int ret = 0;
1315
Michal Kazior85622cd2013-11-25 14:06:22 +01001316 might_sleep();
1317
Kalle Valo5e3dd152013-06-12 20:52:10 +03001318 if (resp && !resp_len)
1319 return -EINVAL;
1320
1321 if (resp && resp_len && *resp_len == 0)
1322 return -EINVAL;
1323
1324 treq = kmemdup(req, req_len, GFP_KERNEL);
1325 if (!treq)
1326 return -ENOMEM;
1327
1328 req_paddr = dma_map_single(ar->dev, treq, req_len, DMA_TO_DEVICE);
1329 ret = dma_mapping_error(ar->dev, req_paddr);
1330 if (ret)
1331 goto err_dma;
1332
1333 if (resp && resp_len) {
1334 tresp = kzalloc(*resp_len, GFP_KERNEL);
1335 if (!tresp) {
1336 ret = -ENOMEM;
1337 goto err_req;
1338 }
1339
1340 resp_paddr = dma_map_single(ar->dev, tresp, *resp_len,
1341 DMA_FROM_DEVICE);
1342 ret = dma_mapping_error(ar->dev, resp_paddr);
1343 if (ret)
1344 goto err_req;
1345
1346 xfer.wait_for_resp = true;
1347 xfer.resp_len = 0;
1348
1349 ath10k_ce_recv_buf_enqueue(ce_rx, &xfer, resp_paddr);
1350 }
1351
Kalle Valo5e3dd152013-06-12 20:52:10 +03001352 ret = ath10k_ce_send(ce_tx, &xfer, req_paddr, req_len, -1, 0);
1353 if (ret)
1354 goto err_resp;
1355
Michal Kazior85622cd2013-11-25 14:06:22 +01001356 ret = ath10k_pci_bmi_wait(ce_tx, ce_rx, &xfer);
1357 if (ret) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001358 u32 unused_buffer;
1359 unsigned int unused_nbytes;
1360 unsigned int unused_id;
1361
Kalle Valo5e3dd152013-06-12 20:52:10 +03001362 ath10k_ce_cancel_send_next(ce_tx, NULL, &unused_buffer,
1363 &unused_nbytes, &unused_id);
1364 } else {
1365 /* non-zero means we did not time out */
1366 ret = 0;
1367 }
1368
1369err_resp:
1370 if (resp) {
1371 u32 unused_buffer;
1372
1373 ath10k_ce_revoke_recv_next(ce_rx, NULL, &unused_buffer);
1374 dma_unmap_single(ar->dev, resp_paddr,
1375 *resp_len, DMA_FROM_DEVICE);
1376 }
1377err_req:
1378 dma_unmap_single(ar->dev, req_paddr, req_len, DMA_TO_DEVICE);
1379
1380 if (ret == 0 && resp_len) {
1381 *resp_len = min(*resp_len, xfer.resp_len);
1382 memcpy(resp, tresp, xfer.resp_len);
1383 }
1384err_dma:
1385 kfree(treq);
1386 kfree(tresp);
1387
1388 return ret;
1389}
1390
Michal Kazior5440ce22013-09-03 15:09:58 +02001391static void ath10k_pci_bmi_send_done(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001392{
Michal Kazior5440ce22013-09-03 15:09:58 +02001393 struct bmi_xfer *xfer;
1394 u32 ce_data;
1395 unsigned int nbytes;
1396 unsigned int transfer_id;
1397
1398 if (ath10k_ce_completed_send_next(ce_state, (void **)&xfer, &ce_data,
1399 &nbytes, &transfer_id))
1400 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001401
Michal Kazior2374b182014-07-14 16:25:25 +03001402 xfer->tx_done = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001403}
1404
Michal Kazior5440ce22013-09-03 15:09:58 +02001405static void ath10k_pci_bmi_recv_data(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001406{
Michal Kazior5440ce22013-09-03 15:09:58 +02001407 struct bmi_xfer *xfer;
1408 u32 ce_data;
1409 unsigned int nbytes;
1410 unsigned int transfer_id;
1411 unsigned int flags;
1412
1413 if (ath10k_ce_completed_recv_next(ce_state, (void **)&xfer, &ce_data,
1414 &nbytes, &transfer_id, &flags))
1415 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001416
1417 if (!xfer->wait_for_resp) {
1418 ath10k_warn("unexpected: BMI data received; ignoring\n");
1419 return;
1420 }
1421
1422 xfer->resp_len = nbytes;
Michal Kazior2374b182014-07-14 16:25:25 +03001423 xfer->rx_done = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001424}
1425
Michal Kazior85622cd2013-11-25 14:06:22 +01001426static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
1427 struct ath10k_ce_pipe *rx_pipe,
1428 struct bmi_xfer *xfer)
1429{
1430 unsigned long timeout = jiffies + BMI_COMMUNICATION_TIMEOUT_HZ;
1431
1432 while (time_before_eq(jiffies, timeout)) {
1433 ath10k_pci_bmi_send_done(tx_pipe);
1434 ath10k_pci_bmi_recv_data(rx_pipe);
1435
Michal Kazior2374b182014-07-14 16:25:25 +03001436 if (xfer->tx_done && (xfer->rx_done == xfer->wait_for_resp))
Michal Kazior85622cd2013-11-25 14:06:22 +01001437 return 0;
1438
1439 schedule();
1440 }
1441
1442 return -ETIMEDOUT;
1443}
1444
Kalle Valo5e3dd152013-06-12 20:52:10 +03001445/*
1446 * Map from service/endpoint to Copy Engine.
1447 * This table is derived from the CE_PCI TABLE, above.
1448 * It is passed to the Target at startup for use by firmware.
1449 */
1450static const struct service_to_pipe target_service_to_ce_map_wlan[] = {
1451 {
1452 ATH10K_HTC_SVC_ID_WMI_DATA_VO,
1453 PIPEDIR_OUT, /* out = UL = host -> target */
1454 3,
1455 },
1456 {
1457 ATH10K_HTC_SVC_ID_WMI_DATA_VO,
1458 PIPEDIR_IN, /* in = DL = target -> host */
1459 2,
1460 },
1461 {
1462 ATH10K_HTC_SVC_ID_WMI_DATA_BK,
1463 PIPEDIR_OUT, /* out = UL = host -> target */
1464 3,
1465 },
1466 {
1467 ATH10K_HTC_SVC_ID_WMI_DATA_BK,
1468 PIPEDIR_IN, /* in = DL = target -> host */
1469 2,
1470 },
1471 {
1472 ATH10K_HTC_SVC_ID_WMI_DATA_BE,
1473 PIPEDIR_OUT, /* out = UL = host -> target */
1474 3,
1475 },
1476 {
1477 ATH10K_HTC_SVC_ID_WMI_DATA_BE,
1478 PIPEDIR_IN, /* in = DL = target -> host */
1479 2,
1480 },
1481 {
1482 ATH10K_HTC_SVC_ID_WMI_DATA_VI,
1483 PIPEDIR_OUT, /* out = UL = host -> target */
1484 3,
1485 },
1486 {
1487 ATH10K_HTC_SVC_ID_WMI_DATA_VI,
1488 PIPEDIR_IN, /* in = DL = target -> host */
1489 2,
1490 },
1491 {
1492 ATH10K_HTC_SVC_ID_WMI_CONTROL,
1493 PIPEDIR_OUT, /* out = UL = host -> target */
1494 3,
1495 },
1496 {
1497 ATH10K_HTC_SVC_ID_WMI_CONTROL,
1498 PIPEDIR_IN, /* in = DL = target -> host */
1499 2,
1500 },
1501 {
1502 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1503 PIPEDIR_OUT, /* out = UL = host -> target */
1504 0, /* could be moved to 3 (share with WMI) */
1505 },
1506 {
1507 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1508 PIPEDIR_IN, /* in = DL = target -> host */
1509 1,
1510 },
1511 {
1512 ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS, /* not currently used */
1513 PIPEDIR_OUT, /* out = UL = host -> target */
1514 0,
1515 },
1516 {
1517 ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS, /* not currently used */
1518 PIPEDIR_IN, /* in = DL = target -> host */
1519 1,
1520 },
1521 {
1522 ATH10K_HTC_SVC_ID_HTT_DATA_MSG,
1523 PIPEDIR_OUT, /* out = UL = host -> target */
1524 4,
1525 },
1526 {
1527 ATH10K_HTC_SVC_ID_HTT_DATA_MSG,
1528 PIPEDIR_IN, /* in = DL = target -> host */
1529 1,
1530 },
1531
1532 /* (Additions here) */
1533
1534 { /* Must be last */
1535 0,
1536 0,
1537 0,
1538 },
1539};
1540
1541/*
1542 * Send an interrupt to the device to wake up the Target CPU
1543 * so it has an opportunity to notice any changed state.
1544 */
1545static int ath10k_pci_wake_target_cpu(struct ath10k *ar)
1546{
1547 int ret;
1548 u32 core_ctrl;
1549
1550 ret = ath10k_pci_diag_read_access(ar, SOC_CORE_BASE_ADDRESS |
1551 CORE_CTRL_ADDRESS,
1552 &core_ctrl);
1553 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001554 ath10k_warn("failed to read core_ctrl: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001555 return ret;
1556 }
1557
1558 /* A_INUM_FIRMWARE interrupt to Target CPU */
1559 core_ctrl |= CORE_CTRL_CPU_INTR_MASK;
1560
1561 ret = ath10k_pci_diag_write_access(ar, SOC_CORE_BASE_ADDRESS |
1562 CORE_CTRL_ADDRESS,
1563 core_ctrl);
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001564 if (ret) {
1565 ath10k_warn("failed to set target CPU interrupt mask: %d\n",
1566 ret);
1567 return ret;
1568 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001569
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001570 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001571}
1572
1573static int ath10k_pci_init_config(struct ath10k *ar)
1574{
1575 u32 interconnect_targ_addr;
1576 u32 pcie_state_targ_addr = 0;
1577 u32 pipe_cfg_targ_addr = 0;
1578 u32 svc_to_pipe_map = 0;
1579 u32 pcie_config_flags = 0;
1580 u32 ealloc_value;
1581 u32 ealloc_targ_addr;
1582 u32 flag2_value;
1583 u32 flag2_targ_addr;
1584 int ret = 0;
1585
1586 /* Download to Target the CE Config and the service-to-CE map */
1587 interconnect_targ_addr =
1588 host_interest_item_address(HI_ITEM(hi_interconnect_state));
1589
1590 /* Supply Target-side CE configuration */
1591 ret = ath10k_pci_diag_read_access(ar, interconnect_targ_addr,
1592 &pcie_state_targ_addr);
1593 if (ret != 0) {
1594 ath10k_err("Failed to get pcie state addr: %d\n", ret);
1595 return ret;
1596 }
1597
1598 if (pcie_state_targ_addr == 0) {
1599 ret = -EIO;
1600 ath10k_err("Invalid pcie state addr\n");
1601 return ret;
1602 }
1603
1604 ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
1605 offsetof(struct pcie_state,
1606 pipe_cfg_addr),
1607 &pipe_cfg_targ_addr);
1608 if (ret != 0) {
1609 ath10k_err("Failed to get pipe cfg addr: %d\n", ret);
1610 return ret;
1611 }
1612
1613 if (pipe_cfg_targ_addr == 0) {
1614 ret = -EIO;
1615 ath10k_err("Invalid pipe cfg addr\n");
1616 return ret;
1617 }
1618
1619 ret = ath10k_pci_diag_write_mem(ar, pipe_cfg_targ_addr,
1620 target_ce_config_wlan,
1621 sizeof(target_ce_config_wlan));
1622
1623 if (ret != 0) {
1624 ath10k_err("Failed to write pipe cfg: %d\n", ret);
1625 return ret;
1626 }
1627
1628 ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
1629 offsetof(struct pcie_state,
1630 svc_to_pipe_map),
1631 &svc_to_pipe_map);
1632 if (ret != 0) {
1633 ath10k_err("Failed to get svc/pipe map: %d\n", ret);
1634 return ret;
1635 }
1636
1637 if (svc_to_pipe_map == 0) {
1638 ret = -EIO;
1639 ath10k_err("Invalid svc_to_pipe map\n");
1640 return ret;
1641 }
1642
1643 ret = ath10k_pci_diag_write_mem(ar, svc_to_pipe_map,
1644 target_service_to_ce_map_wlan,
1645 sizeof(target_service_to_ce_map_wlan));
1646 if (ret != 0) {
1647 ath10k_err("Failed to write svc/pipe map: %d\n", ret);
1648 return ret;
1649 }
1650
1651 ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
1652 offsetof(struct pcie_state,
1653 config_flags),
1654 &pcie_config_flags);
1655 if (ret != 0) {
1656 ath10k_err("Failed to get pcie config_flags: %d\n", ret);
1657 return ret;
1658 }
1659
1660 pcie_config_flags &= ~PCIE_CONFIG_FLAG_ENABLE_L1;
1661
1662 ret = ath10k_pci_diag_write_mem(ar, pcie_state_targ_addr +
1663 offsetof(struct pcie_state, config_flags),
1664 &pcie_config_flags,
1665 sizeof(pcie_config_flags));
1666 if (ret != 0) {
1667 ath10k_err("Failed to write pcie config_flags: %d\n", ret);
1668 return ret;
1669 }
1670
1671 /* configure early allocation */
1672 ealloc_targ_addr = host_interest_item_address(HI_ITEM(hi_early_alloc));
1673
1674 ret = ath10k_pci_diag_read_access(ar, ealloc_targ_addr, &ealloc_value);
1675 if (ret != 0) {
1676 ath10k_err("Faile to get early alloc val: %d\n", ret);
1677 return ret;
1678 }
1679
1680 /* first bank is switched to IRAM */
1681 ealloc_value |= ((HI_EARLY_ALLOC_MAGIC << HI_EARLY_ALLOC_MAGIC_SHIFT) &
1682 HI_EARLY_ALLOC_MAGIC_MASK);
1683 ealloc_value |= ((1 << HI_EARLY_ALLOC_IRAM_BANKS_SHIFT) &
1684 HI_EARLY_ALLOC_IRAM_BANKS_MASK);
1685
1686 ret = ath10k_pci_diag_write_access(ar, ealloc_targ_addr, ealloc_value);
1687 if (ret != 0) {
1688 ath10k_err("Failed to set early alloc val: %d\n", ret);
1689 return ret;
1690 }
1691
1692 /* Tell Target to proceed with initialization */
1693 flag2_targ_addr = host_interest_item_address(HI_ITEM(hi_option_flag2));
1694
1695 ret = ath10k_pci_diag_read_access(ar, flag2_targ_addr, &flag2_value);
1696 if (ret != 0) {
1697 ath10k_err("Failed to get option val: %d\n", ret);
1698 return ret;
1699 }
1700
1701 flag2_value |= HI_OPTION_EARLY_CFG_DONE;
1702
1703 ret = ath10k_pci_diag_write_access(ar, flag2_targ_addr, flag2_value);
1704 if (ret != 0) {
1705 ath10k_err("Failed to set option val: %d\n", ret);
1706 return ret;
1707 }
1708
1709 return 0;
1710}
1711
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001712static int ath10k_pci_alloc_ce(struct ath10k *ar)
1713{
1714 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001715
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001716 for (i = 0; i < CE_COUNT; i++) {
1717 ret = ath10k_ce_alloc_pipe(ar, i, &host_ce_config_wlan[i]);
1718 if (ret) {
1719 ath10k_err("failed to allocate copy engine pipe %d: %d\n",
1720 i, ret);
1721 return ret;
1722 }
1723 }
1724
1725 return 0;
1726}
1727
1728static void ath10k_pci_free_ce(struct ath10k *ar)
1729{
1730 int i;
1731
1732 for (i = 0; i < CE_COUNT; i++)
1733 ath10k_ce_free_pipe(ar, i);
1734}
Kalle Valo5e3dd152013-06-12 20:52:10 +03001735
1736static int ath10k_pci_ce_init(struct ath10k *ar)
1737{
1738 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +02001739 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001740 const struct ce_attr *attr;
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001741 int pipe_num, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001742
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001743 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001744 pipe_info = &ar_pci->pipe_info[pipe_num];
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001745 pipe_info->ce_hdl = &ar_pci->ce_states[pipe_num];
Kalle Valo5e3dd152013-06-12 20:52:10 +03001746 pipe_info->pipe_num = pipe_num;
1747 pipe_info->hif_ce_state = ar;
1748 attr = &host_ce_config_wlan[pipe_num];
1749
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001750 ret = ath10k_ce_init_pipe(ar, pipe_num, attr);
1751 if (ret) {
1752 ath10k_err("failed to initialize copy engine pipe %d: %d\n",
1753 pipe_num, ret);
1754 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001755 }
1756
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001757 if (pipe_num == CE_COUNT - 1) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001758 /*
1759 * Reserve the ultimate CE for
1760 * diagnostic Window support
1761 */
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001762 ar_pci->ce_diag = pipe_info->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001763 continue;
1764 }
1765
1766 pipe_info->buf_sz = (size_t) (attr->src_sz_max);
1767 }
1768
Kalle Valo5e3dd152013-06-12 20:52:10 +03001769 return 0;
1770}
1771
1772static void ath10k_pci_fw_interrupt_handler(struct ath10k *ar)
1773{
1774 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valob39712c2014-03-28 09:32:46 +02001775 u32 fw_indicator;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001776
Kalle Valob39712c2014-03-28 09:32:46 +02001777 fw_indicator = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001778
1779 if (fw_indicator & FW_IND_EVENT_PENDING) {
1780 /* ACK: clear Target-side pending event */
Kalle Valob39712c2014-03-28 09:32:46 +02001781 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001782 fw_indicator & ~FW_IND_EVENT_PENDING);
1783
1784 if (ar_pci->started) {
1785 ath10k_pci_hif_dump_area(ar);
1786 } else {
1787 /*
1788 * Probable Target failure before we're prepared
1789 * to handle it. Generally unexpected.
1790 */
1791 ath10k_warn("early firmware event indicated\n");
1792 }
1793 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001794}
1795
Michal Kaziorde013572014-05-14 16:56:16 +03001796/* this function effectively clears target memory controller assert line */
1797static void ath10k_pci_warm_reset_si0(struct ath10k *ar)
1798{
1799 u32 val;
1800
1801 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1802 ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
1803 val | SOC_RESET_CONTROL_SI0_RST_MASK);
1804 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1805
1806 msleep(10);
1807
1808 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1809 ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
1810 val & ~SOC_RESET_CONTROL_SI0_RST_MASK);
1811 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1812
1813 msleep(10);
1814}
1815
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001816static int ath10k_pci_warm_reset(struct ath10k *ar)
1817{
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001818 u32 val;
1819
Kalle Valo50f87a62014-03-28 09:32:52 +02001820 ath10k_dbg(ATH10K_DBG_BOOT, "boot warm reset\n");
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001821
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001822 /* debug */
1823 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1824 PCIE_INTR_CAUSE_ADDRESS);
1825 ath10k_dbg(ATH10K_DBG_BOOT, "boot host cpu intr cause: 0x%08x\n", val);
1826
1827 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1828 CPU_INTR_ADDRESS);
1829 ath10k_dbg(ATH10K_DBG_BOOT, "boot target cpu intr cause: 0x%08x\n",
1830 val);
1831
1832 /* disable pending irqs */
1833 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
1834 PCIE_INTR_ENABLE_ADDRESS, 0);
1835
1836 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
1837 PCIE_INTR_CLR_ADDRESS, ~0);
1838
1839 msleep(100);
1840
1841 /* clear fw indicator */
Kalle Valob39712c2014-03-28 09:32:46 +02001842 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, 0);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001843
1844 /* clear target LF timer interrupts */
1845 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1846 SOC_LF_TIMER_CONTROL0_ADDRESS);
1847 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS +
1848 SOC_LF_TIMER_CONTROL0_ADDRESS,
1849 val & ~SOC_LF_TIMER_CONTROL0_ENABLE_MASK);
1850
1851 /* reset CE */
1852 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1853 SOC_RESET_CONTROL_ADDRESS);
1854 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1855 val | SOC_RESET_CONTROL_CE_RST_MASK);
1856 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1857 SOC_RESET_CONTROL_ADDRESS);
1858 msleep(10);
1859
1860 /* unreset CE */
1861 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1862 val & ~SOC_RESET_CONTROL_CE_RST_MASK);
1863 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1864 SOC_RESET_CONTROL_ADDRESS);
1865 msleep(10);
1866
Michal Kaziorde013572014-05-14 16:56:16 +03001867 ath10k_pci_warm_reset_si0(ar);
1868
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001869 /* debug */
1870 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1871 PCIE_INTR_CAUSE_ADDRESS);
1872 ath10k_dbg(ATH10K_DBG_BOOT, "boot host cpu intr cause: 0x%08x\n", val);
1873
1874 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1875 CPU_INTR_ADDRESS);
1876 ath10k_dbg(ATH10K_DBG_BOOT, "boot target cpu intr cause: 0x%08x\n",
1877 val);
1878
1879 /* CPU warm reset */
1880 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1881 SOC_RESET_CONTROL_ADDRESS);
1882 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1883 val | SOC_RESET_CONTROL_CPU_WARM_RST_MASK);
1884
1885 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1886 SOC_RESET_CONTROL_ADDRESS);
1887 ath10k_dbg(ATH10K_DBG_BOOT, "boot target reset state: 0x%08x\n", val);
1888
1889 msleep(100);
1890
1891 ath10k_dbg(ATH10K_DBG_BOOT, "boot warm reset complete\n");
1892
Michal Kaziorc0c378f2014-08-07 11:03:28 +02001893 return 0;
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001894}
1895
1896static int __ath10k_pci_hif_power_up(struct ath10k *ar, bool cold_reset)
Michal Kazior8c5c5362013-07-16 09:38:50 +02001897{
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02001898 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo95cbb6a2013-11-20 10:00:35 +02001899 const char *irq_mode;
Michal Kazior8c5c5362013-07-16 09:38:50 +02001900 int ret;
1901
1902 /*
1903 * Bring the target up cleanly.
1904 *
1905 * The target may be in an undefined state with an AUX-powered Target
1906 * and a Host in WoW mode. If the Host crashes, loses power, or is
1907 * restarted (without unloading the driver) then the Target is left
1908 * (aux) powered and running. On a subsequent driver load, the Target
1909 * is in an unexpected state. We try to catch that here in order to
1910 * reset the Target and retry the probe.
1911 */
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001912 if (cold_reset)
1913 ret = ath10k_pci_cold_reset(ar);
1914 else
1915 ret = ath10k_pci_warm_reset(ar);
1916
Michal Kazior5b2589f2013-11-08 08:01:30 +01001917 if (ret) {
1918 ath10k_err("failed to reset target: %d\n", ret);
Michal Kazior98563d52013-11-08 08:01:33 +01001919 goto err;
Michal Kazior5b2589f2013-11-08 08:01:30 +01001920 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02001921
Michal Kazior8c5c5362013-07-16 09:38:50 +02001922 ret = ath10k_pci_ce_init(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001923 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001924 ath10k_err("failed to initialize CE: %d\n", ret);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02001925 goto err;
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001926 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02001927
Michal Kazior98563d52013-11-08 08:01:33 +01001928 ret = ath10k_ce_disable_interrupts(ar);
1929 if (ret) {
1930 ath10k_err("failed to disable CE interrupts: %d\n", ret);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001931 goto err_ce;
1932 }
1933
Michal Kaziorfc15ca12013-11-25 14:06:21 +01001934 ret = ath10k_pci_init_irq(ar);
Michal Kazior98563d52013-11-08 08:01:33 +01001935 if (ret) {
Michal Kaziorfc15ca12013-11-25 14:06:21 +01001936 ath10k_err("failed to init irqs: %d\n", ret);
Michal Kazior98563d52013-11-08 08:01:33 +01001937 goto err_ce;
1938 }
1939
Michal Kaziorab977bd2013-11-25 14:06:26 +01001940 ret = ath10k_pci_request_early_irq(ar);
1941 if (ret) {
1942 ath10k_err("failed to request early irq: %d\n", ret);
1943 goto err_deinit_irq;
1944 }
1945
Michal Kazior98563d52013-11-08 08:01:33 +01001946 ret = ath10k_pci_wait_for_target_init(ar);
1947 if (ret) {
1948 ath10k_err("failed to wait for target to init: %d\n", ret);
Michal Kaziorab977bd2013-11-25 14:06:26 +01001949 goto err_free_early_irq;
Michal Kazior98563d52013-11-08 08:01:33 +01001950 }
1951
1952 ret = ath10k_pci_init_config(ar);
1953 if (ret) {
1954 ath10k_err("failed to setup init config: %d\n", ret);
Michal Kaziorab977bd2013-11-25 14:06:26 +01001955 goto err_free_early_irq;
Michal Kazior98563d52013-11-08 08:01:33 +01001956 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02001957
1958 ret = ath10k_pci_wake_target_cpu(ar);
1959 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001960 ath10k_err("could not wake up target CPU: %d\n", ret);
Michal Kaziorab977bd2013-11-25 14:06:26 +01001961 goto err_free_early_irq;
Michal Kazior8c5c5362013-07-16 09:38:50 +02001962 }
1963
Kalle Valo95cbb6a2013-11-20 10:00:35 +02001964 if (ar_pci->num_msi_intrs > 1)
1965 irq_mode = "MSI-X";
1966 else if (ar_pci->num_msi_intrs == 1)
1967 irq_mode = "MSI";
1968 else
1969 irq_mode = "legacy";
1970
Kalle Valo650b91f2013-11-20 10:00:49 +02001971 if (!test_bit(ATH10K_FLAG_FIRST_BOOT_DONE, &ar->dev_flags))
Kalle Valo78a9cb42014-03-28 09:32:58 +02001972 ath10k_info("pci irq %s irq_mode %d reset_mode %d\n",
1973 irq_mode, ath10k_pci_irq_mode,
1974 ath10k_pci_reset_mode);
Kalle Valo95cbb6a2013-11-20 10:00:35 +02001975
Michal Kazior8c5c5362013-07-16 09:38:50 +02001976 return 0;
1977
Michal Kaziorab977bd2013-11-25 14:06:26 +01001978err_free_early_irq:
1979 ath10k_pci_free_early_irq(ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01001980err_deinit_irq:
1981 ath10k_pci_deinit_irq(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001982err_ce:
1983 ath10k_pci_ce_deinit(ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001984 ath10k_pci_warm_reset(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001985err:
1986 return ret;
1987}
1988
Michal Kazior61c95ce2014-05-14 16:56:16 +03001989static int ath10k_pci_hif_power_up_warm(struct ath10k *ar)
1990{
1991 int i, ret;
1992
1993 /*
1994 * Sometime warm reset succeeds after retries.
1995 *
1996 * FIXME: It might be possible to tune ath10k_pci_warm_reset() to work
1997 * at first try.
1998 */
1999 for (i = 0; i < ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS; i++) {
2000 ret = __ath10k_pci_hif_power_up(ar, false);
2001 if (ret == 0)
2002 break;
2003
2004 ath10k_warn("failed to warm reset (attempt %d out of %d): %d\n",
2005 i + 1, ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS, ret);
2006 }
2007
2008 return ret;
2009}
2010
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002011static int ath10k_pci_hif_power_up(struct ath10k *ar)
2012{
2013 int ret;
2014
Kalle Valo50f87a62014-03-28 09:32:52 +02002015 ath10k_dbg(ATH10K_DBG_BOOT, "boot hif power up\n");
2016
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002017 /*
2018 * Hardware CUS232 version 2 has some issues with cold reset and the
2019 * preferred (and safer) way to perform a device reset is through a
2020 * warm reset.
2021 *
Michal Kazior61c95ce2014-05-14 16:56:16 +03002022 * Warm reset doesn't always work though so fall back to cold reset may
2023 * be necessary.
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002024 */
Michal Kazior61c95ce2014-05-14 16:56:16 +03002025 ret = ath10k_pci_hif_power_up_warm(ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002026 if (ret) {
Kalle Valo35098462014-03-28 09:32:27 +02002027 ath10k_warn("failed to power up target using warm reset: %d\n",
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002028 ret);
2029
Kalle Valo35098462014-03-28 09:32:27 +02002030 if (ath10k_pci_reset_mode == ATH10K_PCI_RESET_WARM_ONLY)
2031 return ret;
2032
2033 ath10k_warn("trying cold reset\n");
2034
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002035 ret = __ath10k_pci_hif_power_up(ar, true);
2036 if (ret) {
2037 ath10k_err("failed to power up target using cold reset too (%d)\n",
2038 ret);
2039 return ret;
2040 }
2041 }
2042
2043 return 0;
2044}
2045
Michal Kazior8c5c5362013-07-16 09:38:50 +02002046static void ath10k_pci_hif_power_down(struct ath10k *ar)
2047{
Kalle Valo50f87a62014-03-28 09:32:52 +02002048 ath10k_dbg(ATH10K_DBG_BOOT, "boot hif power down\n");
2049
Michal Kaziorab977bd2013-11-25 14:06:26 +01002050 ath10k_pci_free_early_irq(ar);
2051 ath10k_pci_kill_tasklet(ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002052 ath10k_pci_deinit_irq(ar);
Michal Kaziordf5e8522014-03-28 10:02:45 +02002053 ath10k_pci_ce_deinit(ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002054 ath10k_pci_warm_reset(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02002055}
2056
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002057#ifdef CONFIG_PM
2058
2059#define ATH10K_PCI_PM_CONTROL 0x44
2060
2061static int ath10k_pci_hif_suspend(struct ath10k *ar)
2062{
2063 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2064 struct pci_dev *pdev = ar_pci->pdev;
2065 u32 val;
2066
2067 pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
2068
2069 if ((val & 0x000000ff) != 0x3) {
2070 pci_save_state(pdev);
2071 pci_disable_device(pdev);
2072 pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
2073 (val & 0xffffff00) | 0x03);
2074 }
2075
2076 return 0;
2077}
2078
2079static int ath10k_pci_hif_resume(struct ath10k *ar)
2080{
2081 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2082 struct pci_dev *pdev = ar_pci->pdev;
2083 u32 val;
2084
2085 pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
2086
2087 if ((val & 0x000000ff) != 0) {
2088 pci_restore_state(pdev);
2089 pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
2090 val & 0xffffff00);
2091 /*
2092 * Suspend/Resume resets the PCI configuration space,
2093 * so we have to re-disable the RETRY_TIMEOUT register (0x41)
2094 * to keep PCI Tx retries from interfering with C3 CPU state
2095 */
2096 pci_read_config_dword(pdev, 0x40, &val);
2097
2098 if ((val & 0x0000ff00) != 0)
2099 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
2100 }
2101
2102 return 0;
2103}
2104#endif
2105
Kalle Valo5e3dd152013-06-12 20:52:10 +03002106static const struct ath10k_hif_ops ath10k_pci_hif_ops = {
Michal Kazior726346f2014-02-27 18:50:04 +02002107 .tx_sg = ath10k_pci_hif_tx_sg,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002108 .exchange_bmi_msg = ath10k_pci_hif_exchange_bmi_msg,
2109 .start = ath10k_pci_hif_start,
2110 .stop = ath10k_pci_hif_stop,
2111 .map_service_to_pipe = ath10k_pci_hif_map_service_to_pipe,
2112 .get_default_pipe = ath10k_pci_hif_get_default_pipe,
2113 .send_complete_check = ath10k_pci_hif_send_complete_check,
Michal Kaziore799bbf2013-07-05 16:15:12 +03002114 .set_callbacks = ath10k_pci_hif_set_callbacks,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002115 .get_free_queue_number = ath10k_pci_hif_get_free_queue_number,
Michal Kazior8c5c5362013-07-16 09:38:50 +02002116 .power_up = ath10k_pci_hif_power_up,
2117 .power_down = ath10k_pci_hif_power_down,
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002118#ifdef CONFIG_PM
2119 .suspend = ath10k_pci_hif_suspend,
2120 .resume = ath10k_pci_hif_resume,
2121#endif
Kalle Valo5e3dd152013-06-12 20:52:10 +03002122};
2123
2124static void ath10k_pci_ce_tasklet(unsigned long ptr)
2125{
Michal Kazior87263e52013-08-27 13:08:01 +02002126 struct ath10k_pci_pipe *pipe = (struct ath10k_pci_pipe *)ptr;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002127 struct ath10k_pci *ar_pci = pipe->ar_pci;
2128
2129 ath10k_ce_per_engine_service(ar_pci->ar, pipe->pipe_num);
2130}
2131
2132static void ath10k_msi_err_tasklet(unsigned long data)
2133{
2134 struct ath10k *ar = (struct ath10k *)data;
2135
2136 ath10k_pci_fw_interrupt_handler(ar);
2137}
2138
2139/*
2140 * Handler for a per-engine interrupt on a PARTICULAR CE.
2141 * This is used in cases where each CE has a private MSI interrupt.
2142 */
2143static irqreturn_t ath10k_pci_per_engine_handler(int irq, void *arg)
2144{
2145 struct ath10k *ar = arg;
2146 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2147 int ce_id = irq - ar_pci->pdev->irq - MSI_ASSIGN_CE_INITIAL;
2148
Dan Carpentere5742672013-06-18 10:28:46 +03002149 if (ce_id < 0 || ce_id >= ARRAY_SIZE(ar_pci->pipe_info)) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03002150 ath10k_warn("unexpected/invalid irq %d ce_id %d\n", irq, ce_id);
2151 return IRQ_HANDLED;
2152 }
2153
2154 /*
2155 * NOTE: We are able to derive ce_id from irq because we
2156 * use a one-to-one mapping for CE's 0..5.
2157 * CE's 6 & 7 do not use interrupts at all.
2158 *
2159 * This mapping must be kept in sync with the mapping
2160 * used by firmware.
2161 */
2162 tasklet_schedule(&ar_pci->pipe_info[ce_id].intr);
2163 return IRQ_HANDLED;
2164}
2165
2166static irqreturn_t ath10k_pci_msi_fw_handler(int irq, void *arg)
2167{
2168 struct ath10k *ar = arg;
2169 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2170
2171 tasklet_schedule(&ar_pci->msi_fw_err);
2172 return IRQ_HANDLED;
2173}
2174
2175/*
2176 * Top-level interrupt handler for all PCI interrupts from a Target.
2177 * When a block of MSI interrupts is allocated, this top-level handler
2178 * is not used; instead, we directly call the correct sub-handler.
2179 */
2180static irqreturn_t ath10k_pci_interrupt_handler(int irq, void *arg)
2181{
2182 struct ath10k *ar = arg;
2183 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2184
2185 if (ar_pci->num_msi_intrs == 0) {
Michal Kaziore5398872013-11-25 14:06:20 +01002186 if (!ath10k_pci_irq_pending(ar))
2187 return IRQ_NONE;
2188
Michal Kazior26852182013-11-25 14:06:25 +01002189 ath10k_pci_disable_and_clear_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002190 }
2191
2192 tasklet_schedule(&ar_pci->intr_tq);
2193
2194 return IRQ_HANDLED;
2195}
2196
Michal Kaziorab977bd2013-11-25 14:06:26 +01002197static void ath10k_pci_early_irq_tasklet(unsigned long data)
2198{
2199 struct ath10k *ar = (struct ath10k *)data;
Michal Kaziorab977bd2013-11-25 14:06:26 +01002200 u32 fw_ind;
Michal Kaziorab977bd2013-11-25 14:06:26 +01002201
Kalle Valob39712c2014-03-28 09:32:46 +02002202 fw_ind = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
Michal Kaziorab977bd2013-11-25 14:06:26 +01002203 if (fw_ind & FW_IND_EVENT_PENDING) {
Kalle Valob39712c2014-03-28 09:32:46 +02002204 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS,
Michal Kaziorab977bd2013-11-25 14:06:26 +01002205 fw_ind & ~FW_IND_EVENT_PENDING);
Michal Kazior1a4ab282014-05-14 16:56:16 +03002206 ath10k_pci_hif_dump_area(ar);
Michal Kaziorab977bd2013-11-25 14:06:26 +01002207 }
2208
Michal Kaziorab977bd2013-11-25 14:06:26 +01002209 ath10k_pci_enable_legacy_irq(ar);
2210}
2211
Kalle Valo5e3dd152013-06-12 20:52:10 +03002212static void ath10k_pci_tasklet(unsigned long data)
2213{
2214 struct ath10k *ar = (struct ath10k *)data;
2215 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2216
2217 ath10k_pci_fw_interrupt_handler(ar); /* FIXME: Handle FW error */
2218 ath10k_ce_per_engine_service_any(ar);
2219
Michal Kazior26852182013-11-25 14:06:25 +01002220 /* Re-enable legacy irq that was disabled in the irq handler */
2221 if (ar_pci->num_msi_intrs == 0)
2222 ath10k_pci_enable_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002223}
2224
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002225static int ath10k_pci_request_irq_msix(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002226{
2227 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002228 int ret, i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002229
2230 ret = request_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW,
2231 ath10k_pci_msi_fw_handler,
2232 IRQF_SHARED, "ath10k_pci", ar);
Michal Kazior591ecdb2013-07-31 10:55:15 +02002233 if (ret) {
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002234 ath10k_warn("failed to request MSI-X fw irq %d: %d\n",
Michal Kazior591ecdb2013-07-31 10:55:15 +02002235 ar_pci->pdev->irq + MSI_ASSIGN_FW, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002236 return ret;
Michal Kazior591ecdb2013-07-31 10:55:15 +02002237 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002238
2239 for (i = MSI_ASSIGN_CE_INITIAL; i <= MSI_ASSIGN_CE_MAX; i++) {
2240 ret = request_irq(ar_pci->pdev->irq + i,
2241 ath10k_pci_per_engine_handler,
2242 IRQF_SHARED, "ath10k_pci", ar);
2243 if (ret) {
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002244 ath10k_warn("failed to request MSI-X ce irq %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002245 ar_pci->pdev->irq + i, ret);
2246
Michal Kazior87b14232013-06-26 08:50:50 +02002247 for (i--; i >= MSI_ASSIGN_CE_INITIAL; i--)
2248 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002249
Michal Kazior87b14232013-06-26 08:50:50 +02002250 free_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002251 return ret;
2252 }
2253 }
2254
Kalle Valo5e3dd152013-06-12 20:52:10 +03002255 return 0;
2256}
2257
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002258static int ath10k_pci_request_irq_msi(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002259{
2260 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2261 int ret;
2262
2263 ret = request_irq(ar_pci->pdev->irq,
2264 ath10k_pci_interrupt_handler,
2265 IRQF_SHARED, "ath10k_pci", ar);
Kalle Valof3782742013-10-17 11:36:15 +03002266 if (ret) {
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002267 ath10k_warn("failed to request MSI irq %d: %d\n",
2268 ar_pci->pdev->irq, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002269 return ret;
Kalle Valof3782742013-10-17 11:36:15 +03002270 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002271
Kalle Valo5e3dd152013-06-12 20:52:10 +03002272 return 0;
2273}
2274
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002275static int ath10k_pci_request_irq_legacy(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002276{
2277 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002278 int ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002279
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002280 ret = request_irq(ar_pci->pdev->irq,
2281 ath10k_pci_interrupt_handler,
2282 IRQF_SHARED, "ath10k_pci", ar);
Kalle Valof3782742013-10-17 11:36:15 +03002283 if (ret) {
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002284 ath10k_warn("failed to request legacy irq %d: %d\n",
2285 ar_pci->pdev->irq, ret);
Kalle Valof3782742013-10-17 11:36:15 +03002286 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002287 }
2288
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002289 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002290}
2291
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002292static int ath10k_pci_request_irq(struct ath10k *ar)
2293{
2294 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2295
2296 switch (ar_pci->num_msi_intrs) {
2297 case 0:
2298 return ath10k_pci_request_irq_legacy(ar);
2299 case 1:
2300 return ath10k_pci_request_irq_msi(ar);
2301 case MSI_NUM_REQUEST:
2302 return ath10k_pci_request_irq_msix(ar);
2303 }
2304
2305 ath10k_warn("unknown irq configuration upon request\n");
2306 return -EINVAL;
2307}
2308
2309static void ath10k_pci_free_irq(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002310{
2311 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2312 int i;
2313
2314 /* There's at least one interrupt irregardless whether its legacy INTR
2315 * or MSI or MSI-X */
2316 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
2317 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002318}
2319
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002320static void ath10k_pci_init_irq_tasklets(struct ath10k *ar)
2321{
2322 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2323 int i;
2324
2325 tasklet_init(&ar_pci->intr_tq, ath10k_pci_tasklet, (unsigned long)ar);
2326 tasklet_init(&ar_pci->msi_fw_err, ath10k_msi_err_tasklet,
2327 (unsigned long)ar);
Michal Kaziorab977bd2013-11-25 14:06:26 +01002328 tasklet_init(&ar_pci->early_irq_tasklet, ath10k_pci_early_irq_tasklet,
2329 (unsigned long)ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002330
2331 for (i = 0; i < CE_COUNT; i++) {
2332 ar_pci->pipe_info[i].ar_pci = ar_pci;
2333 tasklet_init(&ar_pci->pipe_info[i].intr, ath10k_pci_ce_tasklet,
2334 (unsigned long)&ar_pci->pipe_info[i]);
2335 }
2336}
2337
2338static int ath10k_pci_init_irq(struct ath10k *ar)
2339{
2340 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2341 int ret;
2342
2343 ath10k_pci_init_irq_tasklets(ar);
2344
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002345 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_AUTO &&
2346 !test_bit(ATH10K_FLAG_FIRST_BOOT_DONE, &ar->dev_flags))
2347 ath10k_info("limiting irq mode to: %d\n", ath10k_pci_irq_mode);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002348
2349 /* Try MSI-X */
Michal Kazior0edf2572014-08-07 11:03:29 +02002350 if (ath10k_pci_irq_mode == ATH10K_PCI_IRQ_AUTO) {
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002351 ar_pci->num_msi_intrs = MSI_NUM_REQUEST;
Alexander Gordeev5ad68672014-02-13 17:50:02 +02002352 ret = pci_enable_msi_range(ar_pci->pdev, ar_pci->num_msi_intrs,
2353 ar_pci->num_msi_intrs);
2354 if (ret > 0)
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002355 return 0;
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002356
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002357 /* fall-through */
2358 }
2359
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002360 /* Try MSI */
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002361 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_LEGACY) {
2362 ar_pci->num_msi_intrs = 1;
2363 ret = pci_enable_msi(ar_pci->pdev);
2364 if (ret == 0)
2365 return 0;
2366
2367 /* fall-through */
2368 }
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002369
2370 /* Try legacy irq
2371 *
2372 * A potential race occurs here: The CORE_BASE write
2373 * depends on target correctly decoding AXI address but
2374 * host won't know when target writes BAR to CORE_CTRL.
2375 * This write might get lost if target has NOT written BAR.
2376 * For now, fix the race by repeating the write in below
2377 * synchronization checking. */
2378 ar_pci->num_msi_intrs = 0;
2379
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002380 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2381 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002382
2383 return 0;
2384}
2385
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002386static void ath10k_pci_deinit_irq_legacy(struct ath10k *ar)
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002387{
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002388 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2389 0);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002390}
2391
2392static int ath10k_pci_deinit_irq(struct ath10k *ar)
2393{
2394 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2395
2396 switch (ar_pci->num_msi_intrs) {
2397 case 0:
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002398 ath10k_pci_deinit_irq_legacy(ar);
2399 return 0;
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002400 case 1:
2401 /* fall-through */
2402 case MSI_NUM_REQUEST:
2403 pci_disable_msi(ar_pci->pdev);
2404 return 0;
Alexander Gordeevbb8b6212014-02-13 17:50:01 +02002405 default:
2406 pci_disable_msi(ar_pci->pdev);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002407 }
2408
2409 ath10k_warn("unknown irq configuration upon deinit\n");
2410 return -EINVAL;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002411}
2412
Michal Kaziord7fb47f2013-11-08 08:01:26 +01002413static int ath10k_pci_wait_for_target_init(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002414{
2415 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo0399eca2014-03-28 09:32:21 +02002416 unsigned long timeout;
Kalle Valo0399eca2014-03-28 09:32:21 +02002417 u32 val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002418
Kalle Valo50f87a62014-03-28 09:32:52 +02002419 ath10k_dbg(ATH10K_DBG_BOOT, "boot waiting target to initialise\n");
2420
Kalle Valo0399eca2014-03-28 09:32:21 +02002421 timeout = jiffies + msecs_to_jiffies(ATH10K_PCI_TARGET_WAIT);
2422
2423 do {
2424 val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
2425
Kalle Valo50f87a62014-03-28 09:32:52 +02002426 ath10k_dbg(ATH10K_DBG_BOOT, "boot target indicator %x\n", val);
2427
Kalle Valo0399eca2014-03-28 09:32:21 +02002428 /* target should never return this */
2429 if (val == 0xffffffff)
2430 continue;
2431
Michal Kazior7710cd22014-04-23 19:30:04 +03002432 /* the device has crashed so don't bother trying anymore */
2433 if (val & FW_IND_EVENT_PENDING)
2434 break;
2435
Kalle Valo0399eca2014-03-28 09:32:21 +02002436 if (val & FW_IND_INITIALIZED)
2437 break;
2438
Kalle Valo5e3dd152013-06-12 20:52:10 +03002439 if (ar_pci->num_msi_intrs == 0)
2440 /* Fix potential race by repeating CORE_BASE writes */
Kalle Valo0399eca2014-03-28 09:32:21 +02002441 ath10k_pci_soc_write32(ar, PCIE_INTR_ENABLE_ADDRESS,
2442 PCIE_INTR_FIRMWARE_MASK |
2443 PCIE_INTR_CE_MASK_ALL);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002444
Kalle Valo0399eca2014-03-28 09:32:21 +02002445 mdelay(10);
2446 } while (time_before(jiffies, timeout));
2447
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002448 if (val == 0xffffffff) {
2449 ath10k_err("failed to read device register, device is gone\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002450 return -EIO;
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002451 }
2452
Michal Kazior7710cd22014-04-23 19:30:04 +03002453 if (val & FW_IND_EVENT_PENDING) {
2454 ath10k_warn("device has crashed during init\n");
Michal Kazior1a4ab282014-05-14 16:56:16 +03002455 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS,
2456 val & ~FW_IND_EVENT_PENDING);
2457 ath10k_pci_hif_dump_area(ar);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002458 return -ECOMM;
Michal Kazior7710cd22014-04-23 19:30:04 +03002459 }
2460
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002461 if (!(val & FW_IND_INITIALIZED)) {
Kalle Valo0399eca2014-03-28 09:32:21 +02002462 ath10k_err("failed to receive initialized event from target: %08x\n",
2463 val);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002464 return -ETIMEDOUT;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002465 }
2466
Kalle Valo50f87a62014-03-28 09:32:52 +02002467 ath10k_dbg(ATH10K_DBG_BOOT, "boot target initialised\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002468 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002469}
2470
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002471static int ath10k_pci_cold_reset(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002472{
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002473 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002474 u32 val;
2475
Kalle Valo50f87a62014-03-28 09:32:52 +02002476 ath10k_dbg(ATH10K_DBG_BOOT, "boot cold reset\n");
2477
Kalle Valo5e3dd152013-06-12 20:52:10 +03002478 /* Put Target, including PCIe, into RESET. */
Kalle Valoe479ed42013-09-01 10:01:53 +03002479 val = ath10k_pci_reg_read32(ar, SOC_GLOBAL_RESET_ADDRESS);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002480 val |= 1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002481 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002482
2483 for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
Kalle Valoe479ed42013-09-01 10:01:53 +03002484 if (ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03002485 RTC_STATE_COLD_RESET_MASK)
2486 break;
2487 msleep(1);
2488 }
2489
2490 /* Pull Target, including PCIe, out of RESET. */
2491 val &= ~1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002492 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002493
2494 for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
Kalle Valoe479ed42013-09-01 10:01:53 +03002495 if (!(ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03002496 RTC_STATE_COLD_RESET_MASK))
2497 break;
2498 msleep(1);
2499 }
2500
Kalle Valo50f87a62014-03-28 09:32:52 +02002501 ath10k_dbg(ATH10K_DBG_BOOT, "boot cold reset complete\n");
2502
Michal Kazior5b2589f2013-11-08 08:01:30 +01002503 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002504}
2505
Michal Kazior2986e3e2014-08-07 11:03:30 +02002506static int ath10k_pci_claim(struct ath10k *ar)
2507{
2508 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2509 struct pci_dev *pdev = ar_pci->pdev;
2510 u32 lcr_val;
2511 int ret;
2512
2513 pci_set_drvdata(pdev, ar);
2514
2515 ret = pci_enable_device(pdev);
2516 if (ret) {
2517 ath10k_err("failed to enable pci device: %d\n", ret);
2518 return ret;
2519 }
2520
2521 ret = pci_request_region(pdev, BAR_NUM, "ath");
2522 if (ret) {
2523 ath10k_err("failed to request region BAR%d: %d\n", BAR_NUM,
2524 ret);
2525 goto err_device;
2526 }
2527
2528 /* Target expects 32 bit DMA. Enforce it. */
2529 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2530 if (ret) {
2531 ath10k_err("failed to set dma mask to 32-bit: %d\n", ret);
2532 goto err_region;
2533 }
2534
2535 ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2536 if (ret) {
2537 ath10k_err("failed to set consistent dma mask to 32-bit: %d\n",
2538 ret);
2539 goto err_region;
2540 }
2541
2542 pci_set_master(pdev);
2543
2544 /* Workaround: Disable ASPM */
2545 pci_read_config_dword(pdev, 0x80, &lcr_val);
2546 pci_write_config_dword(pdev, 0x80, (lcr_val & 0xffffff00));
2547
2548 /* Arrange for access to Target SoC registers. */
2549 ar_pci->mem = pci_iomap(pdev, BAR_NUM, 0);
2550 if (!ar_pci->mem) {
2551 ath10k_err("failed to iomap BAR%d\n", BAR_NUM);
2552 ret = -EIO;
2553 goto err_master;
2554 }
2555
2556 ath10k_dbg(ATH10K_DBG_BOOT, "boot pci_mem 0x%p\n", ar_pci->mem);
2557 return 0;
2558
2559err_master:
2560 pci_clear_master(pdev);
2561
2562err_region:
2563 pci_release_region(pdev, BAR_NUM);
2564
2565err_device:
2566 pci_disable_device(pdev);
2567
2568 return ret;
2569}
2570
2571static void ath10k_pci_release(struct ath10k *ar)
2572{
2573 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2574 struct pci_dev *pdev = ar_pci->pdev;
2575
2576 pci_iounmap(pdev, ar_pci->mem);
2577 pci_release_region(pdev, BAR_NUM);
2578 pci_clear_master(pdev);
2579 pci_disable_device(pdev);
2580}
2581
Kalle Valo5e3dd152013-06-12 20:52:10 +03002582static int ath10k_pci_probe(struct pci_dev *pdev,
2583 const struct pci_device_id *pci_dev)
2584{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002585 int ret = 0;
2586 struct ath10k *ar;
2587 struct ath10k_pci *ar_pci;
Michal Kazior2986e3e2014-08-07 11:03:30 +02002588 u32 chip_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002589
Kalle Valo50f87a62014-03-28 09:32:52 +02002590 ath10k_dbg(ATH10K_DBG_PCI, "pci probe\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002591
Michal Kaziore7b54192014-08-07 11:03:27 +02002592 ar = ath10k_core_create(sizeof(*ar_pci), &pdev->dev,
2593 &ath10k_pci_hif_ops);
2594 if (!ar) {
2595 ath10k_err("failed to allocate core\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002596 return -ENOMEM;
Michal Kaziore7b54192014-08-07 11:03:27 +02002597 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002598
Michal Kaziore7b54192014-08-07 11:03:27 +02002599 ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002600 ar_pci->pdev = pdev;
2601 ar_pci->dev = &pdev->dev;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002602 ar_pci->ar = ar;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002603
Michal Kazior2986e3e2014-08-07 11:03:30 +02002604 spin_lock_init(&ar_pci->ce_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002605
Michal Kazior2986e3e2014-08-07 11:03:30 +02002606 ret = ath10k_pci_claim(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002607 if (ret) {
Michal Kazior2986e3e2014-08-07 11:03:30 +02002608 ath10k_err("failed to claim device: %d\n", ret);
Michal Kaziore7b54192014-08-07 11:03:27 +02002609 goto err_core_destroy;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002610 }
2611
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002612 ret = ath10k_pci_wake(ar);
Kalle Valoe01ae682013-09-01 11:22:14 +03002613 if (ret) {
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002614 ath10k_err("failed to wake up: %d\n", ret);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002615 goto err_release;
Kalle Valoe01ae682013-09-01 11:22:14 +03002616 }
2617
Kalle Valo233eb972013-10-16 16:46:11 +03002618 chip_id = ath10k_pci_soc_read32(ar, SOC_CHIP_ID_ADDRESS);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002619 if (chip_id == 0xffffffff) {
2620 ath10k_err("failed to get chip id\n");
2621 goto err_sleep;
2622 }
Kalle Valoe01ae682013-09-01 11:22:14 +03002623
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002624 ret = ath10k_pci_alloc_ce(ar);
2625 if (ret) {
2626 ath10k_err("failed to allocate copy engine pipes: %d\n", ret);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002627 goto err_sleep;
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002628 }
2629
Kalle Valoe01ae682013-09-01 11:22:14 +03002630 ret = ath10k_core_register(ar, chip_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002631 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002632 ath10k_err("failed to register driver core: %d\n", ret);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002633 goto err_free_ce;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002634 }
2635
2636 return 0;
2637
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002638err_free_ce:
2639 ath10k_pci_free_ce(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002640
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002641err_sleep:
2642 ath10k_pci_sleep(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002643
2644err_release:
2645 ath10k_pci_release(ar);
2646
Michal Kaziore7b54192014-08-07 11:03:27 +02002647err_core_destroy:
Kalle Valo5e3dd152013-06-12 20:52:10 +03002648 ath10k_core_destroy(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002649
2650 return ret;
2651}
2652
2653static void ath10k_pci_remove(struct pci_dev *pdev)
2654{
2655 struct ath10k *ar = pci_get_drvdata(pdev);
2656 struct ath10k_pci *ar_pci;
2657
Kalle Valo50f87a62014-03-28 09:32:52 +02002658 ath10k_dbg(ATH10K_DBG_PCI, "pci remove\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002659
2660 if (!ar)
2661 return;
2662
2663 ar_pci = ath10k_pci_priv(ar);
2664
2665 if (!ar_pci)
2666 return;
2667
Kalle Valo5e3dd152013-06-12 20:52:10 +03002668 ath10k_core_unregister(ar);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002669 ath10k_pci_free_ce(ar);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002670 ath10k_pci_sleep(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002671 ath10k_pci_release(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002672 ath10k_core_destroy(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002673}
2674
Kalle Valo5e3dd152013-06-12 20:52:10 +03002675MODULE_DEVICE_TABLE(pci, ath10k_pci_id_table);
2676
2677static struct pci_driver ath10k_pci_driver = {
2678 .name = "ath10k_pci",
2679 .id_table = ath10k_pci_id_table,
2680 .probe = ath10k_pci_probe,
2681 .remove = ath10k_pci_remove,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002682};
2683
2684static int __init ath10k_pci_init(void)
2685{
2686 int ret;
2687
2688 ret = pci_register_driver(&ath10k_pci_driver);
2689 if (ret)
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002690 ath10k_err("failed to register PCI driver: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002691
2692 return ret;
2693}
2694module_init(ath10k_pci_init);
2695
2696static void __exit ath10k_pci_exit(void)
2697{
2698 pci_unregister_driver(&ath10k_pci_driver);
2699}
2700
2701module_exit(ath10k_pci_exit);
2702
2703MODULE_AUTHOR("Qualcomm Atheros");
2704MODULE_DESCRIPTION("Driver support for Atheros QCA988X PCIe devices");
2705MODULE_LICENSE("Dual BSD/GPL");
Michal Kazior24c88f72014-07-25 13:32:17 +02002706MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_FW_3_FILE);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002707MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_BOARD_DATA_FILE);