2 * Marvell Wireless LAN device driver: SDIO specific handling
4 * Copyright (C) 2011-2014, 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;
50 static unsigned long iface_work_flags;
52 static struct semaphore add_remove_card_sem;
54 static struct memory_type_mapping generic_mem_type_map[] = {
55 {"DUMP", NULL, 0, 0xDD},
58 static struct memory_type_mapping mem_type_mapping_tbl[] = {
59 {"ITCM", NULL, 0, 0xF0},
60 {"DTCM", NULL, 0, 0xF1},
61 {"SQRAM", NULL, 0, 0xF2},
62 {"APU", NULL, 0, 0xF3},
63 {"CIU", NULL, 0, 0xF4},
64 {"ICU", NULL, 0, 0xF5},
65 {"MAC", NULL, 0, 0xF6},
66 {"EXT7", NULL, 0, 0xF7},
67 {"EXT8", NULL, 0, 0xF8},
68 {"EXT9", NULL, 0, 0xF9},
69 {"EXT10", NULL, 0, 0xFA},
70 {"EXT11", NULL, 0, 0xFB},
71 {"EXT12", NULL, 0, 0xFC},
72 {"EXT13", NULL, 0, 0xFD},
73 {"EXTLAST", NULL, 0, 0xFE},
79 * This function probes an mwifiex device and registers it. It allocates
80 * the card structure, enables SDIO function number and initiates the
81 * device registration and initialization procedure by adding a logical
85 mwifiex_sdio_probe(struct sdio_func *func, const struct sdio_device_id *id)
88 struct sdio_mmc_card *card = NULL;
90 pr_debug("info: vendor=0x%4.04X device=0x%4.04X class=%d function=%d\n",
91 func->vendor, func->device, func->class, func->num);
93 card = kzalloc(sizeof(struct sdio_mmc_card), GFP_KERNEL);
100 func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
102 if (id->driver_data) {
103 struct mwifiex_sdio_device *data = (void *)id->driver_data;
105 card->firmware = data->firmware;
106 card->reg = data->reg;
107 card->max_ports = data->max_ports;
108 card->mp_agg_pkt_limit = data->mp_agg_pkt_limit;
109 card->supports_sdio_new_mode = data->supports_sdio_new_mode;
110 card->has_control_mask = data->has_control_mask;
111 card->tx_buf_size = data->tx_buf_size;
112 card->mp_tx_agg_buf_size = data->mp_tx_agg_buf_size;
113 card->mp_rx_agg_buf_size = data->mp_rx_agg_buf_size;
114 card->can_dump_fw = data->can_dump_fw;
115 card->fw_dump_enh = data->fw_dump_enh;
116 card->can_auto_tdls = data->can_auto_tdls;
117 card->can_ext_scan = data->can_ext_scan;
120 sdio_claim_host(func);
121 ret = sdio_enable_func(func);
122 sdio_release_host(func);
125 pr_err("%s: failed to enable function\n", __func__);
130 if (mwifiex_add_card(card, &add_remove_card_sem, &sdio_ops,
132 pr_err("%s: add card failed\n", __func__);
134 sdio_claim_host(func);
135 ret = sdio_disable_func(func);
136 sdio_release_host(func);
146 * Kernel needs to suspend all functions separately. Therefore all
147 * registered functions must have drivers with suspend and resume
148 * methods. Failing that the kernel simply removes the whole card.
150 * If already not resumed, this function turns on the traffic and
151 * sends a host sleep cancel request to the firmware.
153 static int mwifiex_sdio_resume(struct device *dev)
155 struct sdio_func *func = dev_to_sdio_func(dev);
156 struct sdio_mmc_card *card;
157 struct mwifiex_adapter *adapter;
158 mmc_pm_flag_t pm_flag = 0;
161 pm_flag = sdio_get_host_pm_caps(func);
162 card = sdio_get_drvdata(func);
163 if (!card || !card->adapter) {
164 pr_err("resume: invalid card or adapter\n");
168 pr_err("resume: sdio_func is not specified\n");
172 adapter = card->adapter;
174 if (!adapter->is_suspended) {
175 mwifiex_dbg(adapter, WARN,
176 "device already resumed\n");
180 adapter->is_suspended = false;
182 /* Disable Host Sleep */
183 mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
192 * This function removes the interface and frees up the card structure.
195 mwifiex_sdio_remove(struct sdio_func *func)
197 struct sdio_mmc_card *card;
198 struct mwifiex_adapter *adapter;
199 struct mwifiex_private *priv;
201 card = sdio_get_drvdata(func);
205 adapter = card->adapter;
206 if (!adapter || !adapter->priv_num)
209 mwifiex_dbg(adapter, INFO, "info: SDIO func num=%d\n", func->num);
212 if (adapter->is_suspended)
213 mwifiex_sdio_resume(adapter->dev);
215 mwifiex_deauthenticate_all(adapter);
217 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
218 mwifiex_disable_auto_ds(priv);
219 mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
222 mwifiex_remove_card(card->adapter, &add_remove_card_sem);
228 * Kernel needs to suspend all functions separately. Therefore all
229 * registered functions must have drivers with suspend and resume
230 * methods. Failing that the kernel simply removes the whole card.
232 * If already not suspended, this function allocates and sends a host
233 * sleep activate request to the firmware and turns off the traffic.
235 static int mwifiex_sdio_suspend(struct device *dev)
237 struct sdio_func *func = dev_to_sdio_func(dev);
238 struct sdio_mmc_card *card;
239 struct mwifiex_adapter *adapter;
240 mmc_pm_flag_t pm_flag = 0;
244 pm_flag = sdio_get_host_pm_caps(func);
245 pr_debug("cmd: %s: suspend: PM flag = 0x%x\n",
246 sdio_func_id(func), pm_flag);
247 if (!(pm_flag & MMC_PM_KEEP_POWER)) {
248 pr_err("%s: cannot remain alive while host is"
249 " suspended\n", sdio_func_id(func));
253 card = sdio_get_drvdata(func);
254 if (!card || !card->adapter) {
255 pr_err("suspend: invalid card or adapter\n");
259 pr_err("suspend: sdio_func is not specified\n");
263 adapter = card->adapter;
265 /* Enable the Host Sleep */
266 if (!mwifiex_enable_hs(adapter)) {
267 mwifiex_dbg(adapter, ERROR,
268 "cmd: failed to suspend\n");
269 adapter->hs_enabling = false;
273 mwifiex_dbg(adapter, INFO,
274 "cmd: suspend with MMC_PM_KEEP_POWER\n");
275 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
277 /* Indicate device suspended */
278 adapter->is_suspended = true;
279 adapter->hs_enabling = false;
284 /* Device ID for SD8786 */
285 #define SDIO_DEVICE_ID_MARVELL_8786 (0x9116)
286 /* Device ID for SD8787 */
287 #define SDIO_DEVICE_ID_MARVELL_8787 (0x9119)
288 /* Device ID for SD8797 */
289 #define SDIO_DEVICE_ID_MARVELL_8797 (0x9129)
290 /* Device ID for SD8897 */
291 #define SDIO_DEVICE_ID_MARVELL_8897 (0x912d)
292 /* Device ID for SD8887 */
293 #define SDIO_DEVICE_ID_MARVELL_8887 (0x9135)
294 /* Device ID for SD8801 */
295 #define SDIO_DEVICE_ID_MARVELL_8801 (0x9139)
296 /* Device ID for SD8997 */
297 #define SDIO_DEVICE_ID_MARVELL_8997 (0x9141)
301 static const struct sdio_device_id mwifiex_ids[] = {
302 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8786),
303 .driver_data = (unsigned long) &mwifiex_sdio_sd8786},
304 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787),
305 .driver_data = (unsigned long) &mwifiex_sdio_sd8787},
306 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8797),
307 .driver_data = (unsigned long) &mwifiex_sdio_sd8797},
308 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8897),
309 .driver_data = (unsigned long) &mwifiex_sdio_sd8897},
310 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8887),
311 .driver_data = (unsigned long)&mwifiex_sdio_sd8887},
312 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8801),
313 .driver_data = (unsigned long)&mwifiex_sdio_sd8801},
314 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8997),
315 .driver_data = (unsigned long)&mwifiex_sdio_sd8997},
319 MODULE_DEVICE_TABLE(sdio, mwifiex_ids);
321 static const struct dev_pm_ops mwifiex_sdio_pm_ops = {
322 .suspend = mwifiex_sdio_suspend,
323 .resume = mwifiex_sdio_resume,
326 static struct sdio_driver mwifiex_sdio = {
327 .name = "mwifiex_sdio",
328 .id_table = mwifiex_ids,
329 .probe = mwifiex_sdio_probe,
330 .remove = mwifiex_sdio_remove,
332 .owner = THIS_MODULE,
333 .pm = &mwifiex_sdio_pm_ops,
337 /* Write data into SDIO card register. Caller claims SDIO device. */
339 mwifiex_write_reg_locked(struct sdio_func *func, u32 reg, u8 data)
342 sdio_writeb(func, data, reg, &ret);
347 * This function writes data into SDIO card register.
350 mwifiex_write_reg(struct mwifiex_adapter *adapter, u32 reg, u8 data)
352 struct sdio_mmc_card *card = adapter->card;
355 sdio_claim_host(card->func);
356 ret = mwifiex_write_reg_locked(card->func, reg, data);
357 sdio_release_host(card->func);
363 * This function reads data from SDIO card register.
366 mwifiex_read_reg(struct mwifiex_adapter *adapter, u32 reg, u8 *data)
368 struct sdio_mmc_card *card = adapter->card;
372 sdio_claim_host(card->func);
373 val = sdio_readb(card->func, reg, &ret);
374 sdio_release_host(card->func);
382 * This function writes multiple data into SDIO card memory.
384 * This does not work in suspended mode.
387 mwifiex_write_data_sync(struct mwifiex_adapter *adapter,
388 u8 *buffer, u32 pkt_len, u32 port)
390 struct sdio_mmc_card *card = adapter->card;
393 (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE : BLOCK_MODE;
394 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
397 BLOCK_MODE) ? (pkt_len /
398 MWIFIEX_SDIO_BLOCK_SIZE) : pkt_len;
399 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
401 if (adapter->is_suspended) {
402 mwifiex_dbg(adapter, ERROR,
403 "%s: not allowed while suspended\n", __func__);
407 sdio_claim_host(card->func);
409 ret = sdio_writesb(card->func, ioport, buffer, blk_cnt * blk_size);
411 sdio_release_host(card->func);
417 * This function reads multiple data from SDIO card memory.
419 static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *buffer,
420 u32 len, u32 port, u8 claim)
422 struct sdio_mmc_card *card = adapter->card;
424 u8 blk_mode = (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE
426 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
427 u32 blk_cnt = (blk_mode == BLOCK_MODE) ? (len / MWIFIEX_SDIO_BLOCK_SIZE)
429 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
432 sdio_claim_host(card->func);
434 ret = sdio_readsb(card->func, buffer, ioport, blk_cnt * blk_size);
437 sdio_release_host(card->func);
443 * This function wakes up the card.
445 * A host power up command is written to the card configuration
446 * register to wake up the card.
448 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
450 mwifiex_dbg(adapter, EVENT,
451 "event: wakeup device...\n");
453 return mwifiex_write_reg(adapter, CONFIGURATION_REG, HOST_POWER_UP);
457 * This function is called after the card has woken up.
459 * The card configuration register is reset.
461 static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
463 mwifiex_dbg(adapter, EVENT,
464 "cmd: wakeup device completed\n");
466 return mwifiex_write_reg(adapter, CONFIGURATION_REG, 0);
470 * This function is used to initialize IO ports for the
471 * chipsets supporting SDIO new mode eg SD8897.
473 static int mwifiex_init_sdio_new_mode(struct mwifiex_adapter *adapter)
476 struct sdio_mmc_card *card = adapter->card;
478 adapter->ioport = MEM_PORT;
480 /* enable sdio new mode */
481 if (mwifiex_read_reg(adapter, card->reg->card_cfg_2_1_reg, ®))
483 if (mwifiex_write_reg(adapter, card->reg->card_cfg_2_1_reg,
484 reg | CMD53_NEW_MODE))
487 /* Configure cmd port and enable reading rx length from the register */
488 if (mwifiex_read_reg(adapter, card->reg->cmd_cfg_0, ®))
490 if (mwifiex_write_reg(adapter, card->reg->cmd_cfg_0,
491 reg | CMD_PORT_RD_LEN_EN))
494 /* Enable Dnld/Upld ready auto reset for cmd port after cmd53 is
497 if (mwifiex_read_reg(adapter, card->reg->cmd_cfg_1, ®))
499 if (mwifiex_write_reg(adapter, card->reg->cmd_cfg_1,
500 reg | CMD_PORT_AUTO_EN))
506 /* This function initializes the IO ports.
508 * The following operations are performed -
509 * - Read the IO ports (0, 1 and 2)
510 * - Set host interrupt Reset-To-Read to clear
511 * - Set auto re-enable interrupt
513 static int mwifiex_init_sdio_ioport(struct mwifiex_adapter *adapter)
516 struct sdio_mmc_card *card = adapter->card;
520 if (card->supports_sdio_new_mode) {
521 if (mwifiex_init_sdio_new_mode(adapter))
526 /* Read the IO port */
527 if (!mwifiex_read_reg(adapter, card->reg->io_port_0_reg, ®))
528 adapter->ioport |= (reg & 0xff);
532 if (!mwifiex_read_reg(adapter, card->reg->io_port_1_reg, ®))
533 adapter->ioport |= ((reg & 0xff) << 8);
537 if (!mwifiex_read_reg(adapter, card->reg->io_port_2_reg, ®))
538 adapter->ioport |= ((reg & 0xff) << 16);
542 mwifiex_dbg(adapter, INFO,
543 "info: SDIO FUNC1 IO port: %#x\n", adapter->ioport);
545 /* Set Host interrupt reset to read to clear */
546 if (!mwifiex_read_reg(adapter, card->reg->host_int_rsr_reg, ®))
547 mwifiex_write_reg(adapter, card->reg->host_int_rsr_reg,
548 reg | card->reg->sdio_int_mask);
552 /* Dnld/Upld ready set to auto reset */
553 if (!mwifiex_read_reg(adapter, card->reg->card_misc_cfg_reg, ®))
554 mwifiex_write_reg(adapter, card->reg->card_misc_cfg_reg,
555 reg | AUTO_RE_ENABLE_INT);
563 * This function sends data to the card.
565 static int mwifiex_write_data_to_card(struct mwifiex_adapter *adapter,
566 u8 *payload, u32 pkt_len, u32 port)
572 ret = mwifiex_write_data_sync(adapter, payload, pkt_len, port);
575 mwifiex_dbg(adapter, ERROR,
576 "host_to_card, write iomem\t"
577 "(%d) failed: %d\n", i, ret);
578 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
579 mwifiex_dbg(adapter, ERROR,
580 "write CFG reg failed\n");
583 if (i > MAX_WRITE_IOMEM_RETRY)
592 * This function gets the read port.
594 * If control port bit is set in MP read bitmap, the control port
595 * is returned, otherwise the current read port is returned and
596 * the value is increased (provided it does not reach the maximum
597 * limit, in which case it is reset to 1)
599 static int mwifiex_get_rd_port(struct mwifiex_adapter *adapter, u8 *port)
601 struct sdio_mmc_card *card = adapter->card;
602 const struct mwifiex_sdio_card_reg *reg = card->reg;
603 u32 rd_bitmap = card->mp_rd_bitmap;
605 mwifiex_dbg(adapter, DATA,
606 "data: mp_rd_bitmap=0x%08x\n", rd_bitmap);
608 if (card->supports_sdio_new_mode) {
609 if (!(rd_bitmap & reg->data_port_mask))
612 if (!(rd_bitmap & (CTRL_PORT_MASK | reg->data_port_mask)))
616 if ((card->has_control_mask) &&
617 (card->mp_rd_bitmap & CTRL_PORT_MASK)) {
618 card->mp_rd_bitmap &= (u32) (~CTRL_PORT_MASK);
620 mwifiex_dbg(adapter, DATA,
621 "data: port=%d mp_rd_bitmap=0x%08x\n",
622 *port, card->mp_rd_bitmap);
626 if (!(card->mp_rd_bitmap & (1 << card->curr_rd_port)))
629 /* We are now handling the SDIO data ports */
630 card->mp_rd_bitmap &= (u32)(~(1 << card->curr_rd_port));
631 *port = card->curr_rd_port;
633 if (++card->curr_rd_port == card->max_ports)
634 card->curr_rd_port = reg->start_rd_port;
636 mwifiex_dbg(adapter, DATA,
637 "data: port=%d mp_rd_bitmap=0x%08x -> 0x%08x\n",
638 *port, rd_bitmap, card->mp_rd_bitmap);
644 * This function gets the write port for data.
646 * The current write port is returned if available and the value is
647 * increased (provided it does not reach the maximum limit, in which
648 * case it is reset to 1)
650 static int mwifiex_get_wr_port_data(struct mwifiex_adapter *adapter, u32 *port)
652 struct sdio_mmc_card *card = adapter->card;
653 const struct mwifiex_sdio_card_reg *reg = card->reg;
654 u32 wr_bitmap = card->mp_wr_bitmap;
656 mwifiex_dbg(adapter, DATA,
657 "data: mp_wr_bitmap=0x%08x\n", wr_bitmap);
659 if (!(wr_bitmap & card->mp_data_port_mask)) {
660 adapter->data_sent = true;
664 if (card->mp_wr_bitmap & (1 << card->curr_wr_port)) {
665 card->mp_wr_bitmap &= (u32) (~(1 << card->curr_wr_port));
666 *port = card->curr_wr_port;
667 if (++card->curr_wr_port == card->mp_end_port)
668 card->curr_wr_port = reg->start_wr_port;
670 adapter->data_sent = true;
674 if ((card->has_control_mask) && (*port == CTRL_PORT)) {
675 mwifiex_dbg(adapter, ERROR,
676 "invalid data port=%d cur port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
677 *port, card->curr_wr_port, wr_bitmap,
682 mwifiex_dbg(adapter, DATA,
683 "data: port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
684 *port, wr_bitmap, card->mp_wr_bitmap);
690 * This function polls the card status.
693 mwifiex_sdio_poll_card_status(struct mwifiex_adapter *adapter, u8 bits)
695 struct sdio_mmc_card *card = adapter->card;
699 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
700 if (mwifiex_read_reg(adapter, card->reg->poll_reg, &cs))
702 else if ((cs & bits) == bits)
705 usleep_range(10, 20);
708 mwifiex_dbg(adapter, ERROR,
709 "poll card status failed, tries = %d\n", tries);
715 * This function reads the firmware status.
718 mwifiex_sdio_read_fw_status(struct mwifiex_adapter *adapter, u16 *dat)
720 struct sdio_mmc_card *card = adapter->card;
721 const struct mwifiex_sdio_card_reg *reg = card->reg;
724 if (mwifiex_read_reg(adapter, reg->status_reg_0, &fws0))
727 if (mwifiex_read_reg(adapter, reg->status_reg_1, &fws1))
730 *dat = (u16) ((fws1 << 8) | fws0);
736 * This function disables the host interrupt.
738 * The host interrupt mask is read, the disable bit is reset and
739 * written back to the card host interrupt mask register.
741 static void mwifiex_sdio_disable_host_int(struct mwifiex_adapter *adapter)
743 struct sdio_mmc_card *card = adapter->card;
744 struct sdio_func *func = card->func;
746 sdio_claim_host(func);
747 mwifiex_write_reg_locked(func, card->reg->host_int_mask_reg, 0);
748 sdio_release_irq(func);
749 sdio_release_host(func);
753 * This function reads the interrupt status from card.
755 static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
757 struct sdio_mmc_card *card = adapter->card;
761 if (mwifiex_read_data_sync(adapter, card->mp_regs,
762 card->reg->max_mp_regs,
763 REG_PORT | MWIFIEX_SDIO_BYTE_MODE_MASK, 0)) {
764 mwifiex_dbg(adapter, ERROR, "read mp_regs failed\n");
768 sdio_ireg = card->mp_regs[card->reg->host_int_status_reg];
771 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
772 * For SDIO new mode CMD port interrupts
773 * DN_LD_CMD_PORT_HOST_INT_STATUS and/or
774 * UP_LD_CMD_PORT_HOST_INT_STATUS
775 * Clear the interrupt status register
777 mwifiex_dbg(adapter, INTR,
778 "int: sdio_ireg = %#x\n", sdio_ireg);
779 spin_lock_irqsave(&adapter->int_lock, flags);
780 adapter->int_status |= sdio_ireg;
781 spin_unlock_irqrestore(&adapter->int_lock, flags);
786 * SDIO interrupt handler.
788 * This function reads the interrupt status from firmware and handles
789 * the interrupt in current thread (ksdioirqd) right away.
792 mwifiex_sdio_interrupt(struct sdio_func *func)
794 struct mwifiex_adapter *adapter;
795 struct sdio_mmc_card *card;
797 card = sdio_get_drvdata(func);
798 if (!card || !card->adapter) {
799 pr_err("int: func=%p card=%p adapter=%p\n",
800 func, card, card ? card->adapter : NULL);
803 adapter = card->adapter;
805 if (!adapter->pps_uapsd_mode && adapter->ps_state == PS_STATE_SLEEP)
806 adapter->ps_state = PS_STATE_AWAKE;
808 mwifiex_interrupt_status(adapter);
809 mwifiex_main_process(adapter);
813 * This function enables the host interrupt.
815 * The host interrupt enable mask is written to the card
816 * host interrupt mask register.
818 static int mwifiex_sdio_enable_host_int(struct mwifiex_adapter *adapter)
820 struct sdio_mmc_card *card = adapter->card;
821 struct sdio_func *func = card->func;
824 sdio_claim_host(func);
826 /* Request the SDIO IRQ */
827 ret = sdio_claim_irq(func, mwifiex_sdio_interrupt);
829 mwifiex_dbg(adapter, ERROR,
830 "claim irq failed: ret=%d\n", ret);
834 /* Simply write the mask to the register */
835 ret = mwifiex_write_reg_locked(func, card->reg->host_int_mask_reg,
836 card->reg->host_int_enable);
838 mwifiex_dbg(adapter, ERROR,
839 "enable host interrupt failed\n");
840 sdio_release_irq(func);
844 sdio_release_host(func);
849 * This function sends a data buffer to the card.
851 static int mwifiex_sdio_card_to_host(struct mwifiex_adapter *adapter,
852 u32 *type, u8 *buffer,
853 u32 npayload, u32 ioport)
859 mwifiex_dbg(adapter, ERROR,
860 "%s: buffer is NULL\n", __func__);
864 ret = mwifiex_read_data_sync(adapter, buffer, npayload, ioport, 1);
867 mwifiex_dbg(adapter, ERROR,
868 "%s: read iomem failed: %d\n", __func__,
873 nb = le16_to_cpu(*(__le16 *) (buffer));
875 mwifiex_dbg(adapter, ERROR,
876 "%s: invalid packet, nb=%d npayload=%d\n",
877 __func__, nb, npayload);
881 *type = le16_to_cpu(*(__le16 *) (buffer + 2));
887 * This function downloads the firmware to the card.
889 * Firmware is downloaded to the card in blocks. Every block download
890 * is tested for CRC errors, and retried a number of times before
893 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
894 struct mwifiex_fw_image *fw)
896 struct sdio_mmc_card *card = adapter->card;
897 const struct mwifiex_sdio_card_reg *reg = card->reg;
899 u8 *firmware = fw->fw_buf;
900 u32 firmware_len = fw->fw_len;
905 u32 txlen, tx_blocks = 0, tries;
909 mwifiex_dbg(adapter, ERROR,
910 "firmware image not found! Terminating download\n");
914 mwifiex_dbg(adapter, INFO,
915 "info: downloading FW image (%d bytes)\n",
918 /* Assume that the allocated buffer is 8-byte aligned */
919 fwbuf = kzalloc(MWIFIEX_UPLD_SIZE, GFP_KERNEL);
923 sdio_claim_host(card->func);
925 /* Perform firmware data transfer */
927 /* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY
929 ret = mwifiex_sdio_poll_card_status(adapter, CARD_IO_READY |
932 mwifiex_dbg(adapter, ERROR,
933 "FW download with helper:\t"
934 "poll status timeout @ %d\n", offset);
939 if (offset >= firmware_len)
942 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
943 ret = mwifiex_read_reg(adapter, reg->base_0_reg,
946 mwifiex_dbg(adapter, ERROR,
947 "dev BASE0 register read failed:\t"
948 "base0=%#04X(%d). Terminating dnld\n",
952 ret = mwifiex_read_reg(adapter, reg->base_1_reg,
955 mwifiex_dbg(adapter, ERROR,
956 "dev BASE1 register read failed:\t"
957 "base1=%#04X(%d). Terminating dnld\n",
961 len = (u16) (((base1 & 0xff) << 8) | (base0 & 0xff));
966 usleep_range(10, 20);
971 } else if (len > MWIFIEX_UPLD_SIZE) {
972 mwifiex_dbg(adapter, ERROR,
973 "FW dnld failed @ %d, invalid length %d\n",
983 if (i > MAX_WRITE_IOMEM_RETRY) {
984 mwifiex_dbg(adapter, ERROR,
985 "FW dnld failed @ %d, over max retry\n",
990 mwifiex_dbg(adapter, ERROR,
991 "CRC indicated by the helper:\t"
992 "len = 0x%04X, txlen = %d\n", len, txlen);
994 /* Setting this to 0 to resend from same offset */
999 /* Set blocksize to transfer - checking for last
1001 if (firmware_len - offset < txlen)
1002 txlen = firmware_len - offset;
1004 tx_blocks = (txlen + MWIFIEX_SDIO_BLOCK_SIZE - 1)
1005 / MWIFIEX_SDIO_BLOCK_SIZE;
1007 /* Copy payload to buffer */
1008 memmove(fwbuf, &firmware[offset], txlen);
1011 ret = mwifiex_write_data_sync(adapter, fwbuf, tx_blocks *
1012 MWIFIEX_SDIO_BLOCK_SIZE,
1015 mwifiex_dbg(adapter, ERROR,
1016 "FW download, write iomem (%d) failed @ %d\n",
1018 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
1019 mwifiex_dbg(adapter, ERROR,
1020 "write CFG reg failed\n");
1029 sdio_release_host(card->func);
1031 mwifiex_dbg(adapter, MSG,
1032 "info: FW download over, size %d bytes\n", offset);
1041 * This function checks the firmware status in card.
1043 static int mwifiex_check_fw_status(struct mwifiex_adapter *adapter,
1050 for (tries = 0; tries < poll_num; tries++) {
1051 ret = mwifiex_sdio_read_fw_status(adapter, &firmware_stat);
1054 if (firmware_stat == FIRMWARE_READY_SDIO) {
1066 /* This function checks if WLAN is the winner.
1068 static int mwifiex_check_winner_status(struct mwifiex_adapter *adapter)
1072 struct sdio_mmc_card *card = adapter->card;
1074 if (mwifiex_read_reg(adapter, card->reg->status_reg_0, &winner))
1078 adapter->winner = 0;
1080 adapter->winner = 1;
1086 * This function decode sdio aggreation pkt.
1088 * Based on the the data block size and pkt_len,
1089 * skb data will be decoded to few packets.
1091 static void mwifiex_deaggr_sdio_pkt(struct mwifiex_adapter *adapter,
1092 struct sk_buff *skb)
1094 u32 total_pkt_len, pkt_len;
1095 struct sk_buff *skb_deaggr;
1102 total_pkt_len = skb->len;
1104 while (total_pkt_len >= (SDIO_HEADER_OFFSET + INTF_HEADER_LEN)) {
1105 if (total_pkt_len < adapter->sdio_rx_block_size)
1107 blk_num = *(data + BLOCK_NUMBER_OFFSET);
1108 blk_size = adapter->sdio_rx_block_size * blk_num;
1109 if (blk_size > total_pkt_len) {
1110 mwifiex_dbg(adapter, ERROR,
1111 "%s: error in blk_size,\t"
1112 "blk_num=%d, blk_size=%d, total_pkt_len=%d\n",
1113 __func__, blk_num, blk_size, total_pkt_len);
1116 pkt_len = le16_to_cpu(*(__le16 *)(data + SDIO_HEADER_OFFSET));
1117 pkt_type = le16_to_cpu(*(__le16 *)(data + SDIO_HEADER_OFFSET +
1119 if ((pkt_len + SDIO_HEADER_OFFSET) > blk_size) {
1120 mwifiex_dbg(adapter, ERROR,
1121 "%s: error in pkt_len,\t"
1122 "pkt_len=%d, blk_size=%d\n",
1123 __func__, pkt_len, blk_size);
1126 skb_deaggr = mwifiex_alloc_dma_align_buf(pkt_len,
1127 GFP_KERNEL | GFP_DMA);
1130 skb_put(skb_deaggr, pkt_len);
1131 memcpy(skb_deaggr->data, data + SDIO_HEADER_OFFSET, pkt_len);
1132 skb_pull(skb_deaggr, INTF_HEADER_LEN);
1134 mwifiex_handle_rx_packet(adapter, skb_deaggr);
1136 total_pkt_len -= blk_size;
1141 * This function decodes a received packet.
1143 * Based on the type, the packet is treated as either a data, or
1144 * a command response, or an event, and the correct handler
1145 * function is invoked.
1147 static int mwifiex_decode_rx_packet(struct mwifiex_adapter *adapter,
1148 struct sk_buff *skb, u32 upld_typ)
1151 __le16 *curr_ptr = (__le16 *)skb->data;
1152 u16 pkt_len = le16_to_cpu(*curr_ptr);
1153 struct mwifiex_rxinfo *rx_info;
1155 if (upld_typ != MWIFIEX_TYPE_AGGR_DATA) {
1156 skb_trim(skb, pkt_len);
1157 skb_pull(skb, INTF_HEADER_LEN);
1161 case MWIFIEX_TYPE_AGGR_DATA:
1162 mwifiex_dbg(adapter, INFO,
1163 "info: --- Rx: Aggr Data packet ---\n");
1164 rx_info = MWIFIEX_SKB_RXCB(skb);
1165 rx_info->buf_type = MWIFIEX_TYPE_AGGR_DATA;
1166 if (adapter->rx_work_enabled) {
1167 skb_queue_tail(&adapter->rx_data_q, skb);
1168 atomic_inc(&adapter->rx_pending);
1169 adapter->data_received = true;
1171 mwifiex_deaggr_sdio_pkt(adapter, skb);
1172 dev_kfree_skb_any(skb);
1176 case MWIFIEX_TYPE_DATA:
1177 mwifiex_dbg(adapter, DATA,
1178 "info: --- Rx: Data packet ---\n");
1179 if (adapter->rx_work_enabled) {
1180 skb_queue_tail(&adapter->rx_data_q, skb);
1181 adapter->data_received = true;
1182 atomic_inc(&adapter->rx_pending);
1184 mwifiex_handle_rx_packet(adapter, skb);
1188 case MWIFIEX_TYPE_CMD:
1189 mwifiex_dbg(adapter, CMD,
1190 "info: --- Rx: Cmd Response ---\n");
1191 /* take care of curr_cmd = NULL case */
1192 if (!adapter->curr_cmd) {
1193 cmd_buf = adapter->upld_buf;
1195 if (adapter->ps_state == PS_STATE_SLEEP_CFM)
1196 mwifiex_process_sleep_confirm_resp(adapter,
1200 memcpy(cmd_buf, skb->data,
1201 min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER,
1204 dev_kfree_skb_any(skb);
1206 adapter->cmd_resp_received = true;
1207 adapter->curr_cmd->resp_skb = skb;
1211 case MWIFIEX_TYPE_EVENT:
1212 mwifiex_dbg(adapter, EVENT,
1213 "info: --- Rx: Event ---\n");
1214 adapter->event_cause = le32_to_cpu(*(__le32 *) skb->data);
1216 if ((skb->len > 0) && (skb->len < MAX_EVENT_SIZE))
1217 memcpy(adapter->event_body,
1218 skb->data + MWIFIEX_EVENT_HEADER_LEN,
1221 /* event cause has been saved to adapter->event_cause */
1222 adapter->event_received = true;
1223 adapter->event_skb = skb;
1228 mwifiex_dbg(adapter, ERROR,
1229 "unknown upload type %#x\n", upld_typ);
1230 dev_kfree_skb_any(skb);
1238 * This function transfers received packets from card to driver, performing
1239 * aggregation if required.
1241 * For data received on control port, or if aggregation is disabled, the
1242 * received buffers are uploaded as separate packets. However, if aggregation
1243 * is enabled and required, the buffers are copied onto an aggregation buffer,
1244 * provided there is space left, processed and finally uploaded.
1246 static int mwifiex_sdio_card_to_host_mp_aggr(struct mwifiex_adapter *adapter,
1247 u16 rx_len, u8 port)
1249 struct sdio_mmc_card *card = adapter->card;
1250 s32 f_do_rx_aggr = 0;
1251 s32 f_do_rx_cur = 0;
1253 s32 f_post_aggr_cur = 0;
1254 struct sk_buff *skb_deaggr;
1255 struct sk_buff *skb = NULL;
1256 u32 pkt_len, pkt_type, mport, pind;
1259 if ((card->has_control_mask) && (port == CTRL_PORT)) {
1260 /* Read the command Resp without aggr */
1261 mwifiex_dbg(adapter, CMD,
1262 "info: %s: no aggregation for cmd\t"
1263 "response\n", __func__);
1266 goto rx_curr_single;
1269 if (!card->mpa_rx.enabled) {
1270 mwifiex_dbg(adapter, WARN,
1271 "info: %s: rx aggregation disabled\n",
1275 goto rx_curr_single;
1278 if ((!card->has_control_mask && (card->mp_rd_bitmap &
1279 card->reg->data_port_mask)) ||
1280 (card->has_control_mask && (card->mp_rd_bitmap &
1281 (~((u32) CTRL_PORT_MASK))))) {
1282 /* Some more data RX pending */
1283 mwifiex_dbg(adapter, INFO,
1284 "info: %s: not last packet\n", __func__);
1286 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1287 if (MP_RX_AGGR_BUF_HAS_ROOM(card, rx_len)) {
1290 /* No room in Aggr buf, do rx aggr now */
1292 f_post_aggr_cur = 1;
1295 /* Rx aggr not in progress */
1300 /* No more data RX pending */
1301 mwifiex_dbg(adapter, INFO,
1302 "info: %s: last packet\n", __func__);
1304 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1306 if (MP_RX_AGGR_BUF_HAS_ROOM(card, rx_len))
1309 /* No room in Aggr buf, do rx aggr now */
1317 mwifiex_dbg(adapter, INFO,
1318 "info: current packet aggregation\n");
1319 /* Curr pkt can be aggregated */
1320 mp_rx_aggr_setup(card, rx_len, port);
1322 if (MP_RX_AGGR_PKT_LIMIT_REACHED(card) ||
1323 mp_rx_aggr_port_limit_reached(card)) {
1324 mwifiex_dbg(adapter, INFO,
1325 "info: %s: aggregated packet\t"
1326 "limit reached\n", __func__);
1327 /* No more pkts allowed in Aggr buf, rx it */
1333 /* do aggr RX now */
1334 mwifiex_dbg(adapter, DATA,
1335 "info: do_rx_aggr: num of packets: %d\n",
1336 card->mpa_rx.pkt_cnt);
1338 if (card->supports_sdio_new_mode) {
1342 for (i = 0, port_count = 0; i < card->max_ports; i++)
1343 if (card->mpa_rx.ports & BIT(i))
1346 /* Reading data from "start_port + 0" to "start_port +
1347 * port_count -1", so decrease the count by 1
1350 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1351 (port_count << 8)) + card->mpa_rx.start_port;
1353 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1354 (card->mpa_rx.ports << 4)) +
1355 card->mpa_rx.start_port;
1358 if (mwifiex_read_data_sync(adapter, card->mpa_rx.buf,
1359 card->mpa_rx.buf_len, mport, 1))
1362 curr_ptr = card->mpa_rx.buf;
1364 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1365 u32 *len_arr = card->mpa_rx.len_arr;
1367 /* get curr PKT len & type */
1368 pkt_len = le16_to_cpu(*(__le16 *) &curr_ptr[0]);
1369 pkt_type = le16_to_cpu(*(__le16 *) &curr_ptr[2]);
1371 /* copy pkt to deaggr buf */
1372 skb_deaggr = mwifiex_alloc_dma_align_buf(len_arr[pind],
1376 mwifiex_dbg(adapter, ERROR, "skb allocation failure\t"
1377 "drop pkt len=%d type=%d\n",
1379 curr_ptr += len_arr[pind];
1383 skb_put(skb_deaggr, len_arr[pind]);
1385 if ((pkt_type == MWIFIEX_TYPE_DATA ||
1386 (pkt_type == MWIFIEX_TYPE_AGGR_DATA &&
1387 adapter->sdio_rx_aggr_enable)) &&
1388 (pkt_len <= len_arr[pind])) {
1390 memcpy(skb_deaggr->data, curr_ptr, pkt_len);
1392 skb_trim(skb_deaggr, pkt_len);
1394 /* Process de-aggr packet */
1395 mwifiex_decode_rx_packet(adapter, skb_deaggr,
1398 mwifiex_dbg(adapter, ERROR,
1399 "drop wrong aggr pkt:\t"
1400 "sdio_single_port_rx_aggr=%d\t"
1401 "type=%d len=%d max_len=%d\n",
1402 adapter->sdio_rx_aggr_enable,
1403 pkt_type, pkt_len, len_arr[pind]);
1404 dev_kfree_skb_any(skb_deaggr);
1406 curr_ptr += len_arr[pind];
1408 MP_RX_AGGR_BUF_RESET(card);
1413 mwifiex_dbg(adapter, INFO, "info: RX: port: %d, rx_len: %d\n",
1416 skb = mwifiex_alloc_dma_align_buf(rx_len, GFP_KERNEL | GFP_DMA);
1418 mwifiex_dbg(adapter, ERROR,
1419 "single skb allocated fail,\t"
1420 "drop pkt port=%d len=%d\n", port, rx_len);
1421 if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1422 card->mpa_rx.buf, rx_len,
1423 adapter->ioport + port))
1428 skb_put(skb, rx_len);
1430 if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1431 skb->data, skb->len,
1432 adapter->ioport + port))
1434 if (!adapter->sdio_rx_aggr_enable &&
1435 pkt_type == MWIFIEX_TYPE_AGGR_DATA) {
1436 mwifiex_dbg(adapter, ERROR, "drop wrong pkt type %d\t"
1437 "current SDIO RX Aggr not enabled\n",
1439 dev_kfree_skb_any(skb);
1443 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1445 if (f_post_aggr_cur) {
1446 mwifiex_dbg(adapter, INFO,
1447 "info: current packet aggregation\n");
1448 /* Curr pkt can be aggregated */
1449 mp_rx_aggr_setup(card, rx_len, port);
1454 if (MP_RX_AGGR_IN_PROGRESS(card))
1455 MP_RX_AGGR_BUF_RESET(card);
1457 if (f_do_rx_cur && skb)
1458 /* Single transfer pending. Free curr buff also */
1459 dev_kfree_skb_any(skb);
1465 * This function checks the current interrupt status.
1467 * The following interrupts are checked and handled by this function -
1470 * - Packets received
1472 * Since the firmware does not generate download ready interrupt if the
1473 * port updated is command port only, command sent interrupt checking
1474 * should be done manually, and for every SDIO interrupt.
1476 * In case of Rx packets received, the packets are uploaded from card to
1477 * host and processed accordingly.
1479 static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
1481 struct sdio_mmc_card *card = adapter->card;
1482 const struct mwifiex_sdio_card_reg *reg = card->reg;
1485 struct sk_buff *skb;
1486 u8 port = CTRL_PORT;
1487 u32 len_reg_l, len_reg_u;
1490 unsigned long flags;
1494 spin_lock_irqsave(&adapter->int_lock, flags);
1495 sdio_ireg = adapter->int_status;
1496 adapter->int_status = 0;
1497 spin_unlock_irqrestore(&adapter->int_lock, flags);
1502 /* Following interrupt is only for SDIO new mode */
1503 if (sdio_ireg & DN_LD_CMD_PORT_HOST_INT_STATUS && adapter->cmd_sent)
1504 adapter->cmd_sent = false;
1506 /* Following interrupt is only for SDIO new mode */
1507 if (sdio_ireg & UP_LD_CMD_PORT_HOST_INT_STATUS) {
1510 /* read the len of control packet */
1511 rx_len = card->mp_regs[reg->cmd_rd_len_1] << 8;
1512 rx_len |= (u16)card->mp_regs[reg->cmd_rd_len_0];
1513 rx_blocks = DIV_ROUND_UP(rx_len, MWIFIEX_SDIO_BLOCK_SIZE);
1514 if (rx_len <= INTF_HEADER_LEN ||
1515 (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1516 MWIFIEX_RX_DATA_BUF_SIZE)
1518 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1519 mwifiex_dbg(adapter, INFO, "info: rx_len = %d\n", rx_len);
1521 skb = mwifiex_alloc_dma_align_buf(rx_len, GFP_KERNEL | GFP_DMA);
1525 skb_put(skb, rx_len);
1527 if (mwifiex_sdio_card_to_host(adapter, &pkt_type, skb->data,
1528 skb->len, adapter->ioport |
1530 mwifiex_dbg(adapter, ERROR,
1531 "%s: failed to card_to_host", __func__);
1532 dev_kfree_skb_any(skb);
1536 if ((pkt_type != MWIFIEX_TYPE_CMD) &&
1537 (pkt_type != MWIFIEX_TYPE_EVENT))
1538 mwifiex_dbg(adapter, ERROR,
1539 "%s:Received wrong packet on cmd port",
1542 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1545 if (sdio_ireg & DN_LD_HOST_INT_STATUS) {
1546 bitmap = (u32) card->mp_regs[reg->wr_bitmap_l];
1547 bitmap |= ((u32) card->mp_regs[reg->wr_bitmap_u]) << 8;
1548 if (card->supports_sdio_new_mode) {
1550 ((u32) card->mp_regs[reg->wr_bitmap_1l]) << 16;
1552 ((u32) card->mp_regs[reg->wr_bitmap_1u]) << 24;
1554 card->mp_wr_bitmap = bitmap;
1556 mwifiex_dbg(adapter, INTR,
1557 "int: DNLD: wr_bitmap=0x%x\n",
1558 card->mp_wr_bitmap);
1559 if (adapter->data_sent &&
1560 (card->mp_wr_bitmap & card->mp_data_port_mask)) {
1561 mwifiex_dbg(adapter, INTR,
1562 "info: <--- Tx DONE Interrupt --->\n");
1563 adapter->data_sent = false;
1567 /* As firmware will not generate download ready interrupt if the port
1568 updated is command port only, cmd_sent should be done for any SDIO
1570 if (card->has_control_mask && adapter->cmd_sent) {
1571 /* Check if firmware has attach buffer at command port and
1572 update just that in wr_bit_map. */
1573 card->mp_wr_bitmap |=
1574 (u32) card->mp_regs[reg->wr_bitmap_l] & CTRL_PORT_MASK;
1575 if (card->mp_wr_bitmap & CTRL_PORT_MASK)
1576 adapter->cmd_sent = false;
1579 mwifiex_dbg(adapter, INTR, "info: cmd_sent=%d data_sent=%d\n",
1580 adapter->cmd_sent, adapter->data_sent);
1581 if (sdio_ireg & UP_LD_HOST_INT_STATUS) {
1582 bitmap = (u32) card->mp_regs[reg->rd_bitmap_l];
1583 bitmap |= ((u32) card->mp_regs[reg->rd_bitmap_u]) << 8;
1584 if (card->supports_sdio_new_mode) {
1586 ((u32) card->mp_regs[reg->rd_bitmap_1l]) << 16;
1588 ((u32) card->mp_regs[reg->rd_bitmap_1u]) << 24;
1590 card->mp_rd_bitmap = bitmap;
1591 mwifiex_dbg(adapter, INTR,
1592 "int: UPLD: rd_bitmap=0x%x\n",
1593 card->mp_rd_bitmap);
1596 ret = mwifiex_get_rd_port(adapter, &port);
1598 mwifiex_dbg(adapter, INFO,
1599 "info: no more rd_port available\n");
1602 len_reg_l = reg->rd_len_p0_l + (port << 1);
1603 len_reg_u = reg->rd_len_p0_u + (port << 1);
1604 rx_len = ((u16) card->mp_regs[len_reg_u]) << 8;
1605 rx_len |= (u16) card->mp_regs[len_reg_l];
1606 mwifiex_dbg(adapter, INFO,
1607 "info: RX: port=%d rx_len=%u\n",
1610 (rx_len + MWIFIEX_SDIO_BLOCK_SIZE -
1611 1) / MWIFIEX_SDIO_BLOCK_SIZE;
1612 if (rx_len <= INTF_HEADER_LEN ||
1613 (card->mpa_rx.enabled &&
1614 ((rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1615 card->mpa_rx.buf_size))) {
1616 mwifiex_dbg(adapter, ERROR,
1617 "invalid rx_len=%d\n",
1622 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1623 mwifiex_dbg(adapter, INFO, "info: rx_len = %d\n",
1626 if (mwifiex_sdio_card_to_host_mp_aggr(adapter, rx_len,
1628 mwifiex_dbg(adapter, ERROR,
1629 "card_to_host_mpa failed: int status=%#x\n",
1640 if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
1641 mwifiex_dbg(adapter, ERROR, "read CFG reg failed\n");
1643 mwifiex_dbg(adapter, INFO,
1644 "info: CFG reg val = %d\n", cr);
1646 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, (cr | 0x04)))
1647 mwifiex_dbg(adapter, ERROR,
1648 "write CFG reg failed\n");
1650 mwifiex_dbg(adapter, INFO, "info: write success\n");
1652 if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
1653 mwifiex_dbg(adapter, ERROR,
1654 "read CFG reg failed\n");
1656 mwifiex_dbg(adapter, INFO,
1657 "info: CFG reg val =%x\n", cr);
1663 * This function aggregates transmission buffers in driver and downloads
1664 * the aggregated packet to card.
1666 * The individual packets are aggregated by copying into an aggregation
1667 * buffer and then downloaded to the card. Previous unsent packets in the
1668 * aggregation buffer are pre-copied first before new packets are added.
1669 * Aggregation is done till there is space left in the aggregation buffer,
1670 * or till new packets are available.
1672 * The function will only download the packet to the card when aggregation
1673 * stops, otherwise it will just aggregate the packet in aggregation buffer
1676 static int mwifiex_host_to_card_mp_aggr(struct mwifiex_adapter *adapter,
1677 u8 *payload, u32 pkt_len, u32 port,
1680 struct sdio_mmc_card *card = adapter->card;
1682 s32 f_send_aggr_buf = 0;
1683 s32 f_send_cur_buf = 0;
1684 s32 f_precopy_cur_buf = 0;
1685 s32 f_postcopy_cur_buf = 0;
1688 if (!card->mpa_tx.enabled ||
1689 (card->has_control_mask && (port == CTRL_PORT)) ||
1690 (card->supports_sdio_new_mode && (port == CMD_PORT_SLCT))) {
1691 mwifiex_dbg(adapter, WARN,
1692 "info: %s: tx aggregation disabled\n",
1696 goto tx_curr_single;
1700 /* More pkt in TX queue */
1701 mwifiex_dbg(adapter, INFO,
1702 "info: %s: more packets in queue.\n",
1705 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1706 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len)) {
1707 f_precopy_cur_buf = 1;
1709 if (!(card->mp_wr_bitmap &
1710 (1 << card->curr_wr_port)) ||
1711 !MP_TX_AGGR_BUF_HAS_ROOM(
1712 card, pkt_len + next_pkt_len))
1713 f_send_aggr_buf = 1;
1715 /* No room in Aggr buf, send it */
1716 f_send_aggr_buf = 1;
1718 if (!(card->mp_wr_bitmap &
1719 (1 << card->curr_wr_port)))
1722 f_postcopy_cur_buf = 1;
1725 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len) &&
1726 (card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1727 f_precopy_cur_buf = 1;
1732 /* Last pkt in TX queue */
1733 mwifiex_dbg(adapter, INFO,
1734 "info: %s: Last packet in Tx Queue.\n",
1737 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1738 /* some packs in Aggr buf already */
1739 f_send_aggr_buf = 1;
1741 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len))
1742 f_precopy_cur_buf = 1;
1744 /* No room in Aggr buf, send it */
1751 if (f_precopy_cur_buf) {
1752 mwifiex_dbg(adapter, DATA,
1753 "data: %s: precopy current buffer\n",
1755 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1757 if (MP_TX_AGGR_PKT_LIMIT_REACHED(card) ||
1758 mp_tx_aggr_port_limit_reached(card))
1759 /* No more pkts allowed in Aggr buf, send it */
1760 f_send_aggr_buf = 1;
1763 if (f_send_aggr_buf) {
1764 mwifiex_dbg(adapter, DATA,
1765 "data: %s: send aggr buffer: %d %d\n",
1766 __func__, card->mpa_tx.start_port,
1767 card->mpa_tx.ports);
1768 if (card->supports_sdio_new_mode) {
1772 for (i = 0, port_count = 0; i < card->max_ports; i++)
1773 if (card->mpa_tx.ports & BIT(i))
1776 /* Writing data from "start_port + 0" to "start_port +
1777 * port_count -1", so decrease the count by 1
1780 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1781 (port_count << 8)) + card->mpa_tx.start_port;
1783 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1784 (card->mpa_tx.ports << 4)) +
1785 card->mpa_tx.start_port;
1788 ret = mwifiex_write_data_to_card(adapter, card->mpa_tx.buf,
1789 card->mpa_tx.buf_len, mport);
1791 MP_TX_AGGR_BUF_RESET(card);
1795 if (f_send_cur_buf) {
1796 mwifiex_dbg(adapter, DATA,
1797 "data: %s: send current buffer %d\n",
1799 ret = mwifiex_write_data_to_card(adapter, payload, pkt_len,
1800 adapter->ioport + port);
1803 if (f_postcopy_cur_buf) {
1804 mwifiex_dbg(adapter, DATA,
1805 "data: %s: postcopy current buffer\n",
1807 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1814 * This function downloads data from driver to card.
1816 * Both commands and data packets are transferred to the card by this
1819 * This function adds the SDIO specific header to the front of the buffer
1820 * before transferring. The header contains the length of the packet and
1821 * the type. The firmware handles the packets based upon this set type.
1823 static int mwifiex_sdio_host_to_card(struct mwifiex_adapter *adapter,
1824 u8 type, struct sk_buff *skb,
1825 struct mwifiex_tx_param *tx_param)
1827 struct sdio_mmc_card *card = adapter->card;
1831 u32 port = CTRL_PORT;
1832 u8 *payload = (u8 *)skb->data;
1833 u32 pkt_len = skb->len;
1835 /* Allocate buffer and copy payload */
1836 blk_size = MWIFIEX_SDIO_BLOCK_SIZE;
1837 buf_block_len = (pkt_len + blk_size - 1) / blk_size;
1838 *(__le16 *)&payload[0] = cpu_to_le16((u16)pkt_len);
1839 *(__le16 *)&payload[2] = cpu_to_le16(type);
1842 * This is SDIO specific header
1844 * u16 type (MWIFIEX_TYPE_DATA = 0, MWIFIEX_TYPE_CMD = 1,
1845 * MWIFIEX_TYPE_EVENT = 3)
1847 if (type == MWIFIEX_TYPE_DATA) {
1848 ret = mwifiex_get_wr_port_data(adapter, &port);
1850 mwifiex_dbg(adapter, ERROR,
1851 "%s: no wr_port available\n",
1856 adapter->cmd_sent = true;
1857 /* Type must be MWIFIEX_TYPE_CMD */
1859 if (pkt_len <= INTF_HEADER_LEN ||
1860 pkt_len > MWIFIEX_UPLD_SIZE)
1861 mwifiex_dbg(adapter, ERROR,
1862 "%s: payload=%p, nb=%d\n",
1863 __func__, payload, pkt_len);
1865 if (card->supports_sdio_new_mode)
1866 port = CMD_PORT_SLCT;
1869 /* Transfer data to card */
1870 pkt_len = buf_block_len * blk_size;
1873 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1874 port, tx_param->next_pkt_len
1877 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1881 if (type == MWIFIEX_TYPE_CMD)
1882 adapter->cmd_sent = false;
1883 if (type == MWIFIEX_TYPE_DATA) {
1884 adapter->data_sent = false;
1885 /* restore curr_wr_port in error cases */
1886 card->curr_wr_port = port;
1887 card->mp_wr_bitmap |= (u32)(1 << card->curr_wr_port);
1890 if (type == MWIFIEX_TYPE_DATA) {
1891 if (!(card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1892 adapter->data_sent = true;
1894 adapter->data_sent = false;
1902 * This function allocates the MPA Tx and Rx buffers.
1904 static int mwifiex_alloc_sdio_mpa_buffers(struct mwifiex_adapter *adapter,
1905 u32 mpa_tx_buf_size, u32 mpa_rx_buf_size)
1907 struct sdio_mmc_card *card = adapter->card;
1911 card->mpa_tx.buf = kzalloc(mpa_tx_buf_size, GFP_KERNEL);
1912 if (!card->mpa_tx.buf) {
1917 card->mpa_tx.buf_size = mpa_tx_buf_size;
1919 rx_buf_size = max_t(u32, mpa_rx_buf_size,
1920 (u32)SDIO_MAX_AGGR_BUF_SIZE);
1921 card->mpa_rx.buf = kzalloc(rx_buf_size, GFP_KERNEL);
1922 if (!card->mpa_rx.buf) {
1927 card->mpa_rx.buf_size = rx_buf_size;
1931 kfree(card->mpa_tx.buf);
1932 kfree(card->mpa_rx.buf);
1933 card->mpa_tx.buf_size = 0;
1934 card->mpa_rx.buf_size = 0;
1941 * This function unregisters the SDIO device.
1943 * The SDIO IRQ is released, the function is disabled and driver
1944 * data is set to null.
1947 mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
1949 struct sdio_mmc_card *card = adapter->card;
1951 if (adapter->card) {
1952 sdio_claim_host(card->func);
1953 sdio_disable_func(card->func);
1954 sdio_release_host(card->func);
1959 * This function registers the SDIO device.
1961 * SDIO IRQ is claimed, block size is set and driver data is initialized.
1963 static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
1966 struct sdio_mmc_card *card = adapter->card;
1967 struct sdio_func *func = card->func;
1969 /* save adapter pointer in card */
1970 card->adapter = adapter;
1971 adapter->tx_buf_size = card->tx_buf_size;
1973 sdio_claim_host(func);
1975 /* Set block size */
1976 ret = sdio_set_block_size(card->func, MWIFIEX_SDIO_BLOCK_SIZE);
1977 sdio_release_host(func);
1979 mwifiex_dbg(adapter, ERROR,
1980 "cannot set SDIO block size\n");
1985 adapter->dev = &func->dev;
1987 strcpy(adapter->fw_name, card->firmware);
1988 if (card->fw_dump_enh) {
1989 adapter->mem_type_mapping_tbl = generic_mem_type_map;
1990 adapter->num_mem_types = 1;
1992 adapter->mem_type_mapping_tbl = mem_type_mapping_tbl;
1993 adapter->num_mem_types = ARRAY_SIZE(mem_type_mapping_tbl);
2000 * This function initializes the SDIO driver.
2002 * The following initializations steps are followed -
2003 * - Read the Host interrupt status register to acknowledge
2004 * the first interrupt got from bootloader
2005 * - Disable host interrupt mask register
2007 * - Initialize SDIO variables in card
2008 * - Allocate MP registers
2009 * - Allocate MPA Tx and Rx buffers
2011 static int mwifiex_init_sdio(struct mwifiex_adapter *adapter)
2013 struct sdio_mmc_card *card = adapter->card;
2014 const struct mwifiex_sdio_card_reg *reg = card->reg;
2018 sdio_set_drvdata(card->func, card);
2021 * Read the host_int_status_reg for ACK the first interrupt got
2022 * from the bootloader. If we don't do this we get a interrupt
2023 * as soon as we register the irq.
2025 mwifiex_read_reg(adapter, card->reg->host_int_status_reg, &sdio_ireg);
2027 /* Get SDIO ioport */
2028 mwifiex_init_sdio_ioport(adapter);
2030 /* Initialize SDIO variables in card */
2031 card->mp_rd_bitmap = 0;
2032 card->mp_wr_bitmap = 0;
2033 card->curr_rd_port = reg->start_rd_port;
2034 card->curr_wr_port = reg->start_wr_port;
2036 card->mp_data_port_mask = reg->data_port_mask;
2038 card->mpa_tx.buf_len = 0;
2039 card->mpa_tx.pkt_cnt = 0;
2040 card->mpa_tx.start_port = 0;
2042 card->mpa_tx.enabled = 1;
2043 card->mpa_tx.pkt_aggr_limit = card->mp_agg_pkt_limit;
2045 card->mpa_rx.buf_len = 0;
2046 card->mpa_rx.pkt_cnt = 0;
2047 card->mpa_rx.start_port = 0;
2049 card->mpa_rx.enabled = 1;
2050 card->mpa_rx.pkt_aggr_limit = card->mp_agg_pkt_limit;
2052 /* Allocate buffers for SDIO MP-A */
2053 card->mp_regs = kzalloc(reg->max_mp_regs, GFP_KERNEL);
2057 /* Allocate skb pointer buffers */
2058 card->mpa_rx.skb_arr = kzalloc((sizeof(void *)) *
2059 card->mp_agg_pkt_limit, GFP_KERNEL);
2060 if (!card->mpa_rx.skb_arr) {
2061 kfree(card->mp_regs);
2065 card->mpa_rx.len_arr = kzalloc(sizeof(*card->mpa_rx.len_arr) *
2066 card->mp_agg_pkt_limit, GFP_KERNEL);
2067 if (!card->mpa_rx.len_arr) {
2068 kfree(card->mp_regs);
2069 kfree(card->mpa_rx.skb_arr);
2073 ret = mwifiex_alloc_sdio_mpa_buffers(adapter,
2074 card->mp_tx_agg_buf_size,
2075 card->mp_rx_agg_buf_size);
2077 /* Allocate 32k MPA Tx/Rx buffers if 64k memory allocation fails */
2078 if (ret && (card->mp_tx_agg_buf_size == MWIFIEX_MP_AGGR_BUF_SIZE_MAX ||
2079 card->mp_rx_agg_buf_size == MWIFIEX_MP_AGGR_BUF_SIZE_MAX)) {
2080 /* Disable rx single port aggregation */
2081 adapter->host_disable_sdio_rx_aggr = true;
2083 ret = mwifiex_alloc_sdio_mpa_buffers
2084 (adapter, MWIFIEX_MP_AGGR_BUF_SIZE_32K,
2085 MWIFIEX_MP_AGGR_BUF_SIZE_32K);
2087 /* Disable multi port aggregation */
2088 card->mpa_tx.enabled = 0;
2089 card->mpa_rx.enabled = 0;
2093 adapter->auto_tdls = card->can_auto_tdls;
2094 adapter->ext_scan = card->can_ext_scan;
2099 * This function resets the MPA Tx and Rx buffers.
2101 static void mwifiex_cleanup_mpa_buf(struct mwifiex_adapter *adapter)
2103 struct sdio_mmc_card *card = adapter->card;
2105 MP_TX_AGGR_BUF_RESET(card);
2106 MP_RX_AGGR_BUF_RESET(card);
2110 * This function cleans up the allocated card buffers.
2112 * The following are freed by this function -
2117 static void mwifiex_cleanup_sdio(struct mwifiex_adapter *adapter)
2119 struct sdio_mmc_card *card = adapter->card;
2121 kfree(card->mp_regs);
2122 kfree(card->mpa_rx.skb_arr);
2123 kfree(card->mpa_rx.len_arr);
2124 kfree(card->mpa_tx.buf);
2125 kfree(card->mpa_rx.buf);
2126 sdio_set_drvdata(card->func, NULL);
2131 * This function updates the MP end port in card.
2134 mwifiex_update_mp_end_port(struct mwifiex_adapter *adapter, u16 port)
2136 struct sdio_mmc_card *card = adapter->card;
2137 const struct mwifiex_sdio_card_reg *reg = card->reg;
2140 card->mp_end_port = port;
2142 card->mp_data_port_mask = reg->data_port_mask;
2144 if (reg->start_wr_port) {
2145 for (i = 1; i <= card->max_ports - card->mp_end_port; i++)
2146 card->mp_data_port_mask &=
2147 ~(1 << (card->max_ports - i));
2150 card->curr_wr_port = reg->start_wr_port;
2152 mwifiex_dbg(adapter, CMD,
2153 "cmd: mp_end_port %d, data port mask 0x%x\n",
2154 port, card->mp_data_port_mask);
2157 static void mwifiex_recreate_adapter(struct sdio_mmc_card *card)
2159 struct sdio_func *func = card->func;
2160 const struct sdio_device_id *device_id = card->device_id;
2162 /* TODO mmc_hw_reset does not require destroying and re-probing the
2163 * whole adapter. Hence there was no need to for this rube-goldberg
2164 * design to reload the fw from an external workqueue. If we don't
2165 * destroy the adapter we could reload the fw from
2166 * mwifiex_main_work_queue directly.
2167 * The real difficulty with fw reset is to restore all the user
2168 * settings applied through ioctl. By destroying and recreating the
2169 * adapter, we take the easy way out, since we rely on user space to
2170 * restore them. We assume that user space will treat the new
2171 * incarnation of the adapter(interfaces) as if they had been just
2172 * discovered and initializes them from scratch.
2175 mwifiex_sdio_remove(func);
2177 /* power cycle the adapter */
2178 sdio_claim_host(func);
2179 mmc_hw_reset(func->card->host);
2180 sdio_release_host(func);
2182 mwifiex_sdio_probe(func, device_id);
2185 static struct mwifiex_adapter *save_adapter;
2186 static void mwifiex_sdio_card_reset_work(struct mwifiex_adapter *adapter)
2188 struct sdio_mmc_card *card = adapter->card;
2190 /* TODO card pointer is unprotected. If the adapter is removed
2191 * physically, sdio core might trigger mwifiex_sdio_remove, before this
2192 * workqueue is run, which will destroy the adapter struct. When this
2193 * workqueue eventually exceutes it will dereference an invalid adapter
2196 mwifiex_recreate_adapter(card);
2199 /* This function read/write firmware */
2201 rdwr_status mwifiex_sdio_rdwr_firmware(struct mwifiex_adapter *adapter,
2204 struct sdio_mmc_card *card = adapter->card;
2208 sdio_writeb(card->func, card->reg->fw_dump_host_ready,
2209 card->reg->fw_dump_ctrl, &ret);
2211 mwifiex_dbg(adapter, ERROR, "SDIO Write ERR\n");
2212 return RDWR_STATUS_FAILURE;
2214 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
2215 ctrl_data = sdio_readb(card->func, card->reg->fw_dump_ctrl,
2218 mwifiex_dbg(adapter, ERROR, "SDIO read err\n");
2219 return RDWR_STATUS_FAILURE;
2221 if (ctrl_data == FW_DUMP_DONE)
2223 if (doneflag && ctrl_data == doneflag)
2224 return RDWR_STATUS_DONE;
2225 if (ctrl_data != card->reg->fw_dump_host_ready) {
2226 mwifiex_dbg(adapter, WARN,
2227 "The ctrl reg was changed, re-try again\n");
2228 sdio_writeb(card->func, card->reg->fw_dump_host_ready,
2229 card->reg->fw_dump_ctrl, &ret);
2231 mwifiex_dbg(adapter, ERROR, "SDIO write err\n");
2232 return RDWR_STATUS_FAILURE;
2235 usleep_range(100, 200);
2237 if (ctrl_data == card->reg->fw_dump_host_ready) {
2238 mwifiex_dbg(adapter, ERROR,
2239 "Fail to pull ctrl_data\n");
2240 return RDWR_STATUS_FAILURE;
2243 return RDWR_STATUS_SUCCESS;
2246 /* This function dump firmware memory to file */
2247 static void mwifiex_sdio_fw_dump(struct mwifiex_adapter *adapter)
2249 struct sdio_mmc_card *card = adapter->card;
2251 unsigned int reg, reg_start, reg_end;
2252 u8 *dbg_ptr, *end_ptr, dump_num, idx, i, read_reg, doneflag = 0;
2253 enum rdwr_status stat;
2256 if (!card->can_dump_fw)
2259 for (idx = 0; idx < ARRAY_SIZE(mem_type_mapping_tbl); idx++) {
2260 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
2262 if (entry->mem_ptr) {
2263 vfree(entry->mem_ptr);
2264 entry->mem_ptr = NULL;
2266 entry->mem_size = 0;
2269 mwifiex_pm_wakeup_card(adapter);
2270 sdio_claim_host(card->func);
2272 mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump start ==\n");
2274 stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2275 if (stat == RDWR_STATUS_FAILURE)
2278 reg = card->reg->fw_dump_start;
2279 /* Read the number of the memories which will dump */
2280 dump_num = sdio_readb(card->func, reg, &ret);
2282 mwifiex_dbg(adapter, ERROR, "SDIO read memory length err\n");
2286 /* Read the length of every memory which will dump */
2287 for (idx = 0; idx < dump_num; idx++) {
2288 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
2290 stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2291 if (stat == RDWR_STATUS_FAILURE)
2295 reg = card->reg->fw_dump_start;
2296 for (i = 0; i < 4; i++) {
2297 read_reg = sdio_readb(card->func, reg, &ret);
2299 mwifiex_dbg(adapter, ERROR, "SDIO read err\n");
2302 memory_size |= (read_reg << i*8);
2306 if (memory_size == 0) {
2307 mwifiex_dbg(adapter, DUMP, "Firmware dump Finished!\n");
2308 ret = mwifiex_write_reg(adapter,
2309 card->reg->fw_dump_ctrl,
2312 mwifiex_dbg(adapter, ERROR, "SDIO write err\n");
2318 mwifiex_dbg(adapter, DUMP,
2319 "%s_SIZE=0x%x\n", entry->mem_name, memory_size);
2320 entry->mem_ptr = vmalloc(memory_size + 1);
2321 entry->mem_size = memory_size;
2322 if (!entry->mem_ptr) {
2323 mwifiex_dbg(adapter, ERROR, "Vmalloc %s failed\n",
2327 dbg_ptr = entry->mem_ptr;
2328 end_ptr = dbg_ptr + memory_size;
2330 doneflag = entry->done_flag;
2331 mwifiex_dbg(adapter, DUMP,
2332 "Start %s output, please wait...\n",
2336 stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2337 if (stat == RDWR_STATUS_FAILURE)
2340 reg_start = card->reg->fw_dump_start;
2341 reg_end = card->reg->fw_dump_end;
2342 for (reg = reg_start; reg <= reg_end; reg++) {
2343 *dbg_ptr = sdio_readb(card->func, reg, &ret);
2345 mwifiex_dbg(adapter, ERROR,
2349 if (dbg_ptr < end_ptr)
2352 mwifiex_dbg(adapter, ERROR,
2353 "Allocated buf not enough\n");
2356 if (stat != RDWR_STATUS_DONE)
2359 mwifiex_dbg(adapter, DUMP, "%s done: size=0x%tx\n",
2360 entry->mem_name, dbg_ptr - entry->mem_ptr);
2364 mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump end ==\n");
2367 sdio_release_host(card->func);
2370 static void mwifiex_sdio_generic_fw_dump(struct mwifiex_adapter *adapter)
2372 struct sdio_mmc_card *card = adapter->card;
2373 struct memory_type_mapping *entry = &generic_mem_type_map[0];
2374 unsigned int reg, reg_start, reg_end;
2375 u8 start_flag = 0, done_flag = 0;
2376 u8 *dbg_ptr, *end_ptr;
2377 enum rdwr_status stat;
2378 int ret = -1, tries;
2380 if (!card->fw_dump_enh)
2383 if (entry->mem_ptr) {
2384 vfree(entry->mem_ptr);
2385 entry->mem_ptr = NULL;
2387 entry->mem_size = 0;
2389 mwifiex_pm_wakeup_card(adapter);
2390 sdio_claim_host(card->func);
2392 mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump start ==\n");
2394 stat = mwifiex_sdio_rdwr_firmware(adapter, done_flag);
2395 if (stat == RDWR_STATUS_FAILURE)
2398 reg_start = card->reg->fw_dump_start;
2399 reg_end = card->reg->fw_dump_end;
2400 for (reg = reg_start; reg <= reg_end; reg++) {
2401 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
2402 start_flag = sdio_readb(card->func, reg, &ret);
2404 mwifiex_dbg(adapter, ERROR,
2408 if (start_flag == 0)
2410 if (tries == MAX_POLL_TRIES) {
2411 mwifiex_dbg(adapter, ERROR,
2412 "FW not ready to dump\n");
2417 usleep_range(100, 200);
2420 entry->mem_ptr = vmalloc(0xf0000 + 1);
2421 if (!entry->mem_ptr) {
2425 dbg_ptr = entry->mem_ptr;
2426 entry->mem_size = 0xf0000;
2427 end_ptr = dbg_ptr + entry->mem_size;
2429 done_flag = entry->done_flag;
2430 mwifiex_dbg(adapter, DUMP,
2431 "Start %s output, please wait...\n", entry->mem_name);
2434 stat = mwifiex_sdio_rdwr_firmware(adapter, done_flag);
2435 if (stat == RDWR_STATUS_FAILURE)
2437 for (reg = reg_start; reg <= reg_end; reg++) {
2438 *dbg_ptr = sdio_readb(card->func, reg, &ret);
2440 mwifiex_dbg(adapter, ERROR,
2445 if (dbg_ptr >= end_ptr) {
2448 tmp_ptr = vmalloc(entry->mem_size + 0x4000 + 1);
2452 memcpy(tmp_ptr, entry->mem_ptr,
2454 vfree(entry->mem_ptr);
2455 entry->mem_ptr = tmp_ptr;
2457 dbg_ptr = entry->mem_ptr + entry->mem_size;
2458 entry->mem_size += 0x4000;
2459 end_ptr = entry->mem_ptr + entry->mem_size;
2462 if (stat == RDWR_STATUS_DONE) {
2463 entry->mem_size = dbg_ptr - entry->mem_ptr;
2464 mwifiex_dbg(adapter, DUMP, "dump %s done size=0x%x\n",
2465 entry->mem_name, entry->mem_size);
2470 mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump end ==\n");
2474 mwifiex_dbg(adapter, ERROR, "firmware dump failed\n");
2475 if (entry->mem_ptr) {
2476 vfree(entry->mem_ptr);
2477 entry->mem_ptr = NULL;
2479 entry->mem_size = 0;
2481 sdio_release_host(card->func);
2484 static void mwifiex_sdio_device_dump_work(struct mwifiex_adapter *adapter)
2486 struct sdio_mmc_card *card = adapter->card;
2488 mwifiex_drv_info_dump(adapter);
2489 if (card->fw_dump_enh)
2490 mwifiex_sdio_generic_fw_dump(adapter);
2492 mwifiex_sdio_fw_dump(adapter);
2493 mwifiex_upload_device_dump(adapter);
2496 static void mwifiex_sdio_work(struct work_struct *work)
2498 if (test_and_clear_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP,
2500 mwifiex_sdio_device_dump_work(save_adapter);
2501 if (test_and_clear_bit(MWIFIEX_IFACE_WORK_CARD_RESET,
2503 mwifiex_sdio_card_reset_work(save_adapter);
2506 static DECLARE_WORK(sdio_work, mwifiex_sdio_work);
2507 /* This function resets the card */
2508 static void mwifiex_sdio_card_reset(struct mwifiex_adapter *adapter)
2510 save_adapter = adapter;
2511 if (test_bit(MWIFIEX_IFACE_WORK_CARD_RESET, &iface_work_flags))
2514 set_bit(MWIFIEX_IFACE_WORK_CARD_RESET, &iface_work_flags);
2516 schedule_work(&sdio_work);
2519 /* This function dumps FW information */
2520 static void mwifiex_sdio_device_dump(struct mwifiex_adapter *adapter)
2522 save_adapter = adapter;
2523 if (test_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP, &iface_work_flags))
2526 set_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP, &iface_work_flags);
2527 schedule_work(&sdio_work);
2530 /* Function to dump SDIO function registers and SDIO scratch registers in case
2534 mwifiex_sdio_reg_dump(struct mwifiex_adapter *adapter, char *drv_buf)
2537 struct sdio_mmc_card *cardp = adapter->card;
2539 u8 count, func, data, index = 0, size = 0;
2540 u8 reg, reg_start, reg_end;
2541 char buf[256], *ptr;
2546 mwifiex_dbg(adapter, MSG, "SDIO register dump start\n");
2548 mwifiex_pm_wakeup_card(adapter);
2550 sdio_claim_host(cardp->func);
2552 for (count = 0; count < 5; count++) {
2553 memset(buf, 0, sizeof(buf));
2558 /* Read the registers of SDIO function0 */
2564 /* Read the registers of SDIO function1 */
2566 reg_start = cardp->reg->func1_dump_reg_start;
2567 reg_end = cardp->reg->func1_dump_reg_end;
2572 reg_start = cardp->reg->func1_spec_reg_table[index++];
2573 size = cardp->reg->func1_spec_reg_num;
2574 reg_end = cardp->reg->func1_spec_reg_table[size-1];
2577 /* Read the scratch registers of SDIO function1 */
2581 reg_start = cardp->reg->func1_scratch_reg;
2582 reg_end = reg_start + MWIFIEX_SDIO_SCRATCH_SIZE;
2586 ptr += sprintf(ptr, "SDIO Func%d (%#x-%#x): ",
2587 func, reg_start, reg_end);
2589 ptr += sprintf(ptr, "SDIO Func%d: ", func);
2591 for (reg = reg_start; reg <= reg_end;) {
2593 data = sdio_f0_readb(cardp->func, reg, &ret);
2595 data = sdio_readb(cardp->func, reg, &ret);
2598 ptr += sprintf(ptr, "(%#x) ", reg);
2600 ptr += sprintf(ptr, "%02x ", data);
2602 ptr += sprintf(ptr, "ERR");
2606 if (count == 2 && reg < reg_end)
2607 reg = cardp->reg->func1_spec_reg_table[index++];
2612 mwifiex_dbg(adapter, MSG, "%s\n", buf);
2613 p += sprintf(p, "%s\n", buf);
2616 sdio_release_host(cardp->func);
2618 mwifiex_dbg(adapter, MSG, "SDIO register dump end\n");
2623 static struct mwifiex_if_ops sdio_ops = {
2624 .init_if = mwifiex_init_sdio,
2625 .cleanup_if = mwifiex_cleanup_sdio,
2626 .check_fw_status = mwifiex_check_fw_status,
2627 .check_winner_status = mwifiex_check_winner_status,
2628 .prog_fw = mwifiex_prog_fw_w_helper,
2629 .register_dev = mwifiex_register_dev,
2630 .unregister_dev = mwifiex_unregister_dev,
2631 .enable_int = mwifiex_sdio_enable_host_int,
2632 .disable_int = mwifiex_sdio_disable_host_int,
2633 .process_int_status = mwifiex_process_int_status,
2634 .host_to_card = mwifiex_sdio_host_to_card,
2635 .wakeup = mwifiex_pm_wakeup_card,
2636 .wakeup_complete = mwifiex_pm_wakeup_card_complete,
2639 .update_mp_end_port = mwifiex_update_mp_end_port,
2640 .cleanup_mpa_buf = mwifiex_cleanup_mpa_buf,
2641 .cmdrsp_complete = mwifiex_sdio_cmdrsp_complete,
2642 .event_complete = mwifiex_sdio_event_complete,
2643 .card_reset = mwifiex_sdio_card_reset,
2644 .reg_dump = mwifiex_sdio_reg_dump,
2645 .device_dump = mwifiex_sdio_device_dump,
2646 .deaggr_pkt = mwifiex_deaggr_sdio_pkt,
2650 * This function initializes the SDIO driver.
2652 * This initiates the semaphore and registers the device with
2656 mwifiex_sdio_init_module(void)
2658 sema_init(&add_remove_card_sem, 1);
2660 /* Clear the flag in case user removes the card. */
2663 return sdio_register_driver(&mwifiex_sdio);
2667 * This function cleans up the SDIO driver.
2669 * The following major steps are followed for cleanup -
2670 * - Resume the device if its suspended
2671 * - Disconnect the device if connected
2672 * - Shutdown the firmware
2673 * - Unregister the device from SDIO bus.
2676 mwifiex_sdio_cleanup_module(void)
2678 if (!down_interruptible(&add_remove_card_sem))
2679 up(&add_remove_card_sem);
2681 /* Set the flag as user is removing this module. */
2683 cancel_work_sync(&sdio_work);
2685 sdio_unregister_driver(&mwifiex_sdio);
2688 module_init(mwifiex_sdio_init_module);
2689 module_exit(mwifiex_sdio_cleanup_module);
2691 MODULE_AUTHOR("Marvell International Ltd.");
2692 MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION);
2693 MODULE_VERSION(SDIO_VERSION);
2694 MODULE_LICENSE("GPL v2");
2695 MODULE_FIRMWARE(SD8786_DEFAULT_FW_NAME);
2696 MODULE_FIRMWARE(SD8787_DEFAULT_FW_NAME);
2697 MODULE_FIRMWARE(SD8797_DEFAULT_FW_NAME);
2698 MODULE_FIRMWARE(SD8897_DEFAULT_FW_NAME);
2699 MODULE_FIRMWARE(SD8887_DEFAULT_FW_NAME);
2700 MODULE_FIRMWARE(SD8997_DEFAULT_FW_NAME);