blob: e41665f7eb85aa8478f1602f9cfbffda9d9d11ba [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>
22
23#include "core.h"
24#include "debug.h"
25
26#include "targaddrs.h"
27#include "bmi.h"
28
29#include "hif.h"
30#include "htc.h"
31
32#include "ce.h"
33#include "pci.h"
34
Bartosz Markowski8cc8df92013-08-02 09:58:49 +020035static unsigned int ath10k_target_ps;
Kalle Valo5e3dd152013-06-12 20:52:10 +030036module_param(ath10k_target_ps, uint, 0644);
37MODULE_PARM_DESC(ath10k_target_ps, "Enable ath10k Target (SoC) PS option");
38
Kalle Valo5e3dd152013-06-12 20:52:10 +030039#define QCA988X_2_0_DEVICE_ID (0x003c)
40
41static DEFINE_PCI_DEVICE_TABLE(ath10k_pci_id_table) = {
Kalle Valo5e3dd152013-06-12 20:52:10 +030042 { PCI_VDEVICE(ATHEROS, QCA988X_2_0_DEVICE_ID) }, /* PCI-E QCA988X V2 */
43 {0}
44};
45
46static int ath10k_pci_diag_read_access(struct ath10k *ar, u32 address,
47 u32 *data);
48
49static void ath10k_pci_process_ce(struct ath10k *ar);
50static int ath10k_pci_post_rx(struct ath10k *ar);
Michal Kazior87263e52013-08-27 13:08:01 +020051static int ath10k_pci_post_rx_pipe(struct ath10k_pci_pipe *pipe_info,
Kalle Valo5e3dd152013-06-12 20:52:10 +030052 int num);
Michal Kazior87263e52013-08-27 13:08:01 +020053static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe *pipe_info);
Kalle Valo5e3dd152013-06-12 20:52:10 +030054static void ath10k_pci_stop_ce(struct ath10k *ar);
Michal Kazior5b2589f2013-11-08 08:01:30 +010055static int ath10k_pci_device_reset(struct ath10k *ar);
Michal Kaziord7fb47f2013-11-08 08:01:26 +010056static int ath10k_pci_wait_for_target_init(struct ath10k *ar);
Michal Kazior32270b62013-08-02 09:15:47 +020057static int ath10k_pci_start_intr(struct ath10k *ar);
58static void ath10k_pci_stop_intr(struct ath10k *ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +030059
60static const struct ce_attr host_ce_config_wlan[] = {
Kalle Valo48e9c222013-09-01 10:01:32 +030061 /* CE0: host->target HTC control and raw streams */
62 {
63 .flags = CE_ATTR_FLAGS,
64 .src_nentries = 16,
65 .src_sz_max = 256,
66 .dest_nentries = 0,
67 },
68
69 /* CE1: target->host HTT + HTC control */
70 {
71 .flags = CE_ATTR_FLAGS,
72 .src_nentries = 0,
73 .src_sz_max = 512,
74 .dest_nentries = 512,
75 },
76
77 /* CE2: target->host WMI */
78 {
79 .flags = CE_ATTR_FLAGS,
80 .src_nentries = 0,
81 .src_sz_max = 2048,
82 .dest_nentries = 32,
83 },
84
85 /* CE3: host->target WMI */
86 {
87 .flags = CE_ATTR_FLAGS,
88 .src_nentries = 32,
89 .src_sz_max = 2048,
90 .dest_nentries = 0,
91 },
92
93 /* CE4: host->target HTT */
94 {
95 .flags = CE_ATTR_FLAGS | CE_ATTR_DIS_INTR,
96 .src_nentries = CE_HTT_H2T_MSG_SRC_NENTRIES,
97 .src_sz_max = 256,
98 .dest_nentries = 0,
99 },
100
101 /* CE5: unused */
102 {
103 .flags = CE_ATTR_FLAGS,
104 .src_nentries = 0,
105 .src_sz_max = 0,
106 .dest_nentries = 0,
107 },
108
109 /* CE6: target autonomous hif_memcpy */
110 {
111 .flags = CE_ATTR_FLAGS,
112 .src_nentries = 0,
113 .src_sz_max = 0,
114 .dest_nentries = 0,
115 },
116
117 /* CE7: ce_diag, the Diagnostic Window */
118 {
119 .flags = CE_ATTR_FLAGS,
120 .src_nentries = 2,
121 .src_sz_max = DIAG_TRANSFER_LIMIT,
122 .dest_nentries = 2,
123 },
Kalle Valo5e3dd152013-06-12 20:52:10 +0300124};
125
126/* Target firmware's Copy Engine configuration. */
127static const struct ce_pipe_config target_ce_config_wlan[] = {
Kalle Valod88effb2013-09-01 10:01:39 +0300128 /* CE0: host->target HTC control and raw streams */
129 {
130 .pipenum = 0,
131 .pipedir = PIPEDIR_OUT,
132 .nentries = 32,
133 .nbytes_max = 256,
134 .flags = CE_ATTR_FLAGS,
135 .reserved = 0,
136 },
137
138 /* CE1: target->host HTT + HTC control */
139 {
140 .pipenum = 1,
141 .pipedir = PIPEDIR_IN,
142 .nentries = 32,
143 .nbytes_max = 512,
144 .flags = CE_ATTR_FLAGS,
145 .reserved = 0,
146 },
147
148 /* CE2: target->host WMI */
149 {
150 .pipenum = 2,
151 .pipedir = PIPEDIR_IN,
152 .nentries = 32,
153 .nbytes_max = 2048,
154 .flags = CE_ATTR_FLAGS,
155 .reserved = 0,
156 },
157
158 /* CE3: host->target WMI */
159 {
160 .pipenum = 3,
161 .pipedir = PIPEDIR_OUT,
162 .nentries = 32,
163 .nbytes_max = 2048,
164 .flags = CE_ATTR_FLAGS,
165 .reserved = 0,
166 },
167
168 /* CE4: host->target HTT */
169 {
170 .pipenum = 4,
171 .pipedir = PIPEDIR_OUT,
172 .nentries = 256,
173 .nbytes_max = 256,
174 .flags = CE_ATTR_FLAGS,
175 .reserved = 0,
176 },
177
Kalle Valo5e3dd152013-06-12 20:52:10 +0300178 /* NB: 50% of src nentries, since tx has 2 frags */
Kalle Valod88effb2013-09-01 10:01:39 +0300179
180 /* CE5: unused */
181 {
182 .pipenum = 5,
183 .pipedir = PIPEDIR_OUT,
184 .nentries = 32,
185 .nbytes_max = 2048,
186 .flags = CE_ATTR_FLAGS,
187 .reserved = 0,
188 },
189
190 /* CE6: Reserved for target autonomous hif_memcpy */
191 {
192 .pipenum = 6,
193 .pipedir = PIPEDIR_INOUT,
194 .nentries = 32,
195 .nbytes_max = 4096,
196 .flags = CE_ATTR_FLAGS,
197 .reserved = 0,
198 },
199
Kalle Valo5e3dd152013-06-12 20:52:10 +0300200 /* CE7 used only by Host */
201};
202
203/*
204 * Diagnostic read/write access is provided for startup/config/debug usage.
205 * Caller must guarantee proper alignment, when applicable, and single user
206 * at any moment.
207 */
208static int ath10k_pci_diag_read_mem(struct ath10k *ar, u32 address, void *data,
209 int nbytes)
210{
211 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
212 int ret = 0;
213 u32 buf;
214 unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
215 unsigned int id;
216 unsigned int flags;
Michal Kazior2aa39112013-08-27 13:08:02 +0200217 struct ath10k_ce_pipe *ce_diag;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300218 /* Host buffer address in CE space */
219 u32 ce_data;
220 dma_addr_t ce_data_base = 0;
221 void *data_buf = NULL;
222 int i;
223
224 /*
225 * This code cannot handle reads to non-memory space. Redirect to the
226 * register read fn but preserve the multi word read capability of
227 * this fn
228 */
229 if (address < DRAM_BASE_ADDRESS) {
230 if (!IS_ALIGNED(address, 4) ||
231 !IS_ALIGNED((unsigned long)data, 4))
232 return -EIO;
233
234 while ((nbytes >= 4) && ((ret = ath10k_pci_diag_read_access(
235 ar, address, (u32 *)data)) == 0)) {
236 nbytes -= sizeof(u32);
237 address += sizeof(u32);
238 data += sizeof(u32);
239 }
240 return ret;
241 }
242
243 ce_diag = ar_pci->ce_diag;
244
245 /*
246 * Allocate a temporary bounce buffer to hold caller's data
247 * to be DMA'ed from Target. This guarantees
248 * 1) 4-byte alignment
249 * 2) Buffer in DMA-able space
250 */
251 orig_nbytes = nbytes;
252 data_buf = (unsigned char *)pci_alloc_consistent(ar_pci->pdev,
253 orig_nbytes,
254 &ce_data_base);
255
256 if (!data_buf) {
257 ret = -ENOMEM;
258 goto done;
259 }
260 memset(data_buf, 0, orig_nbytes);
261
262 remaining_bytes = orig_nbytes;
263 ce_data = ce_data_base;
264 while (remaining_bytes) {
265 nbytes = min_t(unsigned int, remaining_bytes,
266 DIAG_TRANSFER_LIMIT);
267
268 ret = ath10k_ce_recv_buf_enqueue(ce_diag, NULL, ce_data);
269 if (ret != 0)
270 goto done;
271
272 /* Request CE to send from Target(!) address to Host buffer */
273 /*
274 * The address supplied by the caller is in the
275 * Target CPU virtual address space.
276 *
277 * In order to use this address with the diagnostic CE,
278 * convert it from Target CPU virtual address space
279 * to CE address space
280 */
281 ath10k_pci_wake(ar);
282 address = TARG_CPU_SPACE_TO_CE_SPACE(ar, ar_pci->mem,
283 address);
284 ath10k_pci_sleep(ar);
285
286 ret = ath10k_ce_send(ce_diag, NULL, (u32)address, nbytes, 0,
287 0);
288 if (ret)
289 goto done;
290
291 i = 0;
292 while (ath10k_ce_completed_send_next(ce_diag, NULL, &buf,
293 &completed_nbytes,
294 &id) != 0) {
295 mdelay(1);
296 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
297 ret = -EBUSY;
298 goto done;
299 }
300 }
301
302 if (nbytes != completed_nbytes) {
303 ret = -EIO;
304 goto done;
305 }
306
307 if (buf != (u32) address) {
308 ret = -EIO;
309 goto done;
310 }
311
312 i = 0;
313 while (ath10k_ce_completed_recv_next(ce_diag, NULL, &buf,
314 &completed_nbytes,
315 &id, &flags) != 0) {
316 mdelay(1);
317
318 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
319 ret = -EBUSY;
320 goto done;
321 }
322 }
323
324 if (nbytes != completed_nbytes) {
325 ret = -EIO;
326 goto done;
327 }
328
329 if (buf != ce_data) {
330 ret = -EIO;
331 goto done;
332 }
333
334 remaining_bytes -= nbytes;
335 address += nbytes;
336 ce_data += nbytes;
337 }
338
339done:
340 if (ret == 0) {
341 /* Copy data from allocated DMA buf to caller's buf */
342 WARN_ON_ONCE(orig_nbytes & 3);
343 for (i = 0; i < orig_nbytes / sizeof(__le32); i++) {
344 ((u32 *)data)[i] =
345 __le32_to_cpu(((__le32 *)data_buf)[i]);
346 }
347 } else
348 ath10k_dbg(ATH10K_DBG_PCI, "%s failure (0x%x)\n",
349 __func__, address);
350
351 if (data_buf)
352 pci_free_consistent(ar_pci->pdev, orig_nbytes,
353 data_buf, ce_data_base);
354
355 return ret;
356}
357
358/* Read 4-byte aligned data from Target memory or register */
359static int ath10k_pci_diag_read_access(struct ath10k *ar, u32 address,
360 u32 *data)
361{
362 /* Assume range doesn't cross this boundary */
363 if (address >= DRAM_BASE_ADDRESS)
364 return ath10k_pci_diag_read_mem(ar, address, data, sizeof(u32));
365
366 ath10k_pci_wake(ar);
367 *data = ath10k_pci_read32(ar, address);
368 ath10k_pci_sleep(ar);
369 return 0;
370}
371
372static int ath10k_pci_diag_write_mem(struct ath10k *ar, u32 address,
373 const void *data, int nbytes)
374{
375 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
376 int ret = 0;
377 u32 buf;
378 unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
379 unsigned int id;
380 unsigned int flags;
Michal Kazior2aa39112013-08-27 13:08:02 +0200381 struct ath10k_ce_pipe *ce_diag;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300382 void *data_buf = NULL;
383 u32 ce_data; /* Host buffer address in CE space */
384 dma_addr_t ce_data_base = 0;
385 int i;
386
387 ce_diag = ar_pci->ce_diag;
388
389 /*
390 * Allocate a temporary bounce buffer to hold caller's data
391 * to be DMA'ed to Target. This guarantees
392 * 1) 4-byte alignment
393 * 2) Buffer in DMA-able space
394 */
395 orig_nbytes = nbytes;
396 data_buf = (unsigned char *)pci_alloc_consistent(ar_pci->pdev,
397 orig_nbytes,
398 &ce_data_base);
399 if (!data_buf) {
400 ret = -ENOMEM;
401 goto done;
402 }
403
404 /* Copy caller's data to allocated DMA buf */
405 WARN_ON_ONCE(orig_nbytes & 3);
406 for (i = 0; i < orig_nbytes / sizeof(__le32); i++)
407 ((__le32 *)data_buf)[i] = __cpu_to_le32(((u32 *)data)[i]);
408
409 /*
410 * The address supplied by the caller is in the
411 * Target CPU virtual address space.
412 *
413 * In order to use this address with the diagnostic CE,
414 * convert it from
415 * Target CPU virtual address space
416 * to
417 * CE address space
418 */
419 ath10k_pci_wake(ar);
420 address = TARG_CPU_SPACE_TO_CE_SPACE(ar, ar_pci->mem, address);
421 ath10k_pci_sleep(ar);
422
423 remaining_bytes = orig_nbytes;
424 ce_data = ce_data_base;
425 while (remaining_bytes) {
426 /* FIXME: check cast */
427 nbytes = min_t(int, remaining_bytes, DIAG_TRANSFER_LIMIT);
428
429 /* Set up to receive directly into Target(!) address */
430 ret = ath10k_ce_recv_buf_enqueue(ce_diag, NULL, address);
431 if (ret != 0)
432 goto done;
433
434 /*
435 * Request CE to send caller-supplied data that
436 * was copied to bounce buffer to Target(!) address.
437 */
438 ret = ath10k_ce_send(ce_diag, NULL, (u32) ce_data,
439 nbytes, 0, 0);
440 if (ret != 0)
441 goto done;
442
443 i = 0;
444 while (ath10k_ce_completed_send_next(ce_diag, NULL, &buf,
445 &completed_nbytes,
446 &id) != 0) {
447 mdelay(1);
448
449 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
450 ret = -EBUSY;
451 goto done;
452 }
453 }
454
455 if (nbytes != completed_nbytes) {
456 ret = -EIO;
457 goto done;
458 }
459
460 if (buf != ce_data) {
461 ret = -EIO;
462 goto done;
463 }
464
465 i = 0;
466 while (ath10k_ce_completed_recv_next(ce_diag, NULL, &buf,
467 &completed_nbytes,
468 &id, &flags) != 0) {
469 mdelay(1);
470
471 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
472 ret = -EBUSY;
473 goto done;
474 }
475 }
476
477 if (nbytes != completed_nbytes) {
478 ret = -EIO;
479 goto done;
480 }
481
482 if (buf != address) {
483 ret = -EIO;
484 goto done;
485 }
486
487 remaining_bytes -= nbytes;
488 address += nbytes;
489 ce_data += nbytes;
490 }
491
492done:
493 if (data_buf) {
494 pci_free_consistent(ar_pci->pdev, orig_nbytes, data_buf,
495 ce_data_base);
496 }
497
498 if (ret != 0)
499 ath10k_dbg(ATH10K_DBG_PCI, "%s failure (0x%x)\n", __func__,
500 address);
501
502 return ret;
503}
504
505/* Write 4B data to Target memory or register */
506static int ath10k_pci_diag_write_access(struct ath10k *ar, u32 address,
507 u32 data)
508{
509 /* Assume range doesn't cross this boundary */
510 if (address >= DRAM_BASE_ADDRESS)
511 return ath10k_pci_diag_write_mem(ar, address, &data,
512 sizeof(u32));
513
514 ath10k_pci_wake(ar);
515 ath10k_pci_write32(ar, address, data);
516 ath10k_pci_sleep(ar);
517 return 0;
518}
519
520static bool ath10k_pci_target_is_awake(struct ath10k *ar)
521{
522 void __iomem *mem = ath10k_pci_priv(ar)->mem;
523 u32 val;
524 val = ioread32(mem + PCIE_LOCAL_BASE_ADDRESS +
525 RTC_STATE_ADDRESS);
526 return (RTC_STATE_V_GET(val) == RTC_STATE_V_ON);
527}
528
Kalle Valo3aebe542013-09-01 10:02:07 +0300529int ath10k_do_pci_wake(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300530{
531 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
532 void __iomem *pci_addr = ar_pci->mem;
533 int tot_delay = 0;
534 int curr_delay = 5;
535
536 if (atomic_read(&ar_pci->keep_awake_count) == 0) {
537 /* Force AWAKE */
538 iowrite32(PCIE_SOC_WAKE_V_MASK,
539 pci_addr + PCIE_LOCAL_BASE_ADDRESS +
540 PCIE_SOC_WAKE_ADDRESS);
541 }
542 atomic_inc(&ar_pci->keep_awake_count);
543
544 if (ar_pci->verified_awake)
Kalle Valo3aebe542013-09-01 10:02:07 +0300545 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300546
547 for (;;) {
548 if (ath10k_pci_target_is_awake(ar)) {
549 ar_pci->verified_awake = true;
Kalle Valo3aebe542013-09-01 10:02:07 +0300550 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300551 }
552
553 if (tot_delay > PCIE_WAKE_TIMEOUT) {
Kalle Valo3aebe542013-09-01 10:02:07 +0300554 ath10k_warn("target took longer %d us to wake up (awake count %d)\n",
555 PCIE_WAKE_TIMEOUT,
Kalle Valo5e3dd152013-06-12 20:52:10 +0300556 atomic_read(&ar_pci->keep_awake_count));
Kalle Valo3aebe542013-09-01 10:02:07 +0300557 return -ETIMEDOUT;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300558 }
559
560 udelay(curr_delay);
561 tot_delay += curr_delay;
562
563 if (curr_delay < 50)
564 curr_delay += 5;
565 }
566}
567
568void ath10k_do_pci_sleep(struct ath10k *ar)
569{
570 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
571 void __iomem *pci_addr = ar_pci->mem;
572
573 if (atomic_dec_and_test(&ar_pci->keep_awake_count)) {
574 /* Allow sleep */
575 ar_pci->verified_awake = false;
576 iowrite32(PCIE_SOC_WAKE_RESET,
577 pci_addr + PCIE_LOCAL_BASE_ADDRESS +
578 PCIE_SOC_WAKE_ADDRESS);
579 }
580}
581
582/*
583 * FIXME: Handle OOM properly.
584 */
585static inline
Michal Kazior87263e52013-08-27 13:08:01 +0200586struct ath10k_pci_compl *get_free_compl(struct ath10k_pci_pipe *pipe_info)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300587{
588 struct ath10k_pci_compl *compl = NULL;
589
590 spin_lock_bh(&pipe_info->pipe_lock);
591 if (list_empty(&pipe_info->compl_free)) {
592 ath10k_warn("Completion buffers are full\n");
593 goto exit;
594 }
595 compl = list_first_entry(&pipe_info->compl_free,
596 struct ath10k_pci_compl, list);
597 list_del(&compl->list);
598exit:
599 spin_unlock_bh(&pipe_info->pipe_lock);
600 return compl;
601}
602
603/* Called by lower (CE) layer when a send to Target completes. */
Michal Kazior5440ce22013-09-03 15:09:58 +0200604static void ath10k_pci_ce_send_done(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300605{
606 struct ath10k *ar = ce_state->ar;
607 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +0200608 struct ath10k_pci_pipe *pipe_info = &ar_pci->pipe_info[ce_state->id];
Kalle Valo5e3dd152013-06-12 20:52:10 +0300609 struct ath10k_pci_compl *compl;
Michal Kazior5440ce22013-09-03 15:09:58 +0200610 void *transfer_context;
611 u32 ce_data;
612 unsigned int nbytes;
613 unsigned int transfer_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300614
Michal Kazior5440ce22013-09-03 15:09:58 +0200615 while (ath10k_ce_completed_send_next(ce_state, &transfer_context,
616 &ce_data, &nbytes,
617 &transfer_id) == 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300618 compl = get_free_compl(pipe_info);
619 if (!compl)
620 break;
621
Michal Kaziorf9d8fec2013-08-13 07:54:56 +0200622 compl->state = ATH10K_PCI_COMPL_SEND;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300623 compl->ce_state = ce_state;
624 compl->pipe_info = pipe_info;
Kalle Valoaa5c1db2013-09-01 10:01:46 +0300625 compl->skb = transfer_context;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300626 compl->nbytes = nbytes;
627 compl->transfer_id = transfer_id;
628 compl->flags = 0;
629
630 /*
631 * Add the completion to the processing queue.
632 */
633 spin_lock_bh(&ar_pci->compl_lock);
634 list_add_tail(&compl->list, &ar_pci->compl_process);
635 spin_unlock_bh(&ar_pci->compl_lock);
Michal Kazior5440ce22013-09-03 15:09:58 +0200636 }
Kalle Valo5e3dd152013-06-12 20:52:10 +0300637
638 ath10k_pci_process_ce(ar);
639}
640
641/* Called by lower (CE) layer when data is received from the Target. */
Michal Kazior5440ce22013-09-03 15:09:58 +0200642static void ath10k_pci_ce_recv_data(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300643{
644 struct ath10k *ar = ce_state->ar;
645 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +0200646 struct ath10k_pci_pipe *pipe_info = &ar_pci->pipe_info[ce_state->id];
Kalle Valo5e3dd152013-06-12 20:52:10 +0300647 struct ath10k_pci_compl *compl;
648 struct sk_buff *skb;
Michal Kazior5440ce22013-09-03 15:09:58 +0200649 void *transfer_context;
650 u32 ce_data;
651 unsigned int nbytes;
652 unsigned int transfer_id;
653 unsigned int flags;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300654
Michal Kazior5440ce22013-09-03 15:09:58 +0200655 while (ath10k_ce_completed_recv_next(ce_state, &transfer_context,
656 &ce_data, &nbytes, &transfer_id,
657 &flags) == 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300658 compl = get_free_compl(pipe_info);
659 if (!compl)
660 break;
661
Michal Kaziorf9d8fec2013-08-13 07:54:56 +0200662 compl->state = ATH10K_PCI_COMPL_RECV;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300663 compl->ce_state = ce_state;
664 compl->pipe_info = pipe_info;
Kalle Valoaa5c1db2013-09-01 10:01:46 +0300665 compl->skb = transfer_context;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300666 compl->nbytes = nbytes;
667 compl->transfer_id = transfer_id;
668 compl->flags = flags;
669
670 skb = transfer_context;
671 dma_unmap_single(ar->dev, ATH10K_SKB_CB(skb)->paddr,
672 skb->len + skb_tailroom(skb),
673 DMA_FROM_DEVICE);
674 /*
675 * Add the completion to the processing queue.
676 */
677 spin_lock_bh(&ar_pci->compl_lock);
678 list_add_tail(&compl->list, &ar_pci->compl_process);
679 spin_unlock_bh(&ar_pci->compl_lock);
Michal Kazior5440ce22013-09-03 15:09:58 +0200680 }
Kalle Valo5e3dd152013-06-12 20:52:10 +0300681
682 ath10k_pci_process_ce(ar);
683}
684
685/* Send the first nbytes bytes of the buffer */
686static int ath10k_pci_hif_send_head(struct ath10k *ar, u8 pipe_id,
687 unsigned int transfer_id,
688 unsigned int bytes, struct sk_buff *nbuf)
689{
690 struct ath10k_skb_cb *skb_cb = ATH10K_SKB_CB(nbuf);
691 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +0200692 struct ath10k_pci_pipe *pipe_info = &(ar_pci->pipe_info[pipe_id]);
Michal Kazior2aa39112013-08-27 13:08:02 +0200693 struct ath10k_ce_pipe *ce_hdl = pipe_info->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300694 unsigned int len;
695 u32 flags = 0;
696 int ret;
697
Kalle Valo5e3dd152013-06-12 20:52:10 +0300698 len = min(bytes, nbuf->len);
699 bytes -= len;
700
701 if (len & 3)
702 ath10k_warn("skb not aligned to 4-byte boundary (%d)\n", len);
703
704 ath10k_dbg(ATH10K_DBG_PCI,
705 "pci send data vaddr %p paddr 0x%llx len %d as %d bytes\n",
706 nbuf->data, (unsigned long long) skb_cb->paddr,
707 nbuf->len, len);
708 ath10k_dbg_dump(ATH10K_DBG_PCI_DUMP, NULL,
709 "ath10k tx: data: ",
710 nbuf->data, nbuf->len);
711
Michal Kazior2e761b52013-10-02 11:03:40 +0200712 ret = ath10k_ce_send(ce_hdl, nbuf, skb_cb->paddr, len, transfer_id,
713 flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300714 if (ret)
715 ath10k_warn("CE send failed: %p\n", nbuf);
716
717 return ret;
718}
719
720static u16 ath10k_pci_hif_get_free_queue_number(struct ath10k *ar, u8 pipe)
721{
722 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior3efcb3b2013-10-02 11:03:41 +0200723 return ath10k_ce_num_free_src_entries(ar_pci->pipe_info[pipe].ce_hdl);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300724}
725
726static void ath10k_pci_hif_dump_area(struct ath10k *ar)
727{
728 u32 reg_dump_area = 0;
729 u32 reg_dump_values[REG_DUMP_COUNT_QCA988X] = {};
730 u32 host_addr;
731 int ret;
732 u32 i;
733
734 ath10k_err("firmware crashed!\n");
735 ath10k_err("hardware name %s version 0x%x\n",
736 ar->hw_params.name, ar->target_version);
737 ath10k_err("firmware version: %u.%u.%u.%u\n", ar->fw_version_major,
738 ar->fw_version_minor, ar->fw_version_release,
739 ar->fw_version_build);
740
741 host_addr = host_interest_item_address(HI_ITEM(hi_failure_state));
742 if (ath10k_pci_diag_read_mem(ar, host_addr,
743 &reg_dump_area, sizeof(u32)) != 0) {
744 ath10k_warn("could not read hi_failure_state\n");
745 return;
746 }
747
748 ath10k_err("target register Dump Location: 0x%08X\n", reg_dump_area);
749
750 ret = ath10k_pci_diag_read_mem(ar, reg_dump_area,
751 &reg_dump_values[0],
752 REG_DUMP_COUNT_QCA988X * sizeof(u32));
753 if (ret != 0) {
754 ath10k_err("could not dump FW Dump Area\n");
755 return;
756 }
757
758 BUILD_BUG_ON(REG_DUMP_COUNT_QCA988X % 4);
759
760 ath10k_err("target Register Dump\n");
761 for (i = 0; i < REG_DUMP_COUNT_QCA988X; i += 4)
762 ath10k_err("[%02d]: 0x%08X 0x%08X 0x%08X 0x%08X\n",
763 i,
764 reg_dump_values[i],
765 reg_dump_values[i + 1],
766 reg_dump_values[i + 2],
767 reg_dump_values[i + 3]);
Michal Kazioraffd3212013-07-16 09:54:35 +0200768
Michal Kazior5e90de82013-10-16 16:46:05 +0300769 queue_work(ar->workqueue, &ar->restart_work);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300770}
771
772static void ath10k_pci_hif_send_complete_check(struct ath10k *ar, u8 pipe,
773 int force)
774{
775 if (!force) {
776 int resources;
777 /*
778 * Decide whether to actually poll for completions, or just
779 * wait for a later chance.
780 * If there seem to be plenty of resources left, then just wait
781 * since checking involves reading a CE register, which is a
782 * relatively expensive operation.
783 */
784 resources = ath10k_pci_hif_get_free_queue_number(ar, pipe);
785
786 /*
787 * If at least 50% of the total resources are still available,
788 * don't bother checking again yet.
789 */
790 if (resources > (host_ce_config_wlan[pipe].src_nentries >> 1))
791 return;
792 }
793 ath10k_ce_per_engine_service(ar, pipe);
794}
795
Michal Kaziore799bbf2013-07-05 16:15:12 +0300796static void ath10k_pci_hif_set_callbacks(struct ath10k *ar,
797 struct ath10k_hif_cb *callbacks)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300798{
799 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
800
801 ath10k_dbg(ATH10K_DBG_PCI, "%s\n", __func__);
802
803 memcpy(&ar_pci->msg_callbacks_current, callbacks,
804 sizeof(ar_pci->msg_callbacks_current));
805}
806
807static int ath10k_pci_start_ce(struct ath10k *ar)
808{
809 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2aa39112013-08-27 13:08:02 +0200810 struct ath10k_ce_pipe *ce_diag = ar_pci->ce_diag;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300811 const struct ce_attr *attr;
Michal Kazior87263e52013-08-27 13:08:01 +0200812 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300813 struct ath10k_pci_compl *compl;
814 int i, pipe_num, completions, disable_interrupts;
815
816 spin_lock_init(&ar_pci->compl_lock);
817 INIT_LIST_HEAD(&ar_pci->compl_process);
818
Michal Kaziorfad6ed72013-11-08 08:01:23 +0100819 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300820 pipe_info = &ar_pci->pipe_info[pipe_num];
821
822 spin_lock_init(&pipe_info->pipe_lock);
823 INIT_LIST_HEAD(&pipe_info->compl_free);
824
825 /* Handle Diagnostic CE specially */
826 if (pipe_info->ce_hdl == ce_diag)
827 continue;
828
829 attr = &host_ce_config_wlan[pipe_num];
830 completions = 0;
831
832 if (attr->src_nentries) {
833 disable_interrupts = attr->flags & CE_ATTR_DIS_INTR;
834 ath10k_ce_send_cb_register(pipe_info->ce_hdl,
835 ath10k_pci_ce_send_done,
836 disable_interrupts);
837 completions += attr->src_nentries;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300838 }
839
840 if (attr->dest_nentries) {
841 ath10k_ce_recv_cb_register(pipe_info->ce_hdl,
842 ath10k_pci_ce_recv_data);
843 completions += attr->dest_nentries;
844 }
845
846 if (completions == 0)
847 continue;
848
849 for (i = 0; i < completions; i++) {
Michal Kaziorffe5daa2013-08-13 07:54:55 +0200850 compl = kmalloc(sizeof(*compl), GFP_KERNEL);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300851 if (!compl) {
852 ath10k_warn("No memory for completion state\n");
853 ath10k_pci_stop_ce(ar);
854 return -ENOMEM;
855 }
856
Michal Kaziorf9d8fec2013-08-13 07:54:56 +0200857 compl->state = ATH10K_PCI_COMPL_FREE;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300858 list_add_tail(&compl->list, &pipe_info->compl_free);
859 }
860 }
861
862 return 0;
863}
864
Michal Kazior96a9d0d2013-11-08 08:01:25 +0100865static void ath10k_pci_kill_tasklet(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300866{
867 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300868 int i;
869
Kalle Valo5e3dd152013-06-12 20:52:10 +0300870 tasklet_kill(&ar_pci->intr_tq);
Michal Kazior103d4f52013-11-08 08:01:24 +0100871 tasklet_kill(&ar_pci->msi_fw_err);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300872
873 for (i = 0; i < CE_COUNT; i++)
874 tasklet_kill(&ar_pci->pipe_info[i].intr);
Michal Kazior96a9d0d2013-11-08 08:01:25 +0100875}
876
877static void ath10k_pci_stop_ce(struct ath10k *ar)
878{
879 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
880 struct ath10k_pci_compl *compl;
881 struct sk_buff *skb;
Michal Kazior28642f42013-11-08 08:01:31 +0100882 int ret;
Michal Kazior96a9d0d2013-11-08 08:01:25 +0100883
Michal Kazior28642f42013-11-08 08:01:31 +0100884 ret = ath10k_ce_disable_interrupts(ar);
885 if (ret)
886 ath10k_warn("failed to disable CE interrupts: %d\n", ret);
887
Michal Kazior96a9d0d2013-11-08 08:01:25 +0100888 ath10k_pci_kill_tasklet(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300889
890 /* Mark pending completions as aborted, so that upper layers free up
891 * their associated resources */
892 spin_lock_bh(&ar_pci->compl_lock);
893 list_for_each_entry(compl, &ar_pci->compl_process, list) {
Kalle Valoaa5c1db2013-09-01 10:01:46 +0300894 skb = compl->skb;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300895 ATH10K_SKB_CB(skb)->is_aborted = true;
896 }
897 spin_unlock_bh(&ar_pci->compl_lock);
898}
899
900static void ath10k_pci_cleanup_ce(struct ath10k *ar)
901{
902 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
903 struct ath10k_pci_compl *compl, *tmp;
Michal Kazior87263e52013-08-27 13:08:01 +0200904 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300905 struct sk_buff *netbuf;
906 int pipe_num;
907
908 /* Free pending completions. */
909 spin_lock_bh(&ar_pci->compl_lock);
910 if (!list_empty(&ar_pci->compl_process))
911 ath10k_warn("pending completions still present! possible memory leaks.\n");
912
913 list_for_each_entry_safe(compl, tmp, &ar_pci->compl_process, list) {
914 list_del(&compl->list);
Kalle Valoaa5c1db2013-09-01 10:01:46 +0300915 netbuf = compl->skb;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300916 dev_kfree_skb_any(netbuf);
917 kfree(compl);
918 }
919 spin_unlock_bh(&ar_pci->compl_lock);
920
921 /* Free unused completions for each pipe. */
Michal Kaziorfad6ed72013-11-08 08:01:23 +0100922 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300923 pipe_info = &ar_pci->pipe_info[pipe_num];
924
925 spin_lock_bh(&pipe_info->pipe_lock);
926 list_for_each_entry_safe(compl, tmp,
927 &pipe_info->compl_free, list) {
928 list_del(&compl->list);
929 kfree(compl);
930 }
931 spin_unlock_bh(&pipe_info->pipe_lock);
932 }
933}
934
935static void ath10k_pci_process_ce(struct ath10k *ar)
936{
937 struct ath10k_pci *ar_pci = ar->hif.priv;
938 struct ath10k_hif_cb *cb = &ar_pci->msg_callbacks_current;
939 struct ath10k_pci_compl *compl;
940 struct sk_buff *skb;
941 unsigned int nbytes;
942 int ret, send_done = 0;
943
944 /* Upper layers aren't ready to handle tx/rx completions in parallel so
945 * we must serialize all completion processing. */
946
947 spin_lock_bh(&ar_pci->compl_lock);
948 if (ar_pci->compl_processing) {
949 spin_unlock_bh(&ar_pci->compl_lock);
950 return;
951 }
952 ar_pci->compl_processing = true;
953 spin_unlock_bh(&ar_pci->compl_lock);
954
955 for (;;) {
956 spin_lock_bh(&ar_pci->compl_lock);
957 if (list_empty(&ar_pci->compl_process)) {
958 spin_unlock_bh(&ar_pci->compl_lock);
959 break;
960 }
961 compl = list_first_entry(&ar_pci->compl_process,
962 struct ath10k_pci_compl, list);
963 list_del(&compl->list);
964 spin_unlock_bh(&ar_pci->compl_lock);
965
Michal Kaziorf9d8fec2013-08-13 07:54:56 +0200966 switch (compl->state) {
967 case ATH10K_PCI_COMPL_SEND:
Kalle Valo5e3dd152013-06-12 20:52:10 +0300968 cb->tx_completion(ar,
Kalle Valoaa5c1db2013-09-01 10:01:46 +0300969 compl->skb,
Kalle Valo5e3dd152013-06-12 20:52:10 +0300970 compl->transfer_id);
971 send_done = 1;
Michal Kaziorf9d8fec2013-08-13 07:54:56 +0200972 break;
973 case ATH10K_PCI_COMPL_RECV:
Kalle Valo5e3dd152013-06-12 20:52:10 +0300974 ret = ath10k_pci_post_rx_pipe(compl->pipe_info, 1);
975 if (ret) {
976 ath10k_warn("Unable to post recv buffer for pipe: %d\n",
977 compl->pipe_info->pipe_num);
978 break;
979 }
980
Kalle Valoaa5c1db2013-09-01 10:01:46 +0300981 skb = compl->skb;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300982 nbytes = compl->nbytes;
983
984 ath10k_dbg(ATH10K_DBG_PCI,
985 "ath10k_pci_ce_recv_data netbuf=%p nbytes=%d\n",
986 skb, nbytes);
987 ath10k_dbg_dump(ATH10K_DBG_PCI_DUMP, NULL,
988 "ath10k rx: ", skb->data, nbytes);
989
990 if (skb->len + skb_tailroom(skb) >= nbytes) {
991 skb_trim(skb, 0);
992 skb_put(skb, nbytes);
993 cb->rx_completion(ar, skb,
994 compl->pipe_info->pipe_num);
995 } else {
996 ath10k_warn("rxed more than expected (nbytes %d, max %d)",
997 nbytes,
998 skb->len + skb_tailroom(skb));
999 }
Michal Kaziorf9d8fec2013-08-13 07:54:56 +02001000 break;
1001 case ATH10K_PCI_COMPL_FREE:
1002 ath10k_warn("free completion cannot be processed\n");
1003 break;
1004 default:
1005 ath10k_warn("invalid completion state (%d)\n",
1006 compl->state);
1007 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001008 }
1009
Michal Kaziorf9d8fec2013-08-13 07:54:56 +02001010 compl->state = ATH10K_PCI_COMPL_FREE;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001011
1012 /*
1013 * Add completion back to the pipe's free list.
1014 */
1015 spin_lock_bh(&compl->pipe_info->pipe_lock);
1016 list_add_tail(&compl->list, &compl->pipe_info->compl_free);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001017 spin_unlock_bh(&compl->pipe_info->pipe_lock);
1018 }
1019
1020 spin_lock_bh(&ar_pci->compl_lock);
1021 ar_pci->compl_processing = false;
1022 spin_unlock_bh(&ar_pci->compl_lock);
1023}
1024
1025/* TODO - temporary mapping while we have too few CE's */
1026static int ath10k_pci_hif_map_service_to_pipe(struct ath10k *ar,
1027 u16 service_id, u8 *ul_pipe,
1028 u8 *dl_pipe, int *ul_is_polled,
1029 int *dl_is_polled)
1030{
1031 int ret = 0;
1032
1033 /* polling for received messages not supported */
1034 *dl_is_polled = 0;
1035
1036 switch (service_id) {
1037 case ATH10K_HTC_SVC_ID_HTT_DATA_MSG:
1038 /*
1039 * Host->target HTT gets its own pipe, so it can be polled
1040 * while other pipes are interrupt driven.
1041 */
1042 *ul_pipe = 4;
1043 /*
1044 * Use the same target->host pipe for HTC ctrl, HTC raw
1045 * streams, and HTT.
1046 */
1047 *dl_pipe = 1;
1048 break;
1049
1050 case ATH10K_HTC_SVC_ID_RSVD_CTRL:
1051 case ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS:
1052 /*
1053 * Note: HTC_RAW_STREAMS_SVC is currently unused, and
1054 * HTC_CTRL_RSVD_SVC could share the same pipe as the
1055 * WMI services. So, if another CE is needed, change
1056 * this to *ul_pipe = 3, which frees up CE 0.
1057 */
1058 /* *ul_pipe = 3; */
1059 *ul_pipe = 0;
1060 *dl_pipe = 1;
1061 break;
1062
1063 case ATH10K_HTC_SVC_ID_WMI_DATA_BK:
1064 case ATH10K_HTC_SVC_ID_WMI_DATA_BE:
1065 case ATH10K_HTC_SVC_ID_WMI_DATA_VI:
1066 case ATH10K_HTC_SVC_ID_WMI_DATA_VO:
1067
1068 case ATH10K_HTC_SVC_ID_WMI_CONTROL:
1069 *ul_pipe = 3;
1070 *dl_pipe = 2;
1071 break;
1072
1073 /* pipe 5 unused */
1074 /* pipe 6 reserved */
1075 /* pipe 7 reserved */
1076
1077 default:
1078 ret = -1;
1079 break;
1080 }
1081 *ul_is_polled =
1082 (host_ce_config_wlan[*ul_pipe].flags & CE_ATTR_DIS_INTR) != 0;
1083
1084 return ret;
1085}
1086
1087static void ath10k_pci_hif_get_default_pipe(struct ath10k *ar,
1088 u8 *ul_pipe, u8 *dl_pipe)
1089{
1090 int ul_is_polled, dl_is_polled;
1091
1092 (void)ath10k_pci_hif_map_service_to_pipe(ar,
1093 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1094 ul_pipe,
1095 dl_pipe,
1096 &ul_is_polled,
1097 &dl_is_polled);
1098}
1099
Michal Kazior87263e52013-08-27 13:08:01 +02001100static int ath10k_pci_post_rx_pipe(struct ath10k_pci_pipe *pipe_info,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001101 int num)
1102{
1103 struct ath10k *ar = pipe_info->hif_ce_state;
1104 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2aa39112013-08-27 13:08:02 +02001105 struct ath10k_ce_pipe *ce_state = pipe_info->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001106 struct sk_buff *skb;
1107 dma_addr_t ce_data;
1108 int i, ret = 0;
1109
1110 if (pipe_info->buf_sz == 0)
1111 return 0;
1112
1113 for (i = 0; i < num; i++) {
1114 skb = dev_alloc_skb(pipe_info->buf_sz);
1115 if (!skb) {
1116 ath10k_warn("could not allocate skbuff for pipe %d\n",
1117 num);
1118 ret = -ENOMEM;
1119 goto err;
1120 }
1121
1122 WARN_ONCE((unsigned long)skb->data & 3, "unaligned skb");
1123
1124 ce_data = dma_map_single(ar->dev, skb->data,
1125 skb->len + skb_tailroom(skb),
1126 DMA_FROM_DEVICE);
1127
1128 if (unlikely(dma_mapping_error(ar->dev, ce_data))) {
1129 ath10k_warn("could not dma map skbuff\n");
1130 dev_kfree_skb_any(skb);
1131 ret = -EIO;
1132 goto err;
1133 }
1134
1135 ATH10K_SKB_CB(skb)->paddr = ce_data;
1136
1137 pci_dma_sync_single_for_device(ar_pci->pdev, ce_data,
1138 pipe_info->buf_sz,
1139 PCI_DMA_FROMDEVICE);
1140
1141 ret = ath10k_ce_recv_buf_enqueue(ce_state, (void *)skb,
1142 ce_data);
1143 if (ret) {
1144 ath10k_warn("could not enqueue to pipe %d (%d)\n",
1145 num, ret);
1146 goto err;
1147 }
1148 }
1149
1150 return ret;
1151
1152err:
1153 ath10k_pci_rx_pipe_cleanup(pipe_info);
1154 return ret;
1155}
1156
1157static int ath10k_pci_post_rx(struct ath10k *ar)
1158{
1159 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +02001160 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001161 const struct ce_attr *attr;
1162 int pipe_num, ret = 0;
1163
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001164 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001165 pipe_info = &ar_pci->pipe_info[pipe_num];
1166 attr = &host_ce_config_wlan[pipe_num];
1167
1168 if (attr->dest_nentries == 0)
1169 continue;
1170
1171 ret = ath10k_pci_post_rx_pipe(pipe_info,
1172 attr->dest_nentries - 1);
1173 if (ret) {
1174 ath10k_warn("Unable to replenish recv buffers for pipe: %d\n",
1175 pipe_num);
1176
1177 for (; pipe_num >= 0; pipe_num--) {
1178 pipe_info = &ar_pci->pipe_info[pipe_num];
1179 ath10k_pci_rx_pipe_cleanup(pipe_info);
1180 }
1181 return ret;
1182 }
1183 }
1184
1185 return 0;
1186}
1187
1188static int ath10k_pci_hif_start(struct ath10k *ar)
1189{
1190 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1191 int ret;
1192
1193 ret = ath10k_pci_start_ce(ar);
1194 if (ret) {
1195 ath10k_warn("could not start CE (%d)\n", ret);
1196 return ret;
1197 }
1198
1199 /* Post buffers once to start things off. */
1200 ret = ath10k_pci_post_rx(ar);
1201 if (ret) {
1202 ath10k_warn("could not post rx pipes (%d)\n", ret);
1203 return ret;
1204 }
1205
1206 ar_pci->started = 1;
1207 return 0;
1208}
1209
Michal Kazior87263e52013-08-27 13:08:01 +02001210static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe *pipe_info)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001211{
1212 struct ath10k *ar;
1213 struct ath10k_pci *ar_pci;
Michal Kazior2aa39112013-08-27 13:08:02 +02001214 struct ath10k_ce_pipe *ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001215 u32 buf_sz;
1216 struct sk_buff *netbuf;
1217 u32 ce_data;
1218
1219 buf_sz = pipe_info->buf_sz;
1220
1221 /* Unused Copy Engine */
1222 if (buf_sz == 0)
1223 return;
1224
1225 ar = pipe_info->hif_ce_state;
1226 ar_pci = ath10k_pci_priv(ar);
1227
1228 if (!ar_pci->started)
1229 return;
1230
1231 ce_hdl = pipe_info->ce_hdl;
1232
1233 while (ath10k_ce_revoke_recv_next(ce_hdl, (void **)&netbuf,
1234 &ce_data) == 0) {
1235 dma_unmap_single(ar->dev, ATH10K_SKB_CB(netbuf)->paddr,
1236 netbuf->len + skb_tailroom(netbuf),
1237 DMA_FROM_DEVICE);
1238 dev_kfree_skb_any(netbuf);
1239 }
1240}
1241
Michal Kazior87263e52013-08-27 13:08:01 +02001242static void ath10k_pci_tx_pipe_cleanup(struct ath10k_pci_pipe *pipe_info)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001243{
1244 struct ath10k *ar;
1245 struct ath10k_pci *ar_pci;
Michal Kazior2aa39112013-08-27 13:08:02 +02001246 struct ath10k_ce_pipe *ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001247 struct sk_buff *netbuf;
1248 u32 ce_data;
1249 unsigned int nbytes;
1250 unsigned int id;
1251 u32 buf_sz;
1252
1253 buf_sz = pipe_info->buf_sz;
1254
1255 /* Unused Copy Engine */
1256 if (buf_sz == 0)
1257 return;
1258
1259 ar = pipe_info->hif_ce_state;
1260 ar_pci = ath10k_pci_priv(ar);
1261
1262 if (!ar_pci->started)
1263 return;
1264
1265 ce_hdl = pipe_info->ce_hdl;
1266
1267 while (ath10k_ce_cancel_send_next(ce_hdl, (void **)&netbuf,
1268 &ce_data, &nbytes, &id) == 0) {
Kalle Valoe9bb0aa2013-09-08 18:36:11 +03001269 /*
1270 * Indicate the completion to higer layer to free
1271 * the buffer
1272 */
1273 ATH10K_SKB_CB(netbuf)->is_aborted = true;
1274 ar_pci->msg_callbacks_current.tx_completion(ar,
1275 netbuf,
1276 id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001277 }
1278}
1279
1280/*
1281 * Cleanup residual buffers for device shutdown:
1282 * buffers that were enqueued for receive
1283 * buffers that were to be sent
1284 * Note: Buffers that had completed but which were
1285 * not yet processed are on a completion queue. They
1286 * are handled when the completion thread shuts down.
1287 */
1288static void ath10k_pci_buffer_cleanup(struct ath10k *ar)
1289{
1290 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1291 int pipe_num;
1292
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001293 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Michal Kazior87263e52013-08-27 13:08:01 +02001294 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001295
1296 pipe_info = &ar_pci->pipe_info[pipe_num];
1297 ath10k_pci_rx_pipe_cleanup(pipe_info);
1298 ath10k_pci_tx_pipe_cleanup(pipe_info);
1299 }
1300}
1301
1302static void ath10k_pci_ce_deinit(struct ath10k *ar)
1303{
1304 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +02001305 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001306 int pipe_num;
1307
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001308 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001309 pipe_info = &ar_pci->pipe_info[pipe_num];
1310 if (pipe_info->ce_hdl) {
1311 ath10k_ce_deinit(pipe_info->ce_hdl);
1312 pipe_info->ce_hdl = NULL;
1313 pipe_info->buf_sz = 0;
1314 }
1315 }
1316}
1317
Michal Kazior32270b62013-08-02 09:15:47 +02001318static void ath10k_pci_disable_irqs(struct ath10k *ar)
1319{
1320 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1321 int i;
1322
1323 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
1324 disable_irq(ar_pci->pdev->irq + i);
1325}
1326
Kalle Valo5e3dd152013-06-12 20:52:10 +03001327static void ath10k_pci_hif_stop(struct ath10k *ar)
1328{
Michal Kazior32270b62013-08-02 09:15:47 +02001329 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1330
Kalle Valo5e3dd152013-06-12 20:52:10 +03001331 ath10k_dbg(ATH10K_DBG_PCI, "%s\n", __func__);
1332
Michal Kazior32270b62013-08-02 09:15:47 +02001333 /* Irqs are never explicitly re-enabled. They are implicitly re-enabled
1334 * by ath10k_pci_start_intr(). */
1335 ath10k_pci_disable_irqs(ar);
1336
Kalle Valo5e3dd152013-06-12 20:52:10 +03001337 ath10k_pci_stop_ce(ar);
1338
1339 /* At this point, asynchronous threads are stopped, the target should
1340 * not DMA nor interrupt. We process the leftovers and then free
1341 * everything else up. */
1342
1343 ath10k_pci_process_ce(ar);
1344 ath10k_pci_cleanup_ce(ar);
1345 ath10k_pci_buffer_cleanup(ar);
Michal Kazior32270b62013-08-02 09:15:47 +02001346
1347 ar_pci->started = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001348}
1349
1350static int ath10k_pci_hif_exchange_bmi_msg(struct ath10k *ar,
1351 void *req, u32 req_len,
1352 void *resp, u32 *resp_len)
1353{
1354 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2aa39112013-08-27 13:08:02 +02001355 struct ath10k_pci_pipe *pci_tx = &ar_pci->pipe_info[BMI_CE_NUM_TO_TARG];
1356 struct ath10k_pci_pipe *pci_rx = &ar_pci->pipe_info[BMI_CE_NUM_TO_HOST];
1357 struct ath10k_ce_pipe *ce_tx = pci_tx->ce_hdl;
1358 struct ath10k_ce_pipe *ce_rx = pci_rx->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001359 dma_addr_t req_paddr = 0;
1360 dma_addr_t resp_paddr = 0;
1361 struct bmi_xfer xfer = {};
1362 void *treq, *tresp = NULL;
1363 int ret = 0;
1364
1365 if (resp && !resp_len)
1366 return -EINVAL;
1367
1368 if (resp && resp_len && *resp_len == 0)
1369 return -EINVAL;
1370
1371 treq = kmemdup(req, req_len, GFP_KERNEL);
1372 if (!treq)
1373 return -ENOMEM;
1374
1375 req_paddr = dma_map_single(ar->dev, treq, req_len, DMA_TO_DEVICE);
1376 ret = dma_mapping_error(ar->dev, req_paddr);
1377 if (ret)
1378 goto err_dma;
1379
1380 if (resp && resp_len) {
1381 tresp = kzalloc(*resp_len, GFP_KERNEL);
1382 if (!tresp) {
1383 ret = -ENOMEM;
1384 goto err_req;
1385 }
1386
1387 resp_paddr = dma_map_single(ar->dev, tresp, *resp_len,
1388 DMA_FROM_DEVICE);
1389 ret = dma_mapping_error(ar->dev, resp_paddr);
1390 if (ret)
1391 goto err_req;
1392
1393 xfer.wait_for_resp = true;
1394 xfer.resp_len = 0;
1395
1396 ath10k_ce_recv_buf_enqueue(ce_rx, &xfer, resp_paddr);
1397 }
1398
1399 init_completion(&xfer.done);
1400
1401 ret = ath10k_ce_send(ce_tx, &xfer, req_paddr, req_len, -1, 0);
1402 if (ret)
1403 goto err_resp;
1404
1405 ret = wait_for_completion_timeout(&xfer.done,
1406 BMI_COMMUNICATION_TIMEOUT_HZ);
1407 if (ret <= 0) {
1408 u32 unused_buffer;
1409 unsigned int unused_nbytes;
1410 unsigned int unused_id;
1411
1412 ret = -ETIMEDOUT;
1413 ath10k_ce_cancel_send_next(ce_tx, NULL, &unused_buffer,
1414 &unused_nbytes, &unused_id);
1415 } else {
1416 /* non-zero means we did not time out */
1417 ret = 0;
1418 }
1419
1420err_resp:
1421 if (resp) {
1422 u32 unused_buffer;
1423
1424 ath10k_ce_revoke_recv_next(ce_rx, NULL, &unused_buffer);
1425 dma_unmap_single(ar->dev, resp_paddr,
1426 *resp_len, DMA_FROM_DEVICE);
1427 }
1428err_req:
1429 dma_unmap_single(ar->dev, req_paddr, req_len, DMA_TO_DEVICE);
1430
1431 if (ret == 0 && resp_len) {
1432 *resp_len = min(*resp_len, xfer.resp_len);
1433 memcpy(resp, tresp, xfer.resp_len);
1434 }
1435err_dma:
1436 kfree(treq);
1437 kfree(tresp);
1438
1439 return ret;
1440}
1441
Michal Kazior5440ce22013-09-03 15:09:58 +02001442static void ath10k_pci_bmi_send_done(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001443{
Michal Kazior5440ce22013-09-03 15:09:58 +02001444 struct bmi_xfer *xfer;
1445 u32 ce_data;
1446 unsigned int nbytes;
1447 unsigned int transfer_id;
1448
1449 if (ath10k_ce_completed_send_next(ce_state, (void **)&xfer, &ce_data,
1450 &nbytes, &transfer_id))
1451 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001452
1453 if (xfer->wait_for_resp)
1454 return;
1455
1456 complete(&xfer->done);
1457}
1458
Michal Kazior5440ce22013-09-03 15:09:58 +02001459static void ath10k_pci_bmi_recv_data(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001460{
Michal Kazior5440ce22013-09-03 15:09:58 +02001461 struct bmi_xfer *xfer;
1462 u32 ce_data;
1463 unsigned int nbytes;
1464 unsigned int transfer_id;
1465 unsigned int flags;
1466
1467 if (ath10k_ce_completed_recv_next(ce_state, (void **)&xfer, &ce_data,
1468 &nbytes, &transfer_id, &flags))
1469 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001470
1471 if (!xfer->wait_for_resp) {
1472 ath10k_warn("unexpected: BMI data received; ignoring\n");
1473 return;
1474 }
1475
1476 xfer->resp_len = nbytes;
1477 complete(&xfer->done);
1478}
1479
1480/*
1481 * Map from service/endpoint to Copy Engine.
1482 * This table is derived from the CE_PCI TABLE, above.
1483 * It is passed to the Target at startup for use by firmware.
1484 */
1485static const struct service_to_pipe target_service_to_ce_map_wlan[] = {
1486 {
1487 ATH10K_HTC_SVC_ID_WMI_DATA_VO,
1488 PIPEDIR_OUT, /* out = UL = host -> target */
1489 3,
1490 },
1491 {
1492 ATH10K_HTC_SVC_ID_WMI_DATA_VO,
1493 PIPEDIR_IN, /* in = DL = target -> host */
1494 2,
1495 },
1496 {
1497 ATH10K_HTC_SVC_ID_WMI_DATA_BK,
1498 PIPEDIR_OUT, /* out = UL = host -> target */
1499 3,
1500 },
1501 {
1502 ATH10K_HTC_SVC_ID_WMI_DATA_BK,
1503 PIPEDIR_IN, /* in = DL = target -> host */
1504 2,
1505 },
1506 {
1507 ATH10K_HTC_SVC_ID_WMI_DATA_BE,
1508 PIPEDIR_OUT, /* out = UL = host -> target */
1509 3,
1510 },
1511 {
1512 ATH10K_HTC_SVC_ID_WMI_DATA_BE,
1513 PIPEDIR_IN, /* in = DL = target -> host */
1514 2,
1515 },
1516 {
1517 ATH10K_HTC_SVC_ID_WMI_DATA_VI,
1518 PIPEDIR_OUT, /* out = UL = host -> target */
1519 3,
1520 },
1521 {
1522 ATH10K_HTC_SVC_ID_WMI_DATA_VI,
1523 PIPEDIR_IN, /* in = DL = target -> host */
1524 2,
1525 },
1526 {
1527 ATH10K_HTC_SVC_ID_WMI_CONTROL,
1528 PIPEDIR_OUT, /* out = UL = host -> target */
1529 3,
1530 },
1531 {
1532 ATH10K_HTC_SVC_ID_WMI_CONTROL,
1533 PIPEDIR_IN, /* in = DL = target -> host */
1534 2,
1535 },
1536 {
1537 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1538 PIPEDIR_OUT, /* out = UL = host -> target */
1539 0, /* could be moved to 3 (share with WMI) */
1540 },
1541 {
1542 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1543 PIPEDIR_IN, /* in = DL = target -> host */
1544 1,
1545 },
1546 {
1547 ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS, /* not currently used */
1548 PIPEDIR_OUT, /* out = UL = host -> target */
1549 0,
1550 },
1551 {
1552 ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS, /* not currently used */
1553 PIPEDIR_IN, /* in = DL = target -> host */
1554 1,
1555 },
1556 {
1557 ATH10K_HTC_SVC_ID_HTT_DATA_MSG,
1558 PIPEDIR_OUT, /* out = UL = host -> target */
1559 4,
1560 },
1561 {
1562 ATH10K_HTC_SVC_ID_HTT_DATA_MSG,
1563 PIPEDIR_IN, /* in = DL = target -> host */
1564 1,
1565 },
1566
1567 /* (Additions here) */
1568
1569 { /* Must be last */
1570 0,
1571 0,
1572 0,
1573 },
1574};
1575
1576/*
1577 * Send an interrupt to the device to wake up the Target CPU
1578 * so it has an opportunity to notice any changed state.
1579 */
1580static int ath10k_pci_wake_target_cpu(struct ath10k *ar)
1581{
1582 int ret;
1583 u32 core_ctrl;
1584
1585 ret = ath10k_pci_diag_read_access(ar, SOC_CORE_BASE_ADDRESS |
1586 CORE_CTRL_ADDRESS,
1587 &core_ctrl);
1588 if (ret) {
1589 ath10k_warn("Unable to read core ctrl\n");
1590 return ret;
1591 }
1592
1593 /* A_INUM_FIRMWARE interrupt to Target CPU */
1594 core_ctrl |= CORE_CTRL_CPU_INTR_MASK;
1595
1596 ret = ath10k_pci_diag_write_access(ar, SOC_CORE_BASE_ADDRESS |
1597 CORE_CTRL_ADDRESS,
1598 core_ctrl);
1599 if (ret)
1600 ath10k_warn("Unable to set interrupt mask\n");
1601
1602 return ret;
1603}
1604
1605static int ath10k_pci_init_config(struct ath10k *ar)
1606{
1607 u32 interconnect_targ_addr;
1608 u32 pcie_state_targ_addr = 0;
1609 u32 pipe_cfg_targ_addr = 0;
1610 u32 svc_to_pipe_map = 0;
1611 u32 pcie_config_flags = 0;
1612 u32 ealloc_value;
1613 u32 ealloc_targ_addr;
1614 u32 flag2_value;
1615 u32 flag2_targ_addr;
1616 int ret = 0;
1617
1618 /* Download to Target the CE Config and the service-to-CE map */
1619 interconnect_targ_addr =
1620 host_interest_item_address(HI_ITEM(hi_interconnect_state));
1621
1622 /* Supply Target-side CE configuration */
1623 ret = ath10k_pci_diag_read_access(ar, interconnect_targ_addr,
1624 &pcie_state_targ_addr);
1625 if (ret != 0) {
1626 ath10k_err("Failed to get pcie state addr: %d\n", ret);
1627 return ret;
1628 }
1629
1630 if (pcie_state_targ_addr == 0) {
1631 ret = -EIO;
1632 ath10k_err("Invalid pcie state addr\n");
1633 return ret;
1634 }
1635
1636 ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
1637 offsetof(struct pcie_state,
1638 pipe_cfg_addr),
1639 &pipe_cfg_targ_addr);
1640 if (ret != 0) {
1641 ath10k_err("Failed to get pipe cfg addr: %d\n", ret);
1642 return ret;
1643 }
1644
1645 if (pipe_cfg_targ_addr == 0) {
1646 ret = -EIO;
1647 ath10k_err("Invalid pipe cfg addr\n");
1648 return ret;
1649 }
1650
1651 ret = ath10k_pci_diag_write_mem(ar, pipe_cfg_targ_addr,
1652 target_ce_config_wlan,
1653 sizeof(target_ce_config_wlan));
1654
1655 if (ret != 0) {
1656 ath10k_err("Failed to write pipe cfg: %d\n", ret);
1657 return ret;
1658 }
1659
1660 ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
1661 offsetof(struct pcie_state,
1662 svc_to_pipe_map),
1663 &svc_to_pipe_map);
1664 if (ret != 0) {
1665 ath10k_err("Failed to get svc/pipe map: %d\n", ret);
1666 return ret;
1667 }
1668
1669 if (svc_to_pipe_map == 0) {
1670 ret = -EIO;
1671 ath10k_err("Invalid svc_to_pipe map\n");
1672 return ret;
1673 }
1674
1675 ret = ath10k_pci_diag_write_mem(ar, svc_to_pipe_map,
1676 target_service_to_ce_map_wlan,
1677 sizeof(target_service_to_ce_map_wlan));
1678 if (ret != 0) {
1679 ath10k_err("Failed to write svc/pipe map: %d\n", ret);
1680 return ret;
1681 }
1682
1683 ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
1684 offsetof(struct pcie_state,
1685 config_flags),
1686 &pcie_config_flags);
1687 if (ret != 0) {
1688 ath10k_err("Failed to get pcie config_flags: %d\n", ret);
1689 return ret;
1690 }
1691
1692 pcie_config_flags &= ~PCIE_CONFIG_FLAG_ENABLE_L1;
1693
1694 ret = ath10k_pci_diag_write_mem(ar, pcie_state_targ_addr +
1695 offsetof(struct pcie_state, config_flags),
1696 &pcie_config_flags,
1697 sizeof(pcie_config_flags));
1698 if (ret != 0) {
1699 ath10k_err("Failed to write pcie config_flags: %d\n", ret);
1700 return ret;
1701 }
1702
1703 /* configure early allocation */
1704 ealloc_targ_addr = host_interest_item_address(HI_ITEM(hi_early_alloc));
1705
1706 ret = ath10k_pci_diag_read_access(ar, ealloc_targ_addr, &ealloc_value);
1707 if (ret != 0) {
1708 ath10k_err("Faile to get early alloc val: %d\n", ret);
1709 return ret;
1710 }
1711
1712 /* first bank is switched to IRAM */
1713 ealloc_value |= ((HI_EARLY_ALLOC_MAGIC << HI_EARLY_ALLOC_MAGIC_SHIFT) &
1714 HI_EARLY_ALLOC_MAGIC_MASK);
1715 ealloc_value |= ((1 << HI_EARLY_ALLOC_IRAM_BANKS_SHIFT) &
1716 HI_EARLY_ALLOC_IRAM_BANKS_MASK);
1717
1718 ret = ath10k_pci_diag_write_access(ar, ealloc_targ_addr, ealloc_value);
1719 if (ret != 0) {
1720 ath10k_err("Failed to set early alloc val: %d\n", ret);
1721 return ret;
1722 }
1723
1724 /* Tell Target to proceed with initialization */
1725 flag2_targ_addr = host_interest_item_address(HI_ITEM(hi_option_flag2));
1726
1727 ret = ath10k_pci_diag_read_access(ar, flag2_targ_addr, &flag2_value);
1728 if (ret != 0) {
1729 ath10k_err("Failed to get option val: %d\n", ret);
1730 return ret;
1731 }
1732
1733 flag2_value |= HI_OPTION_EARLY_CFG_DONE;
1734
1735 ret = ath10k_pci_diag_write_access(ar, flag2_targ_addr, flag2_value);
1736 if (ret != 0) {
1737 ath10k_err("Failed to set option val: %d\n", ret);
1738 return ret;
1739 }
1740
1741 return 0;
1742}
1743
1744
1745
1746static int ath10k_pci_ce_init(struct ath10k *ar)
1747{
1748 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +02001749 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001750 const struct ce_attr *attr;
1751 int pipe_num;
1752
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001753 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001754 pipe_info = &ar_pci->pipe_info[pipe_num];
1755 pipe_info->pipe_num = pipe_num;
1756 pipe_info->hif_ce_state = ar;
1757 attr = &host_ce_config_wlan[pipe_num];
1758
1759 pipe_info->ce_hdl = ath10k_ce_init(ar, pipe_num, attr);
1760 if (pipe_info->ce_hdl == NULL) {
1761 ath10k_err("Unable to initialize CE for pipe: %d\n",
1762 pipe_num);
1763
1764 /* It is safe to call it here. It checks if ce_hdl is
1765 * valid for each pipe */
1766 ath10k_pci_ce_deinit(ar);
1767 return -1;
1768 }
1769
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001770 if (pipe_num == CE_COUNT - 1) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001771 /*
1772 * Reserve the ultimate CE for
1773 * diagnostic Window support
1774 */
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001775 ar_pci->ce_diag = pipe_info->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001776 continue;
1777 }
1778
1779 pipe_info->buf_sz = (size_t) (attr->src_sz_max);
1780 }
1781
1782 /*
1783 * Initially, establish CE completion handlers for use with BMI.
1784 * These are overwritten with generic handlers after we exit BMI phase.
1785 */
1786 pipe_info = &ar_pci->pipe_info[BMI_CE_NUM_TO_TARG];
1787 ath10k_ce_send_cb_register(pipe_info->ce_hdl,
1788 ath10k_pci_bmi_send_done, 0);
1789
1790 pipe_info = &ar_pci->pipe_info[BMI_CE_NUM_TO_HOST];
1791 ath10k_ce_recv_cb_register(pipe_info->ce_hdl,
1792 ath10k_pci_bmi_recv_data);
1793
1794 return 0;
1795}
1796
1797static void ath10k_pci_fw_interrupt_handler(struct ath10k *ar)
1798{
1799 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1800 u32 fw_indicator_address, fw_indicator;
1801
1802 ath10k_pci_wake(ar);
1803
1804 fw_indicator_address = ar_pci->fw_indicator_address;
1805 fw_indicator = ath10k_pci_read32(ar, fw_indicator_address);
1806
1807 if (fw_indicator & FW_IND_EVENT_PENDING) {
1808 /* ACK: clear Target-side pending event */
1809 ath10k_pci_write32(ar, fw_indicator_address,
1810 fw_indicator & ~FW_IND_EVENT_PENDING);
1811
1812 if (ar_pci->started) {
1813 ath10k_pci_hif_dump_area(ar);
1814 } else {
1815 /*
1816 * Probable Target failure before we're prepared
1817 * to handle it. Generally unexpected.
1818 */
1819 ath10k_warn("early firmware event indicated\n");
1820 }
1821 }
1822
1823 ath10k_pci_sleep(ar);
1824}
1825
Michal Kazior8c5c5362013-07-16 09:38:50 +02001826static int ath10k_pci_hif_power_up(struct ath10k *ar)
1827{
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02001828 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001829 int ret;
1830
Michal Kazior32270b62013-08-02 09:15:47 +02001831 ret = ath10k_pci_start_intr(ar);
1832 if (ret) {
1833 ath10k_err("could not start interrupt handling (%d)\n", ret);
1834 goto err;
1835 }
1836
Michal Kazior8c5c5362013-07-16 09:38:50 +02001837 /*
1838 * Bring the target up cleanly.
1839 *
1840 * The target may be in an undefined state with an AUX-powered Target
1841 * and a Host in WoW mode. If the Host crashes, loses power, or is
1842 * restarted (without unloading the driver) then the Target is left
1843 * (aux) powered and running. On a subsequent driver load, the Target
1844 * is in an unexpected state. We try to catch that here in order to
1845 * reset the Target and retry the probe.
1846 */
Michal Kazior5b2589f2013-11-08 08:01:30 +01001847 ret = ath10k_pci_device_reset(ar);
1848 if (ret) {
1849 ath10k_err("failed to reset target: %d\n", ret);
1850 goto err_irq;
1851 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02001852
Michal Kaziord7fb47f2013-11-08 08:01:26 +01001853 ret = ath10k_pci_wait_for_target_init(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001854 if (ret)
Michal Kazior32270b62013-08-02 09:15:47 +02001855 goto err_irq;
Michal Kazior8c5c5362013-07-16 09:38:50 +02001856
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02001857 if (!test_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE, ar_pci->features))
Michal Kazior8c5c5362013-07-16 09:38:50 +02001858 /* Force AWAKE forever */
Michal Kazior8c5c5362013-07-16 09:38:50 +02001859 ath10k_do_pci_wake(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001860
1861 ret = ath10k_pci_ce_init(ar);
1862 if (ret)
1863 goto err_ps;
1864
1865 ret = ath10k_pci_init_config(ar);
1866 if (ret)
1867 goto err_ce;
1868
1869 ret = ath10k_pci_wake_target_cpu(ar);
1870 if (ret) {
1871 ath10k_err("could not wake up target CPU (%d)\n", ret);
1872 goto err_ce;
1873 }
1874
1875 return 0;
1876
1877err_ce:
1878 ath10k_pci_ce_deinit(ar);
1879err_ps:
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02001880 if (!test_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE, ar_pci->features))
Michal Kazior8c5c5362013-07-16 09:38:50 +02001881 ath10k_do_pci_sleep(ar);
Michal Kazior32270b62013-08-02 09:15:47 +02001882err_irq:
1883 ath10k_pci_stop_intr(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001884err:
1885 return ret;
1886}
1887
1888static void ath10k_pci_hif_power_down(struct ath10k *ar)
1889{
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02001890 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1891
Michal Kazior32270b62013-08-02 09:15:47 +02001892 ath10k_pci_stop_intr(ar);
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02001893
Michal Kazior8c5c5362013-07-16 09:38:50 +02001894 ath10k_pci_ce_deinit(ar);
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02001895 if (!test_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE, ar_pci->features))
Michal Kazior8c5c5362013-07-16 09:38:50 +02001896 ath10k_do_pci_sleep(ar);
1897}
1898
Michal Kazior8cd13ca2013-07-16 09:38:54 +02001899#ifdef CONFIG_PM
1900
1901#define ATH10K_PCI_PM_CONTROL 0x44
1902
1903static int ath10k_pci_hif_suspend(struct ath10k *ar)
1904{
1905 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1906 struct pci_dev *pdev = ar_pci->pdev;
1907 u32 val;
1908
1909 pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
1910
1911 if ((val & 0x000000ff) != 0x3) {
1912 pci_save_state(pdev);
1913 pci_disable_device(pdev);
1914 pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
1915 (val & 0xffffff00) | 0x03);
1916 }
1917
1918 return 0;
1919}
1920
1921static int ath10k_pci_hif_resume(struct ath10k *ar)
1922{
1923 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1924 struct pci_dev *pdev = ar_pci->pdev;
1925 u32 val;
1926
1927 pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
1928
1929 if ((val & 0x000000ff) != 0) {
1930 pci_restore_state(pdev);
1931 pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
1932 val & 0xffffff00);
1933 /*
1934 * Suspend/Resume resets the PCI configuration space,
1935 * so we have to re-disable the RETRY_TIMEOUT register (0x41)
1936 * to keep PCI Tx retries from interfering with C3 CPU state
1937 */
1938 pci_read_config_dword(pdev, 0x40, &val);
1939
1940 if ((val & 0x0000ff00) != 0)
1941 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
1942 }
1943
1944 return 0;
1945}
1946#endif
1947
Kalle Valo5e3dd152013-06-12 20:52:10 +03001948static const struct ath10k_hif_ops ath10k_pci_hif_ops = {
1949 .send_head = ath10k_pci_hif_send_head,
1950 .exchange_bmi_msg = ath10k_pci_hif_exchange_bmi_msg,
1951 .start = ath10k_pci_hif_start,
1952 .stop = ath10k_pci_hif_stop,
1953 .map_service_to_pipe = ath10k_pci_hif_map_service_to_pipe,
1954 .get_default_pipe = ath10k_pci_hif_get_default_pipe,
1955 .send_complete_check = ath10k_pci_hif_send_complete_check,
Michal Kaziore799bbf2013-07-05 16:15:12 +03001956 .set_callbacks = ath10k_pci_hif_set_callbacks,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001957 .get_free_queue_number = ath10k_pci_hif_get_free_queue_number,
Michal Kazior8c5c5362013-07-16 09:38:50 +02001958 .power_up = ath10k_pci_hif_power_up,
1959 .power_down = ath10k_pci_hif_power_down,
Michal Kazior8cd13ca2013-07-16 09:38:54 +02001960#ifdef CONFIG_PM
1961 .suspend = ath10k_pci_hif_suspend,
1962 .resume = ath10k_pci_hif_resume,
1963#endif
Kalle Valo5e3dd152013-06-12 20:52:10 +03001964};
1965
1966static void ath10k_pci_ce_tasklet(unsigned long ptr)
1967{
Michal Kazior87263e52013-08-27 13:08:01 +02001968 struct ath10k_pci_pipe *pipe = (struct ath10k_pci_pipe *)ptr;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001969 struct ath10k_pci *ar_pci = pipe->ar_pci;
1970
1971 ath10k_ce_per_engine_service(ar_pci->ar, pipe->pipe_num);
1972}
1973
1974static void ath10k_msi_err_tasklet(unsigned long data)
1975{
1976 struct ath10k *ar = (struct ath10k *)data;
1977
1978 ath10k_pci_fw_interrupt_handler(ar);
1979}
1980
1981/*
1982 * Handler for a per-engine interrupt on a PARTICULAR CE.
1983 * This is used in cases where each CE has a private MSI interrupt.
1984 */
1985static irqreturn_t ath10k_pci_per_engine_handler(int irq, void *arg)
1986{
1987 struct ath10k *ar = arg;
1988 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1989 int ce_id = irq - ar_pci->pdev->irq - MSI_ASSIGN_CE_INITIAL;
1990
Dan Carpentere5742672013-06-18 10:28:46 +03001991 if (ce_id < 0 || ce_id >= ARRAY_SIZE(ar_pci->pipe_info)) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001992 ath10k_warn("unexpected/invalid irq %d ce_id %d\n", irq, ce_id);
1993 return IRQ_HANDLED;
1994 }
1995
1996 /*
1997 * NOTE: We are able to derive ce_id from irq because we
1998 * use a one-to-one mapping for CE's 0..5.
1999 * CE's 6 & 7 do not use interrupts at all.
2000 *
2001 * This mapping must be kept in sync with the mapping
2002 * used by firmware.
2003 */
2004 tasklet_schedule(&ar_pci->pipe_info[ce_id].intr);
2005 return IRQ_HANDLED;
2006}
2007
2008static irqreturn_t ath10k_pci_msi_fw_handler(int irq, void *arg)
2009{
2010 struct ath10k *ar = arg;
2011 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2012
2013 tasklet_schedule(&ar_pci->msi_fw_err);
2014 return IRQ_HANDLED;
2015}
2016
2017/*
2018 * Top-level interrupt handler for all PCI interrupts from a Target.
2019 * When a block of MSI interrupts is allocated, this top-level handler
2020 * is not used; instead, we directly call the correct sub-handler.
2021 */
2022static irqreturn_t ath10k_pci_interrupt_handler(int irq, void *arg)
2023{
2024 struct ath10k *ar = arg;
2025 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2026
2027 if (ar_pci->num_msi_intrs == 0) {
2028 /*
2029 * IMPORTANT: INTR_CLR regiser has to be set after
2030 * INTR_ENABLE is set to 0, otherwise interrupt can not be
2031 * really cleared.
2032 */
2033 iowrite32(0, ar_pci->mem +
2034 (SOC_CORE_BASE_ADDRESS |
2035 PCIE_INTR_ENABLE_ADDRESS));
2036 iowrite32(PCIE_INTR_FIRMWARE_MASK |
2037 PCIE_INTR_CE_MASK_ALL,
2038 ar_pci->mem + (SOC_CORE_BASE_ADDRESS |
2039 PCIE_INTR_CLR_ADDRESS));
2040 /*
2041 * IMPORTANT: this extra read transaction is required to
2042 * flush the posted write buffer.
2043 */
2044 (void) ioread32(ar_pci->mem +
2045 (SOC_CORE_BASE_ADDRESS |
2046 PCIE_INTR_ENABLE_ADDRESS));
2047 }
2048
2049 tasklet_schedule(&ar_pci->intr_tq);
2050
2051 return IRQ_HANDLED;
2052}
2053
2054static void ath10k_pci_tasklet(unsigned long data)
2055{
2056 struct ath10k *ar = (struct ath10k *)data;
2057 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2058
2059 ath10k_pci_fw_interrupt_handler(ar); /* FIXME: Handle FW error */
2060 ath10k_ce_per_engine_service_any(ar);
2061
2062 if (ar_pci->num_msi_intrs == 0) {
2063 /* Enable Legacy PCI line interrupts */
2064 iowrite32(PCIE_INTR_FIRMWARE_MASK |
2065 PCIE_INTR_CE_MASK_ALL,
2066 ar_pci->mem + (SOC_CORE_BASE_ADDRESS |
2067 PCIE_INTR_ENABLE_ADDRESS));
2068 /*
2069 * IMPORTANT: this extra read transaction is required to
2070 * flush the posted write buffer
2071 */
2072 (void) ioread32(ar_pci->mem +
2073 (SOC_CORE_BASE_ADDRESS |
2074 PCIE_INTR_ENABLE_ADDRESS));
2075 }
2076}
2077
2078static int ath10k_pci_start_intr_msix(struct ath10k *ar, int num)
2079{
2080 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2081 int ret;
2082 int i;
2083
2084 ret = pci_enable_msi_block(ar_pci->pdev, num);
2085 if (ret)
2086 return ret;
2087
2088 ret = request_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW,
2089 ath10k_pci_msi_fw_handler,
2090 IRQF_SHARED, "ath10k_pci", ar);
Michal Kazior591ecdb2013-07-31 10:55:15 +02002091 if (ret) {
2092 ath10k_warn("request_irq(%d) failed %d\n",
2093 ar_pci->pdev->irq + MSI_ASSIGN_FW, ret);
2094
2095 pci_disable_msi(ar_pci->pdev);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002096 return ret;
Michal Kazior591ecdb2013-07-31 10:55:15 +02002097 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002098
2099 for (i = MSI_ASSIGN_CE_INITIAL; i <= MSI_ASSIGN_CE_MAX; i++) {
2100 ret = request_irq(ar_pci->pdev->irq + i,
2101 ath10k_pci_per_engine_handler,
2102 IRQF_SHARED, "ath10k_pci", ar);
2103 if (ret) {
2104 ath10k_warn("request_irq(%d) failed %d\n",
2105 ar_pci->pdev->irq + i, ret);
2106
Michal Kazior87b14232013-06-26 08:50:50 +02002107 for (i--; i >= MSI_ASSIGN_CE_INITIAL; i--)
2108 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002109
Michal Kazior87b14232013-06-26 08:50:50 +02002110 free_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002111 pci_disable_msi(ar_pci->pdev);
2112 return ret;
2113 }
2114 }
2115
2116 ath10k_info("MSI-X interrupt handling (%d intrs)\n", num);
2117 return 0;
2118}
2119
2120static int ath10k_pci_start_intr_msi(struct ath10k *ar)
2121{
2122 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2123 int ret;
2124
2125 ret = pci_enable_msi(ar_pci->pdev);
2126 if (ret < 0)
2127 return ret;
2128
2129 ret = request_irq(ar_pci->pdev->irq,
2130 ath10k_pci_interrupt_handler,
2131 IRQF_SHARED, "ath10k_pci", ar);
2132 if (ret < 0) {
2133 pci_disable_msi(ar_pci->pdev);
2134 return ret;
2135 }
2136
2137 ath10k_info("MSI interrupt handling\n");
2138 return 0;
2139}
2140
2141static int ath10k_pci_start_intr_legacy(struct ath10k *ar)
2142{
2143 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2144 int ret;
2145
2146 ret = request_irq(ar_pci->pdev->irq,
2147 ath10k_pci_interrupt_handler,
2148 IRQF_SHARED, "ath10k_pci", ar);
2149 if (ret < 0)
2150 return ret;
2151
Michal Kazior5b2589f2013-11-08 08:01:30 +01002152 ret = ath10k_do_pci_wake(ar);
Kalle Valof3782742013-10-17 11:36:15 +03002153 if (ret) {
Kalle Valof3782742013-10-17 11:36:15 +03002154 free_irq(ar_pci->pdev->irq, ar);
Michal Kazior5b2589f2013-11-08 08:01:30 +01002155 ath10k_err("failed to wake up target: %d\n", ret);
Kalle Valof3782742013-10-17 11:36:15 +03002156 return ret;
2157 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002158
2159 /*
2160 * A potential race occurs here: The CORE_BASE write
2161 * depends on target correctly decoding AXI address but
2162 * host won't know when target writes BAR to CORE_CTRL.
2163 * This write might get lost if target has NOT written BAR.
2164 * For now, fix the race by repeating the write in below
2165 * synchronization checking.
2166 */
2167 iowrite32(PCIE_INTR_FIRMWARE_MASK |
2168 PCIE_INTR_CE_MASK_ALL,
2169 ar_pci->mem + (SOC_CORE_BASE_ADDRESS |
2170 PCIE_INTR_ENABLE_ADDRESS));
Kalle Valo5e3dd152013-06-12 20:52:10 +03002171
Michal Kazior5b2589f2013-11-08 08:01:30 +01002172 ath10k_do_pci_sleep(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002173 ath10k_info("legacy interrupt handling\n");
2174 return 0;
2175}
2176
2177static int ath10k_pci_start_intr(struct ath10k *ar)
2178{
2179 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2180 int num = MSI_NUM_REQUEST;
2181 int ret;
2182 int i;
2183
2184 tasklet_init(&ar_pci->intr_tq, ath10k_pci_tasklet, (unsigned long) ar);
2185 tasklet_init(&ar_pci->msi_fw_err, ath10k_msi_err_tasklet,
2186 (unsigned long) ar);
2187
2188 for (i = 0; i < CE_COUNT; i++) {
2189 ar_pci->pipe_info[i].ar_pci = ar_pci;
2190 tasklet_init(&ar_pci->pipe_info[i].intr,
2191 ath10k_pci_ce_tasklet,
2192 (unsigned long)&ar_pci->pipe_info[i]);
2193 }
2194
2195 if (!test_bit(ATH10K_PCI_FEATURE_MSI_X, ar_pci->features))
2196 num = 1;
2197
2198 if (num > 1) {
2199 ret = ath10k_pci_start_intr_msix(ar, num);
2200 if (ret == 0)
2201 goto exit;
2202
2203 ath10k_warn("MSI-X didn't succeed (%d), trying MSI\n", ret);
2204 num = 1;
2205 }
2206
2207 if (num == 1) {
2208 ret = ath10k_pci_start_intr_msi(ar);
2209 if (ret == 0)
2210 goto exit;
2211
2212 ath10k_warn("MSI didn't succeed (%d), trying legacy INTR\n",
2213 ret);
2214 num = 0;
2215 }
2216
2217 ret = ath10k_pci_start_intr_legacy(ar);
Kalle Valof3782742013-10-17 11:36:15 +03002218 if (ret) {
2219 ath10k_warn("Failed to start legacy interrupts: %d\n", ret);
2220 return ret;
2221 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002222
2223exit:
2224 ar_pci->num_msi_intrs = num;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002225 return ret;
2226}
2227
2228static void ath10k_pci_stop_intr(struct ath10k *ar)
2229{
2230 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2231 int i;
2232
2233 /* There's at least one interrupt irregardless whether its legacy INTR
2234 * or MSI or MSI-X */
2235 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
2236 free_irq(ar_pci->pdev->irq + i, ar);
2237
2238 if (ar_pci->num_msi_intrs > 0)
2239 pci_disable_msi(ar_pci->pdev);
2240}
2241
Michal Kaziord7fb47f2013-11-08 08:01:26 +01002242static int ath10k_pci_wait_for_target_init(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002243{
2244 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2245 int wait_limit = 300; /* 3 sec */
Kalle Valof3782742013-10-17 11:36:15 +03002246 int ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002247
Michal Kazior5b2589f2013-11-08 08:01:30 +01002248 ret = ath10k_do_pci_wake(ar);
Kalle Valof3782742013-10-17 11:36:15 +03002249 if (ret) {
Michal Kazior5b2589f2013-11-08 08:01:30 +01002250 ath10k_err("failed to wake up target: %d\n", ret);
Kalle Valof3782742013-10-17 11:36:15 +03002251 return ret;
2252 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002253
2254 while (wait_limit-- &&
2255 !(ioread32(ar_pci->mem + FW_INDICATOR_ADDRESS) &
2256 FW_IND_INITIALIZED)) {
2257 if (ar_pci->num_msi_intrs == 0)
2258 /* Fix potential race by repeating CORE_BASE writes */
2259 iowrite32(PCIE_INTR_FIRMWARE_MASK |
2260 PCIE_INTR_CE_MASK_ALL,
2261 ar_pci->mem + (SOC_CORE_BASE_ADDRESS |
2262 PCIE_INTR_ENABLE_ADDRESS));
2263 mdelay(10);
2264 }
2265
2266 if (wait_limit < 0) {
Michal Kazior5b2589f2013-11-08 08:01:30 +01002267 ath10k_err("target stalled\n");
2268 ret = -EIO;
2269 goto out;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002270 }
2271
Michal Kazior5b2589f2013-11-08 08:01:30 +01002272out:
2273 ath10k_do_pci_sleep(ar);
2274 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002275}
2276
Michal Kazior5b2589f2013-11-08 08:01:30 +01002277static int ath10k_pci_device_reset(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002278{
Michal Kazior5b2589f2013-11-08 08:01:30 +01002279 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002280 u32 val;
2281
Michal Kazior5b2589f2013-11-08 08:01:30 +01002282 ret = ath10k_do_pci_wake(ar);
2283 if (ret) {
2284 ath10k_err("failed to wake up target: %d\n",
2285 ret);
2286 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002287 }
2288
2289 /* Put Target, including PCIe, into RESET. */
Kalle Valoe479ed42013-09-01 10:01:53 +03002290 val = ath10k_pci_reg_read32(ar, SOC_GLOBAL_RESET_ADDRESS);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002291 val |= 1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002292 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002293
2294 for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
Kalle Valoe479ed42013-09-01 10:01:53 +03002295 if (ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03002296 RTC_STATE_COLD_RESET_MASK)
2297 break;
2298 msleep(1);
2299 }
2300
2301 /* Pull Target, including PCIe, out of RESET. */
2302 val &= ~1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002303 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002304
2305 for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
Kalle Valoe479ed42013-09-01 10:01:53 +03002306 if (!(ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03002307 RTC_STATE_COLD_RESET_MASK))
2308 break;
2309 msleep(1);
2310 }
2311
Michal Kazior5b2589f2013-11-08 08:01:30 +01002312 ath10k_do_pci_sleep(ar);
2313 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002314}
2315
2316static void ath10k_pci_dump_features(struct ath10k_pci *ar_pci)
2317{
2318 int i;
2319
2320 for (i = 0; i < ATH10K_PCI_FEATURE_COUNT; i++) {
2321 if (!test_bit(i, ar_pci->features))
2322 continue;
2323
2324 switch (i) {
2325 case ATH10K_PCI_FEATURE_MSI_X:
Kalle Valo24cfade2013-09-08 17:55:50 +03002326 ath10k_dbg(ATH10K_DBG_BOOT, "device supports MSI-X\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002327 break;
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002328 case ATH10K_PCI_FEATURE_SOC_POWER_SAVE:
Kalle Valo24cfade2013-09-08 17:55:50 +03002329 ath10k_dbg(ATH10K_DBG_BOOT, "QCA98XX SoC power save enabled\n");
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002330 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002331 }
2332 }
2333}
2334
2335static int ath10k_pci_probe(struct pci_dev *pdev,
2336 const struct pci_device_id *pci_dev)
2337{
2338 void __iomem *mem;
2339 int ret = 0;
2340 struct ath10k *ar;
2341 struct ath10k_pci *ar_pci;
Kalle Valoe01ae682013-09-01 11:22:14 +03002342 u32 lcr_val, chip_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002343
2344 ath10k_dbg(ATH10K_DBG_PCI, "%s\n", __func__);
2345
2346 ar_pci = kzalloc(sizeof(*ar_pci), GFP_KERNEL);
2347 if (ar_pci == NULL)
2348 return -ENOMEM;
2349
2350 ar_pci->pdev = pdev;
2351 ar_pci->dev = &pdev->dev;
2352
2353 switch (pci_dev->device) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03002354 case QCA988X_2_0_DEVICE_ID:
2355 set_bit(ATH10K_PCI_FEATURE_MSI_X, ar_pci->features);
2356 break;
2357 default:
2358 ret = -ENODEV;
2359 ath10k_err("Unkown device ID: %d\n", pci_dev->device);
2360 goto err_ar_pci;
2361 }
2362
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002363 if (ath10k_target_ps)
2364 set_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE, ar_pci->features);
2365
Kalle Valo5e3dd152013-06-12 20:52:10 +03002366 ath10k_pci_dump_features(ar_pci);
2367
Michal Kazior3a0861f2013-07-05 16:15:06 +03002368 ar = ath10k_core_create(ar_pci, ar_pci->dev, &ath10k_pci_hif_ops);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002369 if (!ar) {
2370 ath10k_err("ath10k_core_create failed!\n");
2371 ret = -EINVAL;
2372 goto err_ar_pci;
2373 }
2374
Kalle Valo5e3dd152013-06-12 20:52:10 +03002375 ar_pci->ar = ar;
2376 ar_pci->fw_indicator_address = FW_INDICATOR_ADDRESS;
2377 atomic_set(&ar_pci->keep_awake_count, 0);
2378
2379 pci_set_drvdata(pdev, ar);
2380
2381 /*
2382 * Without any knowledge of the Host, the Target may have been reset or
2383 * power cycled and its Config Space may no longer reflect the PCI
2384 * address space that was assigned earlier by the PCI infrastructure.
2385 * Refresh it now.
2386 */
2387 ret = pci_assign_resource(pdev, BAR_NUM);
2388 if (ret) {
2389 ath10k_err("cannot assign PCI space: %d\n", ret);
2390 goto err_ar;
2391 }
2392
2393 ret = pci_enable_device(pdev);
2394 if (ret) {
2395 ath10k_err("cannot enable PCI device: %d\n", ret);
2396 goto err_ar;
2397 }
2398
2399 /* Request MMIO resources */
2400 ret = pci_request_region(pdev, BAR_NUM, "ath");
2401 if (ret) {
2402 ath10k_err("PCI MMIO reservation error: %d\n", ret);
2403 goto err_device;
2404 }
2405
2406 /*
2407 * Target structures have a limit of 32 bit DMA pointers.
2408 * DMA pointers can be wider than 32 bits by default on some systems.
2409 */
2410 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2411 if (ret) {
2412 ath10k_err("32-bit DMA not available: %d\n", ret);
2413 goto err_region;
2414 }
2415
2416 ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2417 if (ret) {
2418 ath10k_err("cannot enable 32-bit consistent DMA\n");
2419 goto err_region;
2420 }
2421
2422 /* Set bus master bit in PCI_COMMAND to enable DMA */
2423 pci_set_master(pdev);
2424
2425 /*
2426 * Temporary FIX: disable ASPM
2427 * Will be removed after the OTP is programmed
2428 */
2429 pci_read_config_dword(pdev, 0x80, &lcr_val);
2430 pci_write_config_dword(pdev, 0x80, (lcr_val & 0xffffff00));
2431
2432 /* Arrange for access to Target SoC registers. */
2433 mem = pci_iomap(pdev, BAR_NUM, 0);
2434 if (!mem) {
2435 ath10k_err("PCI iomap error\n");
2436 ret = -EIO;
2437 goto err_master;
2438 }
2439
2440 ar_pci->mem = mem;
2441
2442 spin_lock_init(&ar_pci->ce_lock);
2443
Kalle Valoe01ae682013-09-01 11:22:14 +03002444 ret = ath10k_do_pci_wake(ar);
2445 if (ret) {
2446 ath10k_err("Failed to get chip id: %d\n", ret);
Wei Yongjun12eb0872013-10-30 13:24:39 +08002447 goto err_iomap;
Kalle Valoe01ae682013-09-01 11:22:14 +03002448 }
2449
Kalle Valo233eb972013-10-16 16:46:11 +03002450 chip_id = ath10k_pci_soc_read32(ar, SOC_CHIP_ID_ADDRESS);
Kalle Valoe01ae682013-09-01 11:22:14 +03002451
2452 ath10k_do_pci_sleep(ar);
2453
Kalle Valo24cfade2013-09-08 17:55:50 +03002454 ath10k_dbg(ATH10K_DBG_BOOT, "boot pci_mem 0x%p\n", ar_pci->mem);
2455
Kalle Valoe01ae682013-09-01 11:22:14 +03002456 ret = ath10k_core_register(ar, chip_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002457 if (ret) {
2458 ath10k_err("could not register driver core (%d)\n", ret);
Michal Kazior32270b62013-08-02 09:15:47 +02002459 goto err_iomap;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002460 }
2461
2462 return 0;
2463
Kalle Valo5e3dd152013-06-12 20:52:10 +03002464err_iomap:
2465 pci_iounmap(pdev, mem);
2466err_master:
2467 pci_clear_master(pdev);
2468err_region:
2469 pci_release_region(pdev, BAR_NUM);
2470err_device:
2471 pci_disable_device(pdev);
2472err_ar:
Kalle Valo5e3dd152013-06-12 20:52:10 +03002473 ath10k_core_destroy(ar);
2474err_ar_pci:
2475 /* call HIF PCI free here */
2476 kfree(ar_pci);
2477
2478 return ret;
2479}
2480
2481static void ath10k_pci_remove(struct pci_dev *pdev)
2482{
2483 struct ath10k *ar = pci_get_drvdata(pdev);
2484 struct ath10k_pci *ar_pci;
2485
2486 ath10k_dbg(ATH10K_DBG_PCI, "%s\n", __func__);
2487
2488 if (!ar)
2489 return;
2490
2491 ar_pci = ath10k_pci_priv(ar);
2492
2493 if (!ar_pci)
2494 return;
2495
2496 tasklet_kill(&ar_pci->msi_fw_err);
2497
2498 ath10k_core_unregister(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002499
Kalle Valo5e3dd152013-06-12 20:52:10 +03002500 pci_iounmap(pdev, ar_pci->mem);
2501 pci_release_region(pdev, BAR_NUM);
2502 pci_clear_master(pdev);
2503 pci_disable_device(pdev);
2504
2505 ath10k_core_destroy(ar);
2506 kfree(ar_pci);
2507}
2508
Kalle Valo5e3dd152013-06-12 20:52:10 +03002509MODULE_DEVICE_TABLE(pci, ath10k_pci_id_table);
2510
2511static struct pci_driver ath10k_pci_driver = {
2512 .name = "ath10k_pci",
2513 .id_table = ath10k_pci_id_table,
2514 .probe = ath10k_pci_probe,
2515 .remove = ath10k_pci_remove,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002516};
2517
2518static int __init ath10k_pci_init(void)
2519{
2520 int ret;
2521
2522 ret = pci_register_driver(&ath10k_pci_driver);
2523 if (ret)
2524 ath10k_err("pci_register_driver failed [%d]\n", ret);
2525
2526 return ret;
2527}
2528module_init(ath10k_pci_init);
2529
2530static void __exit ath10k_pci_exit(void)
2531{
2532 pci_unregister_driver(&ath10k_pci_driver);
2533}
2534
2535module_exit(ath10k_pci_exit);
2536
2537MODULE_AUTHOR("Qualcomm Atheros");
2538MODULE_DESCRIPTION("Driver support for Atheros QCA988X PCIe devices");
2539MODULE_LICENSE("Dual BSD/GPL");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002540MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_FW_FILE);
2541MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_OTP_FILE);
2542MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_BOARD_DATA_FILE);