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