blob: 5425d85fd12bfa1ce8cf109c9e104181726932b9 [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);
Rajkumar Manoharan0e5b2952015-10-12 18:27:01 +0530107static void ath10k_pci_htc_tx_cb(struct ath10k_ce_pipe *ce_state);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300108
109static const struct ce_attr host_ce_config_wlan[] = {
Kalle Valo48e9c222013-09-01 10:01:32 +0300110 /* CE0: host->target HTC control and raw streams */
111 {
112 .flags = CE_ATTR_FLAGS,
113 .src_nentries = 16,
114 .src_sz_max = 256,
115 .dest_nentries = 0,
Rajkumar Manoharan0e5b2952015-10-12 18:27:01 +0530116 .send_cb = ath10k_pci_htc_tx_cb,
Kalle Valo48e9c222013-09-01 10:01:32 +0300117 },
118
119 /* CE1: target->host HTT + HTC control */
120 {
121 .flags = CE_ATTR_FLAGS,
122 .src_nentries = 0,
Michal Kazior63838642015-02-09 15:04:55 +0100123 .src_sz_max = 2048,
Kalle Valo48e9c222013-09-01 10:01:32 +0300124 .dest_nentries = 512,
125 },
126
127 /* CE2: target->host WMI */
128 {
129 .flags = CE_ATTR_FLAGS,
130 .src_nentries = 0,
131 .src_sz_max = 2048,
Rajkumar Manoharan30abb332015-03-04 15:43:44 +0200132 .dest_nentries = 128,
Kalle Valo48e9c222013-09-01 10:01:32 +0300133 },
134
135 /* CE3: host->target WMI */
136 {
137 .flags = CE_ATTR_FLAGS,
138 .src_nentries = 32,
139 .src_sz_max = 2048,
140 .dest_nentries = 0,
Rajkumar Manoharan0e5b2952015-10-12 18:27:01 +0530141 .send_cb = ath10k_pci_htc_tx_cb,
Kalle Valo48e9c222013-09-01 10:01:32 +0300142 },
143
144 /* CE4: host->target HTT */
145 {
146 .flags = CE_ATTR_FLAGS | CE_ATTR_DIS_INTR,
147 .src_nentries = CE_HTT_H2T_MSG_SRC_NENTRIES,
148 .src_sz_max = 256,
149 .dest_nentries = 0,
Rajkumar Manoharan0e5b2952015-10-12 18:27:01 +0530150 .send_cb = ath10k_pci_htc_tx_cb,
Kalle Valo48e9c222013-09-01 10:01:32 +0300151 },
152
153 /* CE5: unused */
154 {
155 .flags = CE_ATTR_FLAGS,
156 .src_nentries = 0,
157 .src_sz_max = 0,
158 .dest_nentries = 0,
159 },
160
161 /* CE6: target autonomous hif_memcpy */
162 {
163 .flags = CE_ATTR_FLAGS,
164 .src_nentries = 0,
165 .src_sz_max = 0,
166 .dest_nentries = 0,
167 },
168
169 /* CE7: ce_diag, the Diagnostic Window */
170 {
171 .flags = CE_ATTR_FLAGS,
172 .src_nentries = 2,
173 .src_sz_max = DIAG_TRANSFER_LIMIT,
174 .dest_nentries = 2,
175 },
Vasanthakumar Thiagarajan050af062015-06-18 12:31:04 +0530176
177 /* CE8: target->host pktlog */
178 {
179 .flags = CE_ATTR_FLAGS,
180 .src_nentries = 0,
181 .src_sz_max = 2048,
182 .dest_nentries = 128,
183 },
184
185 /* CE9 target autonomous qcache memcpy */
186 {
187 .flags = CE_ATTR_FLAGS,
188 .src_nentries = 0,
189 .src_sz_max = 0,
190 .dest_nentries = 0,
191 },
192
193 /* CE10: target autonomous hif memcpy */
194 {
195 .flags = CE_ATTR_FLAGS,
196 .src_nentries = 0,
197 .src_sz_max = 0,
198 .dest_nentries = 0,
199 },
200
201 /* CE11: target autonomous hif memcpy */
202 {
203 .flags = CE_ATTR_FLAGS,
204 .src_nentries = 0,
205 .src_sz_max = 0,
206 .dest_nentries = 0,
207 },
Kalle Valo5e3dd152013-06-12 20:52:10 +0300208};
209
210/* Target firmware's Copy Engine configuration. */
211static const struct ce_pipe_config target_ce_config_wlan[] = {
Kalle Valod88effb2013-09-01 10:01:39 +0300212 /* CE0: host->target HTC control and raw streams */
213 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300214 .pipenum = __cpu_to_le32(0),
215 .pipedir = __cpu_to_le32(PIPEDIR_OUT),
216 .nentries = __cpu_to_le32(32),
217 .nbytes_max = __cpu_to_le32(256),
218 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
219 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300220 },
221
222 /* CE1: target->host HTT + HTC control */
223 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300224 .pipenum = __cpu_to_le32(1),
225 .pipedir = __cpu_to_le32(PIPEDIR_IN),
226 .nentries = __cpu_to_le32(32),
Michal Kazior63838642015-02-09 15:04:55 +0100227 .nbytes_max = __cpu_to_le32(2048),
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300228 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
229 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300230 },
231
232 /* CE2: target->host WMI */
233 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300234 .pipenum = __cpu_to_le32(2),
235 .pipedir = __cpu_to_le32(PIPEDIR_IN),
Rajkumar Manoharan30abb332015-03-04 15:43:44 +0200236 .nentries = __cpu_to_le32(64),
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300237 .nbytes_max = __cpu_to_le32(2048),
238 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
239 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300240 },
241
242 /* CE3: host->target WMI */
243 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300244 .pipenum = __cpu_to_le32(3),
245 .pipedir = __cpu_to_le32(PIPEDIR_OUT),
246 .nentries = __cpu_to_le32(32),
247 .nbytes_max = __cpu_to_le32(2048),
248 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
249 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300250 },
251
252 /* CE4: host->target HTT */
253 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300254 .pipenum = __cpu_to_le32(4),
255 .pipedir = __cpu_to_le32(PIPEDIR_OUT),
256 .nentries = __cpu_to_le32(256),
257 .nbytes_max = __cpu_to_le32(256),
258 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
259 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300260 },
261
Kalle Valo5e3dd152013-06-12 20:52:10 +0300262 /* NB: 50% of src nentries, since tx has 2 frags */
Kalle Valod88effb2013-09-01 10:01:39 +0300263
264 /* CE5: unused */
265 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300266 .pipenum = __cpu_to_le32(5),
267 .pipedir = __cpu_to_le32(PIPEDIR_OUT),
268 .nentries = __cpu_to_le32(32),
269 .nbytes_max = __cpu_to_le32(2048),
270 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
271 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300272 },
273
274 /* CE6: Reserved for target autonomous hif_memcpy */
275 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300276 .pipenum = __cpu_to_le32(6),
277 .pipedir = __cpu_to_le32(PIPEDIR_INOUT),
278 .nentries = __cpu_to_le32(32),
279 .nbytes_max = __cpu_to_le32(4096),
280 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
281 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300282 },
283
Kalle Valo5e3dd152013-06-12 20:52:10 +0300284 /* CE7 used only by Host */
Vasanthakumar Thiagarajan050af062015-06-18 12:31:04 +0530285 {
286 .pipenum = __cpu_to_le32(7),
287 .pipedir = __cpu_to_le32(PIPEDIR_INOUT),
288 .nentries = __cpu_to_le32(0),
289 .nbytes_max = __cpu_to_le32(0),
290 .flags = __cpu_to_le32(0),
291 .reserved = __cpu_to_le32(0),
292 },
293
294 /* CE8 target->host packtlog */
295 {
296 .pipenum = __cpu_to_le32(8),
297 .pipedir = __cpu_to_le32(PIPEDIR_IN),
298 .nentries = __cpu_to_le32(64),
299 .nbytes_max = __cpu_to_le32(2048),
300 .flags = __cpu_to_le32(CE_ATTR_FLAGS | CE_ATTR_DIS_INTR),
301 .reserved = __cpu_to_le32(0),
302 },
303
304 /* CE9 target autonomous qcache memcpy */
305 {
306 .pipenum = __cpu_to_le32(9),
307 .pipedir = __cpu_to_le32(PIPEDIR_INOUT),
308 .nentries = __cpu_to_le32(32),
309 .nbytes_max = __cpu_to_le32(2048),
310 .flags = __cpu_to_le32(CE_ATTR_FLAGS | CE_ATTR_DIS_INTR),
311 .reserved = __cpu_to_le32(0),
312 },
313
314 /* It not necessary to send target wlan configuration for CE10 & CE11
315 * as these CEs are not actively used in target.
316 */
Kalle Valo5e3dd152013-06-12 20:52:10 +0300317};
318
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300319/*
320 * Map from service/endpoint to Copy Engine.
321 * This table is derived from the CE_PCI TABLE, above.
322 * It is passed to the Target at startup for use by firmware.
323 */
324static const struct service_to_pipe target_service_to_ce_map_wlan[] = {
325 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300326 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VO),
327 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
328 __cpu_to_le32(3),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300329 },
330 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300331 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VO),
332 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
333 __cpu_to_le32(2),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300334 },
335 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300336 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BK),
337 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
338 __cpu_to_le32(3),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300339 },
340 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300341 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BK),
342 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
343 __cpu_to_le32(2),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300344 },
345 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300346 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BE),
347 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
348 __cpu_to_le32(3),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300349 },
350 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300351 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BE),
352 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
353 __cpu_to_le32(2),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300354 },
355 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300356 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VI),
357 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
358 __cpu_to_le32(3),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300359 },
360 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300361 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VI),
362 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
363 __cpu_to_le32(2),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300364 },
365 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300366 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_CONTROL),
367 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
368 __cpu_to_le32(3),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300369 },
370 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300371 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_CONTROL),
372 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
373 __cpu_to_le32(2),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300374 },
375 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300376 __cpu_to_le32(ATH10K_HTC_SVC_ID_RSVD_CTRL),
377 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
378 __cpu_to_le32(0),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300379 },
380 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300381 __cpu_to_le32(ATH10K_HTC_SVC_ID_RSVD_CTRL),
382 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
383 __cpu_to_le32(1),
384 },
385 { /* not used */
386 __cpu_to_le32(ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS),
387 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
388 __cpu_to_le32(0),
389 },
390 { /* not used */
391 __cpu_to_le32(ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS),
392 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
393 __cpu_to_le32(1),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300394 },
395 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300396 __cpu_to_le32(ATH10K_HTC_SVC_ID_HTT_DATA_MSG),
397 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
398 __cpu_to_le32(4),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300399 },
400 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300401 __cpu_to_le32(ATH10K_HTC_SVC_ID_HTT_DATA_MSG),
402 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
403 __cpu_to_le32(1),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300404 },
405
406 /* (Additions here) */
407
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300408 { /* must be last */
409 __cpu_to_le32(0),
410 __cpu_to_le32(0),
411 __cpu_to_le32(0),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300412 },
413};
414
Michal Kazior77258d42015-05-18 09:38:18 +0000415static bool ath10k_pci_is_awake(struct ath10k *ar)
416{
417 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
418 u32 val = ioread32(ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
419 RTC_STATE_ADDRESS);
420
421 return RTC_STATE_V_GET(val) == RTC_STATE_V_ON;
422}
423
424static void __ath10k_pci_wake(struct ath10k *ar)
425{
426 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
427
428 lockdep_assert_held(&ar_pci->ps_lock);
429
430 ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps wake reg refcount %lu awake %d\n",
431 ar_pci->ps_wake_refcount, ar_pci->ps_awake);
432
433 iowrite32(PCIE_SOC_WAKE_V_MASK,
434 ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
435 PCIE_SOC_WAKE_ADDRESS);
436}
437
438static void __ath10k_pci_sleep(struct ath10k *ar)
439{
440 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
441
442 lockdep_assert_held(&ar_pci->ps_lock);
443
444 ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps sleep reg refcount %lu awake %d\n",
445 ar_pci->ps_wake_refcount, ar_pci->ps_awake);
446
447 iowrite32(PCIE_SOC_WAKE_RESET,
448 ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
449 PCIE_SOC_WAKE_ADDRESS);
450 ar_pci->ps_awake = false;
451}
452
453static int ath10k_pci_wake_wait(struct ath10k *ar)
454{
455 int tot_delay = 0;
456 int curr_delay = 5;
457
458 while (tot_delay < PCIE_WAKE_TIMEOUT) {
Maharaja Kennadyrajan39b91b82015-10-06 15:19:28 +0300459 if (ath10k_pci_is_awake(ar)) {
460 if (tot_delay > PCIE_WAKE_LATE_US)
461 ath10k_warn(ar, "device wakeup took %d ms which is unusally long, otherwise it works normally.\n",
462 tot_delay / 1000);
Michal Kazior77258d42015-05-18 09:38:18 +0000463 return 0;
Maharaja Kennadyrajan39b91b82015-10-06 15:19:28 +0300464 }
Michal Kazior77258d42015-05-18 09:38:18 +0000465
466 udelay(curr_delay);
467 tot_delay += curr_delay;
468
469 if (curr_delay < 50)
470 curr_delay += 5;
471 }
472
473 return -ETIMEDOUT;
474}
475
476static int ath10k_pci_wake(struct ath10k *ar)
477{
478 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
479 unsigned long flags;
480 int ret = 0;
481
482 spin_lock_irqsave(&ar_pci->ps_lock, flags);
483
484 ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps wake refcount %lu awake %d\n",
485 ar_pci->ps_wake_refcount, ar_pci->ps_awake);
486
487 /* This function can be called very frequently. To avoid excessive
488 * CPU stalls for MMIO reads use a cache var to hold the device state.
489 */
490 if (!ar_pci->ps_awake) {
491 __ath10k_pci_wake(ar);
492
493 ret = ath10k_pci_wake_wait(ar);
494 if (ret == 0)
495 ar_pci->ps_awake = true;
496 }
497
498 if (ret == 0) {
499 ar_pci->ps_wake_refcount++;
500 WARN_ON(ar_pci->ps_wake_refcount == 0);
501 }
502
503 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
504
505 return ret;
506}
507
508static void ath10k_pci_sleep(struct ath10k *ar)
509{
510 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
511 unsigned long flags;
512
513 spin_lock_irqsave(&ar_pci->ps_lock, flags);
514
515 ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps sleep refcount %lu awake %d\n",
516 ar_pci->ps_wake_refcount, ar_pci->ps_awake);
517
518 if (WARN_ON(ar_pci->ps_wake_refcount == 0))
519 goto skip;
520
521 ar_pci->ps_wake_refcount--;
522
523 mod_timer(&ar_pci->ps_timer, jiffies +
524 msecs_to_jiffies(ATH10K_PCI_SLEEP_GRACE_PERIOD_MSEC));
525
526skip:
527 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
528}
529
530static void ath10k_pci_ps_timer(unsigned long ptr)
531{
532 struct ath10k *ar = (void *)ptr;
533 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
534 unsigned long flags;
535
536 spin_lock_irqsave(&ar_pci->ps_lock, flags);
537
538 ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps timer refcount %lu awake %d\n",
539 ar_pci->ps_wake_refcount, ar_pci->ps_awake);
540
541 if (ar_pci->ps_wake_refcount > 0)
542 goto skip;
543
544 __ath10k_pci_sleep(ar);
545
546skip:
547 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
548}
549
550static void ath10k_pci_sleep_sync(struct ath10k *ar)
551{
552 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
553 unsigned long flags;
554
555 del_timer_sync(&ar_pci->ps_timer);
556
557 spin_lock_irqsave(&ar_pci->ps_lock, flags);
558 WARN_ON(ar_pci->ps_wake_refcount > 0);
559 __ath10k_pci_sleep(ar);
560 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
561}
562
563void ath10k_pci_write32(struct ath10k *ar, u32 offset, u32 value)
564{
565 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
566 int ret;
567
Michal Kazioraeae5b42015-06-15 14:46:42 +0300568 if (unlikely(offset + sizeof(value) > ar_pci->mem_len)) {
569 ath10k_warn(ar, "refusing to write mmio out of bounds at 0x%08x - 0x%08zx (max 0x%08zx)\n",
570 offset, offset + sizeof(value), ar_pci->mem_len);
571 return;
572 }
573
Michal Kazior77258d42015-05-18 09:38:18 +0000574 ret = ath10k_pci_wake(ar);
575 if (ret) {
576 ath10k_warn(ar, "failed to wake target for write32 of 0x%08x at 0x%08x: %d\n",
577 value, offset, ret);
578 return;
579 }
580
581 iowrite32(value, ar_pci->mem + offset);
582 ath10k_pci_sleep(ar);
583}
584
585u32 ath10k_pci_read32(struct ath10k *ar, u32 offset)
586{
587 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
588 u32 val;
589 int ret;
590
Michal Kazioraeae5b42015-06-15 14:46:42 +0300591 if (unlikely(offset + sizeof(val) > ar_pci->mem_len)) {
592 ath10k_warn(ar, "refusing to read mmio out of bounds at 0x%08x - 0x%08zx (max 0x%08zx)\n",
593 offset, offset + sizeof(val), ar_pci->mem_len);
594 return 0;
595 }
596
Michal Kazior77258d42015-05-18 09:38:18 +0000597 ret = ath10k_pci_wake(ar);
598 if (ret) {
599 ath10k_warn(ar, "failed to wake target for read32 at 0x%08x: %d\n",
600 offset, ret);
601 return 0xffffffff;
602 }
603
604 val = ioread32(ar_pci->mem + offset);
605 ath10k_pci_sleep(ar);
606
607 return val;
608}
609
610u32 ath10k_pci_soc_read32(struct ath10k *ar, u32 addr)
611{
612 return ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS + addr);
613}
614
615void ath10k_pci_soc_write32(struct ath10k *ar, u32 addr, u32 val)
616{
617 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + addr, val);
618}
619
620u32 ath10k_pci_reg_read32(struct ath10k *ar, u32 addr)
621{
622 return ath10k_pci_read32(ar, PCIE_LOCAL_BASE_ADDRESS + addr);
623}
624
625void ath10k_pci_reg_write32(struct ath10k *ar, u32 addr, u32 val)
626{
627 ath10k_pci_write32(ar, PCIE_LOCAL_BASE_ADDRESS + addr, val);
628}
629
Michal Kaziore5398872013-11-25 14:06:20 +0100630static bool ath10k_pci_irq_pending(struct ath10k *ar)
631{
632 u32 cause;
633
634 /* Check if the shared legacy irq is for us */
635 cause = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
636 PCIE_INTR_CAUSE_ADDRESS);
637 if (cause & (PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL))
638 return true;
639
640 return false;
641}
642
Michal Kazior26852182013-11-25 14:06:25 +0100643static void ath10k_pci_disable_and_clear_legacy_irq(struct ath10k *ar)
644{
645 /* IMPORTANT: INTR_CLR register has to be set after
646 * INTR_ENABLE is set to 0, otherwise interrupt can not be
647 * really cleared. */
648 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
649 0);
650 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_CLR_ADDRESS,
651 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
652
653 /* IMPORTANT: this extra read transaction is required to
654 * flush the posted write buffer. */
Kalle Valocfbc06a2014-09-14 12:50:23 +0300655 (void)ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
656 PCIE_INTR_ENABLE_ADDRESS);
Michal Kazior26852182013-11-25 14:06:25 +0100657}
658
659static void ath10k_pci_enable_legacy_irq(struct ath10k *ar)
660{
661 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
662 PCIE_INTR_ENABLE_ADDRESS,
663 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
664
665 /* IMPORTANT: this extra read transaction is required to
666 * flush the posted write buffer. */
Kalle Valocfbc06a2014-09-14 12:50:23 +0300667 (void)ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
668 PCIE_INTR_ENABLE_ADDRESS);
Michal Kazior26852182013-11-25 14:06:25 +0100669}
670
Michal Kazior403d6272014-08-22 14:23:31 +0200671static inline const char *ath10k_pci_get_irq_method(struct ath10k *ar)
Michal Kaziorab977bd2013-11-25 14:06:26 +0100672{
Michal Kaziorab977bd2013-11-25 14:06:26 +0100673 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
674
Michal Kazior403d6272014-08-22 14:23:31 +0200675 if (ar_pci->num_msi_intrs > 1)
676 return "msi-x";
Kalle Valod8bb26b2014-09-14 12:50:33 +0300677
678 if (ar_pci->num_msi_intrs == 1)
Michal Kazior403d6272014-08-22 14:23:31 +0200679 return "msi";
Kalle Valod8bb26b2014-09-14 12:50:33 +0300680
681 return "legacy";
Michal Kaziorab977bd2013-11-25 14:06:26 +0100682}
683
Michal Kazior728f95e2014-08-22 14:33:14 +0200684static int __ath10k_pci_rx_post_buf(struct ath10k_pci_pipe *pipe)
Michal Kaziorab977bd2013-11-25 14:06:26 +0100685{
Michal Kazior728f95e2014-08-22 14:33:14 +0200686 struct ath10k *ar = pipe->hif_ce_state;
Michal Kaziorab977bd2013-11-25 14:06:26 +0100687 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior728f95e2014-08-22 14:33:14 +0200688 struct ath10k_ce_pipe *ce_pipe = pipe->ce_hdl;
689 struct sk_buff *skb;
690 dma_addr_t paddr;
Michal Kaziorab977bd2013-11-25 14:06:26 +0100691 int ret;
692
Michal Kazior728f95e2014-08-22 14:33:14 +0200693 skb = dev_alloc_skb(pipe->buf_sz);
694 if (!skb)
695 return -ENOMEM;
696
697 WARN_ONCE((unsigned long)skb->data & 3, "unaligned skb");
698
699 paddr = dma_map_single(ar->dev, skb->data,
700 skb->len + skb_tailroom(skb),
701 DMA_FROM_DEVICE);
702 if (unlikely(dma_mapping_error(ar->dev, paddr))) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200703 ath10k_warn(ar, "failed to dma map pci rx buf\n");
Michal Kazior728f95e2014-08-22 14:33:14 +0200704 dev_kfree_skb_any(skb);
705 return -EIO;
706 }
707
Michal Kazior8582bf32015-01-24 12:14:47 +0200708 ATH10K_SKB_RXCB(skb)->paddr = paddr;
Michal Kazior728f95e2014-08-22 14:33:14 +0200709
Rajkumar Manoharanab4e3db2015-10-06 15:19:33 +0300710 spin_lock_bh(&ar_pci->ce_lock);
Michal Kazior728f95e2014-08-22 14:33:14 +0200711 ret = __ath10k_ce_rx_post_buf(ce_pipe, skb, paddr);
Rajkumar Manoharanab4e3db2015-10-06 15:19:33 +0300712 spin_unlock_bh(&ar_pci->ce_lock);
Michal Kaziorab977bd2013-11-25 14:06:26 +0100713 if (ret) {
Michal Kazior728f95e2014-08-22 14:33:14 +0200714 dma_unmap_single(ar->dev, paddr, skb->len + skb_tailroom(skb),
715 DMA_FROM_DEVICE);
716 dev_kfree_skb_any(skb);
Michal Kaziorab977bd2013-11-25 14:06:26 +0100717 return ret;
718 }
719
720 return 0;
721}
722
Rajkumar Manoharanab4e3db2015-10-06 15:19:33 +0300723static void ath10k_pci_rx_post_pipe(struct ath10k_pci_pipe *pipe)
Michal Kaziorab977bd2013-11-25 14:06:26 +0100724{
Michal Kazior728f95e2014-08-22 14:33:14 +0200725 struct ath10k *ar = pipe->hif_ce_state;
726 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
727 struct ath10k_ce_pipe *ce_pipe = pipe->ce_hdl;
728 int ret, num;
729
Michal Kazior728f95e2014-08-22 14:33:14 +0200730 if (pipe->buf_sz == 0)
731 return;
732
733 if (!ce_pipe->dest_ring)
734 return;
735
Rajkumar Manoharanab4e3db2015-10-06 15:19:33 +0300736 spin_lock_bh(&ar_pci->ce_lock);
Michal Kazior728f95e2014-08-22 14:33:14 +0200737 num = __ath10k_ce_rx_num_free_bufs(ce_pipe);
Rajkumar Manoharanab4e3db2015-10-06 15:19:33 +0300738 spin_unlock_bh(&ar_pci->ce_lock);
Michal Kazior728f95e2014-08-22 14:33:14 +0200739 while (num--) {
740 ret = __ath10k_pci_rx_post_buf(pipe);
741 if (ret) {
Rajkumar Manoharanab4e3db2015-10-06 15:19:33 +0300742 if (ret == -ENOSPC)
743 break;
Michal Kazior7aa7a722014-08-25 12:09:38 +0200744 ath10k_warn(ar, "failed to post pci rx buf: %d\n", ret);
Michal Kazior728f95e2014-08-22 14:33:14 +0200745 mod_timer(&ar_pci->rx_post_retry, jiffies +
746 ATH10K_PCI_RX_POST_RETRY_MS);
747 break;
748 }
749 }
750}
751
Michal Kazior728f95e2014-08-22 14:33:14 +0200752static void ath10k_pci_rx_post(struct ath10k *ar)
753{
754 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
755 int i;
756
Michal Kazior728f95e2014-08-22 14:33:14 +0200757 for (i = 0; i < CE_COUNT; i++)
Rajkumar Manoharanab4e3db2015-10-06 15:19:33 +0300758 ath10k_pci_rx_post_pipe(&ar_pci->pipe_info[i]);
Michal Kazior728f95e2014-08-22 14:33:14 +0200759}
760
761static void ath10k_pci_rx_replenish_retry(unsigned long ptr)
762{
763 struct ath10k *ar = (void *)ptr;
764
765 ath10k_pci_rx_post(ar);
Michal Kaziorab977bd2013-11-25 14:06:26 +0100766}
767
Vasanthakumar Thiagarajan418ca592015-06-18 12:31:05 +0530768static u32 ath10k_pci_targ_cpu_to_ce_addr(struct ath10k *ar, u32 addr)
769{
770 u32 val = 0;
771
772 switch (ar->hw_rev) {
773 case ATH10K_HW_QCA988X:
774 case ATH10K_HW_QCA6174:
775 val = (ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
776 CORE_CTRL_ADDRESS) &
Vasanthakumar Thiagarajan3c7e2562015-07-03 19:25:27 +0530777 0x7ff) << 21;
Vasanthakumar Thiagarajan418ca592015-06-18 12:31:05 +0530778 break;
779 case ATH10K_HW_QCA99X0:
780 val = ath10k_pci_read32(ar, PCIE_BAR_REG_ADDRESS);
781 break;
782 }
783
784 val |= 0x100000 | (addr & 0xfffff);
785 return val;
786}
787
Kalle Valo5e3dd152013-06-12 20:52:10 +0300788/*
789 * Diagnostic read/write access is provided for startup/config/debug usage.
790 * Caller must guarantee proper alignment, when applicable, and single user
791 * at any moment.
792 */
793static int ath10k_pci_diag_read_mem(struct ath10k *ar, u32 address, void *data,
794 int nbytes)
795{
796 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
797 int ret = 0;
798 u32 buf;
799 unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
800 unsigned int id;
801 unsigned int flags;
Michal Kazior2aa39112013-08-27 13:08:02 +0200802 struct ath10k_ce_pipe *ce_diag;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300803 /* Host buffer address in CE space */
804 u32 ce_data;
805 dma_addr_t ce_data_base = 0;
806 void *data_buf = NULL;
807 int i;
808
Kalle Valoeef25402014-09-24 14:16:52 +0300809 spin_lock_bh(&ar_pci->ce_lock);
810
Kalle Valo5e3dd152013-06-12 20:52:10 +0300811 ce_diag = ar_pci->ce_diag;
812
813 /*
814 * Allocate a temporary bounce buffer to hold caller's data
815 * to be DMA'ed from Target. This guarantees
816 * 1) 4-byte alignment
817 * 2) Buffer in DMA-able space
818 */
819 orig_nbytes = nbytes;
Michal Kazior68c03242014-03-28 10:02:35 +0200820 data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
821 orig_nbytes,
822 &ce_data_base,
823 GFP_ATOMIC);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300824
825 if (!data_buf) {
826 ret = -ENOMEM;
827 goto done;
828 }
829 memset(data_buf, 0, orig_nbytes);
830
831 remaining_bytes = orig_nbytes;
832 ce_data = ce_data_base;
833 while (remaining_bytes) {
834 nbytes = min_t(unsigned int, remaining_bytes,
835 DIAG_TRANSFER_LIMIT);
836
Kalle Valoeef25402014-09-24 14:16:52 +0300837 ret = __ath10k_ce_rx_post_buf(ce_diag, NULL, ce_data);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300838 if (ret != 0)
839 goto done;
840
841 /* Request CE to send from Target(!) address to Host buffer */
842 /*
843 * The address supplied by the caller is in the
844 * Target CPU virtual address space.
845 *
846 * In order to use this address with the diagnostic CE,
847 * convert it from Target CPU virtual address space
848 * to CE address space
849 */
Vasanthakumar Thiagarajan418ca592015-06-18 12:31:05 +0530850 address = ath10k_pci_targ_cpu_to_ce_addr(ar, address);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300851
Kalle Valoeef25402014-09-24 14:16:52 +0300852 ret = ath10k_ce_send_nolock(ce_diag, NULL, (u32)address, nbytes, 0,
853 0);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300854 if (ret)
855 goto done;
856
857 i = 0;
Kalle Valoeef25402014-09-24 14:16:52 +0300858 while (ath10k_ce_completed_send_next_nolock(ce_diag, NULL, &buf,
859 &completed_nbytes,
860 &id) != 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300861 mdelay(1);
862 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
863 ret = -EBUSY;
864 goto done;
865 }
866 }
867
868 if (nbytes != completed_nbytes) {
869 ret = -EIO;
870 goto done;
871 }
872
Kalle Valocfbc06a2014-09-14 12:50:23 +0300873 if (buf != (u32)address) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300874 ret = -EIO;
875 goto done;
876 }
877
878 i = 0;
Kalle Valoeef25402014-09-24 14:16:52 +0300879 while (ath10k_ce_completed_recv_next_nolock(ce_diag, NULL, &buf,
880 &completed_nbytes,
881 &id, &flags) != 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300882 mdelay(1);
883
884 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
885 ret = -EBUSY;
886 goto done;
887 }
888 }
889
890 if (nbytes != completed_nbytes) {
891 ret = -EIO;
892 goto done;
893 }
894
895 if (buf != ce_data) {
896 ret = -EIO;
897 goto done;
898 }
899
900 remaining_bytes -= nbytes;
901 address += nbytes;
902 ce_data += nbytes;
903 }
904
905done:
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300906 if (ret == 0)
907 memcpy(data, data_buf, orig_nbytes);
908 else
Michal Kazior7aa7a722014-08-25 12:09:38 +0200909 ath10k_warn(ar, "failed to read diag value at 0x%x: %d\n",
Kalle Valo50f87a62014-03-28 09:32:52 +0200910 address, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300911
912 if (data_buf)
Michal Kazior68c03242014-03-28 10:02:35 +0200913 dma_free_coherent(ar->dev, orig_nbytes, data_buf,
914 ce_data_base);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300915
Kalle Valoeef25402014-09-24 14:16:52 +0300916 spin_unlock_bh(&ar_pci->ce_lock);
917
Kalle Valo5e3dd152013-06-12 20:52:10 +0300918 return ret;
919}
920
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300921static int ath10k_pci_diag_read32(struct ath10k *ar, u32 address, u32 *value)
922{
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300923 __le32 val = 0;
924 int ret;
925
926 ret = ath10k_pci_diag_read_mem(ar, address, &val, sizeof(val));
927 *value = __le32_to_cpu(val);
928
929 return ret;
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300930}
931
932static int __ath10k_pci_diag_read_hi(struct ath10k *ar, void *dest,
933 u32 src, u32 len)
934{
935 u32 host_addr, addr;
936 int ret;
937
938 host_addr = host_interest_item_address(src);
939
940 ret = ath10k_pci_diag_read32(ar, host_addr, &addr);
941 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200942 ath10k_warn(ar, "failed to get memcpy hi address for firmware address %d: %d\n",
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300943 src, ret);
944 return ret;
945 }
946
947 ret = ath10k_pci_diag_read_mem(ar, addr, dest, len);
948 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200949 ath10k_warn(ar, "failed to memcpy firmware memory from %d (%d B): %d\n",
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300950 addr, len, ret);
951 return ret;
952 }
953
954 return 0;
955}
956
957#define ath10k_pci_diag_read_hi(ar, dest, src, len) \
Kalle Valo8cc7f262014-09-14 12:50:39 +0300958 __ath10k_pci_diag_read_hi(ar, dest, HI_ITEM(src), len)
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300959
Kalle Valo5e3dd152013-06-12 20:52:10 +0300960static int ath10k_pci_diag_write_mem(struct ath10k *ar, u32 address,
961 const void *data, int nbytes)
962{
963 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
964 int ret = 0;
965 u32 buf;
966 unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
967 unsigned int id;
968 unsigned int flags;
Michal Kazior2aa39112013-08-27 13:08:02 +0200969 struct ath10k_ce_pipe *ce_diag;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300970 void *data_buf = NULL;
971 u32 ce_data; /* Host buffer address in CE space */
972 dma_addr_t ce_data_base = 0;
973 int i;
974
Kalle Valoeef25402014-09-24 14:16:52 +0300975 spin_lock_bh(&ar_pci->ce_lock);
976
Kalle Valo5e3dd152013-06-12 20:52:10 +0300977 ce_diag = ar_pci->ce_diag;
978
979 /*
980 * Allocate a temporary bounce buffer to hold caller's data
981 * to be DMA'ed to Target. This guarantees
982 * 1) 4-byte alignment
983 * 2) Buffer in DMA-able space
984 */
985 orig_nbytes = nbytes;
Michal Kazior68c03242014-03-28 10:02:35 +0200986 data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
987 orig_nbytes,
988 &ce_data_base,
989 GFP_ATOMIC);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300990 if (!data_buf) {
991 ret = -ENOMEM;
992 goto done;
993 }
994
995 /* Copy caller's data to allocated DMA buf */
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300996 memcpy(data_buf, data, orig_nbytes);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300997
998 /*
999 * The address supplied by the caller is in the
1000 * Target CPU virtual address space.
1001 *
1002 * In order to use this address with the diagnostic CE,
1003 * convert it from
1004 * Target CPU virtual address space
1005 * to
1006 * CE address space
1007 */
Vasanthakumar Thiagarajan418ca592015-06-18 12:31:05 +05301008 address = ath10k_pci_targ_cpu_to_ce_addr(ar, address);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001009
1010 remaining_bytes = orig_nbytes;
1011 ce_data = ce_data_base;
1012 while (remaining_bytes) {
1013 /* FIXME: check cast */
1014 nbytes = min_t(int, remaining_bytes, DIAG_TRANSFER_LIMIT);
1015
1016 /* Set up to receive directly into Target(!) address */
Kalle Valoeef25402014-09-24 14:16:52 +03001017 ret = __ath10k_ce_rx_post_buf(ce_diag, NULL, address);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001018 if (ret != 0)
1019 goto done;
1020
1021 /*
1022 * Request CE to send caller-supplied data that
1023 * was copied to bounce buffer to Target(!) address.
1024 */
Kalle Valoeef25402014-09-24 14:16:52 +03001025 ret = ath10k_ce_send_nolock(ce_diag, NULL, (u32)ce_data,
1026 nbytes, 0, 0);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001027 if (ret != 0)
1028 goto done;
1029
1030 i = 0;
Kalle Valoeef25402014-09-24 14:16:52 +03001031 while (ath10k_ce_completed_send_next_nolock(ce_diag, NULL, &buf,
1032 &completed_nbytes,
1033 &id) != 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001034 mdelay(1);
1035
1036 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
1037 ret = -EBUSY;
1038 goto done;
1039 }
1040 }
1041
1042 if (nbytes != completed_nbytes) {
1043 ret = -EIO;
1044 goto done;
1045 }
1046
1047 if (buf != ce_data) {
1048 ret = -EIO;
1049 goto done;
1050 }
1051
1052 i = 0;
Kalle Valoeef25402014-09-24 14:16:52 +03001053 while (ath10k_ce_completed_recv_next_nolock(ce_diag, NULL, &buf,
1054 &completed_nbytes,
1055 &id, &flags) != 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001056 mdelay(1);
1057
1058 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
1059 ret = -EBUSY;
1060 goto done;
1061 }
1062 }
1063
1064 if (nbytes != completed_nbytes) {
1065 ret = -EIO;
1066 goto done;
1067 }
1068
1069 if (buf != address) {
1070 ret = -EIO;
1071 goto done;
1072 }
1073
1074 remaining_bytes -= nbytes;
1075 address += nbytes;
1076 ce_data += nbytes;
1077 }
1078
1079done:
1080 if (data_buf) {
Michal Kazior68c03242014-03-28 10:02:35 +02001081 dma_free_coherent(ar->dev, orig_nbytes, data_buf,
1082 ce_data_base);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001083 }
1084
1085 if (ret != 0)
Michal Kazior7aa7a722014-08-25 12:09:38 +02001086 ath10k_warn(ar, "failed to write diag value at 0x%x: %d\n",
Kalle Valo50f87a62014-03-28 09:32:52 +02001087 address, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001088
Kalle Valoeef25402014-09-24 14:16:52 +03001089 spin_unlock_bh(&ar_pci->ce_lock);
1090
Kalle Valo5e3dd152013-06-12 20:52:10 +03001091 return ret;
1092}
1093
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001094static int ath10k_pci_diag_write32(struct ath10k *ar, u32 address, u32 value)
1095{
1096 __le32 val = __cpu_to_le32(value);
1097
1098 return ath10k_pci_diag_write_mem(ar, address, &val, sizeof(val));
1099}
1100
Kalle Valo5e3dd152013-06-12 20:52:10 +03001101/* Called by lower (CE) layer when a send to Target completes. */
Rajkumar Manoharan0e5b2952015-10-12 18:27:01 +05301102static void ath10k_pci_htc_tx_cb(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001103{
1104 struct ath10k *ar = ce_state->ar;
Michal Kazior1cb86d42014-11-27 11:09:38 +01001105 struct sk_buff_head list;
1106 struct sk_buff *skb;
Michal Kazior5440ce22013-09-03 15:09:58 +02001107 u32 ce_data;
1108 unsigned int nbytes;
1109 unsigned int transfer_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001110
Michal Kazior1cb86d42014-11-27 11:09:38 +01001111 __skb_queue_head_init(&list);
1112 while (ath10k_ce_completed_send_next(ce_state, (void **)&skb, &ce_data,
1113 &nbytes, &transfer_id) == 0) {
Michal Kaziora16942e2014-02-27 18:50:04 +02001114 /* no need to call tx completion for NULL pointers */
Michal Kazior1cb86d42014-11-27 11:09:38 +01001115 if (skb == NULL)
Michal Kazior726346f2014-02-27 18:50:04 +02001116 continue;
1117
Michal Kazior1cb86d42014-11-27 11:09:38 +01001118 __skb_queue_tail(&list, skb);
Michal Kazior5440ce22013-09-03 15:09:58 +02001119 }
Michal Kazior1cb86d42014-11-27 11:09:38 +01001120
1121 while ((skb = __skb_dequeue(&list)))
Rajkumar Manoharan0e5b2952015-10-12 18:27:01 +05301122 ath10k_htc_tx_completion_handler(ar, skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001123}
1124
1125/* Called by lower (CE) layer when data is received from the Target. */
Michal Kazior5440ce22013-09-03 15:09:58 +02001126static void ath10k_pci_ce_recv_data(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001127{
1128 struct ath10k *ar = ce_state->ar;
1129 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +02001130 struct ath10k_pci_pipe *pipe_info = &ar_pci->pipe_info[ce_state->id];
Michal Kazior2f5280d2014-02-27 18:50:05 +02001131 struct ath10k_hif_cb *cb = &ar_pci->msg_callbacks_current;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001132 struct sk_buff *skb;
Michal Kazior1cb86d42014-11-27 11:09:38 +01001133 struct sk_buff_head list;
Michal Kazior5440ce22013-09-03 15:09:58 +02001134 void *transfer_context;
1135 u32 ce_data;
Michal Kazior2f5280d2014-02-27 18:50:05 +02001136 unsigned int nbytes, max_nbytes;
Michal Kazior5440ce22013-09-03 15:09:58 +02001137 unsigned int transfer_id;
1138 unsigned int flags;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001139
Michal Kazior1cb86d42014-11-27 11:09:38 +01001140 __skb_queue_head_init(&list);
Michal Kazior5440ce22013-09-03 15:09:58 +02001141 while (ath10k_ce_completed_recv_next(ce_state, &transfer_context,
1142 &ce_data, &nbytes, &transfer_id,
1143 &flags) == 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001144 skb = transfer_context;
Michal Kazior2f5280d2014-02-27 18:50:05 +02001145 max_nbytes = skb->len + skb_tailroom(skb);
Michal Kazior8582bf32015-01-24 12:14:47 +02001146 dma_unmap_single(ar->dev, ATH10K_SKB_RXCB(skb)->paddr,
Michal Kazior2f5280d2014-02-27 18:50:05 +02001147 max_nbytes, DMA_FROM_DEVICE);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001148
Michal Kazior2f5280d2014-02-27 18:50:05 +02001149 if (unlikely(max_nbytes < nbytes)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001150 ath10k_warn(ar, "rxed more than expected (nbytes %d, max %d)",
Michal Kazior2f5280d2014-02-27 18:50:05 +02001151 nbytes, max_nbytes);
1152 dev_kfree_skb_any(skb);
1153 continue;
1154 }
1155
1156 skb_put(skb, nbytes);
Michal Kazior1cb86d42014-11-27 11:09:38 +01001157 __skb_queue_tail(&list, skb);
1158 }
Michal Kaziora360e542014-09-23 10:22:54 +02001159
Michal Kazior1cb86d42014-11-27 11:09:38 +01001160 while ((skb = __skb_dequeue(&list))) {
Michal Kaziora360e542014-09-23 10:22:54 +02001161 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci rx ce pipe %d len %d\n",
1162 ce_state->id, skb->len);
1163 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci rx: ",
1164 skb->data, skb->len);
1165
Michal Kazior5f07ea42014-11-27 11:09:36 +01001166 cb->rx_completion(ar, skb);
Michal Kazior2f5280d2014-02-27 18:50:05 +02001167 }
Michal Kaziorc29a3802014-07-21 21:03:10 +03001168
Michal Kazior728f95e2014-08-22 14:33:14 +02001169 ath10k_pci_rx_post_pipe(pipe_info);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001170}
1171
Michal Kazior726346f2014-02-27 18:50:04 +02001172static int ath10k_pci_hif_tx_sg(struct ath10k *ar, u8 pipe_id,
1173 struct ath10k_hif_sg_item *items, int n_items)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001174{
Kalle Valo5e3dd152013-06-12 20:52:10 +03001175 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior726346f2014-02-27 18:50:04 +02001176 struct ath10k_pci_pipe *pci_pipe = &ar_pci->pipe_info[pipe_id];
1177 struct ath10k_ce_pipe *ce_pipe = pci_pipe->ce_hdl;
1178 struct ath10k_ce_ring *src_ring = ce_pipe->src_ring;
Michal Kazior7147a132014-05-26 12:02:58 +02001179 unsigned int nentries_mask;
1180 unsigned int sw_index;
1181 unsigned int write_index;
Michal Kazior08b8aa02014-05-26 12:02:59 +02001182 int err, i = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001183
Michal Kazior726346f2014-02-27 18:50:04 +02001184 spin_lock_bh(&ar_pci->ce_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001185
Michal Kazior7147a132014-05-26 12:02:58 +02001186 nentries_mask = src_ring->nentries_mask;
1187 sw_index = src_ring->sw_index;
1188 write_index = src_ring->write_index;
1189
Michal Kazior726346f2014-02-27 18:50:04 +02001190 if (unlikely(CE_RING_DELTA(nentries_mask,
1191 write_index, sw_index - 1) < n_items)) {
1192 err = -ENOBUFS;
Michal Kazior08b8aa02014-05-26 12:02:59 +02001193 goto err;
Michal Kazior726346f2014-02-27 18:50:04 +02001194 }
1195
1196 for (i = 0; i < n_items - 1; i++) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001197 ath10k_dbg(ar, ATH10K_DBG_PCI,
Michal Kazior726346f2014-02-27 18:50:04 +02001198 "pci tx item %d paddr 0x%08x len %d n_items %d\n",
1199 i, items[i].paddr, items[i].len, n_items);
Michal Kazior7aa7a722014-08-25 12:09:38 +02001200 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci tx data: ",
Michal Kazior726346f2014-02-27 18:50:04 +02001201 items[i].vaddr, items[i].len);
1202
1203 err = ath10k_ce_send_nolock(ce_pipe,
1204 items[i].transfer_context,
1205 items[i].paddr,
1206 items[i].len,
1207 items[i].transfer_id,
1208 CE_SEND_FLAG_GATHER);
1209 if (err)
Michal Kazior08b8aa02014-05-26 12:02:59 +02001210 goto err;
Michal Kazior726346f2014-02-27 18:50:04 +02001211 }
1212
1213 /* `i` is equal to `n_items -1` after for() */
Kalle Valo5e3dd152013-06-12 20:52:10 +03001214
Michal Kazior7aa7a722014-08-25 12:09:38 +02001215 ath10k_dbg(ar, ATH10K_DBG_PCI,
Michal Kazior726346f2014-02-27 18:50:04 +02001216 "pci tx item %d paddr 0x%08x len %d n_items %d\n",
1217 i, items[i].paddr, items[i].len, n_items);
Michal Kazior7aa7a722014-08-25 12:09:38 +02001218 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci tx data: ",
Michal Kazior726346f2014-02-27 18:50:04 +02001219 items[i].vaddr, items[i].len);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001220
Michal Kazior726346f2014-02-27 18:50:04 +02001221 err = ath10k_ce_send_nolock(ce_pipe,
1222 items[i].transfer_context,
1223 items[i].paddr,
1224 items[i].len,
1225 items[i].transfer_id,
1226 0);
1227 if (err)
Michal Kazior08b8aa02014-05-26 12:02:59 +02001228 goto err;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001229
Michal Kazior08b8aa02014-05-26 12:02:59 +02001230 spin_unlock_bh(&ar_pci->ce_lock);
1231 return 0;
1232
1233err:
1234 for (; i > 0; i--)
1235 __ath10k_ce_send_revert(ce_pipe);
1236
Michal Kazior726346f2014-02-27 18:50:04 +02001237 spin_unlock_bh(&ar_pci->ce_lock);
1238 return err;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001239}
1240
Kalle Valoeef25402014-09-24 14:16:52 +03001241static int ath10k_pci_hif_diag_read(struct ath10k *ar, u32 address, void *buf,
1242 size_t buf_len)
1243{
1244 return ath10k_pci_diag_read_mem(ar, address, buf, buf_len);
1245}
1246
Kalle Valo5e3dd152013-06-12 20:52:10 +03001247static u16 ath10k_pci_hif_get_free_queue_number(struct ath10k *ar, u8 pipe)
1248{
1249 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo50f87a62014-03-28 09:32:52 +02001250
Michal Kazior7aa7a722014-08-25 12:09:38 +02001251 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif get free queue number\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001252
Michal Kazior3efcb3b2013-10-02 11:03:41 +02001253 return ath10k_ce_num_free_src_entries(ar_pci->pipe_info[pipe].ce_hdl);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001254}
1255
Ben Greear384914b2014-08-25 08:37:32 +03001256static void ath10k_pci_dump_registers(struct ath10k *ar,
1257 struct ath10k_fw_crash_data *crash_data)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001258{
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001259 __le32 reg_dump_values[REG_DUMP_COUNT_QCA988X] = {};
1260 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001261
Ben Greear384914b2014-08-25 08:37:32 +03001262 lockdep_assert_held(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001263
Kalle Valo3d29a3e2014-08-25 08:37:26 +03001264 ret = ath10k_pci_diag_read_hi(ar, &reg_dump_values[0],
1265 hi_failure_state,
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001266 REG_DUMP_COUNT_QCA988X * sizeof(__le32));
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001267 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001268 ath10k_err(ar, "failed to read firmware dump area: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001269 return;
1270 }
1271
1272 BUILD_BUG_ON(REG_DUMP_COUNT_QCA988X % 4);
1273
Michal Kazior7aa7a722014-08-25 12:09:38 +02001274 ath10k_err(ar, "firmware register dump:\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001275 for (i = 0; i < REG_DUMP_COUNT_QCA988X; i += 4)
Michal Kazior7aa7a722014-08-25 12:09:38 +02001276 ath10k_err(ar, "[%02d]: 0x%08X 0x%08X 0x%08X 0x%08X\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001277 i,
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001278 __le32_to_cpu(reg_dump_values[i]),
1279 __le32_to_cpu(reg_dump_values[i + 1]),
1280 __le32_to_cpu(reg_dump_values[i + 2]),
1281 __le32_to_cpu(reg_dump_values[i + 3]));
Michal Kazioraffd3212013-07-16 09:54:35 +02001282
Michal Kazior1bbb1192014-08-25 12:13:14 +02001283 if (!crash_data)
1284 return;
1285
Ben Greear384914b2014-08-25 08:37:32 +03001286 for (i = 0; i < REG_DUMP_COUNT_QCA988X; i++)
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001287 crash_data->registers[i] = reg_dump_values[i];
Ben Greear384914b2014-08-25 08:37:32 +03001288}
1289
Kalle Valo0e9848c2014-08-25 08:37:37 +03001290static void ath10k_pci_fw_crashed_dump(struct ath10k *ar)
Ben Greear384914b2014-08-25 08:37:32 +03001291{
1292 struct ath10k_fw_crash_data *crash_data;
1293 char uuid[50];
1294
1295 spin_lock_bh(&ar->data_lock);
1296
Ben Greearf51dbe72014-09-29 14:41:46 +03001297 ar->stats.fw_crash_counter++;
1298
Ben Greear384914b2014-08-25 08:37:32 +03001299 crash_data = ath10k_debug_get_new_fw_crash_data(ar);
1300
1301 if (crash_data)
1302 scnprintf(uuid, sizeof(uuid), "%pUl", &crash_data->uuid);
1303 else
1304 scnprintf(uuid, sizeof(uuid), "n/a");
1305
Michal Kazior7aa7a722014-08-25 12:09:38 +02001306 ath10k_err(ar, "firmware crashed! (uuid %s)\n", uuid);
Kalle Valo8a0c7972014-08-25 08:37:45 +03001307 ath10k_print_driver_info(ar);
Ben Greear384914b2014-08-25 08:37:32 +03001308 ath10k_pci_dump_registers(ar, crash_data);
1309
Ben Greear384914b2014-08-25 08:37:32 +03001310 spin_unlock_bh(&ar->data_lock);
Michal Kazioraffd3212013-07-16 09:54:35 +02001311
Michal Kazior5e90de82013-10-16 16:46:05 +03001312 queue_work(ar->workqueue, &ar->restart_work);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001313}
1314
1315static void ath10k_pci_hif_send_complete_check(struct ath10k *ar, u8 pipe,
1316 int force)
1317{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001318 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif send complete check\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001319
Kalle Valo5e3dd152013-06-12 20:52:10 +03001320 if (!force) {
1321 int resources;
1322 /*
1323 * Decide whether to actually poll for completions, or just
1324 * wait for a later chance.
1325 * If there seem to be plenty of resources left, then just wait
1326 * since checking involves reading a CE register, which is a
1327 * relatively expensive operation.
1328 */
1329 resources = ath10k_pci_hif_get_free_queue_number(ar, pipe);
1330
1331 /*
1332 * If at least 50% of the total resources are still available,
1333 * don't bother checking again yet.
1334 */
1335 if (resources > (host_ce_config_wlan[pipe].src_nentries >> 1))
1336 return;
1337 }
1338 ath10k_ce_per_engine_service(ar, pipe);
1339}
1340
Michal Kaziore799bbf2013-07-05 16:15:12 +03001341static void ath10k_pci_hif_set_callbacks(struct ath10k *ar,
1342 struct ath10k_hif_cb *callbacks)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001343{
1344 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1345
Michal Kazior7aa7a722014-08-25 12:09:38 +02001346 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif set callbacks\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001347
1348 memcpy(&ar_pci->msg_callbacks_current, callbacks,
1349 sizeof(ar_pci->msg_callbacks_current));
1350}
1351
Michal Kazior96a9d0d2013-11-08 08:01:25 +01001352static void ath10k_pci_kill_tasklet(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001353{
1354 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001355 int i;
1356
Kalle Valo5e3dd152013-06-12 20:52:10 +03001357 tasklet_kill(&ar_pci->intr_tq);
Michal Kazior103d4f52013-11-08 08:01:24 +01001358 tasklet_kill(&ar_pci->msi_fw_err);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001359
1360 for (i = 0; i < CE_COUNT; i++)
1361 tasklet_kill(&ar_pci->pipe_info[i].intr);
Michal Kazior728f95e2014-08-22 14:33:14 +02001362
1363 del_timer_sync(&ar_pci->rx_post_retry);
Michal Kazior96a9d0d2013-11-08 08:01:25 +01001364}
1365
Kalle Valo5e3dd152013-06-12 20:52:10 +03001366static int ath10k_pci_hif_map_service_to_pipe(struct ath10k *ar,
1367 u16 service_id, u8 *ul_pipe,
1368 u8 *dl_pipe, int *ul_is_polled,
1369 int *dl_is_polled)
1370{
Michal Kazior7c6aa252014-08-26 19:14:03 +03001371 const struct service_to_pipe *entry;
1372 bool ul_set = false, dl_set = false;
1373 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001374
Michal Kazior7aa7a722014-08-25 12:09:38 +02001375 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif map service\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001376
Kalle Valo5e3dd152013-06-12 20:52:10 +03001377 /* polling for received messages not supported */
1378 *dl_is_polled = 0;
1379
Michal Kazior7c6aa252014-08-26 19:14:03 +03001380 for (i = 0; i < ARRAY_SIZE(target_service_to_ce_map_wlan); i++) {
1381 entry = &target_service_to_ce_map_wlan[i];
Kalle Valo5e3dd152013-06-12 20:52:10 +03001382
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001383 if (__le32_to_cpu(entry->service_id) != service_id)
Michal Kazior7c6aa252014-08-26 19:14:03 +03001384 continue;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001385
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001386 switch (__le32_to_cpu(entry->pipedir)) {
Michal Kazior7c6aa252014-08-26 19:14:03 +03001387 case PIPEDIR_NONE:
1388 break;
1389 case PIPEDIR_IN:
1390 WARN_ON(dl_set);
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001391 *dl_pipe = __le32_to_cpu(entry->pipenum);
Michal Kazior7c6aa252014-08-26 19:14:03 +03001392 dl_set = true;
1393 break;
1394 case PIPEDIR_OUT:
1395 WARN_ON(ul_set);
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001396 *ul_pipe = __le32_to_cpu(entry->pipenum);
Michal Kazior7c6aa252014-08-26 19:14:03 +03001397 ul_set = true;
1398 break;
1399 case PIPEDIR_INOUT:
1400 WARN_ON(dl_set);
1401 WARN_ON(ul_set);
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001402 *dl_pipe = __le32_to_cpu(entry->pipenum);
1403 *ul_pipe = __le32_to_cpu(entry->pipenum);
Michal Kazior7c6aa252014-08-26 19:14:03 +03001404 dl_set = true;
1405 ul_set = true;
1406 break;
1407 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001408 }
Michal Kazior7c6aa252014-08-26 19:14:03 +03001409
1410 if (WARN_ON(!ul_set || !dl_set))
1411 return -ENOENT;
1412
Kalle Valo5e3dd152013-06-12 20:52:10 +03001413 *ul_is_polled =
1414 (host_ce_config_wlan[*ul_pipe].flags & CE_ATTR_DIS_INTR) != 0;
1415
Michal Kazior7c6aa252014-08-26 19:14:03 +03001416 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001417}
1418
1419static void ath10k_pci_hif_get_default_pipe(struct ath10k *ar,
Kalle Valo5b07e072014-09-14 12:50:06 +03001420 u8 *ul_pipe, u8 *dl_pipe)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001421{
1422 int ul_is_polled, dl_is_polled;
1423
Michal Kazior7aa7a722014-08-25 12:09:38 +02001424 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif get default pipe\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001425
Kalle Valo5e3dd152013-06-12 20:52:10 +03001426 (void)ath10k_pci_hif_map_service_to_pipe(ar,
1427 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1428 ul_pipe,
1429 dl_pipe,
1430 &ul_is_polled,
1431 &dl_is_polled);
1432}
1433
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001434static void ath10k_pci_irq_msi_fw_mask(struct ath10k *ar)
1435{
1436 u32 val;
1437
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +05301438 switch (ar->hw_rev) {
1439 case ATH10K_HW_QCA988X:
1440 case ATH10K_HW_QCA6174:
1441 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1442 CORE_CTRL_ADDRESS);
1443 val &= ~CORE_CTRL_PCIE_REG_31_MASK;
1444 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
1445 CORE_CTRL_ADDRESS, val);
1446 break;
1447 case ATH10K_HW_QCA99X0:
1448 /* TODO: Find appropriate register configuration for QCA99X0
1449 * to mask irq/MSI.
1450 */
1451 break;
1452 }
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001453}
1454
1455static void ath10k_pci_irq_msi_fw_unmask(struct ath10k *ar)
1456{
1457 u32 val;
1458
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +05301459 switch (ar->hw_rev) {
1460 case ATH10K_HW_QCA988X:
1461 case ATH10K_HW_QCA6174:
1462 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1463 CORE_CTRL_ADDRESS);
1464 val |= CORE_CTRL_PCIE_REG_31_MASK;
1465 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
1466 CORE_CTRL_ADDRESS, val);
1467 break;
1468 case ATH10K_HW_QCA99X0:
1469 /* TODO: Find appropriate register configuration for QCA99X0
1470 * to unmask irq/MSI.
1471 */
1472 break;
1473 }
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001474}
1475
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001476static void ath10k_pci_irq_disable(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001477{
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001478 ath10k_ce_disable_interrupts(ar);
1479 ath10k_pci_disable_and_clear_legacy_irq(ar);
1480 ath10k_pci_irq_msi_fw_mask(ar);
1481}
1482
1483static void ath10k_pci_irq_sync(struct ath10k *ar)
1484{
Kalle Valo5e3dd152013-06-12 20:52:10 +03001485 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001486 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001487
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001488 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
1489 synchronize_irq(ar_pci->pdev->irq + i);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001490}
1491
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001492static void ath10k_pci_irq_enable(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001493{
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001494 ath10k_ce_enable_interrupts(ar);
Michal Kaziore75db4e2014-08-28 22:14:16 +03001495 ath10k_pci_enable_legacy_irq(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001496 ath10k_pci_irq_msi_fw_unmask(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001497}
1498
1499static int ath10k_pci_hif_start(struct ath10k *ar)
1500{
Janusz Dziedzic76d870e2015-05-18 09:38:16 +00001501 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo9a149692015-10-05 17:56:36 +03001502
Michal Kazior7aa7a722014-08-25 12:09:38 +02001503 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif start\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001504
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001505 ath10k_pci_irq_enable(ar);
Michal Kazior728f95e2014-08-22 14:33:14 +02001506 ath10k_pci_rx_post(ar);
Kalle Valo50f87a62014-03-28 09:32:52 +02001507
Janusz Dziedzic76d870e2015-05-18 09:38:16 +00001508 pcie_capability_write_word(ar_pci->pdev, PCI_EXP_LNKCTL,
1509 ar_pci->link_ctl);
1510
Kalle Valo5e3dd152013-06-12 20:52:10 +03001511 return 0;
1512}
1513
Michal Kazior099ac7c2014-10-28 10:32:05 +01001514static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe *pci_pipe)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001515{
1516 struct ath10k *ar;
Michal Kazior099ac7c2014-10-28 10:32:05 +01001517 struct ath10k_ce_pipe *ce_pipe;
1518 struct ath10k_ce_ring *ce_ring;
1519 struct sk_buff *skb;
1520 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001521
Michal Kazior099ac7c2014-10-28 10:32:05 +01001522 ar = pci_pipe->hif_ce_state;
1523 ce_pipe = pci_pipe->ce_hdl;
1524 ce_ring = ce_pipe->dest_ring;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001525
Michal Kazior099ac7c2014-10-28 10:32:05 +01001526 if (!ce_ring)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001527 return;
1528
Michal Kazior099ac7c2014-10-28 10:32:05 +01001529 if (!pci_pipe->buf_sz)
1530 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001531
Michal Kazior099ac7c2014-10-28 10:32:05 +01001532 for (i = 0; i < ce_ring->nentries; i++) {
1533 skb = ce_ring->per_transfer_context[i];
1534 if (!skb)
1535 continue;
1536
1537 ce_ring->per_transfer_context[i] = NULL;
1538
Michal Kazior8582bf32015-01-24 12:14:47 +02001539 dma_unmap_single(ar->dev, ATH10K_SKB_RXCB(skb)->paddr,
Michal Kazior099ac7c2014-10-28 10:32:05 +01001540 skb->len + skb_tailroom(skb),
Kalle Valo5e3dd152013-06-12 20:52:10 +03001541 DMA_FROM_DEVICE);
Michal Kazior099ac7c2014-10-28 10:32:05 +01001542 dev_kfree_skb_any(skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001543 }
1544}
1545
Michal Kazior099ac7c2014-10-28 10:32:05 +01001546static void ath10k_pci_tx_pipe_cleanup(struct ath10k_pci_pipe *pci_pipe)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001547{
1548 struct ath10k *ar;
1549 struct ath10k_pci *ar_pci;
Michal Kazior099ac7c2014-10-28 10:32:05 +01001550 struct ath10k_ce_pipe *ce_pipe;
1551 struct ath10k_ce_ring *ce_ring;
1552 struct ce_desc *ce_desc;
1553 struct sk_buff *skb;
Michal Kazior099ac7c2014-10-28 10:32:05 +01001554 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001555
Michal Kazior099ac7c2014-10-28 10:32:05 +01001556 ar = pci_pipe->hif_ce_state;
1557 ar_pci = ath10k_pci_priv(ar);
1558 ce_pipe = pci_pipe->ce_hdl;
1559 ce_ring = ce_pipe->src_ring;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001560
Michal Kazior099ac7c2014-10-28 10:32:05 +01001561 if (!ce_ring)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001562 return;
1563
Michal Kazior099ac7c2014-10-28 10:32:05 +01001564 if (!pci_pipe->buf_sz)
1565 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001566
Michal Kazior099ac7c2014-10-28 10:32:05 +01001567 ce_desc = ce_ring->shadow_base;
1568 if (WARN_ON(!ce_desc))
1569 return;
1570
1571 for (i = 0; i < ce_ring->nentries; i++) {
1572 skb = ce_ring->per_transfer_context[i];
1573 if (!skb)
Michal Kazior2415fc12013-11-08 08:01:32 +01001574 continue;
Michal Kazior2415fc12013-11-08 08:01:32 +01001575
Michal Kazior099ac7c2014-10-28 10:32:05 +01001576 ce_ring->per_transfer_context[i] = NULL;
Michal Kazior099ac7c2014-10-28 10:32:05 +01001577
Rajkumar Manoharan0e5b2952015-10-12 18:27:01 +05301578 ath10k_htc_tx_completion_handler(ar, skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001579 }
1580}
1581
1582/*
1583 * Cleanup residual buffers for device shutdown:
1584 * buffers that were enqueued for receive
1585 * buffers that were to be sent
1586 * Note: Buffers that had completed but which were
1587 * not yet processed are on a completion queue. They
1588 * are handled when the completion thread shuts down.
1589 */
1590static void ath10k_pci_buffer_cleanup(struct ath10k *ar)
1591{
1592 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1593 int pipe_num;
1594
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001595 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Michal Kazior87263e52013-08-27 13:08:01 +02001596 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001597
1598 pipe_info = &ar_pci->pipe_info[pipe_num];
1599 ath10k_pci_rx_pipe_cleanup(pipe_info);
1600 ath10k_pci_tx_pipe_cleanup(pipe_info);
1601 }
1602}
1603
1604static void ath10k_pci_ce_deinit(struct ath10k *ar)
1605{
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001606 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001607
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001608 for (i = 0; i < CE_COUNT; i++)
1609 ath10k_ce_deinit_pipe(ar, i);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001610}
1611
Michal Kazior728f95e2014-08-22 14:33:14 +02001612static void ath10k_pci_flush(struct ath10k *ar)
1613{
1614 ath10k_pci_kill_tasklet(ar);
1615 ath10k_pci_buffer_cleanup(ar);
1616}
1617
Kalle Valo5e3dd152013-06-12 20:52:10 +03001618static void ath10k_pci_hif_stop(struct ath10k *ar)
1619{
Michal Kazior77258d42015-05-18 09:38:18 +00001620 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1621 unsigned long flags;
1622
Michal Kazior7aa7a722014-08-25 12:09:38 +02001623 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif stop\n");
Michal Kazior32270b62013-08-02 09:15:47 +02001624
Michal Kazior10d23db2014-08-22 14:33:15 +02001625 /* Most likely the device has HTT Rx ring configured. The only way to
1626 * prevent the device from accessing (and possible corrupting) host
1627 * memory is to reset the chip now.
Michal Kaziore75db4e2014-08-28 22:14:16 +03001628 *
1629 * There's also no known way of masking MSI interrupts on the device.
1630 * For ranged MSI the CE-related interrupts can be masked. However
1631 * regardless how many MSI interrupts are assigned the first one
1632 * is always used for firmware indications (crashes) and cannot be
1633 * masked. To prevent the device from asserting the interrupt reset it
1634 * before proceeding with cleanup.
Michal Kazior10d23db2014-08-22 14:33:15 +02001635 */
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +05301636 ath10k_pci_safe_chip_reset(ar);
Michal Kaziore75db4e2014-08-28 22:14:16 +03001637
1638 ath10k_pci_irq_disable(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001639 ath10k_pci_irq_sync(ar);
Michal Kaziore75db4e2014-08-28 22:14:16 +03001640 ath10k_pci_flush(ar);
Michal Kazior77258d42015-05-18 09:38:18 +00001641
1642 spin_lock_irqsave(&ar_pci->ps_lock, flags);
1643 WARN_ON(ar_pci->ps_wake_refcount > 0);
1644 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001645}
1646
1647static int ath10k_pci_hif_exchange_bmi_msg(struct ath10k *ar,
1648 void *req, u32 req_len,
1649 void *resp, u32 *resp_len)
1650{
1651 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2aa39112013-08-27 13:08:02 +02001652 struct ath10k_pci_pipe *pci_tx = &ar_pci->pipe_info[BMI_CE_NUM_TO_TARG];
1653 struct ath10k_pci_pipe *pci_rx = &ar_pci->pipe_info[BMI_CE_NUM_TO_HOST];
1654 struct ath10k_ce_pipe *ce_tx = pci_tx->ce_hdl;
1655 struct ath10k_ce_pipe *ce_rx = pci_rx->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001656 dma_addr_t req_paddr = 0;
1657 dma_addr_t resp_paddr = 0;
1658 struct bmi_xfer xfer = {};
1659 void *treq, *tresp = NULL;
1660 int ret = 0;
1661
Michal Kazior85622cd2013-11-25 14:06:22 +01001662 might_sleep();
1663
Kalle Valo5e3dd152013-06-12 20:52:10 +03001664 if (resp && !resp_len)
1665 return -EINVAL;
1666
1667 if (resp && resp_len && *resp_len == 0)
1668 return -EINVAL;
1669
1670 treq = kmemdup(req, req_len, GFP_KERNEL);
1671 if (!treq)
1672 return -ENOMEM;
1673
1674 req_paddr = dma_map_single(ar->dev, treq, req_len, DMA_TO_DEVICE);
1675 ret = dma_mapping_error(ar->dev, req_paddr);
Michal Kazior5e55e3c2015-08-19 13:10:43 +02001676 if (ret) {
1677 ret = -EIO;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001678 goto err_dma;
Michal Kazior5e55e3c2015-08-19 13:10:43 +02001679 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001680
1681 if (resp && resp_len) {
1682 tresp = kzalloc(*resp_len, GFP_KERNEL);
1683 if (!tresp) {
1684 ret = -ENOMEM;
1685 goto err_req;
1686 }
1687
1688 resp_paddr = dma_map_single(ar->dev, tresp, *resp_len,
1689 DMA_FROM_DEVICE);
1690 ret = dma_mapping_error(ar->dev, resp_paddr);
Michal Kazior5e55e3c2015-08-19 13:10:43 +02001691 if (ret) {
1692 ret = EIO;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001693 goto err_req;
Michal Kazior5e55e3c2015-08-19 13:10:43 +02001694 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001695
1696 xfer.wait_for_resp = true;
1697 xfer.resp_len = 0;
1698
Michal Kazior728f95e2014-08-22 14:33:14 +02001699 ath10k_ce_rx_post_buf(ce_rx, &xfer, resp_paddr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001700 }
1701
Kalle Valo5e3dd152013-06-12 20:52:10 +03001702 ret = ath10k_ce_send(ce_tx, &xfer, req_paddr, req_len, -1, 0);
1703 if (ret)
1704 goto err_resp;
1705
Michal Kazior85622cd2013-11-25 14:06:22 +01001706 ret = ath10k_pci_bmi_wait(ce_tx, ce_rx, &xfer);
1707 if (ret) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001708 u32 unused_buffer;
1709 unsigned int unused_nbytes;
1710 unsigned int unused_id;
1711
Kalle Valo5e3dd152013-06-12 20:52:10 +03001712 ath10k_ce_cancel_send_next(ce_tx, NULL, &unused_buffer,
1713 &unused_nbytes, &unused_id);
1714 } else {
1715 /* non-zero means we did not time out */
1716 ret = 0;
1717 }
1718
1719err_resp:
1720 if (resp) {
1721 u32 unused_buffer;
1722
1723 ath10k_ce_revoke_recv_next(ce_rx, NULL, &unused_buffer);
1724 dma_unmap_single(ar->dev, resp_paddr,
1725 *resp_len, DMA_FROM_DEVICE);
1726 }
1727err_req:
1728 dma_unmap_single(ar->dev, req_paddr, req_len, DMA_TO_DEVICE);
1729
1730 if (ret == 0 && resp_len) {
1731 *resp_len = min(*resp_len, xfer.resp_len);
1732 memcpy(resp, tresp, xfer.resp_len);
1733 }
1734err_dma:
1735 kfree(treq);
1736 kfree(tresp);
1737
1738 return ret;
1739}
1740
Michal Kazior5440ce22013-09-03 15:09:58 +02001741static void ath10k_pci_bmi_send_done(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001742{
Michal Kazior5440ce22013-09-03 15:09:58 +02001743 struct bmi_xfer *xfer;
1744 u32 ce_data;
1745 unsigned int nbytes;
1746 unsigned int transfer_id;
1747
1748 if (ath10k_ce_completed_send_next(ce_state, (void **)&xfer, &ce_data,
1749 &nbytes, &transfer_id))
1750 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001751
Michal Kazior2374b182014-07-14 16:25:25 +03001752 xfer->tx_done = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001753}
1754
Michal Kazior5440ce22013-09-03 15:09:58 +02001755static void ath10k_pci_bmi_recv_data(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001756{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001757 struct ath10k *ar = ce_state->ar;
Michal Kazior5440ce22013-09-03 15:09:58 +02001758 struct bmi_xfer *xfer;
1759 u32 ce_data;
1760 unsigned int nbytes;
1761 unsigned int transfer_id;
1762 unsigned int flags;
1763
1764 if (ath10k_ce_completed_recv_next(ce_state, (void **)&xfer, &ce_data,
1765 &nbytes, &transfer_id, &flags))
1766 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001767
Michal Kazior04ed9df2014-10-28 10:34:36 +01001768 if (WARN_ON_ONCE(!xfer))
1769 return;
1770
Kalle Valo5e3dd152013-06-12 20:52:10 +03001771 if (!xfer->wait_for_resp) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001772 ath10k_warn(ar, "unexpected: BMI data received; ignoring\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001773 return;
1774 }
1775
1776 xfer->resp_len = nbytes;
Michal Kazior2374b182014-07-14 16:25:25 +03001777 xfer->rx_done = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001778}
1779
Michal Kazior85622cd2013-11-25 14:06:22 +01001780static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
1781 struct ath10k_ce_pipe *rx_pipe,
1782 struct bmi_xfer *xfer)
1783{
1784 unsigned long timeout = jiffies + BMI_COMMUNICATION_TIMEOUT_HZ;
1785
1786 while (time_before_eq(jiffies, timeout)) {
1787 ath10k_pci_bmi_send_done(tx_pipe);
1788 ath10k_pci_bmi_recv_data(rx_pipe);
1789
Michal Kazior2374b182014-07-14 16:25:25 +03001790 if (xfer->tx_done && (xfer->rx_done == xfer->wait_for_resp))
Michal Kazior85622cd2013-11-25 14:06:22 +01001791 return 0;
1792
1793 schedule();
1794 }
1795
1796 return -ETIMEDOUT;
1797}
1798
Kalle Valo5e3dd152013-06-12 20:52:10 +03001799/*
Kalle Valo5e3dd152013-06-12 20:52:10 +03001800 * Send an interrupt to the device to wake up the Target CPU
1801 * so it has an opportunity to notice any changed state.
1802 */
1803static int ath10k_pci_wake_target_cpu(struct ath10k *ar)
1804{
Michal Kazior9e264942014-09-02 11:00:21 +03001805 u32 addr, val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001806
Michal Kazior9e264942014-09-02 11:00:21 +03001807 addr = SOC_CORE_BASE_ADDRESS | CORE_CTRL_ADDRESS;
1808 val = ath10k_pci_read32(ar, addr);
1809 val |= CORE_CTRL_CPU_INTR_MASK;
1810 ath10k_pci_write32(ar, addr, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001811
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001812 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001813}
1814
Michal Kaziord63955b2015-01-24 12:14:49 +02001815static int ath10k_pci_get_num_banks(struct ath10k *ar)
1816{
1817 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1818
1819 switch (ar_pci->pdev->device) {
1820 case QCA988X_2_0_DEVICE_ID:
Vasanthakumar Thiagarajan8bd47022015-06-18 12:31:03 +05301821 case QCA99X0_2_0_DEVICE_ID:
Michal Kaziord63955b2015-01-24 12:14:49 +02001822 return 1;
Michal Kazior36582e52015-08-13 14:32:26 +02001823 case QCA6164_2_1_DEVICE_ID:
Michal Kaziord63955b2015-01-24 12:14:49 +02001824 case QCA6174_2_1_DEVICE_ID:
1825 switch (MS(ar->chip_id, SOC_CHIP_ID_REV)) {
1826 case QCA6174_HW_1_0_CHIP_ID_REV:
1827 case QCA6174_HW_1_1_CHIP_ID_REV:
Michal Kazior11a002e2015-04-20 09:20:41 +00001828 case QCA6174_HW_2_1_CHIP_ID_REV:
1829 case QCA6174_HW_2_2_CHIP_ID_REV:
Michal Kaziord63955b2015-01-24 12:14:49 +02001830 return 3;
1831 case QCA6174_HW_1_3_CHIP_ID_REV:
1832 return 2;
Michal Kaziord63955b2015-01-24 12:14:49 +02001833 case QCA6174_HW_3_0_CHIP_ID_REV:
1834 case QCA6174_HW_3_1_CHIP_ID_REV:
1835 case QCA6174_HW_3_2_CHIP_ID_REV:
1836 return 9;
1837 }
1838 break;
1839 }
1840
1841 ath10k_warn(ar, "unknown number of banks, assuming 1\n");
1842 return 1;
1843}
1844
Kalle Valo5e3dd152013-06-12 20:52:10 +03001845static int ath10k_pci_init_config(struct ath10k *ar)
1846{
1847 u32 interconnect_targ_addr;
1848 u32 pcie_state_targ_addr = 0;
1849 u32 pipe_cfg_targ_addr = 0;
1850 u32 svc_to_pipe_map = 0;
1851 u32 pcie_config_flags = 0;
1852 u32 ealloc_value;
1853 u32 ealloc_targ_addr;
1854 u32 flag2_value;
1855 u32 flag2_targ_addr;
1856 int ret = 0;
1857
1858 /* Download to Target the CE Config and the service-to-CE map */
1859 interconnect_targ_addr =
1860 host_interest_item_address(HI_ITEM(hi_interconnect_state));
1861
1862 /* Supply Target-side CE configuration */
Michal Kazior9e264942014-09-02 11:00:21 +03001863 ret = ath10k_pci_diag_read32(ar, interconnect_targ_addr,
1864 &pcie_state_targ_addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001865 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001866 ath10k_err(ar, "Failed to get pcie state addr: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001867 return ret;
1868 }
1869
1870 if (pcie_state_targ_addr == 0) {
1871 ret = -EIO;
Michal Kazior7aa7a722014-08-25 12:09:38 +02001872 ath10k_err(ar, "Invalid pcie state addr\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001873 return ret;
1874 }
1875
Michal Kazior9e264942014-09-02 11:00:21 +03001876 ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
Kalle Valo5e3dd152013-06-12 20:52:10 +03001877 offsetof(struct pcie_state,
Michal Kazior9e264942014-09-02 11:00:21 +03001878 pipe_cfg_addr)),
1879 &pipe_cfg_targ_addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001880 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001881 ath10k_err(ar, "Failed to get pipe cfg addr: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001882 return ret;
1883 }
1884
1885 if (pipe_cfg_targ_addr == 0) {
1886 ret = -EIO;
Michal Kazior7aa7a722014-08-25 12:09:38 +02001887 ath10k_err(ar, "Invalid pipe cfg addr\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001888 return ret;
1889 }
1890
1891 ret = ath10k_pci_diag_write_mem(ar, pipe_cfg_targ_addr,
Kalle Valo5b07e072014-09-14 12:50:06 +03001892 target_ce_config_wlan,
Vasanthakumar Thiagarajan050af062015-06-18 12:31:04 +05301893 sizeof(struct ce_pipe_config) *
1894 NUM_TARGET_CE_CONFIG_WLAN);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001895
1896 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001897 ath10k_err(ar, "Failed to write pipe cfg: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001898 return ret;
1899 }
1900
Michal Kazior9e264942014-09-02 11:00:21 +03001901 ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
Kalle Valo5e3dd152013-06-12 20:52:10 +03001902 offsetof(struct pcie_state,
Michal Kazior9e264942014-09-02 11:00:21 +03001903 svc_to_pipe_map)),
1904 &svc_to_pipe_map);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001905 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001906 ath10k_err(ar, "Failed to get svc/pipe map: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001907 return ret;
1908 }
1909
1910 if (svc_to_pipe_map == 0) {
1911 ret = -EIO;
Michal Kazior7aa7a722014-08-25 12:09:38 +02001912 ath10k_err(ar, "Invalid svc_to_pipe map\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001913 return ret;
1914 }
1915
1916 ret = ath10k_pci_diag_write_mem(ar, svc_to_pipe_map,
Kalle Valo5b07e072014-09-14 12:50:06 +03001917 target_service_to_ce_map_wlan,
1918 sizeof(target_service_to_ce_map_wlan));
Kalle Valo5e3dd152013-06-12 20:52:10 +03001919 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001920 ath10k_err(ar, "Failed to write svc/pipe map: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001921 return ret;
1922 }
1923
Michal Kazior9e264942014-09-02 11:00:21 +03001924 ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
Kalle Valo5e3dd152013-06-12 20:52:10 +03001925 offsetof(struct pcie_state,
Michal Kazior9e264942014-09-02 11:00:21 +03001926 config_flags)),
1927 &pcie_config_flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001928 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001929 ath10k_err(ar, "Failed to get pcie config_flags: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001930 return ret;
1931 }
1932
1933 pcie_config_flags &= ~PCIE_CONFIG_FLAG_ENABLE_L1;
1934
Michal Kazior9e264942014-09-02 11:00:21 +03001935 ret = ath10k_pci_diag_write32(ar, (pcie_state_targ_addr +
1936 offsetof(struct pcie_state,
1937 config_flags)),
1938 pcie_config_flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001939 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001940 ath10k_err(ar, "Failed to write pcie config_flags: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001941 return ret;
1942 }
1943
1944 /* configure early allocation */
1945 ealloc_targ_addr = host_interest_item_address(HI_ITEM(hi_early_alloc));
1946
Michal Kazior9e264942014-09-02 11:00:21 +03001947 ret = ath10k_pci_diag_read32(ar, ealloc_targ_addr, &ealloc_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001948 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001949 ath10k_err(ar, "Faile to get early alloc val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001950 return ret;
1951 }
1952
1953 /* first bank is switched to IRAM */
1954 ealloc_value |= ((HI_EARLY_ALLOC_MAGIC << HI_EARLY_ALLOC_MAGIC_SHIFT) &
1955 HI_EARLY_ALLOC_MAGIC_MASK);
Michal Kaziord63955b2015-01-24 12:14:49 +02001956 ealloc_value |= ((ath10k_pci_get_num_banks(ar) <<
1957 HI_EARLY_ALLOC_IRAM_BANKS_SHIFT) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03001958 HI_EARLY_ALLOC_IRAM_BANKS_MASK);
1959
Michal Kazior9e264942014-09-02 11:00:21 +03001960 ret = ath10k_pci_diag_write32(ar, ealloc_targ_addr, ealloc_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001961 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001962 ath10k_err(ar, "Failed to set early alloc val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001963 return ret;
1964 }
1965
1966 /* Tell Target to proceed with initialization */
1967 flag2_targ_addr = host_interest_item_address(HI_ITEM(hi_option_flag2));
1968
Michal Kazior9e264942014-09-02 11:00:21 +03001969 ret = ath10k_pci_diag_read32(ar, flag2_targ_addr, &flag2_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001970 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001971 ath10k_err(ar, "Failed to get option val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001972 return ret;
1973 }
1974
1975 flag2_value |= HI_OPTION_EARLY_CFG_DONE;
1976
Michal Kazior9e264942014-09-02 11:00:21 +03001977 ret = ath10k_pci_diag_write32(ar, flag2_targ_addr, flag2_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001978 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001979 ath10k_err(ar, "Failed to set option val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001980 return ret;
1981 }
1982
1983 return 0;
1984}
1985
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001986static int ath10k_pci_alloc_pipes(struct ath10k *ar)
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001987{
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001988 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1989 struct ath10k_pci_pipe *pipe;
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001990 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001991
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001992 for (i = 0; i < CE_COUNT; i++) {
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001993 pipe = &ar_pci->pipe_info[i];
1994 pipe->ce_hdl = &ar_pci->ce_states[i];
1995 pipe->pipe_num = i;
1996 pipe->hif_ce_state = ar;
1997
1998 ret = ath10k_ce_alloc_pipe(ar, i, &host_ce_config_wlan[i],
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001999 ath10k_pci_ce_recv_data);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002000 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002001 ath10k_err(ar, "failed to allocate copy engine pipe %d: %d\n",
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002002 i, ret);
2003 return ret;
2004 }
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002005
2006 /* Last CE is Diagnostic Window */
Vasanthakumar Thiagarajan050af062015-06-18 12:31:04 +05302007 if (i == CE_DIAG_PIPE) {
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002008 ar_pci->ce_diag = pipe->ce_hdl;
2009 continue;
2010 }
2011
2012 pipe->buf_sz = (size_t)(host_ce_config_wlan[i].src_sz_max);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002013 }
2014
2015 return 0;
2016}
2017
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002018static void ath10k_pci_free_pipes(struct ath10k *ar)
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002019{
2020 int i;
2021
2022 for (i = 0; i < CE_COUNT; i++)
2023 ath10k_ce_free_pipe(ar, i);
2024}
Kalle Valo5e3dd152013-06-12 20:52:10 +03002025
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002026static int ath10k_pci_init_pipes(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002027{
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002028 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002029
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002030 for (i = 0; i < CE_COUNT; i++) {
2031 ret = ath10k_ce_init_pipe(ar, i, &host_ce_config_wlan[i]);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002032 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002033 ath10k_err(ar, "failed to initialize copy engine pipe %d: %d\n",
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002034 i, ret);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002035 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002036 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002037 }
2038
Kalle Valo5e3dd152013-06-12 20:52:10 +03002039 return 0;
2040}
2041
Michal Kazior5c771e72014-08-22 14:23:34 +02002042static bool ath10k_pci_has_fw_crashed(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002043{
Michal Kazior5c771e72014-08-22 14:23:34 +02002044 return ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS) &
2045 FW_IND_EVENT_PENDING;
2046}
Kalle Valo5e3dd152013-06-12 20:52:10 +03002047
Michal Kazior5c771e72014-08-22 14:23:34 +02002048static void ath10k_pci_fw_crashed_clear(struct ath10k *ar)
2049{
2050 u32 val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002051
Michal Kazior5c771e72014-08-22 14:23:34 +02002052 val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
2053 val &= ~FW_IND_EVENT_PENDING;
2054 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002055}
2056
Michal Kaziorde013572014-05-14 16:56:16 +03002057/* this function effectively clears target memory controller assert line */
2058static void ath10k_pci_warm_reset_si0(struct ath10k *ar)
2059{
2060 u32 val;
2061
2062 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
2063 ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
2064 val | SOC_RESET_CONTROL_SI0_RST_MASK);
2065 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
2066
2067 msleep(10);
2068
2069 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
2070 ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
2071 val & ~SOC_RESET_CONTROL_SI0_RST_MASK);
2072 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
2073
2074 msleep(10);
2075}
2076
Michal Kazior61c16482014-10-28 10:32:06 +01002077static void ath10k_pci_warm_reset_cpu(struct ath10k *ar)
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002078{
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002079 u32 val;
2080
Kalle Valob39712c2014-03-28 09:32:46 +02002081 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, 0);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002082
Michal Kazior61c16482014-10-28 10:32:06 +01002083 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
2084 SOC_RESET_CONTROL_ADDRESS);
2085 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
2086 val | SOC_RESET_CONTROL_CPU_WARM_RST_MASK);
2087}
2088
2089static void ath10k_pci_warm_reset_ce(struct ath10k *ar)
2090{
2091 u32 val;
2092
2093 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
2094 SOC_RESET_CONTROL_ADDRESS);
2095
2096 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
2097 val | SOC_RESET_CONTROL_CE_RST_MASK);
2098 msleep(10);
2099 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
2100 val & ~SOC_RESET_CONTROL_CE_RST_MASK);
2101}
2102
2103static void ath10k_pci_warm_reset_clear_lf(struct ath10k *ar)
2104{
2105 u32 val;
2106
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002107 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
2108 SOC_LF_TIMER_CONTROL0_ADDRESS);
2109 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS +
2110 SOC_LF_TIMER_CONTROL0_ADDRESS,
2111 val & ~SOC_LF_TIMER_CONTROL0_ENABLE_MASK);
Michal Kazior61c16482014-10-28 10:32:06 +01002112}
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002113
Michal Kazior61c16482014-10-28 10:32:06 +01002114static int ath10k_pci_warm_reset(struct ath10k *ar)
2115{
2116 int ret;
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002117
Michal Kazior61c16482014-10-28 10:32:06 +01002118 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot warm reset\n");
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002119
Michal Kazior61c16482014-10-28 10:32:06 +01002120 spin_lock_bh(&ar->data_lock);
2121 ar->stats.fw_warm_reset_counter++;
2122 spin_unlock_bh(&ar->data_lock);
2123
2124 ath10k_pci_irq_disable(ar);
2125
2126 /* Make sure the target CPU is not doing anything dangerous, e.g. if it
2127 * were to access copy engine while host performs copy engine reset
2128 * then it is possible for the device to confuse pci-e controller to
2129 * the point of bringing host system to a complete stop (i.e. hang).
2130 */
Michal Kaziorde013572014-05-14 16:56:16 +03002131 ath10k_pci_warm_reset_si0(ar);
Michal Kazior61c16482014-10-28 10:32:06 +01002132 ath10k_pci_warm_reset_cpu(ar);
2133 ath10k_pci_init_pipes(ar);
2134 ath10k_pci_wait_for_target_init(ar);
Michal Kaziorde013572014-05-14 16:56:16 +03002135
Michal Kazior61c16482014-10-28 10:32:06 +01002136 ath10k_pci_warm_reset_clear_lf(ar);
2137 ath10k_pci_warm_reset_ce(ar);
2138 ath10k_pci_warm_reset_cpu(ar);
2139 ath10k_pci_init_pipes(ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002140
Michal Kazior61c16482014-10-28 10:32:06 +01002141 ret = ath10k_pci_wait_for_target_init(ar);
2142 if (ret) {
2143 ath10k_warn(ar, "failed to wait for target init: %d\n", ret);
2144 return ret;
2145 }
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002146
Michal Kazior7aa7a722014-08-25 12:09:38 +02002147 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot warm reset complete\n");
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002148
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002149 return 0;
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002150}
2151
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +05302152static int ath10k_pci_safe_chip_reset(struct ath10k *ar)
2153{
2154 if (QCA_REV_988X(ar) || QCA_REV_6174(ar)) {
2155 return ath10k_pci_warm_reset(ar);
2156 } else if (QCA_REV_99X0(ar)) {
2157 ath10k_pci_irq_disable(ar);
2158 return ath10k_pci_qca99x0_chip_reset(ar);
2159 } else {
2160 return -ENOTSUPP;
2161 }
2162}
2163
Michal Kaziord63955b2015-01-24 12:14:49 +02002164static int ath10k_pci_qca988x_chip_reset(struct ath10k *ar)
Michal Kazior0bc14d02014-10-28 10:32:07 +01002165{
2166 int i, ret;
2167 u32 val;
2168
Michal Kaziord63955b2015-01-24 12:14:49 +02002169 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot 988x chip reset\n");
Michal Kazior0bc14d02014-10-28 10:32:07 +01002170
2171 /* Some hardware revisions (e.g. CUS223v2) has issues with cold reset.
2172 * It is thus preferred to use warm reset which is safer but may not be
2173 * able to recover the device from all possible fail scenarios.
2174 *
2175 * Warm reset doesn't always work on first try so attempt it a few
2176 * times before giving up.
2177 */
2178 for (i = 0; i < ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS; i++) {
2179 ret = ath10k_pci_warm_reset(ar);
2180 if (ret) {
2181 ath10k_warn(ar, "failed to warm reset attempt %d of %d: %d\n",
2182 i + 1, ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS,
2183 ret);
2184 continue;
2185 }
2186
2187 /* FIXME: Sometimes copy engine doesn't recover after warm
2188 * reset. In most cases this needs cold reset. In some of these
2189 * cases the device is in such a state that a cold reset may
2190 * lock up the host.
2191 *
2192 * Reading any host interest register via copy engine is
2193 * sufficient to verify if device is capable of booting
2194 * firmware blob.
2195 */
2196 ret = ath10k_pci_init_pipes(ar);
2197 if (ret) {
2198 ath10k_warn(ar, "failed to init copy engine: %d\n",
2199 ret);
2200 continue;
2201 }
2202
2203 ret = ath10k_pci_diag_read32(ar, QCA988X_HOST_INTEREST_ADDRESS,
2204 &val);
2205 if (ret) {
2206 ath10k_warn(ar, "failed to poke copy engine: %d\n",
2207 ret);
2208 continue;
2209 }
2210
2211 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot chip reset complete (warm)\n");
2212 return 0;
2213 }
2214
2215 if (ath10k_pci_reset_mode == ATH10K_PCI_RESET_WARM_ONLY) {
2216 ath10k_warn(ar, "refusing cold reset as requested\n");
2217 return -EPERM;
2218 }
2219
2220 ret = ath10k_pci_cold_reset(ar);
2221 if (ret) {
2222 ath10k_warn(ar, "failed to cold reset: %d\n", ret);
2223 return ret;
2224 }
2225
2226 ret = ath10k_pci_wait_for_target_init(ar);
2227 if (ret) {
2228 ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
2229 ret);
2230 return ret;
2231 }
2232
Michal Kaziord63955b2015-01-24 12:14:49 +02002233 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca988x chip reset complete (cold)\n");
Michal Kazior0bc14d02014-10-28 10:32:07 +01002234
2235 return 0;
2236}
2237
Michal Kaziord63955b2015-01-24 12:14:49 +02002238static int ath10k_pci_qca6174_chip_reset(struct ath10k *ar)
2239{
2240 int ret;
2241
2242 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca6174 chip reset\n");
2243
2244 /* FIXME: QCA6174 requires cold + warm reset to work. */
2245
2246 ret = ath10k_pci_cold_reset(ar);
2247 if (ret) {
2248 ath10k_warn(ar, "failed to cold reset: %d\n", ret);
2249 return ret;
2250 }
2251
2252 ret = ath10k_pci_wait_for_target_init(ar);
2253 if (ret) {
2254 ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
Kalle Valo617b0f42015-10-05 17:56:35 +03002255 ret);
Michal Kaziord63955b2015-01-24 12:14:49 +02002256 return ret;
2257 }
2258
2259 ret = ath10k_pci_warm_reset(ar);
2260 if (ret) {
2261 ath10k_warn(ar, "failed to warm reset: %d\n", ret);
2262 return ret;
2263 }
2264
2265 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca6174 chip reset complete (cold)\n");
2266
2267 return 0;
2268}
2269
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +05302270static int ath10k_pci_qca99x0_chip_reset(struct ath10k *ar)
2271{
2272 int ret;
2273
2274 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca99x0 chip reset\n");
2275
2276 ret = ath10k_pci_cold_reset(ar);
2277 if (ret) {
2278 ath10k_warn(ar, "failed to cold reset: %d\n", ret);
2279 return ret;
2280 }
2281
2282 ret = ath10k_pci_wait_for_target_init(ar);
2283 if (ret) {
2284 ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
2285 ret);
2286 return ret;
2287 }
2288
2289 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca99x0 chip reset complete (cold)\n");
2290
2291 return 0;
2292}
2293
Michal Kaziord63955b2015-01-24 12:14:49 +02002294static int ath10k_pci_chip_reset(struct ath10k *ar)
2295{
2296 if (QCA_REV_988X(ar))
2297 return ath10k_pci_qca988x_chip_reset(ar);
2298 else if (QCA_REV_6174(ar))
2299 return ath10k_pci_qca6174_chip_reset(ar);
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +05302300 else if (QCA_REV_99X0(ar))
2301 return ath10k_pci_qca99x0_chip_reset(ar);
Michal Kaziord63955b2015-01-24 12:14:49 +02002302 else
2303 return -ENOTSUPP;
2304}
2305
Michal Kazior0bc14d02014-10-28 10:32:07 +01002306static int ath10k_pci_hif_power_up(struct ath10k *ar)
Michal Kazior8c5c5362013-07-16 09:38:50 +02002307{
Janusz Dziedzic76d870e2015-05-18 09:38:16 +00002308 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02002309 int ret;
2310
Michal Kazior0bc14d02014-10-28 10:32:07 +01002311 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif power up\n");
2312
Janusz Dziedzic76d870e2015-05-18 09:38:16 +00002313 pcie_capability_read_word(ar_pci->pdev, PCI_EXP_LNKCTL,
2314 &ar_pci->link_ctl);
2315 pcie_capability_write_word(ar_pci->pdev, PCI_EXP_LNKCTL,
2316 ar_pci->link_ctl & ~PCI_EXP_LNKCTL_ASPMC);
2317
Michal Kazior8c5c5362013-07-16 09:38:50 +02002318 /*
2319 * Bring the target up cleanly.
2320 *
2321 * The target may be in an undefined state with an AUX-powered Target
2322 * and a Host in WoW mode. If the Host crashes, loses power, or is
2323 * restarted (without unloading the driver) then the Target is left
2324 * (aux) powered and running. On a subsequent driver load, the Target
2325 * is in an unexpected state. We try to catch that here in order to
2326 * reset the Target and retry the probe.
2327 */
Michal Kazior0bc14d02014-10-28 10:32:07 +01002328 ret = ath10k_pci_chip_reset(ar);
Michal Kazior5b2589f2013-11-08 08:01:30 +01002329 if (ret) {
Michal Kaziora2fa8802015-01-12 15:29:37 +01002330 if (ath10k_pci_has_fw_crashed(ar)) {
2331 ath10k_warn(ar, "firmware crashed during chip reset\n");
2332 ath10k_pci_fw_crashed_clear(ar);
2333 ath10k_pci_fw_crashed_dump(ar);
2334 }
2335
Michal Kazior0bc14d02014-10-28 10:32:07 +01002336 ath10k_err(ar, "failed to reset chip: %d\n", ret);
Bartosz Markowski707b1bbd2014-10-31 09:03:43 +01002337 goto err_sleep;
Michal Kazior5b2589f2013-11-08 08:01:30 +01002338 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02002339
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002340 ret = ath10k_pci_init_pipes(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02002341 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002342 ath10k_err(ar, "failed to initialize CE: %d\n", ret);
Bartosz Markowski707b1bbd2014-10-31 09:03:43 +01002343 goto err_sleep;
Michal Kaziorab977bd2013-11-25 14:06:26 +01002344 }
2345
Michal Kazior98563d52013-11-08 08:01:33 +01002346 ret = ath10k_pci_init_config(ar);
2347 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002348 ath10k_err(ar, "failed to setup init config: %d\n", ret);
Michal Kazior5c771e72014-08-22 14:23:34 +02002349 goto err_ce;
Michal Kazior98563d52013-11-08 08:01:33 +01002350 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02002351
2352 ret = ath10k_pci_wake_target_cpu(ar);
2353 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002354 ath10k_err(ar, "could not wake up target CPU: %d\n", ret);
Michal Kazior5c771e72014-08-22 14:23:34 +02002355 goto err_ce;
Michal Kazior8c5c5362013-07-16 09:38:50 +02002356 }
2357
2358 return 0;
2359
2360err_ce:
2361 ath10k_pci_ce_deinit(ar);
Michal Kazior0bc14d02014-10-28 10:32:07 +01002362
Bartosz Markowski707b1bbd2014-10-31 09:03:43 +01002363err_sleep:
Michal Kazior8c5c5362013-07-16 09:38:50 +02002364 return ret;
2365}
2366
2367static void ath10k_pci_hif_power_down(struct ath10k *ar)
2368{
Michal Kazior7aa7a722014-08-25 12:09:38 +02002369 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif power down\n");
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002370
Michal Kaziorc011b282014-10-28 10:32:08 +01002371 /* Currently hif_power_up performs effectively a reset and hif_stop
2372 * resets the chip as well so there's no point in resetting here.
2373 */
Michal Kazior8c5c5362013-07-16 09:38:50 +02002374}
2375
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002376#ifdef CONFIG_PM
2377
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002378static int ath10k_pci_hif_suspend(struct ath10k *ar)
2379{
Michal Kazior77258d42015-05-18 09:38:18 +00002380 /* The grace timer can still be counting down and ar->ps_awake be true.
2381 * It is known that the device may be asleep after resuming regardless
2382 * of the SoC powersave state before suspending. Hence make sure the
2383 * device is asleep before proceeding.
2384 */
2385 ath10k_pci_sleep_sync(ar);
Michal Kazior320e14b2015-03-02 13:22:13 +01002386
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002387 return 0;
2388}
2389
2390static int ath10k_pci_hif_resume(struct ath10k *ar)
2391{
2392 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2393 struct pci_dev *pdev = ar_pci->pdev;
2394 u32 val;
2395
Michal Kazior9ff4be92015-03-02 13:22:14 +01002396 /* Suspend/Resume resets the PCI configuration space, so we have to
2397 * re-disable the RETRY_TIMEOUT register (0x41) to keep PCI Tx retries
2398 * from interfering with C3 CPU state. pci_restore_state won't help
2399 * here since it only restores the first 64 bytes pci config header.
2400 */
2401 pci_read_config_dword(pdev, 0x40, &val);
2402 if ((val & 0x0000ff00) != 0)
2403 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002404
Michal Kazior77258d42015-05-18 09:38:18 +00002405 return 0;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002406}
2407#endif
2408
Kalle Valo5e3dd152013-06-12 20:52:10 +03002409static const struct ath10k_hif_ops ath10k_pci_hif_ops = {
Michal Kazior726346f2014-02-27 18:50:04 +02002410 .tx_sg = ath10k_pci_hif_tx_sg,
Kalle Valoeef25402014-09-24 14:16:52 +03002411 .diag_read = ath10k_pci_hif_diag_read,
Yanbo Li9f65ad22014-11-25 12:24:48 +02002412 .diag_write = ath10k_pci_diag_write_mem,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002413 .exchange_bmi_msg = ath10k_pci_hif_exchange_bmi_msg,
2414 .start = ath10k_pci_hif_start,
2415 .stop = ath10k_pci_hif_stop,
2416 .map_service_to_pipe = ath10k_pci_hif_map_service_to_pipe,
2417 .get_default_pipe = ath10k_pci_hif_get_default_pipe,
2418 .send_complete_check = ath10k_pci_hif_send_complete_check,
Michal Kaziore799bbf2013-07-05 16:15:12 +03002419 .set_callbacks = ath10k_pci_hif_set_callbacks,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002420 .get_free_queue_number = ath10k_pci_hif_get_free_queue_number,
Michal Kazior8c5c5362013-07-16 09:38:50 +02002421 .power_up = ath10k_pci_hif_power_up,
2422 .power_down = ath10k_pci_hif_power_down,
Yanbo Li077a3802014-11-25 12:24:33 +02002423 .read32 = ath10k_pci_read32,
2424 .write32 = ath10k_pci_write32,
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002425#ifdef CONFIG_PM
2426 .suspend = ath10k_pci_hif_suspend,
2427 .resume = ath10k_pci_hif_resume,
2428#endif
Kalle Valo5e3dd152013-06-12 20:52:10 +03002429};
2430
2431static void ath10k_pci_ce_tasklet(unsigned long ptr)
2432{
Michal Kazior87263e52013-08-27 13:08:01 +02002433 struct ath10k_pci_pipe *pipe = (struct ath10k_pci_pipe *)ptr;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002434 struct ath10k_pci *ar_pci = pipe->ar_pci;
2435
2436 ath10k_ce_per_engine_service(ar_pci->ar, pipe->pipe_num);
2437}
2438
2439static void ath10k_msi_err_tasklet(unsigned long data)
2440{
2441 struct ath10k *ar = (struct ath10k *)data;
2442
Michal Kazior5c771e72014-08-22 14:23:34 +02002443 if (!ath10k_pci_has_fw_crashed(ar)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002444 ath10k_warn(ar, "received unsolicited fw crash interrupt\n");
Michal Kazior5c771e72014-08-22 14:23:34 +02002445 return;
2446 }
2447
Michal Kazior6f3b7ff2015-01-24 12:14:52 +02002448 ath10k_pci_irq_disable(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02002449 ath10k_pci_fw_crashed_clear(ar);
2450 ath10k_pci_fw_crashed_dump(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002451}
2452
2453/*
2454 * Handler for a per-engine interrupt on a PARTICULAR CE.
2455 * This is used in cases where each CE has a private MSI interrupt.
2456 */
2457static irqreturn_t ath10k_pci_per_engine_handler(int irq, void *arg)
2458{
2459 struct ath10k *ar = arg;
2460 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2461 int ce_id = irq - ar_pci->pdev->irq - MSI_ASSIGN_CE_INITIAL;
2462
Dan Carpentere5742672013-06-18 10:28:46 +03002463 if (ce_id < 0 || ce_id >= ARRAY_SIZE(ar_pci->pipe_info)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002464 ath10k_warn(ar, "unexpected/invalid irq %d ce_id %d\n", irq,
2465 ce_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002466 return IRQ_HANDLED;
2467 }
2468
2469 /*
2470 * NOTE: We are able to derive ce_id from irq because we
2471 * use a one-to-one mapping for CE's 0..5.
2472 * CE's 6 & 7 do not use interrupts at all.
2473 *
2474 * This mapping must be kept in sync with the mapping
2475 * used by firmware.
2476 */
2477 tasklet_schedule(&ar_pci->pipe_info[ce_id].intr);
2478 return IRQ_HANDLED;
2479}
2480
2481static irqreturn_t ath10k_pci_msi_fw_handler(int irq, void *arg)
2482{
2483 struct ath10k *ar = arg;
2484 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2485
2486 tasklet_schedule(&ar_pci->msi_fw_err);
2487 return IRQ_HANDLED;
2488}
2489
2490/*
2491 * Top-level interrupt handler for all PCI interrupts from a Target.
2492 * When a block of MSI interrupts is allocated, this top-level handler
2493 * is not used; instead, we directly call the correct sub-handler.
2494 */
2495static irqreturn_t ath10k_pci_interrupt_handler(int irq, void *arg)
2496{
2497 struct ath10k *ar = arg;
2498 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2499
2500 if (ar_pci->num_msi_intrs == 0) {
Michal Kaziore5398872013-11-25 14:06:20 +01002501 if (!ath10k_pci_irq_pending(ar))
2502 return IRQ_NONE;
2503
Michal Kazior26852182013-11-25 14:06:25 +01002504 ath10k_pci_disable_and_clear_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002505 }
2506
2507 tasklet_schedule(&ar_pci->intr_tq);
2508
2509 return IRQ_HANDLED;
2510}
2511
2512static void ath10k_pci_tasklet(unsigned long data)
2513{
2514 struct ath10k *ar = (struct ath10k *)data;
2515 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2516
Michal Kazior5c771e72014-08-22 14:23:34 +02002517 if (ath10k_pci_has_fw_crashed(ar)) {
Michal Kazior6f3b7ff2015-01-24 12:14:52 +02002518 ath10k_pci_irq_disable(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02002519 ath10k_pci_fw_crashed_clear(ar);
2520 ath10k_pci_fw_crashed_dump(ar);
2521 return;
2522 }
2523
Kalle Valo5e3dd152013-06-12 20:52:10 +03002524 ath10k_ce_per_engine_service_any(ar);
2525
Michal Kazior26852182013-11-25 14:06:25 +01002526 /* Re-enable legacy irq that was disabled in the irq handler */
2527 if (ar_pci->num_msi_intrs == 0)
2528 ath10k_pci_enable_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002529}
2530
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002531static int ath10k_pci_request_irq_msix(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002532{
2533 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002534 int ret, i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002535
2536 ret = request_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW,
2537 ath10k_pci_msi_fw_handler,
2538 IRQF_SHARED, "ath10k_pci", ar);
Michal Kazior591ecdb2013-07-31 10:55:15 +02002539 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002540 ath10k_warn(ar, "failed to request MSI-X fw irq %d: %d\n",
Michal Kazior591ecdb2013-07-31 10:55:15 +02002541 ar_pci->pdev->irq + MSI_ASSIGN_FW, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002542 return ret;
Michal Kazior591ecdb2013-07-31 10:55:15 +02002543 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002544
2545 for (i = MSI_ASSIGN_CE_INITIAL; i <= MSI_ASSIGN_CE_MAX; i++) {
2546 ret = request_irq(ar_pci->pdev->irq + i,
2547 ath10k_pci_per_engine_handler,
2548 IRQF_SHARED, "ath10k_pci", ar);
2549 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002550 ath10k_warn(ar, "failed to request MSI-X ce irq %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002551 ar_pci->pdev->irq + i, ret);
2552
Michal Kazior87b14232013-06-26 08:50:50 +02002553 for (i--; i >= MSI_ASSIGN_CE_INITIAL; i--)
2554 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002555
Michal Kazior87b14232013-06-26 08:50:50 +02002556 free_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002557 return ret;
2558 }
2559 }
2560
Kalle Valo5e3dd152013-06-12 20:52:10 +03002561 return 0;
2562}
2563
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002564static int ath10k_pci_request_irq_msi(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002565{
2566 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2567 int ret;
2568
2569 ret = request_irq(ar_pci->pdev->irq,
2570 ath10k_pci_interrupt_handler,
2571 IRQF_SHARED, "ath10k_pci", ar);
Kalle Valof3782742013-10-17 11:36:15 +03002572 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002573 ath10k_warn(ar, "failed to request MSI irq %d: %d\n",
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002574 ar_pci->pdev->irq, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002575 return ret;
Kalle Valof3782742013-10-17 11:36:15 +03002576 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002577
Kalle Valo5e3dd152013-06-12 20:52:10 +03002578 return 0;
2579}
2580
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002581static int ath10k_pci_request_irq_legacy(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002582{
2583 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002584 int ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002585
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002586 ret = request_irq(ar_pci->pdev->irq,
2587 ath10k_pci_interrupt_handler,
2588 IRQF_SHARED, "ath10k_pci", ar);
Kalle Valof3782742013-10-17 11:36:15 +03002589 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002590 ath10k_warn(ar, "failed to request legacy irq %d: %d\n",
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002591 ar_pci->pdev->irq, ret);
Kalle Valof3782742013-10-17 11:36:15 +03002592 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002593 }
2594
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002595 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002596}
2597
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002598static int ath10k_pci_request_irq(struct ath10k *ar)
2599{
2600 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2601
2602 switch (ar_pci->num_msi_intrs) {
2603 case 0:
2604 return ath10k_pci_request_irq_legacy(ar);
2605 case 1:
2606 return ath10k_pci_request_irq_msi(ar);
Janusz Dziedzicb8402d82015-09-17 08:17:33 +02002607 default:
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002608 return ath10k_pci_request_irq_msix(ar);
2609 }
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002610}
2611
2612static void ath10k_pci_free_irq(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002613{
2614 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2615 int i;
2616
2617 /* There's at least one interrupt irregardless whether its legacy INTR
2618 * or MSI or MSI-X */
2619 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
2620 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002621}
2622
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002623static void ath10k_pci_init_irq_tasklets(struct ath10k *ar)
2624{
2625 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2626 int i;
2627
2628 tasklet_init(&ar_pci->intr_tq, ath10k_pci_tasklet, (unsigned long)ar);
2629 tasklet_init(&ar_pci->msi_fw_err, ath10k_msi_err_tasklet,
2630 (unsigned long)ar);
2631
2632 for (i = 0; i < CE_COUNT; i++) {
2633 ar_pci->pipe_info[i].ar_pci = ar_pci;
2634 tasklet_init(&ar_pci->pipe_info[i].intr, ath10k_pci_ce_tasklet,
2635 (unsigned long)&ar_pci->pipe_info[i]);
2636 }
2637}
2638
2639static int ath10k_pci_init_irq(struct ath10k *ar)
2640{
2641 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2642 int ret;
2643
2644 ath10k_pci_init_irq_tasklets(ar);
2645
Michal Kazior403d6272014-08-22 14:23:31 +02002646 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_AUTO)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002647 ath10k_info(ar, "limiting irq mode to: %d\n",
2648 ath10k_pci_irq_mode);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002649
2650 /* Try MSI-X */
Michal Kazior0edf2572014-08-07 11:03:29 +02002651 if (ath10k_pci_irq_mode == ATH10K_PCI_IRQ_AUTO) {
Janusz Dziedzicb8402d82015-09-17 08:17:33 +02002652 ar_pci->num_msi_intrs = MSI_ASSIGN_CE_MAX + 1;
Alexander Gordeev5ad68672014-02-13 17:50:02 +02002653 ret = pci_enable_msi_range(ar_pci->pdev, ar_pci->num_msi_intrs,
Kalle Valo5b07e072014-09-14 12:50:06 +03002654 ar_pci->num_msi_intrs);
Alexander Gordeev5ad68672014-02-13 17:50:02 +02002655 if (ret > 0)
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002656 return 0;
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002657
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002658 /* fall-through */
2659 }
2660
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002661 /* Try MSI */
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002662 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_LEGACY) {
2663 ar_pci->num_msi_intrs = 1;
2664 ret = pci_enable_msi(ar_pci->pdev);
2665 if (ret == 0)
2666 return 0;
2667
2668 /* fall-through */
2669 }
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002670
2671 /* Try legacy irq
2672 *
2673 * A potential race occurs here: The CORE_BASE write
2674 * depends on target correctly decoding AXI address but
2675 * host won't know when target writes BAR to CORE_CTRL.
2676 * This write might get lost if target has NOT written BAR.
2677 * For now, fix the race by repeating the write in below
2678 * synchronization checking. */
2679 ar_pci->num_msi_intrs = 0;
2680
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002681 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2682 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002683
2684 return 0;
2685}
2686
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002687static void ath10k_pci_deinit_irq_legacy(struct ath10k *ar)
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002688{
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002689 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2690 0);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002691}
2692
2693static int ath10k_pci_deinit_irq(struct ath10k *ar)
2694{
2695 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2696
2697 switch (ar_pci->num_msi_intrs) {
2698 case 0:
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002699 ath10k_pci_deinit_irq_legacy(ar);
Janusz Dziedzicb8402d82015-09-17 08:17:33 +02002700 break;
Alexander Gordeevbb8b6212014-02-13 17:50:01 +02002701 default:
2702 pci_disable_msi(ar_pci->pdev);
Janusz Dziedzicb8402d82015-09-17 08:17:33 +02002703 break;
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002704 }
2705
Janusz Dziedzicb8402d82015-09-17 08:17:33 +02002706 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002707}
2708
Michal Kaziord7fb47f2013-11-08 08:01:26 +01002709static int ath10k_pci_wait_for_target_init(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002710{
2711 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo0399eca2014-03-28 09:32:21 +02002712 unsigned long timeout;
Kalle Valo0399eca2014-03-28 09:32:21 +02002713 u32 val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002714
Michal Kazior7aa7a722014-08-25 12:09:38 +02002715 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot waiting target to initialise\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002716
Kalle Valo0399eca2014-03-28 09:32:21 +02002717 timeout = jiffies + msecs_to_jiffies(ATH10K_PCI_TARGET_WAIT);
2718
2719 do {
2720 val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
2721
Michal Kazior7aa7a722014-08-25 12:09:38 +02002722 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target indicator %x\n",
2723 val);
Kalle Valo50f87a62014-03-28 09:32:52 +02002724
Kalle Valo0399eca2014-03-28 09:32:21 +02002725 /* target should never return this */
2726 if (val == 0xffffffff)
2727 continue;
2728
Michal Kazior7710cd22014-04-23 19:30:04 +03002729 /* the device has crashed so don't bother trying anymore */
2730 if (val & FW_IND_EVENT_PENDING)
2731 break;
2732
Kalle Valo0399eca2014-03-28 09:32:21 +02002733 if (val & FW_IND_INITIALIZED)
2734 break;
2735
Kalle Valo5e3dd152013-06-12 20:52:10 +03002736 if (ar_pci->num_msi_intrs == 0)
2737 /* Fix potential race by repeating CORE_BASE writes */
Michal Kaziora4282492014-10-20 14:14:37 +02002738 ath10k_pci_enable_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002739
Kalle Valo0399eca2014-03-28 09:32:21 +02002740 mdelay(10);
2741 } while (time_before(jiffies, timeout));
2742
Michal Kaziora4282492014-10-20 14:14:37 +02002743 ath10k_pci_disable_and_clear_legacy_irq(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02002744 ath10k_pci_irq_msi_fw_mask(ar);
Michal Kaziora4282492014-10-20 14:14:37 +02002745
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002746 if (val == 0xffffffff) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002747 ath10k_err(ar, "failed to read device register, device is gone\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002748 return -EIO;
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002749 }
2750
Michal Kazior7710cd22014-04-23 19:30:04 +03002751 if (val & FW_IND_EVENT_PENDING) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002752 ath10k_warn(ar, "device has crashed during init\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002753 return -ECOMM;
Michal Kazior7710cd22014-04-23 19:30:04 +03002754 }
2755
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002756 if (!(val & FW_IND_INITIALIZED)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002757 ath10k_err(ar, "failed to receive initialized event from target: %08x\n",
Kalle Valo0399eca2014-03-28 09:32:21 +02002758 val);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002759 return -ETIMEDOUT;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002760 }
2761
Michal Kazior7aa7a722014-08-25 12:09:38 +02002762 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target initialised\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002763 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002764}
2765
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002766static int ath10k_pci_cold_reset(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002767{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002768 u32 val;
2769
Michal Kazior7aa7a722014-08-25 12:09:38 +02002770 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cold reset\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002771
Ben Greearf51dbe72014-09-29 14:41:46 +03002772 spin_lock_bh(&ar->data_lock);
2773
2774 ar->stats.fw_cold_reset_counter++;
2775
2776 spin_unlock_bh(&ar->data_lock);
2777
Kalle Valo5e3dd152013-06-12 20:52:10 +03002778 /* Put Target, including PCIe, into RESET. */
Kalle Valoe479ed42013-09-01 10:01:53 +03002779 val = ath10k_pci_reg_read32(ar, SOC_GLOBAL_RESET_ADDRESS);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002780 val |= 1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002781 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002782
Vasanthakumar Thiagarajanacd19582015-07-10 14:31:20 +05302783 /* After writing into SOC_GLOBAL_RESET to put device into
2784 * reset and pulling out of reset pcie may not be stable
2785 * for any immediate pcie register access and cause bus error,
2786 * add delay before any pcie access request to fix this issue.
2787 */
2788 msleep(20);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002789
2790 /* Pull Target, including PCIe, out of RESET. */
2791 val &= ~1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002792 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002793
Vasanthakumar Thiagarajanacd19582015-07-10 14:31:20 +05302794 msleep(20);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002795
Michal Kazior7aa7a722014-08-25 12:09:38 +02002796 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cold reset complete\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02002797
Michal Kazior5b2589f2013-11-08 08:01:30 +01002798 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002799}
2800
Michal Kazior2986e3e2014-08-07 11:03:30 +02002801static int ath10k_pci_claim(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002802{
Michal Kazior2986e3e2014-08-07 11:03:30 +02002803 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2804 struct pci_dev *pdev = ar_pci->pdev;
Michal Kazior2986e3e2014-08-07 11:03:30 +02002805 int ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002806
2807 pci_set_drvdata(pdev, ar);
2808
Kalle Valo5e3dd152013-06-12 20:52:10 +03002809 ret = pci_enable_device(pdev);
2810 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002811 ath10k_err(ar, "failed to enable pci device: %d\n", ret);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002812 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002813 }
2814
Kalle Valo5e3dd152013-06-12 20:52:10 +03002815 ret = pci_request_region(pdev, BAR_NUM, "ath");
2816 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002817 ath10k_err(ar, "failed to request region BAR%d: %d\n", BAR_NUM,
Michal Kazior2986e3e2014-08-07 11:03:30 +02002818 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002819 goto err_device;
2820 }
2821
Michal Kazior2986e3e2014-08-07 11:03:30 +02002822 /* Target expects 32 bit DMA. Enforce it. */
Kalle Valo5e3dd152013-06-12 20:52:10 +03002823 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2824 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002825 ath10k_err(ar, "failed to set dma mask to 32-bit: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002826 goto err_region;
2827 }
2828
2829 ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2830 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002831 ath10k_err(ar, "failed to set consistent dma mask to 32-bit: %d\n",
Michal Kazior2986e3e2014-08-07 11:03:30 +02002832 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002833 goto err_region;
2834 }
2835
Kalle Valo5e3dd152013-06-12 20:52:10 +03002836 pci_set_master(pdev);
2837
Kalle Valo5e3dd152013-06-12 20:52:10 +03002838 /* Arrange for access to Target SoC registers. */
Michal Kazioraeae5b42015-06-15 14:46:42 +03002839 ar_pci->mem_len = pci_resource_len(pdev, BAR_NUM);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002840 ar_pci->mem = pci_iomap(pdev, BAR_NUM, 0);
2841 if (!ar_pci->mem) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002842 ath10k_err(ar, "failed to iomap BAR%d\n", BAR_NUM);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002843 ret = -EIO;
2844 goto err_master;
2845 }
2846
Michal Kazior7aa7a722014-08-25 12:09:38 +02002847 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot pci_mem 0x%p\n", ar_pci->mem);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002848 return 0;
2849
2850err_master:
2851 pci_clear_master(pdev);
2852
2853err_region:
2854 pci_release_region(pdev, BAR_NUM);
2855
2856err_device:
2857 pci_disable_device(pdev);
2858
2859 return ret;
2860}
2861
2862static void ath10k_pci_release(struct ath10k *ar)
2863{
2864 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2865 struct pci_dev *pdev = ar_pci->pdev;
2866
2867 pci_iounmap(pdev, ar_pci->mem);
2868 pci_release_region(pdev, BAR_NUM);
2869 pci_clear_master(pdev);
2870 pci_disable_device(pdev);
2871}
2872
Michal Kazior7505f7c2014-12-02 10:55:54 +02002873static bool ath10k_pci_chip_is_supported(u32 dev_id, u32 chip_id)
2874{
2875 const struct ath10k_pci_supp_chip *supp_chip;
2876 int i;
2877 u32 rev_id = MS(chip_id, SOC_CHIP_ID_REV);
2878
2879 for (i = 0; i < ARRAY_SIZE(ath10k_pci_supp_chips); i++) {
2880 supp_chip = &ath10k_pci_supp_chips[i];
2881
2882 if (supp_chip->dev_id == dev_id &&
2883 supp_chip->rev_id == rev_id)
2884 return true;
2885 }
2886
2887 return false;
2888}
2889
Kalle Valo5e3dd152013-06-12 20:52:10 +03002890static int ath10k_pci_probe(struct pci_dev *pdev,
2891 const struct pci_device_id *pci_dev)
2892{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002893 int ret = 0;
2894 struct ath10k *ar;
2895 struct ath10k_pci *ar_pci;
Michal Kaziord63955b2015-01-24 12:14:49 +02002896 enum ath10k_hw_rev hw_rev;
Michal Kazior2986e3e2014-08-07 11:03:30 +02002897 u32 chip_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002898
Michal Kaziord63955b2015-01-24 12:14:49 +02002899 switch (pci_dev->device) {
2900 case QCA988X_2_0_DEVICE_ID:
2901 hw_rev = ATH10K_HW_QCA988X;
2902 break;
Michal Kazior36582e52015-08-13 14:32:26 +02002903 case QCA6164_2_1_DEVICE_ID:
Michal Kaziord63955b2015-01-24 12:14:49 +02002904 case QCA6174_2_1_DEVICE_ID:
2905 hw_rev = ATH10K_HW_QCA6174;
2906 break;
Vasanthakumar Thiagarajan8bd47022015-06-18 12:31:03 +05302907 case QCA99X0_2_0_DEVICE_ID:
2908 hw_rev = ATH10K_HW_QCA99X0;
2909 break;
Michal Kaziord63955b2015-01-24 12:14:49 +02002910 default:
2911 WARN_ON(1);
2912 return -ENOTSUPP;
2913 }
2914
2915 ar = ath10k_core_create(sizeof(*ar_pci), &pdev->dev, ATH10K_BUS_PCI,
2916 hw_rev, &ath10k_pci_hif_ops);
Michal Kaziore7b54192014-08-07 11:03:27 +02002917 if (!ar) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002918 dev_err(&pdev->dev, "failed to allocate core\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002919 return -ENOMEM;
Michal Kaziore7b54192014-08-07 11:03:27 +02002920 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002921
Manikanta Pubbisetty0a51b342015-10-09 11:55:58 +03002922 ath10k_dbg(ar, ATH10K_DBG_BOOT, "pci probe %04x:%04x %04x:%04x\n",
2923 pdev->vendor, pdev->device,
2924 pdev->subsystem_vendor, pdev->subsystem_device);
Michal Kazior7aa7a722014-08-25 12:09:38 +02002925
Michal Kaziore7b54192014-08-07 11:03:27 +02002926 ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002927 ar_pci->pdev = pdev;
2928 ar_pci->dev = &pdev->dev;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002929 ar_pci->ar = ar;
Michal Kazior36582e52015-08-13 14:32:26 +02002930 ar->dev_id = pci_dev->device;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002931
Manikanta Pubbisetty0a51b342015-10-09 11:55:58 +03002932 ar->id.vendor = pdev->vendor;
2933 ar->id.device = pdev->device;
2934 ar->id.subsystem_vendor = pdev->subsystem_vendor;
2935 ar->id.subsystem_device = pdev->subsystem_device;
Michal Kaziorde57e2c2015-04-17 09:19:17 +00002936
Kalle Valo5e3dd152013-06-12 20:52:10 +03002937 spin_lock_init(&ar_pci->ce_lock);
Michal Kazior77258d42015-05-18 09:38:18 +00002938 spin_lock_init(&ar_pci->ps_lock);
2939
Michal Kazior728f95e2014-08-22 14:33:14 +02002940 setup_timer(&ar_pci->rx_post_retry, ath10k_pci_rx_replenish_retry,
2941 (unsigned long)ar);
Michal Kazior77258d42015-05-18 09:38:18 +00002942 setup_timer(&ar_pci->ps_timer, ath10k_pci_ps_timer,
2943 (unsigned long)ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002944
Michal Kazior2986e3e2014-08-07 11:03:30 +02002945 ret = ath10k_pci_claim(ar);
Kalle Valoe01ae682013-09-01 11:22:14 +03002946 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002947 ath10k_err(ar, "failed to claim device: %d\n", ret);
Michal Kaziore7b54192014-08-07 11:03:27 +02002948 goto err_core_destroy;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002949 }
2950
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002951 ret = ath10k_pci_alloc_pipes(ar);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002952 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002953 ath10k_err(ar, "failed to allocate copy engine pipes: %d\n",
2954 ret);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002955 goto err_sleep;
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002956 }
2957
Michal Kazior403d6272014-08-22 14:23:31 +02002958 ath10k_pci_ce_deinit(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02002959 ath10k_pci_irq_disable(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02002960
Michal Kazior403d6272014-08-22 14:23:31 +02002961 ret = ath10k_pci_init_irq(ar);
2962 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002963 ath10k_err(ar, "failed to init irqs: %d\n", ret);
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002964 goto err_free_pipes;
Michal Kazior403d6272014-08-22 14:23:31 +02002965 }
2966
Michal Kazior7aa7a722014-08-25 12:09:38 +02002967 ath10k_info(ar, "pci irq %s interrupts %d irq_mode %d reset_mode %d\n",
Michal Kazior403d6272014-08-22 14:23:31 +02002968 ath10k_pci_get_irq_method(ar), ar_pci->num_msi_intrs,
2969 ath10k_pci_irq_mode, ath10k_pci_reset_mode);
2970
Michal Kazior5c771e72014-08-22 14:23:34 +02002971 ret = ath10k_pci_request_irq(ar);
Michal Kazior403d6272014-08-22 14:23:31 +02002972 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002973 ath10k_warn(ar, "failed to request irqs: %d\n", ret);
Michal Kazior403d6272014-08-22 14:23:31 +02002974 goto err_deinit_irq;
2975 }
2976
Michal Kazior1a7fecb2015-01-24 12:14:48 +02002977 ret = ath10k_pci_chip_reset(ar);
2978 if (ret) {
2979 ath10k_err(ar, "failed to reset chip: %d\n", ret);
2980 goto err_free_irq;
2981 }
2982
2983 chip_id = ath10k_pci_soc_read32(ar, SOC_CHIP_ID_ADDRESS);
2984 if (chip_id == 0xffffffff) {
2985 ath10k_err(ar, "failed to get chip id\n");
2986 goto err_free_irq;
2987 }
2988
2989 if (!ath10k_pci_chip_is_supported(pdev->device, chip_id)) {
2990 ath10k_err(ar, "device %04x with chip_id %08x isn't supported\n",
2991 pdev->device, chip_id);
Michal Kaziord9585a92015-04-10 13:01:27 +00002992 goto err_free_irq;
Michal Kazior1a7fecb2015-01-24 12:14:48 +02002993 }
2994
Kalle Valoe01ae682013-09-01 11:22:14 +03002995 ret = ath10k_core_register(ar, chip_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002996 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002997 ath10k_err(ar, "failed to register driver core: %d\n", ret);
Michal Kazior5c771e72014-08-22 14:23:34 +02002998 goto err_free_irq;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002999 }
3000
3001 return 0;
3002
Michal Kazior5c771e72014-08-22 14:23:34 +02003003err_free_irq:
3004 ath10k_pci_free_irq(ar);
Michal Kazior21396272014-08-28 10:24:40 +02003005 ath10k_pci_kill_tasklet(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02003006
Michal Kazior403d6272014-08-22 14:23:31 +02003007err_deinit_irq:
3008 ath10k_pci_deinit_irq(ar);
3009
Michal Kazior84cbf3a2014-10-20 14:14:39 +02003010err_free_pipes:
3011 ath10k_pci_free_pipes(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02003012
Michal Kaziorc0c378f2014-08-07 11:03:28 +02003013err_sleep:
Michal Kazior0bcbbe62015-05-29 07:35:24 +02003014 ath10k_pci_sleep_sync(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02003015 ath10k_pci_release(ar);
3016
Michal Kaziore7b54192014-08-07 11:03:27 +02003017err_core_destroy:
Kalle Valo5e3dd152013-06-12 20:52:10 +03003018 ath10k_core_destroy(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003019
3020 return ret;
3021}
3022
3023static void ath10k_pci_remove(struct pci_dev *pdev)
3024{
3025 struct ath10k *ar = pci_get_drvdata(pdev);
3026 struct ath10k_pci *ar_pci;
3027
Michal Kazior7aa7a722014-08-25 12:09:38 +02003028 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci remove\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03003029
3030 if (!ar)
3031 return;
3032
3033 ar_pci = ath10k_pci_priv(ar);
3034
3035 if (!ar_pci)
3036 return;
3037
Kalle Valo5e3dd152013-06-12 20:52:10 +03003038 ath10k_core_unregister(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02003039 ath10k_pci_free_irq(ar);
Michal Kazior21396272014-08-28 10:24:40 +02003040 ath10k_pci_kill_tasklet(ar);
Michal Kazior403d6272014-08-22 14:23:31 +02003041 ath10k_pci_deinit_irq(ar);
3042 ath10k_pci_ce_deinit(ar);
Michal Kazior84cbf3a2014-10-20 14:14:39 +02003043 ath10k_pci_free_pipes(ar);
Michal Kazior77258d42015-05-18 09:38:18 +00003044 ath10k_pci_sleep_sync(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02003045 ath10k_pci_release(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003046 ath10k_core_destroy(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003047}
3048
Kalle Valo5e3dd152013-06-12 20:52:10 +03003049MODULE_DEVICE_TABLE(pci, ath10k_pci_id_table);
3050
3051static struct pci_driver ath10k_pci_driver = {
3052 .name = "ath10k_pci",
3053 .id_table = ath10k_pci_id_table,
3054 .probe = ath10k_pci_probe,
3055 .remove = ath10k_pci_remove,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003056};
3057
3058static int __init ath10k_pci_init(void)
3059{
3060 int ret;
3061
3062 ret = pci_register_driver(&ath10k_pci_driver);
3063 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003064 printk(KERN_ERR "failed to register ath10k pci driver: %d\n",
3065 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003066
3067 return ret;
3068}
3069module_init(ath10k_pci_init);
3070
3071static void __exit ath10k_pci_exit(void)
3072{
3073 pci_unregister_driver(&ath10k_pci_driver);
3074}
3075
3076module_exit(ath10k_pci_exit);
3077
3078MODULE_AUTHOR("Qualcomm Atheros");
3079MODULE_DESCRIPTION("Driver support for Atheros QCA988X PCIe devices");
3080MODULE_LICENSE("Dual BSD/GPL");
Bartosz Markowski5c427f52015-02-18 13:16:37 +01003081
3082/* QCA988x 2.0 firmware files */
Bartosz Markowski8026cae2014-10-06 14:16:41 +02003083MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_FW_FILE);
3084MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API2_FILE);
3085MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API3_FILE);
Bartosz Markowski5c427f52015-02-18 13:16:37 +01003086MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API4_FILE);
Kalle Valo53513c32015-03-25 13:12:42 +02003087MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API5_FILE);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003088MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_BOARD_DATA_FILE);
Manikanta Pubbisetty0a51b342015-10-09 11:55:58 +03003089MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_BOARD_API2_FILE);
Bartosz Markowski5c427f52015-02-18 13:16:37 +01003090
3091/* QCA6174 2.1 firmware files */
3092MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" ATH10K_FW_API4_FILE);
Michal Kaziore451c1d2015-05-26 13:09:22 +02003093MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" ATH10K_FW_API5_FILE);
Bartosz Markowski5c427f52015-02-18 13:16:37 +01003094MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" QCA6174_HW_2_1_BOARD_DATA_FILE);
Manikanta Pubbisetty0a51b342015-10-09 11:55:58 +03003095MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" ATH10K_BOARD_API2_FILE);
Bartosz Markowski5c427f52015-02-18 13:16:37 +01003096
3097/* QCA6174 3.1 firmware files */
3098MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" ATH10K_FW_API4_FILE);
Michal Kaziore451c1d2015-05-26 13:09:22 +02003099MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" ATH10K_FW_API5_FILE);
Bartosz Markowski5c427f52015-02-18 13:16:37 +01003100MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" QCA6174_HW_3_0_BOARD_DATA_FILE);
Manikanta Pubbisetty0a51b342015-10-09 11:55:58 +03003101MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" ATH10K_BOARD_API2_FILE);