sdio.c 70.9 KB
Newer Older
1 2 3
/*
 * Marvell Wireless LAN device driver: SDIO specific handling
 *
X
Xinming Hu 已提交
4
 * Copyright (C) 2011-2014, Marvell International Ltd.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
 *
 * This software file (the "File") is distributed by Marvell International
 * Ltd. under the terms of the GNU General Public License Version 2, June 1991
 * (the "License").  You may use, redistribute and/or modify this File in
 * accordance with the terms and conditions of the License, a copy of which
 * is available by writing to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
 * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
 *
 * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
 * ARE EXPRESSLY DISCLAIMED.  The License provides additional details about
 * this warranty disclaimer.
 */

#include <linux/firmware.h>

#include "decl.h"
#include "ioctl.h"
#include "util.h"
#include "fw.h"
#include "main.h"
#include "wmm.h"
#include "11n.h"
#include "sdio.h"


#define SDIO_VERSION	"1.0"

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
/* The mwifiex_sdio_remove() callback function is called when
 * user removes this module from kernel space or ejects
 * the card from the slot. The driver handles these 2 cases
 * differently.
 * If the user is removing the module, the few commands (FUNC_SHUTDOWN,
 * HS_CANCEL etc.) are sent to the firmware.
 * If the card is removed, there is no need to send these command.
 *
 * The variable 'user_rmmod' is used to distinguish these two
 * scenarios. This flag is initialized as FALSE in case the card
 * is removed, and will be set to TRUE for module removal when
 * module_exit function is called.
 */
static u8 user_rmmod;

49
static struct mwifiex_if_ops sdio_ops;
50
static unsigned long iface_work_flags;
51

52 53 54 55
static struct memory_type_mapping generic_mem_type_map[] = {
	{"DUMP", NULL, 0, 0xDD},
};

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
static struct memory_type_mapping mem_type_mapping_tbl[] = {
	{"ITCM", NULL, 0, 0xF0},
	{"DTCM", NULL, 0, 0xF1},
	{"SQRAM", NULL, 0, 0xF2},
	{"APU", NULL, 0, 0xF3},
	{"CIU", NULL, 0, 0xF4},
	{"ICU", NULL, 0, 0xF5},
	{"MAC", NULL, 0, 0xF6},
	{"EXT7", NULL, 0, 0xF7},
	{"EXT8", NULL, 0, 0xF8},
	{"EXT9", NULL, 0, 0xF9},
	{"EXT10", NULL, 0, 0xFA},
	{"EXT11", NULL, 0, 0xFB},
	{"EXT12", NULL, 0, 0xFC},
	{"EXT13", NULL, 0, 0xFD},
	{"EXTLAST", NULL, 0, 0xFE},
};

74 75 76 77 78 79 80 81 82 83 84
static const struct of_device_id mwifiex_sdio_of_match_table[] = {
	{ .compatible = "marvell,sd8897" },
	{ .compatible = "marvell,sd8997" },
	{ }
};

/* This function parse device tree node using mmc subnode devicetree API.
 * The device node is saved in card->plt_of_node.
 * if the device tree node exist and include interrupts attributes, this
 * function will also request platform specific wakeup interrupt.
 */
85
static int mwifiex_sdio_probe_of(struct device *dev)
86
{
87
	if (!of_match_node(mwifiex_sdio_of_match_table, dev->of_node)) {
88 89
		dev_err(dev, "required compatible string missing\n");
		return -EINVAL;
90 91 92 93 94
	}

	return 0;
}

95 96 97 98 99 100 101 102 103 104 105
/*
 * SDIO probe.
 *
 * This function probes an mwifiex device and registers it. It allocates
 * the card structure, enables SDIO function number and initiates the
 * device registration and initialization procedure by adding a logical
 * interface.
 */
static int
mwifiex_sdio_probe(struct sdio_func *func, const struct sdio_device_id *id)
{
106
	int ret;
107 108 109
	struct sdio_mmc_card *card = NULL;

	pr_debug("info: vendor=0x%4.04X device=0x%4.04X class=%d function=%d\n",
110
		 func->vendor, func->device, func->class, func->num);
111

112
	card = devm_kzalloc(&func->dev, sizeof(*card), GFP_KERNEL);
113
	if (!card)
114 115
		return -ENOMEM;

116 117
	init_completion(&card->fw_done);

118
	card->func = func;
119
	card->device_id = id;
120 121 122

	func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;

123 124 125 126 127 128 129
	if (id->driver_data) {
		struct mwifiex_sdio_device *data = (void *)id->driver_data;

		card->firmware = data->firmware;
		card->reg = data->reg;
		card->max_ports = data->max_ports;
		card->mp_agg_pkt_limit = data->mp_agg_pkt_limit;
130 131
		card->supports_sdio_new_mode = data->supports_sdio_new_mode;
		card->has_control_mask = data->has_control_mask;
132
		card->tx_buf_size = data->tx_buf_size;
133 134
		card->mp_tx_agg_buf_size = data->mp_tx_agg_buf_size;
		card->mp_rx_agg_buf_size = data->mp_rx_agg_buf_size;
135
		card->can_dump_fw = data->can_dump_fw;
136
		card->fw_dump_enh = data->fw_dump_enh;
137
		card->can_auto_tdls = data->can_auto_tdls;
138
		card->can_ext_scan = data->can_ext_scan;
139 140
	}

141 142 143 144 145
	sdio_claim_host(func);
	ret = sdio_enable_func(func);
	sdio_release_host(func);

	if (ret) {
146
		dev_err(&func->dev, "failed to enable function\n");
147
		return ret;
148 149
	}

150
	/* device tree node parsing and platform specific configuration*/
151
	if (func->dev.of_node) {
152 153
		ret = mwifiex_sdio_probe_of(&func->dev);
		if (ret)
154 155
			goto err_disable;
	}
156

157
	ret = mwifiex_add_card(card, &card->fw_done, &sdio_ops,
158
			       MWIFIEX_SDIO, &func->dev);
159
	if (ret) {
160
		dev_err(&func->dev, "add card failed\n");
161
		goto err_disable;
162 163
	}

164 165 166 167 168 169 170
	return 0;

err_disable:
	sdio_claim_host(func);
	sdio_disable_func(func);
	sdio_release_host(func);

171 172 173
	return ret;
}

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
/*
 * SDIO resume.
 *
 * Kernel needs to suspend all functions separately. Therefore all
 * registered functions must have drivers with suspend and resume
 * methods. Failing that the kernel simply removes the whole card.
 *
 * If already not resumed, this function turns on the traffic and
 * sends a host sleep cancel request to the firmware.
 */
static int mwifiex_sdio_resume(struct device *dev)
{
	struct sdio_func *func = dev_to_sdio_func(dev);
	struct sdio_mmc_card *card;
	struct mwifiex_adapter *adapter;
	mmc_pm_flag_t pm_flag = 0;

191 192 193 194
	pm_flag = sdio_get_host_pm_caps(func);
	card = sdio_get_drvdata(func);
	if (!card || !card->adapter) {
		dev_err(dev, "resume: invalid card or adapter\n");
195 196 197 198 199 200
		return 0;
	}

	adapter = card->adapter;

	if (!adapter->is_suspended) {
201 202
		mwifiex_dbg(adapter, WARN,
			    "device already resumed\n");
203 204 205 206 207 208 209
		return 0;
	}

	adapter->is_suspended = false;

	/* Disable Host Sleep */
	mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
210
			  MWIFIEX_SYNC_CMD);
211

212
	mwifiex_disable_wake(adapter);
213

214 215 216
	return 0;
}

217 218 219 220 221 222 223 224 225
/*
 * SDIO remove.
 *
 * This function removes the interface and frees up the card structure.
 */
static void
mwifiex_sdio_remove(struct sdio_func *func)
{
	struct sdio_mmc_card *card;
226
	struct mwifiex_adapter *adapter;
227
	struct mwifiex_private *priv;
228

229 230 231 232
	card = sdio_get_drvdata(func);
	if (!card)
		return;

233 234
	wait_for_completion(&card->fw_done);

235 236 237 238
	adapter = card->adapter;
	if (!adapter || !adapter->priv_num)
		return;

239 240
	mwifiex_dbg(adapter, INFO, "info: SDIO func num=%d\n", func->num);

241
	if (user_rmmod && !adapter->mfg_mode) {
242
		mwifiex_deauthenticate_all(adapter);
243

244 245 246
		priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
		mwifiex_disable_auto_ds(priv);
		mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
247
	}
248

249
	mwifiex_remove_card(adapter);
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
}

/*
 * SDIO suspend.
 *
 * Kernel needs to suspend all functions separately. Therefore all
 * registered functions must have drivers with suspend and resume
 * methods. Failing that the kernel simply removes the whole card.
 *
 * If already not suspended, this function allocates and sends a host
 * sleep activate request to the firmware and turns off the traffic.
 */
static int mwifiex_sdio_suspend(struct device *dev)
{
	struct sdio_func *func = dev_to_sdio_func(dev);
	struct sdio_mmc_card *card;
266
	struct mwifiex_adapter *adapter;
267 268 269
	mmc_pm_flag_t pm_flag = 0;
	int ret = 0;

270 271 272 273 274 275 276 277 278 279 280 281
	pm_flag = sdio_get_host_pm_caps(func);
	pr_debug("cmd: %s: suspend: PM flag = 0x%x\n",
		 sdio_func_id(func), pm_flag);
	if (!(pm_flag & MMC_PM_KEEP_POWER)) {
		dev_err(dev, "%s: cannot remain alive while host is"
			" suspended\n", sdio_func_id(func));
		return -ENOSYS;
	}

	card = sdio_get_drvdata(func);
	if (!card) {
		dev_err(dev, "suspend: invalid card\n");
282 283 284
		return 0;
	}

285 286 287
	/* Might still be loading firmware */
	wait_for_completion(&card->fw_done);

288
	adapter = card->adapter;
289 290 291 292 293
	if (!adapter) {
		dev_err(dev, "adapter is not valid\n");
		return 0;
	}

294
	mwifiex_enable_wake(adapter);
295

296
	/* Enable the Host Sleep */
297
	if (!mwifiex_enable_hs(adapter)) {
298 299
		mwifiex_dbg(adapter, ERROR,
			    "cmd: failed to suspend\n");
300
		adapter->hs_enabling = false;
301
		return -EFAULT;
302 303
	}

304 305
	mwifiex_dbg(adapter, INFO,
		    "cmd: suspend with MMC_PM_KEEP_POWER\n");
306 307
	ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);

308 309
	/* Indicate device suspended */
	adapter->is_suspended = true;
310
	adapter->hs_enabling = false;
311 312 313 314

	return ret;
}

315 316
/* Device ID for SD8786 */
#define SDIO_DEVICE_ID_MARVELL_8786   (0x9116)
317 318
/* Device ID for SD8787 */
#define SDIO_DEVICE_ID_MARVELL_8787   (0x9119)
319 320
/* Device ID for SD8797 */
#define SDIO_DEVICE_ID_MARVELL_8797   (0x9129)
321 322
/* Device ID for SD8897 */
#define SDIO_DEVICE_ID_MARVELL_8897   (0x912d)
323 324
/* Device ID for SD8887 */
#define SDIO_DEVICE_ID_MARVELL_8887   (0x9135)
325 326
/* Device ID for SD8801 */
#define SDIO_DEVICE_ID_MARVELL_8801   (0x9139)
327 328
/* Device ID for SD8997 */
#define SDIO_DEVICE_ID_MARVELL_8997   (0x9141)
329

330 331 332

/* WLAN IDs */
static const struct sdio_device_id mwifiex_ids[] = {
333 334 335 336 337 338
	{SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8786),
		.driver_data = (unsigned long) &mwifiex_sdio_sd8786},
	{SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787),
		.driver_data = (unsigned long) &mwifiex_sdio_sd8787},
	{SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8797),
		.driver_data = (unsigned long) &mwifiex_sdio_sd8797},
339 340
	{SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8897),
		.driver_data = (unsigned long) &mwifiex_sdio_sd8897},
341 342
	{SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8887),
		.driver_data = (unsigned long)&mwifiex_sdio_sd8887},
343 344
	{SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8801),
		.driver_data = (unsigned long)&mwifiex_sdio_sd8801},
345 346
	{SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8997),
		.driver_data = (unsigned long)&mwifiex_sdio_sd8997},
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
	{},
};

MODULE_DEVICE_TABLE(sdio, mwifiex_ids);

static const struct dev_pm_ops mwifiex_sdio_pm_ops = {
	.suspend = mwifiex_sdio_suspend,
	.resume = mwifiex_sdio_resume,
};

static struct sdio_driver mwifiex_sdio = {
	.name = "mwifiex_sdio",
	.id_table = mwifiex_ids,
	.probe = mwifiex_sdio_probe,
	.remove = mwifiex_sdio_remove,
	.drv = {
		.owner = THIS_MODULE,
		.pm = &mwifiex_sdio_pm_ops,
	}
};

D
Daniel Drake 已提交
368 369 370 371 372 373 374 375 376
/* Write data into SDIO card register. Caller claims SDIO device. */
static int
mwifiex_write_reg_locked(struct sdio_func *func, u32 reg, u8 data)
{
	int ret = -1;
	sdio_writeb(func, data, reg, &ret);
	return ret;
}

377 378 379 380
/*
 * This function writes data into SDIO card register.
 */
static int
381
mwifiex_write_reg(struct mwifiex_adapter *adapter, u32 reg, u8 data)
382 383
{
	struct sdio_mmc_card *card = adapter->card;
D
Daniel Drake 已提交
384
	int ret;
385 386

	sdio_claim_host(card->func);
D
Daniel Drake 已提交
387
	ret = mwifiex_write_reg_locked(card->func, reg, data);
388 389 390 391 392 393 394 395 396
	sdio_release_host(card->func);

	return ret;
}

/*
 * This function reads data from SDIO card register.
 */
static int
397
mwifiex_read_reg(struct mwifiex_adapter *adapter, u32 reg, u8 *data)
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
{
	struct sdio_mmc_card *card = adapter->card;
	int ret = -1;
	u8 val;

	sdio_claim_host(card->func);
	val = sdio_readb(card->func, reg, &ret);
	sdio_release_host(card->func);

	*data = val;

	return ret;
}

/*
 * This function writes multiple data into SDIO card memory.
 *
 * This does not work in suspended mode.
 */
static int
mwifiex_write_data_sync(struct mwifiex_adapter *adapter,
419
			u8 *buffer, u32 pkt_len, u32 port)
420 421
{
	struct sdio_mmc_card *card = adapter->card;
422
	int ret;
423 424 425 426 427 428 429 430 431 432
	u8 blk_mode =
		(port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE : BLOCK_MODE;
	u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
	u32 blk_cnt =
		(blk_mode ==
		 BLOCK_MODE) ? (pkt_len /
				MWIFIEX_SDIO_BLOCK_SIZE) : pkt_len;
	u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);

	if (adapter->is_suspended) {
433 434
		mwifiex_dbg(adapter, ERROR,
			    "%s: not allowed while suspended\n", __func__);
435 436 437 438 439
		return -1;
	}

	sdio_claim_host(card->func);

440
	ret = sdio_writesb(card->func, ioport, buffer, blk_cnt * blk_size);
441 442 443 444 445 446 447 448 449

	sdio_release_host(card->func);

	return ret;
}

/*
 * This function reads multiple data from SDIO card memory.
 */
450 451
static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *buffer,
				  u32 len, u32 port, u8 claim)
452 453
{
	struct sdio_mmc_card *card = adapter->card;
454
	int ret;
455 456
	u8 blk_mode = (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE
		       : BLOCK_MODE;
457
	u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
458 459
	u32 blk_cnt = (blk_mode == BLOCK_MODE) ? (len / MWIFIEX_SDIO_BLOCK_SIZE)
			: len;
460 461 462 463 464
	u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);

	if (claim)
		sdio_claim_host(card->func);

465
	ret = sdio_readsb(card->func, buffer, ioport, blk_cnt * blk_size);
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480

	if (claim)
		sdio_release_host(card->func);

	return ret;
}

/*
 * This function wakes up the card.
 *
 * A host power up command is written to the card configuration
 * register to wake up the card.
 */
static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
{
481 482
	mwifiex_dbg(adapter, EVENT,
		    "event: wakeup device...\n");
483

484
	return mwifiex_write_reg(adapter, CONFIGURATION_REG, HOST_POWER_UP);
485 486 487 488 489 490 491 492 493
}

/*
 * This function is called after the card has woken up.
 *
 * The card configuration register is reset.
 */
static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
{
494 495
	mwifiex_dbg(adapter, EVENT,
		    "cmd: wakeup device completed\n");
496

497
	return mwifiex_write_reg(adapter, CONFIGURATION_REG, 0);
498 499
}

500 501 502 503 504 505 506 507 508 509 510 511 512
static int mwifiex_sdio_dnld_fw(struct mwifiex_adapter *adapter,
			struct mwifiex_fw_image *fw)
{
	struct sdio_mmc_card *card = adapter->card;
	int ret;

	sdio_claim_host(card->func);
	ret = mwifiex_dnld_fw(adapter, fw);
	sdio_release_host(card->func);

	return ret;
}

513
/*
514 515 516 517 518 519
 * This function is used to initialize IO ports for the
 * chipsets supporting SDIO new mode eg SD8897.
 */
static int mwifiex_init_sdio_new_mode(struct mwifiex_adapter *adapter)
{
	u8 reg;
520
	struct sdio_mmc_card *card = adapter->card;
521 522 523 524

	adapter->ioport = MEM_PORT;

	/* enable sdio new mode */
525
	if (mwifiex_read_reg(adapter, card->reg->card_cfg_2_1_reg, &reg))
526
		return -1;
527
	if (mwifiex_write_reg(adapter, card->reg->card_cfg_2_1_reg,
528 529 530 531
			      reg | CMD53_NEW_MODE))
		return -1;

	/* Configure cmd port and enable reading rx length from the register */
532
	if (mwifiex_read_reg(adapter, card->reg->cmd_cfg_0, &reg))
533
		return -1;
534 535
	if (mwifiex_write_reg(adapter, card->reg->cmd_cfg_0,
			      reg | CMD_PORT_RD_LEN_EN))
536 537 538 539 540
		return -1;

	/* Enable Dnld/Upld ready auto reset for cmd port after cmd53 is
	 * completed
	 */
541
	if (mwifiex_read_reg(adapter, card->reg->cmd_cfg_1, &reg))
542
		return -1;
543 544
	if (mwifiex_write_reg(adapter, card->reg->cmd_cfg_1,
			      reg | CMD_PORT_AUTO_EN))
545 546 547 548 549 550
		return -1;

	return 0;
}

/* This function initializes the IO ports.
551 552 553 554 555 556 557 558
 *
 * The following operations are performed -
 *      - Read the IO ports (0, 1 and 2)
 *      - Set host interrupt Reset-To-Read to clear
 *      - Set auto re-enable interrupt
 */
static int mwifiex_init_sdio_ioport(struct mwifiex_adapter *adapter)
{
559
	u8 reg;
560
	struct sdio_mmc_card *card = adapter->card;
561 562 563

	adapter->ioport = 0;

564 565 566 567 568 569
	if (card->supports_sdio_new_mode) {
		if (mwifiex_init_sdio_new_mode(adapter))
			return -1;
		goto cont;
	}

570
	/* Read the IO port */
571
	if (!mwifiex_read_reg(adapter, card->reg->io_port_0_reg, &reg))
572 573 574 575
		adapter->ioport |= (reg & 0xff);
	else
		return -1;

576
	if (!mwifiex_read_reg(adapter, card->reg->io_port_1_reg, &reg))
577 578 579 580
		adapter->ioport |= ((reg & 0xff) << 8);
	else
		return -1;

581
	if (!mwifiex_read_reg(adapter, card->reg->io_port_2_reg, &reg))
582 583 584
		adapter->ioport |= ((reg & 0xff) << 16);
	else
		return -1;
585
cont:
586 587
	mwifiex_dbg(adapter, INFO,
		    "info: SDIO FUNC1 IO port: %#x\n", adapter->ioport);
588 589

	/* Set Host interrupt reset to read to clear */
590 591
	if (!mwifiex_read_reg(adapter, card->reg->host_int_rsr_reg, &reg))
		mwifiex_write_reg(adapter, card->reg->host_int_rsr_reg,
592
				  reg | card->reg->sdio_int_mask);
593 594 595 596
	else
		return -1;

	/* Dnld/Upld ready set to auto reset */
597 598
	if (!mwifiex_read_reg(adapter, card->reg->card_misc_cfg_reg, &reg))
		mwifiex_write_reg(adapter, card->reg->card_misc_cfg_reg,
599 600 601 602 603 604 605 606 607 608 609 610 611 612
				  reg | AUTO_RE_ENABLE_INT);
	else
		return -1;

	return 0;
}

/*
 * This function sends data to the card.
 */
static int mwifiex_write_data_to_card(struct mwifiex_adapter *adapter,
				      u8 *payload, u32 pkt_len, u32 port)
{
	u32 i = 0;
613
	int ret;
614 615

	do {
616
		ret = mwifiex_write_data_sync(adapter, payload, pkt_len, port);
617 618
		if (ret) {
			i++;
619 620 621
			mwifiex_dbg(adapter, ERROR,
				    "host_to_card, write iomem\t"
				    "(%d) failed: %d\n", i, ret);
622
			if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
623 624
				mwifiex_dbg(adapter, ERROR,
					    "write CFG reg failed\n");
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645

			ret = -1;
			if (i > MAX_WRITE_IOMEM_RETRY)
				return ret;
		}
	} while (ret == -1);

	return ret;
}

/*
 * This function gets the read port.
 *
 * If control port bit is set in MP read bitmap, the control port
 * is returned, otherwise the current read port is returned and
 * the value is increased (provided it does not reach the maximum
 * limit, in which case it is reset to 1)
 */
static int mwifiex_get_rd_port(struct mwifiex_adapter *adapter, u8 *port)
{
	struct sdio_mmc_card *card = adapter->card;
646
	const struct mwifiex_sdio_card_reg *reg = card->reg;
647
	u32 rd_bitmap = card->mp_rd_bitmap;
648

649 650
	mwifiex_dbg(adapter, DATA,
		    "data: mp_rd_bitmap=0x%08x\n", rd_bitmap);
651

652 653 654 655 656 657 658
	if (card->supports_sdio_new_mode) {
		if (!(rd_bitmap & reg->data_port_mask))
			return -1;
	} else {
		if (!(rd_bitmap & (CTRL_PORT_MASK | reg->data_port_mask)))
			return -1;
	}
659

660 661
	if ((card->has_control_mask) &&
	    (card->mp_rd_bitmap & CTRL_PORT_MASK)) {
662
		card->mp_rd_bitmap &= (u32) (~CTRL_PORT_MASK);
663
		*port = CTRL_PORT;
664 665 666
		mwifiex_dbg(adapter, DATA,
			    "data: port=%d mp_rd_bitmap=0x%08x\n",
			    *port, card->mp_rd_bitmap);
667
		return 0;
668
	}
669 670 671 672 673 674 675 676 677 678 679

	if (!(card->mp_rd_bitmap & (1 << card->curr_rd_port)))
		return -1;

	/* We are now handling the SDIO data ports */
	card->mp_rd_bitmap &= (u32)(~(1 << card->curr_rd_port));
	*port = card->curr_rd_port;

	if (++card->curr_rd_port == card->max_ports)
		card->curr_rd_port = reg->start_rd_port;

680 681 682
	mwifiex_dbg(adapter, DATA,
		    "data: port=%d mp_rd_bitmap=0x%08x -> 0x%08x\n",
		    *port, rd_bitmap, card->mp_rd_bitmap);
683

684 685 686 687 688 689 690 691 692 693
	return 0;
}

/*
 * This function gets the write port for data.
 *
 * The current write port is returned if available and the value is
 * increased (provided it does not reach the maximum limit, in which
 * case it is reset to 1)
 */
694
static int mwifiex_get_wr_port_data(struct mwifiex_adapter *adapter, u32 *port)
695 696
{
	struct sdio_mmc_card *card = adapter->card;
697
	const struct mwifiex_sdio_card_reg *reg = card->reg;
698
	u32 wr_bitmap = card->mp_wr_bitmap;
699

700 701
	mwifiex_dbg(adapter, DATA,
		    "data: mp_wr_bitmap=0x%08x\n", wr_bitmap);
702

703
	if (!(wr_bitmap & card->mp_data_port_mask)) {
704 705 706
		adapter->data_sent = true;
		return -EBUSY;
	}
707 708

	if (card->mp_wr_bitmap & (1 << card->curr_wr_port)) {
709
		card->mp_wr_bitmap &= (u32) (~(1 << card->curr_wr_port));
710
		*port = card->curr_wr_port;
711
		if (++card->curr_wr_port == card->mp_end_port)
712
			card->curr_wr_port = reg->start_wr_port;
713 714 715 716 717
	} else {
		adapter->data_sent = true;
		return -EBUSY;
	}

718
	if ((card->has_control_mask) && (*port == CTRL_PORT)) {
719 720 721 722
		mwifiex_dbg(adapter, ERROR,
			    "invalid data port=%d cur port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
			    *port, card->curr_wr_port, wr_bitmap,
			    card->mp_wr_bitmap);
723 724 725
		return -1;
	}

726 727 728
	mwifiex_dbg(adapter, DATA,
		    "data: port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
		    *port, wr_bitmap, card->mp_wr_bitmap);
729 730 731 732 733 734 735 736 737 738

	return 0;
}

/*
 * This function polls the card status.
 */
static int
mwifiex_sdio_poll_card_status(struct mwifiex_adapter *adapter, u8 bits)
{
739
	struct sdio_mmc_card *card = adapter->card;
740
	u32 tries;
741
	u8 cs;
742 743

	for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
744
		if (mwifiex_read_reg(adapter, card->reg->poll_reg, &cs))
745 746 747 748
			break;
		else if ((cs & bits) == bits)
			return 0;

749
		usleep_range(10, 20);
750 751
	}

752 753
	mwifiex_dbg(adapter, ERROR,
		    "poll card status failed, tries = %d\n", tries);
754

755 756 757 758 759 760 761 762 763
	return -1;
}

/*
 * This function reads the firmware status.
 */
static int
mwifiex_sdio_read_fw_status(struct mwifiex_adapter *adapter, u16 *dat)
{
764 765
	struct sdio_mmc_card *card = adapter->card;
	const struct mwifiex_sdio_card_reg *reg = card->reg;
766
	u8 fws0, fws1;
767

768
	if (mwifiex_read_reg(adapter, reg->status_reg_0, &fws0))
769 770
		return -1;

771
	if (mwifiex_read_reg(adapter, reg->status_reg_1, &fws1))
772 773 774 775 776 777 778 779 780 781 782 783 784
		return -1;

	*dat = (u16) ((fws1 << 8) | fws0);

	return 0;
}

/*
 * This function disables the host interrupt.
 *
 * The host interrupt mask is read, the disable bit is reset and
 * written back to the card host interrupt mask register.
 */
D
Daniel Drake 已提交
785
static void mwifiex_sdio_disable_host_int(struct mwifiex_adapter *adapter)
786
{
D
Daniel Drake 已提交
787 788
	struct sdio_mmc_card *card = adapter->card;
	struct sdio_func *func = card->func;
789

D
Daniel Drake 已提交
790
	sdio_claim_host(func);
791
	mwifiex_write_reg_locked(func, card->reg->host_int_mask_reg, 0);
D
Daniel Drake 已提交
792 793
	sdio_release_irq(func);
	sdio_release_host(func);
794 795
}

796 797 798 799 800 801 802 803 804 805 806 807
/*
 * This function reads the interrupt status from card.
 */
static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
{
	struct sdio_mmc_card *card = adapter->card;
	u8 sdio_ireg;
	unsigned long flags;

	if (mwifiex_read_data_sync(adapter, card->mp_regs,
				   card->reg->max_mp_regs,
				   REG_PORT | MWIFIEX_SDIO_BYTE_MODE_MASK, 0)) {
808
		mwifiex_dbg(adapter, ERROR, "read mp_regs failed\n");
809 810 811
		return;
	}

812
	sdio_ireg = card->mp_regs[card->reg->host_int_status_reg];
813 814 815 816 817 818 819 820
	if (sdio_ireg) {
		/*
		 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
		 * For SDIO new mode CMD port interrupts
		 *	DN_LD_CMD_PORT_HOST_INT_STATUS and/or
		 *	UP_LD_CMD_PORT_HOST_INT_STATUS
		 * Clear the interrupt status register
		 */
821 822
		mwifiex_dbg(adapter, INTR,
			    "int: sdio_ireg = %#x\n", sdio_ireg);
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
		spin_lock_irqsave(&adapter->int_lock, flags);
		adapter->int_status |= sdio_ireg;
		spin_unlock_irqrestore(&adapter->int_lock, flags);
	}
}

/*
 * SDIO interrupt handler.
 *
 * This function reads the interrupt status from firmware and handles
 * the interrupt in current thread (ksdioirqd) right away.
 */
static void
mwifiex_sdio_interrupt(struct sdio_func *func)
{
	struct mwifiex_adapter *adapter;
	struct sdio_mmc_card *card;

	card = sdio_get_drvdata(func);
	if (!card || !card->adapter) {
843 844
		pr_err("int: func=%p card=%p adapter=%p\n",
		       func, card, card ? card->adapter : NULL);
845 846 847 848 849 850 851 852 853 854 855
		return;
	}
	adapter = card->adapter;

	if (!adapter->pps_uapsd_mode && adapter->ps_state == PS_STATE_SLEEP)
		adapter->ps_state = PS_STATE_AWAKE;

	mwifiex_interrupt_status(adapter);
	mwifiex_main_process(adapter);
}

856 857 858 859 860 861 862 863
/*
 * This function enables the host interrupt.
 *
 * The host interrupt enable mask is written to the card
 * host interrupt mask register.
 */
static int mwifiex_sdio_enable_host_int(struct mwifiex_adapter *adapter)
{
864
	struct sdio_mmc_card *card = adapter->card;
D
Daniel Drake 已提交
865 866 867 868 869 870 871 872
	struct sdio_func *func = card->func;
	int ret;

	sdio_claim_host(func);

	/* Request the SDIO IRQ */
	ret = sdio_claim_irq(func, mwifiex_sdio_interrupt);
	if (ret) {
873 874
		mwifiex_dbg(adapter, ERROR,
			    "claim irq failed: ret=%d\n", ret);
D
Daniel Drake 已提交
875 876
		goto out;
	}
877

878
	/* Simply write the mask to the register */
879
	ret = mwifiex_write_reg_locked(func, card->reg->host_int_mask_reg,
D
Daniel Drake 已提交
880 881
				       card->reg->host_int_enable);
	if (ret) {
882 883
		mwifiex_dbg(adapter, ERROR,
			    "enable host interrupt failed\n");
D
Daniel Drake 已提交
884
		sdio_release_irq(func);
885
	}
D
Daniel Drake 已提交
886 887 888 889

out:
	sdio_release_host(func);
	return ret;
890 891 892 893 894 895 896 897 898
}

/*
 * This function sends a data buffer to the card.
 */
static int mwifiex_sdio_card_to_host(struct mwifiex_adapter *adapter,
				     u32 *type, u8 *buffer,
				     u32 npayload, u32 ioport)
{
899
	int ret;
900 901 902
	u32 nb;

	if (!buffer) {
903 904
		mwifiex_dbg(adapter, ERROR,
			    "%s: buffer is NULL\n", __func__);
905 906 907
		return -1;
	}

908
	ret = mwifiex_read_data_sync(adapter, buffer, npayload, ioport, 1);
909 910

	if (ret) {
911 912
		mwifiex_dbg(adapter, ERROR,
			    "%s: read iomem failed: %d\n", __func__,
913
			ret);
914 915 916 917 918
		return -1;
	}

	nb = le16_to_cpu(*(__le16 *) (buffer));
	if (nb > npayload) {
919 920 921
		mwifiex_dbg(adapter, ERROR,
			    "%s: invalid packet, nb=%d npayload=%d\n",
			    __func__, nb, npayload);
922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
		return -1;
	}

	*type = le16_to_cpu(*(__le16 *) (buffer + 2));

	return ret;
}

/*
 * This function downloads the firmware to the card.
 *
 * Firmware is downloaded to the card in blocks. Every block download
 * is tested for CRC errors, and retried a number of times before
 * returning failure.
 */
static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
				    struct mwifiex_fw_image *fw)
{
940 941
	struct sdio_mmc_card *card = adapter->card;
	const struct mwifiex_sdio_card_reg *reg = card->reg;
942
	int ret;
943 944 945
	u8 *firmware = fw->fw_buf;
	u32 firmware_len = fw->fw_len;
	u32 offset = 0;
946
	u8 base0, base1;
947 948
	u8 *fwbuf;
	u16 len = 0;
949
	u32 txlen, tx_blocks = 0, tries;
950 951 952
	u32 i = 0;

	if (!firmware_len) {
953 954
		mwifiex_dbg(adapter, ERROR,
			    "firmware image not found! Terminating download\n");
955 956 957
		return -1;
	}

958 959 960
	mwifiex_dbg(adapter, INFO,
		    "info: downloading FW image (%d bytes)\n",
		    firmware_len);
961 962 963

	/* Assume that the allocated buffer is 8-byte aligned */
	fwbuf = kzalloc(MWIFIEX_UPLD_SIZE, GFP_KERNEL);
964
	if (!fwbuf)
965
		return -ENOMEM;
966

967 968
	sdio_claim_host(card->func);

969 970 971 972 973 974 975
	/* Perform firmware data transfer */
	do {
		/* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY
		   bits */
		ret = mwifiex_sdio_poll_card_status(adapter, CARD_IO_READY |
						    DN_LD_CARD_RDY);
		if (ret) {
976 977 978
			mwifiex_dbg(adapter, ERROR,
				    "FW download with helper:\t"
				    "poll status timeout @ %d\n", offset);
979 980 981 982 983 984 985 986
			goto done;
		}

		/* More data? */
		if (offset >= firmware_len)
			break;

		for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
987
			ret = mwifiex_read_reg(adapter, reg->base_0_reg,
988 989
					       &base0);
			if (ret) {
990 991 992 993
				mwifiex_dbg(adapter, ERROR,
					    "dev BASE0 register read failed:\t"
					    "base0=%#04X(%d). Terminating dnld\n",
					    base0, base0);
994 995
				goto done;
			}
996
			ret = mwifiex_read_reg(adapter, reg->base_1_reg,
997 998
					       &base1);
			if (ret) {
999 1000 1001 1002
				mwifiex_dbg(adapter, ERROR,
					    "dev BASE1 register read failed:\t"
					    "base1=%#04X(%d). Terminating dnld\n",
					    base1, base1);
1003 1004 1005 1006 1007 1008 1009
				goto done;
			}
			len = (u16) (((base1 & 0xff) << 8) | (base0 & 0xff));

			if (len)
				break;

1010
			usleep_range(10, 20);
1011 1012 1013 1014 1015
		}

		if (!len) {
			break;
		} else if (len > MWIFIEX_UPLD_SIZE) {
1016 1017 1018
			mwifiex_dbg(adapter, ERROR,
				    "FW dnld failed @ %d, invalid length %d\n",
				    offset, len);
1019 1020 1021 1022 1023 1024 1025 1026 1027
			ret = -1;
			goto done;
		}

		txlen = len;

		if (len & BIT(0)) {
			i++;
			if (i > MAX_WRITE_IOMEM_RETRY) {
1028 1029 1030
				mwifiex_dbg(adapter, ERROR,
					    "FW dnld failed @ %d, over max retry\n",
					    offset);
1031 1032 1033
				ret = -1;
				goto done;
			}
1034 1035 1036
			mwifiex_dbg(adapter, ERROR,
				    "CRC indicated by the helper:\t"
				    "len = 0x%04X, txlen = %d\n", len, txlen);
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
			len &= ~BIT(0);
			/* Setting this to 0 to resend from same offset */
			txlen = 0;
		} else {
			i = 0;

			/* Set blocksize to transfer - checking for last
			   block */
			if (firmware_len - offset < txlen)
				txlen = firmware_len - offset;

1048 1049
			tx_blocks = (txlen + MWIFIEX_SDIO_BLOCK_SIZE - 1)
				    / MWIFIEX_SDIO_BLOCK_SIZE;
1050 1051 1052 1053 1054 1055 1056

			/* Copy payload to buffer */
			memmove(fwbuf, &firmware[offset], txlen);
		}

		ret = mwifiex_write_data_sync(adapter, fwbuf, tx_blocks *
					      MWIFIEX_SDIO_BLOCK_SIZE,
1057
					      adapter->ioport);
1058
		if (ret) {
1059 1060 1061
			mwifiex_dbg(adapter, ERROR,
				    "FW download, write iomem (%d) failed @ %d\n",
				    i, offset);
1062
			if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
1063 1064
				mwifiex_dbg(adapter, ERROR,
					    "write CFG reg failed\n");
1065 1066 1067 1068 1069 1070 1071 1072

			ret = -1;
			goto done;
		}

		offset += txlen;
	} while (true);

1073 1074
	mwifiex_dbg(adapter, MSG,
		    "info: FW download over, size %d bytes\n", offset);
1075 1076 1077

	ret = 0;
done:
1078
	sdio_release_host(card->func);
1079 1080 1081 1082 1083 1084 1085 1086
	kfree(fwbuf);
	return ret;
}

/*
 * This function checks the firmware status in card.
 */
static int mwifiex_check_fw_status(struct mwifiex_adapter *adapter,
1087
				   u32 poll_num)
1088 1089 1090 1091 1092 1093 1094 1095 1096
{
	int ret = 0;
	u16 firmware_stat;
	u32 tries;

	for (tries = 0; tries < poll_num; tries++) {
		ret = mwifiex_sdio_read_fw_status(adapter, &firmware_stat);
		if (ret)
			continue;
1097
		if (firmware_stat == FIRMWARE_READY_SDIO) {
1098 1099 1100
			ret = 0;
			break;
		} else {
1101
			msleep(100);
1102 1103 1104 1105
			ret = -1;
		}
	}

1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
	return ret;
}

/* This function checks if WLAN is the winner.
 */
static int mwifiex_check_winner_status(struct mwifiex_adapter *adapter)
{
	int ret = 0;
	u8 winner = 0;
	struct sdio_mmc_card *card = adapter->card;

	if (mwifiex_read_reg(adapter, card->reg->status_reg_0, &winner))
		return -1;

	if (winner)
		adapter->winner = 0;
	else
		adapter->winner = 1;
1124 1125 1126 1127

	return ret;
}

1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
/*
 * This function decode sdio aggreation pkt.
 *
 * Based on the the data block size and pkt_len,
 * skb data will be decoded to few packets.
 */
static void mwifiex_deaggr_sdio_pkt(struct mwifiex_adapter *adapter,
				    struct sk_buff *skb)
{
	u32 total_pkt_len, pkt_len;
	struct sk_buff *skb_deaggr;
	u32 pkt_type;
	u16 blk_size;
	u8 blk_num;
	u8 *data;

	data = skb->data;
	total_pkt_len = skb->len;

	while (total_pkt_len >= (SDIO_HEADER_OFFSET + INTF_HEADER_LEN)) {
		if (total_pkt_len < adapter->sdio_rx_block_size)
			break;
		blk_num = *(data + BLOCK_NUMBER_OFFSET);
		blk_size = adapter->sdio_rx_block_size * blk_num;
		if (blk_size > total_pkt_len) {
1153 1154 1155 1156
			mwifiex_dbg(adapter, ERROR,
				    "%s: error in blk_size,\t"
				    "blk_num=%d, blk_size=%d, total_pkt_len=%d\n",
				    __func__, blk_num, blk_size, total_pkt_len);
1157 1158 1159 1160 1161 1162
			break;
		}
		pkt_len = le16_to_cpu(*(__le16 *)(data + SDIO_HEADER_OFFSET));
		pkt_type = le16_to_cpu(*(__le16 *)(data + SDIO_HEADER_OFFSET +
					 2));
		if ((pkt_len + SDIO_HEADER_OFFSET) > blk_size) {
1163 1164 1165 1166
			mwifiex_dbg(adapter, ERROR,
				    "%s: error in pkt_len,\t"
				    "pkt_len=%d, blk_size=%d\n",
				    __func__, pkt_len, blk_size);
1167 1168
			break;
		}
1169 1170

		skb_deaggr = mwifiex_alloc_dma_align_buf(pkt_len, GFP_KERNEL);
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
		if (!skb_deaggr)
			break;
		skb_put(skb_deaggr, pkt_len);
		memcpy(skb_deaggr->data, data + SDIO_HEADER_OFFSET, pkt_len);
		skb_pull(skb_deaggr, INTF_HEADER_LEN);

		mwifiex_handle_rx_packet(adapter, skb_deaggr);
		data += blk_size;
		total_pkt_len -= blk_size;
	}
}

1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
/*
 * This function decodes a received packet.
 *
 * Based on the type, the packet is treated as either a data, or
 * a command response, or an event, and the correct handler
 * function is invoked.
 */
static int mwifiex_decode_rx_packet(struct mwifiex_adapter *adapter,
				    struct sk_buff *skb, u32 upld_typ)
{
	u8 *cmd_buf;
1194 1195
	__le16 *curr_ptr = (__le16 *)skb->data;
	u16 pkt_len = le16_to_cpu(*curr_ptr);
1196
	struct mwifiex_rxinfo *rx_info;
1197

1198 1199 1200 1201
	if (upld_typ != MWIFIEX_TYPE_AGGR_DATA) {
		skb_trim(skb, pkt_len);
		skb_pull(skb, INTF_HEADER_LEN);
	}
1202 1203

	switch (upld_typ) {
1204
	case MWIFIEX_TYPE_AGGR_DATA:
1205 1206
		mwifiex_dbg(adapter, INFO,
			    "info: --- Rx: Aggr Data packet ---\n");
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
		rx_info = MWIFIEX_SKB_RXCB(skb);
		rx_info->buf_type = MWIFIEX_TYPE_AGGR_DATA;
		if (adapter->rx_work_enabled) {
			skb_queue_tail(&adapter->rx_data_q, skb);
			atomic_inc(&adapter->rx_pending);
			adapter->data_received = true;
		} else {
			mwifiex_deaggr_sdio_pkt(adapter, skb);
			dev_kfree_skb_any(skb);
		}
		break;

1219
	case MWIFIEX_TYPE_DATA:
1220 1221
		mwifiex_dbg(adapter, DATA,
			    "info: --- Rx: Data packet ---\n");
1222 1223 1224 1225 1226 1227 1228
		if (adapter->rx_work_enabled) {
			skb_queue_tail(&adapter->rx_data_q, skb);
			adapter->data_received = true;
			atomic_inc(&adapter->rx_pending);
		} else {
			mwifiex_handle_rx_packet(adapter, skb);
		}
1229 1230 1231
		break;

	case MWIFIEX_TYPE_CMD:
1232 1233
		mwifiex_dbg(adapter, CMD,
			    "info: --- Rx: Cmd Response ---\n");
1234 1235 1236 1237 1238 1239
		/* take care of curr_cmd = NULL case */
		if (!adapter->curr_cmd) {
			cmd_buf = adapter->upld_buf;

			if (adapter->ps_state == PS_STATE_SLEEP_CFM)
				mwifiex_process_sleep_confirm_resp(adapter,
1240 1241
								   skb->data,
								   skb->len);
1242

1243 1244 1245
			memcpy(cmd_buf, skb->data,
			       min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER,
				     skb->len));
1246 1247 1248 1249 1250 1251 1252 1253 1254

			dev_kfree_skb_any(skb);
		} else {
			adapter->cmd_resp_received = true;
			adapter->curr_cmd->resp_skb = skb;
		}
		break;

	case MWIFIEX_TYPE_EVENT:
1255 1256
		mwifiex_dbg(adapter, EVENT,
			    "info: --- Rx: Event ---\n");
1257
		adapter->event_cause = le32_to_cpu(*(__le32 *) skb->data);
1258 1259

		if ((skb->len > 0) && (skb->len  < MAX_EVENT_SIZE))
1260 1261 1262
			memcpy(adapter->event_body,
			       skb->data + MWIFIEX_EVENT_HEADER_LEN,
			       skb->len);
1263 1264 1265 1266 1267 1268 1269 1270

		/* event cause has been saved to adapter->event_cause */
		adapter->event_received = true;
		adapter->event_skb = skb;

		break;

	default:
1271 1272
		mwifiex_dbg(adapter, ERROR,
			    "unknown upload type %#x\n", upld_typ);
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
		dev_kfree_skb_any(skb);
		break;
	}

	return 0;
}

/*
 * This function transfers received packets from card to driver, performing
 * aggregation if required.
 *
 * For data received on control port, or if aggregation is disabled, the
 * received buffers are uploaded as separate packets. However, if aggregation
 * is enabled and required, the buffers are copied onto an aggregation buffer,
 * provided there is space left, processed and finally uploaded.
 */
static int mwifiex_sdio_card_to_host_mp_aggr(struct mwifiex_adapter *adapter,
1290
					     u16 rx_len, u8 port)
1291 1292 1293 1294 1295
{
	struct sdio_mmc_card *card = adapter->card;
	s32 f_do_rx_aggr = 0;
	s32 f_do_rx_cur = 0;
	s32 f_aggr_cur = 0;
1296
	s32 f_post_aggr_cur = 0;
1297
	struct sk_buff *skb_deaggr;
1298 1299
	struct sk_buff *skb = NULL;
	u32 pkt_len, pkt_type, mport, pind;
1300 1301
	u8 *curr_ptr;

1302
	if ((card->has_control_mask) && (port == CTRL_PORT)) {
1303
		/* Read the command Resp without aggr */
1304 1305 1306
		mwifiex_dbg(adapter, CMD,
			    "info: %s: no aggregation for cmd\t"
			    "response\n", __func__);
1307 1308 1309 1310 1311 1312

		f_do_rx_cur = 1;
		goto rx_curr_single;
	}

	if (!card->mpa_rx.enabled) {
1313 1314 1315
		mwifiex_dbg(adapter, WARN,
			    "info: %s: rx aggregation disabled\n",
			    __func__);
1316 1317 1318 1319 1320

		f_do_rx_cur = 1;
		goto rx_curr_single;
	}

1321 1322 1323 1324
	if ((!card->has_control_mask && (card->mp_rd_bitmap &
					 card->reg->data_port_mask)) ||
	    (card->has_control_mask && (card->mp_rd_bitmap &
					(~((u32) CTRL_PORT_MASK))))) {
1325
		/* Some more data RX pending */
1326 1327
		mwifiex_dbg(adapter, INFO,
			    "info: %s: not last packet\n", __func__);
1328 1329

		if (MP_RX_AGGR_IN_PROGRESS(card)) {
1330
			if (MP_RX_AGGR_BUF_HAS_ROOM(card, rx_len)) {
1331 1332 1333 1334
				f_aggr_cur = 1;
			} else {
				/* No room in Aggr buf, do rx aggr now */
				f_do_rx_aggr = 1;
1335
				f_post_aggr_cur = 1;
1336 1337 1338 1339 1340 1341 1342 1343
			}
		} else {
			/* Rx aggr not in progress */
			f_aggr_cur = 1;
		}

	} else {
		/* No more data RX pending */
1344 1345
		mwifiex_dbg(adapter, INFO,
			    "info: %s: last packet\n", __func__);
1346 1347 1348

		if (MP_RX_AGGR_IN_PROGRESS(card)) {
			f_do_rx_aggr = 1;
1349
			if (MP_RX_AGGR_BUF_HAS_ROOM(card, rx_len))
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
				f_aggr_cur = 1;
			else
				/* No room in Aggr buf, do rx aggr now */
				f_do_rx_cur = 1;
		} else {
			f_do_rx_cur = 1;
		}
	}

	if (f_aggr_cur) {
1360 1361
		mwifiex_dbg(adapter, INFO,
			    "info: current packet aggregation\n");
1362
		/* Curr pkt can be aggregated */
1363
		mp_rx_aggr_setup(card, rx_len, port);
1364 1365

		if (MP_RX_AGGR_PKT_LIMIT_REACHED(card) ||
1366
		    mp_rx_aggr_port_limit_reached(card)) {
1367 1368 1369
			mwifiex_dbg(adapter, INFO,
				    "info: %s: aggregated packet\t"
				    "limit reached\n", __func__);
1370 1371 1372 1373 1374 1375 1376
			/* No more pkts allowed in Aggr buf, rx it */
			f_do_rx_aggr = 1;
		}
	}

	if (f_do_rx_aggr) {
		/* do aggr RX now */
1377 1378 1379
		mwifiex_dbg(adapter, DATA,
			    "info: do_rx_aggr: num of packets: %d\n",
			    card->mpa_rx.pkt_cnt);
1380

1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
		if (card->supports_sdio_new_mode) {
			int i;
			u32 port_count;

			for (i = 0, port_count = 0; i < card->max_ports; i++)
				if (card->mpa_rx.ports & BIT(i))
					port_count++;

			/* Reading data from "start_port + 0" to "start_port +
			 * port_count -1", so decrease the count by 1
			 */
			port_count--;
			mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
				 (port_count << 8)) + card->mpa_rx.start_port;
		} else {
			mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
				 (card->mpa_rx.ports << 4)) +
				 card->mpa_rx.start_port;
		}
1400

1401 1402 1403
		if (card->mpa_rx.pkt_cnt == 1)
			mport = adapter->ioport + port;

1404
		if (mwifiex_read_data_sync(adapter, card->mpa_rx.buf,
1405
					   card->mpa_rx.buf_len, mport, 1))
1406
			goto error;
1407 1408 1409 1410

		curr_ptr = card->mpa_rx.buf;

		for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1411
			u32 *len_arr = card->mpa_rx.len_arr;
1412 1413

			/* get curr PKT len & type */
1414 1415
			pkt_len = le16_to_cpu(*(__le16 *) &curr_ptr[0]);
			pkt_type = le16_to_cpu(*(__le16 *) &curr_ptr[2]);
1416 1417

			/* copy pkt to deaggr buf */
1418
			skb_deaggr = mwifiex_alloc_dma_align_buf(len_arr[pind],
1419
								 GFP_KERNEL);
1420
			if (!skb_deaggr) {
1421 1422 1423
				mwifiex_dbg(adapter, ERROR, "skb allocation failure\t"
					    "drop pkt len=%d type=%d\n",
					    pkt_len, pkt_type);
1424 1425 1426 1427
				curr_ptr += len_arr[pind];
				continue;
			}

1428
			skb_put(skb_deaggr, len_arr[pind]);
1429

1430 1431 1432
			if ((pkt_type == MWIFIEX_TYPE_DATA ||
			     (pkt_type == MWIFIEX_TYPE_AGGR_DATA &&
			      adapter->sdio_rx_aggr_enable)) &&
1433
			    (pkt_len <= len_arr[pind])) {
1434 1435 1436 1437 1438 1439 1440 1441 1442

				memcpy(skb_deaggr->data, curr_ptr, pkt_len);

				skb_trim(skb_deaggr, pkt_len);

				/* Process de-aggr packet */
				mwifiex_decode_rx_packet(adapter, skb_deaggr,
							 pkt_type);
			} else {
1443 1444 1445 1446 1447 1448
				mwifiex_dbg(adapter, ERROR,
					    "drop wrong aggr pkt:\t"
					    "sdio_single_port_rx_aggr=%d\t"
					    "type=%d len=%d max_len=%d\n",
					    adapter->sdio_rx_aggr_enable,
					    pkt_type, pkt_len, len_arr[pind]);
1449 1450
				dev_kfree_skb_any(skb_deaggr);
			}
1451
			curr_ptr += len_arr[pind];
1452 1453 1454 1455 1456 1457
		}
		MP_RX_AGGR_BUF_RESET(card);
	}

rx_curr_single:
	if (f_do_rx_cur) {
1458 1459
		mwifiex_dbg(adapter, INFO, "info: RX: port: %d, rx_len: %d\n",
			    port, rx_len);
1460

1461
		skb = mwifiex_alloc_dma_align_buf(rx_len, GFP_KERNEL);
1462
		if (!skb) {
1463 1464 1465
			mwifiex_dbg(adapter, ERROR,
				    "single skb allocated fail,\t"
				    "drop pkt port=%d len=%d\n", port, rx_len);
1466 1467 1468 1469 1470 1471 1472
			if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
						      card->mpa_rx.buf, rx_len,
						      adapter->ioport + port))
				goto error;
			return 0;
		}

1473
		skb_put(skb, rx_len);
1474 1475 1476 1477

		if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
					      skb->data, skb->len,
					      adapter->ioport + port))
1478
			goto error;
1479 1480
		if (!adapter->sdio_rx_aggr_enable &&
		    pkt_type == MWIFIEX_TYPE_AGGR_DATA) {
1481 1482 1483
			mwifiex_dbg(adapter, ERROR, "drop wrong pkt type %d\t"
				    "current SDIO RX Aggr not enabled\n",
				    pkt_type);
1484 1485
			dev_kfree_skb_any(skb);
			return 0;
1486
		}
1487 1488 1489

		mwifiex_decode_rx_packet(adapter, skb, pkt_type);
	}
1490
	if (f_post_aggr_cur) {
1491 1492
		mwifiex_dbg(adapter, INFO,
			    "info: current packet aggregation\n");
1493
		/* Curr pkt can be aggregated */
1494
		mp_rx_aggr_setup(card, rx_len, port);
1495
	}
1496 1497

	return 0;
1498
error:
1499
	if (MP_RX_AGGR_IN_PROGRESS(card))
1500 1501
		MP_RX_AGGR_BUF_RESET(card);

1502
	if (f_do_rx_cur && skb)
1503 1504 1505 1506
		/* Single transfer pending. Free curr buff also */
		dev_kfree_skb_any(skb);

	return -1;
1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526
}

/*
 * This function checks the current interrupt status.
 *
 * The following interrupts are checked and handled by this function -
 *      - Data sent
 *      - Command sent
 *      - Packets received
 *
 * Since the firmware does not generate download ready interrupt if the
 * port updated is command port only, command sent interrupt checking
 * should be done manually, and for every SDIO interrupt.
 *
 * In case of Rx packets received, the packets are uploaded from card to
 * host and processed accordingly.
 */
static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
{
	struct sdio_mmc_card *card = adapter->card;
1527
	const struct mwifiex_sdio_card_reg *reg = card->reg;
1528 1529
	int ret = 0;
	u8 sdio_ireg;
1530
	struct sk_buff *skb;
1531 1532 1533 1534 1535
	u8 port = CTRL_PORT;
	u32 len_reg_l, len_reg_u;
	u32 rx_blocks;
	u16 rx_len;
	unsigned long flags;
1536 1537
	u32 bitmap;
	u8 cr;
1538 1539 1540 1541 1542 1543 1544 1545 1546

	spin_lock_irqsave(&adapter->int_lock, flags);
	sdio_ireg = adapter->int_status;
	adapter->int_status = 0;
	spin_unlock_irqrestore(&adapter->int_lock, flags);

	if (!sdio_ireg)
		return ret;

1547 1548 1549 1550 1551 1552 1553 1554 1555
	/* Following interrupt is only for SDIO new mode */
	if (sdio_ireg & DN_LD_CMD_PORT_HOST_INT_STATUS && adapter->cmd_sent)
		adapter->cmd_sent = false;

	/* Following interrupt is only for SDIO new mode */
	if (sdio_ireg & UP_LD_CMD_PORT_HOST_INT_STATUS) {
		u32 pkt_type;

		/* read the len of control packet */
1556 1557
		rx_len = card->mp_regs[reg->cmd_rd_len_1] << 8;
		rx_len |= (u16)card->mp_regs[reg->cmd_rd_len_0];
1558 1559 1560 1561 1562 1563
		rx_blocks = DIV_ROUND_UP(rx_len, MWIFIEX_SDIO_BLOCK_SIZE);
		if (rx_len <= INTF_HEADER_LEN ||
		    (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
		     MWIFIEX_RX_DATA_BUF_SIZE)
			return -1;
		rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1564
		mwifiex_dbg(adapter, INFO, "info: rx_len = %d\n", rx_len);
1565

1566
		skb = mwifiex_alloc_dma_align_buf(rx_len, GFP_KERNEL);
1567 1568 1569 1570 1571 1572 1573 1574
		if (!skb)
			return -1;

		skb_put(skb, rx_len);

		if (mwifiex_sdio_card_to_host(adapter, &pkt_type, skb->data,
					      skb->len, adapter->ioport |
							CMD_PORT_SLCT)) {
1575 1576
			mwifiex_dbg(adapter, ERROR,
				    "%s: failed to card_to_host", __func__);
1577 1578 1579 1580 1581 1582
			dev_kfree_skb_any(skb);
			goto term_cmd;
		}

		if ((pkt_type != MWIFIEX_TYPE_CMD) &&
		    (pkt_type != MWIFIEX_TYPE_EVENT))
1583 1584 1585
			mwifiex_dbg(adapter, ERROR,
				    "%s:Received wrong packet on cmd port",
				    __func__);
1586 1587 1588 1589

		mwifiex_decode_rx_packet(adapter, skb, pkt_type);
	}

1590
	if (sdio_ireg & DN_LD_HOST_INT_STATUS) {
1591 1592 1593 1594 1595 1596 1597 1598 1599 1600
		bitmap = (u32) card->mp_regs[reg->wr_bitmap_l];
		bitmap |= ((u32) card->mp_regs[reg->wr_bitmap_u]) << 8;
		if (card->supports_sdio_new_mode) {
			bitmap |=
				((u32) card->mp_regs[reg->wr_bitmap_1l]) << 16;
			bitmap |=
				((u32) card->mp_regs[reg->wr_bitmap_1u]) << 24;
		}
		card->mp_wr_bitmap = bitmap;

1601 1602 1603
		mwifiex_dbg(adapter, INTR,
			    "int: DNLD: wr_bitmap=0x%x\n",
			    card->mp_wr_bitmap);
1604 1605
		if (adapter->data_sent &&
		    (card->mp_wr_bitmap & card->mp_data_port_mask)) {
1606 1607
			mwifiex_dbg(adapter, INTR,
				    "info:  <--- Tx DONE Interrupt --->\n");
1608 1609 1610 1611 1612 1613 1614
			adapter->data_sent = false;
		}
	}

	/* As firmware will not generate download ready interrupt if the port
	   updated is command port only, cmd_sent should be done for any SDIO
	   interrupt. */
1615
	if (card->has_control_mask && adapter->cmd_sent) {
1616 1617 1618
		/* Check if firmware has attach buffer at command port and
		   update just that in wr_bit_map. */
		card->mp_wr_bitmap |=
1619
			(u32) card->mp_regs[reg->wr_bitmap_l] & CTRL_PORT_MASK;
1620 1621 1622 1623
		if (card->mp_wr_bitmap & CTRL_PORT_MASK)
			adapter->cmd_sent = false;
	}

1624 1625
	mwifiex_dbg(adapter, INTR, "info: cmd_sent=%d data_sent=%d\n",
		    adapter->cmd_sent, adapter->data_sent);
1626
	if (sdio_ireg & UP_LD_HOST_INT_STATUS) {
1627 1628 1629 1630 1631 1632 1633 1634 1635
		bitmap = (u32) card->mp_regs[reg->rd_bitmap_l];
		bitmap |= ((u32) card->mp_regs[reg->rd_bitmap_u]) << 8;
		if (card->supports_sdio_new_mode) {
			bitmap |=
				((u32) card->mp_regs[reg->rd_bitmap_1l]) << 16;
			bitmap |=
				((u32) card->mp_regs[reg->rd_bitmap_1u]) << 24;
		}
		card->mp_rd_bitmap = bitmap;
1636 1637 1638
		mwifiex_dbg(adapter, INTR,
			    "int: UPLD: rd_bitmap=0x%x\n",
			    card->mp_rd_bitmap);
1639 1640 1641 1642

		while (true) {
			ret = mwifiex_get_rd_port(adapter, &port);
			if (ret) {
1643 1644
				mwifiex_dbg(adapter, INFO,
					    "info: no more rd_port available\n");
1645 1646
				break;
			}
1647 1648
			len_reg_l = reg->rd_len_p0_l + (port << 1);
			len_reg_u = reg->rd_len_p0_u + (port << 1);
1649 1650
			rx_len = ((u16) card->mp_regs[len_reg_u]) << 8;
			rx_len |= (u16) card->mp_regs[len_reg_l];
1651 1652 1653
			mwifiex_dbg(adapter, INFO,
				    "info: RX: port=%d rx_len=%u\n",
				    port, rx_len);
1654 1655 1656
			rx_blocks =
				(rx_len + MWIFIEX_SDIO_BLOCK_SIZE -
				 1) / MWIFIEX_SDIO_BLOCK_SIZE;
1657
			if (rx_len <= INTF_HEADER_LEN ||
1658 1659 1660
			    (card->mpa_rx.enabled &&
			     ((rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
			      card->mpa_rx.buf_size))) {
1661 1662 1663
				mwifiex_dbg(adapter, ERROR,
					    "invalid rx_len=%d\n",
					    rx_len);
1664 1665 1666
				return -1;
			}

1667
			rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1668 1669
			mwifiex_dbg(adapter, INFO, "info: rx_len = %d\n",
				    rx_len);
1670

1671
			if (mwifiex_sdio_card_to_host_mp_aggr(adapter, rx_len,
1672
							      port)) {
1673 1674 1675
				mwifiex_dbg(adapter, ERROR,
					    "card_to_host_mpa failed: int status=%#x\n",
					    sdio_ireg);
1676
				goto term_cmd;
1677 1678 1679 1680 1681
			}
		}
	}

	return 0;
1682 1683 1684 1685

term_cmd:
	/* terminate cmd */
	if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
1686
		mwifiex_dbg(adapter, ERROR, "read CFG reg failed\n");
1687
	else
1688 1689
		mwifiex_dbg(adapter, INFO,
			    "info: CFG reg val = %d\n", cr);
1690 1691

	if (mwifiex_write_reg(adapter, CONFIGURATION_REG, (cr | 0x04)))
1692 1693
		mwifiex_dbg(adapter, ERROR,
			    "write CFG reg failed\n");
1694
	else
1695
		mwifiex_dbg(adapter, INFO, "info: write success\n");
1696 1697

	if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
1698 1699
		mwifiex_dbg(adapter, ERROR,
			    "read CFG reg failed\n");
1700
	else
1701 1702
		mwifiex_dbg(adapter, INFO,
			    "info: CFG reg val =%x\n", cr);
1703 1704

	return -1;
1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721
}

/*
 * This function aggregates transmission buffers in driver and downloads
 * the aggregated packet to card.
 *
 * The individual packets are aggregated by copying into an aggregation
 * buffer and then downloaded to the card. Previous unsent packets in the
 * aggregation buffer are pre-copied first before new packets are added.
 * Aggregation is done till there is space left in the aggregation buffer,
 * or till new packets are available.
 *
 * The function will only download the packet to the card when aggregation
 * stops, otherwise it will just aggregate the packet in aggregation buffer
 * and return.
 */
static int mwifiex_host_to_card_mp_aggr(struct mwifiex_adapter *adapter,
1722
					u8 *payload, u32 pkt_len, u32 port,
1723 1724 1725 1726 1727 1728 1729 1730
					u32 next_pkt_len)
{
	struct sdio_mmc_card *card = adapter->card;
	int ret = 0;
	s32 f_send_aggr_buf = 0;
	s32 f_send_cur_buf = 0;
	s32 f_precopy_cur_buf = 0;
	s32 f_postcopy_cur_buf = 0;
1731
	u32 mport;
1732
	int index;
1733

1734 1735 1736
	if (!card->mpa_tx.enabled ||
	    (card->has_control_mask && (port == CTRL_PORT)) ||
	    (card->supports_sdio_new_mode && (port == CMD_PORT_SLCT))) {
1737 1738 1739
		mwifiex_dbg(adapter, WARN,
			    "info: %s: tx aggregation disabled\n",
			    __func__);
1740 1741 1742 1743 1744 1745 1746

		f_send_cur_buf = 1;
		goto tx_curr_single;
	}

	if (next_pkt_len) {
		/* More pkt in TX queue */
1747 1748 1749
		mwifiex_dbg(adapter, INFO,
			    "info: %s: more packets in queue.\n",
			    __func__);
1750 1751

		if (MP_TX_AGGR_IN_PROGRESS(card)) {
1752
			if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len)) {
1753 1754 1755
				f_precopy_cur_buf = 1;

				if (!(card->mp_wr_bitmap &
1756 1757 1758
				      (1 << card->curr_wr_port)) ||
				    !MP_TX_AGGR_BUF_HAS_ROOM(
					    card, pkt_len + next_pkt_len))
1759 1760 1761 1762 1763
					f_send_aggr_buf = 1;
			} else {
				/* No room in Aggr buf, send it */
				f_send_aggr_buf = 1;

1764
				if (!(card->mp_wr_bitmap &
1765 1766 1767 1768 1769 1770
				      (1 << card->curr_wr_port)))
					f_send_cur_buf = 1;
				else
					f_postcopy_cur_buf = 1;
			}
		} else {
1771 1772
			if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len) &&
			    (card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1773 1774 1775 1776 1777 1778
				f_precopy_cur_buf = 1;
			else
				f_send_cur_buf = 1;
		}
	} else {
		/* Last pkt in TX queue */
1779 1780 1781
		mwifiex_dbg(adapter, INFO,
			    "info: %s: Last packet in Tx Queue.\n",
			    __func__);
1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797

		if (MP_TX_AGGR_IN_PROGRESS(card)) {
			/* some packs in Aggr buf already */
			f_send_aggr_buf = 1;

			if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len))
				f_precopy_cur_buf = 1;
			else
				/* No room in Aggr buf, send it */
				f_send_cur_buf = 1;
		} else {
			f_send_cur_buf = 1;
		}
	}

	if (f_precopy_cur_buf) {
1798 1799 1800
		mwifiex_dbg(adapter, DATA,
			    "data: %s: precopy current buffer\n",
			    __func__);
1801 1802 1803
		MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);

		if (MP_TX_AGGR_PKT_LIMIT_REACHED(card) ||
1804
		    mp_tx_aggr_port_limit_reached(card))
1805 1806 1807 1808 1809
			/* No more pkts allowed in Aggr buf, send it */
			f_send_aggr_buf = 1;
	}

	if (f_send_aggr_buf) {
1810 1811 1812 1813
		mwifiex_dbg(adapter, DATA,
			    "data: %s: send aggr buffer: %d %d\n",
			    __func__, card->mpa_tx.start_port,
			    card->mpa_tx.ports);
1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833
		if (card->supports_sdio_new_mode) {
			u32 port_count;
			int i;

			for (i = 0, port_count = 0; i < card->max_ports; i++)
				if (card->mpa_tx.ports & BIT(i))
					port_count++;

			/* Writing data from "start_port + 0" to "start_port +
			 * port_count -1", so decrease the count by 1
			 */
			port_count--;
			mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
				 (port_count << 8)) + card->mpa_tx.start_port;
		} else {
			mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
				 (card->mpa_tx.ports << 4)) +
				 card->mpa_tx.start_port;
		}

1834 1835 1836
		if (card->mpa_tx.pkt_cnt == 1)
			mport = adapter->ioport + port;

1837
		ret = mwifiex_write_data_to_card(adapter, card->mpa_tx.buf,
1838
						 card->mpa_tx.buf_len, mport);
1839

1840 1841 1842 1843 1844 1845 1846 1847 1848
		/* Save the last multi port tx aggreagation info to debug log */
		index = adapter->dbg.last_sdio_mp_index;
		index = (index + 1) % MWIFIEX_DBG_SDIO_MP_NUM;
		adapter->dbg.last_sdio_mp_index = index;
		adapter->dbg.last_mp_wr_ports[index] = mport;
		adapter->dbg.last_mp_wr_bitmap[index] = card->mp_wr_bitmap;
		adapter->dbg.last_mp_wr_len[index] = card->mpa_tx.buf_len;
		adapter->dbg.last_mp_curr_wr_port[index] = card->curr_wr_port;

1849 1850 1851 1852 1853
		MP_TX_AGGR_BUF_RESET(card);
	}

tx_curr_single:
	if (f_send_cur_buf) {
1854 1855 1856
		mwifiex_dbg(adapter, DATA,
			    "data: %s: send current buffer %d\n",
			    __func__, port);
1857 1858 1859 1860 1861
		ret = mwifiex_write_data_to_card(adapter, payload, pkt_len,
						 adapter->ioport + port);
	}

	if (f_postcopy_cur_buf) {
1862 1863 1864
		mwifiex_dbg(adapter, DATA,
			    "data: %s: postcopy current buffer\n",
			    __func__);
1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881
		MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
	}

	return ret;
}

/*
 * This function downloads data from driver to card.
 *
 * Both commands and data packets are transferred to the card by this
 * function.
 *
 * This function adds the SDIO specific header to the front of the buffer
 * before transferring. The header contains the length of the packet and
 * the type. The firmware handles the packets based upon this set type.
 */
static int mwifiex_sdio_host_to_card(struct mwifiex_adapter *adapter,
1882
				     u8 type, struct sk_buff *skb,
1883 1884 1885
				     struct mwifiex_tx_param *tx_param)
{
	struct sdio_mmc_card *card = adapter->card;
1886
	int ret;
1887 1888
	u32 buf_block_len;
	u32 blk_size;
1889
	u32 port = CTRL_PORT;
1890 1891
	u8 *payload = (u8 *)skb->data;
	u32 pkt_len = skb->len;
1892 1893 1894 1895

	/* Allocate buffer and copy payload */
	blk_size = MWIFIEX_SDIO_BLOCK_SIZE;
	buf_block_len = (pkt_len + blk_size - 1) / blk_size;
1896 1897
	*(__le16 *)&payload[0] = cpu_to_le16((u16)pkt_len);
	*(__le16 *)&payload[2] = cpu_to_le16(type);
1898 1899 1900 1901 1902 1903 1904 1905 1906 1907

	/*
	 * This is SDIO specific header
	 *  u16 length,
	 *  u16 type (MWIFIEX_TYPE_DATA = 0, MWIFIEX_TYPE_CMD = 1,
	 *  MWIFIEX_TYPE_EVENT = 3)
	 */
	if (type == MWIFIEX_TYPE_DATA) {
		ret = mwifiex_get_wr_port_data(adapter, &port);
		if (ret) {
1908 1909 1910
			mwifiex_dbg(adapter, ERROR,
				    "%s: no wr_port available\n",
				    __func__);
1911 1912 1913 1914 1915 1916 1917 1918
			return ret;
		}
	} else {
		adapter->cmd_sent = true;
		/* Type must be MWIFIEX_TYPE_CMD */

		if (pkt_len <= INTF_HEADER_LEN ||
		    pkt_len > MWIFIEX_UPLD_SIZE)
1919 1920 1921
			mwifiex_dbg(adapter, ERROR,
				    "%s: payload=%p, nb=%d\n",
				    __func__, payload, pkt_len);
1922 1923 1924

		if (card->supports_sdio_new_mode)
			port = CMD_PORT_SLCT;
1925 1926 1927 1928 1929 1930 1931
	}

	/* Transfer data to card */
	pkt_len = buf_block_len * blk_size;

	if (tx_param)
		ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1932 1933
						   port, tx_param->next_pkt_len
						   );
1934 1935
	else
		ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1936
						   port, 0);
1937 1938 1939 1940

	if (ret) {
		if (type == MWIFIEX_TYPE_CMD)
			adapter->cmd_sent = false;
1941
		if (type == MWIFIEX_TYPE_DATA) {
1942
			adapter->data_sent = false;
1943 1944 1945 1946
			/* restore curr_wr_port in error cases */
			card->curr_wr_port = port;
			card->mp_wr_bitmap |= (u32)(1 << card->curr_wr_port);
		}
1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965
	} else {
		if (type == MWIFIEX_TYPE_DATA) {
			if (!(card->mp_wr_bitmap & (1 << card->curr_wr_port)))
				adapter->data_sent = true;
			else
				adapter->data_sent = false;
		}
	}

	return ret;
}

/*
 * This function allocates the MPA Tx and Rx buffers.
 */
static int mwifiex_alloc_sdio_mpa_buffers(struct mwifiex_adapter *adapter,
				   u32 mpa_tx_buf_size, u32 mpa_rx_buf_size)
{
	struct sdio_mmc_card *card = adapter->card;
1966
	u32 rx_buf_size;
1967 1968 1969 1970 1971 1972 1973 1974 1975 1976
	int ret = 0;

	card->mpa_tx.buf = kzalloc(mpa_tx_buf_size, GFP_KERNEL);
	if (!card->mpa_tx.buf) {
		ret = -1;
		goto error;
	}

	card->mpa_tx.buf_size = mpa_tx_buf_size;

1977 1978 1979
	rx_buf_size = max_t(u32, mpa_rx_buf_size,
			    (u32)SDIO_MAX_AGGR_BUF_SIZE);
	card->mpa_rx.buf = kzalloc(rx_buf_size, GFP_KERNEL);
1980 1981 1982 1983 1984
	if (!card->mpa_rx.buf) {
		ret = -1;
		goto error;
	}

1985
	card->mpa_rx.buf_size = rx_buf_size;
1986 1987 1988 1989 1990

error:
	if (ret) {
		kfree(card->mpa_tx.buf);
		kfree(card->mpa_rx.buf);
1991 1992
		card->mpa_tx.buf_size = 0;
		card->mpa_rx.buf_size = 0;
1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009
	}

	return ret;
}

/*
 * This function unregisters the SDIO device.
 *
 * The SDIO IRQ is released, the function is disabled and driver
 * data is set to null.
 */
static void
mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
{
	struct sdio_mmc_card *card = adapter->card;

	if (adapter->card) {
2010
		card->adapter = NULL;
2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023
		sdio_claim_host(card->func);
		sdio_disable_func(card->func);
		sdio_release_host(card->func);
	}
}

/*
 * This function registers the SDIO device.
 *
 * SDIO IRQ is claimed, block size is set and driver data is initialized.
 */
static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
{
D
Daniel Drake 已提交
2024
	int ret;
2025 2026 2027 2028 2029
	struct sdio_mmc_card *card = adapter->card;
	struct sdio_func *func = card->func;

	/* save adapter pointer in card */
	card->adapter = adapter;
2030
	adapter->tx_buf_size = card->tx_buf_size;
2031 2032 2033 2034 2035

	sdio_claim_host(func);

	/* Set block size */
	ret = sdio_set_block_size(card->func, MWIFIEX_SDIO_BLOCK_SIZE);
D
Daniel Drake 已提交
2036
	sdio_release_host(func);
2037
	if (ret) {
2038 2039
		mwifiex_dbg(adapter, ERROR,
			    "cannot set SDIO block size\n");
D
Daniel Drake 已提交
2040
		return ret;
2041 2042
	}

2043
	strcpy(adapter->fw_name, card->firmware);
2044 2045 2046 2047 2048 2049 2050
	if (card->fw_dump_enh) {
		adapter->mem_type_mapping_tbl = generic_mem_type_map;
		adapter->num_mem_types = 1;
	} else {
		adapter->mem_type_mapping_tbl = mem_type_mapping_tbl;
		adapter->num_mem_types = ARRAY_SIZE(mem_type_mapping_tbl);
	}
2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069

	return 0;
}

/*
 * This function initializes the SDIO driver.
 *
 * The following initializations steps are followed -
 *      - Read the Host interrupt status register to acknowledge
 *        the first interrupt got from bootloader
 *      - Disable host interrupt mask register
 *      - Get SDIO port
 *      - Initialize SDIO variables in card
 *      - Allocate MP registers
 *      - Allocate MPA Tx and Rx buffers
 */
static int mwifiex_init_sdio(struct mwifiex_adapter *adapter)
{
	struct sdio_mmc_card *card = adapter->card;
2070
	const struct mwifiex_sdio_card_reg *reg = card->reg;
2071
	int ret;
2072
	u8 sdio_ireg;
2073

2074 2075
	sdio_set_drvdata(card->func, card);

2076
	/*
2077
	 * Read the host_int_status_reg for ACK the first interrupt got
2078 2079 2080
	 * from the bootloader. If we don't do this we get a interrupt
	 * as soon as we register the irq.
	 */
2081
	mwifiex_read_reg(adapter, card->reg->host_int_status_reg, &sdio_ireg);
2082 2083 2084 2085 2086 2087 2088

	/* Get SDIO ioport */
	mwifiex_init_sdio_ioport(adapter);

	/* Initialize SDIO variables in card */
	card->mp_rd_bitmap = 0;
	card->mp_wr_bitmap = 0;
2089 2090
	card->curr_rd_port = reg->start_rd_port;
	card->curr_wr_port = reg->start_wr_port;
2091

2092
	card->mp_data_port_mask = reg->data_port_mask;
2093 2094 2095 2096 2097

	card->mpa_tx.buf_len = 0;
	card->mpa_tx.pkt_cnt = 0;
	card->mpa_tx.start_port = 0;

2098
	card->mpa_tx.enabled = 1;
2099
	card->mpa_tx.pkt_aggr_limit = card->mp_agg_pkt_limit;
2100 2101 2102 2103 2104

	card->mpa_rx.buf_len = 0;
	card->mpa_rx.pkt_cnt = 0;
	card->mpa_rx.start_port = 0;

2105
	card->mpa_rx.enabled = 1;
2106
	card->mpa_rx.pkt_aggr_limit = card->mp_agg_pkt_limit;
2107 2108

	/* Allocate buffers for SDIO MP-A */
2109
	card->mp_regs = kzalloc(reg->max_mp_regs, GFP_KERNEL);
2110
	if (!card->mp_regs)
2111
		return -ENOMEM;
2112

2113 2114 2115
	/* Allocate skb pointer buffers */
	card->mpa_rx.skb_arr = kzalloc((sizeof(void *)) *
				       card->mp_agg_pkt_limit, GFP_KERNEL);
I
Insu Yun 已提交
2116 2117 2118 2119 2120
	if (!card->mpa_rx.skb_arr) {
		kfree(card->mp_regs);
		return -ENOMEM;
	}

2121 2122
	card->mpa_rx.len_arr = kzalloc(sizeof(*card->mpa_rx.len_arr) *
				       card->mp_agg_pkt_limit, GFP_KERNEL);
I
Insu Yun 已提交
2123 2124 2125 2126 2127 2128
	if (!card->mpa_rx.len_arr) {
		kfree(card->mp_regs);
		kfree(card->mpa_rx.skb_arr);
		return -ENOMEM;
	}

2129
	ret = mwifiex_alloc_sdio_mpa_buffers(adapter,
2130 2131
					     card->mp_tx_agg_buf_size,
					     card->mp_rx_agg_buf_size);
2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146

	/* Allocate 32k MPA Tx/Rx buffers if 64k memory allocation fails */
	if (ret && (card->mp_tx_agg_buf_size == MWIFIEX_MP_AGGR_BUF_SIZE_MAX ||
		    card->mp_rx_agg_buf_size == MWIFIEX_MP_AGGR_BUF_SIZE_MAX)) {
		/* Disable rx single port aggregation */
		adapter->host_disable_sdio_rx_aggr = true;

		ret = mwifiex_alloc_sdio_mpa_buffers
			(adapter, MWIFIEX_MP_AGGR_BUF_SIZE_32K,
			 MWIFIEX_MP_AGGR_BUF_SIZE_32K);
		if (ret) {
			/* Disable multi port aggregation */
			card->mpa_tx.enabled = 0;
			card->mpa_rx.enabled = 0;
		}
2147 2148
	}

2149
	adapter->auto_tdls = card->can_auto_tdls;
2150
	adapter->ext_scan = card->can_ext_scan;
2151
	return 0;
2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177
}

/*
 * This function resets the MPA Tx and Rx buffers.
 */
static void mwifiex_cleanup_mpa_buf(struct mwifiex_adapter *adapter)
{
	struct sdio_mmc_card *card = adapter->card;

	MP_TX_AGGR_BUF_RESET(card);
	MP_RX_AGGR_BUF_RESET(card);
}

/*
 * This function cleans up the allocated card buffers.
 *
 * The following are freed by this function -
 *      - MP registers
 *      - MPA Tx buffer
 *      - MPA Rx buffer
 */
static void mwifiex_cleanup_sdio(struct mwifiex_adapter *adapter)
{
	struct sdio_mmc_card *card = adapter->card;

	kfree(card->mp_regs);
2178 2179
	kfree(card->mpa_rx.skb_arr);
	kfree(card->mpa_rx.len_arr);
2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190
	kfree(card->mpa_tx.buf);
	kfree(card->mpa_rx.buf);
}

/*
 * This function updates the MP end port in card.
 */
static void
mwifiex_update_mp_end_port(struct mwifiex_adapter *adapter, u16 port)
{
	struct sdio_mmc_card *card = adapter->card;
2191
	const struct mwifiex_sdio_card_reg *reg = card->reg;
2192 2193 2194 2195
	int i;

	card->mp_end_port = port;

2196
	card->mp_data_port_mask = reg->data_port_mask;
2197

2198 2199 2200 2201 2202
	if (reg->start_wr_port) {
		for (i = 1; i <= card->max_ports - card->mp_end_port; i++)
			card->mp_data_port_mask &=
					~(1 << (card->max_ports - i));
	}
2203

2204
	card->curr_wr_port = reg->start_wr_port;
2205

2206 2207 2208
	mwifiex_dbg(adapter, CMD,
		    "cmd: mp_end_port %d, data port mask 0x%x\n",
		    port, card->mp_data_port_mask);
2209 2210
}

2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230
static void mwifiex_recreate_adapter(struct sdio_mmc_card *card)
{
	struct sdio_func *func = card->func;
	const struct sdio_device_id *device_id = card->device_id;

	/* TODO mmc_hw_reset does not require destroying and re-probing the
	 * whole adapter. Hence there was no need to for this rube-goldberg
	 * design to reload the fw from an external workqueue. If we don't
	 * destroy the adapter we could reload the fw from
	 * mwifiex_main_work_queue directly.
	 * The real difficulty with fw reset is to restore all the user
	 * settings applied through ioctl. By destroying and recreating the
	 * adapter, we take the easy way out, since we rely on user space to
	 * restore them. We assume that user space will treat the new
	 * incarnation of the adapter(interfaces) as if they had been just
	 * discovered and initializes them from scratch.
	 */

	mwifiex_sdio_remove(func);

2231 2232 2233 2234 2235 2236 2237 2238
	/*
	 * Normally, we would let the driver core take care of releasing these.
	 * But we're not letting the driver core handle this one. See above
	 * TODO.
	 */
	sdio_set_drvdata(func, NULL);
	devm_kfree(&func->dev, card);

2239 2240 2241 2242 2243 2244 2245 2246
	/* power cycle the adapter */
	sdio_claim_host(func);
	mmc_hw_reset(func->card->host);
	sdio_release_host(func);

	mwifiex_sdio_probe(func, device_id);
}

2247
static struct mwifiex_adapter *save_adapter;
2248
static void mwifiex_sdio_card_reset_work(struct mwifiex_adapter *adapter)
2249
{
2250
	struct sdio_mmc_card *card = adapter->card;
2251

2252 2253 2254 2255 2256 2257 2258
	/* TODO card pointer is unprotected. If the adapter is removed
	 * physically, sdio core might trigger mwifiex_sdio_remove, before this
	 * workqueue is run, which will destroy the adapter struct. When this
	 * workqueue eventually exceutes it will dereference an invalid adapter
	 * pointer
	 */
	mwifiex_recreate_adapter(card);
2259
}
2260

2261 2262 2263 2264 2265 2266 2267 2268 2269
/* This function read/write firmware */
static enum
rdwr_status mwifiex_sdio_rdwr_firmware(struct mwifiex_adapter *adapter,
				       u8 doneflag)
{
	struct sdio_mmc_card *card = adapter->card;
	int ret, tries;
	u8 ctrl_data = 0;

2270 2271
	sdio_writeb(card->func, card->reg->fw_dump_host_ready,
		    card->reg->fw_dump_ctrl, &ret);
2272
	if (ret) {
2273
		mwifiex_dbg(adapter, ERROR, "SDIO Write ERR\n");
2274 2275 2276 2277 2278 2279
		return RDWR_STATUS_FAILURE;
	}
	for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
		ctrl_data = sdio_readb(card->func, card->reg->fw_dump_ctrl,
				       &ret);
		if (ret) {
2280
			mwifiex_dbg(adapter, ERROR, "SDIO read err\n");
2281 2282 2283 2284 2285 2286
			return RDWR_STATUS_FAILURE;
		}
		if (ctrl_data == FW_DUMP_DONE)
			break;
		if (doneflag && ctrl_data == doneflag)
			return RDWR_STATUS_DONE;
2287
		if (ctrl_data != card->reg->fw_dump_host_ready) {
2288
			mwifiex_dbg(adapter, WARN,
2289 2290
				    "The ctrl reg was changed, re-try again\n");
			sdio_writeb(card->func, card->reg->fw_dump_host_ready,
2291 2292
				    card->reg->fw_dump_ctrl, &ret);
			if (ret) {
2293
				mwifiex_dbg(adapter, ERROR, "SDIO write err\n");
2294 2295 2296 2297 2298
				return RDWR_STATUS_FAILURE;
			}
		}
		usleep_range(100, 200);
	}
2299
	if (ctrl_data == card->reg->fw_dump_host_ready) {
2300 2301
		mwifiex_dbg(adapter, ERROR,
			    "Fail to pull ctrl_data\n");
2302 2303 2304 2305 2306 2307 2308
		return RDWR_STATUS_FAILURE;
	}

	return RDWR_STATUS_SUCCESS;
}

/* This function dump firmware memory to file */
2309
static void mwifiex_sdio_fw_dump(struct mwifiex_adapter *adapter)
2310 2311 2312 2313 2314 2315 2316 2317
{
	struct sdio_mmc_card *card = adapter->card;
	int ret = 0;
	unsigned int reg, reg_start, reg_end;
	u8 *dbg_ptr, *end_ptr, dump_num, idx, i, read_reg, doneflag = 0;
	enum rdwr_status stat;
	u32 memory_size;

2318
	if (!card->can_dump_fw)
2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333
		return;

	for (idx = 0; idx < ARRAY_SIZE(mem_type_mapping_tbl); idx++) {
		struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];

		if (entry->mem_ptr) {
			vfree(entry->mem_ptr);
			entry->mem_ptr = NULL;
		}
		entry->mem_size = 0;
	}

	mwifiex_pm_wakeup_card(adapter);
	sdio_claim_host(card->func);

2334
	mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump start ==\n");
2335 2336 2337 2338 2339 2340 2341 2342 2343

	stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
	if (stat == RDWR_STATUS_FAILURE)
		goto done;

	reg = card->reg->fw_dump_start;
	/* Read the number of the memories which will dump */
	dump_num = sdio_readb(card->func, reg, &ret);
	if (ret) {
2344
		mwifiex_dbg(adapter, ERROR, "SDIO read memory length err\n");
2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360
		goto done;
	}

	/* Read the length of every memory which will dump */
	for (idx = 0; idx < dump_num; idx++) {
		struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];

		stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
		if (stat == RDWR_STATUS_FAILURE)
			goto done;

		memory_size = 0;
		reg = card->reg->fw_dump_start;
		for (i = 0; i < 4; i++) {
			read_reg = sdio_readb(card->func, reg, &ret);
			if (ret) {
2361
				mwifiex_dbg(adapter, ERROR, "SDIO read err\n");
2362 2363 2364 2365 2366 2367 2368
				goto done;
			}
			memory_size |= (read_reg << i*8);
			reg++;
		}

		if (memory_size == 0) {
2369
			mwifiex_dbg(adapter, DUMP, "Firmware dump Finished!\n");
2370 2371 2372 2373 2374 2375 2376
			ret = mwifiex_write_reg(adapter,
						card->reg->fw_dump_ctrl,
						FW_DUMP_READ_DONE);
			if (ret) {
				mwifiex_dbg(adapter, ERROR, "SDIO write err\n");
				return;
			}
2377 2378 2379
			break;
		}

2380 2381
		mwifiex_dbg(adapter, DUMP,
			    "%s_SIZE=0x%x\n", entry->mem_name, memory_size);
2382 2383 2384
		entry->mem_ptr = vmalloc(memory_size + 1);
		entry->mem_size = memory_size;
		if (!entry->mem_ptr) {
2385 2386
			mwifiex_dbg(adapter, ERROR, "Vmalloc %s failed\n",
				    entry->mem_name);
2387 2388 2389 2390 2391 2392
			goto done;
		}
		dbg_ptr = entry->mem_ptr;
		end_ptr = dbg_ptr + memory_size;

		doneflag = entry->done_flag;
2393 2394 2395
		mwifiex_dbg(adapter, DUMP,
			    "Start %s output, please wait...\n",
			    entry->mem_name);
2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406

		do {
			stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
			if (stat == RDWR_STATUS_FAILURE)
				goto done;

			reg_start = card->reg->fw_dump_start;
			reg_end = card->reg->fw_dump_end;
			for (reg = reg_start; reg <= reg_end; reg++) {
				*dbg_ptr = sdio_readb(card->func, reg, &ret);
				if (ret) {
2407 2408
					mwifiex_dbg(adapter, ERROR,
						    "SDIO read err\n");
2409 2410 2411 2412 2413
					goto done;
				}
				if (dbg_ptr < end_ptr)
					dbg_ptr++;
				else
2414 2415
					mwifiex_dbg(adapter, ERROR,
						    "Allocated buf not enough\n");
2416 2417 2418 2419 2420
			}

			if (stat != RDWR_STATUS_DONE)
				continue;

2421 2422
			mwifiex_dbg(adapter, DUMP, "%s done: size=0x%tx\n",
				    entry->mem_name, dbg_ptr - entry->mem_ptr);
2423 2424 2425
			break;
		} while (1);
	}
2426
	mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump end ==\n");
2427 2428 2429 2430 2431

done:
	sdio_release_host(card->func);
}

2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545
static void mwifiex_sdio_generic_fw_dump(struct mwifiex_adapter *adapter)
{
	struct sdio_mmc_card *card = adapter->card;
	struct memory_type_mapping *entry = &generic_mem_type_map[0];
	unsigned int reg, reg_start, reg_end;
	u8 start_flag = 0, done_flag = 0;
	u8 *dbg_ptr, *end_ptr;
	enum rdwr_status stat;
	int ret = -1, tries;

	if (!card->fw_dump_enh)
		return;

	if (entry->mem_ptr) {
		vfree(entry->mem_ptr);
		entry->mem_ptr = NULL;
	}
	entry->mem_size = 0;

	mwifiex_pm_wakeup_card(adapter);
	sdio_claim_host(card->func);

	mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump start ==\n");

	stat = mwifiex_sdio_rdwr_firmware(adapter, done_flag);
	if (stat == RDWR_STATUS_FAILURE)
		goto done;

	reg_start = card->reg->fw_dump_start;
	reg_end = card->reg->fw_dump_end;
	for (reg = reg_start; reg <= reg_end; reg++) {
		for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
			start_flag = sdio_readb(card->func, reg, &ret);
			if (ret) {
				mwifiex_dbg(adapter, ERROR,
					    "SDIO read err\n");
				goto done;
			}
			if (start_flag == 0)
				break;
			if (tries == MAX_POLL_TRIES) {
				mwifiex_dbg(adapter, ERROR,
					    "FW not ready to dump\n");
				ret = -1;
				goto done;
			}
		}
		usleep_range(100, 200);
	}

	entry->mem_ptr = vmalloc(0xf0000 + 1);
	if (!entry->mem_ptr) {
		ret = -1;
		goto done;
	}
	dbg_ptr = entry->mem_ptr;
	entry->mem_size = 0xf0000;
	end_ptr = dbg_ptr + entry->mem_size;

	done_flag = entry->done_flag;
	mwifiex_dbg(adapter, DUMP,
		    "Start %s output, please wait...\n", entry->mem_name);

	while (true) {
		stat = mwifiex_sdio_rdwr_firmware(adapter, done_flag);
		if (stat == RDWR_STATUS_FAILURE)
			goto done;
		for (reg = reg_start; reg <= reg_end; reg++) {
			*dbg_ptr = sdio_readb(card->func, reg, &ret);
			if (ret) {
				mwifiex_dbg(adapter, ERROR,
					    "SDIO read err\n");
				goto done;
			}
			dbg_ptr++;
			if (dbg_ptr >= end_ptr) {
				u8 *tmp_ptr;

				tmp_ptr = vmalloc(entry->mem_size + 0x4000 + 1);
				if (!tmp_ptr)
					goto done;

				memcpy(tmp_ptr, entry->mem_ptr,
				       entry->mem_size);
				vfree(entry->mem_ptr);
				entry->mem_ptr = tmp_ptr;
				tmp_ptr = NULL;
				dbg_ptr = entry->mem_ptr + entry->mem_size;
				entry->mem_size += 0x4000;
				end_ptr = entry->mem_ptr + entry->mem_size;
			}
		}
		if (stat == RDWR_STATUS_DONE) {
			entry->mem_size = dbg_ptr - entry->mem_ptr;
			mwifiex_dbg(adapter, DUMP, "dump %s done size=0x%x\n",
				    entry->mem_name, entry->mem_size);
			ret = 0;
			break;
		}
	}
	mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump end ==\n");

done:
	if (ret) {
		mwifiex_dbg(adapter, ERROR, "firmware dump failed\n");
		if (entry->mem_ptr) {
			vfree(entry->mem_ptr);
			entry->mem_ptr = NULL;
		}
		entry->mem_size = 0;
	}
	sdio_release_host(card->func);
}

2546 2547
static void mwifiex_sdio_device_dump_work(struct mwifiex_adapter *adapter)
{
2548 2549
	struct sdio_mmc_card *card = adapter->card;

2550
	mwifiex_drv_info_dump(adapter);
2551 2552 2553 2554
	if (card->fw_dump_enh)
		mwifiex_sdio_generic_fw_dump(adapter);
	else
		mwifiex_sdio_fw_dump(adapter);
2555
	mwifiex_upload_device_dump(adapter);
2556 2557
}

2558 2559
static void mwifiex_sdio_work(struct work_struct *work)
{
2560
	if (test_and_clear_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP,
2561
			       &iface_work_flags))
2562
		mwifiex_sdio_device_dump_work(save_adapter);
2563 2564 2565
	if (test_and_clear_bit(MWIFIEX_IFACE_WORK_CARD_RESET,
			       &iface_work_flags))
		mwifiex_sdio_card_reset_work(save_adapter);
2566
}
2567

2568
static DECLARE_WORK(sdio_work, mwifiex_sdio_work);
2569 2570 2571
/* This function resets the card */
static void mwifiex_sdio_card_reset(struct mwifiex_adapter *adapter)
{
2572 2573
	save_adapter = adapter;
	if (test_bit(MWIFIEX_IFACE_WORK_CARD_RESET, &iface_work_flags))
2574 2575
		return;

2576
	set_bit(MWIFIEX_IFACE_WORK_CARD_RESET, &iface_work_flags);
2577

2578
	schedule_work(&sdio_work);
2579 2580
}

2581
/* This function dumps FW information */
2582
static void mwifiex_sdio_device_dump(struct mwifiex_adapter *adapter)
2583
{
2584
	save_adapter = adapter;
2585
	if (test_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP, &iface_work_flags))
2586 2587
		return;

2588
	set_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP, &iface_work_flags);
2589
	schedule_work(&sdio_work);
2590 2591
}

2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607
/* Function to dump SDIO function registers and SDIO scratch registers in case
 * of FW crash
 */
static int
mwifiex_sdio_reg_dump(struct mwifiex_adapter *adapter, char *drv_buf)
{
	char *p = drv_buf;
	struct sdio_mmc_card *cardp = adapter->card;
	int ret = 0;
	u8 count, func, data, index = 0, size = 0;
	u8 reg, reg_start, reg_end;
	char buf[256], *ptr;

	if (!p)
		return 0;

2608
	mwifiex_dbg(adapter, MSG, "SDIO register dump start\n");
2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673

	mwifiex_pm_wakeup_card(adapter);

	sdio_claim_host(cardp->func);

	for (count = 0; count < 5; count++) {
		memset(buf, 0, sizeof(buf));
		ptr = buf;

		switch (count) {
		case 0:
			/* Read the registers of SDIO function0 */
			func = count;
			reg_start = 0;
			reg_end = 9;
			break;
		case 1:
			/* Read the registers of SDIO function1 */
			func = count;
			reg_start = cardp->reg->func1_dump_reg_start;
			reg_end = cardp->reg->func1_dump_reg_end;
			break;
		case 2:
			index = 0;
			func = 1;
			reg_start = cardp->reg->func1_spec_reg_table[index++];
			size = cardp->reg->func1_spec_reg_num;
			reg_end = cardp->reg->func1_spec_reg_table[size-1];
			break;
		default:
			/* Read the scratch registers of SDIO function1 */
			if (count == 4)
				mdelay(100);
			func = 1;
			reg_start = cardp->reg->func1_scratch_reg;
			reg_end = reg_start + MWIFIEX_SDIO_SCRATCH_SIZE;
		}

		if (count != 2)
			ptr += sprintf(ptr, "SDIO Func%d (%#x-%#x): ",
				       func, reg_start, reg_end);
		else
			ptr += sprintf(ptr, "SDIO Func%d: ", func);

		for (reg = reg_start; reg <= reg_end;) {
			if (func == 0)
				data = sdio_f0_readb(cardp->func, reg, &ret);
			else
				data = sdio_readb(cardp->func, reg, &ret);

			if (count == 2)
				ptr += sprintf(ptr, "(%#x) ", reg);
			if (!ret) {
				ptr += sprintf(ptr, "%02x ", data);
			} else {
				ptr += sprintf(ptr, "ERR");
				break;
			}

			if (count == 2 && reg < reg_end)
				reg = cardp->reg->func1_spec_reg_table[index++];
			else
				reg++;
		}

2674
		mwifiex_dbg(adapter, MSG, "%s\n", buf);
2675 2676 2677 2678 2679
		p += sprintf(p, "%s\n", buf);
	}

	sdio_release_host(cardp->func);

2680
	mwifiex_dbg(adapter, MSG, "SDIO register dump end\n");
2681 2682 2683 2684

	return p - drv_buf;
}

2685 2686 2687 2688
static struct mwifiex_if_ops sdio_ops = {
	.init_if = mwifiex_init_sdio,
	.cleanup_if = mwifiex_cleanup_sdio,
	.check_fw_status = mwifiex_check_fw_status,
2689
	.check_winner_status = mwifiex_check_winner_status,
2690 2691 2692 2693
	.prog_fw = mwifiex_prog_fw_w_helper,
	.register_dev = mwifiex_register_dev,
	.unregister_dev = mwifiex_unregister_dev,
	.enable_int = mwifiex_sdio_enable_host_int,
D
Daniel Drake 已提交
2694
	.disable_int = mwifiex_sdio_disable_host_int,
2695 2696 2697 2698 2699 2700 2701 2702
	.process_int_status = mwifiex_process_int_status,
	.host_to_card = mwifiex_sdio_host_to_card,
	.wakeup = mwifiex_pm_wakeup_card,
	.wakeup_complete = mwifiex_pm_wakeup_card_complete,

	/* SDIO specific */
	.update_mp_end_port = mwifiex_update_mp_end_port,
	.cleanup_mpa_buf = mwifiex_cleanup_mpa_buf,
2703 2704
	.cmdrsp_complete = mwifiex_sdio_cmdrsp_complete,
	.event_complete = mwifiex_sdio_event_complete,
2705
	.dnld_fw = mwifiex_sdio_dnld_fw,
2706
	.card_reset = mwifiex_sdio_card_reset,
2707
	.reg_dump = mwifiex_sdio_reg_dump,
2708
	.device_dump = mwifiex_sdio_device_dump,
2709
	.deaggr_pkt = mwifiex_deaggr_sdio_pkt,
2710 2711 2712 2713 2714
};

/*
 * This function initializes the SDIO driver.
 *
2715
 * This registers the device with SDIO bus.
2716 2717 2718 2719
 */
static int
mwifiex_sdio_init_module(void)
{
2720 2721 2722
	/* Clear the flag in case user removes the card. */
	user_rmmod = 0;

2723
	return sdio_register_driver(&mwifiex_sdio);
2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737
}

/*
 * This function cleans up the SDIO driver.
 *
 * The following major steps are followed for cleanup -
 *      - Resume the device if its suspended
 *      - Disconnect the device if connected
 *      - Shutdown the firmware
 *      - Unregister the device from SDIO bus.
 */
static void
mwifiex_sdio_cleanup_module(void)
{
2738 2739
	/* Set the flag as user is removing this module. */
	user_rmmod = 1;
2740
	cancel_work_sync(&sdio_work);
2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751

	sdio_unregister_driver(&mwifiex_sdio);
}

module_init(mwifiex_sdio_init_module);
module_exit(mwifiex_sdio_cleanup_module);

MODULE_AUTHOR("Marvell International Ltd.");
MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION);
MODULE_VERSION(SDIO_VERSION);
MODULE_LICENSE("GPL v2");
2752
MODULE_FIRMWARE(SD8786_DEFAULT_FW_NAME);
2753 2754
MODULE_FIRMWARE(SD8787_DEFAULT_FW_NAME);
MODULE_FIRMWARE(SD8797_DEFAULT_FW_NAME);
2755
MODULE_FIRMWARE(SD8897_DEFAULT_FW_NAME);
2756
MODULE_FIRMWARE(SD8887_DEFAULT_FW_NAME);
2757
MODULE_FIRMWARE(SD8997_DEFAULT_FW_NAME);