blob: 863117bd18ea9eb08f636a051dcf6de4dc3a3c7f [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 skb = dev_alloc_skb(pipe->buf_sz);
690 if (!skb)
691 return -ENOMEM;
692
693 WARN_ONCE((unsigned long)skb->data & 3, "unaligned skb");
694
695 paddr = dma_map_single(ar->dev, skb->data,
696 skb->len + skb_tailroom(skb),
697 DMA_FROM_DEVICE);
698 if (unlikely(dma_mapping_error(ar->dev, paddr))) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200699 ath10k_warn(ar, "failed to dma map pci rx buf\n");
Michal Kazior728f95e2014-08-22 14:33:14 +0200700 dev_kfree_skb_any(skb);
701 return -EIO;
702 }
703
Michal Kazior8582bf32015-01-24 12:14:47 +0200704 ATH10K_SKB_RXCB(skb)->paddr = paddr;
Michal Kazior728f95e2014-08-22 14:33:14 +0200705
Rajkumar Manoharanab4e3db2015-10-06 15:19:33 +0300706 spin_lock_bh(&ar_pci->ce_lock);
Michal Kazior728f95e2014-08-22 14:33:14 +0200707 ret = __ath10k_ce_rx_post_buf(ce_pipe, skb, paddr);
Rajkumar Manoharanab4e3db2015-10-06 15:19:33 +0300708 spin_unlock_bh(&ar_pci->ce_lock);
Michal Kaziorab977bd2013-11-25 14:06:26 +0100709 if (ret) {
Michal Kazior728f95e2014-08-22 14:33:14 +0200710 dma_unmap_single(ar->dev, paddr, skb->len + skb_tailroom(skb),
711 DMA_FROM_DEVICE);
712 dev_kfree_skb_any(skb);
Michal Kaziorab977bd2013-11-25 14:06:26 +0100713 return ret;
714 }
715
716 return 0;
717}
718
Rajkumar Manoharanab4e3db2015-10-06 15:19:33 +0300719static void ath10k_pci_rx_post_pipe(struct ath10k_pci_pipe *pipe)
Michal Kaziorab977bd2013-11-25 14:06:26 +0100720{
Michal Kazior728f95e2014-08-22 14:33:14 +0200721 struct ath10k *ar = pipe->hif_ce_state;
722 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
723 struct ath10k_ce_pipe *ce_pipe = pipe->ce_hdl;
724 int ret, num;
725
Michal Kazior728f95e2014-08-22 14:33:14 +0200726 if (pipe->buf_sz == 0)
727 return;
728
729 if (!ce_pipe->dest_ring)
730 return;
731
Rajkumar Manoharanab4e3db2015-10-06 15:19:33 +0300732 spin_lock_bh(&ar_pci->ce_lock);
Michal Kazior728f95e2014-08-22 14:33:14 +0200733 num = __ath10k_ce_rx_num_free_bufs(ce_pipe);
Rajkumar Manoharanab4e3db2015-10-06 15:19:33 +0300734 spin_unlock_bh(&ar_pci->ce_lock);
Michal Kazior728f95e2014-08-22 14:33:14 +0200735 while (num--) {
736 ret = __ath10k_pci_rx_post_buf(pipe);
737 if (ret) {
Rajkumar Manoharanab4e3db2015-10-06 15:19:33 +0300738 if (ret == -ENOSPC)
739 break;
Michal Kazior7aa7a722014-08-25 12:09:38 +0200740 ath10k_warn(ar, "failed to post pci rx buf: %d\n", ret);
Michal Kazior728f95e2014-08-22 14:33:14 +0200741 mod_timer(&ar_pci->rx_post_retry, jiffies +
742 ATH10K_PCI_RX_POST_RETRY_MS);
743 break;
744 }
745 }
746}
747
Michal Kazior728f95e2014-08-22 14:33:14 +0200748static void ath10k_pci_rx_post(struct ath10k *ar)
749{
750 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
751 int i;
752
Michal Kazior728f95e2014-08-22 14:33:14 +0200753 for (i = 0; i < CE_COUNT; i++)
Rajkumar Manoharanab4e3db2015-10-06 15:19:33 +0300754 ath10k_pci_rx_post_pipe(&ar_pci->pipe_info[i]);
Michal Kazior728f95e2014-08-22 14:33:14 +0200755}
756
757static void ath10k_pci_rx_replenish_retry(unsigned long ptr)
758{
759 struct ath10k *ar = (void *)ptr;
760
761 ath10k_pci_rx_post(ar);
Michal Kaziorab977bd2013-11-25 14:06:26 +0100762}
763
Vasanthakumar Thiagarajan418ca592015-06-18 12:31:05 +0530764static u32 ath10k_pci_targ_cpu_to_ce_addr(struct ath10k *ar, u32 addr)
765{
766 u32 val = 0;
767
768 switch (ar->hw_rev) {
769 case ATH10K_HW_QCA988X:
770 case ATH10K_HW_QCA6174:
771 val = (ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
772 CORE_CTRL_ADDRESS) &
Vasanthakumar Thiagarajan3c7e2562015-07-03 19:25:27 +0530773 0x7ff) << 21;
Vasanthakumar Thiagarajan418ca592015-06-18 12:31:05 +0530774 break;
775 case ATH10K_HW_QCA99X0:
776 val = ath10k_pci_read32(ar, PCIE_BAR_REG_ADDRESS);
777 break;
778 }
779
780 val |= 0x100000 | (addr & 0xfffff);
781 return val;
782}
783
Kalle Valo5e3dd152013-06-12 20:52:10 +0300784/*
785 * Diagnostic read/write access is provided for startup/config/debug usage.
786 * Caller must guarantee proper alignment, when applicable, and single user
787 * at any moment.
788 */
789static int ath10k_pci_diag_read_mem(struct ath10k *ar, u32 address, void *data,
790 int nbytes)
791{
792 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
793 int ret = 0;
794 u32 buf;
795 unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
796 unsigned int id;
797 unsigned int flags;
Michal Kazior2aa39112013-08-27 13:08:02 +0200798 struct ath10k_ce_pipe *ce_diag;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300799 /* Host buffer address in CE space */
800 u32 ce_data;
801 dma_addr_t ce_data_base = 0;
802 void *data_buf = NULL;
803 int i;
804
Kalle Valoeef25402014-09-24 14:16:52 +0300805 spin_lock_bh(&ar_pci->ce_lock);
806
Kalle Valo5e3dd152013-06-12 20:52:10 +0300807 ce_diag = ar_pci->ce_diag;
808
809 /*
810 * Allocate a temporary bounce buffer to hold caller's data
811 * to be DMA'ed from Target. This guarantees
812 * 1) 4-byte alignment
813 * 2) Buffer in DMA-able space
814 */
815 orig_nbytes = nbytes;
Michal Kazior68c03242014-03-28 10:02:35 +0200816 data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
817 orig_nbytes,
818 &ce_data_base,
819 GFP_ATOMIC);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300820
821 if (!data_buf) {
822 ret = -ENOMEM;
823 goto done;
824 }
825 memset(data_buf, 0, orig_nbytes);
826
827 remaining_bytes = orig_nbytes;
828 ce_data = ce_data_base;
829 while (remaining_bytes) {
830 nbytes = min_t(unsigned int, remaining_bytes,
831 DIAG_TRANSFER_LIMIT);
832
Kalle Valoeef25402014-09-24 14:16:52 +0300833 ret = __ath10k_ce_rx_post_buf(ce_diag, NULL, ce_data);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300834 if (ret != 0)
835 goto done;
836
837 /* Request CE to send from Target(!) address to Host buffer */
838 /*
839 * The address supplied by the caller is in the
840 * Target CPU virtual address space.
841 *
842 * In order to use this address with the diagnostic CE,
843 * convert it from Target CPU virtual address space
844 * to CE address space
845 */
Vasanthakumar Thiagarajan418ca592015-06-18 12:31:05 +0530846 address = ath10k_pci_targ_cpu_to_ce_addr(ar, address);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300847
Kalle Valoeef25402014-09-24 14:16:52 +0300848 ret = ath10k_ce_send_nolock(ce_diag, NULL, (u32)address, nbytes, 0,
849 0);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300850 if (ret)
851 goto done;
852
853 i = 0;
Kalle Valoeef25402014-09-24 14:16:52 +0300854 while (ath10k_ce_completed_send_next_nolock(ce_diag, NULL, &buf,
855 &completed_nbytes,
856 &id) != 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300857 mdelay(1);
858 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
859 ret = -EBUSY;
860 goto done;
861 }
862 }
863
864 if (nbytes != completed_nbytes) {
865 ret = -EIO;
866 goto done;
867 }
868
Kalle Valocfbc06a2014-09-14 12:50:23 +0300869 if (buf != (u32)address) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300870 ret = -EIO;
871 goto done;
872 }
873
874 i = 0;
Kalle Valoeef25402014-09-24 14:16:52 +0300875 while (ath10k_ce_completed_recv_next_nolock(ce_diag, NULL, &buf,
876 &completed_nbytes,
877 &id, &flags) != 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300878 mdelay(1);
879
880 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
881 ret = -EBUSY;
882 goto done;
883 }
884 }
885
886 if (nbytes != completed_nbytes) {
887 ret = -EIO;
888 goto done;
889 }
890
891 if (buf != ce_data) {
892 ret = -EIO;
893 goto done;
894 }
895
896 remaining_bytes -= nbytes;
897 address += nbytes;
898 ce_data += nbytes;
899 }
900
901done:
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300902 if (ret == 0)
903 memcpy(data, data_buf, orig_nbytes);
904 else
Michal Kazior7aa7a722014-08-25 12:09:38 +0200905 ath10k_warn(ar, "failed to read diag value at 0x%x: %d\n",
Kalle Valo50f87a62014-03-28 09:32:52 +0200906 address, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300907
908 if (data_buf)
Michal Kazior68c03242014-03-28 10:02:35 +0200909 dma_free_coherent(ar->dev, orig_nbytes, data_buf,
910 ce_data_base);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300911
Kalle Valoeef25402014-09-24 14:16:52 +0300912 spin_unlock_bh(&ar_pci->ce_lock);
913
Kalle Valo5e3dd152013-06-12 20:52:10 +0300914 return ret;
915}
916
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300917static int ath10k_pci_diag_read32(struct ath10k *ar, u32 address, u32 *value)
918{
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300919 __le32 val = 0;
920 int ret;
921
922 ret = ath10k_pci_diag_read_mem(ar, address, &val, sizeof(val));
923 *value = __le32_to_cpu(val);
924
925 return ret;
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300926}
927
928static int __ath10k_pci_diag_read_hi(struct ath10k *ar, void *dest,
929 u32 src, u32 len)
930{
931 u32 host_addr, addr;
932 int ret;
933
934 host_addr = host_interest_item_address(src);
935
936 ret = ath10k_pci_diag_read32(ar, host_addr, &addr);
937 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200938 ath10k_warn(ar, "failed to get memcpy hi address for firmware address %d: %d\n",
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300939 src, ret);
940 return ret;
941 }
942
943 ret = ath10k_pci_diag_read_mem(ar, addr, dest, len);
944 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200945 ath10k_warn(ar, "failed to memcpy firmware memory from %d (%d B): %d\n",
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300946 addr, len, ret);
947 return ret;
948 }
949
950 return 0;
951}
952
953#define ath10k_pci_diag_read_hi(ar, dest, src, len) \
Kalle Valo8cc7f262014-09-14 12:50:39 +0300954 __ath10k_pci_diag_read_hi(ar, dest, HI_ITEM(src), len)
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300955
Kalle Valo5e3dd152013-06-12 20:52:10 +0300956static int ath10k_pci_diag_write_mem(struct ath10k *ar, u32 address,
957 const void *data, int nbytes)
958{
959 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
960 int ret = 0;
961 u32 buf;
962 unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
963 unsigned int id;
964 unsigned int flags;
Michal Kazior2aa39112013-08-27 13:08:02 +0200965 struct ath10k_ce_pipe *ce_diag;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300966 void *data_buf = NULL;
967 u32 ce_data; /* Host buffer address in CE space */
968 dma_addr_t ce_data_base = 0;
969 int i;
970
Kalle Valoeef25402014-09-24 14:16:52 +0300971 spin_lock_bh(&ar_pci->ce_lock);
972
Kalle Valo5e3dd152013-06-12 20:52:10 +0300973 ce_diag = ar_pci->ce_diag;
974
975 /*
976 * Allocate a temporary bounce buffer to hold caller's data
977 * to be DMA'ed to Target. This guarantees
978 * 1) 4-byte alignment
979 * 2) Buffer in DMA-able space
980 */
981 orig_nbytes = nbytes;
Michal Kazior68c03242014-03-28 10:02:35 +0200982 data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
983 orig_nbytes,
984 &ce_data_base,
985 GFP_ATOMIC);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300986 if (!data_buf) {
987 ret = -ENOMEM;
988 goto done;
989 }
990
991 /* Copy caller's data to allocated DMA buf */
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300992 memcpy(data_buf, data, orig_nbytes);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300993
994 /*
995 * The address supplied by the caller is in the
996 * Target CPU virtual address space.
997 *
998 * In order to use this address with the diagnostic CE,
999 * convert it from
1000 * Target CPU virtual address space
1001 * to
1002 * CE address space
1003 */
Vasanthakumar Thiagarajan418ca592015-06-18 12:31:05 +05301004 address = ath10k_pci_targ_cpu_to_ce_addr(ar, address);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001005
1006 remaining_bytes = orig_nbytes;
1007 ce_data = ce_data_base;
1008 while (remaining_bytes) {
1009 /* FIXME: check cast */
1010 nbytes = min_t(int, remaining_bytes, DIAG_TRANSFER_LIMIT);
1011
1012 /* Set up to receive directly into Target(!) address */
Kalle Valoeef25402014-09-24 14:16:52 +03001013 ret = __ath10k_ce_rx_post_buf(ce_diag, NULL, address);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001014 if (ret != 0)
1015 goto done;
1016
1017 /*
1018 * Request CE to send caller-supplied data that
1019 * was copied to bounce buffer to Target(!) address.
1020 */
Kalle Valoeef25402014-09-24 14:16:52 +03001021 ret = ath10k_ce_send_nolock(ce_diag, NULL, (u32)ce_data,
1022 nbytes, 0, 0);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001023 if (ret != 0)
1024 goto done;
1025
1026 i = 0;
Kalle Valoeef25402014-09-24 14:16:52 +03001027 while (ath10k_ce_completed_send_next_nolock(ce_diag, NULL, &buf,
1028 &completed_nbytes,
1029 &id) != 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001030 mdelay(1);
1031
1032 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
1033 ret = -EBUSY;
1034 goto done;
1035 }
1036 }
1037
1038 if (nbytes != completed_nbytes) {
1039 ret = -EIO;
1040 goto done;
1041 }
1042
1043 if (buf != ce_data) {
1044 ret = -EIO;
1045 goto done;
1046 }
1047
1048 i = 0;
Kalle Valoeef25402014-09-24 14:16:52 +03001049 while (ath10k_ce_completed_recv_next_nolock(ce_diag, NULL, &buf,
1050 &completed_nbytes,
1051 &id, &flags) != 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001052 mdelay(1);
1053
1054 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
1055 ret = -EBUSY;
1056 goto done;
1057 }
1058 }
1059
1060 if (nbytes != completed_nbytes) {
1061 ret = -EIO;
1062 goto done;
1063 }
1064
1065 if (buf != address) {
1066 ret = -EIO;
1067 goto done;
1068 }
1069
1070 remaining_bytes -= nbytes;
1071 address += nbytes;
1072 ce_data += nbytes;
1073 }
1074
1075done:
1076 if (data_buf) {
Michal Kazior68c03242014-03-28 10:02:35 +02001077 dma_free_coherent(ar->dev, orig_nbytes, data_buf,
1078 ce_data_base);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001079 }
1080
1081 if (ret != 0)
Michal Kazior7aa7a722014-08-25 12:09:38 +02001082 ath10k_warn(ar, "failed to write diag value at 0x%x: %d\n",
Kalle Valo50f87a62014-03-28 09:32:52 +02001083 address, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001084
Kalle Valoeef25402014-09-24 14:16:52 +03001085 spin_unlock_bh(&ar_pci->ce_lock);
1086
Kalle Valo5e3dd152013-06-12 20:52:10 +03001087 return ret;
1088}
1089
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001090static int ath10k_pci_diag_write32(struct ath10k *ar, u32 address, u32 value)
1091{
1092 __le32 val = __cpu_to_le32(value);
1093
1094 return ath10k_pci_diag_write_mem(ar, address, &val, sizeof(val));
1095}
1096
Kalle Valo5e3dd152013-06-12 20:52:10 +03001097/* Called by lower (CE) layer when a send to Target completes. */
Michal Kazior5440ce22013-09-03 15:09:58 +02001098static void ath10k_pci_ce_send_done(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001099{
1100 struct ath10k *ar = ce_state->ar;
1101 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2f5280d2014-02-27 18:50:05 +02001102 struct ath10k_hif_cb *cb = &ar_pci->msg_callbacks_current;
Michal Kazior1cb86d42014-11-27 11:09:38 +01001103 struct sk_buff_head list;
1104 struct sk_buff *skb;
Michal Kazior5440ce22013-09-03 15:09:58 +02001105 u32 ce_data;
1106 unsigned int nbytes;
1107 unsigned int transfer_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001108
Michal Kazior1cb86d42014-11-27 11:09:38 +01001109 __skb_queue_head_init(&list);
1110 while (ath10k_ce_completed_send_next(ce_state, (void **)&skb, &ce_data,
1111 &nbytes, &transfer_id) == 0) {
Michal Kaziora16942e2014-02-27 18:50:04 +02001112 /* no need to call tx completion for NULL pointers */
Michal Kazior1cb86d42014-11-27 11:09:38 +01001113 if (skb == NULL)
Michal Kazior726346f2014-02-27 18:50:04 +02001114 continue;
1115
Michal Kazior1cb86d42014-11-27 11:09:38 +01001116 __skb_queue_tail(&list, skb);
Michal Kazior5440ce22013-09-03 15:09:58 +02001117 }
Michal Kazior1cb86d42014-11-27 11:09:38 +01001118
1119 while ((skb = __skb_dequeue(&list)))
1120 cb->tx_completion(ar, skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001121}
1122
1123/* Called by lower (CE) layer when data is received from the Target. */
Michal Kazior5440ce22013-09-03 15:09:58 +02001124static void ath10k_pci_ce_recv_data(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001125{
1126 struct ath10k *ar = ce_state->ar;
1127 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +02001128 struct ath10k_pci_pipe *pipe_info = &ar_pci->pipe_info[ce_state->id];
Michal Kazior2f5280d2014-02-27 18:50:05 +02001129 struct ath10k_hif_cb *cb = &ar_pci->msg_callbacks_current;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001130 struct sk_buff *skb;
Michal Kazior1cb86d42014-11-27 11:09:38 +01001131 struct sk_buff_head list;
Michal Kazior5440ce22013-09-03 15:09:58 +02001132 void *transfer_context;
1133 u32 ce_data;
Michal Kazior2f5280d2014-02-27 18:50:05 +02001134 unsigned int nbytes, max_nbytes;
Michal Kazior5440ce22013-09-03 15:09:58 +02001135 unsigned int transfer_id;
1136 unsigned int flags;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001137
Michal Kazior1cb86d42014-11-27 11:09:38 +01001138 __skb_queue_head_init(&list);
Michal Kazior5440ce22013-09-03 15:09:58 +02001139 while (ath10k_ce_completed_recv_next(ce_state, &transfer_context,
1140 &ce_data, &nbytes, &transfer_id,
1141 &flags) == 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001142 skb = transfer_context;
Michal Kazior2f5280d2014-02-27 18:50:05 +02001143 max_nbytes = skb->len + skb_tailroom(skb);
Michal Kazior8582bf32015-01-24 12:14:47 +02001144 dma_unmap_single(ar->dev, ATH10K_SKB_RXCB(skb)->paddr,
Michal Kazior2f5280d2014-02-27 18:50:05 +02001145 max_nbytes, DMA_FROM_DEVICE);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001146
Michal Kazior2f5280d2014-02-27 18:50:05 +02001147 if (unlikely(max_nbytes < nbytes)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001148 ath10k_warn(ar, "rxed more than expected (nbytes %d, max %d)",
Michal Kazior2f5280d2014-02-27 18:50:05 +02001149 nbytes, max_nbytes);
1150 dev_kfree_skb_any(skb);
1151 continue;
1152 }
1153
1154 skb_put(skb, nbytes);
Michal Kazior1cb86d42014-11-27 11:09:38 +01001155 __skb_queue_tail(&list, skb);
1156 }
Michal Kaziora360e542014-09-23 10:22:54 +02001157
Michal Kazior1cb86d42014-11-27 11:09:38 +01001158 while ((skb = __skb_dequeue(&list))) {
Michal Kaziora360e542014-09-23 10:22:54 +02001159 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci rx ce pipe %d len %d\n",
1160 ce_state->id, skb->len);
1161 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci rx: ",
1162 skb->data, skb->len);
1163
Michal Kazior5f07ea42014-11-27 11:09:36 +01001164 cb->rx_completion(ar, skb);
Michal Kazior2f5280d2014-02-27 18:50:05 +02001165 }
Michal Kaziorc29a3802014-07-21 21:03:10 +03001166
Michal Kazior728f95e2014-08-22 14:33:14 +02001167 ath10k_pci_rx_post_pipe(pipe_info);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001168}
1169
Michal Kazior726346f2014-02-27 18:50:04 +02001170static int ath10k_pci_hif_tx_sg(struct ath10k *ar, u8 pipe_id,
1171 struct ath10k_hif_sg_item *items, int n_items)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001172{
Kalle Valo5e3dd152013-06-12 20:52:10 +03001173 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior726346f2014-02-27 18:50:04 +02001174 struct ath10k_pci_pipe *pci_pipe = &ar_pci->pipe_info[pipe_id];
1175 struct ath10k_ce_pipe *ce_pipe = pci_pipe->ce_hdl;
1176 struct ath10k_ce_ring *src_ring = ce_pipe->src_ring;
Michal Kazior7147a132014-05-26 12:02:58 +02001177 unsigned int nentries_mask;
1178 unsigned int sw_index;
1179 unsigned int write_index;
Michal Kazior08b8aa02014-05-26 12:02:59 +02001180 int err, i = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001181
Michal Kazior726346f2014-02-27 18:50:04 +02001182 spin_lock_bh(&ar_pci->ce_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001183
Michal Kazior7147a132014-05-26 12:02:58 +02001184 nentries_mask = src_ring->nentries_mask;
1185 sw_index = src_ring->sw_index;
1186 write_index = src_ring->write_index;
1187
Michal Kazior726346f2014-02-27 18:50:04 +02001188 if (unlikely(CE_RING_DELTA(nentries_mask,
1189 write_index, sw_index - 1) < n_items)) {
1190 err = -ENOBUFS;
Michal Kazior08b8aa02014-05-26 12:02:59 +02001191 goto err;
Michal Kazior726346f2014-02-27 18:50:04 +02001192 }
1193
1194 for (i = 0; i < n_items - 1; i++) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001195 ath10k_dbg(ar, ATH10K_DBG_PCI,
Michal Kazior726346f2014-02-27 18:50:04 +02001196 "pci tx item %d paddr 0x%08x len %d n_items %d\n",
1197 i, items[i].paddr, items[i].len, n_items);
Michal Kazior7aa7a722014-08-25 12:09:38 +02001198 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci tx data: ",
Michal Kazior726346f2014-02-27 18:50:04 +02001199 items[i].vaddr, items[i].len);
1200
1201 err = ath10k_ce_send_nolock(ce_pipe,
1202 items[i].transfer_context,
1203 items[i].paddr,
1204 items[i].len,
1205 items[i].transfer_id,
1206 CE_SEND_FLAG_GATHER);
1207 if (err)
Michal Kazior08b8aa02014-05-26 12:02:59 +02001208 goto err;
Michal Kazior726346f2014-02-27 18:50:04 +02001209 }
1210
1211 /* `i` is equal to `n_items -1` after for() */
Kalle Valo5e3dd152013-06-12 20:52:10 +03001212
Michal Kazior7aa7a722014-08-25 12:09:38 +02001213 ath10k_dbg(ar, ATH10K_DBG_PCI,
Michal Kazior726346f2014-02-27 18:50:04 +02001214 "pci tx item %d paddr 0x%08x len %d n_items %d\n",
1215 i, items[i].paddr, items[i].len, n_items);
Michal Kazior7aa7a722014-08-25 12:09:38 +02001216 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci tx data: ",
Michal Kazior726346f2014-02-27 18:50:04 +02001217 items[i].vaddr, items[i].len);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001218
Michal Kazior726346f2014-02-27 18:50:04 +02001219 err = ath10k_ce_send_nolock(ce_pipe,
1220 items[i].transfer_context,
1221 items[i].paddr,
1222 items[i].len,
1223 items[i].transfer_id,
1224 0);
1225 if (err)
Michal Kazior08b8aa02014-05-26 12:02:59 +02001226 goto err;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001227
Michal Kazior08b8aa02014-05-26 12:02:59 +02001228 spin_unlock_bh(&ar_pci->ce_lock);
1229 return 0;
1230
1231err:
1232 for (; i > 0; i--)
1233 __ath10k_ce_send_revert(ce_pipe);
1234
Michal Kazior726346f2014-02-27 18:50:04 +02001235 spin_unlock_bh(&ar_pci->ce_lock);
1236 return err;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001237}
1238
Kalle Valoeef25402014-09-24 14:16:52 +03001239static int ath10k_pci_hif_diag_read(struct ath10k *ar, u32 address, void *buf,
1240 size_t buf_len)
1241{
1242 return ath10k_pci_diag_read_mem(ar, address, buf, buf_len);
1243}
1244
Kalle Valo5e3dd152013-06-12 20:52:10 +03001245static u16 ath10k_pci_hif_get_free_queue_number(struct ath10k *ar, u8 pipe)
1246{
1247 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo50f87a62014-03-28 09:32:52 +02001248
Michal Kazior7aa7a722014-08-25 12:09:38 +02001249 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif get free queue number\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001250
Michal Kazior3efcb3b2013-10-02 11:03:41 +02001251 return ath10k_ce_num_free_src_entries(ar_pci->pipe_info[pipe].ce_hdl);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001252}
1253
Ben Greear384914b2014-08-25 08:37:32 +03001254static void ath10k_pci_dump_registers(struct ath10k *ar,
1255 struct ath10k_fw_crash_data *crash_data)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001256{
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001257 __le32 reg_dump_values[REG_DUMP_COUNT_QCA988X] = {};
1258 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001259
Ben Greear384914b2014-08-25 08:37:32 +03001260 lockdep_assert_held(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001261
Kalle Valo3d29a3e2014-08-25 08:37:26 +03001262 ret = ath10k_pci_diag_read_hi(ar, &reg_dump_values[0],
1263 hi_failure_state,
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001264 REG_DUMP_COUNT_QCA988X * sizeof(__le32));
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001265 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001266 ath10k_err(ar, "failed to read firmware dump area: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001267 return;
1268 }
1269
1270 BUILD_BUG_ON(REG_DUMP_COUNT_QCA988X % 4);
1271
Michal Kazior7aa7a722014-08-25 12:09:38 +02001272 ath10k_err(ar, "firmware register dump:\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001273 for (i = 0; i < REG_DUMP_COUNT_QCA988X; i += 4)
Michal Kazior7aa7a722014-08-25 12:09:38 +02001274 ath10k_err(ar, "[%02d]: 0x%08X 0x%08X 0x%08X 0x%08X\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001275 i,
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001276 __le32_to_cpu(reg_dump_values[i]),
1277 __le32_to_cpu(reg_dump_values[i + 1]),
1278 __le32_to_cpu(reg_dump_values[i + 2]),
1279 __le32_to_cpu(reg_dump_values[i + 3]));
Michal Kazioraffd3212013-07-16 09:54:35 +02001280
Michal Kazior1bbb1192014-08-25 12:13:14 +02001281 if (!crash_data)
1282 return;
1283
Ben Greear384914b2014-08-25 08:37:32 +03001284 for (i = 0; i < REG_DUMP_COUNT_QCA988X; i++)
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001285 crash_data->registers[i] = reg_dump_values[i];
Ben Greear384914b2014-08-25 08:37:32 +03001286}
1287
Kalle Valo0e9848c2014-08-25 08:37:37 +03001288static void ath10k_pci_fw_crashed_dump(struct ath10k *ar)
Ben Greear384914b2014-08-25 08:37:32 +03001289{
1290 struct ath10k_fw_crash_data *crash_data;
1291 char uuid[50];
1292
1293 spin_lock_bh(&ar->data_lock);
1294
Ben Greearf51dbe72014-09-29 14:41:46 +03001295 ar->stats.fw_crash_counter++;
1296
Ben Greear384914b2014-08-25 08:37:32 +03001297 crash_data = ath10k_debug_get_new_fw_crash_data(ar);
1298
1299 if (crash_data)
1300 scnprintf(uuid, sizeof(uuid), "%pUl", &crash_data->uuid);
1301 else
1302 scnprintf(uuid, sizeof(uuid), "n/a");
1303
Michal Kazior7aa7a722014-08-25 12:09:38 +02001304 ath10k_err(ar, "firmware crashed! (uuid %s)\n", uuid);
Kalle Valo8a0c7972014-08-25 08:37:45 +03001305 ath10k_print_driver_info(ar);
Ben Greear384914b2014-08-25 08:37:32 +03001306 ath10k_pci_dump_registers(ar, crash_data);
1307
Ben Greear384914b2014-08-25 08:37:32 +03001308 spin_unlock_bh(&ar->data_lock);
Michal Kazioraffd3212013-07-16 09:54:35 +02001309
Michal Kazior5e90de82013-10-16 16:46:05 +03001310 queue_work(ar->workqueue, &ar->restart_work);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001311}
1312
1313static void ath10k_pci_hif_send_complete_check(struct ath10k *ar, u8 pipe,
1314 int force)
1315{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001316 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif send complete check\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001317
Kalle Valo5e3dd152013-06-12 20:52:10 +03001318 if (!force) {
1319 int resources;
1320 /*
1321 * Decide whether to actually poll for completions, or just
1322 * wait for a later chance.
1323 * If there seem to be plenty of resources left, then just wait
1324 * since checking involves reading a CE register, which is a
1325 * relatively expensive operation.
1326 */
1327 resources = ath10k_pci_hif_get_free_queue_number(ar, pipe);
1328
1329 /*
1330 * If at least 50% of the total resources are still available,
1331 * don't bother checking again yet.
1332 */
1333 if (resources > (host_ce_config_wlan[pipe].src_nentries >> 1))
1334 return;
1335 }
1336 ath10k_ce_per_engine_service(ar, pipe);
1337}
1338
Michal Kaziore799bbf2013-07-05 16:15:12 +03001339static void ath10k_pci_hif_set_callbacks(struct ath10k *ar,
1340 struct ath10k_hif_cb *callbacks)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001341{
1342 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1343
Michal Kazior7aa7a722014-08-25 12:09:38 +02001344 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif set callbacks\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001345
1346 memcpy(&ar_pci->msg_callbacks_current, callbacks,
1347 sizeof(ar_pci->msg_callbacks_current));
1348}
1349
Michal Kazior96a9d0d2013-11-08 08:01:25 +01001350static void ath10k_pci_kill_tasklet(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001351{
1352 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001353 int i;
1354
Kalle Valo5e3dd152013-06-12 20:52:10 +03001355 tasklet_kill(&ar_pci->intr_tq);
Michal Kazior103d4f52013-11-08 08:01:24 +01001356 tasklet_kill(&ar_pci->msi_fw_err);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001357
1358 for (i = 0; i < CE_COUNT; i++)
1359 tasklet_kill(&ar_pci->pipe_info[i].intr);
Michal Kazior728f95e2014-08-22 14:33:14 +02001360
1361 del_timer_sync(&ar_pci->rx_post_retry);
Michal Kazior96a9d0d2013-11-08 08:01:25 +01001362}
1363
Kalle Valo5e3dd152013-06-12 20:52:10 +03001364static int ath10k_pci_hif_map_service_to_pipe(struct ath10k *ar,
1365 u16 service_id, u8 *ul_pipe,
1366 u8 *dl_pipe, int *ul_is_polled,
1367 int *dl_is_polled)
1368{
Michal Kazior7c6aa252014-08-26 19:14:03 +03001369 const struct service_to_pipe *entry;
1370 bool ul_set = false, dl_set = false;
1371 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001372
Michal Kazior7aa7a722014-08-25 12:09:38 +02001373 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif map service\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001374
Kalle Valo5e3dd152013-06-12 20:52:10 +03001375 /* polling for received messages not supported */
1376 *dl_is_polled = 0;
1377
Michal Kazior7c6aa252014-08-26 19:14:03 +03001378 for (i = 0; i < ARRAY_SIZE(target_service_to_ce_map_wlan); i++) {
1379 entry = &target_service_to_ce_map_wlan[i];
Kalle Valo5e3dd152013-06-12 20:52:10 +03001380
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001381 if (__le32_to_cpu(entry->service_id) != service_id)
Michal Kazior7c6aa252014-08-26 19:14:03 +03001382 continue;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001383
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001384 switch (__le32_to_cpu(entry->pipedir)) {
Michal Kazior7c6aa252014-08-26 19:14:03 +03001385 case PIPEDIR_NONE:
1386 break;
1387 case PIPEDIR_IN:
1388 WARN_ON(dl_set);
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001389 *dl_pipe = __le32_to_cpu(entry->pipenum);
Michal Kazior7c6aa252014-08-26 19:14:03 +03001390 dl_set = true;
1391 break;
1392 case PIPEDIR_OUT:
1393 WARN_ON(ul_set);
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001394 *ul_pipe = __le32_to_cpu(entry->pipenum);
Michal Kazior7c6aa252014-08-26 19:14:03 +03001395 ul_set = true;
1396 break;
1397 case PIPEDIR_INOUT:
1398 WARN_ON(dl_set);
1399 WARN_ON(ul_set);
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001400 *dl_pipe = __le32_to_cpu(entry->pipenum);
1401 *ul_pipe = __le32_to_cpu(entry->pipenum);
Michal Kazior7c6aa252014-08-26 19:14:03 +03001402 dl_set = true;
1403 ul_set = true;
1404 break;
1405 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001406 }
Michal Kazior7c6aa252014-08-26 19:14:03 +03001407
1408 if (WARN_ON(!ul_set || !dl_set))
1409 return -ENOENT;
1410
Kalle Valo5e3dd152013-06-12 20:52:10 +03001411 *ul_is_polled =
1412 (host_ce_config_wlan[*ul_pipe].flags & CE_ATTR_DIS_INTR) != 0;
1413
Michal Kazior7c6aa252014-08-26 19:14:03 +03001414 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001415}
1416
1417static void ath10k_pci_hif_get_default_pipe(struct ath10k *ar,
Kalle Valo5b07e072014-09-14 12:50:06 +03001418 u8 *ul_pipe, u8 *dl_pipe)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001419{
1420 int ul_is_polled, dl_is_polled;
1421
Michal Kazior7aa7a722014-08-25 12:09:38 +02001422 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif get default pipe\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001423
Kalle Valo5e3dd152013-06-12 20:52:10 +03001424 (void)ath10k_pci_hif_map_service_to_pipe(ar,
1425 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1426 ul_pipe,
1427 dl_pipe,
1428 &ul_is_polled,
1429 &dl_is_polled);
1430}
1431
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001432static void ath10k_pci_irq_msi_fw_mask(struct ath10k *ar)
1433{
1434 u32 val;
1435
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +05301436 switch (ar->hw_rev) {
1437 case ATH10K_HW_QCA988X:
1438 case ATH10K_HW_QCA6174:
1439 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1440 CORE_CTRL_ADDRESS);
1441 val &= ~CORE_CTRL_PCIE_REG_31_MASK;
1442 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
1443 CORE_CTRL_ADDRESS, val);
1444 break;
1445 case ATH10K_HW_QCA99X0:
1446 /* TODO: Find appropriate register configuration for QCA99X0
1447 * to mask irq/MSI.
1448 */
1449 break;
1450 }
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001451}
1452
1453static void ath10k_pci_irq_msi_fw_unmask(struct ath10k *ar)
1454{
1455 u32 val;
1456
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +05301457 switch (ar->hw_rev) {
1458 case ATH10K_HW_QCA988X:
1459 case ATH10K_HW_QCA6174:
1460 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1461 CORE_CTRL_ADDRESS);
1462 val |= CORE_CTRL_PCIE_REG_31_MASK;
1463 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
1464 CORE_CTRL_ADDRESS, val);
1465 break;
1466 case ATH10K_HW_QCA99X0:
1467 /* TODO: Find appropriate register configuration for QCA99X0
1468 * to unmask irq/MSI.
1469 */
1470 break;
1471 }
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001472}
1473
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001474static void ath10k_pci_irq_disable(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001475{
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001476 ath10k_ce_disable_interrupts(ar);
1477 ath10k_pci_disable_and_clear_legacy_irq(ar);
1478 ath10k_pci_irq_msi_fw_mask(ar);
1479}
1480
1481static void ath10k_pci_irq_sync(struct ath10k *ar)
1482{
Kalle Valo5e3dd152013-06-12 20:52:10 +03001483 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001484 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001485
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001486 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
1487 synchronize_irq(ar_pci->pdev->irq + i);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001488}
1489
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001490static void ath10k_pci_irq_enable(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001491{
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001492 ath10k_ce_enable_interrupts(ar);
Michal Kaziore75db4e2014-08-28 22:14:16 +03001493 ath10k_pci_enable_legacy_irq(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001494 ath10k_pci_irq_msi_fw_unmask(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001495}
1496
1497static int ath10k_pci_hif_start(struct ath10k *ar)
1498{
Janusz Dziedzic76d870e2015-05-18 09:38:16 +00001499 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo9a149692015-10-05 17:56:36 +03001500
Michal Kazior7aa7a722014-08-25 12:09:38 +02001501 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif start\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001502
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001503 ath10k_pci_irq_enable(ar);
Michal Kazior728f95e2014-08-22 14:33:14 +02001504 ath10k_pci_rx_post(ar);
Kalle Valo50f87a62014-03-28 09:32:52 +02001505
Janusz Dziedzic76d870e2015-05-18 09:38:16 +00001506 pcie_capability_write_word(ar_pci->pdev, PCI_EXP_LNKCTL,
1507 ar_pci->link_ctl);
1508
Kalle Valo5e3dd152013-06-12 20:52:10 +03001509 return 0;
1510}
1511
Michal Kazior099ac7c2014-10-28 10:32:05 +01001512static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe *pci_pipe)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001513{
1514 struct ath10k *ar;
Michal Kazior099ac7c2014-10-28 10:32:05 +01001515 struct ath10k_ce_pipe *ce_pipe;
1516 struct ath10k_ce_ring *ce_ring;
1517 struct sk_buff *skb;
1518 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001519
Michal Kazior099ac7c2014-10-28 10:32:05 +01001520 ar = pci_pipe->hif_ce_state;
1521 ce_pipe = pci_pipe->ce_hdl;
1522 ce_ring = ce_pipe->dest_ring;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001523
Michal Kazior099ac7c2014-10-28 10:32:05 +01001524 if (!ce_ring)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001525 return;
1526
Michal Kazior099ac7c2014-10-28 10:32:05 +01001527 if (!pci_pipe->buf_sz)
1528 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001529
Michal Kazior099ac7c2014-10-28 10:32:05 +01001530 for (i = 0; i < ce_ring->nentries; i++) {
1531 skb = ce_ring->per_transfer_context[i];
1532 if (!skb)
1533 continue;
1534
1535 ce_ring->per_transfer_context[i] = NULL;
1536
Michal Kazior8582bf32015-01-24 12:14:47 +02001537 dma_unmap_single(ar->dev, ATH10K_SKB_RXCB(skb)->paddr,
Michal Kazior099ac7c2014-10-28 10:32:05 +01001538 skb->len + skb_tailroom(skb),
Kalle Valo5e3dd152013-06-12 20:52:10 +03001539 DMA_FROM_DEVICE);
Michal Kazior099ac7c2014-10-28 10:32:05 +01001540 dev_kfree_skb_any(skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001541 }
1542}
1543
Michal Kazior099ac7c2014-10-28 10:32:05 +01001544static void ath10k_pci_tx_pipe_cleanup(struct ath10k_pci_pipe *pci_pipe)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001545{
1546 struct ath10k *ar;
1547 struct ath10k_pci *ar_pci;
Michal Kazior099ac7c2014-10-28 10:32:05 +01001548 struct ath10k_ce_pipe *ce_pipe;
1549 struct ath10k_ce_ring *ce_ring;
1550 struct ce_desc *ce_desc;
1551 struct sk_buff *skb;
Michal Kazior099ac7c2014-10-28 10:32:05 +01001552 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001553
Michal Kazior099ac7c2014-10-28 10:32:05 +01001554 ar = pci_pipe->hif_ce_state;
1555 ar_pci = ath10k_pci_priv(ar);
1556 ce_pipe = pci_pipe->ce_hdl;
1557 ce_ring = ce_pipe->src_ring;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001558
Michal Kazior099ac7c2014-10-28 10:32:05 +01001559 if (!ce_ring)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001560 return;
1561
Michal Kazior099ac7c2014-10-28 10:32:05 +01001562 if (!pci_pipe->buf_sz)
1563 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001564
Michal Kazior099ac7c2014-10-28 10:32:05 +01001565 ce_desc = ce_ring->shadow_base;
1566 if (WARN_ON(!ce_desc))
1567 return;
1568
1569 for (i = 0; i < ce_ring->nentries; i++) {
1570 skb = ce_ring->per_transfer_context[i];
1571 if (!skb)
Michal Kazior2415fc12013-11-08 08:01:32 +01001572 continue;
Michal Kazior2415fc12013-11-08 08:01:32 +01001573
Michal Kazior099ac7c2014-10-28 10:32:05 +01001574 ce_ring->per_transfer_context[i] = NULL;
Michal Kazior099ac7c2014-10-28 10:32:05 +01001575
Michal Kaziord84a5122014-11-27 11:09:37 +01001576 ar_pci->msg_callbacks_current.tx_completion(ar, skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001577 }
1578}
1579
1580/*
1581 * Cleanup residual buffers for device shutdown:
1582 * buffers that were enqueued for receive
1583 * buffers that were to be sent
1584 * Note: Buffers that had completed but which were
1585 * not yet processed are on a completion queue. They
1586 * are handled when the completion thread shuts down.
1587 */
1588static void ath10k_pci_buffer_cleanup(struct ath10k *ar)
1589{
1590 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1591 int pipe_num;
1592
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001593 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Michal Kazior87263e52013-08-27 13:08:01 +02001594 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001595
1596 pipe_info = &ar_pci->pipe_info[pipe_num];
1597 ath10k_pci_rx_pipe_cleanup(pipe_info);
1598 ath10k_pci_tx_pipe_cleanup(pipe_info);
1599 }
1600}
1601
1602static void ath10k_pci_ce_deinit(struct ath10k *ar)
1603{
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001604 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001605
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001606 for (i = 0; i < CE_COUNT; i++)
1607 ath10k_ce_deinit_pipe(ar, i);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001608}
1609
Michal Kazior728f95e2014-08-22 14:33:14 +02001610static void ath10k_pci_flush(struct ath10k *ar)
1611{
1612 ath10k_pci_kill_tasklet(ar);
1613 ath10k_pci_buffer_cleanup(ar);
1614}
1615
Kalle Valo5e3dd152013-06-12 20:52:10 +03001616static void ath10k_pci_hif_stop(struct ath10k *ar)
1617{
Michal Kazior77258d42015-05-18 09:38:18 +00001618 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1619 unsigned long flags;
1620
Michal Kazior7aa7a722014-08-25 12:09:38 +02001621 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif stop\n");
Michal Kazior32270b62013-08-02 09:15:47 +02001622
Michal Kazior10d23db2014-08-22 14:33:15 +02001623 /* Most likely the device has HTT Rx ring configured. The only way to
1624 * prevent the device from accessing (and possible corrupting) host
1625 * memory is to reset the chip now.
Michal Kaziore75db4e2014-08-28 22:14:16 +03001626 *
1627 * There's also no known way of masking MSI interrupts on the device.
1628 * For ranged MSI the CE-related interrupts can be masked. However
1629 * regardless how many MSI interrupts are assigned the first one
1630 * is always used for firmware indications (crashes) and cannot be
1631 * masked. To prevent the device from asserting the interrupt reset it
1632 * before proceeding with cleanup.
Michal Kazior10d23db2014-08-22 14:33:15 +02001633 */
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +05301634 ath10k_pci_safe_chip_reset(ar);
Michal Kaziore75db4e2014-08-28 22:14:16 +03001635
1636 ath10k_pci_irq_disable(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001637 ath10k_pci_irq_sync(ar);
Michal Kaziore75db4e2014-08-28 22:14:16 +03001638 ath10k_pci_flush(ar);
Michal Kazior77258d42015-05-18 09:38:18 +00001639
1640 spin_lock_irqsave(&ar_pci->ps_lock, flags);
1641 WARN_ON(ar_pci->ps_wake_refcount > 0);
1642 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001643}
1644
1645static int ath10k_pci_hif_exchange_bmi_msg(struct ath10k *ar,
1646 void *req, u32 req_len,
1647 void *resp, u32 *resp_len)
1648{
1649 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2aa39112013-08-27 13:08:02 +02001650 struct ath10k_pci_pipe *pci_tx = &ar_pci->pipe_info[BMI_CE_NUM_TO_TARG];
1651 struct ath10k_pci_pipe *pci_rx = &ar_pci->pipe_info[BMI_CE_NUM_TO_HOST];
1652 struct ath10k_ce_pipe *ce_tx = pci_tx->ce_hdl;
1653 struct ath10k_ce_pipe *ce_rx = pci_rx->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001654 dma_addr_t req_paddr = 0;
1655 dma_addr_t resp_paddr = 0;
1656 struct bmi_xfer xfer = {};
1657 void *treq, *tresp = NULL;
1658 int ret = 0;
1659
Michal Kazior85622cd2013-11-25 14:06:22 +01001660 might_sleep();
1661
Kalle Valo5e3dd152013-06-12 20:52:10 +03001662 if (resp && !resp_len)
1663 return -EINVAL;
1664
1665 if (resp && resp_len && *resp_len == 0)
1666 return -EINVAL;
1667
1668 treq = kmemdup(req, req_len, GFP_KERNEL);
1669 if (!treq)
1670 return -ENOMEM;
1671
1672 req_paddr = dma_map_single(ar->dev, treq, req_len, DMA_TO_DEVICE);
1673 ret = dma_mapping_error(ar->dev, req_paddr);
Michal Kazior5e55e3c2015-08-19 13:10:43 +02001674 if (ret) {
1675 ret = -EIO;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001676 goto err_dma;
Michal Kazior5e55e3c2015-08-19 13:10:43 +02001677 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001678
1679 if (resp && resp_len) {
1680 tresp = kzalloc(*resp_len, GFP_KERNEL);
1681 if (!tresp) {
1682 ret = -ENOMEM;
1683 goto err_req;
1684 }
1685
1686 resp_paddr = dma_map_single(ar->dev, tresp, *resp_len,
1687 DMA_FROM_DEVICE);
1688 ret = dma_mapping_error(ar->dev, resp_paddr);
Michal Kazior5e55e3c2015-08-19 13:10:43 +02001689 if (ret) {
1690 ret = EIO;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001691 goto err_req;
Michal Kazior5e55e3c2015-08-19 13:10:43 +02001692 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001693
1694 xfer.wait_for_resp = true;
1695 xfer.resp_len = 0;
1696
Michal Kazior728f95e2014-08-22 14:33:14 +02001697 ath10k_ce_rx_post_buf(ce_rx, &xfer, resp_paddr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001698 }
1699
Kalle Valo5e3dd152013-06-12 20:52:10 +03001700 ret = ath10k_ce_send(ce_tx, &xfer, req_paddr, req_len, -1, 0);
1701 if (ret)
1702 goto err_resp;
1703
Michal Kazior85622cd2013-11-25 14:06:22 +01001704 ret = ath10k_pci_bmi_wait(ce_tx, ce_rx, &xfer);
1705 if (ret) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001706 u32 unused_buffer;
1707 unsigned int unused_nbytes;
1708 unsigned int unused_id;
1709
Kalle Valo5e3dd152013-06-12 20:52:10 +03001710 ath10k_ce_cancel_send_next(ce_tx, NULL, &unused_buffer,
1711 &unused_nbytes, &unused_id);
1712 } else {
1713 /* non-zero means we did not time out */
1714 ret = 0;
1715 }
1716
1717err_resp:
1718 if (resp) {
1719 u32 unused_buffer;
1720
1721 ath10k_ce_revoke_recv_next(ce_rx, NULL, &unused_buffer);
1722 dma_unmap_single(ar->dev, resp_paddr,
1723 *resp_len, DMA_FROM_DEVICE);
1724 }
1725err_req:
1726 dma_unmap_single(ar->dev, req_paddr, req_len, DMA_TO_DEVICE);
1727
1728 if (ret == 0 && resp_len) {
1729 *resp_len = min(*resp_len, xfer.resp_len);
1730 memcpy(resp, tresp, xfer.resp_len);
1731 }
1732err_dma:
1733 kfree(treq);
1734 kfree(tresp);
1735
1736 return ret;
1737}
1738
Michal Kazior5440ce22013-09-03 15:09:58 +02001739static void ath10k_pci_bmi_send_done(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001740{
Michal Kazior5440ce22013-09-03 15:09:58 +02001741 struct bmi_xfer *xfer;
1742 u32 ce_data;
1743 unsigned int nbytes;
1744 unsigned int transfer_id;
1745
1746 if (ath10k_ce_completed_send_next(ce_state, (void **)&xfer, &ce_data,
1747 &nbytes, &transfer_id))
1748 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001749
Michal Kazior2374b182014-07-14 16:25:25 +03001750 xfer->tx_done = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001751}
1752
Michal Kazior5440ce22013-09-03 15:09:58 +02001753static void ath10k_pci_bmi_recv_data(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001754{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001755 struct ath10k *ar = ce_state->ar;
Michal Kazior5440ce22013-09-03 15:09:58 +02001756 struct bmi_xfer *xfer;
1757 u32 ce_data;
1758 unsigned int nbytes;
1759 unsigned int transfer_id;
1760 unsigned int flags;
1761
1762 if (ath10k_ce_completed_recv_next(ce_state, (void **)&xfer, &ce_data,
1763 &nbytes, &transfer_id, &flags))
1764 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001765
Michal Kazior04ed9df2014-10-28 10:34:36 +01001766 if (WARN_ON_ONCE(!xfer))
1767 return;
1768
Kalle Valo5e3dd152013-06-12 20:52:10 +03001769 if (!xfer->wait_for_resp) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001770 ath10k_warn(ar, "unexpected: BMI data received; ignoring\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001771 return;
1772 }
1773
1774 xfer->resp_len = nbytes;
Michal Kazior2374b182014-07-14 16:25:25 +03001775 xfer->rx_done = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001776}
1777
Michal Kazior85622cd2013-11-25 14:06:22 +01001778static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
1779 struct ath10k_ce_pipe *rx_pipe,
1780 struct bmi_xfer *xfer)
1781{
1782 unsigned long timeout = jiffies + BMI_COMMUNICATION_TIMEOUT_HZ;
1783
1784 while (time_before_eq(jiffies, timeout)) {
1785 ath10k_pci_bmi_send_done(tx_pipe);
1786 ath10k_pci_bmi_recv_data(rx_pipe);
1787
Michal Kazior2374b182014-07-14 16:25:25 +03001788 if (xfer->tx_done && (xfer->rx_done == xfer->wait_for_resp))
Michal Kazior85622cd2013-11-25 14:06:22 +01001789 return 0;
1790
1791 schedule();
1792 }
1793
1794 return -ETIMEDOUT;
1795}
1796
Kalle Valo5e3dd152013-06-12 20:52:10 +03001797/*
Kalle Valo5e3dd152013-06-12 20:52:10 +03001798 * Send an interrupt to the device to wake up the Target CPU
1799 * so it has an opportunity to notice any changed state.
1800 */
1801static int ath10k_pci_wake_target_cpu(struct ath10k *ar)
1802{
Michal Kazior9e264942014-09-02 11:00:21 +03001803 u32 addr, val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001804
Michal Kazior9e264942014-09-02 11:00:21 +03001805 addr = SOC_CORE_BASE_ADDRESS | CORE_CTRL_ADDRESS;
1806 val = ath10k_pci_read32(ar, addr);
1807 val |= CORE_CTRL_CPU_INTR_MASK;
1808 ath10k_pci_write32(ar, addr, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001809
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001810 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001811}
1812
Michal Kaziord63955b2015-01-24 12:14:49 +02001813static int ath10k_pci_get_num_banks(struct ath10k *ar)
1814{
1815 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1816
1817 switch (ar_pci->pdev->device) {
1818 case QCA988X_2_0_DEVICE_ID:
Vasanthakumar Thiagarajan8bd47022015-06-18 12:31:03 +05301819 case QCA99X0_2_0_DEVICE_ID:
Michal Kaziord63955b2015-01-24 12:14:49 +02001820 return 1;
Michal Kazior36582e52015-08-13 14:32:26 +02001821 case QCA6164_2_1_DEVICE_ID:
Michal Kaziord63955b2015-01-24 12:14:49 +02001822 case QCA6174_2_1_DEVICE_ID:
1823 switch (MS(ar->chip_id, SOC_CHIP_ID_REV)) {
1824 case QCA6174_HW_1_0_CHIP_ID_REV:
1825 case QCA6174_HW_1_1_CHIP_ID_REV:
Michal Kazior11a002e2015-04-20 09:20:41 +00001826 case QCA6174_HW_2_1_CHIP_ID_REV:
1827 case QCA6174_HW_2_2_CHIP_ID_REV:
Michal Kaziord63955b2015-01-24 12:14:49 +02001828 return 3;
1829 case QCA6174_HW_1_3_CHIP_ID_REV:
1830 return 2;
Michal Kaziord63955b2015-01-24 12:14:49 +02001831 case QCA6174_HW_3_0_CHIP_ID_REV:
1832 case QCA6174_HW_3_1_CHIP_ID_REV:
1833 case QCA6174_HW_3_2_CHIP_ID_REV:
1834 return 9;
1835 }
1836 break;
1837 }
1838
1839 ath10k_warn(ar, "unknown number of banks, assuming 1\n");
1840 return 1;
1841}
1842
Kalle Valo5e3dd152013-06-12 20:52:10 +03001843static int ath10k_pci_init_config(struct ath10k *ar)
1844{
1845 u32 interconnect_targ_addr;
1846 u32 pcie_state_targ_addr = 0;
1847 u32 pipe_cfg_targ_addr = 0;
1848 u32 svc_to_pipe_map = 0;
1849 u32 pcie_config_flags = 0;
1850 u32 ealloc_value;
1851 u32 ealloc_targ_addr;
1852 u32 flag2_value;
1853 u32 flag2_targ_addr;
1854 int ret = 0;
1855
1856 /* Download to Target the CE Config and the service-to-CE map */
1857 interconnect_targ_addr =
1858 host_interest_item_address(HI_ITEM(hi_interconnect_state));
1859
1860 /* Supply Target-side CE configuration */
Michal Kazior9e264942014-09-02 11:00:21 +03001861 ret = ath10k_pci_diag_read32(ar, interconnect_targ_addr,
1862 &pcie_state_targ_addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001863 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001864 ath10k_err(ar, "Failed to get pcie state addr: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001865 return ret;
1866 }
1867
1868 if (pcie_state_targ_addr == 0) {
1869 ret = -EIO;
Michal Kazior7aa7a722014-08-25 12:09:38 +02001870 ath10k_err(ar, "Invalid pcie state addr\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001871 return ret;
1872 }
1873
Michal Kazior9e264942014-09-02 11:00:21 +03001874 ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
Kalle Valo5e3dd152013-06-12 20:52:10 +03001875 offsetof(struct pcie_state,
Michal Kazior9e264942014-09-02 11:00:21 +03001876 pipe_cfg_addr)),
1877 &pipe_cfg_targ_addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001878 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001879 ath10k_err(ar, "Failed to get pipe cfg addr: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001880 return ret;
1881 }
1882
1883 if (pipe_cfg_targ_addr == 0) {
1884 ret = -EIO;
Michal Kazior7aa7a722014-08-25 12:09:38 +02001885 ath10k_err(ar, "Invalid pipe cfg addr\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001886 return ret;
1887 }
1888
1889 ret = ath10k_pci_diag_write_mem(ar, pipe_cfg_targ_addr,
Kalle Valo5b07e072014-09-14 12:50:06 +03001890 target_ce_config_wlan,
Vasanthakumar Thiagarajan050af062015-06-18 12:31:04 +05301891 sizeof(struct ce_pipe_config) *
1892 NUM_TARGET_CE_CONFIG_WLAN);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001893
1894 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001895 ath10k_err(ar, "Failed to write pipe cfg: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001896 return ret;
1897 }
1898
Michal Kazior9e264942014-09-02 11:00:21 +03001899 ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
Kalle Valo5e3dd152013-06-12 20:52:10 +03001900 offsetof(struct pcie_state,
Michal Kazior9e264942014-09-02 11:00:21 +03001901 svc_to_pipe_map)),
1902 &svc_to_pipe_map);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001903 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001904 ath10k_err(ar, "Failed to get svc/pipe map: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001905 return ret;
1906 }
1907
1908 if (svc_to_pipe_map == 0) {
1909 ret = -EIO;
Michal Kazior7aa7a722014-08-25 12:09:38 +02001910 ath10k_err(ar, "Invalid svc_to_pipe map\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001911 return ret;
1912 }
1913
1914 ret = ath10k_pci_diag_write_mem(ar, svc_to_pipe_map,
Kalle Valo5b07e072014-09-14 12:50:06 +03001915 target_service_to_ce_map_wlan,
1916 sizeof(target_service_to_ce_map_wlan));
Kalle Valo5e3dd152013-06-12 20:52:10 +03001917 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001918 ath10k_err(ar, "Failed to write svc/pipe map: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001919 return ret;
1920 }
1921
Michal Kazior9e264942014-09-02 11:00:21 +03001922 ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
Kalle Valo5e3dd152013-06-12 20:52:10 +03001923 offsetof(struct pcie_state,
Michal Kazior9e264942014-09-02 11:00:21 +03001924 config_flags)),
1925 &pcie_config_flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001926 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001927 ath10k_err(ar, "Failed to get pcie config_flags: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001928 return ret;
1929 }
1930
1931 pcie_config_flags &= ~PCIE_CONFIG_FLAG_ENABLE_L1;
1932
Michal Kazior9e264942014-09-02 11:00:21 +03001933 ret = ath10k_pci_diag_write32(ar, (pcie_state_targ_addr +
1934 offsetof(struct pcie_state,
1935 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 write pcie config_flags: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001939 return ret;
1940 }
1941
1942 /* configure early allocation */
1943 ealloc_targ_addr = host_interest_item_address(HI_ITEM(hi_early_alloc));
1944
Michal Kazior9e264942014-09-02 11:00:21 +03001945 ret = ath10k_pci_diag_read32(ar, ealloc_targ_addr, &ealloc_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001946 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001947 ath10k_err(ar, "Faile to get early alloc val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001948 return ret;
1949 }
1950
1951 /* first bank is switched to IRAM */
1952 ealloc_value |= ((HI_EARLY_ALLOC_MAGIC << HI_EARLY_ALLOC_MAGIC_SHIFT) &
1953 HI_EARLY_ALLOC_MAGIC_MASK);
Michal Kaziord63955b2015-01-24 12:14:49 +02001954 ealloc_value |= ((ath10k_pci_get_num_banks(ar) <<
1955 HI_EARLY_ALLOC_IRAM_BANKS_SHIFT) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03001956 HI_EARLY_ALLOC_IRAM_BANKS_MASK);
1957
Michal Kazior9e264942014-09-02 11:00:21 +03001958 ret = ath10k_pci_diag_write32(ar, ealloc_targ_addr, ealloc_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001959 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001960 ath10k_err(ar, "Failed to set early alloc val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001961 return ret;
1962 }
1963
1964 /* Tell Target to proceed with initialization */
1965 flag2_targ_addr = host_interest_item_address(HI_ITEM(hi_option_flag2));
1966
Michal Kazior9e264942014-09-02 11:00:21 +03001967 ret = ath10k_pci_diag_read32(ar, flag2_targ_addr, &flag2_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001968 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001969 ath10k_err(ar, "Failed to get option val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001970 return ret;
1971 }
1972
1973 flag2_value |= HI_OPTION_EARLY_CFG_DONE;
1974
Michal Kazior9e264942014-09-02 11:00:21 +03001975 ret = ath10k_pci_diag_write32(ar, flag2_targ_addr, flag2_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001976 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001977 ath10k_err(ar, "Failed to set option val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001978 return ret;
1979 }
1980
1981 return 0;
1982}
1983
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001984static int ath10k_pci_alloc_pipes(struct ath10k *ar)
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001985{
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001986 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1987 struct ath10k_pci_pipe *pipe;
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001988 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001989
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001990 for (i = 0; i < CE_COUNT; i++) {
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001991 pipe = &ar_pci->pipe_info[i];
1992 pipe->ce_hdl = &ar_pci->ce_states[i];
1993 pipe->pipe_num = i;
1994 pipe->hif_ce_state = ar;
1995
1996 ret = ath10k_ce_alloc_pipe(ar, i, &host_ce_config_wlan[i],
1997 ath10k_pci_ce_send_done,
1998 ath10k_pci_ce_recv_data);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001999 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002000 ath10k_err(ar, "failed to allocate copy engine pipe %d: %d\n",
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002001 i, ret);
2002 return ret;
2003 }
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002004
2005 /* Last CE is Diagnostic Window */
Vasanthakumar Thiagarajan050af062015-06-18 12:31:04 +05302006 if (i == CE_DIAG_PIPE) {
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002007 ar_pci->ce_diag = pipe->ce_hdl;
2008 continue;
2009 }
2010
2011 pipe->buf_sz = (size_t)(host_ce_config_wlan[i].src_sz_max);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002012 }
2013
2014 return 0;
2015}
2016
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002017static void ath10k_pci_free_pipes(struct ath10k *ar)
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002018{
2019 int i;
2020
2021 for (i = 0; i < CE_COUNT; i++)
2022 ath10k_ce_free_pipe(ar, i);
2023}
Kalle Valo5e3dd152013-06-12 20:52:10 +03002024
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002025static int ath10k_pci_init_pipes(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002026{
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002027 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002028
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002029 for (i = 0; i < CE_COUNT; i++) {
2030 ret = ath10k_ce_init_pipe(ar, i, &host_ce_config_wlan[i]);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002031 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002032 ath10k_err(ar, "failed to initialize copy engine pipe %d: %d\n",
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002033 i, ret);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002034 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002035 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002036 }
2037
Kalle Valo5e3dd152013-06-12 20:52:10 +03002038 return 0;
2039}
2040
Michal Kazior5c771e72014-08-22 14:23:34 +02002041static bool ath10k_pci_has_fw_crashed(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002042{
Michal Kazior5c771e72014-08-22 14:23:34 +02002043 return ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS) &
2044 FW_IND_EVENT_PENDING;
2045}
Kalle Valo5e3dd152013-06-12 20:52:10 +03002046
Michal Kazior5c771e72014-08-22 14:23:34 +02002047static void ath10k_pci_fw_crashed_clear(struct ath10k *ar)
2048{
2049 u32 val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002050
Michal Kazior5c771e72014-08-22 14:23:34 +02002051 val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
2052 val &= ~FW_IND_EVENT_PENDING;
2053 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002054}
2055
Michal Kaziorde013572014-05-14 16:56:16 +03002056/* this function effectively clears target memory controller assert line */
2057static void ath10k_pci_warm_reset_si0(struct ath10k *ar)
2058{
2059 u32 val;
2060
2061 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
2062 ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
2063 val | SOC_RESET_CONTROL_SI0_RST_MASK);
2064 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
2065
2066 msleep(10);
2067
2068 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
2069 ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
2070 val & ~SOC_RESET_CONTROL_SI0_RST_MASK);
2071 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
2072
2073 msleep(10);
2074}
2075
Michal Kazior61c16482014-10-28 10:32:06 +01002076static void ath10k_pci_warm_reset_cpu(struct ath10k *ar)
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002077{
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002078 u32 val;
2079
Kalle Valob39712c2014-03-28 09:32:46 +02002080 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, 0);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002081
Michal Kazior61c16482014-10-28 10:32:06 +01002082 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
2083 SOC_RESET_CONTROL_ADDRESS);
2084 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
2085 val | SOC_RESET_CONTROL_CPU_WARM_RST_MASK);
2086}
2087
2088static void ath10k_pci_warm_reset_ce(struct ath10k *ar)
2089{
2090 u32 val;
2091
2092 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
2093 SOC_RESET_CONTROL_ADDRESS);
2094
2095 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
2096 val | SOC_RESET_CONTROL_CE_RST_MASK);
2097 msleep(10);
2098 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
2099 val & ~SOC_RESET_CONTROL_CE_RST_MASK);
2100}
2101
2102static void ath10k_pci_warm_reset_clear_lf(struct ath10k *ar)
2103{
2104 u32 val;
2105
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002106 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
2107 SOC_LF_TIMER_CONTROL0_ADDRESS);
2108 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS +
2109 SOC_LF_TIMER_CONTROL0_ADDRESS,
2110 val & ~SOC_LF_TIMER_CONTROL0_ENABLE_MASK);
Michal Kazior61c16482014-10-28 10:32:06 +01002111}
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002112
Michal Kazior61c16482014-10-28 10:32:06 +01002113static int ath10k_pci_warm_reset(struct ath10k *ar)
2114{
2115 int ret;
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002116
Michal Kazior61c16482014-10-28 10:32:06 +01002117 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot warm reset\n");
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002118
Michal Kazior61c16482014-10-28 10:32:06 +01002119 spin_lock_bh(&ar->data_lock);
2120 ar->stats.fw_warm_reset_counter++;
2121 spin_unlock_bh(&ar->data_lock);
2122
2123 ath10k_pci_irq_disable(ar);
2124
2125 /* Make sure the target CPU is not doing anything dangerous, e.g. if it
2126 * were to access copy engine while host performs copy engine reset
2127 * then it is possible for the device to confuse pci-e controller to
2128 * the point of bringing host system to a complete stop (i.e. hang).
2129 */
Michal Kaziorde013572014-05-14 16:56:16 +03002130 ath10k_pci_warm_reset_si0(ar);
Michal Kazior61c16482014-10-28 10:32:06 +01002131 ath10k_pci_warm_reset_cpu(ar);
2132 ath10k_pci_init_pipes(ar);
2133 ath10k_pci_wait_for_target_init(ar);
Michal Kaziorde013572014-05-14 16:56:16 +03002134
Michal Kazior61c16482014-10-28 10:32:06 +01002135 ath10k_pci_warm_reset_clear_lf(ar);
2136 ath10k_pci_warm_reset_ce(ar);
2137 ath10k_pci_warm_reset_cpu(ar);
2138 ath10k_pci_init_pipes(ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002139
Michal Kazior61c16482014-10-28 10:32:06 +01002140 ret = ath10k_pci_wait_for_target_init(ar);
2141 if (ret) {
2142 ath10k_warn(ar, "failed to wait for target init: %d\n", ret);
2143 return ret;
2144 }
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002145
Michal Kazior7aa7a722014-08-25 12:09:38 +02002146 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot warm reset complete\n");
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002147
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002148 return 0;
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002149}
2150
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +05302151static int ath10k_pci_safe_chip_reset(struct ath10k *ar)
2152{
2153 if (QCA_REV_988X(ar) || QCA_REV_6174(ar)) {
2154 return ath10k_pci_warm_reset(ar);
2155 } else if (QCA_REV_99X0(ar)) {
2156 ath10k_pci_irq_disable(ar);
2157 return ath10k_pci_qca99x0_chip_reset(ar);
2158 } else {
2159 return -ENOTSUPP;
2160 }
2161}
2162
Michal Kaziord63955b2015-01-24 12:14:49 +02002163static int ath10k_pci_qca988x_chip_reset(struct ath10k *ar)
Michal Kazior0bc14d02014-10-28 10:32:07 +01002164{
2165 int i, ret;
2166 u32 val;
2167
Michal Kaziord63955b2015-01-24 12:14:49 +02002168 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot 988x chip reset\n");
Michal Kazior0bc14d02014-10-28 10:32:07 +01002169
2170 /* Some hardware revisions (e.g. CUS223v2) has issues with cold reset.
2171 * It is thus preferred to use warm reset which is safer but may not be
2172 * able to recover the device from all possible fail scenarios.
2173 *
2174 * Warm reset doesn't always work on first try so attempt it a few
2175 * times before giving up.
2176 */
2177 for (i = 0; i < ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS; i++) {
2178 ret = ath10k_pci_warm_reset(ar);
2179 if (ret) {
2180 ath10k_warn(ar, "failed to warm reset attempt %d of %d: %d\n",
2181 i + 1, ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS,
2182 ret);
2183 continue;
2184 }
2185
2186 /* FIXME: Sometimes copy engine doesn't recover after warm
2187 * reset. In most cases this needs cold reset. In some of these
2188 * cases the device is in such a state that a cold reset may
2189 * lock up the host.
2190 *
2191 * Reading any host interest register via copy engine is
2192 * sufficient to verify if device is capable of booting
2193 * firmware blob.
2194 */
2195 ret = ath10k_pci_init_pipes(ar);
2196 if (ret) {
2197 ath10k_warn(ar, "failed to init copy engine: %d\n",
2198 ret);
2199 continue;
2200 }
2201
2202 ret = ath10k_pci_diag_read32(ar, QCA988X_HOST_INTEREST_ADDRESS,
2203 &val);
2204 if (ret) {
2205 ath10k_warn(ar, "failed to poke copy engine: %d\n",
2206 ret);
2207 continue;
2208 }
2209
2210 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot chip reset complete (warm)\n");
2211 return 0;
2212 }
2213
2214 if (ath10k_pci_reset_mode == ATH10K_PCI_RESET_WARM_ONLY) {
2215 ath10k_warn(ar, "refusing cold reset as requested\n");
2216 return -EPERM;
2217 }
2218
2219 ret = ath10k_pci_cold_reset(ar);
2220 if (ret) {
2221 ath10k_warn(ar, "failed to cold reset: %d\n", ret);
2222 return ret;
2223 }
2224
2225 ret = ath10k_pci_wait_for_target_init(ar);
2226 if (ret) {
2227 ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
2228 ret);
2229 return ret;
2230 }
2231
Michal Kaziord63955b2015-01-24 12:14:49 +02002232 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca988x chip reset complete (cold)\n");
Michal Kazior0bc14d02014-10-28 10:32:07 +01002233
2234 return 0;
2235}
2236
Michal Kaziord63955b2015-01-24 12:14:49 +02002237static int ath10k_pci_qca6174_chip_reset(struct ath10k *ar)
2238{
2239 int ret;
2240
2241 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca6174 chip reset\n");
2242
2243 /* FIXME: QCA6174 requires cold + warm reset to work. */
2244
2245 ret = ath10k_pci_cold_reset(ar);
2246 if (ret) {
2247 ath10k_warn(ar, "failed to cold reset: %d\n", ret);
2248 return ret;
2249 }
2250
2251 ret = ath10k_pci_wait_for_target_init(ar);
2252 if (ret) {
2253 ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
Kalle Valo617b0f42015-10-05 17:56:35 +03002254 ret);
Michal Kaziord63955b2015-01-24 12:14:49 +02002255 return ret;
2256 }
2257
2258 ret = ath10k_pci_warm_reset(ar);
2259 if (ret) {
2260 ath10k_warn(ar, "failed to warm reset: %d\n", ret);
2261 return ret;
2262 }
2263
2264 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca6174 chip reset complete (cold)\n");
2265
2266 return 0;
2267}
2268
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +05302269static int ath10k_pci_qca99x0_chip_reset(struct ath10k *ar)
2270{
2271 int ret;
2272
2273 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca99x0 chip reset\n");
2274
2275 ret = ath10k_pci_cold_reset(ar);
2276 if (ret) {
2277 ath10k_warn(ar, "failed to cold reset: %d\n", ret);
2278 return ret;
2279 }
2280
2281 ret = ath10k_pci_wait_for_target_init(ar);
2282 if (ret) {
2283 ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
2284 ret);
2285 return ret;
2286 }
2287
2288 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca99x0 chip reset complete (cold)\n");
2289
2290 return 0;
2291}
2292
Michal Kaziord63955b2015-01-24 12:14:49 +02002293static int ath10k_pci_chip_reset(struct ath10k *ar)
2294{
2295 if (QCA_REV_988X(ar))
2296 return ath10k_pci_qca988x_chip_reset(ar);
2297 else if (QCA_REV_6174(ar))
2298 return ath10k_pci_qca6174_chip_reset(ar);
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +05302299 else if (QCA_REV_99X0(ar))
2300 return ath10k_pci_qca99x0_chip_reset(ar);
Michal Kaziord63955b2015-01-24 12:14:49 +02002301 else
2302 return -ENOTSUPP;
2303}
2304
Michal Kazior0bc14d02014-10-28 10:32:07 +01002305static int ath10k_pci_hif_power_up(struct ath10k *ar)
Michal Kazior8c5c5362013-07-16 09:38:50 +02002306{
Janusz Dziedzic76d870e2015-05-18 09:38:16 +00002307 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02002308 int ret;
2309
Michal Kazior0bc14d02014-10-28 10:32:07 +01002310 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif power up\n");
2311
Janusz Dziedzic76d870e2015-05-18 09:38:16 +00002312 pcie_capability_read_word(ar_pci->pdev, PCI_EXP_LNKCTL,
2313 &ar_pci->link_ctl);
2314 pcie_capability_write_word(ar_pci->pdev, PCI_EXP_LNKCTL,
2315 ar_pci->link_ctl & ~PCI_EXP_LNKCTL_ASPMC);
2316
Michal Kazior8c5c5362013-07-16 09:38:50 +02002317 /*
2318 * Bring the target up cleanly.
2319 *
2320 * The target may be in an undefined state with an AUX-powered Target
2321 * and a Host in WoW mode. If the Host crashes, loses power, or is
2322 * restarted (without unloading the driver) then the Target is left
2323 * (aux) powered and running. On a subsequent driver load, the Target
2324 * is in an unexpected state. We try to catch that here in order to
2325 * reset the Target and retry the probe.
2326 */
Michal Kazior0bc14d02014-10-28 10:32:07 +01002327 ret = ath10k_pci_chip_reset(ar);
Michal Kazior5b2589f2013-11-08 08:01:30 +01002328 if (ret) {
Michal Kaziora2fa8802015-01-12 15:29:37 +01002329 if (ath10k_pci_has_fw_crashed(ar)) {
2330 ath10k_warn(ar, "firmware crashed during chip reset\n");
2331 ath10k_pci_fw_crashed_clear(ar);
2332 ath10k_pci_fw_crashed_dump(ar);
2333 }
2334
Michal Kazior0bc14d02014-10-28 10:32:07 +01002335 ath10k_err(ar, "failed to reset chip: %d\n", ret);
Bartosz Markowski707b1bbd2014-10-31 09:03:43 +01002336 goto err_sleep;
Michal Kazior5b2589f2013-11-08 08:01:30 +01002337 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02002338
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002339 ret = ath10k_pci_init_pipes(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02002340 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002341 ath10k_err(ar, "failed to initialize CE: %d\n", ret);
Bartosz Markowski707b1bbd2014-10-31 09:03:43 +01002342 goto err_sleep;
Michal Kaziorab977bd2013-11-25 14:06:26 +01002343 }
2344
Michal Kazior98563d52013-11-08 08:01:33 +01002345 ret = ath10k_pci_init_config(ar);
2346 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002347 ath10k_err(ar, "failed to setup init config: %d\n", ret);
Michal Kazior5c771e72014-08-22 14:23:34 +02002348 goto err_ce;
Michal Kazior98563d52013-11-08 08:01:33 +01002349 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02002350
2351 ret = ath10k_pci_wake_target_cpu(ar);
2352 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002353 ath10k_err(ar, "could not wake up target CPU: %d\n", ret);
Michal Kazior5c771e72014-08-22 14:23:34 +02002354 goto err_ce;
Michal Kazior8c5c5362013-07-16 09:38:50 +02002355 }
2356
2357 return 0;
2358
2359err_ce:
2360 ath10k_pci_ce_deinit(ar);
Michal Kazior0bc14d02014-10-28 10:32:07 +01002361
Bartosz Markowski707b1bbd2014-10-31 09:03:43 +01002362err_sleep:
Michal Kazior8c5c5362013-07-16 09:38:50 +02002363 return ret;
2364}
2365
2366static void ath10k_pci_hif_power_down(struct ath10k *ar)
2367{
Michal Kazior7aa7a722014-08-25 12:09:38 +02002368 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif power down\n");
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002369
Michal Kaziorc011b282014-10-28 10:32:08 +01002370 /* Currently hif_power_up performs effectively a reset and hif_stop
2371 * resets the chip as well so there's no point in resetting here.
2372 */
Michal Kazior8c5c5362013-07-16 09:38:50 +02002373}
2374
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002375#ifdef CONFIG_PM
2376
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002377static int ath10k_pci_hif_suspend(struct ath10k *ar)
2378{
Michal Kazior77258d42015-05-18 09:38:18 +00002379 /* The grace timer can still be counting down and ar->ps_awake be true.
2380 * It is known that the device may be asleep after resuming regardless
2381 * of the SoC powersave state before suspending. Hence make sure the
2382 * device is asleep before proceeding.
2383 */
2384 ath10k_pci_sleep_sync(ar);
Michal Kazior320e14b2015-03-02 13:22:13 +01002385
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002386 return 0;
2387}
2388
2389static int ath10k_pci_hif_resume(struct ath10k *ar)
2390{
2391 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2392 struct pci_dev *pdev = ar_pci->pdev;
2393 u32 val;
2394
Michal Kazior9ff4be92015-03-02 13:22:14 +01002395 /* Suspend/Resume resets the PCI configuration space, so we have to
2396 * re-disable the RETRY_TIMEOUT register (0x41) to keep PCI Tx retries
2397 * from interfering with C3 CPU state. pci_restore_state won't help
2398 * here since it only restores the first 64 bytes pci config header.
2399 */
2400 pci_read_config_dword(pdev, 0x40, &val);
2401 if ((val & 0x0000ff00) != 0)
2402 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002403
Michal Kazior77258d42015-05-18 09:38:18 +00002404 return 0;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002405}
2406#endif
2407
Kalle Valo5e3dd152013-06-12 20:52:10 +03002408static const struct ath10k_hif_ops ath10k_pci_hif_ops = {
Michal Kazior726346f2014-02-27 18:50:04 +02002409 .tx_sg = ath10k_pci_hif_tx_sg,
Kalle Valoeef25402014-09-24 14:16:52 +03002410 .diag_read = ath10k_pci_hif_diag_read,
Yanbo Li9f65ad22014-11-25 12:24:48 +02002411 .diag_write = ath10k_pci_diag_write_mem,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002412 .exchange_bmi_msg = ath10k_pci_hif_exchange_bmi_msg,
2413 .start = ath10k_pci_hif_start,
2414 .stop = ath10k_pci_hif_stop,
2415 .map_service_to_pipe = ath10k_pci_hif_map_service_to_pipe,
2416 .get_default_pipe = ath10k_pci_hif_get_default_pipe,
2417 .send_complete_check = ath10k_pci_hif_send_complete_check,
Michal Kaziore799bbf2013-07-05 16:15:12 +03002418 .set_callbacks = ath10k_pci_hif_set_callbacks,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002419 .get_free_queue_number = ath10k_pci_hif_get_free_queue_number,
Michal Kazior8c5c5362013-07-16 09:38:50 +02002420 .power_up = ath10k_pci_hif_power_up,
2421 .power_down = ath10k_pci_hif_power_down,
Yanbo Li077a3802014-11-25 12:24:33 +02002422 .read32 = ath10k_pci_read32,
2423 .write32 = ath10k_pci_write32,
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002424#ifdef CONFIG_PM
2425 .suspend = ath10k_pci_hif_suspend,
2426 .resume = ath10k_pci_hif_resume,
2427#endif
Kalle Valo5e3dd152013-06-12 20:52:10 +03002428};
2429
2430static void ath10k_pci_ce_tasklet(unsigned long ptr)
2431{
Michal Kazior87263e52013-08-27 13:08:01 +02002432 struct ath10k_pci_pipe *pipe = (struct ath10k_pci_pipe *)ptr;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002433 struct ath10k_pci *ar_pci = pipe->ar_pci;
2434
2435 ath10k_ce_per_engine_service(ar_pci->ar, pipe->pipe_num);
2436}
2437
2438static void ath10k_msi_err_tasklet(unsigned long data)
2439{
2440 struct ath10k *ar = (struct ath10k *)data;
2441
Michal Kazior5c771e72014-08-22 14:23:34 +02002442 if (!ath10k_pci_has_fw_crashed(ar)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002443 ath10k_warn(ar, "received unsolicited fw crash interrupt\n");
Michal Kazior5c771e72014-08-22 14:23:34 +02002444 return;
2445 }
2446
Michal Kazior6f3b7ff2015-01-24 12:14:52 +02002447 ath10k_pci_irq_disable(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02002448 ath10k_pci_fw_crashed_clear(ar);
2449 ath10k_pci_fw_crashed_dump(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002450}
2451
2452/*
2453 * Handler for a per-engine interrupt on a PARTICULAR CE.
2454 * This is used in cases where each CE has a private MSI interrupt.
2455 */
2456static irqreturn_t ath10k_pci_per_engine_handler(int irq, void *arg)
2457{
2458 struct ath10k *ar = arg;
2459 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2460 int ce_id = irq - ar_pci->pdev->irq - MSI_ASSIGN_CE_INITIAL;
2461
Dan Carpentere5742672013-06-18 10:28:46 +03002462 if (ce_id < 0 || ce_id >= ARRAY_SIZE(ar_pci->pipe_info)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002463 ath10k_warn(ar, "unexpected/invalid irq %d ce_id %d\n", irq,
2464 ce_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002465 return IRQ_HANDLED;
2466 }
2467
2468 /*
2469 * NOTE: We are able to derive ce_id from irq because we
2470 * use a one-to-one mapping for CE's 0..5.
2471 * CE's 6 & 7 do not use interrupts at all.
2472 *
2473 * This mapping must be kept in sync with the mapping
2474 * used by firmware.
2475 */
2476 tasklet_schedule(&ar_pci->pipe_info[ce_id].intr);
2477 return IRQ_HANDLED;
2478}
2479
2480static irqreturn_t ath10k_pci_msi_fw_handler(int irq, void *arg)
2481{
2482 struct ath10k *ar = arg;
2483 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2484
2485 tasklet_schedule(&ar_pci->msi_fw_err);
2486 return IRQ_HANDLED;
2487}
2488
2489/*
2490 * Top-level interrupt handler for all PCI interrupts from a Target.
2491 * When a block of MSI interrupts is allocated, this top-level handler
2492 * is not used; instead, we directly call the correct sub-handler.
2493 */
2494static irqreturn_t ath10k_pci_interrupt_handler(int irq, void *arg)
2495{
2496 struct ath10k *ar = arg;
2497 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2498
2499 if (ar_pci->num_msi_intrs == 0) {
Michal Kaziore5398872013-11-25 14:06:20 +01002500 if (!ath10k_pci_irq_pending(ar))
2501 return IRQ_NONE;
2502
Michal Kazior26852182013-11-25 14:06:25 +01002503 ath10k_pci_disable_and_clear_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002504 }
2505
2506 tasklet_schedule(&ar_pci->intr_tq);
2507
2508 return IRQ_HANDLED;
2509}
2510
2511static void ath10k_pci_tasklet(unsigned long data)
2512{
2513 struct ath10k *ar = (struct ath10k *)data;
2514 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2515
Michal Kazior5c771e72014-08-22 14:23:34 +02002516 if (ath10k_pci_has_fw_crashed(ar)) {
Michal Kazior6f3b7ff2015-01-24 12:14:52 +02002517 ath10k_pci_irq_disable(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02002518 ath10k_pci_fw_crashed_clear(ar);
2519 ath10k_pci_fw_crashed_dump(ar);
2520 return;
2521 }
2522
Kalle Valo5e3dd152013-06-12 20:52:10 +03002523 ath10k_ce_per_engine_service_any(ar);
2524
Michal Kazior26852182013-11-25 14:06:25 +01002525 /* Re-enable legacy irq that was disabled in the irq handler */
2526 if (ar_pci->num_msi_intrs == 0)
2527 ath10k_pci_enable_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002528}
2529
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002530static int ath10k_pci_request_irq_msix(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002531{
2532 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002533 int ret, i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002534
2535 ret = request_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW,
2536 ath10k_pci_msi_fw_handler,
2537 IRQF_SHARED, "ath10k_pci", ar);
Michal Kazior591ecdb2013-07-31 10:55:15 +02002538 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002539 ath10k_warn(ar, "failed to request MSI-X fw irq %d: %d\n",
Michal Kazior591ecdb2013-07-31 10:55:15 +02002540 ar_pci->pdev->irq + MSI_ASSIGN_FW, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002541 return ret;
Michal Kazior591ecdb2013-07-31 10:55:15 +02002542 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002543
2544 for (i = MSI_ASSIGN_CE_INITIAL; i <= MSI_ASSIGN_CE_MAX; i++) {
2545 ret = request_irq(ar_pci->pdev->irq + i,
2546 ath10k_pci_per_engine_handler,
2547 IRQF_SHARED, "ath10k_pci", ar);
2548 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002549 ath10k_warn(ar, "failed to request MSI-X ce irq %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002550 ar_pci->pdev->irq + i, ret);
2551
Michal Kazior87b14232013-06-26 08:50:50 +02002552 for (i--; i >= MSI_ASSIGN_CE_INITIAL; i--)
2553 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002554
Michal Kazior87b14232013-06-26 08:50:50 +02002555 free_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002556 return ret;
2557 }
2558 }
2559
Kalle Valo5e3dd152013-06-12 20:52:10 +03002560 return 0;
2561}
2562
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002563static int ath10k_pci_request_irq_msi(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002564{
2565 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2566 int ret;
2567
2568 ret = request_irq(ar_pci->pdev->irq,
2569 ath10k_pci_interrupt_handler,
2570 IRQF_SHARED, "ath10k_pci", ar);
Kalle Valof3782742013-10-17 11:36:15 +03002571 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002572 ath10k_warn(ar, "failed to request MSI irq %d: %d\n",
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002573 ar_pci->pdev->irq, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002574 return ret;
Kalle Valof3782742013-10-17 11:36:15 +03002575 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002576
Kalle Valo5e3dd152013-06-12 20:52:10 +03002577 return 0;
2578}
2579
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002580static int ath10k_pci_request_irq_legacy(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002581{
2582 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002583 int ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002584
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002585 ret = request_irq(ar_pci->pdev->irq,
2586 ath10k_pci_interrupt_handler,
2587 IRQF_SHARED, "ath10k_pci", ar);
Kalle Valof3782742013-10-17 11:36:15 +03002588 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002589 ath10k_warn(ar, "failed to request legacy irq %d: %d\n",
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002590 ar_pci->pdev->irq, ret);
Kalle Valof3782742013-10-17 11:36:15 +03002591 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002592 }
2593
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002594 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002595}
2596
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002597static int ath10k_pci_request_irq(struct ath10k *ar)
2598{
2599 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2600
2601 switch (ar_pci->num_msi_intrs) {
2602 case 0:
2603 return ath10k_pci_request_irq_legacy(ar);
2604 case 1:
2605 return ath10k_pci_request_irq_msi(ar);
Janusz Dziedzicb8402d82015-09-17 08:17:33 +02002606 default:
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002607 return ath10k_pci_request_irq_msix(ar);
2608 }
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002609}
2610
2611static void ath10k_pci_free_irq(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002612{
2613 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2614 int i;
2615
2616 /* There's at least one interrupt irregardless whether its legacy INTR
2617 * or MSI or MSI-X */
2618 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
2619 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002620}
2621
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002622static void ath10k_pci_init_irq_tasklets(struct ath10k *ar)
2623{
2624 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2625 int i;
2626
2627 tasklet_init(&ar_pci->intr_tq, ath10k_pci_tasklet, (unsigned long)ar);
2628 tasklet_init(&ar_pci->msi_fw_err, ath10k_msi_err_tasklet,
2629 (unsigned long)ar);
2630
2631 for (i = 0; i < CE_COUNT; i++) {
2632 ar_pci->pipe_info[i].ar_pci = ar_pci;
2633 tasklet_init(&ar_pci->pipe_info[i].intr, ath10k_pci_ce_tasklet,
2634 (unsigned long)&ar_pci->pipe_info[i]);
2635 }
2636}
2637
2638static int ath10k_pci_init_irq(struct ath10k *ar)
2639{
2640 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2641 int ret;
2642
2643 ath10k_pci_init_irq_tasklets(ar);
2644
Michal Kazior403d6272014-08-22 14:23:31 +02002645 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_AUTO)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002646 ath10k_info(ar, "limiting irq mode to: %d\n",
2647 ath10k_pci_irq_mode);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002648
2649 /* Try MSI-X */
Michal Kazior0edf2572014-08-07 11:03:29 +02002650 if (ath10k_pci_irq_mode == ATH10K_PCI_IRQ_AUTO) {
Janusz Dziedzicb8402d82015-09-17 08:17:33 +02002651 ar_pci->num_msi_intrs = MSI_ASSIGN_CE_MAX + 1;
Alexander Gordeev5ad68672014-02-13 17:50:02 +02002652 ret = pci_enable_msi_range(ar_pci->pdev, ar_pci->num_msi_intrs,
Kalle Valo5b07e072014-09-14 12:50:06 +03002653 ar_pci->num_msi_intrs);
Alexander Gordeev5ad68672014-02-13 17:50:02 +02002654 if (ret > 0)
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002655 return 0;
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002656
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002657 /* fall-through */
2658 }
2659
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002660 /* Try MSI */
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002661 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_LEGACY) {
2662 ar_pci->num_msi_intrs = 1;
2663 ret = pci_enable_msi(ar_pci->pdev);
2664 if (ret == 0)
2665 return 0;
2666
2667 /* fall-through */
2668 }
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002669
2670 /* Try legacy irq
2671 *
2672 * A potential race occurs here: The CORE_BASE write
2673 * depends on target correctly decoding AXI address but
2674 * host won't know when target writes BAR to CORE_CTRL.
2675 * This write might get lost if target has NOT written BAR.
2676 * For now, fix the race by repeating the write in below
2677 * synchronization checking. */
2678 ar_pci->num_msi_intrs = 0;
2679
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002680 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2681 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002682
2683 return 0;
2684}
2685
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002686static void ath10k_pci_deinit_irq_legacy(struct ath10k *ar)
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002687{
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002688 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2689 0);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002690}
2691
2692static int ath10k_pci_deinit_irq(struct ath10k *ar)
2693{
2694 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2695
2696 switch (ar_pci->num_msi_intrs) {
2697 case 0:
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002698 ath10k_pci_deinit_irq_legacy(ar);
Janusz Dziedzicb8402d82015-09-17 08:17:33 +02002699 break;
Alexander Gordeevbb8b6212014-02-13 17:50:01 +02002700 default:
2701 pci_disable_msi(ar_pci->pdev);
Janusz Dziedzicb8402d82015-09-17 08:17:33 +02002702 break;
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002703 }
2704
Janusz Dziedzicb8402d82015-09-17 08:17:33 +02002705 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002706}
2707
Michal Kaziord7fb47f2013-11-08 08:01:26 +01002708static int ath10k_pci_wait_for_target_init(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002709{
2710 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo0399eca2014-03-28 09:32:21 +02002711 unsigned long timeout;
Kalle Valo0399eca2014-03-28 09:32:21 +02002712 u32 val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002713
Michal Kazior7aa7a722014-08-25 12:09:38 +02002714 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot waiting target to initialise\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002715
Kalle Valo0399eca2014-03-28 09:32:21 +02002716 timeout = jiffies + msecs_to_jiffies(ATH10K_PCI_TARGET_WAIT);
2717
2718 do {
2719 val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
2720
Michal Kazior7aa7a722014-08-25 12:09:38 +02002721 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target indicator %x\n",
2722 val);
Kalle Valo50f87a62014-03-28 09:32:52 +02002723
Kalle Valo0399eca2014-03-28 09:32:21 +02002724 /* target should never return this */
2725 if (val == 0xffffffff)
2726 continue;
2727
Michal Kazior7710cd22014-04-23 19:30:04 +03002728 /* the device has crashed so don't bother trying anymore */
2729 if (val & FW_IND_EVENT_PENDING)
2730 break;
2731
Kalle Valo0399eca2014-03-28 09:32:21 +02002732 if (val & FW_IND_INITIALIZED)
2733 break;
2734
Kalle Valo5e3dd152013-06-12 20:52:10 +03002735 if (ar_pci->num_msi_intrs == 0)
2736 /* Fix potential race by repeating CORE_BASE writes */
Michal Kaziora4282492014-10-20 14:14:37 +02002737 ath10k_pci_enable_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002738
Kalle Valo0399eca2014-03-28 09:32:21 +02002739 mdelay(10);
2740 } while (time_before(jiffies, timeout));
2741
Michal Kaziora4282492014-10-20 14:14:37 +02002742 ath10k_pci_disable_and_clear_legacy_irq(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02002743 ath10k_pci_irq_msi_fw_mask(ar);
Michal Kaziora4282492014-10-20 14:14:37 +02002744
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002745 if (val == 0xffffffff) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002746 ath10k_err(ar, "failed to read device register, device is gone\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002747 return -EIO;
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002748 }
2749
Michal Kazior7710cd22014-04-23 19:30:04 +03002750 if (val & FW_IND_EVENT_PENDING) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002751 ath10k_warn(ar, "device has crashed during init\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002752 return -ECOMM;
Michal Kazior7710cd22014-04-23 19:30:04 +03002753 }
2754
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002755 if (!(val & FW_IND_INITIALIZED)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002756 ath10k_err(ar, "failed to receive initialized event from target: %08x\n",
Kalle Valo0399eca2014-03-28 09:32:21 +02002757 val);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002758 return -ETIMEDOUT;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002759 }
2760
Michal Kazior7aa7a722014-08-25 12:09:38 +02002761 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target initialised\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002762 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002763}
2764
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002765static int ath10k_pci_cold_reset(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002766{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002767 u32 val;
2768
Michal Kazior7aa7a722014-08-25 12:09:38 +02002769 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cold reset\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002770
Ben Greearf51dbe72014-09-29 14:41:46 +03002771 spin_lock_bh(&ar->data_lock);
2772
2773 ar->stats.fw_cold_reset_counter++;
2774
2775 spin_unlock_bh(&ar->data_lock);
2776
Kalle Valo5e3dd152013-06-12 20:52:10 +03002777 /* Put Target, including PCIe, into RESET. */
Kalle Valoe479ed42013-09-01 10:01:53 +03002778 val = ath10k_pci_reg_read32(ar, SOC_GLOBAL_RESET_ADDRESS);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002779 val |= 1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002780 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002781
Vasanthakumar Thiagarajanacd19582015-07-10 14:31:20 +05302782 /* After writing into SOC_GLOBAL_RESET to put device into
2783 * reset and pulling out of reset pcie may not be stable
2784 * for any immediate pcie register access and cause bus error,
2785 * add delay before any pcie access request to fix this issue.
2786 */
2787 msleep(20);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002788
2789 /* Pull Target, including PCIe, out of RESET. */
2790 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 msleep(20);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002794
Michal Kazior7aa7a722014-08-25 12:09:38 +02002795 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cold reset complete\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02002796
Michal Kazior5b2589f2013-11-08 08:01:30 +01002797 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002798}
2799
Michal Kazior2986e3e2014-08-07 11:03:30 +02002800static int ath10k_pci_claim(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002801{
Michal Kazior2986e3e2014-08-07 11:03:30 +02002802 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2803 struct pci_dev *pdev = ar_pci->pdev;
Michal Kazior2986e3e2014-08-07 11:03:30 +02002804 int ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002805
2806 pci_set_drvdata(pdev, ar);
2807
Kalle Valo5e3dd152013-06-12 20:52:10 +03002808 ret = pci_enable_device(pdev);
2809 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002810 ath10k_err(ar, "failed to enable pci device: %d\n", ret);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002811 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002812 }
2813
Kalle Valo5e3dd152013-06-12 20:52:10 +03002814 ret = pci_request_region(pdev, BAR_NUM, "ath");
2815 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002816 ath10k_err(ar, "failed to request region BAR%d: %d\n", BAR_NUM,
Michal Kazior2986e3e2014-08-07 11:03:30 +02002817 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002818 goto err_device;
2819 }
2820
Michal Kazior2986e3e2014-08-07 11:03:30 +02002821 /* Target expects 32 bit DMA. Enforce it. */
Kalle Valo5e3dd152013-06-12 20:52:10 +03002822 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2823 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002824 ath10k_err(ar, "failed to set dma mask to 32-bit: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002825 goto err_region;
2826 }
2827
2828 ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2829 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002830 ath10k_err(ar, "failed to set consistent dma mask to 32-bit: %d\n",
Michal Kazior2986e3e2014-08-07 11:03:30 +02002831 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002832 goto err_region;
2833 }
2834
Kalle Valo5e3dd152013-06-12 20:52:10 +03002835 pci_set_master(pdev);
2836
Kalle Valo5e3dd152013-06-12 20:52:10 +03002837 /* Arrange for access to Target SoC registers. */
Michal Kazioraeae5b42015-06-15 14:46:42 +03002838 ar_pci->mem_len = pci_resource_len(pdev, BAR_NUM);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002839 ar_pci->mem = pci_iomap(pdev, BAR_NUM, 0);
2840 if (!ar_pci->mem) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002841 ath10k_err(ar, "failed to iomap BAR%d\n", BAR_NUM);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002842 ret = -EIO;
2843 goto err_master;
2844 }
2845
Michal Kazior7aa7a722014-08-25 12:09:38 +02002846 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot pci_mem 0x%p\n", ar_pci->mem);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002847 return 0;
2848
2849err_master:
2850 pci_clear_master(pdev);
2851
2852err_region:
2853 pci_release_region(pdev, BAR_NUM);
2854
2855err_device:
2856 pci_disable_device(pdev);
2857
2858 return ret;
2859}
2860
2861static void ath10k_pci_release(struct ath10k *ar)
2862{
2863 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2864 struct pci_dev *pdev = ar_pci->pdev;
2865
2866 pci_iounmap(pdev, ar_pci->mem);
2867 pci_release_region(pdev, BAR_NUM);
2868 pci_clear_master(pdev);
2869 pci_disable_device(pdev);
2870}
2871
Michal Kazior7505f7c2014-12-02 10:55:54 +02002872static bool ath10k_pci_chip_is_supported(u32 dev_id, u32 chip_id)
2873{
2874 const struct ath10k_pci_supp_chip *supp_chip;
2875 int i;
2876 u32 rev_id = MS(chip_id, SOC_CHIP_ID_REV);
2877
2878 for (i = 0; i < ARRAY_SIZE(ath10k_pci_supp_chips); i++) {
2879 supp_chip = &ath10k_pci_supp_chips[i];
2880
2881 if (supp_chip->dev_id == dev_id &&
2882 supp_chip->rev_id == rev_id)
2883 return true;
2884 }
2885
2886 return false;
2887}
2888
Kalle Valo5e3dd152013-06-12 20:52:10 +03002889static int ath10k_pci_probe(struct pci_dev *pdev,
2890 const struct pci_device_id *pci_dev)
2891{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002892 int ret = 0;
2893 struct ath10k *ar;
2894 struct ath10k_pci *ar_pci;
Michal Kaziord63955b2015-01-24 12:14:49 +02002895 enum ath10k_hw_rev hw_rev;
Michal Kazior2986e3e2014-08-07 11:03:30 +02002896 u32 chip_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002897
Michal Kaziord63955b2015-01-24 12:14:49 +02002898 switch (pci_dev->device) {
2899 case QCA988X_2_0_DEVICE_ID:
2900 hw_rev = ATH10K_HW_QCA988X;
2901 break;
Michal Kazior36582e52015-08-13 14:32:26 +02002902 case QCA6164_2_1_DEVICE_ID:
Michal Kaziord63955b2015-01-24 12:14:49 +02002903 case QCA6174_2_1_DEVICE_ID:
2904 hw_rev = ATH10K_HW_QCA6174;
2905 break;
Vasanthakumar Thiagarajan8bd47022015-06-18 12:31:03 +05302906 case QCA99X0_2_0_DEVICE_ID:
2907 hw_rev = ATH10K_HW_QCA99X0;
2908 break;
Michal Kaziord63955b2015-01-24 12:14:49 +02002909 default:
2910 WARN_ON(1);
2911 return -ENOTSUPP;
2912 }
2913
2914 ar = ath10k_core_create(sizeof(*ar_pci), &pdev->dev, ATH10K_BUS_PCI,
2915 hw_rev, &ath10k_pci_hif_ops);
Michal Kaziore7b54192014-08-07 11:03:27 +02002916 if (!ar) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002917 dev_err(&pdev->dev, "failed to allocate core\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002918 return -ENOMEM;
Michal Kaziore7b54192014-08-07 11:03:27 +02002919 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002920
Manikanta Pubbisetty0a51b342015-10-09 11:55:58 +03002921 ath10k_dbg(ar, ATH10K_DBG_BOOT, "pci probe %04x:%04x %04x:%04x\n",
2922 pdev->vendor, pdev->device,
2923 pdev->subsystem_vendor, pdev->subsystem_device);
Michal Kazior7aa7a722014-08-25 12:09:38 +02002924
Michal Kaziore7b54192014-08-07 11:03:27 +02002925 ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002926 ar_pci->pdev = pdev;
2927 ar_pci->dev = &pdev->dev;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002928 ar_pci->ar = ar;
Michal Kazior36582e52015-08-13 14:32:26 +02002929 ar->dev_id = pci_dev->device;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002930
Manikanta Pubbisetty0a51b342015-10-09 11:55:58 +03002931 ar->id.vendor = pdev->vendor;
2932 ar->id.device = pdev->device;
2933 ar->id.subsystem_vendor = pdev->subsystem_vendor;
2934 ar->id.subsystem_device = pdev->subsystem_device;
Michal Kaziorde57e2c2015-04-17 09:19:17 +00002935
Kalle Valo5e3dd152013-06-12 20:52:10 +03002936 spin_lock_init(&ar_pci->ce_lock);
Michal Kazior77258d42015-05-18 09:38:18 +00002937 spin_lock_init(&ar_pci->ps_lock);
2938
Michal Kazior728f95e2014-08-22 14:33:14 +02002939 setup_timer(&ar_pci->rx_post_retry, ath10k_pci_rx_replenish_retry,
2940 (unsigned long)ar);
Michal Kazior77258d42015-05-18 09:38:18 +00002941 setup_timer(&ar_pci->ps_timer, ath10k_pci_ps_timer,
2942 (unsigned long)ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002943
Michal Kazior2986e3e2014-08-07 11:03:30 +02002944 ret = ath10k_pci_claim(ar);
Kalle Valoe01ae682013-09-01 11:22:14 +03002945 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002946 ath10k_err(ar, "failed to claim device: %d\n", ret);
Michal Kaziore7b54192014-08-07 11:03:27 +02002947 goto err_core_destroy;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002948 }
2949
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002950 ret = ath10k_pci_alloc_pipes(ar);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002951 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002952 ath10k_err(ar, "failed to allocate copy engine pipes: %d\n",
2953 ret);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002954 goto err_sleep;
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002955 }
2956
Michal Kazior403d6272014-08-22 14:23:31 +02002957 ath10k_pci_ce_deinit(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02002958 ath10k_pci_irq_disable(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02002959
Michal Kazior403d6272014-08-22 14:23:31 +02002960 ret = ath10k_pci_init_irq(ar);
2961 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002962 ath10k_err(ar, "failed to init irqs: %d\n", ret);
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002963 goto err_free_pipes;
Michal Kazior403d6272014-08-22 14:23:31 +02002964 }
2965
Michal Kazior7aa7a722014-08-25 12:09:38 +02002966 ath10k_info(ar, "pci irq %s interrupts %d irq_mode %d reset_mode %d\n",
Michal Kazior403d6272014-08-22 14:23:31 +02002967 ath10k_pci_get_irq_method(ar), ar_pci->num_msi_intrs,
2968 ath10k_pci_irq_mode, ath10k_pci_reset_mode);
2969
Michal Kazior5c771e72014-08-22 14:23:34 +02002970 ret = ath10k_pci_request_irq(ar);
Michal Kazior403d6272014-08-22 14:23:31 +02002971 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002972 ath10k_warn(ar, "failed to request irqs: %d\n", ret);
Michal Kazior403d6272014-08-22 14:23:31 +02002973 goto err_deinit_irq;
2974 }
2975
Michal Kazior1a7fecb2015-01-24 12:14:48 +02002976 ret = ath10k_pci_chip_reset(ar);
2977 if (ret) {
2978 ath10k_err(ar, "failed to reset chip: %d\n", ret);
2979 goto err_free_irq;
2980 }
2981
2982 chip_id = ath10k_pci_soc_read32(ar, SOC_CHIP_ID_ADDRESS);
2983 if (chip_id == 0xffffffff) {
2984 ath10k_err(ar, "failed to get chip id\n");
2985 goto err_free_irq;
2986 }
2987
2988 if (!ath10k_pci_chip_is_supported(pdev->device, chip_id)) {
2989 ath10k_err(ar, "device %04x with chip_id %08x isn't supported\n",
2990 pdev->device, chip_id);
Michal Kaziord9585a92015-04-10 13:01:27 +00002991 goto err_free_irq;
Michal Kazior1a7fecb2015-01-24 12:14:48 +02002992 }
2993
Kalle Valoe01ae682013-09-01 11:22:14 +03002994 ret = ath10k_core_register(ar, chip_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002995 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002996 ath10k_err(ar, "failed to register driver core: %d\n", ret);
Michal Kazior5c771e72014-08-22 14:23:34 +02002997 goto err_free_irq;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002998 }
2999
3000 return 0;
3001
Michal Kazior5c771e72014-08-22 14:23:34 +02003002err_free_irq:
3003 ath10k_pci_free_irq(ar);
Michal Kazior21396272014-08-28 10:24:40 +02003004 ath10k_pci_kill_tasklet(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02003005
Michal Kazior403d6272014-08-22 14:23:31 +02003006err_deinit_irq:
3007 ath10k_pci_deinit_irq(ar);
3008
Michal Kazior84cbf3a2014-10-20 14:14:39 +02003009err_free_pipes:
3010 ath10k_pci_free_pipes(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02003011
Michal Kaziorc0c378f2014-08-07 11:03:28 +02003012err_sleep:
Michal Kazior0bcbbe62015-05-29 07:35:24 +02003013 ath10k_pci_sleep_sync(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02003014 ath10k_pci_release(ar);
3015
Michal Kaziore7b54192014-08-07 11:03:27 +02003016err_core_destroy:
Kalle Valo5e3dd152013-06-12 20:52:10 +03003017 ath10k_core_destroy(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003018
3019 return ret;
3020}
3021
3022static void ath10k_pci_remove(struct pci_dev *pdev)
3023{
3024 struct ath10k *ar = pci_get_drvdata(pdev);
3025 struct ath10k_pci *ar_pci;
3026
Michal Kazior7aa7a722014-08-25 12:09:38 +02003027 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci remove\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03003028
3029 if (!ar)
3030 return;
3031
3032 ar_pci = ath10k_pci_priv(ar);
3033
3034 if (!ar_pci)
3035 return;
3036
Kalle Valo5e3dd152013-06-12 20:52:10 +03003037 ath10k_core_unregister(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02003038 ath10k_pci_free_irq(ar);
Michal Kazior21396272014-08-28 10:24:40 +02003039 ath10k_pci_kill_tasklet(ar);
Michal Kazior403d6272014-08-22 14:23:31 +02003040 ath10k_pci_deinit_irq(ar);
3041 ath10k_pci_ce_deinit(ar);
Michal Kazior84cbf3a2014-10-20 14:14:39 +02003042 ath10k_pci_free_pipes(ar);
Michal Kazior77258d42015-05-18 09:38:18 +00003043 ath10k_pci_sleep_sync(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02003044 ath10k_pci_release(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003045 ath10k_core_destroy(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003046}
3047
Kalle Valo5e3dd152013-06-12 20:52:10 +03003048MODULE_DEVICE_TABLE(pci, ath10k_pci_id_table);
3049
3050static struct pci_driver ath10k_pci_driver = {
3051 .name = "ath10k_pci",
3052 .id_table = ath10k_pci_id_table,
3053 .probe = ath10k_pci_probe,
3054 .remove = ath10k_pci_remove,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003055};
3056
3057static int __init ath10k_pci_init(void)
3058{
3059 int ret;
3060
3061 ret = pci_register_driver(&ath10k_pci_driver);
3062 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003063 printk(KERN_ERR "failed to register ath10k pci driver: %d\n",
3064 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003065
3066 return ret;
3067}
3068module_init(ath10k_pci_init);
3069
3070static void __exit ath10k_pci_exit(void)
3071{
3072 pci_unregister_driver(&ath10k_pci_driver);
3073}
3074
3075module_exit(ath10k_pci_exit);
3076
3077MODULE_AUTHOR("Qualcomm Atheros");
3078MODULE_DESCRIPTION("Driver support for Atheros QCA988X PCIe devices");
3079MODULE_LICENSE("Dual BSD/GPL");
Bartosz Markowski5c427f52015-02-18 13:16:37 +01003080
3081/* QCA988x 2.0 firmware files */
Bartosz Markowski8026cae2014-10-06 14:16:41 +02003082MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_FW_FILE);
3083MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API2_FILE);
3084MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API3_FILE);
Bartosz Markowski5c427f52015-02-18 13:16:37 +01003085MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API4_FILE);
Kalle Valo53513c32015-03-25 13:12:42 +02003086MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API5_FILE);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003087MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_BOARD_DATA_FILE);
Manikanta Pubbisetty0a51b342015-10-09 11:55:58 +03003088MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_BOARD_API2_FILE);
Bartosz Markowski5c427f52015-02-18 13:16:37 +01003089
3090/* QCA6174 2.1 firmware files */
3091MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" ATH10K_FW_API4_FILE);
Michal Kaziore451c1d2015-05-26 13:09:22 +02003092MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" ATH10K_FW_API5_FILE);
Bartosz Markowski5c427f52015-02-18 13:16:37 +01003093MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" QCA6174_HW_2_1_BOARD_DATA_FILE);
Manikanta Pubbisetty0a51b342015-10-09 11:55:58 +03003094MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" ATH10K_BOARD_API2_FILE);
Bartosz Markowski5c427f52015-02-18 13:16:37 +01003095
3096/* QCA6174 3.1 firmware files */
3097MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" ATH10K_FW_API4_FILE);
Michal Kaziore451c1d2015-05-26 13:09:22 +02003098MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" ATH10K_FW_API5_FILE);
Bartosz Markowski5c427f52015-02-18 13:16:37 +01003099MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" QCA6174_HW_3_0_BOARD_DATA_FILE);
Manikanta Pubbisetty0a51b342015-10-09 11:55:58 +03003100MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" ATH10K_BOARD_API2_FILE);