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