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