mwifiex: minor cleanup in multiport aggregation
[sfrench/cifs-2.6.git] / drivers / net / wireless / mwifiex / sdio.c
1 /*
2  * Marvell Wireless LAN device driver: SDIO specific handling
3  *
4  * Copyright (C) 2011-2014, 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 struct memory_type_mapping mem_type_mapping_tbl[] = {
54         {"ITCM", NULL, 0, 0xF0},
55         {"DTCM", NULL, 0, 0xF1},
56         {"SQRAM", NULL, 0, 0xF2},
57         {"APU", NULL, 0, 0xF3},
58         {"CIU", NULL, 0, 0xF4},
59         {"ICU", NULL, 0, 0xF5},
60         {"MAC", NULL, 0, 0xF6},
61         {"EXT7", NULL, 0, 0xF7},
62         {"EXT8", NULL, 0, 0xF8},
63         {"EXT9", NULL, 0, 0xF9},
64         {"EXT10", NULL, 0, 0xFA},
65         {"EXT11", NULL, 0, 0xFB},
66         {"EXT12", NULL, 0, 0xFC},
67         {"EXT13", NULL, 0, 0xFD},
68         {"EXTLAST", NULL, 0, 0xFE},
69 };
70
71 /*
72  * SDIO probe.
73  *
74  * This function probes an mwifiex device and registers it. It allocates
75  * the card structure, enables SDIO function number and initiates the
76  * device registration and initialization procedure by adding a logical
77  * interface.
78  */
79 static int
80 mwifiex_sdio_probe(struct sdio_func *func, const struct sdio_device_id *id)
81 {
82         int ret;
83         struct sdio_mmc_card *card = NULL;
84
85         pr_debug("info: vendor=0x%4.04X device=0x%4.04X class=%d function=%d\n",
86                  func->vendor, func->device, func->class, func->num);
87
88         card = kzalloc(sizeof(struct sdio_mmc_card), GFP_KERNEL);
89         if (!card)
90                 return -ENOMEM;
91
92         card->func = func;
93
94         func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
95
96         if (id->driver_data) {
97                 struct mwifiex_sdio_device *data = (void *)id->driver_data;
98
99                 card->firmware = data->firmware;
100                 card->reg = data->reg;
101                 card->max_ports = data->max_ports;
102                 card->mp_agg_pkt_limit = data->mp_agg_pkt_limit;
103                 card->supports_sdio_new_mode = data->supports_sdio_new_mode;
104                 card->has_control_mask = data->has_control_mask;
105                 card->tx_buf_size = data->tx_buf_size;
106                 card->mp_tx_agg_buf_size = data->mp_tx_agg_buf_size;
107                 card->mp_rx_agg_buf_size = data->mp_rx_agg_buf_size;
108                 card->supports_fw_dump = data->supports_fw_dump;
109         }
110
111         sdio_claim_host(func);
112         ret = sdio_enable_func(func);
113         sdio_release_host(func);
114
115         if (ret) {
116                 pr_err("%s: failed to enable function\n", __func__);
117                 kfree(card);
118                 return -EIO;
119         }
120
121         if (mwifiex_add_card(card, &add_remove_card_sem, &sdio_ops,
122                              MWIFIEX_SDIO)) {
123                 pr_err("%s: add card failed\n", __func__);
124                 kfree(card);
125                 sdio_claim_host(func);
126                 ret = sdio_disable_func(func);
127                 sdio_release_host(func);
128                 ret = -1;
129         }
130
131         return ret;
132 }
133
134 /*
135  * SDIO resume.
136  *
137  * Kernel needs to suspend all functions separately. Therefore all
138  * registered functions must have drivers with suspend and resume
139  * methods. Failing that the kernel simply removes the whole card.
140  *
141  * If already not resumed, this function turns on the traffic and
142  * sends a host sleep cancel request to the firmware.
143  */
144 static int mwifiex_sdio_resume(struct device *dev)
145 {
146         struct sdio_func *func = dev_to_sdio_func(dev);
147         struct sdio_mmc_card *card;
148         struct mwifiex_adapter *adapter;
149         mmc_pm_flag_t pm_flag = 0;
150
151         if (func) {
152                 pm_flag = sdio_get_host_pm_caps(func);
153                 card = sdio_get_drvdata(func);
154                 if (!card || !card->adapter) {
155                         pr_err("resume: invalid card or adapter\n");
156                         return 0;
157                 }
158         } else {
159                 pr_err("resume: sdio_func is not specified\n");
160                 return 0;
161         }
162
163         adapter = card->adapter;
164
165         if (!adapter->is_suspended) {
166                 dev_warn(adapter->dev, "device already resumed\n");
167                 return 0;
168         }
169
170         adapter->is_suspended = false;
171
172         /* Disable Host Sleep */
173         mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
174                           MWIFIEX_ASYNC_CMD);
175
176         return 0;
177 }
178
179 /*
180  * SDIO remove.
181  *
182  * This function removes the interface and frees up the card structure.
183  */
184 static void
185 mwifiex_sdio_remove(struct sdio_func *func)
186 {
187         struct sdio_mmc_card *card;
188         struct mwifiex_adapter *adapter;
189         struct mwifiex_private *priv;
190
191         pr_debug("info: SDIO func num=%d\n", func->num);
192
193         card = sdio_get_drvdata(func);
194         if (!card)
195                 return;
196
197         adapter = card->adapter;
198         if (!adapter || !adapter->priv_num)
199                 return;
200
201         cancel_work_sync(&adapter->iface_work);
202
203         if (user_rmmod) {
204                 if (adapter->is_suspended)
205                         mwifiex_sdio_resume(adapter->dev);
206
207                 mwifiex_deauthenticate_all(adapter);
208
209                 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
210                 mwifiex_disable_auto_ds(priv);
211                 mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
212         }
213
214         mwifiex_remove_card(card->adapter, &add_remove_card_sem);
215 }
216
217 /*
218  * SDIO suspend.
219  *
220  * Kernel needs to suspend all functions separately. Therefore all
221  * registered functions must have drivers with suspend and resume
222  * methods. Failing that the kernel simply removes the whole card.
223  *
224  * If already not suspended, this function allocates and sends a host
225  * sleep activate request to the firmware and turns off the traffic.
226  */
227 static int mwifiex_sdio_suspend(struct device *dev)
228 {
229         struct sdio_func *func = dev_to_sdio_func(dev);
230         struct sdio_mmc_card *card;
231         struct mwifiex_adapter *adapter;
232         mmc_pm_flag_t pm_flag = 0;
233         int ret = 0;
234
235         if (func) {
236                 pm_flag = sdio_get_host_pm_caps(func);
237                 pr_debug("cmd: %s: suspend: PM flag = 0x%x\n",
238                          sdio_func_id(func), pm_flag);
239                 if (!(pm_flag & MMC_PM_KEEP_POWER)) {
240                         pr_err("%s: cannot remain alive while host is"
241                                 " suspended\n", sdio_func_id(func));
242                         return -ENOSYS;
243                 }
244
245                 card = sdio_get_drvdata(func);
246                 if (!card || !card->adapter) {
247                         pr_err("suspend: invalid card or adapter\n");
248                         return 0;
249                 }
250         } else {
251                 pr_err("suspend: sdio_func is not specified\n");
252                 return 0;
253         }
254
255         adapter = card->adapter;
256
257         /* Enable the Host Sleep */
258         if (!mwifiex_enable_hs(adapter)) {
259                 dev_err(adapter->dev, "cmd: failed to suspend\n");
260                 adapter->hs_enabling = false;
261                 return -EFAULT;
262         }
263
264         dev_dbg(adapter->dev, "cmd: suspend with MMC_PM_KEEP_POWER\n");
265         ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
266
267         /* Indicate device suspended */
268         adapter->is_suspended = true;
269         adapter->hs_enabling = false;
270
271         return ret;
272 }
273
274 /* Device ID for SD8786 */
275 #define SDIO_DEVICE_ID_MARVELL_8786   (0x9116)
276 /* Device ID for SD8787 */
277 #define SDIO_DEVICE_ID_MARVELL_8787   (0x9119)
278 /* Device ID for SD8797 */
279 #define SDIO_DEVICE_ID_MARVELL_8797   (0x9129)
280 /* Device ID for SD8897 */
281 #define SDIO_DEVICE_ID_MARVELL_8897   (0x912d)
282
283 /* WLAN IDs */
284 static const struct sdio_device_id mwifiex_ids[] = {
285         {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8786),
286                 .driver_data = (unsigned long) &mwifiex_sdio_sd8786},
287         {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787),
288                 .driver_data = (unsigned long) &mwifiex_sdio_sd8787},
289         {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8797),
290                 .driver_data = (unsigned long) &mwifiex_sdio_sd8797},
291         {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8897),
292                 .driver_data = (unsigned long) &mwifiex_sdio_sd8897},
293         {},
294 };
295
296 MODULE_DEVICE_TABLE(sdio, mwifiex_ids);
297
298 static const struct dev_pm_ops mwifiex_sdio_pm_ops = {
299         .suspend = mwifiex_sdio_suspend,
300         .resume = mwifiex_sdio_resume,
301 };
302
303 static struct sdio_driver mwifiex_sdio = {
304         .name = "mwifiex_sdio",
305         .id_table = mwifiex_ids,
306         .probe = mwifiex_sdio_probe,
307         .remove = mwifiex_sdio_remove,
308         .drv = {
309                 .owner = THIS_MODULE,
310                 .pm = &mwifiex_sdio_pm_ops,
311         }
312 };
313
314 /* Write data into SDIO card register. Caller claims SDIO device. */
315 static int
316 mwifiex_write_reg_locked(struct sdio_func *func, u32 reg, u8 data)
317 {
318         int ret = -1;
319         sdio_writeb(func, data, reg, &ret);
320         return ret;
321 }
322
323 /*
324  * This function writes data into SDIO card register.
325  */
326 static int
327 mwifiex_write_reg(struct mwifiex_adapter *adapter, u32 reg, u8 data)
328 {
329         struct sdio_mmc_card *card = adapter->card;
330         int ret;
331
332         sdio_claim_host(card->func);
333         ret = mwifiex_write_reg_locked(card->func, reg, data);
334         sdio_release_host(card->func);
335
336         return ret;
337 }
338
339 /*
340  * This function reads data from SDIO card register.
341  */
342 static int
343 mwifiex_read_reg(struct mwifiex_adapter *adapter, u32 reg, u8 *data)
344 {
345         struct sdio_mmc_card *card = adapter->card;
346         int ret = -1;
347         u8 val;
348
349         sdio_claim_host(card->func);
350         val = sdio_readb(card->func, reg, &ret);
351         sdio_release_host(card->func);
352
353         *data = val;
354
355         return ret;
356 }
357
358 /*
359  * This function writes multiple data into SDIO card memory.
360  *
361  * This does not work in suspended mode.
362  */
363 static int
364 mwifiex_write_data_sync(struct mwifiex_adapter *adapter,
365                         u8 *buffer, u32 pkt_len, u32 port)
366 {
367         struct sdio_mmc_card *card = adapter->card;
368         int ret;
369         u8 blk_mode =
370                 (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE : BLOCK_MODE;
371         u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
372         u32 blk_cnt =
373                 (blk_mode ==
374                  BLOCK_MODE) ? (pkt_len /
375                                 MWIFIEX_SDIO_BLOCK_SIZE) : pkt_len;
376         u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
377
378         if (adapter->is_suspended) {
379                 dev_err(adapter->dev,
380                         "%s: not allowed while suspended\n", __func__);
381                 return -1;
382         }
383
384         sdio_claim_host(card->func);
385
386         ret = sdio_writesb(card->func, ioport, buffer, blk_cnt * blk_size);
387
388         sdio_release_host(card->func);
389
390         return ret;
391 }
392
393 /*
394  * This function reads multiple data from SDIO card memory.
395  */
396 static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *buffer,
397                                   u32 len, u32 port, u8 claim)
398 {
399         struct sdio_mmc_card *card = adapter->card;
400         int ret;
401         u8 blk_mode = (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE
402                        : BLOCK_MODE;
403         u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
404         u32 blk_cnt = (blk_mode == BLOCK_MODE) ? (len / MWIFIEX_SDIO_BLOCK_SIZE)
405                         : len;
406         u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
407
408         if (claim)
409                 sdio_claim_host(card->func);
410
411         ret = sdio_readsb(card->func, buffer, ioport, blk_cnt * blk_size);
412
413         if (claim)
414                 sdio_release_host(card->func);
415
416         return ret;
417 }
418
419 /*
420  * This function wakes up the card.
421  *
422  * A host power up command is written to the card configuration
423  * register to wake up the card.
424  */
425 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
426 {
427         dev_dbg(adapter->dev, "event: wakeup device...\n");
428
429         return mwifiex_write_reg(adapter, CONFIGURATION_REG, HOST_POWER_UP);
430 }
431
432 /*
433  * This function is called after the card has woken up.
434  *
435  * The card configuration register is reset.
436  */
437 static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
438 {
439         dev_dbg(adapter->dev, "cmd: wakeup device completed\n");
440
441         return mwifiex_write_reg(adapter, CONFIGURATION_REG, 0);
442 }
443
444 /*
445  * This function is used to initialize IO ports for the
446  * chipsets supporting SDIO new mode eg SD8897.
447  */
448 static int mwifiex_init_sdio_new_mode(struct mwifiex_adapter *adapter)
449 {
450         u8 reg;
451
452         adapter->ioport = MEM_PORT;
453
454         /* enable sdio new mode */
455         if (mwifiex_read_reg(adapter, CARD_CONFIG_2_1_REG, &reg))
456                 return -1;
457         if (mwifiex_write_reg(adapter, CARD_CONFIG_2_1_REG,
458                               reg | CMD53_NEW_MODE))
459                 return -1;
460
461         /* Configure cmd port and enable reading rx length from the register */
462         if (mwifiex_read_reg(adapter, CMD_CONFIG_0, &reg))
463                 return -1;
464         if (mwifiex_write_reg(adapter, CMD_CONFIG_0, reg | CMD_PORT_RD_LEN_EN))
465                 return -1;
466
467         /* Enable Dnld/Upld ready auto reset for cmd port after cmd53 is
468          * completed
469          */
470         if (mwifiex_read_reg(adapter, CMD_CONFIG_1, &reg))
471                 return -1;
472         if (mwifiex_write_reg(adapter, CMD_CONFIG_1, reg | CMD_PORT_AUTO_EN))
473                 return -1;
474
475         return 0;
476 }
477
478 /* This function initializes the IO ports.
479  *
480  * The following operations are performed -
481  *      - Read the IO ports (0, 1 and 2)
482  *      - Set host interrupt Reset-To-Read to clear
483  *      - Set auto re-enable interrupt
484  */
485 static int mwifiex_init_sdio_ioport(struct mwifiex_adapter *adapter)
486 {
487         u8 reg;
488         struct sdio_mmc_card *card = adapter->card;
489
490         adapter->ioport = 0;
491
492         if (card->supports_sdio_new_mode) {
493                 if (mwifiex_init_sdio_new_mode(adapter))
494                         return -1;
495                 goto cont;
496         }
497
498         /* Read the IO port */
499         if (!mwifiex_read_reg(adapter, IO_PORT_0_REG, &reg))
500                 adapter->ioport |= (reg & 0xff);
501         else
502                 return -1;
503
504         if (!mwifiex_read_reg(adapter, IO_PORT_1_REG, &reg))
505                 adapter->ioport |= ((reg & 0xff) << 8);
506         else
507                 return -1;
508
509         if (!mwifiex_read_reg(adapter, IO_PORT_2_REG, &reg))
510                 adapter->ioport |= ((reg & 0xff) << 16);
511         else
512                 return -1;
513 cont:
514         pr_debug("info: SDIO FUNC1 IO port: %#x\n", adapter->ioport);
515
516         /* Set Host interrupt reset to read to clear */
517         if (!mwifiex_read_reg(adapter, HOST_INT_RSR_REG, &reg))
518                 mwifiex_write_reg(adapter, HOST_INT_RSR_REG,
519                                   reg | card->reg->sdio_int_mask);
520         else
521                 return -1;
522
523         /* Dnld/Upld ready set to auto reset */
524         if (!mwifiex_read_reg(adapter, card->reg->card_misc_cfg_reg, &reg))
525                 mwifiex_write_reg(adapter, card->reg->card_misc_cfg_reg,
526                                   reg | AUTO_RE_ENABLE_INT);
527         else
528                 return -1;
529
530         return 0;
531 }
532
533 /*
534  * This function sends data to the card.
535  */
536 static int mwifiex_write_data_to_card(struct mwifiex_adapter *adapter,
537                                       u8 *payload, u32 pkt_len, u32 port)
538 {
539         u32 i = 0;
540         int ret;
541
542         do {
543                 ret = mwifiex_write_data_sync(adapter, payload, pkt_len, port);
544                 if (ret) {
545                         i++;
546                         dev_err(adapter->dev, "host_to_card, write iomem"
547                                         " (%d) failed: %d\n", i, ret);
548                         if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
549                                 dev_err(adapter->dev, "write CFG reg failed\n");
550
551                         ret = -1;
552                         if (i > MAX_WRITE_IOMEM_RETRY)
553                                 return ret;
554                 }
555         } while (ret == -1);
556
557         return ret;
558 }
559
560 /*
561  * This function gets the read port.
562  *
563  * If control port bit is set in MP read bitmap, the control port
564  * is returned, otherwise the current read port is returned and
565  * the value is increased (provided it does not reach the maximum
566  * limit, in which case it is reset to 1)
567  */
568 static int mwifiex_get_rd_port(struct mwifiex_adapter *adapter, u8 *port)
569 {
570         struct sdio_mmc_card *card = adapter->card;
571         const struct mwifiex_sdio_card_reg *reg = card->reg;
572         u32 rd_bitmap = card->mp_rd_bitmap;
573
574         dev_dbg(adapter->dev, "data: mp_rd_bitmap=0x%08x\n", rd_bitmap);
575
576         if (card->supports_sdio_new_mode) {
577                 if (!(rd_bitmap & reg->data_port_mask))
578                         return -1;
579         } else {
580                 if (!(rd_bitmap & (CTRL_PORT_MASK | reg->data_port_mask)))
581                         return -1;
582         }
583
584         if ((card->has_control_mask) &&
585             (card->mp_rd_bitmap & CTRL_PORT_MASK)) {
586                 card->mp_rd_bitmap &= (u32) (~CTRL_PORT_MASK);
587                 *port = CTRL_PORT;
588                 dev_dbg(adapter->dev, "data: port=%d mp_rd_bitmap=0x%08x\n",
589                         *port, card->mp_rd_bitmap);
590                 return 0;
591         }
592
593         if (!(card->mp_rd_bitmap & (1 << card->curr_rd_port)))
594                 return -1;
595
596         /* We are now handling the SDIO data ports */
597         card->mp_rd_bitmap &= (u32)(~(1 << card->curr_rd_port));
598         *port = card->curr_rd_port;
599
600         if (++card->curr_rd_port == card->max_ports)
601                 card->curr_rd_port = reg->start_rd_port;
602
603         dev_dbg(adapter->dev,
604                 "data: port=%d mp_rd_bitmap=0x%08x -> 0x%08x\n",
605                 *port, rd_bitmap, card->mp_rd_bitmap);
606
607         return 0;
608 }
609
610 /*
611  * This function gets the write port for data.
612  *
613  * The current write port is returned if available and the value is
614  * increased (provided it does not reach the maximum limit, in which
615  * case it is reset to 1)
616  */
617 static int mwifiex_get_wr_port_data(struct mwifiex_adapter *adapter, u32 *port)
618 {
619         struct sdio_mmc_card *card = adapter->card;
620         const struct mwifiex_sdio_card_reg *reg = card->reg;
621         u32 wr_bitmap = card->mp_wr_bitmap;
622
623         dev_dbg(adapter->dev, "data: mp_wr_bitmap=0x%08x\n", wr_bitmap);
624
625         if (!(wr_bitmap & card->mp_data_port_mask)) {
626                 adapter->data_sent = true;
627                 return -EBUSY;
628         }
629
630         if (card->mp_wr_bitmap & (1 << card->curr_wr_port)) {
631                 card->mp_wr_bitmap &= (u32) (~(1 << card->curr_wr_port));
632                 *port = card->curr_wr_port;
633                 if (++card->curr_wr_port == card->mp_end_port)
634                         card->curr_wr_port = reg->start_wr_port;
635         } else {
636                 adapter->data_sent = true;
637                 return -EBUSY;
638         }
639
640         if ((card->has_control_mask) && (*port == CTRL_PORT)) {
641                 dev_err(adapter->dev,
642                         "invalid data port=%d cur port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
643                         *port, card->curr_wr_port, wr_bitmap,
644                         card->mp_wr_bitmap);
645                 return -1;
646         }
647
648         dev_dbg(adapter->dev, "data: port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
649                 *port, wr_bitmap, card->mp_wr_bitmap);
650
651         return 0;
652 }
653
654 /*
655  * This function polls the card status.
656  */
657 static int
658 mwifiex_sdio_poll_card_status(struct mwifiex_adapter *adapter, u8 bits)
659 {
660         struct sdio_mmc_card *card = adapter->card;
661         u32 tries;
662         u8 cs;
663
664         for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
665                 if (mwifiex_read_reg(adapter, card->reg->poll_reg, &cs))
666                         break;
667                 else if ((cs & bits) == bits)
668                         return 0;
669
670                 usleep_range(10, 20);
671         }
672
673         dev_err(adapter->dev, "poll card status failed, tries = %d\n", tries);
674
675         return -1;
676 }
677
678 /*
679  * This function reads the firmware status.
680  */
681 static int
682 mwifiex_sdio_read_fw_status(struct mwifiex_adapter *adapter, u16 *dat)
683 {
684         struct sdio_mmc_card *card = adapter->card;
685         const struct mwifiex_sdio_card_reg *reg = card->reg;
686         u8 fws0, fws1;
687
688         if (mwifiex_read_reg(adapter, reg->status_reg_0, &fws0))
689                 return -1;
690
691         if (mwifiex_read_reg(adapter, reg->status_reg_1, &fws1))
692                 return -1;
693
694         *dat = (u16) ((fws1 << 8) | fws0);
695
696         return 0;
697 }
698
699 /*
700  * This function disables the host interrupt.
701  *
702  * The host interrupt mask is read, the disable bit is reset and
703  * written back to the card host interrupt mask register.
704  */
705 static void mwifiex_sdio_disable_host_int(struct mwifiex_adapter *adapter)
706 {
707         struct sdio_mmc_card *card = adapter->card;
708         struct sdio_func *func = card->func;
709
710         sdio_claim_host(func);
711         mwifiex_write_reg_locked(func, HOST_INT_MASK_REG, 0);
712         sdio_release_irq(func);
713         sdio_release_host(func);
714 }
715
716 /*
717  * This function reads the interrupt status from card.
718  */
719 static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
720 {
721         struct sdio_mmc_card *card = adapter->card;
722         u8 sdio_ireg;
723         unsigned long flags;
724
725         if (mwifiex_read_data_sync(adapter, card->mp_regs,
726                                    card->reg->max_mp_regs,
727                                    REG_PORT | MWIFIEX_SDIO_BYTE_MODE_MASK, 0)) {
728                 dev_err(adapter->dev, "read mp_regs failed\n");
729                 return;
730         }
731
732         sdio_ireg = card->mp_regs[HOST_INTSTATUS_REG];
733         if (sdio_ireg) {
734                 /*
735                  * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
736                  * For SDIO new mode CMD port interrupts
737                  *      DN_LD_CMD_PORT_HOST_INT_STATUS and/or
738                  *      UP_LD_CMD_PORT_HOST_INT_STATUS
739                  * Clear the interrupt status register
740                  */
741                 dev_dbg(adapter->dev, "int: sdio_ireg = %#x\n", sdio_ireg);
742                 spin_lock_irqsave(&adapter->int_lock, flags);
743                 adapter->int_status |= sdio_ireg;
744                 spin_unlock_irqrestore(&adapter->int_lock, flags);
745         }
746 }
747
748 /*
749  * SDIO interrupt handler.
750  *
751  * This function reads the interrupt status from firmware and handles
752  * the interrupt in current thread (ksdioirqd) right away.
753  */
754 static void
755 mwifiex_sdio_interrupt(struct sdio_func *func)
756 {
757         struct mwifiex_adapter *adapter;
758         struct sdio_mmc_card *card;
759
760         card = sdio_get_drvdata(func);
761         if (!card || !card->adapter) {
762                 pr_debug("int: func=%p card=%p adapter=%p\n",
763                          func, card, card ? card->adapter : NULL);
764                 return;
765         }
766         adapter = card->adapter;
767
768         if (!adapter->pps_uapsd_mode && adapter->ps_state == PS_STATE_SLEEP)
769                 adapter->ps_state = PS_STATE_AWAKE;
770
771         mwifiex_interrupt_status(adapter);
772         mwifiex_main_process(adapter);
773 }
774
775 /*
776  * This function enables the host interrupt.
777  *
778  * The host interrupt enable mask is written to the card
779  * host interrupt mask register.
780  */
781 static int mwifiex_sdio_enable_host_int(struct mwifiex_adapter *adapter)
782 {
783         struct sdio_mmc_card *card = adapter->card;
784         struct sdio_func *func = card->func;
785         int ret;
786
787         sdio_claim_host(func);
788
789         /* Request the SDIO IRQ */
790         ret = sdio_claim_irq(func, mwifiex_sdio_interrupt);
791         if (ret) {
792                 dev_err(adapter->dev, "claim irq failed: ret=%d\n", ret);
793                 goto out;
794         }
795
796         /* Simply write the mask to the register */
797         ret = mwifiex_write_reg_locked(func, HOST_INT_MASK_REG,
798                                        card->reg->host_int_enable);
799         if (ret) {
800                 dev_err(adapter->dev, "enable host interrupt failed\n");
801                 sdio_release_irq(func);
802         }
803
804 out:
805         sdio_release_host(func);
806         return ret;
807 }
808
809 /*
810  * This function sends a data buffer to the card.
811  */
812 static int mwifiex_sdio_card_to_host(struct mwifiex_adapter *adapter,
813                                      u32 *type, u8 *buffer,
814                                      u32 npayload, u32 ioport)
815 {
816         int ret;
817         u32 nb;
818
819         if (!buffer) {
820                 dev_err(adapter->dev, "%s: buffer is NULL\n", __func__);
821                 return -1;
822         }
823
824         ret = mwifiex_read_data_sync(adapter, buffer, npayload, ioport, 1);
825
826         if (ret) {
827                 dev_err(adapter->dev, "%s: read iomem failed: %d\n", __func__,
828                         ret);
829                 return -1;
830         }
831
832         nb = le16_to_cpu(*(__le16 *) (buffer));
833         if (nb > npayload) {
834                 dev_err(adapter->dev, "%s: invalid packet, nb=%d npayload=%d\n",
835                         __func__, nb, npayload);
836                 return -1;
837         }
838
839         *type = le16_to_cpu(*(__le16 *) (buffer + 2));
840
841         return ret;
842 }
843
844 /*
845  * This function downloads the firmware to the card.
846  *
847  * Firmware is downloaded to the card in blocks. Every block download
848  * is tested for CRC errors, and retried a number of times before
849  * returning failure.
850  */
851 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
852                                     struct mwifiex_fw_image *fw)
853 {
854         struct sdio_mmc_card *card = adapter->card;
855         const struct mwifiex_sdio_card_reg *reg = card->reg;
856         int ret;
857         u8 *firmware = fw->fw_buf;
858         u32 firmware_len = fw->fw_len;
859         u32 offset = 0;
860         u8 base0, base1;
861         u8 *fwbuf;
862         u16 len = 0;
863         u32 txlen, tx_blocks = 0, tries;
864         u32 i = 0;
865
866         if (!firmware_len) {
867                 dev_err(adapter->dev,
868                         "firmware image not found! Terminating download\n");
869                 return -1;
870         }
871
872         dev_dbg(adapter->dev, "info: downloading FW image (%d bytes)\n",
873                 firmware_len);
874
875         /* Assume that the allocated buffer is 8-byte aligned */
876         fwbuf = kzalloc(MWIFIEX_UPLD_SIZE, GFP_KERNEL);
877         if (!fwbuf)
878                 return -ENOMEM;
879
880         /* Perform firmware data transfer */
881         do {
882                 /* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY
883                    bits */
884                 ret = mwifiex_sdio_poll_card_status(adapter, CARD_IO_READY |
885                                                     DN_LD_CARD_RDY);
886                 if (ret) {
887                         dev_err(adapter->dev, "FW download with helper:"
888                                 " poll status timeout @ %d\n", offset);
889                         goto done;
890                 }
891
892                 /* More data? */
893                 if (offset >= firmware_len)
894                         break;
895
896                 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
897                         ret = mwifiex_read_reg(adapter, reg->base_0_reg,
898                                                &base0);
899                         if (ret) {
900                                 dev_err(adapter->dev,
901                                         "dev BASE0 register read failed: "
902                                         "base0=%#04X(%d). Terminating dnld\n",
903                                         base0, base0);
904                                 goto done;
905                         }
906                         ret = mwifiex_read_reg(adapter, reg->base_1_reg,
907                                                &base1);
908                         if (ret) {
909                                 dev_err(adapter->dev,
910                                         "dev BASE1 register read failed: "
911                                         "base1=%#04X(%d). Terminating dnld\n",
912                                         base1, base1);
913                                 goto done;
914                         }
915                         len = (u16) (((base1 & 0xff) << 8) | (base0 & 0xff));
916
917                         if (len)
918                                 break;
919
920                         usleep_range(10, 20);
921                 }
922
923                 if (!len) {
924                         break;
925                 } else if (len > MWIFIEX_UPLD_SIZE) {
926                         dev_err(adapter->dev,
927                                 "FW dnld failed @ %d, invalid length %d\n",
928                                 offset, len);
929                         ret = -1;
930                         goto done;
931                 }
932
933                 txlen = len;
934
935                 if (len & BIT(0)) {
936                         i++;
937                         if (i > MAX_WRITE_IOMEM_RETRY) {
938                                 dev_err(adapter->dev,
939                                         "FW dnld failed @ %d, over max retry\n",
940                                         offset);
941                                 ret = -1;
942                                 goto done;
943                         }
944                         dev_err(adapter->dev, "CRC indicated by the helper:"
945                                 " len = 0x%04X, txlen = %d\n", len, txlen);
946                         len &= ~BIT(0);
947                         /* Setting this to 0 to resend from same offset */
948                         txlen = 0;
949                 } else {
950                         i = 0;
951
952                         /* Set blocksize to transfer - checking for last
953                            block */
954                         if (firmware_len - offset < txlen)
955                                 txlen = firmware_len - offset;
956
957                         tx_blocks = (txlen + MWIFIEX_SDIO_BLOCK_SIZE - 1)
958                                     / MWIFIEX_SDIO_BLOCK_SIZE;
959
960                         /* Copy payload to buffer */
961                         memmove(fwbuf, &firmware[offset], txlen);
962                 }
963
964                 ret = mwifiex_write_data_sync(adapter, fwbuf, tx_blocks *
965                                               MWIFIEX_SDIO_BLOCK_SIZE,
966                                               adapter->ioport);
967                 if (ret) {
968                         dev_err(adapter->dev,
969                                 "FW download, write iomem (%d) failed @ %d\n",
970                                 i, offset);
971                         if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
972                                 dev_err(adapter->dev, "write CFG reg failed\n");
973
974                         ret = -1;
975                         goto done;
976                 }
977
978                 offset += txlen;
979         } while (true);
980
981         dev_dbg(adapter->dev, "info: FW download over, size %d bytes\n",
982                 offset);
983
984         ret = 0;
985 done:
986         kfree(fwbuf);
987         return ret;
988 }
989
990 /*
991  * This function checks the firmware status in card.
992  *
993  * The winner interface is also determined by this function.
994  */
995 static int mwifiex_check_fw_status(struct mwifiex_adapter *adapter,
996                                    u32 poll_num)
997 {
998         struct sdio_mmc_card *card = adapter->card;
999         int ret = 0;
1000         u16 firmware_stat;
1001         u32 tries;
1002         u8 winner_status;
1003
1004         /* Wait for firmware initialization event */
1005         for (tries = 0; tries < poll_num; tries++) {
1006                 ret = mwifiex_sdio_read_fw_status(adapter, &firmware_stat);
1007                 if (ret)
1008                         continue;
1009                 if (firmware_stat == FIRMWARE_READY_SDIO) {
1010                         ret = 0;
1011                         break;
1012                 } else {
1013                         msleep(100);
1014                         ret = -1;
1015                 }
1016         }
1017
1018         if (ret) {
1019                 if (mwifiex_read_reg
1020                     (adapter, card->reg->status_reg_0, &winner_status))
1021                         winner_status = 0;
1022
1023                 if (winner_status)
1024                         adapter->winner = 0;
1025                 else
1026                         adapter->winner = 1;
1027         }
1028         return ret;
1029 }
1030
1031 /*
1032  * This function decodes a received packet.
1033  *
1034  * Based on the type, the packet is treated as either a data, or
1035  * a command response, or an event, and the correct handler
1036  * function is invoked.
1037  */
1038 static int mwifiex_decode_rx_packet(struct mwifiex_adapter *adapter,
1039                                     struct sk_buff *skb, u32 upld_typ)
1040 {
1041         u8 *cmd_buf;
1042         __le16 *curr_ptr = (__le16 *)skb->data;
1043         u16 pkt_len = le16_to_cpu(*curr_ptr);
1044
1045         skb_trim(skb, pkt_len);
1046         skb_pull(skb, INTF_HEADER_LEN);
1047
1048         switch (upld_typ) {
1049         case MWIFIEX_TYPE_DATA:
1050                 dev_dbg(adapter->dev, "info: --- Rx: Data packet ---\n");
1051                 mwifiex_handle_rx_packet(adapter, skb);
1052                 break;
1053
1054         case MWIFIEX_TYPE_CMD:
1055                 dev_dbg(adapter->dev, "info: --- Rx: Cmd Response ---\n");
1056                 /* take care of curr_cmd = NULL case */
1057                 if (!adapter->curr_cmd) {
1058                         cmd_buf = adapter->upld_buf;
1059
1060                         if (adapter->ps_state == PS_STATE_SLEEP_CFM)
1061                                 mwifiex_process_sleep_confirm_resp(adapter,
1062                                                                    skb->data,
1063                                                                    skb->len);
1064
1065                         memcpy(cmd_buf, skb->data,
1066                                min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER,
1067                                      skb->len));
1068
1069                         dev_kfree_skb_any(skb);
1070                 } else {
1071                         adapter->cmd_resp_received = true;
1072                         adapter->curr_cmd->resp_skb = skb;
1073                 }
1074                 break;
1075
1076         case MWIFIEX_TYPE_EVENT:
1077                 dev_dbg(adapter->dev, "info: --- Rx: Event ---\n");
1078                 adapter->event_cause = le32_to_cpu(*(__le32 *) skb->data);
1079
1080                 if ((skb->len > 0) && (skb->len  < MAX_EVENT_SIZE))
1081                         memcpy(adapter->event_body,
1082                                skb->data + MWIFIEX_EVENT_HEADER_LEN,
1083                                skb->len);
1084
1085                 /* event cause has been saved to adapter->event_cause */
1086                 adapter->event_received = true;
1087                 adapter->event_skb = skb;
1088
1089                 break;
1090
1091         default:
1092                 dev_err(adapter->dev, "unknown upload type %#x\n", upld_typ);
1093                 dev_kfree_skb_any(skb);
1094                 break;
1095         }
1096
1097         return 0;
1098 }
1099
1100 /*
1101  * This function transfers received packets from card to driver, performing
1102  * aggregation if required.
1103  *
1104  * For data received on control port, or if aggregation is disabled, the
1105  * received buffers are uploaded as separate packets. However, if aggregation
1106  * is enabled and required, the buffers are copied onto an aggregation buffer,
1107  * provided there is space left, processed and finally uploaded.
1108  */
1109 static int mwifiex_sdio_card_to_host_mp_aggr(struct mwifiex_adapter *adapter,
1110                                              struct sk_buff *skb, u8 port)
1111 {
1112         struct sdio_mmc_card *card = adapter->card;
1113         s32 f_do_rx_aggr = 0;
1114         s32 f_do_rx_cur = 0;
1115         s32 f_aggr_cur = 0;
1116         struct sk_buff *skb_deaggr;
1117         u32 pind;
1118         u32 pkt_len, pkt_type, mport;
1119         u8 *curr_ptr;
1120         u32 rx_len = skb->len;
1121
1122         if ((card->has_control_mask) && (port == CTRL_PORT)) {
1123                 /* Read the command Resp without aggr */
1124                 dev_dbg(adapter->dev, "info: %s: no aggregation for cmd "
1125                         "response\n", __func__);
1126
1127                 f_do_rx_cur = 1;
1128                 goto rx_curr_single;
1129         }
1130
1131         if (!card->mpa_rx.enabled) {
1132                 dev_dbg(adapter->dev, "info: %s: rx aggregation disabled\n",
1133                         __func__);
1134
1135                 f_do_rx_cur = 1;
1136                 goto rx_curr_single;
1137         }
1138
1139         if ((!card->has_control_mask && (card->mp_rd_bitmap &
1140                                          card->reg->data_port_mask)) ||
1141             (card->has_control_mask && (card->mp_rd_bitmap &
1142                                         (~((u32) CTRL_PORT_MASK))))) {
1143                 /* Some more data RX pending */
1144                 dev_dbg(adapter->dev, "info: %s: not last packet\n", __func__);
1145
1146                 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1147                         if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len)) {
1148                                 f_aggr_cur = 1;
1149                         } else {
1150                                 /* No room in Aggr buf, do rx aggr now */
1151                                 f_do_rx_aggr = 1;
1152                                 f_do_rx_cur = 1;
1153                         }
1154                 } else {
1155                         /* Rx aggr not in progress */
1156                         f_aggr_cur = 1;
1157                 }
1158
1159         } else {
1160                 /* No more data RX pending */
1161                 dev_dbg(adapter->dev, "info: %s: last packet\n", __func__);
1162
1163                 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1164                         f_do_rx_aggr = 1;
1165                         if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len))
1166                                 f_aggr_cur = 1;
1167                         else
1168                                 /* No room in Aggr buf, do rx aggr now */
1169                                 f_do_rx_cur = 1;
1170                 } else {
1171                         f_do_rx_cur = 1;
1172                 }
1173         }
1174
1175         if (f_aggr_cur) {
1176                 dev_dbg(adapter->dev, "info: current packet aggregation\n");
1177                 /* Curr pkt can be aggregated */
1178                 mp_rx_aggr_setup(card, skb, port);
1179
1180                 if (MP_RX_AGGR_PKT_LIMIT_REACHED(card) ||
1181                     mp_rx_aggr_port_limit_reached(card)) {
1182                         dev_dbg(adapter->dev, "info: %s: aggregated packet "
1183                                 "limit reached\n", __func__);
1184                         /* No more pkts allowed in Aggr buf, rx it */
1185                         f_do_rx_aggr = 1;
1186                 }
1187         }
1188
1189         if (f_do_rx_aggr) {
1190                 /* do aggr RX now */
1191                 dev_dbg(adapter->dev, "info: do_rx_aggr: num of packets: %d\n",
1192                         card->mpa_rx.pkt_cnt);
1193
1194                 if (card->supports_sdio_new_mode) {
1195                         int i;
1196                         u32 port_count;
1197
1198                         for (i = 0, port_count = 0; i < card->max_ports; i++)
1199                                 if (card->mpa_rx.ports & BIT(i))
1200                                         port_count++;
1201
1202                         /* Reading data from "start_port + 0" to "start_port +
1203                          * port_count -1", so decrease the count by 1
1204                          */
1205                         port_count--;
1206                         mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1207                                  (port_count << 8)) + card->mpa_rx.start_port;
1208                 } else {
1209                         mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1210                                  (card->mpa_rx.ports << 4)) +
1211                                  card->mpa_rx.start_port;
1212                 }
1213
1214                 if (mwifiex_read_data_sync(adapter, card->mpa_rx.buf,
1215                                            card->mpa_rx.buf_len, mport, 1))
1216                         goto error;
1217
1218                 curr_ptr = card->mpa_rx.buf;
1219
1220                 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1221
1222                         /* get curr PKT len & type */
1223                         pkt_len = le16_to_cpu(*(__le16 *) &curr_ptr[0]);
1224                         pkt_type = le16_to_cpu(*(__le16 *) &curr_ptr[2]);
1225
1226                         /* copy pkt to deaggr buf */
1227                         skb_deaggr = card->mpa_rx.skb_arr[pind];
1228
1229                         if ((pkt_type == MWIFIEX_TYPE_DATA) && (pkt_len <=
1230                                          card->mpa_rx.len_arr[pind])) {
1231
1232                                 memcpy(skb_deaggr->data, curr_ptr, pkt_len);
1233
1234                                 skb_trim(skb_deaggr, pkt_len);
1235
1236                                 /* Process de-aggr packet */
1237                                 mwifiex_decode_rx_packet(adapter, skb_deaggr,
1238                                                          pkt_type);
1239                         } else {
1240                                 dev_err(adapter->dev, "wrong aggr pkt:"
1241                                         " type=%d len=%d max_len=%d\n",
1242                                         pkt_type, pkt_len,
1243                                         card->mpa_rx.len_arr[pind]);
1244                                 dev_kfree_skb_any(skb_deaggr);
1245                         }
1246                         curr_ptr += card->mpa_rx.len_arr[pind];
1247                 }
1248                 MP_RX_AGGR_BUF_RESET(card);
1249         }
1250
1251 rx_curr_single:
1252         if (f_do_rx_cur) {
1253                 dev_dbg(adapter->dev, "info: RX: port: %d, rx_len: %d\n",
1254                         port, rx_len);
1255
1256                 if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1257                                               skb->data, skb->len,
1258                                               adapter->ioport + port))
1259                         goto error;
1260
1261                 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1262         }
1263
1264         return 0;
1265
1266 error:
1267         if (MP_RX_AGGR_IN_PROGRESS(card)) {
1268                 /* Multiport-aggregation transfer failed - cleanup */
1269                 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1270                         /* copy pkt to deaggr buf */
1271                         skb_deaggr = card->mpa_rx.skb_arr[pind];
1272                         dev_kfree_skb_any(skb_deaggr);
1273                 }
1274                 MP_RX_AGGR_BUF_RESET(card);
1275         }
1276
1277         if (f_do_rx_cur)
1278                 /* Single transfer pending. Free curr buff also */
1279                 dev_kfree_skb_any(skb);
1280
1281         return -1;
1282 }
1283
1284 /*
1285  * This function checks the current interrupt status.
1286  *
1287  * The following interrupts are checked and handled by this function -
1288  *      - Data sent
1289  *      - Command sent
1290  *      - Packets received
1291  *
1292  * Since the firmware does not generate download ready interrupt if the
1293  * port updated is command port only, command sent interrupt checking
1294  * should be done manually, and for every SDIO interrupt.
1295  *
1296  * In case of Rx packets received, the packets are uploaded from card to
1297  * host and processed accordingly.
1298  */
1299 static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
1300 {
1301         struct sdio_mmc_card *card = adapter->card;
1302         const struct mwifiex_sdio_card_reg *reg = card->reg;
1303         int ret = 0;
1304         u8 sdio_ireg;
1305         struct sk_buff *skb;
1306         u8 port = CTRL_PORT;
1307         u32 len_reg_l, len_reg_u;
1308         u32 rx_blocks;
1309         u16 rx_len;
1310         unsigned long flags;
1311         u32 bitmap;
1312         u8 cr;
1313
1314         spin_lock_irqsave(&adapter->int_lock, flags);
1315         sdio_ireg = adapter->int_status;
1316         adapter->int_status = 0;
1317         spin_unlock_irqrestore(&adapter->int_lock, flags);
1318
1319         if (!sdio_ireg)
1320                 return ret;
1321
1322         /* Following interrupt is only for SDIO new mode */
1323         if (sdio_ireg & DN_LD_CMD_PORT_HOST_INT_STATUS && adapter->cmd_sent)
1324                 adapter->cmd_sent = false;
1325
1326         /* Following interrupt is only for SDIO new mode */
1327         if (sdio_ireg & UP_LD_CMD_PORT_HOST_INT_STATUS) {
1328                 u32 pkt_type;
1329
1330                 /* read the len of control packet */
1331                 rx_len = card->mp_regs[CMD_RD_LEN_1] << 8;
1332                 rx_len |= (u16) card->mp_regs[CMD_RD_LEN_0];
1333                 rx_blocks = DIV_ROUND_UP(rx_len, MWIFIEX_SDIO_BLOCK_SIZE);
1334                 if (rx_len <= INTF_HEADER_LEN ||
1335                     (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1336                      MWIFIEX_RX_DATA_BUF_SIZE)
1337                         return -1;
1338                 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1339
1340                 skb = dev_alloc_skb(rx_len);
1341                 if (!skb)
1342                         return -1;
1343
1344                 skb_put(skb, rx_len);
1345
1346                 if (mwifiex_sdio_card_to_host(adapter, &pkt_type, skb->data,
1347                                               skb->len, adapter->ioport |
1348                                                         CMD_PORT_SLCT)) {
1349                         dev_err(adapter->dev,
1350                                 "%s: failed to card_to_host", __func__);
1351                         dev_kfree_skb_any(skb);
1352                         goto term_cmd;
1353                 }
1354
1355                 if ((pkt_type != MWIFIEX_TYPE_CMD) &&
1356                     (pkt_type != MWIFIEX_TYPE_EVENT))
1357                         dev_err(adapter->dev,
1358                                 "%s:Received wrong packet on cmd port",
1359                                 __func__);
1360
1361                 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1362         }
1363
1364         if (sdio_ireg & DN_LD_HOST_INT_STATUS) {
1365                 bitmap = (u32) card->mp_regs[reg->wr_bitmap_l];
1366                 bitmap |= ((u32) card->mp_regs[reg->wr_bitmap_u]) << 8;
1367                 if (card->supports_sdio_new_mode) {
1368                         bitmap |=
1369                                 ((u32) card->mp_regs[reg->wr_bitmap_1l]) << 16;
1370                         bitmap |=
1371                                 ((u32) card->mp_regs[reg->wr_bitmap_1u]) << 24;
1372                 }
1373                 card->mp_wr_bitmap = bitmap;
1374
1375                 dev_dbg(adapter->dev, "int: DNLD: wr_bitmap=0x%x\n",
1376                         card->mp_wr_bitmap);
1377                 if (adapter->data_sent &&
1378                     (card->mp_wr_bitmap & card->mp_data_port_mask)) {
1379                         dev_dbg(adapter->dev,
1380                                 "info:  <--- Tx DONE Interrupt --->\n");
1381                         adapter->data_sent = false;
1382                 }
1383         }
1384
1385         /* As firmware will not generate download ready interrupt if the port
1386            updated is command port only, cmd_sent should be done for any SDIO
1387            interrupt. */
1388         if (card->has_control_mask && adapter->cmd_sent) {
1389                 /* Check if firmware has attach buffer at command port and
1390                    update just that in wr_bit_map. */
1391                 card->mp_wr_bitmap |=
1392                         (u32) card->mp_regs[reg->wr_bitmap_l] & CTRL_PORT_MASK;
1393                 if (card->mp_wr_bitmap & CTRL_PORT_MASK)
1394                         adapter->cmd_sent = false;
1395         }
1396
1397         dev_dbg(adapter->dev, "info: cmd_sent=%d data_sent=%d\n",
1398                 adapter->cmd_sent, adapter->data_sent);
1399         if (sdio_ireg & UP_LD_HOST_INT_STATUS) {
1400                 bitmap = (u32) card->mp_regs[reg->rd_bitmap_l];
1401                 bitmap |= ((u32) card->mp_regs[reg->rd_bitmap_u]) << 8;
1402                 if (card->supports_sdio_new_mode) {
1403                         bitmap |=
1404                                 ((u32) card->mp_regs[reg->rd_bitmap_1l]) << 16;
1405                         bitmap |=
1406                                 ((u32) card->mp_regs[reg->rd_bitmap_1u]) << 24;
1407                 }
1408                 card->mp_rd_bitmap = bitmap;
1409                 dev_dbg(adapter->dev, "int: UPLD: rd_bitmap=0x%x\n",
1410                         card->mp_rd_bitmap);
1411
1412                 while (true) {
1413                         ret = mwifiex_get_rd_port(adapter, &port);
1414                         if (ret) {
1415                                 dev_dbg(adapter->dev,
1416                                         "info: no more rd_port available\n");
1417                                 break;
1418                         }
1419                         len_reg_l = reg->rd_len_p0_l + (port << 1);
1420                         len_reg_u = reg->rd_len_p0_u + (port << 1);
1421                         rx_len = ((u16) card->mp_regs[len_reg_u]) << 8;
1422                         rx_len |= (u16) card->mp_regs[len_reg_l];
1423                         dev_dbg(adapter->dev, "info: RX: port=%d rx_len=%u\n",
1424                                 port, rx_len);
1425                         rx_blocks =
1426                                 (rx_len + MWIFIEX_SDIO_BLOCK_SIZE -
1427                                  1) / MWIFIEX_SDIO_BLOCK_SIZE;
1428                         if (rx_len <= INTF_HEADER_LEN ||
1429                             (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1430                              MWIFIEX_RX_DATA_BUF_SIZE) {
1431                                 dev_err(adapter->dev, "invalid rx_len=%d\n",
1432                                         rx_len);
1433                                 return -1;
1434                         }
1435                         rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1436
1437                         skb = dev_alloc_skb(rx_len);
1438
1439                         if (!skb) {
1440                                 dev_err(adapter->dev, "%s: failed to alloc skb",
1441                                         __func__);
1442                                 return -1;
1443                         }
1444
1445                         skb_put(skb, rx_len);
1446
1447                         dev_dbg(adapter->dev, "info: rx_len = %d skb->len = %d\n",
1448                                 rx_len, skb->len);
1449
1450                         if (mwifiex_sdio_card_to_host_mp_aggr(adapter, skb,
1451                                                               port)) {
1452                                 dev_err(adapter->dev, "card_to_host_mpa failed:"
1453                                         " int status=%#x\n", sdio_ireg);
1454                                 goto term_cmd;
1455                         }
1456                 }
1457         }
1458
1459         return 0;
1460
1461 term_cmd:
1462         /* terminate cmd */
1463         if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
1464                 dev_err(adapter->dev, "read CFG reg failed\n");
1465         else
1466                 dev_dbg(adapter->dev, "info: CFG reg val = %d\n", cr);
1467
1468         if (mwifiex_write_reg(adapter, CONFIGURATION_REG, (cr | 0x04)))
1469                 dev_err(adapter->dev, "write CFG reg failed\n");
1470         else
1471                 dev_dbg(adapter->dev, "info: write success\n");
1472
1473         if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
1474                 dev_err(adapter->dev, "read CFG reg failed\n");
1475         else
1476                 dev_dbg(adapter->dev, "info: CFG reg val =%x\n", cr);
1477
1478         return -1;
1479 }
1480
1481 /*
1482  * This function aggregates transmission buffers in driver and downloads
1483  * the aggregated packet to card.
1484  *
1485  * The individual packets are aggregated by copying into an aggregation
1486  * buffer and then downloaded to the card. Previous unsent packets in the
1487  * aggregation buffer are pre-copied first before new packets are added.
1488  * Aggregation is done till there is space left in the aggregation buffer,
1489  * or till new packets are available.
1490  *
1491  * The function will only download the packet to the card when aggregation
1492  * stops, otherwise it will just aggregate the packet in aggregation buffer
1493  * and return.
1494  */
1495 static int mwifiex_host_to_card_mp_aggr(struct mwifiex_adapter *adapter,
1496                                         u8 *payload, u32 pkt_len, u32 port,
1497                                         u32 next_pkt_len)
1498 {
1499         struct sdio_mmc_card *card = adapter->card;
1500         int ret = 0;
1501         s32 f_send_aggr_buf = 0;
1502         s32 f_send_cur_buf = 0;
1503         s32 f_precopy_cur_buf = 0;
1504         s32 f_postcopy_cur_buf = 0;
1505         u32 mport;
1506
1507         if (!card->mpa_tx.enabled ||
1508             (card->has_control_mask && (port == CTRL_PORT)) ||
1509             (card->supports_sdio_new_mode && (port == CMD_PORT_SLCT))) {
1510                 dev_dbg(adapter->dev, "info: %s: tx aggregation disabled\n",
1511                         __func__);
1512
1513                 f_send_cur_buf = 1;
1514                 goto tx_curr_single;
1515         }
1516
1517         if (next_pkt_len) {
1518                 /* More pkt in TX queue */
1519                 dev_dbg(adapter->dev, "info: %s: more packets in queue.\n",
1520                         __func__);
1521
1522                 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1523                         if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len)) {
1524                                 f_precopy_cur_buf = 1;
1525
1526                                 if (!(card->mp_wr_bitmap &
1527                                       (1 << card->curr_wr_port)) ||
1528                                     !MP_TX_AGGR_BUF_HAS_ROOM(
1529                                             card, pkt_len + next_pkt_len))
1530                                         f_send_aggr_buf = 1;
1531                         } else {
1532                                 /* No room in Aggr buf, send it */
1533                                 f_send_aggr_buf = 1;
1534
1535                                 if (!(card->mp_wr_bitmap &
1536                                       (1 << card->curr_wr_port)))
1537                                         f_send_cur_buf = 1;
1538                                 else
1539                                         f_postcopy_cur_buf = 1;
1540                         }
1541                 } else {
1542                         if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len) &&
1543                             (card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1544                                 f_precopy_cur_buf = 1;
1545                         else
1546                                 f_send_cur_buf = 1;
1547                 }
1548         } else {
1549                 /* Last pkt in TX queue */
1550                 dev_dbg(adapter->dev, "info: %s: Last packet in Tx Queue.\n",
1551                         __func__);
1552
1553                 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1554                         /* some packs in Aggr buf already */
1555                         f_send_aggr_buf = 1;
1556
1557                         if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len))
1558                                 f_precopy_cur_buf = 1;
1559                         else
1560                                 /* No room in Aggr buf, send it */
1561                                 f_send_cur_buf = 1;
1562                 } else {
1563                         f_send_cur_buf = 1;
1564                 }
1565         }
1566
1567         if (f_precopy_cur_buf) {
1568                 dev_dbg(adapter->dev, "data: %s: precopy current buffer\n",
1569                         __func__);
1570                 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1571
1572                 if (MP_TX_AGGR_PKT_LIMIT_REACHED(card) ||
1573                     mp_tx_aggr_port_limit_reached(card))
1574                         /* No more pkts allowed in Aggr buf, send it */
1575                         f_send_aggr_buf = 1;
1576         }
1577
1578         if (f_send_aggr_buf) {
1579                 dev_dbg(adapter->dev, "data: %s: send aggr buffer: %d %d\n",
1580                         __func__,
1581                                 card->mpa_tx.start_port, card->mpa_tx.ports);
1582                 if (card->supports_sdio_new_mode) {
1583                         u32 port_count;
1584                         int i;
1585
1586                         for (i = 0, port_count = 0; i < card->max_ports; i++)
1587                                 if (card->mpa_tx.ports & BIT(i))
1588                                         port_count++;
1589
1590                         /* Writing data from "start_port + 0" to "start_port +
1591                          * port_count -1", so decrease the count by 1
1592                          */
1593                         port_count--;
1594                         mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1595                                  (port_count << 8)) + card->mpa_tx.start_port;
1596                 } else {
1597                         mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1598                                  (card->mpa_tx.ports << 4)) +
1599                                  card->mpa_tx.start_port;
1600                 }
1601
1602                 ret = mwifiex_write_data_to_card(adapter, card->mpa_tx.buf,
1603                                                  card->mpa_tx.buf_len, mport);
1604
1605                 MP_TX_AGGR_BUF_RESET(card);
1606         }
1607
1608 tx_curr_single:
1609         if (f_send_cur_buf) {
1610                 dev_dbg(adapter->dev, "data: %s: send current buffer %d\n",
1611                         __func__, port);
1612                 ret = mwifiex_write_data_to_card(adapter, payload, pkt_len,
1613                                                  adapter->ioport + port);
1614         }
1615
1616         if (f_postcopy_cur_buf) {
1617                 dev_dbg(adapter->dev, "data: %s: postcopy current buffer\n",
1618                         __func__);
1619                 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1620         }
1621
1622         return ret;
1623 }
1624
1625 /*
1626  * This function downloads data from driver to card.
1627  *
1628  * Both commands and data packets are transferred to the card by this
1629  * function.
1630  *
1631  * This function adds the SDIO specific header to the front of the buffer
1632  * before transferring. The header contains the length of the packet and
1633  * the type. The firmware handles the packets based upon this set type.
1634  */
1635 static int mwifiex_sdio_host_to_card(struct mwifiex_adapter *adapter,
1636                                      u8 type, struct sk_buff *skb,
1637                                      struct mwifiex_tx_param *tx_param)
1638 {
1639         struct sdio_mmc_card *card = adapter->card;
1640         int ret;
1641         u32 buf_block_len;
1642         u32 blk_size;
1643         u32 port = CTRL_PORT;
1644         u8 *payload = (u8 *)skb->data;
1645         u32 pkt_len = skb->len;
1646
1647         /* Allocate buffer and copy payload */
1648         blk_size = MWIFIEX_SDIO_BLOCK_SIZE;
1649         buf_block_len = (pkt_len + blk_size - 1) / blk_size;
1650         *(__le16 *)&payload[0] = cpu_to_le16((u16)pkt_len);
1651         *(__le16 *)&payload[2] = cpu_to_le16(type);
1652
1653         /*
1654          * This is SDIO specific header
1655          *  u16 length,
1656          *  u16 type (MWIFIEX_TYPE_DATA = 0, MWIFIEX_TYPE_CMD = 1,
1657          *  MWIFIEX_TYPE_EVENT = 3)
1658          */
1659         if (type == MWIFIEX_TYPE_DATA) {
1660                 ret = mwifiex_get_wr_port_data(adapter, &port);
1661                 if (ret) {
1662                         dev_err(adapter->dev, "%s: no wr_port available\n",
1663                                 __func__);
1664                         return ret;
1665                 }
1666         } else {
1667                 adapter->cmd_sent = true;
1668                 /* Type must be MWIFIEX_TYPE_CMD */
1669
1670                 if (pkt_len <= INTF_HEADER_LEN ||
1671                     pkt_len > MWIFIEX_UPLD_SIZE)
1672                         dev_err(adapter->dev, "%s: payload=%p, nb=%d\n",
1673                                 __func__, payload, pkt_len);
1674
1675                 if (card->supports_sdio_new_mode)
1676                         port = CMD_PORT_SLCT;
1677         }
1678
1679         /* Transfer data to card */
1680         pkt_len = buf_block_len * blk_size;
1681
1682         if (tx_param)
1683                 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1684                                                    port, tx_param->next_pkt_len
1685                                                    );
1686         else
1687                 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1688                                                    port, 0);
1689
1690         if (ret) {
1691                 if (type == MWIFIEX_TYPE_CMD)
1692                         adapter->cmd_sent = false;
1693                 if (type == MWIFIEX_TYPE_DATA) {
1694                         adapter->data_sent = false;
1695                         /* restore curr_wr_port in error cases */
1696                         card->curr_wr_port = port;
1697                         card->mp_wr_bitmap |= (u32)(1 << card->curr_wr_port);
1698                 }
1699         } else {
1700                 if (type == MWIFIEX_TYPE_DATA) {
1701                         if (!(card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1702                                 adapter->data_sent = true;
1703                         else
1704                                 adapter->data_sent = false;
1705                 }
1706         }
1707
1708         return ret;
1709 }
1710
1711 /*
1712  * This function allocates the MPA Tx and Rx buffers.
1713  */
1714 static int mwifiex_alloc_sdio_mpa_buffers(struct mwifiex_adapter *adapter,
1715                                    u32 mpa_tx_buf_size, u32 mpa_rx_buf_size)
1716 {
1717         struct sdio_mmc_card *card = adapter->card;
1718         int ret = 0;
1719
1720         card->mpa_tx.buf = kzalloc(mpa_tx_buf_size, GFP_KERNEL);
1721         if (!card->mpa_tx.buf) {
1722                 ret = -1;
1723                 goto error;
1724         }
1725
1726         card->mpa_tx.buf_size = mpa_tx_buf_size;
1727
1728         card->mpa_rx.buf = kzalloc(mpa_rx_buf_size, GFP_KERNEL);
1729         if (!card->mpa_rx.buf) {
1730                 ret = -1;
1731                 goto error;
1732         }
1733
1734         card->mpa_rx.buf_size = mpa_rx_buf_size;
1735
1736 error:
1737         if (ret) {
1738                 kfree(card->mpa_tx.buf);
1739                 kfree(card->mpa_rx.buf);
1740         }
1741
1742         return ret;
1743 }
1744
1745 /*
1746  * This function unregisters the SDIO device.
1747  *
1748  * The SDIO IRQ is released, the function is disabled and driver
1749  * data is set to null.
1750  */
1751 static void
1752 mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
1753 {
1754         struct sdio_mmc_card *card = adapter->card;
1755
1756         if (adapter->card) {
1757                 sdio_claim_host(card->func);
1758                 sdio_disable_func(card->func);
1759                 sdio_release_host(card->func);
1760         }
1761 }
1762
1763 /*
1764  * This function registers the SDIO device.
1765  *
1766  * SDIO IRQ is claimed, block size is set and driver data is initialized.
1767  */
1768 static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
1769 {
1770         int ret;
1771         struct sdio_mmc_card *card = adapter->card;
1772         struct sdio_func *func = card->func;
1773
1774         /* save adapter pointer in card */
1775         card->adapter = adapter;
1776         adapter->tx_buf_size = card->tx_buf_size;
1777
1778         sdio_claim_host(func);
1779
1780         /* Set block size */
1781         ret = sdio_set_block_size(card->func, MWIFIEX_SDIO_BLOCK_SIZE);
1782         sdio_release_host(func);
1783         if (ret) {
1784                 pr_err("cannot set SDIO block size\n");
1785                 return ret;
1786         }
1787
1788
1789         adapter->dev = &func->dev;
1790
1791         strcpy(adapter->fw_name, card->firmware);
1792         adapter->mem_type_mapping_tbl = mem_type_mapping_tbl;
1793         adapter->num_mem_types = ARRAY_SIZE(mem_type_mapping_tbl);
1794
1795         return 0;
1796 }
1797
1798 /*
1799  * This function initializes the SDIO driver.
1800  *
1801  * The following initializations steps are followed -
1802  *      - Read the Host interrupt status register to acknowledge
1803  *        the first interrupt got from bootloader
1804  *      - Disable host interrupt mask register
1805  *      - Get SDIO port
1806  *      - Initialize SDIO variables in card
1807  *      - Allocate MP registers
1808  *      - Allocate MPA Tx and Rx buffers
1809  */
1810 static int mwifiex_init_sdio(struct mwifiex_adapter *adapter)
1811 {
1812         struct sdio_mmc_card *card = adapter->card;
1813         const struct mwifiex_sdio_card_reg *reg = card->reg;
1814         int ret;
1815         u8 sdio_ireg;
1816
1817         sdio_set_drvdata(card->func, card);
1818
1819         /*
1820          * Read the HOST_INT_STATUS_REG for ACK the first interrupt got
1821          * from the bootloader. If we don't do this we get a interrupt
1822          * as soon as we register the irq.
1823          */
1824         mwifiex_read_reg(adapter, HOST_INTSTATUS_REG, &sdio_ireg);
1825
1826         /* Get SDIO ioport */
1827         mwifiex_init_sdio_ioport(adapter);
1828
1829         /* Initialize SDIO variables in card */
1830         card->mp_rd_bitmap = 0;
1831         card->mp_wr_bitmap = 0;
1832         card->curr_rd_port = reg->start_rd_port;
1833         card->curr_wr_port = reg->start_wr_port;
1834
1835         card->mp_data_port_mask = reg->data_port_mask;
1836
1837         card->mpa_tx.buf_len = 0;
1838         card->mpa_tx.pkt_cnt = 0;
1839         card->mpa_tx.start_port = 0;
1840
1841         card->mpa_tx.enabled = 1;
1842         card->mpa_tx.pkt_aggr_limit = card->mp_agg_pkt_limit;
1843
1844         card->mpa_rx.buf_len = 0;
1845         card->mpa_rx.pkt_cnt = 0;
1846         card->mpa_rx.start_port = 0;
1847
1848         card->mpa_rx.enabled = 1;
1849         card->mpa_rx.pkt_aggr_limit = card->mp_agg_pkt_limit;
1850
1851         /* Allocate buffers for SDIO MP-A */
1852         card->mp_regs = kzalloc(reg->max_mp_regs, GFP_KERNEL);
1853         if (!card->mp_regs)
1854                 return -ENOMEM;
1855
1856         /* Allocate skb pointer buffers */
1857         card->mpa_rx.skb_arr = kzalloc((sizeof(void *)) *
1858                                        card->mp_agg_pkt_limit, GFP_KERNEL);
1859         card->mpa_rx.len_arr = kzalloc(sizeof(*card->mpa_rx.len_arr) *
1860                                        card->mp_agg_pkt_limit, GFP_KERNEL);
1861         ret = mwifiex_alloc_sdio_mpa_buffers(adapter,
1862                                              card->mp_tx_agg_buf_size,
1863                                              card->mp_rx_agg_buf_size);
1864         if (ret) {
1865                 dev_err(adapter->dev, "failed to alloc sdio mp-a buffers\n");
1866                 kfree(card->mp_regs);
1867                 return -1;
1868         }
1869
1870         return ret;
1871 }
1872
1873 /*
1874  * This function resets the MPA Tx and Rx buffers.
1875  */
1876 static void mwifiex_cleanup_mpa_buf(struct mwifiex_adapter *adapter)
1877 {
1878         struct sdio_mmc_card *card = adapter->card;
1879
1880         MP_TX_AGGR_BUF_RESET(card);
1881         MP_RX_AGGR_BUF_RESET(card);
1882 }
1883
1884 /*
1885  * This function cleans up the allocated card buffers.
1886  *
1887  * The following are freed by this function -
1888  *      - MP registers
1889  *      - MPA Tx buffer
1890  *      - MPA Rx buffer
1891  */
1892 static void mwifiex_cleanup_sdio(struct mwifiex_adapter *adapter)
1893 {
1894         struct sdio_mmc_card *card = adapter->card;
1895
1896         kfree(card->mp_regs);
1897         kfree(card->mpa_rx.skb_arr);
1898         kfree(card->mpa_rx.len_arr);
1899         kfree(card->mpa_tx.buf);
1900         kfree(card->mpa_rx.buf);
1901         sdio_set_drvdata(card->func, NULL);
1902         kfree(card);
1903 }
1904
1905 /*
1906  * This function updates the MP end port in card.
1907  */
1908 static void
1909 mwifiex_update_mp_end_port(struct mwifiex_adapter *adapter, u16 port)
1910 {
1911         struct sdio_mmc_card *card = adapter->card;
1912         const struct mwifiex_sdio_card_reg *reg = card->reg;
1913         int i;
1914
1915         card->mp_end_port = port;
1916
1917         card->mp_data_port_mask = reg->data_port_mask;
1918
1919         if (reg->start_wr_port) {
1920                 for (i = 1; i <= card->max_ports - card->mp_end_port; i++)
1921                         card->mp_data_port_mask &=
1922                                         ~(1 << (card->max_ports - i));
1923         }
1924
1925         card->curr_wr_port = reg->start_wr_port;
1926
1927         dev_dbg(adapter->dev, "cmd: mp_end_port %d, data port mask 0x%x\n",
1928                 port, card->mp_data_port_mask);
1929 }
1930
1931 static void mwifiex_sdio_card_reset_work(struct mwifiex_adapter *adapter)
1932 {
1933         struct sdio_mmc_card *card = adapter->card;
1934         struct mmc_host *target = card->func->card->host;
1935
1936         /* The actual reset operation must be run outside of driver thread.
1937          * This is because mmc_remove_host() will cause the device to be
1938          * instantly destroyed, and the driver then needs to end its thread,
1939          * leading to a deadlock.
1940          *
1941          * We run it in a totally independent workqueue.
1942          */
1943
1944         pr_err("Resetting card...\n");
1945         mmc_remove_host(target);
1946         /* 20ms delay is based on experiment with sdhci controller */
1947         mdelay(20);
1948         target->rescan_entered = 0; /* rescan non-removable cards */
1949         mmc_add_host(target);
1950 }
1951
1952 /* This function read/write firmware */
1953 static enum
1954 rdwr_status mwifiex_sdio_rdwr_firmware(struct mwifiex_adapter *adapter,
1955                                        u8 doneflag)
1956 {
1957         struct sdio_mmc_card *card = adapter->card;
1958         int ret, tries;
1959         u8 ctrl_data = 0;
1960
1961         sdio_writeb(card->func, FW_DUMP_HOST_READY, card->reg->fw_dump_ctrl,
1962                     &ret);
1963         if (ret) {
1964                 dev_err(adapter->dev, "SDIO Write ERR\n");
1965                 return RDWR_STATUS_FAILURE;
1966         }
1967         for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
1968                 ctrl_data = sdio_readb(card->func, card->reg->fw_dump_ctrl,
1969                                        &ret);
1970                 if (ret) {
1971                         dev_err(adapter->dev, "SDIO read err\n");
1972                         return RDWR_STATUS_FAILURE;
1973                 }
1974                 if (ctrl_data == FW_DUMP_DONE)
1975                         break;
1976                 if (doneflag && ctrl_data == doneflag)
1977                         return RDWR_STATUS_DONE;
1978                 if (ctrl_data != FW_DUMP_HOST_READY) {
1979                         dev_info(adapter->dev,
1980                                  "The ctrl reg was changed, re-try again!\n");
1981                         sdio_writeb(card->func, FW_DUMP_HOST_READY,
1982                                     card->reg->fw_dump_ctrl, &ret);
1983                         if (ret) {
1984                                 dev_err(adapter->dev, "SDIO write err\n");
1985                                 return RDWR_STATUS_FAILURE;
1986                         }
1987                 }
1988                 usleep_range(100, 200);
1989         }
1990         if (ctrl_data == FW_DUMP_HOST_READY) {
1991                 dev_err(adapter->dev, "Fail to pull ctrl_data\n");
1992                 return RDWR_STATUS_FAILURE;
1993         }
1994
1995         return RDWR_STATUS_SUCCESS;
1996 }
1997
1998 /* This function dump firmware memory to file */
1999 static void mwifiex_sdio_fw_dump_work(struct work_struct *work)
2000 {
2001         struct mwifiex_adapter *adapter =
2002                         container_of(work, struct mwifiex_adapter, iface_work);
2003         struct sdio_mmc_card *card = adapter->card;
2004         int ret = 0;
2005         unsigned int reg, reg_start, reg_end;
2006         u8 *dbg_ptr, *end_ptr, dump_num, idx, i, read_reg, doneflag = 0;
2007         enum rdwr_status stat;
2008         u32 memory_size;
2009         static char *env[] = { "DRIVER=mwifiex_sdio", "EVENT=fw_dump", NULL };
2010
2011         if (!card->supports_fw_dump)
2012                 return;
2013
2014         for (idx = 0; idx < ARRAY_SIZE(mem_type_mapping_tbl); idx++) {
2015                 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
2016
2017                 if (entry->mem_ptr) {
2018                         vfree(entry->mem_ptr);
2019                         entry->mem_ptr = NULL;
2020                 }
2021                 entry->mem_size = 0;
2022         }
2023
2024         mwifiex_pm_wakeup_card(adapter);
2025         sdio_claim_host(card->func);
2026
2027         dev_info(adapter->dev, "== mwifiex firmware dump start ==\n");
2028
2029         stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2030         if (stat == RDWR_STATUS_FAILURE)
2031                 goto done;
2032
2033         reg = card->reg->fw_dump_start;
2034         /* Read the number of the memories which will dump */
2035         dump_num = sdio_readb(card->func, reg, &ret);
2036         if (ret) {
2037                 dev_err(adapter->dev, "SDIO read memory length err\n");
2038                 goto done;
2039         }
2040
2041         /* Read the length of every memory which will dump */
2042         for (idx = 0; idx < dump_num; idx++) {
2043                 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
2044
2045                 stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2046                 if (stat == RDWR_STATUS_FAILURE)
2047                         goto done;
2048
2049                 memory_size = 0;
2050                 reg = card->reg->fw_dump_start;
2051                 for (i = 0; i < 4; i++) {
2052                         read_reg = sdio_readb(card->func, reg, &ret);
2053                         if (ret) {
2054                                 dev_err(adapter->dev, "SDIO read err\n");
2055                                 goto done;
2056                         }
2057                         memory_size |= (read_reg << i*8);
2058                         reg++;
2059                 }
2060
2061                 if (memory_size == 0) {
2062                         dev_info(adapter->dev, "Firmware dump Finished!\n");
2063                         break;
2064                 }
2065
2066                 dev_info(adapter->dev,
2067                          "%s_SIZE=0x%x\n", entry->mem_name, memory_size);
2068                 entry->mem_ptr = vmalloc(memory_size + 1);
2069                 entry->mem_size = memory_size;
2070                 if (!entry->mem_ptr) {
2071                         dev_err(adapter->dev, "Vmalloc %s failed\n",
2072                                 entry->mem_name);
2073                         goto done;
2074                 }
2075                 dbg_ptr = entry->mem_ptr;
2076                 end_ptr = dbg_ptr + memory_size;
2077
2078                 doneflag = entry->done_flag;
2079                 dev_info(adapter->dev, "Start %s output, please wait...\n",
2080                          entry->mem_name);
2081
2082                 do {
2083                         stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2084                         if (stat == RDWR_STATUS_FAILURE)
2085                                 goto done;
2086
2087                         reg_start = card->reg->fw_dump_start;
2088                         reg_end = card->reg->fw_dump_end;
2089                         for (reg = reg_start; reg <= reg_end; reg++) {
2090                                 *dbg_ptr = sdio_readb(card->func, reg, &ret);
2091                                 if (ret) {
2092                                         dev_err(adapter->dev,
2093                                                 "SDIO read err\n");
2094                                         goto done;
2095                                 }
2096                                 if (dbg_ptr < end_ptr)
2097                                         dbg_ptr++;
2098                                 else
2099                                         dev_err(adapter->dev,
2100                                                 "Allocated buf not enough\n");
2101                         }
2102
2103                         if (stat != RDWR_STATUS_DONE)
2104                                 continue;
2105
2106                         dev_info(adapter->dev, "%s done: size=0x%tx\n",
2107                                  entry->mem_name, dbg_ptr - entry->mem_ptr);
2108                         break;
2109                 } while (1);
2110         }
2111         dev_info(adapter->dev, "== mwifiex firmware dump end ==\n");
2112
2113         kobject_uevent_env(&adapter->wiphy->dev.kobj, KOBJ_CHANGE, env);
2114
2115 done:
2116         sdio_release_host(card->func);
2117         adapter->curr_mem_idx = 0;
2118 }
2119
2120 static void mwifiex_sdio_work(struct work_struct *work)
2121 {
2122         struct mwifiex_adapter *adapter =
2123                         container_of(work, struct mwifiex_adapter, iface_work);
2124
2125         if (test_and_clear_bit(MWIFIEX_IFACE_WORK_CARD_RESET,
2126                                &adapter->iface_work_flags))
2127                 mwifiex_sdio_card_reset_work(adapter);
2128         if (test_and_clear_bit(MWIFIEX_IFACE_WORK_FW_DUMP,
2129                                &adapter->iface_work_flags))
2130                 mwifiex_sdio_fw_dump_work(work);
2131 }
2132
2133 /* This function resets the card */
2134 static void mwifiex_sdio_card_reset(struct mwifiex_adapter *adapter)
2135 {
2136         if (test_bit(MWIFIEX_IFACE_WORK_CARD_RESET, &adapter->iface_work_flags))
2137                 return;
2138
2139         set_bit(MWIFIEX_IFACE_WORK_CARD_RESET, &adapter->iface_work_flags);
2140
2141         schedule_work(&adapter->iface_work);
2142 }
2143
2144 /* This function dumps FW information */
2145 static void mwifiex_sdio_fw_dump(struct mwifiex_adapter *adapter)
2146 {
2147         if (test_bit(MWIFIEX_IFACE_WORK_FW_DUMP, &adapter->iface_work_flags))
2148                 return;
2149
2150         set_bit(MWIFIEX_IFACE_WORK_FW_DUMP, &adapter->iface_work_flags);
2151         schedule_work(&adapter->iface_work);
2152 }
2153
2154 static struct mwifiex_if_ops sdio_ops = {
2155         .init_if = mwifiex_init_sdio,
2156         .cleanup_if = mwifiex_cleanup_sdio,
2157         .check_fw_status = mwifiex_check_fw_status,
2158         .prog_fw = mwifiex_prog_fw_w_helper,
2159         .register_dev = mwifiex_register_dev,
2160         .unregister_dev = mwifiex_unregister_dev,
2161         .enable_int = mwifiex_sdio_enable_host_int,
2162         .disable_int = mwifiex_sdio_disable_host_int,
2163         .process_int_status = mwifiex_process_int_status,
2164         .host_to_card = mwifiex_sdio_host_to_card,
2165         .wakeup = mwifiex_pm_wakeup_card,
2166         .wakeup_complete = mwifiex_pm_wakeup_card_complete,
2167
2168         /* SDIO specific */
2169         .update_mp_end_port = mwifiex_update_mp_end_port,
2170         .cleanup_mpa_buf = mwifiex_cleanup_mpa_buf,
2171         .cmdrsp_complete = mwifiex_sdio_cmdrsp_complete,
2172         .event_complete = mwifiex_sdio_event_complete,
2173         .card_reset = mwifiex_sdio_card_reset,
2174         .iface_work = mwifiex_sdio_work,
2175         .fw_dump = mwifiex_sdio_fw_dump,
2176 };
2177
2178 /*
2179  * This function initializes the SDIO driver.
2180  *
2181  * This initiates the semaphore and registers the device with
2182  * SDIO bus.
2183  */
2184 static int
2185 mwifiex_sdio_init_module(void)
2186 {
2187         sema_init(&add_remove_card_sem, 1);
2188
2189         /* Clear the flag in case user removes the card. */
2190         user_rmmod = 0;
2191
2192         return sdio_register_driver(&mwifiex_sdio);
2193 }
2194
2195 /*
2196  * This function cleans up the SDIO driver.
2197  *
2198  * The following major steps are followed for cleanup -
2199  *      - Resume the device if its suspended
2200  *      - Disconnect the device if connected
2201  *      - Shutdown the firmware
2202  *      - Unregister the device from SDIO bus.
2203  */
2204 static void
2205 mwifiex_sdio_cleanup_module(void)
2206 {
2207         if (!down_interruptible(&add_remove_card_sem))
2208                 up(&add_remove_card_sem);
2209
2210         /* Set the flag as user is removing this module. */
2211         user_rmmod = 1;
2212
2213         sdio_unregister_driver(&mwifiex_sdio);
2214 }
2215
2216 module_init(mwifiex_sdio_init_module);
2217 module_exit(mwifiex_sdio_cleanup_module);
2218
2219 MODULE_AUTHOR("Marvell International Ltd.");
2220 MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION);
2221 MODULE_VERSION(SDIO_VERSION);
2222 MODULE_LICENSE("GPL v2");
2223 MODULE_FIRMWARE(SD8786_DEFAULT_FW_NAME);
2224 MODULE_FIRMWARE(SD8787_DEFAULT_FW_NAME);
2225 MODULE_FIRMWARE(SD8797_DEFAULT_FW_NAME);
2226 MODULE_FIRMWARE(SD8897_DEFAULT_FW_NAME);