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