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