xref: /linux/drivers/net/wireless/marvell/mwifiex/sdio.c (revision 0883c2c06fb5bcf5b9e008270827e63c09a88c1e)
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 		dev_err(dev, "sdio platform data not available\n");
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,
119 				"fail to parse irq_wifi from device tree\n");
120 		} else {
121 			ret = devm_request_irq(dev, cfg->irq_wifi,
122 					       mwifiex_wake_irq_wifi,
123 					       IRQF_TRIGGER_LOW,
124 					       "wifi_wake", cfg);
125 			if (ret) {
126 				dev_err(dev,
127 					"Failed to request irq_wifi %d (%d)\n",
128 					cfg->irq_wifi, ret);
129 			}
130 			disable_irq(cfg->irq_wifi);
131 		}
132 	}
133 
134 	return 0;
135 }
136 
137 /*
138  * SDIO probe.
139  *
140  * This function probes an mwifiex device and registers it. It allocates
141  * the card structure, enables SDIO function number and initiates the
142  * device registration and initialization procedure by adding a logical
143  * interface.
144  */
145 static int
146 mwifiex_sdio_probe(struct sdio_func *func, const struct sdio_device_id *id)
147 {
148 	int ret;
149 	struct sdio_mmc_card *card = NULL;
150 
151 	pr_debug("info: vendor=0x%4.04X device=0x%4.04X class=%d function=%d\n",
152 		 func->vendor, func->device, func->class, func->num);
153 
154 	card = kzalloc(sizeof(struct sdio_mmc_card), GFP_KERNEL);
155 	if (!card)
156 		return -ENOMEM;
157 
158 	card->func = func;
159 	card->device_id = id;
160 
161 	func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
162 
163 	if (id->driver_data) {
164 		struct mwifiex_sdio_device *data = (void *)id->driver_data;
165 
166 		card->firmware = data->firmware;
167 		card->reg = data->reg;
168 		card->max_ports = data->max_ports;
169 		card->mp_agg_pkt_limit = data->mp_agg_pkt_limit;
170 		card->supports_sdio_new_mode = data->supports_sdio_new_mode;
171 		card->has_control_mask = data->has_control_mask;
172 		card->tx_buf_size = data->tx_buf_size;
173 		card->mp_tx_agg_buf_size = data->mp_tx_agg_buf_size;
174 		card->mp_rx_agg_buf_size = data->mp_rx_agg_buf_size;
175 		card->can_dump_fw = data->can_dump_fw;
176 		card->fw_dump_enh = data->fw_dump_enh;
177 		card->can_auto_tdls = data->can_auto_tdls;
178 		card->can_ext_scan = data->can_ext_scan;
179 	}
180 
181 	sdio_claim_host(func);
182 	ret = sdio_enable_func(func);
183 	sdio_release_host(func);
184 
185 	if (ret) {
186 		pr_err("%s: failed to enable function\n", __func__);
187 		kfree(card);
188 		return -EIO;
189 	}
190 
191 	/* device tree node parsing and platform specific configuration*/
192 	mwifiex_sdio_probe_of(&func->dev, card);
193 
194 	if (mwifiex_add_card(card, &add_remove_card_sem, &sdio_ops,
195 			     MWIFIEX_SDIO)) {
196 		pr_err("%s: add card failed\n", __func__);
197 		kfree(card);
198 		sdio_claim_host(func);
199 		ret = sdio_disable_func(func);
200 		sdio_release_host(func);
201 		ret = -1;
202 	}
203 
204 	return ret;
205 }
206 
207 /*
208  * SDIO resume.
209  *
210  * Kernel needs to suspend all functions separately. Therefore all
211  * registered functions must have drivers with suspend and resume
212  * methods. Failing that the kernel simply removes the whole card.
213  *
214  * If already not resumed, this function turns on the traffic and
215  * sends a host sleep cancel request to the firmware.
216  */
217 static int mwifiex_sdio_resume(struct device *dev)
218 {
219 	struct sdio_func *func = dev_to_sdio_func(dev);
220 	struct sdio_mmc_card *card;
221 	struct mwifiex_adapter *adapter;
222 	mmc_pm_flag_t pm_flag = 0;
223 
224 	if (func) {
225 		pm_flag = sdio_get_host_pm_caps(func);
226 		card = sdio_get_drvdata(func);
227 		if (!card || !card->adapter) {
228 			pr_err("resume: invalid card or adapter\n");
229 			return 0;
230 		}
231 	} else {
232 		pr_err("resume: sdio_func is not specified\n");
233 		return 0;
234 	}
235 
236 	adapter = card->adapter;
237 
238 	if (!adapter->is_suspended) {
239 		mwifiex_dbg(adapter, WARN,
240 			    "device already resumed\n");
241 		return 0;
242 	}
243 
244 	adapter->is_suspended = false;
245 
246 	/* Disable Host Sleep */
247 	mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
248 			  MWIFIEX_SYNC_CMD);
249 
250 	/* Disable platform specific wakeup interrupt */
251 	if (card->plt_wake_cfg && card->plt_wake_cfg->irq_wifi >= 0) {
252 		disable_irq_wake(card->plt_wake_cfg->irq_wifi);
253 		if (!card->plt_wake_cfg->wake_by_wifi)
254 			disable_irq(card->plt_wake_cfg->irq_wifi);
255 	}
256 
257 	return 0;
258 }
259 
260 /*
261  * SDIO remove.
262  *
263  * This function removes the interface and frees up the card structure.
264  */
265 static void
266 mwifiex_sdio_remove(struct sdio_func *func)
267 {
268 	struct sdio_mmc_card *card;
269 	struct mwifiex_adapter *adapter;
270 	struct mwifiex_private *priv;
271 
272 	card = sdio_get_drvdata(func);
273 	if (!card)
274 		return;
275 
276 	adapter = card->adapter;
277 	if (!adapter || !adapter->priv_num)
278 		return;
279 
280 	mwifiex_dbg(adapter, INFO, "info: SDIO func num=%d\n", func->num);
281 
282 	if (user_rmmod) {
283 		if (adapter->is_suspended)
284 			mwifiex_sdio_resume(adapter->dev);
285 
286 		mwifiex_deauthenticate_all(adapter);
287 
288 		priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
289 		mwifiex_disable_auto_ds(priv);
290 		mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
291 	}
292 
293 	mwifiex_remove_card(card->adapter, &add_remove_card_sem);
294 }
295 
296 /*
297  * SDIO suspend.
298  *
299  * Kernel needs to suspend all functions separately. Therefore all
300  * registered functions must have drivers with suspend and resume
301  * methods. Failing that the kernel simply removes the whole card.
302  *
303  * If already not suspended, this function allocates and sends a host
304  * sleep activate request to the firmware and turns off the traffic.
305  */
306 static int mwifiex_sdio_suspend(struct device *dev)
307 {
308 	struct sdio_func *func = dev_to_sdio_func(dev);
309 	struct sdio_mmc_card *card;
310 	struct mwifiex_adapter *adapter;
311 	mmc_pm_flag_t pm_flag = 0;
312 	int ret = 0;
313 
314 	if (func) {
315 		pm_flag = sdio_get_host_pm_caps(func);
316 		pr_debug("cmd: %s: suspend: PM flag = 0x%x\n",
317 			 sdio_func_id(func), pm_flag);
318 		if (!(pm_flag & MMC_PM_KEEP_POWER)) {
319 			pr_err("%s: cannot remain alive while host is"
320 				" suspended\n", sdio_func_id(func));
321 			return -ENOSYS;
322 		}
323 
324 		card = sdio_get_drvdata(func);
325 		if (!card || !card->adapter) {
326 			pr_err("suspend: invalid card or adapter\n");
327 			return 0;
328 		}
329 	} else {
330 		pr_err("suspend: sdio_func is not specified\n");
331 		return 0;
332 	}
333 
334 	adapter = card->adapter;
335 
336 	/* Enable platform specific wakeup interrupt */
337 	if (card->plt_wake_cfg && card->plt_wake_cfg->irq_wifi >= 0) {
338 		card->plt_wake_cfg->wake_by_wifi = false;
339 		enable_irq(card->plt_wake_cfg->irq_wifi);
340 		enable_irq_wake(card->plt_wake_cfg->irq_wifi);
341 	}
342 
343 	/* Enable the Host Sleep */
344 	if (!mwifiex_enable_hs(adapter)) {
345 		mwifiex_dbg(adapter, ERROR,
346 			    "cmd: failed to suspend\n");
347 		adapter->hs_enabling = false;
348 		return -EFAULT;
349 	}
350 
351 	mwifiex_dbg(adapter, INFO,
352 		    "cmd: suspend with MMC_PM_KEEP_POWER\n");
353 	ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
354 
355 	/* Indicate device suspended */
356 	adapter->is_suspended = true;
357 	adapter->hs_enabling = false;
358 
359 	return ret;
360 }
361 
362 /* Device ID for SD8786 */
363 #define SDIO_DEVICE_ID_MARVELL_8786   (0x9116)
364 /* Device ID for SD8787 */
365 #define SDIO_DEVICE_ID_MARVELL_8787   (0x9119)
366 /* Device ID for SD8797 */
367 #define SDIO_DEVICE_ID_MARVELL_8797   (0x9129)
368 /* Device ID for SD8897 */
369 #define SDIO_DEVICE_ID_MARVELL_8897   (0x912d)
370 /* Device ID for SD8887 */
371 #define SDIO_DEVICE_ID_MARVELL_8887   (0x9135)
372 /* Device ID for SD8801 */
373 #define SDIO_DEVICE_ID_MARVELL_8801   (0x9139)
374 /* Device ID for SD8997 */
375 #define SDIO_DEVICE_ID_MARVELL_8997   (0x9141)
376 
377 
378 /* WLAN IDs */
379 static const struct sdio_device_id mwifiex_ids[] = {
380 	{SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8786),
381 		.driver_data = (unsigned long) &mwifiex_sdio_sd8786},
382 	{SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787),
383 		.driver_data = (unsigned long) &mwifiex_sdio_sd8787},
384 	{SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8797),
385 		.driver_data = (unsigned long) &mwifiex_sdio_sd8797},
386 	{SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8897),
387 		.driver_data = (unsigned long) &mwifiex_sdio_sd8897},
388 	{SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8887),
389 		.driver_data = (unsigned long)&mwifiex_sdio_sd8887},
390 	{SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8801),
391 		.driver_data = (unsigned long)&mwifiex_sdio_sd8801},
392 	{SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8997),
393 		.driver_data = (unsigned long)&mwifiex_sdio_sd8997},
394 	{},
395 };
396 
397 MODULE_DEVICE_TABLE(sdio, mwifiex_ids);
398 
399 static const struct dev_pm_ops mwifiex_sdio_pm_ops = {
400 	.suspend = mwifiex_sdio_suspend,
401 	.resume = mwifiex_sdio_resume,
402 };
403 
404 static struct sdio_driver mwifiex_sdio = {
405 	.name = "mwifiex_sdio",
406 	.id_table = mwifiex_ids,
407 	.probe = mwifiex_sdio_probe,
408 	.remove = mwifiex_sdio_remove,
409 	.drv = {
410 		.owner = THIS_MODULE,
411 		.pm = &mwifiex_sdio_pm_ops,
412 	}
413 };
414 
415 /* Write data into SDIO card register. Caller claims SDIO device. */
416 static int
417 mwifiex_write_reg_locked(struct sdio_func *func, u32 reg, u8 data)
418 {
419 	int ret = -1;
420 	sdio_writeb(func, data, reg, &ret);
421 	return ret;
422 }
423 
424 /*
425  * This function writes data into SDIO card register.
426  */
427 static int
428 mwifiex_write_reg(struct mwifiex_adapter *adapter, u32 reg, u8 data)
429 {
430 	struct sdio_mmc_card *card = adapter->card;
431 	int ret;
432 
433 	sdio_claim_host(card->func);
434 	ret = mwifiex_write_reg_locked(card->func, reg, data);
435 	sdio_release_host(card->func);
436 
437 	return ret;
438 }
439 
440 /*
441  * This function reads data from SDIO card register.
442  */
443 static int
444 mwifiex_read_reg(struct mwifiex_adapter *adapter, u32 reg, u8 *data)
445 {
446 	struct sdio_mmc_card *card = adapter->card;
447 	int ret = -1;
448 	u8 val;
449 
450 	sdio_claim_host(card->func);
451 	val = sdio_readb(card->func, reg, &ret);
452 	sdio_release_host(card->func);
453 
454 	*data = val;
455 
456 	return ret;
457 }
458 
459 /*
460  * This function writes multiple data into SDIO card memory.
461  *
462  * This does not work in suspended mode.
463  */
464 static int
465 mwifiex_write_data_sync(struct mwifiex_adapter *adapter,
466 			u8 *buffer, u32 pkt_len, u32 port)
467 {
468 	struct sdio_mmc_card *card = adapter->card;
469 	int ret;
470 	u8 blk_mode =
471 		(port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE : BLOCK_MODE;
472 	u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
473 	u32 blk_cnt =
474 		(blk_mode ==
475 		 BLOCK_MODE) ? (pkt_len /
476 				MWIFIEX_SDIO_BLOCK_SIZE) : pkt_len;
477 	u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
478 
479 	if (adapter->is_suspended) {
480 		mwifiex_dbg(adapter, ERROR,
481 			    "%s: not allowed while suspended\n", __func__);
482 		return -1;
483 	}
484 
485 	sdio_claim_host(card->func);
486 
487 	ret = sdio_writesb(card->func, ioport, buffer, blk_cnt * blk_size);
488 
489 	sdio_release_host(card->func);
490 
491 	return ret;
492 }
493 
494 /*
495  * This function reads multiple data from SDIO card memory.
496  */
497 static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *buffer,
498 				  u32 len, u32 port, u8 claim)
499 {
500 	struct sdio_mmc_card *card = adapter->card;
501 	int ret;
502 	u8 blk_mode = (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE
503 		       : BLOCK_MODE;
504 	u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
505 	u32 blk_cnt = (blk_mode == BLOCK_MODE) ? (len / MWIFIEX_SDIO_BLOCK_SIZE)
506 			: len;
507 	u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
508 
509 	if (claim)
510 		sdio_claim_host(card->func);
511 
512 	ret = sdio_readsb(card->func, buffer, ioport, blk_cnt * blk_size);
513 
514 	if (claim)
515 		sdio_release_host(card->func);
516 
517 	return ret;
518 }
519 
520 /*
521  * This function wakes up the card.
522  *
523  * A host power up command is written to the card configuration
524  * register to wake up the card.
525  */
526 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
527 {
528 	mwifiex_dbg(adapter, EVENT,
529 		    "event: wakeup device...\n");
530 
531 	return mwifiex_write_reg(adapter, CONFIGURATION_REG, HOST_POWER_UP);
532 }
533 
534 /*
535  * This function is called after the card has woken up.
536  *
537  * The card configuration register is reset.
538  */
539 static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
540 {
541 	mwifiex_dbg(adapter, EVENT,
542 		    "cmd: wakeup device completed\n");
543 
544 	return mwifiex_write_reg(adapter, CONFIGURATION_REG, 0);
545 }
546 
547 /*
548  * This function is used to initialize IO ports for the
549  * chipsets supporting SDIO new mode eg SD8897.
550  */
551 static int mwifiex_init_sdio_new_mode(struct mwifiex_adapter *adapter)
552 {
553 	u8 reg;
554 	struct sdio_mmc_card *card = adapter->card;
555 
556 	adapter->ioport = MEM_PORT;
557 
558 	/* enable sdio new mode */
559 	if (mwifiex_read_reg(adapter, card->reg->card_cfg_2_1_reg, &reg))
560 		return -1;
561 	if (mwifiex_write_reg(adapter, card->reg->card_cfg_2_1_reg,
562 			      reg | CMD53_NEW_MODE))
563 		return -1;
564 
565 	/* Configure cmd port and enable reading rx length from the register */
566 	if (mwifiex_read_reg(adapter, card->reg->cmd_cfg_0, &reg))
567 		return -1;
568 	if (mwifiex_write_reg(adapter, card->reg->cmd_cfg_0,
569 			      reg | CMD_PORT_RD_LEN_EN))
570 		return -1;
571 
572 	/* Enable Dnld/Upld ready auto reset for cmd port after cmd53 is
573 	 * completed
574 	 */
575 	if (mwifiex_read_reg(adapter, card->reg->cmd_cfg_1, &reg))
576 		return -1;
577 	if (mwifiex_write_reg(adapter, card->reg->cmd_cfg_1,
578 			      reg | CMD_PORT_AUTO_EN))
579 		return -1;
580 
581 	return 0;
582 }
583 
584 /* This function initializes the IO ports.
585  *
586  * The following operations are performed -
587  *      - Read the IO ports (0, 1 and 2)
588  *      - Set host interrupt Reset-To-Read to clear
589  *      - Set auto re-enable interrupt
590  */
591 static int mwifiex_init_sdio_ioport(struct mwifiex_adapter *adapter)
592 {
593 	u8 reg;
594 	struct sdio_mmc_card *card = adapter->card;
595 
596 	adapter->ioport = 0;
597 
598 	if (card->supports_sdio_new_mode) {
599 		if (mwifiex_init_sdio_new_mode(adapter))
600 			return -1;
601 		goto cont;
602 	}
603 
604 	/* Read the IO port */
605 	if (!mwifiex_read_reg(adapter, card->reg->io_port_0_reg, &reg))
606 		adapter->ioport |= (reg & 0xff);
607 	else
608 		return -1;
609 
610 	if (!mwifiex_read_reg(adapter, card->reg->io_port_1_reg, &reg))
611 		adapter->ioport |= ((reg & 0xff) << 8);
612 	else
613 		return -1;
614 
615 	if (!mwifiex_read_reg(adapter, card->reg->io_port_2_reg, &reg))
616 		adapter->ioport |= ((reg & 0xff) << 16);
617 	else
618 		return -1;
619 cont:
620 	mwifiex_dbg(adapter, INFO,
621 		    "info: SDIO FUNC1 IO port: %#x\n", adapter->ioport);
622 
623 	/* Set Host interrupt reset to read to clear */
624 	if (!mwifiex_read_reg(adapter, card->reg->host_int_rsr_reg, &reg))
625 		mwifiex_write_reg(adapter, card->reg->host_int_rsr_reg,
626 				  reg | card->reg->sdio_int_mask);
627 	else
628 		return -1;
629 
630 	/* Dnld/Upld ready set to auto reset */
631 	if (!mwifiex_read_reg(adapter, card->reg->card_misc_cfg_reg, &reg))
632 		mwifiex_write_reg(adapter, card->reg->card_misc_cfg_reg,
633 				  reg | AUTO_RE_ENABLE_INT);
634 	else
635 		return -1;
636 
637 	return 0;
638 }
639 
640 /*
641  * This function sends data to the card.
642  */
643 static int mwifiex_write_data_to_card(struct mwifiex_adapter *adapter,
644 				      u8 *payload, u32 pkt_len, u32 port)
645 {
646 	u32 i = 0;
647 	int ret;
648 
649 	do {
650 		ret = mwifiex_write_data_sync(adapter, payload, pkt_len, port);
651 		if (ret) {
652 			i++;
653 			mwifiex_dbg(adapter, ERROR,
654 				    "host_to_card, write iomem\t"
655 				    "(%d) failed: %d\n", i, ret);
656 			if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
657 				mwifiex_dbg(adapter, ERROR,
658 					    "write CFG reg failed\n");
659 
660 			ret = -1;
661 			if (i > MAX_WRITE_IOMEM_RETRY)
662 				return ret;
663 		}
664 	} while (ret == -1);
665 
666 	return ret;
667 }
668 
669 /*
670  * This function gets the read port.
671  *
672  * If control port bit is set in MP read bitmap, the control port
673  * is returned, otherwise the current read port is returned and
674  * the value is increased (provided it does not reach the maximum
675  * limit, in which case it is reset to 1)
676  */
677 static int mwifiex_get_rd_port(struct mwifiex_adapter *adapter, u8 *port)
678 {
679 	struct sdio_mmc_card *card = adapter->card;
680 	const struct mwifiex_sdio_card_reg *reg = card->reg;
681 	u32 rd_bitmap = card->mp_rd_bitmap;
682 
683 	mwifiex_dbg(adapter, DATA,
684 		    "data: mp_rd_bitmap=0x%08x\n", rd_bitmap);
685 
686 	if (card->supports_sdio_new_mode) {
687 		if (!(rd_bitmap & reg->data_port_mask))
688 			return -1;
689 	} else {
690 		if (!(rd_bitmap & (CTRL_PORT_MASK | reg->data_port_mask)))
691 			return -1;
692 	}
693 
694 	if ((card->has_control_mask) &&
695 	    (card->mp_rd_bitmap & CTRL_PORT_MASK)) {
696 		card->mp_rd_bitmap &= (u32) (~CTRL_PORT_MASK);
697 		*port = CTRL_PORT;
698 		mwifiex_dbg(adapter, DATA,
699 			    "data: port=%d mp_rd_bitmap=0x%08x\n",
700 			    *port, card->mp_rd_bitmap);
701 		return 0;
702 	}
703 
704 	if (!(card->mp_rd_bitmap & (1 << card->curr_rd_port)))
705 		return -1;
706 
707 	/* We are now handling the SDIO data ports */
708 	card->mp_rd_bitmap &= (u32)(~(1 << card->curr_rd_port));
709 	*port = card->curr_rd_port;
710 
711 	if (++card->curr_rd_port == card->max_ports)
712 		card->curr_rd_port = reg->start_rd_port;
713 
714 	mwifiex_dbg(adapter, DATA,
715 		    "data: port=%d mp_rd_bitmap=0x%08x -> 0x%08x\n",
716 		    *port, rd_bitmap, card->mp_rd_bitmap);
717 
718 	return 0;
719 }
720 
721 /*
722  * This function gets the write port for data.
723  *
724  * The current write port is returned if available and the value is
725  * increased (provided it does not reach the maximum limit, in which
726  * case it is reset to 1)
727  */
728 static int mwifiex_get_wr_port_data(struct mwifiex_adapter *adapter, u32 *port)
729 {
730 	struct sdio_mmc_card *card = adapter->card;
731 	const struct mwifiex_sdio_card_reg *reg = card->reg;
732 	u32 wr_bitmap = card->mp_wr_bitmap;
733 
734 	mwifiex_dbg(adapter, DATA,
735 		    "data: mp_wr_bitmap=0x%08x\n", wr_bitmap);
736 
737 	if (!(wr_bitmap & card->mp_data_port_mask)) {
738 		adapter->data_sent = true;
739 		return -EBUSY;
740 	}
741 
742 	if (card->mp_wr_bitmap & (1 << card->curr_wr_port)) {
743 		card->mp_wr_bitmap &= (u32) (~(1 << card->curr_wr_port));
744 		*port = card->curr_wr_port;
745 		if (++card->curr_wr_port == card->mp_end_port)
746 			card->curr_wr_port = reg->start_wr_port;
747 	} else {
748 		adapter->data_sent = true;
749 		return -EBUSY;
750 	}
751 
752 	if ((card->has_control_mask) && (*port == CTRL_PORT)) {
753 		mwifiex_dbg(adapter, ERROR,
754 			    "invalid data port=%d cur port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
755 			    *port, card->curr_wr_port, wr_bitmap,
756 			    card->mp_wr_bitmap);
757 		return -1;
758 	}
759 
760 	mwifiex_dbg(adapter, DATA,
761 		    "data: port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
762 		    *port, wr_bitmap, card->mp_wr_bitmap);
763 
764 	return 0;
765 }
766 
767 /*
768  * This function polls the card status.
769  */
770 static int
771 mwifiex_sdio_poll_card_status(struct mwifiex_adapter *adapter, u8 bits)
772 {
773 	struct sdio_mmc_card *card = adapter->card;
774 	u32 tries;
775 	u8 cs;
776 
777 	for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
778 		if (mwifiex_read_reg(adapter, card->reg->poll_reg, &cs))
779 			break;
780 		else if ((cs & bits) == bits)
781 			return 0;
782 
783 		usleep_range(10, 20);
784 	}
785 
786 	mwifiex_dbg(adapter, ERROR,
787 		    "poll card status failed, tries = %d\n", tries);
788 
789 	return -1;
790 }
791 
792 /*
793  * This function reads the firmware status.
794  */
795 static int
796 mwifiex_sdio_read_fw_status(struct mwifiex_adapter *adapter, u16 *dat)
797 {
798 	struct sdio_mmc_card *card = adapter->card;
799 	const struct mwifiex_sdio_card_reg *reg = card->reg;
800 	u8 fws0, fws1;
801 
802 	if (mwifiex_read_reg(adapter, reg->status_reg_0, &fws0))
803 		return -1;
804 
805 	if (mwifiex_read_reg(adapter, reg->status_reg_1, &fws1))
806 		return -1;
807 
808 	*dat = (u16) ((fws1 << 8) | fws0);
809 
810 	return 0;
811 }
812 
813 /*
814  * This function disables the host interrupt.
815  *
816  * The host interrupt mask is read, the disable bit is reset and
817  * written back to the card host interrupt mask register.
818  */
819 static void mwifiex_sdio_disable_host_int(struct mwifiex_adapter *adapter)
820 {
821 	struct sdio_mmc_card *card = adapter->card;
822 	struct sdio_func *func = card->func;
823 
824 	sdio_claim_host(func);
825 	mwifiex_write_reg_locked(func, card->reg->host_int_mask_reg, 0);
826 	sdio_release_irq(func);
827 	sdio_release_host(func);
828 }
829 
830 /*
831  * This function reads the interrupt status from card.
832  */
833 static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
834 {
835 	struct sdio_mmc_card *card = adapter->card;
836 	u8 sdio_ireg;
837 	unsigned long flags;
838 
839 	if (mwifiex_read_data_sync(adapter, card->mp_regs,
840 				   card->reg->max_mp_regs,
841 				   REG_PORT | MWIFIEX_SDIO_BYTE_MODE_MASK, 0)) {
842 		mwifiex_dbg(adapter, ERROR, "read mp_regs failed\n");
843 		return;
844 	}
845 
846 	sdio_ireg = card->mp_regs[card->reg->host_int_status_reg];
847 	if (sdio_ireg) {
848 		/*
849 		 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
850 		 * For SDIO new mode CMD port interrupts
851 		 *	DN_LD_CMD_PORT_HOST_INT_STATUS and/or
852 		 *	UP_LD_CMD_PORT_HOST_INT_STATUS
853 		 * Clear the interrupt status register
854 		 */
855 		mwifiex_dbg(adapter, INTR,
856 			    "int: sdio_ireg = %#x\n", sdio_ireg);
857 		spin_lock_irqsave(&adapter->int_lock, flags);
858 		adapter->int_status |= sdio_ireg;
859 		spin_unlock_irqrestore(&adapter->int_lock, flags);
860 	}
861 }
862 
863 /*
864  * SDIO interrupt handler.
865  *
866  * This function reads the interrupt status from firmware and handles
867  * the interrupt in current thread (ksdioirqd) right away.
868  */
869 static void
870 mwifiex_sdio_interrupt(struct sdio_func *func)
871 {
872 	struct mwifiex_adapter *adapter;
873 	struct sdio_mmc_card *card;
874 
875 	card = sdio_get_drvdata(func);
876 	if (!card || !card->adapter) {
877 		pr_err("int: func=%p card=%p adapter=%p\n",
878 		       func, card, card ? card->adapter : NULL);
879 		return;
880 	}
881 	adapter = card->adapter;
882 
883 	if (!adapter->pps_uapsd_mode && adapter->ps_state == PS_STATE_SLEEP)
884 		adapter->ps_state = PS_STATE_AWAKE;
885 
886 	mwifiex_interrupt_status(adapter);
887 	mwifiex_main_process(adapter);
888 }
889 
890 /*
891  * This function enables the host interrupt.
892  *
893  * The host interrupt enable mask is written to the card
894  * host interrupt mask register.
895  */
896 static int mwifiex_sdio_enable_host_int(struct mwifiex_adapter *adapter)
897 {
898 	struct sdio_mmc_card *card = adapter->card;
899 	struct sdio_func *func = card->func;
900 	int ret;
901 
902 	sdio_claim_host(func);
903 
904 	/* Request the SDIO IRQ */
905 	ret = sdio_claim_irq(func, mwifiex_sdio_interrupt);
906 	if (ret) {
907 		mwifiex_dbg(adapter, ERROR,
908 			    "claim irq failed: ret=%d\n", ret);
909 		goto out;
910 	}
911 
912 	/* Simply write the mask to the register */
913 	ret = mwifiex_write_reg_locked(func, card->reg->host_int_mask_reg,
914 				       card->reg->host_int_enable);
915 	if (ret) {
916 		mwifiex_dbg(adapter, ERROR,
917 			    "enable host interrupt failed\n");
918 		sdio_release_irq(func);
919 	}
920 
921 out:
922 	sdio_release_host(func);
923 	return ret;
924 }
925 
926 /*
927  * This function sends a data buffer to the card.
928  */
929 static int mwifiex_sdio_card_to_host(struct mwifiex_adapter *adapter,
930 				     u32 *type, u8 *buffer,
931 				     u32 npayload, u32 ioport)
932 {
933 	int ret;
934 	u32 nb;
935 
936 	if (!buffer) {
937 		mwifiex_dbg(adapter, ERROR,
938 			    "%s: buffer is NULL\n", __func__);
939 		return -1;
940 	}
941 
942 	ret = mwifiex_read_data_sync(adapter, buffer, npayload, ioport, 1);
943 
944 	if (ret) {
945 		mwifiex_dbg(adapter, ERROR,
946 			    "%s: read iomem failed: %d\n", __func__,
947 			ret);
948 		return -1;
949 	}
950 
951 	nb = le16_to_cpu(*(__le16 *) (buffer));
952 	if (nb > npayload) {
953 		mwifiex_dbg(adapter, ERROR,
954 			    "%s: invalid packet, nb=%d npayload=%d\n",
955 			    __func__, nb, npayload);
956 		return -1;
957 	}
958 
959 	*type = le16_to_cpu(*(__le16 *) (buffer + 2));
960 
961 	return ret;
962 }
963 
964 /*
965  * This function downloads the firmware to the card.
966  *
967  * Firmware is downloaded to the card in blocks. Every block download
968  * is tested for CRC errors, and retried a number of times before
969  * returning failure.
970  */
971 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
972 				    struct mwifiex_fw_image *fw)
973 {
974 	struct sdio_mmc_card *card = adapter->card;
975 	const struct mwifiex_sdio_card_reg *reg = card->reg;
976 	int ret;
977 	u8 *firmware = fw->fw_buf;
978 	u32 firmware_len = fw->fw_len;
979 	u32 offset = 0;
980 	u8 base0, base1;
981 	u8 *fwbuf;
982 	u16 len = 0;
983 	u32 txlen, tx_blocks = 0, tries;
984 	u32 i = 0;
985 
986 	if (!firmware_len) {
987 		mwifiex_dbg(adapter, ERROR,
988 			    "firmware image not found! Terminating download\n");
989 		return -1;
990 	}
991 
992 	mwifiex_dbg(adapter, INFO,
993 		    "info: downloading FW image (%d bytes)\n",
994 		    firmware_len);
995 
996 	/* Assume that the allocated buffer is 8-byte aligned */
997 	fwbuf = kzalloc(MWIFIEX_UPLD_SIZE, GFP_KERNEL);
998 	if (!fwbuf)
999 		return -ENOMEM;
1000 
1001 	sdio_claim_host(card->func);
1002 
1003 	/* Perform firmware data transfer */
1004 	do {
1005 		/* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY
1006 		   bits */
1007 		ret = mwifiex_sdio_poll_card_status(adapter, CARD_IO_READY |
1008 						    DN_LD_CARD_RDY);
1009 		if (ret) {
1010 			mwifiex_dbg(adapter, ERROR,
1011 				    "FW download with helper:\t"
1012 				    "poll status timeout @ %d\n", offset);
1013 			goto done;
1014 		}
1015 
1016 		/* More data? */
1017 		if (offset >= firmware_len)
1018 			break;
1019 
1020 		for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
1021 			ret = mwifiex_read_reg(adapter, reg->base_0_reg,
1022 					       &base0);
1023 			if (ret) {
1024 				mwifiex_dbg(adapter, ERROR,
1025 					    "dev BASE0 register read failed:\t"
1026 					    "base0=%#04X(%d). Terminating dnld\n",
1027 					    base0, base0);
1028 				goto done;
1029 			}
1030 			ret = mwifiex_read_reg(adapter, reg->base_1_reg,
1031 					       &base1);
1032 			if (ret) {
1033 				mwifiex_dbg(adapter, ERROR,
1034 					    "dev BASE1 register read failed:\t"
1035 					    "base1=%#04X(%d). Terminating dnld\n",
1036 					    base1, base1);
1037 				goto done;
1038 			}
1039 			len = (u16) (((base1 & 0xff) << 8) | (base0 & 0xff));
1040 
1041 			if (len)
1042 				break;
1043 
1044 			usleep_range(10, 20);
1045 		}
1046 
1047 		if (!len) {
1048 			break;
1049 		} else if (len > MWIFIEX_UPLD_SIZE) {
1050 			mwifiex_dbg(adapter, ERROR,
1051 				    "FW dnld failed @ %d, invalid length %d\n",
1052 				    offset, len);
1053 			ret = -1;
1054 			goto done;
1055 		}
1056 
1057 		txlen = len;
1058 
1059 		if (len & BIT(0)) {
1060 			i++;
1061 			if (i > MAX_WRITE_IOMEM_RETRY) {
1062 				mwifiex_dbg(adapter, ERROR,
1063 					    "FW dnld failed @ %d, over max retry\n",
1064 					    offset);
1065 				ret = -1;
1066 				goto done;
1067 			}
1068 			mwifiex_dbg(adapter, ERROR,
1069 				    "CRC indicated by the helper:\t"
1070 				    "len = 0x%04X, txlen = %d\n", len, txlen);
1071 			len &= ~BIT(0);
1072 			/* Setting this to 0 to resend from same offset */
1073 			txlen = 0;
1074 		} else {
1075 			i = 0;
1076 
1077 			/* Set blocksize to transfer - checking for last
1078 			   block */
1079 			if (firmware_len - offset < txlen)
1080 				txlen = firmware_len - offset;
1081 
1082 			tx_blocks = (txlen + MWIFIEX_SDIO_BLOCK_SIZE - 1)
1083 				    / MWIFIEX_SDIO_BLOCK_SIZE;
1084 
1085 			/* Copy payload to buffer */
1086 			memmove(fwbuf, &firmware[offset], txlen);
1087 		}
1088 
1089 		ret = mwifiex_write_data_sync(adapter, fwbuf, tx_blocks *
1090 					      MWIFIEX_SDIO_BLOCK_SIZE,
1091 					      adapter->ioport);
1092 		if (ret) {
1093 			mwifiex_dbg(adapter, ERROR,
1094 				    "FW download, write iomem (%d) failed @ %d\n",
1095 				    i, offset);
1096 			if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
1097 				mwifiex_dbg(adapter, ERROR,
1098 					    "write CFG reg failed\n");
1099 
1100 			ret = -1;
1101 			goto done;
1102 		}
1103 
1104 		offset += txlen;
1105 	} while (true);
1106 
1107 	mwifiex_dbg(adapter, MSG,
1108 		    "info: FW download over, size %d bytes\n", offset);
1109 
1110 	ret = 0;
1111 done:
1112 	sdio_release_host(card->func);
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);
2793