Linux 3.11-rc3
[cris-mirror.git] / drivers / net / wireless / mwifiex / sdio.c
blob5ee5ed02eccd56645c94f8b4972962130f5b305b
1 /*
2 * Marvell Wireless LAN device driver: SDIO specific handling
4 * Copyright (C) 2011, Marvell International Ltd.
6 * This software file (the "File") is distributed by Marvell International
7 * Ltd. under the terms of the GNU General Public License Version 2, June 1991
8 * (the "License"). You may use, redistribute and/or modify this File in
9 * accordance with the terms and conditions of the License, a copy of which
10 * is available by writing to the Free Software Foundation, Inc.,
11 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
12 * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
14 * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
16 * ARE EXPRESSLY DISCLAIMED. The License provides additional details about
17 * this warranty disclaimer.
20 #include <linux/firmware.h>
22 #include "decl.h"
23 #include "ioctl.h"
24 #include "util.h"
25 #include "fw.h"
26 #include "main.h"
27 #include "wmm.h"
28 #include "11n.h"
29 #include "sdio.h"
32 #define SDIO_VERSION "1.0"
34 /* The mwifiex_sdio_remove() callback function is called when
35 * user removes this module from kernel space or ejects
36 * the card from the slot. The driver handles these 2 cases
37 * differently.
38 * If the user is removing the module, the few commands (FUNC_SHUTDOWN,
39 * HS_CANCEL etc.) are sent to the firmware.
40 * If the card is removed, there is no need to send these command.
42 * The variable 'user_rmmod' is used to distinguish these two
43 * scenarios. This flag is initialized as FALSE in case the card
44 * is removed, and will be set to TRUE for module removal when
45 * module_exit function is called.
47 static u8 user_rmmod;
49 static struct mwifiex_if_ops sdio_ops;
51 static struct semaphore add_remove_card_sem;
53 static int mwifiex_sdio_resume(struct device *dev);
56 * SDIO probe.
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
61 * interface.
63 static int
64 mwifiex_sdio_probe(struct sdio_func *func, const struct sdio_device_id *id)
66 int ret;
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);
73 if (!card)
74 return -ENOMEM;
76 card->func = func;
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;
87 card->supports_sdio_new_mode = data->supports_sdio_new_mode;
88 card->has_control_mask = data->has_control_mask;
91 sdio_claim_host(func);
92 ret = sdio_enable_func(func);
93 sdio_release_host(func);
95 if (ret) {
96 pr_err("%s: failed to enable function\n", __func__);
97 kfree(card);
98 return -EIO;
101 if (mwifiex_add_card(card, &add_remove_card_sem, &sdio_ops,
102 MWIFIEX_SDIO)) {
103 pr_err("%s: add card failed\n", __func__);
104 kfree(card);
105 sdio_claim_host(func);
106 ret = sdio_disable_func(func);
107 sdio_release_host(func);
108 ret = -1;
111 return ret;
115 * SDIO remove.
117 * This function removes the interface and frees up the card structure.
119 static void
120 mwifiex_sdio_remove(struct sdio_func *func)
122 struct sdio_mmc_card *card;
123 struct mwifiex_adapter *adapter;
124 struct mwifiex_private *priv;
125 int i;
127 pr_debug("info: SDIO func num=%d\n", func->num);
129 card = sdio_get_drvdata(func);
130 if (!card)
131 return;
133 adapter = card->adapter;
134 if (!adapter || !adapter->priv_num)
135 return;
137 /* In case driver is removed when asynchronous FW load is in progress */
138 wait_for_completion(&adapter->fw_load);
140 if (user_rmmod) {
141 if (adapter->is_suspended)
142 mwifiex_sdio_resume(adapter->dev);
144 for (i = 0; i < adapter->priv_num; i++)
145 if ((GET_BSS_ROLE(adapter->priv[i]) ==
146 MWIFIEX_BSS_ROLE_STA) &&
147 adapter->priv[i]->media_connected)
148 mwifiex_deauthenticate(adapter->priv[i], NULL);
150 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
151 mwifiex_disable_auto_ds(priv);
152 mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
155 mwifiex_remove_card(card->adapter, &add_remove_card_sem);
156 kfree(card);
160 * SDIO suspend.
162 * Kernel needs to suspend all functions separately. Therefore all
163 * registered functions must have drivers with suspend and resume
164 * methods. Failing that the kernel simply removes the whole card.
166 * If already not suspended, this function allocates and sends a host
167 * sleep activate request to the firmware and turns off the traffic.
169 static int mwifiex_sdio_suspend(struct device *dev)
171 struct sdio_func *func = dev_to_sdio_func(dev);
172 struct sdio_mmc_card *card;
173 struct mwifiex_adapter *adapter;
174 mmc_pm_flag_t pm_flag = 0;
175 int ret = 0;
177 if (func) {
178 pm_flag = sdio_get_host_pm_caps(func);
179 pr_debug("cmd: %s: suspend: PM flag = 0x%x\n",
180 sdio_func_id(func), pm_flag);
181 if (!(pm_flag & MMC_PM_KEEP_POWER)) {
182 pr_err("%s: cannot remain alive while host is"
183 " suspended\n", sdio_func_id(func));
184 return -ENOSYS;
187 card = sdio_get_drvdata(func);
188 if (!card || !card->adapter) {
189 pr_err("suspend: invalid card or adapter\n");
190 return 0;
192 } else {
193 pr_err("suspend: sdio_func is not specified\n");
194 return 0;
197 adapter = card->adapter;
199 /* Enable the Host Sleep */
200 if (!mwifiex_enable_hs(adapter)) {
201 dev_err(adapter->dev, "cmd: failed to suspend\n");
202 return -EFAULT;
205 dev_dbg(adapter->dev, "cmd: suspend with MMC_PM_KEEP_POWER\n");
206 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
208 /* Indicate device suspended */
209 adapter->is_suspended = true;
211 return ret;
215 * SDIO resume.
217 * Kernel needs to suspend all functions separately. Therefore all
218 * registered functions must have drivers with suspend and resume
219 * methods. Failing that the kernel simply removes the whole card.
221 * If already not resumed, this function turns on the traffic and
222 * sends a host sleep cancel request to the firmware.
224 static int mwifiex_sdio_resume(struct device *dev)
226 struct sdio_func *func = dev_to_sdio_func(dev);
227 struct sdio_mmc_card *card;
228 struct mwifiex_adapter *adapter;
229 mmc_pm_flag_t pm_flag = 0;
231 if (func) {
232 pm_flag = sdio_get_host_pm_caps(func);
233 card = sdio_get_drvdata(func);
234 if (!card || !card->adapter) {
235 pr_err("resume: invalid card or adapter\n");
236 return 0;
238 } else {
239 pr_err("resume: sdio_func is not specified\n");
240 return 0;
243 adapter = card->adapter;
245 if (!adapter->is_suspended) {
246 dev_warn(adapter->dev, "device already resumed\n");
247 return 0;
250 adapter->is_suspended = false;
252 /* Disable Host Sleep */
253 mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
254 MWIFIEX_ASYNC_CMD);
256 return 0;
259 /* Device ID for SD8786 */
260 #define SDIO_DEVICE_ID_MARVELL_8786 (0x9116)
261 /* Device ID for SD8787 */
262 #define SDIO_DEVICE_ID_MARVELL_8787 (0x9119)
263 /* Device ID for SD8797 */
264 #define SDIO_DEVICE_ID_MARVELL_8797 (0x9129)
265 /* Device ID for SD8897 */
266 #define SDIO_DEVICE_ID_MARVELL_8897 (0x912d)
268 /* WLAN IDs */
269 static const struct sdio_device_id mwifiex_ids[] = {
270 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8786),
271 .driver_data = (unsigned long) &mwifiex_sdio_sd8786},
272 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787),
273 .driver_data = (unsigned long) &mwifiex_sdio_sd8787},
274 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8797),
275 .driver_data = (unsigned long) &mwifiex_sdio_sd8797},
276 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8897),
277 .driver_data = (unsigned long) &mwifiex_sdio_sd8897},
281 MODULE_DEVICE_TABLE(sdio, mwifiex_ids);
283 static const struct dev_pm_ops mwifiex_sdio_pm_ops = {
284 .suspend = mwifiex_sdio_suspend,
285 .resume = mwifiex_sdio_resume,
288 static struct sdio_driver mwifiex_sdio = {
289 .name = "mwifiex_sdio",
290 .id_table = mwifiex_ids,
291 .probe = mwifiex_sdio_probe,
292 .remove = mwifiex_sdio_remove,
293 .drv = {
294 .owner = THIS_MODULE,
295 .pm = &mwifiex_sdio_pm_ops,
300 * This function writes data into SDIO card register.
302 static int
303 mwifiex_write_reg(struct mwifiex_adapter *adapter, u32 reg, u8 data)
305 struct sdio_mmc_card *card = adapter->card;
306 int ret = -1;
308 sdio_claim_host(card->func);
309 sdio_writeb(card->func, data, reg, &ret);
310 sdio_release_host(card->func);
312 return ret;
316 * This function reads data from SDIO card register.
318 static int
319 mwifiex_read_reg(struct mwifiex_adapter *adapter, u32 reg, u8 *data)
321 struct sdio_mmc_card *card = adapter->card;
322 int ret = -1;
323 u8 val;
325 sdio_claim_host(card->func);
326 val = sdio_readb(card->func, reg, &ret);
327 sdio_release_host(card->func);
329 *data = val;
331 return ret;
335 * This function writes multiple data into SDIO card memory.
337 * This does not work in suspended mode.
339 static int
340 mwifiex_write_data_sync(struct mwifiex_adapter *adapter,
341 u8 *buffer, u32 pkt_len, u32 port)
343 struct sdio_mmc_card *card = adapter->card;
344 int ret;
345 u8 blk_mode =
346 (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE : BLOCK_MODE;
347 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
348 u32 blk_cnt =
349 (blk_mode ==
350 BLOCK_MODE) ? (pkt_len /
351 MWIFIEX_SDIO_BLOCK_SIZE) : pkt_len;
352 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
354 if (adapter->is_suspended) {
355 dev_err(adapter->dev,
356 "%s: not allowed while suspended\n", __func__);
357 return -1;
360 sdio_claim_host(card->func);
362 ret = sdio_writesb(card->func, ioport, buffer, blk_cnt * blk_size);
364 sdio_release_host(card->func);
366 return ret;
370 * This function reads multiple data from SDIO card memory.
372 static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *buffer,
373 u32 len, u32 port, u8 claim)
375 struct sdio_mmc_card *card = adapter->card;
376 int ret;
377 u8 blk_mode = (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE
378 : BLOCK_MODE;
379 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
380 u32 blk_cnt = (blk_mode == BLOCK_MODE) ? (len / MWIFIEX_SDIO_BLOCK_SIZE)
381 : len;
382 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
384 if (claim)
385 sdio_claim_host(card->func);
387 ret = sdio_readsb(card->func, buffer, ioport, blk_cnt * blk_size);
389 if (claim)
390 sdio_release_host(card->func);
392 return ret;
396 * This function wakes up the card.
398 * A host power up command is written to the card configuration
399 * register to wake up the card.
401 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
403 dev_dbg(adapter->dev, "event: wakeup device...\n");
405 return mwifiex_write_reg(adapter, CONFIGURATION_REG, HOST_POWER_UP);
409 * This function is called after the card has woken up.
411 * The card configuration register is reset.
413 static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
415 dev_dbg(adapter->dev, "cmd: wakeup device completed\n");
417 return mwifiex_write_reg(adapter, CONFIGURATION_REG, 0);
421 * This function is used to initialize IO ports for the
422 * chipsets supporting SDIO new mode eg SD8897.
424 static int mwifiex_init_sdio_new_mode(struct mwifiex_adapter *adapter)
426 u8 reg;
428 adapter->ioport = MEM_PORT;
430 /* enable sdio new mode */
431 if (mwifiex_read_reg(adapter, CARD_CONFIG_2_1_REG, &reg))
432 return -1;
433 if (mwifiex_write_reg(adapter, CARD_CONFIG_2_1_REG,
434 reg | CMD53_NEW_MODE))
435 return -1;
437 /* Configure cmd port and enable reading rx length from the register */
438 if (mwifiex_read_reg(adapter, CMD_CONFIG_0, &reg))
439 return -1;
440 if (mwifiex_write_reg(adapter, CMD_CONFIG_0, reg | CMD_PORT_RD_LEN_EN))
441 return -1;
443 /* Enable Dnld/Upld ready auto reset for cmd port after cmd53 is
444 * completed
446 if (mwifiex_read_reg(adapter, CMD_CONFIG_1, &reg))
447 return -1;
448 if (mwifiex_write_reg(adapter, CMD_CONFIG_1, reg | CMD_PORT_AUTO_EN))
449 return -1;
451 return 0;
454 /* This function initializes the IO ports.
456 * The following operations are performed -
457 * - Read the IO ports (0, 1 and 2)
458 * - Set host interrupt Reset-To-Read to clear
459 * - Set auto re-enable interrupt
461 static int mwifiex_init_sdio_ioport(struct mwifiex_adapter *adapter)
463 u8 reg;
464 struct sdio_mmc_card *card = adapter->card;
466 adapter->ioport = 0;
468 if (card->supports_sdio_new_mode) {
469 if (mwifiex_init_sdio_new_mode(adapter))
470 return -1;
471 goto cont;
474 /* Read the IO port */
475 if (!mwifiex_read_reg(adapter, IO_PORT_0_REG, &reg))
476 adapter->ioport |= (reg & 0xff);
477 else
478 return -1;
480 if (!mwifiex_read_reg(adapter, IO_PORT_1_REG, &reg))
481 adapter->ioport |= ((reg & 0xff) << 8);
482 else
483 return -1;
485 if (!mwifiex_read_reg(adapter, IO_PORT_2_REG, &reg))
486 adapter->ioport |= ((reg & 0xff) << 16);
487 else
488 return -1;
489 cont:
490 pr_debug("info: SDIO FUNC1 IO port: %#x\n", adapter->ioport);
492 /* Set Host interrupt reset to read to clear */
493 if (!mwifiex_read_reg(adapter, HOST_INT_RSR_REG, &reg))
494 mwifiex_write_reg(adapter, HOST_INT_RSR_REG,
495 reg | card->reg->sdio_int_mask);
496 else
497 return -1;
499 /* Dnld/Upld ready set to auto reset */
500 if (!mwifiex_read_reg(adapter, card->reg->card_misc_cfg_reg, &reg))
501 mwifiex_write_reg(adapter, card->reg->card_misc_cfg_reg,
502 reg | AUTO_RE_ENABLE_INT);
503 else
504 return -1;
506 return 0;
510 * This function sends data to the card.
512 static int mwifiex_write_data_to_card(struct mwifiex_adapter *adapter,
513 u8 *payload, u32 pkt_len, u32 port)
515 u32 i = 0;
516 int ret;
518 do {
519 ret = mwifiex_write_data_sync(adapter, payload, pkt_len, port);
520 if (ret) {
521 i++;
522 dev_err(adapter->dev, "host_to_card, write iomem"
523 " (%d) failed: %d\n", i, ret);
524 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
525 dev_err(adapter->dev, "write CFG reg failed\n");
527 ret = -1;
528 if (i > MAX_WRITE_IOMEM_RETRY)
529 return ret;
531 } while (ret == -1);
533 return ret;
537 * This function gets the read port.
539 * If control port bit is set in MP read bitmap, the control port
540 * is returned, otherwise the current read port is returned and
541 * the value is increased (provided it does not reach the maximum
542 * limit, in which case it is reset to 1)
544 static int mwifiex_get_rd_port(struct mwifiex_adapter *adapter, u8 *port)
546 struct sdio_mmc_card *card = adapter->card;
547 const struct mwifiex_sdio_card_reg *reg = card->reg;
548 u32 rd_bitmap = card->mp_rd_bitmap;
550 dev_dbg(adapter->dev, "data: mp_rd_bitmap=0x%08x\n", rd_bitmap);
552 if (card->supports_sdio_new_mode) {
553 if (!(rd_bitmap & reg->data_port_mask))
554 return -1;
555 } else {
556 if (!(rd_bitmap & (CTRL_PORT_MASK | reg->data_port_mask)))
557 return -1;
560 if ((card->has_control_mask) &&
561 (card->mp_rd_bitmap & CTRL_PORT_MASK)) {
562 card->mp_rd_bitmap &= (u32) (~CTRL_PORT_MASK);
563 *port = CTRL_PORT;
564 dev_dbg(adapter->dev, "data: port=%d mp_rd_bitmap=0x%08x\n",
565 *port, card->mp_rd_bitmap);
566 return 0;
569 if (!(card->mp_rd_bitmap & (1 << card->curr_rd_port)))
570 return -1;
572 /* We are now handling the SDIO data ports */
573 card->mp_rd_bitmap &= (u32)(~(1 << card->curr_rd_port));
574 *port = card->curr_rd_port;
576 if (++card->curr_rd_port == card->max_ports)
577 card->curr_rd_port = reg->start_rd_port;
579 dev_dbg(adapter->dev,
580 "data: port=%d mp_rd_bitmap=0x%08x -> 0x%08x\n",
581 *port, rd_bitmap, card->mp_rd_bitmap);
583 return 0;
587 * This function gets the write port for data.
589 * The current write port is returned if available and the value is
590 * increased (provided it does not reach the maximum limit, in which
591 * case it is reset to 1)
593 static int mwifiex_get_wr_port_data(struct mwifiex_adapter *adapter, u32 *port)
595 struct sdio_mmc_card *card = adapter->card;
596 const struct mwifiex_sdio_card_reg *reg = card->reg;
597 u32 wr_bitmap = card->mp_wr_bitmap;
599 dev_dbg(adapter->dev, "data: mp_wr_bitmap=0x%08x\n", wr_bitmap);
601 if (card->supports_sdio_new_mode &&
602 !(wr_bitmap & reg->data_port_mask)) {
603 adapter->data_sent = true;
604 return -EBUSY;
605 } else if (!card->supports_sdio_new_mode &&
606 !(wr_bitmap & card->mp_data_port_mask)) {
607 return -1;
610 if (card->mp_wr_bitmap & (1 << card->curr_wr_port)) {
611 card->mp_wr_bitmap &= (u32) (~(1 << card->curr_wr_port));
612 *port = card->curr_wr_port;
613 if (((card->supports_sdio_new_mode) &&
614 (++card->curr_wr_port == card->max_ports)) ||
615 ((!card->supports_sdio_new_mode) &&
616 (++card->curr_wr_port == card->mp_end_port)))
617 card->curr_wr_port = reg->start_wr_port;
618 } else {
619 adapter->data_sent = true;
620 return -EBUSY;
623 if ((card->has_control_mask) && (*port == CTRL_PORT)) {
624 dev_err(adapter->dev,
625 "invalid data port=%d cur port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
626 *port, card->curr_wr_port, wr_bitmap,
627 card->mp_wr_bitmap);
628 return -1;
631 dev_dbg(adapter->dev, "data: port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
632 *port, wr_bitmap, card->mp_wr_bitmap);
634 return 0;
638 * This function polls the card status.
640 static int
641 mwifiex_sdio_poll_card_status(struct mwifiex_adapter *adapter, u8 bits)
643 struct sdio_mmc_card *card = adapter->card;
644 u32 tries;
645 u8 cs;
647 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
648 if (mwifiex_read_reg(adapter, card->reg->poll_reg, &cs))
649 break;
650 else if ((cs & bits) == bits)
651 return 0;
653 usleep_range(10, 20);
656 dev_err(adapter->dev, "poll card status failed, tries = %d\n", tries);
658 return -1;
662 * This function reads the firmware status.
664 static int
665 mwifiex_sdio_read_fw_status(struct mwifiex_adapter *adapter, u16 *dat)
667 struct sdio_mmc_card *card = adapter->card;
668 const struct mwifiex_sdio_card_reg *reg = card->reg;
669 u8 fws0, fws1;
671 if (mwifiex_read_reg(adapter, reg->status_reg_0, &fws0))
672 return -1;
674 if (mwifiex_read_reg(adapter, reg->status_reg_1, &fws1))
675 return -1;
677 *dat = (u16) ((fws1 << 8) | fws0);
679 return 0;
683 * This function disables the host interrupt.
685 * The host interrupt mask is read, the disable bit is reset and
686 * written back to the card host interrupt mask register.
688 static int mwifiex_sdio_disable_host_int(struct mwifiex_adapter *adapter)
690 u8 host_int_mask, host_int_disable = HOST_INT_DISABLE;
692 /* Read back the host_int_mask register */
693 if (mwifiex_read_reg(adapter, HOST_INT_MASK_REG, &host_int_mask))
694 return -1;
696 /* Update with the mask and write back to the register */
697 host_int_mask &= ~host_int_disable;
699 if (mwifiex_write_reg(adapter, HOST_INT_MASK_REG, host_int_mask)) {
700 dev_err(adapter->dev, "disable host interrupt failed\n");
701 return -1;
704 return 0;
708 * This function enables the host interrupt.
710 * The host interrupt enable mask is written to the card
711 * host interrupt mask register.
713 static int mwifiex_sdio_enable_host_int(struct mwifiex_adapter *adapter)
715 struct sdio_mmc_card *card = adapter->card;
717 /* Simply write the mask to the register */
718 if (mwifiex_write_reg(adapter, HOST_INT_MASK_REG,
719 card->reg->host_int_enable)) {
720 dev_err(adapter->dev, "enable host interrupt failed\n");
721 return -1;
723 return 0;
727 * This function sends a data buffer to the card.
729 static int mwifiex_sdio_card_to_host(struct mwifiex_adapter *adapter,
730 u32 *type, u8 *buffer,
731 u32 npayload, u32 ioport)
733 int ret;
734 u32 nb;
736 if (!buffer) {
737 dev_err(adapter->dev, "%s: buffer is NULL\n", __func__);
738 return -1;
741 ret = mwifiex_read_data_sync(adapter, buffer, npayload, ioport, 1);
743 if (ret) {
744 dev_err(adapter->dev, "%s: read iomem failed: %d\n", __func__,
745 ret);
746 return -1;
749 nb = le16_to_cpu(*(__le16 *) (buffer));
750 if (nb > npayload) {
751 dev_err(adapter->dev, "%s: invalid packet, nb=%d npayload=%d\n",
752 __func__, nb, npayload);
753 return -1;
756 *type = le16_to_cpu(*(__le16 *) (buffer + 2));
758 return ret;
762 * This function downloads the firmware to the card.
764 * Firmware is downloaded to the card in blocks. Every block download
765 * is tested for CRC errors, and retried a number of times before
766 * returning failure.
768 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
769 struct mwifiex_fw_image *fw)
771 struct sdio_mmc_card *card = adapter->card;
772 const struct mwifiex_sdio_card_reg *reg = card->reg;
773 int ret;
774 u8 *firmware = fw->fw_buf;
775 u32 firmware_len = fw->fw_len;
776 u32 offset = 0;
777 u8 base0, base1;
778 u8 *fwbuf;
779 u16 len = 0;
780 u32 txlen, tx_blocks = 0, tries;
781 u32 i = 0;
783 if (!firmware_len) {
784 dev_err(adapter->dev,
785 "firmware image not found! Terminating download\n");
786 return -1;
789 dev_dbg(adapter->dev, "info: downloading FW image (%d bytes)\n",
790 firmware_len);
792 /* Assume that the allocated buffer is 8-byte aligned */
793 fwbuf = kzalloc(MWIFIEX_UPLD_SIZE, GFP_KERNEL);
794 if (!fwbuf)
795 return -ENOMEM;
797 /* Perform firmware data transfer */
798 do {
799 /* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY
800 bits */
801 ret = mwifiex_sdio_poll_card_status(adapter, CARD_IO_READY |
802 DN_LD_CARD_RDY);
803 if (ret) {
804 dev_err(adapter->dev, "FW download with helper:"
805 " poll status timeout @ %d\n", offset);
806 goto done;
809 /* More data? */
810 if (offset >= firmware_len)
811 break;
813 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
814 ret = mwifiex_read_reg(adapter, reg->base_0_reg,
815 &base0);
816 if (ret) {
817 dev_err(adapter->dev,
818 "dev BASE0 register read failed: "
819 "base0=%#04X(%d). Terminating dnld\n",
820 base0, base0);
821 goto done;
823 ret = mwifiex_read_reg(adapter, reg->base_1_reg,
824 &base1);
825 if (ret) {
826 dev_err(adapter->dev,
827 "dev BASE1 register read failed: "
828 "base1=%#04X(%d). Terminating dnld\n",
829 base1, base1);
830 goto done;
832 len = (u16) (((base1 & 0xff) << 8) | (base0 & 0xff));
834 if (len)
835 break;
837 usleep_range(10, 20);
840 if (!len) {
841 break;
842 } else if (len > MWIFIEX_UPLD_SIZE) {
843 dev_err(adapter->dev,
844 "FW dnld failed @ %d, invalid length %d\n",
845 offset, len);
846 ret = -1;
847 goto done;
850 txlen = len;
852 if (len & BIT(0)) {
853 i++;
854 if (i > MAX_WRITE_IOMEM_RETRY) {
855 dev_err(adapter->dev,
856 "FW dnld failed @ %d, over max retry\n",
857 offset);
858 ret = -1;
859 goto done;
861 dev_err(adapter->dev, "CRC indicated by the helper:"
862 " len = 0x%04X, txlen = %d\n", len, txlen);
863 len &= ~BIT(0);
864 /* Setting this to 0 to resend from same offset */
865 txlen = 0;
866 } else {
867 i = 0;
869 /* Set blocksize to transfer - checking for last
870 block */
871 if (firmware_len - offset < txlen)
872 txlen = firmware_len - offset;
874 tx_blocks = (txlen + MWIFIEX_SDIO_BLOCK_SIZE - 1)
875 / MWIFIEX_SDIO_BLOCK_SIZE;
877 /* Copy payload to buffer */
878 memmove(fwbuf, &firmware[offset], txlen);
881 ret = mwifiex_write_data_sync(adapter, fwbuf, tx_blocks *
882 MWIFIEX_SDIO_BLOCK_SIZE,
883 adapter->ioport);
884 if (ret) {
885 dev_err(adapter->dev,
886 "FW download, write iomem (%d) failed @ %d\n",
887 i, offset);
888 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
889 dev_err(adapter->dev, "write CFG reg failed\n");
891 ret = -1;
892 goto done;
895 offset += txlen;
896 } while (true);
898 dev_dbg(adapter->dev, "info: FW download over, size %d bytes\n",
899 offset);
901 ret = 0;
902 done:
903 kfree(fwbuf);
904 return ret;
908 * This function checks the firmware status in card.
910 * The winner interface is also determined by this function.
912 static int mwifiex_check_fw_status(struct mwifiex_adapter *adapter,
913 u32 poll_num)
915 struct sdio_mmc_card *card = adapter->card;
916 int ret = 0;
917 u16 firmware_stat;
918 u32 tries;
919 u8 winner_status;
921 /* Wait for firmware initialization event */
922 for (tries = 0; tries < poll_num; tries++) {
923 ret = mwifiex_sdio_read_fw_status(adapter, &firmware_stat);
924 if (ret)
925 continue;
926 if (firmware_stat == FIRMWARE_READY_SDIO) {
927 ret = 0;
928 break;
929 } else {
930 mdelay(100);
931 ret = -1;
935 if (ret) {
936 if (mwifiex_read_reg
937 (adapter, card->reg->status_reg_0, &winner_status))
938 winner_status = 0;
940 if (winner_status)
941 adapter->winner = 0;
942 else
943 adapter->winner = 1;
945 return ret;
949 * This function reads the interrupt status from card.
951 static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
953 struct sdio_mmc_card *card = adapter->card;
954 u8 sdio_ireg;
955 unsigned long flags;
957 if (mwifiex_read_data_sync(adapter, card->mp_regs,
958 card->reg->max_mp_regs,
959 REG_PORT | MWIFIEX_SDIO_BYTE_MODE_MASK, 0)) {
960 dev_err(adapter->dev, "read mp_regs failed\n");
961 return;
964 sdio_ireg = card->mp_regs[HOST_INTSTATUS_REG];
965 if (sdio_ireg) {
967 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
968 * For SDIO new mode CMD port interrupts
969 * DN_LD_CMD_PORT_HOST_INT_STATUS and/or
970 * UP_LD_CMD_PORT_HOST_INT_STATUS
971 * Clear the interrupt status register
973 dev_dbg(adapter->dev, "int: sdio_ireg = %#x\n", sdio_ireg);
974 spin_lock_irqsave(&adapter->int_lock, flags);
975 adapter->int_status |= sdio_ireg;
976 spin_unlock_irqrestore(&adapter->int_lock, flags);
981 * SDIO interrupt handler.
983 * This function reads the interrupt status from firmware and handles
984 * the interrupt in current thread (ksdioirqd) right away.
986 static void
987 mwifiex_sdio_interrupt(struct sdio_func *func)
989 struct mwifiex_adapter *adapter;
990 struct sdio_mmc_card *card;
992 card = sdio_get_drvdata(func);
993 if (!card || !card->adapter) {
994 pr_debug("int: func=%p card=%p adapter=%p\n",
995 func, card, card ? card->adapter : NULL);
996 return;
998 adapter = card->adapter;
1000 if (adapter->surprise_removed)
1001 return;
1003 if (!adapter->pps_uapsd_mode && adapter->ps_state == PS_STATE_SLEEP)
1004 adapter->ps_state = PS_STATE_AWAKE;
1006 mwifiex_interrupt_status(adapter);
1007 mwifiex_main_process(adapter);
1011 * This function decodes a received packet.
1013 * Based on the type, the packet is treated as either a data, or
1014 * a command response, or an event, and the correct handler
1015 * function is invoked.
1017 static int mwifiex_decode_rx_packet(struct mwifiex_adapter *adapter,
1018 struct sk_buff *skb, u32 upld_typ)
1020 u8 *cmd_buf;
1022 skb_pull(skb, INTF_HEADER_LEN);
1024 switch (upld_typ) {
1025 case MWIFIEX_TYPE_DATA:
1026 dev_dbg(adapter->dev, "info: --- Rx: Data packet ---\n");
1027 mwifiex_handle_rx_packet(adapter, skb);
1028 break;
1030 case MWIFIEX_TYPE_CMD:
1031 dev_dbg(adapter->dev, "info: --- Rx: Cmd Response ---\n");
1032 /* take care of curr_cmd = NULL case */
1033 if (!adapter->curr_cmd) {
1034 cmd_buf = adapter->upld_buf;
1036 if (adapter->ps_state == PS_STATE_SLEEP_CFM)
1037 mwifiex_process_sleep_confirm_resp(adapter,
1038 skb->data,
1039 skb->len);
1041 memcpy(cmd_buf, skb->data,
1042 min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER,
1043 skb->len));
1045 dev_kfree_skb_any(skb);
1046 } else {
1047 adapter->cmd_resp_received = true;
1048 adapter->curr_cmd->resp_skb = skb;
1050 break;
1052 case MWIFIEX_TYPE_EVENT:
1053 dev_dbg(adapter->dev, "info: --- Rx: Event ---\n");
1054 adapter->event_cause = *(u32 *) skb->data;
1056 if ((skb->len > 0) && (skb->len < MAX_EVENT_SIZE))
1057 memcpy(adapter->event_body,
1058 skb->data + MWIFIEX_EVENT_HEADER_LEN,
1059 skb->len);
1061 /* event cause has been saved to adapter->event_cause */
1062 adapter->event_received = true;
1063 adapter->event_skb = skb;
1065 break;
1067 default:
1068 dev_err(adapter->dev, "unknown upload type %#x\n", upld_typ);
1069 dev_kfree_skb_any(skb);
1070 break;
1073 return 0;
1077 * This function transfers received packets from card to driver, performing
1078 * aggregation if required.
1080 * For data received on control port, or if aggregation is disabled, the
1081 * received buffers are uploaded as separate packets. However, if aggregation
1082 * is enabled and required, the buffers are copied onto an aggregation buffer,
1083 * provided there is space left, processed and finally uploaded.
1085 static int mwifiex_sdio_card_to_host_mp_aggr(struct mwifiex_adapter *adapter,
1086 struct sk_buff *skb, u8 port)
1088 struct sdio_mmc_card *card = adapter->card;
1089 s32 f_do_rx_aggr = 0;
1090 s32 f_do_rx_cur = 0;
1091 s32 f_aggr_cur = 0;
1092 struct sk_buff *skb_deaggr;
1093 u32 pind;
1094 u32 pkt_len, pkt_type, mport;
1095 u8 *curr_ptr;
1096 u32 rx_len = skb->len;
1098 if ((card->has_control_mask) && (port == CTRL_PORT)) {
1099 /* Read the command Resp without aggr */
1100 dev_dbg(adapter->dev, "info: %s: no aggregation for cmd "
1101 "response\n", __func__);
1103 f_do_rx_cur = 1;
1104 goto rx_curr_single;
1107 if (!card->mpa_rx.enabled) {
1108 dev_dbg(adapter->dev, "info: %s: rx aggregation disabled\n",
1109 __func__);
1111 f_do_rx_cur = 1;
1112 goto rx_curr_single;
1115 if ((!card->has_control_mask && (card->mp_rd_bitmap &
1116 card->reg->data_port_mask)) ||
1117 (card->has_control_mask && (card->mp_rd_bitmap &
1118 (~((u32) CTRL_PORT_MASK))))) {
1119 /* Some more data RX pending */
1120 dev_dbg(adapter->dev, "info: %s: not last packet\n", __func__);
1122 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1123 if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len)) {
1124 f_aggr_cur = 1;
1125 } else {
1126 /* No room in Aggr buf, do rx aggr now */
1127 f_do_rx_aggr = 1;
1128 f_do_rx_cur = 1;
1130 } else {
1131 /* Rx aggr not in progress */
1132 f_aggr_cur = 1;
1135 } else {
1136 /* No more data RX pending */
1137 dev_dbg(adapter->dev, "info: %s: last packet\n", __func__);
1139 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1140 f_do_rx_aggr = 1;
1141 if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len))
1142 f_aggr_cur = 1;
1143 else
1144 /* No room in Aggr buf, do rx aggr now */
1145 f_do_rx_cur = 1;
1146 } else {
1147 f_do_rx_cur = 1;
1151 if (f_aggr_cur) {
1152 dev_dbg(adapter->dev, "info: current packet aggregation\n");
1153 /* Curr pkt can be aggregated */
1154 mp_rx_aggr_setup(card, skb, port);
1156 if (MP_RX_AGGR_PKT_LIMIT_REACHED(card) ||
1157 mp_rx_aggr_port_limit_reached(card)) {
1158 dev_dbg(adapter->dev, "info: %s: aggregated packet "
1159 "limit reached\n", __func__);
1160 /* No more pkts allowed in Aggr buf, rx it */
1161 f_do_rx_aggr = 1;
1165 if (f_do_rx_aggr) {
1166 /* do aggr RX now */
1167 dev_dbg(adapter->dev, "info: do_rx_aggr: num of packets: %d\n",
1168 card->mpa_rx.pkt_cnt);
1170 if (card->supports_sdio_new_mode) {
1171 int i;
1172 u32 port_count;
1174 for (i = 0, port_count = 0; i < card->max_ports; i++)
1175 if (card->mpa_rx.ports & BIT(i))
1176 port_count++;
1178 /* Reading data from "start_port + 0" to "start_port +
1179 * port_count -1", so decrease the count by 1
1181 port_count--;
1182 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1183 (port_count << 8)) + card->mpa_rx.start_port;
1184 } else {
1185 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1186 (card->mpa_rx.ports << 4)) +
1187 card->mpa_rx.start_port;
1190 if (mwifiex_read_data_sync(adapter, card->mpa_rx.buf,
1191 card->mpa_rx.buf_len, mport, 1))
1192 goto error;
1194 curr_ptr = card->mpa_rx.buf;
1196 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1198 /* get curr PKT len & type */
1199 pkt_len = *(u16 *) &curr_ptr[0];
1200 pkt_type = *(u16 *) &curr_ptr[2];
1202 /* copy pkt to deaggr buf */
1203 skb_deaggr = card->mpa_rx.skb_arr[pind];
1205 if ((pkt_type == MWIFIEX_TYPE_DATA) && (pkt_len <=
1206 card->mpa_rx.len_arr[pind])) {
1208 memcpy(skb_deaggr->data, curr_ptr, pkt_len);
1210 skb_trim(skb_deaggr, pkt_len);
1212 /* Process de-aggr packet */
1213 mwifiex_decode_rx_packet(adapter, skb_deaggr,
1214 pkt_type);
1215 } else {
1216 dev_err(adapter->dev, "wrong aggr pkt:"
1217 " type=%d len=%d max_len=%d\n",
1218 pkt_type, pkt_len,
1219 card->mpa_rx.len_arr[pind]);
1220 dev_kfree_skb_any(skb_deaggr);
1222 curr_ptr += card->mpa_rx.len_arr[pind];
1224 MP_RX_AGGR_BUF_RESET(card);
1227 rx_curr_single:
1228 if (f_do_rx_cur) {
1229 dev_dbg(adapter->dev, "info: RX: port: %d, rx_len: %d\n",
1230 port, rx_len);
1232 if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1233 skb->data, skb->len,
1234 adapter->ioport + port))
1235 goto error;
1237 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1240 return 0;
1242 error:
1243 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1244 /* Multiport-aggregation transfer failed - cleanup */
1245 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1246 /* copy pkt to deaggr buf */
1247 skb_deaggr = card->mpa_rx.skb_arr[pind];
1248 dev_kfree_skb_any(skb_deaggr);
1250 MP_RX_AGGR_BUF_RESET(card);
1253 if (f_do_rx_cur)
1254 /* Single transfer pending. Free curr buff also */
1255 dev_kfree_skb_any(skb);
1257 return -1;
1261 * This function checks the current interrupt status.
1263 * The following interrupts are checked and handled by this function -
1264 * - Data sent
1265 * - Command sent
1266 * - Packets received
1268 * Since the firmware does not generate download ready interrupt if the
1269 * port updated is command port only, command sent interrupt checking
1270 * should be done manually, and for every SDIO interrupt.
1272 * In case of Rx packets received, the packets are uploaded from card to
1273 * host and processed accordingly.
1275 static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
1277 struct sdio_mmc_card *card = adapter->card;
1278 const struct mwifiex_sdio_card_reg *reg = card->reg;
1279 int ret = 0;
1280 u8 sdio_ireg;
1281 struct sk_buff *skb;
1282 u8 port = CTRL_PORT;
1283 u32 len_reg_l, len_reg_u;
1284 u32 rx_blocks;
1285 u16 rx_len;
1286 unsigned long flags;
1287 u32 bitmap;
1288 u8 cr;
1290 spin_lock_irqsave(&adapter->int_lock, flags);
1291 sdio_ireg = adapter->int_status;
1292 adapter->int_status = 0;
1293 spin_unlock_irqrestore(&adapter->int_lock, flags);
1295 if (!sdio_ireg)
1296 return ret;
1298 /* Following interrupt is only for SDIO new mode */
1299 if (sdio_ireg & DN_LD_CMD_PORT_HOST_INT_STATUS && adapter->cmd_sent)
1300 adapter->cmd_sent = false;
1302 /* Following interrupt is only for SDIO new mode */
1303 if (sdio_ireg & UP_LD_CMD_PORT_HOST_INT_STATUS) {
1304 u32 pkt_type;
1306 /* read the len of control packet */
1307 rx_len = card->mp_regs[CMD_RD_LEN_1] << 8;
1308 rx_len |= (u16) card->mp_regs[CMD_RD_LEN_0];
1309 rx_blocks = DIV_ROUND_UP(rx_len, MWIFIEX_SDIO_BLOCK_SIZE);
1310 if (rx_len <= INTF_HEADER_LEN ||
1311 (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1312 MWIFIEX_RX_DATA_BUF_SIZE)
1313 return -1;
1314 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1316 skb = dev_alloc_skb(rx_len);
1317 if (!skb)
1318 return -1;
1320 skb_put(skb, rx_len);
1322 if (mwifiex_sdio_card_to_host(adapter, &pkt_type, skb->data,
1323 skb->len, adapter->ioport |
1324 CMD_PORT_SLCT)) {
1325 dev_err(adapter->dev,
1326 "%s: failed to card_to_host", __func__);
1327 dev_kfree_skb_any(skb);
1328 goto term_cmd;
1331 if ((pkt_type != MWIFIEX_TYPE_CMD) &&
1332 (pkt_type != MWIFIEX_TYPE_EVENT))
1333 dev_err(adapter->dev,
1334 "%s:Received wrong packet on cmd port",
1335 __func__);
1337 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1340 if (sdio_ireg & DN_LD_HOST_INT_STATUS) {
1341 bitmap = (u32) card->mp_regs[reg->wr_bitmap_l];
1342 bitmap |= ((u32) card->mp_regs[reg->wr_bitmap_u]) << 8;
1343 if (card->supports_sdio_new_mode) {
1344 bitmap |=
1345 ((u32) card->mp_regs[reg->wr_bitmap_1l]) << 16;
1346 bitmap |=
1347 ((u32) card->mp_regs[reg->wr_bitmap_1u]) << 24;
1349 card->mp_wr_bitmap = bitmap;
1351 dev_dbg(adapter->dev, "int: DNLD: wr_bitmap=0x%x\n",
1352 card->mp_wr_bitmap);
1353 if (adapter->data_sent &&
1354 (card->mp_wr_bitmap & card->mp_data_port_mask)) {
1355 dev_dbg(adapter->dev,
1356 "info: <--- Tx DONE Interrupt --->\n");
1357 adapter->data_sent = false;
1361 /* As firmware will not generate download ready interrupt if the port
1362 updated is command port only, cmd_sent should be done for any SDIO
1363 interrupt. */
1364 if (card->has_control_mask && adapter->cmd_sent) {
1365 /* Check if firmware has attach buffer at command port and
1366 update just that in wr_bit_map. */
1367 card->mp_wr_bitmap |=
1368 (u32) card->mp_regs[reg->wr_bitmap_l] & CTRL_PORT_MASK;
1369 if (card->mp_wr_bitmap & CTRL_PORT_MASK)
1370 adapter->cmd_sent = false;
1373 dev_dbg(adapter->dev, "info: cmd_sent=%d data_sent=%d\n",
1374 adapter->cmd_sent, adapter->data_sent);
1375 if (sdio_ireg & UP_LD_HOST_INT_STATUS) {
1376 bitmap = (u32) card->mp_regs[reg->rd_bitmap_l];
1377 bitmap |= ((u32) card->mp_regs[reg->rd_bitmap_u]) << 8;
1378 if (card->supports_sdio_new_mode) {
1379 bitmap |=
1380 ((u32) card->mp_regs[reg->rd_bitmap_1l]) << 16;
1381 bitmap |=
1382 ((u32) card->mp_regs[reg->rd_bitmap_1u]) << 24;
1384 card->mp_rd_bitmap = bitmap;
1385 dev_dbg(adapter->dev, "int: UPLD: rd_bitmap=0x%x\n",
1386 card->mp_rd_bitmap);
1388 while (true) {
1389 ret = mwifiex_get_rd_port(adapter, &port);
1390 if (ret) {
1391 dev_dbg(adapter->dev,
1392 "info: no more rd_port available\n");
1393 break;
1395 len_reg_l = reg->rd_len_p0_l + (port << 1);
1396 len_reg_u = reg->rd_len_p0_u + (port << 1);
1397 rx_len = ((u16) card->mp_regs[len_reg_u]) << 8;
1398 rx_len |= (u16) card->mp_regs[len_reg_l];
1399 dev_dbg(adapter->dev, "info: RX: port=%d rx_len=%u\n",
1400 port, rx_len);
1401 rx_blocks =
1402 (rx_len + MWIFIEX_SDIO_BLOCK_SIZE -
1403 1) / MWIFIEX_SDIO_BLOCK_SIZE;
1404 if (rx_len <= INTF_HEADER_LEN ||
1405 (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1406 MWIFIEX_RX_DATA_BUF_SIZE) {
1407 dev_err(adapter->dev, "invalid rx_len=%d\n",
1408 rx_len);
1409 return -1;
1411 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1413 skb = dev_alloc_skb(rx_len);
1415 if (!skb) {
1416 dev_err(adapter->dev, "%s: failed to alloc skb",
1417 __func__);
1418 return -1;
1421 skb_put(skb, rx_len);
1423 dev_dbg(adapter->dev, "info: rx_len = %d skb->len = %d\n",
1424 rx_len, skb->len);
1426 if (mwifiex_sdio_card_to_host_mp_aggr(adapter, skb,
1427 port)) {
1428 dev_err(adapter->dev, "card_to_host_mpa failed:"
1429 " int status=%#x\n", sdio_ireg);
1430 goto term_cmd;
1435 return 0;
1437 term_cmd:
1438 /* terminate cmd */
1439 if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
1440 dev_err(adapter->dev, "read CFG reg failed\n");
1441 else
1442 dev_dbg(adapter->dev, "info: CFG reg val = %d\n", cr);
1444 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, (cr | 0x04)))
1445 dev_err(adapter->dev, "write CFG reg failed\n");
1446 else
1447 dev_dbg(adapter->dev, "info: write success\n");
1449 if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
1450 dev_err(adapter->dev, "read CFG reg failed\n");
1451 else
1452 dev_dbg(adapter->dev, "info: CFG reg val =%x\n", cr);
1454 return -1;
1458 * This function aggregates transmission buffers in driver and downloads
1459 * the aggregated packet to card.
1461 * The individual packets are aggregated by copying into an aggregation
1462 * buffer and then downloaded to the card. Previous unsent packets in the
1463 * aggregation buffer are pre-copied first before new packets are added.
1464 * Aggregation is done till there is space left in the aggregation buffer,
1465 * or till new packets are available.
1467 * The function will only download the packet to the card when aggregation
1468 * stops, otherwise it will just aggregate the packet in aggregation buffer
1469 * and return.
1471 static int mwifiex_host_to_card_mp_aggr(struct mwifiex_adapter *adapter,
1472 u8 *payload, u32 pkt_len, u32 port,
1473 u32 next_pkt_len)
1475 struct sdio_mmc_card *card = adapter->card;
1476 int ret = 0;
1477 s32 f_send_aggr_buf = 0;
1478 s32 f_send_cur_buf = 0;
1479 s32 f_precopy_cur_buf = 0;
1480 s32 f_postcopy_cur_buf = 0;
1481 u32 mport;
1483 if (!card->mpa_tx.enabled ||
1484 (card->has_control_mask && (port == CTRL_PORT)) ||
1485 (card->supports_sdio_new_mode && (port == CMD_PORT_SLCT))) {
1486 dev_dbg(adapter->dev, "info: %s: tx aggregation disabled\n",
1487 __func__);
1489 f_send_cur_buf = 1;
1490 goto tx_curr_single;
1493 if (next_pkt_len) {
1494 /* More pkt in TX queue */
1495 dev_dbg(adapter->dev, "info: %s: more packets in queue.\n",
1496 __func__);
1498 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1499 if (!mp_tx_aggr_port_limit_reached(card) &&
1500 MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len)) {
1501 f_precopy_cur_buf = 1;
1503 if (!(card->mp_wr_bitmap &
1504 (1 << card->curr_wr_port)) ||
1505 !MP_TX_AGGR_BUF_HAS_ROOM(
1506 card, pkt_len + next_pkt_len))
1507 f_send_aggr_buf = 1;
1508 } else {
1509 /* No room in Aggr buf, send it */
1510 f_send_aggr_buf = 1;
1512 if (mp_tx_aggr_port_limit_reached(card) ||
1513 !(card->mp_wr_bitmap &
1514 (1 << card->curr_wr_port)))
1515 f_send_cur_buf = 1;
1516 else
1517 f_postcopy_cur_buf = 1;
1519 } else {
1520 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len) &&
1521 (card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1522 f_precopy_cur_buf = 1;
1523 else
1524 f_send_cur_buf = 1;
1526 } else {
1527 /* Last pkt in TX queue */
1528 dev_dbg(adapter->dev, "info: %s: Last packet in Tx Queue.\n",
1529 __func__);
1531 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1532 /* some packs in Aggr buf already */
1533 f_send_aggr_buf = 1;
1535 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len))
1536 f_precopy_cur_buf = 1;
1537 else
1538 /* No room in Aggr buf, send it */
1539 f_send_cur_buf = 1;
1540 } else {
1541 f_send_cur_buf = 1;
1545 if (f_precopy_cur_buf) {
1546 dev_dbg(adapter->dev, "data: %s: precopy current buffer\n",
1547 __func__);
1548 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1550 if (MP_TX_AGGR_PKT_LIMIT_REACHED(card) ||
1551 mp_tx_aggr_port_limit_reached(card))
1552 /* No more pkts allowed in Aggr buf, send it */
1553 f_send_aggr_buf = 1;
1556 if (f_send_aggr_buf) {
1557 dev_dbg(adapter->dev, "data: %s: send aggr buffer: %d %d\n",
1558 __func__,
1559 card->mpa_tx.start_port, card->mpa_tx.ports);
1560 if (card->supports_sdio_new_mode) {
1561 u32 port_count;
1562 int i;
1564 for (i = 0, port_count = 0; i < card->max_ports; i++)
1565 if (card->mpa_tx.ports & BIT(i))
1566 port_count++;
1568 /* Writing data from "start_port + 0" to "start_port +
1569 * port_count -1", so decrease the count by 1
1571 port_count--;
1572 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1573 (port_count << 8)) + card->mpa_tx.start_port;
1574 } else {
1575 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1576 (card->mpa_tx.ports << 4)) +
1577 card->mpa_tx.start_port;
1580 ret = mwifiex_write_data_to_card(adapter, card->mpa_tx.buf,
1581 card->mpa_tx.buf_len, mport);
1583 MP_TX_AGGR_BUF_RESET(card);
1586 tx_curr_single:
1587 if (f_send_cur_buf) {
1588 dev_dbg(adapter->dev, "data: %s: send current buffer %d\n",
1589 __func__, port);
1590 ret = mwifiex_write_data_to_card(adapter, payload, pkt_len,
1591 adapter->ioport + port);
1594 if (f_postcopy_cur_buf) {
1595 dev_dbg(adapter->dev, "data: %s: postcopy current buffer\n",
1596 __func__);
1597 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1600 return ret;
1604 * This function downloads data from driver to card.
1606 * Both commands and data packets are transferred to the card by this
1607 * function.
1609 * This function adds the SDIO specific header to the front of the buffer
1610 * before transferring. The header contains the length of the packet and
1611 * the type. The firmware handles the packets based upon this set type.
1613 static int mwifiex_sdio_host_to_card(struct mwifiex_adapter *adapter,
1614 u8 type, struct sk_buff *skb,
1615 struct mwifiex_tx_param *tx_param)
1617 struct sdio_mmc_card *card = adapter->card;
1618 int ret;
1619 u32 buf_block_len;
1620 u32 blk_size;
1621 u32 port = CTRL_PORT;
1622 u8 *payload = (u8 *)skb->data;
1623 u32 pkt_len = skb->len;
1625 /* Allocate buffer and copy payload */
1626 blk_size = MWIFIEX_SDIO_BLOCK_SIZE;
1627 buf_block_len = (pkt_len + blk_size - 1) / blk_size;
1628 *(u16 *) &payload[0] = (u16) pkt_len;
1629 *(u16 *) &payload[2] = type;
1632 * This is SDIO specific header
1633 * u16 length,
1634 * u16 type (MWIFIEX_TYPE_DATA = 0, MWIFIEX_TYPE_CMD = 1,
1635 * MWIFIEX_TYPE_EVENT = 3)
1637 if (type == MWIFIEX_TYPE_DATA) {
1638 ret = mwifiex_get_wr_port_data(adapter, &port);
1639 if (ret) {
1640 dev_err(adapter->dev, "%s: no wr_port available\n",
1641 __func__);
1642 return ret;
1644 } else {
1645 adapter->cmd_sent = true;
1646 /* Type must be MWIFIEX_TYPE_CMD */
1648 if (pkt_len <= INTF_HEADER_LEN ||
1649 pkt_len > MWIFIEX_UPLD_SIZE)
1650 dev_err(adapter->dev, "%s: payload=%p, nb=%d\n",
1651 __func__, payload, pkt_len);
1653 if (card->supports_sdio_new_mode)
1654 port = CMD_PORT_SLCT;
1657 /* Transfer data to card */
1658 pkt_len = buf_block_len * blk_size;
1660 if (tx_param)
1661 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1662 port, tx_param->next_pkt_len
1664 else
1665 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1666 port, 0);
1668 if (ret) {
1669 if (type == MWIFIEX_TYPE_CMD)
1670 adapter->cmd_sent = false;
1671 if (type == MWIFIEX_TYPE_DATA)
1672 adapter->data_sent = false;
1673 } else {
1674 if (type == MWIFIEX_TYPE_DATA) {
1675 if (!(card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1676 adapter->data_sent = true;
1677 else
1678 adapter->data_sent = false;
1682 return ret;
1686 * This function allocates the MPA Tx and Rx buffers.
1688 static int mwifiex_alloc_sdio_mpa_buffers(struct mwifiex_adapter *adapter,
1689 u32 mpa_tx_buf_size, u32 mpa_rx_buf_size)
1691 struct sdio_mmc_card *card = adapter->card;
1692 int ret = 0;
1694 card->mpa_tx.buf = kzalloc(mpa_tx_buf_size, GFP_KERNEL);
1695 if (!card->mpa_tx.buf) {
1696 ret = -1;
1697 goto error;
1700 card->mpa_tx.buf_size = mpa_tx_buf_size;
1702 card->mpa_rx.buf = kzalloc(mpa_rx_buf_size, GFP_KERNEL);
1703 if (!card->mpa_rx.buf) {
1704 ret = -1;
1705 goto error;
1708 card->mpa_rx.buf_size = mpa_rx_buf_size;
1710 error:
1711 if (ret) {
1712 kfree(card->mpa_tx.buf);
1713 kfree(card->mpa_rx.buf);
1716 return ret;
1720 * This function unregisters the SDIO device.
1722 * The SDIO IRQ is released, the function is disabled and driver
1723 * data is set to null.
1725 static void
1726 mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
1728 struct sdio_mmc_card *card = adapter->card;
1730 if (adapter->card) {
1731 /* Release the SDIO IRQ */
1732 sdio_claim_host(card->func);
1733 sdio_release_irq(card->func);
1734 sdio_disable_func(card->func);
1735 sdio_release_host(card->func);
1736 sdio_set_drvdata(card->func, NULL);
1741 * This function registers the SDIO device.
1743 * SDIO IRQ is claimed, block size is set and driver data is initialized.
1745 static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
1747 int ret = 0;
1748 struct sdio_mmc_card *card = adapter->card;
1749 struct sdio_func *func = card->func;
1751 /* save adapter pointer in card */
1752 card->adapter = adapter;
1754 sdio_claim_host(func);
1756 /* Request the SDIO IRQ */
1757 ret = sdio_claim_irq(func, mwifiex_sdio_interrupt);
1758 if (ret) {
1759 pr_err("claim irq failed: ret=%d\n", ret);
1760 goto disable_func;
1763 /* Set block size */
1764 ret = sdio_set_block_size(card->func, MWIFIEX_SDIO_BLOCK_SIZE);
1765 if (ret) {
1766 pr_err("cannot set SDIO block size\n");
1767 ret = -1;
1768 goto release_irq;
1771 sdio_release_host(func);
1772 sdio_set_drvdata(func, card);
1774 adapter->dev = &func->dev;
1776 strcpy(adapter->fw_name, card->firmware);
1778 return 0;
1780 release_irq:
1781 sdio_release_irq(func);
1782 disable_func:
1783 sdio_disable_func(func);
1784 sdio_release_host(func);
1785 adapter->card = NULL;
1787 return -1;
1791 * This function initializes the SDIO driver.
1793 * The following initializations steps are followed -
1794 * - Read the Host interrupt status register to acknowledge
1795 * the first interrupt got from bootloader
1796 * - Disable host interrupt mask register
1797 * - Get SDIO port
1798 * - Initialize SDIO variables in card
1799 * - Allocate MP registers
1800 * - Allocate MPA Tx and Rx buffers
1802 static int mwifiex_init_sdio(struct mwifiex_adapter *adapter)
1804 struct sdio_mmc_card *card = adapter->card;
1805 const struct mwifiex_sdio_card_reg *reg = card->reg;
1806 int ret;
1807 u8 sdio_ireg;
1810 * Read the HOST_INT_STATUS_REG for ACK the first interrupt got
1811 * from the bootloader. If we don't do this we get a interrupt
1812 * as soon as we register the irq.
1814 mwifiex_read_reg(adapter, HOST_INTSTATUS_REG, &sdio_ireg);
1816 /* Disable host interrupt mask register for SDIO */
1817 mwifiex_sdio_disable_host_int(adapter);
1819 /* Get SDIO ioport */
1820 mwifiex_init_sdio_ioport(adapter);
1822 /* Initialize SDIO variables in card */
1823 card->mp_rd_bitmap = 0;
1824 card->mp_wr_bitmap = 0;
1825 card->curr_rd_port = reg->start_rd_port;
1826 card->curr_wr_port = reg->start_wr_port;
1828 card->mp_data_port_mask = reg->data_port_mask;
1830 card->mpa_tx.buf_len = 0;
1831 card->mpa_tx.pkt_cnt = 0;
1832 card->mpa_tx.start_port = 0;
1834 card->mpa_tx.enabled = 1;
1835 card->mpa_tx.pkt_aggr_limit = card->mp_agg_pkt_limit;
1837 card->mpa_rx.buf_len = 0;
1838 card->mpa_rx.pkt_cnt = 0;
1839 card->mpa_rx.start_port = 0;
1841 card->mpa_rx.enabled = 1;
1842 card->mpa_rx.pkt_aggr_limit = card->mp_agg_pkt_limit;
1844 /* Allocate buffers for SDIO MP-A */
1845 card->mp_regs = kzalloc(reg->max_mp_regs, GFP_KERNEL);
1846 if (!card->mp_regs)
1847 return -ENOMEM;
1849 /* Allocate skb pointer buffers */
1850 card->mpa_rx.skb_arr = kzalloc((sizeof(void *)) *
1851 card->mp_agg_pkt_limit, GFP_KERNEL);
1852 card->mpa_rx.len_arr = kzalloc(sizeof(*card->mpa_rx.len_arr) *
1853 card->mp_agg_pkt_limit, GFP_KERNEL);
1854 ret = mwifiex_alloc_sdio_mpa_buffers(adapter,
1855 SDIO_MP_TX_AGGR_DEF_BUF_SIZE,
1856 SDIO_MP_RX_AGGR_DEF_BUF_SIZE);
1857 if (ret) {
1858 dev_err(adapter->dev, "failed to alloc sdio mp-a buffers\n");
1859 kfree(card->mp_regs);
1860 return -1;
1863 return ret;
1867 * This function resets the MPA Tx and Rx buffers.
1869 static void mwifiex_cleanup_mpa_buf(struct mwifiex_adapter *adapter)
1871 struct sdio_mmc_card *card = adapter->card;
1873 MP_TX_AGGR_BUF_RESET(card);
1874 MP_RX_AGGR_BUF_RESET(card);
1878 * This function cleans up the allocated card buffers.
1880 * The following are freed by this function -
1881 * - MP registers
1882 * - MPA Tx buffer
1883 * - MPA Rx buffer
1885 static void mwifiex_cleanup_sdio(struct mwifiex_adapter *adapter)
1887 struct sdio_mmc_card *card = adapter->card;
1889 kfree(card->mp_regs);
1890 kfree(card->mpa_rx.skb_arr);
1891 kfree(card->mpa_rx.len_arr);
1892 kfree(card->mpa_tx.buf);
1893 kfree(card->mpa_rx.buf);
1897 * This function updates the MP end port in card.
1899 static void
1900 mwifiex_update_mp_end_port(struct mwifiex_adapter *adapter, u16 port)
1902 struct sdio_mmc_card *card = adapter->card;
1903 const struct mwifiex_sdio_card_reg *reg = card->reg;
1904 int i;
1906 card->mp_end_port = port;
1908 card->mp_data_port_mask = reg->data_port_mask;
1910 if (reg->start_wr_port) {
1911 for (i = 1; i <= card->max_ports - card->mp_end_port; i++)
1912 card->mp_data_port_mask &=
1913 ~(1 << (card->max_ports - i));
1916 card->curr_wr_port = reg->start_wr_port;
1918 dev_dbg(adapter->dev, "cmd: mp_end_port %d, data port mask 0x%x\n",
1919 port, card->mp_data_port_mask);
1922 static struct mmc_host *reset_host;
1923 static void sdio_card_reset_worker(struct work_struct *work)
1925 struct mmc_host *target = reset_host;
1927 /* The actual reset operation must be run outside of driver thread.
1928 * This is because mmc_remove_host() will cause the device to be
1929 * instantly destroyed, and the driver then needs to end its thread,
1930 * leading to a deadlock.
1932 * We run it in a totally independent workqueue.
1935 pr_err("Resetting card...\n");
1936 mmc_remove_host(target);
1937 /* 20ms delay is based on experiment with sdhci controller */
1938 mdelay(20);
1939 mmc_add_host(target);
1941 static DECLARE_WORK(card_reset_work, sdio_card_reset_worker);
1943 /* This function resets the card */
1944 static void mwifiex_sdio_card_reset(struct mwifiex_adapter *adapter)
1946 struct sdio_mmc_card *card = adapter->card;
1948 reset_host = card->func->card->host;
1949 schedule_work(&card_reset_work);
1952 static struct mwifiex_if_ops sdio_ops = {
1953 .init_if = mwifiex_init_sdio,
1954 .cleanup_if = mwifiex_cleanup_sdio,
1955 .check_fw_status = mwifiex_check_fw_status,
1956 .prog_fw = mwifiex_prog_fw_w_helper,
1957 .register_dev = mwifiex_register_dev,
1958 .unregister_dev = mwifiex_unregister_dev,
1959 .enable_int = mwifiex_sdio_enable_host_int,
1960 .process_int_status = mwifiex_process_int_status,
1961 .host_to_card = mwifiex_sdio_host_to_card,
1962 .wakeup = mwifiex_pm_wakeup_card,
1963 .wakeup_complete = mwifiex_pm_wakeup_card_complete,
1965 /* SDIO specific */
1966 .update_mp_end_port = mwifiex_update_mp_end_port,
1967 .cleanup_mpa_buf = mwifiex_cleanup_mpa_buf,
1968 .cmdrsp_complete = mwifiex_sdio_cmdrsp_complete,
1969 .event_complete = mwifiex_sdio_event_complete,
1970 .card_reset = mwifiex_sdio_card_reset,
1974 * This function initializes the SDIO driver.
1976 * This initiates the semaphore and registers the device with
1977 * SDIO bus.
1979 static int
1980 mwifiex_sdio_init_module(void)
1982 sema_init(&add_remove_card_sem, 1);
1984 /* Clear the flag in case user removes the card. */
1985 user_rmmod = 0;
1987 return sdio_register_driver(&mwifiex_sdio);
1991 * This function cleans up the SDIO driver.
1993 * The following major steps are followed for cleanup -
1994 * - Resume the device if its suspended
1995 * - Disconnect the device if connected
1996 * - Shutdown the firmware
1997 * - Unregister the device from SDIO bus.
1999 static void
2000 mwifiex_sdio_cleanup_module(void)
2002 if (!down_interruptible(&add_remove_card_sem))
2003 up(&add_remove_card_sem);
2005 /* Set the flag as user is removing this module. */
2006 user_rmmod = 1;
2008 cancel_work_sync(&card_reset_work);
2009 sdio_unregister_driver(&mwifiex_sdio);
2012 module_init(mwifiex_sdio_init_module);
2013 module_exit(mwifiex_sdio_cleanup_module);
2015 MODULE_AUTHOR("Marvell International Ltd.");
2016 MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION);
2017 MODULE_VERSION(SDIO_VERSION);
2018 MODULE_LICENSE("GPL v2");
2019 MODULE_FIRMWARE(SD8786_DEFAULT_FW_NAME);
2020 MODULE_FIRMWARE(SD8787_DEFAULT_FW_NAME);
2021 MODULE_FIRMWARE(SD8797_DEFAULT_FW_NAME);
2022 MODULE_FIRMWARE(SD8897_DEFAULT_FW_NAME);