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