xref: /linux/drivers/net/wireless/marvell/mwifiex/sdio.c (revision d96e39270ba5b81dee63f7a1bdaad01484c94e08)
1277b024eSKalle Valo /*
2277b024eSKalle Valo  * Marvell Wireless LAN device driver: SDIO specific handling
3277b024eSKalle Valo  *
4277b024eSKalle Valo  * Copyright (C) 2011-2014, Marvell International Ltd.
5277b024eSKalle Valo  *
6277b024eSKalle Valo  * This software file (the "File") is distributed by Marvell International
7277b024eSKalle Valo  * Ltd. under the terms of the GNU General Public License Version 2, June 1991
8277b024eSKalle Valo  * (the "License").  You may use, redistribute and/or modify this File in
9277b024eSKalle Valo  * accordance with the terms and conditions of the License, a copy of which
10277b024eSKalle Valo  * is available by writing to the Free Software Foundation, Inc.,
11277b024eSKalle Valo  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
12277b024eSKalle Valo  * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
13277b024eSKalle Valo  *
14277b024eSKalle Valo  * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
15277b024eSKalle Valo  * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
16277b024eSKalle Valo  * ARE EXPRESSLY DISCLAIMED.  The License provides additional details about
17277b024eSKalle Valo  * this warranty disclaimer.
18277b024eSKalle Valo  */
19277b024eSKalle Valo 
20277b024eSKalle Valo #include <linux/firmware.h>
21277b024eSKalle Valo 
22277b024eSKalle Valo #include "decl.h"
23277b024eSKalle Valo #include "ioctl.h"
24277b024eSKalle Valo #include "util.h"
25277b024eSKalle Valo #include "fw.h"
26277b024eSKalle Valo #include "main.h"
27277b024eSKalle Valo #include "wmm.h"
28277b024eSKalle Valo #include "11n.h"
29277b024eSKalle Valo #include "sdio.h"
30277b024eSKalle Valo 
31277b024eSKalle Valo 
32277b024eSKalle Valo #define SDIO_VERSION	"1.0"
33277b024eSKalle Valo 
34277b024eSKalle Valo /* The mwifiex_sdio_remove() callback function is called when
35277b024eSKalle Valo  * user removes this module from kernel space or ejects
36277b024eSKalle Valo  * the card from the slot. The driver handles these 2 cases
37277b024eSKalle Valo  * differently.
38277b024eSKalle Valo  * If the user is removing the module, the few commands (FUNC_SHUTDOWN,
39277b024eSKalle Valo  * HS_CANCEL etc.) are sent to the firmware.
40277b024eSKalle Valo  * If the card is removed, there is no need to send these command.
41277b024eSKalle Valo  *
42277b024eSKalle Valo  * The variable 'user_rmmod' is used to distinguish these two
43277b024eSKalle Valo  * scenarios. This flag is initialized as FALSE in case the card
44277b024eSKalle Valo  * is removed, and will be set to TRUE for module removal when
45277b024eSKalle Valo  * module_exit function is called.
46277b024eSKalle Valo  */
47277b024eSKalle Valo static u8 user_rmmod;
48277b024eSKalle Valo 
49277b024eSKalle Valo static struct mwifiex_if_ops sdio_ops;
50277b024eSKalle Valo static unsigned long iface_work_flags;
51277b024eSKalle Valo 
52277b024eSKalle Valo static struct memory_type_mapping generic_mem_type_map[] = {
53277b024eSKalle Valo 	{"DUMP", NULL, 0, 0xDD},
54277b024eSKalle Valo };
55277b024eSKalle Valo 
56277b024eSKalle Valo static struct memory_type_mapping mem_type_mapping_tbl[] = {
57277b024eSKalle Valo 	{"ITCM", NULL, 0, 0xF0},
58277b024eSKalle Valo 	{"DTCM", NULL, 0, 0xF1},
59277b024eSKalle Valo 	{"SQRAM", NULL, 0, 0xF2},
60277b024eSKalle Valo 	{"APU", NULL, 0, 0xF3},
61277b024eSKalle Valo 	{"CIU", NULL, 0, 0xF4},
62277b024eSKalle Valo 	{"ICU", NULL, 0, 0xF5},
63277b024eSKalle Valo 	{"MAC", NULL, 0, 0xF6},
64277b024eSKalle Valo 	{"EXT7", NULL, 0, 0xF7},
65277b024eSKalle Valo 	{"EXT8", NULL, 0, 0xF8},
66277b024eSKalle Valo 	{"EXT9", NULL, 0, 0xF9},
67277b024eSKalle Valo 	{"EXT10", NULL, 0, 0xFA},
68277b024eSKalle Valo 	{"EXT11", NULL, 0, 0xFB},
69277b024eSKalle Valo 	{"EXT12", NULL, 0, 0xFC},
70277b024eSKalle Valo 	{"EXT13", NULL, 0, 0xFD},
71277b024eSKalle Valo 	{"EXTLAST", NULL, 0, 0xFE},
72277b024eSKalle Valo };
73277b024eSKalle Valo 
74ce4f6f0cSXinming Hu static const struct of_device_id mwifiex_sdio_of_match_table[] = {
75ce4f6f0cSXinming Hu 	{ .compatible = "marvell,sd8897" },
76ce4f6f0cSXinming Hu 	{ .compatible = "marvell,sd8997" },
77ce4f6f0cSXinming Hu 	{ }
78ce4f6f0cSXinming Hu };
79ce4f6f0cSXinming Hu 
80ce4f6f0cSXinming Hu /* This function parse device tree node using mmc subnode devicetree API.
81ce4f6f0cSXinming Hu  * The device node is saved in card->plt_of_node.
82ce4f6f0cSXinming Hu  * if the device tree node exist and include interrupts attributes, this
83ce4f6f0cSXinming Hu  * function will also request platform specific wakeup interrupt.
84ce4f6f0cSXinming Hu  */
85853402a0SRajat Jain static int mwifiex_sdio_probe_of(struct device *dev)
86ce4f6f0cSXinming Hu {
876f49208fSJavier Martinez Canillas 	if (!of_match_node(mwifiex_sdio_of_match_table, dev->of_node)) {
885e94913fSJavier Martinez Canillas 		dev_err(dev, "required compatible string missing\n");
895e94913fSJavier Martinez Canillas 		return -EINVAL;
90ce4f6f0cSXinming Hu 	}
91ce4f6f0cSXinming Hu 
92ce4f6f0cSXinming Hu 	return 0;
93ce4f6f0cSXinming Hu }
94ce4f6f0cSXinming Hu 
95277b024eSKalle Valo /*
96277b024eSKalle Valo  * SDIO probe.
97277b024eSKalle Valo  *
98277b024eSKalle Valo  * This function probes an mwifiex device and registers it. It allocates
99277b024eSKalle Valo  * the card structure, enables SDIO function number and initiates the
100277b024eSKalle Valo  * device registration and initialization procedure by adding a logical
101277b024eSKalle Valo  * interface.
102277b024eSKalle Valo  */
103277b024eSKalle Valo static int
104277b024eSKalle Valo mwifiex_sdio_probe(struct sdio_func *func, const struct sdio_device_id *id)
105277b024eSKalle Valo {
106277b024eSKalle Valo 	int ret;
107277b024eSKalle Valo 	struct sdio_mmc_card *card = NULL;
108277b024eSKalle Valo 
109277b024eSKalle Valo 	pr_debug("info: vendor=0x%4.04X device=0x%4.04X class=%d function=%d\n",
110277b024eSKalle Valo 		 func->vendor, func->device, func->class, func->num);
111277b024eSKalle Valo 
11266b9c182SBrian Norris 	card = devm_kzalloc(&func->dev, sizeof(*card), GFP_KERNEL);
113277b024eSKalle Valo 	if (!card)
114277b024eSKalle Valo 		return -ENOMEM;
115277b024eSKalle Valo 
1164a79aa17SBrian Norris 	init_completion(&card->fw_done);
1174a79aa17SBrian Norris 
118277b024eSKalle Valo 	card->func = func;
119277b024eSKalle Valo 	card->device_id = id;
120277b024eSKalle Valo 
121277b024eSKalle Valo 	func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
122277b024eSKalle Valo 
123277b024eSKalle Valo 	if (id->driver_data) {
124277b024eSKalle Valo 		struct mwifiex_sdio_device *data = (void *)id->driver_data;
125277b024eSKalle Valo 
126277b024eSKalle Valo 		card->firmware = data->firmware;
127277b024eSKalle Valo 		card->reg = data->reg;
128277b024eSKalle Valo 		card->max_ports = data->max_ports;
129277b024eSKalle Valo 		card->mp_agg_pkt_limit = data->mp_agg_pkt_limit;
130277b024eSKalle Valo 		card->supports_sdio_new_mode = data->supports_sdio_new_mode;
131277b024eSKalle Valo 		card->has_control_mask = data->has_control_mask;
132277b024eSKalle Valo 		card->tx_buf_size = data->tx_buf_size;
133277b024eSKalle Valo 		card->mp_tx_agg_buf_size = data->mp_tx_agg_buf_size;
134277b024eSKalle Valo 		card->mp_rx_agg_buf_size = data->mp_rx_agg_buf_size;
135277b024eSKalle Valo 		card->can_dump_fw = data->can_dump_fw;
136277b024eSKalle Valo 		card->fw_dump_enh = data->fw_dump_enh;
137277b024eSKalle Valo 		card->can_auto_tdls = data->can_auto_tdls;
138277b024eSKalle Valo 		card->can_ext_scan = data->can_ext_scan;
139277b024eSKalle Valo 	}
140277b024eSKalle Valo 
141277b024eSKalle Valo 	sdio_claim_host(func);
142277b024eSKalle Valo 	ret = sdio_enable_func(func);
143277b024eSKalle Valo 	sdio_release_host(func);
144277b024eSKalle Valo 
145277b024eSKalle Valo 	if (ret) {
146d3f04eceSJavier Martinez Canillas 		dev_err(&func->dev, "failed to enable function\n");
14766b9c182SBrian Norris 		return ret;
148277b024eSKalle Valo 	}
149277b024eSKalle Valo 
150ce4f6f0cSXinming Hu 	/* device tree node parsing and platform specific configuration*/
151213d9421SJavier Martinez Canillas 	if (func->dev.of_node) {
152853402a0SRajat Jain 		ret = mwifiex_sdio_probe_of(&func->dev);
153853402a0SRajat Jain 		if (ret)
154213d9421SJavier Martinez Canillas 			goto err_disable;
155213d9421SJavier Martinez Canillas 	}
156ce4f6f0cSXinming Hu 
1574a79aa17SBrian Norris 	ret = mwifiex_add_card(card, &card->fw_done, &sdio_ops,
1582e02b581SRajat Jain 			       MWIFIEX_SDIO, &func->dev);
159032e0f54SJavier Martinez Canillas 	if (ret) {
160d3f04eceSJavier Martinez Canillas 		dev_err(&func->dev, "add card failed\n");
161a82f65aaSJavier Martinez Canillas 		goto err_disable;
162a82f65aaSJavier Martinez Canillas 	}
163a82f65aaSJavier Martinez Canillas 
164a82f65aaSJavier Martinez Canillas 	return 0;
165a82f65aaSJavier Martinez Canillas 
166a82f65aaSJavier Martinez Canillas err_disable:
167277b024eSKalle Valo 	sdio_claim_host(func);
168032e0f54SJavier Martinez Canillas 	sdio_disable_func(func);
169277b024eSKalle Valo 	sdio_release_host(func);
170277b024eSKalle Valo 
171277b024eSKalle Valo 	return ret;
172277b024eSKalle Valo }
173277b024eSKalle Valo 
174277b024eSKalle Valo /*
175277b024eSKalle Valo  * SDIO resume.
176277b024eSKalle Valo  *
177277b024eSKalle Valo  * Kernel needs to suspend all functions separately. Therefore all
178277b024eSKalle Valo  * registered functions must have drivers with suspend and resume
179277b024eSKalle Valo  * methods. Failing that the kernel simply removes the whole card.
180277b024eSKalle Valo  *
181277b024eSKalle Valo  * If already not resumed, this function turns on the traffic and
182277b024eSKalle Valo  * sends a host sleep cancel request to the firmware.
183277b024eSKalle Valo  */
184277b024eSKalle Valo static int mwifiex_sdio_resume(struct device *dev)
185277b024eSKalle Valo {
186277b024eSKalle Valo 	struct sdio_func *func = dev_to_sdio_func(dev);
187277b024eSKalle Valo 	struct sdio_mmc_card *card;
188277b024eSKalle Valo 	struct mwifiex_adapter *adapter;
189277b024eSKalle Valo 	mmc_pm_flag_t pm_flag = 0;
190277b024eSKalle Valo 
191277b024eSKalle Valo 	pm_flag = sdio_get_host_pm_caps(func);
192277b024eSKalle Valo 	card = sdio_get_drvdata(func);
193277b024eSKalle Valo 	if (!card || !card->adapter) {
1946caf34cbSBrian Norris 		dev_err(dev, "resume: invalid card or adapter\n");
195277b024eSKalle Valo 		return 0;
196277b024eSKalle Valo 	}
197277b024eSKalle Valo 
198277b024eSKalle Valo 	adapter = card->adapter;
199277b024eSKalle Valo 
200277b024eSKalle Valo 	if (!adapter->is_suspended) {
201277b024eSKalle Valo 		mwifiex_dbg(adapter, WARN,
202277b024eSKalle Valo 			    "device already resumed\n");
203277b024eSKalle Valo 		return 0;
204277b024eSKalle Valo 	}
205277b024eSKalle Valo 
206277b024eSKalle Valo 	adapter->is_suspended = false;
207277b024eSKalle Valo 
208277b024eSKalle Valo 	/* Disable Host Sleep */
209277b024eSKalle Valo 	mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
210a92277bcSAmitkumar Karwar 			  MWIFIEX_SYNC_CMD);
211277b024eSKalle Valo 
212853402a0SRajat Jain 	mwifiex_disable_wake(adapter);
213ce4f6f0cSXinming Hu 
214277b024eSKalle Valo 	return 0;
215277b024eSKalle Valo }
216277b024eSKalle Valo 
217277b024eSKalle Valo /*
218277b024eSKalle Valo  * SDIO remove.
219277b024eSKalle Valo  *
220277b024eSKalle Valo  * This function removes the interface and frees up the card structure.
221277b024eSKalle Valo  */
222277b024eSKalle Valo static void
223277b024eSKalle Valo mwifiex_sdio_remove(struct sdio_func *func)
224277b024eSKalle Valo {
225277b024eSKalle Valo 	struct sdio_mmc_card *card;
226277b024eSKalle Valo 	struct mwifiex_adapter *adapter;
227277b024eSKalle Valo 	struct mwifiex_private *priv;
228277b024eSKalle Valo 
229277b024eSKalle Valo 	card = sdio_get_drvdata(func);
230277b024eSKalle Valo 	if (!card)
231277b024eSKalle Valo 		return;
232277b024eSKalle Valo 
2334a79aa17SBrian Norris 	wait_for_completion(&card->fw_done);
2344a79aa17SBrian Norris 
235277b024eSKalle Valo 	adapter = card->adapter;
236277b024eSKalle Valo 	if (!adapter || !adapter->priv_num)
237277b024eSKalle Valo 		return;
238277b024eSKalle Valo 
239277b024eSKalle Valo 	mwifiex_dbg(adapter, INFO, "info: SDIO func num=%d\n", func->num);
240277b024eSKalle Valo 
241cf5383b0SXinming Hu 	if (user_rmmod && !adapter->mfg_mode) {
242277b024eSKalle Valo 		mwifiex_deauthenticate_all(adapter);
243277b024eSKalle Valo 
244277b024eSKalle Valo 		priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
245277b024eSKalle Valo 		mwifiex_disable_auto_ds(priv);
246277b024eSKalle Valo 		mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
247277b024eSKalle Valo 	}
248277b024eSKalle Valo 
2494a79aa17SBrian Norris 	mwifiex_remove_card(adapter);
250277b024eSKalle Valo }
251277b024eSKalle Valo 
252277b024eSKalle Valo /*
253277b024eSKalle Valo  * SDIO suspend.
254277b024eSKalle Valo  *
255277b024eSKalle Valo  * Kernel needs to suspend all functions separately. Therefore all
256277b024eSKalle Valo  * registered functions must have drivers with suspend and resume
257277b024eSKalle Valo  * methods. Failing that the kernel simply removes the whole card.
258277b024eSKalle Valo  *
259277b024eSKalle Valo  * If already not suspended, this function allocates and sends a host
260277b024eSKalle Valo  * sleep activate request to the firmware and turns off the traffic.
261277b024eSKalle Valo  */
262277b024eSKalle Valo static int mwifiex_sdio_suspend(struct device *dev)
263277b024eSKalle Valo {
264277b024eSKalle Valo 	struct sdio_func *func = dev_to_sdio_func(dev);
265277b024eSKalle Valo 	struct sdio_mmc_card *card;
266277b024eSKalle Valo 	struct mwifiex_adapter *adapter;
267277b024eSKalle Valo 	mmc_pm_flag_t pm_flag = 0;
268277b024eSKalle Valo 	int ret = 0;
269277b024eSKalle Valo 
270277b024eSKalle Valo 	pm_flag = sdio_get_host_pm_caps(func);
271277b024eSKalle Valo 	pr_debug("cmd: %s: suspend: PM flag = 0x%x\n",
272277b024eSKalle Valo 		 sdio_func_id(func), pm_flag);
273277b024eSKalle Valo 	if (!(pm_flag & MMC_PM_KEEP_POWER)) {
2746caf34cbSBrian Norris 		dev_err(dev, "%s: cannot remain alive while host is"
275277b024eSKalle Valo 			" suspended\n", sdio_func_id(func));
276277b024eSKalle Valo 		return -ENOSYS;
277277b024eSKalle Valo 	}
278277b024eSKalle Valo 
279277b024eSKalle Valo 	card = sdio_get_drvdata(func);
280b42dbb27SBrian Norris 	if (!card) {
281b42dbb27SBrian Norris 		dev_err(dev, "suspend: invalid card\n");
282277b024eSKalle Valo 		return 0;
283277b024eSKalle Valo 	}
284277b024eSKalle Valo 
285b42dbb27SBrian Norris 	/* Might still be loading firmware */
286b42dbb27SBrian Norris 	wait_for_completion(&card->fw_done);
287b42dbb27SBrian Norris 
288277b024eSKalle Valo 	adapter = card->adapter;
289b42dbb27SBrian Norris 	if (!adapter) {
290b42dbb27SBrian Norris 		dev_err(dev, "adapter is not valid\n");
291b42dbb27SBrian Norris 		return 0;
292b42dbb27SBrian Norris 	}
293b42dbb27SBrian Norris 
294853402a0SRajat Jain 	mwifiex_enable_wake(adapter);
295ce4f6f0cSXinming Hu 
296277b024eSKalle Valo 	/* Enable the Host Sleep */
297277b024eSKalle Valo 	if (!mwifiex_enable_hs(adapter)) {
298277b024eSKalle Valo 		mwifiex_dbg(adapter, ERROR,
299277b024eSKalle Valo 			    "cmd: failed to suspend\n");
300277b024eSKalle Valo 		adapter->hs_enabling = false;
301*d96e3927SBrian Norris 		mwifiex_disable_wake(adapter);
302277b024eSKalle Valo 		return -EFAULT;
303277b024eSKalle Valo 	}
304277b024eSKalle Valo 
305277b024eSKalle Valo 	mwifiex_dbg(adapter, INFO,
306277b024eSKalle Valo 		    "cmd: suspend with MMC_PM_KEEP_POWER\n");
307277b024eSKalle Valo 	ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
308277b024eSKalle Valo 
309277b024eSKalle Valo 	/* Indicate device suspended */
310277b024eSKalle Valo 	adapter->is_suspended = true;
311277b024eSKalle Valo 	adapter->hs_enabling = false;
312277b024eSKalle Valo 
313277b024eSKalle Valo 	return ret;
314277b024eSKalle Valo }
315277b024eSKalle Valo 
316277b024eSKalle Valo /* Device ID for SD8786 */
317277b024eSKalle Valo #define SDIO_DEVICE_ID_MARVELL_8786   (0x9116)
318277b024eSKalle Valo /* Device ID for SD8787 */
319277b024eSKalle Valo #define SDIO_DEVICE_ID_MARVELL_8787   (0x9119)
320277b024eSKalle Valo /* Device ID for SD8797 */
321277b024eSKalle Valo #define SDIO_DEVICE_ID_MARVELL_8797   (0x9129)
322277b024eSKalle Valo /* Device ID for SD8897 */
323277b024eSKalle Valo #define SDIO_DEVICE_ID_MARVELL_8897   (0x912d)
324277b024eSKalle Valo /* Device ID for SD8887 */
325277b024eSKalle Valo #define SDIO_DEVICE_ID_MARVELL_8887   (0x9135)
326277b024eSKalle Valo /* Device ID for SD8801 */
327277b024eSKalle Valo #define SDIO_DEVICE_ID_MARVELL_8801   (0x9139)
328277b024eSKalle Valo /* Device ID for SD8997 */
329277b024eSKalle Valo #define SDIO_DEVICE_ID_MARVELL_8997   (0x9141)
330277b024eSKalle Valo 
331277b024eSKalle Valo 
332277b024eSKalle Valo /* WLAN IDs */
333277b024eSKalle Valo static const struct sdio_device_id mwifiex_ids[] = {
334277b024eSKalle Valo 	{SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8786),
335277b024eSKalle Valo 		.driver_data = (unsigned long) &mwifiex_sdio_sd8786},
336277b024eSKalle Valo 	{SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787),
337277b024eSKalle Valo 		.driver_data = (unsigned long) &mwifiex_sdio_sd8787},
338277b024eSKalle Valo 	{SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8797),
339277b024eSKalle Valo 		.driver_data = (unsigned long) &mwifiex_sdio_sd8797},
340277b024eSKalle Valo 	{SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8897),
341277b024eSKalle Valo 		.driver_data = (unsigned long) &mwifiex_sdio_sd8897},
342277b024eSKalle Valo 	{SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8887),
343277b024eSKalle Valo 		.driver_data = (unsigned long)&mwifiex_sdio_sd8887},
344277b024eSKalle Valo 	{SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8801),
345277b024eSKalle Valo 		.driver_data = (unsigned long)&mwifiex_sdio_sd8801},
346277b024eSKalle Valo 	{SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8997),
347277b024eSKalle Valo 		.driver_data = (unsigned long)&mwifiex_sdio_sd8997},
348277b024eSKalle Valo 	{},
349277b024eSKalle Valo };
350277b024eSKalle Valo 
351277b024eSKalle Valo MODULE_DEVICE_TABLE(sdio, mwifiex_ids);
352277b024eSKalle Valo 
353277b024eSKalle Valo static const struct dev_pm_ops mwifiex_sdio_pm_ops = {
354277b024eSKalle Valo 	.suspend = mwifiex_sdio_suspend,
355277b024eSKalle Valo 	.resume = mwifiex_sdio_resume,
356277b024eSKalle Valo };
357277b024eSKalle Valo 
358277b024eSKalle Valo static struct sdio_driver mwifiex_sdio = {
359277b024eSKalle Valo 	.name = "mwifiex_sdio",
360277b024eSKalle Valo 	.id_table = mwifiex_ids,
361277b024eSKalle Valo 	.probe = mwifiex_sdio_probe,
362277b024eSKalle Valo 	.remove = mwifiex_sdio_remove,
363277b024eSKalle Valo 	.drv = {
364277b024eSKalle Valo 		.owner = THIS_MODULE,
365277b024eSKalle Valo 		.pm = &mwifiex_sdio_pm_ops,
366277b024eSKalle Valo 	}
367277b024eSKalle Valo };
368277b024eSKalle Valo 
369277b024eSKalle Valo /* Write data into SDIO card register. Caller claims SDIO device. */
370277b024eSKalle Valo static int
371277b024eSKalle Valo mwifiex_write_reg_locked(struct sdio_func *func, u32 reg, u8 data)
372277b024eSKalle Valo {
373277b024eSKalle Valo 	int ret = -1;
374277b024eSKalle Valo 	sdio_writeb(func, data, reg, &ret);
375277b024eSKalle Valo 	return ret;
376277b024eSKalle Valo }
377277b024eSKalle Valo 
378277b024eSKalle Valo /*
379277b024eSKalle Valo  * This function writes data into SDIO card register.
380277b024eSKalle Valo  */
381277b024eSKalle Valo static int
382277b024eSKalle Valo mwifiex_write_reg(struct mwifiex_adapter *adapter, u32 reg, u8 data)
383277b024eSKalle Valo {
384277b024eSKalle Valo 	struct sdio_mmc_card *card = adapter->card;
385277b024eSKalle Valo 	int ret;
386277b024eSKalle Valo 
387277b024eSKalle Valo 	sdio_claim_host(card->func);
388277b024eSKalle Valo 	ret = mwifiex_write_reg_locked(card->func, reg, data);
389277b024eSKalle Valo 	sdio_release_host(card->func);
390277b024eSKalle Valo 
391277b024eSKalle Valo 	return ret;
392277b024eSKalle Valo }
393277b024eSKalle Valo 
394277b024eSKalle Valo /*
395277b024eSKalle Valo  * This function reads data from SDIO card register.
396277b024eSKalle Valo  */
397277b024eSKalle Valo static int
398277b024eSKalle Valo mwifiex_read_reg(struct mwifiex_adapter *adapter, u32 reg, u8 *data)
399277b024eSKalle Valo {
400277b024eSKalle Valo 	struct sdio_mmc_card *card = adapter->card;
401277b024eSKalle Valo 	int ret = -1;
402277b024eSKalle Valo 	u8 val;
403277b024eSKalle Valo 
404277b024eSKalle Valo 	sdio_claim_host(card->func);
405277b024eSKalle Valo 	val = sdio_readb(card->func, reg, &ret);
406277b024eSKalle Valo 	sdio_release_host(card->func);
407277b024eSKalle Valo 
408277b024eSKalle Valo 	*data = val;
409277b024eSKalle Valo 
410277b024eSKalle Valo 	return ret;
411277b024eSKalle Valo }
412277b024eSKalle Valo 
413277b024eSKalle Valo /*
414277b024eSKalle Valo  * This function writes multiple data into SDIO card memory.
415277b024eSKalle Valo  *
416277b024eSKalle Valo  * This does not work in suspended mode.
417277b024eSKalle Valo  */
418277b024eSKalle Valo static int
419277b024eSKalle Valo mwifiex_write_data_sync(struct mwifiex_adapter *adapter,
420277b024eSKalle Valo 			u8 *buffer, u32 pkt_len, u32 port)
421277b024eSKalle Valo {
422277b024eSKalle Valo 	struct sdio_mmc_card *card = adapter->card;
423277b024eSKalle Valo 	int ret;
424277b024eSKalle Valo 	u8 blk_mode =
425277b024eSKalle Valo 		(port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE : BLOCK_MODE;
426277b024eSKalle Valo 	u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
427277b024eSKalle Valo 	u32 blk_cnt =
428277b024eSKalle Valo 		(blk_mode ==
429277b024eSKalle Valo 		 BLOCK_MODE) ? (pkt_len /
430277b024eSKalle Valo 				MWIFIEX_SDIO_BLOCK_SIZE) : pkt_len;
431277b024eSKalle Valo 	u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
432277b024eSKalle Valo 
433277b024eSKalle Valo 	if (adapter->is_suspended) {
434277b024eSKalle Valo 		mwifiex_dbg(adapter, ERROR,
435277b024eSKalle Valo 			    "%s: not allowed while suspended\n", __func__);
436277b024eSKalle Valo 		return -1;
437277b024eSKalle Valo 	}
438277b024eSKalle Valo 
439277b024eSKalle Valo 	sdio_claim_host(card->func);
440277b024eSKalle Valo 
441277b024eSKalle Valo 	ret = sdio_writesb(card->func, ioport, buffer, blk_cnt * blk_size);
442277b024eSKalle Valo 
443277b024eSKalle Valo 	sdio_release_host(card->func);
444277b024eSKalle Valo 
445277b024eSKalle Valo 	return ret;
446277b024eSKalle Valo }
447277b024eSKalle Valo 
448277b024eSKalle Valo /*
449277b024eSKalle Valo  * This function reads multiple data from SDIO card memory.
450277b024eSKalle Valo  */
451277b024eSKalle Valo static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *buffer,
452277b024eSKalle Valo 				  u32 len, u32 port, u8 claim)
453277b024eSKalle Valo {
454277b024eSKalle Valo 	struct sdio_mmc_card *card = adapter->card;
455277b024eSKalle Valo 	int ret;
456277b024eSKalle Valo 	u8 blk_mode = (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE
457277b024eSKalle Valo 		       : BLOCK_MODE;
458277b024eSKalle Valo 	u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
459277b024eSKalle Valo 	u32 blk_cnt = (blk_mode == BLOCK_MODE) ? (len / MWIFIEX_SDIO_BLOCK_SIZE)
460277b024eSKalle Valo 			: len;
461277b024eSKalle Valo 	u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
462277b024eSKalle Valo 
463277b024eSKalle Valo 	if (claim)
464277b024eSKalle Valo 		sdio_claim_host(card->func);
465277b024eSKalle Valo 
466277b024eSKalle Valo 	ret = sdio_readsb(card->func, buffer, ioport, blk_cnt * blk_size);
467277b024eSKalle Valo 
468277b024eSKalle Valo 	if (claim)
469277b024eSKalle Valo 		sdio_release_host(card->func);
470277b024eSKalle Valo 
471277b024eSKalle Valo 	return ret;
472277b024eSKalle Valo }
473277b024eSKalle Valo 
474277b024eSKalle Valo /*
475277b024eSKalle Valo  * This function wakes up the card.
476277b024eSKalle Valo  *
477277b024eSKalle Valo  * A host power up command is written to the card configuration
478277b024eSKalle Valo  * register to wake up the card.
479277b024eSKalle Valo  */
480277b024eSKalle Valo static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
481277b024eSKalle Valo {
482277b024eSKalle Valo 	mwifiex_dbg(adapter, EVENT,
483277b024eSKalle Valo 		    "event: wakeup device...\n");
484277b024eSKalle Valo 
485277b024eSKalle Valo 	return mwifiex_write_reg(adapter, CONFIGURATION_REG, HOST_POWER_UP);
486277b024eSKalle Valo }
487277b024eSKalle Valo 
488277b024eSKalle Valo /*
489277b024eSKalle Valo  * This function is called after the card has woken up.
490277b024eSKalle Valo  *
491277b024eSKalle Valo  * The card configuration register is reset.
492277b024eSKalle Valo  */
493277b024eSKalle Valo static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
494277b024eSKalle Valo {
495277b024eSKalle Valo 	mwifiex_dbg(adapter, EVENT,
496277b024eSKalle Valo 		    "cmd: wakeup device completed\n");
497277b024eSKalle Valo 
498277b024eSKalle Valo 	return mwifiex_write_reg(adapter, CONFIGURATION_REG, 0);
499277b024eSKalle Valo }
500277b024eSKalle Valo 
5012095b142SArnd Bergmann static int mwifiex_sdio_dnld_fw(struct mwifiex_adapter *adapter,
5022095b142SArnd Bergmann 			struct mwifiex_fw_image *fw)
5032095b142SArnd Bergmann {
5042095b142SArnd Bergmann 	struct sdio_mmc_card *card = adapter->card;
5052095b142SArnd Bergmann 	int ret;
5062095b142SArnd Bergmann 
5072095b142SArnd Bergmann 	sdio_claim_host(card->func);
5082095b142SArnd Bergmann 	ret = mwifiex_dnld_fw(adapter, fw);
5092095b142SArnd Bergmann 	sdio_release_host(card->func);
5102095b142SArnd Bergmann 
5112095b142SArnd Bergmann 	return ret;
5122095b142SArnd Bergmann }
5132095b142SArnd Bergmann 
514277b024eSKalle Valo /*
515277b024eSKalle Valo  * This function is used to initialize IO ports for the
516277b024eSKalle Valo  * chipsets supporting SDIO new mode eg SD8897.
517277b024eSKalle Valo  */
518277b024eSKalle Valo static int mwifiex_init_sdio_new_mode(struct mwifiex_adapter *adapter)
519277b024eSKalle Valo {
520277b024eSKalle Valo 	u8 reg;
521277b024eSKalle Valo 	struct sdio_mmc_card *card = adapter->card;
522277b024eSKalle Valo 
523277b024eSKalle Valo 	adapter->ioport = MEM_PORT;
524277b024eSKalle Valo 
525277b024eSKalle Valo 	/* enable sdio new mode */
526277b024eSKalle Valo 	if (mwifiex_read_reg(adapter, card->reg->card_cfg_2_1_reg, &reg))
527277b024eSKalle Valo 		return -1;
528277b024eSKalle Valo 	if (mwifiex_write_reg(adapter, card->reg->card_cfg_2_1_reg,
529277b024eSKalle Valo 			      reg | CMD53_NEW_MODE))
530277b024eSKalle Valo 		return -1;
531277b024eSKalle Valo 
532277b024eSKalle Valo 	/* Configure cmd port and enable reading rx length from the register */
533277b024eSKalle Valo 	if (mwifiex_read_reg(adapter, card->reg->cmd_cfg_0, &reg))
534277b024eSKalle Valo 		return -1;
535277b024eSKalle Valo 	if (mwifiex_write_reg(adapter, card->reg->cmd_cfg_0,
536277b024eSKalle Valo 			      reg | CMD_PORT_RD_LEN_EN))
537277b024eSKalle Valo 		return -1;
538277b024eSKalle Valo 
539277b024eSKalle Valo 	/* Enable Dnld/Upld ready auto reset for cmd port after cmd53 is
540277b024eSKalle Valo 	 * completed
541277b024eSKalle Valo 	 */
542277b024eSKalle Valo 	if (mwifiex_read_reg(adapter, card->reg->cmd_cfg_1, &reg))
543277b024eSKalle Valo 		return -1;
544277b024eSKalle Valo 	if (mwifiex_write_reg(adapter, card->reg->cmd_cfg_1,
545277b024eSKalle Valo 			      reg | CMD_PORT_AUTO_EN))
546277b024eSKalle Valo 		return -1;
547277b024eSKalle Valo 
548277b024eSKalle Valo 	return 0;
549277b024eSKalle Valo }
550277b024eSKalle Valo 
551277b024eSKalle Valo /* This function initializes the IO ports.
552277b024eSKalle Valo  *
553277b024eSKalle Valo  * The following operations are performed -
554277b024eSKalle Valo  *      - Read the IO ports (0, 1 and 2)
555277b024eSKalle Valo  *      - Set host interrupt Reset-To-Read to clear
556277b024eSKalle Valo  *      - Set auto re-enable interrupt
557277b024eSKalle Valo  */
558277b024eSKalle Valo static int mwifiex_init_sdio_ioport(struct mwifiex_adapter *adapter)
559277b024eSKalle Valo {
560277b024eSKalle Valo 	u8 reg;
561277b024eSKalle Valo 	struct sdio_mmc_card *card = adapter->card;
562277b024eSKalle Valo 
563277b024eSKalle Valo 	adapter->ioport = 0;
564277b024eSKalle Valo 
565277b024eSKalle Valo 	if (card->supports_sdio_new_mode) {
566277b024eSKalle Valo 		if (mwifiex_init_sdio_new_mode(adapter))
567277b024eSKalle Valo 			return -1;
568277b024eSKalle Valo 		goto cont;
569277b024eSKalle Valo 	}
570277b024eSKalle Valo 
571277b024eSKalle Valo 	/* Read the IO port */
572277b024eSKalle Valo 	if (!mwifiex_read_reg(adapter, card->reg->io_port_0_reg, &reg))
573277b024eSKalle Valo 		adapter->ioport |= (reg & 0xff);
574277b024eSKalle Valo 	else
575277b024eSKalle Valo 		return -1;
576277b024eSKalle Valo 
577277b024eSKalle Valo 	if (!mwifiex_read_reg(adapter, card->reg->io_port_1_reg, &reg))
578277b024eSKalle Valo 		adapter->ioport |= ((reg & 0xff) << 8);
579277b024eSKalle Valo 	else
580277b024eSKalle Valo 		return -1;
581277b024eSKalle Valo 
582277b024eSKalle Valo 	if (!mwifiex_read_reg(adapter, card->reg->io_port_2_reg, &reg))
583277b024eSKalle Valo 		adapter->ioport |= ((reg & 0xff) << 16);
584277b024eSKalle Valo 	else
585277b024eSKalle Valo 		return -1;
586277b024eSKalle Valo cont:
587277b024eSKalle Valo 	mwifiex_dbg(adapter, INFO,
588277b024eSKalle Valo 		    "info: SDIO FUNC1 IO port: %#x\n", adapter->ioport);
589277b024eSKalle Valo 
590277b024eSKalle Valo 	/* Set Host interrupt reset to read to clear */
591277b024eSKalle Valo 	if (!mwifiex_read_reg(adapter, card->reg->host_int_rsr_reg, &reg))
592277b024eSKalle Valo 		mwifiex_write_reg(adapter, card->reg->host_int_rsr_reg,
593277b024eSKalle Valo 				  reg | card->reg->sdio_int_mask);
594277b024eSKalle Valo 	else
595277b024eSKalle Valo 		return -1;
596277b024eSKalle Valo 
597277b024eSKalle Valo 	/* Dnld/Upld ready set to auto reset */
598277b024eSKalle Valo 	if (!mwifiex_read_reg(adapter, card->reg->card_misc_cfg_reg, &reg))
599277b024eSKalle Valo 		mwifiex_write_reg(adapter, card->reg->card_misc_cfg_reg,
600277b024eSKalle Valo 				  reg | AUTO_RE_ENABLE_INT);
601277b024eSKalle Valo 	else
602277b024eSKalle Valo 		return -1;
603277b024eSKalle Valo 
604277b024eSKalle Valo 	return 0;
605277b024eSKalle Valo }
606277b024eSKalle Valo 
607277b024eSKalle Valo /*
608277b024eSKalle Valo  * This function sends data to the card.
609277b024eSKalle Valo  */
610277b024eSKalle Valo static int mwifiex_write_data_to_card(struct mwifiex_adapter *adapter,
611277b024eSKalle Valo 				      u8 *payload, u32 pkt_len, u32 port)
612277b024eSKalle Valo {
613277b024eSKalle Valo 	u32 i = 0;
614277b024eSKalle Valo 	int ret;
615277b024eSKalle Valo 
616277b024eSKalle Valo 	do {
617277b024eSKalle Valo 		ret = mwifiex_write_data_sync(adapter, payload, pkt_len, port);
618277b024eSKalle Valo 		if (ret) {
619277b024eSKalle Valo 			i++;
620277b024eSKalle Valo 			mwifiex_dbg(adapter, ERROR,
621277b024eSKalle Valo 				    "host_to_card, write iomem\t"
622277b024eSKalle Valo 				    "(%d) failed: %d\n", i, ret);
623277b024eSKalle Valo 			if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
624277b024eSKalle Valo 				mwifiex_dbg(adapter, ERROR,
625277b024eSKalle Valo 					    "write CFG reg failed\n");
626277b024eSKalle Valo 
627277b024eSKalle Valo 			ret = -1;
628277b024eSKalle Valo 			if (i > MAX_WRITE_IOMEM_RETRY)
629277b024eSKalle Valo 				return ret;
630277b024eSKalle Valo 		}
631277b024eSKalle Valo 	} while (ret == -1);
632277b024eSKalle Valo 
633277b024eSKalle Valo 	return ret;
634277b024eSKalle Valo }
635277b024eSKalle Valo 
636277b024eSKalle Valo /*
637277b024eSKalle Valo  * This function gets the read port.
638277b024eSKalle Valo  *
639277b024eSKalle Valo  * If control port bit is set in MP read bitmap, the control port
640277b024eSKalle Valo  * is returned, otherwise the current read port is returned and
641277b024eSKalle Valo  * the value is increased (provided it does not reach the maximum
642277b024eSKalle Valo  * limit, in which case it is reset to 1)
643277b024eSKalle Valo  */
644277b024eSKalle Valo static int mwifiex_get_rd_port(struct mwifiex_adapter *adapter, u8 *port)
645277b024eSKalle Valo {
646277b024eSKalle Valo 	struct sdio_mmc_card *card = adapter->card;
647277b024eSKalle Valo 	const struct mwifiex_sdio_card_reg *reg = card->reg;
648277b024eSKalle Valo 	u32 rd_bitmap = card->mp_rd_bitmap;
649277b024eSKalle Valo 
650277b024eSKalle Valo 	mwifiex_dbg(adapter, DATA,
651277b024eSKalle Valo 		    "data: mp_rd_bitmap=0x%08x\n", rd_bitmap);
652277b024eSKalle Valo 
653277b024eSKalle Valo 	if (card->supports_sdio_new_mode) {
654277b024eSKalle Valo 		if (!(rd_bitmap & reg->data_port_mask))
655277b024eSKalle Valo 			return -1;
656277b024eSKalle Valo 	} else {
657277b024eSKalle Valo 		if (!(rd_bitmap & (CTRL_PORT_MASK | reg->data_port_mask)))
658277b024eSKalle Valo 			return -1;
659277b024eSKalle Valo 	}
660277b024eSKalle Valo 
661277b024eSKalle Valo 	if ((card->has_control_mask) &&
662277b024eSKalle Valo 	    (card->mp_rd_bitmap & CTRL_PORT_MASK)) {
663277b024eSKalle Valo 		card->mp_rd_bitmap &= (u32) (~CTRL_PORT_MASK);
664277b024eSKalle Valo 		*port = CTRL_PORT;
665277b024eSKalle Valo 		mwifiex_dbg(adapter, DATA,
666277b024eSKalle Valo 			    "data: port=%d mp_rd_bitmap=0x%08x\n",
667277b024eSKalle Valo 			    *port, card->mp_rd_bitmap);
668277b024eSKalle Valo 		return 0;
669277b024eSKalle Valo 	}
670277b024eSKalle Valo 
671277b024eSKalle Valo 	if (!(card->mp_rd_bitmap & (1 << card->curr_rd_port)))
672277b024eSKalle Valo 		return -1;
673277b024eSKalle Valo 
674277b024eSKalle Valo 	/* We are now handling the SDIO data ports */
675277b024eSKalle Valo 	card->mp_rd_bitmap &= (u32)(~(1 << card->curr_rd_port));
676277b024eSKalle Valo 	*port = card->curr_rd_port;
677277b024eSKalle Valo 
678277b024eSKalle Valo 	if (++card->curr_rd_port == card->max_ports)
679277b024eSKalle Valo 		card->curr_rd_port = reg->start_rd_port;
680277b024eSKalle Valo 
681277b024eSKalle Valo 	mwifiex_dbg(adapter, DATA,
682277b024eSKalle Valo 		    "data: port=%d mp_rd_bitmap=0x%08x -> 0x%08x\n",
683277b024eSKalle Valo 		    *port, rd_bitmap, card->mp_rd_bitmap);
684277b024eSKalle Valo 
685277b024eSKalle Valo 	return 0;
686277b024eSKalle Valo }
687277b024eSKalle Valo 
688277b024eSKalle Valo /*
689277b024eSKalle Valo  * This function gets the write port for data.
690277b024eSKalle Valo  *
691277b024eSKalle Valo  * The current write port is returned if available and the value is
692277b024eSKalle Valo  * increased (provided it does not reach the maximum limit, in which
693277b024eSKalle Valo  * case it is reset to 1)
694277b024eSKalle Valo  */
695277b024eSKalle Valo static int mwifiex_get_wr_port_data(struct mwifiex_adapter *adapter, u32 *port)
696277b024eSKalle Valo {
697277b024eSKalle Valo 	struct sdio_mmc_card *card = adapter->card;
698277b024eSKalle Valo 	const struct mwifiex_sdio_card_reg *reg = card->reg;
699277b024eSKalle Valo 	u32 wr_bitmap = card->mp_wr_bitmap;
700277b024eSKalle Valo 
701277b024eSKalle Valo 	mwifiex_dbg(adapter, DATA,
702277b024eSKalle Valo 		    "data: mp_wr_bitmap=0x%08x\n", wr_bitmap);
703277b024eSKalle Valo 
704277b024eSKalle Valo 	if (!(wr_bitmap & card->mp_data_port_mask)) {
705277b024eSKalle Valo 		adapter->data_sent = true;
706277b024eSKalle Valo 		return -EBUSY;
707277b024eSKalle Valo 	}
708277b024eSKalle Valo 
709277b024eSKalle Valo 	if (card->mp_wr_bitmap & (1 << card->curr_wr_port)) {
710277b024eSKalle Valo 		card->mp_wr_bitmap &= (u32) (~(1 << card->curr_wr_port));
711277b024eSKalle Valo 		*port = card->curr_wr_port;
712277b024eSKalle Valo 		if (++card->curr_wr_port == card->mp_end_port)
713277b024eSKalle Valo 			card->curr_wr_port = reg->start_wr_port;
714277b024eSKalle Valo 	} else {
715277b024eSKalle Valo 		adapter->data_sent = true;
716277b024eSKalle Valo 		return -EBUSY;
717277b024eSKalle Valo 	}
718277b024eSKalle Valo 
719277b024eSKalle Valo 	if ((card->has_control_mask) && (*port == CTRL_PORT)) {
720277b024eSKalle Valo 		mwifiex_dbg(adapter, ERROR,
721277b024eSKalle Valo 			    "invalid data port=%d cur port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
722277b024eSKalle Valo 			    *port, card->curr_wr_port, wr_bitmap,
723277b024eSKalle Valo 			    card->mp_wr_bitmap);
724277b024eSKalle Valo 		return -1;
725277b024eSKalle Valo 	}
726277b024eSKalle Valo 
727277b024eSKalle Valo 	mwifiex_dbg(adapter, DATA,
728277b024eSKalle Valo 		    "data: port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
729277b024eSKalle Valo 		    *port, wr_bitmap, card->mp_wr_bitmap);
730277b024eSKalle Valo 
731277b024eSKalle Valo 	return 0;
732277b024eSKalle Valo }
733277b024eSKalle Valo 
734277b024eSKalle Valo /*
735277b024eSKalle Valo  * This function polls the card status.
736277b024eSKalle Valo  */
737277b024eSKalle Valo static int
738277b024eSKalle Valo mwifiex_sdio_poll_card_status(struct mwifiex_adapter *adapter, u8 bits)
739277b024eSKalle Valo {
740277b024eSKalle Valo 	struct sdio_mmc_card *card = adapter->card;
741277b024eSKalle Valo 	u32 tries;
742277b024eSKalle Valo 	u8 cs;
743277b024eSKalle Valo 
744277b024eSKalle Valo 	for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
745277b024eSKalle Valo 		if (mwifiex_read_reg(adapter, card->reg->poll_reg, &cs))
746277b024eSKalle Valo 			break;
747277b024eSKalle Valo 		else if ((cs & bits) == bits)
748277b024eSKalle Valo 			return 0;
749277b024eSKalle Valo 
750277b024eSKalle Valo 		usleep_range(10, 20);
751277b024eSKalle Valo 	}
752277b024eSKalle Valo 
753277b024eSKalle Valo 	mwifiex_dbg(adapter, ERROR,
754277b024eSKalle Valo 		    "poll card status failed, tries = %d\n", tries);
755277b024eSKalle Valo 
756277b024eSKalle Valo 	return -1;
757277b024eSKalle Valo }
758277b024eSKalle Valo 
759277b024eSKalle Valo /*
760277b024eSKalle Valo  * This function reads the firmware status.
761277b024eSKalle Valo  */
762277b024eSKalle Valo static int
763277b024eSKalle Valo mwifiex_sdio_read_fw_status(struct mwifiex_adapter *adapter, u16 *dat)
764277b024eSKalle Valo {
765277b024eSKalle Valo 	struct sdio_mmc_card *card = adapter->card;
766277b024eSKalle Valo 	const struct mwifiex_sdio_card_reg *reg = card->reg;
767277b024eSKalle Valo 	u8 fws0, fws1;
768277b024eSKalle Valo 
769277b024eSKalle Valo 	if (mwifiex_read_reg(adapter, reg->status_reg_0, &fws0))
770277b024eSKalle Valo 		return -1;
771277b024eSKalle Valo 
772277b024eSKalle Valo 	if (mwifiex_read_reg(adapter, reg->status_reg_1, &fws1))
773277b024eSKalle Valo 		return -1;
774277b024eSKalle Valo 
775277b024eSKalle Valo 	*dat = (u16) ((fws1 << 8) | fws0);
776277b024eSKalle Valo 
777277b024eSKalle Valo 	return 0;
778277b024eSKalle Valo }
779277b024eSKalle Valo 
780277b024eSKalle Valo /*
781277b024eSKalle Valo  * This function disables the host interrupt.
782277b024eSKalle Valo  *
783277b024eSKalle Valo  * The host interrupt mask is read, the disable bit is reset and
784277b024eSKalle Valo  * written back to the card host interrupt mask register.
785277b024eSKalle Valo  */
786277b024eSKalle Valo static void mwifiex_sdio_disable_host_int(struct mwifiex_adapter *adapter)
787277b024eSKalle Valo {
788277b024eSKalle Valo 	struct sdio_mmc_card *card = adapter->card;
789277b024eSKalle Valo 	struct sdio_func *func = card->func;
790277b024eSKalle Valo 
791277b024eSKalle Valo 	sdio_claim_host(func);
792277b024eSKalle Valo 	mwifiex_write_reg_locked(func, card->reg->host_int_mask_reg, 0);
793277b024eSKalle Valo 	sdio_release_irq(func);
794277b024eSKalle Valo 	sdio_release_host(func);
795277b024eSKalle Valo }
796277b024eSKalle Valo 
797277b024eSKalle Valo /*
798277b024eSKalle Valo  * This function reads the interrupt status from card.
799277b024eSKalle Valo  */
800277b024eSKalle Valo static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
801277b024eSKalle Valo {
802277b024eSKalle Valo 	struct sdio_mmc_card *card = adapter->card;
803277b024eSKalle Valo 	u8 sdio_ireg;
804277b024eSKalle Valo 	unsigned long flags;
805277b024eSKalle Valo 
806277b024eSKalle Valo 	if (mwifiex_read_data_sync(adapter, card->mp_regs,
807277b024eSKalle Valo 				   card->reg->max_mp_regs,
808277b024eSKalle Valo 				   REG_PORT | MWIFIEX_SDIO_BYTE_MODE_MASK, 0)) {
809277b024eSKalle Valo 		mwifiex_dbg(adapter, ERROR, "read mp_regs failed\n");
810277b024eSKalle Valo 		return;
811277b024eSKalle Valo 	}
812277b024eSKalle Valo 
813277b024eSKalle Valo 	sdio_ireg = card->mp_regs[card->reg->host_int_status_reg];
814277b024eSKalle Valo 	if (sdio_ireg) {
815277b024eSKalle Valo 		/*
816277b024eSKalle Valo 		 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
817277b024eSKalle Valo 		 * For SDIO new mode CMD port interrupts
818277b024eSKalle Valo 		 *	DN_LD_CMD_PORT_HOST_INT_STATUS and/or
819277b024eSKalle Valo 		 *	UP_LD_CMD_PORT_HOST_INT_STATUS
820277b024eSKalle Valo 		 * Clear the interrupt status register
821277b024eSKalle Valo 		 */
822277b024eSKalle Valo 		mwifiex_dbg(adapter, INTR,
823277b024eSKalle Valo 			    "int: sdio_ireg = %#x\n", sdio_ireg);
824277b024eSKalle Valo 		spin_lock_irqsave(&adapter->int_lock, flags);
825277b024eSKalle Valo 		adapter->int_status |= sdio_ireg;
826277b024eSKalle Valo 		spin_unlock_irqrestore(&adapter->int_lock, flags);
827277b024eSKalle Valo 	}
828277b024eSKalle Valo }
829277b024eSKalle Valo 
830277b024eSKalle Valo /*
831277b024eSKalle Valo  * SDIO interrupt handler.
832277b024eSKalle Valo  *
833277b024eSKalle Valo  * This function reads the interrupt status from firmware and handles
834277b024eSKalle Valo  * the interrupt in current thread (ksdioirqd) right away.
835277b024eSKalle Valo  */
836277b024eSKalle Valo static void
837277b024eSKalle Valo mwifiex_sdio_interrupt(struct sdio_func *func)
838277b024eSKalle Valo {
839277b024eSKalle Valo 	struct mwifiex_adapter *adapter;
840277b024eSKalle Valo 	struct sdio_mmc_card *card;
841277b024eSKalle Valo 
842277b024eSKalle Valo 	card = sdio_get_drvdata(func);
843277b024eSKalle Valo 	if (!card || !card->adapter) {
84491442431SXinming Hu 		pr_err("int: func=%p card=%p adapter=%p\n",
845277b024eSKalle Valo 		       func, card, card ? card->adapter : NULL);
846277b024eSKalle Valo 		return;
847277b024eSKalle Valo 	}
848277b024eSKalle Valo 	adapter = card->adapter;
849277b024eSKalle Valo 
850277b024eSKalle Valo 	if (!adapter->pps_uapsd_mode && adapter->ps_state == PS_STATE_SLEEP)
851277b024eSKalle Valo 		adapter->ps_state = PS_STATE_AWAKE;
852277b024eSKalle Valo 
853277b024eSKalle Valo 	mwifiex_interrupt_status(adapter);
854277b024eSKalle Valo 	mwifiex_main_process(adapter);
855277b024eSKalle Valo }
856277b024eSKalle Valo 
857277b024eSKalle Valo /*
858277b024eSKalle Valo  * This function enables the host interrupt.
859277b024eSKalle Valo  *
860277b024eSKalle Valo  * The host interrupt enable mask is written to the card
861277b024eSKalle Valo  * host interrupt mask register.
862277b024eSKalle Valo  */
863277b024eSKalle Valo static int mwifiex_sdio_enable_host_int(struct mwifiex_adapter *adapter)
864277b024eSKalle Valo {
865277b024eSKalle Valo 	struct sdio_mmc_card *card = adapter->card;
866277b024eSKalle Valo 	struct sdio_func *func = card->func;
867277b024eSKalle Valo 	int ret;
868277b024eSKalle Valo 
869277b024eSKalle Valo 	sdio_claim_host(func);
870277b024eSKalle Valo 
871277b024eSKalle Valo 	/* Request the SDIO IRQ */
872277b024eSKalle Valo 	ret = sdio_claim_irq(func, mwifiex_sdio_interrupt);
873277b024eSKalle Valo 	if (ret) {
874277b024eSKalle Valo 		mwifiex_dbg(adapter, ERROR,
875277b024eSKalle Valo 			    "claim irq failed: ret=%d\n", ret);
876277b024eSKalle Valo 		goto out;
877277b024eSKalle Valo 	}
878277b024eSKalle Valo 
879277b024eSKalle Valo 	/* Simply write the mask to the register */
880277b024eSKalle Valo 	ret = mwifiex_write_reg_locked(func, card->reg->host_int_mask_reg,
881277b024eSKalle Valo 				       card->reg->host_int_enable);
882277b024eSKalle Valo 	if (ret) {
883277b024eSKalle Valo 		mwifiex_dbg(adapter, ERROR,
884277b024eSKalle Valo 			    "enable host interrupt failed\n");
885277b024eSKalle Valo 		sdio_release_irq(func);
886277b024eSKalle Valo 	}
887277b024eSKalle Valo 
888277b024eSKalle Valo out:
889277b024eSKalle Valo 	sdio_release_host(func);
890277b024eSKalle Valo 	return ret;
891277b024eSKalle Valo }
892277b024eSKalle Valo 
893277b024eSKalle Valo /*
894277b024eSKalle Valo  * This function sends a data buffer to the card.
895277b024eSKalle Valo  */
896277b024eSKalle Valo static int mwifiex_sdio_card_to_host(struct mwifiex_adapter *adapter,
897277b024eSKalle Valo 				     u32 *type, u8 *buffer,
898277b024eSKalle Valo 				     u32 npayload, u32 ioport)
899277b024eSKalle Valo {
900277b024eSKalle Valo 	int ret;
901277b024eSKalle Valo 	u32 nb;
902277b024eSKalle Valo 
903277b024eSKalle Valo 	if (!buffer) {
904277b024eSKalle Valo 		mwifiex_dbg(adapter, ERROR,
905277b024eSKalle Valo 			    "%s: buffer is NULL\n", __func__);
906277b024eSKalle Valo 		return -1;
907277b024eSKalle Valo 	}
908277b024eSKalle Valo 
909277b024eSKalle Valo 	ret = mwifiex_read_data_sync(adapter, buffer, npayload, ioport, 1);
910277b024eSKalle Valo 
911277b024eSKalle Valo 	if (ret) {
912277b024eSKalle Valo 		mwifiex_dbg(adapter, ERROR,
913277b024eSKalle Valo 			    "%s: read iomem failed: %d\n", __func__,
914277b024eSKalle Valo 			ret);
915277b024eSKalle Valo 		return -1;
916277b024eSKalle Valo 	}
917277b024eSKalle Valo 
918277b024eSKalle Valo 	nb = le16_to_cpu(*(__le16 *) (buffer));
919277b024eSKalle Valo 	if (nb > npayload) {
920277b024eSKalle Valo 		mwifiex_dbg(adapter, ERROR,
921277b024eSKalle Valo 			    "%s: invalid packet, nb=%d npayload=%d\n",
922277b024eSKalle Valo 			    __func__, nb, npayload);
923277b024eSKalle Valo 		return -1;
924277b024eSKalle Valo 	}
925277b024eSKalle Valo 
926277b024eSKalle Valo 	*type = le16_to_cpu(*(__le16 *) (buffer + 2));
927277b024eSKalle Valo 
928277b024eSKalle Valo 	return ret;
929277b024eSKalle Valo }
930277b024eSKalle Valo 
931277b024eSKalle Valo /*
932277b024eSKalle Valo  * This function downloads the firmware to the card.
933277b024eSKalle Valo  *
934277b024eSKalle Valo  * Firmware is downloaded to the card in blocks. Every block download
935277b024eSKalle Valo  * is tested for CRC errors, and retried a number of times before
936277b024eSKalle Valo  * returning failure.
937277b024eSKalle Valo  */
938277b024eSKalle Valo static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
939277b024eSKalle Valo 				    struct mwifiex_fw_image *fw)
940277b024eSKalle Valo {
941277b024eSKalle Valo 	struct sdio_mmc_card *card = adapter->card;
942277b024eSKalle Valo 	const struct mwifiex_sdio_card_reg *reg = card->reg;
943277b024eSKalle Valo 	int ret;
944277b024eSKalle Valo 	u8 *firmware = fw->fw_buf;
945277b024eSKalle Valo 	u32 firmware_len = fw->fw_len;
946277b024eSKalle Valo 	u32 offset = 0;
947277b024eSKalle Valo 	u8 base0, base1;
948277b024eSKalle Valo 	u8 *fwbuf;
949277b024eSKalle Valo 	u16 len = 0;
950277b024eSKalle Valo 	u32 txlen, tx_blocks = 0, tries;
951277b024eSKalle Valo 	u32 i = 0;
952277b024eSKalle Valo 
953277b024eSKalle Valo 	if (!firmware_len) {
954277b024eSKalle Valo 		mwifiex_dbg(adapter, ERROR,
955277b024eSKalle Valo 			    "firmware image not found! Terminating download\n");
956277b024eSKalle Valo 		return -1;
957277b024eSKalle Valo 	}
958277b024eSKalle Valo 
959277b024eSKalle Valo 	mwifiex_dbg(adapter, INFO,
960277b024eSKalle Valo 		    "info: downloading FW image (%d bytes)\n",
961277b024eSKalle Valo 		    firmware_len);
962277b024eSKalle Valo 
963277b024eSKalle Valo 	/* Assume that the allocated buffer is 8-byte aligned */
964277b024eSKalle Valo 	fwbuf = kzalloc(MWIFIEX_UPLD_SIZE, GFP_KERNEL);
965277b024eSKalle Valo 	if (!fwbuf)
966277b024eSKalle Valo 		return -ENOMEM;
967277b024eSKalle Valo 
968277b024eSKalle Valo 	sdio_claim_host(card->func);
969277b024eSKalle Valo 
970277b024eSKalle Valo 	/* Perform firmware data transfer */
971277b024eSKalle Valo 	do {
972277b024eSKalle Valo 		/* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY
973277b024eSKalle Valo 		   bits */
974277b024eSKalle Valo 		ret = mwifiex_sdio_poll_card_status(adapter, CARD_IO_READY |
975277b024eSKalle Valo 						    DN_LD_CARD_RDY);
976277b024eSKalle Valo 		if (ret) {
977277b024eSKalle Valo 			mwifiex_dbg(adapter, ERROR,
978277b024eSKalle Valo 				    "FW download with helper:\t"
979277b024eSKalle Valo 				    "poll status timeout @ %d\n", offset);
980277b024eSKalle Valo 			goto done;
981277b024eSKalle Valo 		}
982277b024eSKalle Valo 
983277b024eSKalle Valo 		/* More data? */
984277b024eSKalle Valo 		if (offset >= firmware_len)
985277b024eSKalle Valo 			break;
986277b024eSKalle Valo 
987277b024eSKalle Valo 		for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
988277b024eSKalle Valo 			ret = mwifiex_read_reg(adapter, reg->base_0_reg,
989277b024eSKalle Valo 					       &base0);
990277b024eSKalle Valo 			if (ret) {
991277b024eSKalle Valo 				mwifiex_dbg(adapter, ERROR,
992277b024eSKalle Valo 					    "dev BASE0 register read failed:\t"
993277b024eSKalle Valo 					    "base0=%#04X(%d). Terminating dnld\n",
994277b024eSKalle Valo 					    base0, base0);
995277b024eSKalle Valo 				goto done;
996277b024eSKalle Valo 			}
997277b024eSKalle Valo 			ret = mwifiex_read_reg(adapter, reg->base_1_reg,
998277b024eSKalle Valo 					       &base1);
999277b024eSKalle Valo 			if (ret) {
1000277b024eSKalle Valo 				mwifiex_dbg(adapter, ERROR,
1001277b024eSKalle Valo 					    "dev BASE1 register read failed:\t"
1002277b024eSKalle Valo 					    "base1=%#04X(%d). Terminating dnld\n",
1003277b024eSKalle Valo 					    base1, base1);
1004277b024eSKalle Valo 				goto done;
1005277b024eSKalle Valo 			}
1006277b024eSKalle Valo 			len = (u16) (((base1 & 0xff) << 8) | (base0 & 0xff));
1007277b024eSKalle Valo 
1008277b024eSKalle Valo 			if (len)
1009277b024eSKalle Valo 				break;
1010277b024eSKalle Valo 
1011277b024eSKalle Valo 			usleep_range(10, 20);
1012277b024eSKalle Valo 		}
1013277b024eSKalle Valo 
1014277b024eSKalle Valo 		if (!len) {
1015277b024eSKalle Valo 			break;
1016277b024eSKalle Valo 		} else if (len > MWIFIEX_UPLD_SIZE) {
1017277b024eSKalle Valo 			mwifiex_dbg(adapter, ERROR,
1018277b024eSKalle Valo 				    "FW dnld failed @ %d, invalid length %d\n",
1019277b024eSKalle Valo 				    offset, len);
1020277b024eSKalle Valo 			ret = -1;
1021277b024eSKalle Valo 			goto done;
1022277b024eSKalle Valo 		}
1023277b024eSKalle Valo 
1024277b024eSKalle Valo 		txlen = len;
1025277b024eSKalle Valo 
1026277b024eSKalle Valo 		if (len & BIT(0)) {
1027277b024eSKalle Valo 			i++;
1028277b024eSKalle Valo 			if (i > MAX_WRITE_IOMEM_RETRY) {
1029277b024eSKalle Valo 				mwifiex_dbg(adapter, ERROR,
1030277b024eSKalle Valo 					    "FW dnld failed @ %d, over max retry\n",
1031277b024eSKalle Valo 					    offset);
1032277b024eSKalle Valo 				ret = -1;
1033277b024eSKalle Valo 				goto done;
1034277b024eSKalle Valo 			}
1035277b024eSKalle Valo 			mwifiex_dbg(adapter, ERROR,
1036277b024eSKalle Valo 				    "CRC indicated by the helper:\t"
1037277b024eSKalle Valo 				    "len = 0x%04X, txlen = %d\n", len, txlen);
1038277b024eSKalle Valo 			len &= ~BIT(0);
1039277b024eSKalle Valo 			/* Setting this to 0 to resend from same offset */
1040277b024eSKalle Valo 			txlen = 0;
1041277b024eSKalle Valo 		} else {
1042277b024eSKalle Valo 			i = 0;
1043277b024eSKalle Valo 
1044277b024eSKalle Valo 			/* Set blocksize to transfer - checking for last
1045277b024eSKalle Valo 			   block */
1046277b024eSKalle Valo 			if (firmware_len - offset < txlen)
1047277b024eSKalle Valo 				txlen = firmware_len - offset;
1048277b024eSKalle Valo 
1049277b024eSKalle Valo 			tx_blocks = (txlen + MWIFIEX_SDIO_BLOCK_SIZE - 1)
1050277b024eSKalle Valo 				    / MWIFIEX_SDIO_BLOCK_SIZE;
1051277b024eSKalle Valo 
1052277b024eSKalle Valo 			/* Copy payload to buffer */
1053277b024eSKalle Valo 			memmove(fwbuf, &firmware[offset], txlen);
1054277b024eSKalle Valo 		}
1055277b024eSKalle Valo 
1056277b024eSKalle Valo 		ret = mwifiex_write_data_sync(adapter, fwbuf, tx_blocks *
1057277b024eSKalle Valo 					      MWIFIEX_SDIO_BLOCK_SIZE,
1058277b024eSKalle Valo 					      adapter->ioport);
1059277b024eSKalle Valo 		if (ret) {
1060277b024eSKalle Valo 			mwifiex_dbg(adapter, ERROR,
1061277b024eSKalle Valo 				    "FW download, write iomem (%d) failed @ %d\n",
1062277b024eSKalle Valo 				    i, offset);
1063277b024eSKalle Valo 			if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
1064277b024eSKalle Valo 				mwifiex_dbg(adapter, ERROR,
1065277b024eSKalle Valo 					    "write CFG reg failed\n");
1066277b024eSKalle Valo 
1067277b024eSKalle Valo 			ret = -1;
1068277b024eSKalle Valo 			goto done;
1069277b024eSKalle Valo 		}
1070277b024eSKalle Valo 
1071277b024eSKalle Valo 		offset += txlen;
1072277b024eSKalle Valo 	} while (true);
1073277b024eSKalle Valo 
1074277b024eSKalle Valo 	mwifiex_dbg(adapter, MSG,
1075277b024eSKalle Valo 		    "info: FW download over, size %d bytes\n", offset);
1076277b024eSKalle Valo 
1077277b024eSKalle Valo 	ret = 0;
1078277b024eSKalle Valo done:
1079b977d305SMarty Faltesek 	sdio_release_host(card->func);
1080277b024eSKalle Valo 	kfree(fwbuf);
1081277b024eSKalle Valo 	return ret;
1082277b024eSKalle Valo }
1083277b024eSKalle Valo 
1084277b024eSKalle Valo /*
1085277b024eSKalle Valo  * This function checks the firmware status in card.
1086277b024eSKalle Valo  */
1087277b024eSKalle Valo static int mwifiex_check_fw_status(struct mwifiex_adapter *adapter,
1088277b024eSKalle Valo 				   u32 poll_num)
1089277b024eSKalle Valo {
1090277b024eSKalle Valo 	int ret = 0;
1091277b024eSKalle Valo 	u16 firmware_stat;
1092277b024eSKalle Valo 	u32 tries;
1093277b024eSKalle Valo 
1094277b024eSKalle Valo 	for (tries = 0; tries < poll_num; tries++) {
1095277b024eSKalle Valo 		ret = mwifiex_sdio_read_fw_status(adapter, &firmware_stat);
1096277b024eSKalle Valo 		if (ret)
1097277b024eSKalle Valo 			continue;
1098277b024eSKalle Valo 		if (firmware_stat == FIRMWARE_READY_SDIO) {
1099277b024eSKalle Valo 			ret = 0;
1100277b024eSKalle Valo 			break;
1101277b024eSKalle Valo 		} else {
1102277b024eSKalle Valo 			msleep(100);
1103277b024eSKalle Valo 			ret = -1;
1104277b024eSKalle Valo 		}
1105277b024eSKalle Valo 	}
1106277b024eSKalle Valo 
11072fd5c6edSchunfan chen 	return ret;
11082fd5c6edSchunfan chen }
1109277b024eSKalle Valo 
11102fd5c6edSchunfan chen /* This function checks if WLAN is the winner.
11112fd5c6edSchunfan chen  */
11122fd5c6edSchunfan chen static int mwifiex_check_winner_status(struct mwifiex_adapter *adapter)
11132fd5c6edSchunfan chen {
11142fd5c6edSchunfan chen 	int ret = 0;
11152fd5c6edSchunfan chen 	u8 winner = 0;
11162fd5c6edSchunfan chen 	struct sdio_mmc_card *card = adapter->card;
11172fd5c6edSchunfan chen 
11182fd5c6edSchunfan chen 	if (mwifiex_read_reg(adapter, card->reg->status_reg_0, &winner))
11192fd5c6edSchunfan chen 		return -1;
11202fd5c6edSchunfan chen 
11212fd5c6edSchunfan chen 	if (winner)
1122277b024eSKalle Valo 		adapter->winner = 0;
1123277b024eSKalle Valo 	else
1124277b024eSKalle Valo 		adapter->winner = 1;
11252fd5c6edSchunfan chen 
1126277b024eSKalle Valo 	return ret;
1127277b024eSKalle Valo }
1128277b024eSKalle Valo 
1129277b024eSKalle Valo /*
1130277b024eSKalle Valo  * This function decode sdio aggreation pkt.
1131277b024eSKalle Valo  *
1132277b024eSKalle Valo  * Based on the the data block size and pkt_len,
1133277b024eSKalle Valo  * skb data will be decoded to few packets.
1134277b024eSKalle Valo  */
1135277b024eSKalle Valo static void mwifiex_deaggr_sdio_pkt(struct mwifiex_adapter *adapter,
1136277b024eSKalle Valo 				    struct sk_buff *skb)
1137277b024eSKalle Valo {
1138277b024eSKalle Valo 	u32 total_pkt_len, pkt_len;
1139277b024eSKalle Valo 	struct sk_buff *skb_deaggr;
1140277b024eSKalle Valo 	u32 pkt_type;
1141277b024eSKalle Valo 	u16 blk_size;
1142277b024eSKalle Valo 	u8 blk_num;
1143277b024eSKalle Valo 	u8 *data;
1144277b024eSKalle Valo 
1145277b024eSKalle Valo 	data = skb->data;
1146277b024eSKalle Valo 	total_pkt_len = skb->len;
1147277b024eSKalle Valo 
1148277b024eSKalle Valo 	while (total_pkt_len >= (SDIO_HEADER_OFFSET + INTF_HEADER_LEN)) {
1149277b024eSKalle Valo 		if (total_pkt_len < adapter->sdio_rx_block_size)
1150277b024eSKalle Valo 			break;
1151277b024eSKalle Valo 		blk_num = *(data + BLOCK_NUMBER_OFFSET);
1152277b024eSKalle Valo 		blk_size = adapter->sdio_rx_block_size * blk_num;
1153277b024eSKalle Valo 		if (blk_size > total_pkt_len) {
1154277b024eSKalle Valo 			mwifiex_dbg(adapter, ERROR,
1155277b024eSKalle Valo 				    "%s: error in blk_size,\t"
1156277b024eSKalle Valo 				    "blk_num=%d, blk_size=%d, total_pkt_len=%d\n",
1157277b024eSKalle Valo 				    __func__, blk_num, blk_size, total_pkt_len);
1158277b024eSKalle Valo 			break;
1159277b024eSKalle Valo 		}
1160277b024eSKalle Valo 		pkt_len = le16_to_cpu(*(__le16 *)(data + SDIO_HEADER_OFFSET));
1161277b024eSKalle Valo 		pkt_type = le16_to_cpu(*(__le16 *)(data + SDIO_HEADER_OFFSET +
1162277b024eSKalle Valo 					 2));
1163277b024eSKalle Valo 		if ((pkt_len + SDIO_HEADER_OFFSET) > blk_size) {
1164277b024eSKalle Valo 			mwifiex_dbg(adapter, ERROR,
1165277b024eSKalle Valo 				    "%s: error in pkt_len,\t"
1166277b024eSKalle Valo 				    "pkt_len=%d, blk_size=%d\n",
1167277b024eSKalle Valo 				    __func__, pkt_len, blk_size);
1168277b024eSKalle Valo 			break;
1169277b024eSKalle Valo 		}
117000c54780SXinming Hu 
117100c54780SXinming Hu 		skb_deaggr = mwifiex_alloc_dma_align_buf(pkt_len, GFP_KERNEL);
1172277b024eSKalle Valo 		if (!skb_deaggr)
1173277b024eSKalle Valo 			break;
1174277b024eSKalle Valo 		skb_put(skb_deaggr, pkt_len);
1175277b024eSKalle Valo 		memcpy(skb_deaggr->data, data + SDIO_HEADER_OFFSET, pkt_len);
1176277b024eSKalle Valo 		skb_pull(skb_deaggr, INTF_HEADER_LEN);
1177277b024eSKalle Valo 
1178277b024eSKalle Valo 		mwifiex_handle_rx_packet(adapter, skb_deaggr);
1179277b024eSKalle Valo 		data += blk_size;
1180277b024eSKalle Valo 		total_pkt_len -= blk_size;
1181277b024eSKalle Valo 	}
1182277b024eSKalle Valo }
1183277b024eSKalle Valo 
1184277b024eSKalle Valo /*
1185277b024eSKalle Valo  * This function decodes a received packet.
1186277b024eSKalle Valo  *
1187277b024eSKalle Valo  * Based on the type, the packet is treated as either a data, or
1188277b024eSKalle Valo  * a command response, or an event, and the correct handler
1189277b024eSKalle Valo  * function is invoked.
1190277b024eSKalle Valo  */
1191277b024eSKalle Valo static int mwifiex_decode_rx_packet(struct mwifiex_adapter *adapter,
1192277b024eSKalle Valo 				    struct sk_buff *skb, u32 upld_typ)
1193277b024eSKalle Valo {
1194277b024eSKalle Valo 	u8 *cmd_buf;
1195277b024eSKalle Valo 	__le16 *curr_ptr = (__le16 *)skb->data;
1196277b024eSKalle Valo 	u16 pkt_len = le16_to_cpu(*curr_ptr);
1197277b024eSKalle Valo 	struct mwifiex_rxinfo *rx_info;
1198277b024eSKalle Valo 
1199277b024eSKalle Valo 	if (upld_typ != MWIFIEX_TYPE_AGGR_DATA) {
1200277b024eSKalle Valo 		skb_trim(skb, pkt_len);
1201277b024eSKalle Valo 		skb_pull(skb, INTF_HEADER_LEN);
1202277b024eSKalle Valo 	}
1203277b024eSKalle Valo 
1204277b024eSKalle Valo 	switch (upld_typ) {
1205277b024eSKalle Valo 	case MWIFIEX_TYPE_AGGR_DATA:
1206277b024eSKalle Valo 		mwifiex_dbg(adapter, INFO,
1207277b024eSKalle Valo 			    "info: --- Rx: Aggr Data packet ---\n");
1208277b024eSKalle Valo 		rx_info = MWIFIEX_SKB_RXCB(skb);
1209277b024eSKalle Valo 		rx_info->buf_type = MWIFIEX_TYPE_AGGR_DATA;
1210277b024eSKalle Valo 		if (adapter->rx_work_enabled) {
1211277b024eSKalle Valo 			skb_queue_tail(&adapter->rx_data_q, skb);
1212277b024eSKalle Valo 			atomic_inc(&adapter->rx_pending);
1213277b024eSKalle Valo 			adapter->data_received = true;
1214277b024eSKalle Valo 		} else {
1215277b024eSKalle Valo 			mwifiex_deaggr_sdio_pkt(adapter, skb);
1216277b024eSKalle Valo 			dev_kfree_skb_any(skb);
1217277b024eSKalle Valo 		}
1218277b024eSKalle Valo 		break;
1219277b024eSKalle Valo 
1220277b024eSKalle Valo 	case MWIFIEX_TYPE_DATA:
1221277b024eSKalle Valo 		mwifiex_dbg(adapter, DATA,
1222277b024eSKalle Valo 			    "info: --- Rx: Data packet ---\n");
1223277b024eSKalle Valo 		if (adapter->rx_work_enabled) {
1224277b024eSKalle Valo 			skb_queue_tail(&adapter->rx_data_q, skb);
1225277b024eSKalle Valo 			adapter->data_received = true;
1226277b024eSKalle Valo 			atomic_inc(&adapter->rx_pending);
1227277b024eSKalle Valo 		} else {
1228277b024eSKalle Valo 			mwifiex_handle_rx_packet(adapter, skb);
1229277b024eSKalle Valo 		}
1230277b024eSKalle Valo 		break;
1231277b024eSKalle Valo 
1232277b024eSKalle Valo 	case MWIFIEX_TYPE_CMD:
1233277b024eSKalle Valo 		mwifiex_dbg(adapter, CMD,
1234277b024eSKalle Valo 			    "info: --- Rx: Cmd Response ---\n");
1235277b024eSKalle Valo 		/* take care of curr_cmd = NULL case */
1236277b024eSKalle Valo 		if (!adapter->curr_cmd) {
1237277b024eSKalle Valo 			cmd_buf = adapter->upld_buf;
1238277b024eSKalle Valo 
1239277b024eSKalle Valo 			if (adapter->ps_state == PS_STATE_SLEEP_CFM)
1240277b024eSKalle Valo 				mwifiex_process_sleep_confirm_resp(adapter,
1241277b024eSKalle Valo 								   skb->data,
1242277b024eSKalle Valo 								   skb->len);
1243277b024eSKalle Valo 
1244277b024eSKalle Valo 			memcpy(cmd_buf, skb->data,
1245277b024eSKalle Valo 			       min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER,
1246277b024eSKalle Valo 				     skb->len));
1247277b024eSKalle Valo 
1248277b024eSKalle Valo 			dev_kfree_skb_any(skb);
1249277b024eSKalle Valo 		} else {
1250277b024eSKalle Valo 			adapter->cmd_resp_received = true;
1251277b024eSKalle Valo 			adapter->curr_cmd->resp_skb = skb;
1252277b024eSKalle Valo 		}
1253277b024eSKalle Valo 		break;
1254277b024eSKalle Valo 
1255277b024eSKalle Valo 	case MWIFIEX_TYPE_EVENT:
1256277b024eSKalle Valo 		mwifiex_dbg(adapter, EVENT,
1257277b024eSKalle Valo 			    "info: --- Rx: Event ---\n");
1258277b024eSKalle Valo 		adapter->event_cause = le32_to_cpu(*(__le32 *) skb->data);
1259277b024eSKalle Valo 
1260277b024eSKalle Valo 		if ((skb->len > 0) && (skb->len  < MAX_EVENT_SIZE))
1261277b024eSKalle Valo 			memcpy(adapter->event_body,
1262277b024eSKalle Valo 			       skb->data + MWIFIEX_EVENT_HEADER_LEN,
1263277b024eSKalle Valo 			       skb->len);
1264277b024eSKalle Valo 
1265277b024eSKalle Valo 		/* event cause has been saved to adapter->event_cause */
1266277b024eSKalle Valo 		adapter->event_received = true;
1267277b024eSKalle Valo 		adapter->event_skb = skb;
1268277b024eSKalle Valo 
1269277b024eSKalle Valo 		break;
1270277b024eSKalle Valo 
1271277b024eSKalle Valo 	default:
1272277b024eSKalle Valo 		mwifiex_dbg(adapter, ERROR,
1273277b024eSKalle Valo 			    "unknown upload type %#x\n", upld_typ);
1274277b024eSKalle Valo 		dev_kfree_skb_any(skb);
1275277b024eSKalle Valo 		break;
1276277b024eSKalle Valo 	}
1277277b024eSKalle Valo 
1278277b024eSKalle Valo 	return 0;
1279277b024eSKalle Valo }
1280277b024eSKalle Valo 
1281277b024eSKalle Valo /*
1282277b024eSKalle Valo  * This function transfers received packets from card to driver, performing
1283277b024eSKalle Valo  * aggregation if required.
1284277b024eSKalle Valo  *
1285277b024eSKalle Valo  * For data received on control port, or if aggregation is disabled, the
1286277b024eSKalle Valo  * received buffers are uploaded as separate packets. However, if aggregation
1287277b024eSKalle Valo  * is enabled and required, the buffers are copied onto an aggregation buffer,
1288277b024eSKalle Valo  * provided there is space left, processed and finally uploaded.
1289277b024eSKalle Valo  */
1290277b024eSKalle Valo static int mwifiex_sdio_card_to_host_mp_aggr(struct mwifiex_adapter *adapter,
1291277b024eSKalle Valo 					     u16 rx_len, u8 port)
1292277b024eSKalle Valo {
1293277b024eSKalle Valo 	struct sdio_mmc_card *card = adapter->card;
1294277b024eSKalle Valo 	s32 f_do_rx_aggr = 0;
1295277b024eSKalle Valo 	s32 f_do_rx_cur = 0;
1296277b024eSKalle Valo 	s32 f_aggr_cur = 0;
1297277b024eSKalle Valo 	s32 f_post_aggr_cur = 0;
1298277b024eSKalle Valo 	struct sk_buff *skb_deaggr;
1299277b024eSKalle Valo 	struct sk_buff *skb = NULL;
1300277b024eSKalle Valo 	u32 pkt_len, pkt_type, mport, pind;
1301277b024eSKalle Valo 	u8 *curr_ptr;
1302277b024eSKalle Valo 
1303277b024eSKalle Valo 	if ((card->has_control_mask) && (port == CTRL_PORT)) {
1304277b024eSKalle Valo 		/* Read the command Resp without aggr */
1305277b024eSKalle Valo 		mwifiex_dbg(adapter, CMD,
1306277b024eSKalle Valo 			    "info: %s: no aggregation for cmd\t"
1307277b024eSKalle Valo 			    "response\n", __func__);
1308277b024eSKalle Valo 
1309277b024eSKalle Valo 		f_do_rx_cur = 1;
1310277b024eSKalle Valo 		goto rx_curr_single;
1311277b024eSKalle Valo 	}
1312277b024eSKalle Valo 
1313277b024eSKalle Valo 	if (!card->mpa_rx.enabled) {
1314277b024eSKalle Valo 		mwifiex_dbg(adapter, WARN,
1315277b024eSKalle Valo 			    "info: %s: rx aggregation disabled\n",
1316277b024eSKalle Valo 			    __func__);
1317277b024eSKalle Valo 
1318277b024eSKalle Valo 		f_do_rx_cur = 1;
1319277b024eSKalle Valo 		goto rx_curr_single;
1320277b024eSKalle Valo 	}
1321277b024eSKalle Valo 
1322277b024eSKalle Valo 	if ((!card->has_control_mask && (card->mp_rd_bitmap &
1323277b024eSKalle Valo 					 card->reg->data_port_mask)) ||
1324277b024eSKalle Valo 	    (card->has_control_mask && (card->mp_rd_bitmap &
1325277b024eSKalle Valo 					(~((u32) CTRL_PORT_MASK))))) {
1326277b024eSKalle Valo 		/* Some more data RX pending */
1327277b024eSKalle Valo 		mwifiex_dbg(adapter, INFO,
1328277b024eSKalle Valo 			    "info: %s: not last packet\n", __func__);
1329277b024eSKalle Valo 
1330277b024eSKalle Valo 		if (MP_RX_AGGR_IN_PROGRESS(card)) {
1331277b024eSKalle Valo 			if (MP_RX_AGGR_BUF_HAS_ROOM(card, rx_len)) {
1332277b024eSKalle Valo 				f_aggr_cur = 1;
1333277b024eSKalle Valo 			} else {
1334277b024eSKalle Valo 				/* No room in Aggr buf, do rx aggr now */
1335277b024eSKalle Valo 				f_do_rx_aggr = 1;
1336277b024eSKalle Valo 				f_post_aggr_cur = 1;
1337277b024eSKalle Valo 			}
1338277b024eSKalle Valo 		} else {
1339277b024eSKalle Valo 			/* Rx aggr not in progress */
1340277b024eSKalle Valo 			f_aggr_cur = 1;
1341277b024eSKalle Valo 		}
1342277b024eSKalle Valo 
1343277b024eSKalle Valo 	} else {
1344277b024eSKalle Valo 		/* No more data RX pending */
1345277b024eSKalle Valo 		mwifiex_dbg(adapter, INFO,
1346277b024eSKalle Valo 			    "info: %s: last packet\n", __func__);
1347277b024eSKalle Valo 
1348277b024eSKalle Valo 		if (MP_RX_AGGR_IN_PROGRESS(card)) {
1349277b024eSKalle Valo 			f_do_rx_aggr = 1;
1350277b024eSKalle Valo 			if (MP_RX_AGGR_BUF_HAS_ROOM(card, rx_len))
1351277b024eSKalle Valo 				f_aggr_cur = 1;
1352277b024eSKalle Valo 			else
1353277b024eSKalle Valo 				/* No room in Aggr buf, do rx aggr now */
1354277b024eSKalle Valo 				f_do_rx_cur = 1;
1355277b024eSKalle Valo 		} else {
1356277b024eSKalle Valo 			f_do_rx_cur = 1;
1357277b024eSKalle Valo 		}
1358277b024eSKalle Valo 	}
1359277b024eSKalle Valo 
1360277b024eSKalle Valo 	if (f_aggr_cur) {
1361277b024eSKalle Valo 		mwifiex_dbg(adapter, INFO,
1362277b024eSKalle Valo 			    "info: current packet aggregation\n");
1363277b024eSKalle Valo 		/* Curr pkt can be aggregated */
1364277b024eSKalle Valo 		mp_rx_aggr_setup(card, rx_len, port);
1365277b024eSKalle Valo 
1366277b024eSKalle Valo 		if (MP_RX_AGGR_PKT_LIMIT_REACHED(card) ||
1367277b024eSKalle Valo 		    mp_rx_aggr_port_limit_reached(card)) {
1368277b024eSKalle Valo 			mwifiex_dbg(adapter, INFO,
1369277b024eSKalle Valo 				    "info: %s: aggregated packet\t"
1370277b024eSKalle Valo 				    "limit reached\n", __func__);
1371277b024eSKalle Valo 			/* No more pkts allowed in Aggr buf, rx it */
1372277b024eSKalle Valo 			f_do_rx_aggr = 1;
1373277b024eSKalle Valo 		}
1374277b024eSKalle Valo 	}
1375277b024eSKalle Valo 
1376277b024eSKalle Valo 	if (f_do_rx_aggr) {
1377277b024eSKalle Valo 		/* do aggr RX now */
1378277b024eSKalle Valo 		mwifiex_dbg(adapter, DATA,
1379277b024eSKalle Valo 			    "info: do_rx_aggr: num of packets: %d\n",
1380277b024eSKalle Valo 			    card->mpa_rx.pkt_cnt);
1381277b024eSKalle Valo 
1382277b024eSKalle Valo 		if (card->supports_sdio_new_mode) {
1383277b024eSKalle Valo 			int i;
1384277b024eSKalle Valo 			u32 port_count;
1385277b024eSKalle Valo 
1386277b024eSKalle Valo 			for (i = 0, port_count = 0; i < card->max_ports; i++)
1387277b024eSKalle Valo 				if (card->mpa_rx.ports & BIT(i))
1388277b024eSKalle Valo 					port_count++;
1389277b024eSKalle Valo 
1390277b024eSKalle Valo 			/* Reading data from "start_port + 0" to "start_port +
1391277b024eSKalle Valo 			 * port_count -1", so decrease the count by 1
1392277b024eSKalle Valo 			 */
1393277b024eSKalle Valo 			port_count--;
1394277b024eSKalle Valo 			mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1395277b024eSKalle Valo 				 (port_count << 8)) + card->mpa_rx.start_port;
1396277b024eSKalle Valo 		} else {
1397277b024eSKalle Valo 			mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1398277b024eSKalle Valo 				 (card->mpa_rx.ports << 4)) +
1399277b024eSKalle Valo 				 card->mpa_rx.start_port;
1400277b024eSKalle Valo 		}
1401277b024eSKalle Valo 
14020cb52aacSXinming Hu 		if (card->mpa_rx.pkt_cnt == 1)
14030cb52aacSXinming Hu 			mport = adapter->ioport + port;
14040cb52aacSXinming Hu 
1405277b024eSKalle Valo 		if (mwifiex_read_data_sync(adapter, card->mpa_rx.buf,
1406277b024eSKalle Valo 					   card->mpa_rx.buf_len, mport, 1))
1407277b024eSKalle Valo 			goto error;
1408277b024eSKalle Valo 
1409277b024eSKalle Valo 		curr_ptr = card->mpa_rx.buf;
1410277b024eSKalle Valo 
1411277b024eSKalle Valo 		for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1412277b024eSKalle Valo 			u32 *len_arr = card->mpa_rx.len_arr;
1413277b024eSKalle Valo 
1414277b024eSKalle Valo 			/* get curr PKT len & type */
1415277b024eSKalle Valo 			pkt_len = le16_to_cpu(*(__le16 *) &curr_ptr[0]);
1416277b024eSKalle Valo 			pkt_type = le16_to_cpu(*(__le16 *) &curr_ptr[2]);
1417277b024eSKalle Valo 
1418277b024eSKalle Valo 			/* copy pkt to deaggr buf */
1419277b024eSKalle Valo 			skb_deaggr = mwifiex_alloc_dma_align_buf(len_arr[pind],
142000c54780SXinming Hu 								 GFP_KERNEL);
1421277b024eSKalle Valo 			if (!skb_deaggr) {
1422277b024eSKalle Valo 				mwifiex_dbg(adapter, ERROR, "skb allocation failure\t"
1423277b024eSKalle Valo 					    "drop pkt len=%d type=%d\n",
1424277b024eSKalle Valo 					    pkt_len, pkt_type);
1425277b024eSKalle Valo 				curr_ptr += len_arr[pind];
1426277b024eSKalle Valo 				continue;
1427277b024eSKalle Valo 			}
1428277b024eSKalle Valo 
1429277b024eSKalle Valo 			skb_put(skb_deaggr, len_arr[pind]);
1430277b024eSKalle Valo 
1431277b024eSKalle Valo 			if ((pkt_type == MWIFIEX_TYPE_DATA ||
1432277b024eSKalle Valo 			     (pkt_type == MWIFIEX_TYPE_AGGR_DATA &&
1433277b024eSKalle Valo 			      adapter->sdio_rx_aggr_enable)) &&
1434277b024eSKalle Valo 			    (pkt_len <= len_arr[pind])) {
1435277b024eSKalle Valo 
1436277b024eSKalle Valo 				memcpy(skb_deaggr->data, curr_ptr, pkt_len);
1437277b024eSKalle Valo 
1438277b024eSKalle Valo 				skb_trim(skb_deaggr, pkt_len);
1439277b024eSKalle Valo 
1440277b024eSKalle Valo 				/* Process de-aggr packet */
1441277b024eSKalle Valo 				mwifiex_decode_rx_packet(adapter, skb_deaggr,
1442277b024eSKalle Valo 							 pkt_type);
1443277b024eSKalle Valo 			} else {
1444277b024eSKalle Valo 				mwifiex_dbg(adapter, ERROR,
1445277b024eSKalle Valo 					    "drop wrong aggr pkt:\t"
1446277b024eSKalle Valo 					    "sdio_single_port_rx_aggr=%d\t"
1447277b024eSKalle Valo 					    "type=%d len=%d max_len=%d\n",
1448277b024eSKalle Valo 					    adapter->sdio_rx_aggr_enable,
1449277b024eSKalle Valo 					    pkt_type, pkt_len, len_arr[pind]);
1450277b024eSKalle Valo 				dev_kfree_skb_any(skb_deaggr);
1451277b024eSKalle Valo 			}
1452277b024eSKalle Valo 			curr_ptr += len_arr[pind];
1453277b024eSKalle Valo 		}
1454277b024eSKalle Valo 		MP_RX_AGGR_BUF_RESET(card);
1455277b024eSKalle Valo 	}
1456277b024eSKalle Valo 
1457277b024eSKalle Valo rx_curr_single:
1458277b024eSKalle Valo 	if (f_do_rx_cur) {
1459277b024eSKalle Valo 		mwifiex_dbg(adapter, INFO, "info: RX: port: %d, rx_len: %d\n",
1460277b024eSKalle Valo 			    port, rx_len);
1461277b024eSKalle Valo 
14625c87a55aSMathias Krause 		skb = mwifiex_alloc_dma_align_buf(rx_len, GFP_KERNEL);
1463277b024eSKalle Valo 		if (!skb) {
1464277b024eSKalle Valo 			mwifiex_dbg(adapter, ERROR,
1465277b024eSKalle Valo 				    "single skb allocated fail,\t"
1466277b024eSKalle Valo 				    "drop pkt port=%d len=%d\n", port, rx_len);
1467277b024eSKalle Valo 			if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1468277b024eSKalle Valo 						      card->mpa_rx.buf, rx_len,
1469277b024eSKalle Valo 						      adapter->ioport + port))
1470277b024eSKalle Valo 				goto error;
1471277b024eSKalle Valo 			return 0;
1472277b024eSKalle Valo 		}
1473277b024eSKalle Valo 
1474277b024eSKalle Valo 		skb_put(skb, rx_len);
1475277b024eSKalle Valo 
1476277b024eSKalle Valo 		if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1477277b024eSKalle Valo 					      skb->data, skb->len,
1478277b024eSKalle Valo 					      adapter->ioport + port))
1479277b024eSKalle Valo 			goto error;
1480277b024eSKalle Valo 		if (!adapter->sdio_rx_aggr_enable &&
1481277b024eSKalle Valo 		    pkt_type == MWIFIEX_TYPE_AGGR_DATA) {
1482277b024eSKalle Valo 			mwifiex_dbg(adapter, ERROR, "drop wrong pkt type %d\t"
1483277b024eSKalle Valo 				    "current SDIO RX Aggr not enabled\n",
1484277b024eSKalle Valo 				    pkt_type);
1485277b024eSKalle Valo 			dev_kfree_skb_any(skb);
1486277b024eSKalle Valo 			return 0;
1487277b024eSKalle Valo 		}
1488277b024eSKalle Valo 
1489277b024eSKalle Valo 		mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1490277b024eSKalle Valo 	}
1491277b024eSKalle Valo 	if (f_post_aggr_cur) {
1492277b024eSKalle Valo 		mwifiex_dbg(adapter, INFO,
1493277b024eSKalle Valo 			    "info: current packet aggregation\n");
1494277b024eSKalle Valo 		/* Curr pkt can be aggregated */
1495277b024eSKalle Valo 		mp_rx_aggr_setup(card, rx_len, port);
1496277b024eSKalle Valo 	}
1497277b024eSKalle Valo 
1498277b024eSKalle Valo 	return 0;
1499277b024eSKalle Valo error:
1500277b024eSKalle Valo 	if (MP_RX_AGGR_IN_PROGRESS(card))
1501277b024eSKalle Valo 		MP_RX_AGGR_BUF_RESET(card);
1502277b024eSKalle Valo 
1503277b024eSKalle Valo 	if (f_do_rx_cur && skb)
1504277b024eSKalle Valo 		/* Single transfer pending. Free curr buff also */
1505277b024eSKalle Valo 		dev_kfree_skb_any(skb);
1506277b024eSKalle Valo 
1507277b024eSKalle Valo 	return -1;
1508277b024eSKalle Valo }
1509277b024eSKalle Valo 
1510277b024eSKalle Valo /*
1511277b024eSKalle Valo  * This function checks the current interrupt status.
1512277b024eSKalle Valo  *
1513277b024eSKalle Valo  * The following interrupts are checked and handled by this function -
1514277b024eSKalle Valo  *      - Data sent
1515277b024eSKalle Valo  *      - Command sent
1516277b024eSKalle Valo  *      - Packets received
1517277b024eSKalle Valo  *
1518277b024eSKalle Valo  * Since the firmware does not generate download ready interrupt if the
1519277b024eSKalle Valo  * port updated is command port only, command sent interrupt checking
1520277b024eSKalle Valo  * should be done manually, and for every SDIO interrupt.
1521277b024eSKalle Valo  *
1522277b024eSKalle Valo  * In case of Rx packets received, the packets are uploaded from card to
1523277b024eSKalle Valo  * host and processed accordingly.
1524277b024eSKalle Valo  */
1525277b024eSKalle Valo static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
1526277b024eSKalle Valo {
1527277b024eSKalle Valo 	struct sdio_mmc_card *card = adapter->card;
1528277b024eSKalle Valo 	const struct mwifiex_sdio_card_reg *reg = card->reg;
1529277b024eSKalle Valo 	int ret = 0;
1530277b024eSKalle Valo 	u8 sdio_ireg;
1531277b024eSKalle Valo 	struct sk_buff *skb;
1532277b024eSKalle Valo 	u8 port = CTRL_PORT;
1533277b024eSKalle Valo 	u32 len_reg_l, len_reg_u;
1534277b024eSKalle Valo 	u32 rx_blocks;
1535277b024eSKalle Valo 	u16 rx_len;
1536277b024eSKalle Valo 	unsigned long flags;
1537277b024eSKalle Valo 	u32 bitmap;
1538277b024eSKalle Valo 	u8 cr;
1539277b024eSKalle Valo 
1540277b024eSKalle Valo 	spin_lock_irqsave(&adapter->int_lock, flags);
1541277b024eSKalle Valo 	sdio_ireg = adapter->int_status;
1542277b024eSKalle Valo 	adapter->int_status = 0;
1543277b024eSKalle Valo 	spin_unlock_irqrestore(&adapter->int_lock, flags);
1544277b024eSKalle Valo 
1545277b024eSKalle Valo 	if (!sdio_ireg)
1546277b024eSKalle Valo 		return ret;
1547277b024eSKalle Valo 
1548277b024eSKalle Valo 	/* Following interrupt is only for SDIO new mode */
1549277b024eSKalle Valo 	if (sdio_ireg & DN_LD_CMD_PORT_HOST_INT_STATUS && adapter->cmd_sent)
1550277b024eSKalle Valo 		adapter->cmd_sent = false;
1551277b024eSKalle Valo 
1552277b024eSKalle Valo 	/* Following interrupt is only for SDIO new mode */
1553277b024eSKalle Valo 	if (sdio_ireg & UP_LD_CMD_PORT_HOST_INT_STATUS) {
1554277b024eSKalle Valo 		u32 pkt_type;
1555277b024eSKalle Valo 
1556277b024eSKalle Valo 		/* read the len of control packet */
1557277b024eSKalle Valo 		rx_len = card->mp_regs[reg->cmd_rd_len_1] << 8;
1558277b024eSKalle Valo 		rx_len |= (u16)card->mp_regs[reg->cmd_rd_len_0];
1559277b024eSKalle Valo 		rx_blocks = DIV_ROUND_UP(rx_len, MWIFIEX_SDIO_BLOCK_SIZE);
1560277b024eSKalle Valo 		if (rx_len <= INTF_HEADER_LEN ||
1561277b024eSKalle Valo 		    (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1562277b024eSKalle Valo 		     MWIFIEX_RX_DATA_BUF_SIZE)
1563277b024eSKalle Valo 			return -1;
1564277b024eSKalle Valo 		rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1565277b024eSKalle Valo 		mwifiex_dbg(adapter, INFO, "info: rx_len = %d\n", rx_len);
1566277b024eSKalle Valo 
15675c87a55aSMathias Krause 		skb = mwifiex_alloc_dma_align_buf(rx_len, GFP_KERNEL);
1568277b024eSKalle Valo 		if (!skb)
1569277b024eSKalle Valo 			return -1;
1570277b024eSKalle Valo 
1571277b024eSKalle Valo 		skb_put(skb, rx_len);
1572277b024eSKalle Valo 
1573277b024eSKalle Valo 		if (mwifiex_sdio_card_to_host(adapter, &pkt_type, skb->data,
1574277b024eSKalle Valo 					      skb->len, adapter->ioport |
1575277b024eSKalle Valo 							CMD_PORT_SLCT)) {
1576277b024eSKalle Valo 			mwifiex_dbg(adapter, ERROR,
1577277b024eSKalle Valo 				    "%s: failed to card_to_host", __func__);
1578277b024eSKalle Valo 			dev_kfree_skb_any(skb);
1579277b024eSKalle Valo 			goto term_cmd;
1580277b024eSKalle Valo 		}
1581277b024eSKalle Valo 
1582277b024eSKalle Valo 		if ((pkt_type != MWIFIEX_TYPE_CMD) &&
1583277b024eSKalle Valo 		    (pkt_type != MWIFIEX_TYPE_EVENT))
1584277b024eSKalle Valo 			mwifiex_dbg(adapter, ERROR,
1585277b024eSKalle Valo 				    "%s:Received wrong packet on cmd port",
1586277b024eSKalle Valo 				    __func__);
1587277b024eSKalle Valo 
1588277b024eSKalle Valo 		mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1589277b024eSKalle Valo 	}
1590277b024eSKalle Valo 
1591277b024eSKalle Valo 	if (sdio_ireg & DN_LD_HOST_INT_STATUS) {
1592277b024eSKalle Valo 		bitmap = (u32) card->mp_regs[reg->wr_bitmap_l];
1593277b024eSKalle Valo 		bitmap |= ((u32) card->mp_regs[reg->wr_bitmap_u]) << 8;
1594277b024eSKalle Valo 		if (card->supports_sdio_new_mode) {
1595277b024eSKalle Valo 			bitmap |=
1596277b024eSKalle Valo 				((u32) card->mp_regs[reg->wr_bitmap_1l]) << 16;
1597277b024eSKalle Valo 			bitmap |=
1598277b024eSKalle Valo 				((u32) card->mp_regs[reg->wr_bitmap_1u]) << 24;
1599277b024eSKalle Valo 		}
1600277b024eSKalle Valo 		card->mp_wr_bitmap = bitmap;
1601277b024eSKalle Valo 
1602277b024eSKalle Valo 		mwifiex_dbg(adapter, INTR,
1603277b024eSKalle Valo 			    "int: DNLD: wr_bitmap=0x%x\n",
1604277b024eSKalle Valo 			    card->mp_wr_bitmap);
1605277b024eSKalle Valo 		if (adapter->data_sent &&
1606277b024eSKalle Valo 		    (card->mp_wr_bitmap & card->mp_data_port_mask)) {
1607277b024eSKalle Valo 			mwifiex_dbg(adapter, INTR,
1608277b024eSKalle Valo 				    "info:  <--- Tx DONE Interrupt --->\n");
1609277b024eSKalle Valo 			adapter->data_sent = false;
1610277b024eSKalle Valo 		}
1611277b024eSKalle Valo 	}
1612277b024eSKalle Valo 
1613277b024eSKalle Valo 	/* As firmware will not generate download ready interrupt if the port
1614277b024eSKalle Valo 	   updated is command port only, cmd_sent should be done for any SDIO
1615277b024eSKalle Valo 	   interrupt. */
1616277b024eSKalle Valo 	if (card->has_control_mask && adapter->cmd_sent) {
1617277b024eSKalle Valo 		/* Check if firmware has attach buffer at command port and
1618277b024eSKalle Valo 		   update just that in wr_bit_map. */
1619277b024eSKalle Valo 		card->mp_wr_bitmap |=
1620277b024eSKalle Valo 			(u32) card->mp_regs[reg->wr_bitmap_l] & CTRL_PORT_MASK;
1621277b024eSKalle Valo 		if (card->mp_wr_bitmap & CTRL_PORT_MASK)
1622277b024eSKalle Valo 			adapter->cmd_sent = false;
1623277b024eSKalle Valo 	}
1624277b024eSKalle Valo 
1625277b024eSKalle Valo 	mwifiex_dbg(adapter, INTR, "info: cmd_sent=%d data_sent=%d\n",
1626277b024eSKalle Valo 		    adapter->cmd_sent, adapter->data_sent);
1627277b024eSKalle Valo 	if (sdio_ireg & UP_LD_HOST_INT_STATUS) {
1628277b024eSKalle Valo 		bitmap = (u32) card->mp_regs[reg->rd_bitmap_l];
1629277b024eSKalle Valo 		bitmap |= ((u32) card->mp_regs[reg->rd_bitmap_u]) << 8;
1630277b024eSKalle Valo 		if (card->supports_sdio_new_mode) {
1631277b024eSKalle Valo 			bitmap |=
1632277b024eSKalle Valo 				((u32) card->mp_regs[reg->rd_bitmap_1l]) << 16;
1633277b024eSKalle Valo 			bitmap |=
1634277b024eSKalle Valo 				((u32) card->mp_regs[reg->rd_bitmap_1u]) << 24;
1635277b024eSKalle Valo 		}
1636277b024eSKalle Valo 		card->mp_rd_bitmap = bitmap;
1637277b024eSKalle Valo 		mwifiex_dbg(adapter, INTR,
1638277b024eSKalle Valo 			    "int: UPLD: rd_bitmap=0x%x\n",
1639277b024eSKalle Valo 			    card->mp_rd_bitmap);
1640277b024eSKalle Valo 
1641277b024eSKalle Valo 		while (true) {
1642277b024eSKalle Valo 			ret = mwifiex_get_rd_port(adapter, &port);
1643277b024eSKalle Valo 			if (ret) {
1644277b024eSKalle Valo 				mwifiex_dbg(adapter, INFO,
1645277b024eSKalle Valo 					    "info: no more rd_port available\n");
1646277b024eSKalle Valo 				break;
1647277b024eSKalle Valo 			}
1648277b024eSKalle Valo 			len_reg_l = reg->rd_len_p0_l + (port << 1);
1649277b024eSKalle Valo 			len_reg_u = reg->rd_len_p0_u + (port << 1);
1650277b024eSKalle Valo 			rx_len = ((u16) card->mp_regs[len_reg_u]) << 8;
1651277b024eSKalle Valo 			rx_len |= (u16) card->mp_regs[len_reg_l];
1652277b024eSKalle Valo 			mwifiex_dbg(adapter, INFO,
1653277b024eSKalle Valo 				    "info: RX: port=%d rx_len=%u\n",
1654277b024eSKalle Valo 				    port, rx_len);
1655277b024eSKalle Valo 			rx_blocks =
1656277b024eSKalle Valo 				(rx_len + MWIFIEX_SDIO_BLOCK_SIZE -
1657277b024eSKalle Valo 				 1) / MWIFIEX_SDIO_BLOCK_SIZE;
1658277b024eSKalle Valo 			if (rx_len <= INTF_HEADER_LEN ||
1659277b024eSKalle Valo 			    (card->mpa_rx.enabled &&
1660277b024eSKalle Valo 			     ((rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1661277b024eSKalle Valo 			      card->mpa_rx.buf_size))) {
1662277b024eSKalle Valo 				mwifiex_dbg(adapter, ERROR,
1663277b024eSKalle Valo 					    "invalid rx_len=%d\n",
1664277b024eSKalle Valo 					    rx_len);
1665277b024eSKalle Valo 				return -1;
1666277b024eSKalle Valo 			}
1667277b024eSKalle Valo 
1668277b024eSKalle Valo 			rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1669277b024eSKalle Valo 			mwifiex_dbg(adapter, INFO, "info: rx_len = %d\n",
1670277b024eSKalle Valo 				    rx_len);
1671277b024eSKalle Valo 
1672277b024eSKalle Valo 			if (mwifiex_sdio_card_to_host_mp_aggr(adapter, rx_len,
1673277b024eSKalle Valo 							      port)) {
1674277b024eSKalle Valo 				mwifiex_dbg(adapter, ERROR,
1675277b024eSKalle Valo 					    "card_to_host_mpa failed: int status=%#x\n",
1676277b024eSKalle Valo 					    sdio_ireg);
1677277b024eSKalle Valo 				goto term_cmd;
1678277b024eSKalle Valo 			}
1679277b024eSKalle Valo 		}
1680277b024eSKalle Valo 	}
1681277b024eSKalle Valo 
1682277b024eSKalle Valo 	return 0;
1683277b024eSKalle Valo 
1684277b024eSKalle Valo term_cmd:
1685277b024eSKalle Valo 	/* terminate cmd */
1686277b024eSKalle Valo 	if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
1687277b024eSKalle Valo 		mwifiex_dbg(adapter, ERROR, "read CFG reg failed\n");
1688277b024eSKalle Valo 	else
1689277b024eSKalle Valo 		mwifiex_dbg(adapter, INFO,
1690277b024eSKalle Valo 			    "info: CFG reg val = %d\n", cr);
1691277b024eSKalle Valo 
1692277b024eSKalle Valo 	if (mwifiex_write_reg(adapter, CONFIGURATION_REG, (cr | 0x04)))
1693277b024eSKalle Valo 		mwifiex_dbg(adapter, ERROR,
1694277b024eSKalle Valo 			    "write CFG reg failed\n");
1695277b024eSKalle Valo 	else
1696277b024eSKalle Valo 		mwifiex_dbg(adapter, INFO, "info: write success\n");
1697277b024eSKalle Valo 
1698277b024eSKalle Valo 	if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
1699277b024eSKalle Valo 		mwifiex_dbg(adapter, ERROR,
1700277b024eSKalle Valo 			    "read CFG reg failed\n");
1701277b024eSKalle Valo 	else
1702277b024eSKalle Valo 		mwifiex_dbg(adapter, INFO,
1703277b024eSKalle Valo 			    "info: CFG reg val =%x\n", cr);
1704277b024eSKalle Valo 
1705277b024eSKalle Valo 	return -1;
1706277b024eSKalle Valo }
1707277b024eSKalle Valo 
1708277b024eSKalle Valo /*
1709277b024eSKalle Valo  * This function aggregates transmission buffers in driver and downloads
1710277b024eSKalle Valo  * the aggregated packet to card.
1711277b024eSKalle Valo  *
1712277b024eSKalle Valo  * The individual packets are aggregated by copying into an aggregation
1713277b024eSKalle Valo  * buffer and then downloaded to the card. Previous unsent packets in the
1714277b024eSKalle Valo  * aggregation buffer are pre-copied first before new packets are added.
1715277b024eSKalle Valo  * Aggregation is done till there is space left in the aggregation buffer,
1716277b024eSKalle Valo  * or till new packets are available.
1717277b024eSKalle Valo  *
1718277b024eSKalle Valo  * The function will only download the packet to the card when aggregation
1719277b024eSKalle Valo  * stops, otherwise it will just aggregate the packet in aggregation buffer
1720277b024eSKalle Valo  * and return.
1721277b024eSKalle Valo  */
1722277b024eSKalle Valo static int mwifiex_host_to_card_mp_aggr(struct mwifiex_adapter *adapter,
1723277b024eSKalle Valo 					u8 *payload, u32 pkt_len, u32 port,
1724277b024eSKalle Valo 					u32 next_pkt_len)
1725277b024eSKalle Valo {
1726277b024eSKalle Valo 	struct sdio_mmc_card *card = adapter->card;
1727277b024eSKalle Valo 	int ret = 0;
1728277b024eSKalle Valo 	s32 f_send_aggr_buf = 0;
1729277b024eSKalle Valo 	s32 f_send_cur_buf = 0;
1730277b024eSKalle Valo 	s32 f_precopy_cur_buf = 0;
1731277b024eSKalle Valo 	s32 f_postcopy_cur_buf = 0;
1732277b024eSKalle Valo 	u32 mport;
17338b7ef8b6SXinming Hu 	int index;
1734277b024eSKalle Valo 
1735277b024eSKalle Valo 	if (!card->mpa_tx.enabled ||
1736277b024eSKalle Valo 	    (card->has_control_mask && (port == CTRL_PORT)) ||
1737277b024eSKalle Valo 	    (card->supports_sdio_new_mode && (port == CMD_PORT_SLCT))) {
1738277b024eSKalle Valo 		mwifiex_dbg(adapter, WARN,
1739277b024eSKalle Valo 			    "info: %s: tx aggregation disabled\n",
1740277b024eSKalle Valo 			    __func__);
1741277b024eSKalle Valo 
1742277b024eSKalle Valo 		f_send_cur_buf = 1;
1743277b024eSKalle Valo 		goto tx_curr_single;
1744277b024eSKalle Valo 	}
1745277b024eSKalle Valo 
1746277b024eSKalle Valo 	if (next_pkt_len) {
1747277b024eSKalle Valo 		/* More pkt in TX queue */
1748277b024eSKalle Valo 		mwifiex_dbg(adapter, INFO,
1749277b024eSKalle Valo 			    "info: %s: more packets in queue.\n",
1750277b024eSKalle Valo 			    __func__);
1751277b024eSKalle Valo 
1752277b024eSKalle Valo 		if (MP_TX_AGGR_IN_PROGRESS(card)) {
1753277b024eSKalle Valo 			if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len)) {
1754277b024eSKalle Valo 				f_precopy_cur_buf = 1;
1755277b024eSKalle Valo 
1756277b024eSKalle Valo 				if (!(card->mp_wr_bitmap &
1757277b024eSKalle Valo 				      (1 << card->curr_wr_port)) ||
1758277b024eSKalle Valo 				    !MP_TX_AGGR_BUF_HAS_ROOM(
1759277b024eSKalle Valo 					    card, pkt_len + next_pkt_len))
1760277b024eSKalle Valo 					f_send_aggr_buf = 1;
1761277b024eSKalle Valo 			} else {
1762277b024eSKalle Valo 				/* No room in Aggr buf, send it */
1763277b024eSKalle Valo 				f_send_aggr_buf = 1;
1764277b024eSKalle Valo 
1765277b024eSKalle Valo 				if (!(card->mp_wr_bitmap &
1766277b024eSKalle Valo 				      (1 << card->curr_wr_port)))
1767277b024eSKalle Valo 					f_send_cur_buf = 1;
1768277b024eSKalle Valo 				else
1769277b024eSKalle Valo 					f_postcopy_cur_buf = 1;
1770277b024eSKalle Valo 			}
1771277b024eSKalle Valo 		} else {
1772277b024eSKalle Valo 			if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len) &&
1773277b024eSKalle Valo 			    (card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1774277b024eSKalle Valo 				f_precopy_cur_buf = 1;
1775277b024eSKalle Valo 			else
1776277b024eSKalle Valo 				f_send_cur_buf = 1;
1777277b024eSKalle Valo 		}
1778277b024eSKalle Valo 	} else {
1779277b024eSKalle Valo 		/* Last pkt in TX queue */
1780277b024eSKalle Valo 		mwifiex_dbg(adapter, INFO,
1781277b024eSKalle Valo 			    "info: %s: Last packet in Tx Queue.\n",
1782277b024eSKalle Valo 			    __func__);
1783277b024eSKalle Valo 
1784277b024eSKalle Valo 		if (MP_TX_AGGR_IN_PROGRESS(card)) {
1785277b024eSKalle Valo 			/* some packs in Aggr buf already */
1786277b024eSKalle Valo 			f_send_aggr_buf = 1;
1787277b024eSKalle Valo 
1788277b024eSKalle Valo 			if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len))
1789277b024eSKalle Valo 				f_precopy_cur_buf = 1;
1790277b024eSKalle Valo 			else
1791277b024eSKalle Valo 				/* No room in Aggr buf, send it */
1792277b024eSKalle Valo 				f_send_cur_buf = 1;
1793277b024eSKalle Valo 		} else {
1794277b024eSKalle Valo 			f_send_cur_buf = 1;
1795277b024eSKalle Valo 		}
1796277b024eSKalle Valo 	}
1797277b024eSKalle Valo 
1798277b024eSKalle Valo 	if (f_precopy_cur_buf) {
1799277b024eSKalle Valo 		mwifiex_dbg(adapter, DATA,
1800277b024eSKalle Valo 			    "data: %s: precopy current buffer\n",
1801277b024eSKalle Valo 			    __func__);
1802277b024eSKalle Valo 		MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1803277b024eSKalle Valo 
1804277b024eSKalle Valo 		if (MP_TX_AGGR_PKT_LIMIT_REACHED(card) ||
1805277b024eSKalle Valo 		    mp_tx_aggr_port_limit_reached(card))
1806277b024eSKalle Valo 			/* No more pkts allowed in Aggr buf, send it */
1807277b024eSKalle Valo 			f_send_aggr_buf = 1;
1808277b024eSKalle Valo 	}
1809277b024eSKalle Valo 
1810277b024eSKalle Valo 	if (f_send_aggr_buf) {
1811277b024eSKalle Valo 		mwifiex_dbg(adapter, DATA,
1812277b024eSKalle Valo 			    "data: %s: send aggr buffer: %d %d\n",
1813277b024eSKalle Valo 			    __func__, card->mpa_tx.start_port,
1814277b024eSKalle Valo 			    card->mpa_tx.ports);
1815277b024eSKalle Valo 		if (card->supports_sdio_new_mode) {
1816277b024eSKalle Valo 			u32 port_count;
1817277b024eSKalle Valo 			int i;
1818277b024eSKalle Valo 
1819277b024eSKalle Valo 			for (i = 0, port_count = 0; i < card->max_ports; i++)
1820277b024eSKalle Valo 				if (card->mpa_tx.ports & BIT(i))
1821277b024eSKalle Valo 					port_count++;
1822277b024eSKalle Valo 
1823277b024eSKalle Valo 			/* Writing data from "start_port + 0" to "start_port +
1824277b024eSKalle Valo 			 * port_count -1", so decrease the count by 1
1825277b024eSKalle Valo 			 */
1826277b024eSKalle Valo 			port_count--;
1827277b024eSKalle Valo 			mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1828277b024eSKalle Valo 				 (port_count << 8)) + card->mpa_tx.start_port;
1829277b024eSKalle Valo 		} else {
1830277b024eSKalle Valo 			mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1831277b024eSKalle Valo 				 (card->mpa_tx.ports << 4)) +
1832277b024eSKalle Valo 				 card->mpa_tx.start_port;
1833277b024eSKalle Valo 		}
1834277b024eSKalle Valo 
18350cb52aacSXinming Hu 		if (card->mpa_tx.pkt_cnt == 1)
18360cb52aacSXinming Hu 			mport = adapter->ioport + port;
18370cb52aacSXinming Hu 
1838277b024eSKalle Valo 		ret = mwifiex_write_data_to_card(adapter, card->mpa_tx.buf,
1839277b024eSKalle Valo 						 card->mpa_tx.buf_len, mport);
1840277b024eSKalle Valo 
18418b7ef8b6SXinming Hu 		/* Save the last multi port tx aggreagation info to debug log */
18428b7ef8b6SXinming Hu 		index = adapter->dbg.last_sdio_mp_index;
18438b7ef8b6SXinming Hu 		index = (index + 1) % MWIFIEX_DBG_SDIO_MP_NUM;
18448b7ef8b6SXinming Hu 		adapter->dbg.last_sdio_mp_index = index;
18458b7ef8b6SXinming Hu 		adapter->dbg.last_mp_wr_ports[index] = mport;
18468b7ef8b6SXinming Hu 		adapter->dbg.last_mp_wr_bitmap[index] = card->mp_wr_bitmap;
18478b7ef8b6SXinming Hu 		adapter->dbg.last_mp_wr_len[index] = card->mpa_tx.buf_len;
18488b7ef8b6SXinming Hu 		adapter->dbg.last_mp_curr_wr_port[index] = card->curr_wr_port;
18498b7ef8b6SXinming Hu 
1850277b024eSKalle Valo 		MP_TX_AGGR_BUF_RESET(card);
1851277b024eSKalle Valo 	}
1852277b024eSKalle Valo 
1853277b024eSKalle Valo tx_curr_single:
1854277b024eSKalle Valo 	if (f_send_cur_buf) {
1855277b024eSKalle Valo 		mwifiex_dbg(adapter, DATA,
1856277b024eSKalle Valo 			    "data: %s: send current buffer %d\n",
1857277b024eSKalle Valo 			    __func__, port);
1858277b024eSKalle Valo 		ret = mwifiex_write_data_to_card(adapter, payload, pkt_len,
1859277b024eSKalle Valo 						 adapter->ioport + port);
1860277b024eSKalle Valo 	}
1861277b024eSKalle Valo 
1862277b024eSKalle Valo 	if (f_postcopy_cur_buf) {
1863277b024eSKalle Valo 		mwifiex_dbg(adapter, DATA,
1864277b024eSKalle Valo 			    "data: %s: postcopy current buffer\n",
1865277b024eSKalle Valo 			    __func__);
1866277b024eSKalle Valo 		MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1867277b024eSKalle Valo 	}
1868277b024eSKalle Valo 
1869277b024eSKalle Valo 	return ret;
1870277b024eSKalle Valo }
1871277b024eSKalle Valo 
1872277b024eSKalle Valo /*
1873277b024eSKalle Valo  * This function downloads data from driver to card.
1874277b024eSKalle Valo  *
1875277b024eSKalle Valo  * Both commands and data packets are transferred to the card by this
1876277b024eSKalle Valo  * function.
1877277b024eSKalle Valo  *
1878277b024eSKalle Valo  * This function adds the SDIO specific header to the front of the buffer
1879277b024eSKalle Valo  * before transferring. The header contains the length of the packet and
1880277b024eSKalle Valo  * the type. The firmware handles the packets based upon this set type.
1881277b024eSKalle Valo  */
1882277b024eSKalle Valo static int mwifiex_sdio_host_to_card(struct mwifiex_adapter *adapter,
1883277b024eSKalle Valo 				     u8 type, struct sk_buff *skb,
1884277b024eSKalle Valo 				     struct mwifiex_tx_param *tx_param)
1885277b024eSKalle Valo {
1886277b024eSKalle Valo 	struct sdio_mmc_card *card = adapter->card;
1887277b024eSKalle Valo 	int ret;
1888277b024eSKalle Valo 	u32 buf_block_len;
1889277b024eSKalle Valo 	u32 blk_size;
1890277b024eSKalle Valo 	u32 port = CTRL_PORT;
1891277b024eSKalle Valo 	u8 *payload = (u8 *)skb->data;
1892277b024eSKalle Valo 	u32 pkt_len = skb->len;
1893277b024eSKalle Valo 
1894277b024eSKalle Valo 	/* Allocate buffer and copy payload */
1895277b024eSKalle Valo 	blk_size = MWIFIEX_SDIO_BLOCK_SIZE;
1896277b024eSKalle Valo 	buf_block_len = (pkt_len + blk_size - 1) / blk_size;
1897277b024eSKalle Valo 	*(__le16 *)&payload[0] = cpu_to_le16((u16)pkt_len);
1898277b024eSKalle Valo 	*(__le16 *)&payload[2] = cpu_to_le16(type);
1899277b024eSKalle Valo 
1900277b024eSKalle Valo 	/*
1901277b024eSKalle Valo 	 * This is SDIO specific header
1902277b024eSKalle Valo 	 *  u16 length,
1903277b024eSKalle Valo 	 *  u16 type (MWIFIEX_TYPE_DATA = 0, MWIFIEX_TYPE_CMD = 1,
1904277b024eSKalle Valo 	 *  MWIFIEX_TYPE_EVENT = 3)
1905277b024eSKalle Valo 	 */
1906277b024eSKalle Valo 	if (type == MWIFIEX_TYPE_DATA) {
1907277b024eSKalle Valo 		ret = mwifiex_get_wr_port_data(adapter, &port);
1908277b024eSKalle Valo 		if (ret) {
1909277b024eSKalle Valo 			mwifiex_dbg(adapter, ERROR,
1910277b024eSKalle Valo 				    "%s: no wr_port available\n",
1911277b024eSKalle Valo 				    __func__);
1912277b024eSKalle Valo 			return ret;
1913277b024eSKalle Valo 		}
1914277b024eSKalle Valo 	} else {
1915277b024eSKalle Valo 		adapter->cmd_sent = true;
1916277b024eSKalle Valo 		/* Type must be MWIFIEX_TYPE_CMD */
1917277b024eSKalle Valo 
1918277b024eSKalle Valo 		if (pkt_len <= INTF_HEADER_LEN ||
1919277b024eSKalle Valo 		    pkt_len > MWIFIEX_UPLD_SIZE)
1920277b024eSKalle Valo 			mwifiex_dbg(adapter, ERROR,
1921277b024eSKalle Valo 				    "%s: payload=%p, nb=%d\n",
1922277b024eSKalle Valo 				    __func__, payload, pkt_len);
1923277b024eSKalle Valo 
1924277b024eSKalle Valo 		if (card->supports_sdio_new_mode)
1925277b024eSKalle Valo 			port = CMD_PORT_SLCT;
1926277b024eSKalle Valo 	}
1927277b024eSKalle Valo 
1928277b024eSKalle Valo 	/* Transfer data to card */
1929277b024eSKalle Valo 	pkt_len = buf_block_len * blk_size;
1930277b024eSKalle Valo 
1931277b024eSKalle Valo 	if (tx_param)
1932277b024eSKalle Valo 		ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1933277b024eSKalle Valo 						   port, tx_param->next_pkt_len
1934277b024eSKalle Valo 						   );
1935277b024eSKalle Valo 	else
1936277b024eSKalle Valo 		ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1937277b024eSKalle Valo 						   port, 0);
1938277b024eSKalle Valo 
1939277b024eSKalle Valo 	if (ret) {
1940277b024eSKalle Valo 		if (type == MWIFIEX_TYPE_CMD)
1941277b024eSKalle Valo 			adapter->cmd_sent = false;
1942277b024eSKalle Valo 		if (type == MWIFIEX_TYPE_DATA) {
1943277b024eSKalle Valo 			adapter->data_sent = false;
1944277b024eSKalle Valo 			/* restore curr_wr_port in error cases */
1945277b024eSKalle Valo 			card->curr_wr_port = port;
1946277b024eSKalle Valo 			card->mp_wr_bitmap |= (u32)(1 << card->curr_wr_port);
1947277b024eSKalle Valo 		}
1948277b024eSKalle Valo 	} else {
1949277b024eSKalle Valo 		if (type == MWIFIEX_TYPE_DATA) {
1950277b024eSKalle Valo 			if (!(card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1951277b024eSKalle Valo 				adapter->data_sent = true;
1952277b024eSKalle Valo 			else
1953277b024eSKalle Valo 				adapter->data_sent = false;
1954277b024eSKalle Valo 		}
1955277b024eSKalle Valo 	}
1956277b024eSKalle Valo 
1957277b024eSKalle Valo 	return ret;
1958277b024eSKalle Valo }
1959277b024eSKalle Valo 
1960277b024eSKalle Valo /*
1961277b024eSKalle Valo  * This function allocates the MPA Tx and Rx buffers.
1962277b024eSKalle Valo  */
1963277b024eSKalle Valo static int mwifiex_alloc_sdio_mpa_buffers(struct mwifiex_adapter *adapter,
1964277b024eSKalle Valo 				   u32 mpa_tx_buf_size, u32 mpa_rx_buf_size)
1965277b024eSKalle Valo {
1966277b024eSKalle Valo 	struct sdio_mmc_card *card = adapter->card;
1967277b024eSKalle Valo 	u32 rx_buf_size;
1968277b024eSKalle Valo 	int ret = 0;
1969277b024eSKalle Valo 
1970277b024eSKalle Valo 	card->mpa_tx.buf = kzalloc(mpa_tx_buf_size, GFP_KERNEL);
1971277b024eSKalle Valo 	if (!card->mpa_tx.buf) {
1972277b024eSKalle Valo 		ret = -1;
1973277b024eSKalle Valo 		goto error;
1974277b024eSKalle Valo 	}
1975277b024eSKalle Valo 
1976277b024eSKalle Valo 	card->mpa_tx.buf_size = mpa_tx_buf_size;
1977277b024eSKalle Valo 
1978277b024eSKalle Valo 	rx_buf_size = max_t(u32, mpa_rx_buf_size,
1979277b024eSKalle Valo 			    (u32)SDIO_MAX_AGGR_BUF_SIZE);
1980277b024eSKalle Valo 	card->mpa_rx.buf = kzalloc(rx_buf_size, GFP_KERNEL);
1981277b024eSKalle Valo 	if (!card->mpa_rx.buf) {
1982277b024eSKalle Valo 		ret = -1;
1983277b024eSKalle Valo 		goto error;
1984277b024eSKalle Valo 	}
1985277b024eSKalle Valo 
1986277b024eSKalle Valo 	card->mpa_rx.buf_size = rx_buf_size;
1987277b024eSKalle Valo 
1988277b024eSKalle Valo error:
1989277b024eSKalle Valo 	if (ret) {
1990277b024eSKalle Valo 		kfree(card->mpa_tx.buf);
1991277b024eSKalle Valo 		kfree(card->mpa_rx.buf);
1992277b024eSKalle Valo 		card->mpa_tx.buf_size = 0;
1993277b024eSKalle Valo 		card->mpa_rx.buf_size = 0;
1994277b024eSKalle Valo 	}
1995277b024eSKalle Valo 
1996277b024eSKalle Valo 	return ret;
1997277b024eSKalle Valo }
1998277b024eSKalle Valo 
1999277b024eSKalle Valo /*
2000277b024eSKalle Valo  * This function unregisters the SDIO device.
2001277b024eSKalle Valo  *
2002277b024eSKalle Valo  * The SDIO IRQ is released, the function is disabled and driver
2003277b024eSKalle Valo  * data is set to null.
2004277b024eSKalle Valo  */
2005277b024eSKalle Valo static void
2006277b024eSKalle Valo mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
2007277b024eSKalle Valo {
2008277b024eSKalle Valo 	struct sdio_mmc_card *card = adapter->card;
2009277b024eSKalle Valo 
2010277b024eSKalle Valo 	if (adapter->card) {
2011bcf28a2fSXinming Hu 		card->adapter = NULL;
2012277b024eSKalle Valo 		sdio_claim_host(card->func);
2013277b024eSKalle Valo 		sdio_disable_func(card->func);
2014277b024eSKalle Valo 		sdio_release_host(card->func);
2015277b024eSKalle Valo 	}
2016277b024eSKalle Valo }
2017277b024eSKalle Valo 
2018277b024eSKalle Valo /*
2019277b024eSKalle Valo  * This function registers the SDIO device.
2020277b024eSKalle Valo  *
2021277b024eSKalle Valo  * SDIO IRQ is claimed, block size is set and driver data is initialized.
2022277b024eSKalle Valo  */
2023277b024eSKalle Valo static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
2024277b024eSKalle Valo {
2025277b024eSKalle Valo 	int ret;
2026277b024eSKalle Valo 	struct sdio_mmc_card *card = adapter->card;
2027277b024eSKalle Valo 	struct sdio_func *func = card->func;
2028277b024eSKalle Valo 
2029277b024eSKalle Valo 	/* save adapter pointer in card */
2030277b024eSKalle Valo 	card->adapter = adapter;
2031277b024eSKalle Valo 	adapter->tx_buf_size = card->tx_buf_size;
2032277b024eSKalle Valo 
2033277b024eSKalle Valo 	sdio_claim_host(func);
2034277b024eSKalle Valo 
2035277b024eSKalle Valo 	/* Set block size */
2036277b024eSKalle Valo 	ret = sdio_set_block_size(card->func, MWIFIEX_SDIO_BLOCK_SIZE);
2037277b024eSKalle Valo 	sdio_release_host(func);
2038277b024eSKalle Valo 	if (ret) {
2039277b024eSKalle Valo 		mwifiex_dbg(adapter, ERROR,
2040277b024eSKalle Valo 			    "cannot set SDIO block size\n");
2041277b024eSKalle Valo 		return ret;
2042277b024eSKalle Valo 	}
2043277b024eSKalle Valo 
2044277b024eSKalle Valo 	strcpy(adapter->fw_name, card->firmware);
2045277b024eSKalle Valo 	if (card->fw_dump_enh) {
2046277b024eSKalle Valo 		adapter->mem_type_mapping_tbl = generic_mem_type_map;
2047277b024eSKalle Valo 		adapter->num_mem_types = 1;
2048277b024eSKalle Valo 	} else {
2049277b024eSKalle Valo 		adapter->mem_type_mapping_tbl = mem_type_mapping_tbl;
2050277b024eSKalle Valo 		adapter->num_mem_types = ARRAY_SIZE(mem_type_mapping_tbl);
2051277b024eSKalle Valo 	}
2052277b024eSKalle Valo 
2053277b024eSKalle Valo 	return 0;
2054277b024eSKalle Valo }
2055277b024eSKalle Valo 
2056277b024eSKalle Valo /*
2057277b024eSKalle Valo  * This function initializes the SDIO driver.
2058277b024eSKalle Valo  *
2059277b024eSKalle Valo  * The following initializations steps are followed -
2060277b024eSKalle Valo  *      - Read the Host interrupt status register to acknowledge
2061277b024eSKalle Valo  *        the first interrupt got from bootloader
2062277b024eSKalle Valo  *      - Disable host interrupt mask register
2063277b024eSKalle Valo  *      - Get SDIO port
2064277b024eSKalle Valo  *      - Initialize SDIO variables in card
2065277b024eSKalle Valo  *      - Allocate MP registers
2066277b024eSKalle Valo  *      - Allocate MPA Tx and Rx buffers
2067277b024eSKalle Valo  */
2068277b024eSKalle Valo static int mwifiex_init_sdio(struct mwifiex_adapter *adapter)
2069277b024eSKalle Valo {
2070277b024eSKalle Valo 	struct sdio_mmc_card *card = adapter->card;
2071277b024eSKalle Valo 	const struct mwifiex_sdio_card_reg *reg = card->reg;
2072277b024eSKalle Valo 	int ret;
2073277b024eSKalle Valo 	u8 sdio_ireg;
2074277b024eSKalle Valo 
2075277b024eSKalle Valo 	sdio_set_drvdata(card->func, card);
2076277b024eSKalle Valo 
2077277b024eSKalle Valo 	/*
2078277b024eSKalle Valo 	 * Read the host_int_status_reg for ACK the first interrupt got
2079277b024eSKalle Valo 	 * from the bootloader. If we don't do this we get a interrupt
2080277b024eSKalle Valo 	 * as soon as we register the irq.
2081277b024eSKalle Valo 	 */
2082277b024eSKalle Valo 	mwifiex_read_reg(adapter, card->reg->host_int_status_reg, &sdio_ireg);
2083277b024eSKalle Valo 
2084277b024eSKalle Valo 	/* Get SDIO ioport */
2085277b024eSKalle Valo 	mwifiex_init_sdio_ioport(adapter);
2086277b024eSKalle Valo 
2087277b024eSKalle Valo 	/* Initialize SDIO variables in card */
2088277b024eSKalle Valo 	card->mp_rd_bitmap = 0;
2089277b024eSKalle Valo 	card->mp_wr_bitmap = 0;
2090277b024eSKalle Valo 	card->curr_rd_port = reg->start_rd_port;
2091277b024eSKalle Valo 	card->curr_wr_port = reg->start_wr_port;
2092277b024eSKalle Valo 
2093277b024eSKalle Valo 	card->mp_data_port_mask = reg->data_port_mask;
2094277b024eSKalle Valo 
2095277b024eSKalle Valo 	card->mpa_tx.buf_len = 0;
2096277b024eSKalle Valo 	card->mpa_tx.pkt_cnt = 0;
2097277b024eSKalle Valo 	card->mpa_tx.start_port = 0;
2098277b024eSKalle Valo 
2099277b024eSKalle Valo 	card->mpa_tx.enabled = 1;
2100277b024eSKalle Valo 	card->mpa_tx.pkt_aggr_limit = card->mp_agg_pkt_limit;
2101277b024eSKalle Valo 
2102277b024eSKalle Valo 	card->mpa_rx.buf_len = 0;
2103277b024eSKalle Valo 	card->mpa_rx.pkt_cnt = 0;
2104277b024eSKalle Valo 	card->mpa_rx.start_port = 0;
2105277b024eSKalle Valo 
2106277b024eSKalle Valo 	card->mpa_rx.enabled = 1;
2107277b024eSKalle Valo 	card->mpa_rx.pkt_aggr_limit = card->mp_agg_pkt_limit;
2108277b024eSKalle Valo 
2109277b024eSKalle Valo 	/* Allocate buffers for SDIO MP-A */
2110277b024eSKalle Valo 	card->mp_regs = kzalloc(reg->max_mp_regs, GFP_KERNEL);
2111277b024eSKalle Valo 	if (!card->mp_regs)
2112277b024eSKalle Valo 		return -ENOMEM;
2113277b024eSKalle Valo 
2114277b024eSKalle Valo 	/* Allocate skb pointer buffers */
2115277b024eSKalle Valo 	card->mpa_rx.skb_arr = kzalloc((sizeof(void *)) *
2116277b024eSKalle Valo 				       card->mp_agg_pkt_limit, GFP_KERNEL);
211750f85e22SInsu Yun 	if (!card->mpa_rx.skb_arr) {
211850f85e22SInsu Yun 		kfree(card->mp_regs);
211950f85e22SInsu Yun 		return -ENOMEM;
212050f85e22SInsu Yun 	}
212150f85e22SInsu Yun 
2122277b024eSKalle Valo 	card->mpa_rx.len_arr = kzalloc(sizeof(*card->mpa_rx.len_arr) *
2123277b024eSKalle Valo 				       card->mp_agg_pkt_limit, GFP_KERNEL);
212450f85e22SInsu Yun 	if (!card->mpa_rx.len_arr) {
212550f85e22SInsu Yun 		kfree(card->mp_regs);
212650f85e22SInsu Yun 		kfree(card->mpa_rx.skb_arr);
212750f85e22SInsu Yun 		return -ENOMEM;
212850f85e22SInsu Yun 	}
212950f85e22SInsu Yun 
2130277b024eSKalle Valo 	ret = mwifiex_alloc_sdio_mpa_buffers(adapter,
2131277b024eSKalle Valo 					     card->mp_tx_agg_buf_size,
2132277b024eSKalle Valo 					     card->mp_rx_agg_buf_size);
2133277b024eSKalle Valo 
2134277b024eSKalle Valo 	/* Allocate 32k MPA Tx/Rx buffers if 64k memory allocation fails */
2135277b024eSKalle Valo 	if (ret && (card->mp_tx_agg_buf_size == MWIFIEX_MP_AGGR_BUF_SIZE_MAX ||
2136277b024eSKalle Valo 		    card->mp_rx_agg_buf_size == MWIFIEX_MP_AGGR_BUF_SIZE_MAX)) {
2137277b024eSKalle Valo 		/* Disable rx single port aggregation */
2138277b024eSKalle Valo 		adapter->host_disable_sdio_rx_aggr = true;
2139277b024eSKalle Valo 
2140277b024eSKalle Valo 		ret = mwifiex_alloc_sdio_mpa_buffers
2141277b024eSKalle Valo 			(adapter, MWIFIEX_MP_AGGR_BUF_SIZE_32K,
2142277b024eSKalle Valo 			 MWIFIEX_MP_AGGR_BUF_SIZE_32K);
2143277b024eSKalle Valo 		if (ret) {
2144277b024eSKalle Valo 			/* Disable multi port aggregation */
2145277b024eSKalle Valo 			card->mpa_tx.enabled = 0;
2146277b024eSKalle Valo 			card->mpa_rx.enabled = 0;
2147277b024eSKalle Valo 		}
2148277b024eSKalle Valo 	}
2149277b024eSKalle Valo 
2150277b024eSKalle Valo 	adapter->auto_tdls = card->can_auto_tdls;
2151277b024eSKalle Valo 	adapter->ext_scan = card->can_ext_scan;
2152277b024eSKalle Valo 	return 0;
2153277b024eSKalle Valo }
2154277b024eSKalle Valo 
2155277b024eSKalle Valo /*
2156277b024eSKalle Valo  * This function resets the MPA Tx and Rx buffers.
2157277b024eSKalle Valo  */
2158277b024eSKalle Valo static void mwifiex_cleanup_mpa_buf(struct mwifiex_adapter *adapter)
2159277b024eSKalle Valo {
2160277b024eSKalle Valo 	struct sdio_mmc_card *card = adapter->card;
2161277b024eSKalle Valo 
2162277b024eSKalle Valo 	MP_TX_AGGR_BUF_RESET(card);
2163277b024eSKalle Valo 	MP_RX_AGGR_BUF_RESET(card);
2164277b024eSKalle Valo }
2165277b024eSKalle Valo 
2166277b024eSKalle Valo /*
2167277b024eSKalle Valo  * This function cleans up the allocated card buffers.
2168277b024eSKalle Valo  *
2169277b024eSKalle Valo  * The following are freed by this function -
2170277b024eSKalle Valo  *      - MP registers
2171277b024eSKalle Valo  *      - MPA Tx buffer
2172277b024eSKalle Valo  *      - MPA Rx buffer
2173277b024eSKalle Valo  */
2174277b024eSKalle Valo static void mwifiex_cleanup_sdio(struct mwifiex_adapter *adapter)
2175277b024eSKalle Valo {
2176277b024eSKalle Valo 	struct sdio_mmc_card *card = adapter->card;
2177277b024eSKalle Valo 
2178277b024eSKalle Valo 	kfree(card->mp_regs);
2179277b024eSKalle Valo 	kfree(card->mpa_rx.skb_arr);
2180277b024eSKalle Valo 	kfree(card->mpa_rx.len_arr);
2181277b024eSKalle Valo 	kfree(card->mpa_tx.buf);
2182277b024eSKalle Valo 	kfree(card->mpa_rx.buf);
2183277b024eSKalle Valo }
2184277b024eSKalle Valo 
2185277b024eSKalle Valo /*
2186277b024eSKalle Valo  * This function updates the MP end port in card.
2187277b024eSKalle Valo  */
2188277b024eSKalle Valo static void
2189277b024eSKalle Valo mwifiex_update_mp_end_port(struct mwifiex_adapter *adapter, u16 port)
2190277b024eSKalle Valo {
2191277b024eSKalle Valo 	struct sdio_mmc_card *card = adapter->card;
2192277b024eSKalle Valo 	const struct mwifiex_sdio_card_reg *reg = card->reg;
2193277b024eSKalle Valo 	int i;
2194277b024eSKalle Valo 
2195277b024eSKalle Valo 	card->mp_end_port = port;
2196277b024eSKalle Valo 
2197277b024eSKalle Valo 	card->mp_data_port_mask = reg->data_port_mask;
2198277b024eSKalle Valo 
2199277b024eSKalle Valo 	if (reg->start_wr_port) {
2200277b024eSKalle Valo 		for (i = 1; i <= card->max_ports - card->mp_end_port; i++)
2201277b024eSKalle Valo 			card->mp_data_port_mask &=
2202277b024eSKalle Valo 					~(1 << (card->max_ports - i));
2203277b024eSKalle Valo 	}
2204277b024eSKalle Valo 
2205277b024eSKalle Valo 	card->curr_wr_port = reg->start_wr_port;
2206277b024eSKalle Valo 
2207277b024eSKalle Valo 	mwifiex_dbg(adapter, CMD,
2208277b024eSKalle Valo 		    "cmd: mp_end_port %d, data port mask 0x%x\n",
2209277b024eSKalle Valo 		    port, card->mp_data_port_mask);
2210277b024eSKalle Valo }
2211277b024eSKalle Valo 
2212277b024eSKalle Valo static void mwifiex_recreate_adapter(struct sdio_mmc_card *card)
2213277b024eSKalle Valo {
2214277b024eSKalle Valo 	struct sdio_func *func = card->func;
2215277b024eSKalle Valo 	const struct sdio_device_id *device_id = card->device_id;
2216277b024eSKalle Valo 
2217277b024eSKalle Valo 	/* TODO mmc_hw_reset does not require destroying and re-probing the
2218277b024eSKalle Valo 	 * whole adapter. Hence there was no need to for this rube-goldberg
2219277b024eSKalle Valo 	 * design to reload the fw from an external workqueue. If we don't
2220277b024eSKalle Valo 	 * destroy the adapter we could reload the fw from
2221277b024eSKalle Valo 	 * mwifiex_main_work_queue directly.
2222277b024eSKalle Valo 	 * The real difficulty with fw reset is to restore all the user
2223277b024eSKalle Valo 	 * settings applied through ioctl. By destroying and recreating the
2224277b024eSKalle Valo 	 * adapter, we take the easy way out, since we rely on user space to
2225277b024eSKalle Valo 	 * restore them. We assume that user space will treat the new
2226277b024eSKalle Valo 	 * incarnation of the adapter(interfaces) as if they had been just
2227277b024eSKalle Valo 	 * discovered and initializes them from scratch.
2228277b024eSKalle Valo 	 */
2229277b024eSKalle Valo 
2230277b024eSKalle Valo 	mwifiex_sdio_remove(func);
2231277b024eSKalle Valo 
223266b9c182SBrian Norris 	/*
223366b9c182SBrian Norris 	 * Normally, we would let the driver core take care of releasing these.
223466b9c182SBrian Norris 	 * But we're not letting the driver core handle this one. See above
223566b9c182SBrian Norris 	 * TODO.
223666b9c182SBrian Norris 	 */
223766b9c182SBrian Norris 	sdio_set_drvdata(func, NULL);
223866b9c182SBrian Norris 	devm_kfree(&func->dev, card);
223966b9c182SBrian Norris 
2240277b024eSKalle Valo 	/* power cycle the adapter */
2241277b024eSKalle Valo 	sdio_claim_host(func);
2242277b024eSKalle Valo 	mmc_hw_reset(func->card->host);
2243277b024eSKalle Valo 	sdio_release_host(func);
2244277b024eSKalle Valo 
2245277b024eSKalle Valo 	mwifiex_sdio_probe(func, device_id);
2246277b024eSKalle Valo }
2247277b024eSKalle Valo 
2248277b024eSKalle Valo static struct mwifiex_adapter *save_adapter;
2249277b024eSKalle Valo static void mwifiex_sdio_card_reset_work(struct mwifiex_adapter *adapter)
2250277b024eSKalle Valo {
2251277b024eSKalle Valo 	struct sdio_mmc_card *card = adapter->card;
2252277b024eSKalle Valo 
2253277b024eSKalle Valo 	/* TODO card pointer is unprotected. If the adapter is removed
2254277b024eSKalle Valo 	 * physically, sdio core might trigger mwifiex_sdio_remove, before this
2255277b024eSKalle Valo 	 * workqueue is run, which will destroy the adapter struct. When this
2256277b024eSKalle Valo 	 * workqueue eventually exceutes it will dereference an invalid adapter
2257277b024eSKalle Valo 	 * pointer
2258277b024eSKalle Valo 	 */
2259277b024eSKalle Valo 	mwifiex_recreate_adapter(card);
2260277b024eSKalle Valo }
2261277b024eSKalle Valo 
2262277b024eSKalle Valo /* This function read/write firmware */
2263277b024eSKalle Valo static enum
2264277b024eSKalle Valo rdwr_status mwifiex_sdio_rdwr_firmware(struct mwifiex_adapter *adapter,
2265277b024eSKalle Valo 				       u8 doneflag)
2266277b024eSKalle Valo {
2267277b024eSKalle Valo 	struct sdio_mmc_card *card = adapter->card;
2268277b024eSKalle Valo 	int ret, tries;
2269277b024eSKalle Valo 	u8 ctrl_data = 0;
2270277b024eSKalle Valo 
2271277b024eSKalle Valo 	sdio_writeb(card->func, card->reg->fw_dump_host_ready,
2272277b024eSKalle Valo 		    card->reg->fw_dump_ctrl, &ret);
2273277b024eSKalle Valo 	if (ret) {
2274277b024eSKalle Valo 		mwifiex_dbg(adapter, ERROR, "SDIO Write ERR\n");
2275277b024eSKalle Valo 		return RDWR_STATUS_FAILURE;
2276277b024eSKalle Valo 	}
2277277b024eSKalle Valo 	for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
2278277b024eSKalle Valo 		ctrl_data = sdio_readb(card->func, card->reg->fw_dump_ctrl,
2279277b024eSKalle Valo 				       &ret);
2280277b024eSKalle Valo 		if (ret) {
2281277b024eSKalle Valo 			mwifiex_dbg(adapter, ERROR, "SDIO read err\n");
2282277b024eSKalle Valo 			return RDWR_STATUS_FAILURE;
2283277b024eSKalle Valo 		}
2284277b024eSKalle Valo 		if (ctrl_data == FW_DUMP_DONE)
2285277b024eSKalle Valo 			break;
2286277b024eSKalle Valo 		if (doneflag && ctrl_data == doneflag)
2287277b024eSKalle Valo 			return RDWR_STATUS_DONE;
2288277b024eSKalle Valo 		if (ctrl_data != card->reg->fw_dump_host_ready) {
2289277b024eSKalle Valo 			mwifiex_dbg(adapter, WARN,
2290277b024eSKalle Valo 				    "The ctrl reg was changed, re-try again\n");
2291277b024eSKalle Valo 			sdio_writeb(card->func, card->reg->fw_dump_host_ready,
2292277b024eSKalle Valo 				    card->reg->fw_dump_ctrl, &ret);
2293277b024eSKalle Valo 			if (ret) {
2294277b024eSKalle Valo 				mwifiex_dbg(adapter, ERROR, "SDIO write err\n");
2295277b024eSKalle Valo 				return RDWR_STATUS_FAILURE;
2296277b024eSKalle Valo 			}
2297277b024eSKalle Valo 		}
2298277b024eSKalle Valo 		usleep_range(100, 200);
2299277b024eSKalle Valo 	}
2300277b024eSKalle Valo 	if (ctrl_data == card->reg->fw_dump_host_ready) {
2301277b024eSKalle Valo 		mwifiex_dbg(adapter, ERROR,
2302277b024eSKalle Valo 			    "Fail to pull ctrl_data\n");
2303277b024eSKalle Valo 		return RDWR_STATUS_FAILURE;
2304277b024eSKalle Valo 	}
2305277b024eSKalle Valo 
2306277b024eSKalle Valo 	return RDWR_STATUS_SUCCESS;
2307277b024eSKalle Valo }
2308277b024eSKalle Valo 
2309277b024eSKalle Valo /* This function dump firmware memory to file */
2310277b024eSKalle Valo static void mwifiex_sdio_fw_dump(struct mwifiex_adapter *adapter)
2311277b024eSKalle Valo {
2312277b024eSKalle Valo 	struct sdio_mmc_card *card = adapter->card;
2313277b024eSKalle Valo 	int ret = 0;
2314277b024eSKalle Valo 	unsigned int reg, reg_start, reg_end;
2315277b024eSKalle Valo 	u8 *dbg_ptr, *end_ptr, dump_num, idx, i, read_reg, doneflag = 0;
2316277b024eSKalle Valo 	enum rdwr_status stat;
2317277b024eSKalle Valo 	u32 memory_size;
2318277b024eSKalle Valo 
2319277b024eSKalle Valo 	if (!card->can_dump_fw)
2320277b024eSKalle Valo 		return;
2321277b024eSKalle Valo 
2322277b024eSKalle Valo 	for (idx = 0; idx < ARRAY_SIZE(mem_type_mapping_tbl); idx++) {
2323277b024eSKalle Valo 		struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
2324277b024eSKalle Valo 
2325277b024eSKalle Valo 		if (entry->mem_ptr) {
2326277b024eSKalle Valo 			vfree(entry->mem_ptr);
2327277b024eSKalle Valo 			entry->mem_ptr = NULL;
2328277b024eSKalle Valo 		}
2329277b024eSKalle Valo 		entry->mem_size = 0;
2330277b024eSKalle Valo 	}
2331277b024eSKalle Valo 
2332277b024eSKalle Valo 	mwifiex_pm_wakeup_card(adapter);
2333277b024eSKalle Valo 	sdio_claim_host(card->func);
2334277b024eSKalle Valo 
2335277b024eSKalle Valo 	mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump start ==\n");
2336277b024eSKalle Valo 
2337277b024eSKalle Valo 	stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2338277b024eSKalle Valo 	if (stat == RDWR_STATUS_FAILURE)
2339277b024eSKalle Valo 		goto done;
2340277b024eSKalle Valo 
2341277b024eSKalle Valo 	reg = card->reg->fw_dump_start;
2342277b024eSKalle Valo 	/* Read the number of the memories which will dump */
2343277b024eSKalle Valo 	dump_num = sdio_readb(card->func, reg, &ret);
2344277b024eSKalle Valo 	if (ret) {
2345277b024eSKalle Valo 		mwifiex_dbg(adapter, ERROR, "SDIO read memory length err\n");
2346277b024eSKalle Valo 		goto done;
2347277b024eSKalle Valo 	}
2348277b024eSKalle Valo 
2349277b024eSKalle Valo 	/* Read the length of every memory which will dump */
2350277b024eSKalle Valo 	for (idx = 0; idx < dump_num; idx++) {
2351277b024eSKalle Valo 		struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
2352277b024eSKalle Valo 
2353277b024eSKalle Valo 		stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2354277b024eSKalle Valo 		if (stat == RDWR_STATUS_FAILURE)
2355277b024eSKalle Valo 			goto done;
2356277b024eSKalle Valo 
2357277b024eSKalle Valo 		memory_size = 0;
2358277b024eSKalle Valo 		reg = card->reg->fw_dump_start;
2359277b024eSKalle Valo 		for (i = 0; i < 4; i++) {
2360277b024eSKalle Valo 			read_reg = sdio_readb(card->func, reg, &ret);
2361277b024eSKalle Valo 			if (ret) {
2362277b024eSKalle Valo 				mwifiex_dbg(adapter, ERROR, "SDIO read err\n");
2363277b024eSKalle Valo 				goto done;
2364277b024eSKalle Valo 			}
2365277b024eSKalle Valo 			memory_size |= (read_reg << i*8);
2366277b024eSKalle Valo 			reg++;
2367277b024eSKalle Valo 		}
2368277b024eSKalle Valo 
2369277b024eSKalle Valo 		if (memory_size == 0) {
2370277b024eSKalle Valo 			mwifiex_dbg(adapter, DUMP, "Firmware dump Finished!\n");
2371277b024eSKalle Valo 			ret = mwifiex_write_reg(adapter,
2372277b024eSKalle Valo 						card->reg->fw_dump_ctrl,
2373277b024eSKalle Valo 						FW_DUMP_READ_DONE);
2374277b024eSKalle Valo 			if (ret) {
2375277b024eSKalle Valo 				mwifiex_dbg(adapter, ERROR, "SDIO write err\n");
2376277b024eSKalle Valo 				return;
2377277b024eSKalle Valo 			}
2378277b024eSKalle Valo 			break;
2379277b024eSKalle Valo 		}
2380277b024eSKalle Valo 
2381277b024eSKalle Valo 		mwifiex_dbg(adapter, DUMP,
2382277b024eSKalle Valo 			    "%s_SIZE=0x%x\n", entry->mem_name, memory_size);
2383277b024eSKalle Valo 		entry->mem_ptr = vmalloc(memory_size + 1);
2384277b024eSKalle Valo 		entry->mem_size = memory_size;
2385277b024eSKalle Valo 		if (!entry->mem_ptr) {
2386277b024eSKalle Valo 			mwifiex_dbg(adapter, ERROR, "Vmalloc %s failed\n",
2387277b024eSKalle Valo 				    entry->mem_name);
2388277b024eSKalle Valo 			goto done;
2389277b024eSKalle Valo 		}
2390277b024eSKalle Valo 		dbg_ptr = entry->mem_ptr;
2391277b024eSKalle Valo 		end_ptr = dbg_ptr + memory_size;
2392277b024eSKalle Valo 
2393277b024eSKalle Valo 		doneflag = entry->done_flag;
2394277b024eSKalle Valo 		mwifiex_dbg(adapter, DUMP,
2395277b024eSKalle Valo 			    "Start %s output, please wait...\n",
2396277b024eSKalle Valo 			    entry->mem_name);
2397277b024eSKalle Valo 
2398277b024eSKalle Valo 		do {
2399277b024eSKalle Valo 			stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2400277b024eSKalle Valo 			if (stat == RDWR_STATUS_FAILURE)
2401277b024eSKalle Valo 				goto done;
2402277b024eSKalle Valo 
2403277b024eSKalle Valo 			reg_start = card->reg->fw_dump_start;
2404277b024eSKalle Valo 			reg_end = card->reg->fw_dump_end;
2405277b024eSKalle Valo 			for (reg = reg_start; reg <= reg_end; reg++) {
2406277b024eSKalle Valo 				*dbg_ptr = sdio_readb(card->func, reg, &ret);
2407277b024eSKalle Valo 				if (ret) {
2408277b024eSKalle Valo 					mwifiex_dbg(adapter, ERROR,
2409277b024eSKalle Valo 						    "SDIO read err\n");
2410277b024eSKalle Valo 					goto done;
2411277b024eSKalle Valo 				}
2412277b024eSKalle Valo 				if (dbg_ptr < end_ptr)
2413277b024eSKalle Valo 					dbg_ptr++;
2414277b024eSKalle Valo 				else
2415277b024eSKalle Valo 					mwifiex_dbg(adapter, ERROR,
2416277b024eSKalle Valo 						    "Allocated buf not enough\n");
2417277b024eSKalle Valo 			}
2418277b024eSKalle Valo 
2419277b024eSKalle Valo 			if (stat != RDWR_STATUS_DONE)
2420277b024eSKalle Valo 				continue;
2421277b024eSKalle Valo 
2422277b024eSKalle Valo 			mwifiex_dbg(adapter, DUMP, "%s done: size=0x%tx\n",
2423277b024eSKalle Valo 				    entry->mem_name, dbg_ptr - entry->mem_ptr);
2424277b024eSKalle Valo 			break;
2425277b024eSKalle Valo 		} while (1);
2426277b024eSKalle Valo 	}
2427277b024eSKalle Valo 	mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump end ==\n");
2428277b024eSKalle Valo 
2429277b024eSKalle Valo done:
2430277b024eSKalle Valo 	sdio_release_host(card->func);
2431277b024eSKalle Valo }
2432277b024eSKalle Valo 
2433277b024eSKalle Valo static void mwifiex_sdio_generic_fw_dump(struct mwifiex_adapter *adapter)
2434277b024eSKalle Valo {
2435277b024eSKalle Valo 	struct sdio_mmc_card *card = adapter->card;
2436277b024eSKalle Valo 	struct memory_type_mapping *entry = &generic_mem_type_map[0];
2437277b024eSKalle Valo 	unsigned int reg, reg_start, reg_end;
2438277b024eSKalle Valo 	u8 start_flag = 0, done_flag = 0;
2439277b024eSKalle Valo 	u8 *dbg_ptr, *end_ptr;
2440277b024eSKalle Valo 	enum rdwr_status stat;
2441277b024eSKalle Valo 	int ret = -1, tries;
2442277b024eSKalle Valo 
2443277b024eSKalle Valo 	if (!card->fw_dump_enh)
2444277b024eSKalle Valo 		return;
2445277b024eSKalle Valo 
2446277b024eSKalle Valo 	if (entry->mem_ptr) {
2447277b024eSKalle Valo 		vfree(entry->mem_ptr);
2448277b024eSKalle Valo 		entry->mem_ptr = NULL;
2449277b024eSKalle Valo 	}
2450277b024eSKalle Valo 	entry->mem_size = 0;
2451277b024eSKalle Valo 
2452277b024eSKalle Valo 	mwifiex_pm_wakeup_card(adapter);
2453277b024eSKalle Valo 	sdio_claim_host(card->func);
2454277b024eSKalle Valo 
2455277b024eSKalle Valo 	mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump start ==\n");
2456277b024eSKalle Valo 
2457277b024eSKalle Valo 	stat = mwifiex_sdio_rdwr_firmware(adapter, done_flag);
2458277b024eSKalle Valo 	if (stat == RDWR_STATUS_FAILURE)
2459277b024eSKalle Valo 		goto done;
2460277b024eSKalle Valo 
2461277b024eSKalle Valo 	reg_start = card->reg->fw_dump_start;
2462277b024eSKalle Valo 	reg_end = card->reg->fw_dump_end;
2463277b024eSKalle Valo 	for (reg = reg_start; reg <= reg_end; reg++) {
2464277b024eSKalle Valo 		for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
2465277b024eSKalle Valo 			start_flag = sdio_readb(card->func, reg, &ret);
2466277b024eSKalle Valo 			if (ret) {
2467277b024eSKalle Valo 				mwifiex_dbg(adapter, ERROR,
2468277b024eSKalle Valo 					    "SDIO read err\n");
2469277b024eSKalle Valo 				goto done;
2470277b024eSKalle Valo 			}
2471277b024eSKalle Valo 			if (start_flag == 0)
2472277b024eSKalle Valo 				break;
2473277b024eSKalle Valo 			if (tries == MAX_POLL_TRIES) {
2474277b024eSKalle Valo 				mwifiex_dbg(adapter, ERROR,
2475277b024eSKalle Valo 					    "FW not ready to dump\n");
2476277b024eSKalle Valo 				ret = -1;
2477277b024eSKalle Valo 				goto done;
2478277b024eSKalle Valo 			}
2479277b024eSKalle Valo 		}
2480277b024eSKalle Valo 		usleep_range(100, 200);
2481277b024eSKalle Valo 	}
2482277b024eSKalle Valo 
2483277b024eSKalle Valo 	entry->mem_ptr = vmalloc(0xf0000 + 1);
2484277b024eSKalle Valo 	if (!entry->mem_ptr) {
2485277b024eSKalle Valo 		ret = -1;
2486277b024eSKalle Valo 		goto done;
2487277b024eSKalle Valo 	}
2488277b024eSKalle Valo 	dbg_ptr = entry->mem_ptr;
2489277b024eSKalle Valo 	entry->mem_size = 0xf0000;
2490277b024eSKalle Valo 	end_ptr = dbg_ptr + entry->mem_size;
2491277b024eSKalle Valo 
2492277b024eSKalle Valo 	done_flag = entry->done_flag;
2493277b024eSKalle Valo 	mwifiex_dbg(adapter, DUMP,
2494277b024eSKalle Valo 		    "Start %s output, please wait...\n", entry->mem_name);
2495277b024eSKalle Valo 
2496277b024eSKalle Valo 	while (true) {
2497277b024eSKalle Valo 		stat = mwifiex_sdio_rdwr_firmware(adapter, done_flag);
2498277b024eSKalle Valo 		if (stat == RDWR_STATUS_FAILURE)
2499277b024eSKalle Valo 			goto done;
2500277b024eSKalle Valo 		for (reg = reg_start; reg <= reg_end; reg++) {
2501277b024eSKalle Valo 			*dbg_ptr = sdio_readb(card->func, reg, &ret);
2502277b024eSKalle Valo 			if (ret) {
2503277b024eSKalle Valo 				mwifiex_dbg(adapter, ERROR,
2504277b024eSKalle Valo 					    "SDIO read err\n");
2505277b024eSKalle Valo 				goto done;
2506277b024eSKalle Valo 			}
2507277b024eSKalle Valo 			dbg_ptr++;
2508277b024eSKalle Valo 			if (dbg_ptr >= end_ptr) {
2509277b024eSKalle Valo 				u8 *tmp_ptr;
2510277b024eSKalle Valo 
2511277b024eSKalle Valo 				tmp_ptr = vmalloc(entry->mem_size + 0x4000 + 1);
2512277b024eSKalle Valo 				if (!tmp_ptr)
2513277b024eSKalle Valo 					goto done;
2514277b024eSKalle Valo 
2515277b024eSKalle Valo 				memcpy(tmp_ptr, entry->mem_ptr,
2516277b024eSKalle Valo 				       entry->mem_size);
2517277b024eSKalle Valo 				vfree(entry->mem_ptr);
2518277b024eSKalle Valo 				entry->mem_ptr = tmp_ptr;
2519277b024eSKalle Valo 				tmp_ptr = NULL;
2520277b024eSKalle Valo 				dbg_ptr = entry->mem_ptr + entry->mem_size;
2521277b024eSKalle Valo 				entry->mem_size += 0x4000;
2522277b024eSKalle Valo 				end_ptr = entry->mem_ptr + entry->mem_size;
2523277b024eSKalle Valo 			}
2524277b024eSKalle Valo 		}
2525277b024eSKalle Valo 		if (stat == RDWR_STATUS_DONE) {
2526277b024eSKalle Valo 			entry->mem_size = dbg_ptr - entry->mem_ptr;
2527277b024eSKalle Valo 			mwifiex_dbg(adapter, DUMP, "dump %s done size=0x%x\n",
2528277b024eSKalle Valo 				    entry->mem_name, entry->mem_size);
2529277b024eSKalle Valo 			ret = 0;
2530277b024eSKalle Valo 			break;
2531277b024eSKalle Valo 		}
2532277b024eSKalle Valo 	}
2533277b024eSKalle Valo 	mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump end ==\n");
2534277b024eSKalle Valo 
2535277b024eSKalle Valo done:
2536277b024eSKalle Valo 	if (ret) {
2537277b024eSKalle Valo 		mwifiex_dbg(adapter, ERROR, "firmware dump failed\n");
2538277b024eSKalle Valo 		if (entry->mem_ptr) {
2539277b024eSKalle Valo 			vfree(entry->mem_ptr);
2540277b024eSKalle Valo 			entry->mem_ptr = NULL;
2541277b024eSKalle Valo 		}
2542277b024eSKalle Valo 		entry->mem_size = 0;
2543277b024eSKalle Valo 	}
2544277b024eSKalle Valo 	sdio_release_host(card->func);
2545277b024eSKalle Valo }
2546277b024eSKalle Valo 
2547277b024eSKalle Valo static void mwifiex_sdio_device_dump_work(struct mwifiex_adapter *adapter)
2548277b024eSKalle Valo {
2549277b024eSKalle Valo 	struct sdio_mmc_card *card = adapter->card;
2550277b024eSKalle Valo 
2551277b024eSKalle Valo 	mwifiex_drv_info_dump(adapter);
2552277b024eSKalle Valo 	if (card->fw_dump_enh)
2553277b024eSKalle Valo 		mwifiex_sdio_generic_fw_dump(adapter);
2554277b024eSKalle Valo 	else
2555277b024eSKalle Valo 		mwifiex_sdio_fw_dump(adapter);
2556277b024eSKalle Valo 	mwifiex_upload_device_dump(adapter);
2557277b024eSKalle Valo }
2558277b024eSKalle Valo 
2559277b024eSKalle Valo static void mwifiex_sdio_work(struct work_struct *work)
2560277b024eSKalle Valo {
2561277b024eSKalle Valo 	if (test_and_clear_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP,
2562277b024eSKalle Valo 			       &iface_work_flags))
2563277b024eSKalle Valo 		mwifiex_sdio_device_dump_work(save_adapter);
2564277b024eSKalle Valo 	if (test_and_clear_bit(MWIFIEX_IFACE_WORK_CARD_RESET,
2565277b024eSKalle Valo 			       &iface_work_flags))
2566277b024eSKalle Valo 		mwifiex_sdio_card_reset_work(save_adapter);
2567277b024eSKalle Valo }
2568277b024eSKalle Valo 
2569277b024eSKalle Valo static DECLARE_WORK(sdio_work, mwifiex_sdio_work);
2570277b024eSKalle Valo /* This function resets the card */
2571277b024eSKalle Valo static void mwifiex_sdio_card_reset(struct mwifiex_adapter *adapter)
2572277b024eSKalle Valo {
2573277b024eSKalle Valo 	save_adapter = adapter;
2574277b024eSKalle Valo 	if (test_bit(MWIFIEX_IFACE_WORK_CARD_RESET, &iface_work_flags))
2575277b024eSKalle Valo 		return;
2576277b024eSKalle Valo 
2577277b024eSKalle Valo 	set_bit(MWIFIEX_IFACE_WORK_CARD_RESET, &iface_work_flags);
2578277b024eSKalle Valo 
2579277b024eSKalle Valo 	schedule_work(&sdio_work);
2580277b024eSKalle Valo }
2581277b024eSKalle Valo 
2582277b024eSKalle Valo /* This function dumps FW information */
2583277b024eSKalle Valo static void mwifiex_sdio_device_dump(struct mwifiex_adapter *adapter)
2584277b024eSKalle Valo {
2585277b024eSKalle Valo 	save_adapter = adapter;
2586277b024eSKalle Valo 	if (test_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP, &iface_work_flags))
2587277b024eSKalle Valo 		return;
2588277b024eSKalle Valo 
2589277b024eSKalle Valo 	set_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP, &iface_work_flags);
2590277b024eSKalle Valo 	schedule_work(&sdio_work);
2591277b024eSKalle Valo }
2592277b024eSKalle Valo 
2593277b024eSKalle Valo /* Function to dump SDIO function registers and SDIO scratch registers in case
2594277b024eSKalle Valo  * of FW crash
2595277b024eSKalle Valo  */
2596277b024eSKalle Valo static int
2597277b024eSKalle Valo mwifiex_sdio_reg_dump(struct mwifiex_adapter *adapter, char *drv_buf)
2598277b024eSKalle Valo {
2599277b024eSKalle Valo 	char *p = drv_buf;
2600277b024eSKalle Valo 	struct sdio_mmc_card *cardp = adapter->card;
2601277b024eSKalle Valo 	int ret = 0;
2602277b024eSKalle Valo 	u8 count, func, data, index = 0, size = 0;
2603277b024eSKalle Valo 	u8 reg, reg_start, reg_end;
2604277b024eSKalle Valo 	char buf[256], *ptr;
2605277b024eSKalle Valo 
2606277b024eSKalle Valo 	if (!p)
2607277b024eSKalle Valo 		return 0;
2608277b024eSKalle Valo 
2609277b024eSKalle Valo 	mwifiex_dbg(adapter, MSG, "SDIO register dump start\n");
2610277b024eSKalle Valo 
2611277b024eSKalle Valo 	mwifiex_pm_wakeup_card(adapter);
2612277b024eSKalle Valo 
2613277b024eSKalle Valo 	sdio_claim_host(cardp->func);
2614277b024eSKalle Valo 
2615277b024eSKalle Valo 	for (count = 0; count < 5; count++) {
2616277b024eSKalle Valo 		memset(buf, 0, sizeof(buf));
2617277b024eSKalle Valo 		ptr = buf;
2618277b024eSKalle Valo 
2619277b024eSKalle Valo 		switch (count) {
2620277b024eSKalle Valo 		case 0:
2621277b024eSKalle Valo 			/* Read the registers of SDIO function0 */
2622277b024eSKalle Valo 			func = count;
2623277b024eSKalle Valo 			reg_start = 0;
2624277b024eSKalle Valo 			reg_end = 9;
2625277b024eSKalle Valo 			break;
2626277b024eSKalle Valo 		case 1:
2627277b024eSKalle Valo 			/* Read the registers of SDIO function1 */
2628277b024eSKalle Valo 			func = count;
2629277b024eSKalle Valo 			reg_start = cardp->reg->func1_dump_reg_start;
2630277b024eSKalle Valo 			reg_end = cardp->reg->func1_dump_reg_end;
2631277b024eSKalle Valo 			break;
2632277b024eSKalle Valo 		case 2:
2633277b024eSKalle Valo 			index = 0;
2634277b024eSKalle Valo 			func = 1;
2635277b024eSKalle Valo 			reg_start = cardp->reg->func1_spec_reg_table[index++];
2636277b024eSKalle Valo 			size = cardp->reg->func1_spec_reg_num;
2637277b024eSKalle Valo 			reg_end = cardp->reg->func1_spec_reg_table[size-1];
2638277b024eSKalle Valo 			break;
2639277b024eSKalle Valo 		default:
2640277b024eSKalle Valo 			/* Read the scratch registers of SDIO function1 */
2641277b024eSKalle Valo 			if (count == 4)
2642277b024eSKalle Valo 				mdelay(100);
2643277b024eSKalle Valo 			func = 1;
2644277b024eSKalle Valo 			reg_start = cardp->reg->func1_scratch_reg;
2645277b024eSKalle Valo 			reg_end = reg_start + MWIFIEX_SDIO_SCRATCH_SIZE;
2646277b024eSKalle Valo 		}
2647277b024eSKalle Valo 
2648277b024eSKalle Valo 		if (count != 2)
2649277b024eSKalle Valo 			ptr += sprintf(ptr, "SDIO Func%d (%#x-%#x): ",
2650277b024eSKalle Valo 				       func, reg_start, reg_end);
2651277b024eSKalle Valo 		else
2652277b024eSKalle Valo 			ptr += sprintf(ptr, "SDIO Func%d: ", func);
2653277b024eSKalle Valo 
2654277b024eSKalle Valo 		for (reg = reg_start; reg <= reg_end;) {
2655277b024eSKalle Valo 			if (func == 0)
2656277b024eSKalle Valo 				data = sdio_f0_readb(cardp->func, reg, &ret);
2657277b024eSKalle Valo 			else
2658277b024eSKalle Valo 				data = sdio_readb(cardp->func, reg, &ret);
2659277b024eSKalle Valo 
2660277b024eSKalle Valo 			if (count == 2)
2661277b024eSKalle Valo 				ptr += sprintf(ptr, "(%#x) ", reg);
2662277b024eSKalle Valo 			if (!ret) {
2663277b024eSKalle Valo 				ptr += sprintf(ptr, "%02x ", data);
2664277b024eSKalle Valo 			} else {
2665277b024eSKalle Valo 				ptr += sprintf(ptr, "ERR");
2666277b024eSKalle Valo 				break;
2667277b024eSKalle Valo 			}
2668277b024eSKalle Valo 
2669277b024eSKalle Valo 			if (count == 2 && reg < reg_end)
2670277b024eSKalle Valo 				reg = cardp->reg->func1_spec_reg_table[index++];
2671277b024eSKalle Valo 			else
2672277b024eSKalle Valo 				reg++;
2673277b024eSKalle Valo 		}
2674277b024eSKalle Valo 
2675277b024eSKalle Valo 		mwifiex_dbg(adapter, MSG, "%s\n", buf);
2676277b024eSKalle Valo 		p += sprintf(p, "%s\n", buf);
2677277b024eSKalle Valo 	}
2678277b024eSKalle Valo 
2679277b024eSKalle Valo 	sdio_release_host(cardp->func);
2680277b024eSKalle Valo 
2681277b024eSKalle Valo 	mwifiex_dbg(adapter, MSG, "SDIO register dump end\n");
2682277b024eSKalle Valo 
2683277b024eSKalle Valo 	return p - drv_buf;
2684277b024eSKalle Valo }
2685277b024eSKalle Valo 
2686277b024eSKalle Valo static struct mwifiex_if_ops sdio_ops = {
2687277b024eSKalle Valo 	.init_if = mwifiex_init_sdio,
2688277b024eSKalle Valo 	.cleanup_if = mwifiex_cleanup_sdio,
2689277b024eSKalle Valo 	.check_fw_status = mwifiex_check_fw_status,
26902fd5c6edSchunfan chen 	.check_winner_status = mwifiex_check_winner_status,
2691277b024eSKalle Valo 	.prog_fw = mwifiex_prog_fw_w_helper,
2692277b024eSKalle Valo 	.register_dev = mwifiex_register_dev,
2693277b024eSKalle Valo 	.unregister_dev = mwifiex_unregister_dev,
2694277b024eSKalle Valo 	.enable_int = mwifiex_sdio_enable_host_int,
2695277b024eSKalle Valo 	.disable_int = mwifiex_sdio_disable_host_int,
2696277b024eSKalle Valo 	.process_int_status = mwifiex_process_int_status,
2697277b024eSKalle Valo 	.host_to_card = mwifiex_sdio_host_to_card,
2698277b024eSKalle Valo 	.wakeup = mwifiex_pm_wakeup_card,
2699277b024eSKalle Valo 	.wakeup_complete = mwifiex_pm_wakeup_card_complete,
2700277b024eSKalle Valo 
2701277b024eSKalle Valo 	/* SDIO specific */
2702277b024eSKalle Valo 	.update_mp_end_port = mwifiex_update_mp_end_port,
2703277b024eSKalle Valo 	.cleanup_mpa_buf = mwifiex_cleanup_mpa_buf,
2704277b024eSKalle Valo 	.cmdrsp_complete = mwifiex_sdio_cmdrsp_complete,
2705277b024eSKalle Valo 	.event_complete = mwifiex_sdio_event_complete,
27062095b142SArnd Bergmann 	.dnld_fw = mwifiex_sdio_dnld_fw,
2707277b024eSKalle Valo 	.card_reset = mwifiex_sdio_card_reset,
2708277b024eSKalle Valo 	.reg_dump = mwifiex_sdio_reg_dump,
2709277b024eSKalle Valo 	.device_dump = mwifiex_sdio_device_dump,
2710277b024eSKalle Valo 	.deaggr_pkt = mwifiex_deaggr_sdio_pkt,
2711277b024eSKalle Valo };
2712277b024eSKalle Valo 
2713277b024eSKalle Valo /*
2714277b024eSKalle Valo  * This function initializes the SDIO driver.
2715277b024eSKalle Valo  *
27164a79aa17SBrian Norris  * This registers the device with SDIO bus.
2717277b024eSKalle Valo  */
2718277b024eSKalle Valo static int
2719277b024eSKalle Valo mwifiex_sdio_init_module(void)
2720277b024eSKalle Valo {
2721277b024eSKalle Valo 	/* Clear the flag in case user removes the card. */
2722277b024eSKalle Valo 	user_rmmod = 0;
2723277b024eSKalle Valo 
2724277b024eSKalle Valo 	return sdio_register_driver(&mwifiex_sdio);
2725277b024eSKalle Valo }
2726277b024eSKalle Valo 
2727277b024eSKalle Valo /*
2728277b024eSKalle Valo  * This function cleans up the SDIO driver.
2729277b024eSKalle Valo  *
2730277b024eSKalle Valo  * The following major steps are followed for cleanup -
2731277b024eSKalle Valo  *      - Resume the device if its suspended
2732277b024eSKalle Valo  *      - Disconnect the device if connected
2733277b024eSKalle Valo  *      - Shutdown the firmware
2734277b024eSKalle Valo  *      - Unregister the device from SDIO bus.
2735277b024eSKalle Valo  */
2736277b024eSKalle Valo static void
2737277b024eSKalle Valo mwifiex_sdio_cleanup_module(void)
2738277b024eSKalle Valo {
2739277b024eSKalle Valo 	/* Set the flag as user is removing this module. */
2740277b024eSKalle Valo 	user_rmmod = 1;
2741277b024eSKalle Valo 	cancel_work_sync(&sdio_work);
2742277b024eSKalle Valo 
2743277b024eSKalle Valo 	sdio_unregister_driver(&mwifiex_sdio);
2744277b024eSKalle Valo }
2745277b024eSKalle Valo 
2746277b024eSKalle Valo module_init(mwifiex_sdio_init_module);
2747277b024eSKalle Valo module_exit(mwifiex_sdio_cleanup_module);
2748277b024eSKalle Valo 
2749277b024eSKalle Valo MODULE_AUTHOR("Marvell International Ltd.");
2750277b024eSKalle Valo MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION);
2751277b024eSKalle Valo MODULE_VERSION(SDIO_VERSION);
2752277b024eSKalle Valo MODULE_LICENSE("GPL v2");
2753277b024eSKalle Valo MODULE_FIRMWARE(SD8786_DEFAULT_FW_NAME);
2754277b024eSKalle Valo MODULE_FIRMWARE(SD8787_DEFAULT_FW_NAME);
2755277b024eSKalle Valo MODULE_FIRMWARE(SD8797_DEFAULT_FW_NAME);
2756277b024eSKalle Valo MODULE_FIRMWARE(SD8897_DEFAULT_FW_NAME);
2757277b024eSKalle Valo MODULE_FIRMWARE(SD8887_DEFAULT_FW_NAME);
2758277b024eSKalle Valo MODULE_FIRMWARE(SD8997_DEFAULT_FW_NAME);
2759