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