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