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