fix a kmap leak in virtio_console
[linux/fpc-iii.git] / drivers / net / wireless / mwifiex / sdio.c
blobb44a31523461e2c5ba7d768c941fc0934abaacb6
1 /*
2 * Marvell Wireless LAN device driver: SDIO specific handling
4 * Copyright (C) 2011, Marvell International Ltd.
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.
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.
20 #include <linux/firmware.h>
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"
32 #define SDIO_VERSION "1.0"
34 /* The mwifiex_sdio_remove() callback function is called when
35 * user removes this module from kernel space or ejects
36 * the card from the slot. The driver handles these 2 cases
37 * differently.
38 * If the user is removing the module, the few commands (FUNC_SHUTDOWN,
39 * HS_CANCEL etc.) are sent to the firmware.
40 * If the card is removed, there is no need to send these command.
42 * The variable 'user_rmmod' is used to distinguish these two
43 * scenarios. This flag is initialized as FALSE in case the card
44 * is removed, and will be set to TRUE for module removal when
45 * module_exit function is called.
47 static u8 user_rmmod;
49 static struct mwifiex_if_ops sdio_ops;
51 static struct semaphore add_remove_card_sem;
54 * SDIO probe.
56 * This function probes an mwifiex device and registers it. It allocates
57 * the card structure, enables SDIO function number and initiates the
58 * device registration and initialization procedure by adding a logical
59 * interface.
61 static int
62 mwifiex_sdio_probe(struct sdio_func *func, const struct sdio_device_id *id)
64 int ret;
65 struct sdio_mmc_card *card = NULL;
67 pr_debug("info: vendor=0x%4.04X device=0x%4.04X class=%d function=%d\n",
68 func->vendor, func->device, func->class, func->num);
70 card = kzalloc(sizeof(struct sdio_mmc_card), GFP_KERNEL);
71 if (!card)
72 return -ENOMEM;
74 card->func = func;
76 func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
78 if (id->driver_data) {
79 struct mwifiex_sdio_device *data = (void *)id->driver_data;
81 card->firmware = data->firmware;
82 card->reg = data->reg;
83 card->max_ports = data->max_ports;
84 card->mp_agg_pkt_limit = data->mp_agg_pkt_limit;
85 card->supports_sdio_new_mode = data->supports_sdio_new_mode;
86 card->has_control_mask = data->has_control_mask;
89 sdio_claim_host(func);
90 ret = sdio_enable_func(func);
91 sdio_release_host(func);
93 if (ret) {
94 pr_err("%s: failed to enable function\n", __func__);
95 kfree(card);
96 return -EIO;
99 if (mwifiex_add_card(card, &add_remove_card_sem, &sdio_ops,
100 MWIFIEX_SDIO)) {
101 pr_err("%s: add card failed\n", __func__);
102 kfree(card);
103 sdio_claim_host(func);
104 ret = sdio_disable_func(func);
105 sdio_release_host(func);
106 ret = -1;
109 return ret;
113 * SDIO resume.
115 * Kernel needs to suspend all functions separately. Therefore all
116 * registered functions must have drivers with suspend and resume
117 * methods. Failing that the kernel simply removes the whole card.
119 * If already not resumed, this function turns on the traffic and
120 * sends a host sleep cancel request to the firmware.
122 static int mwifiex_sdio_resume(struct device *dev)
124 struct sdio_func *func = dev_to_sdio_func(dev);
125 struct sdio_mmc_card *card;
126 struct mwifiex_adapter *adapter;
127 mmc_pm_flag_t pm_flag = 0;
129 if (func) {
130 pm_flag = sdio_get_host_pm_caps(func);
131 card = sdio_get_drvdata(func);
132 if (!card || !card->adapter) {
133 pr_err("resume: invalid card or adapter\n");
134 return 0;
136 } else {
137 pr_err("resume: sdio_func is not specified\n");
138 return 0;
141 adapter = card->adapter;
143 if (!adapter->is_suspended) {
144 dev_warn(adapter->dev, "device already resumed\n");
145 return 0;
148 adapter->is_suspended = false;
150 /* Disable Host Sleep */
151 mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
152 MWIFIEX_ASYNC_CMD);
154 return 0;
158 * SDIO remove.
160 * This function removes the interface and frees up the card structure.
162 static void
163 mwifiex_sdio_remove(struct sdio_func *func)
165 struct sdio_mmc_card *card;
166 struct mwifiex_adapter *adapter;
167 struct mwifiex_private *priv;
168 int i;
170 pr_debug("info: SDIO func num=%d\n", func->num);
172 card = sdio_get_drvdata(func);
173 if (!card)
174 return;
176 adapter = card->adapter;
177 if (!adapter || !adapter->priv_num)
178 return;
180 /* In case driver is removed when asynchronous FW load is in progress */
181 wait_for_completion(&adapter->fw_load);
183 if (user_rmmod) {
184 if (adapter->is_suspended)
185 mwifiex_sdio_resume(adapter->dev);
187 for (i = 0; i < adapter->priv_num; i++)
188 if ((GET_BSS_ROLE(adapter->priv[i]) ==
189 MWIFIEX_BSS_ROLE_STA) &&
190 adapter->priv[i]->media_connected)
191 mwifiex_deauthenticate(adapter->priv[i], NULL);
193 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
194 mwifiex_disable_auto_ds(priv);
195 mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
198 mwifiex_remove_card(card->adapter, &add_remove_card_sem);
202 * SDIO suspend.
204 * Kernel needs to suspend all functions separately. Therefore all
205 * registered functions must have drivers with suspend and resume
206 * methods. Failing that the kernel simply removes the whole card.
208 * If already not suspended, this function allocates and sends a host
209 * sleep activate request to the firmware and turns off the traffic.
211 static int mwifiex_sdio_suspend(struct device *dev)
213 struct sdio_func *func = dev_to_sdio_func(dev);
214 struct sdio_mmc_card *card;
215 struct mwifiex_adapter *adapter;
216 mmc_pm_flag_t pm_flag = 0;
217 int ret = 0;
219 if (func) {
220 pm_flag = sdio_get_host_pm_caps(func);
221 pr_debug("cmd: %s: suspend: PM flag = 0x%x\n",
222 sdio_func_id(func), pm_flag);
223 if (!(pm_flag & MMC_PM_KEEP_POWER)) {
224 pr_err("%s: cannot remain alive while host is"
225 " suspended\n", sdio_func_id(func));
226 return -ENOSYS;
229 card = sdio_get_drvdata(func);
230 if (!card || !card->adapter) {
231 pr_err("suspend: invalid card or adapter\n");
232 return 0;
234 } else {
235 pr_err("suspend: sdio_func is not specified\n");
236 return 0;
239 adapter = card->adapter;
241 /* Enable the Host Sleep */
242 if (!mwifiex_enable_hs(adapter)) {
243 dev_err(adapter->dev, "cmd: failed to suspend\n");
244 return -EFAULT;
247 dev_dbg(adapter->dev, "cmd: suspend with MMC_PM_KEEP_POWER\n");
248 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
250 /* Indicate device suspended */
251 adapter->is_suspended = true;
253 return ret;
256 /* Device ID for SD8786 */
257 #define SDIO_DEVICE_ID_MARVELL_8786 (0x9116)
258 /* Device ID for SD8787 */
259 #define SDIO_DEVICE_ID_MARVELL_8787 (0x9119)
260 /* Device ID for SD8797 */
261 #define SDIO_DEVICE_ID_MARVELL_8797 (0x9129)
262 /* Device ID for SD8897 */
263 #define SDIO_DEVICE_ID_MARVELL_8897 (0x912d)
265 /* WLAN IDs */
266 static const struct sdio_device_id mwifiex_ids[] = {
267 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8786),
268 .driver_data = (unsigned long) &mwifiex_sdio_sd8786},
269 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787),
270 .driver_data = (unsigned long) &mwifiex_sdio_sd8787},
271 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8797),
272 .driver_data = (unsigned long) &mwifiex_sdio_sd8797},
273 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8897),
274 .driver_data = (unsigned long) &mwifiex_sdio_sd8897},
278 MODULE_DEVICE_TABLE(sdio, mwifiex_ids);
280 static const struct dev_pm_ops mwifiex_sdio_pm_ops = {
281 .suspend = mwifiex_sdio_suspend,
282 .resume = mwifiex_sdio_resume,
285 static struct sdio_driver mwifiex_sdio = {
286 .name = "mwifiex_sdio",
287 .id_table = mwifiex_ids,
288 .probe = mwifiex_sdio_probe,
289 .remove = mwifiex_sdio_remove,
290 .drv = {
291 .owner = THIS_MODULE,
292 .pm = &mwifiex_sdio_pm_ops,
296 /* Write data into SDIO card register. Caller claims SDIO device. */
297 static int
298 mwifiex_write_reg_locked(struct sdio_func *func, u32 reg, u8 data)
300 int ret = -1;
301 sdio_writeb(func, data, reg, &ret);
302 return ret;
306 * This function writes data into SDIO card register.
308 static int
309 mwifiex_write_reg(struct mwifiex_adapter *adapter, u32 reg, u8 data)
311 struct sdio_mmc_card *card = adapter->card;
312 int ret;
314 sdio_claim_host(card->func);
315 ret = mwifiex_write_reg_locked(card->func, reg, data);
316 sdio_release_host(card->func);
318 return ret;
322 * This function reads data from SDIO card register.
324 static int
325 mwifiex_read_reg(struct mwifiex_adapter *adapter, u32 reg, u8 *data)
327 struct sdio_mmc_card *card = adapter->card;
328 int ret = -1;
329 u8 val;
331 sdio_claim_host(card->func);
332 val = sdio_readb(card->func, reg, &ret);
333 sdio_release_host(card->func);
335 *data = val;
337 return ret;
341 * This function writes multiple data into SDIO card memory.
343 * This does not work in suspended mode.
345 static int
346 mwifiex_write_data_sync(struct mwifiex_adapter *adapter,
347 u8 *buffer, u32 pkt_len, u32 port)
349 struct sdio_mmc_card *card = adapter->card;
350 int ret;
351 u8 blk_mode =
352 (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE : BLOCK_MODE;
353 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
354 u32 blk_cnt =
355 (blk_mode ==
356 BLOCK_MODE) ? (pkt_len /
357 MWIFIEX_SDIO_BLOCK_SIZE) : pkt_len;
358 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
360 if (adapter->is_suspended) {
361 dev_err(adapter->dev,
362 "%s: not allowed while suspended\n", __func__);
363 return -1;
366 sdio_claim_host(card->func);
368 ret = sdio_writesb(card->func, ioport, buffer, blk_cnt * blk_size);
370 sdio_release_host(card->func);
372 return ret;
376 * This function reads multiple data from SDIO card memory.
378 static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *buffer,
379 u32 len, u32 port, u8 claim)
381 struct sdio_mmc_card *card = adapter->card;
382 int ret;
383 u8 blk_mode = (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE
384 : BLOCK_MODE;
385 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
386 u32 blk_cnt = (blk_mode == BLOCK_MODE) ? (len / MWIFIEX_SDIO_BLOCK_SIZE)
387 : len;
388 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
390 if (claim)
391 sdio_claim_host(card->func);
393 ret = sdio_readsb(card->func, buffer, ioport, blk_cnt * blk_size);
395 if (claim)
396 sdio_release_host(card->func);
398 return ret;
402 * This function wakes up the card.
404 * A host power up command is written to the card configuration
405 * register to wake up the card.
407 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
409 dev_dbg(adapter->dev, "event: wakeup device...\n");
411 return mwifiex_write_reg(adapter, CONFIGURATION_REG, HOST_POWER_UP);
415 * This function is called after the card has woken up.
417 * The card configuration register is reset.
419 static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
421 dev_dbg(adapter->dev, "cmd: wakeup device completed\n");
423 return mwifiex_write_reg(adapter, CONFIGURATION_REG, 0);
427 * This function is used to initialize IO ports for the
428 * chipsets supporting SDIO new mode eg SD8897.
430 static int mwifiex_init_sdio_new_mode(struct mwifiex_adapter *adapter)
432 u8 reg;
434 adapter->ioport = MEM_PORT;
436 /* enable sdio new mode */
437 if (mwifiex_read_reg(adapter, CARD_CONFIG_2_1_REG, &reg))
438 return -1;
439 if (mwifiex_write_reg(adapter, CARD_CONFIG_2_1_REG,
440 reg | CMD53_NEW_MODE))
441 return -1;
443 /* Configure cmd port and enable reading rx length from the register */
444 if (mwifiex_read_reg(adapter, CMD_CONFIG_0, &reg))
445 return -1;
446 if (mwifiex_write_reg(adapter, CMD_CONFIG_0, reg | CMD_PORT_RD_LEN_EN))
447 return -1;
449 /* Enable Dnld/Upld ready auto reset for cmd port after cmd53 is
450 * completed
452 if (mwifiex_read_reg(adapter, CMD_CONFIG_1, &reg))
453 return -1;
454 if (mwifiex_write_reg(adapter, CMD_CONFIG_1, reg | CMD_PORT_AUTO_EN))
455 return -1;
457 return 0;
460 /* This function initializes the IO ports.
462 * The following operations are performed -
463 * - Read the IO ports (0, 1 and 2)
464 * - Set host interrupt Reset-To-Read to clear
465 * - Set auto re-enable interrupt
467 static int mwifiex_init_sdio_ioport(struct mwifiex_adapter *adapter)
469 u8 reg;
470 struct sdio_mmc_card *card = adapter->card;
472 adapter->ioport = 0;
474 if (card->supports_sdio_new_mode) {
475 if (mwifiex_init_sdio_new_mode(adapter))
476 return -1;
477 goto cont;
480 /* Read the IO port */
481 if (!mwifiex_read_reg(adapter, IO_PORT_0_REG, &reg))
482 adapter->ioport |= (reg & 0xff);
483 else
484 return -1;
486 if (!mwifiex_read_reg(adapter, IO_PORT_1_REG, &reg))
487 adapter->ioport |= ((reg & 0xff) << 8);
488 else
489 return -1;
491 if (!mwifiex_read_reg(adapter, IO_PORT_2_REG, &reg))
492 adapter->ioport |= ((reg & 0xff) << 16);
493 else
494 return -1;
495 cont:
496 pr_debug("info: SDIO FUNC1 IO port: %#x\n", adapter->ioport);
498 /* Set Host interrupt reset to read to clear */
499 if (!mwifiex_read_reg(adapter, HOST_INT_RSR_REG, &reg))
500 mwifiex_write_reg(adapter, HOST_INT_RSR_REG,
501 reg | card->reg->sdio_int_mask);
502 else
503 return -1;
505 /* Dnld/Upld ready set to auto reset */
506 if (!mwifiex_read_reg(adapter, card->reg->card_misc_cfg_reg, &reg))
507 mwifiex_write_reg(adapter, card->reg->card_misc_cfg_reg,
508 reg | AUTO_RE_ENABLE_INT);
509 else
510 return -1;
512 return 0;
516 * This function sends data to the card.
518 static int mwifiex_write_data_to_card(struct mwifiex_adapter *adapter,
519 u8 *payload, u32 pkt_len, u32 port)
521 u32 i = 0;
522 int ret;
524 do {
525 ret = mwifiex_write_data_sync(adapter, payload, pkt_len, port);
526 if (ret) {
527 i++;
528 dev_err(adapter->dev, "host_to_card, write iomem"
529 " (%d) failed: %d\n", i, ret);
530 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
531 dev_err(adapter->dev, "write CFG reg failed\n");
533 ret = -1;
534 if (i > MAX_WRITE_IOMEM_RETRY)
535 return ret;
537 } while (ret == -1);
539 return ret;
543 * This function gets the read port.
545 * If control port bit is set in MP read bitmap, the control port
546 * is returned, otherwise the current read port is returned and
547 * the value is increased (provided it does not reach the maximum
548 * limit, in which case it is reset to 1)
550 static int mwifiex_get_rd_port(struct mwifiex_adapter *adapter, u8 *port)
552 struct sdio_mmc_card *card = adapter->card;
553 const struct mwifiex_sdio_card_reg *reg = card->reg;
554 u32 rd_bitmap = card->mp_rd_bitmap;
556 dev_dbg(adapter->dev, "data: mp_rd_bitmap=0x%08x\n", rd_bitmap);
558 if (card->supports_sdio_new_mode) {
559 if (!(rd_bitmap & reg->data_port_mask))
560 return -1;
561 } else {
562 if (!(rd_bitmap & (CTRL_PORT_MASK | reg->data_port_mask)))
563 return -1;
566 if ((card->has_control_mask) &&
567 (card->mp_rd_bitmap & CTRL_PORT_MASK)) {
568 card->mp_rd_bitmap &= (u32) (~CTRL_PORT_MASK);
569 *port = CTRL_PORT;
570 dev_dbg(adapter->dev, "data: port=%d mp_rd_bitmap=0x%08x\n",
571 *port, card->mp_rd_bitmap);
572 return 0;
575 if (!(card->mp_rd_bitmap & (1 << card->curr_rd_port)))
576 return -1;
578 /* We are now handling the SDIO data ports */
579 card->mp_rd_bitmap &= (u32)(~(1 << card->curr_rd_port));
580 *port = card->curr_rd_port;
582 if (++card->curr_rd_port == card->max_ports)
583 card->curr_rd_port = reg->start_rd_port;
585 dev_dbg(adapter->dev,
586 "data: port=%d mp_rd_bitmap=0x%08x -> 0x%08x\n",
587 *port, rd_bitmap, card->mp_rd_bitmap);
589 return 0;
593 * This function gets the write port for data.
595 * The current write port is returned if available and the value is
596 * increased (provided it does not reach the maximum limit, in which
597 * case it is reset to 1)
599 static int mwifiex_get_wr_port_data(struct mwifiex_adapter *adapter, u32 *port)
601 struct sdio_mmc_card *card = adapter->card;
602 const struct mwifiex_sdio_card_reg *reg = card->reg;
603 u32 wr_bitmap = card->mp_wr_bitmap;
605 dev_dbg(adapter->dev, "data: mp_wr_bitmap=0x%08x\n", wr_bitmap);
607 if (card->supports_sdio_new_mode &&
608 !(wr_bitmap & reg->data_port_mask)) {
609 adapter->data_sent = true;
610 return -EBUSY;
611 } else if (!card->supports_sdio_new_mode &&
612 !(wr_bitmap & card->mp_data_port_mask)) {
613 return -1;
616 if (card->mp_wr_bitmap & (1 << card->curr_wr_port)) {
617 card->mp_wr_bitmap &= (u32) (~(1 << card->curr_wr_port));
618 *port = card->curr_wr_port;
619 if (((card->supports_sdio_new_mode) &&
620 (++card->curr_wr_port == card->max_ports)) ||
621 ((!card->supports_sdio_new_mode) &&
622 (++card->curr_wr_port == card->mp_end_port)))
623 card->curr_wr_port = reg->start_wr_port;
624 } else {
625 adapter->data_sent = true;
626 return -EBUSY;
629 if ((card->has_control_mask) && (*port == CTRL_PORT)) {
630 dev_err(adapter->dev,
631 "invalid data port=%d cur port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
632 *port, card->curr_wr_port, wr_bitmap,
633 card->mp_wr_bitmap);
634 return -1;
637 dev_dbg(adapter->dev, "data: port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
638 *port, wr_bitmap, card->mp_wr_bitmap);
640 return 0;
644 * This function polls the card status.
646 static int
647 mwifiex_sdio_poll_card_status(struct mwifiex_adapter *adapter, u8 bits)
649 struct sdio_mmc_card *card = adapter->card;
650 u32 tries;
651 u8 cs;
653 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
654 if (mwifiex_read_reg(adapter, card->reg->poll_reg, &cs))
655 break;
656 else if ((cs & bits) == bits)
657 return 0;
659 usleep_range(10, 20);
662 dev_err(adapter->dev, "poll card status failed, tries = %d\n", tries);
664 return -1;
668 * This function reads the firmware status.
670 static int
671 mwifiex_sdio_read_fw_status(struct mwifiex_adapter *adapter, u16 *dat)
673 struct sdio_mmc_card *card = adapter->card;
674 const struct mwifiex_sdio_card_reg *reg = card->reg;
675 u8 fws0, fws1;
677 if (mwifiex_read_reg(adapter, reg->status_reg_0, &fws0))
678 return -1;
680 if (mwifiex_read_reg(adapter, reg->status_reg_1, &fws1))
681 return -1;
683 *dat = (u16) ((fws1 << 8) | fws0);
685 return 0;
689 * This function disables the host interrupt.
691 * The host interrupt mask is read, the disable bit is reset and
692 * written back to the card host interrupt mask register.
694 static void mwifiex_sdio_disable_host_int(struct mwifiex_adapter *adapter)
696 struct sdio_mmc_card *card = adapter->card;
697 struct sdio_func *func = card->func;
699 sdio_claim_host(func);
700 mwifiex_write_reg_locked(func, HOST_INT_MASK_REG, 0);
701 sdio_release_irq(func);
702 sdio_release_host(func);
706 * This function reads the interrupt status from card.
708 static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
710 struct sdio_mmc_card *card = adapter->card;
711 u8 sdio_ireg;
712 unsigned long flags;
714 if (mwifiex_read_data_sync(adapter, card->mp_regs,
715 card->reg->max_mp_regs,
716 REG_PORT | MWIFIEX_SDIO_BYTE_MODE_MASK, 0)) {
717 dev_err(adapter->dev, "read mp_regs failed\n");
718 return;
721 sdio_ireg = card->mp_regs[HOST_INTSTATUS_REG];
722 if (sdio_ireg) {
724 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
725 * For SDIO new mode CMD port interrupts
726 * DN_LD_CMD_PORT_HOST_INT_STATUS and/or
727 * UP_LD_CMD_PORT_HOST_INT_STATUS
728 * Clear the interrupt status register
730 dev_dbg(adapter->dev, "int: sdio_ireg = %#x\n", sdio_ireg);
731 spin_lock_irqsave(&adapter->int_lock, flags);
732 adapter->int_status |= sdio_ireg;
733 spin_unlock_irqrestore(&adapter->int_lock, flags);
738 * SDIO interrupt handler.
740 * This function reads the interrupt status from firmware and handles
741 * the interrupt in current thread (ksdioirqd) right away.
743 static void
744 mwifiex_sdio_interrupt(struct sdio_func *func)
746 struct mwifiex_adapter *adapter;
747 struct sdio_mmc_card *card;
749 card = sdio_get_drvdata(func);
750 if (!card || !card->adapter) {
751 pr_debug("int: func=%p card=%p adapter=%p\n",
752 func, card, card ? card->adapter : NULL);
753 return;
755 adapter = card->adapter;
757 if (!adapter->pps_uapsd_mode && adapter->ps_state == PS_STATE_SLEEP)
758 adapter->ps_state = PS_STATE_AWAKE;
760 mwifiex_interrupt_status(adapter);
761 mwifiex_main_process(adapter);
765 * This function enables the host interrupt.
767 * The host interrupt enable mask is written to the card
768 * host interrupt mask register.
770 static int mwifiex_sdio_enable_host_int(struct mwifiex_adapter *adapter)
772 struct sdio_mmc_card *card = adapter->card;
773 struct sdio_func *func = card->func;
774 int ret;
776 sdio_claim_host(func);
778 /* Request the SDIO IRQ */
779 ret = sdio_claim_irq(func, mwifiex_sdio_interrupt);
780 if (ret) {
781 dev_err(adapter->dev, "claim irq failed: ret=%d\n", ret);
782 goto out;
785 /* Simply write the mask to the register */
786 ret = mwifiex_write_reg_locked(func, HOST_INT_MASK_REG,
787 card->reg->host_int_enable);
788 if (ret) {
789 dev_err(adapter->dev, "enable host interrupt failed\n");
790 sdio_release_irq(func);
793 out:
794 sdio_release_host(func);
795 return ret;
799 * This function sends a data buffer to the card.
801 static int mwifiex_sdio_card_to_host(struct mwifiex_adapter *adapter,
802 u32 *type, u8 *buffer,
803 u32 npayload, u32 ioport)
805 int ret;
806 u32 nb;
808 if (!buffer) {
809 dev_err(adapter->dev, "%s: buffer is NULL\n", __func__);
810 return -1;
813 ret = mwifiex_read_data_sync(adapter, buffer, npayload, ioport, 1);
815 if (ret) {
816 dev_err(adapter->dev, "%s: read iomem failed: %d\n", __func__,
817 ret);
818 return -1;
821 nb = le16_to_cpu(*(__le16 *) (buffer));
822 if (nb > npayload) {
823 dev_err(adapter->dev, "%s: invalid packet, nb=%d npayload=%d\n",
824 __func__, nb, npayload);
825 return -1;
828 *type = le16_to_cpu(*(__le16 *) (buffer + 2));
830 return ret;
834 * This function downloads the firmware to the card.
836 * Firmware is downloaded to the card in blocks. Every block download
837 * is tested for CRC errors, and retried a number of times before
838 * returning failure.
840 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
841 struct mwifiex_fw_image *fw)
843 struct sdio_mmc_card *card = adapter->card;
844 const struct mwifiex_sdio_card_reg *reg = card->reg;
845 int ret;
846 u8 *firmware = fw->fw_buf;
847 u32 firmware_len = fw->fw_len;
848 u32 offset = 0;
849 u8 base0, base1;
850 u8 *fwbuf;
851 u16 len = 0;
852 u32 txlen, tx_blocks = 0, tries;
853 u32 i = 0;
855 if (!firmware_len) {
856 dev_err(adapter->dev,
857 "firmware image not found! Terminating download\n");
858 return -1;
861 dev_dbg(adapter->dev, "info: downloading FW image (%d bytes)\n",
862 firmware_len);
864 /* Assume that the allocated buffer is 8-byte aligned */
865 fwbuf = kzalloc(MWIFIEX_UPLD_SIZE, GFP_KERNEL);
866 if (!fwbuf)
867 return -ENOMEM;
869 /* Perform firmware data transfer */
870 do {
871 /* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY
872 bits */
873 ret = mwifiex_sdio_poll_card_status(adapter, CARD_IO_READY |
874 DN_LD_CARD_RDY);
875 if (ret) {
876 dev_err(adapter->dev, "FW download with helper:"
877 " poll status timeout @ %d\n", offset);
878 goto done;
881 /* More data? */
882 if (offset >= firmware_len)
883 break;
885 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
886 ret = mwifiex_read_reg(adapter, reg->base_0_reg,
887 &base0);
888 if (ret) {
889 dev_err(adapter->dev,
890 "dev BASE0 register read failed: "
891 "base0=%#04X(%d). Terminating dnld\n",
892 base0, base0);
893 goto done;
895 ret = mwifiex_read_reg(adapter, reg->base_1_reg,
896 &base1);
897 if (ret) {
898 dev_err(adapter->dev,
899 "dev BASE1 register read failed: "
900 "base1=%#04X(%d). Terminating dnld\n",
901 base1, base1);
902 goto done;
904 len = (u16) (((base1 & 0xff) << 8) | (base0 & 0xff));
906 if (len)
907 break;
909 usleep_range(10, 20);
912 if (!len) {
913 break;
914 } else if (len > MWIFIEX_UPLD_SIZE) {
915 dev_err(adapter->dev,
916 "FW dnld failed @ %d, invalid length %d\n",
917 offset, len);
918 ret = -1;
919 goto done;
922 txlen = len;
924 if (len & BIT(0)) {
925 i++;
926 if (i > MAX_WRITE_IOMEM_RETRY) {
927 dev_err(adapter->dev,
928 "FW dnld failed @ %d, over max retry\n",
929 offset);
930 ret = -1;
931 goto done;
933 dev_err(adapter->dev, "CRC indicated by the helper:"
934 " len = 0x%04X, txlen = %d\n", len, txlen);
935 len &= ~BIT(0);
936 /* Setting this to 0 to resend from same offset */
937 txlen = 0;
938 } else {
939 i = 0;
941 /* Set blocksize to transfer - checking for last
942 block */
943 if (firmware_len - offset < txlen)
944 txlen = firmware_len - offset;
946 tx_blocks = (txlen + MWIFIEX_SDIO_BLOCK_SIZE - 1)
947 / MWIFIEX_SDIO_BLOCK_SIZE;
949 /* Copy payload to buffer */
950 memmove(fwbuf, &firmware[offset], txlen);
953 ret = mwifiex_write_data_sync(adapter, fwbuf, tx_blocks *
954 MWIFIEX_SDIO_BLOCK_SIZE,
955 adapter->ioport);
956 if (ret) {
957 dev_err(adapter->dev,
958 "FW download, write iomem (%d) failed @ %d\n",
959 i, offset);
960 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
961 dev_err(adapter->dev, "write CFG reg failed\n");
963 ret = -1;
964 goto done;
967 offset += txlen;
968 } while (true);
970 dev_dbg(adapter->dev, "info: FW download over, size %d bytes\n",
971 offset);
973 ret = 0;
974 done:
975 kfree(fwbuf);
976 return ret;
980 * This function checks the firmware status in card.
982 * The winner interface is also determined by this function.
984 static int mwifiex_check_fw_status(struct mwifiex_adapter *adapter,
985 u32 poll_num)
987 struct sdio_mmc_card *card = adapter->card;
988 int ret = 0;
989 u16 firmware_stat;
990 u32 tries;
991 u8 winner_status;
993 /* Wait for firmware initialization event */
994 for (tries = 0; tries < poll_num; tries++) {
995 ret = mwifiex_sdio_read_fw_status(adapter, &firmware_stat);
996 if (ret)
997 continue;
998 if (firmware_stat == FIRMWARE_READY_SDIO) {
999 ret = 0;
1000 break;
1001 } else {
1002 msleep(100);
1003 ret = -1;
1007 if (ret) {
1008 if (mwifiex_read_reg
1009 (adapter, card->reg->status_reg_0, &winner_status))
1010 winner_status = 0;
1012 if (winner_status)
1013 adapter->winner = 0;
1014 else
1015 adapter->winner = 1;
1017 return ret;
1021 * This function decodes a received packet.
1023 * Based on the type, the packet is treated as either a data, or
1024 * a command response, or an event, and the correct handler
1025 * function is invoked.
1027 static int mwifiex_decode_rx_packet(struct mwifiex_adapter *adapter,
1028 struct sk_buff *skb, u32 upld_typ)
1030 u8 *cmd_buf;
1031 __le16 *curr_ptr = (__le16 *)skb->data;
1032 u16 pkt_len = le16_to_cpu(*curr_ptr);
1034 skb_trim(skb, pkt_len);
1035 skb_pull(skb, INTF_HEADER_LEN);
1037 switch (upld_typ) {
1038 case MWIFIEX_TYPE_DATA:
1039 dev_dbg(adapter->dev, "info: --- Rx: Data packet ---\n");
1040 mwifiex_handle_rx_packet(adapter, skb);
1041 break;
1043 case MWIFIEX_TYPE_CMD:
1044 dev_dbg(adapter->dev, "info: --- Rx: Cmd Response ---\n");
1045 /* take care of curr_cmd = NULL case */
1046 if (!adapter->curr_cmd) {
1047 cmd_buf = adapter->upld_buf;
1049 if (adapter->ps_state == PS_STATE_SLEEP_CFM)
1050 mwifiex_process_sleep_confirm_resp(adapter,
1051 skb->data,
1052 skb->len);
1054 memcpy(cmd_buf, skb->data,
1055 min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER,
1056 skb->len));
1058 dev_kfree_skb_any(skb);
1059 } else {
1060 adapter->cmd_resp_received = true;
1061 adapter->curr_cmd->resp_skb = skb;
1063 break;
1065 case MWIFIEX_TYPE_EVENT:
1066 dev_dbg(adapter->dev, "info: --- Rx: Event ---\n");
1067 adapter->event_cause = le32_to_cpu(*(__le32 *) skb->data);
1069 if ((skb->len > 0) && (skb->len < MAX_EVENT_SIZE))
1070 memcpy(adapter->event_body,
1071 skb->data + MWIFIEX_EVENT_HEADER_LEN,
1072 skb->len);
1074 /* event cause has been saved to adapter->event_cause */
1075 adapter->event_received = true;
1076 adapter->event_skb = skb;
1078 break;
1080 default:
1081 dev_err(adapter->dev, "unknown upload type %#x\n", upld_typ);
1082 dev_kfree_skb_any(skb);
1083 break;
1086 return 0;
1090 * This function transfers received packets from card to driver, performing
1091 * aggregation if required.
1093 * For data received on control port, or if aggregation is disabled, the
1094 * received buffers are uploaded as separate packets. However, if aggregation
1095 * is enabled and required, the buffers are copied onto an aggregation buffer,
1096 * provided there is space left, processed and finally uploaded.
1098 static int mwifiex_sdio_card_to_host_mp_aggr(struct mwifiex_adapter *adapter,
1099 struct sk_buff *skb, u8 port)
1101 struct sdio_mmc_card *card = adapter->card;
1102 s32 f_do_rx_aggr = 0;
1103 s32 f_do_rx_cur = 0;
1104 s32 f_aggr_cur = 0;
1105 struct sk_buff *skb_deaggr;
1106 u32 pind;
1107 u32 pkt_len, pkt_type, mport;
1108 u8 *curr_ptr;
1109 u32 rx_len = skb->len;
1111 if ((card->has_control_mask) && (port == CTRL_PORT)) {
1112 /* Read the command Resp without aggr */
1113 dev_dbg(adapter->dev, "info: %s: no aggregation for cmd "
1114 "response\n", __func__);
1116 f_do_rx_cur = 1;
1117 goto rx_curr_single;
1120 if (!card->mpa_rx.enabled) {
1121 dev_dbg(adapter->dev, "info: %s: rx aggregation disabled\n",
1122 __func__);
1124 f_do_rx_cur = 1;
1125 goto rx_curr_single;
1128 if ((!card->has_control_mask && (card->mp_rd_bitmap &
1129 card->reg->data_port_mask)) ||
1130 (card->has_control_mask && (card->mp_rd_bitmap &
1131 (~((u32) CTRL_PORT_MASK))))) {
1132 /* Some more data RX pending */
1133 dev_dbg(adapter->dev, "info: %s: not last packet\n", __func__);
1135 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1136 if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len)) {
1137 f_aggr_cur = 1;
1138 } else {
1139 /* No room in Aggr buf, do rx aggr now */
1140 f_do_rx_aggr = 1;
1141 f_do_rx_cur = 1;
1143 } else {
1144 /* Rx aggr not in progress */
1145 f_aggr_cur = 1;
1148 } else {
1149 /* No more data RX pending */
1150 dev_dbg(adapter->dev, "info: %s: last packet\n", __func__);
1152 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1153 f_do_rx_aggr = 1;
1154 if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len))
1155 f_aggr_cur = 1;
1156 else
1157 /* No room in Aggr buf, do rx aggr now */
1158 f_do_rx_cur = 1;
1159 } else {
1160 f_do_rx_cur = 1;
1164 if (f_aggr_cur) {
1165 dev_dbg(adapter->dev, "info: current packet aggregation\n");
1166 /* Curr pkt can be aggregated */
1167 mp_rx_aggr_setup(card, skb, port);
1169 if (MP_RX_AGGR_PKT_LIMIT_REACHED(card) ||
1170 mp_rx_aggr_port_limit_reached(card)) {
1171 dev_dbg(adapter->dev, "info: %s: aggregated packet "
1172 "limit reached\n", __func__);
1173 /* No more pkts allowed in Aggr buf, rx it */
1174 f_do_rx_aggr = 1;
1178 if (f_do_rx_aggr) {
1179 /* do aggr RX now */
1180 dev_dbg(adapter->dev, "info: do_rx_aggr: num of packets: %d\n",
1181 card->mpa_rx.pkt_cnt);
1183 if (card->supports_sdio_new_mode) {
1184 int i;
1185 u32 port_count;
1187 for (i = 0, port_count = 0; i < card->max_ports; i++)
1188 if (card->mpa_rx.ports & BIT(i))
1189 port_count++;
1191 /* Reading data from "start_port + 0" to "start_port +
1192 * port_count -1", so decrease the count by 1
1194 port_count--;
1195 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1196 (port_count << 8)) + card->mpa_rx.start_port;
1197 } else {
1198 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1199 (card->mpa_rx.ports << 4)) +
1200 card->mpa_rx.start_port;
1203 if (mwifiex_read_data_sync(adapter, card->mpa_rx.buf,
1204 card->mpa_rx.buf_len, mport, 1))
1205 goto error;
1207 curr_ptr = card->mpa_rx.buf;
1209 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1211 /* get curr PKT len & type */
1212 pkt_len = le16_to_cpu(*(__le16 *) &curr_ptr[0]);
1213 pkt_type = le16_to_cpu(*(__le16 *) &curr_ptr[2]);
1215 /* copy pkt to deaggr buf */
1216 skb_deaggr = card->mpa_rx.skb_arr[pind];
1218 if ((pkt_type == MWIFIEX_TYPE_DATA) && (pkt_len <=
1219 card->mpa_rx.len_arr[pind])) {
1221 memcpy(skb_deaggr->data, curr_ptr, pkt_len);
1223 skb_trim(skb_deaggr, pkt_len);
1225 /* Process de-aggr packet */
1226 mwifiex_decode_rx_packet(adapter, skb_deaggr,
1227 pkt_type);
1228 } else {
1229 dev_err(adapter->dev, "wrong aggr pkt:"
1230 " type=%d len=%d max_len=%d\n",
1231 pkt_type, pkt_len,
1232 card->mpa_rx.len_arr[pind]);
1233 dev_kfree_skb_any(skb_deaggr);
1235 curr_ptr += card->mpa_rx.len_arr[pind];
1237 MP_RX_AGGR_BUF_RESET(card);
1240 rx_curr_single:
1241 if (f_do_rx_cur) {
1242 dev_dbg(adapter->dev, "info: RX: port: %d, rx_len: %d\n",
1243 port, rx_len);
1245 if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1246 skb->data, skb->len,
1247 adapter->ioport + port))
1248 goto error;
1250 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1253 return 0;
1255 error:
1256 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1257 /* Multiport-aggregation transfer failed - cleanup */
1258 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1259 /* copy pkt to deaggr buf */
1260 skb_deaggr = card->mpa_rx.skb_arr[pind];
1261 dev_kfree_skb_any(skb_deaggr);
1263 MP_RX_AGGR_BUF_RESET(card);
1266 if (f_do_rx_cur)
1267 /* Single transfer pending. Free curr buff also */
1268 dev_kfree_skb_any(skb);
1270 return -1;
1274 * This function checks the current interrupt status.
1276 * The following interrupts are checked and handled by this function -
1277 * - Data sent
1278 * - Command sent
1279 * - Packets received
1281 * Since the firmware does not generate download ready interrupt if the
1282 * port updated is command port only, command sent interrupt checking
1283 * should be done manually, and for every SDIO interrupt.
1285 * In case of Rx packets received, the packets are uploaded from card to
1286 * host and processed accordingly.
1288 static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
1290 struct sdio_mmc_card *card = adapter->card;
1291 const struct mwifiex_sdio_card_reg *reg = card->reg;
1292 int ret = 0;
1293 u8 sdio_ireg;
1294 struct sk_buff *skb;
1295 u8 port = CTRL_PORT;
1296 u32 len_reg_l, len_reg_u;
1297 u32 rx_blocks;
1298 u16 rx_len;
1299 unsigned long flags;
1300 u32 bitmap;
1301 u8 cr;
1303 spin_lock_irqsave(&adapter->int_lock, flags);
1304 sdio_ireg = adapter->int_status;
1305 adapter->int_status = 0;
1306 spin_unlock_irqrestore(&adapter->int_lock, flags);
1308 if (!sdio_ireg)
1309 return ret;
1311 /* Following interrupt is only for SDIO new mode */
1312 if (sdio_ireg & DN_LD_CMD_PORT_HOST_INT_STATUS && adapter->cmd_sent)
1313 adapter->cmd_sent = false;
1315 /* Following interrupt is only for SDIO new mode */
1316 if (sdio_ireg & UP_LD_CMD_PORT_HOST_INT_STATUS) {
1317 u32 pkt_type;
1319 /* read the len of control packet */
1320 rx_len = card->mp_regs[CMD_RD_LEN_1] << 8;
1321 rx_len |= (u16) card->mp_regs[CMD_RD_LEN_0];
1322 rx_blocks = DIV_ROUND_UP(rx_len, MWIFIEX_SDIO_BLOCK_SIZE);
1323 if (rx_len <= INTF_HEADER_LEN ||
1324 (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1325 MWIFIEX_RX_DATA_BUF_SIZE)
1326 return -1;
1327 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1329 skb = dev_alloc_skb(rx_len);
1330 if (!skb)
1331 return -1;
1333 skb_put(skb, rx_len);
1335 if (mwifiex_sdio_card_to_host(adapter, &pkt_type, skb->data,
1336 skb->len, adapter->ioport |
1337 CMD_PORT_SLCT)) {
1338 dev_err(adapter->dev,
1339 "%s: failed to card_to_host", __func__);
1340 dev_kfree_skb_any(skb);
1341 goto term_cmd;
1344 if ((pkt_type != MWIFIEX_TYPE_CMD) &&
1345 (pkt_type != MWIFIEX_TYPE_EVENT))
1346 dev_err(adapter->dev,
1347 "%s:Received wrong packet on cmd port",
1348 __func__);
1350 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1353 if (sdio_ireg & DN_LD_HOST_INT_STATUS) {
1354 bitmap = (u32) card->mp_regs[reg->wr_bitmap_l];
1355 bitmap |= ((u32) card->mp_regs[reg->wr_bitmap_u]) << 8;
1356 if (card->supports_sdio_new_mode) {
1357 bitmap |=
1358 ((u32) card->mp_regs[reg->wr_bitmap_1l]) << 16;
1359 bitmap |=
1360 ((u32) card->mp_regs[reg->wr_bitmap_1u]) << 24;
1362 card->mp_wr_bitmap = bitmap;
1364 dev_dbg(adapter->dev, "int: DNLD: wr_bitmap=0x%x\n",
1365 card->mp_wr_bitmap);
1366 if (adapter->data_sent &&
1367 (card->mp_wr_bitmap & card->mp_data_port_mask)) {
1368 dev_dbg(adapter->dev,
1369 "info: <--- Tx DONE Interrupt --->\n");
1370 adapter->data_sent = false;
1374 /* As firmware will not generate download ready interrupt if the port
1375 updated is command port only, cmd_sent should be done for any SDIO
1376 interrupt. */
1377 if (card->has_control_mask && adapter->cmd_sent) {
1378 /* Check if firmware has attach buffer at command port and
1379 update just that in wr_bit_map. */
1380 card->mp_wr_bitmap |=
1381 (u32) card->mp_regs[reg->wr_bitmap_l] & CTRL_PORT_MASK;
1382 if (card->mp_wr_bitmap & CTRL_PORT_MASK)
1383 adapter->cmd_sent = false;
1386 dev_dbg(adapter->dev, "info: cmd_sent=%d data_sent=%d\n",
1387 adapter->cmd_sent, adapter->data_sent);
1388 if (sdio_ireg & UP_LD_HOST_INT_STATUS) {
1389 bitmap = (u32) card->mp_regs[reg->rd_bitmap_l];
1390 bitmap |= ((u32) card->mp_regs[reg->rd_bitmap_u]) << 8;
1391 if (card->supports_sdio_new_mode) {
1392 bitmap |=
1393 ((u32) card->mp_regs[reg->rd_bitmap_1l]) << 16;
1394 bitmap |=
1395 ((u32) card->mp_regs[reg->rd_bitmap_1u]) << 24;
1397 card->mp_rd_bitmap = bitmap;
1398 dev_dbg(adapter->dev, "int: UPLD: rd_bitmap=0x%x\n",
1399 card->mp_rd_bitmap);
1401 while (true) {
1402 ret = mwifiex_get_rd_port(adapter, &port);
1403 if (ret) {
1404 dev_dbg(adapter->dev,
1405 "info: no more rd_port available\n");
1406 break;
1408 len_reg_l = reg->rd_len_p0_l + (port << 1);
1409 len_reg_u = reg->rd_len_p0_u + (port << 1);
1410 rx_len = ((u16) card->mp_regs[len_reg_u]) << 8;
1411 rx_len |= (u16) card->mp_regs[len_reg_l];
1412 dev_dbg(adapter->dev, "info: RX: port=%d rx_len=%u\n",
1413 port, rx_len);
1414 rx_blocks =
1415 (rx_len + MWIFIEX_SDIO_BLOCK_SIZE -
1416 1) / MWIFIEX_SDIO_BLOCK_SIZE;
1417 if (rx_len <= INTF_HEADER_LEN ||
1418 (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1419 MWIFIEX_RX_DATA_BUF_SIZE) {
1420 dev_err(adapter->dev, "invalid rx_len=%d\n",
1421 rx_len);
1422 return -1;
1424 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1426 skb = dev_alloc_skb(rx_len);
1428 if (!skb) {
1429 dev_err(adapter->dev, "%s: failed to alloc skb",
1430 __func__);
1431 return -1;
1434 skb_put(skb, rx_len);
1436 dev_dbg(adapter->dev, "info: rx_len = %d skb->len = %d\n",
1437 rx_len, skb->len);
1439 if (mwifiex_sdio_card_to_host_mp_aggr(adapter, skb,
1440 port)) {
1441 dev_err(adapter->dev, "card_to_host_mpa failed:"
1442 " int status=%#x\n", sdio_ireg);
1443 goto term_cmd;
1448 return 0;
1450 term_cmd:
1451 /* terminate cmd */
1452 if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
1453 dev_err(adapter->dev, "read CFG reg failed\n");
1454 else
1455 dev_dbg(adapter->dev, "info: CFG reg val = %d\n", cr);
1457 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, (cr | 0x04)))
1458 dev_err(adapter->dev, "write CFG reg failed\n");
1459 else
1460 dev_dbg(adapter->dev, "info: write success\n");
1462 if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
1463 dev_err(adapter->dev, "read CFG reg failed\n");
1464 else
1465 dev_dbg(adapter->dev, "info: CFG reg val =%x\n", cr);
1467 return -1;
1471 * This function aggregates transmission buffers in driver and downloads
1472 * the aggregated packet to card.
1474 * The individual packets are aggregated by copying into an aggregation
1475 * buffer and then downloaded to the card. Previous unsent packets in the
1476 * aggregation buffer are pre-copied first before new packets are added.
1477 * Aggregation is done till there is space left in the aggregation buffer,
1478 * or till new packets are available.
1480 * The function will only download the packet to the card when aggregation
1481 * stops, otherwise it will just aggregate the packet in aggregation buffer
1482 * and return.
1484 static int mwifiex_host_to_card_mp_aggr(struct mwifiex_adapter *adapter,
1485 u8 *payload, u32 pkt_len, u32 port,
1486 u32 next_pkt_len)
1488 struct sdio_mmc_card *card = adapter->card;
1489 int ret = 0;
1490 s32 f_send_aggr_buf = 0;
1491 s32 f_send_cur_buf = 0;
1492 s32 f_precopy_cur_buf = 0;
1493 s32 f_postcopy_cur_buf = 0;
1494 u32 mport;
1496 if (!card->mpa_tx.enabled ||
1497 (card->has_control_mask && (port == CTRL_PORT)) ||
1498 (card->supports_sdio_new_mode && (port == CMD_PORT_SLCT))) {
1499 dev_dbg(adapter->dev, "info: %s: tx aggregation disabled\n",
1500 __func__);
1502 f_send_cur_buf = 1;
1503 goto tx_curr_single;
1506 if (next_pkt_len) {
1507 /* More pkt in TX queue */
1508 dev_dbg(adapter->dev, "info: %s: more packets in queue.\n",
1509 __func__);
1511 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1512 if (!mp_tx_aggr_port_limit_reached(card) &&
1513 MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len)) {
1514 f_precopy_cur_buf = 1;
1516 if (!(card->mp_wr_bitmap &
1517 (1 << card->curr_wr_port)) ||
1518 !MP_TX_AGGR_BUF_HAS_ROOM(
1519 card, pkt_len + next_pkt_len))
1520 f_send_aggr_buf = 1;
1521 } else {
1522 /* No room in Aggr buf, send it */
1523 f_send_aggr_buf = 1;
1525 if (mp_tx_aggr_port_limit_reached(card) ||
1526 !(card->mp_wr_bitmap &
1527 (1 << card->curr_wr_port)))
1528 f_send_cur_buf = 1;
1529 else
1530 f_postcopy_cur_buf = 1;
1532 } else {
1533 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len) &&
1534 (card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1535 f_precopy_cur_buf = 1;
1536 else
1537 f_send_cur_buf = 1;
1539 } else {
1540 /* Last pkt in TX queue */
1541 dev_dbg(adapter->dev, "info: %s: Last packet in Tx Queue.\n",
1542 __func__);
1544 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1545 /* some packs in Aggr buf already */
1546 f_send_aggr_buf = 1;
1548 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len))
1549 f_precopy_cur_buf = 1;
1550 else
1551 /* No room in Aggr buf, send it */
1552 f_send_cur_buf = 1;
1553 } else {
1554 f_send_cur_buf = 1;
1558 if (f_precopy_cur_buf) {
1559 dev_dbg(adapter->dev, "data: %s: precopy current buffer\n",
1560 __func__);
1561 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1563 if (MP_TX_AGGR_PKT_LIMIT_REACHED(card) ||
1564 mp_tx_aggr_port_limit_reached(card))
1565 /* No more pkts allowed in Aggr buf, send it */
1566 f_send_aggr_buf = 1;
1569 if (f_send_aggr_buf) {
1570 dev_dbg(adapter->dev, "data: %s: send aggr buffer: %d %d\n",
1571 __func__,
1572 card->mpa_tx.start_port, card->mpa_tx.ports);
1573 if (card->supports_sdio_new_mode) {
1574 u32 port_count;
1575 int i;
1577 for (i = 0, port_count = 0; i < card->max_ports; i++)
1578 if (card->mpa_tx.ports & BIT(i))
1579 port_count++;
1581 /* Writing data from "start_port + 0" to "start_port +
1582 * port_count -1", so decrease the count by 1
1584 port_count--;
1585 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1586 (port_count << 8)) + card->mpa_tx.start_port;
1587 } else {
1588 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1589 (card->mpa_tx.ports << 4)) +
1590 card->mpa_tx.start_port;
1593 ret = mwifiex_write_data_to_card(adapter, card->mpa_tx.buf,
1594 card->mpa_tx.buf_len, mport);
1596 MP_TX_AGGR_BUF_RESET(card);
1599 tx_curr_single:
1600 if (f_send_cur_buf) {
1601 dev_dbg(adapter->dev, "data: %s: send current buffer %d\n",
1602 __func__, port);
1603 ret = mwifiex_write_data_to_card(adapter, payload, pkt_len,
1604 adapter->ioport + port);
1607 if (f_postcopy_cur_buf) {
1608 dev_dbg(adapter->dev, "data: %s: postcopy current buffer\n",
1609 __func__);
1610 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1613 return ret;
1617 * This function downloads data from driver to card.
1619 * Both commands and data packets are transferred to the card by this
1620 * function.
1622 * This function adds the SDIO specific header to the front of the buffer
1623 * before transferring. The header contains the length of the packet and
1624 * the type. The firmware handles the packets based upon this set type.
1626 static int mwifiex_sdio_host_to_card(struct mwifiex_adapter *adapter,
1627 u8 type, struct sk_buff *skb,
1628 struct mwifiex_tx_param *tx_param)
1630 struct sdio_mmc_card *card = adapter->card;
1631 int ret;
1632 u32 buf_block_len;
1633 u32 blk_size;
1634 u32 port = CTRL_PORT;
1635 u8 *payload = (u8 *)skb->data;
1636 u32 pkt_len = skb->len;
1638 /* Allocate buffer and copy payload */
1639 blk_size = MWIFIEX_SDIO_BLOCK_SIZE;
1640 buf_block_len = (pkt_len + blk_size - 1) / blk_size;
1641 *(__le16 *)&payload[0] = cpu_to_le16((u16)pkt_len);
1642 *(__le16 *)&payload[2] = cpu_to_le16(type);
1645 * This is SDIO specific header
1646 * u16 length,
1647 * u16 type (MWIFIEX_TYPE_DATA = 0, MWIFIEX_TYPE_CMD = 1,
1648 * MWIFIEX_TYPE_EVENT = 3)
1650 if (type == MWIFIEX_TYPE_DATA) {
1651 ret = mwifiex_get_wr_port_data(adapter, &port);
1652 if (ret) {
1653 dev_err(adapter->dev, "%s: no wr_port available\n",
1654 __func__);
1655 return ret;
1657 } else {
1658 adapter->cmd_sent = true;
1659 /* Type must be MWIFIEX_TYPE_CMD */
1661 if (pkt_len <= INTF_HEADER_LEN ||
1662 pkt_len > MWIFIEX_UPLD_SIZE)
1663 dev_err(adapter->dev, "%s: payload=%p, nb=%d\n",
1664 __func__, payload, pkt_len);
1666 if (card->supports_sdio_new_mode)
1667 port = CMD_PORT_SLCT;
1670 /* Transfer data to card */
1671 pkt_len = buf_block_len * blk_size;
1673 if (tx_param)
1674 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1675 port, tx_param->next_pkt_len
1677 else
1678 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1679 port, 0);
1681 if (ret) {
1682 if (type == MWIFIEX_TYPE_CMD)
1683 adapter->cmd_sent = false;
1684 if (type == MWIFIEX_TYPE_DATA)
1685 adapter->data_sent = false;
1686 } else {
1687 if (type == MWIFIEX_TYPE_DATA) {
1688 if (!(card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1689 adapter->data_sent = true;
1690 else
1691 adapter->data_sent = false;
1695 return ret;
1699 * This function allocates the MPA Tx and Rx buffers.
1701 static int mwifiex_alloc_sdio_mpa_buffers(struct mwifiex_adapter *adapter,
1702 u32 mpa_tx_buf_size, u32 mpa_rx_buf_size)
1704 struct sdio_mmc_card *card = adapter->card;
1705 int ret = 0;
1707 card->mpa_tx.buf = kzalloc(mpa_tx_buf_size, GFP_KERNEL);
1708 if (!card->mpa_tx.buf) {
1709 ret = -1;
1710 goto error;
1713 card->mpa_tx.buf_size = mpa_tx_buf_size;
1715 card->mpa_rx.buf = kzalloc(mpa_rx_buf_size, GFP_KERNEL);
1716 if (!card->mpa_rx.buf) {
1717 ret = -1;
1718 goto error;
1721 card->mpa_rx.buf_size = mpa_rx_buf_size;
1723 error:
1724 if (ret) {
1725 kfree(card->mpa_tx.buf);
1726 kfree(card->mpa_rx.buf);
1729 return ret;
1733 * This function unregisters the SDIO device.
1735 * The SDIO IRQ is released, the function is disabled and driver
1736 * data is set to null.
1738 static void
1739 mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
1741 struct sdio_mmc_card *card = adapter->card;
1743 if (adapter->card) {
1744 sdio_claim_host(card->func);
1745 sdio_disable_func(card->func);
1746 sdio_release_host(card->func);
1751 * This function registers the SDIO device.
1753 * SDIO IRQ is claimed, block size is set and driver data is initialized.
1755 static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
1757 int ret;
1758 struct sdio_mmc_card *card = adapter->card;
1759 struct sdio_func *func = card->func;
1761 /* save adapter pointer in card */
1762 card->adapter = adapter;
1764 sdio_claim_host(func);
1766 /* Set block size */
1767 ret = sdio_set_block_size(card->func, MWIFIEX_SDIO_BLOCK_SIZE);
1768 sdio_release_host(func);
1769 if (ret) {
1770 pr_err("cannot set SDIO block size\n");
1771 return ret;
1775 adapter->dev = &func->dev;
1777 strcpy(adapter->fw_name, card->firmware);
1779 return 0;
1783 * This function initializes the SDIO driver.
1785 * The following initializations steps are followed -
1786 * - Read the Host interrupt status register to acknowledge
1787 * the first interrupt got from bootloader
1788 * - Disable host interrupt mask register
1789 * - Get SDIO port
1790 * - Initialize SDIO variables in card
1791 * - Allocate MP registers
1792 * - Allocate MPA Tx and Rx buffers
1794 static int mwifiex_init_sdio(struct mwifiex_adapter *adapter)
1796 struct sdio_mmc_card *card = adapter->card;
1797 const struct mwifiex_sdio_card_reg *reg = card->reg;
1798 int ret;
1799 u8 sdio_ireg;
1801 sdio_set_drvdata(card->func, card);
1804 * Read the HOST_INT_STATUS_REG for ACK the first interrupt got
1805 * from the bootloader. If we don't do this we get a interrupt
1806 * as soon as we register the irq.
1808 mwifiex_read_reg(adapter, HOST_INTSTATUS_REG, &sdio_ireg);
1810 /* Get SDIO ioport */
1811 mwifiex_init_sdio_ioport(adapter);
1813 /* Initialize SDIO variables in card */
1814 card->mp_rd_bitmap = 0;
1815 card->mp_wr_bitmap = 0;
1816 card->curr_rd_port = reg->start_rd_port;
1817 card->curr_wr_port = reg->start_wr_port;
1819 card->mp_data_port_mask = reg->data_port_mask;
1821 card->mpa_tx.buf_len = 0;
1822 card->mpa_tx.pkt_cnt = 0;
1823 card->mpa_tx.start_port = 0;
1825 card->mpa_tx.enabled = 1;
1826 card->mpa_tx.pkt_aggr_limit = card->mp_agg_pkt_limit;
1828 card->mpa_rx.buf_len = 0;
1829 card->mpa_rx.pkt_cnt = 0;
1830 card->mpa_rx.start_port = 0;
1832 card->mpa_rx.enabled = 1;
1833 card->mpa_rx.pkt_aggr_limit = card->mp_agg_pkt_limit;
1835 /* Allocate buffers for SDIO MP-A */
1836 card->mp_regs = kzalloc(reg->max_mp_regs, GFP_KERNEL);
1837 if (!card->mp_regs)
1838 return -ENOMEM;
1840 /* Allocate skb pointer buffers */
1841 card->mpa_rx.skb_arr = kzalloc((sizeof(void *)) *
1842 card->mp_agg_pkt_limit, GFP_KERNEL);
1843 card->mpa_rx.len_arr = kzalloc(sizeof(*card->mpa_rx.len_arr) *
1844 card->mp_agg_pkt_limit, GFP_KERNEL);
1845 ret = mwifiex_alloc_sdio_mpa_buffers(adapter,
1846 SDIO_MP_TX_AGGR_DEF_BUF_SIZE,
1847 SDIO_MP_RX_AGGR_DEF_BUF_SIZE);
1848 if (ret) {
1849 dev_err(adapter->dev, "failed to alloc sdio mp-a buffers\n");
1850 kfree(card->mp_regs);
1851 return -1;
1854 return ret;
1858 * This function resets the MPA Tx and Rx buffers.
1860 static void mwifiex_cleanup_mpa_buf(struct mwifiex_adapter *adapter)
1862 struct sdio_mmc_card *card = adapter->card;
1864 MP_TX_AGGR_BUF_RESET(card);
1865 MP_RX_AGGR_BUF_RESET(card);
1869 * This function cleans up the allocated card buffers.
1871 * The following are freed by this function -
1872 * - MP registers
1873 * - MPA Tx buffer
1874 * - MPA Rx buffer
1876 static void mwifiex_cleanup_sdio(struct mwifiex_adapter *adapter)
1878 struct sdio_mmc_card *card = adapter->card;
1880 kfree(card->mp_regs);
1881 kfree(card->mpa_rx.skb_arr);
1882 kfree(card->mpa_rx.len_arr);
1883 kfree(card->mpa_tx.buf);
1884 kfree(card->mpa_rx.buf);
1885 sdio_set_drvdata(card->func, NULL);
1886 kfree(card);
1890 * This function updates the MP end port in card.
1892 static void
1893 mwifiex_update_mp_end_port(struct mwifiex_adapter *adapter, u16 port)
1895 struct sdio_mmc_card *card = adapter->card;
1896 const struct mwifiex_sdio_card_reg *reg = card->reg;
1897 int i;
1899 card->mp_end_port = port;
1901 card->mp_data_port_mask = reg->data_port_mask;
1903 if (reg->start_wr_port) {
1904 for (i = 1; i <= card->max_ports - card->mp_end_port; i++)
1905 card->mp_data_port_mask &=
1906 ~(1 << (card->max_ports - i));
1909 card->curr_wr_port = reg->start_wr_port;
1911 dev_dbg(adapter->dev, "cmd: mp_end_port %d, data port mask 0x%x\n",
1912 port, card->mp_data_port_mask);
1915 static struct mmc_host *reset_host;
1916 static void sdio_card_reset_worker(struct work_struct *work)
1918 struct mmc_host *target = reset_host;
1920 /* The actual reset operation must be run outside of driver thread.
1921 * This is because mmc_remove_host() will cause the device to be
1922 * instantly destroyed, and the driver then needs to end its thread,
1923 * leading to a deadlock.
1925 * We run it in a totally independent workqueue.
1928 pr_err("Resetting card...\n");
1929 mmc_remove_host(target);
1930 /* 20ms delay is based on experiment with sdhci controller */
1931 mdelay(20);
1932 mmc_add_host(target);
1934 static DECLARE_WORK(card_reset_work, sdio_card_reset_worker);
1936 /* This function resets the card */
1937 static void mwifiex_sdio_card_reset(struct mwifiex_adapter *adapter)
1939 struct sdio_mmc_card *card = adapter->card;
1941 reset_host = card->func->card->host;
1942 schedule_work(&card_reset_work);
1945 static struct mwifiex_if_ops sdio_ops = {
1946 .init_if = mwifiex_init_sdio,
1947 .cleanup_if = mwifiex_cleanup_sdio,
1948 .check_fw_status = mwifiex_check_fw_status,
1949 .prog_fw = mwifiex_prog_fw_w_helper,
1950 .register_dev = mwifiex_register_dev,
1951 .unregister_dev = mwifiex_unregister_dev,
1952 .enable_int = mwifiex_sdio_enable_host_int,
1953 .disable_int = mwifiex_sdio_disable_host_int,
1954 .process_int_status = mwifiex_process_int_status,
1955 .host_to_card = mwifiex_sdio_host_to_card,
1956 .wakeup = mwifiex_pm_wakeup_card,
1957 .wakeup_complete = mwifiex_pm_wakeup_card_complete,
1959 /* SDIO specific */
1960 .update_mp_end_port = mwifiex_update_mp_end_port,
1961 .cleanup_mpa_buf = mwifiex_cleanup_mpa_buf,
1962 .cmdrsp_complete = mwifiex_sdio_cmdrsp_complete,
1963 .event_complete = mwifiex_sdio_event_complete,
1964 .card_reset = mwifiex_sdio_card_reset,
1968 * This function initializes the SDIO driver.
1970 * This initiates the semaphore and registers the device with
1971 * SDIO bus.
1973 static int
1974 mwifiex_sdio_init_module(void)
1976 sema_init(&add_remove_card_sem, 1);
1978 /* Clear the flag in case user removes the card. */
1979 user_rmmod = 0;
1981 return sdio_register_driver(&mwifiex_sdio);
1985 * This function cleans up the SDIO driver.
1987 * The following major steps are followed for cleanup -
1988 * - Resume the device if its suspended
1989 * - Disconnect the device if connected
1990 * - Shutdown the firmware
1991 * - Unregister the device from SDIO bus.
1993 static void
1994 mwifiex_sdio_cleanup_module(void)
1996 if (!down_interruptible(&add_remove_card_sem))
1997 up(&add_remove_card_sem);
1999 /* Set the flag as user is removing this module. */
2000 user_rmmod = 1;
2002 cancel_work_sync(&card_reset_work);
2003 sdio_unregister_driver(&mwifiex_sdio);
2006 module_init(mwifiex_sdio_init_module);
2007 module_exit(mwifiex_sdio_cleanup_module);
2009 MODULE_AUTHOR("Marvell International Ltd.");
2010 MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION);
2011 MODULE_VERSION(SDIO_VERSION);
2012 MODULE_LICENSE("GPL v2");
2013 MODULE_FIRMWARE(SD8786_DEFAULT_FW_NAME);
2014 MODULE_FIRMWARE(SD8787_DEFAULT_FW_NAME);
2015 MODULE_FIRMWARE(SD8797_DEFAULT_FW_NAME);
2016 MODULE_FIRMWARE(SD8897_DEFAULT_FW_NAME);