sdio.c revision 5dbd326ca7acc54320c38bdc474600c811f8ad6b
1/*
2 * Marvell Wireless LAN device driver: SDIO specific handling
3 *
4 * Copyright (C) 2011, 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;
50
51static struct semaphore add_remove_card_sem;
52
53static int mwifiex_sdio_resume(struct device *dev);
54
55/*
56 * SDIO probe.
57 *
58 * This function probes an mwifiex device and registers it. It allocates
59 * the card structure, enables SDIO function number and initiates the
60 * device registration and initialization procedure by adding a logical
61 * interface.
62 */
63static int
64mwifiex_sdio_probe(struct sdio_func *func, const struct sdio_device_id *id)
65{
66	int ret;
67	struct sdio_mmc_card *card = NULL;
68
69	pr_debug("info: vendor=0x%4.04X device=0x%4.04X class=%d function=%d\n",
70		 func->vendor, func->device, func->class, func->num);
71
72	card = kzalloc(sizeof(struct sdio_mmc_card), GFP_KERNEL);
73	if (!card) {
74		pr_err("%s: failed to alloc memory\n", __func__);
75		return -ENOMEM;
76	}
77
78	card->func = func;
79
80	func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
81
82	sdio_claim_host(func);
83	ret = sdio_enable_func(func);
84	sdio_release_host(func);
85
86	if (ret) {
87		pr_err("%s: failed to enable function\n", __func__);
88		kfree(card);
89		return -EIO;
90	}
91
92	if (mwifiex_add_card(card, &add_remove_card_sem, &sdio_ops,
93			     MWIFIEX_SDIO)) {
94		pr_err("%s: add card failed\n", __func__);
95		kfree(card);
96		sdio_claim_host(func);
97		ret = sdio_disable_func(func);
98		sdio_release_host(func);
99		ret = -1;
100	}
101
102	return ret;
103}
104
105/*
106 * SDIO remove.
107 *
108 * This function removes the interface and frees up the card structure.
109 */
110static void
111mwifiex_sdio_remove(struct sdio_func *func)
112{
113	struct sdio_mmc_card *card;
114	struct mwifiex_adapter *adapter;
115	struct mwifiex_private *priv;
116	int i;
117
118	pr_debug("info: SDIO func num=%d\n", func->num);
119
120	card = sdio_get_drvdata(func);
121	if (!card)
122		return;
123
124	adapter = card->adapter;
125	if (!adapter || !adapter->priv_num)
126		return;
127
128	if (user_rmmod) {
129		if (adapter->is_suspended)
130			mwifiex_sdio_resume(adapter->dev);
131
132		for (i = 0; i < adapter->priv_num; i++)
133			if ((GET_BSS_ROLE(adapter->priv[i]) ==
134						MWIFIEX_BSS_ROLE_STA) &&
135			    adapter->priv[i]->media_connected)
136				mwifiex_deauthenticate(adapter->priv[i], NULL);
137
138		priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
139		mwifiex_disable_auto_ds(priv);
140		mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
141	}
142
143	mwifiex_remove_card(card->adapter, &add_remove_card_sem);
144	kfree(card);
145}
146
147/*
148 * SDIO suspend.
149 *
150 * Kernel needs to suspend all functions separately. Therefore all
151 * registered functions must have drivers with suspend and resume
152 * methods. Failing that the kernel simply removes the whole card.
153 *
154 * If already not suspended, this function allocates and sends a host
155 * sleep activate request to the firmware and turns off the traffic.
156 */
157static int mwifiex_sdio_suspend(struct device *dev)
158{
159	struct sdio_func *func = dev_to_sdio_func(dev);
160	struct sdio_mmc_card *card;
161	struct mwifiex_adapter *adapter;
162	mmc_pm_flag_t pm_flag = 0;
163	int hs_actived = 0;
164	int i;
165	int ret = 0;
166
167	if (func) {
168		pm_flag = sdio_get_host_pm_caps(func);
169		pr_debug("cmd: %s: suspend: PM flag = 0x%x\n",
170			 sdio_func_id(func), pm_flag);
171		if (!(pm_flag & MMC_PM_KEEP_POWER)) {
172			pr_err("%s: cannot remain alive while host is"
173				" suspended\n", sdio_func_id(func));
174			return -ENOSYS;
175		}
176
177		card = sdio_get_drvdata(func);
178		if (!card || !card->adapter) {
179			pr_err("suspend: invalid card or adapter\n");
180			return 0;
181		}
182	} else {
183		pr_err("suspend: sdio_func is not specified\n");
184		return 0;
185	}
186
187	adapter = card->adapter;
188
189	/* Enable the Host Sleep */
190	hs_actived = mwifiex_enable_hs(adapter);
191	if (hs_actived) {
192		pr_debug("cmd: suspend with MMC_PM_KEEP_POWER\n");
193		ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
194	}
195
196	/* Indicate device suspended */
197	adapter->is_suspended = true;
198
199	for (i = 0; i < adapter->priv_num; i++)
200		netif_carrier_off(adapter->priv[i]->netdev);
201
202	return ret;
203}
204
205/*
206 * SDIO resume.
207 *
208 * Kernel needs to suspend all functions separately. Therefore all
209 * registered functions must have drivers with suspend and resume
210 * methods. Failing that the kernel simply removes the whole card.
211 *
212 * If already not resumed, this function turns on the traffic and
213 * sends a host sleep cancel request to the firmware.
214 */
215static int mwifiex_sdio_resume(struct device *dev)
216{
217	struct sdio_func *func = dev_to_sdio_func(dev);
218	struct sdio_mmc_card *card;
219	struct mwifiex_adapter *adapter;
220	mmc_pm_flag_t pm_flag = 0;
221	int i;
222
223	if (func) {
224		pm_flag = sdio_get_host_pm_caps(func);
225		card = sdio_get_drvdata(func);
226		if (!card || !card->adapter) {
227			pr_err("resume: invalid card or adapter\n");
228			return 0;
229		}
230	} else {
231		pr_err("resume: sdio_func is not specified\n");
232		return 0;
233	}
234
235	adapter = card->adapter;
236
237	if (!adapter->is_suspended) {
238		dev_warn(adapter->dev, "device already resumed\n");
239		return 0;
240	}
241
242	adapter->is_suspended = false;
243
244	for (i = 0; i < adapter->priv_num; i++)
245		if (adapter->priv[i]->media_connected)
246			netif_carrier_on(adapter->priv[i]->netdev);
247
248	/* Disable Host Sleep */
249	mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
250			  MWIFIEX_ASYNC_CMD);
251
252	return 0;
253}
254
255/* Device ID for SD8787 */
256#define SDIO_DEVICE_ID_MARVELL_8787   (0x9119)
257/* Device ID for SD8797 */
258#define SDIO_DEVICE_ID_MARVELL_8797   (0x9129)
259
260/* WLAN IDs */
261static const struct sdio_device_id mwifiex_ids[] = {
262	{SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787)},
263	{SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8797)},
264	{},
265};
266
267MODULE_DEVICE_TABLE(sdio, mwifiex_ids);
268
269static const struct dev_pm_ops mwifiex_sdio_pm_ops = {
270	.suspend = mwifiex_sdio_suspend,
271	.resume = mwifiex_sdio_resume,
272};
273
274static struct sdio_driver mwifiex_sdio = {
275	.name = "mwifiex_sdio",
276	.id_table = mwifiex_ids,
277	.probe = mwifiex_sdio_probe,
278	.remove = mwifiex_sdio_remove,
279	.drv = {
280		.owner = THIS_MODULE,
281		.pm = &mwifiex_sdio_pm_ops,
282	}
283};
284
285/*
286 * This function writes data into SDIO card register.
287 */
288static int
289mwifiex_write_reg(struct mwifiex_adapter *adapter, u32 reg, u32 data)
290{
291	struct sdio_mmc_card *card = adapter->card;
292	int ret = -1;
293
294	sdio_claim_host(card->func);
295	sdio_writeb(card->func, (u8) data, reg, &ret);
296	sdio_release_host(card->func);
297
298	return ret;
299}
300
301/*
302 * This function reads data from SDIO card register.
303 */
304static int
305mwifiex_read_reg(struct mwifiex_adapter *adapter, u32 reg, u32 *data)
306{
307	struct sdio_mmc_card *card = adapter->card;
308	int ret = -1;
309	u8 val;
310
311	sdio_claim_host(card->func);
312	val = sdio_readb(card->func, reg, &ret);
313	sdio_release_host(card->func);
314
315	*data = val;
316
317	return ret;
318}
319
320/*
321 * This function writes multiple data into SDIO card memory.
322 *
323 * This does not work in suspended mode.
324 */
325static int
326mwifiex_write_data_sync(struct mwifiex_adapter *adapter,
327			u8 *buffer, u32 pkt_len, u32 port)
328{
329	struct sdio_mmc_card *card = adapter->card;
330	int ret = -1;
331	u8 blk_mode =
332		(port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE : BLOCK_MODE;
333	u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
334	u32 blk_cnt =
335		(blk_mode ==
336		 BLOCK_MODE) ? (pkt_len /
337				MWIFIEX_SDIO_BLOCK_SIZE) : pkt_len;
338	u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
339
340	if (adapter->is_suspended) {
341		dev_err(adapter->dev,
342			"%s: not allowed while suspended\n", __func__);
343		return -1;
344	}
345
346	sdio_claim_host(card->func);
347
348	if (!sdio_writesb(card->func, ioport, buffer, blk_cnt * blk_size))
349		ret = 0;
350
351	sdio_release_host(card->func);
352
353	return ret;
354}
355
356/*
357 * This function reads multiple data from SDIO card memory.
358 */
359static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *buffer,
360				  u32 len, u32 port, u8 claim)
361{
362	struct sdio_mmc_card *card = adapter->card;
363	int ret = -1;
364	u8 blk_mode = (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE
365		       : BLOCK_MODE;
366	u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
367	u32 blk_cnt = (blk_mode == BLOCK_MODE) ? (len / MWIFIEX_SDIO_BLOCK_SIZE)
368			: len;
369	u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
370
371	if (claim)
372		sdio_claim_host(card->func);
373
374	if (!sdio_readsb(card->func, buffer, ioport, blk_cnt * blk_size))
375		ret = 0;
376
377	if (claim)
378		sdio_release_host(card->func);
379
380	return ret;
381}
382
383/*
384 * This function wakes up the card.
385 *
386 * A host power up command is written to the card configuration
387 * register to wake up the card.
388 */
389static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
390{
391	dev_dbg(adapter->dev, "event: wakeup device...\n");
392
393	return mwifiex_write_reg(adapter, CONFIGURATION_REG, HOST_POWER_UP);
394}
395
396/*
397 * This function is called after the card has woken up.
398 *
399 * The card configuration register is reset.
400 */
401static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
402{
403	dev_dbg(adapter->dev, "cmd: wakeup device completed\n");
404
405	return mwifiex_write_reg(adapter, CONFIGURATION_REG, 0);
406}
407
408/*
409 * This function initializes the IO ports.
410 *
411 * The following operations are performed -
412 *      - Read the IO ports (0, 1 and 2)
413 *      - Set host interrupt Reset-To-Read to clear
414 *      - Set auto re-enable interrupt
415 */
416static int mwifiex_init_sdio_ioport(struct mwifiex_adapter *adapter)
417{
418	u32 reg;
419
420	adapter->ioport = 0;
421
422	/* Read the IO port */
423	if (!mwifiex_read_reg(adapter, IO_PORT_0_REG, &reg))
424		adapter->ioport |= (reg & 0xff);
425	else
426		return -1;
427
428	if (!mwifiex_read_reg(adapter, IO_PORT_1_REG, &reg))
429		adapter->ioport |= ((reg & 0xff) << 8);
430	else
431		return -1;
432
433	if (!mwifiex_read_reg(adapter, IO_PORT_2_REG, &reg))
434		adapter->ioport |= ((reg & 0xff) << 16);
435	else
436		return -1;
437
438	pr_debug("info: SDIO FUNC1 IO port: %#x\n", adapter->ioport);
439
440	/* Set Host interrupt reset to read to clear */
441	if (!mwifiex_read_reg(adapter, HOST_INT_RSR_REG, &reg))
442		mwifiex_write_reg(adapter, HOST_INT_RSR_REG,
443				  reg | SDIO_INT_MASK);
444	else
445		return -1;
446
447	/* Dnld/Upld ready set to auto reset */
448	if (!mwifiex_read_reg(adapter, CARD_MISC_CFG_REG, &reg))
449		mwifiex_write_reg(adapter, CARD_MISC_CFG_REG,
450				  reg | AUTO_RE_ENABLE_INT);
451	else
452		return -1;
453
454	return 0;
455}
456
457/*
458 * This function sends data to the card.
459 */
460static int mwifiex_write_data_to_card(struct mwifiex_adapter *adapter,
461				      u8 *payload, u32 pkt_len, u32 port)
462{
463	u32 i = 0;
464	int ret;
465
466	do {
467		ret = mwifiex_write_data_sync(adapter, payload, pkt_len, port);
468		if (ret) {
469			i++;
470			dev_err(adapter->dev, "host_to_card, write iomem"
471					" (%d) failed: %d\n", i, ret);
472			if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
473				dev_err(adapter->dev, "write CFG reg failed\n");
474
475			ret = -1;
476			if (i > MAX_WRITE_IOMEM_RETRY)
477				return ret;
478		}
479	} while (ret == -1);
480
481	return ret;
482}
483
484/*
485 * This function gets the read port.
486 *
487 * If control port bit is set in MP read bitmap, the control port
488 * is returned, otherwise the current read port is returned and
489 * the value is increased (provided it does not reach the maximum
490 * limit, in which case it is reset to 1)
491 */
492static int mwifiex_get_rd_port(struct mwifiex_adapter *adapter, u8 *port)
493{
494	struct sdio_mmc_card *card = adapter->card;
495	u16 rd_bitmap = card->mp_rd_bitmap;
496
497	dev_dbg(adapter->dev, "data: mp_rd_bitmap=0x%04x\n", rd_bitmap);
498
499	if (!(rd_bitmap & (CTRL_PORT_MASK | DATA_PORT_MASK)))
500		return -1;
501
502	if (card->mp_rd_bitmap & CTRL_PORT_MASK) {
503		card->mp_rd_bitmap &= (u16) (~CTRL_PORT_MASK);
504		*port = CTRL_PORT;
505		dev_dbg(adapter->dev, "data: port=%d mp_rd_bitmap=0x%04x\n",
506			*port, card->mp_rd_bitmap);
507	} else {
508		if (card->mp_rd_bitmap & (1 << card->curr_rd_port)) {
509			card->mp_rd_bitmap &= (u16)
510						(~(1 << card->curr_rd_port));
511			*port = card->curr_rd_port;
512
513			if (++card->curr_rd_port == MAX_PORT)
514				card->curr_rd_port = 1;
515		} else {
516			return -1;
517		}
518
519		dev_dbg(adapter->dev,
520			"data: port=%d mp_rd_bitmap=0x%04x -> 0x%04x\n",
521			*port, rd_bitmap, card->mp_rd_bitmap);
522	}
523	return 0;
524}
525
526/*
527 * This function gets the write port for data.
528 *
529 * The current write port is returned if available and the value is
530 * increased (provided it does not reach the maximum limit, in which
531 * case it is reset to 1)
532 */
533static int mwifiex_get_wr_port_data(struct mwifiex_adapter *adapter, u8 *port)
534{
535	struct sdio_mmc_card *card = adapter->card;
536	u16 wr_bitmap = card->mp_wr_bitmap;
537
538	dev_dbg(adapter->dev, "data: mp_wr_bitmap=0x%04x\n", wr_bitmap);
539
540	if (!(wr_bitmap & card->mp_data_port_mask))
541		return -1;
542
543	if (card->mp_wr_bitmap & (1 << card->curr_wr_port)) {
544		card->mp_wr_bitmap &= (u16) (~(1 << card->curr_wr_port));
545		*port = card->curr_wr_port;
546		if (++card->curr_wr_port == card->mp_end_port)
547			card->curr_wr_port = 1;
548	} else {
549		adapter->data_sent = true;
550		return -EBUSY;
551	}
552
553	if (*port == CTRL_PORT) {
554		dev_err(adapter->dev, "invalid data port=%d cur port=%d"
555			" mp_wr_bitmap=0x%04x -> 0x%04x\n",
556			*port, card->curr_wr_port, wr_bitmap,
557			card->mp_wr_bitmap);
558		return -1;
559	}
560
561	dev_dbg(adapter->dev, "data: port=%d mp_wr_bitmap=0x%04x -> 0x%04x\n",
562		*port, wr_bitmap, card->mp_wr_bitmap);
563
564	return 0;
565}
566
567/*
568 * This function polls the card status.
569 */
570static int
571mwifiex_sdio_poll_card_status(struct mwifiex_adapter *adapter, u8 bits)
572{
573	u32 tries;
574	u32 cs;
575
576	for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
577		if (mwifiex_read_reg(adapter, CARD_STATUS_REG, &cs))
578			break;
579		else if ((cs & bits) == bits)
580			return 0;
581
582		usleep_range(10, 20);
583	}
584
585	dev_err(adapter->dev, "poll card status failed, tries = %d\n", tries);
586
587	return -1;
588}
589
590/*
591 * This function reads the firmware status.
592 */
593static int
594mwifiex_sdio_read_fw_status(struct mwifiex_adapter *adapter, u16 *dat)
595{
596	u32 fws0, fws1;
597
598	if (mwifiex_read_reg(adapter, CARD_FW_STATUS0_REG, &fws0))
599		return -1;
600
601	if (mwifiex_read_reg(adapter, CARD_FW_STATUS1_REG, &fws1))
602		return -1;
603
604	*dat = (u16) ((fws1 << 8) | fws0);
605
606	return 0;
607}
608
609/*
610 * This function disables the host interrupt.
611 *
612 * The host interrupt mask is read, the disable bit is reset and
613 * written back to the card host interrupt mask register.
614 */
615static int mwifiex_sdio_disable_host_int(struct mwifiex_adapter *adapter)
616{
617	u32 host_int_mask;
618
619	/* Read back the host_int_mask register */
620	if (mwifiex_read_reg(adapter, HOST_INT_MASK_REG, &host_int_mask))
621		return -1;
622
623	/* Update with the mask and write back to the register */
624	host_int_mask &= ~HOST_INT_DISABLE;
625
626	if (mwifiex_write_reg(adapter, HOST_INT_MASK_REG, host_int_mask)) {
627		dev_err(adapter->dev, "disable host interrupt failed\n");
628		return -1;
629	}
630
631	return 0;
632}
633
634/*
635 * This function enables the host interrupt.
636 *
637 * The host interrupt enable mask is written to the card
638 * host interrupt mask register.
639 */
640static int mwifiex_sdio_enable_host_int(struct mwifiex_adapter *adapter)
641{
642	/* Simply write the mask to the register */
643	if (mwifiex_write_reg(adapter, HOST_INT_MASK_REG, HOST_INT_ENABLE)) {
644		dev_err(adapter->dev, "enable host interrupt failed\n");
645		return -1;
646	}
647	return 0;
648}
649
650/*
651 * This function sends a data buffer to the card.
652 */
653static int mwifiex_sdio_card_to_host(struct mwifiex_adapter *adapter,
654				     u32 *type, u8 *buffer,
655				     u32 npayload, u32 ioport)
656{
657	int ret;
658	u32 nb;
659
660	if (!buffer) {
661		dev_err(adapter->dev, "%s: buffer is NULL\n", __func__);
662		return -1;
663	}
664
665	ret = mwifiex_read_data_sync(adapter, buffer, npayload, ioport, 1);
666
667	if (ret) {
668		dev_err(adapter->dev, "%s: read iomem failed: %d\n", __func__,
669			ret);
670		return -1;
671	}
672
673	nb = le16_to_cpu(*(__le16 *) (buffer));
674	if (nb > npayload) {
675		dev_err(adapter->dev, "%s: invalid packet, nb=%d npayload=%d\n",
676			__func__, nb, npayload);
677		return -1;
678	}
679
680	*type = le16_to_cpu(*(__le16 *) (buffer + 2));
681
682	return ret;
683}
684
685/*
686 * This function downloads the firmware to the card.
687 *
688 * Firmware is downloaded to the card in blocks. Every block download
689 * is tested for CRC errors, and retried a number of times before
690 * returning failure.
691 */
692static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
693				    struct mwifiex_fw_image *fw)
694{
695	int ret;
696	u8 *firmware = fw->fw_buf;
697	u32 firmware_len = fw->fw_len;
698	u32 offset = 0;
699	u32 base0, base1;
700	u8 *fwbuf;
701	u16 len = 0;
702	u32 txlen, tx_blocks = 0, tries;
703	u32 i = 0;
704
705	if (!firmware_len) {
706		dev_err(adapter->dev,
707			"firmware image not found! Terminating download\n");
708		return -1;
709	}
710
711	dev_dbg(adapter->dev, "info: downloading FW image (%d bytes)\n",
712		firmware_len);
713
714	/* Assume that the allocated buffer is 8-byte aligned */
715	fwbuf = kzalloc(MWIFIEX_UPLD_SIZE, GFP_KERNEL);
716	if (!fwbuf) {
717		dev_err(adapter->dev,
718			"unable to alloc buffer for FW. Terminating dnld\n");
719		return -ENOMEM;
720	}
721
722	/* Perform firmware data transfer */
723	do {
724		/* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY
725		   bits */
726		ret = mwifiex_sdio_poll_card_status(adapter, CARD_IO_READY |
727						    DN_LD_CARD_RDY);
728		if (ret) {
729			dev_err(adapter->dev, "FW download with helper:"
730				" poll status timeout @ %d\n", offset);
731			goto done;
732		}
733
734		/* More data? */
735		if (offset >= firmware_len)
736			break;
737
738		for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
739			ret = mwifiex_read_reg(adapter, HOST_F1_RD_BASE_0,
740					       &base0);
741			if (ret) {
742				dev_err(adapter->dev,
743					"dev BASE0 register read failed: "
744					"base0=%#04X(%d). Terminating dnld\n",
745					base0, base0);
746				goto done;
747			}
748			ret = mwifiex_read_reg(adapter, HOST_F1_RD_BASE_1,
749					       &base1);
750			if (ret) {
751				dev_err(adapter->dev,
752					"dev BASE1 register read failed: "
753					"base1=%#04X(%d). Terminating dnld\n",
754					base1, base1);
755				goto done;
756			}
757			len = (u16) (((base1 & 0xff) << 8) | (base0 & 0xff));
758
759			if (len)
760				break;
761
762			usleep_range(10, 20);
763		}
764
765		if (!len) {
766			break;
767		} else if (len > MWIFIEX_UPLD_SIZE) {
768			dev_err(adapter->dev,
769				"FW dnld failed @ %d, invalid length %d\n",
770				offset, len);
771			ret = -1;
772			goto done;
773		}
774
775		txlen = len;
776
777		if (len & BIT(0)) {
778			i++;
779			if (i > MAX_WRITE_IOMEM_RETRY) {
780				dev_err(adapter->dev,
781					"FW dnld failed @ %d, over max retry\n",
782					offset);
783				ret = -1;
784				goto done;
785			}
786			dev_err(adapter->dev, "CRC indicated by the helper:"
787				" len = 0x%04X, txlen = %d\n", len, txlen);
788			len &= ~BIT(0);
789			/* Setting this to 0 to resend from same offset */
790			txlen = 0;
791		} else {
792			i = 0;
793
794			/* Set blocksize to transfer - checking for last
795			   block */
796			if (firmware_len - offset < txlen)
797				txlen = firmware_len - offset;
798
799			tx_blocks = (txlen + MWIFIEX_SDIO_BLOCK_SIZE - 1)
800				    / MWIFIEX_SDIO_BLOCK_SIZE;
801
802			/* Copy payload to buffer */
803			memmove(fwbuf, &firmware[offset], txlen);
804		}
805
806		ret = mwifiex_write_data_sync(adapter, fwbuf, tx_blocks *
807					      MWIFIEX_SDIO_BLOCK_SIZE,
808					      adapter->ioport);
809		if (ret) {
810			dev_err(adapter->dev,
811				"FW download, write iomem (%d) failed @ %d\n",
812				i, offset);
813			if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
814				dev_err(adapter->dev, "write CFG reg failed\n");
815
816			ret = -1;
817			goto done;
818		}
819
820		offset += txlen;
821	} while (true);
822
823	dev_dbg(adapter->dev, "info: FW download over, size %d bytes\n",
824		offset);
825
826	ret = 0;
827done:
828	kfree(fwbuf);
829	return ret;
830}
831
832/*
833 * This function checks the firmware status in card.
834 *
835 * The winner interface is also determined by this function.
836 */
837static int mwifiex_check_fw_status(struct mwifiex_adapter *adapter,
838				   u32 poll_num)
839{
840	int ret = 0;
841	u16 firmware_stat;
842	u32 tries;
843	u32 winner_status;
844
845	/* Wait for firmware initialization event */
846	for (tries = 0; tries < poll_num; tries++) {
847		ret = mwifiex_sdio_read_fw_status(adapter, &firmware_stat);
848		if (ret)
849			continue;
850		if (firmware_stat == FIRMWARE_READY_SDIO) {
851			ret = 0;
852			break;
853		} else {
854			mdelay(100);
855			ret = -1;
856		}
857	}
858
859	if (ret) {
860		if (mwifiex_read_reg
861		    (adapter, CARD_FW_STATUS0_REG, &winner_status))
862			winner_status = 0;
863
864		if (winner_status)
865			adapter->winner = 0;
866		else
867			adapter->winner = 1;
868	}
869	return ret;
870}
871
872/*
873 * This function reads the interrupt status from card.
874 */
875static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
876{
877	struct sdio_mmc_card *card = adapter->card;
878	u32 sdio_ireg;
879	unsigned long flags;
880
881	if (mwifiex_read_data_sync(adapter, card->mp_regs, MAX_MP_REGS,
882				   REG_PORT | MWIFIEX_SDIO_BYTE_MODE_MASK,
883				   0)) {
884		dev_err(adapter->dev, "read mp_regs failed\n");
885		return;
886	}
887
888	sdio_ireg = card->mp_regs[HOST_INTSTATUS_REG];
889	if (sdio_ireg) {
890		/*
891		 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
892		 * Clear the interrupt status register
893		 */
894		dev_dbg(adapter->dev, "int: sdio_ireg = %#x\n", sdio_ireg);
895		spin_lock_irqsave(&adapter->int_lock, flags);
896		adapter->int_status |= sdio_ireg;
897		spin_unlock_irqrestore(&adapter->int_lock, flags);
898	}
899}
900
901/*
902 * SDIO interrupt handler.
903 *
904 * This function reads the interrupt status from firmware and assigns
905 * the main process in workqueue which will handle the interrupt.
906 */
907static void
908mwifiex_sdio_interrupt(struct sdio_func *func)
909{
910	struct mwifiex_adapter *adapter;
911	struct sdio_mmc_card *card;
912
913	card = sdio_get_drvdata(func);
914	if (!card || !card->adapter) {
915		pr_debug("int: func=%p card=%p adapter=%p\n",
916			 func, card, card ? card->adapter : NULL);
917		return;
918	}
919	adapter = card->adapter;
920
921	if (adapter->surprise_removed)
922		return;
923
924	if (!adapter->pps_uapsd_mode && adapter->ps_state == PS_STATE_SLEEP)
925		adapter->ps_state = PS_STATE_AWAKE;
926
927	mwifiex_interrupt_status(adapter);
928	queue_work(adapter->workqueue, &adapter->main_work);
929}
930
931/*
932 * This function decodes a received packet.
933 *
934 * Based on the type, the packet is treated as either a data, or
935 * a command response, or an event, and the correct handler
936 * function is invoked.
937 */
938static int mwifiex_decode_rx_packet(struct mwifiex_adapter *adapter,
939				    struct sk_buff *skb, u32 upld_typ)
940{
941	u8 *cmd_buf;
942
943	skb_pull(skb, INTF_HEADER_LEN);
944
945	switch (upld_typ) {
946	case MWIFIEX_TYPE_DATA:
947		dev_dbg(adapter->dev, "info: --- Rx: Data packet ---\n");
948		mwifiex_handle_rx_packet(adapter, skb);
949		break;
950
951	case MWIFIEX_TYPE_CMD:
952		dev_dbg(adapter->dev, "info: --- Rx: Cmd Response ---\n");
953		/* take care of curr_cmd = NULL case */
954		if (!adapter->curr_cmd) {
955			cmd_buf = adapter->upld_buf;
956
957			if (adapter->ps_state == PS_STATE_SLEEP_CFM)
958				mwifiex_process_sleep_confirm_resp(adapter,
959								   skb->data,
960								   skb->len);
961
962			memcpy(cmd_buf, skb->data,
963			       min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER,
964				     skb->len));
965
966			dev_kfree_skb_any(skb);
967		} else {
968			adapter->cmd_resp_received = true;
969			adapter->curr_cmd->resp_skb = skb;
970		}
971		break;
972
973	case MWIFIEX_TYPE_EVENT:
974		dev_dbg(adapter->dev, "info: --- Rx: Event ---\n");
975		adapter->event_cause = *(u32 *) skb->data;
976
977		skb_pull(skb, MWIFIEX_EVENT_HEADER_LEN);
978
979		if ((skb->len > 0) && (skb->len  < MAX_EVENT_SIZE))
980			memcpy(adapter->event_body, skb->data, skb->len);
981
982		/* event cause has been saved to adapter->event_cause */
983		adapter->event_received = true;
984		adapter->event_skb = skb;
985
986		break;
987
988	default:
989		dev_err(adapter->dev, "unknown upload type %#x\n", upld_typ);
990		dev_kfree_skb_any(skb);
991		break;
992	}
993
994	return 0;
995}
996
997/*
998 * This function transfers received packets from card to driver, performing
999 * aggregation if required.
1000 *
1001 * For data received on control port, or if aggregation is disabled, the
1002 * received buffers are uploaded as separate packets. However, if aggregation
1003 * is enabled and required, the buffers are copied onto an aggregation buffer,
1004 * provided there is space left, processed and finally uploaded.
1005 */
1006static int mwifiex_sdio_card_to_host_mp_aggr(struct mwifiex_adapter *adapter,
1007					     struct sk_buff *skb, u8 port)
1008{
1009	struct sdio_mmc_card *card = adapter->card;
1010	s32 f_do_rx_aggr = 0;
1011	s32 f_do_rx_cur = 0;
1012	s32 f_aggr_cur = 0;
1013	struct sk_buff *skb_deaggr;
1014	u32 pind;
1015	u32 pkt_len, pkt_type = 0;
1016	u8 *curr_ptr;
1017	u32 rx_len = skb->len;
1018
1019	if (port == CTRL_PORT) {
1020		/* Read the command Resp without aggr */
1021		dev_dbg(adapter->dev, "info: %s: no aggregation for cmd "
1022			"response\n", __func__);
1023
1024		f_do_rx_cur = 1;
1025		goto rx_curr_single;
1026	}
1027
1028	if (!card->mpa_rx.enabled) {
1029		dev_dbg(adapter->dev, "info: %s: rx aggregation disabled\n",
1030			__func__);
1031
1032		f_do_rx_cur = 1;
1033		goto rx_curr_single;
1034	}
1035
1036	if (card->mp_rd_bitmap & (~((u16) CTRL_PORT_MASK))) {
1037		/* Some more data RX pending */
1038		dev_dbg(adapter->dev, "info: %s: not last packet\n", __func__);
1039
1040		if (MP_RX_AGGR_IN_PROGRESS(card)) {
1041			if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len)) {
1042				f_aggr_cur = 1;
1043			} else {
1044				/* No room in Aggr buf, do rx aggr now */
1045				f_do_rx_aggr = 1;
1046				f_do_rx_cur = 1;
1047			}
1048		} else {
1049			/* Rx aggr not in progress */
1050			f_aggr_cur = 1;
1051		}
1052
1053	} else {
1054		/* No more data RX pending */
1055		dev_dbg(adapter->dev, "info: %s: last packet\n", __func__);
1056
1057		if (MP_RX_AGGR_IN_PROGRESS(card)) {
1058			f_do_rx_aggr = 1;
1059			if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len))
1060				f_aggr_cur = 1;
1061			else
1062				/* No room in Aggr buf, do rx aggr now */
1063				f_do_rx_cur = 1;
1064		} else {
1065			f_do_rx_cur = 1;
1066		}
1067	}
1068
1069	if (f_aggr_cur) {
1070		dev_dbg(adapter->dev, "info: current packet aggregation\n");
1071		/* Curr pkt can be aggregated */
1072		MP_RX_AGGR_SETUP(card, skb, port);
1073
1074		if (MP_RX_AGGR_PKT_LIMIT_REACHED(card) ||
1075		    MP_RX_AGGR_PORT_LIMIT_REACHED(card)) {
1076			dev_dbg(adapter->dev, "info: %s: aggregated packet "
1077				"limit reached\n", __func__);
1078			/* No more pkts allowed in Aggr buf, rx it */
1079			f_do_rx_aggr = 1;
1080		}
1081	}
1082
1083	if (f_do_rx_aggr) {
1084		/* do aggr RX now */
1085		dev_dbg(adapter->dev, "info: do_rx_aggr: num of packets: %d\n",
1086			card->mpa_rx.pkt_cnt);
1087
1088		if (mwifiex_read_data_sync(adapter, card->mpa_rx.buf,
1089					   card->mpa_rx.buf_len,
1090					   (adapter->ioport | 0x1000 |
1091					    (card->mpa_rx.ports << 4)) +
1092					   card->mpa_rx.start_port, 1))
1093			goto error;
1094
1095		curr_ptr = card->mpa_rx.buf;
1096
1097		for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1098
1099			/* get curr PKT len & type */
1100			pkt_len = *(u16 *) &curr_ptr[0];
1101			pkt_type = *(u16 *) &curr_ptr[2];
1102
1103			/* copy pkt to deaggr buf */
1104			skb_deaggr = card->mpa_rx.skb_arr[pind];
1105
1106			if ((pkt_type == MWIFIEX_TYPE_DATA) && (pkt_len <=
1107					 card->mpa_rx.len_arr[pind])) {
1108
1109				memcpy(skb_deaggr->data, curr_ptr, pkt_len);
1110
1111				skb_trim(skb_deaggr, pkt_len);
1112
1113				/* Process de-aggr packet */
1114				mwifiex_decode_rx_packet(adapter, skb_deaggr,
1115							 pkt_type);
1116			} else {
1117				dev_err(adapter->dev, "wrong aggr pkt:"
1118					" type=%d len=%d max_len=%d\n",
1119					pkt_type, pkt_len,
1120					card->mpa_rx.len_arr[pind]);
1121				dev_kfree_skb_any(skb_deaggr);
1122			}
1123			curr_ptr += card->mpa_rx.len_arr[pind];
1124		}
1125		MP_RX_AGGR_BUF_RESET(card);
1126	}
1127
1128rx_curr_single:
1129	if (f_do_rx_cur) {
1130		dev_dbg(adapter->dev, "info: RX: port: %d, rx_len: %d\n",
1131			port, rx_len);
1132
1133		if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1134					      skb->data, skb->len,
1135					      adapter->ioport + port))
1136			goto error;
1137
1138		mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1139	}
1140
1141	return 0;
1142
1143error:
1144	if (MP_RX_AGGR_IN_PROGRESS(card)) {
1145		/* Multiport-aggregation transfer failed - cleanup */
1146		for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1147			/* copy pkt to deaggr buf */
1148			skb_deaggr = card->mpa_rx.skb_arr[pind];
1149			dev_kfree_skb_any(skb_deaggr);
1150		}
1151		MP_RX_AGGR_BUF_RESET(card);
1152	}
1153
1154	if (f_do_rx_cur)
1155		/* Single transfer pending. Free curr buff also */
1156		dev_kfree_skb_any(skb);
1157
1158	return -1;
1159}
1160
1161/*
1162 * This function checks the current interrupt status.
1163 *
1164 * The following interrupts are checked and handled by this function -
1165 *      - Data sent
1166 *      - Command sent
1167 *      - Packets received
1168 *
1169 * Since the firmware does not generate download ready interrupt if the
1170 * port updated is command port only, command sent interrupt checking
1171 * should be done manually, and for every SDIO interrupt.
1172 *
1173 * In case of Rx packets received, the packets are uploaded from card to
1174 * host and processed accordingly.
1175 */
1176static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
1177{
1178	struct sdio_mmc_card *card = adapter->card;
1179	int ret = 0;
1180	u8 sdio_ireg;
1181	struct sk_buff *skb;
1182	u8 port = CTRL_PORT;
1183	u32 len_reg_l, len_reg_u;
1184	u32 rx_blocks;
1185	u16 rx_len;
1186	unsigned long flags;
1187
1188	spin_lock_irqsave(&adapter->int_lock, flags);
1189	sdio_ireg = adapter->int_status;
1190	adapter->int_status = 0;
1191	spin_unlock_irqrestore(&adapter->int_lock, flags);
1192
1193	if (!sdio_ireg)
1194		return ret;
1195
1196	if (sdio_ireg & DN_LD_HOST_INT_STATUS) {
1197		card->mp_wr_bitmap = ((u16) card->mp_regs[WR_BITMAP_U]) << 8;
1198		card->mp_wr_bitmap |= (u16) card->mp_regs[WR_BITMAP_L];
1199		dev_dbg(adapter->dev, "int: DNLD: wr_bitmap=0x%04x\n",
1200			card->mp_wr_bitmap);
1201		if (adapter->data_sent &&
1202		    (card->mp_wr_bitmap & card->mp_data_port_mask)) {
1203			dev_dbg(adapter->dev,
1204				"info:  <--- Tx DONE Interrupt --->\n");
1205			adapter->data_sent = false;
1206		}
1207	}
1208
1209	/* As firmware will not generate download ready interrupt if the port
1210	   updated is command port only, cmd_sent should be done for any SDIO
1211	   interrupt. */
1212	if (adapter->cmd_sent) {
1213		/* Check if firmware has attach buffer at command port and
1214		   update just that in wr_bit_map. */
1215		card->mp_wr_bitmap |=
1216			(u16) card->mp_regs[WR_BITMAP_L] & CTRL_PORT_MASK;
1217		if (card->mp_wr_bitmap & CTRL_PORT_MASK)
1218			adapter->cmd_sent = false;
1219	}
1220
1221	dev_dbg(adapter->dev, "info: cmd_sent=%d data_sent=%d\n",
1222		adapter->cmd_sent, adapter->data_sent);
1223	if (sdio_ireg & UP_LD_HOST_INT_STATUS) {
1224		card->mp_rd_bitmap = ((u16) card->mp_regs[RD_BITMAP_U]) << 8;
1225		card->mp_rd_bitmap |= (u16) card->mp_regs[RD_BITMAP_L];
1226		dev_dbg(adapter->dev, "int: UPLD: rd_bitmap=0x%04x\n",
1227			card->mp_rd_bitmap);
1228
1229		while (true) {
1230			ret = mwifiex_get_rd_port(adapter, &port);
1231			if (ret) {
1232				dev_dbg(adapter->dev,
1233					"info: no more rd_port available\n");
1234				break;
1235			}
1236			len_reg_l = RD_LEN_P0_L + (port << 1);
1237			len_reg_u = RD_LEN_P0_U + (port << 1);
1238			rx_len = ((u16) card->mp_regs[len_reg_u]) << 8;
1239			rx_len |= (u16) card->mp_regs[len_reg_l];
1240			dev_dbg(adapter->dev, "info: RX: port=%d rx_len=%u\n",
1241				port, rx_len);
1242			rx_blocks =
1243				(rx_len + MWIFIEX_SDIO_BLOCK_SIZE -
1244				 1) / MWIFIEX_SDIO_BLOCK_SIZE;
1245			if (rx_len <= INTF_HEADER_LEN ||
1246			    (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1247			     MWIFIEX_RX_DATA_BUF_SIZE) {
1248				dev_err(adapter->dev, "invalid rx_len=%d\n",
1249					rx_len);
1250				return -1;
1251			}
1252			rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1253
1254			skb = dev_alloc_skb(rx_len);
1255
1256			if (!skb) {
1257				dev_err(adapter->dev, "%s: failed to alloc skb",
1258					__func__);
1259				return -1;
1260			}
1261
1262			skb_put(skb, rx_len);
1263
1264			dev_dbg(adapter->dev, "info: rx_len = %d skb->len = %d\n",
1265				rx_len, skb->len);
1266
1267			if (mwifiex_sdio_card_to_host_mp_aggr(adapter, skb,
1268							      port)) {
1269				u32 cr = 0;
1270
1271				dev_err(adapter->dev, "card_to_host_mpa failed:"
1272					" int status=%#x\n", sdio_ireg);
1273				if (mwifiex_read_reg(adapter,
1274						     CONFIGURATION_REG, &cr))
1275					dev_err(adapter->dev,
1276						"read CFG reg failed\n");
1277
1278				dev_dbg(adapter->dev,
1279					"info: CFG reg val = %d\n", cr);
1280				if (mwifiex_write_reg(adapter,
1281						      CONFIGURATION_REG,
1282						      (cr | 0x04)))
1283					dev_err(adapter->dev,
1284						"write CFG reg failed\n");
1285
1286				dev_dbg(adapter->dev, "info: write success\n");
1287				if (mwifiex_read_reg(adapter,
1288						     CONFIGURATION_REG, &cr))
1289					dev_err(adapter->dev,
1290						"read CFG reg failed\n");
1291
1292				dev_dbg(adapter->dev,
1293					"info: CFG reg val =%x\n", cr);
1294				return -1;
1295			}
1296		}
1297	}
1298
1299	return 0;
1300}
1301
1302/*
1303 * This function aggregates transmission buffers in driver and downloads
1304 * the aggregated packet to card.
1305 *
1306 * The individual packets are aggregated by copying into an aggregation
1307 * buffer and then downloaded to the card. Previous unsent packets in the
1308 * aggregation buffer are pre-copied first before new packets are added.
1309 * Aggregation is done till there is space left in the aggregation buffer,
1310 * or till new packets are available.
1311 *
1312 * The function will only download the packet to the card when aggregation
1313 * stops, otherwise it will just aggregate the packet in aggregation buffer
1314 * and return.
1315 */
1316static int mwifiex_host_to_card_mp_aggr(struct mwifiex_adapter *adapter,
1317					u8 *payload, u32 pkt_len, u8 port,
1318					u32 next_pkt_len)
1319{
1320	struct sdio_mmc_card *card = adapter->card;
1321	int ret = 0;
1322	s32 f_send_aggr_buf = 0;
1323	s32 f_send_cur_buf = 0;
1324	s32 f_precopy_cur_buf = 0;
1325	s32 f_postcopy_cur_buf = 0;
1326
1327	if ((!card->mpa_tx.enabled) || (port == CTRL_PORT)) {
1328		dev_dbg(adapter->dev, "info: %s: tx aggregation disabled\n",
1329			__func__);
1330
1331		f_send_cur_buf = 1;
1332		goto tx_curr_single;
1333	}
1334
1335	if (next_pkt_len) {
1336		/* More pkt in TX queue */
1337		dev_dbg(adapter->dev, "info: %s: more packets in queue.\n",
1338			__func__);
1339
1340		if (MP_TX_AGGR_IN_PROGRESS(card)) {
1341			if (!MP_TX_AGGR_PORT_LIMIT_REACHED(card) &&
1342			    MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len)) {
1343				f_precopy_cur_buf = 1;
1344
1345				if (!(card->mp_wr_bitmap &
1346				      (1 << card->curr_wr_port)) ||
1347				    !MP_TX_AGGR_BUF_HAS_ROOM(
1348					    card, pkt_len + next_pkt_len))
1349					f_send_aggr_buf = 1;
1350			} else {
1351				/* No room in Aggr buf, send it */
1352				f_send_aggr_buf = 1;
1353
1354				if (MP_TX_AGGR_PORT_LIMIT_REACHED(card) ||
1355				    !(card->mp_wr_bitmap &
1356				      (1 << card->curr_wr_port)))
1357					f_send_cur_buf = 1;
1358				else
1359					f_postcopy_cur_buf = 1;
1360			}
1361		} else {
1362			if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len) &&
1363			    (card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1364				f_precopy_cur_buf = 1;
1365			else
1366				f_send_cur_buf = 1;
1367		}
1368	} else {
1369		/* Last pkt in TX queue */
1370		dev_dbg(adapter->dev, "info: %s: Last packet in Tx Queue.\n",
1371			__func__);
1372
1373		if (MP_TX_AGGR_IN_PROGRESS(card)) {
1374			/* some packs in Aggr buf already */
1375			f_send_aggr_buf = 1;
1376
1377			if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len))
1378				f_precopy_cur_buf = 1;
1379			else
1380				/* No room in Aggr buf, send it */
1381				f_send_cur_buf = 1;
1382		} else {
1383			f_send_cur_buf = 1;
1384		}
1385	}
1386
1387	if (f_precopy_cur_buf) {
1388		dev_dbg(adapter->dev, "data: %s: precopy current buffer\n",
1389			__func__);
1390		MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1391
1392		if (MP_TX_AGGR_PKT_LIMIT_REACHED(card) ||
1393		    MP_TX_AGGR_PORT_LIMIT_REACHED(card))
1394			/* No more pkts allowed in Aggr buf, send it */
1395			f_send_aggr_buf = 1;
1396	}
1397
1398	if (f_send_aggr_buf) {
1399		dev_dbg(adapter->dev, "data: %s: send aggr buffer: %d %d\n",
1400			__func__,
1401				card->mpa_tx.start_port, card->mpa_tx.ports);
1402		ret = mwifiex_write_data_to_card(adapter, card->mpa_tx.buf,
1403						 card->mpa_tx.buf_len,
1404						 (adapter->ioport | 0x1000 |
1405						 (card->mpa_tx.ports << 4)) +
1406						  card->mpa_tx.start_port);
1407
1408		MP_TX_AGGR_BUF_RESET(card);
1409	}
1410
1411tx_curr_single:
1412	if (f_send_cur_buf) {
1413		dev_dbg(adapter->dev, "data: %s: send current buffer %d\n",
1414			__func__, port);
1415		ret = mwifiex_write_data_to_card(adapter, payload, pkt_len,
1416						 adapter->ioport + port);
1417	}
1418
1419	if (f_postcopy_cur_buf) {
1420		dev_dbg(adapter->dev, "data: %s: postcopy current buffer\n",
1421			__func__);
1422		MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1423	}
1424
1425	return ret;
1426}
1427
1428/*
1429 * This function downloads data from driver to card.
1430 *
1431 * Both commands and data packets are transferred to the card by this
1432 * function.
1433 *
1434 * This function adds the SDIO specific header to the front of the buffer
1435 * before transferring. The header contains the length of the packet and
1436 * the type. The firmware handles the packets based upon this set type.
1437 */
1438static int mwifiex_sdio_host_to_card(struct mwifiex_adapter *adapter,
1439				     u8 type, struct sk_buff *skb,
1440				     struct mwifiex_tx_param *tx_param)
1441{
1442	struct sdio_mmc_card *card = adapter->card;
1443	int ret;
1444	u32 buf_block_len;
1445	u32 blk_size;
1446	u8 port = CTRL_PORT;
1447	u8 *payload = (u8 *)skb->data;
1448	u32 pkt_len = skb->len;
1449
1450	/* Allocate buffer and copy payload */
1451	blk_size = MWIFIEX_SDIO_BLOCK_SIZE;
1452	buf_block_len = (pkt_len + blk_size - 1) / blk_size;
1453	*(u16 *) &payload[0] = (u16) pkt_len;
1454	*(u16 *) &payload[2] = type;
1455
1456	/*
1457	 * This is SDIO specific header
1458	 *  u16 length,
1459	 *  u16 type (MWIFIEX_TYPE_DATA = 0, MWIFIEX_TYPE_CMD = 1,
1460	 *  MWIFIEX_TYPE_EVENT = 3)
1461	 */
1462	if (type == MWIFIEX_TYPE_DATA) {
1463		ret = mwifiex_get_wr_port_data(adapter, &port);
1464		if (ret) {
1465			dev_err(adapter->dev, "%s: no wr_port available\n",
1466				__func__);
1467			return ret;
1468		}
1469	} else {
1470		adapter->cmd_sent = true;
1471		/* Type must be MWIFIEX_TYPE_CMD */
1472
1473		if (pkt_len <= INTF_HEADER_LEN ||
1474		    pkt_len > MWIFIEX_UPLD_SIZE)
1475			dev_err(adapter->dev, "%s: payload=%p, nb=%d\n",
1476				__func__, payload, pkt_len);
1477	}
1478
1479	/* Transfer data to card */
1480	pkt_len = buf_block_len * blk_size;
1481
1482	if (tx_param)
1483		ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1484						   port, tx_param->next_pkt_len
1485						   );
1486	else
1487		ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1488						   port, 0);
1489
1490	if (ret) {
1491		if (type == MWIFIEX_TYPE_CMD)
1492			adapter->cmd_sent = false;
1493		if (type == MWIFIEX_TYPE_DATA)
1494			adapter->data_sent = false;
1495	} else {
1496		if (type == MWIFIEX_TYPE_DATA) {
1497			if (!(card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1498				adapter->data_sent = true;
1499			else
1500				adapter->data_sent = false;
1501		}
1502	}
1503
1504	return ret;
1505}
1506
1507/*
1508 * This function allocates the MPA Tx and Rx buffers.
1509 */
1510static int mwifiex_alloc_sdio_mpa_buffers(struct mwifiex_adapter *adapter,
1511				   u32 mpa_tx_buf_size, u32 mpa_rx_buf_size)
1512{
1513	struct sdio_mmc_card *card = adapter->card;
1514	int ret = 0;
1515
1516	card->mpa_tx.buf = kzalloc(mpa_tx_buf_size, GFP_KERNEL);
1517	if (!card->mpa_tx.buf) {
1518		dev_err(adapter->dev, "could not alloc buffer for MP-A TX\n");
1519		ret = -1;
1520		goto error;
1521	}
1522
1523	card->mpa_tx.buf_size = mpa_tx_buf_size;
1524
1525	card->mpa_rx.buf = kzalloc(mpa_rx_buf_size, GFP_KERNEL);
1526	if (!card->mpa_rx.buf) {
1527		dev_err(adapter->dev, "could not alloc buffer for MP-A RX\n");
1528		ret = -1;
1529		goto error;
1530	}
1531
1532	card->mpa_rx.buf_size = mpa_rx_buf_size;
1533
1534error:
1535	if (ret) {
1536		kfree(card->mpa_tx.buf);
1537		kfree(card->mpa_rx.buf);
1538	}
1539
1540	return ret;
1541}
1542
1543/*
1544 * This function unregisters the SDIO device.
1545 *
1546 * The SDIO IRQ is released, the function is disabled and driver
1547 * data is set to null.
1548 */
1549static void
1550mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
1551{
1552	struct sdio_mmc_card *card = adapter->card;
1553
1554	if (adapter->card) {
1555		/* Release the SDIO IRQ */
1556		sdio_claim_host(card->func);
1557		sdio_release_irq(card->func);
1558		sdio_disable_func(card->func);
1559		sdio_release_host(card->func);
1560		sdio_set_drvdata(card->func, NULL);
1561	}
1562}
1563
1564/*
1565 * This function registers the SDIO device.
1566 *
1567 * SDIO IRQ is claimed, block size is set and driver data is initialized.
1568 */
1569static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
1570{
1571	int ret = 0;
1572	struct sdio_mmc_card *card = adapter->card;
1573	struct sdio_func *func = card->func;
1574
1575	/* save adapter pointer in card */
1576	card->adapter = adapter;
1577
1578	sdio_claim_host(func);
1579
1580	/* Request the SDIO IRQ */
1581	ret = sdio_claim_irq(func, mwifiex_sdio_interrupt);
1582	if (ret) {
1583		pr_err("claim irq failed: ret=%d\n", ret);
1584		goto disable_func;
1585	}
1586
1587	/* Set block size */
1588	ret = sdio_set_block_size(card->func, MWIFIEX_SDIO_BLOCK_SIZE);
1589	if (ret) {
1590		pr_err("cannot set SDIO block size\n");
1591		ret = -1;
1592		goto release_irq;
1593	}
1594
1595	sdio_release_host(func);
1596	sdio_set_drvdata(func, card);
1597
1598	adapter->dev = &func->dev;
1599
1600	switch (func->device) {
1601	case SDIO_DEVICE_ID_MARVELL_8797:
1602		strcpy(adapter->fw_name, SD8797_DEFAULT_FW_NAME);
1603		break;
1604	case SDIO_DEVICE_ID_MARVELL_8787:
1605	default:
1606		strcpy(adapter->fw_name, SD8787_DEFAULT_FW_NAME);
1607		break;
1608	}
1609
1610	return 0;
1611
1612release_irq:
1613	sdio_release_irq(func);
1614disable_func:
1615	sdio_disable_func(func);
1616	sdio_release_host(func);
1617	adapter->card = NULL;
1618
1619	return -1;
1620}
1621
1622/*
1623 * This function initializes the SDIO driver.
1624 *
1625 * The following initializations steps are followed -
1626 *      - Read the Host interrupt status register to acknowledge
1627 *        the first interrupt got from bootloader
1628 *      - Disable host interrupt mask register
1629 *      - Get SDIO port
1630 *      - Initialize SDIO variables in card
1631 *      - Allocate MP registers
1632 *      - Allocate MPA Tx and Rx buffers
1633 */
1634static int mwifiex_init_sdio(struct mwifiex_adapter *adapter)
1635{
1636	struct sdio_mmc_card *card = adapter->card;
1637	int ret;
1638	u32 sdio_ireg;
1639
1640	/*
1641	 * Read the HOST_INT_STATUS_REG for ACK the first interrupt got
1642	 * from the bootloader. If we don't do this we get a interrupt
1643	 * as soon as we register the irq.
1644	 */
1645	mwifiex_read_reg(adapter, HOST_INTSTATUS_REG, &sdio_ireg);
1646
1647	/* Disable host interrupt mask register for SDIO */
1648	mwifiex_sdio_disable_host_int(adapter);
1649
1650	/* Get SDIO ioport */
1651	mwifiex_init_sdio_ioport(adapter);
1652
1653	/* Initialize SDIO variables in card */
1654	card->mp_rd_bitmap = 0;
1655	card->mp_wr_bitmap = 0;
1656	card->curr_rd_port = 1;
1657	card->curr_wr_port = 1;
1658
1659	card->mp_data_port_mask = DATA_PORT_MASK;
1660
1661	card->mpa_tx.buf_len = 0;
1662	card->mpa_tx.pkt_cnt = 0;
1663	card->mpa_tx.start_port = 0;
1664
1665	card->mpa_tx.enabled = 1;
1666	card->mpa_tx.pkt_aggr_limit = SDIO_MP_AGGR_DEF_PKT_LIMIT;
1667
1668	card->mpa_rx.buf_len = 0;
1669	card->mpa_rx.pkt_cnt = 0;
1670	card->mpa_rx.start_port = 0;
1671
1672	card->mpa_rx.enabled = 1;
1673	card->mpa_rx.pkt_aggr_limit = SDIO_MP_AGGR_DEF_PKT_LIMIT;
1674
1675	/* Allocate buffers for SDIO MP-A */
1676	card->mp_regs = kzalloc(MAX_MP_REGS, GFP_KERNEL);
1677	if (!card->mp_regs) {
1678		dev_err(adapter->dev, "failed to alloc mp_regs\n");
1679		return -ENOMEM;
1680	}
1681
1682	ret = mwifiex_alloc_sdio_mpa_buffers(adapter,
1683					     SDIO_MP_TX_AGGR_DEF_BUF_SIZE,
1684					     SDIO_MP_RX_AGGR_DEF_BUF_SIZE);
1685	if (ret) {
1686		dev_err(adapter->dev, "failed to alloc sdio mp-a buffers\n");
1687		kfree(card->mp_regs);
1688		return -1;
1689	}
1690
1691	return ret;
1692}
1693
1694/*
1695 * This function resets the MPA Tx and Rx buffers.
1696 */
1697static void mwifiex_cleanup_mpa_buf(struct mwifiex_adapter *adapter)
1698{
1699	struct sdio_mmc_card *card = adapter->card;
1700
1701	MP_TX_AGGR_BUF_RESET(card);
1702	MP_RX_AGGR_BUF_RESET(card);
1703}
1704
1705/*
1706 * This function cleans up the allocated card buffers.
1707 *
1708 * The following are freed by this function -
1709 *      - MP registers
1710 *      - MPA Tx buffer
1711 *      - MPA Rx buffer
1712 */
1713static void mwifiex_cleanup_sdio(struct mwifiex_adapter *adapter)
1714{
1715	struct sdio_mmc_card *card = adapter->card;
1716
1717	kfree(card->mp_regs);
1718	kfree(card->mpa_tx.buf);
1719	kfree(card->mpa_rx.buf);
1720}
1721
1722/*
1723 * This function updates the MP end port in card.
1724 */
1725static void
1726mwifiex_update_mp_end_port(struct mwifiex_adapter *adapter, u16 port)
1727{
1728	struct sdio_mmc_card *card = adapter->card;
1729	int i;
1730
1731	card->mp_end_port = port;
1732
1733	card->mp_data_port_mask = DATA_PORT_MASK;
1734
1735	for (i = 1; i <= MAX_PORT - card->mp_end_port; i++)
1736		card->mp_data_port_mask &= ~(1 << (MAX_PORT - i));
1737
1738	card->curr_wr_port = 1;
1739
1740	dev_dbg(adapter->dev, "cmd: mp_end_port %d, data port mask 0x%x\n",
1741		port, card->mp_data_port_mask);
1742}
1743
1744static struct mwifiex_if_ops sdio_ops = {
1745	.init_if = mwifiex_init_sdio,
1746	.cleanup_if = mwifiex_cleanup_sdio,
1747	.check_fw_status = mwifiex_check_fw_status,
1748	.prog_fw = mwifiex_prog_fw_w_helper,
1749	.register_dev = mwifiex_register_dev,
1750	.unregister_dev = mwifiex_unregister_dev,
1751	.enable_int = mwifiex_sdio_enable_host_int,
1752	.process_int_status = mwifiex_process_int_status,
1753	.host_to_card = mwifiex_sdio_host_to_card,
1754	.wakeup = mwifiex_pm_wakeup_card,
1755	.wakeup_complete = mwifiex_pm_wakeup_card_complete,
1756
1757	/* SDIO specific */
1758	.update_mp_end_port = mwifiex_update_mp_end_port,
1759	.cleanup_mpa_buf = mwifiex_cleanup_mpa_buf,
1760	.cmdrsp_complete = mwifiex_sdio_cmdrsp_complete,
1761	.event_complete = mwifiex_sdio_event_complete,
1762};
1763
1764/*
1765 * This function initializes the SDIO driver.
1766 *
1767 * This initiates the semaphore and registers the device with
1768 * SDIO bus.
1769 */
1770static int
1771mwifiex_sdio_init_module(void)
1772{
1773	sema_init(&add_remove_card_sem, 1);
1774
1775	/* Clear the flag in case user removes the card. */
1776	user_rmmod = 0;
1777
1778	return sdio_register_driver(&mwifiex_sdio);
1779}
1780
1781/*
1782 * This function cleans up the SDIO driver.
1783 *
1784 * The following major steps are followed for cleanup -
1785 *      - Resume the device if its suspended
1786 *      - Disconnect the device if connected
1787 *      - Shutdown the firmware
1788 *      - Unregister the device from SDIO bus.
1789 */
1790static void
1791mwifiex_sdio_cleanup_module(void)
1792{
1793	if (!down_interruptible(&add_remove_card_sem))
1794		up(&add_remove_card_sem);
1795
1796	/* Set the flag as user is removing this module. */
1797	user_rmmod = 1;
1798
1799	sdio_unregister_driver(&mwifiex_sdio);
1800}
1801
1802module_init(mwifiex_sdio_init_module);
1803module_exit(mwifiex_sdio_cleanup_module);
1804
1805MODULE_AUTHOR("Marvell International Ltd.");
1806MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION);
1807MODULE_VERSION(SDIO_VERSION);
1808MODULE_LICENSE("GPL v2");
1809MODULE_FIRMWARE(SD8787_DEFAULT_FW_NAME);
1810MODULE_FIRMWARE(SD8797_DEFAULT_FW_NAME);
1811