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