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