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