blob: ecaf21315244b1659f2bccbb2afafabc0afafacb [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 Kazior8c5c5362013-07-16 09:38:50 +020055static void ath10k_pci_device_reset(struct ath10k *ar);
56static int ath10k_pci_reset_target(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
529static void ath10k_pci_wait(struct ath10k *ar)
530{
531 int n = 100;
532
533 while (n-- && !ath10k_pci_target_is_awake(ar))
534 msleep(10);
535
536 if (n < 0)
537 ath10k_warn("Unable to wakeup target\n");
538}
539
Kalle Valo3aebe542013-09-01 10:02:07 +0300540int ath10k_do_pci_wake(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300541{
542 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
543 void __iomem *pci_addr = ar_pci->mem;
544 int tot_delay = 0;
545 int curr_delay = 5;
546
547 if (atomic_read(&ar_pci->keep_awake_count) == 0) {
548 /* Force AWAKE */
549 iowrite32(PCIE_SOC_WAKE_V_MASK,
550 pci_addr + PCIE_LOCAL_BASE_ADDRESS +
551 PCIE_SOC_WAKE_ADDRESS);
552 }
553 atomic_inc(&ar_pci->keep_awake_count);
554
555 if (ar_pci->verified_awake)
Kalle Valo3aebe542013-09-01 10:02:07 +0300556 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300557
558 for (;;) {
559 if (ath10k_pci_target_is_awake(ar)) {
560 ar_pci->verified_awake = true;
Kalle Valo3aebe542013-09-01 10:02:07 +0300561 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300562 }
563
564 if (tot_delay > PCIE_WAKE_TIMEOUT) {
Kalle Valo3aebe542013-09-01 10:02:07 +0300565 ath10k_warn("target took longer %d us to wake up (awake count %d)\n",
566 PCIE_WAKE_TIMEOUT,
Kalle Valo5e3dd152013-06-12 20:52:10 +0300567 atomic_read(&ar_pci->keep_awake_count));
Kalle Valo3aebe542013-09-01 10:02:07 +0300568 return -ETIMEDOUT;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300569 }
570
571 udelay(curr_delay);
572 tot_delay += curr_delay;
573
574 if (curr_delay < 50)
575 curr_delay += 5;
576 }
577}
578
579void ath10k_do_pci_sleep(struct ath10k *ar)
580{
581 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
582 void __iomem *pci_addr = ar_pci->mem;
583
584 if (atomic_dec_and_test(&ar_pci->keep_awake_count)) {
585 /* Allow sleep */
586 ar_pci->verified_awake = false;
587 iowrite32(PCIE_SOC_WAKE_RESET,
588 pci_addr + PCIE_LOCAL_BASE_ADDRESS +
589 PCIE_SOC_WAKE_ADDRESS);
590 }
591}
592
593/*
594 * FIXME: Handle OOM properly.
595 */
596static inline
Michal Kazior87263e52013-08-27 13:08:01 +0200597struct ath10k_pci_compl *get_free_compl(struct ath10k_pci_pipe *pipe_info)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300598{
599 struct ath10k_pci_compl *compl = NULL;
600
601 spin_lock_bh(&pipe_info->pipe_lock);
602 if (list_empty(&pipe_info->compl_free)) {
603 ath10k_warn("Completion buffers are full\n");
604 goto exit;
605 }
606 compl = list_first_entry(&pipe_info->compl_free,
607 struct ath10k_pci_compl, list);
608 list_del(&compl->list);
609exit:
610 spin_unlock_bh(&pipe_info->pipe_lock);
611 return compl;
612}
613
614/* Called by lower (CE) layer when a send to Target completes. */
Michal Kazior5440ce22013-09-03 15:09:58 +0200615static void ath10k_pci_ce_send_done(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300616{
617 struct ath10k *ar = ce_state->ar;
618 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +0200619 struct ath10k_pci_pipe *pipe_info = &ar_pci->pipe_info[ce_state->id];
Kalle Valo5e3dd152013-06-12 20:52:10 +0300620 struct ath10k_pci_compl *compl;
Michal Kazior5440ce22013-09-03 15:09:58 +0200621 void *transfer_context;
622 u32 ce_data;
623 unsigned int nbytes;
624 unsigned int transfer_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300625
Michal Kazior5440ce22013-09-03 15:09:58 +0200626 while (ath10k_ce_completed_send_next(ce_state, &transfer_context,
627 &ce_data, &nbytes,
628 &transfer_id) == 0) {
Kalle Valoe9bb0aa2013-09-08 18:36:11 +0300629 spin_lock_bh(&pipe_info->pipe_lock);
630 pipe_info->num_sends_allowed++;
631 spin_unlock_bh(&pipe_info->pipe_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300632
633 compl = get_free_compl(pipe_info);
634 if (!compl)
635 break;
636
Michal Kaziorf9d8fec2013-08-13 07:54:56 +0200637 compl->state = ATH10K_PCI_COMPL_SEND;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300638 compl->ce_state = ce_state;
639 compl->pipe_info = pipe_info;
Kalle Valoaa5c1db2013-09-01 10:01:46 +0300640 compl->skb = transfer_context;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300641 compl->nbytes = nbytes;
642 compl->transfer_id = transfer_id;
643 compl->flags = 0;
644
645 /*
646 * Add the completion to the processing queue.
647 */
648 spin_lock_bh(&ar_pci->compl_lock);
649 list_add_tail(&compl->list, &ar_pci->compl_process);
650 spin_unlock_bh(&ar_pci->compl_lock);
Michal Kazior5440ce22013-09-03 15:09:58 +0200651 }
Kalle Valo5e3dd152013-06-12 20:52:10 +0300652
653 ath10k_pci_process_ce(ar);
654}
655
656/* Called by lower (CE) layer when data is received from the Target. */
Michal Kazior5440ce22013-09-03 15:09:58 +0200657static void ath10k_pci_ce_recv_data(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300658{
659 struct ath10k *ar = ce_state->ar;
660 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +0200661 struct ath10k_pci_pipe *pipe_info = &ar_pci->pipe_info[ce_state->id];
Kalle Valo5e3dd152013-06-12 20:52:10 +0300662 struct ath10k_pci_compl *compl;
663 struct sk_buff *skb;
Michal Kazior5440ce22013-09-03 15:09:58 +0200664 void *transfer_context;
665 u32 ce_data;
666 unsigned int nbytes;
667 unsigned int transfer_id;
668 unsigned int flags;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300669
Michal Kazior5440ce22013-09-03 15:09:58 +0200670 while (ath10k_ce_completed_recv_next(ce_state, &transfer_context,
671 &ce_data, &nbytes, &transfer_id,
672 &flags) == 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300673 compl = get_free_compl(pipe_info);
674 if (!compl)
675 break;
676
Michal Kaziorf9d8fec2013-08-13 07:54:56 +0200677 compl->state = ATH10K_PCI_COMPL_RECV;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300678 compl->ce_state = ce_state;
679 compl->pipe_info = pipe_info;
Kalle Valoaa5c1db2013-09-01 10:01:46 +0300680 compl->skb = transfer_context;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300681 compl->nbytes = nbytes;
682 compl->transfer_id = transfer_id;
683 compl->flags = flags;
684
685 skb = transfer_context;
686 dma_unmap_single(ar->dev, ATH10K_SKB_CB(skb)->paddr,
687 skb->len + skb_tailroom(skb),
688 DMA_FROM_DEVICE);
689 /*
690 * Add the completion to the processing queue.
691 */
692 spin_lock_bh(&ar_pci->compl_lock);
693 list_add_tail(&compl->list, &ar_pci->compl_process);
694 spin_unlock_bh(&ar_pci->compl_lock);
Michal Kazior5440ce22013-09-03 15:09:58 +0200695 }
Kalle Valo5e3dd152013-06-12 20:52:10 +0300696
697 ath10k_pci_process_ce(ar);
698}
699
700/* Send the first nbytes bytes of the buffer */
701static int ath10k_pci_hif_send_head(struct ath10k *ar, u8 pipe_id,
702 unsigned int transfer_id,
703 unsigned int bytes, struct sk_buff *nbuf)
704{
705 struct ath10k_skb_cb *skb_cb = ATH10K_SKB_CB(nbuf);
706 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +0200707 struct ath10k_pci_pipe *pipe_info = &(ar_pci->pipe_info[pipe_id]);
Michal Kazior2aa39112013-08-27 13:08:02 +0200708 struct ath10k_ce_pipe *ce_hdl = pipe_info->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300709 unsigned int len;
710 u32 flags = 0;
711 int ret;
712
Kalle Valo5e3dd152013-06-12 20:52:10 +0300713 len = min(bytes, nbuf->len);
714 bytes -= len;
715
716 if (len & 3)
717 ath10k_warn("skb not aligned to 4-byte boundary (%d)\n", len);
718
719 ath10k_dbg(ATH10K_DBG_PCI,
720 "pci send data vaddr %p paddr 0x%llx len %d as %d bytes\n",
721 nbuf->data, (unsigned long long) skb_cb->paddr,
722 nbuf->len, len);
723 ath10k_dbg_dump(ATH10K_DBG_PCI_DUMP, NULL,
724 "ath10k tx: data: ",
725 nbuf->data, nbuf->len);
726
Kalle Valo5e3dd152013-06-12 20:52:10 +0300727 /* Make sure we have resources to handle this request */
728 spin_lock_bh(&pipe_info->pipe_lock);
729 if (!pipe_info->num_sends_allowed) {
730 ath10k_warn("Pipe: %d is full\n", pipe_id);
731 spin_unlock_bh(&pipe_info->pipe_lock);
732 return -ENOSR;
733 }
734 pipe_info->num_sends_allowed--;
735 spin_unlock_bh(&pipe_info->pipe_lock);
736
Kalle Valoe9bb0aa2013-09-08 18:36:11 +0300737 ret = ath10k_ce_sendlist_send(ce_hdl, nbuf, transfer_id,
738 skb_cb->paddr, len, flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300739 if (ret)
740 ath10k_warn("CE send failed: %p\n", nbuf);
741
742 return ret;
743}
744
745static u16 ath10k_pci_hif_get_free_queue_number(struct ath10k *ar, u8 pipe)
746{
747 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +0200748 struct ath10k_pci_pipe *pipe_info = &(ar_pci->pipe_info[pipe]);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300749 int ret;
750
751 spin_lock_bh(&pipe_info->pipe_lock);
752 ret = pipe_info->num_sends_allowed;
753 spin_unlock_bh(&pipe_info->pipe_lock);
754
755 return ret;
756}
757
758static void ath10k_pci_hif_dump_area(struct ath10k *ar)
759{
760 u32 reg_dump_area = 0;
761 u32 reg_dump_values[REG_DUMP_COUNT_QCA988X] = {};
762 u32 host_addr;
763 int ret;
764 u32 i;
765
766 ath10k_err("firmware crashed!\n");
767 ath10k_err("hardware name %s version 0x%x\n",
768 ar->hw_params.name, ar->target_version);
769 ath10k_err("firmware version: %u.%u.%u.%u\n", ar->fw_version_major,
770 ar->fw_version_minor, ar->fw_version_release,
771 ar->fw_version_build);
772
773 host_addr = host_interest_item_address(HI_ITEM(hi_failure_state));
774 if (ath10k_pci_diag_read_mem(ar, host_addr,
775 &reg_dump_area, sizeof(u32)) != 0) {
776 ath10k_warn("could not read hi_failure_state\n");
777 return;
778 }
779
780 ath10k_err("target register Dump Location: 0x%08X\n", reg_dump_area);
781
782 ret = ath10k_pci_diag_read_mem(ar, reg_dump_area,
783 &reg_dump_values[0],
784 REG_DUMP_COUNT_QCA988X * sizeof(u32));
785 if (ret != 0) {
786 ath10k_err("could not dump FW Dump Area\n");
787 return;
788 }
789
790 BUILD_BUG_ON(REG_DUMP_COUNT_QCA988X % 4);
791
792 ath10k_err("target Register Dump\n");
793 for (i = 0; i < REG_DUMP_COUNT_QCA988X; i += 4)
794 ath10k_err("[%02d]: 0x%08X 0x%08X 0x%08X 0x%08X\n",
795 i,
796 reg_dump_values[i],
797 reg_dump_values[i + 1],
798 reg_dump_values[i + 2],
799 reg_dump_values[i + 3]);
Michal Kazioraffd3212013-07-16 09:54:35 +0200800
801 ieee80211_queue_work(ar->hw, &ar->restart_work);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300802}
803
804static void ath10k_pci_hif_send_complete_check(struct ath10k *ar, u8 pipe,
805 int force)
806{
807 if (!force) {
808 int resources;
809 /*
810 * Decide whether to actually poll for completions, or just
811 * wait for a later chance.
812 * If there seem to be plenty of resources left, then just wait
813 * since checking involves reading a CE register, which is a
814 * relatively expensive operation.
815 */
816 resources = ath10k_pci_hif_get_free_queue_number(ar, pipe);
817
818 /*
819 * If at least 50% of the total resources are still available,
820 * don't bother checking again yet.
821 */
822 if (resources > (host_ce_config_wlan[pipe].src_nentries >> 1))
823 return;
824 }
825 ath10k_ce_per_engine_service(ar, pipe);
826}
827
Michal Kaziore799bbf2013-07-05 16:15:12 +0300828static void ath10k_pci_hif_set_callbacks(struct ath10k *ar,
829 struct ath10k_hif_cb *callbacks)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300830{
831 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
832
833 ath10k_dbg(ATH10K_DBG_PCI, "%s\n", __func__);
834
835 memcpy(&ar_pci->msg_callbacks_current, callbacks,
836 sizeof(ar_pci->msg_callbacks_current));
837}
838
839static int ath10k_pci_start_ce(struct ath10k *ar)
840{
841 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2aa39112013-08-27 13:08:02 +0200842 struct ath10k_ce_pipe *ce_diag = ar_pci->ce_diag;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300843 const struct ce_attr *attr;
Michal Kazior87263e52013-08-27 13:08:01 +0200844 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300845 struct ath10k_pci_compl *compl;
846 int i, pipe_num, completions, disable_interrupts;
847
848 spin_lock_init(&ar_pci->compl_lock);
849 INIT_LIST_HEAD(&ar_pci->compl_process);
850
851 for (pipe_num = 0; pipe_num < ar_pci->ce_count; pipe_num++) {
852 pipe_info = &ar_pci->pipe_info[pipe_num];
853
854 spin_lock_init(&pipe_info->pipe_lock);
855 INIT_LIST_HEAD(&pipe_info->compl_free);
856
857 /* Handle Diagnostic CE specially */
858 if (pipe_info->ce_hdl == ce_diag)
859 continue;
860
861 attr = &host_ce_config_wlan[pipe_num];
862 completions = 0;
863
864 if (attr->src_nentries) {
865 disable_interrupts = attr->flags & CE_ATTR_DIS_INTR;
866 ath10k_ce_send_cb_register(pipe_info->ce_hdl,
867 ath10k_pci_ce_send_done,
868 disable_interrupts);
869 completions += attr->src_nentries;
870 pipe_info->num_sends_allowed = attr->src_nentries - 1;
871 }
872
873 if (attr->dest_nentries) {
874 ath10k_ce_recv_cb_register(pipe_info->ce_hdl,
875 ath10k_pci_ce_recv_data);
876 completions += attr->dest_nentries;
877 }
878
879 if (completions == 0)
880 continue;
881
882 for (i = 0; i < completions; i++) {
Michal Kaziorffe5daa2013-08-13 07:54:55 +0200883 compl = kmalloc(sizeof(*compl), GFP_KERNEL);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300884 if (!compl) {
885 ath10k_warn("No memory for completion state\n");
886 ath10k_pci_stop_ce(ar);
887 return -ENOMEM;
888 }
889
Michal Kaziorf9d8fec2013-08-13 07:54:56 +0200890 compl->state = ATH10K_PCI_COMPL_FREE;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300891 list_add_tail(&compl->list, &pipe_info->compl_free);
892 }
893 }
894
895 return 0;
896}
897
898static void ath10k_pci_stop_ce(struct ath10k *ar)
899{
900 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
901 struct ath10k_pci_compl *compl;
902 struct sk_buff *skb;
903 int i;
904
905 ath10k_ce_disable_interrupts(ar);
906
907 /* Cancel the pending tasklet */
908 tasklet_kill(&ar_pci->intr_tq);
909
910 for (i = 0; i < CE_COUNT; i++)
911 tasklet_kill(&ar_pci->pipe_info[i].intr);
912
913 /* Mark pending completions as aborted, so that upper layers free up
914 * their associated resources */
915 spin_lock_bh(&ar_pci->compl_lock);
916 list_for_each_entry(compl, &ar_pci->compl_process, list) {
Kalle Valoaa5c1db2013-09-01 10:01:46 +0300917 skb = compl->skb;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300918 ATH10K_SKB_CB(skb)->is_aborted = true;
919 }
920 spin_unlock_bh(&ar_pci->compl_lock);
921}
922
923static void ath10k_pci_cleanup_ce(struct ath10k *ar)
924{
925 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
926 struct ath10k_pci_compl *compl, *tmp;
Michal Kazior87263e52013-08-27 13:08:01 +0200927 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300928 struct sk_buff *netbuf;
929 int pipe_num;
930
931 /* Free pending completions. */
932 spin_lock_bh(&ar_pci->compl_lock);
933 if (!list_empty(&ar_pci->compl_process))
934 ath10k_warn("pending completions still present! possible memory leaks.\n");
935
936 list_for_each_entry_safe(compl, tmp, &ar_pci->compl_process, list) {
937 list_del(&compl->list);
Kalle Valoaa5c1db2013-09-01 10:01:46 +0300938 netbuf = compl->skb;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300939 dev_kfree_skb_any(netbuf);
940 kfree(compl);
941 }
942 spin_unlock_bh(&ar_pci->compl_lock);
943
944 /* Free unused completions for each pipe. */
945 for (pipe_num = 0; pipe_num < ar_pci->ce_count; pipe_num++) {
946 pipe_info = &ar_pci->pipe_info[pipe_num];
947
948 spin_lock_bh(&pipe_info->pipe_lock);
949 list_for_each_entry_safe(compl, tmp,
950 &pipe_info->compl_free, list) {
951 list_del(&compl->list);
952 kfree(compl);
953 }
954 spin_unlock_bh(&pipe_info->pipe_lock);
955 }
956}
957
958static void ath10k_pci_process_ce(struct ath10k *ar)
959{
960 struct ath10k_pci *ar_pci = ar->hif.priv;
961 struct ath10k_hif_cb *cb = &ar_pci->msg_callbacks_current;
962 struct ath10k_pci_compl *compl;
963 struct sk_buff *skb;
964 unsigned int nbytes;
965 int ret, send_done = 0;
966
967 /* Upper layers aren't ready to handle tx/rx completions in parallel so
968 * we must serialize all completion processing. */
969
970 spin_lock_bh(&ar_pci->compl_lock);
971 if (ar_pci->compl_processing) {
972 spin_unlock_bh(&ar_pci->compl_lock);
973 return;
974 }
975 ar_pci->compl_processing = true;
976 spin_unlock_bh(&ar_pci->compl_lock);
977
978 for (;;) {
979 spin_lock_bh(&ar_pci->compl_lock);
980 if (list_empty(&ar_pci->compl_process)) {
981 spin_unlock_bh(&ar_pci->compl_lock);
982 break;
983 }
984 compl = list_first_entry(&ar_pci->compl_process,
985 struct ath10k_pci_compl, list);
986 list_del(&compl->list);
987 spin_unlock_bh(&ar_pci->compl_lock);
988
Michal Kaziorf9d8fec2013-08-13 07:54:56 +0200989 switch (compl->state) {
990 case ATH10K_PCI_COMPL_SEND:
Kalle Valo5e3dd152013-06-12 20:52:10 +0300991 cb->tx_completion(ar,
Kalle Valoaa5c1db2013-09-01 10:01:46 +0300992 compl->skb,
Kalle Valo5e3dd152013-06-12 20:52:10 +0300993 compl->transfer_id);
994 send_done = 1;
Michal Kaziorf9d8fec2013-08-13 07:54:56 +0200995 break;
996 case ATH10K_PCI_COMPL_RECV:
Kalle Valo5e3dd152013-06-12 20:52:10 +0300997 ret = ath10k_pci_post_rx_pipe(compl->pipe_info, 1);
998 if (ret) {
999 ath10k_warn("Unable to post recv buffer for pipe: %d\n",
1000 compl->pipe_info->pipe_num);
1001 break;
1002 }
1003
Kalle Valoaa5c1db2013-09-01 10:01:46 +03001004 skb = compl->skb;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001005 nbytes = compl->nbytes;
1006
1007 ath10k_dbg(ATH10K_DBG_PCI,
1008 "ath10k_pci_ce_recv_data netbuf=%p nbytes=%d\n",
1009 skb, nbytes);
1010 ath10k_dbg_dump(ATH10K_DBG_PCI_DUMP, NULL,
1011 "ath10k rx: ", skb->data, nbytes);
1012
1013 if (skb->len + skb_tailroom(skb) >= nbytes) {
1014 skb_trim(skb, 0);
1015 skb_put(skb, nbytes);
1016 cb->rx_completion(ar, skb,
1017 compl->pipe_info->pipe_num);
1018 } else {
1019 ath10k_warn("rxed more than expected (nbytes %d, max %d)",
1020 nbytes,
1021 skb->len + skb_tailroom(skb));
1022 }
Michal Kaziorf9d8fec2013-08-13 07:54:56 +02001023 break;
1024 case ATH10K_PCI_COMPL_FREE:
1025 ath10k_warn("free completion cannot be processed\n");
1026 break;
1027 default:
1028 ath10k_warn("invalid completion state (%d)\n",
1029 compl->state);
1030 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001031 }
1032
Michal Kaziorf9d8fec2013-08-13 07:54:56 +02001033 compl->state = ATH10K_PCI_COMPL_FREE;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001034
1035 /*
1036 * Add completion back to the pipe's free list.
1037 */
1038 spin_lock_bh(&compl->pipe_info->pipe_lock);
1039 list_add_tail(&compl->list, &compl->pipe_info->compl_free);
1040 compl->pipe_info->num_sends_allowed += send_done;
1041 spin_unlock_bh(&compl->pipe_info->pipe_lock);
1042 }
1043
1044 spin_lock_bh(&ar_pci->compl_lock);
1045 ar_pci->compl_processing = false;
1046 spin_unlock_bh(&ar_pci->compl_lock);
1047}
1048
1049/* TODO - temporary mapping while we have too few CE's */
1050static int ath10k_pci_hif_map_service_to_pipe(struct ath10k *ar,
1051 u16 service_id, u8 *ul_pipe,
1052 u8 *dl_pipe, int *ul_is_polled,
1053 int *dl_is_polled)
1054{
1055 int ret = 0;
1056
1057 /* polling for received messages not supported */
1058 *dl_is_polled = 0;
1059
1060 switch (service_id) {
1061 case ATH10K_HTC_SVC_ID_HTT_DATA_MSG:
1062 /*
1063 * Host->target HTT gets its own pipe, so it can be polled
1064 * while other pipes are interrupt driven.
1065 */
1066 *ul_pipe = 4;
1067 /*
1068 * Use the same target->host pipe for HTC ctrl, HTC raw
1069 * streams, and HTT.
1070 */
1071 *dl_pipe = 1;
1072 break;
1073
1074 case ATH10K_HTC_SVC_ID_RSVD_CTRL:
1075 case ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS:
1076 /*
1077 * Note: HTC_RAW_STREAMS_SVC is currently unused, and
1078 * HTC_CTRL_RSVD_SVC could share the same pipe as the
1079 * WMI services. So, if another CE is needed, change
1080 * this to *ul_pipe = 3, which frees up CE 0.
1081 */
1082 /* *ul_pipe = 3; */
1083 *ul_pipe = 0;
1084 *dl_pipe = 1;
1085 break;
1086
1087 case ATH10K_HTC_SVC_ID_WMI_DATA_BK:
1088 case ATH10K_HTC_SVC_ID_WMI_DATA_BE:
1089 case ATH10K_HTC_SVC_ID_WMI_DATA_VI:
1090 case ATH10K_HTC_SVC_ID_WMI_DATA_VO:
1091
1092 case ATH10K_HTC_SVC_ID_WMI_CONTROL:
1093 *ul_pipe = 3;
1094 *dl_pipe = 2;
1095 break;
1096
1097 /* pipe 5 unused */
1098 /* pipe 6 reserved */
1099 /* pipe 7 reserved */
1100
1101 default:
1102 ret = -1;
1103 break;
1104 }
1105 *ul_is_polled =
1106 (host_ce_config_wlan[*ul_pipe].flags & CE_ATTR_DIS_INTR) != 0;
1107
1108 return ret;
1109}
1110
1111static void ath10k_pci_hif_get_default_pipe(struct ath10k *ar,
1112 u8 *ul_pipe, u8 *dl_pipe)
1113{
1114 int ul_is_polled, dl_is_polled;
1115
1116 (void)ath10k_pci_hif_map_service_to_pipe(ar,
1117 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1118 ul_pipe,
1119 dl_pipe,
1120 &ul_is_polled,
1121 &dl_is_polled);
1122}
1123
Michal Kazior87263e52013-08-27 13:08:01 +02001124static int ath10k_pci_post_rx_pipe(struct ath10k_pci_pipe *pipe_info,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001125 int num)
1126{
1127 struct ath10k *ar = pipe_info->hif_ce_state;
1128 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2aa39112013-08-27 13:08:02 +02001129 struct ath10k_ce_pipe *ce_state = pipe_info->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001130 struct sk_buff *skb;
1131 dma_addr_t ce_data;
1132 int i, ret = 0;
1133
1134 if (pipe_info->buf_sz == 0)
1135 return 0;
1136
1137 for (i = 0; i < num; i++) {
1138 skb = dev_alloc_skb(pipe_info->buf_sz);
1139 if (!skb) {
1140 ath10k_warn("could not allocate skbuff for pipe %d\n",
1141 num);
1142 ret = -ENOMEM;
1143 goto err;
1144 }
1145
1146 WARN_ONCE((unsigned long)skb->data & 3, "unaligned skb");
1147
1148 ce_data = dma_map_single(ar->dev, skb->data,
1149 skb->len + skb_tailroom(skb),
1150 DMA_FROM_DEVICE);
1151
1152 if (unlikely(dma_mapping_error(ar->dev, ce_data))) {
1153 ath10k_warn("could not dma map skbuff\n");
1154 dev_kfree_skb_any(skb);
1155 ret = -EIO;
1156 goto err;
1157 }
1158
1159 ATH10K_SKB_CB(skb)->paddr = ce_data;
1160
1161 pci_dma_sync_single_for_device(ar_pci->pdev, ce_data,
1162 pipe_info->buf_sz,
1163 PCI_DMA_FROMDEVICE);
1164
1165 ret = ath10k_ce_recv_buf_enqueue(ce_state, (void *)skb,
1166 ce_data);
1167 if (ret) {
1168 ath10k_warn("could not enqueue to pipe %d (%d)\n",
1169 num, ret);
1170 goto err;
1171 }
1172 }
1173
1174 return ret;
1175
1176err:
1177 ath10k_pci_rx_pipe_cleanup(pipe_info);
1178 return ret;
1179}
1180
1181static int ath10k_pci_post_rx(struct ath10k *ar)
1182{
1183 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +02001184 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001185 const struct ce_attr *attr;
1186 int pipe_num, ret = 0;
1187
1188 for (pipe_num = 0; pipe_num < ar_pci->ce_count; pipe_num++) {
1189 pipe_info = &ar_pci->pipe_info[pipe_num];
1190 attr = &host_ce_config_wlan[pipe_num];
1191
1192 if (attr->dest_nentries == 0)
1193 continue;
1194
1195 ret = ath10k_pci_post_rx_pipe(pipe_info,
1196 attr->dest_nentries - 1);
1197 if (ret) {
1198 ath10k_warn("Unable to replenish recv buffers for pipe: %d\n",
1199 pipe_num);
1200
1201 for (; pipe_num >= 0; pipe_num--) {
1202 pipe_info = &ar_pci->pipe_info[pipe_num];
1203 ath10k_pci_rx_pipe_cleanup(pipe_info);
1204 }
1205 return ret;
1206 }
1207 }
1208
1209 return 0;
1210}
1211
1212static int ath10k_pci_hif_start(struct ath10k *ar)
1213{
1214 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1215 int ret;
1216
1217 ret = ath10k_pci_start_ce(ar);
1218 if (ret) {
1219 ath10k_warn("could not start CE (%d)\n", ret);
1220 return ret;
1221 }
1222
1223 /* Post buffers once to start things off. */
1224 ret = ath10k_pci_post_rx(ar);
1225 if (ret) {
1226 ath10k_warn("could not post rx pipes (%d)\n", ret);
1227 return ret;
1228 }
1229
1230 ar_pci->started = 1;
1231 return 0;
1232}
1233
Michal Kazior87263e52013-08-27 13:08:01 +02001234static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe *pipe_info)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001235{
1236 struct ath10k *ar;
1237 struct ath10k_pci *ar_pci;
Michal Kazior2aa39112013-08-27 13:08:02 +02001238 struct ath10k_ce_pipe *ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001239 u32 buf_sz;
1240 struct sk_buff *netbuf;
1241 u32 ce_data;
1242
1243 buf_sz = pipe_info->buf_sz;
1244
1245 /* Unused Copy Engine */
1246 if (buf_sz == 0)
1247 return;
1248
1249 ar = pipe_info->hif_ce_state;
1250 ar_pci = ath10k_pci_priv(ar);
1251
1252 if (!ar_pci->started)
1253 return;
1254
1255 ce_hdl = pipe_info->ce_hdl;
1256
1257 while (ath10k_ce_revoke_recv_next(ce_hdl, (void **)&netbuf,
1258 &ce_data) == 0) {
1259 dma_unmap_single(ar->dev, ATH10K_SKB_CB(netbuf)->paddr,
1260 netbuf->len + skb_tailroom(netbuf),
1261 DMA_FROM_DEVICE);
1262 dev_kfree_skb_any(netbuf);
1263 }
1264}
1265
Michal Kazior87263e52013-08-27 13:08:01 +02001266static void ath10k_pci_tx_pipe_cleanup(struct ath10k_pci_pipe *pipe_info)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001267{
1268 struct ath10k *ar;
1269 struct ath10k_pci *ar_pci;
Michal Kazior2aa39112013-08-27 13:08:02 +02001270 struct ath10k_ce_pipe *ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001271 struct sk_buff *netbuf;
1272 u32 ce_data;
1273 unsigned int nbytes;
1274 unsigned int id;
1275 u32 buf_sz;
1276
1277 buf_sz = pipe_info->buf_sz;
1278
1279 /* Unused Copy Engine */
1280 if (buf_sz == 0)
1281 return;
1282
1283 ar = pipe_info->hif_ce_state;
1284 ar_pci = ath10k_pci_priv(ar);
1285
1286 if (!ar_pci->started)
1287 return;
1288
1289 ce_hdl = pipe_info->ce_hdl;
1290
1291 while (ath10k_ce_cancel_send_next(ce_hdl, (void **)&netbuf,
1292 &ce_data, &nbytes, &id) == 0) {
Kalle Valoe9bb0aa2013-09-08 18:36:11 +03001293 /*
1294 * Indicate the completion to higer layer to free
1295 * the buffer
1296 */
1297 ATH10K_SKB_CB(netbuf)->is_aborted = true;
1298 ar_pci->msg_callbacks_current.tx_completion(ar,
1299 netbuf,
1300 id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001301 }
1302}
1303
1304/*
1305 * Cleanup residual buffers for device shutdown:
1306 * buffers that were enqueued for receive
1307 * buffers that were to be sent
1308 * Note: Buffers that had completed but which were
1309 * not yet processed are on a completion queue. They
1310 * are handled when the completion thread shuts down.
1311 */
1312static void ath10k_pci_buffer_cleanup(struct ath10k *ar)
1313{
1314 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1315 int pipe_num;
1316
1317 for (pipe_num = 0; pipe_num < ar_pci->ce_count; pipe_num++) {
Michal Kazior87263e52013-08-27 13:08:01 +02001318 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001319
1320 pipe_info = &ar_pci->pipe_info[pipe_num];
1321 ath10k_pci_rx_pipe_cleanup(pipe_info);
1322 ath10k_pci_tx_pipe_cleanup(pipe_info);
1323 }
1324}
1325
1326static void ath10k_pci_ce_deinit(struct ath10k *ar)
1327{
1328 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +02001329 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001330 int pipe_num;
1331
1332 for (pipe_num = 0; pipe_num < ar_pci->ce_count; pipe_num++) {
1333 pipe_info = &ar_pci->pipe_info[pipe_num];
1334 if (pipe_info->ce_hdl) {
1335 ath10k_ce_deinit(pipe_info->ce_hdl);
1336 pipe_info->ce_hdl = NULL;
1337 pipe_info->buf_sz = 0;
1338 }
1339 }
1340}
1341
Michal Kazior32270b62013-08-02 09:15:47 +02001342static void ath10k_pci_disable_irqs(struct ath10k *ar)
1343{
1344 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1345 int i;
1346
1347 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
1348 disable_irq(ar_pci->pdev->irq + i);
1349}
1350
Kalle Valo5e3dd152013-06-12 20:52:10 +03001351static void ath10k_pci_hif_stop(struct ath10k *ar)
1352{
Michal Kazior32270b62013-08-02 09:15:47 +02001353 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1354
Kalle Valo5e3dd152013-06-12 20:52:10 +03001355 ath10k_dbg(ATH10K_DBG_PCI, "%s\n", __func__);
1356
Michal Kazior32270b62013-08-02 09:15:47 +02001357 /* Irqs are never explicitly re-enabled. They are implicitly re-enabled
1358 * by ath10k_pci_start_intr(). */
1359 ath10k_pci_disable_irqs(ar);
1360
Kalle Valo5e3dd152013-06-12 20:52:10 +03001361 ath10k_pci_stop_ce(ar);
1362
1363 /* At this point, asynchronous threads are stopped, the target should
1364 * not DMA nor interrupt. We process the leftovers and then free
1365 * everything else up. */
1366
1367 ath10k_pci_process_ce(ar);
1368 ath10k_pci_cleanup_ce(ar);
1369 ath10k_pci_buffer_cleanup(ar);
Michal Kazior32270b62013-08-02 09:15:47 +02001370
1371 ar_pci->started = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001372}
1373
1374static int ath10k_pci_hif_exchange_bmi_msg(struct ath10k *ar,
1375 void *req, u32 req_len,
1376 void *resp, u32 *resp_len)
1377{
1378 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2aa39112013-08-27 13:08:02 +02001379 struct ath10k_pci_pipe *pci_tx = &ar_pci->pipe_info[BMI_CE_NUM_TO_TARG];
1380 struct ath10k_pci_pipe *pci_rx = &ar_pci->pipe_info[BMI_CE_NUM_TO_HOST];
1381 struct ath10k_ce_pipe *ce_tx = pci_tx->ce_hdl;
1382 struct ath10k_ce_pipe *ce_rx = pci_rx->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001383 dma_addr_t req_paddr = 0;
1384 dma_addr_t resp_paddr = 0;
1385 struct bmi_xfer xfer = {};
1386 void *treq, *tresp = NULL;
1387 int ret = 0;
1388
1389 if (resp && !resp_len)
1390 return -EINVAL;
1391
1392 if (resp && resp_len && *resp_len == 0)
1393 return -EINVAL;
1394
1395 treq = kmemdup(req, req_len, GFP_KERNEL);
1396 if (!treq)
1397 return -ENOMEM;
1398
1399 req_paddr = dma_map_single(ar->dev, treq, req_len, DMA_TO_DEVICE);
1400 ret = dma_mapping_error(ar->dev, req_paddr);
1401 if (ret)
1402 goto err_dma;
1403
1404 if (resp && resp_len) {
1405 tresp = kzalloc(*resp_len, GFP_KERNEL);
1406 if (!tresp) {
1407 ret = -ENOMEM;
1408 goto err_req;
1409 }
1410
1411 resp_paddr = dma_map_single(ar->dev, tresp, *resp_len,
1412 DMA_FROM_DEVICE);
1413 ret = dma_mapping_error(ar->dev, resp_paddr);
1414 if (ret)
1415 goto err_req;
1416
1417 xfer.wait_for_resp = true;
1418 xfer.resp_len = 0;
1419
1420 ath10k_ce_recv_buf_enqueue(ce_rx, &xfer, resp_paddr);
1421 }
1422
1423 init_completion(&xfer.done);
1424
1425 ret = ath10k_ce_send(ce_tx, &xfer, req_paddr, req_len, -1, 0);
1426 if (ret)
1427 goto err_resp;
1428
1429 ret = wait_for_completion_timeout(&xfer.done,
1430 BMI_COMMUNICATION_TIMEOUT_HZ);
1431 if (ret <= 0) {
1432 u32 unused_buffer;
1433 unsigned int unused_nbytes;
1434 unsigned int unused_id;
1435
1436 ret = -ETIMEDOUT;
1437 ath10k_ce_cancel_send_next(ce_tx, NULL, &unused_buffer,
1438 &unused_nbytes, &unused_id);
1439 } else {
1440 /* non-zero means we did not time out */
1441 ret = 0;
1442 }
1443
1444err_resp:
1445 if (resp) {
1446 u32 unused_buffer;
1447
1448 ath10k_ce_revoke_recv_next(ce_rx, NULL, &unused_buffer);
1449 dma_unmap_single(ar->dev, resp_paddr,
1450 *resp_len, DMA_FROM_DEVICE);
1451 }
1452err_req:
1453 dma_unmap_single(ar->dev, req_paddr, req_len, DMA_TO_DEVICE);
1454
1455 if (ret == 0 && resp_len) {
1456 *resp_len = min(*resp_len, xfer.resp_len);
1457 memcpy(resp, tresp, xfer.resp_len);
1458 }
1459err_dma:
1460 kfree(treq);
1461 kfree(tresp);
1462
1463 return ret;
1464}
1465
Michal Kazior5440ce22013-09-03 15:09:58 +02001466static void ath10k_pci_bmi_send_done(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001467{
Michal Kazior5440ce22013-09-03 15:09:58 +02001468 struct bmi_xfer *xfer;
1469 u32 ce_data;
1470 unsigned int nbytes;
1471 unsigned int transfer_id;
1472
1473 if (ath10k_ce_completed_send_next(ce_state, (void **)&xfer, &ce_data,
1474 &nbytes, &transfer_id))
1475 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001476
1477 if (xfer->wait_for_resp)
1478 return;
1479
1480 complete(&xfer->done);
1481}
1482
Michal Kazior5440ce22013-09-03 15:09:58 +02001483static void ath10k_pci_bmi_recv_data(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001484{
Michal Kazior5440ce22013-09-03 15:09:58 +02001485 struct bmi_xfer *xfer;
1486 u32 ce_data;
1487 unsigned int nbytes;
1488 unsigned int transfer_id;
1489 unsigned int flags;
1490
1491 if (ath10k_ce_completed_recv_next(ce_state, (void **)&xfer, &ce_data,
1492 &nbytes, &transfer_id, &flags))
1493 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001494
1495 if (!xfer->wait_for_resp) {
1496 ath10k_warn("unexpected: BMI data received; ignoring\n");
1497 return;
1498 }
1499
1500 xfer->resp_len = nbytes;
1501 complete(&xfer->done);
1502}
1503
1504/*
1505 * Map from service/endpoint to Copy Engine.
1506 * This table is derived from the CE_PCI TABLE, above.
1507 * It is passed to the Target at startup for use by firmware.
1508 */
1509static const struct service_to_pipe target_service_to_ce_map_wlan[] = {
1510 {
1511 ATH10K_HTC_SVC_ID_WMI_DATA_VO,
1512 PIPEDIR_OUT, /* out = UL = host -> target */
1513 3,
1514 },
1515 {
1516 ATH10K_HTC_SVC_ID_WMI_DATA_VO,
1517 PIPEDIR_IN, /* in = DL = target -> host */
1518 2,
1519 },
1520 {
1521 ATH10K_HTC_SVC_ID_WMI_DATA_BK,
1522 PIPEDIR_OUT, /* out = UL = host -> target */
1523 3,
1524 },
1525 {
1526 ATH10K_HTC_SVC_ID_WMI_DATA_BK,
1527 PIPEDIR_IN, /* in = DL = target -> host */
1528 2,
1529 },
1530 {
1531 ATH10K_HTC_SVC_ID_WMI_DATA_BE,
1532 PIPEDIR_OUT, /* out = UL = host -> target */
1533 3,
1534 },
1535 {
1536 ATH10K_HTC_SVC_ID_WMI_DATA_BE,
1537 PIPEDIR_IN, /* in = DL = target -> host */
1538 2,
1539 },
1540 {
1541 ATH10K_HTC_SVC_ID_WMI_DATA_VI,
1542 PIPEDIR_OUT, /* out = UL = host -> target */
1543 3,
1544 },
1545 {
1546 ATH10K_HTC_SVC_ID_WMI_DATA_VI,
1547 PIPEDIR_IN, /* in = DL = target -> host */
1548 2,
1549 },
1550 {
1551 ATH10K_HTC_SVC_ID_WMI_CONTROL,
1552 PIPEDIR_OUT, /* out = UL = host -> target */
1553 3,
1554 },
1555 {
1556 ATH10K_HTC_SVC_ID_WMI_CONTROL,
1557 PIPEDIR_IN, /* in = DL = target -> host */
1558 2,
1559 },
1560 {
1561 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1562 PIPEDIR_OUT, /* out = UL = host -> target */
1563 0, /* could be moved to 3 (share with WMI) */
1564 },
1565 {
1566 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1567 PIPEDIR_IN, /* in = DL = target -> host */
1568 1,
1569 },
1570 {
1571 ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS, /* not currently used */
1572 PIPEDIR_OUT, /* out = UL = host -> target */
1573 0,
1574 },
1575 {
1576 ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS, /* not currently used */
1577 PIPEDIR_IN, /* in = DL = target -> host */
1578 1,
1579 },
1580 {
1581 ATH10K_HTC_SVC_ID_HTT_DATA_MSG,
1582 PIPEDIR_OUT, /* out = UL = host -> target */
1583 4,
1584 },
1585 {
1586 ATH10K_HTC_SVC_ID_HTT_DATA_MSG,
1587 PIPEDIR_IN, /* in = DL = target -> host */
1588 1,
1589 },
1590
1591 /* (Additions here) */
1592
1593 { /* Must be last */
1594 0,
1595 0,
1596 0,
1597 },
1598};
1599
1600/*
1601 * Send an interrupt to the device to wake up the Target CPU
1602 * so it has an opportunity to notice any changed state.
1603 */
1604static int ath10k_pci_wake_target_cpu(struct ath10k *ar)
1605{
1606 int ret;
1607 u32 core_ctrl;
1608
1609 ret = ath10k_pci_diag_read_access(ar, SOC_CORE_BASE_ADDRESS |
1610 CORE_CTRL_ADDRESS,
1611 &core_ctrl);
1612 if (ret) {
1613 ath10k_warn("Unable to read core ctrl\n");
1614 return ret;
1615 }
1616
1617 /* A_INUM_FIRMWARE interrupt to Target CPU */
1618 core_ctrl |= CORE_CTRL_CPU_INTR_MASK;
1619
1620 ret = ath10k_pci_diag_write_access(ar, SOC_CORE_BASE_ADDRESS |
1621 CORE_CTRL_ADDRESS,
1622 core_ctrl);
1623 if (ret)
1624 ath10k_warn("Unable to set interrupt mask\n");
1625
1626 return ret;
1627}
1628
1629static int ath10k_pci_init_config(struct ath10k *ar)
1630{
1631 u32 interconnect_targ_addr;
1632 u32 pcie_state_targ_addr = 0;
1633 u32 pipe_cfg_targ_addr = 0;
1634 u32 svc_to_pipe_map = 0;
1635 u32 pcie_config_flags = 0;
1636 u32 ealloc_value;
1637 u32 ealloc_targ_addr;
1638 u32 flag2_value;
1639 u32 flag2_targ_addr;
1640 int ret = 0;
1641
1642 /* Download to Target the CE Config and the service-to-CE map */
1643 interconnect_targ_addr =
1644 host_interest_item_address(HI_ITEM(hi_interconnect_state));
1645
1646 /* Supply Target-side CE configuration */
1647 ret = ath10k_pci_diag_read_access(ar, interconnect_targ_addr,
1648 &pcie_state_targ_addr);
1649 if (ret != 0) {
1650 ath10k_err("Failed to get pcie state addr: %d\n", ret);
1651 return ret;
1652 }
1653
1654 if (pcie_state_targ_addr == 0) {
1655 ret = -EIO;
1656 ath10k_err("Invalid pcie state addr\n");
1657 return ret;
1658 }
1659
1660 ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
1661 offsetof(struct pcie_state,
1662 pipe_cfg_addr),
1663 &pipe_cfg_targ_addr);
1664 if (ret != 0) {
1665 ath10k_err("Failed to get pipe cfg addr: %d\n", ret);
1666 return ret;
1667 }
1668
1669 if (pipe_cfg_targ_addr == 0) {
1670 ret = -EIO;
1671 ath10k_err("Invalid pipe cfg addr\n");
1672 return ret;
1673 }
1674
1675 ret = ath10k_pci_diag_write_mem(ar, pipe_cfg_targ_addr,
1676 target_ce_config_wlan,
1677 sizeof(target_ce_config_wlan));
1678
1679 if (ret != 0) {
1680 ath10k_err("Failed to write pipe cfg: %d\n", ret);
1681 return ret;
1682 }
1683
1684 ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
1685 offsetof(struct pcie_state,
1686 svc_to_pipe_map),
1687 &svc_to_pipe_map);
1688 if (ret != 0) {
1689 ath10k_err("Failed to get svc/pipe map: %d\n", ret);
1690 return ret;
1691 }
1692
1693 if (svc_to_pipe_map == 0) {
1694 ret = -EIO;
1695 ath10k_err("Invalid svc_to_pipe map\n");
1696 return ret;
1697 }
1698
1699 ret = ath10k_pci_diag_write_mem(ar, svc_to_pipe_map,
1700 target_service_to_ce_map_wlan,
1701 sizeof(target_service_to_ce_map_wlan));
1702 if (ret != 0) {
1703 ath10k_err("Failed to write svc/pipe map: %d\n", ret);
1704 return ret;
1705 }
1706
1707 ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
1708 offsetof(struct pcie_state,
1709 config_flags),
1710 &pcie_config_flags);
1711 if (ret != 0) {
1712 ath10k_err("Failed to get pcie config_flags: %d\n", ret);
1713 return ret;
1714 }
1715
1716 pcie_config_flags &= ~PCIE_CONFIG_FLAG_ENABLE_L1;
1717
1718 ret = ath10k_pci_diag_write_mem(ar, pcie_state_targ_addr +
1719 offsetof(struct pcie_state, config_flags),
1720 &pcie_config_flags,
1721 sizeof(pcie_config_flags));
1722 if (ret != 0) {
1723 ath10k_err("Failed to write pcie config_flags: %d\n", ret);
1724 return ret;
1725 }
1726
1727 /* configure early allocation */
1728 ealloc_targ_addr = host_interest_item_address(HI_ITEM(hi_early_alloc));
1729
1730 ret = ath10k_pci_diag_read_access(ar, ealloc_targ_addr, &ealloc_value);
1731 if (ret != 0) {
1732 ath10k_err("Faile to get early alloc val: %d\n", ret);
1733 return ret;
1734 }
1735
1736 /* first bank is switched to IRAM */
1737 ealloc_value |= ((HI_EARLY_ALLOC_MAGIC << HI_EARLY_ALLOC_MAGIC_SHIFT) &
1738 HI_EARLY_ALLOC_MAGIC_MASK);
1739 ealloc_value |= ((1 << HI_EARLY_ALLOC_IRAM_BANKS_SHIFT) &
1740 HI_EARLY_ALLOC_IRAM_BANKS_MASK);
1741
1742 ret = ath10k_pci_diag_write_access(ar, ealloc_targ_addr, ealloc_value);
1743 if (ret != 0) {
1744 ath10k_err("Failed to set early alloc val: %d\n", ret);
1745 return ret;
1746 }
1747
1748 /* Tell Target to proceed with initialization */
1749 flag2_targ_addr = host_interest_item_address(HI_ITEM(hi_option_flag2));
1750
1751 ret = ath10k_pci_diag_read_access(ar, flag2_targ_addr, &flag2_value);
1752 if (ret != 0) {
1753 ath10k_err("Failed to get option val: %d\n", ret);
1754 return ret;
1755 }
1756
1757 flag2_value |= HI_OPTION_EARLY_CFG_DONE;
1758
1759 ret = ath10k_pci_diag_write_access(ar, flag2_targ_addr, flag2_value);
1760 if (ret != 0) {
1761 ath10k_err("Failed to set option val: %d\n", ret);
1762 return ret;
1763 }
1764
1765 return 0;
1766}
1767
1768
1769
1770static int ath10k_pci_ce_init(struct ath10k *ar)
1771{
1772 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +02001773 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001774 const struct ce_attr *attr;
1775 int pipe_num;
1776
1777 for (pipe_num = 0; pipe_num < ar_pci->ce_count; pipe_num++) {
1778 pipe_info = &ar_pci->pipe_info[pipe_num];
1779 pipe_info->pipe_num = pipe_num;
1780 pipe_info->hif_ce_state = ar;
1781 attr = &host_ce_config_wlan[pipe_num];
1782
1783 pipe_info->ce_hdl = ath10k_ce_init(ar, pipe_num, attr);
1784 if (pipe_info->ce_hdl == NULL) {
1785 ath10k_err("Unable to initialize CE for pipe: %d\n",
1786 pipe_num);
1787
1788 /* It is safe to call it here. It checks if ce_hdl is
1789 * valid for each pipe */
1790 ath10k_pci_ce_deinit(ar);
1791 return -1;
1792 }
1793
1794 if (pipe_num == ar_pci->ce_count - 1) {
1795 /*
1796 * Reserve the ultimate CE for
1797 * diagnostic Window support
1798 */
1799 ar_pci->ce_diag =
1800 ar_pci->pipe_info[ar_pci->ce_count - 1].ce_hdl;
1801 continue;
1802 }
1803
1804 pipe_info->buf_sz = (size_t) (attr->src_sz_max);
1805 }
1806
1807 /*
1808 * Initially, establish CE completion handlers for use with BMI.
1809 * These are overwritten with generic handlers after we exit BMI phase.
1810 */
1811 pipe_info = &ar_pci->pipe_info[BMI_CE_NUM_TO_TARG];
1812 ath10k_ce_send_cb_register(pipe_info->ce_hdl,
1813 ath10k_pci_bmi_send_done, 0);
1814
1815 pipe_info = &ar_pci->pipe_info[BMI_CE_NUM_TO_HOST];
1816 ath10k_ce_recv_cb_register(pipe_info->ce_hdl,
1817 ath10k_pci_bmi_recv_data);
1818
1819 return 0;
1820}
1821
1822static void ath10k_pci_fw_interrupt_handler(struct ath10k *ar)
1823{
1824 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1825 u32 fw_indicator_address, fw_indicator;
1826
1827 ath10k_pci_wake(ar);
1828
1829 fw_indicator_address = ar_pci->fw_indicator_address;
1830 fw_indicator = ath10k_pci_read32(ar, fw_indicator_address);
1831
1832 if (fw_indicator & FW_IND_EVENT_PENDING) {
1833 /* ACK: clear Target-side pending event */
1834 ath10k_pci_write32(ar, fw_indicator_address,
1835 fw_indicator & ~FW_IND_EVENT_PENDING);
1836
1837 if (ar_pci->started) {
1838 ath10k_pci_hif_dump_area(ar);
1839 } else {
1840 /*
1841 * Probable Target failure before we're prepared
1842 * to handle it. Generally unexpected.
1843 */
1844 ath10k_warn("early firmware event indicated\n");
1845 }
1846 }
1847
1848 ath10k_pci_sleep(ar);
1849}
1850
Michal Kazior8c5c5362013-07-16 09:38:50 +02001851static int ath10k_pci_hif_power_up(struct ath10k *ar)
1852{
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02001853 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001854 int ret;
1855
Michal Kazior32270b62013-08-02 09:15:47 +02001856 ret = ath10k_pci_start_intr(ar);
1857 if (ret) {
1858 ath10k_err("could not start interrupt handling (%d)\n", ret);
1859 goto err;
1860 }
1861
Michal Kazior8c5c5362013-07-16 09:38:50 +02001862 /*
1863 * Bring the target up cleanly.
1864 *
1865 * The target may be in an undefined state with an AUX-powered Target
1866 * and a Host in WoW mode. If the Host crashes, loses power, or is
1867 * restarted (without unloading the driver) then the Target is left
1868 * (aux) powered and running. On a subsequent driver load, the Target
1869 * is in an unexpected state. We try to catch that here in order to
1870 * reset the Target and retry the probe.
1871 */
1872 ath10k_pci_device_reset(ar);
1873
1874 ret = ath10k_pci_reset_target(ar);
1875 if (ret)
Michal Kazior32270b62013-08-02 09:15:47 +02001876 goto err_irq;
Michal Kazior8c5c5362013-07-16 09:38:50 +02001877
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02001878 if (!test_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE, ar_pci->features))
Michal Kazior8c5c5362013-07-16 09:38:50 +02001879 /* Force AWAKE forever */
Michal Kazior8c5c5362013-07-16 09:38:50 +02001880 ath10k_do_pci_wake(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001881
1882 ret = ath10k_pci_ce_init(ar);
1883 if (ret)
1884 goto err_ps;
1885
1886 ret = ath10k_pci_init_config(ar);
1887 if (ret)
1888 goto err_ce;
1889
1890 ret = ath10k_pci_wake_target_cpu(ar);
1891 if (ret) {
1892 ath10k_err("could not wake up target CPU (%d)\n", ret);
1893 goto err_ce;
1894 }
1895
1896 return 0;
1897
1898err_ce:
1899 ath10k_pci_ce_deinit(ar);
1900err_ps:
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02001901 if (!test_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE, ar_pci->features))
Michal Kazior8c5c5362013-07-16 09:38:50 +02001902 ath10k_do_pci_sleep(ar);
Michal Kazior32270b62013-08-02 09:15:47 +02001903err_irq:
1904 ath10k_pci_stop_intr(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001905err:
1906 return ret;
1907}
1908
1909static void ath10k_pci_hif_power_down(struct ath10k *ar)
1910{
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02001911 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1912
Michal Kazior32270b62013-08-02 09:15:47 +02001913 ath10k_pci_stop_intr(ar);
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02001914
Michal Kazior8c5c5362013-07-16 09:38:50 +02001915 ath10k_pci_ce_deinit(ar);
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02001916 if (!test_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE, ar_pci->features))
Michal Kazior8c5c5362013-07-16 09:38:50 +02001917 ath10k_do_pci_sleep(ar);
1918}
1919
Michal Kazior8cd13ca2013-07-16 09:38:54 +02001920#ifdef CONFIG_PM
1921
1922#define ATH10K_PCI_PM_CONTROL 0x44
1923
1924static int ath10k_pci_hif_suspend(struct ath10k *ar)
1925{
1926 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1927 struct pci_dev *pdev = ar_pci->pdev;
1928 u32 val;
1929
1930 pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
1931
1932 if ((val & 0x000000ff) != 0x3) {
1933 pci_save_state(pdev);
1934 pci_disable_device(pdev);
1935 pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
1936 (val & 0xffffff00) | 0x03);
1937 }
1938
1939 return 0;
1940}
1941
1942static int ath10k_pci_hif_resume(struct ath10k *ar)
1943{
1944 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1945 struct pci_dev *pdev = ar_pci->pdev;
1946 u32 val;
1947
1948 pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
1949
1950 if ((val & 0x000000ff) != 0) {
1951 pci_restore_state(pdev);
1952 pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
1953 val & 0xffffff00);
1954 /*
1955 * Suspend/Resume resets the PCI configuration space,
1956 * so we have to re-disable the RETRY_TIMEOUT register (0x41)
1957 * to keep PCI Tx retries from interfering with C3 CPU state
1958 */
1959 pci_read_config_dword(pdev, 0x40, &val);
1960
1961 if ((val & 0x0000ff00) != 0)
1962 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
1963 }
1964
1965 return 0;
1966}
1967#endif
1968
Kalle Valo5e3dd152013-06-12 20:52:10 +03001969static const struct ath10k_hif_ops ath10k_pci_hif_ops = {
1970 .send_head = ath10k_pci_hif_send_head,
1971 .exchange_bmi_msg = ath10k_pci_hif_exchange_bmi_msg,
1972 .start = ath10k_pci_hif_start,
1973 .stop = ath10k_pci_hif_stop,
1974 .map_service_to_pipe = ath10k_pci_hif_map_service_to_pipe,
1975 .get_default_pipe = ath10k_pci_hif_get_default_pipe,
1976 .send_complete_check = ath10k_pci_hif_send_complete_check,
Michal Kaziore799bbf2013-07-05 16:15:12 +03001977 .set_callbacks = ath10k_pci_hif_set_callbacks,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001978 .get_free_queue_number = ath10k_pci_hif_get_free_queue_number,
Michal Kazior8c5c5362013-07-16 09:38:50 +02001979 .power_up = ath10k_pci_hif_power_up,
1980 .power_down = ath10k_pci_hif_power_down,
Michal Kazior8cd13ca2013-07-16 09:38:54 +02001981#ifdef CONFIG_PM
1982 .suspend = ath10k_pci_hif_suspend,
1983 .resume = ath10k_pci_hif_resume,
1984#endif
Kalle Valo5e3dd152013-06-12 20:52:10 +03001985};
1986
1987static void ath10k_pci_ce_tasklet(unsigned long ptr)
1988{
Michal Kazior87263e52013-08-27 13:08:01 +02001989 struct ath10k_pci_pipe *pipe = (struct ath10k_pci_pipe *)ptr;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001990 struct ath10k_pci *ar_pci = pipe->ar_pci;
1991
1992 ath10k_ce_per_engine_service(ar_pci->ar, pipe->pipe_num);
1993}
1994
1995static void ath10k_msi_err_tasklet(unsigned long data)
1996{
1997 struct ath10k *ar = (struct ath10k *)data;
1998
1999 ath10k_pci_fw_interrupt_handler(ar);
2000}
2001
2002/*
2003 * Handler for a per-engine interrupt on a PARTICULAR CE.
2004 * This is used in cases where each CE has a private MSI interrupt.
2005 */
2006static irqreturn_t ath10k_pci_per_engine_handler(int irq, void *arg)
2007{
2008 struct ath10k *ar = arg;
2009 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2010 int ce_id = irq - ar_pci->pdev->irq - MSI_ASSIGN_CE_INITIAL;
2011
Dan Carpentere5742672013-06-18 10:28:46 +03002012 if (ce_id < 0 || ce_id >= ARRAY_SIZE(ar_pci->pipe_info)) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03002013 ath10k_warn("unexpected/invalid irq %d ce_id %d\n", irq, ce_id);
2014 return IRQ_HANDLED;
2015 }
2016
2017 /*
2018 * NOTE: We are able to derive ce_id from irq because we
2019 * use a one-to-one mapping for CE's 0..5.
2020 * CE's 6 & 7 do not use interrupts at all.
2021 *
2022 * This mapping must be kept in sync with the mapping
2023 * used by firmware.
2024 */
2025 tasklet_schedule(&ar_pci->pipe_info[ce_id].intr);
2026 return IRQ_HANDLED;
2027}
2028
2029static irqreturn_t ath10k_pci_msi_fw_handler(int irq, void *arg)
2030{
2031 struct ath10k *ar = arg;
2032 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2033
2034 tasklet_schedule(&ar_pci->msi_fw_err);
2035 return IRQ_HANDLED;
2036}
2037
2038/*
2039 * Top-level interrupt handler for all PCI interrupts from a Target.
2040 * When a block of MSI interrupts is allocated, this top-level handler
2041 * is not used; instead, we directly call the correct sub-handler.
2042 */
2043static irqreturn_t ath10k_pci_interrupt_handler(int irq, void *arg)
2044{
2045 struct ath10k *ar = arg;
2046 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2047
2048 if (ar_pci->num_msi_intrs == 0) {
2049 /*
2050 * IMPORTANT: INTR_CLR regiser has to be set after
2051 * INTR_ENABLE is set to 0, otherwise interrupt can not be
2052 * really cleared.
2053 */
2054 iowrite32(0, ar_pci->mem +
2055 (SOC_CORE_BASE_ADDRESS |
2056 PCIE_INTR_ENABLE_ADDRESS));
2057 iowrite32(PCIE_INTR_FIRMWARE_MASK |
2058 PCIE_INTR_CE_MASK_ALL,
2059 ar_pci->mem + (SOC_CORE_BASE_ADDRESS |
2060 PCIE_INTR_CLR_ADDRESS));
2061 /*
2062 * IMPORTANT: this extra read transaction is required to
2063 * flush the posted write buffer.
2064 */
2065 (void) ioread32(ar_pci->mem +
2066 (SOC_CORE_BASE_ADDRESS |
2067 PCIE_INTR_ENABLE_ADDRESS));
2068 }
2069
2070 tasklet_schedule(&ar_pci->intr_tq);
2071
2072 return IRQ_HANDLED;
2073}
2074
2075static void ath10k_pci_tasklet(unsigned long data)
2076{
2077 struct ath10k *ar = (struct ath10k *)data;
2078 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2079
2080 ath10k_pci_fw_interrupt_handler(ar); /* FIXME: Handle FW error */
2081 ath10k_ce_per_engine_service_any(ar);
2082
2083 if (ar_pci->num_msi_intrs == 0) {
2084 /* Enable Legacy PCI line interrupts */
2085 iowrite32(PCIE_INTR_FIRMWARE_MASK |
2086 PCIE_INTR_CE_MASK_ALL,
2087 ar_pci->mem + (SOC_CORE_BASE_ADDRESS |
2088 PCIE_INTR_ENABLE_ADDRESS));
2089 /*
2090 * IMPORTANT: this extra read transaction is required to
2091 * flush the posted write buffer
2092 */
2093 (void) ioread32(ar_pci->mem +
2094 (SOC_CORE_BASE_ADDRESS |
2095 PCIE_INTR_ENABLE_ADDRESS));
2096 }
2097}
2098
2099static int ath10k_pci_start_intr_msix(struct ath10k *ar, int num)
2100{
2101 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2102 int ret;
2103 int i;
2104
2105 ret = pci_enable_msi_block(ar_pci->pdev, num);
2106 if (ret)
2107 return ret;
2108
2109 ret = request_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW,
2110 ath10k_pci_msi_fw_handler,
2111 IRQF_SHARED, "ath10k_pci", ar);
Michal Kazior591ecdb2013-07-31 10:55:15 +02002112 if (ret) {
2113 ath10k_warn("request_irq(%d) failed %d\n",
2114 ar_pci->pdev->irq + MSI_ASSIGN_FW, ret);
2115
2116 pci_disable_msi(ar_pci->pdev);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002117 return ret;
Michal Kazior591ecdb2013-07-31 10:55:15 +02002118 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002119
2120 for (i = MSI_ASSIGN_CE_INITIAL; i <= MSI_ASSIGN_CE_MAX; i++) {
2121 ret = request_irq(ar_pci->pdev->irq + i,
2122 ath10k_pci_per_engine_handler,
2123 IRQF_SHARED, "ath10k_pci", ar);
2124 if (ret) {
2125 ath10k_warn("request_irq(%d) failed %d\n",
2126 ar_pci->pdev->irq + i, ret);
2127
Michal Kazior87b14232013-06-26 08:50:50 +02002128 for (i--; i >= MSI_ASSIGN_CE_INITIAL; i--)
2129 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002130
Michal Kazior87b14232013-06-26 08:50:50 +02002131 free_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002132 pci_disable_msi(ar_pci->pdev);
2133 return ret;
2134 }
2135 }
2136
2137 ath10k_info("MSI-X interrupt handling (%d intrs)\n", num);
2138 return 0;
2139}
2140
2141static int ath10k_pci_start_intr_msi(struct ath10k *ar)
2142{
2143 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2144 int ret;
2145
2146 ret = pci_enable_msi(ar_pci->pdev);
2147 if (ret < 0)
2148 return ret;
2149
2150 ret = request_irq(ar_pci->pdev->irq,
2151 ath10k_pci_interrupt_handler,
2152 IRQF_SHARED, "ath10k_pci", ar);
2153 if (ret < 0) {
2154 pci_disable_msi(ar_pci->pdev);
2155 return ret;
2156 }
2157
2158 ath10k_info("MSI interrupt handling\n");
2159 return 0;
2160}
2161
2162static int ath10k_pci_start_intr_legacy(struct ath10k *ar)
2163{
2164 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2165 int ret;
2166
2167 ret = request_irq(ar_pci->pdev->irq,
2168 ath10k_pci_interrupt_handler,
2169 IRQF_SHARED, "ath10k_pci", ar);
2170 if (ret < 0)
2171 return ret;
2172
2173 /*
2174 * Make sure to wake the Target before enabling Legacy
2175 * Interrupt.
2176 */
2177 iowrite32(PCIE_SOC_WAKE_V_MASK,
2178 ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
2179 PCIE_SOC_WAKE_ADDRESS);
2180
2181 ath10k_pci_wait(ar);
2182
2183 /*
2184 * A potential race occurs here: The CORE_BASE write
2185 * depends on target correctly decoding AXI address but
2186 * host won't know when target writes BAR to CORE_CTRL.
2187 * This write might get lost if target has NOT written BAR.
2188 * For now, fix the race by repeating the write in below
2189 * synchronization checking.
2190 */
2191 iowrite32(PCIE_INTR_FIRMWARE_MASK |
2192 PCIE_INTR_CE_MASK_ALL,
2193 ar_pci->mem + (SOC_CORE_BASE_ADDRESS |
2194 PCIE_INTR_ENABLE_ADDRESS));
2195 iowrite32(PCIE_SOC_WAKE_RESET,
2196 ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
2197 PCIE_SOC_WAKE_ADDRESS);
2198
2199 ath10k_info("legacy interrupt handling\n");
2200 return 0;
2201}
2202
2203static int ath10k_pci_start_intr(struct ath10k *ar)
2204{
2205 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2206 int num = MSI_NUM_REQUEST;
2207 int ret;
2208 int i;
2209
2210 tasklet_init(&ar_pci->intr_tq, ath10k_pci_tasklet, (unsigned long) ar);
2211 tasklet_init(&ar_pci->msi_fw_err, ath10k_msi_err_tasklet,
2212 (unsigned long) ar);
2213
2214 for (i = 0; i < CE_COUNT; i++) {
2215 ar_pci->pipe_info[i].ar_pci = ar_pci;
2216 tasklet_init(&ar_pci->pipe_info[i].intr,
2217 ath10k_pci_ce_tasklet,
2218 (unsigned long)&ar_pci->pipe_info[i]);
2219 }
2220
2221 if (!test_bit(ATH10K_PCI_FEATURE_MSI_X, ar_pci->features))
2222 num = 1;
2223
2224 if (num > 1) {
2225 ret = ath10k_pci_start_intr_msix(ar, num);
2226 if (ret == 0)
2227 goto exit;
2228
2229 ath10k_warn("MSI-X didn't succeed (%d), trying MSI\n", ret);
2230 num = 1;
2231 }
2232
2233 if (num == 1) {
2234 ret = ath10k_pci_start_intr_msi(ar);
2235 if (ret == 0)
2236 goto exit;
2237
2238 ath10k_warn("MSI didn't succeed (%d), trying legacy INTR\n",
2239 ret);
2240 num = 0;
2241 }
2242
2243 ret = ath10k_pci_start_intr_legacy(ar);
2244
2245exit:
2246 ar_pci->num_msi_intrs = num;
2247 ar_pci->ce_count = CE_COUNT;
2248 return ret;
2249}
2250
2251static void ath10k_pci_stop_intr(struct ath10k *ar)
2252{
2253 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2254 int i;
2255
2256 /* There's at least one interrupt irregardless whether its legacy INTR
2257 * or MSI or MSI-X */
2258 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
2259 free_irq(ar_pci->pdev->irq + i, ar);
2260
2261 if (ar_pci->num_msi_intrs > 0)
2262 pci_disable_msi(ar_pci->pdev);
2263}
2264
2265static int ath10k_pci_reset_target(struct ath10k *ar)
2266{
2267 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2268 int wait_limit = 300; /* 3 sec */
2269
2270 /* Wait for Target to finish initialization before we proceed. */
2271 iowrite32(PCIE_SOC_WAKE_V_MASK,
2272 ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
2273 PCIE_SOC_WAKE_ADDRESS);
2274
2275 ath10k_pci_wait(ar);
2276
2277 while (wait_limit-- &&
2278 !(ioread32(ar_pci->mem + FW_INDICATOR_ADDRESS) &
2279 FW_IND_INITIALIZED)) {
2280 if (ar_pci->num_msi_intrs == 0)
2281 /* Fix potential race by repeating CORE_BASE writes */
2282 iowrite32(PCIE_INTR_FIRMWARE_MASK |
2283 PCIE_INTR_CE_MASK_ALL,
2284 ar_pci->mem + (SOC_CORE_BASE_ADDRESS |
2285 PCIE_INTR_ENABLE_ADDRESS));
2286 mdelay(10);
2287 }
2288
2289 if (wait_limit < 0) {
2290 ath10k_err("Target stalled\n");
2291 iowrite32(PCIE_SOC_WAKE_RESET,
2292 ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
2293 PCIE_SOC_WAKE_ADDRESS);
2294 return -EIO;
2295 }
2296
2297 iowrite32(PCIE_SOC_WAKE_RESET,
2298 ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
2299 PCIE_SOC_WAKE_ADDRESS);
2300
2301 return 0;
2302}
2303
Michal Kazior7a5fe3f2013-07-05 16:15:11 +03002304static void ath10k_pci_device_reset(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002305{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002306 int i;
2307 u32 val;
2308
2309 if (!SOC_GLOBAL_RESET_ADDRESS)
2310 return;
2311
Kalle Valoe479ed42013-09-01 10:01:53 +03002312 ath10k_pci_reg_write32(ar, PCIE_SOC_WAKE_ADDRESS,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002313 PCIE_SOC_WAKE_V_MASK);
2314 for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
2315 if (ath10k_pci_target_is_awake(ar))
2316 break;
2317 msleep(1);
2318 }
2319
2320 /* Put Target, including PCIe, into RESET. */
Kalle Valoe479ed42013-09-01 10:01:53 +03002321 val = ath10k_pci_reg_read32(ar, SOC_GLOBAL_RESET_ADDRESS);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002322 val |= 1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002323 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002324
2325 for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
Kalle Valoe479ed42013-09-01 10:01:53 +03002326 if (ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03002327 RTC_STATE_COLD_RESET_MASK)
2328 break;
2329 msleep(1);
2330 }
2331
2332 /* Pull Target, including PCIe, out of RESET. */
2333 val &= ~1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002334 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002335
2336 for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
Kalle Valoe479ed42013-09-01 10:01:53 +03002337 if (!(ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03002338 RTC_STATE_COLD_RESET_MASK))
2339 break;
2340 msleep(1);
2341 }
2342
Kalle Valoe479ed42013-09-01 10:01:53 +03002343 ath10k_pci_reg_write32(ar, PCIE_SOC_WAKE_ADDRESS, PCIE_SOC_WAKE_RESET);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002344}
2345
2346static void ath10k_pci_dump_features(struct ath10k_pci *ar_pci)
2347{
2348 int i;
2349
2350 for (i = 0; i < ATH10K_PCI_FEATURE_COUNT; i++) {
2351 if (!test_bit(i, ar_pci->features))
2352 continue;
2353
2354 switch (i) {
2355 case ATH10K_PCI_FEATURE_MSI_X:
Kalle Valo24cfade2013-09-08 17:55:50 +03002356 ath10k_dbg(ATH10K_DBG_BOOT, "device supports MSI-X\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002357 break;
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002358 case ATH10K_PCI_FEATURE_SOC_POWER_SAVE:
Kalle Valo24cfade2013-09-08 17:55:50 +03002359 ath10k_dbg(ATH10K_DBG_BOOT, "QCA98XX SoC power save enabled\n");
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002360 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002361 }
2362 }
2363}
2364
2365static int ath10k_pci_probe(struct pci_dev *pdev,
2366 const struct pci_device_id *pci_dev)
2367{
2368 void __iomem *mem;
2369 int ret = 0;
2370 struct ath10k *ar;
2371 struct ath10k_pci *ar_pci;
Kalle Valoe01ae682013-09-01 11:22:14 +03002372 u32 lcr_val, chip_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002373
2374 ath10k_dbg(ATH10K_DBG_PCI, "%s\n", __func__);
2375
2376 ar_pci = kzalloc(sizeof(*ar_pci), GFP_KERNEL);
2377 if (ar_pci == NULL)
2378 return -ENOMEM;
2379
2380 ar_pci->pdev = pdev;
2381 ar_pci->dev = &pdev->dev;
2382
2383 switch (pci_dev->device) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03002384 case QCA988X_2_0_DEVICE_ID:
2385 set_bit(ATH10K_PCI_FEATURE_MSI_X, ar_pci->features);
2386 break;
2387 default:
2388 ret = -ENODEV;
2389 ath10k_err("Unkown device ID: %d\n", pci_dev->device);
2390 goto err_ar_pci;
2391 }
2392
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002393 if (ath10k_target_ps)
2394 set_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE, ar_pci->features);
2395
Kalle Valo5e3dd152013-06-12 20:52:10 +03002396 ath10k_pci_dump_features(ar_pci);
2397
Michal Kazior3a0861f2013-07-05 16:15:06 +03002398 ar = ath10k_core_create(ar_pci, ar_pci->dev, &ath10k_pci_hif_ops);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002399 if (!ar) {
2400 ath10k_err("ath10k_core_create failed!\n");
2401 ret = -EINVAL;
2402 goto err_ar_pci;
2403 }
2404
Kalle Valo5e3dd152013-06-12 20:52:10 +03002405 ar_pci->ar = ar;
2406 ar_pci->fw_indicator_address = FW_INDICATOR_ADDRESS;
2407 atomic_set(&ar_pci->keep_awake_count, 0);
2408
2409 pci_set_drvdata(pdev, ar);
2410
2411 /*
2412 * Without any knowledge of the Host, the Target may have been reset or
2413 * power cycled and its Config Space may no longer reflect the PCI
2414 * address space that was assigned earlier by the PCI infrastructure.
2415 * Refresh it now.
2416 */
2417 ret = pci_assign_resource(pdev, BAR_NUM);
2418 if (ret) {
2419 ath10k_err("cannot assign PCI space: %d\n", ret);
2420 goto err_ar;
2421 }
2422
2423 ret = pci_enable_device(pdev);
2424 if (ret) {
2425 ath10k_err("cannot enable PCI device: %d\n", ret);
2426 goto err_ar;
2427 }
2428
2429 /* Request MMIO resources */
2430 ret = pci_request_region(pdev, BAR_NUM, "ath");
2431 if (ret) {
2432 ath10k_err("PCI MMIO reservation error: %d\n", ret);
2433 goto err_device;
2434 }
2435
2436 /*
2437 * Target structures have a limit of 32 bit DMA pointers.
2438 * DMA pointers can be wider than 32 bits by default on some systems.
2439 */
2440 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2441 if (ret) {
2442 ath10k_err("32-bit DMA not available: %d\n", ret);
2443 goto err_region;
2444 }
2445
2446 ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2447 if (ret) {
2448 ath10k_err("cannot enable 32-bit consistent DMA\n");
2449 goto err_region;
2450 }
2451
2452 /* Set bus master bit in PCI_COMMAND to enable DMA */
2453 pci_set_master(pdev);
2454
2455 /*
2456 * Temporary FIX: disable ASPM
2457 * Will be removed after the OTP is programmed
2458 */
2459 pci_read_config_dword(pdev, 0x80, &lcr_val);
2460 pci_write_config_dword(pdev, 0x80, (lcr_val & 0xffffff00));
2461
2462 /* Arrange for access to Target SoC registers. */
2463 mem = pci_iomap(pdev, BAR_NUM, 0);
2464 if (!mem) {
2465 ath10k_err("PCI iomap error\n");
2466 ret = -EIO;
2467 goto err_master;
2468 }
2469
2470 ar_pci->mem = mem;
2471
2472 spin_lock_init(&ar_pci->ce_lock);
2473
Kalle Valoe01ae682013-09-01 11:22:14 +03002474 ret = ath10k_do_pci_wake(ar);
2475 if (ret) {
2476 ath10k_err("Failed to get chip id: %d\n", ret);
2477 return ret;
2478 }
2479
2480 chip_id = ath10k_pci_read32(ar,
2481 RTC_SOC_BASE_ADDRESS + SOC_CHIP_ID_ADDRESS);
2482
2483 ath10k_do_pci_sleep(ar);
2484
Kalle Valo24cfade2013-09-08 17:55:50 +03002485 ath10k_dbg(ATH10K_DBG_BOOT, "boot pci_mem 0x%p\n", ar_pci->mem);
2486
Kalle Valoe01ae682013-09-01 11:22:14 +03002487 ret = ath10k_core_register(ar, chip_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002488 if (ret) {
2489 ath10k_err("could not register driver core (%d)\n", ret);
Michal Kazior32270b62013-08-02 09:15:47 +02002490 goto err_iomap;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002491 }
2492
2493 return 0;
2494
Kalle Valo5e3dd152013-06-12 20:52:10 +03002495err_iomap:
2496 pci_iounmap(pdev, mem);
2497err_master:
2498 pci_clear_master(pdev);
2499err_region:
2500 pci_release_region(pdev, BAR_NUM);
2501err_device:
2502 pci_disable_device(pdev);
2503err_ar:
2504 pci_set_drvdata(pdev, NULL);
2505 ath10k_core_destroy(ar);
2506err_ar_pci:
2507 /* call HIF PCI free here */
2508 kfree(ar_pci);
2509
2510 return ret;
2511}
2512
2513static void ath10k_pci_remove(struct pci_dev *pdev)
2514{
2515 struct ath10k *ar = pci_get_drvdata(pdev);
2516 struct ath10k_pci *ar_pci;
2517
2518 ath10k_dbg(ATH10K_DBG_PCI, "%s\n", __func__);
2519
2520 if (!ar)
2521 return;
2522
2523 ar_pci = ath10k_pci_priv(ar);
2524
2525 if (!ar_pci)
2526 return;
2527
2528 tasklet_kill(&ar_pci->msi_fw_err);
2529
2530 ath10k_core_unregister(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002531
2532 pci_set_drvdata(pdev, NULL);
2533 pci_iounmap(pdev, ar_pci->mem);
2534 pci_release_region(pdev, BAR_NUM);
2535 pci_clear_master(pdev);
2536 pci_disable_device(pdev);
2537
2538 ath10k_core_destroy(ar);
2539 kfree(ar_pci);
2540}
2541
Kalle Valo5e3dd152013-06-12 20:52:10 +03002542MODULE_DEVICE_TABLE(pci, ath10k_pci_id_table);
2543
2544static struct pci_driver ath10k_pci_driver = {
2545 .name = "ath10k_pci",
2546 .id_table = ath10k_pci_id_table,
2547 .probe = ath10k_pci_probe,
2548 .remove = ath10k_pci_remove,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002549};
2550
2551static int __init ath10k_pci_init(void)
2552{
2553 int ret;
2554
2555 ret = pci_register_driver(&ath10k_pci_driver);
2556 if (ret)
2557 ath10k_err("pci_register_driver failed [%d]\n", ret);
2558
2559 return ret;
2560}
2561module_init(ath10k_pci_init);
2562
2563static void __exit ath10k_pci_exit(void)
2564{
2565 pci_unregister_driver(&ath10k_pci_driver);
2566}
2567
2568module_exit(ath10k_pci_exit);
2569
2570MODULE_AUTHOR("Qualcomm Atheros");
2571MODULE_DESCRIPTION("Driver support for Atheros QCA988X PCIe devices");
2572MODULE_LICENSE("Dual BSD/GPL");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002573MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_FW_FILE);
2574MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_OTP_FILE);
2575MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_BOARD_DATA_FILE);