f06dd3941bac5ea9eb36e42642b144fd23d0f354
[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_reset_mode {
37         ATH10K_PCI_RESET_AUTO = 0,
38         ATH10K_PCI_RESET_WARM_ONLY = 1,
39 };
40
41 static unsigned int ath10k_pci_irq_mode = ATH10K_PCI_IRQ_AUTO;
42 static unsigned int ath10k_pci_reset_mode = ATH10K_PCI_RESET_AUTO;
43
44 module_param_named(irq_mode, ath10k_pci_irq_mode, uint, 0644);
45 MODULE_PARM_DESC(irq_mode, "0: auto, 1: legacy, 2: msi (default: 0)");
46
47 module_param_named(reset_mode, ath10k_pci_reset_mode, uint, 0644);
48 MODULE_PARM_DESC(reset_mode, "0: auto, 1: warm only (default: 0)");
49
50 /* how long wait to wait for target to initialise, in ms */
51 #define ATH10K_PCI_TARGET_WAIT 3000
52 #define ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS 3
53
54 static const struct pci_device_id ath10k_pci_id_table[] = {
55         { PCI_VDEVICE(ATHEROS, QCA988X_2_0_DEVICE_ID) }, /* PCI-E QCA988X V2 */
56         { PCI_VDEVICE(ATHEROS, QCA6164_2_1_DEVICE_ID) }, /* PCI-E QCA6164 V2.1 */
57         { PCI_VDEVICE(ATHEROS, QCA6174_2_1_DEVICE_ID) }, /* PCI-E QCA6174 V2.1 */
58         { PCI_VDEVICE(ATHEROS, QCA99X0_2_0_DEVICE_ID) }, /* PCI-E QCA99X0 V2 */
59         { PCI_VDEVICE(ATHEROS, QCA9984_1_0_DEVICE_ID) }, /* PCI-E QCA9984 V1 */
60         { PCI_VDEVICE(ATHEROS, QCA9377_1_0_DEVICE_ID) }, /* PCI-E QCA9377 V1 */
61         { PCI_VDEVICE(ATHEROS, QCA9887_1_0_DEVICE_ID) }, /* PCI-E QCA9887 */
62         {0}
63 };
64
65 static const struct ath10k_pci_supp_chip ath10k_pci_supp_chips[] = {
66         /* QCA988X pre 2.0 chips are not supported because they need some nasty
67          * hacks. ath10k doesn't have them and these devices crash horribly
68          * because of that.
69          */
70         { QCA988X_2_0_DEVICE_ID, QCA988X_HW_2_0_CHIP_ID_REV },
71
72         { QCA6164_2_1_DEVICE_ID, QCA6174_HW_2_1_CHIP_ID_REV },
73         { QCA6164_2_1_DEVICE_ID, QCA6174_HW_2_2_CHIP_ID_REV },
74         { QCA6164_2_1_DEVICE_ID, QCA6174_HW_3_0_CHIP_ID_REV },
75         { QCA6164_2_1_DEVICE_ID, QCA6174_HW_3_1_CHIP_ID_REV },
76         { QCA6164_2_1_DEVICE_ID, QCA6174_HW_3_2_CHIP_ID_REV },
77
78         { QCA6174_2_1_DEVICE_ID, QCA6174_HW_2_1_CHIP_ID_REV },
79         { QCA6174_2_1_DEVICE_ID, QCA6174_HW_2_2_CHIP_ID_REV },
80         { QCA6174_2_1_DEVICE_ID, QCA6174_HW_3_0_CHIP_ID_REV },
81         { QCA6174_2_1_DEVICE_ID, QCA6174_HW_3_1_CHIP_ID_REV },
82         { QCA6174_2_1_DEVICE_ID, QCA6174_HW_3_2_CHIP_ID_REV },
83
84         { QCA99X0_2_0_DEVICE_ID, QCA99X0_HW_2_0_CHIP_ID_REV },
85
86         { QCA9984_1_0_DEVICE_ID, QCA9984_HW_1_0_CHIP_ID_REV },
87
88         { QCA9377_1_0_DEVICE_ID, QCA9377_HW_1_0_CHIP_ID_REV },
89         { QCA9377_1_0_DEVICE_ID, QCA9377_HW_1_1_CHIP_ID_REV },
90
91         { QCA9887_1_0_DEVICE_ID, QCA9887_HW_1_0_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->oper_irq_mode == ATH10K_PCI_IRQ_MSI)
749                 return "msi";
750
751         return "legacy";
752 }
753
754 static int __ath10k_pci_rx_post_buf(struct ath10k_pci_pipe *pipe)
755 {
756         struct ath10k *ar = pipe->hif_ce_state;
757         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
758         struct ath10k_ce_pipe *ce_pipe = pipe->ce_hdl;
759         struct sk_buff *skb;
760         dma_addr_t paddr;
761         int ret;
762
763         skb = dev_alloc_skb(pipe->buf_sz);
764         if (!skb)
765                 return -ENOMEM;
766
767         WARN_ONCE((unsigned long)skb->data & 3, "unaligned skb");
768
769         paddr = dma_map_single(ar->dev, skb->data,
770                                skb->len + skb_tailroom(skb),
771                                DMA_FROM_DEVICE);
772         if (unlikely(dma_mapping_error(ar->dev, paddr))) {
773                 ath10k_warn(ar, "failed to dma map pci rx buf\n");
774                 dev_kfree_skb_any(skb);
775                 return -EIO;
776         }
777
778         ATH10K_SKB_RXCB(skb)->paddr = paddr;
779
780         spin_lock_bh(&ar_pci->ce_lock);
781         ret = __ath10k_ce_rx_post_buf(ce_pipe, skb, paddr);
782         spin_unlock_bh(&ar_pci->ce_lock);
783         if (ret) {
784                 dma_unmap_single(ar->dev, paddr, skb->len + skb_tailroom(skb),
785                                  DMA_FROM_DEVICE);
786                 dev_kfree_skb_any(skb);
787                 return ret;
788         }
789
790         return 0;
791 }
792
793 static void ath10k_pci_rx_post_pipe(struct ath10k_pci_pipe *pipe)
794 {
795         struct ath10k *ar = pipe->hif_ce_state;
796         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
797         struct ath10k_ce_pipe *ce_pipe = pipe->ce_hdl;
798         int ret, num;
799
800         if (pipe->buf_sz == 0)
801                 return;
802
803         if (!ce_pipe->dest_ring)
804                 return;
805
806         spin_lock_bh(&ar_pci->ce_lock);
807         num = __ath10k_ce_rx_num_free_bufs(ce_pipe);
808         spin_unlock_bh(&ar_pci->ce_lock);
809
810         while (num >= 0) {
811                 ret = __ath10k_pci_rx_post_buf(pipe);
812                 if (ret) {
813                         if (ret == -ENOSPC)
814                                 break;
815                         ath10k_warn(ar, "failed to post pci rx buf: %d\n", ret);
816                         mod_timer(&ar_pci->rx_post_retry, jiffies +
817                                   ATH10K_PCI_RX_POST_RETRY_MS);
818                         break;
819                 }
820                 num--;
821         }
822 }
823
824 void ath10k_pci_rx_post(struct ath10k *ar)
825 {
826         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
827         int i;
828
829         for (i = 0; i < CE_COUNT; i++)
830                 ath10k_pci_rx_post_pipe(&ar_pci->pipe_info[i]);
831 }
832
833 void ath10k_pci_rx_replenish_retry(unsigned long ptr)
834 {
835         struct ath10k *ar = (void *)ptr;
836
837         ath10k_pci_rx_post(ar);
838 }
839
840 static u32 ath10k_pci_targ_cpu_to_ce_addr(struct ath10k *ar, u32 addr)
841 {
842         u32 val = 0;
843
844         switch (ar->hw_rev) {
845         case ATH10K_HW_QCA988X:
846         case ATH10K_HW_QCA9887:
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_QCA9984:
855         case ATH10K_HW_QCA4019:
856                 val = ath10k_pci_read32(ar, PCIE_BAR_REG_ADDRESS);
857                 break;
858         }
859
860         val |= 0x100000 | (addr & 0xfffff);
861         return val;
862 }
863
864 /*
865  * Diagnostic read/write access is provided for startup/config/debug usage.
866  * Caller must guarantee proper alignment, when applicable, and single user
867  * at any moment.
868  */
869 static int ath10k_pci_diag_read_mem(struct ath10k *ar, u32 address, void *data,
870                                     int nbytes)
871 {
872         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
873         int ret = 0;
874         u32 *buf;
875         unsigned int completed_nbytes, alloc_nbytes, remaining_bytes;
876         struct ath10k_ce_pipe *ce_diag;
877         /* Host buffer address in CE space */
878         u32 ce_data;
879         dma_addr_t ce_data_base = 0;
880         void *data_buf = NULL;
881         int i;
882
883         spin_lock_bh(&ar_pci->ce_lock);
884
885         ce_diag = ar_pci->ce_diag;
886
887         /*
888          * Allocate a temporary bounce buffer to hold caller's data
889          * to be DMA'ed from Target. This guarantees
890          *   1) 4-byte alignment
891          *   2) Buffer in DMA-able space
892          */
893         alloc_nbytes = min_t(unsigned int, nbytes, DIAG_TRANSFER_LIMIT);
894
895         data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
896                                                        alloc_nbytes,
897                                                        &ce_data_base,
898                                                        GFP_ATOMIC);
899
900         if (!data_buf) {
901                 ret = -ENOMEM;
902                 goto done;
903         }
904         memset(data_buf, 0, alloc_nbytes);
905
906         remaining_bytes = nbytes;
907         ce_data = ce_data_base;
908         while (remaining_bytes) {
909                 nbytes = min_t(unsigned int, remaining_bytes,
910                                DIAG_TRANSFER_LIMIT);
911
912                 ret = __ath10k_ce_rx_post_buf(ce_diag, &ce_data, ce_data);
913                 if (ret != 0)
914                         goto done;
915
916                 /* Request CE to send from Target(!) address to Host buffer */
917                 /*
918                  * The address supplied by the caller is in the
919                  * Target CPU virtual address space.
920                  *
921                  * In order to use this address with the diagnostic CE,
922                  * convert it from Target CPU virtual address space
923                  * to CE address space
924                  */
925                 address = ath10k_pci_targ_cpu_to_ce_addr(ar, address);
926
927                 ret = ath10k_ce_send_nolock(ce_diag, NULL, (u32)address, nbytes, 0,
928                                             0);
929                 if (ret)
930                         goto done;
931
932                 i = 0;
933                 while (ath10k_ce_completed_send_next_nolock(ce_diag,
934                                                             NULL) != 0) {
935                         mdelay(1);
936                         if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
937                                 ret = -EBUSY;
938                                 goto done;
939                         }
940                 }
941
942                 i = 0;
943                 while (ath10k_ce_completed_recv_next_nolock(ce_diag,
944                                                             (void **)&buf,
945                                                             &completed_nbytes)
946                                                                 != 0) {
947                         mdelay(1);
948
949                         if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
950                                 ret = -EBUSY;
951                                 goto done;
952                         }
953                 }
954
955                 if (nbytes != completed_nbytes) {
956                         ret = -EIO;
957                         goto done;
958                 }
959
960                 if (*buf != ce_data) {
961                         ret = -EIO;
962                         goto done;
963                 }
964
965                 remaining_bytes -= nbytes;
966
967                 if (ret) {
968                         ath10k_warn(ar, "failed to read diag value at 0x%x: %d\n",
969                                     address, ret);
970                         break;
971                 }
972                 memcpy(data, data_buf, nbytes);
973
974                 address += nbytes;
975                 data += nbytes;
976         }
977
978 done:
979
980         if (data_buf)
981                 dma_free_coherent(ar->dev, alloc_nbytes, data_buf,
982                                   ce_data_base);
983
984         spin_unlock_bh(&ar_pci->ce_lock);
985
986         return ret;
987 }
988
989 static int ath10k_pci_diag_read32(struct ath10k *ar, u32 address, u32 *value)
990 {
991         __le32 val = 0;
992         int ret;
993
994         ret = ath10k_pci_diag_read_mem(ar, address, &val, sizeof(val));
995         *value = __le32_to_cpu(val);
996
997         return ret;
998 }
999
1000 static int __ath10k_pci_diag_read_hi(struct ath10k *ar, void *dest,
1001                                      u32 src, u32 len)
1002 {
1003         u32 host_addr, addr;
1004         int ret;
1005
1006         host_addr = host_interest_item_address(src);
1007
1008         ret = ath10k_pci_diag_read32(ar, host_addr, &addr);
1009         if (ret != 0) {
1010                 ath10k_warn(ar, "failed to get memcpy hi address for firmware address %d: %d\n",
1011                             src, ret);
1012                 return ret;
1013         }
1014
1015         ret = ath10k_pci_diag_read_mem(ar, addr, dest, len);
1016         if (ret != 0) {
1017                 ath10k_warn(ar, "failed to memcpy firmware memory from %d (%d B): %d\n",
1018                             addr, len, ret);
1019                 return ret;
1020         }
1021
1022         return 0;
1023 }
1024
1025 #define ath10k_pci_diag_read_hi(ar, dest, src, len)             \
1026         __ath10k_pci_diag_read_hi(ar, dest, HI_ITEM(src), len)
1027
1028 int ath10k_pci_diag_write_mem(struct ath10k *ar, u32 address,
1029                               const void *data, int nbytes)
1030 {
1031         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1032         int ret = 0;
1033         u32 *buf;
1034         unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
1035         struct ath10k_ce_pipe *ce_diag;
1036         void *data_buf = NULL;
1037         u32 ce_data;    /* Host buffer address in CE space */
1038         dma_addr_t ce_data_base = 0;
1039         int i;
1040
1041         spin_lock_bh(&ar_pci->ce_lock);
1042
1043         ce_diag = ar_pci->ce_diag;
1044
1045         /*
1046          * Allocate a temporary bounce buffer to hold caller's data
1047          * to be DMA'ed to Target. This guarantees
1048          *   1) 4-byte alignment
1049          *   2) Buffer in DMA-able space
1050          */
1051         orig_nbytes = nbytes;
1052         data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
1053                                                        orig_nbytes,
1054                                                        &ce_data_base,
1055                                                        GFP_ATOMIC);
1056         if (!data_buf) {
1057                 ret = -ENOMEM;
1058                 goto done;
1059         }
1060
1061         /* Copy caller's data to allocated DMA buf */
1062         memcpy(data_buf, data, orig_nbytes);
1063
1064         /*
1065          * The address supplied by the caller is in the
1066          * Target CPU virtual address space.
1067          *
1068          * In order to use this address with the diagnostic CE,
1069          * convert it from
1070          *    Target CPU virtual address space
1071          * to
1072          *    CE address space
1073          */
1074         address = ath10k_pci_targ_cpu_to_ce_addr(ar, address);
1075
1076         remaining_bytes = orig_nbytes;
1077         ce_data = ce_data_base;
1078         while (remaining_bytes) {
1079                 /* FIXME: check cast */
1080                 nbytes = min_t(int, remaining_bytes, DIAG_TRANSFER_LIMIT);
1081
1082                 /* Set up to receive directly into Target(!) address */
1083                 ret = __ath10k_ce_rx_post_buf(ce_diag, &address, address);
1084                 if (ret != 0)
1085                         goto done;
1086
1087                 /*
1088                  * Request CE to send caller-supplied data that
1089                  * was copied to bounce buffer to Target(!) address.
1090                  */
1091                 ret = ath10k_ce_send_nolock(ce_diag, NULL, (u32)ce_data,
1092                                             nbytes, 0, 0);
1093                 if (ret != 0)
1094                         goto done;
1095
1096                 i = 0;
1097                 while (ath10k_ce_completed_send_next_nolock(ce_diag,
1098                                                             NULL) != 0) {
1099                         mdelay(1);
1100
1101                         if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
1102                                 ret = -EBUSY;
1103                                 goto done;
1104                         }
1105                 }
1106
1107                 i = 0;
1108                 while (ath10k_ce_completed_recv_next_nolock(ce_diag,
1109                                                             (void **)&buf,
1110                                                             &completed_nbytes)
1111                                                                 != 0) {
1112                         mdelay(1);
1113
1114                         if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
1115                                 ret = -EBUSY;
1116                                 goto done;
1117                         }
1118                 }
1119
1120                 if (nbytes != completed_nbytes) {
1121                         ret = -EIO;
1122                         goto done;
1123                 }
1124
1125                 if (*buf != address) {
1126                         ret = -EIO;
1127                         goto done;
1128                 }
1129
1130                 remaining_bytes -= nbytes;
1131                 address += nbytes;
1132                 ce_data += nbytes;
1133         }
1134
1135 done:
1136         if (data_buf) {
1137                 dma_free_coherent(ar->dev, orig_nbytes, data_buf,
1138                                   ce_data_base);
1139         }
1140
1141         if (ret != 0)
1142                 ath10k_warn(ar, "failed to write diag value at 0x%x: %d\n",
1143                             address, ret);
1144
1145         spin_unlock_bh(&ar_pci->ce_lock);
1146
1147         return ret;
1148 }
1149
1150 static int ath10k_pci_diag_write32(struct ath10k *ar, u32 address, u32 value)
1151 {
1152         __le32 val = __cpu_to_le32(value);
1153
1154         return ath10k_pci_diag_write_mem(ar, address, &val, sizeof(val));
1155 }
1156
1157 /* Called by lower (CE) layer when a send to Target completes. */
1158 static void ath10k_pci_htc_tx_cb(struct ath10k_ce_pipe *ce_state)
1159 {
1160         struct ath10k *ar = ce_state->ar;
1161         struct sk_buff_head list;
1162         struct sk_buff *skb;
1163
1164         __skb_queue_head_init(&list);
1165         while (ath10k_ce_completed_send_next(ce_state, (void **)&skb) == 0) {
1166                 /* no need to call tx completion for NULL pointers */
1167                 if (skb == NULL)
1168                         continue;
1169
1170                 __skb_queue_tail(&list, skb);
1171         }
1172
1173         while ((skb = __skb_dequeue(&list)))
1174                 ath10k_htc_tx_completion_handler(ar, skb);
1175 }
1176
1177 static void ath10k_pci_process_rx_cb(struct ath10k_ce_pipe *ce_state,
1178                                      void (*callback)(struct ath10k *ar,
1179                                                       struct sk_buff *skb))
1180 {
1181         struct ath10k *ar = ce_state->ar;
1182         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1183         struct ath10k_pci_pipe *pipe_info =  &ar_pci->pipe_info[ce_state->id];
1184         struct sk_buff *skb;
1185         struct sk_buff_head list;
1186         void *transfer_context;
1187         unsigned int nbytes, max_nbytes;
1188
1189         __skb_queue_head_init(&list);
1190         while (ath10k_ce_completed_recv_next(ce_state, &transfer_context,
1191                                              &nbytes) == 0) {
1192                 skb = transfer_context;
1193                 max_nbytes = skb->len + skb_tailroom(skb);
1194                 dma_unmap_single(ar->dev, ATH10K_SKB_RXCB(skb)->paddr,
1195                                  max_nbytes, DMA_FROM_DEVICE);
1196
1197                 if (unlikely(max_nbytes < nbytes)) {
1198                         ath10k_warn(ar, "rxed more than expected (nbytes %d, max %d)",
1199                                     nbytes, max_nbytes);
1200                         dev_kfree_skb_any(skb);
1201                         continue;
1202                 }
1203
1204                 skb_put(skb, nbytes);
1205                 __skb_queue_tail(&list, skb);
1206         }
1207
1208         while ((skb = __skb_dequeue(&list))) {
1209                 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci rx ce pipe %d len %d\n",
1210                            ce_state->id, skb->len);
1211                 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci rx: ",
1212                                 skb->data, skb->len);
1213
1214                 callback(ar, skb);
1215         }
1216
1217         ath10k_pci_rx_post_pipe(pipe_info);
1218 }
1219
1220 static void ath10k_pci_process_htt_rx_cb(struct ath10k_ce_pipe *ce_state,
1221                                          void (*callback)(struct ath10k *ar,
1222                                                           struct sk_buff *skb))
1223 {
1224         struct ath10k *ar = ce_state->ar;
1225         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1226         struct ath10k_pci_pipe *pipe_info =  &ar_pci->pipe_info[ce_state->id];
1227         struct ath10k_ce_pipe *ce_pipe = pipe_info->ce_hdl;
1228         struct sk_buff *skb;
1229         struct sk_buff_head list;
1230         void *transfer_context;
1231         unsigned int nbytes, max_nbytes, nentries;
1232         int orig_len;
1233
1234         /* No need to aquire ce_lock for CE5, since this is the only place CE5
1235          * is processed other than init and deinit. Before releasing CE5
1236          * buffers, interrupts are disabled. Thus CE5 access is serialized.
1237          */
1238         __skb_queue_head_init(&list);
1239         while (ath10k_ce_completed_recv_next_nolock(ce_state, &transfer_context,
1240                                                     &nbytes) == 0) {
1241                 skb = transfer_context;
1242                 max_nbytes = skb->len + skb_tailroom(skb);
1243
1244                 if (unlikely(max_nbytes < nbytes)) {
1245                         ath10k_warn(ar, "rxed more than expected (nbytes %d, max %d)",
1246                                     nbytes, max_nbytes);
1247                         continue;
1248                 }
1249
1250                 dma_sync_single_for_cpu(ar->dev, ATH10K_SKB_RXCB(skb)->paddr,
1251                                         max_nbytes, DMA_FROM_DEVICE);
1252                 skb_put(skb, nbytes);
1253                 __skb_queue_tail(&list, skb);
1254         }
1255
1256         nentries = skb_queue_len(&list);
1257         while ((skb = __skb_dequeue(&list))) {
1258                 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci rx ce pipe %d len %d\n",
1259                            ce_state->id, skb->len);
1260                 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci rx: ",
1261                                 skb->data, skb->len);
1262
1263                 orig_len = skb->len;
1264                 callback(ar, skb);
1265                 skb_push(skb, orig_len - skb->len);
1266                 skb_reset_tail_pointer(skb);
1267                 skb_trim(skb, 0);
1268
1269                 /*let device gain the buffer again*/
1270                 dma_sync_single_for_device(ar->dev, ATH10K_SKB_RXCB(skb)->paddr,
1271                                            skb->len + skb_tailroom(skb),
1272                                            DMA_FROM_DEVICE);
1273         }
1274         ath10k_ce_rx_update_write_idx(ce_pipe, nentries);
1275 }
1276
1277 /* Called by lower (CE) layer when data is received from the Target. */
1278 static void ath10k_pci_htc_rx_cb(struct ath10k_ce_pipe *ce_state)
1279 {
1280         ath10k_pci_process_rx_cb(ce_state, ath10k_htc_rx_completion_handler);
1281 }
1282
1283 static void ath10k_pci_htt_htc_rx_cb(struct ath10k_ce_pipe *ce_state)
1284 {
1285         /* CE4 polling needs to be done whenever CE pipe which transports
1286          * HTT Rx (target->host) is processed.
1287          */
1288         ath10k_ce_per_engine_service(ce_state->ar, 4);
1289
1290         ath10k_pci_process_rx_cb(ce_state, ath10k_htc_rx_completion_handler);
1291 }
1292
1293 /* Called by lower (CE) layer when data is received from the Target.
1294  * Only 10.4 firmware uses separate CE to transfer pktlog data.
1295  */
1296 static void ath10k_pci_pktlog_rx_cb(struct ath10k_ce_pipe *ce_state)
1297 {
1298         ath10k_pci_process_rx_cb(ce_state,
1299                                  ath10k_htt_rx_pktlog_completion_handler);
1300 }
1301
1302 /* Called by lower (CE) layer when a send to HTT Target completes. */
1303 static void ath10k_pci_htt_tx_cb(struct ath10k_ce_pipe *ce_state)
1304 {
1305         struct ath10k *ar = ce_state->ar;
1306         struct sk_buff *skb;
1307
1308         while (ath10k_ce_completed_send_next(ce_state, (void **)&skb) == 0) {
1309                 /* no need to call tx completion for NULL pointers */
1310                 if (!skb)
1311                         continue;
1312
1313                 dma_unmap_single(ar->dev, ATH10K_SKB_CB(skb)->paddr,
1314                                  skb->len, DMA_TO_DEVICE);
1315                 ath10k_htt_hif_tx_complete(ar, skb);
1316         }
1317 }
1318
1319 static void ath10k_pci_htt_rx_deliver(struct ath10k *ar, struct sk_buff *skb)
1320 {
1321         skb_pull(skb, sizeof(struct ath10k_htc_hdr));
1322         ath10k_htt_t2h_msg_handler(ar, skb);
1323 }
1324
1325 /* Called by lower (CE) layer when HTT data is received from the Target. */
1326 static void ath10k_pci_htt_rx_cb(struct ath10k_ce_pipe *ce_state)
1327 {
1328         /* CE4 polling needs to be done whenever CE pipe which transports
1329          * HTT Rx (target->host) is processed.
1330          */
1331         ath10k_ce_per_engine_service(ce_state->ar, 4);
1332
1333         ath10k_pci_process_htt_rx_cb(ce_state, ath10k_pci_htt_rx_deliver);
1334 }
1335
1336 int ath10k_pci_hif_tx_sg(struct ath10k *ar, u8 pipe_id,
1337                          struct ath10k_hif_sg_item *items, int n_items)
1338 {
1339         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1340         struct ath10k_pci_pipe *pci_pipe = &ar_pci->pipe_info[pipe_id];
1341         struct ath10k_ce_pipe *ce_pipe = pci_pipe->ce_hdl;
1342         struct ath10k_ce_ring *src_ring = ce_pipe->src_ring;
1343         unsigned int nentries_mask;
1344         unsigned int sw_index;
1345         unsigned int write_index;
1346         int err, i = 0;
1347
1348         spin_lock_bh(&ar_pci->ce_lock);
1349
1350         nentries_mask = src_ring->nentries_mask;
1351         sw_index = src_ring->sw_index;
1352         write_index = src_ring->write_index;
1353
1354         if (unlikely(CE_RING_DELTA(nentries_mask,
1355                                    write_index, sw_index - 1) < n_items)) {
1356                 err = -ENOBUFS;
1357                 goto err;
1358         }
1359
1360         for (i = 0; i < n_items - 1; i++) {
1361                 ath10k_dbg(ar, ATH10K_DBG_PCI,
1362                            "pci tx item %d paddr 0x%08x len %d n_items %d\n",
1363                            i, items[i].paddr, items[i].len, n_items);
1364                 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci tx data: ",
1365                                 items[i].vaddr, items[i].len);
1366
1367                 err = ath10k_ce_send_nolock(ce_pipe,
1368                                             items[i].transfer_context,
1369                                             items[i].paddr,
1370                                             items[i].len,
1371                                             items[i].transfer_id,
1372                                             CE_SEND_FLAG_GATHER);
1373                 if (err)
1374                         goto err;
1375         }
1376
1377         /* `i` is equal to `n_items -1` after for() */
1378
1379         ath10k_dbg(ar, ATH10K_DBG_PCI,
1380                    "pci tx item %d paddr 0x%08x len %d n_items %d\n",
1381                    i, items[i].paddr, items[i].len, n_items);
1382         ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci tx data: ",
1383                         items[i].vaddr, items[i].len);
1384
1385         err = ath10k_ce_send_nolock(ce_pipe,
1386                                     items[i].transfer_context,
1387                                     items[i].paddr,
1388                                     items[i].len,
1389                                     items[i].transfer_id,
1390                                     0);
1391         if (err)
1392                 goto err;
1393
1394         spin_unlock_bh(&ar_pci->ce_lock);
1395         return 0;
1396
1397 err:
1398         for (; i > 0; i--)
1399                 __ath10k_ce_send_revert(ce_pipe);
1400
1401         spin_unlock_bh(&ar_pci->ce_lock);
1402         return err;
1403 }
1404
1405 int ath10k_pci_hif_diag_read(struct ath10k *ar, u32 address, void *buf,
1406                              size_t buf_len)
1407 {
1408         return ath10k_pci_diag_read_mem(ar, address, buf, buf_len);
1409 }
1410
1411 u16 ath10k_pci_hif_get_free_queue_number(struct ath10k *ar, u8 pipe)
1412 {
1413         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1414
1415         ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif get free queue number\n");
1416
1417         return ath10k_ce_num_free_src_entries(ar_pci->pipe_info[pipe].ce_hdl);
1418 }
1419
1420 static void ath10k_pci_dump_registers(struct ath10k *ar,
1421                                       struct ath10k_fw_crash_data *crash_data)
1422 {
1423         __le32 reg_dump_values[REG_DUMP_COUNT_QCA988X] = {};
1424         int i, ret;
1425
1426         lockdep_assert_held(&ar->data_lock);
1427
1428         ret = ath10k_pci_diag_read_hi(ar, &reg_dump_values[0],
1429                                       hi_failure_state,
1430                                       REG_DUMP_COUNT_QCA988X * sizeof(__le32));
1431         if (ret) {
1432                 ath10k_err(ar, "failed to read firmware dump area: %d\n", ret);
1433                 return;
1434         }
1435
1436         BUILD_BUG_ON(REG_DUMP_COUNT_QCA988X % 4);
1437
1438         ath10k_err(ar, "firmware register dump:\n");
1439         for (i = 0; i < REG_DUMP_COUNT_QCA988X; i += 4)
1440                 ath10k_err(ar, "[%02d]: 0x%08X 0x%08X 0x%08X 0x%08X\n",
1441                            i,
1442                            __le32_to_cpu(reg_dump_values[i]),
1443                            __le32_to_cpu(reg_dump_values[i + 1]),
1444                            __le32_to_cpu(reg_dump_values[i + 2]),
1445                            __le32_to_cpu(reg_dump_values[i + 3]));
1446
1447         if (!crash_data)
1448                 return;
1449
1450         for (i = 0; i < REG_DUMP_COUNT_QCA988X; i++)
1451                 crash_data->registers[i] = reg_dump_values[i];
1452 }
1453
1454 static void ath10k_pci_fw_crashed_dump(struct ath10k *ar)
1455 {
1456         struct ath10k_fw_crash_data *crash_data;
1457         char uuid[50];
1458
1459         spin_lock_bh(&ar->data_lock);
1460
1461         ar->stats.fw_crash_counter++;
1462
1463         crash_data = ath10k_debug_get_new_fw_crash_data(ar);
1464
1465         if (crash_data)
1466                 scnprintf(uuid, sizeof(uuid), "%pUl", &crash_data->uuid);
1467         else
1468                 scnprintf(uuid, sizeof(uuid), "n/a");
1469
1470         ath10k_err(ar, "firmware crashed! (uuid %s)\n", uuid);
1471         ath10k_print_driver_info(ar);
1472         ath10k_pci_dump_registers(ar, crash_data);
1473
1474         spin_unlock_bh(&ar->data_lock);
1475
1476         queue_work(ar->workqueue, &ar->restart_work);
1477 }
1478
1479 void ath10k_pci_hif_send_complete_check(struct ath10k *ar, u8 pipe,
1480                                         int force)
1481 {
1482         ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif send complete check\n");
1483
1484         if (!force) {
1485                 int resources;
1486                 /*
1487                  * Decide whether to actually poll for completions, or just
1488                  * wait for a later chance.
1489                  * If there seem to be plenty of resources left, then just wait
1490                  * since checking involves reading a CE register, which is a
1491                  * relatively expensive operation.
1492                  */
1493                 resources = ath10k_pci_hif_get_free_queue_number(ar, pipe);
1494
1495                 /*
1496                  * If at least 50% of the total resources are still available,
1497                  * don't bother checking again yet.
1498                  */
1499                 if (resources > (host_ce_config_wlan[pipe].src_nentries >> 1))
1500                         return;
1501         }
1502         ath10k_ce_per_engine_service(ar, pipe);
1503 }
1504
1505 void ath10k_pci_kill_tasklet(struct ath10k *ar)
1506 {
1507         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1508
1509         tasklet_kill(&ar_pci->intr_tq);
1510
1511         del_timer_sync(&ar_pci->rx_post_retry);
1512 }
1513
1514 int ath10k_pci_hif_map_service_to_pipe(struct ath10k *ar, u16 service_id,
1515                                        u8 *ul_pipe, u8 *dl_pipe)
1516 {
1517         const struct service_to_pipe *entry;
1518         bool ul_set = false, dl_set = false;
1519         int i;
1520
1521         ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif map service\n");
1522
1523         for (i = 0; i < ARRAY_SIZE(target_service_to_ce_map_wlan); i++) {
1524                 entry = &target_service_to_ce_map_wlan[i];
1525
1526                 if (__le32_to_cpu(entry->service_id) != service_id)
1527                         continue;
1528
1529                 switch (__le32_to_cpu(entry->pipedir)) {
1530                 case PIPEDIR_NONE:
1531                         break;
1532                 case PIPEDIR_IN:
1533                         WARN_ON(dl_set);
1534                         *dl_pipe = __le32_to_cpu(entry->pipenum);
1535                         dl_set = true;
1536                         break;
1537                 case PIPEDIR_OUT:
1538                         WARN_ON(ul_set);
1539                         *ul_pipe = __le32_to_cpu(entry->pipenum);
1540                         ul_set = true;
1541                         break;
1542                 case PIPEDIR_INOUT:
1543                         WARN_ON(dl_set);
1544                         WARN_ON(ul_set);
1545                         *dl_pipe = __le32_to_cpu(entry->pipenum);
1546                         *ul_pipe = __le32_to_cpu(entry->pipenum);
1547                         dl_set = true;
1548                         ul_set = true;
1549                         break;
1550                 }
1551         }
1552
1553         if (WARN_ON(!ul_set || !dl_set))
1554                 return -ENOENT;
1555
1556         return 0;
1557 }
1558
1559 void ath10k_pci_hif_get_default_pipe(struct ath10k *ar,
1560                                      u8 *ul_pipe, u8 *dl_pipe)
1561 {
1562         ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif get default pipe\n");
1563
1564         (void)ath10k_pci_hif_map_service_to_pipe(ar,
1565                                                  ATH10K_HTC_SVC_ID_RSVD_CTRL,
1566                                                  ul_pipe, dl_pipe);
1567 }
1568
1569 static void ath10k_pci_irq_msi_fw_mask(struct ath10k *ar)
1570 {
1571         u32 val;
1572
1573         switch (ar->hw_rev) {
1574         case ATH10K_HW_QCA988X:
1575         case ATH10K_HW_QCA9887:
1576         case ATH10K_HW_QCA6174:
1577         case ATH10K_HW_QCA9377:
1578                 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1579                                         CORE_CTRL_ADDRESS);
1580                 val &= ~CORE_CTRL_PCIE_REG_31_MASK;
1581                 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
1582                                    CORE_CTRL_ADDRESS, val);
1583                 break;
1584         case ATH10K_HW_QCA99X0:
1585         case ATH10K_HW_QCA9984:
1586         case ATH10K_HW_QCA4019:
1587                 /* TODO: Find appropriate register configuration for QCA99X0
1588                  *  to mask irq/MSI.
1589                  */
1590                  break;
1591         }
1592 }
1593
1594 static void ath10k_pci_irq_msi_fw_unmask(struct ath10k *ar)
1595 {
1596         u32 val;
1597
1598         switch (ar->hw_rev) {
1599         case ATH10K_HW_QCA988X:
1600         case ATH10K_HW_QCA9887:
1601         case ATH10K_HW_QCA6174:
1602         case ATH10K_HW_QCA9377:
1603                 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1604                                         CORE_CTRL_ADDRESS);
1605                 val |= CORE_CTRL_PCIE_REG_31_MASK;
1606                 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
1607                                    CORE_CTRL_ADDRESS, val);
1608                 break;
1609         case ATH10K_HW_QCA99X0:
1610         case ATH10K_HW_QCA9984:
1611         case ATH10K_HW_QCA4019:
1612                 /* TODO: Find appropriate register configuration for QCA99X0
1613                  *  to unmask irq/MSI.
1614                  */
1615                 break;
1616         }
1617 }
1618
1619 static void ath10k_pci_irq_disable(struct ath10k *ar)
1620 {
1621         ath10k_ce_disable_interrupts(ar);
1622         ath10k_pci_disable_and_clear_legacy_irq(ar);
1623         ath10k_pci_irq_msi_fw_mask(ar);
1624 }
1625
1626 static void ath10k_pci_irq_sync(struct ath10k *ar)
1627 {
1628         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1629
1630         synchronize_irq(ar_pci->pdev->irq);
1631 }
1632
1633 static void ath10k_pci_irq_enable(struct ath10k *ar)
1634 {
1635         ath10k_ce_enable_interrupts(ar);
1636         ath10k_pci_enable_legacy_irq(ar);
1637         ath10k_pci_irq_msi_fw_unmask(ar);
1638 }
1639
1640 static int ath10k_pci_hif_start(struct ath10k *ar)
1641 {
1642         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1643
1644         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif start\n");
1645
1646         ath10k_pci_irq_enable(ar);
1647         ath10k_pci_rx_post(ar);
1648
1649         pcie_capability_write_word(ar_pci->pdev, PCI_EXP_LNKCTL,
1650                                    ar_pci->link_ctl);
1651
1652         return 0;
1653 }
1654
1655 static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe *pci_pipe)
1656 {
1657         struct ath10k *ar;
1658         struct ath10k_ce_pipe *ce_pipe;
1659         struct ath10k_ce_ring *ce_ring;
1660         struct sk_buff *skb;
1661         int i;
1662
1663         ar = pci_pipe->hif_ce_state;
1664         ce_pipe = pci_pipe->ce_hdl;
1665         ce_ring = ce_pipe->dest_ring;
1666
1667         if (!ce_ring)
1668                 return;
1669
1670         if (!pci_pipe->buf_sz)
1671                 return;
1672
1673         for (i = 0; i < ce_ring->nentries; i++) {
1674                 skb = ce_ring->per_transfer_context[i];
1675                 if (!skb)
1676                         continue;
1677
1678                 ce_ring->per_transfer_context[i] = NULL;
1679
1680                 dma_unmap_single(ar->dev, ATH10K_SKB_RXCB(skb)->paddr,
1681                                  skb->len + skb_tailroom(skb),
1682                                  DMA_FROM_DEVICE);
1683                 dev_kfree_skb_any(skb);
1684         }
1685 }
1686
1687 static void ath10k_pci_tx_pipe_cleanup(struct ath10k_pci_pipe *pci_pipe)
1688 {
1689         struct ath10k *ar;
1690         struct ath10k_pci *ar_pci;
1691         struct ath10k_ce_pipe *ce_pipe;
1692         struct ath10k_ce_ring *ce_ring;
1693         struct sk_buff *skb;
1694         int i;
1695
1696         ar = pci_pipe->hif_ce_state;
1697         ar_pci = ath10k_pci_priv(ar);
1698         ce_pipe = pci_pipe->ce_hdl;
1699         ce_ring = ce_pipe->src_ring;
1700
1701         if (!ce_ring)
1702                 return;
1703
1704         if (!pci_pipe->buf_sz)
1705                 return;
1706
1707         for (i = 0; i < ce_ring->nentries; i++) {
1708                 skb = ce_ring->per_transfer_context[i];
1709                 if (!skb)
1710                         continue;
1711
1712                 ce_ring->per_transfer_context[i] = NULL;
1713
1714                 ath10k_htc_tx_completion_handler(ar, skb);
1715         }
1716 }
1717
1718 /*
1719  * Cleanup residual buffers for device shutdown:
1720  *    buffers that were enqueued for receive
1721  *    buffers that were to be sent
1722  * Note: Buffers that had completed but which were
1723  * not yet processed are on a completion queue. They
1724  * are handled when the completion thread shuts down.
1725  */
1726 static void ath10k_pci_buffer_cleanup(struct ath10k *ar)
1727 {
1728         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1729         int pipe_num;
1730
1731         for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
1732                 struct ath10k_pci_pipe *pipe_info;
1733
1734                 pipe_info = &ar_pci->pipe_info[pipe_num];
1735                 ath10k_pci_rx_pipe_cleanup(pipe_info);
1736                 ath10k_pci_tx_pipe_cleanup(pipe_info);
1737         }
1738 }
1739
1740 void ath10k_pci_ce_deinit(struct ath10k *ar)
1741 {
1742         int i;
1743
1744         for (i = 0; i < CE_COUNT; i++)
1745                 ath10k_ce_deinit_pipe(ar, i);
1746 }
1747
1748 void ath10k_pci_flush(struct ath10k *ar)
1749 {
1750         ath10k_pci_kill_tasklet(ar);
1751         ath10k_pci_buffer_cleanup(ar);
1752 }
1753
1754 static void ath10k_pci_hif_stop(struct ath10k *ar)
1755 {
1756         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1757         unsigned long flags;
1758
1759         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif stop\n");
1760
1761         /* Most likely the device has HTT Rx ring configured. The only way to
1762          * prevent the device from accessing (and possible corrupting) host
1763          * memory is to reset the chip now.
1764          *
1765          * There's also no known way of masking MSI interrupts on the device.
1766          * For ranged MSI the CE-related interrupts can be masked. However
1767          * regardless how many MSI interrupts are assigned the first one
1768          * is always used for firmware indications (crashes) and cannot be
1769          * masked. To prevent the device from asserting the interrupt reset it
1770          * before proceeding with cleanup.
1771          */
1772         ath10k_pci_safe_chip_reset(ar);
1773
1774         ath10k_pci_irq_disable(ar);
1775         ath10k_pci_irq_sync(ar);
1776         ath10k_pci_flush(ar);
1777
1778         spin_lock_irqsave(&ar_pci->ps_lock, flags);
1779         WARN_ON(ar_pci->ps_wake_refcount > 0);
1780         spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
1781 }
1782
1783 int ath10k_pci_hif_exchange_bmi_msg(struct ath10k *ar,
1784                                     void *req, u32 req_len,
1785                                     void *resp, u32 *resp_len)
1786 {
1787         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1788         struct ath10k_pci_pipe *pci_tx = &ar_pci->pipe_info[BMI_CE_NUM_TO_TARG];
1789         struct ath10k_pci_pipe *pci_rx = &ar_pci->pipe_info[BMI_CE_NUM_TO_HOST];
1790         struct ath10k_ce_pipe *ce_tx = pci_tx->ce_hdl;
1791         struct ath10k_ce_pipe *ce_rx = pci_rx->ce_hdl;
1792         dma_addr_t req_paddr = 0;
1793         dma_addr_t resp_paddr = 0;
1794         struct bmi_xfer xfer = {};
1795         void *treq, *tresp = NULL;
1796         int ret = 0;
1797
1798         might_sleep();
1799
1800         if (resp && !resp_len)
1801                 return -EINVAL;
1802
1803         if (resp && resp_len && *resp_len == 0)
1804                 return -EINVAL;
1805
1806         treq = kmemdup(req, req_len, GFP_KERNEL);
1807         if (!treq)
1808                 return -ENOMEM;
1809
1810         req_paddr = dma_map_single(ar->dev, treq, req_len, DMA_TO_DEVICE);
1811         ret = dma_mapping_error(ar->dev, req_paddr);
1812         if (ret) {
1813                 ret = -EIO;
1814                 goto err_dma;
1815         }
1816
1817         if (resp && resp_len) {
1818                 tresp = kzalloc(*resp_len, GFP_KERNEL);
1819                 if (!tresp) {
1820                         ret = -ENOMEM;
1821                         goto err_req;
1822                 }
1823
1824                 resp_paddr = dma_map_single(ar->dev, tresp, *resp_len,
1825                                             DMA_FROM_DEVICE);
1826                 ret = dma_mapping_error(ar->dev, resp_paddr);
1827                 if (ret) {
1828                         ret = -EIO;
1829                         goto err_req;
1830                 }
1831
1832                 xfer.wait_for_resp = true;
1833                 xfer.resp_len = 0;
1834
1835                 ath10k_ce_rx_post_buf(ce_rx, &xfer, resp_paddr);
1836         }
1837
1838         ret = ath10k_ce_send(ce_tx, &xfer, req_paddr, req_len, -1, 0);
1839         if (ret)
1840                 goto err_resp;
1841
1842         ret = ath10k_pci_bmi_wait(ce_tx, ce_rx, &xfer);
1843         if (ret) {
1844                 u32 unused_buffer;
1845                 unsigned int unused_nbytes;
1846                 unsigned int unused_id;
1847
1848                 ath10k_ce_cancel_send_next(ce_tx, NULL, &unused_buffer,
1849                                            &unused_nbytes, &unused_id);
1850         } else {
1851                 /* non-zero means we did not time out */
1852                 ret = 0;
1853         }
1854
1855 err_resp:
1856         if (resp) {
1857                 u32 unused_buffer;
1858
1859                 ath10k_ce_revoke_recv_next(ce_rx, NULL, &unused_buffer);
1860                 dma_unmap_single(ar->dev, resp_paddr,
1861                                  *resp_len, DMA_FROM_DEVICE);
1862         }
1863 err_req:
1864         dma_unmap_single(ar->dev, req_paddr, req_len, DMA_TO_DEVICE);
1865
1866         if (ret == 0 && resp_len) {
1867                 *resp_len = min(*resp_len, xfer.resp_len);
1868                 memcpy(resp, tresp, xfer.resp_len);
1869         }
1870 err_dma:
1871         kfree(treq);
1872         kfree(tresp);
1873
1874         return ret;
1875 }
1876
1877 static void ath10k_pci_bmi_send_done(struct ath10k_ce_pipe *ce_state)
1878 {
1879         struct bmi_xfer *xfer;
1880
1881         if (ath10k_ce_completed_send_next(ce_state, (void **)&xfer))
1882                 return;
1883
1884         xfer->tx_done = true;
1885 }
1886
1887 static void ath10k_pci_bmi_recv_data(struct ath10k_ce_pipe *ce_state)
1888 {
1889         struct ath10k *ar = ce_state->ar;
1890         struct bmi_xfer *xfer;
1891         unsigned int nbytes;
1892
1893         if (ath10k_ce_completed_recv_next(ce_state, (void **)&xfer,
1894                                           &nbytes))
1895                 return;
1896
1897         if (WARN_ON_ONCE(!xfer))
1898                 return;
1899
1900         if (!xfer->wait_for_resp) {
1901                 ath10k_warn(ar, "unexpected: BMI data received; ignoring\n");
1902                 return;
1903         }
1904
1905         xfer->resp_len = nbytes;
1906         xfer->rx_done = true;
1907 }
1908
1909 static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
1910                                struct ath10k_ce_pipe *rx_pipe,
1911                                struct bmi_xfer *xfer)
1912 {
1913         unsigned long timeout = jiffies + BMI_COMMUNICATION_TIMEOUT_HZ;
1914
1915         while (time_before_eq(jiffies, timeout)) {
1916                 ath10k_pci_bmi_send_done(tx_pipe);
1917                 ath10k_pci_bmi_recv_data(rx_pipe);
1918
1919                 if (xfer->tx_done && (xfer->rx_done == xfer->wait_for_resp))
1920                         return 0;
1921
1922                 schedule();
1923         }
1924
1925         return -ETIMEDOUT;
1926 }
1927
1928 /*
1929  * Send an interrupt to the device to wake up the Target CPU
1930  * so it has an opportunity to notice any changed state.
1931  */
1932 static int ath10k_pci_wake_target_cpu(struct ath10k *ar)
1933 {
1934         u32 addr, val;
1935
1936         addr = SOC_CORE_BASE_ADDRESS | CORE_CTRL_ADDRESS;
1937         val = ath10k_pci_read32(ar, addr);
1938         val |= CORE_CTRL_CPU_INTR_MASK;
1939         ath10k_pci_write32(ar, addr, val);
1940
1941         return 0;
1942 }
1943
1944 static int ath10k_pci_get_num_banks(struct ath10k *ar)
1945 {
1946         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1947
1948         switch (ar_pci->pdev->device) {
1949         case QCA988X_2_0_DEVICE_ID:
1950         case QCA99X0_2_0_DEVICE_ID:
1951         case QCA9984_1_0_DEVICE_ID:
1952         case QCA9887_1_0_DEVICE_ID:
1953                 return 1;
1954         case QCA6164_2_1_DEVICE_ID:
1955         case QCA6174_2_1_DEVICE_ID:
1956                 switch (MS(ar->chip_id, SOC_CHIP_ID_REV)) {
1957                 case QCA6174_HW_1_0_CHIP_ID_REV:
1958                 case QCA6174_HW_1_1_CHIP_ID_REV:
1959                 case QCA6174_HW_2_1_CHIP_ID_REV:
1960                 case QCA6174_HW_2_2_CHIP_ID_REV:
1961                         return 3;
1962                 case QCA6174_HW_1_3_CHIP_ID_REV:
1963                         return 2;
1964                 case QCA6174_HW_3_0_CHIP_ID_REV:
1965                 case QCA6174_HW_3_1_CHIP_ID_REV:
1966                 case QCA6174_HW_3_2_CHIP_ID_REV:
1967                         return 9;
1968                 }
1969                 break;
1970         case QCA9377_1_0_DEVICE_ID:
1971                 return 2;
1972         }
1973
1974         ath10k_warn(ar, "unknown number of banks, assuming 1\n");
1975         return 1;
1976 }
1977
1978 static int ath10k_bus_get_num_banks(struct ath10k *ar)
1979 {
1980         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1981
1982         return ar_pci->bus_ops->get_num_banks(ar);
1983 }
1984
1985 int ath10k_pci_init_config(struct ath10k *ar)
1986 {
1987         u32 interconnect_targ_addr;
1988         u32 pcie_state_targ_addr = 0;
1989         u32 pipe_cfg_targ_addr = 0;
1990         u32 svc_to_pipe_map = 0;
1991         u32 pcie_config_flags = 0;
1992         u32 ealloc_value;
1993         u32 ealloc_targ_addr;
1994         u32 flag2_value;
1995         u32 flag2_targ_addr;
1996         int ret = 0;
1997
1998         /* Download to Target the CE Config and the service-to-CE map */
1999         interconnect_targ_addr =
2000                 host_interest_item_address(HI_ITEM(hi_interconnect_state));
2001
2002         /* Supply Target-side CE configuration */
2003         ret = ath10k_pci_diag_read32(ar, interconnect_targ_addr,
2004                                      &pcie_state_targ_addr);
2005         if (ret != 0) {
2006                 ath10k_err(ar, "Failed to get pcie state addr: %d\n", ret);
2007                 return ret;
2008         }
2009
2010         if (pcie_state_targ_addr == 0) {
2011                 ret = -EIO;
2012                 ath10k_err(ar, "Invalid pcie state addr\n");
2013                 return ret;
2014         }
2015
2016         ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
2017                                           offsetof(struct pcie_state,
2018                                                    pipe_cfg_addr)),
2019                                      &pipe_cfg_targ_addr);
2020         if (ret != 0) {
2021                 ath10k_err(ar, "Failed to get pipe cfg addr: %d\n", ret);
2022                 return ret;
2023         }
2024
2025         if (pipe_cfg_targ_addr == 0) {
2026                 ret = -EIO;
2027                 ath10k_err(ar, "Invalid pipe cfg addr\n");
2028                 return ret;
2029         }
2030
2031         ret = ath10k_pci_diag_write_mem(ar, pipe_cfg_targ_addr,
2032                                         target_ce_config_wlan,
2033                                         sizeof(struct ce_pipe_config) *
2034                                         NUM_TARGET_CE_CONFIG_WLAN);
2035
2036         if (ret != 0) {
2037                 ath10k_err(ar, "Failed to write pipe cfg: %d\n", ret);
2038                 return ret;
2039         }
2040
2041         ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
2042                                           offsetof(struct pcie_state,
2043                                                    svc_to_pipe_map)),
2044                                      &svc_to_pipe_map);
2045         if (ret != 0) {
2046                 ath10k_err(ar, "Failed to get svc/pipe map: %d\n", ret);
2047                 return ret;
2048         }
2049
2050         if (svc_to_pipe_map == 0) {
2051                 ret = -EIO;
2052                 ath10k_err(ar, "Invalid svc_to_pipe map\n");
2053                 return ret;
2054         }
2055
2056         ret = ath10k_pci_diag_write_mem(ar, svc_to_pipe_map,
2057                                         target_service_to_ce_map_wlan,
2058                                         sizeof(target_service_to_ce_map_wlan));
2059         if (ret != 0) {
2060                 ath10k_err(ar, "Failed to write svc/pipe map: %d\n", ret);
2061                 return ret;
2062         }
2063
2064         ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
2065                                           offsetof(struct pcie_state,
2066                                                    config_flags)),
2067                                      &pcie_config_flags);
2068         if (ret != 0) {
2069                 ath10k_err(ar, "Failed to get pcie config_flags: %d\n", ret);
2070                 return ret;
2071         }
2072
2073         pcie_config_flags &= ~PCIE_CONFIG_FLAG_ENABLE_L1;
2074
2075         ret = ath10k_pci_diag_write32(ar, (pcie_state_targ_addr +
2076                                            offsetof(struct pcie_state,
2077                                                     config_flags)),
2078                                       pcie_config_flags);
2079         if (ret != 0) {
2080                 ath10k_err(ar, "Failed to write pcie config_flags: %d\n", ret);
2081                 return ret;
2082         }
2083
2084         /* configure early allocation */
2085         ealloc_targ_addr = host_interest_item_address(HI_ITEM(hi_early_alloc));
2086
2087         ret = ath10k_pci_diag_read32(ar, ealloc_targ_addr, &ealloc_value);
2088         if (ret != 0) {
2089                 ath10k_err(ar, "Faile to get early alloc val: %d\n", ret);
2090                 return ret;
2091         }
2092
2093         /* first bank is switched to IRAM */
2094         ealloc_value |= ((HI_EARLY_ALLOC_MAGIC << HI_EARLY_ALLOC_MAGIC_SHIFT) &
2095                          HI_EARLY_ALLOC_MAGIC_MASK);
2096         ealloc_value |= ((ath10k_bus_get_num_banks(ar) <<
2097                           HI_EARLY_ALLOC_IRAM_BANKS_SHIFT) &
2098                          HI_EARLY_ALLOC_IRAM_BANKS_MASK);
2099
2100         ret = ath10k_pci_diag_write32(ar, ealloc_targ_addr, ealloc_value);
2101         if (ret != 0) {
2102                 ath10k_err(ar, "Failed to set early alloc val: %d\n", ret);
2103                 return ret;
2104         }
2105
2106         /* Tell Target to proceed with initialization */
2107         flag2_targ_addr = host_interest_item_address(HI_ITEM(hi_option_flag2));
2108
2109         ret = ath10k_pci_diag_read32(ar, flag2_targ_addr, &flag2_value);
2110         if (ret != 0) {
2111                 ath10k_err(ar, "Failed to get option val: %d\n", ret);
2112                 return ret;
2113         }
2114
2115         flag2_value |= HI_OPTION_EARLY_CFG_DONE;
2116
2117         ret = ath10k_pci_diag_write32(ar, flag2_targ_addr, flag2_value);
2118         if (ret != 0) {
2119                 ath10k_err(ar, "Failed to set option val: %d\n", ret);
2120                 return ret;
2121         }
2122
2123         return 0;
2124 }
2125
2126 static void ath10k_pci_override_ce_config(struct ath10k *ar)
2127 {
2128         struct ce_attr *attr;
2129         struct ce_pipe_config *config;
2130
2131         /* For QCA6174 we're overriding the Copy Engine 5 configuration,
2132          * since it is currently used for other feature.
2133          */
2134
2135         /* Override Host's Copy Engine 5 configuration */
2136         attr = &host_ce_config_wlan[5];
2137         attr->src_sz_max = 0;
2138         attr->dest_nentries = 0;
2139
2140         /* Override Target firmware's Copy Engine configuration */
2141         config = &target_ce_config_wlan[5];
2142         config->pipedir = __cpu_to_le32(PIPEDIR_OUT);
2143         config->nbytes_max = __cpu_to_le32(2048);
2144
2145         /* Map from service/endpoint to Copy Engine */
2146         target_service_to_ce_map_wlan[15].pipenum = __cpu_to_le32(1);
2147 }
2148
2149 int ath10k_pci_alloc_pipes(struct ath10k *ar)
2150 {
2151         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2152         struct ath10k_pci_pipe *pipe;
2153         int i, ret;
2154
2155         for (i = 0; i < CE_COUNT; i++) {
2156                 pipe = &ar_pci->pipe_info[i];
2157                 pipe->ce_hdl = &ar_pci->ce_states[i];
2158                 pipe->pipe_num = i;
2159                 pipe->hif_ce_state = ar;
2160
2161                 ret = ath10k_ce_alloc_pipe(ar, i, &host_ce_config_wlan[i]);
2162                 if (ret) {
2163                         ath10k_err(ar, "failed to allocate copy engine pipe %d: %d\n",
2164                                    i, ret);
2165                         return ret;
2166                 }
2167
2168                 /* Last CE is Diagnostic Window */
2169                 if (i == CE_DIAG_PIPE) {
2170                         ar_pci->ce_diag = pipe->ce_hdl;
2171                         continue;
2172                 }
2173
2174                 pipe->buf_sz = (size_t)(host_ce_config_wlan[i].src_sz_max);
2175         }
2176
2177         return 0;
2178 }
2179
2180 void ath10k_pci_free_pipes(struct ath10k *ar)
2181 {
2182         int i;
2183
2184         for (i = 0; i < CE_COUNT; i++)
2185                 ath10k_ce_free_pipe(ar, i);
2186 }
2187
2188 int ath10k_pci_init_pipes(struct ath10k *ar)
2189 {
2190         int i, ret;
2191
2192         for (i = 0; i < CE_COUNT; i++) {
2193                 ret = ath10k_ce_init_pipe(ar, i, &host_ce_config_wlan[i]);
2194                 if (ret) {
2195                         ath10k_err(ar, "failed to initialize copy engine pipe %d: %d\n",
2196                                    i, ret);
2197                         return ret;
2198                 }
2199         }
2200
2201         return 0;
2202 }
2203
2204 static bool ath10k_pci_has_fw_crashed(struct ath10k *ar)
2205 {
2206         return ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS) &
2207                FW_IND_EVENT_PENDING;
2208 }
2209
2210 static void ath10k_pci_fw_crashed_clear(struct ath10k *ar)
2211 {
2212         u32 val;
2213
2214         val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
2215         val &= ~FW_IND_EVENT_PENDING;
2216         ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, val);
2217 }
2218
2219 /* this function effectively clears target memory controller assert line */
2220 static void ath10k_pci_warm_reset_si0(struct ath10k *ar)
2221 {
2222         u32 val;
2223
2224         val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
2225         ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
2226                                val | SOC_RESET_CONTROL_SI0_RST_MASK);
2227         val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
2228
2229         msleep(10);
2230
2231         val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
2232         ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
2233                                val & ~SOC_RESET_CONTROL_SI0_RST_MASK);
2234         val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
2235
2236         msleep(10);
2237 }
2238
2239 static void ath10k_pci_warm_reset_cpu(struct ath10k *ar)
2240 {
2241         u32 val;
2242
2243         ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, 0);
2244
2245         val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
2246                                 SOC_RESET_CONTROL_ADDRESS);
2247         ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
2248                            val | SOC_RESET_CONTROL_CPU_WARM_RST_MASK);
2249 }
2250
2251 static void ath10k_pci_warm_reset_ce(struct ath10k *ar)
2252 {
2253         u32 val;
2254
2255         val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
2256                                 SOC_RESET_CONTROL_ADDRESS);
2257
2258         ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
2259                            val | SOC_RESET_CONTROL_CE_RST_MASK);
2260         msleep(10);
2261         ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
2262                            val & ~SOC_RESET_CONTROL_CE_RST_MASK);
2263 }
2264
2265 static void ath10k_pci_warm_reset_clear_lf(struct ath10k *ar)
2266 {
2267         u32 val;
2268
2269         val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
2270                                 SOC_LF_TIMER_CONTROL0_ADDRESS);
2271         ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS +
2272                            SOC_LF_TIMER_CONTROL0_ADDRESS,
2273                            val & ~SOC_LF_TIMER_CONTROL0_ENABLE_MASK);
2274 }
2275
2276 static int ath10k_pci_warm_reset(struct ath10k *ar)
2277 {
2278         int ret;
2279
2280         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot warm reset\n");
2281
2282         spin_lock_bh(&ar->data_lock);
2283         ar->stats.fw_warm_reset_counter++;
2284         spin_unlock_bh(&ar->data_lock);
2285
2286         ath10k_pci_irq_disable(ar);
2287
2288         /* Make sure the target CPU is not doing anything dangerous, e.g. if it
2289          * were to access copy engine while host performs copy engine reset
2290          * then it is possible for the device to confuse pci-e controller to
2291          * the point of bringing host system to a complete stop (i.e. hang).
2292          */
2293         ath10k_pci_warm_reset_si0(ar);
2294         ath10k_pci_warm_reset_cpu(ar);
2295         ath10k_pci_init_pipes(ar);
2296         ath10k_pci_wait_for_target_init(ar);
2297
2298         ath10k_pci_warm_reset_clear_lf(ar);
2299         ath10k_pci_warm_reset_ce(ar);
2300         ath10k_pci_warm_reset_cpu(ar);
2301         ath10k_pci_init_pipes(ar);
2302
2303         ret = ath10k_pci_wait_for_target_init(ar);
2304         if (ret) {
2305                 ath10k_warn(ar, "failed to wait for target init: %d\n", ret);
2306                 return ret;
2307         }
2308
2309         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot warm reset complete\n");
2310
2311         return 0;
2312 }
2313
2314 static int ath10k_pci_qca99x0_soft_chip_reset(struct ath10k *ar)
2315 {
2316         ath10k_pci_irq_disable(ar);
2317         return ath10k_pci_qca99x0_chip_reset(ar);
2318 }
2319
2320 static int ath10k_pci_safe_chip_reset(struct ath10k *ar)
2321 {
2322         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2323
2324         if (!ar_pci->pci_soft_reset)
2325                 return -ENOTSUPP;
2326
2327         return ar_pci->pci_soft_reset(ar);
2328 }
2329
2330 static int ath10k_pci_qca988x_chip_reset(struct ath10k *ar)
2331 {
2332         int i, ret;
2333         u32 val;
2334
2335         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot 988x chip reset\n");
2336
2337         /* Some hardware revisions (e.g. CUS223v2) has issues with cold reset.
2338          * It is thus preferred to use warm reset which is safer but may not be
2339          * able to recover the device from all possible fail scenarios.
2340          *
2341          * Warm reset doesn't always work on first try so attempt it a few
2342          * times before giving up.
2343          */
2344         for (i = 0; i < ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS; i++) {
2345                 ret = ath10k_pci_warm_reset(ar);
2346                 if (ret) {
2347                         ath10k_warn(ar, "failed to warm reset attempt %d of %d: %d\n",
2348                                     i + 1, ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS,
2349                                     ret);
2350                         continue;
2351                 }
2352
2353                 /* FIXME: Sometimes copy engine doesn't recover after warm
2354                  * reset. In most cases this needs cold reset. In some of these
2355                  * cases the device is in such a state that a cold reset may
2356                  * lock up the host.
2357                  *
2358                  * Reading any host interest register via copy engine is
2359                  * sufficient to verify if device is capable of booting
2360                  * firmware blob.
2361                  */
2362                 ret = ath10k_pci_init_pipes(ar);
2363                 if (ret) {
2364                         ath10k_warn(ar, "failed to init copy engine: %d\n",
2365                                     ret);
2366                         continue;
2367                 }
2368
2369                 ret = ath10k_pci_diag_read32(ar, QCA988X_HOST_INTEREST_ADDRESS,
2370                                              &val);
2371                 if (ret) {
2372                         ath10k_warn(ar, "failed to poke copy engine: %d\n",
2373                                     ret);
2374                         continue;
2375                 }
2376
2377                 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot chip reset complete (warm)\n");
2378                 return 0;
2379         }
2380
2381         if (ath10k_pci_reset_mode == ATH10K_PCI_RESET_WARM_ONLY) {
2382                 ath10k_warn(ar, "refusing cold reset as requested\n");
2383                 return -EPERM;
2384         }
2385
2386         ret = ath10k_pci_cold_reset(ar);
2387         if (ret) {
2388                 ath10k_warn(ar, "failed to cold reset: %d\n", ret);
2389                 return ret;
2390         }
2391
2392         ret = ath10k_pci_wait_for_target_init(ar);
2393         if (ret) {
2394                 ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
2395                             ret);
2396                 return ret;
2397         }
2398
2399         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca988x chip reset complete (cold)\n");
2400
2401         return 0;
2402 }
2403
2404 static int ath10k_pci_qca6174_chip_reset(struct ath10k *ar)
2405 {
2406         int ret;
2407
2408         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca6174 chip reset\n");
2409
2410         /* FIXME: QCA6174 requires cold + warm reset to work. */
2411
2412         ret = ath10k_pci_cold_reset(ar);
2413         if (ret) {
2414                 ath10k_warn(ar, "failed to cold reset: %d\n", ret);
2415                 return ret;
2416         }
2417
2418         ret = ath10k_pci_wait_for_target_init(ar);
2419         if (ret) {
2420                 ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
2421                             ret);
2422                 return ret;
2423         }
2424
2425         ret = ath10k_pci_warm_reset(ar);
2426         if (ret) {
2427                 ath10k_warn(ar, "failed to warm reset: %d\n", ret);
2428                 return ret;
2429         }
2430
2431         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca6174 chip reset complete (cold)\n");
2432
2433         return 0;
2434 }
2435
2436 static int ath10k_pci_qca99x0_chip_reset(struct ath10k *ar)
2437 {
2438         int ret;
2439
2440         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca99x0 chip reset\n");
2441
2442         ret = ath10k_pci_cold_reset(ar);
2443         if (ret) {
2444                 ath10k_warn(ar, "failed to cold reset: %d\n", ret);
2445                 return ret;
2446         }
2447
2448         ret = ath10k_pci_wait_for_target_init(ar);
2449         if (ret) {
2450                 ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
2451                             ret);
2452                 return ret;
2453         }
2454
2455         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca99x0 chip reset complete (cold)\n");
2456
2457         return 0;
2458 }
2459
2460 static int ath10k_pci_chip_reset(struct ath10k *ar)
2461 {
2462         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2463
2464         if (WARN_ON(!ar_pci->pci_hard_reset))
2465                 return -ENOTSUPP;
2466
2467         return ar_pci->pci_hard_reset(ar);
2468 }
2469
2470 static int ath10k_pci_hif_power_up(struct ath10k *ar)
2471 {
2472         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2473         int ret;
2474
2475         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif power up\n");
2476
2477         pcie_capability_read_word(ar_pci->pdev, PCI_EXP_LNKCTL,
2478                                   &ar_pci->link_ctl);
2479         pcie_capability_write_word(ar_pci->pdev, PCI_EXP_LNKCTL,
2480                                    ar_pci->link_ctl & ~PCI_EXP_LNKCTL_ASPMC);
2481
2482         /*
2483          * Bring the target up cleanly.
2484          *
2485          * The target may be in an undefined state with an AUX-powered Target
2486          * and a Host in WoW mode. If the Host crashes, loses power, or is
2487          * restarted (without unloading the driver) then the Target is left
2488          * (aux) powered and running. On a subsequent driver load, the Target
2489          * is in an unexpected state. We try to catch that here in order to
2490          * reset the Target and retry the probe.
2491          */
2492         ret = ath10k_pci_chip_reset(ar);
2493         if (ret) {
2494                 if (ath10k_pci_has_fw_crashed(ar)) {
2495                         ath10k_warn(ar, "firmware crashed during chip reset\n");
2496                         ath10k_pci_fw_crashed_clear(ar);
2497                         ath10k_pci_fw_crashed_dump(ar);
2498                 }
2499
2500                 ath10k_err(ar, "failed to reset chip: %d\n", ret);
2501                 goto err_sleep;
2502         }
2503
2504         ret = ath10k_pci_init_pipes(ar);
2505         if (ret) {
2506                 ath10k_err(ar, "failed to initialize CE: %d\n", ret);
2507                 goto err_sleep;
2508         }
2509
2510         ret = ath10k_pci_init_config(ar);
2511         if (ret) {
2512                 ath10k_err(ar, "failed to setup init config: %d\n", ret);
2513                 goto err_ce;
2514         }
2515
2516         ret = ath10k_pci_wake_target_cpu(ar);
2517         if (ret) {
2518                 ath10k_err(ar, "could not wake up target CPU: %d\n", ret);
2519                 goto err_ce;
2520         }
2521
2522         return 0;
2523
2524 err_ce:
2525         ath10k_pci_ce_deinit(ar);
2526
2527 err_sleep:
2528         return ret;
2529 }
2530
2531 void ath10k_pci_hif_power_down(struct ath10k *ar)
2532 {
2533         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif power down\n");
2534
2535         /* Currently hif_power_up performs effectively a reset and hif_stop
2536          * resets the chip as well so there's no point in resetting here.
2537          */
2538 }
2539
2540 #ifdef CONFIG_PM
2541
2542 static int ath10k_pci_hif_suspend(struct ath10k *ar)
2543 {
2544         /* The grace timer can still be counting down and ar->ps_awake be true.
2545          * It is known that the device may be asleep after resuming regardless
2546          * of the SoC powersave state before suspending. Hence make sure the
2547          * device is asleep before proceeding.
2548          */
2549         ath10k_pci_sleep_sync(ar);
2550
2551         return 0;
2552 }
2553
2554 static int ath10k_pci_hif_resume(struct ath10k *ar)
2555 {
2556         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2557         struct pci_dev *pdev = ar_pci->pdev;
2558         u32 val;
2559         int ret = 0;
2560
2561         ret = ath10k_pci_force_wake(ar);
2562         if (ret) {
2563                 ath10k_err(ar, "failed to wake up target: %d\n", ret);
2564                 return ret;
2565         }
2566
2567         /* Suspend/Resume resets the PCI configuration space, so we have to
2568          * re-disable the RETRY_TIMEOUT register (0x41) to keep PCI Tx retries
2569          * from interfering with C3 CPU state. pci_restore_state won't help
2570          * here since it only restores the first 64 bytes pci config header.
2571          */
2572         pci_read_config_dword(pdev, 0x40, &val);
2573         if ((val & 0x0000ff00) != 0)
2574                 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
2575
2576         return ret;
2577 }
2578 #endif
2579
2580 static bool ath10k_pci_validate_cal(void *data, size_t size)
2581 {
2582         __le16 *cal_words = data;
2583         u16 checksum = 0;
2584         size_t i;
2585
2586         if (size % 2 != 0)
2587                 return false;
2588
2589         for (i = 0; i < size / 2; i++)
2590                 checksum ^= le16_to_cpu(cal_words[i]);
2591
2592         return checksum == 0xffff;
2593 }
2594
2595 static void ath10k_pci_enable_eeprom(struct ath10k *ar)
2596 {
2597         /* Enable SI clock */
2598         ath10k_pci_soc_write32(ar, CLOCK_CONTROL_OFFSET, 0x0);
2599
2600         /* Configure GPIOs for I2C operation */
2601         ath10k_pci_write32(ar,
2602                            GPIO_BASE_ADDRESS + GPIO_PIN0_OFFSET +
2603                            4 * QCA9887_1_0_I2C_SDA_GPIO_PIN,
2604                            SM(QCA9887_1_0_I2C_SDA_PIN_CONFIG,
2605                               GPIO_PIN0_CONFIG) |
2606                            SM(1, GPIO_PIN0_PAD_PULL));
2607
2608         ath10k_pci_write32(ar,
2609                            GPIO_BASE_ADDRESS + GPIO_PIN0_OFFSET +
2610                            4 * QCA9887_1_0_SI_CLK_GPIO_PIN,
2611                            SM(QCA9887_1_0_SI_CLK_PIN_CONFIG, GPIO_PIN0_CONFIG) |
2612                            SM(1, GPIO_PIN0_PAD_PULL));
2613
2614         ath10k_pci_write32(ar,
2615                            GPIO_BASE_ADDRESS +
2616                            QCA9887_1_0_GPIO_ENABLE_W1TS_LOW_ADDRESS,
2617                            1u << QCA9887_1_0_SI_CLK_GPIO_PIN);
2618
2619         /* In Swift ASIC - EEPROM clock will be (110MHz/512) = 214KHz */
2620         ath10k_pci_write32(ar,
2621                            SI_BASE_ADDRESS + SI_CONFIG_OFFSET,
2622                            SM(1, SI_CONFIG_ERR_INT) |
2623                            SM(1, SI_CONFIG_BIDIR_OD_DATA) |
2624                            SM(1, SI_CONFIG_I2C) |
2625                            SM(1, SI_CONFIG_POS_SAMPLE) |
2626                            SM(1, SI_CONFIG_INACTIVE_DATA) |
2627                            SM(1, SI_CONFIG_INACTIVE_CLK) |
2628                            SM(8, SI_CONFIG_DIVIDER));
2629 }
2630
2631 static int ath10k_pci_read_eeprom(struct ath10k *ar, u16 addr, u8 *out)
2632 {
2633         u32 reg;
2634         int wait_limit;
2635
2636         /* set device select byte and for the read operation */
2637         reg = QCA9887_EEPROM_SELECT_READ |
2638               SM(addr, QCA9887_EEPROM_ADDR_LO) |
2639               SM(addr >> 8, QCA9887_EEPROM_ADDR_HI);
2640         ath10k_pci_write32(ar, SI_BASE_ADDRESS + SI_TX_DATA0_OFFSET, reg);
2641
2642         /* write transmit data, transfer length, and START bit */
2643         ath10k_pci_write32(ar, SI_BASE_ADDRESS + SI_CS_OFFSET,
2644                            SM(1, SI_CS_START) | SM(1, SI_CS_RX_CNT) |
2645                            SM(4, SI_CS_TX_CNT));
2646
2647         /* wait max 1 sec */
2648         wait_limit = 100000;
2649
2650         /* wait for SI_CS_DONE_INT */
2651         do {
2652                 reg = ath10k_pci_read32(ar, SI_BASE_ADDRESS + SI_CS_OFFSET);
2653                 if (MS(reg, SI_CS_DONE_INT))
2654                         break;
2655
2656                 wait_limit--;
2657                 udelay(10);
2658         } while (wait_limit > 0);
2659
2660         if (!MS(reg, SI_CS_DONE_INT)) {
2661                 ath10k_err(ar, "timeout while reading device EEPROM at %04x\n",
2662                            addr);
2663                 return -ETIMEDOUT;
2664         }
2665
2666         /* clear SI_CS_DONE_INT */
2667         ath10k_pci_write32(ar, SI_BASE_ADDRESS + SI_CS_OFFSET, reg);
2668
2669         if (MS(reg, SI_CS_DONE_ERR)) {
2670                 ath10k_err(ar, "failed to read device EEPROM at %04x\n", addr);
2671                 return -EIO;
2672         }
2673
2674         /* extract receive data */
2675         reg = ath10k_pci_read32(ar, SI_BASE_ADDRESS + SI_RX_DATA0_OFFSET);
2676         *out = reg;
2677
2678         return 0;
2679 }
2680
2681 static int ath10k_pci_hif_fetch_cal_eeprom(struct ath10k *ar, void **data,
2682                                            size_t *data_len)
2683 {
2684         u8 *caldata = NULL;
2685         size_t calsize, i;
2686         int ret;
2687
2688         if (!QCA_REV_9887(ar))
2689                 return -EOPNOTSUPP;
2690
2691         calsize = ar->hw_params.cal_data_len;
2692         caldata = kmalloc(calsize, GFP_KERNEL);
2693         if (!caldata)
2694                 return -ENOMEM;
2695
2696         ath10k_pci_enable_eeprom(ar);
2697
2698         for (i = 0; i < calsize; i++) {
2699                 ret = ath10k_pci_read_eeprom(ar, i, &caldata[i]);
2700                 if (ret)
2701                         goto err_free;
2702         }
2703
2704         if (!ath10k_pci_validate_cal(caldata, calsize))
2705                 goto err_free;
2706
2707         *data = caldata;
2708         *data_len = calsize;
2709
2710         return 0;
2711
2712 err_free:
2713         kfree(data);
2714
2715         return -EINVAL;
2716 }
2717
2718 static const struct ath10k_hif_ops ath10k_pci_hif_ops = {
2719         .tx_sg                  = ath10k_pci_hif_tx_sg,
2720         .diag_read              = ath10k_pci_hif_diag_read,
2721         .diag_write             = ath10k_pci_diag_write_mem,
2722         .exchange_bmi_msg       = ath10k_pci_hif_exchange_bmi_msg,
2723         .start                  = ath10k_pci_hif_start,
2724         .stop                   = ath10k_pci_hif_stop,
2725         .map_service_to_pipe    = ath10k_pci_hif_map_service_to_pipe,
2726         .get_default_pipe       = ath10k_pci_hif_get_default_pipe,
2727         .send_complete_check    = ath10k_pci_hif_send_complete_check,
2728         .get_free_queue_number  = ath10k_pci_hif_get_free_queue_number,
2729         .power_up               = ath10k_pci_hif_power_up,
2730         .power_down             = ath10k_pci_hif_power_down,
2731         .read32                 = ath10k_pci_read32,
2732         .write32                = ath10k_pci_write32,
2733 #ifdef CONFIG_PM
2734         .suspend                = ath10k_pci_hif_suspend,
2735         .resume                 = ath10k_pci_hif_resume,
2736 #endif
2737         .fetch_cal_eeprom       = ath10k_pci_hif_fetch_cal_eeprom,
2738 };
2739
2740 /*
2741  * Top-level interrupt handler for all PCI interrupts from a Target.
2742  * When a block of MSI interrupts is allocated, this top-level handler
2743  * is not used; instead, we directly call the correct sub-handler.
2744  */
2745 static irqreturn_t ath10k_pci_interrupt_handler(int irq, void *arg)
2746 {
2747         struct ath10k *ar = arg;
2748         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2749         int ret;
2750
2751         ret = ath10k_pci_force_wake(ar);
2752         if (ret) {
2753                 ath10k_warn(ar, "failed to wake device up on irq: %d\n", ret);
2754                 return IRQ_NONE;
2755         }
2756
2757         if (ar_pci->oper_irq_mode == ATH10K_PCI_IRQ_LEGACY) {
2758                 if (!ath10k_pci_irq_pending(ar))
2759                         return IRQ_NONE;
2760
2761                 ath10k_pci_disable_and_clear_legacy_irq(ar);
2762         }
2763
2764         tasklet_schedule(&ar_pci->intr_tq);
2765
2766         return IRQ_HANDLED;
2767 }
2768
2769 static void ath10k_pci_tasklet(unsigned long data)
2770 {
2771         struct ath10k *ar = (struct ath10k *)data;
2772         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2773
2774         if (ath10k_pci_has_fw_crashed(ar)) {
2775                 ath10k_pci_irq_disable(ar);
2776                 ath10k_pci_fw_crashed_clear(ar);
2777                 ath10k_pci_fw_crashed_dump(ar);
2778                 return;
2779         }
2780
2781         ath10k_ce_per_engine_service_any(ar);
2782
2783         /* Re-enable legacy irq that was disabled in the irq handler */
2784         if (ar_pci->oper_irq_mode == ATH10K_PCI_IRQ_LEGACY)
2785                 ath10k_pci_enable_legacy_irq(ar);
2786 }
2787
2788 static int ath10k_pci_request_irq_msi(struct ath10k *ar)
2789 {
2790         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2791         int ret;
2792
2793         ret = request_irq(ar_pci->pdev->irq,
2794                           ath10k_pci_interrupt_handler,
2795                           IRQF_SHARED, "ath10k_pci", ar);
2796         if (ret) {
2797                 ath10k_warn(ar, "failed to request MSI irq %d: %d\n",
2798                             ar_pci->pdev->irq, ret);
2799                 return ret;
2800         }
2801
2802         return 0;
2803 }
2804
2805 static int ath10k_pci_request_irq_legacy(struct ath10k *ar)
2806 {
2807         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2808         int ret;
2809
2810         ret = request_irq(ar_pci->pdev->irq,
2811                           ath10k_pci_interrupt_handler,
2812                           IRQF_SHARED, "ath10k_pci", ar);
2813         if (ret) {
2814                 ath10k_warn(ar, "failed to request legacy irq %d: %d\n",
2815                             ar_pci->pdev->irq, ret);
2816                 return ret;
2817         }
2818
2819         return 0;
2820 }
2821
2822 static int ath10k_pci_request_irq(struct ath10k *ar)
2823 {
2824         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2825
2826         switch (ar_pci->oper_irq_mode) {
2827         case ATH10K_PCI_IRQ_LEGACY:
2828                 return ath10k_pci_request_irq_legacy(ar);
2829         case ATH10K_PCI_IRQ_MSI:
2830                 return ath10k_pci_request_irq_msi(ar);
2831         default:
2832                 return -EINVAL;
2833         }
2834 }
2835
2836 static void ath10k_pci_free_irq(struct ath10k *ar)
2837 {
2838         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2839
2840         free_irq(ar_pci->pdev->irq, ar);
2841 }
2842
2843 void ath10k_pci_init_irq_tasklets(struct ath10k *ar)
2844 {
2845         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2846
2847         tasklet_init(&ar_pci->intr_tq, ath10k_pci_tasklet, (unsigned long)ar);
2848 }
2849
2850 static int ath10k_pci_init_irq(struct ath10k *ar)
2851 {
2852         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2853         int ret;
2854
2855         ath10k_pci_init_irq_tasklets(ar);
2856
2857         if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_AUTO)
2858                 ath10k_info(ar, "limiting irq mode to: %d\n",
2859                             ath10k_pci_irq_mode);
2860
2861         /* Try MSI */
2862         if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_LEGACY) {
2863                 ar_pci->oper_irq_mode = ATH10K_PCI_IRQ_MSI;
2864                 ret = pci_enable_msi(ar_pci->pdev);
2865                 if (ret == 0)
2866                         return 0;
2867
2868                 /* fall-through */
2869         }
2870
2871         /* Try legacy irq
2872          *
2873          * A potential race occurs here: The CORE_BASE write
2874          * depends on target correctly decoding AXI address but
2875          * host won't know when target writes BAR to CORE_CTRL.
2876          * This write might get lost if target has NOT written BAR.
2877          * For now, fix the race by repeating the write in below
2878          * synchronization checking. */
2879         ar_pci->oper_irq_mode = ATH10K_PCI_IRQ_LEGACY;
2880
2881         ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2882                            PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
2883
2884         return 0;
2885 }
2886
2887 static void ath10k_pci_deinit_irq_legacy(struct ath10k *ar)
2888 {
2889         ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2890                            0);
2891 }
2892
2893 static int ath10k_pci_deinit_irq(struct ath10k *ar)
2894 {
2895         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2896
2897         switch (ar_pci->oper_irq_mode) {
2898         case ATH10K_PCI_IRQ_LEGACY:
2899                 ath10k_pci_deinit_irq_legacy(ar);
2900                 break;
2901         default:
2902                 pci_disable_msi(ar_pci->pdev);
2903                 break;
2904         }
2905
2906         return 0;
2907 }
2908
2909 int ath10k_pci_wait_for_target_init(struct ath10k *ar)
2910 {
2911         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2912         unsigned long timeout;
2913         u32 val;
2914
2915         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot waiting target to initialise\n");
2916
2917         timeout = jiffies + msecs_to_jiffies(ATH10K_PCI_TARGET_WAIT);
2918
2919         do {
2920                 val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
2921
2922                 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target indicator %x\n",
2923                            val);
2924
2925                 /* target should never return this */
2926                 if (val == 0xffffffff)
2927                         continue;
2928
2929                 /* the device has crashed so don't bother trying anymore */
2930                 if (val & FW_IND_EVENT_PENDING)
2931                         break;
2932
2933                 if (val & FW_IND_INITIALIZED)
2934                         break;
2935
2936                 if (ar_pci->oper_irq_mode == ATH10K_PCI_IRQ_LEGACY)
2937                         /* Fix potential race by repeating CORE_BASE writes */
2938                         ath10k_pci_enable_legacy_irq(ar);
2939
2940                 mdelay(10);
2941         } while (time_before(jiffies, timeout));
2942
2943         ath10k_pci_disable_and_clear_legacy_irq(ar);
2944         ath10k_pci_irq_msi_fw_mask(ar);
2945
2946         if (val == 0xffffffff) {
2947                 ath10k_err(ar, "failed to read device register, device is gone\n");
2948                 return -EIO;
2949         }
2950
2951         if (val & FW_IND_EVENT_PENDING) {
2952                 ath10k_warn(ar, "device has crashed during init\n");
2953                 return -ECOMM;
2954         }
2955
2956         if (!(val & FW_IND_INITIALIZED)) {
2957                 ath10k_err(ar, "failed to receive initialized event from target: %08x\n",
2958                            val);
2959                 return -ETIMEDOUT;
2960         }
2961
2962         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target initialised\n");
2963         return 0;
2964 }
2965
2966 static int ath10k_pci_cold_reset(struct ath10k *ar)
2967 {
2968         u32 val;
2969
2970         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cold reset\n");
2971
2972         spin_lock_bh(&ar->data_lock);
2973
2974         ar->stats.fw_cold_reset_counter++;
2975
2976         spin_unlock_bh(&ar->data_lock);
2977
2978         /* Put Target, including PCIe, into RESET. */
2979         val = ath10k_pci_reg_read32(ar, SOC_GLOBAL_RESET_ADDRESS);
2980         val |= 1;
2981         ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
2982
2983         /* After writing into SOC_GLOBAL_RESET to put device into
2984          * reset and pulling out of reset pcie may not be stable
2985          * for any immediate pcie register access and cause bus error,
2986          * add delay before any pcie access request to fix this issue.
2987          */
2988         msleep(20);
2989
2990         /* Pull Target, including PCIe, out of RESET. */
2991         val &= ~1;
2992         ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
2993
2994         msleep(20);
2995
2996         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cold reset complete\n");
2997
2998         return 0;
2999 }
3000
3001 static int ath10k_pci_claim(struct ath10k *ar)
3002 {
3003         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
3004         struct pci_dev *pdev = ar_pci->pdev;
3005         int ret;
3006
3007         pci_set_drvdata(pdev, ar);
3008
3009         ret = pci_enable_device(pdev);
3010         if (ret) {
3011                 ath10k_err(ar, "failed to enable pci device: %d\n", ret);
3012                 return ret;
3013         }
3014
3015         ret = pci_request_region(pdev, BAR_NUM, "ath");
3016         if (ret) {
3017                 ath10k_err(ar, "failed to request region BAR%d: %d\n", BAR_NUM,
3018                            ret);
3019                 goto err_device;
3020         }
3021
3022         /* Target expects 32 bit DMA. Enforce it. */
3023         ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
3024         if (ret) {
3025                 ath10k_err(ar, "failed to set dma mask to 32-bit: %d\n", ret);
3026                 goto err_region;
3027         }
3028
3029         ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
3030         if (ret) {
3031                 ath10k_err(ar, "failed to set consistent dma mask to 32-bit: %d\n",
3032                            ret);
3033                 goto err_region;
3034         }
3035
3036         pci_set_master(pdev);
3037
3038         /* Arrange for access to Target SoC registers. */
3039         ar_pci->mem_len = pci_resource_len(pdev, BAR_NUM);
3040         ar_pci->mem = pci_iomap(pdev, BAR_NUM, 0);
3041         if (!ar_pci->mem) {
3042                 ath10k_err(ar, "failed to iomap BAR%d\n", BAR_NUM);
3043                 ret = -EIO;
3044                 goto err_master;
3045         }
3046
3047         ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot pci_mem 0x%p\n", ar_pci->mem);
3048         return 0;
3049
3050 err_master:
3051         pci_clear_master(pdev);
3052
3053 err_region:
3054         pci_release_region(pdev, BAR_NUM);
3055
3056 err_device:
3057         pci_disable_device(pdev);
3058
3059         return ret;
3060 }
3061
3062 static void ath10k_pci_release(struct ath10k *ar)
3063 {
3064         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
3065         struct pci_dev *pdev = ar_pci->pdev;
3066
3067         pci_iounmap(pdev, ar_pci->mem);
3068         pci_release_region(pdev, BAR_NUM);
3069         pci_clear_master(pdev);
3070         pci_disable_device(pdev);
3071 }
3072
3073 static bool ath10k_pci_chip_is_supported(u32 dev_id, u32 chip_id)
3074 {
3075         const struct ath10k_pci_supp_chip *supp_chip;
3076         int i;
3077         u32 rev_id = MS(chip_id, SOC_CHIP_ID_REV);
3078
3079         for (i = 0; i < ARRAY_SIZE(ath10k_pci_supp_chips); i++) {
3080                 supp_chip = &ath10k_pci_supp_chips[i];
3081
3082                 if (supp_chip->dev_id == dev_id &&
3083                     supp_chip->rev_id == rev_id)
3084                         return true;
3085         }
3086
3087         return false;
3088 }
3089
3090 int ath10k_pci_setup_resource(struct ath10k *ar)
3091 {
3092         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
3093         int ret;
3094
3095         spin_lock_init(&ar_pci->ce_lock);
3096         spin_lock_init(&ar_pci->ps_lock);
3097
3098         setup_timer(&ar_pci->rx_post_retry, ath10k_pci_rx_replenish_retry,
3099                     (unsigned long)ar);
3100
3101         if (QCA_REV_6174(ar))
3102                 ath10k_pci_override_ce_config(ar);
3103
3104         ret = ath10k_pci_alloc_pipes(ar);
3105         if (ret) {
3106                 ath10k_err(ar, "failed to allocate copy engine pipes: %d\n",
3107                            ret);
3108                 return ret;
3109         }
3110
3111         return 0;
3112 }
3113
3114 void ath10k_pci_release_resource(struct ath10k *ar)
3115 {
3116         ath10k_pci_kill_tasklet(ar);
3117         ath10k_pci_ce_deinit(ar);
3118         ath10k_pci_free_pipes(ar);
3119 }
3120
3121 static const struct ath10k_bus_ops ath10k_pci_bus_ops = {
3122         .read32         = ath10k_bus_pci_read32,
3123         .write32        = ath10k_bus_pci_write32,
3124         .get_num_banks  = ath10k_pci_get_num_banks,
3125 };
3126
3127 static int ath10k_pci_probe(struct pci_dev *pdev,
3128                             const struct pci_device_id *pci_dev)
3129 {
3130         int ret = 0;
3131         struct ath10k *ar;
3132         struct ath10k_pci *ar_pci;
3133         enum ath10k_hw_rev hw_rev;
3134         u32 chip_id;
3135         bool pci_ps;
3136         int (*pci_soft_reset)(struct ath10k *ar);
3137         int (*pci_hard_reset)(struct ath10k *ar);
3138
3139         switch (pci_dev->device) {
3140         case QCA988X_2_0_DEVICE_ID:
3141                 hw_rev = ATH10K_HW_QCA988X;
3142                 pci_ps = false;
3143                 pci_soft_reset = ath10k_pci_warm_reset;
3144                 pci_hard_reset = ath10k_pci_qca988x_chip_reset;
3145                 break;
3146         case QCA9887_1_0_DEVICE_ID:
3147                 dev_warn(&pdev->dev, "QCA9887 support is still experimental, there are likely bugs. You have been warned.\n");
3148                 hw_rev = ATH10K_HW_QCA9887;
3149                 pci_ps = false;
3150                 pci_soft_reset = ath10k_pci_warm_reset;
3151                 pci_hard_reset = ath10k_pci_qca988x_chip_reset;
3152                 break;
3153         case QCA6164_2_1_DEVICE_ID:
3154         case QCA6174_2_1_DEVICE_ID:
3155                 hw_rev = ATH10K_HW_QCA6174;
3156                 pci_ps = true;
3157                 pci_soft_reset = ath10k_pci_warm_reset;
3158                 pci_hard_reset = ath10k_pci_qca6174_chip_reset;
3159                 break;
3160         case QCA99X0_2_0_DEVICE_ID:
3161                 hw_rev = ATH10K_HW_QCA99X0;
3162                 pci_ps = false;
3163                 pci_soft_reset = ath10k_pci_qca99x0_soft_chip_reset;
3164                 pci_hard_reset = ath10k_pci_qca99x0_chip_reset;
3165                 break;
3166         case QCA9984_1_0_DEVICE_ID:
3167                 hw_rev = ATH10K_HW_QCA9984;
3168                 pci_ps = false;
3169                 pci_soft_reset = ath10k_pci_qca99x0_soft_chip_reset;
3170                 pci_hard_reset = ath10k_pci_qca99x0_chip_reset;
3171                 break;
3172         case QCA9377_1_0_DEVICE_ID:
3173                 hw_rev = ATH10K_HW_QCA9377;
3174                 pci_ps = true;
3175                 pci_soft_reset = NULL;
3176                 pci_hard_reset = ath10k_pci_qca6174_chip_reset;
3177                 break;
3178         default:
3179                 WARN_ON(1);
3180                 return -ENOTSUPP;
3181         }
3182
3183         ar = ath10k_core_create(sizeof(*ar_pci), &pdev->dev, ATH10K_BUS_PCI,
3184                                 hw_rev, &ath10k_pci_hif_ops);
3185         if (!ar) {
3186                 dev_err(&pdev->dev, "failed to allocate core\n");
3187                 return -ENOMEM;
3188         }
3189
3190         ath10k_dbg(ar, ATH10K_DBG_BOOT, "pci probe %04x:%04x %04x:%04x\n",
3191                    pdev->vendor, pdev->device,
3192                    pdev->subsystem_vendor, pdev->subsystem_device);
3193
3194         ar_pci = ath10k_pci_priv(ar);
3195         ar_pci->pdev = pdev;
3196         ar_pci->dev = &pdev->dev;
3197         ar_pci->ar = ar;
3198         ar->dev_id = pci_dev->device;
3199         ar_pci->pci_ps = pci_ps;
3200         ar_pci->bus_ops = &ath10k_pci_bus_ops;
3201         ar_pci->pci_soft_reset = pci_soft_reset;
3202         ar_pci->pci_hard_reset = pci_hard_reset;
3203
3204         ar->id.vendor = pdev->vendor;
3205         ar->id.device = pdev->device;
3206         ar->id.subsystem_vendor = pdev->subsystem_vendor;
3207         ar->id.subsystem_device = pdev->subsystem_device;
3208
3209         setup_timer(&ar_pci->ps_timer, ath10k_pci_ps_timer,
3210                     (unsigned long)ar);
3211
3212         ret = ath10k_pci_setup_resource(ar);
3213         if (ret) {
3214                 ath10k_err(ar, "failed to setup resource: %d\n", ret);
3215                 goto err_core_destroy;
3216         }
3217
3218         ret = ath10k_pci_claim(ar);
3219         if (ret) {
3220                 ath10k_err(ar, "failed to claim device: %d\n", ret);
3221                 goto err_free_pipes;
3222         }
3223
3224         ret = ath10k_pci_force_wake(ar);
3225         if (ret) {
3226                 ath10k_warn(ar, "failed to wake up device : %d\n", ret);
3227                 goto err_sleep;
3228         }
3229
3230         ath10k_pci_ce_deinit(ar);
3231         ath10k_pci_irq_disable(ar);
3232
3233         ret = ath10k_pci_init_irq(ar);
3234         if (ret) {
3235                 ath10k_err(ar, "failed to init irqs: %d\n", ret);
3236                 goto err_sleep;
3237         }
3238
3239         ath10k_info(ar, "pci irq %s oper_irq_mode %d irq_mode %d reset_mode %d\n",
3240                     ath10k_pci_get_irq_method(ar), ar_pci->oper_irq_mode,
3241                     ath10k_pci_irq_mode, ath10k_pci_reset_mode);
3242
3243         ret = ath10k_pci_request_irq(ar);
3244         if (ret) {
3245                 ath10k_warn(ar, "failed to request irqs: %d\n", ret);
3246                 goto err_deinit_irq;
3247         }
3248
3249         ret = ath10k_pci_chip_reset(ar);
3250         if (ret) {
3251                 ath10k_err(ar, "failed to reset chip: %d\n", ret);
3252                 goto err_free_irq;
3253         }
3254
3255         chip_id = ath10k_pci_soc_read32(ar, SOC_CHIP_ID_ADDRESS);
3256         if (chip_id == 0xffffffff) {
3257                 ath10k_err(ar, "failed to get chip id\n");
3258                 goto err_free_irq;
3259         }
3260
3261         if (!ath10k_pci_chip_is_supported(pdev->device, chip_id)) {
3262                 ath10k_err(ar, "device %04x with chip_id %08x isn't supported\n",
3263                            pdev->device, chip_id);
3264                 goto err_free_irq;
3265         }
3266
3267         ret = ath10k_core_register(ar, chip_id);
3268         if (ret) {
3269                 ath10k_err(ar, "failed to register driver core: %d\n", ret);
3270                 goto err_free_irq;
3271         }
3272
3273         return 0;
3274
3275 err_free_irq:
3276         ath10k_pci_free_irq(ar);
3277         ath10k_pci_kill_tasklet(ar);
3278
3279 err_deinit_irq:
3280         ath10k_pci_deinit_irq(ar);
3281
3282 err_sleep:
3283         ath10k_pci_sleep_sync(ar);
3284         ath10k_pci_release(ar);
3285
3286 err_free_pipes:
3287         ath10k_pci_free_pipes(ar);
3288
3289 err_core_destroy:
3290         ath10k_core_destroy(ar);
3291
3292         return ret;
3293 }
3294
3295 static void ath10k_pci_remove(struct pci_dev *pdev)
3296 {
3297         struct ath10k *ar = pci_get_drvdata(pdev);
3298         struct ath10k_pci *ar_pci;
3299
3300         ath10k_dbg(ar, ATH10K_DBG_PCI, "pci remove\n");
3301
3302         if (!ar)
3303                 return;
3304
3305         ar_pci = ath10k_pci_priv(ar);
3306
3307         if (!ar_pci)
3308                 return;
3309
3310         ath10k_core_unregister(ar);
3311         ath10k_pci_free_irq(ar);
3312         ath10k_pci_deinit_irq(ar);
3313         ath10k_pci_release_resource(ar);
3314         ath10k_pci_sleep_sync(ar);
3315         ath10k_pci_release(ar);
3316         ath10k_core_destroy(ar);
3317 }
3318
3319 MODULE_DEVICE_TABLE(pci, ath10k_pci_id_table);
3320
3321 static struct pci_driver ath10k_pci_driver = {
3322         .name = "ath10k_pci",
3323         .id_table = ath10k_pci_id_table,
3324         .probe = ath10k_pci_probe,
3325         .remove = ath10k_pci_remove,
3326 };
3327
3328 static int __init ath10k_pci_init(void)
3329 {
3330         int ret;
3331
3332         ret = pci_register_driver(&ath10k_pci_driver);
3333         if (ret)
3334                 printk(KERN_ERR "failed to register ath10k pci driver: %d\n",
3335                        ret);
3336
3337         ret = ath10k_ahb_init();
3338         if (ret)
3339                 printk(KERN_ERR "ahb init failed: %d\n", ret);
3340
3341         return ret;
3342 }
3343 module_init(ath10k_pci_init);
3344
3345 static void __exit ath10k_pci_exit(void)
3346 {
3347         pci_unregister_driver(&ath10k_pci_driver);
3348         ath10k_ahb_exit();
3349 }
3350
3351 module_exit(ath10k_pci_exit);
3352
3353 MODULE_AUTHOR("Qualcomm Atheros");
3354 MODULE_DESCRIPTION("Driver support for Qualcomm Atheros 802.11ac WLAN PCIe/AHB devices");
3355 MODULE_LICENSE("Dual BSD/GPL");
3356
3357 /* QCA988x 2.0 firmware files */
3358 MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API2_FILE);
3359 MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API3_FILE);
3360 MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API4_FILE);
3361 MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API5_FILE);
3362 MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_BOARD_DATA_FILE);
3363 MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_BOARD_API2_FILE);
3364
3365 /* QCA9887 1.0 firmware files */
3366 MODULE_FIRMWARE(QCA9887_HW_1_0_FW_DIR "/" ATH10K_FW_API5_FILE);
3367 MODULE_FIRMWARE(QCA9887_HW_1_0_FW_DIR "/" QCA9887_HW_1_0_BOARD_DATA_FILE);
3368 MODULE_FIRMWARE(QCA9887_HW_1_0_FW_DIR "/" ATH10K_BOARD_API2_FILE);
3369
3370 /* QCA6174 2.1 firmware files */
3371 MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" ATH10K_FW_API4_FILE);
3372 MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" ATH10K_FW_API5_FILE);
3373 MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" QCA6174_HW_2_1_BOARD_DATA_FILE);
3374 MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" ATH10K_BOARD_API2_FILE);
3375
3376 /* QCA6174 3.1 firmware files */
3377 MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" ATH10K_FW_API4_FILE);
3378 MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" ATH10K_FW_API5_FILE);
3379 MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" QCA6174_HW_3_0_BOARD_DATA_FILE);
3380 MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" ATH10K_BOARD_API2_FILE);
3381
3382 /* QCA9377 1.0 firmware files */
3383 MODULE_FIRMWARE(QCA9377_HW_1_0_FW_DIR "/" ATH10K_FW_API5_FILE);
3384 MODULE_FIRMWARE(QCA9377_HW_1_0_FW_DIR "/" QCA9377_HW_1_0_BOARD_DATA_FILE);