]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/net/wireless/ath/ath6kl/sdio.c
2a025e69ff89fe6e621c3c7afad6f1fea13c8df3
[karo-tx-linux.git] / drivers / net / wireless / ath / ath6kl / sdio.c
1 /*
2  * Copyright (c) 2004-2011 Atheros Communications Inc.
3  * Copyright (c) 2011-2012 Qualcomm Atheros, Inc.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 #include <linux/module.h>
19 #include <linux/mmc/card.h>
20 #include <linux/mmc/mmc.h>
21 #include <linux/mmc/host.h>
22 #include <linux/mmc/sdio_func.h>
23 #include <linux/mmc/sdio_ids.h>
24 #include <linux/mmc/sdio.h>
25 #include <linux/mmc/sd.h>
26 #include "hif.h"
27 #include "hif-ops.h"
28 #include "target.h"
29 #include "debug.h"
30 #include "cfg80211.h"
31 #include "trace.h"
32
33 struct ath6kl_sdio {
34         struct sdio_func *func;
35
36         /* protects access to bus_req_freeq */
37         spinlock_t lock;
38
39         /* free list */
40         struct list_head bus_req_freeq;
41
42         /* available bus requests */
43         struct bus_request bus_req[BUS_REQUEST_MAX_NUM];
44
45         struct ath6kl *ar;
46
47         u8 *dma_buffer;
48
49         /* protects access to dma_buffer */
50         struct mutex dma_buffer_mutex;
51
52         /* scatter request list head */
53         struct list_head scat_req;
54
55         atomic_t irq_handling;
56         wait_queue_head_t irq_wq;
57
58         /* protects access to scat_req */
59         spinlock_t scat_lock;
60
61         bool scatter_enabled;
62
63         bool is_disabled;
64         const struct sdio_device_id *id;
65         struct work_struct wr_async_work;
66         struct list_head wr_asyncq;
67
68         /* protects access to wr_asyncq */
69         spinlock_t wr_async_lock;
70 };
71
72 #define CMD53_ARG_READ          0
73 #define CMD53_ARG_WRITE         1
74 #define CMD53_ARG_BLOCK_BASIS   1
75 #define CMD53_ARG_FIXED_ADDRESS 0
76 #define CMD53_ARG_INCR_ADDRESS  1
77
78 static inline struct ath6kl_sdio *ath6kl_sdio_priv(struct ath6kl *ar)
79 {
80         return ar->hif_priv;
81 }
82
83 /*
84  * Macro to check if DMA buffer is WORD-aligned and DMA-able.
85  * Most host controllers assume the buffer is DMA'able and will
86  * bug-check otherwise (i.e. buffers on the stack). virt_addr_valid
87  * check fails on stack memory.
88  */
89 static inline bool buf_needs_bounce(u8 *buf)
90 {
91         return ((unsigned long) buf & 0x3) || !virt_addr_valid(buf);
92 }
93
94 static void ath6kl_sdio_set_mbox_info(struct ath6kl *ar)
95 {
96         struct ath6kl_mbox_info *mbox_info = &ar->mbox_info;
97
98         /* EP1 has an extended range */
99         mbox_info->htc_addr = HIF_MBOX_BASE_ADDR;
100         mbox_info->htc_ext_addr = HIF_MBOX0_EXT_BASE_ADDR;
101         mbox_info->htc_ext_sz = HIF_MBOX0_EXT_WIDTH;
102         mbox_info->block_size = HIF_MBOX_BLOCK_SIZE;
103         mbox_info->gmbox_addr = HIF_GMBOX_BASE_ADDR;
104         mbox_info->gmbox_sz = HIF_GMBOX_WIDTH;
105 }
106
107 static inline void ath6kl_sdio_set_cmd53_arg(u32 *arg, u8 rw, u8 func,
108                                              u8 mode, u8 opcode, u32 addr,
109                                              u16 blksz)
110 {
111         *arg = (((rw & 1) << 31) |
112                 ((func & 0x7) << 28) |
113                 ((mode & 1) << 27) |
114                 ((opcode & 1) << 26) |
115                 ((addr & 0x1FFFF) << 9) |
116                 (blksz & 0x1FF));
117 }
118
119 static inline void ath6kl_sdio_set_cmd52_arg(u32 *arg, u8 write, u8 raw,
120                                              unsigned int address,
121                                              unsigned char val)
122 {
123         const u8 func = 0;
124
125         *arg = ((write & 1) << 31) |
126                ((func & 0x7) << 28) |
127                ((raw & 1) << 27) |
128                (1 << 26) |
129                ((address & 0x1FFFF) << 9) |
130                (1 << 8) |
131                (val & 0xFF);
132 }
133
134 static int ath6kl_sdio_func0_cmd52_wr_byte(struct mmc_card *card,
135                                            unsigned int address,
136                                            unsigned char byte)
137 {
138         struct mmc_command io_cmd;
139
140         memset(&io_cmd, 0, sizeof(io_cmd));
141         ath6kl_sdio_set_cmd52_arg(&io_cmd.arg, 1, 0, address, byte);
142         io_cmd.opcode = SD_IO_RW_DIRECT;
143         io_cmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
144
145         return mmc_wait_for_cmd(card->host, &io_cmd, 0);
146 }
147
148 static int ath6kl_sdio_io(struct sdio_func *func, u32 request, u32 addr,
149                           u8 *buf, u32 len)
150 {
151         int ret = 0;
152
153         sdio_claim_host(func);
154
155         if (request & HIF_WRITE) {
156                 /* FIXME: looks like ugly workaround for something */
157                 if (addr >= HIF_MBOX_BASE_ADDR &&
158                     addr <= HIF_MBOX_END_ADDR)
159                         addr += (HIF_MBOX_WIDTH - len);
160
161                 /* FIXME: this also looks like ugly workaround */
162                 if (addr == HIF_MBOX0_EXT_BASE_ADDR)
163                         addr += HIF_MBOX0_EXT_WIDTH - len;
164
165                 if (request & HIF_FIXED_ADDRESS)
166                         ret = sdio_writesb(func, addr, buf, len);
167                 else
168                         ret = sdio_memcpy_toio(func, addr, buf, len);
169         } else {
170                 if (request & HIF_FIXED_ADDRESS)
171                         ret = sdio_readsb(func, buf, addr, len);
172                 else
173                         ret = sdio_memcpy_fromio(func, buf, addr, len);
174         }
175
176         sdio_release_host(func);
177
178         ath6kl_dbg(ATH6KL_DBG_SDIO, "%s addr 0x%x%s buf 0x%p len %d\n",
179                    request & HIF_WRITE ? "wr" : "rd", addr,
180                    request & HIF_FIXED_ADDRESS ? " (fixed)" : "", buf, len);
181         ath6kl_dbg_dump(ATH6KL_DBG_SDIO_DUMP, NULL, "sdio ", buf, len);
182
183         trace_ath6kl_sdio(addr, request, buf, len);
184
185         return ret;
186 }
187
188 static struct bus_request *ath6kl_sdio_alloc_busreq(struct ath6kl_sdio *ar_sdio)
189 {
190         struct bus_request *bus_req;
191
192         spin_lock_bh(&ar_sdio->lock);
193
194         if (list_empty(&ar_sdio->bus_req_freeq)) {
195                 spin_unlock_bh(&ar_sdio->lock);
196                 return NULL;
197         }
198
199         bus_req = list_first_entry(&ar_sdio->bus_req_freeq,
200                                    struct bus_request, list);
201         list_del(&bus_req->list);
202
203         spin_unlock_bh(&ar_sdio->lock);
204         ath6kl_dbg(ATH6KL_DBG_SCATTER, "%s: bus request 0x%p\n",
205                    __func__, bus_req);
206
207         return bus_req;
208 }
209
210 static void ath6kl_sdio_free_bus_req(struct ath6kl_sdio *ar_sdio,
211                                      struct bus_request *bus_req)
212 {
213         ath6kl_dbg(ATH6KL_DBG_SCATTER, "%s: bus request 0x%p\n",
214                    __func__, bus_req);
215
216         spin_lock_bh(&ar_sdio->lock);
217         list_add_tail(&bus_req->list, &ar_sdio->bus_req_freeq);
218         spin_unlock_bh(&ar_sdio->lock);
219 }
220
221 static void ath6kl_sdio_setup_scat_data(struct hif_scatter_req *scat_req,
222                                         struct mmc_data *data)
223 {
224         struct scatterlist *sg;
225         struct hif_scatter_item *scat_list;
226         int i;
227
228         data->blksz = HIF_MBOX_BLOCK_SIZE;
229         data->blocks = scat_req->len / HIF_MBOX_BLOCK_SIZE;
230
231         ath6kl_dbg(ATH6KL_DBG_SCATTER,
232                    "hif-scatter: (%s) addr: 0x%X, (block len: %d, block count: %d) , (tot:%d,sg:%d)\n",
233                    (scat_req->req & HIF_WRITE) ? "WR" : "RD", scat_req->addr,
234                    data->blksz, data->blocks, scat_req->len,
235                    scat_req->scat_entries);
236
237         data->flags = (scat_req->req & HIF_WRITE) ? MMC_DATA_WRITE :
238                                                     MMC_DATA_READ;
239
240         /* fill SG entries */
241         sg = scat_req->sgentries;
242         sg_init_table(sg, scat_req->scat_entries);
243
244         scat_list = &scat_req->scat_list[0];
245
246         /* assemble SG list */
247         for (i = 0; i < scat_req->scat_entries; i++, sg++, scat_list++) {
248                 ath6kl_dbg(ATH6KL_DBG_SCATTER, "%d: addr:0x%p, len:%d\n",
249                            i, scat_list->buf, scat_list->len);
250
251                 sg_set_buf(sg, scat_list->buf, scat_list->len);
252         }
253
254         /* set scatter-gather table for request */
255         data->sg = scat_req->sgentries;
256         data->sg_len = scat_req->scat_entries;
257 }
258
259 static int ath6kl_sdio_scat_rw(struct ath6kl_sdio *ar_sdio,
260                                struct bus_request *req)
261 {
262         struct mmc_request mmc_req;
263         struct mmc_command cmd;
264         struct mmc_data data;
265         struct hif_scatter_req *scat_req;
266         u8 opcode, rw;
267         int status, len;
268
269         scat_req = req->scat_req;
270
271         if (scat_req->virt_scat) {
272                 len = scat_req->len;
273                 if (scat_req->req & HIF_BLOCK_BASIS)
274                         len = round_down(len, HIF_MBOX_BLOCK_SIZE);
275
276                 status = ath6kl_sdio_io(ar_sdio->func, scat_req->req,
277                                         scat_req->addr, scat_req->virt_dma_buf,
278                                         len);
279                 goto scat_complete;
280         }
281
282         memset(&mmc_req, 0, sizeof(struct mmc_request));
283         memset(&cmd, 0, sizeof(struct mmc_command));
284         memset(&data, 0, sizeof(struct mmc_data));
285
286         ath6kl_sdio_setup_scat_data(scat_req, &data);
287
288         opcode = (scat_req->req & HIF_FIXED_ADDRESS) ?
289                   CMD53_ARG_FIXED_ADDRESS : CMD53_ARG_INCR_ADDRESS;
290
291         rw = (scat_req->req & HIF_WRITE) ? CMD53_ARG_WRITE : CMD53_ARG_READ;
292
293         /* Fixup the address so that the last byte will fall on MBOX EOM */
294         if (scat_req->req & HIF_WRITE) {
295                 if (scat_req->addr == HIF_MBOX_BASE_ADDR)
296                         scat_req->addr += HIF_MBOX_WIDTH - scat_req->len;
297                 else
298                         /* Uses extended address range */
299                         scat_req->addr += HIF_MBOX0_EXT_WIDTH - scat_req->len;
300         }
301
302         /* set command argument */
303         ath6kl_sdio_set_cmd53_arg(&cmd.arg, rw, ar_sdio->func->num,
304                                   CMD53_ARG_BLOCK_BASIS, opcode, scat_req->addr,
305                                   data.blocks);
306
307         cmd.opcode = SD_IO_RW_EXTENDED;
308         cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_ADTC;
309
310         mmc_req.cmd = &cmd;
311         mmc_req.data = &data;
312
313         sdio_claim_host(ar_sdio->func);
314
315         mmc_set_data_timeout(&data, ar_sdio->func->card);
316
317         trace_ath6kl_sdio_scat(scat_req->addr,
318                                scat_req->req,
319                                scat_req->len,
320                                scat_req->scat_entries,
321                                scat_req->scat_list);
322
323         /* synchronous call to process request */
324         mmc_wait_for_req(ar_sdio->func->card->host, &mmc_req);
325
326         sdio_release_host(ar_sdio->func);
327
328         status = cmd.error ? cmd.error : data.error;
329
330 scat_complete:
331         scat_req->status = status;
332
333         if (scat_req->status)
334                 ath6kl_err("Scatter write request failed:%d\n",
335                            scat_req->status);
336
337         if (scat_req->req & HIF_ASYNCHRONOUS)
338                 scat_req->complete(ar_sdio->ar->htc_target, scat_req);
339
340         return status;
341 }
342
343 static int ath6kl_sdio_alloc_prep_scat_req(struct ath6kl_sdio *ar_sdio,
344                                            int n_scat_entry, int n_scat_req,
345                                            bool virt_scat)
346 {
347         struct hif_scatter_req *s_req;
348         struct bus_request *bus_req;
349         int i, scat_req_sz, scat_list_sz, size;
350         u8 *virt_buf;
351
352         scat_list_sz = n_scat_entry * sizeof(struct hif_scatter_item);
353         scat_req_sz = sizeof(*s_req) + scat_list_sz;
354
355         if (!virt_scat)
356                 size = sizeof(struct scatterlist) * n_scat_entry;
357         else
358                 size =  2 * L1_CACHE_BYTES +
359                         ATH6KL_MAX_TRANSFER_SIZE_PER_SCATTER;
360
361         for (i = 0; i < n_scat_req; i++) {
362                 /* allocate the scatter request */
363                 s_req = kzalloc(scat_req_sz, GFP_KERNEL);
364                 if (!s_req)
365                         return -ENOMEM;
366
367                 if (virt_scat) {
368                         virt_buf = kzalloc(size, GFP_KERNEL);
369                         if (!virt_buf) {
370                                 kfree(s_req);
371                                 return -ENOMEM;
372                         }
373
374                         s_req->virt_dma_buf =
375                                 (u8 *)L1_CACHE_ALIGN((unsigned long)virt_buf);
376                 } else {
377                         /* allocate sglist */
378                         s_req->sgentries = kzalloc(size, GFP_KERNEL);
379
380                         if (!s_req->sgentries) {
381                                 kfree(s_req);
382                                 return -ENOMEM;
383                         }
384                 }
385
386                 /* allocate a bus request for this scatter request */
387                 bus_req = ath6kl_sdio_alloc_busreq(ar_sdio);
388                 if (!bus_req) {
389                         kfree(s_req->sgentries);
390                         kfree(s_req->virt_dma_buf);
391                         kfree(s_req);
392                         return -ENOMEM;
393                 }
394
395                 /* assign the scatter request to this bus request */
396                 bus_req->scat_req = s_req;
397                 s_req->busrequest = bus_req;
398
399                 s_req->virt_scat = virt_scat;
400
401                 /* add it to the scatter pool */
402                 hif_scatter_req_add(ar_sdio->ar, s_req);
403         }
404
405         return 0;
406 }
407
408 static int ath6kl_sdio_read_write_sync(struct ath6kl *ar, u32 addr, u8 *buf,
409                                        u32 len, u32 request)
410 {
411         struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
412         u8  *tbuf = NULL;
413         int ret;
414         bool bounced = false;
415
416         if (request & HIF_BLOCK_BASIS)
417                 len = round_down(len, HIF_MBOX_BLOCK_SIZE);
418
419         if (buf_needs_bounce(buf)) {
420                 if (!ar_sdio->dma_buffer)
421                         return -ENOMEM;
422                 mutex_lock(&ar_sdio->dma_buffer_mutex);
423                 tbuf = ar_sdio->dma_buffer;
424
425                 if (request & HIF_WRITE)
426                         memcpy(tbuf, buf, len);
427
428                 bounced = true;
429         } else {
430                 tbuf = buf;
431         }
432
433         ret = ath6kl_sdio_io(ar_sdio->func, request, addr, tbuf, len);
434         if ((request & HIF_READ) && bounced)
435                 memcpy(buf, tbuf, len);
436
437         if (bounced)
438                 mutex_unlock(&ar_sdio->dma_buffer_mutex);
439
440         return ret;
441 }
442
443 static void __ath6kl_sdio_write_async(struct ath6kl_sdio *ar_sdio,
444                                       struct bus_request *req)
445 {
446         if (req->scat_req) {
447                 ath6kl_sdio_scat_rw(ar_sdio, req);
448         } else {
449                 void *context;
450                 int status;
451
452                 status = ath6kl_sdio_read_write_sync(ar_sdio->ar, req->address,
453                                                      req->buffer, req->length,
454                                                      req->request);
455                 context = req->packet;
456                 ath6kl_sdio_free_bus_req(ar_sdio, req);
457                 ath6kl_hif_rw_comp_handler(context, status);
458         }
459 }
460
461 static void ath6kl_sdio_write_async_work(struct work_struct *work)
462 {
463         struct ath6kl_sdio *ar_sdio;
464         struct bus_request *req, *tmp_req;
465
466         ar_sdio = container_of(work, struct ath6kl_sdio, wr_async_work);
467
468         spin_lock_bh(&ar_sdio->wr_async_lock);
469         list_for_each_entry_safe(req, tmp_req, &ar_sdio->wr_asyncq, list) {
470                 list_del(&req->list);
471                 spin_unlock_bh(&ar_sdio->wr_async_lock);
472                 __ath6kl_sdio_write_async(ar_sdio, req);
473                 spin_lock_bh(&ar_sdio->wr_async_lock);
474         }
475         spin_unlock_bh(&ar_sdio->wr_async_lock);
476 }
477
478 static void ath6kl_sdio_irq_handler(struct sdio_func *func)
479 {
480         int status;
481         struct ath6kl_sdio *ar_sdio;
482
483         ath6kl_dbg(ATH6KL_DBG_SDIO, "irq\n");
484
485         ar_sdio = sdio_get_drvdata(func);
486         atomic_set(&ar_sdio->irq_handling, 1);
487         /*
488          * Release the host during interrups so we can pick it back up when
489          * we process commands.
490          */
491         sdio_release_host(ar_sdio->func);
492
493         status = ath6kl_hif_intr_bh_handler(ar_sdio->ar);
494         sdio_claim_host(ar_sdio->func);
495
496         atomic_set(&ar_sdio->irq_handling, 0);
497         wake_up(&ar_sdio->irq_wq);
498
499         WARN_ON(status && status != -ECANCELED);
500 }
501
502 static int ath6kl_sdio_power_on(struct ath6kl *ar)
503 {
504         struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
505         struct sdio_func *func = ar_sdio->func;
506         int ret = 0;
507
508         if (!ar_sdio->is_disabled)
509                 return 0;
510
511         ath6kl_dbg(ATH6KL_DBG_BOOT, "sdio power on\n");
512
513         sdio_claim_host(func);
514
515         ret = sdio_enable_func(func);
516         if (ret) {
517                 ath6kl_err("Unable to enable sdio func: %d)\n", ret);
518                 sdio_release_host(func);
519                 return ret;
520         }
521
522         sdio_release_host(func);
523
524         /*
525          * Wait for hardware to initialise. It should take a lot less than
526          * 10 ms but let's be conservative here.
527          */
528         msleep(10);
529
530         ar_sdio->is_disabled = false;
531
532         return ret;
533 }
534
535 static int ath6kl_sdio_power_off(struct ath6kl *ar)
536 {
537         struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
538         int ret;
539
540         if (ar_sdio->is_disabled)
541                 return 0;
542
543         ath6kl_dbg(ATH6KL_DBG_BOOT, "sdio power off\n");
544
545         /* Disable the card */
546         sdio_claim_host(ar_sdio->func);
547         ret = sdio_disable_func(ar_sdio->func);
548         sdio_release_host(ar_sdio->func);
549
550         if (ret)
551                 return ret;
552
553         ar_sdio->is_disabled = true;
554
555         return ret;
556 }
557
558 static int ath6kl_sdio_write_async(struct ath6kl *ar, u32 address, u8 *buffer,
559                                    u32 length, u32 request,
560                                    struct htc_packet *packet)
561 {
562         struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
563         struct bus_request *bus_req;
564
565         bus_req = ath6kl_sdio_alloc_busreq(ar_sdio);
566
567         if (WARN_ON_ONCE(!bus_req))
568                 return -ENOMEM;
569
570         bus_req->address = address;
571         bus_req->buffer = buffer;
572         bus_req->length = length;
573         bus_req->request = request;
574         bus_req->packet = packet;
575
576         spin_lock_bh(&ar_sdio->wr_async_lock);
577         list_add_tail(&bus_req->list, &ar_sdio->wr_asyncq);
578         spin_unlock_bh(&ar_sdio->wr_async_lock);
579         queue_work(ar->ath6kl_wq, &ar_sdio->wr_async_work);
580
581         return 0;
582 }
583
584 static void ath6kl_sdio_irq_enable(struct ath6kl *ar)
585 {
586         struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
587         int ret;
588
589         sdio_claim_host(ar_sdio->func);
590
591         /* Register the isr */
592         ret =  sdio_claim_irq(ar_sdio->func, ath6kl_sdio_irq_handler);
593         if (ret)
594                 ath6kl_err("Failed to claim sdio irq: %d\n", ret);
595
596         sdio_release_host(ar_sdio->func);
597 }
598
599 static bool ath6kl_sdio_is_on_irq(struct ath6kl *ar)
600 {
601         struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
602
603         return !atomic_read(&ar_sdio->irq_handling);
604 }
605
606 static void ath6kl_sdio_irq_disable(struct ath6kl *ar)
607 {
608         struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
609         int ret;
610
611         sdio_claim_host(ar_sdio->func);
612
613         if (atomic_read(&ar_sdio->irq_handling)) {
614                 sdio_release_host(ar_sdio->func);
615
616                 ret = wait_event_interruptible(ar_sdio->irq_wq,
617                                                ath6kl_sdio_is_on_irq(ar));
618                 if (ret)
619                         return;
620
621                 sdio_claim_host(ar_sdio->func);
622         }
623
624         ret = sdio_release_irq(ar_sdio->func);
625         if (ret)
626                 ath6kl_err("Failed to release sdio irq: %d\n", ret);
627
628         sdio_release_host(ar_sdio->func);
629 }
630
631 static struct hif_scatter_req *ath6kl_sdio_scatter_req_get(struct ath6kl *ar)
632 {
633         struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
634         struct hif_scatter_req *node = NULL;
635
636         spin_lock_bh(&ar_sdio->scat_lock);
637
638         if (!list_empty(&ar_sdio->scat_req)) {
639                 node = list_first_entry(&ar_sdio->scat_req,
640                                         struct hif_scatter_req, list);
641                 list_del(&node->list);
642
643                 node->scat_q_depth = get_queue_depth(&ar_sdio->scat_req);
644         }
645
646         spin_unlock_bh(&ar_sdio->scat_lock);
647
648         return node;
649 }
650
651 static void ath6kl_sdio_scatter_req_add(struct ath6kl *ar,
652                                         struct hif_scatter_req *s_req)
653 {
654         struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
655
656         spin_lock_bh(&ar_sdio->scat_lock);
657
658         list_add_tail(&s_req->list, &ar_sdio->scat_req);
659
660         spin_unlock_bh(&ar_sdio->scat_lock);
661 }
662
663 /* scatter gather read write request */
664 static int ath6kl_sdio_async_rw_scatter(struct ath6kl *ar,
665                                         struct hif_scatter_req *scat_req)
666 {
667         struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
668         u32 request = scat_req->req;
669         int status = 0;
670
671         if (!scat_req->len)
672                 return -EINVAL;
673
674         ath6kl_dbg(ATH6KL_DBG_SCATTER,
675                    "hif-scatter: total len: %d scatter entries: %d\n",
676                    scat_req->len, scat_req->scat_entries);
677
678         if (request & HIF_SYNCHRONOUS) {
679                 status = ath6kl_sdio_scat_rw(ar_sdio, scat_req->busrequest);
680         } else {
681                 spin_lock_bh(&ar_sdio->wr_async_lock);
682                 list_add_tail(&scat_req->busrequest->list, &ar_sdio->wr_asyncq);
683                 spin_unlock_bh(&ar_sdio->wr_async_lock);
684                 queue_work(ar->ath6kl_wq, &ar_sdio->wr_async_work);
685         }
686
687         return status;
688 }
689
690 /* clean up scatter support */
691 static void ath6kl_sdio_cleanup_scatter(struct ath6kl *ar)
692 {
693         struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
694         struct hif_scatter_req *s_req, *tmp_req;
695
696         /* empty the free list */
697         spin_lock_bh(&ar_sdio->scat_lock);
698         list_for_each_entry_safe(s_req, tmp_req, &ar_sdio->scat_req, list) {
699                 list_del(&s_req->list);
700                 spin_unlock_bh(&ar_sdio->scat_lock);
701
702                 /*
703                  * FIXME: should we also call completion handler with
704                  * ath6kl_hif_rw_comp_handler() with status -ECANCELED so
705                  * that the packet is properly freed?
706                  */
707                 if (s_req->busrequest)
708                         ath6kl_sdio_free_bus_req(ar_sdio, s_req->busrequest);
709                 kfree(s_req->virt_dma_buf);
710                 kfree(s_req->sgentries);
711                 kfree(s_req);
712
713                 spin_lock_bh(&ar_sdio->scat_lock);
714         }
715         spin_unlock_bh(&ar_sdio->scat_lock);
716 }
717
718 /* setup of HIF scatter resources */
719 static int ath6kl_sdio_enable_scatter(struct ath6kl *ar)
720 {
721         struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
722         struct htc_target *target = ar->htc_target;
723         int ret = 0;
724         bool virt_scat = false;
725
726         if (ar_sdio->scatter_enabled)
727                 return 0;
728
729         ar_sdio->scatter_enabled = true;
730
731         /* check if host supports scatter and it meets our requirements */
732         if (ar_sdio->func->card->host->max_segs < MAX_SCATTER_ENTRIES_PER_REQ) {
733                 ath6kl_err("host only supports scatter of :%d entries, need: %d\n",
734                            ar_sdio->func->card->host->max_segs,
735                            MAX_SCATTER_ENTRIES_PER_REQ);
736                 virt_scat = true;
737         }
738
739         if (!virt_scat) {
740                 ret = ath6kl_sdio_alloc_prep_scat_req(ar_sdio,
741                                 MAX_SCATTER_ENTRIES_PER_REQ,
742                                 MAX_SCATTER_REQUESTS, virt_scat);
743
744                 if (!ret) {
745                         ath6kl_dbg(ATH6KL_DBG_BOOT,
746                                    "hif-scatter enabled requests %d entries %d\n",
747                                    MAX_SCATTER_REQUESTS,
748                                    MAX_SCATTER_ENTRIES_PER_REQ);
749
750                         target->max_scat_entries = MAX_SCATTER_ENTRIES_PER_REQ;
751                         target->max_xfer_szper_scatreq =
752                                                 MAX_SCATTER_REQ_TRANSFER_SIZE;
753                 } else {
754                         ath6kl_sdio_cleanup_scatter(ar);
755                         ath6kl_warn("hif scatter resource setup failed, trying virtual scatter method\n");
756                 }
757         }
758
759         if (virt_scat || ret) {
760                 ret = ath6kl_sdio_alloc_prep_scat_req(ar_sdio,
761                                 ATH6KL_SCATTER_ENTRIES_PER_REQ,
762                                 ATH6KL_SCATTER_REQS, virt_scat);
763
764                 if (ret) {
765                         ath6kl_err("failed to alloc virtual scatter resources !\n");
766                         ath6kl_sdio_cleanup_scatter(ar);
767                         return ret;
768                 }
769
770                 ath6kl_dbg(ATH6KL_DBG_BOOT,
771                            "virtual scatter enabled requests %d entries %d\n",
772                            ATH6KL_SCATTER_REQS, ATH6KL_SCATTER_ENTRIES_PER_REQ);
773
774                 target->max_scat_entries = ATH6KL_SCATTER_ENTRIES_PER_REQ;
775                 target->max_xfer_szper_scatreq =
776                                         ATH6KL_MAX_TRANSFER_SIZE_PER_SCATTER;
777         }
778
779         return 0;
780 }
781
782 static int ath6kl_sdio_config(struct ath6kl *ar)
783 {
784         struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
785         struct sdio_func *func = ar_sdio->func;
786         int ret;
787
788         sdio_claim_host(func);
789
790         if ((ar_sdio->id->device & MANUFACTURER_ID_ATH6KL_BASE_MASK) >=
791             MANUFACTURER_ID_AR6003_BASE) {
792                 /* enable 4-bit ASYNC interrupt on AR6003 or later */
793                 ret = ath6kl_sdio_func0_cmd52_wr_byte(func->card,
794                                                 CCCR_SDIO_IRQ_MODE_REG,
795                                                 SDIO_IRQ_MODE_ASYNC_4BIT_IRQ);
796                 if (ret) {
797                         ath6kl_err("Failed to enable 4-bit async irq mode %d\n",
798                                    ret);
799                         goto out;
800                 }
801
802                 ath6kl_dbg(ATH6KL_DBG_BOOT, "4-bit async irq mode enabled\n");
803         }
804
805         /* give us some time to enable, in ms */
806         func->enable_timeout = 100;
807
808         ret = sdio_set_block_size(func, HIF_MBOX_BLOCK_SIZE);
809         if (ret) {
810                 ath6kl_err("Set sdio block size %d failed: %d)\n",
811                            HIF_MBOX_BLOCK_SIZE, ret);
812                 goto out;
813         }
814
815 out:
816         sdio_release_host(func);
817
818         return ret;
819 }
820
821 static int ath6kl_set_sdio_pm_caps(struct ath6kl *ar)
822 {
823         struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
824         struct sdio_func *func = ar_sdio->func;
825         mmc_pm_flag_t flags;
826         int ret;
827
828         flags = sdio_get_host_pm_caps(func);
829
830         ath6kl_dbg(ATH6KL_DBG_SUSPEND, "sdio suspend pm_caps 0x%x\n", flags);
831
832         if (!(flags & MMC_PM_WAKE_SDIO_IRQ) ||
833             !(flags & MMC_PM_KEEP_POWER))
834                 return -EINVAL;
835
836         ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
837         if (ret) {
838                 ath6kl_err("set sdio keep pwr flag failed: %d\n", ret);
839                 return ret;
840         }
841
842         /* sdio irq wakes up host */
843         ret = sdio_set_host_pm_flags(func, MMC_PM_WAKE_SDIO_IRQ);
844         if (ret)
845                 ath6kl_err("set sdio wake irq flag failed: %d\n", ret);
846
847         return ret;
848 }
849
850 static int ath6kl_sdio_suspend(struct ath6kl *ar, struct cfg80211_wowlan *wow)
851 {
852         struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
853         struct sdio_func *func = ar_sdio->func;
854         mmc_pm_flag_t flags;
855         bool try_deepsleep = false;
856         int ret;
857
858         if (ar->suspend_mode == WLAN_POWER_STATE_WOW ||
859             (!ar->suspend_mode && wow)) {
860                 ret = ath6kl_set_sdio_pm_caps(ar);
861                 if (ret)
862                         goto cut_pwr;
863
864                 ret = ath6kl_cfg80211_suspend(ar, ATH6KL_CFG_SUSPEND_WOW, wow);
865                 if (ret && ret != -ENOTCONN)
866                         ath6kl_err("wow suspend failed: %d\n", ret);
867
868                 if (ret &&
869                     (!ar->wow_suspend_mode ||
870                      ar->wow_suspend_mode == WLAN_POWER_STATE_DEEP_SLEEP))
871                         try_deepsleep = true;
872                 else if (ret &&
873                          ar->wow_suspend_mode == WLAN_POWER_STATE_CUT_PWR)
874                         goto cut_pwr;
875                 if (!ret)
876                         return 0;
877         }
878
879         if (ar->suspend_mode == WLAN_POWER_STATE_DEEP_SLEEP ||
880             !ar->suspend_mode || try_deepsleep) {
881                 flags = sdio_get_host_pm_caps(func);
882                 if (!(flags & MMC_PM_KEEP_POWER))
883                         goto cut_pwr;
884
885                 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
886                 if (ret)
887                         goto cut_pwr;
888
889                 /*
890                  * Workaround to support Deep Sleep with MSM, set the host pm
891                  * flag as MMC_PM_WAKE_SDIO_IRQ to allow SDCC deiver to disable
892                  * the sdc2_clock and internally allows MSM to enter
893                  * TCXO shutdown properly.
894                  */
895                 if ((flags & MMC_PM_WAKE_SDIO_IRQ)) {
896                         ret = sdio_set_host_pm_flags(func,
897                                                 MMC_PM_WAKE_SDIO_IRQ);
898                         if (ret)
899                                 goto cut_pwr;
900                 }
901
902                 ret = ath6kl_cfg80211_suspend(ar, ATH6KL_CFG_SUSPEND_DEEPSLEEP,
903                                               NULL);
904                 if (ret)
905                         goto cut_pwr;
906
907                 return 0;
908         }
909
910 cut_pwr:
911         if (func->card && func->card->host)
912                 func->card->host->pm_flags &= ~MMC_PM_KEEP_POWER;
913
914         return ath6kl_cfg80211_suspend(ar, ATH6KL_CFG_SUSPEND_CUTPOWER, NULL);
915 }
916
917 static int ath6kl_sdio_resume(struct ath6kl *ar)
918 {
919         switch (ar->state) {
920         case ATH6KL_STATE_OFF:
921         case ATH6KL_STATE_CUTPOWER:
922                 ath6kl_dbg(ATH6KL_DBG_SUSPEND,
923                            "sdio resume configuring sdio\n");
924
925                 /* need to set sdio settings after power is cut from sdio */
926                 ath6kl_sdio_config(ar);
927                 break;
928
929         case ATH6KL_STATE_ON:
930                 break;
931
932         case ATH6KL_STATE_DEEPSLEEP:
933                 break;
934
935         case ATH6KL_STATE_WOW:
936                 break;
937
938         case ATH6KL_STATE_SUSPENDING:
939                 break;
940
941         case ATH6KL_STATE_RESUMING:
942                 break;
943
944         case ATH6KL_STATE_RECOVERY:
945                 break;
946         }
947
948         ath6kl_cfg80211_resume(ar);
949
950         return 0;
951 }
952
953 /* set the window address register (using 4-byte register access ). */
954 static int ath6kl_set_addrwin_reg(struct ath6kl *ar, u32 reg_addr, u32 addr)
955 {
956         int status;
957         u8 addr_val[4];
958         s32 i;
959
960         /*
961          * Write bytes 1,2,3 of the register to set the upper address bytes,
962          * the LSB is written last to initiate the access cycle
963          */
964
965         for (i = 1; i <= 3; i++) {
966                 /*
967                  * Fill the buffer with the address byte value we want to
968                  * hit 4 times.
969                  */
970                 memset(addr_val, ((u8 *)&addr)[i], 4);
971
972                 /*
973                  * Hit each byte of the register address with a 4-byte
974                  * write operation to the same address, this is a harmless
975                  * operation.
976                  */
977                 status = ath6kl_sdio_read_write_sync(ar, reg_addr + i, addr_val,
978                                              4, HIF_WR_SYNC_BYTE_FIX);
979                 if (status)
980                         break;
981         }
982
983         if (status) {
984                 ath6kl_err("%s: failed to write initial bytes of 0x%x to window reg: 0x%X\n",
985                            __func__, addr, reg_addr);
986                 return status;
987         }
988
989         /*
990          * Write the address register again, this time write the whole
991          * 4-byte value. The effect here is that the LSB write causes the
992          * cycle to start, the extra 3 byte write to bytes 1,2,3 has no
993          * effect since we are writing the same values again
994          */
995         status = ath6kl_sdio_read_write_sync(ar, reg_addr, (u8 *)(&addr),
996                                      4, HIF_WR_SYNC_BYTE_INC);
997
998         if (status) {
999                 ath6kl_err("%s: failed to write 0x%x to window reg: 0x%X\n",
1000                            __func__, addr, reg_addr);
1001                 return status;
1002         }
1003
1004         return 0;
1005 }
1006
1007 static int ath6kl_sdio_diag_read32(struct ath6kl *ar, u32 address, u32 *data)
1008 {
1009         int status;
1010
1011         /* set window register to start read cycle */
1012         status = ath6kl_set_addrwin_reg(ar, WINDOW_READ_ADDR_ADDRESS,
1013                                         address);
1014
1015         if (status)
1016                 return status;
1017
1018         /* read the data */
1019         status = ath6kl_sdio_read_write_sync(ar, WINDOW_DATA_ADDRESS,
1020                                 (u8 *)data, sizeof(u32), HIF_RD_SYNC_BYTE_INC);
1021         if (status) {
1022                 ath6kl_err("%s: failed to read from window data addr\n",
1023                            __func__);
1024                 return status;
1025         }
1026
1027         return status;
1028 }
1029
1030 static int ath6kl_sdio_diag_write32(struct ath6kl *ar, u32 address,
1031                                     __le32 data)
1032 {
1033         int status;
1034         u32 val = (__force u32) data;
1035
1036         /* set write data */
1037         status = ath6kl_sdio_read_write_sync(ar, WINDOW_DATA_ADDRESS,
1038                                 (u8 *) &val, sizeof(u32), HIF_WR_SYNC_BYTE_INC);
1039         if (status) {
1040                 ath6kl_err("%s: failed to write 0x%x to window data addr\n",
1041                            __func__, data);
1042                 return status;
1043         }
1044
1045         /* set window register, which starts the write cycle */
1046         return ath6kl_set_addrwin_reg(ar, WINDOW_WRITE_ADDR_ADDRESS,
1047                                       address);
1048 }
1049
1050 static int ath6kl_sdio_bmi_credits(struct ath6kl *ar)
1051 {
1052         u32 addr;
1053         unsigned long timeout;
1054         int ret;
1055
1056         ar->bmi.cmd_credits = 0;
1057
1058         /* Read the counter register to get the command credits */
1059         addr = COUNT_DEC_ADDRESS + (HTC_MAILBOX_NUM_MAX + ENDPOINT1) * 4;
1060
1061         timeout = jiffies + msecs_to_jiffies(BMI_COMMUNICATION_TIMEOUT);
1062         while (time_before(jiffies, timeout) && !ar->bmi.cmd_credits) {
1063                 /*
1064                  * Hit the credit counter with a 4-byte access, the first byte
1065                  * read will hit the counter and cause a decrement, while the
1066                  * remaining 3 bytes has no effect. The rationale behind this
1067                  * is to make all HIF accesses 4-byte aligned.
1068                  */
1069                 ret = ath6kl_sdio_read_write_sync(ar, addr,
1070                                          (u8 *)&ar->bmi.cmd_credits, 4,
1071                                          HIF_RD_SYNC_BYTE_INC);
1072                 if (ret) {
1073                         ath6kl_err("Unable to decrement the command credit count register: %d\n",
1074                                    ret);
1075                         return ret;
1076                 }
1077
1078                 /* The counter is only 8 bits.
1079                  * Ignore anything in the upper 3 bytes
1080                  */
1081                 ar->bmi.cmd_credits &= 0xFF;
1082         }
1083
1084         if (!ar->bmi.cmd_credits) {
1085                 ath6kl_err("bmi communication timeout\n");
1086                 return -ETIMEDOUT;
1087         }
1088
1089         return 0;
1090 }
1091
1092 static int ath6kl_bmi_get_rx_lkahd(struct ath6kl *ar)
1093 {
1094         unsigned long timeout;
1095         u32 rx_word = 0;
1096         int ret = 0;
1097
1098         timeout = jiffies + msecs_to_jiffies(BMI_COMMUNICATION_TIMEOUT);
1099         while ((time_before(jiffies, timeout)) && !rx_word) {
1100                 ret = ath6kl_sdio_read_write_sync(ar,
1101                                         RX_LOOKAHEAD_VALID_ADDRESS,
1102                                         (u8 *)&rx_word, sizeof(rx_word),
1103                                         HIF_RD_SYNC_BYTE_INC);
1104                 if (ret) {
1105                         ath6kl_err("unable to read RX_LOOKAHEAD_VALID\n");
1106                         return ret;
1107                 }
1108
1109                  /* all we really want is one bit */
1110                 rx_word &= (1 << ENDPOINT1);
1111         }
1112
1113         if (!rx_word) {
1114                 ath6kl_err("bmi_recv_buf FIFO empty\n");
1115                 return -EINVAL;
1116         }
1117
1118         return ret;
1119 }
1120
1121 static int ath6kl_sdio_bmi_write(struct ath6kl *ar, u8 *buf, u32 len)
1122 {
1123         int ret;
1124         u32 addr;
1125
1126         ret = ath6kl_sdio_bmi_credits(ar);
1127         if (ret)
1128                 return ret;
1129
1130         addr = ar->mbox_info.htc_addr;
1131
1132         ret = ath6kl_sdio_read_write_sync(ar, addr, buf, len,
1133                                           HIF_WR_SYNC_BYTE_INC);
1134         if (ret) {
1135                 ath6kl_err("unable to send the bmi data to the device\n");
1136                 return ret;
1137         }
1138
1139         return 0;
1140 }
1141
1142 static int ath6kl_sdio_bmi_read(struct ath6kl *ar, u8 *buf, u32 len)
1143 {
1144         int ret;
1145         u32 addr;
1146
1147         /*
1148          * During normal bootup, small reads may be required.
1149          * Rather than issue an HIF Read and then wait as the Target
1150          * adds successive bytes to the FIFO, we wait here until
1151          * we know that response data is available.
1152          *
1153          * This allows us to cleanly timeout on an unexpected
1154          * Target failure rather than risk problems at the HIF level.
1155          * In particular, this avoids SDIO timeouts and possibly garbage
1156          * data on some host controllers.  And on an interconnect
1157          * such as Compact Flash (as well as some SDIO masters) which
1158          * does not provide any indication on data timeout, it avoids
1159          * a potential hang or garbage response.
1160          *
1161          * Synchronization is more difficult for reads larger than the
1162          * size of the MBOX FIFO (128B), because the Target is unable
1163          * to push the 129th byte of data until AFTER the Host posts an
1164          * HIF Read and removes some FIFO data.  So for large reads the
1165          * Host proceeds to post an HIF Read BEFORE all the data is
1166          * actually available to read.  Fortunately, large BMI reads do
1167          * not occur in practice -- they're supported for debug/development.
1168          *
1169          * So Host/Target BMI synchronization is divided into these cases:
1170          *  CASE 1: length < 4
1171          *        Should not happen
1172          *
1173          *  CASE 2: 4 <= length <= 128
1174          *        Wait for first 4 bytes to be in FIFO
1175          *        If CONSERVATIVE_BMI_READ is enabled, also wait for
1176          *        a BMI command credit, which indicates that the ENTIRE
1177          *        response is available in the the FIFO
1178          *
1179          *  CASE 3: length > 128
1180          *        Wait for the first 4 bytes to be in FIFO
1181          *
1182          * For most uses, a small timeout should be sufficient and we will
1183          * usually see a response quickly; but there may be some unusual
1184          * (debug) cases of BMI_EXECUTE where we want an larger timeout.
1185          * For now, we use an unbounded busy loop while waiting for
1186          * BMI_EXECUTE.
1187          *
1188          * If BMI_EXECUTE ever needs to support longer-latency execution,
1189          * especially in production, this code needs to be enhanced to sleep
1190          * and yield.  Also note that BMI_COMMUNICATION_TIMEOUT is currently
1191          * a function of Host processor speed.
1192          */
1193         if (len >= 4) { /* NB: Currently, always true */
1194                 ret = ath6kl_bmi_get_rx_lkahd(ar);
1195                 if (ret)
1196                         return ret;
1197         }
1198
1199         addr = ar->mbox_info.htc_addr;
1200         ret = ath6kl_sdio_read_write_sync(ar, addr, buf, len,
1201                                   HIF_RD_SYNC_BYTE_INC);
1202         if (ret) {
1203                 ath6kl_err("Unable to read the bmi data from the device: %d\n",
1204                            ret);
1205                 return ret;
1206         }
1207
1208         return 0;
1209 }
1210
1211 static void ath6kl_sdio_stop(struct ath6kl *ar)
1212 {
1213         struct ath6kl_sdio *ar_sdio = ath6kl_sdio_priv(ar);
1214         struct bus_request *req, *tmp_req;
1215         void *context;
1216
1217         /* FIXME: make sure that wq is not queued again */
1218
1219         cancel_work_sync(&ar_sdio->wr_async_work);
1220
1221         spin_lock_bh(&ar_sdio->wr_async_lock);
1222
1223         list_for_each_entry_safe(req, tmp_req, &ar_sdio->wr_asyncq, list) {
1224                 list_del(&req->list);
1225
1226                 if (req->scat_req) {
1227                         /* this is a scatter gather request */
1228                         req->scat_req->status = -ECANCELED;
1229                         req->scat_req->complete(ar_sdio->ar->htc_target,
1230                                                 req->scat_req);
1231                 } else {
1232                         context = req->packet;
1233                         ath6kl_sdio_free_bus_req(ar_sdio, req);
1234                         ath6kl_hif_rw_comp_handler(context, -ECANCELED);
1235                 }
1236         }
1237
1238         spin_unlock_bh(&ar_sdio->wr_async_lock);
1239
1240         WARN_ON(get_queue_depth(&ar_sdio->scat_req) != 4);
1241 }
1242
1243 static const struct ath6kl_hif_ops ath6kl_sdio_ops = {
1244         .read_write_sync = ath6kl_sdio_read_write_sync,
1245         .write_async = ath6kl_sdio_write_async,
1246         .irq_enable = ath6kl_sdio_irq_enable,
1247         .irq_disable = ath6kl_sdio_irq_disable,
1248         .scatter_req_get = ath6kl_sdio_scatter_req_get,
1249         .scatter_req_add = ath6kl_sdio_scatter_req_add,
1250         .enable_scatter = ath6kl_sdio_enable_scatter,
1251         .scat_req_rw = ath6kl_sdio_async_rw_scatter,
1252         .cleanup_scatter = ath6kl_sdio_cleanup_scatter,
1253         .suspend = ath6kl_sdio_suspend,
1254         .resume = ath6kl_sdio_resume,
1255         .diag_read32 = ath6kl_sdio_diag_read32,
1256         .diag_write32 = ath6kl_sdio_diag_write32,
1257         .bmi_read = ath6kl_sdio_bmi_read,
1258         .bmi_write = ath6kl_sdio_bmi_write,
1259         .power_on = ath6kl_sdio_power_on,
1260         .power_off = ath6kl_sdio_power_off,
1261         .stop = ath6kl_sdio_stop,
1262 };
1263
1264 #ifdef CONFIG_PM_SLEEP
1265
1266 /*
1267  * Empty handlers so that mmc subsystem doesn't remove us entirely during
1268  * suspend. We instead follow cfg80211 suspend/resume handlers.
1269  */
1270 static int ath6kl_sdio_pm_suspend(struct device *device)
1271 {
1272         ath6kl_dbg(ATH6KL_DBG_SUSPEND, "sdio pm suspend\n");
1273
1274         return 0;
1275 }
1276
1277 static int ath6kl_sdio_pm_resume(struct device *device)
1278 {
1279         ath6kl_dbg(ATH6KL_DBG_SUSPEND, "sdio pm resume\n");
1280
1281         return 0;
1282 }
1283
1284 static SIMPLE_DEV_PM_OPS(ath6kl_sdio_pm_ops, ath6kl_sdio_pm_suspend,
1285                          ath6kl_sdio_pm_resume);
1286
1287 #define ATH6KL_SDIO_PM_OPS (&ath6kl_sdio_pm_ops)
1288
1289 #else
1290
1291 #define ATH6KL_SDIO_PM_OPS NULL
1292
1293 #endif /* CONFIG_PM_SLEEP */
1294
1295 static int ath6kl_sdio_probe(struct sdio_func *func,
1296                              const struct sdio_device_id *id)
1297 {
1298         int ret;
1299         struct ath6kl_sdio *ar_sdio;
1300         struct ath6kl *ar;
1301         int count;
1302
1303         ath6kl_dbg(ATH6KL_DBG_BOOT,
1304                    "sdio new func %d vendor 0x%x device 0x%x block 0x%x/0x%x\n",
1305                    func->num, func->vendor, func->device,
1306                    func->max_blksize, func->cur_blksize);
1307
1308         ar_sdio = kzalloc(sizeof(struct ath6kl_sdio), GFP_KERNEL);
1309         if (!ar_sdio)
1310                 return -ENOMEM;
1311
1312         ar_sdio->dma_buffer = kzalloc(HIF_DMA_BUFFER_SIZE, GFP_KERNEL);
1313         if (!ar_sdio->dma_buffer) {
1314                 ret = -ENOMEM;
1315                 goto err_hif;
1316         }
1317
1318         ar_sdio->func = func;
1319         sdio_set_drvdata(func, ar_sdio);
1320
1321         ar_sdio->id = id;
1322         ar_sdio->is_disabled = true;
1323
1324         spin_lock_init(&ar_sdio->lock);
1325         spin_lock_init(&ar_sdio->scat_lock);
1326         spin_lock_init(&ar_sdio->wr_async_lock);
1327         mutex_init(&ar_sdio->dma_buffer_mutex);
1328
1329         INIT_LIST_HEAD(&ar_sdio->scat_req);
1330         INIT_LIST_HEAD(&ar_sdio->bus_req_freeq);
1331         INIT_LIST_HEAD(&ar_sdio->wr_asyncq);
1332
1333         INIT_WORK(&ar_sdio->wr_async_work, ath6kl_sdio_write_async_work);
1334
1335         init_waitqueue_head(&ar_sdio->irq_wq);
1336
1337         for (count = 0; count < BUS_REQUEST_MAX_NUM; count++)
1338                 ath6kl_sdio_free_bus_req(ar_sdio, &ar_sdio->bus_req[count]);
1339
1340         ar = ath6kl_core_create(&ar_sdio->func->dev);
1341         if (!ar) {
1342                 ath6kl_err("Failed to alloc ath6kl core\n");
1343                 ret = -ENOMEM;
1344                 goto err_dma;
1345         }
1346
1347         ar_sdio->ar = ar;
1348         ar->hif_type = ATH6KL_HIF_TYPE_SDIO;
1349         ar->hif_priv = ar_sdio;
1350         ar->hif_ops = &ath6kl_sdio_ops;
1351         ar->bmi.max_data_size = 256;
1352
1353         ath6kl_sdio_set_mbox_info(ar);
1354
1355         ret = ath6kl_sdio_config(ar);
1356         if (ret) {
1357                 ath6kl_err("Failed to config sdio: %d\n", ret);
1358                 goto err_core_alloc;
1359         }
1360
1361         ret = ath6kl_core_init(ar, ATH6KL_HTC_TYPE_MBOX);
1362         if (ret) {
1363                 ath6kl_err("Failed to init ath6kl core\n");
1364                 goto err_core_alloc;
1365         }
1366
1367         return ret;
1368
1369 err_core_alloc:
1370         ath6kl_core_destroy(ar_sdio->ar);
1371 err_dma:
1372         kfree(ar_sdio->dma_buffer);
1373 err_hif:
1374         kfree(ar_sdio);
1375
1376         return ret;
1377 }
1378
1379 static void ath6kl_sdio_remove(struct sdio_func *func)
1380 {
1381         struct ath6kl_sdio *ar_sdio;
1382
1383         ath6kl_dbg(ATH6KL_DBG_BOOT,
1384                    "sdio removed func %d vendor 0x%x device 0x%x\n",
1385                    func->num, func->vendor, func->device);
1386
1387         ar_sdio = sdio_get_drvdata(func);
1388
1389         ath6kl_stop_txrx(ar_sdio->ar);
1390         cancel_work_sync(&ar_sdio->wr_async_work);
1391
1392         ath6kl_core_cleanup(ar_sdio->ar);
1393         ath6kl_core_destroy(ar_sdio->ar);
1394
1395         kfree(ar_sdio->dma_buffer);
1396         kfree(ar_sdio);
1397 }
1398
1399 static const struct sdio_device_id ath6kl_sdio_devices[] = {
1400         {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6003_BASE | 0x0))},
1401         {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6003_BASE | 0x1))},
1402         {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6004_BASE | 0x0))},
1403         {SDIO_DEVICE(MANUFACTURER_CODE, (MANUFACTURER_ID_AR6004_BASE | 0x1))},
1404         {},
1405 };
1406
1407 MODULE_DEVICE_TABLE(sdio, ath6kl_sdio_devices);
1408
1409 static struct sdio_driver ath6kl_sdio_driver = {
1410         .name = "ath6kl_sdio",
1411         .id_table = ath6kl_sdio_devices,
1412         .probe = ath6kl_sdio_probe,
1413         .remove = ath6kl_sdio_remove,
1414         .drv.pm = ATH6KL_SDIO_PM_OPS,
1415 };
1416
1417 static int __init ath6kl_sdio_init(void)
1418 {
1419         int ret;
1420
1421         ret = sdio_register_driver(&ath6kl_sdio_driver);
1422         if (ret)
1423                 ath6kl_err("sdio driver registration failed: %d\n", ret);
1424
1425         return ret;
1426 }
1427
1428 static void __exit ath6kl_sdio_exit(void)
1429 {
1430         sdio_unregister_driver(&ath6kl_sdio_driver);
1431 }
1432
1433 module_init(ath6kl_sdio_init);
1434 module_exit(ath6kl_sdio_exit);
1435
1436 MODULE_AUTHOR("Atheros Communications, Inc.");
1437 MODULE_DESCRIPTION("Driver support for Atheros AR600x SDIO devices");
1438 MODULE_LICENSE("Dual BSD/GPL");
1439
1440 MODULE_FIRMWARE(AR6003_HW_2_0_FW_DIR "/" AR6003_HW_2_0_OTP_FILE);
1441 MODULE_FIRMWARE(AR6003_HW_2_0_FW_DIR "/" AR6003_HW_2_0_FIRMWARE_FILE);
1442 MODULE_FIRMWARE(AR6003_HW_2_0_FW_DIR "/" AR6003_HW_2_0_PATCH_FILE);
1443 MODULE_FIRMWARE(AR6003_HW_2_0_BOARD_DATA_FILE);
1444 MODULE_FIRMWARE(AR6003_HW_2_0_DEFAULT_BOARD_DATA_FILE);
1445 MODULE_FIRMWARE(AR6003_HW_2_1_1_FW_DIR "/" AR6003_HW_2_1_1_OTP_FILE);
1446 MODULE_FIRMWARE(AR6003_HW_2_1_1_FW_DIR "/" AR6003_HW_2_1_1_FIRMWARE_FILE);
1447 MODULE_FIRMWARE(AR6003_HW_2_1_1_FW_DIR "/" AR6003_HW_2_1_1_PATCH_FILE);
1448 MODULE_FIRMWARE(AR6003_HW_2_1_1_BOARD_DATA_FILE);
1449 MODULE_FIRMWARE(AR6003_HW_2_1_1_DEFAULT_BOARD_DATA_FILE);
1450 MODULE_FIRMWARE(AR6004_HW_1_0_FW_DIR "/" AR6004_HW_1_0_FIRMWARE_FILE);
1451 MODULE_FIRMWARE(AR6004_HW_1_0_BOARD_DATA_FILE);
1452 MODULE_FIRMWARE(AR6004_HW_1_0_DEFAULT_BOARD_DATA_FILE);
1453 MODULE_FIRMWARE(AR6004_HW_1_1_FW_DIR "/" AR6004_HW_1_1_FIRMWARE_FILE);
1454 MODULE_FIRMWARE(AR6004_HW_1_1_BOARD_DATA_FILE);
1455 MODULE_FIRMWARE(AR6004_HW_1_1_DEFAULT_BOARD_DATA_FILE);
1456 MODULE_FIRMWARE(AR6004_HW_1_2_FW_DIR "/" AR6004_HW_1_2_FIRMWARE_FILE);
1457 MODULE_FIRMWARE(AR6004_HW_1_2_BOARD_DATA_FILE);
1458 MODULE_FIRMWARE(AR6004_HW_1_2_DEFAULT_BOARD_DATA_FILE);
1459 MODULE_FIRMWARE(AR6004_HW_1_3_FW_DIR "/" AR6004_HW_1_3_FIRMWARE_FILE);
1460 MODULE_FIRMWARE(AR6004_HW_1_3_BOARD_DATA_FILE);
1461 MODULE_FIRMWARE(AR6004_HW_1_3_DEFAULT_BOARD_DATA_FILE);