blob: f207756cbb796fa6fc12da6cee08b2303e6e418d [file] [log] [blame]
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001/*
2 * Marvell Wireless LAN device driver: SDIO specific handling
3 *
4 * Copyright (C) 2011, Marvell International Ltd.
5 *
6 * This software file (the "File") is distributed by Marvell International
7 * Ltd. under the terms of the GNU General Public License Version 2, June 1991
8 * (the "License"). You may use, redistribute and/or modify this File in
9 * accordance with the terms and conditions of the License, a copy of which
10 * is available by writing to the Free Software Foundation, Inc.,
11 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
12 * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
13 *
14 * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
16 * ARE EXPRESSLY DISCLAIMED. The License provides additional details about
17 * this warranty disclaimer.
18 */
19
20#include <linux/firmware.h>
21
22#include "decl.h"
23#include "ioctl.h"
24#include "util.h"
25#include "fw.h"
26#include "main.h"
27#include "wmm.h"
28#include "11n.h"
29#include "sdio.h"
30
31
32#define SDIO_VERSION "1.0"
33
34static struct mwifiex_if_ops sdio_ops;
35
36static struct semaphore add_remove_card_sem;
37
38/*
39 * SDIO probe.
40 *
41 * This function probes an mwifiex device and registers it. It allocates
42 * the card structure, enables SDIO function number and initiates the
43 * device registration and initialization procedure by adding a logical
44 * interface.
45 */
46static int
47mwifiex_sdio_probe(struct sdio_func *func, const struct sdio_device_id *id)
48{
49 int ret = 0;
50 struct sdio_mmc_card *card = NULL;
51
52 pr_debug("info: vendor=0x%4.04X device=0x%4.04X class=%d function=%d\n",
53 func->vendor, func->device, func->class, func->num);
54
55 card = kzalloc(sizeof(struct sdio_mmc_card), GFP_KERNEL);
56 if (!card) {
57 pr_err("%s: failed to alloc memory\n", __func__);
58 return -ENOMEM;
59 }
60
61 card->func = func;
62
63 func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
64
65 sdio_claim_host(func);
66 ret = sdio_enable_func(func);
67 sdio_release_host(func);
68
69 if (ret) {
70 pr_err("%s: failed to enable function\n", __func__);
71 return -EIO;
72 }
73
74 if (mwifiex_add_card(card, &add_remove_card_sem, &sdio_ops)) {
75 pr_err("%s: add card failed\n", __func__);
76 kfree(card);
77 sdio_claim_host(func);
78 ret = sdio_disable_func(func);
79 sdio_release_host(func);
80 ret = -1;
81 }
82
83 return ret;
84}
85
86/*
87 * SDIO remove.
88 *
89 * This function removes the interface and frees up the card structure.
90 */
91static void
92mwifiex_sdio_remove(struct sdio_func *func)
93{
94 struct sdio_mmc_card *card;
95
96 pr_debug("info: SDIO func num=%d\n", func->num);
97
98 if (func) {
99 card = sdio_get_drvdata(func);
100 if (card) {
101 mwifiex_remove_card(card->adapter,
102 &add_remove_card_sem);
103 kfree(card);
104 }
105 }
106}
107
108/*
109 * SDIO suspend.
110 *
111 * Kernel needs to suspend all functions separately. Therefore all
112 * registered functions must have drivers with suspend and resume
113 * methods. Failing that the kernel simply removes the whole card.
114 *
115 * If already not suspended, this function allocates and sends a host
116 * sleep activate request to the firmware and turns off the traffic.
117 */
118static int mwifiex_sdio_suspend(struct device *dev)
119{
120 struct sdio_func *func = dev_to_sdio_func(dev);
121 struct sdio_mmc_card *card;
122 struct mwifiex_adapter *adapter = NULL;
123 mmc_pm_flag_t pm_flag = 0;
124 int hs_actived = 0;
125 int i;
126 int ret = 0;
127
128 if (func) {
129 pm_flag = sdio_get_host_pm_caps(func);
130 pr_debug("cmd: %s: suspend: PM flag = 0x%x\n",
131 sdio_func_id(func), pm_flag);
132 if (!(pm_flag & MMC_PM_KEEP_POWER)) {
133 pr_err("%s: cannot remain alive while host is"
134 " suspended\n", sdio_func_id(func));
135 return -ENOSYS;
136 }
137
138 card = sdio_get_drvdata(func);
139 if (!card || !card->adapter) {
140 pr_err("suspend: invalid card or adapter\n");
141 return 0;
142 }
143 } else {
144 pr_err("suspend: sdio_func is not specified\n");
145 return 0;
146 }
147
148 adapter = card->adapter;
149
150 /* Enable the Host Sleep */
151 hs_actived = mwifiex_enable_hs(adapter);
152 if (hs_actived) {
153 pr_debug("cmd: suspend with MMC_PM_KEEP_POWER\n");
154 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
155 }
156
157 /* Indicate device suspended */
158 adapter->is_suspended = true;
159
160 for (i = 0; i < adapter->priv_num; i++)
161 netif_carrier_off(adapter->priv[i]->netdev);
162
163 return ret;
164}
165
166/*
167 * SDIO resume.
168 *
169 * Kernel needs to suspend all functions separately. Therefore all
170 * registered functions must have drivers with suspend and resume
171 * methods. Failing that the kernel simply removes the whole card.
172 *
173 * If already not resumed, this function turns on the traffic and
174 * sends a host sleep cancel request to the firmware.
175 */
176static int mwifiex_sdio_resume(struct device *dev)
177{
178 struct sdio_func *func = dev_to_sdio_func(dev);
179 struct sdio_mmc_card *card;
180 struct mwifiex_adapter *adapter = NULL;
181 mmc_pm_flag_t pm_flag = 0;
182 int i;
183
184 if (func) {
185 pm_flag = sdio_get_host_pm_caps(func);
186 card = sdio_get_drvdata(func);
187 if (!card || !card->adapter) {
188 pr_err("resume: invalid card or adapter\n");
189 return 0;
190 }
191 } else {
192 pr_err("resume: sdio_func is not specified\n");
193 return 0;
194 }
195
196 adapter = card->adapter;
197
198 if (!adapter->is_suspended) {
199 dev_warn(adapter->dev, "device already resumed\n");
200 return 0;
201 }
202
203 adapter->is_suspended = false;
204
205 for (i = 0; i < adapter->priv_num; i++)
206 if (adapter->priv[i]->media_connected)
207 netif_carrier_on(adapter->priv[i]->netdev);
208
209 /* Disable Host Sleep */
210 mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700211 MWIFIEX_ASYNC_CMD);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700212
213 return 0;
214}
215
216/* Device ID for SD8787 */
217#define SDIO_DEVICE_ID_MARVELL_8787 (0x9119)
218
219/* WLAN IDs */
220static const struct sdio_device_id mwifiex_ids[] = {
221 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787)},
222 {},
223};
224
225MODULE_DEVICE_TABLE(sdio, mwifiex_ids);
226
227static const struct dev_pm_ops mwifiex_sdio_pm_ops = {
228 .suspend = mwifiex_sdio_suspend,
229 .resume = mwifiex_sdio_resume,
230};
231
232static struct sdio_driver mwifiex_sdio = {
233 .name = "mwifiex_sdio",
234 .id_table = mwifiex_ids,
235 .probe = mwifiex_sdio_probe,
236 .remove = mwifiex_sdio_remove,
237 .drv = {
238 .owner = THIS_MODULE,
239 .pm = &mwifiex_sdio_pm_ops,
240 }
241};
242
243/*
244 * This function writes data into SDIO card register.
245 */
246static int
247mwifiex_write_reg(struct mwifiex_adapter *adapter, u32 reg, u32 data)
248{
249 struct sdio_mmc_card *card = adapter->card;
250 int ret = -1;
251
252 sdio_claim_host(card->func);
253 sdio_writeb(card->func, (u8) data, reg, &ret);
254 sdio_release_host(card->func);
255
256 return ret;
257}
258
259/*
260 * This function reads data from SDIO card register.
261 */
262static int
263mwifiex_read_reg(struct mwifiex_adapter *adapter, u32 reg, u32 *data)
264{
265 struct sdio_mmc_card *card = adapter->card;
266 int ret = -1;
267 u8 val;
268
269 sdio_claim_host(card->func);
270 val = sdio_readb(card->func, reg, &ret);
271 sdio_release_host(card->func);
272
273 *data = val;
274
275 return ret;
276}
277
278/*
279 * This function writes multiple data into SDIO card memory.
280 *
281 * This does not work in suspended mode.
282 */
283static int
284mwifiex_write_data_sync(struct mwifiex_adapter *adapter,
285 u8 *buffer, u32 pkt_len, u32 port, u32 timeout)
286{
287 struct sdio_mmc_card *card = adapter->card;
288 int ret = -1;
289 u8 blk_mode =
290 (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE : BLOCK_MODE;
291 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
292 u32 blk_cnt =
293 (blk_mode ==
294 BLOCK_MODE) ? (pkt_len /
295 MWIFIEX_SDIO_BLOCK_SIZE) : pkt_len;
296 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
297
298 if (adapter->is_suspended) {
299 dev_err(adapter->dev,
300 "%s: not allowed while suspended\n", __func__);
301 return -1;
302 }
303
304 sdio_claim_host(card->func);
305
306 if (!sdio_writesb(card->func, ioport, buffer, blk_cnt * blk_size))
307 ret = 0;
308
309 sdio_release_host(card->func);
310
311 return ret;
312}
313
314/*
315 * This function reads multiple data from SDIO card memory.
316 */
317static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter,
318 u8 *buffer, u32 len,
319 u32 port, u32 timeout, u8 claim)
320{
321 struct sdio_mmc_card *card = adapter->card;
322 int ret = -1;
323 u8 blk_mode =
324 (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE : BLOCK_MODE;
325 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
326 u32 blk_cnt =
327 (blk_mode ==
328 BLOCK_MODE) ? (len / MWIFIEX_SDIO_BLOCK_SIZE) : len;
329 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
330
331 if (claim)
332 sdio_claim_host(card->func);
333
334 if (!sdio_readsb(card->func, buffer, ioport, blk_cnt * blk_size))
335 ret = 0;
336
337 if (claim)
338 sdio_release_host(card->func);
339
340 return ret;
341}
342
343/*
344 * This function wakes up the card.
345 *
346 * A host power up command is written to the card configuration
347 * register to wake up the card.
348 */
349static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
350{
351 int ret;
352
353 dev_dbg(adapter->dev, "event: wakeup device...\n");
354 ret = mwifiex_write_reg(adapter, CONFIGURATION_REG, HOST_POWER_UP);
355
356 return ret;
357}
358
359/*
360 * This function is called after the card has woken up.
361 *
362 * The card configuration register is reset.
363 */
364static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
365{
366 int ret;
367
368 dev_dbg(adapter->dev, "cmd: wakeup device completed\n");
369 ret = mwifiex_write_reg(adapter, CONFIGURATION_REG, 0);
370
371 return ret;
372}
373
374/*
375 * This function initializes the IO ports.
376 *
377 * The following operations are performed -
378 * - Read the IO ports (0, 1 and 2)
379 * - Set host interrupt Reset-To-Read to clear
380 * - Set auto re-enable interrupt
381 */
382static int mwifiex_init_sdio_ioport(struct mwifiex_adapter *adapter)
383{
384 u32 reg;
385
386 adapter->ioport = 0;
387
388 /* Read the IO port */
389 if (!mwifiex_read_reg(adapter, IO_PORT_0_REG, &reg))
390 adapter->ioport |= (reg & 0xff);
391 else
392 return -1;
393
394 if (!mwifiex_read_reg(adapter, IO_PORT_1_REG, &reg))
395 adapter->ioport |= ((reg & 0xff) << 8);
396 else
397 return -1;
398
399 if (!mwifiex_read_reg(adapter, IO_PORT_2_REG, &reg))
400 adapter->ioport |= ((reg & 0xff) << 16);
401 else
402 return -1;
403
404 pr_debug("info: SDIO FUNC1 IO port: %#x\n", adapter->ioport);
405
406 /* Set Host interrupt reset to read to clear */
407 if (!mwifiex_read_reg(adapter, HOST_INT_RSR_REG, &reg))
408 mwifiex_write_reg(adapter, HOST_INT_RSR_REG,
409 reg | SDIO_INT_MASK);
410 else
411 return -1;
412
413 /* Dnld/Upld ready set to auto reset */
414 if (!mwifiex_read_reg(adapter, CARD_MISC_CFG_REG, &reg))
415 mwifiex_write_reg(adapter, CARD_MISC_CFG_REG,
416 reg | AUTO_RE_ENABLE_INT);
417 else
418 return -1;
419
420 return 0;
421}
422
423/*
424 * This function sends data to the card.
425 */
426static int mwifiex_write_data_to_card(struct mwifiex_adapter *adapter,
427 u8 *payload, u32 pkt_len, u32 port)
428{
429 u32 i = 0;
430 int ret = 0;
431
432 do {
433 ret = mwifiex_write_data_sync(adapter, payload, pkt_len,
434 port, 0);
435 if (ret) {
436 i++;
437 dev_err(adapter->dev, "host_to_card, write iomem"
438 " (%d) failed: %d\n", i, ret);
439 if (mwifiex_write_reg(adapter,
440 CONFIGURATION_REG, 0x04))
441 dev_err(adapter->dev, "write CFG reg failed\n");
442
443 ret = -1;
444 if (i > MAX_WRITE_IOMEM_RETRY)
445 return ret;
446 }
447 } while (ret == -1);
448
449 return ret;
450}
451
452/*
453 * This function gets the read port.
454 *
455 * If control port bit is set in MP read bitmap, the control port
456 * is returned, otherwise the current read port is returned and
457 * the value is increased (provided it does not reach the maximum
458 * limit, in which case it is reset to 1)
459 */
460static int mwifiex_get_rd_port(struct mwifiex_adapter *adapter, u8 *port)
461{
462 struct sdio_mmc_card *card = adapter->card;
463 u16 rd_bitmap = card->mp_rd_bitmap;
464
465 dev_dbg(adapter->dev, "data: mp_rd_bitmap=0x%04x\n", rd_bitmap);
466
467 if (!(rd_bitmap & (CTRL_PORT_MASK | DATA_PORT_MASK)))
468 return -1;
469
470 if (card->mp_rd_bitmap & CTRL_PORT_MASK) {
471 card->mp_rd_bitmap &= (u16) (~CTRL_PORT_MASK);
472 *port = CTRL_PORT;
473 dev_dbg(adapter->dev, "data: port=%d mp_rd_bitmap=0x%04x\n",
474 *port, card->mp_rd_bitmap);
475 } else {
476 if (card->mp_rd_bitmap & (1 << card->curr_rd_port)) {
477 card->mp_rd_bitmap &=
478 (u16) (~(1 << card->curr_rd_port));
479 *port = card->curr_rd_port;
480
481 if (++card->curr_rd_port == MAX_PORT)
482 card->curr_rd_port = 1;
483 } else {
484 return -1;
485 }
486
487 dev_dbg(adapter->dev,
488 "data: port=%d mp_rd_bitmap=0x%04x -> 0x%04x\n",
489 *port, rd_bitmap, card->mp_rd_bitmap);
490 }
491 return 0;
492}
493
494/*
495 * This function gets the write port for data.
496 *
497 * The current write port is returned if available and the value is
498 * increased (provided it does not reach the maximum limit, in which
499 * case it is reset to 1)
500 */
501static int mwifiex_get_wr_port_data(struct mwifiex_adapter *adapter, u8 *port)
502{
503 struct sdio_mmc_card *card = adapter->card;
504 u16 wr_bitmap = card->mp_wr_bitmap;
505
506 dev_dbg(adapter->dev, "data: mp_wr_bitmap=0x%04x\n", wr_bitmap);
507
508 if (!(wr_bitmap & card->mp_data_port_mask))
509 return -1;
510
511 if (card->mp_wr_bitmap & (1 << card->curr_wr_port)) {
512 card->mp_wr_bitmap &= (u16) (~(1 << card->curr_wr_port));
513 *port = card->curr_wr_port;
514 if (++card->curr_wr_port == card->mp_end_port)
515 card->curr_wr_port = 1;
516 } else {
517 adapter->data_sent = true;
518 return -EBUSY;
519 }
520
521 if (*port == CTRL_PORT) {
522 dev_err(adapter->dev, "invalid data port=%d cur port=%d"
523 " mp_wr_bitmap=0x%04x -> 0x%04x\n",
524 *port, card->curr_wr_port, wr_bitmap,
525 card->mp_wr_bitmap);
526 return -1;
527 }
528
529 dev_dbg(adapter->dev, "data: port=%d mp_wr_bitmap=0x%04x -> 0x%04x\n",
530 *port, wr_bitmap, card->mp_wr_bitmap);
531
532 return 0;
533}
534
535/*
536 * This function polls the card status.
537 */
538static int
539mwifiex_sdio_poll_card_status(struct mwifiex_adapter *adapter, u8 bits)
540{
541 u32 tries;
542 u32 cs = 0;
543
544 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
545 if (mwifiex_read_reg(adapter, CARD_STATUS_REG, &cs))
546 break;
547 else if ((cs & bits) == bits)
548 return 0;
549
550 udelay(10);
551 }
552
553 dev_err(adapter->dev, "poll card status failed, tries = %d\n",
554 tries);
555 return -1;
556}
557
558/*
559 * This function reads the firmware status.
560 */
561static int
562mwifiex_sdio_read_fw_status(struct mwifiex_adapter *adapter, u16 *dat)
563{
564 u32 fws0 = 0, fws1 = 0;
565
566 if (mwifiex_read_reg(adapter, CARD_FW_STATUS0_REG, &fws0))
567 return -1;
568
569 if (mwifiex_read_reg(adapter, CARD_FW_STATUS1_REG, &fws1))
570 return -1;
571
572 *dat = (u16) ((fws1 << 8) | fws0);
573
574 return 0;
575}
576
577/*
578 * This function disables the host interrupt.
579 *
580 * The host interrupt mask is read, the disable bit is reset and
581 * written back to the card host interrupt mask register.
582 */
583static int mwifiex_sdio_disable_host_int(struct mwifiex_adapter *adapter)
584{
585 u32 host_int_mask = 0;
586
587 /* Read back the host_int_mask register */
588 if (mwifiex_read_reg(adapter, HOST_INT_MASK_REG, &host_int_mask))
589 return -1;
590
591 /* Update with the mask and write back to the register */
592 host_int_mask &= ~HOST_INT_DISABLE;
593
594 if (mwifiex_write_reg(adapter, HOST_INT_MASK_REG, host_int_mask)) {
595 dev_err(adapter->dev, "disable host interrupt failed\n");
596 return -1;
597 }
598
599 return 0;
600}
601
602/*
603 * This function enables the host interrupt.
604 *
605 * The host interrupt enable mask is written to the card
606 * host interrupt mask register.
607 */
608static int mwifiex_sdio_enable_host_int(struct mwifiex_adapter *adapter)
609{
610 /* Simply write the mask to the register */
611 if (mwifiex_write_reg(adapter, HOST_INT_MASK_REG, HOST_INT_ENABLE)) {
612 dev_err(adapter->dev, "enable host interrupt failed\n");
613 return -1;
614 }
615 return 0;
616}
617
618/*
619 * This function sends a data buffer to the card.
620 */
621static int mwifiex_sdio_card_to_host(struct mwifiex_adapter *adapter,
622 u32 *type, u8 *buffer,
623 u32 npayload, u32 ioport)
624{
625 int ret = 0;
626 u32 nb;
627
628 if (!buffer) {
629 dev_err(adapter->dev, "%s: buffer is NULL\n", __func__);
630 return -1;
631 }
632
633 ret = mwifiex_read_data_sync(adapter, buffer, npayload, ioport, 0, 1);
634
635 if (ret) {
636 dev_err(adapter->dev, "%s: read iomem failed: %d\n", __func__,
637 ret);
638 return -1;
639 }
640
641 nb = le16_to_cpu(*(__le16 *) (buffer));
642 if (nb > npayload) {
643 dev_err(adapter->dev, "%s: invalid packet, nb=%d, npayload=%d\n",
644 __func__, nb, npayload);
645 return -1;
646 }
647
648 *type = le16_to_cpu(*(__le16 *) (buffer + 2));
649
650 return ret;
651}
652
653/*
654 * This function downloads the firmware to the card.
655 *
656 * Firmware is downloaded to the card in blocks. Every block download
657 * is tested for CRC errors, and retried a number of times before
658 * returning failure.
659 */
660static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
661 struct mwifiex_fw_image *fw)
662{
663 int ret = 0;
664 u8 *firmware = fw->fw_buf;
665 u32 firmware_len = fw->fw_len;
666 u32 offset = 0;
667 u32 base0, base1;
668 u8 *fwbuf;
669 u16 len = 0;
670 u32 txlen = 0, tx_blocks = 0, tries = 0;
671 u32 i = 0;
672
673 if (!firmware_len) {
674 dev_err(adapter->dev, "firmware image not found!"
675 " Terminating download\n");
676 return -1;
677 }
678
679 dev_dbg(adapter->dev, "info: downloading FW image (%d bytes)\n",
680 firmware_len);
681
682 /* Assume that the allocated buffer is 8-byte aligned */
683 fwbuf = kzalloc(MWIFIEX_UPLD_SIZE, GFP_KERNEL);
684 if (!fwbuf) {
685 dev_err(adapter->dev, "unable to alloc buffer for firmware."
686 " Terminating download\n");
687 return -1;
688 }
689
690 /* Perform firmware data transfer */
691 do {
692 /* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY
693 bits */
694 ret = mwifiex_sdio_poll_card_status(adapter, CARD_IO_READY |
695 DN_LD_CARD_RDY);
696 if (ret) {
697 dev_err(adapter->dev, "FW download with helper:"
698 " poll status timeout @ %d\n", offset);
699 goto done;
700 }
701
702 /* More data? */
703 if (offset >= firmware_len)
704 break;
705
706 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
707 ret = mwifiex_read_reg(adapter, HOST_F1_RD_BASE_0,
708 &base0);
709 if (ret) {
710 dev_err(adapter->dev, "dev BASE0 register read"
711 " failed: base0=0x%04X(%d). Terminating "
712 "download\n", base0, base0);
713 goto done;
714 }
715 ret = mwifiex_read_reg(adapter, HOST_F1_RD_BASE_1,
716 &base1);
717 if (ret) {
718 dev_err(adapter->dev, "dev BASE1 register read"
719 " failed: base1=0x%04X(%d). Terminating "
720 "download\n", base1, base1);
721 goto done;
722 }
723 len = (u16) (((base1 & 0xff) << 8) | (base0 & 0xff));
724
725 if (len)
726 break;
727
728 udelay(10);
729 }
730
731 if (!len) {
732 break;
733 } else if (len > MWIFIEX_UPLD_SIZE) {
734 dev_err(adapter->dev, "FW download failed @ %d,"
735 " invalid length %d\n", offset, len);
736 ret = -1;
737 goto done;
738 }
739
740 txlen = len;
741
742 if (len & BIT(0)) {
743 i++;
744 if (i > MAX_WRITE_IOMEM_RETRY) {
745 dev_err(adapter->dev, "FW download failed @"
746 " %d, over max retry count\n", offset);
747 ret = -1;
748 goto done;
749 }
750 dev_err(adapter->dev, "CRC indicated by the helper:"
751 " len = 0x%04X, txlen = %d\n", len, txlen);
752 len &= ~BIT(0);
753 /* Setting this to 0 to resend from same offset */
754 txlen = 0;
755 } else {
756 i = 0;
757
758 /* Set blocksize to transfer - checking for last
759 block */
760 if (firmware_len - offset < txlen)
761 txlen = firmware_len - offset;
762
763 tx_blocks = (txlen + MWIFIEX_SDIO_BLOCK_SIZE -
764 1) / MWIFIEX_SDIO_BLOCK_SIZE;
765
766 /* Copy payload to buffer */
767 memmove(fwbuf, &firmware[offset], txlen);
768 }
769
770 ret = mwifiex_write_data_sync(adapter, fwbuf, tx_blocks *
771 MWIFIEX_SDIO_BLOCK_SIZE,
772 adapter->ioport, 0);
773 if (ret) {
774 dev_err(adapter->dev, "FW download, write iomem (%d)"
775 " failed @ %d\n", i, offset);
776 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
777 dev_err(adapter->dev, "write CFG reg failed\n");
778
779 ret = -1;
780 goto done;
781 }
782
783 offset += txlen;
784 } while (true);
785
786 dev_dbg(adapter->dev, "info: FW download over, size %d bytes\n",
787 offset);
788
789 ret = 0;
790done:
791 kfree(fwbuf);
792 return ret;
793}
794
795/*
796 * This function checks the firmware status in card.
797 *
798 * The winner interface is also determined by this function.
799 */
800static int mwifiex_check_fw_status(struct mwifiex_adapter *adapter,
801 u32 poll_num, int *winner)
802{
803 int ret = 0;
804 u16 firmware_stat;
805 u32 tries;
806 u32 winner_status;
807
808 /* Wait for firmware initialization event */
809 for (tries = 0; tries < poll_num; tries++) {
810 ret = mwifiex_sdio_read_fw_status(adapter, &firmware_stat);
811 if (ret)
812 continue;
813 if (firmware_stat == FIRMWARE_READY) {
814 ret = 0;
815 break;
816 } else {
817 mdelay(100);
818 ret = -1;
819 }
820 }
821
822 if (winner && ret) {
823 if (mwifiex_read_reg
824 (adapter, CARD_FW_STATUS0_REG, &winner_status))
825 winner_status = 0;
826
827 if (winner_status)
828 *winner = 0;
829 else
830 *winner = 1;
831 }
832 return ret;
833}
834
835/*
836 * This function reads the interrupt status from card.
837 */
838static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
839{
840 struct sdio_mmc_card *card = adapter->card;
841 u32 sdio_ireg = 0;
842 unsigned long flags;
843
844 if (mwifiex_read_data_sync(adapter, card->mp_regs, MAX_MP_REGS,
845 REG_PORT | MWIFIEX_SDIO_BYTE_MODE_MASK, 0,
846 0)) {
847 dev_err(adapter->dev, "read mp_regs failed\n");
848 return;
849 }
850
851 sdio_ireg = card->mp_regs[HOST_INTSTATUS_REG];
852 if (sdio_ireg) {
853 /*
854 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
855 * Clear the interrupt status register
856 */
857 dev_dbg(adapter->dev, "int: sdio_ireg = %#x\n", sdio_ireg);
858 spin_lock_irqsave(&adapter->int_lock, flags);
859 adapter->int_status |= sdio_ireg;
860 spin_unlock_irqrestore(&adapter->int_lock, flags);
861 }
862
863 return;
864}
865
866/*
867 * SDIO interrupt handler.
868 *
869 * This function reads the interrupt status from firmware and assigns
870 * the main process in workqueue which will handle the interrupt.
871 */
872static void
873mwifiex_sdio_interrupt(struct sdio_func *func)
874{
875 struct mwifiex_adapter *adapter;
876 struct sdio_mmc_card *card;
877
878 card = sdio_get_drvdata(func);
879 if (!card || !card->adapter) {
880 pr_debug("int: func=%p card=%p adapter=%p\n",
881 func, card, card ? card->adapter : NULL);
882 return;
883 }
884 adapter = card->adapter;
885
886 if (adapter->surprise_removed)
887 return;
888
889 if (!adapter->pps_uapsd_mode && adapter->ps_state == PS_STATE_SLEEP)
890 adapter->ps_state = PS_STATE_AWAKE;
891
892 mwifiex_interrupt_status(adapter);
893 queue_work(adapter->workqueue, &adapter->main_work);
894
895 return;
896}
897
898/*
899 * This function decodes a received packet.
900 *
901 * Based on the type, the packet is treated as either a data, or
902 * a command response, or an event, and the correct handler
903 * function is invoked.
904 */
905static int mwifiex_decode_rx_packet(struct mwifiex_adapter *adapter,
906 struct sk_buff *skb, u32 upld_typ)
907{
908 u8 *cmd_buf;
909
910 skb_pull(skb, INTF_HEADER_LEN);
911
912 switch (upld_typ) {
913 case MWIFIEX_TYPE_DATA:
914 dev_dbg(adapter->dev, "info: --- Rx: Data packet ---\n");
915 mwifiex_handle_rx_packet(adapter, skb);
916 break;
917
918 case MWIFIEX_TYPE_CMD:
919 dev_dbg(adapter->dev, "info: --- Rx: Cmd Response ---\n");
920 /* take care of curr_cmd = NULL case */
921 if (!adapter->curr_cmd) {
922 cmd_buf = adapter->upld_buf;
923
924 if (adapter->ps_state == PS_STATE_SLEEP_CFM)
925 mwifiex_process_sleep_confirm_resp(adapter,
926 skb->data, skb->len);
927
928 memcpy(cmd_buf, skb->data, min_t(u32,
929 MWIFIEX_SIZE_OF_CMD_BUFFER, skb->len));
930
931 dev_kfree_skb_any(skb);
932 } else {
933 adapter->cmd_resp_received = true;
934 adapter->curr_cmd->resp_skb = skb;
935 }
936 break;
937
938 case MWIFIEX_TYPE_EVENT:
939 dev_dbg(adapter->dev, "info: --- Rx: Event ---\n");
940 adapter->event_cause = *(u32 *) skb->data;
941
942 skb_pull(skb, MWIFIEX_EVENT_HEADER_LEN);
943
944 if ((skb->len > 0) && (skb->len < MAX_EVENT_SIZE))
945 memcpy(adapter->event_body, skb->data, skb->len);
946
947 /* event cause has been saved to adapter->event_cause */
948 adapter->event_received = true;
949 adapter->event_skb = skb;
950
951 break;
952
953 default:
954 dev_err(adapter->dev, "unknown upload type %#x\n", upld_typ);
955 dev_kfree_skb_any(skb);
956 break;
957 }
958
959 return 0;
960}
961
962/*
963 * This function transfers received packets from card to driver, performing
964 * aggregation if required.
965 *
966 * For data received on control port, or if aggregation is disabled, the
967 * received buffers are uploaded as separate packets. However, if aggregation
968 * is enabled and required, the buffers are copied onto an aggregation buffer,
969 * provided there is space left, processed and finally uploaded.
970 */
971static int mwifiex_sdio_card_to_host_mp_aggr(struct mwifiex_adapter *adapter,
972 struct sk_buff *skb, u8 port)
973{
974 struct sdio_mmc_card *card = adapter->card;
975 s32 f_do_rx_aggr = 0;
976 s32 f_do_rx_cur = 0;
977 s32 f_aggr_cur = 0;
978 struct sk_buff *skb_deaggr;
979 u32 pind = 0;
980 u32 pkt_len, pkt_type = 0;
981 u8 *curr_ptr;
982 u32 rx_len = skb->len;
983
984 if (port == CTRL_PORT) {
985 /* Read the command Resp without aggr */
986 dev_dbg(adapter->dev, "info: %s: no aggregation for cmd "
987 "response\n", __func__);
988
989 f_do_rx_cur = 1;
990 goto rx_curr_single;
991 }
992
993 if (!card->mpa_rx.enabled) {
994 dev_dbg(adapter->dev, "info: %s: rx aggregation disabled\n",
995 __func__);
996
997 f_do_rx_cur = 1;
998 goto rx_curr_single;
999 }
1000
1001 if (card->mp_rd_bitmap & (~((u16) CTRL_PORT_MASK))) {
1002 /* Some more data RX pending */
1003 dev_dbg(adapter->dev, "info: %s: not last packet\n", __func__);
1004
1005 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1006 if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len)) {
1007 f_aggr_cur = 1;
1008 } else {
1009 /* No room in Aggr buf, do rx aggr now */
1010 f_do_rx_aggr = 1;
1011 f_do_rx_cur = 1;
1012 }
1013 } else {
1014 /* Rx aggr not in progress */
1015 f_aggr_cur = 1;
1016 }
1017
1018 } else {
1019 /* No more data RX pending */
1020 dev_dbg(adapter->dev, "info: %s: last packet\n", __func__);
1021
1022 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1023 f_do_rx_aggr = 1;
1024 if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len))
1025 f_aggr_cur = 1;
1026 else
1027 /* No room in Aggr buf, do rx aggr now */
1028 f_do_rx_cur = 1;
1029 } else {
1030 f_do_rx_cur = 1;
1031 }
1032 }
1033
1034 if (f_aggr_cur) {
1035 dev_dbg(adapter->dev, "info: current packet aggregation\n");
1036 /* Curr pkt can be aggregated */
1037 MP_RX_AGGR_SETUP(card, skb, port);
1038
1039 if (MP_RX_AGGR_PKT_LIMIT_REACHED(card) ||
1040 MP_RX_AGGR_PORT_LIMIT_REACHED(card)) {
1041 dev_dbg(adapter->dev, "info: %s: aggregated packet "
1042 "limit reached\n", __func__);
1043 /* No more pkts allowed in Aggr buf, rx it */
1044 f_do_rx_aggr = 1;
1045 }
1046 }
1047
1048 if (f_do_rx_aggr) {
1049 /* do aggr RX now */
1050 dev_dbg(adapter->dev, "info: do_rx_aggr: num of packets: %d\n",
1051 card->mpa_rx.pkt_cnt);
1052
1053 if (mwifiex_read_data_sync(adapter, card->mpa_rx.buf,
1054 card->mpa_rx.buf_len,
1055 (adapter->ioport | 0x1000 |
1056 (card->mpa_rx.ports << 4)) +
1057 card->mpa_rx.start_port, 0, 1))
1058 return -1;
1059
1060 curr_ptr = card->mpa_rx.buf;
1061
1062 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1063
1064 /* get curr PKT len & type */
1065 pkt_len = *(u16 *) &curr_ptr[0];
1066 pkt_type = *(u16 *) &curr_ptr[2];
1067
1068 /* copy pkt to deaggr buf */
1069 skb_deaggr = card->mpa_rx.skb_arr[pind];
1070
1071 if ((pkt_type == MWIFIEX_TYPE_DATA) && (pkt_len <=
1072 card->mpa_rx.len_arr[pind])) {
1073
1074 memcpy(skb_deaggr->data, curr_ptr, pkt_len);
1075
1076 skb_trim(skb_deaggr, pkt_len);
1077
1078 /* Process de-aggr packet */
1079 mwifiex_decode_rx_packet(adapter, skb_deaggr,
1080 pkt_type);
1081 } else {
1082 dev_err(adapter->dev, "wrong aggr pkt:"
1083 " type=%d len=%d max_len=%d\n",
1084 pkt_type, pkt_len,
1085 card->mpa_rx.len_arr[pind]);
1086 dev_kfree_skb_any(skb_deaggr);
1087 }
1088 curr_ptr += card->mpa_rx.len_arr[pind];
1089 }
1090 MP_RX_AGGR_BUF_RESET(card);
1091 }
1092
1093rx_curr_single:
1094 if (f_do_rx_cur) {
1095 dev_dbg(adapter->dev, "info: RX: port: %d, rx_len: %d\n",
1096 port, rx_len);
1097
1098 if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1099 skb->data, skb->len,
1100 adapter->ioport + port))
1101 return -1;
1102
1103 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1104 }
1105
1106 return 0;
1107}
1108
1109/*
1110 * This function checks the current interrupt status.
1111 *
1112 * The following interrupts are checked and handled by this function -
1113 * - Data sent
1114 * - Command sent
1115 * - Packets received
1116 *
1117 * Since the firmware does not generate download ready interrupt if the
1118 * port updated is command port only, command sent interrupt checking
1119 * should be done manually, and for every SDIO interrupt.
1120 *
1121 * In case of Rx packets received, the packets are uploaded from card to
1122 * host and processed accordingly.
1123 */
1124static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
1125{
1126 struct sdio_mmc_card *card = adapter->card;
1127 int ret = 0;
1128 u8 sdio_ireg;
1129 struct sk_buff *skb = NULL;
1130 u8 port = CTRL_PORT;
1131 u32 len_reg_l, len_reg_u;
1132 u32 rx_blocks;
1133 u16 rx_len;
1134 unsigned long flags;
1135
1136 spin_lock_irqsave(&adapter->int_lock, flags);
1137 sdio_ireg = adapter->int_status;
1138 adapter->int_status = 0;
1139 spin_unlock_irqrestore(&adapter->int_lock, flags);
1140
1141 if (!sdio_ireg)
1142 return ret;
1143
1144 if (sdio_ireg & DN_LD_HOST_INT_STATUS) {
1145 card->mp_wr_bitmap = ((u16) card->mp_regs[WR_BITMAP_U]) << 8;
1146 card->mp_wr_bitmap |= (u16) card->mp_regs[WR_BITMAP_L];
1147 dev_dbg(adapter->dev, "int: DNLD: wr_bitmap=0x%04x\n",
1148 card->mp_wr_bitmap);
1149 if (adapter->data_sent &&
1150 (card->mp_wr_bitmap & card->mp_data_port_mask)) {
1151 dev_dbg(adapter->dev,
1152 "info: <--- Tx DONE Interrupt --->\n");
1153 adapter->data_sent = false;
1154 }
1155 }
1156
1157 /* As firmware will not generate download ready interrupt if the port
1158 updated is command port only, cmd_sent should be done for any SDIO
1159 interrupt. */
1160 if (adapter->cmd_sent) {
1161 /* Check if firmware has attach buffer at command port and
1162 update just that in wr_bit_map. */
1163 card->mp_wr_bitmap |=
1164 (u16) card->mp_regs[WR_BITMAP_L] & CTRL_PORT_MASK;
1165 if (card->mp_wr_bitmap & CTRL_PORT_MASK)
1166 adapter->cmd_sent = false;
1167 }
1168
1169 dev_dbg(adapter->dev, "info: cmd_sent=%d data_sent=%d\n",
1170 adapter->cmd_sent, adapter->data_sent);
1171 if (sdio_ireg & UP_LD_HOST_INT_STATUS) {
1172 card->mp_rd_bitmap = ((u16) card->mp_regs[RD_BITMAP_U]) << 8;
1173 card->mp_rd_bitmap |= (u16) card->mp_regs[RD_BITMAP_L];
1174 dev_dbg(adapter->dev, "int: UPLD: rd_bitmap=0x%04x\n",
1175 card->mp_rd_bitmap);
1176
1177 while (true) {
1178 ret = mwifiex_get_rd_port(adapter, &port);
1179 if (ret) {
1180 dev_dbg(adapter->dev,
1181 "info: no more rd_port available\n");
1182 break;
1183 }
1184 len_reg_l = RD_LEN_P0_L + (port << 1);
1185 len_reg_u = RD_LEN_P0_U + (port << 1);
1186 rx_len = ((u16) card->mp_regs[len_reg_u]) << 8;
1187 rx_len |= (u16) card->mp_regs[len_reg_l];
1188 dev_dbg(adapter->dev, "info: RX: port=%d rx_len=%u\n",
1189 port, rx_len);
1190 rx_blocks =
1191 (rx_len + MWIFIEX_SDIO_BLOCK_SIZE -
1192 1) / MWIFIEX_SDIO_BLOCK_SIZE;
1193 if (rx_len <= INTF_HEADER_LEN
1194 || (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1195 MWIFIEX_RX_DATA_BUF_SIZE) {
1196 dev_err(adapter->dev, "invalid rx_len=%d\n",
1197 rx_len);
1198 return -1;
1199 }
1200 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1201
1202 skb = dev_alloc_skb(rx_len);
1203
1204 if (!skb) {
1205 dev_err(adapter->dev, "%s: failed to alloc skb",
1206 __func__);
1207 return -1;
1208 }
1209
1210 skb_put(skb, rx_len);
1211
1212 dev_dbg(adapter->dev, "info: rx_len = %d skb->len = %d\n",
1213 rx_len, skb->len);
1214
1215 if (mwifiex_sdio_card_to_host_mp_aggr(adapter, skb,
1216 port)) {
1217 u32 cr = 0;
1218
1219 dev_err(adapter->dev, "card_to_host_mpa failed:"
1220 " int status=%#x\n", sdio_ireg);
1221 if (mwifiex_read_reg(adapter,
1222 CONFIGURATION_REG, &cr))
1223 dev_err(adapter->dev,
1224 "read CFG reg failed\n");
1225
1226 dev_dbg(adapter->dev,
1227 "info: CFG reg val = %d\n", cr);
1228 if (mwifiex_write_reg(adapter,
1229 CONFIGURATION_REG,
1230 (cr | 0x04)))
1231 dev_err(adapter->dev,
1232 "write CFG reg failed\n");
1233
1234 dev_dbg(adapter->dev, "info: write success\n");
1235 if (mwifiex_read_reg(adapter,
1236 CONFIGURATION_REG, &cr))
1237 dev_err(adapter->dev,
1238 "read CFG reg failed\n");
1239
1240 dev_dbg(adapter->dev,
1241 "info: CFG reg val =%x\n", cr);
1242 dev_kfree_skb_any(skb);
1243 return -1;
1244 }
1245 }
1246 }
1247
1248 return 0;
1249}
1250
1251/*
1252 * This function aggregates transmission buffers in driver and downloads
1253 * the aggregated packet to card.
1254 *
1255 * The individual packets are aggregated by copying into an aggregation
1256 * buffer and then downloaded to the card. Previous unsent packets in the
1257 * aggregation buffer are pre-copied first before new packets are added.
1258 * Aggregation is done till there is space left in the aggregation buffer,
1259 * or till new packets are available.
1260 *
1261 * The function will only download the packet to the card when aggregation
1262 * stops, otherwise it will just aggregate the packet in aggregation buffer
1263 * and return.
1264 */
1265static int mwifiex_host_to_card_mp_aggr(struct mwifiex_adapter *adapter,
1266 u8 *payload, u32 pkt_len, u8 port,
1267 u32 next_pkt_len)
1268{
1269 struct sdio_mmc_card *card = adapter->card;
1270 int ret = 0;
1271 s32 f_send_aggr_buf = 0;
1272 s32 f_send_cur_buf = 0;
1273 s32 f_precopy_cur_buf = 0;
1274 s32 f_postcopy_cur_buf = 0;
1275
1276 if ((!card->mpa_tx.enabled) || (port == CTRL_PORT)) {
1277 dev_dbg(adapter->dev, "info: %s: tx aggregation disabled\n",
1278 __func__);
1279
1280 f_send_cur_buf = 1;
1281 goto tx_curr_single;
1282 }
1283
1284 if (next_pkt_len) {
1285 /* More pkt in TX queue */
1286 dev_dbg(adapter->dev, "info: %s: more packets in queue.\n",
1287 __func__);
1288
1289 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1290 if (!MP_TX_AGGR_PORT_LIMIT_REACHED(card) &&
1291 MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len)) {
1292 f_precopy_cur_buf = 1;
1293
1294 if (!(card->mp_wr_bitmap &
1295 (1 << card->curr_wr_port))
1296 || !MP_TX_AGGR_BUF_HAS_ROOM(
1297 card, next_pkt_len))
1298 f_send_aggr_buf = 1;
1299 } else {
1300 /* No room in Aggr buf, send it */
1301 f_send_aggr_buf = 1;
1302
1303 if (MP_TX_AGGR_PORT_LIMIT_REACHED(card) ||
1304 !(card->mp_wr_bitmap &
1305 (1 << card->curr_wr_port)))
1306 f_send_cur_buf = 1;
1307 else
1308 f_postcopy_cur_buf = 1;
1309 }
1310 } else {
1311 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len)
1312 && (card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1313 f_precopy_cur_buf = 1;
1314 else
1315 f_send_cur_buf = 1;
1316 }
1317 } else {
1318 /* Last pkt in TX queue */
1319 dev_dbg(adapter->dev, "info: %s: Last packet in Tx Queue.\n",
1320 __func__);
1321
1322 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1323 /* some packs in Aggr buf already */
1324 f_send_aggr_buf = 1;
1325
1326 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len))
1327 f_precopy_cur_buf = 1;
1328 else
1329 /* No room in Aggr buf, send it */
1330 f_send_cur_buf = 1;
1331 } else {
1332 f_send_cur_buf = 1;
1333 }
1334 }
1335
1336 if (f_precopy_cur_buf) {
1337 dev_dbg(adapter->dev, "data: %s: precopy current buffer\n",
1338 __func__);
1339 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1340
1341 if (MP_TX_AGGR_PKT_LIMIT_REACHED(card) ||
1342 MP_TX_AGGR_PORT_LIMIT_REACHED(card))
1343 /* No more pkts allowed in Aggr buf, send it */
1344 f_send_aggr_buf = 1;
1345 }
1346
1347 if (f_send_aggr_buf) {
1348 dev_dbg(adapter->dev, "data: %s: send aggr buffer: %d %d\n",
1349 __func__,
1350 card->mpa_tx.start_port, card->mpa_tx.ports);
1351 ret = mwifiex_write_data_to_card(adapter, card->mpa_tx.buf,
1352 card->mpa_tx.buf_len,
1353 (adapter->ioport | 0x1000 |
1354 (card->mpa_tx.ports << 4)) +
1355 card->mpa_tx.start_port);
1356
1357 MP_TX_AGGR_BUF_RESET(card);
1358 }
1359
1360tx_curr_single:
1361 if (f_send_cur_buf) {
1362 dev_dbg(adapter->dev, "data: %s: send current buffer %d\n",
1363 __func__, port);
1364 ret = mwifiex_write_data_to_card(adapter, payload, pkt_len,
1365 adapter->ioport + port);
1366 }
1367
1368 if (f_postcopy_cur_buf) {
1369 dev_dbg(adapter->dev, "data: %s: postcopy current buffer\n",
1370 __func__);
1371 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1372 }
1373
1374 return ret;
1375}
1376
1377/*
1378 * This function downloads data from driver to card.
1379 *
1380 * Both commands and data packets are transferred to the card by this
1381 * function.
1382 *
1383 * This function adds the SDIO specific header to the front of the buffer
1384 * before transferring. The header contains the length of the packet and
1385 * the type. The firmware handles the packets based upon this set type.
1386 */
1387static int mwifiex_sdio_host_to_card(struct mwifiex_adapter *adapter,
1388 u8 type, u8 *payload, u32 pkt_len,
1389 struct mwifiex_tx_param *tx_param)
1390{
1391 struct sdio_mmc_card *card = adapter->card;
1392 int ret = 0;
1393 u32 buf_block_len;
1394 u32 blk_size;
1395 u8 port = CTRL_PORT;
1396
1397 /* Allocate buffer and copy payload */
1398 blk_size = MWIFIEX_SDIO_BLOCK_SIZE;
1399 buf_block_len = (pkt_len + blk_size - 1) / blk_size;
1400 *(u16 *) &payload[0] = (u16) pkt_len;
1401 *(u16 *) &payload[2] = type;
1402
1403 /*
1404 * This is SDIO specific header
1405 * u16 length,
1406 * u16 type (MWIFIEX_TYPE_DATA = 0, MWIFIEX_TYPE_CMD = 1,
1407 * MWIFIEX_TYPE_EVENT = 3)
1408 */
1409 if (type == MWIFIEX_TYPE_DATA) {
1410 ret = mwifiex_get_wr_port_data(adapter, &port);
1411 if (ret) {
1412 dev_err(adapter->dev, "%s: no wr_port available\n",
1413 __func__);
1414 return ret;
1415 }
1416 } else {
1417 adapter->cmd_sent = true;
1418 /* Type must be MWIFIEX_TYPE_CMD */
1419
1420 if (pkt_len <= INTF_HEADER_LEN ||
1421 pkt_len > MWIFIEX_UPLD_SIZE)
1422 dev_err(adapter->dev, "%s: payload=%p, nb=%d\n",
1423 __func__, payload, pkt_len);
1424 }
1425
1426 /* Transfer data to card */
1427 pkt_len = buf_block_len * blk_size;
1428
1429 if (tx_param)
1430 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1431 port, tx_param->next_pkt_len);
1432 else
1433 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1434 port, 0);
1435
1436 if (ret) {
1437 if (type == MWIFIEX_TYPE_CMD)
1438 adapter->cmd_sent = false;
1439 if (type == MWIFIEX_TYPE_DATA)
1440 adapter->data_sent = false;
1441 } else {
1442 if (type == MWIFIEX_TYPE_DATA) {
1443 if (!(card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1444 adapter->data_sent = true;
1445 else
1446 adapter->data_sent = false;
1447 }
1448 }
1449
1450 return ret;
1451}
1452
1453/*
1454 * This function allocates the MPA Tx and Rx buffers.
1455 */
1456static int mwifiex_alloc_sdio_mpa_buffers(struct mwifiex_adapter *adapter,
1457 u32 mpa_tx_buf_size, u32 mpa_rx_buf_size)
1458{
1459 struct sdio_mmc_card *card = adapter->card;
1460 int ret = 0;
1461
1462 card->mpa_tx.buf = kzalloc(mpa_tx_buf_size, GFP_KERNEL);
1463 if (!card->mpa_tx.buf) {
1464 dev_err(adapter->dev, "could not alloc buffer for MP-A TX\n");
1465 ret = -1;
1466 goto error;
1467 }
1468
1469 card->mpa_tx.buf_size = mpa_tx_buf_size;
1470
1471 card->mpa_rx.buf = kzalloc(mpa_rx_buf_size, GFP_KERNEL);
1472 if (!card->mpa_rx.buf) {
1473 dev_err(adapter->dev, "could not alloc buffer for MP-A RX\n");
1474 ret = -1;
1475 goto error;
1476 }
1477
1478 card->mpa_rx.buf_size = mpa_rx_buf_size;
1479
1480error:
1481 if (ret) {
1482 kfree(card->mpa_tx.buf);
1483 kfree(card->mpa_rx.buf);
1484 }
1485
1486 return ret;
1487}
1488
1489/*
1490 * This function unregisters the SDIO device.
1491 *
1492 * The SDIO IRQ is released, the function is disabled and driver
1493 * data is set to null.
1494 */
1495static void
1496mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
1497{
1498 struct sdio_mmc_card *card = adapter->card;
1499
1500 if (adapter->card) {
1501 /* Release the SDIO IRQ */
1502 sdio_claim_host(card->func);
1503 sdio_release_irq(card->func);
1504 sdio_disable_func(card->func);
1505 sdio_release_host(card->func);
1506 sdio_set_drvdata(card->func, NULL);
1507 }
1508}
1509
1510/*
1511 * This function registers the SDIO device.
1512 *
1513 * SDIO IRQ is claimed, block size is set and driver data is initialized.
1514 */
1515static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
1516{
1517 int ret = 0;
1518 struct sdio_mmc_card *card = adapter->card;
1519 struct sdio_func *func = card->func;
1520
1521 /* save adapter pointer in card */
1522 card->adapter = adapter;
1523
1524 sdio_claim_host(func);
1525
1526 /* Request the SDIO IRQ */
1527 ret = sdio_claim_irq(func, mwifiex_sdio_interrupt);
1528 if (ret) {
1529 pr_err("claim irq failed: ret=%d\n", ret);
1530 goto disable_func;
1531 }
1532
1533 /* Set block size */
1534 ret = sdio_set_block_size(card->func, MWIFIEX_SDIO_BLOCK_SIZE);
1535 if (ret) {
1536 pr_err("cannot set SDIO block size\n");
1537 ret = -1;
1538 goto release_irq;
1539 }
1540
1541 sdio_release_host(func);
1542 sdio_set_drvdata(func, card);
1543
1544 adapter->dev = &func->dev;
1545
1546 return 0;
1547
1548release_irq:
1549 sdio_release_irq(func);
1550disable_func:
1551 sdio_disable_func(func);
1552 sdio_release_host(func);
1553 adapter->card = NULL;
1554
1555 return -1;
1556}
1557
1558/*
1559 * This function initializes the SDIO driver.
1560 *
1561 * The following initializations steps are followed -
1562 * - Read the Host interrupt status register to acknowledge
1563 * the first interrupt got from bootloader
1564 * - Disable host interrupt mask register
1565 * - Get SDIO port
1566 * - Get revision ID
1567 * - Initialize SDIO variables in card
1568 * - Allocate MP registers
1569 * - Allocate MPA Tx and Rx buffers
1570 */
1571static int mwifiex_init_sdio(struct mwifiex_adapter *adapter)
1572{
1573 struct sdio_mmc_card *card = adapter->card;
1574 int ret;
1575 u32 sdio_ireg = 0;
1576
1577 /*
1578 * Read the HOST_INT_STATUS_REG for ACK the first interrupt got
1579 * from the bootloader. If we don't do this we get a interrupt
1580 * as soon as we register the irq.
1581 */
1582 mwifiex_read_reg(adapter, HOST_INTSTATUS_REG, &sdio_ireg);
1583
1584 /* Disable host interrupt mask register for SDIO */
1585 mwifiex_sdio_disable_host_int(adapter);
1586
1587 /* Get SDIO ioport */
1588 mwifiex_init_sdio_ioport(adapter);
1589
1590 /* Get revision ID */
1591#define REV_ID_REG 0x5c
1592 mwifiex_read_reg(adapter, REV_ID_REG, &adapter->revision_id);
1593
1594 /* Initialize SDIO variables in card */
1595 card->mp_rd_bitmap = 0;
1596 card->mp_wr_bitmap = 0;
1597 card->curr_rd_port = 1;
1598 card->curr_wr_port = 1;
1599
1600 card->mp_data_port_mask = DATA_PORT_MASK;
1601
1602 card->mpa_tx.buf_len = 0;
1603 card->mpa_tx.pkt_cnt = 0;
1604 card->mpa_tx.start_port = 0;
1605
1606 card->mpa_tx.enabled = 0;
1607 card->mpa_tx.pkt_aggr_limit = SDIO_MP_AGGR_DEF_PKT_LIMIT;
1608
1609 card->mpa_rx.buf_len = 0;
1610 card->mpa_rx.pkt_cnt = 0;
1611 card->mpa_rx.start_port = 0;
1612
1613 card->mpa_rx.enabled = 0;
1614 card->mpa_rx.pkt_aggr_limit = SDIO_MP_AGGR_DEF_PKT_LIMIT;
1615
1616 /* Allocate buffers for SDIO MP-A */
1617 card->mp_regs = kzalloc(MAX_MP_REGS, GFP_KERNEL);
1618 if (!card->mp_regs) {
1619 dev_err(adapter->dev, "failed to alloc mp_regs\n");
1620 return -1;
1621 }
1622
1623 ret = mwifiex_alloc_sdio_mpa_buffers(adapter,
1624 SDIO_MP_TX_AGGR_DEF_BUF_SIZE,
1625 SDIO_MP_RX_AGGR_DEF_BUF_SIZE);
1626 if (ret) {
1627 dev_err(adapter->dev, "failed to alloc sdio mp-a buffers\n");
1628 kfree(card->mp_regs);
1629 return -1;
1630 }
1631
1632 return ret;
1633}
1634
1635/*
1636 * This function resets the MPA Tx and Rx buffers.
1637 */
1638static void mwifiex_cleanup_mpa_buf(struct mwifiex_adapter *adapter)
1639{
1640 struct sdio_mmc_card *card = adapter->card;
1641
1642 MP_TX_AGGR_BUF_RESET(card);
1643 MP_RX_AGGR_BUF_RESET(card);
1644}
1645
1646/*
1647 * This function cleans up the allocated card buffers.
1648 *
1649 * The following are freed by this function -
1650 * - MP registers
1651 * - MPA Tx buffer
1652 * - MPA Rx buffer
1653 */
1654static void mwifiex_cleanup_sdio(struct mwifiex_adapter *adapter)
1655{
1656 struct sdio_mmc_card *card = adapter->card;
1657
1658 kfree(card->mp_regs);
1659 kfree(card->mpa_tx.buf);
1660 kfree(card->mpa_rx.buf);
1661}
1662
1663/*
1664 * This function updates the MP end port in card.
1665 */
1666static void
1667mwifiex_update_mp_end_port(struct mwifiex_adapter *adapter, u16 port)
1668{
1669 struct sdio_mmc_card *card = adapter->card;
1670 int i;
1671
1672 card->mp_end_port = port;
1673
1674 card->mp_data_port_mask = DATA_PORT_MASK;
1675
1676 for (i = 1; i <= MAX_PORT - card->mp_end_port; i++)
1677 card->mp_data_port_mask &= ~(1 << (MAX_PORT - i));
1678
1679 card->curr_wr_port = 1;
1680
1681 dev_dbg(adapter->dev, "cmd: mp_end_port %d, data port mask 0x%x\n",
1682 port, card->mp_data_port_mask);
1683}
1684
1685static struct mwifiex_if_ops sdio_ops = {
1686 .init_if = mwifiex_init_sdio,
1687 .cleanup_if = mwifiex_cleanup_sdio,
1688 .check_fw_status = mwifiex_check_fw_status,
1689 .prog_fw = mwifiex_prog_fw_w_helper,
1690 .register_dev = mwifiex_register_dev,
1691 .unregister_dev = mwifiex_unregister_dev,
1692 .enable_int = mwifiex_sdio_enable_host_int,
1693 .process_int_status = mwifiex_process_int_status,
1694 .host_to_card = mwifiex_sdio_host_to_card,
1695 .wakeup = mwifiex_pm_wakeup_card,
1696 .wakeup_complete = mwifiex_pm_wakeup_card_complete,
1697
1698 /* SDIO specific */
1699 .update_mp_end_port = mwifiex_update_mp_end_port,
1700 .cleanup_mpa_buf = mwifiex_cleanup_mpa_buf,
1701};
1702
1703/*
1704 * This function initializes the SDIO driver.
1705 *
1706 * This initiates the semaphore and registers the device with
1707 * SDIO bus.
1708 */
1709static int
1710mwifiex_sdio_init_module(void)
1711{
1712 int ret;
1713
1714 sema_init(&add_remove_card_sem, 1);
1715
1716 ret = sdio_register_driver(&mwifiex_sdio);
1717
1718 return ret;
1719}
1720
1721/*
1722 * This function cleans up the SDIO driver.
1723 *
1724 * The following major steps are followed for cleanup -
1725 * - Resume the device if its suspended
1726 * - Disconnect the device if connected
1727 * - Shutdown the firmware
1728 * - Unregister the device from SDIO bus.
1729 */
1730static void
1731mwifiex_sdio_cleanup_module(void)
1732{
1733 struct mwifiex_adapter *adapter = g_adapter;
1734 int i;
1735
1736 if (down_interruptible(&add_remove_card_sem))
1737 goto exit_sem_err;
1738
1739 if (!adapter || !adapter->priv_num)
1740 goto exit;
1741
1742 if (adapter->is_suspended)
1743 mwifiex_sdio_resume(adapter->dev);
1744
1745 for (i = 0; i < adapter->priv_num; i++)
1746 if ((GET_BSS_ROLE(adapter->priv[i]) == MWIFIEX_BSS_ROLE_STA) &&
1747 adapter->priv[i]->media_connected)
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001748 mwifiex_deauthenticate(adapter->priv[i], NULL);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001749
1750 if (!adapter->surprise_removed)
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001751 mwifiex_init_shutdown_fw(mwifiex_get_priv(adapter,
1752 MWIFIEX_BSS_ROLE_ANY),
1753 MWIFIEX_FUNC_SHUTDOWN);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001754
1755exit:
1756 up(&add_remove_card_sem);
1757
1758exit_sem_err:
1759 sdio_unregister_driver(&mwifiex_sdio);
1760}
1761
1762module_init(mwifiex_sdio_init_module);
1763module_exit(mwifiex_sdio_cleanup_module);
1764
1765MODULE_AUTHOR("Marvell International Ltd.");
1766MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION);
1767MODULE_VERSION(SDIO_VERSION);
1768MODULE_LICENSE("GPL v2");
1769MODULE_FIRMWARE("sd8787.bin");