ath10k: make sure to really disable irqs
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / ath / ath10k / pci.c
1 /*
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 #include <linux/bitops.h>
23
24 #include "core.h"
25 #include "debug.h"
26
27 #include "targaddrs.h"
28 #include "bmi.h"
29
30 #include "hif.h"
31 #include "htc.h"
32
33 #include "ce.h"
34 #include "pci.h"
35
36 enum ath10k_pci_irq_mode {
37         ATH10K_PCI_IRQ_AUTO = 0,
38         ATH10K_PCI_IRQ_LEGACY = 1,
39         ATH10K_PCI_IRQ_MSI = 2,
40 };
41
42 enum ath10k_pci_reset_mode {
43         ATH10K_PCI_RESET_AUTO = 0,
44         ATH10K_PCI_RESET_WARM_ONLY = 1,
45 };
46
47 static unsigned int ath10k_pci_irq_mode = ATH10K_PCI_IRQ_AUTO;
48 static unsigned int ath10k_pci_reset_mode = ATH10K_PCI_RESET_AUTO;
49
50 module_param_named(irq_mode, ath10k_pci_irq_mode, uint, 0644);
51 MODULE_PARM_DESC(irq_mode, "0: auto, 1: legacy, 2: msi (default: 0)");
52
53 module_param_named(reset_mode, ath10k_pci_reset_mode, uint, 0644);
54 MODULE_PARM_DESC(reset_mode, "0: auto, 1: warm only (default: 0)");
55
56 /* how long wait to wait for target to initialise, in ms */
57 #define ATH10K_PCI_TARGET_WAIT 3000
58 #define ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS 3
59
60 #define QCA988X_2_0_DEVICE_ID   (0x003c)
61
62 static DEFINE_PCI_DEVICE_TABLE(ath10k_pci_id_table) = {
63         { PCI_VDEVICE(ATHEROS, QCA988X_2_0_DEVICE_ID) }, /* PCI-E QCA988X V2 */
64         {0}
65 };
66
67 static int ath10k_pci_diag_read_access(struct ath10k *ar, u32 address,
68                                        u32 *data);
69
70 static int ath10k_pci_post_rx(struct ath10k *ar);
71 static int ath10k_pci_post_rx_pipe(struct ath10k_pci_pipe *pipe_info,
72                                              int num);
73 static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe *pipe_info);
74 static int ath10k_pci_cold_reset(struct ath10k *ar);
75 static int ath10k_pci_warm_reset(struct ath10k *ar);
76 static int ath10k_pci_wait_for_target_init(struct ath10k *ar);
77 static int ath10k_pci_init_irq(struct ath10k *ar);
78 static int ath10k_pci_deinit_irq(struct ath10k *ar);
79 static int ath10k_pci_request_irq(struct ath10k *ar);
80 static void ath10k_pci_free_irq(struct ath10k *ar);
81 static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
82                                struct ath10k_ce_pipe *rx_pipe,
83                                struct bmi_xfer *xfer);
84
85 static const struct ce_attr host_ce_config_wlan[] = {
86         /* CE0: host->target HTC control and raw streams */
87         {
88                 .flags = CE_ATTR_FLAGS,
89                 .src_nentries = 16,
90                 .src_sz_max = 256,
91                 .dest_nentries = 0,
92         },
93
94         /* CE1: target->host HTT + HTC control */
95         {
96                 .flags = CE_ATTR_FLAGS,
97                 .src_nentries = 0,
98                 .src_sz_max = 512,
99                 .dest_nentries = 512,
100         },
101
102         /* CE2: target->host WMI */
103         {
104                 .flags = CE_ATTR_FLAGS,
105                 .src_nentries = 0,
106                 .src_sz_max = 2048,
107                 .dest_nentries = 32,
108         },
109
110         /* CE3: host->target WMI */
111         {
112                 .flags = CE_ATTR_FLAGS,
113                 .src_nentries = 32,
114                 .src_sz_max = 2048,
115                 .dest_nentries = 0,
116         },
117
118         /* CE4: host->target HTT */
119         {
120                 .flags = CE_ATTR_FLAGS | CE_ATTR_DIS_INTR,
121                 .src_nentries = CE_HTT_H2T_MSG_SRC_NENTRIES,
122                 .src_sz_max = 256,
123                 .dest_nentries = 0,
124         },
125
126         /* CE5: unused */
127         {
128                 .flags = CE_ATTR_FLAGS,
129                 .src_nentries = 0,
130                 .src_sz_max = 0,
131                 .dest_nentries = 0,
132         },
133
134         /* CE6: target autonomous hif_memcpy */
135         {
136                 .flags = CE_ATTR_FLAGS,
137                 .src_nentries = 0,
138                 .src_sz_max = 0,
139                 .dest_nentries = 0,
140         },
141
142         /* CE7: ce_diag, the Diagnostic Window */
143         {
144                 .flags = CE_ATTR_FLAGS,
145                 .src_nentries = 2,
146                 .src_sz_max = DIAG_TRANSFER_LIMIT,
147                 .dest_nentries = 2,
148         },
149 };
150
151 /* Target firmware's Copy Engine configuration. */
152 static const struct ce_pipe_config target_ce_config_wlan[] = {
153         /* CE0: host->target HTC control and raw streams */
154         {
155                 .pipenum = 0,
156                 .pipedir = PIPEDIR_OUT,
157                 .nentries = 32,
158                 .nbytes_max = 256,
159                 .flags = CE_ATTR_FLAGS,
160                 .reserved = 0,
161         },
162
163         /* CE1: target->host HTT + HTC control */
164         {
165                 .pipenum = 1,
166                 .pipedir = PIPEDIR_IN,
167                 .nentries = 32,
168                 .nbytes_max = 512,
169                 .flags = CE_ATTR_FLAGS,
170                 .reserved = 0,
171         },
172
173         /* CE2: target->host WMI */
174         {
175                 .pipenum = 2,
176                 .pipedir = PIPEDIR_IN,
177                 .nentries = 32,
178                 .nbytes_max = 2048,
179                 .flags = CE_ATTR_FLAGS,
180                 .reserved = 0,
181         },
182
183         /* CE3: host->target WMI */
184         {
185                 .pipenum = 3,
186                 .pipedir = PIPEDIR_OUT,
187                 .nentries = 32,
188                 .nbytes_max = 2048,
189                 .flags = CE_ATTR_FLAGS,
190                 .reserved = 0,
191         },
192
193         /* CE4: host->target HTT */
194         {
195                 .pipenum = 4,
196                 .pipedir = PIPEDIR_OUT,
197                 .nentries = 256,
198                 .nbytes_max = 256,
199                 .flags = CE_ATTR_FLAGS,
200                 .reserved = 0,
201         },
202
203         /* NB: 50% of src nentries, since tx has 2 frags */
204
205         /* CE5: unused */
206         {
207                 .pipenum = 5,
208                 .pipedir = PIPEDIR_OUT,
209                 .nentries = 32,
210                 .nbytes_max = 2048,
211                 .flags = CE_ATTR_FLAGS,
212                 .reserved = 0,
213         },
214
215         /* CE6: Reserved for target autonomous hif_memcpy */
216         {
217                 .pipenum = 6,
218                 .pipedir = PIPEDIR_INOUT,
219                 .nentries = 32,
220                 .nbytes_max = 4096,
221                 .flags = CE_ATTR_FLAGS,
222                 .reserved = 0,
223         },
224
225         /* CE7 used only by Host */
226 };
227
228 static bool ath10k_pci_irq_pending(struct ath10k *ar)
229 {
230         u32 cause;
231
232         /* Check if the shared legacy irq is for us */
233         cause = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
234                                   PCIE_INTR_CAUSE_ADDRESS);
235         if (cause & (PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL))
236                 return true;
237
238         return false;
239 }
240
241 static void ath10k_pci_disable_and_clear_legacy_irq(struct ath10k *ar)
242 {
243         /* IMPORTANT: INTR_CLR register has to be set after
244          * INTR_ENABLE is set to 0, otherwise interrupt can not be
245          * really cleared. */
246         ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
247                            0);
248         ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_CLR_ADDRESS,
249                            PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
250
251         /* IMPORTANT: this extra read transaction is required to
252          * flush the posted write buffer. */
253         (void) ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
254                                  PCIE_INTR_ENABLE_ADDRESS);
255 }
256
257 static void ath10k_pci_enable_legacy_irq(struct ath10k *ar)
258 {
259         ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
260                            PCIE_INTR_ENABLE_ADDRESS,
261                            PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
262
263         /* IMPORTANT: this extra read transaction is required to
264          * flush the posted write buffer. */
265         (void) ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
266                                  PCIE_INTR_ENABLE_ADDRESS);
267 }
268
269 static irqreturn_t ath10k_pci_early_irq_handler(int irq, void *arg)
270 {
271         struct ath10k *ar = arg;
272         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
273
274         if (ar_pci->num_msi_intrs == 0) {
275                 if (!ath10k_pci_irq_pending(ar))
276                         return IRQ_NONE;
277
278                 ath10k_pci_disable_and_clear_legacy_irq(ar);
279         }
280
281         tasklet_schedule(&ar_pci->early_irq_tasklet);
282
283         return IRQ_HANDLED;
284 }
285
286 static int ath10k_pci_request_early_irq(struct ath10k *ar)
287 {
288         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
289         int ret;
290
291         /* Regardless whether MSI-X/MSI/legacy irqs have been set up the first
292          * interrupt from irq vector is triggered in all cases for FW
293          * indication/errors */
294         ret = request_irq(ar_pci->pdev->irq, ath10k_pci_early_irq_handler,
295                           IRQF_SHARED, "ath10k_pci (early)", ar);
296         if (ret) {
297                 ath10k_warn("failed to request early irq: %d\n", ret);
298                 return ret;
299         }
300
301         return 0;
302 }
303
304 static void ath10k_pci_free_early_irq(struct ath10k *ar)
305 {
306         free_irq(ath10k_pci_priv(ar)->pdev->irq, ar);
307 }
308
309 static inline const char *ath10k_pci_get_irq_method(struct ath10k *ar)
310 {
311         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
312
313         if (ar_pci->num_msi_intrs > 1)
314                 return "msi-x";
315         else if (ar_pci->num_msi_intrs == 1)
316                 return "msi";
317         else
318                 return "legacy";
319 }
320
321 /*
322  * Diagnostic read/write access is provided for startup/config/debug usage.
323  * Caller must guarantee proper alignment, when applicable, and single user
324  * at any moment.
325  */
326 static int ath10k_pci_diag_read_mem(struct ath10k *ar, u32 address, void *data,
327                                     int nbytes)
328 {
329         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
330         int ret = 0;
331         u32 buf;
332         unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
333         unsigned int id;
334         unsigned int flags;
335         struct ath10k_ce_pipe *ce_diag;
336         /* Host buffer address in CE space */
337         u32 ce_data;
338         dma_addr_t ce_data_base = 0;
339         void *data_buf = NULL;
340         int i;
341
342         /*
343          * This code cannot handle reads to non-memory space. Redirect to the
344          * register read fn but preserve the multi word read capability of
345          * this fn
346          */
347         if (address < DRAM_BASE_ADDRESS) {
348                 if (!IS_ALIGNED(address, 4) ||
349                     !IS_ALIGNED((unsigned long)data, 4))
350                         return -EIO;
351
352                 while ((nbytes >= 4) &&  ((ret = ath10k_pci_diag_read_access(
353                                            ar, address, (u32 *)data)) == 0)) {
354                         nbytes -= sizeof(u32);
355                         address += sizeof(u32);
356                         data += sizeof(u32);
357                 }
358                 return ret;
359         }
360
361         ce_diag = ar_pci->ce_diag;
362
363         /*
364          * Allocate a temporary bounce buffer to hold caller's data
365          * to be DMA'ed from Target. This guarantees
366          *   1) 4-byte alignment
367          *   2) Buffer in DMA-able space
368          */
369         orig_nbytes = nbytes;
370         data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
371                                                        orig_nbytes,
372                                                        &ce_data_base,
373                                                        GFP_ATOMIC);
374
375         if (!data_buf) {
376                 ret = -ENOMEM;
377                 goto done;
378         }
379         memset(data_buf, 0, orig_nbytes);
380
381         remaining_bytes = orig_nbytes;
382         ce_data = ce_data_base;
383         while (remaining_bytes) {
384                 nbytes = min_t(unsigned int, remaining_bytes,
385                                DIAG_TRANSFER_LIMIT);
386
387                 ret = ath10k_ce_recv_buf_enqueue(ce_diag, NULL, ce_data);
388                 if (ret != 0)
389                         goto done;
390
391                 /* Request CE to send from Target(!) address to Host buffer */
392                 /*
393                  * The address supplied by the caller is in the
394                  * Target CPU virtual address space.
395                  *
396                  * In order to use this address with the diagnostic CE,
397                  * convert it from Target CPU virtual address space
398                  * to CE address space
399                  */
400                 address = TARG_CPU_SPACE_TO_CE_SPACE(ar, ar_pci->mem,
401                                                      address);
402
403                 ret = ath10k_ce_send(ce_diag, NULL, (u32)address, nbytes, 0,
404                                  0);
405                 if (ret)
406                         goto done;
407
408                 i = 0;
409                 while (ath10k_ce_completed_send_next(ce_diag, NULL, &buf,
410                                                      &completed_nbytes,
411                                                      &id) != 0) {
412                         mdelay(1);
413                         if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
414                                 ret = -EBUSY;
415                                 goto done;
416                         }
417                 }
418
419                 if (nbytes != completed_nbytes) {
420                         ret = -EIO;
421                         goto done;
422                 }
423
424                 if (buf != (u32) address) {
425                         ret = -EIO;
426                         goto done;
427                 }
428
429                 i = 0;
430                 while (ath10k_ce_completed_recv_next(ce_diag, NULL, &buf,
431                                                      &completed_nbytes,
432                                                      &id, &flags) != 0) {
433                         mdelay(1);
434
435                         if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
436                                 ret = -EBUSY;
437                                 goto done;
438                         }
439                 }
440
441                 if (nbytes != completed_nbytes) {
442                         ret = -EIO;
443                         goto done;
444                 }
445
446                 if (buf != ce_data) {
447                         ret = -EIO;
448                         goto done;
449                 }
450
451                 remaining_bytes -= nbytes;
452                 address += nbytes;
453                 ce_data += nbytes;
454         }
455
456 done:
457         if (ret == 0) {
458                 /* Copy data from allocated DMA buf to caller's buf */
459                 WARN_ON_ONCE(orig_nbytes & 3);
460                 for (i = 0; i < orig_nbytes / sizeof(__le32); i++) {
461                         ((u32 *)data)[i] =
462                                 __le32_to_cpu(((__le32 *)data_buf)[i]);
463                 }
464         } else
465                 ath10k_warn("failed to read diag value at 0x%x: %d\n",
466                             address, ret);
467
468         if (data_buf)
469                 dma_free_coherent(ar->dev, orig_nbytes, data_buf,
470                                   ce_data_base);
471
472         return ret;
473 }
474
475 static int ath10k_pci_diag_read32(struct ath10k *ar, u32 address, u32 *value)
476 {
477         return ath10k_pci_diag_read_mem(ar, address, value, sizeof(u32));
478 }
479
480 static int __ath10k_pci_diag_read_hi(struct ath10k *ar, void *dest,
481                                      u32 src, u32 len)
482 {
483         u32 host_addr, addr;
484         int ret;
485
486         host_addr = host_interest_item_address(src);
487
488         ret = ath10k_pci_diag_read32(ar, host_addr, &addr);
489         if (ret != 0) {
490                 ath10k_warn("failed to get memcpy hi address for firmware address %d: %d\n",
491                             src, ret);
492                 return ret;
493         }
494
495         ret = ath10k_pci_diag_read_mem(ar, addr, dest, len);
496         if (ret != 0) {
497                 ath10k_warn("failed to memcpy firmware memory from %d (%d B): %d\n",
498                             addr, len, ret);
499                 return ret;
500         }
501
502         return 0;
503 }
504
505 #define ath10k_pci_diag_read_hi(ar, dest, src, len)             \
506         __ath10k_pci_diag_read_hi(ar, dest, HI_ITEM(src), len);
507
508 /* Read 4-byte aligned data from Target memory or register */
509 static int ath10k_pci_diag_read_access(struct ath10k *ar, u32 address,
510                                        u32 *data)
511 {
512         /* Assume range doesn't cross this boundary */
513         if (address >= DRAM_BASE_ADDRESS)
514                 return ath10k_pci_diag_read32(ar, address, data);
515
516         *data = ath10k_pci_read32(ar, address);
517         return 0;
518 }
519
520 static int ath10k_pci_diag_write_mem(struct ath10k *ar, u32 address,
521                                      const void *data, int nbytes)
522 {
523         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
524         int ret = 0;
525         u32 buf;
526         unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
527         unsigned int id;
528         unsigned int flags;
529         struct ath10k_ce_pipe *ce_diag;
530         void *data_buf = NULL;
531         u32 ce_data;    /* Host buffer address in CE space */
532         dma_addr_t ce_data_base = 0;
533         int i;
534
535         ce_diag = ar_pci->ce_diag;
536
537         /*
538          * Allocate a temporary bounce buffer to hold caller's data
539          * to be DMA'ed to Target. This guarantees
540          *   1) 4-byte alignment
541          *   2) Buffer in DMA-able space
542          */
543         orig_nbytes = nbytes;
544         data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
545                                                        orig_nbytes,
546                                                        &ce_data_base,
547                                                        GFP_ATOMIC);
548         if (!data_buf) {
549                 ret = -ENOMEM;
550                 goto done;
551         }
552
553         /* Copy caller's data to allocated DMA buf */
554         WARN_ON_ONCE(orig_nbytes & 3);
555         for (i = 0; i < orig_nbytes / sizeof(__le32); i++)
556                 ((__le32 *)data_buf)[i] = __cpu_to_le32(((u32 *)data)[i]);
557
558         /*
559          * The address supplied by the caller is in the
560          * Target CPU virtual address space.
561          *
562          * In order to use this address with the diagnostic CE,
563          * convert it from
564          *    Target CPU virtual address space
565          * to
566          *    CE address space
567          */
568         address = TARG_CPU_SPACE_TO_CE_SPACE(ar, ar_pci->mem, address);
569
570         remaining_bytes = orig_nbytes;
571         ce_data = ce_data_base;
572         while (remaining_bytes) {
573                 /* FIXME: check cast */
574                 nbytes = min_t(int, remaining_bytes, DIAG_TRANSFER_LIMIT);
575
576                 /* Set up to receive directly into Target(!) address */
577                 ret = ath10k_ce_recv_buf_enqueue(ce_diag, NULL, address);
578                 if (ret != 0)
579                         goto done;
580
581                 /*
582                  * Request CE to send caller-supplied data that
583                  * was copied to bounce buffer to Target(!) address.
584                  */
585                 ret = ath10k_ce_send(ce_diag, NULL, (u32) ce_data,
586                                      nbytes, 0, 0);
587                 if (ret != 0)
588                         goto done;
589
590                 i = 0;
591                 while (ath10k_ce_completed_send_next(ce_diag, NULL, &buf,
592                                                      &completed_nbytes,
593                                                      &id) != 0) {
594                         mdelay(1);
595
596                         if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
597                                 ret = -EBUSY;
598                                 goto done;
599                         }
600                 }
601
602                 if (nbytes != completed_nbytes) {
603                         ret = -EIO;
604                         goto done;
605                 }
606
607                 if (buf != ce_data) {
608                         ret = -EIO;
609                         goto done;
610                 }
611
612                 i = 0;
613                 while (ath10k_ce_completed_recv_next(ce_diag, NULL, &buf,
614                                                      &completed_nbytes,
615                                                      &id, &flags) != 0) {
616                         mdelay(1);
617
618                         if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
619                                 ret = -EBUSY;
620                                 goto done;
621                         }
622                 }
623
624                 if (nbytes != completed_nbytes) {
625                         ret = -EIO;
626                         goto done;
627                 }
628
629                 if (buf != address) {
630                         ret = -EIO;
631                         goto done;
632                 }
633
634                 remaining_bytes -= nbytes;
635                 address += nbytes;
636                 ce_data += nbytes;
637         }
638
639 done:
640         if (data_buf) {
641                 dma_free_coherent(ar->dev, orig_nbytes, data_buf,
642                                   ce_data_base);
643         }
644
645         if (ret != 0)
646                 ath10k_warn("failed to write diag value at 0x%x: %d\n",
647                             address, ret);
648
649         return ret;
650 }
651
652 /* Write 4B data to Target memory or register */
653 static int ath10k_pci_diag_write_access(struct ath10k *ar, u32 address,
654                                         u32 data)
655 {
656         /* Assume range doesn't cross this boundary */
657         if (address >= DRAM_BASE_ADDRESS)
658                 return ath10k_pci_diag_write_mem(ar, address, &data,
659                                                  sizeof(u32));
660
661         ath10k_pci_write32(ar, address, data);
662         return 0;
663 }
664
665 static bool ath10k_pci_is_awake(struct ath10k *ar)
666 {
667         u32 val = ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS);
668
669         return RTC_STATE_V_GET(val) == RTC_STATE_V_ON;
670 }
671
672 static int ath10k_pci_wake_wait(struct ath10k *ar)
673 {
674         int tot_delay = 0;
675         int curr_delay = 5;
676
677         while (tot_delay < PCIE_WAKE_TIMEOUT) {
678                 if (ath10k_pci_is_awake(ar))
679                         return 0;
680
681                 udelay(curr_delay);
682                 tot_delay += curr_delay;
683
684                 if (curr_delay < 50)
685                         curr_delay += 5;
686         }
687
688         return -ETIMEDOUT;
689 }
690
691 static int ath10k_pci_wake(struct ath10k *ar)
692 {
693         ath10k_pci_reg_write32(ar, PCIE_SOC_WAKE_ADDRESS,
694                                PCIE_SOC_WAKE_V_MASK);
695         return ath10k_pci_wake_wait(ar);
696 }
697
698 static void ath10k_pci_sleep(struct ath10k *ar)
699 {
700         ath10k_pci_reg_write32(ar, PCIE_SOC_WAKE_ADDRESS,
701                                PCIE_SOC_WAKE_RESET);
702 }
703
704 /* Called by lower (CE) layer when a send to Target completes. */
705 static void ath10k_pci_ce_send_done(struct ath10k_ce_pipe *ce_state)
706 {
707         struct ath10k *ar = ce_state->ar;
708         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
709         struct ath10k_hif_cb *cb = &ar_pci->msg_callbacks_current;
710         void *transfer_context;
711         u32 ce_data;
712         unsigned int nbytes;
713         unsigned int transfer_id;
714
715         while (ath10k_ce_completed_send_next(ce_state, &transfer_context,
716                                              &ce_data, &nbytes,
717                                              &transfer_id) == 0) {
718                 /* no need to call tx completion for NULL pointers */
719                 if (transfer_context == NULL)
720                         continue;
721
722                 cb->tx_completion(ar, transfer_context, transfer_id);
723         }
724 }
725
726 /* Called by lower (CE) layer when data is received from the Target. */
727 static void ath10k_pci_ce_recv_data(struct ath10k_ce_pipe *ce_state)
728 {
729         struct ath10k *ar = ce_state->ar;
730         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
731         struct ath10k_pci_pipe *pipe_info =  &ar_pci->pipe_info[ce_state->id];
732         struct ath10k_hif_cb *cb = &ar_pci->msg_callbacks_current;
733         struct sk_buff *skb;
734         void *transfer_context;
735         u32 ce_data;
736         unsigned int nbytes, max_nbytes;
737         unsigned int transfer_id;
738         unsigned int flags;
739         int err, num_replenish = 0;
740
741         while (ath10k_ce_completed_recv_next(ce_state, &transfer_context,
742                                              &ce_data, &nbytes, &transfer_id,
743                                              &flags) == 0) {
744                 num_replenish++;
745                 skb = transfer_context;
746                 max_nbytes = skb->len + skb_tailroom(skb);
747                 dma_unmap_single(ar->dev, ATH10K_SKB_CB(skb)->paddr,
748                                  max_nbytes, DMA_FROM_DEVICE);
749
750                 if (unlikely(max_nbytes < nbytes)) {
751                         ath10k_warn("rxed more than expected (nbytes %d, max %d)",
752                                     nbytes, max_nbytes);
753                         dev_kfree_skb_any(skb);
754                         continue;
755                 }
756
757                 skb_put(skb, nbytes);
758                 cb->rx_completion(ar, skb, pipe_info->pipe_num);
759         }
760
761         err = ath10k_pci_post_rx_pipe(pipe_info, num_replenish);
762         if (unlikely(err)) {
763                 /* FIXME: retry */
764                 ath10k_warn("failed to replenish CE rx ring %d (%d bufs): %d\n",
765                             pipe_info->pipe_num, num_replenish, err);
766         }
767 }
768
769 static int ath10k_pci_hif_tx_sg(struct ath10k *ar, u8 pipe_id,
770                                 struct ath10k_hif_sg_item *items, int n_items)
771 {
772         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
773         struct ath10k_pci_pipe *pci_pipe = &ar_pci->pipe_info[pipe_id];
774         struct ath10k_ce_pipe *ce_pipe = pci_pipe->ce_hdl;
775         struct ath10k_ce_ring *src_ring = ce_pipe->src_ring;
776         unsigned int nentries_mask;
777         unsigned int sw_index;
778         unsigned int write_index;
779         int err, i = 0;
780
781         spin_lock_bh(&ar_pci->ce_lock);
782
783         nentries_mask = src_ring->nentries_mask;
784         sw_index = src_ring->sw_index;
785         write_index = src_ring->write_index;
786
787         if (unlikely(CE_RING_DELTA(nentries_mask,
788                                    write_index, sw_index - 1) < n_items)) {
789                 err = -ENOBUFS;
790                 goto err;
791         }
792
793         for (i = 0; i < n_items - 1; i++) {
794                 ath10k_dbg(ATH10K_DBG_PCI,
795                            "pci tx item %d paddr 0x%08x len %d n_items %d\n",
796                            i, items[i].paddr, items[i].len, n_items);
797                 ath10k_dbg_dump(ATH10K_DBG_PCI_DUMP, NULL, "item data: ",
798                                 items[i].vaddr, items[i].len);
799
800                 err = ath10k_ce_send_nolock(ce_pipe,
801                                             items[i].transfer_context,
802                                             items[i].paddr,
803                                             items[i].len,
804                                             items[i].transfer_id,
805                                             CE_SEND_FLAG_GATHER);
806                 if (err)
807                         goto err;
808         }
809
810         /* `i` is equal to `n_items -1` after for() */
811
812         ath10k_dbg(ATH10K_DBG_PCI,
813                    "pci tx item %d paddr 0x%08x len %d n_items %d\n",
814                    i, items[i].paddr, items[i].len, n_items);
815         ath10k_dbg_dump(ATH10K_DBG_PCI_DUMP, NULL, "item data: ",
816                         items[i].vaddr, items[i].len);
817
818         err = ath10k_ce_send_nolock(ce_pipe,
819                                     items[i].transfer_context,
820                                     items[i].paddr,
821                                     items[i].len,
822                                     items[i].transfer_id,
823                                     0);
824         if (err)
825                 goto err;
826
827         spin_unlock_bh(&ar_pci->ce_lock);
828         return 0;
829
830 err:
831         for (; i > 0; i--)
832                 __ath10k_ce_send_revert(ce_pipe);
833
834         spin_unlock_bh(&ar_pci->ce_lock);
835         return err;
836 }
837
838 static u16 ath10k_pci_hif_get_free_queue_number(struct ath10k *ar, u8 pipe)
839 {
840         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
841
842         ath10k_dbg(ATH10K_DBG_PCI, "pci hif get free queue number\n");
843
844         return ath10k_ce_num_free_src_entries(ar_pci->pipe_info[pipe].ce_hdl);
845 }
846
847 static void ath10k_pci_dump_registers(struct ath10k *ar,
848                                       struct ath10k_fw_crash_data *crash_data)
849 {
850         u32 i, reg_dump_values[REG_DUMP_COUNT_QCA988X] = {};
851         int ret;
852
853         lockdep_assert_held(&ar->data_lock);
854
855         ret = ath10k_pci_diag_read_hi(ar, &reg_dump_values[0],
856                                       hi_failure_state,
857                                       REG_DUMP_COUNT_QCA988X * sizeof(u32));
858         if (ret) {
859                 ath10k_err("failed to read firmware dump area: %d\n", ret);
860                 return;
861         }
862
863         BUILD_BUG_ON(REG_DUMP_COUNT_QCA988X % 4);
864
865         ath10k_err("firmware register dump:\n");
866         for (i = 0; i < REG_DUMP_COUNT_QCA988X; i += 4)
867                 ath10k_err("[%02d]: 0x%08X 0x%08X 0x%08X 0x%08X\n",
868                            i,
869                            reg_dump_values[i],
870                            reg_dump_values[i + 1],
871                            reg_dump_values[i + 2],
872                            reg_dump_values[i + 3]);
873
874         /* crash_data is in little endian */
875         for (i = 0; i < REG_DUMP_COUNT_QCA988X; i++)
876                 crash_data->registers[i] = cpu_to_le32(reg_dump_values[i]);
877 }
878
879 static void ath10k_pci_fw_crashed_dump(struct ath10k *ar)
880 {
881         struct ath10k_fw_crash_data *crash_data;
882         char uuid[50];
883
884         spin_lock_bh(&ar->data_lock);
885
886         crash_data = ath10k_debug_get_new_fw_crash_data(ar);
887
888         if (crash_data)
889                 scnprintf(uuid, sizeof(uuid), "%pUl", &crash_data->uuid);
890         else
891                 scnprintf(uuid, sizeof(uuid), "n/a");
892
893         ath10k_err("firmware crashed! (uuid %s)\n", uuid);
894         ath10k_print_driver_info(ar);
895
896         if (!crash_data)
897                 goto exit;
898
899         ath10k_pci_dump_registers(ar, crash_data);
900
901 exit:
902         spin_unlock_bh(&ar->data_lock);
903
904         queue_work(ar->workqueue, &ar->restart_work);
905 }
906
907 static void ath10k_pci_hif_send_complete_check(struct ath10k *ar, u8 pipe,
908                                                int force)
909 {
910         ath10k_dbg(ATH10K_DBG_PCI, "pci hif send complete check\n");
911
912         if (!force) {
913                 int resources;
914                 /*
915                  * Decide whether to actually poll for completions, or just
916                  * wait for a later chance.
917                  * If there seem to be plenty of resources left, then just wait
918                  * since checking involves reading a CE register, which is a
919                  * relatively expensive operation.
920                  */
921                 resources = ath10k_pci_hif_get_free_queue_number(ar, pipe);
922
923                 /*
924                  * If at least 50% of the total resources are still available,
925                  * don't bother checking again yet.
926                  */
927                 if (resources > (host_ce_config_wlan[pipe].src_nentries >> 1))
928                         return;
929         }
930         ath10k_ce_per_engine_service(ar, pipe);
931 }
932
933 static void ath10k_pci_hif_set_callbacks(struct ath10k *ar,
934                                          struct ath10k_hif_cb *callbacks)
935 {
936         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
937
938         ath10k_dbg(ATH10K_DBG_PCI, "pci hif set callbacks\n");
939
940         memcpy(&ar_pci->msg_callbacks_current, callbacks,
941                sizeof(ar_pci->msg_callbacks_current));
942 }
943
944 static void ath10k_pci_kill_tasklet(struct ath10k *ar)
945 {
946         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
947         int i;
948
949         tasklet_kill(&ar_pci->intr_tq);
950         tasklet_kill(&ar_pci->msi_fw_err);
951         tasklet_kill(&ar_pci->early_irq_tasklet);
952
953         for (i = 0; i < CE_COUNT; i++)
954                 tasklet_kill(&ar_pci->pipe_info[i].intr);
955 }
956
957 /* TODO - temporary mapping while we have too few CE's */
958 static int ath10k_pci_hif_map_service_to_pipe(struct ath10k *ar,
959                                               u16 service_id, u8 *ul_pipe,
960                                               u8 *dl_pipe, int *ul_is_polled,
961                                               int *dl_is_polled)
962 {
963         int ret = 0;
964
965         ath10k_dbg(ATH10K_DBG_PCI, "pci hif map service\n");
966
967         /* polling for received messages not supported */
968         *dl_is_polled = 0;
969
970         switch (service_id) {
971         case ATH10K_HTC_SVC_ID_HTT_DATA_MSG:
972                 /*
973                  * Host->target HTT gets its own pipe, so it can be polled
974                  * while other pipes are interrupt driven.
975                  */
976                 *ul_pipe = 4;
977                 /*
978                  * Use the same target->host pipe for HTC ctrl, HTC raw
979                  * streams, and HTT.
980                  */
981                 *dl_pipe = 1;
982                 break;
983
984         case ATH10K_HTC_SVC_ID_RSVD_CTRL:
985         case ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS:
986                 /*
987                  * Note: HTC_RAW_STREAMS_SVC is currently unused, and
988                  * HTC_CTRL_RSVD_SVC could share the same pipe as the
989                  * WMI services.  So, if another CE is needed, change
990                  * this to *ul_pipe = 3, which frees up CE 0.
991                  */
992                 /* *ul_pipe = 3; */
993                 *ul_pipe = 0;
994                 *dl_pipe = 1;
995                 break;
996
997         case ATH10K_HTC_SVC_ID_WMI_DATA_BK:
998         case ATH10K_HTC_SVC_ID_WMI_DATA_BE:
999         case ATH10K_HTC_SVC_ID_WMI_DATA_VI:
1000         case ATH10K_HTC_SVC_ID_WMI_DATA_VO:
1001
1002         case ATH10K_HTC_SVC_ID_WMI_CONTROL:
1003                 *ul_pipe = 3;
1004                 *dl_pipe = 2;
1005                 break;
1006
1007                 /* pipe 5 unused   */
1008                 /* pipe 6 reserved */
1009                 /* pipe 7 reserved */
1010
1011         default:
1012                 ret = -1;
1013                 break;
1014         }
1015         *ul_is_polled =
1016                 (host_ce_config_wlan[*ul_pipe].flags & CE_ATTR_DIS_INTR) != 0;
1017
1018         return ret;
1019 }
1020
1021 static void ath10k_pci_hif_get_default_pipe(struct ath10k *ar,
1022                                                 u8 *ul_pipe, u8 *dl_pipe)
1023 {
1024         int ul_is_polled, dl_is_polled;
1025
1026         ath10k_dbg(ATH10K_DBG_PCI, "pci hif get default pipe\n");
1027
1028         (void)ath10k_pci_hif_map_service_to_pipe(ar,
1029                                                  ATH10K_HTC_SVC_ID_RSVD_CTRL,
1030                                                  ul_pipe,
1031                                                  dl_pipe,
1032                                                  &ul_is_polled,
1033                                                  &dl_is_polled);
1034 }
1035
1036 static int ath10k_pci_post_rx_pipe(struct ath10k_pci_pipe *pipe_info,
1037                                    int num)
1038 {
1039         struct ath10k *ar = pipe_info->hif_ce_state;
1040         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1041         struct ath10k_ce_pipe *ce_state = pipe_info->ce_hdl;
1042         struct sk_buff *skb;
1043         dma_addr_t ce_data;
1044         int i, ret = 0;
1045
1046         if (pipe_info->buf_sz == 0)
1047                 return 0;
1048
1049         for (i = 0; i < num; i++) {
1050                 skb = dev_alloc_skb(pipe_info->buf_sz);
1051                 if (!skb) {
1052                         ath10k_warn("failed to allocate skbuff for pipe %d\n",
1053                                     num);
1054                         ret = -ENOMEM;
1055                         goto err;
1056                 }
1057
1058                 WARN_ONCE((unsigned long)skb->data & 3, "unaligned skb");
1059
1060                 ce_data = dma_map_single(ar->dev, skb->data,
1061                                          skb->len + skb_tailroom(skb),
1062                                          DMA_FROM_DEVICE);
1063
1064                 if (unlikely(dma_mapping_error(ar->dev, ce_data))) {
1065                         ath10k_warn("failed to DMA map sk_buff\n");
1066                         dev_kfree_skb_any(skb);
1067                         ret = -EIO;
1068                         goto err;
1069                 }
1070
1071                 ATH10K_SKB_CB(skb)->paddr = ce_data;
1072
1073                 pci_dma_sync_single_for_device(ar_pci->pdev, ce_data,
1074                                                pipe_info->buf_sz,
1075                                                PCI_DMA_FROMDEVICE);
1076
1077                 ret = ath10k_ce_recv_buf_enqueue(ce_state, (void *)skb,
1078                                                  ce_data);
1079                 if (ret) {
1080                         ath10k_warn("failed to enqueue to pipe %d: %d\n",
1081                                     num, ret);
1082                         goto err;
1083                 }
1084         }
1085
1086         return ret;
1087
1088 err:
1089         ath10k_pci_rx_pipe_cleanup(pipe_info);
1090         return ret;
1091 }
1092
1093 static int ath10k_pci_post_rx(struct ath10k *ar)
1094 {
1095         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1096         struct ath10k_pci_pipe *pipe_info;
1097         const struct ce_attr *attr;
1098         int pipe_num, ret = 0;
1099
1100         for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
1101                 pipe_info = &ar_pci->pipe_info[pipe_num];
1102                 attr = &host_ce_config_wlan[pipe_num];
1103
1104                 if (attr->dest_nentries == 0)
1105                         continue;
1106
1107                 ret = ath10k_pci_post_rx_pipe(pipe_info,
1108                                               attr->dest_nentries - 1);
1109                 if (ret) {
1110                         ath10k_warn("failed to post RX buffer for pipe %d: %d\n",
1111                                     pipe_num, ret);
1112
1113                         for (; pipe_num >= 0; pipe_num--) {
1114                                 pipe_info = &ar_pci->pipe_info[pipe_num];
1115                                 ath10k_pci_rx_pipe_cleanup(pipe_info);
1116                         }
1117                         return ret;
1118                 }
1119         }
1120
1121         return 0;
1122 }
1123
1124 static void ath10k_pci_irq_disable(struct ath10k *ar)
1125 {
1126         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1127         int i;
1128
1129         ath10k_ce_disable_interrupts(ar);
1130
1131         /* Regardless how many interrupts were assigned for MSI the first one
1132          * is always used for firmware indications (crashes). There's no way to
1133          * mask the irq in the device so call disable_irq(). Legacy (shared)
1134          * interrupts can be masked on the device though.
1135          */
1136         if (ar_pci->num_msi_intrs > 0)
1137                 disable_irq(ar_pci->pdev->irq);
1138         else
1139                 ath10k_pci_disable_and_clear_legacy_irq(ar);
1140
1141         for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
1142                 synchronize_irq(ar_pci->pdev->irq + i);
1143 }
1144
1145 static void ath10k_pci_irq_enable(struct ath10k *ar)
1146 {
1147         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1148
1149         ath10k_ce_enable_interrupts(ar);
1150
1151         /* See comment in ath10k_pci_irq_disable() */
1152         if (ar_pci->num_msi_intrs > 0)
1153                 enable_irq(ar_pci->pdev->irq);
1154         else
1155                 ath10k_pci_enable_legacy_irq(ar);
1156 }
1157
1158 static int ath10k_pci_hif_start(struct ath10k *ar)
1159 {
1160         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1161         int ret, ret_early;
1162
1163         ath10k_dbg(ATH10K_DBG_BOOT, "boot hif start\n");
1164
1165         ath10k_pci_free_early_irq(ar);
1166         ath10k_pci_kill_tasklet(ar);
1167
1168         ret = ath10k_pci_request_irq(ar);
1169         if (ret) {
1170                 ath10k_warn("failed to post RX buffers for all pipes: %d\n",
1171                             ret);
1172                 goto err_early_irq;
1173         }
1174
1175         ath10k_pci_irq_enable(ar);
1176
1177         /* Post buffers once to start things off. */
1178         ret = ath10k_pci_post_rx(ar);
1179         if (ret) {
1180                 ath10k_warn("failed to post RX buffers for all pipes: %d\n",
1181                             ret);
1182                 goto err_stop;
1183         }
1184
1185         ar_pci->started = 1;
1186         return 0;
1187
1188 err_stop:
1189         ath10k_pci_irq_disable(ar);
1190         ath10k_pci_free_irq(ar);
1191         ath10k_pci_kill_tasklet(ar);
1192 err_early_irq:
1193         /* Though there should be no interrupts (device was reset)
1194          * power_down() expects the early IRQ to be installed as per the
1195          * driver lifecycle. */
1196         ret_early = ath10k_pci_request_early_irq(ar);
1197         if (ret_early)
1198                 ath10k_warn("failed to re-enable early irq: %d\n", ret_early);
1199
1200         return ret;
1201 }
1202
1203 static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe *pipe_info)
1204 {
1205         struct ath10k *ar;
1206         struct ath10k_pci *ar_pci;
1207         struct ath10k_ce_pipe *ce_hdl;
1208         u32 buf_sz;
1209         struct sk_buff *netbuf;
1210         u32 ce_data;
1211
1212         buf_sz = pipe_info->buf_sz;
1213
1214         /* Unused Copy Engine */
1215         if (buf_sz == 0)
1216                 return;
1217
1218         ar = pipe_info->hif_ce_state;
1219         ar_pci = ath10k_pci_priv(ar);
1220
1221         if (!ar_pci->started)
1222                 return;
1223
1224         ce_hdl = pipe_info->ce_hdl;
1225
1226         while (ath10k_ce_revoke_recv_next(ce_hdl, (void **)&netbuf,
1227                                           &ce_data) == 0) {
1228                 dma_unmap_single(ar->dev, ATH10K_SKB_CB(netbuf)->paddr,
1229                                  netbuf->len + skb_tailroom(netbuf),
1230                                  DMA_FROM_DEVICE);
1231                 dev_kfree_skb_any(netbuf);
1232         }
1233 }
1234
1235 static void ath10k_pci_tx_pipe_cleanup(struct ath10k_pci_pipe *pipe_info)
1236 {
1237         struct ath10k *ar;
1238         struct ath10k_pci *ar_pci;
1239         struct ath10k_ce_pipe *ce_hdl;
1240         struct sk_buff *netbuf;
1241         u32 ce_data;
1242         unsigned int nbytes;
1243         unsigned int id;
1244         u32 buf_sz;
1245
1246         buf_sz = pipe_info->buf_sz;
1247
1248         /* Unused Copy Engine */
1249         if (buf_sz == 0)
1250                 return;
1251
1252         ar = pipe_info->hif_ce_state;
1253         ar_pci = ath10k_pci_priv(ar);
1254
1255         if (!ar_pci->started)
1256                 return;
1257
1258         ce_hdl = pipe_info->ce_hdl;
1259
1260         while (ath10k_ce_cancel_send_next(ce_hdl, (void **)&netbuf,
1261                                           &ce_data, &nbytes, &id) == 0) {
1262                 /* no need to call tx completion for NULL pointers */
1263                 if (!netbuf)
1264                         continue;
1265
1266                 ar_pci->msg_callbacks_current.tx_completion(ar,
1267                                                             netbuf,
1268                                                             id);
1269         }
1270 }
1271
1272 /*
1273  * Cleanup residual buffers for device shutdown:
1274  *    buffers that were enqueued for receive
1275  *    buffers that were to be sent
1276  * Note: Buffers that had completed but which were
1277  * not yet processed are on a completion queue. They
1278  * are handled when the completion thread shuts down.
1279  */
1280 static void ath10k_pci_buffer_cleanup(struct ath10k *ar)
1281 {
1282         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1283         int pipe_num;
1284
1285         for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
1286                 struct ath10k_pci_pipe *pipe_info;
1287
1288                 pipe_info = &ar_pci->pipe_info[pipe_num];
1289                 ath10k_pci_rx_pipe_cleanup(pipe_info);
1290                 ath10k_pci_tx_pipe_cleanup(pipe_info);
1291         }
1292 }
1293
1294 static void ath10k_pci_ce_deinit(struct ath10k *ar)
1295 {
1296         int i;
1297
1298         for (i = 0; i < CE_COUNT; i++)
1299                 ath10k_ce_deinit_pipe(ar, i);
1300 }
1301
1302 static void ath10k_pci_hif_stop(struct ath10k *ar)
1303 {
1304         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1305         int ret;
1306
1307         ath10k_dbg(ATH10K_DBG_BOOT, "boot hif stop\n");
1308
1309         if (WARN_ON(!ar_pci->started))
1310                 return;
1311
1312         ath10k_pci_irq_disable(ar);
1313         ath10k_pci_free_irq(ar);
1314         ath10k_pci_kill_tasklet(ar);
1315
1316         ret = ath10k_pci_request_early_irq(ar);
1317         if (ret)
1318                 ath10k_warn("failed to re-enable early irq: %d\n", ret);
1319
1320         /* At this point, asynchronous threads are stopped, the target should
1321          * not DMA nor interrupt. We process the leftovers and then free
1322          * everything else up. */
1323
1324         ath10k_pci_buffer_cleanup(ar);
1325
1326         /* Make the sure the device won't access any structures on the host by
1327          * resetting it. The device was fed with PCI CE ringbuffer
1328          * configuration during init. If ringbuffers are freed and the device
1329          * were to access them this could lead to memory corruption on the
1330          * host. */
1331         ath10k_pci_warm_reset(ar);
1332
1333         ar_pci->started = 0;
1334 }
1335
1336 static int ath10k_pci_hif_exchange_bmi_msg(struct ath10k *ar,
1337                                            void *req, u32 req_len,
1338                                            void *resp, u32 *resp_len)
1339 {
1340         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1341         struct ath10k_pci_pipe *pci_tx = &ar_pci->pipe_info[BMI_CE_NUM_TO_TARG];
1342         struct ath10k_pci_pipe *pci_rx = &ar_pci->pipe_info[BMI_CE_NUM_TO_HOST];
1343         struct ath10k_ce_pipe *ce_tx = pci_tx->ce_hdl;
1344         struct ath10k_ce_pipe *ce_rx = pci_rx->ce_hdl;
1345         dma_addr_t req_paddr = 0;
1346         dma_addr_t resp_paddr = 0;
1347         struct bmi_xfer xfer = {};
1348         void *treq, *tresp = NULL;
1349         int ret = 0;
1350
1351         might_sleep();
1352
1353         if (resp && !resp_len)
1354                 return -EINVAL;
1355
1356         if (resp && resp_len && *resp_len == 0)
1357                 return -EINVAL;
1358
1359         treq = kmemdup(req, req_len, GFP_KERNEL);
1360         if (!treq)
1361                 return -ENOMEM;
1362
1363         req_paddr = dma_map_single(ar->dev, treq, req_len, DMA_TO_DEVICE);
1364         ret = dma_mapping_error(ar->dev, req_paddr);
1365         if (ret)
1366                 goto err_dma;
1367
1368         if (resp && resp_len) {
1369                 tresp = kzalloc(*resp_len, GFP_KERNEL);
1370                 if (!tresp) {
1371                         ret = -ENOMEM;
1372                         goto err_req;
1373                 }
1374
1375                 resp_paddr = dma_map_single(ar->dev, tresp, *resp_len,
1376                                             DMA_FROM_DEVICE);
1377                 ret = dma_mapping_error(ar->dev, resp_paddr);
1378                 if (ret)
1379                         goto err_req;
1380
1381                 xfer.wait_for_resp = true;
1382                 xfer.resp_len = 0;
1383
1384                 ath10k_ce_recv_buf_enqueue(ce_rx, &xfer, resp_paddr);
1385         }
1386
1387         ret = ath10k_ce_send(ce_tx, &xfer, req_paddr, req_len, -1, 0);
1388         if (ret)
1389                 goto err_resp;
1390
1391         ret = ath10k_pci_bmi_wait(ce_tx, ce_rx, &xfer);
1392         if (ret) {
1393                 u32 unused_buffer;
1394                 unsigned int unused_nbytes;
1395                 unsigned int unused_id;
1396
1397                 ath10k_ce_cancel_send_next(ce_tx, NULL, &unused_buffer,
1398                                            &unused_nbytes, &unused_id);
1399         } else {
1400                 /* non-zero means we did not time out */
1401                 ret = 0;
1402         }
1403
1404 err_resp:
1405         if (resp) {
1406                 u32 unused_buffer;
1407
1408                 ath10k_ce_revoke_recv_next(ce_rx, NULL, &unused_buffer);
1409                 dma_unmap_single(ar->dev, resp_paddr,
1410                                  *resp_len, DMA_FROM_DEVICE);
1411         }
1412 err_req:
1413         dma_unmap_single(ar->dev, req_paddr, req_len, DMA_TO_DEVICE);
1414
1415         if (ret == 0 && resp_len) {
1416                 *resp_len = min(*resp_len, xfer.resp_len);
1417                 memcpy(resp, tresp, xfer.resp_len);
1418         }
1419 err_dma:
1420         kfree(treq);
1421         kfree(tresp);
1422
1423         return ret;
1424 }
1425
1426 static void ath10k_pci_bmi_send_done(struct ath10k_ce_pipe *ce_state)
1427 {
1428         struct bmi_xfer *xfer;
1429         u32 ce_data;
1430         unsigned int nbytes;
1431         unsigned int transfer_id;
1432
1433         if (ath10k_ce_completed_send_next(ce_state, (void **)&xfer, &ce_data,
1434                                           &nbytes, &transfer_id))
1435                 return;
1436
1437         xfer->tx_done = true;
1438 }
1439
1440 static void ath10k_pci_bmi_recv_data(struct ath10k_ce_pipe *ce_state)
1441 {
1442         struct bmi_xfer *xfer;
1443         u32 ce_data;
1444         unsigned int nbytes;
1445         unsigned int transfer_id;
1446         unsigned int flags;
1447
1448         if (ath10k_ce_completed_recv_next(ce_state, (void **)&xfer, &ce_data,
1449                                           &nbytes, &transfer_id, &flags))
1450                 return;
1451
1452         if (!xfer->wait_for_resp) {
1453                 ath10k_warn("unexpected: BMI data received; ignoring\n");
1454                 return;
1455         }
1456
1457         xfer->resp_len = nbytes;
1458         xfer->rx_done = true;
1459 }
1460
1461 static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
1462                                struct ath10k_ce_pipe *rx_pipe,
1463                                struct bmi_xfer *xfer)
1464 {
1465         unsigned long timeout = jiffies + BMI_COMMUNICATION_TIMEOUT_HZ;
1466
1467         while (time_before_eq(jiffies, timeout)) {
1468                 ath10k_pci_bmi_send_done(tx_pipe);
1469                 ath10k_pci_bmi_recv_data(rx_pipe);
1470
1471                 if (xfer->tx_done && (xfer->rx_done == xfer->wait_for_resp))
1472                         return 0;
1473
1474                 schedule();
1475         }
1476
1477         return -ETIMEDOUT;
1478 }
1479
1480 /*
1481  * Map from service/endpoint to Copy Engine.
1482  * This table is derived from the CE_PCI TABLE, above.
1483  * It is passed to the Target at startup for use by firmware.
1484  */
1485 static const struct service_to_pipe target_service_to_ce_map_wlan[] = {
1486         {
1487                  ATH10K_HTC_SVC_ID_WMI_DATA_VO,
1488                  PIPEDIR_OUT,           /* out = UL = host -> target */
1489                  3,
1490         },
1491         {
1492                  ATH10K_HTC_SVC_ID_WMI_DATA_VO,
1493                  PIPEDIR_IN,            /* in = DL = target -> host */
1494                  2,
1495         },
1496         {
1497                  ATH10K_HTC_SVC_ID_WMI_DATA_BK,
1498                  PIPEDIR_OUT,           /* out = UL = host -> target */
1499                  3,
1500         },
1501         {
1502                  ATH10K_HTC_SVC_ID_WMI_DATA_BK,
1503                  PIPEDIR_IN,            /* in = DL = target -> host */
1504                  2,
1505         },
1506         {
1507                  ATH10K_HTC_SVC_ID_WMI_DATA_BE,
1508                  PIPEDIR_OUT,           /* out = UL = host -> target */
1509                  3,
1510         },
1511         {
1512                  ATH10K_HTC_SVC_ID_WMI_DATA_BE,
1513                  PIPEDIR_IN,            /* in = DL = target -> host */
1514                  2,
1515         },
1516         {
1517                  ATH10K_HTC_SVC_ID_WMI_DATA_VI,
1518                  PIPEDIR_OUT,           /* out = UL = host -> target */
1519                  3,
1520         },
1521         {
1522                  ATH10K_HTC_SVC_ID_WMI_DATA_VI,
1523                  PIPEDIR_IN,            /* in = DL = target -> host */
1524                  2,
1525         },
1526         {
1527                  ATH10K_HTC_SVC_ID_WMI_CONTROL,
1528                  PIPEDIR_OUT,           /* out = UL = host -> target */
1529                  3,
1530         },
1531         {
1532                  ATH10K_HTC_SVC_ID_WMI_CONTROL,
1533                  PIPEDIR_IN,            /* in = DL = target -> host */
1534                  2,
1535         },
1536         {
1537                  ATH10K_HTC_SVC_ID_RSVD_CTRL,
1538                  PIPEDIR_OUT,           /* out = UL = host -> target */
1539                  0,             /* could be moved to 3 (share with WMI) */
1540         },
1541         {
1542                  ATH10K_HTC_SVC_ID_RSVD_CTRL,
1543                  PIPEDIR_IN,            /* in = DL = target -> host */
1544                  1,
1545         },
1546         {
1547                  ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS,    /* not currently used */
1548                  PIPEDIR_OUT,           /* out = UL = host -> target */
1549                  0,
1550         },
1551         {
1552                  ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS,    /* not currently used */
1553                  PIPEDIR_IN,            /* in = DL = target -> host */
1554                  1,
1555         },
1556         {
1557                  ATH10K_HTC_SVC_ID_HTT_DATA_MSG,
1558                  PIPEDIR_OUT,           /* out = UL = host -> target */
1559                  4,
1560         },
1561         {
1562                  ATH10K_HTC_SVC_ID_HTT_DATA_MSG,
1563                  PIPEDIR_IN,            /* in = DL = target -> host */
1564                  1,
1565         },
1566
1567         /* (Additions here) */
1568
1569         {                               /* Must be last */
1570                  0,
1571                  0,
1572                  0,
1573         },
1574 };
1575
1576 /*
1577  * Send an interrupt to the device to wake up the Target CPU
1578  * so it has an opportunity to notice any changed state.
1579  */
1580 static int ath10k_pci_wake_target_cpu(struct ath10k *ar)
1581 {
1582         int ret;
1583         u32 core_ctrl;
1584
1585         ret = ath10k_pci_diag_read_access(ar, SOC_CORE_BASE_ADDRESS |
1586                                               CORE_CTRL_ADDRESS,
1587                                           &core_ctrl);
1588         if (ret) {
1589                 ath10k_warn("failed to read core_ctrl: %d\n", ret);
1590                 return ret;
1591         }
1592
1593         /* A_INUM_FIRMWARE interrupt to Target CPU */
1594         core_ctrl |= CORE_CTRL_CPU_INTR_MASK;
1595
1596         ret = ath10k_pci_diag_write_access(ar, SOC_CORE_BASE_ADDRESS |
1597                                                CORE_CTRL_ADDRESS,
1598                                            core_ctrl);
1599         if (ret) {
1600                 ath10k_warn("failed to set target CPU interrupt mask: %d\n",
1601                             ret);
1602                 return ret;
1603         }
1604
1605         return 0;
1606 }
1607
1608 static int ath10k_pci_init_config(struct ath10k *ar)
1609 {
1610         u32 interconnect_targ_addr;
1611         u32 pcie_state_targ_addr = 0;
1612         u32 pipe_cfg_targ_addr = 0;
1613         u32 svc_to_pipe_map = 0;
1614         u32 pcie_config_flags = 0;
1615         u32 ealloc_value;
1616         u32 ealloc_targ_addr;
1617         u32 flag2_value;
1618         u32 flag2_targ_addr;
1619         int ret = 0;
1620
1621         /* Download to Target the CE Config and the service-to-CE map */
1622         interconnect_targ_addr =
1623                 host_interest_item_address(HI_ITEM(hi_interconnect_state));
1624
1625         /* Supply Target-side CE configuration */
1626         ret = ath10k_pci_diag_read_access(ar, interconnect_targ_addr,
1627                                           &pcie_state_targ_addr);
1628         if (ret != 0) {
1629                 ath10k_err("Failed to get pcie state addr: %d\n", ret);
1630                 return ret;
1631         }
1632
1633         if (pcie_state_targ_addr == 0) {
1634                 ret = -EIO;
1635                 ath10k_err("Invalid pcie state addr\n");
1636                 return ret;
1637         }
1638
1639         ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
1640                                           offsetof(struct pcie_state,
1641                                                    pipe_cfg_addr),
1642                                           &pipe_cfg_targ_addr);
1643         if (ret != 0) {
1644                 ath10k_err("Failed to get pipe cfg addr: %d\n", ret);
1645                 return ret;
1646         }
1647
1648         if (pipe_cfg_targ_addr == 0) {
1649                 ret = -EIO;
1650                 ath10k_err("Invalid pipe cfg addr\n");
1651                 return ret;
1652         }
1653
1654         ret = ath10k_pci_diag_write_mem(ar, pipe_cfg_targ_addr,
1655                                  target_ce_config_wlan,
1656                                  sizeof(target_ce_config_wlan));
1657
1658         if (ret != 0) {
1659                 ath10k_err("Failed to write pipe cfg: %d\n", ret);
1660                 return ret;
1661         }
1662
1663         ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
1664                                           offsetof(struct pcie_state,
1665                                                    svc_to_pipe_map),
1666                                           &svc_to_pipe_map);
1667         if (ret != 0) {
1668                 ath10k_err("Failed to get svc/pipe map: %d\n", ret);
1669                 return ret;
1670         }
1671
1672         if (svc_to_pipe_map == 0) {
1673                 ret = -EIO;
1674                 ath10k_err("Invalid svc_to_pipe map\n");
1675                 return ret;
1676         }
1677
1678         ret = ath10k_pci_diag_write_mem(ar, svc_to_pipe_map,
1679                                  target_service_to_ce_map_wlan,
1680                                  sizeof(target_service_to_ce_map_wlan));
1681         if (ret != 0) {
1682                 ath10k_err("Failed to write svc/pipe map: %d\n", ret);
1683                 return ret;
1684         }
1685
1686         ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
1687                                           offsetof(struct pcie_state,
1688                                                    config_flags),
1689                                           &pcie_config_flags);
1690         if (ret != 0) {
1691                 ath10k_err("Failed to get pcie config_flags: %d\n", ret);
1692                 return ret;
1693         }
1694
1695         pcie_config_flags &= ~PCIE_CONFIG_FLAG_ENABLE_L1;
1696
1697         ret = ath10k_pci_diag_write_mem(ar, pcie_state_targ_addr +
1698                                  offsetof(struct pcie_state, config_flags),
1699                                  &pcie_config_flags,
1700                                  sizeof(pcie_config_flags));
1701         if (ret != 0) {
1702                 ath10k_err("Failed to write pcie config_flags: %d\n", ret);
1703                 return ret;
1704         }
1705
1706         /* configure early allocation */
1707         ealloc_targ_addr = host_interest_item_address(HI_ITEM(hi_early_alloc));
1708
1709         ret = ath10k_pci_diag_read_access(ar, ealloc_targ_addr, &ealloc_value);
1710         if (ret != 0) {
1711                 ath10k_err("Faile to get early alloc val: %d\n", ret);
1712                 return ret;
1713         }
1714
1715         /* first bank is switched to IRAM */
1716         ealloc_value |= ((HI_EARLY_ALLOC_MAGIC << HI_EARLY_ALLOC_MAGIC_SHIFT) &
1717                          HI_EARLY_ALLOC_MAGIC_MASK);
1718         ealloc_value |= ((1 << HI_EARLY_ALLOC_IRAM_BANKS_SHIFT) &
1719                          HI_EARLY_ALLOC_IRAM_BANKS_MASK);
1720
1721         ret = ath10k_pci_diag_write_access(ar, ealloc_targ_addr, ealloc_value);
1722         if (ret != 0) {
1723                 ath10k_err("Failed to set early alloc val: %d\n", ret);
1724                 return ret;
1725         }
1726
1727         /* Tell Target to proceed with initialization */
1728         flag2_targ_addr = host_interest_item_address(HI_ITEM(hi_option_flag2));
1729
1730         ret = ath10k_pci_diag_read_access(ar, flag2_targ_addr, &flag2_value);
1731         if (ret != 0) {
1732                 ath10k_err("Failed to get option val: %d\n", ret);
1733                 return ret;
1734         }
1735
1736         flag2_value |= HI_OPTION_EARLY_CFG_DONE;
1737
1738         ret = ath10k_pci_diag_write_access(ar, flag2_targ_addr, flag2_value);
1739         if (ret != 0) {
1740                 ath10k_err("Failed to set option val: %d\n", ret);
1741                 return ret;
1742         }
1743
1744         return 0;
1745 }
1746
1747 static int ath10k_pci_alloc_ce(struct ath10k *ar)
1748 {
1749         int i, ret;
1750
1751         for (i = 0; i < CE_COUNT; i++) {
1752                 ret = ath10k_ce_alloc_pipe(ar, i, &host_ce_config_wlan[i]);
1753                 if (ret) {
1754                         ath10k_err("failed to allocate copy engine pipe %d: %d\n",
1755                                    i, ret);
1756                         return ret;
1757                 }
1758         }
1759
1760         return 0;
1761 }
1762
1763 static void ath10k_pci_free_ce(struct ath10k *ar)
1764 {
1765         int i;
1766
1767         for (i = 0; i < CE_COUNT; i++)
1768                 ath10k_ce_free_pipe(ar, i);
1769 }
1770
1771 static int ath10k_pci_ce_init(struct ath10k *ar)
1772 {
1773         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1774         struct ath10k_pci_pipe *pipe_info;
1775         const struct ce_attr *attr;
1776         int pipe_num, ret;
1777
1778         for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
1779                 pipe_info = &ar_pci->pipe_info[pipe_num];
1780                 pipe_info->ce_hdl = &ar_pci->ce_states[pipe_num];
1781                 pipe_info->pipe_num = pipe_num;
1782                 pipe_info->hif_ce_state = ar;
1783                 attr = &host_ce_config_wlan[pipe_num];
1784
1785                 ret = ath10k_ce_init_pipe(ar, pipe_num, attr,
1786                                           ath10k_pci_ce_send_done,
1787                                           ath10k_pci_ce_recv_data);
1788                 if (ret) {
1789                         ath10k_err("failed to initialize copy engine pipe %d: %d\n",
1790                                    pipe_num, ret);
1791                         return ret;
1792                 }
1793
1794                 if (pipe_num == CE_COUNT - 1) {
1795                         /*
1796                          * Reserve the ultimate CE for
1797                          * diagnostic Window support
1798                          */
1799                         ar_pci->ce_diag = pipe_info->ce_hdl;
1800                         continue;
1801                 }
1802
1803                 pipe_info->buf_sz = (size_t) (attr->src_sz_max);
1804         }
1805
1806         return 0;
1807 }
1808
1809 static void ath10k_pci_fw_interrupt_handler(struct ath10k *ar)
1810 {
1811         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1812         u32 fw_indicator;
1813
1814         fw_indicator = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
1815
1816         if (fw_indicator & FW_IND_EVENT_PENDING) {
1817                 /* ACK: clear Target-side pending event */
1818                 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS,
1819                                    fw_indicator & ~FW_IND_EVENT_PENDING);
1820
1821                 if (ar_pci->started) {
1822                         ath10k_pci_fw_crashed_dump(ar);
1823                 } else {
1824                         /*
1825                          * Probable Target failure before we're prepared
1826                          * to handle it.  Generally unexpected.
1827                          */
1828                         ath10k_warn("early firmware event indicated\n");
1829                 }
1830         }
1831 }
1832
1833 /* this function effectively clears target memory controller assert line */
1834 static void ath10k_pci_warm_reset_si0(struct ath10k *ar)
1835 {
1836         u32 val;
1837
1838         val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1839         ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
1840                                val | SOC_RESET_CONTROL_SI0_RST_MASK);
1841         val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1842
1843         msleep(10);
1844
1845         val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1846         ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
1847                                val & ~SOC_RESET_CONTROL_SI0_RST_MASK);
1848         val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1849
1850         msleep(10);
1851 }
1852
1853 static int ath10k_pci_warm_reset(struct ath10k *ar)
1854 {
1855         u32 val;
1856
1857         ath10k_dbg(ATH10K_DBG_BOOT, "boot warm reset\n");
1858
1859         /* debug */
1860         val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1861                                 PCIE_INTR_CAUSE_ADDRESS);
1862         ath10k_dbg(ATH10K_DBG_BOOT, "boot host cpu intr cause: 0x%08x\n", val);
1863
1864         val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1865                                 CPU_INTR_ADDRESS);
1866         ath10k_dbg(ATH10K_DBG_BOOT, "boot target cpu intr cause: 0x%08x\n",
1867                    val);
1868
1869         /* disable pending irqs */
1870         ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
1871                            PCIE_INTR_ENABLE_ADDRESS, 0);
1872
1873         ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
1874                            PCIE_INTR_CLR_ADDRESS, ~0);
1875
1876         msleep(100);
1877
1878         /* clear fw indicator */
1879         ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, 0);
1880
1881         /* clear target LF timer interrupts */
1882         val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1883                                 SOC_LF_TIMER_CONTROL0_ADDRESS);
1884         ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS +
1885                            SOC_LF_TIMER_CONTROL0_ADDRESS,
1886                            val & ~SOC_LF_TIMER_CONTROL0_ENABLE_MASK);
1887
1888         /* reset CE */
1889         val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1890                                 SOC_RESET_CONTROL_ADDRESS);
1891         ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1892                            val | SOC_RESET_CONTROL_CE_RST_MASK);
1893         val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1894                                 SOC_RESET_CONTROL_ADDRESS);
1895         msleep(10);
1896
1897         /* unreset CE */
1898         ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1899                            val & ~SOC_RESET_CONTROL_CE_RST_MASK);
1900         val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1901                                 SOC_RESET_CONTROL_ADDRESS);
1902         msleep(10);
1903
1904         ath10k_pci_warm_reset_si0(ar);
1905
1906         /* debug */
1907         val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1908                                 PCIE_INTR_CAUSE_ADDRESS);
1909         ath10k_dbg(ATH10K_DBG_BOOT, "boot host cpu intr cause: 0x%08x\n", val);
1910
1911         val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1912                                 CPU_INTR_ADDRESS);
1913         ath10k_dbg(ATH10K_DBG_BOOT, "boot target cpu intr cause: 0x%08x\n",
1914                    val);
1915
1916         /* CPU warm reset */
1917         val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1918                                 SOC_RESET_CONTROL_ADDRESS);
1919         ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1920                            val | SOC_RESET_CONTROL_CPU_WARM_RST_MASK);
1921
1922         val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1923                                 SOC_RESET_CONTROL_ADDRESS);
1924         ath10k_dbg(ATH10K_DBG_BOOT, "boot target reset state: 0x%08x\n", val);
1925
1926         msleep(100);
1927
1928         ath10k_dbg(ATH10K_DBG_BOOT, "boot warm reset complete\n");
1929
1930         return 0;
1931 }
1932
1933 static int __ath10k_pci_hif_power_up(struct ath10k *ar, bool cold_reset)
1934 {
1935         int ret;
1936
1937         /*
1938          * Bring the target up cleanly.
1939          *
1940          * The target may be in an undefined state with an AUX-powered Target
1941          * and a Host in WoW mode. If the Host crashes, loses power, or is
1942          * restarted (without unloading the driver) then the Target is left
1943          * (aux) powered and running. On a subsequent driver load, the Target
1944          * is in an unexpected state. We try to catch that here in order to
1945          * reset the Target and retry the probe.
1946          */
1947         if (cold_reset)
1948                 ret = ath10k_pci_cold_reset(ar);
1949         else
1950                 ret = ath10k_pci_warm_reset(ar);
1951
1952         if (ret) {
1953                 ath10k_err("failed to reset target: %d\n", ret);
1954                 goto err;
1955         }
1956
1957         ret = ath10k_pci_ce_init(ar);
1958         if (ret) {
1959                 ath10k_err("failed to initialize CE: %d\n", ret);
1960                 goto err;
1961         }
1962
1963         ret = ath10k_pci_request_early_irq(ar);
1964         if (ret) {
1965                 ath10k_err("failed to request early irq: %d\n", ret);
1966                 goto err_ce;
1967         }
1968
1969         ret = ath10k_pci_wait_for_target_init(ar);
1970         if (ret) {
1971                 ath10k_err("failed to wait for target to init: %d\n", ret);
1972                 goto err_free_early_irq;
1973         }
1974
1975         ret = ath10k_pci_init_config(ar);
1976         if (ret) {
1977                 ath10k_err("failed to setup init config: %d\n", ret);
1978                 goto err_free_early_irq;
1979         }
1980
1981         ret = ath10k_pci_wake_target_cpu(ar);
1982         if (ret) {
1983                 ath10k_err("could not wake up target CPU: %d\n", ret);
1984                 goto err_free_early_irq;
1985         }
1986
1987         return 0;
1988
1989 err_free_early_irq:
1990         ath10k_pci_free_early_irq(ar);
1991 err_ce:
1992         ath10k_pci_ce_deinit(ar);
1993         ath10k_pci_warm_reset(ar);
1994 err:
1995         return ret;
1996 }
1997
1998 static int ath10k_pci_hif_power_up_warm(struct ath10k *ar)
1999 {
2000         int i, ret;
2001
2002         /*
2003          * Sometime warm reset succeeds after retries.
2004          *
2005          * FIXME: It might be possible to tune ath10k_pci_warm_reset() to work
2006          * at first try.
2007          */
2008         for (i = 0; i < ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS; i++) {
2009                 ret = __ath10k_pci_hif_power_up(ar, false);
2010                 if (ret == 0)
2011                         break;
2012
2013                 ath10k_warn("failed to warm reset (attempt %d out of %d): %d\n",
2014                             i + 1, ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS, ret);
2015         }
2016
2017         return ret;
2018 }
2019
2020 static int ath10k_pci_hif_power_up(struct ath10k *ar)
2021 {
2022         int ret;
2023
2024         ath10k_dbg(ATH10K_DBG_BOOT, "boot hif power up\n");
2025
2026         /*
2027          * Hardware CUS232 version 2 has some issues with cold reset and the
2028          * preferred (and safer) way to perform a device reset is through a
2029          * warm reset.
2030          *
2031          * Warm reset doesn't always work though so fall back to cold reset may
2032          * be necessary.
2033          */
2034         ret = ath10k_pci_hif_power_up_warm(ar);
2035         if (ret) {
2036                 ath10k_warn("failed to power up target using warm reset: %d\n",
2037                             ret);
2038
2039                 if (ath10k_pci_reset_mode == ATH10K_PCI_RESET_WARM_ONLY)
2040                         return ret;
2041
2042                 ath10k_warn("trying cold reset\n");
2043
2044                 ret = __ath10k_pci_hif_power_up(ar, true);
2045                 if (ret) {
2046                         ath10k_err("failed to power up target using cold reset too (%d)\n",
2047                                    ret);
2048                         return ret;
2049                 }
2050         }
2051
2052         return 0;
2053 }
2054
2055 static void ath10k_pci_hif_power_down(struct ath10k *ar)
2056 {
2057         ath10k_dbg(ATH10K_DBG_BOOT, "boot hif power down\n");
2058
2059         ath10k_pci_free_early_irq(ar);
2060         ath10k_pci_kill_tasklet(ar);
2061         ath10k_pci_warm_reset(ar);
2062 }
2063
2064 #ifdef CONFIG_PM
2065
2066 #define ATH10K_PCI_PM_CONTROL 0x44
2067
2068 static int ath10k_pci_hif_suspend(struct ath10k *ar)
2069 {
2070         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2071         struct pci_dev *pdev = ar_pci->pdev;
2072         u32 val;
2073
2074         pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
2075
2076         if ((val & 0x000000ff) != 0x3) {
2077                 pci_save_state(pdev);
2078                 pci_disable_device(pdev);
2079                 pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
2080                                        (val & 0xffffff00) | 0x03);
2081         }
2082
2083         return 0;
2084 }
2085
2086 static int ath10k_pci_hif_resume(struct ath10k *ar)
2087 {
2088         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2089         struct pci_dev *pdev = ar_pci->pdev;
2090         u32 val;
2091
2092         pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
2093
2094         if ((val & 0x000000ff) != 0) {
2095                 pci_restore_state(pdev);
2096                 pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
2097                                        val & 0xffffff00);
2098                 /*
2099                  * Suspend/Resume resets the PCI configuration space,
2100                  * so we have to re-disable the RETRY_TIMEOUT register (0x41)
2101                  * to keep PCI Tx retries from interfering with C3 CPU state
2102                  */
2103                 pci_read_config_dword(pdev, 0x40, &val);
2104
2105                 if ((val & 0x0000ff00) != 0)
2106                         pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
2107         }
2108
2109         return 0;
2110 }
2111 #endif
2112
2113 static const struct ath10k_hif_ops ath10k_pci_hif_ops = {
2114         .tx_sg                  = ath10k_pci_hif_tx_sg,
2115         .exchange_bmi_msg       = ath10k_pci_hif_exchange_bmi_msg,
2116         .start                  = ath10k_pci_hif_start,
2117         .stop                   = ath10k_pci_hif_stop,
2118         .map_service_to_pipe    = ath10k_pci_hif_map_service_to_pipe,
2119         .get_default_pipe       = ath10k_pci_hif_get_default_pipe,
2120         .send_complete_check    = ath10k_pci_hif_send_complete_check,
2121         .set_callbacks          = ath10k_pci_hif_set_callbacks,
2122         .get_free_queue_number  = ath10k_pci_hif_get_free_queue_number,
2123         .power_up               = ath10k_pci_hif_power_up,
2124         .power_down             = ath10k_pci_hif_power_down,
2125 #ifdef CONFIG_PM
2126         .suspend                = ath10k_pci_hif_suspend,
2127         .resume                 = ath10k_pci_hif_resume,
2128 #endif
2129 };
2130
2131 static void ath10k_pci_ce_tasklet(unsigned long ptr)
2132 {
2133         struct ath10k_pci_pipe *pipe = (struct ath10k_pci_pipe *)ptr;
2134         struct ath10k_pci *ar_pci = pipe->ar_pci;
2135
2136         ath10k_ce_per_engine_service(ar_pci->ar, pipe->pipe_num);
2137 }
2138
2139 static void ath10k_msi_err_tasklet(unsigned long data)
2140 {
2141         struct ath10k *ar = (struct ath10k *)data;
2142
2143         ath10k_pci_fw_interrupt_handler(ar);
2144 }
2145
2146 /*
2147  * Handler for a per-engine interrupt on a PARTICULAR CE.
2148  * This is used in cases where each CE has a private MSI interrupt.
2149  */
2150 static irqreturn_t ath10k_pci_per_engine_handler(int irq, void *arg)
2151 {
2152         struct ath10k *ar = arg;
2153         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2154         int ce_id = irq - ar_pci->pdev->irq - MSI_ASSIGN_CE_INITIAL;
2155
2156         if (ce_id < 0 || ce_id >= ARRAY_SIZE(ar_pci->pipe_info)) {
2157                 ath10k_warn("unexpected/invalid irq %d ce_id %d\n", irq, ce_id);
2158                 return IRQ_HANDLED;
2159         }
2160
2161         /*
2162          * NOTE: We are able to derive ce_id from irq because we
2163          * use a one-to-one mapping for CE's 0..5.
2164          * CE's 6 & 7 do not use interrupts at all.
2165          *
2166          * This mapping must be kept in sync with the mapping
2167          * used by firmware.
2168          */
2169         tasklet_schedule(&ar_pci->pipe_info[ce_id].intr);
2170         return IRQ_HANDLED;
2171 }
2172
2173 static irqreturn_t ath10k_pci_msi_fw_handler(int irq, void *arg)
2174 {
2175         struct ath10k *ar = arg;
2176         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2177
2178         tasklet_schedule(&ar_pci->msi_fw_err);
2179         return IRQ_HANDLED;
2180 }
2181
2182 /*
2183  * Top-level interrupt handler for all PCI interrupts from a Target.
2184  * When a block of MSI interrupts is allocated, this top-level handler
2185  * is not used; instead, we directly call the correct sub-handler.
2186  */
2187 static irqreturn_t ath10k_pci_interrupt_handler(int irq, void *arg)
2188 {
2189         struct ath10k *ar = arg;
2190         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2191
2192         if (ar_pci->num_msi_intrs == 0) {
2193                 if (!ath10k_pci_irq_pending(ar))
2194                         return IRQ_NONE;
2195
2196                 ath10k_pci_disable_and_clear_legacy_irq(ar);
2197         }
2198
2199         tasklet_schedule(&ar_pci->intr_tq);
2200
2201         return IRQ_HANDLED;
2202 }
2203
2204 static void ath10k_pci_early_irq_tasklet(unsigned long data)
2205 {
2206         struct ath10k *ar = (struct ath10k *)data;
2207         u32 fw_ind;
2208
2209         fw_ind = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
2210         if (fw_ind & FW_IND_EVENT_PENDING) {
2211                 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS,
2212                                    fw_ind & ~FW_IND_EVENT_PENDING);
2213                 ath10k_pci_fw_crashed_dump(ar);
2214         }
2215
2216         ath10k_pci_enable_legacy_irq(ar);
2217 }
2218
2219 static void ath10k_pci_tasklet(unsigned long data)
2220 {
2221         struct ath10k *ar = (struct ath10k *)data;
2222         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2223
2224         ath10k_pci_fw_interrupt_handler(ar); /* FIXME: Handle FW error */
2225         ath10k_ce_per_engine_service_any(ar);
2226
2227         /* Re-enable legacy irq that was disabled in the irq handler */
2228         if (ar_pci->num_msi_intrs == 0)
2229                 ath10k_pci_enable_legacy_irq(ar);
2230 }
2231
2232 static int ath10k_pci_request_irq_msix(struct ath10k *ar)
2233 {
2234         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2235         int ret, i;
2236
2237         ret = request_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW,
2238                           ath10k_pci_msi_fw_handler,
2239                           IRQF_SHARED, "ath10k_pci", ar);
2240         if (ret) {
2241                 ath10k_warn("failed to request MSI-X fw irq %d: %d\n",
2242                             ar_pci->pdev->irq + MSI_ASSIGN_FW, ret);
2243                 return ret;
2244         }
2245
2246         for (i = MSI_ASSIGN_CE_INITIAL; i <= MSI_ASSIGN_CE_MAX; i++) {
2247                 ret = request_irq(ar_pci->pdev->irq + i,
2248                                   ath10k_pci_per_engine_handler,
2249                                   IRQF_SHARED, "ath10k_pci", ar);
2250                 if (ret) {
2251                         ath10k_warn("failed to request MSI-X ce irq %d: %d\n",
2252                                     ar_pci->pdev->irq + i, ret);
2253
2254                         for (i--; i >= MSI_ASSIGN_CE_INITIAL; i--)
2255                                 free_irq(ar_pci->pdev->irq + i, ar);
2256
2257                         free_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW, ar);
2258                         return ret;
2259                 }
2260         }
2261
2262         return 0;
2263 }
2264
2265 static int ath10k_pci_request_irq_msi(struct ath10k *ar)
2266 {
2267         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2268         int ret;
2269
2270         ret = request_irq(ar_pci->pdev->irq,
2271                           ath10k_pci_interrupt_handler,
2272                           IRQF_SHARED, "ath10k_pci", ar);
2273         if (ret) {
2274                 ath10k_warn("failed to request MSI irq %d: %d\n",
2275                             ar_pci->pdev->irq, ret);
2276                 return ret;
2277         }
2278
2279         return 0;
2280 }
2281
2282 static int ath10k_pci_request_irq_legacy(struct ath10k *ar)
2283 {
2284         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2285         int ret;
2286
2287         ret = request_irq(ar_pci->pdev->irq,
2288                           ath10k_pci_interrupt_handler,
2289                           IRQF_SHARED, "ath10k_pci", ar);
2290         if (ret) {
2291                 ath10k_warn("failed to request legacy irq %d: %d\n",
2292                             ar_pci->pdev->irq, ret);
2293                 return ret;
2294         }
2295
2296         return 0;
2297 }
2298
2299 static int ath10k_pci_request_irq(struct ath10k *ar)
2300 {
2301         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2302
2303         switch (ar_pci->num_msi_intrs) {
2304         case 0:
2305                 return ath10k_pci_request_irq_legacy(ar);
2306         case 1:
2307                 return ath10k_pci_request_irq_msi(ar);
2308         case MSI_NUM_REQUEST:
2309                 return ath10k_pci_request_irq_msix(ar);
2310         }
2311
2312         ath10k_warn("unknown irq configuration upon request\n");
2313         return -EINVAL;
2314 }
2315
2316 static void ath10k_pci_free_irq(struct ath10k *ar)
2317 {
2318         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2319         int i;
2320
2321         /* There's at least one interrupt irregardless whether its legacy INTR
2322          * or MSI or MSI-X */
2323         for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
2324                 free_irq(ar_pci->pdev->irq + i, ar);
2325 }
2326
2327 static void ath10k_pci_init_irq_tasklets(struct ath10k *ar)
2328 {
2329         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2330         int i;
2331
2332         tasklet_init(&ar_pci->intr_tq, ath10k_pci_tasklet, (unsigned long)ar);
2333         tasklet_init(&ar_pci->msi_fw_err, ath10k_msi_err_tasklet,
2334                      (unsigned long)ar);
2335         tasklet_init(&ar_pci->early_irq_tasklet, ath10k_pci_early_irq_tasklet,
2336                      (unsigned long)ar);
2337
2338         for (i = 0; i < CE_COUNT; i++) {
2339                 ar_pci->pipe_info[i].ar_pci = ar_pci;
2340                 tasklet_init(&ar_pci->pipe_info[i].intr, ath10k_pci_ce_tasklet,
2341                              (unsigned long)&ar_pci->pipe_info[i]);
2342         }
2343 }
2344
2345 static int ath10k_pci_init_irq(struct ath10k *ar)
2346 {
2347         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2348         int ret;
2349
2350         ath10k_pci_init_irq_tasklets(ar);
2351
2352         if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_AUTO)
2353                 ath10k_info("limiting irq mode to: %d\n", ath10k_pci_irq_mode);
2354
2355         /* Try MSI-X */
2356         if (ath10k_pci_irq_mode == ATH10K_PCI_IRQ_AUTO) {
2357                 ar_pci->num_msi_intrs = MSI_NUM_REQUEST;
2358                 ret = pci_enable_msi_range(ar_pci->pdev, ar_pci->num_msi_intrs,
2359                                                          ar_pci->num_msi_intrs);
2360                 if (ret > 0)
2361                         return 0;
2362
2363                 /* fall-through */
2364         }
2365
2366         /* Try MSI */
2367         if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_LEGACY) {
2368                 ar_pci->num_msi_intrs = 1;
2369                 ret = pci_enable_msi(ar_pci->pdev);
2370                 if (ret == 0)
2371                         return 0;
2372
2373                 /* fall-through */
2374         }
2375
2376         /* Try legacy irq
2377          *
2378          * A potential race occurs here: The CORE_BASE write
2379          * depends on target correctly decoding AXI address but
2380          * host won't know when target writes BAR to CORE_CTRL.
2381          * This write might get lost if target has NOT written BAR.
2382          * For now, fix the race by repeating the write in below
2383          * synchronization checking. */
2384         ar_pci->num_msi_intrs = 0;
2385
2386         ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2387                            PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
2388
2389         return 0;
2390 }
2391
2392 static void ath10k_pci_deinit_irq_legacy(struct ath10k *ar)
2393 {
2394         ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2395                            0);
2396 }
2397
2398 static int ath10k_pci_deinit_irq(struct ath10k *ar)
2399 {
2400         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2401
2402         switch (ar_pci->num_msi_intrs) {
2403         case 0:
2404                 ath10k_pci_deinit_irq_legacy(ar);
2405                 return 0;
2406         case 1:
2407                 /* fall-through */
2408         case MSI_NUM_REQUEST:
2409                 pci_disable_msi(ar_pci->pdev);
2410                 return 0;
2411         default:
2412                 pci_disable_msi(ar_pci->pdev);
2413         }
2414
2415         ath10k_warn("unknown irq configuration upon deinit\n");
2416         return -EINVAL;
2417 }
2418
2419 static int ath10k_pci_wait_for_target_init(struct ath10k *ar)
2420 {
2421         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2422         unsigned long timeout;
2423         u32 val;
2424
2425         ath10k_dbg(ATH10K_DBG_BOOT, "boot waiting target to initialise\n");
2426
2427         timeout = jiffies + msecs_to_jiffies(ATH10K_PCI_TARGET_WAIT);
2428
2429         do {
2430                 val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
2431
2432                 ath10k_dbg(ATH10K_DBG_BOOT, "boot target indicator %x\n", val);
2433
2434                 /* target should never return this */
2435                 if (val == 0xffffffff)
2436                         continue;
2437
2438                 /* the device has crashed so don't bother trying anymore */
2439                 if (val & FW_IND_EVENT_PENDING)
2440                         break;
2441
2442                 if (val & FW_IND_INITIALIZED)
2443                         break;
2444
2445                 if (ar_pci->num_msi_intrs == 0)
2446                         /* Fix potential race by repeating CORE_BASE writes */
2447                         ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
2448                                            PCIE_INTR_ENABLE_ADDRESS,
2449                                            PCIE_INTR_FIRMWARE_MASK |
2450                                            PCIE_INTR_CE_MASK_ALL);
2451
2452                 mdelay(10);
2453         } while (time_before(jiffies, timeout));
2454
2455         if (val == 0xffffffff) {
2456                 ath10k_err("failed to read device register, device is gone\n");
2457                 return -EIO;
2458         }
2459
2460         if (val & FW_IND_EVENT_PENDING) {
2461                 ath10k_warn("device has crashed during init\n");
2462                 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS,
2463                                    val & ~FW_IND_EVENT_PENDING);
2464                 ath10k_pci_fw_crashed_dump(ar);
2465                 return -ECOMM;
2466         }
2467
2468         if (!(val & FW_IND_INITIALIZED)) {
2469                 ath10k_err("failed to receive initialized event from target: %08x\n",
2470                            val);
2471                 return -ETIMEDOUT;
2472         }
2473
2474         ath10k_dbg(ATH10K_DBG_BOOT, "boot target initialised\n");
2475         return 0;
2476 }
2477
2478 static int ath10k_pci_cold_reset(struct ath10k *ar)
2479 {
2480         int i;
2481         u32 val;
2482
2483         ath10k_dbg(ATH10K_DBG_BOOT, "boot cold reset\n");
2484
2485         /* Put Target, including PCIe, into RESET. */
2486         val = ath10k_pci_reg_read32(ar, SOC_GLOBAL_RESET_ADDRESS);
2487         val |= 1;
2488         ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
2489
2490         for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
2491                 if (ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
2492                                           RTC_STATE_COLD_RESET_MASK)
2493                         break;
2494                 msleep(1);
2495         }
2496
2497         /* Pull Target, including PCIe, out of RESET. */
2498         val &= ~1;
2499         ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
2500
2501         for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
2502                 if (!(ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
2503                                             RTC_STATE_COLD_RESET_MASK))
2504                         break;
2505                 msleep(1);
2506         }
2507
2508         ath10k_dbg(ATH10K_DBG_BOOT, "boot cold reset complete\n");
2509
2510         return 0;
2511 }
2512
2513 static int ath10k_pci_claim(struct ath10k *ar)
2514 {
2515         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2516         struct pci_dev *pdev = ar_pci->pdev;
2517         u32 lcr_val;
2518         int ret;
2519
2520         pci_set_drvdata(pdev, ar);
2521
2522         ret = pci_enable_device(pdev);
2523         if (ret) {
2524                 ath10k_err("failed to enable pci device: %d\n", ret);
2525                 return ret;
2526         }
2527
2528         ret = pci_request_region(pdev, BAR_NUM, "ath");
2529         if (ret) {
2530                 ath10k_err("failed to request region BAR%d: %d\n", BAR_NUM,
2531                            ret);
2532                 goto err_device;
2533         }
2534
2535         /* Target expects 32 bit DMA. Enforce it. */
2536         ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2537         if (ret) {
2538                 ath10k_err("failed to set dma mask to 32-bit: %d\n", ret);
2539                 goto err_region;
2540         }
2541
2542         ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2543         if (ret) {
2544                 ath10k_err("failed to set consistent dma mask to 32-bit: %d\n",
2545                            ret);
2546                 goto err_region;
2547         }
2548
2549         pci_set_master(pdev);
2550
2551         /* Workaround: Disable ASPM */
2552         pci_read_config_dword(pdev, 0x80, &lcr_val);
2553         pci_write_config_dword(pdev, 0x80, (lcr_val & 0xffffff00));
2554
2555         /* Arrange for access to Target SoC registers. */
2556         ar_pci->mem = pci_iomap(pdev, BAR_NUM, 0);
2557         if (!ar_pci->mem) {
2558                 ath10k_err("failed to iomap BAR%d\n", BAR_NUM);
2559                 ret = -EIO;
2560                 goto err_master;
2561         }
2562
2563         ath10k_dbg(ATH10K_DBG_BOOT, "boot pci_mem 0x%p\n", ar_pci->mem);
2564         return 0;
2565
2566 err_master:
2567         pci_clear_master(pdev);
2568
2569 err_region:
2570         pci_release_region(pdev, BAR_NUM);
2571
2572 err_device:
2573         pci_disable_device(pdev);
2574
2575         return ret;
2576 }
2577
2578 static void ath10k_pci_release(struct ath10k *ar)
2579 {
2580         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2581         struct pci_dev *pdev = ar_pci->pdev;
2582
2583         pci_iounmap(pdev, ar_pci->mem);
2584         pci_release_region(pdev, BAR_NUM);
2585         pci_clear_master(pdev);
2586         pci_disable_device(pdev);
2587 }
2588
2589 static int ath10k_pci_probe(struct pci_dev *pdev,
2590                             const struct pci_device_id *pci_dev)
2591 {
2592         int ret = 0;
2593         struct ath10k *ar;
2594         struct ath10k_pci *ar_pci;
2595         u32 chip_id;
2596
2597         ath10k_dbg(ATH10K_DBG_PCI, "pci probe\n");
2598
2599         ar = ath10k_core_create(sizeof(*ar_pci), &pdev->dev,
2600                                 &ath10k_pci_hif_ops);
2601         if (!ar) {
2602                 ath10k_err("failed to allocate core\n");
2603                 return -ENOMEM;
2604         }
2605
2606         ar_pci = ath10k_pci_priv(ar);
2607         ar_pci->pdev = pdev;
2608         ar_pci->dev = &pdev->dev;
2609         ar_pci->ar = ar;
2610
2611         spin_lock_init(&ar_pci->ce_lock);
2612
2613         ret = ath10k_pci_claim(ar);
2614         if (ret) {
2615                 ath10k_err("failed to claim device: %d\n", ret);
2616                 goto err_core_destroy;
2617         }
2618
2619         ret = ath10k_pci_wake(ar);
2620         if (ret) {
2621                 ath10k_err("failed to wake up: %d\n", ret);
2622                 goto err_release;
2623         }
2624
2625         chip_id = ath10k_pci_soc_read32(ar, SOC_CHIP_ID_ADDRESS);
2626         if (chip_id == 0xffffffff) {
2627                 ath10k_err("failed to get chip id\n");
2628                 goto err_sleep;
2629         }
2630
2631         ret = ath10k_pci_alloc_ce(ar);
2632         if (ret) {
2633                 ath10k_err("failed to allocate copy engine pipes: %d\n", ret);
2634                 goto err_sleep;
2635         }
2636
2637         ath10k_pci_ce_deinit(ar);
2638
2639         ret = ath10k_ce_disable_interrupts(ar);
2640         if (ret) {
2641                 ath10k_err("failed to disable copy engine interrupts: %d\n",
2642                            ret);
2643                 goto err_free_ce;
2644         }
2645
2646         ret = ath10k_pci_init_irq(ar);
2647         if (ret) {
2648                 ath10k_err("failed to init irqs: %d\n", ret);
2649                 goto err_free_ce;
2650         }
2651
2652         ath10k_info("pci irq %s interrupts %d irq_mode %d reset_mode %d\n",
2653                     ath10k_pci_get_irq_method(ar), ar_pci->num_msi_intrs,
2654                     ath10k_pci_irq_mode, ath10k_pci_reset_mode);
2655
2656         ret = ath10k_core_register(ar, chip_id);
2657         if (ret) {
2658                 ath10k_err("failed to register driver core: %d\n", ret);
2659                 goto err_deinit_irq;
2660         }
2661
2662         return 0;
2663
2664 err_deinit_irq:
2665         ath10k_pci_deinit_irq(ar);
2666
2667 err_free_ce:
2668         ath10k_pci_free_ce(ar);
2669
2670 err_sleep:
2671         ath10k_pci_sleep(ar);
2672
2673 err_release:
2674         ath10k_pci_release(ar);
2675
2676 err_core_destroy:
2677         ath10k_core_destroy(ar);
2678
2679         return ret;
2680 }
2681
2682 static void ath10k_pci_remove(struct pci_dev *pdev)
2683 {
2684         struct ath10k *ar = pci_get_drvdata(pdev);
2685         struct ath10k_pci *ar_pci;
2686
2687         ath10k_dbg(ATH10K_DBG_PCI, "pci remove\n");
2688
2689         if (!ar)
2690                 return;
2691
2692         ar_pci = ath10k_pci_priv(ar);
2693
2694         if (!ar_pci)
2695                 return;
2696
2697         ath10k_core_unregister(ar);
2698         ath10k_pci_deinit_irq(ar);
2699         ath10k_pci_ce_deinit(ar);
2700         ath10k_pci_free_ce(ar);
2701         ath10k_pci_sleep(ar);
2702         ath10k_pci_release(ar);
2703         ath10k_core_destroy(ar);
2704 }
2705
2706 MODULE_DEVICE_TABLE(pci, ath10k_pci_id_table);
2707
2708 static struct pci_driver ath10k_pci_driver = {
2709         .name = "ath10k_pci",
2710         .id_table = ath10k_pci_id_table,
2711         .probe = ath10k_pci_probe,
2712         .remove = ath10k_pci_remove,
2713 };
2714
2715 static int __init ath10k_pci_init(void)
2716 {
2717         int ret;
2718
2719         ret = pci_register_driver(&ath10k_pci_driver);
2720         if (ret)
2721                 ath10k_err("failed to register PCI driver: %d\n", ret);
2722
2723         return ret;
2724 }
2725 module_init(ath10k_pci_init);
2726
2727 static void __exit ath10k_pci_exit(void)
2728 {
2729         pci_unregister_driver(&ath10k_pci_driver);
2730 }
2731
2732 module_exit(ath10k_pci_exit);
2733
2734 MODULE_AUTHOR("Qualcomm Atheros");
2735 MODULE_DESCRIPTION("Driver support for Atheros QCA988X PCIe devices");
2736 MODULE_LICENSE("Dual BSD/GPL");
2737 MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_FW_3_FILE);
2738 MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_BOARD_DATA_FILE);