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