blob: 7af39118b1f4af4427288b997dfa976a115d1a20 [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 Kazior36582e52015-08-13 14:32:26 +020061#define QCA6164_2_1_DEVICE_ID (0x0041)
Michal Kaziord63955b2015-01-24 12:14:49 +020062#define QCA6174_2_1_DEVICE_ID (0x003e)
Vasanthakumar Thiagarajan8bd47022015-06-18 12:31:03 +053063#define QCA99X0_2_0_DEVICE_ID (0x0040)
Kalle Valo5e3dd152013-06-12 20:52:10 +030064
Benoit Taine9baa3c32014-08-08 15:56:03 +020065static const struct pci_device_id ath10k_pci_id_table[] = {
Kalle Valo5e3dd152013-06-12 20:52:10 +030066 { PCI_VDEVICE(ATHEROS, QCA988X_2_0_DEVICE_ID) }, /* PCI-E QCA988X V2 */
Michal Kazior36582e52015-08-13 14:32:26 +020067 { PCI_VDEVICE(ATHEROS, QCA6164_2_1_DEVICE_ID) }, /* PCI-E QCA6164 V2.1 */
Michal Kaziord63955b2015-01-24 12:14:49 +020068 { PCI_VDEVICE(ATHEROS, QCA6174_2_1_DEVICE_ID) }, /* PCI-E QCA6174 V2.1 */
Vasanthakumar Thiagarajan8a055a82015-07-29 11:40:39 +030069 { PCI_VDEVICE(ATHEROS, QCA99X0_2_0_DEVICE_ID) }, /* PCI-E QCA99X0 V2 */
Kalle Valo5e3dd152013-06-12 20:52:10 +030070 {0}
71};
72
Michal Kazior7505f7c2014-12-02 10:55:54 +020073static const struct ath10k_pci_supp_chip ath10k_pci_supp_chips[] = {
74 /* QCA988X pre 2.0 chips are not supported because they need some nasty
75 * hacks. ath10k doesn't have them and these devices crash horribly
76 * because of that.
77 */
78 { QCA988X_2_0_DEVICE_ID, QCA988X_HW_2_0_CHIP_ID_REV },
Michal Kazior36582e52015-08-13 14:32:26 +020079
80 { QCA6164_2_1_DEVICE_ID, QCA6174_HW_2_1_CHIP_ID_REV },
81 { QCA6164_2_1_DEVICE_ID, QCA6174_HW_2_2_CHIP_ID_REV },
82 { QCA6164_2_1_DEVICE_ID, QCA6174_HW_3_0_CHIP_ID_REV },
83 { QCA6164_2_1_DEVICE_ID, QCA6174_HW_3_1_CHIP_ID_REV },
84 { QCA6164_2_1_DEVICE_ID, QCA6174_HW_3_2_CHIP_ID_REV },
85
Michal Kaziord63955b2015-01-24 12:14:49 +020086 { QCA6174_2_1_DEVICE_ID, QCA6174_HW_2_1_CHIP_ID_REV },
87 { QCA6174_2_1_DEVICE_ID, QCA6174_HW_2_2_CHIP_ID_REV },
88 { QCA6174_2_1_DEVICE_ID, QCA6174_HW_3_0_CHIP_ID_REV },
89 { QCA6174_2_1_DEVICE_ID, QCA6174_HW_3_1_CHIP_ID_REV },
90 { QCA6174_2_1_DEVICE_ID, QCA6174_HW_3_2_CHIP_ID_REV },
Michal Kazior36582e52015-08-13 14:32:26 +020091
Vasanthakumar Thiagarajan8a055a82015-07-29 11:40:39 +030092 { QCA99X0_2_0_DEVICE_ID, QCA99X0_HW_2_0_CHIP_ID_REV },
Michal Kazior7505f7c2014-12-02 10:55:54 +020093};
94
Michal Kazior728f95e2014-08-22 14:33:14 +020095static void ath10k_pci_buffer_cleanup(struct ath10k *ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +010096static int ath10k_pci_cold_reset(struct ath10k *ar);
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +053097static int ath10k_pci_safe_chip_reset(struct ath10k *ar);
Michal Kaziord7fb47f2013-11-08 08:01:26 +010098static int ath10k_pci_wait_for_target_init(struct ath10k *ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +010099static int ath10k_pci_init_irq(struct ath10k *ar);
100static int ath10k_pci_deinit_irq(struct ath10k *ar);
101static int ath10k_pci_request_irq(struct ath10k *ar);
102static void ath10k_pci_free_irq(struct ath10k *ar);
Michal Kazior85622cd2013-11-25 14:06:22 +0100103static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
104 struct ath10k_ce_pipe *rx_pipe,
105 struct bmi_xfer *xfer);
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +0530106static int ath10k_pci_qca99x0_chip_reset(struct ath10k *ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300107
108static const struct ce_attr host_ce_config_wlan[] = {
Kalle Valo48e9c222013-09-01 10:01:32 +0300109 /* CE0: host->target HTC control and raw streams */
110 {
111 .flags = CE_ATTR_FLAGS,
112 .src_nentries = 16,
113 .src_sz_max = 256,
114 .dest_nentries = 0,
115 },
116
117 /* CE1: target->host HTT + HTC control */
118 {
119 .flags = CE_ATTR_FLAGS,
120 .src_nentries = 0,
Michal Kazior63838642015-02-09 15:04:55 +0100121 .src_sz_max = 2048,
Kalle Valo48e9c222013-09-01 10:01:32 +0300122 .dest_nentries = 512,
123 },
124
125 /* CE2: target->host WMI */
126 {
127 .flags = CE_ATTR_FLAGS,
128 .src_nentries = 0,
129 .src_sz_max = 2048,
Rajkumar Manoharan30abb332015-03-04 15:43:44 +0200130 .dest_nentries = 128,
Kalle Valo48e9c222013-09-01 10:01:32 +0300131 },
132
133 /* CE3: host->target WMI */
134 {
135 .flags = CE_ATTR_FLAGS,
136 .src_nentries = 32,
137 .src_sz_max = 2048,
138 .dest_nentries = 0,
139 },
140
141 /* CE4: host->target HTT */
142 {
143 .flags = CE_ATTR_FLAGS | CE_ATTR_DIS_INTR,
144 .src_nentries = CE_HTT_H2T_MSG_SRC_NENTRIES,
145 .src_sz_max = 256,
146 .dest_nentries = 0,
147 },
148
149 /* CE5: unused */
150 {
151 .flags = CE_ATTR_FLAGS,
152 .src_nentries = 0,
153 .src_sz_max = 0,
154 .dest_nentries = 0,
155 },
156
157 /* CE6: target autonomous hif_memcpy */
158 {
159 .flags = CE_ATTR_FLAGS,
160 .src_nentries = 0,
161 .src_sz_max = 0,
162 .dest_nentries = 0,
163 },
164
165 /* CE7: ce_diag, the Diagnostic Window */
166 {
167 .flags = CE_ATTR_FLAGS,
168 .src_nentries = 2,
169 .src_sz_max = DIAG_TRANSFER_LIMIT,
170 .dest_nentries = 2,
171 },
Vasanthakumar Thiagarajan050af062015-06-18 12:31:04 +0530172
173 /* CE8: target->host pktlog */
174 {
175 .flags = CE_ATTR_FLAGS,
176 .src_nentries = 0,
177 .src_sz_max = 2048,
178 .dest_nentries = 128,
179 },
180
181 /* CE9 target autonomous qcache memcpy */
182 {
183 .flags = CE_ATTR_FLAGS,
184 .src_nentries = 0,
185 .src_sz_max = 0,
186 .dest_nentries = 0,
187 },
188
189 /* CE10: target autonomous hif memcpy */
190 {
191 .flags = CE_ATTR_FLAGS,
192 .src_nentries = 0,
193 .src_sz_max = 0,
194 .dest_nentries = 0,
195 },
196
197 /* CE11: target autonomous hif memcpy */
198 {
199 .flags = CE_ATTR_FLAGS,
200 .src_nentries = 0,
201 .src_sz_max = 0,
202 .dest_nentries = 0,
203 },
Kalle Valo5e3dd152013-06-12 20:52:10 +0300204};
205
206/* Target firmware's Copy Engine configuration. */
207static const struct ce_pipe_config target_ce_config_wlan[] = {
Kalle Valod88effb2013-09-01 10:01:39 +0300208 /* CE0: host->target HTC control and raw streams */
209 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300210 .pipenum = __cpu_to_le32(0),
211 .pipedir = __cpu_to_le32(PIPEDIR_OUT),
212 .nentries = __cpu_to_le32(32),
213 .nbytes_max = __cpu_to_le32(256),
214 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
215 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300216 },
217
218 /* CE1: target->host HTT + HTC control */
219 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300220 .pipenum = __cpu_to_le32(1),
221 .pipedir = __cpu_to_le32(PIPEDIR_IN),
222 .nentries = __cpu_to_le32(32),
Michal Kazior63838642015-02-09 15:04:55 +0100223 .nbytes_max = __cpu_to_le32(2048),
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300224 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
225 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300226 },
227
228 /* CE2: target->host WMI */
229 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300230 .pipenum = __cpu_to_le32(2),
231 .pipedir = __cpu_to_le32(PIPEDIR_IN),
Rajkumar Manoharan30abb332015-03-04 15:43:44 +0200232 .nentries = __cpu_to_le32(64),
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300233 .nbytes_max = __cpu_to_le32(2048),
234 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
235 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300236 },
237
238 /* CE3: host->target WMI */
239 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300240 .pipenum = __cpu_to_le32(3),
241 .pipedir = __cpu_to_le32(PIPEDIR_OUT),
242 .nentries = __cpu_to_le32(32),
243 .nbytes_max = __cpu_to_le32(2048),
244 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
245 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300246 },
247
248 /* CE4: host->target HTT */
249 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300250 .pipenum = __cpu_to_le32(4),
251 .pipedir = __cpu_to_le32(PIPEDIR_OUT),
252 .nentries = __cpu_to_le32(256),
253 .nbytes_max = __cpu_to_le32(256),
254 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
255 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300256 },
257
Kalle Valo5e3dd152013-06-12 20:52:10 +0300258 /* NB: 50% of src nentries, since tx has 2 frags */
Kalle Valod88effb2013-09-01 10:01:39 +0300259
260 /* CE5: unused */
261 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300262 .pipenum = __cpu_to_le32(5),
263 .pipedir = __cpu_to_le32(PIPEDIR_OUT),
264 .nentries = __cpu_to_le32(32),
265 .nbytes_max = __cpu_to_le32(2048),
266 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
267 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300268 },
269
270 /* CE6: Reserved for target autonomous hif_memcpy */
271 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300272 .pipenum = __cpu_to_le32(6),
273 .pipedir = __cpu_to_le32(PIPEDIR_INOUT),
274 .nentries = __cpu_to_le32(32),
275 .nbytes_max = __cpu_to_le32(4096),
276 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
277 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300278 },
279
Kalle Valo5e3dd152013-06-12 20:52:10 +0300280 /* CE7 used only by Host */
Vasanthakumar Thiagarajan050af062015-06-18 12:31:04 +0530281 {
282 .pipenum = __cpu_to_le32(7),
283 .pipedir = __cpu_to_le32(PIPEDIR_INOUT),
284 .nentries = __cpu_to_le32(0),
285 .nbytes_max = __cpu_to_le32(0),
286 .flags = __cpu_to_le32(0),
287 .reserved = __cpu_to_le32(0),
288 },
289
290 /* CE8 target->host packtlog */
291 {
292 .pipenum = __cpu_to_le32(8),
293 .pipedir = __cpu_to_le32(PIPEDIR_IN),
294 .nentries = __cpu_to_le32(64),
295 .nbytes_max = __cpu_to_le32(2048),
296 .flags = __cpu_to_le32(CE_ATTR_FLAGS | CE_ATTR_DIS_INTR),
297 .reserved = __cpu_to_le32(0),
298 },
299
300 /* CE9 target autonomous qcache memcpy */
301 {
302 .pipenum = __cpu_to_le32(9),
303 .pipedir = __cpu_to_le32(PIPEDIR_INOUT),
304 .nentries = __cpu_to_le32(32),
305 .nbytes_max = __cpu_to_le32(2048),
306 .flags = __cpu_to_le32(CE_ATTR_FLAGS | CE_ATTR_DIS_INTR),
307 .reserved = __cpu_to_le32(0),
308 },
309
310 /* It not necessary to send target wlan configuration for CE10 & CE11
311 * as these CEs are not actively used in target.
312 */
Kalle Valo5e3dd152013-06-12 20:52:10 +0300313};
314
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300315/*
316 * Map from service/endpoint to Copy Engine.
317 * This table is derived from the CE_PCI TABLE, above.
318 * It is passed to the Target at startup for use by firmware.
319 */
320static const struct service_to_pipe target_service_to_ce_map_wlan[] = {
321 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300322 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VO),
323 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
324 __cpu_to_le32(3),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300325 },
326 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300327 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VO),
328 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
329 __cpu_to_le32(2),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300330 },
331 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300332 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BK),
333 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
334 __cpu_to_le32(3),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300335 },
336 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300337 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BK),
338 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
339 __cpu_to_le32(2),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300340 },
341 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300342 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BE),
343 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
344 __cpu_to_le32(3),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300345 },
346 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300347 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BE),
348 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
349 __cpu_to_le32(2),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300350 },
351 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300352 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VI),
353 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
354 __cpu_to_le32(3),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300355 },
356 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300357 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VI),
358 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
359 __cpu_to_le32(2),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300360 },
361 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300362 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_CONTROL),
363 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
364 __cpu_to_le32(3),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300365 },
366 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300367 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_CONTROL),
368 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
369 __cpu_to_le32(2),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300370 },
371 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300372 __cpu_to_le32(ATH10K_HTC_SVC_ID_RSVD_CTRL),
373 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
374 __cpu_to_le32(0),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300375 },
376 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300377 __cpu_to_le32(ATH10K_HTC_SVC_ID_RSVD_CTRL),
378 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
379 __cpu_to_le32(1),
380 },
381 { /* not used */
382 __cpu_to_le32(ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS),
383 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
384 __cpu_to_le32(0),
385 },
386 { /* not used */
387 __cpu_to_le32(ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS),
388 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
389 __cpu_to_le32(1),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300390 },
391 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300392 __cpu_to_le32(ATH10K_HTC_SVC_ID_HTT_DATA_MSG),
393 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
394 __cpu_to_le32(4),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300395 },
396 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300397 __cpu_to_le32(ATH10K_HTC_SVC_ID_HTT_DATA_MSG),
398 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
399 __cpu_to_le32(1),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300400 },
401
402 /* (Additions here) */
403
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300404 { /* must be last */
405 __cpu_to_le32(0),
406 __cpu_to_le32(0),
407 __cpu_to_le32(0),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300408 },
409};
410
Michal Kazior77258d42015-05-18 09:38:18 +0000411static bool ath10k_pci_is_awake(struct ath10k *ar)
412{
413 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
414 u32 val = ioread32(ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
415 RTC_STATE_ADDRESS);
416
417 return RTC_STATE_V_GET(val) == RTC_STATE_V_ON;
418}
419
420static void __ath10k_pci_wake(struct ath10k *ar)
421{
422 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
423
424 lockdep_assert_held(&ar_pci->ps_lock);
425
426 ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps wake reg refcount %lu awake %d\n",
427 ar_pci->ps_wake_refcount, ar_pci->ps_awake);
428
429 iowrite32(PCIE_SOC_WAKE_V_MASK,
430 ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
431 PCIE_SOC_WAKE_ADDRESS);
432}
433
434static void __ath10k_pci_sleep(struct ath10k *ar)
435{
436 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
437
438 lockdep_assert_held(&ar_pci->ps_lock);
439
440 ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps sleep reg refcount %lu awake %d\n",
441 ar_pci->ps_wake_refcount, ar_pci->ps_awake);
442
443 iowrite32(PCIE_SOC_WAKE_RESET,
444 ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
445 PCIE_SOC_WAKE_ADDRESS);
446 ar_pci->ps_awake = false;
447}
448
449static int ath10k_pci_wake_wait(struct ath10k *ar)
450{
451 int tot_delay = 0;
452 int curr_delay = 5;
453
454 while (tot_delay < PCIE_WAKE_TIMEOUT) {
Maharaja Kennadyrajan39b91b82015-10-06 15:19:28 +0300455 if (ath10k_pci_is_awake(ar)) {
456 if (tot_delay > PCIE_WAKE_LATE_US)
457 ath10k_warn(ar, "device wakeup took %d ms which is unusally long, otherwise it works normally.\n",
458 tot_delay / 1000);
Michal Kazior77258d42015-05-18 09:38:18 +0000459 return 0;
Maharaja Kennadyrajan39b91b82015-10-06 15:19:28 +0300460 }
Michal Kazior77258d42015-05-18 09:38:18 +0000461
462 udelay(curr_delay);
463 tot_delay += curr_delay;
464
465 if (curr_delay < 50)
466 curr_delay += 5;
467 }
468
469 return -ETIMEDOUT;
470}
471
472static int ath10k_pci_wake(struct ath10k *ar)
473{
474 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
475 unsigned long flags;
476 int ret = 0;
477
478 spin_lock_irqsave(&ar_pci->ps_lock, flags);
479
480 ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps wake refcount %lu awake %d\n",
481 ar_pci->ps_wake_refcount, ar_pci->ps_awake);
482
483 /* This function can be called very frequently. To avoid excessive
484 * CPU stalls for MMIO reads use a cache var to hold the device state.
485 */
486 if (!ar_pci->ps_awake) {
487 __ath10k_pci_wake(ar);
488
489 ret = ath10k_pci_wake_wait(ar);
490 if (ret == 0)
491 ar_pci->ps_awake = true;
492 }
493
494 if (ret == 0) {
495 ar_pci->ps_wake_refcount++;
496 WARN_ON(ar_pci->ps_wake_refcount == 0);
497 }
498
499 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
500
501 return ret;
502}
503
504static void ath10k_pci_sleep(struct ath10k *ar)
505{
506 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
507 unsigned long flags;
508
509 spin_lock_irqsave(&ar_pci->ps_lock, flags);
510
511 ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps sleep refcount %lu awake %d\n",
512 ar_pci->ps_wake_refcount, ar_pci->ps_awake);
513
514 if (WARN_ON(ar_pci->ps_wake_refcount == 0))
515 goto skip;
516
517 ar_pci->ps_wake_refcount--;
518
519 mod_timer(&ar_pci->ps_timer, jiffies +
520 msecs_to_jiffies(ATH10K_PCI_SLEEP_GRACE_PERIOD_MSEC));
521
522skip:
523 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
524}
525
526static void ath10k_pci_ps_timer(unsigned long ptr)
527{
528 struct ath10k *ar = (void *)ptr;
529 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
530 unsigned long flags;
531
532 spin_lock_irqsave(&ar_pci->ps_lock, flags);
533
534 ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps timer refcount %lu awake %d\n",
535 ar_pci->ps_wake_refcount, ar_pci->ps_awake);
536
537 if (ar_pci->ps_wake_refcount > 0)
538 goto skip;
539
540 __ath10k_pci_sleep(ar);
541
542skip:
543 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
544}
545
546static void ath10k_pci_sleep_sync(struct ath10k *ar)
547{
548 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
549 unsigned long flags;
550
551 del_timer_sync(&ar_pci->ps_timer);
552
553 spin_lock_irqsave(&ar_pci->ps_lock, flags);
554 WARN_ON(ar_pci->ps_wake_refcount > 0);
555 __ath10k_pci_sleep(ar);
556 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
557}
558
559void ath10k_pci_write32(struct ath10k *ar, u32 offset, u32 value)
560{
561 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
562 int ret;
563
Michal Kazioraeae5b42015-06-15 14:46:42 +0300564 if (unlikely(offset + sizeof(value) > ar_pci->mem_len)) {
565 ath10k_warn(ar, "refusing to write mmio out of bounds at 0x%08x - 0x%08zx (max 0x%08zx)\n",
566 offset, offset + sizeof(value), ar_pci->mem_len);
567 return;
568 }
569
Michal Kazior77258d42015-05-18 09:38:18 +0000570 ret = ath10k_pci_wake(ar);
571 if (ret) {
572 ath10k_warn(ar, "failed to wake target for write32 of 0x%08x at 0x%08x: %d\n",
573 value, offset, ret);
574 return;
575 }
576
577 iowrite32(value, ar_pci->mem + offset);
578 ath10k_pci_sleep(ar);
579}
580
581u32 ath10k_pci_read32(struct ath10k *ar, u32 offset)
582{
583 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
584 u32 val;
585 int ret;
586
Michal Kazioraeae5b42015-06-15 14:46:42 +0300587 if (unlikely(offset + sizeof(val) > ar_pci->mem_len)) {
588 ath10k_warn(ar, "refusing to read mmio out of bounds at 0x%08x - 0x%08zx (max 0x%08zx)\n",
589 offset, offset + sizeof(val), ar_pci->mem_len);
590 return 0;
591 }
592
Michal Kazior77258d42015-05-18 09:38:18 +0000593 ret = ath10k_pci_wake(ar);
594 if (ret) {
595 ath10k_warn(ar, "failed to wake target for read32 at 0x%08x: %d\n",
596 offset, ret);
597 return 0xffffffff;
598 }
599
600 val = ioread32(ar_pci->mem + offset);
601 ath10k_pci_sleep(ar);
602
603 return val;
604}
605
606u32 ath10k_pci_soc_read32(struct ath10k *ar, u32 addr)
607{
608 return ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS + addr);
609}
610
611void ath10k_pci_soc_write32(struct ath10k *ar, u32 addr, u32 val)
612{
613 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + addr, val);
614}
615
616u32 ath10k_pci_reg_read32(struct ath10k *ar, u32 addr)
617{
618 return ath10k_pci_read32(ar, PCIE_LOCAL_BASE_ADDRESS + addr);
619}
620
621void ath10k_pci_reg_write32(struct ath10k *ar, u32 addr, u32 val)
622{
623 ath10k_pci_write32(ar, PCIE_LOCAL_BASE_ADDRESS + addr, val);
624}
625
Michal Kaziore5398872013-11-25 14:06:20 +0100626static bool ath10k_pci_irq_pending(struct ath10k *ar)
627{
628 u32 cause;
629
630 /* Check if the shared legacy irq is for us */
631 cause = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
632 PCIE_INTR_CAUSE_ADDRESS);
633 if (cause & (PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL))
634 return true;
635
636 return false;
637}
638
Michal Kazior26852182013-11-25 14:06:25 +0100639static void ath10k_pci_disable_and_clear_legacy_irq(struct ath10k *ar)
640{
641 /* IMPORTANT: INTR_CLR register has to be set after
642 * INTR_ENABLE is set to 0, otherwise interrupt can not be
643 * really cleared. */
644 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
645 0);
646 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_CLR_ADDRESS,
647 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
648
649 /* IMPORTANT: this extra read transaction is required to
650 * flush the posted write buffer. */
Kalle Valocfbc06a2014-09-14 12:50:23 +0300651 (void)ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
652 PCIE_INTR_ENABLE_ADDRESS);
Michal Kazior26852182013-11-25 14:06:25 +0100653}
654
655static void ath10k_pci_enable_legacy_irq(struct ath10k *ar)
656{
657 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
658 PCIE_INTR_ENABLE_ADDRESS,
659 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
660
661 /* IMPORTANT: this extra read transaction is required to
662 * flush the posted write buffer. */
Kalle Valocfbc06a2014-09-14 12:50:23 +0300663 (void)ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
664 PCIE_INTR_ENABLE_ADDRESS);
Michal Kazior26852182013-11-25 14:06:25 +0100665}
666
Michal Kazior403d6272014-08-22 14:23:31 +0200667static inline const char *ath10k_pci_get_irq_method(struct ath10k *ar)
Michal Kaziorab977bd2013-11-25 14:06:26 +0100668{
Michal Kaziorab977bd2013-11-25 14:06:26 +0100669 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
670
Michal Kazior403d6272014-08-22 14:23:31 +0200671 if (ar_pci->num_msi_intrs > 1)
672 return "msi-x";
Kalle Valod8bb26b2014-09-14 12:50:33 +0300673
674 if (ar_pci->num_msi_intrs == 1)
Michal Kazior403d6272014-08-22 14:23:31 +0200675 return "msi";
Kalle Valod8bb26b2014-09-14 12:50:33 +0300676
677 return "legacy";
Michal Kaziorab977bd2013-11-25 14:06:26 +0100678}
679
Michal Kazior728f95e2014-08-22 14:33:14 +0200680static int __ath10k_pci_rx_post_buf(struct ath10k_pci_pipe *pipe)
Michal Kaziorab977bd2013-11-25 14:06:26 +0100681{
Michal Kazior728f95e2014-08-22 14:33:14 +0200682 struct ath10k *ar = pipe->hif_ce_state;
Michal Kaziorab977bd2013-11-25 14:06:26 +0100683 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior728f95e2014-08-22 14:33:14 +0200684 struct ath10k_ce_pipe *ce_pipe = pipe->ce_hdl;
685 struct sk_buff *skb;
686 dma_addr_t paddr;
Michal Kaziorab977bd2013-11-25 14:06:26 +0100687 int ret;
688
Michal Kazior728f95e2014-08-22 14:33:14 +0200689 lockdep_assert_held(&ar_pci->ce_lock);
690
691 skb = dev_alloc_skb(pipe->buf_sz);
692 if (!skb)
693 return -ENOMEM;
694
695 WARN_ONCE((unsigned long)skb->data & 3, "unaligned skb");
696
697 paddr = dma_map_single(ar->dev, skb->data,
698 skb->len + skb_tailroom(skb),
699 DMA_FROM_DEVICE);
700 if (unlikely(dma_mapping_error(ar->dev, paddr))) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200701 ath10k_warn(ar, "failed to dma map pci rx buf\n");
Michal Kazior728f95e2014-08-22 14:33:14 +0200702 dev_kfree_skb_any(skb);
703 return -EIO;
704 }
705
Michal Kazior8582bf32015-01-24 12:14:47 +0200706 ATH10K_SKB_RXCB(skb)->paddr = paddr;
Michal Kazior728f95e2014-08-22 14:33:14 +0200707
708 ret = __ath10k_ce_rx_post_buf(ce_pipe, skb, paddr);
Michal Kaziorab977bd2013-11-25 14:06:26 +0100709 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200710 ath10k_warn(ar, "failed to post pci rx buf: %d\n", ret);
Michal Kazior728f95e2014-08-22 14:33:14 +0200711 dma_unmap_single(ar->dev, paddr, skb->len + skb_tailroom(skb),
712 DMA_FROM_DEVICE);
713 dev_kfree_skb_any(skb);
Michal Kaziorab977bd2013-11-25 14:06:26 +0100714 return ret;
715 }
716
717 return 0;
718}
719
Michal Kazior728f95e2014-08-22 14:33:14 +0200720static void __ath10k_pci_rx_post_pipe(struct ath10k_pci_pipe *pipe)
Michal Kaziorab977bd2013-11-25 14:06:26 +0100721{
Michal Kazior728f95e2014-08-22 14:33:14 +0200722 struct ath10k *ar = pipe->hif_ce_state;
723 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
724 struct ath10k_ce_pipe *ce_pipe = pipe->ce_hdl;
725 int ret, num;
726
727 lockdep_assert_held(&ar_pci->ce_lock);
728
729 if (pipe->buf_sz == 0)
730 return;
731
732 if (!ce_pipe->dest_ring)
733 return;
734
735 num = __ath10k_ce_rx_num_free_bufs(ce_pipe);
736 while (num--) {
737 ret = __ath10k_pci_rx_post_buf(pipe);
738 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200739 ath10k_warn(ar, "failed to post pci rx buf: %d\n", ret);
Michal Kazior728f95e2014-08-22 14:33:14 +0200740 mod_timer(&ar_pci->rx_post_retry, jiffies +
741 ATH10K_PCI_RX_POST_RETRY_MS);
742 break;
743 }
744 }
745}
746
747static void ath10k_pci_rx_post_pipe(struct ath10k_pci_pipe *pipe)
748{
749 struct ath10k *ar = pipe->hif_ce_state;
750 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
751
752 spin_lock_bh(&ar_pci->ce_lock);
753 __ath10k_pci_rx_post_pipe(pipe);
754 spin_unlock_bh(&ar_pci->ce_lock);
755}
756
757static void ath10k_pci_rx_post(struct ath10k *ar)
758{
759 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
760 int i;
761
762 spin_lock_bh(&ar_pci->ce_lock);
763 for (i = 0; i < CE_COUNT; i++)
764 __ath10k_pci_rx_post_pipe(&ar_pci->pipe_info[i]);
765 spin_unlock_bh(&ar_pci->ce_lock);
766}
767
768static void ath10k_pci_rx_replenish_retry(unsigned long ptr)
769{
770 struct ath10k *ar = (void *)ptr;
771
772 ath10k_pci_rx_post(ar);
Michal Kaziorab977bd2013-11-25 14:06:26 +0100773}
774
Vasanthakumar Thiagarajan418ca592015-06-18 12:31:05 +0530775static u32 ath10k_pci_targ_cpu_to_ce_addr(struct ath10k *ar, u32 addr)
776{
777 u32 val = 0;
778
779 switch (ar->hw_rev) {
780 case ATH10K_HW_QCA988X:
781 case ATH10K_HW_QCA6174:
782 val = (ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
783 CORE_CTRL_ADDRESS) &
Vasanthakumar Thiagarajan3c7e2562015-07-03 19:25:27 +0530784 0x7ff) << 21;
Vasanthakumar Thiagarajan418ca592015-06-18 12:31:05 +0530785 break;
786 case ATH10K_HW_QCA99X0:
787 val = ath10k_pci_read32(ar, PCIE_BAR_REG_ADDRESS);
788 break;
789 }
790
791 val |= 0x100000 | (addr & 0xfffff);
792 return val;
793}
794
Kalle Valo5e3dd152013-06-12 20:52:10 +0300795/*
796 * Diagnostic read/write access is provided for startup/config/debug usage.
797 * Caller must guarantee proper alignment, when applicable, and single user
798 * at any moment.
799 */
800static int ath10k_pci_diag_read_mem(struct ath10k *ar, u32 address, void *data,
801 int nbytes)
802{
803 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
804 int ret = 0;
805 u32 buf;
806 unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
807 unsigned int id;
808 unsigned int flags;
Michal Kazior2aa39112013-08-27 13:08:02 +0200809 struct ath10k_ce_pipe *ce_diag;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300810 /* Host buffer address in CE space */
811 u32 ce_data;
812 dma_addr_t ce_data_base = 0;
813 void *data_buf = NULL;
814 int i;
815
Kalle Valoeef25402014-09-24 14:16:52 +0300816 spin_lock_bh(&ar_pci->ce_lock);
817
Kalle Valo5e3dd152013-06-12 20:52:10 +0300818 ce_diag = ar_pci->ce_diag;
819
820 /*
821 * Allocate a temporary bounce buffer to hold caller's data
822 * to be DMA'ed from Target. This guarantees
823 * 1) 4-byte alignment
824 * 2) Buffer in DMA-able space
825 */
826 orig_nbytes = nbytes;
Michal Kazior68c03242014-03-28 10:02:35 +0200827 data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
828 orig_nbytes,
829 &ce_data_base,
830 GFP_ATOMIC);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300831
832 if (!data_buf) {
833 ret = -ENOMEM;
834 goto done;
835 }
836 memset(data_buf, 0, orig_nbytes);
837
838 remaining_bytes = orig_nbytes;
839 ce_data = ce_data_base;
840 while (remaining_bytes) {
841 nbytes = min_t(unsigned int, remaining_bytes,
842 DIAG_TRANSFER_LIMIT);
843
Kalle Valoeef25402014-09-24 14:16:52 +0300844 ret = __ath10k_ce_rx_post_buf(ce_diag, NULL, ce_data);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300845 if (ret != 0)
846 goto done;
847
848 /* Request CE to send from Target(!) address to Host buffer */
849 /*
850 * The address supplied by the caller is in the
851 * Target CPU virtual address space.
852 *
853 * In order to use this address with the diagnostic CE,
854 * convert it from Target CPU virtual address space
855 * to CE address space
856 */
Vasanthakumar Thiagarajan418ca592015-06-18 12:31:05 +0530857 address = ath10k_pci_targ_cpu_to_ce_addr(ar, address);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300858
Kalle Valoeef25402014-09-24 14:16:52 +0300859 ret = ath10k_ce_send_nolock(ce_diag, NULL, (u32)address, nbytes, 0,
860 0);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300861 if (ret)
862 goto done;
863
864 i = 0;
Kalle Valoeef25402014-09-24 14:16:52 +0300865 while (ath10k_ce_completed_send_next_nolock(ce_diag, NULL, &buf,
866 &completed_nbytes,
867 &id) != 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300868 mdelay(1);
869 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
870 ret = -EBUSY;
871 goto done;
872 }
873 }
874
875 if (nbytes != completed_nbytes) {
876 ret = -EIO;
877 goto done;
878 }
879
Kalle Valocfbc06a2014-09-14 12:50:23 +0300880 if (buf != (u32)address) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300881 ret = -EIO;
882 goto done;
883 }
884
885 i = 0;
Kalle Valoeef25402014-09-24 14:16:52 +0300886 while (ath10k_ce_completed_recv_next_nolock(ce_diag, NULL, &buf,
887 &completed_nbytes,
888 &id, &flags) != 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300889 mdelay(1);
890
891 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
892 ret = -EBUSY;
893 goto done;
894 }
895 }
896
897 if (nbytes != completed_nbytes) {
898 ret = -EIO;
899 goto done;
900 }
901
902 if (buf != ce_data) {
903 ret = -EIO;
904 goto done;
905 }
906
907 remaining_bytes -= nbytes;
908 address += nbytes;
909 ce_data += nbytes;
910 }
911
912done:
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300913 if (ret == 0)
914 memcpy(data, data_buf, orig_nbytes);
915 else
Michal Kazior7aa7a722014-08-25 12:09:38 +0200916 ath10k_warn(ar, "failed to read diag value at 0x%x: %d\n",
Kalle Valo50f87a62014-03-28 09:32:52 +0200917 address, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300918
919 if (data_buf)
Michal Kazior68c03242014-03-28 10:02:35 +0200920 dma_free_coherent(ar->dev, orig_nbytes, data_buf,
921 ce_data_base);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300922
Kalle Valoeef25402014-09-24 14:16:52 +0300923 spin_unlock_bh(&ar_pci->ce_lock);
924
Kalle Valo5e3dd152013-06-12 20:52:10 +0300925 return ret;
926}
927
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300928static int ath10k_pci_diag_read32(struct ath10k *ar, u32 address, u32 *value)
929{
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300930 __le32 val = 0;
931 int ret;
932
933 ret = ath10k_pci_diag_read_mem(ar, address, &val, sizeof(val));
934 *value = __le32_to_cpu(val);
935
936 return ret;
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300937}
938
939static int __ath10k_pci_diag_read_hi(struct ath10k *ar, void *dest,
940 u32 src, u32 len)
941{
942 u32 host_addr, addr;
943 int ret;
944
945 host_addr = host_interest_item_address(src);
946
947 ret = ath10k_pci_diag_read32(ar, host_addr, &addr);
948 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200949 ath10k_warn(ar, "failed to get memcpy hi address for firmware address %d: %d\n",
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300950 src, ret);
951 return ret;
952 }
953
954 ret = ath10k_pci_diag_read_mem(ar, addr, dest, len);
955 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200956 ath10k_warn(ar, "failed to memcpy firmware memory from %d (%d B): %d\n",
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300957 addr, len, ret);
958 return ret;
959 }
960
961 return 0;
962}
963
964#define ath10k_pci_diag_read_hi(ar, dest, src, len) \
Kalle Valo8cc7f262014-09-14 12:50:39 +0300965 __ath10k_pci_diag_read_hi(ar, dest, HI_ITEM(src), len)
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300966
Kalle Valo5e3dd152013-06-12 20:52:10 +0300967static int ath10k_pci_diag_write_mem(struct ath10k *ar, u32 address,
968 const void *data, int nbytes)
969{
970 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
971 int ret = 0;
972 u32 buf;
973 unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
974 unsigned int id;
975 unsigned int flags;
Michal Kazior2aa39112013-08-27 13:08:02 +0200976 struct ath10k_ce_pipe *ce_diag;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300977 void *data_buf = NULL;
978 u32 ce_data; /* Host buffer address in CE space */
979 dma_addr_t ce_data_base = 0;
980 int i;
981
Kalle Valoeef25402014-09-24 14:16:52 +0300982 spin_lock_bh(&ar_pci->ce_lock);
983
Kalle Valo5e3dd152013-06-12 20:52:10 +0300984 ce_diag = ar_pci->ce_diag;
985
986 /*
987 * Allocate a temporary bounce buffer to hold caller's data
988 * to be DMA'ed to Target. This guarantees
989 * 1) 4-byte alignment
990 * 2) Buffer in DMA-able space
991 */
992 orig_nbytes = nbytes;
Michal Kazior68c03242014-03-28 10:02:35 +0200993 data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
994 orig_nbytes,
995 &ce_data_base,
996 GFP_ATOMIC);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300997 if (!data_buf) {
998 ret = -ENOMEM;
999 goto done;
1000 }
1001
1002 /* Copy caller's data to allocated DMA buf */
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001003 memcpy(data_buf, data, orig_nbytes);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001004
1005 /*
1006 * The address supplied by the caller is in the
1007 * Target CPU virtual address space.
1008 *
1009 * In order to use this address with the diagnostic CE,
1010 * convert it from
1011 * Target CPU virtual address space
1012 * to
1013 * CE address space
1014 */
Vasanthakumar Thiagarajan418ca592015-06-18 12:31:05 +05301015 address = ath10k_pci_targ_cpu_to_ce_addr(ar, address);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001016
1017 remaining_bytes = orig_nbytes;
1018 ce_data = ce_data_base;
1019 while (remaining_bytes) {
1020 /* FIXME: check cast */
1021 nbytes = min_t(int, remaining_bytes, DIAG_TRANSFER_LIMIT);
1022
1023 /* Set up to receive directly into Target(!) address */
Kalle Valoeef25402014-09-24 14:16:52 +03001024 ret = __ath10k_ce_rx_post_buf(ce_diag, NULL, address);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001025 if (ret != 0)
1026 goto done;
1027
1028 /*
1029 * Request CE to send caller-supplied data that
1030 * was copied to bounce buffer to Target(!) address.
1031 */
Kalle Valoeef25402014-09-24 14:16:52 +03001032 ret = ath10k_ce_send_nolock(ce_diag, NULL, (u32)ce_data,
1033 nbytes, 0, 0);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001034 if (ret != 0)
1035 goto done;
1036
1037 i = 0;
Kalle Valoeef25402014-09-24 14:16:52 +03001038 while (ath10k_ce_completed_send_next_nolock(ce_diag, NULL, &buf,
1039 &completed_nbytes,
1040 &id) != 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001041 mdelay(1);
1042
1043 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
1044 ret = -EBUSY;
1045 goto done;
1046 }
1047 }
1048
1049 if (nbytes != completed_nbytes) {
1050 ret = -EIO;
1051 goto done;
1052 }
1053
1054 if (buf != ce_data) {
1055 ret = -EIO;
1056 goto done;
1057 }
1058
1059 i = 0;
Kalle Valoeef25402014-09-24 14:16:52 +03001060 while (ath10k_ce_completed_recv_next_nolock(ce_diag, NULL, &buf,
1061 &completed_nbytes,
1062 &id, &flags) != 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001063 mdelay(1);
1064
1065 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
1066 ret = -EBUSY;
1067 goto done;
1068 }
1069 }
1070
1071 if (nbytes != completed_nbytes) {
1072 ret = -EIO;
1073 goto done;
1074 }
1075
1076 if (buf != address) {
1077 ret = -EIO;
1078 goto done;
1079 }
1080
1081 remaining_bytes -= nbytes;
1082 address += nbytes;
1083 ce_data += nbytes;
1084 }
1085
1086done:
1087 if (data_buf) {
Michal Kazior68c03242014-03-28 10:02:35 +02001088 dma_free_coherent(ar->dev, orig_nbytes, data_buf,
1089 ce_data_base);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001090 }
1091
1092 if (ret != 0)
Michal Kazior7aa7a722014-08-25 12:09:38 +02001093 ath10k_warn(ar, "failed to write diag value at 0x%x: %d\n",
Kalle Valo50f87a62014-03-28 09:32:52 +02001094 address, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001095
Kalle Valoeef25402014-09-24 14:16:52 +03001096 spin_unlock_bh(&ar_pci->ce_lock);
1097
Kalle Valo5e3dd152013-06-12 20:52:10 +03001098 return ret;
1099}
1100
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001101static int ath10k_pci_diag_write32(struct ath10k *ar, u32 address, u32 value)
1102{
1103 __le32 val = __cpu_to_le32(value);
1104
1105 return ath10k_pci_diag_write_mem(ar, address, &val, sizeof(val));
1106}
1107
Kalle Valo5e3dd152013-06-12 20:52:10 +03001108/* Called by lower (CE) layer when a send to Target completes. */
Michal Kazior5440ce22013-09-03 15:09:58 +02001109static void ath10k_pci_ce_send_done(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001110{
1111 struct ath10k *ar = ce_state->ar;
1112 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2f5280d2014-02-27 18:50:05 +02001113 struct ath10k_hif_cb *cb = &ar_pci->msg_callbacks_current;
Michal Kazior1cb86d42014-11-27 11:09:38 +01001114 struct sk_buff_head list;
1115 struct sk_buff *skb;
Michal Kazior5440ce22013-09-03 15:09:58 +02001116 u32 ce_data;
1117 unsigned int nbytes;
1118 unsigned int transfer_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001119
Michal Kazior1cb86d42014-11-27 11:09:38 +01001120 __skb_queue_head_init(&list);
1121 while (ath10k_ce_completed_send_next(ce_state, (void **)&skb, &ce_data,
1122 &nbytes, &transfer_id) == 0) {
Michal Kaziora16942e2014-02-27 18:50:04 +02001123 /* no need to call tx completion for NULL pointers */
Michal Kazior1cb86d42014-11-27 11:09:38 +01001124 if (skb == NULL)
Michal Kazior726346f2014-02-27 18:50:04 +02001125 continue;
1126
Michal Kazior1cb86d42014-11-27 11:09:38 +01001127 __skb_queue_tail(&list, skb);
Michal Kazior5440ce22013-09-03 15:09:58 +02001128 }
Michal Kazior1cb86d42014-11-27 11:09:38 +01001129
1130 while ((skb = __skb_dequeue(&list)))
1131 cb->tx_completion(ar, skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001132}
1133
1134/* Called by lower (CE) layer when data is received from the Target. */
Michal Kazior5440ce22013-09-03 15:09:58 +02001135static void ath10k_pci_ce_recv_data(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001136{
1137 struct ath10k *ar = ce_state->ar;
1138 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +02001139 struct ath10k_pci_pipe *pipe_info = &ar_pci->pipe_info[ce_state->id];
Michal Kazior2f5280d2014-02-27 18:50:05 +02001140 struct ath10k_hif_cb *cb = &ar_pci->msg_callbacks_current;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001141 struct sk_buff *skb;
Michal Kazior1cb86d42014-11-27 11:09:38 +01001142 struct sk_buff_head list;
Michal Kazior5440ce22013-09-03 15:09:58 +02001143 void *transfer_context;
1144 u32 ce_data;
Michal Kazior2f5280d2014-02-27 18:50:05 +02001145 unsigned int nbytes, max_nbytes;
Michal Kazior5440ce22013-09-03 15:09:58 +02001146 unsigned int transfer_id;
1147 unsigned int flags;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001148
Michal Kazior1cb86d42014-11-27 11:09:38 +01001149 __skb_queue_head_init(&list);
Michal Kazior5440ce22013-09-03 15:09:58 +02001150 while (ath10k_ce_completed_recv_next(ce_state, &transfer_context,
1151 &ce_data, &nbytes, &transfer_id,
1152 &flags) == 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001153 skb = transfer_context;
Michal Kazior2f5280d2014-02-27 18:50:05 +02001154 max_nbytes = skb->len + skb_tailroom(skb);
Michal Kazior8582bf32015-01-24 12:14:47 +02001155 dma_unmap_single(ar->dev, ATH10K_SKB_RXCB(skb)->paddr,
Michal Kazior2f5280d2014-02-27 18:50:05 +02001156 max_nbytes, DMA_FROM_DEVICE);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001157
Michal Kazior2f5280d2014-02-27 18:50:05 +02001158 if (unlikely(max_nbytes < nbytes)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001159 ath10k_warn(ar, "rxed more than expected (nbytes %d, max %d)",
Michal Kazior2f5280d2014-02-27 18:50:05 +02001160 nbytes, max_nbytes);
1161 dev_kfree_skb_any(skb);
1162 continue;
1163 }
1164
1165 skb_put(skb, nbytes);
Michal Kazior1cb86d42014-11-27 11:09:38 +01001166 __skb_queue_tail(&list, skb);
1167 }
Michal Kaziora360e542014-09-23 10:22:54 +02001168
Michal Kazior1cb86d42014-11-27 11:09:38 +01001169 while ((skb = __skb_dequeue(&list))) {
Michal Kaziora360e542014-09-23 10:22:54 +02001170 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci rx ce pipe %d len %d\n",
1171 ce_state->id, skb->len);
1172 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci rx: ",
1173 skb->data, skb->len);
1174
Michal Kazior5f07ea42014-11-27 11:09:36 +01001175 cb->rx_completion(ar, skb);
Michal Kazior2f5280d2014-02-27 18:50:05 +02001176 }
Michal Kaziorc29a3802014-07-21 21:03:10 +03001177
Michal Kazior728f95e2014-08-22 14:33:14 +02001178 ath10k_pci_rx_post_pipe(pipe_info);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001179}
1180
Michal Kazior726346f2014-02-27 18:50:04 +02001181static int ath10k_pci_hif_tx_sg(struct ath10k *ar, u8 pipe_id,
1182 struct ath10k_hif_sg_item *items, int n_items)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001183{
Kalle Valo5e3dd152013-06-12 20:52:10 +03001184 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior726346f2014-02-27 18:50:04 +02001185 struct ath10k_pci_pipe *pci_pipe = &ar_pci->pipe_info[pipe_id];
1186 struct ath10k_ce_pipe *ce_pipe = pci_pipe->ce_hdl;
1187 struct ath10k_ce_ring *src_ring = ce_pipe->src_ring;
Michal Kazior7147a132014-05-26 12:02:58 +02001188 unsigned int nentries_mask;
1189 unsigned int sw_index;
1190 unsigned int write_index;
Michal Kazior08b8aa02014-05-26 12:02:59 +02001191 int err, i = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001192
Michal Kazior726346f2014-02-27 18:50:04 +02001193 spin_lock_bh(&ar_pci->ce_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001194
Michal Kazior7147a132014-05-26 12:02:58 +02001195 nentries_mask = src_ring->nentries_mask;
1196 sw_index = src_ring->sw_index;
1197 write_index = src_ring->write_index;
1198
Michal Kazior726346f2014-02-27 18:50:04 +02001199 if (unlikely(CE_RING_DELTA(nentries_mask,
1200 write_index, sw_index - 1) < n_items)) {
1201 err = -ENOBUFS;
Michal Kazior08b8aa02014-05-26 12:02:59 +02001202 goto err;
Michal Kazior726346f2014-02-27 18:50:04 +02001203 }
1204
1205 for (i = 0; i < n_items - 1; i++) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001206 ath10k_dbg(ar, ATH10K_DBG_PCI,
Michal Kazior726346f2014-02-27 18:50:04 +02001207 "pci tx item %d paddr 0x%08x len %d n_items %d\n",
1208 i, items[i].paddr, items[i].len, n_items);
Michal Kazior7aa7a722014-08-25 12:09:38 +02001209 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci tx data: ",
Michal Kazior726346f2014-02-27 18:50:04 +02001210 items[i].vaddr, items[i].len);
1211
1212 err = ath10k_ce_send_nolock(ce_pipe,
1213 items[i].transfer_context,
1214 items[i].paddr,
1215 items[i].len,
1216 items[i].transfer_id,
1217 CE_SEND_FLAG_GATHER);
1218 if (err)
Michal Kazior08b8aa02014-05-26 12:02:59 +02001219 goto err;
Michal Kazior726346f2014-02-27 18:50:04 +02001220 }
1221
1222 /* `i` is equal to `n_items -1` after for() */
Kalle Valo5e3dd152013-06-12 20:52:10 +03001223
Michal Kazior7aa7a722014-08-25 12:09:38 +02001224 ath10k_dbg(ar, ATH10K_DBG_PCI,
Michal Kazior726346f2014-02-27 18:50:04 +02001225 "pci tx item %d paddr 0x%08x len %d n_items %d\n",
1226 i, items[i].paddr, items[i].len, n_items);
Michal Kazior7aa7a722014-08-25 12:09:38 +02001227 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci tx data: ",
Michal Kazior726346f2014-02-27 18:50:04 +02001228 items[i].vaddr, items[i].len);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001229
Michal Kazior726346f2014-02-27 18:50:04 +02001230 err = ath10k_ce_send_nolock(ce_pipe,
1231 items[i].transfer_context,
1232 items[i].paddr,
1233 items[i].len,
1234 items[i].transfer_id,
1235 0);
1236 if (err)
Michal Kazior08b8aa02014-05-26 12:02:59 +02001237 goto err;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001238
Michal Kazior08b8aa02014-05-26 12:02:59 +02001239 spin_unlock_bh(&ar_pci->ce_lock);
1240 return 0;
1241
1242err:
1243 for (; i > 0; i--)
1244 __ath10k_ce_send_revert(ce_pipe);
1245
Michal Kazior726346f2014-02-27 18:50:04 +02001246 spin_unlock_bh(&ar_pci->ce_lock);
1247 return err;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001248}
1249
Kalle Valoeef25402014-09-24 14:16:52 +03001250static int ath10k_pci_hif_diag_read(struct ath10k *ar, u32 address, void *buf,
1251 size_t buf_len)
1252{
1253 return ath10k_pci_diag_read_mem(ar, address, buf, buf_len);
1254}
1255
Kalle Valo5e3dd152013-06-12 20:52:10 +03001256static u16 ath10k_pci_hif_get_free_queue_number(struct ath10k *ar, u8 pipe)
1257{
1258 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo50f87a62014-03-28 09:32:52 +02001259
Michal Kazior7aa7a722014-08-25 12:09:38 +02001260 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif get free queue number\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001261
Michal Kazior3efcb3b2013-10-02 11:03:41 +02001262 return ath10k_ce_num_free_src_entries(ar_pci->pipe_info[pipe].ce_hdl);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001263}
1264
Ben Greear384914b2014-08-25 08:37:32 +03001265static void ath10k_pci_dump_registers(struct ath10k *ar,
1266 struct ath10k_fw_crash_data *crash_data)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001267{
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001268 __le32 reg_dump_values[REG_DUMP_COUNT_QCA988X] = {};
1269 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001270
Ben Greear384914b2014-08-25 08:37:32 +03001271 lockdep_assert_held(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001272
Kalle Valo3d29a3e2014-08-25 08:37:26 +03001273 ret = ath10k_pci_diag_read_hi(ar, &reg_dump_values[0],
1274 hi_failure_state,
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001275 REG_DUMP_COUNT_QCA988X * sizeof(__le32));
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001276 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001277 ath10k_err(ar, "failed to read firmware dump area: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001278 return;
1279 }
1280
1281 BUILD_BUG_ON(REG_DUMP_COUNT_QCA988X % 4);
1282
Michal Kazior7aa7a722014-08-25 12:09:38 +02001283 ath10k_err(ar, "firmware register dump:\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001284 for (i = 0; i < REG_DUMP_COUNT_QCA988X; i += 4)
Michal Kazior7aa7a722014-08-25 12:09:38 +02001285 ath10k_err(ar, "[%02d]: 0x%08X 0x%08X 0x%08X 0x%08X\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001286 i,
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001287 __le32_to_cpu(reg_dump_values[i]),
1288 __le32_to_cpu(reg_dump_values[i + 1]),
1289 __le32_to_cpu(reg_dump_values[i + 2]),
1290 __le32_to_cpu(reg_dump_values[i + 3]));
Michal Kazioraffd3212013-07-16 09:54:35 +02001291
Michal Kazior1bbb1192014-08-25 12:13:14 +02001292 if (!crash_data)
1293 return;
1294
Ben Greear384914b2014-08-25 08:37:32 +03001295 for (i = 0; i < REG_DUMP_COUNT_QCA988X; i++)
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001296 crash_data->registers[i] = reg_dump_values[i];
Ben Greear384914b2014-08-25 08:37:32 +03001297}
1298
Kalle Valo0e9848c2014-08-25 08:37:37 +03001299static void ath10k_pci_fw_crashed_dump(struct ath10k *ar)
Ben Greear384914b2014-08-25 08:37:32 +03001300{
1301 struct ath10k_fw_crash_data *crash_data;
1302 char uuid[50];
1303
1304 spin_lock_bh(&ar->data_lock);
1305
Ben Greearf51dbe72014-09-29 14:41:46 +03001306 ar->stats.fw_crash_counter++;
1307
Ben Greear384914b2014-08-25 08:37:32 +03001308 crash_data = ath10k_debug_get_new_fw_crash_data(ar);
1309
1310 if (crash_data)
1311 scnprintf(uuid, sizeof(uuid), "%pUl", &crash_data->uuid);
1312 else
1313 scnprintf(uuid, sizeof(uuid), "n/a");
1314
Michal Kazior7aa7a722014-08-25 12:09:38 +02001315 ath10k_err(ar, "firmware crashed! (uuid %s)\n", uuid);
Kalle Valo8a0c7972014-08-25 08:37:45 +03001316 ath10k_print_driver_info(ar);
Ben Greear384914b2014-08-25 08:37:32 +03001317 ath10k_pci_dump_registers(ar, crash_data);
1318
Ben Greear384914b2014-08-25 08:37:32 +03001319 spin_unlock_bh(&ar->data_lock);
Michal Kazioraffd3212013-07-16 09:54:35 +02001320
Michal Kazior5e90de82013-10-16 16:46:05 +03001321 queue_work(ar->workqueue, &ar->restart_work);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001322}
1323
1324static void ath10k_pci_hif_send_complete_check(struct ath10k *ar, u8 pipe,
1325 int force)
1326{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001327 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif send complete check\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001328
Kalle Valo5e3dd152013-06-12 20:52:10 +03001329 if (!force) {
1330 int resources;
1331 /*
1332 * Decide whether to actually poll for completions, or just
1333 * wait for a later chance.
1334 * If there seem to be plenty of resources left, then just wait
1335 * since checking involves reading a CE register, which is a
1336 * relatively expensive operation.
1337 */
1338 resources = ath10k_pci_hif_get_free_queue_number(ar, pipe);
1339
1340 /*
1341 * If at least 50% of the total resources are still available,
1342 * don't bother checking again yet.
1343 */
1344 if (resources > (host_ce_config_wlan[pipe].src_nentries >> 1))
1345 return;
1346 }
1347 ath10k_ce_per_engine_service(ar, pipe);
1348}
1349
Michal Kaziore799bbf2013-07-05 16:15:12 +03001350static void ath10k_pci_hif_set_callbacks(struct ath10k *ar,
1351 struct ath10k_hif_cb *callbacks)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001352{
1353 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1354
Michal Kazior7aa7a722014-08-25 12:09:38 +02001355 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif set callbacks\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001356
1357 memcpy(&ar_pci->msg_callbacks_current, callbacks,
1358 sizeof(ar_pci->msg_callbacks_current));
1359}
1360
Michal Kazior96a9d0d2013-11-08 08:01:25 +01001361static void ath10k_pci_kill_tasklet(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001362{
1363 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001364 int i;
1365
Kalle Valo5e3dd152013-06-12 20:52:10 +03001366 tasklet_kill(&ar_pci->intr_tq);
Michal Kazior103d4f52013-11-08 08:01:24 +01001367 tasklet_kill(&ar_pci->msi_fw_err);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001368
1369 for (i = 0; i < CE_COUNT; i++)
1370 tasklet_kill(&ar_pci->pipe_info[i].intr);
Michal Kazior728f95e2014-08-22 14:33:14 +02001371
1372 del_timer_sync(&ar_pci->rx_post_retry);
Michal Kazior96a9d0d2013-11-08 08:01:25 +01001373}
1374
Kalle Valo5e3dd152013-06-12 20:52:10 +03001375static int ath10k_pci_hif_map_service_to_pipe(struct ath10k *ar,
1376 u16 service_id, u8 *ul_pipe,
1377 u8 *dl_pipe, int *ul_is_polled,
1378 int *dl_is_polled)
1379{
Michal Kazior7c6aa252014-08-26 19:14:03 +03001380 const struct service_to_pipe *entry;
1381 bool ul_set = false, dl_set = false;
1382 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001383
Michal Kazior7aa7a722014-08-25 12:09:38 +02001384 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif map service\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001385
Kalle Valo5e3dd152013-06-12 20:52:10 +03001386 /* polling for received messages not supported */
1387 *dl_is_polled = 0;
1388
Michal Kazior7c6aa252014-08-26 19:14:03 +03001389 for (i = 0; i < ARRAY_SIZE(target_service_to_ce_map_wlan); i++) {
1390 entry = &target_service_to_ce_map_wlan[i];
Kalle Valo5e3dd152013-06-12 20:52:10 +03001391
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001392 if (__le32_to_cpu(entry->service_id) != service_id)
Michal Kazior7c6aa252014-08-26 19:14:03 +03001393 continue;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001394
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001395 switch (__le32_to_cpu(entry->pipedir)) {
Michal Kazior7c6aa252014-08-26 19:14:03 +03001396 case PIPEDIR_NONE:
1397 break;
1398 case PIPEDIR_IN:
1399 WARN_ON(dl_set);
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001400 *dl_pipe = __le32_to_cpu(entry->pipenum);
Michal Kazior7c6aa252014-08-26 19:14:03 +03001401 dl_set = true;
1402 break;
1403 case PIPEDIR_OUT:
1404 WARN_ON(ul_set);
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001405 *ul_pipe = __le32_to_cpu(entry->pipenum);
Michal Kazior7c6aa252014-08-26 19:14:03 +03001406 ul_set = true;
1407 break;
1408 case PIPEDIR_INOUT:
1409 WARN_ON(dl_set);
1410 WARN_ON(ul_set);
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001411 *dl_pipe = __le32_to_cpu(entry->pipenum);
1412 *ul_pipe = __le32_to_cpu(entry->pipenum);
Michal Kazior7c6aa252014-08-26 19:14:03 +03001413 dl_set = true;
1414 ul_set = true;
1415 break;
1416 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001417 }
Michal Kazior7c6aa252014-08-26 19:14:03 +03001418
1419 if (WARN_ON(!ul_set || !dl_set))
1420 return -ENOENT;
1421
Kalle Valo5e3dd152013-06-12 20:52:10 +03001422 *ul_is_polled =
1423 (host_ce_config_wlan[*ul_pipe].flags & CE_ATTR_DIS_INTR) != 0;
1424
Michal Kazior7c6aa252014-08-26 19:14:03 +03001425 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001426}
1427
1428static void ath10k_pci_hif_get_default_pipe(struct ath10k *ar,
Kalle Valo5b07e072014-09-14 12:50:06 +03001429 u8 *ul_pipe, u8 *dl_pipe)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001430{
1431 int ul_is_polled, dl_is_polled;
1432
Michal Kazior7aa7a722014-08-25 12:09:38 +02001433 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif get default pipe\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001434
Kalle Valo5e3dd152013-06-12 20:52:10 +03001435 (void)ath10k_pci_hif_map_service_to_pipe(ar,
1436 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1437 ul_pipe,
1438 dl_pipe,
1439 &ul_is_polled,
1440 &dl_is_polled);
1441}
1442
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001443static void ath10k_pci_irq_msi_fw_mask(struct ath10k *ar)
1444{
1445 u32 val;
1446
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +05301447 switch (ar->hw_rev) {
1448 case ATH10K_HW_QCA988X:
1449 case ATH10K_HW_QCA6174:
1450 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1451 CORE_CTRL_ADDRESS);
1452 val &= ~CORE_CTRL_PCIE_REG_31_MASK;
1453 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
1454 CORE_CTRL_ADDRESS, val);
1455 break;
1456 case ATH10K_HW_QCA99X0:
1457 /* TODO: Find appropriate register configuration for QCA99X0
1458 * to mask irq/MSI.
1459 */
1460 break;
1461 }
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001462}
1463
1464static void ath10k_pci_irq_msi_fw_unmask(struct ath10k *ar)
1465{
1466 u32 val;
1467
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +05301468 switch (ar->hw_rev) {
1469 case ATH10K_HW_QCA988X:
1470 case ATH10K_HW_QCA6174:
1471 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1472 CORE_CTRL_ADDRESS);
1473 val |= CORE_CTRL_PCIE_REG_31_MASK;
1474 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
1475 CORE_CTRL_ADDRESS, val);
1476 break;
1477 case ATH10K_HW_QCA99X0:
1478 /* TODO: Find appropriate register configuration for QCA99X0
1479 * to unmask irq/MSI.
1480 */
1481 break;
1482 }
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001483}
1484
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001485static void ath10k_pci_irq_disable(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001486{
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001487 ath10k_ce_disable_interrupts(ar);
1488 ath10k_pci_disable_and_clear_legacy_irq(ar);
1489 ath10k_pci_irq_msi_fw_mask(ar);
1490}
1491
1492static void ath10k_pci_irq_sync(struct ath10k *ar)
1493{
Kalle Valo5e3dd152013-06-12 20:52:10 +03001494 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001495 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001496
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001497 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
1498 synchronize_irq(ar_pci->pdev->irq + i);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001499}
1500
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001501static void ath10k_pci_irq_enable(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001502{
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001503 ath10k_ce_enable_interrupts(ar);
Michal Kaziore75db4e2014-08-28 22:14:16 +03001504 ath10k_pci_enable_legacy_irq(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001505 ath10k_pci_irq_msi_fw_unmask(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001506}
1507
1508static int ath10k_pci_hif_start(struct ath10k *ar)
1509{
Janusz Dziedzic76d870e2015-05-18 09:38:16 +00001510 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo9a149692015-10-05 17:56:36 +03001511
Michal Kazior7aa7a722014-08-25 12:09:38 +02001512 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif start\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001513
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001514 ath10k_pci_irq_enable(ar);
Michal Kazior728f95e2014-08-22 14:33:14 +02001515 ath10k_pci_rx_post(ar);
Kalle Valo50f87a62014-03-28 09:32:52 +02001516
Janusz Dziedzic76d870e2015-05-18 09:38:16 +00001517 pcie_capability_write_word(ar_pci->pdev, PCI_EXP_LNKCTL,
1518 ar_pci->link_ctl);
1519
Kalle Valo5e3dd152013-06-12 20:52:10 +03001520 return 0;
1521}
1522
Michal Kazior099ac7c2014-10-28 10:32:05 +01001523static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe *pci_pipe)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001524{
1525 struct ath10k *ar;
Michal Kazior099ac7c2014-10-28 10:32:05 +01001526 struct ath10k_ce_pipe *ce_pipe;
1527 struct ath10k_ce_ring *ce_ring;
1528 struct sk_buff *skb;
1529 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001530
Michal Kazior099ac7c2014-10-28 10:32:05 +01001531 ar = pci_pipe->hif_ce_state;
1532 ce_pipe = pci_pipe->ce_hdl;
1533 ce_ring = ce_pipe->dest_ring;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001534
Michal Kazior099ac7c2014-10-28 10:32:05 +01001535 if (!ce_ring)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001536 return;
1537
Michal Kazior099ac7c2014-10-28 10:32:05 +01001538 if (!pci_pipe->buf_sz)
1539 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001540
Michal Kazior099ac7c2014-10-28 10:32:05 +01001541 for (i = 0; i < ce_ring->nentries; i++) {
1542 skb = ce_ring->per_transfer_context[i];
1543 if (!skb)
1544 continue;
1545
1546 ce_ring->per_transfer_context[i] = NULL;
1547
Michal Kazior8582bf32015-01-24 12:14:47 +02001548 dma_unmap_single(ar->dev, ATH10K_SKB_RXCB(skb)->paddr,
Michal Kazior099ac7c2014-10-28 10:32:05 +01001549 skb->len + skb_tailroom(skb),
Kalle Valo5e3dd152013-06-12 20:52:10 +03001550 DMA_FROM_DEVICE);
Michal Kazior099ac7c2014-10-28 10:32:05 +01001551 dev_kfree_skb_any(skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001552 }
1553}
1554
Michal Kazior099ac7c2014-10-28 10:32:05 +01001555static void ath10k_pci_tx_pipe_cleanup(struct ath10k_pci_pipe *pci_pipe)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001556{
1557 struct ath10k *ar;
1558 struct ath10k_pci *ar_pci;
Michal Kazior099ac7c2014-10-28 10:32:05 +01001559 struct ath10k_ce_pipe *ce_pipe;
1560 struct ath10k_ce_ring *ce_ring;
1561 struct ce_desc *ce_desc;
1562 struct sk_buff *skb;
Michal Kazior099ac7c2014-10-28 10:32:05 +01001563 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001564
Michal Kazior099ac7c2014-10-28 10:32:05 +01001565 ar = pci_pipe->hif_ce_state;
1566 ar_pci = ath10k_pci_priv(ar);
1567 ce_pipe = pci_pipe->ce_hdl;
1568 ce_ring = ce_pipe->src_ring;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001569
Michal Kazior099ac7c2014-10-28 10:32:05 +01001570 if (!ce_ring)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001571 return;
1572
Michal Kazior099ac7c2014-10-28 10:32:05 +01001573 if (!pci_pipe->buf_sz)
1574 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001575
Michal Kazior099ac7c2014-10-28 10:32:05 +01001576 ce_desc = ce_ring->shadow_base;
1577 if (WARN_ON(!ce_desc))
1578 return;
1579
1580 for (i = 0; i < ce_ring->nentries; i++) {
1581 skb = ce_ring->per_transfer_context[i];
1582 if (!skb)
Michal Kazior2415fc12013-11-08 08:01:32 +01001583 continue;
Michal Kazior2415fc12013-11-08 08:01:32 +01001584
Michal Kazior099ac7c2014-10-28 10:32:05 +01001585 ce_ring->per_transfer_context[i] = NULL;
Michal Kazior099ac7c2014-10-28 10:32:05 +01001586
Michal Kaziord84a5122014-11-27 11:09:37 +01001587 ar_pci->msg_callbacks_current.tx_completion(ar, skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001588 }
1589}
1590
1591/*
1592 * Cleanup residual buffers for device shutdown:
1593 * buffers that were enqueued for receive
1594 * buffers that were to be sent
1595 * Note: Buffers that had completed but which were
1596 * not yet processed are on a completion queue. They
1597 * are handled when the completion thread shuts down.
1598 */
1599static void ath10k_pci_buffer_cleanup(struct ath10k *ar)
1600{
1601 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1602 int pipe_num;
1603
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001604 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Michal Kazior87263e52013-08-27 13:08:01 +02001605 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001606
1607 pipe_info = &ar_pci->pipe_info[pipe_num];
1608 ath10k_pci_rx_pipe_cleanup(pipe_info);
1609 ath10k_pci_tx_pipe_cleanup(pipe_info);
1610 }
1611}
1612
1613static void ath10k_pci_ce_deinit(struct ath10k *ar)
1614{
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001615 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001616
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001617 for (i = 0; i < CE_COUNT; i++)
1618 ath10k_ce_deinit_pipe(ar, i);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001619}
1620
Michal Kazior728f95e2014-08-22 14:33:14 +02001621static void ath10k_pci_flush(struct ath10k *ar)
1622{
1623 ath10k_pci_kill_tasklet(ar);
1624 ath10k_pci_buffer_cleanup(ar);
1625}
1626
Kalle Valo5e3dd152013-06-12 20:52:10 +03001627static void ath10k_pci_hif_stop(struct ath10k *ar)
1628{
Michal Kazior77258d42015-05-18 09:38:18 +00001629 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1630 unsigned long flags;
1631
Michal Kazior7aa7a722014-08-25 12:09:38 +02001632 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif stop\n");
Michal Kazior32270b62013-08-02 09:15:47 +02001633
Michal Kazior10d23db2014-08-22 14:33:15 +02001634 /* Most likely the device has HTT Rx ring configured. The only way to
1635 * prevent the device from accessing (and possible corrupting) host
1636 * memory is to reset the chip now.
Michal Kaziore75db4e2014-08-28 22:14:16 +03001637 *
1638 * There's also no known way of masking MSI interrupts on the device.
1639 * For ranged MSI the CE-related interrupts can be masked. However
1640 * regardless how many MSI interrupts are assigned the first one
1641 * is always used for firmware indications (crashes) and cannot be
1642 * masked. To prevent the device from asserting the interrupt reset it
1643 * before proceeding with cleanup.
Michal Kazior10d23db2014-08-22 14:33:15 +02001644 */
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +05301645 ath10k_pci_safe_chip_reset(ar);
Michal Kaziore75db4e2014-08-28 22:14:16 +03001646
1647 ath10k_pci_irq_disable(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001648 ath10k_pci_irq_sync(ar);
Michal Kaziore75db4e2014-08-28 22:14:16 +03001649 ath10k_pci_flush(ar);
Michal Kazior77258d42015-05-18 09:38:18 +00001650
1651 spin_lock_irqsave(&ar_pci->ps_lock, flags);
1652 WARN_ON(ar_pci->ps_wake_refcount > 0);
1653 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001654}
1655
1656static int ath10k_pci_hif_exchange_bmi_msg(struct ath10k *ar,
1657 void *req, u32 req_len,
1658 void *resp, u32 *resp_len)
1659{
1660 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2aa39112013-08-27 13:08:02 +02001661 struct ath10k_pci_pipe *pci_tx = &ar_pci->pipe_info[BMI_CE_NUM_TO_TARG];
1662 struct ath10k_pci_pipe *pci_rx = &ar_pci->pipe_info[BMI_CE_NUM_TO_HOST];
1663 struct ath10k_ce_pipe *ce_tx = pci_tx->ce_hdl;
1664 struct ath10k_ce_pipe *ce_rx = pci_rx->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001665 dma_addr_t req_paddr = 0;
1666 dma_addr_t resp_paddr = 0;
1667 struct bmi_xfer xfer = {};
1668 void *treq, *tresp = NULL;
1669 int ret = 0;
1670
Michal Kazior85622cd2013-11-25 14:06:22 +01001671 might_sleep();
1672
Kalle Valo5e3dd152013-06-12 20:52:10 +03001673 if (resp && !resp_len)
1674 return -EINVAL;
1675
1676 if (resp && resp_len && *resp_len == 0)
1677 return -EINVAL;
1678
1679 treq = kmemdup(req, req_len, GFP_KERNEL);
1680 if (!treq)
1681 return -ENOMEM;
1682
1683 req_paddr = dma_map_single(ar->dev, treq, req_len, DMA_TO_DEVICE);
1684 ret = dma_mapping_error(ar->dev, req_paddr);
Michal Kazior5e55e3c2015-08-19 13:10:43 +02001685 if (ret) {
1686 ret = -EIO;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001687 goto err_dma;
Michal Kazior5e55e3c2015-08-19 13:10:43 +02001688 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001689
1690 if (resp && resp_len) {
1691 tresp = kzalloc(*resp_len, GFP_KERNEL);
1692 if (!tresp) {
1693 ret = -ENOMEM;
1694 goto err_req;
1695 }
1696
1697 resp_paddr = dma_map_single(ar->dev, tresp, *resp_len,
1698 DMA_FROM_DEVICE);
1699 ret = dma_mapping_error(ar->dev, resp_paddr);
Michal Kazior5e55e3c2015-08-19 13:10:43 +02001700 if (ret) {
1701 ret = EIO;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001702 goto err_req;
Michal Kazior5e55e3c2015-08-19 13:10:43 +02001703 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001704
1705 xfer.wait_for_resp = true;
1706 xfer.resp_len = 0;
1707
Michal Kazior728f95e2014-08-22 14:33:14 +02001708 ath10k_ce_rx_post_buf(ce_rx, &xfer, resp_paddr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001709 }
1710
Kalle Valo5e3dd152013-06-12 20:52:10 +03001711 ret = ath10k_ce_send(ce_tx, &xfer, req_paddr, req_len, -1, 0);
1712 if (ret)
1713 goto err_resp;
1714
Michal Kazior85622cd2013-11-25 14:06:22 +01001715 ret = ath10k_pci_bmi_wait(ce_tx, ce_rx, &xfer);
1716 if (ret) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001717 u32 unused_buffer;
1718 unsigned int unused_nbytes;
1719 unsigned int unused_id;
1720
Kalle Valo5e3dd152013-06-12 20:52:10 +03001721 ath10k_ce_cancel_send_next(ce_tx, NULL, &unused_buffer,
1722 &unused_nbytes, &unused_id);
1723 } else {
1724 /* non-zero means we did not time out */
1725 ret = 0;
1726 }
1727
1728err_resp:
1729 if (resp) {
1730 u32 unused_buffer;
1731
1732 ath10k_ce_revoke_recv_next(ce_rx, NULL, &unused_buffer);
1733 dma_unmap_single(ar->dev, resp_paddr,
1734 *resp_len, DMA_FROM_DEVICE);
1735 }
1736err_req:
1737 dma_unmap_single(ar->dev, req_paddr, req_len, DMA_TO_DEVICE);
1738
1739 if (ret == 0 && resp_len) {
1740 *resp_len = min(*resp_len, xfer.resp_len);
1741 memcpy(resp, tresp, xfer.resp_len);
1742 }
1743err_dma:
1744 kfree(treq);
1745 kfree(tresp);
1746
1747 return ret;
1748}
1749
Michal Kazior5440ce22013-09-03 15:09:58 +02001750static void ath10k_pci_bmi_send_done(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001751{
Michal Kazior5440ce22013-09-03 15:09:58 +02001752 struct bmi_xfer *xfer;
1753 u32 ce_data;
1754 unsigned int nbytes;
1755 unsigned int transfer_id;
1756
1757 if (ath10k_ce_completed_send_next(ce_state, (void **)&xfer, &ce_data,
1758 &nbytes, &transfer_id))
1759 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001760
Michal Kazior2374b182014-07-14 16:25:25 +03001761 xfer->tx_done = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001762}
1763
Michal Kazior5440ce22013-09-03 15:09:58 +02001764static void ath10k_pci_bmi_recv_data(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001765{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001766 struct ath10k *ar = ce_state->ar;
Michal Kazior5440ce22013-09-03 15:09:58 +02001767 struct bmi_xfer *xfer;
1768 u32 ce_data;
1769 unsigned int nbytes;
1770 unsigned int transfer_id;
1771 unsigned int flags;
1772
1773 if (ath10k_ce_completed_recv_next(ce_state, (void **)&xfer, &ce_data,
1774 &nbytes, &transfer_id, &flags))
1775 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001776
Michal Kazior04ed9df2014-10-28 10:34:36 +01001777 if (WARN_ON_ONCE(!xfer))
1778 return;
1779
Kalle Valo5e3dd152013-06-12 20:52:10 +03001780 if (!xfer->wait_for_resp) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001781 ath10k_warn(ar, "unexpected: BMI data received; ignoring\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001782 return;
1783 }
1784
1785 xfer->resp_len = nbytes;
Michal Kazior2374b182014-07-14 16:25:25 +03001786 xfer->rx_done = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001787}
1788
Michal Kazior85622cd2013-11-25 14:06:22 +01001789static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
1790 struct ath10k_ce_pipe *rx_pipe,
1791 struct bmi_xfer *xfer)
1792{
1793 unsigned long timeout = jiffies + BMI_COMMUNICATION_TIMEOUT_HZ;
1794
1795 while (time_before_eq(jiffies, timeout)) {
1796 ath10k_pci_bmi_send_done(tx_pipe);
1797 ath10k_pci_bmi_recv_data(rx_pipe);
1798
Michal Kazior2374b182014-07-14 16:25:25 +03001799 if (xfer->tx_done && (xfer->rx_done == xfer->wait_for_resp))
Michal Kazior85622cd2013-11-25 14:06:22 +01001800 return 0;
1801
1802 schedule();
1803 }
1804
1805 return -ETIMEDOUT;
1806}
1807
Kalle Valo5e3dd152013-06-12 20:52:10 +03001808/*
Kalle Valo5e3dd152013-06-12 20:52:10 +03001809 * Send an interrupt to the device to wake up the Target CPU
1810 * so it has an opportunity to notice any changed state.
1811 */
1812static int ath10k_pci_wake_target_cpu(struct ath10k *ar)
1813{
Michal Kazior9e264942014-09-02 11:00:21 +03001814 u32 addr, val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001815
Michal Kazior9e264942014-09-02 11:00:21 +03001816 addr = SOC_CORE_BASE_ADDRESS | CORE_CTRL_ADDRESS;
1817 val = ath10k_pci_read32(ar, addr);
1818 val |= CORE_CTRL_CPU_INTR_MASK;
1819 ath10k_pci_write32(ar, addr, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001820
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001821 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001822}
1823
Michal Kaziord63955b2015-01-24 12:14:49 +02001824static int ath10k_pci_get_num_banks(struct ath10k *ar)
1825{
1826 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1827
1828 switch (ar_pci->pdev->device) {
1829 case QCA988X_2_0_DEVICE_ID:
Vasanthakumar Thiagarajan8bd47022015-06-18 12:31:03 +05301830 case QCA99X0_2_0_DEVICE_ID:
Michal Kaziord63955b2015-01-24 12:14:49 +02001831 return 1;
Michal Kazior36582e52015-08-13 14:32:26 +02001832 case QCA6164_2_1_DEVICE_ID:
Michal Kaziord63955b2015-01-24 12:14:49 +02001833 case QCA6174_2_1_DEVICE_ID:
1834 switch (MS(ar->chip_id, SOC_CHIP_ID_REV)) {
1835 case QCA6174_HW_1_0_CHIP_ID_REV:
1836 case QCA6174_HW_1_1_CHIP_ID_REV:
Michal Kazior11a002e2015-04-20 09:20:41 +00001837 case QCA6174_HW_2_1_CHIP_ID_REV:
1838 case QCA6174_HW_2_2_CHIP_ID_REV:
Michal Kaziord63955b2015-01-24 12:14:49 +02001839 return 3;
1840 case QCA6174_HW_1_3_CHIP_ID_REV:
1841 return 2;
Michal Kaziord63955b2015-01-24 12:14:49 +02001842 case QCA6174_HW_3_0_CHIP_ID_REV:
1843 case QCA6174_HW_3_1_CHIP_ID_REV:
1844 case QCA6174_HW_3_2_CHIP_ID_REV:
1845 return 9;
1846 }
1847 break;
1848 }
1849
1850 ath10k_warn(ar, "unknown number of banks, assuming 1\n");
1851 return 1;
1852}
1853
Kalle Valo5e3dd152013-06-12 20:52:10 +03001854static int ath10k_pci_init_config(struct ath10k *ar)
1855{
1856 u32 interconnect_targ_addr;
1857 u32 pcie_state_targ_addr = 0;
1858 u32 pipe_cfg_targ_addr = 0;
1859 u32 svc_to_pipe_map = 0;
1860 u32 pcie_config_flags = 0;
1861 u32 ealloc_value;
1862 u32 ealloc_targ_addr;
1863 u32 flag2_value;
1864 u32 flag2_targ_addr;
1865 int ret = 0;
1866
1867 /* Download to Target the CE Config and the service-to-CE map */
1868 interconnect_targ_addr =
1869 host_interest_item_address(HI_ITEM(hi_interconnect_state));
1870
1871 /* Supply Target-side CE configuration */
Michal Kazior9e264942014-09-02 11:00:21 +03001872 ret = ath10k_pci_diag_read32(ar, interconnect_targ_addr,
1873 &pcie_state_targ_addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001874 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001875 ath10k_err(ar, "Failed to get pcie state addr: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001876 return ret;
1877 }
1878
1879 if (pcie_state_targ_addr == 0) {
1880 ret = -EIO;
Michal Kazior7aa7a722014-08-25 12:09:38 +02001881 ath10k_err(ar, "Invalid pcie state addr\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001882 return ret;
1883 }
1884
Michal Kazior9e264942014-09-02 11:00:21 +03001885 ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
Kalle Valo5e3dd152013-06-12 20:52:10 +03001886 offsetof(struct pcie_state,
Michal Kazior9e264942014-09-02 11:00:21 +03001887 pipe_cfg_addr)),
1888 &pipe_cfg_targ_addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001889 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001890 ath10k_err(ar, "Failed to get pipe cfg addr: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001891 return ret;
1892 }
1893
1894 if (pipe_cfg_targ_addr == 0) {
1895 ret = -EIO;
Michal Kazior7aa7a722014-08-25 12:09:38 +02001896 ath10k_err(ar, "Invalid pipe cfg addr\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001897 return ret;
1898 }
1899
1900 ret = ath10k_pci_diag_write_mem(ar, pipe_cfg_targ_addr,
Kalle Valo5b07e072014-09-14 12:50:06 +03001901 target_ce_config_wlan,
Vasanthakumar Thiagarajan050af062015-06-18 12:31:04 +05301902 sizeof(struct ce_pipe_config) *
1903 NUM_TARGET_CE_CONFIG_WLAN);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001904
1905 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001906 ath10k_err(ar, "Failed to write pipe cfg: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001907 return ret;
1908 }
1909
Michal Kazior9e264942014-09-02 11:00:21 +03001910 ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
Kalle Valo5e3dd152013-06-12 20:52:10 +03001911 offsetof(struct pcie_state,
Michal Kazior9e264942014-09-02 11:00:21 +03001912 svc_to_pipe_map)),
1913 &svc_to_pipe_map);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001914 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001915 ath10k_err(ar, "Failed to get svc/pipe map: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001916 return ret;
1917 }
1918
1919 if (svc_to_pipe_map == 0) {
1920 ret = -EIO;
Michal Kazior7aa7a722014-08-25 12:09:38 +02001921 ath10k_err(ar, "Invalid svc_to_pipe map\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001922 return ret;
1923 }
1924
1925 ret = ath10k_pci_diag_write_mem(ar, svc_to_pipe_map,
Kalle Valo5b07e072014-09-14 12:50:06 +03001926 target_service_to_ce_map_wlan,
1927 sizeof(target_service_to_ce_map_wlan));
Kalle Valo5e3dd152013-06-12 20:52:10 +03001928 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001929 ath10k_err(ar, "Failed to write svc/pipe map: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001930 return ret;
1931 }
1932
Michal Kazior9e264942014-09-02 11:00:21 +03001933 ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
Kalle Valo5e3dd152013-06-12 20:52:10 +03001934 offsetof(struct pcie_state,
Michal Kazior9e264942014-09-02 11:00:21 +03001935 config_flags)),
1936 &pcie_config_flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001937 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001938 ath10k_err(ar, "Failed to get pcie config_flags: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001939 return ret;
1940 }
1941
1942 pcie_config_flags &= ~PCIE_CONFIG_FLAG_ENABLE_L1;
1943
Michal Kazior9e264942014-09-02 11:00:21 +03001944 ret = ath10k_pci_diag_write32(ar, (pcie_state_targ_addr +
1945 offsetof(struct pcie_state,
1946 config_flags)),
1947 pcie_config_flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001948 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001949 ath10k_err(ar, "Failed to write pcie config_flags: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001950 return ret;
1951 }
1952
1953 /* configure early allocation */
1954 ealloc_targ_addr = host_interest_item_address(HI_ITEM(hi_early_alloc));
1955
Michal Kazior9e264942014-09-02 11:00:21 +03001956 ret = ath10k_pci_diag_read32(ar, ealloc_targ_addr, &ealloc_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001957 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001958 ath10k_err(ar, "Faile to get early alloc val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001959 return ret;
1960 }
1961
1962 /* first bank is switched to IRAM */
1963 ealloc_value |= ((HI_EARLY_ALLOC_MAGIC << HI_EARLY_ALLOC_MAGIC_SHIFT) &
1964 HI_EARLY_ALLOC_MAGIC_MASK);
Michal Kaziord63955b2015-01-24 12:14:49 +02001965 ealloc_value |= ((ath10k_pci_get_num_banks(ar) <<
1966 HI_EARLY_ALLOC_IRAM_BANKS_SHIFT) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03001967 HI_EARLY_ALLOC_IRAM_BANKS_MASK);
1968
Michal Kazior9e264942014-09-02 11:00:21 +03001969 ret = ath10k_pci_diag_write32(ar, ealloc_targ_addr, ealloc_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001970 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001971 ath10k_err(ar, "Failed to set early alloc val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001972 return ret;
1973 }
1974
1975 /* Tell Target to proceed with initialization */
1976 flag2_targ_addr = host_interest_item_address(HI_ITEM(hi_option_flag2));
1977
Michal Kazior9e264942014-09-02 11:00:21 +03001978 ret = ath10k_pci_diag_read32(ar, flag2_targ_addr, &flag2_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001979 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001980 ath10k_err(ar, "Failed to get option val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001981 return ret;
1982 }
1983
1984 flag2_value |= HI_OPTION_EARLY_CFG_DONE;
1985
Michal Kazior9e264942014-09-02 11:00:21 +03001986 ret = ath10k_pci_diag_write32(ar, flag2_targ_addr, flag2_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001987 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001988 ath10k_err(ar, "Failed to set option val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001989 return ret;
1990 }
1991
1992 return 0;
1993}
1994
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001995static int ath10k_pci_alloc_pipes(struct ath10k *ar)
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001996{
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001997 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1998 struct ath10k_pci_pipe *pipe;
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001999 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002000
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002001 for (i = 0; i < CE_COUNT; i++) {
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002002 pipe = &ar_pci->pipe_info[i];
2003 pipe->ce_hdl = &ar_pci->ce_states[i];
2004 pipe->pipe_num = i;
2005 pipe->hif_ce_state = ar;
2006
2007 ret = ath10k_ce_alloc_pipe(ar, i, &host_ce_config_wlan[i],
2008 ath10k_pci_ce_send_done,
2009 ath10k_pci_ce_recv_data);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002010 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002011 ath10k_err(ar, "failed to allocate copy engine pipe %d: %d\n",
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002012 i, ret);
2013 return ret;
2014 }
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002015
2016 /* Last CE is Diagnostic Window */
Vasanthakumar Thiagarajan050af062015-06-18 12:31:04 +05302017 if (i == CE_DIAG_PIPE) {
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002018 ar_pci->ce_diag = pipe->ce_hdl;
2019 continue;
2020 }
2021
2022 pipe->buf_sz = (size_t)(host_ce_config_wlan[i].src_sz_max);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002023 }
2024
2025 return 0;
2026}
2027
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002028static void ath10k_pci_free_pipes(struct ath10k *ar)
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002029{
2030 int i;
2031
2032 for (i = 0; i < CE_COUNT; i++)
2033 ath10k_ce_free_pipe(ar, i);
2034}
Kalle Valo5e3dd152013-06-12 20:52:10 +03002035
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002036static int ath10k_pci_init_pipes(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002037{
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002038 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002039
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002040 for (i = 0; i < CE_COUNT; i++) {
2041 ret = ath10k_ce_init_pipe(ar, i, &host_ce_config_wlan[i]);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002042 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002043 ath10k_err(ar, "failed to initialize copy engine pipe %d: %d\n",
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002044 i, ret);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002045 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002046 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002047 }
2048
Kalle Valo5e3dd152013-06-12 20:52:10 +03002049 return 0;
2050}
2051
Michal Kazior5c771e72014-08-22 14:23:34 +02002052static bool ath10k_pci_has_fw_crashed(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002053{
Michal Kazior5c771e72014-08-22 14:23:34 +02002054 return ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS) &
2055 FW_IND_EVENT_PENDING;
2056}
Kalle Valo5e3dd152013-06-12 20:52:10 +03002057
Michal Kazior5c771e72014-08-22 14:23:34 +02002058static void ath10k_pci_fw_crashed_clear(struct ath10k *ar)
2059{
2060 u32 val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002061
Michal Kazior5c771e72014-08-22 14:23:34 +02002062 val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
2063 val &= ~FW_IND_EVENT_PENDING;
2064 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002065}
2066
Michal Kaziorde013572014-05-14 16:56:16 +03002067/* this function effectively clears target memory controller assert line */
2068static void ath10k_pci_warm_reset_si0(struct ath10k *ar)
2069{
2070 u32 val;
2071
2072 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
2073 ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
2074 val | SOC_RESET_CONTROL_SI0_RST_MASK);
2075 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
2076
2077 msleep(10);
2078
2079 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
2080 ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
2081 val & ~SOC_RESET_CONTROL_SI0_RST_MASK);
2082 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
2083
2084 msleep(10);
2085}
2086
Michal Kazior61c16482014-10-28 10:32:06 +01002087static void ath10k_pci_warm_reset_cpu(struct ath10k *ar)
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002088{
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002089 u32 val;
2090
Kalle Valob39712c2014-03-28 09:32:46 +02002091 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, 0);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002092
Michal Kazior61c16482014-10-28 10:32:06 +01002093 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
2094 SOC_RESET_CONTROL_ADDRESS);
2095 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
2096 val | SOC_RESET_CONTROL_CPU_WARM_RST_MASK);
2097}
2098
2099static void ath10k_pci_warm_reset_ce(struct ath10k *ar)
2100{
2101 u32 val;
2102
2103 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
2104 SOC_RESET_CONTROL_ADDRESS);
2105
2106 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
2107 val | SOC_RESET_CONTROL_CE_RST_MASK);
2108 msleep(10);
2109 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
2110 val & ~SOC_RESET_CONTROL_CE_RST_MASK);
2111}
2112
2113static void ath10k_pci_warm_reset_clear_lf(struct ath10k *ar)
2114{
2115 u32 val;
2116
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002117 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
2118 SOC_LF_TIMER_CONTROL0_ADDRESS);
2119 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS +
2120 SOC_LF_TIMER_CONTROL0_ADDRESS,
2121 val & ~SOC_LF_TIMER_CONTROL0_ENABLE_MASK);
Michal Kazior61c16482014-10-28 10:32:06 +01002122}
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002123
Michal Kazior61c16482014-10-28 10:32:06 +01002124static int ath10k_pci_warm_reset(struct ath10k *ar)
2125{
2126 int ret;
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002127
Michal Kazior61c16482014-10-28 10:32:06 +01002128 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot warm reset\n");
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002129
Michal Kazior61c16482014-10-28 10:32:06 +01002130 spin_lock_bh(&ar->data_lock);
2131 ar->stats.fw_warm_reset_counter++;
2132 spin_unlock_bh(&ar->data_lock);
2133
2134 ath10k_pci_irq_disable(ar);
2135
2136 /* Make sure the target CPU is not doing anything dangerous, e.g. if it
2137 * were to access copy engine while host performs copy engine reset
2138 * then it is possible for the device to confuse pci-e controller to
2139 * the point of bringing host system to a complete stop (i.e. hang).
2140 */
Michal Kaziorde013572014-05-14 16:56:16 +03002141 ath10k_pci_warm_reset_si0(ar);
Michal Kazior61c16482014-10-28 10:32:06 +01002142 ath10k_pci_warm_reset_cpu(ar);
2143 ath10k_pci_init_pipes(ar);
2144 ath10k_pci_wait_for_target_init(ar);
Michal Kaziorde013572014-05-14 16:56:16 +03002145
Michal Kazior61c16482014-10-28 10:32:06 +01002146 ath10k_pci_warm_reset_clear_lf(ar);
2147 ath10k_pci_warm_reset_ce(ar);
2148 ath10k_pci_warm_reset_cpu(ar);
2149 ath10k_pci_init_pipes(ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002150
Michal Kazior61c16482014-10-28 10:32:06 +01002151 ret = ath10k_pci_wait_for_target_init(ar);
2152 if (ret) {
2153 ath10k_warn(ar, "failed to wait for target init: %d\n", ret);
2154 return ret;
2155 }
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002156
Michal Kazior7aa7a722014-08-25 12:09:38 +02002157 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot warm reset complete\n");
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002158
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002159 return 0;
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002160}
2161
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +05302162static int ath10k_pci_safe_chip_reset(struct ath10k *ar)
2163{
2164 if (QCA_REV_988X(ar) || QCA_REV_6174(ar)) {
2165 return ath10k_pci_warm_reset(ar);
2166 } else if (QCA_REV_99X0(ar)) {
2167 ath10k_pci_irq_disable(ar);
2168 return ath10k_pci_qca99x0_chip_reset(ar);
2169 } else {
2170 return -ENOTSUPP;
2171 }
2172}
2173
Michal Kaziord63955b2015-01-24 12:14:49 +02002174static int ath10k_pci_qca988x_chip_reset(struct ath10k *ar)
Michal Kazior0bc14d02014-10-28 10:32:07 +01002175{
2176 int i, ret;
2177 u32 val;
2178
Michal Kaziord63955b2015-01-24 12:14:49 +02002179 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot 988x chip reset\n");
Michal Kazior0bc14d02014-10-28 10:32:07 +01002180
2181 /* Some hardware revisions (e.g. CUS223v2) has issues with cold reset.
2182 * It is thus preferred to use warm reset which is safer but may not be
2183 * able to recover the device from all possible fail scenarios.
2184 *
2185 * Warm reset doesn't always work on first try so attempt it a few
2186 * times before giving up.
2187 */
2188 for (i = 0; i < ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS; i++) {
2189 ret = ath10k_pci_warm_reset(ar);
2190 if (ret) {
2191 ath10k_warn(ar, "failed to warm reset attempt %d of %d: %d\n",
2192 i + 1, ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS,
2193 ret);
2194 continue;
2195 }
2196
2197 /* FIXME: Sometimes copy engine doesn't recover after warm
2198 * reset. In most cases this needs cold reset. In some of these
2199 * cases the device is in such a state that a cold reset may
2200 * lock up the host.
2201 *
2202 * Reading any host interest register via copy engine is
2203 * sufficient to verify if device is capable of booting
2204 * firmware blob.
2205 */
2206 ret = ath10k_pci_init_pipes(ar);
2207 if (ret) {
2208 ath10k_warn(ar, "failed to init copy engine: %d\n",
2209 ret);
2210 continue;
2211 }
2212
2213 ret = ath10k_pci_diag_read32(ar, QCA988X_HOST_INTEREST_ADDRESS,
2214 &val);
2215 if (ret) {
2216 ath10k_warn(ar, "failed to poke copy engine: %d\n",
2217 ret);
2218 continue;
2219 }
2220
2221 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot chip reset complete (warm)\n");
2222 return 0;
2223 }
2224
2225 if (ath10k_pci_reset_mode == ATH10K_PCI_RESET_WARM_ONLY) {
2226 ath10k_warn(ar, "refusing cold reset as requested\n");
2227 return -EPERM;
2228 }
2229
2230 ret = ath10k_pci_cold_reset(ar);
2231 if (ret) {
2232 ath10k_warn(ar, "failed to cold reset: %d\n", ret);
2233 return ret;
2234 }
2235
2236 ret = ath10k_pci_wait_for_target_init(ar);
2237 if (ret) {
2238 ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
2239 ret);
2240 return ret;
2241 }
2242
Michal Kaziord63955b2015-01-24 12:14:49 +02002243 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca988x chip reset complete (cold)\n");
Michal Kazior0bc14d02014-10-28 10:32:07 +01002244
2245 return 0;
2246}
2247
Michal Kaziord63955b2015-01-24 12:14:49 +02002248static int ath10k_pci_qca6174_chip_reset(struct ath10k *ar)
2249{
2250 int ret;
2251
2252 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca6174 chip reset\n");
2253
2254 /* FIXME: QCA6174 requires cold + warm reset to work. */
2255
2256 ret = ath10k_pci_cold_reset(ar);
2257 if (ret) {
2258 ath10k_warn(ar, "failed to cold reset: %d\n", ret);
2259 return ret;
2260 }
2261
2262 ret = ath10k_pci_wait_for_target_init(ar);
2263 if (ret) {
2264 ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
Kalle Valo617b0f42015-10-05 17:56:35 +03002265 ret);
Michal Kaziord63955b2015-01-24 12:14:49 +02002266 return ret;
2267 }
2268
2269 ret = ath10k_pci_warm_reset(ar);
2270 if (ret) {
2271 ath10k_warn(ar, "failed to warm reset: %d\n", ret);
2272 return ret;
2273 }
2274
2275 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca6174 chip reset complete (cold)\n");
2276
2277 return 0;
2278}
2279
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +05302280static int ath10k_pci_qca99x0_chip_reset(struct ath10k *ar)
2281{
2282 int ret;
2283
2284 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca99x0 chip reset\n");
2285
2286 ret = ath10k_pci_cold_reset(ar);
2287 if (ret) {
2288 ath10k_warn(ar, "failed to cold reset: %d\n", ret);
2289 return ret;
2290 }
2291
2292 ret = ath10k_pci_wait_for_target_init(ar);
2293 if (ret) {
2294 ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
2295 ret);
2296 return ret;
2297 }
2298
2299 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca99x0 chip reset complete (cold)\n");
2300
2301 return 0;
2302}
2303
Michal Kaziord63955b2015-01-24 12:14:49 +02002304static int ath10k_pci_chip_reset(struct ath10k *ar)
2305{
2306 if (QCA_REV_988X(ar))
2307 return ath10k_pci_qca988x_chip_reset(ar);
2308 else if (QCA_REV_6174(ar))
2309 return ath10k_pci_qca6174_chip_reset(ar);
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +05302310 else if (QCA_REV_99X0(ar))
2311 return ath10k_pci_qca99x0_chip_reset(ar);
Michal Kaziord63955b2015-01-24 12:14:49 +02002312 else
2313 return -ENOTSUPP;
2314}
2315
Michal Kazior0bc14d02014-10-28 10:32:07 +01002316static int ath10k_pci_hif_power_up(struct ath10k *ar)
Michal Kazior8c5c5362013-07-16 09:38:50 +02002317{
Janusz Dziedzic76d870e2015-05-18 09:38:16 +00002318 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02002319 int ret;
2320
Michal Kazior0bc14d02014-10-28 10:32:07 +01002321 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif power up\n");
2322
Janusz Dziedzic76d870e2015-05-18 09:38:16 +00002323 pcie_capability_read_word(ar_pci->pdev, PCI_EXP_LNKCTL,
2324 &ar_pci->link_ctl);
2325 pcie_capability_write_word(ar_pci->pdev, PCI_EXP_LNKCTL,
2326 ar_pci->link_ctl & ~PCI_EXP_LNKCTL_ASPMC);
2327
Michal Kazior8c5c5362013-07-16 09:38:50 +02002328 /*
2329 * Bring the target up cleanly.
2330 *
2331 * The target may be in an undefined state with an AUX-powered Target
2332 * and a Host in WoW mode. If the Host crashes, loses power, or is
2333 * restarted (without unloading the driver) then the Target is left
2334 * (aux) powered and running. On a subsequent driver load, the Target
2335 * is in an unexpected state. We try to catch that here in order to
2336 * reset the Target and retry the probe.
2337 */
Michal Kazior0bc14d02014-10-28 10:32:07 +01002338 ret = ath10k_pci_chip_reset(ar);
Michal Kazior5b2589f2013-11-08 08:01:30 +01002339 if (ret) {
Michal Kaziora2fa8802015-01-12 15:29:37 +01002340 if (ath10k_pci_has_fw_crashed(ar)) {
2341 ath10k_warn(ar, "firmware crashed during chip reset\n");
2342 ath10k_pci_fw_crashed_clear(ar);
2343 ath10k_pci_fw_crashed_dump(ar);
2344 }
2345
Michal Kazior0bc14d02014-10-28 10:32:07 +01002346 ath10k_err(ar, "failed to reset chip: %d\n", ret);
Bartosz Markowski707b1bbd2014-10-31 09:03:43 +01002347 goto err_sleep;
Michal Kazior5b2589f2013-11-08 08:01:30 +01002348 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02002349
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002350 ret = ath10k_pci_init_pipes(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02002351 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002352 ath10k_err(ar, "failed to initialize CE: %d\n", ret);
Bartosz Markowski707b1bbd2014-10-31 09:03:43 +01002353 goto err_sleep;
Michal Kaziorab977bd2013-11-25 14:06:26 +01002354 }
2355
Michal Kazior98563d52013-11-08 08:01:33 +01002356 ret = ath10k_pci_init_config(ar);
2357 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002358 ath10k_err(ar, "failed to setup init config: %d\n", ret);
Michal Kazior5c771e72014-08-22 14:23:34 +02002359 goto err_ce;
Michal Kazior98563d52013-11-08 08:01:33 +01002360 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02002361
2362 ret = ath10k_pci_wake_target_cpu(ar);
2363 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002364 ath10k_err(ar, "could not wake up target CPU: %d\n", ret);
Michal Kazior5c771e72014-08-22 14:23:34 +02002365 goto err_ce;
Michal Kazior8c5c5362013-07-16 09:38:50 +02002366 }
2367
2368 return 0;
2369
2370err_ce:
2371 ath10k_pci_ce_deinit(ar);
Michal Kazior0bc14d02014-10-28 10:32:07 +01002372
Bartosz Markowski707b1bbd2014-10-31 09:03:43 +01002373err_sleep:
Michal Kazior8c5c5362013-07-16 09:38:50 +02002374 return ret;
2375}
2376
2377static void ath10k_pci_hif_power_down(struct ath10k *ar)
2378{
Michal Kazior7aa7a722014-08-25 12:09:38 +02002379 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif power down\n");
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002380
Michal Kaziorc011b282014-10-28 10:32:08 +01002381 /* Currently hif_power_up performs effectively a reset and hif_stop
2382 * resets the chip as well so there's no point in resetting here.
2383 */
Michal Kazior8c5c5362013-07-16 09:38:50 +02002384}
2385
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002386#ifdef CONFIG_PM
2387
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002388static int ath10k_pci_hif_suspend(struct ath10k *ar)
2389{
Michal Kazior77258d42015-05-18 09:38:18 +00002390 /* The grace timer can still be counting down and ar->ps_awake be true.
2391 * It is known that the device may be asleep after resuming regardless
2392 * of the SoC powersave state before suspending. Hence make sure the
2393 * device is asleep before proceeding.
2394 */
2395 ath10k_pci_sleep_sync(ar);
Michal Kazior320e14b2015-03-02 13:22:13 +01002396
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002397 return 0;
2398}
2399
2400static int ath10k_pci_hif_resume(struct ath10k *ar)
2401{
2402 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2403 struct pci_dev *pdev = ar_pci->pdev;
2404 u32 val;
2405
Michal Kazior9ff4be92015-03-02 13:22:14 +01002406 /* Suspend/Resume resets the PCI configuration space, so we have to
2407 * re-disable the RETRY_TIMEOUT register (0x41) to keep PCI Tx retries
2408 * from interfering with C3 CPU state. pci_restore_state won't help
2409 * here since it only restores the first 64 bytes pci config header.
2410 */
2411 pci_read_config_dword(pdev, 0x40, &val);
2412 if ((val & 0x0000ff00) != 0)
2413 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002414
Michal Kazior77258d42015-05-18 09:38:18 +00002415 return 0;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002416}
2417#endif
2418
Kalle Valo5e3dd152013-06-12 20:52:10 +03002419static const struct ath10k_hif_ops ath10k_pci_hif_ops = {
Michal Kazior726346f2014-02-27 18:50:04 +02002420 .tx_sg = ath10k_pci_hif_tx_sg,
Kalle Valoeef25402014-09-24 14:16:52 +03002421 .diag_read = ath10k_pci_hif_diag_read,
Yanbo Li9f65ad22014-11-25 12:24:48 +02002422 .diag_write = ath10k_pci_diag_write_mem,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002423 .exchange_bmi_msg = ath10k_pci_hif_exchange_bmi_msg,
2424 .start = ath10k_pci_hif_start,
2425 .stop = ath10k_pci_hif_stop,
2426 .map_service_to_pipe = ath10k_pci_hif_map_service_to_pipe,
2427 .get_default_pipe = ath10k_pci_hif_get_default_pipe,
2428 .send_complete_check = ath10k_pci_hif_send_complete_check,
Michal Kaziore799bbf2013-07-05 16:15:12 +03002429 .set_callbacks = ath10k_pci_hif_set_callbacks,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002430 .get_free_queue_number = ath10k_pci_hif_get_free_queue_number,
Michal Kazior8c5c5362013-07-16 09:38:50 +02002431 .power_up = ath10k_pci_hif_power_up,
2432 .power_down = ath10k_pci_hif_power_down,
Yanbo Li077a3802014-11-25 12:24:33 +02002433 .read32 = ath10k_pci_read32,
2434 .write32 = ath10k_pci_write32,
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002435#ifdef CONFIG_PM
2436 .suspend = ath10k_pci_hif_suspend,
2437 .resume = ath10k_pci_hif_resume,
2438#endif
Kalle Valo5e3dd152013-06-12 20:52:10 +03002439};
2440
2441static void ath10k_pci_ce_tasklet(unsigned long ptr)
2442{
Michal Kazior87263e52013-08-27 13:08:01 +02002443 struct ath10k_pci_pipe *pipe = (struct ath10k_pci_pipe *)ptr;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002444 struct ath10k_pci *ar_pci = pipe->ar_pci;
2445
2446 ath10k_ce_per_engine_service(ar_pci->ar, pipe->pipe_num);
2447}
2448
2449static void ath10k_msi_err_tasklet(unsigned long data)
2450{
2451 struct ath10k *ar = (struct ath10k *)data;
2452
Michal Kazior5c771e72014-08-22 14:23:34 +02002453 if (!ath10k_pci_has_fw_crashed(ar)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002454 ath10k_warn(ar, "received unsolicited fw crash interrupt\n");
Michal Kazior5c771e72014-08-22 14:23:34 +02002455 return;
2456 }
2457
Michal Kazior6f3b7ff2015-01-24 12:14:52 +02002458 ath10k_pci_irq_disable(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02002459 ath10k_pci_fw_crashed_clear(ar);
2460 ath10k_pci_fw_crashed_dump(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002461}
2462
2463/*
2464 * Handler for a per-engine interrupt on a PARTICULAR CE.
2465 * This is used in cases where each CE has a private MSI interrupt.
2466 */
2467static irqreturn_t ath10k_pci_per_engine_handler(int irq, void *arg)
2468{
2469 struct ath10k *ar = arg;
2470 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2471 int ce_id = irq - ar_pci->pdev->irq - MSI_ASSIGN_CE_INITIAL;
2472
Dan Carpentere5742672013-06-18 10:28:46 +03002473 if (ce_id < 0 || ce_id >= ARRAY_SIZE(ar_pci->pipe_info)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002474 ath10k_warn(ar, "unexpected/invalid irq %d ce_id %d\n", irq,
2475 ce_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002476 return IRQ_HANDLED;
2477 }
2478
2479 /*
2480 * NOTE: We are able to derive ce_id from irq because we
2481 * use a one-to-one mapping for CE's 0..5.
2482 * CE's 6 & 7 do not use interrupts at all.
2483 *
2484 * This mapping must be kept in sync with the mapping
2485 * used by firmware.
2486 */
2487 tasklet_schedule(&ar_pci->pipe_info[ce_id].intr);
2488 return IRQ_HANDLED;
2489}
2490
2491static irqreturn_t ath10k_pci_msi_fw_handler(int irq, void *arg)
2492{
2493 struct ath10k *ar = arg;
2494 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2495
2496 tasklet_schedule(&ar_pci->msi_fw_err);
2497 return IRQ_HANDLED;
2498}
2499
2500/*
2501 * Top-level interrupt handler for all PCI interrupts from a Target.
2502 * When a block of MSI interrupts is allocated, this top-level handler
2503 * is not used; instead, we directly call the correct sub-handler.
2504 */
2505static irqreturn_t ath10k_pci_interrupt_handler(int irq, void *arg)
2506{
2507 struct ath10k *ar = arg;
2508 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2509
2510 if (ar_pci->num_msi_intrs == 0) {
Michal Kaziore5398872013-11-25 14:06:20 +01002511 if (!ath10k_pci_irq_pending(ar))
2512 return IRQ_NONE;
2513
Michal Kazior26852182013-11-25 14:06:25 +01002514 ath10k_pci_disable_and_clear_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002515 }
2516
2517 tasklet_schedule(&ar_pci->intr_tq);
2518
2519 return IRQ_HANDLED;
2520}
2521
2522static void ath10k_pci_tasklet(unsigned long data)
2523{
2524 struct ath10k *ar = (struct ath10k *)data;
2525 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2526
Michal Kazior5c771e72014-08-22 14:23:34 +02002527 if (ath10k_pci_has_fw_crashed(ar)) {
Michal Kazior6f3b7ff2015-01-24 12:14:52 +02002528 ath10k_pci_irq_disable(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02002529 ath10k_pci_fw_crashed_clear(ar);
2530 ath10k_pci_fw_crashed_dump(ar);
2531 return;
2532 }
2533
Kalle Valo5e3dd152013-06-12 20:52:10 +03002534 ath10k_ce_per_engine_service_any(ar);
2535
Michal Kazior26852182013-11-25 14:06:25 +01002536 /* Re-enable legacy irq that was disabled in the irq handler */
2537 if (ar_pci->num_msi_intrs == 0)
2538 ath10k_pci_enable_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002539}
2540
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002541static int ath10k_pci_request_irq_msix(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002542{
2543 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002544 int ret, i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002545
2546 ret = request_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW,
2547 ath10k_pci_msi_fw_handler,
2548 IRQF_SHARED, "ath10k_pci", ar);
Michal Kazior591ecdb2013-07-31 10:55:15 +02002549 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002550 ath10k_warn(ar, "failed to request MSI-X fw irq %d: %d\n",
Michal Kazior591ecdb2013-07-31 10:55:15 +02002551 ar_pci->pdev->irq + MSI_ASSIGN_FW, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002552 return ret;
Michal Kazior591ecdb2013-07-31 10:55:15 +02002553 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002554
2555 for (i = MSI_ASSIGN_CE_INITIAL; i <= MSI_ASSIGN_CE_MAX; i++) {
2556 ret = request_irq(ar_pci->pdev->irq + i,
2557 ath10k_pci_per_engine_handler,
2558 IRQF_SHARED, "ath10k_pci", ar);
2559 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002560 ath10k_warn(ar, "failed to request MSI-X ce irq %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002561 ar_pci->pdev->irq + i, ret);
2562
Michal Kazior87b14232013-06-26 08:50:50 +02002563 for (i--; i >= MSI_ASSIGN_CE_INITIAL; i--)
2564 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002565
Michal Kazior87b14232013-06-26 08:50:50 +02002566 free_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002567 return ret;
2568 }
2569 }
2570
Kalle Valo5e3dd152013-06-12 20:52:10 +03002571 return 0;
2572}
2573
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002574static int ath10k_pci_request_irq_msi(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002575{
2576 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2577 int ret;
2578
2579 ret = request_irq(ar_pci->pdev->irq,
2580 ath10k_pci_interrupt_handler,
2581 IRQF_SHARED, "ath10k_pci", ar);
Kalle Valof3782742013-10-17 11:36:15 +03002582 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002583 ath10k_warn(ar, "failed to request MSI irq %d: %d\n",
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002584 ar_pci->pdev->irq, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002585 return ret;
Kalle Valof3782742013-10-17 11:36:15 +03002586 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002587
Kalle Valo5e3dd152013-06-12 20:52:10 +03002588 return 0;
2589}
2590
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002591static int ath10k_pci_request_irq_legacy(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002592{
2593 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002594 int ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002595
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002596 ret = request_irq(ar_pci->pdev->irq,
2597 ath10k_pci_interrupt_handler,
2598 IRQF_SHARED, "ath10k_pci", ar);
Kalle Valof3782742013-10-17 11:36:15 +03002599 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002600 ath10k_warn(ar, "failed to request legacy irq %d: %d\n",
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002601 ar_pci->pdev->irq, ret);
Kalle Valof3782742013-10-17 11:36:15 +03002602 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002603 }
2604
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002605 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002606}
2607
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002608static int ath10k_pci_request_irq(struct ath10k *ar)
2609{
2610 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2611
2612 switch (ar_pci->num_msi_intrs) {
2613 case 0:
2614 return ath10k_pci_request_irq_legacy(ar);
2615 case 1:
2616 return ath10k_pci_request_irq_msi(ar);
Janusz Dziedzicb8402d82015-09-17 08:17:33 +02002617 default:
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002618 return ath10k_pci_request_irq_msix(ar);
2619 }
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002620}
2621
2622static void ath10k_pci_free_irq(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002623{
2624 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2625 int i;
2626
2627 /* There's at least one interrupt irregardless whether its legacy INTR
2628 * or MSI or MSI-X */
2629 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
2630 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002631}
2632
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002633static void ath10k_pci_init_irq_tasklets(struct ath10k *ar)
2634{
2635 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2636 int i;
2637
2638 tasklet_init(&ar_pci->intr_tq, ath10k_pci_tasklet, (unsigned long)ar);
2639 tasklet_init(&ar_pci->msi_fw_err, ath10k_msi_err_tasklet,
2640 (unsigned long)ar);
2641
2642 for (i = 0; i < CE_COUNT; i++) {
2643 ar_pci->pipe_info[i].ar_pci = ar_pci;
2644 tasklet_init(&ar_pci->pipe_info[i].intr, ath10k_pci_ce_tasklet,
2645 (unsigned long)&ar_pci->pipe_info[i]);
2646 }
2647}
2648
2649static int ath10k_pci_init_irq(struct ath10k *ar)
2650{
2651 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2652 int ret;
2653
2654 ath10k_pci_init_irq_tasklets(ar);
2655
Michal Kazior403d6272014-08-22 14:23:31 +02002656 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_AUTO)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002657 ath10k_info(ar, "limiting irq mode to: %d\n",
2658 ath10k_pci_irq_mode);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002659
2660 /* Try MSI-X */
Michal Kazior0edf2572014-08-07 11:03:29 +02002661 if (ath10k_pci_irq_mode == ATH10K_PCI_IRQ_AUTO) {
Janusz Dziedzicb8402d82015-09-17 08:17:33 +02002662 ar_pci->num_msi_intrs = MSI_ASSIGN_CE_MAX + 1;
Alexander Gordeev5ad68672014-02-13 17:50:02 +02002663 ret = pci_enable_msi_range(ar_pci->pdev, ar_pci->num_msi_intrs,
Kalle Valo5b07e072014-09-14 12:50:06 +03002664 ar_pci->num_msi_intrs);
Alexander Gordeev5ad68672014-02-13 17:50:02 +02002665 if (ret > 0)
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002666 return 0;
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002667
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002668 /* fall-through */
2669 }
2670
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002671 /* Try MSI */
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002672 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_LEGACY) {
2673 ar_pci->num_msi_intrs = 1;
2674 ret = pci_enable_msi(ar_pci->pdev);
2675 if (ret == 0)
2676 return 0;
2677
2678 /* fall-through */
2679 }
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002680
2681 /* Try legacy irq
2682 *
2683 * A potential race occurs here: The CORE_BASE write
2684 * depends on target correctly decoding AXI address but
2685 * host won't know when target writes BAR to CORE_CTRL.
2686 * This write might get lost if target has NOT written BAR.
2687 * For now, fix the race by repeating the write in below
2688 * synchronization checking. */
2689 ar_pci->num_msi_intrs = 0;
2690
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002691 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2692 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002693
2694 return 0;
2695}
2696
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002697static void ath10k_pci_deinit_irq_legacy(struct ath10k *ar)
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002698{
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002699 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2700 0);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002701}
2702
2703static int ath10k_pci_deinit_irq(struct ath10k *ar)
2704{
2705 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2706
2707 switch (ar_pci->num_msi_intrs) {
2708 case 0:
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002709 ath10k_pci_deinit_irq_legacy(ar);
Janusz Dziedzicb8402d82015-09-17 08:17:33 +02002710 break;
Alexander Gordeevbb8b6212014-02-13 17:50:01 +02002711 default:
2712 pci_disable_msi(ar_pci->pdev);
Janusz Dziedzicb8402d82015-09-17 08:17:33 +02002713 break;
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002714 }
2715
Janusz Dziedzicb8402d82015-09-17 08:17:33 +02002716 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002717}
2718
Michal Kaziord7fb47f2013-11-08 08:01:26 +01002719static int ath10k_pci_wait_for_target_init(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002720{
2721 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo0399eca2014-03-28 09:32:21 +02002722 unsigned long timeout;
Kalle Valo0399eca2014-03-28 09:32:21 +02002723 u32 val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002724
Michal Kazior7aa7a722014-08-25 12:09:38 +02002725 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot waiting target to initialise\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002726
Kalle Valo0399eca2014-03-28 09:32:21 +02002727 timeout = jiffies + msecs_to_jiffies(ATH10K_PCI_TARGET_WAIT);
2728
2729 do {
2730 val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
2731
Michal Kazior7aa7a722014-08-25 12:09:38 +02002732 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target indicator %x\n",
2733 val);
Kalle Valo50f87a62014-03-28 09:32:52 +02002734
Kalle Valo0399eca2014-03-28 09:32:21 +02002735 /* target should never return this */
2736 if (val == 0xffffffff)
2737 continue;
2738
Michal Kazior7710cd22014-04-23 19:30:04 +03002739 /* the device has crashed so don't bother trying anymore */
2740 if (val & FW_IND_EVENT_PENDING)
2741 break;
2742
Kalle Valo0399eca2014-03-28 09:32:21 +02002743 if (val & FW_IND_INITIALIZED)
2744 break;
2745
Kalle Valo5e3dd152013-06-12 20:52:10 +03002746 if (ar_pci->num_msi_intrs == 0)
2747 /* Fix potential race by repeating CORE_BASE writes */
Michal Kaziora4282492014-10-20 14:14:37 +02002748 ath10k_pci_enable_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002749
Kalle Valo0399eca2014-03-28 09:32:21 +02002750 mdelay(10);
2751 } while (time_before(jiffies, timeout));
2752
Michal Kaziora4282492014-10-20 14:14:37 +02002753 ath10k_pci_disable_and_clear_legacy_irq(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02002754 ath10k_pci_irq_msi_fw_mask(ar);
Michal Kaziora4282492014-10-20 14:14:37 +02002755
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002756 if (val == 0xffffffff) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002757 ath10k_err(ar, "failed to read device register, device is gone\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002758 return -EIO;
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002759 }
2760
Michal Kazior7710cd22014-04-23 19:30:04 +03002761 if (val & FW_IND_EVENT_PENDING) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002762 ath10k_warn(ar, "device has crashed during init\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002763 return -ECOMM;
Michal Kazior7710cd22014-04-23 19:30:04 +03002764 }
2765
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002766 if (!(val & FW_IND_INITIALIZED)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002767 ath10k_err(ar, "failed to receive initialized event from target: %08x\n",
Kalle Valo0399eca2014-03-28 09:32:21 +02002768 val);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002769 return -ETIMEDOUT;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002770 }
2771
Michal Kazior7aa7a722014-08-25 12:09:38 +02002772 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target initialised\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002773 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002774}
2775
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002776static int ath10k_pci_cold_reset(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002777{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002778 u32 val;
2779
Michal Kazior7aa7a722014-08-25 12:09:38 +02002780 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cold reset\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002781
Ben Greearf51dbe72014-09-29 14:41:46 +03002782 spin_lock_bh(&ar->data_lock);
2783
2784 ar->stats.fw_cold_reset_counter++;
2785
2786 spin_unlock_bh(&ar->data_lock);
2787
Kalle Valo5e3dd152013-06-12 20:52:10 +03002788 /* Put Target, including PCIe, into RESET. */
Kalle Valoe479ed42013-09-01 10:01:53 +03002789 val = ath10k_pci_reg_read32(ar, SOC_GLOBAL_RESET_ADDRESS);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002790 val |= 1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002791 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002792
Vasanthakumar Thiagarajanacd19582015-07-10 14:31:20 +05302793 /* After writing into SOC_GLOBAL_RESET to put device into
2794 * reset and pulling out of reset pcie may not be stable
2795 * for any immediate pcie register access and cause bus error,
2796 * add delay before any pcie access request to fix this issue.
2797 */
2798 msleep(20);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002799
2800 /* Pull Target, including PCIe, out of RESET. */
2801 val &= ~1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002802 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002803
Vasanthakumar Thiagarajanacd19582015-07-10 14:31:20 +05302804 msleep(20);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002805
Michal Kazior7aa7a722014-08-25 12:09:38 +02002806 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cold reset complete\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02002807
Michal Kazior5b2589f2013-11-08 08:01:30 +01002808 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002809}
2810
Michal Kazior2986e3e2014-08-07 11:03:30 +02002811static int ath10k_pci_claim(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002812{
Michal Kazior2986e3e2014-08-07 11:03:30 +02002813 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2814 struct pci_dev *pdev = ar_pci->pdev;
Michal Kazior2986e3e2014-08-07 11:03:30 +02002815 int ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002816
2817 pci_set_drvdata(pdev, ar);
2818
Kalle Valo5e3dd152013-06-12 20:52:10 +03002819 ret = pci_enable_device(pdev);
2820 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002821 ath10k_err(ar, "failed to enable pci device: %d\n", ret);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002822 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002823 }
2824
Kalle Valo5e3dd152013-06-12 20:52:10 +03002825 ret = pci_request_region(pdev, BAR_NUM, "ath");
2826 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002827 ath10k_err(ar, "failed to request region BAR%d: %d\n", BAR_NUM,
Michal Kazior2986e3e2014-08-07 11:03:30 +02002828 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002829 goto err_device;
2830 }
2831
Michal Kazior2986e3e2014-08-07 11:03:30 +02002832 /* Target expects 32 bit DMA. Enforce it. */
Kalle Valo5e3dd152013-06-12 20:52:10 +03002833 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2834 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002835 ath10k_err(ar, "failed to set dma mask to 32-bit: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002836 goto err_region;
2837 }
2838
2839 ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2840 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002841 ath10k_err(ar, "failed to set consistent dma mask to 32-bit: %d\n",
Michal Kazior2986e3e2014-08-07 11:03:30 +02002842 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002843 goto err_region;
2844 }
2845
Kalle Valo5e3dd152013-06-12 20:52:10 +03002846 pci_set_master(pdev);
2847
Kalle Valo5e3dd152013-06-12 20:52:10 +03002848 /* Arrange for access to Target SoC registers. */
Michal Kazioraeae5b42015-06-15 14:46:42 +03002849 ar_pci->mem_len = pci_resource_len(pdev, BAR_NUM);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002850 ar_pci->mem = pci_iomap(pdev, BAR_NUM, 0);
2851 if (!ar_pci->mem) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002852 ath10k_err(ar, "failed to iomap BAR%d\n", BAR_NUM);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002853 ret = -EIO;
2854 goto err_master;
2855 }
2856
Michal Kazior7aa7a722014-08-25 12:09:38 +02002857 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot pci_mem 0x%p\n", ar_pci->mem);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002858 return 0;
2859
2860err_master:
2861 pci_clear_master(pdev);
2862
2863err_region:
2864 pci_release_region(pdev, BAR_NUM);
2865
2866err_device:
2867 pci_disable_device(pdev);
2868
2869 return ret;
2870}
2871
2872static void ath10k_pci_release(struct ath10k *ar)
2873{
2874 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2875 struct pci_dev *pdev = ar_pci->pdev;
2876
2877 pci_iounmap(pdev, ar_pci->mem);
2878 pci_release_region(pdev, BAR_NUM);
2879 pci_clear_master(pdev);
2880 pci_disable_device(pdev);
2881}
2882
Michal Kazior7505f7c2014-12-02 10:55:54 +02002883static bool ath10k_pci_chip_is_supported(u32 dev_id, u32 chip_id)
2884{
2885 const struct ath10k_pci_supp_chip *supp_chip;
2886 int i;
2887 u32 rev_id = MS(chip_id, SOC_CHIP_ID_REV);
2888
2889 for (i = 0; i < ARRAY_SIZE(ath10k_pci_supp_chips); i++) {
2890 supp_chip = &ath10k_pci_supp_chips[i];
2891
2892 if (supp_chip->dev_id == dev_id &&
2893 supp_chip->rev_id == rev_id)
2894 return true;
2895 }
2896
2897 return false;
2898}
2899
Kalle Valo5e3dd152013-06-12 20:52:10 +03002900static int ath10k_pci_probe(struct pci_dev *pdev,
2901 const struct pci_device_id *pci_dev)
2902{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002903 int ret = 0;
2904 struct ath10k *ar;
2905 struct ath10k_pci *ar_pci;
Michal Kaziord63955b2015-01-24 12:14:49 +02002906 enum ath10k_hw_rev hw_rev;
Michal Kazior2986e3e2014-08-07 11:03:30 +02002907 u32 chip_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002908
Michal Kaziord63955b2015-01-24 12:14:49 +02002909 switch (pci_dev->device) {
2910 case QCA988X_2_0_DEVICE_ID:
2911 hw_rev = ATH10K_HW_QCA988X;
2912 break;
Michal Kazior36582e52015-08-13 14:32:26 +02002913 case QCA6164_2_1_DEVICE_ID:
Michal Kaziord63955b2015-01-24 12:14:49 +02002914 case QCA6174_2_1_DEVICE_ID:
2915 hw_rev = ATH10K_HW_QCA6174;
2916 break;
Vasanthakumar Thiagarajan8bd47022015-06-18 12:31:03 +05302917 case QCA99X0_2_0_DEVICE_ID:
2918 hw_rev = ATH10K_HW_QCA99X0;
2919 break;
Michal Kaziord63955b2015-01-24 12:14:49 +02002920 default:
2921 WARN_ON(1);
2922 return -ENOTSUPP;
2923 }
2924
2925 ar = ath10k_core_create(sizeof(*ar_pci), &pdev->dev, ATH10K_BUS_PCI,
2926 hw_rev, &ath10k_pci_hif_ops);
Michal Kaziore7b54192014-08-07 11:03:27 +02002927 if (!ar) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002928 dev_err(&pdev->dev, "failed to allocate core\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002929 return -ENOMEM;
Michal Kaziore7b54192014-08-07 11:03:27 +02002930 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002931
Michal Kazior7aa7a722014-08-25 12:09:38 +02002932 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci probe\n");
2933
Michal Kaziore7b54192014-08-07 11:03:27 +02002934 ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002935 ar_pci->pdev = pdev;
2936 ar_pci->dev = &pdev->dev;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002937 ar_pci->ar = ar;
Michal Kazior36582e52015-08-13 14:32:26 +02002938 ar->dev_id = pci_dev->device;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002939
Michal Kaziorde57e2c2015-04-17 09:19:17 +00002940 if (pdev->subsystem_vendor || pdev->subsystem_device)
2941 scnprintf(ar->spec_board_id, sizeof(ar->spec_board_id),
2942 "%04x:%04x:%04x:%04x",
2943 pdev->vendor, pdev->device,
2944 pdev->subsystem_vendor, pdev->subsystem_device);
2945
Kalle Valo5e3dd152013-06-12 20:52:10 +03002946 spin_lock_init(&ar_pci->ce_lock);
Michal Kazior77258d42015-05-18 09:38:18 +00002947 spin_lock_init(&ar_pci->ps_lock);
2948
Michal Kazior728f95e2014-08-22 14:33:14 +02002949 setup_timer(&ar_pci->rx_post_retry, ath10k_pci_rx_replenish_retry,
2950 (unsigned long)ar);
Michal Kazior77258d42015-05-18 09:38:18 +00002951 setup_timer(&ar_pci->ps_timer, ath10k_pci_ps_timer,
2952 (unsigned long)ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002953
Michal Kazior2986e3e2014-08-07 11:03:30 +02002954 ret = ath10k_pci_claim(ar);
Kalle Valoe01ae682013-09-01 11:22:14 +03002955 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002956 ath10k_err(ar, "failed to claim device: %d\n", ret);
Michal Kaziore7b54192014-08-07 11:03:27 +02002957 goto err_core_destroy;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002958 }
2959
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002960 ret = ath10k_pci_alloc_pipes(ar);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002961 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002962 ath10k_err(ar, "failed to allocate copy engine pipes: %d\n",
2963 ret);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002964 goto err_sleep;
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002965 }
2966
Michal Kazior403d6272014-08-22 14:23:31 +02002967 ath10k_pci_ce_deinit(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02002968 ath10k_pci_irq_disable(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02002969
Michal Kazior403d6272014-08-22 14:23:31 +02002970 ret = ath10k_pci_init_irq(ar);
2971 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002972 ath10k_err(ar, "failed to init irqs: %d\n", ret);
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002973 goto err_free_pipes;
Michal Kazior403d6272014-08-22 14:23:31 +02002974 }
2975
Michal Kazior7aa7a722014-08-25 12:09:38 +02002976 ath10k_info(ar, "pci irq %s interrupts %d irq_mode %d reset_mode %d\n",
Michal Kazior403d6272014-08-22 14:23:31 +02002977 ath10k_pci_get_irq_method(ar), ar_pci->num_msi_intrs,
2978 ath10k_pci_irq_mode, ath10k_pci_reset_mode);
2979
Michal Kazior5c771e72014-08-22 14:23:34 +02002980 ret = ath10k_pci_request_irq(ar);
Michal Kazior403d6272014-08-22 14:23:31 +02002981 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002982 ath10k_warn(ar, "failed to request irqs: %d\n", ret);
Michal Kazior403d6272014-08-22 14:23:31 +02002983 goto err_deinit_irq;
2984 }
2985
Michal Kazior1a7fecb2015-01-24 12:14:48 +02002986 ret = ath10k_pci_chip_reset(ar);
2987 if (ret) {
2988 ath10k_err(ar, "failed to reset chip: %d\n", ret);
2989 goto err_free_irq;
2990 }
2991
2992 chip_id = ath10k_pci_soc_read32(ar, SOC_CHIP_ID_ADDRESS);
2993 if (chip_id == 0xffffffff) {
2994 ath10k_err(ar, "failed to get chip id\n");
2995 goto err_free_irq;
2996 }
2997
2998 if (!ath10k_pci_chip_is_supported(pdev->device, chip_id)) {
2999 ath10k_err(ar, "device %04x with chip_id %08x isn't supported\n",
3000 pdev->device, chip_id);
Michal Kaziord9585a92015-04-10 13:01:27 +00003001 goto err_free_irq;
Michal Kazior1a7fecb2015-01-24 12:14:48 +02003002 }
3003
Kalle Valoe01ae682013-09-01 11:22:14 +03003004 ret = ath10k_core_register(ar, chip_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003005 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003006 ath10k_err(ar, "failed to register driver core: %d\n", ret);
Michal Kazior5c771e72014-08-22 14:23:34 +02003007 goto err_free_irq;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003008 }
3009
3010 return 0;
3011
Michal Kazior5c771e72014-08-22 14:23:34 +02003012err_free_irq:
3013 ath10k_pci_free_irq(ar);
Michal Kazior21396272014-08-28 10:24:40 +02003014 ath10k_pci_kill_tasklet(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02003015
Michal Kazior403d6272014-08-22 14:23:31 +02003016err_deinit_irq:
3017 ath10k_pci_deinit_irq(ar);
3018
Michal Kazior84cbf3a2014-10-20 14:14:39 +02003019err_free_pipes:
3020 ath10k_pci_free_pipes(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02003021
Michal Kaziorc0c378f2014-08-07 11:03:28 +02003022err_sleep:
Michal Kazior0bcbbe62015-05-29 07:35:24 +02003023 ath10k_pci_sleep_sync(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02003024 ath10k_pci_release(ar);
3025
Michal Kaziore7b54192014-08-07 11:03:27 +02003026err_core_destroy:
Kalle Valo5e3dd152013-06-12 20:52:10 +03003027 ath10k_core_destroy(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003028
3029 return ret;
3030}
3031
3032static void ath10k_pci_remove(struct pci_dev *pdev)
3033{
3034 struct ath10k *ar = pci_get_drvdata(pdev);
3035 struct ath10k_pci *ar_pci;
3036
Michal Kazior7aa7a722014-08-25 12:09:38 +02003037 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci remove\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03003038
3039 if (!ar)
3040 return;
3041
3042 ar_pci = ath10k_pci_priv(ar);
3043
3044 if (!ar_pci)
3045 return;
3046
Kalle Valo5e3dd152013-06-12 20:52:10 +03003047 ath10k_core_unregister(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02003048 ath10k_pci_free_irq(ar);
Michal Kazior21396272014-08-28 10:24:40 +02003049 ath10k_pci_kill_tasklet(ar);
Michal Kazior403d6272014-08-22 14:23:31 +02003050 ath10k_pci_deinit_irq(ar);
3051 ath10k_pci_ce_deinit(ar);
Michal Kazior84cbf3a2014-10-20 14:14:39 +02003052 ath10k_pci_free_pipes(ar);
Michal Kazior77258d42015-05-18 09:38:18 +00003053 ath10k_pci_sleep_sync(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02003054 ath10k_pci_release(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003055 ath10k_core_destroy(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003056}
3057
Kalle Valo5e3dd152013-06-12 20:52:10 +03003058MODULE_DEVICE_TABLE(pci, ath10k_pci_id_table);
3059
3060static struct pci_driver ath10k_pci_driver = {
3061 .name = "ath10k_pci",
3062 .id_table = ath10k_pci_id_table,
3063 .probe = ath10k_pci_probe,
3064 .remove = ath10k_pci_remove,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003065};
3066
3067static int __init ath10k_pci_init(void)
3068{
3069 int ret;
3070
3071 ret = pci_register_driver(&ath10k_pci_driver);
3072 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003073 printk(KERN_ERR "failed to register ath10k pci driver: %d\n",
3074 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003075
3076 return ret;
3077}
3078module_init(ath10k_pci_init);
3079
3080static void __exit ath10k_pci_exit(void)
3081{
3082 pci_unregister_driver(&ath10k_pci_driver);
3083}
3084
3085module_exit(ath10k_pci_exit);
3086
3087MODULE_AUTHOR("Qualcomm Atheros");
3088MODULE_DESCRIPTION("Driver support for Atheros QCA988X PCIe devices");
3089MODULE_LICENSE("Dual BSD/GPL");
Bartosz Markowski5c427f52015-02-18 13:16:37 +01003090
3091/* QCA988x 2.0 firmware files */
Bartosz Markowski8026cae2014-10-06 14:16:41 +02003092MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_FW_FILE);
3093MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API2_FILE);
3094MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API3_FILE);
Bartosz Markowski5c427f52015-02-18 13:16:37 +01003095MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API4_FILE);
Kalle Valo53513c32015-03-25 13:12:42 +02003096MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API5_FILE);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003097MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_BOARD_DATA_FILE);
Bartosz Markowski5c427f52015-02-18 13:16:37 +01003098
3099/* QCA6174 2.1 firmware files */
3100MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" ATH10K_FW_API4_FILE);
Michal Kaziore451c1d2015-05-26 13:09:22 +02003101MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" ATH10K_FW_API5_FILE);
Bartosz Markowski5c427f52015-02-18 13:16:37 +01003102MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" QCA6174_HW_2_1_BOARD_DATA_FILE);
3103
3104/* QCA6174 3.1 firmware files */
3105MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" ATH10K_FW_API4_FILE);
Michal Kaziore451c1d2015-05-26 13:09:22 +02003106MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" ATH10K_FW_API5_FILE);
Bartosz Markowski5c427f52015-02-18 13:16:37 +01003107MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" QCA6174_HW_3_0_BOARD_DATA_FILE);