blob: 631b026a487b2fc17de97c969d08971dab8a565b [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 Kaziorc0c378f2014-08-07 11:03:28 +0200822static int ath10k_pci_wake(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300823{
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200824 ath10k_pci_reg_write32(ar, PCIE_SOC_WAKE_ADDRESS,
825 PCIE_SOC_WAKE_V_MASK);
826 return ath10k_pci_wake_wait(ar);
827}
Kalle Valo5e3dd152013-06-12 20:52:10 +0300828
Michal Kaziorc0c378f2014-08-07 11:03:28 +0200829static void ath10k_pci_sleep(struct ath10k *ar)
830{
831 ath10k_pci_reg_write32(ar, PCIE_SOC_WAKE_ADDRESS,
832 PCIE_SOC_WAKE_RESET);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300833}
834
Kalle Valo5e3dd152013-06-12 20:52:10 +0300835/* Called by lower (CE) layer when a send to Target completes. */
Michal Kazior5440ce22013-09-03 15:09:58 +0200836static void ath10k_pci_ce_send_done(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300837{
838 struct ath10k *ar = ce_state->ar;
839 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2f5280d2014-02-27 18:50:05 +0200840 struct ath10k_hif_cb *cb = &ar_pci->msg_callbacks_current;
Michal Kazior1cb86d42014-11-27 11:09:38 +0100841 struct sk_buff_head list;
842 struct sk_buff *skb;
Michal Kazior5440ce22013-09-03 15:09:58 +0200843 u32 ce_data;
844 unsigned int nbytes;
845 unsigned int transfer_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300846
Michal Kazior1cb86d42014-11-27 11:09:38 +0100847 __skb_queue_head_init(&list);
848 while (ath10k_ce_completed_send_next(ce_state, (void **)&skb, &ce_data,
849 &nbytes, &transfer_id) == 0) {
Michal Kaziora16942e2014-02-27 18:50:04 +0200850 /* no need to call tx completion for NULL pointers */
Michal Kazior1cb86d42014-11-27 11:09:38 +0100851 if (skb == NULL)
Michal Kazior726346f2014-02-27 18:50:04 +0200852 continue;
853
Michal Kazior1cb86d42014-11-27 11:09:38 +0100854 __skb_queue_tail(&list, skb);
Michal Kazior5440ce22013-09-03 15:09:58 +0200855 }
Michal Kazior1cb86d42014-11-27 11:09:38 +0100856
857 while ((skb = __skb_dequeue(&list)))
858 cb->tx_completion(ar, skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300859}
860
861/* Called by lower (CE) layer when data is received from the Target. */
Michal Kazior5440ce22013-09-03 15:09:58 +0200862static void ath10k_pci_ce_recv_data(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300863{
864 struct ath10k *ar = ce_state->ar;
865 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior87263e52013-08-27 13:08:01 +0200866 struct ath10k_pci_pipe *pipe_info = &ar_pci->pipe_info[ce_state->id];
Michal Kazior2f5280d2014-02-27 18:50:05 +0200867 struct ath10k_hif_cb *cb = &ar_pci->msg_callbacks_current;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300868 struct sk_buff *skb;
Michal Kazior1cb86d42014-11-27 11:09:38 +0100869 struct sk_buff_head list;
Michal Kazior5440ce22013-09-03 15:09:58 +0200870 void *transfer_context;
871 u32 ce_data;
Michal Kazior2f5280d2014-02-27 18:50:05 +0200872 unsigned int nbytes, max_nbytes;
Michal Kazior5440ce22013-09-03 15:09:58 +0200873 unsigned int transfer_id;
874 unsigned int flags;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300875
Michal Kazior1cb86d42014-11-27 11:09:38 +0100876 __skb_queue_head_init(&list);
Michal Kazior5440ce22013-09-03 15:09:58 +0200877 while (ath10k_ce_completed_recv_next(ce_state, &transfer_context,
878 &ce_data, &nbytes, &transfer_id,
879 &flags) == 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300880 skb = transfer_context;
Michal Kazior2f5280d2014-02-27 18:50:05 +0200881 max_nbytes = skb->len + skb_tailroom(skb);
Michal Kazior8582bf32015-01-24 12:14:47 +0200882 dma_unmap_single(ar->dev, ATH10K_SKB_RXCB(skb)->paddr,
Michal Kazior2f5280d2014-02-27 18:50:05 +0200883 max_nbytes, DMA_FROM_DEVICE);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300884
Michal Kazior2f5280d2014-02-27 18:50:05 +0200885 if (unlikely(max_nbytes < nbytes)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200886 ath10k_warn(ar, "rxed more than expected (nbytes %d, max %d)",
Michal Kazior2f5280d2014-02-27 18:50:05 +0200887 nbytes, max_nbytes);
888 dev_kfree_skb_any(skb);
889 continue;
890 }
891
892 skb_put(skb, nbytes);
Michal Kazior1cb86d42014-11-27 11:09:38 +0100893 __skb_queue_tail(&list, skb);
894 }
Michal Kaziora360e542014-09-23 10:22:54 +0200895
Michal Kazior1cb86d42014-11-27 11:09:38 +0100896 while ((skb = __skb_dequeue(&list))) {
Michal Kaziora360e542014-09-23 10:22:54 +0200897 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci rx ce pipe %d len %d\n",
898 ce_state->id, skb->len);
899 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci rx: ",
900 skb->data, skb->len);
901
Michal Kazior5f07ea42014-11-27 11:09:36 +0100902 cb->rx_completion(ar, skb);
Michal Kazior2f5280d2014-02-27 18:50:05 +0200903 }
Michal Kaziorc29a3802014-07-21 21:03:10 +0300904
Michal Kazior728f95e2014-08-22 14:33:14 +0200905 ath10k_pci_rx_post_pipe(pipe_info);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300906}
907
Michal Kazior726346f2014-02-27 18:50:04 +0200908static int ath10k_pci_hif_tx_sg(struct ath10k *ar, u8 pipe_id,
909 struct ath10k_hif_sg_item *items, int n_items)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300910{
Kalle Valo5e3dd152013-06-12 20:52:10 +0300911 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior726346f2014-02-27 18:50:04 +0200912 struct ath10k_pci_pipe *pci_pipe = &ar_pci->pipe_info[pipe_id];
913 struct ath10k_ce_pipe *ce_pipe = pci_pipe->ce_hdl;
914 struct ath10k_ce_ring *src_ring = ce_pipe->src_ring;
Michal Kazior7147a132014-05-26 12:02:58 +0200915 unsigned int nentries_mask;
916 unsigned int sw_index;
917 unsigned int write_index;
Michal Kazior08b8aa02014-05-26 12:02:59 +0200918 int err, i = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300919
Michal Kazior726346f2014-02-27 18:50:04 +0200920 spin_lock_bh(&ar_pci->ce_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300921
Michal Kazior7147a132014-05-26 12:02:58 +0200922 nentries_mask = src_ring->nentries_mask;
923 sw_index = src_ring->sw_index;
924 write_index = src_ring->write_index;
925
Michal Kazior726346f2014-02-27 18:50:04 +0200926 if (unlikely(CE_RING_DELTA(nentries_mask,
927 write_index, sw_index - 1) < n_items)) {
928 err = -ENOBUFS;
Michal Kazior08b8aa02014-05-26 12:02:59 +0200929 goto err;
Michal Kazior726346f2014-02-27 18:50:04 +0200930 }
931
932 for (i = 0; i < n_items - 1; i++) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200933 ath10k_dbg(ar, ATH10K_DBG_PCI,
Michal Kazior726346f2014-02-27 18:50:04 +0200934 "pci tx item %d paddr 0x%08x len %d n_items %d\n",
935 i, items[i].paddr, items[i].len, n_items);
Michal Kazior7aa7a722014-08-25 12:09:38 +0200936 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci tx data: ",
Michal Kazior726346f2014-02-27 18:50:04 +0200937 items[i].vaddr, items[i].len);
938
939 err = ath10k_ce_send_nolock(ce_pipe,
940 items[i].transfer_context,
941 items[i].paddr,
942 items[i].len,
943 items[i].transfer_id,
944 CE_SEND_FLAG_GATHER);
945 if (err)
Michal Kazior08b8aa02014-05-26 12:02:59 +0200946 goto err;
Michal Kazior726346f2014-02-27 18:50:04 +0200947 }
948
949 /* `i` is equal to `n_items -1` after for() */
Kalle Valo5e3dd152013-06-12 20:52:10 +0300950
Michal Kazior7aa7a722014-08-25 12:09:38 +0200951 ath10k_dbg(ar, ATH10K_DBG_PCI,
Michal Kazior726346f2014-02-27 18:50:04 +0200952 "pci tx item %d paddr 0x%08x len %d n_items %d\n",
953 i, items[i].paddr, items[i].len, n_items);
Michal Kazior7aa7a722014-08-25 12:09:38 +0200954 ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci tx data: ",
Michal Kazior726346f2014-02-27 18:50:04 +0200955 items[i].vaddr, items[i].len);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300956
Michal Kazior726346f2014-02-27 18:50:04 +0200957 err = ath10k_ce_send_nolock(ce_pipe,
958 items[i].transfer_context,
959 items[i].paddr,
960 items[i].len,
961 items[i].transfer_id,
962 0);
963 if (err)
Michal Kazior08b8aa02014-05-26 12:02:59 +0200964 goto err;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300965
Michal Kazior08b8aa02014-05-26 12:02:59 +0200966 spin_unlock_bh(&ar_pci->ce_lock);
967 return 0;
968
969err:
970 for (; i > 0; i--)
971 __ath10k_ce_send_revert(ce_pipe);
972
Michal Kazior726346f2014-02-27 18:50:04 +0200973 spin_unlock_bh(&ar_pci->ce_lock);
974 return err;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300975}
976
Kalle Valoeef25402014-09-24 14:16:52 +0300977static int ath10k_pci_hif_diag_read(struct ath10k *ar, u32 address, void *buf,
978 size_t buf_len)
979{
980 return ath10k_pci_diag_read_mem(ar, address, buf, buf_len);
981}
982
Kalle Valo5e3dd152013-06-12 20:52:10 +0300983static u16 ath10k_pci_hif_get_free_queue_number(struct ath10k *ar, u8 pipe)
984{
985 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo50f87a62014-03-28 09:32:52 +0200986
Michal Kazior7aa7a722014-08-25 12:09:38 +0200987 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif get free queue number\n");
Kalle Valo50f87a62014-03-28 09:32:52 +0200988
Michal Kazior3efcb3b2013-10-02 11:03:41 +0200989 return ath10k_ce_num_free_src_entries(ar_pci->pipe_info[pipe].ce_hdl);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300990}
991
Ben Greear384914b2014-08-25 08:37:32 +0300992static void ath10k_pci_dump_registers(struct ath10k *ar,
993 struct ath10k_fw_crash_data *crash_data)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300994{
Michal Kazior0fdc14e42014-08-26 19:14:03 +0300995 __le32 reg_dump_values[REG_DUMP_COUNT_QCA988X] = {};
996 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300997
Ben Greear384914b2014-08-25 08:37:32 +0300998 lockdep_assert_held(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300999
Kalle Valo3d29a3e2014-08-25 08:37:26 +03001000 ret = ath10k_pci_diag_read_hi(ar, &reg_dump_values[0],
1001 hi_failure_state,
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001002 REG_DUMP_COUNT_QCA988X * sizeof(__le32));
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001003 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001004 ath10k_err(ar, "failed to read firmware dump area: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001005 return;
1006 }
1007
1008 BUILD_BUG_ON(REG_DUMP_COUNT_QCA988X % 4);
1009
Michal Kazior7aa7a722014-08-25 12:09:38 +02001010 ath10k_err(ar, "firmware register dump:\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001011 for (i = 0; i < REG_DUMP_COUNT_QCA988X; i += 4)
Michal Kazior7aa7a722014-08-25 12:09:38 +02001012 ath10k_err(ar, "[%02d]: 0x%08X 0x%08X 0x%08X 0x%08X\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001013 i,
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001014 __le32_to_cpu(reg_dump_values[i]),
1015 __le32_to_cpu(reg_dump_values[i + 1]),
1016 __le32_to_cpu(reg_dump_values[i + 2]),
1017 __le32_to_cpu(reg_dump_values[i + 3]));
Michal Kazioraffd3212013-07-16 09:54:35 +02001018
Michal Kazior1bbb1192014-08-25 12:13:14 +02001019 if (!crash_data)
1020 return;
1021
Ben Greear384914b2014-08-25 08:37:32 +03001022 for (i = 0; i < REG_DUMP_COUNT_QCA988X; i++)
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001023 crash_data->registers[i] = reg_dump_values[i];
Ben Greear384914b2014-08-25 08:37:32 +03001024}
1025
Kalle Valo0e9848c2014-08-25 08:37:37 +03001026static void ath10k_pci_fw_crashed_dump(struct ath10k *ar)
Ben Greear384914b2014-08-25 08:37:32 +03001027{
1028 struct ath10k_fw_crash_data *crash_data;
1029 char uuid[50];
1030
1031 spin_lock_bh(&ar->data_lock);
1032
Ben Greearf51dbe72014-09-29 14:41:46 +03001033 ar->stats.fw_crash_counter++;
1034
Ben Greear384914b2014-08-25 08:37:32 +03001035 crash_data = ath10k_debug_get_new_fw_crash_data(ar);
1036
1037 if (crash_data)
1038 scnprintf(uuid, sizeof(uuid), "%pUl", &crash_data->uuid);
1039 else
1040 scnprintf(uuid, sizeof(uuid), "n/a");
1041
Michal Kazior7aa7a722014-08-25 12:09:38 +02001042 ath10k_err(ar, "firmware crashed! (uuid %s)\n", uuid);
Kalle Valo8a0c7972014-08-25 08:37:45 +03001043 ath10k_print_driver_info(ar);
Ben Greear384914b2014-08-25 08:37:32 +03001044 ath10k_pci_dump_registers(ar, crash_data);
1045
Ben Greear384914b2014-08-25 08:37:32 +03001046 spin_unlock_bh(&ar->data_lock);
Michal Kazioraffd3212013-07-16 09:54:35 +02001047
Michal Kazior5e90de82013-10-16 16:46:05 +03001048 queue_work(ar->workqueue, &ar->restart_work);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001049}
1050
1051static void ath10k_pci_hif_send_complete_check(struct ath10k *ar, u8 pipe,
1052 int force)
1053{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001054 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif send complete check\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001055
Kalle Valo5e3dd152013-06-12 20:52:10 +03001056 if (!force) {
1057 int resources;
1058 /*
1059 * Decide whether to actually poll for completions, or just
1060 * wait for a later chance.
1061 * If there seem to be plenty of resources left, then just wait
1062 * since checking involves reading a CE register, which is a
1063 * relatively expensive operation.
1064 */
1065 resources = ath10k_pci_hif_get_free_queue_number(ar, pipe);
1066
1067 /*
1068 * If at least 50% of the total resources are still available,
1069 * don't bother checking again yet.
1070 */
1071 if (resources > (host_ce_config_wlan[pipe].src_nentries >> 1))
1072 return;
1073 }
1074 ath10k_ce_per_engine_service(ar, pipe);
1075}
1076
Michal Kaziore799bbf2013-07-05 16:15:12 +03001077static void ath10k_pci_hif_set_callbacks(struct ath10k *ar,
1078 struct ath10k_hif_cb *callbacks)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001079{
1080 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1081
Michal Kazior7aa7a722014-08-25 12:09:38 +02001082 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif set callbacks\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001083
1084 memcpy(&ar_pci->msg_callbacks_current, callbacks,
1085 sizeof(ar_pci->msg_callbacks_current));
1086}
1087
Michal Kazior96a9d0d2013-11-08 08:01:25 +01001088static void ath10k_pci_kill_tasklet(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001089{
1090 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001091 int i;
1092
Kalle Valo5e3dd152013-06-12 20:52:10 +03001093 tasklet_kill(&ar_pci->intr_tq);
Michal Kazior103d4f52013-11-08 08:01:24 +01001094 tasklet_kill(&ar_pci->msi_fw_err);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001095
1096 for (i = 0; i < CE_COUNT; i++)
1097 tasklet_kill(&ar_pci->pipe_info[i].intr);
Michal Kazior728f95e2014-08-22 14:33:14 +02001098
1099 del_timer_sync(&ar_pci->rx_post_retry);
Michal Kazior96a9d0d2013-11-08 08:01:25 +01001100}
1101
Kalle Valo5e3dd152013-06-12 20:52:10 +03001102static int ath10k_pci_hif_map_service_to_pipe(struct ath10k *ar,
1103 u16 service_id, u8 *ul_pipe,
1104 u8 *dl_pipe, int *ul_is_polled,
1105 int *dl_is_polled)
1106{
Michal Kazior7c6aa252014-08-26 19:14:03 +03001107 const struct service_to_pipe *entry;
1108 bool ul_set = false, dl_set = false;
1109 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001110
Michal Kazior7aa7a722014-08-25 12:09:38 +02001111 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif map service\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001112
Kalle Valo5e3dd152013-06-12 20:52:10 +03001113 /* polling for received messages not supported */
1114 *dl_is_polled = 0;
1115
Michal Kazior7c6aa252014-08-26 19:14:03 +03001116 for (i = 0; i < ARRAY_SIZE(target_service_to_ce_map_wlan); i++) {
1117 entry = &target_service_to_ce_map_wlan[i];
Kalle Valo5e3dd152013-06-12 20:52:10 +03001118
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001119 if (__le32_to_cpu(entry->service_id) != service_id)
Michal Kazior7c6aa252014-08-26 19:14:03 +03001120 continue;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001121
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001122 switch (__le32_to_cpu(entry->pipedir)) {
Michal Kazior7c6aa252014-08-26 19:14:03 +03001123 case PIPEDIR_NONE:
1124 break;
1125 case PIPEDIR_IN:
1126 WARN_ON(dl_set);
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001127 *dl_pipe = __le32_to_cpu(entry->pipenum);
Michal Kazior7c6aa252014-08-26 19:14:03 +03001128 dl_set = true;
1129 break;
1130 case PIPEDIR_OUT:
1131 WARN_ON(ul_set);
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001132 *ul_pipe = __le32_to_cpu(entry->pipenum);
Michal Kazior7c6aa252014-08-26 19:14:03 +03001133 ul_set = true;
1134 break;
1135 case PIPEDIR_INOUT:
1136 WARN_ON(dl_set);
1137 WARN_ON(ul_set);
Michal Kazior0fdc14e42014-08-26 19:14:03 +03001138 *dl_pipe = __le32_to_cpu(entry->pipenum);
1139 *ul_pipe = __le32_to_cpu(entry->pipenum);
Michal Kazior7c6aa252014-08-26 19:14:03 +03001140 dl_set = true;
1141 ul_set = true;
1142 break;
1143 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001144 }
Michal Kazior7c6aa252014-08-26 19:14:03 +03001145
1146 if (WARN_ON(!ul_set || !dl_set))
1147 return -ENOENT;
1148
Kalle Valo5e3dd152013-06-12 20:52:10 +03001149 *ul_is_polled =
1150 (host_ce_config_wlan[*ul_pipe].flags & CE_ATTR_DIS_INTR) != 0;
1151
Michal Kazior7c6aa252014-08-26 19:14:03 +03001152 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001153}
1154
1155static void ath10k_pci_hif_get_default_pipe(struct ath10k *ar,
Kalle Valo5b07e072014-09-14 12:50:06 +03001156 u8 *ul_pipe, u8 *dl_pipe)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001157{
1158 int ul_is_polled, dl_is_polled;
1159
Michal Kazior7aa7a722014-08-25 12:09:38 +02001160 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif get default pipe\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02001161
Kalle Valo5e3dd152013-06-12 20:52:10 +03001162 (void)ath10k_pci_hif_map_service_to_pipe(ar,
1163 ATH10K_HTC_SVC_ID_RSVD_CTRL,
1164 ul_pipe,
1165 dl_pipe,
1166 &ul_is_polled,
1167 &dl_is_polled);
1168}
1169
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001170static void ath10k_pci_irq_msi_fw_mask(struct ath10k *ar)
1171{
1172 u32 val;
1173
1174 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS + CORE_CTRL_ADDRESS);
1175 val &= ~CORE_CTRL_PCIE_REG_31_MASK;
1176
1177 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + CORE_CTRL_ADDRESS, val);
1178}
1179
1180static void ath10k_pci_irq_msi_fw_unmask(struct ath10k *ar)
1181{
1182 u32 val;
1183
1184 val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS + CORE_CTRL_ADDRESS);
1185 val |= CORE_CTRL_PCIE_REG_31_MASK;
1186
1187 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + CORE_CTRL_ADDRESS, val);
1188}
1189
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001190static void ath10k_pci_irq_disable(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001191{
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001192 ath10k_ce_disable_interrupts(ar);
1193 ath10k_pci_disable_and_clear_legacy_irq(ar);
1194 ath10k_pci_irq_msi_fw_mask(ar);
1195}
1196
1197static void ath10k_pci_irq_sync(struct ath10k *ar)
1198{
Kalle Valo5e3dd152013-06-12 20:52:10 +03001199 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001200 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001201
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001202 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
1203 synchronize_irq(ar_pci->pdev->irq + i);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001204}
1205
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001206static void ath10k_pci_irq_enable(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001207{
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001208 ath10k_ce_enable_interrupts(ar);
Michal Kaziore75db4e2014-08-28 22:14:16 +03001209 ath10k_pci_enable_legacy_irq(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001210 ath10k_pci_irq_msi_fw_unmask(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001211}
1212
1213static int ath10k_pci_hif_start(struct ath10k *ar)
1214{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001215 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif start\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001216
Michal Kaziorec5ba4d2014-08-22 14:23:33 +02001217 ath10k_pci_irq_enable(ar);
Michal Kazior728f95e2014-08-22 14:33:14 +02001218 ath10k_pci_rx_post(ar);
Kalle Valo50f87a62014-03-28 09:32:52 +02001219
Kalle Valo5e3dd152013-06-12 20:52:10 +03001220 return 0;
1221}
1222
Michal Kazior099ac7c2014-10-28 10:32:05 +01001223static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe *pci_pipe)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001224{
1225 struct ath10k *ar;
Michal Kazior099ac7c2014-10-28 10:32:05 +01001226 struct ath10k_ce_pipe *ce_pipe;
1227 struct ath10k_ce_ring *ce_ring;
1228 struct sk_buff *skb;
1229 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001230
Michal Kazior099ac7c2014-10-28 10:32:05 +01001231 ar = pci_pipe->hif_ce_state;
1232 ce_pipe = pci_pipe->ce_hdl;
1233 ce_ring = ce_pipe->dest_ring;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001234
Michal Kazior099ac7c2014-10-28 10:32:05 +01001235 if (!ce_ring)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001236 return;
1237
Michal Kazior099ac7c2014-10-28 10:32:05 +01001238 if (!pci_pipe->buf_sz)
1239 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001240
Michal Kazior099ac7c2014-10-28 10:32:05 +01001241 for (i = 0; i < ce_ring->nentries; i++) {
1242 skb = ce_ring->per_transfer_context[i];
1243 if (!skb)
1244 continue;
1245
1246 ce_ring->per_transfer_context[i] = NULL;
1247
Michal Kazior8582bf32015-01-24 12:14:47 +02001248 dma_unmap_single(ar->dev, ATH10K_SKB_RXCB(skb)->paddr,
Michal Kazior099ac7c2014-10-28 10:32:05 +01001249 skb->len + skb_tailroom(skb),
Kalle Valo5e3dd152013-06-12 20:52:10 +03001250 DMA_FROM_DEVICE);
Michal Kazior099ac7c2014-10-28 10:32:05 +01001251 dev_kfree_skb_any(skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001252 }
1253}
1254
Michal Kazior099ac7c2014-10-28 10:32:05 +01001255static void ath10k_pci_tx_pipe_cleanup(struct ath10k_pci_pipe *pci_pipe)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001256{
1257 struct ath10k *ar;
1258 struct ath10k_pci *ar_pci;
Michal Kazior099ac7c2014-10-28 10:32:05 +01001259 struct ath10k_ce_pipe *ce_pipe;
1260 struct ath10k_ce_ring *ce_ring;
1261 struct ce_desc *ce_desc;
1262 struct sk_buff *skb;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001263 unsigned int id;
Michal Kazior099ac7c2014-10-28 10:32:05 +01001264 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001265
Michal Kazior099ac7c2014-10-28 10:32:05 +01001266 ar = pci_pipe->hif_ce_state;
1267 ar_pci = ath10k_pci_priv(ar);
1268 ce_pipe = pci_pipe->ce_hdl;
1269 ce_ring = ce_pipe->src_ring;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001270
Michal Kazior099ac7c2014-10-28 10:32:05 +01001271 if (!ce_ring)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001272 return;
1273
Michal Kazior099ac7c2014-10-28 10:32:05 +01001274 if (!pci_pipe->buf_sz)
1275 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001276
Michal Kazior099ac7c2014-10-28 10:32:05 +01001277 ce_desc = ce_ring->shadow_base;
1278 if (WARN_ON(!ce_desc))
1279 return;
1280
1281 for (i = 0; i < ce_ring->nentries; i++) {
1282 skb = ce_ring->per_transfer_context[i];
1283 if (!skb)
Michal Kazior2415fc12013-11-08 08:01:32 +01001284 continue;
Michal Kazior2415fc12013-11-08 08:01:32 +01001285
Michal Kazior099ac7c2014-10-28 10:32:05 +01001286 ce_ring->per_transfer_context[i] = NULL;
1287 id = MS(__le16_to_cpu(ce_desc[i].flags),
1288 CE_DESC_FLAGS_META_DATA);
1289
Michal Kaziord84a5122014-11-27 11:09:37 +01001290 ar_pci->msg_callbacks_current.tx_completion(ar, skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001291 }
1292}
1293
1294/*
1295 * Cleanup residual buffers for device shutdown:
1296 * buffers that were enqueued for receive
1297 * buffers that were to be sent
1298 * Note: Buffers that had completed but which were
1299 * not yet processed are on a completion queue. They
1300 * are handled when the completion thread shuts down.
1301 */
1302static void ath10k_pci_buffer_cleanup(struct ath10k *ar)
1303{
1304 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1305 int pipe_num;
1306
Michal Kaziorfad6ed72013-11-08 08:01:23 +01001307 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
Michal Kazior87263e52013-08-27 13:08:01 +02001308 struct ath10k_pci_pipe *pipe_info;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001309
1310 pipe_info = &ar_pci->pipe_info[pipe_num];
1311 ath10k_pci_rx_pipe_cleanup(pipe_info);
1312 ath10k_pci_tx_pipe_cleanup(pipe_info);
1313 }
1314}
1315
1316static void ath10k_pci_ce_deinit(struct ath10k *ar)
1317{
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001318 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001319
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001320 for (i = 0; i < CE_COUNT; i++)
1321 ath10k_ce_deinit_pipe(ar, i);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001322}
1323
Michal Kazior728f95e2014-08-22 14:33:14 +02001324static void ath10k_pci_flush(struct ath10k *ar)
1325{
1326 ath10k_pci_kill_tasklet(ar);
1327 ath10k_pci_buffer_cleanup(ar);
1328}
1329
Kalle Valo5e3dd152013-06-12 20:52:10 +03001330static void ath10k_pci_hif_stop(struct ath10k *ar)
1331{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001332 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif stop\n");
Michal Kazior32270b62013-08-02 09:15:47 +02001333
Michal Kazior10d23db2014-08-22 14:33:15 +02001334 /* Most likely the device has HTT Rx ring configured. The only way to
1335 * prevent the device from accessing (and possible corrupting) host
1336 * memory is to reset the chip now.
Michal Kaziore75db4e2014-08-28 22:14:16 +03001337 *
1338 * There's also no known way of masking MSI interrupts on the device.
1339 * For ranged MSI the CE-related interrupts can be masked. However
1340 * regardless how many MSI interrupts are assigned the first one
1341 * is always used for firmware indications (crashes) and cannot be
1342 * masked. To prevent the device from asserting the interrupt reset it
1343 * before proceeding with cleanup.
Michal Kazior10d23db2014-08-22 14:33:15 +02001344 */
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001345 ath10k_pci_warm_reset(ar);
Michal Kaziore75db4e2014-08-28 22:14:16 +03001346
1347 ath10k_pci_irq_disable(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02001348 ath10k_pci_irq_sync(ar);
Michal Kaziore75db4e2014-08-28 22:14:16 +03001349 ath10k_pci_flush(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001350}
1351
1352static int ath10k_pci_hif_exchange_bmi_msg(struct ath10k *ar,
1353 void *req, u32 req_len,
1354 void *resp, u32 *resp_len)
1355{
1356 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kazior2aa39112013-08-27 13:08:02 +02001357 struct ath10k_pci_pipe *pci_tx = &ar_pci->pipe_info[BMI_CE_NUM_TO_TARG];
1358 struct ath10k_pci_pipe *pci_rx = &ar_pci->pipe_info[BMI_CE_NUM_TO_HOST];
1359 struct ath10k_ce_pipe *ce_tx = pci_tx->ce_hdl;
1360 struct ath10k_ce_pipe *ce_rx = pci_rx->ce_hdl;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001361 dma_addr_t req_paddr = 0;
1362 dma_addr_t resp_paddr = 0;
1363 struct bmi_xfer xfer = {};
1364 void *treq, *tresp = NULL;
1365 int ret = 0;
1366
Michal Kazior85622cd2013-11-25 14:06:22 +01001367 might_sleep();
1368
Kalle Valo5e3dd152013-06-12 20:52:10 +03001369 if (resp && !resp_len)
1370 return -EINVAL;
1371
1372 if (resp && resp_len && *resp_len == 0)
1373 return -EINVAL;
1374
1375 treq = kmemdup(req, req_len, GFP_KERNEL);
1376 if (!treq)
1377 return -ENOMEM;
1378
1379 req_paddr = dma_map_single(ar->dev, treq, req_len, DMA_TO_DEVICE);
1380 ret = dma_mapping_error(ar->dev, req_paddr);
1381 if (ret)
1382 goto err_dma;
1383
1384 if (resp && resp_len) {
1385 tresp = kzalloc(*resp_len, GFP_KERNEL);
1386 if (!tresp) {
1387 ret = -ENOMEM;
1388 goto err_req;
1389 }
1390
1391 resp_paddr = dma_map_single(ar->dev, tresp, *resp_len,
1392 DMA_FROM_DEVICE);
1393 ret = dma_mapping_error(ar->dev, resp_paddr);
1394 if (ret)
1395 goto err_req;
1396
1397 xfer.wait_for_resp = true;
1398 xfer.resp_len = 0;
1399
Michal Kazior728f95e2014-08-22 14:33:14 +02001400 ath10k_ce_rx_post_buf(ce_rx, &xfer, resp_paddr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001401 }
1402
Kalle Valo5e3dd152013-06-12 20:52:10 +03001403 ret = ath10k_ce_send(ce_tx, &xfer, req_paddr, req_len, -1, 0);
1404 if (ret)
1405 goto err_resp;
1406
Michal Kazior85622cd2013-11-25 14:06:22 +01001407 ret = ath10k_pci_bmi_wait(ce_tx, ce_rx, &xfer);
1408 if (ret) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001409 u32 unused_buffer;
1410 unsigned int unused_nbytes;
1411 unsigned int unused_id;
1412
Kalle Valo5e3dd152013-06-12 20:52:10 +03001413 ath10k_ce_cancel_send_next(ce_tx, NULL, &unused_buffer,
1414 &unused_nbytes, &unused_id);
1415 } else {
1416 /* non-zero means we did not time out */
1417 ret = 0;
1418 }
1419
1420err_resp:
1421 if (resp) {
1422 u32 unused_buffer;
1423
1424 ath10k_ce_revoke_recv_next(ce_rx, NULL, &unused_buffer);
1425 dma_unmap_single(ar->dev, resp_paddr,
1426 *resp_len, DMA_FROM_DEVICE);
1427 }
1428err_req:
1429 dma_unmap_single(ar->dev, req_paddr, req_len, DMA_TO_DEVICE);
1430
1431 if (ret == 0 && resp_len) {
1432 *resp_len = min(*resp_len, xfer.resp_len);
1433 memcpy(resp, tresp, xfer.resp_len);
1434 }
1435err_dma:
1436 kfree(treq);
1437 kfree(tresp);
1438
1439 return ret;
1440}
1441
Michal Kazior5440ce22013-09-03 15:09:58 +02001442static void ath10k_pci_bmi_send_done(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001443{
Michal Kazior5440ce22013-09-03 15:09:58 +02001444 struct bmi_xfer *xfer;
1445 u32 ce_data;
1446 unsigned int nbytes;
1447 unsigned int transfer_id;
1448
1449 if (ath10k_ce_completed_send_next(ce_state, (void **)&xfer, &ce_data,
1450 &nbytes, &transfer_id))
1451 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001452
Michal Kazior2374b182014-07-14 16:25:25 +03001453 xfer->tx_done = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001454}
1455
Michal Kazior5440ce22013-09-03 15:09:58 +02001456static void ath10k_pci_bmi_recv_data(struct ath10k_ce_pipe *ce_state)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001457{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001458 struct ath10k *ar = ce_state->ar;
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 unsigned int flags;
1464
1465 if (ath10k_ce_completed_recv_next(ce_state, (void **)&xfer, &ce_data,
1466 &nbytes, &transfer_id, &flags))
1467 return;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001468
Michal Kazior04ed9df2014-10-28 10:34:36 +01001469 if (WARN_ON_ONCE(!xfer))
1470 return;
1471
Kalle Valo5e3dd152013-06-12 20:52:10 +03001472 if (!xfer->wait_for_resp) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001473 ath10k_warn(ar, "unexpected: BMI data received; ignoring\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001474 return;
1475 }
1476
1477 xfer->resp_len = nbytes;
Michal Kazior2374b182014-07-14 16:25:25 +03001478 xfer->rx_done = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001479}
1480
Michal Kazior85622cd2013-11-25 14:06:22 +01001481static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
1482 struct ath10k_ce_pipe *rx_pipe,
1483 struct bmi_xfer *xfer)
1484{
1485 unsigned long timeout = jiffies + BMI_COMMUNICATION_TIMEOUT_HZ;
1486
1487 while (time_before_eq(jiffies, timeout)) {
1488 ath10k_pci_bmi_send_done(tx_pipe);
1489 ath10k_pci_bmi_recv_data(rx_pipe);
1490
Michal Kazior2374b182014-07-14 16:25:25 +03001491 if (xfer->tx_done && (xfer->rx_done == xfer->wait_for_resp))
Michal Kazior85622cd2013-11-25 14:06:22 +01001492 return 0;
1493
1494 schedule();
1495 }
1496
1497 return -ETIMEDOUT;
1498}
1499
Kalle Valo5e3dd152013-06-12 20:52:10 +03001500/*
Kalle Valo5e3dd152013-06-12 20:52:10 +03001501 * Send an interrupt to the device to wake up the Target CPU
1502 * so it has an opportunity to notice any changed state.
1503 */
1504static int ath10k_pci_wake_target_cpu(struct ath10k *ar)
1505{
Michal Kazior9e264942014-09-02 11:00:21 +03001506 u32 addr, val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001507
Michal Kazior9e264942014-09-02 11:00:21 +03001508 addr = SOC_CORE_BASE_ADDRESS | CORE_CTRL_ADDRESS;
1509 val = ath10k_pci_read32(ar, addr);
1510 val |= CORE_CTRL_CPU_INTR_MASK;
1511 ath10k_pci_write32(ar, addr, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001512
Michal Kazior1d2b48d2013-11-08 08:01:34 +01001513 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001514}
1515
Michal Kaziord63955b2015-01-24 12:14:49 +02001516static int ath10k_pci_get_num_banks(struct ath10k *ar)
1517{
1518 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1519
1520 switch (ar_pci->pdev->device) {
1521 case QCA988X_2_0_DEVICE_ID:
1522 return 1;
1523 case QCA6174_2_1_DEVICE_ID:
1524 switch (MS(ar->chip_id, SOC_CHIP_ID_REV)) {
1525 case QCA6174_HW_1_0_CHIP_ID_REV:
1526 case QCA6174_HW_1_1_CHIP_ID_REV:
1527 return 3;
1528 case QCA6174_HW_1_3_CHIP_ID_REV:
1529 return 2;
1530 case QCA6174_HW_2_1_CHIP_ID_REV:
1531 case QCA6174_HW_2_2_CHIP_ID_REV:
1532 return 6;
1533 case QCA6174_HW_3_0_CHIP_ID_REV:
1534 case QCA6174_HW_3_1_CHIP_ID_REV:
1535 case QCA6174_HW_3_2_CHIP_ID_REV:
1536 return 9;
1537 }
1538 break;
1539 }
1540
1541 ath10k_warn(ar, "unknown number of banks, assuming 1\n");
1542 return 1;
1543}
1544
Kalle Valo5e3dd152013-06-12 20:52:10 +03001545static int ath10k_pci_init_config(struct ath10k *ar)
1546{
1547 u32 interconnect_targ_addr;
1548 u32 pcie_state_targ_addr = 0;
1549 u32 pipe_cfg_targ_addr = 0;
1550 u32 svc_to_pipe_map = 0;
1551 u32 pcie_config_flags = 0;
1552 u32 ealloc_value;
1553 u32 ealloc_targ_addr;
1554 u32 flag2_value;
1555 u32 flag2_targ_addr;
1556 int ret = 0;
1557
1558 /* Download to Target the CE Config and the service-to-CE map */
1559 interconnect_targ_addr =
1560 host_interest_item_address(HI_ITEM(hi_interconnect_state));
1561
1562 /* Supply Target-side CE configuration */
Michal Kazior9e264942014-09-02 11:00:21 +03001563 ret = ath10k_pci_diag_read32(ar, interconnect_targ_addr,
1564 &pcie_state_targ_addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001565 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001566 ath10k_err(ar, "Failed to get pcie state addr: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001567 return ret;
1568 }
1569
1570 if (pcie_state_targ_addr == 0) {
1571 ret = -EIO;
Michal Kazior7aa7a722014-08-25 12:09:38 +02001572 ath10k_err(ar, "Invalid pcie state addr\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001573 return ret;
1574 }
1575
Michal Kazior9e264942014-09-02 11:00:21 +03001576 ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
Kalle Valo5e3dd152013-06-12 20:52:10 +03001577 offsetof(struct pcie_state,
Michal Kazior9e264942014-09-02 11:00:21 +03001578 pipe_cfg_addr)),
1579 &pipe_cfg_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 pipe cfg addr: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001582 return ret;
1583 }
1584
1585 if (pipe_cfg_targ_addr == 0) {
1586 ret = -EIO;
Michal Kazior7aa7a722014-08-25 12:09:38 +02001587 ath10k_err(ar, "Invalid pipe cfg addr\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001588 return ret;
1589 }
1590
1591 ret = ath10k_pci_diag_write_mem(ar, pipe_cfg_targ_addr,
Kalle Valo5b07e072014-09-14 12:50:06 +03001592 target_ce_config_wlan,
1593 sizeof(target_ce_config_wlan));
Kalle Valo5e3dd152013-06-12 20:52:10 +03001594
1595 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001596 ath10k_err(ar, "Failed to write pipe cfg: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001597 return ret;
1598 }
1599
Michal Kazior9e264942014-09-02 11:00:21 +03001600 ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
Kalle Valo5e3dd152013-06-12 20:52:10 +03001601 offsetof(struct pcie_state,
Michal Kazior9e264942014-09-02 11:00:21 +03001602 svc_to_pipe_map)),
1603 &svc_to_pipe_map);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001604 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001605 ath10k_err(ar, "Failed to get svc/pipe map: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001606 return ret;
1607 }
1608
1609 if (svc_to_pipe_map == 0) {
1610 ret = -EIO;
Michal Kazior7aa7a722014-08-25 12:09:38 +02001611 ath10k_err(ar, "Invalid svc_to_pipe map\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03001612 return ret;
1613 }
1614
1615 ret = ath10k_pci_diag_write_mem(ar, svc_to_pipe_map,
Kalle Valo5b07e072014-09-14 12:50:06 +03001616 target_service_to_ce_map_wlan,
1617 sizeof(target_service_to_ce_map_wlan));
Kalle Valo5e3dd152013-06-12 20:52:10 +03001618 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001619 ath10k_err(ar, "Failed to write svc/pipe map: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001620 return ret;
1621 }
1622
Michal Kazior9e264942014-09-02 11:00:21 +03001623 ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
Kalle Valo5e3dd152013-06-12 20:52:10 +03001624 offsetof(struct pcie_state,
Michal Kazior9e264942014-09-02 11:00:21 +03001625 config_flags)),
1626 &pcie_config_flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001627 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001628 ath10k_err(ar, "Failed to get pcie config_flags: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001629 return ret;
1630 }
1631
1632 pcie_config_flags &= ~PCIE_CONFIG_FLAG_ENABLE_L1;
1633
Michal Kazior9e264942014-09-02 11:00:21 +03001634 ret = ath10k_pci_diag_write32(ar, (pcie_state_targ_addr +
1635 offsetof(struct pcie_state,
1636 config_flags)),
1637 pcie_config_flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001638 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001639 ath10k_err(ar, "Failed to write pcie config_flags: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001640 return ret;
1641 }
1642
1643 /* configure early allocation */
1644 ealloc_targ_addr = host_interest_item_address(HI_ITEM(hi_early_alloc));
1645
Michal Kazior9e264942014-09-02 11:00:21 +03001646 ret = ath10k_pci_diag_read32(ar, ealloc_targ_addr, &ealloc_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001647 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001648 ath10k_err(ar, "Faile to get early alloc val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001649 return ret;
1650 }
1651
1652 /* first bank is switched to IRAM */
1653 ealloc_value |= ((HI_EARLY_ALLOC_MAGIC << HI_EARLY_ALLOC_MAGIC_SHIFT) &
1654 HI_EARLY_ALLOC_MAGIC_MASK);
Michal Kaziord63955b2015-01-24 12:14:49 +02001655 ealloc_value |= ((ath10k_pci_get_num_banks(ar) <<
1656 HI_EARLY_ALLOC_IRAM_BANKS_SHIFT) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03001657 HI_EARLY_ALLOC_IRAM_BANKS_MASK);
1658
Michal Kazior9e264942014-09-02 11:00:21 +03001659 ret = ath10k_pci_diag_write32(ar, ealloc_targ_addr, ealloc_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001660 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001661 ath10k_err(ar, "Failed to set early alloc val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001662 return ret;
1663 }
1664
1665 /* Tell Target to proceed with initialization */
1666 flag2_targ_addr = host_interest_item_address(HI_ITEM(hi_option_flag2));
1667
Michal Kazior9e264942014-09-02 11:00:21 +03001668 ret = ath10k_pci_diag_read32(ar, flag2_targ_addr, &flag2_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001669 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001670 ath10k_err(ar, "Failed to get option val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001671 return ret;
1672 }
1673
1674 flag2_value |= HI_OPTION_EARLY_CFG_DONE;
1675
Michal Kazior9e264942014-09-02 11:00:21 +03001676 ret = ath10k_pci_diag_write32(ar, flag2_targ_addr, flag2_value);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001677 if (ret != 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001678 ath10k_err(ar, "Failed to set option val: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001679 return ret;
1680 }
1681
1682 return 0;
1683}
1684
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001685static int ath10k_pci_alloc_pipes(struct ath10k *ar)
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001686{
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001687 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1688 struct ath10k_pci_pipe *pipe;
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001689 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001690
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001691 for (i = 0; i < CE_COUNT; i++) {
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001692 pipe = &ar_pci->pipe_info[i];
1693 pipe->ce_hdl = &ar_pci->ce_states[i];
1694 pipe->pipe_num = i;
1695 pipe->hif_ce_state = ar;
1696
1697 ret = ath10k_ce_alloc_pipe(ar, i, &host_ce_config_wlan[i],
1698 ath10k_pci_ce_send_done,
1699 ath10k_pci_ce_recv_data);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001700 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001701 ath10k_err(ar, "failed to allocate copy engine pipe %d: %d\n",
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001702 i, ret);
1703 return ret;
1704 }
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001705
1706 /* Last CE is Diagnostic Window */
1707 if (i == CE_COUNT - 1) {
1708 ar_pci->ce_diag = pipe->ce_hdl;
1709 continue;
1710 }
1711
1712 pipe->buf_sz = (size_t)(host_ce_config_wlan[i].src_sz_max);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001713 }
1714
1715 return 0;
1716}
1717
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001718static void ath10k_pci_free_pipes(struct ath10k *ar)
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001719{
1720 int i;
1721
1722 for (i = 0; i < CE_COUNT; i++)
1723 ath10k_ce_free_pipe(ar, i);
1724}
Kalle Valo5e3dd152013-06-12 20:52:10 +03001725
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001726static int ath10k_pci_init_pipes(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001727{
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001728 int i, ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001729
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001730 for (i = 0; i < CE_COUNT; i++) {
1731 ret = ath10k_ce_init_pipe(ar, i, &host_ce_config_wlan[i]);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001732 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001733 ath10k_err(ar, "failed to initialize copy engine pipe %d: %d\n",
Michal Kazior84cbf3a2014-10-20 14:14:39 +02001734 i, ret);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02001735 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001736 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001737 }
1738
Kalle Valo5e3dd152013-06-12 20:52:10 +03001739 return 0;
1740}
1741
Michal Kazior5c771e72014-08-22 14:23:34 +02001742static bool ath10k_pci_has_fw_crashed(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001743{
Michal Kazior5c771e72014-08-22 14:23:34 +02001744 return ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS) &
1745 FW_IND_EVENT_PENDING;
1746}
Kalle Valo5e3dd152013-06-12 20:52:10 +03001747
Michal Kazior5c771e72014-08-22 14:23:34 +02001748static void ath10k_pci_fw_crashed_clear(struct ath10k *ar)
1749{
1750 u32 val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001751
Michal Kazior5c771e72014-08-22 14:23:34 +02001752 val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
1753 val &= ~FW_IND_EVENT_PENDING;
1754 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001755}
1756
Michal Kaziorde013572014-05-14 16:56:16 +03001757/* this function effectively clears target memory controller assert line */
1758static void ath10k_pci_warm_reset_si0(struct ath10k *ar)
1759{
1760 u32 val;
1761
1762 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1763 ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
1764 val | SOC_RESET_CONTROL_SI0_RST_MASK);
1765 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1766
1767 msleep(10);
1768
1769 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1770 ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
1771 val & ~SOC_RESET_CONTROL_SI0_RST_MASK);
1772 val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
1773
1774 msleep(10);
1775}
1776
Michal Kazior61c16482014-10-28 10:32:06 +01001777static void ath10k_pci_warm_reset_cpu(struct ath10k *ar)
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001778{
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001779 u32 val;
1780
Kalle Valob39712c2014-03-28 09:32:46 +02001781 ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, 0);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001782
Michal Kazior61c16482014-10-28 10:32:06 +01001783 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1784 SOC_RESET_CONTROL_ADDRESS);
1785 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1786 val | SOC_RESET_CONTROL_CPU_WARM_RST_MASK);
1787}
1788
1789static void ath10k_pci_warm_reset_ce(struct ath10k *ar)
1790{
1791 u32 val;
1792
1793 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1794 SOC_RESET_CONTROL_ADDRESS);
1795
1796 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1797 val | SOC_RESET_CONTROL_CE_RST_MASK);
1798 msleep(10);
1799 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
1800 val & ~SOC_RESET_CONTROL_CE_RST_MASK);
1801}
1802
1803static void ath10k_pci_warm_reset_clear_lf(struct ath10k *ar)
1804{
1805 u32 val;
1806
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001807 val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
1808 SOC_LF_TIMER_CONTROL0_ADDRESS);
1809 ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS +
1810 SOC_LF_TIMER_CONTROL0_ADDRESS,
1811 val & ~SOC_LF_TIMER_CONTROL0_ENABLE_MASK);
Michal Kazior61c16482014-10-28 10:32:06 +01001812}
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001813
Michal Kazior61c16482014-10-28 10:32:06 +01001814static int ath10k_pci_warm_reset(struct ath10k *ar)
1815{
1816 int ret;
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001817
Michal Kazior61c16482014-10-28 10:32:06 +01001818 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot warm reset\n");
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001819
Michal Kazior61c16482014-10-28 10:32:06 +01001820 spin_lock_bh(&ar->data_lock);
1821 ar->stats.fw_warm_reset_counter++;
1822 spin_unlock_bh(&ar->data_lock);
1823
1824 ath10k_pci_irq_disable(ar);
1825
1826 /* Make sure the target CPU is not doing anything dangerous, e.g. if it
1827 * were to access copy engine while host performs copy engine reset
1828 * then it is possible for the device to confuse pci-e controller to
1829 * the point of bringing host system to a complete stop (i.e. hang).
1830 */
Michal Kaziorde013572014-05-14 16:56:16 +03001831 ath10k_pci_warm_reset_si0(ar);
Michal Kazior61c16482014-10-28 10:32:06 +01001832 ath10k_pci_warm_reset_cpu(ar);
1833 ath10k_pci_init_pipes(ar);
1834 ath10k_pci_wait_for_target_init(ar);
Michal Kaziorde013572014-05-14 16:56:16 +03001835
Michal Kazior61c16482014-10-28 10:32:06 +01001836 ath10k_pci_warm_reset_clear_lf(ar);
1837 ath10k_pci_warm_reset_ce(ar);
1838 ath10k_pci_warm_reset_cpu(ar);
1839 ath10k_pci_init_pipes(ar);
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001840
Michal Kazior61c16482014-10-28 10:32:06 +01001841 ret = ath10k_pci_wait_for_target_init(ar);
1842 if (ret) {
1843 ath10k_warn(ar, "failed to wait for target init: %d\n", ret);
1844 return ret;
1845 }
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001846
Michal Kazior7aa7a722014-08-25 12:09:38 +02001847 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot warm reset complete\n");
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001848
Michal Kaziorc0c378f2014-08-07 11:03:28 +02001849 return 0;
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01001850}
1851
Michal Kaziord63955b2015-01-24 12:14:49 +02001852static int ath10k_pci_qca988x_chip_reset(struct ath10k *ar)
Michal Kazior0bc14d02014-10-28 10:32:07 +01001853{
1854 int i, ret;
1855 u32 val;
1856
Michal Kaziord63955b2015-01-24 12:14:49 +02001857 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot 988x chip reset\n");
Michal Kazior0bc14d02014-10-28 10:32:07 +01001858
1859 /* Some hardware revisions (e.g. CUS223v2) has issues with cold reset.
1860 * It is thus preferred to use warm reset which is safer but may not be
1861 * able to recover the device from all possible fail scenarios.
1862 *
1863 * Warm reset doesn't always work on first try so attempt it a few
1864 * times before giving up.
1865 */
1866 for (i = 0; i < ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS; i++) {
1867 ret = ath10k_pci_warm_reset(ar);
1868 if (ret) {
1869 ath10k_warn(ar, "failed to warm reset attempt %d of %d: %d\n",
1870 i + 1, ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS,
1871 ret);
1872 continue;
1873 }
1874
1875 /* FIXME: Sometimes copy engine doesn't recover after warm
1876 * reset. In most cases this needs cold reset. In some of these
1877 * cases the device is in such a state that a cold reset may
1878 * lock up the host.
1879 *
1880 * Reading any host interest register via copy engine is
1881 * sufficient to verify if device is capable of booting
1882 * firmware blob.
1883 */
1884 ret = ath10k_pci_init_pipes(ar);
1885 if (ret) {
1886 ath10k_warn(ar, "failed to init copy engine: %d\n",
1887 ret);
1888 continue;
1889 }
1890
1891 ret = ath10k_pci_diag_read32(ar, QCA988X_HOST_INTEREST_ADDRESS,
1892 &val);
1893 if (ret) {
1894 ath10k_warn(ar, "failed to poke copy engine: %d\n",
1895 ret);
1896 continue;
1897 }
1898
1899 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot chip reset complete (warm)\n");
1900 return 0;
1901 }
1902
1903 if (ath10k_pci_reset_mode == ATH10K_PCI_RESET_WARM_ONLY) {
1904 ath10k_warn(ar, "refusing cold reset as requested\n");
1905 return -EPERM;
1906 }
1907
1908 ret = ath10k_pci_cold_reset(ar);
1909 if (ret) {
1910 ath10k_warn(ar, "failed to cold reset: %d\n", ret);
1911 return ret;
1912 }
1913
1914 ret = ath10k_pci_wait_for_target_init(ar);
1915 if (ret) {
1916 ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
1917 ret);
1918 return ret;
1919 }
1920
Michal Kaziord63955b2015-01-24 12:14:49 +02001921 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca988x chip reset complete (cold)\n");
Michal Kazior0bc14d02014-10-28 10:32:07 +01001922
1923 return 0;
1924}
1925
Michal Kaziord63955b2015-01-24 12:14:49 +02001926static int ath10k_pci_qca6174_chip_reset(struct ath10k *ar)
1927{
1928 int ret;
1929
1930 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca6174 chip reset\n");
1931
1932 /* FIXME: QCA6174 requires cold + warm reset to work. */
1933
1934 ret = ath10k_pci_cold_reset(ar);
1935 if (ret) {
1936 ath10k_warn(ar, "failed to cold reset: %d\n", ret);
1937 return ret;
1938 }
1939
1940 ret = ath10k_pci_wait_for_target_init(ar);
1941 if (ret) {
1942 ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
1943 ret);
1944 return ret;
1945 }
1946
1947 ret = ath10k_pci_warm_reset(ar);
1948 if (ret) {
1949 ath10k_warn(ar, "failed to warm reset: %d\n", ret);
1950 return ret;
1951 }
1952
1953 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca6174 chip reset complete (cold)\n");
1954
1955 return 0;
1956}
1957
1958static int ath10k_pci_chip_reset(struct ath10k *ar)
1959{
1960 if (QCA_REV_988X(ar))
1961 return ath10k_pci_qca988x_chip_reset(ar);
1962 else if (QCA_REV_6174(ar))
1963 return ath10k_pci_qca6174_chip_reset(ar);
1964 else
1965 return -ENOTSUPP;
1966}
1967
Michal Kazior0bc14d02014-10-28 10:32:07 +01001968static int ath10k_pci_hif_power_up(struct ath10k *ar)
Michal Kazior8c5c5362013-07-16 09:38:50 +02001969{
1970 int ret;
1971
Michal Kazior0bc14d02014-10-28 10:32:07 +01001972 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif power up\n");
1973
Bartosz Markowski707b1bbd2014-10-31 09:03:43 +01001974 ret = ath10k_pci_wake(ar);
1975 if (ret) {
1976 ath10k_err(ar, "failed to wake up target: %d\n", ret);
1977 return ret;
1978 }
1979
Michal Kazior8c5c5362013-07-16 09:38:50 +02001980 /*
1981 * Bring the target up cleanly.
1982 *
1983 * The target may be in an undefined state with an AUX-powered Target
1984 * and a Host in WoW mode. If the Host crashes, loses power, or is
1985 * restarted (without unloading the driver) then the Target is left
1986 * (aux) powered and running. On a subsequent driver load, the Target
1987 * is in an unexpected state. We try to catch that here in order to
1988 * reset the Target and retry the probe.
1989 */
Michal Kazior0bc14d02014-10-28 10:32:07 +01001990 ret = ath10k_pci_chip_reset(ar);
Michal Kazior5b2589f2013-11-08 08:01:30 +01001991 if (ret) {
Michal Kaziora2fa8802015-01-12 15:29:37 +01001992 if (ath10k_pci_has_fw_crashed(ar)) {
1993 ath10k_warn(ar, "firmware crashed during chip reset\n");
1994 ath10k_pci_fw_crashed_clear(ar);
1995 ath10k_pci_fw_crashed_dump(ar);
1996 }
1997
Michal Kazior0bc14d02014-10-28 10:32:07 +01001998 ath10k_err(ar, "failed to reset chip: %d\n", ret);
Bartosz Markowski707b1bbd2014-10-31 09:03:43 +01001999 goto err_sleep;
Michal Kazior5b2589f2013-11-08 08:01:30 +01002000 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02002001
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002002 ret = ath10k_pci_init_pipes(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02002003 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002004 ath10k_err(ar, "failed to initialize CE: %d\n", ret);
Bartosz Markowski707b1bbd2014-10-31 09:03:43 +01002005 goto err_sleep;
Michal Kaziorab977bd2013-11-25 14:06:26 +01002006 }
2007
Michal Kazior98563d52013-11-08 08:01:33 +01002008 ret = ath10k_pci_init_config(ar);
2009 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002010 ath10k_err(ar, "failed to setup init config: %d\n", ret);
Michal Kazior5c771e72014-08-22 14:23:34 +02002011 goto err_ce;
Michal Kazior98563d52013-11-08 08:01:33 +01002012 }
Michal Kazior8c5c5362013-07-16 09:38:50 +02002013
2014 ret = ath10k_pci_wake_target_cpu(ar);
2015 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002016 ath10k_err(ar, "could not wake up target CPU: %d\n", ret);
Michal Kazior5c771e72014-08-22 14:23:34 +02002017 goto err_ce;
Michal Kazior8c5c5362013-07-16 09:38:50 +02002018 }
2019
2020 return 0;
2021
2022err_ce:
2023 ath10k_pci_ce_deinit(ar);
Michal Kazior0bc14d02014-10-28 10:32:07 +01002024
Bartosz Markowski707b1bbd2014-10-31 09:03:43 +01002025err_sleep:
2026 ath10k_pci_sleep(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02002027 return ret;
2028}
2029
2030static void ath10k_pci_hif_power_down(struct ath10k *ar)
2031{
Michal Kazior7aa7a722014-08-25 12:09:38 +02002032 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif power down\n");
Bartosz Markowski8cc8df92013-08-02 09:58:49 +02002033
Michal Kaziorc011b282014-10-28 10:32:08 +01002034 /* Currently hif_power_up performs effectively a reset and hif_stop
2035 * resets the chip as well so there's no point in resetting here.
2036 */
Bartosz Markowski707b1bbd2014-10-31 09:03:43 +01002037
2038 ath10k_pci_sleep(ar);
Michal Kazior8c5c5362013-07-16 09:38:50 +02002039}
2040
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002041#ifdef CONFIG_PM
2042
2043#define ATH10K_PCI_PM_CONTROL 0x44
2044
2045static int ath10k_pci_hif_suspend(struct ath10k *ar)
2046{
2047 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2048 struct pci_dev *pdev = ar_pci->pdev;
2049 u32 val;
2050
2051 pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
2052
2053 if ((val & 0x000000ff) != 0x3) {
2054 pci_save_state(pdev);
2055 pci_disable_device(pdev);
2056 pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
2057 (val & 0xffffff00) | 0x03);
2058 }
2059
2060 return 0;
2061}
2062
2063static int ath10k_pci_hif_resume(struct ath10k *ar)
2064{
2065 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2066 struct pci_dev *pdev = ar_pci->pdev;
2067 u32 val;
2068
2069 pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
2070
2071 if ((val & 0x000000ff) != 0) {
2072 pci_restore_state(pdev);
2073 pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
2074 val & 0xffffff00);
2075 /*
2076 * Suspend/Resume resets the PCI configuration space,
2077 * so we have to re-disable the RETRY_TIMEOUT register (0x41)
2078 * to keep PCI Tx retries from interfering with C3 CPU state
2079 */
2080 pci_read_config_dword(pdev, 0x40, &val);
2081
2082 if ((val & 0x0000ff00) != 0)
2083 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
2084 }
2085
2086 return 0;
2087}
2088#endif
2089
Kalle Valo5e3dd152013-06-12 20:52:10 +03002090static const struct ath10k_hif_ops ath10k_pci_hif_ops = {
Michal Kazior726346f2014-02-27 18:50:04 +02002091 .tx_sg = ath10k_pci_hif_tx_sg,
Kalle Valoeef25402014-09-24 14:16:52 +03002092 .diag_read = ath10k_pci_hif_diag_read,
Yanbo Li9f65ad22014-11-25 12:24:48 +02002093 .diag_write = ath10k_pci_diag_write_mem,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002094 .exchange_bmi_msg = ath10k_pci_hif_exchange_bmi_msg,
2095 .start = ath10k_pci_hif_start,
2096 .stop = ath10k_pci_hif_stop,
2097 .map_service_to_pipe = ath10k_pci_hif_map_service_to_pipe,
2098 .get_default_pipe = ath10k_pci_hif_get_default_pipe,
2099 .send_complete_check = ath10k_pci_hif_send_complete_check,
Michal Kaziore799bbf2013-07-05 16:15:12 +03002100 .set_callbacks = ath10k_pci_hif_set_callbacks,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002101 .get_free_queue_number = ath10k_pci_hif_get_free_queue_number,
Michal Kazior8c5c5362013-07-16 09:38:50 +02002102 .power_up = ath10k_pci_hif_power_up,
2103 .power_down = ath10k_pci_hif_power_down,
Yanbo Li077a3802014-11-25 12:24:33 +02002104 .read32 = ath10k_pci_read32,
2105 .write32 = ath10k_pci_write32,
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002106#ifdef CONFIG_PM
2107 .suspend = ath10k_pci_hif_suspend,
2108 .resume = ath10k_pci_hif_resume,
2109#endif
Kalle Valo5e3dd152013-06-12 20:52:10 +03002110};
2111
2112static void ath10k_pci_ce_tasklet(unsigned long ptr)
2113{
Michal Kazior87263e52013-08-27 13:08:01 +02002114 struct ath10k_pci_pipe *pipe = (struct ath10k_pci_pipe *)ptr;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002115 struct ath10k_pci *ar_pci = pipe->ar_pci;
2116
2117 ath10k_ce_per_engine_service(ar_pci->ar, pipe->pipe_num);
2118}
2119
2120static void ath10k_msi_err_tasklet(unsigned long data)
2121{
2122 struct ath10k *ar = (struct ath10k *)data;
2123
Michal Kazior5c771e72014-08-22 14:23:34 +02002124 if (!ath10k_pci_has_fw_crashed(ar)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002125 ath10k_warn(ar, "received unsolicited fw crash interrupt\n");
Michal Kazior5c771e72014-08-22 14:23:34 +02002126 return;
2127 }
2128
Michal Kazior6f3b7ff2015-01-24 12:14:52 +02002129 ath10k_pci_irq_disable(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02002130 ath10k_pci_fw_crashed_clear(ar);
2131 ath10k_pci_fw_crashed_dump(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002132}
2133
2134/*
2135 * Handler for a per-engine interrupt on a PARTICULAR CE.
2136 * This is used in cases where each CE has a private MSI interrupt.
2137 */
2138static irqreturn_t ath10k_pci_per_engine_handler(int irq, void *arg)
2139{
2140 struct ath10k *ar = arg;
2141 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2142 int ce_id = irq - ar_pci->pdev->irq - MSI_ASSIGN_CE_INITIAL;
2143
Dan Carpentere5742672013-06-18 10:28:46 +03002144 if (ce_id < 0 || ce_id >= ARRAY_SIZE(ar_pci->pipe_info)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002145 ath10k_warn(ar, "unexpected/invalid irq %d ce_id %d\n", irq,
2146 ce_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002147 return IRQ_HANDLED;
2148 }
2149
2150 /*
2151 * NOTE: We are able to derive ce_id from irq because we
2152 * use a one-to-one mapping for CE's 0..5.
2153 * CE's 6 & 7 do not use interrupts at all.
2154 *
2155 * This mapping must be kept in sync with the mapping
2156 * used by firmware.
2157 */
2158 tasklet_schedule(&ar_pci->pipe_info[ce_id].intr);
2159 return IRQ_HANDLED;
2160}
2161
2162static irqreturn_t ath10k_pci_msi_fw_handler(int irq, void *arg)
2163{
2164 struct ath10k *ar = arg;
2165 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2166
2167 tasklet_schedule(&ar_pci->msi_fw_err);
2168 return IRQ_HANDLED;
2169}
2170
2171/*
2172 * Top-level interrupt handler for all PCI interrupts from a Target.
2173 * When a block of MSI interrupts is allocated, this top-level handler
2174 * is not used; instead, we directly call the correct sub-handler.
2175 */
2176static irqreturn_t ath10k_pci_interrupt_handler(int irq, void *arg)
2177{
2178 struct ath10k *ar = arg;
2179 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2180
2181 if (ar_pci->num_msi_intrs == 0) {
Michal Kaziore5398872013-11-25 14:06:20 +01002182 if (!ath10k_pci_irq_pending(ar))
2183 return IRQ_NONE;
2184
Michal Kazior26852182013-11-25 14:06:25 +01002185 ath10k_pci_disable_and_clear_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002186 }
2187
2188 tasklet_schedule(&ar_pci->intr_tq);
2189
2190 return IRQ_HANDLED;
2191}
2192
2193static void ath10k_pci_tasklet(unsigned long data)
2194{
2195 struct ath10k *ar = (struct ath10k *)data;
2196 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2197
Michal Kazior5c771e72014-08-22 14:23:34 +02002198 if (ath10k_pci_has_fw_crashed(ar)) {
Michal Kazior6f3b7ff2015-01-24 12:14:52 +02002199 ath10k_pci_irq_disable(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02002200 ath10k_pci_fw_crashed_clear(ar);
2201 ath10k_pci_fw_crashed_dump(ar);
2202 return;
2203 }
2204
Kalle Valo5e3dd152013-06-12 20:52:10 +03002205 ath10k_ce_per_engine_service_any(ar);
2206
Michal Kazior26852182013-11-25 14:06:25 +01002207 /* Re-enable legacy irq that was disabled in the irq handler */
2208 if (ar_pci->num_msi_intrs == 0)
2209 ath10k_pci_enable_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002210}
2211
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002212static int ath10k_pci_request_irq_msix(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002213{
2214 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002215 int ret, i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002216
2217 ret = request_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW,
2218 ath10k_pci_msi_fw_handler,
2219 IRQF_SHARED, "ath10k_pci", ar);
Michal Kazior591ecdb2013-07-31 10:55:15 +02002220 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002221 ath10k_warn(ar, "failed to request MSI-X fw irq %d: %d\n",
Michal Kazior591ecdb2013-07-31 10:55:15 +02002222 ar_pci->pdev->irq + MSI_ASSIGN_FW, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002223 return ret;
Michal Kazior591ecdb2013-07-31 10:55:15 +02002224 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002225
2226 for (i = MSI_ASSIGN_CE_INITIAL; i <= MSI_ASSIGN_CE_MAX; i++) {
2227 ret = request_irq(ar_pci->pdev->irq + i,
2228 ath10k_pci_per_engine_handler,
2229 IRQF_SHARED, "ath10k_pci", ar);
2230 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002231 ath10k_warn(ar, "failed to request MSI-X ce irq %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002232 ar_pci->pdev->irq + i, ret);
2233
Michal Kazior87b14232013-06-26 08:50:50 +02002234 for (i--; i >= MSI_ASSIGN_CE_INITIAL; i--)
2235 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002236
Michal Kazior87b14232013-06-26 08:50:50 +02002237 free_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002238 return ret;
2239 }
2240 }
2241
Kalle Valo5e3dd152013-06-12 20:52:10 +03002242 return 0;
2243}
2244
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002245static int ath10k_pci_request_irq_msi(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002246{
2247 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2248 int ret;
2249
2250 ret = request_irq(ar_pci->pdev->irq,
2251 ath10k_pci_interrupt_handler,
2252 IRQF_SHARED, "ath10k_pci", ar);
Kalle Valof3782742013-10-17 11:36:15 +03002253 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002254 ath10k_warn(ar, "failed to request MSI irq %d: %d\n",
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002255 ar_pci->pdev->irq, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002256 return ret;
Kalle Valof3782742013-10-17 11:36:15 +03002257 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002258
Kalle Valo5e3dd152013-06-12 20:52:10 +03002259 return 0;
2260}
2261
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002262static int ath10k_pci_request_irq_legacy(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002263{
2264 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002265 int ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002266
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002267 ret = request_irq(ar_pci->pdev->irq,
2268 ath10k_pci_interrupt_handler,
2269 IRQF_SHARED, "ath10k_pci", ar);
Kalle Valof3782742013-10-17 11:36:15 +03002270 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002271 ath10k_warn(ar, "failed to request legacy irq %d: %d\n",
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002272 ar_pci->pdev->irq, ret);
Kalle Valof3782742013-10-17 11:36:15 +03002273 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002274 }
2275
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002276 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002277}
2278
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002279static int ath10k_pci_request_irq(struct ath10k *ar)
2280{
2281 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2282
2283 switch (ar_pci->num_msi_intrs) {
2284 case 0:
2285 return ath10k_pci_request_irq_legacy(ar);
2286 case 1:
2287 return ath10k_pci_request_irq_msi(ar);
2288 case MSI_NUM_REQUEST:
2289 return ath10k_pci_request_irq_msix(ar);
2290 }
2291
Michal Kazior7aa7a722014-08-25 12:09:38 +02002292 ath10k_warn(ar, "unknown irq configuration upon request\n");
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002293 return -EINVAL;
2294}
2295
2296static void ath10k_pci_free_irq(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002297{
2298 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2299 int i;
2300
2301 /* There's at least one interrupt irregardless whether its legacy INTR
2302 * or MSI or MSI-X */
2303 for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
2304 free_irq(ar_pci->pdev->irq + i, ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002305}
2306
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002307static void ath10k_pci_init_irq_tasklets(struct ath10k *ar)
2308{
2309 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2310 int i;
2311
2312 tasklet_init(&ar_pci->intr_tq, ath10k_pci_tasklet, (unsigned long)ar);
2313 tasklet_init(&ar_pci->msi_fw_err, ath10k_msi_err_tasklet,
2314 (unsigned long)ar);
2315
2316 for (i = 0; i < CE_COUNT; i++) {
2317 ar_pci->pipe_info[i].ar_pci = ar_pci;
2318 tasklet_init(&ar_pci->pipe_info[i].intr, ath10k_pci_ce_tasklet,
2319 (unsigned long)&ar_pci->pipe_info[i]);
2320 }
2321}
2322
2323static int ath10k_pci_init_irq(struct ath10k *ar)
2324{
2325 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2326 int ret;
2327
2328 ath10k_pci_init_irq_tasklets(ar);
2329
Michal Kazior403d6272014-08-22 14:23:31 +02002330 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_AUTO)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002331 ath10k_info(ar, "limiting irq mode to: %d\n",
2332 ath10k_pci_irq_mode);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002333
2334 /* Try MSI-X */
Michal Kazior0edf2572014-08-07 11:03:29 +02002335 if (ath10k_pci_irq_mode == ATH10K_PCI_IRQ_AUTO) {
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002336 ar_pci->num_msi_intrs = MSI_NUM_REQUEST;
Alexander Gordeev5ad68672014-02-13 17:50:02 +02002337 ret = pci_enable_msi_range(ar_pci->pdev, ar_pci->num_msi_intrs,
Kalle Valo5b07e072014-09-14 12:50:06 +03002338 ar_pci->num_msi_intrs);
Alexander Gordeev5ad68672014-02-13 17:50:02 +02002339 if (ret > 0)
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002340 return 0;
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002341
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002342 /* fall-through */
2343 }
2344
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002345 /* Try MSI */
Michal Kaziorcfe9c452013-11-25 14:06:27 +01002346 if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_LEGACY) {
2347 ar_pci->num_msi_intrs = 1;
2348 ret = pci_enable_msi(ar_pci->pdev);
2349 if (ret == 0)
2350 return 0;
2351
2352 /* fall-through */
2353 }
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002354
2355 /* Try legacy irq
2356 *
2357 * A potential race occurs here: The CORE_BASE write
2358 * depends on target correctly decoding AXI address but
2359 * host won't know when target writes BAR to CORE_CTRL.
2360 * This write might get lost if target has NOT written BAR.
2361 * For now, fix the race by repeating the write in below
2362 * synchronization checking. */
2363 ar_pci->num_msi_intrs = 0;
2364
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002365 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2366 PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002367
2368 return 0;
2369}
2370
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002371static void ath10k_pci_deinit_irq_legacy(struct ath10k *ar)
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002372{
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002373 ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
2374 0);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002375}
2376
2377static int ath10k_pci_deinit_irq(struct ath10k *ar)
2378{
2379 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2380
2381 switch (ar_pci->num_msi_intrs) {
2382 case 0:
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002383 ath10k_pci_deinit_irq_legacy(ar);
2384 return 0;
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002385 case 1:
2386 /* fall-through */
2387 case MSI_NUM_REQUEST:
2388 pci_disable_msi(ar_pci->pdev);
2389 return 0;
Alexander Gordeevbb8b6212014-02-13 17:50:01 +02002390 default:
2391 pci_disable_msi(ar_pci->pdev);
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002392 }
2393
Michal Kazior7aa7a722014-08-25 12:09:38 +02002394 ath10k_warn(ar, "unknown irq configuration upon deinit\n");
Michal Kaziorfc15ca12013-11-25 14:06:21 +01002395 return -EINVAL;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002396}
2397
Michal Kaziord7fb47f2013-11-08 08:01:26 +01002398static int ath10k_pci_wait_for_target_init(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002399{
2400 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
Kalle Valo0399eca2014-03-28 09:32:21 +02002401 unsigned long timeout;
Kalle Valo0399eca2014-03-28 09:32:21 +02002402 u32 val;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002403
Michal Kazior7aa7a722014-08-25 12:09:38 +02002404 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot waiting target to initialise\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002405
Kalle Valo0399eca2014-03-28 09:32:21 +02002406 timeout = jiffies + msecs_to_jiffies(ATH10K_PCI_TARGET_WAIT);
2407
2408 do {
2409 val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
2410
Michal Kazior7aa7a722014-08-25 12:09:38 +02002411 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target indicator %x\n",
2412 val);
Kalle Valo50f87a62014-03-28 09:32:52 +02002413
Kalle Valo0399eca2014-03-28 09:32:21 +02002414 /* target should never return this */
2415 if (val == 0xffffffff)
2416 continue;
2417
Michal Kazior7710cd22014-04-23 19:30:04 +03002418 /* the device has crashed so don't bother trying anymore */
2419 if (val & FW_IND_EVENT_PENDING)
2420 break;
2421
Kalle Valo0399eca2014-03-28 09:32:21 +02002422 if (val & FW_IND_INITIALIZED)
2423 break;
2424
Kalle Valo5e3dd152013-06-12 20:52:10 +03002425 if (ar_pci->num_msi_intrs == 0)
2426 /* Fix potential race by repeating CORE_BASE writes */
Michal Kaziora4282492014-10-20 14:14:37 +02002427 ath10k_pci_enable_legacy_irq(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002428
Kalle Valo0399eca2014-03-28 09:32:21 +02002429 mdelay(10);
2430 } while (time_before(jiffies, timeout));
2431
Michal Kaziora4282492014-10-20 14:14:37 +02002432 ath10k_pci_disable_and_clear_legacy_irq(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02002433 ath10k_pci_irq_msi_fw_mask(ar);
Michal Kaziora4282492014-10-20 14:14:37 +02002434
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002435 if (val == 0xffffffff) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002436 ath10k_err(ar, "failed to read device register, device is gone\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002437 return -EIO;
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002438 }
2439
Michal Kazior7710cd22014-04-23 19:30:04 +03002440 if (val & FW_IND_EVENT_PENDING) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002441 ath10k_warn(ar, "device has crashed during init\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002442 return -ECOMM;
Michal Kazior7710cd22014-04-23 19:30:04 +03002443 }
2444
Michal Kazior6a4f6e12014-04-23 19:30:03 +03002445 if (!(val & FW_IND_INITIALIZED)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002446 ath10k_err(ar, "failed to receive initialized event from target: %08x\n",
Kalle Valo0399eca2014-03-28 09:32:21 +02002447 val);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002448 return -ETIMEDOUT;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002449 }
2450
Michal Kazior7aa7a722014-08-25 12:09:38 +02002451 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target initialised\n");
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002452 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002453}
2454
Michal Kaziorfc36e3f2014-02-10 17:14:22 +01002455static int ath10k_pci_cold_reset(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002456{
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002457 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002458 u32 val;
2459
Michal Kazior7aa7a722014-08-25 12:09:38 +02002460 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cold reset\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002461
Ben Greearf51dbe72014-09-29 14:41:46 +03002462 spin_lock_bh(&ar->data_lock);
2463
2464 ar->stats.fw_cold_reset_counter++;
2465
2466 spin_unlock_bh(&ar->data_lock);
2467
Kalle Valo5e3dd152013-06-12 20:52:10 +03002468 /* Put Target, including PCIe, into RESET. */
Kalle Valoe479ed42013-09-01 10:01:53 +03002469 val = ath10k_pci_reg_read32(ar, SOC_GLOBAL_RESET_ADDRESS);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002470 val |= 1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002471 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002472
2473 for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
Kalle Valoe479ed42013-09-01 10:01:53 +03002474 if (ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03002475 RTC_STATE_COLD_RESET_MASK)
2476 break;
2477 msleep(1);
2478 }
2479
2480 /* Pull Target, including PCIe, out of RESET. */
2481 val &= ~1;
Kalle Valoe479ed42013-09-01 10:01:53 +03002482 ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002483
2484 for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
Kalle Valoe479ed42013-09-01 10:01:53 +03002485 if (!(ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
Kalle Valo5e3dd152013-06-12 20:52:10 +03002486 RTC_STATE_COLD_RESET_MASK))
2487 break;
2488 msleep(1);
2489 }
2490
Michal Kazior7aa7a722014-08-25 12:09:38 +02002491 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cold reset complete\n");
Kalle Valo50f87a62014-03-28 09:32:52 +02002492
Michal Kazior5b2589f2013-11-08 08:01:30 +01002493 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002494}
2495
Michal Kazior2986e3e2014-08-07 11:03:30 +02002496static int ath10k_pci_claim(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002497{
Michal Kazior2986e3e2014-08-07 11:03:30 +02002498 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2499 struct pci_dev *pdev = ar_pci->pdev;
2500 u32 lcr_val;
2501 int ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002502
2503 pci_set_drvdata(pdev, ar);
2504
Kalle Valo5e3dd152013-06-12 20:52:10 +03002505 ret = pci_enable_device(pdev);
2506 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002507 ath10k_err(ar, "failed to enable pci device: %d\n", ret);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002508 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002509 }
2510
Kalle Valo5e3dd152013-06-12 20:52:10 +03002511 ret = pci_request_region(pdev, BAR_NUM, "ath");
2512 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002513 ath10k_err(ar, "failed to request region BAR%d: %d\n", BAR_NUM,
Michal Kazior2986e3e2014-08-07 11:03:30 +02002514 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002515 goto err_device;
2516 }
2517
Michal Kazior2986e3e2014-08-07 11:03:30 +02002518 /* Target expects 32 bit DMA. Enforce it. */
Kalle Valo5e3dd152013-06-12 20:52:10 +03002519 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2520 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002521 ath10k_err(ar, "failed to set dma mask to 32-bit: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002522 goto err_region;
2523 }
2524
2525 ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2526 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002527 ath10k_err(ar, "failed to set consistent dma mask to 32-bit: %d\n",
Michal Kazior2986e3e2014-08-07 11:03:30 +02002528 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002529 goto err_region;
2530 }
2531
Kalle Valo5e3dd152013-06-12 20:52:10 +03002532 pci_set_master(pdev);
2533
Michal Kazior2986e3e2014-08-07 11:03:30 +02002534 /* Workaround: Disable ASPM */
Kalle Valo5e3dd152013-06-12 20:52:10 +03002535 pci_read_config_dword(pdev, 0x80, &lcr_val);
2536 pci_write_config_dword(pdev, 0x80, (lcr_val & 0xffffff00));
2537
2538 /* Arrange for access to Target SoC registers. */
Michal Kazior2986e3e2014-08-07 11:03:30 +02002539 ar_pci->mem = pci_iomap(pdev, BAR_NUM, 0);
2540 if (!ar_pci->mem) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002541 ath10k_err(ar, "failed to iomap BAR%d\n", BAR_NUM);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002542 ret = -EIO;
2543 goto err_master;
2544 }
2545
Michal Kazior7aa7a722014-08-25 12:09:38 +02002546 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot pci_mem 0x%p\n", ar_pci->mem);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002547 return 0;
2548
2549err_master:
2550 pci_clear_master(pdev);
2551
2552err_region:
2553 pci_release_region(pdev, BAR_NUM);
2554
2555err_device:
2556 pci_disable_device(pdev);
2557
2558 return ret;
2559}
2560
2561static void ath10k_pci_release(struct ath10k *ar)
2562{
2563 struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
2564 struct pci_dev *pdev = ar_pci->pdev;
2565
2566 pci_iounmap(pdev, ar_pci->mem);
2567 pci_release_region(pdev, BAR_NUM);
2568 pci_clear_master(pdev);
2569 pci_disable_device(pdev);
2570}
2571
Michal Kazior7505f7c2014-12-02 10:55:54 +02002572static bool ath10k_pci_chip_is_supported(u32 dev_id, u32 chip_id)
2573{
2574 const struct ath10k_pci_supp_chip *supp_chip;
2575 int i;
2576 u32 rev_id = MS(chip_id, SOC_CHIP_ID_REV);
2577
2578 for (i = 0; i < ARRAY_SIZE(ath10k_pci_supp_chips); i++) {
2579 supp_chip = &ath10k_pci_supp_chips[i];
2580
2581 if (supp_chip->dev_id == dev_id &&
2582 supp_chip->rev_id == rev_id)
2583 return true;
2584 }
2585
2586 return false;
2587}
2588
Kalle Valo5e3dd152013-06-12 20:52:10 +03002589static int ath10k_pci_probe(struct pci_dev *pdev,
2590 const struct pci_device_id *pci_dev)
2591{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002592 int ret = 0;
2593 struct ath10k *ar;
2594 struct ath10k_pci *ar_pci;
Michal Kaziord63955b2015-01-24 12:14:49 +02002595 enum ath10k_hw_rev hw_rev;
Michal Kazior2986e3e2014-08-07 11:03:30 +02002596 u32 chip_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002597
Michal Kaziord63955b2015-01-24 12:14:49 +02002598 switch (pci_dev->device) {
2599 case QCA988X_2_0_DEVICE_ID:
2600 hw_rev = ATH10K_HW_QCA988X;
2601 break;
2602 case QCA6174_2_1_DEVICE_ID:
2603 hw_rev = ATH10K_HW_QCA6174;
2604 break;
2605 default:
2606 WARN_ON(1);
2607 return -ENOTSUPP;
2608 }
2609
2610 ar = ath10k_core_create(sizeof(*ar_pci), &pdev->dev, ATH10K_BUS_PCI,
2611 hw_rev, &ath10k_pci_hif_ops);
Michal Kaziore7b54192014-08-07 11:03:27 +02002612 if (!ar) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002613 dev_err(&pdev->dev, "failed to allocate core\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002614 return -ENOMEM;
Michal Kaziore7b54192014-08-07 11:03:27 +02002615 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002616
Michal Kazior7aa7a722014-08-25 12:09:38 +02002617 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci probe\n");
2618
Michal Kaziore7b54192014-08-07 11:03:27 +02002619 ar_pci = ath10k_pci_priv(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002620 ar_pci->pdev = pdev;
2621 ar_pci->dev = &pdev->dev;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002622 ar_pci->ar = ar;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002623
2624 spin_lock_init(&ar_pci->ce_lock);
Michal Kazior728f95e2014-08-22 14:33:14 +02002625 setup_timer(&ar_pci->rx_post_retry, ath10k_pci_rx_replenish_retry,
2626 (unsigned long)ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002627
Michal Kazior2986e3e2014-08-07 11:03:30 +02002628 ret = ath10k_pci_claim(ar);
Kalle Valoe01ae682013-09-01 11:22:14 +03002629 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002630 ath10k_err(ar, "failed to claim device: %d\n", ret);
Michal Kaziore7b54192014-08-07 11:03:27 +02002631 goto err_core_destroy;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002632 }
2633
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002634 ret = ath10k_pci_wake(ar);
Kalle Valoe01ae682013-09-01 11:22:14 +03002635 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002636 ath10k_err(ar, "failed to wake up: %d\n", ret);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002637 goto err_release;
Kalle Valoe01ae682013-09-01 11:22:14 +03002638 }
2639
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002640 ret = ath10k_pci_alloc_pipes(ar);
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002641 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002642 ath10k_err(ar, "failed to allocate copy engine pipes: %d\n",
2643 ret);
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002644 goto err_sleep;
Michal Kazior25d0dbc2014-03-28 10:02:38 +02002645 }
2646
Michal Kazior403d6272014-08-22 14:23:31 +02002647 ath10k_pci_ce_deinit(ar);
Michal Kazior7c0f0e32014-10-20 14:14:38 +02002648 ath10k_pci_irq_disable(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02002649
Michal Kazior403d6272014-08-22 14:23:31 +02002650 ret = ath10k_pci_init_irq(ar);
2651 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002652 ath10k_err(ar, "failed to init irqs: %d\n", ret);
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002653 goto err_free_pipes;
Michal Kazior403d6272014-08-22 14:23:31 +02002654 }
2655
Michal Kazior7aa7a722014-08-25 12:09:38 +02002656 ath10k_info(ar, "pci irq %s interrupts %d irq_mode %d reset_mode %d\n",
Michal Kazior403d6272014-08-22 14:23:31 +02002657 ath10k_pci_get_irq_method(ar), ar_pci->num_msi_intrs,
2658 ath10k_pci_irq_mode, ath10k_pci_reset_mode);
2659
Michal Kazior5c771e72014-08-22 14:23:34 +02002660 ret = ath10k_pci_request_irq(ar);
Michal Kazior403d6272014-08-22 14:23:31 +02002661 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002662 ath10k_warn(ar, "failed to request irqs: %d\n", ret);
Michal Kazior403d6272014-08-22 14:23:31 +02002663 goto err_deinit_irq;
2664 }
2665
Michal Kazior1a7fecb2015-01-24 12:14:48 +02002666 ret = ath10k_pci_chip_reset(ar);
2667 if (ret) {
2668 ath10k_err(ar, "failed to reset chip: %d\n", ret);
2669 goto err_free_irq;
2670 }
2671
2672 chip_id = ath10k_pci_soc_read32(ar, SOC_CHIP_ID_ADDRESS);
2673 if (chip_id == 0xffffffff) {
2674 ath10k_err(ar, "failed to get chip id\n");
2675 goto err_free_irq;
2676 }
2677
2678 if (!ath10k_pci_chip_is_supported(pdev->device, chip_id)) {
2679 ath10k_err(ar, "device %04x with chip_id %08x isn't supported\n",
2680 pdev->device, chip_id);
2681 goto err_sleep;
2682 }
2683
Bartosz Markowski707b1bbd2014-10-31 09:03:43 +01002684 ath10k_pci_sleep(ar);
2685
Kalle Valoe01ae682013-09-01 11:22:14 +03002686 ret = ath10k_core_register(ar, chip_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002687 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002688 ath10k_err(ar, "failed to register driver core: %d\n", ret);
Michal Kazior5c771e72014-08-22 14:23:34 +02002689 goto err_free_irq;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002690 }
2691
2692 return 0;
2693
Michal Kazior5c771e72014-08-22 14:23:34 +02002694err_free_irq:
2695 ath10k_pci_free_irq(ar);
Michal Kazior21396272014-08-28 10:24:40 +02002696 ath10k_pci_kill_tasklet(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02002697
Michal Kazior403d6272014-08-22 14:23:31 +02002698err_deinit_irq:
2699 ath10k_pci_deinit_irq(ar);
2700
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002701err_free_pipes:
2702 ath10k_pci_free_pipes(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002703
Michal Kaziorc0c378f2014-08-07 11:03:28 +02002704err_sleep:
2705 ath10k_pci_sleep(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002706
2707err_release:
2708 ath10k_pci_release(ar);
2709
Michal Kaziore7b54192014-08-07 11:03:27 +02002710err_core_destroy:
Kalle Valo5e3dd152013-06-12 20:52:10 +03002711 ath10k_core_destroy(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002712
2713 return ret;
2714}
2715
2716static void ath10k_pci_remove(struct pci_dev *pdev)
2717{
2718 struct ath10k *ar = pci_get_drvdata(pdev);
2719 struct ath10k_pci *ar_pci;
2720
Michal Kazior7aa7a722014-08-25 12:09:38 +02002721 ath10k_dbg(ar, ATH10K_DBG_PCI, "pci remove\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002722
2723 if (!ar)
2724 return;
2725
2726 ar_pci = ath10k_pci_priv(ar);
2727
2728 if (!ar_pci)
2729 return;
2730
Kalle Valo5e3dd152013-06-12 20:52:10 +03002731 ath10k_core_unregister(ar);
Michal Kazior5c771e72014-08-22 14:23:34 +02002732 ath10k_pci_free_irq(ar);
Michal Kazior21396272014-08-28 10:24:40 +02002733 ath10k_pci_kill_tasklet(ar);
Michal Kazior403d6272014-08-22 14:23:31 +02002734 ath10k_pci_deinit_irq(ar);
2735 ath10k_pci_ce_deinit(ar);
Michal Kazior84cbf3a2014-10-20 14:14:39 +02002736 ath10k_pci_free_pipes(ar);
Michal Kazior2986e3e2014-08-07 11:03:30 +02002737 ath10k_pci_release(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002738 ath10k_core_destroy(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002739}
2740
Kalle Valo5e3dd152013-06-12 20:52:10 +03002741MODULE_DEVICE_TABLE(pci, ath10k_pci_id_table);
2742
2743static struct pci_driver ath10k_pci_driver = {
2744 .name = "ath10k_pci",
2745 .id_table = ath10k_pci_id_table,
2746 .probe = ath10k_pci_probe,
2747 .remove = ath10k_pci_remove,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002748};
2749
2750static int __init ath10k_pci_init(void)
2751{
2752 int ret;
2753
2754 ret = pci_register_driver(&ath10k_pci_driver);
2755 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002756 printk(KERN_ERR "failed to register ath10k pci driver: %d\n",
2757 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002758
2759 return ret;
2760}
2761module_init(ath10k_pci_init);
2762
2763static void __exit ath10k_pci_exit(void)
2764{
2765 pci_unregister_driver(&ath10k_pci_driver);
2766}
2767
2768module_exit(ath10k_pci_exit);
2769
2770MODULE_AUTHOR("Qualcomm Atheros");
2771MODULE_DESCRIPTION("Driver support for Atheros QCA988X PCIe devices");
2772MODULE_LICENSE("Dual BSD/GPL");
Bartosz Markowski8026cae2014-10-06 14:16:41 +02002773MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_FW_FILE);
2774MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API2_FILE);
2775MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API3_FILE);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002776MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_BOARD_DATA_FILE);