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