blob: 0b897269f129f05f02ba1d195a5605298cf19f3b [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 Valoaa5c1db42013-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 Valoaa5c1db42013-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 Valoaa5c1db42013-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 Valoaa5c1db42013-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 Valoaa5c1db42013-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 Valoaa5c1db42013-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 */
Michal Kazior2415fc12013-11-08 08:01:32 +01001273
1274 if (!netbuf) {
1275 ath10k_warn("invalid sk_buff on CE %d - NULL pointer. firmware crashed?\n",
1276 ce_hdl->id);
1277 continue;
1278 }
1279
Kalle Valoe9bb0aa2013-09-08 18:36:11 +03001280 ATH10K_SKB_CB(netbuf)->is_aborted = true;
1281 ar_pci->msg_callbacks_current.tx_completion(ar,
1282 netbuf,
1283 id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001284 }
1285}
1286
1287/*
1288 * Cleanup residual buffers for device shutdown:
1289 * buffers that were enqueued for receive
1290 * buffers that were to be sent
1291 * Note: Buffers that had completed but which were
1292 * not yet processed are on a completion queue. They
1293 * are handled when the completion thread shuts down.
1294 */
1295static void ath10k_pci_buffer_cleanup(struct ath10k *ar)
1296{
1297 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1298 int pipe_num;
1299
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001300 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Michal Kazior87263e52013-08-27 13:08:01 +02001301 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001302
1303 pipe_info = &ar_pci->pipe_info[pipe_num];
1304 ath10k_pci_rx_pipe_cleanup(pipe_info);
1305 ath10k_pci_tx_pipe_cleanup(pipe_info);
1306 }
1307}
1308
1309static void ath10k_pci_ce_deinit(struct ath10k *ar)
1310{
1311 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +02001312 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001313 int pipe_num;
1314
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001315 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001316 pipe_info = &ar_pci->pipe_info[pipe_num];
1317 if (pipe_info->ce_hdl) {
1318 ath10k_ce_deinit(pipe_info->ce_hdl);
1319 pipe_info->ce_hdl = NULL;
1320 pipe_info->buf_sz = 0;
1321 }
1322 }
1323}
1324
Michal Kazior32270b62013-08-02 09:15:47 +02001325static void ath10k_pci_disable_irqs(struct ath10k *ar)
1326{
1327 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1328 int i;
1329
1330 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
1331 disable_irq(ar_pci->pdev->irq + i);
1332}
1333
Kalle Valo5e3dd152013-06-12 20:52:10 +03001334static void ath10k_pci_hif_stop(struct ath10k *ar)
1335{
Michal Kazior32270b62013-08-02 09:15:47 +02001336 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1337
Kalle Valo5e3dd152013-06-12 20:52:10 +03001338 ath10k_dbg(ATH10K_DBG_PCI, "%s\n", __func__);
1339
Michal Kazior32270b62013-08-02 09:15:47 +02001340 /* Irqs are never explicitly re-enabled. They are implicitly re-enabled
1341 * by ath10k_pci_start_intr(). */
1342 ath10k_pci_disable_irqs(ar);
1343
Kalle Valo5e3dd152013-06-12 20:52:10 +03001344 ath10k_pci_stop_ce(ar);
1345
1346 /* At this point, asynchronous threads are stopped, the target should
1347 * not DMA nor interrupt. We process the leftovers and then free
1348 * everything else up. */
1349
1350 ath10k_pci_process_ce(ar);
1351 ath10k_pci_cleanup_ce(ar);
1352 ath10k_pci_buffer_cleanup(ar);
Michal Kazior32270b62013-08-02 09:15:47 +02001353
1354 ar_pci->started = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001355}
1356
1357static int ath10k_pci_hif_exchange_bmi_msg(struct ath10k *ar,
1358 void *req, u32 req_len,
1359 void *resp, u32 *resp_len)
1360{
1361 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2aa39112013-08-27 13:08:02 +02001362 struct ath10k_pci_pipe *pci_tx = &ar_pci->pipe_info[BMI_CE_NUM_TO_TARG];
1363 struct ath10k_pci_pipe *pci_rx = &ar_pci->pipe_info[BMI_CE_NUM_TO_HOST];
1364 struct ath10k_ce_pipe *ce_tx = pci_tx->ce_hdl;
1365 struct ath10k_ce_pipe *ce_rx = pci_rx->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001366 dma_addr_t req_paddr = 0;
1367 dma_addr_t resp_paddr = 0;
1368 struct bmi_xfer xfer = {};
1369 void *treq, *tresp = NULL;
1370 int ret = 0;
1371
1372 if (resp && !resp_len)
1373 return -EINVAL;
1374
1375 if (resp && resp_len && *resp_len == 0)
1376 return -EINVAL;
1377
1378 treq = kmemdup(req, req_len, GFP_KERNEL);
1379 if (!treq)
1380 return -ENOMEM;
1381
1382 req_paddr = dma_map_single(ar->dev, treq, req_len, DMA_TO_DEVICE);
1383 ret = dma_mapping_error(ar->dev, req_paddr);
1384 if (ret)
1385 goto err_dma;
1386
1387 if (resp && resp_len) {
1388 tresp = kzalloc(*resp_len, GFP_KERNEL);
1389 if (!tresp) {
1390 ret = -ENOMEM;
1391 goto err_req;
1392 }
1393
1394 resp_paddr = dma_map_single(ar->dev, tresp, *resp_len,
1395 DMA_FROM_DEVICE);
1396 ret = dma_mapping_error(ar->dev, resp_paddr);
1397 if (ret)
1398 goto err_req;
1399
1400 xfer.wait_for_resp = true;
1401 xfer.resp_len = 0;
1402
1403 ath10k_ce_recv_buf_enqueue(ce_rx, &xfer, resp_paddr);
1404 }
1405
1406 init_completion(&xfer.done);
1407
1408 ret = ath10k_ce_send(ce_tx, &xfer, req_paddr, req_len, -1, 0);
1409 if (ret)
1410 goto err_resp;
1411
1412 ret = wait_for_completion_timeout(&xfer.done,
1413 BMI_COMMUNICATION_TIMEOUT_HZ);
1414 if (ret <= 0) {
1415 u32 unused_buffer;
1416 unsigned int unused_nbytes;
1417 unsigned int unused_id;
1418
1419 ret = -ETIMEDOUT;
1420 ath10k_ce_cancel_send_next(ce_tx, NULL, &unused_buffer,
1421 &unused_nbytes, &unused_id);
1422 } else {
1423 /* non-zero means we did not time out */
1424 ret = 0;
1425 }
1426
1427err_resp:
1428 if (resp) {
1429 u32 unused_buffer;
1430
1431 ath10k_ce_revoke_recv_next(ce_rx, NULL, &unused_buffer);
1432 dma_unmap_single(ar->dev, resp_paddr,
1433 *resp_len, DMA_FROM_DEVICE);
1434 }
1435err_req:
1436 dma_unmap_single(ar->dev, req_paddr, req_len, DMA_TO_DEVICE);
1437
1438 if (ret == 0 && resp_len) {
1439 *resp_len = min(*resp_len, xfer.resp_len);
1440 memcpy(resp, tresp, xfer.resp_len);
1441 }
1442err_dma:
1443 kfree(treq);
1444 kfree(tresp);
1445
1446 return ret;
1447}
1448
Michal Kazior5440ce22013-09-03 15:09:58 +02001449static void ath10k_pci_bmi_send_done(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001450{
Michal Kazior5440ce22013-09-03 15:09:58 +02001451 struct bmi_xfer *xfer;
1452 u32 ce_data;
1453 unsigned int nbytes;
1454 unsigned int transfer_id;
1455
1456 if (ath10k_ce_completed_send_next(ce_state, (void **)&xfer, &ce_data,
1457 &nbytes, &transfer_id))
1458 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001459
1460 if (xfer->wait_for_resp)
1461 return;
1462
1463 complete(&xfer->done);
1464}
1465
Michal Kazior5440ce22013-09-03 15:09:58 +02001466static void ath10k_pci_bmi_recv_data(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 unsigned int flags;
1473
1474 if (ath10k_ce_completed_recv_next(ce_state, (void **)&xfer, &ce_data,
1475 &nbytes, &transfer_id, &flags))
1476 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001477
1478 if (!xfer->wait_for_resp) {
1479 ath10k_warn("unexpected: BMI data received; ignoring\n");
1480 return;
1481 }
1482
1483 xfer->resp_len = nbytes;
1484 complete(&xfer->done);
1485}
1486
1487/*
1488 * Map from service/endpoint to Copy Engine.
1489 * This table is derived from the CE_PCI TABLE, above.
1490 * It is passed to the Target at startup for use by firmware.
1491 */
1492static const struct service_to_pipe target_service_to_ce_map_wlan[] = {
1493 {
1494 ATH10K_HTC_SVC_ID_WMI_DATA_VO,
1495 PIPEDIR_OUT, /* out = UL = host -> target */
1496 3,
1497 },
1498 {
1499 ATH10K_HTC_SVC_ID_WMI_DATA_VO,
1500 PIPEDIR_IN, /* in = DL = target -> host */
1501 2,
1502 },
1503 {
1504 ATH10K_HTC_SVC_ID_WMI_DATA_BK,
1505 PIPEDIR_OUT, /* out = UL = host -> target */
1506 3,
1507 },
1508 {
1509 ATH10K_HTC_SVC_ID_WMI_DATA_BK,
1510 PIPEDIR_IN, /* in = DL = target -> host */
1511 2,
1512 },
1513 {
1514 ATH10K_HTC_SVC_ID_WMI_DATA_BE,
1515 PIPEDIR_OUT, /* out = UL = host -> target */
1516 3,
1517 },
1518 {
1519 ATH10K_HTC_SVC_ID_WMI_DATA_BE,
1520 PIPEDIR_IN, /* in = DL = target -> host */
1521 2,
1522 },
1523 {
1524 ATH10K_HTC_SVC_ID_WMI_DATA_VI,
1525 PIPEDIR_OUT, /* out = UL = host -> target */
1526 3,
1527 },
1528 {
1529 ATH10K_HTC_SVC_ID_WMI_DATA_VI,
1530 PIPEDIR_IN, /* in = DL = target -> host */
1531 2,
1532 },
1533 {
1534 ATH10K_HTC_SVC_ID_WMI_CONTROL,
1535 PIPEDIR_OUT, /* out = UL = host -> target */
1536 3,
1537 },
1538 {
1539 ATH10K_HTC_SVC_ID_WMI_CONTROL,
1540 PIPEDIR_IN, /* in = DL = target -> host */
1541 2,
1542 },
1543 {
1544 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1545 PIPEDIR_OUT, /* out = UL = host -> target */
1546 0, /* could be moved to 3 (share with WMI) */
1547 },
1548 {
1549 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1550 PIPEDIR_IN, /* in = DL = target -> host */
1551 1,
1552 },
1553 {
1554 ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS, /* not currently used */
1555 PIPEDIR_OUT, /* out = UL = host -> target */
1556 0,
1557 },
1558 {
1559 ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS, /* not currently used */
1560 PIPEDIR_IN, /* in = DL = target -> host */
1561 1,
1562 },
1563 {
1564 ATH10K_HTC_SVC_ID_HTT_DATA_MSG,
1565 PIPEDIR_OUT, /* out = UL = host -> target */
1566 4,
1567 },
1568 {
1569 ATH10K_HTC_SVC_ID_HTT_DATA_MSG,
1570 PIPEDIR_IN, /* in = DL = target -> host */
1571 1,
1572 },
1573
1574 /* (Additions here) */
1575
1576 { /* Must be last */
1577 0,
1578 0,
1579 0,
1580 },
1581};
1582
1583/*
1584 * Send an interrupt to the device to wake up the Target CPU
1585 * so it has an opportunity to notice any changed state.
1586 */
1587static int ath10k_pci_wake_target_cpu(struct ath10k *ar)
1588{
1589 int ret;
1590 u32 core_ctrl;
1591
1592 ret = ath10k_pci_diag_read_access(ar, SOC_CORE_BASE_ADDRESS |
1593 CORE_CTRL_ADDRESS,
1594 &core_ctrl);
1595 if (ret) {
1596 ath10k_warn("Unable to read core ctrl\n");
1597 return ret;
1598 }
1599
1600 /* A_INUM_FIRMWARE interrupt to Target CPU */
1601 core_ctrl |= CORE_CTRL_CPU_INTR_MASK;
1602
1603 ret = ath10k_pci_diag_write_access(ar, SOC_CORE_BASE_ADDRESS |
1604 CORE_CTRL_ADDRESS,
1605 core_ctrl);
1606 if (ret)
1607 ath10k_warn("Unable to set interrupt mask\n");
1608
1609 return ret;
1610}
1611
1612static int ath10k_pci_init_config(struct ath10k *ar)
1613{
1614 u32 interconnect_targ_addr;
1615 u32 pcie_state_targ_addr = 0;
1616 u32 pipe_cfg_targ_addr = 0;
1617 u32 svc_to_pipe_map = 0;
1618 u32 pcie_config_flags = 0;
1619 u32 ealloc_value;
1620 u32 ealloc_targ_addr;
1621 u32 flag2_value;
1622 u32 flag2_targ_addr;
1623 int ret = 0;
1624
1625 /* Download to Target the CE Config and the service-to-CE map */
1626 interconnect_targ_addr =
1627 host_interest_item_address(HI_ITEM(hi_interconnect_state));
1628
1629 /* Supply Target-side CE configuration */
1630 ret = ath10k_pci_diag_read_access(ar, interconnect_targ_addr,
1631 &pcie_state_targ_addr);
1632 if (ret != 0) {
1633 ath10k_err("Failed to get pcie state addr: %d\n", ret);
1634 return ret;
1635 }
1636
1637 if (pcie_state_targ_addr == 0) {
1638 ret = -EIO;
1639 ath10k_err("Invalid pcie state addr\n");
1640 return ret;
1641 }
1642
1643 ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
1644 offsetof(struct pcie_state,
1645 pipe_cfg_addr),
1646 &pipe_cfg_targ_addr);
1647 if (ret != 0) {
1648 ath10k_err("Failed to get pipe cfg addr: %d\n", ret);
1649 return ret;
1650 }
1651
1652 if (pipe_cfg_targ_addr == 0) {
1653 ret = -EIO;
1654 ath10k_err("Invalid pipe cfg addr\n");
1655 return ret;
1656 }
1657
1658 ret = ath10k_pci_diag_write_mem(ar, pipe_cfg_targ_addr,
1659 target_ce_config_wlan,
1660 sizeof(target_ce_config_wlan));
1661
1662 if (ret != 0) {
1663 ath10k_err("Failed to write pipe cfg: %d\n", ret);
1664 return ret;
1665 }
1666
1667 ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
1668 offsetof(struct pcie_state,
1669 svc_to_pipe_map),
1670 &svc_to_pipe_map);
1671 if (ret != 0) {
1672 ath10k_err("Failed to get svc/pipe map: %d\n", ret);
1673 return ret;
1674 }
1675
1676 if (svc_to_pipe_map == 0) {
1677 ret = -EIO;
1678 ath10k_err("Invalid svc_to_pipe map\n");
1679 return ret;
1680 }
1681
1682 ret = ath10k_pci_diag_write_mem(ar, svc_to_pipe_map,
1683 target_service_to_ce_map_wlan,
1684 sizeof(target_service_to_ce_map_wlan));
1685 if (ret != 0) {
1686 ath10k_err("Failed to write svc/pipe map: %d\n", ret);
1687 return ret;
1688 }
1689
1690 ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
1691 offsetof(struct pcie_state,
1692 config_flags),
1693 &pcie_config_flags);
1694 if (ret != 0) {
1695 ath10k_err("Failed to get pcie config_flags: %d\n", ret);
1696 return ret;
1697 }
1698
1699 pcie_config_flags &= ~PCIE_CONFIG_FLAG_ENABLE_L1;
1700
1701 ret = ath10k_pci_diag_write_mem(ar, pcie_state_targ_addr +
1702 offsetof(struct pcie_state, config_flags),
1703 &pcie_config_flags,
1704 sizeof(pcie_config_flags));
1705 if (ret != 0) {
1706 ath10k_err("Failed to write pcie config_flags: %d\n", ret);
1707 return ret;
1708 }
1709
1710 /* configure early allocation */
1711 ealloc_targ_addr = host_interest_item_address(HI_ITEM(hi_early_alloc));
1712
1713 ret = ath10k_pci_diag_read_access(ar, ealloc_targ_addr, &ealloc_value);
1714 if (ret != 0) {
1715 ath10k_err("Faile to get early alloc val: %d\n", ret);
1716 return ret;
1717 }
1718
1719 /* first bank is switched to IRAM */
1720 ealloc_value |= ((HI_EARLY_ALLOC_MAGIC << HI_EARLY_ALLOC_MAGIC_SHIFT) &
1721 HI_EARLY_ALLOC_MAGIC_MASK);
1722 ealloc_value |= ((1 << HI_EARLY_ALLOC_IRAM_BANKS_SHIFT) &
1723 HI_EARLY_ALLOC_IRAM_BANKS_MASK);
1724
1725 ret = ath10k_pci_diag_write_access(ar, ealloc_targ_addr, ealloc_value);
1726 if (ret != 0) {
1727 ath10k_err("Failed to set early alloc val: %d\n", ret);
1728 return ret;
1729 }
1730
1731 /* Tell Target to proceed with initialization */
1732 flag2_targ_addr = host_interest_item_address(HI_ITEM(hi_option_flag2));
1733
1734 ret = ath10k_pci_diag_read_access(ar, flag2_targ_addr, &flag2_value);
1735 if (ret != 0) {
1736 ath10k_err("Failed to get option val: %d\n", ret);
1737 return ret;
1738 }
1739
1740 flag2_value |= HI_OPTION_EARLY_CFG_DONE;
1741
1742 ret = ath10k_pci_diag_write_access(ar, flag2_targ_addr, flag2_value);
1743 if (ret != 0) {
1744 ath10k_err("Failed to set option val: %d\n", ret);
1745 return ret;
1746 }
1747
1748 return 0;
1749}
1750
1751
1752
1753static int ath10k_pci_ce_init(struct ath10k *ar)
1754{
1755 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +02001756 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001757 const struct ce_attr *attr;
1758 int pipe_num;
1759
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001760 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001761 pipe_info = &ar_pci->pipe_info[pipe_num];
1762 pipe_info->pipe_num = pipe_num;
1763 pipe_info->hif_ce_state = ar;
1764 attr = &host_ce_config_wlan[pipe_num];
1765
1766 pipe_info->ce_hdl = ath10k_ce_init(ar, pipe_num, attr);
1767 if (pipe_info->ce_hdl == NULL) {
1768 ath10k_err("Unable to initialize CE for pipe: %d\n",
1769 pipe_num);
1770
1771 /* It is safe to call it here. It checks if ce_hdl is
1772 * valid for each pipe */
1773 ath10k_pci_ce_deinit(ar);
1774 return -1;
1775 }
1776
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001777 if (pipe_num == CE_COUNT - 1) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001778 /*
1779 * Reserve the ultimate CE for
1780 * diagnostic Window support
1781 */
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001782 ar_pci->ce_diag = pipe_info->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001783 continue;
1784 }
1785
1786 pipe_info->buf_sz = (size_t) (attr->src_sz_max);
1787 }
1788
1789 /*
1790 * Initially, establish CE completion handlers for use with BMI.
1791 * These are overwritten with generic handlers after we exit BMI phase.
1792 */
1793 pipe_info = &ar_pci->pipe_info[BMI_CE_NUM_TO_TARG];
1794 ath10k_ce_send_cb_register(pipe_info->ce_hdl,
1795 ath10k_pci_bmi_send_done, 0);
1796
1797 pipe_info = &ar_pci->pipe_info[BMI_CE_NUM_TO_HOST];
1798 ath10k_ce_recv_cb_register(pipe_info->ce_hdl,
1799 ath10k_pci_bmi_recv_data);
1800
1801 return 0;
1802}
1803
1804static void ath10k_pci_fw_interrupt_handler(struct ath10k *ar)
1805{
1806 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1807 u32 fw_indicator_address, fw_indicator;
1808
1809 ath10k_pci_wake(ar);
1810
1811 fw_indicator_address = ar_pci->fw_indicator_address;
1812 fw_indicator = ath10k_pci_read32(ar, fw_indicator_address);
1813
1814 if (fw_indicator & FW_IND_EVENT_PENDING) {
1815 /* ACK: clear Target-side pending event */
1816 ath10k_pci_write32(ar, fw_indicator_address,
1817 fw_indicator & ~FW_IND_EVENT_PENDING);
1818
1819 if (ar_pci->started) {
1820 ath10k_pci_hif_dump_area(ar);
1821 } else {
1822 /*
1823 * Probable Target failure before we're prepared
1824 * to handle it. Generally unexpected.
1825 */
1826 ath10k_warn("early firmware event indicated\n");
1827 }
1828 }
1829
1830 ath10k_pci_sleep(ar);
1831}
1832
Michal Kazior8c5c5362013-07-16 09:38:50 +02001833static int ath10k_pci_hif_power_up(struct ath10k *ar)
1834{
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02001835 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001836 int ret;
1837
Michal Kazior32270b62013-08-02 09:15:47 +02001838 ret = ath10k_pci_start_intr(ar);
1839 if (ret) {
1840 ath10k_err("could not start interrupt handling (%d)\n", ret);
1841 goto err;
1842 }
1843
Michal Kazior8c5c5362013-07-16 09:38:50 +02001844 /*
1845 * Bring the target up cleanly.
1846 *
1847 * The target may be in an undefined state with an AUX-powered Target
1848 * and a Host in WoW mode. If the Host crashes, loses power, or is
1849 * restarted (without unloading the driver) then the Target is left
1850 * (aux) powered and running. On a subsequent driver load, the Target
1851 * is in an unexpected state. We try to catch that here in order to
1852 * reset the Target and retry the probe.
1853 */
Michal Kazior5b2589f2013-11-08 08:01:30 +01001854 ret = ath10k_pci_device_reset(ar);
1855 if (ret) {
1856 ath10k_err("failed to reset target: %d\n", ret);
1857 goto err_irq;
1858 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02001859
Michal Kaziord7fb47f2013-11-08 08:01:26 +01001860 ret = ath10k_pci_wait_for_target_init(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001861 if (ret)
Michal Kazior32270b62013-08-02 09:15:47 +02001862 goto err_irq;
Michal Kazior8c5c5362013-07-16 09:38:50 +02001863
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02001864 if (!test_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE, ar_pci->features))
Michal Kazior8c5c5362013-07-16 09:38:50 +02001865 /* Force AWAKE forever */
Michal Kazior8c5c5362013-07-16 09:38:50 +02001866 ath10k_do_pci_wake(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001867
1868 ret = ath10k_pci_ce_init(ar);
1869 if (ret)
1870 goto err_ps;
1871
1872 ret = ath10k_pci_init_config(ar);
1873 if (ret)
1874 goto err_ce;
1875
1876 ret = ath10k_pci_wake_target_cpu(ar);
1877 if (ret) {
1878 ath10k_err("could not wake up target CPU (%d)\n", ret);
1879 goto err_ce;
1880 }
1881
1882 return 0;
1883
1884err_ce:
1885 ath10k_pci_ce_deinit(ar);
1886err_ps:
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02001887 if (!test_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE, ar_pci->features))
Michal Kazior8c5c5362013-07-16 09:38:50 +02001888 ath10k_do_pci_sleep(ar);
Michal Kazior32270b62013-08-02 09:15:47 +02001889err_irq:
1890 ath10k_pci_stop_intr(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001891err:
1892 return ret;
1893}
1894
1895static void ath10k_pci_hif_power_down(struct ath10k *ar)
1896{
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02001897 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1898
Michal Kazior32270b62013-08-02 09:15:47 +02001899 ath10k_pci_stop_intr(ar);
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02001900
Michal Kazior8c5c5362013-07-16 09:38:50 +02001901 ath10k_pci_ce_deinit(ar);
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02001902 if (!test_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE, ar_pci->features))
Michal Kazior8c5c5362013-07-16 09:38:50 +02001903 ath10k_do_pci_sleep(ar);
1904}
1905
Michal Kazior8cd13ca2013-07-16 09:38:54 +02001906#ifdef CONFIG_PM
1907
1908#define ATH10K_PCI_PM_CONTROL 0x44
1909
1910static int ath10k_pci_hif_suspend(struct ath10k *ar)
1911{
1912 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1913 struct pci_dev *pdev = ar_pci->pdev;
1914 u32 val;
1915
1916 pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
1917
1918 if ((val & 0x000000ff) != 0x3) {
1919 pci_save_state(pdev);
1920 pci_disable_device(pdev);
1921 pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
1922 (val & 0xffffff00) | 0x03);
1923 }
1924
1925 return 0;
1926}
1927
1928static int ath10k_pci_hif_resume(struct ath10k *ar)
1929{
1930 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1931 struct pci_dev *pdev = ar_pci->pdev;
1932 u32 val;
1933
1934 pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
1935
1936 if ((val & 0x000000ff) != 0) {
1937 pci_restore_state(pdev);
1938 pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
1939 val & 0xffffff00);
1940 /*
1941 * Suspend/Resume resets the PCI configuration space,
1942 * so we have to re-disable the RETRY_TIMEOUT register (0x41)
1943 * to keep PCI Tx retries from interfering with C3 CPU state
1944 */
1945 pci_read_config_dword(pdev, 0x40, &val);
1946
1947 if ((val & 0x0000ff00) != 0)
1948 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
1949 }
1950
1951 return 0;
1952}
1953#endif
1954
Kalle Valo5e3dd152013-06-12 20:52:10 +03001955static const struct ath10k_hif_ops ath10k_pci_hif_ops = {
1956 .send_head = ath10k_pci_hif_send_head,
1957 .exchange_bmi_msg = ath10k_pci_hif_exchange_bmi_msg,
1958 .start = ath10k_pci_hif_start,
1959 .stop = ath10k_pci_hif_stop,
1960 .map_service_to_pipe = ath10k_pci_hif_map_service_to_pipe,
1961 .get_default_pipe = ath10k_pci_hif_get_default_pipe,
1962 .send_complete_check = ath10k_pci_hif_send_complete_check,
Michal Kaziore799bbf2013-07-05 16:15:12 +03001963 .set_callbacks = ath10k_pci_hif_set_callbacks,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001964 .get_free_queue_number = ath10k_pci_hif_get_free_queue_number,
Michal Kazior8c5c5362013-07-16 09:38:50 +02001965 .power_up = ath10k_pci_hif_power_up,
1966 .power_down = ath10k_pci_hif_power_down,
Michal Kazior8cd13ca2013-07-16 09:38:54 +02001967#ifdef CONFIG_PM
1968 .suspend = ath10k_pci_hif_suspend,
1969 .resume = ath10k_pci_hif_resume,
1970#endif
Kalle Valo5e3dd152013-06-12 20:52:10 +03001971};
1972
1973static void ath10k_pci_ce_tasklet(unsigned long ptr)
1974{
Michal Kazior87263e52013-08-27 13:08:01 +02001975 struct ath10k_pci_pipe *pipe = (struct ath10k_pci_pipe *)ptr;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001976 struct ath10k_pci *ar_pci = pipe->ar_pci;
1977
1978 ath10k_ce_per_engine_service(ar_pci->ar, pipe->pipe_num);
1979}
1980
1981static void ath10k_msi_err_tasklet(unsigned long data)
1982{
1983 struct ath10k *ar = (struct ath10k *)data;
1984
1985 ath10k_pci_fw_interrupt_handler(ar);
1986}
1987
1988/*
1989 * Handler for a per-engine interrupt on a PARTICULAR CE.
1990 * This is used in cases where each CE has a private MSI interrupt.
1991 */
1992static irqreturn_t ath10k_pci_per_engine_handler(int irq, void *arg)
1993{
1994 struct ath10k *ar = arg;
1995 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1996 int ce_id = irq - ar_pci->pdev->irq - MSI_ASSIGN_CE_INITIAL;
1997
Dan Carpentere5742672013-06-18 10:28:46 +03001998 if (ce_id < 0 || ce_id >= ARRAY_SIZE(ar_pci->pipe_info)) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001999 ath10k_warn("unexpected/invalid irq %d ce_id %d\n", irq, ce_id);
2000 return IRQ_HANDLED;
2001 }
2002
2003 /*
2004 * NOTE: We are able to derive ce_id from irq because we
2005 * use a one-to-one mapping for CE's 0..5.
2006 * CE's 6 & 7 do not use interrupts at all.
2007 *
2008 * This mapping must be kept in sync with the mapping
2009 * used by firmware.
2010 */
2011 tasklet_schedule(&ar_pci->pipe_info[ce_id].intr);
2012 return IRQ_HANDLED;
2013}
2014
2015static irqreturn_t ath10k_pci_msi_fw_handler(int irq, void *arg)
2016{
2017 struct ath10k *ar = arg;
2018 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2019
2020 tasklet_schedule(&ar_pci->msi_fw_err);
2021 return IRQ_HANDLED;
2022}
2023
2024/*
2025 * Top-level interrupt handler for all PCI interrupts from a Target.
2026 * When a block of MSI interrupts is allocated, this top-level handler
2027 * is not used; instead, we directly call the correct sub-handler.
2028 */
2029static irqreturn_t ath10k_pci_interrupt_handler(int irq, void *arg)
2030{
2031 struct ath10k *ar = arg;
2032 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2033
2034 if (ar_pci->num_msi_intrs == 0) {
2035 /*
2036 * IMPORTANT: INTR_CLR regiser has to be set after
2037 * INTR_ENABLE is set to 0, otherwise interrupt can not be
2038 * really cleared.
2039 */
2040 iowrite32(0, ar_pci->mem +
2041 (SOC_CORE_BASE_ADDRESS |
2042 PCIE_INTR_ENABLE_ADDRESS));
2043 iowrite32(PCIE_INTR_FIRMWARE_MASK |
2044 PCIE_INTR_CE_MASK_ALL,
2045 ar_pci->mem + (SOC_CORE_BASE_ADDRESS |
2046 PCIE_INTR_CLR_ADDRESS));
2047 /*
2048 * IMPORTANT: this extra read transaction is required to
2049 * flush the posted write buffer.
2050 */
2051 (void) ioread32(ar_pci->mem +
2052 (SOC_CORE_BASE_ADDRESS |
2053 PCIE_INTR_ENABLE_ADDRESS));
2054 }
2055
2056 tasklet_schedule(&ar_pci->intr_tq);
2057
2058 return IRQ_HANDLED;
2059}
2060
2061static void ath10k_pci_tasklet(unsigned long data)
2062{
2063 struct ath10k *ar = (struct ath10k *)data;
2064 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2065
2066 ath10k_pci_fw_interrupt_handler(ar); /* FIXME: Handle FW error */
2067 ath10k_ce_per_engine_service_any(ar);
2068
2069 if (ar_pci->num_msi_intrs == 0) {
2070 /* Enable Legacy PCI line interrupts */
2071 iowrite32(PCIE_INTR_FIRMWARE_MASK |
2072 PCIE_INTR_CE_MASK_ALL,
2073 ar_pci->mem + (SOC_CORE_BASE_ADDRESS |
2074 PCIE_INTR_ENABLE_ADDRESS));
2075 /*
2076 * IMPORTANT: this extra read transaction is required to
2077 * flush the posted write buffer
2078 */
2079 (void) ioread32(ar_pci->mem +
2080 (SOC_CORE_BASE_ADDRESS |
2081 PCIE_INTR_ENABLE_ADDRESS));
2082 }
2083}
2084
2085static int ath10k_pci_start_intr_msix(struct ath10k *ar, int num)
2086{
2087 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2088 int ret;
2089 int i;
2090
2091 ret = pci_enable_msi_block(ar_pci->pdev, num);
2092 if (ret)
2093 return ret;
2094
2095 ret = request_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW,
2096 ath10k_pci_msi_fw_handler,
2097 IRQF_SHARED, "ath10k_pci", ar);
Michal Kazior591ecdb2013-07-31 10:55:15 +02002098 if (ret) {
2099 ath10k_warn("request_irq(%d) failed %d\n",
2100 ar_pci->pdev->irq + MSI_ASSIGN_FW, ret);
2101
2102 pci_disable_msi(ar_pci->pdev);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002103 return ret;
Michal Kazior591ecdb2013-07-31 10:55:15 +02002104 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002105
2106 for (i = MSI_ASSIGN_CE_INITIAL; i <= MSI_ASSIGN_CE_MAX; i++) {
2107 ret = request_irq(ar_pci->pdev->irq + i,
2108 ath10k_pci_per_engine_handler,
2109 IRQF_SHARED, "ath10k_pci", ar);
2110 if (ret) {
2111 ath10k_warn("request_irq(%d) failed %d\n",
2112 ar_pci->pdev->irq + i, ret);
2113
Michal Kazior87b14232013-06-26 08:50:50 +02002114 for (i--; i >= MSI_ASSIGN_CE_INITIAL; i--)
2115 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002116
Michal Kazior87b14232013-06-26 08:50:50 +02002117 free_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002118 pci_disable_msi(ar_pci->pdev);
2119 return ret;
2120 }
2121 }
2122
2123 ath10k_info("MSI-X interrupt handling (%d intrs)\n", num);
2124 return 0;
2125}
2126
2127static int ath10k_pci_start_intr_msi(struct ath10k *ar)
2128{
2129 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2130 int ret;
2131
2132 ret = pci_enable_msi(ar_pci->pdev);
2133 if (ret < 0)
2134 return ret;
2135
2136 ret = request_irq(ar_pci->pdev->irq,
2137 ath10k_pci_interrupt_handler,
2138 IRQF_SHARED, "ath10k_pci", ar);
2139 if (ret < 0) {
2140 pci_disable_msi(ar_pci->pdev);
2141 return ret;
2142 }
2143
2144 ath10k_info("MSI interrupt handling\n");
2145 return 0;
2146}
2147
2148static int ath10k_pci_start_intr_legacy(struct ath10k *ar)
2149{
2150 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2151 int ret;
2152
2153 ret = request_irq(ar_pci->pdev->irq,
2154 ath10k_pci_interrupt_handler,
2155 IRQF_SHARED, "ath10k_pci", ar);
2156 if (ret < 0)
2157 return ret;
2158
Michal Kazior5b2589f2013-11-08 08:01:30 +01002159 ret = ath10k_do_pci_wake(ar);
Kalle Valof3782742013-10-17 11:36:15 +03002160 if (ret) {
Kalle Valof3782742013-10-17 11:36:15 +03002161 free_irq(ar_pci->pdev->irq, ar);
Michal Kazior5b2589f2013-11-08 08:01:30 +01002162 ath10k_err("failed to wake up target: %d\n", ret);
Kalle Valof3782742013-10-17 11:36:15 +03002163 return ret;
2164 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002165
2166 /*
2167 * A potential race occurs here: The CORE_BASE write
2168 * depends on target correctly decoding AXI address but
2169 * host won't know when target writes BAR to CORE_CTRL.
2170 * This write might get lost if target has NOT written BAR.
2171 * For now, fix the race by repeating the write in below
2172 * synchronization checking.
2173 */
2174 iowrite32(PCIE_INTR_FIRMWARE_MASK |
2175 PCIE_INTR_CE_MASK_ALL,
2176 ar_pci->mem + (SOC_CORE_BASE_ADDRESS |
2177 PCIE_INTR_ENABLE_ADDRESS));
Kalle Valo5e3dd152013-06-12 20:52:10 +03002178
Michal Kazior5b2589f2013-11-08 08:01:30 +01002179 ath10k_do_pci_sleep(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002180 ath10k_info("legacy interrupt handling\n");
2181 return 0;
2182}
2183
2184static int ath10k_pci_start_intr(struct ath10k *ar)
2185{
2186 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2187 int num = MSI_NUM_REQUEST;
2188 int ret;
2189 int i;
2190
2191 tasklet_init(&ar_pci->intr_tq, ath10k_pci_tasklet, (unsigned long) ar);
2192 tasklet_init(&ar_pci->msi_fw_err, ath10k_msi_err_tasklet,
2193 (unsigned long) ar);
2194
2195 for (i = 0; i < CE_COUNT; i++) {
2196 ar_pci->pipe_info[i].ar_pci = ar_pci;
2197 tasklet_init(&ar_pci->pipe_info[i].intr,
2198 ath10k_pci_ce_tasklet,
2199 (unsigned long)&ar_pci->pipe_info[i]);
2200 }
2201
2202 if (!test_bit(ATH10K_PCI_FEATURE_MSI_X, ar_pci->features))
2203 num = 1;
2204
2205 if (num > 1) {
2206 ret = ath10k_pci_start_intr_msix(ar, num);
2207 if (ret == 0)
2208 goto exit;
2209
2210 ath10k_warn("MSI-X didn't succeed (%d), trying MSI\n", ret);
2211 num = 1;
2212 }
2213
2214 if (num == 1) {
2215 ret = ath10k_pci_start_intr_msi(ar);
2216 if (ret == 0)
2217 goto exit;
2218
2219 ath10k_warn("MSI didn't succeed (%d), trying legacy INTR\n",
2220 ret);
2221 num = 0;
2222 }
2223
2224 ret = ath10k_pci_start_intr_legacy(ar);
Kalle Valof3782742013-10-17 11:36:15 +03002225 if (ret) {
2226 ath10k_warn("Failed to start legacy interrupts: %d\n", ret);
2227 return ret;
2228 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002229
2230exit:
2231 ar_pci->num_msi_intrs = num;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002232 return ret;
2233}
2234
2235static void ath10k_pci_stop_intr(struct ath10k *ar)
2236{
2237 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2238 int i;
2239
2240 /* There's at least one interrupt irregardless whether its legacy INTR
2241 * or MSI or MSI-X */
2242 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
2243 free_irq(ar_pci->pdev->irq + i, ar);
2244
2245 if (ar_pci->num_msi_intrs > 0)
2246 pci_disable_msi(ar_pci->pdev);
2247}
2248
Michal Kaziord7fb47f2013-11-08 08:01:26 +01002249static int ath10k_pci_wait_for_target_init(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002250{
2251 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2252 int wait_limit = 300; /* 3 sec */
Kalle Valof3782742013-10-17 11:36:15 +03002253 int ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002254
Michal Kazior5b2589f2013-11-08 08:01:30 +01002255 ret = ath10k_do_pci_wake(ar);
Kalle Valof3782742013-10-17 11:36:15 +03002256 if (ret) {
Michal Kazior5b2589f2013-11-08 08:01:30 +01002257 ath10k_err("failed to wake up target: %d\n", ret);
Kalle Valof3782742013-10-17 11:36:15 +03002258 return ret;
2259 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002260
2261 while (wait_limit-- &&
2262 !(ioread32(ar_pci->mem + FW_INDICATOR_ADDRESS) &
2263 FW_IND_INITIALIZED)) {
2264 if (ar_pci->num_msi_intrs == 0)
2265 /* Fix potential race by repeating CORE_BASE writes */
2266 iowrite32(PCIE_INTR_FIRMWARE_MASK |
2267 PCIE_INTR_CE_MASK_ALL,
2268 ar_pci->mem + (SOC_CORE_BASE_ADDRESS |
2269 PCIE_INTR_ENABLE_ADDRESS));
2270 mdelay(10);
2271 }
2272
2273 if (wait_limit < 0) {
Michal Kazior5b2589f2013-11-08 08:01:30 +01002274 ath10k_err("target stalled\n");
2275 ret = -EIO;
2276 goto out;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002277 }
2278
Michal Kazior5b2589f2013-11-08 08:01:30 +01002279out:
2280 ath10k_do_pci_sleep(ar);
2281 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002282}
2283
Michal Kazior5b2589f2013-11-08 08:01:30 +01002284static int ath10k_pci_device_reset(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002285{
Michal Kazior5b2589f2013-11-08 08:01:30 +01002286 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002287 u32 val;
2288
Michal Kazior5b2589f2013-11-08 08:01:30 +01002289 ret = ath10k_do_pci_wake(ar);
2290 if (ret) {
2291 ath10k_err("failed to wake up target: %d\n",
2292 ret);
2293 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002294 }
2295
2296 /* Put Target, including PCIe, into RESET. */
Kalle Valoe479ed42013-09-01 10:01:53 +03002297 val = ath10k_pci_reg_read32(ar, SOC_GLOBAL_RESET_ADDRESS);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002298 val |= 1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002299 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002300
2301 for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
Kalle Valoe479ed42013-09-01 10:01:53 +03002302 if (ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03002303 RTC_STATE_COLD_RESET_MASK)
2304 break;
2305 msleep(1);
2306 }
2307
2308 /* Pull Target, including PCIe, out of RESET. */
2309 val &= ~1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002310 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002311
2312 for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
Kalle Valoe479ed42013-09-01 10:01:53 +03002313 if (!(ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03002314 RTC_STATE_COLD_RESET_MASK))
2315 break;
2316 msleep(1);
2317 }
2318
Michal Kazior5b2589f2013-11-08 08:01:30 +01002319 ath10k_do_pci_sleep(ar);
2320 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002321}
2322
2323static void ath10k_pci_dump_features(struct ath10k_pci *ar_pci)
2324{
2325 int i;
2326
2327 for (i = 0; i < ATH10K_PCI_FEATURE_COUNT; i++) {
2328 if (!test_bit(i, ar_pci->features))
2329 continue;
2330
2331 switch (i) {
2332 case ATH10K_PCI_FEATURE_MSI_X:
Kalle Valo24cfade2013-09-08 17:55:50 +03002333 ath10k_dbg(ATH10K_DBG_BOOT, "device supports MSI-X\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002334 break;
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002335 case ATH10K_PCI_FEATURE_SOC_POWER_SAVE:
Kalle Valo24cfade2013-09-08 17:55:50 +03002336 ath10k_dbg(ATH10K_DBG_BOOT, "QCA98XX SoC power save enabled\n");
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002337 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002338 }
2339 }
2340}
2341
2342static int ath10k_pci_probe(struct pci_dev *pdev,
2343 const struct pci_device_id *pci_dev)
2344{
2345 void __iomem *mem;
2346 int ret = 0;
2347 struct ath10k *ar;
2348 struct ath10k_pci *ar_pci;
Kalle Valoe01ae682013-09-01 11:22:14 +03002349 u32 lcr_val, chip_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002350
2351 ath10k_dbg(ATH10K_DBG_PCI, "%s\n", __func__);
2352
2353 ar_pci = kzalloc(sizeof(*ar_pci), GFP_KERNEL);
2354 if (ar_pci == NULL)
2355 return -ENOMEM;
2356
2357 ar_pci->pdev = pdev;
2358 ar_pci->dev = &pdev->dev;
2359
2360 switch (pci_dev->device) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03002361 case QCA988X_2_0_DEVICE_ID:
2362 set_bit(ATH10K_PCI_FEATURE_MSI_X, ar_pci->features);
2363 break;
2364 default:
2365 ret = -ENODEV;
2366 ath10k_err("Unkown device ID: %d\n", pci_dev->device);
2367 goto err_ar_pci;
2368 }
2369
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002370 if (ath10k_target_ps)
2371 set_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE, ar_pci->features);
2372
Kalle Valo5e3dd152013-06-12 20:52:10 +03002373 ath10k_pci_dump_features(ar_pci);
2374
Michal Kazior3a0861f2013-07-05 16:15:06 +03002375 ar = ath10k_core_create(ar_pci, ar_pci->dev, &ath10k_pci_hif_ops);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002376 if (!ar) {
2377 ath10k_err("ath10k_core_create failed!\n");
2378 ret = -EINVAL;
2379 goto err_ar_pci;
2380 }
2381
Kalle Valo5e3dd152013-06-12 20:52:10 +03002382 ar_pci->ar = ar;
2383 ar_pci->fw_indicator_address = FW_INDICATOR_ADDRESS;
2384 atomic_set(&ar_pci->keep_awake_count, 0);
2385
2386 pci_set_drvdata(pdev, ar);
2387
2388 /*
2389 * Without any knowledge of the Host, the Target may have been reset or
2390 * power cycled and its Config Space may no longer reflect the PCI
2391 * address space that was assigned earlier by the PCI infrastructure.
2392 * Refresh it now.
2393 */
2394 ret = pci_assign_resource(pdev, BAR_NUM);
2395 if (ret) {
2396 ath10k_err("cannot assign PCI space: %d\n", ret);
2397 goto err_ar;
2398 }
2399
2400 ret = pci_enable_device(pdev);
2401 if (ret) {
2402 ath10k_err("cannot enable PCI device: %d\n", ret);
2403 goto err_ar;
2404 }
2405
2406 /* Request MMIO resources */
2407 ret = pci_request_region(pdev, BAR_NUM, "ath");
2408 if (ret) {
2409 ath10k_err("PCI MMIO reservation error: %d\n", ret);
2410 goto err_device;
2411 }
2412
2413 /*
2414 * Target structures have a limit of 32 bit DMA pointers.
2415 * DMA pointers can be wider than 32 bits by default on some systems.
2416 */
2417 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2418 if (ret) {
2419 ath10k_err("32-bit DMA not available: %d\n", ret);
2420 goto err_region;
2421 }
2422
2423 ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2424 if (ret) {
2425 ath10k_err("cannot enable 32-bit consistent DMA\n");
2426 goto err_region;
2427 }
2428
2429 /* Set bus master bit in PCI_COMMAND to enable DMA */
2430 pci_set_master(pdev);
2431
2432 /*
2433 * Temporary FIX: disable ASPM
2434 * Will be removed after the OTP is programmed
2435 */
2436 pci_read_config_dword(pdev, 0x80, &lcr_val);
2437 pci_write_config_dword(pdev, 0x80, (lcr_val & 0xffffff00));
2438
2439 /* Arrange for access to Target SoC registers. */
2440 mem = pci_iomap(pdev, BAR_NUM, 0);
2441 if (!mem) {
2442 ath10k_err("PCI iomap error\n");
2443 ret = -EIO;
2444 goto err_master;
2445 }
2446
2447 ar_pci->mem = mem;
2448
2449 spin_lock_init(&ar_pci->ce_lock);
2450
Kalle Valoe01ae682013-09-01 11:22:14 +03002451 ret = ath10k_do_pci_wake(ar);
2452 if (ret) {
2453 ath10k_err("Failed to get chip id: %d\n", ret);
Wei Yongjun12eb0872013-10-30 13:24:39 +08002454 goto err_iomap;
Kalle Valoe01ae682013-09-01 11:22:14 +03002455 }
2456
Kalle Valo233eb972013-10-16 16:46:11 +03002457 chip_id = ath10k_pci_soc_read32(ar, SOC_CHIP_ID_ADDRESS);
Kalle Valoe01ae682013-09-01 11:22:14 +03002458
2459 ath10k_do_pci_sleep(ar);
2460
Kalle Valo24cfade2013-09-08 17:55:50 +03002461 ath10k_dbg(ATH10K_DBG_BOOT, "boot pci_mem 0x%p\n", ar_pci->mem);
2462
Kalle Valoe01ae682013-09-01 11:22:14 +03002463 ret = ath10k_core_register(ar, chip_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002464 if (ret) {
2465 ath10k_err("could not register driver core (%d)\n", ret);
Michal Kazior32270b62013-08-02 09:15:47 +02002466 goto err_iomap;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002467 }
2468
2469 return 0;
2470
Kalle Valo5e3dd152013-06-12 20:52:10 +03002471err_iomap:
2472 pci_iounmap(pdev, mem);
2473err_master:
2474 pci_clear_master(pdev);
2475err_region:
2476 pci_release_region(pdev, BAR_NUM);
2477err_device:
2478 pci_disable_device(pdev);
2479err_ar:
Kalle Valo5e3dd152013-06-12 20:52:10 +03002480 ath10k_core_destroy(ar);
2481err_ar_pci:
2482 /* call HIF PCI free here */
2483 kfree(ar_pci);
2484
2485 return ret;
2486}
2487
2488static void ath10k_pci_remove(struct pci_dev *pdev)
2489{
2490 struct ath10k *ar = pci_get_drvdata(pdev);
2491 struct ath10k_pci *ar_pci;
2492
2493 ath10k_dbg(ATH10K_DBG_PCI, "%s\n", __func__);
2494
2495 if (!ar)
2496 return;
2497
2498 ar_pci = ath10k_pci_priv(ar);
2499
2500 if (!ar_pci)
2501 return;
2502
2503 tasklet_kill(&ar_pci->msi_fw_err);
2504
2505 ath10k_core_unregister(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002506
Kalle Valo5e3dd152013-06-12 20:52:10 +03002507 pci_iounmap(pdev, ar_pci->mem);
2508 pci_release_region(pdev, BAR_NUM);
2509 pci_clear_master(pdev);
2510 pci_disable_device(pdev);
2511
2512 ath10k_core_destroy(ar);
2513 kfree(ar_pci);
2514}
2515
Kalle Valo5e3dd152013-06-12 20:52:10 +03002516MODULE_DEVICE_TABLE(pci, ath10k_pci_id_table);
2517
2518static struct pci_driver ath10k_pci_driver = {
2519 .name = "ath10k_pci",
2520 .id_table = ath10k_pci_id_table,
2521 .probe = ath10k_pci_probe,
2522 .remove = ath10k_pci_remove,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002523};
2524
2525static int __init ath10k_pci_init(void)
2526{
2527 int ret;
2528
2529 ret = pci_register_driver(&ath10k_pci_driver);
2530 if (ret)
2531 ath10k_err("pci_register_driver failed [%d]\n", ret);
2532
2533 return ret;
2534}
2535module_init(ath10k_pci_init);
2536
2537static void __exit ath10k_pci_exit(void)
2538{
2539 pci_unregister_driver(&ath10k_pci_driver);
2540}
2541
2542module_exit(ath10k_pci_exit);
2543
2544MODULE_AUTHOR("Qualcomm Atheros");
2545MODULE_DESCRIPTION("Driver support for Atheros QCA988X PCIe devices");
2546MODULE_LICENSE("Dual BSD/GPL");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002547MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_FW_FILE);
2548MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_OTP_FILE);
2549MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_BOARD_DATA_FILE);