blob: 3fca200b986ca75baa0de2568d36460be709d834 [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)
Bartosz Markowskia226b512015-10-28 15:09:53 +010064#define QCA9377_1_0_DEVICE_ID (0x0042)
Kalle Valo5e3dd152013-06-12 20:52:10 +030065
Benoit Taine9baa3c32014-08-08 15:56:03 +020066static const struct pci_device_id ath10k_pci_id_table[] = {
Kalle Valo5e3dd152013-06-12 20:52:10 +030067 { PCI_VDEVICE(ATHEROS, QCA988X_2_0_DEVICE_ID) }, /* PCI-E QCA988X V2 */
Michal Kazior36582e52015-08-13 14:32:26 +020068 { PCI_VDEVICE(ATHEROS, QCA6164_2_1_DEVICE_ID) }, /* PCI-E QCA6164 V2.1 */
Michal Kaziord63955b2015-01-24 12:14:49 +020069 { PCI_VDEVICE(ATHEROS, QCA6174_2_1_DEVICE_ID) }, /* PCI-E QCA6174 V2.1 */
Vasanthakumar Thiagarajan8a055a82015-07-29 11:40:39 +030070 { PCI_VDEVICE(ATHEROS, QCA99X0_2_0_DEVICE_ID) }, /* PCI-E QCA99X0 V2 */
Bartosz Markowskia226b512015-10-28 15:09:53 +010071 { PCI_VDEVICE(ATHEROS, QCA9377_1_0_DEVICE_ID) }, /* PCI-E QCA9377 V1 */
Kalle Valo5e3dd152013-06-12 20:52:10 +030072 {0}
73};
74
Michal Kazior7505f7c2014-12-02 10:55:54 +020075static const struct ath10k_pci_supp_chip ath10k_pci_supp_chips[] = {
76 /* QCA988X pre 2.0 chips are not supported because they need some nasty
77 * hacks. ath10k doesn't have them and these devices crash horribly
78 * because of that.
79 */
80 { QCA988X_2_0_DEVICE_ID, QCA988X_HW_2_0_CHIP_ID_REV },
Michal Kazior36582e52015-08-13 14:32:26 +020081
82 { QCA6164_2_1_DEVICE_ID, QCA6174_HW_2_1_CHIP_ID_REV },
83 { QCA6164_2_1_DEVICE_ID, QCA6174_HW_2_2_CHIP_ID_REV },
84 { QCA6164_2_1_DEVICE_ID, QCA6174_HW_3_0_CHIP_ID_REV },
85 { QCA6164_2_1_DEVICE_ID, QCA6174_HW_3_1_CHIP_ID_REV },
86 { QCA6164_2_1_DEVICE_ID, QCA6174_HW_3_2_CHIP_ID_REV },
87
Michal Kaziord63955b2015-01-24 12:14:49 +020088 { QCA6174_2_1_DEVICE_ID, QCA6174_HW_2_1_CHIP_ID_REV },
89 { QCA6174_2_1_DEVICE_ID, QCA6174_HW_2_2_CHIP_ID_REV },
90 { QCA6174_2_1_DEVICE_ID, QCA6174_HW_3_0_CHIP_ID_REV },
91 { QCA6174_2_1_DEVICE_ID, QCA6174_HW_3_1_CHIP_ID_REV },
92 { QCA6174_2_1_DEVICE_ID, QCA6174_HW_3_2_CHIP_ID_REV },
Michal Kazior36582e52015-08-13 14:32:26 +020093
Vasanthakumar Thiagarajan8a055a82015-07-29 11:40:39 +030094 { QCA99X0_2_0_DEVICE_ID, QCA99X0_HW_2_0_CHIP_ID_REV },
Bartosz Markowskia226b512015-10-28 15:09:53 +010095 { QCA9377_1_0_DEVICE_ID, QCA9377_HW_1_0_CHIP_ID_REV },
Michal Kazior7505f7c2014-12-02 10:55:54 +020096};
97
Michal Kazior728f95e2014-08-22 14:33:14 +020098static void ath10k_pci_buffer_cleanup(struct ath10k *ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +010099static int ath10k_pci_cold_reset(struct ath10k *ar);
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +0530100static int ath10k_pci_safe_chip_reset(struct ath10k *ar);
Michal Kaziord7fb47f2013-11-08 08:01:26 +0100101static int ath10k_pci_wait_for_target_init(struct ath10k *ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +0100102static int ath10k_pci_init_irq(struct ath10k *ar);
103static int ath10k_pci_deinit_irq(struct ath10k *ar);
104static int ath10k_pci_request_irq(struct ath10k *ar);
105static void ath10k_pci_free_irq(struct ath10k *ar);
Michal Kazior85622cd2013-11-25 14:06:22 +0100106static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
107 struct ath10k_ce_pipe *rx_pipe,
108 struct bmi_xfer *xfer);
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +0530109static int ath10k_pci_qca99x0_chip_reset(struct ath10k *ar);
Rajkumar Manoharan0e5b2952015-10-12 18:27:01 +0530110static void ath10k_pci_htc_tx_cb(struct ath10k_ce_pipe *ce_state);
Rajkumar Manoharan9d9bdbb2015-10-12 18:27:02 +0530111static void ath10k_pci_htc_rx_cb(struct ath10k_ce_pipe *ce_state);
Rajkumar Manoharana70587b2015-10-12 18:27:04 +0530112static void ath10k_pci_htt_tx_cb(struct ath10k_ce_pipe *ce_state);
113static void ath10k_pci_htt_rx_cb(struct ath10k_ce_pipe *ce_state);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300114
115static const struct ce_attr host_ce_config_wlan[] = {
Kalle Valo48e9c222013-09-01 10:01:32 +0300116 /* CE0: host->target HTC control and raw streams */
117 {
118 .flags = CE_ATTR_FLAGS,
119 .src_nentries = 16,
120 .src_sz_max = 256,
121 .dest_nentries = 0,
Rajkumar Manoharan0e5b2952015-10-12 18:27:01 +0530122 .send_cb = ath10k_pci_htc_tx_cb,
Kalle Valo48e9c222013-09-01 10:01:32 +0300123 },
124
125 /* CE1: target->host HTT + HTC control */
126 {
127 .flags = CE_ATTR_FLAGS,
128 .src_nentries = 0,
Michal Kazior63838642015-02-09 15:04:55 +0100129 .src_sz_max = 2048,
Kalle Valo48e9c222013-09-01 10:01:32 +0300130 .dest_nentries = 512,
Rajkumar Manoharan9d9bdbb2015-10-12 18:27:02 +0530131 .recv_cb = ath10k_pci_htc_rx_cb,
Kalle Valo48e9c222013-09-01 10:01:32 +0300132 },
133
134 /* CE2: target->host WMI */
135 {
136 .flags = CE_ATTR_FLAGS,
137 .src_nentries = 0,
138 .src_sz_max = 2048,
Rajkumar Manoharan30abb332015-03-04 15:43:44 +0200139 .dest_nentries = 128,
Rajkumar Manoharan9d9bdbb2015-10-12 18:27:02 +0530140 .recv_cb = ath10k_pci_htc_rx_cb,
Kalle Valo48e9c222013-09-01 10:01:32 +0300141 },
142
143 /* CE3: host->target WMI */
144 {
145 .flags = CE_ATTR_FLAGS,
146 .src_nentries = 32,
147 .src_sz_max = 2048,
148 .dest_nentries = 0,
Rajkumar Manoharan0e5b2952015-10-12 18:27:01 +0530149 .send_cb = ath10k_pci_htc_tx_cb,
Kalle Valo48e9c222013-09-01 10:01:32 +0300150 },
151
152 /* CE4: host->target HTT */
153 {
154 .flags = CE_ATTR_FLAGS | CE_ATTR_DIS_INTR,
155 .src_nentries = CE_HTT_H2T_MSG_SRC_NENTRIES,
156 .src_sz_max = 256,
157 .dest_nentries = 0,
Rajkumar Manoharana70587b2015-10-12 18:27:04 +0530158 .send_cb = ath10k_pci_htt_tx_cb,
Kalle Valo48e9c222013-09-01 10:01:32 +0300159 },
160
Rajkumar Manoharana70587b2015-10-12 18:27:04 +0530161 /* CE5: target->host HTT (HIF->HTT) */
Kalle Valo48e9c222013-09-01 10:01:32 +0300162 {
163 .flags = CE_ATTR_FLAGS,
164 .src_nentries = 0,
Rajkumar Manoharana70587b2015-10-12 18:27:04 +0530165 .src_sz_max = 512,
166 .dest_nentries = 512,
167 .recv_cb = ath10k_pci_htt_rx_cb,
Kalle Valo48e9c222013-09-01 10:01:32 +0300168 },
169
170 /* CE6: target autonomous hif_memcpy */
171 {
172 .flags = CE_ATTR_FLAGS,
173 .src_nentries = 0,
174 .src_sz_max = 0,
175 .dest_nentries = 0,
176 },
177
178 /* CE7: ce_diag, the Diagnostic Window */
179 {
180 .flags = CE_ATTR_FLAGS,
181 .src_nentries = 2,
182 .src_sz_max = DIAG_TRANSFER_LIMIT,
183 .dest_nentries = 2,
184 },
Vasanthakumar Thiagarajan050af062015-06-18 12:31:04 +0530185
186 /* CE8: target->host pktlog */
187 {
188 .flags = CE_ATTR_FLAGS,
189 .src_nentries = 0,
190 .src_sz_max = 2048,
191 .dest_nentries = 128,
192 },
193
194 /* CE9 target autonomous qcache memcpy */
195 {
196 .flags = CE_ATTR_FLAGS,
197 .src_nentries = 0,
198 .src_sz_max = 0,
199 .dest_nentries = 0,
200 },
201
202 /* CE10: target autonomous hif memcpy */
203 {
204 .flags = CE_ATTR_FLAGS,
205 .src_nentries = 0,
206 .src_sz_max = 0,
207 .dest_nentries = 0,
208 },
209
210 /* CE11: target autonomous hif memcpy */
211 {
212 .flags = CE_ATTR_FLAGS,
213 .src_nentries = 0,
214 .src_sz_max = 0,
215 .dest_nentries = 0,
216 },
Kalle Valo5e3dd152013-06-12 20:52:10 +0300217};
218
219/* Target firmware's Copy Engine configuration. */
220static const struct ce_pipe_config target_ce_config_wlan[] = {
Kalle Valod88effb2013-09-01 10:01:39 +0300221 /* CE0: host->target HTC control and raw streams */
222 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300223 .pipenum = __cpu_to_le32(0),
224 .pipedir = __cpu_to_le32(PIPEDIR_OUT),
225 .nentries = __cpu_to_le32(32),
226 .nbytes_max = __cpu_to_le32(256),
227 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
228 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300229 },
230
231 /* CE1: target->host HTT + HTC control */
232 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300233 .pipenum = __cpu_to_le32(1),
234 .pipedir = __cpu_to_le32(PIPEDIR_IN),
235 .nentries = __cpu_to_le32(32),
Michal Kazior63838642015-02-09 15:04:55 +0100236 .nbytes_max = __cpu_to_le32(2048),
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300237 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
238 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300239 },
240
241 /* CE2: target->host WMI */
242 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300243 .pipenum = __cpu_to_le32(2),
244 .pipedir = __cpu_to_le32(PIPEDIR_IN),
Rajkumar Manoharan30abb332015-03-04 15:43:44 +0200245 .nentries = __cpu_to_le32(64),
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300246 .nbytes_max = __cpu_to_le32(2048),
247 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
248 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300249 },
250
251 /* CE3: host->target WMI */
252 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300253 .pipenum = __cpu_to_le32(3),
254 .pipedir = __cpu_to_le32(PIPEDIR_OUT),
255 .nentries = __cpu_to_le32(32),
256 .nbytes_max = __cpu_to_le32(2048),
257 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
258 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300259 },
260
261 /* CE4: host->target HTT */
262 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300263 .pipenum = __cpu_to_le32(4),
264 .pipedir = __cpu_to_le32(PIPEDIR_OUT),
265 .nentries = __cpu_to_le32(256),
266 .nbytes_max = __cpu_to_le32(256),
267 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
268 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300269 },
270
Kalle Valo5e3dd152013-06-12 20:52:10 +0300271 /* NB: 50% of src nentries, since tx has 2 frags */
Kalle Valod88effb2013-09-01 10:01:39 +0300272
Rajkumar Manoharana70587b2015-10-12 18:27:04 +0530273 /* CE5: target->host HTT (HIF->HTT) */
Kalle Valod88effb2013-09-01 10:01:39 +0300274 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300275 .pipenum = __cpu_to_le32(5),
Rajkumar Manoharana70587b2015-10-12 18:27:04 +0530276 .pipedir = __cpu_to_le32(PIPEDIR_IN),
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300277 .nentries = __cpu_to_le32(32),
Rajkumar Manoharana70587b2015-10-12 18:27:04 +0530278 .nbytes_max = __cpu_to_le32(512),
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300279 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
280 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300281 },
282
283 /* CE6: Reserved for target autonomous hif_memcpy */
284 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300285 .pipenum = __cpu_to_le32(6),
286 .pipedir = __cpu_to_le32(PIPEDIR_INOUT),
287 .nentries = __cpu_to_le32(32),
288 .nbytes_max = __cpu_to_le32(4096),
289 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
290 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300291 },
292
Kalle Valo5e3dd152013-06-12 20:52:10 +0300293 /* CE7 used only by Host */
Vasanthakumar Thiagarajan050af062015-06-18 12:31:04 +0530294 {
295 .pipenum = __cpu_to_le32(7),
296 .pipedir = __cpu_to_le32(PIPEDIR_INOUT),
297 .nentries = __cpu_to_le32(0),
298 .nbytes_max = __cpu_to_le32(0),
299 .flags = __cpu_to_le32(0),
300 .reserved = __cpu_to_le32(0),
301 },
302
303 /* CE8 target->host packtlog */
304 {
305 .pipenum = __cpu_to_le32(8),
306 .pipedir = __cpu_to_le32(PIPEDIR_IN),
307 .nentries = __cpu_to_le32(64),
308 .nbytes_max = __cpu_to_le32(2048),
309 .flags = __cpu_to_le32(CE_ATTR_FLAGS | CE_ATTR_DIS_INTR),
310 .reserved = __cpu_to_le32(0),
311 },
312
313 /* CE9 target autonomous qcache memcpy */
314 {
315 .pipenum = __cpu_to_le32(9),
316 .pipedir = __cpu_to_le32(PIPEDIR_INOUT),
317 .nentries = __cpu_to_le32(32),
318 .nbytes_max = __cpu_to_le32(2048),
319 .flags = __cpu_to_le32(CE_ATTR_FLAGS | CE_ATTR_DIS_INTR),
320 .reserved = __cpu_to_le32(0),
321 },
322
323 /* It not necessary to send target wlan configuration for CE10 & CE11
324 * as these CEs are not actively used in target.
325 */
Kalle Valo5e3dd152013-06-12 20:52:10 +0300326};
327
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300328/*
329 * Map from service/endpoint to Copy Engine.
330 * This table is derived from the CE_PCI TABLE, above.
331 * It is passed to the Target at startup for use by firmware.
332 */
333static const struct service_to_pipe target_service_to_ce_map_wlan[] = {
334 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300335 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VO),
336 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
337 __cpu_to_le32(3),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300338 },
339 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300340 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VO),
341 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
342 __cpu_to_le32(2),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300343 },
344 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300345 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BK),
346 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
347 __cpu_to_le32(3),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300348 },
349 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300350 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BK),
351 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
352 __cpu_to_le32(2),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300353 },
354 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300355 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BE),
356 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
357 __cpu_to_le32(3),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300358 },
359 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300360 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BE),
361 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
362 __cpu_to_le32(2),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300363 },
364 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300365 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VI),
366 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
367 __cpu_to_le32(3),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300368 },
369 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300370 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VI),
371 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
372 __cpu_to_le32(2),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300373 },
374 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300375 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_CONTROL),
376 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
377 __cpu_to_le32(3),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300378 },
379 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300380 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_CONTROL),
381 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
382 __cpu_to_le32(2),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300383 },
384 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300385 __cpu_to_le32(ATH10K_HTC_SVC_ID_RSVD_CTRL),
386 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
387 __cpu_to_le32(0),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300388 },
389 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300390 __cpu_to_le32(ATH10K_HTC_SVC_ID_RSVD_CTRL),
391 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
392 __cpu_to_le32(1),
393 },
394 { /* not used */
395 __cpu_to_le32(ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS),
396 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
397 __cpu_to_le32(0),
398 },
399 { /* not used */
400 __cpu_to_le32(ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS),
401 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
402 __cpu_to_le32(1),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300403 },
404 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300405 __cpu_to_le32(ATH10K_HTC_SVC_ID_HTT_DATA_MSG),
406 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
407 __cpu_to_le32(4),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300408 },
409 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300410 __cpu_to_le32(ATH10K_HTC_SVC_ID_HTT_DATA_MSG),
411 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
Rajkumar Manoharana70587b2015-10-12 18:27:04 +0530412 __cpu_to_le32(5),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300413 },
414
415 /* (Additions here) */
416
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300417 { /* must be last */
418 __cpu_to_le32(0),
419 __cpu_to_le32(0),
420 __cpu_to_le32(0),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300421 },
422};
423
Michal Kazior77258d42015-05-18 09:38:18 +0000424static bool ath10k_pci_is_awake(struct ath10k *ar)
425{
426 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
427 u32 val = ioread32(ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
428 RTC_STATE_ADDRESS);
429
430 return RTC_STATE_V_GET(val) == RTC_STATE_V_ON;
431}
432
433static void __ath10k_pci_wake(struct ath10k *ar)
434{
435 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
436
437 lockdep_assert_held(&ar_pci->ps_lock);
438
439 ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps wake reg refcount %lu awake %d\n",
440 ar_pci->ps_wake_refcount, ar_pci->ps_awake);
441
442 iowrite32(PCIE_SOC_WAKE_V_MASK,
443 ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
444 PCIE_SOC_WAKE_ADDRESS);
445}
446
447static void __ath10k_pci_sleep(struct ath10k *ar)
448{
449 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
450
451 lockdep_assert_held(&ar_pci->ps_lock);
452
453 ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps sleep reg refcount %lu awake %d\n",
454 ar_pci->ps_wake_refcount, ar_pci->ps_awake);
455
456 iowrite32(PCIE_SOC_WAKE_RESET,
457 ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
458 PCIE_SOC_WAKE_ADDRESS);
459 ar_pci->ps_awake = false;
460}
461
462static int ath10k_pci_wake_wait(struct ath10k *ar)
463{
464 int tot_delay = 0;
465 int curr_delay = 5;
466
467 while (tot_delay < PCIE_WAKE_TIMEOUT) {
Maharaja Kennadyrajan39b91b82015-10-06 15:19:28 +0300468 if (ath10k_pci_is_awake(ar)) {
469 if (tot_delay > PCIE_WAKE_LATE_US)
470 ath10k_warn(ar, "device wakeup took %d ms which is unusally long, otherwise it works normally.\n",
471 tot_delay / 1000);
Michal Kazior77258d42015-05-18 09:38:18 +0000472 return 0;
Maharaja Kennadyrajan39b91b82015-10-06 15:19:28 +0300473 }
Michal Kazior77258d42015-05-18 09:38:18 +0000474
475 udelay(curr_delay);
476 tot_delay += curr_delay;
477
478 if (curr_delay < 50)
479 curr_delay += 5;
480 }
481
482 return -ETIMEDOUT;
483}
484
Anilkumar Kolli1aaf8ef2015-10-16 15:54:51 +0300485static int ath10k_pci_force_wake(struct ath10k *ar)
486{
487 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
488 unsigned long flags;
489 int ret = 0;
490
491 spin_lock_irqsave(&ar_pci->ps_lock, flags);
492
493 if (!ar_pci->ps_awake) {
494 iowrite32(PCIE_SOC_WAKE_V_MASK,
495 ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
496 PCIE_SOC_WAKE_ADDRESS);
497
498 ret = ath10k_pci_wake_wait(ar);
499 if (ret == 0)
500 ar_pci->ps_awake = true;
501 }
502
503 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
504
505 return ret;
506}
507
508static void ath10k_pci_force_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 iowrite32(PCIE_SOC_WAKE_RESET,
516 ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
517 PCIE_SOC_WAKE_ADDRESS);
518 ar_pci->ps_awake = false;
519
520 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
521}
522
Michal Kazior77258d42015-05-18 09:38:18 +0000523static int ath10k_pci_wake(struct ath10k *ar)
524{
525 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
526 unsigned long flags;
527 int ret = 0;
528
Anilkumar Kolli1aaf8ef2015-10-16 15:54:51 +0300529 if (ar_pci->pci_ps == 0)
530 return ret;
531
Michal Kazior77258d42015-05-18 09:38:18 +0000532 spin_lock_irqsave(&ar_pci->ps_lock, flags);
533
534 ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps wake refcount %lu awake %d\n",
535 ar_pci->ps_wake_refcount, ar_pci->ps_awake);
536
537 /* This function can be called very frequently. To avoid excessive
538 * CPU stalls for MMIO reads use a cache var to hold the device state.
539 */
540 if (!ar_pci->ps_awake) {
541 __ath10k_pci_wake(ar);
542
543 ret = ath10k_pci_wake_wait(ar);
544 if (ret == 0)
545 ar_pci->ps_awake = true;
546 }
547
548 if (ret == 0) {
549 ar_pci->ps_wake_refcount++;
550 WARN_ON(ar_pci->ps_wake_refcount == 0);
551 }
552
553 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
554
555 return ret;
556}
557
558static void ath10k_pci_sleep(struct ath10k *ar)
559{
560 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
561 unsigned long flags;
562
Anilkumar Kolli1aaf8ef2015-10-16 15:54:51 +0300563 if (ar_pci->pci_ps == 0)
564 return;
565
Michal Kazior77258d42015-05-18 09:38:18 +0000566 spin_lock_irqsave(&ar_pci->ps_lock, flags);
567
568 ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps sleep refcount %lu awake %d\n",
569 ar_pci->ps_wake_refcount, ar_pci->ps_awake);
570
571 if (WARN_ON(ar_pci->ps_wake_refcount == 0))
572 goto skip;
573
574 ar_pci->ps_wake_refcount--;
575
576 mod_timer(&ar_pci->ps_timer, jiffies +
577 msecs_to_jiffies(ATH10K_PCI_SLEEP_GRACE_PERIOD_MSEC));
578
579skip:
580 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
581}
582
583static void ath10k_pci_ps_timer(unsigned long ptr)
584{
585 struct ath10k *ar = (void *)ptr;
586 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
587 unsigned long flags;
588
589 spin_lock_irqsave(&ar_pci->ps_lock, flags);
590
591 ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps timer refcount %lu awake %d\n",
592 ar_pci->ps_wake_refcount, ar_pci->ps_awake);
593
594 if (ar_pci->ps_wake_refcount > 0)
595 goto skip;
596
597 __ath10k_pci_sleep(ar);
598
599skip:
600 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
601}
602
603static void ath10k_pci_sleep_sync(struct ath10k *ar)
604{
605 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
606 unsigned long flags;
607
Anilkumar Kolli1aaf8ef2015-10-16 15:54:51 +0300608 if (ar_pci->pci_ps == 0) {
609 ath10k_pci_force_sleep(ar);
610 return;
611 }
612
Michal Kazior77258d42015-05-18 09:38:18 +0000613 del_timer_sync(&ar_pci->ps_timer);
614
615 spin_lock_irqsave(&ar_pci->ps_lock, flags);
616 WARN_ON(ar_pci->ps_wake_refcount > 0);
617 __ath10k_pci_sleep(ar);
618 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
619}
620
621void ath10k_pci_write32(struct ath10k *ar, u32 offset, u32 value)
622{
623 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
624 int ret;
625
Michal Kazioraeae5b42015-06-15 14:46:42 +0300626 if (unlikely(offset + sizeof(value) > ar_pci->mem_len)) {
627 ath10k_warn(ar, "refusing to write mmio out of bounds at 0x%08x - 0x%08zx (max 0x%08zx)\n",
628 offset, offset + sizeof(value), ar_pci->mem_len);
629 return;
630 }
631
Michal Kazior77258d42015-05-18 09:38:18 +0000632 ret = ath10k_pci_wake(ar);
633 if (ret) {
634 ath10k_warn(ar, "failed to wake target for write32 of 0x%08x at 0x%08x: %d\n",
635 value, offset, ret);
636 return;
637 }
638
639 iowrite32(value, ar_pci->mem + offset);
640 ath10k_pci_sleep(ar);
641}
642
643u32 ath10k_pci_read32(struct ath10k *ar, u32 offset)
644{
645 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
646 u32 val;
647 int ret;
648
Michal Kazioraeae5b42015-06-15 14:46:42 +0300649 if (unlikely(offset + sizeof(val) > ar_pci->mem_len)) {
650 ath10k_warn(ar, "refusing to read mmio out of bounds at 0x%08x - 0x%08zx (max 0x%08zx)\n",
651 offset, offset + sizeof(val), ar_pci->mem_len);
652 return 0;
653 }
654
Michal Kazior77258d42015-05-18 09:38:18 +0000655 ret = ath10k_pci_wake(ar);
656 if (ret) {
657 ath10k_warn(ar, "failed to wake target for read32 at 0x%08x: %d\n",
658 offset, ret);
659 return 0xffffffff;
660 }
661
662 val = ioread32(ar_pci->mem + offset);
663 ath10k_pci_sleep(ar);
664
665 return val;
666}
667
668u32 ath10k_pci_soc_read32(struct ath10k *ar, u32 addr)
669{
670 return ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS + addr);
671}
672
673void ath10k_pci_soc_write32(struct ath10k *ar, u32 addr, u32 val)
674{
675 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + addr, val);
676}
677
678u32 ath10k_pci_reg_read32(struct ath10k *ar, u32 addr)
679{
680 return ath10k_pci_read32(ar, PCIE_LOCAL_BASE_ADDRESS + addr);
681}
682
683void ath10k_pci_reg_write32(struct ath10k *ar, u32 addr, u32 val)
684{
685 ath10k_pci_write32(ar, PCIE_LOCAL_BASE_ADDRESS + addr, val);
686}
687
Michal Kaziore5398872013-11-25 14:06:20 +0100688static bool ath10k_pci_irq_pending(struct ath10k *ar)
689{
690 u32 cause;
691
692 /* Check if the shared legacy irq is for us */
693 cause = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
694 PCIE_INTR_CAUSE_ADDRESS);
695 if (cause & (PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL))
696 return true;
697
698 return false;
699}
700
Michal Kazior26852182013-11-25 14:06:25 +0100701static void ath10k_pci_disable_and_clear_legacy_irq(struct ath10k *ar)
702{
703 /* IMPORTANT: INTR_CLR register has to be set after
704 * INTR_ENABLE is set to 0, otherwise interrupt can not be
705 * really cleared. */
706 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
707 0);
708 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_CLR_ADDRESS,
709 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
710
711 /* IMPORTANT: this extra read transaction is required to
712 * flush the posted write buffer. */
Kalle Valocfbc06a2014-09-14 12:50:23 +0300713 (void)ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
714 PCIE_INTR_ENABLE_ADDRESS);
Michal Kazior26852182013-11-25 14:06:25 +0100715}
716
717static void ath10k_pci_enable_legacy_irq(struct ath10k *ar)
718{
719 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
720 PCIE_INTR_ENABLE_ADDRESS,
721 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
722
723 /* IMPORTANT: this extra read transaction is required to
724 * flush the posted write buffer. */
Kalle Valocfbc06a2014-09-14 12:50:23 +0300725 (void)ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
726 PCIE_INTR_ENABLE_ADDRESS);
Michal Kazior26852182013-11-25 14:06:25 +0100727}
728
Michal Kazior403d6272014-08-22 14:23:31 +0200729static inline const char *ath10k_pci_get_irq_method(struct ath10k *ar)
Michal Kaziorab977bd2013-11-25 14:06:26 +0100730{
Michal Kaziorab977bd2013-11-25 14:06:26 +0100731 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
732
Michal Kazior403d6272014-08-22 14:23:31 +0200733 if (ar_pci->num_msi_intrs > 1)
734 return "msi-x";
Kalle Valod8bb26b2014-09-14 12:50:33 +0300735
736 if (ar_pci->num_msi_intrs == 1)
Michal Kazior403d6272014-08-22 14:23:31 +0200737 return "msi";
Kalle Valod8bb26b2014-09-14 12:50:33 +0300738
739 return "legacy";
Michal Kaziorab977bd2013-11-25 14:06:26 +0100740}
741
Michal Kazior728f95e2014-08-22 14:33:14 +0200742static int __ath10k_pci_rx_post_buf(struct ath10k_pci_pipe *pipe)
Michal Kaziorab977bd2013-11-25 14:06:26 +0100743{
Michal Kazior728f95e2014-08-22 14:33:14 +0200744 struct ath10k *ar = pipe->hif_ce_state;
Michal Kaziorab977bd2013-11-25 14:06:26 +0100745 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior728f95e2014-08-22 14:33:14 +0200746 struct ath10k_ce_pipe *ce_pipe = pipe->ce_hdl;
747 struct sk_buff *skb;
748 dma_addr_t paddr;
Michal Kaziorab977bd2013-11-25 14:06:26 +0100749 int ret;
750
Michal Kazior728f95e2014-08-22 14:33:14 +0200751 skb = dev_alloc_skb(pipe->buf_sz);
752 if (!skb)
753 return -ENOMEM;
754
755 WARN_ONCE((unsigned long)skb->data & 3, "unaligned skb");
756
757 paddr = dma_map_single(ar->dev, skb->data,
758 skb->len + skb_tailroom(skb),
759 DMA_FROM_DEVICE);
760 if (unlikely(dma_mapping_error(ar->dev, paddr))) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200761 ath10k_warn(ar, "failed to dma map pci rx buf\n");
Michal Kazior728f95e2014-08-22 14:33:14 +0200762 dev_kfree_skb_any(skb);
763 return -EIO;
764 }
765
Michal Kazior8582bf32015-01-24 12:14:47 +0200766 ATH10K_SKB_RXCB(skb)->paddr = paddr;
Michal Kazior728f95e2014-08-22 14:33:14 +0200767
Rajkumar Manoharanab4e3db2015-10-06 15:19:33 +0300768 spin_lock_bh(&ar_pci->ce_lock);
Michal Kazior728f95e2014-08-22 14:33:14 +0200769 ret = __ath10k_ce_rx_post_buf(ce_pipe, skb, paddr);
Rajkumar Manoharanab4e3db2015-10-06 15:19:33 +0300770 spin_unlock_bh(&ar_pci->ce_lock);
Michal Kaziorab977bd2013-11-25 14:06:26 +0100771 if (ret) {
Michal Kazior728f95e2014-08-22 14:33:14 +0200772 dma_unmap_single(ar->dev, paddr, skb->len + skb_tailroom(skb),
773 DMA_FROM_DEVICE);
774 dev_kfree_skb_any(skb);
Michal Kaziorab977bd2013-11-25 14:06:26 +0100775 return ret;
776 }
777
778 return 0;
779}
780
Rajkumar Manoharanab4e3db2015-10-06 15:19:33 +0300781static void ath10k_pci_rx_post_pipe(struct ath10k_pci_pipe *pipe)
Michal Kaziorab977bd2013-11-25 14:06:26 +0100782{
Michal Kazior728f95e2014-08-22 14:33:14 +0200783 struct ath10k *ar = pipe->hif_ce_state;
784 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
785 struct ath10k_ce_pipe *ce_pipe = pipe->ce_hdl;
786 int ret, num;
787
Michal Kazior728f95e2014-08-22 14:33:14 +0200788 if (pipe->buf_sz == 0)
789 return;
790
791 if (!ce_pipe->dest_ring)
792 return;
793
Rajkumar Manoharanab4e3db2015-10-06 15:19:33 +0300794 spin_lock_bh(&ar_pci->ce_lock);
Michal Kazior728f95e2014-08-22 14:33:14 +0200795 num = __ath10k_ce_rx_num_free_bufs(ce_pipe);
Rajkumar Manoharanab4e3db2015-10-06 15:19:33 +0300796 spin_unlock_bh(&ar_pci->ce_lock);
Michal Kazior728f95e2014-08-22 14:33:14 +0200797 while (num--) {
798 ret = __ath10k_pci_rx_post_buf(pipe);
799 if (ret) {
Rajkumar Manoharanab4e3db2015-10-06 15:19:33 +0300800 if (ret == -ENOSPC)
801 break;
Michal Kazior7aa7a722014-08-25 12:09:38 +0200802 ath10k_warn(ar, "failed to post pci rx buf: %d\n", ret);
Michal Kazior728f95e2014-08-22 14:33:14 +0200803 mod_timer(&ar_pci->rx_post_retry, jiffies +
804 ATH10K_PCI_RX_POST_RETRY_MS);
805 break;
806 }
807 }
808}
809
Michal Kazior728f95e2014-08-22 14:33:14 +0200810static void ath10k_pci_rx_post(struct ath10k *ar)
811{
812 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
813 int i;
814
Michal Kazior728f95e2014-08-22 14:33:14 +0200815 for (i = 0; i < CE_COUNT; i++)
Rajkumar Manoharanab4e3db2015-10-06 15:19:33 +0300816 ath10k_pci_rx_post_pipe(&ar_pci->pipe_info[i]);
Michal Kazior728f95e2014-08-22 14:33:14 +0200817}
818
819static void ath10k_pci_rx_replenish_retry(unsigned long ptr)
820{
821 struct ath10k *ar = (void *)ptr;
822
823 ath10k_pci_rx_post(ar);
Michal Kaziorab977bd2013-11-25 14:06:26 +0100824}
825
Vasanthakumar Thiagarajan418ca592015-06-18 12:31:05 +0530826static u32 ath10k_pci_targ_cpu_to_ce_addr(struct ath10k *ar, u32 addr)
827{
828 u32 val = 0;
829
830 switch (ar->hw_rev) {
831 case ATH10K_HW_QCA988X:
832 case ATH10K_HW_QCA6174:
Bartosz Markowskia226b512015-10-28 15:09:53 +0100833 case ATH10K_HW_QCA9377:
Vasanthakumar Thiagarajan418ca592015-06-18 12:31:05 +0530834 val = (ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
835 CORE_CTRL_ADDRESS) &
Vasanthakumar Thiagarajan3c7e2562015-07-03 19:25:27 +0530836 0x7ff) << 21;
Vasanthakumar Thiagarajan418ca592015-06-18 12:31:05 +0530837 break;
838 case ATH10K_HW_QCA99X0:
839 val = ath10k_pci_read32(ar, PCIE_BAR_REG_ADDRESS);
840 break;
841 }
842
843 val |= 0x100000 | (addr & 0xfffff);
844 return val;
845}
846
Kalle Valo5e3dd152013-06-12 20:52:10 +0300847/*
848 * Diagnostic read/write access is provided for startup/config/debug usage.
849 * Caller must guarantee proper alignment, when applicable, and single user
850 * at any moment.
851 */
852static int ath10k_pci_diag_read_mem(struct ath10k *ar, u32 address, void *data,
853 int nbytes)
854{
855 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
856 int ret = 0;
857 u32 buf;
858 unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
859 unsigned int id;
860 unsigned int flags;
Michal Kazior2aa39112013-08-27 13:08:02 +0200861 struct ath10k_ce_pipe *ce_diag;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300862 /* Host buffer address in CE space */
863 u32 ce_data;
864 dma_addr_t ce_data_base = 0;
865 void *data_buf = NULL;
866 int i;
867
Kalle Valoeef25402014-09-24 14:16:52 +0300868 spin_lock_bh(&ar_pci->ce_lock);
869
Kalle Valo5e3dd152013-06-12 20:52:10 +0300870 ce_diag = ar_pci->ce_diag;
871
872 /*
873 * Allocate a temporary bounce buffer to hold caller's data
874 * to be DMA'ed from Target. This guarantees
875 * 1) 4-byte alignment
876 * 2) Buffer in DMA-able space
877 */
878 orig_nbytes = nbytes;
Michal Kazior68c03242014-03-28 10:02:35 +0200879 data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
880 orig_nbytes,
881 &ce_data_base,
882 GFP_ATOMIC);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300883
884 if (!data_buf) {
885 ret = -ENOMEM;
886 goto done;
887 }
888 memset(data_buf, 0, orig_nbytes);
889
890 remaining_bytes = orig_nbytes;
891 ce_data = ce_data_base;
892 while (remaining_bytes) {
893 nbytes = min_t(unsigned int, remaining_bytes,
894 DIAG_TRANSFER_LIMIT);
895
Kalle Valoeef25402014-09-24 14:16:52 +0300896 ret = __ath10k_ce_rx_post_buf(ce_diag, NULL, ce_data);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300897 if (ret != 0)
898 goto done;
899
900 /* Request CE to send from Target(!) address to Host buffer */
901 /*
902 * The address supplied by the caller is in the
903 * Target CPU virtual address space.
904 *
905 * In order to use this address with the diagnostic CE,
906 * convert it from Target CPU virtual address space
907 * to CE address space
908 */
Vasanthakumar Thiagarajan418ca592015-06-18 12:31:05 +0530909 address = ath10k_pci_targ_cpu_to_ce_addr(ar, address);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300910
Kalle Valoeef25402014-09-24 14:16:52 +0300911 ret = ath10k_ce_send_nolock(ce_diag, NULL, (u32)address, nbytes, 0,
912 0);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300913 if (ret)
914 goto done;
915
916 i = 0;
Rajkumar Manoharan765952e2015-10-23 18:01:05 +0530917 while (ath10k_ce_completed_send_next_nolock(ce_diag,
918 NULL) != 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300919 mdelay(1);
920 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
921 ret = -EBUSY;
922 goto done;
923 }
924 }
925
Kalle Valo5e3dd152013-06-12 20:52:10 +0300926 i = 0;
Kalle Valoeef25402014-09-24 14:16:52 +0300927 while (ath10k_ce_completed_recv_next_nolock(ce_diag, NULL, &buf,
928 &completed_nbytes,
929 &id, &flags) != 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300930 mdelay(1);
931
932 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
933 ret = -EBUSY;
934 goto done;
935 }
936 }
937
938 if (nbytes != completed_nbytes) {
939 ret = -EIO;
940 goto done;
941 }
942
943 if (buf != ce_data) {
944 ret = -EIO;
945 goto done;
946 }
947
948 remaining_bytes -= nbytes;
949 address += nbytes;
950 ce_data += nbytes;
951 }
952
953done:
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300954 if (ret == 0)
955 memcpy(data, data_buf, orig_nbytes);
956 else
Michal Kazior7aa7a722014-08-25 12:09:38 +0200957 ath10k_warn(ar, "failed to read diag value at 0x%x: %d\n",
Kalle Valo50f87a62014-03-28 09:32:52 +0200958 address, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300959
960 if (data_buf)
Michal Kazior68c03242014-03-28 10:02:35 +0200961 dma_free_coherent(ar->dev, orig_nbytes, data_buf,
962 ce_data_base);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300963
Kalle Valoeef25402014-09-24 14:16:52 +0300964 spin_unlock_bh(&ar_pci->ce_lock);
965
Kalle Valo5e3dd152013-06-12 20:52:10 +0300966 return ret;
967}
968
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300969static int ath10k_pci_diag_read32(struct ath10k *ar, u32 address, u32 *value)
970{
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300971 __le32 val = 0;
972 int ret;
973
974 ret = ath10k_pci_diag_read_mem(ar, address, &val, sizeof(val));
975 *value = __le32_to_cpu(val);
976
977 return ret;
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300978}
979
980static int __ath10k_pci_diag_read_hi(struct ath10k *ar, void *dest,
981 u32 src, u32 len)
982{
983 u32 host_addr, addr;
984 int ret;
985
986 host_addr = host_interest_item_address(src);
987
988 ret = ath10k_pci_diag_read32(ar, host_addr, &addr);
989 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200990 ath10k_warn(ar, "failed to get memcpy hi address for firmware address %d: %d\n",
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300991 src, ret);
992 return ret;
993 }
994
995 ret = ath10k_pci_diag_read_mem(ar, addr, dest, len);
996 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200997 ath10k_warn(ar, "failed to memcpy firmware memory from %d (%d B): %d\n",
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300998 addr, len, ret);
999 return ret;
1000 }
1001
1002 return 0;
1003}
1004
1005#define ath10k_pci_diag_read_hi(ar, dest, src, len) \
Kalle Valo8cc7f262014-09-14 12:50:39 +03001006 __ath10k_pci_diag_read_hi(ar, dest, HI_ITEM(src), len)
Kalle Valo3d29a3e2014-08-25 08:37:26 +03001007
Kalle Valo5e3dd152013-06-12 20:52:10 +03001008static int ath10k_pci_diag_write_mem(struct ath10k *ar, u32 address,
1009 const void *data, int nbytes)
1010{
1011 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1012 int ret = 0;
1013 u32 buf;
1014 unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
1015 unsigned int id;
1016 unsigned int flags;
Michal Kazior2aa39112013-08-27 13:08:02 +02001017 struct ath10k_ce_pipe *ce_diag;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001018 void *data_buf = NULL;
1019 u32 ce_data; /* Host buffer address in CE space */
1020 dma_addr_t ce_data_base = 0;
1021 int i;
1022
Kalle Valoeef25402014-09-24 14:16:52 +03001023 spin_lock_bh(&ar_pci->ce_lock);
1024
Kalle Valo5e3dd152013-06-12 20:52:10 +03001025 ce_diag = ar_pci->ce_diag;
1026
1027 /*
1028 * Allocate a temporary bounce buffer to hold caller's data
1029 * to be DMA'ed to Target. This guarantees
1030 * 1) 4-byte alignment
1031 * 2) Buffer in DMA-able space
1032 */
1033 orig_nbytes = nbytes;
Michal Kazior68c03242014-03-28 10:02:35 +02001034 data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
1035 orig_nbytes,
1036 &ce_data_base,
1037 GFP_ATOMIC);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001038 if (!data_buf) {
1039 ret = -ENOMEM;
1040 goto done;
1041 }
1042
1043 /* Copy caller's data to allocated DMA buf */
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001044 memcpy(data_buf, data, orig_nbytes);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001045
1046 /*
1047 * The address supplied by the caller is in the
1048 * Target CPU virtual address space.
1049 *
1050 * In order to use this address with the diagnostic CE,
1051 * convert it from
1052 * Target CPU virtual address space
1053 * to
1054 * CE address space
1055 */
Vasanthakumar Thiagarajan418ca592015-06-18 12:31:05 +05301056 address = ath10k_pci_targ_cpu_to_ce_addr(ar, address);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001057
1058 remaining_bytes = orig_nbytes;
1059 ce_data = ce_data_base;
1060 while (remaining_bytes) {
1061 /* FIXME: check cast */
1062 nbytes = min_t(int, remaining_bytes, DIAG_TRANSFER_LIMIT);
1063
1064 /* Set up to receive directly into Target(!) address */
Kalle Valoeef25402014-09-24 14:16:52 +03001065 ret = __ath10k_ce_rx_post_buf(ce_diag, NULL, address);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001066 if (ret != 0)
1067 goto done;
1068
1069 /*
1070 * Request CE to send caller-supplied data that
1071 * was copied to bounce buffer to Target(!) address.
1072 */
Kalle Valoeef25402014-09-24 14:16:52 +03001073 ret = ath10k_ce_send_nolock(ce_diag, NULL, (u32)ce_data,
1074 nbytes, 0, 0);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001075 if (ret != 0)
1076 goto done;
1077
1078 i = 0;
Rajkumar Manoharan765952e2015-10-23 18:01:05 +05301079 while (ath10k_ce_completed_send_next_nolock(ce_diag,
1080 NULL) != 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001081 mdelay(1);
1082
1083 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
1084 ret = -EBUSY;
1085 goto done;
1086 }
1087 }
1088
Kalle Valo5e3dd152013-06-12 20:52:10 +03001089 i = 0;
Kalle Valoeef25402014-09-24 14:16:52 +03001090 while (ath10k_ce_completed_recv_next_nolock(ce_diag, NULL, &buf,
1091 &completed_nbytes,
1092 &id, &flags) != 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001093 mdelay(1);
1094
1095 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
1096 ret = -EBUSY;
1097 goto done;
1098 }
1099 }
1100
1101 if (nbytes != completed_nbytes) {
1102 ret = -EIO;
1103 goto done;
1104 }
1105
1106 if (buf != address) {
1107 ret = -EIO;
1108 goto done;
1109 }
1110
1111 remaining_bytes -= nbytes;
1112 address += nbytes;
1113 ce_data += nbytes;
1114 }
1115
1116done:
1117 if (data_buf) {
Michal Kazior68c03242014-03-28 10:02:35 +02001118 dma_free_coherent(ar->dev, orig_nbytes, data_buf,
1119 ce_data_base);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001120 }
1121
1122 if (ret != 0)
Michal Kazior7aa7a722014-08-25 12:09:38 +02001123 ath10k_warn(ar, "failed to write diag value at 0x%x: %d\n",
Kalle Valo50f87a62014-03-28 09:32:52 +02001124 address, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001125
Kalle Valoeef25402014-09-24 14:16:52 +03001126 spin_unlock_bh(&ar_pci->ce_lock);
1127
Kalle Valo5e3dd152013-06-12 20:52:10 +03001128 return ret;
1129}
1130
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001131static int ath10k_pci_diag_write32(struct ath10k *ar, u32 address, u32 value)
1132{
1133 __le32 val = __cpu_to_le32(value);
1134
1135 return ath10k_pci_diag_write_mem(ar, address, &val, sizeof(val));
1136}
1137
Kalle Valo5e3dd152013-06-12 20:52:10 +03001138/* Called by lower (CE) layer when a send to Target completes. */
Rajkumar Manoharan0e5b2952015-10-12 18:27:01 +05301139static void ath10k_pci_htc_tx_cb(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001140{
1141 struct ath10k *ar = ce_state->ar;
Michal Kazior1cb86d42014-11-27 11:09:38 +01001142 struct sk_buff_head list;
1143 struct sk_buff *skb;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001144
Michal Kazior1cb86d42014-11-27 11:09:38 +01001145 __skb_queue_head_init(&list);
Rajkumar Manoharan765952e2015-10-23 18:01:05 +05301146 while (ath10k_ce_completed_send_next(ce_state, (void **)&skb) == 0) {
Michal Kaziora16942e2014-02-27 18:50:04 +02001147 /* no need to call tx completion for NULL pointers */
Michal Kazior1cb86d42014-11-27 11:09:38 +01001148 if (skb == NULL)
Michal Kazior726346f2014-02-27 18:50:04 +02001149 continue;
1150
Michal Kazior1cb86d42014-11-27 11:09:38 +01001151 __skb_queue_tail(&list, skb);
Michal Kazior5440ce22013-09-03 15:09:58 +02001152 }
Michal Kazior1cb86d42014-11-27 11:09:38 +01001153
1154 while ((skb = __skb_dequeue(&list)))
Rajkumar Manoharan0e5b2952015-10-12 18:27:01 +05301155 ath10k_htc_tx_completion_handler(ar, skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001156}
1157
Rajkumar Manoharana70587b2015-10-12 18:27:04 +05301158static void ath10k_pci_process_rx_cb(struct ath10k_ce_pipe *ce_state,
1159 void (*callback)(struct ath10k *ar,
1160 struct sk_buff *skb))
Kalle Valo5e3dd152013-06-12 20:52:10 +03001161{
1162 struct ath10k *ar = ce_state->ar;
1163 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +02001164 struct ath10k_pci_pipe *pipe_info = &ar_pci->pipe_info[ce_state->id];
Kalle Valo5e3dd152013-06-12 20:52:10 +03001165 struct sk_buff *skb;
Michal Kazior1cb86d42014-11-27 11:09:38 +01001166 struct sk_buff_head list;
Michal Kazior5440ce22013-09-03 15:09:58 +02001167 void *transfer_context;
1168 u32 ce_data;
Michal Kazior2f5280d2014-02-27 18:50:05 +02001169 unsigned int nbytes, max_nbytes;
Michal Kazior5440ce22013-09-03 15:09:58 +02001170 unsigned int transfer_id;
1171 unsigned int flags;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001172
Michal Kazior1cb86d42014-11-27 11:09:38 +01001173 __skb_queue_head_init(&list);
Michal Kazior5440ce22013-09-03 15:09:58 +02001174 while (ath10k_ce_completed_recv_next(ce_state, &transfer_context,
1175 &ce_data, &nbytes, &transfer_id,
1176 &flags) == 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001177 skb = transfer_context;
Michal Kazior2f5280d2014-02-27 18:50:05 +02001178 max_nbytes = skb->len + skb_tailroom(skb);
Michal Kazior8582bf32015-01-24 12:14:47 +02001179 dma_unmap_single(ar->dev, ATH10K_SKB_RXCB(skb)->paddr,
Michal Kazior2f5280d2014-02-27 18:50:05 +02001180 max_nbytes, DMA_FROM_DEVICE);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001181
Michal Kazior2f5280d2014-02-27 18:50:05 +02001182 if (unlikely(max_nbytes < nbytes)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001183 ath10k_warn(ar, "rxed more than expected (nbytes %d, max %d)",
Michal Kazior2f5280d2014-02-27 18:50:05 +02001184 nbytes, max_nbytes);
1185 dev_kfree_skb_any(skb);
1186 continue;
1187 }
1188
1189 skb_put(skb, nbytes);
Michal Kazior1cb86d42014-11-27 11:09:38 +01001190 __skb_queue_tail(&list, skb);
1191 }
Michal Kaziora360e542014-09-23 10:22:54 +02001192
Michal Kazior1cb86d42014-11-27 11:09:38 +01001193 while ((skb = __skb_dequeue(&list))) {
Michal Kaziora360e542014-09-23 10:22:54 +02001194 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci rx ce pipe %d len %d\n",
1195 ce_state->id, skb->len);
1196 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci rx: ",
1197 skb->data, skb->len);
1198
Rajkumar Manoharana70587b2015-10-12 18:27:04 +05301199 callback(ar, skb);
Michal Kazior2f5280d2014-02-27 18:50:05 +02001200 }
Michal Kaziorc29a3802014-07-21 21:03:10 +03001201
Michal Kazior728f95e2014-08-22 14:33:14 +02001202 ath10k_pci_rx_post_pipe(pipe_info);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001203}
1204
Rajkumar Manoharana70587b2015-10-12 18:27:04 +05301205/* Called by lower (CE) layer when data is received from the Target. */
1206static void ath10k_pci_htc_rx_cb(struct ath10k_ce_pipe *ce_state)
1207{
1208 ath10k_pci_process_rx_cb(ce_state, ath10k_htc_rx_completion_handler);
1209}
1210
1211/* Called by lower (CE) layer when a send to HTT Target completes. */
1212static void ath10k_pci_htt_tx_cb(struct ath10k_ce_pipe *ce_state)
1213{
1214 struct ath10k *ar = ce_state->ar;
1215 struct sk_buff *skb;
Rajkumar Manoharana70587b2015-10-12 18:27:04 +05301216
Rajkumar Manoharan765952e2015-10-23 18:01:05 +05301217 while (ath10k_ce_completed_send_next(ce_state, (void **)&skb) == 0) {
Rajkumar Manoharana70587b2015-10-12 18:27:04 +05301218 /* no need to call tx completion for NULL pointers */
1219 if (!skb)
1220 continue;
1221
1222 dma_unmap_single(ar->dev, ATH10K_SKB_CB(skb)->paddr,
1223 skb->len, DMA_TO_DEVICE);
1224 ath10k_htt_hif_tx_complete(ar, skb);
1225 }
1226}
1227
1228static void ath10k_pci_htt_rx_deliver(struct ath10k *ar, struct sk_buff *skb)
1229{
1230 skb_pull(skb, sizeof(struct ath10k_htc_hdr));
1231 ath10k_htt_t2h_msg_handler(ar, skb);
1232}
1233
1234/* Called by lower (CE) layer when HTT data is received from the Target. */
1235static void ath10k_pci_htt_rx_cb(struct ath10k_ce_pipe *ce_state)
1236{
1237 /* CE4 polling needs to be done whenever CE pipe which transports
1238 * HTT Rx (target->host) is processed.
1239 */
1240 ath10k_ce_per_engine_service(ce_state->ar, 4);
1241
1242 ath10k_pci_process_rx_cb(ce_state, ath10k_pci_htt_rx_deliver);
1243}
1244
Michal Kazior726346f2014-02-27 18:50:04 +02001245static int ath10k_pci_hif_tx_sg(struct ath10k *ar, u8 pipe_id,
1246 struct ath10k_hif_sg_item *items, int n_items)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001247{
Kalle Valo5e3dd152013-06-12 20:52:10 +03001248 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior726346f2014-02-27 18:50:04 +02001249 struct ath10k_pci_pipe *pci_pipe = &ar_pci->pipe_info[pipe_id];
1250 struct ath10k_ce_pipe *ce_pipe = pci_pipe->ce_hdl;
1251 struct ath10k_ce_ring *src_ring = ce_pipe->src_ring;
Michal Kazior7147a132014-05-26 12:02:58 +02001252 unsigned int nentries_mask;
1253 unsigned int sw_index;
1254 unsigned int write_index;
Michal Kazior08b8aa02014-05-26 12:02:59 +02001255 int err, i = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001256
Michal Kazior726346f2014-02-27 18:50:04 +02001257 spin_lock_bh(&ar_pci->ce_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001258
Michal Kazior7147a132014-05-26 12:02:58 +02001259 nentries_mask = src_ring->nentries_mask;
1260 sw_index = src_ring->sw_index;
1261 write_index = src_ring->write_index;
1262
Michal Kazior726346f2014-02-27 18:50:04 +02001263 if (unlikely(CE_RING_DELTA(nentries_mask,
1264 write_index, sw_index - 1) < n_items)) {
1265 err = -ENOBUFS;
Michal Kazior08b8aa02014-05-26 12:02:59 +02001266 goto err;
Michal Kazior726346f2014-02-27 18:50:04 +02001267 }
1268
1269 for (i = 0; i < n_items - 1; i++) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001270 ath10k_dbg(ar, ATH10K_DBG_PCI,
Michal Kazior726346f2014-02-27 18:50:04 +02001271 "pci tx item %d paddr 0x%08x len %d n_items %d\n",
1272 i, items[i].paddr, items[i].len, n_items);
Michal Kazior7aa7a722014-08-25 12:09:38 +02001273 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci tx data: ",
Michal Kazior726346f2014-02-27 18:50:04 +02001274 items[i].vaddr, items[i].len);
1275
1276 err = ath10k_ce_send_nolock(ce_pipe,
1277 items[i].transfer_context,
1278 items[i].paddr,
1279 items[i].len,
1280 items[i].transfer_id,
1281 CE_SEND_FLAG_GATHER);
1282 if (err)
Michal Kazior08b8aa02014-05-26 12:02:59 +02001283 goto err;
Michal Kazior726346f2014-02-27 18:50:04 +02001284 }
1285
1286 /* `i` is equal to `n_items -1` after for() */
Kalle Valo5e3dd152013-06-12 20:52:10 +03001287
Michal Kazior7aa7a722014-08-25 12:09:38 +02001288 ath10k_dbg(ar, ATH10K_DBG_PCI,
Michal Kazior726346f2014-02-27 18:50:04 +02001289 "pci tx item %d paddr 0x%08x len %d n_items %d\n",
1290 i, items[i].paddr, items[i].len, n_items);
Michal Kazior7aa7a722014-08-25 12:09:38 +02001291 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci tx data: ",
Michal Kazior726346f2014-02-27 18:50:04 +02001292 items[i].vaddr, items[i].len);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001293
Michal Kazior726346f2014-02-27 18:50:04 +02001294 err = ath10k_ce_send_nolock(ce_pipe,
1295 items[i].transfer_context,
1296 items[i].paddr,
1297 items[i].len,
1298 items[i].transfer_id,
1299 0);
1300 if (err)
Michal Kazior08b8aa02014-05-26 12:02:59 +02001301 goto err;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001302
Michal Kazior08b8aa02014-05-26 12:02:59 +02001303 spin_unlock_bh(&ar_pci->ce_lock);
1304 return 0;
1305
1306err:
1307 for (; i > 0; i--)
1308 __ath10k_ce_send_revert(ce_pipe);
1309
Michal Kazior726346f2014-02-27 18:50:04 +02001310 spin_unlock_bh(&ar_pci->ce_lock);
1311 return err;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001312}
1313
Kalle Valoeef25402014-09-24 14:16:52 +03001314static int ath10k_pci_hif_diag_read(struct ath10k *ar, u32 address, void *buf,
1315 size_t buf_len)
1316{
1317 return ath10k_pci_diag_read_mem(ar, address, buf, buf_len);
1318}
1319
Kalle Valo5e3dd152013-06-12 20:52:10 +03001320static u16 ath10k_pci_hif_get_free_queue_number(struct ath10k *ar, u8 pipe)
1321{
1322 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo50f87a62014-03-28 09:32:52 +02001323
Michal Kazior7aa7a722014-08-25 12:09:38 +02001324 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif get free queue number\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001325
Michal Kazior3efcb3b2013-10-02 11:03:41 +02001326 return ath10k_ce_num_free_src_entries(ar_pci->pipe_info[pipe].ce_hdl);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001327}
1328
Ben Greear384914b2014-08-25 08:37:32 +03001329static void ath10k_pci_dump_registers(struct ath10k *ar,
1330 struct ath10k_fw_crash_data *crash_data)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001331{
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001332 __le32 reg_dump_values[REG_DUMP_COUNT_QCA988X] = {};
1333 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001334
Ben Greear384914b2014-08-25 08:37:32 +03001335 lockdep_assert_held(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001336
Kalle Valo3d29a3e2014-08-25 08:37:26 +03001337 ret = ath10k_pci_diag_read_hi(ar, &reg_dump_values[0],
1338 hi_failure_state,
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001339 REG_DUMP_COUNT_QCA988X * sizeof(__le32));
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001340 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001341 ath10k_err(ar, "failed to read firmware dump area: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001342 return;
1343 }
1344
1345 BUILD_BUG_ON(REG_DUMP_COUNT_QCA988X % 4);
1346
Michal Kazior7aa7a722014-08-25 12:09:38 +02001347 ath10k_err(ar, "firmware register dump:\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001348 for (i = 0; i < REG_DUMP_COUNT_QCA988X; i += 4)
Michal Kazior7aa7a722014-08-25 12:09:38 +02001349 ath10k_err(ar, "[%02d]: 0x%08X 0x%08X 0x%08X 0x%08X\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001350 i,
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001351 __le32_to_cpu(reg_dump_values[i]),
1352 __le32_to_cpu(reg_dump_values[i + 1]),
1353 __le32_to_cpu(reg_dump_values[i + 2]),
1354 __le32_to_cpu(reg_dump_values[i + 3]));
Michal Kazioraffd3212013-07-16 09:54:35 +02001355
Michal Kazior1bbb1192014-08-25 12:13:14 +02001356 if (!crash_data)
1357 return;
1358
Ben Greear384914b2014-08-25 08:37:32 +03001359 for (i = 0; i < REG_DUMP_COUNT_QCA988X; i++)
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001360 crash_data->registers[i] = reg_dump_values[i];
Ben Greear384914b2014-08-25 08:37:32 +03001361}
1362
Kalle Valo0e9848c2014-08-25 08:37:37 +03001363static void ath10k_pci_fw_crashed_dump(struct ath10k *ar)
Ben Greear384914b2014-08-25 08:37:32 +03001364{
1365 struct ath10k_fw_crash_data *crash_data;
1366 char uuid[50];
1367
1368 spin_lock_bh(&ar->data_lock);
1369
Ben Greearf51dbe72014-09-29 14:41:46 +03001370 ar->stats.fw_crash_counter++;
1371
Ben Greear384914b2014-08-25 08:37:32 +03001372 crash_data = ath10k_debug_get_new_fw_crash_data(ar);
1373
1374 if (crash_data)
1375 scnprintf(uuid, sizeof(uuid), "%pUl", &crash_data->uuid);
1376 else
1377 scnprintf(uuid, sizeof(uuid), "n/a");
1378
Michal Kazior7aa7a722014-08-25 12:09:38 +02001379 ath10k_err(ar, "firmware crashed! (uuid %s)\n", uuid);
Kalle Valo8a0c7972014-08-25 08:37:45 +03001380 ath10k_print_driver_info(ar);
Ben Greear384914b2014-08-25 08:37:32 +03001381 ath10k_pci_dump_registers(ar, crash_data);
1382
Ben Greear384914b2014-08-25 08:37:32 +03001383 spin_unlock_bh(&ar->data_lock);
Michal Kazioraffd3212013-07-16 09:54:35 +02001384
Michal Kazior5e90de82013-10-16 16:46:05 +03001385 queue_work(ar->workqueue, &ar->restart_work);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001386}
1387
1388static void ath10k_pci_hif_send_complete_check(struct ath10k *ar, u8 pipe,
1389 int force)
1390{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001391 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif send complete check\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001392
Kalle Valo5e3dd152013-06-12 20:52:10 +03001393 if (!force) {
1394 int resources;
1395 /*
1396 * Decide whether to actually poll for completions, or just
1397 * wait for a later chance.
1398 * If there seem to be plenty of resources left, then just wait
1399 * since checking involves reading a CE register, which is a
1400 * relatively expensive operation.
1401 */
1402 resources = ath10k_pci_hif_get_free_queue_number(ar, pipe);
1403
1404 /*
1405 * If at least 50% of the total resources are still available,
1406 * don't bother checking again yet.
1407 */
1408 if (resources > (host_ce_config_wlan[pipe].src_nentries >> 1))
1409 return;
1410 }
1411 ath10k_ce_per_engine_service(ar, pipe);
1412}
1413
Michal Kazior96a9d0d2013-11-08 08:01:25 +01001414static void ath10k_pci_kill_tasklet(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001415{
1416 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001417 int i;
1418
Kalle Valo5e3dd152013-06-12 20:52:10 +03001419 tasklet_kill(&ar_pci->intr_tq);
Michal Kazior103d4f52013-11-08 08:01:24 +01001420 tasklet_kill(&ar_pci->msi_fw_err);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001421
1422 for (i = 0; i < CE_COUNT; i++)
1423 tasklet_kill(&ar_pci->pipe_info[i].intr);
Michal Kazior728f95e2014-08-22 14:33:14 +02001424
1425 del_timer_sync(&ar_pci->rx_post_retry);
Michal Kazior96a9d0d2013-11-08 08:01:25 +01001426}
1427
Rajkumar Manoharan400143e2015-10-12 18:27:06 +05301428static int ath10k_pci_hif_map_service_to_pipe(struct ath10k *ar, u16 service_id,
1429 u8 *ul_pipe, u8 *dl_pipe)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001430{
Michal Kazior7c6aa252014-08-26 19:14:03 +03001431 const struct service_to_pipe *entry;
1432 bool ul_set = false, dl_set = false;
1433 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001434
Michal Kazior7aa7a722014-08-25 12:09:38 +02001435 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif map service\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001436
Michal Kazior7c6aa252014-08-26 19:14:03 +03001437 for (i = 0; i < ARRAY_SIZE(target_service_to_ce_map_wlan); i++) {
1438 entry = &target_service_to_ce_map_wlan[i];
Kalle Valo5e3dd152013-06-12 20:52:10 +03001439
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001440 if (__le32_to_cpu(entry->service_id) != service_id)
Michal Kazior7c6aa252014-08-26 19:14:03 +03001441 continue;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001442
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001443 switch (__le32_to_cpu(entry->pipedir)) {
Michal Kazior7c6aa252014-08-26 19:14:03 +03001444 case PIPEDIR_NONE:
1445 break;
1446 case PIPEDIR_IN:
1447 WARN_ON(dl_set);
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001448 *dl_pipe = __le32_to_cpu(entry->pipenum);
Michal Kazior7c6aa252014-08-26 19:14:03 +03001449 dl_set = true;
1450 break;
1451 case PIPEDIR_OUT:
1452 WARN_ON(ul_set);
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001453 *ul_pipe = __le32_to_cpu(entry->pipenum);
Michal Kazior7c6aa252014-08-26 19:14:03 +03001454 ul_set = true;
1455 break;
1456 case PIPEDIR_INOUT:
1457 WARN_ON(dl_set);
1458 WARN_ON(ul_set);
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001459 *dl_pipe = __le32_to_cpu(entry->pipenum);
1460 *ul_pipe = __le32_to_cpu(entry->pipenum);
Michal Kazior7c6aa252014-08-26 19:14:03 +03001461 dl_set = true;
1462 ul_set = true;
1463 break;
1464 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001465 }
Michal Kazior7c6aa252014-08-26 19:14:03 +03001466
1467 if (WARN_ON(!ul_set || !dl_set))
1468 return -ENOENT;
1469
Michal Kazior7c6aa252014-08-26 19:14:03 +03001470 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001471}
1472
1473static void ath10k_pci_hif_get_default_pipe(struct ath10k *ar,
Kalle Valo5b07e072014-09-14 12:50:06 +03001474 u8 *ul_pipe, u8 *dl_pipe)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001475{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001476 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif get default pipe\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001477
Kalle Valo5e3dd152013-06-12 20:52:10 +03001478 (void)ath10k_pci_hif_map_service_to_pipe(ar,
1479 ATH10K_HTC_SVC_ID_RSVD_CTRL,
Rajkumar Manoharan400143e2015-10-12 18:27:06 +05301480 ul_pipe, dl_pipe);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001481}
1482
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001483static void ath10k_pci_irq_msi_fw_mask(struct ath10k *ar)
1484{
1485 u32 val;
1486
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +05301487 switch (ar->hw_rev) {
1488 case ATH10K_HW_QCA988X:
1489 case ATH10K_HW_QCA6174:
Bartosz Markowskia226b512015-10-28 15:09:53 +01001490 case ATH10K_HW_QCA9377:
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +05301491 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1492 CORE_CTRL_ADDRESS);
1493 val &= ~CORE_CTRL_PCIE_REG_31_MASK;
1494 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
1495 CORE_CTRL_ADDRESS, val);
1496 break;
1497 case ATH10K_HW_QCA99X0:
1498 /* TODO: Find appropriate register configuration for QCA99X0
1499 * to mask irq/MSI.
1500 */
1501 break;
1502 }
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001503}
1504
1505static void ath10k_pci_irq_msi_fw_unmask(struct ath10k *ar)
1506{
1507 u32 val;
1508
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +05301509 switch (ar->hw_rev) {
1510 case ATH10K_HW_QCA988X:
1511 case ATH10K_HW_QCA6174:
Bartosz Markowskia226b512015-10-28 15:09:53 +01001512 case ATH10K_HW_QCA9377:
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +05301513 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1514 CORE_CTRL_ADDRESS);
1515 val |= CORE_CTRL_PCIE_REG_31_MASK;
1516 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
1517 CORE_CTRL_ADDRESS, val);
1518 break;
1519 case ATH10K_HW_QCA99X0:
1520 /* TODO: Find appropriate register configuration for QCA99X0
1521 * to unmask irq/MSI.
1522 */
1523 break;
1524 }
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001525}
1526
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001527static void ath10k_pci_irq_disable(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001528{
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001529 ath10k_ce_disable_interrupts(ar);
1530 ath10k_pci_disable_and_clear_legacy_irq(ar);
1531 ath10k_pci_irq_msi_fw_mask(ar);
1532}
1533
1534static void ath10k_pci_irq_sync(struct ath10k *ar)
1535{
Kalle Valo5e3dd152013-06-12 20:52:10 +03001536 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001537 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001538
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001539 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
1540 synchronize_irq(ar_pci->pdev->irq + i);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001541}
1542
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001543static void ath10k_pci_irq_enable(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001544{
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001545 ath10k_ce_enable_interrupts(ar);
Michal Kaziore75db4e2014-08-28 22:14:16 +03001546 ath10k_pci_enable_legacy_irq(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001547 ath10k_pci_irq_msi_fw_unmask(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001548}
1549
1550static int ath10k_pci_hif_start(struct ath10k *ar)
1551{
Janusz Dziedzic76d870e2015-05-18 09:38:16 +00001552 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo9a149692015-10-05 17:56:36 +03001553
Michal Kazior7aa7a722014-08-25 12:09:38 +02001554 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif start\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001555
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001556 ath10k_pci_irq_enable(ar);
Michal Kazior728f95e2014-08-22 14:33:14 +02001557 ath10k_pci_rx_post(ar);
Kalle Valo50f87a62014-03-28 09:32:52 +02001558
Janusz Dziedzic76d870e2015-05-18 09:38:16 +00001559 pcie_capability_write_word(ar_pci->pdev, PCI_EXP_LNKCTL,
1560 ar_pci->link_ctl);
1561
Kalle Valo5e3dd152013-06-12 20:52:10 +03001562 return 0;
1563}
1564
Michal Kazior099ac7c2014-10-28 10:32:05 +01001565static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe *pci_pipe)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001566{
1567 struct ath10k *ar;
Michal Kazior099ac7c2014-10-28 10:32:05 +01001568 struct ath10k_ce_pipe *ce_pipe;
1569 struct ath10k_ce_ring *ce_ring;
1570 struct sk_buff *skb;
1571 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001572
Michal Kazior099ac7c2014-10-28 10:32:05 +01001573 ar = pci_pipe->hif_ce_state;
1574 ce_pipe = pci_pipe->ce_hdl;
1575 ce_ring = ce_pipe->dest_ring;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001576
Michal Kazior099ac7c2014-10-28 10:32:05 +01001577 if (!ce_ring)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001578 return;
1579
Michal Kazior099ac7c2014-10-28 10:32:05 +01001580 if (!pci_pipe->buf_sz)
1581 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001582
Michal Kazior099ac7c2014-10-28 10:32:05 +01001583 for (i = 0; i < ce_ring->nentries; i++) {
1584 skb = ce_ring->per_transfer_context[i];
1585 if (!skb)
1586 continue;
1587
1588 ce_ring->per_transfer_context[i] = NULL;
1589
Michal Kazior8582bf32015-01-24 12:14:47 +02001590 dma_unmap_single(ar->dev, ATH10K_SKB_RXCB(skb)->paddr,
Michal Kazior099ac7c2014-10-28 10:32:05 +01001591 skb->len + skb_tailroom(skb),
Kalle Valo5e3dd152013-06-12 20:52:10 +03001592 DMA_FROM_DEVICE);
Michal Kazior099ac7c2014-10-28 10:32:05 +01001593 dev_kfree_skb_any(skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001594 }
1595}
1596
Michal Kazior099ac7c2014-10-28 10:32:05 +01001597static void ath10k_pci_tx_pipe_cleanup(struct ath10k_pci_pipe *pci_pipe)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001598{
1599 struct ath10k *ar;
1600 struct ath10k_pci *ar_pci;
Michal Kazior099ac7c2014-10-28 10:32:05 +01001601 struct ath10k_ce_pipe *ce_pipe;
1602 struct ath10k_ce_ring *ce_ring;
Michal Kazior099ac7c2014-10-28 10:32:05 +01001603 struct sk_buff *skb;
Michal Kazior099ac7c2014-10-28 10:32:05 +01001604 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001605
Michal Kazior099ac7c2014-10-28 10:32:05 +01001606 ar = pci_pipe->hif_ce_state;
1607 ar_pci = ath10k_pci_priv(ar);
1608 ce_pipe = pci_pipe->ce_hdl;
1609 ce_ring = ce_pipe->src_ring;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001610
Michal Kazior099ac7c2014-10-28 10:32:05 +01001611 if (!ce_ring)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001612 return;
1613
Michal Kazior099ac7c2014-10-28 10:32:05 +01001614 if (!pci_pipe->buf_sz)
1615 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001616
Michal Kazior099ac7c2014-10-28 10:32:05 +01001617 for (i = 0; i < ce_ring->nentries; i++) {
1618 skb = ce_ring->per_transfer_context[i];
1619 if (!skb)
Michal Kazior2415fc12013-11-08 08:01:32 +01001620 continue;
Michal Kazior2415fc12013-11-08 08:01:32 +01001621
Michal Kazior099ac7c2014-10-28 10:32:05 +01001622 ce_ring->per_transfer_context[i] = NULL;
Michal Kazior099ac7c2014-10-28 10:32:05 +01001623
Rajkumar Manoharan0e5b2952015-10-12 18:27:01 +05301624 ath10k_htc_tx_completion_handler(ar, skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001625 }
1626}
1627
1628/*
1629 * Cleanup residual buffers for device shutdown:
1630 * buffers that were enqueued for receive
1631 * buffers that were to be sent
1632 * Note: Buffers that had completed but which were
1633 * not yet processed are on a completion queue. They
1634 * are handled when the completion thread shuts down.
1635 */
1636static void ath10k_pci_buffer_cleanup(struct ath10k *ar)
1637{
1638 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1639 int pipe_num;
1640
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001641 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Michal Kazior87263e52013-08-27 13:08:01 +02001642 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001643
1644 pipe_info = &ar_pci->pipe_info[pipe_num];
1645 ath10k_pci_rx_pipe_cleanup(pipe_info);
1646 ath10k_pci_tx_pipe_cleanup(pipe_info);
1647 }
1648}
1649
1650static void ath10k_pci_ce_deinit(struct ath10k *ar)
1651{
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001652 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001653
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001654 for (i = 0; i < CE_COUNT; i++)
1655 ath10k_ce_deinit_pipe(ar, i);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001656}
1657
Michal Kazior728f95e2014-08-22 14:33:14 +02001658static void ath10k_pci_flush(struct ath10k *ar)
1659{
1660 ath10k_pci_kill_tasklet(ar);
1661 ath10k_pci_buffer_cleanup(ar);
1662}
1663
Kalle Valo5e3dd152013-06-12 20:52:10 +03001664static void ath10k_pci_hif_stop(struct ath10k *ar)
1665{
Michal Kazior77258d42015-05-18 09:38:18 +00001666 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1667 unsigned long flags;
1668
Michal Kazior7aa7a722014-08-25 12:09:38 +02001669 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif stop\n");
Michal Kazior32270b62013-08-02 09:15:47 +02001670
Michal Kazior10d23db2014-08-22 14:33:15 +02001671 /* Most likely the device has HTT Rx ring configured. The only way to
1672 * prevent the device from accessing (and possible corrupting) host
1673 * memory is to reset the chip now.
Michal Kaziore75db4e2014-08-28 22:14:16 +03001674 *
1675 * There's also no known way of masking MSI interrupts on the device.
1676 * For ranged MSI the CE-related interrupts can be masked. However
1677 * regardless how many MSI interrupts are assigned the first one
1678 * is always used for firmware indications (crashes) and cannot be
1679 * masked. To prevent the device from asserting the interrupt reset it
1680 * before proceeding with cleanup.
Michal Kazior10d23db2014-08-22 14:33:15 +02001681 */
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +05301682 ath10k_pci_safe_chip_reset(ar);
Michal Kaziore75db4e2014-08-28 22:14:16 +03001683
1684 ath10k_pci_irq_disable(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001685 ath10k_pci_irq_sync(ar);
Michal Kaziore75db4e2014-08-28 22:14:16 +03001686 ath10k_pci_flush(ar);
Michal Kazior77258d42015-05-18 09:38:18 +00001687
1688 spin_lock_irqsave(&ar_pci->ps_lock, flags);
1689 WARN_ON(ar_pci->ps_wake_refcount > 0);
1690 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001691}
1692
1693static int ath10k_pci_hif_exchange_bmi_msg(struct ath10k *ar,
1694 void *req, u32 req_len,
1695 void *resp, u32 *resp_len)
1696{
1697 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2aa39112013-08-27 13:08:02 +02001698 struct ath10k_pci_pipe *pci_tx = &ar_pci->pipe_info[BMI_CE_NUM_TO_TARG];
1699 struct ath10k_pci_pipe *pci_rx = &ar_pci->pipe_info[BMI_CE_NUM_TO_HOST];
1700 struct ath10k_ce_pipe *ce_tx = pci_tx->ce_hdl;
1701 struct ath10k_ce_pipe *ce_rx = pci_rx->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001702 dma_addr_t req_paddr = 0;
1703 dma_addr_t resp_paddr = 0;
1704 struct bmi_xfer xfer = {};
1705 void *treq, *tresp = NULL;
1706 int ret = 0;
1707
Michal Kazior85622cd2013-11-25 14:06:22 +01001708 might_sleep();
1709
Kalle Valo5e3dd152013-06-12 20:52:10 +03001710 if (resp && !resp_len)
1711 return -EINVAL;
1712
1713 if (resp && resp_len && *resp_len == 0)
1714 return -EINVAL;
1715
1716 treq = kmemdup(req, req_len, GFP_KERNEL);
1717 if (!treq)
1718 return -ENOMEM;
1719
1720 req_paddr = dma_map_single(ar->dev, treq, req_len, DMA_TO_DEVICE);
1721 ret = dma_mapping_error(ar->dev, req_paddr);
Michal Kazior5e55e3c2015-08-19 13:10:43 +02001722 if (ret) {
1723 ret = -EIO;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001724 goto err_dma;
Michal Kazior5e55e3c2015-08-19 13:10:43 +02001725 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001726
1727 if (resp && resp_len) {
1728 tresp = kzalloc(*resp_len, GFP_KERNEL);
1729 if (!tresp) {
1730 ret = -ENOMEM;
1731 goto err_req;
1732 }
1733
1734 resp_paddr = dma_map_single(ar->dev, tresp, *resp_len,
1735 DMA_FROM_DEVICE);
1736 ret = dma_mapping_error(ar->dev, resp_paddr);
Michal Kazior5e55e3c2015-08-19 13:10:43 +02001737 if (ret) {
1738 ret = EIO;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001739 goto err_req;
Michal Kazior5e55e3c2015-08-19 13:10:43 +02001740 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001741
1742 xfer.wait_for_resp = true;
1743 xfer.resp_len = 0;
1744
Michal Kazior728f95e2014-08-22 14:33:14 +02001745 ath10k_ce_rx_post_buf(ce_rx, &xfer, resp_paddr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001746 }
1747
Kalle Valo5e3dd152013-06-12 20:52:10 +03001748 ret = ath10k_ce_send(ce_tx, &xfer, req_paddr, req_len, -1, 0);
1749 if (ret)
1750 goto err_resp;
1751
Michal Kazior85622cd2013-11-25 14:06:22 +01001752 ret = ath10k_pci_bmi_wait(ce_tx, ce_rx, &xfer);
1753 if (ret) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001754 u32 unused_buffer;
1755 unsigned int unused_nbytes;
1756 unsigned int unused_id;
1757
Kalle Valo5e3dd152013-06-12 20:52:10 +03001758 ath10k_ce_cancel_send_next(ce_tx, NULL, &unused_buffer,
1759 &unused_nbytes, &unused_id);
1760 } else {
1761 /* non-zero means we did not time out */
1762 ret = 0;
1763 }
1764
1765err_resp:
1766 if (resp) {
1767 u32 unused_buffer;
1768
1769 ath10k_ce_revoke_recv_next(ce_rx, NULL, &unused_buffer);
1770 dma_unmap_single(ar->dev, resp_paddr,
1771 *resp_len, DMA_FROM_DEVICE);
1772 }
1773err_req:
1774 dma_unmap_single(ar->dev, req_paddr, req_len, DMA_TO_DEVICE);
1775
1776 if (ret == 0 && resp_len) {
1777 *resp_len = min(*resp_len, xfer.resp_len);
1778 memcpy(resp, tresp, xfer.resp_len);
1779 }
1780err_dma:
1781 kfree(treq);
1782 kfree(tresp);
1783
1784 return ret;
1785}
1786
Michal Kazior5440ce22013-09-03 15:09:58 +02001787static void ath10k_pci_bmi_send_done(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001788{
Michal Kazior5440ce22013-09-03 15:09:58 +02001789 struct bmi_xfer *xfer;
Michal Kazior5440ce22013-09-03 15:09:58 +02001790
Rajkumar Manoharan765952e2015-10-23 18:01:05 +05301791 if (ath10k_ce_completed_send_next(ce_state, (void **)&xfer))
Michal Kazior5440ce22013-09-03 15:09:58 +02001792 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001793
Michal Kazior2374b182014-07-14 16:25:25 +03001794 xfer->tx_done = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001795}
1796
Michal Kazior5440ce22013-09-03 15:09:58 +02001797static void ath10k_pci_bmi_recv_data(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001798{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001799 struct ath10k *ar = ce_state->ar;
Michal Kazior5440ce22013-09-03 15:09:58 +02001800 struct bmi_xfer *xfer;
1801 u32 ce_data;
1802 unsigned int nbytes;
1803 unsigned int transfer_id;
1804 unsigned int flags;
1805
1806 if (ath10k_ce_completed_recv_next(ce_state, (void **)&xfer, &ce_data,
1807 &nbytes, &transfer_id, &flags))
1808 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001809
Michal Kazior04ed9df2014-10-28 10:34:36 +01001810 if (WARN_ON_ONCE(!xfer))
1811 return;
1812
Kalle Valo5e3dd152013-06-12 20:52:10 +03001813 if (!xfer->wait_for_resp) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001814 ath10k_warn(ar, "unexpected: BMI data received; ignoring\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001815 return;
1816 }
1817
1818 xfer->resp_len = nbytes;
Michal Kazior2374b182014-07-14 16:25:25 +03001819 xfer->rx_done = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001820}
1821
Michal Kazior85622cd2013-11-25 14:06:22 +01001822static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
1823 struct ath10k_ce_pipe *rx_pipe,
1824 struct bmi_xfer *xfer)
1825{
1826 unsigned long timeout = jiffies + BMI_COMMUNICATION_TIMEOUT_HZ;
1827
1828 while (time_before_eq(jiffies, timeout)) {
1829 ath10k_pci_bmi_send_done(tx_pipe);
1830 ath10k_pci_bmi_recv_data(rx_pipe);
1831
Michal Kazior2374b182014-07-14 16:25:25 +03001832 if (xfer->tx_done && (xfer->rx_done == xfer->wait_for_resp))
Michal Kazior85622cd2013-11-25 14:06:22 +01001833 return 0;
1834
1835 schedule();
1836 }
1837
1838 return -ETIMEDOUT;
1839}
1840
Kalle Valo5e3dd152013-06-12 20:52:10 +03001841/*
Kalle Valo5e3dd152013-06-12 20:52:10 +03001842 * Send an interrupt to the device to wake up the Target CPU
1843 * so it has an opportunity to notice any changed state.
1844 */
1845static int ath10k_pci_wake_target_cpu(struct ath10k *ar)
1846{
Michal Kazior9e264942014-09-02 11:00:21 +03001847 u32 addr, val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001848
Michal Kazior9e264942014-09-02 11:00:21 +03001849 addr = SOC_CORE_BASE_ADDRESS | CORE_CTRL_ADDRESS;
1850 val = ath10k_pci_read32(ar, addr);
1851 val |= CORE_CTRL_CPU_INTR_MASK;
1852 ath10k_pci_write32(ar, addr, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001853
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001854 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001855}
1856
Michal Kaziord63955b2015-01-24 12:14:49 +02001857static int ath10k_pci_get_num_banks(struct ath10k *ar)
1858{
1859 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1860
1861 switch (ar_pci->pdev->device) {
1862 case QCA988X_2_0_DEVICE_ID:
Vasanthakumar Thiagarajan8bd47022015-06-18 12:31:03 +05301863 case QCA99X0_2_0_DEVICE_ID:
Michal Kaziord63955b2015-01-24 12:14:49 +02001864 return 1;
Michal Kazior36582e52015-08-13 14:32:26 +02001865 case QCA6164_2_1_DEVICE_ID:
Michal Kaziord63955b2015-01-24 12:14:49 +02001866 case QCA6174_2_1_DEVICE_ID:
1867 switch (MS(ar->chip_id, SOC_CHIP_ID_REV)) {
1868 case QCA6174_HW_1_0_CHIP_ID_REV:
1869 case QCA6174_HW_1_1_CHIP_ID_REV:
Michal Kazior11a002e2015-04-20 09:20:41 +00001870 case QCA6174_HW_2_1_CHIP_ID_REV:
1871 case QCA6174_HW_2_2_CHIP_ID_REV:
Michal Kaziord63955b2015-01-24 12:14:49 +02001872 return 3;
1873 case QCA6174_HW_1_3_CHIP_ID_REV:
1874 return 2;
Michal Kaziord63955b2015-01-24 12:14:49 +02001875 case QCA6174_HW_3_0_CHIP_ID_REV:
1876 case QCA6174_HW_3_1_CHIP_ID_REV:
1877 case QCA6174_HW_3_2_CHIP_ID_REV:
1878 return 9;
1879 }
1880 break;
Bartosz Markowskia226b512015-10-28 15:09:53 +01001881 case QCA9377_1_0_DEVICE_ID:
1882 return 2;
Michal Kaziord63955b2015-01-24 12:14:49 +02001883 }
1884
1885 ath10k_warn(ar, "unknown number of banks, assuming 1\n");
1886 return 1;
1887}
1888
Kalle Valo5e3dd152013-06-12 20:52:10 +03001889static int ath10k_pci_init_config(struct ath10k *ar)
1890{
1891 u32 interconnect_targ_addr;
1892 u32 pcie_state_targ_addr = 0;
1893 u32 pipe_cfg_targ_addr = 0;
1894 u32 svc_to_pipe_map = 0;
1895 u32 pcie_config_flags = 0;
1896 u32 ealloc_value;
1897 u32 ealloc_targ_addr;
1898 u32 flag2_value;
1899 u32 flag2_targ_addr;
1900 int ret = 0;
1901
1902 /* Download to Target the CE Config and the service-to-CE map */
1903 interconnect_targ_addr =
1904 host_interest_item_address(HI_ITEM(hi_interconnect_state));
1905
1906 /* Supply Target-side CE configuration */
Michal Kazior9e264942014-09-02 11:00:21 +03001907 ret = ath10k_pci_diag_read32(ar, interconnect_targ_addr,
1908 &pcie_state_targ_addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001909 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001910 ath10k_err(ar, "Failed to get pcie state addr: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001911 return ret;
1912 }
1913
1914 if (pcie_state_targ_addr == 0) {
1915 ret = -EIO;
Michal Kazior7aa7a722014-08-25 12:09:38 +02001916 ath10k_err(ar, "Invalid pcie state addr\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001917 return ret;
1918 }
1919
Michal Kazior9e264942014-09-02 11:00:21 +03001920 ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
Kalle Valo5e3dd152013-06-12 20:52:10 +03001921 offsetof(struct pcie_state,
Michal Kazior9e264942014-09-02 11:00:21 +03001922 pipe_cfg_addr)),
1923 &pipe_cfg_targ_addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001924 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001925 ath10k_err(ar, "Failed to get pipe cfg addr: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001926 return ret;
1927 }
1928
1929 if (pipe_cfg_targ_addr == 0) {
1930 ret = -EIO;
Michal Kazior7aa7a722014-08-25 12:09:38 +02001931 ath10k_err(ar, "Invalid pipe cfg addr\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001932 return ret;
1933 }
1934
1935 ret = ath10k_pci_diag_write_mem(ar, pipe_cfg_targ_addr,
Kalle Valo5b07e072014-09-14 12:50:06 +03001936 target_ce_config_wlan,
Vasanthakumar Thiagarajan050af062015-06-18 12:31:04 +05301937 sizeof(struct ce_pipe_config) *
1938 NUM_TARGET_CE_CONFIG_WLAN);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001939
1940 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001941 ath10k_err(ar, "Failed to write pipe cfg: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001942 return ret;
1943 }
1944
Michal Kazior9e264942014-09-02 11:00:21 +03001945 ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
Kalle Valo5e3dd152013-06-12 20:52:10 +03001946 offsetof(struct pcie_state,
Michal Kazior9e264942014-09-02 11:00:21 +03001947 svc_to_pipe_map)),
1948 &svc_to_pipe_map);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001949 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001950 ath10k_err(ar, "Failed to get svc/pipe map: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001951 return ret;
1952 }
1953
1954 if (svc_to_pipe_map == 0) {
1955 ret = -EIO;
Michal Kazior7aa7a722014-08-25 12:09:38 +02001956 ath10k_err(ar, "Invalid svc_to_pipe map\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001957 return ret;
1958 }
1959
1960 ret = ath10k_pci_diag_write_mem(ar, svc_to_pipe_map,
Kalle Valo5b07e072014-09-14 12:50:06 +03001961 target_service_to_ce_map_wlan,
1962 sizeof(target_service_to_ce_map_wlan));
Kalle Valo5e3dd152013-06-12 20:52:10 +03001963 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001964 ath10k_err(ar, "Failed to write svc/pipe map: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001965 return ret;
1966 }
1967
Michal Kazior9e264942014-09-02 11:00:21 +03001968 ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
Kalle Valo5e3dd152013-06-12 20:52:10 +03001969 offsetof(struct pcie_state,
Michal Kazior9e264942014-09-02 11:00:21 +03001970 config_flags)),
1971 &pcie_config_flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001972 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001973 ath10k_err(ar, "Failed to get pcie config_flags: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001974 return ret;
1975 }
1976
1977 pcie_config_flags &= ~PCIE_CONFIG_FLAG_ENABLE_L1;
1978
Michal Kazior9e264942014-09-02 11:00:21 +03001979 ret = ath10k_pci_diag_write32(ar, (pcie_state_targ_addr +
1980 offsetof(struct pcie_state,
1981 config_flags)),
1982 pcie_config_flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001983 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001984 ath10k_err(ar, "Failed to write pcie config_flags: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001985 return ret;
1986 }
1987
1988 /* configure early allocation */
1989 ealloc_targ_addr = host_interest_item_address(HI_ITEM(hi_early_alloc));
1990
Michal Kazior9e264942014-09-02 11:00:21 +03001991 ret = ath10k_pci_diag_read32(ar, ealloc_targ_addr, &ealloc_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001992 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001993 ath10k_err(ar, "Faile to get early alloc val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001994 return ret;
1995 }
1996
1997 /* first bank is switched to IRAM */
1998 ealloc_value |= ((HI_EARLY_ALLOC_MAGIC << HI_EARLY_ALLOC_MAGIC_SHIFT) &
1999 HI_EARLY_ALLOC_MAGIC_MASK);
Michal Kaziord63955b2015-01-24 12:14:49 +02002000 ealloc_value |= ((ath10k_pci_get_num_banks(ar) <<
2001 HI_EARLY_ALLOC_IRAM_BANKS_SHIFT) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03002002 HI_EARLY_ALLOC_IRAM_BANKS_MASK);
2003
Michal Kazior9e264942014-09-02 11:00:21 +03002004 ret = ath10k_pci_diag_write32(ar, ealloc_targ_addr, ealloc_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002005 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002006 ath10k_err(ar, "Failed to set early alloc val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002007 return ret;
2008 }
2009
2010 /* Tell Target to proceed with initialization */
2011 flag2_targ_addr = host_interest_item_address(HI_ITEM(hi_option_flag2));
2012
Michal Kazior9e264942014-09-02 11:00:21 +03002013 ret = ath10k_pci_diag_read32(ar, flag2_targ_addr, &flag2_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002014 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002015 ath10k_err(ar, "Failed to get option val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002016 return ret;
2017 }
2018
2019 flag2_value |= HI_OPTION_EARLY_CFG_DONE;
2020
Michal Kazior9e264942014-09-02 11:00:21 +03002021 ret = ath10k_pci_diag_write32(ar, flag2_targ_addr, flag2_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002022 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002023 ath10k_err(ar, "Failed to set option val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002024 return ret;
2025 }
2026
2027 return 0;
2028}
2029
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002030static int ath10k_pci_alloc_pipes(struct ath10k *ar)
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002031{
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002032 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2033 struct ath10k_pci_pipe *pipe;
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002034 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002035
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002036 for (i = 0; i < CE_COUNT; i++) {
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002037 pipe = &ar_pci->pipe_info[i];
2038 pipe->ce_hdl = &ar_pci->ce_states[i];
2039 pipe->pipe_num = i;
2040 pipe->hif_ce_state = ar;
2041
Rajkumar Manoharan9d9bdbb2015-10-12 18:27:02 +05302042 ret = ath10k_ce_alloc_pipe(ar, i, &host_ce_config_wlan[i]);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002043 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002044 ath10k_err(ar, "failed to allocate copy engine pipe %d: %d\n",
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002045 i, ret);
2046 return ret;
2047 }
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002048
2049 /* Last CE is Diagnostic Window */
Vasanthakumar Thiagarajan050af062015-06-18 12:31:04 +05302050 if (i == CE_DIAG_PIPE) {
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002051 ar_pci->ce_diag = pipe->ce_hdl;
2052 continue;
2053 }
2054
2055 pipe->buf_sz = (size_t)(host_ce_config_wlan[i].src_sz_max);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002056 }
2057
2058 return 0;
2059}
2060
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002061static void ath10k_pci_free_pipes(struct ath10k *ar)
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002062{
2063 int i;
2064
2065 for (i = 0; i < CE_COUNT; i++)
2066 ath10k_ce_free_pipe(ar, i);
2067}
Kalle Valo5e3dd152013-06-12 20:52:10 +03002068
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002069static int ath10k_pci_init_pipes(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002070{
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002071 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002072
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002073 for (i = 0; i < CE_COUNT; i++) {
2074 ret = ath10k_ce_init_pipe(ar, i, &host_ce_config_wlan[i]);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002075 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002076 ath10k_err(ar, "failed to initialize copy engine pipe %d: %d\n",
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002077 i, ret);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002078 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002079 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002080 }
2081
Kalle Valo5e3dd152013-06-12 20:52:10 +03002082 return 0;
2083}
2084
Michal Kazior5c771e72014-08-22 14:23:34 +02002085static bool ath10k_pci_has_fw_crashed(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002086{
Michal Kazior5c771e72014-08-22 14:23:34 +02002087 return ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS) &
2088 FW_IND_EVENT_PENDING;
2089}
Kalle Valo5e3dd152013-06-12 20:52:10 +03002090
Michal Kazior5c771e72014-08-22 14:23:34 +02002091static void ath10k_pci_fw_crashed_clear(struct ath10k *ar)
2092{
2093 u32 val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002094
Michal Kazior5c771e72014-08-22 14:23:34 +02002095 val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
2096 val &= ~FW_IND_EVENT_PENDING;
2097 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002098}
2099
Michal Kaziorde013572014-05-14 16:56:16 +03002100/* this function effectively clears target memory controller assert line */
2101static void ath10k_pci_warm_reset_si0(struct ath10k *ar)
2102{
2103 u32 val;
2104
2105 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
2106 ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
2107 val | SOC_RESET_CONTROL_SI0_RST_MASK);
2108 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
2109
2110 msleep(10);
2111
2112 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
2113 ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
2114 val & ~SOC_RESET_CONTROL_SI0_RST_MASK);
2115 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
2116
2117 msleep(10);
2118}
2119
Michal Kazior61c16482014-10-28 10:32:06 +01002120static void ath10k_pci_warm_reset_cpu(struct ath10k *ar)
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002121{
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002122 u32 val;
2123
Kalle Valob39712c2014-03-28 09:32:46 +02002124 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, 0);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002125
Michal Kazior61c16482014-10-28 10:32:06 +01002126 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
2127 SOC_RESET_CONTROL_ADDRESS);
2128 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
2129 val | SOC_RESET_CONTROL_CPU_WARM_RST_MASK);
2130}
2131
2132static void ath10k_pci_warm_reset_ce(struct ath10k *ar)
2133{
2134 u32 val;
2135
2136 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
2137 SOC_RESET_CONTROL_ADDRESS);
2138
2139 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
2140 val | SOC_RESET_CONTROL_CE_RST_MASK);
2141 msleep(10);
2142 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
2143 val & ~SOC_RESET_CONTROL_CE_RST_MASK);
2144}
2145
2146static void ath10k_pci_warm_reset_clear_lf(struct ath10k *ar)
2147{
2148 u32 val;
2149
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002150 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
2151 SOC_LF_TIMER_CONTROL0_ADDRESS);
2152 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS +
2153 SOC_LF_TIMER_CONTROL0_ADDRESS,
2154 val & ~SOC_LF_TIMER_CONTROL0_ENABLE_MASK);
Michal Kazior61c16482014-10-28 10:32:06 +01002155}
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002156
Michal Kazior61c16482014-10-28 10:32:06 +01002157static int ath10k_pci_warm_reset(struct ath10k *ar)
2158{
2159 int ret;
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002160
Michal Kazior61c16482014-10-28 10:32:06 +01002161 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot warm reset\n");
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002162
Michal Kazior61c16482014-10-28 10:32:06 +01002163 spin_lock_bh(&ar->data_lock);
2164 ar->stats.fw_warm_reset_counter++;
2165 spin_unlock_bh(&ar->data_lock);
2166
2167 ath10k_pci_irq_disable(ar);
2168
2169 /* Make sure the target CPU is not doing anything dangerous, e.g. if it
2170 * were to access copy engine while host performs copy engine reset
2171 * then it is possible for the device to confuse pci-e controller to
2172 * the point of bringing host system to a complete stop (i.e. hang).
2173 */
Michal Kaziorde013572014-05-14 16:56:16 +03002174 ath10k_pci_warm_reset_si0(ar);
Michal Kazior61c16482014-10-28 10:32:06 +01002175 ath10k_pci_warm_reset_cpu(ar);
2176 ath10k_pci_init_pipes(ar);
2177 ath10k_pci_wait_for_target_init(ar);
Michal Kaziorde013572014-05-14 16:56:16 +03002178
Michal Kazior61c16482014-10-28 10:32:06 +01002179 ath10k_pci_warm_reset_clear_lf(ar);
2180 ath10k_pci_warm_reset_ce(ar);
2181 ath10k_pci_warm_reset_cpu(ar);
2182 ath10k_pci_init_pipes(ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002183
Michal Kazior61c16482014-10-28 10:32:06 +01002184 ret = ath10k_pci_wait_for_target_init(ar);
2185 if (ret) {
2186 ath10k_warn(ar, "failed to wait for target init: %d\n", ret);
2187 return ret;
2188 }
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002189
Michal Kazior7aa7a722014-08-25 12:09:38 +02002190 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot warm reset complete\n");
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002191
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002192 return 0;
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002193}
2194
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +05302195static int ath10k_pci_safe_chip_reset(struct ath10k *ar)
2196{
2197 if (QCA_REV_988X(ar) || QCA_REV_6174(ar)) {
2198 return ath10k_pci_warm_reset(ar);
2199 } else if (QCA_REV_99X0(ar)) {
2200 ath10k_pci_irq_disable(ar);
2201 return ath10k_pci_qca99x0_chip_reset(ar);
2202 } else {
2203 return -ENOTSUPP;
2204 }
2205}
2206
Michal Kaziord63955b2015-01-24 12:14:49 +02002207static int ath10k_pci_qca988x_chip_reset(struct ath10k *ar)
Michal Kazior0bc14d02014-10-28 10:32:07 +01002208{
2209 int i, ret;
2210 u32 val;
2211
Michal Kaziord63955b2015-01-24 12:14:49 +02002212 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot 988x chip reset\n");
Michal Kazior0bc14d02014-10-28 10:32:07 +01002213
2214 /* Some hardware revisions (e.g. CUS223v2) has issues with cold reset.
2215 * It is thus preferred to use warm reset which is safer but may not be
2216 * able to recover the device from all possible fail scenarios.
2217 *
2218 * Warm reset doesn't always work on first try so attempt it a few
2219 * times before giving up.
2220 */
2221 for (i = 0; i < ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS; i++) {
2222 ret = ath10k_pci_warm_reset(ar);
2223 if (ret) {
2224 ath10k_warn(ar, "failed to warm reset attempt %d of %d: %d\n",
2225 i + 1, ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS,
2226 ret);
2227 continue;
2228 }
2229
2230 /* FIXME: Sometimes copy engine doesn't recover after warm
2231 * reset. In most cases this needs cold reset. In some of these
2232 * cases the device is in such a state that a cold reset may
2233 * lock up the host.
2234 *
2235 * Reading any host interest register via copy engine is
2236 * sufficient to verify if device is capable of booting
2237 * firmware blob.
2238 */
2239 ret = ath10k_pci_init_pipes(ar);
2240 if (ret) {
2241 ath10k_warn(ar, "failed to init copy engine: %d\n",
2242 ret);
2243 continue;
2244 }
2245
2246 ret = ath10k_pci_diag_read32(ar, QCA988X_HOST_INTEREST_ADDRESS,
2247 &val);
2248 if (ret) {
2249 ath10k_warn(ar, "failed to poke copy engine: %d\n",
2250 ret);
2251 continue;
2252 }
2253
2254 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot chip reset complete (warm)\n");
2255 return 0;
2256 }
2257
2258 if (ath10k_pci_reset_mode == ATH10K_PCI_RESET_WARM_ONLY) {
2259 ath10k_warn(ar, "refusing cold reset as requested\n");
2260 return -EPERM;
2261 }
2262
2263 ret = ath10k_pci_cold_reset(ar);
2264 if (ret) {
2265 ath10k_warn(ar, "failed to cold reset: %d\n", ret);
2266 return ret;
2267 }
2268
2269 ret = ath10k_pci_wait_for_target_init(ar);
2270 if (ret) {
2271 ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
2272 ret);
2273 return ret;
2274 }
2275
Michal Kaziord63955b2015-01-24 12:14:49 +02002276 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca988x chip reset complete (cold)\n");
Michal Kazior0bc14d02014-10-28 10:32:07 +01002277
2278 return 0;
2279}
2280
Michal Kaziord63955b2015-01-24 12:14:49 +02002281static int ath10k_pci_qca6174_chip_reset(struct ath10k *ar)
2282{
2283 int ret;
2284
2285 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca6174 chip reset\n");
2286
2287 /* FIXME: QCA6174 requires cold + warm reset to work. */
2288
2289 ret = ath10k_pci_cold_reset(ar);
2290 if (ret) {
2291 ath10k_warn(ar, "failed to cold reset: %d\n", ret);
2292 return ret;
2293 }
2294
2295 ret = ath10k_pci_wait_for_target_init(ar);
2296 if (ret) {
2297 ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
Kalle Valo617b0f42015-10-05 17:56:35 +03002298 ret);
Michal Kaziord63955b2015-01-24 12:14:49 +02002299 return ret;
2300 }
2301
2302 ret = ath10k_pci_warm_reset(ar);
2303 if (ret) {
2304 ath10k_warn(ar, "failed to warm reset: %d\n", ret);
2305 return ret;
2306 }
2307
2308 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca6174 chip reset complete (cold)\n");
2309
2310 return 0;
2311}
2312
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +05302313static int ath10k_pci_qca99x0_chip_reset(struct ath10k *ar)
2314{
2315 int ret;
2316
2317 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca99x0 chip reset\n");
2318
2319 ret = ath10k_pci_cold_reset(ar);
2320 if (ret) {
2321 ath10k_warn(ar, "failed to cold reset: %d\n", ret);
2322 return ret;
2323 }
2324
2325 ret = ath10k_pci_wait_for_target_init(ar);
2326 if (ret) {
2327 ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
2328 ret);
2329 return ret;
2330 }
2331
2332 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca99x0 chip reset complete (cold)\n");
2333
2334 return 0;
2335}
2336
Michal Kaziord63955b2015-01-24 12:14:49 +02002337static int ath10k_pci_chip_reset(struct ath10k *ar)
2338{
2339 if (QCA_REV_988X(ar))
2340 return ath10k_pci_qca988x_chip_reset(ar);
2341 else if (QCA_REV_6174(ar))
2342 return ath10k_pci_qca6174_chip_reset(ar);
Bartosz Markowskia226b512015-10-28 15:09:53 +01002343 else if (QCA_REV_9377(ar))
2344 return ath10k_pci_qca6174_chip_reset(ar);
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +05302345 else if (QCA_REV_99X0(ar))
2346 return ath10k_pci_qca99x0_chip_reset(ar);
Michal Kaziord63955b2015-01-24 12:14:49 +02002347 else
2348 return -ENOTSUPP;
2349}
2350
Michal Kazior0bc14d02014-10-28 10:32:07 +01002351static int ath10k_pci_hif_power_up(struct ath10k *ar)
Michal Kazior8c5c5362013-07-16 09:38:50 +02002352{
Janusz Dziedzic76d870e2015-05-18 09:38:16 +00002353 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02002354 int ret;
2355
Michal Kazior0bc14d02014-10-28 10:32:07 +01002356 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif power up\n");
2357
Janusz Dziedzic76d870e2015-05-18 09:38:16 +00002358 pcie_capability_read_word(ar_pci->pdev, PCI_EXP_LNKCTL,
2359 &ar_pci->link_ctl);
2360 pcie_capability_write_word(ar_pci->pdev, PCI_EXP_LNKCTL,
2361 ar_pci->link_ctl & ~PCI_EXP_LNKCTL_ASPMC);
2362
Michal Kazior8c5c5362013-07-16 09:38:50 +02002363 /*
2364 * Bring the target up cleanly.
2365 *
2366 * The target may be in an undefined state with an AUX-powered Target
2367 * and a Host in WoW mode. If the Host crashes, loses power, or is
2368 * restarted (without unloading the driver) then the Target is left
2369 * (aux) powered and running. On a subsequent driver load, the Target
2370 * is in an unexpected state. We try to catch that here in order to
2371 * reset the Target and retry the probe.
2372 */
Michal Kazior0bc14d02014-10-28 10:32:07 +01002373 ret = ath10k_pci_chip_reset(ar);
Michal Kazior5b2589f2013-11-08 08:01:30 +01002374 if (ret) {
Michal Kaziora2fa8802015-01-12 15:29:37 +01002375 if (ath10k_pci_has_fw_crashed(ar)) {
2376 ath10k_warn(ar, "firmware crashed during chip reset\n");
2377 ath10k_pci_fw_crashed_clear(ar);
2378 ath10k_pci_fw_crashed_dump(ar);
2379 }
2380
Michal Kazior0bc14d02014-10-28 10:32:07 +01002381 ath10k_err(ar, "failed to reset chip: %d\n", ret);
Bartosz Markowski707b1bbd2014-10-31 09:03:43 +01002382 goto err_sleep;
Michal Kazior5b2589f2013-11-08 08:01:30 +01002383 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02002384
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002385 ret = ath10k_pci_init_pipes(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02002386 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002387 ath10k_err(ar, "failed to initialize CE: %d\n", ret);
Bartosz Markowski707b1bbd2014-10-31 09:03:43 +01002388 goto err_sleep;
Michal Kaziorab977bd2013-11-25 14:06:26 +01002389 }
2390
Michal Kazior98563d52013-11-08 08:01:33 +01002391 ret = ath10k_pci_init_config(ar);
2392 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002393 ath10k_err(ar, "failed to setup init config: %d\n", ret);
Michal Kazior5c771e72014-08-22 14:23:34 +02002394 goto err_ce;
Michal Kazior98563d52013-11-08 08:01:33 +01002395 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02002396
2397 ret = ath10k_pci_wake_target_cpu(ar);
2398 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002399 ath10k_err(ar, "could not wake up target CPU: %d\n", ret);
Michal Kazior5c771e72014-08-22 14:23:34 +02002400 goto err_ce;
Michal Kazior8c5c5362013-07-16 09:38:50 +02002401 }
2402
2403 return 0;
2404
2405err_ce:
2406 ath10k_pci_ce_deinit(ar);
Michal Kazior0bc14d02014-10-28 10:32:07 +01002407
Bartosz Markowski707b1bbd2014-10-31 09:03:43 +01002408err_sleep:
Michal Kazior8c5c5362013-07-16 09:38:50 +02002409 return ret;
2410}
2411
2412static void ath10k_pci_hif_power_down(struct ath10k *ar)
2413{
Michal Kazior7aa7a722014-08-25 12:09:38 +02002414 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif power down\n");
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002415
Michal Kaziorc011b282014-10-28 10:32:08 +01002416 /* Currently hif_power_up performs effectively a reset and hif_stop
2417 * resets the chip as well so there's no point in resetting here.
2418 */
Michal Kazior8c5c5362013-07-16 09:38:50 +02002419}
2420
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002421#ifdef CONFIG_PM
2422
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002423static int ath10k_pci_hif_suspend(struct ath10k *ar)
2424{
Michal Kazior77258d42015-05-18 09:38:18 +00002425 /* The grace timer can still be counting down and ar->ps_awake be true.
2426 * It is known that the device may be asleep after resuming regardless
2427 * of the SoC powersave state before suspending. Hence make sure the
2428 * device is asleep before proceeding.
2429 */
2430 ath10k_pci_sleep_sync(ar);
Michal Kazior320e14b2015-03-02 13:22:13 +01002431
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002432 return 0;
2433}
2434
2435static int ath10k_pci_hif_resume(struct ath10k *ar)
2436{
2437 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2438 struct pci_dev *pdev = ar_pci->pdev;
2439 u32 val;
Anilkumar Kolli1aaf8ef2015-10-16 15:54:51 +03002440 int ret = 0;
2441
2442 if (ar_pci->pci_ps == 0) {
2443 ret = ath10k_pci_force_wake(ar);
2444 if (ret) {
2445 ath10k_err(ar, "failed to wake up target: %d\n", ret);
2446 return ret;
2447 }
2448 }
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002449
Michal Kazior9ff4be92015-03-02 13:22:14 +01002450 /* Suspend/Resume resets the PCI configuration space, so we have to
2451 * re-disable the RETRY_TIMEOUT register (0x41) to keep PCI Tx retries
2452 * from interfering with C3 CPU state. pci_restore_state won't help
2453 * here since it only restores the first 64 bytes pci config header.
2454 */
2455 pci_read_config_dword(pdev, 0x40, &val);
2456 if ((val & 0x0000ff00) != 0)
2457 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002458
Anilkumar Kolli1aaf8ef2015-10-16 15:54:51 +03002459 return ret;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002460}
2461#endif
2462
Kalle Valo5e3dd152013-06-12 20:52:10 +03002463static const struct ath10k_hif_ops ath10k_pci_hif_ops = {
Michal Kazior726346f2014-02-27 18:50:04 +02002464 .tx_sg = ath10k_pci_hif_tx_sg,
Kalle Valoeef25402014-09-24 14:16:52 +03002465 .diag_read = ath10k_pci_hif_diag_read,
Yanbo Li9f65ad22014-11-25 12:24:48 +02002466 .diag_write = ath10k_pci_diag_write_mem,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002467 .exchange_bmi_msg = ath10k_pci_hif_exchange_bmi_msg,
2468 .start = ath10k_pci_hif_start,
2469 .stop = ath10k_pci_hif_stop,
2470 .map_service_to_pipe = ath10k_pci_hif_map_service_to_pipe,
2471 .get_default_pipe = ath10k_pci_hif_get_default_pipe,
2472 .send_complete_check = ath10k_pci_hif_send_complete_check,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002473 .get_free_queue_number = ath10k_pci_hif_get_free_queue_number,
Michal Kazior8c5c5362013-07-16 09:38:50 +02002474 .power_up = ath10k_pci_hif_power_up,
2475 .power_down = ath10k_pci_hif_power_down,
Yanbo Li077a3802014-11-25 12:24:33 +02002476 .read32 = ath10k_pci_read32,
2477 .write32 = ath10k_pci_write32,
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002478#ifdef CONFIG_PM
2479 .suspend = ath10k_pci_hif_suspend,
2480 .resume = ath10k_pci_hif_resume,
2481#endif
Kalle Valo5e3dd152013-06-12 20:52:10 +03002482};
2483
2484static void ath10k_pci_ce_tasklet(unsigned long ptr)
2485{
Michal Kazior87263e52013-08-27 13:08:01 +02002486 struct ath10k_pci_pipe *pipe = (struct ath10k_pci_pipe *)ptr;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002487 struct ath10k_pci *ar_pci = pipe->ar_pci;
2488
2489 ath10k_ce_per_engine_service(ar_pci->ar, pipe->pipe_num);
2490}
2491
2492static void ath10k_msi_err_tasklet(unsigned long data)
2493{
2494 struct ath10k *ar = (struct ath10k *)data;
2495
Michal Kazior5c771e72014-08-22 14:23:34 +02002496 if (!ath10k_pci_has_fw_crashed(ar)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002497 ath10k_warn(ar, "received unsolicited fw crash interrupt\n");
Michal Kazior5c771e72014-08-22 14:23:34 +02002498 return;
2499 }
2500
Michal Kazior6f3b7ff2015-01-24 12:14:52 +02002501 ath10k_pci_irq_disable(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02002502 ath10k_pci_fw_crashed_clear(ar);
2503 ath10k_pci_fw_crashed_dump(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002504}
2505
2506/*
2507 * Handler for a per-engine interrupt on a PARTICULAR CE.
2508 * This is used in cases where each CE has a private MSI interrupt.
2509 */
2510static irqreturn_t ath10k_pci_per_engine_handler(int irq, void *arg)
2511{
2512 struct ath10k *ar = arg;
2513 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2514 int ce_id = irq - ar_pci->pdev->irq - MSI_ASSIGN_CE_INITIAL;
2515
Dan Carpentere5742672013-06-18 10:28:46 +03002516 if (ce_id < 0 || ce_id >= ARRAY_SIZE(ar_pci->pipe_info)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002517 ath10k_warn(ar, "unexpected/invalid irq %d ce_id %d\n", irq,
2518 ce_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002519 return IRQ_HANDLED;
2520 }
2521
2522 /*
2523 * NOTE: We are able to derive ce_id from irq because we
2524 * use a one-to-one mapping for CE's 0..5.
2525 * CE's 6 & 7 do not use interrupts at all.
2526 *
2527 * This mapping must be kept in sync with the mapping
2528 * used by firmware.
2529 */
2530 tasklet_schedule(&ar_pci->pipe_info[ce_id].intr);
2531 return IRQ_HANDLED;
2532}
2533
2534static irqreturn_t ath10k_pci_msi_fw_handler(int irq, void *arg)
2535{
2536 struct ath10k *ar = arg;
2537 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2538
2539 tasklet_schedule(&ar_pci->msi_fw_err);
2540 return IRQ_HANDLED;
2541}
2542
2543/*
2544 * Top-level interrupt handler for all PCI interrupts from a Target.
2545 * When a block of MSI interrupts is allocated, this top-level handler
2546 * is not used; instead, we directly call the correct sub-handler.
2547 */
2548static irqreturn_t ath10k_pci_interrupt_handler(int irq, void *arg)
2549{
2550 struct ath10k *ar = arg;
2551 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Anilkumar Kolli1aaf8ef2015-10-16 15:54:51 +03002552 int ret;
2553
2554 if (ar_pci->pci_ps == 0) {
2555 ret = ath10k_pci_force_wake(ar);
2556 if (ret) {
2557 ath10k_warn(ar, "failed to wake device up on irq: %d\n",
2558 ret);
2559 return IRQ_NONE;
2560 }
2561 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002562
2563 if (ar_pci->num_msi_intrs == 0) {
Michal Kaziore5398872013-11-25 14:06:20 +01002564 if (!ath10k_pci_irq_pending(ar))
2565 return IRQ_NONE;
2566
Michal Kazior26852182013-11-25 14:06:25 +01002567 ath10k_pci_disable_and_clear_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002568 }
2569
2570 tasklet_schedule(&ar_pci->intr_tq);
2571
2572 return IRQ_HANDLED;
2573}
2574
2575static void ath10k_pci_tasklet(unsigned long data)
2576{
2577 struct ath10k *ar = (struct ath10k *)data;
2578 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2579
Michal Kazior5c771e72014-08-22 14:23:34 +02002580 if (ath10k_pci_has_fw_crashed(ar)) {
Michal Kazior6f3b7ff2015-01-24 12:14:52 +02002581 ath10k_pci_irq_disable(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02002582 ath10k_pci_fw_crashed_clear(ar);
2583 ath10k_pci_fw_crashed_dump(ar);
2584 return;
2585 }
2586
Kalle Valo5e3dd152013-06-12 20:52:10 +03002587 ath10k_ce_per_engine_service_any(ar);
2588
Michal Kazior26852182013-11-25 14:06:25 +01002589 /* Re-enable legacy irq that was disabled in the irq handler */
2590 if (ar_pci->num_msi_intrs == 0)
2591 ath10k_pci_enable_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002592}
2593
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002594static int ath10k_pci_request_irq_msix(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002595{
2596 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002597 int ret, i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002598
2599 ret = request_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW,
2600 ath10k_pci_msi_fw_handler,
2601 IRQF_SHARED, "ath10k_pci", ar);
Michal Kazior591ecdb2013-07-31 10:55:15 +02002602 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002603 ath10k_warn(ar, "failed to request MSI-X fw irq %d: %d\n",
Michal Kazior591ecdb2013-07-31 10:55:15 +02002604 ar_pci->pdev->irq + MSI_ASSIGN_FW, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002605 return ret;
Michal Kazior591ecdb2013-07-31 10:55:15 +02002606 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002607
2608 for (i = MSI_ASSIGN_CE_INITIAL; i <= MSI_ASSIGN_CE_MAX; i++) {
2609 ret = request_irq(ar_pci->pdev->irq + i,
2610 ath10k_pci_per_engine_handler,
2611 IRQF_SHARED, "ath10k_pci", ar);
2612 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002613 ath10k_warn(ar, "failed to request MSI-X ce irq %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002614 ar_pci->pdev->irq + i, ret);
2615
Michal Kazior87b14232013-06-26 08:50:50 +02002616 for (i--; i >= MSI_ASSIGN_CE_INITIAL; i--)
2617 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002618
Michal Kazior87b14232013-06-26 08:50:50 +02002619 free_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002620 return ret;
2621 }
2622 }
2623
Kalle Valo5e3dd152013-06-12 20:52:10 +03002624 return 0;
2625}
2626
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002627static int ath10k_pci_request_irq_msi(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002628{
2629 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2630 int ret;
2631
2632 ret = request_irq(ar_pci->pdev->irq,
2633 ath10k_pci_interrupt_handler,
2634 IRQF_SHARED, "ath10k_pci", ar);
Kalle Valof3782742013-10-17 11:36:15 +03002635 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002636 ath10k_warn(ar, "failed to request MSI irq %d: %d\n",
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002637 ar_pci->pdev->irq, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002638 return ret;
Kalle Valof3782742013-10-17 11:36:15 +03002639 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002640
Kalle Valo5e3dd152013-06-12 20:52:10 +03002641 return 0;
2642}
2643
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002644static int ath10k_pci_request_irq_legacy(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002645{
2646 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002647 int ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002648
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002649 ret = request_irq(ar_pci->pdev->irq,
2650 ath10k_pci_interrupt_handler,
2651 IRQF_SHARED, "ath10k_pci", ar);
Kalle Valof3782742013-10-17 11:36:15 +03002652 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002653 ath10k_warn(ar, "failed to request legacy irq %d: %d\n",
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002654 ar_pci->pdev->irq, ret);
Kalle Valof3782742013-10-17 11:36:15 +03002655 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002656 }
2657
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002658 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002659}
2660
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002661static int ath10k_pci_request_irq(struct ath10k *ar)
2662{
2663 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2664
2665 switch (ar_pci->num_msi_intrs) {
2666 case 0:
2667 return ath10k_pci_request_irq_legacy(ar);
2668 case 1:
2669 return ath10k_pci_request_irq_msi(ar);
Janusz Dziedzicb8402d82015-09-17 08:17:33 +02002670 default:
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002671 return ath10k_pci_request_irq_msix(ar);
2672 }
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002673}
2674
2675static void ath10k_pci_free_irq(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002676{
2677 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2678 int i;
2679
2680 /* There's at least one interrupt irregardless whether its legacy INTR
2681 * or MSI or MSI-X */
2682 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
2683 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002684}
2685
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002686static void ath10k_pci_init_irq_tasklets(struct ath10k *ar)
2687{
2688 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2689 int i;
2690
2691 tasklet_init(&ar_pci->intr_tq, ath10k_pci_tasklet, (unsigned long)ar);
2692 tasklet_init(&ar_pci->msi_fw_err, ath10k_msi_err_tasklet,
2693 (unsigned long)ar);
2694
2695 for (i = 0; i < CE_COUNT; i++) {
2696 ar_pci->pipe_info[i].ar_pci = ar_pci;
2697 tasklet_init(&ar_pci->pipe_info[i].intr, ath10k_pci_ce_tasklet,
2698 (unsigned long)&ar_pci->pipe_info[i]);
2699 }
2700}
2701
2702static int ath10k_pci_init_irq(struct ath10k *ar)
2703{
2704 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2705 int ret;
2706
2707 ath10k_pci_init_irq_tasklets(ar);
2708
Michal Kazior403d6272014-08-22 14:23:31 +02002709 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_AUTO)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002710 ath10k_info(ar, "limiting irq mode to: %d\n",
2711 ath10k_pci_irq_mode);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002712
2713 /* Try MSI-X */
Michal Kazior0edf2572014-08-07 11:03:29 +02002714 if (ath10k_pci_irq_mode == ATH10K_PCI_IRQ_AUTO) {
Janusz Dziedzicb8402d82015-09-17 08:17:33 +02002715 ar_pci->num_msi_intrs = MSI_ASSIGN_CE_MAX + 1;
Alexander Gordeev5ad68672014-02-13 17:50:02 +02002716 ret = pci_enable_msi_range(ar_pci->pdev, ar_pci->num_msi_intrs,
Kalle Valo5b07e072014-09-14 12:50:06 +03002717 ar_pci->num_msi_intrs);
Alexander Gordeev5ad68672014-02-13 17:50:02 +02002718 if (ret > 0)
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002719 return 0;
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002720
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002721 /* fall-through */
2722 }
2723
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002724 /* Try MSI */
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002725 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_LEGACY) {
2726 ar_pci->num_msi_intrs = 1;
2727 ret = pci_enable_msi(ar_pci->pdev);
2728 if (ret == 0)
2729 return 0;
2730
2731 /* fall-through */
2732 }
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002733
2734 /* Try legacy irq
2735 *
2736 * A potential race occurs here: The CORE_BASE write
2737 * depends on target correctly decoding AXI address but
2738 * host won't know when target writes BAR to CORE_CTRL.
2739 * This write might get lost if target has NOT written BAR.
2740 * For now, fix the race by repeating the write in below
2741 * synchronization checking. */
2742 ar_pci->num_msi_intrs = 0;
2743
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002744 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2745 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002746
2747 return 0;
2748}
2749
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002750static void ath10k_pci_deinit_irq_legacy(struct ath10k *ar)
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002751{
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002752 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2753 0);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002754}
2755
2756static int ath10k_pci_deinit_irq(struct ath10k *ar)
2757{
2758 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2759
2760 switch (ar_pci->num_msi_intrs) {
2761 case 0:
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002762 ath10k_pci_deinit_irq_legacy(ar);
Janusz Dziedzicb8402d82015-09-17 08:17:33 +02002763 break;
Alexander Gordeevbb8b6212014-02-13 17:50:01 +02002764 default:
2765 pci_disable_msi(ar_pci->pdev);
Janusz Dziedzicb8402d82015-09-17 08:17:33 +02002766 break;
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002767 }
2768
Janusz Dziedzicb8402d82015-09-17 08:17:33 +02002769 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002770}
2771
Michal Kaziord7fb47f2013-11-08 08:01:26 +01002772static int ath10k_pci_wait_for_target_init(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002773{
2774 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo0399eca2014-03-28 09:32:21 +02002775 unsigned long timeout;
Kalle Valo0399eca2014-03-28 09:32:21 +02002776 u32 val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002777
Michal Kazior7aa7a722014-08-25 12:09:38 +02002778 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot waiting target to initialise\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002779
Kalle Valo0399eca2014-03-28 09:32:21 +02002780 timeout = jiffies + msecs_to_jiffies(ATH10K_PCI_TARGET_WAIT);
2781
2782 do {
2783 val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
2784
Michal Kazior7aa7a722014-08-25 12:09:38 +02002785 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target indicator %x\n",
2786 val);
Kalle Valo50f87a62014-03-28 09:32:52 +02002787
Kalle Valo0399eca2014-03-28 09:32:21 +02002788 /* target should never return this */
2789 if (val == 0xffffffff)
2790 continue;
2791
Michal Kazior7710cd22014-04-23 19:30:04 +03002792 /* the device has crashed so don't bother trying anymore */
2793 if (val & FW_IND_EVENT_PENDING)
2794 break;
2795
Kalle Valo0399eca2014-03-28 09:32:21 +02002796 if (val & FW_IND_INITIALIZED)
2797 break;
2798
Kalle Valo5e3dd152013-06-12 20:52:10 +03002799 if (ar_pci->num_msi_intrs == 0)
2800 /* Fix potential race by repeating CORE_BASE writes */
Michal Kaziora4282492014-10-20 14:14:37 +02002801 ath10k_pci_enable_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002802
Kalle Valo0399eca2014-03-28 09:32:21 +02002803 mdelay(10);
2804 } while (time_before(jiffies, timeout));
2805
Michal Kaziora4282492014-10-20 14:14:37 +02002806 ath10k_pci_disable_and_clear_legacy_irq(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02002807 ath10k_pci_irq_msi_fw_mask(ar);
Michal Kaziora4282492014-10-20 14:14:37 +02002808
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002809 if (val == 0xffffffff) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002810 ath10k_err(ar, "failed to read device register, device is gone\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002811 return -EIO;
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002812 }
2813
Michal Kazior7710cd22014-04-23 19:30:04 +03002814 if (val & FW_IND_EVENT_PENDING) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002815 ath10k_warn(ar, "device has crashed during init\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002816 return -ECOMM;
Michal Kazior7710cd22014-04-23 19:30:04 +03002817 }
2818
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002819 if (!(val & FW_IND_INITIALIZED)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002820 ath10k_err(ar, "failed to receive initialized event from target: %08x\n",
Kalle Valo0399eca2014-03-28 09:32:21 +02002821 val);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002822 return -ETIMEDOUT;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002823 }
2824
Michal Kazior7aa7a722014-08-25 12:09:38 +02002825 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target initialised\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002826 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002827}
2828
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002829static int ath10k_pci_cold_reset(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002830{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002831 u32 val;
2832
Michal Kazior7aa7a722014-08-25 12:09:38 +02002833 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cold reset\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002834
Ben Greearf51dbe72014-09-29 14:41:46 +03002835 spin_lock_bh(&ar->data_lock);
2836
2837 ar->stats.fw_cold_reset_counter++;
2838
2839 spin_unlock_bh(&ar->data_lock);
2840
Kalle Valo5e3dd152013-06-12 20:52:10 +03002841 /* Put Target, including PCIe, into RESET. */
Kalle Valoe479ed42013-09-01 10:01:53 +03002842 val = ath10k_pci_reg_read32(ar, SOC_GLOBAL_RESET_ADDRESS);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002843 val |= 1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002844 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002845
Vasanthakumar Thiagarajanacd19582015-07-10 14:31:20 +05302846 /* After writing into SOC_GLOBAL_RESET to put device into
2847 * reset and pulling out of reset pcie may not be stable
2848 * for any immediate pcie register access and cause bus error,
2849 * add delay before any pcie access request to fix this issue.
2850 */
2851 msleep(20);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002852
2853 /* Pull Target, including PCIe, out of RESET. */
2854 val &= ~1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002855 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002856
Vasanthakumar Thiagarajanacd19582015-07-10 14:31:20 +05302857 msleep(20);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002858
Michal Kazior7aa7a722014-08-25 12:09:38 +02002859 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cold reset complete\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02002860
Michal Kazior5b2589f2013-11-08 08:01:30 +01002861 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002862}
2863
Michal Kazior2986e3e2014-08-07 11:03:30 +02002864static int ath10k_pci_claim(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002865{
Michal Kazior2986e3e2014-08-07 11:03:30 +02002866 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2867 struct pci_dev *pdev = ar_pci->pdev;
Michal Kazior2986e3e2014-08-07 11:03:30 +02002868 int ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002869
2870 pci_set_drvdata(pdev, ar);
2871
Kalle Valo5e3dd152013-06-12 20:52:10 +03002872 ret = pci_enable_device(pdev);
2873 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002874 ath10k_err(ar, "failed to enable pci device: %d\n", ret);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002875 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002876 }
2877
Kalle Valo5e3dd152013-06-12 20:52:10 +03002878 ret = pci_request_region(pdev, BAR_NUM, "ath");
2879 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002880 ath10k_err(ar, "failed to request region BAR%d: %d\n", BAR_NUM,
Michal Kazior2986e3e2014-08-07 11:03:30 +02002881 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002882 goto err_device;
2883 }
2884
Michal Kazior2986e3e2014-08-07 11:03:30 +02002885 /* Target expects 32 bit DMA. Enforce it. */
Kalle Valo5e3dd152013-06-12 20:52:10 +03002886 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2887 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002888 ath10k_err(ar, "failed to set dma mask to 32-bit: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002889 goto err_region;
2890 }
2891
2892 ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2893 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002894 ath10k_err(ar, "failed to set consistent dma mask to 32-bit: %d\n",
Michal Kazior2986e3e2014-08-07 11:03:30 +02002895 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002896 goto err_region;
2897 }
2898
Kalle Valo5e3dd152013-06-12 20:52:10 +03002899 pci_set_master(pdev);
2900
Kalle Valo5e3dd152013-06-12 20:52:10 +03002901 /* Arrange for access to Target SoC registers. */
Michal Kazioraeae5b42015-06-15 14:46:42 +03002902 ar_pci->mem_len = pci_resource_len(pdev, BAR_NUM);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002903 ar_pci->mem = pci_iomap(pdev, BAR_NUM, 0);
2904 if (!ar_pci->mem) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002905 ath10k_err(ar, "failed to iomap BAR%d\n", BAR_NUM);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002906 ret = -EIO;
2907 goto err_master;
2908 }
2909
Michal Kazior7aa7a722014-08-25 12:09:38 +02002910 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot pci_mem 0x%p\n", ar_pci->mem);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002911 return 0;
2912
2913err_master:
2914 pci_clear_master(pdev);
2915
2916err_region:
2917 pci_release_region(pdev, BAR_NUM);
2918
2919err_device:
2920 pci_disable_device(pdev);
2921
2922 return ret;
2923}
2924
2925static void ath10k_pci_release(struct ath10k *ar)
2926{
2927 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2928 struct pci_dev *pdev = ar_pci->pdev;
2929
2930 pci_iounmap(pdev, ar_pci->mem);
2931 pci_release_region(pdev, BAR_NUM);
2932 pci_clear_master(pdev);
2933 pci_disable_device(pdev);
2934}
2935
Michal Kazior7505f7c2014-12-02 10:55:54 +02002936static bool ath10k_pci_chip_is_supported(u32 dev_id, u32 chip_id)
2937{
2938 const struct ath10k_pci_supp_chip *supp_chip;
2939 int i;
2940 u32 rev_id = MS(chip_id, SOC_CHIP_ID_REV);
2941
2942 for (i = 0; i < ARRAY_SIZE(ath10k_pci_supp_chips); i++) {
2943 supp_chip = &ath10k_pci_supp_chips[i];
2944
2945 if (supp_chip->dev_id == dev_id &&
2946 supp_chip->rev_id == rev_id)
2947 return true;
2948 }
2949
2950 return false;
2951}
2952
Kalle Valo5e3dd152013-06-12 20:52:10 +03002953static int ath10k_pci_probe(struct pci_dev *pdev,
2954 const struct pci_device_id *pci_dev)
2955{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002956 int ret = 0;
2957 struct ath10k *ar;
2958 struct ath10k_pci *ar_pci;
Michal Kaziord63955b2015-01-24 12:14:49 +02002959 enum ath10k_hw_rev hw_rev;
Michal Kazior2986e3e2014-08-07 11:03:30 +02002960 u32 chip_id;
Anilkumar Kolli1aaf8ef2015-10-16 15:54:51 +03002961 bool pci_ps;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002962
Michal Kaziord63955b2015-01-24 12:14:49 +02002963 switch (pci_dev->device) {
2964 case QCA988X_2_0_DEVICE_ID:
2965 hw_rev = ATH10K_HW_QCA988X;
Anilkumar Kolli1aaf8ef2015-10-16 15:54:51 +03002966 pci_ps = false;
Michal Kaziord63955b2015-01-24 12:14:49 +02002967 break;
Michal Kazior36582e52015-08-13 14:32:26 +02002968 case QCA6164_2_1_DEVICE_ID:
Michal Kaziord63955b2015-01-24 12:14:49 +02002969 case QCA6174_2_1_DEVICE_ID:
2970 hw_rev = ATH10K_HW_QCA6174;
Anilkumar Kolli1aaf8ef2015-10-16 15:54:51 +03002971 pci_ps = true;
Michal Kaziord63955b2015-01-24 12:14:49 +02002972 break;
Vasanthakumar Thiagarajan8bd47022015-06-18 12:31:03 +05302973 case QCA99X0_2_0_DEVICE_ID:
2974 hw_rev = ATH10K_HW_QCA99X0;
Anilkumar Kolli1aaf8ef2015-10-16 15:54:51 +03002975 pci_ps = false;
Vasanthakumar Thiagarajan8bd47022015-06-18 12:31:03 +05302976 break;
Bartosz Markowskia226b512015-10-28 15:09:53 +01002977 case QCA9377_1_0_DEVICE_ID:
2978 hw_rev = ATH10K_HW_QCA9377;
2979 pci_ps = true;
2980 break;
Michal Kaziord63955b2015-01-24 12:14:49 +02002981 default:
2982 WARN_ON(1);
2983 return -ENOTSUPP;
2984 }
2985
2986 ar = ath10k_core_create(sizeof(*ar_pci), &pdev->dev, ATH10K_BUS_PCI,
2987 hw_rev, &ath10k_pci_hif_ops);
Michal Kaziore7b54192014-08-07 11:03:27 +02002988 if (!ar) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002989 dev_err(&pdev->dev, "failed to allocate core\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002990 return -ENOMEM;
Michal Kaziore7b54192014-08-07 11:03:27 +02002991 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002992
Manikanta Pubbisetty0a51b342015-10-09 11:55:58 +03002993 ath10k_dbg(ar, ATH10K_DBG_BOOT, "pci probe %04x:%04x %04x:%04x\n",
2994 pdev->vendor, pdev->device,
2995 pdev->subsystem_vendor, pdev->subsystem_device);
Michal Kazior7aa7a722014-08-25 12:09:38 +02002996
Michal Kaziore7b54192014-08-07 11:03:27 +02002997 ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002998 ar_pci->pdev = pdev;
2999 ar_pci->dev = &pdev->dev;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003000 ar_pci->ar = ar;
Michal Kazior36582e52015-08-13 14:32:26 +02003001 ar->dev_id = pci_dev->device;
Anilkumar Kolli1aaf8ef2015-10-16 15:54:51 +03003002 ar_pci->pci_ps = pci_ps;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003003
Manikanta Pubbisetty0a51b342015-10-09 11:55:58 +03003004 ar->id.vendor = pdev->vendor;
3005 ar->id.device = pdev->device;
3006 ar->id.subsystem_vendor = pdev->subsystem_vendor;
3007 ar->id.subsystem_device = pdev->subsystem_device;
Michal Kaziorde57e2c2015-04-17 09:19:17 +00003008
Kalle Valo5e3dd152013-06-12 20:52:10 +03003009 spin_lock_init(&ar_pci->ce_lock);
Michal Kazior77258d42015-05-18 09:38:18 +00003010 spin_lock_init(&ar_pci->ps_lock);
3011
Michal Kazior728f95e2014-08-22 14:33:14 +02003012 setup_timer(&ar_pci->rx_post_retry, ath10k_pci_rx_replenish_retry,
3013 (unsigned long)ar);
Michal Kazior77258d42015-05-18 09:38:18 +00003014 setup_timer(&ar_pci->ps_timer, ath10k_pci_ps_timer,
3015 (unsigned long)ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003016
Michal Kazior2986e3e2014-08-07 11:03:30 +02003017 ret = ath10k_pci_claim(ar);
Kalle Valoe01ae682013-09-01 11:22:14 +03003018 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003019 ath10k_err(ar, "failed to claim device: %d\n", ret);
Michal Kaziore7b54192014-08-07 11:03:27 +02003020 goto err_core_destroy;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003021 }
3022
Michal Kazior84cbf3a2014-10-20 14:14:39 +02003023 ret = ath10k_pci_alloc_pipes(ar);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02003024 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003025 ath10k_err(ar, "failed to allocate copy engine pipes: %d\n",
3026 ret);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02003027 goto err_sleep;
Michal Kazior25d0dbc2014-03-28 10:02:38 +02003028 }
3029
Michal Kazior403d6272014-08-22 14:23:31 +02003030 ath10k_pci_ce_deinit(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02003031 ath10k_pci_irq_disable(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02003032
Anilkumar Kolli1aaf8ef2015-10-16 15:54:51 +03003033 if (ar_pci->pci_ps == 0) {
3034 ret = ath10k_pci_force_wake(ar);
3035 if (ret) {
3036 ath10k_warn(ar, "failed to wake up device : %d\n", ret);
3037 goto err_free_pipes;
3038 }
3039 }
3040
Michal Kazior403d6272014-08-22 14:23:31 +02003041 ret = ath10k_pci_init_irq(ar);
3042 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003043 ath10k_err(ar, "failed to init irqs: %d\n", ret);
Michal Kazior84cbf3a2014-10-20 14:14:39 +02003044 goto err_free_pipes;
Michal Kazior403d6272014-08-22 14:23:31 +02003045 }
3046
Michal Kazior7aa7a722014-08-25 12:09:38 +02003047 ath10k_info(ar, "pci irq %s interrupts %d irq_mode %d reset_mode %d\n",
Michal Kazior403d6272014-08-22 14:23:31 +02003048 ath10k_pci_get_irq_method(ar), ar_pci->num_msi_intrs,
3049 ath10k_pci_irq_mode, ath10k_pci_reset_mode);
3050
Michal Kazior5c771e72014-08-22 14:23:34 +02003051 ret = ath10k_pci_request_irq(ar);
Michal Kazior403d6272014-08-22 14:23:31 +02003052 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003053 ath10k_warn(ar, "failed to request irqs: %d\n", ret);
Michal Kazior403d6272014-08-22 14:23:31 +02003054 goto err_deinit_irq;
3055 }
3056
Michal Kazior1a7fecb2015-01-24 12:14:48 +02003057 ret = ath10k_pci_chip_reset(ar);
3058 if (ret) {
3059 ath10k_err(ar, "failed to reset chip: %d\n", ret);
3060 goto err_free_irq;
3061 }
3062
3063 chip_id = ath10k_pci_soc_read32(ar, SOC_CHIP_ID_ADDRESS);
3064 if (chip_id == 0xffffffff) {
3065 ath10k_err(ar, "failed to get chip id\n");
3066 goto err_free_irq;
3067 }
3068
3069 if (!ath10k_pci_chip_is_supported(pdev->device, chip_id)) {
3070 ath10k_err(ar, "device %04x with chip_id %08x isn't supported\n",
3071 pdev->device, chip_id);
Michal Kaziord9585a92015-04-10 13:01:27 +00003072 goto err_free_irq;
Michal Kazior1a7fecb2015-01-24 12:14:48 +02003073 }
3074
Kalle Valoe01ae682013-09-01 11:22:14 +03003075 ret = ath10k_core_register(ar, chip_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003076 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003077 ath10k_err(ar, "failed to register driver core: %d\n", ret);
Michal Kazior5c771e72014-08-22 14:23:34 +02003078 goto err_free_irq;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003079 }
3080
3081 return 0;
3082
Michal Kazior5c771e72014-08-22 14:23:34 +02003083err_free_irq:
3084 ath10k_pci_free_irq(ar);
Michal Kazior21396272014-08-28 10:24:40 +02003085 ath10k_pci_kill_tasklet(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02003086
Michal Kazior403d6272014-08-22 14:23:31 +02003087err_deinit_irq:
3088 ath10k_pci_deinit_irq(ar);
3089
Michal Kazior84cbf3a2014-10-20 14:14:39 +02003090err_free_pipes:
3091 ath10k_pci_free_pipes(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02003092
Michal Kaziorc0c378f2014-08-07 11:03:28 +02003093err_sleep:
Michal Kazior0bcbbe62015-05-29 07:35:24 +02003094 ath10k_pci_sleep_sync(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02003095 ath10k_pci_release(ar);
3096
Michal Kaziore7b54192014-08-07 11:03:27 +02003097err_core_destroy:
Kalle Valo5e3dd152013-06-12 20:52:10 +03003098 ath10k_core_destroy(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003099
3100 return ret;
3101}
3102
3103static void ath10k_pci_remove(struct pci_dev *pdev)
3104{
3105 struct ath10k *ar = pci_get_drvdata(pdev);
3106 struct ath10k_pci *ar_pci;
3107
Michal Kazior7aa7a722014-08-25 12:09:38 +02003108 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci remove\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03003109
3110 if (!ar)
3111 return;
3112
3113 ar_pci = ath10k_pci_priv(ar);
3114
3115 if (!ar_pci)
3116 return;
3117
Kalle Valo5e3dd152013-06-12 20:52:10 +03003118 ath10k_core_unregister(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02003119 ath10k_pci_free_irq(ar);
Michal Kazior21396272014-08-28 10:24:40 +02003120 ath10k_pci_kill_tasklet(ar);
Michal Kazior403d6272014-08-22 14:23:31 +02003121 ath10k_pci_deinit_irq(ar);
3122 ath10k_pci_ce_deinit(ar);
Michal Kazior84cbf3a2014-10-20 14:14:39 +02003123 ath10k_pci_free_pipes(ar);
Michal Kazior77258d42015-05-18 09:38:18 +00003124 ath10k_pci_sleep_sync(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02003125 ath10k_pci_release(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003126 ath10k_core_destroy(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003127}
3128
Kalle Valo5e3dd152013-06-12 20:52:10 +03003129MODULE_DEVICE_TABLE(pci, ath10k_pci_id_table);
3130
3131static struct pci_driver ath10k_pci_driver = {
3132 .name = "ath10k_pci",
3133 .id_table = ath10k_pci_id_table,
3134 .probe = ath10k_pci_probe,
3135 .remove = ath10k_pci_remove,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003136};
3137
3138static int __init ath10k_pci_init(void)
3139{
3140 int ret;
3141
3142 ret = pci_register_driver(&ath10k_pci_driver);
3143 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003144 printk(KERN_ERR "failed to register ath10k pci driver: %d\n",
3145 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003146
3147 return ret;
3148}
3149module_init(ath10k_pci_init);
3150
3151static void __exit ath10k_pci_exit(void)
3152{
3153 pci_unregister_driver(&ath10k_pci_driver);
3154}
3155
3156module_exit(ath10k_pci_exit);
3157
3158MODULE_AUTHOR("Qualcomm Atheros");
3159MODULE_DESCRIPTION("Driver support for Atheros QCA988X PCIe devices");
3160MODULE_LICENSE("Dual BSD/GPL");
Bartosz Markowski5c427f52015-02-18 13:16:37 +01003161
3162/* QCA988x 2.0 firmware files */
Bartosz Markowski8026cae2014-10-06 14:16:41 +02003163MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_FW_FILE);
3164MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API2_FILE);
3165MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API3_FILE);
Bartosz Markowski5c427f52015-02-18 13:16:37 +01003166MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API4_FILE);
Kalle Valo53513c32015-03-25 13:12:42 +02003167MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API5_FILE);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003168MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_BOARD_DATA_FILE);
Manikanta Pubbisetty0a51b342015-10-09 11:55:58 +03003169MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_BOARD_API2_FILE);
Bartosz Markowski5c427f52015-02-18 13:16:37 +01003170
3171/* QCA6174 2.1 firmware files */
3172MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" ATH10K_FW_API4_FILE);
Michal Kaziore451c1d2015-05-26 13:09:22 +02003173MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" ATH10K_FW_API5_FILE);
Bartosz Markowski5c427f52015-02-18 13:16:37 +01003174MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" QCA6174_HW_2_1_BOARD_DATA_FILE);
Manikanta Pubbisetty0a51b342015-10-09 11:55:58 +03003175MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" ATH10K_BOARD_API2_FILE);
Bartosz Markowski5c427f52015-02-18 13:16:37 +01003176
3177/* QCA6174 3.1 firmware files */
3178MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" ATH10K_FW_API4_FILE);
Michal Kaziore451c1d2015-05-26 13:09:22 +02003179MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" ATH10K_FW_API5_FILE);
Bartosz Markowski5c427f52015-02-18 13:16:37 +01003180MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" QCA6174_HW_3_0_BOARD_DATA_FILE);
Manikanta Pubbisetty0a51b342015-10-09 11:55:58 +03003181MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" ATH10K_BOARD_API2_FILE);
Bartosz Markowskia226b512015-10-28 15:09:53 +01003182
3183/* QCA9377 1.0 firmware files */
3184MODULE_FIRMWARE(QCA9377_HW_1_0_FW_DIR "/" ATH10K_FW_API5_FILE);
3185MODULE_FIRMWARE(QCA9377_HW_1_0_FW_DIR "/" QCA9377_HW_1_0_BOARD_DATA_FILE);