Merge branch 'next' of git://git.infradead.org/users/vkoul/slave-dma
[cascardo/linux.git] / drivers / net / wireless / ath / ath6kl / sdio.c
1 /*
2  * Copyright (c) 2004-2011 Atheros Communications Inc.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include <linux/mmc/card.h>
18 #include <linux/mmc/mmc.h>
19 #include <linux/mmc/host.h>
20 #include <linux/mmc/sdio_func.h>
21 #include <linux/mmc/sdio_ids.h>
22 #include <linux/mmc/sdio.h>
23 #include <linux/mmc/sd.h>
24 #include "htc_hif.h"
25 #include "hif-ops.h"
26 #include "target.h"
27 #include "debug.h"
28 #include "cfg80211.h"
29
30 struct ath6kl_sdio {
31         struct sdio_func *func;
32
33         spinlock_t lock;
34
35         /* free list */
36         struct list_head bus_req_freeq;
37
38         /* available bus requests */
39         struct bus_request bus_req[BUS_REQUEST_MAX_NUM];
40
41         struct ath6kl *ar;
42         u8 *dma_buffer;
43
44         /* scatter request list head */
45         struct list_head scat_req;
46
47         spinlock_t scat_lock;
48         bool is_disabled;
49         atomic_t irq_handling;
50         const struct sdio_device_id *id;
51         struct work_struct wr_async_work;
52         struct list_head wr_asyncq;
53         spinlock_t wr_async_lock;
54 };
55
56 #define CMD53_ARG_READ          0
57 #define CMD53_ARG_WRITE         1
58 #define CMD53_ARG_BLOCK_BASIS   1
59 #define CMD53_ARG_FIXED_ADDRESS 0
60 #define CMD53_ARG_INCR_ADDRESS  1
61
62 static inline struct ath6kl_sdio *ath6kl_sdio_priv(struct ath6kl *ar)
63 {
64         return ar->hif_priv;
65 }
66
67 /*
68  * Macro to check if DMA buffer is WORD-aligned and DMA-able.
69  * Most host controllers assume the buffer is DMA'able and will
70  * bug-check otherwise (i.e. buffers on the stack). virt_addr_valid
71  * check fails on stack memory.
72  */
73 static inline bool buf_needs_bounce(u8 *buf)
74 {
75         return ((unsigned long) buf & 0x3) || !virt_addr_valid(buf);
76 }
77
78 static void ath6kl_sdio_set_mbox_info(struct ath6kl *ar)
79 {
80         struct ath6kl_mbox_info *mbox_info = &ar->mbox_info;
81
82         /* EP1 has an extended range */
83         mbox_info->htc_addr = HIF_MBOX_BASE_ADDR;
84         mbox_info->htc_ext_addr = HIF_MBOX0_EXT_BASE_ADDR;
85         mbox_info->htc_ext_sz = HIF_MBOX0_EXT_WIDTH;
86         mbox_info->block_size = HIF_MBOX_BLOCK_SIZE;
87         mbox_info->gmbox_addr = HIF_GMBOX_BASE_ADDR;
88         mbox_info->gmbox_sz = HIF_GMBOX_WIDTH;
89 }
90
91 static inline void ath6kl_sdio_set_cmd53_arg(u32 *arg, u8 rw, u8 func,
92                                              u8 mode, u8 opcode, u32 addr,
93                                              u16 blksz)
94 {
95         *arg = (((rw & 1) << 31) |
96                 ((func & 0x7) << 28) |
97                 ((mode & 1) << 27) |
98                 ((opcode & 1) << 26) |
99                 ((addr & 0x1FFFF) << 9) |
100                 (blksz & 0x1FF));
101 }
102
103 static inline void ath6kl_sdio_set_cmd52_arg(u32 *arg, u8 write, u8 raw,
104                                              unsigned int address,
105                                              unsigned char val)
106 {
107         const u8 func = 0;
108
109         *arg = ((write & 1) << 31) |
110                ((func & 0x7) << 28) |
111                ((raw & 1) << 27) |
112                (1 << 26) |
113                ((address & 0x1FFFF) << 9) |
114                (1 << 8) |
115                (val & 0xFF);
116 }
117
118 static int ath6kl_sdio_func0_cmd52_wr_byte(struct mmc_card *card,
119                                            unsigned int address,
120                                            unsigned char byte)
121 {
122         struct mmc_command io_cmd;
123
124         memset(&io_cmd, 0, sizeof(io_cmd));
125         ath6kl_sdio_set_cmd52_arg(&io_cmd.arg, 1, 0, address, byte);
126         io_cmd.opcode = SD_IO_RW_DIRECT;
127         io_cmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
128
129         return mmc_wait_for_cmd(card->host, &io_cmd, 0);
130 }
131
132 static int ath6kl_sdio_io(struct sdio_func *func, u32 request, u32 addr,
133                           u8 *buf, u32 len)
134 {
135         int ret = 0;
136
137         if (request & HIF_WRITE) {
138                 /* FIXME: looks like ugly workaround for something */
139                 if (addr >= HIF_MBOX_BASE_ADDR &&
140                     addr <= HIF_MBOX_END_ADDR)
141                         addr += (HIF_MBOX_WIDTH - len);
142
143                 /* FIXME: this also looks like ugly workaround */
144                 if (addr == HIF_MBOX0_EXT_BASE_ADDR)
145                         addr += HIF_MBOX0_EXT_WIDTH - len;
146
147                 if (request & HIF_FIXED_ADDRESS)
148                         ret = sdio_writesb(func, addr, buf, len);
149                 else
150                         ret = sdio_memcpy_toio(func, addr, buf, len);
151         } else {
152                 if (request & HIF_FIXED_ADDRESS)
153                         ret = sdio_readsb(func, buf, addr, len);
154                 else
155                         ret = sdio_memcpy_fromio(func, buf, addr, len);
156         }
157
158         ath6kl_dbg(ATH6KL_DBG_SDIO, "%s addr 0x%x%s buf 0x%p len %d\n",
159                    request & HIF_WRITE ? "wr" : "rd", addr,
160                    request & HIF_FIXED_ADDRESS ? " (fixed)" : "", buf, len);
161         ath6kl_dbg_dump(ATH6KL_DBG_SDIO_DUMP, NULL, "sdio ", buf, len);
162
163         return ret;
164 }
165
166 static struct bus_request *ath6kl_sdio_alloc_busreq(struct ath6kl_sdio *ar_sdio)
167 {
168         struct bus_request *bus_req;
169         unsigned long flag;
170
171         spin_lock_irqsave(&ar_sdio->lock, flag);
172
173         if (list_empty(&ar_sdio->bus_req_freeq)) {
174                 spin_unlock_irqrestore(&ar_sdio->lock, flag);
175                 return NULL;
176         }
177
178         bus_req = list_first_entry(&ar_sdio->bus_req_freeq,
179                                    struct bus_request, list);
180         list_del(&bus_req->list);
181
182         spin_unlock_irqrestore(&ar_sdio->lock, flag);
183         ath6kl_dbg(ATH6KL_DBG_SCATTER, "%s: bus request 0x%p\n",
184                    __func__, bus_req);
185
186         return bus_req;
187 }
188
189 static void ath6kl_sdio_free_bus_req(struct ath6kl_sdio *ar_sdio,
190                                      struct bus_request *bus_req)
191 {
192         unsigned long flag;
193
194         ath6kl_dbg(ATH6KL_DBG_SCATTER, "%s: bus request 0x%p\n",
195                    __func__, bus_req);
196
197         spin_lock_irqsave(&ar_sdio->lock, flag);
198         list_add_tail(&bus_req->list, &ar_sdio->bus_req_freeq);
199         spin_unlock_irqrestore(&ar_sdio->lock, flag);
200 }
201
202 static void ath6kl_sdio_setup_scat_data(struct hif_scatter_req *scat_req,
203                                         struct mmc_data *data)
204 {
205         struct scatterlist *sg;
206         int i;
207
208         data->blksz = HIF_MBOX_BLOCK_SIZE;
209         data->blocks = scat_req->len / HIF_MBOX_BLOCK_SIZE;
210
211         ath6kl_dbg(ATH6KL_DBG_SCATTER,
212                    "hif-scatter: (%s) addr: 0x%X, (block len: %d, block count: %d) , (tot:%d,sg:%d)\n",
213                    (scat_req->req & HIF_WRITE) ? "WR" : "RD", scat_req->addr,
214                    data->blksz, data->blocks, scat_req->len,
215                    scat_req->scat_entries);
216
217         data->flags = (scat_req->req & HIF_WRITE) ? MMC_DATA_WRITE :
218                                                     MMC_DATA_READ;
219
220         /* fill SG entries */
221         sg = scat_req->sgentries;
222         sg_init_table(sg, scat_req->scat_entries);
223
224         /* assemble SG list */
225         for (i = 0; i < scat_req->scat_entries; i++, sg++) {
226                 ath6kl_dbg(ATH6KL_DBG_SCATTER, "%d: addr:0x%p, len:%d\n",
227                            i, scat_req->scat_list[i].buf,
228                            scat_req->scat_list[i].len);
229
230                 sg_set_buf(sg, scat_req->scat_list[i].buf,
231                            scat_req->scat_list[i].len);
232         }
233
234         /* set scatter-gather table for request */
235         data->sg = scat_req->sgentries;
236         data->sg_len = scat_req->scat_entries;
237 }
238
239 static int ath6kl_sdio_scat_rw(struct ath6kl_sdio *ar_sdio,
240                                struct bus_request *req)
241 {
242         struct mmc_request mmc_req;
243         struct mmc_command cmd;
244         struct mmc_data data;
245         struct hif_scatter_req *scat_req;
246         u8 opcode, rw;
247         int status, len;
248
249         scat_req = req->scat_req;
250
251         if (scat_req->virt_scat) {
252                 len = scat_req->len;
253                 if (scat_req->req & HIF_BLOCK_BASIS)
254                         len = round_down(len, HIF_MBOX_BLOCK_SIZE);
255
256                 status = ath6kl_sdio_io(ar_sdio->func, scat_req->req,
257                                         scat_req->addr, scat_req->virt_dma_buf,
258                                         len);
259                 goto scat_complete;
260         }
261
262         memset(&mmc_req, 0, sizeof(struct mmc_request));
263         memset(&cmd, 0, sizeof(struct mmc_command));
264         memset(&data, 0, sizeof(struct mmc_data));
265
266         ath6kl_sdio_setup_scat_data(scat_req, &data);
267
268         opcode = (scat_req->req & HIF_FIXED_ADDRESS) ?
269                   CMD53_ARG_FIXED_ADDRESS : CMD53_ARG_INCR_ADDRESS;
270
271         rw = (scat_req->req & HIF_WRITE) ? CMD53_ARG_WRITE : CMD53_ARG_READ;
272
273         /* Fixup the address so that the last byte will fall on MBOX EOM */
274         if (scat_req->req & HIF_WRITE) {
275                 if (scat_req->addr == HIF_MBOX_BASE_ADDR)
276                         scat_req->addr += HIF_MBOX_WIDTH - scat_req->len;
277                 else
278                         /* Uses extended address range */
279                         scat_req->addr += HIF_MBOX0_EXT_WIDTH - scat_req->len;
280         }
281
282         /* set command argument */
283         ath6kl_sdio_set_cmd53_arg(&cmd.arg, rw, ar_sdio->func->num,
284                                   CMD53_ARG_BLOCK_BASIS, opcode, scat_req->addr,
285                                   data.blocks);
286
287         cmd.opcode = SD_IO_RW_EXTENDED;
288         cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_ADTC;
289
290         mmc_req.cmd = &cmd;
291         mmc_req.data = &data;
292
293         mmc_set_data_timeout(&data, ar_sdio->func->card);
294         /* synchronous call to process request */
295         mmc_wait_for_req(ar_sdio->func->card->host, &mmc_req);
296
297         status = cmd.error ? cmd.error : data.error;
298
299 scat_complete:
300         scat_req->status = status;
301
302         if (scat_req->status)
303                 ath6kl_err("Scatter write request failed:%d\n",
304                            scat_req->status);
305
306         if (scat_req->req & HIF_ASYNCHRONOUS)
307                 scat_req->complete(ar_sdio->ar->htc_target, scat_req);
308
309         return status;
310 }
311
312 static int ath6kl_sdio_alloc_prep_scat_req(struct ath6kl_sdio *ar_sdio,
313                                            int n_scat_entry, int n_scat_req,
314                                            bool virt_scat)
315 {
316         struct hif_scatter_req *s_req;
317         struct bus_request *bus_req;
318         int i, scat_req_sz, scat_list_sz, sg_sz, buf_sz;
319         u8 *virt_buf;
320
321         scat_list_sz = (n_scat_entry - 1) * sizeof(struct hif_scatter_item);
322         scat_req_sz = sizeof(*s_req) + scat_list_sz;
323
324         if (!virt_scat)
325                 sg_sz = sizeof(struct scatterlist) * n_scat_entry;
326         else
327                 buf_sz =  2 * L1_CACHE_BYTES +
328                           ATH6KL_MAX_TRANSFER_SIZE_PER_SCATTER;
329
330         for (i = 0; i < n_scat_req; i++) {
331                 /* allocate the scatter request */
332                 s_req = kzalloc(scat_req_sz, GFP_KERNEL);
333                 if (!s_req)
334                         return -ENOMEM;
335
336                 if (virt_scat) {
337                         virt_buf = kzalloc(buf_sz, GFP_KERNEL);
338                         if (!virt_buf) {
339                                 kfree(s_req);
340                                 return -ENOMEM;
341                         }
342
343                         s_req->virt_dma_buf =
344                                 (u8 *)L1_CACHE_ALIGN((unsigned long)virt_buf);
345                 } else {
346                         /* allocate sglist */
347                         s_req->sgentries = kzalloc(sg_sz, GFP_KERNEL);
348
349                         if (!s_req->sgentries) {
350                                 kfree(s_req);
351                                 return -ENOMEM;
352                         }
353                 }
354
355                 /* allocate a bus request for this scatter request */
356                 bus_req = ath6kl_sdio_alloc_busreq(ar_sdio);
357                 if (!bus_req) {
358                         kfree(s_req->sgentries);
359                         kfree(s_req->virt_dma_buf);
360                         kfree(s_req);
361                         return -ENOMEM;
362                 }
363
364                 /* assign the scatter request to this bus request */
365                 bus_req->scat_req = s_req;
366                 s_req->busrequest = bus_req;
367
368                 s_req->virt_scat = virt_scat;
369
370                 /* add it to the scatter pool */
371                 hif_scatter_req_add(ar_sdio->ar, s_req);
372         }
373
374         return 0;
375 }
376
377 static int ath6kl_sdio_read_write_sync(struct ath6kl *ar, u32 addr, u8 *buf,
378                                        u32 len, u32 request)
379 {
380         struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
381         u8  *tbuf = NULL;
382         int ret;
383         bool bounced = false;
384
385         if (request & HIF_BLOCK_BASIS)
386                 len = round_down(len, HIF_MBOX_BLOCK_SIZE);
387
388         if (buf_needs_bounce(buf)) {
389                 if (!ar_sdio->dma_buffer)
390                         return -ENOMEM;
391                 tbuf = ar_sdio->dma_buffer;
392                 memcpy(tbuf, buf, len);
393                 bounced = true;
394         } else
395                 tbuf = buf;
396
397         sdio_claim_host(ar_sdio->func);
398         ret = ath6kl_sdio_io(ar_sdio->func, request, addr, tbuf, len);
399         if ((request & HIF_READ) && bounced)
400                 memcpy(buf, tbuf, len);
401         sdio_release_host(ar_sdio->func);
402
403         return ret;
404 }
405
406 static void __ath6kl_sdio_write_async(struct ath6kl_sdio *ar_sdio,
407                                       struct bus_request *req)
408 {
409         if (req->scat_req)
410                 ath6kl_sdio_scat_rw(ar_sdio, req);
411         else {
412                 void *context;
413                 int status;
414
415                 status = ath6kl_sdio_read_write_sync(ar_sdio->ar, req->address,
416                                                      req->buffer, req->length,
417                                                      req->request);
418                 context = req->packet;
419                 ath6kl_sdio_free_bus_req(ar_sdio, req);
420                 ath6kldev_rw_comp_handler(context, status);
421         }
422 }
423
424 static void ath6kl_sdio_write_async_work(struct work_struct *work)
425 {
426         struct ath6kl_sdio *ar_sdio;
427         unsigned long flags;
428         struct bus_request *req, *tmp_req;
429
430         ar_sdio = container_of(work, struct ath6kl_sdio, wr_async_work);
431         sdio_claim_host(ar_sdio->func);
432
433         spin_lock_irqsave(&ar_sdio->wr_async_lock, flags);
434         list_for_each_entry_safe(req, tmp_req, &ar_sdio->wr_asyncq, list) {
435                 list_del(&req->list);
436                 spin_unlock_irqrestore(&ar_sdio->wr_async_lock, flags);
437                 __ath6kl_sdio_write_async(ar_sdio, req);
438                 spin_lock_irqsave(&ar_sdio->wr_async_lock, flags);
439         }
440         spin_unlock_irqrestore(&ar_sdio->wr_async_lock, flags);
441
442         sdio_release_host(ar_sdio->func);
443 }
444
445 static void ath6kl_sdio_irq_handler(struct sdio_func *func)
446 {
447         int status;
448         struct ath6kl_sdio *ar_sdio;
449
450         ath6kl_dbg(ATH6KL_DBG_SDIO, "irq\n");
451
452         ar_sdio = sdio_get_drvdata(func);
453         atomic_set(&ar_sdio->irq_handling, 1);
454
455         /*
456          * Release the host during interrups so we can pick it back up when
457          * we process commands.
458          */
459         sdio_release_host(ar_sdio->func);
460
461         status = ath6kldev_intr_bh_handler(ar_sdio->ar);
462         sdio_claim_host(ar_sdio->func);
463         atomic_set(&ar_sdio->irq_handling, 0);
464         WARN_ON(status && status != -ECANCELED);
465 }
466
467 static int ath6kl_sdio_power_on(struct ath6kl_sdio *ar_sdio)
468 {
469         struct sdio_func *func = ar_sdio->func;
470         int ret = 0;
471
472         if (!ar_sdio->is_disabled)
473                 return 0;
474
475         sdio_claim_host(func);
476
477         ret = sdio_enable_func(func);
478         if (ret) {
479                 ath6kl_err("Unable to enable sdio func: %d)\n", ret);
480                 sdio_release_host(func);
481                 return ret;
482         }
483
484         sdio_release_host(func);
485
486         /*
487          * Wait for hardware to initialise. It should take a lot less than
488          * 10 ms but let's be conservative here.
489          */
490         msleep(10);
491
492         ar_sdio->is_disabled = false;
493
494         return ret;
495 }
496
497 static int ath6kl_sdio_power_off(struct ath6kl_sdio *ar_sdio)
498 {
499         int ret;
500
501         if (ar_sdio->is_disabled)
502                 return 0;
503
504         /* Disable the card */
505         sdio_claim_host(ar_sdio->func);
506         ret = sdio_disable_func(ar_sdio->func);
507         sdio_release_host(ar_sdio->func);
508
509         if (ret)
510                 return ret;
511
512         ar_sdio->is_disabled = true;
513
514         return ret;
515 }
516
517 static int ath6kl_sdio_write_async(struct ath6kl *ar, u32 address, u8 *buffer,
518                                    u32 length, u32 request,
519                                    struct htc_packet *packet)
520 {
521         struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
522         struct bus_request *bus_req;
523         unsigned long flags;
524
525         bus_req = ath6kl_sdio_alloc_busreq(ar_sdio);
526
527         if (!bus_req)
528                 return -ENOMEM;
529
530         bus_req->address = address;
531         bus_req->buffer = buffer;
532         bus_req->length = length;
533         bus_req->request = request;
534         bus_req->packet = packet;
535
536         spin_lock_irqsave(&ar_sdio->wr_async_lock, flags);
537         list_add_tail(&bus_req->list, &ar_sdio->wr_asyncq);
538         spin_unlock_irqrestore(&ar_sdio->wr_async_lock, flags);
539         queue_work(ar->ath6kl_wq, &ar_sdio->wr_async_work);
540
541         return 0;
542 }
543
544 static void ath6kl_sdio_irq_enable(struct ath6kl *ar)
545 {
546         struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
547         int ret;
548
549         sdio_claim_host(ar_sdio->func);
550
551         /* Register the isr */
552         ret =  sdio_claim_irq(ar_sdio->func, ath6kl_sdio_irq_handler);
553         if (ret)
554                 ath6kl_err("Failed to claim sdio irq: %d\n", ret);
555
556         sdio_release_host(ar_sdio->func);
557 }
558
559 static void ath6kl_sdio_irq_disable(struct ath6kl *ar)
560 {
561         struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
562         int ret;
563
564         sdio_claim_host(ar_sdio->func);
565
566         /* Mask our function IRQ */
567         while (atomic_read(&ar_sdio->irq_handling)) {
568                 sdio_release_host(ar_sdio->func);
569                 schedule_timeout(HZ / 10);
570                 sdio_claim_host(ar_sdio->func);
571         }
572
573         ret = sdio_release_irq(ar_sdio->func);
574         if (ret)
575                 ath6kl_err("Failed to release sdio irq: %d\n", ret);
576
577         sdio_release_host(ar_sdio->func);
578 }
579
580 static struct hif_scatter_req *ath6kl_sdio_scatter_req_get(struct ath6kl *ar)
581 {
582         struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
583         struct hif_scatter_req *node = NULL;
584         unsigned long flag;
585
586         spin_lock_irqsave(&ar_sdio->scat_lock, flag);
587
588         if (!list_empty(&ar_sdio->scat_req)) {
589                 node = list_first_entry(&ar_sdio->scat_req,
590                                         struct hif_scatter_req, list);
591                 list_del(&node->list);
592         }
593
594         spin_unlock_irqrestore(&ar_sdio->scat_lock, flag);
595
596         return node;
597 }
598
599 static void ath6kl_sdio_scatter_req_add(struct ath6kl *ar,
600                                         struct hif_scatter_req *s_req)
601 {
602         struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
603         unsigned long flag;
604
605         spin_lock_irqsave(&ar_sdio->scat_lock, flag);
606
607         list_add_tail(&s_req->list, &ar_sdio->scat_req);
608
609         spin_unlock_irqrestore(&ar_sdio->scat_lock, flag);
610
611 }
612
613 /* scatter gather read write request */
614 static int ath6kl_sdio_async_rw_scatter(struct ath6kl *ar,
615                                         struct hif_scatter_req *scat_req)
616 {
617         struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
618         u32 request = scat_req->req;
619         int status = 0;
620         unsigned long flags;
621
622         if (!scat_req->len)
623                 return -EINVAL;
624
625         ath6kl_dbg(ATH6KL_DBG_SCATTER,
626                 "hif-scatter: total len: %d scatter entries: %d\n",
627                 scat_req->len, scat_req->scat_entries);
628
629         if (request & HIF_SYNCHRONOUS) {
630                 sdio_claim_host(ar_sdio->func);
631                 status = ath6kl_sdio_scat_rw(ar_sdio, scat_req->busrequest);
632                 sdio_release_host(ar_sdio->func);
633         } else {
634                 spin_lock_irqsave(&ar_sdio->wr_async_lock, flags);
635                 list_add_tail(&scat_req->busrequest->list, &ar_sdio->wr_asyncq);
636                 spin_unlock_irqrestore(&ar_sdio->wr_async_lock, flags);
637                 queue_work(ar->ath6kl_wq, &ar_sdio->wr_async_work);
638         }
639
640         return status;
641 }
642
643 /* clean up scatter support */
644 static void ath6kl_sdio_cleanup_scatter(struct ath6kl *ar)
645 {
646         struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
647         struct hif_scatter_req *s_req, *tmp_req;
648         unsigned long flag;
649
650         /* empty the free list */
651         spin_lock_irqsave(&ar_sdio->scat_lock, flag);
652         list_for_each_entry_safe(s_req, tmp_req, &ar_sdio->scat_req, list) {
653                 list_del(&s_req->list);
654                 spin_unlock_irqrestore(&ar_sdio->scat_lock, flag);
655
656                 if (s_req->busrequest)
657                         ath6kl_sdio_free_bus_req(ar_sdio, s_req->busrequest);
658                 kfree(s_req->virt_dma_buf);
659                 kfree(s_req->sgentries);
660                 kfree(s_req);
661
662                 spin_lock_irqsave(&ar_sdio->scat_lock, flag);
663         }
664         spin_unlock_irqrestore(&ar_sdio->scat_lock, flag);
665 }
666
667 /* setup of HIF scatter resources */
668 static int ath6kl_sdio_enable_scatter(struct ath6kl *ar)
669 {
670         struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
671         struct htc_target *target = ar->htc_target;
672         int ret;
673         bool virt_scat = false;
674
675         /* check if host supports scatter and it meets our requirements */
676         if (ar_sdio->func->card->host->max_segs < MAX_SCATTER_ENTRIES_PER_REQ) {
677                 ath6kl_err("host only supports scatter of :%d entries, need: %d\n",
678                            ar_sdio->func->card->host->max_segs,
679                            MAX_SCATTER_ENTRIES_PER_REQ);
680                 virt_scat = true;
681         }
682
683         if (!virt_scat) {
684                 ret = ath6kl_sdio_alloc_prep_scat_req(ar_sdio,
685                                 MAX_SCATTER_ENTRIES_PER_REQ,
686                                 MAX_SCATTER_REQUESTS, virt_scat);
687
688                 if (!ret) {
689                         ath6kl_dbg(ATH6KL_DBG_SCATTER,
690                                    "hif-scatter enabled: max scatter req : %d entries: %d\n",
691                                    MAX_SCATTER_REQUESTS,
692                                    MAX_SCATTER_ENTRIES_PER_REQ);
693
694                         target->max_scat_entries = MAX_SCATTER_ENTRIES_PER_REQ;
695                         target->max_xfer_szper_scatreq =
696                                                 MAX_SCATTER_REQ_TRANSFER_SIZE;
697                 } else {
698                         ath6kl_sdio_cleanup_scatter(ar);
699                         ath6kl_warn("hif scatter resource setup failed, trying virtual scatter method\n");
700                 }
701         }
702
703         if (virt_scat || ret) {
704                 ret = ath6kl_sdio_alloc_prep_scat_req(ar_sdio,
705                                 ATH6KL_SCATTER_ENTRIES_PER_REQ,
706                                 ATH6KL_SCATTER_REQS, virt_scat);
707
708                 if (ret) {
709                         ath6kl_err("failed to alloc virtual scatter resources !\n");
710                         ath6kl_sdio_cleanup_scatter(ar);
711                         return ret;
712                 }
713
714                 ath6kl_dbg(ATH6KL_DBG_SCATTER,
715                            "Vitual scatter enabled, max_scat_req:%d, entries:%d\n",
716                            ATH6KL_SCATTER_REQS, ATH6KL_SCATTER_ENTRIES_PER_REQ);
717
718                 target->max_scat_entries = ATH6KL_SCATTER_ENTRIES_PER_REQ;
719                 target->max_xfer_szper_scatreq =
720                                         ATH6KL_MAX_TRANSFER_SIZE_PER_SCATTER;
721         }
722
723         return 0;
724 }
725
726 static int ath6kl_sdio_suspend(struct ath6kl *ar)
727 {
728         struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
729         struct sdio_func *func = ar_sdio->func;
730         mmc_pm_flag_t flags;
731         int ret;
732
733         flags = sdio_get_host_pm_caps(func);
734
735         if (!(flags & MMC_PM_KEEP_POWER))
736                 /* as host doesn't support keep power we need to bail out */
737                 ath6kl_dbg(ATH6KL_DBG_SDIO,
738                            "func %d doesn't support MMC_PM_KEEP_POWER\n",
739                            func->num);
740                 return -EINVAL;
741
742         ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
743         if (ret) {
744                 printk(KERN_ERR "ath6kl: set sdio pm flags failed: %d\n",
745                        ret);
746                 return ret;
747         }
748
749         ath6kl_deep_sleep_enable(ar);
750
751         return 0;
752 }
753
754 static const struct ath6kl_hif_ops ath6kl_sdio_ops = {
755         .read_write_sync = ath6kl_sdio_read_write_sync,
756         .write_async = ath6kl_sdio_write_async,
757         .irq_enable = ath6kl_sdio_irq_enable,
758         .irq_disable = ath6kl_sdio_irq_disable,
759         .scatter_req_get = ath6kl_sdio_scatter_req_get,
760         .scatter_req_add = ath6kl_sdio_scatter_req_add,
761         .enable_scatter = ath6kl_sdio_enable_scatter,
762         .scat_req_rw = ath6kl_sdio_async_rw_scatter,
763         .cleanup_scatter = ath6kl_sdio_cleanup_scatter,
764         .suspend = ath6kl_sdio_suspend,
765 };
766
767 static int ath6kl_sdio_probe(struct sdio_func *func,
768                              const struct sdio_device_id *id)
769 {
770         int ret;
771         struct ath6kl_sdio *ar_sdio;
772         struct ath6kl *ar;
773         int count;
774
775         ath6kl_dbg(ATH6KL_DBG_SDIO,
776                    "new func %d vendor 0x%x device 0x%x block 0x%x/0x%x\n",
777                    func->num, func->vendor, func->device,
778                    func->max_blksize, func->cur_blksize);
779
780         ar_sdio = kzalloc(sizeof(struct ath6kl_sdio), GFP_KERNEL);
781         if (!ar_sdio)
782                 return -ENOMEM;
783
784         ar_sdio->dma_buffer = kzalloc(HIF_DMA_BUFFER_SIZE, GFP_KERNEL);
785         if (!ar_sdio->dma_buffer) {
786                 ret = -ENOMEM;
787                 goto err_hif;
788         }
789
790         ar_sdio->func = func;
791         sdio_set_drvdata(func, ar_sdio);
792
793         ar_sdio->id = id;
794         ar_sdio->is_disabled = true;
795
796         spin_lock_init(&ar_sdio->lock);
797         spin_lock_init(&ar_sdio->scat_lock);
798         spin_lock_init(&ar_sdio->wr_async_lock);
799
800         INIT_LIST_HEAD(&ar_sdio->scat_req);
801         INIT_LIST_HEAD(&ar_sdio->bus_req_freeq);
802         INIT_LIST_HEAD(&ar_sdio->wr_asyncq);
803
804         INIT_WORK(&ar_sdio->wr_async_work, ath6kl_sdio_write_async_work);
805
806         for (count = 0; count < BUS_REQUEST_MAX_NUM; count++)
807                 ath6kl_sdio_free_bus_req(ar_sdio, &ar_sdio->bus_req[count]);
808
809         ar = ath6kl_core_alloc(&ar_sdio->func->dev);
810         if (!ar) {
811                 ath6kl_err("Failed to alloc ath6kl core\n");
812                 ret = -ENOMEM;
813                 goto err_dma;
814         }
815
816         ar_sdio->ar = ar;
817         ar->hif_priv = ar_sdio;
818         ar->hif_ops = &ath6kl_sdio_ops;
819
820         ath6kl_sdio_set_mbox_info(ar);
821
822         sdio_claim_host(func);
823
824         if ((ar_sdio->id->device & MANUFACTURER_ID_ATH6KL_BASE_MASK) >=
825             MANUFACTURER_ID_AR6003_BASE) {
826                 /* enable 4-bit ASYNC interrupt on AR6003 or later */
827                 ret = ath6kl_sdio_func0_cmd52_wr_byte(func->card,
828                                                 CCCR_SDIO_IRQ_MODE_REG,
829                                                 SDIO_IRQ_MODE_ASYNC_4BIT_IRQ);
830                 if (ret) {
831                         ath6kl_err("Failed to enable 4-bit async irq mode %d\n",
832                                    ret);
833                         sdio_release_host(func);
834                         goto err_cfg80211;
835                 }
836
837                 ath6kl_dbg(ATH6KL_DBG_SDIO, "4-bit async irq mode enabled\n");
838         }
839
840         /* give us some time to enable, in ms */
841         func->enable_timeout = 100;
842
843         sdio_release_host(func);
844
845         ret = ath6kl_sdio_power_on(ar_sdio);
846         if (ret)
847                 goto err_cfg80211;
848
849         sdio_claim_host(func);
850
851         ret = sdio_set_block_size(func, HIF_MBOX_BLOCK_SIZE);
852         if (ret) {
853                 ath6kl_err("Set sdio block size %d failed: %d)\n",
854                            HIF_MBOX_BLOCK_SIZE, ret);
855                 sdio_release_host(func);
856                 goto err_off;
857         }
858
859         sdio_release_host(func);
860
861         ret = ath6kl_core_init(ar);
862         if (ret) {
863                 ath6kl_err("Failed to init ath6kl core\n");
864                 goto err_off;
865         }
866
867         return ret;
868
869 err_off:
870         ath6kl_sdio_power_off(ar_sdio);
871 err_cfg80211:
872         ath6kl_cfg80211_deinit(ar_sdio->ar);
873 err_dma:
874         kfree(ar_sdio->dma_buffer);
875 err_hif:
876         kfree(ar_sdio);
877
878         return ret;
879 }
880
881 static void ath6kl_sdio_remove(struct sdio_func *func)
882 {
883         struct ath6kl_sdio *ar_sdio;
884
885         ath6kl_dbg(ATH6KL_DBG_SDIO,
886                    "removed func %d vendor 0x%x device 0x%x\n",
887                    func->num, func->vendor, func->device);
888
889         ar_sdio = sdio_get_drvdata(func);
890
891         ath6kl_stop_txrx(ar_sdio->ar);
892         cancel_work_sync(&ar_sdio->wr_async_work);
893
894         ath6kl_unavail_ev(ar_sdio->ar);
895
896         ath6kl_sdio_power_off(ar_sdio);
897
898         kfree(ar_sdio->dma_buffer);
899         kfree(ar_sdio);
900 }
901
902 static const struct sdio_device_id ath6kl_sdio_devices[] = {
903         {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6003_BASE | 0x0))},
904         {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6003_BASE | 0x1))},
905         {},
906 };
907
908 MODULE_DEVICE_TABLE(sdio, ath6kl_sdio_devices);
909
910 static struct sdio_driver ath6kl_sdio_driver = {
911         .name = "ath6kl_sdio",
912         .id_table = ath6kl_sdio_devices,
913         .probe = ath6kl_sdio_probe,
914         .remove = ath6kl_sdio_remove,
915 };
916
917 static int __init ath6kl_sdio_init(void)
918 {
919         int ret;
920
921         ret = sdio_register_driver(&ath6kl_sdio_driver);
922         if (ret)
923                 ath6kl_err("sdio driver registration failed: %d\n", ret);
924
925         return ret;
926 }
927
928 static void __exit ath6kl_sdio_exit(void)
929 {
930         sdio_unregister_driver(&ath6kl_sdio_driver);
931 }
932
933 module_init(ath6kl_sdio_init);
934 module_exit(ath6kl_sdio_exit);
935
936 MODULE_AUTHOR("Atheros Communications, Inc.");
937 MODULE_DESCRIPTION("Driver support for Atheros AR600x SDIO devices");
938 MODULE_LICENSE("Dual BSD/GPL");
939
940 MODULE_FIRMWARE(AR6003_REV2_OTP_FILE);
941 MODULE_FIRMWARE(AR6003_REV2_FIRMWARE_FILE);
942 MODULE_FIRMWARE(AR6003_REV2_PATCH_FILE);
943 MODULE_FIRMWARE(AR6003_REV2_BOARD_DATA_FILE);
944 MODULE_FIRMWARE(AR6003_REV2_DEFAULT_BOARD_DATA_FILE);
945 MODULE_FIRMWARE(AR6003_REV3_OTP_FILE);
946 MODULE_FIRMWARE(AR6003_REV3_FIRMWARE_FILE);
947 MODULE_FIRMWARE(AR6003_REV3_PATCH_FILE);
948 MODULE_FIRMWARE(AR6003_REV3_BOARD_DATA_FILE);
949 MODULE_FIRMWARE(AR6003_REV3_DEFAULT_BOARD_DATA_FILE);