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