blob: bc8c3e3345c1aa1443f9f1c3f1ab390d1d0ff1a6 [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
Ben Greear384914b2014-08-25 08:37:32 +0300835static void ath10k_pci_dump_registers(struct ath10k *ar,
836 struct ath10k_fw_crash_data *crash_data)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300837{
Ben Greear384914b2014-08-25 08:37:32 +0300838 u32 i, reg_dump_values[REG_DUMP_COUNT_QCA988X] = {};
Kalle Valo5e3dd152013-06-12 20:52:10 +0300839 int ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300840
Ben Greear384914b2014-08-25 08:37:32 +0300841 lockdep_assert_held(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300842
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300843 ret = ath10k_pci_diag_read_hi(ar, &reg_dump_values[0],
844 hi_failure_state,
845 REG_DUMP_COUNT_QCA988X * sizeof(u32));
Michal Kazior1d2b48d2013-11-08 08:01:34 +0100846 if (ret) {
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300847 ath10k_err("failed to read firmware dump area: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300848 return;
849 }
850
851 BUILD_BUG_ON(REG_DUMP_COUNT_QCA988X % 4);
852
Kalle Valo0e9848c2014-08-25 08:37:37 +0300853 ath10k_err("firmware register dump:\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +0300854 for (i = 0; i < REG_DUMP_COUNT_QCA988X; i += 4)
855 ath10k_err("[%02d]: 0x%08X 0x%08X 0x%08X 0x%08X\n",
856 i,
857 reg_dump_values[i],
858 reg_dump_values[i + 1],
859 reg_dump_values[i + 2],
860 reg_dump_values[i + 3]);
Michal Kazioraffd3212013-07-16 09:54:35 +0200861
Ben Greear384914b2014-08-25 08:37:32 +0300862 /* crash_data is in little endian */
863 for (i = 0; i < REG_DUMP_COUNT_QCA988X; i++)
864 crash_data->registers[i] = cpu_to_le32(reg_dump_values[i]);
865}
866
Kalle Valo0e9848c2014-08-25 08:37:37 +0300867static void ath10k_pci_fw_crashed_dump(struct ath10k *ar)
Ben Greear384914b2014-08-25 08:37:32 +0300868{
869 struct ath10k_fw_crash_data *crash_data;
870 char uuid[50];
871
872 spin_lock_bh(&ar->data_lock);
873
874 crash_data = ath10k_debug_get_new_fw_crash_data(ar);
875
876 if (crash_data)
877 scnprintf(uuid, sizeof(uuid), "%pUl", &crash_data->uuid);
878 else
879 scnprintf(uuid, sizeof(uuid), "n/a");
880
881 ath10k_err("firmware crashed! (uuid %s)\n", uuid);
882 ath10k_err("hardware name %s version 0x%x\n",
883 ar->hw_params.name, ar->target_version);
884 ath10k_err("firmware version: %s\n", ar->hw->wiphy->fw_version);
885
886 if (!crash_data)
887 goto exit;
888
889 ath10k_pci_dump_registers(ar, crash_data);
890
891exit:
892 spin_unlock_bh(&ar->data_lock);
893
Michal Kazior5e90de82013-10-16 16:46:05 +0300894 queue_work(ar->workqueue, &ar->restart_work);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300895}
896
897static void ath10k_pci_hif_send_complete_check(struct ath10k *ar, u8 pipe,
898 int force)
899{
Kalle Valo50f87a62014-03-28 09:32:52 +0200900 ath10k_dbg(ATH10K_DBG_PCI, "pci hif send complete check\n");
901
Kalle Valo5e3dd152013-06-12 20:52:10 +0300902 if (!force) {
903 int resources;
904 /*
905 * Decide whether to actually poll for completions, or just
906 * wait for a later chance.
907 * If there seem to be plenty of resources left, then just wait
908 * since checking involves reading a CE register, which is a
909 * relatively expensive operation.
910 */
911 resources = ath10k_pci_hif_get_free_queue_number(ar, pipe);
912
913 /*
914 * If at least 50% of the total resources are still available,
915 * don't bother checking again yet.
916 */
917 if (resources > (host_ce_config_wlan[pipe].src_nentries >> 1))
918 return;
919 }
920 ath10k_ce_per_engine_service(ar, pipe);
921}
922
Michal Kaziore799bbf2013-07-05 16:15:12 +0300923static void ath10k_pci_hif_set_callbacks(struct ath10k *ar,
924 struct ath10k_hif_cb *callbacks)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300925{
926 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
927
Kalle Valo50f87a62014-03-28 09:32:52 +0200928 ath10k_dbg(ATH10K_DBG_PCI, "pci hif set callbacks\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +0300929
930 memcpy(&ar_pci->msg_callbacks_current, callbacks,
931 sizeof(ar_pci->msg_callbacks_current));
932}
933
Michal Kaziorc80de122013-11-25 14:06:23 +0100934static int ath10k_pci_setup_ce_irq(struct ath10k *ar)
935{
936 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
937 const struct ce_attr *attr;
938 struct ath10k_pci_pipe *pipe_info;
939 int pipe_num, disable_interrupts;
940
941 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
942 pipe_info = &ar_pci->pipe_info[pipe_num];
943
944 /* Handle Diagnostic CE specially */
945 if (pipe_info->ce_hdl == ar_pci->ce_diag)
946 continue;
947
948 attr = &host_ce_config_wlan[pipe_num];
949
950 if (attr->src_nentries) {
951 disable_interrupts = attr->flags & CE_ATTR_DIS_INTR;
952 ath10k_ce_send_cb_register(pipe_info->ce_hdl,
953 ath10k_pci_ce_send_done,
954 disable_interrupts);
955 }
956
957 if (attr->dest_nentries)
958 ath10k_ce_recv_cb_register(pipe_info->ce_hdl,
959 ath10k_pci_ce_recv_data);
960 }
961
962 return 0;
963}
964
Michal Kazior96a9d0d2013-11-08 08:01:25 +0100965static void ath10k_pci_kill_tasklet(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300966{
967 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300968 int i;
969
Kalle Valo5e3dd152013-06-12 20:52:10 +0300970 tasklet_kill(&ar_pci->intr_tq);
Michal Kazior103d4f52013-11-08 08:01:24 +0100971 tasklet_kill(&ar_pci->msi_fw_err);
Michal Kaziorab977bd2013-11-25 14:06:26 +0100972 tasklet_kill(&ar_pci->early_irq_tasklet);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300973
974 for (i = 0; i < CE_COUNT; i++)
975 tasklet_kill(&ar_pci->pipe_info[i].intr);
Michal Kazior96a9d0d2013-11-08 08:01:25 +0100976}
977
Kalle Valo5e3dd152013-06-12 20:52:10 +0300978/* TODO - temporary mapping while we have too few CE's */
979static int ath10k_pci_hif_map_service_to_pipe(struct ath10k *ar,
980 u16 service_id, u8 *ul_pipe,
981 u8 *dl_pipe, int *ul_is_polled,
982 int *dl_is_polled)
983{
984 int ret = 0;
985
Kalle Valo50f87a62014-03-28 09:32:52 +0200986 ath10k_dbg(ATH10K_DBG_PCI, "pci hif map service\n");
987
Kalle Valo5e3dd152013-06-12 20:52:10 +0300988 /* polling for received messages not supported */
989 *dl_is_polled = 0;
990
991 switch (service_id) {
992 case ATH10K_HTC_SVC_ID_HTT_DATA_MSG:
993 /*
994 * Host->target HTT gets its own pipe, so it can be polled
995 * while other pipes are interrupt driven.
996 */
997 *ul_pipe = 4;
998 /*
999 * Use the same target->host pipe for HTC ctrl, HTC raw
1000 * streams, and HTT.
1001 */
1002 *dl_pipe = 1;
1003 break;
1004
1005 case ATH10K_HTC_SVC_ID_RSVD_CTRL:
1006 case ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS:
1007 /*
1008 * Note: HTC_RAW_STREAMS_SVC is currently unused, and
1009 * HTC_CTRL_RSVD_SVC could share the same pipe as the
1010 * WMI services. So, if another CE is needed, change
1011 * this to *ul_pipe = 3, which frees up CE 0.
1012 */
1013 /* *ul_pipe = 3; */
1014 *ul_pipe = 0;
1015 *dl_pipe = 1;
1016 break;
1017
1018 case ATH10K_HTC_SVC_ID_WMI_DATA_BK:
1019 case ATH10K_HTC_SVC_ID_WMI_DATA_BE:
1020 case ATH10K_HTC_SVC_ID_WMI_DATA_VI:
1021 case ATH10K_HTC_SVC_ID_WMI_DATA_VO:
1022
1023 case ATH10K_HTC_SVC_ID_WMI_CONTROL:
1024 *ul_pipe = 3;
1025 *dl_pipe = 2;
1026 break;
1027
1028 /* pipe 5 unused */
1029 /* pipe 6 reserved */
1030 /* pipe 7 reserved */
1031
1032 default:
1033 ret = -1;
1034 break;
1035 }
1036 *ul_is_polled =
1037 (host_ce_config_wlan[*ul_pipe].flags & CE_ATTR_DIS_INTR) != 0;
1038
1039 return ret;
1040}
1041
1042static void ath10k_pci_hif_get_default_pipe(struct ath10k *ar,
1043 u8 *ul_pipe, u8 *dl_pipe)
1044{
1045 int ul_is_polled, dl_is_polled;
1046
Kalle Valo50f87a62014-03-28 09:32:52 +02001047 ath10k_dbg(ATH10K_DBG_PCI, "pci hif get default pipe\n");
1048
Kalle Valo5e3dd152013-06-12 20:52:10 +03001049 (void)ath10k_pci_hif_map_service_to_pipe(ar,
1050 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1051 ul_pipe,
1052 dl_pipe,
1053 &ul_is_polled,
1054 &dl_is_polled);
1055}
1056
Michal Kazior87263e52013-08-27 13:08:01 +02001057static int ath10k_pci_post_rx_pipe(struct ath10k_pci_pipe *pipe_info,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001058 int num)
1059{
1060 struct ath10k *ar = pipe_info->hif_ce_state;
1061 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2aa39112013-08-27 13:08:02 +02001062 struct ath10k_ce_pipe *ce_state = pipe_info->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001063 struct sk_buff *skb;
1064 dma_addr_t ce_data;
1065 int i, ret = 0;
1066
1067 if (pipe_info->buf_sz == 0)
1068 return 0;
1069
1070 for (i = 0; i < num; i++) {
1071 skb = dev_alloc_skb(pipe_info->buf_sz);
1072 if (!skb) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001073 ath10k_warn("failed to allocate skbuff for pipe %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001074 num);
1075 ret = -ENOMEM;
1076 goto err;
1077 }
1078
1079 WARN_ONCE((unsigned long)skb->data & 3, "unaligned skb");
1080
1081 ce_data = dma_map_single(ar->dev, skb->data,
1082 skb->len + skb_tailroom(skb),
1083 DMA_FROM_DEVICE);
1084
1085 if (unlikely(dma_mapping_error(ar->dev, ce_data))) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001086 ath10k_warn("failed to DMA map sk_buff\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001087 dev_kfree_skb_any(skb);
1088 ret = -EIO;
1089 goto err;
1090 }
1091
1092 ATH10K_SKB_CB(skb)->paddr = ce_data;
1093
1094 pci_dma_sync_single_for_device(ar_pci->pdev, ce_data,
1095 pipe_info->buf_sz,
1096 PCI_DMA_FROMDEVICE);
1097
1098 ret = ath10k_ce_recv_buf_enqueue(ce_state, (void *)skb,
1099 ce_data);
1100 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001101 ath10k_warn("failed to enqueue to pipe %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001102 num, ret);
1103 goto err;
1104 }
1105 }
1106
1107 return ret;
1108
1109err:
1110 ath10k_pci_rx_pipe_cleanup(pipe_info);
1111 return ret;
1112}
1113
1114static int ath10k_pci_post_rx(struct ath10k *ar)
1115{
1116 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +02001117 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001118 const struct ce_attr *attr;
1119 int pipe_num, ret = 0;
1120
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001121 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001122 pipe_info = &ar_pci->pipe_info[pipe_num];
1123 attr = &host_ce_config_wlan[pipe_num];
1124
1125 if (attr->dest_nentries == 0)
1126 continue;
1127
1128 ret = ath10k_pci_post_rx_pipe(pipe_info,
1129 attr->dest_nentries - 1);
1130 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001131 ath10k_warn("failed to post RX buffer for pipe %d: %d\n",
1132 pipe_num, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001133
1134 for (; pipe_num >= 0; pipe_num--) {
1135 pipe_info = &ar_pci->pipe_info[pipe_num];
1136 ath10k_pci_rx_pipe_cleanup(pipe_info);
1137 }
1138 return ret;
1139 }
1140 }
1141
1142 return 0;
1143}
1144
1145static int ath10k_pci_hif_start(struct ath10k *ar)
1146{
1147 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorab977bd2013-11-25 14:06:26 +01001148 int ret, ret_early;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001149
Kalle Valo50f87a62014-03-28 09:32:52 +02001150 ath10k_dbg(ATH10K_DBG_BOOT, "boot hif start\n");
1151
Michal Kaziorab977bd2013-11-25 14:06:26 +01001152 ath10k_pci_free_early_irq(ar);
1153 ath10k_pci_kill_tasklet(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001154
Michal Kazior5d1aa942013-11-25 14:06:24 +01001155 ret = ath10k_pci_request_irq(ar);
1156 if (ret) {
1157 ath10k_warn("failed to post RX buffers for all pipes: %d\n",
1158 ret);
Michal Kazior2f5280d2014-02-27 18:50:05 +02001159 goto err_early_irq;
Michal Kazior5d1aa942013-11-25 14:06:24 +01001160 }
1161
Michal Kaziorc80de122013-11-25 14:06:23 +01001162 ret = ath10k_pci_setup_ce_irq(ar);
1163 if (ret) {
1164 ath10k_warn("failed to setup CE interrupts: %d\n", ret);
Michal Kazior5d1aa942013-11-25 14:06:24 +01001165 goto err_stop;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001166 }
1167
1168 /* Post buffers once to start things off. */
1169 ret = ath10k_pci_post_rx(ar);
1170 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001171 ath10k_warn("failed to post RX buffers for all pipes: %d\n",
1172 ret);
Michal Kazior5d1aa942013-11-25 14:06:24 +01001173 goto err_stop;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001174 }
1175
1176 ar_pci->started = 1;
1177 return 0;
Michal Kaziorc80de122013-11-25 14:06:23 +01001178
Michal Kazior5d1aa942013-11-25 14:06:24 +01001179err_stop:
1180 ath10k_ce_disable_interrupts(ar);
1181 ath10k_pci_free_irq(ar);
1182 ath10k_pci_kill_tasklet(ar);
Michal Kaziorab977bd2013-11-25 14:06:26 +01001183err_early_irq:
1184 /* Though there should be no interrupts (device was reset)
1185 * power_down() expects the early IRQ to be installed as per the
1186 * driver lifecycle. */
1187 ret_early = ath10k_pci_request_early_irq(ar);
1188 if (ret_early)
1189 ath10k_warn("failed to re-enable early irq: %d\n", ret_early);
1190
Michal Kaziorc80de122013-11-25 14:06:23 +01001191 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001192}
1193
Michal Kazior87263e52013-08-27 13:08:01 +02001194static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe *pipe_info)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001195{
1196 struct ath10k *ar;
1197 struct ath10k_pci *ar_pci;
Michal Kazior2aa39112013-08-27 13:08:02 +02001198 struct ath10k_ce_pipe *ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001199 u32 buf_sz;
1200 struct sk_buff *netbuf;
1201 u32 ce_data;
1202
1203 buf_sz = pipe_info->buf_sz;
1204
1205 /* Unused Copy Engine */
1206 if (buf_sz == 0)
1207 return;
1208
1209 ar = pipe_info->hif_ce_state;
1210 ar_pci = ath10k_pci_priv(ar);
1211
1212 if (!ar_pci->started)
1213 return;
1214
1215 ce_hdl = pipe_info->ce_hdl;
1216
1217 while (ath10k_ce_revoke_recv_next(ce_hdl, (void **)&netbuf,
1218 &ce_data) == 0) {
1219 dma_unmap_single(ar->dev, ATH10K_SKB_CB(netbuf)->paddr,
1220 netbuf->len + skb_tailroom(netbuf),
1221 DMA_FROM_DEVICE);
1222 dev_kfree_skb_any(netbuf);
1223 }
1224}
1225
Michal Kazior87263e52013-08-27 13:08:01 +02001226static void ath10k_pci_tx_pipe_cleanup(struct ath10k_pci_pipe *pipe_info)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001227{
1228 struct ath10k *ar;
1229 struct ath10k_pci *ar_pci;
Michal Kazior2aa39112013-08-27 13:08:02 +02001230 struct ath10k_ce_pipe *ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001231 struct sk_buff *netbuf;
1232 u32 ce_data;
1233 unsigned int nbytes;
1234 unsigned int id;
1235 u32 buf_sz;
1236
1237 buf_sz = pipe_info->buf_sz;
1238
1239 /* Unused Copy Engine */
1240 if (buf_sz == 0)
1241 return;
1242
1243 ar = pipe_info->hif_ce_state;
1244 ar_pci = ath10k_pci_priv(ar);
1245
1246 if (!ar_pci->started)
1247 return;
1248
1249 ce_hdl = pipe_info->ce_hdl;
1250
1251 while (ath10k_ce_cancel_send_next(ce_hdl, (void **)&netbuf,
1252 &ce_data, &nbytes, &id) == 0) {
Michal Kaziora16942e2014-02-27 18:50:04 +02001253 /* no need to call tx completion for NULL pointers */
1254 if (!netbuf)
Michal Kazior2415fc12013-11-08 08:01:32 +01001255 continue;
Michal Kazior2415fc12013-11-08 08:01:32 +01001256
Kalle Valoe9bb0aa2013-09-08 18:36:11 +03001257 ar_pci->msg_callbacks_current.tx_completion(ar,
1258 netbuf,
1259 id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001260 }
1261}
1262
1263/*
1264 * Cleanup residual buffers for device shutdown:
1265 * buffers that were enqueued for receive
1266 * buffers that were to be sent
1267 * Note: Buffers that had completed but which were
1268 * not yet processed are on a completion queue. They
1269 * are handled when the completion thread shuts down.
1270 */
1271static void ath10k_pci_buffer_cleanup(struct ath10k *ar)
1272{
1273 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1274 int pipe_num;
1275
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001276 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Michal Kazior87263e52013-08-27 13:08:01 +02001277 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001278
1279 pipe_info = &ar_pci->pipe_info[pipe_num];
1280 ath10k_pci_rx_pipe_cleanup(pipe_info);
1281 ath10k_pci_tx_pipe_cleanup(pipe_info);
1282 }
1283}
1284
1285static void ath10k_pci_ce_deinit(struct ath10k *ar)
1286{
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001287 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001288
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001289 for (i = 0; i < CE_COUNT; i++)
1290 ath10k_ce_deinit_pipe(ar, i);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001291}
1292
1293static void ath10k_pci_hif_stop(struct ath10k *ar)
1294{
Michal Kazior32270b62013-08-02 09:15:47 +02001295 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior5d1aa942013-11-25 14:06:24 +01001296 int ret;
Michal Kazior32270b62013-08-02 09:15:47 +02001297
Kalle Valo50f87a62014-03-28 09:32:52 +02001298 ath10k_dbg(ATH10K_DBG_BOOT, "boot hif stop\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001299
Michal Kaziorf2708be2014-05-16 17:15:39 +03001300 if (WARN_ON(!ar_pci->started))
1301 return;
1302
Michal Kazior5d1aa942013-11-25 14:06:24 +01001303 ret = ath10k_ce_disable_interrupts(ar);
1304 if (ret)
1305 ath10k_warn("failed to disable CE interrupts: %d\n", ret);
Michal Kazior32270b62013-08-02 09:15:47 +02001306
Michal Kazior5d1aa942013-11-25 14:06:24 +01001307 ath10k_pci_free_irq(ar);
1308 ath10k_pci_kill_tasklet(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001309
Michal Kaziorab977bd2013-11-25 14:06:26 +01001310 ret = ath10k_pci_request_early_irq(ar);
1311 if (ret)
1312 ath10k_warn("failed to re-enable early irq: %d\n", ret);
1313
Kalle Valo5e3dd152013-06-12 20:52:10 +03001314 /* At this point, asynchronous threads are stopped, the target should
1315 * not DMA nor interrupt. We process the leftovers and then free
1316 * everything else up. */
1317
Kalle Valo5e3dd152013-06-12 20:52:10 +03001318 ath10k_pci_buffer_cleanup(ar);
Michal Kazior32270b62013-08-02 09:15:47 +02001319
Michal Kazior6a42a472013-11-08 08:01:35 +01001320 /* Make the sure the device won't access any structures on the host by
1321 * resetting it. The device was fed with PCI CE ringbuffer
1322 * configuration during init. If ringbuffers are freed and the device
1323 * were to access them this could lead to memory corruption on the
1324 * host. */
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001325 ath10k_pci_warm_reset(ar);
Michal Kazior6a42a472013-11-08 08:01:35 +01001326
Michal Kazior32270b62013-08-02 09:15:47 +02001327 ar_pci->started = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001328}
1329
1330static int ath10k_pci_hif_exchange_bmi_msg(struct ath10k *ar,
1331 void *req, u32 req_len,
1332 void *resp, u32 *resp_len)
1333{
1334 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2aa39112013-08-27 13:08:02 +02001335 struct ath10k_pci_pipe *pci_tx = &ar_pci->pipe_info[BMI_CE_NUM_TO_TARG];
1336 struct ath10k_pci_pipe *pci_rx = &ar_pci->pipe_info[BMI_CE_NUM_TO_HOST];
1337 struct ath10k_ce_pipe *ce_tx = pci_tx->ce_hdl;
1338 struct ath10k_ce_pipe *ce_rx = pci_rx->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001339 dma_addr_t req_paddr = 0;
1340 dma_addr_t resp_paddr = 0;
1341 struct bmi_xfer xfer = {};
1342 void *treq, *tresp = NULL;
1343 int ret = 0;
1344
Michal Kazior85622cd2013-11-25 14:06:22 +01001345 might_sleep();
1346
Kalle Valo5e3dd152013-06-12 20:52:10 +03001347 if (resp && !resp_len)
1348 return -EINVAL;
1349
1350 if (resp && resp_len && *resp_len == 0)
1351 return -EINVAL;
1352
1353 treq = kmemdup(req, req_len, GFP_KERNEL);
1354 if (!treq)
1355 return -ENOMEM;
1356
1357 req_paddr = dma_map_single(ar->dev, treq, req_len, DMA_TO_DEVICE);
1358 ret = dma_mapping_error(ar->dev, req_paddr);
1359 if (ret)
1360 goto err_dma;
1361
1362 if (resp && resp_len) {
1363 tresp = kzalloc(*resp_len, GFP_KERNEL);
1364 if (!tresp) {
1365 ret = -ENOMEM;
1366 goto err_req;
1367 }
1368
1369 resp_paddr = dma_map_single(ar->dev, tresp, *resp_len,
1370 DMA_FROM_DEVICE);
1371 ret = dma_mapping_error(ar->dev, resp_paddr);
1372 if (ret)
1373 goto err_req;
1374
1375 xfer.wait_for_resp = true;
1376 xfer.resp_len = 0;
1377
1378 ath10k_ce_recv_buf_enqueue(ce_rx, &xfer, resp_paddr);
1379 }
1380
Kalle Valo5e3dd152013-06-12 20:52:10 +03001381 ret = ath10k_ce_send(ce_tx, &xfer, req_paddr, req_len, -1, 0);
1382 if (ret)
1383 goto err_resp;
1384
Michal Kazior85622cd2013-11-25 14:06:22 +01001385 ret = ath10k_pci_bmi_wait(ce_tx, ce_rx, &xfer);
1386 if (ret) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001387 u32 unused_buffer;
1388 unsigned int unused_nbytes;
1389 unsigned int unused_id;
1390
Kalle Valo5e3dd152013-06-12 20:52:10 +03001391 ath10k_ce_cancel_send_next(ce_tx, NULL, &unused_buffer,
1392 &unused_nbytes, &unused_id);
1393 } else {
1394 /* non-zero means we did not time out */
1395 ret = 0;
1396 }
1397
1398err_resp:
1399 if (resp) {
1400 u32 unused_buffer;
1401
1402 ath10k_ce_revoke_recv_next(ce_rx, NULL, &unused_buffer);
1403 dma_unmap_single(ar->dev, resp_paddr,
1404 *resp_len, DMA_FROM_DEVICE);
1405 }
1406err_req:
1407 dma_unmap_single(ar->dev, req_paddr, req_len, DMA_TO_DEVICE);
1408
1409 if (ret == 0 && resp_len) {
1410 *resp_len = min(*resp_len, xfer.resp_len);
1411 memcpy(resp, tresp, xfer.resp_len);
1412 }
1413err_dma:
1414 kfree(treq);
1415 kfree(tresp);
1416
1417 return ret;
1418}
1419
Michal Kazior5440ce22013-09-03 15:09:58 +02001420static void ath10k_pci_bmi_send_done(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001421{
Michal Kazior5440ce22013-09-03 15:09:58 +02001422 struct bmi_xfer *xfer;
1423 u32 ce_data;
1424 unsigned int nbytes;
1425 unsigned int transfer_id;
1426
1427 if (ath10k_ce_completed_send_next(ce_state, (void **)&xfer, &ce_data,
1428 &nbytes, &transfer_id))
1429 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001430
Michal Kazior2374b182014-07-14 16:25:25 +03001431 xfer->tx_done = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001432}
1433
Michal Kazior5440ce22013-09-03 15:09:58 +02001434static void ath10k_pci_bmi_recv_data(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001435{
Michal Kazior5440ce22013-09-03 15:09:58 +02001436 struct bmi_xfer *xfer;
1437 u32 ce_data;
1438 unsigned int nbytes;
1439 unsigned int transfer_id;
1440 unsigned int flags;
1441
1442 if (ath10k_ce_completed_recv_next(ce_state, (void **)&xfer, &ce_data,
1443 &nbytes, &transfer_id, &flags))
1444 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001445
1446 if (!xfer->wait_for_resp) {
1447 ath10k_warn("unexpected: BMI data received; ignoring\n");
1448 return;
1449 }
1450
1451 xfer->resp_len = nbytes;
Michal Kazior2374b182014-07-14 16:25:25 +03001452 xfer->rx_done = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001453}
1454
Michal Kazior85622cd2013-11-25 14:06:22 +01001455static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
1456 struct ath10k_ce_pipe *rx_pipe,
1457 struct bmi_xfer *xfer)
1458{
1459 unsigned long timeout = jiffies + BMI_COMMUNICATION_TIMEOUT_HZ;
1460
1461 while (time_before_eq(jiffies, timeout)) {
1462 ath10k_pci_bmi_send_done(tx_pipe);
1463 ath10k_pci_bmi_recv_data(rx_pipe);
1464
Michal Kazior2374b182014-07-14 16:25:25 +03001465 if (xfer->tx_done && (xfer->rx_done == xfer->wait_for_resp))
Michal Kazior85622cd2013-11-25 14:06:22 +01001466 return 0;
1467
1468 schedule();
1469 }
1470
1471 return -ETIMEDOUT;
1472}
1473
Kalle Valo5e3dd152013-06-12 20:52:10 +03001474/*
1475 * Map from service/endpoint to Copy Engine.
1476 * This table is derived from the CE_PCI TABLE, above.
1477 * It is passed to the Target at startup for use by firmware.
1478 */
1479static const struct service_to_pipe target_service_to_ce_map_wlan[] = {
1480 {
1481 ATH10K_HTC_SVC_ID_WMI_DATA_VO,
1482 PIPEDIR_OUT, /* out = UL = host -> target */
1483 3,
1484 },
1485 {
1486 ATH10K_HTC_SVC_ID_WMI_DATA_VO,
1487 PIPEDIR_IN, /* in = DL = target -> host */
1488 2,
1489 },
1490 {
1491 ATH10K_HTC_SVC_ID_WMI_DATA_BK,
1492 PIPEDIR_OUT, /* out = UL = host -> target */
1493 3,
1494 },
1495 {
1496 ATH10K_HTC_SVC_ID_WMI_DATA_BK,
1497 PIPEDIR_IN, /* in = DL = target -> host */
1498 2,
1499 },
1500 {
1501 ATH10K_HTC_SVC_ID_WMI_DATA_BE,
1502 PIPEDIR_OUT, /* out = UL = host -> target */
1503 3,
1504 },
1505 {
1506 ATH10K_HTC_SVC_ID_WMI_DATA_BE,
1507 PIPEDIR_IN, /* in = DL = target -> host */
1508 2,
1509 },
1510 {
1511 ATH10K_HTC_SVC_ID_WMI_DATA_VI,
1512 PIPEDIR_OUT, /* out = UL = host -> target */
1513 3,
1514 },
1515 {
1516 ATH10K_HTC_SVC_ID_WMI_DATA_VI,
1517 PIPEDIR_IN, /* in = DL = target -> host */
1518 2,
1519 },
1520 {
1521 ATH10K_HTC_SVC_ID_WMI_CONTROL,
1522 PIPEDIR_OUT, /* out = UL = host -> target */
1523 3,
1524 },
1525 {
1526 ATH10K_HTC_SVC_ID_WMI_CONTROL,
1527 PIPEDIR_IN, /* in = DL = target -> host */
1528 2,
1529 },
1530 {
1531 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1532 PIPEDIR_OUT, /* out = UL = host -> target */
1533 0, /* could be moved to 3 (share with WMI) */
1534 },
1535 {
1536 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1537 PIPEDIR_IN, /* in = DL = target -> host */
1538 1,
1539 },
1540 {
1541 ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS, /* not currently used */
1542 PIPEDIR_OUT, /* out = UL = host -> target */
1543 0,
1544 },
1545 {
1546 ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS, /* not currently used */
1547 PIPEDIR_IN, /* in = DL = target -> host */
1548 1,
1549 },
1550 {
1551 ATH10K_HTC_SVC_ID_HTT_DATA_MSG,
1552 PIPEDIR_OUT, /* out = UL = host -> target */
1553 4,
1554 },
1555 {
1556 ATH10K_HTC_SVC_ID_HTT_DATA_MSG,
1557 PIPEDIR_IN, /* in = DL = target -> host */
1558 1,
1559 },
1560
1561 /* (Additions here) */
1562
1563 { /* Must be last */
1564 0,
1565 0,
1566 0,
1567 },
1568};
1569
1570/*
1571 * Send an interrupt to the device to wake up the Target CPU
1572 * so it has an opportunity to notice any changed state.
1573 */
1574static int ath10k_pci_wake_target_cpu(struct ath10k *ar)
1575{
1576 int ret;
1577 u32 core_ctrl;
1578
1579 ret = ath10k_pci_diag_read_access(ar, SOC_CORE_BASE_ADDRESS |
1580 CORE_CTRL_ADDRESS,
1581 &core_ctrl);
1582 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001583 ath10k_warn("failed to read core_ctrl: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001584 return ret;
1585 }
1586
1587 /* A_INUM_FIRMWARE interrupt to Target CPU */
1588 core_ctrl |= CORE_CTRL_CPU_INTR_MASK;
1589
1590 ret = ath10k_pci_diag_write_access(ar, SOC_CORE_BASE_ADDRESS |
1591 CORE_CTRL_ADDRESS,
1592 core_ctrl);
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001593 if (ret) {
1594 ath10k_warn("failed to set target CPU interrupt mask: %d\n",
1595 ret);
1596 return ret;
1597 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001598
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001599 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001600}
1601
1602static int ath10k_pci_init_config(struct ath10k *ar)
1603{
1604 u32 interconnect_targ_addr;
1605 u32 pcie_state_targ_addr = 0;
1606 u32 pipe_cfg_targ_addr = 0;
1607 u32 svc_to_pipe_map = 0;
1608 u32 pcie_config_flags = 0;
1609 u32 ealloc_value;
1610 u32 ealloc_targ_addr;
1611 u32 flag2_value;
1612 u32 flag2_targ_addr;
1613 int ret = 0;
1614
1615 /* Download to Target the CE Config and the service-to-CE map */
1616 interconnect_targ_addr =
1617 host_interest_item_address(HI_ITEM(hi_interconnect_state));
1618
1619 /* Supply Target-side CE configuration */
1620 ret = ath10k_pci_diag_read_access(ar, interconnect_targ_addr,
1621 &pcie_state_targ_addr);
1622 if (ret != 0) {
1623 ath10k_err("Failed to get pcie state addr: %d\n", ret);
1624 return ret;
1625 }
1626
1627 if (pcie_state_targ_addr == 0) {
1628 ret = -EIO;
1629 ath10k_err("Invalid pcie state addr\n");
1630 return ret;
1631 }
1632
1633 ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
1634 offsetof(struct pcie_state,
1635 pipe_cfg_addr),
1636 &pipe_cfg_targ_addr);
1637 if (ret != 0) {
1638 ath10k_err("Failed to get pipe cfg addr: %d\n", ret);
1639 return ret;
1640 }
1641
1642 if (pipe_cfg_targ_addr == 0) {
1643 ret = -EIO;
1644 ath10k_err("Invalid pipe cfg addr\n");
1645 return ret;
1646 }
1647
1648 ret = ath10k_pci_diag_write_mem(ar, pipe_cfg_targ_addr,
1649 target_ce_config_wlan,
1650 sizeof(target_ce_config_wlan));
1651
1652 if (ret != 0) {
1653 ath10k_err("Failed to write pipe cfg: %d\n", ret);
1654 return ret;
1655 }
1656
1657 ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
1658 offsetof(struct pcie_state,
1659 svc_to_pipe_map),
1660 &svc_to_pipe_map);
1661 if (ret != 0) {
1662 ath10k_err("Failed to get svc/pipe map: %d\n", ret);
1663 return ret;
1664 }
1665
1666 if (svc_to_pipe_map == 0) {
1667 ret = -EIO;
1668 ath10k_err("Invalid svc_to_pipe map\n");
1669 return ret;
1670 }
1671
1672 ret = ath10k_pci_diag_write_mem(ar, svc_to_pipe_map,
1673 target_service_to_ce_map_wlan,
1674 sizeof(target_service_to_ce_map_wlan));
1675 if (ret != 0) {
1676 ath10k_err("Failed to write svc/pipe map: %d\n", ret);
1677 return ret;
1678 }
1679
1680 ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
1681 offsetof(struct pcie_state,
1682 config_flags),
1683 &pcie_config_flags);
1684 if (ret != 0) {
1685 ath10k_err("Failed to get pcie config_flags: %d\n", ret);
1686 return ret;
1687 }
1688
1689 pcie_config_flags &= ~PCIE_CONFIG_FLAG_ENABLE_L1;
1690
1691 ret = ath10k_pci_diag_write_mem(ar, pcie_state_targ_addr +
1692 offsetof(struct pcie_state, config_flags),
1693 &pcie_config_flags,
1694 sizeof(pcie_config_flags));
1695 if (ret != 0) {
1696 ath10k_err("Failed to write pcie config_flags: %d\n", ret);
1697 return ret;
1698 }
1699
1700 /* configure early allocation */
1701 ealloc_targ_addr = host_interest_item_address(HI_ITEM(hi_early_alloc));
1702
1703 ret = ath10k_pci_diag_read_access(ar, ealloc_targ_addr, &ealloc_value);
1704 if (ret != 0) {
1705 ath10k_err("Faile to get early alloc val: %d\n", ret);
1706 return ret;
1707 }
1708
1709 /* first bank is switched to IRAM */
1710 ealloc_value |= ((HI_EARLY_ALLOC_MAGIC << HI_EARLY_ALLOC_MAGIC_SHIFT) &
1711 HI_EARLY_ALLOC_MAGIC_MASK);
1712 ealloc_value |= ((1 << HI_EARLY_ALLOC_IRAM_BANKS_SHIFT) &
1713 HI_EARLY_ALLOC_IRAM_BANKS_MASK);
1714
1715 ret = ath10k_pci_diag_write_access(ar, ealloc_targ_addr, ealloc_value);
1716 if (ret != 0) {
1717 ath10k_err("Failed to set early alloc val: %d\n", ret);
1718 return ret;
1719 }
1720
1721 /* Tell Target to proceed with initialization */
1722 flag2_targ_addr = host_interest_item_address(HI_ITEM(hi_option_flag2));
1723
1724 ret = ath10k_pci_diag_read_access(ar, flag2_targ_addr, &flag2_value);
1725 if (ret != 0) {
1726 ath10k_err("Failed to get option val: %d\n", ret);
1727 return ret;
1728 }
1729
1730 flag2_value |= HI_OPTION_EARLY_CFG_DONE;
1731
1732 ret = ath10k_pci_diag_write_access(ar, flag2_targ_addr, flag2_value);
1733 if (ret != 0) {
1734 ath10k_err("Failed to set option val: %d\n", ret);
1735 return ret;
1736 }
1737
1738 return 0;
1739}
1740
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001741static int ath10k_pci_alloc_ce(struct ath10k *ar)
1742{
1743 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001744
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001745 for (i = 0; i < CE_COUNT; i++) {
1746 ret = ath10k_ce_alloc_pipe(ar, i, &host_ce_config_wlan[i]);
1747 if (ret) {
1748 ath10k_err("failed to allocate copy engine pipe %d: %d\n",
1749 i, ret);
1750 return ret;
1751 }
1752 }
1753
1754 return 0;
1755}
1756
1757static void ath10k_pci_free_ce(struct ath10k *ar)
1758{
1759 int i;
1760
1761 for (i = 0; i < CE_COUNT; i++)
1762 ath10k_ce_free_pipe(ar, i);
1763}
Kalle Valo5e3dd152013-06-12 20:52:10 +03001764
1765static int ath10k_pci_ce_init(struct ath10k *ar)
1766{
1767 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +02001768 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001769 const struct ce_attr *attr;
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001770 int pipe_num, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001771
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001772 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001773 pipe_info = &ar_pci->pipe_info[pipe_num];
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001774 pipe_info->ce_hdl = &ar_pci->ce_states[pipe_num];
Kalle Valo5e3dd152013-06-12 20:52:10 +03001775 pipe_info->pipe_num = pipe_num;
1776 pipe_info->hif_ce_state = ar;
1777 attr = &host_ce_config_wlan[pipe_num];
1778
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001779 ret = ath10k_ce_init_pipe(ar, pipe_num, attr);
1780 if (ret) {
1781 ath10k_err("failed to initialize copy engine pipe %d: %d\n",
1782 pipe_num, ret);
1783 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001784 }
1785
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001786 if (pipe_num == CE_COUNT - 1) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001787 /*
1788 * Reserve the ultimate CE for
1789 * diagnostic Window support
1790 */
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001791 ar_pci->ce_diag = pipe_info->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001792 continue;
1793 }
1794
1795 pipe_info->buf_sz = (size_t) (attr->src_sz_max);
1796 }
1797
Kalle Valo5e3dd152013-06-12 20:52:10 +03001798 return 0;
1799}
1800
1801static void ath10k_pci_fw_interrupt_handler(struct ath10k *ar)
1802{
1803 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valob39712c2014-03-28 09:32:46 +02001804 u32 fw_indicator;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001805
Kalle Valob39712c2014-03-28 09:32:46 +02001806 fw_indicator = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001807
1808 if (fw_indicator & FW_IND_EVENT_PENDING) {
1809 /* ACK: clear Target-side pending event */
Kalle Valob39712c2014-03-28 09:32:46 +02001810 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001811 fw_indicator & ~FW_IND_EVENT_PENDING);
1812
1813 if (ar_pci->started) {
Kalle Valo0e9848c2014-08-25 08:37:37 +03001814 ath10k_pci_fw_crashed_dump(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001815 } else {
1816 /*
1817 * Probable Target failure before we're prepared
1818 * to handle it. Generally unexpected.
1819 */
1820 ath10k_warn("early firmware event indicated\n");
1821 }
1822 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001823}
1824
Michal Kaziorde013572014-05-14 16:56:16 +03001825/* this function effectively clears target memory controller assert line */
1826static void ath10k_pci_warm_reset_si0(struct ath10k *ar)
1827{
1828 u32 val;
1829
1830 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1831 ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
1832 val | SOC_RESET_CONTROL_SI0_RST_MASK);
1833 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1834
1835 msleep(10);
1836
1837 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1838 ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
1839 val & ~SOC_RESET_CONTROL_SI0_RST_MASK);
1840 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1841
1842 msleep(10);
1843}
1844
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001845static int ath10k_pci_warm_reset(struct ath10k *ar)
1846{
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001847 u32 val;
1848
Kalle Valo50f87a62014-03-28 09:32:52 +02001849 ath10k_dbg(ATH10K_DBG_BOOT, "boot warm reset\n");
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001850
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001851 /* debug */
1852 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1853 PCIE_INTR_CAUSE_ADDRESS);
1854 ath10k_dbg(ATH10K_DBG_BOOT, "boot host cpu intr cause: 0x%08x\n", val);
1855
1856 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1857 CPU_INTR_ADDRESS);
1858 ath10k_dbg(ATH10K_DBG_BOOT, "boot target cpu intr cause: 0x%08x\n",
1859 val);
1860
1861 /* disable pending irqs */
1862 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
1863 PCIE_INTR_ENABLE_ADDRESS, 0);
1864
1865 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
1866 PCIE_INTR_CLR_ADDRESS, ~0);
1867
1868 msleep(100);
1869
1870 /* clear fw indicator */
Kalle Valob39712c2014-03-28 09:32:46 +02001871 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, 0);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001872
1873 /* clear target LF timer interrupts */
1874 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1875 SOC_LF_TIMER_CONTROL0_ADDRESS);
1876 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS +
1877 SOC_LF_TIMER_CONTROL0_ADDRESS,
1878 val & ~SOC_LF_TIMER_CONTROL0_ENABLE_MASK);
1879
1880 /* reset CE */
1881 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1882 SOC_RESET_CONTROL_ADDRESS);
1883 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1884 val | SOC_RESET_CONTROL_CE_RST_MASK);
1885 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1886 SOC_RESET_CONTROL_ADDRESS);
1887 msleep(10);
1888
1889 /* unreset CE */
1890 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1891 val & ~SOC_RESET_CONTROL_CE_RST_MASK);
1892 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1893 SOC_RESET_CONTROL_ADDRESS);
1894 msleep(10);
1895
Michal Kaziorde013572014-05-14 16:56:16 +03001896 ath10k_pci_warm_reset_si0(ar);
1897
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001898 /* debug */
1899 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1900 PCIE_INTR_CAUSE_ADDRESS);
1901 ath10k_dbg(ATH10K_DBG_BOOT, "boot host cpu intr cause: 0x%08x\n", val);
1902
1903 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1904 CPU_INTR_ADDRESS);
1905 ath10k_dbg(ATH10K_DBG_BOOT, "boot target cpu intr cause: 0x%08x\n",
1906 val);
1907
1908 /* CPU warm reset */
1909 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1910 SOC_RESET_CONTROL_ADDRESS);
1911 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1912 val | SOC_RESET_CONTROL_CPU_WARM_RST_MASK);
1913
1914 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1915 SOC_RESET_CONTROL_ADDRESS);
1916 ath10k_dbg(ATH10K_DBG_BOOT, "boot target reset state: 0x%08x\n", val);
1917
1918 msleep(100);
1919
1920 ath10k_dbg(ATH10K_DBG_BOOT, "boot warm reset complete\n");
1921
Michal Kaziorc0c378f2014-08-07 11:03:28 +02001922 return 0;
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001923}
1924
1925static int __ath10k_pci_hif_power_up(struct ath10k *ar, bool cold_reset)
Michal Kazior8c5c5362013-07-16 09:38:50 +02001926{
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02001927 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo95cbb6a2013-11-20 10:00:35 +02001928 const char *irq_mode;
Michal Kazior8c5c5362013-07-16 09:38:50 +02001929 int ret;
1930
1931 /*
1932 * Bring the target up cleanly.
1933 *
1934 * The target may be in an undefined state with an AUX-powered Target
1935 * and a Host in WoW mode. If the Host crashes, loses power, or is
1936 * restarted (without unloading the driver) then the Target is left
1937 * (aux) powered and running. On a subsequent driver load, the Target
1938 * is in an unexpected state. We try to catch that here in order to
1939 * reset the Target and retry the probe.
1940 */
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001941 if (cold_reset)
1942 ret = ath10k_pci_cold_reset(ar);
1943 else
1944 ret = ath10k_pci_warm_reset(ar);
1945
Michal Kazior5b2589f2013-11-08 08:01:30 +01001946 if (ret) {
1947 ath10k_err("failed to reset target: %d\n", ret);
Michal Kazior98563d52013-11-08 08:01:33 +01001948 goto err;
Michal Kazior5b2589f2013-11-08 08:01:30 +01001949 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02001950
Michal Kazior8c5c5362013-07-16 09:38:50 +02001951 ret = ath10k_pci_ce_init(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001952 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001953 ath10k_err("failed to initialize CE: %d\n", ret);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02001954 goto err;
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001955 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02001956
Michal Kazior98563d52013-11-08 08:01:33 +01001957 ret = ath10k_ce_disable_interrupts(ar);
1958 if (ret) {
1959 ath10k_err("failed to disable CE interrupts: %d\n", ret);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001960 goto err_ce;
1961 }
1962
Michal Kaziorfc15ca12013-11-25 14:06:21 +01001963 ret = ath10k_pci_init_irq(ar);
Michal Kazior98563d52013-11-08 08:01:33 +01001964 if (ret) {
Michal Kaziorfc15ca12013-11-25 14:06:21 +01001965 ath10k_err("failed to init irqs: %d\n", ret);
Michal Kazior98563d52013-11-08 08:01:33 +01001966 goto err_ce;
1967 }
1968
Michal Kaziorab977bd2013-11-25 14:06:26 +01001969 ret = ath10k_pci_request_early_irq(ar);
1970 if (ret) {
1971 ath10k_err("failed to request early irq: %d\n", ret);
1972 goto err_deinit_irq;
1973 }
1974
Michal Kazior98563d52013-11-08 08:01:33 +01001975 ret = ath10k_pci_wait_for_target_init(ar);
1976 if (ret) {
1977 ath10k_err("failed to wait for target to init: %d\n", ret);
Michal Kaziorab977bd2013-11-25 14:06:26 +01001978 goto err_free_early_irq;
Michal Kazior98563d52013-11-08 08:01:33 +01001979 }
1980
1981 ret = ath10k_pci_init_config(ar);
1982 if (ret) {
1983 ath10k_err("failed to setup init config: %d\n", ret);
Michal Kaziorab977bd2013-11-25 14:06:26 +01001984 goto err_free_early_irq;
Michal Kazior98563d52013-11-08 08:01:33 +01001985 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02001986
1987 ret = ath10k_pci_wake_target_cpu(ar);
1988 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001989 ath10k_err("could not wake up target CPU: %d\n", ret);
Michal Kaziorab977bd2013-11-25 14:06:26 +01001990 goto err_free_early_irq;
Michal Kazior8c5c5362013-07-16 09:38:50 +02001991 }
1992
Kalle Valo95cbb6a2013-11-20 10:00:35 +02001993 if (ar_pci->num_msi_intrs > 1)
1994 irq_mode = "MSI-X";
1995 else if (ar_pci->num_msi_intrs == 1)
1996 irq_mode = "MSI";
1997 else
1998 irq_mode = "legacy";
1999
Kalle Valo650b91f2013-11-20 10:00:49 +02002000 if (!test_bit(ATH10K_FLAG_FIRST_BOOT_DONE, &ar->dev_flags))
Kalle Valo78a9cb42014-03-28 09:32:58 +02002001 ath10k_info("pci irq %s irq_mode %d reset_mode %d\n",
2002 irq_mode, ath10k_pci_irq_mode,
2003 ath10k_pci_reset_mode);
Kalle Valo95cbb6a2013-11-20 10:00:35 +02002004
Michal Kazior8c5c5362013-07-16 09:38:50 +02002005 return 0;
2006
Michal Kaziorab977bd2013-11-25 14:06:26 +01002007err_free_early_irq:
2008 ath10k_pci_free_early_irq(ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002009err_deinit_irq:
2010 ath10k_pci_deinit_irq(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02002011err_ce:
2012 ath10k_pci_ce_deinit(ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002013 ath10k_pci_warm_reset(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02002014err:
2015 return ret;
2016}
2017
Michal Kazior61c95ce2014-05-14 16:56:16 +03002018static int ath10k_pci_hif_power_up_warm(struct ath10k *ar)
2019{
2020 int i, ret;
2021
2022 /*
2023 * Sometime warm reset succeeds after retries.
2024 *
2025 * FIXME: It might be possible to tune ath10k_pci_warm_reset() to work
2026 * at first try.
2027 */
2028 for (i = 0; i < ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS; i++) {
2029 ret = __ath10k_pci_hif_power_up(ar, false);
2030 if (ret == 0)
2031 break;
2032
2033 ath10k_warn("failed to warm reset (attempt %d out of %d): %d\n",
2034 i + 1, ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS, ret);
2035 }
2036
2037 return ret;
2038}
2039
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002040static int ath10k_pci_hif_power_up(struct ath10k *ar)
2041{
2042 int ret;
2043
Kalle Valo50f87a62014-03-28 09:32:52 +02002044 ath10k_dbg(ATH10K_DBG_BOOT, "boot hif power up\n");
2045
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002046 /*
2047 * Hardware CUS232 version 2 has some issues with cold reset and the
2048 * preferred (and safer) way to perform a device reset is through a
2049 * warm reset.
2050 *
Michal Kazior61c95ce2014-05-14 16:56:16 +03002051 * Warm reset doesn't always work though so fall back to cold reset may
2052 * be necessary.
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002053 */
Michal Kazior61c95ce2014-05-14 16:56:16 +03002054 ret = ath10k_pci_hif_power_up_warm(ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002055 if (ret) {
Kalle Valo35098462014-03-28 09:32:27 +02002056 ath10k_warn("failed to power up target using warm reset: %d\n",
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002057 ret);
2058
Kalle Valo35098462014-03-28 09:32:27 +02002059 if (ath10k_pci_reset_mode == ATH10K_PCI_RESET_WARM_ONLY)
2060 return ret;
2061
2062 ath10k_warn("trying cold reset\n");
2063
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002064 ret = __ath10k_pci_hif_power_up(ar, true);
2065 if (ret) {
2066 ath10k_err("failed to power up target using cold reset too (%d)\n",
2067 ret);
2068 return ret;
2069 }
2070 }
2071
2072 return 0;
2073}
2074
Michal Kazior8c5c5362013-07-16 09:38:50 +02002075static void ath10k_pci_hif_power_down(struct ath10k *ar)
2076{
Kalle Valo50f87a62014-03-28 09:32:52 +02002077 ath10k_dbg(ATH10K_DBG_BOOT, "boot hif power down\n");
2078
Michal Kaziorab977bd2013-11-25 14:06:26 +01002079 ath10k_pci_free_early_irq(ar);
2080 ath10k_pci_kill_tasklet(ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002081 ath10k_pci_deinit_irq(ar);
Michal Kaziordf5e8522014-03-28 10:02:45 +02002082 ath10k_pci_ce_deinit(ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002083 ath10k_pci_warm_reset(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02002084}
2085
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002086#ifdef CONFIG_PM
2087
2088#define ATH10K_PCI_PM_CONTROL 0x44
2089
2090static int ath10k_pci_hif_suspend(struct ath10k *ar)
2091{
2092 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2093 struct pci_dev *pdev = ar_pci->pdev;
2094 u32 val;
2095
2096 pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
2097
2098 if ((val & 0x000000ff) != 0x3) {
2099 pci_save_state(pdev);
2100 pci_disable_device(pdev);
2101 pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
2102 (val & 0xffffff00) | 0x03);
2103 }
2104
2105 return 0;
2106}
2107
2108static int ath10k_pci_hif_resume(struct ath10k *ar)
2109{
2110 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2111 struct pci_dev *pdev = ar_pci->pdev;
2112 u32 val;
2113
2114 pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
2115
2116 if ((val & 0x000000ff) != 0) {
2117 pci_restore_state(pdev);
2118 pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
2119 val & 0xffffff00);
2120 /*
2121 * Suspend/Resume resets the PCI configuration space,
2122 * so we have to re-disable the RETRY_TIMEOUT register (0x41)
2123 * to keep PCI Tx retries from interfering with C3 CPU state
2124 */
2125 pci_read_config_dword(pdev, 0x40, &val);
2126
2127 if ((val & 0x0000ff00) != 0)
2128 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
2129 }
2130
2131 return 0;
2132}
2133#endif
2134
Kalle Valo5e3dd152013-06-12 20:52:10 +03002135static const struct ath10k_hif_ops ath10k_pci_hif_ops = {
Michal Kazior726346f2014-02-27 18:50:04 +02002136 .tx_sg = ath10k_pci_hif_tx_sg,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002137 .exchange_bmi_msg = ath10k_pci_hif_exchange_bmi_msg,
2138 .start = ath10k_pci_hif_start,
2139 .stop = ath10k_pci_hif_stop,
2140 .map_service_to_pipe = ath10k_pci_hif_map_service_to_pipe,
2141 .get_default_pipe = ath10k_pci_hif_get_default_pipe,
2142 .send_complete_check = ath10k_pci_hif_send_complete_check,
Michal Kaziore799bbf2013-07-05 16:15:12 +03002143 .set_callbacks = ath10k_pci_hif_set_callbacks,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002144 .get_free_queue_number = ath10k_pci_hif_get_free_queue_number,
Michal Kazior8c5c5362013-07-16 09:38:50 +02002145 .power_up = ath10k_pci_hif_power_up,
2146 .power_down = ath10k_pci_hif_power_down,
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002147#ifdef CONFIG_PM
2148 .suspend = ath10k_pci_hif_suspend,
2149 .resume = ath10k_pci_hif_resume,
2150#endif
Kalle Valo5e3dd152013-06-12 20:52:10 +03002151};
2152
2153static void ath10k_pci_ce_tasklet(unsigned long ptr)
2154{
Michal Kazior87263e52013-08-27 13:08:01 +02002155 struct ath10k_pci_pipe *pipe = (struct ath10k_pci_pipe *)ptr;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002156 struct ath10k_pci *ar_pci = pipe->ar_pci;
2157
2158 ath10k_ce_per_engine_service(ar_pci->ar, pipe->pipe_num);
2159}
2160
2161static void ath10k_msi_err_tasklet(unsigned long data)
2162{
2163 struct ath10k *ar = (struct ath10k *)data;
2164
2165 ath10k_pci_fw_interrupt_handler(ar);
2166}
2167
2168/*
2169 * Handler for a per-engine interrupt on a PARTICULAR CE.
2170 * This is used in cases where each CE has a private MSI interrupt.
2171 */
2172static irqreturn_t ath10k_pci_per_engine_handler(int irq, void *arg)
2173{
2174 struct ath10k *ar = arg;
2175 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2176 int ce_id = irq - ar_pci->pdev->irq - MSI_ASSIGN_CE_INITIAL;
2177
Dan Carpentere5742672013-06-18 10:28:46 +03002178 if (ce_id < 0 || ce_id >= ARRAY_SIZE(ar_pci->pipe_info)) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03002179 ath10k_warn("unexpected/invalid irq %d ce_id %d\n", irq, ce_id);
2180 return IRQ_HANDLED;
2181 }
2182
2183 /*
2184 * NOTE: We are able to derive ce_id from irq because we
2185 * use a one-to-one mapping for CE's 0..5.
2186 * CE's 6 & 7 do not use interrupts at all.
2187 *
2188 * This mapping must be kept in sync with the mapping
2189 * used by firmware.
2190 */
2191 tasklet_schedule(&ar_pci->pipe_info[ce_id].intr);
2192 return IRQ_HANDLED;
2193}
2194
2195static irqreturn_t ath10k_pci_msi_fw_handler(int irq, void *arg)
2196{
2197 struct ath10k *ar = arg;
2198 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2199
2200 tasklet_schedule(&ar_pci->msi_fw_err);
2201 return IRQ_HANDLED;
2202}
2203
2204/*
2205 * Top-level interrupt handler for all PCI interrupts from a Target.
2206 * When a block of MSI interrupts is allocated, this top-level handler
2207 * is not used; instead, we directly call the correct sub-handler.
2208 */
2209static irqreturn_t ath10k_pci_interrupt_handler(int irq, void *arg)
2210{
2211 struct ath10k *ar = arg;
2212 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2213
2214 if (ar_pci->num_msi_intrs == 0) {
Michal Kaziore5398872013-11-25 14:06:20 +01002215 if (!ath10k_pci_irq_pending(ar))
2216 return IRQ_NONE;
2217
Michal Kazior26852182013-11-25 14:06:25 +01002218 ath10k_pci_disable_and_clear_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002219 }
2220
2221 tasklet_schedule(&ar_pci->intr_tq);
2222
2223 return IRQ_HANDLED;
2224}
2225
Michal Kaziorab977bd2013-11-25 14:06:26 +01002226static void ath10k_pci_early_irq_tasklet(unsigned long data)
2227{
2228 struct ath10k *ar = (struct ath10k *)data;
Michal Kaziorab977bd2013-11-25 14:06:26 +01002229 u32 fw_ind;
Michal Kaziorab977bd2013-11-25 14:06:26 +01002230
Kalle Valob39712c2014-03-28 09:32:46 +02002231 fw_ind = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
Michal Kaziorab977bd2013-11-25 14:06:26 +01002232 if (fw_ind & FW_IND_EVENT_PENDING) {
Kalle Valob39712c2014-03-28 09:32:46 +02002233 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS,
Michal Kaziorab977bd2013-11-25 14:06:26 +01002234 fw_ind & ~FW_IND_EVENT_PENDING);
Kalle Valo0e9848c2014-08-25 08:37:37 +03002235 ath10k_pci_fw_crashed_dump(ar);
Michal Kaziorab977bd2013-11-25 14:06:26 +01002236 }
2237
Michal Kaziorab977bd2013-11-25 14:06:26 +01002238 ath10k_pci_enable_legacy_irq(ar);
2239}
2240
Kalle Valo5e3dd152013-06-12 20:52:10 +03002241static void ath10k_pci_tasklet(unsigned long data)
2242{
2243 struct ath10k *ar = (struct ath10k *)data;
2244 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2245
2246 ath10k_pci_fw_interrupt_handler(ar); /* FIXME: Handle FW error */
2247 ath10k_ce_per_engine_service_any(ar);
2248
Michal Kazior26852182013-11-25 14:06:25 +01002249 /* Re-enable legacy irq that was disabled in the irq handler */
2250 if (ar_pci->num_msi_intrs == 0)
2251 ath10k_pci_enable_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002252}
2253
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002254static int ath10k_pci_request_irq_msix(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002255{
2256 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002257 int ret, i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002258
2259 ret = request_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW,
2260 ath10k_pci_msi_fw_handler,
2261 IRQF_SHARED, "ath10k_pci", ar);
Michal Kazior591ecdb2013-07-31 10:55:15 +02002262 if (ret) {
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002263 ath10k_warn("failed to request MSI-X fw irq %d: %d\n",
Michal Kazior591ecdb2013-07-31 10:55:15 +02002264 ar_pci->pdev->irq + MSI_ASSIGN_FW, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002265 return ret;
Michal Kazior591ecdb2013-07-31 10:55:15 +02002266 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002267
2268 for (i = MSI_ASSIGN_CE_INITIAL; i <= MSI_ASSIGN_CE_MAX; i++) {
2269 ret = request_irq(ar_pci->pdev->irq + i,
2270 ath10k_pci_per_engine_handler,
2271 IRQF_SHARED, "ath10k_pci", ar);
2272 if (ret) {
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002273 ath10k_warn("failed to request MSI-X ce irq %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002274 ar_pci->pdev->irq + i, ret);
2275
Michal Kazior87b14232013-06-26 08:50:50 +02002276 for (i--; i >= MSI_ASSIGN_CE_INITIAL; i--)
2277 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002278
Michal Kazior87b14232013-06-26 08:50:50 +02002279 free_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002280 return ret;
2281 }
2282 }
2283
Kalle Valo5e3dd152013-06-12 20:52:10 +03002284 return 0;
2285}
2286
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002287static int ath10k_pci_request_irq_msi(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002288{
2289 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2290 int ret;
2291
2292 ret = request_irq(ar_pci->pdev->irq,
2293 ath10k_pci_interrupt_handler,
2294 IRQF_SHARED, "ath10k_pci", ar);
Kalle Valof3782742013-10-17 11:36:15 +03002295 if (ret) {
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002296 ath10k_warn("failed to request MSI irq %d: %d\n",
2297 ar_pci->pdev->irq, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002298 return ret;
Kalle Valof3782742013-10-17 11:36:15 +03002299 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002300
Kalle Valo5e3dd152013-06-12 20:52:10 +03002301 return 0;
2302}
2303
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002304static int ath10k_pci_request_irq_legacy(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002305{
2306 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002307 int ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002308
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002309 ret = request_irq(ar_pci->pdev->irq,
2310 ath10k_pci_interrupt_handler,
2311 IRQF_SHARED, "ath10k_pci", ar);
Kalle Valof3782742013-10-17 11:36:15 +03002312 if (ret) {
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002313 ath10k_warn("failed to request legacy irq %d: %d\n",
2314 ar_pci->pdev->irq, ret);
Kalle Valof3782742013-10-17 11:36:15 +03002315 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002316 }
2317
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002318 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002319}
2320
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002321static int ath10k_pci_request_irq(struct ath10k *ar)
2322{
2323 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2324
2325 switch (ar_pci->num_msi_intrs) {
2326 case 0:
2327 return ath10k_pci_request_irq_legacy(ar);
2328 case 1:
2329 return ath10k_pci_request_irq_msi(ar);
2330 case MSI_NUM_REQUEST:
2331 return ath10k_pci_request_irq_msix(ar);
2332 }
2333
2334 ath10k_warn("unknown irq configuration upon request\n");
2335 return -EINVAL;
2336}
2337
2338static void ath10k_pci_free_irq(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002339{
2340 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2341 int i;
2342
2343 /* There's at least one interrupt irregardless whether its legacy INTR
2344 * or MSI or MSI-X */
2345 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
2346 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002347}
2348
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002349static void ath10k_pci_init_irq_tasklets(struct ath10k *ar)
2350{
2351 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2352 int i;
2353
2354 tasklet_init(&ar_pci->intr_tq, ath10k_pci_tasklet, (unsigned long)ar);
2355 tasklet_init(&ar_pci->msi_fw_err, ath10k_msi_err_tasklet,
2356 (unsigned long)ar);
Michal Kaziorab977bd2013-11-25 14:06:26 +01002357 tasklet_init(&ar_pci->early_irq_tasklet, ath10k_pci_early_irq_tasklet,
2358 (unsigned long)ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002359
2360 for (i = 0; i < CE_COUNT; i++) {
2361 ar_pci->pipe_info[i].ar_pci = ar_pci;
2362 tasklet_init(&ar_pci->pipe_info[i].intr, ath10k_pci_ce_tasklet,
2363 (unsigned long)&ar_pci->pipe_info[i]);
2364 }
2365}
2366
2367static int ath10k_pci_init_irq(struct ath10k *ar)
2368{
2369 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2370 int ret;
2371
2372 ath10k_pci_init_irq_tasklets(ar);
2373
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002374 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_AUTO &&
2375 !test_bit(ATH10K_FLAG_FIRST_BOOT_DONE, &ar->dev_flags))
2376 ath10k_info("limiting irq mode to: %d\n", ath10k_pci_irq_mode);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002377
2378 /* Try MSI-X */
Michal Kazior0edf2572014-08-07 11:03:29 +02002379 if (ath10k_pci_irq_mode == ATH10K_PCI_IRQ_AUTO) {
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002380 ar_pci->num_msi_intrs = MSI_NUM_REQUEST;
Alexander Gordeev5ad68672014-02-13 17:50:02 +02002381 ret = pci_enable_msi_range(ar_pci->pdev, ar_pci->num_msi_intrs,
2382 ar_pci->num_msi_intrs);
2383 if (ret > 0)
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002384 return 0;
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002385
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002386 /* fall-through */
2387 }
2388
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002389 /* Try MSI */
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002390 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_LEGACY) {
2391 ar_pci->num_msi_intrs = 1;
2392 ret = pci_enable_msi(ar_pci->pdev);
2393 if (ret == 0)
2394 return 0;
2395
2396 /* fall-through */
2397 }
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002398
2399 /* Try legacy irq
2400 *
2401 * A potential race occurs here: The CORE_BASE write
2402 * depends on target correctly decoding AXI address but
2403 * host won't know when target writes BAR to CORE_CTRL.
2404 * This write might get lost if target has NOT written BAR.
2405 * For now, fix the race by repeating the write in below
2406 * synchronization checking. */
2407 ar_pci->num_msi_intrs = 0;
2408
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002409 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2410 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002411
2412 return 0;
2413}
2414
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002415static void ath10k_pci_deinit_irq_legacy(struct ath10k *ar)
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002416{
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002417 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2418 0);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002419}
2420
2421static int ath10k_pci_deinit_irq(struct ath10k *ar)
2422{
2423 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2424
2425 switch (ar_pci->num_msi_intrs) {
2426 case 0:
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002427 ath10k_pci_deinit_irq_legacy(ar);
2428 return 0;
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002429 case 1:
2430 /* fall-through */
2431 case MSI_NUM_REQUEST:
2432 pci_disable_msi(ar_pci->pdev);
2433 return 0;
Alexander Gordeevbb8b6212014-02-13 17:50:01 +02002434 default:
2435 pci_disable_msi(ar_pci->pdev);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002436 }
2437
2438 ath10k_warn("unknown irq configuration upon deinit\n");
2439 return -EINVAL;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002440}
2441
Michal Kaziord7fb47f2013-11-08 08:01:26 +01002442static int ath10k_pci_wait_for_target_init(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002443{
2444 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo0399eca2014-03-28 09:32:21 +02002445 unsigned long timeout;
Kalle Valo0399eca2014-03-28 09:32:21 +02002446 u32 val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002447
Kalle Valo50f87a62014-03-28 09:32:52 +02002448 ath10k_dbg(ATH10K_DBG_BOOT, "boot waiting target to initialise\n");
2449
Kalle Valo0399eca2014-03-28 09:32:21 +02002450 timeout = jiffies + msecs_to_jiffies(ATH10K_PCI_TARGET_WAIT);
2451
2452 do {
2453 val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
2454
Kalle Valo50f87a62014-03-28 09:32:52 +02002455 ath10k_dbg(ATH10K_DBG_BOOT, "boot target indicator %x\n", val);
2456
Kalle Valo0399eca2014-03-28 09:32:21 +02002457 /* target should never return this */
2458 if (val == 0xffffffff)
2459 continue;
2460
Michal Kazior7710cd22014-04-23 19:30:04 +03002461 /* the device has crashed so don't bother trying anymore */
2462 if (val & FW_IND_EVENT_PENDING)
2463 break;
2464
Kalle Valo0399eca2014-03-28 09:32:21 +02002465 if (val & FW_IND_INITIALIZED)
2466 break;
2467
Kalle Valo5e3dd152013-06-12 20:52:10 +03002468 if (ar_pci->num_msi_intrs == 0)
2469 /* Fix potential race by repeating CORE_BASE writes */
Kalle Valo0399eca2014-03-28 09:32:21 +02002470 ath10k_pci_soc_write32(ar, PCIE_INTR_ENABLE_ADDRESS,
2471 PCIE_INTR_FIRMWARE_MASK |
2472 PCIE_INTR_CE_MASK_ALL);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002473
Kalle Valo0399eca2014-03-28 09:32:21 +02002474 mdelay(10);
2475 } while (time_before(jiffies, timeout));
2476
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002477 if (val == 0xffffffff) {
2478 ath10k_err("failed to read device register, device is gone\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002479 return -EIO;
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002480 }
2481
Michal Kazior7710cd22014-04-23 19:30:04 +03002482 if (val & FW_IND_EVENT_PENDING) {
2483 ath10k_warn("device has crashed during init\n");
Michal Kazior1a4ab282014-05-14 16:56:16 +03002484 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS,
2485 val & ~FW_IND_EVENT_PENDING);
Kalle Valo0e9848c2014-08-25 08:37:37 +03002486 ath10k_pci_fw_crashed_dump(ar);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002487 return -ECOMM;
Michal Kazior7710cd22014-04-23 19:30:04 +03002488 }
2489
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002490 if (!(val & FW_IND_INITIALIZED)) {
Kalle Valo0399eca2014-03-28 09:32:21 +02002491 ath10k_err("failed to receive initialized event from target: %08x\n",
2492 val);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002493 return -ETIMEDOUT;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002494 }
2495
Kalle Valo50f87a62014-03-28 09:32:52 +02002496 ath10k_dbg(ATH10K_DBG_BOOT, "boot target initialised\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002497 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002498}
2499
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002500static int ath10k_pci_cold_reset(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002501{
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002502 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002503 u32 val;
2504
Kalle Valo50f87a62014-03-28 09:32:52 +02002505 ath10k_dbg(ATH10K_DBG_BOOT, "boot cold reset\n");
2506
Kalle Valo5e3dd152013-06-12 20:52:10 +03002507 /* Put Target, including PCIe, into RESET. */
Kalle Valoe479ed42013-09-01 10:01:53 +03002508 val = ath10k_pci_reg_read32(ar, SOC_GLOBAL_RESET_ADDRESS);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002509 val |= 1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002510 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002511
2512 for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
Kalle Valoe479ed42013-09-01 10:01:53 +03002513 if (ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03002514 RTC_STATE_COLD_RESET_MASK)
2515 break;
2516 msleep(1);
2517 }
2518
2519 /* Pull Target, including PCIe, out of RESET. */
2520 val &= ~1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002521 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002522
2523 for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
Kalle Valoe479ed42013-09-01 10:01:53 +03002524 if (!(ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03002525 RTC_STATE_COLD_RESET_MASK))
2526 break;
2527 msleep(1);
2528 }
2529
Kalle Valo50f87a62014-03-28 09:32:52 +02002530 ath10k_dbg(ATH10K_DBG_BOOT, "boot cold reset complete\n");
2531
Michal Kazior5b2589f2013-11-08 08:01:30 +01002532 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002533}
2534
Michal Kazior2986e3e2014-08-07 11:03:30 +02002535static int ath10k_pci_claim(struct ath10k *ar)
2536{
2537 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2538 struct pci_dev *pdev = ar_pci->pdev;
2539 u32 lcr_val;
2540 int ret;
2541
2542 pci_set_drvdata(pdev, ar);
2543
2544 ret = pci_enable_device(pdev);
2545 if (ret) {
2546 ath10k_err("failed to enable pci device: %d\n", ret);
2547 return ret;
2548 }
2549
2550 ret = pci_request_region(pdev, BAR_NUM, "ath");
2551 if (ret) {
2552 ath10k_err("failed to request region BAR%d: %d\n", BAR_NUM,
2553 ret);
2554 goto err_device;
2555 }
2556
2557 /* Target expects 32 bit DMA. Enforce it. */
2558 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2559 if (ret) {
2560 ath10k_err("failed to set dma mask to 32-bit: %d\n", ret);
2561 goto err_region;
2562 }
2563
2564 ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2565 if (ret) {
2566 ath10k_err("failed to set consistent dma mask to 32-bit: %d\n",
2567 ret);
2568 goto err_region;
2569 }
2570
2571 pci_set_master(pdev);
2572
2573 /* Workaround: Disable ASPM */
2574 pci_read_config_dword(pdev, 0x80, &lcr_val);
2575 pci_write_config_dword(pdev, 0x80, (lcr_val & 0xffffff00));
2576
2577 /* Arrange for access to Target SoC registers. */
2578 ar_pci->mem = pci_iomap(pdev, BAR_NUM, 0);
2579 if (!ar_pci->mem) {
2580 ath10k_err("failed to iomap BAR%d\n", BAR_NUM);
2581 ret = -EIO;
2582 goto err_master;
2583 }
2584
2585 ath10k_dbg(ATH10K_DBG_BOOT, "boot pci_mem 0x%p\n", ar_pci->mem);
2586 return 0;
2587
2588err_master:
2589 pci_clear_master(pdev);
2590
2591err_region:
2592 pci_release_region(pdev, BAR_NUM);
2593
2594err_device:
2595 pci_disable_device(pdev);
2596
2597 return ret;
2598}
2599
2600static void ath10k_pci_release(struct ath10k *ar)
2601{
2602 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2603 struct pci_dev *pdev = ar_pci->pdev;
2604
2605 pci_iounmap(pdev, ar_pci->mem);
2606 pci_release_region(pdev, BAR_NUM);
2607 pci_clear_master(pdev);
2608 pci_disable_device(pdev);
2609}
2610
Kalle Valo5e3dd152013-06-12 20:52:10 +03002611static int ath10k_pci_probe(struct pci_dev *pdev,
2612 const struct pci_device_id *pci_dev)
2613{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002614 int ret = 0;
2615 struct ath10k *ar;
2616 struct ath10k_pci *ar_pci;
Michal Kazior2986e3e2014-08-07 11:03:30 +02002617 u32 chip_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002618
Kalle Valo50f87a62014-03-28 09:32:52 +02002619 ath10k_dbg(ATH10K_DBG_PCI, "pci probe\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002620
Michal Kaziore7b54192014-08-07 11:03:27 +02002621 ar = ath10k_core_create(sizeof(*ar_pci), &pdev->dev,
2622 &ath10k_pci_hif_ops);
2623 if (!ar) {
2624 ath10k_err("failed to allocate core\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002625 return -ENOMEM;
Michal Kaziore7b54192014-08-07 11:03:27 +02002626 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002627
Michal Kaziore7b54192014-08-07 11:03:27 +02002628 ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002629 ar_pci->pdev = pdev;
2630 ar_pci->dev = &pdev->dev;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002631 ar_pci->ar = ar;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002632
Michal Kazior2986e3e2014-08-07 11:03:30 +02002633 spin_lock_init(&ar_pci->ce_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002634
Michal Kazior2986e3e2014-08-07 11:03:30 +02002635 ret = ath10k_pci_claim(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002636 if (ret) {
Michal Kazior2986e3e2014-08-07 11:03:30 +02002637 ath10k_err("failed to claim device: %d\n", ret);
Michal Kaziore7b54192014-08-07 11:03:27 +02002638 goto err_core_destroy;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002639 }
2640
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002641 ret = ath10k_pci_wake(ar);
Kalle Valoe01ae682013-09-01 11:22:14 +03002642 if (ret) {
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002643 ath10k_err("failed to wake up: %d\n", ret);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002644 goto err_release;
Kalle Valoe01ae682013-09-01 11:22:14 +03002645 }
2646
Kalle Valo233eb972013-10-16 16:46:11 +03002647 chip_id = ath10k_pci_soc_read32(ar, SOC_CHIP_ID_ADDRESS);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002648 if (chip_id == 0xffffffff) {
2649 ath10k_err("failed to get chip id\n");
2650 goto err_sleep;
2651 }
Kalle Valoe01ae682013-09-01 11:22:14 +03002652
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002653 ret = ath10k_pci_alloc_ce(ar);
2654 if (ret) {
2655 ath10k_err("failed to allocate copy engine pipes: %d\n", ret);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002656 goto err_sleep;
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002657 }
2658
Kalle Valoe01ae682013-09-01 11:22:14 +03002659 ret = ath10k_core_register(ar, chip_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002660 if (ret) {
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002661 ath10k_err("failed to register driver core: %d\n", ret);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002662 goto err_free_ce;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002663 }
2664
2665 return 0;
2666
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002667err_free_ce:
2668 ath10k_pci_free_ce(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002669
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002670err_sleep:
2671 ath10k_pci_sleep(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002672
2673err_release:
2674 ath10k_pci_release(ar);
2675
Michal Kaziore7b54192014-08-07 11:03:27 +02002676err_core_destroy:
Kalle Valo5e3dd152013-06-12 20:52:10 +03002677 ath10k_core_destroy(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002678
2679 return ret;
2680}
2681
2682static void ath10k_pci_remove(struct pci_dev *pdev)
2683{
2684 struct ath10k *ar = pci_get_drvdata(pdev);
2685 struct ath10k_pci *ar_pci;
2686
Kalle Valo50f87a62014-03-28 09:32:52 +02002687 ath10k_dbg(ATH10K_DBG_PCI, "pci remove\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002688
2689 if (!ar)
2690 return;
2691
2692 ar_pci = ath10k_pci_priv(ar);
2693
2694 if (!ar_pci)
2695 return;
2696
Kalle Valo5e3dd152013-06-12 20:52:10 +03002697 ath10k_core_unregister(ar);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002698 ath10k_pci_free_ce(ar);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002699 ath10k_pci_sleep(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002700 ath10k_pci_release(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002701 ath10k_core_destroy(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002702}
2703
Kalle Valo5e3dd152013-06-12 20:52:10 +03002704MODULE_DEVICE_TABLE(pci, ath10k_pci_id_table);
2705
2706static struct pci_driver ath10k_pci_driver = {
2707 .name = "ath10k_pci",
2708 .id_table = ath10k_pci_id_table,
2709 .probe = ath10k_pci_probe,
2710 .remove = ath10k_pci_remove,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002711};
2712
2713static int __init ath10k_pci_init(void)
2714{
2715 int ret;
2716
2717 ret = pci_register_driver(&ath10k_pci_driver);
2718 if (ret)
Michal Kazior1d2b48d2013-11-08 08:01:34 +01002719 ath10k_err("failed to register PCI driver: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002720
2721 return ret;
2722}
2723module_init(ath10k_pci_init);
2724
2725static void __exit ath10k_pci_exit(void)
2726{
2727 pci_unregister_driver(&ath10k_pci_driver);
2728}
2729
2730module_exit(ath10k_pci_exit);
2731
2732MODULE_AUTHOR("Qualcomm Atheros");
2733MODULE_DESCRIPTION("Driver support for Atheros QCA988X PCIe devices");
2734MODULE_LICENSE("Dual BSD/GPL");
Michal Kazior24c88f72014-07-25 13:32:17 +02002735MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_FW_3_FILE);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002736MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_BOARD_DATA_FILE);