290a61afde1a35dd9cf097a0c0481b47ec37ad0f
[cascardo/linux.git] / drivers / net / wireless / ath / ath10k / pci.c
1 /*
2  * Copyright (c) 2005-2011 Atheros Communications Inc.
3  * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 #include <linux/pci.h>
19 #include <linux/module.h>
20 #include <linux/interrupt.h>
21 #include <linux/spinlock.h>
22 #include <linux/bitops.h>
23
24 #include "core.h"
25 #include "debug.h"
26
27 #include "targaddrs.h"
28 #include "bmi.h"
29
30 #include "hif.h"
31 #include "htc.h"
32
33 #include "ce.h"
34 #include "pci.h"
35
36 enum ath10k_pci_irq_mode {
37         ATH10K_PCI_IRQ_AUTO = 0,
38         ATH10K_PCI_IRQ_LEGACY = 1,
39         ATH10K_PCI_IRQ_MSI = 2,
40 };
41
42 enum ath10k_pci_reset_mode {
43         ATH10K_PCI_RESET_AUTO = 0,
44         ATH10K_PCI_RESET_WARM_ONLY = 1,
45 };
46
47 static unsigned int ath10k_pci_irq_mode = ATH10K_PCI_IRQ_AUTO;
48 static unsigned int ath10k_pci_reset_mode = ATH10K_PCI_RESET_AUTO;
49
50 module_param_named(irq_mode, ath10k_pci_irq_mode, uint, 0644);
51 MODULE_PARM_DESC(irq_mode, "0: auto, 1: legacy, 2: msi (default: 0)");
52
53 module_param_named(reset_mode, ath10k_pci_reset_mode, uint, 0644);
54 MODULE_PARM_DESC(reset_mode, "0: auto, 1: warm only (default: 0)");
55
56 /* how long wait to wait for target to initialise, in ms */
57 #define ATH10K_PCI_TARGET_WAIT 3000
58 #define ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS 3
59
60 static const struct pci_device_id ath10k_pci_id_table[] = {
61         { PCI_VDEVICE(ATHEROS, QCA988X_2_0_DEVICE_ID) }, /* PCI-E QCA988X V2 */
62         { PCI_VDEVICE(ATHEROS, QCA6164_2_1_DEVICE_ID) }, /* PCI-E QCA6164 V2.1 */
63         { PCI_VDEVICE(ATHEROS, QCA6174_2_1_DEVICE_ID) }, /* PCI-E QCA6174 V2.1 */
64         { PCI_VDEVICE(ATHEROS, QCA99X0_2_0_DEVICE_ID) }, /* PCI-E QCA99X0 V2 */
65         { PCI_VDEVICE(ATHEROS, QCA9377_1_0_DEVICE_ID) }, /* PCI-E QCA9377 V1 */
66         {0}
67 };
68
69 static const struct ath10k_pci_supp_chip ath10k_pci_supp_chips[] = {
70         /* QCA988X pre 2.0 chips are not supported because they need some nasty
71          * hacks. ath10k doesn't have them and these devices crash horribly
72          * because of that.
73          */
74         { QCA988X_2_0_DEVICE_ID, QCA988X_HW_2_0_CHIP_ID_REV },
75
76         { QCA6164_2_1_DEVICE_ID, QCA6174_HW_2_1_CHIP_ID_REV },
77         { QCA6164_2_1_DEVICE_ID, QCA6174_HW_2_2_CHIP_ID_REV },
78         { QCA6164_2_1_DEVICE_ID, QCA6174_HW_3_0_CHIP_ID_REV },
79         { QCA6164_2_1_DEVICE_ID, QCA6174_HW_3_1_CHIP_ID_REV },
80         { QCA6164_2_1_DEVICE_ID, QCA6174_HW_3_2_CHIP_ID_REV },
81
82         { QCA6174_2_1_DEVICE_ID, QCA6174_HW_2_1_CHIP_ID_REV },
83         { QCA6174_2_1_DEVICE_ID, QCA6174_HW_2_2_CHIP_ID_REV },
84         { QCA6174_2_1_DEVICE_ID, QCA6174_HW_3_0_CHIP_ID_REV },
85         { QCA6174_2_1_DEVICE_ID, QCA6174_HW_3_1_CHIP_ID_REV },
86         { QCA6174_2_1_DEVICE_ID, QCA6174_HW_3_2_CHIP_ID_REV },
87
88         { QCA99X0_2_0_DEVICE_ID, QCA99X0_HW_2_0_CHIP_ID_REV },
89
90         { QCA9377_1_0_DEVICE_ID, QCA9377_HW_1_0_CHIP_ID_REV },
91         { QCA9377_1_0_DEVICE_ID, QCA9377_HW_1_1_CHIP_ID_REV },
92 };
93
94 static void ath10k_pci_buffer_cleanup(struct ath10k *ar);
95 static int ath10k_pci_cold_reset(struct ath10k *ar);
96 static int ath10k_pci_safe_chip_reset(struct ath10k *ar);
97 static int ath10k_pci_init_irq(struct ath10k *ar);
98 static int ath10k_pci_deinit_irq(struct ath10k *ar);
99 static int ath10k_pci_request_irq(struct ath10k *ar);
100 static void ath10k_pci_free_irq(struct ath10k *ar);
101 static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
102                                struct ath10k_ce_pipe *rx_pipe,
103                                struct bmi_xfer *xfer);
104 static int ath10k_pci_qca99x0_chip_reset(struct ath10k *ar);
105 static void ath10k_pci_htc_tx_cb(struct ath10k_ce_pipe *ce_state);
106 static void ath10k_pci_htc_rx_cb(struct ath10k_ce_pipe *ce_state);
107 static void ath10k_pci_htt_tx_cb(struct ath10k_ce_pipe *ce_state);
108 static void ath10k_pci_htt_rx_cb(struct ath10k_ce_pipe *ce_state);
109 static void ath10k_pci_htt_htc_rx_cb(struct ath10k_ce_pipe *ce_state);
110 static void ath10k_pci_pktlog_rx_cb(struct ath10k_ce_pipe *ce_state);
111
112 static struct ce_attr host_ce_config_wlan[] = {
113         /* CE0: host->target HTC control and raw streams */
114         {
115                 .flags = CE_ATTR_FLAGS,
116                 .src_nentries = 16,
117                 .src_sz_max = 256,
118                 .dest_nentries = 0,
119                 .send_cb = ath10k_pci_htc_tx_cb,
120         },
121
122         /* CE1: target->host HTT + HTC control */
123         {
124                 .flags = CE_ATTR_FLAGS,
125                 .src_nentries = 0,
126                 .src_sz_max = 2048,
127                 .dest_nentries = 512,
128                 .recv_cb = ath10k_pci_htt_htc_rx_cb,
129         },
130
131         /* CE2: target->host WMI */
132         {
133                 .flags = CE_ATTR_FLAGS,
134                 .src_nentries = 0,
135                 .src_sz_max = 2048,
136                 .dest_nentries = 128,
137                 .recv_cb = ath10k_pci_htc_rx_cb,
138         },
139
140         /* CE3: host->target WMI */
141         {
142                 .flags = CE_ATTR_FLAGS,
143                 .src_nentries = 32,
144                 .src_sz_max = 2048,
145                 .dest_nentries = 0,
146                 .send_cb = ath10k_pci_htc_tx_cb,
147         },
148
149         /* CE4: host->target HTT */
150         {
151                 .flags = CE_ATTR_FLAGS | CE_ATTR_DIS_INTR,
152                 .src_nentries = CE_HTT_H2T_MSG_SRC_NENTRIES,
153                 .src_sz_max = 256,
154                 .dest_nentries = 0,
155                 .send_cb = ath10k_pci_htt_tx_cb,
156         },
157
158         /* CE5: target->host HTT (HIF->HTT) */
159         {
160                 .flags = CE_ATTR_FLAGS,
161                 .src_nentries = 0,
162                 .src_sz_max = 512,
163                 .dest_nentries = 512,
164                 .recv_cb = ath10k_pci_htt_rx_cb,
165         },
166
167         /* CE6: target autonomous hif_memcpy */
168         {
169                 .flags = CE_ATTR_FLAGS,
170                 .src_nentries = 0,
171                 .src_sz_max = 0,
172                 .dest_nentries = 0,
173         },
174
175         /* CE7: ce_diag, the Diagnostic Window */
176         {
177                 .flags = CE_ATTR_FLAGS,
178                 .src_nentries = 2,
179                 .src_sz_max = DIAG_TRANSFER_LIMIT,
180                 .dest_nentries = 2,
181         },
182
183         /* CE8: target->host pktlog */
184         {
185                 .flags = CE_ATTR_FLAGS,
186                 .src_nentries = 0,
187                 .src_sz_max = 2048,
188                 .dest_nentries = 128,
189                 .recv_cb = ath10k_pci_pktlog_rx_cb,
190         },
191
192         /* CE9 target autonomous qcache memcpy */
193         {
194                 .flags = CE_ATTR_FLAGS,
195                 .src_nentries = 0,
196                 .src_sz_max = 0,
197                 .dest_nentries = 0,
198         },
199
200         /* CE10: target autonomous hif memcpy */
201         {
202                 .flags = CE_ATTR_FLAGS,
203                 .src_nentries = 0,
204                 .src_sz_max = 0,
205                 .dest_nentries = 0,
206         },
207
208         /* CE11: target autonomous hif memcpy */
209         {
210                 .flags = CE_ATTR_FLAGS,
211                 .src_nentries = 0,
212                 .src_sz_max = 0,
213                 .dest_nentries = 0,
214         },
215 };
216
217 /* Target firmware's Copy Engine configuration. */
218 static struct ce_pipe_config target_ce_config_wlan[] = {
219         /* CE0: host->target HTC control and raw streams */
220         {
221                 .pipenum = __cpu_to_le32(0),
222                 .pipedir = __cpu_to_le32(PIPEDIR_OUT),
223                 .nentries = __cpu_to_le32(32),
224                 .nbytes_max = __cpu_to_le32(256),
225                 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
226                 .reserved = __cpu_to_le32(0),
227         },
228
229         /* CE1: target->host HTT + HTC control */
230         {
231                 .pipenum = __cpu_to_le32(1),
232                 .pipedir = __cpu_to_le32(PIPEDIR_IN),
233                 .nentries = __cpu_to_le32(32),
234                 .nbytes_max = __cpu_to_le32(2048),
235                 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
236                 .reserved = __cpu_to_le32(0),
237         },
238
239         /* CE2: target->host WMI */
240         {
241                 .pipenum = __cpu_to_le32(2),
242                 .pipedir = __cpu_to_le32(PIPEDIR_IN),
243                 .nentries = __cpu_to_le32(64),
244                 .nbytes_max = __cpu_to_le32(2048),
245                 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
246                 .reserved = __cpu_to_le32(0),
247         },
248
249         /* CE3: host->target WMI */
250         {
251                 .pipenum = __cpu_to_le32(3),
252                 .pipedir = __cpu_to_le32(PIPEDIR_OUT),
253                 .nentries = __cpu_to_le32(32),
254                 .nbytes_max = __cpu_to_le32(2048),
255                 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
256                 .reserved = __cpu_to_le32(0),
257         },
258
259         /* CE4: host->target HTT */
260         {
261                 .pipenum = __cpu_to_le32(4),
262                 .pipedir = __cpu_to_le32(PIPEDIR_OUT),
263                 .nentries = __cpu_to_le32(256),
264                 .nbytes_max = __cpu_to_le32(256),
265                 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
266                 .reserved = __cpu_to_le32(0),
267         },
268
269         /* NB: 50% of src nentries, since tx has 2 frags */
270
271         /* CE5: target->host HTT (HIF->HTT) */
272         {
273                 .pipenum = __cpu_to_le32(5),
274                 .pipedir = __cpu_to_le32(PIPEDIR_IN),
275                 .nentries = __cpu_to_le32(32),
276                 .nbytes_max = __cpu_to_le32(512),
277                 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
278                 .reserved = __cpu_to_le32(0),
279         },
280
281         /* CE6: Reserved for target autonomous hif_memcpy */
282         {
283                 .pipenum = __cpu_to_le32(6),
284                 .pipedir = __cpu_to_le32(PIPEDIR_INOUT),
285                 .nentries = __cpu_to_le32(32),
286                 .nbytes_max = __cpu_to_le32(4096),
287                 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
288                 .reserved = __cpu_to_le32(0),
289         },
290
291         /* CE7 used only by Host */
292         {
293                 .pipenum = __cpu_to_le32(7),
294                 .pipedir = __cpu_to_le32(PIPEDIR_INOUT),
295                 .nentries = __cpu_to_le32(0),
296                 .nbytes_max = __cpu_to_le32(0),
297                 .flags = __cpu_to_le32(0),
298                 .reserved = __cpu_to_le32(0),
299         },
300
301         /* CE8 target->host packtlog */
302         {
303                 .pipenum = __cpu_to_le32(8),
304                 .pipedir = __cpu_to_le32(PIPEDIR_IN),
305                 .nentries = __cpu_to_le32(64),
306                 .nbytes_max = __cpu_to_le32(2048),
307                 .flags = __cpu_to_le32(CE_ATTR_FLAGS | CE_ATTR_DIS_INTR),
308                 .reserved = __cpu_to_le32(0),
309         },
310
311         /* CE9 target autonomous qcache memcpy */
312         {
313                 .pipenum = __cpu_to_le32(9),
314                 .pipedir = __cpu_to_le32(PIPEDIR_INOUT),
315                 .nentries = __cpu_to_le32(32),
316                 .nbytes_max = __cpu_to_le32(2048),
317                 .flags = __cpu_to_le32(CE_ATTR_FLAGS | CE_ATTR_DIS_INTR),
318                 .reserved = __cpu_to_le32(0),
319         },
320
321         /* It not necessary to send target wlan configuration for CE10 & CE11
322          * as these CEs are not actively used in target.
323          */
324 };
325
326 /*
327  * Map from service/endpoint to Copy Engine.
328  * This table is derived from the CE_PCI TABLE, above.
329  * It is passed to the Target at startup for use by firmware.
330  */
331 static struct service_to_pipe target_service_to_ce_map_wlan[] = {
332         {
333                 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VO),
334                 __cpu_to_le32(PIPEDIR_OUT),     /* out = UL = host -> target */
335                 __cpu_to_le32(3),
336         },
337         {
338                 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VO),
339                 __cpu_to_le32(PIPEDIR_IN),      /* in = DL = target -> host */
340                 __cpu_to_le32(2),
341         },
342         {
343                 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BK),
344                 __cpu_to_le32(PIPEDIR_OUT),     /* out = UL = host -> target */
345                 __cpu_to_le32(3),
346         },
347         {
348                 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BK),
349                 __cpu_to_le32(PIPEDIR_IN),      /* in = DL = target -> host */
350                 __cpu_to_le32(2),
351         },
352         {
353                 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BE),
354                 __cpu_to_le32(PIPEDIR_OUT),     /* out = UL = host -> target */
355                 __cpu_to_le32(3),
356         },
357         {
358                 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BE),
359                 __cpu_to_le32(PIPEDIR_IN),      /* in = DL = target -> host */
360                 __cpu_to_le32(2),
361         },
362         {
363                 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VI),
364                 __cpu_to_le32(PIPEDIR_OUT),     /* out = UL = host -> target */
365                 __cpu_to_le32(3),
366         },
367         {
368                 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VI),
369                 __cpu_to_le32(PIPEDIR_IN),      /* in = DL = target -> host */
370                 __cpu_to_le32(2),
371         },
372         {
373                 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_CONTROL),
374                 __cpu_to_le32(PIPEDIR_OUT),     /* out = UL = host -> target */
375                 __cpu_to_le32(3),
376         },
377         {
378                 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_CONTROL),
379                 __cpu_to_le32(PIPEDIR_IN),      /* in = DL = target -> host */
380                 __cpu_to_le32(2),
381         },
382         {
383                 __cpu_to_le32(ATH10K_HTC_SVC_ID_RSVD_CTRL),
384                 __cpu_to_le32(PIPEDIR_OUT),     /* out = UL = host -> target */
385                 __cpu_to_le32(0),
386         },
387         {
388                 __cpu_to_le32(ATH10K_HTC_SVC_ID_RSVD_CTRL),
389                 __cpu_to_le32(PIPEDIR_IN),      /* in = DL = target -> host */
390                 __cpu_to_le32(1),
391         },
392         { /* not used */
393                 __cpu_to_le32(ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS),
394                 __cpu_to_le32(PIPEDIR_OUT),     /* out = UL = host -> target */
395                 __cpu_to_le32(0),
396         },
397         { /* not used */
398                 __cpu_to_le32(ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS),
399                 __cpu_to_le32(PIPEDIR_IN),      /* in = DL = target -> host */
400                 __cpu_to_le32(1),
401         },
402         {
403                 __cpu_to_le32(ATH10K_HTC_SVC_ID_HTT_DATA_MSG),
404                 __cpu_to_le32(PIPEDIR_OUT),     /* out = UL = host -> target */
405                 __cpu_to_le32(4),
406         },
407         {
408                 __cpu_to_le32(ATH10K_HTC_SVC_ID_HTT_DATA_MSG),
409                 __cpu_to_le32(PIPEDIR_IN),      /* in = DL = target -> host */
410                 __cpu_to_le32(5),
411         },
412
413         /* (Additions here) */
414
415         { /* must be last */
416                 __cpu_to_le32(0),
417                 __cpu_to_le32(0),
418                 __cpu_to_le32(0),
419         },
420 };
421
422 static bool ath10k_pci_is_awake(struct ath10k *ar)
423 {
424         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
425         u32 val = ioread32(ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
426                            RTC_STATE_ADDRESS);
427
428         return RTC_STATE_V_GET(val) == RTC_STATE_V_ON;
429 }
430
431 static void __ath10k_pci_wake(struct ath10k *ar)
432 {
433         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
434
435         lockdep_assert_held(&ar_pci->ps_lock);
436
437         ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps wake reg refcount %lu awake %d\n",
438                    ar_pci->ps_wake_refcount, ar_pci->ps_awake);
439
440         iowrite32(PCIE_SOC_WAKE_V_MASK,
441                   ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
442                   PCIE_SOC_WAKE_ADDRESS);
443 }
444
445 static void __ath10k_pci_sleep(struct ath10k *ar)
446 {
447         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
448
449         lockdep_assert_held(&ar_pci->ps_lock);
450
451         ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps sleep reg refcount %lu awake %d\n",
452                    ar_pci->ps_wake_refcount, ar_pci->ps_awake);
453
454         iowrite32(PCIE_SOC_WAKE_RESET,
455                   ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
456                   PCIE_SOC_WAKE_ADDRESS);
457         ar_pci->ps_awake = false;
458 }
459
460 static int ath10k_pci_wake_wait(struct ath10k *ar)
461 {
462         int tot_delay = 0;
463         int curr_delay = 5;
464
465         while (tot_delay < PCIE_WAKE_TIMEOUT) {
466                 if (ath10k_pci_is_awake(ar)) {
467                         if (tot_delay > PCIE_WAKE_LATE_US)
468                                 ath10k_warn(ar, "device wakeup took %d ms which is unusally long, otherwise it works normally.\n",
469                                             tot_delay / 1000);
470                         return 0;
471                 }
472
473                 udelay(curr_delay);
474                 tot_delay += curr_delay;
475
476                 if (curr_delay < 50)
477                         curr_delay += 5;
478         }
479
480         return -ETIMEDOUT;
481 }
482
483 static int ath10k_pci_force_wake(struct ath10k *ar)
484 {
485         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
486         unsigned long flags;
487         int ret = 0;
488
489         if (ar_pci->pci_ps)
490                 return ret;
491
492         spin_lock_irqsave(&ar_pci->ps_lock, flags);
493
494         if (!ar_pci->ps_awake) {
495                 iowrite32(PCIE_SOC_WAKE_V_MASK,
496                           ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
497                           PCIE_SOC_WAKE_ADDRESS);
498
499                 ret = ath10k_pci_wake_wait(ar);
500                 if (ret == 0)
501                         ar_pci->ps_awake = true;
502         }
503
504         spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
505
506         return ret;
507 }
508
509 static void ath10k_pci_force_sleep(struct ath10k *ar)
510 {
511         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
512         unsigned long flags;
513
514         spin_lock_irqsave(&ar_pci->ps_lock, flags);
515
516         iowrite32(PCIE_SOC_WAKE_RESET,
517                   ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
518                   PCIE_SOC_WAKE_ADDRESS);
519         ar_pci->ps_awake = false;
520
521         spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
522 }
523
524 static int ath10k_pci_wake(struct ath10k *ar)
525 {
526         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
527         unsigned long flags;
528         int ret = 0;
529
530         if (ar_pci->pci_ps == 0)
531                 return ret;
532
533         spin_lock_irqsave(&ar_pci->ps_lock, flags);
534
535         ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps wake refcount %lu awake %d\n",
536                    ar_pci->ps_wake_refcount, ar_pci->ps_awake);
537
538         /* This function can be called very frequently. To avoid excessive
539          * CPU stalls for MMIO reads use a cache var to hold the device state.
540          */
541         if (!ar_pci->ps_awake) {
542                 __ath10k_pci_wake(ar);
543
544                 ret = ath10k_pci_wake_wait(ar);
545                 if (ret == 0)
546                         ar_pci->ps_awake = true;
547         }
548
549         if (ret == 0) {
550                 ar_pci->ps_wake_refcount++;
551                 WARN_ON(ar_pci->ps_wake_refcount == 0);
552         }
553
554         spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
555
556         return ret;
557 }
558
559 static void ath10k_pci_sleep(struct ath10k *ar)
560 {
561         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
562         unsigned long flags;
563
564         if (ar_pci->pci_ps == 0)
565                 return;
566
567         spin_lock_irqsave(&ar_pci->ps_lock, flags);
568
569         ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps sleep refcount %lu awake %d\n",
570                    ar_pci->ps_wake_refcount, ar_pci->ps_awake);
571
572         if (WARN_ON(ar_pci->ps_wake_refcount == 0))
573                 goto skip;
574
575         ar_pci->ps_wake_refcount--;
576
577         mod_timer(&ar_pci->ps_timer, jiffies +
578                   msecs_to_jiffies(ATH10K_PCI_SLEEP_GRACE_PERIOD_MSEC));
579
580 skip:
581         spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
582 }
583
584 static void ath10k_pci_ps_timer(unsigned long ptr)
585 {
586         struct ath10k *ar = (void *)ptr;
587         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
588         unsigned long flags;
589
590         spin_lock_irqsave(&ar_pci->ps_lock, flags);
591
592         ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps timer refcount %lu awake %d\n",
593                    ar_pci->ps_wake_refcount, ar_pci->ps_awake);
594
595         if (ar_pci->ps_wake_refcount > 0)
596                 goto skip;
597
598         __ath10k_pci_sleep(ar);
599
600 skip:
601         spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
602 }
603
604 static void ath10k_pci_sleep_sync(struct ath10k *ar)
605 {
606         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
607         unsigned long flags;
608
609         if (ar_pci->pci_ps == 0) {
610                 ath10k_pci_force_sleep(ar);
611                 return;
612         }
613
614         del_timer_sync(&ar_pci->ps_timer);
615
616         spin_lock_irqsave(&ar_pci->ps_lock, flags);
617         WARN_ON(ar_pci->ps_wake_refcount > 0);
618         __ath10k_pci_sleep(ar);
619         spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
620 }
621
622 static void ath10k_bus_pci_write32(struct ath10k *ar, u32 offset, u32 value)
623 {
624         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
625         int ret;
626
627         if (unlikely(offset + sizeof(value) > ar_pci->mem_len)) {
628                 ath10k_warn(ar, "refusing to write mmio out of bounds at 0x%08x - 0x%08zx (max 0x%08zx)\n",
629                             offset, offset + sizeof(value), ar_pci->mem_len);
630                 return;
631         }
632
633         ret = ath10k_pci_wake(ar);
634         if (ret) {
635                 ath10k_warn(ar, "failed to wake target for write32 of 0x%08x at 0x%08x: %d\n",
636                             value, offset, ret);
637                 return;
638         }
639
640         iowrite32(value, ar_pci->mem + offset);
641         ath10k_pci_sleep(ar);
642 }
643
644 static u32 ath10k_bus_pci_read32(struct ath10k *ar, u32 offset)
645 {
646         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
647         u32 val;
648         int ret;
649
650         if (unlikely(offset + sizeof(val) > ar_pci->mem_len)) {
651                 ath10k_warn(ar, "refusing to read mmio out of bounds at 0x%08x - 0x%08zx (max 0x%08zx)\n",
652                             offset, offset + sizeof(val), ar_pci->mem_len);
653                 return 0;
654         }
655
656         ret = ath10k_pci_wake(ar);
657         if (ret) {
658                 ath10k_warn(ar, "failed to wake target for read32 at 0x%08x: %d\n",
659                             offset, ret);
660                 return 0xffffffff;
661         }
662
663         val = ioread32(ar_pci->mem + offset);
664         ath10k_pci_sleep(ar);
665
666         return val;
667 }
668
669 inline void ath10k_pci_write32(struct ath10k *ar, u32 offset, u32 value)
670 {
671         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
672
673         ar_pci->bus_ops->write32(ar, offset, value);
674 }
675
676 inline u32 ath10k_pci_read32(struct ath10k *ar, u32 offset)
677 {
678         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
679
680         return ar_pci->bus_ops->read32(ar, offset);
681 }
682
683 u32 ath10k_pci_soc_read32(struct ath10k *ar, u32 addr)
684 {
685         return ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS + addr);
686 }
687
688 void ath10k_pci_soc_write32(struct ath10k *ar, u32 addr, u32 val)
689 {
690         ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + addr, val);
691 }
692
693 u32 ath10k_pci_reg_read32(struct ath10k *ar, u32 addr)
694 {
695         return ath10k_pci_read32(ar, PCIE_LOCAL_BASE_ADDRESS + addr);
696 }
697
698 void ath10k_pci_reg_write32(struct ath10k *ar, u32 addr, u32 val)
699 {
700         ath10k_pci_write32(ar, PCIE_LOCAL_BASE_ADDRESS + addr, val);
701 }
702
703 bool ath10k_pci_irq_pending(struct ath10k *ar)
704 {
705         u32 cause;
706
707         /* Check if the shared legacy irq is for us */
708         cause = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
709                                   PCIE_INTR_CAUSE_ADDRESS);
710         if (cause & (PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL))
711                 return true;
712
713         return false;
714 }
715
716 void ath10k_pci_disable_and_clear_legacy_irq(struct ath10k *ar)
717 {
718         /* IMPORTANT: INTR_CLR register has to be set after
719          * INTR_ENABLE is set to 0, otherwise interrupt can not be
720          * really cleared. */
721         ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
722                            0);
723         ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_CLR_ADDRESS,
724                            PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
725
726         /* IMPORTANT: this extra read transaction is required to
727          * flush the posted write buffer. */
728         (void)ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
729                                 PCIE_INTR_ENABLE_ADDRESS);
730 }
731
732 void ath10k_pci_enable_legacy_irq(struct ath10k *ar)
733 {
734         ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
735                            PCIE_INTR_ENABLE_ADDRESS,
736                            PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
737
738         /* IMPORTANT: this extra read transaction is required to
739          * flush the posted write buffer. */
740         (void)ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
741                                 PCIE_INTR_ENABLE_ADDRESS);
742 }
743
744 static inline const char *ath10k_pci_get_irq_method(struct ath10k *ar)
745 {
746         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
747
748         if (ar_pci->num_msi_intrs > 1)
749                 return "msi-x";
750
751         if (ar_pci->num_msi_intrs == 1)
752                 return "msi";
753
754         return "legacy";
755 }
756
757 static int __ath10k_pci_rx_post_buf(struct ath10k_pci_pipe *pipe)
758 {
759         struct ath10k *ar = pipe->hif_ce_state;
760         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
761         struct ath10k_ce_pipe *ce_pipe = pipe->ce_hdl;
762         struct sk_buff *skb;
763         dma_addr_t paddr;
764         int ret;
765
766         skb = dev_alloc_skb(pipe->buf_sz);
767         if (!skb)
768                 return -ENOMEM;
769
770         WARN_ONCE((unsigned long)skb->data & 3, "unaligned skb");
771
772         paddr = dma_map_single(ar->dev, skb->data,
773                                skb->len + skb_tailroom(skb),
774                                DMA_FROM_DEVICE);
775         if (unlikely(dma_mapping_error(ar->dev, paddr))) {
776                 ath10k_warn(ar, "failed to dma map pci rx buf\n");
777                 dev_kfree_skb_any(skb);
778                 return -EIO;
779         }
780
781         ATH10K_SKB_RXCB(skb)->paddr = paddr;
782
783         spin_lock_bh(&ar_pci->ce_lock);
784         ret = __ath10k_ce_rx_post_buf(ce_pipe, skb, paddr);
785         spin_unlock_bh(&ar_pci->ce_lock);
786         if (ret) {
787                 dma_unmap_single(ar->dev, paddr, skb->len + skb_tailroom(skb),
788                                  DMA_FROM_DEVICE);
789                 dev_kfree_skb_any(skb);
790                 return ret;
791         }
792
793         return 0;
794 }
795
796 static void ath10k_pci_rx_post_pipe(struct ath10k_pci_pipe *pipe)
797 {
798         struct ath10k *ar = pipe->hif_ce_state;
799         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
800         struct ath10k_ce_pipe *ce_pipe = pipe->ce_hdl;
801         int ret, num;
802
803         if (pipe->buf_sz == 0)
804                 return;
805
806         if (!ce_pipe->dest_ring)
807                 return;
808
809         spin_lock_bh(&ar_pci->ce_lock);
810         num = __ath10k_ce_rx_num_free_bufs(ce_pipe);
811         spin_unlock_bh(&ar_pci->ce_lock);
812         while (num--) {
813                 ret = __ath10k_pci_rx_post_buf(pipe);
814                 if (ret) {
815                         if (ret == -ENOSPC)
816                                 break;
817                         ath10k_warn(ar, "failed to post pci rx buf: %d\n", ret);
818                         mod_timer(&ar_pci->rx_post_retry, jiffies +
819                                   ATH10K_PCI_RX_POST_RETRY_MS);
820                         break;
821                 }
822         }
823 }
824
825 void ath10k_pci_rx_post(struct ath10k *ar)
826 {
827         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
828         int i;
829
830         for (i = 0; i < CE_COUNT; i++)
831                 ath10k_pci_rx_post_pipe(&ar_pci->pipe_info[i]);
832 }
833
834 void ath10k_pci_rx_replenish_retry(unsigned long ptr)
835 {
836         struct ath10k *ar = (void *)ptr;
837
838         ath10k_pci_rx_post(ar);
839 }
840
841 static u32 ath10k_pci_targ_cpu_to_ce_addr(struct ath10k *ar, u32 addr)
842 {
843         u32 val = 0;
844
845         switch (ar->hw_rev) {
846         case ATH10K_HW_QCA988X:
847         case ATH10K_HW_QCA6174:
848         case ATH10K_HW_QCA9377:
849                 val = (ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
850                                           CORE_CTRL_ADDRESS) &
851                        0x7ff) << 21;
852                 break;
853         case ATH10K_HW_QCA99X0:
854         case ATH10K_HW_QCA4019:
855                 val = ath10k_pci_read32(ar, PCIE_BAR_REG_ADDRESS);
856                 break;
857         }
858
859         val |= 0x100000 | (addr & 0xfffff);
860         return val;
861 }
862
863 /*
864  * Diagnostic read/write access is provided for startup/config/debug usage.
865  * Caller must guarantee proper alignment, when applicable, and single user
866  * at any moment.
867  */
868 static int ath10k_pci_diag_read_mem(struct ath10k *ar, u32 address, void *data,
869                                     int nbytes)
870 {
871         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
872         int ret = 0;
873         u32 *buf;
874         unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
875         struct ath10k_ce_pipe *ce_diag;
876         /* Host buffer address in CE space */
877         u32 ce_data;
878         dma_addr_t ce_data_base = 0;
879         void *data_buf = NULL;
880         int i;
881
882         spin_lock_bh(&ar_pci->ce_lock);
883
884         ce_diag = ar_pci->ce_diag;
885
886         /*
887          * Allocate a temporary bounce buffer to hold caller's data
888          * to be DMA'ed from Target. This guarantees
889          *   1) 4-byte alignment
890          *   2) Buffer in DMA-able space
891          */
892         orig_nbytes = nbytes;
893         data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
894                                                        orig_nbytes,
895                                                        &ce_data_base,
896                                                        GFP_ATOMIC);
897
898         if (!data_buf) {
899                 ret = -ENOMEM;
900                 goto done;
901         }
902         memset(data_buf, 0, orig_nbytes);
903
904         remaining_bytes = orig_nbytes;
905         ce_data = ce_data_base;
906         while (remaining_bytes) {
907                 nbytes = min_t(unsigned int, remaining_bytes,
908                                DIAG_TRANSFER_LIMIT);
909
910                 ret = __ath10k_ce_rx_post_buf(ce_diag, &ce_data, ce_data);
911                 if (ret != 0)
912                         goto done;
913
914                 /* Request CE to send from Target(!) address to Host buffer */
915                 /*
916                  * The address supplied by the caller is in the
917                  * Target CPU virtual address space.
918                  *
919                  * In order to use this address with the diagnostic CE,
920                  * convert it from Target CPU virtual address space
921                  * to CE address space
922                  */
923                 address = ath10k_pci_targ_cpu_to_ce_addr(ar, address);
924
925                 ret = ath10k_ce_send_nolock(ce_diag, NULL, (u32)address, nbytes, 0,
926                                             0);
927                 if (ret)
928                         goto done;
929
930                 i = 0;
931                 while (ath10k_ce_completed_send_next_nolock(ce_diag,
932                                                             NULL) != 0) {
933                         mdelay(1);
934                         if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
935                                 ret = -EBUSY;
936                                 goto done;
937                         }
938                 }
939
940                 i = 0;
941                 while (ath10k_ce_completed_recv_next_nolock(ce_diag,
942                                                             (void **)&buf,
943                                                             &completed_nbytes)
944                                                                 != 0) {
945                         mdelay(1);
946
947                         if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
948                                 ret = -EBUSY;
949                                 goto done;
950                         }
951                 }
952
953                 if (nbytes != completed_nbytes) {
954                         ret = -EIO;
955                         goto done;
956                 }
957
958                 if (*buf != ce_data) {
959                         ret = -EIO;
960                         goto done;
961                 }
962
963                 remaining_bytes -= nbytes;
964                 address += nbytes;
965                 ce_data += nbytes;
966         }
967
968 done:
969         if (ret == 0)
970                 memcpy(data, data_buf, orig_nbytes);
971         else
972                 ath10k_warn(ar, "failed to read diag value at 0x%x: %d\n",
973                             address, ret);
974
975         if (data_buf)
976                 dma_free_coherent(ar->dev, orig_nbytes, data_buf,
977                                   ce_data_base);
978
979         spin_unlock_bh(&ar_pci->ce_lock);
980
981         return ret;
982 }
983
984 static int ath10k_pci_diag_read32(struct ath10k *ar, u32 address, u32 *value)
985 {
986         __le32 val = 0;
987         int ret;
988
989         ret = ath10k_pci_diag_read_mem(ar, address, &val, sizeof(val));
990         *value = __le32_to_cpu(val);
991
992         return ret;
993 }
994
995 static int __ath10k_pci_diag_read_hi(struct ath10k *ar, void *dest,
996                                      u32 src, u32 len)
997 {
998         u32 host_addr, addr;
999         int ret;
1000
1001         host_addr = host_interest_item_address(src);
1002
1003         ret = ath10k_pci_diag_read32(ar, host_addr, &addr);
1004         if (ret != 0) {
1005                 ath10k_warn(ar, "failed to get memcpy hi address for firmware address %d: %d\n",
1006                             src, ret);
1007                 return ret;
1008         }
1009
1010         ret = ath10k_pci_diag_read_mem(ar, addr, dest, len);
1011         if (ret != 0) {
1012                 ath10k_warn(ar, "failed to memcpy firmware memory from %d (%d B): %d\n",
1013                             addr, len, ret);
1014                 return ret;
1015         }
1016
1017         return 0;
1018 }
1019
1020 #define ath10k_pci_diag_read_hi(ar, dest, src, len)             \
1021         __ath10k_pci_diag_read_hi(ar, dest, HI_ITEM(src), len)
1022
1023 int ath10k_pci_diag_write_mem(struct ath10k *ar, u32 address,
1024                               const void *data, int nbytes)
1025 {
1026         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1027         int ret = 0;
1028         u32 *buf;
1029         unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
1030         struct ath10k_ce_pipe *ce_diag;
1031         void *data_buf = NULL;
1032         u32 ce_data;    /* Host buffer address in CE space */
1033         dma_addr_t ce_data_base = 0;
1034         int i;
1035
1036         spin_lock_bh(&ar_pci->ce_lock);
1037
1038         ce_diag = ar_pci->ce_diag;
1039
1040         /*
1041          * Allocate a temporary bounce buffer to hold caller's data
1042          * to be DMA'ed to Target. This guarantees
1043          *   1) 4-byte alignment
1044          *   2) Buffer in DMA-able space
1045          */
1046         orig_nbytes = nbytes;
1047         data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
1048                                                        orig_nbytes,
1049                                                        &ce_data_base,
1050                                                        GFP_ATOMIC);
1051         if (!data_buf) {
1052                 ret = -ENOMEM;
1053                 goto done;
1054         }
1055
1056         /* Copy caller's data to allocated DMA buf */
1057         memcpy(data_buf, data, orig_nbytes);
1058
1059         /*
1060          * The address supplied by the caller is in the
1061          * Target CPU virtual address space.
1062          *
1063          * In order to use this address with the diagnostic CE,
1064          * convert it from
1065          *    Target CPU virtual address space
1066          * to
1067          *    CE address space
1068          */
1069         address = ath10k_pci_targ_cpu_to_ce_addr(ar, address);
1070
1071         remaining_bytes = orig_nbytes;
1072         ce_data = ce_data_base;
1073         while (remaining_bytes) {
1074                 /* FIXME: check cast */
1075                 nbytes = min_t(int, remaining_bytes, DIAG_TRANSFER_LIMIT);
1076
1077                 /* Set up to receive directly into Target(!) address */
1078                 ret = __ath10k_ce_rx_post_buf(ce_diag, &address, address);
1079                 if (ret != 0)
1080                         goto done;
1081
1082                 /*
1083                  * Request CE to send caller-supplied data that
1084                  * was copied to bounce buffer to Target(!) address.
1085                  */
1086                 ret = ath10k_ce_send_nolock(ce_diag, NULL, (u32)ce_data,
1087                                             nbytes, 0, 0);
1088                 if (ret != 0)
1089                         goto done;
1090
1091                 i = 0;
1092                 while (ath10k_ce_completed_send_next_nolock(ce_diag,
1093                                                             NULL) != 0) {
1094                         mdelay(1);
1095
1096                         if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
1097                                 ret = -EBUSY;
1098                                 goto done;
1099                         }
1100                 }
1101
1102                 i = 0;
1103                 while (ath10k_ce_completed_recv_next_nolock(ce_diag,
1104                                                             (void **)&buf,
1105                                                             &completed_nbytes)
1106                                                                 != 0) {
1107                         mdelay(1);
1108
1109                         if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
1110                                 ret = -EBUSY;
1111                                 goto done;
1112                         }
1113                 }
1114
1115                 if (nbytes != completed_nbytes) {
1116                         ret = -EIO;
1117                         goto done;
1118                 }
1119
1120                 if (*buf != address) {
1121                         ret = -EIO;
1122                         goto done;
1123                 }
1124
1125                 remaining_bytes -= nbytes;
1126                 address += nbytes;
1127                 ce_data += nbytes;
1128         }
1129
1130 done:
1131         if (data_buf) {
1132                 dma_free_coherent(ar->dev, orig_nbytes, data_buf,
1133                                   ce_data_base);
1134         }
1135
1136         if (ret != 0)
1137                 ath10k_warn(ar, "failed to write diag value at 0x%x: %d\n",
1138                             address, ret);
1139
1140         spin_unlock_bh(&ar_pci->ce_lock);
1141
1142         return ret;
1143 }
1144
1145 static int ath10k_pci_diag_write32(struct ath10k *ar, u32 address, u32 value)
1146 {
1147         __le32 val = __cpu_to_le32(value);
1148
1149         return ath10k_pci_diag_write_mem(ar, address, &val, sizeof(val));
1150 }
1151
1152 /* Called by lower (CE) layer when a send to Target completes. */
1153 static void ath10k_pci_htc_tx_cb(struct ath10k_ce_pipe *ce_state)
1154 {
1155         struct ath10k *ar = ce_state->ar;
1156         struct sk_buff_head list;
1157         struct sk_buff *skb;
1158
1159         __skb_queue_head_init(&list);
1160         while (ath10k_ce_completed_send_next(ce_state, (void **)&skb) == 0) {
1161                 /* no need to call tx completion for NULL pointers */
1162                 if (skb == NULL)
1163                         continue;
1164
1165                 __skb_queue_tail(&list, skb);
1166         }
1167
1168         while ((skb = __skb_dequeue(&list)))
1169                 ath10k_htc_tx_completion_handler(ar, skb);
1170 }
1171
1172 static void ath10k_pci_process_rx_cb(struct ath10k_ce_pipe *ce_state,
1173                                      void (*callback)(struct ath10k *ar,
1174                                                       struct sk_buff *skb))
1175 {
1176         struct ath10k *ar = ce_state->ar;
1177         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1178         struct ath10k_pci_pipe *pipe_info =  &ar_pci->pipe_info[ce_state->id];
1179         struct sk_buff *skb;
1180         struct sk_buff_head list;
1181         void *transfer_context;
1182         unsigned int nbytes, max_nbytes;
1183
1184         __skb_queue_head_init(&list);
1185         while (ath10k_ce_completed_recv_next(ce_state, &transfer_context,
1186                                              &nbytes) == 0) {
1187                 skb = transfer_context;
1188                 max_nbytes = skb->len + skb_tailroom(skb);
1189                 dma_unmap_single(ar->dev, ATH10K_SKB_RXCB(skb)->paddr,
1190                                  max_nbytes, DMA_FROM_DEVICE);
1191
1192                 if (unlikely(max_nbytes < nbytes)) {
1193                         ath10k_warn(ar, "rxed more than expected (nbytes %d, max %d)",
1194                                     nbytes, max_nbytes);
1195                         dev_kfree_skb_any(skb);
1196                         continue;
1197                 }
1198
1199                 skb_put(skb, nbytes);
1200                 __skb_queue_tail(&list, skb);
1201         }
1202
1203         while ((skb = __skb_dequeue(&list))) {
1204                 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci rx ce pipe %d len %d\n",
1205                            ce_state->id, skb->len);
1206                 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci rx: ",
1207                                 skb->data, skb->len);
1208
1209                 callback(ar, skb);
1210         }
1211
1212         ath10k_pci_rx_post_pipe(pipe_info);
1213 }
1214
1215 /* Called by lower (CE) layer when data is received from the Target. */
1216 static void ath10k_pci_htc_rx_cb(struct ath10k_ce_pipe *ce_state)
1217 {
1218         ath10k_pci_process_rx_cb(ce_state, ath10k_htc_rx_completion_handler);
1219 }
1220
1221 static void ath10k_pci_htt_htc_rx_cb(struct ath10k_ce_pipe *ce_state)
1222 {
1223         /* CE4 polling needs to be done whenever CE pipe which transports
1224          * HTT Rx (target->host) is processed.
1225          */
1226         ath10k_ce_per_engine_service(ce_state->ar, 4);
1227
1228         ath10k_pci_process_rx_cb(ce_state, ath10k_htc_rx_completion_handler);
1229 }
1230
1231 /* Called by lower (CE) layer when data is received from the Target.
1232  * Only 10.4 firmware uses separate CE to transfer pktlog data.
1233  */
1234 static void ath10k_pci_pktlog_rx_cb(struct ath10k_ce_pipe *ce_state)
1235 {
1236         ath10k_pci_process_rx_cb(ce_state,
1237                                  ath10k_htt_rx_pktlog_completion_handler);
1238 }
1239
1240 /* Called by lower (CE) layer when a send to HTT Target completes. */
1241 static void ath10k_pci_htt_tx_cb(struct ath10k_ce_pipe *ce_state)
1242 {
1243         struct ath10k *ar = ce_state->ar;
1244         struct sk_buff *skb;
1245
1246         while (ath10k_ce_completed_send_next(ce_state, (void **)&skb) == 0) {
1247                 /* no need to call tx completion for NULL pointers */
1248                 if (!skb)
1249                         continue;
1250
1251                 dma_unmap_single(ar->dev, ATH10K_SKB_CB(skb)->paddr,
1252                                  skb->len, DMA_TO_DEVICE);
1253                 ath10k_htt_hif_tx_complete(ar, skb);
1254         }
1255 }
1256
1257 static void ath10k_pci_htt_rx_deliver(struct ath10k *ar, struct sk_buff *skb)
1258 {
1259         skb_pull(skb, sizeof(struct ath10k_htc_hdr));
1260         ath10k_htt_t2h_msg_handler(ar, skb);
1261 }
1262
1263 /* Called by lower (CE) layer when HTT data is received from the Target. */
1264 static void ath10k_pci_htt_rx_cb(struct ath10k_ce_pipe *ce_state)
1265 {
1266         /* CE4 polling needs to be done whenever CE pipe which transports
1267          * HTT Rx (target->host) is processed.
1268          */
1269         ath10k_ce_per_engine_service(ce_state->ar, 4);
1270
1271         ath10k_pci_process_rx_cb(ce_state, ath10k_pci_htt_rx_deliver);
1272 }
1273
1274 int ath10k_pci_hif_tx_sg(struct ath10k *ar, u8 pipe_id,
1275                          struct ath10k_hif_sg_item *items, int n_items)
1276 {
1277         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1278         struct ath10k_pci_pipe *pci_pipe = &ar_pci->pipe_info[pipe_id];
1279         struct ath10k_ce_pipe *ce_pipe = pci_pipe->ce_hdl;
1280         struct ath10k_ce_ring *src_ring = ce_pipe->src_ring;
1281         unsigned int nentries_mask;
1282         unsigned int sw_index;
1283         unsigned int write_index;
1284         int err, i = 0;
1285
1286         spin_lock_bh(&ar_pci->ce_lock);
1287
1288         nentries_mask = src_ring->nentries_mask;
1289         sw_index = src_ring->sw_index;
1290         write_index = src_ring->write_index;
1291
1292         if (unlikely(CE_RING_DELTA(nentries_mask,
1293                                    write_index, sw_index - 1) < n_items)) {
1294                 err = -ENOBUFS;
1295                 goto err;
1296         }
1297
1298         for (i = 0; i < n_items - 1; i++) {
1299                 ath10k_dbg(ar, ATH10K_DBG_PCI,
1300                            "pci tx item %d paddr 0x%08x len %d n_items %d\n",
1301                            i, items[i].paddr, items[i].len, n_items);
1302                 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci tx data: ",
1303                                 items[i].vaddr, items[i].len);
1304
1305                 err = ath10k_ce_send_nolock(ce_pipe,
1306                                             items[i].transfer_context,
1307                                             items[i].paddr,
1308                                             items[i].len,
1309                                             items[i].transfer_id,
1310                                             CE_SEND_FLAG_GATHER);
1311                 if (err)
1312                         goto err;
1313         }
1314
1315         /* `i` is equal to `n_items -1` after for() */
1316
1317         ath10k_dbg(ar, ATH10K_DBG_PCI,
1318                    "pci tx item %d paddr 0x%08x len %d n_items %d\n",
1319                    i, items[i].paddr, items[i].len, n_items);
1320         ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci tx data: ",
1321                         items[i].vaddr, items[i].len);
1322
1323         err = ath10k_ce_send_nolock(ce_pipe,
1324                                     items[i].transfer_context,
1325                                     items[i].paddr,
1326                                     items[i].len,
1327                                     items[i].transfer_id,
1328                                     0);
1329         if (err)
1330                 goto err;
1331
1332         spin_unlock_bh(&ar_pci->ce_lock);
1333         return 0;
1334
1335 err:
1336         for (; i > 0; i--)
1337                 __ath10k_ce_send_revert(ce_pipe);
1338
1339         spin_unlock_bh(&ar_pci->ce_lock);
1340         return err;
1341 }
1342
1343 int ath10k_pci_hif_diag_read(struct ath10k *ar, u32 address, void *buf,
1344                              size_t buf_len)
1345 {
1346         return ath10k_pci_diag_read_mem(ar, address, buf, buf_len);
1347 }
1348
1349 u16 ath10k_pci_hif_get_free_queue_number(struct ath10k *ar, u8 pipe)
1350 {
1351         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1352
1353         ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif get free queue number\n");
1354
1355         return ath10k_ce_num_free_src_entries(ar_pci->pipe_info[pipe].ce_hdl);
1356 }
1357
1358 static void ath10k_pci_dump_registers(struct ath10k *ar,
1359                                       struct ath10k_fw_crash_data *crash_data)
1360 {
1361         __le32 reg_dump_values[REG_DUMP_COUNT_QCA988X] = {};
1362         int i, ret;
1363
1364         lockdep_assert_held(&ar->data_lock);
1365
1366         ret = ath10k_pci_diag_read_hi(ar, &reg_dump_values[0],
1367                                       hi_failure_state,
1368                                       REG_DUMP_COUNT_QCA988X * sizeof(__le32));
1369         if (ret) {
1370                 ath10k_err(ar, "failed to read firmware dump area: %d\n", ret);
1371                 return;
1372         }
1373
1374         BUILD_BUG_ON(REG_DUMP_COUNT_QCA988X % 4);
1375
1376         ath10k_err(ar, "firmware register dump:\n");
1377         for (i = 0; i < REG_DUMP_COUNT_QCA988X; i += 4)
1378                 ath10k_err(ar, "[%02d]: 0x%08X 0x%08X 0x%08X 0x%08X\n",
1379                            i,
1380                            __le32_to_cpu(reg_dump_values[i]),
1381                            __le32_to_cpu(reg_dump_values[i + 1]),
1382                            __le32_to_cpu(reg_dump_values[i + 2]),
1383                            __le32_to_cpu(reg_dump_values[i + 3]));
1384
1385         if (!crash_data)
1386                 return;
1387
1388         for (i = 0; i < REG_DUMP_COUNT_QCA988X; i++)
1389                 crash_data->registers[i] = reg_dump_values[i];
1390 }
1391
1392 static void ath10k_pci_fw_crashed_dump(struct ath10k *ar)
1393 {
1394         struct ath10k_fw_crash_data *crash_data;
1395         char uuid[50];
1396
1397         spin_lock_bh(&ar->data_lock);
1398
1399         ar->stats.fw_crash_counter++;
1400
1401         crash_data = ath10k_debug_get_new_fw_crash_data(ar);
1402
1403         if (crash_data)
1404                 scnprintf(uuid, sizeof(uuid), "%pUl", &crash_data->uuid);
1405         else
1406                 scnprintf(uuid, sizeof(uuid), "n/a");
1407
1408         ath10k_err(ar, "firmware crashed! (uuid %s)\n", uuid);
1409         ath10k_print_driver_info(ar);
1410         ath10k_pci_dump_registers(ar, crash_data);
1411
1412         spin_unlock_bh(&ar->data_lock);
1413
1414         queue_work(ar->workqueue, &ar->restart_work);
1415 }
1416
1417 void ath10k_pci_hif_send_complete_check(struct ath10k *ar, u8 pipe,
1418                                         int force)
1419 {
1420         ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif send complete check\n");
1421
1422         if (!force) {
1423                 int resources;
1424                 /*
1425                  * Decide whether to actually poll for completions, or just
1426                  * wait for a later chance.
1427                  * If there seem to be plenty of resources left, then just wait
1428                  * since checking involves reading a CE register, which is a
1429                  * relatively expensive operation.
1430                  */
1431                 resources = ath10k_pci_hif_get_free_queue_number(ar, pipe);
1432
1433                 /*
1434                  * If at least 50% of the total resources are still available,
1435                  * don't bother checking again yet.
1436                  */
1437                 if (resources > (host_ce_config_wlan[pipe].src_nentries >> 1))
1438                         return;
1439         }
1440         ath10k_ce_per_engine_service(ar, pipe);
1441 }
1442
1443 void ath10k_pci_kill_tasklet(struct ath10k *ar)
1444 {
1445         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1446         int i;
1447
1448         tasklet_kill(&ar_pci->intr_tq);
1449         tasklet_kill(&ar_pci->msi_fw_err);
1450
1451         for (i = 0; i < CE_COUNT; i++)
1452                 tasklet_kill(&ar_pci->pipe_info[i].intr);
1453
1454         del_timer_sync(&ar_pci->rx_post_retry);
1455 }
1456
1457 int ath10k_pci_hif_map_service_to_pipe(struct ath10k *ar, u16 service_id,
1458                                        u8 *ul_pipe, u8 *dl_pipe)
1459 {
1460         const struct service_to_pipe *entry;
1461         bool ul_set = false, dl_set = false;
1462         int i;
1463
1464         ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif map service\n");
1465
1466         for (i = 0; i < ARRAY_SIZE(target_service_to_ce_map_wlan); i++) {
1467                 entry = &target_service_to_ce_map_wlan[i];
1468
1469                 if (__le32_to_cpu(entry->service_id) != service_id)
1470                         continue;
1471
1472                 switch (__le32_to_cpu(entry->pipedir)) {
1473                 case PIPEDIR_NONE:
1474                         break;
1475                 case PIPEDIR_IN:
1476                         WARN_ON(dl_set);
1477                         *dl_pipe = __le32_to_cpu(entry->pipenum);
1478                         dl_set = true;
1479                         break;
1480                 case PIPEDIR_OUT:
1481                         WARN_ON(ul_set);
1482                         *ul_pipe = __le32_to_cpu(entry->pipenum);
1483                         ul_set = true;
1484                         break;
1485                 case PIPEDIR_INOUT:
1486                         WARN_ON(dl_set);
1487                         WARN_ON(ul_set);
1488                         *dl_pipe = __le32_to_cpu(entry->pipenum);
1489                         *ul_pipe = __le32_to_cpu(entry->pipenum);
1490                         dl_set = true;
1491                         ul_set = true;
1492                         break;
1493                 }
1494         }
1495
1496         if (WARN_ON(!ul_set || !dl_set))
1497                 return -ENOENT;
1498
1499         return 0;
1500 }
1501
1502 void ath10k_pci_hif_get_default_pipe(struct ath10k *ar,
1503                                      u8 *ul_pipe, u8 *dl_pipe)
1504 {
1505         ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif get default pipe\n");
1506
1507         (void)ath10k_pci_hif_map_service_to_pipe(ar,
1508                                                  ATH10K_HTC_SVC_ID_RSVD_CTRL,
1509                                                  ul_pipe, dl_pipe);
1510 }
1511
1512 static void ath10k_pci_irq_msi_fw_mask(struct ath10k *ar)
1513 {
1514         u32 val;
1515
1516         switch (ar->hw_rev) {
1517         case ATH10K_HW_QCA988X:
1518         case ATH10K_HW_QCA6174:
1519         case ATH10K_HW_QCA9377:
1520                 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1521                                         CORE_CTRL_ADDRESS);
1522                 val &= ~CORE_CTRL_PCIE_REG_31_MASK;
1523                 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
1524                                    CORE_CTRL_ADDRESS, val);
1525                 break;
1526         case ATH10K_HW_QCA99X0:
1527         case ATH10K_HW_QCA4019:
1528                 /* TODO: Find appropriate register configuration for QCA99X0
1529                  *  to mask irq/MSI.
1530                  */
1531                  break;
1532         }
1533 }
1534
1535 static void ath10k_pci_irq_msi_fw_unmask(struct ath10k *ar)
1536 {
1537         u32 val;
1538
1539         switch (ar->hw_rev) {
1540         case ATH10K_HW_QCA988X:
1541         case ATH10K_HW_QCA6174:
1542         case ATH10K_HW_QCA9377:
1543                 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1544                                         CORE_CTRL_ADDRESS);
1545                 val |= CORE_CTRL_PCIE_REG_31_MASK;
1546                 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
1547                                    CORE_CTRL_ADDRESS, val);
1548                 break;
1549         case ATH10K_HW_QCA99X0:
1550         case ATH10K_HW_QCA4019:
1551                 /* TODO: Find appropriate register configuration for QCA99X0
1552                  *  to unmask irq/MSI.
1553                  */
1554                 break;
1555         }
1556 }
1557
1558 static void ath10k_pci_irq_disable(struct ath10k *ar)
1559 {
1560         ath10k_ce_disable_interrupts(ar);
1561         ath10k_pci_disable_and_clear_legacy_irq(ar);
1562         ath10k_pci_irq_msi_fw_mask(ar);
1563 }
1564
1565 static void ath10k_pci_irq_sync(struct ath10k *ar)
1566 {
1567         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1568         int i;
1569
1570         for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
1571                 synchronize_irq(ar_pci->pdev->irq + i);
1572 }
1573
1574 static void ath10k_pci_irq_enable(struct ath10k *ar)
1575 {
1576         ath10k_ce_enable_interrupts(ar);
1577         ath10k_pci_enable_legacy_irq(ar);
1578         ath10k_pci_irq_msi_fw_unmask(ar);
1579 }
1580
1581 static int ath10k_pci_hif_start(struct ath10k *ar)
1582 {
1583         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1584
1585         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif start\n");
1586
1587         ath10k_pci_irq_enable(ar);
1588         ath10k_pci_rx_post(ar);
1589
1590         pcie_capability_write_word(ar_pci->pdev, PCI_EXP_LNKCTL,
1591                                    ar_pci->link_ctl);
1592
1593         return 0;
1594 }
1595
1596 static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe *pci_pipe)
1597 {
1598         struct ath10k *ar;
1599         struct ath10k_ce_pipe *ce_pipe;
1600         struct ath10k_ce_ring *ce_ring;
1601         struct sk_buff *skb;
1602         int i;
1603
1604         ar = pci_pipe->hif_ce_state;
1605         ce_pipe = pci_pipe->ce_hdl;
1606         ce_ring = ce_pipe->dest_ring;
1607
1608         if (!ce_ring)
1609                 return;
1610
1611         if (!pci_pipe->buf_sz)
1612                 return;
1613
1614         for (i = 0; i < ce_ring->nentries; i++) {
1615                 skb = ce_ring->per_transfer_context[i];
1616                 if (!skb)
1617                         continue;
1618
1619                 ce_ring->per_transfer_context[i] = NULL;
1620
1621                 dma_unmap_single(ar->dev, ATH10K_SKB_RXCB(skb)->paddr,
1622                                  skb->len + skb_tailroom(skb),
1623                                  DMA_FROM_DEVICE);
1624                 dev_kfree_skb_any(skb);
1625         }
1626 }
1627
1628 static void ath10k_pci_tx_pipe_cleanup(struct ath10k_pci_pipe *pci_pipe)
1629 {
1630         struct ath10k *ar;
1631         struct ath10k_pci *ar_pci;
1632         struct ath10k_ce_pipe *ce_pipe;
1633         struct ath10k_ce_ring *ce_ring;
1634         struct sk_buff *skb;
1635         int i;
1636
1637         ar = pci_pipe->hif_ce_state;
1638         ar_pci = ath10k_pci_priv(ar);
1639         ce_pipe = pci_pipe->ce_hdl;
1640         ce_ring = ce_pipe->src_ring;
1641
1642         if (!ce_ring)
1643                 return;
1644
1645         if (!pci_pipe->buf_sz)
1646                 return;
1647
1648         for (i = 0; i < ce_ring->nentries; i++) {
1649                 skb = ce_ring->per_transfer_context[i];
1650                 if (!skb)
1651                         continue;
1652
1653                 ce_ring->per_transfer_context[i] = NULL;
1654
1655                 ath10k_htc_tx_completion_handler(ar, skb);
1656         }
1657 }
1658
1659 /*
1660  * Cleanup residual buffers for device shutdown:
1661  *    buffers that were enqueued for receive
1662  *    buffers that were to be sent
1663  * Note: Buffers that had completed but which were
1664  * not yet processed are on a completion queue. They
1665  * are handled when the completion thread shuts down.
1666  */
1667 static void ath10k_pci_buffer_cleanup(struct ath10k *ar)
1668 {
1669         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1670         int pipe_num;
1671
1672         for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
1673                 struct ath10k_pci_pipe *pipe_info;
1674
1675                 pipe_info = &ar_pci->pipe_info[pipe_num];
1676                 ath10k_pci_rx_pipe_cleanup(pipe_info);
1677                 ath10k_pci_tx_pipe_cleanup(pipe_info);
1678         }
1679 }
1680
1681 void ath10k_pci_ce_deinit(struct ath10k *ar)
1682 {
1683         int i;
1684
1685         for (i = 0; i < CE_COUNT; i++)
1686                 ath10k_ce_deinit_pipe(ar, i);
1687 }
1688
1689 void ath10k_pci_flush(struct ath10k *ar)
1690 {
1691         ath10k_pci_kill_tasklet(ar);
1692         ath10k_pci_buffer_cleanup(ar);
1693 }
1694
1695 static void ath10k_pci_hif_stop(struct ath10k *ar)
1696 {
1697         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1698         unsigned long flags;
1699
1700         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif stop\n");
1701
1702         /* Most likely the device has HTT Rx ring configured. The only way to
1703          * prevent the device from accessing (and possible corrupting) host
1704          * memory is to reset the chip now.
1705          *
1706          * There's also no known way of masking MSI interrupts on the device.
1707          * For ranged MSI the CE-related interrupts can be masked. However
1708          * regardless how many MSI interrupts are assigned the first one
1709          * is always used for firmware indications (crashes) and cannot be
1710          * masked. To prevent the device from asserting the interrupt reset it
1711          * before proceeding with cleanup.
1712          */
1713         ath10k_pci_safe_chip_reset(ar);
1714
1715         ath10k_pci_irq_disable(ar);
1716         ath10k_pci_irq_sync(ar);
1717         ath10k_pci_flush(ar);
1718
1719         spin_lock_irqsave(&ar_pci->ps_lock, flags);
1720         WARN_ON(ar_pci->ps_wake_refcount > 0);
1721         spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
1722 }
1723
1724 int ath10k_pci_hif_exchange_bmi_msg(struct ath10k *ar,
1725                                     void *req, u32 req_len,
1726                                     void *resp, u32 *resp_len)
1727 {
1728         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1729         struct ath10k_pci_pipe *pci_tx = &ar_pci->pipe_info[BMI_CE_NUM_TO_TARG];
1730         struct ath10k_pci_pipe *pci_rx = &ar_pci->pipe_info[BMI_CE_NUM_TO_HOST];
1731         struct ath10k_ce_pipe *ce_tx = pci_tx->ce_hdl;
1732         struct ath10k_ce_pipe *ce_rx = pci_rx->ce_hdl;
1733         dma_addr_t req_paddr = 0;
1734         dma_addr_t resp_paddr = 0;
1735         struct bmi_xfer xfer = {};
1736         void *treq, *tresp = NULL;
1737         int ret = 0;
1738
1739         might_sleep();
1740
1741         if (resp && !resp_len)
1742                 return -EINVAL;
1743
1744         if (resp && resp_len && *resp_len == 0)
1745                 return -EINVAL;
1746
1747         treq = kmemdup(req, req_len, GFP_KERNEL);
1748         if (!treq)
1749                 return -ENOMEM;
1750
1751         req_paddr = dma_map_single(ar->dev, treq, req_len, DMA_TO_DEVICE);
1752         ret = dma_mapping_error(ar->dev, req_paddr);
1753         if (ret) {
1754                 ret = -EIO;
1755                 goto err_dma;
1756         }
1757
1758         if (resp && resp_len) {
1759                 tresp = kzalloc(*resp_len, GFP_KERNEL);
1760                 if (!tresp) {
1761                         ret = -ENOMEM;
1762                         goto err_req;
1763                 }
1764
1765                 resp_paddr = dma_map_single(ar->dev, tresp, *resp_len,
1766                                             DMA_FROM_DEVICE);
1767                 ret = dma_mapping_error(ar->dev, resp_paddr);
1768                 if (ret) {
1769                         ret = -EIO;
1770                         goto err_req;
1771                 }
1772
1773                 xfer.wait_for_resp = true;
1774                 xfer.resp_len = 0;
1775
1776                 ath10k_ce_rx_post_buf(ce_rx, &xfer, resp_paddr);
1777         }
1778
1779         ret = ath10k_ce_send(ce_tx, &xfer, req_paddr, req_len, -1, 0);
1780         if (ret)
1781                 goto err_resp;
1782
1783         ret = ath10k_pci_bmi_wait(ce_tx, ce_rx, &xfer);
1784         if (ret) {
1785                 u32 unused_buffer;
1786                 unsigned int unused_nbytes;
1787                 unsigned int unused_id;
1788
1789                 ath10k_ce_cancel_send_next(ce_tx, NULL, &unused_buffer,
1790                                            &unused_nbytes, &unused_id);
1791         } else {
1792                 /* non-zero means we did not time out */
1793                 ret = 0;
1794         }
1795
1796 err_resp:
1797         if (resp) {
1798                 u32 unused_buffer;
1799
1800                 ath10k_ce_revoke_recv_next(ce_rx, NULL, &unused_buffer);
1801                 dma_unmap_single(ar->dev, resp_paddr,
1802                                  *resp_len, DMA_FROM_DEVICE);
1803         }
1804 err_req:
1805         dma_unmap_single(ar->dev, req_paddr, req_len, DMA_TO_DEVICE);
1806
1807         if (ret == 0 && resp_len) {
1808                 *resp_len = min(*resp_len, xfer.resp_len);
1809                 memcpy(resp, tresp, xfer.resp_len);
1810         }
1811 err_dma:
1812         kfree(treq);
1813         kfree(tresp);
1814
1815         return ret;
1816 }
1817
1818 static void ath10k_pci_bmi_send_done(struct ath10k_ce_pipe *ce_state)
1819 {
1820         struct bmi_xfer *xfer;
1821
1822         if (ath10k_ce_completed_send_next(ce_state, (void **)&xfer))
1823                 return;
1824
1825         xfer->tx_done = true;
1826 }
1827
1828 static void ath10k_pci_bmi_recv_data(struct ath10k_ce_pipe *ce_state)
1829 {
1830         struct ath10k *ar = ce_state->ar;
1831         struct bmi_xfer *xfer;
1832         unsigned int nbytes;
1833
1834         if (ath10k_ce_completed_recv_next(ce_state, (void **)&xfer,
1835                                           &nbytes))
1836                 return;
1837
1838         if (WARN_ON_ONCE(!xfer))
1839                 return;
1840
1841         if (!xfer->wait_for_resp) {
1842                 ath10k_warn(ar, "unexpected: BMI data received; ignoring\n");
1843                 return;
1844         }
1845
1846         xfer->resp_len = nbytes;
1847         xfer->rx_done = true;
1848 }
1849
1850 static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
1851                                struct ath10k_ce_pipe *rx_pipe,
1852                                struct bmi_xfer *xfer)
1853 {
1854         unsigned long timeout = jiffies + BMI_COMMUNICATION_TIMEOUT_HZ;
1855
1856         while (time_before_eq(jiffies, timeout)) {
1857                 ath10k_pci_bmi_send_done(tx_pipe);
1858                 ath10k_pci_bmi_recv_data(rx_pipe);
1859
1860                 if (xfer->tx_done && (xfer->rx_done == xfer->wait_for_resp))
1861                         return 0;
1862
1863                 schedule();
1864         }
1865
1866         return -ETIMEDOUT;
1867 }
1868
1869 /*
1870  * Send an interrupt to the device to wake up the Target CPU
1871  * so it has an opportunity to notice any changed state.
1872  */
1873 static int ath10k_pci_wake_target_cpu(struct ath10k *ar)
1874 {
1875         u32 addr, val;
1876
1877         addr = SOC_CORE_BASE_ADDRESS | CORE_CTRL_ADDRESS;
1878         val = ath10k_pci_read32(ar, addr);
1879         val |= CORE_CTRL_CPU_INTR_MASK;
1880         ath10k_pci_write32(ar, addr, val);
1881
1882         return 0;
1883 }
1884
1885 static int ath10k_pci_get_num_banks(struct ath10k *ar)
1886 {
1887         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1888
1889         switch (ar_pci->pdev->device) {
1890         case QCA988X_2_0_DEVICE_ID:
1891         case QCA99X0_2_0_DEVICE_ID:
1892                 return 1;
1893         case QCA6164_2_1_DEVICE_ID:
1894         case QCA6174_2_1_DEVICE_ID:
1895                 switch (MS(ar->chip_id, SOC_CHIP_ID_REV)) {
1896                 case QCA6174_HW_1_0_CHIP_ID_REV:
1897                 case QCA6174_HW_1_1_CHIP_ID_REV:
1898                 case QCA6174_HW_2_1_CHIP_ID_REV:
1899                 case QCA6174_HW_2_2_CHIP_ID_REV:
1900                         return 3;
1901                 case QCA6174_HW_1_3_CHIP_ID_REV:
1902                         return 2;
1903                 case QCA6174_HW_3_0_CHIP_ID_REV:
1904                 case QCA6174_HW_3_1_CHIP_ID_REV:
1905                 case QCA6174_HW_3_2_CHIP_ID_REV:
1906                         return 9;
1907                 }
1908                 break;
1909         case QCA9377_1_0_DEVICE_ID:
1910                 return 2;
1911         }
1912
1913         ath10k_warn(ar, "unknown number of banks, assuming 1\n");
1914         return 1;
1915 }
1916
1917 static int ath10k_bus_get_num_banks(struct ath10k *ar)
1918 {
1919         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1920
1921         return ar_pci->bus_ops->get_num_banks(ar);
1922 }
1923
1924 int ath10k_pci_init_config(struct ath10k *ar)
1925 {
1926         u32 interconnect_targ_addr;
1927         u32 pcie_state_targ_addr = 0;
1928         u32 pipe_cfg_targ_addr = 0;
1929         u32 svc_to_pipe_map = 0;
1930         u32 pcie_config_flags = 0;
1931         u32 ealloc_value;
1932         u32 ealloc_targ_addr;
1933         u32 flag2_value;
1934         u32 flag2_targ_addr;
1935         int ret = 0;
1936
1937         /* Download to Target the CE Config and the service-to-CE map */
1938         interconnect_targ_addr =
1939                 host_interest_item_address(HI_ITEM(hi_interconnect_state));
1940
1941         /* Supply Target-side CE configuration */
1942         ret = ath10k_pci_diag_read32(ar, interconnect_targ_addr,
1943                                      &pcie_state_targ_addr);
1944         if (ret != 0) {
1945                 ath10k_err(ar, "Failed to get pcie state addr: %d\n", ret);
1946                 return ret;
1947         }
1948
1949         if (pcie_state_targ_addr == 0) {
1950                 ret = -EIO;
1951                 ath10k_err(ar, "Invalid pcie state addr\n");
1952                 return ret;
1953         }
1954
1955         ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
1956                                           offsetof(struct pcie_state,
1957                                                    pipe_cfg_addr)),
1958                                      &pipe_cfg_targ_addr);
1959         if (ret != 0) {
1960                 ath10k_err(ar, "Failed to get pipe cfg addr: %d\n", ret);
1961                 return ret;
1962         }
1963
1964         if (pipe_cfg_targ_addr == 0) {
1965                 ret = -EIO;
1966                 ath10k_err(ar, "Invalid pipe cfg addr\n");
1967                 return ret;
1968         }
1969
1970         ret = ath10k_pci_diag_write_mem(ar, pipe_cfg_targ_addr,
1971                                         target_ce_config_wlan,
1972                                         sizeof(struct ce_pipe_config) *
1973                                         NUM_TARGET_CE_CONFIG_WLAN);
1974
1975         if (ret != 0) {
1976                 ath10k_err(ar, "Failed to write pipe cfg: %d\n", ret);
1977                 return ret;
1978         }
1979
1980         ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
1981                                           offsetof(struct pcie_state,
1982                                                    svc_to_pipe_map)),
1983                                      &svc_to_pipe_map);
1984         if (ret != 0) {
1985                 ath10k_err(ar, "Failed to get svc/pipe map: %d\n", ret);
1986                 return ret;
1987         }
1988
1989         if (svc_to_pipe_map == 0) {
1990                 ret = -EIO;
1991                 ath10k_err(ar, "Invalid svc_to_pipe map\n");
1992                 return ret;
1993         }
1994
1995         ret = ath10k_pci_diag_write_mem(ar, svc_to_pipe_map,
1996                                         target_service_to_ce_map_wlan,
1997                                         sizeof(target_service_to_ce_map_wlan));
1998         if (ret != 0) {
1999                 ath10k_err(ar, "Failed to write svc/pipe map: %d\n", ret);
2000                 return ret;
2001         }
2002
2003         ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
2004                                           offsetof(struct pcie_state,
2005                                                    config_flags)),
2006                                      &pcie_config_flags);
2007         if (ret != 0) {
2008                 ath10k_err(ar, "Failed to get pcie config_flags: %d\n", ret);
2009                 return ret;
2010         }
2011
2012         pcie_config_flags &= ~PCIE_CONFIG_FLAG_ENABLE_L1;
2013
2014         ret = ath10k_pci_diag_write32(ar, (pcie_state_targ_addr +
2015                                            offsetof(struct pcie_state,
2016                                                     config_flags)),
2017                                       pcie_config_flags);
2018         if (ret != 0) {
2019                 ath10k_err(ar, "Failed to write pcie config_flags: %d\n", ret);
2020                 return ret;
2021         }
2022
2023         /* configure early allocation */
2024         ealloc_targ_addr = host_interest_item_address(HI_ITEM(hi_early_alloc));
2025
2026         ret = ath10k_pci_diag_read32(ar, ealloc_targ_addr, &ealloc_value);
2027         if (ret != 0) {
2028                 ath10k_err(ar, "Faile to get early alloc val: %d\n", ret);
2029                 return ret;
2030         }
2031
2032         /* first bank is switched to IRAM */
2033         ealloc_value |= ((HI_EARLY_ALLOC_MAGIC << HI_EARLY_ALLOC_MAGIC_SHIFT) &
2034                          HI_EARLY_ALLOC_MAGIC_MASK);
2035         ealloc_value |= ((ath10k_bus_get_num_banks(ar) <<
2036                           HI_EARLY_ALLOC_IRAM_BANKS_SHIFT) &
2037                          HI_EARLY_ALLOC_IRAM_BANKS_MASK);
2038
2039         ret = ath10k_pci_diag_write32(ar, ealloc_targ_addr, ealloc_value);
2040         if (ret != 0) {
2041                 ath10k_err(ar, "Failed to set early alloc val: %d\n", ret);
2042                 return ret;
2043         }
2044
2045         /* Tell Target to proceed with initialization */
2046         flag2_targ_addr = host_interest_item_address(HI_ITEM(hi_option_flag2));
2047
2048         ret = ath10k_pci_diag_read32(ar, flag2_targ_addr, &flag2_value);
2049         if (ret != 0) {
2050                 ath10k_err(ar, "Failed to get option val: %d\n", ret);
2051                 return ret;
2052         }
2053
2054         flag2_value |= HI_OPTION_EARLY_CFG_DONE;
2055
2056         ret = ath10k_pci_diag_write32(ar, flag2_targ_addr, flag2_value);
2057         if (ret != 0) {
2058                 ath10k_err(ar, "Failed to set option val: %d\n", ret);
2059                 return ret;
2060         }
2061
2062         return 0;
2063 }
2064
2065 static void ath10k_pci_override_ce_config(struct ath10k *ar)
2066 {
2067         struct ce_attr *attr;
2068         struct ce_pipe_config *config;
2069
2070         /* For QCA6174 we're overriding the Copy Engine 5 configuration,
2071          * since it is currently used for other feature.
2072          */
2073
2074         /* Override Host's Copy Engine 5 configuration */
2075         attr = &host_ce_config_wlan[5];
2076         attr->src_sz_max = 0;
2077         attr->dest_nentries = 0;
2078
2079         /* Override Target firmware's Copy Engine configuration */
2080         config = &target_ce_config_wlan[5];
2081         config->pipedir = __cpu_to_le32(PIPEDIR_OUT);
2082         config->nbytes_max = __cpu_to_le32(2048);
2083
2084         /* Map from service/endpoint to Copy Engine */
2085         target_service_to_ce_map_wlan[15].pipenum = __cpu_to_le32(1);
2086 }
2087
2088 int ath10k_pci_alloc_pipes(struct ath10k *ar)
2089 {
2090         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2091         struct ath10k_pci_pipe *pipe;
2092         int i, ret;
2093
2094         for (i = 0; i < CE_COUNT; i++) {
2095                 pipe = &ar_pci->pipe_info[i];
2096                 pipe->ce_hdl = &ar_pci->ce_states[i];
2097                 pipe->pipe_num = i;
2098                 pipe->hif_ce_state = ar;
2099
2100                 ret = ath10k_ce_alloc_pipe(ar, i, &host_ce_config_wlan[i]);
2101                 if (ret) {
2102                         ath10k_err(ar, "failed to allocate copy engine pipe %d: %d\n",
2103                                    i, ret);
2104                         return ret;
2105                 }
2106
2107                 /* Last CE is Diagnostic Window */
2108                 if (i == CE_DIAG_PIPE) {
2109                         ar_pci->ce_diag = pipe->ce_hdl;
2110                         continue;
2111                 }
2112
2113                 pipe->buf_sz = (size_t)(host_ce_config_wlan[i].src_sz_max);
2114         }
2115
2116         return 0;
2117 }
2118
2119 void ath10k_pci_free_pipes(struct ath10k *ar)
2120 {
2121         int i;
2122
2123         for (i = 0; i < CE_COUNT; i++)
2124                 ath10k_ce_free_pipe(ar, i);
2125 }
2126
2127 int ath10k_pci_init_pipes(struct ath10k *ar)
2128 {
2129         int i, ret;
2130
2131         for (i = 0; i < CE_COUNT; i++) {
2132                 ret = ath10k_ce_init_pipe(ar, i, &host_ce_config_wlan[i]);
2133                 if (ret) {
2134                         ath10k_err(ar, "failed to initialize copy engine pipe %d: %d\n",
2135                                    i, ret);
2136                         return ret;
2137                 }
2138         }
2139
2140         return 0;
2141 }
2142
2143 static bool ath10k_pci_has_fw_crashed(struct ath10k *ar)
2144 {
2145         return ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS) &
2146                FW_IND_EVENT_PENDING;
2147 }
2148
2149 static void ath10k_pci_fw_crashed_clear(struct ath10k *ar)
2150 {
2151         u32 val;
2152
2153         val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
2154         val &= ~FW_IND_EVENT_PENDING;
2155         ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, val);
2156 }
2157
2158 /* this function effectively clears target memory controller assert line */
2159 static void ath10k_pci_warm_reset_si0(struct ath10k *ar)
2160 {
2161         u32 val;
2162
2163         val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
2164         ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
2165                                val | SOC_RESET_CONTROL_SI0_RST_MASK);
2166         val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
2167
2168         msleep(10);
2169
2170         val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
2171         ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
2172                                val & ~SOC_RESET_CONTROL_SI0_RST_MASK);
2173         val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
2174
2175         msleep(10);
2176 }
2177
2178 static void ath10k_pci_warm_reset_cpu(struct ath10k *ar)
2179 {
2180         u32 val;
2181
2182         ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, 0);
2183
2184         val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
2185                                 SOC_RESET_CONTROL_ADDRESS);
2186         ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
2187                            val | SOC_RESET_CONTROL_CPU_WARM_RST_MASK);
2188 }
2189
2190 static void ath10k_pci_warm_reset_ce(struct ath10k *ar)
2191 {
2192         u32 val;
2193
2194         val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
2195                                 SOC_RESET_CONTROL_ADDRESS);
2196
2197         ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
2198                            val | SOC_RESET_CONTROL_CE_RST_MASK);
2199         msleep(10);
2200         ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
2201                            val & ~SOC_RESET_CONTROL_CE_RST_MASK);
2202 }
2203
2204 static void ath10k_pci_warm_reset_clear_lf(struct ath10k *ar)
2205 {
2206         u32 val;
2207
2208         val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
2209                                 SOC_LF_TIMER_CONTROL0_ADDRESS);
2210         ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS +
2211                            SOC_LF_TIMER_CONTROL0_ADDRESS,
2212                            val & ~SOC_LF_TIMER_CONTROL0_ENABLE_MASK);
2213 }
2214
2215 static int ath10k_pci_warm_reset(struct ath10k *ar)
2216 {
2217         int ret;
2218
2219         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot warm reset\n");
2220
2221         spin_lock_bh(&ar->data_lock);
2222         ar->stats.fw_warm_reset_counter++;
2223         spin_unlock_bh(&ar->data_lock);
2224
2225         ath10k_pci_irq_disable(ar);
2226
2227         /* Make sure the target CPU is not doing anything dangerous, e.g. if it
2228          * were to access copy engine while host performs copy engine reset
2229          * then it is possible for the device to confuse pci-e controller to
2230          * the point of bringing host system to a complete stop (i.e. hang).
2231          */
2232         ath10k_pci_warm_reset_si0(ar);
2233         ath10k_pci_warm_reset_cpu(ar);
2234         ath10k_pci_init_pipes(ar);
2235         ath10k_pci_wait_for_target_init(ar);
2236
2237         ath10k_pci_warm_reset_clear_lf(ar);
2238         ath10k_pci_warm_reset_ce(ar);
2239         ath10k_pci_warm_reset_cpu(ar);
2240         ath10k_pci_init_pipes(ar);
2241
2242         ret = ath10k_pci_wait_for_target_init(ar);
2243         if (ret) {
2244                 ath10k_warn(ar, "failed to wait for target init: %d\n", ret);
2245                 return ret;
2246         }
2247
2248         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot warm reset complete\n");
2249
2250         return 0;
2251 }
2252
2253 static int ath10k_pci_safe_chip_reset(struct ath10k *ar)
2254 {
2255         if (QCA_REV_988X(ar) || QCA_REV_6174(ar)) {
2256                 return ath10k_pci_warm_reset(ar);
2257         } else if (QCA_REV_99X0(ar)) {
2258                 ath10k_pci_irq_disable(ar);
2259                 return ath10k_pci_qca99x0_chip_reset(ar);
2260         } else {
2261                 return -ENOTSUPP;
2262         }
2263 }
2264
2265 static int ath10k_pci_qca988x_chip_reset(struct ath10k *ar)
2266 {
2267         int i, ret;
2268         u32 val;
2269
2270         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot 988x chip reset\n");
2271
2272         /* Some hardware revisions (e.g. CUS223v2) has issues with cold reset.
2273          * It is thus preferred to use warm reset which is safer but may not be
2274          * able to recover the device from all possible fail scenarios.
2275          *
2276          * Warm reset doesn't always work on first try so attempt it a few
2277          * times before giving up.
2278          */
2279         for (i = 0; i < ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS; i++) {
2280                 ret = ath10k_pci_warm_reset(ar);
2281                 if (ret) {
2282                         ath10k_warn(ar, "failed to warm reset attempt %d of %d: %d\n",
2283                                     i + 1, ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS,
2284                                     ret);
2285                         continue;
2286                 }
2287
2288                 /* FIXME: Sometimes copy engine doesn't recover after warm
2289                  * reset. In most cases this needs cold reset. In some of these
2290                  * cases the device is in such a state that a cold reset may
2291                  * lock up the host.
2292                  *
2293                  * Reading any host interest register via copy engine is
2294                  * sufficient to verify if device is capable of booting
2295                  * firmware blob.
2296                  */
2297                 ret = ath10k_pci_init_pipes(ar);
2298                 if (ret) {
2299                         ath10k_warn(ar, "failed to init copy engine: %d\n",
2300                                     ret);
2301                         continue;
2302                 }
2303
2304                 ret = ath10k_pci_diag_read32(ar, QCA988X_HOST_INTEREST_ADDRESS,
2305                                              &val);
2306                 if (ret) {
2307                         ath10k_warn(ar, "failed to poke copy engine: %d\n",
2308                                     ret);
2309                         continue;
2310                 }
2311
2312                 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot chip reset complete (warm)\n");
2313                 return 0;
2314         }
2315
2316         if (ath10k_pci_reset_mode == ATH10K_PCI_RESET_WARM_ONLY) {
2317                 ath10k_warn(ar, "refusing cold reset as requested\n");
2318                 return -EPERM;
2319         }
2320
2321         ret = ath10k_pci_cold_reset(ar);
2322         if (ret) {
2323                 ath10k_warn(ar, "failed to cold reset: %d\n", ret);
2324                 return ret;
2325         }
2326
2327         ret = ath10k_pci_wait_for_target_init(ar);
2328         if (ret) {
2329                 ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
2330                             ret);
2331                 return ret;
2332         }
2333
2334         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca988x chip reset complete (cold)\n");
2335
2336         return 0;
2337 }
2338
2339 static int ath10k_pci_qca6174_chip_reset(struct ath10k *ar)
2340 {
2341         int ret;
2342
2343         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca6174 chip reset\n");
2344
2345         /* FIXME: QCA6174 requires cold + warm reset to work. */
2346
2347         ret = ath10k_pci_cold_reset(ar);
2348         if (ret) {
2349                 ath10k_warn(ar, "failed to cold reset: %d\n", ret);
2350                 return ret;
2351         }
2352
2353         ret = ath10k_pci_wait_for_target_init(ar);
2354         if (ret) {
2355                 ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
2356                             ret);
2357                 return ret;
2358         }
2359
2360         ret = ath10k_pci_warm_reset(ar);
2361         if (ret) {
2362                 ath10k_warn(ar, "failed to warm reset: %d\n", ret);
2363                 return ret;
2364         }
2365
2366         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca6174 chip reset complete (cold)\n");
2367
2368         return 0;
2369 }
2370
2371 static int ath10k_pci_qca99x0_chip_reset(struct ath10k *ar)
2372 {
2373         int ret;
2374
2375         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca99x0 chip reset\n");
2376
2377         ret = ath10k_pci_cold_reset(ar);
2378         if (ret) {
2379                 ath10k_warn(ar, "failed to cold reset: %d\n", ret);
2380                 return ret;
2381         }
2382
2383         ret = ath10k_pci_wait_for_target_init(ar);
2384         if (ret) {
2385                 ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
2386                             ret);
2387                 return ret;
2388         }
2389
2390         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca99x0 chip reset complete (cold)\n");
2391
2392         return 0;
2393 }
2394
2395 static int ath10k_pci_chip_reset(struct ath10k *ar)
2396 {
2397         if (QCA_REV_988X(ar))
2398                 return ath10k_pci_qca988x_chip_reset(ar);
2399         else if (QCA_REV_6174(ar))
2400                 return ath10k_pci_qca6174_chip_reset(ar);
2401         else if (QCA_REV_9377(ar))
2402                 return ath10k_pci_qca6174_chip_reset(ar);
2403         else if (QCA_REV_99X0(ar))
2404                 return ath10k_pci_qca99x0_chip_reset(ar);
2405         else
2406                 return -ENOTSUPP;
2407 }
2408
2409 static int ath10k_pci_hif_power_up(struct ath10k *ar)
2410 {
2411         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2412         int ret;
2413
2414         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif power up\n");
2415
2416         pcie_capability_read_word(ar_pci->pdev, PCI_EXP_LNKCTL,
2417                                   &ar_pci->link_ctl);
2418         pcie_capability_write_word(ar_pci->pdev, PCI_EXP_LNKCTL,
2419                                    ar_pci->link_ctl & ~PCI_EXP_LNKCTL_ASPMC);
2420
2421         /*
2422          * Bring the target up cleanly.
2423          *
2424          * The target may be in an undefined state with an AUX-powered Target
2425          * and a Host in WoW mode. If the Host crashes, loses power, or is
2426          * restarted (without unloading the driver) then the Target is left
2427          * (aux) powered and running. On a subsequent driver load, the Target
2428          * is in an unexpected state. We try to catch that here in order to
2429          * reset the Target and retry the probe.
2430          */
2431         ret = ath10k_pci_chip_reset(ar);
2432         if (ret) {
2433                 if (ath10k_pci_has_fw_crashed(ar)) {
2434                         ath10k_warn(ar, "firmware crashed during chip reset\n");
2435                         ath10k_pci_fw_crashed_clear(ar);
2436                         ath10k_pci_fw_crashed_dump(ar);
2437                 }
2438
2439                 ath10k_err(ar, "failed to reset chip: %d\n", ret);
2440                 goto err_sleep;
2441         }
2442
2443         ret = ath10k_pci_init_pipes(ar);
2444         if (ret) {
2445                 ath10k_err(ar, "failed to initialize CE: %d\n", ret);
2446                 goto err_sleep;
2447         }
2448
2449         ret = ath10k_pci_init_config(ar);
2450         if (ret) {
2451                 ath10k_err(ar, "failed to setup init config: %d\n", ret);
2452                 goto err_ce;
2453         }
2454
2455         ret = ath10k_pci_wake_target_cpu(ar);
2456         if (ret) {
2457                 ath10k_err(ar, "could not wake up target CPU: %d\n", ret);
2458                 goto err_ce;
2459         }
2460
2461         return 0;
2462
2463 err_ce:
2464         ath10k_pci_ce_deinit(ar);
2465
2466 err_sleep:
2467         return ret;
2468 }
2469
2470 void ath10k_pci_hif_power_down(struct ath10k *ar)
2471 {
2472         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif power down\n");
2473
2474         /* Currently hif_power_up performs effectively a reset and hif_stop
2475          * resets the chip as well so there's no point in resetting here.
2476          */
2477 }
2478
2479 #ifdef CONFIG_PM
2480
2481 static int ath10k_pci_hif_suspend(struct ath10k *ar)
2482 {
2483         /* The grace timer can still be counting down and ar->ps_awake be true.
2484          * It is known that the device may be asleep after resuming regardless
2485          * of the SoC powersave state before suspending. Hence make sure the
2486          * device is asleep before proceeding.
2487          */
2488         ath10k_pci_sleep_sync(ar);
2489
2490         return 0;
2491 }
2492
2493 static int ath10k_pci_hif_resume(struct ath10k *ar)
2494 {
2495         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2496         struct pci_dev *pdev = ar_pci->pdev;
2497         u32 val;
2498         int ret = 0;
2499
2500         ret = ath10k_pci_force_wake(ar);
2501         if (ret) {
2502                 ath10k_err(ar, "failed to wake up target: %d\n", ret);
2503                 return ret;
2504         }
2505
2506         /* Suspend/Resume resets the PCI configuration space, so we have to
2507          * re-disable the RETRY_TIMEOUT register (0x41) to keep PCI Tx retries
2508          * from interfering with C3 CPU state. pci_restore_state won't help
2509          * here since it only restores the first 64 bytes pci config header.
2510          */
2511         pci_read_config_dword(pdev, 0x40, &val);
2512         if ((val & 0x0000ff00) != 0)
2513                 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
2514
2515         return ret;
2516 }
2517 #endif
2518
2519 static const struct ath10k_hif_ops ath10k_pci_hif_ops = {
2520         .tx_sg                  = ath10k_pci_hif_tx_sg,
2521         .diag_read              = ath10k_pci_hif_diag_read,
2522         .diag_write             = ath10k_pci_diag_write_mem,
2523         .exchange_bmi_msg       = ath10k_pci_hif_exchange_bmi_msg,
2524         .start                  = ath10k_pci_hif_start,
2525         .stop                   = ath10k_pci_hif_stop,
2526         .map_service_to_pipe    = ath10k_pci_hif_map_service_to_pipe,
2527         .get_default_pipe       = ath10k_pci_hif_get_default_pipe,
2528         .send_complete_check    = ath10k_pci_hif_send_complete_check,
2529         .get_free_queue_number  = ath10k_pci_hif_get_free_queue_number,
2530         .power_up               = ath10k_pci_hif_power_up,
2531         .power_down             = ath10k_pci_hif_power_down,
2532         .read32                 = ath10k_pci_read32,
2533         .write32                = ath10k_pci_write32,
2534 #ifdef CONFIG_PM
2535         .suspend                = ath10k_pci_hif_suspend,
2536         .resume                 = ath10k_pci_hif_resume,
2537 #endif
2538 };
2539
2540 static void ath10k_pci_ce_tasklet(unsigned long ptr)
2541 {
2542         struct ath10k_pci_pipe *pipe = (struct ath10k_pci_pipe *)ptr;
2543         struct ath10k_pci *ar_pci = pipe->ar_pci;
2544
2545         ath10k_ce_per_engine_service(ar_pci->ar, pipe->pipe_num);
2546 }
2547
2548 static void ath10k_msi_err_tasklet(unsigned long data)
2549 {
2550         struct ath10k *ar = (struct ath10k *)data;
2551
2552         if (!ath10k_pci_has_fw_crashed(ar)) {
2553                 ath10k_warn(ar, "received unsolicited fw crash interrupt\n");
2554                 return;
2555         }
2556
2557         ath10k_pci_irq_disable(ar);
2558         ath10k_pci_fw_crashed_clear(ar);
2559         ath10k_pci_fw_crashed_dump(ar);
2560 }
2561
2562 /*
2563  * Handler for a per-engine interrupt on a PARTICULAR CE.
2564  * This is used in cases where each CE has a private MSI interrupt.
2565  */
2566 static irqreturn_t ath10k_pci_per_engine_handler(int irq, void *arg)
2567 {
2568         struct ath10k *ar = arg;
2569         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2570         int ce_id = irq - ar_pci->pdev->irq - MSI_ASSIGN_CE_INITIAL;
2571
2572         if (ce_id < 0 || ce_id >= ARRAY_SIZE(ar_pci->pipe_info)) {
2573                 ath10k_warn(ar, "unexpected/invalid irq %d ce_id %d\n", irq,
2574                             ce_id);
2575                 return IRQ_HANDLED;
2576         }
2577
2578         /*
2579          * NOTE: We are able to derive ce_id from irq because we
2580          * use a one-to-one mapping for CE's 0..5.
2581          * CE's 6 & 7 do not use interrupts at all.
2582          *
2583          * This mapping must be kept in sync with the mapping
2584          * used by firmware.
2585          */
2586         tasklet_schedule(&ar_pci->pipe_info[ce_id].intr);
2587         return IRQ_HANDLED;
2588 }
2589
2590 static irqreturn_t ath10k_pci_msi_fw_handler(int irq, void *arg)
2591 {
2592         struct ath10k *ar = arg;
2593         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2594
2595         tasklet_schedule(&ar_pci->msi_fw_err);
2596         return IRQ_HANDLED;
2597 }
2598
2599 /*
2600  * Top-level interrupt handler for all PCI interrupts from a Target.
2601  * When a block of MSI interrupts is allocated, this top-level handler
2602  * is not used; instead, we directly call the correct sub-handler.
2603  */
2604 static irqreturn_t ath10k_pci_interrupt_handler(int irq, void *arg)
2605 {
2606         struct ath10k *ar = arg;
2607         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2608         int ret;
2609
2610         ret = ath10k_pci_force_wake(ar);
2611         if (ret) {
2612                 ath10k_warn(ar, "failed to wake device up on irq: %d\n", ret);
2613                 return IRQ_NONE;
2614         }
2615
2616         if (ar_pci->num_msi_intrs == 0) {
2617                 if (!ath10k_pci_irq_pending(ar))
2618                         return IRQ_NONE;
2619
2620                 ath10k_pci_disable_and_clear_legacy_irq(ar);
2621         }
2622
2623         tasklet_schedule(&ar_pci->intr_tq);
2624
2625         return IRQ_HANDLED;
2626 }
2627
2628 static void ath10k_pci_tasklet(unsigned long data)
2629 {
2630         struct ath10k *ar = (struct ath10k *)data;
2631         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2632
2633         if (ath10k_pci_has_fw_crashed(ar)) {
2634                 ath10k_pci_irq_disable(ar);
2635                 ath10k_pci_fw_crashed_clear(ar);
2636                 ath10k_pci_fw_crashed_dump(ar);
2637                 return;
2638         }
2639
2640         ath10k_ce_per_engine_service_any(ar);
2641
2642         /* Re-enable legacy irq that was disabled in the irq handler */
2643         if (ar_pci->num_msi_intrs == 0)
2644                 ath10k_pci_enable_legacy_irq(ar);
2645 }
2646
2647 static int ath10k_pci_request_irq_msix(struct ath10k *ar)
2648 {
2649         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2650         int ret, i;
2651
2652         ret = request_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW,
2653                           ath10k_pci_msi_fw_handler,
2654                           IRQF_SHARED, "ath10k_pci", ar);
2655         if (ret) {
2656                 ath10k_warn(ar, "failed to request MSI-X fw irq %d: %d\n",
2657                             ar_pci->pdev->irq + MSI_ASSIGN_FW, ret);
2658                 return ret;
2659         }
2660
2661         for (i = MSI_ASSIGN_CE_INITIAL; i <= MSI_ASSIGN_CE_MAX; i++) {
2662                 ret = request_irq(ar_pci->pdev->irq + i,
2663                                   ath10k_pci_per_engine_handler,
2664                                   IRQF_SHARED, "ath10k_pci", ar);
2665                 if (ret) {
2666                         ath10k_warn(ar, "failed to request MSI-X ce irq %d: %d\n",
2667                                     ar_pci->pdev->irq + i, ret);
2668
2669                         for (i--; i >= MSI_ASSIGN_CE_INITIAL; i--)
2670                                 free_irq(ar_pci->pdev->irq + i, ar);
2671
2672                         free_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW, ar);
2673                         return ret;
2674                 }
2675         }
2676
2677         return 0;
2678 }
2679
2680 static int ath10k_pci_request_irq_msi(struct ath10k *ar)
2681 {
2682         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2683         int ret;
2684
2685         ret = request_irq(ar_pci->pdev->irq,
2686                           ath10k_pci_interrupt_handler,
2687                           IRQF_SHARED, "ath10k_pci", ar);
2688         if (ret) {
2689                 ath10k_warn(ar, "failed to request MSI irq %d: %d\n",
2690                             ar_pci->pdev->irq, ret);
2691                 return ret;
2692         }
2693
2694         return 0;
2695 }
2696
2697 static int ath10k_pci_request_irq_legacy(struct ath10k *ar)
2698 {
2699         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2700         int ret;
2701
2702         ret = request_irq(ar_pci->pdev->irq,
2703                           ath10k_pci_interrupt_handler,
2704                           IRQF_SHARED, "ath10k_pci", ar);
2705         if (ret) {
2706                 ath10k_warn(ar, "failed to request legacy irq %d: %d\n",
2707                             ar_pci->pdev->irq, ret);
2708                 return ret;
2709         }
2710
2711         return 0;
2712 }
2713
2714 static int ath10k_pci_request_irq(struct ath10k *ar)
2715 {
2716         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2717
2718         switch (ar_pci->num_msi_intrs) {
2719         case 0:
2720                 return ath10k_pci_request_irq_legacy(ar);
2721         case 1:
2722                 return ath10k_pci_request_irq_msi(ar);
2723         default:
2724                 return ath10k_pci_request_irq_msix(ar);
2725         }
2726 }
2727
2728 static void ath10k_pci_free_irq(struct ath10k *ar)
2729 {
2730         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2731         int i;
2732
2733         /* There's at least one interrupt irregardless whether its legacy INTR
2734          * or MSI or MSI-X */
2735         for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
2736                 free_irq(ar_pci->pdev->irq + i, ar);
2737 }
2738
2739 void ath10k_pci_init_irq_tasklets(struct ath10k *ar)
2740 {
2741         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2742         int i;
2743
2744         tasklet_init(&ar_pci->intr_tq, ath10k_pci_tasklet, (unsigned long)ar);
2745         tasklet_init(&ar_pci->msi_fw_err, ath10k_msi_err_tasklet,
2746                      (unsigned long)ar);
2747
2748         for (i = 0; i < CE_COUNT; i++) {
2749                 ar_pci->pipe_info[i].ar_pci = ar_pci;
2750                 tasklet_init(&ar_pci->pipe_info[i].intr, ath10k_pci_ce_tasklet,
2751                              (unsigned long)&ar_pci->pipe_info[i]);
2752         }
2753 }
2754
2755 static int ath10k_pci_init_irq(struct ath10k *ar)
2756 {
2757         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2758         int ret;
2759
2760         ath10k_pci_init_irq_tasklets(ar);
2761
2762         if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_AUTO)
2763                 ath10k_info(ar, "limiting irq mode to: %d\n",
2764                             ath10k_pci_irq_mode);
2765
2766         /* Try MSI-X */
2767         if (ath10k_pci_irq_mode == ATH10K_PCI_IRQ_AUTO) {
2768                 ar_pci->num_msi_intrs = MSI_ASSIGN_CE_MAX + 1;
2769                 ret = pci_enable_msi_range(ar_pci->pdev, ar_pci->num_msi_intrs,
2770                                            ar_pci->num_msi_intrs);
2771                 if (ret > 0)
2772                         return 0;
2773
2774                 /* fall-through */
2775         }
2776
2777         /* Try MSI */
2778         if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_LEGACY) {
2779                 ar_pci->num_msi_intrs = 1;
2780                 ret = pci_enable_msi(ar_pci->pdev);
2781                 if (ret == 0)
2782                         return 0;
2783
2784                 /* fall-through */
2785         }
2786
2787         /* Try legacy irq
2788          *
2789          * A potential race occurs here: The CORE_BASE write
2790          * depends on target correctly decoding AXI address but
2791          * host won't know when target writes BAR to CORE_CTRL.
2792          * This write might get lost if target has NOT written BAR.
2793          * For now, fix the race by repeating the write in below
2794          * synchronization checking. */
2795         ar_pci->num_msi_intrs = 0;
2796
2797         ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2798                            PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
2799
2800         return 0;
2801 }
2802
2803 static void ath10k_pci_deinit_irq_legacy(struct ath10k *ar)
2804 {
2805         ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2806                            0);
2807 }
2808
2809 static int ath10k_pci_deinit_irq(struct ath10k *ar)
2810 {
2811         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2812
2813         switch (ar_pci->num_msi_intrs) {
2814         case 0:
2815                 ath10k_pci_deinit_irq_legacy(ar);
2816                 break;
2817         default:
2818                 pci_disable_msi(ar_pci->pdev);
2819                 break;
2820         }
2821
2822         return 0;
2823 }
2824
2825 int ath10k_pci_wait_for_target_init(struct ath10k *ar)
2826 {
2827         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2828         unsigned long timeout;
2829         u32 val;
2830
2831         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot waiting target to initialise\n");
2832
2833         timeout = jiffies + msecs_to_jiffies(ATH10K_PCI_TARGET_WAIT);
2834
2835         do {
2836                 val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
2837
2838                 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target indicator %x\n",
2839                            val);
2840
2841                 /* target should never return this */
2842                 if (val == 0xffffffff)
2843                         continue;
2844
2845                 /* the device has crashed so don't bother trying anymore */
2846                 if (val & FW_IND_EVENT_PENDING)
2847                         break;
2848
2849                 if (val & FW_IND_INITIALIZED)
2850                         break;
2851
2852                 if (ar_pci->num_msi_intrs == 0)
2853                         /* Fix potential race by repeating CORE_BASE writes */
2854                         ath10k_pci_enable_legacy_irq(ar);
2855
2856                 mdelay(10);
2857         } while (time_before(jiffies, timeout));
2858
2859         ath10k_pci_disable_and_clear_legacy_irq(ar);
2860         ath10k_pci_irq_msi_fw_mask(ar);
2861
2862         if (val == 0xffffffff) {
2863                 ath10k_err(ar, "failed to read device register, device is gone\n");
2864                 return -EIO;
2865         }
2866
2867         if (val & FW_IND_EVENT_PENDING) {
2868                 ath10k_warn(ar, "device has crashed during init\n");
2869                 return -ECOMM;
2870         }
2871
2872         if (!(val & FW_IND_INITIALIZED)) {
2873                 ath10k_err(ar, "failed to receive initialized event from target: %08x\n",
2874                            val);
2875                 return -ETIMEDOUT;
2876         }
2877
2878         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target initialised\n");
2879         return 0;
2880 }
2881
2882 static int ath10k_pci_cold_reset(struct ath10k *ar)
2883 {
2884         u32 val;
2885
2886         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cold reset\n");
2887
2888         spin_lock_bh(&ar->data_lock);
2889
2890         ar->stats.fw_cold_reset_counter++;
2891
2892         spin_unlock_bh(&ar->data_lock);
2893
2894         /* Put Target, including PCIe, into RESET. */
2895         val = ath10k_pci_reg_read32(ar, SOC_GLOBAL_RESET_ADDRESS);
2896         val |= 1;
2897         ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
2898
2899         /* After writing into SOC_GLOBAL_RESET to put device into
2900          * reset and pulling out of reset pcie may not be stable
2901          * for any immediate pcie register access and cause bus error,
2902          * add delay before any pcie access request to fix this issue.
2903          */
2904         msleep(20);
2905
2906         /* Pull Target, including PCIe, out of RESET. */
2907         val &= ~1;
2908         ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
2909
2910         msleep(20);
2911
2912         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cold reset complete\n");
2913
2914         return 0;
2915 }
2916
2917 static int ath10k_pci_claim(struct ath10k *ar)
2918 {
2919         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2920         struct pci_dev *pdev = ar_pci->pdev;
2921         int ret;
2922
2923         pci_set_drvdata(pdev, ar);
2924
2925         ret = pci_enable_device(pdev);
2926         if (ret) {
2927                 ath10k_err(ar, "failed to enable pci device: %d\n", ret);
2928                 return ret;
2929         }
2930
2931         ret = pci_request_region(pdev, BAR_NUM, "ath");
2932         if (ret) {
2933                 ath10k_err(ar, "failed to request region BAR%d: %d\n", BAR_NUM,
2934                            ret);
2935                 goto err_device;
2936         }
2937
2938         /* Target expects 32 bit DMA. Enforce it. */
2939         ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2940         if (ret) {
2941                 ath10k_err(ar, "failed to set dma mask to 32-bit: %d\n", ret);
2942                 goto err_region;
2943         }
2944
2945         ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2946         if (ret) {
2947                 ath10k_err(ar, "failed to set consistent dma mask to 32-bit: %d\n",
2948                            ret);
2949                 goto err_region;
2950         }
2951
2952         pci_set_master(pdev);
2953
2954         /* Arrange for access to Target SoC registers. */
2955         ar_pci->mem_len = pci_resource_len(pdev, BAR_NUM);
2956         ar_pci->mem = pci_iomap(pdev, BAR_NUM, 0);
2957         if (!ar_pci->mem) {
2958                 ath10k_err(ar, "failed to iomap BAR%d\n", BAR_NUM);
2959                 ret = -EIO;
2960                 goto err_master;
2961         }
2962
2963         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot pci_mem 0x%p\n", ar_pci->mem);
2964         return 0;
2965
2966 err_master:
2967         pci_clear_master(pdev);
2968
2969 err_region:
2970         pci_release_region(pdev, BAR_NUM);
2971
2972 err_device:
2973         pci_disable_device(pdev);
2974
2975         return ret;
2976 }
2977
2978 static void ath10k_pci_release(struct ath10k *ar)
2979 {
2980         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2981         struct pci_dev *pdev = ar_pci->pdev;
2982
2983         pci_iounmap(pdev, ar_pci->mem);
2984         pci_release_region(pdev, BAR_NUM);
2985         pci_clear_master(pdev);
2986         pci_disable_device(pdev);
2987 }
2988
2989 static bool ath10k_pci_chip_is_supported(u32 dev_id, u32 chip_id)
2990 {
2991         const struct ath10k_pci_supp_chip *supp_chip;
2992         int i;
2993         u32 rev_id = MS(chip_id, SOC_CHIP_ID_REV);
2994
2995         for (i = 0; i < ARRAY_SIZE(ath10k_pci_supp_chips); i++) {
2996                 supp_chip = &ath10k_pci_supp_chips[i];
2997
2998                 if (supp_chip->dev_id == dev_id &&
2999                     supp_chip->rev_id == rev_id)
3000                         return true;
3001         }
3002
3003         return false;
3004 }
3005
3006 int ath10k_pci_setup_resource(struct ath10k *ar)
3007 {
3008         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
3009         int ret;
3010
3011         spin_lock_init(&ar_pci->ce_lock);
3012         spin_lock_init(&ar_pci->ps_lock);
3013
3014         setup_timer(&ar_pci->rx_post_retry, ath10k_pci_rx_replenish_retry,
3015                     (unsigned long)ar);
3016
3017         if (QCA_REV_6174(ar))
3018                 ath10k_pci_override_ce_config(ar);
3019
3020         ret = ath10k_pci_alloc_pipes(ar);
3021         if (ret) {
3022                 ath10k_err(ar, "failed to allocate copy engine pipes: %d\n",
3023                            ret);
3024                 return ret;
3025         }
3026
3027         return 0;
3028 }
3029
3030 void ath10k_pci_release_resource(struct ath10k *ar)
3031 {
3032         ath10k_pci_kill_tasklet(ar);
3033         ath10k_pci_ce_deinit(ar);
3034         ath10k_pci_free_pipes(ar);
3035 }
3036
3037 static const struct ath10k_bus_ops ath10k_pci_bus_ops = {
3038         .read32         = ath10k_bus_pci_read32,
3039         .write32        = ath10k_bus_pci_write32,
3040         .get_num_banks  = ath10k_pci_get_num_banks,
3041 };
3042
3043 static int ath10k_pci_probe(struct pci_dev *pdev,
3044                             const struct pci_device_id *pci_dev)
3045 {
3046         int ret = 0;
3047         struct ath10k *ar;
3048         struct ath10k_pci *ar_pci;
3049         enum ath10k_hw_rev hw_rev;
3050         u32 chip_id;
3051         bool pci_ps;
3052
3053         switch (pci_dev->device) {
3054         case QCA988X_2_0_DEVICE_ID:
3055                 hw_rev = ATH10K_HW_QCA988X;
3056                 pci_ps = false;
3057                 break;
3058         case QCA6164_2_1_DEVICE_ID:
3059         case QCA6174_2_1_DEVICE_ID:
3060                 hw_rev = ATH10K_HW_QCA6174;
3061                 pci_ps = true;
3062                 break;
3063         case QCA99X0_2_0_DEVICE_ID:
3064                 hw_rev = ATH10K_HW_QCA99X0;
3065                 pci_ps = false;
3066                 break;
3067         case QCA9377_1_0_DEVICE_ID:
3068                 hw_rev = ATH10K_HW_QCA9377;
3069                 pci_ps = true;
3070                 break;
3071         default:
3072                 WARN_ON(1);
3073                 return -ENOTSUPP;
3074         }
3075
3076         ar = ath10k_core_create(sizeof(*ar_pci), &pdev->dev, ATH10K_BUS_PCI,
3077                                 hw_rev, &ath10k_pci_hif_ops);
3078         if (!ar) {
3079                 dev_err(&pdev->dev, "failed to allocate core\n");
3080                 return -ENOMEM;
3081         }
3082
3083         ath10k_dbg(ar, ATH10K_DBG_BOOT, "pci probe %04x:%04x %04x:%04x\n",
3084                    pdev->vendor, pdev->device,
3085                    pdev->subsystem_vendor, pdev->subsystem_device);
3086
3087         ar_pci = ath10k_pci_priv(ar);
3088         ar_pci->pdev = pdev;
3089         ar_pci->dev = &pdev->dev;
3090         ar_pci->ar = ar;
3091         ar->dev_id = pci_dev->device;
3092         ar_pci->pci_ps = pci_ps;
3093         ar_pci->bus_ops = &ath10k_pci_bus_ops;
3094
3095         ar->id.vendor = pdev->vendor;
3096         ar->id.device = pdev->device;
3097         ar->id.subsystem_vendor = pdev->subsystem_vendor;
3098         ar->id.subsystem_device = pdev->subsystem_device;
3099
3100         setup_timer(&ar_pci->ps_timer, ath10k_pci_ps_timer,
3101                     (unsigned long)ar);
3102
3103         ret = ath10k_pci_setup_resource(ar);
3104         if (ret) {
3105                 ath10k_err(ar, "failed to setup resource: %d\n", ret);
3106                 goto err_core_destroy;
3107         }
3108
3109         ret = ath10k_pci_claim(ar);
3110         if (ret) {
3111                 ath10k_err(ar, "failed to claim device: %d\n", ret);
3112                 goto err_free_pipes;
3113         }
3114
3115         ret = ath10k_pci_force_wake(ar);
3116         if (ret) {
3117                 ath10k_warn(ar, "failed to wake up device : %d\n", ret);
3118                 goto err_sleep;
3119         }
3120
3121         ath10k_pci_ce_deinit(ar);
3122         ath10k_pci_irq_disable(ar);
3123
3124         ret = ath10k_pci_init_irq(ar);
3125         if (ret) {
3126                 ath10k_err(ar, "failed to init irqs: %d\n", ret);
3127                 goto err_sleep;
3128         }
3129
3130         ath10k_info(ar, "pci irq %s interrupts %d irq_mode %d reset_mode %d\n",
3131                     ath10k_pci_get_irq_method(ar), ar_pci->num_msi_intrs,
3132                     ath10k_pci_irq_mode, ath10k_pci_reset_mode);
3133
3134         ret = ath10k_pci_request_irq(ar);
3135         if (ret) {
3136                 ath10k_warn(ar, "failed to request irqs: %d\n", ret);
3137                 goto err_deinit_irq;
3138         }
3139
3140         ret = ath10k_pci_chip_reset(ar);
3141         if (ret) {
3142                 ath10k_err(ar, "failed to reset chip: %d\n", ret);
3143                 goto err_free_irq;
3144         }
3145
3146         chip_id = ath10k_pci_soc_read32(ar, SOC_CHIP_ID_ADDRESS);
3147         if (chip_id == 0xffffffff) {
3148                 ath10k_err(ar, "failed to get chip id\n");
3149                 goto err_free_irq;
3150         }
3151
3152         if (!ath10k_pci_chip_is_supported(pdev->device, chip_id)) {
3153                 ath10k_err(ar, "device %04x with chip_id %08x isn't supported\n",
3154                            pdev->device, chip_id);
3155                 goto err_free_irq;
3156         }
3157
3158         ret = ath10k_core_register(ar, chip_id);
3159         if (ret) {
3160                 ath10k_err(ar, "failed to register driver core: %d\n", ret);
3161                 goto err_free_irq;
3162         }
3163
3164         return 0;
3165
3166 err_free_irq:
3167         ath10k_pci_free_irq(ar);
3168         ath10k_pci_kill_tasklet(ar);
3169
3170 err_deinit_irq:
3171         ath10k_pci_deinit_irq(ar);
3172
3173 err_sleep:
3174         ath10k_pci_sleep_sync(ar);
3175         ath10k_pci_release(ar);
3176
3177 err_free_pipes:
3178         ath10k_pci_free_pipes(ar);
3179
3180 err_core_destroy:
3181         ath10k_core_destroy(ar);
3182
3183         return ret;
3184 }
3185
3186 static void ath10k_pci_remove(struct pci_dev *pdev)
3187 {
3188         struct ath10k *ar = pci_get_drvdata(pdev);
3189         struct ath10k_pci *ar_pci;
3190
3191         ath10k_dbg(ar, ATH10K_DBG_PCI, "pci remove\n");
3192
3193         if (!ar)
3194                 return;
3195
3196         ar_pci = ath10k_pci_priv(ar);
3197
3198         if (!ar_pci)
3199                 return;
3200
3201         ath10k_core_unregister(ar);
3202         ath10k_pci_free_irq(ar);
3203         ath10k_pci_deinit_irq(ar);
3204         ath10k_pci_release_resource(ar);
3205         ath10k_pci_sleep_sync(ar);
3206         ath10k_pci_release(ar);
3207         ath10k_core_destroy(ar);
3208 }
3209
3210 MODULE_DEVICE_TABLE(pci, ath10k_pci_id_table);
3211
3212 static struct pci_driver ath10k_pci_driver = {
3213         .name = "ath10k_pci",
3214         .id_table = ath10k_pci_id_table,
3215         .probe = ath10k_pci_probe,
3216         .remove = ath10k_pci_remove,
3217 };
3218
3219 static int __init ath10k_pci_init(void)
3220 {
3221         int ret;
3222
3223         ret = pci_register_driver(&ath10k_pci_driver);
3224         if (ret)
3225                 printk(KERN_ERR "failed to register ath10k pci driver: %d\n",
3226                        ret);
3227
3228         ret = ath10k_ahb_init();
3229         if (ret)
3230                 printk(KERN_ERR "ahb init failed: %d\n", ret);
3231
3232         return ret;
3233 }
3234 module_init(ath10k_pci_init);
3235
3236 static void __exit ath10k_pci_exit(void)
3237 {
3238         pci_unregister_driver(&ath10k_pci_driver);
3239         ath10k_ahb_exit();
3240 }
3241
3242 module_exit(ath10k_pci_exit);
3243
3244 MODULE_AUTHOR("Qualcomm Atheros");
3245 MODULE_DESCRIPTION("Driver support for Atheros QCA988X PCIe devices");
3246 MODULE_LICENSE("Dual BSD/GPL");
3247
3248 /* QCA988x 2.0 firmware files */
3249 MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_FW_FILE);
3250 MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API2_FILE);
3251 MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API3_FILE);
3252 MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API4_FILE);
3253 MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API5_FILE);
3254 MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_BOARD_DATA_FILE);
3255 MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_BOARD_API2_FILE);
3256
3257 /* QCA6174 2.1 firmware files */
3258 MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" ATH10K_FW_API4_FILE);
3259 MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" ATH10K_FW_API5_FILE);
3260 MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" QCA6174_HW_2_1_BOARD_DATA_FILE);
3261 MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" ATH10K_BOARD_API2_FILE);
3262
3263 /* QCA6174 3.1 firmware files */
3264 MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" ATH10K_FW_API4_FILE);
3265 MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" ATH10K_FW_API5_FILE);
3266 MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" QCA6174_HW_3_0_BOARD_DATA_FILE);
3267 MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" ATH10K_BOARD_API2_FILE);
3268
3269 /* QCA9377 1.0 firmware files */
3270 MODULE_FIRMWARE(QCA9377_HW_1_0_FW_DIR "/" ATH10K_FW_API5_FILE);
3271 MODULE_FIRMWARE(QCA9377_HW_1_0_FW_DIR "/" QCA9377_HW_1_0_BOARD_DATA_FILE);