blob: 8be07c653b2d0b4e14ef71b4ba090543b61331a5 [file] [log] [blame]
Kalle Valo5e3dd152013-06-12 20:52:10 +03001/*
2 * Copyright (c) 2005-2011 Atheros Communications Inc.
3 * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <linux/pci.h>
19#include <linux/module.h>
20#include <linux/interrupt.h>
21#include <linux/spinlock.h>
Kalle Valo650b91f2013-11-20 10:00:49 +020022#include <linux/bitops.h>
Kalle Valo5e3dd152013-06-12 20:52:10 +030023
24#include "core.h"
25#include "debug.h"
26
27#include "targaddrs.h"
28#include "bmi.h"
29
30#include "hif.h"
31#include "htc.h"
32
33#include "ce.h"
34#include "pci.h"
35
Michal Kaziorcfe9c452013-11-25 14:06:27 +010036enum ath10k_pci_irq_mode {
37 ATH10K_PCI_IRQ_AUTO = 0,
38 ATH10K_PCI_IRQ_LEGACY = 1,
39 ATH10K_PCI_IRQ_MSI = 2,
40};
41
Kalle Valo35098462014-03-28 09:32:27 +020042enum ath10k_pci_reset_mode {
43 ATH10K_PCI_RESET_AUTO = 0,
44 ATH10K_PCI_RESET_WARM_ONLY = 1,
45};
46
Michal Kaziorcfe9c452013-11-25 14:06:27 +010047static unsigned int ath10k_pci_irq_mode = ATH10K_PCI_IRQ_AUTO;
Kalle Valo35098462014-03-28 09:32:27 +020048static unsigned int ath10k_pci_reset_mode = ATH10K_PCI_RESET_AUTO;
Michal Kaziorcfe9c452013-11-25 14:06:27 +010049
Michal Kaziorcfe9c452013-11-25 14:06:27 +010050module_param_named(irq_mode, ath10k_pci_irq_mode, uint, 0644);
51MODULE_PARM_DESC(irq_mode, "0: auto, 1: legacy, 2: msi (default: 0)");
52
Kalle Valo35098462014-03-28 09:32:27 +020053module_param_named(reset_mode, ath10k_pci_reset_mode, uint, 0644);
54MODULE_PARM_DESC(reset_mode, "0: auto, 1: warm only (default: 0)");
55
Kalle Valo0399eca2014-03-28 09:32:21 +020056/* how long wait to wait for target to initialise, in ms */
57#define ATH10K_PCI_TARGET_WAIT 3000
Michal Kazior61c95ce2014-05-14 16:56:16 +030058#define ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS 3
Kalle Valo0399eca2014-03-28 09:32:21 +020059
Kalle Valo5e3dd152013-06-12 20:52:10 +030060#define QCA988X_2_0_DEVICE_ID (0x003c)
Michal Kaziord63955b2015-01-24 12:14:49 +020061#define QCA6174_2_1_DEVICE_ID (0x003e)
Kalle Valo5e3dd152013-06-12 20:52:10 +030062
Benoit Taine9baa3c32014-08-08 15:56:03 +020063static const struct pci_device_id ath10k_pci_id_table[] = {
Kalle Valo5e3dd152013-06-12 20:52:10 +030064 { PCI_VDEVICE(ATHEROS, QCA988X_2_0_DEVICE_ID) }, /* PCI-E QCA988X V2 */
Michal Kaziord63955b2015-01-24 12:14:49 +020065 { PCI_VDEVICE(ATHEROS, QCA6174_2_1_DEVICE_ID) }, /* PCI-E QCA6174 V2.1 */
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 Kaziord63955b2015-01-24 12:14:49 +020075 { QCA6174_2_1_DEVICE_ID, QCA6174_HW_2_1_CHIP_ID_REV },
76 { QCA6174_2_1_DEVICE_ID, QCA6174_HW_2_2_CHIP_ID_REV },
77 { QCA6174_2_1_DEVICE_ID, QCA6174_HW_3_0_CHIP_ID_REV },
78 { QCA6174_2_1_DEVICE_ID, QCA6174_HW_3_1_CHIP_ID_REV },
79 { QCA6174_2_1_DEVICE_ID, QCA6174_HW_3_2_CHIP_ID_REV },
Michal Kazior7505f7c2014-12-02 10:55:54 +020080};
81
Michal Kazior728f95e2014-08-22 14:33:14 +020082static void ath10k_pci_buffer_cleanup(struct ath10k *ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +010083static int ath10k_pci_cold_reset(struct ath10k *ar);
84static int ath10k_pci_warm_reset(struct ath10k *ar);
Michal Kaziord7fb47f2013-11-08 08:01:26 +010085static int ath10k_pci_wait_for_target_init(struct ath10k *ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +010086static int ath10k_pci_init_irq(struct ath10k *ar);
87static int ath10k_pci_deinit_irq(struct ath10k *ar);
88static int ath10k_pci_request_irq(struct ath10k *ar);
89static void ath10k_pci_free_irq(struct ath10k *ar);
Michal Kazior85622cd2013-11-25 14:06:22 +010090static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
91 struct ath10k_ce_pipe *rx_pipe,
92 struct bmi_xfer *xfer);
Kalle Valo5e3dd152013-06-12 20:52:10 +030093
94static const struct ce_attr host_ce_config_wlan[] = {
Kalle Valo48e9c222013-09-01 10:01:32 +030095 /* CE0: host->target HTC control and raw streams */
96 {
97 .flags = CE_ATTR_FLAGS,
98 .src_nentries = 16,
99 .src_sz_max = 256,
100 .dest_nentries = 0,
101 },
102
103 /* CE1: target->host HTT + HTC control */
104 {
105 .flags = CE_ATTR_FLAGS,
106 .src_nentries = 0,
Michal Kazior63838642015-02-09 15:04:55 +0100107 .src_sz_max = 2048,
Kalle Valo48e9c222013-09-01 10:01:32 +0300108 .dest_nentries = 512,
109 },
110
111 /* CE2: target->host WMI */
112 {
113 .flags = CE_ATTR_FLAGS,
114 .src_nentries = 0,
115 .src_sz_max = 2048,
Rajkumar Manoharan30abb332015-03-04 15:43:44 +0200116 .dest_nentries = 128,
Kalle Valo48e9c222013-09-01 10:01:32 +0300117 },
118
119 /* CE3: host->target WMI */
120 {
121 .flags = CE_ATTR_FLAGS,
122 .src_nentries = 32,
123 .src_sz_max = 2048,
124 .dest_nentries = 0,
125 },
126
127 /* CE4: host->target HTT */
128 {
129 .flags = CE_ATTR_FLAGS | CE_ATTR_DIS_INTR,
130 .src_nentries = CE_HTT_H2T_MSG_SRC_NENTRIES,
131 .src_sz_max = 256,
132 .dest_nentries = 0,
133 },
134
135 /* CE5: unused */
136 {
137 .flags = CE_ATTR_FLAGS,
138 .src_nentries = 0,
139 .src_sz_max = 0,
140 .dest_nentries = 0,
141 },
142
143 /* CE6: target autonomous hif_memcpy */
144 {
145 .flags = CE_ATTR_FLAGS,
146 .src_nentries = 0,
147 .src_sz_max = 0,
148 .dest_nentries = 0,
149 },
150
151 /* CE7: ce_diag, the Diagnostic Window */
152 {
153 .flags = CE_ATTR_FLAGS,
154 .src_nentries = 2,
155 .src_sz_max = DIAG_TRANSFER_LIMIT,
156 .dest_nentries = 2,
157 },
Kalle Valo5e3dd152013-06-12 20:52:10 +0300158};
159
160/* Target firmware's Copy Engine configuration. */
161static const struct ce_pipe_config target_ce_config_wlan[] = {
Kalle Valod88effb2013-09-01 10:01:39 +0300162 /* CE0: host->target HTC control and raw streams */
163 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300164 .pipenum = __cpu_to_le32(0),
165 .pipedir = __cpu_to_le32(PIPEDIR_OUT),
166 .nentries = __cpu_to_le32(32),
167 .nbytes_max = __cpu_to_le32(256),
168 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
169 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300170 },
171
172 /* CE1: target->host HTT + HTC control */
173 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300174 .pipenum = __cpu_to_le32(1),
175 .pipedir = __cpu_to_le32(PIPEDIR_IN),
176 .nentries = __cpu_to_le32(32),
Michal Kazior63838642015-02-09 15:04:55 +0100177 .nbytes_max = __cpu_to_le32(2048),
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300178 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
179 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300180 },
181
182 /* CE2: target->host WMI */
183 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300184 .pipenum = __cpu_to_le32(2),
185 .pipedir = __cpu_to_le32(PIPEDIR_IN),
Rajkumar Manoharan30abb332015-03-04 15:43:44 +0200186 .nentries = __cpu_to_le32(64),
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300187 .nbytes_max = __cpu_to_le32(2048),
188 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
189 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300190 },
191
192 /* CE3: host->target WMI */
193 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300194 .pipenum = __cpu_to_le32(3),
195 .pipedir = __cpu_to_le32(PIPEDIR_OUT),
196 .nentries = __cpu_to_le32(32),
197 .nbytes_max = __cpu_to_le32(2048),
198 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
199 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300200 },
201
202 /* CE4: host->target HTT */
203 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300204 .pipenum = __cpu_to_le32(4),
205 .pipedir = __cpu_to_le32(PIPEDIR_OUT),
206 .nentries = __cpu_to_le32(256),
207 .nbytes_max = __cpu_to_le32(256),
208 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
209 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300210 },
211
Kalle Valo5e3dd152013-06-12 20:52:10 +0300212 /* NB: 50% of src nentries, since tx has 2 frags */
Kalle Valod88effb2013-09-01 10:01:39 +0300213
214 /* CE5: unused */
215 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300216 .pipenum = __cpu_to_le32(5),
217 .pipedir = __cpu_to_le32(PIPEDIR_OUT),
218 .nentries = __cpu_to_le32(32),
219 .nbytes_max = __cpu_to_le32(2048),
220 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
221 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300222 },
223
224 /* CE6: Reserved for target autonomous hif_memcpy */
225 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300226 .pipenum = __cpu_to_le32(6),
227 .pipedir = __cpu_to_le32(PIPEDIR_INOUT),
228 .nentries = __cpu_to_le32(32),
229 .nbytes_max = __cpu_to_le32(4096),
230 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
231 .reserved = __cpu_to_le32(0),
Kalle Valod88effb2013-09-01 10:01:39 +0300232 },
233
Kalle Valo5e3dd152013-06-12 20:52:10 +0300234 /* CE7 used only by Host */
235};
236
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300237/*
238 * Map from service/endpoint to Copy Engine.
239 * This table is derived from the CE_PCI TABLE, above.
240 * It is passed to the Target at startup for use by firmware.
241 */
242static const struct service_to_pipe target_service_to_ce_map_wlan[] = {
243 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300244 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VO),
245 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
246 __cpu_to_le32(3),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300247 },
248 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300249 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VO),
250 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
251 __cpu_to_le32(2),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300252 },
253 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300254 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BK),
255 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
256 __cpu_to_le32(3),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300257 },
258 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300259 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BK),
260 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
261 __cpu_to_le32(2),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300262 },
263 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300264 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BE),
265 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
266 __cpu_to_le32(3),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300267 },
268 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300269 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BE),
270 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
271 __cpu_to_le32(2),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300272 },
273 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300274 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VI),
275 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
276 __cpu_to_le32(3),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300277 },
278 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300279 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VI),
280 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
281 __cpu_to_le32(2),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300282 },
283 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300284 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_CONTROL),
285 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
286 __cpu_to_le32(3),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300287 },
288 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300289 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_CONTROL),
290 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
291 __cpu_to_le32(2),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300292 },
293 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300294 __cpu_to_le32(ATH10K_HTC_SVC_ID_RSVD_CTRL),
295 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
296 __cpu_to_le32(0),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300297 },
298 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300299 __cpu_to_le32(ATH10K_HTC_SVC_ID_RSVD_CTRL),
300 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
301 __cpu_to_le32(1),
302 },
303 { /* not used */
304 __cpu_to_le32(ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS),
305 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
306 __cpu_to_le32(0),
307 },
308 { /* not used */
309 __cpu_to_le32(ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS),
310 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
311 __cpu_to_le32(1),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300312 },
313 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300314 __cpu_to_le32(ATH10K_HTC_SVC_ID_HTT_DATA_MSG),
315 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
316 __cpu_to_le32(4),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300317 },
318 {
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300319 __cpu_to_le32(ATH10K_HTC_SVC_ID_HTT_DATA_MSG),
320 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
321 __cpu_to_le32(1),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300322 },
323
324 /* (Additions here) */
325
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300326 { /* must be last */
327 __cpu_to_le32(0),
328 __cpu_to_le32(0),
329 __cpu_to_le32(0),
Michal Kaziord7bfb7a2014-08-26 19:14:02 +0300330 },
331};
332
Michal Kaziore5398872013-11-25 14:06:20 +0100333static bool ath10k_pci_irq_pending(struct ath10k *ar)
334{
335 u32 cause;
336
337 /* Check if the shared legacy irq is for us */
338 cause = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
339 PCIE_INTR_CAUSE_ADDRESS);
340 if (cause & (PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL))
341 return true;
342
343 return false;
344}
345
Michal Kazior26852182013-11-25 14:06:25 +0100346static void ath10k_pci_disable_and_clear_legacy_irq(struct ath10k *ar)
347{
348 /* IMPORTANT: INTR_CLR register has to be set after
349 * INTR_ENABLE is set to 0, otherwise interrupt can not be
350 * really cleared. */
351 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
352 0);
353 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_CLR_ADDRESS,
354 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
355
356 /* IMPORTANT: this extra read transaction is required to
357 * flush the posted write buffer. */
Kalle Valocfbc06a2014-09-14 12:50:23 +0300358 (void)ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
359 PCIE_INTR_ENABLE_ADDRESS);
Michal Kazior26852182013-11-25 14:06:25 +0100360}
361
362static void ath10k_pci_enable_legacy_irq(struct ath10k *ar)
363{
364 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
365 PCIE_INTR_ENABLE_ADDRESS,
366 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
367
368 /* IMPORTANT: this extra read transaction is required to
369 * flush the posted write buffer. */
Kalle Valocfbc06a2014-09-14 12:50:23 +0300370 (void)ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
371 PCIE_INTR_ENABLE_ADDRESS);
Michal Kazior26852182013-11-25 14:06:25 +0100372}
373
Michal Kazior403d6272014-08-22 14:23:31 +0200374static inline const char *ath10k_pci_get_irq_method(struct ath10k *ar)
Michal Kaziorab977bd2013-11-25 14:06:26 +0100375{
Michal Kaziorab977bd2013-11-25 14:06:26 +0100376 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
377
Michal Kazior403d6272014-08-22 14:23:31 +0200378 if (ar_pci->num_msi_intrs > 1)
379 return "msi-x";
Kalle Valod8bb26b2014-09-14 12:50:33 +0300380
381 if (ar_pci->num_msi_intrs == 1)
Michal Kazior403d6272014-08-22 14:23:31 +0200382 return "msi";
Kalle Valod8bb26b2014-09-14 12:50:33 +0300383
384 return "legacy";
Michal Kaziorab977bd2013-11-25 14:06:26 +0100385}
386
Michal Kazior728f95e2014-08-22 14:33:14 +0200387static int __ath10k_pci_rx_post_buf(struct ath10k_pci_pipe *pipe)
Michal Kaziorab977bd2013-11-25 14:06:26 +0100388{
Michal Kazior728f95e2014-08-22 14:33:14 +0200389 struct ath10k *ar = pipe->hif_ce_state;
Michal Kaziorab977bd2013-11-25 14:06:26 +0100390 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior728f95e2014-08-22 14:33:14 +0200391 struct ath10k_ce_pipe *ce_pipe = pipe->ce_hdl;
392 struct sk_buff *skb;
393 dma_addr_t paddr;
Michal Kaziorab977bd2013-11-25 14:06:26 +0100394 int ret;
395
Michal Kazior728f95e2014-08-22 14:33:14 +0200396 lockdep_assert_held(&ar_pci->ce_lock);
397
398 skb = dev_alloc_skb(pipe->buf_sz);
399 if (!skb)
400 return -ENOMEM;
401
402 WARN_ONCE((unsigned long)skb->data & 3, "unaligned skb");
403
404 paddr = dma_map_single(ar->dev, skb->data,
405 skb->len + skb_tailroom(skb),
406 DMA_FROM_DEVICE);
407 if (unlikely(dma_mapping_error(ar->dev, paddr))) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200408 ath10k_warn(ar, "failed to dma map pci rx buf\n");
Michal Kazior728f95e2014-08-22 14:33:14 +0200409 dev_kfree_skb_any(skb);
410 return -EIO;
411 }
412
Michal Kazior8582bf32015-01-24 12:14:47 +0200413 ATH10K_SKB_RXCB(skb)->paddr = paddr;
Michal Kazior728f95e2014-08-22 14:33:14 +0200414
415 ret = __ath10k_ce_rx_post_buf(ce_pipe, skb, paddr);
Michal Kaziorab977bd2013-11-25 14:06:26 +0100416 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200417 ath10k_warn(ar, "failed to post pci rx buf: %d\n", ret);
Michal Kazior728f95e2014-08-22 14:33:14 +0200418 dma_unmap_single(ar->dev, paddr, skb->len + skb_tailroom(skb),
419 DMA_FROM_DEVICE);
420 dev_kfree_skb_any(skb);
Michal Kaziorab977bd2013-11-25 14:06:26 +0100421 return ret;
422 }
423
424 return 0;
425}
426
Michal Kazior728f95e2014-08-22 14:33:14 +0200427static void __ath10k_pci_rx_post_pipe(struct ath10k_pci_pipe *pipe)
Michal Kaziorab977bd2013-11-25 14:06:26 +0100428{
Michal Kazior728f95e2014-08-22 14:33:14 +0200429 struct ath10k *ar = pipe->hif_ce_state;
430 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
431 struct ath10k_ce_pipe *ce_pipe = pipe->ce_hdl;
432 int ret, num;
433
434 lockdep_assert_held(&ar_pci->ce_lock);
435
436 if (pipe->buf_sz == 0)
437 return;
438
439 if (!ce_pipe->dest_ring)
440 return;
441
442 num = __ath10k_ce_rx_num_free_bufs(ce_pipe);
443 while (num--) {
444 ret = __ath10k_pci_rx_post_buf(pipe);
445 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200446 ath10k_warn(ar, "failed to post pci rx buf: %d\n", ret);
Michal Kazior728f95e2014-08-22 14:33:14 +0200447 mod_timer(&ar_pci->rx_post_retry, jiffies +
448 ATH10K_PCI_RX_POST_RETRY_MS);
449 break;
450 }
451 }
452}
453
454static void ath10k_pci_rx_post_pipe(struct ath10k_pci_pipe *pipe)
455{
456 struct ath10k *ar = pipe->hif_ce_state;
457 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
458
459 spin_lock_bh(&ar_pci->ce_lock);
460 __ath10k_pci_rx_post_pipe(pipe);
461 spin_unlock_bh(&ar_pci->ce_lock);
462}
463
464static void ath10k_pci_rx_post(struct ath10k *ar)
465{
466 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
467 int i;
468
469 spin_lock_bh(&ar_pci->ce_lock);
470 for (i = 0; i < CE_COUNT; i++)
471 __ath10k_pci_rx_post_pipe(&ar_pci->pipe_info[i]);
472 spin_unlock_bh(&ar_pci->ce_lock);
473}
474
475static void ath10k_pci_rx_replenish_retry(unsigned long ptr)
476{
477 struct ath10k *ar = (void *)ptr;
478
479 ath10k_pci_rx_post(ar);
Michal Kaziorab977bd2013-11-25 14:06:26 +0100480}
481
Kalle Valo5e3dd152013-06-12 20:52:10 +0300482/*
483 * Diagnostic read/write access is provided for startup/config/debug usage.
484 * Caller must guarantee proper alignment, when applicable, and single user
485 * at any moment.
486 */
487static int ath10k_pci_diag_read_mem(struct ath10k *ar, u32 address, void *data,
488 int nbytes)
489{
490 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
491 int ret = 0;
492 u32 buf;
493 unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
494 unsigned int id;
495 unsigned int flags;
Michal Kazior2aa39112013-08-27 13:08:02 +0200496 struct ath10k_ce_pipe *ce_diag;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300497 /* Host buffer address in CE space */
498 u32 ce_data;
499 dma_addr_t ce_data_base = 0;
500 void *data_buf = NULL;
501 int i;
502
Kalle Valoeef25402014-09-24 14:16:52 +0300503 spin_lock_bh(&ar_pci->ce_lock);
504
Kalle Valo5e3dd152013-06-12 20:52:10 +0300505 ce_diag = ar_pci->ce_diag;
506
507 /*
508 * Allocate a temporary bounce buffer to hold caller's data
509 * to be DMA'ed from Target. This guarantees
510 * 1) 4-byte alignment
511 * 2) Buffer in DMA-able space
512 */
513 orig_nbytes = nbytes;
Michal Kazior68c03242014-03-28 10:02:35 +0200514 data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
515 orig_nbytes,
516 &ce_data_base,
517 GFP_ATOMIC);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300518
519 if (!data_buf) {
520 ret = -ENOMEM;
521 goto done;
522 }
523 memset(data_buf, 0, orig_nbytes);
524
525 remaining_bytes = orig_nbytes;
526 ce_data = ce_data_base;
527 while (remaining_bytes) {
528 nbytes = min_t(unsigned int, remaining_bytes,
529 DIAG_TRANSFER_LIMIT);
530
Kalle Valoeef25402014-09-24 14:16:52 +0300531 ret = __ath10k_ce_rx_post_buf(ce_diag, NULL, ce_data);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300532 if (ret != 0)
533 goto done;
534
535 /* Request CE to send from Target(!) address to Host buffer */
536 /*
537 * The address supplied by the caller is in the
538 * Target CPU virtual address space.
539 *
540 * In order to use this address with the diagnostic CE,
541 * convert it from Target CPU virtual address space
542 * to CE address space
543 */
Kalle Valo5e3dd152013-06-12 20:52:10 +0300544 address = TARG_CPU_SPACE_TO_CE_SPACE(ar, ar_pci->mem,
545 address);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300546
Kalle Valoeef25402014-09-24 14:16:52 +0300547 ret = ath10k_ce_send_nolock(ce_diag, NULL, (u32)address, nbytes, 0,
548 0);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300549 if (ret)
550 goto done;
551
552 i = 0;
Kalle Valoeef25402014-09-24 14:16:52 +0300553 while (ath10k_ce_completed_send_next_nolock(ce_diag, NULL, &buf,
554 &completed_nbytes,
555 &id) != 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300556 mdelay(1);
557 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
558 ret = -EBUSY;
559 goto done;
560 }
561 }
562
563 if (nbytes != completed_nbytes) {
564 ret = -EIO;
565 goto done;
566 }
567
Kalle Valocfbc06a2014-09-14 12:50:23 +0300568 if (buf != (u32)address) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300569 ret = -EIO;
570 goto done;
571 }
572
573 i = 0;
Kalle Valoeef25402014-09-24 14:16:52 +0300574 while (ath10k_ce_completed_recv_next_nolock(ce_diag, NULL, &buf,
575 &completed_nbytes,
576 &id, &flags) != 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300577 mdelay(1);
578
579 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
580 ret = -EBUSY;
581 goto done;
582 }
583 }
584
585 if (nbytes != completed_nbytes) {
586 ret = -EIO;
587 goto done;
588 }
589
590 if (buf != ce_data) {
591 ret = -EIO;
592 goto done;
593 }
594
595 remaining_bytes -= nbytes;
596 address += nbytes;
597 ce_data += nbytes;
598 }
599
600done:
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300601 if (ret == 0)
602 memcpy(data, data_buf, orig_nbytes);
603 else
Michal Kazior7aa7a722014-08-25 12:09:38 +0200604 ath10k_warn(ar, "failed to read diag value at 0x%x: %d\n",
Kalle Valo50f87a62014-03-28 09:32:52 +0200605 address, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300606
607 if (data_buf)
Michal Kazior68c03242014-03-28 10:02:35 +0200608 dma_free_coherent(ar->dev, orig_nbytes, data_buf,
609 ce_data_base);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300610
Kalle Valoeef25402014-09-24 14:16:52 +0300611 spin_unlock_bh(&ar_pci->ce_lock);
612
Kalle Valo5e3dd152013-06-12 20:52:10 +0300613 return ret;
614}
615
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300616static int ath10k_pci_diag_read32(struct ath10k *ar, u32 address, u32 *value)
617{
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300618 __le32 val = 0;
619 int ret;
620
621 ret = ath10k_pci_diag_read_mem(ar, address, &val, sizeof(val));
622 *value = __le32_to_cpu(val);
623
624 return ret;
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300625}
626
627static int __ath10k_pci_diag_read_hi(struct ath10k *ar, void *dest,
628 u32 src, u32 len)
629{
630 u32 host_addr, addr;
631 int ret;
632
633 host_addr = host_interest_item_address(src);
634
635 ret = ath10k_pci_diag_read32(ar, host_addr, &addr);
636 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200637 ath10k_warn(ar, "failed to get memcpy hi address for firmware address %d: %d\n",
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300638 src, ret);
639 return ret;
640 }
641
642 ret = ath10k_pci_diag_read_mem(ar, addr, dest, len);
643 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200644 ath10k_warn(ar, "failed to memcpy firmware memory from %d (%d B): %d\n",
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300645 addr, len, ret);
646 return ret;
647 }
648
649 return 0;
650}
651
652#define ath10k_pci_diag_read_hi(ar, dest, src, len) \
Kalle Valo8cc7f262014-09-14 12:50:39 +0300653 __ath10k_pci_diag_read_hi(ar, dest, HI_ITEM(src), len)
Kalle Valo3d29a3e2014-08-25 08:37:26 +0300654
Kalle Valo5e3dd152013-06-12 20:52:10 +0300655static int ath10k_pci_diag_write_mem(struct ath10k *ar, u32 address,
656 const void *data, int nbytes)
657{
658 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
659 int ret = 0;
660 u32 buf;
661 unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
662 unsigned int id;
663 unsigned int flags;
Michal Kazior2aa39112013-08-27 13:08:02 +0200664 struct ath10k_ce_pipe *ce_diag;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300665 void *data_buf = NULL;
666 u32 ce_data; /* Host buffer address in CE space */
667 dma_addr_t ce_data_base = 0;
668 int i;
669
Kalle Valoeef25402014-09-24 14:16:52 +0300670 spin_lock_bh(&ar_pci->ce_lock);
671
Kalle Valo5e3dd152013-06-12 20:52:10 +0300672 ce_diag = ar_pci->ce_diag;
673
674 /*
675 * Allocate a temporary bounce buffer to hold caller's data
676 * to be DMA'ed to Target. This guarantees
677 * 1) 4-byte alignment
678 * 2) Buffer in DMA-able space
679 */
680 orig_nbytes = nbytes;
Michal Kazior68c03242014-03-28 10:02:35 +0200681 data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
682 orig_nbytes,
683 &ce_data_base,
684 GFP_ATOMIC);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300685 if (!data_buf) {
686 ret = -ENOMEM;
687 goto done;
688 }
689
690 /* Copy caller's data to allocated DMA buf */
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300691 memcpy(data_buf, data, orig_nbytes);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300692
693 /*
694 * The address supplied by the caller is in the
695 * Target CPU virtual address space.
696 *
697 * In order to use this address with the diagnostic CE,
698 * convert it from
699 * Target CPU virtual address space
700 * to
701 * CE address space
702 */
Kalle Valo5e3dd152013-06-12 20:52:10 +0300703 address = TARG_CPU_SPACE_TO_CE_SPACE(ar, ar_pci->mem, address);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300704
705 remaining_bytes = orig_nbytes;
706 ce_data = ce_data_base;
707 while (remaining_bytes) {
708 /* FIXME: check cast */
709 nbytes = min_t(int, remaining_bytes, DIAG_TRANSFER_LIMIT);
710
711 /* Set up to receive directly into Target(!) address */
Kalle Valoeef25402014-09-24 14:16:52 +0300712 ret = __ath10k_ce_rx_post_buf(ce_diag, NULL, address);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300713 if (ret != 0)
714 goto done;
715
716 /*
717 * Request CE to send caller-supplied data that
718 * was copied to bounce buffer to Target(!) address.
719 */
Kalle Valoeef25402014-09-24 14:16:52 +0300720 ret = ath10k_ce_send_nolock(ce_diag, NULL, (u32)ce_data,
721 nbytes, 0, 0);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300722 if (ret != 0)
723 goto done;
724
725 i = 0;
Kalle Valoeef25402014-09-24 14:16:52 +0300726 while (ath10k_ce_completed_send_next_nolock(ce_diag, NULL, &buf,
727 &completed_nbytes,
728 &id) != 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300729 mdelay(1);
730
731 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
732 ret = -EBUSY;
733 goto done;
734 }
735 }
736
737 if (nbytes != completed_nbytes) {
738 ret = -EIO;
739 goto done;
740 }
741
742 if (buf != ce_data) {
743 ret = -EIO;
744 goto done;
745 }
746
747 i = 0;
Kalle Valoeef25402014-09-24 14:16:52 +0300748 while (ath10k_ce_completed_recv_next_nolock(ce_diag, NULL, &buf,
749 &completed_nbytes,
750 &id, &flags) != 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300751 mdelay(1);
752
753 if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
754 ret = -EBUSY;
755 goto done;
756 }
757 }
758
759 if (nbytes != completed_nbytes) {
760 ret = -EIO;
761 goto done;
762 }
763
764 if (buf != address) {
765 ret = -EIO;
766 goto done;
767 }
768
769 remaining_bytes -= nbytes;
770 address += nbytes;
771 ce_data += nbytes;
772 }
773
774done:
775 if (data_buf) {
Michal Kazior68c03242014-03-28 10:02:35 +0200776 dma_free_coherent(ar->dev, orig_nbytes, data_buf,
777 ce_data_base);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300778 }
779
780 if (ret != 0)
Michal Kazior7aa7a722014-08-25 12:09:38 +0200781 ath10k_warn(ar, "failed to write diag value at 0x%x: %d\n",
Kalle Valo50f87a62014-03-28 09:32:52 +0200782 address, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300783
Kalle Valoeef25402014-09-24 14:16:52 +0300784 spin_unlock_bh(&ar_pci->ce_lock);
785
Kalle Valo5e3dd152013-06-12 20:52:10 +0300786 return ret;
787}
788
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300789static int ath10k_pci_diag_write32(struct ath10k *ar, u32 address, u32 value)
790{
791 __le32 val = __cpu_to_le32(value);
792
793 return ath10k_pci_diag_write_mem(ar, address, &val, sizeof(val));
794}
795
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200796static bool ath10k_pci_is_awake(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300797{
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200798 u32 val = ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS);
799
800 return RTC_STATE_V_GET(val) == RTC_STATE_V_ON;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300801}
802
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200803static int ath10k_pci_wake_wait(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300804{
Kalle Valo5e3dd152013-06-12 20:52:10 +0300805 int tot_delay = 0;
806 int curr_delay = 5;
807
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200808 while (tot_delay < PCIE_WAKE_TIMEOUT) {
809 if (ath10k_pci_is_awake(ar))
Kalle Valo3aebe542013-09-01 10:02:07 +0300810 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300811
812 udelay(curr_delay);
813 tot_delay += curr_delay;
814
815 if (curr_delay < 50)
816 curr_delay += 5;
817 }
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200818
819 return -ETIMEDOUT;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300820}
821
Michal Kazior320e14b2015-03-02 13:22:13 +0100822/* The rule is host is forbidden from accessing device registers while it's
823 * asleep. Currently ath10k_pci_wake() and ath10k_pci_sleep() calls aren't
824 * balanced and the device is kept awake all the time. This is intended for a
825 * simpler solution for the following problems:
826 *
827 * * device can enter sleep during s2ram without the host knowing,
828 *
829 * * irq handlers access registers which is a problem if other device asserts
830 * a shared irq line when ath10k is between hif_power_down() and
831 * hif_power_up().
832 *
833 * FIXME: If power consumption is a concern (and there are *real* gains) then a
834 * refcounted wake/sleep needs to be implemented.
835 */
836
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200837static int ath10k_pci_wake(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300838{
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200839 ath10k_pci_reg_write32(ar, PCIE_SOC_WAKE_ADDRESS,
840 PCIE_SOC_WAKE_V_MASK);
841 return ath10k_pci_wake_wait(ar);
842}
Kalle Valo5e3dd152013-06-12 20:52:10 +0300843
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200844static void ath10k_pci_sleep(struct ath10k *ar)
845{
846 ath10k_pci_reg_write32(ar, PCIE_SOC_WAKE_ADDRESS,
847 PCIE_SOC_WAKE_RESET);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300848}
849
Kalle Valo5e3dd152013-06-12 20:52:10 +0300850/* Called by lower (CE) layer when a send to Target completes. */
Michal Kazior5440ce22013-09-03 15:09:58 +0200851static void ath10k_pci_ce_send_done(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300852{
853 struct ath10k *ar = ce_state->ar;
854 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2f5280d2014-02-27 18:50:05 +0200855 struct ath10k_hif_cb *cb = &ar_pci->msg_callbacks_current;
Michal Kazior1cb86d42014-11-27 11:09:38 +0100856 struct sk_buff_head list;
857 struct sk_buff *skb;
Michal Kazior5440ce22013-09-03 15:09:58 +0200858 u32 ce_data;
859 unsigned int nbytes;
860 unsigned int transfer_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300861
Michal Kazior1cb86d42014-11-27 11:09:38 +0100862 __skb_queue_head_init(&list);
863 while (ath10k_ce_completed_send_next(ce_state, (void **)&skb, &ce_data,
864 &nbytes, &transfer_id) == 0) {
Michal Kaziora16942e2014-02-27 18:50:04 +0200865 /* no need to call tx completion for NULL pointers */
Michal Kazior1cb86d42014-11-27 11:09:38 +0100866 if (skb == NULL)
Michal Kazior726346f2014-02-27 18:50:04 +0200867 continue;
868
Michal Kazior1cb86d42014-11-27 11:09:38 +0100869 __skb_queue_tail(&list, skb);
Michal Kazior5440ce22013-09-03 15:09:58 +0200870 }
Michal Kazior1cb86d42014-11-27 11:09:38 +0100871
872 while ((skb = __skb_dequeue(&list)))
873 cb->tx_completion(ar, skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300874}
875
876/* Called by lower (CE) layer when data is received from the Target. */
Michal Kazior5440ce22013-09-03 15:09:58 +0200877static void ath10k_pci_ce_recv_data(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300878{
879 struct ath10k *ar = ce_state->ar;
880 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +0200881 struct ath10k_pci_pipe *pipe_info = &ar_pci->pipe_info[ce_state->id];
Michal Kazior2f5280d2014-02-27 18:50:05 +0200882 struct ath10k_hif_cb *cb = &ar_pci->msg_callbacks_current;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300883 struct sk_buff *skb;
Michal Kazior1cb86d42014-11-27 11:09:38 +0100884 struct sk_buff_head list;
Michal Kazior5440ce22013-09-03 15:09:58 +0200885 void *transfer_context;
886 u32 ce_data;
Michal Kazior2f5280d2014-02-27 18:50:05 +0200887 unsigned int nbytes, max_nbytes;
Michal Kazior5440ce22013-09-03 15:09:58 +0200888 unsigned int transfer_id;
889 unsigned int flags;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300890
Michal Kazior1cb86d42014-11-27 11:09:38 +0100891 __skb_queue_head_init(&list);
Michal Kazior5440ce22013-09-03 15:09:58 +0200892 while (ath10k_ce_completed_recv_next(ce_state, &transfer_context,
893 &ce_data, &nbytes, &transfer_id,
894 &flags) == 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300895 skb = transfer_context;
Michal Kazior2f5280d2014-02-27 18:50:05 +0200896 max_nbytes = skb->len + skb_tailroom(skb);
Michal Kazior8582bf32015-01-24 12:14:47 +0200897 dma_unmap_single(ar->dev, ATH10K_SKB_RXCB(skb)->paddr,
Michal Kazior2f5280d2014-02-27 18:50:05 +0200898 max_nbytes, DMA_FROM_DEVICE);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300899
Michal Kazior2f5280d2014-02-27 18:50:05 +0200900 if (unlikely(max_nbytes < nbytes)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200901 ath10k_warn(ar, "rxed more than expected (nbytes %d, max %d)",
Michal Kazior2f5280d2014-02-27 18:50:05 +0200902 nbytes, max_nbytes);
903 dev_kfree_skb_any(skb);
904 continue;
905 }
906
907 skb_put(skb, nbytes);
Michal Kazior1cb86d42014-11-27 11:09:38 +0100908 __skb_queue_tail(&list, skb);
909 }
Michal Kaziora360e542014-09-23 10:22:54 +0200910
Michal Kazior1cb86d42014-11-27 11:09:38 +0100911 while ((skb = __skb_dequeue(&list))) {
Michal Kaziora360e542014-09-23 10:22:54 +0200912 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci rx ce pipe %d len %d\n",
913 ce_state->id, skb->len);
914 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci rx: ",
915 skb->data, skb->len);
916
Michal Kazior5f07ea42014-11-27 11:09:36 +0100917 cb->rx_completion(ar, skb);
Michal Kazior2f5280d2014-02-27 18:50:05 +0200918 }
Michal Kaziorc29a3802014-07-21 21:03:10 +0300919
Michal Kazior728f95e2014-08-22 14:33:14 +0200920 ath10k_pci_rx_post_pipe(pipe_info);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300921}
922
Michal Kazior726346f2014-02-27 18:50:04 +0200923static int ath10k_pci_hif_tx_sg(struct ath10k *ar, u8 pipe_id,
924 struct ath10k_hif_sg_item *items, int n_items)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300925{
Kalle Valo5e3dd152013-06-12 20:52:10 +0300926 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior726346f2014-02-27 18:50:04 +0200927 struct ath10k_pci_pipe *pci_pipe = &ar_pci->pipe_info[pipe_id];
928 struct ath10k_ce_pipe *ce_pipe = pci_pipe->ce_hdl;
929 struct ath10k_ce_ring *src_ring = ce_pipe->src_ring;
Michal Kazior7147a132014-05-26 12:02:58 +0200930 unsigned int nentries_mask;
931 unsigned int sw_index;
932 unsigned int write_index;
Michal Kazior08b8aa02014-05-26 12:02:59 +0200933 int err, i = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300934
Michal Kazior726346f2014-02-27 18:50:04 +0200935 spin_lock_bh(&ar_pci->ce_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300936
Michal Kazior7147a132014-05-26 12:02:58 +0200937 nentries_mask = src_ring->nentries_mask;
938 sw_index = src_ring->sw_index;
939 write_index = src_ring->write_index;
940
Michal Kazior726346f2014-02-27 18:50:04 +0200941 if (unlikely(CE_RING_DELTA(nentries_mask,
942 write_index, sw_index - 1) < n_items)) {
943 err = -ENOBUFS;
Michal Kazior08b8aa02014-05-26 12:02:59 +0200944 goto err;
Michal Kazior726346f2014-02-27 18:50:04 +0200945 }
946
947 for (i = 0; i < n_items - 1; i++) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200948 ath10k_dbg(ar, ATH10K_DBG_PCI,
Michal Kazior726346f2014-02-27 18:50:04 +0200949 "pci tx item %d paddr 0x%08x len %d n_items %d\n",
950 i, items[i].paddr, items[i].len, n_items);
Michal Kazior7aa7a722014-08-25 12:09:38 +0200951 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci tx data: ",
Michal Kazior726346f2014-02-27 18:50:04 +0200952 items[i].vaddr, items[i].len);
953
954 err = ath10k_ce_send_nolock(ce_pipe,
955 items[i].transfer_context,
956 items[i].paddr,
957 items[i].len,
958 items[i].transfer_id,
959 CE_SEND_FLAG_GATHER);
960 if (err)
Michal Kazior08b8aa02014-05-26 12:02:59 +0200961 goto err;
Michal Kazior726346f2014-02-27 18:50:04 +0200962 }
963
964 /* `i` is equal to `n_items -1` after for() */
Kalle Valo5e3dd152013-06-12 20:52:10 +0300965
Michal Kazior7aa7a722014-08-25 12:09:38 +0200966 ath10k_dbg(ar, ATH10K_DBG_PCI,
Michal Kazior726346f2014-02-27 18:50:04 +0200967 "pci tx item %d paddr 0x%08x len %d n_items %d\n",
968 i, items[i].paddr, items[i].len, n_items);
Michal Kazior7aa7a722014-08-25 12:09:38 +0200969 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci tx data: ",
Michal Kazior726346f2014-02-27 18:50:04 +0200970 items[i].vaddr, items[i].len);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300971
Michal Kazior726346f2014-02-27 18:50:04 +0200972 err = ath10k_ce_send_nolock(ce_pipe,
973 items[i].transfer_context,
974 items[i].paddr,
975 items[i].len,
976 items[i].transfer_id,
977 0);
978 if (err)
Michal Kazior08b8aa02014-05-26 12:02:59 +0200979 goto err;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300980
Michal Kazior08b8aa02014-05-26 12:02:59 +0200981 spin_unlock_bh(&ar_pci->ce_lock);
982 return 0;
983
984err:
985 for (; i > 0; i--)
986 __ath10k_ce_send_revert(ce_pipe);
987
Michal Kazior726346f2014-02-27 18:50:04 +0200988 spin_unlock_bh(&ar_pci->ce_lock);
989 return err;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300990}
991
Kalle Valoeef25402014-09-24 14:16:52 +0300992static int ath10k_pci_hif_diag_read(struct ath10k *ar, u32 address, void *buf,
993 size_t buf_len)
994{
995 return ath10k_pci_diag_read_mem(ar, address, buf, buf_len);
996}
997
Kalle Valo5e3dd152013-06-12 20:52:10 +0300998static u16 ath10k_pci_hif_get_free_queue_number(struct ath10k *ar, u8 pipe)
999{
1000 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo50f87a62014-03-28 09:32:52 +02001001
Michal Kazior7aa7a722014-08-25 12:09:38 +02001002 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif get free queue number\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001003
Michal Kazior3efcb3b2013-10-02 11:03:41 +02001004 return ath10k_ce_num_free_src_entries(ar_pci->pipe_info[pipe].ce_hdl);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001005}
1006
Ben Greear384914b2014-08-25 08:37:32 +03001007static void ath10k_pci_dump_registers(struct ath10k *ar,
1008 struct ath10k_fw_crash_data *crash_data)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001009{
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001010 __le32 reg_dump_values[REG_DUMP_COUNT_QCA988X] = {};
1011 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001012
Ben Greear384914b2014-08-25 08:37:32 +03001013 lockdep_assert_held(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001014
Kalle Valo3d29a3e2014-08-25 08:37:26 +03001015 ret = ath10k_pci_diag_read_hi(ar, &reg_dump_values[0],
1016 hi_failure_state,
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001017 REG_DUMP_COUNT_QCA988X * sizeof(__le32));
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001018 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001019 ath10k_err(ar, "failed to read firmware dump area: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001020 return;
1021 }
1022
1023 BUILD_BUG_ON(REG_DUMP_COUNT_QCA988X % 4);
1024
Michal Kazior7aa7a722014-08-25 12:09:38 +02001025 ath10k_err(ar, "firmware register dump:\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001026 for (i = 0; i < REG_DUMP_COUNT_QCA988X; i += 4)
Michal Kazior7aa7a722014-08-25 12:09:38 +02001027 ath10k_err(ar, "[%02d]: 0x%08X 0x%08X 0x%08X 0x%08X\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001028 i,
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001029 __le32_to_cpu(reg_dump_values[i]),
1030 __le32_to_cpu(reg_dump_values[i + 1]),
1031 __le32_to_cpu(reg_dump_values[i + 2]),
1032 __le32_to_cpu(reg_dump_values[i + 3]));
Michal Kazioraffd3212013-07-16 09:54:35 +02001033
Michal Kazior1bbb1192014-08-25 12:13:14 +02001034 if (!crash_data)
1035 return;
1036
Ben Greear384914b2014-08-25 08:37:32 +03001037 for (i = 0; i < REG_DUMP_COUNT_QCA988X; i++)
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001038 crash_data->registers[i] = reg_dump_values[i];
Ben Greear384914b2014-08-25 08:37:32 +03001039}
1040
Kalle Valo0e9848c2014-08-25 08:37:37 +03001041static void ath10k_pci_fw_crashed_dump(struct ath10k *ar)
Ben Greear384914b2014-08-25 08:37:32 +03001042{
1043 struct ath10k_fw_crash_data *crash_data;
1044 char uuid[50];
1045
1046 spin_lock_bh(&ar->data_lock);
1047
Ben Greearf51dbe72014-09-29 14:41:46 +03001048 ar->stats.fw_crash_counter++;
1049
Ben Greear384914b2014-08-25 08:37:32 +03001050 crash_data = ath10k_debug_get_new_fw_crash_data(ar);
1051
1052 if (crash_data)
1053 scnprintf(uuid, sizeof(uuid), "%pUl", &crash_data->uuid);
1054 else
1055 scnprintf(uuid, sizeof(uuid), "n/a");
1056
Michal Kazior7aa7a722014-08-25 12:09:38 +02001057 ath10k_err(ar, "firmware crashed! (uuid %s)\n", uuid);
Kalle Valo8a0c7972014-08-25 08:37:45 +03001058 ath10k_print_driver_info(ar);
Ben Greear384914b2014-08-25 08:37:32 +03001059 ath10k_pci_dump_registers(ar, crash_data);
1060
Ben Greear384914b2014-08-25 08:37:32 +03001061 spin_unlock_bh(&ar->data_lock);
Michal Kazioraffd3212013-07-16 09:54:35 +02001062
Michal Kazior5e90de82013-10-16 16:46:05 +03001063 queue_work(ar->workqueue, &ar->restart_work);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001064}
1065
1066static void ath10k_pci_hif_send_complete_check(struct ath10k *ar, u8 pipe,
1067 int force)
1068{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001069 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif send complete check\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001070
Kalle Valo5e3dd152013-06-12 20:52:10 +03001071 if (!force) {
1072 int resources;
1073 /*
1074 * Decide whether to actually poll for completions, or just
1075 * wait for a later chance.
1076 * If there seem to be plenty of resources left, then just wait
1077 * since checking involves reading a CE register, which is a
1078 * relatively expensive operation.
1079 */
1080 resources = ath10k_pci_hif_get_free_queue_number(ar, pipe);
1081
1082 /*
1083 * If at least 50% of the total resources are still available,
1084 * don't bother checking again yet.
1085 */
1086 if (resources > (host_ce_config_wlan[pipe].src_nentries >> 1))
1087 return;
1088 }
1089 ath10k_ce_per_engine_service(ar, pipe);
1090}
1091
Michal Kaziore799bbf2013-07-05 16:15:12 +03001092static void ath10k_pci_hif_set_callbacks(struct ath10k *ar,
1093 struct ath10k_hif_cb *callbacks)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001094{
1095 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1096
Michal Kazior7aa7a722014-08-25 12:09:38 +02001097 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif set callbacks\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001098
1099 memcpy(&ar_pci->msg_callbacks_current, callbacks,
1100 sizeof(ar_pci->msg_callbacks_current));
1101}
1102
Michal Kazior96a9d0d2013-11-08 08:01:25 +01001103static void ath10k_pci_kill_tasklet(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001104{
1105 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001106 int i;
1107
Kalle Valo5e3dd152013-06-12 20:52:10 +03001108 tasklet_kill(&ar_pci->intr_tq);
Michal Kazior103d4f52013-11-08 08:01:24 +01001109 tasklet_kill(&ar_pci->msi_fw_err);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001110
1111 for (i = 0; i < CE_COUNT; i++)
1112 tasklet_kill(&ar_pci->pipe_info[i].intr);
Michal Kazior728f95e2014-08-22 14:33:14 +02001113
1114 del_timer_sync(&ar_pci->rx_post_retry);
Michal Kazior96a9d0d2013-11-08 08:01:25 +01001115}
1116
Kalle Valo5e3dd152013-06-12 20:52:10 +03001117static int ath10k_pci_hif_map_service_to_pipe(struct ath10k *ar,
1118 u16 service_id, u8 *ul_pipe,
1119 u8 *dl_pipe, int *ul_is_polled,
1120 int *dl_is_polled)
1121{
Michal Kazior7c6aa252014-08-26 19:14:03 +03001122 const struct service_to_pipe *entry;
1123 bool ul_set = false, dl_set = false;
1124 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001125
Michal Kazior7aa7a722014-08-25 12:09:38 +02001126 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif map service\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001127
Kalle Valo5e3dd152013-06-12 20:52:10 +03001128 /* polling for received messages not supported */
1129 *dl_is_polled = 0;
1130
Michal Kazior7c6aa252014-08-26 19:14:03 +03001131 for (i = 0; i < ARRAY_SIZE(target_service_to_ce_map_wlan); i++) {
1132 entry = &target_service_to_ce_map_wlan[i];
Kalle Valo5e3dd152013-06-12 20:52:10 +03001133
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001134 if (__le32_to_cpu(entry->service_id) != service_id)
Michal Kazior7c6aa252014-08-26 19:14:03 +03001135 continue;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001136
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001137 switch (__le32_to_cpu(entry->pipedir)) {
Michal Kazior7c6aa252014-08-26 19:14:03 +03001138 case PIPEDIR_NONE:
1139 break;
1140 case PIPEDIR_IN:
1141 WARN_ON(dl_set);
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001142 *dl_pipe = __le32_to_cpu(entry->pipenum);
Michal Kazior7c6aa252014-08-26 19:14:03 +03001143 dl_set = true;
1144 break;
1145 case PIPEDIR_OUT:
1146 WARN_ON(ul_set);
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001147 *ul_pipe = __le32_to_cpu(entry->pipenum);
Michal Kazior7c6aa252014-08-26 19:14:03 +03001148 ul_set = true;
1149 break;
1150 case PIPEDIR_INOUT:
1151 WARN_ON(dl_set);
1152 WARN_ON(ul_set);
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001153 *dl_pipe = __le32_to_cpu(entry->pipenum);
1154 *ul_pipe = __le32_to_cpu(entry->pipenum);
Michal Kazior7c6aa252014-08-26 19:14:03 +03001155 dl_set = true;
1156 ul_set = true;
1157 break;
1158 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001159 }
Michal Kazior7c6aa252014-08-26 19:14:03 +03001160
1161 if (WARN_ON(!ul_set || !dl_set))
1162 return -ENOENT;
1163
Kalle Valo5e3dd152013-06-12 20:52:10 +03001164 *ul_is_polled =
1165 (host_ce_config_wlan[*ul_pipe].flags & CE_ATTR_DIS_INTR) != 0;
1166
Michal Kazior7c6aa252014-08-26 19:14:03 +03001167 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001168}
1169
1170static void ath10k_pci_hif_get_default_pipe(struct ath10k *ar,
Kalle Valo5b07e072014-09-14 12:50:06 +03001171 u8 *ul_pipe, u8 *dl_pipe)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001172{
1173 int ul_is_polled, dl_is_polled;
1174
Michal Kazior7aa7a722014-08-25 12:09:38 +02001175 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif get default pipe\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001176
Kalle Valo5e3dd152013-06-12 20:52:10 +03001177 (void)ath10k_pci_hif_map_service_to_pipe(ar,
1178 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1179 ul_pipe,
1180 dl_pipe,
1181 &ul_is_polled,
1182 &dl_is_polled);
1183}
1184
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001185static void ath10k_pci_irq_msi_fw_mask(struct ath10k *ar)
1186{
1187 u32 val;
1188
1189 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS + CORE_CTRL_ADDRESS);
1190 val &= ~CORE_CTRL_PCIE_REG_31_MASK;
1191
1192 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + CORE_CTRL_ADDRESS, val);
1193}
1194
1195static void ath10k_pci_irq_msi_fw_unmask(struct ath10k *ar)
1196{
1197 u32 val;
1198
1199 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS + CORE_CTRL_ADDRESS);
1200 val |= CORE_CTRL_PCIE_REG_31_MASK;
1201
1202 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + CORE_CTRL_ADDRESS, val);
1203}
1204
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001205static void ath10k_pci_irq_disable(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001206{
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001207 ath10k_ce_disable_interrupts(ar);
1208 ath10k_pci_disable_and_clear_legacy_irq(ar);
1209 ath10k_pci_irq_msi_fw_mask(ar);
1210}
1211
1212static void ath10k_pci_irq_sync(struct ath10k *ar)
1213{
Kalle Valo5e3dd152013-06-12 20:52:10 +03001214 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001215 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001216
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001217 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
1218 synchronize_irq(ar_pci->pdev->irq + i);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001219}
1220
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001221static void ath10k_pci_irq_enable(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001222{
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001223 ath10k_ce_enable_interrupts(ar);
Michal Kaziore75db4e2014-08-28 22:14:16 +03001224 ath10k_pci_enable_legacy_irq(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001225 ath10k_pci_irq_msi_fw_unmask(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001226}
1227
1228static int ath10k_pci_hif_start(struct ath10k *ar)
1229{
Janusz Dziedzic76d870e2015-05-18 09:38:16 +00001230 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior7aa7a722014-08-25 12:09:38 +02001231 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif start\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001232
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001233 ath10k_pci_irq_enable(ar);
Michal Kazior728f95e2014-08-22 14:33:14 +02001234 ath10k_pci_rx_post(ar);
Kalle Valo50f87a62014-03-28 09:32:52 +02001235
Janusz Dziedzic76d870e2015-05-18 09:38:16 +00001236 pcie_capability_write_word(ar_pci->pdev, PCI_EXP_LNKCTL,
1237 ar_pci->link_ctl);
1238
Kalle Valo5e3dd152013-06-12 20:52:10 +03001239 return 0;
1240}
1241
Michal Kazior099ac7c2014-10-28 10:32:05 +01001242static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe *pci_pipe)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001243{
1244 struct ath10k *ar;
Michal Kazior099ac7c2014-10-28 10:32:05 +01001245 struct ath10k_ce_pipe *ce_pipe;
1246 struct ath10k_ce_ring *ce_ring;
1247 struct sk_buff *skb;
1248 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001249
Michal Kazior099ac7c2014-10-28 10:32:05 +01001250 ar = pci_pipe->hif_ce_state;
1251 ce_pipe = pci_pipe->ce_hdl;
1252 ce_ring = ce_pipe->dest_ring;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001253
Michal Kazior099ac7c2014-10-28 10:32:05 +01001254 if (!ce_ring)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001255 return;
1256
Michal Kazior099ac7c2014-10-28 10:32:05 +01001257 if (!pci_pipe->buf_sz)
1258 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001259
Michal Kazior099ac7c2014-10-28 10:32:05 +01001260 for (i = 0; i < ce_ring->nentries; i++) {
1261 skb = ce_ring->per_transfer_context[i];
1262 if (!skb)
1263 continue;
1264
1265 ce_ring->per_transfer_context[i] = NULL;
1266
Michal Kazior8582bf32015-01-24 12:14:47 +02001267 dma_unmap_single(ar->dev, ATH10K_SKB_RXCB(skb)->paddr,
Michal Kazior099ac7c2014-10-28 10:32:05 +01001268 skb->len + skb_tailroom(skb),
Kalle Valo5e3dd152013-06-12 20:52:10 +03001269 DMA_FROM_DEVICE);
Michal Kazior099ac7c2014-10-28 10:32:05 +01001270 dev_kfree_skb_any(skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001271 }
1272}
1273
Michal Kazior099ac7c2014-10-28 10:32:05 +01001274static void ath10k_pci_tx_pipe_cleanup(struct ath10k_pci_pipe *pci_pipe)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001275{
1276 struct ath10k *ar;
1277 struct ath10k_pci *ar_pci;
Michal Kazior099ac7c2014-10-28 10:32:05 +01001278 struct ath10k_ce_pipe *ce_pipe;
1279 struct ath10k_ce_ring *ce_ring;
1280 struct ce_desc *ce_desc;
1281 struct sk_buff *skb;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001282 unsigned int id;
Michal Kazior099ac7c2014-10-28 10:32:05 +01001283 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001284
Michal Kazior099ac7c2014-10-28 10:32:05 +01001285 ar = pci_pipe->hif_ce_state;
1286 ar_pci = ath10k_pci_priv(ar);
1287 ce_pipe = pci_pipe->ce_hdl;
1288 ce_ring = ce_pipe->src_ring;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001289
Michal Kazior099ac7c2014-10-28 10:32:05 +01001290 if (!ce_ring)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001291 return;
1292
Michal Kazior099ac7c2014-10-28 10:32:05 +01001293 if (!pci_pipe->buf_sz)
1294 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001295
Michal Kazior099ac7c2014-10-28 10:32:05 +01001296 ce_desc = ce_ring->shadow_base;
1297 if (WARN_ON(!ce_desc))
1298 return;
1299
1300 for (i = 0; i < ce_ring->nentries; i++) {
1301 skb = ce_ring->per_transfer_context[i];
1302 if (!skb)
Michal Kazior2415fc12013-11-08 08:01:32 +01001303 continue;
Michal Kazior2415fc12013-11-08 08:01:32 +01001304
Michal Kazior099ac7c2014-10-28 10:32:05 +01001305 ce_ring->per_transfer_context[i] = NULL;
1306 id = MS(__le16_to_cpu(ce_desc[i].flags),
1307 CE_DESC_FLAGS_META_DATA);
1308
Michal Kaziord84a5122014-11-27 11:09:37 +01001309 ar_pci->msg_callbacks_current.tx_completion(ar, skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001310 }
1311}
1312
1313/*
1314 * Cleanup residual buffers for device shutdown:
1315 * buffers that were enqueued for receive
1316 * buffers that were to be sent
1317 * Note: Buffers that had completed but which were
1318 * not yet processed are on a completion queue. They
1319 * are handled when the completion thread shuts down.
1320 */
1321static void ath10k_pci_buffer_cleanup(struct ath10k *ar)
1322{
1323 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1324 int pipe_num;
1325
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001326 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Michal Kazior87263e52013-08-27 13:08:01 +02001327 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001328
1329 pipe_info = &ar_pci->pipe_info[pipe_num];
1330 ath10k_pci_rx_pipe_cleanup(pipe_info);
1331 ath10k_pci_tx_pipe_cleanup(pipe_info);
1332 }
1333}
1334
1335static void ath10k_pci_ce_deinit(struct ath10k *ar)
1336{
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001337 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001338
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001339 for (i = 0; i < CE_COUNT; i++)
1340 ath10k_ce_deinit_pipe(ar, i);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001341}
1342
Michal Kazior728f95e2014-08-22 14:33:14 +02001343static void ath10k_pci_flush(struct ath10k *ar)
1344{
1345 ath10k_pci_kill_tasklet(ar);
1346 ath10k_pci_buffer_cleanup(ar);
1347}
1348
Kalle Valo5e3dd152013-06-12 20:52:10 +03001349static void ath10k_pci_hif_stop(struct ath10k *ar)
1350{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001351 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif stop\n");
Michal Kazior32270b62013-08-02 09:15:47 +02001352
Michal Kazior10d23db2014-08-22 14:33:15 +02001353 /* Most likely the device has HTT Rx ring configured. The only way to
1354 * prevent the device from accessing (and possible corrupting) host
1355 * memory is to reset the chip now.
Michal Kaziore75db4e2014-08-28 22:14:16 +03001356 *
1357 * There's also no known way of masking MSI interrupts on the device.
1358 * For ranged MSI the CE-related interrupts can be masked. However
1359 * regardless how many MSI interrupts are assigned the first one
1360 * is always used for firmware indications (crashes) and cannot be
1361 * masked. To prevent the device from asserting the interrupt reset it
1362 * before proceeding with cleanup.
Michal Kazior10d23db2014-08-22 14:33:15 +02001363 */
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001364 ath10k_pci_warm_reset(ar);
Michal Kaziore75db4e2014-08-28 22:14:16 +03001365
1366 ath10k_pci_irq_disable(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001367 ath10k_pci_irq_sync(ar);
Michal Kaziore75db4e2014-08-28 22:14:16 +03001368 ath10k_pci_flush(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001369}
1370
1371static int ath10k_pci_hif_exchange_bmi_msg(struct ath10k *ar,
1372 void *req, u32 req_len,
1373 void *resp, u32 *resp_len)
1374{
1375 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2aa39112013-08-27 13:08:02 +02001376 struct ath10k_pci_pipe *pci_tx = &ar_pci->pipe_info[BMI_CE_NUM_TO_TARG];
1377 struct ath10k_pci_pipe *pci_rx = &ar_pci->pipe_info[BMI_CE_NUM_TO_HOST];
1378 struct ath10k_ce_pipe *ce_tx = pci_tx->ce_hdl;
1379 struct ath10k_ce_pipe *ce_rx = pci_rx->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001380 dma_addr_t req_paddr = 0;
1381 dma_addr_t resp_paddr = 0;
1382 struct bmi_xfer xfer = {};
1383 void *treq, *tresp = NULL;
1384 int ret = 0;
1385
Michal Kazior85622cd2013-11-25 14:06:22 +01001386 might_sleep();
1387
Kalle Valo5e3dd152013-06-12 20:52:10 +03001388 if (resp && !resp_len)
1389 return -EINVAL;
1390
1391 if (resp && resp_len && *resp_len == 0)
1392 return -EINVAL;
1393
1394 treq = kmemdup(req, req_len, GFP_KERNEL);
1395 if (!treq)
1396 return -ENOMEM;
1397
1398 req_paddr = dma_map_single(ar->dev, treq, req_len, DMA_TO_DEVICE);
1399 ret = dma_mapping_error(ar->dev, req_paddr);
1400 if (ret)
1401 goto err_dma;
1402
1403 if (resp && resp_len) {
1404 tresp = kzalloc(*resp_len, GFP_KERNEL);
1405 if (!tresp) {
1406 ret = -ENOMEM;
1407 goto err_req;
1408 }
1409
1410 resp_paddr = dma_map_single(ar->dev, tresp, *resp_len,
1411 DMA_FROM_DEVICE);
1412 ret = dma_mapping_error(ar->dev, resp_paddr);
1413 if (ret)
1414 goto err_req;
1415
1416 xfer.wait_for_resp = true;
1417 xfer.resp_len = 0;
1418
Michal Kazior728f95e2014-08-22 14:33:14 +02001419 ath10k_ce_rx_post_buf(ce_rx, &xfer, resp_paddr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001420 }
1421
Kalle Valo5e3dd152013-06-12 20:52:10 +03001422 ret = ath10k_ce_send(ce_tx, &xfer, req_paddr, req_len, -1, 0);
1423 if (ret)
1424 goto err_resp;
1425
Michal Kazior85622cd2013-11-25 14:06:22 +01001426 ret = ath10k_pci_bmi_wait(ce_tx, ce_rx, &xfer);
1427 if (ret) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001428 u32 unused_buffer;
1429 unsigned int unused_nbytes;
1430 unsigned int unused_id;
1431
Kalle Valo5e3dd152013-06-12 20:52:10 +03001432 ath10k_ce_cancel_send_next(ce_tx, NULL, &unused_buffer,
1433 &unused_nbytes, &unused_id);
1434 } else {
1435 /* non-zero means we did not time out */
1436 ret = 0;
1437 }
1438
1439err_resp:
1440 if (resp) {
1441 u32 unused_buffer;
1442
1443 ath10k_ce_revoke_recv_next(ce_rx, NULL, &unused_buffer);
1444 dma_unmap_single(ar->dev, resp_paddr,
1445 *resp_len, DMA_FROM_DEVICE);
1446 }
1447err_req:
1448 dma_unmap_single(ar->dev, req_paddr, req_len, DMA_TO_DEVICE);
1449
1450 if (ret == 0 && resp_len) {
1451 *resp_len = min(*resp_len, xfer.resp_len);
1452 memcpy(resp, tresp, xfer.resp_len);
1453 }
1454err_dma:
1455 kfree(treq);
1456 kfree(tresp);
1457
1458 return ret;
1459}
1460
Michal Kazior5440ce22013-09-03 15:09:58 +02001461static void ath10k_pci_bmi_send_done(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001462{
Michal Kazior5440ce22013-09-03 15:09:58 +02001463 struct bmi_xfer *xfer;
1464 u32 ce_data;
1465 unsigned int nbytes;
1466 unsigned int transfer_id;
1467
1468 if (ath10k_ce_completed_send_next(ce_state, (void **)&xfer, &ce_data,
1469 &nbytes, &transfer_id))
1470 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001471
Michal Kazior2374b182014-07-14 16:25:25 +03001472 xfer->tx_done = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001473}
1474
Michal Kazior5440ce22013-09-03 15:09:58 +02001475static void ath10k_pci_bmi_recv_data(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001476{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001477 struct ath10k *ar = ce_state->ar;
Michal Kazior5440ce22013-09-03 15:09:58 +02001478 struct bmi_xfer *xfer;
1479 u32 ce_data;
1480 unsigned int nbytes;
1481 unsigned int transfer_id;
1482 unsigned int flags;
1483
1484 if (ath10k_ce_completed_recv_next(ce_state, (void **)&xfer, &ce_data,
1485 &nbytes, &transfer_id, &flags))
1486 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001487
Michal Kazior04ed9df2014-10-28 10:34:36 +01001488 if (WARN_ON_ONCE(!xfer))
1489 return;
1490
Kalle Valo5e3dd152013-06-12 20:52:10 +03001491 if (!xfer->wait_for_resp) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001492 ath10k_warn(ar, "unexpected: BMI data received; ignoring\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001493 return;
1494 }
1495
1496 xfer->resp_len = nbytes;
Michal Kazior2374b182014-07-14 16:25:25 +03001497 xfer->rx_done = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001498}
1499
Michal Kazior85622cd2013-11-25 14:06:22 +01001500static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
1501 struct ath10k_ce_pipe *rx_pipe,
1502 struct bmi_xfer *xfer)
1503{
1504 unsigned long timeout = jiffies + BMI_COMMUNICATION_TIMEOUT_HZ;
1505
1506 while (time_before_eq(jiffies, timeout)) {
1507 ath10k_pci_bmi_send_done(tx_pipe);
1508 ath10k_pci_bmi_recv_data(rx_pipe);
1509
Michal Kazior2374b182014-07-14 16:25:25 +03001510 if (xfer->tx_done && (xfer->rx_done == xfer->wait_for_resp))
Michal Kazior85622cd2013-11-25 14:06:22 +01001511 return 0;
1512
1513 schedule();
1514 }
1515
1516 return -ETIMEDOUT;
1517}
1518
Kalle Valo5e3dd152013-06-12 20:52:10 +03001519/*
Kalle Valo5e3dd152013-06-12 20:52:10 +03001520 * Send an interrupt to the device to wake up the Target CPU
1521 * so it has an opportunity to notice any changed state.
1522 */
1523static int ath10k_pci_wake_target_cpu(struct ath10k *ar)
1524{
Michal Kazior9e264942014-09-02 11:00:21 +03001525 u32 addr, val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001526
Michal Kazior9e264942014-09-02 11:00:21 +03001527 addr = SOC_CORE_BASE_ADDRESS | CORE_CTRL_ADDRESS;
1528 val = ath10k_pci_read32(ar, addr);
1529 val |= CORE_CTRL_CPU_INTR_MASK;
1530 ath10k_pci_write32(ar, addr, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001531
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001532 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001533}
1534
Michal Kaziord63955b2015-01-24 12:14:49 +02001535static int ath10k_pci_get_num_banks(struct ath10k *ar)
1536{
1537 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1538
1539 switch (ar_pci->pdev->device) {
1540 case QCA988X_2_0_DEVICE_ID:
1541 return 1;
1542 case QCA6174_2_1_DEVICE_ID:
1543 switch (MS(ar->chip_id, SOC_CHIP_ID_REV)) {
1544 case QCA6174_HW_1_0_CHIP_ID_REV:
1545 case QCA6174_HW_1_1_CHIP_ID_REV:
Michal Kazior11a002e2015-04-20 09:20:41 +00001546 case QCA6174_HW_2_1_CHIP_ID_REV:
1547 case QCA6174_HW_2_2_CHIP_ID_REV:
Michal Kaziord63955b2015-01-24 12:14:49 +02001548 return 3;
1549 case QCA6174_HW_1_3_CHIP_ID_REV:
1550 return 2;
Michal Kaziord63955b2015-01-24 12:14:49 +02001551 case QCA6174_HW_3_0_CHIP_ID_REV:
1552 case QCA6174_HW_3_1_CHIP_ID_REV:
1553 case QCA6174_HW_3_2_CHIP_ID_REV:
1554 return 9;
1555 }
1556 break;
1557 }
1558
1559 ath10k_warn(ar, "unknown number of banks, assuming 1\n");
1560 return 1;
1561}
1562
Kalle Valo5e3dd152013-06-12 20:52:10 +03001563static int ath10k_pci_init_config(struct ath10k *ar)
1564{
1565 u32 interconnect_targ_addr;
1566 u32 pcie_state_targ_addr = 0;
1567 u32 pipe_cfg_targ_addr = 0;
1568 u32 svc_to_pipe_map = 0;
1569 u32 pcie_config_flags = 0;
1570 u32 ealloc_value;
1571 u32 ealloc_targ_addr;
1572 u32 flag2_value;
1573 u32 flag2_targ_addr;
1574 int ret = 0;
1575
1576 /* Download to Target the CE Config and the service-to-CE map */
1577 interconnect_targ_addr =
1578 host_interest_item_address(HI_ITEM(hi_interconnect_state));
1579
1580 /* Supply Target-side CE configuration */
Michal Kazior9e264942014-09-02 11:00:21 +03001581 ret = ath10k_pci_diag_read32(ar, interconnect_targ_addr,
1582 &pcie_state_targ_addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001583 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001584 ath10k_err(ar, "Failed to get pcie state addr: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001585 return ret;
1586 }
1587
1588 if (pcie_state_targ_addr == 0) {
1589 ret = -EIO;
Michal Kazior7aa7a722014-08-25 12:09:38 +02001590 ath10k_err(ar, "Invalid pcie state addr\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001591 return ret;
1592 }
1593
Michal Kazior9e264942014-09-02 11:00:21 +03001594 ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
Kalle Valo5e3dd152013-06-12 20:52:10 +03001595 offsetof(struct pcie_state,
Michal Kazior9e264942014-09-02 11:00:21 +03001596 pipe_cfg_addr)),
1597 &pipe_cfg_targ_addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001598 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001599 ath10k_err(ar, "Failed to get pipe cfg addr: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001600 return ret;
1601 }
1602
1603 if (pipe_cfg_targ_addr == 0) {
1604 ret = -EIO;
Michal Kazior7aa7a722014-08-25 12:09:38 +02001605 ath10k_err(ar, "Invalid pipe cfg addr\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001606 return ret;
1607 }
1608
1609 ret = ath10k_pci_diag_write_mem(ar, pipe_cfg_targ_addr,
Kalle Valo5b07e072014-09-14 12:50:06 +03001610 target_ce_config_wlan,
1611 sizeof(target_ce_config_wlan));
Kalle Valo5e3dd152013-06-12 20:52:10 +03001612
1613 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001614 ath10k_err(ar, "Failed to write pipe cfg: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001615 return ret;
1616 }
1617
Michal Kazior9e264942014-09-02 11:00:21 +03001618 ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
Kalle Valo5e3dd152013-06-12 20:52:10 +03001619 offsetof(struct pcie_state,
Michal Kazior9e264942014-09-02 11:00:21 +03001620 svc_to_pipe_map)),
1621 &svc_to_pipe_map);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001622 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001623 ath10k_err(ar, "Failed to get svc/pipe map: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001624 return ret;
1625 }
1626
1627 if (svc_to_pipe_map == 0) {
1628 ret = -EIO;
Michal Kazior7aa7a722014-08-25 12:09:38 +02001629 ath10k_err(ar, "Invalid svc_to_pipe map\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001630 return ret;
1631 }
1632
1633 ret = ath10k_pci_diag_write_mem(ar, svc_to_pipe_map,
Kalle Valo5b07e072014-09-14 12:50:06 +03001634 target_service_to_ce_map_wlan,
1635 sizeof(target_service_to_ce_map_wlan));
Kalle Valo5e3dd152013-06-12 20:52:10 +03001636 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001637 ath10k_err(ar, "Failed to write svc/pipe map: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001638 return ret;
1639 }
1640
Michal Kazior9e264942014-09-02 11:00:21 +03001641 ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
Kalle Valo5e3dd152013-06-12 20:52:10 +03001642 offsetof(struct pcie_state,
Michal Kazior9e264942014-09-02 11:00:21 +03001643 config_flags)),
1644 &pcie_config_flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001645 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001646 ath10k_err(ar, "Failed to get pcie config_flags: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001647 return ret;
1648 }
1649
1650 pcie_config_flags &= ~PCIE_CONFIG_FLAG_ENABLE_L1;
1651
Michal Kazior9e264942014-09-02 11:00:21 +03001652 ret = ath10k_pci_diag_write32(ar, (pcie_state_targ_addr +
1653 offsetof(struct pcie_state,
1654 config_flags)),
1655 pcie_config_flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001656 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001657 ath10k_err(ar, "Failed to write pcie config_flags: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001658 return ret;
1659 }
1660
1661 /* configure early allocation */
1662 ealloc_targ_addr = host_interest_item_address(HI_ITEM(hi_early_alloc));
1663
Michal Kazior9e264942014-09-02 11:00:21 +03001664 ret = ath10k_pci_diag_read32(ar, ealloc_targ_addr, &ealloc_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001665 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001666 ath10k_err(ar, "Faile to get early alloc val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001667 return ret;
1668 }
1669
1670 /* first bank is switched to IRAM */
1671 ealloc_value |= ((HI_EARLY_ALLOC_MAGIC << HI_EARLY_ALLOC_MAGIC_SHIFT) &
1672 HI_EARLY_ALLOC_MAGIC_MASK);
Michal Kaziord63955b2015-01-24 12:14:49 +02001673 ealloc_value |= ((ath10k_pci_get_num_banks(ar) <<
1674 HI_EARLY_ALLOC_IRAM_BANKS_SHIFT) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03001675 HI_EARLY_ALLOC_IRAM_BANKS_MASK);
1676
Michal Kazior9e264942014-09-02 11:00:21 +03001677 ret = ath10k_pci_diag_write32(ar, ealloc_targ_addr, ealloc_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001678 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001679 ath10k_err(ar, "Failed to set early alloc val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001680 return ret;
1681 }
1682
1683 /* Tell Target to proceed with initialization */
1684 flag2_targ_addr = host_interest_item_address(HI_ITEM(hi_option_flag2));
1685
Michal Kazior9e264942014-09-02 11:00:21 +03001686 ret = ath10k_pci_diag_read32(ar, flag2_targ_addr, &flag2_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001687 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001688 ath10k_err(ar, "Failed to get option val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001689 return ret;
1690 }
1691
1692 flag2_value |= HI_OPTION_EARLY_CFG_DONE;
1693
Michal Kazior9e264942014-09-02 11:00:21 +03001694 ret = ath10k_pci_diag_write32(ar, flag2_targ_addr, flag2_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001695 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001696 ath10k_err(ar, "Failed to set option val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001697 return ret;
1698 }
1699
1700 return 0;
1701}
1702
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001703static int ath10k_pci_alloc_pipes(struct ath10k *ar)
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001704{
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001705 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1706 struct ath10k_pci_pipe *pipe;
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001707 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001708
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001709 for (i = 0; i < CE_COUNT; i++) {
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001710 pipe = &ar_pci->pipe_info[i];
1711 pipe->ce_hdl = &ar_pci->ce_states[i];
1712 pipe->pipe_num = i;
1713 pipe->hif_ce_state = ar;
1714
1715 ret = ath10k_ce_alloc_pipe(ar, i, &host_ce_config_wlan[i],
1716 ath10k_pci_ce_send_done,
1717 ath10k_pci_ce_recv_data);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001718 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001719 ath10k_err(ar, "failed to allocate copy engine pipe %d: %d\n",
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001720 i, ret);
1721 return ret;
1722 }
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001723
1724 /* Last CE is Diagnostic Window */
1725 if (i == CE_COUNT - 1) {
1726 ar_pci->ce_diag = pipe->ce_hdl;
1727 continue;
1728 }
1729
1730 pipe->buf_sz = (size_t)(host_ce_config_wlan[i].src_sz_max);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001731 }
1732
1733 return 0;
1734}
1735
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001736static void ath10k_pci_free_pipes(struct ath10k *ar)
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001737{
1738 int i;
1739
1740 for (i = 0; i < CE_COUNT; i++)
1741 ath10k_ce_free_pipe(ar, i);
1742}
Kalle Valo5e3dd152013-06-12 20:52:10 +03001743
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001744static int ath10k_pci_init_pipes(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001745{
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001746 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001747
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001748 for (i = 0; i < CE_COUNT; i++) {
1749 ret = ath10k_ce_init_pipe(ar, i, &host_ce_config_wlan[i]);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001750 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001751 ath10k_err(ar, "failed to initialize copy engine pipe %d: %d\n",
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001752 i, ret);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001753 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001754 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001755 }
1756
Kalle Valo5e3dd152013-06-12 20:52:10 +03001757 return 0;
1758}
1759
Michal Kazior5c771e72014-08-22 14:23:34 +02001760static bool ath10k_pci_has_fw_crashed(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001761{
Michal Kazior5c771e72014-08-22 14:23:34 +02001762 return ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS) &
1763 FW_IND_EVENT_PENDING;
1764}
Kalle Valo5e3dd152013-06-12 20:52:10 +03001765
Michal Kazior5c771e72014-08-22 14:23:34 +02001766static void ath10k_pci_fw_crashed_clear(struct ath10k *ar)
1767{
1768 u32 val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001769
Michal Kazior5c771e72014-08-22 14:23:34 +02001770 val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
1771 val &= ~FW_IND_EVENT_PENDING;
1772 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001773}
1774
Michal Kaziorde013572014-05-14 16:56:16 +03001775/* this function effectively clears target memory controller assert line */
1776static void ath10k_pci_warm_reset_si0(struct ath10k *ar)
1777{
1778 u32 val;
1779
1780 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1781 ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
1782 val | SOC_RESET_CONTROL_SI0_RST_MASK);
1783 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1784
1785 msleep(10);
1786
1787 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1788 ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
1789 val & ~SOC_RESET_CONTROL_SI0_RST_MASK);
1790 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1791
1792 msleep(10);
1793}
1794
Michal Kazior61c16482014-10-28 10:32:06 +01001795static void ath10k_pci_warm_reset_cpu(struct ath10k *ar)
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001796{
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001797 u32 val;
1798
Kalle Valob39712c2014-03-28 09:32:46 +02001799 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, 0);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001800
Michal Kazior61c16482014-10-28 10:32:06 +01001801 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1802 SOC_RESET_CONTROL_ADDRESS);
1803 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1804 val | SOC_RESET_CONTROL_CPU_WARM_RST_MASK);
1805}
1806
1807static void ath10k_pci_warm_reset_ce(struct ath10k *ar)
1808{
1809 u32 val;
1810
1811 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1812 SOC_RESET_CONTROL_ADDRESS);
1813
1814 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1815 val | SOC_RESET_CONTROL_CE_RST_MASK);
1816 msleep(10);
1817 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1818 val & ~SOC_RESET_CONTROL_CE_RST_MASK);
1819}
1820
1821static void ath10k_pci_warm_reset_clear_lf(struct ath10k *ar)
1822{
1823 u32 val;
1824
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001825 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1826 SOC_LF_TIMER_CONTROL0_ADDRESS);
1827 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS +
1828 SOC_LF_TIMER_CONTROL0_ADDRESS,
1829 val & ~SOC_LF_TIMER_CONTROL0_ENABLE_MASK);
Michal Kazior61c16482014-10-28 10:32:06 +01001830}
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001831
Michal Kazior61c16482014-10-28 10:32:06 +01001832static int ath10k_pci_warm_reset(struct ath10k *ar)
1833{
1834 int ret;
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001835
Michal Kazior61c16482014-10-28 10:32:06 +01001836 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot warm reset\n");
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001837
Michal Kazior61c16482014-10-28 10:32:06 +01001838 spin_lock_bh(&ar->data_lock);
1839 ar->stats.fw_warm_reset_counter++;
1840 spin_unlock_bh(&ar->data_lock);
1841
1842 ath10k_pci_irq_disable(ar);
1843
1844 /* Make sure the target CPU is not doing anything dangerous, e.g. if it
1845 * were to access copy engine while host performs copy engine reset
1846 * then it is possible for the device to confuse pci-e controller to
1847 * the point of bringing host system to a complete stop (i.e. hang).
1848 */
Michal Kaziorde013572014-05-14 16:56:16 +03001849 ath10k_pci_warm_reset_si0(ar);
Michal Kazior61c16482014-10-28 10:32:06 +01001850 ath10k_pci_warm_reset_cpu(ar);
1851 ath10k_pci_init_pipes(ar);
1852 ath10k_pci_wait_for_target_init(ar);
Michal Kaziorde013572014-05-14 16:56:16 +03001853
Michal Kazior61c16482014-10-28 10:32:06 +01001854 ath10k_pci_warm_reset_clear_lf(ar);
1855 ath10k_pci_warm_reset_ce(ar);
1856 ath10k_pci_warm_reset_cpu(ar);
1857 ath10k_pci_init_pipes(ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001858
Michal Kazior61c16482014-10-28 10:32:06 +01001859 ret = ath10k_pci_wait_for_target_init(ar);
1860 if (ret) {
1861 ath10k_warn(ar, "failed to wait for target init: %d\n", ret);
1862 return ret;
1863 }
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001864
Michal Kazior7aa7a722014-08-25 12:09:38 +02001865 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot warm reset complete\n");
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001866
Michal Kaziorc0c378f2014-08-07 11:03:28 +02001867 return 0;
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001868}
1869
Michal Kaziord63955b2015-01-24 12:14:49 +02001870static int ath10k_pci_qca988x_chip_reset(struct ath10k *ar)
Michal Kazior0bc14d02014-10-28 10:32:07 +01001871{
1872 int i, ret;
1873 u32 val;
1874
Michal Kaziord63955b2015-01-24 12:14:49 +02001875 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot 988x chip reset\n");
Michal Kazior0bc14d02014-10-28 10:32:07 +01001876
1877 /* Some hardware revisions (e.g. CUS223v2) has issues with cold reset.
1878 * It is thus preferred to use warm reset which is safer but may not be
1879 * able to recover the device from all possible fail scenarios.
1880 *
1881 * Warm reset doesn't always work on first try so attempt it a few
1882 * times before giving up.
1883 */
1884 for (i = 0; i < ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS; i++) {
1885 ret = ath10k_pci_warm_reset(ar);
1886 if (ret) {
1887 ath10k_warn(ar, "failed to warm reset attempt %d of %d: %d\n",
1888 i + 1, ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS,
1889 ret);
1890 continue;
1891 }
1892
1893 /* FIXME: Sometimes copy engine doesn't recover after warm
1894 * reset. In most cases this needs cold reset. In some of these
1895 * cases the device is in such a state that a cold reset may
1896 * lock up the host.
1897 *
1898 * Reading any host interest register via copy engine is
1899 * sufficient to verify if device is capable of booting
1900 * firmware blob.
1901 */
1902 ret = ath10k_pci_init_pipes(ar);
1903 if (ret) {
1904 ath10k_warn(ar, "failed to init copy engine: %d\n",
1905 ret);
1906 continue;
1907 }
1908
1909 ret = ath10k_pci_diag_read32(ar, QCA988X_HOST_INTEREST_ADDRESS,
1910 &val);
1911 if (ret) {
1912 ath10k_warn(ar, "failed to poke copy engine: %d\n",
1913 ret);
1914 continue;
1915 }
1916
1917 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot chip reset complete (warm)\n");
1918 return 0;
1919 }
1920
1921 if (ath10k_pci_reset_mode == ATH10K_PCI_RESET_WARM_ONLY) {
1922 ath10k_warn(ar, "refusing cold reset as requested\n");
1923 return -EPERM;
1924 }
1925
1926 ret = ath10k_pci_cold_reset(ar);
1927 if (ret) {
1928 ath10k_warn(ar, "failed to cold reset: %d\n", ret);
1929 return ret;
1930 }
1931
1932 ret = ath10k_pci_wait_for_target_init(ar);
1933 if (ret) {
1934 ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
1935 ret);
1936 return ret;
1937 }
1938
Michal Kaziord63955b2015-01-24 12:14:49 +02001939 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca988x chip reset complete (cold)\n");
Michal Kazior0bc14d02014-10-28 10:32:07 +01001940
1941 return 0;
1942}
1943
Michal Kaziord63955b2015-01-24 12:14:49 +02001944static int ath10k_pci_qca6174_chip_reset(struct ath10k *ar)
1945{
1946 int ret;
1947
1948 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca6174 chip reset\n");
1949
1950 /* FIXME: QCA6174 requires cold + warm reset to work. */
1951
1952 ret = ath10k_pci_cold_reset(ar);
1953 if (ret) {
1954 ath10k_warn(ar, "failed to cold reset: %d\n", ret);
1955 return ret;
1956 }
1957
1958 ret = ath10k_pci_wait_for_target_init(ar);
1959 if (ret) {
1960 ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
1961 ret);
1962 return ret;
1963 }
1964
1965 ret = ath10k_pci_warm_reset(ar);
1966 if (ret) {
1967 ath10k_warn(ar, "failed to warm reset: %d\n", ret);
1968 return ret;
1969 }
1970
1971 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca6174 chip reset complete (cold)\n");
1972
1973 return 0;
1974}
1975
1976static int ath10k_pci_chip_reset(struct ath10k *ar)
1977{
1978 if (QCA_REV_988X(ar))
1979 return ath10k_pci_qca988x_chip_reset(ar);
1980 else if (QCA_REV_6174(ar))
1981 return ath10k_pci_qca6174_chip_reset(ar);
1982 else
1983 return -ENOTSUPP;
1984}
1985
Michal Kazior0bc14d02014-10-28 10:32:07 +01001986static int ath10k_pci_hif_power_up(struct ath10k *ar)
Michal Kazior8c5c5362013-07-16 09:38:50 +02001987{
Janusz Dziedzic76d870e2015-05-18 09:38:16 +00001988 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02001989 int ret;
1990
Michal Kazior0bc14d02014-10-28 10:32:07 +01001991 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif power up\n");
1992
Bartosz Markowski707b1bbd2014-10-31 09:03:43 +01001993 ret = ath10k_pci_wake(ar);
1994 if (ret) {
1995 ath10k_err(ar, "failed to wake up target: %d\n", ret);
1996 return ret;
1997 }
1998
Janusz Dziedzic76d870e2015-05-18 09:38:16 +00001999 pcie_capability_read_word(ar_pci->pdev, PCI_EXP_LNKCTL,
2000 &ar_pci->link_ctl);
2001 pcie_capability_write_word(ar_pci->pdev, PCI_EXP_LNKCTL,
2002 ar_pci->link_ctl & ~PCI_EXP_LNKCTL_ASPMC);
2003
Michal Kazior8c5c5362013-07-16 09:38:50 +02002004 /*
2005 * Bring the target up cleanly.
2006 *
2007 * The target may be in an undefined state with an AUX-powered Target
2008 * and a Host in WoW mode. If the Host crashes, loses power, or is
2009 * restarted (without unloading the driver) then the Target is left
2010 * (aux) powered and running. On a subsequent driver load, the Target
2011 * is in an unexpected state. We try to catch that here in order to
2012 * reset the Target and retry the probe.
2013 */
Michal Kazior0bc14d02014-10-28 10:32:07 +01002014 ret = ath10k_pci_chip_reset(ar);
Michal Kazior5b2589f2013-11-08 08:01:30 +01002015 if (ret) {
Michal Kaziora2fa8802015-01-12 15:29:37 +01002016 if (ath10k_pci_has_fw_crashed(ar)) {
2017 ath10k_warn(ar, "firmware crashed during chip reset\n");
2018 ath10k_pci_fw_crashed_clear(ar);
2019 ath10k_pci_fw_crashed_dump(ar);
2020 }
2021
Michal Kazior0bc14d02014-10-28 10:32:07 +01002022 ath10k_err(ar, "failed to reset chip: %d\n", ret);
Bartosz Markowski707b1bbd2014-10-31 09:03:43 +01002023 goto err_sleep;
Michal Kazior5b2589f2013-11-08 08:01:30 +01002024 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02002025
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002026 ret = ath10k_pci_init_pipes(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02002027 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002028 ath10k_err(ar, "failed to initialize CE: %d\n", ret);
Bartosz Markowski707b1bbd2014-10-31 09:03:43 +01002029 goto err_sleep;
Michal Kaziorab977bd2013-11-25 14:06:26 +01002030 }
2031
Michal Kazior98563d52013-11-08 08:01:33 +01002032 ret = ath10k_pci_init_config(ar);
2033 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002034 ath10k_err(ar, "failed to setup init config: %d\n", ret);
Michal Kazior5c771e72014-08-22 14:23:34 +02002035 goto err_ce;
Michal Kazior98563d52013-11-08 08:01:33 +01002036 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02002037
2038 ret = ath10k_pci_wake_target_cpu(ar);
2039 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002040 ath10k_err(ar, "could not wake up target CPU: %d\n", ret);
Michal Kazior5c771e72014-08-22 14:23:34 +02002041 goto err_ce;
Michal Kazior8c5c5362013-07-16 09:38:50 +02002042 }
2043
2044 return 0;
2045
2046err_ce:
2047 ath10k_pci_ce_deinit(ar);
Michal Kazior0bc14d02014-10-28 10:32:07 +01002048
Bartosz Markowski707b1bbd2014-10-31 09:03:43 +01002049err_sleep:
2050 ath10k_pci_sleep(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02002051 return ret;
2052}
2053
2054static void ath10k_pci_hif_power_down(struct ath10k *ar)
2055{
Michal Kazior7aa7a722014-08-25 12:09:38 +02002056 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif power down\n");
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002057
Michal Kaziorc011b282014-10-28 10:32:08 +01002058 /* Currently hif_power_up performs effectively a reset and hif_stop
2059 * resets the chip as well so there's no point in resetting here.
2060 */
Michal Kazior8c5c5362013-07-16 09:38:50 +02002061}
2062
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002063#ifdef CONFIG_PM
2064
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002065static int ath10k_pci_hif_suspend(struct ath10k *ar)
2066{
Michal Kazior320e14b2015-03-02 13:22:13 +01002067 ath10k_pci_sleep(ar);
2068
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002069 return 0;
2070}
2071
2072static int ath10k_pci_hif_resume(struct ath10k *ar)
2073{
2074 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2075 struct pci_dev *pdev = ar_pci->pdev;
2076 u32 val;
Michal Kazior320e14b2015-03-02 13:22:13 +01002077 int ret;
2078
2079 ret = ath10k_pci_wake(ar);
2080 if (ret) {
2081 ath10k_err(ar, "failed to wake device up on resume: %d\n", ret);
2082 return ret;
2083 }
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002084
Michal Kazior9ff4be92015-03-02 13:22:14 +01002085 /* Suspend/Resume resets the PCI configuration space, so we have to
2086 * re-disable the RETRY_TIMEOUT register (0x41) to keep PCI Tx retries
2087 * from interfering with C3 CPU state. pci_restore_state won't help
2088 * here since it only restores the first 64 bytes pci config header.
2089 */
2090 pci_read_config_dword(pdev, 0x40, &val);
2091 if ((val & 0x0000ff00) != 0)
2092 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002093
Michal Kazior320e14b2015-03-02 13:22:13 +01002094 return ret;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002095}
2096#endif
2097
Kalle Valo5e3dd152013-06-12 20:52:10 +03002098static const struct ath10k_hif_ops ath10k_pci_hif_ops = {
Michal Kazior726346f2014-02-27 18:50:04 +02002099 .tx_sg = ath10k_pci_hif_tx_sg,
Kalle Valoeef25402014-09-24 14:16:52 +03002100 .diag_read = ath10k_pci_hif_diag_read,
Yanbo Li9f65ad22014-11-25 12:24:48 +02002101 .diag_write = ath10k_pci_diag_write_mem,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002102 .exchange_bmi_msg = ath10k_pci_hif_exchange_bmi_msg,
2103 .start = ath10k_pci_hif_start,
2104 .stop = ath10k_pci_hif_stop,
2105 .map_service_to_pipe = ath10k_pci_hif_map_service_to_pipe,
2106 .get_default_pipe = ath10k_pci_hif_get_default_pipe,
2107 .send_complete_check = ath10k_pci_hif_send_complete_check,
Michal Kaziore799bbf2013-07-05 16:15:12 +03002108 .set_callbacks = ath10k_pci_hif_set_callbacks,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002109 .get_free_queue_number = ath10k_pci_hif_get_free_queue_number,
Michal Kazior8c5c5362013-07-16 09:38:50 +02002110 .power_up = ath10k_pci_hif_power_up,
2111 .power_down = ath10k_pci_hif_power_down,
Yanbo Li077a3802014-11-25 12:24:33 +02002112 .read32 = ath10k_pci_read32,
2113 .write32 = ath10k_pci_write32,
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002114#ifdef CONFIG_PM
2115 .suspend = ath10k_pci_hif_suspend,
2116 .resume = ath10k_pci_hif_resume,
2117#endif
Kalle Valo5e3dd152013-06-12 20:52:10 +03002118};
2119
2120static void ath10k_pci_ce_tasklet(unsigned long ptr)
2121{
Michal Kazior87263e52013-08-27 13:08:01 +02002122 struct ath10k_pci_pipe *pipe = (struct ath10k_pci_pipe *)ptr;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002123 struct ath10k_pci *ar_pci = pipe->ar_pci;
2124
2125 ath10k_ce_per_engine_service(ar_pci->ar, pipe->pipe_num);
2126}
2127
2128static void ath10k_msi_err_tasklet(unsigned long data)
2129{
2130 struct ath10k *ar = (struct ath10k *)data;
2131
Michal Kazior5c771e72014-08-22 14:23:34 +02002132 if (!ath10k_pci_has_fw_crashed(ar)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002133 ath10k_warn(ar, "received unsolicited fw crash interrupt\n");
Michal Kazior5c771e72014-08-22 14:23:34 +02002134 return;
2135 }
2136
Michal Kazior6f3b7ff2015-01-24 12:14:52 +02002137 ath10k_pci_irq_disable(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02002138 ath10k_pci_fw_crashed_clear(ar);
2139 ath10k_pci_fw_crashed_dump(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002140}
2141
2142/*
2143 * Handler for a per-engine interrupt on a PARTICULAR CE.
2144 * This is used in cases where each CE has a private MSI interrupt.
2145 */
2146static irqreturn_t ath10k_pci_per_engine_handler(int irq, void *arg)
2147{
2148 struct ath10k *ar = arg;
2149 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2150 int ce_id = irq - ar_pci->pdev->irq - MSI_ASSIGN_CE_INITIAL;
2151
Dan Carpentere5742672013-06-18 10:28:46 +03002152 if (ce_id < 0 || ce_id >= ARRAY_SIZE(ar_pci->pipe_info)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002153 ath10k_warn(ar, "unexpected/invalid irq %d ce_id %d\n", irq,
2154 ce_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002155 return IRQ_HANDLED;
2156 }
2157
2158 /*
2159 * NOTE: We are able to derive ce_id from irq because we
2160 * use a one-to-one mapping for CE's 0..5.
2161 * CE's 6 & 7 do not use interrupts at all.
2162 *
2163 * This mapping must be kept in sync with the mapping
2164 * used by firmware.
2165 */
2166 tasklet_schedule(&ar_pci->pipe_info[ce_id].intr);
2167 return IRQ_HANDLED;
2168}
2169
2170static irqreturn_t ath10k_pci_msi_fw_handler(int irq, void *arg)
2171{
2172 struct ath10k *ar = arg;
2173 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2174
2175 tasklet_schedule(&ar_pci->msi_fw_err);
2176 return IRQ_HANDLED;
2177}
2178
2179/*
2180 * Top-level interrupt handler for all PCI interrupts from a Target.
2181 * When a block of MSI interrupts is allocated, this top-level handler
2182 * is not used; instead, we directly call the correct sub-handler.
2183 */
2184static irqreturn_t ath10k_pci_interrupt_handler(int irq, void *arg)
2185{
2186 struct ath10k *ar = arg;
2187 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior320e14b2015-03-02 13:22:13 +01002188 int ret;
2189
2190 ret = ath10k_pci_wake(ar);
2191 if (ret) {
2192 ath10k_warn(ar, "failed to wake device up on irq: %d\n", ret);
2193 return IRQ_NONE;
2194 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002195
2196 if (ar_pci->num_msi_intrs == 0) {
Michal Kaziore5398872013-11-25 14:06:20 +01002197 if (!ath10k_pci_irq_pending(ar))
2198 return IRQ_NONE;
2199
Michal Kazior26852182013-11-25 14:06:25 +01002200 ath10k_pci_disable_and_clear_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002201 }
2202
2203 tasklet_schedule(&ar_pci->intr_tq);
2204
2205 return IRQ_HANDLED;
2206}
2207
2208static void ath10k_pci_tasklet(unsigned long data)
2209{
2210 struct ath10k *ar = (struct ath10k *)data;
2211 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2212
Michal Kazior5c771e72014-08-22 14:23:34 +02002213 if (ath10k_pci_has_fw_crashed(ar)) {
Michal Kazior6f3b7ff2015-01-24 12:14:52 +02002214 ath10k_pci_irq_disable(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02002215 ath10k_pci_fw_crashed_clear(ar);
2216 ath10k_pci_fw_crashed_dump(ar);
2217 return;
2218 }
2219
Kalle Valo5e3dd152013-06-12 20:52:10 +03002220 ath10k_ce_per_engine_service_any(ar);
2221
Michal Kazior26852182013-11-25 14:06:25 +01002222 /* Re-enable legacy irq that was disabled in the irq handler */
2223 if (ar_pci->num_msi_intrs == 0)
2224 ath10k_pci_enable_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002225}
2226
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002227static int ath10k_pci_request_irq_msix(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002228{
2229 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002230 int ret, i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002231
2232 ret = request_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW,
2233 ath10k_pci_msi_fw_handler,
2234 IRQF_SHARED, "ath10k_pci", ar);
Michal Kazior591ecdb2013-07-31 10:55:15 +02002235 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002236 ath10k_warn(ar, "failed to request MSI-X fw irq %d: %d\n",
Michal Kazior591ecdb2013-07-31 10:55:15 +02002237 ar_pci->pdev->irq + MSI_ASSIGN_FW, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002238 return ret;
Michal Kazior591ecdb2013-07-31 10:55:15 +02002239 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002240
2241 for (i = MSI_ASSIGN_CE_INITIAL; i <= MSI_ASSIGN_CE_MAX; i++) {
2242 ret = request_irq(ar_pci->pdev->irq + i,
2243 ath10k_pci_per_engine_handler,
2244 IRQF_SHARED, "ath10k_pci", ar);
2245 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002246 ath10k_warn(ar, "failed to request MSI-X ce irq %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002247 ar_pci->pdev->irq + i, ret);
2248
Michal Kazior87b14232013-06-26 08:50:50 +02002249 for (i--; i >= MSI_ASSIGN_CE_INITIAL; i--)
2250 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002251
Michal Kazior87b14232013-06-26 08:50:50 +02002252 free_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002253 return ret;
2254 }
2255 }
2256
Kalle Valo5e3dd152013-06-12 20:52:10 +03002257 return 0;
2258}
2259
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002260static int ath10k_pci_request_irq_msi(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002261{
2262 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2263 int ret;
2264
2265 ret = request_irq(ar_pci->pdev->irq,
2266 ath10k_pci_interrupt_handler,
2267 IRQF_SHARED, "ath10k_pci", ar);
Kalle Valof3782742013-10-17 11:36:15 +03002268 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002269 ath10k_warn(ar, "failed to request MSI irq %d: %d\n",
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002270 ar_pci->pdev->irq, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002271 return ret;
Kalle Valof3782742013-10-17 11:36:15 +03002272 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002273
Kalle Valo5e3dd152013-06-12 20:52:10 +03002274 return 0;
2275}
2276
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002277static int ath10k_pci_request_irq_legacy(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002278{
2279 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002280 int ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002281
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002282 ret = request_irq(ar_pci->pdev->irq,
2283 ath10k_pci_interrupt_handler,
2284 IRQF_SHARED, "ath10k_pci", ar);
Kalle Valof3782742013-10-17 11:36:15 +03002285 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002286 ath10k_warn(ar, "failed to request legacy irq %d: %d\n",
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002287 ar_pci->pdev->irq, ret);
Kalle Valof3782742013-10-17 11:36:15 +03002288 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002289 }
2290
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002291 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002292}
2293
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002294static int ath10k_pci_request_irq(struct ath10k *ar)
2295{
2296 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2297
2298 switch (ar_pci->num_msi_intrs) {
2299 case 0:
2300 return ath10k_pci_request_irq_legacy(ar);
2301 case 1:
2302 return ath10k_pci_request_irq_msi(ar);
2303 case MSI_NUM_REQUEST:
2304 return ath10k_pci_request_irq_msix(ar);
2305 }
2306
Michal Kazior7aa7a722014-08-25 12:09:38 +02002307 ath10k_warn(ar, "unknown irq configuration upon request\n");
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002308 return -EINVAL;
2309}
2310
2311static void ath10k_pci_free_irq(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002312{
2313 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2314 int i;
2315
2316 /* There's at least one interrupt irregardless whether its legacy INTR
2317 * or MSI or MSI-X */
2318 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
2319 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002320}
2321
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002322static void ath10k_pci_init_irq_tasklets(struct ath10k *ar)
2323{
2324 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2325 int i;
2326
2327 tasklet_init(&ar_pci->intr_tq, ath10k_pci_tasklet, (unsigned long)ar);
2328 tasklet_init(&ar_pci->msi_fw_err, ath10k_msi_err_tasklet,
2329 (unsigned long)ar);
2330
2331 for (i = 0; i < CE_COUNT; i++) {
2332 ar_pci->pipe_info[i].ar_pci = ar_pci;
2333 tasklet_init(&ar_pci->pipe_info[i].intr, ath10k_pci_ce_tasklet,
2334 (unsigned long)&ar_pci->pipe_info[i]);
2335 }
2336}
2337
2338static int ath10k_pci_init_irq(struct ath10k *ar)
2339{
2340 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2341 int ret;
2342
2343 ath10k_pci_init_irq_tasklets(ar);
2344
Michal Kazior403d6272014-08-22 14:23:31 +02002345 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_AUTO)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002346 ath10k_info(ar, "limiting irq mode to: %d\n",
2347 ath10k_pci_irq_mode);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002348
2349 /* Try MSI-X */
Michal Kazior0edf2572014-08-07 11:03:29 +02002350 if (ath10k_pci_irq_mode == ATH10K_PCI_IRQ_AUTO) {
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002351 ar_pci->num_msi_intrs = MSI_NUM_REQUEST;
Alexander Gordeev5ad68672014-02-13 17:50:02 +02002352 ret = pci_enable_msi_range(ar_pci->pdev, ar_pci->num_msi_intrs,
Kalle Valo5b07e072014-09-14 12:50:06 +03002353 ar_pci->num_msi_intrs);
Alexander Gordeev5ad68672014-02-13 17:50:02 +02002354 if (ret > 0)
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002355 return 0;
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002356
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002357 /* fall-through */
2358 }
2359
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002360 /* Try MSI */
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002361 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_LEGACY) {
2362 ar_pci->num_msi_intrs = 1;
2363 ret = pci_enable_msi(ar_pci->pdev);
2364 if (ret == 0)
2365 return 0;
2366
2367 /* fall-through */
2368 }
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002369
2370 /* Try legacy irq
2371 *
2372 * A potential race occurs here: The CORE_BASE write
2373 * depends on target correctly decoding AXI address but
2374 * host won't know when target writes BAR to CORE_CTRL.
2375 * This write might get lost if target has NOT written BAR.
2376 * For now, fix the race by repeating the write in below
2377 * synchronization checking. */
2378 ar_pci->num_msi_intrs = 0;
2379
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002380 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2381 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002382
2383 return 0;
2384}
2385
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002386static void ath10k_pci_deinit_irq_legacy(struct ath10k *ar)
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002387{
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002388 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2389 0);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002390}
2391
2392static int ath10k_pci_deinit_irq(struct ath10k *ar)
2393{
2394 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2395
2396 switch (ar_pci->num_msi_intrs) {
2397 case 0:
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002398 ath10k_pci_deinit_irq_legacy(ar);
2399 return 0;
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002400 case 1:
2401 /* fall-through */
2402 case MSI_NUM_REQUEST:
2403 pci_disable_msi(ar_pci->pdev);
2404 return 0;
Alexander Gordeevbb8b6212014-02-13 17:50:01 +02002405 default:
2406 pci_disable_msi(ar_pci->pdev);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002407 }
2408
Michal Kazior7aa7a722014-08-25 12:09:38 +02002409 ath10k_warn(ar, "unknown irq configuration upon deinit\n");
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002410 return -EINVAL;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002411}
2412
Michal Kaziord7fb47f2013-11-08 08:01:26 +01002413static int ath10k_pci_wait_for_target_init(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002414{
2415 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo0399eca2014-03-28 09:32:21 +02002416 unsigned long timeout;
Kalle Valo0399eca2014-03-28 09:32:21 +02002417 u32 val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002418
Michal Kazior7aa7a722014-08-25 12:09:38 +02002419 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot waiting target to initialise\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002420
Kalle Valo0399eca2014-03-28 09:32:21 +02002421 timeout = jiffies + msecs_to_jiffies(ATH10K_PCI_TARGET_WAIT);
2422
2423 do {
2424 val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
2425
Michal Kazior7aa7a722014-08-25 12:09:38 +02002426 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target indicator %x\n",
2427 val);
Kalle Valo50f87a62014-03-28 09:32:52 +02002428
Kalle Valo0399eca2014-03-28 09:32:21 +02002429 /* target should never return this */
2430 if (val == 0xffffffff)
2431 continue;
2432
Michal Kazior7710cd22014-04-23 19:30:04 +03002433 /* the device has crashed so don't bother trying anymore */
2434 if (val & FW_IND_EVENT_PENDING)
2435 break;
2436
Kalle Valo0399eca2014-03-28 09:32:21 +02002437 if (val & FW_IND_INITIALIZED)
2438 break;
2439
Kalle Valo5e3dd152013-06-12 20:52:10 +03002440 if (ar_pci->num_msi_intrs == 0)
2441 /* Fix potential race by repeating CORE_BASE writes */
Michal Kaziora4282492014-10-20 14:14:37 +02002442 ath10k_pci_enable_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002443
Kalle Valo0399eca2014-03-28 09:32:21 +02002444 mdelay(10);
2445 } while (time_before(jiffies, timeout));
2446
Michal Kaziora4282492014-10-20 14:14:37 +02002447 ath10k_pci_disable_and_clear_legacy_irq(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02002448 ath10k_pci_irq_msi_fw_mask(ar);
Michal Kaziora4282492014-10-20 14:14:37 +02002449
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002450 if (val == 0xffffffff) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002451 ath10k_err(ar, "failed to read device register, device is gone\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002452 return -EIO;
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002453 }
2454
Michal Kazior7710cd22014-04-23 19:30:04 +03002455 if (val & FW_IND_EVENT_PENDING) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002456 ath10k_warn(ar, "device has crashed during init\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002457 return -ECOMM;
Michal Kazior7710cd22014-04-23 19:30:04 +03002458 }
2459
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002460 if (!(val & FW_IND_INITIALIZED)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002461 ath10k_err(ar, "failed to receive initialized event from target: %08x\n",
Kalle Valo0399eca2014-03-28 09:32:21 +02002462 val);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002463 return -ETIMEDOUT;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002464 }
2465
Michal Kazior7aa7a722014-08-25 12:09:38 +02002466 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target initialised\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002467 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002468}
2469
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002470static int ath10k_pci_cold_reset(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002471{
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002472 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002473 u32 val;
2474
Michal Kazior7aa7a722014-08-25 12:09:38 +02002475 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cold reset\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002476
Ben Greearf51dbe72014-09-29 14:41:46 +03002477 spin_lock_bh(&ar->data_lock);
2478
2479 ar->stats.fw_cold_reset_counter++;
2480
2481 spin_unlock_bh(&ar->data_lock);
2482
Kalle Valo5e3dd152013-06-12 20:52:10 +03002483 /* Put Target, including PCIe, into RESET. */
Kalle Valoe479ed42013-09-01 10:01:53 +03002484 val = ath10k_pci_reg_read32(ar, SOC_GLOBAL_RESET_ADDRESS);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002485 val |= 1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002486 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002487
2488 for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
Kalle Valoe479ed42013-09-01 10:01:53 +03002489 if (ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03002490 RTC_STATE_COLD_RESET_MASK)
2491 break;
2492 msleep(1);
2493 }
2494
2495 /* Pull Target, including PCIe, out of RESET. */
2496 val &= ~1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002497 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002498
2499 for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
Kalle Valoe479ed42013-09-01 10:01:53 +03002500 if (!(ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03002501 RTC_STATE_COLD_RESET_MASK))
2502 break;
2503 msleep(1);
2504 }
2505
Michal Kazior7aa7a722014-08-25 12:09:38 +02002506 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cold reset complete\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02002507
Michal Kazior5b2589f2013-11-08 08:01:30 +01002508 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002509}
2510
Michal Kazior2986e3e2014-08-07 11:03:30 +02002511static int ath10k_pci_claim(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002512{
Michal Kazior2986e3e2014-08-07 11:03:30 +02002513 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2514 struct pci_dev *pdev = ar_pci->pdev;
Michal Kazior2986e3e2014-08-07 11:03:30 +02002515 int ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002516
2517 pci_set_drvdata(pdev, ar);
2518
Kalle Valo5e3dd152013-06-12 20:52:10 +03002519 ret = pci_enable_device(pdev);
2520 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002521 ath10k_err(ar, "failed to enable pci device: %d\n", ret);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002522 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002523 }
2524
Kalle Valo5e3dd152013-06-12 20:52:10 +03002525 ret = pci_request_region(pdev, BAR_NUM, "ath");
2526 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002527 ath10k_err(ar, "failed to request region BAR%d: %d\n", BAR_NUM,
Michal Kazior2986e3e2014-08-07 11:03:30 +02002528 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002529 goto err_device;
2530 }
2531
Michal Kazior2986e3e2014-08-07 11:03:30 +02002532 /* Target expects 32 bit DMA. Enforce it. */
Kalle Valo5e3dd152013-06-12 20:52:10 +03002533 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2534 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002535 ath10k_err(ar, "failed to set dma mask to 32-bit: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002536 goto err_region;
2537 }
2538
2539 ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2540 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002541 ath10k_err(ar, "failed to set consistent dma mask to 32-bit: %d\n",
Michal Kazior2986e3e2014-08-07 11:03:30 +02002542 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002543 goto err_region;
2544 }
2545
Kalle Valo5e3dd152013-06-12 20:52:10 +03002546 pci_set_master(pdev);
2547
Kalle Valo5e3dd152013-06-12 20:52:10 +03002548 /* Arrange for access to Target SoC registers. */
Michal Kazior2986e3e2014-08-07 11:03:30 +02002549 ar_pci->mem = pci_iomap(pdev, BAR_NUM, 0);
2550 if (!ar_pci->mem) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002551 ath10k_err(ar, "failed to iomap BAR%d\n", BAR_NUM);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002552 ret = -EIO;
2553 goto err_master;
2554 }
2555
Michal Kazior7aa7a722014-08-25 12:09:38 +02002556 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot pci_mem 0x%p\n", ar_pci->mem);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002557 return 0;
2558
2559err_master:
2560 pci_clear_master(pdev);
2561
2562err_region:
2563 pci_release_region(pdev, BAR_NUM);
2564
2565err_device:
2566 pci_disable_device(pdev);
2567
2568 return ret;
2569}
2570
2571static void ath10k_pci_release(struct ath10k *ar)
2572{
2573 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2574 struct pci_dev *pdev = ar_pci->pdev;
2575
2576 pci_iounmap(pdev, ar_pci->mem);
2577 pci_release_region(pdev, BAR_NUM);
2578 pci_clear_master(pdev);
2579 pci_disable_device(pdev);
2580}
2581
Michal Kazior7505f7c2014-12-02 10:55:54 +02002582static bool ath10k_pci_chip_is_supported(u32 dev_id, u32 chip_id)
2583{
2584 const struct ath10k_pci_supp_chip *supp_chip;
2585 int i;
2586 u32 rev_id = MS(chip_id, SOC_CHIP_ID_REV);
2587
2588 for (i = 0; i < ARRAY_SIZE(ath10k_pci_supp_chips); i++) {
2589 supp_chip = &ath10k_pci_supp_chips[i];
2590
2591 if (supp_chip->dev_id == dev_id &&
2592 supp_chip->rev_id == rev_id)
2593 return true;
2594 }
2595
2596 return false;
2597}
2598
Kalle Valo5e3dd152013-06-12 20:52:10 +03002599static int ath10k_pci_probe(struct pci_dev *pdev,
2600 const struct pci_device_id *pci_dev)
2601{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002602 int ret = 0;
2603 struct ath10k *ar;
2604 struct ath10k_pci *ar_pci;
Michal Kaziord63955b2015-01-24 12:14:49 +02002605 enum ath10k_hw_rev hw_rev;
Michal Kazior2986e3e2014-08-07 11:03:30 +02002606 u32 chip_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002607
Michal Kaziord63955b2015-01-24 12:14:49 +02002608 switch (pci_dev->device) {
2609 case QCA988X_2_0_DEVICE_ID:
2610 hw_rev = ATH10K_HW_QCA988X;
2611 break;
2612 case QCA6174_2_1_DEVICE_ID:
2613 hw_rev = ATH10K_HW_QCA6174;
2614 break;
2615 default:
2616 WARN_ON(1);
2617 return -ENOTSUPP;
2618 }
2619
2620 ar = ath10k_core_create(sizeof(*ar_pci), &pdev->dev, ATH10K_BUS_PCI,
2621 hw_rev, &ath10k_pci_hif_ops);
Michal Kaziore7b54192014-08-07 11:03:27 +02002622 if (!ar) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002623 dev_err(&pdev->dev, "failed to allocate core\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002624 return -ENOMEM;
Michal Kaziore7b54192014-08-07 11:03:27 +02002625 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002626
Michal Kazior7aa7a722014-08-25 12:09:38 +02002627 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci probe\n");
2628
Michal Kaziore7b54192014-08-07 11:03:27 +02002629 ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002630 ar_pci->pdev = pdev;
2631 ar_pci->dev = &pdev->dev;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002632 ar_pci->ar = ar;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002633
Michal Kaziorde57e2c2015-04-17 09:19:17 +00002634 if (pdev->subsystem_vendor || pdev->subsystem_device)
2635 scnprintf(ar->spec_board_id, sizeof(ar->spec_board_id),
2636 "%04x:%04x:%04x:%04x",
2637 pdev->vendor, pdev->device,
2638 pdev->subsystem_vendor, pdev->subsystem_device);
2639
Kalle Valo5e3dd152013-06-12 20:52:10 +03002640 spin_lock_init(&ar_pci->ce_lock);
Michal Kazior728f95e2014-08-22 14:33:14 +02002641 setup_timer(&ar_pci->rx_post_retry, ath10k_pci_rx_replenish_retry,
2642 (unsigned long)ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002643
Michal Kazior2986e3e2014-08-07 11:03:30 +02002644 ret = ath10k_pci_claim(ar);
Kalle Valoe01ae682013-09-01 11:22:14 +03002645 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002646 ath10k_err(ar, "failed to claim device: %d\n", ret);
Michal Kaziore7b54192014-08-07 11:03:27 +02002647 goto err_core_destroy;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002648 }
2649
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002650 ret = ath10k_pci_wake(ar);
Kalle Valoe01ae682013-09-01 11:22:14 +03002651 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002652 ath10k_err(ar, "failed to wake up: %d\n", ret);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002653 goto err_release;
Kalle Valoe01ae682013-09-01 11:22:14 +03002654 }
2655
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002656 ret = ath10k_pci_alloc_pipes(ar);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002657 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002658 ath10k_err(ar, "failed to allocate copy engine pipes: %d\n",
2659 ret);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002660 goto err_sleep;
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002661 }
2662
Michal Kazior403d6272014-08-22 14:23:31 +02002663 ath10k_pci_ce_deinit(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02002664 ath10k_pci_irq_disable(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02002665
Michal Kazior403d6272014-08-22 14:23:31 +02002666 ret = ath10k_pci_init_irq(ar);
2667 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002668 ath10k_err(ar, "failed to init irqs: %d\n", ret);
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002669 goto err_free_pipes;
Michal Kazior403d6272014-08-22 14:23:31 +02002670 }
2671
Michal Kazior7aa7a722014-08-25 12:09:38 +02002672 ath10k_info(ar, "pci irq %s interrupts %d irq_mode %d reset_mode %d\n",
Michal Kazior403d6272014-08-22 14:23:31 +02002673 ath10k_pci_get_irq_method(ar), ar_pci->num_msi_intrs,
2674 ath10k_pci_irq_mode, ath10k_pci_reset_mode);
2675
Michal Kazior5c771e72014-08-22 14:23:34 +02002676 ret = ath10k_pci_request_irq(ar);
Michal Kazior403d6272014-08-22 14:23:31 +02002677 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002678 ath10k_warn(ar, "failed to request irqs: %d\n", ret);
Michal Kazior403d6272014-08-22 14:23:31 +02002679 goto err_deinit_irq;
2680 }
2681
Michal Kazior1a7fecb2015-01-24 12:14:48 +02002682 ret = ath10k_pci_chip_reset(ar);
2683 if (ret) {
2684 ath10k_err(ar, "failed to reset chip: %d\n", ret);
2685 goto err_free_irq;
2686 }
2687
2688 chip_id = ath10k_pci_soc_read32(ar, SOC_CHIP_ID_ADDRESS);
2689 if (chip_id == 0xffffffff) {
2690 ath10k_err(ar, "failed to get chip id\n");
2691 goto err_free_irq;
2692 }
2693
2694 if (!ath10k_pci_chip_is_supported(pdev->device, chip_id)) {
2695 ath10k_err(ar, "device %04x with chip_id %08x isn't supported\n",
2696 pdev->device, chip_id);
Michal Kaziord9585a92015-04-10 13:01:27 +00002697 goto err_free_irq;
Michal Kazior1a7fecb2015-01-24 12:14:48 +02002698 }
2699
Kalle Valoe01ae682013-09-01 11:22:14 +03002700 ret = ath10k_core_register(ar, chip_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002701 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002702 ath10k_err(ar, "failed to register driver core: %d\n", ret);
Michal Kazior5c771e72014-08-22 14:23:34 +02002703 goto err_free_irq;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002704 }
2705
2706 return 0;
2707
Michal Kazior5c771e72014-08-22 14:23:34 +02002708err_free_irq:
2709 ath10k_pci_free_irq(ar);
Michal Kazior21396272014-08-28 10:24:40 +02002710 ath10k_pci_kill_tasklet(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02002711
Michal Kazior403d6272014-08-22 14:23:31 +02002712err_deinit_irq:
2713 ath10k_pci_deinit_irq(ar);
2714
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002715err_free_pipes:
2716 ath10k_pci_free_pipes(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002717
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002718err_sleep:
2719 ath10k_pci_sleep(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002720
2721err_release:
2722 ath10k_pci_release(ar);
2723
Michal Kaziore7b54192014-08-07 11:03:27 +02002724err_core_destroy:
Kalle Valo5e3dd152013-06-12 20:52:10 +03002725 ath10k_core_destroy(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002726
2727 return ret;
2728}
2729
2730static void ath10k_pci_remove(struct pci_dev *pdev)
2731{
2732 struct ath10k *ar = pci_get_drvdata(pdev);
2733 struct ath10k_pci *ar_pci;
2734
Michal Kazior7aa7a722014-08-25 12:09:38 +02002735 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci remove\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002736
2737 if (!ar)
2738 return;
2739
2740 ar_pci = ath10k_pci_priv(ar);
2741
2742 if (!ar_pci)
2743 return;
2744
Kalle Valo5e3dd152013-06-12 20:52:10 +03002745 ath10k_core_unregister(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02002746 ath10k_pci_free_irq(ar);
Michal Kazior21396272014-08-28 10:24:40 +02002747 ath10k_pci_kill_tasklet(ar);
Michal Kazior403d6272014-08-22 14:23:31 +02002748 ath10k_pci_deinit_irq(ar);
2749 ath10k_pci_ce_deinit(ar);
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002750 ath10k_pci_free_pipes(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002751 ath10k_pci_release(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002752 ath10k_core_destroy(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002753}
2754
Kalle Valo5e3dd152013-06-12 20:52:10 +03002755MODULE_DEVICE_TABLE(pci, ath10k_pci_id_table);
2756
2757static struct pci_driver ath10k_pci_driver = {
2758 .name = "ath10k_pci",
2759 .id_table = ath10k_pci_id_table,
2760 .probe = ath10k_pci_probe,
2761 .remove = ath10k_pci_remove,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002762};
2763
2764static int __init ath10k_pci_init(void)
2765{
2766 int ret;
2767
2768 ret = pci_register_driver(&ath10k_pci_driver);
2769 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002770 printk(KERN_ERR "failed to register ath10k pci driver: %d\n",
2771 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002772
2773 return ret;
2774}
2775module_init(ath10k_pci_init);
2776
2777static void __exit ath10k_pci_exit(void)
2778{
2779 pci_unregister_driver(&ath10k_pci_driver);
2780}
2781
2782module_exit(ath10k_pci_exit);
2783
2784MODULE_AUTHOR("Qualcomm Atheros");
2785MODULE_DESCRIPTION("Driver support for Atheros QCA988X PCIe devices");
2786MODULE_LICENSE("Dual BSD/GPL");
Bartosz Markowski5c427f52015-02-18 13:16:37 +01002787
2788/* QCA988x 2.0 firmware files */
Bartosz Markowski8026cae2014-10-06 14:16:41 +02002789MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_FW_FILE);
2790MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API2_FILE);
2791MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API3_FILE);
Bartosz Markowski5c427f52015-02-18 13:16:37 +01002792MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API4_FILE);
Kalle Valo53513c32015-03-25 13:12:42 +02002793MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API5_FILE);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002794MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_BOARD_DATA_FILE);
Bartosz Markowski5c427f52015-02-18 13:16:37 +01002795
2796/* QCA6174 2.1 firmware files */
2797MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" ATH10K_FW_API4_FILE);
2798MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" QCA6174_HW_2_1_BOARD_DATA_FILE);
2799
2800/* QCA6174 3.1 firmware files */
2801MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" ATH10K_FW_API4_FILE);
2802MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" QCA6174_HW_3_0_BOARD_DATA_FILE);