pcie.c 63.9 KB
Newer Older
1 2 3 4 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 34 35 36 37 38 39 40 41
/*
 * Marvell Wireless LAN device driver: PCIE specific handling
 *
 * Copyright (C) 2011, Marvell International Ltd.
 *
 * 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 "pcie.h"

#define PCIE_VERSION	"1.0"
#define DRV_NAME        "Marvell mwifiex PCIe"

static u8 user_rmmod;

static struct mwifiex_if_ops pcie_ops;

static struct semaphore add_remove_card_sem;
static int mwifiex_pcie_enable_host_int(struct mwifiex_adapter *adapter);
static int mwifiex_pcie_resume(struct pci_dev *pdev);

42 43 44
static int
mwifiex_map_pci_memory(struct mwifiex_adapter *adapter, struct sk_buff *skb,
		       int size, int flags)
45
{
46 47
	struct pcie_service_card *card = adapter->card;
	dma_addr_t buf_pa;
48

49 50 51 52 53 54 55
	buf_pa = pci_map_single(card->dev, skb->data, size, flags);
	if (pci_dma_mapping_error(card->dev, buf_pa)) {
		dev_err(adapter->dev, "failed to map pci memory!\n");
		return -1;
	}
	memcpy(skb->cb, &buf_pa, sizeof(dma_addr_t));
	return 0;
56 57 58 59 60 61 62 63 64
}

/*
 * This function reads sleep cookie and checks if FW is ready
 */
static bool mwifiex_pcie_ok_to_access_hw(struct mwifiex_adapter *adapter)
{
	u32 *cookie_addr;
	struct pcie_service_card *card = adapter->card;
65 66 67 68
	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;

	if (!reg->sleep_cookie)
		return true;
69

70 71
	if (card->sleep_cookie_vbase) {
		cookie_addr = (u32 *)card->sleep_cookie_vbase;
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
		dev_dbg(adapter->dev, "info: ACCESS_HW: sleep cookie=0x%x\n",
			*cookie_addr);
		if (*cookie_addr == FW_AWAKE_COOKIE)
			return true;
	}

	return false;
}

/*
 * This function probes an mwifiex device and registers it. It allocates
 * the card structure, enables PCIE function number and initiates the
 * device registration and initialization procedure by adding a logical
 * interface.
 */
static int mwifiex_pcie_probe(struct pci_dev *pdev,
					const struct pci_device_id *ent)
{
	struct pcie_service_card *card;

	pr_debug("info: vendor=0x%4.04X device=0x%4.04X rev=%d\n",
93
		 pdev->vendor, pdev->device, pdev->revision);
94 95

	card = kzalloc(sizeof(struct pcie_service_card), GFP_KERNEL);
96
	if (!card)
97 98 99 100
		return -ENOMEM;

	card->dev = pdev;

101 102 103 104 105 106 107
	if (ent->driver_data) {
		struct mwifiex_pcie_device *data = (void *)ent->driver_data;
		card->pcie.firmware = data->firmware;
		card->pcie.reg = data->reg;
		card->pcie.blksz_fw_dl = data->blksz_fw_dl;
	}

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
	if (mwifiex_add_card(card, &add_remove_card_sem, &pcie_ops,
			     MWIFIEX_PCIE)) {
		pr_err("%s failed\n", __func__);
		kfree(card);
		return -1;
	}

	return 0;
}

/*
 * This function removes the interface and frees up the card structure.
 */
static void mwifiex_pcie_remove(struct pci_dev *pdev)
{
	struct pcie_service_card *card;
	struct mwifiex_adapter *adapter;
125
	struct mwifiex_private *priv;
126 127 128 129 130 131 132 133 134 135
	int i;

	card = pci_get_drvdata(pdev);
	if (!card)
		return;

	adapter = card->adapter;
	if (!adapter || !adapter->priv_num)
		return;

136 137 138
	/* In case driver is removed when asynchronous FW load is in progress */
	wait_for_completion(&adapter->fw_load);

139 140 141 142 143 144 145 146
	if (user_rmmod) {
#ifdef CONFIG_PM
		if (adapter->is_suspended)
			mwifiex_pcie_resume(pdev);
#endif

		for (i = 0; i < adapter->priv_num; i++)
			if ((GET_BSS_ROLE(adapter->priv[i]) ==
147 148
			     MWIFIEX_BSS_ROLE_STA) &&
			    adapter->priv[i]->media_connected)
149 150
				mwifiex_deauthenticate(adapter->priv[i], NULL);

151
		priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
152

153 154 155
		mwifiex_disable_auto_ds(priv);

		mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
	}

	mwifiex_remove_card(card->adapter, &add_remove_card_sem);
	kfree(card);
}

/*
 * 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_pcie_suspend(struct pci_dev *pdev, pm_message_t state)
{
	struct mwifiex_adapter *adapter;
	struct pcie_service_card *card;
174
	int hs_actived;
175 176 177

	if (pdev) {
		card = (struct pcie_service_card *) pci_get_drvdata(pdev);
178
		if (!card || !card->adapter) {
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
			pr_err("Card or adapter structure is not valid\n");
			return 0;
		}
	} else {
		pr_err("PCIE device is not specified\n");
		return 0;
	}

	adapter = card->adapter;

	hs_actived = mwifiex_enable_hs(adapter);

	/* Indicate device suspended */
	adapter->is_suspended = true;

	return 0;
}

/*
 * 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_pcie_resume(struct pci_dev *pdev)
{
	struct mwifiex_adapter *adapter;
	struct pcie_service_card *card;

	if (pdev) {
		card = (struct pcie_service_card *) pci_get_drvdata(pdev);
		if (!card || !card->adapter) {
			pr_err("Card or adapter structure is not valid\n");
			return 0;
		}
	} else {
		pr_err("PCIE device is not specified\n");
		return 0;
	}

	adapter = card->adapter;

	if (!adapter->is_suspended) {
		dev_warn(adapter->dev, "Device already resumed\n");
		return 0;
	}

	adapter->is_suspended = false;

	mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
231
			  MWIFIEX_ASYNC_CMD);
232 233 234 235 236 237 238 239

	return 0;
}

static DEFINE_PCI_DEVICE_TABLE(mwifiex_ids) = {
	{
		PCIE_VENDOR_ID_MARVELL, PCIE_DEVICE_ID_MARVELL_88W8766P,
		PCI_ANY_ID, PCI_ANY_ID, 0, 0,
240
		.driver_data = (unsigned long) &mwifiex_pcie8766,
241
	},
Avinash Patil's avatar
Avinash Patil committed
242 243 244 245 246
	{
		PCIE_VENDOR_ID_MARVELL, PCIE_DEVICE_ID_MARVELL_88W8897,
		PCI_ANY_ID, PCI_ANY_ID, 0, 0,
		.driver_data = (unsigned long) &mwifiex_pcie8897,
	},
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
	{},
};

MODULE_DEVICE_TABLE(pci, mwifiex_ids);

/* PCI Device Driver */
static struct pci_driver __refdata mwifiex_pcie = {
	.name     = "mwifiex_pcie",
	.id_table = mwifiex_ids,
	.probe    = mwifiex_pcie_probe,
	.remove   = mwifiex_pcie_remove,
#ifdef CONFIG_PM
	/* Power Management Hooks */
	.suspend  = mwifiex_pcie_suspend,
	.resume   = mwifiex_pcie_resume,
#endif
};

/*
 * This function writes data into PCIE card register.
 */
static int mwifiex_write_reg(struct mwifiex_adapter *adapter, int reg, u32 data)
{
	struct pcie_service_card *card = adapter->card;

	iowrite32(data, card->pci_mmap1 + reg);

	return 0;
}

/*
 * This function reads data from PCIE card register.
 */
static int mwifiex_read_reg(struct mwifiex_adapter *adapter, int reg, u32 *data)
{
	struct pcie_service_card *card = adapter->card;

	*data = ioread32(card->pci_mmap1 + reg);

	return 0;
}

/*
 * 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)
{
	int i = 0;
298 299
	struct pcie_service_card *card = adapter->card;
	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
300

301
	while (reg->sleep_cookie && mwifiex_pcie_ok_to_access_hw(adapter)) {
302
		i++;
303
		usleep_range(10, 20);
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
		/* 50ms max wait */
		if (i == 50000)
			break;
	}

	dev_dbg(adapter->dev, "event: Wakeup device...\n");

	/* Enable interrupts or any chip access will wakeup device */
	if (mwifiex_write_reg(adapter, PCIE_HOST_INT_MASK, HOST_INTR_MASK)) {
		dev_warn(adapter->dev, "Enable host interrupt failed\n");
		return -1;
	}

	dev_dbg(adapter->dev, "PCIE wakeup: Setting PS_STATE_AWAKE\n");
	adapter->ps_state = PS_STATE_AWAKE;

	return 0;
}

/*
 * 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)
{
	dev_dbg(adapter->dev, "cmd: Wakeup device completed\n");

	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.
 */
static int mwifiex_pcie_disable_host_int(struct mwifiex_adapter *adapter)
{
	if (mwifiex_pcie_ok_to_access_hw(adapter)) {
		if (mwifiex_write_reg(adapter, PCIE_HOST_INT_MASK,
				      0x00000000)) {
			dev_warn(adapter->dev, "Disable host interrupt failed\n");
			return -1;
		}
	}

	return 0;
}

/*
 * This function enables the host interrupt.
 *
 * The host interrupt enable mask is written to the card
 * host interrupt mask register.
 */
static int mwifiex_pcie_enable_host_int(struct mwifiex_adapter *adapter)
{
	if (mwifiex_pcie_ok_to_access_hw(adapter)) {
		/* Simply write the mask to the register */
		if (mwifiex_write_reg(adapter, PCIE_HOST_INT_MASK,
				      HOST_INTR_MASK)) {
			dev_warn(adapter->dev, "Enable host interrupt failed\n");
			return -1;
		}
	}

	return 0;
}

/*
375 376 377 378 379
 * This function initializes TX buffer ring descriptors
 */
static int mwifiex_init_txq_ring(struct mwifiex_adapter *adapter)
{
	struct pcie_service_card *card = adapter->card;
Avinash Patil's avatar
Avinash Patil committed
380
	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
381
	struct mwifiex_pcie_buf_desc *desc;
Avinash Patil's avatar
Avinash Patil committed
382
	struct mwifiex_pfu_buf_desc *desc2;
383 384 385 386
	int i;

	for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
		card->tx_buf_list[i] = NULL;
Avinash Patil's avatar
Avinash Patil committed
387 388 389 390 391 392 393 394 395 396 397
		if (reg->pfu_enabled) {
			card->txbd_ring[i] = (void *)card->txbd_ring_vbase +
					     (sizeof(*desc2) * i);
			desc2 = card->txbd_ring[i];
			memset(desc2, 0, sizeof(*desc2));
		} else {
			card->txbd_ring[i] = (void *)card->txbd_ring_vbase +
					     (sizeof(*desc) * i);
			desc = card->txbd_ring[i];
			memset(desc, 0, sizeof(*desc));
		}
398 399 400 401 402 403 404 405 406 407 408 409
	}

	return 0;
}

/* This function initializes RX buffer ring descriptors. Each SKB is allocated
 * here and after mapping PCI memory, its physical address is assigned to
 * PCIE Rx buffer descriptor's physical address.
 */
static int mwifiex_init_rxq_ring(struct mwifiex_adapter *adapter)
{
	struct pcie_service_card *card = adapter->card;
Avinash Patil's avatar
Avinash Patil committed
410
	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
411 412
	struct sk_buff *skb;
	struct mwifiex_pcie_buf_desc *desc;
Avinash Patil's avatar
Avinash Patil committed
413
	struct mwifiex_pfu_buf_desc *desc2;
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
	dma_addr_t buf_pa;
	int i;

	for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
		/* Allocate skb here so that firmware can DMA data from it */
		skb = dev_alloc_skb(MWIFIEX_RX_DATA_BUF_SIZE);
		if (!skb) {
			dev_err(adapter->dev,
				"Unable to allocate skb for RX ring.\n");
			kfree(card->rxbd_ring_vbase);
			return -ENOMEM;
		}

		if (mwifiex_map_pci_memory(adapter, skb,
					   MWIFIEX_RX_DATA_BUF_SIZE,
					   PCI_DMA_FROMDEVICE))
			return -1;

		MWIFIEX_SKB_PACB(skb, &buf_pa);

		dev_dbg(adapter->dev,
			"info: RX ring: skb=%p len=%d data=%p buf_pa=%#x:%x\n",
			skb, skb->len, skb->data, (u32)buf_pa,
			(u32)((u64)buf_pa >> 32));

		card->rx_buf_list[i] = skb;
Avinash Patil's avatar
Avinash Patil committed
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
		if (reg->pfu_enabled) {
			card->rxbd_ring[i] = (void *)card->rxbd_ring_vbase +
					     (sizeof(*desc2) * i);
			desc2 = card->rxbd_ring[i];
			desc2->paddr = buf_pa;
			desc2->len = (u16)skb->len;
			desc2->frag_len = (u16)skb->len;
			desc2->flags = reg->ring_flag_eop | reg->ring_flag_sop;
			desc2->offset = 0;
		} else {
			card->rxbd_ring[i] = (void *)(card->rxbd_ring_vbase +
					     (sizeof(*desc) * i));
			desc = card->rxbd_ring[i];
			desc->paddr = buf_pa;
			desc->len = (u16)skb->len;
			desc->flags = 0;
		}
457 458 459 460 461 462 463 464 465 466 467 468
	}

	return 0;
}

/* This function initializes event buffer ring descriptors. Each SKB is
 * allocated here and after mapping PCI memory, its physical address is assigned
 * to PCIE Rx buffer descriptor's physical address
 */
static int mwifiex_pcie_init_evt_ring(struct mwifiex_adapter *adapter)
{
	struct pcie_service_card *card = adapter->card;
469
	struct mwifiex_evt_buf_desc *desc;
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
	struct sk_buff *skb;
	dma_addr_t buf_pa;
	int i;

	for (i = 0; i < MWIFIEX_MAX_EVT_BD; i++) {
		/* Allocate skb here so that firmware can DMA data from it */
		skb = dev_alloc_skb(MAX_EVENT_SIZE);
		if (!skb) {
			dev_err(adapter->dev,
				"Unable to allocate skb for EVENT buf.\n");
			kfree(card->evtbd_ring_vbase);
			return -ENOMEM;
		}
		skb_put(skb, MAX_EVENT_SIZE);

		if (mwifiex_map_pci_memory(adapter, skb, MAX_EVENT_SIZE,
					   PCI_DMA_FROMDEVICE))
			return -1;

		MWIFIEX_SKB_PACB(skb, &buf_pa);

		dev_dbg(adapter->dev,
			"info: EVT ring: skb=%p len=%d data=%p buf_pa=%#x:%x\n",
			skb, skb->len, skb->data, (u32)buf_pa,
			(u32)((u64)buf_pa >> 32));

		card->evt_buf_list[i] = skb;
		card->evtbd_ring[i] = (void *)(card->evtbd_ring_vbase +
				      (sizeof(*desc) * i));
		desc = card->evtbd_ring[i];
		desc->paddr = buf_pa;
		desc->len = (u16)skb->len;
		desc->flags = 0;
	}

	return 0;
}

/* This function cleans up TX buffer rings. If any of the buffer list has valid
 * SKB address, associated SKB is freed.
 */
static void mwifiex_cleanup_txq_ring(struct mwifiex_adapter *adapter)
{
	struct pcie_service_card *card = adapter->card;
Avinash Patil's avatar
Avinash Patil committed
514
	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
515 516
	struct sk_buff *skb;
	struct mwifiex_pcie_buf_desc *desc;
Avinash Patil's avatar
Avinash Patil committed
517
	struct mwifiex_pfu_buf_desc *desc2;
518 519 520
	int i;

	for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
Avinash Patil's avatar
Avinash Patil committed
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
		if (reg->pfu_enabled) {
			desc2 = card->txbd_ring[i];
			if (card->tx_buf_list[i]) {
				skb = card->tx_buf_list[i];
				pci_unmap_single(card->dev, desc2->paddr,
						 skb->len, PCI_DMA_TODEVICE);
				dev_kfree_skb_any(skb);
			}
			memset(desc2, 0, sizeof(*desc2));
		} else {
			desc = card->txbd_ring[i];
			if (card->tx_buf_list[i]) {
				skb = card->tx_buf_list[i];
				pci_unmap_single(card->dev, desc->paddr,
						 skb->len, PCI_DMA_TODEVICE);
				dev_kfree_skb_any(skb);
			}
			memset(desc, 0, sizeof(*desc));
539 540 541 542 543 544 545 546 547 548 549 550 551
		}
		card->tx_buf_list[i] = NULL;
	}

	return;
}

/* This function cleans up RX buffer rings. If any of the buffer list has valid
 * SKB address, associated SKB is freed.
 */
static void mwifiex_cleanup_rxq_ring(struct mwifiex_adapter *adapter)
{
	struct pcie_service_card *card = adapter->card;
Avinash Patil's avatar
Avinash Patil committed
552
	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
553
	struct mwifiex_pcie_buf_desc *desc;
Avinash Patil's avatar
Avinash Patil committed
554
	struct mwifiex_pfu_buf_desc *desc2;
555 556 557 558
	struct sk_buff *skb;
	int i;

	for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
Avinash Patil's avatar
Avinash Patil committed
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
		if (reg->pfu_enabled) {
			desc2 = card->rxbd_ring[i];
			if (card->rx_buf_list[i]) {
				skb = card->rx_buf_list[i];
				pci_unmap_single(card->dev, desc2->paddr,
						 skb->len, PCI_DMA_TODEVICE);
				dev_kfree_skb_any(skb);
			}
			memset(desc2, 0, sizeof(*desc2));
		} else {
			desc = card->rxbd_ring[i];
			if (card->rx_buf_list[i]) {
				skb = card->rx_buf_list[i];
				pci_unmap_single(card->dev, desc->paddr,
						 skb->len, PCI_DMA_TODEVICE);
				dev_kfree_skb_any(skb);
			}
			memset(desc, 0, sizeof(*desc));
577
		}
Avinash Patil's avatar
Avinash Patil committed
578
		card->rx_buf_list[i] = NULL;
579 580 581 582 583 584 585 586 587 588 589
	}

	return;
}

/* This function cleans up event buffer rings. If any of the buffer list has
 * valid SKB address, associated SKB is freed.
 */
static void mwifiex_cleanup_evt_ring(struct mwifiex_adapter *adapter)
{
	struct pcie_service_card *card = adapter->card;
590
	struct mwifiex_evt_buf_desc *desc;
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
	struct sk_buff *skb;
	int i;

	for (i = 0; i < MWIFIEX_MAX_EVT_BD; i++) {
		desc = card->evtbd_ring[i];
		if (card->evt_buf_list[i]) {
			skb = card->evt_buf_list[i];
			pci_unmap_single(card->dev, desc->paddr, MAX_EVENT_SIZE,
					 PCI_DMA_FROMDEVICE);
			dev_kfree_skb_any(skb);
		}
		card->evt_buf_list[i] = NULL;
		memset(desc, 0, sizeof(*desc));
	}

	return;
}

/* This function creates buffer descriptor ring for TX
610 611 612 613
 */
static int mwifiex_pcie_create_txbd_ring(struct mwifiex_adapter *adapter)
{
	struct pcie_service_card *card = adapter->card;
614
	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
615 616 617 618 619 620 621

	/*
	 * driver maintaines the write pointer and firmware maintaines the read
	 * pointer. The write pointer starts at 0 (zero) while the read pointer
	 * starts at zero with rollover bit set
	 */
	card->txbd_wrptr = 0;
Avinash Patil's avatar
Avinash Patil committed
622 623 624 625 626

	if (reg->pfu_enabled)
		card->txbd_rdptr = 0;
	else
		card->txbd_rdptr |= reg->tx_rollover_ind;
627 628 629

	/* allocate shared memory for the BD ring and divide the same in to
	   several descriptors */
Avinash Patil's avatar
Avinash Patil committed
630 631 632 633 634 635 636
	if (reg->pfu_enabled)
		card->txbd_ring_size = sizeof(struct mwifiex_pfu_buf_desc) *
				       MWIFIEX_MAX_TXRX_BD;
	else
		card->txbd_ring_size = sizeof(struct mwifiex_pcie_buf_desc) *
				       MWIFIEX_MAX_TXRX_BD;

637
	dev_dbg(adapter->dev, "info: txbd_ring: Allocating %d bytes\n",
638
		card->txbd_ring_size);
639 640 641
	card->txbd_ring_vbase = pci_alloc_consistent(card->dev,
						     card->txbd_ring_size,
						     &card->txbd_ring_pbase);
642
	if (!card->txbd_ring_vbase) {
643 644 645
		dev_err(adapter->dev,
			"allocate consistent memory (%d bytes) failed!\n",
			card->txbd_ring_size);
646
		return -ENOMEM;
647
	}
648 649
	dev_dbg(adapter->dev,
		"info: txbd_ring - base: %p, pbase: %#x:%x, len: %x\n",
650
		card->txbd_ring_vbase, (unsigned int)card->txbd_ring_pbase,
651
		(u32)((u64)card->txbd_ring_pbase >> 32), card->txbd_ring_size);
652

653
	return mwifiex_init_txq_ring(adapter);
654 655 656 657 658
}

static int mwifiex_pcie_delete_txbd_ring(struct mwifiex_adapter *adapter)
{
	struct pcie_service_card *card = adapter->card;
659
	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
660

661
	mwifiex_cleanup_txq_ring(adapter);
662

663 664 665 666
	if (card->txbd_ring_vbase)
		pci_free_consistent(card->dev, card->txbd_ring_size,
				    card->txbd_ring_vbase,
				    card->txbd_ring_pbase);
667 668
	card->txbd_ring_size = 0;
	card->txbd_wrptr = 0;
669
	card->txbd_rdptr = 0 | reg->tx_rollover_ind;
670
	card->txbd_ring_vbase = NULL;
671
	card->txbd_ring_pbase = 0;
672 673 674 675 676 677 678 679 680 681

	return 0;
}

/*
 * This function creates buffer descriptor ring for RX
 */
static int mwifiex_pcie_create_rxbd_ring(struct mwifiex_adapter *adapter)
{
	struct pcie_service_card *card = adapter->card;
682
	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
683 684 685 686 687 688 689

	/*
	 * driver maintaines the read pointer and firmware maintaines the write
	 * pointer. The write pointer starts at 0 (zero) while the read pointer
	 * starts at zero with rollover bit set
	 */
	card->rxbd_wrptr = 0;
690
	card->rxbd_rdptr = reg->rx_rollover_ind;
691

Avinash Patil's avatar
Avinash Patil committed
692 693 694 695 696 697 698
	if (reg->pfu_enabled)
		card->rxbd_ring_size = sizeof(struct mwifiex_pfu_buf_desc) *
				       MWIFIEX_MAX_TXRX_BD;
	else
		card->rxbd_ring_size = sizeof(struct mwifiex_pcie_buf_desc) *
				       MWIFIEX_MAX_TXRX_BD;

699
	dev_dbg(adapter->dev, "info: rxbd_ring: Allocating %d bytes\n",
700
		card->rxbd_ring_size);
701 702 703
	card->rxbd_ring_vbase = pci_alloc_consistent(card->dev,
						     card->rxbd_ring_size,
						     &card->rxbd_ring_pbase);
704
	if (!card->rxbd_ring_vbase) {
705 706 707
		dev_err(adapter->dev,
			"allocate consistent memory (%d bytes) failed!\n",
			card->rxbd_ring_size);
708
		return -ENOMEM;
709 710
	}

711 712 713 714 715
	dev_dbg(adapter->dev,
		"info: rxbd_ring - base: %p, pbase: %#x:%x, len: %#x\n",
		card->rxbd_ring_vbase, (u32)card->rxbd_ring_pbase,
		(u32)((u64)card->rxbd_ring_pbase >> 32),
		card->rxbd_ring_size);
716

717
	return mwifiex_init_rxq_ring(adapter);
718 719 720 721 722 723 724 725
}

/*
 * This function deletes Buffer descriptor ring for RX
 */
static int mwifiex_pcie_delete_rxbd_ring(struct mwifiex_adapter *adapter)
{
	struct pcie_service_card *card = adapter->card;
726
	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
727

728
	mwifiex_cleanup_rxq_ring(adapter);
729

730 731 732 733
	if (card->rxbd_ring_vbase)
		pci_free_consistent(card->dev, card->rxbd_ring_size,
				    card->rxbd_ring_vbase,
				    card->rxbd_ring_pbase);
734 735
	card->rxbd_ring_size = 0;
	card->rxbd_wrptr = 0;
736
	card->rxbd_rdptr = 0 | reg->rx_rollover_ind;
737
	card->rxbd_ring_vbase = NULL;
738
	card->rxbd_ring_pbase = 0;
739 740 741 742 743 744 745 746 747 748

	return 0;
}

/*
 * This function creates buffer descriptor ring for Events
 */
static int mwifiex_pcie_create_evtbd_ring(struct mwifiex_adapter *adapter)
{
	struct pcie_service_card *card = adapter->card;
749
	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
750 751 752 753 754 755 756

	/*
	 * driver maintaines the read pointer and firmware maintaines the write
	 * pointer. The write pointer starts at 0 (zero) while the read pointer
	 * starts at zero with rollover bit set
	 */
	card->evtbd_wrptr = 0;
757
	card->evtbd_rdptr = reg->evt_rollover_ind;
758

759
	card->evtbd_ring_size = sizeof(struct mwifiex_evt_buf_desc) *
Avinash Patil's avatar
Avinash Patil committed
760 761
				MWIFIEX_MAX_EVT_BD;

762
	dev_dbg(adapter->dev, "info: evtbd_ring: Allocating %d bytes\n",
763
		card->evtbd_ring_size);
764 765 766
	card->evtbd_ring_vbase = pci_alloc_consistent(card->dev,
						      card->evtbd_ring_size,
						      &card->evtbd_ring_pbase);
767
	if (!card->evtbd_ring_vbase) {
768
		dev_err(adapter->dev,
769 770
			"allocate consistent memory (%d bytes) failed!\n",
			card->evtbd_ring_size);
771
		return -ENOMEM;
772 773
	}

774 775 776 777 778
	dev_dbg(adapter->dev,
		"info: CMDRSP/EVT bd_ring - base: %p pbase: %#x:%x len: %#x\n",
		card->evtbd_ring_vbase, (u32)card->evtbd_ring_pbase,
		(u32)((u64)card->evtbd_ring_pbase >> 32),
		card->evtbd_ring_size);
779

780
	return mwifiex_pcie_init_evt_ring(adapter);
781 782 783 784 785 786 787 788
}

/*
 * This function deletes Buffer descriptor ring for Events
 */
static int mwifiex_pcie_delete_evtbd_ring(struct mwifiex_adapter *adapter)
{
	struct pcie_service_card *card = adapter->card;
789
	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
790

791
	mwifiex_cleanup_evt_ring(adapter);
792

793 794 795 796
	if (card->evtbd_ring_vbase)
		pci_free_consistent(card->dev, card->evtbd_ring_size,
				    card->evtbd_ring_vbase,
				    card->evtbd_ring_pbase);
797
	card->evtbd_wrptr = 0;
798
	card->evtbd_rdptr = 0 | reg->evt_rollover_ind;
799 800
	card->evtbd_ring_size = 0;
	card->evtbd_ring_vbase = NULL;
801
	card->evtbd_ring_pbase = 0;
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816

	return 0;
}

/*
 * This function allocates a buffer for CMDRSP
 */
static int mwifiex_pcie_alloc_cmdrsp_buf(struct mwifiex_adapter *adapter)
{
	struct pcie_service_card *card = adapter->card;
	struct sk_buff *skb;

	/* Allocate memory for receiving command response data */
	skb = dev_alloc_skb(MWIFIEX_UPLD_SIZE);
	if (!skb) {
817 818
		dev_err(adapter->dev,
			"Unable to allocate skb for command response data.\n");
819 820 821
		return -ENOMEM;
	}
	skb_put(skb, MWIFIEX_UPLD_SIZE);
822 823 824
	if (mwifiex_map_pci_memory(adapter, skb, MWIFIEX_UPLD_SIZE,
				   PCI_DMA_FROMDEVICE))
		return -1;
825

826
	card->cmdrsp_buf = skb;
827 828 829 830 831 832 833 834 835 836

	return 0;
}

/*
 * This function deletes a buffer for CMDRSP
 */
static int mwifiex_pcie_delete_cmdrsp_buf(struct mwifiex_adapter *adapter)
{
	struct pcie_service_card *card;
837
	dma_addr_t buf_pa;
838 839 840 841 842 843

	if (!adapter)
		return 0;

	card = adapter->card;

844 845 846 847
	if (card && card->cmdrsp_buf) {
		MWIFIEX_SKB_PACB(card->cmdrsp_buf, &buf_pa);
		pci_unmap_single(card->dev, buf_pa, MWIFIEX_UPLD_SIZE,
				 PCI_DMA_FROMDEVICE);
848
		dev_kfree_skb_any(card->cmdrsp_buf);
849
	}
850

851 852 853 854
	if (card && card->cmd_buf) {
		MWIFIEX_SKB_PACB(card->cmd_buf, &buf_pa);
		pci_unmap_single(card->dev, buf_pa, MWIFIEX_SIZE_OF_CMD_BUFFER,
				 PCI_DMA_TODEVICE);
855
		dev_kfree_skb_any(card->cmd_buf);
856
	}
857 858 859 860 861 862 863 864 865 866
	return 0;
}

/*
 * This function allocates a buffer for sleep cookie
 */
static int mwifiex_pcie_alloc_sleep_cookie_buf(struct mwifiex_adapter *adapter)
{
	struct pcie_service_card *card = adapter->card;

867 868 869 870
	card->sleep_cookie_vbase = pci_alloc_consistent(card->dev, sizeof(u32),
						     &card->sleep_cookie_pbase);
	if (!card->sleep_cookie_vbase) {
		dev_err(adapter->dev, "pci_alloc_consistent failed!\n");
871 872 873
		return -ENOMEM;
	}
	/* Init val of Sleep Cookie */
874
	*(u32 *)card->sleep_cookie_vbase = FW_AWAKE_COOKIE;
875 876

	dev_dbg(adapter->dev, "alloc_scook: sleep cookie=0x%x\n",
877
		*((u32 *)card->sleep_cookie_vbase));
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893

	return 0;
}

/*
 * This function deletes buffer for sleep cookie
 */
static int mwifiex_pcie_delete_sleep_cookie_buf(struct mwifiex_adapter *adapter)
{
	struct pcie_service_card *card;

	if (!adapter)
		return 0;

	card = adapter->card;

894 895 896 897 898
	if (card && card->sleep_cookie_vbase) {
		pci_free_consistent(card->dev, sizeof(u32),
				    card->sleep_cookie_vbase,
				    card->sleep_cookie_pbase);
		card->sleep_cookie_vbase = NULL;
899 900 901 902 903
	}

	return 0;
}

904 905 906 907 908 909 910
/* This function flushes the TX buffer descriptor ring
 * This function defined as handler is also called while cleaning TXRX
 * during disconnect/ bss stop.
 */
static int mwifiex_clean_pcie_ring_buf(struct mwifiex_adapter *adapter)
{
	struct pcie_service_card *card = adapter->card;
911
	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
912 913 914
	u32 rdptr;

	/* Read the TX ring read pointer set by firmware */
915
	if (mwifiex_read_reg(adapter, reg->tx_rdptr, &rdptr)) {
916
		dev_err(adapter->dev,
917
			"Flush TXBD: failed to read reg->tx_rdptr\n");
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
		return -1;
	}

	if (!mwifiex_pcie_txbd_empty(card, rdptr)) {
		card->txbd_flush = 1;
		/* write pointer already set at last send
		 * send dnld-rdy intr again, wait for completion.
		 */
		if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
				      CPU_INTR_DNLD_RDY)) {
			dev_err(adapter->dev,
				"failed to assert dnld-rdy interrupt.\n");
			return -1;
		}
	}
	return 0;
}

936
/*
937
 * This function unmaps and frees downloaded data buffer
938
 */
939
static int mwifiex_pcie_send_data_complete(struct mwifiex_adapter *adapter)
940
{
941 942
	struct sk_buff *skb;
	dma_addr_t buf_pa;
Avinash Patil's avatar
Avinash Patil committed
943
	u32 wrdoneidx, rdptr, num_tx_buffs, unmap_count = 0;
944
	struct mwifiex_pcie_buf_desc *desc;
Avinash Patil's avatar
Avinash Patil committed
945
	struct mwifiex_pfu_buf_desc *desc2;
946
	struct pcie_service_card *card = adapter->card;
947
	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
948 949 950 951 952

	if (!mwifiex_pcie_ok_to_access_hw(adapter))
		mwifiex_pm_wakeup_card(adapter);

	/* Read the TX ring read pointer set by firmware */
953
	if (mwifiex_read_reg(adapter, reg->tx_rdptr, &rdptr)) {
954
		dev_err(adapter->dev,
955
			"SEND COMP: failed to read reg->tx_rdptr\n");
956 957 958
		return -1;
	}

959 960
	dev_dbg(adapter->dev, "SEND COMP: rdptr_prev=0x%x, rdptr=0x%x\n",
		card->txbd_rdptr, rdptr);
961

Avinash Patil's avatar
Avinash Patil committed
962
	num_tx_buffs = MWIFIEX_MAX_TXRX_BD << reg->tx_start_ptr;
963
	/* free from previous txbd_rdptr to current txbd_rdptr */
964 965 966 967
	while (((card->txbd_rdptr & reg->tx_mask) !=
		(rdptr & reg->tx_mask)) ||
	       ((card->txbd_rdptr & reg->tx_rollover_ind) !=
		(rdptr & reg->tx_rollover_ind))) {
Avinash Patil's avatar
Avinash Patil committed
968 969
		wrdoneidx = (card->txbd_rdptr & reg->tx_mask) >>
			    reg->tx_start_ptr;
970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989

		skb = card->tx_buf_list[wrdoneidx];
		if (skb) {
			dev_dbg(adapter->dev,
				"SEND COMP: Detach skb %p at txbd_rdidx=%d\n",
				skb, wrdoneidx);
			MWIFIEX_SKB_PACB(skb, &buf_pa);
			pci_unmap_single(card->dev, buf_pa, skb->len,
					 PCI_DMA_TODEVICE);

			unmap_count++;

			if (card->txbd_flush)
				mwifiex_write_data_complete(adapter, skb, 0,
							    -1);
			else
				mwifiex_write_data_complete(adapter, skb, 0, 0);
		}

		card->tx_buf_list[wrdoneidx] = NULL;
Avinash Patil's avatar
Avinash Patil committed
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006

		if (reg->pfu_enabled) {
			desc2 = (void *)card->txbd_ring[wrdoneidx];
			memset(desc2, 0, sizeof(*desc2));
		} else {
			desc = card->txbd_ring[wrdoneidx];
			memset(desc, 0, sizeof(*desc));
		}
		switch (card->dev->device) {
		case PCIE_DEVICE_ID_MARVELL_88W8766P:
			card->txbd_rdptr++;
			break;
		case PCIE_DEVICE_ID_MARVELL_88W8897:
			card->txbd_rdptr += reg->ring_tx_start_ptr;
			break;
		}

1007

1008
		if ((card->txbd_rdptr & reg->tx_mask) == num_tx_buffs)
1009
			card->txbd_rdptr = ((card->txbd_rdptr &
1010 1011
					     reg->tx_rollover_ind) ^
					     reg->tx_rollover_ind);
1012 1013 1014 1015 1016 1017
	}

	if (unmap_count)
		adapter->data_sent = false;

	if (card->txbd_flush) {
1018
		if (mwifiex_pcie_txbd_empty(card, card->txbd_rdptr))
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
			card->txbd_flush = 0;
		else
			mwifiex_clean_pcie_ring_buf(adapter);
	}

	return 0;
}

/* This function sends data buffer to device. First 4 bytes of payload
 * are filled with payload length and payload type. Then this payload
 * is mapped to PCI device memory. Tx ring pointers are advanced accordingly.
 * Download ready interrupt to FW is deffered if Tx ring is not full and
 * additional payload can be accomodated.
 */
static int
mwifiex_pcie_send_data(struct mwifiex_adapter *adapter, struct sk_buff *skb,
		       struct mwifiex_tx_param *tx_param)
{
	struct pcie_service_card *card = adapter->card;
1038
	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
Avinash Patil's avatar
Avinash Patil committed
1039
	u32 wrindx, num_tx_buffs, rx_val;
1040 1041
	int ret;
	dma_addr_t buf_pa;
1042
	struct mwifiex_pcie_buf_desc *desc;
Avinash Patil's avatar
Avinash Patil committed
1043
	struct mwifiex_pfu_buf_desc *desc2;
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
	__le16 *tmp;

	if (!(skb->data && skb->len)) {
		dev_err(adapter->dev, "%s(): invalid parameter <%p, %#x>\n",
			__func__, skb->data, skb->len);
		return -1;
	}

	if (!mwifiex_pcie_ok_to_access_hw(adapter))
		mwifiex_pm_wakeup_card(adapter);

Avinash Patil's avatar
Avinash Patil committed
1055
	num_tx_buffs = MWIFIEX_MAX_TXRX_BD << reg->tx_start_ptr;
1056 1057 1058
	dev_dbg(adapter->dev, "info: SEND DATA: <Rd: %#x, Wr: %#x>\n",
		card->txbd_rdptr, card->txbd_wrptr);
	if (mwifiex_pcie_txbd_not_full(card)) {
1059 1060 1061
		u8 *payload;

		adapter->data_sent = true;
1062
		payload = skb->data;
1063 1064 1065 1066
		tmp = (__le16 *)&payload[0];
		*tmp = cpu_to_le16((u16)skb->len);
		tmp = (__le16 *)&payload[2];
		*tmp = cpu_to_le16(MWIFIEX_TYPE_DATA);
1067 1068 1069 1070 1071

		if (mwifiex_map_pci_memory(adapter, skb, skb->len ,
					   PCI_DMA_TODEVICE))
			return -1;

Avinash Patil's avatar
Avinash Patil committed
1072
		wrindx = (card->txbd_wrptr & reg->tx_mask) >> reg->tx_start_ptr;
1073 1074
		MWIFIEX_SKB_PACB(skb, &buf_pa);
		card->tx_buf_list[wrindx] = skb;
1075

Avinash Patil's avatar
Avinash Patil committed
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
		if (reg->pfu_enabled) {
			desc2 = (void *)card->txbd_ring[wrindx];
			desc2->paddr = buf_pa;
			desc2->len = (u16)skb->len;
			desc2->frag_len = (u16)skb->len;
			desc2->offset = 0;
			desc2->flags = MWIFIEX_BD_FLAG_FIRST_DESC |
					 MWIFIEX_BD_FLAG_LAST_DESC;
		} else {
			desc = card->txbd_ring[wrindx];
			desc->paddr = buf_pa;
			desc->len = (u16)skb->len;
			desc->flags = MWIFIEX_BD_FLAG_FIRST_DESC |
				      MWIFIEX_BD_FLAG_LAST_DESC;
		}

		switch (card->dev->device) {
		case PCIE_DEVICE_ID_MARVELL_88W8766P:
			card->txbd_wrptr++;
			break;
		case PCIE_DEVICE_ID_MARVELL_88W8897:
			card->txbd_wrptr += reg->ring_tx_start_ptr;
			break;
		}

		if ((card->txbd_wrptr & reg->tx_mask) == num_tx_buffs)
1102
			card->txbd_wrptr = ((card->txbd_wrptr &
1103 1104
						reg->tx_rollover_ind) ^
						reg->tx_rollover_ind);
1105

Avinash Patil's avatar
Avinash Patil committed
1106
		rx_val = card->rxbd_rdptr & reg->rx_wrap_mask;
1107 1108
		/* Write the TX ring write pointer in to reg->tx_wrptr */
		if (mwifiex_write_reg(adapter, reg->tx_wrptr,
Avinash Patil's avatar
Avinash Patil committed
1109
				      card->txbd_wrptr | rx_val)) {
1110
			dev_err(adapter->dev,
1111
				"SEND DATA: failed to write reg->tx_wrptr\n");
1112 1113
			ret = -1;
			goto done_unmap;
1114
		}
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
		if ((mwifiex_pcie_txbd_not_full(card)) &&
		    tx_param->next_pkt_len) {
			/* have more packets and TxBD still can hold more */
			dev_dbg(adapter->dev,
				"SEND DATA: delay dnld-rdy interrupt.\n");
			adapter->data_sent = false;
		} else {
			/* Send the TX ready interrupt */
			if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
					      CPU_INTR_DNLD_RDY)) {
				dev_err(adapter->dev,
					"SEND DATA: failed to assert dnld-rdy interrupt.\n");
				ret = -1;
				goto done_unmap;
			}
1130 1131
		}
		dev_dbg(adapter->dev, "info: SEND DATA: Updated <Rd: %#x, Wr: "
1132
			"%#x> and sent packet to firmware successfully\n",
1133
			card->txbd_rdptr, card->txbd_wrptr);
1134
	} else {
1135 1136
		dev_dbg(adapter->dev,
			"info: TX Ring full, can't send packets to fw\n");
1137 1138 1139 1140
		adapter->data_sent = true;
		/* Send the TX ready interrupt */
		if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
				      CPU_INTR_DNLD_RDY))
1141 1142
			dev_err(adapter->dev,
				"SEND DATA: failed to assert door-bell intr\n");
1143 1144 1145
		return -EBUSY;
	}

1146 1147 1148 1149 1150
	return -EINPROGRESS;
done_unmap:
	MWIFIEX_SKB_PACB(skb, &buf_pa);
	pci_unmap_single(card->dev, buf_pa, skb->len, PCI_DMA_TODEVICE);
	card->tx_buf_list[wrindx] = NULL;
Avinash Patil's avatar
Avinash Patil committed
1151 1152 1153 1154 1155
	if (reg->pfu_enabled)
		memset(desc2, 0, sizeof(*desc2));
	else
		memset(desc, 0, sizeof(*desc));

1156
	return ret;
1157 1158 1159 1160 1161 1162 1163 1164 1165
}

/*
 * This function handles received buffer ring and
 * dispatches packets to upper
 */
static int mwifiex_pcie_process_recv_data(struct mwifiex_adapter *adapter)
{
	struct pcie_service_card *card = adapter->card;
1166
	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
Avinash Patil's avatar
Avinash Patil committed
1167
	u32 wrptr, rd_index, tx_val;
1168
	dma_addr_t buf_pa;
1169 1170
	int ret = 0;
	struct sk_buff *skb_tmp = NULL;
1171
	struct mwifiex_pcie_buf_desc *desc;
Avinash Patil's avatar
Avinash Patil committed
1172
	struct mwifiex_pfu_buf_desc *desc2;
1173

1174 1175 1176
	if (!mwifiex_pcie_ok_to_access_hw(adapter))
		mwifiex_pm_wakeup_card(adapter);

1177
	/* Read the RX ring Write pointer set by firmware */
1178
	if (mwifiex_read_reg(adapter, reg->rx_wrptr, &wrptr)) {
1179
		dev_err(adapter->dev,
1180
			"RECV DATA: failed to read reg->rx_wrptr\n");
1181 1182 1183
		ret = -1;
		goto done;
	}
1184
	card->rxbd_wrptr = wrptr;
1185

1186 1187 1188 1189
	while (((wrptr & reg->rx_mask) !=
		(card->rxbd_rdptr & reg->rx_mask)) ||
	       ((wrptr & reg->rx_rollover_ind) ==
		(card->rxbd_rdptr & reg->rx_rollover_ind))) {
1190 1191
		struct sk_buff *skb_data;
		u16 rx_len;
1192
		__le16 pkt_len;
1193

1194
		rd_index = card->rxbd_rdptr & reg->rx_mask;
1195 1196
		skb_data = card->rx_buf_list[rd_index];