blob: e552770a9e6d10d54e05772949be436a33b96d74 [file] [log] [blame]
Kalle Valo5e3dd152013-06-12 20:52:10 +03001/*
2 * Copyright (c) 2005-2011 Atheros Communications Inc.
3 * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <linux/pci.h>
19#include <linux/module.h>
20#include <linux/interrupt.h>
21#include <linux/spinlock.h>
22
23#include "core.h"
24#include "debug.h"
25
26#include "targaddrs.h"
27#include "bmi.h"
28
29#include "hif.h"
30#include "htc.h"
31
32#include "ce.h"
33#include "pci.h"
34
Bartosz Markowski8cc8df92013-08-02 09:58:49 +020035static unsigned int ath10k_target_ps;
Kalle Valo5e3dd152013-06-12 20:52:10 +030036module_param(ath10k_target_ps, uint, 0644);
37MODULE_PARM_DESC(ath10k_target_ps, "Enable ath10k Target (SoC) PS option");
38
Kalle Valo5e3dd152013-06-12 20:52:10 +030039#define QCA988X_2_0_DEVICE_ID (0x003c)
40
41static DEFINE_PCI_DEVICE_TABLE(ath10k_pci_id_table) = {
Kalle Valo5e3dd152013-06-12 20:52:10 +030042 { PCI_VDEVICE(ATHEROS, QCA988X_2_0_DEVICE_ID) }, /* PCI-E QCA988X V2 */
43 {0}
44};
45
46static int ath10k_pci_diag_read_access(struct ath10k *ar, u32 address,
47 u32 *data);
48
49static void ath10k_pci_process_ce(struct ath10k *ar);
50static int ath10k_pci_post_rx(struct ath10k *ar);
Michal Kazior87263e52013-08-27 13:08:01 +020051static int ath10k_pci_post_rx_pipe(struct ath10k_pci_pipe *pipe_info,
Kalle Valo5e3dd152013-06-12 20:52:10 +030052 int num);
Michal Kazior87263e52013-08-27 13:08:01 +020053static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe *pipe_info);
Kalle Valo5e3dd152013-06-12 20:52:10 +030054static void ath10k_pci_stop_ce(struct ath10k *ar);
Michal Kazior5b2589f2013-11-08 08:01:30 +010055static int ath10k_pci_device_reset(struct ath10k *ar);
Michal Kaziord7fb47f2013-11-08 08:01:26 +010056static int ath10k_pci_wait_for_target_init(struct ath10k *ar);
Michal Kazior32270b62013-08-02 09:15:47 +020057static int ath10k_pci_start_intr(struct ath10k *ar);
58static void ath10k_pci_stop_intr(struct ath10k *ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +030059
60static const struct ce_attr host_ce_config_wlan[] = {
Kalle Valo48e9c222013-09-01 10:01:32 +030061 /* CE0: host->target HTC control and raw streams */
62 {
63 .flags = CE_ATTR_FLAGS,
64 .src_nentries = 16,
65 .src_sz_max = 256,
66 .dest_nentries = 0,
67 },
68
69 /* CE1: target->host HTT + HTC control */
70 {
71 .flags = CE_ATTR_FLAGS,
72 .src_nentries = 0,
73 .src_sz_max = 512,
74 .dest_nentries = 512,
75 },
76
77 /* CE2: target->host WMI */
78 {
79 .flags = CE_ATTR_FLAGS,
80 .src_nentries = 0,
81 .src_sz_max = 2048,
82 .dest_nentries = 32,
83 },
84
85 /* CE3: host->target WMI */
86 {
87 .flags = CE_ATTR_FLAGS,
88 .src_nentries = 32,
89 .src_sz_max = 2048,
90 .dest_nentries = 0,
91 },
92
93 /* CE4: host->target HTT */
94 {
95 .flags = CE_ATTR_FLAGS | CE_ATTR_DIS_INTR,
96 .src_nentries = CE_HTT_H2T_MSG_SRC_NENTRIES,
97 .src_sz_max = 256,
98 .dest_nentries = 0,
99 },
100
101 /* CE5: unused */
102 {
103 .flags = CE_ATTR_FLAGS,
104 .src_nentries = 0,
105 .src_sz_max = 0,
106 .dest_nentries = 0,
107 },
108
109 /* CE6: target autonomous hif_memcpy */
110 {
111 .flags = CE_ATTR_FLAGS,
112 .src_nentries = 0,
113 .src_sz_max = 0,
114 .dest_nentries = 0,
115 },
116
117 /* CE7: ce_diag, the Diagnostic Window */
118 {
119 .flags = CE_ATTR_FLAGS,
120 .src_nentries = 2,
121 .src_sz_max = DIAG_TRANSFER_LIMIT,
122 .dest_nentries = 2,
123 },
Kalle Valo5e3dd152013-06-12 20:52:10 +0300124};
125
126/* Target firmware's Copy Engine configuration. */
127static const struct ce_pipe_config target_ce_config_wlan[] = {
Kalle Valod88effb2013-09-01 10:01:39 +0300128 /* CE0: host->target HTC control and raw streams */
129 {
130 .pipenum = 0,
131 .pipedir = PIPEDIR_OUT,
132 .nentries = 32,
133 .nbytes_max = 256,
134 .flags = CE_ATTR_FLAGS,
135 .reserved = 0,
136 },
137
138 /* CE1: target->host HTT + HTC control */
139 {
140 .pipenum = 1,
141 .pipedir = PIPEDIR_IN,
142 .nentries = 32,
143 .nbytes_max = 512,
144 .flags = CE_ATTR_FLAGS,
145 .reserved = 0,
146 },
147
148 /* CE2: target->host WMI */
149 {
150 .pipenum = 2,
151 .pipedir = PIPEDIR_IN,
152 .nentries = 32,
153 .nbytes_max = 2048,
154 .flags = CE_ATTR_FLAGS,
155 .reserved = 0,
156 },
157
158 /* CE3: host->target WMI */
159 {
160 .pipenum = 3,
161 .pipedir = PIPEDIR_OUT,
162 .nentries = 32,
163 .nbytes_max = 2048,
164 .flags = CE_ATTR_FLAGS,
165 .reserved = 0,
166 },
167
168 /* CE4: host->target HTT */
169 {
170 .pipenum = 4,
171 .pipedir = PIPEDIR_OUT,
172 .nentries = 256,
173 .nbytes_max = 256,
174 .flags = CE_ATTR_FLAGS,
175 .reserved = 0,
176 },
177
Kalle Valo5e3dd152013-06-12 20:52:10 +0300178 /* NB: 50% of src nentries, since tx has 2 frags */
Kalle Valod88effb2013-09-01 10:01:39 +0300179
180 /* CE5: unused */
181 {
182 .pipenum = 5,
183 .pipedir = PIPEDIR_OUT,
184 .nentries = 32,
185 .nbytes_max = 2048,
186 .flags = CE_ATTR_FLAGS,
187 .reserved = 0,
188 },
189
190 /* CE6: Reserved for target autonomous hif_memcpy */
191 {
192 .pipenum = 6,
193 .pipedir = PIPEDIR_INOUT,
194 .nentries = 32,
195 .nbytes_max = 4096,
196 .flags = CE_ATTR_FLAGS,
197 .reserved = 0,
198 },
199
Kalle Valo5e3dd152013-06-12 20:52:10 +0300200 /* CE7 used only by Host */
201};
202
203/*
204 * Diagnostic read/write access is provided for startup/config/debug usage.
205 * Caller must guarantee proper alignment, when applicable, and single user
206 * at any moment.
207 */
208static int ath10k_pci_diag_read_mem(struct ath10k *ar, u32 address, void *data,
209 int nbytes)
210{
211 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
212 int ret = 0;
213 u32 buf;
214 unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
215 unsigned int id;
216 unsigned int flags;
Michal Kazior2aa39112013-08-27 13:08:02 +0200217 struct ath10k_ce_pipe *ce_diag;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300218 /* Host buffer address in CE space */
219 u32 ce_data;
220 dma_addr_t ce_data_base = 0;
221 void *data_buf = NULL;
222 int i;
223
224 /*
225 * This code cannot handle reads to non-memory space. Redirect to the
226 * register read fn but preserve the multi word read capability of
227 * this fn
228 */
229 if (address < DRAM_BASE_ADDRESS) {
230 if (!IS_ALIGNED(address, 4) ||
231 !IS_ALIGNED((unsigned long)data, 4))
232 return -EIO;
233
234 while ((nbytes >= 4) && ((ret = ath10k_pci_diag_read_access(
235 ar, address, (u32 *)data)) == 0)) {
236 nbytes -= sizeof(u32);
237 address += sizeof(u32);
238 data += sizeof(u32);
239 }
240 return ret;
241 }
242
243 ce_diag = ar_pci->ce_diag;
244
245 /*
246 * Allocate a temporary bounce buffer to hold caller's data
247 * to be DMA'ed from Target. This guarantees
248 * 1) 4-byte alignment
249 * 2) Buffer in DMA-able space
250 */
251 orig_nbytes = nbytes;
252 data_buf = (unsigned char *)pci_alloc_consistent(ar_pci->pdev,
253 orig_nbytes,
254 &ce_data_base);
255
256 if (!data_buf) {
257 ret = -ENOMEM;
258 goto done;
259 }
260 memset(data_buf, 0, orig_nbytes);
261
262 remaining_bytes = orig_nbytes;
263 ce_data = ce_data_base;
264 while (remaining_bytes) {
265 nbytes = min_t(unsigned int, remaining_bytes,
266 DIAG_TRANSFER_LIMIT);
267
268 ret = ath10k_ce_recv_buf_enqueue(ce_diag, NULL, ce_data);
269 if (ret != 0)
270 goto done;
271
272 /* Request CE to send from Target(!) address to Host buffer */
273 /*
274 * The address supplied by the caller is in the
275 * Target CPU virtual address space.
276 *
277 * In order to use this address with the diagnostic CE,
278 * convert it from Target CPU virtual address space
279 * to CE address space
280 */
281 ath10k_pci_wake(ar);
282 address = TARG_CPU_SPACE_TO_CE_SPACE(ar, ar_pci->mem,
283 address);
284 ath10k_pci_sleep(ar);
285
286 ret = ath10k_ce_send(ce_diag, NULL, (u32)address, nbytes, 0,
287 0);
288 if (ret)
289 goto done;
290
291 i = 0;
292 while (ath10k_ce_completed_send_next(ce_diag, NULL, &buf,
293 &completed_nbytes,
294 &id) != 0) {
295 mdelay(1);
296 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
297 ret = -EBUSY;
298 goto done;
299 }
300 }
301
302 if (nbytes != completed_nbytes) {
303 ret = -EIO;
304 goto done;
305 }
306
307 if (buf != (u32) address) {
308 ret = -EIO;
309 goto done;
310 }
311
312 i = 0;
313 while (ath10k_ce_completed_recv_next(ce_diag, NULL, &buf,
314 &completed_nbytes,
315 &id, &flags) != 0) {
316 mdelay(1);
317
318 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
319 ret = -EBUSY;
320 goto done;
321 }
322 }
323
324 if (nbytes != completed_nbytes) {
325 ret = -EIO;
326 goto done;
327 }
328
329 if (buf != ce_data) {
330 ret = -EIO;
331 goto done;
332 }
333
334 remaining_bytes -= nbytes;
335 address += nbytes;
336 ce_data += nbytes;
337 }
338
339done:
340 if (ret == 0) {
341 /* Copy data from allocated DMA buf to caller's buf */
342 WARN_ON_ONCE(orig_nbytes & 3);
343 for (i = 0; i < orig_nbytes / sizeof(__le32); i++) {
344 ((u32 *)data)[i] =
345 __le32_to_cpu(((__le32 *)data_buf)[i]);
346 }
347 } else
348 ath10k_dbg(ATH10K_DBG_PCI, "%s failure (0x%x)\n",
349 __func__, address);
350
351 if (data_buf)
352 pci_free_consistent(ar_pci->pdev, orig_nbytes,
353 data_buf, ce_data_base);
354
355 return ret;
356}
357
358/* Read 4-byte aligned data from Target memory or register */
359static int ath10k_pci_diag_read_access(struct ath10k *ar, u32 address,
360 u32 *data)
361{
362 /* Assume range doesn't cross this boundary */
363 if (address >= DRAM_BASE_ADDRESS)
364 return ath10k_pci_diag_read_mem(ar, address, data, sizeof(u32));
365
366 ath10k_pci_wake(ar);
367 *data = ath10k_pci_read32(ar, address);
368 ath10k_pci_sleep(ar);
369 return 0;
370}
371
372static int ath10k_pci_diag_write_mem(struct ath10k *ar, u32 address,
373 const void *data, int nbytes)
374{
375 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
376 int ret = 0;
377 u32 buf;
378 unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
379 unsigned int id;
380 unsigned int flags;
Michal Kazior2aa39112013-08-27 13:08:02 +0200381 struct ath10k_ce_pipe *ce_diag;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300382 void *data_buf = NULL;
383 u32 ce_data; /* Host buffer address in CE space */
384 dma_addr_t ce_data_base = 0;
385 int i;
386
387 ce_diag = ar_pci->ce_diag;
388
389 /*
390 * Allocate a temporary bounce buffer to hold caller's data
391 * to be DMA'ed to Target. This guarantees
392 * 1) 4-byte alignment
393 * 2) Buffer in DMA-able space
394 */
395 orig_nbytes = nbytes;
396 data_buf = (unsigned char *)pci_alloc_consistent(ar_pci->pdev,
397 orig_nbytes,
398 &ce_data_base);
399 if (!data_buf) {
400 ret = -ENOMEM;
401 goto done;
402 }
403
404 /* Copy caller's data to allocated DMA buf */
405 WARN_ON_ONCE(orig_nbytes & 3);
406 for (i = 0; i < orig_nbytes / sizeof(__le32); i++)
407 ((__le32 *)data_buf)[i] = __cpu_to_le32(((u32 *)data)[i]);
408
409 /*
410 * The address supplied by the caller is in the
411 * Target CPU virtual address space.
412 *
413 * In order to use this address with the diagnostic CE,
414 * convert it from
415 * Target CPU virtual address space
416 * to
417 * CE address space
418 */
419 ath10k_pci_wake(ar);
420 address = TARG_CPU_SPACE_TO_CE_SPACE(ar, ar_pci->mem, address);
421 ath10k_pci_sleep(ar);
422
423 remaining_bytes = orig_nbytes;
424 ce_data = ce_data_base;
425 while (remaining_bytes) {
426 /* FIXME: check cast */
427 nbytes = min_t(int, remaining_bytes, DIAG_TRANSFER_LIMIT);
428
429 /* Set up to receive directly into Target(!) address */
430 ret = ath10k_ce_recv_buf_enqueue(ce_diag, NULL, address);
431 if (ret != 0)
432 goto done;
433
434 /*
435 * Request CE to send caller-supplied data that
436 * was copied to bounce buffer to Target(!) address.
437 */
438 ret = ath10k_ce_send(ce_diag, NULL, (u32) ce_data,
439 nbytes, 0, 0);
440 if (ret != 0)
441 goto done;
442
443 i = 0;
444 while (ath10k_ce_completed_send_next(ce_diag, NULL, &buf,
445 &completed_nbytes,
446 &id) != 0) {
447 mdelay(1);
448
449 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
450 ret = -EBUSY;
451 goto done;
452 }
453 }
454
455 if (nbytes != completed_nbytes) {
456 ret = -EIO;
457 goto done;
458 }
459
460 if (buf != ce_data) {
461 ret = -EIO;
462 goto done;
463 }
464
465 i = 0;
466 while (ath10k_ce_completed_recv_next(ce_diag, NULL, &buf,
467 &completed_nbytes,
468 &id, &flags) != 0) {
469 mdelay(1);
470
471 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
472 ret = -EBUSY;
473 goto done;
474 }
475 }
476
477 if (nbytes != completed_nbytes) {
478 ret = -EIO;
479 goto done;
480 }
481
482 if (buf != address) {
483 ret = -EIO;
484 goto done;
485 }
486
487 remaining_bytes -= nbytes;
488 address += nbytes;
489 ce_data += nbytes;
490 }
491
492done:
493 if (data_buf) {
494 pci_free_consistent(ar_pci->pdev, orig_nbytes, data_buf,
495 ce_data_base);
496 }
497
498 if (ret != 0)
499 ath10k_dbg(ATH10K_DBG_PCI, "%s failure (0x%x)\n", __func__,
500 address);
501
502 return ret;
503}
504
505/* Write 4B data to Target memory or register */
506static int ath10k_pci_diag_write_access(struct ath10k *ar, u32 address,
507 u32 data)
508{
509 /* Assume range doesn't cross this boundary */
510 if (address >= DRAM_BASE_ADDRESS)
511 return ath10k_pci_diag_write_mem(ar, address, &data,
512 sizeof(u32));
513
514 ath10k_pci_wake(ar);
515 ath10k_pci_write32(ar, address, data);
516 ath10k_pci_sleep(ar);
517 return 0;
518}
519
520static bool ath10k_pci_target_is_awake(struct ath10k *ar)
521{
522 void __iomem *mem = ath10k_pci_priv(ar)->mem;
523 u32 val;
524 val = ioread32(mem + PCIE_LOCAL_BASE_ADDRESS +
525 RTC_STATE_ADDRESS);
526 return (RTC_STATE_V_GET(val) == RTC_STATE_V_ON);
527}
528
Kalle Valo3aebe542013-09-01 10:02:07 +0300529int ath10k_do_pci_wake(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300530{
531 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
532 void __iomem *pci_addr = ar_pci->mem;
533 int tot_delay = 0;
534 int curr_delay = 5;
535
536 if (atomic_read(&ar_pci->keep_awake_count) == 0) {
537 /* Force AWAKE */
538 iowrite32(PCIE_SOC_WAKE_V_MASK,
539 pci_addr + PCIE_LOCAL_BASE_ADDRESS +
540 PCIE_SOC_WAKE_ADDRESS);
541 }
542 atomic_inc(&ar_pci->keep_awake_count);
543
544 if (ar_pci->verified_awake)
Kalle Valo3aebe542013-09-01 10:02:07 +0300545 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300546
547 for (;;) {
548 if (ath10k_pci_target_is_awake(ar)) {
549 ar_pci->verified_awake = true;
Kalle Valo3aebe542013-09-01 10:02:07 +0300550 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300551 }
552
553 if (tot_delay > PCIE_WAKE_TIMEOUT) {
Kalle Valo3aebe542013-09-01 10:02:07 +0300554 ath10k_warn("target took longer %d us to wake up (awake count %d)\n",
555 PCIE_WAKE_TIMEOUT,
Kalle Valo5e3dd152013-06-12 20:52:10 +0300556 atomic_read(&ar_pci->keep_awake_count));
Kalle Valo3aebe542013-09-01 10:02:07 +0300557 return -ETIMEDOUT;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300558 }
559
560 udelay(curr_delay);
561 tot_delay += curr_delay;
562
563 if (curr_delay < 50)
564 curr_delay += 5;
565 }
566}
567
568void ath10k_do_pci_sleep(struct ath10k *ar)
569{
570 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
571 void __iomem *pci_addr = ar_pci->mem;
572
573 if (atomic_dec_and_test(&ar_pci->keep_awake_count)) {
574 /* Allow sleep */
575 ar_pci->verified_awake = false;
576 iowrite32(PCIE_SOC_WAKE_RESET,
577 pci_addr + PCIE_LOCAL_BASE_ADDRESS +
578 PCIE_SOC_WAKE_ADDRESS);
579 }
580}
581
582/*
583 * FIXME: Handle OOM properly.
584 */
585static inline
Michal Kazior87263e52013-08-27 13:08:01 +0200586struct ath10k_pci_compl *get_free_compl(struct ath10k_pci_pipe *pipe_info)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300587{
588 struct ath10k_pci_compl *compl = NULL;
589
590 spin_lock_bh(&pipe_info->pipe_lock);
591 if (list_empty(&pipe_info->compl_free)) {
592 ath10k_warn("Completion buffers are full\n");
593 goto exit;
594 }
595 compl = list_first_entry(&pipe_info->compl_free,
596 struct ath10k_pci_compl, list);
597 list_del(&compl->list);
598exit:
599 spin_unlock_bh(&pipe_info->pipe_lock);
600 return compl;
601}
602
603/* Called by lower (CE) layer when a send to Target completes. */
Michal Kazior5440ce22013-09-03 15:09:58 +0200604static void ath10k_pci_ce_send_done(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300605{
606 struct ath10k *ar = ce_state->ar;
607 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +0200608 struct ath10k_pci_pipe *pipe_info = &ar_pci->pipe_info[ce_state->id];
Kalle Valo5e3dd152013-06-12 20:52:10 +0300609 struct ath10k_pci_compl *compl;
Michal Kazior5440ce22013-09-03 15:09:58 +0200610 void *transfer_context;
611 u32 ce_data;
612 unsigned int nbytes;
613 unsigned int transfer_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300614
Michal Kazior5440ce22013-09-03 15:09:58 +0200615 while (ath10k_ce_completed_send_next(ce_state, &transfer_context,
616 &ce_data, &nbytes,
617 &transfer_id) == 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300618 compl = get_free_compl(pipe_info);
619 if (!compl)
620 break;
621
Michal Kaziorf9d8fec2013-08-13 07:54:56 +0200622 compl->state = ATH10K_PCI_COMPL_SEND;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300623 compl->ce_state = ce_state;
624 compl->pipe_info = pipe_info;
Kalle Valoaa5c1db2013-09-01 10:01:46 +0300625 compl->skb = transfer_context;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300626 compl->nbytes = nbytes;
627 compl->transfer_id = transfer_id;
628 compl->flags = 0;
629
630 /*
631 * Add the completion to the processing queue.
632 */
633 spin_lock_bh(&ar_pci->compl_lock);
634 list_add_tail(&compl->list, &ar_pci->compl_process);
635 spin_unlock_bh(&ar_pci->compl_lock);
Michal Kazior5440ce22013-09-03 15:09:58 +0200636 }
Kalle Valo5e3dd152013-06-12 20:52:10 +0300637
638 ath10k_pci_process_ce(ar);
639}
640
641/* Called by lower (CE) layer when data is received from the Target. */
Michal Kazior5440ce22013-09-03 15:09:58 +0200642static void ath10k_pci_ce_recv_data(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300643{
644 struct ath10k *ar = ce_state->ar;
645 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +0200646 struct ath10k_pci_pipe *pipe_info = &ar_pci->pipe_info[ce_state->id];
Kalle Valo5e3dd152013-06-12 20:52:10 +0300647 struct ath10k_pci_compl *compl;
648 struct sk_buff *skb;
Michal Kazior5440ce22013-09-03 15:09:58 +0200649 void *transfer_context;
650 u32 ce_data;
651 unsigned int nbytes;
652 unsigned int transfer_id;
653 unsigned int flags;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300654
Michal Kazior5440ce22013-09-03 15:09:58 +0200655 while (ath10k_ce_completed_recv_next(ce_state, &transfer_context,
656 &ce_data, &nbytes, &transfer_id,
657 &flags) == 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300658 compl = get_free_compl(pipe_info);
659 if (!compl)
660 break;
661
Michal Kaziorf9d8fec2013-08-13 07:54:56 +0200662 compl->state = ATH10K_PCI_COMPL_RECV;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300663 compl->ce_state = ce_state;
664 compl->pipe_info = pipe_info;
Kalle Valoaa5c1db2013-09-01 10:01:46 +0300665 compl->skb = transfer_context;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300666 compl->nbytes = nbytes;
667 compl->transfer_id = transfer_id;
668 compl->flags = flags;
669
670 skb = transfer_context;
671 dma_unmap_single(ar->dev, ATH10K_SKB_CB(skb)->paddr,
672 skb->len + skb_tailroom(skb),
673 DMA_FROM_DEVICE);
674 /*
675 * Add the completion to the processing queue.
676 */
677 spin_lock_bh(&ar_pci->compl_lock);
678 list_add_tail(&compl->list, &ar_pci->compl_process);
679 spin_unlock_bh(&ar_pci->compl_lock);
Michal Kazior5440ce22013-09-03 15:09:58 +0200680 }
Kalle Valo5e3dd152013-06-12 20:52:10 +0300681
682 ath10k_pci_process_ce(ar);
683}
684
685/* Send the first nbytes bytes of the buffer */
686static int ath10k_pci_hif_send_head(struct ath10k *ar, u8 pipe_id,
687 unsigned int transfer_id,
688 unsigned int bytes, struct sk_buff *nbuf)
689{
690 struct ath10k_skb_cb *skb_cb = ATH10K_SKB_CB(nbuf);
691 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +0200692 struct ath10k_pci_pipe *pipe_info = &(ar_pci->pipe_info[pipe_id]);
Michal Kazior2aa39112013-08-27 13:08:02 +0200693 struct ath10k_ce_pipe *ce_hdl = pipe_info->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300694 unsigned int len;
695 u32 flags = 0;
696 int ret;
697
Kalle Valo5e3dd152013-06-12 20:52:10 +0300698 len = min(bytes, nbuf->len);
699 bytes -= len;
700
701 if (len & 3)
702 ath10k_warn("skb not aligned to 4-byte boundary (%d)\n", len);
703
704 ath10k_dbg(ATH10K_DBG_PCI,
705 "pci send data vaddr %p paddr 0x%llx len %d as %d bytes\n",
706 nbuf->data, (unsigned long long) skb_cb->paddr,
707 nbuf->len, len);
708 ath10k_dbg_dump(ATH10K_DBG_PCI_DUMP, NULL,
709 "ath10k tx: data: ",
710 nbuf->data, nbuf->len);
711
Michal Kazior2e761b52013-10-02 11:03:40 +0200712 ret = ath10k_ce_send(ce_hdl, nbuf, skb_cb->paddr, len, transfer_id,
713 flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300714 if (ret)
715 ath10k_warn("CE send failed: %p\n", nbuf);
716
717 return ret;
718}
719
720static u16 ath10k_pci_hif_get_free_queue_number(struct ath10k *ar, u8 pipe)
721{
722 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior3efcb3b2013-10-02 11:03:41 +0200723 return ath10k_ce_num_free_src_entries(ar_pci->pipe_info[pipe].ce_hdl);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300724}
725
726static void ath10k_pci_hif_dump_area(struct ath10k *ar)
727{
728 u32 reg_dump_area = 0;
729 u32 reg_dump_values[REG_DUMP_COUNT_QCA988X] = {};
730 u32 host_addr;
731 int ret;
732 u32 i;
733
734 ath10k_err("firmware crashed!\n");
735 ath10k_err("hardware name %s version 0x%x\n",
736 ar->hw_params.name, ar->target_version);
737 ath10k_err("firmware version: %u.%u.%u.%u\n", ar->fw_version_major,
738 ar->fw_version_minor, ar->fw_version_release,
739 ar->fw_version_build);
740
741 host_addr = host_interest_item_address(HI_ITEM(hi_failure_state));
742 if (ath10k_pci_diag_read_mem(ar, host_addr,
743 &reg_dump_area, sizeof(u32)) != 0) {
744 ath10k_warn("could not read hi_failure_state\n");
745 return;
746 }
747
748 ath10k_err("target register Dump Location: 0x%08X\n", reg_dump_area);
749
750 ret = ath10k_pci_diag_read_mem(ar, reg_dump_area,
751 &reg_dump_values[0],
752 REG_DUMP_COUNT_QCA988X * sizeof(u32));
753 if (ret != 0) {
754 ath10k_err("could not dump FW Dump Area\n");
755 return;
756 }
757
758 BUILD_BUG_ON(REG_DUMP_COUNT_QCA988X % 4);
759
760 ath10k_err("target Register Dump\n");
761 for (i = 0; i < REG_DUMP_COUNT_QCA988X; i += 4)
762 ath10k_err("[%02d]: 0x%08X 0x%08X 0x%08X 0x%08X\n",
763 i,
764 reg_dump_values[i],
765 reg_dump_values[i + 1],
766 reg_dump_values[i + 2],
767 reg_dump_values[i + 3]);
Michal Kazioraffd3212013-07-16 09:54:35 +0200768
Michal Kazior5e90de82013-10-16 16:46:05 +0300769 queue_work(ar->workqueue, &ar->restart_work);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300770}
771
772static void ath10k_pci_hif_send_complete_check(struct ath10k *ar, u8 pipe,
773 int force)
774{
775 if (!force) {
776 int resources;
777 /*
778 * Decide whether to actually poll for completions, or just
779 * wait for a later chance.
780 * If there seem to be plenty of resources left, then just wait
781 * since checking involves reading a CE register, which is a
782 * relatively expensive operation.
783 */
784 resources = ath10k_pci_hif_get_free_queue_number(ar, pipe);
785
786 /*
787 * If at least 50% of the total resources are still available,
788 * don't bother checking again yet.
789 */
790 if (resources > (host_ce_config_wlan[pipe].src_nentries >> 1))
791 return;
792 }
793 ath10k_ce_per_engine_service(ar, pipe);
794}
795
Michal Kaziore799bbf2013-07-05 16:15:12 +0300796static void ath10k_pci_hif_set_callbacks(struct ath10k *ar,
797 struct ath10k_hif_cb *callbacks)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300798{
799 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
800
801 ath10k_dbg(ATH10K_DBG_PCI, "%s\n", __func__);
802
803 memcpy(&ar_pci->msg_callbacks_current, callbacks,
804 sizeof(ar_pci->msg_callbacks_current));
805}
806
807static int ath10k_pci_start_ce(struct ath10k *ar)
808{
809 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2aa39112013-08-27 13:08:02 +0200810 struct ath10k_ce_pipe *ce_diag = ar_pci->ce_diag;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300811 const struct ce_attr *attr;
Michal Kazior87263e52013-08-27 13:08:01 +0200812 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300813 struct ath10k_pci_compl *compl;
814 int i, pipe_num, completions, disable_interrupts;
815
816 spin_lock_init(&ar_pci->compl_lock);
817 INIT_LIST_HEAD(&ar_pci->compl_process);
818
Michal Kaziorfad6ed72013-11-08 08:01:23 +0100819 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300820 pipe_info = &ar_pci->pipe_info[pipe_num];
821
822 spin_lock_init(&pipe_info->pipe_lock);
823 INIT_LIST_HEAD(&pipe_info->compl_free);
824
825 /* Handle Diagnostic CE specially */
826 if (pipe_info->ce_hdl == ce_diag)
827 continue;
828
829 attr = &host_ce_config_wlan[pipe_num];
830 completions = 0;
831
832 if (attr->src_nentries) {
833 disable_interrupts = attr->flags & CE_ATTR_DIS_INTR;
834 ath10k_ce_send_cb_register(pipe_info->ce_hdl,
835 ath10k_pci_ce_send_done,
836 disable_interrupts);
837 completions += attr->src_nentries;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300838 }
839
840 if (attr->dest_nentries) {
841 ath10k_ce_recv_cb_register(pipe_info->ce_hdl,
842 ath10k_pci_ce_recv_data);
843 completions += attr->dest_nentries;
844 }
845
846 if (completions == 0)
847 continue;
848
849 for (i = 0; i < completions; i++) {
Michal Kaziorffe5daa2013-08-13 07:54:55 +0200850 compl = kmalloc(sizeof(*compl), GFP_KERNEL);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300851 if (!compl) {
852 ath10k_warn("No memory for completion state\n");
853 ath10k_pci_stop_ce(ar);
854 return -ENOMEM;
855 }
856
Michal Kaziorf9d8fec2013-08-13 07:54:56 +0200857 compl->state = ATH10K_PCI_COMPL_FREE;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300858 list_add_tail(&compl->list, &pipe_info->compl_free);
859 }
860 }
861
862 return 0;
863}
864
Michal Kazior96a9d0d2013-11-08 08:01:25 +0100865static void ath10k_pci_kill_tasklet(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300866{
867 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300868 int i;
869
Kalle Valo5e3dd152013-06-12 20:52:10 +0300870 tasklet_kill(&ar_pci->intr_tq);
Michal Kazior103d4f52013-11-08 08:01:24 +0100871 tasklet_kill(&ar_pci->msi_fw_err);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300872
873 for (i = 0; i < CE_COUNT; i++)
874 tasklet_kill(&ar_pci->pipe_info[i].intr);
Michal Kazior96a9d0d2013-11-08 08:01:25 +0100875}
876
877static void ath10k_pci_stop_ce(struct ath10k *ar)
878{
879 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
880 struct ath10k_pci_compl *compl;
881 struct sk_buff *skb;
Michal Kazior28642f42013-11-08 08:01:31 +0100882 int ret;
Michal Kazior96a9d0d2013-11-08 08:01:25 +0100883
Michal Kazior28642f42013-11-08 08:01:31 +0100884 ret = ath10k_ce_disable_interrupts(ar);
885 if (ret)
886 ath10k_warn("failed to disable CE interrupts: %d\n", ret);
887
Michal Kazior96a9d0d2013-11-08 08:01:25 +0100888 ath10k_pci_kill_tasklet(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300889
890 /* Mark pending completions as aborted, so that upper layers free up
891 * their associated resources */
892 spin_lock_bh(&ar_pci->compl_lock);
893 list_for_each_entry(compl, &ar_pci->compl_process, list) {
Kalle Valoaa5c1db2013-09-01 10:01:46 +0300894 skb = compl->skb;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300895 ATH10K_SKB_CB(skb)->is_aborted = true;
896 }
897 spin_unlock_bh(&ar_pci->compl_lock);
898}
899
900static void ath10k_pci_cleanup_ce(struct ath10k *ar)
901{
902 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
903 struct ath10k_pci_compl *compl, *tmp;
Michal Kazior87263e52013-08-27 13:08:01 +0200904 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300905 struct sk_buff *netbuf;
906 int pipe_num;
907
908 /* Free pending completions. */
909 spin_lock_bh(&ar_pci->compl_lock);
910 if (!list_empty(&ar_pci->compl_process))
911 ath10k_warn("pending completions still present! possible memory leaks.\n");
912
913 list_for_each_entry_safe(compl, tmp, &ar_pci->compl_process, list) {
914 list_del(&compl->list);
Kalle Valoaa5c1db2013-09-01 10:01:46 +0300915 netbuf = compl->skb;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300916 dev_kfree_skb_any(netbuf);
917 kfree(compl);
918 }
919 spin_unlock_bh(&ar_pci->compl_lock);
920
921 /* Free unused completions for each pipe. */
Michal Kaziorfad6ed72013-11-08 08:01:23 +0100922 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300923 pipe_info = &ar_pci->pipe_info[pipe_num];
924
925 spin_lock_bh(&pipe_info->pipe_lock);
926 list_for_each_entry_safe(compl, tmp,
927 &pipe_info->compl_free, list) {
928 list_del(&compl->list);
929 kfree(compl);
930 }
931 spin_unlock_bh(&pipe_info->pipe_lock);
932 }
933}
934
935static void ath10k_pci_process_ce(struct ath10k *ar)
936{
937 struct ath10k_pci *ar_pci = ar->hif.priv;
938 struct ath10k_hif_cb *cb = &ar_pci->msg_callbacks_current;
939 struct ath10k_pci_compl *compl;
940 struct sk_buff *skb;
941 unsigned int nbytes;
942 int ret, send_done = 0;
943
944 /* Upper layers aren't ready to handle tx/rx completions in parallel so
945 * we must serialize all completion processing. */
946
947 spin_lock_bh(&ar_pci->compl_lock);
948 if (ar_pci->compl_processing) {
949 spin_unlock_bh(&ar_pci->compl_lock);
950 return;
951 }
952 ar_pci->compl_processing = true;
953 spin_unlock_bh(&ar_pci->compl_lock);
954
955 for (;;) {
956 spin_lock_bh(&ar_pci->compl_lock);
957 if (list_empty(&ar_pci->compl_process)) {
958 spin_unlock_bh(&ar_pci->compl_lock);
959 break;
960 }
961 compl = list_first_entry(&ar_pci->compl_process,
962 struct ath10k_pci_compl, list);
963 list_del(&compl->list);
964 spin_unlock_bh(&ar_pci->compl_lock);
965
Michal Kaziorf9d8fec2013-08-13 07:54:56 +0200966 switch (compl->state) {
967 case ATH10K_PCI_COMPL_SEND:
Kalle Valo5e3dd152013-06-12 20:52:10 +0300968 cb->tx_completion(ar,
Kalle Valoaa5c1db2013-09-01 10:01:46 +0300969 compl->skb,
Kalle Valo5e3dd152013-06-12 20:52:10 +0300970 compl->transfer_id);
971 send_done = 1;
Michal Kaziorf9d8fec2013-08-13 07:54:56 +0200972 break;
973 case ATH10K_PCI_COMPL_RECV:
Kalle Valo5e3dd152013-06-12 20:52:10 +0300974 ret = ath10k_pci_post_rx_pipe(compl->pipe_info, 1);
975 if (ret) {
976 ath10k_warn("Unable to post recv buffer for pipe: %d\n",
977 compl->pipe_info->pipe_num);
978 break;
979 }
980
Kalle Valoaa5c1db2013-09-01 10:01:46 +0300981 skb = compl->skb;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300982 nbytes = compl->nbytes;
983
984 ath10k_dbg(ATH10K_DBG_PCI,
985 "ath10k_pci_ce_recv_data netbuf=%p nbytes=%d\n",
986 skb, nbytes);
987 ath10k_dbg_dump(ATH10K_DBG_PCI_DUMP, NULL,
988 "ath10k rx: ", skb->data, nbytes);
989
990 if (skb->len + skb_tailroom(skb) >= nbytes) {
991 skb_trim(skb, 0);
992 skb_put(skb, nbytes);
993 cb->rx_completion(ar, skb,
994 compl->pipe_info->pipe_num);
995 } else {
996 ath10k_warn("rxed more than expected (nbytes %d, max %d)",
997 nbytes,
998 skb->len + skb_tailroom(skb));
999 }
Michal Kaziorf9d8fec2013-08-13 07:54:56 +02001000 break;
1001 case ATH10K_PCI_COMPL_FREE:
1002 ath10k_warn("free completion cannot be processed\n");
1003 break;
1004 default:
1005 ath10k_warn("invalid completion state (%d)\n",
1006 compl->state);
1007 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001008 }
1009
Michal Kaziorf9d8fec2013-08-13 07:54:56 +02001010 compl->state = ATH10K_PCI_COMPL_FREE;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001011
1012 /*
1013 * Add completion back to the pipe's free list.
1014 */
1015 spin_lock_bh(&compl->pipe_info->pipe_lock);
1016 list_add_tail(&compl->list, &compl->pipe_info->compl_free);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001017 spin_unlock_bh(&compl->pipe_info->pipe_lock);
1018 }
1019
1020 spin_lock_bh(&ar_pci->compl_lock);
1021 ar_pci->compl_processing = false;
1022 spin_unlock_bh(&ar_pci->compl_lock);
1023}
1024
1025/* TODO - temporary mapping while we have too few CE's */
1026static int ath10k_pci_hif_map_service_to_pipe(struct ath10k *ar,
1027 u16 service_id, u8 *ul_pipe,
1028 u8 *dl_pipe, int *ul_is_polled,
1029 int *dl_is_polled)
1030{
1031 int ret = 0;
1032
1033 /* polling for received messages not supported */
1034 *dl_is_polled = 0;
1035
1036 switch (service_id) {
1037 case ATH10K_HTC_SVC_ID_HTT_DATA_MSG:
1038 /*
1039 * Host->target HTT gets its own pipe, so it can be polled
1040 * while other pipes are interrupt driven.
1041 */
1042 *ul_pipe = 4;
1043 /*
1044 * Use the same target->host pipe for HTC ctrl, HTC raw
1045 * streams, and HTT.
1046 */
1047 *dl_pipe = 1;
1048 break;
1049
1050 case ATH10K_HTC_SVC_ID_RSVD_CTRL:
1051 case ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS:
1052 /*
1053 * Note: HTC_RAW_STREAMS_SVC is currently unused, and
1054 * HTC_CTRL_RSVD_SVC could share the same pipe as the
1055 * WMI services. So, if another CE is needed, change
1056 * this to *ul_pipe = 3, which frees up CE 0.
1057 */
1058 /* *ul_pipe = 3; */
1059 *ul_pipe = 0;
1060 *dl_pipe = 1;
1061 break;
1062
1063 case ATH10K_HTC_SVC_ID_WMI_DATA_BK:
1064 case ATH10K_HTC_SVC_ID_WMI_DATA_BE:
1065 case ATH10K_HTC_SVC_ID_WMI_DATA_VI:
1066 case ATH10K_HTC_SVC_ID_WMI_DATA_VO:
1067
1068 case ATH10K_HTC_SVC_ID_WMI_CONTROL:
1069 *ul_pipe = 3;
1070 *dl_pipe = 2;
1071 break;
1072
1073 /* pipe 5 unused */
1074 /* pipe 6 reserved */
1075 /* pipe 7 reserved */
1076
1077 default:
1078 ret = -1;
1079 break;
1080 }
1081 *ul_is_polled =
1082 (host_ce_config_wlan[*ul_pipe].flags & CE_ATTR_DIS_INTR) != 0;
1083
1084 return ret;
1085}
1086
1087static void ath10k_pci_hif_get_default_pipe(struct ath10k *ar,
1088 u8 *ul_pipe, u8 *dl_pipe)
1089{
1090 int ul_is_polled, dl_is_polled;
1091
1092 (void)ath10k_pci_hif_map_service_to_pipe(ar,
1093 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1094 ul_pipe,
1095 dl_pipe,
1096 &ul_is_polled,
1097 &dl_is_polled);
1098}
1099
Michal Kazior87263e52013-08-27 13:08:01 +02001100static int ath10k_pci_post_rx_pipe(struct ath10k_pci_pipe *pipe_info,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001101 int num)
1102{
1103 struct ath10k *ar = pipe_info->hif_ce_state;
1104 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2aa39112013-08-27 13:08:02 +02001105 struct ath10k_ce_pipe *ce_state = pipe_info->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001106 struct sk_buff *skb;
1107 dma_addr_t ce_data;
1108 int i, ret = 0;
1109
1110 if (pipe_info->buf_sz == 0)
1111 return 0;
1112
1113 for (i = 0; i < num; i++) {
1114 skb = dev_alloc_skb(pipe_info->buf_sz);
1115 if (!skb) {
1116 ath10k_warn("could not allocate skbuff for pipe %d\n",
1117 num);
1118 ret = -ENOMEM;
1119 goto err;
1120 }
1121
1122 WARN_ONCE((unsigned long)skb->data & 3, "unaligned skb");
1123
1124 ce_data = dma_map_single(ar->dev, skb->data,
1125 skb->len + skb_tailroom(skb),
1126 DMA_FROM_DEVICE);
1127
1128 if (unlikely(dma_mapping_error(ar->dev, ce_data))) {
1129 ath10k_warn("could not dma map skbuff\n");
1130 dev_kfree_skb_any(skb);
1131 ret = -EIO;
1132 goto err;
1133 }
1134
1135 ATH10K_SKB_CB(skb)->paddr = ce_data;
1136
1137 pci_dma_sync_single_for_device(ar_pci->pdev, ce_data,
1138 pipe_info->buf_sz,
1139 PCI_DMA_FROMDEVICE);
1140
1141 ret = ath10k_ce_recv_buf_enqueue(ce_state, (void *)skb,
1142 ce_data);
1143 if (ret) {
1144 ath10k_warn("could not enqueue to pipe %d (%d)\n",
1145 num, ret);
1146 goto err;
1147 }
1148 }
1149
1150 return ret;
1151
1152err:
1153 ath10k_pci_rx_pipe_cleanup(pipe_info);
1154 return ret;
1155}
1156
1157static int ath10k_pci_post_rx(struct ath10k *ar)
1158{
1159 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +02001160 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001161 const struct ce_attr *attr;
1162 int pipe_num, ret = 0;
1163
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001164 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001165 pipe_info = &ar_pci->pipe_info[pipe_num];
1166 attr = &host_ce_config_wlan[pipe_num];
1167
1168 if (attr->dest_nentries == 0)
1169 continue;
1170
1171 ret = ath10k_pci_post_rx_pipe(pipe_info,
1172 attr->dest_nentries - 1);
1173 if (ret) {
1174 ath10k_warn("Unable to replenish recv buffers for pipe: %d\n",
1175 pipe_num);
1176
1177 for (; pipe_num >= 0; pipe_num--) {
1178 pipe_info = &ar_pci->pipe_info[pipe_num];
1179 ath10k_pci_rx_pipe_cleanup(pipe_info);
1180 }
1181 return ret;
1182 }
1183 }
1184
1185 return 0;
1186}
1187
1188static int ath10k_pci_hif_start(struct ath10k *ar)
1189{
1190 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1191 int ret;
1192
1193 ret = ath10k_pci_start_ce(ar);
1194 if (ret) {
1195 ath10k_warn("could not start CE (%d)\n", ret);
1196 return ret;
1197 }
1198
1199 /* Post buffers once to start things off. */
1200 ret = ath10k_pci_post_rx(ar);
1201 if (ret) {
1202 ath10k_warn("could not post rx pipes (%d)\n", ret);
1203 return ret;
1204 }
1205
1206 ar_pci->started = 1;
1207 return 0;
1208}
1209
Michal Kazior87263e52013-08-27 13:08:01 +02001210static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe *pipe_info)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001211{
1212 struct ath10k *ar;
1213 struct ath10k_pci *ar_pci;
Michal Kazior2aa39112013-08-27 13:08:02 +02001214 struct ath10k_ce_pipe *ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001215 u32 buf_sz;
1216 struct sk_buff *netbuf;
1217 u32 ce_data;
1218
1219 buf_sz = pipe_info->buf_sz;
1220
1221 /* Unused Copy Engine */
1222 if (buf_sz == 0)
1223 return;
1224
1225 ar = pipe_info->hif_ce_state;
1226 ar_pci = ath10k_pci_priv(ar);
1227
1228 if (!ar_pci->started)
1229 return;
1230
1231 ce_hdl = pipe_info->ce_hdl;
1232
1233 while (ath10k_ce_revoke_recv_next(ce_hdl, (void **)&netbuf,
1234 &ce_data) == 0) {
1235 dma_unmap_single(ar->dev, ATH10K_SKB_CB(netbuf)->paddr,
1236 netbuf->len + skb_tailroom(netbuf),
1237 DMA_FROM_DEVICE);
1238 dev_kfree_skb_any(netbuf);
1239 }
1240}
1241
Michal Kazior87263e52013-08-27 13:08:01 +02001242static void ath10k_pci_tx_pipe_cleanup(struct ath10k_pci_pipe *pipe_info)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001243{
1244 struct ath10k *ar;
1245 struct ath10k_pci *ar_pci;
Michal Kazior2aa39112013-08-27 13:08:02 +02001246 struct ath10k_ce_pipe *ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001247 struct sk_buff *netbuf;
1248 u32 ce_data;
1249 unsigned int nbytes;
1250 unsigned int id;
1251 u32 buf_sz;
1252
1253 buf_sz = pipe_info->buf_sz;
1254
1255 /* Unused Copy Engine */
1256 if (buf_sz == 0)
1257 return;
1258
1259 ar = pipe_info->hif_ce_state;
1260 ar_pci = ath10k_pci_priv(ar);
1261
1262 if (!ar_pci->started)
1263 return;
1264
1265 ce_hdl = pipe_info->ce_hdl;
1266
1267 while (ath10k_ce_cancel_send_next(ce_hdl, (void **)&netbuf,
1268 &ce_data, &nbytes, &id) == 0) {
Kalle Valoe9bb0aa2013-09-08 18:36:11 +03001269 /*
1270 * Indicate the completion to higer layer to free
1271 * the buffer
1272 */
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
Kalle Valo5e3dd152013-06-12 20:52:10 +03001789 return 0;
1790}
1791
1792static void ath10k_pci_fw_interrupt_handler(struct ath10k *ar)
1793{
1794 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1795 u32 fw_indicator_address, fw_indicator;
1796
1797 ath10k_pci_wake(ar);
1798
1799 fw_indicator_address = ar_pci->fw_indicator_address;
1800 fw_indicator = ath10k_pci_read32(ar, fw_indicator_address);
1801
1802 if (fw_indicator & FW_IND_EVENT_PENDING) {
1803 /* ACK: clear Target-side pending event */
1804 ath10k_pci_write32(ar, fw_indicator_address,
1805 fw_indicator & ~FW_IND_EVENT_PENDING);
1806
1807 if (ar_pci->started) {
1808 ath10k_pci_hif_dump_area(ar);
1809 } else {
1810 /*
1811 * Probable Target failure before we're prepared
1812 * to handle it. Generally unexpected.
1813 */
1814 ath10k_warn("early firmware event indicated\n");
1815 }
1816 }
1817
1818 ath10k_pci_sleep(ar);
1819}
1820
Michal Kazior98563d52013-11-08 08:01:33 +01001821static void ath10k_pci_start_bmi(struct ath10k *ar)
1822{
1823 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1824 struct ath10k_pci_pipe *pipe;
1825
1826 /*
1827 * Initially, establish CE completion handlers for use with BMI.
1828 * These are overwritten with generic handlers after we exit BMI phase.
1829 */
1830 pipe = &ar_pci->pipe_info[BMI_CE_NUM_TO_TARG];
1831 ath10k_ce_send_cb_register(pipe->ce_hdl, ath10k_pci_bmi_send_done, 0);
1832
1833 pipe = &ar_pci->pipe_info[BMI_CE_NUM_TO_HOST];
1834 ath10k_ce_recv_cb_register(pipe->ce_hdl, ath10k_pci_bmi_recv_data);
1835}
1836
Michal Kazior8c5c5362013-07-16 09:38:50 +02001837static int ath10k_pci_hif_power_up(struct ath10k *ar)
1838{
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02001839 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001840 int ret;
1841
1842 /*
1843 * Bring the target up cleanly.
1844 *
1845 * The target may be in an undefined state with an AUX-powered Target
1846 * and a Host in WoW mode. If the Host crashes, loses power, or is
1847 * restarted (without unloading the driver) then the Target is left
1848 * (aux) powered and running. On a subsequent driver load, the Target
1849 * is in an unexpected state. We try to catch that here in order to
1850 * reset the Target and retry the probe.
1851 */
Michal Kazior5b2589f2013-11-08 08:01:30 +01001852 ret = ath10k_pci_device_reset(ar);
1853 if (ret) {
1854 ath10k_err("failed to reset target: %d\n", ret);
Michal Kazior98563d52013-11-08 08:01:33 +01001855 goto err;
Michal Kazior5b2589f2013-11-08 08:01:30 +01001856 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02001857
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02001858 if (!test_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE, ar_pci->features))
Michal Kazior8c5c5362013-07-16 09:38:50 +02001859 /* Force AWAKE forever */
Michal Kazior8c5c5362013-07-16 09:38:50 +02001860 ath10k_do_pci_wake(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001861
1862 ret = ath10k_pci_ce_init(ar);
1863 if (ret)
1864 goto err_ps;
1865
Michal Kazior98563d52013-11-08 08:01:33 +01001866 ret = ath10k_ce_disable_interrupts(ar);
1867 if (ret) {
1868 ath10k_err("failed to disable CE interrupts: %d\n", ret);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001869 goto err_ce;
Michal Kazior98563d52013-11-08 08:01:33 +01001870 }
1871
1872 ret = ath10k_pci_start_intr(ar);
1873 if (ret) {
1874 ath10k_err("failed to start interrupt handling: %d\n", ret);
1875 goto err_ce;
1876 }
1877
1878 ret = ath10k_pci_wait_for_target_init(ar);
1879 if (ret) {
1880 ath10k_err("failed to wait for target to init: %d\n", ret);
1881 goto err_irq;
1882 }
1883
1884 ret = ath10k_ce_enable_err_irq(ar);
1885 if (ret) {
1886 ath10k_err("failed to enable CE error irq: %d\n", ret);
1887 goto err_irq;
1888 }
1889
1890 ret = ath10k_pci_init_config(ar);
1891 if (ret) {
1892 ath10k_err("failed to setup init config: %d\n", ret);
1893 goto err_irq;
1894 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02001895
1896 ret = ath10k_pci_wake_target_cpu(ar);
1897 if (ret) {
1898 ath10k_err("could not wake up target CPU (%d)\n", ret);
Michal Kazior98563d52013-11-08 08:01:33 +01001899 goto err_irq;
Michal Kazior8c5c5362013-07-16 09:38:50 +02001900 }
1901
Michal Kazior98563d52013-11-08 08:01:33 +01001902 ath10k_pci_start_bmi(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001903 return 0;
1904
Michal Kazior98563d52013-11-08 08:01:33 +01001905err_irq:
1906 ath10k_ce_disable_interrupts(ar);
1907 ath10k_pci_stop_intr(ar);
1908 ath10k_pci_kill_tasklet(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001909err_ce:
1910 ath10k_pci_ce_deinit(ar);
1911err_ps:
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02001912 if (!test_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE, ar_pci->features))
Michal Kazior8c5c5362013-07-16 09:38:50 +02001913 ath10k_do_pci_sleep(ar);
1914err:
1915 return ret;
1916}
1917
1918static void ath10k_pci_hif_power_down(struct ath10k *ar)
1919{
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02001920 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1921
Michal Kazior32270b62013-08-02 09:15:47 +02001922 ath10k_pci_stop_intr(ar);
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02001923
Michal Kazior8c5c5362013-07-16 09:38:50 +02001924 ath10k_pci_ce_deinit(ar);
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02001925 if (!test_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE, ar_pci->features))
Michal Kazior8c5c5362013-07-16 09:38:50 +02001926 ath10k_do_pci_sleep(ar);
1927}
1928
Michal Kazior8cd13ca2013-07-16 09:38:54 +02001929#ifdef CONFIG_PM
1930
1931#define ATH10K_PCI_PM_CONTROL 0x44
1932
1933static int ath10k_pci_hif_suspend(struct ath10k *ar)
1934{
1935 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1936 struct pci_dev *pdev = ar_pci->pdev;
1937 u32 val;
1938
1939 pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
1940
1941 if ((val & 0x000000ff) != 0x3) {
1942 pci_save_state(pdev);
1943 pci_disable_device(pdev);
1944 pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
1945 (val & 0xffffff00) | 0x03);
1946 }
1947
1948 return 0;
1949}
1950
1951static int ath10k_pci_hif_resume(struct ath10k *ar)
1952{
1953 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1954 struct pci_dev *pdev = ar_pci->pdev;
1955 u32 val;
1956
1957 pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
1958
1959 if ((val & 0x000000ff) != 0) {
1960 pci_restore_state(pdev);
1961 pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
1962 val & 0xffffff00);
1963 /*
1964 * Suspend/Resume resets the PCI configuration space,
1965 * so we have to re-disable the RETRY_TIMEOUT register (0x41)
1966 * to keep PCI Tx retries from interfering with C3 CPU state
1967 */
1968 pci_read_config_dword(pdev, 0x40, &val);
1969
1970 if ((val & 0x0000ff00) != 0)
1971 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
1972 }
1973
1974 return 0;
1975}
1976#endif
1977
Kalle Valo5e3dd152013-06-12 20:52:10 +03001978static const struct ath10k_hif_ops ath10k_pci_hif_ops = {
1979 .send_head = ath10k_pci_hif_send_head,
1980 .exchange_bmi_msg = ath10k_pci_hif_exchange_bmi_msg,
1981 .start = ath10k_pci_hif_start,
1982 .stop = ath10k_pci_hif_stop,
1983 .map_service_to_pipe = ath10k_pci_hif_map_service_to_pipe,
1984 .get_default_pipe = ath10k_pci_hif_get_default_pipe,
1985 .send_complete_check = ath10k_pci_hif_send_complete_check,
Michal Kaziore799bbf2013-07-05 16:15:12 +03001986 .set_callbacks = ath10k_pci_hif_set_callbacks,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001987 .get_free_queue_number = ath10k_pci_hif_get_free_queue_number,
Michal Kazior8c5c5362013-07-16 09:38:50 +02001988 .power_up = ath10k_pci_hif_power_up,
1989 .power_down = ath10k_pci_hif_power_down,
Michal Kazior8cd13ca2013-07-16 09:38:54 +02001990#ifdef CONFIG_PM
1991 .suspend = ath10k_pci_hif_suspend,
1992 .resume = ath10k_pci_hif_resume,
1993#endif
Kalle Valo5e3dd152013-06-12 20:52:10 +03001994};
1995
1996static void ath10k_pci_ce_tasklet(unsigned long ptr)
1997{
Michal Kazior87263e52013-08-27 13:08:01 +02001998 struct ath10k_pci_pipe *pipe = (struct ath10k_pci_pipe *)ptr;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001999 struct ath10k_pci *ar_pci = pipe->ar_pci;
2000
2001 ath10k_ce_per_engine_service(ar_pci->ar, pipe->pipe_num);
2002}
2003
2004static void ath10k_msi_err_tasklet(unsigned long data)
2005{
2006 struct ath10k *ar = (struct ath10k *)data;
2007
2008 ath10k_pci_fw_interrupt_handler(ar);
2009}
2010
2011/*
2012 * Handler for a per-engine interrupt on a PARTICULAR CE.
2013 * This is used in cases where each CE has a private MSI interrupt.
2014 */
2015static irqreturn_t ath10k_pci_per_engine_handler(int irq, void *arg)
2016{
2017 struct ath10k *ar = arg;
2018 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2019 int ce_id = irq - ar_pci->pdev->irq - MSI_ASSIGN_CE_INITIAL;
2020
Dan Carpentere5742672013-06-18 10:28:46 +03002021 if (ce_id < 0 || ce_id >= ARRAY_SIZE(ar_pci->pipe_info)) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03002022 ath10k_warn("unexpected/invalid irq %d ce_id %d\n", irq, ce_id);
2023 return IRQ_HANDLED;
2024 }
2025
2026 /*
2027 * NOTE: We are able to derive ce_id from irq because we
2028 * use a one-to-one mapping for CE's 0..5.
2029 * CE's 6 & 7 do not use interrupts at all.
2030 *
2031 * This mapping must be kept in sync with the mapping
2032 * used by firmware.
2033 */
2034 tasklet_schedule(&ar_pci->pipe_info[ce_id].intr);
2035 return IRQ_HANDLED;
2036}
2037
2038static irqreturn_t ath10k_pci_msi_fw_handler(int irq, void *arg)
2039{
2040 struct ath10k *ar = arg;
2041 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2042
2043 tasklet_schedule(&ar_pci->msi_fw_err);
2044 return IRQ_HANDLED;
2045}
2046
2047/*
2048 * Top-level interrupt handler for all PCI interrupts from a Target.
2049 * When a block of MSI interrupts is allocated, this top-level handler
2050 * is not used; instead, we directly call the correct sub-handler.
2051 */
2052static irqreturn_t ath10k_pci_interrupt_handler(int irq, void *arg)
2053{
2054 struct ath10k *ar = arg;
2055 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2056
2057 if (ar_pci->num_msi_intrs == 0) {
2058 /*
2059 * IMPORTANT: INTR_CLR regiser has to be set after
2060 * INTR_ENABLE is set to 0, otherwise interrupt can not be
2061 * really cleared.
2062 */
2063 iowrite32(0, ar_pci->mem +
2064 (SOC_CORE_BASE_ADDRESS |
2065 PCIE_INTR_ENABLE_ADDRESS));
2066 iowrite32(PCIE_INTR_FIRMWARE_MASK |
2067 PCIE_INTR_CE_MASK_ALL,
2068 ar_pci->mem + (SOC_CORE_BASE_ADDRESS |
2069 PCIE_INTR_CLR_ADDRESS));
2070 /*
2071 * IMPORTANT: this extra read transaction is required to
2072 * flush the posted write buffer.
2073 */
2074 (void) ioread32(ar_pci->mem +
2075 (SOC_CORE_BASE_ADDRESS |
2076 PCIE_INTR_ENABLE_ADDRESS));
2077 }
2078
2079 tasklet_schedule(&ar_pci->intr_tq);
2080
2081 return IRQ_HANDLED;
2082}
2083
2084static void ath10k_pci_tasklet(unsigned long data)
2085{
2086 struct ath10k *ar = (struct ath10k *)data;
2087 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2088
2089 ath10k_pci_fw_interrupt_handler(ar); /* FIXME: Handle FW error */
2090 ath10k_ce_per_engine_service_any(ar);
2091
2092 if (ar_pci->num_msi_intrs == 0) {
2093 /* Enable Legacy PCI line interrupts */
2094 iowrite32(PCIE_INTR_FIRMWARE_MASK |
2095 PCIE_INTR_CE_MASK_ALL,
2096 ar_pci->mem + (SOC_CORE_BASE_ADDRESS |
2097 PCIE_INTR_ENABLE_ADDRESS));
2098 /*
2099 * IMPORTANT: this extra read transaction is required to
2100 * flush the posted write buffer
2101 */
2102 (void) ioread32(ar_pci->mem +
2103 (SOC_CORE_BASE_ADDRESS |
2104 PCIE_INTR_ENABLE_ADDRESS));
2105 }
2106}
2107
2108static int ath10k_pci_start_intr_msix(struct ath10k *ar, int num)
2109{
2110 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2111 int ret;
2112 int i;
2113
2114 ret = pci_enable_msi_block(ar_pci->pdev, num);
2115 if (ret)
2116 return ret;
2117
2118 ret = request_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW,
2119 ath10k_pci_msi_fw_handler,
2120 IRQF_SHARED, "ath10k_pci", ar);
Michal Kazior591ecdb2013-07-31 10:55:15 +02002121 if (ret) {
2122 ath10k_warn("request_irq(%d) failed %d\n",
2123 ar_pci->pdev->irq + MSI_ASSIGN_FW, ret);
2124
2125 pci_disable_msi(ar_pci->pdev);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002126 return ret;
Michal Kazior591ecdb2013-07-31 10:55:15 +02002127 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002128
2129 for (i = MSI_ASSIGN_CE_INITIAL; i <= MSI_ASSIGN_CE_MAX; i++) {
2130 ret = request_irq(ar_pci->pdev->irq + i,
2131 ath10k_pci_per_engine_handler,
2132 IRQF_SHARED, "ath10k_pci", ar);
2133 if (ret) {
2134 ath10k_warn("request_irq(%d) failed %d\n",
2135 ar_pci->pdev->irq + i, ret);
2136
Michal Kazior87b14232013-06-26 08:50:50 +02002137 for (i--; i >= MSI_ASSIGN_CE_INITIAL; i--)
2138 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002139
Michal Kazior87b14232013-06-26 08:50:50 +02002140 free_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002141 pci_disable_msi(ar_pci->pdev);
2142 return ret;
2143 }
2144 }
2145
2146 ath10k_info("MSI-X interrupt handling (%d intrs)\n", num);
2147 return 0;
2148}
2149
2150static int ath10k_pci_start_intr_msi(struct ath10k *ar)
2151{
2152 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2153 int ret;
2154
2155 ret = pci_enable_msi(ar_pci->pdev);
2156 if (ret < 0)
2157 return ret;
2158
2159 ret = request_irq(ar_pci->pdev->irq,
2160 ath10k_pci_interrupt_handler,
2161 IRQF_SHARED, "ath10k_pci", ar);
2162 if (ret < 0) {
2163 pci_disable_msi(ar_pci->pdev);
2164 return ret;
2165 }
2166
2167 ath10k_info("MSI interrupt handling\n");
2168 return 0;
2169}
2170
2171static int ath10k_pci_start_intr_legacy(struct ath10k *ar)
2172{
2173 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2174 int ret;
2175
2176 ret = request_irq(ar_pci->pdev->irq,
2177 ath10k_pci_interrupt_handler,
2178 IRQF_SHARED, "ath10k_pci", ar);
2179 if (ret < 0)
2180 return ret;
2181
Michal Kazior98563d52013-11-08 08:01:33 +01002182 ret = ath10k_pci_wake(ar);
Kalle Valof3782742013-10-17 11:36:15 +03002183 if (ret) {
Kalle Valof3782742013-10-17 11:36:15 +03002184 free_irq(ar_pci->pdev->irq, ar);
Michal Kazior5b2589f2013-11-08 08:01:30 +01002185 ath10k_err("failed to wake up target: %d\n", ret);
Kalle Valof3782742013-10-17 11:36:15 +03002186 return ret;
2187 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002188
2189 /*
2190 * A potential race occurs here: The CORE_BASE write
2191 * depends on target correctly decoding AXI address but
2192 * host won't know when target writes BAR to CORE_CTRL.
2193 * This write might get lost if target has NOT written BAR.
2194 * For now, fix the race by repeating the write in below
2195 * synchronization checking.
2196 */
2197 iowrite32(PCIE_INTR_FIRMWARE_MASK |
2198 PCIE_INTR_CE_MASK_ALL,
2199 ar_pci->mem + (SOC_CORE_BASE_ADDRESS |
2200 PCIE_INTR_ENABLE_ADDRESS));
Kalle Valo5e3dd152013-06-12 20:52:10 +03002201
Michal Kazior98563d52013-11-08 08:01:33 +01002202 ath10k_pci_sleep(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002203 ath10k_info("legacy interrupt handling\n");
2204 return 0;
2205}
2206
2207static int ath10k_pci_start_intr(struct ath10k *ar)
2208{
2209 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2210 int num = MSI_NUM_REQUEST;
2211 int ret;
2212 int i;
2213
2214 tasklet_init(&ar_pci->intr_tq, ath10k_pci_tasklet, (unsigned long) ar);
2215 tasklet_init(&ar_pci->msi_fw_err, ath10k_msi_err_tasklet,
2216 (unsigned long) ar);
2217
2218 for (i = 0; i < CE_COUNT; i++) {
2219 ar_pci->pipe_info[i].ar_pci = ar_pci;
2220 tasklet_init(&ar_pci->pipe_info[i].intr,
2221 ath10k_pci_ce_tasklet,
2222 (unsigned long)&ar_pci->pipe_info[i]);
2223 }
2224
2225 if (!test_bit(ATH10K_PCI_FEATURE_MSI_X, ar_pci->features))
2226 num = 1;
2227
2228 if (num > 1) {
2229 ret = ath10k_pci_start_intr_msix(ar, num);
2230 if (ret == 0)
2231 goto exit;
2232
2233 ath10k_warn("MSI-X didn't succeed (%d), trying MSI\n", ret);
2234 num = 1;
2235 }
2236
2237 if (num == 1) {
2238 ret = ath10k_pci_start_intr_msi(ar);
2239 if (ret == 0)
2240 goto exit;
2241
2242 ath10k_warn("MSI didn't succeed (%d), trying legacy INTR\n",
2243 ret);
2244 num = 0;
2245 }
2246
2247 ret = ath10k_pci_start_intr_legacy(ar);
Kalle Valof3782742013-10-17 11:36:15 +03002248 if (ret) {
2249 ath10k_warn("Failed to start legacy interrupts: %d\n", ret);
2250 return ret;
2251 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002252
2253exit:
2254 ar_pci->num_msi_intrs = num;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002255 return ret;
2256}
2257
2258static void ath10k_pci_stop_intr(struct ath10k *ar)
2259{
2260 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2261 int i;
2262
2263 /* There's at least one interrupt irregardless whether its legacy INTR
2264 * or MSI or MSI-X */
2265 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
2266 free_irq(ar_pci->pdev->irq + i, ar);
2267
2268 if (ar_pci->num_msi_intrs > 0)
2269 pci_disable_msi(ar_pci->pdev);
2270}
2271
Michal Kaziord7fb47f2013-11-08 08:01:26 +01002272static int ath10k_pci_wait_for_target_init(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002273{
2274 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2275 int wait_limit = 300; /* 3 sec */
Kalle Valof3782742013-10-17 11:36:15 +03002276 int ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002277
Michal Kazior98563d52013-11-08 08:01:33 +01002278 ret = ath10k_pci_wake(ar);
Kalle Valof3782742013-10-17 11:36:15 +03002279 if (ret) {
Michal Kazior5b2589f2013-11-08 08:01:30 +01002280 ath10k_err("failed to wake up target: %d\n", ret);
Kalle Valof3782742013-10-17 11:36:15 +03002281 return ret;
2282 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002283
2284 while (wait_limit-- &&
2285 !(ioread32(ar_pci->mem + FW_INDICATOR_ADDRESS) &
2286 FW_IND_INITIALIZED)) {
2287 if (ar_pci->num_msi_intrs == 0)
2288 /* Fix potential race by repeating CORE_BASE writes */
2289 iowrite32(PCIE_INTR_FIRMWARE_MASK |
2290 PCIE_INTR_CE_MASK_ALL,
2291 ar_pci->mem + (SOC_CORE_BASE_ADDRESS |
2292 PCIE_INTR_ENABLE_ADDRESS));
2293 mdelay(10);
2294 }
2295
2296 if (wait_limit < 0) {
Michal Kazior5b2589f2013-11-08 08:01:30 +01002297 ath10k_err("target stalled\n");
2298 ret = -EIO;
2299 goto out;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002300 }
2301
Michal Kazior5b2589f2013-11-08 08:01:30 +01002302out:
Michal Kazior98563d52013-11-08 08:01:33 +01002303 ath10k_pci_sleep(ar);
Michal Kazior5b2589f2013-11-08 08:01:30 +01002304 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002305}
2306
Michal Kazior5b2589f2013-11-08 08:01:30 +01002307static int ath10k_pci_device_reset(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002308{
Michal Kazior5b2589f2013-11-08 08:01:30 +01002309 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002310 u32 val;
2311
Michal Kazior5b2589f2013-11-08 08:01:30 +01002312 ret = ath10k_do_pci_wake(ar);
2313 if (ret) {
2314 ath10k_err("failed to wake up target: %d\n",
2315 ret);
2316 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002317 }
2318
2319 /* Put Target, including PCIe, into RESET. */
Kalle Valoe479ed42013-09-01 10:01:53 +03002320 val = ath10k_pci_reg_read32(ar, SOC_GLOBAL_RESET_ADDRESS);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002321 val |= 1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002322 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002323
2324 for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
Kalle Valoe479ed42013-09-01 10:01:53 +03002325 if (ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03002326 RTC_STATE_COLD_RESET_MASK)
2327 break;
2328 msleep(1);
2329 }
2330
2331 /* Pull Target, including PCIe, out of RESET. */
2332 val &= ~1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002333 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002334
2335 for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
Kalle Valoe479ed42013-09-01 10:01:53 +03002336 if (!(ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03002337 RTC_STATE_COLD_RESET_MASK))
2338 break;
2339 msleep(1);
2340 }
2341
Michal Kazior5b2589f2013-11-08 08:01:30 +01002342 ath10k_do_pci_sleep(ar);
2343 return 0;
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);
Wei Yongjun12eb0872013-10-30 13:24:39 +08002477 goto err_iomap;
Kalle Valoe01ae682013-09-01 11:22:14 +03002478 }
2479
Kalle Valo233eb972013-10-16 16:46:11 +03002480 chip_id = ath10k_pci_soc_read32(ar, SOC_CHIP_ID_ADDRESS);
Kalle Valoe01ae682013-09-01 11:22:14 +03002481
2482 ath10k_do_pci_sleep(ar);
2483
Kalle Valo24cfade2013-09-08 17:55:50 +03002484 ath10k_dbg(ATH10K_DBG_BOOT, "boot pci_mem 0x%p\n", ar_pci->mem);
2485
Kalle Valoe01ae682013-09-01 11:22:14 +03002486 ret = ath10k_core_register(ar, chip_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002487 if (ret) {
2488 ath10k_err("could not register driver core (%d)\n", ret);
Michal Kazior32270b62013-08-02 09:15:47 +02002489 goto err_iomap;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002490 }
2491
2492 return 0;
2493
Kalle Valo5e3dd152013-06-12 20:52:10 +03002494err_iomap:
2495 pci_iounmap(pdev, mem);
2496err_master:
2497 pci_clear_master(pdev);
2498err_region:
2499 pci_release_region(pdev, BAR_NUM);
2500err_device:
2501 pci_disable_device(pdev);
2502err_ar:
Kalle Valo5e3dd152013-06-12 20:52:10 +03002503 ath10k_core_destroy(ar);
2504err_ar_pci:
2505 /* call HIF PCI free here */
2506 kfree(ar_pci);
2507
2508 return ret;
2509}
2510
2511static void ath10k_pci_remove(struct pci_dev *pdev)
2512{
2513 struct ath10k *ar = pci_get_drvdata(pdev);
2514 struct ath10k_pci *ar_pci;
2515
2516 ath10k_dbg(ATH10K_DBG_PCI, "%s\n", __func__);
2517
2518 if (!ar)
2519 return;
2520
2521 ar_pci = ath10k_pci_priv(ar);
2522
2523 if (!ar_pci)
2524 return;
2525
2526 tasklet_kill(&ar_pci->msi_fw_err);
2527
2528 ath10k_core_unregister(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002529
Kalle Valo5e3dd152013-06-12 20:52:10 +03002530 pci_iounmap(pdev, ar_pci->mem);
2531 pci_release_region(pdev, BAR_NUM);
2532 pci_clear_master(pdev);
2533 pci_disable_device(pdev);
2534
2535 ath10k_core_destroy(ar);
2536 kfree(ar_pci);
2537}
2538
Kalle Valo5e3dd152013-06-12 20:52:10 +03002539MODULE_DEVICE_TABLE(pci, ath10k_pci_id_table);
2540
2541static struct pci_driver ath10k_pci_driver = {
2542 .name = "ath10k_pci",
2543 .id_table = ath10k_pci_id_table,
2544 .probe = ath10k_pci_probe,
2545 .remove = ath10k_pci_remove,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002546};
2547
2548static int __init ath10k_pci_init(void)
2549{
2550 int ret;
2551
2552 ret = pci_register_driver(&ath10k_pci_driver);
2553 if (ret)
2554 ath10k_err("pci_register_driver failed [%d]\n", ret);
2555
2556 return ret;
2557}
2558module_init(ath10k_pci_init);
2559
2560static void __exit ath10k_pci_exit(void)
2561{
2562 pci_unregister_driver(&ath10k_pci_driver);
2563}
2564
2565module_exit(ath10k_pci_exit);
2566
2567MODULE_AUTHOR("Qualcomm Atheros");
2568MODULE_DESCRIPTION("Driver support for Atheros QCA988X PCIe devices");
2569MODULE_LICENSE("Dual BSD/GPL");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002570MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_FW_FILE);
2571MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_OTP_FILE);
2572MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_BOARD_DATA_FILE);