blob: f2d7cf1ad3a9e9204a89c3954ab9ef3cf5905c45 [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{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001230 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif start\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001231
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001232 ath10k_pci_irq_enable(ar);
Michal Kazior728f95e2014-08-22 14:33:14 +02001233 ath10k_pci_rx_post(ar);
Kalle Valo50f87a62014-03-28 09:32:52 +02001234
Kalle Valo5e3dd152013-06-12 20:52:10 +03001235 return 0;
1236}
1237
Michal Kazior099ac7c2014-10-28 10:32:05 +01001238static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe *pci_pipe)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001239{
1240 struct ath10k *ar;
Michal Kazior099ac7c2014-10-28 10:32:05 +01001241 struct ath10k_ce_pipe *ce_pipe;
1242 struct ath10k_ce_ring *ce_ring;
1243 struct sk_buff *skb;
1244 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001245
Michal Kazior099ac7c2014-10-28 10:32:05 +01001246 ar = pci_pipe->hif_ce_state;
1247 ce_pipe = pci_pipe->ce_hdl;
1248 ce_ring = ce_pipe->dest_ring;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001249
Michal Kazior099ac7c2014-10-28 10:32:05 +01001250 if (!ce_ring)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001251 return;
1252
Michal Kazior099ac7c2014-10-28 10:32:05 +01001253 if (!pci_pipe->buf_sz)
1254 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001255
Michal Kazior099ac7c2014-10-28 10:32:05 +01001256 for (i = 0; i < ce_ring->nentries; i++) {
1257 skb = ce_ring->per_transfer_context[i];
1258 if (!skb)
1259 continue;
1260
1261 ce_ring->per_transfer_context[i] = NULL;
1262
Michal Kazior8582bf32015-01-24 12:14:47 +02001263 dma_unmap_single(ar->dev, ATH10K_SKB_RXCB(skb)->paddr,
Michal Kazior099ac7c2014-10-28 10:32:05 +01001264 skb->len + skb_tailroom(skb),
Kalle Valo5e3dd152013-06-12 20:52:10 +03001265 DMA_FROM_DEVICE);
Michal Kazior099ac7c2014-10-28 10:32:05 +01001266 dev_kfree_skb_any(skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001267 }
1268}
1269
Michal Kazior099ac7c2014-10-28 10:32:05 +01001270static void ath10k_pci_tx_pipe_cleanup(struct ath10k_pci_pipe *pci_pipe)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001271{
1272 struct ath10k *ar;
1273 struct ath10k_pci *ar_pci;
Michal Kazior099ac7c2014-10-28 10:32:05 +01001274 struct ath10k_ce_pipe *ce_pipe;
1275 struct ath10k_ce_ring *ce_ring;
1276 struct ce_desc *ce_desc;
1277 struct sk_buff *skb;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001278 unsigned int id;
Michal Kazior099ac7c2014-10-28 10:32:05 +01001279 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001280
Michal Kazior099ac7c2014-10-28 10:32:05 +01001281 ar = pci_pipe->hif_ce_state;
1282 ar_pci = ath10k_pci_priv(ar);
1283 ce_pipe = pci_pipe->ce_hdl;
1284 ce_ring = ce_pipe->src_ring;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001285
Michal Kazior099ac7c2014-10-28 10:32:05 +01001286 if (!ce_ring)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001287 return;
1288
Michal Kazior099ac7c2014-10-28 10:32:05 +01001289 if (!pci_pipe->buf_sz)
1290 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001291
Michal Kazior099ac7c2014-10-28 10:32:05 +01001292 ce_desc = ce_ring->shadow_base;
1293 if (WARN_ON(!ce_desc))
1294 return;
1295
1296 for (i = 0; i < ce_ring->nentries; i++) {
1297 skb = ce_ring->per_transfer_context[i];
1298 if (!skb)
Michal Kazior2415fc12013-11-08 08:01:32 +01001299 continue;
Michal Kazior2415fc12013-11-08 08:01:32 +01001300
Michal Kazior099ac7c2014-10-28 10:32:05 +01001301 ce_ring->per_transfer_context[i] = NULL;
1302 id = MS(__le16_to_cpu(ce_desc[i].flags),
1303 CE_DESC_FLAGS_META_DATA);
1304
Michal Kaziord84a5122014-11-27 11:09:37 +01001305 ar_pci->msg_callbacks_current.tx_completion(ar, skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001306 }
1307}
1308
1309/*
1310 * Cleanup residual buffers for device shutdown:
1311 * buffers that were enqueued for receive
1312 * buffers that were to be sent
1313 * Note: Buffers that had completed but which were
1314 * not yet processed are on a completion queue. They
1315 * are handled when the completion thread shuts down.
1316 */
1317static void ath10k_pci_buffer_cleanup(struct ath10k *ar)
1318{
1319 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1320 int pipe_num;
1321
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001322 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Michal Kazior87263e52013-08-27 13:08:01 +02001323 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001324
1325 pipe_info = &ar_pci->pipe_info[pipe_num];
1326 ath10k_pci_rx_pipe_cleanup(pipe_info);
1327 ath10k_pci_tx_pipe_cleanup(pipe_info);
1328 }
1329}
1330
1331static void ath10k_pci_ce_deinit(struct ath10k *ar)
1332{
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001333 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001334
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001335 for (i = 0; i < CE_COUNT; i++)
1336 ath10k_ce_deinit_pipe(ar, i);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001337}
1338
Michal Kazior728f95e2014-08-22 14:33:14 +02001339static void ath10k_pci_flush(struct ath10k *ar)
1340{
1341 ath10k_pci_kill_tasklet(ar);
1342 ath10k_pci_buffer_cleanup(ar);
1343}
1344
Kalle Valo5e3dd152013-06-12 20:52:10 +03001345static void ath10k_pci_hif_stop(struct ath10k *ar)
1346{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001347 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif stop\n");
Michal Kazior32270b62013-08-02 09:15:47 +02001348
Michal Kazior10d23db2014-08-22 14:33:15 +02001349 /* Most likely the device has HTT Rx ring configured. The only way to
1350 * prevent the device from accessing (and possible corrupting) host
1351 * memory is to reset the chip now.
Michal Kaziore75db4e2014-08-28 22:14:16 +03001352 *
1353 * There's also no known way of masking MSI interrupts on the device.
1354 * For ranged MSI the CE-related interrupts can be masked. However
1355 * regardless how many MSI interrupts are assigned the first one
1356 * is always used for firmware indications (crashes) and cannot be
1357 * masked. To prevent the device from asserting the interrupt reset it
1358 * before proceeding with cleanup.
Michal Kazior10d23db2014-08-22 14:33:15 +02001359 */
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001360 ath10k_pci_warm_reset(ar);
Michal Kaziore75db4e2014-08-28 22:14:16 +03001361
1362 ath10k_pci_irq_disable(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001363 ath10k_pci_irq_sync(ar);
Michal Kaziore75db4e2014-08-28 22:14:16 +03001364 ath10k_pci_flush(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001365}
1366
1367static int ath10k_pci_hif_exchange_bmi_msg(struct ath10k *ar,
1368 void *req, u32 req_len,
1369 void *resp, u32 *resp_len)
1370{
1371 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2aa39112013-08-27 13:08:02 +02001372 struct ath10k_pci_pipe *pci_tx = &ar_pci->pipe_info[BMI_CE_NUM_TO_TARG];
1373 struct ath10k_pci_pipe *pci_rx = &ar_pci->pipe_info[BMI_CE_NUM_TO_HOST];
1374 struct ath10k_ce_pipe *ce_tx = pci_tx->ce_hdl;
1375 struct ath10k_ce_pipe *ce_rx = pci_rx->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001376 dma_addr_t req_paddr = 0;
1377 dma_addr_t resp_paddr = 0;
1378 struct bmi_xfer xfer = {};
1379 void *treq, *tresp = NULL;
1380 int ret = 0;
1381
Michal Kazior85622cd2013-11-25 14:06:22 +01001382 might_sleep();
1383
Kalle Valo5e3dd152013-06-12 20:52:10 +03001384 if (resp && !resp_len)
1385 return -EINVAL;
1386
1387 if (resp && resp_len && *resp_len == 0)
1388 return -EINVAL;
1389
1390 treq = kmemdup(req, req_len, GFP_KERNEL);
1391 if (!treq)
1392 return -ENOMEM;
1393
1394 req_paddr = dma_map_single(ar->dev, treq, req_len, DMA_TO_DEVICE);
1395 ret = dma_mapping_error(ar->dev, req_paddr);
1396 if (ret)
1397 goto err_dma;
1398
1399 if (resp && resp_len) {
1400 tresp = kzalloc(*resp_len, GFP_KERNEL);
1401 if (!tresp) {
1402 ret = -ENOMEM;
1403 goto err_req;
1404 }
1405
1406 resp_paddr = dma_map_single(ar->dev, tresp, *resp_len,
1407 DMA_FROM_DEVICE);
1408 ret = dma_mapping_error(ar->dev, resp_paddr);
1409 if (ret)
1410 goto err_req;
1411
1412 xfer.wait_for_resp = true;
1413 xfer.resp_len = 0;
1414
Michal Kazior728f95e2014-08-22 14:33:14 +02001415 ath10k_ce_rx_post_buf(ce_rx, &xfer, resp_paddr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001416 }
1417
Kalle Valo5e3dd152013-06-12 20:52:10 +03001418 ret = ath10k_ce_send(ce_tx, &xfer, req_paddr, req_len, -1, 0);
1419 if (ret)
1420 goto err_resp;
1421
Michal Kazior85622cd2013-11-25 14:06:22 +01001422 ret = ath10k_pci_bmi_wait(ce_tx, ce_rx, &xfer);
1423 if (ret) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001424 u32 unused_buffer;
1425 unsigned int unused_nbytes;
1426 unsigned int unused_id;
1427
Kalle Valo5e3dd152013-06-12 20:52:10 +03001428 ath10k_ce_cancel_send_next(ce_tx, NULL, &unused_buffer,
1429 &unused_nbytes, &unused_id);
1430 } else {
1431 /* non-zero means we did not time out */
1432 ret = 0;
1433 }
1434
1435err_resp:
1436 if (resp) {
1437 u32 unused_buffer;
1438
1439 ath10k_ce_revoke_recv_next(ce_rx, NULL, &unused_buffer);
1440 dma_unmap_single(ar->dev, resp_paddr,
1441 *resp_len, DMA_FROM_DEVICE);
1442 }
1443err_req:
1444 dma_unmap_single(ar->dev, req_paddr, req_len, DMA_TO_DEVICE);
1445
1446 if (ret == 0 && resp_len) {
1447 *resp_len = min(*resp_len, xfer.resp_len);
1448 memcpy(resp, tresp, xfer.resp_len);
1449 }
1450err_dma:
1451 kfree(treq);
1452 kfree(tresp);
1453
1454 return ret;
1455}
1456
Michal Kazior5440ce22013-09-03 15:09:58 +02001457static void ath10k_pci_bmi_send_done(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001458{
Michal Kazior5440ce22013-09-03 15:09:58 +02001459 struct bmi_xfer *xfer;
1460 u32 ce_data;
1461 unsigned int nbytes;
1462 unsigned int transfer_id;
1463
1464 if (ath10k_ce_completed_send_next(ce_state, (void **)&xfer, &ce_data,
1465 &nbytes, &transfer_id))
1466 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001467
Michal Kazior2374b182014-07-14 16:25:25 +03001468 xfer->tx_done = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001469}
1470
Michal Kazior5440ce22013-09-03 15:09:58 +02001471static void ath10k_pci_bmi_recv_data(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001472{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001473 struct ath10k *ar = ce_state->ar;
Michal Kazior5440ce22013-09-03 15:09:58 +02001474 struct bmi_xfer *xfer;
1475 u32 ce_data;
1476 unsigned int nbytes;
1477 unsigned int transfer_id;
1478 unsigned int flags;
1479
1480 if (ath10k_ce_completed_recv_next(ce_state, (void **)&xfer, &ce_data,
1481 &nbytes, &transfer_id, &flags))
1482 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001483
Michal Kazior04ed9df2014-10-28 10:34:36 +01001484 if (WARN_ON_ONCE(!xfer))
1485 return;
1486
Kalle Valo5e3dd152013-06-12 20:52:10 +03001487 if (!xfer->wait_for_resp) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001488 ath10k_warn(ar, "unexpected: BMI data received; ignoring\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001489 return;
1490 }
1491
1492 xfer->resp_len = nbytes;
Michal Kazior2374b182014-07-14 16:25:25 +03001493 xfer->rx_done = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001494}
1495
Michal Kazior85622cd2013-11-25 14:06:22 +01001496static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
1497 struct ath10k_ce_pipe *rx_pipe,
1498 struct bmi_xfer *xfer)
1499{
1500 unsigned long timeout = jiffies + BMI_COMMUNICATION_TIMEOUT_HZ;
1501
1502 while (time_before_eq(jiffies, timeout)) {
1503 ath10k_pci_bmi_send_done(tx_pipe);
1504 ath10k_pci_bmi_recv_data(rx_pipe);
1505
Michal Kazior2374b182014-07-14 16:25:25 +03001506 if (xfer->tx_done && (xfer->rx_done == xfer->wait_for_resp))
Michal Kazior85622cd2013-11-25 14:06:22 +01001507 return 0;
1508
1509 schedule();
1510 }
1511
1512 return -ETIMEDOUT;
1513}
1514
Kalle Valo5e3dd152013-06-12 20:52:10 +03001515/*
Kalle Valo5e3dd152013-06-12 20:52:10 +03001516 * Send an interrupt to the device to wake up the Target CPU
1517 * so it has an opportunity to notice any changed state.
1518 */
1519static int ath10k_pci_wake_target_cpu(struct ath10k *ar)
1520{
Michal Kazior9e264942014-09-02 11:00:21 +03001521 u32 addr, val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001522
Michal Kazior9e264942014-09-02 11:00:21 +03001523 addr = SOC_CORE_BASE_ADDRESS | CORE_CTRL_ADDRESS;
1524 val = ath10k_pci_read32(ar, addr);
1525 val |= CORE_CTRL_CPU_INTR_MASK;
1526 ath10k_pci_write32(ar, addr, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001527
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001528 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001529}
1530
Michal Kaziord63955b2015-01-24 12:14:49 +02001531static int ath10k_pci_get_num_banks(struct ath10k *ar)
1532{
1533 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1534
1535 switch (ar_pci->pdev->device) {
1536 case QCA988X_2_0_DEVICE_ID:
1537 return 1;
1538 case QCA6174_2_1_DEVICE_ID:
1539 switch (MS(ar->chip_id, SOC_CHIP_ID_REV)) {
1540 case QCA6174_HW_1_0_CHIP_ID_REV:
1541 case QCA6174_HW_1_1_CHIP_ID_REV:
1542 return 3;
1543 case QCA6174_HW_1_3_CHIP_ID_REV:
1544 return 2;
1545 case QCA6174_HW_2_1_CHIP_ID_REV:
1546 case QCA6174_HW_2_2_CHIP_ID_REV:
1547 return 6;
1548 case QCA6174_HW_3_0_CHIP_ID_REV:
1549 case QCA6174_HW_3_1_CHIP_ID_REV:
1550 case QCA6174_HW_3_2_CHIP_ID_REV:
1551 return 9;
1552 }
1553 break;
1554 }
1555
1556 ath10k_warn(ar, "unknown number of banks, assuming 1\n");
1557 return 1;
1558}
1559
Kalle Valo5e3dd152013-06-12 20:52:10 +03001560static int ath10k_pci_init_config(struct ath10k *ar)
1561{
1562 u32 interconnect_targ_addr;
1563 u32 pcie_state_targ_addr = 0;
1564 u32 pipe_cfg_targ_addr = 0;
1565 u32 svc_to_pipe_map = 0;
1566 u32 pcie_config_flags = 0;
1567 u32 ealloc_value;
1568 u32 ealloc_targ_addr;
1569 u32 flag2_value;
1570 u32 flag2_targ_addr;
1571 int ret = 0;
1572
1573 /* Download to Target the CE Config and the service-to-CE map */
1574 interconnect_targ_addr =
1575 host_interest_item_address(HI_ITEM(hi_interconnect_state));
1576
1577 /* Supply Target-side CE configuration */
Michal Kazior9e264942014-09-02 11:00:21 +03001578 ret = ath10k_pci_diag_read32(ar, interconnect_targ_addr,
1579 &pcie_state_targ_addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001580 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001581 ath10k_err(ar, "Failed to get pcie state addr: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001582 return ret;
1583 }
1584
1585 if (pcie_state_targ_addr == 0) {
1586 ret = -EIO;
Michal Kazior7aa7a722014-08-25 12:09:38 +02001587 ath10k_err(ar, "Invalid pcie state addr\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001588 return ret;
1589 }
1590
Michal Kazior9e264942014-09-02 11:00:21 +03001591 ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
Kalle Valo5e3dd152013-06-12 20:52:10 +03001592 offsetof(struct pcie_state,
Michal Kazior9e264942014-09-02 11:00:21 +03001593 pipe_cfg_addr)),
1594 &pipe_cfg_targ_addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001595 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001596 ath10k_err(ar, "Failed to get pipe cfg addr: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001597 return ret;
1598 }
1599
1600 if (pipe_cfg_targ_addr == 0) {
1601 ret = -EIO;
Michal Kazior7aa7a722014-08-25 12:09:38 +02001602 ath10k_err(ar, "Invalid pipe cfg addr\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001603 return ret;
1604 }
1605
1606 ret = ath10k_pci_diag_write_mem(ar, pipe_cfg_targ_addr,
Kalle Valo5b07e072014-09-14 12:50:06 +03001607 target_ce_config_wlan,
1608 sizeof(target_ce_config_wlan));
Kalle Valo5e3dd152013-06-12 20:52:10 +03001609
1610 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001611 ath10k_err(ar, "Failed to write pipe cfg: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001612 return ret;
1613 }
1614
Michal Kazior9e264942014-09-02 11:00:21 +03001615 ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
Kalle Valo5e3dd152013-06-12 20:52:10 +03001616 offsetof(struct pcie_state,
Michal Kazior9e264942014-09-02 11:00:21 +03001617 svc_to_pipe_map)),
1618 &svc_to_pipe_map);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001619 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001620 ath10k_err(ar, "Failed to get svc/pipe map: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001621 return ret;
1622 }
1623
1624 if (svc_to_pipe_map == 0) {
1625 ret = -EIO;
Michal Kazior7aa7a722014-08-25 12:09:38 +02001626 ath10k_err(ar, "Invalid svc_to_pipe map\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001627 return ret;
1628 }
1629
1630 ret = ath10k_pci_diag_write_mem(ar, svc_to_pipe_map,
Kalle Valo5b07e072014-09-14 12:50:06 +03001631 target_service_to_ce_map_wlan,
1632 sizeof(target_service_to_ce_map_wlan));
Kalle Valo5e3dd152013-06-12 20:52:10 +03001633 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001634 ath10k_err(ar, "Failed to write svc/pipe map: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001635 return ret;
1636 }
1637
Michal Kazior9e264942014-09-02 11:00:21 +03001638 ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
Kalle Valo5e3dd152013-06-12 20:52:10 +03001639 offsetof(struct pcie_state,
Michal Kazior9e264942014-09-02 11:00:21 +03001640 config_flags)),
1641 &pcie_config_flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001642 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001643 ath10k_err(ar, "Failed to get pcie config_flags: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001644 return ret;
1645 }
1646
1647 pcie_config_flags &= ~PCIE_CONFIG_FLAG_ENABLE_L1;
1648
Michal Kazior9e264942014-09-02 11:00:21 +03001649 ret = ath10k_pci_diag_write32(ar, (pcie_state_targ_addr +
1650 offsetof(struct pcie_state,
1651 config_flags)),
1652 pcie_config_flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001653 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001654 ath10k_err(ar, "Failed to write pcie config_flags: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001655 return ret;
1656 }
1657
1658 /* configure early allocation */
1659 ealloc_targ_addr = host_interest_item_address(HI_ITEM(hi_early_alloc));
1660
Michal Kazior9e264942014-09-02 11:00:21 +03001661 ret = ath10k_pci_diag_read32(ar, ealloc_targ_addr, &ealloc_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001662 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001663 ath10k_err(ar, "Faile to get early alloc val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001664 return ret;
1665 }
1666
1667 /* first bank is switched to IRAM */
1668 ealloc_value |= ((HI_EARLY_ALLOC_MAGIC << HI_EARLY_ALLOC_MAGIC_SHIFT) &
1669 HI_EARLY_ALLOC_MAGIC_MASK);
Michal Kaziord63955b2015-01-24 12:14:49 +02001670 ealloc_value |= ((ath10k_pci_get_num_banks(ar) <<
1671 HI_EARLY_ALLOC_IRAM_BANKS_SHIFT) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03001672 HI_EARLY_ALLOC_IRAM_BANKS_MASK);
1673
Michal Kazior9e264942014-09-02 11:00:21 +03001674 ret = ath10k_pci_diag_write32(ar, ealloc_targ_addr, ealloc_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001675 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001676 ath10k_err(ar, "Failed to set early alloc val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001677 return ret;
1678 }
1679
1680 /* Tell Target to proceed with initialization */
1681 flag2_targ_addr = host_interest_item_address(HI_ITEM(hi_option_flag2));
1682
Michal Kazior9e264942014-09-02 11:00:21 +03001683 ret = ath10k_pci_diag_read32(ar, flag2_targ_addr, &flag2_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001684 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001685 ath10k_err(ar, "Failed to get option val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001686 return ret;
1687 }
1688
1689 flag2_value |= HI_OPTION_EARLY_CFG_DONE;
1690
Michal Kazior9e264942014-09-02 11:00:21 +03001691 ret = ath10k_pci_diag_write32(ar, flag2_targ_addr, flag2_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001692 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001693 ath10k_err(ar, "Failed to set option val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001694 return ret;
1695 }
1696
1697 return 0;
1698}
1699
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001700static int ath10k_pci_alloc_pipes(struct ath10k *ar)
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001701{
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001702 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1703 struct ath10k_pci_pipe *pipe;
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001704 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001705
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001706 for (i = 0; i < CE_COUNT; i++) {
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001707 pipe = &ar_pci->pipe_info[i];
1708 pipe->ce_hdl = &ar_pci->ce_states[i];
1709 pipe->pipe_num = i;
1710 pipe->hif_ce_state = ar;
1711
1712 ret = ath10k_ce_alloc_pipe(ar, i, &host_ce_config_wlan[i],
1713 ath10k_pci_ce_send_done,
1714 ath10k_pci_ce_recv_data);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001715 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001716 ath10k_err(ar, "failed to allocate copy engine pipe %d: %d\n",
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001717 i, ret);
1718 return ret;
1719 }
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001720
1721 /* Last CE is Diagnostic Window */
1722 if (i == CE_COUNT - 1) {
1723 ar_pci->ce_diag = pipe->ce_hdl;
1724 continue;
1725 }
1726
1727 pipe->buf_sz = (size_t)(host_ce_config_wlan[i].src_sz_max);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001728 }
1729
1730 return 0;
1731}
1732
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001733static void ath10k_pci_free_pipes(struct ath10k *ar)
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001734{
1735 int i;
1736
1737 for (i = 0; i < CE_COUNT; i++)
1738 ath10k_ce_free_pipe(ar, i);
1739}
Kalle Valo5e3dd152013-06-12 20:52:10 +03001740
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001741static int ath10k_pci_init_pipes(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001742{
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001743 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001744
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001745 for (i = 0; i < CE_COUNT; i++) {
1746 ret = ath10k_ce_init_pipe(ar, i, &host_ce_config_wlan[i]);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001747 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001748 ath10k_err(ar, "failed to initialize copy engine pipe %d: %d\n",
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001749 i, ret);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001750 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001751 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001752 }
1753
Kalle Valo5e3dd152013-06-12 20:52:10 +03001754 return 0;
1755}
1756
Michal Kazior5c771e72014-08-22 14:23:34 +02001757static bool ath10k_pci_has_fw_crashed(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001758{
Michal Kazior5c771e72014-08-22 14:23:34 +02001759 return ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS) &
1760 FW_IND_EVENT_PENDING;
1761}
Kalle Valo5e3dd152013-06-12 20:52:10 +03001762
Michal Kazior5c771e72014-08-22 14:23:34 +02001763static void ath10k_pci_fw_crashed_clear(struct ath10k *ar)
1764{
1765 u32 val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001766
Michal Kazior5c771e72014-08-22 14:23:34 +02001767 val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
1768 val &= ~FW_IND_EVENT_PENDING;
1769 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001770}
1771
Michal Kaziorde013572014-05-14 16:56:16 +03001772/* this function effectively clears target memory controller assert line */
1773static void ath10k_pci_warm_reset_si0(struct ath10k *ar)
1774{
1775 u32 val;
1776
1777 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1778 ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
1779 val | SOC_RESET_CONTROL_SI0_RST_MASK);
1780 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1781
1782 msleep(10);
1783
1784 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1785 ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
1786 val & ~SOC_RESET_CONTROL_SI0_RST_MASK);
1787 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1788
1789 msleep(10);
1790}
1791
Michal Kazior61c16482014-10-28 10:32:06 +01001792static void ath10k_pci_warm_reset_cpu(struct ath10k *ar)
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001793{
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001794 u32 val;
1795
Kalle Valob39712c2014-03-28 09:32:46 +02001796 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, 0);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001797
Michal Kazior61c16482014-10-28 10:32:06 +01001798 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1799 SOC_RESET_CONTROL_ADDRESS);
1800 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1801 val | SOC_RESET_CONTROL_CPU_WARM_RST_MASK);
1802}
1803
1804static void ath10k_pci_warm_reset_ce(struct ath10k *ar)
1805{
1806 u32 val;
1807
1808 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1809 SOC_RESET_CONTROL_ADDRESS);
1810
1811 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1812 val | SOC_RESET_CONTROL_CE_RST_MASK);
1813 msleep(10);
1814 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1815 val & ~SOC_RESET_CONTROL_CE_RST_MASK);
1816}
1817
1818static void ath10k_pci_warm_reset_clear_lf(struct ath10k *ar)
1819{
1820 u32 val;
1821
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001822 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1823 SOC_LF_TIMER_CONTROL0_ADDRESS);
1824 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS +
1825 SOC_LF_TIMER_CONTROL0_ADDRESS,
1826 val & ~SOC_LF_TIMER_CONTROL0_ENABLE_MASK);
Michal Kazior61c16482014-10-28 10:32:06 +01001827}
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001828
Michal Kazior61c16482014-10-28 10:32:06 +01001829static int ath10k_pci_warm_reset(struct ath10k *ar)
1830{
1831 int ret;
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001832
Michal Kazior61c16482014-10-28 10:32:06 +01001833 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot warm reset\n");
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001834
Michal Kazior61c16482014-10-28 10:32:06 +01001835 spin_lock_bh(&ar->data_lock);
1836 ar->stats.fw_warm_reset_counter++;
1837 spin_unlock_bh(&ar->data_lock);
1838
1839 ath10k_pci_irq_disable(ar);
1840
1841 /* Make sure the target CPU is not doing anything dangerous, e.g. if it
1842 * were to access copy engine while host performs copy engine reset
1843 * then it is possible for the device to confuse pci-e controller to
1844 * the point of bringing host system to a complete stop (i.e. hang).
1845 */
Michal Kaziorde013572014-05-14 16:56:16 +03001846 ath10k_pci_warm_reset_si0(ar);
Michal Kazior61c16482014-10-28 10:32:06 +01001847 ath10k_pci_warm_reset_cpu(ar);
1848 ath10k_pci_init_pipes(ar);
1849 ath10k_pci_wait_for_target_init(ar);
Michal Kaziorde013572014-05-14 16:56:16 +03001850
Michal Kazior61c16482014-10-28 10:32:06 +01001851 ath10k_pci_warm_reset_clear_lf(ar);
1852 ath10k_pci_warm_reset_ce(ar);
1853 ath10k_pci_warm_reset_cpu(ar);
1854 ath10k_pci_init_pipes(ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001855
Michal Kazior61c16482014-10-28 10:32:06 +01001856 ret = ath10k_pci_wait_for_target_init(ar);
1857 if (ret) {
1858 ath10k_warn(ar, "failed to wait for target init: %d\n", ret);
1859 return ret;
1860 }
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001861
Michal Kazior7aa7a722014-08-25 12:09:38 +02001862 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot warm reset complete\n");
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001863
Michal Kaziorc0c378f2014-08-07 11:03:28 +02001864 return 0;
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001865}
1866
Michal Kaziord63955b2015-01-24 12:14:49 +02001867static int ath10k_pci_qca988x_chip_reset(struct ath10k *ar)
Michal Kazior0bc14d02014-10-28 10:32:07 +01001868{
1869 int i, ret;
1870 u32 val;
1871
Michal Kaziord63955b2015-01-24 12:14:49 +02001872 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot 988x chip reset\n");
Michal Kazior0bc14d02014-10-28 10:32:07 +01001873
1874 /* Some hardware revisions (e.g. CUS223v2) has issues with cold reset.
1875 * It is thus preferred to use warm reset which is safer but may not be
1876 * able to recover the device from all possible fail scenarios.
1877 *
1878 * Warm reset doesn't always work on first try so attempt it a few
1879 * times before giving up.
1880 */
1881 for (i = 0; i < ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS; i++) {
1882 ret = ath10k_pci_warm_reset(ar);
1883 if (ret) {
1884 ath10k_warn(ar, "failed to warm reset attempt %d of %d: %d\n",
1885 i + 1, ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS,
1886 ret);
1887 continue;
1888 }
1889
1890 /* FIXME: Sometimes copy engine doesn't recover after warm
1891 * reset. In most cases this needs cold reset. In some of these
1892 * cases the device is in such a state that a cold reset may
1893 * lock up the host.
1894 *
1895 * Reading any host interest register via copy engine is
1896 * sufficient to verify if device is capable of booting
1897 * firmware blob.
1898 */
1899 ret = ath10k_pci_init_pipes(ar);
1900 if (ret) {
1901 ath10k_warn(ar, "failed to init copy engine: %d\n",
1902 ret);
1903 continue;
1904 }
1905
1906 ret = ath10k_pci_diag_read32(ar, QCA988X_HOST_INTEREST_ADDRESS,
1907 &val);
1908 if (ret) {
1909 ath10k_warn(ar, "failed to poke copy engine: %d\n",
1910 ret);
1911 continue;
1912 }
1913
1914 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot chip reset complete (warm)\n");
1915 return 0;
1916 }
1917
1918 if (ath10k_pci_reset_mode == ATH10K_PCI_RESET_WARM_ONLY) {
1919 ath10k_warn(ar, "refusing cold reset as requested\n");
1920 return -EPERM;
1921 }
1922
1923 ret = ath10k_pci_cold_reset(ar);
1924 if (ret) {
1925 ath10k_warn(ar, "failed to cold reset: %d\n", ret);
1926 return ret;
1927 }
1928
1929 ret = ath10k_pci_wait_for_target_init(ar);
1930 if (ret) {
1931 ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
1932 ret);
1933 return ret;
1934 }
1935
Michal Kaziord63955b2015-01-24 12:14:49 +02001936 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca988x chip reset complete (cold)\n");
Michal Kazior0bc14d02014-10-28 10:32:07 +01001937
1938 return 0;
1939}
1940
Michal Kaziord63955b2015-01-24 12:14:49 +02001941static int ath10k_pci_qca6174_chip_reset(struct ath10k *ar)
1942{
1943 int ret;
1944
1945 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca6174 chip reset\n");
1946
1947 /* FIXME: QCA6174 requires cold + warm reset to work. */
1948
1949 ret = ath10k_pci_cold_reset(ar);
1950 if (ret) {
1951 ath10k_warn(ar, "failed to cold reset: %d\n", ret);
1952 return ret;
1953 }
1954
1955 ret = ath10k_pci_wait_for_target_init(ar);
1956 if (ret) {
1957 ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
1958 ret);
1959 return ret;
1960 }
1961
1962 ret = ath10k_pci_warm_reset(ar);
1963 if (ret) {
1964 ath10k_warn(ar, "failed to warm reset: %d\n", ret);
1965 return ret;
1966 }
1967
1968 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca6174 chip reset complete (cold)\n");
1969
1970 return 0;
1971}
1972
1973static int ath10k_pci_chip_reset(struct ath10k *ar)
1974{
1975 if (QCA_REV_988X(ar))
1976 return ath10k_pci_qca988x_chip_reset(ar);
1977 else if (QCA_REV_6174(ar))
1978 return ath10k_pci_qca6174_chip_reset(ar);
1979 else
1980 return -ENOTSUPP;
1981}
1982
Michal Kazior0bc14d02014-10-28 10:32:07 +01001983static int ath10k_pci_hif_power_up(struct ath10k *ar)
Michal Kazior8c5c5362013-07-16 09:38:50 +02001984{
1985 int ret;
1986
Michal Kazior0bc14d02014-10-28 10:32:07 +01001987 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif power up\n");
1988
Bartosz Markowski707b1bbd2014-10-31 09:03:43 +01001989 ret = ath10k_pci_wake(ar);
1990 if (ret) {
1991 ath10k_err(ar, "failed to wake up target: %d\n", ret);
1992 return ret;
1993 }
1994
Michal Kazior8c5c5362013-07-16 09:38:50 +02001995 /*
1996 * Bring the target up cleanly.
1997 *
1998 * The target may be in an undefined state with an AUX-powered Target
1999 * and a Host in WoW mode. If the Host crashes, loses power, or is
2000 * restarted (without unloading the driver) then the Target is left
2001 * (aux) powered and running. On a subsequent driver load, the Target
2002 * is in an unexpected state. We try to catch that here in order to
2003 * reset the Target and retry the probe.
2004 */
Michal Kazior0bc14d02014-10-28 10:32:07 +01002005 ret = ath10k_pci_chip_reset(ar);
Michal Kazior5b2589f2013-11-08 08:01:30 +01002006 if (ret) {
Michal Kaziora2fa8802015-01-12 15:29:37 +01002007 if (ath10k_pci_has_fw_crashed(ar)) {
2008 ath10k_warn(ar, "firmware crashed during chip reset\n");
2009 ath10k_pci_fw_crashed_clear(ar);
2010 ath10k_pci_fw_crashed_dump(ar);
2011 }
2012
Michal Kazior0bc14d02014-10-28 10:32:07 +01002013 ath10k_err(ar, "failed to reset chip: %d\n", ret);
Bartosz Markowski707b1bbd2014-10-31 09:03:43 +01002014 goto err_sleep;
Michal Kazior5b2589f2013-11-08 08:01:30 +01002015 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02002016
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002017 ret = ath10k_pci_init_pipes(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02002018 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002019 ath10k_err(ar, "failed to initialize CE: %d\n", ret);
Bartosz Markowski707b1bbd2014-10-31 09:03:43 +01002020 goto err_sleep;
Michal Kaziorab977bd2013-11-25 14:06:26 +01002021 }
2022
Michal Kazior98563d52013-11-08 08:01:33 +01002023 ret = ath10k_pci_init_config(ar);
2024 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002025 ath10k_err(ar, "failed to setup init config: %d\n", ret);
Michal Kazior5c771e72014-08-22 14:23:34 +02002026 goto err_ce;
Michal Kazior98563d52013-11-08 08:01:33 +01002027 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02002028
2029 ret = ath10k_pci_wake_target_cpu(ar);
2030 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002031 ath10k_err(ar, "could not wake up target CPU: %d\n", ret);
Michal Kazior5c771e72014-08-22 14:23:34 +02002032 goto err_ce;
Michal Kazior8c5c5362013-07-16 09:38:50 +02002033 }
2034
2035 return 0;
2036
2037err_ce:
2038 ath10k_pci_ce_deinit(ar);
Michal Kazior0bc14d02014-10-28 10:32:07 +01002039
Bartosz Markowski707b1bbd2014-10-31 09:03:43 +01002040err_sleep:
2041 ath10k_pci_sleep(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02002042 return ret;
2043}
2044
2045static void ath10k_pci_hif_power_down(struct ath10k *ar)
2046{
Michal Kazior7aa7a722014-08-25 12:09:38 +02002047 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif power down\n");
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002048
Michal Kaziorc011b282014-10-28 10:32:08 +01002049 /* Currently hif_power_up performs effectively a reset and hif_stop
2050 * resets the chip as well so there's no point in resetting here.
2051 */
Michal Kazior8c5c5362013-07-16 09:38:50 +02002052}
2053
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002054#ifdef CONFIG_PM
2055
2056#define ATH10K_PCI_PM_CONTROL 0x44
2057
2058static int ath10k_pci_hif_suspend(struct ath10k *ar)
2059{
2060 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2061 struct pci_dev *pdev = ar_pci->pdev;
2062 u32 val;
2063
Michal Kazior320e14b2015-03-02 13:22:13 +01002064 ath10k_pci_sleep(ar);
2065
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002066 pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
2067
2068 if ((val & 0x000000ff) != 0x3) {
2069 pci_save_state(pdev);
2070 pci_disable_device(pdev);
2071 pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
2072 (val & 0xffffff00) | 0x03);
2073 }
2074
2075 return 0;
2076}
2077
2078static int ath10k_pci_hif_resume(struct ath10k *ar)
2079{
2080 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2081 struct pci_dev *pdev = ar_pci->pdev;
2082 u32 val;
Michal Kazior320e14b2015-03-02 13:22:13 +01002083 int ret;
2084
2085 ret = ath10k_pci_wake(ar);
2086 if (ret) {
2087 ath10k_err(ar, "failed to wake device up on resume: %d\n", ret);
2088 return ret;
2089 }
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002090
2091 pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
2092
2093 if ((val & 0x000000ff) != 0) {
2094 pci_restore_state(pdev);
2095 pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
2096 val & 0xffffff00);
2097 /*
2098 * Suspend/Resume resets the PCI configuration space,
2099 * so we have to re-disable the RETRY_TIMEOUT register (0x41)
2100 * to keep PCI Tx retries from interfering with C3 CPU state
2101 */
2102 pci_read_config_dword(pdev, 0x40, &val);
2103
2104 if ((val & 0x0000ff00) != 0)
2105 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
2106 }
2107
Michal Kazior320e14b2015-03-02 13:22:13 +01002108 return ret;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002109}
2110#endif
2111
Kalle Valo5e3dd152013-06-12 20:52:10 +03002112static const struct ath10k_hif_ops ath10k_pci_hif_ops = {
Michal Kazior726346f2014-02-27 18:50:04 +02002113 .tx_sg = ath10k_pci_hif_tx_sg,
Kalle Valoeef25402014-09-24 14:16:52 +03002114 .diag_read = ath10k_pci_hif_diag_read,
Yanbo Li9f65ad22014-11-25 12:24:48 +02002115 .diag_write = ath10k_pci_diag_write_mem,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002116 .exchange_bmi_msg = ath10k_pci_hif_exchange_bmi_msg,
2117 .start = ath10k_pci_hif_start,
2118 .stop = ath10k_pci_hif_stop,
2119 .map_service_to_pipe = ath10k_pci_hif_map_service_to_pipe,
2120 .get_default_pipe = ath10k_pci_hif_get_default_pipe,
2121 .send_complete_check = ath10k_pci_hif_send_complete_check,
Michal Kaziore799bbf2013-07-05 16:15:12 +03002122 .set_callbacks = ath10k_pci_hif_set_callbacks,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002123 .get_free_queue_number = ath10k_pci_hif_get_free_queue_number,
Michal Kazior8c5c5362013-07-16 09:38:50 +02002124 .power_up = ath10k_pci_hif_power_up,
2125 .power_down = ath10k_pci_hif_power_down,
Yanbo Li077a3802014-11-25 12:24:33 +02002126 .read32 = ath10k_pci_read32,
2127 .write32 = ath10k_pci_write32,
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002128#ifdef CONFIG_PM
2129 .suspend = ath10k_pci_hif_suspend,
2130 .resume = ath10k_pci_hif_resume,
2131#endif
Kalle Valo5e3dd152013-06-12 20:52:10 +03002132};
2133
2134static void ath10k_pci_ce_tasklet(unsigned long ptr)
2135{
Michal Kazior87263e52013-08-27 13:08:01 +02002136 struct ath10k_pci_pipe *pipe = (struct ath10k_pci_pipe *)ptr;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002137 struct ath10k_pci *ar_pci = pipe->ar_pci;
2138
2139 ath10k_ce_per_engine_service(ar_pci->ar, pipe->pipe_num);
2140}
2141
2142static void ath10k_msi_err_tasklet(unsigned long data)
2143{
2144 struct ath10k *ar = (struct ath10k *)data;
2145
Michal Kazior5c771e72014-08-22 14:23:34 +02002146 if (!ath10k_pci_has_fw_crashed(ar)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002147 ath10k_warn(ar, "received unsolicited fw crash interrupt\n");
Michal Kazior5c771e72014-08-22 14:23:34 +02002148 return;
2149 }
2150
Michal Kazior6f3b7ff2015-01-24 12:14:52 +02002151 ath10k_pci_irq_disable(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02002152 ath10k_pci_fw_crashed_clear(ar);
2153 ath10k_pci_fw_crashed_dump(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002154}
2155
2156/*
2157 * Handler for a per-engine interrupt on a PARTICULAR CE.
2158 * This is used in cases where each CE has a private MSI interrupt.
2159 */
2160static irqreturn_t ath10k_pci_per_engine_handler(int irq, void *arg)
2161{
2162 struct ath10k *ar = arg;
2163 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2164 int ce_id = irq - ar_pci->pdev->irq - MSI_ASSIGN_CE_INITIAL;
2165
Dan Carpentere5742672013-06-18 10:28:46 +03002166 if (ce_id < 0 || ce_id >= ARRAY_SIZE(ar_pci->pipe_info)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002167 ath10k_warn(ar, "unexpected/invalid irq %d ce_id %d\n", irq,
2168 ce_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002169 return IRQ_HANDLED;
2170 }
2171
2172 /*
2173 * NOTE: We are able to derive ce_id from irq because we
2174 * use a one-to-one mapping for CE's 0..5.
2175 * CE's 6 & 7 do not use interrupts at all.
2176 *
2177 * This mapping must be kept in sync with the mapping
2178 * used by firmware.
2179 */
2180 tasklet_schedule(&ar_pci->pipe_info[ce_id].intr);
2181 return IRQ_HANDLED;
2182}
2183
2184static irqreturn_t ath10k_pci_msi_fw_handler(int irq, void *arg)
2185{
2186 struct ath10k *ar = arg;
2187 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2188
2189 tasklet_schedule(&ar_pci->msi_fw_err);
2190 return IRQ_HANDLED;
2191}
2192
2193/*
2194 * Top-level interrupt handler for all PCI interrupts from a Target.
2195 * When a block of MSI interrupts is allocated, this top-level handler
2196 * is not used; instead, we directly call the correct sub-handler.
2197 */
2198static irqreturn_t ath10k_pci_interrupt_handler(int irq, void *arg)
2199{
2200 struct ath10k *ar = arg;
2201 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior320e14b2015-03-02 13:22:13 +01002202 int ret;
2203
2204 ret = ath10k_pci_wake(ar);
2205 if (ret) {
2206 ath10k_warn(ar, "failed to wake device up on irq: %d\n", ret);
2207 return IRQ_NONE;
2208 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002209
2210 if (ar_pci->num_msi_intrs == 0) {
Michal Kaziore5398872013-11-25 14:06:20 +01002211 if (!ath10k_pci_irq_pending(ar))
2212 return IRQ_NONE;
2213
Michal Kazior26852182013-11-25 14:06:25 +01002214 ath10k_pci_disable_and_clear_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002215 }
2216
2217 tasklet_schedule(&ar_pci->intr_tq);
2218
2219 return IRQ_HANDLED;
2220}
2221
2222static void ath10k_pci_tasklet(unsigned long data)
2223{
2224 struct ath10k *ar = (struct ath10k *)data;
2225 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2226
Michal Kazior5c771e72014-08-22 14:23:34 +02002227 if (ath10k_pci_has_fw_crashed(ar)) {
Michal Kazior6f3b7ff2015-01-24 12:14:52 +02002228 ath10k_pci_irq_disable(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02002229 ath10k_pci_fw_crashed_clear(ar);
2230 ath10k_pci_fw_crashed_dump(ar);
2231 return;
2232 }
2233
Kalle Valo5e3dd152013-06-12 20:52:10 +03002234 ath10k_ce_per_engine_service_any(ar);
2235
Michal Kazior26852182013-11-25 14:06:25 +01002236 /* Re-enable legacy irq that was disabled in the irq handler */
2237 if (ar_pci->num_msi_intrs == 0)
2238 ath10k_pci_enable_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002239}
2240
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002241static int ath10k_pci_request_irq_msix(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002242{
2243 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002244 int ret, i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002245
2246 ret = request_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW,
2247 ath10k_pci_msi_fw_handler,
2248 IRQF_SHARED, "ath10k_pci", ar);
Michal Kazior591ecdb2013-07-31 10:55:15 +02002249 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002250 ath10k_warn(ar, "failed to request MSI-X fw irq %d: %d\n",
Michal Kazior591ecdb2013-07-31 10:55:15 +02002251 ar_pci->pdev->irq + MSI_ASSIGN_FW, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002252 return ret;
Michal Kazior591ecdb2013-07-31 10:55:15 +02002253 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002254
2255 for (i = MSI_ASSIGN_CE_INITIAL; i <= MSI_ASSIGN_CE_MAX; i++) {
2256 ret = request_irq(ar_pci->pdev->irq + i,
2257 ath10k_pci_per_engine_handler,
2258 IRQF_SHARED, "ath10k_pci", ar);
2259 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002260 ath10k_warn(ar, "failed to request MSI-X ce irq %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002261 ar_pci->pdev->irq + i, ret);
2262
Michal Kazior87b14232013-06-26 08:50:50 +02002263 for (i--; i >= MSI_ASSIGN_CE_INITIAL; i--)
2264 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002265
Michal Kazior87b14232013-06-26 08:50:50 +02002266 free_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002267 return ret;
2268 }
2269 }
2270
Kalle Valo5e3dd152013-06-12 20:52:10 +03002271 return 0;
2272}
2273
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002274static int ath10k_pci_request_irq_msi(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002275{
2276 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2277 int ret;
2278
2279 ret = request_irq(ar_pci->pdev->irq,
2280 ath10k_pci_interrupt_handler,
2281 IRQF_SHARED, "ath10k_pci", ar);
Kalle Valof3782742013-10-17 11:36:15 +03002282 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002283 ath10k_warn(ar, "failed to request MSI irq %d: %d\n",
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002284 ar_pci->pdev->irq, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002285 return ret;
Kalle Valof3782742013-10-17 11:36:15 +03002286 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002287
Kalle Valo5e3dd152013-06-12 20:52:10 +03002288 return 0;
2289}
2290
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002291static int ath10k_pci_request_irq_legacy(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002292{
2293 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002294 int ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002295
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002296 ret = request_irq(ar_pci->pdev->irq,
2297 ath10k_pci_interrupt_handler,
2298 IRQF_SHARED, "ath10k_pci", ar);
Kalle Valof3782742013-10-17 11:36:15 +03002299 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002300 ath10k_warn(ar, "failed to request legacy irq %d: %d\n",
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002301 ar_pci->pdev->irq, ret);
Kalle Valof3782742013-10-17 11:36:15 +03002302 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002303 }
2304
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002305 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002306}
2307
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002308static int ath10k_pci_request_irq(struct ath10k *ar)
2309{
2310 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2311
2312 switch (ar_pci->num_msi_intrs) {
2313 case 0:
2314 return ath10k_pci_request_irq_legacy(ar);
2315 case 1:
2316 return ath10k_pci_request_irq_msi(ar);
2317 case MSI_NUM_REQUEST:
2318 return ath10k_pci_request_irq_msix(ar);
2319 }
2320
Michal Kazior7aa7a722014-08-25 12:09:38 +02002321 ath10k_warn(ar, "unknown irq configuration upon request\n");
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002322 return -EINVAL;
2323}
2324
2325static void ath10k_pci_free_irq(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002326{
2327 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2328 int i;
2329
2330 /* There's at least one interrupt irregardless whether its legacy INTR
2331 * or MSI or MSI-X */
2332 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
2333 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002334}
2335
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002336static void ath10k_pci_init_irq_tasklets(struct ath10k *ar)
2337{
2338 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2339 int i;
2340
2341 tasklet_init(&ar_pci->intr_tq, ath10k_pci_tasklet, (unsigned long)ar);
2342 tasklet_init(&ar_pci->msi_fw_err, ath10k_msi_err_tasklet,
2343 (unsigned long)ar);
2344
2345 for (i = 0; i < CE_COUNT; i++) {
2346 ar_pci->pipe_info[i].ar_pci = ar_pci;
2347 tasklet_init(&ar_pci->pipe_info[i].intr, ath10k_pci_ce_tasklet,
2348 (unsigned long)&ar_pci->pipe_info[i]);
2349 }
2350}
2351
2352static int ath10k_pci_init_irq(struct ath10k *ar)
2353{
2354 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2355 int ret;
2356
2357 ath10k_pci_init_irq_tasklets(ar);
2358
Michal Kazior403d6272014-08-22 14:23:31 +02002359 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_AUTO)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002360 ath10k_info(ar, "limiting irq mode to: %d\n",
2361 ath10k_pci_irq_mode);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002362
2363 /* Try MSI-X */
Michal Kazior0edf2572014-08-07 11:03:29 +02002364 if (ath10k_pci_irq_mode == ATH10K_PCI_IRQ_AUTO) {
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002365 ar_pci->num_msi_intrs = MSI_NUM_REQUEST;
Alexander Gordeev5ad68672014-02-13 17:50:02 +02002366 ret = pci_enable_msi_range(ar_pci->pdev, ar_pci->num_msi_intrs,
Kalle Valo5b07e072014-09-14 12:50:06 +03002367 ar_pci->num_msi_intrs);
Alexander Gordeev5ad68672014-02-13 17:50:02 +02002368 if (ret > 0)
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002369 return 0;
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002370
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002371 /* fall-through */
2372 }
2373
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002374 /* Try MSI */
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002375 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_LEGACY) {
2376 ar_pci->num_msi_intrs = 1;
2377 ret = pci_enable_msi(ar_pci->pdev);
2378 if (ret == 0)
2379 return 0;
2380
2381 /* fall-through */
2382 }
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002383
2384 /* Try legacy irq
2385 *
2386 * A potential race occurs here: The CORE_BASE write
2387 * depends on target correctly decoding AXI address but
2388 * host won't know when target writes BAR to CORE_CTRL.
2389 * This write might get lost if target has NOT written BAR.
2390 * For now, fix the race by repeating the write in below
2391 * synchronization checking. */
2392 ar_pci->num_msi_intrs = 0;
2393
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002394 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2395 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002396
2397 return 0;
2398}
2399
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002400static void ath10k_pci_deinit_irq_legacy(struct ath10k *ar)
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002401{
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002402 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2403 0);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002404}
2405
2406static int ath10k_pci_deinit_irq(struct ath10k *ar)
2407{
2408 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2409
2410 switch (ar_pci->num_msi_intrs) {
2411 case 0:
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002412 ath10k_pci_deinit_irq_legacy(ar);
2413 return 0;
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002414 case 1:
2415 /* fall-through */
2416 case MSI_NUM_REQUEST:
2417 pci_disable_msi(ar_pci->pdev);
2418 return 0;
Alexander Gordeevbb8b6212014-02-13 17:50:01 +02002419 default:
2420 pci_disable_msi(ar_pci->pdev);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002421 }
2422
Michal Kazior7aa7a722014-08-25 12:09:38 +02002423 ath10k_warn(ar, "unknown irq configuration upon deinit\n");
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002424 return -EINVAL;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002425}
2426
Michal Kaziord7fb47f2013-11-08 08:01:26 +01002427static int ath10k_pci_wait_for_target_init(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002428{
2429 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo0399eca2014-03-28 09:32:21 +02002430 unsigned long timeout;
Kalle Valo0399eca2014-03-28 09:32:21 +02002431 u32 val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002432
Michal Kazior7aa7a722014-08-25 12:09:38 +02002433 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot waiting target to initialise\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002434
Kalle Valo0399eca2014-03-28 09:32:21 +02002435 timeout = jiffies + msecs_to_jiffies(ATH10K_PCI_TARGET_WAIT);
2436
2437 do {
2438 val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
2439
Michal Kazior7aa7a722014-08-25 12:09:38 +02002440 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target indicator %x\n",
2441 val);
Kalle Valo50f87a62014-03-28 09:32:52 +02002442
Kalle Valo0399eca2014-03-28 09:32:21 +02002443 /* target should never return this */
2444 if (val == 0xffffffff)
2445 continue;
2446
Michal Kazior7710cd22014-04-23 19:30:04 +03002447 /* the device has crashed so don't bother trying anymore */
2448 if (val & FW_IND_EVENT_PENDING)
2449 break;
2450
Kalle Valo0399eca2014-03-28 09:32:21 +02002451 if (val & FW_IND_INITIALIZED)
2452 break;
2453
Kalle Valo5e3dd152013-06-12 20:52:10 +03002454 if (ar_pci->num_msi_intrs == 0)
2455 /* Fix potential race by repeating CORE_BASE writes */
Michal Kaziora4282492014-10-20 14:14:37 +02002456 ath10k_pci_enable_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002457
Kalle Valo0399eca2014-03-28 09:32:21 +02002458 mdelay(10);
2459 } while (time_before(jiffies, timeout));
2460
Michal Kaziora4282492014-10-20 14:14:37 +02002461 ath10k_pci_disable_and_clear_legacy_irq(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02002462 ath10k_pci_irq_msi_fw_mask(ar);
Michal Kaziora4282492014-10-20 14:14:37 +02002463
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002464 if (val == 0xffffffff) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002465 ath10k_err(ar, "failed to read device register, device is gone\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002466 return -EIO;
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002467 }
2468
Michal Kazior7710cd22014-04-23 19:30:04 +03002469 if (val & FW_IND_EVENT_PENDING) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002470 ath10k_warn(ar, "device has crashed during init\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002471 return -ECOMM;
Michal Kazior7710cd22014-04-23 19:30:04 +03002472 }
2473
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002474 if (!(val & FW_IND_INITIALIZED)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002475 ath10k_err(ar, "failed to receive initialized event from target: %08x\n",
Kalle Valo0399eca2014-03-28 09:32:21 +02002476 val);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002477 return -ETIMEDOUT;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002478 }
2479
Michal Kazior7aa7a722014-08-25 12:09:38 +02002480 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target initialised\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002481 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002482}
2483
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002484static int ath10k_pci_cold_reset(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002485{
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002486 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002487 u32 val;
2488
Michal Kazior7aa7a722014-08-25 12:09:38 +02002489 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cold reset\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002490
Ben Greearf51dbe72014-09-29 14:41:46 +03002491 spin_lock_bh(&ar->data_lock);
2492
2493 ar->stats.fw_cold_reset_counter++;
2494
2495 spin_unlock_bh(&ar->data_lock);
2496
Kalle Valo5e3dd152013-06-12 20:52:10 +03002497 /* Put Target, including PCIe, into RESET. */
Kalle Valoe479ed42013-09-01 10:01:53 +03002498 val = ath10k_pci_reg_read32(ar, SOC_GLOBAL_RESET_ADDRESS);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002499 val |= 1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002500 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002501
2502 for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
Kalle Valoe479ed42013-09-01 10:01:53 +03002503 if (ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03002504 RTC_STATE_COLD_RESET_MASK)
2505 break;
2506 msleep(1);
2507 }
2508
2509 /* Pull Target, including PCIe, out of RESET. */
2510 val &= ~1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002511 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002512
2513 for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
Kalle Valoe479ed42013-09-01 10:01:53 +03002514 if (!(ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03002515 RTC_STATE_COLD_RESET_MASK))
2516 break;
2517 msleep(1);
2518 }
2519
Michal Kazior7aa7a722014-08-25 12:09:38 +02002520 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cold reset complete\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02002521
Michal Kazior5b2589f2013-11-08 08:01:30 +01002522 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002523}
2524
Michal Kazior2986e3e2014-08-07 11:03:30 +02002525static int ath10k_pci_claim(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002526{
Michal Kazior2986e3e2014-08-07 11:03:30 +02002527 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2528 struct pci_dev *pdev = ar_pci->pdev;
2529 u32 lcr_val;
2530 int ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002531
2532 pci_set_drvdata(pdev, ar);
2533
Kalle Valo5e3dd152013-06-12 20:52:10 +03002534 ret = pci_enable_device(pdev);
2535 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002536 ath10k_err(ar, "failed to enable pci device: %d\n", ret);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002537 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002538 }
2539
Kalle Valo5e3dd152013-06-12 20:52:10 +03002540 ret = pci_request_region(pdev, BAR_NUM, "ath");
2541 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002542 ath10k_err(ar, "failed to request region BAR%d: %d\n", BAR_NUM,
Michal Kazior2986e3e2014-08-07 11:03:30 +02002543 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002544 goto err_device;
2545 }
2546
Michal Kazior2986e3e2014-08-07 11:03:30 +02002547 /* Target expects 32 bit DMA. Enforce it. */
Kalle Valo5e3dd152013-06-12 20:52:10 +03002548 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2549 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002550 ath10k_err(ar, "failed to set dma mask to 32-bit: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002551 goto err_region;
2552 }
2553
2554 ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2555 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002556 ath10k_err(ar, "failed to set consistent dma mask to 32-bit: %d\n",
Michal Kazior2986e3e2014-08-07 11:03:30 +02002557 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002558 goto err_region;
2559 }
2560
Kalle Valo5e3dd152013-06-12 20:52:10 +03002561 pci_set_master(pdev);
2562
Michal Kazior2986e3e2014-08-07 11:03:30 +02002563 /* Workaround: Disable ASPM */
Kalle Valo5e3dd152013-06-12 20:52:10 +03002564 pci_read_config_dword(pdev, 0x80, &lcr_val);
2565 pci_write_config_dword(pdev, 0x80, (lcr_val & 0xffffff00));
2566
2567 /* Arrange for access to Target SoC registers. */
Michal Kazior2986e3e2014-08-07 11:03:30 +02002568 ar_pci->mem = pci_iomap(pdev, BAR_NUM, 0);
2569 if (!ar_pci->mem) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002570 ath10k_err(ar, "failed to iomap BAR%d\n", BAR_NUM);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002571 ret = -EIO;
2572 goto err_master;
2573 }
2574
Michal Kazior7aa7a722014-08-25 12:09:38 +02002575 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot pci_mem 0x%p\n", ar_pci->mem);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002576 return 0;
2577
2578err_master:
2579 pci_clear_master(pdev);
2580
2581err_region:
2582 pci_release_region(pdev, BAR_NUM);
2583
2584err_device:
2585 pci_disable_device(pdev);
2586
2587 return ret;
2588}
2589
2590static void ath10k_pci_release(struct ath10k *ar)
2591{
2592 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2593 struct pci_dev *pdev = ar_pci->pdev;
2594
2595 pci_iounmap(pdev, ar_pci->mem);
2596 pci_release_region(pdev, BAR_NUM);
2597 pci_clear_master(pdev);
2598 pci_disable_device(pdev);
2599}
2600
Michal Kazior7505f7c2014-12-02 10:55:54 +02002601static bool ath10k_pci_chip_is_supported(u32 dev_id, u32 chip_id)
2602{
2603 const struct ath10k_pci_supp_chip *supp_chip;
2604 int i;
2605 u32 rev_id = MS(chip_id, SOC_CHIP_ID_REV);
2606
2607 for (i = 0; i < ARRAY_SIZE(ath10k_pci_supp_chips); i++) {
2608 supp_chip = &ath10k_pci_supp_chips[i];
2609
2610 if (supp_chip->dev_id == dev_id &&
2611 supp_chip->rev_id == rev_id)
2612 return true;
2613 }
2614
2615 return false;
2616}
2617
Kalle Valo5e3dd152013-06-12 20:52:10 +03002618static int ath10k_pci_probe(struct pci_dev *pdev,
2619 const struct pci_device_id *pci_dev)
2620{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002621 int ret = 0;
2622 struct ath10k *ar;
2623 struct ath10k_pci *ar_pci;
Michal Kaziord63955b2015-01-24 12:14:49 +02002624 enum ath10k_hw_rev hw_rev;
Michal Kazior2986e3e2014-08-07 11:03:30 +02002625 u32 chip_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002626
Michal Kaziord63955b2015-01-24 12:14:49 +02002627 switch (pci_dev->device) {
2628 case QCA988X_2_0_DEVICE_ID:
2629 hw_rev = ATH10K_HW_QCA988X;
2630 break;
2631 case QCA6174_2_1_DEVICE_ID:
2632 hw_rev = ATH10K_HW_QCA6174;
2633 break;
2634 default:
2635 WARN_ON(1);
2636 return -ENOTSUPP;
2637 }
2638
2639 ar = ath10k_core_create(sizeof(*ar_pci), &pdev->dev, ATH10K_BUS_PCI,
2640 hw_rev, &ath10k_pci_hif_ops);
Michal Kaziore7b54192014-08-07 11:03:27 +02002641 if (!ar) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002642 dev_err(&pdev->dev, "failed to allocate core\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002643 return -ENOMEM;
Michal Kaziore7b54192014-08-07 11:03:27 +02002644 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002645
Michal Kazior7aa7a722014-08-25 12:09:38 +02002646 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci probe\n");
2647
Michal Kaziore7b54192014-08-07 11:03:27 +02002648 ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002649 ar_pci->pdev = pdev;
2650 ar_pci->dev = &pdev->dev;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002651 ar_pci->ar = ar;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002652
2653 spin_lock_init(&ar_pci->ce_lock);
Michal Kazior728f95e2014-08-22 14:33:14 +02002654 setup_timer(&ar_pci->rx_post_retry, ath10k_pci_rx_replenish_retry,
2655 (unsigned long)ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002656
Michal Kazior2986e3e2014-08-07 11:03:30 +02002657 ret = ath10k_pci_claim(ar);
Kalle Valoe01ae682013-09-01 11:22:14 +03002658 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002659 ath10k_err(ar, "failed to claim device: %d\n", ret);
Michal Kaziore7b54192014-08-07 11:03:27 +02002660 goto err_core_destroy;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002661 }
2662
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002663 ret = ath10k_pci_wake(ar);
Kalle Valoe01ae682013-09-01 11:22:14 +03002664 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002665 ath10k_err(ar, "failed to wake up: %d\n", ret);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002666 goto err_release;
Kalle Valoe01ae682013-09-01 11:22:14 +03002667 }
2668
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002669 ret = ath10k_pci_alloc_pipes(ar);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002670 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002671 ath10k_err(ar, "failed to allocate copy engine pipes: %d\n",
2672 ret);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002673 goto err_sleep;
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002674 }
2675
Michal Kazior403d6272014-08-22 14:23:31 +02002676 ath10k_pci_ce_deinit(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02002677 ath10k_pci_irq_disable(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02002678
Michal Kazior403d6272014-08-22 14:23:31 +02002679 ret = ath10k_pci_init_irq(ar);
2680 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002681 ath10k_err(ar, "failed to init irqs: %d\n", ret);
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002682 goto err_free_pipes;
Michal Kazior403d6272014-08-22 14:23:31 +02002683 }
2684
Michal Kazior7aa7a722014-08-25 12:09:38 +02002685 ath10k_info(ar, "pci irq %s interrupts %d irq_mode %d reset_mode %d\n",
Michal Kazior403d6272014-08-22 14:23:31 +02002686 ath10k_pci_get_irq_method(ar), ar_pci->num_msi_intrs,
2687 ath10k_pci_irq_mode, ath10k_pci_reset_mode);
2688
Michal Kazior5c771e72014-08-22 14:23:34 +02002689 ret = ath10k_pci_request_irq(ar);
Michal Kazior403d6272014-08-22 14:23:31 +02002690 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002691 ath10k_warn(ar, "failed to request irqs: %d\n", ret);
Michal Kazior403d6272014-08-22 14:23:31 +02002692 goto err_deinit_irq;
2693 }
2694
Michal Kazior1a7fecb2015-01-24 12:14:48 +02002695 ret = ath10k_pci_chip_reset(ar);
2696 if (ret) {
2697 ath10k_err(ar, "failed to reset chip: %d\n", ret);
2698 goto err_free_irq;
2699 }
2700
2701 chip_id = ath10k_pci_soc_read32(ar, SOC_CHIP_ID_ADDRESS);
2702 if (chip_id == 0xffffffff) {
2703 ath10k_err(ar, "failed to get chip id\n");
2704 goto err_free_irq;
2705 }
2706
2707 if (!ath10k_pci_chip_is_supported(pdev->device, chip_id)) {
2708 ath10k_err(ar, "device %04x with chip_id %08x isn't supported\n",
2709 pdev->device, chip_id);
2710 goto err_sleep;
2711 }
2712
Kalle Valoe01ae682013-09-01 11:22:14 +03002713 ret = ath10k_core_register(ar, chip_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002714 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002715 ath10k_err(ar, "failed to register driver core: %d\n", ret);
Michal Kazior5c771e72014-08-22 14:23:34 +02002716 goto err_free_irq;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002717 }
2718
2719 return 0;
2720
Michal Kazior5c771e72014-08-22 14:23:34 +02002721err_free_irq:
2722 ath10k_pci_free_irq(ar);
Michal Kazior21396272014-08-28 10:24:40 +02002723 ath10k_pci_kill_tasklet(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02002724
Michal Kazior403d6272014-08-22 14:23:31 +02002725err_deinit_irq:
2726 ath10k_pci_deinit_irq(ar);
2727
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002728err_free_pipes:
2729 ath10k_pci_free_pipes(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002730
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002731err_sleep:
2732 ath10k_pci_sleep(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002733
2734err_release:
2735 ath10k_pci_release(ar);
2736
Michal Kaziore7b54192014-08-07 11:03:27 +02002737err_core_destroy:
Kalle Valo5e3dd152013-06-12 20:52:10 +03002738 ath10k_core_destroy(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002739
2740 return ret;
2741}
2742
2743static void ath10k_pci_remove(struct pci_dev *pdev)
2744{
2745 struct ath10k *ar = pci_get_drvdata(pdev);
2746 struct ath10k_pci *ar_pci;
2747
Michal Kazior7aa7a722014-08-25 12:09:38 +02002748 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci remove\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002749
2750 if (!ar)
2751 return;
2752
2753 ar_pci = ath10k_pci_priv(ar);
2754
2755 if (!ar_pci)
2756 return;
2757
Kalle Valo5e3dd152013-06-12 20:52:10 +03002758 ath10k_core_unregister(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02002759 ath10k_pci_free_irq(ar);
Michal Kazior21396272014-08-28 10:24:40 +02002760 ath10k_pci_kill_tasklet(ar);
Michal Kazior403d6272014-08-22 14:23:31 +02002761 ath10k_pci_deinit_irq(ar);
2762 ath10k_pci_ce_deinit(ar);
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002763 ath10k_pci_free_pipes(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002764 ath10k_pci_release(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002765 ath10k_core_destroy(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002766}
2767
Kalle Valo5e3dd152013-06-12 20:52:10 +03002768MODULE_DEVICE_TABLE(pci, ath10k_pci_id_table);
2769
2770static struct pci_driver ath10k_pci_driver = {
2771 .name = "ath10k_pci",
2772 .id_table = ath10k_pci_id_table,
2773 .probe = ath10k_pci_probe,
2774 .remove = ath10k_pci_remove,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002775};
2776
2777static int __init ath10k_pci_init(void)
2778{
2779 int ret;
2780
2781 ret = pci_register_driver(&ath10k_pci_driver);
2782 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002783 printk(KERN_ERR "failed to register ath10k pci driver: %d\n",
2784 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002785
2786 return ret;
2787}
2788module_init(ath10k_pci_init);
2789
2790static void __exit ath10k_pci_exit(void)
2791{
2792 pci_unregister_driver(&ath10k_pci_driver);
2793}
2794
2795module_exit(ath10k_pci_exit);
2796
2797MODULE_AUTHOR("Qualcomm Atheros");
2798MODULE_DESCRIPTION("Driver support for Atheros QCA988X PCIe devices");
2799MODULE_LICENSE("Dual BSD/GPL");
Bartosz Markowski5c427f52015-02-18 13:16:37 +01002800
2801/* QCA988x 2.0 firmware files */
Bartosz Markowski8026cae2014-10-06 14:16:41 +02002802MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_FW_FILE);
2803MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API2_FILE);
2804MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API3_FILE);
Bartosz Markowski5c427f52015-02-18 13:16:37 +01002805MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API4_FILE);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002806MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_BOARD_DATA_FILE);
Bartosz Markowski5c427f52015-02-18 13:16:37 +01002807
2808/* QCA6174 2.1 firmware files */
2809MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" ATH10K_FW_API4_FILE);
2810MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" QCA6174_HW_2_1_BOARD_DATA_FILE);
2811
2812/* QCA6174 3.1 firmware files */
2813MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" ATH10K_FW_API4_FILE);
2814MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" QCA6174_HW_3_0_BOARD_DATA_FILE);