cbd9dcd88b989d0e7ffcc824b9bcb3bf2e1daea7
[cascardo/linux.git] / drivers / net / wireless / marvell / 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 static unsigned long iface_work_flags;
51
52 static struct semaphore add_remove_card_sem;
53
54 static struct memory_type_mapping generic_mem_type_map[] = {
55         {"DUMP", NULL, 0, 0xDD},
56 };
57
58 static struct memory_type_mapping mem_type_mapping_tbl[] = {
59         {"ITCM", NULL, 0, 0xF0},
60         {"DTCM", NULL, 0, 0xF1},
61         {"SQRAM", NULL, 0, 0xF2},
62         {"APU", NULL, 0, 0xF3},
63         {"CIU", NULL, 0, 0xF4},
64         {"ICU", NULL, 0, 0xF5},
65         {"MAC", NULL, 0, 0xF6},
66         {"EXT7", NULL, 0, 0xF7},
67         {"EXT8", NULL, 0, 0xF8},
68         {"EXT9", NULL, 0, 0xF9},
69         {"EXT10", NULL, 0, 0xFA},
70         {"EXT11", NULL, 0, 0xFB},
71         {"EXT12", NULL, 0, 0xFC},
72         {"EXT13", NULL, 0, 0xFD},
73         {"EXTLAST", NULL, 0, 0xFE},
74 };
75
76 static const struct of_device_id mwifiex_sdio_of_match_table[] = {
77         { .compatible = "marvell,sd8897" },
78         { .compatible = "marvell,sd8997" },
79         { }
80 };
81
82 static irqreturn_t mwifiex_wake_irq_wifi(int irq, void *priv)
83 {
84         struct mwifiex_plt_wake_cfg *cfg = priv;
85
86         if (cfg->irq_wifi >= 0) {
87                 pr_info("%s: wake by wifi", __func__);
88                 cfg->wake_by_wifi = true;
89                 disable_irq_nosync(irq);
90         }
91
92         return IRQ_HANDLED;
93 }
94
95 /* This function parse device tree node using mmc subnode devicetree API.
96  * The device node is saved in card->plt_of_node.
97  * if the device tree node exist and include interrupts attributes, this
98  * function will also request platform specific wakeup interrupt.
99  */
100 static int mwifiex_sdio_probe_of(struct device *dev, struct sdio_mmc_card *card)
101 {
102         struct mwifiex_plt_wake_cfg *cfg;
103         int ret;
104
105         if (!dev->of_node ||
106             !of_match_node(mwifiex_sdio_of_match_table, dev->of_node)) {
107                 pr_err("sdio platform data not available");
108                 return -1;
109         }
110
111         card->plt_of_node = dev->of_node;
112         card->plt_wake_cfg = devm_kzalloc(dev, sizeof(*card->plt_wake_cfg),
113                                           GFP_KERNEL);
114         cfg = card->plt_wake_cfg;
115         if (cfg && card->plt_of_node) {
116                 cfg->irq_wifi = irq_of_parse_and_map(card->plt_of_node, 0);
117                 if (!cfg->irq_wifi) {
118                         dev_err(dev, "fail to parse irq_wifi from device tree");
119                 } else {
120                         ret = devm_request_irq(dev, cfg->irq_wifi,
121                                                mwifiex_wake_irq_wifi,
122                                                IRQF_TRIGGER_LOW,
123                                                "wifi_wake", cfg);
124                         if (ret) {
125                                 dev_err(dev,
126                                         "Failed to request irq_wifi %d (%d)\n",
127                                         cfg->irq_wifi, ret);
128                         }
129                         disable_irq(cfg->irq_wifi);
130                 }
131         }
132
133         return 0;
134 }
135
136 /*
137  * SDIO probe.
138  *
139  * This function probes an mwifiex device and registers it. It allocates
140  * the card structure, enables SDIO function number and initiates the
141  * device registration and initialization procedure by adding a logical
142  * interface.
143  */
144 static int
145 mwifiex_sdio_probe(struct sdio_func *func, const struct sdio_device_id *id)
146 {
147         int ret;
148         struct sdio_mmc_card *card = NULL;
149
150         pr_debug("info: vendor=0x%4.04X device=0x%4.04X class=%d function=%d\n",
151                  func->vendor, func->device, func->class, func->num);
152
153         card = kzalloc(sizeof(struct sdio_mmc_card), GFP_KERNEL);
154         if (!card)
155                 return -ENOMEM;
156
157         card->func = func;
158         card->device_id = id;
159
160         func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
161
162         if (id->driver_data) {
163                 struct mwifiex_sdio_device *data = (void *)id->driver_data;
164
165                 card->firmware = data->firmware;
166                 card->reg = data->reg;
167                 card->max_ports = data->max_ports;
168                 card->mp_agg_pkt_limit = data->mp_agg_pkt_limit;
169                 card->supports_sdio_new_mode = data->supports_sdio_new_mode;
170                 card->has_control_mask = data->has_control_mask;
171                 card->tx_buf_size = data->tx_buf_size;
172                 card->mp_tx_agg_buf_size = data->mp_tx_agg_buf_size;
173                 card->mp_rx_agg_buf_size = data->mp_rx_agg_buf_size;
174                 card->can_dump_fw = data->can_dump_fw;
175                 card->fw_dump_enh = data->fw_dump_enh;
176                 card->can_auto_tdls = data->can_auto_tdls;
177                 card->can_ext_scan = data->can_ext_scan;
178         }
179
180         sdio_claim_host(func);
181         ret = sdio_enable_func(func);
182         sdio_release_host(func);
183
184         if (ret) {
185                 pr_err("%s: failed to enable function\n", __func__);
186                 kfree(card);
187                 return -EIO;
188         }
189
190         /* device tree node parsing and platform specific configuration*/
191         mwifiex_sdio_probe_of(&func->dev, card);
192
193         if (mwifiex_add_card(card, &add_remove_card_sem, &sdio_ops,
194                              MWIFIEX_SDIO)) {
195                 pr_err("%s: add card failed\n", __func__);
196                 kfree(card);
197                 sdio_claim_host(func);
198                 ret = sdio_disable_func(func);
199                 sdio_release_host(func);
200                 ret = -1;
201         }
202
203         return ret;
204 }
205
206 /*
207  * SDIO resume.
208  *
209  * Kernel needs to suspend all functions separately. Therefore all
210  * registered functions must have drivers with suspend and resume
211  * methods. Failing that the kernel simply removes the whole card.
212  *
213  * If already not resumed, this function turns on the traffic and
214  * sends a host sleep cancel request to the firmware.
215  */
216 static int mwifiex_sdio_resume(struct device *dev)
217 {
218         struct sdio_func *func = dev_to_sdio_func(dev);
219         struct sdio_mmc_card *card;
220         struct mwifiex_adapter *adapter;
221         mmc_pm_flag_t pm_flag = 0;
222
223         if (func) {
224                 pm_flag = sdio_get_host_pm_caps(func);
225                 card = sdio_get_drvdata(func);
226                 if (!card || !card->adapter) {
227                         pr_err("resume: invalid card or adapter\n");
228                         return 0;
229                 }
230         } else {
231                 pr_err("resume: sdio_func is not specified\n");
232                 return 0;
233         }
234
235         adapter = card->adapter;
236
237         if (!adapter->is_suspended) {
238                 mwifiex_dbg(adapter, WARN,
239                             "device already resumed\n");
240                 return 0;
241         }
242
243         adapter->is_suspended = false;
244
245         /* Disable Host Sleep */
246         mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
247                           MWIFIEX_SYNC_CMD);
248
249         /* Disable platform specific wakeup interrupt */
250         if (card->plt_wake_cfg && card->plt_wake_cfg->irq_wifi >= 0) {
251                 disable_irq_wake(card->plt_wake_cfg->irq_wifi);
252                 if (!card->plt_wake_cfg->wake_by_wifi)
253                         disable_irq(card->plt_wake_cfg->irq_wifi);
254         }
255
256         return 0;
257 }
258
259 /*
260  * SDIO remove.
261  *
262  * This function removes the interface and frees up the card structure.
263  */
264 static void
265 mwifiex_sdio_remove(struct sdio_func *func)
266 {
267         struct sdio_mmc_card *card;
268         struct mwifiex_adapter *adapter;
269         struct mwifiex_private *priv;
270
271         card = sdio_get_drvdata(func);
272         if (!card)
273                 return;
274
275         adapter = card->adapter;
276         if (!adapter || !adapter->priv_num)
277                 return;
278
279         mwifiex_dbg(adapter, INFO, "info: SDIO func num=%d\n", func->num);
280
281         if (user_rmmod) {
282                 if (adapter->is_suspended)
283                         mwifiex_sdio_resume(adapter->dev);
284
285                 mwifiex_deauthenticate_all(adapter);
286
287                 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
288                 mwifiex_disable_auto_ds(priv);
289                 mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
290         }
291
292         mwifiex_remove_card(card->adapter, &add_remove_card_sem);
293 }
294
295 /*
296  * SDIO suspend.
297  *
298  * Kernel needs to suspend all functions separately. Therefore all
299  * registered functions must have drivers with suspend and resume
300  * methods. Failing that the kernel simply removes the whole card.
301  *
302  * If already not suspended, this function allocates and sends a host
303  * sleep activate request to the firmware and turns off the traffic.
304  */
305 static int mwifiex_sdio_suspend(struct device *dev)
306 {
307         struct sdio_func *func = dev_to_sdio_func(dev);
308         struct sdio_mmc_card *card;
309         struct mwifiex_adapter *adapter;
310         mmc_pm_flag_t pm_flag = 0;
311         int ret = 0;
312
313         if (func) {
314                 pm_flag = sdio_get_host_pm_caps(func);
315                 pr_debug("cmd: %s: suspend: PM flag = 0x%x\n",
316                          sdio_func_id(func), pm_flag);
317                 if (!(pm_flag & MMC_PM_KEEP_POWER)) {
318                         pr_err("%s: cannot remain alive while host is"
319                                 " suspended\n", sdio_func_id(func));
320                         return -ENOSYS;
321                 }
322
323                 card = sdio_get_drvdata(func);
324                 if (!card || !card->adapter) {
325                         pr_err("suspend: invalid card or adapter\n");
326                         return 0;
327                 }
328         } else {
329                 pr_err("suspend: sdio_func is not specified\n");
330                 return 0;
331         }
332
333         adapter = card->adapter;
334
335         /* Enable platform specific wakeup interrupt */
336         if (card->plt_wake_cfg && card->plt_wake_cfg->irq_wifi >= 0) {
337                 card->plt_wake_cfg->wake_by_wifi = false;
338                 enable_irq(card->plt_wake_cfg->irq_wifi);
339                 enable_irq_wake(card->plt_wake_cfg->irq_wifi);
340         }
341
342         /* Enable the Host Sleep */
343         if (!mwifiex_enable_hs(adapter)) {
344                 mwifiex_dbg(adapter, ERROR,
345                             "cmd: failed to suspend\n");
346                 adapter->hs_enabling = false;
347                 return -EFAULT;
348         }
349
350         mwifiex_dbg(adapter, INFO,
351                     "cmd: suspend with MMC_PM_KEEP_POWER\n");
352         ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
353
354         /* Indicate device suspended */
355         adapter->is_suspended = true;
356         adapter->hs_enabling = false;
357
358         return ret;
359 }
360
361 /* Device ID for SD8786 */
362 #define SDIO_DEVICE_ID_MARVELL_8786   (0x9116)
363 /* Device ID for SD8787 */
364 #define SDIO_DEVICE_ID_MARVELL_8787   (0x9119)
365 /* Device ID for SD8797 */
366 #define SDIO_DEVICE_ID_MARVELL_8797   (0x9129)
367 /* Device ID for SD8897 */
368 #define SDIO_DEVICE_ID_MARVELL_8897   (0x912d)
369 /* Device ID for SD8887 */
370 #define SDIO_DEVICE_ID_MARVELL_8887   (0x9135)
371 /* Device ID for SD8801 */
372 #define SDIO_DEVICE_ID_MARVELL_8801   (0x9139)
373 /* Device ID for SD8997 */
374 #define SDIO_DEVICE_ID_MARVELL_8997   (0x9141)
375
376
377 /* WLAN IDs */
378 static const struct sdio_device_id mwifiex_ids[] = {
379         {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8786),
380                 .driver_data = (unsigned long) &mwifiex_sdio_sd8786},
381         {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787),
382                 .driver_data = (unsigned long) &mwifiex_sdio_sd8787},
383         {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8797),
384                 .driver_data = (unsigned long) &mwifiex_sdio_sd8797},
385         {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8897),
386                 .driver_data = (unsigned long) &mwifiex_sdio_sd8897},
387         {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8887),
388                 .driver_data = (unsigned long)&mwifiex_sdio_sd8887},
389         {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8801),
390                 .driver_data = (unsigned long)&mwifiex_sdio_sd8801},
391         {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8997),
392                 .driver_data = (unsigned long)&mwifiex_sdio_sd8997},
393         {},
394 };
395
396 MODULE_DEVICE_TABLE(sdio, mwifiex_ids);
397
398 static const struct dev_pm_ops mwifiex_sdio_pm_ops = {
399         .suspend = mwifiex_sdio_suspend,
400         .resume = mwifiex_sdio_resume,
401 };
402
403 static struct sdio_driver mwifiex_sdio = {
404         .name = "mwifiex_sdio",
405         .id_table = mwifiex_ids,
406         .probe = mwifiex_sdio_probe,
407         .remove = mwifiex_sdio_remove,
408         .drv = {
409                 .owner = THIS_MODULE,
410                 .pm = &mwifiex_sdio_pm_ops,
411         }
412 };
413
414 /* Write data into SDIO card register. Caller claims SDIO device. */
415 static int
416 mwifiex_write_reg_locked(struct sdio_func *func, u32 reg, u8 data)
417 {
418         int ret = -1;
419         sdio_writeb(func, data, reg, &ret);
420         return ret;
421 }
422
423 /*
424  * This function writes data into SDIO card register.
425  */
426 static int
427 mwifiex_write_reg(struct mwifiex_adapter *adapter, u32 reg, u8 data)
428 {
429         struct sdio_mmc_card *card = adapter->card;
430         int ret;
431
432         sdio_claim_host(card->func);
433         ret = mwifiex_write_reg_locked(card->func, reg, data);
434         sdio_release_host(card->func);
435
436         return ret;
437 }
438
439 /*
440  * This function reads data from SDIO card register.
441  */
442 static int
443 mwifiex_read_reg(struct mwifiex_adapter *adapter, u32 reg, u8 *data)
444 {
445         struct sdio_mmc_card *card = adapter->card;
446         int ret = -1;
447         u8 val;
448
449         sdio_claim_host(card->func);
450         val = sdio_readb(card->func, reg, &ret);
451         sdio_release_host(card->func);
452
453         *data = val;
454
455         return ret;
456 }
457
458 /*
459  * This function writes multiple data into SDIO card memory.
460  *
461  * This does not work in suspended mode.
462  */
463 static int
464 mwifiex_write_data_sync(struct mwifiex_adapter *adapter,
465                         u8 *buffer, u32 pkt_len, u32 port)
466 {
467         struct sdio_mmc_card *card = adapter->card;
468         int ret;
469         u8 blk_mode =
470                 (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE : BLOCK_MODE;
471         u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
472         u32 blk_cnt =
473                 (blk_mode ==
474                  BLOCK_MODE) ? (pkt_len /
475                                 MWIFIEX_SDIO_BLOCK_SIZE) : pkt_len;
476         u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
477
478         if (adapter->is_suspended) {
479                 mwifiex_dbg(adapter, ERROR,
480                             "%s: not allowed while suspended\n", __func__);
481                 return -1;
482         }
483
484         sdio_claim_host(card->func);
485
486         ret = sdio_writesb(card->func, ioport, buffer, blk_cnt * blk_size);
487
488         sdio_release_host(card->func);
489
490         return ret;
491 }
492
493 /*
494  * This function reads multiple data from SDIO card memory.
495  */
496 static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *buffer,
497                                   u32 len, u32 port, u8 claim)
498 {
499         struct sdio_mmc_card *card = adapter->card;
500         int ret;
501         u8 blk_mode = (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE
502                        : BLOCK_MODE;
503         u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
504         u32 blk_cnt = (blk_mode == BLOCK_MODE) ? (len / MWIFIEX_SDIO_BLOCK_SIZE)
505                         : len;
506         u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
507
508         if (claim)
509                 sdio_claim_host(card->func);
510
511         ret = sdio_readsb(card->func, buffer, ioport, blk_cnt * blk_size);
512
513         if (claim)
514                 sdio_release_host(card->func);
515
516         return ret;
517 }
518
519 /*
520  * This function wakes up the card.
521  *
522  * A host power up command is written to the card configuration
523  * register to wake up the card.
524  */
525 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
526 {
527         mwifiex_dbg(adapter, EVENT,
528                     "event: wakeup device...\n");
529
530         return mwifiex_write_reg(adapter, CONFIGURATION_REG, HOST_POWER_UP);
531 }
532
533 /*
534  * This function is called after the card has woken up.
535  *
536  * The card configuration register is reset.
537  */
538 static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
539 {
540         mwifiex_dbg(adapter, EVENT,
541                     "cmd: wakeup device completed\n");
542
543         return mwifiex_write_reg(adapter, CONFIGURATION_REG, 0);
544 }
545
546 /*
547  * This function is used to initialize IO ports for the
548  * chipsets supporting SDIO new mode eg SD8897.
549  */
550 static int mwifiex_init_sdio_new_mode(struct mwifiex_adapter *adapter)
551 {
552         u8 reg;
553         struct sdio_mmc_card *card = adapter->card;
554
555         adapter->ioport = MEM_PORT;
556
557         /* enable sdio new mode */
558         if (mwifiex_read_reg(adapter, card->reg->card_cfg_2_1_reg, &reg))
559                 return -1;
560         if (mwifiex_write_reg(adapter, card->reg->card_cfg_2_1_reg,
561                               reg | CMD53_NEW_MODE))
562                 return -1;
563
564         /* Configure cmd port and enable reading rx length from the register */
565         if (mwifiex_read_reg(adapter, card->reg->cmd_cfg_0, &reg))
566                 return -1;
567         if (mwifiex_write_reg(adapter, card->reg->cmd_cfg_0,
568                               reg | CMD_PORT_RD_LEN_EN))
569                 return -1;
570
571         /* Enable Dnld/Upld ready auto reset for cmd port after cmd53 is
572          * completed
573          */
574         if (mwifiex_read_reg(adapter, card->reg->cmd_cfg_1, &reg))
575                 return -1;
576         if (mwifiex_write_reg(adapter, card->reg->cmd_cfg_1,
577                               reg | CMD_PORT_AUTO_EN))
578                 return -1;
579
580         return 0;
581 }
582
583 /* This function initializes the IO ports.
584  *
585  * The following operations are performed -
586  *      - Read the IO ports (0, 1 and 2)
587  *      - Set host interrupt Reset-To-Read to clear
588  *      - Set auto re-enable interrupt
589  */
590 static int mwifiex_init_sdio_ioport(struct mwifiex_adapter *adapter)
591 {
592         u8 reg;
593         struct sdio_mmc_card *card = adapter->card;
594
595         adapter->ioport = 0;
596
597         if (card->supports_sdio_new_mode) {
598                 if (mwifiex_init_sdio_new_mode(adapter))
599                         return -1;
600                 goto cont;
601         }
602
603         /* Read the IO port */
604         if (!mwifiex_read_reg(adapter, card->reg->io_port_0_reg, &reg))
605                 adapter->ioport |= (reg & 0xff);
606         else
607                 return -1;
608
609         if (!mwifiex_read_reg(adapter, card->reg->io_port_1_reg, &reg))
610                 adapter->ioport |= ((reg & 0xff) << 8);
611         else
612                 return -1;
613
614         if (!mwifiex_read_reg(adapter, card->reg->io_port_2_reg, &reg))
615                 adapter->ioport |= ((reg & 0xff) << 16);
616         else
617                 return -1;
618 cont:
619         mwifiex_dbg(adapter, INFO,
620                     "info: SDIO FUNC1 IO port: %#x\n", adapter->ioport);
621
622         /* Set Host interrupt reset to read to clear */
623         if (!mwifiex_read_reg(adapter, card->reg->host_int_rsr_reg, &reg))
624                 mwifiex_write_reg(adapter, card->reg->host_int_rsr_reg,
625                                   reg | card->reg->sdio_int_mask);
626         else
627                 return -1;
628
629         /* Dnld/Upld ready set to auto reset */
630         if (!mwifiex_read_reg(adapter, card->reg->card_misc_cfg_reg, &reg))
631                 mwifiex_write_reg(adapter, card->reg->card_misc_cfg_reg,
632                                   reg | AUTO_RE_ENABLE_INT);
633         else
634                 return -1;
635
636         return 0;
637 }
638
639 /*
640  * This function sends data to the card.
641  */
642 static int mwifiex_write_data_to_card(struct mwifiex_adapter *adapter,
643                                       u8 *payload, u32 pkt_len, u32 port)
644 {
645         u32 i = 0;
646         int ret;
647
648         do {
649                 ret = mwifiex_write_data_sync(adapter, payload, pkt_len, port);
650                 if (ret) {
651                         i++;
652                         mwifiex_dbg(adapter, ERROR,
653                                     "host_to_card, write iomem\t"
654                                     "(%d) failed: %d\n", i, ret);
655                         if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
656                                 mwifiex_dbg(adapter, ERROR,
657                                             "write CFG reg failed\n");
658
659                         ret = -1;
660                         if (i > MAX_WRITE_IOMEM_RETRY)
661                                 return ret;
662                 }
663         } while (ret == -1);
664
665         return ret;
666 }
667
668 /*
669  * This function gets the read port.
670  *
671  * If control port bit is set in MP read bitmap, the control port
672  * is returned, otherwise the current read port is returned and
673  * the value is increased (provided it does not reach the maximum
674  * limit, in which case it is reset to 1)
675  */
676 static int mwifiex_get_rd_port(struct mwifiex_adapter *adapter, u8 *port)
677 {
678         struct sdio_mmc_card *card = adapter->card;
679         const struct mwifiex_sdio_card_reg *reg = card->reg;
680         u32 rd_bitmap = card->mp_rd_bitmap;
681
682         mwifiex_dbg(adapter, DATA,
683                     "data: mp_rd_bitmap=0x%08x\n", rd_bitmap);
684
685         if (card->supports_sdio_new_mode) {
686                 if (!(rd_bitmap & reg->data_port_mask))
687                         return -1;
688         } else {
689                 if (!(rd_bitmap & (CTRL_PORT_MASK | reg->data_port_mask)))
690                         return -1;
691         }
692
693         if ((card->has_control_mask) &&
694             (card->mp_rd_bitmap & CTRL_PORT_MASK)) {
695                 card->mp_rd_bitmap &= (u32) (~CTRL_PORT_MASK);
696                 *port = CTRL_PORT;
697                 mwifiex_dbg(adapter, DATA,
698                             "data: port=%d mp_rd_bitmap=0x%08x\n",
699                             *port, card->mp_rd_bitmap);
700                 return 0;
701         }
702
703         if (!(card->mp_rd_bitmap & (1 << card->curr_rd_port)))
704                 return -1;
705
706         /* We are now handling the SDIO data ports */
707         card->mp_rd_bitmap &= (u32)(~(1 << card->curr_rd_port));
708         *port = card->curr_rd_port;
709
710         if (++card->curr_rd_port == card->max_ports)
711                 card->curr_rd_port = reg->start_rd_port;
712
713         mwifiex_dbg(adapter, DATA,
714                     "data: port=%d mp_rd_bitmap=0x%08x -> 0x%08x\n",
715                     *port, rd_bitmap, card->mp_rd_bitmap);
716
717         return 0;
718 }
719
720 /*
721  * This function gets the write port for data.
722  *
723  * The current write port is returned if available and the value is
724  * increased (provided it does not reach the maximum limit, in which
725  * case it is reset to 1)
726  */
727 static int mwifiex_get_wr_port_data(struct mwifiex_adapter *adapter, u32 *port)
728 {
729         struct sdio_mmc_card *card = adapter->card;
730         const struct mwifiex_sdio_card_reg *reg = card->reg;
731         u32 wr_bitmap = card->mp_wr_bitmap;
732
733         mwifiex_dbg(adapter, DATA,
734                     "data: mp_wr_bitmap=0x%08x\n", wr_bitmap);
735
736         if (!(wr_bitmap & card->mp_data_port_mask)) {
737                 adapter->data_sent = true;
738                 return -EBUSY;
739         }
740
741         if (card->mp_wr_bitmap & (1 << card->curr_wr_port)) {
742                 card->mp_wr_bitmap &= (u32) (~(1 << card->curr_wr_port));
743                 *port = card->curr_wr_port;
744                 if (++card->curr_wr_port == card->mp_end_port)
745                         card->curr_wr_port = reg->start_wr_port;
746         } else {
747                 adapter->data_sent = true;
748                 return -EBUSY;
749         }
750
751         if ((card->has_control_mask) && (*port == CTRL_PORT)) {
752                 mwifiex_dbg(adapter, ERROR,
753                             "invalid data port=%d cur port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
754                             *port, card->curr_wr_port, wr_bitmap,
755                             card->mp_wr_bitmap);
756                 return -1;
757         }
758
759         mwifiex_dbg(adapter, DATA,
760                     "data: port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
761                     *port, wr_bitmap, card->mp_wr_bitmap);
762
763         return 0;
764 }
765
766 /*
767  * This function polls the card status.
768  */
769 static int
770 mwifiex_sdio_poll_card_status(struct mwifiex_adapter *adapter, u8 bits)
771 {
772         struct sdio_mmc_card *card = adapter->card;
773         u32 tries;
774         u8 cs;
775
776         for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
777                 if (mwifiex_read_reg(adapter, card->reg->poll_reg, &cs))
778                         break;
779                 else if ((cs & bits) == bits)
780                         return 0;
781
782                 usleep_range(10, 20);
783         }
784
785         mwifiex_dbg(adapter, ERROR,
786                     "poll card status failed, tries = %d\n", tries);
787
788         return -1;
789 }
790
791 /*
792  * This function reads the firmware status.
793  */
794 static int
795 mwifiex_sdio_read_fw_status(struct mwifiex_adapter *adapter, u16 *dat)
796 {
797         struct sdio_mmc_card *card = adapter->card;
798         const struct mwifiex_sdio_card_reg *reg = card->reg;
799         u8 fws0, fws1;
800
801         if (mwifiex_read_reg(adapter, reg->status_reg_0, &fws0))
802                 return -1;
803
804         if (mwifiex_read_reg(adapter, reg->status_reg_1, &fws1))
805                 return -1;
806
807         *dat = (u16) ((fws1 << 8) | fws0);
808
809         return 0;
810 }
811
812 /*
813  * This function disables the host interrupt.
814  *
815  * The host interrupt mask is read, the disable bit is reset and
816  * written back to the card host interrupt mask register.
817  */
818 static void mwifiex_sdio_disable_host_int(struct mwifiex_adapter *adapter)
819 {
820         struct sdio_mmc_card *card = adapter->card;
821         struct sdio_func *func = card->func;
822
823         sdio_claim_host(func);
824         mwifiex_write_reg_locked(func, card->reg->host_int_mask_reg, 0);
825         sdio_release_irq(func);
826         sdio_release_host(func);
827 }
828
829 /*
830  * This function reads the interrupt status from card.
831  */
832 static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
833 {
834         struct sdio_mmc_card *card = adapter->card;
835         u8 sdio_ireg;
836         unsigned long flags;
837
838         if (mwifiex_read_data_sync(adapter, card->mp_regs,
839                                    card->reg->max_mp_regs,
840                                    REG_PORT | MWIFIEX_SDIO_BYTE_MODE_MASK, 0)) {
841                 mwifiex_dbg(adapter, ERROR, "read mp_regs failed\n");
842                 return;
843         }
844
845         sdio_ireg = card->mp_regs[card->reg->host_int_status_reg];
846         if (sdio_ireg) {
847                 /*
848                  * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
849                  * For SDIO new mode CMD port interrupts
850                  *      DN_LD_CMD_PORT_HOST_INT_STATUS and/or
851                  *      UP_LD_CMD_PORT_HOST_INT_STATUS
852                  * Clear the interrupt status register
853                  */
854                 mwifiex_dbg(adapter, INTR,
855                             "int: sdio_ireg = %#x\n", sdio_ireg);
856                 spin_lock_irqsave(&adapter->int_lock, flags);
857                 adapter->int_status |= sdio_ireg;
858                 spin_unlock_irqrestore(&adapter->int_lock, flags);
859         }
860 }
861
862 /*
863  * SDIO interrupt handler.
864  *
865  * This function reads the interrupt status from firmware and handles
866  * the interrupt in current thread (ksdioirqd) right away.
867  */
868 static void
869 mwifiex_sdio_interrupt(struct sdio_func *func)
870 {
871         struct mwifiex_adapter *adapter;
872         struct sdio_mmc_card *card;
873
874         card = sdio_get_drvdata(func);
875         if (!card || !card->adapter) {
876                 pr_err("int: func=%p card=%p adapter=%p\n",
877                        func, card, card ? card->adapter : NULL);
878                 return;
879         }
880         adapter = card->adapter;
881
882         if (!adapter->pps_uapsd_mode && adapter->ps_state == PS_STATE_SLEEP)
883                 adapter->ps_state = PS_STATE_AWAKE;
884
885         mwifiex_interrupt_status(adapter);
886         mwifiex_main_process(adapter);
887 }
888
889 /*
890  * This function enables the host interrupt.
891  *
892  * The host interrupt enable mask is written to the card
893  * host interrupt mask register.
894  */
895 static int mwifiex_sdio_enable_host_int(struct mwifiex_adapter *adapter)
896 {
897         struct sdio_mmc_card *card = adapter->card;
898         struct sdio_func *func = card->func;
899         int ret;
900
901         sdio_claim_host(func);
902
903         /* Request the SDIO IRQ */
904         ret = sdio_claim_irq(func, mwifiex_sdio_interrupt);
905         if (ret) {
906                 mwifiex_dbg(adapter, ERROR,
907                             "claim irq failed: ret=%d\n", ret);
908                 goto out;
909         }
910
911         /* Simply write the mask to the register */
912         ret = mwifiex_write_reg_locked(func, card->reg->host_int_mask_reg,
913                                        card->reg->host_int_enable);
914         if (ret) {
915                 mwifiex_dbg(adapter, ERROR,
916                             "enable host interrupt failed\n");
917                 sdio_release_irq(func);
918         }
919
920 out:
921         sdio_release_host(func);
922         return ret;
923 }
924
925 /*
926  * This function sends a data buffer to the card.
927  */
928 static int mwifiex_sdio_card_to_host(struct mwifiex_adapter *adapter,
929                                      u32 *type, u8 *buffer,
930                                      u32 npayload, u32 ioport)
931 {
932         int ret;
933         u32 nb;
934
935         if (!buffer) {
936                 mwifiex_dbg(adapter, ERROR,
937                             "%s: buffer is NULL\n", __func__);
938                 return -1;
939         }
940
941         ret = mwifiex_read_data_sync(adapter, buffer, npayload, ioport, 1);
942
943         if (ret) {
944                 mwifiex_dbg(adapter, ERROR,
945                             "%s: read iomem failed: %d\n", __func__,
946                         ret);
947                 return -1;
948         }
949
950         nb = le16_to_cpu(*(__le16 *) (buffer));
951         if (nb > npayload) {
952                 mwifiex_dbg(adapter, ERROR,
953                             "%s: invalid packet, nb=%d npayload=%d\n",
954                             __func__, nb, npayload);
955                 return -1;
956         }
957
958         *type = le16_to_cpu(*(__le16 *) (buffer + 2));
959
960         return ret;
961 }
962
963 /*
964  * This function downloads the firmware to the card.
965  *
966  * Firmware is downloaded to the card in blocks. Every block download
967  * is tested for CRC errors, and retried a number of times before
968  * returning failure.
969  */
970 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
971                                     struct mwifiex_fw_image *fw)
972 {
973         struct sdio_mmc_card *card = adapter->card;
974         const struct mwifiex_sdio_card_reg *reg = card->reg;
975         int ret;
976         u8 *firmware = fw->fw_buf;
977         u32 firmware_len = fw->fw_len;
978         u32 offset = 0;
979         u8 base0, base1;
980         u8 *fwbuf;
981         u16 len = 0;
982         u32 txlen, tx_blocks = 0, tries;
983         u32 i = 0;
984
985         if (!firmware_len) {
986                 mwifiex_dbg(adapter, ERROR,
987                             "firmware image not found! Terminating download\n");
988                 return -1;
989         }
990
991         mwifiex_dbg(adapter, INFO,
992                     "info: downloading FW image (%d bytes)\n",
993                     firmware_len);
994
995         /* Assume that the allocated buffer is 8-byte aligned */
996         fwbuf = kzalloc(MWIFIEX_UPLD_SIZE, GFP_KERNEL);
997         if (!fwbuf)
998                 return -ENOMEM;
999
1000         sdio_claim_host(card->func);
1001
1002         /* Perform firmware data transfer */
1003         do {
1004                 /* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY
1005                    bits */
1006                 ret = mwifiex_sdio_poll_card_status(adapter, CARD_IO_READY |
1007                                                     DN_LD_CARD_RDY);
1008                 if (ret) {
1009                         mwifiex_dbg(adapter, ERROR,
1010                                     "FW download with helper:\t"
1011                                     "poll status timeout @ %d\n", offset);
1012                         goto done;
1013                 }
1014
1015                 /* More data? */
1016                 if (offset >= firmware_len)
1017                         break;
1018
1019                 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
1020                         ret = mwifiex_read_reg(adapter, reg->base_0_reg,
1021                                                &base0);
1022                         if (ret) {
1023                                 mwifiex_dbg(adapter, ERROR,
1024                                             "dev BASE0 register read failed:\t"
1025                                             "base0=%#04X(%d). Terminating dnld\n",
1026                                             base0, base0);
1027                                 goto done;
1028                         }
1029                         ret = mwifiex_read_reg(adapter, reg->base_1_reg,
1030                                                &base1);
1031                         if (ret) {
1032                                 mwifiex_dbg(adapter, ERROR,
1033                                             "dev BASE1 register read failed:\t"
1034                                             "base1=%#04X(%d). Terminating dnld\n",
1035                                             base1, base1);
1036                                 goto done;
1037                         }
1038                         len = (u16) (((base1 & 0xff) << 8) | (base0 & 0xff));
1039
1040                         if (len)
1041                                 break;
1042
1043                         usleep_range(10, 20);
1044                 }
1045
1046                 if (!len) {
1047                         break;
1048                 } else if (len > MWIFIEX_UPLD_SIZE) {
1049                         mwifiex_dbg(adapter, ERROR,
1050                                     "FW dnld failed @ %d, invalid length %d\n",
1051                                     offset, len);
1052                         ret = -1;
1053                         goto done;
1054                 }
1055
1056                 txlen = len;
1057
1058                 if (len & BIT(0)) {
1059                         i++;
1060                         if (i > MAX_WRITE_IOMEM_RETRY) {
1061                                 mwifiex_dbg(adapter, ERROR,
1062                                             "FW dnld failed @ %d, over max retry\n",
1063                                             offset);
1064                                 ret = -1;
1065                                 goto done;
1066                         }
1067                         mwifiex_dbg(adapter, ERROR,
1068                                     "CRC indicated by the helper:\t"
1069                                     "len = 0x%04X, txlen = %d\n", len, txlen);
1070                         len &= ~BIT(0);
1071                         /* Setting this to 0 to resend from same offset */
1072                         txlen = 0;
1073                 } else {
1074                         i = 0;
1075
1076                         /* Set blocksize to transfer - checking for last
1077                            block */
1078                         if (firmware_len - offset < txlen)
1079                                 txlen = firmware_len - offset;
1080
1081                         tx_blocks = (txlen + MWIFIEX_SDIO_BLOCK_SIZE - 1)
1082                                     / MWIFIEX_SDIO_BLOCK_SIZE;
1083
1084                         /* Copy payload to buffer */
1085                         memmove(fwbuf, &firmware[offset], txlen);
1086                 }
1087
1088                 ret = mwifiex_write_data_sync(adapter, fwbuf, tx_blocks *
1089                                               MWIFIEX_SDIO_BLOCK_SIZE,
1090                                               adapter->ioport);
1091                 if (ret) {
1092                         mwifiex_dbg(adapter, ERROR,
1093                                     "FW download, write iomem (%d) failed @ %d\n",
1094                                     i, offset);
1095                         if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
1096                                 mwifiex_dbg(adapter, ERROR,
1097                                             "write CFG reg failed\n");
1098
1099                         ret = -1;
1100                         goto done;
1101                 }
1102
1103                 offset += txlen;
1104         } while (true);
1105
1106         sdio_release_host(card->func);
1107
1108         mwifiex_dbg(adapter, MSG,
1109                     "info: FW download over, size %d bytes\n", offset);
1110
1111         ret = 0;
1112 done:
1113         kfree(fwbuf);
1114         return ret;
1115 }
1116
1117 /*
1118  * This function checks the firmware status in card.
1119  */
1120 static int mwifiex_check_fw_status(struct mwifiex_adapter *adapter,
1121                                    u32 poll_num)
1122 {
1123         int ret = 0;
1124         u16 firmware_stat;
1125         u32 tries;
1126
1127         for (tries = 0; tries < poll_num; tries++) {
1128                 ret = mwifiex_sdio_read_fw_status(adapter, &firmware_stat);
1129                 if (ret)
1130                         continue;
1131                 if (firmware_stat == FIRMWARE_READY_SDIO) {
1132                         ret = 0;
1133                         break;
1134                 } else {
1135                         msleep(100);
1136                         ret = -1;
1137                 }
1138         }
1139
1140         return ret;
1141 }
1142
1143 /* This function checks if WLAN is the winner.
1144  */
1145 static int mwifiex_check_winner_status(struct mwifiex_adapter *adapter)
1146 {
1147         int ret = 0;
1148         u8 winner = 0;
1149         struct sdio_mmc_card *card = adapter->card;
1150
1151         if (mwifiex_read_reg(adapter, card->reg->status_reg_0, &winner))
1152                 return -1;
1153
1154         if (winner)
1155                 adapter->winner = 0;
1156         else
1157                 adapter->winner = 1;
1158
1159         return ret;
1160 }
1161
1162 /*
1163  * This function decode sdio aggreation pkt.
1164  *
1165  * Based on the the data block size and pkt_len,
1166  * skb data will be decoded to few packets.
1167  */
1168 static void mwifiex_deaggr_sdio_pkt(struct mwifiex_adapter *adapter,
1169                                     struct sk_buff *skb)
1170 {
1171         u32 total_pkt_len, pkt_len;
1172         struct sk_buff *skb_deaggr;
1173         u32 pkt_type;
1174         u16 blk_size;
1175         u8 blk_num;
1176         u8 *data;
1177
1178         data = skb->data;
1179         total_pkt_len = skb->len;
1180
1181         while (total_pkt_len >= (SDIO_HEADER_OFFSET + INTF_HEADER_LEN)) {
1182                 if (total_pkt_len < adapter->sdio_rx_block_size)
1183                         break;
1184                 blk_num = *(data + BLOCK_NUMBER_OFFSET);
1185                 blk_size = adapter->sdio_rx_block_size * blk_num;
1186                 if (blk_size > total_pkt_len) {
1187                         mwifiex_dbg(adapter, ERROR,
1188                                     "%s: error in blk_size,\t"
1189                                     "blk_num=%d, blk_size=%d, total_pkt_len=%d\n",
1190                                     __func__, blk_num, blk_size, total_pkt_len);
1191                         break;
1192                 }
1193                 pkt_len = le16_to_cpu(*(__le16 *)(data + SDIO_HEADER_OFFSET));
1194                 pkt_type = le16_to_cpu(*(__le16 *)(data + SDIO_HEADER_OFFSET +
1195                                          2));
1196                 if ((pkt_len + SDIO_HEADER_OFFSET) > blk_size) {
1197                         mwifiex_dbg(adapter, ERROR,
1198                                     "%s: error in pkt_len,\t"
1199                                     "pkt_len=%d, blk_size=%d\n",
1200                                     __func__, pkt_len, blk_size);
1201                         break;
1202                 }
1203
1204                 skb_deaggr = mwifiex_alloc_dma_align_buf(pkt_len, GFP_KERNEL);
1205                 if (!skb_deaggr)
1206                         break;
1207                 skb_put(skb_deaggr, pkt_len);
1208                 memcpy(skb_deaggr->data, data + SDIO_HEADER_OFFSET, pkt_len);
1209                 skb_pull(skb_deaggr, INTF_HEADER_LEN);
1210
1211                 mwifiex_handle_rx_packet(adapter, skb_deaggr);
1212                 data += blk_size;
1213                 total_pkt_len -= blk_size;
1214         }
1215 }
1216
1217 /*
1218  * This function decodes a received packet.
1219  *
1220  * Based on the type, the packet is treated as either a data, or
1221  * a command response, or an event, and the correct handler
1222  * function is invoked.
1223  */
1224 static int mwifiex_decode_rx_packet(struct mwifiex_adapter *adapter,
1225                                     struct sk_buff *skb, u32 upld_typ)
1226 {
1227         u8 *cmd_buf;
1228         __le16 *curr_ptr = (__le16 *)skb->data;
1229         u16 pkt_len = le16_to_cpu(*curr_ptr);
1230         struct mwifiex_rxinfo *rx_info;
1231
1232         if (upld_typ != MWIFIEX_TYPE_AGGR_DATA) {
1233                 skb_trim(skb, pkt_len);
1234                 skb_pull(skb, INTF_HEADER_LEN);
1235         }
1236
1237         switch (upld_typ) {
1238         case MWIFIEX_TYPE_AGGR_DATA:
1239                 mwifiex_dbg(adapter, INFO,
1240                             "info: --- Rx: Aggr Data packet ---\n");
1241                 rx_info = MWIFIEX_SKB_RXCB(skb);
1242                 rx_info->buf_type = MWIFIEX_TYPE_AGGR_DATA;
1243                 if (adapter->rx_work_enabled) {
1244                         skb_queue_tail(&adapter->rx_data_q, skb);
1245                         atomic_inc(&adapter->rx_pending);
1246                         adapter->data_received = true;
1247                 } else {
1248                         mwifiex_deaggr_sdio_pkt(adapter, skb);
1249                         dev_kfree_skb_any(skb);
1250                 }
1251                 break;
1252
1253         case MWIFIEX_TYPE_DATA:
1254                 mwifiex_dbg(adapter, DATA,
1255                             "info: --- Rx: Data packet ---\n");
1256                 if (adapter->rx_work_enabled) {
1257                         skb_queue_tail(&adapter->rx_data_q, skb);
1258                         adapter->data_received = true;
1259                         atomic_inc(&adapter->rx_pending);
1260                 } else {
1261                         mwifiex_handle_rx_packet(adapter, skb);
1262                 }
1263                 break;
1264
1265         case MWIFIEX_TYPE_CMD:
1266                 mwifiex_dbg(adapter, CMD,
1267                             "info: --- Rx: Cmd Response ---\n");
1268                 /* take care of curr_cmd = NULL case */
1269                 if (!adapter->curr_cmd) {
1270                         cmd_buf = adapter->upld_buf;
1271
1272                         if (adapter->ps_state == PS_STATE_SLEEP_CFM)
1273                                 mwifiex_process_sleep_confirm_resp(adapter,
1274                                                                    skb->data,
1275                                                                    skb->len);
1276
1277                         memcpy(cmd_buf, skb->data,
1278                                min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER,
1279                                      skb->len));
1280
1281                         dev_kfree_skb_any(skb);
1282                 } else {
1283                         adapter->cmd_resp_received = true;
1284                         adapter->curr_cmd->resp_skb = skb;
1285                 }
1286                 break;
1287
1288         case MWIFIEX_TYPE_EVENT:
1289                 mwifiex_dbg(adapter, EVENT,
1290                             "info: --- Rx: Event ---\n");
1291                 adapter->event_cause = le32_to_cpu(*(__le32 *) skb->data);
1292
1293                 if ((skb->len > 0) && (skb->len  < MAX_EVENT_SIZE))
1294                         memcpy(adapter->event_body,
1295                                skb->data + MWIFIEX_EVENT_HEADER_LEN,
1296                                skb->len);
1297
1298                 /* event cause has been saved to adapter->event_cause */
1299                 adapter->event_received = true;
1300                 adapter->event_skb = skb;
1301
1302                 break;
1303
1304         default:
1305                 mwifiex_dbg(adapter, ERROR,
1306                             "unknown upload type %#x\n", upld_typ);
1307                 dev_kfree_skb_any(skb);
1308                 break;
1309         }
1310
1311         return 0;
1312 }
1313
1314 /*
1315  * This function transfers received packets from card to driver, performing
1316  * aggregation if required.
1317  *
1318  * For data received on control port, or if aggregation is disabled, the
1319  * received buffers are uploaded as separate packets. However, if aggregation
1320  * is enabled and required, the buffers are copied onto an aggregation buffer,
1321  * provided there is space left, processed and finally uploaded.
1322  */
1323 static int mwifiex_sdio_card_to_host_mp_aggr(struct mwifiex_adapter *adapter,
1324                                              u16 rx_len, u8 port)
1325 {
1326         struct sdio_mmc_card *card = adapter->card;
1327         s32 f_do_rx_aggr = 0;
1328         s32 f_do_rx_cur = 0;
1329         s32 f_aggr_cur = 0;
1330         s32 f_post_aggr_cur = 0;
1331         struct sk_buff *skb_deaggr;
1332         struct sk_buff *skb = NULL;
1333         u32 pkt_len, pkt_type, mport, pind;
1334         u8 *curr_ptr;
1335
1336         if ((card->has_control_mask) && (port == CTRL_PORT)) {
1337                 /* Read the command Resp without aggr */
1338                 mwifiex_dbg(adapter, CMD,
1339                             "info: %s: no aggregation for cmd\t"
1340                             "response\n", __func__);
1341
1342                 f_do_rx_cur = 1;
1343                 goto rx_curr_single;
1344         }
1345
1346         if (!card->mpa_rx.enabled) {
1347                 mwifiex_dbg(adapter, WARN,
1348                             "info: %s: rx aggregation disabled\n",
1349                             __func__);
1350
1351                 f_do_rx_cur = 1;
1352                 goto rx_curr_single;
1353         }
1354
1355         if ((!card->has_control_mask && (card->mp_rd_bitmap &
1356                                          card->reg->data_port_mask)) ||
1357             (card->has_control_mask && (card->mp_rd_bitmap &
1358                                         (~((u32) CTRL_PORT_MASK))))) {
1359                 /* Some more data RX pending */
1360                 mwifiex_dbg(adapter, INFO,
1361                             "info: %s: not last packet\n", __func__);
1362
1363                 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1364                         if (MP_RX_AGGR_BUF_HAS_ROOM(card, rx_len)) {
1365                                 f_aggr_cur = 1;
1366                         } else {
1367                                 /* No room in Aggr buf, do rx aggr now */
1368                                 f_do_rx_aggr = 1;
1369                                 f_post_aggr_cur = 1;
1370                         }
1371                 } else {
1372                         /* Rx aggr not in progress */
1373                         f_aggr_cur = 1;
1374                 }
1375
1376         } else {
1377                 /* No more data RX pending */
1378                 mwifiex_dbg(adapter, INFO,
1379                             "info: %s: last packet\n", __func__);
1380
1381                 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1382                         f_do_rx_aggr = 1;
1383                         if (MP_RX_AGGR_BUF_HAS_ROOM(card, rx_len))
1384                                 f_aggr_cur = 1;
1385                         else
1386                                 /* No room in Aggr buf, do rx aggr now */
1387                                 f_do_rx_cur = 1;
1388                 } else {
1389                         f_do_rx_cur = 1;
1390                 }
1391         }
1392
1393         if (f_aggr_cur) {
1394                 mwifiex_dbg(adapter, INFO,
1395                             "info: current packet aggregation\n");
1396                 /* Curr pkt can be aggregated */
1397                 mp_rx_aggr_setup(card, rx_len, port);
1398
1399                 if (MP_RX_AGGR_PKT_LIMIT_REACHED(card) ||
1400                     mp_rx_aggr_port_limit_reached(card)) {
1401                         mwifiex_dbg(adapter, INFO,
1402                                     "info: %s: aggregated packet\t"
1403                                     "limit reached\n", __func__);
1404                         /* No more pkts allowed in Aggr buf, rx it */
1405                         f_do_rx_aggr = 1;
1406                 }
1407         }
1408
1409         if (f_do_rx_aggr) {
1410                 /* do aggr RX now */
1411                 mwifiex_dbg(adapter, DATA,
1412                             "info: do_rx_aggr: num of packets: %d\n",
1413                             card->mpa_rx.pkt_cnt);
1414
1415                 if (card->supports_sdio_new_mode) {
1416                         int i;
1417                         u32 port_count;
1418
1419                         for (i = 0, port_count = 0; i < card->max_ports; i++)
1420                                 if (card->mpa_rx.ports & BIT(i))
1421                                         port_count++;
1422
1423                         /* Reading data from "start_port + 0" to "start_port +
1424                          * port_count -1", so decrease the count by 1
1425                          */
1426                         port_count--;
1427                         mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1428                                  (port_count << 8)) + card->mpa_rx.start_port;
1429                 } else {
1430                         mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1431                                  (card->mpa_rx.ports << 4)) +
1432                                  card->mpa_rx.start_port;
1433                 }
1434
1435                 if (card->mpa_rx.pkt_cnt == 1)
1436                         mport = adapter->ioport + port;
1437
1438                 if (mwifiex_read_data_sync(adapter, card->mpa_rx.buf,
1439                                            card->mpa_rx.buf_len, mport, 1))
1440                         goto error;
1441
1442                 curr_ptr = card->mpa_rx.buf;
1443
1444                 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1445                         u32 *len_arr = card->mpa_rx.len_arr;
1446
1447                         /* get curr PKT len & type */
1448                         pkt_len = le16_to_cpu(*(__le16 *) &curr_ptr[0]);
1449                         pkt_type = le16_to_cpu(*(__le16 *) &curr_ptr[2]);
1450
1451                         /* copy pkt to deaggr buf */
1452                         skb_deaggr = mwifiex_alloc_dma_align_buf(len_arr[pind],
1453                                                                  GFP_KERNEL);
1454                         if (!skb_deaggr) {
1455                                 mwifiex_dbg(adapter, ERROR, "skb allocation failure\t"
1456                                             "drop pkt len=%d type=%d\n",
1457                                             pkt_len, pkt_type);
1458                                 curr_ptr += len_arr[pind];
1459                                 continue;
1460                         }
1461
1462                         skb_put(skb_deaggr, len_arr[pind]);
1463
1464                         if ((pkt_type == MWIFIEX_TYPE_DATA ||
1465                              (pkt_type == MWIFIEX_TYPE_AGGR_DATA &&
1466                               adapter->sdio_rx_aggr_enable)) &&
1467                             (pkt_len <= len_arr[pind])) {
1468
1469                                 memcpy(skb_deaggr->data, curr_ptr, pkt_len);
1470
1471                                 skb_trim(skb_deaggr, pkt_len);
1472
1473                                 /* Process de-aggr packet */
1474                                 mwifiex_decode_rx_packet(adapter, skb_deaggr,
1475                                                          pkt_type);
1476                         } else {
1477                                 mwifiex_dbg(adapter, ERROR,
1478                                             "drop wrong aggr pkt:\t"
1479                                             "sdio_single_port_rx_aggr=%d\t"
1480                                             "type=%d len=%d max_len=%d\n",
1481                                             adapter->sdio_rx_aggr_enable,
1482                                             pkt_type, pkt_len, len_arr[pind]);
1483                                 dev_kfree_skb_any(skb_deaggr);
1484                         }
1485                         curr_ptr += len_arr[pind];
1486                 }
1487                 MP_RX_AGGR_BUF_RESET(card);
1488         }
1489
1490 rx_curr_single:
1491         if (f_do_rx_cur) {
1492                 mwifiex_dbg(adapter, INFO, "info: RX: port: %d, rx_len: %d\n",
1493                             port, rx_len);
1494
1495                 skb = mwifiex_alloc_dma_align_buf(rx_len, GFP_KERNEL | GFP_DMA);
1496                 if (!skb) {
1497                         mwifiex_dbg(adapter, ERROR,
1498                                     "single skb allocated fail,\t"
1499                                     "drop pkt port=%d len=%d\n", port, rx_len);
1500                         if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1501                                                       card->mpa_rx.buf, rx_len,
1502                                                       adapter->ioport + port))
1503                                 goto error;
1504                         return 0;
1505                 }
1506
1507                 skb_put(skb, rx_len);
1508
1509                 if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1510                                               skb->data, skb->len,
1511                                               adapter->ioport + port))
1512                         goto error;
1513                 if (!adapter->sdio_rx_aggr_enable &&
1514                     pkt_type == MWIFIEX_TYPE_AGGR_DATA) {
1515                         mwifiex_dbg(adapter, ERROR, "drop wrong pkt type %d\t"
1516                                     "current SDIO RX Aggr not enabled\n",
1517                                     pkt_type);
1518                         dev_kfree_skb_any(skb);
1519                         return 0;
1520                 }
1521
1522                 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1523         }
1524         if (f_post_aggr_cur) {
1525                 mwifiex_dbg(adapter, INFO,
1526                             "info: current packet aggregation\n");
1527                 /* Curr pkt can be aggregated */
1528                 mp_rx_aggr_setup(card, rx_len, port);
1529         }
1530
1531         return 0;
1532 error:
1533         if (MP_RX_AGGR_IN_PROGRESS(card))
1534                 MP_RX_AGGR_BUF_RESET(card);
1535
1536         if (f_do_rx_cur && skb)
1537                 /* Single transfer pending. Free curr buff also */
1538                 dev_kfree_skb_any(skb);
1539
1540         return -1;
1541 }
1542
1543 /*
1544  * This function checks the current interrupt status.
1545  *
1546  * The following interrupts are checked and handled by this function -
1547  *      - Data sent
1548  *      - Command sent
1549  *      - Packets received
1550  *
1551  * Since the firmware does not generate download ready interrupt if the
1552  * port updated is command port only, command sent interrupt checking
1553  * should be done manually, and for every SDIO interrupt.
1554  *
1555  * In case of Rx packets received, the packets are uploaded from card to
1556  * host and processed accordingly.
1557  */
1558 static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
1559 {
1560         struct sdio_mmc_card *card = adapter->card;
1561         const struct mwifiex_sdio_card_reg *reg = card->reg;
1562         int ret = 0;
1563         u8 sdio_ireg;
1564         struct sk_buff *skb;
1565         u8 port = CTRL_PORT;
1566         u32 len_reg_l, len_reg_u;
1567         u32 rx_blocks;
1568         u16 rx_len;
1569         unsigned long flags;
1570         u32 bitmap;
1571         u8 cr;
1572
1573         spin_lock_irqsave(&adapter->int_lock, flags);
1574         sdio_ireg = adapter->int_status;
1575         adapter->int_status = 0;
1576         spin_unlock_irqrestore(&adapter->int_lock, flags);
1577
1578         if (!sdio_ireg)
1579                 return ret;
1580
1581         /* Following interrupt is only for SDIO new mode */
1582         if (sdio_ireg & DN_LD_CMD_PORT_HOST_INT_STATUS && adapter->cmd_sent)
1583                 adapter->cmd_sent = false;
1584
1585         /* Following interrupt is only for SDIO new mode */
1586         if (sdio_ireg & UP_LD_CMD_PORT_HOST_INT_STATUS) {
1587                 u32 pkt_type;
1588
1589                 /* read the len of control packet */
1590                 rx_len = card->mp_regs[reg->cmd_rd_len_1] << 8;
1591                 rx_len |= (u16)card->mp_regs[reg->cmd_rd_len_0];
1592                 rx_blocks = DIV_ROUND_UP(rx_len, MWIFIEX_SDIO_BLOCK_SIZE);
1593                 if (rx_len <= INTF_HEADER_LEN ||
1594                     (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1595                      MWIFIEX_RX_DATA_BUF_SIZE)
1596                         return -1;
1597                 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1598                 mwifiex_dbg(adapter, INFO, "info: rx_len = %d\n", rx_len);
1599
1600                 skb = mwifiex_alloc_dma_align_buf(rx_len, GFP_KERNEL | GFP_DMA);
1601                 if (!skb)
1602                         return -1;
1603
1604                 skb_put(skb, rx_len);
1605
1606                 if (mwifiex_sdio_card_to_host(adapter, &pkt_type, skb->data,
1607                                               skb->len, adapter->ioport |
1608                                                         CMD_PORT_SLCT)) {
1609                         mwifiex_dbg(adapter, ERROR,
1610                                     "%s: failed to card_to_host", __func__);
1611                         dev_kfree_skb_any(skb);
1612                         goto term_cmd;
1613                 }
1614
1615                 if ((pkt_type != MWIFIEX_TYPE_CMD) &&
1616                     (pkt_type != MWIFIEX_TYPE_EVENT))
1617                         mwifiex_dbg(adapter, ERROR,
1618                                     "%s:Received wrong packet on cmd port",
1619                                     __func__);
1620
1621                 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1622         }
1623
1624         if (sdio_ireg & DN_LD_HOST_INT_STATUS) {
1625                 bitmap = (u32) card->mp_regs[reg->wr_bitmap_l];
1626                 bitmap |= ((u32) card->mp_regs[reg->wr_bitmap_u]) << 8;
1627                 if (card->supports_sdio_new_mode) {
1628                         bitmap |=
1629                                 ((u32) card->mp_regs[reg->wr_bitmap_1l]) << 16;
1630                         bitmap |=
1631                                 ((u32) card->mp_regs[reg->wr_bitmap_1u]) << 24;
1632                 }
1633                 card->mp_wr_bitmap = bitmap;
1634
1635                 mwifiex_dbg(adapter, INTR,
1636                             "int: DNLD: wr_bitmap=0x%x\n",
1637                             card->mp_wr_bitmap);
1638                 if (adapter->data_sent &&
1639                     (card->mp_wr_bitmap & card->mp_data_port_mask)) {
1640                         mwifiex_dbg(adapter, INTR,
1641                                     "info:  <--- Tx DONE Interrupt --->\n");
1642                         adapter->data_sent = false;
1643                 }
1644         }
1645
1646         /* As firmware will not generate download ready interrupt if the port
1647            updated is command port only, cmd_sent should be done for any SDIO
1648            interrupt. */
1649         if (card->has_control_mask && adapter->cmd_sent) {
1650                 /* Check if firmware has attach buffer at command port and
1651                    update just that in wr_bit_map. */
1652                 card->mp_wr_bitmap |=
1653                         (u32) card->mp_regs[reg->wr_bitmap_l] & CTRL_PORT_MASK;
1654                 if (card->mp_wr_bitmap & CTRL_PORT_MASK)
1655                         adapter->cmd_sent = false;
1656         }
1657
1658         mwifiex_dbg(adapter, INTR, "info: cmd_sent=%d data_sent=%d\n",
1659                     adapter->cmd_sent, adapter->data_sent);
1660         if (sdio_ireg & UP_LD_HOST_INT_STATUS) {
1661                 bitmap = (u32) card->mp_regs[reg->rd_bitmap_l];
1662                 bitmap |= ((u32) card->mp_regs[reg->rd_bitmap_u]) << 8;
1663                 if (card->supports_sdio_new_mode) {
1664                         bitmap |=
1665                                 ((u32) card->mp_regs[reg->rd_bitmap_1l]) << 16;
1666                         bitmap |=
1667                                 ((u32) card->mp_regs[reg->rd_bitmap_1u]) << 24;
1668                 }
1669                 card->mp_rd_bitmap = bitmap;
1670                 mwifiex_dbg(adapter, INTR,
1671                             "int: UPLD: rd_bitmap=0x%x\n",
1672                             card->mp_rd_bitmap);
1673
1674                 while (true) {
1675                         ret = mwifiex_get_rd_port(adapter, &port);
1676                         if (ret) {
1677                                 mwifiex_dbg(adapter, INFO,
1678                                             "info: no more rd_port available\n");
1679                                 break;
1680                         }
1681                         len_reg_l = reg->rd_len_p0_l + (port << 1);
1682                         len_reg_u = reg->rd_len_p0_u + (port << 1);
1683                         rx_len = ((u16) card->mp_regs[len_reg_u]) << 8;
1684                         rx_len |= (u16) card->mp_regs[len_reg_l];
1685                         mwifiex_dbg(adapter, INFO,
1686                                     "info: RX: port=%d rx_len=%u\n",
1687                                     port, rx_len);
1688                         rx_blocks =
1689                                 (rx_len + MWIFIEX_SDIO_BLOCK_SIZE -
1690                                  1) / MWIFIEX_SDIO_BLOCK_SIZE;
1691                         if (rx_len <= INTF_HEADER_LEN ||
1692                             (card->mpa_rx.enabled &&
1693                              ((rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1694                               card->mpa_rx.buf_size))) {
1695                                 mwifiex_dbg(adapter, ERROR,
1696                                             "invalid rx_len=%d\n",
1697                                             rx_len);
1698                                 return -1;
1699                         }
1700
1701                         rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1702                         mwifiex_dbg(adapter, INFO, "info: rx_len = %d\n",
1703                                     rx_len);
1704
1705                         if (mwifiex_sdio_card_to_host_mp_aggr(adapter, rx_len,
1706                                                               port)) {
1707                                 mwifiex_dbg(adapter, ERROR,
1708                                             "card_to_host_mpa failed: int status=%#x\n",
1709                                             sdio_ireg);
1710                                 goto term_cmd;
1711                         }
1712                 }
1713         }
1714
1715         return 0;
1716
1717 term_cmd:
1718         /* terminate cmd */
1719         if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
1720                 mwifiex_dbg(adapter, ERROR, "read CFG reg failed\n");
1721         else
1722                 mwifiex_dbg(adapter, INFO,
1723                             "info: CFG reg val = %d\n", cr);
1724
1725         if (mwifiex_write_reg(adapter, CONFIGURATION_REG, (cr | 0x04)))
1726                 mwifiex_dbg(adapter, ERROR,
1727                             "write CFG reg failed\n");
1728         else
1729                 mwifiex_dbg(adapter, INFO, "info: write success\n");
1730
1731         if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
1732                 mwifiex_dbg(adapter, ERROR,
1733                             "read CFG reg failed\n");
1734         else
1735                 mwifiex_dbg(adapter, INFO,
1736                             "info: CFG reg val =%x\n", cr);
1737
1738         return -1;
1739 }
1740
1741 /*
1742  * This function aggregates transmission buffers in driver and downloads
1743  * the aggregated packet to card.
1744  *
1745  * The individual packets are aggregated by copying into an aggregation
1746  * buffer and then downloaded to the card. Previous unsent packets in the
1747  * aggregation buffer are pre-copied first before new packets are added.
1748  * Aggregation is done till there is space left in the aggregation buffer,
1749  * or till new packets are available.
1750  *
1751  * The function will only download the packet to the card when aggregation
1752  * stops, otherwise it will just aggregate the packet in aggregation buffer
1753  * and return.
1754  */
1755 static int mwifiex_host_to_card_mp_aggr(struct mwifiex_adapter *adapter,
1756                                         u8 *payload, u32 pkt_len, u32 port,
1757                                         u32 next_pkt_len)
1758 {
1759         struct sdio_mmc_card *card = adapter->card;
1760         int ret = 0;
1761         s32 f_send_aggr_buf = 0;
1762         s32 f_send_cur_buf = 0;
1763         s32 f_precopy_cur_buf = 0;
1764         s32 f_postcopy_cur_buf = 0;
1765         u32 mport;
1766         int index;
1767
1768         if (!card->mpa_tx.enabled ||
1769             (card->has_control_mask && (port == CTRL_PORT)) ||
1770             (card->supports_sdio_new_mode && (port == CMD_PORT_SLCT))) {
1771                 mwifiex_dbg(adapter, WARN,
1772                             "info: %s: tx aggregation disabled\n",
1773                             __func__);
1774
1775                 f_send_cur_buf = 1;
1776                 goto tx_curr_single;
1777         }
1778
1779         if (next_pkt_len) {
1780                 /* More pkt in TX queue */
1781                 mwifiex_dbg(adapter, INFO,
1782                             "info: %s: more packets in queue.\n",
1783                             __func__);
1784
1785                 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1786                         if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len)) {
1787                                 f_precopy_cur_buf = 1;
1788
1789                                 if (!(card->mp_wr_bitmap &
1790                                       (1 << card->curr_wr_port)) ||
1791                                     !MP_TX_AGGR_BUF_HAS_ROOM(
1792                                             card, pkt_len + next_pkt_len))
1793                                         f_send_aggr_buf = 1;
1794                         } else {
1795                                 /* No room in Aggr buf, send it */
1796                                 f_send_aggr_buf = 1;
1797
1798                                 if (!(card->mp_wr_bitmap &
1799                                       (1 << card->curr_wr_port)))
1800                                         f_send_cur_buf = 1;
1801                                 else
1802                                         f_postcopy_cur_buf = 1;
1803                         }
1804                 } else {
1805                         if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len) &&
1806                             (card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1807                                 f_precopy_cur_buf = 1;
1808                         else
1809                                 f_send_cur_buf = 1;
1810                 }
1811         } else {
1812                 /* Last pkt in TX queue */
1813                 mwifiex_dbg(adapter, INFO,
1814                             "info: %s: Last packet in Tx Queue.\n",
1815                             __func__);
1816
1817                 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1818                         /* some packs in Aggr buf already */
1819                         f_send_aggr_buf = 1;
1820
1821                         if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len))
1822                                 f_precopy_cur_buf = 1;
1823                         else
1824                                 /* No room in Aggr buf, send it */
1825                                 f_send_cur_buf = 1;
1826                 } else {
1827                         f_send_cur_buf = 1;
1828                 }
1829         }
1830
1831         if (f_precopy_cur_buf) {
1832                 mwifiex_dbg(adapter, DATA,
1833                             "data: %s: precopy current buffer\n",
1834                             __func__);
1835                 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1836
1837                 if (MP_TX_AGGR_PKT_LIMIT_REACHED(card) ||
1838                     mp_tx_aggr_port_limit_reached(card))
1839                         /* No more pkts allowed in Aggr buf, send it */
1840                         f_send_aggr_buf = 1;
1841         }
1842
1843         if (f_send_aggr_buf) {
1844                 mwifiex_dbg(adapter, DATA,
1845                             "data: %s: send aggr buffer: %d %d\n",
1846                             __func__, card->mpa_tx.start_port,
1847                             card->mpa_tx.ports);
1848                 if (card->supports_sdio_new_mode) {
1849                         u32 port_count;
1850                         int i;
1851
1852                         for (i = 0, port_count = 0; i < card->max_ports; i++)
1853                                 if (card->mpa_tx.ports & BIT(i))
1854                                         port_count++;
1855
1856                         /* Writing data from "start_port + 0" to "start_port +
1857                          * port_count -1", so decrease the count by 1
1858                          */
1859                         port_count--;
1860                         mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1861                                  (port_count << 8)) + card->mpa_tx.start_port;
1862                 } else {
1863                         mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1864                                  (card->mpa_tx.ports << 4)) +
1865                                  card->mpa_tx.start_port;
1866                 }
1867
1868                 if (card->mpa_tx.pkt_cnt == 1)
1869                         mport = adapter->ioport + port;
1870
1871                 ret = mwifiex_write_data_to_card(adapter, card->mpa_tx.buf,
1872                                                  card->mpa_tx.buf_len, mport);
1873
1874                 /* Save the last multi port tx aggreagation info to debug log */
1875                 index = adapter->dbg.last_sdio_mp_index;
1876                 index = (index + 1) % MWIFIEX_DBG_SDIO_MP_NUM;
1877                 adapter->dbg.last_sdio_mp_index = index;
1878                 adapter->dbg.last_mp_wr_ports[index] = mport;
1879                 adapter->dbg.last_mp_wr_bitmap[index] = card->mp_wr_bitmap;
1880                 adapter->dbg.last_mp_wr_len[index] = card->mpa_tx.buf_len;
1881                 adapter->dbg.last_mp_curr_wr_port[index] = card->curr_wr_port;
1882
1883                 MP_TX_AGGR_BUF_RESET(card);
1884         }
1885
1886 tx_curr_single:
1887         if (f_send_cur_buf) {
1888                 mwifiex_dbg(adapter, DATA,
1889                             "data: %s: send current buffer %d\n",
1890                             __func__, port);
1891                 ret = mwifiex_write_data_to_card(adapter, payload, pkt_len,
1892                                                  adapter->ioport + port);
1893         }
1894
1895         if (f_postcopy_cur_buf) {
1896                 mwifiex_dbg(adapter, DATA,
1897                             "data: %s: postcopy current buffer\n",
1898                             __func__);
1899                 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1900         }
1901
1902         return ret;
1903 }
1904
1905 /*
1906  * This function downloads data from driver to card.
1907  *
1908  * Both commands and data packets are transferred to the card by this
1909  * function.
1910  *
1911  * This function adds the SDIO specific header to the front of the buffer
1912  * before transferring. The header contains the length of the packet and
1913  * the type. The firmware handles the packets based upon this set type.
1914  */
1915 static int mwifiex_sdio_host_to_card(struct mwifiex_adapter *adapter,
1916                                      u8 type, struct sk_buff *skb,
1917                                      struct mwifiex_tx_param *tx_param)
1918 {
1919         struct sdio_mmc_card *card = adapter->card;
1920         int ret;
1921         u32 buf_block_len;
1922         u32 blk_size;
1923         u32 port = CTRL_PORT;
1924         u8 *payload = (u8 *)skb->data;
1925         u32 pkt_len = skb->len;
1926
1927         /* Allocate buffer and copy payload */
1928         blk_size = MWIFIEX_SDIO_BLOCK_SIZE;
1929         buf_block_len = (pkt_len + blk_size - 1) / blk_size;
1930         *(__le16 *)&payload[0] = cpu_to_le16((u16)pkt_len);
1931         *(__le16 *)&payload[2] = cpu_to_le16(type);
1932
1933         /*
1934          * This is SDIO specific header
1935          *  u16 length,
1936          *  u16 type (MWIFIEX_TYPE_DATA = 0, MWIFIEX_TYPE_CMD = 1,
1937          *  MWIFIEX_TYPE_EVENT = 3)
1938          */
1939         if (type == MWIFIEX_TYPE_DATA) {
1940                 ret = mwifiex_get_wr_port_data(adapter, &port);
1941                 if (ret) {
1942                         mwifiex_dbg(adapter, ERROR,
1943                                     "%s: no wr_port available\n",
1944                                     __func__);
1945                         return ret;
1946                 }
1947         } else {
1948                 adapter->cmd_sent = true;
1949                 /* Type must be MWIFIEX_TYPE_CMD */
1950
1951                 if (pkt_len <= INTF_HEADER_LEN ||
1952                     pkt_len > MWIFIEX_UPLD_SIZE)
1953                         mwifiex_dbg(adapter, ERROR,
1954                                     "%s: payload=%p, nb=%d\n",
1955                                     __func__, payload, pkt_len);
1956
1957                 if (card->supports_sdio_new_mode)
1958                         port = CMD_PORT_SLCT;
1959         }
1960
1961         /* Transfer data to card */
1962         pkt_len = buf_block_len * blk_size;
1963
1964         if (tx_param)
1965                 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1966                                                    port, tx_param->next_pkt_len
1967                                                    );
1968         else
1969                 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1970                                                    port, 0);
1971
1972         if (ret) {
1973                 if (type == MWIFIEX_TYPE_CMD)
1974                         adapter->cmd_sent = false;
1975                 if (type == MWIFIEX_TYPE_DATA) {
1976                         adapter->data_sent = false;
1977                         /* restore curr_wr_port in error cases */
1978                         card->curr_wr_port = port;
1979                         card->mp_wr_bitmap |= (u32)(1 << card->curr_wr_port);
1980                 }
1981         } else {
1982                 if (type == MWIFIEX_TYPE_DATA) {
1983                         if (!(card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1984                                 adapter->data_sent = true;
1985                         else
1986                                 adapter->data_sent = false;
1987                 }
1988         }
1989
1990         return ret;
1991 }
1992
1993 /*
1994  * This function allocates the MPA Tx and Rx buffers.
1995  */
1996 static int mwifiex_alloc_sdio_mpa_buffers(struct mwifiex_adapter *adapter,
1997                                    u32 mpa_tx_buf_size, u32 mpa_rx_buf_size)
1998 {
1999         struct sdio_mmc_card *card = adapter->card;
2000         u32 rx_buf_size;
2001         int ret = 0;
2002
2003         card->mpa_tx.buf = kzalloc(mpa_tx_buf_size, GFP_KERNEL);
2004         if (!card->mpa_tx.buf) {
2005                 ret = -1;
2006                 goto error;
2007         }
2008
2009         card->mpa_tx.buf_size = mpa_tx_buf_size;
2010
2011         rx_buf_size = max_t(u32, mpa_rx_buf_size,
2012                             (u32)SDIO_MAX_AGGR_BUF_SIZE);
2013         card->mpa_rx.buf = kzalloc(rx_buf_size, GFP_KERNEL);
2014         if (!card->mpa_rx.buf) {
2015                 ret = -1;
2016                 goto error;
2017         }
2018
2019         card->mpa_rx.buf_size = rx_buf_size;
2020
2021 error:
2022         if (ret) {
2023                 kfree(card->mpa_tx.buf);
2024                 kfree(card->mpa_rx.buf);
2025                 card->mpa_tx.buf_size = 0;
2026                 card->mpa_rx.buf_size = 0;
2027         }
2028
2029         return ret;
2030 }
2031
2032 /*
2033  * This function unregisters the SDIO device.
2034  *
2035  * The SDIO IRQ is released, the function is disabled and driver
2036  * data is set to null.
2037  */
2038 static void
2039 mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
2040 {
2041         struct sdio_mmc_card *card = adapter->card;
2042
2043         if (adapter->card) {
2044                 sdio_claim_host(card->func);
2045                 sdio_disable_func(card->func);
2046                 sdio_release_host(card->func);
2047         }
2048 }
2049
2050 /*
2051  * This function registers the SDIO device.
2052  *
2053  * SDIO IRQ is claimed, block size is set and driver data is initialized.
2054  */
2055 static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
2056 {
2057         int ret;
2058         struct sdio_mmc_card *card = adapter->card;
2059         struct sdio_func *func = card->func;
2060
2061         /* save adapter pointer in card */
2062         card->adapter = adapter;
2063         adapter->tx_buf_size = card->tx_buf_size;
2064
2065         sdio_claim_host(func);
2066
2067         /* Set block size */
2068         ret = sdio_set_block_size(card->func, MWIFIEX_SDIO_BLOCK_SIZE);
2069         sdio_release_host(func);
2070         if (ret) {
2071                 mwifiex_dbg(adapter, ERROR,
2072                             "cannot set SDIO block size\n");
2073                 return ret;
2074         }
2075
2076
2077         adapter->dev = &func->dev;
2078
2079         strcpy(adapter->fw_name, card->firmware);
2080         if (card->fw_dump_enh) {
2081                 adapter->mem_type_mapping_tbl = generic_mem_type_map;
2082                 adapter->num_mem_types = 1;
2083         } else {
2084                 adapter->mem_type_mapping_tbl = mem_type_mapping_tbl;
2085                 adapter->num_mem_types = ARRAY_SIZE(mem_type_mapping_tbl);
2086         }
2087
2088         return 0;
2089 }
2090
2091 /*
2092  * This function initializes the SDIO driver.
2093  *
2094  * The following initializations steps are followed -
2095  *      - Read the Host interrupt status register to acknowledge
2096  *        the first interrupt got from bootloader
2097  *      - Disable host interrupt mask register
2098  *      - Get SDIO port
2099  *      - Initialize SDIO variables in card
2100  *      - Allocate MP registers
2101  *      - Allocate MPA Tx and Rx buffers
2102  */
2103 static int mwifiex_init_sdio(struct mwifiex_adapter *adapter)
2104 {
2105         struct sdio_mmc_card *card = adapter->card;
2106         const struct mwifiex_sdio_card_reg *reg = card->reg;
2107         int ret;
2108         u8 sdio_ireg;
2109
2110         sdio_set_drvdata(card->func, card);
2111
2112         /*
2113          * Read the host_int_status_reg for ACK the first interrupt got
2114          * from the bootloader. If we don't do this we get a interrupt
2115          * as soon as we register the irq.
2116          */
2117         mwifiex_read_reg(adapter, card->reg->host_int_status_reg, &sdio_ireg);
2118
2119         /* Get SDIO ioport */
2120         mwifiex_init_sdio_ioport(adapter);
2121
2122         /* Initialize SDIO variables in card */
2123         card->mp_rd_bitmap = 0;
2124         card->mp_wr_bitmap = 0;
2125         card->curr_rd_port = reg->start_rd_port;
2126         card->curr_wr_port = reg->start_wr_port;
2127
2128         card->mp_data_port_mask = reg->data_port_mask;
2129
2130         card->mpa_tx.buf_len = 0;
2131         card->mpa_tx.pkt_cnt = 0;
2132         card->mpa_tx.start_port = 0;
2133
2134         card->mpa_tx.enabled = 1;
2135         card->mpa_tx.pkt_aggr_limit = card->mp_agg_pkt_limit;
2136
2137         card->mpa_rx.buf_len = 0;
2138         card->mpa_rx.pkt_cnt = 0;
2139         card->mpa_rx.start_port = 0;
2140
2141         card->mpa_rx.enabled = 1;
2142         card->mpa_rx.pkt_aggr_limit = card->mp_agg_pkt_limit;
2143
2144         /* Allocate buffers for SDIO MP-A */
2145         card->mp_regs = kzalloc(reg->max_mp_regs, GFP_KERNEL);
2146         if (!card->mp_regs)
2147                 return -ENOMEM;
2148
2149         /* Allocate skb pointer buffers */
2150         card->mpa_rx.skb_arr = kzalloc((sizeof(void *)) *
2151                                        card->mp_agg_pkt_limit, GFP_KERNEL);
2152         if (!card->mpa_rx.skb_arr) {
2153                 kfree(card->mp_regs);
2154                 return -ENOMEM;
2155         }
2156
2157         card->mpa_rx.len_arr = kzalloc(sizeof(*card->mpa_rx.len_arr) *
2158                                        card->mp_agg_pkt_limit, GFP_KERNEL);
2159         if (!card->mpa_rx.len_arr) {
2160                 kfree(card->mp_regs);
2161                 kfree(card->mpa_rx.skb_arr);
2162                 return -ENOMEM;
2163         }
2164
2165         ret = mwifiex_alloc_sdio_mpa_buffers(adapter,
2166                                              card->mp_tx_agg_buf_size,
2167                                              card->mp_rx_agg_buf_size);
2168
2169         /* Allocate 32k MPA Tx/Rx buffers if 64k memory allocation fails */
2170         if (ret && (card->mp_tx_agg_buf_size == MWIFIEX_MP_AGGR_BUF_SIZE_MAX ||
2171                     card->mp_rx_agg_buf_size == MWIFIEX_MP_AGGR_BUF_SIZE_MAX)) {
2172                 /* Disable rx single port aggregation */
2173                 adapter->host_disable_sdio_rx_aggr = true;
2174
2175                 ret = mwifiex_alloc_sdio_mpa_buffers
2176                         (adapter, MWIFIEX_MP_AGGR_BUF_SIZE_32K,
2177                          MWIFIEX_MP_AGGR_BUF_SIZE_32K);
2178                 if (ret) {
2179                         /* Disable multi port aggregation */
2180                         card->mpa_tx.enabled = 0;
2181                         card->mpa_rx.enabled = 0;
2182                 }
2183         }
2184
2185         adapter->auto_tdls = card->can_auto_tdls;
2186         adapter->ext_scan = card->can_ext_scan;
2187         return 0;
2188 }
2189
2190 /*
2191  * This function resets the MPA Tx and Rx buffers.
2192  */
2193 static void mwifiex_cleanup_mpa_buf(struct mwifiex_adapter *adapter)
2194 {
2195         struct sdio_mmc_card *card = adapter->card;
2196
2197         MP_TX_AGGR_BUF_RESET(card);
2198         MP_RX_AGGR_BUF_RESET(card);
2199 }
2200
2201 /*
2202  * This function cleans up the allocated card buffers.
2203  *
2204  * The following are freed by this function -
2205  *      - MP registers
2206  *      - MPA Tx buffer
2207  *      - MPA Rx buffer
2208  */
2209 static void mwifiex_cleanup_sdio(struct mwifiex_adapter *adapter)
2210 {
2211         struct sdio_mmc_card *card = adapter->card;
2212
2213         kfree(card->mp_regs);
2214         kfree(card->mpa_rx.skb_arr);
2215         kfree(card->mpa_rx.len_arr);
2216         kfree(card->mpa_tx.buf);
2217         kfree(card->mpa_rx.buf);
2218         sdio_set_drvdata(card->func, NULL);
2219         kfree(card);
2220 }
2221
2222 /*
2223  * This function updates the MP end port in card.
2224  */
2225 static void
2226 mwifiex_update_mp_end_port(struct mwifiex_adapter *adapter, u16 port)
2227 {
2228         struct sdio_mmc_card *card = adapter->card;
2229         const struct mwifiex_sdio_card_reg *reg = card->reg;
2230         int i;
2231
2232         card->mp_end_port = port;
2233
2234         card->mp_data_port_mask = reg->data_port_mask;
2235
2236         if (reg->start_wr_port) {
2237                 for (i = 1; i <= card->max_ports - card->mp_end_port; i++)
2238                         card->mp_data_port_mask &=
2239                                         ~(1 << (card->max_ports - i));
2240         }
2241
2242         card->curr_wr_port = reg->start_wr_port;
2243
2244         mwifiex_dbg(adapter, CMD,
2245                     "cmd: mp_end_port %d, data port mask 0x%x\n",
2246                     port, card->mp_data_port_mask);
2247 }
2248
2249 static void mwifiex_recreate_adapter(struct sdio_mmc_card *card)
2250 {
2251         struct sdio_func *func = card->func;
2252         const struct sdio_device_id *device_id = card->device_id;
2253
2254         /* TODO mmc_hw_reset does not require destroying and re-probing the
2255          * whole adapter. Hence there was no need to for this rube-goldberg
2256          * design to reload the fw from an external workqueue. If we don't
2257          * destroy the adapter we could reload the fw from
2258          * mwifiex_main_work_queue directly.
2259          * The real difficulty with fw reset is to restore all the user
2260          * settings applied through ioctl. By destroying and recreating the
2261          * adapter, we take the easy way out, since we rely on user space to
2262          * restore them. We assume that user space will treat the new
2263          * incarnation of the adapter(interfaces) as if they had been just
2264          * discovered and initializes them from scratch.
2265          */
2266
2267         mwifiex_sdio_remove(func);
2268
2269         /* power cycle the adapter */
2270         sdio_claim_host(func);
2271         mmc_hw_reset(func->card->host);
2272         sdio_release_host(func);
2273
2274         mwifiex_sdio_probe(func, device_id);
2275 }
2276
2277 static struct mwifiex_adapter *save_adapter;
2278 static void mwifiex_sdio_card_reset_work(struct mwifiex_adapter *adapter)
2279 {
2280         struct sdio_mmc_card *card = adapter->card;
2281
2282         /* TODO card pointer is unprotected. If the adapter is removed
2283          * physically, sdio core might trigger mwifiex_sdio_remove, before this
2284          * workqueue is run, which will destroy the adapter struct. When this
2285          * workqueue eventually exceutes it will dereference an invalid adapter
2286          * pointer
2287          */
2288         mwifiex_recreate_adapter(card);
2289 }
2290
2291 /* This function read/write firmware */
2292 static enum
2293 rdwr_status mwifiex_sdio_rdwr_firmware(struct mwifiex_adapter *adapter,
2294                                        u8 doneflag)
2295 {
2296         struct sdio_mmc_card *card = adapter->card;
2297         int ret, tries;
2298         u8 ctrl_data = 0;
2299
2300         sdio_writeb(card->func, card->reg->fw_dump_host_ready,
2301                     card->reg->fw_dump_ctrl, &ret);
2302         if (ret) {
2303                 mwifiex_dbg(adapter, ERROR, "SDIO Write ERR\n");
2304                 return RDWR_STATUS_FAILURE;
2305         }
2306         for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
2307                 ctrl_data = sdio_readb(card->func, card->reg->fw_dump_ctrl,
2308                                        &ret);
2309                 if (ret) {
2310                         mwifiex_dbg(adapter, ERROR, "SDIO read err\n");
2311                         return RDWR_STATUS_FAILURE;
2312                 }
2313                 if (ctrl_data == FW_DUMP_DONE)
2314                         break;
2315                 if (doneflag && ctrl_data == doneflag)
2316                         return RDWR_STATUS_DONE;
2317                 if (ctrl_data != card->reg->fw_dump_host_ready) {
2318                         mwifiex_dbg(adapter, WARN,
2319                                     "The ctrl reg was changed, re-try again\n");
2320                         sdio_writeb(card->func, card->reg->fw_dump_host_ready,
2321                                     card->reg->fw_dump_ctrl, &ret);
2322                         if (ret) {
2323                                 mwifiex_dbg(adapter, ERROR, "SDIO write err\n");
2324                                 return RDWR_STATUS_FAILURE;
2325                         }
2326                 }
2327                 usleep_range(100, 200);
2328         }
2329         if (ctrl_data == card->reg->fw_dump_host_ready) {
2330                 mwifiex_dbg(adapter, ERROR,
2331                             "Fail to pull ctrl_data\n");
2332                 return RDWR_STATUS_FAILURE;
2333         }
2334
2335         return RDWR_STATUS_SUCCESS;
2336 }
2337
2338 /* This function dump firmware memory to file */
2339 static void mwifiex_sdio_fw_dump(struct mwifiex_adapter *adapter)
2340 {
2341         struct sdio_mmc_card *card = adapter->card;
2342         int ret = 0;
2343         unsigned int reg, reg_start, reg_end;
2344         u8 *dbg_ptr, *end_ptr, dump_num, idx, i, read_reg, doneflag = 0;
2345         enum rdwr_status stat;
2346         u32 memory_size;
2347
2348         if (!card->can_dump_fw)
2349                 return;
2350
2351         for (idx = 0; idx < ARRAY_SIZE(mem_type_mapping_tbl); idx++) {
2352                 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
2353
2354                 if (entry->mem_ptr) {
2355                         vfree(entry->mem_ptr);
2356                         entry->mem_ptr = NULL;
2357                 }
2358                 entry->mem_size = 0;
2359         }
2360
2361         mwifiex_pm_wakeup_card(adapter);
2362         sdio_claim_host(card->func);
2363
2364         mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump start ==\n");
2365
2366         stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2367         if (stat == RDWR_STATUS_FAILURE)
2368                 goto done;
2369
2370         reg = card->reg->fw_dump_start;
2371         /* Read the number of the memories which will dump */
2372         dump_num = sdio_readb(card->func, reg, &ret);
2373         if (ret) {
2374                 mwifiex_dbg(adapter, ERROR, "SDIO read memory length err\n");
2375                 goto done;
2376         }
2377
2378         /* Read the length of every memory which will dump */
2379         for (idx = 0; idx < dump_num; idx++) {
2380                 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
2381
2382                 stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2383                 if (stat == RDWR_STATUS_FAILURE)
2384                         goto done;
2385
2386                 memory_size = 0;
2387                 reg = card->reg->fw_dump_start;
2388                 for (i = 0; i < 4; i++) {
2389                         read_reg = sdio_readb(card->func, reg, &ret);
2390                         if (ret) {
2391                                 mwifiex_dbg(adapter, ERROR, "SDIO read err\n");
2392                                 goto done;
2393                         }
2394                         memory_size |= (read_reg << i*8);
2395                         reg++;
2396                 }
2397
2398                 if (memory_size == 0) {
2399                         mwifiex_dbg(adapter, DUMP, "Firmware dump Finished!\n");
2400                         ret = mwifiex_write_reg(adapter,
2401                                                 card->reg->fw_dump_ctrl,
2402                                                 FW_DUMP_READ_DONE);
2403                         if (ret) {
2404                                 mwifiex_dbg(adapter, ERROR, "SDIO write err\n");
2405                                 return;
2406                         }
2407                         break;
2408                 }
2409
2410                 mwifiex_dbg(adapter, DUMP,
2411                             "%s_SIZE=0x%x\n", entry->mem_name, memory_size);
2412                 entry->mem_ptr = vmalloc(memory_size + 1);
2413                 entry->mem_size = memory_size;
2414                 if (!entry->mem_ptr) {
2415                         mwifiex_dbg(adapter, ERROR, "Vmalloc %s failed\n",
2416                                     entry->mem_name);
2417                         goto done;
2418                 }
2419                 dbg_ptr = entry->mem_ptr;
2420                 end_ptr = dbg_ptr + memory_size;
2421
2422                 doneflag = entry->done_flag;
2423                 mwifiex_dbg(adapter, DUMP,
2424                             "Start %s output, please wait...\n",
2425                             entry->mem_name);
2426
2427                 do {
2428                         stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2429                         if (stat == RDWR_STATUS_FAILURE)
2430                                 goto done;
2431
2432                         reg_start = card->reg->fw_dump_start;
2433                         reg_end = card->reg->fw_dump_end;
2434                         for (reg = reg_start; reg <= reg_end; reg++) {
2435                                 *dbg_ptr = sdio_readb(card->func, reg, &ret);
2436                                 if (ret) {
2437                                         mwifiex_dbg(adapter, ERROR,
2438                                                     "SDIO read err\n");
2439                                         goto done;
2440                                 }
2441                                 if (dbg_ptr < end_ptr)
2442                                         dbg_ptr++;
2443                                 else
2444                                         mwifiex_dbg(adapter, ERROR,
2445                                                     "Allocated buf not enough\n");
2446                         }
2447
2448                         if (stat != RDWR_STATUS_DONE)
2449                                 continue;
2450
2451                         mwifiex_dbg(adapter, DUMP, "%s done: size=0x%tx\n",
2452                                     entry->mem_name, dbg_ptr - entry->mem_ptr);
2453                         break;
2454                 } while (1);
2455         }
2456         mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump end ==\n");
2457
2458 done:
2459         sdio_release_host(card->func);
2460 }
2461
2462 static void mwifiex_sdio_generic_fw_dump(struct mwifiex_adapter *adapter)
2463 {
2464         struct sdio_mmc_card *card = adapter->card;
2465         struct memory_type_mapping *entry = &generic_mem_type_map[0];
2466         unsigned int reg, reg_start, reg_end;
2467         u8 start_flag = 0, done_flag = 0;
2468         u8 *dbg_ptr, *end_ptr;
2469         enum rdwr_status stat;
2470         int ret = -1, tries;
2471
2472         if (!card->fw_dump_enh)
2473                 return;
2474
2475         if (entry->mem_ptr) {
2476                 vfree(entry->mem_ptr);
2477                 entry->mem_ptr = NULL;
2478         }
2479         entry->mem_size = 0;
2480
2481         mwifiex_pm_wakeup_card(adapter);
2482         sdio_claim_host(card->func);
2483
2484         mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump start ==\n");
2485
2486         stat = mwifiex_sdio_rdwr_firmware(adapter, done_flag);
2487         if (stat == RDWR_STATUS_FAILURE)
2488                 goto done;
2489
2490         reg_start = card->reg->fw_dump_start;
2491         reg_end = card->reg->fw_dump_end;
2492         for (reg = reg_start; reg <= reg_end; reg++) {
2493                 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
2494                         start_flag = sdio_readb(card->func, reg, &ret);
2495                         if (ret) {
2496                                 mwifiex_dbg(adapter, ERROR,
2497                                             "SDIO read err\n");
2498                                 goto done;
2499                         }
2500                         if (start_flag == 0)
2501                                 break;
2502                         if (tries == MAX_POLL_TRIES) {
2503                                 mwifiex_dbg(adapter, ERROR,
2504                                             "FW not ready to dump\n");
2505                                 ret = -1;
2506                                 goto done;
2507                         }
2508                 }
2509                 usleep_range(100, 200);
2510         }
2511
2512         entry->mem_ptr = vmalloc(0xf0000 + 1);
2513         if (!entry->mem_ptr) {
2514                 ret = -1;
2515                 goto done;
2516         }
2517         dbg_ptr = entry->mem_ptr;
2518         entry->mem_size = 0xf0000;
2519         end_ptr = dbg_ptr + entry->mem_size;
2520
2521         done_flag = entry->done_flag;
2522         mwifiex_dbg(adapter, DUMP,
2523                     "Start %s output, please wait...\n", entry->mem_name);
2524
2525         while (true) {
2526                 stat = mwifiex_sdio_rdwr_firmware(adapter, done_flag);
2527                 if (stat == RDWR_STATUS_FAILURE)
2528                         goto done;
2529                 for (reg = reg_start; reg <= reg_end; reg++) {
2530                         *dbg_ptr = sdio_readb(card->func, reg, &ret);
2531                         if (ret) {
2532                                 mwifiex_dbg(adapter, ERROR,
2533                                             "SDIO read err\n");
2534                                 goto done;
2535                         }
2536                         dbg_ptr++;
2537                         if (dbg_ptr >= end_ptr) {
2538                                 u8 *tmp_ptr;
2539
2540                                 tmp_ptr = vmalloc(entry->mem_size + 0x4000 + 1);
2541                                 if (!tmp_ptr)
2542                                         goto done;
2543
2544                                 memcpy(tmp_ptr, entry->mem_ptr,
2545                                        entry->mem_size);
2546                                 vfree(entry->mem_ptr);
2547                                 entry->mem_ptr = tmp_ptr;
2548                                 tmp_ptr = NULL;
2549                                 dbg_ptr = entry->mem_ptr + entry->mem_size;
2550                                 entry->mem_size += 0x4000;
2551                                 end_ptr = entry->mem_ptr + entry->mem_size;
2552                         }
2553                 }
2554                 if (stat == RDWR_STATUS_DONE) {
2555                         entry->mem_size = dbg_ptr - entry->mem_ptr;
2556                         mwifiex_dbg(adapter, DUMP, "dump %s done size=0x%x\n",
2557                                     entry->mem_name, entry->mem_size);
2558                         ret = 0;
2559                         break;
2560                 }
2561         }
2562         mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump end ==\n");
2563
2564 done:
2565         if (ret) {
2566                 mwifiex_dbg(adapter, ERROR, "firmware dump failed\n");
2567                 if (entry->mem_ptr) {
2568                         vfree(entry->mem_ptr);
2569                         entry->mem_ptr = NULL;
2570                 }
2571                 entry->mem_size = 0;
2572         }
2573         sdio_release_host(card->func);
2574 }
2575
2576 static void mwifiex_sdio_device_dump_work(struct mwifiex_adapter *adapter)
2577 {
2578         struct sdio_mmc_card *card = adapter->card;
2579
2580         mwifiex_drv_info_dump(adapter);
2581         if (card->fw_dump_enh)
2582                 mwifiex_sdio_generic_fw_dump(adapter);
2583         else
2584                 mwifiex_sdio_fw_dump(adapter);
2585         mwifiex_upload_device_dump(adapter);
2586 }
2587
2588 static void mwifiex_sdio_work(struct work_struct *work)
2589 {
2590         if (test_and_clear_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP,
2591                                &iface_work_flags))
2592                 mwifiex_sdio_device_dump_work(save_adapter);
2593         if (test_and_clear_bit(MWIFIEX_IFACE_WORK_CARD_RESET,
2594                                &iface_work_flags))
2595                 mwifiex_sdio_card_reset_work(save_adapter);
2596 }
2597
2598 static DECLARE_WORK(sdio_work, mwifiex_sdio_work);
2599 /* This function resets the card */
2600 static void mwifiex_sdio_card_reset(struct mwifiex_adapter *adapter)
2601 {
2602         save_adapter = adapter;
2603         if (test_bit(MWIFIEX_IFACE_WORK_CARD_RESET, &iface_work_flags))
2604                 return;
2605
2606         set_bit(MWIFIEX_IFACE_WORK_CARD_RESET, &iface_work_flags);
2607
2608         schedule_work(&sdio_work);
2609 }
2610
2611 /* This function dumps FW information */
2612 static void mwifiex_sdio_device_dump(struct mwifiex_adapter *adapter)
2613 {
2614         save_adapter = adapter;
2615         if (test_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP, &iface_work_flags))
2616                 return;
2617
2618         set_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP, &iface_work_flags);
2619         schedule_work(&sdio_work);
2620 }
2621
2622 /* Function to dump SDIO function registers and SDIO scratch registers in case
2623  * of FW crash
2624  */
2625 static int
2626 mwifiex_sdio_reg_dump(struct mwifiex_adapter *adapter, char *drv_buf)
2627 {
2628         char *p = drv_buf;
2629         struct sdio_mmc_card *cardp = adapter->card;
2630         int ret = 0;
2631         u8 count, func, data, index = 0, size = 0;
2632         u8 reg, reg_start, reg_end;
2633         char buf[256], *ptr;
2634
2635         if (!p)
2636                 return 0;
2637
2638         mwifiex_dbg(adapter, MSG, "SDIO register dump start\n");
2639
2640         mwifiex_pm_wakeup_card(adapter);
2641
2642         sdio_claim_host(cardp->func);
2643
2644         for (count = 0; count < 5; count++) {
2645                 memset(buf, 0, sizeof(buf));
2646                 ptr = buf;
2647
2648                 switch (count) {
2649                 case 0:
2650                         /* Read the registers of SDIO function0 */
2651                         func = count;
2652                         reg_start = 0;
2653                         reg_end = 9;
2654                         break;
2655                 case 1:
2656                         /* Read the registers of SDIO function1 */
2657                         func = count;
2658                         reg_start = cardp->reg->func1_dump_reg_start;
2659                         reg_end = cardp->reg->func1_dump_reg_end;
2660                         break;
2661                 case 2:
2662                         index = 0;
2663                         func = 1;
2664                         reg_start = cardp->reg->func1_spec_reg_table[index++];
2665                         size = cardp->reg->func1_spec_reg_num;
2666                         reg_end = cardp->reg->func1_spec_reg_table[size-1];
2667                         break;
2668                 default:
2669                         /* Read the scratch registers of SDIO function1 */
2670                         if (count == 4)
2671                                 mdelay(100);
2672                         func = 1;
2673                         reg_start = cardp->reg->func1_scratch_reg;
2674                         reg_end = reg_start + MWIFIEX_SDIO_SCRATCH_SIZE;
2675                 }
2676
2677                 if (count != 2)
2678                         ptr += sprintf(ptr, "SDIO Func%d (%#x-%#x): ",
2679                                        func, reg_start, reg_end);
2680                 else
2681                         ptr += sprintf(ptr, "SDIO Func%d: ", func);
2682
2683                 for (reg = reg_start; reg <= reg_end;) {
2684                         if (func == 0)
2685                                 data = sdio_f0_readb(cardp->func, reg, &ret);
2686                         else
2687                                 data = sdio_readb(cardp->func, reg, &ret);
2688
2689                         if (count == 2)
2690                                 ptr += sprintf(ptr, "(%#x) ", reg);
2691                         if (!ret) {
2692                                 ptr += sprintf(ptr, "%02x ", data);
2693                         } else {
2694                                 ptr += sprintf(ptr, "ERR");
2695                                 break;
2696                         }
2697
2698                         if (count == 2 && reg < reg_end)
2699                                 reg = cardp->reg->func1_spec_reg_table[index++];
2700                         else
2701                                 reg++;
2702                 }
2703
2704                 mwifiex_dbg(adapter, MSG, "%s\n", buf);
2705                 p += sprintf(p, "%s\n", buf);
2706         }
2707
2708         sdio_release_host(cardp->func);
2709
2710         mwifiex_dbg(adapter, MSG, "SDIO register dump end\n");
2711
2712         return p - drv_buf;
2713 }
2714
2715 static struct mwifiex_if_ops sdio_ops = {
2716         .init_if = mwifiex_init_sdio,
2717         .cleanup_if = mwifiex_cleanup_sdio,
2718         .check_fw_status = mwifiex_check_fw_status,
2719         .check_winner_status = mwifiex_check_winner_status,
2720         .prog_fw = mwifiex_prog_fw_w_helper,
2721         .register_dev = mwifiex_register_dev,
2722         .unregister_dev = mwifiex_unregister_dev,
2723         .enable_int = mwifiex_sdio_enable_host_int,
2724         .disable_int = mwifiex_sdio_disable_host_int,
2725         .process_int_status = mwifiex_process_int_status,
2726         .host_to_card = mwifiex_sdio_host_to_card,
2727         .wakeup = mwifiex_pm_wakeup_card,
2728         .wakeup_complete = mwifiex_pm_wakeup_card_complete,
2729
2730         /* SDIO specific */
2731         .update_mp_end_port = mwifiex_update_mp_end_port,
2732         .cleanup_mpa_buf = mwifiex_cleanup_mpa_buf,
2733         .cmdrsp_complete = mwifiex_sdio_cmdrsp_complete,
2734         .event_complete = mwifiex_sdio_event_complete,
2735         .card_reset = mwifiex_sdio_card_reset,
2736         .reg_dump = mwifiex_sdio_reg_dump,
2737         .device_dump = mwifiex_sdio_device_dump,
2738         .deaggr_pkt = mwifiex_deaggr_sdio_pkt,
2739 };
2740
2741 /*
2742  * This function initializes the SDIO driver.
2743  *
2744  * This initiates the semaphore and registers the device with
2745  * SDIO bus.
2746  */
2747 static int
2748 mwifiex_sdio_init_module(void)
2749 {
2750         sema_init(&add_remove_card_sem, 1);
2751
2752         /* Clear the flag in case user removes the card. */
2753         user_rmmod = 0;
2754
2755         return sdio_register_driver(&mwifiex_sdio);
2756 }
2757
2758 /*
2759  * This function cleans up the SDIO driver.
2760  *
2761  * The following major steps are followed for cleanup -
2762  *      - Resume the device if its suspended
2763  *      - Disconnect the device if connected
2764  *      - Shutdown the firmware
2765  *      - Unregister the device from SDIO bus.
2766  */
2767 static void
2768 mwifiex_sdio_cleanup_module(void)
2769 {
2770         if (!down_interruptible(&add_remove_card_sem))
2771                 up(&add_remove_card_sem);
2772
2773         /* Set the flag as user is removing this module. */
2774         user_rmmod = 1;
2775         cancel_work_sync(&sdio_work);
2776
2777         sdio_unregister_driver(&mwifiex_sdio);
2778 }
2779
2780 module_init(mwifiex_sdio_init_module);
2781 module_exit(mwifiex_sdio_cleanup_module);
2782
2783 MODULE_AUTHOR("Marvell International Ltd.");
2784 MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION);
2785 MODULE_VERSION(SDIO_VERSION);
2786 MODULE_LICENSE("GPL v2");
2787 MODULE_FIRMWARE(SD8786_DEFAULT_FW_NAME);
2788 MODULE_FIRMWARE(SD8787_DEFAULT_FW_NAME);
2789 MODULE_FIRMWARE(SD8797_DEFAULT_FW_NAME);
2790 MODULE_FIRMWARE(SD8897_DEFAULT_FW_NAME);
2791 MODULE_FIRMWARE(SD8887_DEFAULT_FW_NAME);
2792 MODULE_FIRMWARE(SD8997_DEFAULT_FW_NAME);