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