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