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