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