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