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