pcie.c 64.2 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
/*
 * 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;

40 41
static int
mwifiex_map_pci_memory(struct mwifiex_adapter *adapter, struct sk_buff *skb,
42
		       size_t size, int flags)
43
{
44
	struct pcie_service_card *card = adapter->card;
45
	struct mwifiex_dma_mapping mapping;
46

47 48
	mapping.addr = pci_map_single(card->dev, skb->data, size, flags);
	if (pci_dma_mapping_error(card->dev, mapping.addr)) {
49 50 51
		dev_err(adapter->dev, "failed to map pci memory!\n");
		return -1;
	}
52 53
	mapping.len = size;
	memcpy(skb->cb, &mapping, sizeof(mapping));
54
	return 0;
55 56
}

57 58 59 60 61 62 63 64 65 66
static void mwifiex_unmap_pci_memory(struct mwifiex_adapter *adapter,
				     struct sk_buff *skb, int flags)
{
	struct pcie_service_card *card = adapter->card;
	struct mwifiex_dma_mapping mapping;

	MWIFIEX_SKB_PACB(skb, &mapping);
	pci_unmap_single(card->dev, mapping.addr, mapping.len, flags);
}

67 68 69 70 71 72 73
/*
 * 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;
74 75 76 77
	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;

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

79 80
	if (card->sleep_cookie_vbase) {
		cookie_addr = (u32 *)card->sleep_cookie_vbase;
81 82 83 84 85 86 87 88 89
		dev_dbg(adapter->dev, "info: ACCESS_HW: sleep cookie=0x%x\n",
			*cookie_addr);
		if (*cookie_addr == FW_AWAKE_COOKIE)
			return true;
	}

	return false;
}

90
#ifdef CONFIG_PM_SLEEP
91 92 93 94 95 96 97 98
/*
 * 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.
 */
99
static int mwifiex_pcie_suspend(struct device *dev)
100 101 102 103
{
	struct mwifiex_adapter *adapter;
	struct pcie_service_card *card;
	int hs_actived;
104
	struct pci_dev *pdev = to_pci_dev(dev);
105 106

	if (pdev) {
107
		card = pci_get_drvdata(pdev);
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
		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;

	hs_actived = mwifiex_enable_hs(adapter);

	/* Indicate device suspended */
	adapter->is_suspended = true;
123
	adapter->hs_enabling = false;
124 125 126 127 128 129 130 131 132 133 134 135

	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.
 */
136
static int mwifiex_pcie_resume(struct device *dev)
137 138 139
{
	struct mwifiex_adapter *adapter;
	struct pcie_service_card *card;
140
	struct pci_dev *pdev = to_pci_dev(dev);
141 142

	if (pdev) {
143
		card = pci_get_drvdata(pdev);
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
		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),
			  MWIFIEX_ASYNC_CMD);

	return 0;
}
167
#endif
168

169 170 171 172 173 174 175 176 177 178 179 180
/*
 * 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",
181
		 pdev->vendor, pdev->device, pdev->revision);
182 183

	card = kzalloc(sizeof(struct pcie_service_card), GFP_KERNEL);
184
	if (!card)
185 186 187 188
		return -ENOMEM;

	card->dev = pdev;

189 190 191 192 193
	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;
194
		card->pcie.tx_buf_size = data->tx_buf_size;
195 196
	}

197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
	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;
214
	struct mwifiex_private *priv;
215 216 217 218 219 220 221 222 223 224

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

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

	if (user_rmmod) {
225
#ifdef CONFIG_PM_SLEEP
226
		if (adapter->is_suspended)
227
			mwifiex_pcie_resume(&pdev->dev);
228 229
#endif

230
		mwifiex_deauthenticate_all(adapter);
231

232
		priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
233

234 235 236
		mwifiex_disable_auto_ds(priv);

		mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
237 238 239 240 241
	}

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

242 243 244 245 246 247 248 249
static void mwifiex_pcie_shutdown(struct pci_dev *pdev)
{
	user_rmmod = 1;
	mwifiex_pcie_remove(pdev);

	return;
}

250 251 252 253
static DEFINE_PCI_DEVICE_TABLE(mwifiex_ids) = {
	{
		PCIE_VENDOR_ID_MARVELL, PCIE_DEVICE_ID_MARVELL_88W8766P,
		PCI_ANY_ID, PCI_ANY_ID, 0, 0,
254
		.driver_data = (unsigned long) &mwifiex_pcie8766,
255
	},
A
Avinash Patil 已提交
256 257 258 259 260
	{
		PCIE_VENDOR_ID_MARVELL, PCIE_DEVICE_ID_MARVELL_88W8897,
		PCI_ANY_ID, PCI_ANY_ID, 0, 0,
		.driver_data = (unsigned long) &mwifiex_pcie8897,
	},
261 262 263 264 265
	{},
};

MODULE_DEVICE_TABLE(pci, mwifiex_ids);

266 267 268 269 270 271
#ifdef CONFIG_PM_SLEEP
/* Power Management Hooks */
static SIMPLE_DEV_PM_OPS(mwifiex_pcie_pm_ops, mwifiex_pcie_suspend,
				mwifiex_pcie_resume);
#endif

272 273 274 275 276 277
/* 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,
278 279 280 281
#ifdef CONFIG_PM_SLEEP
	.driver   = {
		.pm = &mwifiex_pcie_pm_ops,
	},
282
#endif
283
	.shutdown = mwifiex_pcie_shutdown,
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
};

/*
 * 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;
}

/*
311
 * This function adds delay loop to ensure FW is awake before proceeding.
312
 */
313
static void mwifiex_pcie_dev_wakeup_delay(struct mwifiex_adapter *adapter)
314 315 316
{
	int i = 0;

317
	while (mwifiex_pcie_ok_to_access_hw(adapter)) {
318
		i++;
319
		usleep_range(10, 20);
320
		/* 50ms max wait */
321
		if (i == 5000)
322 323 324
			break;
	}

325 326 327
	return;
}

328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
static void mwifiex_delay_for_sleep_cookie(struct mwifiex_adapter *adapter,
					   u32 max_delay_loop_cnt)
{
	struct pcie_service_card *card = adapter->card;
	u8 *buffer;
	u32 sleep_cookie, count;

	for (count = 0; count < max_delay_loop_cnt; count++) {
		buffer = card->cmdrsp_buf->data - INTF_HEADER_LEN;
		sleep_cookie = *(u32 *)buffer;

		if (sleep_cookie == MWIFIEX_DEF_SLEEP_COOKIE) {
			dev_dbg(adapter->dev,
				"sleep cookie found at count %d\n", count);
			break;
		}
		usleep_range(20, 30);
	}

	if (count >= max_delay_loop_cnt)
		dev_dbg(adapter->dev,
			"max count reached while accessing sleep cookie\n");
}

352 353 354 355 356 357 358
/* This function wakes up the card by reading fw_status register. */
static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
{
	u32 fw_status;
	struct pcie_service_card *card = adapter->card;
	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;

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

361 362 363 364 365 366
	if (reg->sleep_cookie)
		mwifiex_pcie_dev_wakeup_delay(adapter);

	/* Reading fw_status register will wakeup device */
	if (mwifiex_read_reg(adapter, reg->fw_status, &fw_status)) {
		dev_warn(adapter->dev, "Reading fw_status register failed\n");
367 368 369
		return -1;
	}

370 371 372 373 374
	if (reg->sleep_cookie) {
		mwifiex_pcie_dev_wakeup_delay(adapter);
		dev_dbg(adapter->dev, "PCIE wakeup: Setting PS_STATE_AWAKE\n");
		adapter->ps_state = PS_STATE_AWAKE;
	}
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430

	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;
}

/*
431 432 433 434 435
 * This function initializes TX buffer ring descriptors
 */
static int mwifiex_init_txq_ring(struct mwifiex_adapter *adapter)
{
	struct pcie_service_card *card = adapter->card;
A
Avinash Patil 已提交
436
	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
437
	struct mwifiex_pcie_buf_desc *desc;
A
Avinash Patil 已提交
438
	struct mwifiex_pfu_buf_desc *desc2;
439 440 441 442
	int i;

	for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
		card->tx_buf_list[i] = NULL;
A
Avinash Patil 已提交
443 444 445 446 447 448 449 450 451 452 453
		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));
		}
454 455 456 457 458 459 460 461 462 463 464 465
	}

	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;
A
Avinash Patil 已提交
466
	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
467 468
	struct sk_buff *skb;
	struct mwifiex_pcie_buf_desc *desc;
A
Avinash Patil 已提交
469
	struct mwifiex_pfu_buf_desc *desc2;
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
	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;

488
		buf_pa = MWIFIEX_SKB_DMA_ADDR(skb);
489 490 491 492 493 494 495

		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;
A
Avinash Patil 已提交
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
		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;
		}
513 514 515 516 517 518 519 520 521 522 523 524
	}

	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;
525
	struct mwifiex_evt_buf_desc *desc;
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
	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;

545
		buf_pa = MWIFIEX_SKB_DMA_ADDR(skb);
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569

		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;
A
Avinash Patil 已提交
570
	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
571 572
	struct sk_buff *skb;
	struct mwifiex_pcie_buf_desc *desc;
A
Avinash Patil 已提交
573
	struct mwifiex_pfu_buf_desc *desc2;
574 575 576
	int i;

	for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
A
Avinash Patil 已提交
577 578 579 580
		if (reg->pfu_enabled) {
			desc2 = card->txbd_ring[i];
			if (card->tx_buf_list[i]) {
				skb = card->tx_buf_list[i];
581 582
				mwifiex_unmap_pci_memory(adapter, skb,
							 PCI_DMA_TODEVICE);
A
Avinash Patil 已提交
583 584 585 586 587 588 589
				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];
590 591
				mwifiex_unmap_pci_memory(adapter, skb,
							 PCI_DMA_TODEVICE);
A
Avinash Patil 已提交
592 593 594
				dev_kfree_skb_any(skb);
			}
			memset(desc, 0, sizeof(*desc));
595 596 597 598 599 600 601 602 603 604 605 606 607
		}
		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;
A
Avinash Patil 已提交
608
	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
609
	struct mwifiex_pcie_buf_desc *desc;
A
Avinash Patil 已提交
610
	struct mwifiex_pfu_buf_desc *desc2;
611 612 613 614
	struct sk_buff *skb;
	int i;

	for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
A
Avinash Patil 已提交
615 616 617 618
		if (reg->pfu_enabled) {
			desc2 = card->rxbd_ring[i];
			if (card->rx_buf_list[i]) {
				skb = card->rx_buf_list[i];
619 620
				mwifiex_unmap_pci_memory(adapter, skb,
							 PCI_DMA_FROMDEVICE);
A
Avinash Patil 已提交
621 622 623 624 625 626 627
				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];
628 629
				mwifiex_unmap_pci_memory(adapter, skb,
							 PCI_DMA_FROMDEVICE);
A
Avinash Patil 已提交
630 631 632
				dev_kfree_skb_any(skb);
			}
			memset(desc, 0, sizeof(*desc));
633
		}
A
Avinash Patil 已提交
634
		card->rx_buf_list[i] = NULL;
635 636 637 638 639 640 641 642 643 644 645
	}

	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;
646
	struct mwifiex_evt_buf_desc *desc;
647 648 649 650 651 652 653
	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];
654 655
			mwifiex_unmap_pci_memory(adapter, skb,
						 PCI_DMA_FROMDEVICE);
656 657 658 659 660 661 662 663 664 665
			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
666 667 668 669
 */
static int mwifiex_pcie_create_txbd_ring(struct mwifiex_adapter *adapter)
{
	struct pcie_service_card *card = adapter->card;
670
	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
671 672 673 674 675 676 677

	/*
	 * 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;
A
Avinash Patil 已提交
678 679 680 681 682

	if (reg->pfu_enabled)
		card->txbd_rdptr = 0;
	else
		card->txbd_rdptr |= reg->tx_rollover_ind;
683 684 685

	/* allocate shared memory for the BD ring and divide the same in to
	   several descriptors */
A
Avinash Patil 已提交
686 687 688 689 690 691 692
	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;

693
	dev_dbg(adapter->dev, "info: txbd_ring: Allocating %d bytes\n",
694
		card->txbd_ring_size);
695 696 697
	card->txbd_ring_vbase = pci_alloc_consistent(card->dev,
						     card->txbd_ring_size,
						     &card->txbd_ring_pbase);
698
	if (!card->txbd_ring_vbase) {
699 700 701
		dev_err(adapter->dev,
			"allocate consistent memory (%d bytes) failed!\n",
			card->txbd_ring_size);
702
		return -ENOMEM;
703
	}
704 705
	dev_dbg(adapter->dev,
		"info: txbd_ring - base: %p, pbase: %#x:%x, len: %x\n",
706
		card->txbd_ring_vbase, (unsigned int)card->txbd_ring_pbase,
707
		(u32)((u64)card->txbd_ring_pbase >> 32), card->txbd_ring_size);
708

709
	return mwifiex_init_txq_ring(adapter);
710 711 712 713 714
}

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

717
	mwifiex_cleanup_txq_ring(adapter);
718

719 720 721 722
	if (card->txbd_ring_vbase)
		pci_free_consistent(card->dev, card->txbd_ring_size,
				    card->txbd_ring_vbase,
				    card->txbd_ring_pbase);
723 724
	card->txbd_ring_size = 0;
	card->txbd_wrptr = 0;
725
	card->txbd_rdptr = 0 | reg->tx_rollover_ind;
726
	card->txbd_ring_vbase = NULL;
727
	card->txbd_ring_pbase = 0;
728 729 730 731 732 733 734 735 736 737

	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;
738
	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
739 740 741 742 743 744 745

	/*
	 * 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;
746
	card->rxbd_rdptr = reg->rx_rollover_ind;
747

A
Avinash Patil 已提交
748 749 750 751 752 753 754
	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;

755
	dev_dbg(adapter->dev, "info: rxbd_ring: Allocating %d bytes\n",
756
		card->rxbd_ring_size);
757 758 759
	card->rxbd_ring_vbase = pci_alloc_consistent(card->dev,
						     card->rxbd_ring_size,
						     &card->rxbd_ring_pbase);
760
	if (!card->rxbd_ring_vbase) {
761 762 763
		dev_err(adapter->dev,
			"allocate consistent memory (%d bytes) failed!\n",
			card->rxbd_ring_size);
764
		return -ENOMEM;
765 766
	}

767 768 769 770 771
	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);
772

773
	return mwifiex_init_rxq_ring(adapter);
774 775 776 777 778 779 780 781
}

/*
 * 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;
782
	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
783

784
	mwifiex_cleanup_rxq_ring(adapter);
785

786 787 788 789
	if (card->rxbd_ring_vbase)
		pci_free_consistent(card->dev, card->rxbd_ring_size,
				    card->rxbd_ring_vbase,
				    card->rxbd_ring_pbase);
790 791
	card->rxbd_ring_size = 0;
	card->rxbd_wrptr = 0;
792
	card->rxbd_rdptr = 0 | reg->rx_rollover_ind;
793
	card->rxbd_ring_vbase = NULL;
794
	card->rxbd_ring_pbase = 0;
795 796 797 798 799 800 801 802 803 804

	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;
805
	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
806 807 808 809 810 811 812

	/*
	 * 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;
813
	card->evtbd_rdptr = reg->evt_rollover_ind;
814

815
	card->evtbd_ring_size = sizeof(struct mwifiex_evt_buf_desc) *
A
Avinash Patil 已提交
816 817
				MWIFIEX_MAX_EVT_BD;

818
	dev_dbg(adapter->dev, "info: evtbd_ring: Allocating %d bytes\n",
819
		card->evtbd_ring_size);
820 821 822
	card->evtbd_ring_vbase = pci_alloc_consistent(card->dev,
						      card->evtbd_ring_size,
						      &card->evtbd_ring_pbase);
823
	if (!card->evtbd_ring_vbase) {
824
		dev_err(adapter->dev,
825 826
			"allocate consistent memory (%d bytes) failed!\n",
			card->evtbd_ring_size);
827
		return -ENOMEM;
828 829
	}

830 831 832 833 834
	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);
835

836
	return mwifiex_pcie_init_evt_ring(adapter);
837 838 839 840 841 842 843 844
}

/*
 * 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;
845
	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
846

847
	mwifiex_cleanup_evt_ring(adapter);
848

849 850 851 852
	if (card->evtbd_ring_vbase)
		pci_free_consistent(card->dev, card->evtbd_ring_size,
				    card->evtbd_ring_vbase,
				    card->evtbd_ring_pbase);
853
	card->evtbd_wrptr = 0;
854
	card->evtbd_rdptr = 0 | reg->evt_rollover_ind;
855 856
	card->evtbd_ring_size = 0;
	card->evtbd_ring_vbase = NULL;
857
	card->evtbd_ring_pbase = 0;
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872

	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) {
873 874
		dev_err(adapter->dev,
			"Unable to allocate skb for command response data.\n");
875 876 877
		return -ENOMEM;
	}
	skb_put(skb, MWIFIEX_UPLD_SIZE);
878 879 880
	if (mwifiex_map_pci_memory(adapter, skb, MWIFIEX_UPLD_SIZE,
				   PCI_DMA_FROMDEVICE))
		return -1;
881

882
	card->cmdrsp_buf = skb;
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898

	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;

	if (!adapter)
		return 0;

	card = adapter->card;

899
	if (card && card->cmdrsp_buf) {
900 901
		mwifiex_unmap_pci_memory(adapter, card->cmdrsp_buf,
					 PCI_DMA_FROMDEVICE);
902
		dev_kfree_skb_any(card->cmdrsp_buf);
903
	}
904

905
	if (card && card->cmd_buf) {
906 907
		mwifiex_unmap_pci_memory(adapter, card->cmd_buf,
					 PCI_DMA_TODEVICE);
908
	}
909 910 911 912 913 914 915 916 917 918
	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;

919 920 921 922
	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");
923 924 925
		return -ENOMEM;
	}
	/* Init val of Sleep Cookie */
926
	*(u32 *)card->sleep_cookie_vbase = FW_AWAKE_COOKIE;
927 928

	dev_dbg(adapter->dev, "alloc_scook: sleep cookie=0x%x\n",
929
		*((u32 *)card->sleep_cookie_vbase));
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945

	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;

946 947 948 949 950
	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;
951 952 953 954 955
	}

	return 0;
}

956 957 958 959 960 961 962 963
/* 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;

964
	if (!mwifiex_pcie_txbd_empty(card, card->txbd_rdptr)) {
965 966 967 968 969 970 971 972 973 974 975 976 977 978
		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;
}

979
/*
980
 * This function unmaps and frees downloaded data buffer
981
 */
982
static int mwifiex_pcie_send_data_complete(struct mwifiex_adapter *adapter)
983
{
984
	struct sk_buff *skb;
A
Avinash Patil 已提交
985
	u32 wrdoneidx, rdptr, num_tx_buffs, unmap_count = 0;
986
	struct mwifiex_pcie_buf_desc *desc;
A
Avinash Patil 已提交
987
	struct mwifiex_pfu_buf_desc *desc2;
988
	struct pcie_service_card *card = adapter->card;
989
	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
990 991 992 993 994

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

	/* Read the TX ring read pointer set by firmware */
995
	if (mwifiex_read_reg(adapter, reg->tx_rdptr, &rdptr)) {
996
		dev_err(adapter->dev,
997
			"SEND COMP: failed to read reg->tx_rdptr\n");
998 999 1000
		return -1;
	}

1001 1002
	dev_dbg(adapter->dev, "SEND COMP: rdptr_prev=0x%x, rdptr=0x%x\n",
		card->txbd_rdptr, rdptr);
1003

A
Avinash Patil 已提交
1004
	num_tx_buffs = MWIFIEX_MAX_TXRX_BD << reg->tx_start_ptr;
1005
	/* free from previous txbd_rdptr to current txbd_rdptr */
1006 1007 1008 1009
	while (((card->txbd_rdptr & reg->tx_mask) !=
		(rdptr & reg->tx_mask)) ||
	       ((card->txbd_rdptr & reg->tx_rollover_ind) !=
		(rdptr & reg->tx_rollover_ind))) {
A
Avinash Patil 已提交
1010 1011
		wrdoneidx = (card->txbd_rdptr & reg->tx_mask) >>
			    reg->tx_start_ptr;
1012 1013

		skb = card->tx_buf_list[wrdoneidx];
1014

1015 1016 1017 1018
		if (skb) {
			dev_dbg(adapter->dev,
				"SEND COMP: Detach skb %p at txbd_rdidx=%d\n",
				skb, wrdoneidx);
1019 1020
			mwifiex_unmap_pci_memory(adapter, skb,
						 PCI_DMA_TODEVICE);
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031

			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;
A
Avinash Patil 已提交
1032 1033

		if (reg->pfu_enabled) {
1034
			desc2 = card->txbd_ring[wrdoneidx];
A
Avinash Patil 已提交
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
			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;
		}

1049

1050
		if ((card->txbd_rdptr & reg->tx_mask) == num_tx_buffs)
1051
			card->txbd_rdptr = ((card->txbd_rdptr &
1052 1053
					     reg->tx_rollover_ind) ^
					     reg->tx_rollover_ind);
1054 1055 1056 1057 1058 1059
	}

	if (unmap_count)
		adapter->data_sent = false;

	if (card->txbd_flush) {
1060
		if (mwifiex_pcie_txbd_empty(card, card->txbd_rdptr))
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
			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;
1080
	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
A
Avinash Patil 已提交
1081
	u32 wrindx, num_tx_buffs, rx_val;
1082 1083
	int ret;
	dma_addr_t buf_pa;
1084 1085
	struct mwifiex_pcie_buf_desc *desc = NULL;
	struct mwifiex_pfu_buf_desc *desc2 = NULL;
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
	__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);

A
Avinash Patil 已提交
1097
	num_tx_buffs = MWIFIEX_MAX_TXRX_BD << reg->tx_start_ptr;
1098 1099 1100
	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)) {
1101 1102 1103
		u8 *payload;

		adapter->data_sent = true;
1104
		payload = skb->data;
1105 1106 1107 1108
		tmp = (__le16 *)&payload[0];
		*tmp = cpu_to_le16((u16)skb->len);
		tmp = (__le16 *)&payload[2];
		*tmp = cpu_to_le16(MWIFIEX_TYPE_DATA);
1109

1110
		if (mwifiex_map_pci_memory(adapter, skb, skb->len,
1111 1112 1113
					   PCI_DMA_TODEVICE))
			return -1;

A
Avinash Patil 已提交
1114
		wrindx = (card->txbd_wrptr & reg->tx_mask) >> reg->tx_start_ptr;
1115
		buf_pa = MWIFIEX_SKB_DMA_ADDR(skb);
1116
		card->tx_buf_list[wrindx] = skb;
1117

A
Avinash Patil 已提交
1118
		if (reg->pfu_enabled) {
1119
			desc2 = card->txbd_ring[wrindx];
A
Avinash Patil 已提交
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
			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)
1144
			card->txbd_wrptr = ((card->txbd_wrptr &
1145 1146
						reg->tx_rollover_ind) ^
						reg->tx_rollover_ind);
1147

A
Avinash Patil 已提交
1148
		rx_val = card->rxbd_rdptr & reg->rx_wrap_mask;
1149 1150
		/* Write the TX ring write pointer in to reg->tx_wrptr */
		if (mwifiex_write_reg(adapter, reg->tx_wrptr,
A
Avinash Patil 已提交
1151
				      card->txbd_wrptr | rx_val)) {
1152
			dev_err(adapter->dev,
1153
				"SEND DATA: failed to write reg->tx_wrptr\n");
1154 1155
			ret = -1;
			goto done_unmap;
1156
		}
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
		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;
			}
1172 1173
		}
		dev_dbg(adapter->dev, "info: SEND DATA: Updated <Rd: %#x, Wr: "
1174
			"%#x> and sent packet to firmware successfully\n",
1175
			card->txbd_rdptr, card->txbd_wrptr);
1176
	} else {
1177 1178
		dev_dbg(adapter->dev,
			"info: TX Ring full, can't send packets to fw\n");
1179 1180 1181 1182
		adapter->data_sent = true;
		/* Send the TX ready interrupt */
		if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
				      CPU_INTR_DNLD_RDY))
1183 1184
			dev_err(adapter->dev,
				"SEND DATA: failed to assert door-bell intr\n");
1185 1186 1187
		return -EBUSY;
	}

1188 1189
	return -EINPROGRESS;
done_unmap:
1190
	mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_TODEVICE);
1191
	card->tx_buf_list[wrindx] = NULL;
A
Avinash Patil 已提交
1192 1193 1194 1195 1196
	if (reg->pfu_enabled)
		memset(desc2, 0, sizeof(*desc2));
	else
		memset(desc, 0, sizeof(*desc));

1197
	return ret;
1198 1199 1200 1201 1202 1203 1204 1205 1206
}

/*
 * 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;
1207
	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
A
Avinash Patil 已提交
1208
	u32 wrptr, rd_index, tx_val;
1209
	dma_addr_t buf_pa;
1210 1211
	int ret = 0;
	struct sk_buff *skb_tmp = NULL;
1212
	struct mwifiex_pcie_buf_desc *desc;
A
Avinash Patil 已提交
1213
	struct mwifiex_pfu_buf_desc *desc2;
1214

1215 1216 1217
	if (!mwifiex_pcie_ok_to_access_hw(adapter))
		mwifiex_pm_wakeup_card(adapter);

1218
	/* Read the RX ring Write pointer set by firmware */
1219
	if (mwifiex_read_reg(adapter, reg->rx_wrptr, &wrptr)) {
1220
		dev_err(adapter->dev,
1221
			"RECV DATA: failed to read reg->rx_wrptr\n");
1222 1223 1224
		ret = -1;
		goto done;
	}
1225
	card->rxbd_wrptr = wrptr;
1226

1227 1228 1229 1230
	while (((wrptr & reg->rx_mask) !=
		(card->rxbd_rdptr & reg->rx_mask)) ||
	       ((wrptr & reg->rx_rollover_ind) ==
		(card->rxbd_rdptr & reg->rx_rollover_ind))) {
1231 1232
		struct sk_buff *skb_data;
		u16 rx_len;
1233
		__le16 pkt_len;
1234

1235
		rd_index = card->rxbd_rdptr & reg->rx_mask;
1236 1237
		skb_data = card->rx_buf_list[rd_index];

1238 1239 1240 1241 1242 1243
		/* If skb allocation was failed earlier for Rx packet,
		 * rx_buf_list[rd_index] would have been left with a NULL.
		 */
		if (!skb_data)
			return -ENOMEM;

1244
		mwifiex_unmap_pci_memory(adapter, skb_data, PCI_DMA_FROMDEVICE);
1245 1246
		card->rx_buf_list[rd_index] = NULL;

1247
		/* Get data length from interface header -
1248 1249 1250 1251 1252
		 * first 2 bytes for len, next 2 bytes is for type
		 */
		pkt_len = *((__le16 *)skb_data->data);
		rx_len = le16_to_cpu(pkt_len);
		skb_put(skb_data, rx_len);
1253 1254 1255
		dev_dbg(adapter->dev,
			"info: RECV DATA: Rd=%#x, Wr=%#x, Len=%d\n",
			card->rxbd_rdptr, wrptr, rx_len);
1256 1257 1258 1259
		skb_pull(skb_data, INTF_HEADER_LEN);
		mwifiex_handle_rx_packet(adapter, skb_data);

		skb_tmp = dev_alloc_skb(MWIFIEX_RX_DATA_BUF_SIZE);
1260
		if (!skb_tmp) {
1261 1262 1263
			dev_err(adapter->dev,
				"Unable to allocate skb.\n");
			return -ENOMEM;
1264 1265
		}

1266 1267 1268 1269 1270
		if (mwifiex_map_pci_memory(adapter, skb_tmp,
					   MWIFIEX_RX_DATA_BUF_SIZE,
					   PCI_DMA_FROMDEVICE))
			return -1;

1271
		buf_pa = MWIFIEX_SKB_DMA_ADDR(skb_tmp);
1272 1273 1274 1275 1276

		dev_dbg(adapter->dev,
			"RECV DATA: Attach new sk_buff %p at rxbd_rdidx=%d\n",
			skb_tmp, rd_index);
		card->rx_buf_list[rd_index] = skb_tmp;
A
Avinash Patil 已提交
1277 1278

		if (reg->pfu_enabled) {
1279
			desc2 = card->rxbd_ring[rd_index];
A
Avinash Patil 已提交
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
			desc2->paddr = buf_pa;
			desc2->len = skb_tmp->len;
			desc2->frag_len = skb_tmp->len;
			desc2->offset = 0;
			desc2->flags = reg->ring_flag_sop | reg->ring_flag_eop;
		} else {
			desc = card->rxbd_ring[rd_index];
			desc->paddr = buf_pa;
			desc->len = skb_tmp->len;
			desc->flags = 0;
		}
1291

1292
		if ((++card->rxbd_rdptr & reg->rx_mask) ==
1293 1294
							MWIFIEX_MAX_TXRX_BD) {
			card->rxbd_rdptr = ((card->rxbd_rdptr &
1295 1296
					     reg->rx_rollover_ind) ^
					     reg->rx_rollover_ind);
1297 1298
		}
		dev_dbg(adapter->dev, "info: RECV DATA: <Rd: %#x, Wr: %#x>\n",
1299
			card->rxbd_rdptr, wrptr);
1300

A
Avinash Patil 已提交
1301
		tx_val = card->txbd_wrptr & reg->tx_wrap_mask;
1302 1303
		/* Write the RX ring read pointer in to reg->rx_rdptr */
		if (mwifiex_write_reg(adapter, reg->rx_rdptr,
A
Avinash Patil 已提交
1304
				      card->rxbd_rdptr | tx_val)) {
1305
			dev_err(adapter->dev,
1306
				"RECV DATA: failed to write reg->rx_rdptr\n");
1307 1308 1309 1310 1311
			ret = -1;
			goto done;
		}

		/* Read the RX ring Write pointer set by firmware */
1312
		if (mwifiex_read_reg(adapter, reg->rx_wrptr, &wrptr)) {
1313
			dev_err(adapter->dev,
1314
				"RECV DATA: failed to read reg->rx_wrptr\n");
1315 1316 1317
			ret = -1;
			goto done;
		}
1318 1319
		dev_dbg(adapter->dev,
			"info: RECV DATA: Rcvd packet from fw successfully\n");
1320
		card->rxbd_wrptr = wrptr;
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332
	}

done:
	return ret;
}

/*
 * This function downloads the boot command to device
 */
static int
mwifiex_pcie_send_boot_cmd(struct mwifiex_adapter *adapter, struct sk_buff *skb)
{
1333 1334
	dma_addr_t buf_pa;
	struct pcie_service_card *card = adapter->card;
1335
	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1336

1337
	if (!(skb->data && skb->len)) {
1338
		dev_err(adapter->dev,
1339 1340
			"Invalid parameter in %s <%p. len %d>\n",
			__func__, skb->data, skb->len);
1341 1342 1343
		return -1;
	}

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

1347
	buf_pa = MWIFIEX_SKB_DMA_ADDR(skb);
1348

1349 1350 1351 1352
	/* Write the lower 32bits of the physical address to low command
	 * address scratch register
	 */
	if (mwifiex_write_reg(adapter, reg->cmd_addr_lo, (u32)buf_pa)) {
1353 1354 1355
		dev_err(adapter->dev,
			"%s: failed to write download command to boot code.\n",
			__func__);
1356
		mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_TODEVICE);
1357 1358 1359
		return -1;
	}

1360 1361 1362 1363
	/* Write the upper 32bits of the physical address to high command
	 * address scratch register
	 */
	if (mwifiex_write_reg(adapter, reg->cmd_addr_hi,
1364
			      (u32)((u64)buf_pa >> 32))) {
1365 1366 1367
		dev_err(adapter->dev,
			"%s: failed to write download command to boot code.\n",
			__func__);
1368
		mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_TODEVICE);
1369 1370 1371
		return -1;
	}

1372 1373
	/* Write the command length to cmd_size scratch register */
	if (mwifiex_write_reg(adapter, reg->cmd_size, skb->len)) {
1374
		dev_err(adapter->dev,
1375
			"%s: failed to write command len to cmd_size scratch reg\n",
1376
			__func__);
1377
		mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_TODEVICE);
1378 1379 1380 1381 1382 1383
		return -1;
	}

	/* Ring the door bell */
	if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
			      CPU_INTR_DOOR_BELL)) {
1384 1385
		dev_err(adapter->dev,
			"%s: failed to assert door-bell intr\n", __func__);
1386
		mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_TODEVICE);
1387 1388 1389 1390 1391 1392
		return -1;
	}

	return 0;
}

1393 1394 1395 1396 1397 1398
/* This function init rx port in firmware which in turn enables to receive data
 * from device before transmitting any packet.
 */
static int mwifiex_pcie_init_fw_port(struct mwifiex_adapter *adapter)
{
	struct pcie_service_card *card = adapter->card;
1399
	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
A
Avinash Patil 已提交
1400
	int tx_wrap = card->txbd_wrptr & reg->tx_wrap_mask;
1401

1402
	/* Write the RX ring read pointer in to reg->rx_rdptr */
A
Avinash Patil 已提交
1403 1404
	if (mwifiex_write_reg(adapter, reg->rx_rdptr, card->rxbd_rdptr |
			      tx_wrap)) {
1405
		dev_err(adapter->dev,
1406
			"RECV DATA: failed to write reg->rx_rdptr\n");
1407 1408 1409 1410 1411 1412
		return -1;
	}
	return 0;
}

/* This function downloads commands to the device
1413 1414 1415 1416 1417
 */
static int
mwifiex_pcie_send_cmd(struct mwifiex_adapter *adapter, struct sk_buff *skb)
{
	struct pcie_service_card *card = adapter->card;
1418
	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1419
	int ret = 0;
1420 1421
	dma_addr_t cmd_buf_pa, cmdrsp_buf_pa;
	u8 *payload = (u8 *)skb->data;
1422 1423 1424

	if (!(skb->data && skb->len)) {
		dev_err(adapter->dev, "Invalid parameter in %s <%p, %#x>\n",
1425
			__func__, skb->data, skb->len);
1426 1427 1428 1429 1430
		return -1;
	}

	/* Make sure a command response buffer is available */
	if (!card->cmdrsp_buf) {
1431 1432
		dev_err(adapter->dev,
			"No response buffer available, send command failed\n");
1433 1434 1435
		return -EBUSY;
	}

1436 1437
	if (!mwifiex_pcie_ok_to_access_hw(adapter))
		mwifiex_pm_wakeup_card(adapter);
1438 1439

	adapter->cmd_sent = true;
1440 1441 1442 1443 1444 1445 1446 1447

	*(__le16 *)&payload[0] = cpu_to_le16((u16)skb->len);
	*(__le16 *)&payload[2] = cpu_to_le16(MWIFIEX_TYPE_CMD);

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

	card->cmd_buf = skb;
1448 1449 1450

	/* To send a command, the driver will:
		1. Write the 64bit physical address of the data buffer to
1451
		   cmd response address low  + cmd response address high
1452 1453 1454 1455 1456 1457 1458 1459
		2. Ring the door bell (i.e. set the door bell interrupt)

		In response to door bell interrupt, the firmware will perform
		the DMA of the command packet (first header to obtain the total
		length and then rest of the command).
	*/

	if (card->cmdrsp_buf) {
1460
		cmdrsp_buf_pa = MWIFIEX_SKB_DMA_ADDR(card->cmdrsp_buf);
1461 1462
		/* Write the lower 32bits of the cmdrsp buffer physical
		   address */
1463
		if (mwifiex_write_reg(adapter, reg->cmdrsp_addr_lo,
1464
				      (u32)cmdrsp_buf_pa)) {
1465 1466
			dev_err(adapter->dev,
				"Failed to write download cmd to boot code.\n");
1467 1468 1469 1470 1471
			ret = -1;
			goto done;
		}
		/* Write the upper 32bits of the cmdrsp buffer physical
		   address */
1472
		if (mwifiex_write_reg(adapter, reg->cmdrsp_addr_hi,
1473
				      (u32)((u64)cmdrsp_buf_pa >> 32))) {
1474 1475
			dev_err(adapter->dev,
				"Failed to write download cmd to boot code.\n");
1476 1477 1478 1479 1480
			ret = -1;
			goto done;
		}
	}

1481
	cmd_buf_pa = MWIFIEX_SKB_DMA_ADDR(card->cmd_buf);
1482 1483 1484
	/* Write the lower 32bits of the physical address to reg->cmd_addr_lo */
	if (mwifiex_write_reg(adapter, reg->cmd_addr_lo,
			      (u32)cmd_buf_pa)) {
1485 1486
		dev_err(adapter->dev,
			"Failed to write download cmd to boot code.\n");
1487 1488 1489
		ret = -1;
		goto done;
	}
1490 1491
	/* Write the upper 32bits of the physical address to reg->cmd_addr_hi */
	if (mwifiex_write_reg(adapter, reg->cmd_addr_hi,
1492
			      (u32)((u64)cmd_buf_pa >> 32))) {
1493 1494
		dev_err(adapter->dev,
			"Failed to write download cmd to boot code.\n");
1495 1496 1497 1498
		ret = -1;
		goto done;
	}

1499 1500 1501
	/* Write the command length to reg->cmd_size */
	if (mwifiex_write_reg(adapter, reg->cmd_size,
			      card->cmd_buf->len)) {
1502
		dev_err(adapter->dev,
1503
			"Failed to write cmd len to reg->cmd_size\n");
1504 1505 1506 1507 1508 1509 1510
		ret = -1;
		goto done;
	}

	/* Ring the door bell */
	if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
			      CPU_INTR_DOOR_BELL)) {
1511 1512
		dev_err(adapter->dev,
			"Failed to assert door-bell intr\n");
1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
		ret = -1;
		goto done;
	}

done:
	if (ret)
		adapter->cmd_sent = false;

	return 0;
}

/*
 * This function handles command complete interrupt
 */
static int mwifiex_pcie_process_cmd_complete(struct mwifiex_adapter *adapter)
{
	struct pcie_service_card *card = adapter->card;
1530
	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1531
	struct sk_buff *skb = card->cmdrsp_buf;
1532
	int count = 0;
1533 1534
	u16 rx_len;
	__le16 pkt_len;
1535 1536 1537

	dev_dbg(adapter->dev, "info: Rx CMD Response\n");

1538
	mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_FROMDEVICE);
1539

1540 1541 1542 1543 1544 1545 1546
	/* Unmap the command as a response has been received. */
	if (card->cmd_buf) {
		mwifiex_unmap_pci_memory(adapter, card->cmd_buf,
					 PCI_DMA_TODEVICE);
		card->cmd_buf = NULL;
	}

1547 1548 1549 1550 1551
	pkt_len = *((__le16 *)skb->data);
	rx_len = le16_to_cpu(pkt_len);
	skb_trim(skb, rx_len);
	skb_pull(skb, INTF_HEADER_LEN);

1552 1553
	if (!adapter->curr_cmd) {
		if (adapter->ps_state == PS_STATE_SLEEP_CFM) {
1554 1555
			mwifiex_process_sleep_confirm_resp(adapter, skb->data,
							   skb->len);
1556 1557 1558 1559 1560 1561 1562 1563
			mwifiex_pcie_enable_host_int(adapter);
			if (mwifiex_write_reg(adapter,
					      PCIE_CPU_INT_EVENT,
					      CPU_INTR_SLEEP_CFM_DONE)) {
				dev_warn(adapter->dev,
					 "Write register failed\n");
				return -1;
			}
1564 1565
			mwifiex_delay_for_sleep_cookie(adapter,
						       MWIFIEX_MAX_DELAY_COUNT);
1566 1567
			while (reg->sleep_cookie && (count++ < 10) &&
			       mwifiex_pcie_ok_to_access_hw(adapter))
1568
				usleep_range(50, 60);
1569
		} else {
1570 1571
			dev_err(adapter->dev,
				"There is no command but got cmdrsp\n");
1572
		}
1573 1574
		memcpy(adapter->upld_buf, skb->data,
		       min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER, skb->len));
1575
		skb_push(skb, INTF_HEADER_LEN);
1576 1577 1578
		if (mwifiex_map_pci_memory(adapter, skb, MWIFIEX_UPLD_SIZE,
					   PCI_DMA_FROMDEVICE))
			return -1;
1579
	} else if (mwifiex_pcie_ok_to_access_hw(adapter)) {
1580
		adapter->curr_cmd->resp_skb = skb;
1581 1582 1583 1584 1585 1586 1587 1588
		adapter->cmd_resp_received = true;
		/* Take the pointer and set it to CMD node and will
		   return in the response complete callback */
		card->cmdrsp_buf = NULL;

		/* Clear the cmd-rsp buffer address in scratch registers. This
		   will prevent firmware from writing to the same response
		   buffer again. */
1589
		if (mwifiex_write_reg(adapter, reg->cmdrsp_addr_lo, 0)) {
1590 1591
			dev_err(adapter->dev,
				"cmd_done: failed to clear cmd_rsp_addr_lo\n");
1592 1593 1594 1595
			return -1;
		}
		/* Write the upper 32bits of the cmdrsp buffer physical
		   address */
1596
		if (mwifiex_write_reg(adapter, reg->cmdrsp_addr_hi, 0)) {
1597 1598
			dev_err(adapter->dev,
				"cmd_done: failed to clear cmd_rsp_addr_hi\n");
1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616
			return -1;
		}
	}

	return 0;
}

/*
 * Command Response processing complete handler
 */
static int mwifiex_pcie_cmdrsp_complete(struct mwifiex_adapter *adapter,
					struct sk_buff *skb)
{
	struct pcie_service_card *card = adapter->card;

	if (skb) {
		card->cmdrsp_buf = skb;
		skb_push(card->cmdrsp_buf, INTF_HEADER_LEN);
1617 1618 1619 1620 1621
		if (mwifiex_map_pci_memory(adapter, skb, MWIFIEX_UPLD_SIZE,
					   PCI_DMA_FROMDEVICE))
			return -1;
	}

1622 1623 1624 1625 1626 1627 1628 1629 1630
	return 0;
}

/*
 * This function handles firmware event ready interrupt
 */
static int mwifiex_pcie_process_event_ready(struct mwifiex_adapter *adapter)
{
	struct pcie_service_card *card = adapter->card;
1631
	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1632 1633
	u32 rdptr = card->evtbd_rdptr & MWIFIEX_EVTBD_MASK;
	u32 wrptr, event;
1634
	struct mwifiex_evt_buf_desc *desc;
1635 1636 1637

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

	if (adapter->event_received) {
1640 1641
		dev_dbg(adapter->dev, "info: Event being processed, "
			"do not process this interrupt just yet\n");
1642 1643 1644 1645 1646 1647 1648 1649 1650
		return 0;
	}

	if (rdptr >= MWIFIEX_MAX_EVT_BD) {
		dev_dbg(adapter->dev, "info: Invalid read pointer...\n");
		return -1;
	}

	/* Read the event ring write pointer set by firmware */
1651
	if (mwifiex_read_reg(adapter, reg->evt_wrptr, &wrptr)) {
1652
		dev_err(adapter->dev,
1653
			"EventReady: failed to read reg->evt_wrptr\n");
1654 1655 1656 1657
		return -1;
	}

	dev_dbg(adapter->dev, "info: EventReady: Initial <Rd: 0x%x, Wr: 0x%x>",
1658 1659 1660
		card->evtbd_rdptr, wrptr);
	if (((wrptr & MWIFIEX_EVTBD_MASK) != (card->evtbd_rdptr
					      & MWIFIEX_EVTBD_MASK)) ||
1661 1662
	    ((wrptr & reg->evt_rollover_ind) ==
	     (card->evtbd_rdptr & reg->evt_rollover_ind))) {
1663 1664 1665 1666 1667 1668
		struct sk_buff *skb_cmd;
		__le16 data_len = 0;
		u16 evt_len;

		dev_dbg(adapter->dev, "info: Read Index: %d\n", rdptr);
		skb_cmd = card->evt_buf_list[rdptr];
1669
		mwifiex_unmap_pci_memory(adapter, skb_cmd, PCI_DMA_FROMDEVICE);
1670

1671 1672 1673
		/* Take the pointer and set it to event pointer in adapter
		   and will return back after event handling callback */
		card->evt_buf_list[rdptr] = NULL;
1674 1675
		desc = card->evtbd_ring[rdptr];
		memset(desc, 0, sizeof(*desc));
1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710

		event = *(u32 *) &skb_cmd->data[INTF_HEADER_LEN];
		adapter->event_cause = event;
		/* The first 4bytes will be the event transfer header
		   len is 2 bytes followed by type which is 2 bytes */
		memcpy(&data_len, skb_cmd->data, sizeof(__le16));
		evt_len = le16_to_cpu(data_len);

		skb_pull(skb_cmd, INTF_HEADER_LEN);
		dev_dbg(adapter->dev, "info: Event length: %d\n", evt_len);

		if ((evt_len > 0) && (evt_len  < MAX_EVENT_SIZE))
			memcpy(adapter->event_body, skb_cmd->data +
			       MWIFIEX_EVENT_HEADER_LEN, evt_len -
			       MWIFIEX_EVENT_HEADER_LEN);

		adapter->event_received = true;
		adapter->event_skb = skb_cmd;

		/* Do not update the event read pointer here, wait till the
		   buffer is released. This is just to make things simpler,
		   we need to find a better method of managing these buffers.
		*/
	}

	return 0;
}

/*
 * Event processing complete handler
 */
static int mwifiex_pcie_event_complete(struct mwifiex_adapter *adapter,
				       struct sk_buff *skb)
{
	struct pcie_service_card *card = adapter->card;
1711
	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1712 1713 1714
	int ret = 0;
	u32 rdptr = card->evtbd_rdptr & MWIFIEX_EVTBD_MASK;
	u32 wrptr;
1715
	struct mwifiex_evt_buf_desc *desc;
1716 1717 1718 1719

	if (!skb)
		return 0;

1720
	if (rdptr >= MWIFIEX_MAX_EVT_BD) {
1721
		dev_err(adapter->dev, "event_complete: Invalid rdptr 0x%x\n",
1722
			rdptr);
1723
		return -EINVAL;
1724
	}
1725 1726

	/* Read the event ring write pointer set by firmware */
1727
	if (mwifiex_read_reg(adapter, reg->evt_wrptr, &wrptr)) {
1728
		dev_err(adapter->dev,
1729
			"event_complete: failed to read reg->evt_wrptr\n");
1730
		return -1;
1731 1732 1733 1734
	}

	if (!card->evt_buf_list[rdptr]) {
		skb_push(skb, INTF_HEADER_LEN);
1735 1736 1737 1738
		if (mwifiex_map_pci_memory(adapter, skb,
					   MAX_EVENT_SIZE,
					   PCI_DMA_FROMDEVICE))
			return -1;
1739
		card->evt_buf_list[rdptr] = skb;
1740
		desc = card->evtbd_ring[rdptr];
1741
		desc->paddr = MWIFIEX_SKB_DMA_ADDR(skb);
1742 1743
		desc->len = (u16)skb->len;
		desc->flags = 0;
1744 1745
		skb = NULL;
	} else {
1746 1747 1748
		dev_dbg(adapter->dev,
			"info: ERROR: buf still valid at index %d, <%p, %p>\n",
			rdptr, card->evt_buf_list[rdptr], skb);
1749 1750 1751 1752
	}

	if ((++card->evtbd_rdptr & MWIFIEX_EVTBD_MASK) == MWIFIEX_MAX_EVT_BD) {
		card->evtbd_rdptr = ((card->evtbd_rdptr &
1753 1754
					reg->evt_rollover_ind) ^
					reg->evt_rollover_ind);
1755 1756 1757
	}

	dev_dbg(adapter->dev, "info: Updated <Rd: 0x%x, Wr: 0x%x>",
1758
		card->evtbd_rdptr, wrptr);
1759

1760 1761 1762
	/* Write the event ring read pointer in to reg->evt_rdptr */
	if (mwifiex_write_reg(adapter, reg->evt_rdptr,
			      card->evtbd_rdptr)) {
1763
		dev_err(adapter->dev,
1764
			"event_complete: failed to read reg->evt_rdptr\n");
1765
		return -1;
1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790
	}

	dev_dbg(adapter->dev, "info: Check Events Again\n");
	ret = mwifiex_pcie_process_event_ready(adapter);

	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)
{
	int ret;
	u8 *firmware = fw->fw_buf;
	u32 firmware_len = fw->fw_len;
	u32 offset = 0;
	struct sk_buff *skb;
	u32 txlen, tx_blocks = 0, tries, len;
	u32 block_retry_cnt = 0;
1791
	struct pcie_service_card *card = adapter->card;
1792
	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1793 1794

	if (!firmware || !firmware_len) {
1795 1796
		dev_err(adapter->dev,
			"No firmware image found! Terminating download\n");
1797 1798 1799 1800
		return -1;
	}

	dev_dbg(adapter->dev, "info: Downloading FW image (%d bytes)\n",
1801
		firmware_len);
1802 1803

	if (mwifiex_pcie_disable_host_int(adapter)) {
1804 1805
		dev_err(adapter->dev,
			"%s: Disabling interrupts failed.\n", __func__);
1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823
		return -1;
	}

	skb = dev_alloc_skb(MWIFIEX_UPLD_SIZE);
	if (!skb) {
		ret = -ENOMEM;
		goto done;
	}

	/* Perform firmware data transfer */
	do {
		u32 ireg_intr = 0;

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

		for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
1824
			ret = mwifiex_read_reg(adapter, reg->cmd_size,
1825 1826
					       &len);
			if (ret) {
1827 1828
				dev_warn(adapter->dev,
					 "Failed reading len from boot code\n");
1829 1830 1831 1832
				goto done;
			}
			if (len)
				break;
1833
			usleep_range(10, 20);
1834 1835 1836 1837 1838 1839
		}

		if (!len) {
			break;
		} else if (len > MWIFIEX_UPLD_SIZE) {
			pr_err("FW download failure @ %d, invalid length %d\n",
1840
			       offset, len);
1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855
			ret = -1;
			goto done;
		}

		txlen = len;

		if (len & BIT(0)) {
			block_retry_cnt++;
			if (block_retry_cnt > MAX_WRITE_IOMEM_RETRY) {
				pr_err("FW download failure @ %d, over max "
				       "retry count\n", offset);
				ret = -1;
				goto done;
			}
			dev_err(adapter->dev, "FW CRC error indicated by the "
1856 1857
				"helper: len = 0x%04X, txlen = %d\n",
				len, txlen);
1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869
			len &= ~BIT(0);
			/* Setting this to 0 to resend from same offset */
			txlen = 0;
		} else {
			block_retry_cnt = 0;
			/* Set blocksize to transfer - checking for
			   last block */
			if (firmware_len - offset < txlen)
				txlen = firmware_len - offset;

			dev_dbg(adapter->dev, ".");

1870 1871
			tx_blocks = (txlen + card->pcie.blksz_fw_dl - 1) /
				    card->pcie.blksz_fw_dl;
1872 1873 1874 1875 1876 1877

			/* Copy payload to buffer */
			memmove(skb->data, &firmware[offset], txlen);
		}

		skb_put(skb, MWIFIEX_UPLD_SIZE - skb->len);
1878
		skb_trim(skb, tx_blocks * card->pcie.blksz_fw_dl);
1879 1880 1881

		/* Send the boot command to device */
		if (mwifiex_pcie_send_boot_cmd(adapter, skb)) {
1882 1883
			dev_err(adapter->dev,
				"Failed to send firmware download command\n");
1884 1885 1886
			ret = -1;
			goto done;
		}
1887

1888 1889 1890 1891 1892
		/* Wait for the command done interrupt */
		do {
			if (mwifiex_read_reg(adapter, PCIE_CPU_INT_STATUS,
					     &ireg_intr)) {
				dev_err(adapter->dev, "%s: Failed to read "
1893 1894
					"interrupt status during fw dnld.\n",
					__func__);
1895 1896
				mwifiex_unmap_pci_memory(adapter, skb,
							 PCI_DMA_TODEVICE);
1897 1898 1899 1900 1901
				ret = -1;
				goto done;
			}
		} while ((ireg_intr & CPU_INTR_DOOR_BELL) ==
			 CPU_INTR_DOOR_BELL);
1902

1903
		mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_TODEVICE);
1904

1905 1906 1907 1908
		offset += txlen;
	} while (true);

	dev_dbg(adapter->dev, "info:\nFW download over, size %d bytes\n",
1909
		offset);
1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927

	ret = 0;

done:
	dev_kfree_skb_any(skb);
	return ret;
}

/*
 * This function checks the firmware status in card.
 *
 * The winner interface is also determined by this function.
 */
static int
mwifiex_check_fw_status(struct mwifiex_adapter *adapter, u32 poll_num)
{
	int ret = 0;
	u32 firmware_stat, winner_status;
1928 1929
	struct pcie_service_card *card = adapter->card;
	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1930 1931 1932 1933
	u32 tries;

	/* Mask spurios interrupts */
	if (mwifiex_write_reg(adapter, PCIE_HOST_INT_STATUS_MASK,
1934
			      HOST_INTR_MASK)) {
1935 1936 1937 1938 1939
		dev_warn(adapter->dev, "Write register failed\n");
		return -1;
	}

	dev_dbg(adapter->dev, "Setting driver ready signature\n");
1940 1941
	if (mwifiex_write_reg(adapter, reg->drv_rdy,
			      FIRMWARE_READY_PCIE)) {
1942 1943
		dev_err(adapter->dev,
			"Failed to write driver ready signature\n");
1944 1945 1946 1947 1948
		return -1;
	}

	/* Wait for firmware initialization event */
	for (tries = 0; tries < poll_num; tries++) {
1949
		if (mwifiex_read_reg(adapter, reg->fw_status,
1950 1951 1952 1953 1954 1955 1956 1957 1958 1959
				     &firmware_stat))
			ret = -1;
		else
			ret = 0;
		if (ret)
			continue;
		if (firmware_stat == FIRMWARE_READY_PCIE) {
			ret = 0;
			break;
		} else {
1960
			msleep(100);
1961 1962 1963 1964 1965
			ret = -1;
		}
	}

	if (ret) {
1966
		if (mwifiex_read_reg(adapter, reg->fw_status,
1967 1968 1969 1970 1971 1972
				     &winner_status))
			ret = -1;
		else if (!winner_status) {
			dev_err(adapter->dev, "PCI-E is the winner\n");
			adapter->winner = 1;
		} else {
1973 1974 1975
			dev_err(adapter->dev,
				"PCI-E is not the winner <%#x,%d>, exit dnld\n",
				ret, adapter->winner);
1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011
		}
	}

	return ret;
}

/*
 * This function reads the interrupt status from card.
 */
static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
{
	u32 pcie_ireg;
	unsigned long flags;

	if (!mwifiex_pcie_ok_to_access_hw(adapter))
		return;

	if (mwifiex_read_reg(adapter, PCIE_HOST_INT_STATUS, &pcie_ireg)) {
		dev_warn(adapter->dev, "Read register failed\n");
		return;
	}

	if ((pcie_ireg != 0xFFFFFFFF) && (pcie_ireg)) {

		mwifiex_pcie_disable_host_int(adapter);

		/* Clear the pending interrupts */
		if (mwifiex_write_reg(adapter, PCIE_HOST_INT_STATUS,
				      ~pcie_ireg)) {
			dev_warn(adapter->dev, "Write register failed\n");
			return;
		}
		spin_lock_irqsave(&adapter->int_lock, flags);
		adapter->int_status |= pcie_ireg;
		spin_unlock_irqrestore(&adapter->int_lock, flags);

2012 2013 2014
		if (!adapter->pps_uapsd_mode &&
		    adapter->ps_state == PS_STATE_SLEEP &&
		    mwifiex_pcie_ok_to_access_hw(adapter)) {
2015 2016 2017
				/* Potentially for PCIe we could get other
				 * interrupts like shared. Don't change power
				 * state until cookie is set */
2018 2019
				adapter->ps_state = PS_STATE_AWAKE;
				adapter->pm_wakeup_fw_try = false;
2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040
		}
	}
}

/*
 * Interrupt handler for PCIe root port
 *
 * This function reads the interrupt status from firmware and assigns
 * the main process in workqueue which will handle the interrupt.
 */
static irqreturn_t mwifiex_pcie_interrupt(int irq, void *context)
{
	struct pci_dev *pdev = (struct pci_dev *)context;
	struct pcie_service_card *card;
	struct mwifiex_adapter *adapter;

	if (!pdev) {
		pr_debug("info: %s: pdev is NULL\n", (u8 *)pdev);
		goto exit;
	}

2041
	card = pci_get_drvdata(pdev);
2042 2043
	if (!card || !card->adapter) {
		pr_debug("info: %s: card=%p adapter=%p\n", __func__, card,
2044
			 card ? card->adapter : NULL);
2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074
		goto exit;
	}
	adapter = card->adapter;

	if (adapter->surprise_removed)
		goto exit;

	mwifiex_interrupt_status(adapter);
	queue_work(adapter->workqueue, &adapter->main_work);

exit:
	return IRQ_HANDLED;
}

/*
 * This function checks the current interrupt status.
 *
 * The following interrupts are checked and handled by this function -
 *      - Data sent
 *      - Command sent
 *      - Command received
 *      - Packets received
 *      - Events received
 *
 * 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)
{
	int ret;
2075
	u32 pcie_ireg;
2076 2077 2078 2079
	unsigned long flags;

	spin_lock_irqsave(&adapter->int_lock, flags);
	/* Clear out unused interrupts */
2080 2081
	pcie_ireg = adapter->int_status;
	adapter->int_status = 0;
2082 2083
	spin_unlock_irqrestore(&adapter->int_lock, flags);

2084 2085 2086
	while (pcie_ireg & HOST_INTR_MASK) {
		if (pcie_ireg & HOST_INTR_DNLD_DONE) {
			pcie_ireg &= ~HOST_INTR_DNLD_DONE;
2087 2088 2089 2090
			dev_dbg(adapter->dev, "info: TX DNLD Done\n");
			ret = mwifiex_pcie_send_data_complete(adapter);
			if (ret)
				return ret;
2091
		}
2092 2093
		if (pcie_ireg & HOST_INTR_UPLD_RDY) {
			pcie_ireg &= ~HOST_INTR_UPLD_RDY;
2094 2095 2096 2097 2098
			dev_dbg(adapter->dev, "info: Rx DATA\n");
			ret = mwifiex_pcie_process_recv_data(adapter);
			if (ret)
				return ret;
		}
2099 2100
		if (pcie_ireg & HOST_INTR_EVENT_RDY) {
			pcie_ireg &= ~HOST_INTR_EVENT_RDY;
2101 2102 2103 2104 2105 2106
			dev_dbg(adapter->dev, "info: Rx EVENT\n");
			ret = mwifiex_pcie_process_event_ready(adapter);
			if (ret)
				return ret;
		}

2107 2108
		if (pcie_ireg & HOST_INTR_CMD_DONE) {
			pcie_ireg &= ~HOST_INTR_CMD_DONE;
2109
			if (adapter->cmd_sent) {
2110 2111
				dev_dbg(adapter->dev,
					"info: CMD sent Interrupt\n");
2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122
				adapter->cmd_sent = false;
			}
			/* Handle command response */
			ret = mwifiex_pcie_process_cmd_complete(adapter);
			if (ret)
				return ret;
		}

		if (mwifiex_pcie_ok_to_access_hw(adapter)) {
			if (mwifiex_read_reg(adapter, PCIE_HOST_INT_STATUS,
					     &pcie_ireg)) {
2123 2124
				dev_warn(adapter->dev,
					 "Read register failed\n");
2125 2126 2127 2128 2129
				return -1;
			}

			if ((pcie_ireg != 0xFFFFFFFF) && (pcie_ireg)) {
				if (mwifiex_write_reg(adapter,
2130 2131 2132 2133
						      PCIE_HOST_INT_STATUS,
						      ~pcie_ireg)) {
					dev_warn(adapter->dev,
						 "Write register failed\n");
2134 2135 2136 2137 2138 2139 2140
					return -1;
				}
			}

		}
	}
	dev_dbg(adapter->dev, "info: cmd_sent=%d data_sent=%d\n",
2141
		adapter->cmd_sent, adapter->data_sent);
2142 2143
	if (adapter->ps_state != PS_STATE_SLEEP)
		mwifiex_pcie_enable_host_int(adapter);
2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161

	return 0;
}

/*
 * 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 PCIE 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_pcie_host_to_card(struct mwifiex_adapter *adapter, u8 type,
				     struct sk_buff *skb,
				     struct mwifiex_tx_param *tx_param)
{
2162 2163
	if (!skb) {
		dev_err(adapter->dev, "Passed NULL skb to %s\n", __func__);
2164 2165 2166 2167
		return -1;
	}

	if (type == MWIFIEX_TYPE_DATA)
2168
		return mwifiex_pcie_send_data(adapter, skb, tx_param);
2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189
	else if (type == MWIFIEX_TYPE_CMD)
		return mwifiex_pcie_send_cmd(adapter, skb);

	return 0;
}

/*
 * This function initializes the PCI-E host memory space, WCB rings, etc.
 *
 * The following initializations steps are followed -
 *      - Allocate TXBD ring buffers
 *      - Allocate RXBD ring buffers
 *      - Allocate event BD ring buffers
 *      - Allocate command response ring buffer
 *      - Allocate sleep cookie buffer
 */
static int mwifiex_pcie_init(struct mwifiex_adapter *adapter)
{
	struct pcie_service_card *card = adapter->card;
	int ret;
	struct pci_dev *pdev = card->dev;
2190
	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220

	pci_set_drvdata(pdev, card);

	ret = pci_enable_device(pdev);
	if (ret)
		goto err_enable_dev;

	pci_set_master(pdev);

	dev_dbg(adapter->dev, "try set_consistent_dma_mask(32)\n");
	ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
	if (ret) {
		dev_err(adapter->dev, "set_dma_mask(32) failed\n");
		goto err_set_dma_mask;
	}

	ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
	if (ret) {
		dev_err(adapter->dev, "set_consistent_dma_mask(64) failed\n");
		goto err_set_dma_mask;
	}

	ret = pci_request_region(pdev, 0, DRV_NAME);
	if (ret) {
		dev_err(adapter->dev, "req_reg(0) error\n");
		goto err_req_region0;
	}
	card->pci_mmap = pci_iomap(pdev, 0, 0);
	if (!card->pci_mmap) {
		dev_err(adapter->dev, "iomap(0) error\n");
2221
		ret = -EIO;
2222 2223 2224 2225 2226 2227 2228 2229 2230 2231
		goto err_iomap0;
	}
	ret = pci_request_region(pdev, 2, DRV_NAME);
	if (ret) {
		dev_err(adapter->dev, "req_reg(2) error\n");
		goto err_req_region2;
	}
	card->pci_mmap1 = pci_iomap(pdev, 2, 0);
	if (!card->pci_mmap1) {
		dev_err(adapter->dev, "iomap(2) error\n");
2232
		ret = -EIO;
2233 2234 2235
		goto err_iomap2;
	}

2236 2237 2238
	dev_dbg(adapter->dev,
		"PCI memory map Virt0: %p PCI memory map Virt2: %p\n",
		card->pci_mmap, card->pci_mmap1);
2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252

	card->cmdrsp_buf = NULL;
	ret = mwifiex_pcie_create_txbd_ring(adapter);
	if (ret)
		goto err_cre_txbd;
	ret = mwifiex_pcie_create_rxbd_ring(adapter);
	if (ret)
		goto err_cre_rxbd;
	ret = mwifiex_pcie_create_evtbd_ring(adapter);
	if (ret)
		goto err_cre_evtbd;
	ret = mwifiex_pcie_alloc_cmdrsp_buf(adapter);
	if (ret)
		goto err_alloc_cmdbuf;
2253 2254 2255 2256 2257 2258 2259
	if (reg->sleep_cookie) {
		ret = mwifiex_pcie_alloc_sleep_cookie_buf(adapter);
		if (ret)
			goto err_alloc_cookie;
	} else {
		card->sleep_cookie_vbase = NULL;
	}
2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299
	return ret;

err_alloc_cookie:
	mwifiex_pcie_delete_cmdrsp_buf(adapter);
err_alloc_cmdbuf:
	mwifiex_pcie_delete_evtbd_ring(adapter);
err_cre_evtbd:
	mwifiex_pcie_delete_rxbd_ring(adapter);
err_cre_rxbd:
	mwifiex_pcie_delete_txbd_ring(adapter);
err_cre_txbd:
	pci_iounmap(pdev, card->pci_mmap1);
err_iomap2:
	pci_release_region(pdev, 2);
err_req_region2:
	pci_iounmap(pdev, card->pci_mmap);
err_iomap0:
	pci_release_region(pdev, 0);
err_req_region0:
err_set_dma_mask:
	pci_disable_device(pdev);
err_enable_dev:
	pci_set_drvdata(pdev, NULL);
	return ret;
}

/*
 * This function cleans up the allocated card buffers.
 *
 * The following are freed by this function -
 *      - TXBD ring buffers
 *      - RXBD ring buffers
 *      - Event BD ring buffers
 *      - Command response ring buffer
 *      - Sleep cookie buffer
 */
static void mwifiex_pcie_cleanup(struct mwifiex_adapter *adapter)
{
	struct pcie_service_card *card = adapter->card;
	struct pci_dev *pdev = card->dev;
2300
	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
2301 2302

	if (user_rmmod) {
2303
		dev_dbg(adapter->dev, "Clearing driver ready signature\n");
2304
		if (mwifiex_write_reg(adapter, reg->drv_rdy, 0x00000000))
2305 2306
			dev_err(adapter->dev,
				"Failed to write driver not-ready signature\n");
2307 2308 2309 2310 2311
	}

	if (pdev) {
		pci_iounmap(pdev, card->pci_mmap);
		pci_iounmap(pdev, card->pci_mmap1);
2312
		pci_disable_device(pdev);
2313 2314
		pci_release_region(pdev, 2);
		pci_release_region(pdev, 0);
2315 2316
		pci_set_drvdata(pdev, NULL);
	}
2317
	kfree(card);
2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342
}

/*
 * This function registers the PCIE device.
 *
 * PCIE IRQ is claimed, block size is set and driver data is initialized.
 */
static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
{
	int ret;
	struct pcie_service_card *card = adapter->card;
	struct pci_dev *pdev = card->dev;

	/* save adapter pointer in card */
	card->adapter = adapter;

	ret = request_irq(pdev->irq, mwifiex_pcie_interrupt, IRQF_SHARED,
			  "MRVL_PCIE", pdev);
	if (ret) {
		pr_err("request_irq failed: ret=%d\n", ret);
		adapter->card = NULL;
		return -1;
	}

	adapter->dev = &pdev->dev;
2343
	adapter->tx_buf_size = card->pcie.tx_buf_size;
2344
	strcpy(adapter->fw_name, card->pcie.firmware);
2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357

	return 0;
}

/*
 * This function unregisters the PCIE device.
 *
 * The PCIE IRQ is released, the function is disabled and driver
 * data is set to null.
 */
static void mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
{
	struct pcie_service_card *card = adapter->card;
2358
	const struct mwifiex_pcie_card_reg *reg;
2359 2360 2361 2362

	if (card) {
		dev_dbg(adapter->dev, "%s(): calling free_irq()\n", __func__);
		free_irq(card->dev->irq, card->dev);
2363

2364 2365 2366 2367
		reg = card->pcie.reg;
		if (reg->sleep_cookie)
			mwifiex_pcie_delete_sleep_cookie_buf(adapter);

2368 2369 2370 2371 2372
		mwifiex_pcie_delete_cmdrsp_buf(adapter);
		mwifiex_pcie_delete_evtbd_ring(adapter);
		mwifiex_pcie_delete_rxbd_ring(adapter);
		mwifiex_pcie_delete_txbd_ring(adapter);
		card->cmdrsp_buf = NULL;
2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393
	}
}

static struct mwifiex_if_ops pcie_ops = {
	.init_if =			mwifiex_pcie_init,
	.cleanup_if =			mwifiex_pcie_cleanup,
	.check_fw_status =		mwifiex_check_fw_status,
	.prog_fw =			mwifiex_prog_fw_w_helper,
	.register_dev =			mwifiex_register_dev,
	.unregister_dev =		mwifiex_unregister_dev,
	.enable_int =			mwifiex_pcie_enable_host_int,
	.process_int_status =		mwifiex_process_int_status,
	.host_to_card =			mwifiex_pcie_host_to_card,
	.wakeup =			mwifiex_pm_wakeup_card,
	.wakeup_complete =		mwifiex_pm_wakeup_card_complete,

	/* PCIE specific */
	.cmdrsp_complete =		mwifiex_pcie_cmdrsp_complete,
	.event_complete =		mwifiex_pcie_event_complete,
	.update_mp_end_port =		NULL,
	.cleanup_mpa_buf =		NULL,
2394
	.init_fw_port =			mwifiex_pcie_init_fw_port,
2395
	.clean_pcie_ring =		mwifiex_clean_pcie_ring_buf,
2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407
};

/*
 * This function initializes the PCIE driver module.
 *
 * This initiates the semaphore and registers the device with
 * PCIE bus.
 */
static int mwifiex_pcie_init_module(void)
{
	int ret;

A
Avinash Patil 已提交
2408
	pr_debug("Marvell PCIe Driver\n");
2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450

	sema_init(&add_remove_card_sem, 1);

	/* Clear the flag in case user removes the card. */
	user_rmmod = 0;

	ret = pci_register_driver(&mwifiex_pcie);
	if (ret)
		pr_err("Driver register failed!\n");
	else
		pr_debug("info: Driver registered successfully!\n");

	return ret;
}

/*
 * This function cleans up the PCIE 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 PCIE bus.
 */
static void mwifiex_pcie_cleanup_module(void)
{
	if (!down_interruptible(&add_remove_card_sem))
		up(&add_remove_card_sem);

	/* Set the flag as user is removing this module. */
	user_rmmod = 1;

	pci_unregister_driver(&mwifiex_pcie);
}

module_init(mwifiex_pcie_init_module);
module_exit(mwifiex_pcie_cleanup_module);

MODULE_AUTHOR("Marvell International Ltd.");
MODULE_DESCRIPTION("Marvell WiFi-Ex PCI-Express Driver version " PCIE_VERSION);
MODULE_VERSION(PCIE_VERSION);
MODULE_LICENSE("GPL v2");
A
Avinash Patil 已提交
2451 2452
MODULE_FIRMWARE(PCIE8766_DEFAULT_FW_NAME);
MODULE_FIRMWARE(PCIE8897_DEFAULT_FW_NAME);