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