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