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>
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
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.
49 static struct mwifiex_if_ops sdio_ops;
51 static struct semaphore add_remove_card_sem;
53 static int mwifiex_sdio_resume(struct device *dev);
58 * This function probes an mwifiex device and registers it. It allocates
59 * the card structure, enables SDIO function number and initiates the
60 * device registration and initialization procedure by adding a logical
64 mwifiex_sdio_probe(struct sdio_func *func, const struct sdio_device_id *id)
67 struct sdio_mmc_card *card = NULL;
69 pr_debug("info: vendor=0x%4.04X device=0x%4.04X class=%d function=%d\n",
70 func->vendor, func->device, func->class, func->num);
72 card = kzalloc(sizeof(struct sdio_mmc_card), GFP_KERNEL);
78 func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
80 if (id->driver_data) {
81 struct mwifiex_sdio_device *data = (void *)id->driver_data;
83 card->firmware = data->firmware;
84 card->reg = data->reg;
85 card->max_ports = data->max_ports;
86 card->mp_agg_pkt_limit = data->mp_agg_pkt_limit;
89 sdio_claim_host(func);
90 ret = sdio_enable_func(func);
91 sdio_release_host(func);
94 pr_err("%s: failed to enable function\n", __func__);
99 if (mwifiex_add_card(card, &add_remove_card_sem, &sdio_ops,
101 pr_err("%s: add card failed\n", __func__);
103 sdio_claim_host(func);
104 ret = sdio_disable_func(func);
105 sdio_release_host(func);
115 * This function removes the interface and frees up the card structure.
118 mwifiex_sdio_remove(struct sdio_func *func)
120 struct sdio_mmc_card *card;
121 struct mwifiex_adapter *adapter;
122 struct mwifiex_private *priv;
125 pr_debug("info: SDIO func num=%d\n", func->num);
127 card = sdio_get_drvdata(func);
131 adapter = card->adapter;
132 if (!adapter || !adapter->priv_num)
135 /* In case driver is removed when asynchronous FW load is in progress */
136 wait_for_completion(&adapter->fw_load);
139 if (adapter->is_suspended)
140 mwifiex_sdio_resume(adapter->dev);
142 for (i = 0; i < adapter->priv_num; i++)
143 if ((GET_BSS_ROLE(adapter->priv[i]) ==
144 MWIFIEX_BSS_ROLE_STA) &&
145 adapter->priv[i]->media_connected)
146 mwifiex_deauthenticate(adapter->priv[i], NULL);
148 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
149 mwifiex_disable_auto_ds(priv);
150 mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
153 mwifiex_remove_card(card->adapter, &add_remove_card_sem);
160 * Kernel needs to suspend all functions separately. Therefore all
161 * registered functions must have drivers with suspend and resume
162 * methods. Failing that the kernel simply removes the whole card.
164 * If already not suspended, this function allocates and sends a host
165 * sleep activate request to the firmware and turns off the traffic.
167 static int mwifiex_sdio_suspend(struct device *dev)
169 struct sdio_func *func = dev_to_sdio_func(dev);
170 struct sdio_mmc_card *card;
171 struct mwifiex_adapter *adapter;
172 mmc_pm_flag_t pm_flag = 0;
176 pm_flag = sdio_get_host_pm_caps(func);
177 pr_debug("cmd: %s: suspend: PM flag = 0x%x\n",
178 sdio_func_id(func), pm_flag);
179 if (!(pm_flag & MMC_PM_KEEP_POWER)) {
180 pr_err("%s: cannot remain alive while host is"
181 " suspended\n", sdio_func_id(func));
185 card = sdio_get_drvdata(func);
186 if (!card || !card->adapter) {
187 pr_err("suspend: invalid card or adapter\n");
191 pr_err("suspend: sdio_func is not specified\n");
195 adapter = card->adapter;
197 /* Enable the Host Sleep */
198 if (!mwifiex_enable_hs(adapter)) {
199 dev_err(adapter->dev, "cmd: failed to suspend\n");
203 dev_dbg(adapter->dev, "cmd: suspend with MMC_PM_KEEP_POWER\n");
204 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
206 /* Indicate device suspended */
207 adapter->is_suspended = true;
215 * Kernel needs to suspend all functions separately. Therefore all
216 * registered functions must have drivers with suspend and resume
217 * methods. Failing that the kernel simply removes the whole card.
219 * If already not resumed, this function turns on the traffic and
220 * sends a host sleep cancel request to the firmware.
222 static int mwifiex_sdio_resume(struct device *dev)
224 struct sdio_func *func = dev_to_sdio_func(dev);
225 struct sdio_mmc_card *card;
226 struct mwifiex_adapter *adapter;
227 mmc_pm_flag_t pm_flag = 0;
230 pm_flag = sdio_get_host_pm_caps(func);
231 card = sdio_get_drvdata(func);
232 if (!card || !card->adapter) {
233 pr_err("resume: invalid card or adapter\n");
237 pr_err("resume: sdio_func is not specified\n");
241 adapter = card->adapter;
243 if (!adapter->is_suspended) {
244 dev_warn(adapter->dev, "device already resumed\n");
248 adapter->is_suspended = false;
250 /* Disable Host Sleep */
251 mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
257 /* Device ID for SD8786 */
258 #define SDIO_DEVICE_ID_MARVELL_8786 (0x9116)
259 /* Device ID for SD8787 */
260 #define SDIO_DEVICE_ID_MARVELL_8787 (0x9119)
261 /* Device ID for SD8797 */
262 #define SDIO_DEVICE_ID_MARVELL_8797 (0x9129)
265 static const struct sdio_device_id mwifiex_ids[] = {
266 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8786),
267 .driver_data = (unsigned long) &mwifiex_sdio_sd8786},
268 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787),
269 .driver_data = (unsigned long) &mwifiex_sdio_sd8787},
270 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8797),
271 .driver_data = (unsigned long) &mwifiex_sdio_sd8797},
275 MODULE_DEVICE_TABLE(sdio, mwifiex_ids);
277 static const struct dev_pm_ops mwifiex_sdio_pm_ops = {
278 .suspend = mwifiex_sdio_suspend,
279 .resume = mwifiex_sdio_resume,
282 static struct sdio_driver mwifiex_sdio = {
283 .name = "mwifiex_sdio",
284 .id_table = mwifiex_ids,
285 .probe = mwifiex_sdio_probe,
286 .remove = mwifiex_sdio_remove,
288 .owner = THIS_MODULE,
289 .pm = &mwifiex_sdio_pm_ops,
294 * This function writes data into SDIO card register.
297 mwifiex_write_reg(struct mwifiex_adapter *adapter, u32 reg, u8 data)
299 struct sdio_mmc_card *card = adapter->card;
302 sdio_claim_host(card->func);
303 sdio_writeb(card->func, data, reg, &ret);
304 sdio_release_host(card->func);
310 * This function reads data from SDIO card register.
313 mwifiex_read_reg(struct mwifiex_adapter *adapter, u32 reg, u8 *data)
315 struct sdio_mmc_card *card = adapter->card;
319 sdio_claim_host(card->func);
320 val = sdio_readb(card->func, reg, &ret);
321 sdio_release_host(card->func);
329 * This function writes multiple data into SDIO card memory.
331 * This does not work in suspended mode.
334 mwifiex_write_data_sync(struct mwifiex_adapter *adapter,
335 u8 *buffer, u32 pkt_len, u32 port)
337 struct sdio_mmc_card *card = adapter->card;
340 (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE : BLOCK_MODE;
341 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
344 BLOCK_MODE) ? (pkt_len /
345 MWIFIEX_SDIO_BLOCK_SIZE) : pkt_len;
346 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
348 if (adapter->is_suspended) {
349 dev_err(adapter->dev,
350 "%s: not allowed while suspended\n", __func__);
354 sdio_claim_host(card->func);
356 ret = sdio_writesb(card->func, ioport, buffer, blk_cnt * blk_size);
358 sdio_release_host(card->func);
364 * This function reads multiple data from SDIO card memory.
366 static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *buffer,
367 u32 len, u32 port, u8 claim)
369 struct sdio_mmc_card *card = adapter->card;
371 u8 blk_mode = (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE
373 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
374 u32 blk_cnt = (blk_mode == BLOCK_MODE) ? (len / MWIFIEX_SDIO_BLOCK_SIZE)
376 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
379 sdio_claim_host(card->func);
381 ret = sdio_readsb(card->func, buffer, ioport, blk_cnt * blk_size);
384 sdio_release_host(card->func);
390 * This function wakes up the card.
392 * A host power up command is written to the card configuration
393 * register to wake up the card.
395 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
397 dev_dbg(adapter->dev, "event: wakeup device...\n");
399 return mwifiex_write_reg(adapter, CONFIGURATION_REG, HOST_POWER_UP);
403 * This function is called after the card has woken up.
405 * The card configuration register is reset.
407 static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
409 dev_dbg(adapter->dev, "cmd: wakeup device completed\n");
411 return mwifiex_write_reg(adapter, CONFIGURATION_REG, 0);
415 * This function initializes the IO ports.
417 * The following operations are performed -
418 * - Read the IO ports (0, 1 and 2)
419 * - Set host interrupt Reset-To-Read to clear
420 * - Set auto re-enable interrupt
422 static int mwifiex_init_sdio_ioport(struct mwifiex_adapter *adapter)
425 struct sdio_mmc_card *card = adapter->card;
429 /* Read the IO port */
430 if (!mwifiex_read_reg(adapter, IO_PORT_0_REG, ®))
431 adapter->ioport |= (reg & 0xff);
435 if (!mwifiex_read_reg(adapter, IO_PORT_1_REG, ®))
436 adapter->ioport |= ((reg & 0xff) << 8);
440 if (!mwifiex_read_reg(adapter, IO_PORT_2_REG, ®))
441 adapter->ioport |= ((reg & 0xff) << 16);
445 pr_debug("info: SDIO FUNC1 IO port: %#x\n", adapter->ioport);
447 /* Set Host interrupt reset to read to clear */
448 if (!mwifiex_read_reg(adapter, HOST_INT_RSR_REG, ®))
449 mwifiex_write_reg(adapter, HOST_INT_RSR_REG,
450 reg | card->reg->sdio_int_mask);
454 /* Dnld/Upld ready set to auto reset */
455 if (!mwifiex_read_reg(adapter, card->reg->card_misc_cfg_reg, ®))
456 mwifiex_write_reg(adapter, card->reg->card_misc_cfg_reg,
457 reg | AUTO_RE_ENABLE_INT);
465 * This function sends data to the card.
467 static int mwifiex_write_data_to_card(struct mwifiex_adapter *adapter,
468 u8 *payload, u32 pkt_len, u32 port)
474 ret = mwifiex_write_data_sync(adapter, payload, pkt_len, port);
477 dev_err(adapter->dev, "host_to_card, write iomem"
478 " (%d) failed: %d\n", i, ret);
479 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
480 dev_err(adapter->dev, "write CFG reg failed\n");
483 if (i > MAX_WRITE_IOMEM_RETRY)
492 * This function gets the read port.
494 * If control port bit is set in MP read bitmap, the control port
495 * is returned, otherwise the current read port is returned and
496 * the value is increased (provided it does not reach the maximum
497 * limit, in which case it is reset to 1)
499 static int mwifiex_get_rd_port(struct mwifiex_adapter *adapter, u8 *port)
501 struct sdio_mmc_card *card = adapter->card;
502 const struct mwifiex_sdio_card_reg *reg = card->reg;
503 u32 rd_bitmap = card->mp_rd_bitmap;
505 dev_dbg(adapter->dev, "data: mp_rd_bitmap=0x%08x\n", rd_bitmap);
507 if (!(rd_bitmap & (CTRL_PORT_MASK | reg->data_port_mask)))
510 if (card->mp_rd_bitmap & CTRL_PORT_MASK) {
511 card->mp_rd_bitmap &= (u32) (~CTRL_PORT_MASK);
513 dev_dbg(adapter->dev, "data: port=%d mp_rd_bitmap=0x%08x\n",
514 *port, card->mp_rd_bitmap);
518 if (!(card->mp_rd_bitmap & (1 << card->curr_rd_port)))
521 /* We are now handling the SDIO data ports */
522 card->mp_rd_bitmap &= (u32)(~(1 << card->curr_rd_port));
523 *port = card->curr_rd_port;
525 if (++card->curr_rd_port == card->max_ports)
526 card->curr_rd_port = reg->start_rd_port;
528 dev_dbg(adapter->dev,
529 "data: port=%d mp_rd_bitmap=0x%08x -> 0x%08x\n",
530 *port, rd_bitmap, card->mp_rd_bitmap);
536 * This function gets the write port for data.
538 * The current write port is returned if available and the value is
539 * increased (provided it does not reach the maximum limit, in which
540 * case it is reset to 1)
542 static int mwifiex_get_wr_port_data(struct mwifiex_adapter *adapter, u32 *port)
544 struct sdio_mmc_card *card = adapter->card;
545 u32 wr_bitmap = card->mp_wr_bitmap;
547 dev_dbg(adapter->dev, "data: mp_wr_bitmap=0x%08x\n", wr_bitmap);
549 if (!(wr_bitmap & card->mp_data_port_mask))
552 if (card->mp_wr_bitmap & (1 << card->curr_wr_port)) {
553 card->mp_wr_bitmap &= (u32) (~(1 << card->curr_wr_port));
554 *port = card->curr_wr_port;
555 if (++card->curr_wr_port == card->mp_end_port)
556 card->curr_wr_port = card->reg->start_wr_port;
558 adapter->data_sent = true;
562 if (*port == CTRL_PORT) {
563 dev_err(adapter->dev,
564 "invalid data port=%d cur port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
565 *port, card->curr_wr_port, wr_bitmap,
570 dev_dbg(adapter->dev, "data: port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
571 *port, wr_bitmap, card->mp_wr_bitmap);
577 * This function polls the card status.
580 mwifiex_sdio_poll_card_status(struct mwifiex_adapter *adapter, u8 bits)
582 struct sdio_mmc_card *card = adapter->card;
586 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
587 if (mwifiex_read_reg(adapter, card->reg->poll_reg, &cs))
589 else if ((cs & bits) == bits)
592 usleep_range(10, 20);
595 dev_err(adapter->dev, "poll card status failed, tries = %d\n", tries);
601 * This function reads the firmware status.
604 mwifiex_sdio_read_fw_status(struct mwifiex_adapter *adapter, u16 *dat)
606 struct sdio_mmc_card *card = adapter->card;
607 const struct mwifiex_sdio_card_reg *reg = card->reg;
610 if (mwifiex_read_reg(adapter, reg->status_reg_0, &fws0))
613 if (mwifiex_read_reg(adapter, reg->status_reg_1, &fws1))
616 *dat = (u16) ((fws1 << 8) | fws0);
622 * This function disables the host interrupt.
624 * The host interrupt mask is read, the disable bit is reset and
625 * written back to the card host interrupt mask register.
627 static int mwifiex_sdio_disable_host_int(struct mwifiex_adapter *adapter)
629 u8 host_int_mask, host_int_disable = HOST_INT_DISABLE;
631 /* Read back the host_int_mask register */
632 if (mwifiex_read_reg(adapter, HOST_INT_MASK_REG, &host_int_mask))
635 /* Update with the mask and write back to the register */
636 host_int_mask &= ~host_int_disable;
638 if (mwifiex_write_reg(adapter, HOST_INT_MASK_REG, host_int_mask)) {
639 dev_err(adapter->dev, "disable host interrupt failed\n");
647 * This function enables the host interrupt.
649 * The host interrupt enable mask is written to the card
650 * host interrupt mask register.
652 static int mwifiex_sdio_enable_host_int(struct mwifiex_adapter *adapter)
654 struct sdio_mmc_card *card = adapter->card;
656 /* Simply write the mask to the register */
657 if (mwifiex_write_reg(adapter, HOST_INT_MASK_REG,
658 card->reg->host_int_enable)) {
659 dev_err(adapter->dev, "enable host interrupt failed\n");
666 * This function sends a data buffer to the card.
668 static int mwifiex_sdio_card_to_host(struct mwifiex_adapter *adapter,
669 u32 *type, u8 *buffer,
670 u32 npayload, u32 ioport)
676 dev_err(adapter->dev, "%s: buffer is NULL\n", __func__);
680 ret = mwifiex_read_data_sync(adapter, buffer, npayload, ioport, 1);
683 dev_err(adapter->dev, "%s: read iomem failed: %d\n", __func__,
688 nb = le16_to_cpu(*(__le16 *) (buffer));
690 dev_err(adapter->dev, "%s: invalid packet, nb=%d npayload=%d\n",
691 __func__, nb, npayload);
695 *type = le16_to_cpu(*(__le16 *) (buffer + 2));
701 * This function downloads the firmware to the card.
703 * Firmware is downloaded to the card in blocks. Every block download
704 * is tested for CRC errors, and retried a number of times before
707 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
708 struct mwifiex_fw_image *fw)
710 struct sdio_mmc_card *card = adapter->card;
711 const struct mwifiex_sdio_card_reg *reg = card->reg;
713 u8 *firmware = fw->fw_buf;
714 u32 firmware_len = fw->fw_len;
719 u32 txlen, tx_blocks = 0, tries;
723 dev_err(adapter->dev,
724 "firmware image not found! Terminating download\n");
728 dev_dbg(adapter->dev, "info: downloading FW image (%d bytes)\n",
731 /* Assume that the allocated buffer is 8-byte aligned */
732 fwbuf = kzalloc(MWIFIEX_UPLD_SIZE, GFP_KERNEL);
736 /* Perform firmware data transfer */
738 /* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY
740 ret = mwifiex_sdio_poll_card_status(adapter, CARD_IO_READY |
743 dev_err(adapter->dev, "FW download with helper:"
744 " poll status timeout @ %d\n", offset);
749 if (offset >= firmware_len)
752 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
753 ret = mwifiex_read_reg(adapter, reg->base_0_reg,
756 dev_err(adapter->dev,
757 "dev BASE0 register read failed: "
758 "base0=%#04X(%d). Terminating dnld\n",
762 ret = mwifiex_read_reg(adapter, reg->base_1_reg,
765 dev_err(adapter->dev,
766 "dev BASE1 register read failed: "
767 "base1=%#04X(%d). Terminating dnld\n",
771 len = (u16) (((base1 & 0xff) << 8) | (base0 & 0xff));
776 usleep_range(10, 20);
781 } else if (len > MWIFIEX_UPLD_SIZE) {
782 dev_err(adapter->dev,
783 "FW dnld failed @ %d, invalid length %d\n",
793 if (i > MAX_WRITE_IOMEM_RETRY) {
794 dev_err(adapter->dev,
795 "FW dnld failed @ %d, over max retry\n",
800 dev_err(adapter->dev, "CRC indicated by the helper:"
801 " len = 0x%04X, txlen = %d\n", len, txlen);
803 /* Setting this to 0 to resend from same offset */
808 /* Set blocksize to transfer - checking for last
810 if (firmware_len - offset < txlen)
811 txlen = firmware_len - offset;
813 tx_blocks = (txlen + MWIFIEX_SDIO_BLOCK_SIZE - 1)
814 / MWIFIEX_SDIO_BLOCK_SIZE;
816 /* Copy payload to buffer */
817 memmove(fwbuf, &firmware[offset], txlen);
820 ret = mwifiex_write_data_sync(adapter, fwbuf, tx_blocks *
821 MWIFIEX_SDIO_BLOCK_SIZE,
824 dev_err(adapter->dev,
825 "FW download, write iomem (%d) failed @ %d\n",
827 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
828 dev_err(adapter->dev, "write CFG reg failed\n");
837 dev_dbg(adapter->dev, "info: FW download over, size %d bytes\n",
847 * This function checks the firmware status in card.
849 * The winner interface is also determined by this function.
851 static int mwifiex_check_fw_status(struct mwifiex_adapter *adapter,
854 struct sdio_mmc_card *card = adapter->card;
860 /* Wait for firmware initialization event */
861 for (tries = 0; tries < poll_num; tries++) {
862 ret = mwifiex_sdio_read_fw_status(adapter, &firmware_stat);
865 if (firmware_stat == FIRMWARE_READY_SDIO) {
876 (adapter, card->reg->status_reg_0, &winner_status))
888 * This function reads the interrupt status from card.
890 static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
892 struct sdio_mmc_card *card = adapter->card;
896 if (mwifiex_read_data_sync(adapter, card->mp_regs,
897 card->reg->max_mp_regs,
898 REG_PORT | MWIFIEX_SDIO_BYTE_MODE_MASK, 0)) {
899 dev_err(adapter->dev, "read mp_regs failed\n");
903 sdio_ireg = card->mp_regs[HOST_INTSTATUS_REG];
906 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
907 * Clear the interrupt status register
909 dev_dbg(adapter->dev, "int: sdio_ireg = %#x\n", sdio_ireg);
910 spin_lock_irqsave(&adapter->int_lock, flags);
911 adapter->int_status |= sdio_ireg;
912 spin_unlock_irqrestore(&adapter->int_lock, flags);
917 * SDIO interrupt handler.
919 * This function reads the interrupt status from firmware and handles
920 * the interrupt in current thread (ksdioirqd) right away.
923 mwifiex_sdio_interrupt(struct sdio_func *func)
925 struct mwifiex_adapter *adapter;
926 struct sdio_mmc_card *card;
928 card = sdio_get_drvdata(func);
929 if (!card || !card->adapter) {
930 pr_debug("int: func=%p card=%p adapter=%p\n",
931 func, card, card ? card->adapter : NULL);
934 adapter = card->adapter;
936 if (adapter->surprise_removed)
939 if (!adapter->pps_uapsd_mode && adapter->ps_state == PS_STATE_SLEEP)
940 adapter->ps_state = PS_STATE_AWAKE;
942 mwifiex_interrupt_status(adapter);
943 mwifiex_main_process(adapter);
947 * This function decodes a received packet.
949 * Based on the type, the packet is treated as either a data, or
950 * a command response, or an event, and the correct handler
951 * function is invoked.
953 static int mwifiex_decode_rx_packet(struct mwifiex_adapter *adapter,
954 struct sk_buff *skb, u32 upld_typ)
958 skb_pull(skb, INTF_HEADER_LEN);
961 case MWIFIEX_TYPE_DATA:
962 dev_dbg(adapter->dev, "info: --- Rx: Data packet ---\n");
963 mwifiex_handle_rx_packet(adapter, skb);
966 case MWIFIEX_TYPE_CMD:
967 dev_dbg(adapter->dev, "info: --- Rx: Cmd Response ---\n");
968 /* take care of curr_cmd = NULL case */
969 if (!adapter->curr_cmd) {
970 cmd_buf = adapter->upld_buf;
972 if (adapter->ps_state == PS_STATE_SLEEP_CFM)
973 mwifiex_process_sleep_confirm_resp(adapter,
977 memcpy(cmd_buf, skb->data,
978 min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER,
981 dev_kfree_skb_any(skb);
983 adapter->cmd_resp_received = true;
984 adapter->curr_cmd->resp_skb = skb;
988 case MWIFIEX_TYPE_EVENT:
989 dev_dbg(adapter->dev, "info: --- Rx: Event ---\n");
990 adapter->event_cause = *(u32 *) skb->data;
992 if ((skb->len > 0) && (skb->len < MAX_EVENT_SIZE))
993 memcpy(adapter->event_body,
994 skb->data + MWIFIEX_EVENT_HEADER_LEN,
997 /* event cause has been saved to adapter->event_cause */
998 adapter->event_received = true;
999 adapter->event_skb = skb;
1004 dev_err(adapter->dev, "unknown upload type %#x\n", upld_typ);
1005 dev_kfree_skb_any(skb);
1013 * This function transfers received packets from card to driver, performing
1014 * aggregation if required.
1016 * For data received on control port, or if aggregation is disabled, the
1017 * received buffers are uploaded as separate packets. However, if aggregation
1018 * is enabled and required, the buffers are copied onto an aggregation buffer,
1019 * provided there is space left, processed and finally uploaded.
1021 static int mwifiex_sdio_card_to_host_mp_aggr(struct mwifiex_adapter *adapter,
1022 struct sk_buff *skb, u8 port)
1024 struct sdio_mmc_card *card = adapter->card;
1025 s32 f_do_rx_aggr = 0;
1026 s32 f_do_rx_cur = 0;
1028 struct sk_buff *skb_deaggr;
1030 u32 pkt_len, pkt_type, mport;
1032 u32 rx_len = skb->len;
1034 if (port == CTRL_PORT) {
1035 /* Read the command Resp without aggr */
1036 dev_dbg(adapter->dev, "info: %s: no aggregation for cmd "
1037 "response\n", __func__);
1040 goto rx_curr_single;
1043 if (!card->mpa_rx.enabled) {
1044 dev_dbg(adapter->dev, "info: %s: rx aggregation disabled\n",
1048 goto rx_curr_single;
1051 if (card->mp_rd_bitmap & (~((u32) CTRL_PORT_MASK))) {
1052 /* Some more data RX pending */
1053 dev_dbg(adapter->dev, "info: %s: not last packet\n", __func__);
1055 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1056 if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len)) {
1059 /* No room in Aggr buf, do rx aggr now */
1064 /* Rx aggr not in progress */
1069 /* No more data RX pending */
1070 dev_dbg(adapter->dev, "info: %s: last packet\n", __func__);
1072 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1074 if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len))
1077 /* No room in Aggr buf, do rx aggr now */
1085 dev_dbg(adapter->dev, "info: current packet aggregation\n");
1086 /* Curr pkt can be aggregated */
1087 MP_RX_AGGR_SETUP(card, skb, port);
1089 if (MP_RX_AGGR_PKT_LIMIT_REACHED(card) ||
1090 MP_RX_AGGR_PORT_LIMIT_REACHED(card)) {
1091 dev_dbg(adapter->dev, "info: %s: aggregated packet "
1092 "limit reached\n", __func__);
1093 /* No more pkts allowed in Aggr buf, rx it */
1099 /* do aggr RX now */
1100 dev_dbg(adapter->dev, "info: do_rx_aggr: num of packets: %d\n",
1101 card->mpa_rx.pkt_cnt);
1103 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1104 (card->mpa_rx.ports << 4)) + card->mpa_rx.start_port;
1106 if (mwifiex_read_data_sync(adapter, card->mpa_rx.buf,
1107 card->mpa_rx.buf_len, mport, 1))
1110 curr_ptr = card->mpa_rx.buf;
1112 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1114 /* get curr PKT len & type */
1115 pkt_len = *(u16 *) &curr_ptr[0];
1116 pkt_type = *(u16 *) &curr_ptr[2];
1118 /* copy pkt to deaggr buf */
1119 skb_deaggr = card->mpa_rx.skb_arr[pind];
1121 if ((pkt_type == MWIFIEX_TYPE_DATA) && (pkt_len <=
1122 card->mpa_rx.len_arr[pind])) {
1124 memcpy(skb_deaggr->data, curr_ptr, pkt_len);
1126 skb_trim(skb_deaggr, pkt_len);
1128 /* Process de-aggr packet */
1129 mwifiex_decode_rx_packet(adapter, skb_deaggr,
1132 dev_err(adapter->dev, "wrong aggr pkt:"
1133 " type=%d len=%d max_len=%d\n",
1135 card->mpa_rx.len_arr[pind]);
1136 dev_kfree_skb_any(skb_deaggr);
1138 curr_ptr += card->mpa_rx.len_arr[pind];
1140 MP_RX_AGGR_BUF_RESET(card);
1145 dev_dbg(adapter->dev, "info: RX: port: %d, rx_len: %d\n",
1148 if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1149 skb->data, skb->len,
1150 adapter->ioport + port))
1153 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1159 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1160 /* Multiport-aggregation transfer failed - cleanup */
1161 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1162 /* copy pkt to deaggr buf */
1163 skb_deaggr = card->mpa_rx.skb_arr[pind];
1164 dev_kfree_skb_any(skb_deaggr);
1166 MP_RX_AGGR_BUF_RESET(card);
1170 /* Single transfer pending. Free curr buff also */
1171 dev_kfree_skb_any(skb);
1177 * This function checks the current interrupt status.
1179 * The following interrupts are checked and handled by this function -
1182 * - Packets received
1184 * Since the firmware does not generate download ready interrupt if the
1185 * port updated is command port only, command sent interrupt checking
1186 * should be done manually, and for every SDIO interrupt.
1188 * In case of Rx packets received, the packets are uploaded from card to
1189 * host and processed accordingly.
1191 static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
1193 struct sdio_mmc_card *card = adapter->card;
1194 const struct mwifiex_sdio_card_reg *reg = card->reg;
1197 struct sk_buff *skb;
1198 u8 port = CTRL_PORT;
1199 u32 len_reg_l, len_reg_u;
1202 unsigned long flags;
1204 spin_lock_irqsave(&adapter->int_lock, flags);
1205 sdio_ireg = adapter->int_status;
1206 adapter->int_status = 0;
1207 spin_unlock_irqrestore(&adapter->int_lock, flags);
1212 if (sdio_ireg & DN_LD_HOST_INT_STATUS) {
1213 card->mp_wr_bitmap =
1214 ((u32) card->mp_regs[reg->wr_bitmap_u]) << 8;
1215 card->mp_wr_bitmap |=
1216 (u32) card->mp_regs[reg->wr_bitmap_l];
1217 dev_dbg(adapter->dev, "int: DNLD: wr_bitmap=0x%08x\n",
1218 card->mp_wr_bitmap);
1219 if (adapter->data_sent &&
1220 (card->mp_wr_bitmap & card->mp_data_port_mask)) {
1221 dev_dbg(adapter->dev,
1222 "info: <--- Tx DONE Interrupt --->\n");
1223 adapter->data_sent = false;
1227 /* As firmware will not generate download ready interrupt if the port
1228 updated is command port only, cmd_sent should be done for any SDIO
1230 if (adapter->cmd_sent) {
1231 /* Check if firmware has attach buffer at command port and
1232 update just that in wr_bit_map. */
1233 card->mp_wr_bitmap |=
1234 (u32) card->mp_regs[reg->wr_bitmap_l] & CTRL_PORT_MASK;
1235 if (card->mp_wr_bitmap & CTRL_PORT_MASK)
1236 adapter->cmd_sent = false;
1239 dev_dbg(adapter->dev, "info: cmd_sent=%d data_sent=%d\n",
1240 adapter->cmd_sent, adapter->data_sent);
1241 if (sdio_ireg & UP_LD_HOST_INT_STATUS) {
1242 card->mp_rd_bitmap =
1243 ((u32) card->mp_regs[reg->rd_bitmap_u]) << 8;
1244 card->mp_rd_bitmap |= (u32) card->mp_regs[reg->rd_bitmap_l];
1245 dev_dbg(adapter->dev, "int: UPLD: rd_bitmap=0x%08x\n",
1246 card->mp_rd_bitmap);
1249 ret = mwifiex_get_rd_port(adapter, &port);
1251 dev_dbg(adapter->dev,
1252 "info: no more rd_port available\n");
1255 len_reg_l = reg->rd_len_p0_l + (port << 1);
1256 len_reg_u = reg->rd_len_p0_u + (port << 1);
1257 rx_len = ((u16) card->mp_regs[len_reg_u]) << 8;
1258 rx_len |= (u16) card->mp_regs[len_reg_l];
1259 dev_dbg(adapter->dev, "info: RX: port=%d rx_len=%u\n",
1262 (rx_len + MWIFIEX_SDIO_BLOCK_SIZE -
1263 1) / MWIFIEX_SDIO_BLOCK_SIZE;
1264 if (rx_len <= INTF_HEADER_LEN ||
1265 (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1266 MWIFIEX_RX_DATA_BUF_SIZE) {
1267 dev_err(adapter->dev, "invalid rx_len=%d\n",
1271 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1273 skb = dev_alloc_skb(rx_len);
1276 dev_err(adapter->dev, "%s: failed to alloc skb",
1281 skb_put(skb, rx_len);
1283 dev_dbg(adapter->dev, "info: rx_len = %d skb->len = %d\n",
1286 if (mwifiex_sdio_card_to_host_mp_aggr(adapter, skb,
1290 dev_err(adapter->dev, "card_to_host_mpa failed:"
1291 " int status=%#x\n", sdio_ireg);
1292 if (mwifiex_read_reg(adapter,
1293 CONFIGURATION_REG, &cr))
1294 dev_err(adapter->dev,
1295 "read CFG reg failed\n");
1297 dev_dbg(adapter->dev,
1298 "info: CFG reg val = %d\n", cr);
1299 if (mwifiex_write_reg(adapter,
1302 dev_err(adapter->dev,
1303 "write CFG reg failed\n");
1305 dev_dbg(adapter->dev, "info: write success\n");
1306 if (mwifiex_read_reg(adapter,
1307 CONFIGURATION_REG, &cr))
1308 dev_err(adapter->dev,
1309 "read CFG reg failed\n");
1311 dev_dbg(adapter->dev,
1312 "info: CFG reg val =%x\n", cr);
1322 * This function aggregates transmission buffers in driver and downloads
1323 * the aggregated packet to card.
1325 * The individual packets are aggregated by copying into an aggregation
1326 * buffer and then downloaded to the card. Previous unsent packets in the
1327 * aggregation buffer are pre-copied first before new packets are added.
1328 * Aggregation is done till there is space left in the aggregation buffer,
1329 * or till new packets are available.
1331 * The function will only download the packet to the card when aggregation
1332 * stops, otherwise it will just aggregate the packet in aggregation buffer
1335 static int mwifiex_host_to_card_mp_aggr(struct mwifiex_adapter *adapter,
1336 u8 *payload, u32 pkt_len, u32 port,
1339 struct sdio_mmc_card *card = adapter->card;
1341 s32 f_send_aggr_buf = 0;
1342 s32 f_send_cur_buf = 0;
1343 s32 f_precopy_cur_buf = 0;
1344 s32 f_postcopy_cur_buf = 0;
1347 if ((!card->mpa_tx.enabled) || (port == CTRL_PORT)) {
1348 dev_dbg(adapter->dev, "info: %s: tx aggregation disabled\n",
1352 goto tx_curr_single;
1356 /* More pkt in TX queue */
1357 dev_dbg(adapter->dev, "info: %s: more packets in queue.\n",
1360 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1361 if (!MP_TX_AGGR_PORT_LIMIT_REACHED(card) &&
1362 MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len)) {
1363 f_precopy_cur_buf = 1;
1365 if (!(card->mp_wr_bitmap &
1366 (1 << card->curr_wr_port)) ||
1367 !MP_TX_AGGR_BUF_HAS_ROOM(
1368 card, pkt_len + next_pkt_len))
1369 f_send_aggr_buf = 1;
1371 /* No room in Aggr buf, send it */
1372 f_send_aggr_buf = 1;
1374 if (MP_TX_AGGR_PORT_LIMIT_REACHED(card) ||
1375 !(card->mp_wr_bitmap &
1376 (1 << card->curr_wr_port)))
1379 f_postcopy_cur_buf = 1;
1382 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len) &&
1383 (card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1384 f_precopy_cur_buf = 1;
1389 /* Last pkt in TX queue */
1390 dev_dbg(adapter->dev, "info: %s: Last packet in Tx Queue.\n",
1393 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1394 /* some packs in Aggr buf already */
1395 f_send_aggr_buf = 1;
1397 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len))
1398 f_precopy_cur_buf = 1;
1400 /* No room in Aggr buf, send it */
1407 if (f_precopy_cur_buf) {
1408 dev_dbg(adapter->dev, "data: %s: precopy current buffer\n",
1410 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1412 if (MP_TX_AGGR_PKT_LIMIT_REACHED(card) ||
1413 MP_TX_AGGR_PORT_LIMIT_REACHED(card))
1414 /* No more pkts allowed in Aggr buf, send it */
1415 f_send_aggr_buf = 1;
1418 if (f_send_aggr_buf) {
1419 dev_dbg(adapter->dev, "data: %s: send aggr buffer: %d %d\n",
1421 card->mpa_tx.start_port, card->mpa_tx.ports);
1422 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1423 (card->mpa_tx.ports << 4)) + card->mpa_tx.start_port;
1424 ret = mwifiex_write_data_to_card(adapter, card->mpa_tx.buf,
1425 card->mpa_tx.buf_len, mport);
1427 MP_TX_AGGR_BUF_RESET(card);
1431 if (f_send_cur_buf) {
1432 dev_dbg(adapter->dev, "data: %s: send current buffer %d\n",
1434 ret = mwifiex_write_data_to_card(adapter, payload, pkt_len,
1435 adapter->ioport + port);
1438 if (f_postcopy_cur_buf) {
1439 dev_dbg(adapter->dev, "data: %s: postcopy current buffer\n",
1441 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1448 * This function downloads data from driver to card.
1450 * Both commands and data packets are transferred to the card by this
1453 * This function adds the SDIO specific header to the front of the buffer
1454 * before transferring. The header contains the length of the packet and
1455 * the type. The firmware handles the packets based upon this set type.
1457 static int mwifiex_sdio_host_to_card(struct mwifiex_adapter *adapter,
1458 u8 type, struct sk_buff *skb,
1459 struct mwifiex_tx_param *tx_param)
1461 struct sdio_mmc_card *card = adapter->card;
1465 u32 port = CTRL_PORT;
1466 u8 *payload = (u8 *)skb->data;
1467 u32 pkt_len = skb->len;
1469 /* Allocate buffer and copy payload */
1470 blk_size = MWIFIEX_SDIO_BLOCK_SIZE;
1471 buf_block_len = (pkt_len + blk_size - 1) / blk_size;
1472 *(u16 *) &payload[0] = (u16) pkt_len;
1473 *(u16 *) &payload[2] = type;
1476 * This is SDIO specific header
1478 * u16 type (MWIFIEX_TYPE_DATA = 0, MWIFIEX_TYPE_CMD = 1,
1479 * MWIFIEX_TYPE_EVENT = 3)
1481 if (type == MWIFIEX_TYPE_DATA) {
1482 ret = mwifiex_get_wr_port_data(adapter, &port);
1484 dev_err(adapter->dev, "%s: no wr_port available\n",
1489 adapter->cmd_sent = true;
1490 /* Type must be MWIFIEX_TYPE_CMD */
1492 if (pkt_len <= INTF_HEADER_LEN ||
1493 pkt_len > MWIFIEX_UPLD_SIZE)
1494 dev_err(adapter->dev, "%s: payload=%p, nb=%d\n",
1495 __func__, payload, pkt_len);
1498 /* Transfer data to card */
1499 pkt_len = buf_block_len * blk_size;
1502 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1503 port, tx_param->next_pkt_len
1506 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1510 if (type == MWIFIEX_TYPE_CMD)
1511 adapter->cmd_sent = false;
1512 if (type == MWIFIEX_TYPE_DATA)
1513 adapter->data_sent = false;
1515 if (type == MWIFIEX_TYPE_DATA) {
1516 if (!(card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1517 adapter->data_sent = true;
1519 adapter->data_sent = false;
1527 * This function allocates the MPA Tx and Rx buffers.
1529 static int mwifiex_alloc_sdio_mpa_buffers(struct mwifiex_adapter *adapter,
1530 u32 mpa_tx_buf_size, u32 mpa_rx_buf_size)
1532 struct sdio_mmc_card *card = adapter->card;
1535 card->mpa_tx.buf = kzalloc(mpa_tx_buf_size, GFP_KERNEL);
1536 if (!card->mpa_tx.buf) {
1541 card->mpa_tx.buf_size = mpa_tx_buf_size;
1543 card->mpa_rx.buf = kzalloc(mpa_rx_buf_size, GFP_KERNEL);
1544 if (!card->mpa_rx.buf) {
1549 card->mpa_rx.buf_size = mpa_rx_buf_size;
1553 kfree(card->mpa_tx.buf);
1554 kfree(card->mpa_rx.buf);
1561 * This function unregisters the SDIO device.
1563 * The SDIO IRQ is released, the function is disabled and driver
1564 * data is set to null.
1567 mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
1569 struct sdio_mmc_card *card = adapter->card;
1571 if (adapter->card) {
1572 /* Release the SDIO IRQ */
1573 sdio_claim_host(card->func);
1574 sdio_release_irq(card->func);
1575 sdio_disable_func(card->func);
1576 sdio_release_host(card->func);
1577 sdio_set_drvdata(card->func, NULL);
1582 * This function registers the SDIO device.
1584 * SDIO IRQ is claimed, block size is set and driver data is initialized.
1586 static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
1589 struct sdio_mmc_card *card = adapter->card;
1590 struct sdio_func *func = card->func;
1592 /* save adapter pointer in card */
1593 card->adapter = adapter;
1595 sdio_claim_host(func);
1597 /* Request the SDIO IRQ */
1598 ret = sdio_claim_irq(func, mwifiex_sdio_interrupt);
1600 pr_err("claim irq failed: ret=%d\n", ret);
1604 /* Set block size */
1605 ret = sdio_set_block_size(card->func, MWIFIEX_SDIO_BLOCK_SIZE);
1607 pr_err("cannot set SDIO block size\n");
1612 sdio_release_host(func);
1613 sdio_set_drvdata(func, card);
1615 adapter->dev = &func->dev;
1617 strcpy(adapter->fw_name, card->firmware);
1622 sdio_release_irq(func);
1624 sdio_disable_func(func);
1625 sdio_release_host(func);
1626 adapter->card = NULL;
1632 * This function initializes the SDIO driver.
1634 * The following initializations steps are followed -
1635 * - Read the Host interrupt status register to acknowledge
1636 * the first interrupt got from bootloader
1637 * - Disable host interrupt mask register
1639 * - Initialize SDIO variables in card
1640 * - Allocate MP registers
1641 * - Allocate MPA Tx and Rx buffers
1643 static int mwifiex_init_sdio(struct mwifiex_adapter *adapter)
1645 struct sdio_mmc_card *card = adapter->card;
1646 const struct mwifiex_sdio_card_reg *reg = card->reg;
1651 * Read the HOST_INT_STATUS_REG for ACK the first interrupt got
1652 * from the bootloader. If we don't do this we get a interrupt
1653 * as soon as we register the irq.
1655 mwifiex_read_reg(adapter, HOST_INTSTATUS_REG, &sdio_ireg);
1657 /* Disable host interrupt mask register for SDIO */
1658 mwifiex_sdio_disable_host_int(adapter);
1660 /* Get SDIO ioport */
1661 mwifiex_init_sdio_ioport(adapter);
1663 /* Initialize SDIO variables in card */
1664 card->mp_rd_bitmap = 0;
1665 card->mp_wr_bitmap = 0;
1666 card->curr_rd_port = reg->start_rd_port;
1667 card->curr_wr_port = reg->start_wr_port;
1669 card->mp_data_port_mask = reg->data_port_mask;
1671 card->mpa_tx.buf_len = 0;
1672 card->mpa_tx.pkt_cnt = 0;
1673 card->mpa_tx.start_port = 0;
1675 card->mpa_tx.enabled = 1;
1676 card->mpa_tx.pkt_aggr_limit = card->mp_agg_pkt_limit;
1678 card->mpa_rx.buf_len = 0;
1679 card->mpa_rx.pkt_cnt = 0;
1680 card->mpa_rx.start_port = 0;
1682 card->mpa_rx.enabled = 1;
1683 card->mpa_rx.pkt_aggr_limit = card->mp_agg_pkt_limit;
1685 /* Allocate buffers for SDIO MP-A */
1686 card->mp_regs = kzalloc(reg->max_mp_regs, GFP_KERNEL);
1690 ret = mwifiex_alloc_sdio_mpa_buffers(adapter,
1691 SDIO_MP_TX_AGGR_DEF_BUF_SIZE,
1692 SDIO_MP_RX_AGGR_DEF_BUF_SIZE);
1694 dev_err(adapter->dev, "failed to alloc sdio mp-a buffers\n");
1695 kfree(card->mp_regs);
1703 * This function resets the MPA Tx and Rx buffers.
1705 static void mwifiex_cleanup_mpa_buf(struct mwifiex_adapter *adapter)
1707 struct sdio_mmc_card *card = adapter->card;
1709 MP_TX_AGGR_BUF_RESET(card);
1710 MP_RX_AGGR_BUF_RESET(card);
1714 * This function cleans up the allocated card buffers.
1716 * The following are freed by this function -
1721 static void mwifiex_cleanup_sdio(struct mwifiex_adapter *adapter)
1723 struct sdio_mmc_card *card = adapter->card;
1725 kfree(card->mp_regs);
1726 kfree(card->mpa_tx.buf);
1727 kfree(card->mpa_rx.buf);
1731 * This function updates the MP end port in card.
1734 mwifiex_update_mp_end_port(struct mwifiex_adapter *adapter, u16 port)
1736 struct sdio_mmc_card *card = adapter->card;
1737 const struct mwifiex_sdio_card_reg *reg = card->reg;
1740 card->mp_end_port = port;
1742 card->mp_data_port_mask = reg->data_port_mask;
1744 for (i = 1; i <= card->max_ports - card->mp_end_port; i++)
1745 card->mp_data_port_mask &= ~(1 << (card->max_ports - i));
1747 card->curr_wr_port = reg->start_wr_port;
1749 dev_dbg(adapter->dev, "cmd: mp_end_port %d, data port mask 0x%x\n",
1750 port, card->mp_data_port_mask);
1753 static struct mmc_host *reset_host;
1754 static void sdio_card_reset_worker(struct work_struct *work)
1756 struct mmc_host *target = reset_host;
1758 /* The actual reset operation must be run outside of driver thread.
1759 * This is because mmc_remove_host() will cause the device to be
1760 * instantly destroyed, and the driver then needs to end its thread,
1761 * leading to a deadlock.
1763 * We run it in a totally independent workqueue.
1766 pr_err("Resetting card...\n");
1767 mmc_remove_host(target);
1768 /* 20ms delay is based on experiment with sdhci controller */
1770 mmc_add_host(target);
1772 static DECLARE_WORK(card_reset_work, sdio_card_reset_worker);
1774 /* This function resets the card */
1775 static void mwifiex_sdio_card_reset(struct mwifiex_adapter *adapter)
1777 struct sdio_mmc_card *card = adapter->card;
1779 reset_host = card->func->card->host;
1780 schedule_work(&card_reset_work);
1783 static struct mwifiex_if_ops sdio_ops = {
1784 .init_if = mwifiex_init_sdio,
1785 .cleanup_if = mwifiex_cleanup_sdio,
1786 .check_fw_status = mwifiex_check_fw_status,
1787 .prog_fw = mwifiex_prog_fw_w_helper,
1788 .register_dev = mwifiex_register_dev,
1789 .unregister_dev = mwifiex_unregister_dev,
1790 .enable_int = mwifiex_sdio_enable_host_int,
1791 .process_int_status = mwifiex_process_int_status,
1792 .host_to_card = mwifiex_sdio_host_to_card,
1793 .wakeup = mwifiex_pm_wakeup_card,
1794 .wakeup_complete = mwifiex_pm_wakeup_card_complete,
1797 .update_mp_end_port = mwifiex_update_mp_end_port,
1798 .cleanup_mpa_buf = mwifiex_cleanup_mpa_buf,
1799 .cmdrsp_complete = mwifiex_sdio_cmdrsp_complete,
1800 .event_complete = mwifiex_sdio_event_complete,
1801 .card_reset = mwifiex_sdio_card_reset,
1805 * This function initializes the SDIO driver.
1807 * This initiates the semaphore and registers the device with
1811 mwifiex_sdio_init_module(void)
1813 sema_init(&add_remove_card_sem, 1);
1815 /* Clear the flag in case user removes the card. */
1818 return sdio_register_driver(&mwifiex_sdio);
1822 * This function cleans up the SDIO driver.
1824 * The following major steps are followed for cleanup -
1825 * - Resume the device if its suspended
1826 * - Disconnect the device if connected
1827 * - Shutdown the firmware
1828 * - Unregister the device from SDIO bus.
1831 mwifiex_sdio_cleanup_module(void)
1833 if (!down_interruptible(&add_remove_card_sem))
1834 up(&add_remove_card_sem);
1836 /* Set the flag as user is removing this module. */
1839 cancel_work_sync(&card_reset_work);
1840 sdio_unregister_driver(&mwifiex_sdio);
1843 module_init(mwifiex_sdio_init_module);
1844 module_exit(mwifiex_sdio_cleanup_module);
1846 MODULE_AUTHOR("Marvell International Ltd.");
1847 MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION);
1848 MODULE_VERSION(SDIO_VERSION);
1849 MODULE_LICENSE("GPL v2");
1850 MODULE_FIRMWARE(SD8786_DEFAULT_FW_NAME);
1851 MODULE_FIRMWARE(SD8787_DEFAULT_FW_NAME);
1852 MODULE_FIRMWARE(SD8797_DEFAULT_FW_NAME);