blob: 24b1927a075183571828143051354d34dc24ee52 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: ISC
2/*
3 * Copyright (c) 2004-2011 Atheros Communications Inc.
4 * Copyright (c) 2011-2012,2017 Qualcomm Atheros, Inc.
5 * Copyright (c) 2016-2017 Erik Stromdahl <erik.stromdahl@gmail.com>
6 */
7
8#include <linux/module.h>
9#include <linux/mmc/card.h>
10#include <linux/mmc/mmc.h>
11#include <linux/mmc/host.h>
12#include <linux/mmc/sdio_func.h>
13#include <linux/mmc/sdio_ids.h>
14#include <linux/mmc/sdio.h>
15#include <linux/mmc/sd.h>
16#include <linux/bitfield.h>
17#include "core.h"
18#include "bmi.h"
19#include "debug.h"
20#include "hif.h"
21#include "htc.h"
22#include "mac.h"
23#include "targaddrs.h"
24#include "trace.h"
25#include "sdio.h"
26
27/* inlined helper functions */
28
29static inline int ath10k_sdio_calc_txrx_padded_len(struct ath10k_sdio *ar_sdio,
30 size_t len)
31{
32 return __ALIGN_MASK((len), ar_sdio->mbox_info.block_mask);
33}
34
35static inline enum ath10k_htc_ep_id pipe_id_to_eid(u8 pipe_id)
36{
37 return (enum ath10k_htc_ep_id)pipe_id;
38}
39
40static inline void ath10k_sdio_mbox_free_rx_pkt(struct ath10k_sdio_rx_data *pkt)
41{
42 dev_kfree_skb(pkt->skb);
43 pkt->skb = NULL;
44 pkt->alloc_len = 0;
45 pkt->act_len = 0;
46 pkt->trailer_only = false;
47}
48
49static inline int ath10k_sdio_mbox_alloc_rx_pkt(struct ath10k_sdio_rx_data *pkt,
50 size_t act_len, size_t full_len,
51 bool part_of_bundle,
52 bool last_in_bundle)
53{
54 pkt->skb = dev_alloc_skb(full_len);
55 if (!pkt->skb)
56 return -ENOMEM;
57
58 pkt->act_len = act_len;
59 pkt->alloc_len = full_len;
60 pkt->part_of_bundle = part_of_bundle;
61 pkt->last_in_bundle = last_in_bundle;
62 pkt->trailer_only = false;
63
64 return 0;
65}
66
67static inline bool is_trailer_only_msg(struct ath10k_sdio_rx_data *pkt)
68{
69 bool trailer_only = false;
70 struct ath10k_htc_hdr *htc_hdr =
71 (struct ath10k_htc_hdr *)pkt->skb->data;
72 u16 len = __le16_to_cpu(htc_hdr->len);
73
74 if (len == htc_hdr->trailer_len)
75 trailer_only = true;
76
77 return trailer_only;
78}
79
80/* sdio/mmc functions */
81
82static inline void ath10k_sdio_set_cmd52_arg(u32 *arg, u8 write, u8 raw,
83 unsigned int address,
84 unsigned char val)
85{
86 *arg = FIELD_PREP(BIT(31), write) |
87 FIELD_PREP(BIT(27), raw) |
88 FIELD_PREP(BIT(26), 1) |
89 FIELD_PREP(GENMASK(25, 9), address) |
90 FIELD_PREP(BIT(8), 1) |
91 FIELD_PREP(GENMASK(7, 0), val);
92}
93
94static int ath10k_sdio_func0_cmd52_wr_byte(struct mmc_card *card,
95 unsigned int address,
96 unsigned char byte)
97{
98 struct mmc_command io_cmd;
99
100 memset(&io_cmd, 0, sizeof(io_cmd));
101 ath10k_sdio_set_cmd52_arg(&io_cmd.arg, 1, 0, address, byte);
102 io_cmd.opcode = SD_IO_RW_DIRECT;
103 io_cmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
104
105 return mmc_wait_for_cmd(card->host, &io_cmd, 0);
106}
107
108static int ath10k_sdio_func0_cmd52_rd_byte(struct mmc_card *card,
109 unsigned int address,
110 unsigned char *byte)
111{
112 struct mmc_command io_cmd;
113 int ret;
114
115 memset(&io_cmd, 0, sizeof(io_cmd));
116 ath10k_sdio_set_cmd52_arg(&io_cmd.arg, 0, 0, address, 0);
117 io_cmd.opcode = SD_IO_RW_DIRECT;
118 io_cmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
119
120 ret = mmc_wait_for_cmd(card->host, &io_cmd, 0);
121 if (!ret)
122 *byte = io_cmd.resp[0];
123
124 return ret;
125}
126
127static int ath10k_sdio_config(struct ath10k *ar)
128{
129 struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
130 struct sdio_func *func = ar_sdio->func;
131 unsigned char byte, asyncintdelay = 2;
132 int ret;
133
134 ath10k_dbg(ar, ATH10K_DBG_BOOT, "sdio configuration\n");
135
136 sdio_claim_host(func);
137
138 byte = 0;
139 ret = ath10k_sdio_func0_cmd52_rd_byte(func->card,
140 SDIO_CCCR_DRIVE_STRENGTH,
141 &byte);
142
143 byte &= ~ATH10K_SDIO_DRIVE_DTSX_MASK;
144 byte |= FIELD_PREP(ATH10K_SDIO_DRIVE_DTSX_MASK,
145 ATH10K_SDIO_DRIVE_DTSX_TYPE_D);
146
147 ret = ath10k_sdio_func0_cmd52_wr_byte(func->card,
148 SDIO_CCCR_DRIVE_STRENGTH,
149 byte);
150
151 byte = 0;
152 ret = ath10k_sdio_func0_cmd52_rd_byte(
153 func->card,
154 CCCR_SDIO_DRIVER_STRENGTH_ENABLE_ADDR,
155 &byte);
156
157 byte |= (CCCR_SDIO_DRIVER_STRENGTH_ENABLE_A |
158 CCCR_SDIO_DRIVER_STRENGTH_ENABLE_C |
159 CCCR_SDIO_DRIVER_STRENGTH_ENABLE_D);
160
161 ret = ath10k_sdio_func0_cmd52_wr_byte(func->card,
162 CCCR_SDIO_DRIVER_STRENGTH_ENABLE_ADDR,
163 byte);
164 if (ret) {
165 ath10k_warn(ar, "failed to enable driver strength: %d\n", ret);
166 goto out;
167 }
168
169 byte = 0;
170 ret = ath10k_sdio_func0_cmd52_rd_byte(func->card,
171 CCCR_SDIO_IRQ_MODE_REG_SDIO3,
172 &byte);
173
174 byte |= SDIO_IRQ_MODE_ASYNC_4BIT_IRQ_SDIO3;
175
176 ret = ath10k_sdio_func0_cmd52_wr_byte(func->card,
177 CCCR_SDIO_IRQ_MODE_REG_SDIO3,
178 byte);
179 if (ret) {
180 ath10k_warn(ar, "failed to enable 4-bit async irq mode: %d\n",
181 ret);
182 goto out;
183 }
184
185 byte = 0;
186 ret = ath10k_sdio_func0_cmd52_rd_byte(func->card,
187 CCCR_SDIO_ASYNC_INT_DELAY_ADDRESS,
188 &byte);
189
190 byte &= ~CCCR_SDIO_ASYNC_INT_DELAY_MASK;
191 byte |= FIELD_PREP(CCCR_SDIO_ASYNC_INT_DELAY_MASK, asyncintdelay);
192
193 ret = ath10k_sdio_func0_cmd52_wr_byte(func->card,
194 CCCR_SDIO_ASYNC_INT_DELAY_ADDRESS,
195 byte);
196
197 /* give us some time to enable, in ms */
198 func->enable_timeout = 100;
199
200 ret = sdio_set_block_size(func, ar_sdio->mbox_info.block_size);
201 if (ret) {
202 ath10k_warn(ar, "failed to set sdio block size to %d: %d\n",
203 ar_sdio->mbox_info.block_size, ret);
204 goto out;
205 }
206
207out:
208 sdio_release_host(func);
209 return ret;
210}
211
212static int ath10k_sdio_write32(struct ath10k *ar, u32 addr, u32 val)
213{
214 struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
215 struct sdio_func *func = ar_sdio->func;
216 int ret;
217
218 sdio_claim_host(func);
219
220 sdio_writel(func, val, addr, &ret);
221 if (ret) {
222 ath10k_warn(ar, "failed to write 0x%x to address 0x%x: %d\n",
223 val, addr, ret);
224 goto out;
225 }
226
227 ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio write32 addr 0x%x val 0x%x\n",
228 addr, val);
229
230out:
231 sdio_release_host(func);
232
233 return ret;
234}
235
236static int ath10k_sdio_writesb32(struct ath10k *ar, u32 addr, u32 val)
237{
238 struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
239 struct sdio_func *func = ar_sdio->func;
240 __le32 *buf;
241 int ret;
242
243 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
244 if (!buf)
245 return -ENOMEM;
246
247 *buf = cpu_to_le32(val);
248
249 sdio_claim_host(func);
250
251 ret = sdio_writesb(func, addr, buf, sizeof(*buf));
252 if (ret) {
253 ath10k_warn(ar, "failed to write value 0x%x to fixed sb address 0x%x: %d\n",
254 val, addr, ret);
255 goto out;
256 }
257
258 ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio writesb32 addr 0x%x val 0x%x\n",
259 addr, val);
260
261out:
262 sdio_release_host(func);
263
264 kfree(buf);
265
266 return ret;
267}
268
269static int ath10k_sdio_read32(struct ath10k *ar, u32 addr, u32 *val)
270{
271 struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
272 struct sdio_func *func = ar_sdio->func;
273 int ret;
274
275 sdio_claim_host(func);
276 *val = sdio_readl(func, addr, &ret);
277 if (ret) {
278 ath10k_warn(ar, "failed to read from address 0x%x: %d\n",
279 addr, ret);
280 goto out;
281 }
282
283 ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio read32 addr 0x%x val 0x%x\n",
284 addr, *val);
285
286out:
287 sdio_release_host(func);
288
289 return ret;
290}
291
292static int ath10k_sdio_read(struct ath10k *ar, u32 addr, void *buf, size_t len)
293{
294 struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
295 struct sdio_func *func = ar_sdio->func;
296 int ret;
297
298 sdio_claim_host(func);
299
300 ret = sdio_memcpy_fromio(func, buf, addr, len);
301 if (ret) {
302 ath10k_warn(ar, "failed to read from address 0x%x: %d\n",
303 addr, ret);
304 goto out;
305 }
306
307 ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio read addr 0x%x buf 0x%p len %zu\n",
308 addr, buf, len);
309 ath10k_dbg_dump(ar, ATH10K_DBG_SDIO_DUMP, NULL, "sdio read ", buf, len);
310
311out:
312 sdio_release_host(func);
313
314 return ret;
315}
316
317static int ath10k_sdio_write(struct ath10k *ar, u32 addr, const void *buf, size_t len)
318{
319 struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
320 struct sdio_func *func = ar_sdio->func;
321 int ret;
322
323 sdio_claim_host(func);
324
325 /* For some reason toio() doesn't have const for the buffer, need
326 * an ugly hack to workaround that.
327 */
328 ret = sdio_memcpy_toio(func, addr, (void *)buf, len);
329 if (ret) {
330 ath10k_warn(ar, "failed to write to address 0x%x: %d\n",
331 addr, ret);
332 goto out;
333 }
334
335 ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio write addr 0x%x buf 0x%p len %zu\n",
336 addr, buf, len);
337 ath10k_dbg_dump(ar, ATH10K_DBG_SDIO_DUMP, NULL, "sdio write ", buf, len);
338
339out:
340 sdio_release_host(func);
341
342 return ret;
343}
344
345static int ath10k_sdio_readsb(struct ath10k *ar, u32 addr, void *buf, size_t len)
346{
347 struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
348 struct sdio_func *func = ar_sdio->func;
349 int ret;
350
351 sdio_claim_host(func);
352
353 len = round_down(len, ar_sdio->mbox_info.block_size);
354
355 ret = sdio_readsb(func, buf, addr, len);
356 if (ret) {
357 ath10k_warn(ar, "failed to read from fixed (sb) address 0x%x: %d\n",
358 addr, ret);
359 goto out;
360 }
361
362 ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio readsb addr 0x%x buf 0x%p len %zu\n",
363 addr, buf, len);
364 ath10k_dbg_dump(ar, ATH10K_DBG_SDIO_DUMP, NULL, "sdio readsb ", buf, len);
365
366out:
367 sdio_release_host(func);
368
369 return ret;
370}
371
372/* HIF mbox functions */
373
374static int ath10k_sdio_mbox_rx_process_packet(struct ath10k *ar,
375 struct ath10k_sdio_rx_data *pkt,
376 u32 *lookaheads,
377 int *n_lookaheads)
378{
379 struct ath10k_htc *htc = &ar->htc;
380 struct sk_buff *skb = pkt->skb;
381 struct ath10k_htc_hdr *htc_hdr = (struct ath10k_htc_hdr *)skb->data;
382 bool trailer_present = htc_hdr->flags & ATH10K_HTC_FLAG_TRAILER_PRESENT;
383 enum ath10k_htc_ep_id eid;
384 u8 *trailer;
385 int ret;
386
387 if (trailer_present) {
388 trailer = skb->data + skb->len - htc_hdr->trailer_len;
389
390 eid = pipe_id_to_eid(htc_hdr->eid);
391
392 ret = ath10k_htc_process_trailer(htc,
393 trailer,
394 htc_hdr->trailer_len,
395 eid,
396 lookaheads,
397 n_lookaheads);
398 if (ret)
399 return ret;
400
401 if (is_trailer_only_msg(pkt))
402 pkt->trailer_only = true;
403
404 skb_trim(skb, skb->len - htc_hdr->trailer_len);
405 }
406
407 skb_pull(skb, sizeof(*htc_hdr));
408
409 return 0;
410}
411
412static int ath10k_sdio_mbox_rx_process_packets(struct ath10k *ar,
413 u32 lookaheads[],
414 int *n_lookahead)
415{
416 struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
417 struct ath10k_htc *htc = &ar->htc;
418 struct ath10k_sdio_rx_data *pkt;
419 struct ath10k_htc_ep *ep;
420 enum ath10k_htc_ep_id id;
421 int ret, i, *n_lookahead_local;
422 u32 *lookaheads_local;
423 int lookahead_idx = 0;
424
425 for (i = 0; i < ar_sdio->n_rx_pkts; i++) {
426 lookaheads_local = lookaheads;
427 n_lookahead_local = n_lookahead;
428
429 id = ((struct ath10k_htc_hdr *)
430 &lookaheads[lookahead_idx++])->eid;
431
432 if (id >= ATH10K_HTC_EP_COUNT) {
433 ath10k_warn(ar, "invalid endpoint in look-ahead: %d\n",
434 id);
435 ret = -ENOMEM;
436 goto out;
437 }
438
439 ep = &htc->endpoint[id];
440
441 if (ep->service_id == 0) {
442 ath10k_warn(ar, "ep %d is not connected\n", id);
443 ret = -ENOMEM;
444 goto out;
445 }
446
447 pkt = &ar_sdio->rx_pkts[i];
448
449 if (pkt->part_of_bundle && !pkt->last_in_bundle) {
450 /* Only read lookahead's from RX trailers
451 * for the last packet in a bundle.
452 */
453 lookahead_idx--;
454 lookaheads_local = NULL;
455 n_lookahead_local = NULL;
456 }
457
458 ret = ath10k_sdio_mbox_rx_process_packet(ar,
459 pkt,
460 lookaheads_local,
461 n_lookahead_local);
462 if (ret)
463 goto out;
464
465 if (!pkt->trailer_only)
466 ep->ep_ops.ep_rx_complete(ar_sdio->ar, pkt->skb);
467 else
468 kfree_skb(pkt->skb);
469
470 /* The RX complete handler now owns the skb...*/
471 pkt->skb = NULL;
472 pkt->alloc_len = 0;
473 }
474
475 ret = 0;
476
477out:
478 /* Free all packets that was not passed on to the RX completion
479 * handler...
480 */
481 for (; i < ar_sdio->n_rx_pkts; i++)
482 ath10k_sdio_mbox_free_rx_pkt(&ar_sdio->rx_pkts[i]);
483
484 return ret;
485}
486
487static int ath10k_sdio_mbox_alloc_pkt_bundle(struct ath10k *ar,
488 struct ath10k_sdio_rx_data *rx_pkts,
489 struct ath10k_htc_hdr *htc_hdr,
490 size_t full_len, size_t act_len,
491 size_t *bndl_cnt)
492{
493 int ret, i;
494
495 *bndl_cnt = FIELD_GET(ATH10K_HTC_FLAG_BUNDLE_MASK, htc_hdr->flags);
496
497 if (*bndl_cnt > HTC_HOST_MAX_MSG_PER_RX_BUNDLE) {
498 ath10k_warn(ar,
499 "HTC bundle length %u exceeds maximum %u\n",
500 le16_to_cpu(htc_hdr->len),
501 HTC_HOST_MAX_MSG_PER_RX_BUNDLE);
502 return -ENOMEM;
503 }
504
505 /* Allocate bndl_cnt extra skb's for the bundle.
506 * The package containing the
507 * ATH10K_HTC_FLAG_BUNDLE_MASK flag is not included
508 * in bndl_cnt. The skb for that packet will be
509 * allocated separately.
510 */
511 for (i = 0; i < *bndl_cnt; i++) {
512 ret = ath10k_sdio_mbox_alloc_rx_pkt(&rx_pkts[i],
513 act_len,
514 full_len,
515 true,
516 false);
517 if (ret)
518 return ret;
519 }
520
521 return 0;
522}
523
524static int ath10k_sdio_mbox_rx_alloc(struct ath10k *ar,
525 u32 lookaheads[], int n_lookaheads)
526{
527 struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
528 struct ath10k_htc_hdr *htc_hdr;
529 size_t full_len, act_len;
530 bool last_in_bundle;
531 int ret, i;
532
533 if (n_lookaheads > ATH10K_SDIO_MAX_RX_MSGS) {
534 ath10k_warn(ar,
535 "the total number of pkgs to be fetched (%u) exceeds maximum %u\n",
536 n_lookaheads,
537 ATH10K_SDIO_MAX_RX_MSGS);
538 ret = -ENOMEM;
539 goto err;
540 }
541
542 for (i = 0; i < n_lookaheads; i++) {
543 htc_hdr = (struct ath10k_htc_hdr *)&lookaheads[i];
544 last_in_bundle = false;
545
546 if (le16_to_cpu(htc_hdr->len) >
547 ATH10K_HTC_MBOX_MAX_PAYLOAD_LENGTH) {
548 ath10k_warn(ar,
549 "payload length %d exceeds max htc length: %zu\n",
550 le16_to_cpu(htc_hdr->len),
551 ATH10K_HTC_MBOX_MAX_PAYLOAD_LENGTH);
552 ret = -ENOMEM;
553
554 queue_work(ar->workqueue, &ar->restart_work);
555 ath10k_warn(ar, "exceeds length, start recovery\n");
556
557 goto err;
558 }
559
560 act_len = le16_to_cpu(htc_hdr->len) + sizeof(*htc_hdr);
561 full_len = ath10k_sdio_calc_txrx_padded_len(ar_sdio, act_len);
562
563 if (full_len > ATH10K_SDIO_MAX_BUFFER_SIZE) {
564 ath10k_warn(ar,
565 "rx buffer requested with invalid htc_hdr length (%d, 0x%x): %d\n",
566 htc_hdr->eid, htc_hdr->flags,
567 le16_to_cpu(htc_hdr->len));
568 ret = -EINVAL;
569 goto err;
570 }
571
572 if (htc_hdr->flags & ATH10K_HTC_FLAG_BUNDLE_MASK) {
573 /* HTC header indicates that every packet to follow
574 * has the same padded length so that it can be
575 * optimally fetched as a full bundle.
576 */
577 size_t bndl_cnt;
578
579 ret = ath10k_sdio_mbox_alloc_pkt_bundle(ar,
580 &ar_sdio->rx_pkts[i],
581 htc_hdr,
582 full_len,
583 act_len,
584 &bndl_cnt);
585
586 if (ret) {
587 ath10k_warn(ar, "alloc_bundle error %d\n", ret);
588 goto err;
589 }
590
591 n_lookaheads += bndl_cnt;
592 i += bndl_cnt;
593 /*Next buffer will be the last in the bundle */
594 last_in_bundle = true;
595 }
596
597 /* Allocate skb for packet. If the packet had the
598 * ATH10K_HTC_FLAG_BUNDLE_MASK flag set, all bundled
599 * packet skb's have been allocated in the previous step.
600 */
601 if (htc_hdr->flags & ATH10K_HTC_FLAGS_RECV_1MORE_BLOCK)
602 full_len += ATH10K_HIF_MBOX_BLOCK_SIZE;
603
604 ret = ath10k_sdio_mbox_alloc_rx_pkt(&ar_sdio->rx_pkts[i],
605 act_len,
606 full_len,
607 last_in_bundle,
608 last_in_bundle);
609 if (ret) {
610 ath10k_warn(ar, "alloc_rx_pkt error %d\n", ret);
611 goto err;
612 }
613 }
614
615 ar_sdio->n_rx_pkts = i;
616
617 return 0;
618
619err:
620 for (i = 0; i < ATH10K_SDIO_MAX_RX_MSGS; i++) {
621 if (!ar_sdio->rx_pkts[i].alloc_len)
622 break;
623 ath10k_sdio_mbox_free_rx_pkt(&ar_sdio->rx_pkts[i]);
624 }
625
626 return ret;
627}
628
629static int ath10k_sdio_mbox_rx_packet(struct ath10k *ar,
630 struct ath10k_sdio_rx_data *pkt)
631{
632 struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
633 struct sk_buff *skb = pkt->skb;
634 struct ath10k_htc_hdr *htc_hdr;
635 int ret;
636
637 ret = ath10k_sdio_readsb(ar, ar_sdio->mbox_info.htc_addr,
638 skb->data, pkt->alloc_len);
639 if (ret)
640 goto out;
641
642 /* Update actual length. The original length may be incorrect,
643 * as the FW will bundle multiple packets as long as their sizes
644 * fit within the same aligned length (pkt->alloc_len).
645 */
646 htc_hdr = (struct ath10k_htc_hdr *)skb->data;
647 pkt->act_len = le16_to_cpu(htc_hdr->len) + sizeof(*htc_hdr);
648 if (pkt->act_len > pkt->alloc_len) {
649 ath10k_warn(ar, "rx packet too large (%zu > %zu)\n",
650 pkt->act_len, pkt->alloc_len);
651 ret = -EMSGSIZE;
652 goto out;
653 }
654
655 skb_put(skb, pkt->act_len);
656
657out:
658 pkt->status = ret;
659
660 return ret;
661}
662
663static int ath10k_sdio_mbox_rx_fetch(struct ath10k *ar)
664{
665 struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
666 int ret, i;
667
668 for (i = 0; i < ar_sdio->n_rx_pkts; i++) {
669 ret = ath10k_sdio_mbox_rx_packet(ar,
670 &ar_sdio->rx_pkts[i]);
671 if (ret)
672 goto err;
673 }
674
675 return 0;
676
677err:
678 /* Free all packets that was not successfully fetched. */
679 for (; i < ar_sdio->n_rx_pkts; i++)
680 ath10k_sdio_mbox_free_rx_pkt(&ar_sdio->rx_pkts[i]);
681
682 return ret;
683}
684
685/* This is the timeout for mailbox processing done in the sdio irq
686 * handler. The timeout is deliberately set quite high since SDIO dump logs
687 * over serial port can/will add a substantial overhead to the processing
688 * (if enabled).
689 */
690#define SDIO_MBOX_PROCESSING_TIMEOUT_HZ (20 * HZ)
691
692static int ath10k_sdio_mbox_rxmsg_pending_handler(struct ath10k *ar,
693 u32 msg_lookahead, bool *done)
694{
695 struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
696 u32 lookaheads[ATH10K_SDIO_MAX_RX_MSGS];
697 int n_lookaheads = 1;
698 unsigned long timeout;
699 int ret;
700
701 *done = true;
702
703 /* Copy the lookahead obtained from the HTC register table into our
704 * temp array as a start value.
705 */
706 lookaheads[0] = msg_lookahead;
707
708 timeout = jiffies + SDIO_MBOX_PROCESSING_TIMEOUT_HZ;
709 do {
710 /* Try to allocate as many HTC RX packets indicated by
711 * n_lookaheads.
712 */
713 ret = ath10k_sdio_mbox_rx_alloc(ar, lookaheads,
714 n_lookaheads);
715 if (ret)
716 break;
717
718 if (ar_sdio->n_rx_pkts >= 2)
719 /* A recv bundle was detected, force IRQ status
720 * re-check again.
721 */
722 *done = false;
723
724 ret = ath10k_sdio_mbox_rx_fetch(ar);
725
726 /* Process fetched packets. This will potentially update
727 * n_lookaheads depending on if the packets contain lookahead
728 * reports.
729 */
730 n_lookaheads = 0;
731 ret = ath10k_sdio_mbox_rx_process_packets(ar,
732 lookaheads,
733 &n_lookaheads);
734
735 if (!n_lookaheads || ret)
736 break;
737
738 /* For SYNCH processing, if we get here, we are running
739 * through the loop again due to updated lookaheads. Set
740 * flag that we should re-check IRQ status registers again
741 * before leaving IRQ processing, this can net better
742 * performance in high throughput situations.
743 */
744 *done = false;
745 } while (time_before(jiffies, timeout));
746
747 if (ret && (ret != -ECANCELED))
748 ath10k_warn(ar, "failed to get pending recv messages: %d\n",
749 ret);
750
751 return ret;
752}
753
754static int ath10k_sdio_mbox_proc_dbg_intr(struct ath10k *ar)
755{
756 u32 val;
757 int ret;
758
759 /* TODO: Add firmware crash handling */
760 ath10k_warn(ar, "firmware crashed\n");
761
762 /* read counter to clear the interrupt, the debug error interrupt is
763 * counter 0.
764 */
765 ret = ath10k_sdio_read32(ar, MBOX_COUNT_DEC_ADDRESS, &val);
766 if (ret)
767 ath10k_warn(ar, "failed to clear debug interrupt: %d\n", ret);
768
769 return ret;
770}
771
772static int ath10k_sdio_mbox_proc_counter_intr(struct ath10k *ar)
773{
774 struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
775 struct ath10k_sdio_irq_data *irq_data = &ar_sdio->irq_data;
776 u8 counter_int_status;
777 int ret;
778
779 mutex_lock(&irq_data->mtx);
780 counter_int_status = irq_data->irq_proc_reg->counter_int_status &
781 irq_data->irq_en_reg->cntr_int_status_en;
782
783 /* NOTE: other modules like GMBOX may use the counter interrupt for
784 * credit flow control on other counters, we only need to check for
785 * the debug assertion counter interrupt.
786 */
787 if (counter_int_status & ATH10K_SDIO_TARGET_DEBUG_INTR_MASK)
788 ret = ath10k_sdio_mbox_proc_dbg_intr(ar);
789 else
790 ret = 0;
791
792 mutex_unlock(&irq_data->mtx);
793
794 return ret;
795}
796
797static int ath10k_sdio_mbox_proc_err_intr(struct ath10k *ar)
798{
799 struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
800 struct ath10k_sdio_irq_data *irq_data = &ar_sdio->irq_data;
801 u8 error_int_status;
802 int ret;
803
804 ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio error interrupt\n");
805
806 error_int_status = irq_data->irq_proc_reg->error_int_status & 0x0F;
807 if (!error_int_status) {
808 ath10k_warn(ar, "invalid error interrupt status: 0x%x\n",
809 error_int_status);
810 return -EIO;
811 }
812
813 ath10k_dbg(ar, ATH10K_DBG_SDIO,
814 "sdio error_int_status 0x%x\n", error_int_status);
815
816 if (FIELD_GET(MBOX_ERROR_INT_STATUS_WAKEUP_MASK,
817 error_int_status))
818 ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio interrupt error wakeup\n");
819
820 if (FIELD_GET(MBOX_ERROR_INT_STATUS_RX_UNDERFLOW_MASK,
821 error_int_status))
822 ath10k_warn(ar, "rx underflow interrupt error\n");
823
824 if (FIELD_GET(MBOX_ERROR_INT_STATUS_TX_OVERFLOW_MASK,
825 error_int_status))
826 ath10k_warn(ar, "tx overflow interrupt error\n");
827
828 /* Clear the interrupt */
829 irq_data->irq_proc_reg->error_int_status &= ~error_int_status;
830
831 /* set W1C value to clear the interrupt, this hits the register first */
832 ret = ath10k_sdio_writesb32(ar, MBOX_ERROR_INT_STATUS_ADDRESS,
833 error_int_status);
834 if (ret) {
835 ath10k_warn(ar, "unable to write to error int status address: %d\n",
836 ret);
837 return ret;
838 }
839
840 return 0;
841}
842
843static int ath10k_sdio_mbox_proc_cpu_intr(struct ath10k *ar)
844{
845 struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
846 struct ath10k_sdio_irq_data *irq_data = &ar_sdio->irq_data;
847 u8 cpu_int_status;
848 int ret;
849
850 mutex_lock(&irq_data->mtx);
851 cpu_int_status = irq_data->irq_proc_reg->cpu_int_status &
852 irq_data->irq_en_reg->cpu_int_status_en;
853 if (!cpu_int_status) {
854 ath10k_warn(ar, "CPU interrupt status is zero\n");
855 ret = -EIO;
856 goto out;
857 }
858
859 /* Clear the interrupt */
860 irq_data->irq_proc_reg->cpu_int_status &= ~cpu_int_status;
861
862 /* Set up the register transfer buffer to hit the register 4 times,
863 * this is done to make the access 4-byte aligned to mitigate issues
864 * with host bus interconnects that restrict bus transfer lengths to
865 * be a multiple of 4-bytes.
866 *
867 * Set W1C value to clear the interrupt, this hits the register first.
868 */
869 ret = ath10k_sdio_writesb32(ar, MBOX_CPU_INT_STATUS_ADDRESS,
870 cpu_int_status);
871 if (ret) {
872 ath10k_warn(ar, "unable to write to cpu interrupt status address: %d\n",
873 ret);
874 goto out;
875 }
876
877out:
878 mutex_unlock(&irq_data->mtx);
879 if (cpu_int_status & MBOX_CPU_STATUS_ENABLE_ASSERT_MASK) {
880 ath10k_err(ar, "firmware crashed!\n");
881 queue_work(ar->workqueue, &ar->restart_work);
882 }
883 return ret;
884}
885
886static int ath10k_sdio_mbox_read_int_status(struct ath10k *ar,
887 u8 *host_int_status,
888 u32 *lookahead)
889{
890 struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
891 struct ath10k_sdio_irq_data *irq_data = &ar_sdio->irq_data;
892 struct ath10k_sdio_irq_proc_regs *irq_proc_reg = irq_data->irq_proc_reg;
893 struct ath10k_sdio_irq_enable_regs *irq_en_reg = irq_data->irq_en_reg;
894 u8 htc_mbox = FIELD_PREP(ATH10K_HTC_MAILBOX_MASK, 1);
895 int ret;
896
897 mutex_lock(&irq_data->mtx);
898
899 *lookahead = 0;
900 *host_int_status = 0;
901
902 /* int_status_en is supposed to be non zero, otherwise interrupts
903 * shouldn't be enabled. There is however a short time frame during
904 * initialization between the irq register and int_status_en init
905 * where this can happen.
906 * We silently ignore this condition.
907 */
908 if (!irq_en_reg->int_status_en) {
909 ret = 0;
910 goto out;
911 }
912
913 /* Read the first sizeof(struct ath10k_irq_proc_registers)
914 * bytes of the HTC register table. This
915 * will yield us the value of different int status
916 * registers and the lookahead registers.
917 */
918 ret = ath10k_sdio_read(ar, MBOX_HOST_INT_STATUS_ADDRESS,
919 irq_proc_reg, sizeof(*irq_proc_reg));
920 if (ret)
921 goto out;
922
923 /* Update only those registers that are enabled */
924 *host_int_status = irq_proc_reg->host_int_status &
925 irq_en_reg->int_status_en;
926
927 /* Look at mbox status */
928 if (!(*host_int_status & htc_mbox)) {
929 *lookahead = 0;
930 ret = 0;
931 goto out;
932 }
933
934 /* Mask out pending mbox value, we use look ahead as
935 * the real flag for mbox processing.
936 */
937 *host_int_status &= ~htc_mbox;
938 if (irq_proc_reg->rx_lookahead_valid & htc_mbox) {
939 *lookahead = le32_to_cpu(
940 irq_proc_reg->rx_lookahead[ATH10K_HTC_MAILBOX]);
941 if (!*lookahead)
942 ath10k_warn(ar, "sdio mbox lookahead is zero\n");
943 }
944
945out:
946 mutex_unlock(&irq_data->mtx);
947 return ret;
948}
949
950static int ath10k_sdio_mbox_proc_pending_irqs(struct ath10k *ar,
951 bool *done)
952{
953 u8 host_int_status;
954 u32 lookahead;
955 int ret;
956
957 /* NOTE: HIF implementation guarantees that the context of this
958 * call allows us to perform SYNCHRONOUS I/O, that is we can block,
959 * sleep or call any API that can block or switch thread/task
960 * contexts. This is a fully schedulable context.
961 */
962
963 ret = ath10k_sdio_mbox_read_int_status(ar,
964 &host_int_status,
965 &lookahead);
966 if (ret) {
967 *done = true;
968 goto out;
969 }
970
971 if (!host_int_status && !lookahead) {
972 ret = 0;
973 *done = true;
974 goto out;
975 }
976
977 if (lookahead) {
978 ath10k_dbg(ar, ATH10K_DBG_SDIO,
979 "sdio pending mailbox msg lookahead 0x%08x\n",
980 lookahead);
981
982 ret = ath10k_sdio_mbox_rxmsg_pending_handler(ar,
983 lookahead,
984 done);
985 if (ret)
986 goto out;
987 }
988
989 /* now, handle the rest of the interrupts */
990 ath10k_dbg(ar, ATH10K_DBG_SDIO,
991 "sdio host_int_status 0x%x\n", host_int_status);
992
993 if (FIELD_GET(MBOX_HOST_INT_STATUS_CPU_MASK, host_int_status)) {
994 /* CPU Interrupt */
995 ret = ath10k_sdio_mbox_proc_cpu_intr(ar);
996 if (ret)
997 goto out;
998 }
999
1000 if (FIELD_GET(MBOX_HOST_INT_STATUS_ERROR_MASK, host_int_status)) {
1001 /* Error Interrupt */
1002 ret = ath10k_sdio_mbox_proc_err_intr(ar);
1003 if (ret)
1004 goto out;
1005 }
1006
1007 if (FIELD_GET(MBOX_HOST_INT_STATUS_COUNTER_MASK, host_int_status))
1008 /* Counter Interrupt */
1009 ret = ath10k_sdio_mbox_proc_counter_intr(ar);
1010
1011 ret = 0;
1012
1013out:
1014 /* An optimization to bypass reading the IRQ status registers
1015 * unecessarily which can re-wake the target, if upper layers
1016 * determine that we are in a low-throughput mode, we can rely on
1017 * taking another interrupt rather than re-checking the status
1018 * registers which can re-wake the target.
1019 *
1020 * NOTE : for host interfaces that makes use of detecting pending
1021 * mbox messages at hif can not use this optimization due to
1022 * possible side effects, SPI requires the host to drain all
1023 * messages from the mailbox before exiting the ISR routine.
1024 */
1025
1026 ath10k_dbg(ar, ATH10K_DBG_SDIO,
1027 "sdio pending irqs done %d status %d",
1028 *done, ret);
1029
1030 return ret;
1031}
1032
1033static void ath10k_sdio_set_mbox_info(struct ath10k *ar)
1034{
1035 struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
1036 struct ath10k_mbox_info *mbox_info = &ar_sdio->mbox_info;
1037 u16 device = ar_sdio->func->device, dev_id_base, dev_id_chiprev;
1038
1039 mbox_info->htc_addr = ATH10K_HIF_MBOX_BASE_ADDR;
1040 mbox_info->block_size = ATH10K_HIF_MBOX_BLOCK_SIZE;
1041 mbox_info->block_mask = ATH10K_HIF_MBOX_BLOCK_SIZE - 1;
1042 mbox_info->gmbox_addr = ATH10K_HIF_GMBOX_BASE_ADDR;
1043 mbox_info->gmbox_sz = ATH10K_HIF_GMBOX_WIDTH;
1044
1045 mbox_info->ext_info[0].htc_ext_addr = ATH10K_HIF_MBOX0_EXT_BASE_ADDR;
1046
1047 dev_id_base = FIELD_GET(QCA_MANUFACTURER_ID_BASE, device);
1048 dev_id_chiprev = FIELD_GET(QCA_MANUFACTURER_ID_REV_MASK, device);
1049 switch (dev_id_base) {
1050 case QCA_MANUFACTURER_ID_AR6005_BASE:
1051 if (dev_id_chiprev < 4)
1052 mbox_info->ext_info[0].htc_ext_sz =
1053 ATH10K_HIF_MBOX0_EXT_WIDTH;
1054 else
1055 /* from QCA6174 2.0(0x504), the width has been extended
1056 * to 56K
1057 */
1058 mbox_info->ext_info[0].htc_ext_sz =
1059 ATH10K_HIF_MBOX0_EXT_WIDTH_ROME_2_0;
1060 break;
1061 case QCA_MANUFACTURER_ID_QCA9377_BASE:
1062 mbox_info->ext_info[0].htc_ext_sz =
1063 ATH10K_HIF_MBOX0_EXT_WIDTH_ROME_2_0;
1064 break;
1065 default:
1066 mbox_info->ext_info[0].htc_ext_sz =
1067 ATH10K_HIF_MBOX0_EXT_WIDTH;
1068 }
1069
1070 mbox_info->ext_info[1].htc_ext_addr =
1071 mbox_info->ext_info[0].htc_ext_addr +
1072 mbox_info->ext_info[0].htc_ext_sz +
1073 ATH10K_HIF_MBOX_DUMMY_SPACE_SIZE;
1074 mbox_info->ext_info[1].htc_ext_sz = ATH10K_HIF_MBOX1_EXT_WIDTH;
1075}
1076
1077/* BMI functions */
1078
1079static int ath10k_sdio_bmi_credits(struct ath10k *ar)
1080{
1081 u32 addr, cmd_credits;
1082 unsigned long timeout;
1083 int ret;
1084
1085 /* Read the counter register to get the command credits */
1086 addr = MBOX_COUNT_DEC_ADDRESS + ATH10K_HIF_MBOX_NUM_MAX * 4;
1087 timeout = jiffies + BMI_COMMUNICATION_TIMEOUT_HZ;
1088 cmd_credits = 0;
1089
1090 while (time_before(jiffies, timeout) && !cmd_credits) {
1091 /* Hit the credit counter with a 4-byte access, the first byte
1092 * read will hit the counter and cause a decrement, while the
1093 * remaining 3 bytes has no effect. The rationale behind this
1094 * is to make all HIF accesses 4-byte aligned.
1095 */
1096 ret = ath10k_sdio_read32(ar, addr, &cmd_credits);
1097 if (ret) {
1098 ath10k_warn(ar,
1099 "unable to decrement the command credit count register: %d\n",
1100 ret);
1101 return ret;
1102 }
1103
1104 /* The counter is only 8 bits.
1105 * Ignore anything in the upper 3 bytes
1106 */
1107 cmd_credits &= 0xFF;
1108 }
1109
1110 if (!cmd_credits) {
1111 ath10k_warn(ar, "bmi communication timeout\n");
1112 return -ETIMEDOUT;
1113 }
1114
1115 return 0;
1116}
1117
1118static int ath10k_sdio_bmi_get_rx_lookahead(struct ath10k *ar)
1119{
1120 unsigned long timeout;
1121 u32 rx_word;
1122 int ret;
1123
1124 timeout = jiffies + BMI_COMMUNICATION_TIMEOUT_HZ;
1125 rx_word = 0;
1126
1127 while ((time_before(jiffies, timeout)) && !rx_word) {
1128 ret = ath10k_sdio_read32(ar,
1129 MBOX_HOST_INT_STATUS_ADDRESS,
1130 &rx_word);
1131 if (ret) {
1132 ath10k_warn(ar, "unable to read RX_LOOKAHEAD_VALID: %d\n", ret);
1133 return ret;
1134 }
1135
1136 /* all we really want is one bit */
1137 rx_word &= 1;
1138 }
1139
1140 if (!rx_word) {
1141 ath10k_warn(ar, "bmi_recv_buf FIFO empty\n");
1142 return -EINVAL;
1143 }
1144
1145 return ret;
1146}
1147
1148static int ath10k_sdio_bmi_exchange_msg(struct ath10k *ar,
1149 void *req, u32 req_len,
1150 void *resp, u32 *resp_len)
1151{
1152 struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
1153 u32 addr;
1154 int ret;
1155
1156 if (req) {
1157 ret = ath10k_sdio_bmi_credits(ar);
1158 if (ret)
1159 return ret;
1160
1161 addr = ar_sdio->mbox_info.htc_addr;
1162
1163 memcpy(ar_sdio->bmi_buf, req, req_len);
1164 ret = ath10k_sdio_write(ar, addr, ar_sdio->bmi_buf, req_len);
1165 if (ret) {
1166 ath10k_warn(ar,
1167 "unable to send the bmi data to the device: %d\n",
1168 ret);
1169 return ret;
1170 }
1171 }
1172
1173 if (!resp || !resp_len)
1174 /* No response expected */
1175 return 0;
1176
1177 /* During normal bootup, small reads may be required.
1178 * Rather than issue an HIF Read and then wait as the Target
1179 * adds successive bytes to the FIFO, we wait here until
1180 * we know that response data is available.
1181 *
1182 * This allows us to cleanly timeout on an unexpected
1183 * Target failure rather than risk problems at the HIF level.
1184 * In particular, this avoids SDIO timeouts and possibly garbage
1185 * data on some host controllers. And on an interconnect
1186 * such as Compact Flash (as well as some SDIO masters) which
1187 * does not provide any indication on data timeout, it avoids
1188 * a potential hang or garbage response.
1189 *
1190 * Synchronization is more difficult for reads larger than the
1191 * size of the MBOX FIFO (128B), because the Target is unable
1192 * to push the 129th byte of data until AFTER the Host posts an
1193 * HIF Read and removes some FIFO data. So for large reads the
1194 * Host proceeds to post an HIF Read BEFORE all the data is
1195 * actually available to read. Fortunately, large BMI reads do
1196 * not occur in practice -- they're supported for debug/development.
1197 *
1198 * So Host/Target BMI synchronization is divided into these cases:
1199 * CASE 1: length < 4
1200 * Should not happen
1201 *
1202 * CASE 2: 4 <= length <= 128
1203 * Wait for first 4 bytes to be in FIFO
1204 * If CONSERVATIVE_BMI_READ is enabled, also wait for
1205 * a BMI command credit, which indicates that the ENTIRE
1206 * response is available in the the FIFO
1207 *
1208 * CASE 3: length > 128
1209 * Wait for the first 4 bytes to be in FIFO
1210 *
1211 * For most uses, a small timeout should be sufficient and we will
1212 * usually see a response quickly; but there may be some unusual
1213 * (debug) cases of BMI_EXECUTE where we want an larger timeout.
1214 * For now, we use an unbounded busy loop while waiting for
1215 * BMI_EXECUTE.
1216 *
1217 * If BMI_EXECUTE ever needs to support longer-latency execution,
1218 * especially in production, this code needs to be enhanced to sleep
1219 * and yield. Also note that BMI_COMMUNICATION_TIMEOUT is currently
1220 * a function of Host processor speed.
1221 */
1222 ret = ath10k_sdio_bmi_get_rx_lookahead(ar);
1223 if (ret)
1224 return ret;
1225
1226 /* We always read from the start of the mbox address */
1227 addr = ar_sdio->mbox_info.htc_addr;
1228 ret = ath10k_sdio_read(ar, addr, ar_sdio->bmi_buf, *resp_len);
1229 if (ret) {
1230 ath10k_warn(ar,
1231 "unable to read the bmi data from the device: %d\n",
1232 ret);
1233 return ret;
1234 }
1235
1236 memcpy(resp, ar_sdio->bmi_buf, *resp_len);
1237
1238 return 0;
1239}
1240
1241/* sdio async handling functions */
1242
1243static struct ath10k_sdio_bus_request
1244*ath10k_sdio_alloc_busreq(struct ath10k *ar)
1245{
1246 struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
1247 struct ath10k_sdio_bus_request *bus_req;
1248
1249 spin_lock_bh(&ar_sdio->lock);
1250
1251 if (list_empty(&ar_sdio->bus_req_freeq)) {
1252 bus_req = NULL;
1253 goto out;
1254 }
1255
1256 bus_req = list_first_entry(&ar_sdio->bus_req_freeq,
1257 struct ath10k_sdio_bus_request, list);
1258 list_del(&bus_req->list);
1259
1260out:
1261 spin_unlock_bh(&ar_sdio->lock);
1262 return bus_req;
1263}
1264
1265static void ath10k_sdio_free_bus_req(struct ath10k *ar,
1266 struct ath10k_sdio_bus_request *bus_req)
1267{
1268 struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
1269
1270 memset(bus_req, 0, sizeof(*bus_req));
1271
1272 spin_lock_bh(&ar_sdio->lock);
1273 list_add_tail(&bus_req->list, &ar_sdio->bus_req_freeq);
1274 spin_unlock_bh(&ar_sdio->lock);
1275}
1276
1277static void __ath10k_sdio_write_async(struct ath10k *ar,
1278 struct ath10k_sdio_bus_request *req)
1279{
1280 struct ath10k_htc_ep *ep;
1281 struct sk_buff *skb;
1282 int ret;
1283
1284 skb = req->skb;
1285 ret = ath10k_sdio_write(ar, req->address, skb->data, skb->len);
1286 if (ret)
1287 ath10k_warn(ar, "failed to write skb to 0x%x asynchronously: %d",
1288 req->address, ret);
1289
1290 if (req->htc_msg) {
1291 ep = &ar->htc.endpoint[req->eid];
1292 ath10k_htc_notify_tx_completion(ep, skb);
1293 } else if (req->comp) {
1294 complete(req->comp);
1295 }
1296
1297 ath10k_sdio_free_bus_req(ar, req);
1298}
1299
1300static void ath10k_sdio_write_async_work(struct work_struct *work)
1301{
1302 struct ath10k_sdio *ar_sdio = container_of(work, struct ath10k_sdio,
1303 wr_async_work);
1304 struct ath10k *ar = ar_sdio->ar;
1305 struct ath10k_sdio_bus_request *req, *tmp_req;
1306
1307 spin_lock_bh(&ar_sdio->wr_async_lock);
1308
1309 list_for_each_entry_safe(req, tmp_req, &ar_sdio->wr_asyncq, list) {
1310 list_del(&req->list);
1311 spin_unlock_bh(&ar_sdio->wr_async_lock);
1312 __ath10k_sdio_write_async(ar, req);
1313 spin_lock_bh(&ar_sdio->wr_async_lock);
1314 }
1315
1316 spin_unlock_bh(&ar_sdio->wr_async_lock);
1317}
1318
1319static int ath10k_sdio_prep_async_req(struct ath10k *ar, u32 addr,
1320 struct sk_buff *skb,
1321 struct completion *comp,
1322 bool htc_msg, enum ath10k_htc_ep_id eid)
1323{
1324 struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
1325 struct ath10k_sdio_bus_request *bus_req;
1326
1327 /* Allocate a bus request for the message and queue it on the
1328 * SDIO workqueue.
1329 */
1330 bus_req = ath10k_sdio_alloc_busreq(ar);
1331 if (!bus_req) {
1332 ath10k_warn(ar,
1333 "unable to allocate bus request for async request\n");
1334 return -ENOMEM;
1335 }
1336
1337 bus_req->skb = skb;
1338 bus_req->eid = eid;
1339 bus_req->address = addr;
1340 bus_req->htc_msg = htc_msg;
1341 bus_req->comp = comp;
1342
1343 spin_lock_bh(&ar_sdio->wr_async_lock);
1344 list_add_tail(&bus_req->list, &ar_sdio->wr_asyncq);
1345 spin_unlock_bh(&ar_sdio->wr_async_lock);
1346
1347 return 0;
1348}
1349
1350/* IRQ handler */
1351
1352static void ath10k_sdio_irq_handler(struct sdio_func *func)
1353{
1354 struct ath10k_sdio *ar_sdio = sdio_get_drvdata(func);
1355 struct ath10k *ar = ar_sdio->ar;
1356 unsigned long timeout;
1357 bool done = false;
1358 int ret;
1359
1360 /* Release the host during interrupts so we can pick it back up when
1361 * we process commands.
1362 */
1363 sdio_release_host(ar_sdio->func);
1364
1365 timeout = jiffies + ATH10K_SDIO_HIF_COMMUNICATION_TIMEOUT_HZ;
1366 do {
1367 ret = ath10k_sdio_mbox_proc_pending_irqs(ar, &done);
1368 if (ret)
1369 break;
1370 } while (time_before(jiffies, timeout) && !done);
1371
1372 ath10k_mac_tx_push_pending(ar);
1373
1374 sdio_claim_host(ar_sdio->func);
1375
1376 if (ret && ret != -ECANCELED)
1377 ath10k_warn(ar, "failed to process pending SDIO interrupts: %d\n",
1378 ret);
1379}
1380
1381/* sdio HIF functions */
1382
1383static int ath10k_sdio_hif_disable_intrs(struct ath10k *ar)
1384{
1385 struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
1386 struct ath10k_sdio_irq_data *irq_data = &ar_sdio->irq_data;
1387 struct ath10k_sdio_irq_enable_regs *regs = irq_data->irq_en_reg;
1388 int ret;
1389
1390 mutex_lock(&irq_data->mtx);
1391
1392 memset(regs, 0, sizeof(*regs));
1393 ret = ath10k_sdio_write(ar, MBOX_INT_STATUS_ENABLE_ADDRESS,
1394 &regs->int_status_en, sizeof(*regs));
1395 if (ret)
1396 ath10k_warn(ar, "unable to disable sdio interrupts: %d\n", ret);
1397
1398 mutex_unlock(&irq_data->mtx);
1399
1400 return ret;
1401}
1402
1403static int ath10k_sdio_hif_power_up(struct ath10k *ar,
1404 enum ath10k_firmware_mode fw_mode)
1405{
1406 struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
1407 struct sdio_func *func = ar_sdio->func;
1408 int ret;
1409
1410 if (!ar_sdio->is_disabled)
1411 return 0;
1412
1413 ath10k_dbg(ar, ATH10K_DBG_BOOT, "sdio power on\n");
1414
1415 ret = ath10k_sdio_config(ar);
1416 if (ret) {
1417 ath10k_err(ar, "failed to config sdio: %d\n", ret);
1418 return ret;
1419 }
1420
1421 sdio_claim_host(func);
1422
1423 ret = sdio_enable_func(func);
1424 if (ret) {
1425 ath10k_warn(ar, "unable to enable sdio function: %d)\n", ret);
1426 sdio_release_host(func);
1427 return ret;
1428 }
1429
1430 sdio_release_host(func);
1431
1432 /* Wait for hardware to initialise. It should take a lot less than
1433 * 20 ms but let's be conservative here.
1434 */
1435 msleep(20);
1436
1437 ar_sdio->is_disabled = false;
1438
1439 ret = ath10k_sdio_hif_disable_intrs(ar);
1440 if (ret)
1441 return ret;
1442
1443 return 0;
1444}
1445
1446static void ath10k_sdio_hif_power_down(struct ath10k *ar)
1447{
1448 struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
1449 int ret;
1450
1451 if (ar_sdio->is_disabled)
1452 return;
1453
1454 ath10k_dbg(ar, ATH10K_DBG_BOOT, "sdio power off\n");
1455
1456 /* Disable the card */
1457 sdio_claim_host(ar_sdio->func);
1458
1459 ret = sdio_disable_func(ar_sdio->func);
1460 if (ret) {
1461 ath10k_warn(ar, "unable to disable sdio function: %d\n", ret);
1462 sdio_release_host(ar_sdio->func);
1463 return;
1464 }
1465
1466 ret = mmc_hw_reset(ar_sdio->func->card->host);
1467 if (ret)
1468 ath10k_warn(ar, "unable to reset sdio: %d\n", ret);
1469
1470 sdio_release_host(ar_sdio->func);
1471
1472 ar_sdio->is_disabled = true;
1473}
1474
1475static int ath10k_sdio_hif_tx_sg(struct ath10k *ar, u8 pipe_id,
1476 struct ath10k_hif_sg_item *items, int n_items)
1477{
1478 struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
1479 enum ath10k_htc_ep_id eid;
1480 struct sk_buff *skb;
1481 int ret, i;
1482
1483 eid = pipe_id_to_eid(pipe_id);
1484
1485 for (i = 0; i < n_items; i++) {
1486 size_t padded_len;
1487 u32 address;
1488
1489 skb = items[i].transfer_context;
1490 padded_len = ath10k_sdio_calc_txrx_padded_len(ar_sdio,
1491 skb->len);
1492 skb_trim(skb, padded_len);
1493
1494 /* Write TX data to the end of the mbox address space */
1495 address = ar_sdio->mbox_addr[eid] + ar_sdio->mbox_size[eid] -
1496 skb->len;
1497 ret = ath10k_sdio_prep_async_req(ar, address, skb,
1498 NULL, true, eid);
1499 if (ret)
1500 return ret;
1501 }
1502
1503 queue_work(ar_sdio->workqueue, &ar_sdio->wr_async_work);
1504
1505 return 0;
1506}
1507
1508static int ath10k_sdio_hif_enable_intrs(struct ath10k *ar)
1509{
1510 struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
1511 struct ath10k_sdio_irq_data *irq_data = &ar_sdio->irq_data;
1512 struct ath10k_sdio_irq_enable_regs *regs = irq_data->irq_en_reg;
1513 int ret;
1514
1515 mutex_lock(&irq_data->mtx);
1516
1517 /* Enable all but CPU interrupts */
1518 regs->int_status_en = FIELD_PREP(MBOX_INT_STATUS_ENABLE_ERROR_MASK, 1) |
1519 FIELD_PREP(MBOX_INT_STATUS_ENABLE_CPU_MASK, 1) |
1520 FIELD_PREP(MBOX_INT_STATUS_ENABLE_COUNTER_MASK, 1);
1521
1522 /* NOTE: There are some cases where HIF can do detection of
1523 * pending mbox messages which is disabled now.
1524 */
1525 regs->int_status_en |=
1526 FIELD_PREP(MBOX_INT_STATUS_ENABLE_MBOX_DATA_MASK, 1);
1527
1528 /* Set up the CPU Interrupt Status Register, enable CPU sourced interrupt #0
1529 * #0 is used for report assertion from target
1530 */
1531 regs->cpu_int_status_en = FIELD_PREP(MBOX_CPU_STATUS_ENABLE_ASSERT_MASK, 1);
1532
1533 /* Set up the Error Interrupt status Register */
1534 regs->err_int_status_en =
1535 FIELD_PREP(MBOX_ERROR_STATUS_ENABLE_RX_UNDERFLOW_MASK, 1) |
1536 FIELD_PREP(MBOX_ERROR_STATUS_ENABLE_TX_OVERFLOW_MASK, 1);
1537
1538 /* Enable Counter interrupt status register to get fatal errors for
1539 * debugging.
1540 */
1541 regs->cntr_int_status_en =
1542 FIELD_PREP(MBOX_COUNTER_INT_STATUS_ENABLE_BIT_MASK,
1543 ATH10K_SDIO_TARGET_DEBUG_INTR_MASK);
1544
1545 ret = ath10k_sdio_write(ar, MBOX_INT_STATUS_ENABLE_ADDRESS,
1546 &regs->int_status_en, sizeof(*regs));
1547 if (ret)
1548 ath10k_warn(ar,
1549 "failed to update mbox interrupt status register : %d\n",
1550 ret);
1551
1552 mutex_unlock(&irq_data->mtx);
1553 return ret;
1554}
1555
1556static int ath10k_sdio_hif_set_mbox_sleep(struct ath10k *ar, bool enable_sleep)
1557{
1558 u32 val;
1559 int ret;
1560
1561 ret = ath10k_sdio_read32(ar, ATH10K_FIFO_TIMEOUT_AND_CHIP_CONTROL, &val);
1562 if (ret) {
1563 ath10k_warn(ar, "failed to read fifo/chip control register: %d\n",
1564 ret);
1565 return ret;
1566 }
1567
1568 if (enable_sleep)
1569 val &= ATH10K_FIFO_TIMEOUT_AND_CHIP_CONTROL_DISABLE_SLEEP_OFF;
1570 else
1571 val |= ATH10K_FIFO_TIMEOUT_AND_CHIP_CONTROL_DISABLE_SLEEP_ON;
1572
1573 ret = ath10k_sdio_write32(ar, ATH10K_FIFO_TIMEOUT_AND_CHIP_CONTROL, val);
1574 if (ret) {
1575 ath10k_warn(ar, "failed to write to FIFO_TIMEOUT_AND_CHIP_CONTROL: %d",
1576 ret);
1577 return ret;
1578 }
1579
1580 return 0;
1581}
1582
1583/* HIF diagnostics */
1584
1585static int ath10k_sdio_hif_diag_read(struct ath10k *ar, u32 address, void *buf,
1586 size_t buf_len)
1587{
1588 int ret;
1589 void *mem;
1590
1591 mem = kzalloc(buf_len, GFP_KERNEL);
1592 if (!mem)
1593 return -ENOMEM;
1594
1595 /* set window register to start read cycle */
1596 ret = ath10k_sdio_write32(ar, MBOX_WINDOW_READ_ADDR_ADDRESS, address);
1597 if (ret) {
1598 ath10k_warn(ar, "failed to set mbox window read address: %d", ret);
1599 goto out;
1600 }
1601
1602 /* read the data */
1603 ret = ath10k_sdio_read(ar, MBOX_WINDOW_DATA_ADDRESS, mem, buf_len);
1604 if (ret) {
1605 ath10k_warn(ar, "failed to read from mbox window data address: %d\n",
1606 ret);
1607 goto out;
1608 }
1609
1610 memcpy(buf, mem, buf_len);
1611
1612out:
1613 kfree(mem);
1614
1615 return ret;
1616}
1617
1618static int ath10k_sdio_hif_diag_read32(struct ath10k *ar, u32 address,
1619 u32 *value)
1620{
1621 __le32 *val;
1622 int ret;
1623
1624 val = kzalloc(sizeof(*val), GFP_KERNEL);
1625 if (!val)
1626 return -ENOMEM;
1627
1628 ret = ath10k_sdio_hif_diag_read(ar, address, val, sizeof(*val));
1629 if (ret)
1630 goto out;
1631
1632 *value = __le32_to_cpu(*val);
1633
1634out:
1635 kfree(val);
1636
1637 return ret;
1638}
1639
1640static int ath10k_sdio_hif_diag_write_mem(struct ath10k *ar, u32 address,
1641 const void *data, int nbytes)
1642{
1643 int ret;
1644
1645 /* set write data */
1646 ret = ath10k_sdio_write(ar, MBOX_WINDOW_DATA_ADDRESS, data, nbytes);
1647 if (ret) {
1648 ath10k_warn(ar,
1649 "failed to write 0x%p to mbox window data address: %d\n",
1650 data, ret);
1651 return ret;
1652 }
1653
1654 /* set window register, which starts the write cycle */
1655 ret = ath10k_sdio_write32(ar, MBOX_WINDOW_WRITE_ADDR_ADDRESS, address);
1656 if (ret) {
1657 ath10k_warn(ar, "failed to set mbox window write address: %d", ret);
1658 return ret;
1659 }
1660
1661 return 0;
1662}
1663
1664static int ath10k_sdio_hif_swap_mailbox(struct ath10k *ar)
1665{
1666 struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
1667 u32 addr, val;
1668 int ret = 0;
1669
1670 addr = host_interest_item_address(HI_ITEM(hi_acs_flags));
1671
1672 ret = ath10k_sdio_hif_diag_read32(ar, addr, &val);
1673 if (ret) {
1674 ath10k_warn(ar, "unable to read hi_acs_flags : %d\n", ret);
1675 return ret;
1676 }
1677
1678 if (val & HI_ACS_FLAGS_SDIO_SWAP_MAILBOX_FW_ACK) {
1679 ath10k_dbg(ar, ATH10K_DBG_SDIO,
1680 "sdio mailbox swap service enabled\n");
1681 ar_sdio->swap_mbox = true;
1682 } else {
1683 ath10k_dbg(ar, ATH10K_DBG_SDIO,
1684 "sdio mailbox swap service disabled\n");
1685 ar_sdio->swap_mbox = false;
1686 }
1687
1688 return 0;
1689}
1690
1691/* HIF start/stop */
1692
1693static int ath10k_sdio_hif_start(struct ath10k *ar)
1694{
1695 struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
1696 int ret;
1697
1698 /* Sleep 20 ms before HIF interrupts are disabled.
1699 * This will give target plenty of time to process the BMI done
1700 * request before interrupts are disabled.
1701 */
1702 msleep(20);
1703 ret = ath10k_sdio_hif_disable_intrs(ar);
1704 if (ret)
1705 return ret;
1706
1707 /* eid 0 always uses the lower part of the extended mailbox address
1708 * space (ext_info[0].htc_ext_addr).
1709 */
1710 ar_sdio->mbox_addr[0] = ar_sdio->mbox_info.ext_info[0].htc_ext_addr;
1711 ar_sdio->mbox_size[0] = ar_sdio->mbox_info.ext_info[0].htc_ext_sz;
1712
1713 sdio_claim_host(ar_sdio->func);
1714
1715 /* Register the isr */
1716 ret = sdio_claim_irq(ar_sdio->func, ath10k_sdio_irq_handler);
1717 if (ret) {
1718 ath10k_warn(ar, "failed to claim sdio interrupt: %d\n", ret);
1719 sdio_release_host(ar_sdio->func);
1720 return ret;
1721 }
1722
1723 sdio_release_host(ar_sdio->func);
1724
1725 ret = ath10k_sdio_hif_enable_intrs(ar);
1726 if (ret)
1727 ath10k_warn(ar, "failed to enable sdio interrupts: %d\n", ret);
1728
1729 /* Enable sleep and then disable it again */
1730 ret = ath10k_sdio_hif_set_mbox_sleep(ar, true);
1731 if (ret)
1732 return ret;
1733
1734 /* Wait for 20ms for the written value to take effect */
1735 msleep(20);
1736
1737 ret = ath10k_sdio_hif_set_mbox_sleep(ar, false);
1738 if (ret)
1739 return ret;
1740
1741 return 0;
1742}
1743
1744#define SDIO_IRQ_DISABLE_TIMEOUT_HZ (3 * HZ)
1745
1746static void ath10k_sdio_irq_disable(struct ath10k *ar)
1747{
1748 struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
1749 struct ath10k_sdio_irq_data *irq_data = &ar_sdio->irq_data;
1750 struct ath10k_sdio_irq_enable_regs *regs = irq_data->irq_en_reg;
1751 struct sk_buff *skb;
1752 struct completion irqs_disabled_comp;
1753 int ret;
1754
1755 skb = dev_alloc_skb(sizeof(*regs));
1756 if (!skb)
1757 return;
1758
1759 mutex_lock(&irq_data->mtx);
1760
1761 memset(regs, 0, sizeof(*regs)); /* disable all interrupts */
1762 memcpy(skb->data, regs, sizeof(*regs));
1763 skb_put(skb, sizeof(*regs));
1764
1765 mutex_unlock(&irq_data->mtx);
1766
1767 init_completion(&irqs_disabled_comp);
1768 ret = ath10k_sdio_prep_async_req(ar, MBOX_INT_STATUS_ENABLE_ADDRESS,
1769 skb, &irqs_disabled_comp, false, 0);
1770 if (ret)
1771 goto out;
1772
1773 queue_work(ar_sdio->workqueue, &ar_sdio->wr_async_work);
1774
1775 /* Wait for the completion of the IRQ disable request.
1776 * If there is a timeout we will try to disable irq's anyway.
1777 */
1778 ret = wait_for_completion_timeout(&irqs_disabled_comp,
1779 SDIO_IRQ_DISABLE_TIMEOUT_HZ);
1780 if (!ret)
1781 ath10k_warn(ar, "sdio irq disable request timed out\n");
1782
1783 sdio_claim_host(ar_sdio->func);
1784
1785 ret = sdio_release_irq(ar_sdio->func);
1786 if (ret)
1787 ath10k_warn(ar, "failed to release sdio interrupt: %d\n", ret);
1788
1789 sdio_release_host(ar_sdio->func);
1790
1791out:
1792 kfree_skb(skb);
1793}
1794
1795static void ath10k_sdio_hif_stop(struct ath10k *ar)
1796{
1797 struct ath10k_sdio_bus_request *req, *tmp_req;
1798 struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
1799
1800 ath10k_sdio_irq_disable(ar);
1801
1802 cancel_work_sync(&ar_sdio->wr_async_work);
1803
1804 spin_lock_bh(&ar_sdio->wr_async_lock);
1805
1806 /* Free all bus requests that have not been handled */
1807 list_for_each_entry_safe(req, tmp_req, &ar_sdio->wr_asyncq, list) {
1808 struct ath10k_htc_ep *ep;
1809
1810 list_del(&req->list);
1811
1812 if (req->htc_msg) {
1813 ep = &ar->htc.endpoint[req->eid];
1814 ath10k_htc_notify_tx_completion(ep, req->skb);
1815 } else if (req->skb) {
1816 kfree_skb(req->skb);
1817 }
1818 ath10k_sdio_free_bus_req(ar, req);
1819 }
1820
1821 spin_unlock_bh(&ar_sdio->wr_async_lock);
1822}
1823
1824#ifdef CONFIG_PM
1825
1826static int ath10k_sdio_hif_suspend(struct ath10k *ar)
1827{
1828 return -EOPNOTSUPP;
1829}
1830
1831static int ath10k_sdio_hif_resume(struct ath10k *ar)
1832{
1833 switch (ar->state) {
1834 case ATH10K_STATE_OFF:
1835 ath10k_dbg(ar, ATH10K_DBG_SDIO,
1836 "sdio resume configuring sdio\n");
1837
1838 /* need to set sdio settings after power is cut from sdio */
1839 ath10k_sdio_config(ar);
1840 break;
1841
1842 case ATH10K_STATE_ON:
1843 default:
1844 break;
1845 }
1846
1847 return 0;
1848}
1849#endif
1850
1851static int ath10k_sdio_hif_map_service_to_pipe(struct ath10k *ar,
1852 u16 service_id,
1853 u8 *ul_pipe, u8 *dl_pipe)
1854{
1855 struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
1856 struct ath10k_htc *htc = &ar->htc;
1857 u32 htt_addr, wmi_addr, htt_mbox_size, wmi_mbox_size;
1858 enum ath10k_htc_ep_id eid;
1859 bool ep_found = false;
1860 int i;
1861
1862 /* For sdio, we are interested in the mapping between eid
1863 * and pipeid rather than service_id to pipe_id.
1864 * First we find out which eid has been allocated to the
1865 * service...
1866 */
1867 for (i = 0; i < ATH10K_HTC_EP_COUNT; i++) {
1868 if (htc->endpoint[i].service_id == service_id) {
1869 eid = htc->endpoint[i].eid;
1870 ep_found = true;
1871 break;
1872 }
1873 }
1874
1875 if (!ep_found)
1876 return -EINVAL;
1877
1878 /* Then we create the simplest mapping possible between pipeid
1879 * and eid
1880 */
1881 *ul_pipe = *dl_pipe = (u8)eid;
1882
1883 /* Normally, HTT will use the upper part of the extended
1884 * mailbox address space (ext_info[1].htc_ext_addr) and WMI ctrl
1885 * the lower part (ext_info[0].htc_ext_addr).
1886 * If fw wants swapping of mailbox addresses, the opposite is true.
1887 */
1888 if (ar_sdio->swap_mbox) {
1889 htt_addr = ar_sdio->mbox_info.ext_info[0].htc_ext_addr;
1890 wmi_addr = ar_sdio->mbox_info.ext_info[1].htc_ext_addr;
1891 htt_mbox_size = ar_sdio->mbox_info.ext_info[0].htc_ext_sz;
1892 wmi_mbox_size = ar_sdio->mbox_info.ext_info[1].htc_ext_sz;
1893 } else {
1894 htt_addr = ar_sdio->mbox_info.ext_info[1].htc_ext_addr;
1895 wmi_addr = ar_sdio->mbox_info.ext_info[0].htc_ext_addr;
1896 htt_mbox_size = ar_sdio->mbox_info.ext_info[1].htc_ext_sz;
1897 wmi_mbox_size = ar_sdio->mbox_info.ext_info[0].htc_ext_sz;
1898 }
1899
1900 switch (service_id) {
1901 case ATH10K_HTC_SVC_ID_RSVD_CTRL:
1902 /* HTC ctrl ep mbox address has already been setup in
1903 * ath10k_sdio_hif_start
1904 */
1905 break;
1906 case ATH10K_HTC_SVC_ID_WMI_CONTROL:
1907 ar_sdio->mbox_addr[eid] = wmi_addr;
1908 ar_sdio->mbox_size[eid] = wmi_mbox_size;
1909 ath10k_dbg(ar, ATH10K_DBG_SDIO,
1910 "sdio wmi ctrl mbox_addr 0x%x mbox_size %d\n",
1911 ar_sdio->mbox_addr[eid], ar_sdio->mbox_size[eid]);
1912 break;
1913 case ATH10K_HTC_SVC_ID_HTT_DATA_MSG:
1914 ar_sdio->mbox_addr[eid] = htt_addr;
1915 ar_sdio->mbox_size[eid] = htt_mbox_size;
1916 ath10k_dbg(ar, ATH10K_DBG_SDIO,
1917 "sdio htt data mbox_addr 0x%x mbox_size %d\n",
1918 ar_sdio->mbox_addr[eid], ar_sdio->mbox_size[eid]);
1919 break;
1920 default:
1921 ath10k_warn(ar, "unsupported HTC service id: %d\n",
1922 service_id);
1923 return -EINVAL;
1924 }
1925
1926 return 0;
1927}
1928
1929static void ath10k_sdio_hif_get_default_pipe(struct ath10k *ar,
1930 u8 *ul_pipe, u8 *dl_pipe)
1931{
1932 ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio hif get default pipe\n");
1933
1934 /* HTC ctrl ep (SVC id 1) always has eid (and pipe_id in our
1935 * case) == 0
1936 */
1937 *ul_pipe = 0;
1938 *dl_pipe = 0;
1939}
1940
1941/* This op is currently only used by htc_wait_target if the HTC ready
1942 * message times out. It is not applicable for SDIO since there is nothing
1943 * we can do if the HTC ready message does not arrive in time.
1944 * TODO: Make this op non mandatory by introducing a NULL check in the
1945 * hif op wrapper.
1946 */
1947static void ath10k_sdio_hif_send_complete_check(struct ath10k *ar,
1948 u8 pipe, int force)
1949{
1950}
1951
1952static const struct ath10k_hif_ops ath10k_sdio_hif_ops = {
1953 .tx_sg = ath10k_sdio_hif_tx_sg,
1954 .diag_read = ath10k_sdio_hif_diag_read,
1955 .diag_write = ath10k_sdio_hif_diag_write_mem,
1956 .exchange_bmi_msg = ath10k_sdio_bmi_exchange_msg,
1957 .start = ath10k_sdio_hif_start,
1958 .stop = ath10k_sdio_hif_stop,
1959 .swap_mailbox = ath10k_sdio_hif_swap_mailbox,
1960 .map_service_to_pipe = ath10k_sdio_hif_map_service_to_pipe,
1961 .get_default_pipe = ath10k_sdio_hif_get_default_pipe,
1962 .send_complete_check = ath10k_sdio_hif_send_complete_check,
1963 .power_up = ath10k_sdio_hif_power_up,
1964 .power_down = ath10k_sdio_hif_power_down,
1965#ifdef CONFIG_PM
1966 .suspend = ath10k_sdio_hif_suspend,
1967 .resume = ath10k_sdio_hif_resume,
1968#endif
1969};
1970
1971#ifdef CONFIG_PM_SLEEP
1972
1973/* Empty handlers so that mmc subsystem doesn't remove us entirely during
1974 * suspend. We instead follow cfg80211 suspend/resume handlers.
1975 */
1976static int ath10k_sdio_pm_suspend(struct device *device)
1977{
1978 return 0;
1979}
1980
1981static int ath10k_sdio_pm_resume(struct device *device)
1982{
1983 return 0;
1984}
1985
1986static SIMPLE_DEV_PM_OPS(ath10k_sdio_pm_ops, ath10k_sdio_pm_suspend,
1987 ath10k_sdio_pm_resume);
1988
1989#define ATH10K_SDIO_PM_OPS (&ath10k_sdio_pm_ops)
1990
1991#else
1992
1993#define ATH10K_SDIO_PM_OPS NULL
1994
1995#endif /* CONFIG_PM_SLEEP */
1996
1997static int ath10k_sdio_probe(struct sdio_func *func,
1998 const struct sdio_device_id *id)
1999{
2000 struct ath10k_sdio *ar_sdio;
2001 struct ath10k *ar;
2002 enum ath10k_hw_rev hw_rev;
2003 u32 dev_id_base;
2004 struct ath10k_bus_params bus_params = {};
2005 int ret, i;
2006
2007 /* Assumption: All SDIO based chipsets (so far) are QCA6174 based.
2008 * If there will be newer chipsets that does not use the hw reg
2009 * setup as defined in qca6174_regs and qca6174_values, this
2010 * assumption is no longer valid and hw_rev must be setup differently
2011 * depending on chipset.
2012 */
2013 hw_rev = ATH10K_HW_QCA6174;
2014
2015 ar = ath10k_core_create(sizeof(*ar_sdio), &func->dev, ATH10K_BUS_SDIO,
2016 hw_rev, &ath10k_sdio_hif_ops);
2017 if (!ar) {
2018 dev_err(&func->dev, "failed to allocate core\n");
2019 return -ENOMEM;
2020 }
2021
2022 ath10k_dbg(ar, ATH10K_DBG_BOOT,
2023 "sdio new func %d vendor 0x%x device 0x%x block 0x%x/0x%x\n",
2024 func->num, func->vendor, func->device,
2025 func->max_blksize, func->cur_blksize);
2026
2027 ar_sdio = ath10k_sdio_priv(ar);
2028
2029 ar_sdio->irq_data.irq_proc_reg =
2030 devm_kzalloc(ar->dev, sizeof(struct ath10k_sdio_irq_proc_regs),
2031 GFP_KERNEL);
2032 if (!ar_sdio->irq_data.irq_proc_reg) {
2033 ret = -ENOMEM;
2034 goto err_core_destroy;
2035 }
2036
2037 ar_sdio->irq_data.irq_en_reg =
2038 devm_kzalloc(ar->dev, sizeof(struct ath10k_sdio_irq_enable_regs),
2039 GFP_KERNEL);
2040 if (!ar_sdio->irq_data.irq_en_reg) {
2041 ret = -ENOMEM;
2042 goto err_core_destroy;
2043 }
2044
2045 ar_sdio->bmi_buf = devm_kzalloc(ar->dev, BMI_MAX_CMDBUF_SIZE, GFP_KERNEL);
2046 if (!ar_sdio->bmi_buf) {
2047 ret = -ENOMEM;
2048 goto err_core_destroy;
2049 }
2050
2051 ar_sdio->func = func;
2052 sdio_set_drvdata(func, ar_sdio);
2053
2054 ar_sdio->is_disabled = true;
2055 ar_sdio->ar = ar;
2056
2057 spin_lock_init(&ar_sdio->lock);
2058 spin_lock_init(&ar_sdio->wr_async_lock);
2059 mutex_init(&ar_sdio->irq_data.mtx);
2060
2061 INIT_LIST_HEAD(&ar_sdio->bus_req_freeq);
2062 INIT_LIST_HEAD(&ar_sdio->wr_asyncq);
2063
2064 INIT_WORK(&ar_sdio->wr_async_work, ath10k_sdio_write_async_work);
2065 ar_sdio->workqueue = create_singlethread_workqueue("ath10k_sdio_wq");
2066 if (!ar_sdio->workqueue) {
2067 ret = -ENOMEM;
2068 goto err_core_destroy;
2069 }
2070
2071 for (i = 0; i < ATH10K_SDIO_BUS_REQUEST_MAX_NUM; i++)
2072 ath10k_sdio_free_bus_req(ar, &ar_sdio->bus_req[i]);
2073
2074 dev_id_base = FIELD_GET(QCA_MANUFACTURER_ID_BASE, id->device);
2075 switch (dev_id_base) {
2076 case QCA_MANUFACTURER_ID_AR6005_BASE:
2077 case QCA_MANUFACTURER_ID_QCA9377_BASE:
2078 ar->dev_id = QCA9377_1_0_DEVICE_ID;
2079 break;
2080 default:
2081 ret = -ENODEV;
2082 ath10k_err(ar, "unsupported device id %u (0x%x)\n",
2083 dev_id_base, id->device);
2084 goto err_free_wq;
2085 }
2086
2087 ar->id.vendor = id->vendor;
2088 ar->id.device = id->device;
2089
2090 ath10k_sdio_set_mbox_info(ar);
2091
2092 bus_params.dev_type = ATH10K_DEV_TYPE_HL;
2093 /* TODO: don't know yet how to get chip_id with SDIO */
2094 bus_params.chip_id = 0;
2095 bus_params.hl_msdu_ids = true;
2096
2097 ret = ath10k_core_register(ar, &bus_params);
2098 if (ret) {
2099 ath10k_err(ar, "failed to register driver core: %d\n", ret);
2100 goto err_free_wq;
2101 }
2102
2103 /* TODO: remove this once SDIO support is fully implemented */
2104 ath10k_warn(ar, "WARNING: ath10k SDIO support is work-in-progress, problems may arise!\n");
2105
2106 return 0;
2107
2108err_free_wq:
2109 destroy_workqueue(ar_sdio->workqueue);
2110err_core_destroy:
2111 ath10k_core_destroy(ar);
2112
2113 return ret;
2114}
2115
2116static void ath10k_sdio_remove(struct sdio_func *func)
2117{
2118 struct ath10k_sdio *ar_sdio = sdio_get_drvdata(func);
2119 struct ath10k *ar = ar_sdio->ar;
2120
2121 ath10k_dbg(ar, ATH10K_DBG_BOOT,
2122 "sdio removed func %d vendor 0x%x device 0x%x\n",
2123 func->num, func->vendor, func->device);
2124
2125 ath10k_core_unregister(ar);
2126 ath10k_core_destroy(ar);
2127
2128 flush_workqueue(ar_sdio->workqueue);
2129 destroy_workqueue(ar_sdio->workqueue);
2130}
2131
2132static const struct sdio_device_id ath10k_sdio_devices[] = {
2133 {SDIO_DEVICE(QCA_MANUFACTURER_CODE,
2134 (QCA_SDIO_ID_AR6005_BASE | 0xA))},
2135 {SDIO_DEVICE(QCA_MANUFACTURER_CODE,
2136 (QCA_SDIO_ID_QCA9377_BASE | 0x1))},
2137 {},
2138};
2139
2140MODULE_DEVICE_TABLE(sdio, ath10k_sdio_devices);
2141
2142static struct sdio_driver ath10k_sdio_driver = {
2143 .name = "ath10k_sdio",
2144 .id_table = ath10k_sdio_devices,
2145 .probe = ath10k_sdio_probe,
2146 .remove = ath10k_sdio_remove,
2147 .drv = {
2148 .owner = THIS_MODULE,
2149 .pm = ATH10K_SDIO_PM_OPS,
2150 },
2151};
2152
2153static int __init ath10k_sdio_init(void)
2154{
2155 int ret;
2156
2157 ret = sdio_register_driver(&ath10k_sdio_driver);
2158 if (ret)
2159 pr_err("sdio driver registration failed: %d\n", ret);
2160
2161 return ret;
2162}
2163
2164static void __exit ath10k_sdio_exit(void)
2165{
2166 sdio_unregister_driver(&ath10k_sdio_driver);
2167}
2168
2169module_init(ath10k_sdio_init);
2170module_exit(ath10k_sdio_exit);
2171
2172MODULE_AUTHOR("Qualcomm Atheros");
2173MODULE_DESCRIPTION("Driver support for Qualcomm Atheros 802.11ac WLAN SDIO devices");
2174MODULE_LICENSE("Dual BSD/GPL");