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