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