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