blob: 14dce2a1b39fa92343b88a5d8afd56350333deea [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
Benoit Taine9baa3c32014-08-08 15:56:03 +020060static const struct pci_device_id ath10k_pci_id_table[] = {
Kalle Valo5e3dd152013-06-12 20:52:10 +030061 { PCI_VDEVICE(ATHEROS, QCA988X_2_0_DEVICE_ID) }, /* PCI-E QCA988X V2 */
Michal Kazior36582e52015-08-13 14:32:26 +020062 { PCI_VDEVICE(ATHEROS, QCA6164_2_1_DEVICE_ID) }, /* PCI-E QCA6164 V2.1 */
Michal Kaziord63955b2015-01-24 12:14:49 +020063 { PCI_VDEVICE(ATHEROS, QCA6174_2_1_DEVICE_ID) }, /* PCI-E QCA6174 V2.1 */
Vasanthakumar Thiagarajan8a055a82015-07-29 11:40:39 +030064 { PCI_VDEVICE(ATHEROS, QCA99X0_2_0_DEVICE_ID) }, /* PCI-E QCA99X0 V2 */
Bartosz Markowskia226b512015-10-28 15:09:53 +010065 { PCI_VDEVICE(ATHEROS, QCA9377_1_0_DEVICE_ID) }, /* PCI-E QCA9377 V1 */
Kalle Valo5e3dd152013-06-12 20:52:10 +030066 {0}
67};
68
Michal Kazior7505f7c2014-12-02 10:55:54 +020069static const struct ath10k_pci_supp_chip ath10k_pci_supp_chips[] = {
70 /* QCA988X pre 2.0 chips are not supported because they need some nasty
71 * hacks. ath10k doesn't have them and these devices crash horribly
72 * because of that.
73 */
74 { QCA988X_2_0_DEVICE_ID, QCA988X_HW_2_0_CHIP_ID_REV },
Michal Kazior36582e52015-08-13 14:32:26 +020075
76 { QCA6164_2_1_DEVICE_ID, QCA6174_HW_2_1_CHIP_ID_REV },
77 { QCA6164_2_1_DEVICE_ID, QCA6174_HW_2_2_CHIP_ID_REV },
78 { QCA6164_2_1_DEVICE_ID, QCA6174_HW_3_0_CHIP_ID_REV },
79 { QCA6164_2_1_DEVICE_ID, QCA6174_HW_3_1_CHIP_ID_REV },
80 { QCA6164_2_1_DEVICE_ID, QCA6174_HW_3_2_CHIP_ID_REV },
81
Michal Kaziord63955b2015-01-24 12:14:49 +020082 { QCA6174_2_1_DEVICE_ID, QCA6174_HW_2_1_CHIP_ID_REV },
83 { QCA6174_2_1_DEVICE_ID, QCA6174_HW_2_2_CHIP_ID_REV },
84 { QCA6174_2_1_DEVICE_ID, QCA6174_HW_3_0_CHIP_ID_REV },
85 { QCA6174_2_1_DEVICE_ID, QCA6174_HW_3_1_CHIP_ID_REV },
86 { QCA6174_2_1_DEVICE_ID, QCA6174_HW_3_2_CHIP_ID_REV },
Michal Kazior36582e52015-08-13 14:32:26 +020087
Vasanthakumar Thiagarajan8a055a82015-07-29 11:40:39 +030088 { QCA99X0_2_0_DEVICE_ID, QCA99X0_HW_2_0_CHIP_ID_REV },
Bartosz Markowski12551ce2015-11-05 09:50:40 +010089 { QCA9377_1_0_DEVICE_ID, QCA9377_HW_1_1_CHIP_ID_REV },
Michal Kazior7505f7c2014-12-02 10:55:54 +020090};
91
Michal Kazior728f95e2014-08-22 14:33:14 +020092static void ath10k_pci_buffer_cleanup(struct ath10k *ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +010093static int ath10k_pci_cold_reset(struct ath10k *ar);
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +053094static int ath10k_pci_safe_chip_reset(struct ath10k *ar);
Michal Kaziord7fb47f2013-11-08 08:01:26 +010095static int ath10k_pci_wait_for_target_init(struct ath10k *ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +010096static int ath10k_pci_init_irq(struct ath10k *ar);
97static int ath10k_pci_deinit_irq(struct ath10k *ar);
98static int ath10k_pci_request_irq(struct ath10k *ar);
99static void ath10k_pci_free_irq(struct ath10k *ar);
Michal Kazior85622cd2013-11-25 14:06:22 +0100100static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
101 struct ath10k_ce_pipe *rx_pipe,
102 struct bmi_xfer *xfer);
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +0530103static int ath10k_pci_qca99x0_chip_reset(struct ath10k *ar);
Rajkumar Manoharan0e5b2952015-10-12 18:27:01 +0530104static void ath10k_pci_htc_tx_cb(struct ath10k_ce_pipe *ce_state);
Rajkumar Manoharan9d9bdbb2015-10-12 18:27:02 +0530105static void ath10k_pci_htc_rx_cb(struct ath10k_ce_pipe *ce_state);
Rajkumar Manoharana70587b2015-10-12 18:27:04 +0530106static void ath10k_pci_htt_tx_cb(struct ath10k_ce_pipe *ce_state);
107static void ath10k_pci_htt_rx_cb(struct ath10k_ce_pipe *ce_state);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300108
109static const struct ce_attr host_ce_config_wlan[] = {
Kalle Valo48e9c222013-09-01 10:01:32 +0300110 /* CE0: host->target HTC control and raw streams */
111 {
112 .flags = CE_ATTR_FLAGS,
113 .src_nentries = 16,
114 .src_sz_max = 256,
115 .dest_nentries = 0,
Rajkumar Manoharan0e5b2952015-10-12 18:27:01 +0530116 .send_cb = ath10k_pci_htc_tx_cb,
Kalle Valo48e9c222013-09-01 10:01:32 +0300117 },
118
119 /* CE1: target->host HTT + HTC control */
120 {
121 .flags = CE_ATTR_FLAGS,
122 .src_nentries = 0,
Michal Kazior63838642015-02-09 15:04:55 +0100123 .src_sz_max = 2048,
Kalle Valo48e9c222013-09-01 10:01:32 +0300124 .dest_nentries = 512,
Rajkumar Manoharan9d9bdbb2015-10-12 18:27:02 +0530125 .recv_cb = ath10k_pci_htc_rx_cb,
Kalle Valo48e9c222013-09-01 10:01:32 +0300126 },
127
128 /* CE2: target->host WMI */
129 {
130 .flags = CE_ATTR_FLAGS,
131 .src_nentries = 0,
132 .src_sz_max = 2048,
Rajkumar Manoharan30abb332015-03-04 15:43:44 +0200133 .dest_nentries = 128,
Rajkumar Manoharan9d9bdbb2015-10-12 18:27:02 +0530134 .recv_cb = ath10k_pci_htc_rx_cb,
Kalle Valo48e9c222013-09-01 10:01:32 +0300135 },
136
137 /* CE3: host->target WMI */
138 {
139 .flags = CE_ATTR_FLAGS,
140 .src_nentries = 32,
141 .src_sz_max = 2048,
142 .dest_nentries = 0,
Rajkumar Manoharan0e5b2952015-10-12 18:27:01 +0530143 .send_cb = ath10k_pci_htc_tx_cb,
Kalle Valo48e9c222013-09-01 10:01:32 +0300144 },
145
146 /* CE4: host->target HTT */
147 {
148 .flags = CE_ATTR_FLAGS | CE_ATTR_DIS_INTR,
149 .src_nentries = CE_HTT_H2T_MSG_SRC_NENTRIES,
150 .src_sz_max = 256,
151 .dest_nentries = 0,
Rajkumar Manoharana70587b2015-10-12 18:27:04 +0530152 .send_cb = ath10k_pci_htt_tx_cb,
Kalle Valo48e9c222013-09-01 10:01:32 +0300153 },
154
Rajkumar Manoharana70587b2015-10-12 18:27:04 +0530155 /* CE5: target->host HTT (HIF->HTT) */
Kalle Valo48e9c222013-09-01 10:01:32 +0300156 {
157 .flags = CE_ATTR_FLAGS,
158 .src_nentries = 0,
Rajkumar Manoharana70587b2015-10-12 18:27:04 +0530159 .src_sz_max = 512,
160 .dest_nentries = 512,
161 .recv_cb = ath10k_pci_htt_rx_cb,
Kalle Valo48e9c222013-09-01 10:01:32 +0300162 },
163
164 /* CE6: target autonomous hif_memcpy */
165 {
166 .flags = CE_ATTR_FLAGS,
167 .src_nentries = 0,
168 .src_sz_max = 0,
169 .dest_nentries = 0,
170 },
171
172 /* CE7: ce_diag, the Diagnostic Window */
173 {
174 .flags = CE_ATTR_FLAGS,
175 .src_nentries = 2,
176 .src_sz_max = DIAG_TRANSFER_LIMIT,
177 .dest_nentries = 2,
178 },
Vasanthakumar Thiagarajan050af062015-06-18 12:31:04 +0530179
180 /* CE8: target->host pktlog */
181 {
182 .flags = CE_ATTR_FLAGS,
183 .src_nentries = 0,
184 .src_sz_max = 2048,
185 .dest_nentries = 128,
186 },
187
188 /* CE9 target autonomous qcache memcpy */
189 {
190 .flags = CE_ATTR_FLAGS,
191 .src_nentries = 0,
192 .src_sz_max = 0,
193 .dest_nentries = 0,
194 },
195
196 /* CE10: target autonomous hif memcpy */
197 {
198 .flags = CE_ATTR_FLAGS,
199 .src_nentries = 0,
200 .src_sz_max = 0,
201 .dest_nentries = 0,
202 },
203
204 /* CE11: target autonomous hif memcpy */
205 {
206 .flags = CE_ATTR_FLAGS,
207 .src_nentries = 0,
208 .src_sz_max = 0,
209 .dest_nentries = 0,
210 },
Kalle Valo5e3dd152013-06-12 20:52:10 +0300211};
212
213/* Target firmware's Copy Engine configuration. */
214static const struct ce_pipe_config target_ce_config_wlan[] = {
Kalle Valod88effb2013-09-01 10:01:39 +0300215 /* CE0: host->target HTC control and raw streams */
216 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300217 .pipenum = __cpu_to_le32(0),
218 .pipedir = __cpu_to_le32(PIPEDIR_OUT),
219 .nentries = __cpu_to_le32(32),
220 .nbytes_max = __cpu_to_le32(256),
221 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
222 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300223 },
224
225 /* CE1: target->host HTT + HTC control */
226 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300227 .pipenum = __cpu_to_le32(1),
228 .pipedir = __cpu_to_le32(PIPEDIR_IN),
229 .nentries = __cpu_to_le32(32),
Michal Kazior63838642015-02-09 15:04:55 +0100230 .nbytes_max = __cpu_to_le32(2048),
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300231 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
232 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300233 },
234
235 /* CE2: target->host WMI */
236 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300237 .pipenum = __cpu_to_le32(2),
238 .pipedir = __cpu_to_le32(PIPEDIR_IN),
Rajkumar Manoharan30abb332015-03-04 15:43:44 +0200239 .nentries = __cpu_to_le32(64),
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300240 .nbytes_max = __cpu_to_le32(2048),
241 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
242 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300243 },
244
245 /* CE3: host->target WMI */
246 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300247 .pipenum = __cpu_to_le32(3),
248 .pipedir = __cpu_to_le32(PIPEDIR_OUT),
249 .nentries = __cpu_to_le32(32),
250 .nbytes_max = __cpu_to_le32(2048),
251 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
252 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300253 },
254
255 /* CE4: host->target HTT */
256 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300257 .pipenum = __cpu_to_le32(4),
258 .pipedir = __cpu_to_le32(PIPEDIR_OUT),
259 .nentries = __cpu_to_le32(256),
260 .nbytes_max = __cpu_to_le32(256),
261 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
262 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300263 },
264
Kalle Valo5e3dd152013-06-12 20:52:10 +0300265 /* NB: 50% of src nentries, since tx has 2 frags */
Kalle Valod88effb2013-09-01 10:01:39 +0300266
Rajkumar Manoharana70587b2015-10-12 18:27:04 +0530267 /* CE5: target->host HTT (HIF->HTT) */
Kalle Valod88effb2013-09-01 10:01:39 +0300268 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300269 .pipenum = __cpu_to_le32(5),
Rajkumar Manoharana70587b2015-10-12 18:27:04 +0530270 .pipedir = __cpu_to_le32(PIPEDIR_IN),
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300271 .nentries = __cpu_to_le32(32),
Rajkumar Manoharana70587b2015-10-12 18:27:04 +0530272 .nbytes_max = __cpu_to_le32(512),
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300273 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
274 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300275 },
276
277 /* CE6: Reserved for target autonomous hif_memcpy */
278 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300279 .pipenum = __cpu_to_le32(6),
280 .pipedir = __cpu_to_le32(PIPEDIR_INOUT),
281 .nentries = __cpu_to_le32(32),
282 .nbytes_max = __cpu_to_le32(4096),
283 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
284 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300285 },
286
Kalle Valo5e3dd152013-06-12 20:52:10 +0300287 /* CE7 used only by Host */
Vasanthakumar Thiagarajan050af062015-06-18 12:31:04 +0530288 {
289 .pipenum = __cpu_to_le32(7),
290 .pipedir = __cpu_to_le32(PIPEDIR_INOUT),
291 .nentries = __cpu_to_le32(0),
292 .nbytes_max = __cpu_to_le32(0),
293 .flags = __cpu_to_le32(0),
294 .reserved = __cpu_to_le32(0),
295 },
296
297 /* CE8 target->host packtlog */
298 {
299 .pipenum = __cpu_to_le32(8),
300 .pipedir = __cpu_to_le32(PIPEDIR_IN),
301 .nentries = __cpu_to_le32(64),
302 .nbytes_max = __cpu_to_le32(2048),
303 .flags = __cpu_to_le32(CE_ATTR_FLAGS | CE_ATTR_DIS_INTR),
304 .reserved = __cpu_to_le32(0),
305 },
306
307 /* CE9 target autonomous qcache memcpy */
308 {
309 .pipenum = __cpu_to_le32(9),
310 .pipedir = __cpu_to_le32(PIPEDIR_INOUT),
311 .nentries = __cpu_to_le32(32),
312 .nbytes_max = __cpu_to_le32(2048),
313 .flags = __cpu_to_le32(CE_ATTR_FLAGS | CE_ATTR_DIS_INTR),
314 .reserved = __cpu_to_le32(0),
315 },
316
317 /* It not necessary to send target wlan configuration for CE10 & CE11
318 * as these CEs are not actively used in target.
319 */
Kalle Valo5e3dd152013-06-12 20:52:10 +0300320};
321
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300322/*
323 * Map from service/endpoint to Copy Engine.
324 * This table is derived from the CE_PCI TABLE, above.
325 * It is passed to the Target at startup for use by firmware.
326 */
327static const struct service_to_pipe target_service_to_ce_map_wlan[] = {
328 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300329 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VO),
330 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
331 __cpu_to_le32(3),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300332 },
333 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300334 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VO),
335 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
336 __cpu_to_le32(2),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300337 },
338 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300339 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BK),
340 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
341 __cpu_to_le32(3),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300342 },
343 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300344 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BK),
345 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
346 __cpu_to_le32(2),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300347 },
348 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300349 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BE),
350 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
351 __cpu_to_le32(3),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300352 },
353 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300354 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BE),
355 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
356 __cpu_to_le32(2),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300357 },
358 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300359 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VI),
360 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
361 __cpu_to_le32(3),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300362 },
363 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300364 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VI),
365 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
366 __cpu_to_le32(2),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300367 },
368 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300369 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_CONTROL),
370 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
371 __cpu_to_le32(3),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300372 },
373 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300374 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_CONTROL),
375 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
376 __cpu_to_le32(2),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300377 },
378 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300379 __cpu_to_le32(ATH10K_HTC_SVC_ID_RSVD_CTRL),
380 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
381 __cpu_to_le32(0),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300382 },
383 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300384 __cpu_to_le32(ATH10K_HTC_SVC_ID_RSVD_CTRL),
385 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
386 __cpu_to_le32(1),
387 },
388 { /* not used */
389 __cpu_to_le32(ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS),
390 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
391 __cpu_to_le32(0),
392 },
393 { /* not used */
394 __cpu_to_le32(ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS),
395 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
396 __cpu_to_le32(1),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300397 },
398 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300399 __cpu_to_le32(ATH10K_HTC_SVC_ID_HTT_DATA_MSG),
400 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
401 __cpu_to_le32(4),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300402 },
403 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300404 __cpu_to_le32(ATH10K_HTC_SVC_ID_HTT_DATA_MSG),
405 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
Rajkumar Manoharana70587b2015-10-12 18:27:04 +0530406 __cpu_to_le32(5),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300407 },
408
409 /* (Additions here) */
410
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300411 { /* must be last */
412 __cpu_to_le32(0),
413 __cpu_to_le32(0),
414 __cpu_to_le32(0),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300415 },
416};
417
Michal Kazior77258d42015-05-18 09:38:18 +0000418static bool ath10k_pci_is_awake(struct ath10k *ar)
419{
420 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
421 u32 val = ioread32(ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
422 RTC_STATE_ADDRESS);
423
424 return RTC_STATE_V_GET(val) == RTC_STATE_V_ON;
425}
426
427static void __ath10k_pci_wake(struct ath10k *ar)
428{
429 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
430
431 lockdep_assert_held(&ar_pci->ps_lock);
432
433 ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps wake reg refcount %lu awake %d\n",
434 ar_pci->ps_wake_refcount, ar_pci->ps_awake);
435
436 iowrite32(PCIE_SOC_WAKE_V_MASK,
437 ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
438 PCIE_SOC_WAKE_ADDRESS);
439}
440
441static void __ath10k_pci_sleep(struct ath10k *ar)
442{
443 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
444
445 lockdep_assert_held(&ar_pci->ps_lock);
446
447 ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps sleep reg refcount %lu awake %d\n",
448 ar_pci->ps_wake_refcount, ar_pci->ps_awake);
449
450 iowrite32(PCIE_SOC_WAKE_RESET,
451 ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
452 PCIE_SOC_WAKE_ADDRESS);
453 ar_pci->ps_awake = false;
454}
455
456static int ath10k_pci_wake_wait(struct ath10k *ar)
457{
458 int tot_delay = 0;
459 int curr_delay = 5;
460
461 while (tot_delay < PCIE_WAKE_TIMEOUT) {
Maharaja Kennadyrajan39b91b82015-10-06 15:19:28 +0300462 if (ath10k_pci_is_awake(ar)) {
463 if (tot_delay > PCIE_WAKE_LATE_US)
464 ath10k_warn(ar, "device wakeup took %d ms which is unusally long, otherwise it works normally.\n",
465 tot_delay / 1000);
Michal Kazior77258d42015-05-18 09:38:18 +0000466 return 0;
Maharaja Kennadyrajan39b91b82015-10-06 15:19:28 +0300467 }
Michal Kazior77258d42015-05-18 09:38:18 +0000468
469 udelay(curr_delay);
470 tot_delay += curr_delay;
471
472 if (curr_delay < 50)
473 curr_delay += 5;
474 }
475
476 return -ETIMEDOUT;
477}
478
Anilkumar Kolli1aaf8ef2015-10-16 15:54:51 +0300479static int ath10k_pci_force_wake(struct ath10k *ar)
480{
481 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
482 unsigned long flags;
483 int ret = 0;
484
485 spin_lock_irqsave(&ar_pci->ps_lock, flags);
486
487 if (!ar_pci->ps_awake) {
488 iowrite32(PCIE_SOC_WAKE_V_MASK,
489 ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
490 PCIE_SOC_WAKE_ADDRESS);
491
492 ret = ath10k_pci_wake_wait(ar);
493 if (ret == 0)
494 ar_pci->ps_awake = true;
495 }
496
497 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
498
499 return ret;
500}
501
502static void ath10k_pci_force_sleep(struct ath10k *ar)
503{
504 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
505 unsigned long flags;
506
507 spin_lock_irqsave(&ar_pci->ps_lock, flags);
508
509 iowrite32(PCIE_SOC_WAKE_RESET,
510 ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
511 PCIE_SOC_WAKE_ADDRESS);
512 ar_pci->ps_awake = false;
513
514 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
515}
516
Michal Kazior77258d42015-05-18 09:38:18 +0000517static int ath10k_pci_wake(struct ath10k *ar)
518{
519 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
520 unsigned long flags;
521 int ret = 0;
522
Anilkumar Kolli1aaf8ef2015-10-16 15:54:51 +0300523 if (ar_pci->pci_ps == 0)
524 return ret;
525
Michal Kazior77258d42015-05-18 09:38:18 +0000526 spin_lock_irqsave(&ar_pci->ps_lock, flags);
527
528 ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps wake refcount %lu awake %d\n",
529 ar_pci->ps_wake_refcount, ar_pci->ps_awake);
530
531 /* This function can be called very frequently. To avoid excessive
532 * CPU stalls for MMIO reads use a cache var to hold the device state.
533 */
534 if (!ar_pci->ps_awake) {
535 __ath10k_pci_wake(ar);
536
537 ret = ath10k_pci_wake_wait(ar);
538 if (ret == 0)
539 ar_pci->ps_awake = true;
540 }
541
542 if (ret == 0) {
543 ar_pci->ps_wake_refcount++;
544 WARN_ON(ar_pci->ps_wake_refcount == 0);
545 }
546
547 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
548
549 return ret;
550}
551
552static void ath10k_pci_sleep(struct ath10k *ar)
553{
554 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
555 unsigned long flags;
556
Anilkumar Kolli1aaf8ef2015-10-16 15:54:51 +0300557 if (ar_pci->pci_ps == 0)
558 return;
559
Michal Kazior77258d42015-05-18 09:38:18 +0000560 spin_lock_irqsave(&ar_pci->ps_lock, flags);
561
562 ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps sleep refcount %lu awake %d\n",
563 ar_pci->ps_wake_refcount, ar_pci->ps_awake);
564
565 if (WARN_ON(ar_pci->ps_wake_refcount == 0))
566 goto skip;
567
568 ar_pci->ps_wake_refcount--;
569
570 mod_timer(&ar_pci->ps_timer, jiffies +
571 msecs_to_jiffies(ATH10K_PCI_SLEEP_GRACE_PERIOD_MSEC));
572
573skip:
574 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
575}
576
577static void ath10k_pci_ps_timer(unsigned long ptr)
578{
579 struct ath10k *ar = (void *)ptr;
580 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
581 unsigned long flags;
582
583 spin_lock_irqsave(&ar_pci->ps_lock, flags);
584
585 ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps timer refcount %lu awake %d\n",
586 ar_pci->ps_wake_refcount, ar_pci->ps_awake);
587
588 if (ar_pci->ps_wake_refcount > 0)
589 goto skip;
590
591 __ath10k_pci_sleep(ar);
592
593skip:
594 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
595}
596
597static void ath10k_pci_sleep_sync(struct ath10k *ar)
598{
599 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
600 unsigned long flags;
601
Anilkumar Kolli1aaf8ef2015-10-16 15:54:51 +0300602 if (ar_pci->pci_ps == 0) {
603 ath10k_pci_force_sleep(ar);
604 return;
605 }
606
Michal Kazior77258d42015-05-18 09:38:18 +0000607 del_timer_sync(&ar_pci->ps_timer);
608
609 spin_lock_irqsave(&ar_pci->ps_lock, flags);
610 WARN_ON(ar_pci->ps_wake_refcount > 0);
611 __ath10k_pci_sleep(ar);
612 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
613}
614
615void ath10k_pci_write32(struct ath10k *ar, u32 offset, u32 value)
616{
617 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
618 int ret;
619
Michal Kazioraeae5b42015-06-15 14:46:42 +0300620 if (unlikely(offset + sizeof(value) > ar_pci->mem_len)) {
621 ath10k_warn(ar, "refusing to write mmio out of bounds at 0x%08x - 0x%08zx (max 0x%08zx)\n",
622 offset, offset + sizeof(value), ar_pci->mem_len);
623 return;
624 }
625
Michal Kazior77258d42015-05-18 09:38:18 +0000626 ret = ath10k_pci_wake(ar);
627 if (ret) {
628 ath10k_warn(ar, "failed to wake target for write32 of 0x%08x at 0x%08x: %d\n",
629 value, offset, ret);
630 return;
631 }
632
633 iowrite32(value, ar_pci->mem + offset);
634 ath10k_pci_sleep(ar);
635}
636
637u32 ath10k_pci_read32(struct ath10k *ar, u32 offset)
638{
639 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
640 u32 val;
641 int ret;
642
Michal Kazioraeae5b42015-06-15 14:46:42 +0300643 if (unlikely(offset + sizeof(val) > ar_pci->mem_len)) {
644 ath10k_warn(ar, "refusing to read mmio out of bounds at 0x%08x - 0x%08zx (max 0x%08zx)\n",
645 offset, offset + sizeof(val), ar_pci->mem_len);
646 return 0;
647 }
648
Michal Kazior77258d42015-05-18 09:38:18 +0000649 ret = ath10k_pci_wake(ar);
650 if (ret) {
651 ath10k_warn(ar, "failed to wake target for read32 at 0x%08x: %d\n",
652 offset, ret);
653 return 0xffffffff;
654 }
655
656 val = ioread32(ar_pci->mem + offset);
657 ath10k_pci_sleep(ar);
658
659 return val;
660}
661
662u32 ath10k_pci_soc_read32(struct ath10k *ar, u32 addr)
663{
664 return ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS + addr);
665}
666
667void ath10k_pci_soc_write32(struct ath10k *ar, u32 addr, u32 val)
668{
669 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + addr, val);
670}
671
672u32 ath10k_pci_reg_read32(struct ath10k *ar, u32 addr)
673{
674 return ath10k_pci_read32(ar, PCIE_LOCAL_BASE_ADDRESS + addr);
675}
676
677void ath10k_pci_reg_write32(struct ath10k *ar, u32 addr, u32 val)
678{
679 ath10k_pci_write32(ar, PCIE_LOCAL_BASE_ADDRESS + addr, val);
680}
681
Michal Kaziore5398872013-11-25 14:06:20 +0100682static bool ath10k_pci_irq_pending(struct ath10k *ar)
683{
684 u32 cause;
685
686 /* Check if the shared legacy irq is for us */
687 cause = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
688 PCIE_INTR_CAUSE_ADDRESS);
689 if (cause & (PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL))
690 return true;
691
692 return false;
693}
694
Michal Kazior26852182013-11-25 14:06:25 +0100695static void ath10k_pci_disable_and_clear_legacy_irq(struct ath10k *ar)
696{
697 /* IMPORTANT: INTR_CLR register has to be set after
698 * INTR_ENABLE is set to 0, otherwise interrupt can not be
699 * really cleared. */
700 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
701 0);
702 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_CLR_ADDRESS,
703 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
704
705 /* IMPORTANT: this extra read transaction is required to
706 * flush the posted write buffer. */
Kalle Valocfbc06a2014-09-14 12:50:23 +0300707 (void)ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
708 PCIE_INTR_ENABLE_ADDRESS);
Michal Kazior26852182013-11-25 14:06:25 +0100709}
710
711static void ath10k_pci_enable_legacy_irq(struct ath10k *ar)
712{
713 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
714 PCIE_INTR_ENABLE_ADDRESS,
715 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
716
717 /* IMPORTANT: this extra read transaction is required to
718 * flush the posted write buffer. */
Kalle Valocfbc06a2014-09-14 12:50:23 +0300719 (void)ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
720 PCIE_INTR_ENABLE_ADDRESS);
Michal Kazior26852182013-11-25 14:06:25 +0100721}
722
Michal Kazior403d6272014-08-22 14:23:31 +0200723static inline const char *ath10k_pci_get_irq_method(struct ath10k *ar)
Michal Kaziorab977bd2013-11-25 14:06:26 +0100724{
Michal Kaziorab977bd2013-11-25 14:06:26 +0100725 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
726
Michal Kazior403d6272014-08-22 14:23:31 +0200727 if (ar_pci->num_msi_intrs > 1)
728 return "msi-x";
Kalle Valod8bb26b2014-09-14 12:50:33 +0300729
730 if (ar_pci->num_msi_intrs == 1)
Michal Kazior403d6272014-08-22 14:23:31 +0200731 return "msi";
Kalle Valod8bb26b2014-09-14 12:50:33 +0300732
733 return "legacy";
Michal Kaziorab977bd2013-11-25 14:06:26 +0100734}
735
Michal Kazior728f95e2014-08-22 14:33:14 +0200736static int __ath10k_pci_rx_post_buf(struct ath10k_pci_pipe *pipe)
Michal Kaziorab977bd2013-11-25 14:06:26 +0100737{
Michal Kazior728f95e2014-08-22 14:33:14 +0200738 struct ath10k *ar = pipe->hif_ce_state;
Michal Kaziorab977bd2013-11-25 14:06:26 +0100739 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior728f95e2014-08-22 14:33:14 +0200740 struct ath10k_ce_pipe *ce_pipe = pipe->ce_hdl;
741 struct sk_buff *skb;
742 dma_addr_t paddr;
Michal Kaziorab977bd2013-11-25 14:06:26 +0100743 int ret;
744
Michal Kazior728f95e2014-08-22 14:33:14 +0200745 skb = dev_alloc_skb(pipe->buf_sz);
746 if (!skb)
747 return -ENOMEM;
748
749 WARN_ONCE((unsigned long)skb->data & 3, "unaligned skb");
750
751 paddr = dma_map_single(ar->dev, skb->data,
752 skb->len + skb_tailroom(skb),
753 DMA_FROM_DEVICE);
754 if (unlikely(dma_mapping_error(ar->dev, paddr))) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200755 ath10k_warn(ar, "failed to dma map pci rx buf\n");
Michal Kazior728f95e2014-08-22 14:33:14 +0200756 dev_kfree_skb_any(skb);
757 return -EIO;
758 }
759
Michal Kazior8582bf32015-01-24 12:14:47 +0200760 ATH10K_SKB_RXCB(skb)->paddr = paddr;
Michal Kazior728f95e2014-08-22 14:33:14 +0200761
Rajkumar Manoharanab4e3db2015-10-06 15:19:33 +0300762 spin_lock_bh(&ar_pci->ce_lock);
Michal Kazior728f95e2014-08-22 14:33:14 +0200763 ret = __ath10k_ce_rx_post_buf(ce_pipe, skb, paddr);
Rajkumar Manoharanab4e3db2015-10-06 15:19:33 +0300764 spin_unlock_bh(&ar_pci->ce_lock);
Michal Kaziorab977bd2013-11-25 14:06:26 +0100765 if (ret) {
Michal Kazior728f95e2014-08-22 14:33:14 +0200766 dma_unmap_single(ar->dev, paddr, skb->len + skb_tailroom(skb),
767 DMA_FROM_DEVICE);
768 dev_kfree_skb_any(skb);
Michal Kaziorab977bd2013-11-25 14:06:26 +0100769 return ret;
770 }
771
772 return 0;
773}
774
Rajkumar Manoharanab4e3db2015-10-06 15:19:33 +0300775static void ath10k_pci_rx_post_pipe(struct ath10k_pci_pipe *pipe)
Michal Kaziorab977bd2013-11-25 14:06:26 +0100776{
Michal Kazior728f95e2014-08-22 14:33:14 +0200777 struct ath10k *ar = pipe->hif_ce_state;
778 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
779 struct ath10k_ce_pipe *ce_pipe = pipe->ce_hdl;
780 int ret, num;
781
Michal Kazior728f95e2014-08-22 14:33:14 +0200782 if (pipe->buf_sz == 0)
783 return;
784
785 if (!ce_pipe->dest_ring)
786 return;
787
Rajkumar Manoharanab4e3db2015-10-06 15:19:33 +0300788 spin_lock_bh(&ar_pci->ce_lock);
Michal Kazior728f95e2014-08-22 14:33:14 +0200789 num = __ath10k_ce_rx_num_free_bufs(ce_pipe);
Rajkumar Manoharanab4e3db2015-10-06 15:19:33 +0300790 spin_unlock_bh(&ar_pci->ce_lock);
Michal Kazior728f95e2014-08-22 14:33:14 +0200791 while (num--) {
792 ret = __ath10k_pci_rx_post_buf(pipe);
793 if (ret) {
Rajkumar Manoharanab4e3db2015-10-06 15:19:33 +0300794 if (ret == -ENOSPC)
795 break;
Michal Kazior7aa7a722014-08-25 12:09:38 +0200796 ath10k_warn(ar, "failed to post pci rx buf: %d\n", ret);
Michal Kazior728f95e2014-08-22 14:33:14 +0200797 mod_timer(&ar_pci->rx_post_retry, jiffies +
798 ATH10K_PCI_RX_POST_RETRY_MS);
799 break;
800 }
801 }
802}
803
Michal Kazior728f95e2014-08-22 14:33:14 +0200804static void ath10k_pci_rx_post(struct ath10k *ar)
805{
806 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
807 int i;
808
Michal Kazior728f95e2014-08-22 14:33:14 +0200809 for (i = 0; i < CE_COUNT; i++)
Rajkumar Manoharanab4e3db2015-10-06 15:19:33 +0300810 ath10k_pci_rx_post_pipe(&ar_pci->pipe_info[i]);
Michal Kazior728f95e2014-08-22 14:33:14 +0200811}
812
813static void ath10k_pci_rx_replenish_retry(unsigned long ptr)
814{
815 struct ath10k *ar = (void *)ptr;
816
817 ath10k_pci_rx_post(ar);
Michal Kaziorab977bd2013-11-25 14:06:26 +0100818}
819
Vasanthakumar Thiagarajan418ca592015-06-18 12:31:05 +0530820static u32 ath10k_pci_targ_cpu_to_ce_addr(struct ath10k *ar, u32 addr)
821{
822 u32 val = 0;
823
824 switch (ar->hw_rev) {
825 case ATH10K_HW_QCA988X:
826 case ATH10K_HW_QCA6174:
Bartosz Markowskia226b512015-10-28 15:09:53 +0100827 case ATH10K_HW_QCA9377:
Vasanthakumar Thiagarajan418ca592015-06-18 12:31:05 +0530828 val = (ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
829 CORE_CTRL_ADDRESS) &
Vasanthakumar Thiagarajan3c7e2562015-07-03 19:25:27 +0530830 0x7ff) << 21;
Vasanthakumar Thiagarajan418ca592015-06-18 12:31:05 +0530831 break;
832 case ATH10K_HW_QCA99X0:
833 val = ath10k_pci_read32(ar, PCIE_BAR_REG_ADDRESS);
834 break;
835 }
836
837 val |= 0x100000 | (addr & 0xfffff);
838 return val;
839}
840
Kalle Valo5e3dd152013-06-12 20:52:10 +0300841/*
842 * Diagnostic read/write access is provided for startup/config/debug usage.
843 * Caller must guarantee proper alignment, when applicable, and single user
844 * at any moment.
845 */
846static int ath10k_pci_diag_read_mem(struct ath10k *ar, u32 address, void *data,
847 int nbytes)
848{
849 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
850 int ret = 0;
851 u32 buf;
852 unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
853 unsigned int id;
854 unsigned int flags;
Michal Kazior2aa39112013-08-27 13:08:02 +0200855 struct ath10k_ce_pipe *ce_diag;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300856 /* Host buffer address in CE space */
857 u32 ce_data;
858 dma_addr_t ce_data_base = 0;
859 void *data_buf = NULL;
860 int i;
861
Kalle Valoeef25402014-09-24 14:16:52 +0300862 spin_lock_bh(&ar_pci->ce_lock);
863
Kalle Valo5e3dd152013-06-12 20:52:10 +0300864 ce_diag = ar_pci->ce_diag;
865
866 /*
867 * Allocate a temporary bounce buffer to hold caller's data
868 * to be DMA'ed from Target. This guarantees
869 * 1) 4-byte alignment
870 * 2) Buffer in DMA-able space
871 */
872 orig_nbytes = nbytes;
Michal Kazior68c03242014-03-28 10:02:35 +0200873 data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
874 orig_nbytes,
875 &ce_data_base,
876 GFP_ATOMIC);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300877
878 if (!data_buf) {
879 ret = -ENOMEM;
880 goto done;
881 }
882 memset(data_buf, 0, orig_nbytes);
883
884 remaining_bytes = orig_nbytes;
885 ce_data = ce_data_base;
886 while (remaining_bytes) {
887 nbytes = min_t(unsigned int, remaining_bytes,
888 DIAG_TRANSFER_LIMIT);
889
Kalle Valoeef25402014-09-24 14:16:52 +0300890 ret = __ath10k_ce_rx_post_buf(ce_diag, NULL, ce_data);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300891 if (ret != 0)
892 goto done;
893
894 /* Request CE to send from Target(!) address to Host buffer */
895 /*
896 * The address supplied by the caller is in the
897 * Target CPU virtual address space.
898 *
899 * In order to use this address with the diagnostic CE,
900 * convert it from Target CPU virtual address space
901 * to CE address space
902 */
Vasanthakumar Thiagarajan418ca592015-06-18 12:31:05 +0530903 address = ath10k_pci_targ_cpu_to_ce_addr(ar, address);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300904
Kalle Valoeef25402014-09-24 14:16:52 +0300905 ret = ath10k_ce_send_nolock(ce_diag, NULL, (u32)address, nbytes, 0,
906 0);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300907 if (ret)
908 goto done;
909
910 i = 0;
Rajkumar Manoharan765952e2015-10-23 18:01:05 +0530911 while (ath10k_ce_completed_send_next_nolock(ce_diag,
912 NULL) != 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300913 mdelay(1);
914 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
915 ret = -EBUSY;
916 goto done;
917 }
918 }
919
Kalle Valo5e3dd152013-06-12 20:52:10 +0300920 i = 0;
Kalle Valoeef25402014-09-24 14:16:52 +0300921 while (ath10k_ce_completed_recv_next_nolock(ce_diag, NULL, &buf,
922 &completed_nbytes,
923 &id, &flags) != 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300924 mdelay(1);
925
926 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
927 ret = -EBUSY;
928 goto done;
929 }
930 }
931
932 if (nbytes != completed_nbytes) {
933 ret = -EIO;
934 goto done;
935 }
936
937 if (buf != ce_data) {
938 ret = -EIO;
939 goto done;
940 }
941
942 remaining_bytes -= nbytes;
943 address += nbytes;
944 ce_data += nbytes;
945 }
946
947done:
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300948 if (ret == 0)
949 memcpy(data, data_buf, orig_nbytes);
950 else
Michal Kazior7aa7a722014-08-25 12:09:38 +0200951 ath10k_warn(ar, "failed to read diag value at 0x%x: %d\n",
Kalle Valo50f87a62014-03-28 09:32:52 +0200952 address, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300953
954 if (data_buf)
Michal Kazior68c03242014-03-28 10:02:35 +0200955 dma_free_coherent(ar->dev, orig_nbytes, data_buf,
956 ce_data_base);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300957
Kalle Valoeef25402014-09-24 14:16:52 +0300958 spin_unlock_bh(&ar_pci->ce_lock);
959
Kalle Valo5e3dd152013-06-12 20:52:10 +0300960 return ret;
961}
962
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300963static int ath10k_pci_diag_read32(struct ath10k *ar, u32 address, u32 *value)
964{
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300965 __le32 val = 0;
966 int ret;
967
968 ret = ath10k_pci_diag_read_mem(ar, address, &val, sizeof(val));
969 *value = __le32_to_cpu(val);
970
971 return ret;
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300972}
973
974static int __ath10k_pci_diag_read_hi(struct ath10k *ar, void *dest,
975 u32 src, u32 len)
976{
977 u32 host_addr, addr;
978 int ret;
979
980 host_addr = host_interest_item_address(src);
981
982 ret = ath10k_pci_diag_read32(ar, host_addr, &addr);
983 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200984 ath10k_warn(ar, "failed to get memcpy hi address for firmware address %d: %d\n",
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300985 src, ret);
986 return ret;
987 }
988
989 ret = ath10k_pci_diag_read_mem(ar, addr, dest, len);
990 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200991 ath10k_warn(ar, "failed to memcpy firmware memory from %d (%d B): %d\n",
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300992 addr, len, ret);
993 return ret;
994 }
995
996 return 0;
997}
998
999#define ath10k_pci_diag_read_hi(ar, dest, src, len) \
Kalle Valo8cc7f262014-09-14 12:50:39 +03001000 __ath10k_pci_diag_read_hi(ar, dest, HI_ITEM(src), len)
Kalle Valo3d29a3e2014-08-25 08:37:26 +03001001
Kalle Valo5e3dd152013-06-12 20:52:10 +03001002static int ath10k_pci_diag_write_mem(struct ath10k *ar, u32 address,
1003 const void *data, int nbytes)
1004{
1005 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1006 int ret = 0;
1007 u32 buf;
1008 unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
1009 unsigned int id;
1010 unsigned int flags;
Michal Kazior2aa39112013-08-27 13:08:02 +02001011 struct ath10k_ce_pipe *ce_diag;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001012 void *data_buf = NULL;
1013 u32 ce_data; /* Host buffer address in CE space */
1014 dma_addr_t ce_data_base = 0;
1015 int i;
1016
Kalle Valoeef25402014-09-24 14:16:52 +03001017 spin_lock_bh(&ar_pci->ce_lock);
1018
Kalle Valo5e3dd152013-06-12 20:52:10 +03001019 ce_diag = ar_pci->ce_diag;
1020
1021 /*
1022 * Allocate a temporary bounce buffer to hold caller's data
1023 * to be DMA'ed to Target. This guarantees
1024 * 1) 4-byte alignment
1025 * 2) Buffer in DMA-able space
1026 */
1027 orig_nbytes = nbytes;
Michal Kazior68c03242014-03-28 10:02:35 +02001028 data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
1029 orig_nbytes,
1030 &ce_data_base,
1031 GFP_ATOMIC);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001032 if (!data_buf) {
1033 ret = -ENOMEM;
1034 goto done;
1035 }
1036
1037 /* Copy caller's data to allocated DMA buf */
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001038 memcpy(data_buf, data, orig_nbytes);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001039
1040 /*
1041 * The address supplied by the caller is in the
1042 * Target CPU virtual address space.
1043 *
1044 * In order to use this address with the diagnostic CE,
1045 * convert it from
1046 * Target CPU virtual address space
1047 * to
1048 * CE address space
1049 */
Vasanthakumar Thiagarajan418ca592015-06-18 12:31:05 +05301050 address = ath10k_pci_targ_cpu_to_ce_addr(ar, address);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001051
1052 remaining_bytes = orig_nbytes;
1053 ce_data = ce_data_base;
1054 while (remaining_bytes) {
1055 /* FIXME: check cast */
1056 nbytes = min_t(int, remaining_bytes, DIAG_TRANSFER_LIMIT);
1057
1058 /* Set up to receive directly into Target(!) address */
Kalle Valoeef25402014-09-24 14:16:52 +03001059 ret = __ath10k_ce_rx_post_buf(ce_diag, NULL, address);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001060 if (ret != 0)
1061 goto done;
1062
1063 /*
1064 * Request CE to send caller-supplied data that
1065 * was copied to bounce buffer to Target(!) address.
1066 */
Kalle Valoeef25402014-09-24 14:16:52 +03001067 ret = ath10k_ce_send_nolock(ce_diag, NULL, (u32)ce_data,
1068 nbytes, 0, 0);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001069 if (ret != 0)
1070 goto done;
1071
1072 i = 0;
Rajkumar Manoharan765952e2015-10-23 18:01:05 +05301073 while (ath10k_ce_completed_send_next_nolock(ce_diag,
1074 NULL) != 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001075 mdelay(1);
1076
1077 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
1078 ret = -EBUSY;
1079 goto done;
1080 }
1081 }
1082
Kalle Valo5e3dd152013-06-12 20:52:10 +03001083 i = 0;
Kalle Valoeef25402014-09-24 14:16:52 +03001084 while (ath10k_ce_completed_recv_next_nolock(ce_diag, NULL, &buf,
1085 &completed_nbytes,
1086 &id, &flags) != 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001087 mdelay(1);
1088
1089 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
1090 ret = -EBUSY;
1091 goto done;
1092 }
1093 }
1094
1095 if (nbytes != completed_nbytes) {
1096 ret = -EIO;
1097 goto done;
1098 }
1099
1100 if (buf != address) {
1101 ret = -EIO;
1102 goto done;
1103 }
1104
1105 remaining_bytes -= nbytes;
1106 address += nbytes;
1107 ce_data += nbytes;
1108 }
1109
1110done:
1111 if (data_buf) {
Michal Kazior68c03242014-03-28 10:02:35 +02001112 dma_free_coherent(ar->dev, orig_nbytes, data_buf,
1113 ce_data_base);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001114 }
1115
1116 if (ret != 0)
Michal Kazior7aa7a722014-08-25 12:09:38 +02001117 ath10k_warn(ar, "failed to write diag value at 0x%x: %d\n",
Kalle Valo50f87a62014-03-28 09:32:52 +02001118 address, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001119
Kalle Valoeef25402014-09-24 14:16:52 +03001120 spin_unlock_bh(&ar_pci->ce_lock);
1121
Kalle Valo5e3dd152013-06-12 20:52:10 +03001122 return ret;
1123}
1124
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001125static int ath10k_pci_diag_write32(struct ath10k *ar, u32 address, u32 value)
1126{
1127 __le32 val = __cpu_to_le32(value);
1128
1129 return ath10k_pci_diag_write_mem(ar, address, &val, sizeof(val));
1130}
1131
Kalle Valo5e3dd152013-06-12 20:52:10 +03001132/* Called by lower (CE) layer when a send to Target completes. */
Rajkumar Manoharan0e5b2952015-10-12 18:27:01 +05301133static void ath10k_pci_htc_tx_cb(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001134{
1135 struct ath10k *ar = ce_state->ar;
Michal Kazior1cb86d42014-11-27 11:09:38 +01001136 struct sk_buff_head list;
1137 struct sk_buff *skb;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001138
Michal Kazior1cb86d42014-11-27 11:09:38 +01001139 __skb_queue_head_init(&list);
Rajkumar Manoharan765952e2015-10-23 18:01:05 +05301140 while (ath10k_ce_completed_send_next(ce_state, (void **)&skb) == 0) {
Michal Kaziora16942e2014-02-27 18:50:04 +02001141 /* no need to call tx completion for NULL pointers */
Michal Kazior1cb86d42014-11-27 11:09:38 +01001142 if (skb == NULL)
Michal Kazior726346f2014-02-27 18:50:04 +02001143 continue;
1144
Michal Kazior1cb86d42014-11-27 11:09:38 +01001145 __skb_queue_tail(&list, skb);
Michal Kazior5440ce22013-09-03 15:09:58 +02001146 }
Michal Kazior1cb86d42014-11-27 11:09:38 +01001147
1148 while ((skb = __skb_dequeue(&list)))
Rajkumar Manoharan0e5b2952015-10-12 18:27:01 +05301149 ath10k_htc_tx_completion_handler(ar, skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001150}
1151
Rajkumar Manoharana70587b2015-10-12 18:27:04 +05301152static void ath10k_pci_process_rx_cb(struct ath10k_ce_pipe *ce_state,
1153 void (*callback)(struct ath10k *ar,
1154 struct sk_buff *skb))
Kalle Valo5e3dd152013-06-12 20:52:10 +03001155{
1156 struct ath10k *ar = ce_state->ar;
1157 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +02001158 struct ath10k_pci_pipe *pipe_info = &ar_pci->pipe_info[ce_state->id];
Kalle Valo5e3dd152013-06-12 20:52:10 +03001159 struct sk_buff *skb;
Michal Kazior1cb86d42014-11-27 11:09:38 +01001160 struct sk_buff_head list;
Michal Kazior5440ce22013-09-03 15:09:58 +02001161 void *transfer_context;
1162 u32 ce_data;
Michal Kazior2f5280d2014-02-27 18:50:05 +02001163 unsigned int nbytes, max_nbytes;
Michal Kazior5440ce22013-09-03 15:09:58 +02001164 unsigned int transfer_id;
1165 unsigned int flags;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001166
Michal Kazior1cb86d42014-11-27 11:09:38 +01001167 __skb_queue_head_init(&list);
Michal Kazior5440ce22013-09-03 15:09:58 +02001168 while (ath10k_ce_completed_recv_next(ce_state, &transfer_context,
1169 &ce_data, &nbytes, &transfer_id,
1170 &flags) == 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001171 skb = transfer_context;
Michal Kazior2f5280d2014-02-27 18:50:05 +02001172 max_nbytes = skb->len + skb_tailroom(skb);
Michal Kazior8582bf32015-01-24 12:14:47 +02001173 dma_unmap_single(ar->dev, ATH10K_SKB_RXCB(skb)->paddr,
Michal Kazior2f5280d2014-02-27 18:50:05 +02001174 max_nbytes, DMA_FROM_DEVICE);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001175
Michal Kazior2f5280d2014-02-27 18:50:05 +02001176 if (unlikely(max_nbytes < nbytes)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001177 ath10k_warn(ar, "rxed more than expected (nbytes %d, max %d)",
Michal Kazior2f5280d2014-02-27 18:50:05 +02001178 nbytes, max_nbytes);
1179 dev_kfree_skb_any(skb);
1180 continue;
1181 }
1182
1183 skb_put(skb, nbytes);
Michal Kazior1cb86d42014-11-27 11:09:38 +01001184 __skb_queue_tail(&list, skb);
1185 }
Michal Kaziora360e542014-09-23 10:22:54 +02001186
Michal Kazior1cb86d42014-11-27 11:09:38 +01001187 while ((skb = __skb_dequeue(&list))) {
Michal Kaziora360e542014-09-23 10:22:54 +02001188 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci rx ce pipe %d len %d\n",
1189 ce_state->id, skb->len);
1190 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci rx: ",
1191 skb->data, skb->len);
1192
Rajkumar Manoharana70587b2015-10-12 18:27:04 +05301193 callback(ar, skb);
Michal Kazior2f5280d2014-02-27 18:50:05 +02001194 }
Michal Kaziorc29a3802014-07-21 21:03:10 +03001195
Michal Kazior728f95e2014-08-22 14:33:14 +02001196 ath10k_pci_rx_post_pipe(pipe_info);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001197}
1198
Rajkumar Manoharana70587b2015-10-12 18:27:04 +05301199/* Called by lower (CE) layer when data is received from the Target. */
1200static void ath10k_pci_htc_rx_cb(struct ath10k_ce_pipe *ce_state)
1201{
1202 ath10k_pci_process_rx_cb(ce_state, ath10k_htc_rx_completion_handler);
1203}
1204
1205/* Called by lower (CE) layer when a send to HTT Target completes. */
1206static void ath10k_pci_htt_tx_cb(struct ath10k_ce_pipe *ce_state)
1207{
1208 struct ath10k *ar = ce_state->ar;
1209 struct sk_buff *skb;
Rajkumar Manoharana70587b2015-10-12 18:27:04 +05301210
Rajkumar Manoharan765952e2015-10-23 18:01:05 +05301211 while (ath10k_ce_completed_send_next(ce_state, (void **)&skb) == 0) {
Rajkumar Manoharana70587b2015-10-12 18:27:04 +05301212 /* no need to call tx completion for NULL pointers */
1213 if (!skb)
1214 continue;
1215
1216 dma_unmap_single(ar->dev, ATH10K_SKB_CB(skb)->paddr,
1217 skb->len, DMA_TO_DEVICE);
1218 ath10k_htt_hif_tx_complete(ar, skb);
1219 }
1220}
1221
1222static void ath10k_pci_htt_rx_deliver(struct ath10k *ar, struct sk_buff *skb)
1223{
1224 skb_pull(skb, sizeof(struct ath10k_htc_hdr));
1225 ath10k_htt_t2h_msg_handler(ar, skb);
1226}
1227
1228/* Called by lower (CE) layer when HTT data is received from the Target. */
1229static void ath10k_pci_htt_rx_cb(struct ath10k_ce_pipe *ce_state)
1230{
1231 /* CE4 polling needs to be done whenever CE pipe which transports
1232 * HTT Rx (target->host) is processed.
1233 */
1234 ath10k_ce_per_engine_service(ce_state->ar, 4);
1235
1236 ath10k_pci_process_rx_cb(ce_state, ath10k_pci_htt_rx_deliver);
1237}
1238
Michal Kazior726346f2014-02-27 18:50:04 +02001239static int ath10k_pci_hif_tx_sg(struct ath10k *ar, u8 pipe_id,
1240 struct ath10k_hif_sg_item *items, int n_items)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001241{
Kalle Valo5e3dd152013-06-12 20:52:10 +03001242 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior726346f2014-02-27 18:50:04 +02001243 struct ath10k_pci_pipe *pci_pipe = &ar_pci->pipe_info[pipe_id];
1244 struct ath10k_ce_pipe *ce_pipe = pci_pipe->ce_hdl;
1245 struct ath10k_ce_ring *src_ring = ce_pipe->src_ring;
Michal Kazior7147a132014-05-26 12:02:58 +02001246 unsigned int nentries_mask;
1247 unsigned int sw_index;
1248 unsigned int write_index;
Michal Kazior08b8aa02014-05-26 12:02:59 +02001249 int err, i = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001250
Michal Kazior726346f2014-02-27 18:50:04 +02001251 spin_lock_bh(&ar_pci->ce_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001252
Michal Kazior7147a132014-05-26 12:02:58 +02001253 nentries_mask = src_ring->nentries_mask;
1254 sw_index = src_ring->sw_index;
1255 write_index = src_ring->write_index;
1256
Michal Kazior726346f2014-02-27 18:50:04 +02001257 if (unlikely(CE_RING_DELTA(nentries_mask,
1258 write_index, sw_index - 1) < n_items)) {
1259 err = -ENOBUFS;
Michal Kazior08b8aa02014-05-26 12:02:59 +02001260 goto err;
Michal Kazior726346f2014-02-27 18:50:04 +02001261 }
1262
1263 for (i = 0; i < n_items - 1; i++) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001264 ath10k_dbg(ar, ATH10K_DBG_PCI,
Michal Kazior726346f2014-02-27 18:50:04 +02001265 "pci tx item %d paddr 0x%08x len %d n_items %d\n",
1266 i, items[i].paddr, items[i].len, n_items);
Michal Kazior7aa7a722014-08-25 12:09:38 +02001267 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci tx data: ",
Michal Kazior726346f2014-02-27 18:50:04 +02001268 items[i].vaddr, items[i].len);
1269
1270 err = ath10k_ce_send_nolock(ce_pipe,
1271 items[i].transfer_context,
1272 items[i].paddr,
1273 items[i].len,
1274 items[i].transfer_id,
1275 CE_SEND_FLAG_GATHER);
1276 if (err)
Michal Kazior08b8aa02014-05-26 12:02:59 +02001277 goto err;
Michal Kazior726346f2014-02-27 18:50:04 +02001278 }
1279
1280 /* `i` is equal to `n_items -1` after for() */
Kalle Valo5e3dd152013-06-12 20:52:10 +03001281
Michal Kazior7aa7a722014-08-25 12:09:38 +02001282 ath10k_dbg(ar, ATH10K_DBG_PCI,
Michal Kazior726346f2014-02-27 18:50:04 +02001283 "pci tx item %d paddr 0x%08x len %d n_items %d\n",
1284 i, items[i].paddr, items[i].len, n_items);
Michal Kazior7aa7a722014-08-25 12:09:38 +02001285 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci tx data: ",
Michal Kazior726346f2014-02-27 18:50:04 +02001286 items[i].vaddr, items[i].len);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001287
Michal Kazior726346f2014-02-27 18:50:04 +02001288 err = ath10k_ce_send_nolock(ce_pipe,
1289 items[i].transfer_context,
1290 items[i].paddr,
1291 items[i].len,
1292 items[i].transfer_id,
1293 0);
1294 if (err)
Michal Kazior08b8aa02014-05-26 12:02:59 +02001295 goto err;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001296
Michal Kazior08b8aa02014-05-26 12:02:59 +02001297 spin_unlock_bh(&ar_pci->ce_lock);
1298 return 0;
1299
1300err:
1301 for (; i > 0; i--)
1302 __ath10k_ce_send_revert(ce_pipe);
1303
Michal Kazior726346f2014-02-27 18:50:04 +02001304 spin_unlock_bh(&ar_pci->ce_lock);
1305 return err;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001306}
1307
Kalle Valoeef25402014-09-24 14:16:52 +03001308static int ath10k_pci_hif_diag_read(struct ath10k *ar, u32 address, void *buf,
1309 size_t buf_len)
1310{
1311 return ath10k_pci_diag_read_mem(ar, address, buf, buf_len);
1312}
1313
Kalle Valo5e3dd152013-06-12 20:52:10 +03001314static u16 ath10k_pci_hif_get_free_queue_number(struct ath10k *ar, u8 pipe)
1315{
1316 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo50f87a62014-03-28 09:32:52 +02001317
Michal Kazior7aa7a722014-08-25 12:09:38 +02001318 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif get free queue number\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001319
Michal Kazior3efcb3b2013-10-02 11:03:41 +02001320 return ath10k_ce_num_free_src_entries(ar_pci->pipe_info[pipe].ce_hdl);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001321}
1322
Ben Greear384914b2014-08-25 08:37:32 +03001323static void ath10k_pci_dump_registers(struct ath10k *ar,
1324 struct ath10k_fw_crash_data *crash_data)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001325{
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001326 __le32 reg_dump_values[REG_DUMP_COUNT_QCA988X] = {};
1327 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001328
Ben Greear384914b2014-08-25 08:37:32 +03001329 lockdep_assert_held(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001330
Kalle Valo3d29a3e2014-08-25 08:37:26 +03001331 ret = ath10k_pci_diag_read_hi(ar, &reg_dump_values[0],
1332 hi_failure_state,
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001333 REG_DUMP_COUNT_QCA988X * sizeof(__le32));
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001334 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001335 ath10k_err(ar, "failed to read firmware dump area: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001336 return;
1337 }
1338
1339 BUILD_BUG_ON(REG_DUMP_COUNT_QCA988X % 4);
1340
Michal Kazior7aa7a722014-08-25 12:09:38 +02001341 ath10k_err(ar, "firmware register dump:\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001342 for (i = 0; i < REG_DUMP_COUNT_QCA988X; i += 4)
Michal Kazior7aa7a722014-08-25 12:09:38 +02001343 ath10k_err(ar, "[%02d]: 0x%08X 0x%08X 0x%08X 0x%08X\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001344 i,
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001345 __le32_to_cpu(reg_dump_values[i]),
1346 __le32_to_cpu(reg_dump_values[i + 1]),
1347 __le32_to_cpu(reg_dump_values[i + 2]),
1348 __le32_to_cpu(reg_dump_values[i + 3]));
Michal Kazioraffd3212013-07-16 09:54:35 +02001349
Michal Kazior1bbb1192014-08-25 12:13:14 +02001350 if (!crash_data)
1351 return;
1352
Ben Greear384914b2014-08-25 08:37:32 +03001353 for (i = 0; i < REG_DUMP_COUNT_QCA988X; i++)
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001354 crash_data->registers[i] = reg_dump_values[i];
Ben Greear384914b2014-08-25 08:37:32 +03001355}
1356
Kalle Valo0e9848c2014-08-25 08:37:37 +03001357static void ath10k_pci_fw_crashed_dump(struct ath10k *ar)
Ben Greear384914b2014-08-25 08:37:32 +03001358{
1359 struct ath10k_fw_crash_data *crash_data;
1360 char uuid[50];
1361
1362 spin_lock_bh(&ar->data_lock);
1363
Ben Greearf51dbe72014-09-29 14:41:46 +03001364 ar->stats.fw_crash_counter++;
1365
Ben Greear384914b2014-08-25 08:37:32 +03001366 crash_data = ath10k_debug_get_new_fw_crash_data(ar);
1367
1368 if (crash_data)
1369 scnprintf(uuid, sizeof(uuid), "%pUl", &crash_data->uuid);
1370 else
1371 scnprintf(uuid, sizeof(uuid), "n/a");
1372
Michal Kazior7aa7a722014-08-25 12:09:38 +02001373 ath10k_err(ar, "firmware crashed! (uuid %s)\n", uuid);
Kalle Valo8a0c7972014-08-25 08:37:45 +03001374 ath10k_print_driver_info(ar);
Ben Greear384914b2014-08-25 08:37:32 +03001375 ath10k_pci_dump_registers(ar, crash_data);
1376
Ben Greear384914b2014-08-25 08:37:32 +03001377 spin_unlock_bh(&ar->data_lock);
Michal Kazioraffd3212013-07-16 09:54:35 +02001378
Michal Kazior5e90de82013-10-16 16:46:05 +03001379 queue_work(ar->workqueue, &ar->restart_work);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001380}
1381
1382static void ath10k_pci_hif_send_complete_check(struct ath10k *ar, u8 pipe,
1383 int force)
1384{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001385 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif send complete check\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001386
Kalle Valo5e3dd152013-06-12 20:52:10 +03001387 if (!force) {
1388 int resources;
1389 /*
1390 * Decide whether to actually poll for completions, or just
1391 * wait for a later chance.
1392 * If there seem to be plenty of resources left, then just wait
1393 * since checking involves reading a CE register, which is a
1394 * relatively expensive operation.
1395 */
1396 resources = ath10k_pci_hif_get_free_queue_number(ar, pipe);
1397
1398 /*
1399 * If at least 50% of the total resources are still available,
1400 * don't bother checking again yet.
1401 */
1402 if (resources > (host_ce_config_wlan[pipe].src_nentries >> 1))
1403 return;
1404 }
1405 ath10k_ce_per_engine_service(ar, pipe);
1406}
1407
Michal Kazior96a9d0d2013-11-08 08:01:25 +01001408static void ath10k_pci_kill_tasklet(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001409{
1410 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001411 int i;
1412
Kalle Valo5e3dd152013-06-12 20:52:10 +03001413 tasklet_kill(&ar_pci->intr_tq);
Michal Kazior103d4f52013-11-08 08:01:24 +01001414 tasklet_kill(&ar_pci->msi_fw_err);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001415
1416 for (i = 0; i < CE_COUNT; i++)
1417 tasklet_kill(&ar_pci->pipe_info[i].intr);
Michal Kazior728f95e2014-08-22 14:33:14 +02001418
1419 del_timer_sync(&ar_pci->rx_post_retry);
Michal Kazior96a9d0d2013-11-08 08:01:25 +01001420}
1421
Rajkumar Manoharan400143e2015-10-12 18:27:06 +05301422static int ath10k_pci_hif_map_service_to_pipe(struct ath10k *ar, u16 service_id,
1423 u8 *ul_pipe, u8 *dl_pipe)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001424{
Michal Kazior7c6aa252014-08-26 19:14:03 +03001425 const struct service_to_pipe *entry;
1426 bool ul_set = false, dl_set = false;
1427 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001428
Michal Kazior7aa7a722014-08-25 12:09:38 +02001429 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif map service\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001430
Michal Kazior7c6aa252014-08-26 19:14:03 +03001431 for (i = 0; i < ARRAY_SIZE(target_service_to_ce_map_wlan); i++) {
1432 entry = &target_service_to_ce_map_wlan[i];
Kalle Valo5e3dd152013-06-12 20:52:10 +03001433
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001434 if (__le32_to_cpu(entry->service_id) != service_id)
Michal Kazior7c6aa252014-08-26 19:14:03 +03001435 continue;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001436
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001437 switch (__le32_to_cpu(entry->pipedir)) {
Michal Kazior7c6aa252014-08-26 19:14:03 +03001438 case PIPEDIR_NONE:
1439 break;
1440 case PIPEDIR_IN:
1441 WARN_ON(dl_set);
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001442 *dl_pipe = __le32_to_cpu(entry->pipenum);
Michal Kazior7c6aa252014-08-26 19:14:03 +03001443 dl_set = true;
1444 break;
1445 case PIPEDIR_OUT:
1446 WARN_ON(ul_set);
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001447 *ul_pipe = __le32_to_cpu(entry->pipenum);
Michal Kazior7c6aa252014-08-26 19:14:03 +03001448 ul_set = true;
1449 break;
1450 case PIPEDIR_INOUT:
1451 WARN_ON(dl_set);
1452 WARN_ON(ul_set);
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001453 *dl_pipe = __le32_to_cpu(entry->pipenum);
1454 *ul_pipe = __le32_to_cpu(entry->pipenum);
Michal Kazior7c6aa252014-08-26 19:14:03 +03001455 dl_set = true;
1456 ul_set = true;
1457 break;
1458 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001459 }
Michal Kazior7c6aa252014-08-26 19:14:03 +03001460
1461 if (WARN_ON(!ul_set || !dl_set))
1462 return -ENOENT;
1463
Michal Kazior7c6aa252014-08-26 19:14:03 +03001464 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001465}
1466
1467static void ath10k_pci_hif_get_default_pipe(struct ath10k *ar,
Kalle Valo5b07e072014-09-14 12:50:06 +03001468 u8 *ul_pipe, u8 *dl_pipe)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001469{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001470 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif get default pipe\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001471
Kalle Valo5e3dd152013-06-12 20:52:10 +03001472 (void)ath10k_pci_hif_map_service_to_pipe(ar,
1473 ATH10K_HTC_SVC_ID_RSVD_CTRL,
Rajkumar Manoharan400143e2015-10-12 18:27:06 +05301474 ul_pipe, dl_pipe);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001475}
1476
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001477static void ath10k_pci_irq_msi_fw_mask(struct ath10k *ar)
1478{
1479 u32 val;
1480
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +05301481 switch (ar->hw_rev) {
1482 case ATH10K_HW_QCA988X:
1483 case ATH10K_HW_QCA6174:
Bartosz Markowskia226b512015-10-28 15:09:53 +01001484 case ATH10K_HW_QCA9377:
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +05301485 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1486 CORE_CTRL_ADDRESS);
1487 val &= ~CORE_CTRL_PCIE_REG_31_MASK;
1488 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
1489 CORE_CTRL_ADDRESS, val);
1490 break;
1491 case ATH10K_HW_QCA99X0:
1492 /* TODO: Find appropriate register configuration for QCA99X0
1493 * to mask irq/MSI.
1494 */
1495 break;
1496 }
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001497}
1498
1499static void ath10k_pci_irq_msi_fw_unmask(struct ath10k *ar)
1500{
1501 u32 val;
1502
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +05301503 switch (ar->hw_rev) {
1504 case ATH10K_HW_QCA988X:
1505 case ATH10K_HW_QCA6174:
Bartosz Markowskia226b512015-10-28 15:09:53 +01001506 case ATH10K_HW_QCA9377:
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +05301507 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
1508 CORE_CTRL_ADDRESS);
1509 val |= CORE_CTRL_PCIE_REG_31_MASK;
1510 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
1511 CORE_CTRL_ADDRESS, val);
1512 break;
1513 case ATH10K_HW_QCA99X0:
1514 /* TODO: Find appropriate register configuration for QCA99X0
1515 * to unmask irq/MSI.
1516 */
1517 break;
1518 }
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001519}
1520
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001521static void ath10k_pci_irq_disable(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001522{
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001523 ath10k_ce_disable_interrupts(ar);
1524 ath10k_pci_disable_and_clear_legacy_irq(ar);
1525 ath10k_pci_irq_msi_fw_mask(ar);
1526}
1527
1528static void ath10k_pci_irq_sync(struct ath10k *ar)
1529{
Kalle Valo5e3dd152013-06-12 20:52:10 +03001530 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001531 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001532
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001533 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
1534 synchronize_irq(ar_pci->pdev->irq + i);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001535}
1536
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001537static void ath10k_pci_irq_enable(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001538{
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001539 ath10k_ce_enable_interrupts(ar);
Michal Kaziore75db4e2014-08-28 22:14:16 +03001540 ath10k_pci_enable_legacy_irq(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001541 ath10k_pci_irq_msi_fw_unmask(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001542}
1543
1544static int ath10k_pci_hif_start(struct ath10k *ar)
1545{
Janusz Dziedzic76d870e2015-05-18 09:38:16 +00001546 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo9a149692015-10-05 17:56:36 +03001547
Michal Kazior7aa7a722014-08-25 12:09:38 +02001548 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif start\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001549
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001550 ath10k_pci_irq_enable(ar);
Michal Kazior728f95e2014-08-22 14:33:14 +02001551 ath10k_pci_rx_post(ar);
Kalle Valo50f87a62014-03-28 09:32:52 +02001552
Janusz Dziedzic76d870e2015-05-18 09:38:16 +00001553 pcie_capability_write_word(ar_pci->pdev, PCI_EXP_LNKCTL,
1554 ar_pci->link_ctl);
1555
Kalle Valo5e3dd152013-06-12 20:52:10 +03001556 return 0;
1557}
1558
Michal Kazior099ac7c2014-10-28 10:32:05 +01001559static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe *pci_pipe)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001560{
1561 struct ath10k *ar;
Michal Kazior099ac7c2014-10-28 10:32:05 +01001562 struct ath10k_ce_pipe *ce_pipe;
1563 struct ath10k_ce_ring *ce_ring;
1564 struct sk_buff *skb;
1565 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001566
Michal Kazior099ac7c2014-10-28 10:32:05 +01001567 ar = pci_pipe->hif_ce_state;
1568 ce_pipe = pci_pipe->ce_hdl;
1569 ce_ring = ce_pipe->dest_ring;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001570
Michal Kazior099ac7c2014-10-28 10:32:05 +01001571 if (!ce_ring)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001572 return;
1573
Michal Kazior099ac7c2014-10-28 10:32:05 +01001574 if (!pci_pipe->buf_sz)
1575 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001576
Michal Kazior099ac7c2014-10-28 10:32:05 +01001577 for (i = 0; i < ce_ring->nentries; i++) {
1578 skb = ce_ring->per_transfer_context[i];
1579 if (!skb)
1580 continue;
1581
1582 ce_ring->per_transfer_context[i] = NULL;
1583
Michal Kazior8582bf32015-01-24 12:14:47 +02001584 dma_unmap_single(ar->dev, ATH10K_SKB_RXCB(skb)->paddr,
Michal Kazior099ac7c2014-10-28 10:32:05 +01001585 skb->len + skb_tailroom(skb),
Kalle Valo5e3dd152013-06-12 20:52:10 +03001586 DMA_FROM_DEVICE);
Michal Kazior099ac7c2014-10-28 10:32:05 +01001587 dev_kfree_skb_any(skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001588 }
1589}
1590
Michal Kazior099ac7c2014-10-28 10:32:05 +01001591static void ath10k_pci_tx_pipe_cleanup(struct ath10k_pci_pipe *pci_pipe)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001592{
1593 struct ath10k *ar;
1594 struct ath10k_pci *ar_pci;
Michal Kazior099ac7c2014-10-28 10:32:05 +01001595 struct ath10k_ce_pipe *ce_pipe;
1596 struct ath10k_ce_ring *ce_ring;
Michal Kazior099ac7c2014-10-28 10:32:05 +01001597 struct sk_buff *skb;
Michal Kazior099ac7c2014-10-28 10:32:05 +01001598 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001599
Michal Kazior099ac7c2014-10-28 10:32:05 +01001600 ar = pci_pipe->hif_ce_state;
1601 ar_pci = ath10k_pci_priv(ar);
1602 ce_pipe = pci_pipe->ce_hdl;
1603 ce_ring = ce_pipe->src_ring;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001604
Michal Kazior099ac7c2014-10-28 10:32:05 +01001605 if (!ce_ring)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001606 return;
1607
Michal Kazior099ac7c2014-10-28 10:32:05 +01001608 if (!pci_pipe->buf_sz)
1609 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001610
Michal Kazior099ac7c2014-10-28 10:32:05 +01001611 for (i = 0; i < ce_ring->nentries; i++) {
1612 skb = ce_ring->per_transfer_context[i];
1613 if (!skb)
Michal Kazior2415fc12013-11-08 08:01:32 +01001614 continue;
Michal Kazior2415fc12013-11-08 08:01:32 +01001615
Michal Kazior099ac7c2014-10-28 10:32:05 +01001616 ce_ring->per_transfer_context[i] = NULL;
Michal Kazior099ac7c2014-10-28 10:32:05 +01001617
Rajkumar Manoharan0e5b2952015-10-12 18:27:01 +05301618 ath10k_htc_tx_completion_handler(ar, skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001619 }
1620}
1621
1622/*
1623 * Cleanup residual buffers for device shutdown:
1624 * buffers that were enqueued for receive
1625 * buffers that were to be sent
1626 * Note: Buffers that had completed but which were
1627 * not yet processed are on a completion queue. They
1628 * are handled when the completion thread shuts down.
1629 */
1630static void ath10k_pci_buffer_cleanup(struct ath10k *ar)
1631{
1632 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1633 int pipe_num;
1634
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001635 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Michal Kazior87263e52013-08-27 13:08:01 +02001636 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001637
1638 pipe_info = &ar_pci->pipe_info[pipe_num];
1639 ath10k_pci_rx_pipe_cleanup(pipe_info);
1640 ath10k_pci_tx_pipe_cleanup(pipe_info);
1641 }
1642}
1643
1644static void ath10k_pci_ce_deinit(struct ath10k *ar)
1645{
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001646 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001647
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001648 for (i = 0; i < CE_COUNT; i++)
1649 ath10k_ce_deinit_pipe(ar, i);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001650}
1651
Michal Kazior728f95e2014-08-22 14:33:14 +02001652static void ath10k_pci_flush(struct ath10k *ar)
1653{
1654 ath10k_pci_kill_tasklet(ar);
1655 ath10k_pci_buffer_cleanup(ar);
1656}
1657
Kalle Valo5e3dd152013-06-12 20:52:10 +03001658static void ath10k_pci_hif_stop(struct ath10k *ar)
1659{
Michal Kazior77258d42015-05-18 09:38:18 +00001660 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1661 unsigned long flags;
1662
Michal Kazior7aa7a722014-08-25 12:09:38 +02001663 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif stop\n");
Michal Kazior32270b62013-08-02 09:15:47 +02001664
Michal Kazior10d23db2014-08-22 14:33:15 +02001665 /* Most likely the device has HTT Rx ring configured. The only way to
1666 * prevent the device from accessing (and possible corrupting) host
1667 * memory is to reset the chip now.
Michal Kaziore75db4e2014-08-28 22:14:16 +03001668 *
1669 * There's also no known way of masking MSI interrupts on the device.
1670 * For ranged MSI the CE-related interrupts can be masked. However
1671 * regardless how many MSI interrupts are assigned the first one
1672 * is always used for firmware indications (crashes) and cannot be
1673 * masked. To prevent the device from asserting the interrupt reset it
1674 * before proceeding with cleanup.
Michal Kazior10d23db2014-08-22 14:33:15 +02001675 */
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +05301676 ath10k_pci_safe_chip_reset(ar);
Michal Kaziore75db4e2014-08-28 22:14:16 +03001677
1678 ath10k_pci_irq_disable(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001679 ath10k_pci_irq_sync(ar);
Michal Kaziore75db4e2014-08-28 22:14:16 +03001680 ath10k_pci_flush(ar);
Michal Kazior77258d42015-05-18 09:38:18 +00001681
1682 spin_lock_irqsave(&ar_pci->ps_lock, flags);
1683 WARN_ON(ar_pci->ps_wake_refcount > 0);
1684 spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001685}
1686
1687static int ath10k_pci_hif_exchange_bmi_msg(struct ath10k *ar,
1688 void *req, u32 req_len,
1689 void *resp, u32 *resp_len)
1690{
1691 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2aa39112013-08-27 13:08:02 +02001692 struct ath10k_pci_pipe *pci_tx = &ar_pci->pipe_info[BMI_CE_NUM_TO_TARG];
1693 struct ath10k_pci_pipe *pci_rx = &ar_pci->pipe_info[BMI_CE_NUM_TO_HOST];
1694 struct ath10k_ce_pipe *ce_tx = pci_tx->ce_hdl;
1695 struct ath10k_ce_pipe *ce_rx = pci_rx->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001696 dma_addr_t req_paddr = 0;
1697 dma_addr_t resp_paddr = 0;
1698 struct bmi_xfer xfer = {};
1699 void *treq, *tresp = NULL;
1700 int ret = 0;
1701
Michal Kazior85622cd2013-11-25 14:06:22 +01001702 might_sleep();
1703
Kalle Valo5e3dd152013-06-12 20:52:10 +03001704 if (resp && !resp_len)
1705 return -EINVAL;
1706
1707 if (resp && resp_len && *resp_len == 0)
1708 return -EINVAL;
1709
1710 treq = kmemdup(req, req_len, GFP_KERNEL);
1711 if (!treq)
1712 return -ENOMEM;
1713
1714 req_paddr = dma_map_single(ar->dev, treq, req_len, DMA_TO_DEVICE);
1715 ret = dma_mapping_error(ar->dev, req_paddr);
Michal Kazior5e55e3c2015-08-19 13:10:43 +02001716 if (ret) {
1717 ret = -EIO;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001718 goto err_dma;
Michal Kazior5e55e3c2015-08-19 13:10:43 +02001719 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001720
1721 if (resp && resp_len) {
1722 tresp = kzalloc(*resp_len, GFP_KERNEL);
1723 if (!tresp) {
1724 ret = -ENOMEM;
1725 goto err_req;
1726 }
1727
1728 resp_paddr = dma_map_single(ar->dev, tresp, *resp_len,
1729 DMA_FROM_DEVICE);
1730 ret = dma_mapping_error(ar->dev, resp_paddr);
Michal Kazior5e55e3c2015-08-19 13:10:43 +02001731 if (ret) {
1732 ret = EIO;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001733 goto err_req;
Michal Kazior5e55e3c2015-08-19 13:10:43 +02001734 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001735
1736 xfer.wait_for_resp = true;
1737 xfer.resp_len = 0;
1738
Michal Kazior728f95e2014-08-22 14:33:14 +02001739 ath10k_ce_rx_post_buf(ce_rx, &xfer, resp_paddr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001740 }
1741
Kalle Valo5e3dd152013-06-12 20:52:10 +03001742 ret = ath10k_ce_send(ce_tx, &xfer, req_paddr, req_len, -1, 0);
1743 if (ret)
1744 goto err_resp;
1745
Michal Kazior85622cd2013-11-25 14:06:22 +01001746 ret = ath10k_pci_bmi_wait(ce_tx, ce_rx, &xfer);
1747 if (ret) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001748 u32 unused_buffer;
1749 unsigned int unused_nbytes;
1750 unsigned int unused_id;
1751
Kalle Valo5e3dd152013-06-12 20:52:10 +03001752 ath10k_ce_cancel_send_next(ce_tx, NULL, &unused_buffer,
1753 &unused_nbytes, &unused_id);
1754 } else {
1755 /* non-zero means we did not time out */
1756 ret = 0;
1757 }
1758
1759err_resp:
1760 if (resp) {
1761 u32 unused_buffer;
1762
1763 ath10k_ce_revoke_recv_next(ce_rx, NULL, &unused_buffer);
1764 dma_unmap_single(ar->dev, resp_paddr,
1765 *resp_len, DMA_FROM_DEVICE);
1766 }
1767err_req:
1768 dma_unmap_single(ar->dev, req_paddr, req_len, DMA_TO_DEVICE);
1769
1770 if (ret == 0 && resp_len) {
1771 *resp_len = min(*resp_len, xfer.resp_len);
1772 memcpy(resp, tresp, xfer.resp_len);
1773 }
1774err_dma:
1775 kfree(treq);
1776 kfree(tresp);
1777
1778 return ret;
1779}
1780
Michal Kazior5440ce22013-09-03 15:09:58 +02001781static void ath10k_pci_bmi_send_done(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001782{
Michal Kazior5440ce22013-09-03 15:09:58 +02001783 struct bmi_xfer *xfer;
Michal Kazior5440ce22013-09-03 15:09:58 +02001784
Rajkumar Manoharan765952e2015-10-23 18:01:05 +05301785 if (ath10k_ce_completed_send_next(ce_state, (void **)&xfer))
Michal Kazior5440ce22013-09-03 15:09:58 +02001786 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001787
Michal Kazior2374b182014-07-14 16:25:25 +03001788 xfer->tx_done = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001789}
1790
Michal Kazior5440ce22013-09-03 15:09:58 +02001791static void ath10k_pci_bmi_recv_data(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001792{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001793 struct ath10k *ar = ce_state->ar;
Michal Kazior5440ce22013-09-03 15:09:58 +02001794 struct bmi_xfer *xfer;
1795 u32 ce_data;
1796 unsigned int nbytes;
1797 unsigned int transfer_id;
1798 unsigned int flags;
1799
1800 if (ath10k_ce_completed_recv_next(ce_state, (void **)&xfer, &ce_data,
1801 &nbytes, &transfer_id, &flags))
1802 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001803
Michal Kazior04ed9df2014-10-28 10:34:36 +01001804 if (WARN_ON_ONCE(!xfer))
1805 return;
1806
Kalle Valo5e3dd152013-06-12 20:52:10 +03001807 if (!xfer->wait_for_resp) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001808 ath10k_warn(ar, "unexpected: BMI data received; ignoring\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001809 return;
1810 }
1811
1812 xfer->resp_len = nbytes;
Michal Kazior2374b182014-07-14 16:25:25 +03001813 xfer->rx_done = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001814}
1815
Michal Kazior85622cd2013-11-25 14:06:22 +01001816static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
1817 struct ath10k_ce_pipe *rx_pipe,
1818 struct bmi_xfer *xfer)
1819{
1820 unsigned long timeout = jiffies + BMI_COMMUNICATION_TIMEOUT_HZ;
1821
1822 while (time_before_eq(jiffies, timeout)) {
1823 ath10k_pci_bmi_send_done(tx_pipe);
1824 ath10k_pci_bmi_recv_data(rx_pipe);
1825
Michal Kazior2374b182014-07-14 16:25:25 +03001826 if (xfer->tx_done && (xfer->rx_done == xfer->wait_for_resp))
Michal Kazior85622cd2013-11-25 14:06:22 +01001827 return 0;
1828
1829 schedule();
1830 }
1831
1832 return -ETIMEDOUT;
1833}
1834
Kalle Valo5e3dd152013-06-12 20:52:10 +03001835/*
Kalle Valo5e3dd152013-06-12 20:52:10 +03001836 * Send an interrupt to the device to wake up the Target CPU
1837 * so it has an opportunity to notice any changed state.
1838 */
1839static int ath10k_pci_wake_target_cpu(struct ath10k *ar)
1840{
Michal Kazior9e264942014-09-02 11:00:21 +03001841 u32 addr, val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001842
Michal Kazior9e264942014-09-02 11:00:21 +03001843 addr = SOC_CORE_BASE_ADDRESS | CORE_CTRL_ADDRESS;
1844 val = ath10k_pci_read32(ar, addr);
1845 val |= CORE_CTRL_CPU_INTR_MASK;
1846 ath10k_pci_write32(ar, addr, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001847
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001848 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001849}
1850
Michal Kaziord63955b2015-01-24 12:14:49 +02001851static int ath10k_pci_get_num_banks(struct ath10k *ar)
1852{
1853 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1854
1855 switch (ar_pci->pdev->device) {
1856 case QCA988X_2_0_DEVICE_ID:
Vasanthakumar Thiagarajan8bd47022015-06-18 12:31:03 +05301857 case QCA99X0_2_0_DEVICE_ID:
Michal Kaziord63955b2015-01-24 12:14:49 +02001858 return 1;
Michal Kazior36582e52015-08-13 14:32:26 +02001859 case QCA6164_2_1_DEVICE_ID:
Michal Kaziord63955b2015-01-24 12:14:49 +02001860 case QCA6174_2_1_DEVICE_ID:
1861 switch (MS(ar->chip_id, SOC_CHIP_ID_REV)) {
1862 case QCA6174_HW_1_0_CHIP_ID_REV:
1863 case QCA6174_HW_1_1_CHIP_ID_REV:
Michal Kazior11a002e2015-04-20 09:20:41 +00001864 case QCA6174_HW_2_1_CHIP_ID_REV:
1865 case QCA6174_HW_2_2_CHIP_ID_REV:
Michal Kaziord63955b2015-01-24 12:14:49 +02001866 return 3;
1867 case QCA6174_HW_1_3_CHIP_ID_REV:
1868 return 2;
Michal Kaziord63955b2015-01-24 12:14:49 +02001869 case QCA6174_HW_3_0_CHIP_ID_REV:
1870 case QCA6174_HW_3_1_CHIP_ID_REV:
1871 case QCA6174_HW_3_2_CHIP_ID_REV:
1872 return 9;
1873 }
1874 break;
Bartosz Markowskia226b512015-10-28 15:09:53 +01001875 case QCA9377_1_0_DEVICE_ID:
1876 return 2;
Michal Kaziord63955b2015-01-24 12:14:49 +02001877 }
1878
1879 ath10k_warn(ar, "unknown number of banks, assuming 1\n");
1880 return 1;
1881}
1882
Kalle Valo5e3dd152013-06-12 20:52:10 +03001883static int ath10k_pci_init_config(struct ath10k *ar)
1884{
1885 u32 interconnect_targ_addr;
1886 u32 pcie_state_targ_addr = 0;
1887 u32 pipe_cfg_targ_addr = 0;
1888 u32 svc_to_pipe_map = 0;
1889 u32 pcie_config_flags = 0;
1890 u32 ealloc_value;
1891 u32 ealloc_targ_addr;
1892 u32 flag2_value;
1893 u32 flag2_targ_addr;
1894 int ret = 0;
1895
1896 /* Download to Target the CE Config and the service-to-CE map */
1897 interconnect_targ_addr =
1898 host_interest_item_address(HI_ITEM(hi_interconnect_state));
1899
1900 /* Supply Target-side CE configuration */
Michal Kazior9e264942014-09-02 11:00:21 +03001901 ret = ath10k_pci_diag_read32(ar, interconnect_targ_addr,
1902 &pcie_state_targ_addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001903 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001904 ath10k_err(ar, "Failed to get pcie state addr: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001905 return ret;
1906 }
1907
1908 if (pcie_state_targ_addr == 0) {
1909 ret = -EIO;
Michal Kazior7aa7a722014-08-25 12:09:38 +02001910 ath10k_err(ar, "Invalid pcie state addr\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001911 return ret;
1912 }
1913
Michal Kazior9e264942014-09-02 11:00:21 +03001914 ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
Kalle Valo5e3dd152013-06-12 20:52:10 +03001915 offsetof(struct pcie_state,
Michal Kazior9e264942014-09-02 11:00:21 +03001916 pipe_cfg_addr)),
1917 &pipe_cfg_targ_addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001918 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001919 ath10k_err(ar, "Failed to get pipe cfg addr: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001920 return ret;
1921 }
1922
1923 if (pipe_cfg_targ_addr == 0) {
1924 ret = -EIO;
Michal Kazior7aa7a722014-08-25 12:09:38 +02001925 ath10k_err(ar, "Invalid pipe cfg addr\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001926 return ret;
1927 }
1928
1929 ret = ath10k_pci_diag_write_mem(ar, pipe_cfg_targ_addr,
Kalle Valo5b07e072014-09-14 12:50:06 +03001930 target_ce_config_wlan,
Vasanthakumar Thiagarajan050af062015-06-18 12:31:04 +05301931 sizeof(struct ce_pipe_config) *
1932 NUM_TARGET_CE_CONFIG_WLAN);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001933
1934 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001935 ath10k_err(ar, "Failed to write pipe cfg: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001936 return ret;
1937 }
1938
Michal Kazior9e264942014-09-02 11:00:21 +03001939 ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
Kalle Valo5e3dd152013-06-12 20:52:10 +03001940 offsetof(struct pcie_state,
Michal Kazior9e264942014-09-02 11:00:21 +03001941 svc_to_pipe_map)),
1942 &svc_to_pipe_map);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001943 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001944 ath10k_err(ar, "Failed to get svc/pipe map: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001945 return ret;
1946 }
1947
1948 if (svc_to_pipe_map == 0) {
1949 ret = -EIO;
Michal Kazior7aa7a722014-08-25 12:09:38 +02001950 ath10k_err(ar, "Invalid svc_to_pipe map\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001951 return ret;
1952 }
1953
1954 ret = ath10k_pci_diag_write_mem(ar, svc_to_pipe_map,
Kalle Valo5b07e072014-09-14 12:50:06 +03001955 target_service_to_ce_map_wlan,
1956 sizeof(target_service_to_ce_map_wlan));
Kalle Valo5e3dd152013-06-12 20:52:10 +03001957 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001958 ath10k_err(ar, "Failed to write svc/pipe map: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001959 return ret;
1960 }
1961
Michal Kazior9e264942014-09-02 11:00:21 +03001962 ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
Kalle Valo5e3dd152013-06-12 20:52:10 +03001963 offsetof(struct pcie_state,
Michal Kazior9e264942014-09-02 11:00:21 +03001964 config_flags)),
1965 &pcie_config_flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001966 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001967 ath10k_err(ar, "Failed to get pcie config_flags: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001968 return ret;
1969 }
1970
1971 pcie_config_flags &= ~PCIE_CONFIG_FLAG_ENABLE_L1;
1972
Michal Kazior9e264942014-09-02 11:00:21 +03001973 ret = ath10k_pci_diag_write32(ar, (pcie_state_targ_addr +
1974 offsetof(struct pcie_state,
1975 config_flags)),
1976 pcie_config_flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001977 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001978 ath10k_err(ar, "Failed to write pcie config_flags: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001979 return ret;
1980 }
1981
1982 /* configure early allocation */
1983 ealloc_targ_addr = host_interest_item_address(HI_ITEM(hi_early_alloc));
1984
Michal Kazior9e264942014-09-02 11:00:21 +03001985 ret = ath10k_pci_diag_read32(ar, ealloc_targ_addr, &ealloc_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001986 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001987 ath10k_err(ar, "Faile to get early alloc val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001988 return ret;
1989 }
1990
1991 /* first bank is switched to IRAM */
1992 ealloc_value |= ((HI_EARLY_ALLOC_MAGIC << HI_EARLY_ALLOC_MAGIC_SHIFT) &
1993 HI_EARLY_ALLOC_MAGIC_MASK);
Michal Kaziord63955b2015-01-24 12:14:49 +02001994 ealloc_value |= ((ath10k_pci_get_num_banks(ar) <<
1995 HI_EARLY_ALLOC_IRAM_BANKS_SHIFT) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03001996 HI_EARLY_ALLOC_IRAM_BANKS_MASK);
1997
Michal Kazior9e264942014-09-02 11:00:21 +03001998 ret = ath10k_pci_diag_write32(ar, ealloc_targ_addr, ealloc_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001999 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002000 ath10k_err(ar, "Failed to set early alloc val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002001 return ret;
2002 }
2003
2004 /* Tell Target to proceed with initialization */
2005 flag2_targ_addr = host_interest_item_address(HI_ITEM(hi_option_flag2));
2006
Michal Kazior9e264942014-09-02 11:00:21 +03002007 ret = ath10k_pci_diag_read32(ar, flag2_targ_addr, &flag2_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002008 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002009 ath10k_err(ar, "Failed to get option val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002010 return ret;
2011 }
2012
2013 flag2_value |= HI_OPTION_EARLY_CFG_DONE;
2014
Michal Kazior9e264942014-09-02 11:00:21 +03002015 ret = ath10k_pci_diag_write32(ar, flag2_targ_addr, flag2_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002016 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002017 ath10k_err(ar, "Failed to set option val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002018 return ret;
2019 }
2020
2021 return 0;
2022}
2023
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002024static int ath10k_pci_alloc_pipes(struct ath10k *ar)
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002025{
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002026 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2027 struct ath10k_pci_pipe *pipe;
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002028 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002029
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002030 for (i = 0; i < CE_COUNT; i++) {
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002031 pipe = &ar_pci->pipe_info[i];
2032 pipe->ce_hdl = &ar_pci->ce_states[i];
2033 pipe->pipe_num = i;
2034 pipe->hif_ce_state = ar;
2035
Rajkumar Manoharan9d9bdbb2015-10-12 18:27:02 +05302036 ret = ath10k_ce_alloc_pipe(ar, i, &host_ce_config_wlan[i]);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002037 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002038 ath10k_err(ar, "failed to allocate copy engine pipe %d: %d\n",
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002039 i, ret);
2040 return ret;
2041 }
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002042
2043 /* Last CE is Diagnostic Window */
Vasanthakumar Thiagarajan050af062015-06-18 12:31:04 +05302044 if (i == CE_DIAG_PIPE) {
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002045 ar_pci->ce_diag = pipe->ce_hdl;
2046 continue;
2047 }
2048
2049 pipe->buf_sz = (size_t)(host_ce_config_wlan[i].src_sz_max);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002050 }
2051
2052 return 0;
2053}
2054
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002055static void ath10k_pci_free_pipes(struct ath10k *ar)
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002056{
2057 int i;
2058
2059 for (i = 0; i < CE_COUNT; i++)
2060 ath10k_ce_free_pipe(ar, i);
2061}
Kalle Valo5e3dd152013-06-12 20:52:10 +03002062
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002063static int ath10k_pci_init_pipes(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002064{
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002065 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002066
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002067 for (i = 0; i < CE_COUNT; i++) {
2068 ret = ath10k_ce_init_pipe(ar, i, &host_ce_config_wlan[i]);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002069 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002070 ath10k_err(ar, "failed to initialize copy engine pipe %d: %d\n",
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002071 i, ret);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002072 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002073 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002074 }
2075
Kalle Valo5e3dd152013-06-12 20:52:10 +03002076 return 0;
2077}
2078
Michal Kazior5c771e72014-08-22 14:23:34 +02002079static bool ath10k_pci_has_fw_crashed(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002080{
Michal Kazior5c771e72014-08-22 14:23:34 +02002081 return ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS) &
2082 FW_IND_EVENT_PENDING;
2083}
Kalle Valo5e3dd152013-06-12 20:52:10 +03002084
Michal Kazior5c771e72014-08-22 14:23:34 +02002085static void ath10k_pci_fw_crashed_clear(struct ath10k *ar)
2086{
2087 u32 val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002088
Michal Kazior5c771e72014-08-22 14:23:34 +02002089 val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
2090 val &= ~FW_IND_EVENT_PENDING;
2091 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002092}
2093
Michal Kaziorde013572014-05-14 16:56:16 +03002094/* this function effectively clears target memory controller assert line */
2095static void ath10k_pci_warm_reset_si0(struct ath10k *ar)
2096{
2097 u32 val;
2098
2099 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
2100 ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
2101 val | SOC_RESET_CONTROL_SI0_RST_MASK);
2102 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
2103
2104 msleep(10);
2105
2106 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
2107 ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
2108 val & ~SOC_RESET_CONTROL_SI0_RST_MASK);
2109 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
2110
2111 msleep(10);
2112}
2113
Michal Kazior61c16482014-10-28 10:32:06 +01002114static void ath10k_pci_warm_reset_cpu(struct ath10k *ar)
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002115{
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002116 u32 val;
2117
Kalle Valob39712c2014-03-28 09:32:46 +02002118 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, 0);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002119
Michal Kazior61c16482014-10-28 10:32:06 +01002120 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
2121 SOC_RESET_CONTROL_ADDRESS);
2122 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
2123 val | SOC_RESET_CONTROL_CPU_WARM_RST_MASK);
2124}
2125
2126static void ath10k_pci_warm_reset_ce(struct ath10k *ar)
2127{
2128 u32 val;
2129
2130 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
2131 SOC_RESET_CONTROL_ADDRESS);
2132
2133 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
2134 val | SOC_RESET_CONTROL_CE_RST_MASK);
2135 msleep(10);
2136 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
2137 val & ~SOC_RESET_CONTROL_CE_RST_MASK);
2138}
2139
2140static void ath10k_pci_warm_reset_clear_lf(struct ath10k *ar)
2141{
2142 u32 val;
2143
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002144 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
2145 SOC_LF_TIMER_CONTROL0_ADDRESS);
2146 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS +
2147 SOC_LF_TIMER_CONTROL0_ADDRESS,
2148 val & ~SOC_LF_TIMER_CONTROL0_ENABLE_MASK);
Michal Kazior61c16482014-10-28 10:32:06 +01002149}
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002150
Michal Kazior61c16482014-10-28 10:32:06 +01002151static int ath10k_pci_warm_reset(struct ath10k *ar)
2152{
2153 int ret;
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002154
Michal Kazior61c16482014-10-28 10:32:06 +01002155 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot warm reset\n");
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002156
Michal Kazior61c16482014-10-28 10:32:06 +01002157 spin_lock_bh(&ar->data_lock);
2158 ar->stats.fw_warm_reset_counter++;
2159 spin_unlock_bh(&ar->data_lock);
2160
2161 ath10k_pci_irq_disable(ar);
2162
2163 /* Make sure the target CPU is not doing anything dangerous, e.g. if it
2164 * were to access copy engine while host performs copy engine reset
2165 * then it is possible for the device to confuse pci-e controller to
2166 * the point of bringing host system to a complete stop (i.e. hang).
2167 */
Michal Kaziorde013572014-05-14 16:56:16 +03002168 ath10k_pci_warm_reset_si0(ar);
Michal Kazior61c16482014-10-28 10:32:06 +01002169 ath10k_pci_warm_reset_cpu(ar);
2170 ath10k_pci_init_pipes(ar);
2171 ath10k_pci_wait_for_target_init(ar);
Michal Kaziorde013572014-05-14 16:56:16 +03002172
Michal Kazior61c16482014-10-28 10:32:06 +01002173 ath10k_pci_warm_reset_clear_lf(ar);
2174 ath10k_pci_warm_reset_ce(ar);
2175 ath10k_pci_warm_reset_cpu(ar);
2176 ath10k_pci_init_pipes(ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002177
Michal Kazior61c16482014-10-28 10:32:06 +01002178 ret = ath10k_pci_wait_for_target_init(ar);
2179 if (ret) {
2180 ath10k_warn(ar, "failed to wait for target init: %d\n", ret);
2181 return ret;
2182 }
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002183
Michal Kazior7aa7a722014-08-25 12:09:38 +02002184 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot warm reset complete\n");
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002185
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002186 return 0;
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002187}
2188
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +05302189static int ath10k_pci_safe_chip_reset(struct ath10k *ar)
2190{
2191 if (QCA_REV_988X(ar) || QCA_REV_6174(ar)) {
2192 return ath10k_pci_warm_reset(ar);
2193 } else if (QCA_REV_99X0(ar)) {
2194 ath10k_pci_irq_disable(ar);
2195 return ath10k_pci_qca99x0_chip_reset(ar);
2196 } else {
2197 return -ENOTSUPP;
2198 }
2199}
2200
Michal Kaziord63955b2015-01-24 12:14:49 +02002201static int ath10k_pci_qca988x_chip_reset(struct ath10k *ar)
Michal Kazior0bc14d02014-10-28 10:32:07 +01002202{
2203 int i, ret;
2204 u32 val;
2205
Michal Kaziord63955b2015-01-24 12:14:49 +02002206 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot 988x chip reset\n");
Michal Kazior0bc14d02014-10-28 10:32:07 +01002207
2208 /* Some hardware revisions (e.g. CUS223v2) has issues with cold reset.
2209 * It is thus preferred to use warm reset which is safer but may not be
2210 * able to recover the device from all possible fail scenarios.
2211 *
2212 * Warm reset doesn't always work on first try so attempt it a few
2213 * times before giving up.
2214 */
2215 for (i = 0; i < ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS; i++) {
2216 ret = ath10k_pci_warm_reset(ar);
2217 if (ret) {
2218 ath10k_warn(ar, "failed to warm reset attempt %d of %d: %d\n",
2219 i + 1, ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS,
2220 ret);
2221 continue;
2222 }
2223
2224 /* FIXME: Sometimes copy engine doesn't recover after warm
2225 * reset. In most cases this needs cold reset. In some of these
2226 * cases the device is in such a state that a cold reset may
2227 * lock up the host.
2228 *
2229 * Reading any host interest register via copy engine is
2230 * sufficient to verify if device is capable of booting
2231 * firmware blob.
2232 */
2233 ret = ath10k_pci_init_pipes(ar);
2234 if (ret) {
2235 ath10k_warn(ar, "failed to init copy engine: %d\n",
2236 ret);
2237 continue;
2238 }
2239
2240 ret = ath10k_pci_diag_read32(ar, QCA988X_HOST_INTEREST_ADDRESS,
2241 &val);
2242 if (ret) {
2243 ath10k_warn(ar, "failed to poke copy engine: %d\n",
2244 ret);
2245 continue;
2246 }
2247
2248 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot chip reset complete (warm)\n");
2249 return 0;
2250 }
2251
2252 if (ath10k_pci_reset_mode == ATH10K_PCI_RESET_WARM_ONLY) {
2253 ath10k_warn(ar, "refusing cold reset as requested\n");
2254 return -EPERM;
2255 }
2256
2257 ret = ath10k_pci_cold_reset(ar);
2258 if (ret) {
2259 ath10k_warn(ar, "failed to cold reset: %d\n", ret);
2260 return ret;
2261 }
2262
2263 ret = ath10k_pci_wait_for_target_init(ar);
2264 if (ret) {
2265 ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
2266 ret);
2267 return ret;
2268 }
2269
Michal Kaziord63955b2015-01-24 12:14:49 +02002270 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca988x chip reset complete (cold)\n");
Michal Kazior0bc14d02014-10-28 10:32:07 +01002271
2272 return 0;
2273}
2274
Michal Kaziord63955b2015-01-24 12:14:49 +02002275static int ath10k_pci_qca6174_chip_reset(struct ath10k *ar)
2276{
2277 int ret;
2278
2279 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca6174 chip reset\n");
2280
2281 /* FIXME: QCA6174 requires cold + warm reset to work. */
2282
2283 ret = ath10k_pci_cold_reset(ar);
2284 if (ret) {
2285 ath10k_warn(ar, "failed to cold reset: %d\n", ret);
2286 return ret;
2287 }
2288
2289 ret = ath10k_pci_wait_for_target_init(ar);
2290 if (ret) {
2291 ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
Kalle Valo617b0f42015-10-05 17:56:35 +03002292 ret);
Michal Kaziord63955b2015-01-24 12:14:49 +02002293 return ret;
2294 }
2295
2296 ret = ath10k_pci_warm_reset(ar);
2297 if (ret) {
2298 ath10k_warn(ar, "failed to warm reset: %d\n", ret);
2299 return ret;
2300 }
2301
2302 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca6174 chip reset complete (cold)\n");
2303
2304 return 0;
2305}
2306
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +05302307static int ath10k_pci_qca99x0_chip_reset(struct ath10k *ar)
2308{
2309 int ret;
2310
2311 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca99x0 chip reset\n");
2312
2313 ret = ath10k_pci_cold_reset(ar);
2314 if (ret) {
2315 ath10k_warn(ar, "failed to cold reset: %d\n", ret);
2316 return ret;
2317 }
2318
2319 ret = ath10k_pci_wait_for_target_init(ar);
2320 if (ret) {
2321 ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
2322 ret);
2323 return ret;
2324 }
2325
2326 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca99x0 chip reset complete (cold)\n");
2327
2328 return 0;
2329}
2330
Michal Kaziord63955b2015-01-24 12:14:49 +02002331static int ath10k_pci_chip_reset(struct ath10k *ar)
2332{
2333 if (QCA_REV_988X(ar))
2334 return ath10k_pci_qca988x_chip_reset(ar);
2335 else if (QCA_REV_6174(ar))
2336 return ath10k_pci_qca6174_chip_reset(ar);
Bartosz Markowskia226b512015-10-28 15:09:53 +01002337 else if (QCA_REV_9377(ar))
2338 return ath10k_pci_qca6174_chip_reset(ar);
Vasanthakumar Thiagarajan6e4202c2015-06-18 12:31:06 +05302339 else if (QCA_REV_99X0(ar))
2340 return ath10k_pci_qca99x0_chip_reset(ar);
Michal Kaziord63955b2015-01-24 12:14:49 +02002341 else
2342 return -ENOTSUPP;
2343}
2344
Michal Kazior0bc14d02014-10-28 10:32:07 +01002345static int ath10k_pci_hif_power_up(struct ath10k *ar)
Michal Kazior8c5c5362013-07-16 09:38:50 +02002346{
Janusz Dziedzic76d870e2015-05-18 09:38:16 +00002347 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02002348 int ret;
2349
Michal Kazior0bc14d02014-10-28 10:32:07 +01002350 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif power up\n");
2351
Janusz Dziedzic76d870e2015-05-18 09:38:16 +00002352 pcie_capability_read_word(ar_pci->pdev, PCI_EXP_LNKCTL,
2353 &ar_pci->link_ctl);
2354 pcie_capability_write_word(ar_pci->pdev, PCI_EXP_LNKCTL,
2355 ar_pci->link_ctl & ~PCI_EXP_LNKCTL_ASPMC);
2356
Michal Kazior8c5c5362013-07-16 09:38:50 +02002357 /*
2358 * Bring the target up cleanly.
2359 *
2360 * The target may be in an undefined state with an AUX-powered Target
2361 * and a Host in WoW mode. If the Host crashes, loses power, or is
2362 * restarted (without unloading the driver) then the Target is left
2363 * (aux) powered and running. On a subsequent driver load, the Target
2364 * is in an unexpected state. We try to catch that here in order to
2365 * reset the Target and retry the probe.
2366 */
Michal Kazior0bc14d02014-10-28 10:32:07 +01002367 ret = ath10k_pci_chip_reset(ar);
Michal Kazior5b2589f2013-11-08 08:01:30 +01002368 if (ret) {
Michal Kaziora2fa8802015-01-12 15:29:37 +01002369 if (ath10k_pci_has_fw_crashed(ar)) {
2370 ath10k_warn(ar, "firmware crashed during chip reset\n");
2371 ath10k_pci_fw_crashed_clear(ar);
2372 ath10k_pci_fw_crashed_dump(ar);
2373 }
2374
Michal Kazior0bc14d02014-10-28 10:32:07 +01002375 ath10k_err(ar, "failed to reset chip: %d\n", ret);
Bartosz Markowski707b1bbd2014-10-31 09:03:43 +01002376 goto err_sleep;
Michal Kazior5b2589f2013-11-08 08:01:30 +01002377 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02002378
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002379 ret = ath10k_pci_init_pipes(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02002380 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002381 ath10k_err(ar, "failed to initialize CE: %d\n", ret);
Bartosz Markowski707b1bbd2014-10-31 09:03:43 +01002382 goto err_sleep;
Michal Kaziorab977bd2013-11-25 14:06:26 +01002383 }
2384
Michal Kazior98563d52013-11-08 08:01:33 +01002385 ret = ath10k_pci_init_config(ar);
2386 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002387 ath10k_err(ar, "failed to setup init config: %d\n", ret);
Michal Kazior5c771e72014-08-22 14:23:34 +02002388 goto err_ce;
Michal Kazior98563d52013-11-08 08:01:33 +01002389 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02002390
2391 ret = ath10k_pci_wake_target_cpu(ar);
2392 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002393 ath10k_err(ar, "could not wake up target CPU: %d\n", ret);
Michal Kazior5c771e72014-08-22 14:23:34 +02002394 goto err_ce;
Michal Kazior8c5c5362013-07-16 09:38:50 +02002395 }
2396
2397 return 0;
2398
2399err_ce:
2400 ath10k_pci_ce_deinit(ar);
Michal Kazior0bc14d02014-10-28 10:32:07 +01002401
Bartosz Markowski707b1bbd2014-10-31 09:03:43 +01002402err_sleep:
Michal Kazior8c5c5362013-07-16 09:38:50 +02002403 return ret;
2404}
2405
2406static void ath10k_pci_hif_power_down(struct ath10k *ar)
2407{
Michal Kazior7aa7a722014-08-25 12:09:38 +02002408 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif power down\n");
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002409
Michal Kaziorc011b282014-10-28 10:32:08 +01002410 /* Currently hif_power_up performs effectively a reset and hif_stop
2411 * resets the chip as well so there's no point in resetting here.
2412 */
Michal Kazior8c5c5362013-07-16 09:38:50 +02002413}
2414
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002415#ifdef CONFIG_PM
2416
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002417static int ath10k_pci_hif_suspend(struct ath10k *ar)
2418{
Michal Kazior77258d42015-05-18 09:38:18 +00002419 /* The grace timer can still be counting down and ar->ps_awake be true.
2420 * It is known that the device may be asleep after resuming regardless
2421 * of the SoC powersave state before suspending. Hence make sure the
2422 * device is asleep before proceeding.
2423 */
2424 ath10k_pci_sleep_sync(ar);
Michal Kazior320e14b2015-03-02 13:22:13 +01002425
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002426 return 0;
2427}
2428
2429static int ath10k_pci_hif_resume(struct ath10k *ar)
2430{
2431 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2432 struct pci_dev *pdev = ar_pci->pdev;
2433 u32 val;
Anilkumar Kolli1aaf8ef2015-10-16 15:54:51 +03002434 int ret = 0;
2435
2436 if (ar_pci->pci_ps == 0) {
2437 ret = ath10k_pci_force_wake(ar);
2438 if (ret) {
2439 ath10k_err(ar, "failed to wake up target: %d\n", ret);
2440 return ret;
2441 }
2442 }
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002443
Michal Kazior9ff4be92015-03-02 13:22:14 +01002444 /* Suspend/Resume resets the PCI configuration space, so we have to
2445 * re-disable the RETRY_TIMEOUT register (0x41) to keep PCI Tx retries
2446 * from interfering with C3 CPU state. pci_restore_state won't help
2447 * here since it only restores the first 64 bytes pci config header.
2448 */
2449 pci_read_config_dword(pdev, 0x40, &val);
2450 if ((val & 0x0000ff00) != 0)
2451 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002452
Anilkumar Kolli1aaf8ef2015-10-16 15:54:51 +03002453 return ret;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002454}
2455#endif
2456
Kalle Valo5e3dd152013-06-12 20:52:10 +03002457static const struct ath10k_hif_ops ath10k_pci_hif_ops = {
Michal Kazior726346f2014-02-27 18:50:04 +02002458 .tx_sg = ath10k_pci_hif_tx_sg,
Kalle Valoeef25402014-09-24 14:16:52 +03002459 .diag_read = ath10k_pci_hif_diag_read,
Yanbo Li9f65ad22014-11-25 12:24:48 +02002460 .diag_write = ath10k_pci_diag_write_mem,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002461 .exchange_bmi_msg = ath10k_pci_hif_exchange_bmi_msg,
2462 .start = ath10k_pci_hif_start,
2463 .stop = ath10k_pci_hif_stop,
2464 .map_service_to_pipe = ath10k_pci_hif_map_service_to_pipe,
2465 .get_default_pipe = ath10k_pci_hif_get_default_pipe,
2466 .send_complete_check = ath10k_pci_hif_send_complete_check,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002467 .get_free_queue_number = ath10k_pci_hif_get_free_queue_number,
Michal Kazior8c5c5362013-07-16 09:38:50 +02002468 .power_up = ath10k_pci_hif_power_up,
2469 .power_down = ath10k_pci_hif_power_down,
Yanbo Li077a3802014-11-25 12:24:33 +02002470 .read32 = ath10k_pci_read32,
2471 .write32 = ath10k_pci_write32,
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002472#ifdef CONFIG_PM
2473 .suspend = ath10k_pci_hif_suspend,
2474 .resume = ath10k_pci_hif_resume,
2475#endif
Kalle Valo5e3dd152013-06-12 20:52:10 +03002476};
2477
2478static void ath10k_pci_ce_tasklet(unsigned long ptr)
2479{
Michal Kazior87263e52013-08-27 13:08:01 +02002480 struct ath10k_pci_pipe *pipe = (struct ath10k_pci_pipe *)ptr;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002481 struct ath10k_pci *ar_pci = pipe->ar_pci;
2482
2483 ath10k_ce_per_engine_service(ar_pci->ar, pipe->pipe_num);
2484}
2485
2486static void ath10k_msi_err_tasklet(unsigned long data)
2487{
2488 struct ath10k *ar = (struct ath10k *)data;
2489
Michal Kazior5c771e72014-08-22 14:23:34 +02002490 if (!ath10k_pci_has_fw_crashed(ar)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002491 ath10k_warn(ar, "received unsolicited fw crash interrupt\n");
Michal Kazior5c771e72014-08-22 14:23:34 +02002492 return;
2493 }
2494
Michal Kazior6f3b7ff2015-01-24 12:14:52 +02002495 ath10k_pci_irq_disable(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02002496 ath10k_pci_fw_crashed_clear(ar);
2497 ath10k_pci_fw_crashed_dump(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002498}
2499
2500/*
2501 * Handler for a per-engine interrupt on a PARTICULAR CE.
2502 * This is used in cases where each CE has a private MSI interrupt.
2503 */
2504static irqreturn_t ath10k_pci_per_engine_handler(int irq, void *arg)
2505{
2506 struct ath10k *ar = arg;
2507 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2508 int ce_id = irq - ar_pci->pdev->irq - MSI_ASSIGN_CE_INITIAL;
2509
Dan Carpentere5742672013-06-18 10:28:46 +03002510 if (ce_id < 0 || ce_id >= ARRAY_SIZE(ar_pci->pipe_info)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002511 ath10k_warn(ar, "unexpected/invalid irq %d ce_id %d\n", irq,
2512 ce_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002513 return IRQ_HANDLED;
2514 }
2515
2516 /*
2517 * NOTE: We are able to derive ce_id from irq because we
2518 * use a one-to-one mapping for CE's 0..5.
2519 * CE's 6 & 7 do not use interrupts at all.
2520 *
2521 * This mapping must be kept in sync with the mapping
2522 * used by firmware.
2523 */
2524 tasklet_schedule(&ar_pci->pipe_info[ce_id].intr);
2525 return IRQ_HANDLED;
2526}
2527
2528static irqreturn_t ath10k_pci_msi_fw_handler(int irq, void *arg)
2529{
2530 struct ath10k *ar = arg;
2531 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2532
2533 tasklet_schedule(&ar_pci->msi_fw_err);
2534 return IRQ_HANDLED;
2535}
2536
2537/*
2538 * Top-level interrupt handler for all PCI interrupts from a Target.
2539 * When a block of MSI interrupts is allocated, this top-level handler
2540 * is not used; instead, we directly call the correct sub-handler.
2541 */
2542static irqreturn_t ath10k_pci_interrupt_handler(int irq, void *arg)
2543{
2544 struct ath10k *ar = arg;
2545 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Anilkumar Kolli1aaf8ef2015-10-16 15:54:51 +03002546 int ret;
2547
2548 if (ar_pci->pci_ps == 0) {
2549 ret = ath10k_pci_force_wake(ar);
2550 if (ret) {
2551 ath10k_warn(ar, "failed to wake device up on irq: %d\n",
2552 ret);
2553 return IRQ_NONE;
2554 }
2555 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002556
2557 if (ar_pci->num_msi_intrs == 0) {
Michal Kaziore5398872013-11-25 14:06:20 +01002558 if (!ath10k_pci_irq_pending(ar))
2559 return IRQ_NONE;
2560
Michal Kazior26852182013-11-25 14:06:25 +01002561 ath10k_pci_disable_and_clear_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002562 }
2563
2564 tasklet_schedule(&ar_pci->intr_tq);
2565
2566 return IRQ_HANDLED;
2567}
2568
2569static void ath10k_pci_tasklet(unsigned long data)
2570{
2571 struct ath10k *ar = (struct ath10k *)data;
2572 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2573
Michal Kazior5c771e72014-08-22 14:23:34 +02002574 if (ath10k_pci_has_fw_crashed(ar)) {
Michal Kazior6f3b7ff2015-01-24 12:14:52 +02002575 ath10k_pci_irq_disable(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02002576 ath10k_pci_fw_crashed_clear(ar);
2577 ath10k_pci_fw_crashed_dump(ar);
2578 return;
2579 }
2580
Kalle Valo5e3dd152013-06-12 20:52:10 +03002581 ath10k_ce_per_engine_service_any(ar);
2582
Michal Kazior26852182013-11-25 14:06:25 +01002583 /* Re-enable legacy irq that was disabled in the irq handler */
2584 if (ar_pci->num_msi_intrs == 0)
2585 ath10k_pci_enable_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002586}
2587
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002588static int ath10k_pci_request_irq_msix(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002589{
2590 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002591 int ret, i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002592
2593 ret = request_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW,
2594 ath10k_pci_msi_fw_handler,
2595 IRQF_SHARED, "ath10k_pci", ar);
Michal Kazior591ecdb2013-07-31 10:55:15 +02002596 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002597 ath10k_warn(ar, "failed to request MSI-X fw irq %d: %d\n",
Michal Kazior591ecdb2013-07-31 10:55:15 +02002598 ar_pci->pdev->irq + MSI_ASSIGN_FW, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002599 return ret;
Michal Kazior591ecdb2013-07-31 10:55:15 +02002600 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002601
2602 for (i = MSI_ASSIGN_CE_INITIAL; i <= MSI_ASSIGN_CE_MAX; i++) {
2603 ret = request_irq(ar_pci->pdev->irq + i,
2604 ath10k_pci_per_engine_handler,
2605 IRQF_SHARED, "ath10k_pci", ar);
2606 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002607 ath10k_warn(ar, "failed to request MSI-X ce irq %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002608 ar_pci->pdev->irq + i, ret);
2609
Michal Kazior87b14232013-06-26 08:50:50 +02002610 for (i--; i >= MSI_ASSIGN_CE_INITIAL; i--)
2611 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002612
Michal Kazior87b14232013-06-26 08:50:50 +02002613 free_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002614 return ret;
2615 }
2616 }
2617
Kalle Valo5e3dd152013-06-12 20:52:10 +03002618 return 0;
2619}
2620
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002621static int ath10k_pci_request_irq_msi(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002622{
2623 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2624 int ret;
2625
2626 ret = request_irq(ar_pci->pdev->irq,
2627 ath10k_pci_interrupt_handler,
2628 IRQF_SHARED, "ath10k_pci", ar);
Kalle Valof3782742013-10-17 11:36:15 +03002629 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002630 ath10k_warn(ar, "failed to request MSI irq %d: %d\n",
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002631 ar_pci->pdev->irq, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002632 return ret;
Kalle Valof3782742013-10-17 11:36:15 +03002633 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002634
Kalle Valo5e3dd152013-06-12 20:52:10 +03002635 return 0;
2636}
2637
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002638static int ath10k_pci_request_irq_legacy(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002639{
2640 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002641 int ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002642
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002643 ret = request_irq(ar_pci->pdev->irq,
2644 ath10k_pci_interrupt_handler,
2645 IRQF_SHARED, "ath10k_pci", ar);
Kalle Valof3782742013-10-17 11:36:15 +03002646 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002647 ath10k_warn(ar, "failed to request legacy irq %d: %d\n",
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002648 ar_pci->pdev->irq, ret);
Kalle Valof3782742013-10-17 11:36:15 +03002649 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002650 }
2651
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002652 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002653}
2654
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002655static int ath10k_pci_request_irq(struct ath10k *ar)
2656{
2657 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2658
2659 switch (ar_pci->num_msi_intrs) {
2660 case 0:
2661 return ath10k_pci_request_irq_legacy(ar);
2662 case 1:
2663 return ath10k_pci_request_irq_msi(ar);
Janusz Dziedzicb8402d82015-09-17 08:17:33 +02002664 default:
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002665 return ath10k_pci_request_irq_msix(ar);
2666 }
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002667}
2668
2669static void ath10k_pci_free_irq(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002670{
2671 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2672 int i;
2673
2674 /* There's at least one interrupt irregardless whether its legacy INTR
2675 * or MSI or MSI-X */
2676 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
2677 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002678}
2679
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002680static void ath10k_pci_init_irq_tasklets(struct ath10k *ar)
2681{
2682 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2683 int i;
2684
2685 tasklet_init(&ar_pci->intr_tq, ath10k_pci_tasklet, (unsigned long)ar);
2686 tasklet_init(&ar_pci->msi_fw_err, ath10k_msi_err_tasklet,
2687 (unsigned long)ar);
2688
2689 for (i = 0; i < CE_COUNT; i++) {
2690 ar_pci->pipe_info[i].ar_pci = ar_pci;
2691 tasklet_init(&ar_pci->pipe_info[i].intr, ath10k_pci_ce_tasklet,
2692 (unsigned long)&ar_pci->pipe_info[i]);
2693 }
2694}
2695
2696static int ath10k_pci_init_irq(struct ath10k *ar)
2697{
2698 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2699 int ret;
2700
2701 ath10k_pci_init_irq_tasklets(ar);
2702
Michal Kazior403d6272014-08-22 14:23:31 +02002703 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_AUTO)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002704 ath10k_info(ar, "limiting irq mode to: %d\n",
2705 ath10k_pci_irq_mode);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002706
2707 /* Try MSI-X */
Michal Kazior0edf2572014-08-07 11:03:29 +02002708 if (ath10k_pci_irq_mode == ATH10K_PCI_IRQ_AUTO) {
Janusz Dziedzicb8402d82015-09-17 08:17:33 +02002709 ar_pci->num_msi_intrs = MSI_ASSIGN_CE_MAX + 1;
Alexander Gordeev5ad68672014-02-13 17:50:02 +02002710 ret = pci_enable_msi_range(ar_pci->pdev, ar_pci->num_msi_intrs,
Kalle Valo5b07e072014-09-14 12:50:06 +03002711 ar_pci->num_msi_intrs);
Alexander Gordeev5ad68672014-02-13 17:50:02 +02002712 if (ret > 0)
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002713 return 0;
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002714
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002715 /* fall-through */
2716 }
2717
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002718 /* Try MSI */
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002719 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_LEGACY) {
2720 ar_pci->num_msi_intrs = 1;
2721 ret = pci_enable_msi(ar_pci->pdev);
2722 if (ret == 0)
2723 return 0;
2724
2725 /* fall-through */
2726 }
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002727
2728 /* Try legacy irq
2729 *
2730 * A potential race occurs here: The CORE_BASE write
2731 * depends on target correctly decoding AXI address but
2732 * host won't know when target writes BAR to CORE_CTRL.
2733 * This write might get lost if target has NOT written BAR.
2734 * For now, fix the race by repeating the write in below
2735 * synchronization checking. */
2736 ar_pci->num_msi_intrs = 0;
2737
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002738 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2739 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002740
2741 return 0;
2742}
2743
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002744static void ath10k_pci_deinit_irq_legacy(struct ath10k *ar)
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002745{
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002746 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2747 0);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002748}
2749
2750static int ath10k_pci_deinit_irq(struct ath10k *ar)
2751{
2752 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2753
2754 switch (ar_pci->num_msi_intrs) {
2755 case 0:
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002756 ath10k_pci_deinit_irq_legacy(ar);
Janusz Dziedzicb8402d82015-09-17 08:17:33 +02002757 break;
Alexander Gordeevbb8b6212014-02-13 17:50:01 +02002758 default:
2759 pci_disable_msi(ar_pci->pdev);
Janusz Dziedzicb8402d82015-09-17 08:17:33 +02002760 break;
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002761 }
2762
Janusz Dziedzicb8402d82015-09-17 08:17:33 +02002763 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002764}
2765
Michal Kaziord7fb47f2013-11-08 08:01:26 +01002766static int ath10k_pci_wait_for_target_init(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002767{
2768 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo0399eca2014-03-28 09:32:21 +02002769 unsigned long timeout;
Kalle Valo0399eca2014-03-28 09:32:21 +02002770 u32 val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002771
Michal Kazior7aa7a722014-08-25 12:09:38 +02002772 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot waiting target to initialise\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002773
Kalle Valo0399eca2014-03-28 09:32:21 +02002774 timeout = jiffies + msecs_to_jiffies(ATH10K_PCI_TARGET_WAIT);
2775
2776 do {
2777 val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
2778
Michal Kazior7aa7a722014-08-25 12:09:38 +02002779 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target indicator %x\n",
2780 val);
Kalle Valo50f87a62014-03-28 09:32:52 +02002781
Kalle Valo0399eca2014-03-28 09:32:21 +02002782 /* target should never return this */
2783 if (val == 0xffffffff)
2784 continue;
2785
Michal Kazior7710cd22014-04-23 19:30:04 +03002786 /* the device has crashed so don't bother trying anymore */
2787 if (val & FW_IND_EVENT_PENDING)
2788 break;
2789
Kalle Valo0399eca2014-03-28 09:32:21 +02002790 if (val & FW_IND_INITIALIZED)
2791 break;
2792
Kalle Valo5e3dd152013-06-12 20:52:10 +03002793 if (ar_pci->num_msi_intrs == 0)
2794 /* Fix potential race by repeating CORE_BASE writes */
Michal Kaziora4282492014-10-20 14:14:37 +02002795 ath10k_pci_enable_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002796
Kalle Valo0399eca2014-03-28 09:32:21 +02002797 mdelay(10);
2798 } while (time_before(jiffies, timeout));
2799
Michal Kaziora4282492014-10-20 14:14:37 +02002800 ath10k_pci_disable_and_clear_legacy_irq(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02002801 ath10k_pci_irq_msi_fw_mask(ar);
Michal Kaziora4282492014-10-20 14:14:37 +02002802
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002803 if (val == 0xffffffff) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002804 ath10k_err(ar, "failed to read device register, device is gone\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002805 return -EIO;
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002806 }
2807
Michal Kazior7710cd22014-04-23 19:30:04 +03002808 if (val & FW_IND_EVENT_PENDING) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002809 ath10k_warn(ar, "device has crashed during init\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002810 return -ECOMM;
Michal Kazior7710cd22014-04-23 19:30:04 +03002811 }
2812
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002813 if (!(val & FW_IND_INITIALIZED)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002814 ath10k_err(ar, "failed to receive initialized event from target: %08x\n",
Kalle Valo0399eca2014-03-28 09:32:21 +02002815 val);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002816 return -ETIMEDOUT;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002817 }
2818
Michal Kazior7aa7a722014-08-25 12:09:38 +02002819 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target initialised\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002820 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002821}
2822
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002823static int ath10k_pci_cold_reset(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002824{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002825 u32 val;
2826
Michal Kazior7aa7a722014-08-25 12:09:38 +02002827 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cold reset\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002828
Ben Greearf51dbe72014-09-29 14:41:46 +03002829 spin_lock_bh(&ar->data_lock);
2830
2831 ar->stats.fw_cold_reset_counter++;
2832
2833 spin_unlock_bh(&ar->data_lock);
2834
Kalle Valo5e3dd152013-06-12 20:52:10 +03002835 /* Put Target, including PCIe, into RESET. */
Kalle Valoe479ed42013-09-01 10:01:53 +03002836 val = ath10k_pci_reg_read32(ar, SOC_GLOBAL_RESET_ADDRESS);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002837 val |= 1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002838 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002839
Vasanthakumar Thiagarajanacd19582015-07-10 14:31:20 +05302840 /* After writing into SOC_GLOBAL_RESET to put device into
2841 * reset and pulling out of reset pcie may not be stable
2842 * for any immediate pcie register access and cause bus error,
2843 * add delay before any pcie access request to fix this issue.
2844 */
2845 msleep(20);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002846
2847 /* Pull Target, including PCIe, out of RESET. */
2848 val &= ~1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002849 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002850
Vasanthakumar Thiagarajanacd19582015-07-10 14:31:20 +05302851 msleep(20);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002852
Michal Kazior7aa7a722014-08-25 12:09:38 +02002853 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cold reset complete\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02002854
Michal Kazior5b2589f2013-11-08 08:01:30 +01002855 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002856}
2857
Michal Kazior2986e3e2014-08-07 11:03:30 +02002858static int ath10k_pci_claim(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002859{
Michal Kazior2986e3e2014-08-07 11:03:30 +02002860 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2861 struct pci_dev *pdev = ar_pci->pdev;
Michal Kazior2986e3e2014-08-07 11:03:30 +02002862 int ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002863
2864 pci_set_drvdata(pdev, ar);
2865
Kalle Valo5e3dd152013-06-12 20:52:10 +03002866 ret = pci_enable_device(pdev);
2867 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002868 ath10k_err(ar, "failed to enable pci device: %d\n", ret);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002869 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002870 }
2871
Kalle Valo5e3dd152013-06-12 20:52:10 +03002872 ret = pci_request_region(pdev, BAR_NUM, "ath");
2873 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002874 ath10k_err(ar, "failed to request region BAR%d: %d\n", BAR_NUM,
Michal Kazior2986e3e2014-08-07 11:03:30 +02002875 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002876 goto err_device;
2877 }
2878
Michal Kazior2986e3e2014-08-07 11:03:30 +02002879 /* Target expects 32 bit DMA. Enforce it. */
Kalle Valo5e3dd152013-06-12 20:52:10 +03002880 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2881 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002882 ath10k_err(ar, "failed to set dma mask to 32-bit: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002883 goto err_region;
2884 }
2885
2886 ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2887 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002888 ath10k_err(ar, "failed to set consistent dma mask to 32-bit: %d\n",
Michal Kazior2986e3e2014-08-07 11:03:30 +02002889 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002890 goto err_region;
2891 }
2892
Kalle Valo5e3dd152013-06-12 20:52:10 +03002893 pci_set_master(pdev);
2894
Kalle Valo5e3dd152013-06-12 20:52:10 +03002895 /* Arrange for access to Target SoC registers. */
Michal Kazioraeae5b42015-06-15 14:46:42 +03002896 ar_pci->mem_len = pci_resource_len(pdev, BAR_NUM);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002897 ar_pci->mem = pci_iomap(pdev, BAR_NUM, 0);
2898 if (!ar_pci->mem) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002899 ath10k_err(ar, "failed to iomap BAR%d\n", BAR_NUM);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002900 ret = -EIO;
2901 goto err_master;
2902 }
2903
Michal Kazior7aa7a722014-08-25 12:09:38 +02002904 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot pci_mem 0x%p\n", ar_pci->mem);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002905 return 0;
2906
2907err_master:
2908 pci_clear_master(pdev);
2909
2910err_region:
2911 pci_release_region(pdev, BAR_NUM);
2912
2913err_device:
2914 pci_disable_device(pdev);
2915
2916 return ret;
2917}
2918
2919static void ath10k_pci_release(struct ath10k *ar)
2920{
2921 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2922 struct pci_dev *pdev = ar_pci->pdev;
2923
2924 pci_iounmap(pdev, ar_pci->mem);
2925 pci_release_region(pdev, BAR_NUM);
2926 pci_clear_master(pdev);
2927 pci_disable_device(pdev);
2928}
2929
Michal Kazior7505f7c2014-12-02 10:55:54 +02002930static bool ath10k_pci_chip_is_supported(u32 dev_id, u32 chip_id)
2931{
2932 const struct ath10k_pci_supp_chip *supp_chip;
2933 int i;
2934 u32 rev_id = MS(chip_id, SOC_CHIP_ID_REV);
2935
2936 for (i = 0; i < ARRAY_SIZE(ath10k_pci_supp_chips); i++) {
2937 supp_chip = &ath10k_pci_supp_chips[i];
2938
2939 if (supp_chip->dev_id == dev_id &&
2940 supp_chip->rev_id == rev_id)
2941 return true;
2942 }
2943
2944 return false;
2945}
2946
Kalle Valo5e3dd152013-06-12 20:52:10 +03002947static int ath10k_pci_probe(struct pci_dev *pdev,
2948 const struct pci_device_id *pci_dev)
2949{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002950 int ret = 0;
2951 struct ath10k *ar;
2952 struct ath10k_pci *ar_pci;
Michal Kaziord63955b2015-01-24 12:14:49 +02002953 enum ath10k_hw_rev hw_rev;
Michal Kazior2986e3e2014-08-07 11:03:30 +02002954 u32 chip_id;
Anilkumar Kolli1aaf8ef2015-10-16 15:54:51 +03002955 bool pci_ps;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002956
Michal Kaziord63955b2015-01-24 12:14:49 +02002957 switch (pci_dev->device) {
2958 case QCA988X_2_0_DEVICE_ID:
2959 hw_rev = ATH10K_HW_QCA988X;
Anilkumar Kolli1aaf8ef2015-10-16 15:54:51 +03002960 pci_ps = false;
Michal Kaziord63955b2015-01-24 12:14:49 +02002961 break;
Michal Kazior36582e52015-08-13 14:32:26 +02002962 case QCA6164_2_1_DEVICE_ID:
Michal Kaziord63955b2015-01-24 12:14:49 +02002963 case QCA6174_2_1_DEVICE_ID:
2964 hw_rev = ATH10K_HW_QCA6174;
Anilkumar Kolli1aaf8ef2015-10-16 15:54:51 +03002965 pci_ps = true;
Michal Kaziord63955b2015-01-24 12:14:49 +02002966 break;
Vasanthakumar Thiagarajan8bd47022015-06-18 12:31:03 +05302967 case QCA99X0_2_0_DEVICE_ID:
2968 hw_rev = ATH10K_HW_QCA99X0;
Anilkumar Kolli1aaf8ef2015-10-16 15:54:51 +03002969 pci_ps = false;
Vasanthakumar Thiagarajan8bd47022015-06-18 12:31:03 +05302970 break;
Bartosz Markowskia226b512015-10-28 15:09:53 +01002971 case QCA9377_1_0_DEVICE_ID:
2972 hw_rev = ATH10K_HW_QCA9377;
2973 pci_ps = true;
2974 break;
Michal Kaziord63955b2015-01-24 12:14:49 +02002975 default:
2976 WARN_ON(1);
2977 return -ENOTSUPP;
2978 }
2979
2980 ar = ath10k_core_create(sizeof(*ar_pci), &pdev->dev, ATH10K_BUS_PCI,
2981 hw_rev, &ath10k_pci_hif_ops);
Michal Kaziore7b54192014-08-07 11:03:27 +02002982 if (!ar) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002983 dev_err(&pdev->dev, "failed to allocate core\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002984 return -ENOMEM;
Michal Kaziore7b54192014-08-07 11:03:27 +02002985 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002986
Manikanta Pubbisetty0a51b342015-10-09 11:55:58 +03002987 ath10k_dbg(ar, ATH10K_DBG_BOOT, "pci probe %04x:%04x %04x:%04x\n",
2988 pdev->vendor, pdev->device,
2989 pdev->subsystem_vendor, pdev->subsystem_device);
Michal Kazior7aa7a722014-08-25 12:09:38 +02002990
Michal Kaziore7b54192014-08-07 11:03:27 +02002991 ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002992 ar_pci->pdev = pdev;
2993 ar_pci->dev = &pdev->dev;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002994 ar_pci->ar = ar;
Michal Kazior36582e52015-08-13 14:32:26 +02002995 ar->dev_id = pci_dev->device;
Anilkumar Kolli1aaf8ef2015-10-16 15:54:51 +03002996 ar_pci->pci_ps = pci_ps;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002997
Manikanta Pubbisetty0a51b342015-10-09 11:55:58 +03002998 ar->id.vendor = pdev->vendor;
2999 ar->id.device = pdev->device;
3000 ar->id.subsystem_vendor = pdev->subsystem_vendor;
3001 ar->id.subsystem_device = pdev->subsystem_device;
Michal Kaziorde57e2c2015-04-17 09:19:17 +00003002
Kalle Valo5e3dd152013-06-12 20:52:10 +03003003 spin_lock_init(&ar_pci->ce_lock);
Michal Kazior77258d42015-05-18 09:38:18 +00003004 spin_lock_init(&ar_pci->ps_lock);
3005
Michal Kazior728f95e2014-08-22 14:33:14 +02003006 setup_timer(&ar_pci->rx_post_retry, ath10k_pci_rx_replenish_retry,
3007 (unsigned long)ar);
Michal Kazior77258d42015-05-18 09:38:18 +00003008 setup_timer(&ar_pci->ps_timer, ath10k_pci_ps_timer,
3009 (unsigned long)ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003010
Michal Kazior2986e3e2014-08-07 11:03:30 +02003011 ret = ath10k_pci_claim(ar);
Kalle Valoe01ae682013-09-01 11:22:14 +03003012 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003013 ath10k_err(ar, "failed to claim device: %d\n", ret);
Michal Kaziore7b54192014-08-07 11:03:27 +02003014 goto err_core_destroy;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003015 }
3016
Michal Kazior84cbf3a2014-10-20 14:14:39 +02003017 ret = ath10k_pci_alloc_pipes(ar);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02003018 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003019 ath10k_err(ar, "failed to allocate copy engine pipes: %d\n",
3020 ret);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02003021 goto err_sleep;
Michal Kazior25d0dbc2014-03-28 10:02:38 +02003022 }
3023
Michal Kazior403d6272014-08-22 14:23:31 +02003024 ath10k_pci_ce_deinit(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02003025 ath10k_pci_irq_disable(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02003026
Anilkumar Kolli1aaf8ef2015-10-16 15:54:51 +03003027 if (ar_pci->pci_ps == 0) {
3028 ret = ath10k_pci_force_wake(ar);
3029 if (ret) {
3030 ath10k_warn(ar, "failed to wake up device : %d\n", ret);
3031 goto err_free_pipes;
3032 }
3033 }
3034
Michal Kazior403d6272014-08-22 14:23:31 +02003035 ret = ath10k_pci_init_irq(ar);
3036 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003037 ath10k_err(ar, "failed to init irqs: %d\n", ret);
Michal Kazior84cbf3a2014-10-20 14:14:39 +02003038 goto err_free_pipes;
Michal Kazior403d6272014-08-22 14:23:31 +02003039 }
3040
Michal Kazior7aa7a722014-08-25 12:09:38 +02003041 ath10k_info(ar, "pci irq %s interrupts %d irq_mode %d reset_mode %d\n",
Michal Kazior403d6272014-08-22 14:23:31 +02003042 ath10k_pci_get_irq_method(ar), ar_pci->num_msi_intrs,
3043 ath10k_pci_irq_mode, ath10k_pci_reset_mode);
3044
Michal Kazior5c771e72014-08-22 14:23:34 +02003045 ret = ath10k_pci_request_irq(ar);
Michal Kazior403d6272014-08-22 14:23:31 +02003046 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003047 ath10k_warn(ar, "failed to request irqs: %d\n", ret);
Michal Kazior403d6272014-08-22 14:23:31 +02003048 goto err_deinit_irq;
3049 }
3050
Michal Kazior1a7fecb2015-01-24 12:14:48 +02003051 ret = ath10k_pci_chip_reset(ar);
3052 if (ret) {
3053 ath10k_err(ar, "failed to reset chip: %d\n", ret);
3054 goto err_free_irq;
3055 }
3056
3057 chip_id = ath10k_pci_soc_read32(ar, SOC_CHIP_ID_ADDRESS);
3058 if (chip_id == 0xffffffff) {
3059 ath10k_err(ar, "failed to get chip id\n");
3060 goto err_free_irq;
3061 }
3062
3063 if (!ath10k_pci_chip_is_supported(pdev->device, chip_id)) {
3064 ath10k_err(ar, "device %04x with chip_id %08x isn't supported\n",
3065 pdev->device, chip_id);
Michal Kaziord9585a92015-04-10 13:01:27 +00003066 goto err_free_irq;
Michal Kazior1a7fecb2015-01-24 12:14:48 +02003067 }
3068
Kalle Valoe01ae682013-09-01 11:22:14 +03003069 ret = ath10k_core_register(ar, chip_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003070 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003071 ath10k_err(ar, "failed to register driver core: %d\n", ret);
Michal Kazior5c771e72014-08-22 14:23:34 +02003072 goto err_free_irq;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003073 }
3074
3075 return 0;
3076
Michal Kazior5c771e72014-08-22 14:23:34 +02003077err_free_irq:
3078 ath10k_pci_free_irq(ar);
Michal Kazior21396272014-08-28 10:24:40 +02003079 ath10k_pci_kill_tasklet(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02003080
Michal Kazior403d6272014-08-22 14:23:31 +02003081err_deinit_irq:
3082 ath10k_pci_deinit_irq(ar);
3083
Michal Kazior84cbf3a2014-10-20 14:14:39 +02003084err_free_pipes:
3085 ath10k_pci_free_pipes(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02003086
Michal Kaziorc0c378f2014-08-07 11:03:28 +02003087err_sleep:
Michal Kazior0bcbbe62015-05-29 07:35:24 +02003088 ath10k_pci_sleep_sync(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02003089 ath10k_pci_release(ar);
3090
Michal Kaziore7b54192014-08-07 11:03:27 +02003091err_core_destroy:
Kalle Valo5e3dd152013-06-12 20:52:10 +03003092 ath10k_core_destroy(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003093
3094 return ret;
3095}
3096
3097static void ath10k_pci_remove(struct pci_dev *pdev)
3098{
3099 struct ath10k *ar = pci_get_drvdata(pdev);
3100 struct ath10k_pci *ar_pci;
3101
Michal Kazior7aa7a722014-08-25 12:09:38 +02003102 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci remove\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03003103
3104 if (!ar)
3105 return;
3106
3107 ar_pci = ath10k_pci_priv(ar);
3108
3109 if (!ar_pci)
3110 return;
3111
Kalle Valo5e3dd152013-06-12 20:52:10 +03003112 ath10k_core_unregister(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02003113 ath10k_pci_free_irq(ar);
Michal Kazior21396272014-08-28 10:24:40 +02003114 ath10k_pci_kill_tasklet(ar);
Michal Kazior403d6272014-08-22 14:23:31 +02003115 ath10k_pci_deinit_irq(ar);
3116 ath10k_pci_ce_deinit(ar);
Michal Kazior84cbf3a2014-10-20 14:14:39 +02003117 ath10k_pci_free_pipes(ar);
Michal Kazior77258d42015-05-18 09:38:18 +00003118 ath10k_pci_sleep_sync(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02003119 ath10k_pci_release(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003120 ath10k_core_destroy(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003121}
3122
Kalle Valo5e3dd152013-06-12 20:52:10 +03003123MODULE_DEVICE_TABLE(pci, ath10k_pci_id_table);
3124
3125static struct pci_driver ath10k_pci_driver = {
3126 .name = "ath10k_pci",
3127 .id_table = ath10k_pci_id_table,
3128 .probe = ath10k_pci_probe,
3129 .remove = ath10k_pci_remove,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003130};
3131
3132static int __init ath10k_pci_init(void)
3133{
3134 int ret;
3135
3136 ret = pci_register_driver(&ath10k_pci_driver);
3137 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003138 printk(KERN_ERR "failed to register ath10k pci driver: %d\n",
3139 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003140
3141 return ret;
3142}
3143module_init(ath10k_pci_init);
3144
3145static void __exit ath10k_pci_exit(void)
3146{
3147 pci_unregister_driver(&ath10k_pci_driver);
3148}
3149
3150module_exit(ath10k_pci_exit);
3151
3152MODULE_AUTHOR("Qualcomm Atheros");
3153MODULE_DESCRIPTION("Driver support for Atheros QCA988X PCIe devices");
3154MODULE_LICENSE("Dual BSD/GPL");
Bartosz Markowski5c427f52015-02-18 13:16:37 +01003155
3156/* QCA988x 2.0 firmware files */
Bartosz Markowski8026cae2014-10-06 14:16:41 +02003157MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_FW_FILE);
3158MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API2_FILE);
3159MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API3_FILE);
Bartosz Markowski5c427f52015-02-18 13:16:37 +01003160MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API4_FILE);
Kalle Valo53513c32015-03-25 13:12:42 +02003161MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API5_FILE);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003162MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_BOARD_DATA_FILE);
Manikanta Pubbisetty0a51b342015-10-09 11:55:58 +03003163MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_BOARD_API2_FILE);
Bartosz Markowski5c427f52015-02-18 13:16:37 +01003164
3165/* QCA6174 2.1 firmware files */
3166MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" ATH10K_FW_API4_FILE);
Michal Kaziore451c1d2015-05-26 13:09:22 +02003167MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" ATH10K_FW_API5_FILE);
Bartosz Markowski5c427f52015-02-18 13:16:37 +01003168MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" QCA6174_HW_2_1_BOARD_DATA_FILE);
Manikanta Pubbisetty0a51b342015-10-09 11:55:58 +03003169MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" ATH10K_BOARD_API2_FILE);
Bartosz Markowski5c427f52015-02-18 13:16:37 +01003170
3171/* QCA6174 3.1 firmware files */
3172MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" ATH10K_FW_API4_FILE);
Michal Kaziore451c1d2015-05-26 13:09:22 +02003173MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" ATH10K_FW_API5_FILE);
Bartosz Markowski5c427f52015-02-18 13:16:37 +01003174MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" QCA6174_HW_3_0_BOARD_DATA_FILE);
Manikanta Pubbisetty0a51b342015-10-09 11:55:58 +03003175MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" ATH10K_BOARD_API2_FILE);
Bartosz Markowskia226b512015-10-28 15:09:53 +01003176
3177/* QCA9377 1.0 firmware files */
3178MODULE_FIRMWARE(QCA9377_HW_1_0_FW_DIR "/" ATH10K_FW_API5_FILE);
3179MODULE_FIRMWARE(QCA9377_HW_1_0_FW_DIR "/" QCA9377_HW_1_0_BOARD_DATA_FILE);