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