blob: df7d74e751498565021925348bdae76c6917fd77 [file] [log] [blame]
Kalle Valo5e3dd152013-06-12 20:52:10 +03001/*
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>
Kalle Valo650b91f2013-11-20 10:00:49 +020022#include <linux/bitops.h>
Kalle Valo5e3dd152013-06-12 20:52:10 +030023
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
Michal Kaziorcfe9c452013-11-25 14:06:27 +010036enum ath10k_pci_irq_mode {
37 ATH10K_PCI_IRQ_AUTO = 0,
38 ATH10K_PCI_IRQ_LEGACY = 1,
39 ATH10K_PCI_IRQ_MSI = 2,
40};
41
Kalle Valo35098462014-03-28 09:32:27 +020042enum ath10k_pci_reset_mode {
43 ATH10K_PCI_RESET_AUTO = 0,
44 ATH10K_PCI_RESET_WARM_ONLY = 1,
45};
46
Michal Kaziorcfe9c452013-11-25 14:06:27 +010047static unsigned int ath10k_pci_irq_mode = ATH10K_PCI_IRQ_AUTO;
Kalle Valo35098462014-03-28 09:32:27 +020048static unsigned int ath10k_pci_reset_mode = ATH10K_PCI_RESET_AUTO;
Michal Kaziorcfe9c452013-11-25 14:06:27 +010049
Michal Kaziorcfe9c452013-11-25 14:06:27 +010050module_param_named(irq_mode, ath10k_pci_irq_mode, uint, 0644);
51MODULE_PARM_DESC(irq_mode, "0: auto, 1: legacy, 2: msi (default: 0)");
52
Kalle Valo35098462014-03-28 09:32:27 +020053module_param_named(reset_mode, ath10k_pci_reset_mode, uint, 0644);
54MODULE_PARM_DESC(reset_mode, "0: auto, 1: warm only (default: 0)");
55
Kalle Valo0399eca2014-03-28 09:32:21 +020056/* how long wait to wait for target to initialise, in ms */
57#define ATH10K_PCI_TARGET_WAIT 3000
Michal Kazior61c95ce2014-05-14 16:56:16 +030058#define ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS 3
Kalle Valo0399eca2014-03-28 09:32:21 +020059
Kalle Valo5e3dd152013-06-12 20:52:10 +030060#define QCA988X_2_0_DEVICE_ID (0x003c)
Michal Kaziord63955b2015-01-24 12:14:49 +020061#define QCA6174_2_1_DEVICE_ID (0x003e)
Vasanthakumar Thiagarajan8bd47022015-06-18 12:31:03 +053062#define QCA99X0_2_0_DEVICE_ID (0x0040)
Kalle Valo5e3dd152013-06-12 20:52:10 +030063
Benoit Taine9baa3c32014-08-08 15:56:03 +020064static const struct pci_device_id ath10k_pci_id_table[] = {
Kalle Valo5e3dd152013-06-12 20:52:10 +030065 { PCI_VDEVICE(ATHEROS, QCA988X_2_0_DEVICE_ID) }, /* PCI-E QCA988X V2 */
Michal Kaziord63955b2015-01-24 12:14:49 +020066 { PCI_VDEVICE(ATHEROS, QCA6174_2_1_DEVICE_ID) }, /* PCI-E QCA6174 V2.1 */
Kalle Valo5e3dd152013-06-12 20:52:10 +030067 {0}
68};
69
Michal Kazior7505f7c2014-12-02 10:55:54 +020070static const struct ath10k_pci_supp_chip ath10k_pci_supp_chips[] = {
71 /* QCA988X pre 2.0 chips are not supported because they need some nasty
72 * hacks. ath10k doesn't have them and these devices crash horribly
73 * because of that.
74 */
75 { QCA988X_2_0_DEVICE_ID, QCA988X_HW_2_0_CHIP_ID_REV },
Michal Kaziord63955b2015-01-24 12:14:49 +020076 { QCA6174_2_1_DEVICE_ID, QCA6174_HW_2_1_CHIP_ID_REV },
77 { QCA6174_2_1_DEVICE_ID, QCA6174_HW_2_2_CHIP_ID_REV },
78 { QCA6174_2_1_DEVICE_ID, QCA6174_HW_3_0_CHIP_ID_REV },
79 { QCA6174_2_1_DEVICE_ID, QCA6174_HW_3_1_CHIP_ID_REV },
80 { QCA6174_2_1_DEVICE_ID, QCA6174_HW_3_2_CHIP_ID_REV },
Michal Kazior7505f7c2014-12-02 10:55:54 +020081};
82
Michal Kazior728f95e2014-08-22 14:33:14 +020083static void ath10k_pci_buffer_cleanup(struct ath10k *ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +010084static int ath10k_pci_cold_reset(struct ath10k *ar);
85static int ath10k_pci_warm_reset(struct ath10k *ar);
Michal Kaziord7fb47f2013-11-08 08:01:26 +010086static int ath10k_pci_wait_for_target_init(struct ath10k *ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +010087static int ath10k_pci_init_irq(struct ath10k *ar);
88static int ath10k_pci_deinit_irq(struct ath10k *ar);
89static int ath10k_pci_request_irq(struct ath10k *ar);
90static void ath10k_pci_free_irq(struct ath10k *ar);
Michal Kazior85622cd2013-11-25 14:06:22 +010091static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
92 struct ath10k_ce_pipe *rx_pipe,
93 struct bmi_xfer *xfer);
Kalle Valo5e3dd152013-06-12 20:52:10 +030094
95static const struct ce_attr host_ce_config_wlan[] = {
Kalle Valo48e9c222013-09-01 10:01:32 +030096 /* CE0: host->target HTC control and raw streams */
97 {
98 .flags = CE_ATTR_FLAGS,
99 .src_nentries = 16,
100 .src_sz_max = 256,
101 .dest_nentries = 0,
102 },
103
104 /* CE1: target->host HTT + HTC control */
105 {
106 .flags = CE_ATTR_FLAGS,
107 .src_nentries = 0,
Michal Kazior63838642015-02-09 15:04:55 +0100108 .src_sz_max = 2048,
Kalle Valo48e9c222013-09-01 10:01:32 +0300109 .dest_nentries = 512,
110 },
111
112 /* CE2: target->host WMI */
113 {
114 .flags = CE_ATTR_FLAGS,
115 .src_nentries = 0,
116 .src_sz_max = 2048,
Rajkumar Manoharan30abb332015-03-04 15:43:44 +0200117 .dest_nentries = 128,
Kalle Valo48e9c222013-09-01 10:01:32 +0300118 },
119
120 /* CE3: host->target WMI */
121 {
122 .flags = CE_ATTR_FLAGS,
123 .src_nentries = 32,
124 .src_sz_max = 2048,
125 .dest_nentries = 0,
126 },
127
128 /* CE4: host->target HTT */
129 {
130 .flags = CE_ATTR_FLAGS | CE_ATTR_DIS_INTR,
131 .src_nentries = CE_HTT_H2T_MSG_SRC_NENTRIES,
132 .src_sz_max = 256,
133 .dest_nentries = 0,
134 },
135
136 /* CE5: unused */
137 {
138 .flags = CE_ATTR_FLAGS,
139 .src_nentries = 0,
140 .src_sz_max = 0,
141 .dest_nentries = 0,
142 },
143
144 /* CE6: target autonomous hif_memcpy */
145 {
146 .flags = CE_ATTR_FLAGS,
147 .src_nentries = 0,
148 .src_sz_max = 0,
149 .dest_nentries = 0,
150 },
151
152 /* CE7: ce_diag, the Diagnostic Window */
153 {
154 .flags = CE_ATTR_FLAGS,
155 .src_nentries = 2,
156 .src_sz_max = DIAG_TRANSFER_LIMIT,
157 .dest_nentries = 2,
158 },
Vasanthakumar Thiagarajan050af062015-06-18 12:31:04 +0530159
160 /* CE8: target->host pktlog */
161 {
162 .flags = CE_ATTR_FLAGS,
163 .src_nentries = 0,
164 .src_sz_max = 2048,
165 .dest_nentries = 128,
166 },
167
168 /* CE9 target autonomous qcache memcpy */
169 {
170 .flags = CE_ATTR_FLAGS,
171 .src_nentries = 0,
172 .src_sz_max = 0,
173 .dest_nentries = 0,
174 },
175
176 /* CE10: target autonomous hif memcpy */
177 {
178 .flags = CE_ATTR_FLAGS,
179 .src_nentries = 0,
180 .src_sz_max = 0,
181 .dest_nentries = 0,
182 },
183
184 /* CE11: target autonomous hif memcpy */
185 {
186 .flags = CE_ATTR_FLAGS,
187 .src_nentries = 0,
188 .src_sz_max = 0,
189 .dest_nentries = 0,
190 },
Kalle Valo5e3dd152013-06-12 20:52:10 +0300191};
192
193/* Target firmware's Copy Engine configuration. */
194static const struct ce_pipe_config target_ce_config_wlan[] = {
Kalle Valod88effb2013-09-01 10:01:39 +0300195 /* CE0: host->target HTC control and raw streams */
196 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300197 .pipenum = __cpu_to_le32(0),
198 .pipedir = __cpu_to_le32(PIPEDIR_OUT),
199 .nentries = __cpu_to_le32(32),
200 .nbytes_max = __cpu_to_le32(256),
201 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
202 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300203 },
204
205 /* CE1: target->host HTT + HTC control */
206 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300207 .pipenum = __cpu_to_le32(1),
208 .pipedir = __cpu_to_le32(PIPEDIR_IN),
209 .nentries = __cpu_to_le32(32),
Michal Kazior63838642015-02-09 15:04:55 +0100210 .nbytes_max = __cpu_to_le32(2048),
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300211 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
212 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300213 },
214
215 /* CE2: target->host WMI */
216 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300217 .pipenum = __cpu_to_le32(2),
218 .pipedir = __cpu_to_le32(PIPEDIR_IN),
Rajkumar Manoharan30abb332015-03-04 15:43:44 +0200219 .nentries = __cpu_to_le32(64),
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300220 .nbytes_max = __cpu_to_le32(2048),
221 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
222 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300223 },
224
225 /* CE3: host->target WMI */
226 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300227 .pipenum = __cpu_to_le32(3),
228 .pipedir = __cpu_to_le32(PIPEDIR_OUT),
229 .nentries = __cpu_to_le32(32),
230 .nbytes_max = __cpu_to_le32(2048),
231 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
232 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300233 },
234
235 /* CE4: host->target HTT */
236 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300237 .pipenum = __cpu_to_le32(4),
238 .pipedir = __cpu_to_le32(PIPEDIR_OUT),
239 .nentries = __cpu_to_le32(256),
240 .nbytes_max = __cpu_to_le32(256),
241 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
242 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300243 },
244
Kalle Valo5e3dd152013-06-12 20:52:10 +0300245 /* NB: 50% of src nentries, since tx has 2 frags */
Kalle Valod88effb2013-09-01 10:01:39 +0300246
247 /* CE5: unused */
248 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300249 .pipenum = __cpu_to_le32(5),
250 .pipedir = __cpu_to_le32(PIPEDIR_OUT),
251 .nentries = __cpu_to_le32(32),
252 .nbytes_max = __cpu_to_le32(2048),
253 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
254 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300255 },
256
257 /* CE6: Reserved for target autonomous hif_memcpy */
258 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300259 .pipenum = __cpu_to_le32(6),
260 .pipedir = __cpu_to_le32(PIPEDIR_INOUT),
261 .nentries = __cpu_to_le32(32),
262 .nbytes_max = __cpu_to_le32(4096),
263 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
264 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300265 },
266
Kalle Valo5e3dd152013-06-12 20:52:10 +0300267 /* CE7 used only by Host */
Vasanthakumar Thiagarajan050af062015-06-18 12:31:04 +0530268 {
269 .pipenum = __cpu_to_le32(7),
270 .pipedir = __cpu_to_le32(PIPEDIR_INOUT),
271 .nentries = __cpu_to_le32(0),
272 .nbytes_max = __cpu_to_le32(0),
273 .flags = __cpu_to_le32(0),
274 .reserved = __cpu_to_le32(0),
275 },
276
277 /* CE8 target->host packtlog */
278 {
279 .pipenum = __cpu_to_le32(8),
280 .pipedir = __cpu_to_le32(PIPEDIR_IN),
281 .nentries = __cpu_to_le32(64),
282 .nbytes_max = __cpu_to_le32(2048),
283 .flags = __cpu_to_le32(CE_ATTR_FLAGS | CE_ATTR_DIS_INTR),
284 .reserved = __cpu_to_le32(0),
285 },
286
287 /* CE9 target autonomous qcache memcpy */
288 {
289 .pipenum = __cpu_to_le32(9),
290 .pipedir = __cpu_to_le32(PIPEDIR_INOUT),
291 .nentries = __cpu_to_le32(32),
292 .nbytes_max = __cpu_to_le32(2048),
293 .flags = __cpu_to_le32(CE_ATTR_FLAGS | CE_ATTR_DIS_INTR),
294 .reserved = __cpu_to_le32(0),
295 },
296
297 /* It not necessary to send target wlan configuration for CE10 & CE11
298 * as these CEs are not actively used in target.
299 */
Kalle Valo5e3dd152013-06-12 20:52:10 +0300300};
301
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300302/*
303 * Map from service/endpoint to Copy Engine.
304 * This table is derived from the CE_PCI TABLE, above.
305 * It is passed to the Target at startup for use by firmware.
306 */
307static const struct service_to_pipe target_service_to_ce_map_wlan[] = {
308 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300309 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VO),
310 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
311 __cpu_to_le32(3),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300312 },
313 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300314 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VO),
315 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
316 __cpu_to_le32(2),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300317 },
318 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300319 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BK),
320 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
321 __cpu_to_le32(3),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300322 },
323 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300324 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BK),
325 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
326 __cpu_to_le32(2),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300327 },
328 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300329 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BE),
330 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
331 __cpu_to_le32(3),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300332 },
333 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300334 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BE),
335 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
336 __cpu_to_le32(2),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300337 },
338 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300339 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VI),
340 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
341 __cpu_to_le32(3),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300342 },
343 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300344 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VI),
345 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
346 __cpu_to_le32(2),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300347 },
348 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300349 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_CONTROL),
350 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
351 __cpu_to_le32(3),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300352 },
353 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300354 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_CONTROL),
355 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
356 __cpu_to_le32(2),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300357 },
358 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300359 __cpu_to_le32(ATH10K_HTC_SVC_ID_RSVD_CTRL),
360 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
361 __cpu_to_le32(0),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300362 },
363 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300364 __cpu_to_le32(ATH10K_HTC_SVC_ID_RSVD_CTRL),
365 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
366 __cpu_to_le32(1),
367 },
368 { /* not used */
369 __cpu_to_le32(ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS),
370 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
371 __cpu_to_le32(0),
372 },
373 { /* not used */
374 __cpu_to_le32(ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS),
375 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
376 __cpu_to_le32(1),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300377 },
378 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300379 __cpu_to_le32(ATH10K_HTC_SVC_ID_HTT_DATA_MSG),
380 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
381 __cpu_to_le32(4),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300382 },
383 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300384 __cpu_to_le32(ATH10K_HTC_SVC_ID_HTT_DATA_MSG),
385 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
386 __cpu_to_le32(1),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300387 },
388
389 /* (Additions here) */
390
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300391 { /* must be last */
392 __cpu_to_le32(0),
393 __cpu_to_le32(0),
394 __cpu_to_le32(0),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300395 },
396};
397
Michal Kazior77258d42015-05-18 09:38:18 +0000398static bool ath10k_pci_is_awake(struct ath10k *ar)
399{
400 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
401 u32 val = ioread32(ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
402 RTC_STATE_ADDRESS);
403
404 return RTC_STATE_V_GET(val) == RTC_STATE_V_ON;
405}
406
407static void __ath10k_pci_wake(struct ath10k *ar)
408{
409 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
410
411 lockdep_assert_held(&ar_pci->ps_lock);
412
413 ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps wake reg refcount %lu awake %d\n",
414 ar_pci->ps_wake_refcount, ar_pci->ps_awake);
415
416 iowrite32(PCIE_SOC_WAKE_V_MASK,
417 ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
418 PCIE_SOC_WAKE_ADDRESS);
419}
420
421static void __ath10k_pci_sleep(struct ath10k *ar)
422{
423 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
424
425 lockdep_assert_held(&ar_pci->ps_lock);
426
427 ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps sleep reg refcount %lu awake %d\n",
428 ar_pci->ps_wake_refcount, ar_pci->ps_awake);
429
430 iowrite32(PCIE_SOC_WAKE_RESET,
431 ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
432 PCIE_SOC_WAKE_ADDRESS);
433 ar_pci->ps_awake = false;
434}
435
436static int ath10k_pci_wake_wait(struct ath10k *ar)
437{
438 int tot_delay = 0;
439 int curr_delay = 5;
440
441 while (tot_delay < PCIE_WAKE_TIMEOUT) {
442 if (ath10k_pci_is_awake(ar))
443 return 0;
444
445 udelay(curr_delay);
446 tot_delay += curr_delay;
447
448 if (curr_delay < 50)
449 curr_delay += 5;
450 }
451
452 return -ETIMEDOUT;
453}
454
455static int ath10k_pci_wake(struct ath10k *ar)
456{
457 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
458 unsigned long flags;
459 int ret = 0;
460
461 spin_lock_irqsave(&ar_pci->ps_lock, flags);
462
463 ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps wake refcount %lu awake %d\n",
464 ar_pci->ps_wake_refcount, ar_pci->ps_awake);
465
466 /* This function can be called very frequently. To avoid excessive
467 * CPU stalls for MMIO reads use a cache var to hold the device state.
468 */
469 if (!ar_pci->ps_awake) {
470 __ath10k_pci_wake(ar);
471
472 ret = ath10k_pci_wake_wait(ar);
473 if (ret == 0)
474 ar_pci->ps_awake = true;
475 }
476
477 if (ret == 0) {
478 ar_pci->ps_wake_refcount++;
479 WARN_ON(ar_pci->ps_wake_refcount == 0);
480 }
481
482 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
483
484 return ret;
485}
486
487static void ath10k_pci_sleep(struct ath10k *ar)
488{
489 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
490 unsigned long flags;
491
492 spin_lock_irqsave(&ar_pci->ps_lock, flags);
493
494 ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps sleep refcount %lu awake %d\n",
495 ar_pci->ps_wake_refcount, ar_pci->ps_awake);
496
497 if (WARN_ON(ar_pci->ps_wake_refcount == 0))
498 goto skip;
499
500 ar_pci->ps_wake_refcount--;
501
502 mod_timer(&ar_pci->ps_timer, jiffies +
503 msecs_to_jiffies(ATH10K_PCI_SLEEP_GRACE_PERIOD_MSEC));
504
505skip:
506 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
507}
508
509static void ath10k_pci_ps_timer(unsigned long ptr)
510{
511 struct ath10k *ar = (void *)ptr;
512 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
513 unsigned long flags;
514
515 spin_lock_irqsave(&ar_pci->ps_lock, flags);
516
517 ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps timer refcount %lu awake %d\n",
518 ar_pci->ps_wake_refcount, ar_pci->ps_awake);
519
520 if (ar_pci->ps_wake_refcount > 0)
521 goto skip;
522
523 __ath10k_pci_sleep(ar);
524
525skip:
526 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
527}
528
529static void ath10k_pci_sleep_sync(struct ath10k *ar)
530{
531 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
532 unsigned long flags;
533
534 del_timer_sync(&ar_pci->ps_timer);
535
536 spin_lock_irqsave(&ar_pci->ps_lock, flags);
537 WARN_ON(ar_pci->ps_wake_refcount > 0);
538 __ath10k_pci_sleep(ar);
539 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
540}
541
542void ath10k_pci_write32(struct ath10k *ar, u32 offset, u32 value)
543{
544 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
545 int ret;
546
Michal Kazioraeae5b42015-06-15 14:46:42 +0300547 if (unlikely(offset + sizeof(value) > ar_pci->mem_len)) {
548 ath10k_warn(ar, "refusing to write mmio out of bounds at 0x%08x - 0x%08zx (max 0x%08zx)\n",
549 offset, offset + sizeof(value), ar_pci->mem_len);
550 return;
551 }
552
Michal Kazior77258d42015-05-18 09:38:18 +0000553 ret = ath10k_pci_wake(ar);
554 if (ret) {
555 ath10k_warn(ar, "failed to wake target for write32 of 0x%08x at 0x%08x: %d\n",
556 value, offset, ret);
557 return;
558 }
559
560 iowrite32(value, ar_pci->mem + offset);
561 ath10k_pci_sleep(ar);
562}
563
564u32 ath10k_pci_read32(struct ath10k *ar, u32 offset)
565{
566 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
567 u32 val;
568 int ret;
569
Michal Kazioraeae5b42015-06-15 14:46:42 +0300570 if (unlikely(offset + sizeof(val) > ar_pci->mem_len)) {
571 ath10k_warn(ar, "refusing to read mmio out of bounds at 0x%08x - 0x%08zx (max 0x%08zx)\n",
572 offset, offset + sizeof(val), ar_pci->mem_len);
573 return 0;
574 }
575
Michal Kazior77258d42015-05-18 09:38:18 +0000576 ret = ath10k_pci_wake(ar);
577 if (ret) {
578 ath10k_warn(ar, "failed to wake target for read32 at 0x%08x: %d\n",
579 offset, ret);
580 return 0xffffffff;
581 }
582
583 val = ioread32(ar_pci->mem + offset);
584 ath10k_pci_sleep(ar);
585
586 return val;
587}
588
589u32 ath10k_pci_soc_read32(struct ath10k *ar, u32 addr)
590{
591 return ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS + addr);
592}
593
594void ath10k_pci_soc_write32(struct ath10k *ar, u32 addr, u32 val)
595{
596 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + addr, val);
597}
598
599u32 ath10k_pci_reg_read32(struct ath10k *ar, u32 addr)
600{
601 return ath10k_pci_read32(ar, PCIE_LOCAL_BASE_ADDRESS + addr);
602}
603
604void ath10k_pci_reg_write32(struct ath10k *ar, u32 addr, u32 val)
605{
606 ath10k_pci_write32(ar, PCIE_LOCAL_BASE_ADDRESS + addr, val);
607}
608
Michal Kaziore5398872013-11-25 14:06:20 +0100609static bool ath10k_pci_irq_pending(struct ath10k *ar)
610{
611 u32 cause;
612
613 /* Check if the shared legacy irq is for us */
614 cause = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
615 PCIE_INTR_CAUSE_ADDRESS);
616 if (cause & (PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL))
617 return true;
618
619 return false;
620}
621
Michal Kazior26852182013-11-25 14:06:25 +0100622static void ath10k_pci_disable_and_clear_legacy_irq(struct ath10k *ar)
623{
624 /* IMPORTANT: INTR_CLR register has to be set after
625 * INTR_ENABLE is set to 0, otherwise interrupt can not be
626 * really cleared. */
627 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
628 0);
629 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_CLR_ADDRESS,
630 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
631
632 /* IMPORTANT: this extra read transaction is required to
633 * flush the posted write buffer. */
Kalle Valocfbc06a2014-09-14 12:50:23 +0300634 (void)ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
635 PCIE_INTR_ENABLE_ADDRESS);
Michal Kazior26852182013-11-25 14:06:25 +0100636}
637
638static void ath10k_pci_enable_legacy_irq(struct ath10k *ar)
639{
640 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
641 PCIE_INTR_ENABLE_ADDRESS,
642 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
643
644 /* IMPORTANT: this extra read transaction is required to
645 * flush the posted write buffer. */
Kalle Valocfbc06a2014-09-14 12:50:23 +0300646 (void)ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
647 PCIE_INTR_ENABLE_ADDRESS);
Michal Kazior26852182013-11-25 14:06:25 +0100648}
649
Michal Kazior403d6272014-08-22 14:23:31 +0200650static inline const char *ath10k_pci_get_irq_method(struct ath10k *ar)
Michal Kaziorab977bd2013-11-25 14:06:26 +0100651{
Michal Kaziorab977bd2013-11-25 14:06:26 +0100652 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
653
Michal Kazior403d6272014-08-22 14:23:31 +0200654 if (ar_pci->num_msi_intrs > 1)
655 return "msi-x";
Kalle Valod8bb26b2014-09-14 12:50:33 +0300656
657 if (ar_pci->num_msi_intrs == 1)
Michal Kazior403d6272014-08-22 14:23:31 +0200658 return "msi";
Kalle Valod8bb26b2014-09-14 12:50:33 +0300659
660 return "legacy";
Michal Kaziorab977bd2013-11-25 14:06:26 +0100661}
662
Michal Kazior728f95e2014-08-22 14:33:14 +0200663static int __ath10k_pci_rx_post_buf(struct ath10k_pci_pipe *pipe)
Michal Kaziorab977bd2013-11-25 14:06:26 +0100664{
Michal Kazior728f95e2014-08-22 14:33:14 +0200665 struct ath10k *ar = pipe->hif_ce_state;
Michal Kaziorab977bd2013-11-25 14:06:26 +0100666 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior728f95e2014-08-22 14:33:14 +0200667 struct ath10k_ce_pipe *ce_pipe = pipe->ce_hdl;
668 struct sk_buff *skb;
669 dma_addr_t paddr;
Michal Kaziorab977bd2013-11-25 14:06:26 +0100670 int ret;
671
Michal Kazior728f95e2014-08-22 14:33:14 +0200672 lockdep_assert_held(&ar_pci->ce_lock);
673
674 skb = dev_alloc_skb(pipe->buf_sz);
675 if (!skb)
676 return -ENOMEM;
677
678 WARN_ONCE((unsigned long)skb->data & 3, "unaligned skb");
679
680 paddr = dma_map_single(ar->dev, skb->data,
681 skb->len + skb_tailroom(skb),
682 DMA_FROM_DEVICE);
683 if (unlikely(dma_mapping_error(ar->dev, paddr))) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200684 ath10k_warn(ar, "failed to dma map pci rx buf\n");
Michal Kazior728f95e2014-08-22 14:33:14 +0200685 dev_kfree_skb_any(skb);
686 return -EIO;
687 }
688
Michal Kazior8582bf32015-01-24 12:14:47 +0200689 ATH10K_SKB_RXCB(skb)->paddr = paddr;
Michal Kazior728f95e2014-08-22 14:33:14 +0200690
691 ret = __ath10k_ce_rx_post_buf(ce_pipe, skb, paddr);
Michal Kaziorab977bd2013-11-25 14:06:26 +0100692 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200693 ath10k_warn(ar, "failed to post pci rx buf: %d\n", ret);
Michal Kazior728f95e2014-08-22 14:33:14 +0200694 dma_unmap_single(ar->dev, paddr, skb->len + skb_tailroom(skb),
695 DMA_FROM_DEVICE);
696 dev_kfree_skb_any(skb);
Michal Kaziorab977bd2013-11-25 14:06:26 +0100697 return ret;
698 }
699
700 return 0;
701}
702
Michal Kazior728f95e2014-08-22 14:33:14 +0200703static void __ath10k_pci_rx_post_pipe(struct ath10k_pci_pipe *pipe)
Michal Kaziorab977bd2013-11-25 14:06:26 +0100704{
Michal Kazior728f95e2014-08-22 14:33:14 +0200705 struct ath10k *ar = pipe->hif_ce_state;
706 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
707 struct ath10k_ce_pipe *ce_pipe = pipe->ce_hdl;
708 int ret, num;
709
710 lockdep_assert_held(&ar_pci->ce_lock);
711
712 if (pipe->buf_sz == 0)
713 return;
714
715 if (!ce_pipe->dest_ring)
716 return;
717
718 num = __ath10k_ce_rx_num_free_bufs(ce_pipe);
719 while (num--) {
720 ret = __ath10k_pci_rx_post_buf(pipe);
721 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200722 ath10k_warn(ar, "failed to post pci rx buf: %d\n", ret);
Michal Kazior728f95e2014-08-22 14:33:14 +0200723 mod_timer(&ar_pci->rx_post_retry, jiffies +
724 ATH10K_PCI_RX_POST_RETRY_MS);
725 break;
726 }
727 }
728}
729
730static void ath10k_pci_rx_post_pipe(struct ath10k_pci_pipe *pipe)
731{
732 struct ath10k *ar = pipe->hif_ce_state;
733 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
734
735 spin_lock_bh(&ar_pci->ce_lock);
736 __ath10k_pci_rx_post_pipe(pipe);
737 spin_unlock_bh(&ar_pci->ce_lock);
738}
739
740static void ath10k_pci_rx_post(struct ath10k *ar)
741{
742 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
743 int i;
744
745 spin_lock_bh(&ar_pci->ce_lock);
746 for (i = 0; i < CE_COUNT; i++)
747 __ath10k_pci_rx_post_pipe(&ar_pci->pipe_info[i]);
748 spin_unlock_bh(&ar_pci->ce_lock);
749}
750
751static void ath10k_pci_rx_replenish_retry(unsigned long ptr)
752{
753 struct ath10k *ar = (void *)ptr;
754
755 ath10k_pci_rx_post(ar);
Michal Kaziorab977bd2013-11-25 14:06:26 +0100756}
757
Vasanthakumar Thiagarajan418ca592015-06-18 12:31:05 +0530758static u32 ath10k_pci_targ_cpu_to_ce_addr(struct ath10k *ar, u32 addr)
759{
760 u32 val = 0;
761
762 switch (ar->hw_rev) {
763 case ATH10K_HW_QCA988X:
764 case ATH10K_HW_QCA6174:
765 val = (ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
766 CORE_CTRL_ADDRESS) &
767 0x7fff) << 21;
768 break;
769 case ATH10K_HW_QCA99X0:
770 val = ath10k_pci_read32(ar, PCIE_BAR_REG_ADDRESS);
771 break;
772 }
773
774 val |= 0x100000 | (addr & 0xfffff);
775 return val;
776}
777
Kalle Valo5e3dd152013-06-12 20:52:10 +0300778/*
779 * Diagnostic read/write access is provided for startup/config/debug usage.
780 * Caller must guarantee proper alignment, when applicable, and single user
781 * at any moment.
782 */
783static int ath10k_pci_diag_read_mem(struct ath10k *ar, u32 address, void *data,
784 int nbytes)
785{
786 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
787 int ret = 0;
788 u32 buf;
789 unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
790 unsigned int id;
791 unsigned int flags;
Michal Kazior2aa39112013-08-27 13:08:02 +0200792 struct ath10k_ce_pipe *ce_diag;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300793 /* Host buffer address in CE space */
794 u32 ce_data;
795 dma_addr_t ce_data_base = 0;
796 void *data_buf = NULL;
797 int i;
798
Kalle Valoeef25402014-09-24 14:16:52 +0300799 spin_lock_bh(&ar_pci->ce_lock);
800
Kalle Valo5e3dd152013-06-12 20:52:10 +0300801 ce_diag = ar_pci->ce_diag;
802
803 /*
804 * Allocate a temporary bounce buffer to hold caller's data
805 * to be DMA'ed from Target. This guarantees
806 * 1) 4-byte alignment
807 * 2) Buffer in DMA-able space
808 */
809 orig_nbytes = nbytes;
Michal Kazior68c03242014-03-28 10:02:35 +0200810 data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
811 orig_nbytes,
812 &ce_data_base,
813 GFP_ATOMIC);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300814
815 if (!data_buf) {
816 ret = -ENOMEM;
817 goto done;
818 }
819 memset(data_buf, 0, orig_nbytes);
820
821 remaining_bytes = orig_nbytes;
822 ce_data = ce_data_base;
823 while (remaining_bytes) {
824 nbytes = min_t(unsigned int, remaining_bytes,
825 DIAG_TRANSFER_LIMIT);
826
Kalle Valoeef25402014-09-24 14:16:52 +0300827 ret = __ath10k_ce_rx_post_buf(ce_diag, NULL, ce_data);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300828 if (ret != 0)
829 goto done;
830
831 /* Request CE to send from Target(!) address to Host buffer */
832 /*
833 * The address supplied by the caller is in the
834 * Target CPU virtual address space.
835 *
836 * In order to use this address with the diagnostic CE,
837 * convert it from Target CPU virtual address space
838 * to CE address space
839 */
Vasanthakumar Thiagarajan418ca592015-06-18 12:31:05 +0530840 address = ath10k_pci_targ_cpu_to_ce_addr(ar, address);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300841
Kalle Valoeef25402014-09-24 14:16:52 +0300842 ret = ath10k_ce_send_nolock(ce_diag, NULL, (u32)address, nbytes, 0,
843 0);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300844 if (ret)
845 goto done;
846
847 i = 0;
Kalle Valoeef25402014-09-24 14:16:52 +0300848 while (ath10k_ce_completed_send_next_nolock(ce_diag, NULL, &buf,
849 &completed_nbytes,
850 &id) != 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300851 mdelay(1);
852 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
853 ret = -EBUSY;
854 goto done;
855 }
856 }
857
858 if (nbytes != completed_nbytes) {
859 ret = -EIO;
860 goto done;
861 }
862
Kalle Valocfbc06a2014-09-14 12:50:23 +0300863 if (buf != (u32)address) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300864 ret = -EIO;
865 goto done;
866 }
867
868 i = 0;
Kalle Valoeef25402014-09-24 14:16:52 +0300869 while (ath10k_ce_completed_recv_next_nolock(ce_diag, NULL, &buf,
870 &completed_nbytes,
871 &id, &flags) != 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300872 mdelay(1);
873
874 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
875 ret = -EBUSY;
876 goto done;
877 }
878 }
879
880 if (nbytes != completed_nbytes) {
881 ret = -EIO;
882 goto done;
883 }
884
885 if (buf != ce_data) {
886 ret = -EIO;
887 goto done;
888 }
889
890 remaining_bytes -= nbytes;
891 address += nbytes;
892 ce_data += nbytes;
893 }
894
895done:
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300896 if (ret == 0)
897 memcpy(data, data_buf, orig_nbytes);
898 else
Michal Kazior7aa7a722014-08-25 12:09:38 +0200899 ath10k_warn(ar, "failed to read diag value at 0x%x: %d\n",
Kalle Valo50f87a62014-03-28 09:32:52 +0200900 address, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300901
902 if (data_buf)
Michal Kazior68c03242014-03-28 10:02:35 +0200903 dma_free_coherent(ar->dev, orig_nbytes, data_buf,
904 ce_data_base);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300905
Kalle Valoeef25402014-09-24 14:16:52 +0300906 spin_unlock_bh(&ar_pci->ce_lock);
907
Kalle Valo5e3dd152013-06-12 20:52:10 +0300908 return ret;
909}
910
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300911static int ath10k_pci_diag_read32(struct ath10k *ar, u32 address, u32 *value)
912{
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300913 __le32 val = 0;
914 int ret;
915
916 ret = ath10k_pci_diag_read_mem(ar, address, &val, sizeof(val));
917 *value = __le32_to_cpu(val);
918
919 return ret;
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300920}
921
922static int __ath10k_pci_diag_read_hi(struct ath10k *ar, void *dest,
923 u32 src, u32 len)
924{
925 u32 host_addr, addr;
926 int ret;
927
928 host_addr = host_interest_item_address(src);
929
930 ret = ath10k_pci_diag_read32(ar, host_addr, &addr);
931 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200932 ath10k_warn(ar, "failed to get memcpy hi address for firmware address %d: %d\n",
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300933 src, ret);
934 return ret;
935 }
936
937 ret = ath10k_pci_diag_read_mem(ar, addr, dest, len);
938 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200939 ath10k_warn(ar, "failed to memcpy firmware memory from %d (%d B): %d\n",
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300940 addr, len, ret);
941 return ret;
942 }
943
944 return 0;
945}
946
947#define ath10k_pci_diag_read_hi(ar, dest, src, len) \
Kalle Valo8cc7f262014-09-14 12:50:39 +0300948 __ath10k_pci_diag_read_hi(ar, dest, HI_ITEM(src), len)
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300949
Kalle Valo5e3dd152013-06-12 20:52:10 +0300950static int ath10k_pci_diag_write_mem(struct ath10k *ar, u32 address,
951 const void *data, int nbytes)
952{
953 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
954 int ret = 0;
955 u32 buf;
956 unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
957 unsigned int id;
958 unsigned int flags;
Michal Kazior2aa39112013-08-27 13:08:02 +0200959 struct ath10k_ce_pipe *ce_diag;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300960 void *data_buf = NULL;
961 u32 ce_data; /* Host buffer address in CE space */
962 dma_addr_t ce_data_base = 0;
963 int i;
964
Kalle Valoeef25402014-09-24 14:16:52 +0300965 spin_lock_bh(&ar_pci->ce_lock);
966
Kalle Valo5e3dd152013-06-12 20:52:10 +0300967 ce_diag = ar_pci->ce_diag;
968
969 /*
970 * Allocate a temporary bounce buffer to hold caller's data
971 * to be DMA'ed to Target. This guarantees
972 * 1) 4-byte alignment
973 * 2) Buffer in DMA-able space
974 */
975 orig_nbytes = nbytes;
Michal Kazior68c03242014-03-28 10:02:35 +0200976 data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
977 orig_nbytes,
978 &ce_data_base,
979 GFP_ATOMIC);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300980 if (!data_buf) {
981 ret = -ENOMEM;
982 goto done;
983 }
984
985 /* Copy caller's data to allocated DMA buf */
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300986 memcpy(data_buf, data, orig_nbytes);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300987
988 /*
989 * The address supplied by the caller is in the
990 * Target CPU virtual address space.
991 *
992 * In order to use this address with the diagnostic CE,
993 * convert it from
994 * Target CPU virtual address space
995 * to
996 * CE address space
997 */
Vasanthakumar Thiagarajan418ca592015-06-18 12:31:05 +0530998 address = ath10k_pci_targ_cpu_to_ce_addr(ar, address);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300999
1000 remaining_bytes = orig_nbytes;
1001 ce_data = ce_data_base;
1002 while (remaining_bytes) {
1003 /* FIXME: check cast */
1004 nbytes = min_t(int, remaining_bytes, DIAG_TRANSFER_LIMIT);
1005
1006 /* Set up to receive directly into Target(!) address */
Kalle Valoeef25402014-09-24 14:16:52 +03001007 ret = __ath10k_ce_rx_post_buf(ce_diag, NULL, address);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001008 if (ret != 0)
1009 goto done;
1010
1011 /*
1012 * Request CE to send caller-supplied data that
1013 * was copied to bounce buffer to Target(!) address.
1014 */
Kalle Valoeef25402014-09-24 14:16:52 +03001015 ret = ath10k_ce_send_nolock(ce_diag, NULL, (u32)ce_data,
1016 nbytes, 0, 0);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001017 if (ret != 0)
1018 goto done;
1019
1020 i = 0;
Kalle Valoeef25402014-09-24 14:16:52 +03001021 while (ath10k_ce_completed_send_next_nolock(ce_diag, NULL, &buf,
1022 &completed_nbytes,
1023 &id) != 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001024 mdelay(1);
1025
1026 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
1027 ret = -EBUSY;
1028 goto done;
1029 }
1030 }
1031
1032 if (nbytes != completed_nbytes) {
1033 ret = -EIO;
1034 goto done;
1035 }
1036
1037 if (buf != ce_data) {
1038 ret = -EIO;
1039 goto done;
1040 }
1041
1042 i = 0;
Kalle Valoeef25402014-09-24 14:16:52 +03001043 while (ath10k_ce_completed_recv_next_nolock(ce_diag, NULL, &buf,
1044 &completed_nbytes,
1045 &id, &flags) != 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001046 mdelay(1);
1047
1048 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
1049 ret = -EBUSY;
1050 goto done;
1051 }
1052 }
1053
1054 if (nbytes != completed_nbytes) {
1055 ret = -EIO;
1056 goto done;
1057 }
1058
1059 if (buf != address) {
1060 ret = -EIO;
1061 goto done;
1062 }
1063
1064 remaining_bytes -= nbytes;
1065 address += nbytes;
1066 ce_data += nbytes;
1067 }
1068
1069done:
1070 if (data_buf) {
Michal Kazior68c03242014-03-28 10:02:35 +02001071 dma_free_coherent(ar->dev, orig_nbytes, data_buf,
1072 ce_data_base);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001073 }
1074
1075 if (ret != 0)
Michal Kazior7aa7a722014-08-25 12:09:38 +02001076 ath10k_warn(ar, "failed to write diag value at 0x%x: %d\n",
Kalle Valo50f87a62014-03-28 09:32:52 +02001077 address, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001078
Kalle Valoeef25402014-09-24 14:16:52 +03001079 spin_unlock_bh(&ar_pci->ce_lock);
1080
Kalle Valo5e3dd152013-06-12 20:52:10 +03001081 return ret;
1082}
1083
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001084static int ath10k_pci_diag_write32(struct ath10k *ar, u32 address, u32 value)
1085{
1086 __le32 val = __cpu_to_le32(value);
1087
1088 return ath10k_pci_diag_write_mem(ar, address, &val, sizeof(val));
1089}
1090
Kalle Valo5e3dd152013-06-12 20:52:10 +03001091/* Called by lower (CE) layer when a send to Target completes. */
Michal Kazior5440ce22013-09-03 15:09:58 +02001092static void ath10k_pci_ce_send_done(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001093{
1094 struct ath10k *ar = ce_state->ar;
1095 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2f5280d2014-02-27 18:50:05 +02001096 struct ath10k_hif_cb *cb = &ar_pci->msg_callbacks_current;
Michal Kazior1cb86d42014-11-27 11:09:38 +01001097 struct sk_buff_head list;
1098 struct sk_buff *skb;
Michal Kazior5440ce22013-09-03 15:09:58 +02001099 u32 ce_data;
1100 unsigned int nbytes;
1101 unsigned int transfer_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001102
Michal Kazior1cb86d42014-11-27 11:09:38 +01001103 __skb_queue_head_init(&list);
1104 while (ath10k_ce_completed_send_next(ce_state, (void **)&skb, &ce_data,
1105 &nbytes, &transfer_id) == 0) {
Michal Kaziora16942e2014-02-27 18:50:04 +02001106 /* no need to call tx completion for NULL pointers */
Michal Kazior1cb86d42014-11-27 11:09:38 +01001107 if (skb == NULL)
Michal Kazior726346f2014-02-27 18:50:04 +02001108 continue;
1109
Michal Kazior1cb86d42014-11-27 11:09:38 +01001110 __skb_queue_tail(&list, skb);
Michal Kazior5440ce22013-09-03 15:09:58 +02001111 }
Michal Kazior1cb86d42014-11-27 11:09:38 +01001112
1113 while ((skb = __skb_dequeue(&list)))
1114 cb->tx_completion(ar, skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001115}
1116
1117/* Called by lower (CE) layer when data is received from the Target. */
Michal Kazior5440ce22013-09-03 15:09:58 +02001118static void ath10k_pci_ce_recv_data(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001119{
1120 struct ath10k *ar = ce_state->ar;
1121 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +02001122 struct ath10k_pci_pipe *pipe_info = &ar_pci->pipe_info[ce_state->id];
Michal Kazior2f5280d2014-02-27 18:50:05 +02001123 struct ath10k_hif_cb *cb = &ar_pci->msg_callbacks_current;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001124 struct sk_buff *skb;
Michal Kazior1cb86d42014-11-27 11:09:38 +01001125 struct sk_buff_head list;
Michal Kazior5440ce22013-09-03 15:09:58 +02001126 void *transfer_context;
1127 u32 ce_data;
Michal Kazior2f5280d2014-02-27 18:50:05 +02001128 unsigned int nbytes, max_nbytes;
Michal Kazior5440ce22013-09-03 15:09:58 +02001129 unsigned int transfer_id;
1130 unsigned int flags;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001131
Michal Kazior1cb86d42014-11-27 11:09:38 +01001132 __skb_queue_head_init(&list);
Michal Kazior5440ce22013-09-03 15:09:58 +02001133 while (ath10k_ce_completed_recv_next(ce_state, &transfer_context,
1134 &ce_data, &nbytes, &transfer_id,
1135 &flags) == 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001136 skb = transfer_context;
Michal Kazior2f5280d2014-02-27 18:50:05 +02001137 max_nbytes = skb->len + skb_tailroom(skb);
Michal Kazior8582bf32015-01-24 12:14:47 +02001138 dma_unmap_single(ar->dev, ATH10K_SKB_RXCB(skb)->paddr,
Michal Kazior2f5280d2014-02-27 18:50:05 +02001139 max_nbytes, DMA_FROM_DEVICE);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001140
Michal Kazior2f5280d2014-02-27 18:50:05 +02001141 if (unlikely(max_nbytes < nbytes)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001142 ath10k_warn(ar, "rxed more than expected (nbytes %d, max %d)",
Michal Kazior2f5280d2014-02-27 18:50:05 +02001143 nbytes, max_nbytes);
1144 dev_kfree_skb_any(skb);
1145 continue;
1146 }
1147
1148 skb_put(skb, nbytes);
Michal Kazior1cb86d42014-11-27 11:09:38 +01001149 __skb_queue_tail(&list, skb);
1150 }
Michal Kaziora360e542014-09-23 10:22:54 +02001151
Michal Kazior1cb86d42014-11-27 11:09:38 +01001152 while ((skb = __skb_dequeue(&list))) {
Michal Kaziora360e542014-09-23 10:22:54 +02001153 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci rx ce pipe %d len %d\n",
1154 ce_state->id, skb->len);
1155 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci rx: ",
1156 skb->data, skb->len);
1157
Michal Kazior5f07ea42014-11-27 11:09:36 +01001158 cb->rx_completion(ar, skb);
Michal Kazior2f5280d2014-02-27 18:50:05 +02001159 }
Michal Kaziorc29a3802014-07-21 21:03:10 +03001160
Michal Kazior728f95e2014-08-22 14:33:14 +02001161 ath10k_pci_rx_post_pipe(pipe_info);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001162}
1163
Michal Kazior726346f2014-02-27 18:50:04 +02001164static int ath10k_pci_hif_tx_sg(struct ath10k *ar, u8 pipe_id,
1165 struct ath10k_hif_sg_item *items, int n_items)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001166{
Kalle Valo5e3dd152013-06-12 20:52:10 +03001167 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior726346f2014-02-27 18:50:04 +02001168 struct ath10k_pci_pipe *pci_pipe = &ar_pci->pipe_info[pipe_id];
1169 struct ath10k_ce_pipe *ce_pipe = pci_pipe->ce_hdl;
1170 struct ath10k_ce_ring *src_ring = ce_pipe->src_ring;
Michal Kazior7147a132014-05-26 12:02:58 +02001171 unsigned int nentries_mask;
1172 unsigned int sw_index;
1173 unsigned int write_index;
Michal Kazior08b8aa02014-05-26 12:02:59 +02001174 int err, i = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001175
Michal Kazior726346f2014-02-27 18:50:04 +02001176 spin_lock_bh(&ar_pci->ce_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001177
Michal Kazior7147a132014-05-26 12:02:58 +02001178 nentries_mask = src_ring->nentries_mask;
1179 sw_index = src_ring->sw_index;
1180 write_index = src_ring->write_index;
1181
Michal Kazior726346f2014-02-27 18:50:04 +02001182 if (unlikely(CE_RING_DELTA(nentries_mask,
1183 write_index, sw_index - 1) < n_items)) {
1184 err = -ENOBUFS;
Michal Kazior08b8aa02014-05-26 12:02:59 +02001185 goto err;
Michal Kazior726346f2014-02-27 18:50:04 +02001186 }
1187
1188 for (i = 0; i < n_items - 1; i++) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001189 ath10k_dbg(ar, ATH10K_DBG_PCI,
Michal Kazior726346f2014-02-27 18:50:04 +02001190 "pci tx item %d paddr 0x%08x len %d n_items %d\n",
1191 i, items[i].paddr, items[i].len, n_items);
Michal Kazior7aa7a722014-08-25 12:09:38 +02001192 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci tx data: ",
Michal Kazior726346f2014-02-27 18:50:04 +02001193 items[i].vaddr, items[i].len);
1194
1195 err = ath10k_ce_send_nolock(ce_pipe,
1196 items[i].transfer_context,
1197 items[i].paddr,
1198 items[i].len,
1199 items[i].transfer_id,
1200 CE_SEND_FLAG_GATHER);
1201 if (err)
Michal Kazior08b8aa02014-05-26 12:02:59 +02001202 goto err;
Michal Kazior726346f2014-02-27 18:50:04 +02001203 }
1204
1205 /* `i` is equal to `n_items -1` after for() */
Kalle Valo5e3dd152013-06-12 20:52:10 +03001206
Michal Kazior7aa7a722014-08-25 12:09:38 +02001207 ath10k_dbg(ar, ATH10K_DBG_PCI,
Michal Kazior726346f2014-02-27 18:50:04 +02001208 "pci tx item %d paddr 0x%08x len %d n_items %d\n",
1209 i, items[i].paddr, items[i].len, n_items);
Michal Kazior7aa7a722014-08-25 12:09:38 +02001210 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci tx data: ",
Michal Kazior726346f2014-02-27 18:50:04 +02001211 items[i].vaddr, items[i].len);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001212
Michal Kazior726346f2014-02-27 18:50:04 +02001213 err = ath10k_ce_send_nolock(ce_pipe,
1214 items[i].transfer_context,
1215 items[i].paddr,
1216 items[i].len,
1217 items[i].transfer_id,
1218 0);
1219 if (err)
Michal Kazior08b8aa02014-05-26 12:02:59 +02001220 goto err;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001221
Michal Kazior08b8aa02014-05-26 12:02:59 +02001222 spin_unlock_bh(&ar_pci->ce_lock);
1223 return 0;
1224
1225err:
1226 for (; i > 0; i--)
1227 __ath10k_ce_send_revert(ce_pipe);
1228
Michal Kazior726346f2014-02-27 18:50:04 +02001229 spin_unlock_bh(&ar_pci->ce_lock);
1230 return err;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001231}
1232
Kalle Valoeef25402014-09-24 14:16:52 +03001233static int ath10k_pci_hif_diag_read(struct ath10k *ar, u32 address, void *buf,
1234 size_t buf_len)
1235{
1236 return ath10k_pci_diag_read_mem(ar, address, buf, buf_len);
1237}
1238
Kalle Valo5e3dd152013-06-12 20:52:10 +03001239static u16 ath10k_pci_hif_get_free_queue_number(struct ath10k *ar, u8 pipe)
1240{
1241 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo50f87a62014-03-28 09:32:52 +02001242
Michal Kazior7aa7a722014-08-25 12:09:38 +02001243 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif get free queue number\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001244
Michal Kazior3efcb3b2013-10-02 11:03:41 +02001245 return ath10k_ce_num_free_src_entries(ar_pci->pipe_info[pipe].ce_hdl);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001246}
1247
Ben Greear384914b2014-08-25 08:37:32 +03001248static void ath10k_pci_dump_registers(struct ath10k *ar,
1249 struct ath10k_fw_crash_data *crash_data)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001250{
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001251 __le32 reg_dump_values[REG_DUMP_COUNT_QCA988X] = {};
1252 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001253
Ben Greear384914b2014-08-25 08:37:32 +03001254 lockdep_assert_held(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001255
Kalle Valo3d29a3e2014-08-25 08:37:26 +03001256 ret = ath10k_pci_diag_read_hi(ar, &reg_dump_values[0],
1257 hi_failure_state,
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001258 REG_DUMP_COUNT_QCA988X * sizeof(__le32));
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001259 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001260 ath10k_err(ar, "failed to read firmware dump area: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001261 return;
1262 }
1263
1264 BUILD_BUG_ON(REG_DUMP_COUNT_QCA988X % 4);
1265
Michal Kazior7aa7a722014-08-25 12:09:38 +02001266 ath10k_err(ar, "firmware register dump:\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001267 for (i = 0; i < REG_DUMP_COUNT_QCA988X; i += 4)
Michal Kazior7aa7a722014-08-25 12:09:38 +02001268 ath10k_err(ar, "[%02d]: 0x%08X 0x%08X 0x%08X 0x%08X\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001269 i,
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001270 __le32_to_cpu(reg_dump_values[i]),
1271 __le32_to_cpu(reg_dump_values[i + 1]),
1272 __le32_to_cpu(reg_dump_values[i + 2]),
1273 __le32_to_cpu(reg_dump_values[i + 3]));
Michal Kazioraffd3212013-07-16 09:54:35 +02001274
Michal Kazior1bbb1192014-08-25 12:13:14 +02001275 if (!crash_data)
1276 return;
1277
Ben Greear384914b2014-08-25 08:37:32 +03001278 for (i = 0; i < REG_DUMP_COUNT_QCA988X; i++)
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001279 crash_data->registers[i] = reg_dump_values[i];
Ben Greear384914b2014-08-25 08:37:32 +03001280}
1281
Kalle Valo0e9848c2014-08-25 08:37:37 +03001282static void ath10k_pci_fw_crashed_dump(struct ath10k *ar)
Ben Greear384914b2014-08-25 08:37:32 +03001283{
1284 struct ath10k_fw_crash_data *crash_data;
1285 char uuid[50];
1286
1287 spin_lock_bh(&ar->data_lock);
1288
Ben Greearf51dbe72014-09-29 14:41:46 +03001289 ar->stats.fw_crash_counter++;
1290
Ben Greear384914b2014-08-25 08:37:32 +03001291 crash_data = ath10k_debug_get_new_fw_crash_data(ar);
1292
1293 if (crash_data)
1294 scnprintf(uuid, sizeof(uuid), "%pUl", &crash_data->uuid);
1295 else
1296 scnprintf(uuid, sizeof(uuid), "n/a");
1297
Michal Kazior7aa7a722014-08-25 12:09:38 +02001298 ath10k_err(ar, "firmware crashed! (uuid %s)\n", uuid);
Kalle Valo8a0c7972014-08-25 08:37:45 +03001299 ath10k_print_driver_info(ar);
Ben Greear384914b2014-08-25 08:37:32 +03001300 ath10k_pci_dump_registers(ar, crash_data);
1301
Ben Greear384914b2014-08-25 08:37:32 +03001302 spin_unlock_bh(&ar->data_lock);
Michal Kazioraffd3212013-07-16 09:54:35 +02001303
Michal Kazior5e90de82013-10-16 16:46:05 +03001304 queue_work(ar->workqueue, &ar->restart_work);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001305}
1306
1307static void ath10k_pci_hif_send_complete_check(struct ath10k *ar, u8 pipe,
1308 int force)
1309{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001310 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif send complete check\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001311
Kalle Valo5e3dd152013-06-12 20:52:10 +03001312 if (!force) {
1313 int resources;
1314 /*
1315 * Decide whether to actually poll for completions, or just
1316 * wait for a later chance.
1317 * If there seem to be plenty of resources left, then just wait
1318 * since checking involves reading a CE register, which is a
1319 * relatively expensive operation.
1320 */
1321 resources = ath10k_pci_hif_get_free_queue_number(ar, pipe);
1322
1323 /*
1324 * If at least 50% of the total resources are still available,
1325 * don't bother checking again yet.
1326 */
1327 if (resources > (host_ce_config_wlan[pipe].src_nentries >> 1))
1328 return;
1329 }
1330 ath10k_ce_per_engine_service(ar, pipe);
1331}
1332
Michal Kaziore799bbf2013-07-05 16:15:12 +03001333static void ath10k_pci_hif_set_callbacks(struct ath10k *ar,
1334 struct ath10k_hif_cb *callbacks)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001335{
1336 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1337
Michal Kazior7aa7a722014-08-25 12:09:38 +02001338 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif set callbacks\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001339
1340 memcpy(&ar_pci->msg_callbacks_current, callbacks,
1341 sizeof(ar_pci->msg_callbacks_current));
1342}
1343
Michal Kazior96a9d0d2013-11-08 08:01:25 +01001344static void ath10k_pci_kill_tasklet(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001345{
1346 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001347 int i;
1348
Kalle Valo5e3dd152013-06-12 20:52:10 +03001349 tasklet_kill(&ar_pci->intr_tq);
Michal Kazior103d4f52013-11-08 08:01:24 +01001350 tasklet_kill(&ar_pci->msi_fw_err);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001351
1352 for (i = 0; i < CE_COUNT; i++)
1353 tasklet_kill(&ar_pci->pipe_info[i].intr);
Michal Kazior728f95e2014-08-22 14:33:14 +02001354
1355 del_timer_sync(&ar_pci->rx_post_retry);
Michal Kazior96a9d0d2013-11-08 08:01:25 +01001356}
1357
Kalle Valo5e3dd152013-06-12 20:52:10 +03001358static int ath10k_pci_hif_map_service_to_pipe(struct ath10k *ar,
1359 u16 service_id, u8 *ul_pipe,
1360 u8 *dl_pipe, int *ul_is_polled,
1361 int *dl_is_polled)
1362{
Michal Kazior7c6aa252014-08-26 19:14:03 +03001363 const struct service_to_pipe *entry;
1364 bool ul_set = false, dl_set = false;
1365 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001366
Michal Kazior7aa7a722014-08-25 12:09:38 +02001367 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif map service\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001368
Kalle Valo5e3dd152013-06-12 20:52:10 +03001369 /* polling for received messages not supported */
1370 *dl_is_polled = 0;
1371
Michal Kazior7c6aa252014-08-26 19:14:03 +03001372 for (i = 0; i < ARRAY_SIZE(target_service_to_ce_map_wlan); i++) {
1373 entry = &target_service_to_ce_map_wlan[i];
Kalle Valo5e3dd152013-06-12 20:52:10 +03001374
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001375 if (__le32_to_cpu(entry->service_id) != service_id)
Michal Kazior7c6aa252014-08-26 19:14:03 +03001376 continue;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001377
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001378 switch (__le32_to_cpu(entry->pipedir)) {
Michal Kazior7c6aa252014-08-26 19:14:03 +03001379 case PIPEDIR_NONE:
1380 break;
1381 case PIPEDIR_IN:
1382 WARN_ON(dl_set);
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001383 *dl_pipe = __le32_to_cpu(entry->pipenum);
Michal Kazior7c6aa252014-08-26 19:14:03 +03001384 dl_set = true;
1385 break;
1386 case PIPEDIR_OUT:
1387 WARN_ON(ul_set);
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001388 *ul_pipe = __le32_to_cpu(entry->pipenum);
Michal Kazior7c6aa252014-08-26 19:14:03 +03001389 ul_set = true;
1390 break;
1391 case PIPEDIR_INOUT:
1392 WARN_ON(dl_set);
1393 WARN_ON(ul_set);
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001394 *dl_pipe = __le32_to_cpu(entry->pipenum);
1395 *ul_pipe = __le32_to_cpu(entry->pipenum);
Michal Kazior7c6aa252014-08-26 19:14:03 +03001396 dl_set = true;
1397 ul_set = true;
1398 break;
1399 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001400 }
Michal Kazior7c6aa252014-08-26 19:14:03 +03001401
1402 if (WARN_ON(!ul_set || !dl_set))
1403 return -ENOENT;
1404
Kalle Valo5e3dd152013-06-12 20:52:10 +03001405 *ul_is_polled =
1406 (host_ce_config_wlan[*ul_pipe].flags & CE_ATTR_DIS_INTR) != 0;
1407
Michal Kazior7c6aa252014-08-26 19:14:03 +03001408 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001409}
1410
1411static void ath10k_pci_hif_get_default_pipe(struct ath10k *ar,
Kalle Valo5b07e072014-09-14 12:50:06 +03001412 u8 *ul_pipe, u8 *dl_pipe)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001413{
1414 int ul_is_polled, dl_is_polled;
1415
Michal Kazior7aa7a722014-08-25 12:09:38 +02001416 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif get default pipe\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001417
Kalle Valo5e3dd152013-06-12 20:52:10 +03001418 (void)ath10k_pci_hif_map_service_to_pipe(ar,
1419 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1420 ul_pipe,
1421 dl_pipe,
1422 &ul_is_polled,
1423 &dl_is_polled);
1424}
1425
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001426static void ath10k_pci_irq_msi_fw_mask(struct ath10k *ar)
1427{
1428 u32 val;
1429
1430 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS + CORE_CTRL_ADDRESS);
1431 val &= ~CORE_CTRL_PCIE_REG_31_MASK;
1432
1433 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + CORE_CTRL_ADDRESS, val);
1434}
1435
1436static void ath10k_pci_irq_msi_fw_unmask(struct ath10k *ar)
1437{
1438 u32 val;
1439
1440 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS + CORE_CTRL_ADDRESS);
1441 val |= CORE_CTRL_PCIE_REG_31_MASK;
1442
1443 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + CORE_CTRL_ADDRESS, val);
1444}
1445
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001446static void ath10k_pci_irq_disable(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001447{
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001448 ath10k_ce_disable_interrupts(ar);
1449 ath10k_pci_disable_and_clear_legacy_irq(ar);
1450 ath10k_pci_irq_msi_fw_mask(ar);
1451}
1452
1453static void ath10k_pci_irq_sync(struct ath10k *ar)
1454{
Kalle Valo5e3dd152013-06-12 20:52:10 +03001455 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001456 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001457
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001458 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
1459 synchronize_irq(ar_pci->pdev->irq + i);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001460}
1461
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001462static void ath10k_pci_irq_enable(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001463{
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001464 ath10k_ce_enable_interrupts(ar);
Michal Kaziore75db4e2014-08-28 22:14:16 +03001465 ath10k_pci_enable_legacy_irq(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001466 ath10k_pci_irq_msi_fw_unmask(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001467}
1468
1469static int ath10k_pci_hif_start(struct ath10k *ar)
1470{
Janusz Dziedzic76d870e2015-05-18 09:38:16 +00001471 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior7aa7a722014-08-25 12:09:38 +02001472 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif start\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001473
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001474 ath10k_pci_irq_enable(ar);
Michal Kazior728f95e2014-08-22 14:33:14 +02001475 ath10k_pci_rx_post(ar);
Kalle Valo50f87a62014-03-28 09:32:52 +02001476
Janusz Dziedzic76d870e2015-05-18 09:38:16 +00001477 pcie_capability_write_word(ar_pci->pdev, PCI_EXP_LNKCTL,
1478 ar_pci->link_ctl);
1479
Kalle Valo5e3dd152013-06-12 20:52:10 +03001480 return 0;
1481}
1482
Michal Kazior099ac7c2014-10-28 10:32:05 +01001483static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe *pci_pipe)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001484{
1485 struct ath10k *ar;
Michal Kazior099ac7c2014-10-28 10:32:05 +01001486 struct ath10k_ce_pipe *ce_pipe;
1487 struct ath10k_ce_ring *ce_ring;
1488 struct sk_buff *skb;
1489 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001490
Michal Kazior099ac7c2014-10-28 10:32:05 +01001491 ar = pci_pipe->hif_ce_state;
1492 ce_pipe = pci_pipe->ce_hdl;
1493 ce_ring = ce_pipe->dest_ring;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001494
Michal Kazior099ac7c2014-10-28 10:32:05 +01001495 if (!ce_ring)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001496 return;
1497
Michal Kazior099ac7c2014-10-28 10:32:05 +01001498 if (!pci_pipe->buf_sz)
1499 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001500
Michal Kazior099ac7c2014-10-28 10:32:05 +01001501 for (i = 0; i < ce_ring->nentries; i++) {
1502 skb = ce_ring->per_transfer_context[i];
1503 if (!skb)
1504 continue;
1505
1506 ce_ring->per_transfer_context[i] = NULL;
1507
Michal Kazior8582bf32015-01-24 12:14:47 +02001508 dma_unmap_single(ar->dev, ATH10K_SKB_RXCB(skb)->paddr,
Michal Kazior099ac7c2014-10-28 10:32:05 +01001509 skb->len + skb_tailroom(skb),
Kalle Valo5e3dd152013-06-12 20:52:10 +03001510 DMA_FROM_DEVICE);
Michal Kazior099ac7c2014-10-28 10:32:05 +01001511 dev_kfree_skb_any(skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001512 }
1513}
1514
Michal Kazior099ac7c2014-10-28 10:32:05 +01001515static void ath10k_pci_tx_pipe_cleanup(struct ath10k_pci_pipe *pci_pipe)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001516{
1517 struct ath10k *ar;
1518 struct ath10k_pci *ar_pci;
Michal Kazior099ac7c2014-10-28 10:32:05 +01001519 struct ath10k_ce_pipe *ce_pipe;
1520 struct ath10k_ce_ring *ce_ring;
1521 struct ce_desc *ce_desc;
1522 struct sk_buff *skb;
Michal Kazior099ac7c2014-10-28 10:32:05 +01001523 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001524
Michal Kazior099ac7c2014-10-28 10:32:05 +01001525 ar = pci_pipe->hif_ce_state;
1526 ar_pci = ath10k_pci_priv(ar);
1527 ce_pipe = pci_pipe->ce_hdl;
1528 ce_ring = ce_pipe->src_ring;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001529
Michal Kazior099ac7c2014-10-28 10:32:05 +01001530 if (!ce_ring)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001531 return;
1532
Michal Kazior099ac7c2014-10-28 10:32:05 +01001533 if (!pci_pipe->buf_sz)
1534 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001535
Michal Kazior099ac7c2014-10-28 10:32:05 +01001536 ce_desc = ce_ring->shadow_base;
1537 if (WARN_ON(!ce_desc))
1538 return;
1539
1540 for (i = 0; i < ce_ring->nentries; i++) {
1541 skb = ce_ring->per_transfer_context[i];
1542 if (!skb)
Michal Kazior2415fc12013-11-08 08:01:32 +01001543 continue;
Michal Kazior2415fc12013-11-08 08:01:32 +01001544
Michal Kazior099ac7c2014-10-28 10:32:05 +01001545 ce_ring->per_transfer_context[i] = NULL;
Michal Kazior099ac7c2014-10-28 10:32:05 +01001546
Michal Kaziord84a5122014-11-27 11:09:37 +01001547 ar_pci->msg_callbacks_current.tx_completion(ar, skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001548 }
1549}
1550
1551/*
1552 * Cleanup residual buffers for device shutdown:
1553 * buffers that were enqueued for receive
1554 * buffers that were to be sent
1555 * Note: Buffers that had completed but which were
1556 * not yet processed are on a completion queue. They
1557 * are handled when the completion thread shuts down.
1558 */
1559static void ath10k_pci_buffer_cleanup(struct ath10k *ar)
1560{
1561 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1562 int pipe_num;
1563
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001564 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Michal Kazior87263e52013-08-27 13:08:01 +02001565 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001566
1567 pipe_info = &ar_pci->pipe_info[pipe_num];
1568 ath10k_pci_rx_pipe_cleanup(pipe_info);
1569 ath10k_pci_tx_pipe_cleanup(pipe_info);
1570 }
1571}
1572
1573static void ath10k_pci_ce_deinit(struct ath10k *ar)
1574{
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001575 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001576
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001577 for (i = 0; i < CE_COUNT; i++)
1578 ath10k_ce_deinit_pipe(ar, i);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001579}
1580
Michal Kazior728f95e2014-08-22 14:33:14 +02001581static void ath10k_pci_flush(struct ath10k *ar)
1582{
1583 ath10k_pci_kill_tasklet(ar);
1584 ath10k_pci_buffer_cleanup(ar);
1585}
1586
Kalle Valo5e3dd152013-06-12 20:52:10 +03001587static void ath10k_pci_hif_stop(struct ath10k *ar)
1588{
Michal Kazior77258d42015-05-18 09:38:18 +00001589 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1590 unsigned long flags;
1591
Michal Kazior7aa7a722014-08-25 12:09:38 +02001592 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif stop\n");
Michal Kazior32270b62013-08-02 09:15:47 +02001593
Michal Kazior10d23db2014-08-22 14:33:15 +02001594 /* Most likely the device has HTT Rx ring configured. The only way to
1595 * prevent the device from accessing (and possible corrupting) host
1596 * memory is to reset the chip now.
Michal Kaziore75db4e2014-08-28 22:14:16 +03001597 *
1598 * There's also no known way of masking MSI interrupts on the device.
1599 * For ranged MSI the CE-related interrupts can be masked. However
1600 * regardless how many MSI interrupts are assigned the first one
1601 * is always used for firmware indications (crashes) and cannot be
1602 * masked. To prevent the device from asserting the interrupt reset it
1603 * before proceeding with cleanup.
Michal Kazior10d23db2014-08-22 14:33:15 +02001604 */
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001605 ath10k_pci_warm_reset(ar);
Michal Kaziore75db4e2014-08-28 22:14:16 +03001606
1607 ath10k_pci_irq_disable(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001608 ath10k_pci_irq_sync(ar);
Michal Kaziore75db4e2014-08-28 22:14:16 +03001609 ath10k_pci_flush(ar);
Michal Kazior77258d42015-05-18 09:38:18 +00001610
1611 spin_lock_irqsave(&ar_pci->ps_lock, flags);
1612 WARN_ON(ar_pci->ps_wake_refcount > 0);
1613 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001614}
1615
1616static int ath10k_pci_hif_exchange_bmi_msg(struct ath10k *ar,
1617 void *req, u32 req_len,
1618 void *resp, u32 *resp_len)
1619{
1620 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2aa39112013-08-27 13:08:02 +02001621 struct ath10k_pci_pipe *pci_tx = &ar_pci->pipe_info[BMI_CE_NUM_TO_TARG];
1622 struct ath10k_pci_pipe *pci_rx = &ar_pci->pipe_info[BMI_CE_NUM_TO_HOST];
1623 struct ath10k_ce_pipe *ce_tx = pci_tx->ce_hdl;
1624 struct ath10k_ce_pipe *ce_rx = pci_rx->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001625 dma_addr_t req_paddr = 0;
1626 dma_addr_t resp_paddr = 0;
1627 struct bmi_xfer xfer = {};
1628 void *treq, *tresp = NULL;
1629 int ret = 0;
1630
Michal Kazior85622cd2013-11-25 14:06:22 +01001631 might_sleep();
1632
Kalle Valo5e3dd152013-06-12 20:52:10 +03001633 if (resp && !resp_len)
1634 return -EINVAL;
1635
1636 if (resp && resp_len && *resp_len == 0)
1637 return -EINVAL;
1638
1639 treq = kmemdup(req, req_len, GFP_KERNEL);
1640 if (!treq)
1641 return -ENOMEM;
1642
1643 req_paddr = dma_map_single(ar->dev, treq, req_len, DMA_TO_DEVICE);
1644 ret = dma_mapping_error(ar->dev, req_paddr);
1645 if (ret)
1646 goto err_dma;
1647
1648 if (resp && resp_len) {
1649 tresp = kzalloc(*resp_len, GFP_KERNEL);
1650 if (!tresp) {
1651 ret = -ENOMEM;
1652 goto err_req;
1653 }
1654
1655 resp_paddr = dma_map_single(ar->dev, tresp, *resp_len,
1656 DMA_FROM_DEVICE);
1657 ret = dma_mapping_error(ar->dev, resp_paddr);
1658 if (ret)
1659 goto err_req;
1660
1661 xfer.wait_for_resp = true;
1662 xfer.resp_len = 0;
1663
Michal Kazior728f95e2014-08-22 14:33:14 +02001664 ath10k_ce_rx_post_buf(ce_rx, &xfer, resp_paddr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001665 }
1666
Kalle Valo5e3dd152013-06-12 20:52:10 +03001667 ret = ath10k_ce_send(ce_tx, &xfer, req_paddr, req_len, -1, 0);
1668 if (ret)
1669 goto err_resp;
1670
Michal Kazior85622cd2013-11-25 14:06:22 +01001671 ret = ath10k_pci_bmi_wait(ce_tx, ce_rx, &xfer);
1672 if (ret) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001673 u32 unused_buffer;
1674 unsigned int unused_nbytes;
1675 unsigned int unused_id;
1676
Kalle Valo5e3dd152013-06-12 20:52:10 +03001677 ath10k_ce_cancel_send_next(ce_tx, NULL, &unused_buffer,
1678 &unused_nbytes, &unused_id);
1679 } else {
1680 /* non-zero means we did not time out */
1681 ret = 0;
1682 }
1683
1684err_resp:
1685 if (resp) {
1686 u32 unused_buffer;
1687
1688 ath10k_ce_revoke_recv_next(ce_rx, NULL, &unused_buffer);
1689 dma_unmap_single(ar->dev, resp_paddr,
1690 *resp_len, DMA_FROM_DEVICE);
1691 }
1692err_req:
1693 dma_unmap_single(ar->dev, req_paddr, req_len, DMA_TO_DEVICE);
1694
1695 if (ret == 0 && resp_len) {
1696 *resp_len = min(*resp_len, xfer.resp_len);
1697 memcpy(resp, tresp, xfer.resp_len);
1698 }
1699err_dma:
1700 kfree(treq);
1701 kfree(tresp);
1702
1703 return ret;
1704}
1705
Michal Kazior5440ce22013-09-03 15:09:58 +02001706static void ath10k_pci_bmi_send_done(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001707{
Michal Kazior5440ce22013-09-03 15:09:58 +02001708 struct bmi_xfer *xfer;
1709 u32 ce_data;
1710 unsigned int nbytes;
1711 unsigned int transfer_id;
1712
1713 if (ath10k_ce_completed_send_next(ce_state, (void **)&xfer, &ce_data,
1714 &nbytes, &transfer_id))
1715 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001716
Michal Kazior2374b182014-07-14 16:25:25 +03001717 xfer->tx_done = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001718}
1719
Michal Kazior5440ce22013-09-03 15:09:58 +02001720static void ath10k_pci_bmi_recv_data(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001721{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001722 struct ath10k *ar = ce_state->ar;
Michal Kazior5440ce22013-09-03 15:09:58 +02001723 struct bmi_xfer *xfer;
1724 u32 ce_data;
1725 unsigned int nbytes;
1726 unsigned int transfer_id;
1727 unsigned int flags;
1728
1729 if (ath10k_ce_completed_recv_next(ce_state, (void **)&xfer, &ce_data,
1730 &nbytes, &transfer_id, &flags))
1731 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001732
Michal Kazior04ed9df2014-10-28 10:34:36 +01001733 if (WARN_ON_ONCE(!xfer))
1734 return;
1735
Kalle Valo5e3dd152013-06-12 20:52:10 +03001736 if (!xfer->wait_for_resp) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001737 ath10k_warn(ar, "unexpected: BMI data received; ignoring\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001738 return;
1739 }
1740
1741 xfer->resp_len = nbytes;
Michal Kazior2374b182014-07-14 16:25:25 +03001742 xfer->rx_done = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001743}
1744
Michal Kazior85622cd2013-11-25 14:06:22 +01001745static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
1746 struct ath10k_ce_pipe *rx_pipe,
1747 struct bmi_xfer *xfer)
1748{
1749 unsigned long timeout = jiffies + BMI_COMMUNICATION_TIMEOUT_HZ;
1750
1751 while (time_before_eq(jiffies, timeout)) {
1752 ath10k_pci_bmi_send_done(tx_pipe);
1753 ath10k_pci_bmi_recv_data(rx_pipe);
1754
Michal Kazior2374b182014-07-14 16:25:25 +03001755 if (xfer->tx_done && (xfer->rx_done == xfer->wait_for_resp))
Michal Kazior85622cd2013-11-25 14:06:22 +01001756 return 0;
1757
1758 schedule();
1759 }
1760
1761 return -ETIMEDOUT;
1762}
1763
Kalle Valo5e3dd152013-06-12 20:52:10 +03001764/*
Kalle Valo5e3dd152013-06-12 20:52:10 +03001765 * Send an interrupt to the device to wake up the Target CPU
1766 * so it has an opportunity to notice any changed state.
1767 */
1768static int ath10k_pci_wake_target_cpu(struct ath10k *ar)
1769{
Michal Kazior9e264942014-09-02 11:00:21 +03001770 u32 addr, val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001771
Michal Kazior9e264942014-09-02 11:00:21 +03001772 addr = SOC_CORE_BASE_ADDRESS | CORE_CTRL_ADDRESS;
1773 val = ath10k_pci_read32(ar, addr);
1774 val |= CORE_CTRL_CPU_INTR_MASK;
1775 ath10k_pci_write32(ar, addr, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001776
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001777 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001778}
1779
Michal Kaziord63955b2015-01-24 12:14:49 +02001780static int ath10k_pci_get_num_banks(struct ath10k *ar)
1781{
1782 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1783
1784 switch (ar_pci->pdev->device) {
1785 case QCA988X_2_0_DEVICE_ID:
Vasanthakumar Thiagarajan8bd47022015-06-18 12:31:03 +05301786 case QCA99X0_2_0_DEVICE_ID:
Michal Kaziord63955b2015-01-24 12:14:49 +02001787 return 1;
1788 case QCA6174_2_1_DEVICE_ID:
1789 switch (MS(ar->chip_id, SOC_CHIP_ID_REV)) {
1790 case QCA6174_HW_1_0_CHIP_ID_REV:
1791 case QCA6174_HW_1_1_CHIP_ID_REV:
Michal Kazior11a002e2015-04-20 09:20:41 +00001792 case QCA6174_HW_2_1_CHIP_ID_REV:
1793 case QCA6174_HW_2_2_CHIP_ID_REV:
Michal Kaziord63955b2015-01-24 12:14:49 +02001794 return 3;
1795 case QCA6174_HW_1_3_CHIP_ID_REV:
1796 return 2;
Michal Kaziord63955b2015-01-24 12:14:49 +02001797 case QCA6174_HW_3_0_CHIP_ID_REV:
1798 case QCA6174_HW_3_1_CHIP_ID_REV:
1799 case QCA6174_HW_3_2_CHIP_ID_REV:
1800 return 9;
1801 }
1802 break;
1803 }
1804
1805 ath10k_warn(ar, "unknown number of banks, assuming 1\n");
1806 return 1;
1807}
1808
Kalle Valo5e3dd152013-06-12 20:52:10 +03001809static int ath10k_pci_init_config(struct ath10k *ar)
1810{
1811 u32 interconnect_targ_addr;
1812 u32 pcie_state_targ_addr = 0;
1813 u32 pipe_cfg_targ_addr = 0;
1814 u32 svc_to_pipe_map = 0;
1815 u32 pcie_config_flags = 0;
1816 u32 ealloc_value;
1817 u32 ealloc_targ_addr;
1818 u32 flag2_value;
1819 u32 flag2_targ_addr;
1820 int ret = 0;
1821
1822 /* Download to Target the CE Config and the service-to-CE map */
1823 interconnect_targ_addr =
1824 host_interest_item_address(HI_ITEM(hi_interconnect_state));
1825
1826 /* Supply Target-side CE configuration */
Michal Kazior9e264942014-09-02 11:00:21 +03001827 ret = ath10k_pci_diag_read32(ar, interconnect_targ_addr,
1828 &pcie_state_targ_addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001829 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001830 ath10k_err(ar, "Failed to get pcie state addr: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001831 return ret;
1832 }
1833
1834 if (pcie_state_targ_addr == 0) {
1835 ret = -EIO;
Michal Kazior7aa7a722014-08-25 12:09:38 +02001836 ath10k_err(ar, "Invalid pcie state addr\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001837 return ret;
1838 }
1839
Michal Kazior9e264942014-09-02 11:00:21 +03001840 ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
Kalle Valo5e3dd152013-06-12 20:52:10 +03001841 offsetof(struct pcie_state,
Michal Kazior9e264942014-09-02 11:00:21 +03001842 pipe_cfg_addr)),
1843 &pipe_cfg_targ_addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001844 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001845 ath10k_err(ar, "Failed to get pipe cfg addr: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001846 return ret;
1847 }
1848
1849 if (pipe_cfg_targ_addr == 0) {
1850 ret = -EIO;
Michal Kazior7aa7a722014-08-25 12:09:38 +02001851 ath10k_err(ar, "Invalid pipe cfg addr\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001852 return ret;
1853 }
1854
1855 ret = ath10k_pci_diag_write_mem(ar, pipe_cfg_targ_addr,
Kalle Valo5b07e072014-09-14 12:50:06 +03001856 target_ce_config_wlan,
Vasanthakumar Thiagarajan050af062015-06-18 12:31:04 +05301857 sizeof(struct ce_pipe_config) *
1858 NUM_TARGET_CE_CONFIG_WLAN);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001859
1860 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001861 ath10k_err(ar, "Failed to write pipe cfg: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001862 return ret;
1863 }
1864
Michal Kazior9e264942014-09-02 11:00:21 +03001865 ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
Kalle Valo5e3dd152013-06-12 20:52:10 +03001866 offsetof(struct pcie_state,
Michal Kazior9e264942014-09-02 11:00:21 +03001867 svc_to_pipe_map)),
1868 &svc_to_pipe_map);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001869 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001870 ath10k_err(ar, "Failed to get svc/pipe map: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001871 return ret;
1872 }
1873
1874 if (svc_to_pipe_map == 0) {
1875 ret = -EIO;
Michal Kazior7aa7a722014-08-25 12:09:38 +02001876 ath10k_err(ar, "Invalid svc_to_pipe map\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001877 return ret;
1878 }
1879
1880 ret = ath10k_pci_diag_write_mem(ar, svc_to_pipe_map,
Kalle Valo5b07e072014-09-14 12:50:06 +03001881 target_service_to_ce_map_wlan,
1882 sizeof(target_service_to_ce_map_wlan));
Kalle Valo5e3dd152013-06-12 20:52:10 +03001883 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001884 ath10k_err(ar, "Failed to write svc/pipe map: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001885 return ret;
1886 }
1887
Michal Kazior9e264942014-09-02 11:00:21 +03001888 ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
Kalle Valo5e3dd152013-06-12 20:52:10 +03001889 offsetof(struct pcie_state,
Michal Kazior9e264942014-09-02 11:00:21 +03001890 config_flags)),
1891 &pcie_config_flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001892 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001893 ath10k_err(ar, "Failed to get pcie config_flags: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001894 return ret;
1895 }
1896
1897 pcie_config_flags &= ~PCIE_CONFIG_FLAG_ENABLE_L1;
1898
Michal Kazior9e264942014-09-02 11:00:21 +03001899 ret = ath10k_pci_diag_write32(ar, (pcie_state_targ_addr +
1900 offsetof(struct pcie_state,
1901 config_flags)),
1902 pcie_config_flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001903 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001904 ath10k_err(ar, "Failed to write pcie config_flags: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001905 return ret;
1906 }
1907
1908 /* configure early allocation */
1909 ealloc_targ_addr = host_interest_item_address(HI_ITEM(hi_early_alloc));
1910
Michal Kazior9e264942014-09-02 11:00:21 +03001911 ret = ath10k_pci_diag_read32(ar, ealloc_targ_addr, &ealloc_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001912 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001913 ath10k_err(ar, "Faile to get early alloc val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001914 return ret;
1915 }
1916
1917 /* first bank is switched to IRAM */
1918 ealloc_value |= ((HI_EARLY_ALLOC_MAGIC << HI_EARLY_ALLOC_MAGIC_SHIFT) &
1919 HI_EARLY_ALLOC_MAGIC_MASK);
Michal Kaziord63955b2015-01-24 12:14:49 +02001920 ealloc_value |= ((ath10k_pci_get_num_banks(ar) <<
1921 HI_EARLY_ALLOC_IRAM_BANKS_SHIFT) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03001922 HI_EARLY_ALLOC_IRAM_BANKS_MASK);
1923
Michal Kazior9e264942014-09-02 11:00:21 +03001924 ret = ath10k_pci_diag_write32(ar, ealloc_targ_addr, ealloc_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001925 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001926 ath10k_err(ar, "Failed to set early alloc val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001927 return ret;
1928 }
1929
1930 /* Tell Target to proceed with initialization */
1931 flag2_targ_addr = host_interest_item_address(HI_ITEM(hi_option_flag2));
1932
Michal Kazior9e264942014-09-02 11:00:21 +03001933 ret = ath10k_pci_diag_read32(ar, flag2_targ_addr, &flag2_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001934 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001935 ath10k_err(ar, "Failed to get option val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001936 return ret;
1937 }
1938
1939 flag2_value |= HI_OPTION_EARLY_CFG_DONE;
1940
Michal Kazior9e264942014-09-02 11:00:21 +03001941 ret = ath10k_pci_diag_write32(ar, flag2_targ_addr, flag2_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001942 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001943 ath10k_err(ar, "Failed to set option val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001944 return ret;
1945 }
1946
1947 return 0;
1948}
1949
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001950static int ath10k_pci_alloc_pipes(struct ath10k *ar)
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001951{
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001952 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1953 struct ath10k_pci_pipe *pipe;
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001954 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001955
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001956 for (i = 0; i < CE_COUNT; i++) {
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001957 pipe = &ar_pci->pipe_info[i];
1958 pipe->ce_hdl = &ar_pci->ce_states[i];
1959 pipe->pipe_num = i;
1960 pipe->hif_ce_state = ar;
1961
1962 ret = ath10k_ce_alloc_pipe(ar, i, &host_ce_config_wlan[i],
1963 ath10k_pci_ce_send_done,
1964 ath10k_pci_ce_recv_data);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001965 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001966 ath10k_err(ar, "failed to allocate copy engine pipe %d: %d\n",
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001967 i, ret);
1968 return ret;
1969 }
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001970
1971 /* Last CE is Diagnostic Window */
Vasanthakumar Thiagarajan050af062015-06-18 12:31:04 +05301972 if (i == CE_DIAG_PIPE) {
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001973 ar_pci->ce_diag = pipe->ce_hdl;
1974 continue;
1975 }
1976
1977 pipe->buf_sz = (size_t)(host_ce_config_wlan[i].src_sz_max);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001978 }
1979
1980 return 0;
1981}
1982
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001983static void ath10k_pci_free_pipes(struct ath10k *ar)
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001984{
1985 int i;
1986
1987 for (i = 0; i < CE_COUNT; i++)
1988 ath10k_ce_free_pipe(ar, i);
1989}
Kalle Valo5e3dd152013-06-12 20:52:10 +03001990
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001991static int ath10k_pci_init_pipes(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001992{
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001993 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001994
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001995 for (i = 0; i < CE_COUNT; i++) {
1996 ret = ath10k_ce_init_pipe(ar, i, &host_ce_config_wlan[i]);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001997 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001998 ath10k_err(ar, "failed to initialize copy engine pipe %d: %d\n",
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001999 i, ret);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002000 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002001 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002002 }
2003
Kalle Valo5e3dd152013-06-12 20:52:10 +03002004 return 0;
2005}
2006
Michal Kazior5c771e72014-08-22 14:23:34 +02002007static bool ath10k_pci_has_fw_crashed(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002008{
Michal Kazior5c771e72014-08-22 14:23:34 +02002009 return ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS) &
2010 FW_IND_EVENT_PENDING;
2011}
Kalle Valo5e3dd152013-06-12 20:52:10 +03002012
Michal Kazior5c771e72014-08-22 14:23:34 +02002013static void ath10k_pci_fw_crashed_clear(struct ath10k *ar)
2014{
2015 u32 val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002016
Michal Kazior5c771e72014-08-22 14:23:34 +02002017 val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
2018 val &= ~FW_IND_EVENT_PENDING;
2019 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002020}
2021
Michal Kaziorde013572014-05-14 16:56:16 +03002022/* this function effectively clears target memory controller assert line */
2023static void ath10k_pci_warm_reset_si0(struct ath10k *ar)
2024{
2025 u32 val;
2026
2027 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
2028 ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
2029 val | SOC_RESET_CONTROL_SI0_RST_MASK);
2030 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
2031
2032 msleep(10);
2033
2034 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
2035 ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
2036 val & ~SOC_RESET_CONTROL_SI0_RST_MASK);
2037 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
2038
2039 msleep(10);
2040}
2041
Michal Kazior61c16482014-10-28 10:32:06 +01002042static void ath10k_pci_warm_reset_cpu(struct ath10k *ar)
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002043{
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002044 u32 val;
2045
Kalle Valob39712c2014-03-28 09:32:46 +02002046 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, 0);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002047
Michal Kazior61c16482014-10-28 10:32:06 +01002048 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
2049 SOC_RESET_CONTROL_ADDRESS);
2050 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
2051 val | SOC_RESET_CONTROL_CPU_WARM_RST_MASK);
2052}
2053
2054static void ath10k_pci_warm_reset_ce(struct ath10k *ar)
2055{
2056 u32 val;
2057
2058 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
2059 SOC_RESET_CONTROL_ADDRESS);
2060
2061 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
2062 val | SOC_RESET_CONTROL_CE_RST_MASK);
2063 msleep(10);
2064 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
2065 val & ~SOC_RESET_CONTROL_CE_RST_MASK);
2066}
2067
2068static void ath10k_pci_warm_reset_clear_lf(struct ath10k *ar)
2069{
2070 u32 val;
2071
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002072 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
2073 SOC_LF_TIMER_CONTROL0_ADDRESS);
2074 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS +
2075 SOC_LF_TIMER_CONTROL0_ADDRESS,
2076 val & ~SOC_LF_TIMER_CONTROL0_ENABLE_MASK);
Michal Kazior61c16482014-10-28 10:32:06 +01002077}
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002078
Michal Kazior61c16482014-10-28 10:32:06 +01002079static int ath10k_pci_warm_reset(struct ath10k *ar)
2080{
2081 int ret;
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002082
Michal Kazior61c16482014-10-28 10:32:06 +01002083 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot warm reset\n");
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002084
Michal Kazior61c16482014-10-28 10:32:06 +01002085 spin_lock_bh(&ar->data_lock);
2086 ar->stats.fw_warm_reset_counter++;
2087 spin_unlock_bh(&ar->data_lock);
2088
2089 ath10k_pci_irq_disable(ar);
2090
2091 /* Make sure the target CPU is not doing anything dangerous, e.g. if it
2092 * were to access copy engine while host performs copy engine reset
2093 * then it is possible for the device to confuse pci-e controller to
2094 * the point of bringing host system to a complete stop (i.e. hang).
2095 */
Michal Kaziorde013572014-05-14 16:56:16 +03002096 ath10k_pci_warm_reset_si0(ar);
Michal Kazior61c16482014-10-28 10:32:06 +01002097 ath10k_pci_warm_reset_cpu(ar);
2098 ath10k_pci_init_pipes(ar);
2099 ath10k_pci_wait_for_target_init(ar);
Michal Kaziorde013572014-05-14 16:56:16 +03002100
Michal Kazior61c16482014-10-28 10:32:06 +01002101 ath10k_pci_warm_reset_clear_lf(ar);
2102 ath10k_pci_warm_reset_ce(ar);
2103 ath10k_pci_warm_reset_cpu(ar);
2104 ath10k_pci_init_pipes(ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002105
Michal Kazior61c16482014-10-28 10:32:06 +01002106 ret = ath10k_pci_wait_for_target_init(ar);
2107 if (ret) {
2108 ath10k_warn(ar, "failed to wait for target init: %d\n", ret);
2109 return ret;
2110 }
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002111
Michal Kazior7aa7a722014-08-25 12:09:38 +02002112 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot warm reset complete\n");
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002113
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002114 return 0;
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002115}
2116
Michal Kaziord63955b2015-01-24 12:14:49 +02002117static int ath10k_pci_qca988x_chip_reset(struct ath10k *ar)
Michal Kazior0bc14d02014-10-28 10:32:07 +01002118{
2119 int i, ret;
2120 u32 val;
2121
Michal Kaziord63955b2015-01-24 12:14:49 +02002122 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot 988x chip reset\n");
Michal Kazior0bc14d02014-10-28 10:32:07 +01002123
2124 /* Some hardware revisions (e.g. CUS223v2) has issues with cold reset.
2125 * It is thus preferred to use warm reset which is safer but may not be
2126 * able to recover the device from all possible fail scenarios.
2127 *
2128 * Warm reset doesn't always work on first try so attempt it a few
2129 * times before giving up.
2130 */
2131 for (i = 0; i < ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS; i++) {
2132 ret = ath10k_pci_warm_reset(ar);
2133 if (ret) {
2134 ath10k_warn(ar, "failed to warm reset attempt %d of %d: %d\n",
2135 i + 1, ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS,
2136 ret);
2137 continue;
2138 }
2139
2140 /* FIXME: Sometimes copy engine doesn't recover after warm
2141 * reset. In most cases this needs cold reset. In some of these
2142 * cases the device is in such a state that a cold reset may
2143 * lock up the host.
2144 *
2145 * Reading any host interest register via copy engine is
2146 * sufficient to verify if device is capable of booting
2147 * firmware blob.
2148 */
2149 ret = ath10k_pci_init_pipes(ar);
2150 if (ret) {
2151 ath10k_warn(ar, "failed to init copy engine: %d\n",
2152 ret);
2153 continue;
2154 }
2155
2156 ret = ath10k_pci_diag_read32(ar, QCA988X_HOST_INTEREST_ADDRESS,
2157 &val);
2158 if (ret) {
2159 ath10k_warn(ar, "failed to poke copy engine: %d\n",
2160 ret);
2161 continue;
2162 }
2163
2164 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot chip reset complete (warm)\n");
2165 return 0;
2166 }
2167
2168 if (ath10k_pci_reset_mode == ATH10K_PCI_RESET_WARM_ONLY) {
2169 ath10k_warn(ar, "refusing cold reset as requested\n");
2170 return -EPERM;
2171 }
2172
2173 ret = ath10k_pci_cold_reset(ar);
2174 if (ret) {
2175 ath10k_warn(ar, "failed to cold reset: %d\n", ret);
2176 return ret;
2177 }
2178
2179 ret = ath10k_pci_wait_for_target_init(ar);
2180 if (ret) {
2181 ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
2182 ret);
2183 return ret;
2184 }
2185
Michal Kaziord63955b2015-01-24 12:14:49 +02002186 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca988x chip reset complete (cold)\n");
Michal Kazior0bc14d02014-10-28 10:32:07 +01002187
2188 return 0;
2189}
2190
Michal Kaziord63955b2015-01-24 12:14:49 +02002191static int ath10k_pci_qca6174_chip_reset(struct ath10k *ar)
2192{
2193 int ret;
2194
2195 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca6174 chip reset\n");
2196
2197 /* FIXME: QCA6174 requires cold + warm reset to work. */
2198
2199 ret = ath10k_pci_cold_reset(ar);
2200 if (ret) {
2201 ath10k_warn(ar, "failed to cold reset: %d\n", ret);
2202 return ret;
2203 }
2204
2205 ret = ath10k_pci_wait_for_target_init(ar);
2206 if (ret) {
2207 ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
2208 ret);
2209 return ret;
2210 }
2211
2212 ret = ath10k_pci_warm_reset(ar);
2213 if (ret) {
2214 ath10k_warn(ar, "failed to warm reset: %d\n", ret);
2215 return ret;
2216 }
2217
2218 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca6174 chip reset complete (cold)\n");
2219
2220 return 0;
2221}
2222
2223static int ath10k_pci_chip_reset(struct ath10k *ar)
2224{
2225 if (QCA_REV_988X(ar))
2226 return ath10k_pci_qca988x_chip_reset(ar);
2227 else if (QCA_REV_6174(ar))
2228 return ath10k_pci_qca6174_chip_reset(ar);
2229 else
2230 return -ENOTSUPP;
2231}
2232
Michal Kazior0bc14d02014-10-28 10:32:07 +01002233static int ath10k_pci_hif_power_up(struct ath10k *ar)
Michal Kazior8c5c5362013-07-16 09:38:50 +02002234{
Janusz Dziedzic76d870e2015-05-18 09:38:16 +00002235 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02002236 int ret;
2237
Michal Kazior0bc14d02014-10-28 10:32:07 +01002238 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif power up\n");
2239
Janusz Dziedzic76d870e2015-05-18 09:38:16 +00002240 pcie_capability_read_word(ar_pci->pdev, PCI_EXP_LNKCTL,
2241 &ar_pci->link_ctl);
2242 pcie_capability_write_word(ar_pci->pdev, PCI_EXP_LNKCTL,
2243 ar_pci->link_ctl & ~PCI_EXP_LNKCTL_ASPMC);
2244
Michal Kazior8c5c5362013-07-16 09:38:50 +02002245 /*
2246 * Bring the target up cleanly.
2247 *
2248 * The target may be in an undefined state with an AUX-powered Target
2249 * and a Host in WoW mode. If the Host crashes, loses power, or is
2250 * restarted (without unloading the driver) then the Target is left
2251 * (aux) powered and running. On a subsequent driver load, the Target
2252 * is in an unexpected state. We try to catch that here in order to
2253 * reset the Target and retry the probe.
2254 */
Michal Kazior0bc14d02014-10-28 10:32:07 +01002255 ret = ath10k_pci_chip_reset(ar);
Michal Kazior5b2589f2013-11-08 08:01:30 +01002256 if (ret) {
Michal Kaziora2fa8802015-01-12 15:29:37 +01002257 if (ath10k_pci_has_fw_crashed(ar)) {
2258 ath10k_warn(ar, "firmware crashed during chip reset\n");
2259 ath10k_pci_fw_crashed_clear(ar);
2260 ath10k_pci_fw_crashed_dump(ar);
2261 }
2262
Michal Kazior0bc14d02014-10-28 10:32:07 +01002263 ath10k_err(ar, "failed to reset chip: %d\n", ret);
Bartosz Markowski707b1bbd2014-10-31 09:03:43 +01002264 goto err_sleep;
Michal Kazior5b2589f2013-11-08 08:01:30 +01002265 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02002266
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002267 ret = ath10k_pci_init_pipes(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02002268 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002269 ath10k_err(ar, "failed to initialize CE: %d\n", ret);
Bartosz Markowski707b1bbd2014-10-31 09:03:43 +01002270 goto err_sleep;
Michal Kaziorab977bd2013-11-25 14:06:26 +01002271 }
2272
Michal Kazior98563d52013-11-08 08:01:33 +01002273 ret = ath10k_pci_init_config(ar);
2274 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002275 ath10k_err(ar, "failed to setup init config: %d\n", ret);
Michal Kazior5c771e72014-08-22 14:23:34 +02002276 goto err_ce;
Michal Kazior98563d52013-11-08 08:01:33 +01002277 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02002278
2279 ret = ath10k_pci_wake_target_cpu(ar);
2280 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002281 ath10k_err(ar, "could not wake up target CPU: %d\n", ret);
Michal Kazior5c771e72014-08-22 14:23:34 +02002282 goto err_ce;
Michal Kazior8c5c5362013-07-16 09:38:50 +02002283 }
2284
2285 return 0;
2286
2287err_ce:
2288 ath10k_pci_ce_deinit(ar);
Michal Kazior0bc14d02014-10-28 10:32:07 +01002289
Bartosz Markowski707b1bbd2014-10-31 09:03:43 +01002290err_sleep:
Michal Kazior8c5c5362013-07-16 09:38:50 +02002291 return ret;
2292}
2293
2294static void ath10k_pci_hif_power_down(struct ath10k *ar)
2295{
Michal Kazior7aa7a722014-08-25 12:09:38 +02002296 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif power down\n");
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002297
Michal Kaziorc011b282014-10-28 10:32:08 +01002298 /* Currently hif_power_up performs effectively a reset and hif_stop
2299 * resets the chip as well so there's no point in resetting here.
2300 */
Michal Kazior8c5c5362013-07-16 09:38:50 +02002301}
2302
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002303#ifdef CONFIG_PM
2304
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002305static int ath10k_pci_hif_suspend(struct ath10k *ar)
2306{
Michal Kazior77258d42015-05-18 09:38:18 +00002307 /* The grace timer can still be counting down and ar->ps_awake be true.
2308 * It is known that the device may be asleep after resuming regardless
2309 * of the SoC powersave state before suspending. Hence make sure the
2310 * device is asleep before proceeding.
2311 */
2312 ath10k_pci_sleep_sync(ar);
Michal Kazior320e14b2015-03-02 13:22:13 +01002313
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002314 return 0;
2315}
2316
2317static int ath10k_pci_hif_resume(struct ath10k *ar)
2318{
2319 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2320 struct pci_dev *pdev = ar_pci->pdev;
2321 u32 val;
2322
Michal Kazior9ff4be92015-03-02 13:22:14 +01002323 /* Suspend/Resume resets the PCI configuration space, so we have to
2324 * re-disable the RETRY_TIMEOUT register (0x41) to keep PCI Tx retries
2325 * from interfering with C3 CPU state. pci_restore_state won't help
2326 * here since it only restores the first 64 bytes pci config header.
2327 */
2328 pci_read_config_dword(pdev, 0x40, &val);
2329 if ((val & 0x0000ff00) != 0)
2330 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002331
Michal Kazior77258d42015-05-18 09:38:18 +00002332 return 0;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002333}
2334#endif
2335
Kalle Valo5e3dd152013-06-12 20:52:10 +03002336static const struct ath10k_hif_ops ath10k_pci_hif_ops = {
Michal Kazior726346f2014-02-27 18:50:04 +02002337 .tx_sg = ath10k_pci_hif_tx_sg,
Kalle Valoeef25402014-09-24 14:16:52 +03002338 .diag_read = ath10k_pci_hif_diag_read,
Yanbo Li9f65ad22014-11-25 12:24:48 +02002339 .diag_write = ath10k_pci_diag_write_mem,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002340 .exchange_bmi_msg = ath10k_pci_hif_exchange_bmi_msg,
2341 .start = ath10k_pci_hif_start,
2342 .stop = ath10k_pci_hif_stop,
2343 .map_service_to_pipe = ath10k_pci_hif_map_service_to_pipe,
2344 .get_default_pipe = ath10k_pci_hif_get_default_pipe,
2345 .send_complete_check = ath10k_pci_hif_send_complete_check,
Michal Kaziore799bbf2013-07-05 16:15:12 +03002346 .set_callbacks = ath10k_pci_hif_set_callbacks,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002347 .get_free_queue_number = ath10k_pci_hif_get_free_queue_number,
Michal Kazior8c5c5362013-07-16 09:38:50 +02002348 .power_up = ath10k_pci_hif_power_up,
2349 .power_down = ath10k_pci_hif_power_down,
Yanbo Li077a3802014-11-25 12:24:33 +02002350 .read32 = ath10k_pci_read32,
2351 .write32 = ath10k_pci_write32,
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002352#ifdef CONFIG_PM
2353 .suspend = ath10k_pci_hif_suspend,
2354 .resume = ath10k_pci_hif_resume,
2355#endif
Kalle Valo5e3dd152013-06-12 20:52:10 +03002356};
2357
2358static void ath10k_pci_ce_tasklet(unsigned long ptr)
2359{
Michal Kazior87263e52013-08-27 13:08:01 +02002360 struct ath10k_pci_pipe *pipe = (struct ath10k_pci_pipe *)ptr;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002361 struct ath10k_pci *ar_pci = pipe->ar_pci;
2362
2363 ath10k_ce_per_engine_service(ar_pci->ar, pipe->pipe_num);
2364}
2365
2366static void ath10k_msi_err_tasklet(unsigned long data)
2367{
2368 struct ath10k *ar = (struct ath10k *)data;
2369
Michal Kazior5c771e72014-08-22 14:23:34 +02002370 if (!ath10k_pci_has_fw_crashed(ar)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002371 ath10k_warn(ar, "received unsolicited fw crash interrupt\n");
Michal Kazior5c771e72014-08-22 14:23:34 +02002372 return;
2373 }
2374
Michal Kazior6f3b7ff2015-01-24 12:14:52 +02002375 ath10k_pci_irq_disable(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02002376 ath10k_pci_fw_crashed_clear(ar);
2377 ath10k_pci_fw_crashed_dump(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002378}
2379
2380/*
2381 * Handler for a per-engine interrupt on a PARTICULAR CE.
2382 * This is used in cases where each CE has a private MSI interrupt.
2383 */
2384static irqreturn_t ath10k_pci_per_engine_handler(int irq, void *arg)
2385{
2386 struct ath10k *ar = arg;
2387 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2388 int ce_id = irq - ar_pci->pdev->irq - MSI_ASSIGN_CE_INITIAL;
2389
Dan Carpentere5742672013-06-18 10:28:46 +03002390 if (ce_id < 0 || ce_id >= ARRAY_SIZE(ar_pci->pipe_info)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002391 ath10k_warn(ar, "unexpected/invalid irq %d ce_id %d\n", irq,
2392 ce_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002393 return IRQ_HANDLED;
2394 }
2395
2396 /*
2397 * NOTE: We are able to derive ce_id from irq because we
2398 * use a one-to-one mapping for CE's 0..5.
2399 * CE's 6 & 7 do not use interrupts at all.
2400 *
2401 * This mapping must be kept in sync with the mapping
2402 * used by firmware.
2403 */
2404 tasklet_schedule(&ar_pci->pipe_info[ce_id].intr);
2405 return IRQ_HANDLED;
2406}
2407
2408static irqreturn_t ath10k_pci_msi_fw_handler(int irq, void *arg)
2409{
2410 struct ath10k *ar = arg;
2411 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2412
2413 tasklet_schedule(&ar_pci->msi_fw_err);
2414 return IRQ_HANDLED;
2415}
2416
2417/*
2418 * Top-level interrupt handler for all PCI interrupts from a Target.
2419 * When a block of MSI interrupts is allocated, this top-level handler
2420 * is not used; instead, we directly call the correct sub-handler.
2421 */
2422static irqreturn_t ath10k_pci_interrupt_handler(int irq, void *arg)
2423{
2424 struct ath10k *ar = arg;
2425 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2426
2427 if (ar_pci->num_msi_intrs == 0) {
Michal Kaziore5398872013-11-25 14:06:20 +01002428 if (!ath10k_pci_irq_pending(ar))
2429 return IRQ_NONE;
2430
Michal Kazior26852182013-11-25 14:06:25 +01002431 ath10k_pci_disable_and_clear_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002432 }
2433
2434 tasklet_schedule(&ar_pci->intr_tq);
2435
2436 return IRQ_HANDLED;
2437}
2438
2439static void ath10k_pci_tasklet(unsigned long data)
2440{
2441 struct ath10k *ar = (struct ath10k *)data;
2442 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2443
Michal Kazior5c771e72014-08-22 14:23:34 +02002444 if (ath10k_pci_has_fw_crashed(ar)) {
Michal Kazior6f3b7ff2015-01-24 12:14:52 +02002445 ath10k_pci_irq_disable(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02002446 ath10k_pci_fw_crashed_clear(ar);
2447 ath10k_pci_fw_crashed_dump(ar);
2448 return;
2449 }
2450
Kalle Valo5e3dd152013-06-12 20:52:10 +03002451 ath10k_ce_per_engine_service_any(ar);
2452
Michal Kazior26852182013-11-25 14:06:25 +01002453 /* Re-enable legacy irq that was disabled in the irq handler */
2454 if (ar_pci->num_msi_intrs == 0)
2455 ath10k_pci_enable_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002456}
2457
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002458static int ath10k_pci_request_irq_msix(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002459{
2460 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002461 int ret, i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002462
2463 ret = request_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW,
2464 ath10k_pci_msi_fw_handler,
2465 IRQF_SHARED, "ath10k_pci", ar);
Michal Kazior591ecdb2013-07-31 10:55:15 +02002466 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002467 ath10k_warn(ar, "failed to request MSI-X fw irq %d: %d\n",
Michal Kazior591ecdb2013-07-31 10:55:15 +02002468 ar_pci->pdev->irq + MSI_ASSIGN_FW, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002469 return ret;
Michal Kazior591ecdb2013-07-31 10:55:15 +02002470 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002471
2472 for (i = MSI_ASSIGN_CE_INITIAL; i <= MSI_ASSIGN_CE_MAX; i++) {
2473 ret = request_irq(ar_pci->pdev->irq + i,
2474 ath10k_pci_per_engine_handler,
2475 IRQF_SHARED, "ath10k_pci", ar);
2476 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002477 ath10k_warn(ar, "failed to request MSI-X ce irq %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002478 ar_pci->pdev->irq + i, ret);
2479
Michal Kazior87b14232013-06-26 08:50:50 +02002480 for (i--; i >= MSI_ASSIGN_CE_INITIAL; i--)
2481 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002482
Michal Kazior87b14232013-06-26 08:50:50 +02002483 free_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002484 return ret;
2485 }
2486 }
2487
Kalle Valo5e3dd152013-06-12 20:52:10 +03002488 return 0;
2489}
2490
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002491static int ath10k_pci_request_irq_msi(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002492{
2493 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2494 int ret;
2495
2496 ret = request_irq(ar_pci->pdev->irq,
2497 ath10k_pci_interrupt_handler,
2498 IRQF_SHARED, "ath10k_pci", ar);
Kalle Valof3782742013-10-17 11:36:15 +03002499 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002500 ath10k_warn(ar, "failed to request MSI irq %d: %d\n",
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002501 ar_pci->pdev->irq, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002502 return ret;
Kalle Valof3782742013-10-17 11:36:15 +03002503 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002504
Kalle Valo5e3dd152013-06-12 20:52:10 +03002505 return 0;
2506}
2507
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002508static int ath10k_pci_request_irq_legacy(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002509{
2510 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002511 int ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002512
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002513 ret = request_irq(ar_pci->pdev->irq,
2514 ath10k_pci_interrupt_handler,
2515 IRQF_SHARED, "ath10k_pci", ar);
Kalle Valof3782742013-10-17 11:36:15 +03002516 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002517 ath10k_warn(ar, "failed to request legacy irq %d: %d\n",
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002518 ar_pci->pdev->irq, ret);
Kalle Valof3782742013-10-17 11:36:15 +03002519 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002520 }
2521
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002522 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002523}
2524
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002525static int ath10k_pci_request_irq(struct ath10k *ar)
2526{
2527 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2528
2529 switch (ar_pci->num_msi_intrs) {
2530 case 0:
2531 return ath10k_pci_request_irq_legacy(ar);
2532 case 1:
2533 return ath10k_pci_request_irq_msi(ar);
2534 case MSI_NUM_REQUEST:
2535 return ath10k_pci_request_irq_msix(ar);
2536 }
2537
Michal Kazior7aa7a722014-08-25 12:09:38 +02002538 ath10k_warn(ar, "unknown irq configuration upon request\n");
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002539 return -EINVAL;
2540}
2541
2542static void ath10k_pci_free_irq(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002543{
2544 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2545 int i;
2546
2547 /* There's at least one interrupt irregardless whether its legacy INTR
2548 * or MSI or MSI-X */
2549 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
2550 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002551}
2552
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002553static void ath10k_pci_init_irq_tasklets(struct ath10k *ar)
2554{
2555 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2556 int i;
2557
2558 tasklet_init(&ar_pci->intr_tq, ath10k_pci_tasklet, (unsigned long)ar);
2559 tasklet_init(&ar_pci->msi_fw_err, ath10k_msi_err_tasklet,
2560 (unsigned long)ar);
2561
2562 for (i = 0; i < CE_COUNT; i++) {
2563 ar_pci->pipe_info[i].ar_pci = ar_pci;
2564 tasklet_init(&ar_pci->pipe_info[i].intr, ath10k_pci_ce_tasklet,
2565 (unsigned long)&ar_pci->pipe_info[i]);
2566 }
2567}
2568
2569static int ath10k_pci_init_irq(struct ath10k *ar)
2570{
2571 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2572 int ret;
2573
2574 ath10k_pci_init_irq_tasklets(ar);
2575
Michal Kazior403d6272014-08-22 14:23:31 +02002576 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_AUTO)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002577 ath10k_info(ar, "limiting irq mode to: %d\n",
2578 ath10k_pci_irq_mode);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002579
2580 /* Try MSI-X */
Michal Kazior0edf2572014-08-07 11:03:29 +02002581 if (ath10k_pci_irq_mode == ATH10K_PCI_IRQ_AUTO) {
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002582 ar_pci->num_msi_intrs = MSI_NUM_REQUEST;
Alexander Gordeev5ad68672014-02-13 17:50:02 +02002583 ret = pci_enable_msi_range(ar_pci->pdev, ar_pci->num_msi_intrs,
Kalle Valo5b07e072014-09-14 12:50:06 +03002584 ar_pci->num_msi_intrs);
Alexander Gordeev5ad68672014-02-13 17:50:02 +02002585 if (ret > 0)
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002586 return 0;
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002587
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002588 /* fall-through */
2589 }
2590
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002591 /* Try MSI */
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002592 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_LEGACY) {
2593 ar_pci->num_msi_intrs = 1;
2594 ret = pci_enable_msi(ar_pci->pdev);
2595 if (ret == 0)
2596 return 0;
2597
2598 /* fall-through */
2599 }
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002600
2601 /* Try legacy irq
2602 *
2603 * A potential race occurs here: The CORE_BASE write
2604 * depends on target correctly decoding AXI address but
2605 * host won't know when target writes BAR to CORE_CTRL.
2606 * This write might get lost if target has NOT written BAR.
2607 * For now, fix the race by repeating the write in below
2608 * synchronization checking. */
2609 ar_pci->num_msi_intrs = 0;
2610
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002611 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2612 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002613
2614 return 0;
2615}
2616
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002617static void ath10k_pci_deinit_irq_legacy(struct ath10k *ar)
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002618{
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002619 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2620 0);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002621}
2622
2623static int ath10k_pci_deinit_irq(struct ath10k *ar)
2624{
2625 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2626
2627 switch (ar_pci->num_msi_intrs) {
2628 case 0:
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002629 ath10k_pci_deinit_irq_legacy(ar);
2630 return 0;
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002631 case 1:
2632 /* fall-through */
2633 case MSI_NUM_REQUEST:
2634 pci_disable_msi(ar_pci->pdev);
2635 return 0;
Alexander Gordeevbb8b6212014-02-13 17:50:01 +02002636 default:
2637 pci_disable_msi(ar_pci->pdev);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002638 }
2639
Michal Kazior7aa7a722014-08-25 12:09:38 +02002640 ath10k_warn(ar, "unknown irq configuration upon deinit\n");
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002641 return -EINVAL;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002642}
2643
Michal Kaziord7fb47f2013-11-08 08:01:26 +01002644static int ath10k_pci_wait_for_target_init(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002645{
2646 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo0399eca2014-03-28 09:32:21 +02002647 unsigned long timeout;
Kalle Valo0399eca2014-03-28 09:32:21 +02002648 u32 val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002649
Michal Kazior7aa7a722014-08-25 12:09:38 +02002650 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot waiting target to initialise\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002651
Kalle Valo0399eca2014-03-28 09:32:21 +02002652 timeout = jiffies + msecs_to_jiffies(ATH10K_PCI_TARGET_WAIT);
2653
2654 do {
2655 val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
2656
Michal Kazior7aa7a722014-08-25 12:09:38 +02002657 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target indicator %x\n",
2658 val);
Kalle Valo50f87a62014-03-28 09:32:52 +02002659
Kalle Valo0399eca2014-03-28 09:32:21 +02002660 /* target should never return this */
2661 if (val == 0xffffffff)
2662 continue;
2663
Michal Kazior7710cd22014-04-23 19:30:04 +03002664 /* the device has crashed so don't bother trying anymore */
2665 if (val & FW_IND_EVENT_PENDING)
2666 break;
2667
Kalle Valo0399eca2014-03-28 09:32:21 +02002668 if (val & FW_IND_INITIALIZED)
2669 break;
2670
Kalle Valo5e3dd152013-06-12 20:52:10 +03002671 if (ar_pci->num_msi_intrs == 0)
2672 /* Fix potential race by repeating CORE_BASE writes */
Michal Kaziora4282492014-10-20 14:14:37 +02002673 ath10k_pci_enable_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002674
Kalle Valo0399eca2014-03-28 09:32:21 +02002675 mdelay(10);
2676 } while (time_before(jiffies, timeout));
2677
Michal Kaziora4282492014-10-20 14:14:37 +02002678 ath10k_pci_disable_and_clear_legacy_irq(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02002679 ath10k_pci_irq_msi_fw_mask(ar);
Michal Kaziora4282492014-10-20 14:14:37 +02002680
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002681 if (val == 0xffffffff) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002682 ath10k_err(ar, "failed to read device register, device is gone\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002683 return -EIO;
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002684 }
2685
Michal Kazior7710cd22014-04-23 19:30:04 +03002686 if (val & FW_IND_EVENT_PENDING) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002687 ath10k_warn(ar, "device has crashed during init\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002688 return -ECOMM;
Michal Kazior7710cd22014-04-23 19:30:04 +03002689 }
2690
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002691 if (!(val & FW_IND_INITIALIZED)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002692 ath10k_err(ar, "failed to receive initialized event from target: %08x\n",
Kalle Valo0399eca2014-03-28 09:32:21 +02002693 val);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002694 return -ETIMEDOUT;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002695 }
2696
Michal Kazior7aa7a722014-08-25 12:09:38 +02002697 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target initialised\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002698 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002699}
2700
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002701static int ath10k_pci_cold_reset(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002702{
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002703 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002704 u32 val;
2705
Michal Kazior7aa7a722014-08-25 12:09:38 +02002706 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cold reset\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002707
Ben Greearf51dbe72014-09-29 14:41:46 +03002708 spin_lock_bh(&ar->data_lock);
2709
2710 ar->stats.fw_cold_reset_counter++;
2711
2712 spin_unlock_bh(&ar->data_lock);
2713
Kalle Valo5e3dd152013-06-12 20:52:10 +03002714 /* Put Target, including PCIe, into RESET. */
Kalle Valoe479ed42013-09-01 10:01:53 +03002715 val = ath10k_pci_reg_read32(ar, SOC_GLOBAL_RESET_ADDRESS);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002716 val |= 1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002717 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002718
2719 for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
Kalle Valoe479ed42013-09-01 10:01:53 +03002720 if (ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03002721 RTC_STATE_COLD_RESET_MASK)
2722 break;
2723 msleep(1);
2724 }
2725
2726 /* Pull Target, including PCIe, out of RESET. */
2727 val &= ~1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002728 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002729
2730 for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
Kalle Valoe479ed42013-09-01 10:01:53 +03002731 if (!(ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03002732 RTC_STATE_COLD_RESET_MASK))
2733 break;
2734 msleep(1);
2735 }
2736
Michal Kazior7aa7a722014-08-25 12:09:38 +02002737 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cold reset complete\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02002738
Michal Kazior5b2589f2013-11-08 08:01:30 +01002739 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002740}
2741
Michal Kazior2986e3e2014-08-07 11:03:30 +02002742static int ath10k_pci_claim(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002743{
Michal Kazior2986e3e2014-08-07 11:03:30 +02002744 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2745 struct pci_dev *pdev = ar_pci->pdev;
Michal Kazior2986e3e2014-08-07 11:03:30 +02002746 int ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002747
2748 pci_set_drvdata(pdev, ar);
2749
Kalle Valo5e3dd152013-06-12 20:52:10 +03002750 ret = pci_enable_device(pdev);
2751 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002752 ath10k_err(ar, "failed to enable pci device: %d\n", ret);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002753 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002754 }
2755
Kalle Valo5e3dd152013-06-12 20:52:10 +03002756 ret = pci_request_region(pdev, BAR_NUM, "ath");
2757 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002758 ath10k_err(ar, "failed to request region BAR%d: %d\n", BAR_NUM,
Michal Kazior2986e3e2014-08-07 11:03:30 +02002759 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002760 goto err_device;
2761 }
2762
Michal Kazior2986e3e2014-08-07 11:03:30 +02002763 /* Target expects 32 bit DMA. Enforce it. */
Kalle Valo5e3dd152013-06-12 20:52:10 +03002764 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2765 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002766 ath10k_err(ar, "failed to set dma mask to 32-bit: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002767 goto err_region;
2768 }
2769
2770 ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2771 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002772 ath10k_err(ar, "failed to set consistent dma mask to 32-bit: %d\n",
Michal Kazior2986e3e2014-08-07 11:03:30 +02002773 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002774 goto err_region;
2775 }
2776
Kalle Valo5e3dd152013-06-12 20:52:10 +03002777 pci_set_master(pdev);
2778
Kalle Valo5e3dd152013-06-12 20:52:10 +03002779 /* Arrange for access to Target SoC registers. */
Michal Kazioraeae5b42015-06-15 14:46:42 +03002780 ar_pci->mem_len = pci_resource_len(pdev, BAR_NUM);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002781 ar_pci->mem = pci_iomap(pdev, BAR_NUM, 0);
2782 if (!ar_pci->mem) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002783 ath10k_err(ar, "failed to iomap BAR%d\n", BAR_NUM);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002784 ret = -EIO;
2785 goto err_master;
2786 }
2787
Michal Kazior7aa7a722014-08-25 12:09:38 +02002788 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot pci_mem 0x%p\n", ar_pci->mem);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002789 return 0;
2790
2791err_master:
2792 pci_clear_master(pdev);
2793
2794err_region:
2795 pci_release_region(pdev, BAR_NUM);
2796
2797err_device:
2798 pci_disable_device(pdev);
2799
2800 return ret;
2801}
2802
2803static void ath10k_pci_release(struct ath10k *ar)
2804{
2805 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2806 struct pci_dev *pdev = ar_pci->pdev;
2807
2808 pci_iounmap(pdev, ar_pci->mem);
2809 pci_release_region(pdev, BAR_NUM);
2810 pci_clear_master(pdev);
2811 pci_disable_device(pdev);
2812}
2813
Michal Kazior7505f7c2014-12-02 10:55:54 +02002814static bool ath10k_pci_chip_is_supported(u32 dev_id, u32 chip_id)
2815{
2816 const struct ath10k_pci_supp_chip *supp_chip;
2817 int i;
2818 u32 rev_id = MS(chip_id, SOC_CHIP_ID_REV);
2819
2820 for (i = 0; i < ARRAY_SIZE(ath10k_pci_supp_chips); i++) {
2821 supp_chip = &ath10k_pci_supp_chips[i];
2822
2823 if (supp_chip->dev_id == dev_id &&
2824 supp_chip->rev_id == rev_id)
2825 return true;
2826 }
2827
2828 return false;
2829}
2830
Kalle Valo5e3dd152013-06-12 20:52:10 +03002831static int ath10k_pci_probe(struct pci_dev *pdev,
2832 const struct pci_device_id *pci_dev)
2833{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002834 int ret = 0;
2835 struct ath10k *ar;
2836 struct ath10k_pci *ar_pci;
Michal Kaziord63955b2015-01-24 12:14:49 +02002837 enum ath10k_hw_rev hw_rev;
Michal Kazior2986e3e2014-08-07 11:03:30 +02002838 u32 chip_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002839
Michal Kaziord63955b2015-01-24 12:14:49 +02002840 switch (pci_dev->device) {
2841 case QCA988X_2_0_DEVICE_ID:
2842 hw_rev = ATH10K_HW_QCA988X;
2843 break;
2844 case QCA6174_2_1_DEVICE_ID:
2845 hw_rev = ATH10K_HW_QCA6174;
2846 break;
Vasanthakumar Thiagarajan8bd47022015-06-18 12:31:03 +05302847 case QCA99X0_2_0_DEVICE_ID:
2848 hw_rev = ATH10K_HW_QCA99X0;
2849 break;
Michal Kaziord63955b2015-01-24 12:14:49 +02002850 default:
2851 WARN_ON(1);
2852 return -ENOTSUPP;
2853 }
2854
2855 ar = ath10k_core_create(sizeof(*ar_pci), &pdev->dev, ATH10K_BUS_PCI,
2856 hw_rev, &ath10k_pci_hif_ops);
Michal Kaziore7b54192014-08-07 11:03:27 +02002857 if (!ar) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002858 dev_err(&pdev->dev, "failed to allocate core\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002859 return -ENOMEM;
Michal Kaziore7b54192014-08-07 11:03:27 +02002860 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002861
Michal Kazior7aa7a722014-08-25 12:09:38 +02002862 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci probe\n");
2863
Michal Kaziore7b54192014-08-07 11:03:27 +02002864 ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002865 ar_pci->pdev = pdev;
2866 ar_pci->dev = &pdev->dev;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002867 ar_pci->ar = ar;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002868
Michal Kaziorde57e2c2015-04-17 09:19:17 +00002869 if (pdev->subsystem_vendor || pdev->subsystem_device)
2870 scnprintf(ar->spec_board_id, sizeof(ar->spec_board_id),
2871 "%04x:%04x:%04x:%04x",
2872 pdev->vendor, pdev->device,
2873 pdev->subsystem_vendor, pdev->subsystem_device);
2874
Kalle Valo5e3dd152013-06-12 20:52:10 +03002875 spin_lock_init(&ar_pci->ce_lock);
Michal Kazior77258d42015-05-18 09:38:18 +00002876 spin_lock_init(&ar_pci->ps_lock);
2877
Michal Kazior728f95e2014-08-22 14:33:14 +02002878 setup_timer(&ar_pci->rx_post_retry, ath10k_pci_rx_replenish_retry,
2879 (unsigned long)ar);
Michal Kazior77258d42015-05-18 09:38:18 +00002880 setup_timer(&ar_pci->ps_timer, ath10k_pci_ps_timer,
2881 (unsigned long)ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002882
Michal Kazior2986e3e2014-08-07 11:03:30 +02002883 ret = ath10k_pci_claim(ar);
Kalle Valoe01ae682013-09-01 11:22:14 +03002884 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002885 ath10k_err(ar, "failed to claim device: %d\n", ret);
Michal Kaziore7b54192014-08-07 11:03:27 +02002886 goto err_core_destroy;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002887 }
2888
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002889 ret = ath10k_pci_alloc_pipes(ar);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002890 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002891 ath10k_err(ar, "failed to allocate copy engine pipes: %d\n",
2892 ret);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002893 goto err_sleep;
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002894 }
2895
Michal Kazior403d6272014-08-22 14:23:31 +02002896 ath10k_pci_ce_deinit(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02002897 ath10k_pci_irq_disable(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02002898
Michal Kazior403d6272014-08-22 14:23:31 +02002899 ret = ath10k_pci_init_irq(ar);
2900 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002901 ath10k_err(ar, "failed to init irqs: %d\n", ret);
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002902 goto err_free_pipes;
Michal Kazior403d6272014-08-22 14:23:31 +02002903 }
2904
Michal Kazior7aa7a722014-08-25 12:09:38 +02002905 ath10k_info(ar, "pci irq %s interrupts %d irq_mode %d reset_mode %d\n",
Michal Kazior403d6272014-08-22 14:23:31 +02002906 ath10k_pci_get_irq_method(ar), ar_pci->num_msi_intrs,
2907 ath10k_pci_irq_mode, ath10k_pci_reset_mode);
2908
Michal Kazior5c771e72014-08-22 14:23:34 +02002909 ret = ath10k_pci_request_irq(ar);
Michal Kazior403d6272014-08-22 14:23:31 +02002910 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002911 ath10k_warn(ar, "failed to request irqs: %d\n", ret);
Michal Kazior403d6272014-08-22 14:23:31 +02002912 goto err_deinit_irq;
2913 }
2914
Michal Kazior1a7fecb2015-01-24 12:14:48 +02002915 ret = ath10k_pci_chip_reset(ar);
2916 if (ret) {
2917 ath10k_err(ar, "failed to reset chip: %d\n", ret);
2918 goto err_free_irq;
2919 }
2920
2921 chip_id = ath10k_pci_soc_read32(ar, SOC_CHIP_ID_ADDRESS);
2922 if (chip_id == 0xffffffff) {
2923 ath10k_err(ar, "failed to get chip id\n");
2924 goto err_free_irq;
2925 }
2926
2927 if (!ath10k_pci_chip_is_supported(pdev->device, chip_id)) {
2928 ath10k_err(ar, "device %04x with chip_id %08x isn't supported\n",
2929 pdev->device, chip_id);
Michal Kaziord9585a92015-04-10 13:01:27 +00002930 goto err_free_irq;
Michal Kazior1a7fecb2015-01-24 12:14:48 +02002931 }
2932
Kalle Valoe01ae682013-09-01 11:22:14 +03002933 ret = ath10k_core_register(ar, chip_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002934 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002935 ath10k_err(ar, "failed to register driver core: %d\n", ret);
Michal Kazior5c771e72014-08-22 14:23:34 +02002936 goto err_free_irq;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002937 }
2938
2939 return 0;
2940
Michal Kazior5c771e72014-08-22 14:23:34 +02002941err_free_irq:
2942 ath10k_pci_free_irq(ar);
Michal Kazior21396272014-08-28 10:24:40 +02002943 ath10k_pci_kill_tasklet(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02002944
Michal Kazior403d6272014-08-22 14:23:31 +02002945err_deinit_irq:
2946 ath10k_pci_deinit_irq(ar);
2947
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002948err_free_pipes:
2949 ath10k_pci_free_pipes(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002950
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002951err_sleep:
Michal Kazior0bcbbe62015-05-29 07:35:24 +02002952 ath10k_pci_sleep_sync(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002953 ath10k_pci_release(ar);
2954
Michal Kaziore7b54192014-08-07 11:03:27 +02002955err_core_destroy:
Kalle Valo5e3dd152013-06-12 20:52:10 +03002956 ath10k_core_destroy(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002957
2958 return ret;
2959}
2960
2961static void ath10k_pci_remove(struct pci_dev *pdev)
2962{
2963 struct ath10k *ar = pci_get_drvdata(pdev);
2964 struct ath10k_pci *ar_pci;
2965
Michal Kazior7aa7a722014-08-25 12:09:38 +02002966 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci remove\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002967
2968 if (!ar)
2969 return;
2970
2971 ar_pci = ath10k_pci_priv(ar);
2972
2973 if (!ar_pci)
2974 return;
2975
Kalle Valo5e3dd152013-06-12 20:52:10 +03002976 ath10k_core_unregister(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02002977 ath10k_pci_free_irq(ar);
Michal Kazior21396272014-08-28 10:24:40 +02002978 ath10k_pci_kill_tasklet(ar);
Michal Kazior403d6272014-08-22 14:23:31 +02002979 ath10k_pci_deinit_irq(ar);
2980 ath10k_pci_ce_deinit(ar);
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002981 ath10k_pci_free_pipes(ar);
Michal Kazior77258d42015-05-18 09:38:18 +00002982 ath10k_pci_sleep_sync(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002983 ath10k_pci_release(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002984 ath10k_core_destroy(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002985}
2986
Kalle Valo5e3dd152013-06-12 20:52:10 +03002987MODULE_DEVICE_TABLE(pci, ath10k_pci_id_table);
2988
2989static struct pci_driver ath10k_pci_driver = {
2990 .name = "ath10k_pci",
2991 .id_table = ath10k_pci_id_table,
2992 .probe = ath10k_pci_probe,
2993 .remove = ath10k_pci_remove,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002994};
2995
2996static int __init ath10k_pci_init(void)
2997{
2998 int ret;
2999
3000 ret = pci_register_driver(&ath10k_pci_driver);
3001 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003002 printk(KERN_ERR "failed to register ath10k pci driver: %d\n",
3003 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003004
3005 return ret;
3006}
3007module_init(ath10k_pci_init);
3008
3009static void __exit ath10k_pci_exit(void)
3010{
3011 pci_unregister_driver(&ath10k_pci_driver);
3012}
3013
3014module_exit(ath10k_pci_exit);
3015
3016MODULE_AUTHOR("Qualcomm Atheros");
3017MODULE_DESCRIPTION("Driver support for Atheros QCA988X PCIe devices");
3018MODULE_LICENSE("Dual BSD/GPL");
Bartosz Markowski5c427f52015-02-18 13:16:37 +01003019
3020/* QCA988x 2.0 firmware files */
Bartosz Markowski8026cae2014-10-06 14:16:41 +02003021MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_FW_FILE);
3022MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API2_FILE);
3023MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API3_FILE);
Bartosz Markowski5c427f52015-02-18 13:16:37 +01003024MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API4_FILE);
Kalle Valo53513c32015-03-25 13:12:42 +02003025MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API5_FILE);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003026MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_BOARD_DATA_FILE);
Bartosz Markowski5c427f52015-02-18 13:16:37 +01003027
3028/* QCA6174 2.1 firmware files */
3029MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" ATH10K_FW_API4_FILE);
Michal Kaziore451c1d2015-05-26 13:09:22 +02003030MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" ATH10K_FW_API5_FILE);
Bartosz Markowski5c427f52015-02-18 13:16:37 +01003031MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" QCA6174_HW_2_1_BOARD_DATA_FILE);
3032
3033/* QCA6174 3.1 firmware files */
3034MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" ATH10K_FW_API4_FILE);
Michal Kaziore451c1d2015-05-26 13:09:22 +02003035MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" ATH10K_FW_API5_FILE);
Bartosz Markowski5c427f52015-02-18 13:16:37 +01003036MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" QCA6174_HW_3_0_BOARD_DATA_FILE);