blob: 110fcad609b900f9abe32aec3f6e0ab8b8df6e63 [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) {
455 if (ath10k_pci_is_awake(ar))
456 return 0;
457
458 udelay(curr_delay);
459 tot_delay += curr_delay;
460
461 if (curr_delay < 50)
462 curr_delay += 5;
463 }
464
465 return -ETIMEDOUT;
466}
467
468static int ath10k_pci_wake(struct ath10k *ar)
469{
470 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
471 unsigned long flags;
472 int ret = 0;
473
474 spin_lock_irqsave(&ar_pci->ps_lock, flags);
475
476 ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps wake refcount %lu awake %d\n",
477 ar_pci->ps_wake_refcount, ar_pci->ps_awake);
478
479 /* This function can be called very frequently. To avoid excessive
480 * CPU stalls for MMIO reads use a cache var to hold the device state.
481 */
482 if (!ar_pci->ps_awake) {
483 __ath10k_pci_wake(ar);
484
485 ret = ath10k_pci_wake_wait(ar);
486 if (ret == 0)
487 ar_pci->ps_awake = true;
488 }
489
490 if (ret == 0) {
491 ar_pci->ps_wake_refcount++;
492 WARN_ON(ar_pci->ps_wake_refcount == 0);
493 }
494
495 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
496
497 return ret;
498}
499
500static void ath10k_pci_sleep(struct ath10k *ar)
501{
502 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
503 unsigned long flags;
504
505 spin_lock_irqsave(&ar_pci->ps_lock, flags);
506
507 ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps sleep refcount %lu awake %d\n",
508 ar_pci->ps_wake_refcount, ar_pci->ps_awake);
509
510 if (WARN_ON(ar_pci->ps_wake_refcount == 0))
511 goto skip;
512
513 ar_pci->ps_wake_refcount--;
514
515 mod_timer(&ar_pci->ps_timer, jiffies +
516 msecs_to_jiffies(ATH10K_PCI_SLEEP_GRACE_PERIOD_MSEC));
517
518skip:
519 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
520}
521
522static void ath10k_pci_ps_timer(unsigned long ptr)
523{
524 struct ath10k *ar = (void *)ptr;
525 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
526 unsigned long flags;
527
528 spin_lock_irqsave(&ar_pci->ps_lock, flags);
529
530 ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps timer refcount %lu awake %d\n",
531 ar_pci->ps_wake_refcount, ar_pci->ps_awake);
532
533 if (ar_pci->ps_wake_refcount > 0)
534 goto skip;
535
536 __ath10k_pci_sleep(ar);
537
538skip:
539 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
540}
541
542static void ath10k_pci_sleep_sync(struct ath10k *ar)
543{
544 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
545 unsigned long flags;
546
547 del_timer_sync(&ar_pci->ps_timer);
548
549 spin_lock_irqsave(&ar_pci->ps_lock, flags);
550 WARN_ON(ar_pci->ps_wake_refcount > 0);
551 __ath10k_pci_sleep(ar);
552 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
553}
554
555void ath10k_pci_write32(struct ath10k *ar, u32 offset, u32 value)
556{
557 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
558 int ret;
559
Michal Kazioraeae5b42015-06-15 14:46:42 +0300560 if (unlikely(offset + sizeof(value) > ar_pci->mem_len)) {
561 ath10k_warn(ar, "refusing to write mmio out of bounds at 0x%08x - 0x%08zx (max 0x%08zx)\n",
562 offset, offset + sizeof(value), ar_pci->mem_len);
563 return;
564 }
565
Michal Kazior77258d42015-05-18 09:38:18 +0000566 ret = ath10k_pci_wake(ar);
567 if (ret) {
568 ath10k_warn(ar, "failed to wake target for write32 of 0x%08x at 0x%08x: %d\n",
569 value, offset, ret);
570 return;
571 }
572
573 iowrite32(value, ar_pci->mem + offset);
574 ath10k_pci_sleep(ar);
575}
576
577u32 ath10k_pci_read32(struct ath10k *ar, u32 offset)
578{
579 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
580 u32 val;
581 int ret;
582
Michal Kazioraeae5b42015-06-15 14:46:42 +0300583 if (unlikely(offset + sizeof(val) > ar_pci->mem_len)) {
584 ath10k_warn(ar, "refusing to read mmio out of bounds at 0x%08x - 0x%08zx (max 0x%08zx)\n",
585 offset, offset + sizeof(val), ar_pci->mem_len);
586 return 0;
587 }
588
Michal Kazior77258d42015-05-18 09:38:18 +0000589 ret = ath10k_pci_wake(ar);
590 if (ret) {
591 ath10k_warn(ar, "failed to wake target for read32 at 0x%08x: %d\n",
592 offset, ret);
593 return 0xffffffff;
594 }
595
596 val = ioread32(ar_pci->mem + offset);
597 ath10k_pci_sleep(ar);
598
599 return val;
600}
601
602u32 ath10k_pci_soc_read32(struct ath10k *ar, u32 addr)
603{
604 return ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS + addr);
605}
606
607void ath10k_pci_soc_write32(struct ath10k *ar, u32 addr, u32 val)
608{
609 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + addr, val);
610}
611
612u32 ath10k_pci_reg_read32(struct ath10k *ar, u32 addr)
613{
614 return ath10k_pci_read32(ar, PCIE_LOCAL_BASE_ADDRESS + addr);
615}
616
617void ath10k_pci_reg_write32(struct ath10k *ar, u32 addr, u32 val)
618{
619 ath10k_pci_write32(ar, PCIE_LOCAL_BASE_ADDRESS + addr, val);
620}
621
Michal Kaziore5398872013-11-25 14:06:20 +0100622static bool ath10k_pci_irq_pending(struct ath10k *ar)
623{
624 u32 cause;
625
626 /* Check if the shared legacy irq is for us */
627 cause = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
628 PCIE_INTR_CAUSE_ADDRESS);
629 if (cause & (PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL))
630 return true;
631
632 return false;
633}
634
Michal Kazior26852182013-11-25 14:06:25 +0100635static void ath10k_pci_disable_and_clear_legacy_irq(struct ath10k *ar)
636{
637 /* IMPORTANT: INTR_CLR register has to be set after
638 * INTR_ENABLE is set to 0, otherwise interrupt can not be
639 * really cleared. */
640 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
641 0);
642 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_CLR_ADDRESS,
643 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
644
645 /* IMPORTANT: this extra read transaction is required to
646 * flush the posted write buffer. */
Kalle Valocfbc06a2014-09-14 12:50:23 +0300647 (void)ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
648 PCIE_INTR_ENABLE_ADDRESS);
Michal Kazior26852182013-11-25 14:06:25 +0100649}
650
651static void ath10k_pci_enable_legacy_irq(struct ath10k *ar)
652{
653 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
654 PCIE_INTR_ENABLE_ADDRESS,
655 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
656
657 /* IMPORTANT: this extra read transaction is required to
658 * flush the posted write buffer. */
Kalle Valocfbc06a2014-09-14 12:50:23 +0300659 (void)ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
660 PCIE_INTR_ENABLE_ADDRESS);
Michal Kazior26852182013-11-25 14:06:25 +0100661}
662
Michal Kazior403d6272014-08-22 14:23:31 +0200663static inline const char *ath10k_pci_get_irq_method(struct ath10k *ar)
Michal Kaziorab977bd2013-11-25 14:06:26 +0100664{
Michal Kaziorab977bd2013-11-25 14:06:26 +0100665 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
666
Michal Kazior403d6272014-08-22 14:23:31 +0200667 if (ar_pci->num_msi_intrs > 1)
668 return "msi-x";
Kalle Valod8bb26b2014-09-14 12:50:33 +0300669
670 if (ar_pci->num_msi_intrs == 1)
Michal Kazior403d6272014-08-22 14:23:31 +0200671 return "msi";
Kalle Valod8bb26b2014-09-14 12:50:33 +0300672
673 return "legacy";
Michal Kaziorab977bd2013-11-25 14:06:26 +0100674}
675
Michal Kazior728f95e2014-08-22 14:33:14 +0200676static int __ath10k_pci_rx_post_buf(struct ath10k_pci_pipe *pipe)
Michal Kaziorab977bd2013-11-25 14:06:26 +0100677{
Michal Kazior728f95e2014-08-22 14:33:14 +0200678 struct ath10k *ar = pipe->hif_ce_state;
Michal Kaziorab977bd2013-11-25 14:06:26 +0100679 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior728f95e2014-08-22 14:33:14 +0200680 struct ath10k_ce_pipe *ce_pipe = pipe->ce_hdl;
681 struct sk_buff *skb;
682 dma_addr_t paddr;
Michal Kaziorab977bd2013-11-25 14:06:26 +0100683 int ret;
684
Michal Kazior728f95e2014-08-22 14:33:14 +0200685 lockdep_assert_held(&ar_pci->ce_lock);
686
687 skb = dev_alloc_skb(pipe->buf_sz);
688 if (!skb)
689 return -ENOMEM;
690
691 WARN_ONCE((unsigned long)skb->data & 3, "unaligned skb");
692
693 paddr = dma_map_single(ar->dev, skb->data,
694 skb->len + skb_tailroom(skb),
695 DMA_FROM_DEVICE);
696 if (unlikely(dma_mapping_error(ar->dev, paddr))) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200697 ath10k_warn(ar, "failed to dma map pci rx buf\n");
Michal Kazior728f95e2014-08-22 14:33:14 +0200698 dev_kfree_skb_any(skb);
699 return -EIO;
700 }
701
Michal Kazior8582bf32015-01-24 12:14:47 +0200702 ATH10K_SKB_RXCB(skb)->paddr = paddr;
Michal Kazior728f95e2014-08-22 14:33:14 +0200703
704 ret = __ath10k_ce_rx_post_buf(ce_pipe, skb, paddr);
Michal Kaziorab977bd2013-11-25 14:06:26 +0100705 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200706 ath10k_warn(ar, "failed to post pci rx buf: %d\n", ret);
Michal Kazior728f95e2014-08-22 14:33:14 +0200707 dma_unmap_single(ar->dev, paddr, skb->len + skb_tailroom(skb),
708 DMA_FROM_DEVICE);
709 dev_kfree_skb_any(skb);
Michal Kaziorab977bd2013-11-25 14:06:26 +0100710 return ret;
711 }
712
713 return 0;
714}
715
Michal Kazior728f95e2014-08-22 14:33:14 +0200716static void __ath10k_pci_rx_post_pipe(struct ath10k_pci_pipe *pipe)
Michal Kaziorab977bd2013-11-25 14:06:26 +0100717{
Michal Kazior728f95e2014-08-22 14:33:14 +0200718 struct ath10k *ar = pipe->hif_ce_state;
719 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
720 struct ath10k_ce_pipe *ce_pipe = pipe->ce_hdl;
721 int ret, num;
722
723 lockdep_assert_held(&ar_pci->ce_lock);
724
725 if (pipe->buf_sz == 0)
726 return;
727
728 if (!ce_pipe->dest_ring)
729 return;
730
731 num = __ath10k_ce_rx_num_free_bufs(ce_pipe);
732 while (num--) {
733 ret = __ath10k_pci_rx_post_buf(pipe);
734 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200735 ath10k_warn(ar, "failed to post pci rx buf: %d\n", ret);
Michal Kazior728f95e2014-08-22 14:33:14 +0200736 mod_timer(&ar_pci->rx_post_retry, jiffies +
737 ATH10K_PCI_RX_POST_RETRY_MS);
738 break;
739 }
740 }
741}
742
743static void ath10k_pci_rx_post_pipe(struct ath10k_pci_pipe *pipe)
744{
745 struct ath10k *ar = pipe->hif_ce_state;
746 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
747
748 spin_lock_bh(&ar_pci->ce_lock);
749 __ath10k_pci_rx_post_pipe(pipe);
750 spin_unlock_bh(&ar_pci->ce_lock);
751}
752
753static void ath10k_pci_rx_post(struct ath10k *ar)
754{
755 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
756 int i;
757
758 spin_lock_bh(&ar_pci->ce_lock);
759 for (i = 0; i < CE_COUNT; i++)
760 __ath10k_pci_rx_post_pipe(&ar_pci->pipe_info[i]);
761 spin_unlock_bh(&ar_pci->ce_lock);
762}
763
764static void ath10k_pci_rx_replenish_retry(unsigned long ptr)
765{
766 struct ath10k *ar = (void *)ptr;
767
768 ath10k_pci_rx_post(ar);
Michal Kaziorab977bd2013-11-25 14:06:26 +0100769}
770
Vasanthakumar Thiagarajan418ca592015-06-18 12:31:05 +0530771static u32 ath10k_pci_targ_cpu_to_ce_addr(struct ath10k *ar, u32 addr)
772{
773 u32 val = 0;
774
775 switch (ar->hw_rev) {
776 case ATH10K_HW_QCA988X:
777 case ATH10K_HW_QCA6174:
778 val = (ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
779 CORE_CTRL_ADDRESS) &
Vasanthakumar Thiagarajan3c7e2562015-07-03 19:25:27 +0530780 0x7ff) << 21;
Vasanthakumar Thiagarajan418ca592015-06-18 12:31:05 +0530781 break;
782 case ATH10K_HW_QCA99X0:
783 val = ath10k_pci_read32(ar, PCIE_BAR_REG_ADDRESS);
784 break;
785 }
786
787 val |= 0x100000 | (addr & 0xfffff);
788 return val;
789}
790
Kalle Valo5e3dd152013-06-12 20:52:10 +0300791/*
792 * Diagnostic read/write access is provided for startup/config/debug usage.
793 * Caller must guarantee proper alignment, when applicable, and single user
794 * at any moment.
795 */
796static int ath10k_pci_diag_read_mem(struct ath10k *ar, u32 address, void *data,
797 int nbytes)
798{
799 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
800 int ret = 0;
801 u32 buf;
802 unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
803 unsigned int id;
804 unsigned int flags;
Michal Kazior2aa39112013-08-27 13:08:02 +0200805 struct ath10k_ce_pipe *ce_diag;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300806 /* Host buffer address in CE space */
807 u32 ce_data;
808 dma_addr_t ce_data_base = 0;
809 void *data_buf = NULL;
810 int i;
811
Kalle Valoeef25402014-09-24 14:16:52 +0300812 spin_lock_bh(&ar_pci->ce_lock);
813
Kalle Valo5e3dd152013-06-12 20:52:10 +0300814 ce_diag = ar_pci->ce_diag;
815
816 /*
817 * Allocate a temporary bounce buffer to hold caller's data
818 * to be DMA'ed from Target. This guarantees
819 * 1) 4-byte alignment
820 * 2) Buffer in DMA-able space
821 */
822 orig_nbytes = nbytes;
Michal Kazior68c03242014-03-28 10:02:35 +0200823 data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
824 orig_nbytes,
825 &ce_data_base,
826 GFP_ATOMIC);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300827
828 if (!data_buf) {
829 ret = -ENOMEM;
830 goto done;
831 }
832 memset(data_buf, 0, orig_nbytes);
833
834 remaining_bytes = orig_nbytes;
835 ce_data = ce_data_base;
836 while (remaining_bytes) {
837 nbytes = min_t(unsigned int, remaining_bytes,
838 DIAG_TRANSFER_LIMIT);
839
Kalle Valoeef25402014-09-24 14:16:52 +0300840 ret = __ath10k_ce_rx_post_buf(ce_diag, NULL, ce_data);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300841 if (ret != 0)
842 goto done;
843
844 /* Request CE to send from Target(!) address to Host buffer */
845 /*
846 * The address supplied by the caller is in the
847 * Target CPU virtual address space.
848 *
849 * In order to use this address with the diagnostic CE,
850 * convert it from Target CPU virtual address space
851 * to CE address space
852 */
Vasanthakumar Thiagarajan418ca592015-06-18 12:31:05 +0530853 address = ath10k_pci_targ_cpu_to_ce_addr(ar, address);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300854
Kalle Valoeef25402014-09-24 14:16:52 +0300855 ret = ath10k_ce_send_nolock(ce_diag, NULL, (u32)address, nbytes, 0,
856 0);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300857 if (ret)
858 goto done;
859
860 i = 0;
Kalle Valoeef25402014-09-24 14:16:52 +0300861 while (ath10k_ce_completed_send_next_nolock(ce_diag, NULL, &buf,
862 &completed_nbytes,
863 &id) != 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300864 mdelay(1);
865 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
866 ret = -EBUSY;
867 goto done;
868 }
869 }
870
871 if (nbytes != completed_nbytes) {
872 ret = -EIO;
873 goto done;
874 }
875
Kalle Valocfbc06a2014-09-14 12:50:23 +0300876 if (buf != (u32)address) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300877 ret = -EIO;
878 goto done;
879 }
880
881 i = 0;
Kalle Valoeef25402014-09-24 14:16:52 +0300882 while (ath10k_ce_completed_recv_next_nolock(ce_diag, NULL, &buf,
883 &completed_nbytes,
884 &id, &flags) != 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300885 mdelay(1);
886
887 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
888 ret = -EBUSY;
889 goto done;
890 }
891 }
892
893 if (nbytes != completed_nbytes) {
894 ret = -EIO;
895 goto done;
896 }
897
898 if (buf != ce_data) {
899 ret = -EIO;
900 goto done;
901 }
902
903 remaining_bytes -= nbytes;
904 address += nbytes;
905 ce_data += nbytes;
906 }
907
908done:
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300909 if (ret == 0)
910 memcpy(data, data_buf, orig_nbytes);
911 else
Michal Kazior7aa7a722014-08-25 12:09:38 +0200912 ath10k_warn(ar, "failed to read diag value at 0x%x: %d\n",
Kalle Valo50f87a62014-03-28 09:32:52 +0200913 address, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300914
915 if (data_buf)
Michal Kazior68c03242014-03-28 10:02:35 +0200916 dma_free_coherent(ar->dev, orig_nbytes, data_buf,
917 ce_data_base);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300918
Kalle Valoeef25402014-09-24 14:16:52 +0300919 spin_unlock_bh(&ar_pci->ce_lock);
920
Kalle Valo5e3dd152013-06-12 20:52:10 +0300921 return ret;
922}
923
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300924static int ath10k_pci_diag_read32(struct ath10k *ar, u32 address, u32 *value)
925{
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300926 __le32 val = 0;
927 int ret;
928
929 ret = ath10k_pci_diag_read_mem(ar, address, &val, sizeof(val));
930 *value = __le32_to_cpu(val);
931
932 return ret;
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300933}
934
935static int __ath10k_pci_diag_read_hi(struct ath10k *ar, void *dest,
936 u32 src, u32 len)
937{
938 u32 host_addr, addr;
939 int ret;
940
941 host_addr = host_interest_item_address(src);
942
943 ret = ath10k_pci_diag_read32(ar, host_addr, &addr);
944 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200945 ath10k_warn(ar, "failed to get memcpy hi address for firmware address %d: %d\n",
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300946 src, ret);
947 return ret;
948 }
949
950 ret = ath10k_pci_diag_read_mem(ar, addr, dest, len);
951 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200952 ath10k_warn(ar, "failed to memcpy firmware memory from %d (%d B): %d\n",
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300953 addr, len, ret);
954 return ret;
955 }
956
957 return 0;
958}
959
960#define ath10k_pci_diag_read_hi(ar, dest, src, len) \
Kalle Valo8cc7f262014-09-14 12:50:39 +0300961 __ath10k_pci_diag_read_hi(ar, dest, HI_ITEM(src), len)
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300962
Kalle Valo5e3dd152013-06-12 20:52:10 +0300963static int ath10k_pci_diag_write_mem(struct ath10k *ar, u32 address,
964 const void *data, int nbytes)
965{
966 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
967 int ret = 0;
968 u32 buf;
969 unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
970 unsigned int id;
971 unsigned int flags;
Michal Kazior2aa39112013-08-27 13:08:02 +0200972 struct ath10k_ce_pipe *ce_diag;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300973 void *data_buf = NULL;
974 u32 ce_data; /* Host buffer address in CE space */
975 dma_addr_t ce_data_base = 0;
976 int i;
977
Kalle Valoeef25402014-09-24 14:16:52 +0300978 spin_lock_bh(&ar_pci->ce_lock);
979
Kalle Valo5e3dd152013-06-12 20:52:10 +0300980 ce_diag = ar_pci->ce_diag;
981
982 /*
983 * Allocate a temporary bounce buffer to hold caller's data
984 * to be DMA'ed to Target. This guarantees
985 * 1) 4-byte alignment
986 * 2) Buffer in DMA-able space
987 */
988 orig_nbytes = nbytes;
Michal Kazior68c03242014-03-28 10:02:35 +0200989 data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
990 orig_nbytes,
991 &ce_data_base,
992 GFP_ATOMIC);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300993 if (!data_buf) {
994 ret = -ENOMEM;
995 goto done;
996 }
997
998 /* Copy caller's data to allocated DMA buf */
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300999 memcpy(data_buf, data, orig_nbytes);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001000
1001 /*
1002 * The address supplied by the caller is in the
1003 * Target CPU virtual address space.
1004 *
1005 * In order to use this address with the diagnostic CE,
1006 * convert it from
1007 * Target CPU virtual address space
1008 * to
1009 * CE address space
1010 */
Vasanthakumar Thiagarajan418ca592015-06-18 12:31:05 +05301011 address = ath10k_pci_targ_cpu_to_ce_addr(ar, address);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001012
1013 remaining_bytes = orig_nbytes;
1014 ce_data = ce_data_base;
1015 while (remaining_bytes) {
1016 /* FIXME: check cast */
1017 nbytes = min_t(int, remaining_bytes, DIAG_TRANSFER_LIMIT);
1018
1019 /* Set up to receive directly into Target(!) address */
Kalle Valoeef25402014-09-24 14:16:52 +03001020 ret = __ath10k_ce_rx_post_buf(ce_diag, NULL, address);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001021 if (ret != 0)
1022 goto done;
1023
1024 /*
1025 * Request CE to send caller-supplied data that
1026 * was copied to bounce buffer to Target(!) address.
1027 */
Kalle Valoeef25402014-09-24 14:16:52 +03001028 ret = ath10k_ce_send_nolock(ce_diag, NULL, (u32)ce_data,
1029 nbytes, 0, 0);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001030 if (ret != 0)
1031 goto done;
1032
1033 i = 0;
Kalle Valoeef25402014-09-24 14:16:52 +03001034 while (ath10k_ce_completed_send_next_nolock(ce_diag, NULL, &buf,
1035 &completed_nbytes,
1036 &id) != 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001037 mdelay(1);
1038
1039 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
1040 ret = -EBUSY;
1041 goto done;
1042 }
1043 }
1044
1045 if (nbytes != completed_nbytes) {
1046 ret = -EIO;
1047 goto done;
1048 }
1049
1050 if (buf != ce_data) {
1051 ret = -EIO;
1052 goto done;
1053 }
1054
1055 i = 0;
Kalle Valoeef25402014-09-24 14:16:52 +03001056 while (ath10k_ce_completed_recv_next_nolock(ce_diag, NULL, &buf,
1057 &completed_nbytes,
1058 &id, &flags) != 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001059 mdelay(1);
1060
1061 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
1062 ret = -EBUSY;
1063 goto done;
1064 }
1065 }
1066
1067 if (nbytes != completed_nbytes) {
1068 ret = -EIO;
1069 goto done;
1070 }
1071
1072 if (buf != address) {
1073 ret = -EIO;
1074 goto done;
1075 }
1076
1077 remaining_bytes -= nbytes;
1078 address += nbytes;
1079 ce_data += nbytes;
1080 }
1081
1082done:
1083 if (data_buf) {
Michal Kazior68c03242014-03-28 10:02:35 +02001084 dma_free_coherent(ar->dev, orig_nbytes, data_buf,
1085 ce_data_base);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001086 }
1087
1088 if (ret != 0)
Michal Kazior7aa7a722014-08-25 12:09:38 +02001089 ath10k_warn(ar, "failed to write diag value at 0x%x: %d\n",
Kalle Valo50f87a62014-03-28 09:32:52 +02001090 address, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001091
Kalle Valoeef25402014-09-24 14:16:52 +03001092 spin_unlock_bh(&ar_pci->ce_lock);
1093
Kalle Valo5e3dd152013-06-12 20:52:10 +03001094 return ret;
1095}
1096
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001097static int ath10k_pci_diag_write32(struct ath10k *ar, u32 address, u32 value)
1098{
1099 __le32 val = __cpu_to_le32(value);
1100
1101 return ath10k_pci_diag_write_mem(ar, address, &val, sizeof(val));
1102}
1103
Kalle Valo5e3dd152013-06-12 20:52:10 +03001104/* Called by lower (CE) layer when a send to Target completes. */
Michal Kazior5440ce22013-09-03 15:09:58 +02001105static void ath10k_pci_ce_send_done(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001106{
1107 struct ath10k *ar = ce_state->ar;
1108 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2f5280d2014-02-27 18:50:05 +02001109 struct ath10k_hif_cb *cb = &ar_pci->msg_callbacks_current;
Michal Kazior1cb86d42014-11-27 11:09:38 +01001110 struct sk_buff_head list;
1111 struct sk_buff *skb;
Michal Kazior5440ce22013-09-03 15:09:58 +02001112 u32 ce_data;
1113 unsigned int nbytes;
1114 unsigned int transfer_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001115
Michal Kazior1cb86d42014-11-27 11:09:38 +01001116 __skb_queue_head_init(&list);
1117 while (ath10k_ce_completed_send_next(ce_state, (void **)&skb, &ce_data,
1118 &nbytes, &transfer_id) == 0) {
Michal Kaziora16942e2014-02-27 18:50:04 +02001119 /* no need to call tx completion for NULL pointers */
Michal Kazior1cb86d42014-11-27 11:09:38 +01001120 if (skb == NULL)
Michal Kazior726346f2014-02-27 18:50:04 +02001121 continue;
1122
Michal Kazior1cb86d42014-11-27 11:09:38 +01001123 __skb_queue_tail(&list, skb);
Michal Kazior5440ce22013-09-03 15:09:58 +02001124 }
Michal Kazior1cb86d42014-11-27 11:09:38 +01001125
1126 while ((skb = __skb_dequeue(&list)))
1127 cb->tx_completion(ar, skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001128}
1129
1130/* Called by lower (CE) layer when data is received from the Target. */
Michal Kazior5440ce22013-09-03 15:09:58 +02001131static void ath10k_pci_ce_recv_data(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001132{
1133 struct ath10k *ar = ce_state->ar;
1134 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +02001135 struct ath10k_pci_pipe *pipe_info = &ar_pci->pipe_info[ce_state->id];
Michal Kazior2f5280d2014-02-27 18:50:05 +02001136 struct ath10k_hif_cb *cb = &ar_pci->msg_callbacks_current;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001137 struct sk_buff *skb;
Michal Kazior1cb86d42014-11-27 11:09:38 +01001138 struct sk_buff_head list;
Michal Kazior5440ce22013-09-03 15:09:58 +02001139 void *transfer_context;
1140 u32 ce_data;
Michal Kazior2f5280d2014-02-27 18:50:05 +02001141 unsigned int nbytes, max_nbytes;
Michal Kazior5440ce22013-09-03 15:09:58 +02001142 unsigned int transfer_id;
1143 unsigned int flags;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001144
Michal Kazior1cb86d42014-11-27 11:09:38 +01001145 __skb_queue_head_init(&list);
Michal Kazior5440ce22013-09-03 15:09:58 +02001146 while (ath10k_ce_completed_recv_next(ce_state, &transfer_context,
1147 &ce_data, &nbytes, &transfer_id,
1148 &flags) == 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001149 skb = transfer_context;
Michal Kazior2f5280d2014-02-27 18:50:05 +02001150 max_nbytes = skb->len + skb_tailroom(skb);
Michal Kazior8582bf32015-01-24 12:14:47 +02001151 dma_unmap_single(ar->dev, ATH10K_SKB_RXCB(skb)->paddr,
Michal Kazior2f5280d2014-02-27 18:50:05 +02001152 max_nbytes, DMA_FROM_DEVICE);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001153
Michal Kazior2f5280d2014-02-27 18:50:05 +02001154 if (unlikely(max_nbytes < nbytes)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001155 ath10k_warn(ar, "rxed more than expected (nbytes %d, max %d)",
Michal Kazior2f5280d2014-02-27 18:50:05 +02001156 nbytes, max_nbytes);
1157 dev_kfree_skb_any(skb);
1158 continue;
1159 }
1160
1161 skb_put(skb, nbytes);
Michal Kazior1cb86d42014-11-27 11:09:38 +01001162 __skb_queue_tail(&list, skb);
1163 }
Michal Kaziora360e542014-09-23 10:22:54 +02001164
Michal Kazior1cb86d42014-11-27 11:09:38 +01001165 while ((skb = __skb_dequeue(&list))) {
Michal Kaziora360e542014-09-23 10:22:54 +02001166 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci rx ce pipe %d len %d\n",
1167 ce_state->id, skb->len);
1168 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci rx: ",
1169 skb->data, skb->len);
1170
Michal Kazior5f07ea42014-11-27 11:09:36 +01001171 cb->rx_completion(ar, skb);
Michal Kazior2f5280d2014-02-27 18:50:05 +02001172 }
Michal Kaziorc29a3802014-07-21 21:03:10 +03001173
Michal Kazior728f95e2014-08-22 14:33:14 +02001174 ath10k_pci_rx_post_pipe(pipe_info);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001175}
1176
Michal Kazior726346f2014-02-27 18:50:04 +02001177static int ath10k_pci_hif_tx_sg(struct ath10k *ar, u8 pipe_id,
1178 struct ath10k_hif_sg_item *items, int n_items)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001179{
Kalle Valo5e3dd152013-06-12 20:52:10 +03001180 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior726346f2014-02-27 18:50:04 +02001181 struct ath10k_pci_pipe *pci_pipe = &ar_pci->pipe_info[pipe_id];
1182 struct ath10k_ce_pipe *ce_pipe = pci_pipe->ce_hdl;
1183 struct ath10k_ce_ring *src_ring = ce_pipe->src_ring;
Michal Kazior7147a132014-05-26 12:02:58 +02001184 unsigned int nentries_mask;
1185 unsigned int sw_index;
1186 unsigned int write_index;
Michal Kazior08b8aa02014-05-26 12:02:59 +02001187 int err, i = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001188
Michal Kazior726346f2014-02-27 18:50:04 +02001189 spin_lock_bh(&ar_pci->ce_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001190
Michal Kazior7147a132014-05-26 12:02:58 +02001191 nentries_mask = src_ring->nentries_mask;
1192 sw_index = src_ring->sw_index;
1193 write_index = src_ring->write_index;
1194
Michal Kazior726346f2014-02-27 18:50:04 +02001195 if (unlikely(CE_RING_DELTA(nentries_mask,
1196 write_index, sw_index - 1) < n_items)) {
1197 err = -ENOBUFS;
Michal Kazior08b8aa02014-05-26 12:02:59 +02001198 goto err;
Michal Kazior726346f2014-02-27 18:50:04 +02001199 }
1200
1201 for (i = 0; i < n_items - 1; i++) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001202 ath10k_dbg(ar, ATH10K_DBG_PCI,
Michal Kazior726346f2014-02-27 18:50:04 +02001203 "pci tx item %d paddr 0x%08x len %d n_items %d\n",
1204 i, items[i].paddr, items[i].len, n_items);
Michal Kazior7aa7a722014-08-25 12:09:38 +02001205 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci tx data: ",
Michal Kazior726346f2014-02-27 18:50:04 +02001206 items[i].vaddr, items[i].len);
1207
1208 err = ath10k_ce_send_nolock(ce_pipe,
1209 items[i].transfer_context,
1210 items[i].paddr,
1211 items[i].len,
1212 items[i].transfer_id,
1213 CE_SEND_FLAG_GATHER);
1214 if (err)
Michal Kazior08b8aa02014-05-26 12:02:59 +02001215 goto err;
Michal Kazior726346f2014-02-27 18:50:04 +02001216 }
1217
1218 /* `i` is equal to `n_items -1` after for() */
Kalle Valo5e3dd152013-06-12 20:52:10 +03001219
Michal Kazior7aa7a722014-08-25 12:09:38 +02001220 ath10k_dbg(ar, ATH10K_DBG_PCI,
Michal Kazior726346f2014-02-27 18:50:04 +02001221 "pci tx item %d paddr 0x%08x len %d n_items %d\n",
1222 i, items[i].paddr, items[i].len, n_items);
Michal Kazior7aa7a722014-08-25 12:09:38 +02001223 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci tx data: ",
Michal Kazior726346f2014-02-27 18:50:04 +02001224 items[i].vaddr, items[i].len);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001225
Michal Kazior726346f2014-02-27 18:50:04 +02001226 err = ath10k_ce_send_nolock(ce_pipe,
1227 items[i].transfer_context,
1228 items[i].paddr,
1229 items[i].len,
1230 items[i].transfer_id,
1231 0);
1232 if (err)
Michal Kazior08b8aa02014-05-26 12:02:59 +02001233 goto err;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001234
Michal Kazior08b8aa02014-05-26 12:02:59 +02001235 spin_unlock_bh(&ar_pci->ce_lock);
1236 return 0;
1237
1238err:
1239 for (; i > 0; i--)
1240 __ath10k_ce_send_revert(ce_pipe);
1241
Michal Kazior726346f2014-02-27 18:50:04 +02001242 spin_unlock_bh(&ar_pci->ce_lock);
1243 return err;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001244}
1245
Kalle Valoeef25402014-09-24 14:16:52 +03001246static int ath10k_pci_hif_diag_read(struct ath10k *ar, u32 address, void *buf,
1247 size_t buf_len)
1248{
1249 return ath10k_pci_diag_read_mem(ar, address, buf, buf_len);
1250}
1251
Kalle Valo5e3dd152013-06-12 20:52:10 +03001252static u16 ath10k_pci_hif_get_free_queue_number(struct ath10k *ar, u8 pipe)
1253{
1254 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo50f87a62014-03-28 09:32:52 +02001255
Michal Kazior7aa7a722014-08-25 12:09:38 +02001256 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif get free queue number\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001257
Michal Kazior3efcb3b2013-10-02 11:03:41 +02001258 return ath10k_ce_num_free_src_entries(ar_pci->pipe_info[pipe].ce_hdl);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001259}
1260
Ben Greear384914b2014-08-25 08:37:32 +03001261static void ath10k_pci_dump_registers(struct ath10k *ar,
1262 struct ath10k_fw_crash_data *crash_data)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001263{
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001264 __le32 reg_dump_values[REG_DUMP_COUNT_QCA988X] = {};
1265 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001266
Ben Greear384914b2014-08-25 08:37:32 +03001267 lockdep_assert_held(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001268
Kalle Valo3d29a3e2014-08-25 08:37:26 +03001269 ret = ath10k_pci_diag_read_hi(ar, &reg_dump_values[0],
1270 hi_failure_state,
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001271 REG_DUMP_COUNT_QCA988X * sizeof(__le32));
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001272 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001273 ath10k_err(ar, "failed to read firmware dump area: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001274 return;
1275 }
1276
1277 BUILD_BUG_ON(REG_DUMP_COUNT_QCA988X % 4);
1278
Michal Kazior7aa7a722014-08-25 12:09:38 +02001279 ath10k_err(ar, "firmware register dump:\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001280 for (i = 0; i < REG_DUMP_COUNT_QCA988X; i += 4)
Michal Kazior7aa7a722014-08-25 12:09:38 +02001281 ath10k_err(ar, "[%02d]: 0x%08X 0x%08X 0x%08X 0x%08X\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001282 i,
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001283 __le32_to_cpu(reg_dump_values[i]),
1284 __le32_to_cpu(reg_dump_values[i + 1]),
1285 __le32_to_cpu(reg_dump_values[i + 2]),
1286 __le32_to_cpu(reg_dump_values[i + 3]));
Michal Kazioraffd3212013-07-16 09:54:35 +02001287
Michal Kazior1bbb1192014-08-25 12:13:14 +02001288 if (!crash_data)
1289 return;
1290
Ben Greear384914b2014-08-25 08:37:32 +03001291 for (i = 0; i < REG_DUMP_COUNT_QCA988X; i++)
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001292 crash_data->registers[i] = reg_dump_values[i];
Ben Greear384914b2014-08-25 08:37:32 +03001293}
1294
Kalle Valo0e9848c2014-08-25 08:37:37 +03001295static void ath10k_pci_fw_crashed_dump(struct ath10k *ar)
Ben Greear384914b2014-08-25 08:37:32 +03001296{
1297 struct ath10k_fw_crash_data *crash_data;
1298 char uuid[50];
1299
1300 spin_lock_bh(&ar->data_lock);
1301
Ben Greearf51dbe72014-09-29 14:41:46 +03001302 ar->stats.fw_crash_counter++;
1303
Ben Greear384914b2014-08-25 08:37:32 +03001304 crash_data = ath10k_debug_get_new_fw_crash_data(ar);
1305
1306 if (crash_data)
1307 scnprintf(uuid, sizeof(uuid), "%pUl", &crash_data->uuid);
1308 else
1309 scnprintf(uuid, sizeof(uuid), "n/a");
1310
Michal Kazior7aa7a722014-08-25 12:09:38 +02001311 ath10k_err(ar, "firmware crashed! (uuid %s)\n", uuid);
Kalle Valo8a0c7972014-08-25 08:37:45 +03001312 ath10k_print_driver_info(ar);
Ben Greear384914b2014-08-25 08:37:32 +03001313 ath10k_pci_dump_registers(ar, crash_data);
1314
Ben Greear384914b2014-08-25 08:37:32 +03001315 spin_unlock_bh(&ar->data_lock);
Michal Kazioraffd3212013-07-16 09:54:35 +02001316
Michal Kazior5e90de82013-10-16 16:46:05 +03001317 queue_work(ar->workqueue, &ar->restart_work);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001318}
1319
1320static void ath10k_pci_hif_send_complete_check(struct ath10k *ar, u8 pipe,
1321 int force)
1322{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001323 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif send complete check\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001324
Kalle Valo5e3dd152013-06-12 20:52:10 +03001325 if (!force) {
1326 int resources;
1327 /*
1328 * Decide whether to actually poll for completions, or just
1329 * wait for a later chance.
1330 * If there seem to be plenty of resources left, then just wait
1331 * since checking involves reading a CE register, which is a
1332 * relatively expensive operation.
1333 */
1334 resources = ath10k_pci_hif_get_free_queue_number(ar, pipe);
1335
1336 /*
1337 * If at least 50% of the total resources are still available,
1338 * don't bother checking again yet.
1339 */
1340 if (resources > (host_ce_config_wlan[pipe].src_nentries >> 1))
1341 return;
1342 }
1343 ath10k_ce_per_engine_service(ar, pipe);
1344}
1345
Michal Kaziore799bbf2013-07-05 16:15:12 +03001346static void ath10k_pci_hif_set_callbacks(struct ath10k *ar,
1347 struct ath10k_hif_cb *callbacks)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001348{
1349 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1350
Michal Kazior7aa7a722014-08-25 12:09:38 +02001351 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif set callbacks\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001352
1353 memcpy(&ar_pci->msg_callbacks_current, callbacks,
1354 sizeof(ar_pci->msg_callbacks_current));
1355}
1356
Michal Kazior96a9d0d2013-11-08 08:01:25 +01001357static void ath10k_pci_kill_tasklet(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001358{
1359 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001360 int i;
1361
Kalle Valo5e3dd152013-06-12 20:52:10 +03001362 tasklet_kill(&ar_pci->intr_tq);
Michal Kazior103d4f52013-11-08 08:01:24 +01001363 tasklet_kill(&ar_pci->msi_fw_err);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001364
1365 for (i = 0; i < CE_COUNT; i++)
1366 tasklet_kill(&ar_pci->pipe_info[i].intr);
Michal Kazior728f95e2014-08-22 14:33:14 +02001367
1368 del_timer_sync(&ar_pci->rx_post_retry);
Michal Kazior96a9d0d2013-11-08 08:01:25 +01001369}
1370
Kalle Valo5e3dd152013-06-12 20:52:10 +03001371static int ath10k_pci_hif_map_service_to_pipe(struct ath10k *ar,
1372 u16 service_id, u8 *ul_pipe,
1373 u8 *dl_pipe, int *ul_is_polled,
1374 int *dl_is_polled)
1375{
Michal Kazior7c6aa252014-08-26 19:14:03 +03001376 const struct service_to_pipe *entry;
1377 bool ul_set = false, dl_set = false;
1378 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001379
Michal Kazior7aa7a722014-08-25 12:09:38 +02001380 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif map service\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001381
Kalle Valo5e3dd152013-06-12 20:52:10 +03001382 /* polling for received messages not supported */
1383 *dl_is_polled = 0;
1384
Michal Kazior7c6aa252014-08-26 19:14:03 +03001385 for (i = 0; i < ARRAY_SIZE(target_service_to_ce_map_wlan); i++) {
1386 entry = &target_service_to_ce_map_wlan[i];
Kalle Valo5e3dd152013-06-12 20:52:10 +03001387
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001388 if (__le32_to_cpu(entry->service_id) != service_id)
Michal Kazior7c6aa252014-08-26 19:14:03 +03001389 continue;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001390
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001391 switch (__le32_to_cpu(entry->pipedir)) {
Michal Kazior7c6aa252014-08-26 19:14:03 +03001392 case PIPEDIR_NONE:
1393 break;
1394 case PIPEDIR_IN:
1395 WARN_ON(dl_set);
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001396 *dl_pipe = __le32_to_cpu(entry->pipenum);
Michal Kazior7c6aa252014-08-26 19:14:03 +03001397 dl_set = true;
1398 break;
1399 case PIPEDIR_OUT:
1400 WARN_ON(ul_set);
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001401 *ul_pipe = __le32_to_cpu(entry->pipenum);
Michal Kazior7c6aa252014-08-26 19:14:03 +03001402 ul_set = true;
1403 break;
1404 case PIPEDIR_INOUT:
1405 WARN_ON(dl_set);
1406 WARN_ON(ul_set);
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001407 *dl_pipe = __le32_to_cpu(entry->pipenum);
1408 *ul_pipe = __le32_to_cpu(entry->pipenum);
Michal Kazior7c6aa252014-08-26 19:14:03 +03001409 dl_set = true;
1410 ul_set = true;
1411 break;
1412 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001413 }
Michal Kazior7c6aa252014-08-26 19:14:03 +03001414
1415 if (WARN_ON(!ul_set || !dl_set))
1416 return -ENOENT;
1417
Kalle Valo5e3dd152013-06-12 20:52:10 +03001418 *ul_is_polled =
1419 (host_ce_config_wlan[*ul_pipe].flags & CE_ATTR_DIS_INTR) != 0;
1420
Michal Kazior7c6aa252014-08-26 19:14:03 +03001421 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001422}
1423
1424static void ath10k_pci_hif_get_default_pipe(struct ath10k *ar,
Kalle Valo5b07e072014-09-14 12:50:06 +03001425 u8 *ul_pipe, u8 *dl_pipe)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001426{
1427 int ul_is_polled, dl_is_polled;
1428
Michal Kazior7aa7a722014-08-25 12:09:38 +02001429 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif get default pipe\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001430
Kalle Valo5e3dd152013-06-12 20:52:10 +03001431 (void)ath10k_pci_hif_map_service_to_pipe(ar,
1432 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1433 ul_pipe,
1434 dl_pipe,
1435 &ul_is_polled,
1436 &dl_is_polled);
1437}
1438
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001439static void ath10k_pci_irq_msi_fw_mask(struct ath10k *ar)
1440{
1441 u32 val;
1442
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +05301443 switch (ar->hw_rev) {
1444 case ATH10K_HW_QCA988X:
1445 case ATH10K_HW_QCA6174:
1446 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1447 CORE_CTRL_ADDRESS);
1448 val &= ~CORE_CTRL_PCIE_REG_31_MASK;
1449 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
1450 CORE_CTRL_ADDRESS, val);
1451 break;
1452 case ATH10K_HW_QCA99X0:
1453 /* TODO: Find appropriate register configuration for QCA99X0
1454 * to mask irq/MSI.
1455 */
1456 break;
1457 }
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001458}
1459
1460static void ath10k_pci_irq_msi_fw_unmask(struct ath10k *ar)
1461{
1462 u32 val;
1463
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +05301464 switch (ar->hw_rev) {
1465 case ATH10K_HW_QCA988X:
1466 case ATH10K_HW_QCA6174:
1467 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1468 CORE_CTRL_ADDRESS);
1469 val |= CORE_CTRL_PCIE_REG_31_MASK;
1470 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
1471 CORE_CTRL_ADDRESS, val);
1472 break;
1473 case ATH10K_HW_QCA99X0:
1474 /* TODO: Find appropriate register configuration for QCA99X0
1475 * to unmask irq/MSI.
1476 */
1477 break;
1478 }
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001479}
1480
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001481static void ath10k_pci_irq_disable(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001482{
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001483 ath10k_ce_disable_interrupts(ar);
1484 ath10k_pci_disable_and_clear_legacy_irq(ar);
1485 ath10k_pci_irq_msi_fw_mask(ar);
1486}
1487
1488static void ath10k_pci_irq_sync(struct ath10k *ar)
1489{
Kalle Valo5e3dd152013-06-12 20:52:10 +03001490 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001491 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001492
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001493 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
1494 synchronize_irq(ar_pci->pdev->irq + i);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001495}
1496
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001497static void ath10k_pci_irq_enable(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001498{
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001499 ath10k_ce_enable_interrupts(ar);
Michal Kaziore75db4e2014-08-28 22:14:16 +03001500 ath10k_pci_enable_legacy_irq(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001501 ath10k_pci_irq_msi_fw_unmask(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001502}
1503
1504static int ath10k_pci_hif_start(struct ath10k *ar)
1505{
Janusz Dziedzic76d870e2015-05-18 09:38:16 +00001506 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior7aa7a722014-08-25 12:09:38 +02001507 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif start\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001508
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001509 ath10k_pci_irq_enable(ar);
Michal Kazior728f95e2014-08-22 14:33:14 +02001510 ath10k_pci_rx_post(ar);
Kalle Valo50f87a62014-03-28 09:32:52 +02001511
Janusz Dziedzic76d870e2015-05-18 09:38:16 +00001512 pcie_capability_write_word(ar_pci->pdev, PCI_EXP_LNKCTL,
1513 ar_pci->link_ctl);
1514
Kalle Valo5e3dd152013-06-12 20:52:10 +03001515 return 0;
1516}
1517
Michal Kazior099ac7c2014-10-28 10:32:05 +01001518static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe *pci_pipe)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001519{
1520 struct ath10k *ar;
Michal Kazior099ac7c2014-10-28 10:32:05 +01001521 struct ath10k_ce_pipe *ce_pipe;
1522 struct ath10k_ce_ring *ce_ring;
1523 struct sk_buff *skb;
1524 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001525
Michal Kazior099ac7c2014-10-28 10:32:05 +01001526 ar = pci_pipe->hif_ce_state;
1527 ce_pipe = pci_pipe->ce_hdl;
1528 ce_ring = ce_pipe->dest_ring;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001529
Michal Kazior099ac7c2014-10-28 10:32:05 +01001530 if (!ce_ring)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001531 return;
1532
Michal Kazior099ac7c2014-10-28 10:32:05 +01001533 if (!pci_pipe->buf_sz)
1534 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001535
Michal Kazior099ac7c2014-10-28 10:32:05 +01001536 for (i = 0; i < ce_ring->nentries; i++) {
1537 skb = ce_ring->per_transfer_context[i];
1538 if (!skb)
1539 continue;
1540
1541 ce_ring->per_transfer_context[i] = NULL;
1542
Michal Kazior8582bf32015-01-24 12:14:47 +02001543 dma_unmap_single(ar->dev, ATH10K_SKB_RXCB(skb)->paddr,
Michal Kazior099ac7c2014-10-28 10:32:05 +01001544 skb->len + skb_tailroom(skb),
Kalle Valo5e3dd152013-06-12 20:52:10 +03001545 DMA_FROM_DEVICE);
Michal Kazior099ac7c2014-10-28 10:32:05 +01001546 dev_kfree_skb_any(skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001547 }
1548}
1549
Michal Kazior099ac7c2014-10-28 10:32:05 +01001550static void ath10k_pci_tx_pipe_cleanup(struct ath10k_pci_pipe *pci_pipe)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001551{
1552 struct ath10k *ar;
1553 struct ath10k_pci *ar_pci;
Michal Kazior099ac7c2014-10-28 10:32:05 +01001554 struct ath10k_ce_pipe *ce_pipe;
1555 struct ath10k_ce_ring *ce_ring;
1556 struct ce_desc *ce_desc;
1557 struct sk_buff *skb;
Michal Kazior099ac7c2014-10-28 10:32:05 +01001558 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001559
Michal Kazior099ac7c2014-10-28 10:32:05 +01001560 ar = pci_pipe->hif_ce_state;
1561 ar_pci = ath10k_pci_priv(ar);
1562 ce_pipe = pci_pipe->ce_hdl;
1563 ce_ring = ce_pipe->src_ring;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001564
Michal Kazior099ac7c2014-10-28 10:32:05 +01001565 if (!ce_ring)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001566 return;
1567
Michal Kazior099ac7c2014-10-28 10:32:05 +01001568 if (!pci_pipe->buf_sz)
1569 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001570
Michal Kazior099ac7c2014-10-28 10:32:05 +01001571 ce_desc = ce_ring->shadow_base;
1572 if (WARN_ON(!ce_desc))
1573 return;
1574
1575 for (i = 0; i < ce_ring->nentries; i++) {
1576 skb = ce_ring->per_transfer_context[i];
1577 if (!skb)
Michal Kazior2415fc12013-11-08 08:01:32 +01001578 continue;
Michal Kazior2415fc12013-11-08 08:01:32 +01001579
Michal Kazior099ac7c2014-10-28 10:32:05 +01001580 ce_ring->per_transfer_context[i] = NULL;
Michal Kazior099ac7c2014-10-28 10:32:05 +01001581
Michal Kaziord84a5122014-11-27 11:09:37 +01001582 ar_pci->msg_callbacks_current.tx_completion(ar, skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001583 }
1584}
1585
1586/*
1587 * Cleanup residual buffers for device shutdown:
1588 * buffers that were enqueued for receive
1589 * buffers that were to be sent
1590 * Note: Buffers that had completed but which were
1591 * not yet processed are on a completion queue. They
1592 * are handled when the completion thread shuts down.
1593 */
1594static void ath10k_pci_buffer_cleanup(struct ath10k *ar)
1595{
1596 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1597 int pipe_num;
1598
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001599 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Michal Kazior87263e52013-08-27 13:08:01 +02001600 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001601
1602 pipe_info = &ar_pci->pipe_info[pipe_num];
1603 ath10k_pci_rx_pipe_cleanup(pipe_info);
1604 ath10k_pci_tx_pipe_cleanup(pipe_info);
1605 }
1606}
1607
1608static void ath10k_pci_ce_deinit(struct ath10k *ar)
1609{
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001610 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001611
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001612 for (i = 0; i < CE_COUNT; i++)
1613 ath10k_ce_deinit_pipe(ar, i);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001614}
1615
Michal Kazior728f95e2014-08-22 14:33:14 +02001616static void ath10k_pci_flush(struct ath10k *ar)
1617{
1618 ath10k_pci_kill_tasklet(ar);
1619 ath10k_pci_buffer_cleanup(ar);
1620}
1621
Kalle Valo5e3dd152013-06-12 20:52:10 +03001622static void ath10k_pci_hif_stop(struct ath10k *ar)
1623{
Michal Kazior77258d42015-05-18 09:38:18 +00001624 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1625 unsigned long flags;
1626
Michal Kazior7aa7a722014-08-25 12:09:38 +02001627 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif stop\n");
Michal Kazior32270b62013-08-02 09:15:47 +02001628
Michal Kazior10d23db2014-08-22 14:33:15 +02001629 /* Most likely the device has HTT Rx ring configured. The only way to
1630 * prevent the device from accessing (and possible corrupting) host
1631 * memory is to reset the chip now.
Michal Kaziore75db4e2014-08-28 22:14:16 +03001632 *
1633 * There's also no known way of masking MSI interrupts on the device.
1634 * For ranged MSI the CE-related interrupts can be masked. However
1635 * regardless how many MSI interrupts are assigned the first one
1636 * is always used for firmware indications (crashes) and cannot be
1637 * masked. To prevent the device from asserting the interrupt reset it
1638 * before proceeding with cleanup.
Michal Kazior10d23db2014-08-22 14:33:15 +02001639 */
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +05301640 ath10k_pci_safe_chip_reset(ar);
Michal Kaziore75db4e2014-08-28 22:14:16 +03001641
1642 ath10k_pci_irq_disable(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001643 ath10k_pci_irq_sync(ar);
Michal Kaziore75db4e2014-08-28 22:14:16 +03001644 ath10k_pci_flush(ar);
Michal Kazior77258d42015-05-18 09:38:18 +00001645
1646 spin_lock_irqsave(&ar_pci->ps_lock, flags);
1647 WARN_ON(ar_pci->ps_wake_refcount > 0);
1648 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001649}
1650
1651static int ath10k_pci_hif_exchange_bmi_msg(struct ath10k *ar,
1652 void *req, u32 req_len,
1653 void *resp, u32 *resp_len)
1654{
1655 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2aa39112013-08-27 13:08:02 +02001656 struct ath10k_pci_pipe *pci_tx = &ar_pci->pipe_info[BMI_CE_NUM_TO_TARG];
1657 struct ath10k_pci_pipe *pci_rx = &ar_pci->pipe_info[BMI_CE_NUM_TO_HOST];
1658 struct ath10k_ce_pipe *ce_tx = pci_tx->ce_hdl;
1659 struct ath10k_ce_pipe *ce_rx = pci_rx->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001660 dma_addr_t req_paddr = 0;
1661 dma_addr_t resp_paddr = 0;
1662 struct bmi_xfer xfer = {};
1663 void *treq, *tresp = NULL;
1664 int ret = 0;
1665
Michal Kazior85622cd2013-11-25 14:06:22 +01001666 might_sleep();
1667
Kalle Valo5e3dd152013-06-12 20:52:10 +03001668 if (resp && !resp_len)
1669 return -EINVAL;
1670
1671 if (resp && resp_len && *resp_len == 0)
1672 return -EINVAL;
1673
1674 treq = kmemdup(req, req_len, GFP_KERNEL);
1675 if (!treq)
1676 return -ENOMEM;
1677
1678 req_paddr = dma_map_single(ar->dev, treq, req_len, DMA_TO_DEVICE);
1679 ret = dma_mapping_error(ar->dev, req_paddr);
Michal Kazior5e55e3c2015-08-19 13:10:43 +02001680 if (ret) {
1681 ret = -EIO;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001682 goto err_dma;
Michal Kazior5e55e3c2015-08-19 13:10:43 +02001683 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001684
1685 if (resp && resp_len) {
1686 tresp = kzalloc(*resp_len, GFP_KERNEL);
1687 if (!tresp) {
1688 ret = -ENOMEM;
1689 goto err_req;
1690 }
1691
1692 resp_paddr = dma_map_single(ar->dev, tresp, *resp_len,
1693 DMA_FROM_DEVICE);
1694 ret = dma_mapping_error(ar->dev, resp_paddr);
Michal Kazior5e55e3c2015-08-19 13:10:43 +02001695 if (ret) {
1696 ret = EIO;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001697 goto err_req;
Michal Kazior5e55e3c2015-08-19 13:10:43 +02001698 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001699
1700 xfer.wait_for_resp = true;
1701 xfer.resp_len = 0;
1702
Michal Kazior728f95e2014-08-22 14:33:14 +02001703 ath10k_ce_rx_post_buf(ce_rx, &xfer, resp_paddr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001704 }
1705
Kalle Valo5e3dd152013-06-12 20:52:10 +03001706 ret = ath10k_ce_send(ce_tx, &xfer, req_paddr, req_len, -1, 0);
1707 if (ret)
1708 goto err_resp;
1709
Michal Kazior85622cd2013-11-25 14:06:22 +01001710 ret = ath10k_pci_bmi_wait(ce_tx, ce_rx, &xfer);
1711 if (ret) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001712 u32 unused_buffer;
1713 unsigned int unused_nbytes;
1714 unsigned int unused_id;
1715
Kalle Valo5e3dd152013-06-12 20:52:10 +03001716 ath10k_ce_cancel_send_next(ce_tx, NULL, &unused_buffer,
1717 &unused_nbytes, &unused_id);
1718 } else {
1719 /* non-zero means we did not time out */
1720 ret = 0;
1721 }
1722
1723err_resp:
1724 if (resp) {
1725 u32 unused_buffer;
1726
1727 ath10k_ce_revoke_recv_next(ce_rx, NULL, &unused_buffer);
1728 dma_unmap_single(ar->dev, resp_paddr,
1729 *resp_len, DMA_FROM_DEVICE);
1730 }
1731err_req:
1732 dma_unmap_single(ar->dev, req_paddr, req_len, DMA_TO_DEVICE);
1733
1734 if (ret == 0 && resp_len) {
1735 *resp_len = min(*resp_len, xfer.resp_len);
1736 memcpy(resp, tresp, xfer.resp_len);
1737 }
1738err_dma:
1739 kfree(treq);
1740 kfree(tresp);
1741
1742 return ret;
1743}
1744
Michal Kazior5440ce22013-09-03 15:09:58 +02001745static void ath10k_pci_bmi_send_done(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001746{
Michal Kazior5440ce22013-09-03 15:09:58 +02001747 struct bmi_xfer *xfer;
1748 u32 ce_data;
1749 unsigned int nbytes;
1750 unsigned int transfer_id;
1751
1752 if (ath10k_ce_completed_send_next(ce_state, (void **)&xfer, &ce_data,
1753 &nbytes, &transfer_id))
1754 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001755
Michal Kazior2374b182014-07-14 16:25:25 +03001756 xfer->tx_done = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001757}
1758
Michal Kazior5440ce22013-09-03 15:09:58 +02001759static void ath10k_pci_bmi_recv_data(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001760{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001761 struct ath10k *ar = ce_state->ar;
Michal Kazior5440ce22013-09-03 15:09:58 +02001762 struct bmi_xfer *xfer;
1763 u32 ce_data;
1764 unsigned int nbytes;
1765 unsigned int transfer_id;
1766 unsigned int flags;
1767
1768 if (ath10k_ce_completed_recv_next(ce_state, (void **)&xfer, &ce_data,
1769 &nbytes, &transfer_id, &flags))
1770 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001771
Michal Kazior04ed9df2014-10-28 10:34:36 +01001772 if (WARN_ON_ONCE(!xfer))
1773 return;
1774
Kalle Valo5e3dd152013-06-12 20:52:10 +03001775 if (!xfer->wait_for_resp) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001776 ath10k_warn(ar, "unexpected: BMI data received; ignoring\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001777 return;
1778 }
1779
1780 xfer->resp_len = nbytes;
Michal Kazior2374b182014-07-14 16:25:25 +03001781 xfer->rx_done = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001782}
1783
Michal Kazior85622cd2013-11-25 14:06:22 +01001784static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
1785 struct ath10k_ce_pipe *rx_pipe,
1786 struct bmi_xfer *xfer)
1787{
1788 unsigned long timeout = jiffies + BMI_COMMUNICATION_TIMEOUT_HZ;
1789
1790 while (time_before_eq(jiffies, timeout)) {
1791 ath10k_pci_bmi_send_done(tx_pipe);
1792 ath10k_pci_bmi_recv_data(rx_pipe);
1793
Michal Kazior2374b182014-07-14 16:25:25 +03001794 if (xfer->tx_done && (xfer->rx_done == xfer->wait_for_resp))
Michal Kazior85622cd2013-11-25 14:06:22 +01001795 return 0;
1796
1797 schedule();
1798 }
1799
1800 return -ETIMEDOUT;
1801}
1802
Kalle Valo5e3dd152013-06-12 20:52:10 +03001803/*
Kalle Valo5e3dd152013-06-12 20:52:10 +03001804 * Send an interrupt to the device to wake up the Target CPU
1805 * so it has an opportunity to notice any changed state.
1806 */
1807static int ath10k_pci_wake_target_cpu(struct ath10k *ar)
1808{
Michal Kazior9e264942014-09-02 11:00:21 +03001809 u32 addr, val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001810
Michal Kazior9e264942014-09-02 11:00:21 +03001811 addr = SOC_CORE_BASE_ADDRESS | CORE_CTRL_ADDRESS;
1812 val = ath10k_pci_read32(ar, addr);
1813 val |= CORE_CTRL_CPU_INTR_MASK;
1814 ath10k_pci_write32(ar, addr, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001815
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001816 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001817}
1818
Michal Kaziord63955b2015-01-24 12:14:49 +02001819static int ath10k_pci_get_num_banks(struct ath10k *ar)
1820{
1821 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1822
1823 switch (ar_pci->pdev->device) {
1824 case QCA988X_2_0_DEVICE_ID:
Vasanthakumar Thiagarajan8bd47022015-06-18 12:31:03 +05301825 case QCA99X0_2_0_DEVICE_ID:
Michal Kaziord63955b2015-01-24 12:14:49 +02001826 return 1;
Michal Kazior36582e52015-08-13 14:32:26 +02001827 case QCA6164_2_1_DEVICE_ID:
Michal Kaziord63955b2015-01-24 12:14:49 +02001828 case QCA6174_2_1_DEVICE_ID:
1829 switch (MS(ar->chip_id, SOC_CHIP_ID_REV)) {
1830 case QCA6174_HW_1_0_CHIP_ID_REV:
1831 case QCA6174_HW_1_1_CHIP_ID_REV:
Michal Kazior11a002e2015-04-20 09:20:41 +00001832 case QCA6174_HW_2_1_CHIP_ID_REV:
1833 case QCA6174_HW_2_2_CHIP_ID_REV:
Michal Kaziord63955b2015-01-24 12:14:49 +02001834 return 3;
1835 case QCA6174_HW_1_3_CHIP_ID_REV:
1836 return 2;
Michal Kaziord63955b2015-01-24 12:14:49 +02001837 case QCA6174_HW_3_0_CHIP_ID_REV:
1838 case QCA6174_HW_3_1_CHIP_ID_REV:
1839 case QCA6174_HW_3_2_CHIP_ID_REV:
1840 return 9;
1841 }
1842 break;
1843 }
1844
1845 ath10k_warn(ar, "unknown number of banks, assuming 1\n");
1846 return 1;
1847}
1848
Kalle Valo5e3dd152013-06-12 20:52:10 +03001849static int ath10k_pci_init_config(struct ath10k *ar)
1850{
1851 u32 interconnect_targ_addr;
1852 u32 pcie_state_targ_addr = 0;
1853 u32 pipe_cfg_targ_addr = 0;
1854 u32 svc_to_pipe_map = 0;
1855 u32 pcie_config_flags = 0;
1856 u32 ealloc_value;
1857 u32 ealloc_targ_addr;
1858 u32 flag2_value;
1859 u32 flag2_targ_addr;
1860 int ret = 0;
1861
1862 /* Download to Target the CE Config and the service-to-CE map */
1863 interconnect_targ_addr =
1864 host_interest_item_address(HI_ITEM(hi_interconnect_state));
1865
1866 /* Supply Target-side CE configuration */
Michal Kazior9e264942014-09-02 11:00:21 +03001867 ret = ath10k_pci_diag_read32(ar, interconnect_targ_addr,
1868 &pcie_state_targ_addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001869 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001870 ath10k_err(ar, "Failed to get pcie state addr: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001871 return ret;
1872 }
1873
1874 if (pcie_state_targ_addr == 0) {
1875 ret = -EIO;
Michal Kazior7aa7a722014-08-25 12:09:38 +02001876 ath10k_err(ar, "Invalid pcie state addr\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001877 return ret;
1878 }
1879
Michal Kazior9e264942014-09-02 11:00:21 +03001880 ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
Kalle Valo5e3dd152013-06-12 20:52:10 +03001881 offsetof(struct pcie_state,
Michal Kazior9e264942014-09-02 11:00:21 +03001882 pipe_cfg_addr)),
1883 &pipe_cfg_targ_addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001884 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001885 ath10k_err(ar, "Failed to get pipe cfg addr: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001886 return ret;
1887 }
1888
1889 if (pipe_cfg_targ_addr == 0) {
1890 ret = -EIO;
Michal Kazior7aa7a722014-08-25 12:09:38 +02001891 ath10k_err(ar, "Invalid pipe cfg addr\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001892 return ret;
1893 }
1894
1895 ret = ath10k_pci_diag_write_mem(ar, pipe_cfg_targ_addr,
Kalle Valo5b07e072014-09-14 12:50:06 +03001896 target_ce_config_wlan,
Vasanthakumar Thiagarajan050af062015-06-18 12:31:04 +05301897 sizeof(struct ce_pipe_config) *
1898 NUM_TARGET_CE_CONFIG_WLAN);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001899
1900 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001901 ath10k_err(ar, "Failed to write pipe cfg: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001902 return ret;
1903 }
1904
Michal Kazior9e264942014-09-02 11:00:21 +03001905 ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
Kalle Valo5e3dd152013-06-12 20:52:10 +03001906 offsetof(struct pcie_state,
Michal Kazior9e264942014-09-02 11:00:21 +03001907 svc_to_pipe_map)),
1908 &svc_to_pipe_map);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001909 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001910 ath10k_err(ar, "Failed to get svc/pipe map: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001911 return ret;
1912 }
1913
1914 if (svc_to_pipe_map == 0) {
1915 ret = -EIO;
Michal Kazior7aa7a722014-08-25 12:09:38 +02001916 ath10k_err(ar, "Invalid svc_to_pipe map\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001917 return ret;
1918 }
1919
1920 ret = ath10k_pci_diag_write_mem(ar, svc_to_pipe_map,
Kalle Valo5b07e072014-09-14 12:50:06 +03001921 target_service_to_ce_map_wlan,
1922 sizeof(target_service_to_ce_map_wlan));
Kalle Valo5e3dd152013-06-12 20:52:10 +03001923 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001924 ath10k_err(ar, "Failed to write svc/pipe map: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001925 return ret;
1926 }
1927
Michal Kazior9e264942014-09-02 11:00:21 +03001928 ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
Kalle Valo5e3dd152013-06-12 20:52:10 +03001929 offsetof(struct pcie_state,
Michal Kazior9e264942014-09-02 11:00:21 +03001930 config_flags)),
1931 &pcie_config_flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001932 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001933 ath10k_err(ar, "Failed to get pcie config_flags: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001934 return ret;
1935 }
1936
1937 pcie_config_flags &= ~PCIE_CONFIG_FLAG_ENABLE_L1;
1938
Michal Kazior9e264942014-09-02 11:00:21 +03001939 ret = ath10k_pci_diag_write32(ar, (pcie_state_targ_addr +
1940 offsetof(struct pcie_state,
1941 config_flags)),
1942 pcie_config_flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001943 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001944 ath10k_err(ar, "Failed to write pcie config_flags: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001945 return ret;
1946 }
1947
1948 /* configure early allocation */
1949 ealloc_targ_addr = host_interest_item_address(HI_ITEM(hi_early_alloc));
1950
Michal Kazior9e264942014-09-02 11:00:21 +03001951 ret = ath10k_pci_diag_read32(ar, ealloc_targ_addr, &ealloc_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001952 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001953 ath10k_err(ar, "Faile to get early alloc val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001954 return ret;
1955 }
1956
1957 /* first bank is switched to IRAM */
1958 ealloc_value |= ((HI_EARLY_ALLOC_MAGIC << HI_EARLY_ALLOC_MAGIC_SHIFT) &
1959 HI_EARLY_ALLOC_MAGIC_MASK);
Michal Kaziord63955b2015-01-24 12:14:49 +02001960 ealloc_value |= ((ath10k_pci_get_num_banks(ar) <<
1961 HI_EARLY_ALLOC_IRAM_BANKS_SHIFT) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03001962 HI_EARLY_ALLOC_IRAM_BANKS_MASK);
1963
Michal Kazior9e264942014-09-02 11:00:21 +03001964 ret = ath10k_pci_diag_write32(ar, ealloc_targ_addr, ealloc_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001965 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001966 ath10k_err(ar, "Failed to set early alloc val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001967 return ret;
1968 }
1969
1970 /* Tell Target to proceed with initialization */
1971 flag2_targ_addr = host_interest_item_address(HI_ITEM(hi_option_flag2));
1972
Michal Kazior9e264942014-09-02 11:00:21 +03001973 ret = ath10k_pci_diag_read32(ar, flag2_targ_addr, &flag2_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001974 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001975 ath10k_err(ar, "Failed to get option val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001976 return ret;
1977 }
1978
1979 flag2_value |= HI_OPTION_EARLY_CFG_DONE;
1980
Michal Kazior9e264942014-09-02 11:00:21 +03001981 ret = ath10k_pci_diag_write32(ar, flag2_targ_addr, flag2_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001982 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001983 ath10k_err(ar, "Failed to set option val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001984 return ret;
1985 }
1986
1987 return 0;
1988}
1989
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001990static int ath10k_pci_alloc_pipes(struct ath10k *ar)
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001991{
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001992 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1993 struct ath10k_pci_pipe *pipe;
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001994 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001995
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001996 for (i = 0; i < CE_COUNT; i++) {
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001997 pipe = &ar_pci->pipe_info[i];
1998 pipe->ce_hdl = &ar_pci->ce_states[i];
1999 pipe->pipe_num = i;
2000 pipe->hif_ce_state = ar;
2001
2002 ret = ath10k_ce_alloc_pipe(ar, i, &host_ce_config_wlan[i],
2003 ath10k_pci_ce_send_done,
2004 ath10k_pci_ce_recv_data);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002005 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002006 ath10k_err(ar, "failed to allocate copy engine pipe %d: %d\n",
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002007 i, ret);
2008 return ret;
2009 }
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002010
2011 /* Last CE is Diagnostic Window */
Vasanthakumar Thiagarajan050af062015-06-18 12:31:04 +05302012 if (i == CE_DIAG_PIPE) {
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002013 ar_pci->ce_diag = pipe->ce_hdl;
2014 continue;
2015 }
2016
2017 pipe->buf_sz = (size_t)(host_ce_config_wlan[i].src_sz_max);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002018 }
2019
2020 return 0;
2021}
2022
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002023static void ath10k_pci_free_pipes(struct ath10k *ar)
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002024{
2025 int i;
2026
2027 for (i = 0; i < CE_COUNT; i++)
2028 ath10k_ce_free_pipe(ar, i);
2029}
Kalle Valo5e3dd152013-06-12 20:52:10 +03002030
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002031static int ath10k_pci_init_pipes(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002032{
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002033 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002034
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002035 for (i = 0; i < CE_COUNT; i++) {
2036 ret = ath10k_ce_init_pipe(ar, i, &host_ce_config_wlan[i]);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002037 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002038 ath10k_err(ar, "failed to initialize copy engine pipe %d: %d\n",
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002039 i, ret);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002040 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002041 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002042 }
2043
Kalle Valo5e3dd152013-06-12 20:52:10 +03002044 return 0;
2045}
2046
Michal Kazior5c771e72014-08-22 14:23:34 +02002047static bool ath10k_pci_has_fw_crashed(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002048{
Michal Kazior5c771e72014-08-22 14:23:34 +02002049 return ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS) &
2050 FW_IND_EVENT_PENDING;
2051}
Kalle Valo5e3dd152013-06-12 20:52:10 +03002052
Michal Kazior5c771e72014-08-22 14:23:34 +02002053static void ath10k_pci_fw_crashed_clear(struct ath10k *ar)
2054{
2055 u32 val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002056
Michal Kazior5c771e72014-08-22 14:23:34 +02002057 val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
2058 val &= ~FW_IND_EVENT_PENDING;
2059 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002060}
2061
Michal Kaziorde013572014-05-14 16:56:16 +03002062/* this function effectively clears target memory controller assert line */
2063static void ath10k_pci_warm_reset_si0(struct ath10k *ar)
2064{
2065 u32 val;
2066
2067 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
2068 ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
2069 val | SOC_RESET_CONTROL_SI0_RST_MASK);
2070 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
2071
2072 msleep(10);
2073
2074 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
2075 ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
2076 val & ~SOC_RESET_CONTROL_SI0_RST_MASK);
2077 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
2078
2079 msleep(10);
2080}
2081
Michal Kazior61c16482014-10-28 10:32:06 +01002082static void ath10k_pci_warm_reset_cpu(struct ath10k *ar)
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002083{
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002084 u32 val;
2085
Kalle Valob39712c2014-03-28 09:32:46 +02002086 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, 0);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002087
Michal Kazior61c16482014-10-28 10:32:06 +01002088 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
2089 SOC_RESET_CONTROL_ADDRESS);
2090 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
2091 val | SOC_RESET_CONTROL_CPU_WARM_RST_MASK);
2092}
2093
2094static void ath10k_pci_warm_reset_ce(struct ath10k *ar)
2095{
2096 u32 val;
2097
2098 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
2099 SOC_RESET_CONTROL_ADDRESS);
2100
2101 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
2102 val | SOC_RESET_CONTROL_CE_RST_MASK);
2103 msleep(10);
2104 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
2105 val & ~SOC_RESET_CONTROL_CE_RST_MASK);
2106}
2107
2108static void ath10k_pci_warm_reset_clear_lf(struct ath10k *ar)
2109{
2110 u32 val;
2111
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002112 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
2113 SOC_LF_TIMER_CONTROL0_ADDRESS);
2114 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS +
2115 SOC_LF_TIMER_CONTROL0_ADDRESS,
2116 val & ~SOC_LF_TIMER_CONTROL0_ENABLE_MASK);
Michal Kazior61c16482014-10-28 10:32:06 +01002117}
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002118
Michal Kazior61c16482014-10-28 10:32:06 +01002119static int ath10k_pci_warm_reset(struct ath10k *ar)
2120{
2121 int ret;
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002122
Michal Kazior61c16482014-10-28 10:32:06 +01002123 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot warm reset\n");
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002124
Michal Kazior61c16482014-10-28 10:32:06 +01002125 spin_lock_bh(&ar->data_lock);
2126 ar->stats.fw_warm_reset_counter++;
2127 spin_unlock_bh(&ar->data_lock);
2128
2129 ath10k_pci_irq_disable(ar);
2130
2131 /* Make sure the target CPU is not doing anything dangerous, e.g. if it
2132 * were to access copy engine while host performs copy engine reset
2133 * then it is possible for the device to confuse pci-e controller to
2134 * the point of bringing host system to a complete stop (i.e. hang).
2135 */
Michal Kaziorde013572014-05-14 16:56:16 +03002136 ath10k_pci_warm_reset_si0(ar);
Michal Kazior61c16482014-10-28 10:32:06 +01002137 ath10k_pci_warm_reset_cpu(ar);
2138 ath10k_pci_init_pipes(ar);
2139 ath10k_pci_wait_for_target_init(ar);
Michal Kaziorde013572014-05-14 16:56:16 +03002140
Michal Kazior61c16482014-10-28 10:32:06 +01002141 ath10k_pci_warm_reset_clear_lf(ar);
2142 ath10k_pci_warm_reset_ce(ar);
2143 ath10k_pci_warm_reset_cpu(ar);
2144 ath10k_pci_init_pipes(ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002145
Michal Kazior61c16482014-10-28 10:32:06 +01002146 ret = ath10k_pci_wait_for_target_init(ar);
2147 if (ret) {
2148 ath10k_warn(ar, "failed to wait for target init: %d\n", ret);
2149 return ret;
2150 }
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002151
Michal Kazior7aa7a722014-08-25 12:09:38 +02002152 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot warm reset complete\n");
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002153
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002154 return 0;
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002155}
2156
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +05302157static int ath10k_pci_safe_chip_reset(struct ath10k *ar)
2158{
2159 if (QCA_REV_988X(ar) || QCA_REV_6174(ar)) {
2160 return ath10k_pci_warm_reset(ar);
2161 } else if (QCA_REV_99X0(ar)) {
2162 ath10k_pci_irq_disable(ar);
2163 return ath10k_pci_qca99x0_chip_reset(ar);
2164 } else {
2165 return -ENOTSUPP;
2166 }
2167}
2168
Michal Kaziord63955b2015-01-24 12:14:49 +02002169static int ath10k_pci_qca988x_chip_reset(struct ath10k *ar)
Michal Kazior0bc14d02014-10-28 10:32:07 +01002170{
2171 int i, ret;
2172 u32 val;
2173
Michal Kaziord63955b2015-01-24 12:14:49 +02002174 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot 988x chip reset\n");
Michal Kazior0bc14d02014-10-28 10:32:07 +01002175
2176 /* Some hardware revisions (e.g. CUS223v2) has issues with cold reset.
2177 * It is thus preferred to use warm reset which is safer but may not be
2178 * able to recover the device from all possible fail scenarios.
2179 *
2180 * Warm reset doesn't always work on first try so attempt it a few
2181 * times before giving up.
2182 */
2183 for (i = 0; i < ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS; i++) {
2184 ret = ath10k_pci_warm_reset(ar);
2185 if (ret) {
2186 ath10k_warn(ar, "failed to warm reset attempt %d of %d: %d\n",
2187 i + 1, ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS,
2188 ret);
2189 continue;
2190 }
2191
2192 /* FIXME: Sometimes copy engine doesn't recover after warm
2193 * reset. In most cases this needs cold reset. In some of these
2194 * cases the device is in such a state that a cold reset may
2195 * lock up the host.
2196 *
2197 * Reading any host interest register via copy engine is
2198 * sufficient to verify if device is capable of booting
2199 * firmware blob.
2200 */
2201 ret = ath10k_pci_init_pipes(ar);
2202 if (ret) {
2203 ath10k_warn(ar, "failed to init copy engine: %d\n",
2204 ret);
2205 continue;
2206 }
2207
2208 ret = ath10k_pci_diag_read32(ar, QCA988X_HOST_INTEREST_ADDRESS,
2209 &val);
2210 if (ret) {
2211 ath10k_warn(ar, "failed to poke copy engine: %d\n",
2212 ret);
2213 continue;
2214 }
2215
2216 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot chip reset complete (warm)\n");
2217 return 0;
2218 }
2219
2220 if (ath10k_pci_reset_mode == ATH10K_PCI_RESET_WARM_ONLY) {
2221 ath10k_warn(ar, "refusing cold reset as requested\n");
2222 return -EPERM;
2223 }
2224
2225 ret = ath10k_pci_cold_reset(ar);
2226 if (ret) {
2227 ath10k_warn(ar, "failed to cold reset: %d\n", ret);
2228 return ret;
2229 }
2230
2231 ret = ath10k_pci_wait_for_target_init(ar);
2232 if (ret) {
2233 ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
2234 ret);
2235 return ret;
2236 }
2237
Michal Kaziord63955b2015-01-24 12:14:49 +02002238 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca988x chip reset complete (cold)\n");
Michal Kazior0bc14d02014-10-28 10:32:07 +01002239
2240 return 0;
2241}
2242
Michal Kaziord63955b2015-01-24 12:14:49 +02002243static int ath10k_pci_qca6174_chip_reset(struct ath10k *ar)
2244{
2245 int ret;
2246
2247 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca6174 chip reset\n");
2248
2249 /* FIXME: QCA6174 requires cold + warm reset to work. */
2250
2251 ret = ath10k_pci_cold_reset(ar);
2252 if (ret) {
2253 ath10k_warn(ar, "failed to cold reset: %d\n", ret);
2254 return ret;
2255 }
2256
2257 ret = ath10k_pci_wait_for_target_init(ar);
2258 if (ret) {
2259 ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
2260 ret);
2261 return ret;
2262 }
2263
2264 ret = ath10k_pci_warm_reset(ar);
2265 if (ret) {
2266 ath10k_warn(ar, "failed to warm reset: %d\n", ret);
2267 return ret;
2268 }
2269
2270 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca6174 chip reset complete (cold)\n");
2271
2272 return 0;
2273}
2274
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +05302275static int ath10k_pci_qca99x0_chip_reset(struct ath10k *ar)
2276{
2277 int ret;
2278
2279 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca99x0 chip reset\n");
2280
2281 ret = ath10k_pci_cold_reset(ar);
2282 if (ret) {
2283 ath10k_warn(ar, "failed to cold reset: %d\n", ret);
2284 return ret;
2285 }
2286
2287 ret = ath10k_pci_wait_for_target_init(ar);
2288 if (ret) {
2289 ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
2290 ret);
2291 return ret;
2292 }
2293
2294 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca99x0 chip reset complete (cold)\n");
2295
2296 return 0;
2297}
2298
Michal Kaziord63955b2015-01-24 12:14:49 +02002299static int ath10k_pci_chip_reset(struct ath10k *ar)
2300{
2301 if (QCA_REV_988X(ar))
2302 return ath10k_pci_qca988x_chip_reset(ar);
2303 else if (QCA_REV_6174(ar))
2304 return ath10k_pci_qca6174_chip_reset(ar);
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +05302305 else if (QCA_REV_99X0(ar))
2306 return ath10k_pci_qca99x0_chip_reset(ar);
Michal Kaziord63955b2015-01-24 12:14:49 +02002307 else
2308 return -ENOTSUPP;
2309}
2310
Michal Kazior0bc14d02014-10-28 10:32:07 +01002311static int ath10k_pci_hif_power_up(struct ath10k *ar)
Michal Kazior8c5c5362013-07-16 09:38:50 +02002312{
Janusz Dziedzic76d870e2015-05-18 09:38:16 +00002313 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02002314 int ret;
2315
Michal Kazior0bc14d02014-10-28 10:32:07 +01002316 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif power up\n");
2317
Janusz Dziedzic76d870e2015-05-18 09:38:16 +00002318 pcie_capability_read_word(ar_pci->pdev, PCI_EXP_LNKCTL,
2319 &ar_pci->link_ctl);
2320 pcie_capability_write_word(ar_pci->pdev, PCI_EXP_LNKCTL,
2321 ar_pci->link_ctl & ~PCI_EXP_LNKCTL_ASPMC);
2322
Michal Kazior8c5c5362013-07-16 09:38:50 +02002323 /*
2324 * Bring the target up cleanly.
2325 *
2326 * The target may be in an undefined state with an AUX-powered Target
2327 * and a Host in WoW mode. If the Host crashes, loses power, or is
2328 * restarted (without unloading the driver) then the Target is left
2329 * (aux) powered and running. On a subsequent driver load, the Target
2330 * is in an unexpected state. We try to catch that here in order to
2331 * reset the Target and retry the probe.
2332 */
Michal Kazior0bc14d02014-10-28 10:32:07 +01002333 ret = ath10k_pci_chip_reset(ar);
Michal Kazior5b2589f2013-11-08 08:01:30 +01002334 if (ret) {
Michal Kaziora2fa8802015-01-12 15:29:37 +01002335 if (ath10k_pci_has_fw_crashed(ar)) {
2336 ath10k_warn(ar, "firmware crashed during chip reset\n");
2337 ath10k_pci_fw_crashed_clear(ar);
2338 ath10k_pci_fw_crashed_dump(ar);
2339 }
2340
Michal Kazior0bc14d02014-10-28 10:32:07 +01002341 ath10k_err(ar, "failed to reset chip: %d\n", ret);
Bartosz Markowski707b1bbd2014-10-31 09:03:43 +01002342 goto err_sleep;
Michal Kazior5b2589f2013-11-08 08:01:30 +01002343 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02002344
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002345 ret = ath10k_pci_init_pipes(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02002346 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002347 ath10k_err(ar, "failed to initialize CE: %d\n", ret);
Bartosz Markowski707b1bbd2014-10-31 09:03:43 +01002348 goto err_sleep;
Michal Kaziorab977bd2013-11-25 14:06:26 +01002349 }
2350
Michal Kazior98563d52013-11-08 08:01:33 +01002351 ret = ath10k_pci_init_config(ar);
2352 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002353 ath10k_err(ar, "failed to setup init config: %d\n", ret);
Michal Kazior5c771e72014-08-22 14:23:34 +02002354 goto err_ce;
Michal Kazior98563d52013-11-08 08:01:33 +01002355 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02002356
2357 ret = ath10k_pci_wake_target_cpu(ar);
2358 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002359 ath10k_err(ar, "could not wake up target CPU: %d\n", ret);
Michal Kazior5c771e72014-08-22 14:23:34 +02002360 goto err_ce;
Michal Kazior8c5c5362013-07-16 09:38:50 +02002361 }
2362
2363 return 0;
2364
2365err_ce:
2366 ath10k_pci_ce_deinit(ar);
Michal Kazior0bc14d02014-10-28 10:32:07 +01002367
Bartosz Markowski707b1bbd2014-10-31 09:03:43 +01002368err_sleep:
Michal Kazior8c5c5362013-07-16 09:38:50 +02002369 return ret;
2370}
2371
2372static void ath10k_pci_hif_power_down(struct ath10k *ar)
2373{
Michal Kazior7aa7a722014-08-25 12:09:38 +02002374 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif power down\n");
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002375
Michal Kaziorc011b282014-10-28 10:32:08 +01002376 /* Currently hif_power_up performs effectively a reset and hif_stop
2377 * resets the chip as well so there's no point in resetting here.
2378 */
Michal Kazior8c5c5362013-07-16 09:38:50 +02002379}
2380
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002381#ifdef CONFIG_PM
2382
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002383static int ath10k_pci_hif_suspend(struct ath10k *ar)
2384{
Michal Kazior77258d42015-05-18 09:38:18 +00002385 /* The grace timer can still be counting down and ar->ps_awake be true.
2386 * It is known that the device may be asleep after resuming regardless
2387 * of the SoC powersave state before suspending. Hence make sure the
2388 * device is asleep before proceeding.
2389 */
2390 ath10k_pci_sleep_sync(ar);
Michal Kazior320e14b2015-03-02 13:22:13 +01002391
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002392 return 0;
2393}
2394
2395static int ath10k_pci_hif_resume(struct ath10k *ar)
2396{
2397 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2398 struct pci_dev *pdev = ar_pci->pdev;
2399 u32 val;
2400
Michal Kazior9ff4be92015-03-02 13:22:14 +01002401 /* Suspend/Resume resets the PCI configuration space, so we have to
2402 * re-disable the RETRY_TIMEOUT register (0x41) to keep PCI Tx retries
2403 * from interfering with C3 CPU state. pci_restore_state won't help
2404 * here since it only restores the first 64 bytes pci config header.
2405 */
2406 pci_read_config_dword(pdev, 0x40, &val);
2407 if ((val & 0x0000ff00) != 0)
2408 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002409
Michal Kazior77258d42015-05-18 09:38:18 +00002410 return 0;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002411}
2412#endif
2413
Kalle Valo5e3dd152013-06-12 20:52:10 +03002414static const struct ath10k_hif_ops ath10k_pci_hif_ops = {
Michal Kazior726346f2014-02-27 18:50:04 +02002415 .tx_sg = ath10k_pci_hif_tx_sg,
Kalle Valoeef25402014-09-24 14:16:52 +03002416 .diag_read = ath10k_pci_hif_diag_read,
Yanbo Li9f65ad22014-11-25 12:24:48 +02002417 .diag_write = ath10k_pci_diag_write_mem,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002418 .exchange_bmi_msg = ath10k_pci_hif_exchange_bmi_msg,
2419 .start = ath10k_pci_hif_start,
2420 .stop = ath10k_pci_hif_stop,
2421 .map_service_to_pipe = ath10k_pci_hif_map_service_to_pipe,
2422 .get_default_pipe = ath10k_pci_hif_get_default_pipe,
2423 .send_complete_check = ath10k_pci_hif_send_complete_check,
Michal Kaziore799bbf2013-07-05 16:15:12 +03002424 .set_callbacks = ath10k_pci_hif_set_callbacks,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002425 .get_free_queue_number = ath10k_pci_hif_get_free_queue_number,
Michal Kazior8c5c5362013-07-16 09:38:50 +02002426 .power_up = ath10k_pci_hif_power_up,
2427 .power_down = ath10k_pci_hif_power_down,
Yanbo Li077a3802014-11-25 12:24:33 +02002428 .read32 = ath10k_pci_read32,
2429 .write32 = ath10k_pci_write32,
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002430#ifdef CONFIG_PM
2431 .suspend = ath10k_pci_hif_suspend,
2432 .resume = ath10k_pci_hif_resume,
2433#endif
Kalle Valo5e3dd152013-06-12 20:52:10 +03002434};
2435
2436static void ath10k_pci_ce_tasklet(unsigned long ptr)
2437{
Michal Kazior87263e52013-08-27 13:08:01 +02002438 struct ath10k_pci_pipe *pipe = (struct ath10k_pci_pipe *)ptr;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002439 struct ath10k_pci *ar_pci = pipe->ar_pci;
2440
2441 ath10k_ce_per_engine_service(ar_pci->ar, pipe->pipe_num);
2442}
2443
2444static void ath10k_msi_err_tasklet(unsigned long data)
2445{
2446 struct ath10k *ar = (struct ath10k *)data;
2447
Michal Kazior5c771e72014-08-22 14:23:34 +02002448 if (!ath10k_pci_has_fw_crashed(ar)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002449 ath10k_warn(ar, "received unsolicited fw crash interrupt\n");
Michal Kazior5c771e72014-08-22 14:23:34 +02002450 return;
2451 }
2452
Michal Kazior6f3b7ff2015-01-24 12:14:52 +02002453 ath10k_pci_irq_disable(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02002454 ath10k_pci_fw_crashed_clear(ar);
2455 ath10k_pci_fw_crashed_dump(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002456}
2457
2458/*
2459 * Handler for a per-engine interrupt on a PARTICULAR CE.
2460 * This is used in cases where each CE has a private MSI interrupt.
2461 */
2462static irqreturn_t ath10k_pci_per_engine_handler(int irq, void *arg)
2463{
2464 struct ath10k *ar = arg;
2465 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2466 int ce_id = irq - ar_pci->pdev->irq - MSI_ASSIGN_CE_INITIAL;
2467
Dan Carpentere5742672013-06-18 10:28:46 +03002468 if (ce_id < 0 || ce_id >= ARRAY_SIZE(ar_pci->pipe_info)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002469 ath10k_warn(ar, "unexpected/invalid irq %d ce_id %d\n", irq,
2470 ce_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002471 return IRQ_HANDLED;
2472 }
2473
2474 /*
2475 * NOTE: We are able to derive ce_id from irq because we
2476 * use a one-to-one mapping for CE's 0..5.
2477 * CE's 6 & 7 do not use interrupts at all.
2478 *
2479 * This mapping must be kept in sync with the mapping
2480 * used by firmware.
2481 */
2482 tasklet_schedule(&ar_pci->pipe_info[ce_id].intr);
2483 return IRQ_HANDLED;
2484}
2485
2486static irqreturn_t ath10k_pci_msi_fw_handler(int irq, void *arg)
2487{
2488 struct ath10k *ar = arg;
2489 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2490
2491 tasklet_schedule(&ar_pci->msi_fw_err);
2492 return IRQ_HANDLED;
2493}
2494
2495/*
2496 * Top-level interrupt handler for all PCI interrupts from a Target.
2497 * When a block of MSI interrupts is allocated, this top-level handler
2498 * is not used; instead, we directly call the correct sub-handler.
2499 */
2500static irqreturn_t ath10k_pci_interrupt_handler(int irq, void *arg)
2501{
2502 struct ath10k *ar = arg;
2503 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2504
2505 if (ar_pci->num_msi_intrs == 0) {
Michal Kaziore5398872013-11-25 14:06:20 +01002506 if (!ath10k_pci_irq_pending(ar))
2507 return IRQ_NONE;
2508
Michal Kazior26852182013-11-25 14:06:25 +01002509 ath10k_pci_disable_and_clear_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002510 }
2511
2512 tasklet_schedule(&ar_pci->intr_tq);
2513
2514 return IRQ_HANDLED;
2515}
2516
2517static void ath10k_pci_tasklet(unsigned long data)
2518{
2519 struct ath10k *ar = (struct ath10k *)data;
2520 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2521
Michal Kazior5c771e72014-08-22 14:23:34 +02002522 if (ath10k_pci_has_fw_crashed(ar)) {
Michal Kazior6f3b7ff2015-01-24 12:14:52 +02002523 ath10k_pci_irq_disable(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02002524 ath10k_pci_fw_crashed_clear(ar);
2525 ath10k_pci_fw_crashed_dump(ar);
2526 return;
2527 }
2528
Kalle Valo5e3dd152013-06-12 20:52:10 +03002529 ath10k_ce_per_engine_service_any(ar);
2530
Michal Kazior26852182013-11-25 14:06:25 +01002531 /* Re-enable legacy irq that was disabled in the irq handler */
2532 if (ar_pci->num_msi_intrs == 0)
2533 ath10k_pci_enable_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002534}
2535
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002536static int ath10k_pci_request_irq_msix(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002537{
2538 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002539 int ret, i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002540
2541 ret = request_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW,
2542 ath10k_pci_msi_fw_handler,
2543 IRQF_SHARED, "ath10k_pci", ar);
Michal Kazior591ecdb2013-07-31 10:55:15 +02002544 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002545 ath10k_warn(ar, "failed to request MSI-X fw irq %d: %d\n",
Michal Kazior591ecdb2013-07-31 10:55:15 +02002546 ar_pci->pdev->irq + MSI_ASSIGN_FW, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002547 return ret;
Michal Kazior591ecdb2013-07-31 10:55:15 +02002548 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002549
2550 for (i = MSI_ASSIGN_CE_INITIAL; i <= MSI_ASSIGN_CE_MAX; i++) {
2551 ret = request_irq(ar_pci->pdev->irq + i,
2552 ath10k_pci_per_engine_handler,
2553 IRQF_SHARED, "ath10k_pci", ar);
2554 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002555 ath10k_warn(ar, "failed to request MSI-X ce irq %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002556 ar_pci->pdev->irq + i, ret);
2557
Michal Kazior87b14232013-06-26 08:50:50 +02002558 for (i--; i >= MSI_ASSIGN_CE_INITIAL; i--)
2559 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002560
Michal Kazior87b14232013-06-26 08:50:50 +02002561 free_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002562 return ret;
2563 }
2564 }
2565
Kalle Valo5e3dd152013-06-12 20:52:10 +03002566 return 0;
2567}
2568
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002569static int ath10k_pci_request_irq_msi(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002570{
2571 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2572 int ret;
2573
2574 ret = request_irq(ar_pci->pdev->irq,
2575 ath10k_pci_interrupt_handler,
2576 IRQF_SHARED, "ath10k_pci", ar);
Kalle Valof3782742013-10-17 11:36:15 +03002577 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002578 ath10k_warn(ar, "failed to request MSI irq %d: %d\n",
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002579 ar_pci->pdev->irq, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002580 return ret;
Kalle Valof3782742013-10-17 11:36:15 +03002581 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002582
Kalle Valo5e3dd152013-06-12 20:52:10 +03002583 return 0;
2584}
2585
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002586static int ath10k_pci_request_irq_legacy(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002587{
2588 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002589 int ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002590
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002591 ret = request_irq(ar_pci->pdev->irq,
2592 ath10k_pci_interrupt_handler,
2593 IRQF_SHARED, "ath10k_pci", ar);
Kalle Valof3782742013-10-17 11:36:15 +03002594 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002595 ath10k_warn(ar, "failed to request legacy irq %d: %d\n",
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002596 ar_pci->pdev->irq, ret);
Kalle Valof3782742013-10-17 11:36:15 +03002597 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002598 }
2599
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002600 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002601}
2602
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002603static int ath10k_pci_request_irq(struct ath10k *ar)
2604{
2605 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2606
2607 switch (ar_pci->num_msi_intrs) {
2608 case 0:
2609 return ath10k_pci_request_irq_legacy(ar);
2610 case 1:
2611 return ath10k_pci_request_irq_msi(ar);
Janusz Dziedzicb8402d82015-09-17 08:17:33 +02002612 default:
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002613 return ath10k_pci_request_irq_msix(ar);
2614 }
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002615}
2616
2617static void ath10k_pci_free_irq(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002618{
2619 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2620 int i;
2621
2622 /* There's at least one interrupt irregardless whether its legacy INTR
2623 * or MSI or MSI-X */
2624 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
2625 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002626}
2627
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002628static void ath10k_pci_init_irq_tasklets(struct ath10k *ar)
2629{
2630 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2631 int i;
2632
2633 tasklet_init(&ar_pci->intr_tq, ath10k_pci_tasklet, (unsigned long)ar);
2634 tasklet_init(&ar_pci->msi_fw_err, ath10k_msi_err_tasklet,
2635 (unsigned long)ar);
2636
2637 for (i = 0; i < CE_COUNT; i++) {
2638 ar_pci->pipe_info[i].ar_pci = ar_pci;
2639 tasklet_init(&ar_pci->pipe_info[i].intr, ath10k_pci_ce_tasklet,
2640 (unsigned long)&ar_pci->pipe_info[i]);
2641 }
2642}
2643
2644static int ath10k_pci_init_irq(struct ath10k *ar)
2645{
2646 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2647 int ret;
2648
2649 ath10k_pci_init_irq_tasklets(ar);
2650
Michal Kazior403d6272014-08-22 14:23:31 +02002651 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_AUTO)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002652 ath10k_info(ar, "limiting irq mode to: %d\n",
2653 ath10k_pci_irq_mode);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002654
2655 /* Try MSI-X */
Michal Kazior0edf2572014-08-07 11:03:29 +02002656 if (ath10k_pci_irq_mode == ATH10K_PCI_IRQ_AUTO) {
Janusz Dziedzicb8402d82015-09-17 08:17:33 +02002657 ar_pci->num_msi_intrs = MSI_ASSIGN_CE_MAX + 1;
Alexander Gordeev5ad68672014-02-13 17:50:02 +02002658 ret = pci_enable_msi_range(ar_pci->pdev, ar_pci->num_msi_intrs,
Kalle Valo5b07e072014-09-14 12:50:06 +03002659 ar_pci->num_msi_intrs);
Alexander Gordeev5ad68672014-02-13 17:50:02 +02002660 if (ret > 0)
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002661 return 0;
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002662
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002663 /* fall-through */
2664 }
2665
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002666 /* Try MSI */
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002667 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_LEGACY) {
2668 ar_pci->num_msi_intrs = 1;
2669 ret = pci_enable_msi(ar_pci->pdev);
2670 if (ret == 0)
2671 return 0;
2672
2673 /* fall-through */
2674 }
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002675
2676 /* Try legacy irq
2677 *
2678 * A potential race occurs here: The CORE_BASE write
2679 * depends on target correctly decoding AXI address but
2680 * host won't know when target writes BAR to CORE_CTRL.
2681 * This write might get lost if target has NOT written BAR.
2682 * For now, fix the race by repeating the write in below
2683 * synchronization checking. */
2684 ar_pci->num_msi_intrs = 0;
2685
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002686 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2687 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002688
2689 return 0;
2690}
2691
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002692static void ath10k_pci_deinit_irq_legacy(struct ath10k *ar)
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002693{
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002694 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2695 0);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002696}
2697
2698static int ath10k_pci_deinit_irq(struct ath10k *ar)
2699{
2700 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2701
2702 switch (ar_pci->num_msi_intrs) {
2703 case 0:
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002704 ath10k_pci_deinit_irq_legacy(ar);
Janusz Dziedzicb8402d82015-09-17 08:17:33 +02002705 break;
Alexander Gordeevbb8b6212014-02-13 17:50:01 +02002706 default:
2707 pci_disable_msi(ar_pci->pdev);
Janusz Dziedzicb8402d82015-09-17 08:17:33 +02002708 break;
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002709 }
2710
Janusz Dziedzicb8402d82015-09-17 08:17:33 +02002711 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002712}
2713
Michal Kaziord7fb47f2013-11-08 08:01:26 +01002714static int ath10k_pci_wait_for_target_init(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002715{
2716 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo0399eca2014-03-28 09:32:21 +02002717 unsigned long timeout;
Kalle Valo0399eca2014-03-28 09:32:21 +02002718 u32 val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002719
Michal Kazior7aa7a722014-08-25 12:09:38 +02002720 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot waiting target to initialise\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002721
Kalle Valo0399eca2014-03-28 09:32:21 +02002722 timeout = jiffies + msecs_to_jiffies(ATH10K_PCI_TARGET_WAIT);
2723
2724 do {
2725 val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
2726
Michal Kazior7aa7a722014-08-25 12:09:38 +02002727 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target indicator %x\n",
2728 val);
Kalle Valo50f87a62014-03-28 09:32:52 +02002729
Kalle Valo0399eca2014-03-28 09:32:21 +02002730 /* target should never return this */
2731 if (val == 0xffffffff)
2732 continue;
2733
Michal Kazior7710cd22014-04-23 19:30:04 +03002734 /* the device has crashed so don't bother trying anymore */
2735 if (val & FW_IND_EVENT_PENDING)
2736 break;
2737
Kalle Valo0399eca2014-03-28 09:32:21 +02002738 if (val & FW_IND_INITIALIZED)
2739 break;
2740
Kalle Valo5e3dd152013-06-12 20:52:10 +03002741 if (ar_pci->num_msi_intrs == 0)
2742 /* Fix potential race by repeating CORE_BASE writes */
Michal Kaziora4282492014-10-20 14:14:37 +02002743 ath10k_pci_enable_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002744
Kalle Valo0399eca2014-03-28 09:32:21 +02002745 mdelay(10);
2746 } while (time_before(jiffies, timeout));
2747
Michal Kaziora4282492014-10-20 14:14:37 +02002748 ath10k_pci_disable_and_clear_legacy_irq(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02002749 ath10k_pci_irq_msi_fw_mask(ar);
Michal Kaziora4282492014-10-20 14:14:37 +02002750
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002751 if (val == 0xffffffff) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002752 ath10k_err(ar, "failed to read device register, device is gone\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002753 return -EIO;
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002754 }
2755
Michal Kazior7710cd22014-04-23 19:30:04 +03002756 if (val & FW_IND_EVENT_PENDING) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002757 ath10k_warn(ar, "device has crashed during init\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002758 return -ECOMM;
Michal Kazior7710cd22014-04-23 19:30:04 +03002759 }
2760
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002761 if (!(val & FW_IND_INITIALIZED)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002762 ath10k_err(ar, "failed to receive initialized event from target: %08x\n",
Kalle Valo0399eca2014-03-28 09:32:21 +02002763 val);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002764 return -ETIMEDOUT;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002765 }
2766
Michal Kazior7aa7a722014-08-25 12:09:38 +02002767 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target initialised\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002768 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002769}
2770
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002771static int ath10k_pci_cold_reset(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002772{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002773 u32 val;
2774
Michal Kazior7aa7a722014-08-25 12:09:38 +02002775 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cold reset\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002776
Ben Greearf51dbe72014-09-29 14:41:46 +03002777 spin_lock_bh(&ar->data_lock);
2778
2779 ar->stats.fw_cold_reset_counter++;
2780
2781 spin_unlock_bh(&ar->data_lock);
2782
Kalle Valo5e3dd152013-06-12 20:52:10 +03002783 /* Put Target, including PCIe, into RESET. */
Kalle Valoe479ed42013-09-01 10:01:53 +03002784 val = ath10k_pci_reg_read32(ar, SOC_GLOBAL_RESET_ADDRESS);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002785 val |= 1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002786 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002787
Vasanthakumar Thiagarajanacd19582015-07-10 14:31:20 +05302788 /* After writing into SOC_GLOBAL_RESET to put device into
2789 * reset and pulling out of reset pcie may not be stable
2790 * for any immediate pcie register access and cause bus error,
2791 * add delay before any pcie access request to fix this issue.
2792 */
2793 msleep(20);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002794
2795 /* Pull Target, including PCIe, out of RESET. */
2796 val &= ~1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002797 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002798
Vasanthakumar Thiagarajanacd19582015-07-10 14:31:20 +05302799 msleep(20);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002800
Michal Kazior7aa7a722014-08-25 12:09:38 +02002801 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cold reset complete\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02002802
Michal Kazior5b2589f2013-11-08 08:01:30 +01002803 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002804}
2805
Michal Kazior2986e3e2014-08-07 11:03:30 +02002806static int ath10k_pci_claim(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002807{
Michal Kazior2986e3e2014-08-07 11:03:30 +02002808 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2809 struct pci_dev *pdev = ar_pci->pdev;
Michal Kazior2986e3e2014-08-07 11:03:30 +02002810 int ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002811
2812 pci_set_drvdata(pdev, ar);
2813
Kalle Valo5e3dd152013-06-12 20:52:10 +03002814 ret = pci_enable_device(pdev);
2815 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002816 ath10k_err(ar, "failed to enable pci device: %d\n", ret);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002817 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002818 }
2819
Kalle Valo5e3dd152013-06-12 20:52:10 +03002820 ret = pci_request_region(pdev, BAR_NUM, "ath");
2821 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002822 ath10k_err(ar, "failed to request region BAR%d: %d\n", BAR_NUM,
Michal Kazior2986e3e2014-08-07 11:03:30 +02002823 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002824 goto err_device;
2825 }
2826
Michal Kazior2986e3e2014-08-07 11:03:30 +02002827 /* Target expects 32 bit DMA. Enforce it. */
Kalle Valo5e3dd152013-06-12 20:52:10 +03002828 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2829 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002830 ath10k_err(ar, "failed to set dma mask to 32-bit: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002831 goto err_region;
2832 }
2833
2834 ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2835 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002836 ath10k_err(ar, "failed to set consistent dma mask to 32-bit: %d\n",
Michal Kazior2986e3e2014-08-07 11:03:30 +02002837 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002838 goto err_region;
2839 }
2840
Kalle Valo5e3dd152013-06-12 20:52:10 +03002841 pci_set_master(pdev);
2842
Kalle Valo5e3dd152013-06-12 20:52:10 +03002843 /* Arrange for access to Target SoC registers. */
Michal Kazioraeae5b42015-06-15 14:46:42 +03002844 ar_pci->mem_len = pci_resource_len(pdev, BAR_NUM);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002845 ar_pci->mem = pci_iomap(pdev, BAR_NUM, 0);
2846 if (!ar_pci->mem) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002847 ath10k_err(ar, "failed to iomap BAR%d\n", BAR_NUM);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002848 ret = -EIO;
2849 goto err_master;
2850 }
2851
Michal Kazior7aa7a722014-08-25 12:09:38 +02002852 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot pci_mem 0x%p\n", ar_pci->mem);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002853 return 0;
2854
2855err_master:
2856 pci_clear_master(pdev);
2857
2858err_region:
2859 pci_release_region(pdev, BAR_NUM);
2860
2861err_device:
2862 pci_disable_device(pdev);
2863
2864 return ret;
2865}
2866
2867static void ath10k_pci_release(struct ath10k *ar)
2868{
2869 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2870 struct pci_dev *pdev = ar_pci->pdev;
2871
2872 pci_iounmap(pdev, ar_pci->mem);
2873 pci_release_region(pdev, BAR_NUM);
2874 pci_clear_master(pdev);
2875 pci_disable_device(pdev);
2876}
2877
Michal Kazior7505f7c2014-12-02 10:55:54 +02002878static bool ath10k_pci_chip_is_supported(u32 dev_id, u32 chip_id)
2879{
2880 const struct ath10k_pci_supp_chip *supp_chip;
2881 int i;
2882 u32 rev_id = MS(chip_id, SOC_CHIP_ID_REV);
2883
2884 for (i = 0; i < ARRAY_SIZE(ath10k_pci_supp_chips); i++) {
2885 supp_chip = &ath10k_pci_supp_chips[i];
2886
2887 if (supp_chip->dev_id == dev_id &&
2888 supp_chip->rev_id == rev_id)
2889 return true;
2890 }
2891
2892 return false;
2893}
2894
Kalle Valo5e3dd152013-06-12 20:52:10 +03002895static int ath10k_pci_probe(struct pci_dev *pdev,
2896 const struct pci_device_id *pci_dev)
2897{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002898 int ret = 0;
2899 struct ath10k *ar;
2900 struct ath10k_pci *ar_pci;
Michal Kaziord63955b2015-01-24 12:14:49 +02002901 enum ath10k_hw_rev hw_rev;
Michal Kazior2986e3e2014-08-07 11:03:30 +02002902 u32 chip_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002903
Michal Kaziord63955b2015-01-24 12:14:49 +02002904 switch (pci_dev->device) {
2905 case QCA988X_2_0_DEVICE_ID:
2906 hw_rev = ATH10K_HW_QCA988X;
2907 break;
Michal Kazior36582e52015-08-13 14:32:26 +02002908 case QCA6164_2_1_DEVICE_ID:
Michal Kaziord63955b2015-01-24 12:14:49 +02002909 case QCA6174_2_1_DEVICE_ID:
2910 hw_rev = ATH10K_HW_QCA6174;
2911 break;
Vasanthakumar Thiagarajan8bd47022015-06-18 12:31:03 +05302912 case QCA99X0_2_0_DEVICE_ID:
2913 hw_rev = ATH10K_HW_QCA99X0;
2914 break;
Michal Kaziord63955b2015-01-24 12:14:49 +02002915 default:
2916 WARN_ON(1);
2917 return -ENOTSUPP;
2918 }
2919
2920 ar = ath10k_core_create(sizeof(*ar_pci), &pdev->dev, ATH10K_BUS_PCI,
2921 hw_rev, &ath10k_pci_hif_ops);
Michal Kaziore7b54192014-08-07 11:03:27 +02002922 if (!ar) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002923 dev_err(&pdev->dev, "failed to allocate core\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002924 return -ENOMEM;
Michal Kaziore7b54192014-08-07 11:03:27 +02002925 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002926
Michal Kazior7aa7a722014-08-25 12:09:38 +02002927 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci probe\n");
2928
Michal Kaziore7b54192014-08-07 11:03:27 +02002929 ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002930 ar_pci->pdev = pdev;
2931 ar_pci->dev = &pdev->dev;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002932 ar_pci->ar = ar;
Michal Kazior36582e52015-08-13 14:32:26 +02002933 ar->dev_id = pci_dev->device;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002934
Michal Kaziorde57e2c2015-04-17 09:19:17 +00002935 if (pdev->subsystem_vendor || pdev->subsystem_device)
2936 scnprintf(ar->spec_board_id, sizeof(ar->spec_board_id),
2937 "%04x:%04x:%04x:%04x",
2938 pdev->vendor, pdev->device,
2939 pdev->subsystem_vendor, pdev->subsystem_device);
2940
Kalle Valo5e3dd152013-06-12 20:52:10 +03002941 spin_lock_init(&ar_pci->ce_lock);
Michal Kazior77258d42015-05-18 09:38:18 +00002942 spin_lock_init(&ar_pci->ps_lock);
2943
Michal Kazior728f95e2014-08-22 14:33:14 +02002944 setup_timer(&ar_pci->rx_post_retry, ath10k_pci_rx_replenish_retry,
2945 (unsigned long)ar);
Michal Kazior77258d42015-05-18 09:38:18 +00002946 setup_timer(&ar_pci->ps_timer, ath10k_pci_ps_timer,
2947 (unsigned long)ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002948
Michal Kazior2986e3e2014-08-07 11:03:30 +02002949 ret = ath10k_pci_claim(ar);
Kalle Valoe01ae682013-09-01 11:22:14 +03002950 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002951 ath10k_err(ar, "failed to claim device: %d\n", ret);
Michal Kaziore7b54192014-08-07 11:03:27 +02002952 goto err_core_destroy;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002953 }
2954
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002955 ret = ath10k_pci_alloc_pipes(ar);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002956 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002957 ath10k_err(ar, "failed to allocate copy engine pipes: %d\n",
2958 ret);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002959 goto err_sleep;
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002960 }
2961
Michal Kazior403d6272014-08-22 14:23:31 +02002962 ath10k_pci_ce_deinit(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02002963 ath10k_pci_irq_disable(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02002964
Michal Kazior403d6272014-08-22 14:23:31 +02002965 ret = ath10k_pci_init_irq(ar);
2966 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002967 ath10k_err(ar, "failed to init irqs: %d\n", ret);
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002968 goto err_free_pipes;
Michal Kazior403d6272014-08-22 14:23:31 +02002969 }
2970
Michal Kazior7aa7a722014-08-25 12:09:38 +02002971 ath10k_info(ar, "pci irq %s interrupts %d irq_mode %d reset_mode %d\n",
Michal Kazior403d6272014-08-22 14:23:31 +02002972 ath10k_pci_get_irq_method(ar), ar_pci->num_msi_intrs,
2973 ath10k_pci_irq_mode, ath10k_pci_reset_mode);
2974
Michal Kazior5c771e72014-08-22 14:23:34 +02002975 ret = ath10k_pci_request_irq(ar);
Michal Kazior403d6272014-08-22 14:23:31 +02002976 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002977 ath10k_warn(ar, "failed to request irqs: %d\n", ret);
Michal Kazior403d6272014-08-22 14:23:31 +02002978 goto err_deinit_irq;
2979 }
2980
Michal Kazior1a7fecb2015-01-24 12:14:48 +02002981 ret = ath10k_pci_chip_reset(ar);
2982 if (ret) {
2983 ath10k_err(ar, "failed to reset chip: %d\n", ret);
2984 goto err_free_irq;
2985 }
2986
2987 chip_id = ath10k_pci_soc_read32(ar, SOC_CHIP_ID_ADDRESS);
2988 if (chip_id == 0xffffffff) {
2989 ath10k_err(ar, "failed to get chip id\n");
2990 goto err_free_irq;
2991 }
2992
2993 if (!ath10k_pci_chip_is_supported(pdev->device, chip_id)) {
2994 ath10k_err(ar, "device %04x with chip_id %08x isn't supported\n",
2995 pdev->device, chip_id);
Michal Kaziord9585a92015-04-10 13:01:27 +00002996 goto err_free_irq;
Michal Kazior1a7fecb2015-01-24 12:14:48 +02002997 }
2998
Kalle Valoe01ae682013-09-01 11:22:14 +03002999 ret = ath10k_core_register(ar, chip_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003000 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003001 ath10k_err(ar, "failed to register driver core: %d\n", ret);
Michal Kazior5c771e72014-08-22 14:23:34 +02003002 goto err_free_irq;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003003 }
3004
3005 return 0;
3006
Michal Kazior5c771e72014-08-22 14:23:34 +02003007err_free_irq:
3008 ath10k_pci_free_irq(ar);
Michal Kazior21396272014-08-28 10:24:40 +02003009 ath10k_pci_kill_tasklet(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02003010
Michal Kazior403d6272014-08-22 14:23:31 +02003011err_deinit_irq:
3012 ath10k_pci_deinit_irq(ar);
3013
Michal Kazior84cbf3a2014-10-20 14:14:39 +02003014err_free_pipes:
3015 ath10k_pci_free_pipes(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02003016
Michal Kaziorc0c378f2014-08-07 11:03:28 +02003017err_sleep:
Michal Kazior0bcbbe62015-05-29 07:35:24 +02003018 ath10k_pci_sleep_sync(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02003019 ath10k_pci_release(ar);
3020
Michal Kaziore7b54192014-08-07 11:03:27 +02003021err_core_destroy:
Kalle Valo5e3dd152013-06-12 20:52:10 +03003022 ath10k_core_destroy(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003023
3024 return ret;
3025}
3026
3027static void ath10k_pci_remove(struct pci_dev *pdev)
3028{
3029 struct ath10k *ar = pci_get_drvdata(pdev);
3030 struct ath10k_pci *ar_pci;
3031
Michal Kazior7aa7a722014-08-25 12:09:38 +02003032 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci remove\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03003033
3034 if (!ar)
3035 return;
3036
3037 ar_pci = ath10k_pci_priv(ar);
3038
3039 if (!ar_pci)
3040 return;
3041
Kalle Valo5e3dd152013-06-12 20:52:10 +03003042 ath10k_core_unregister(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02003043 ath10k_pci_free_irq(ar);
Michal Kazior21396272014-08-28 10:24:40 +02003044 ath10k_pci_kill_tasklet(ar);
Michal Kazior403d6272014-08-22 14:23:31 +02003045 ath10k_pci_deinit_irq(ar);
3046 ath10k_pci_ce_deinit(ar);
Michal Kazior84cbf3a2014-10-20 14:14:39 +02003047 ath10k_pci_free_pipes(ar);
Michal Kazior77258d42015-05-18 09:38:18 +00003048 ath10k_pci_sleep_sync(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02003049 ath10k_pci_release(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003050 ath10k_core_destroy(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003051}
3052
Kalle Valo5e3dd152013-06-12 20:52:10 +03003053MODULE_DEVICE_TABLE(pci, ath10k_pci_id_table);
3054
3055static struct pci_driver ath10k_pci_driver = {
3056 .name = "ath10k_pci",
3057 .id_table = ath10k_pci_id_table,
3058 .probe = ath10k_pci_probe,
3059 .remove = ath10k_pci_remove,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003060};
3061
3062static int __init ath10k_pci_init(void)
3063{
3064 int ret;
3065
3066 ret = pci_register_driver(&ath10k_pci_driver);
3067 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003068 printk(KERN_ERR "failed to register ath10k pci driver: %d\n",
3069 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003070
3071 return ret;
3072}
3073module_init(ath10k_pci_init);
3074
3075static void __exit ath10k_pci_exit(void)
3076{
3077 pci_unregister_driver(&ath10k_pci_driver);
3078}
3079
3080module_exit(ath10k_pci_exit);
3081
3082MODULE_AUTHOR("Qualcomm Atheros");
3083MODULE_DESCRIPTION("Driver support for Atheros QCA988X PCIe devices");
3084MODULE_LICENSE("Dual BSD/GPL");
Bartosz Markowski5c427f52015-02-18 13:16:37 +01003085
3086/* QCA988x 2.0 firmware files */
Bartosz Markowski8026cae2014-10-06 14:16:41 +02003087MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_FW_FILE);
3088MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API2_FILE);
3089MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API3_FILE);
Bartosz Markowski5c427f52015-02-18 13:16:37 +01003090MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API4_FILE);
Kalle Valo53513c32015-03-25 13:12:42 +02003091MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API5_FILE);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003092MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_BOARD_DATA_FILE);
Bartosz Markowski5c427f52015-02-18 13:16:37 +01003093
3094/* QCA6174 2.1 firmware files */
3095MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" ATH10K_FW_API4_FILE);
Michal Kaziore451c1d2015-05-26 13:09:22 +02003096MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" ATH10K_FW_API5_FILE);
Bartosz Markowski5c427f52015-02-18 13:16:37 +01003097MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" QCA6174_HW_2_1_BOARD_DATA_FILE);
3098
3099/* QCA6174 3.1 firmware files */
3100MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" ATH10K_FW_API4_FILE);
Michal Kaziore451c1d2015-05-26 13:09:22 +02003101MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" ATH10K_FW_API5_FILE);
Bartosz Markowski5c427f52015-02-18 13:16:37 +01003102MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" QCA6174_HW_3_0_BOARD_DATA_FILE);