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