1/*
2 * Marvell Wireless LAN device driver: PCIE specific handling
3 *
4 * Copyright (C) 2011-2014, Marvell International Ltd.
5 *
6 * This software file (the "File") is distributed by Marvell International
7 * Ltd. under the terms of the GNU General Public License Version 2, June 1991
8 * (the "License").  You may use, redistribute and/or modify this File in
9 * accordance with the terms and conditions of the License, a copy of which
10 * is available by writing to the Free Software Foundation, Inc.,
11 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
12 * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
13 *
14 * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
16 * ARE EXPRESSLY DISCLAIMED.  The License provides additional details about
17 * this warranty disclaimer.
18 */
19
20#include <linux/firmware.h>
21
22#include "decl.h"
23#include "ioctl.h"
24#include "util.h"
25#include "fw.h"
26#include "main.h"
27#include "wmm.h"
28#include "11n.h"
29#include "pcie.h"
30
31#define PCIE_VERSION	"1.0"
32#define DRV_NAME        "Marvell mwifiex PCIe"
33
34static u8 user_rmmod;
35
36static struct mwifiex_if_ops pcie_ops;
37
38static struct semaphore add_remove_card_sem;
39
40static struct memory_type_mapping mem_type_mapping_tbl[] = {
41	{"ITCM", NULL, 0, 0xF0},
42	{"DTCM", NULL, 0, 0xF1},
43	{"SQRAM", NULL, 0, 0xF2},
44	{"IRAM", NULL, 0, 0xF3},
45	{"APU", NULL, 0, 0xF4},
46	{"CIU", NULL, 0, 0xF5},
47	{"ICU", NULL, 0, 0xF6},
48	{"MAC", NULL, 0, 0xF7},
49};
50
51static int
52mwifiex_map_pci_memory(struct mwifiex_adapter *adapter, struct sk_buff *skb,
53		       size_t size, int flags)
54{
55	struct pcie_service_card *card = adapter->card;
56	struct mwifiex_dma_mapping mapping;
57
58	mapping.addr = pci_map_single(card->dev, skb->data, size, flags);
59	if (pci_dma_mapping_error(card->dev, mapping.addr)) {
60		dev_err(adapter->dev, "failed to map pci memory!\n");
61		return -1;
62	}
63	mapping.len = size;
64	mwifiex_store_mapping(skb, &mapping);
65	return 0;
66}
67
68static void mwifiex_unmap_pci_memory(struct mwifiex_adapter *adapter,
69				     struct sk_buff *skb, int flags)
70{
71	struct pcie_service_card *card = adapter->card;
72	struct mwifiex_dma_mapping mapping;
73
74	mwifiex_get_mapping(skb, &mapping);
75	pci_unmap_single(card->dev, mapping.addr, mapping.len, flags);
76}
77
78/*
79 * This function reads sleep cookie and checks if FW is ready
80 */
81static bool mwifiex_pcie_ok_to_access_hw(struct mwifiex_adapter *adapter)
82{
83	u32 *cookie_addr;
84	struct pcie_service_card *card = adapter->card;
85	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
86
87	if (!reg->sleep_cookie)
88		return true;
89
90	if (card->sleep_cookie_vbase) {
91		cookie_addr = (u32 *)card->sleep_cookie_vbase;
92		dev_dbg(adapter->dev, "info: ACCESS_HW: sleep cookie=0x%x\n",
93			*cookie_addr);
94		if (*cookie_addr == FW_AWAKE_COOKIE)
95			return true;
96	}
97
98	return false;
99}
100
101#ifdef CONFIG_PM_SLEEP
102/*
103 * Kernel needs to suspend all functions separately. Therefore all
104 * registered functions must have drivers with suspend and resume
105 * methods. Failing that the kernel simply removes the whole card.
106 *
107 * If already not suspended, this function allocates and sends a host
108 * sleep activate request to the firmware and turns off the traffic.
109 */
110static int mwifiex_pcie_suspend(struct device *dev)
111{
112	struct mwifiex_adapter *adapter;
113	struct pcie_service_card *card;
114	int hs_actived;
115	struct pci_dev *pdev = to_pci_dev(dev);
116
117	if (pdev) {
118		card = pci_get_drvdata(pdev);
119		if (!card || !card->adapter) {
120			pr_err("Card or adapter structure is not valid\n");
121			return 0;
122		}
123	} else {
124		pr_err("PCIE device is not specified\n");
125		return 0;
126	}
127
128	adapter = card->adapter;
129
130	hs_actived = mwifiex_enable_hs(adapter);
131
132	/* Indicate device suspended */
133	adapter->is_suspended = true;
134	adapter->hs_enabling = false;
135
136	return 0;
137}
138
139/*
140 * Kernel needs to suspend all functions separately. Therefore all
141 * registered functions must have drivers with suspend and resume
142 * methods. Failing that the kernel simply removes the whole card.
143 *
144 * If already not resumed, this function turns on the traffic and
145 * sends a host sleep cancel request to the firmware.
146 */
147static int mwifiex_pcie_resume(struct device *dev)
148{
149	struct mwifiex_adapter *adapter;
150	struct pcie_service_card *card;
151	struct pci_dev *pdev = to_pci_dev(dev);
152
153	if (pdev) {
154		card = pci_get_drvdata(pdev);
155		if (!card || !card->adapter) {
156			pr_err("Card or adapter structure is not valid\n");
157			return 0;
158		}
159	} else {
160		pr_err("PCIE device is not specified\n");
161		return 0;
162	}
163
164	adapter = card->adapter;
165
166	if (!adapter->is_suspended) {
167		dev_warn(adapter->dev, "Device already resumed\n");
168		return 0;
169	}
170
171	adapter->is_suspended = false;
172
173	mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
174			  MWIFIEX_ASYNC_CMD);
175
176	return 0;
177}
178#endif
179
180/*
181 * This function probes an mwifiex device and registers it. It allocates
182 * the card structure, enables PCIE function number and initiates the
183 * device registration and initialization procedure by adding a logical
184 * interface.
185 */
186static int mwifiex_pcie_probe(struct pci_dev *pdev,
187					const struct pci_device_id *ent)
188{
189	struct pcie_service_card *card;
190
191	pr_debug("info: vendor=0x%4.04X device=0x%4.04X rev=%d\n",
192		 pdev->vendor, pdev->device, pdev->revision);
193
194	card = kzalloc(sizeof(struct pcie_service_card), GFP_KERNEL);
195	if (!card)
196		return -ENOMEM;
197
198	card->dev = pdev;
199
200	if (ent->driver_data) {
201		struct mwifiex_pcie_device *data = (void *)ent->driver_data;
202		card->pcie.firmware = data->firmware;
203		card->pcie.reg = data->reg;
204		card->pcie.blksz_fw_dl = data->blksz_fw_dl;
205		card->pcie.tx_buf_size = data->tx_buf_size;
206		card->pcie.can_dump_fw = data->can_dump_fw;
207		card->pcie.can_ext_scan = data->can_ext_scan;
208	}
209
210	if (mwifiex_add_card(card, &add_remove_card_sem, &pcie_ops,
211			     MWIFIEX_PCIE)) {
212		pr_err("%s failed\n", __func__);
213		kfree(card);
214		return -1;
215	}
216
217	return 0;
218}
219
220/*
221 * This function removes the interface and frees up the card structure.
222 */
223static void mwifiex_pcie_remove(struct pci_dev *pdev)
224{
225	struct pcie_service_card *card;
226	struct mwifiex_adapter *adapter;
227	struct mwifiex_private *priv;
228
229	card = pci_get_drvdata(pdev);
230	if (!card)
231		return;
232
233	adapter = card->adapter;
234	if (!adapter || !adapter->priv_num)
235		return;
236
237	if (user_rmmod) {
238#ifdef CONFIG_PM_SLEEP
239		if (adapter->is_suspended)
240			mwifiex_pcie_resume(&pdev->dev);
241#endif
242
243		mwifiex_deauthenticate_all(adapter);
244
245		priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
246
247		mwifiex_disable_auto_ds(priv);
248
249		mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
250	}
251
252	mwifiex_remove_card(card->adapter, &add_remove_card_sem);
253}
254
255static void mwifiex_pcie_shutdown(struct pci_dev *pdev)
256{
257	user_rmmod = 1;
258	mwifiex_pcie_remove(pdev);
259
260	return;
261}
262
263static const struct pci_device_id mwifiex_ids[] = {
264	{
265		PCIE_VENDOR_ID_MARVELL, PCIE_DEVICE_ID_MARVELL_88W8766P,
266		PCI_ANY_ID, PCI_ANY_ID, 0, 0,
267		.driver_data = (unsigned long) &mwifiex_pcie8766,
268	},
269	{
270		PCIE_VENDOR_ID_MARVELL, PCIE_DEVICE_ID_MARVELL_88W8897,
271		PCI_ANY_ID, PCI_ANY_ID, 0, 0,
272		.driver_data = (unsigned long) &mwifiex_pcie8897,
273	},
274	{},
275};
276
277MODULE_DEVICE_TABLE(pci, mwifiex_ids);
278
279#ifdef CONFIG_PM_SLEEP
280/* Power Management Hooks */
281static SIMPLE_DEV_PM_OPS(mwifiex_pcie_pm_ops, mwifiex_pcie_suspend,
282				mwifiex_pcie_resume);
283#endif
284
285/* PCI Device Driver */
286static struct pci_driver __refdata mwifiex_pcie = {
287	.name     = "mwifiex_pcie",
288	.id_table = mwifiex_ids,
289	.probe    = mwifiex_pcie_probe,
290	.remove   = mwifiex_pcie_remove,
291#ifdef CONFIG_PM_SLEEP
292	.driver   = {
293		.pm = &mwifiex_pcie_pm_ops,
294	},
295#endif
296	.shutdown = mwifiex_pcie_shutdown,
297};
298
299/*
300 * This function writes data into PCIE card register.
301 */
302static int mwifiex_write_reg(struct mwifiex_adapter *adapter, int reg, u32 data)
303{
304	struct pcie_service_card *card = adapter->card;
305
306	iowrite32(data, card->pci_mmap1 + reg);
307
308	return 0;
309}
310
311/*
312 * This function reads data from PCIE card register.
313 */
314static int mwifiex_read_reg(struct mwifiex_adapter *adapter, int reg, u32 *data)
315{
316	struct pcie_service_card *card = adapter->card;
317
318	*data = ioread32(card->pci_mmap1 + reg);
319
320	return 0;
321}
322
323/* This function reads u8 data from PCIE card register. */
324static int mwifiex_read_reg_byte(struct mwifiex_adapter *adapter,
325				 int reg, u8 *data)
326{
327	struct pcie_service_card *card = adapter->card;
328
329	*data = ioread8(card->pci_mmap1 + reg);
330
331	return 0;
332}
333
334/*
335 * This function adds delay loop to ensure FW is awake before proceeding.
336 */
337static void mwifiex_pcie_dev_wakeup_delay(struct mwifiex_adapter *adapter)
338{
339	int i = 0;
340
341	while (mwifiex_pcie_ok_to_access_hw(adapter)) {
342		i++;
343		usleep_range(10, 20);
344		/* 50ms max wait */
345		if (i == 5000)
346			break;
347	}
348
349	return;
350}
351
352static void mwifiex_delay_for_sleep_cookie(struct mwifiex_adapter *adapter,
353					   u32 max_delay_loop_cnt)
354{
355	struct pcie_service_card *card = adapter->card;
356	u8 *buffer;
357	u32 sleep_cookie, count;
358
359	for (count = 0; count < max_delay_loop_cnt; count++) {
360		buffer = card->cmdrsp_buf->data - INTF_HEADER_LEN;
361		sleep_cookie = *(u32 *)buffer;
362
363		if (sleep_cookie == MWIFIEX_DEF_SLEEP_COOKIE) {
364			dev_dbg(adapter->dev,
365				"sleep cookie found at count %d\n", count);
366			break;
367		}
368		usleep_range(20, 30);
369	}
370
371	if (count >= max_delay_loop_cnt)
372		dev_dbg(adapter->dev,
373			"max count reached while accessing sleep cookie\n");
374}
375
376/* This function wakes up the card by reading fw_status register. */
377static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
378{
379	u32 fw_status;
380	struct pcie_service_card *card = adapter->card;
381	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
382
383	dev_dbg(adapter->dev, "event: Wakeup device...\n");
384
385	if (reg->sleep_cookie)
386		mwifiex_pcie_dev_wakeup_delay(adapter);
387
388	/* Reading fw_status register will wakeup device */
389	if (mwifiex_read_reg(adapter, reg->fw_status, &fw_status)) {
390		dev_warn(adapter->dev, "Reading fw_status register failed\n");
391		return -1;
392	}
393
394	if (reg->sleep_cookie) {
395		mwifiex_pcie_dev_wakeup_delay(adapter);
396		dev_dbg(adapter->dev, "PCIE wakeup: Setting PS_STATE_AWAKE\n");
397		adapter->ps_state = PS_STATE_AWAKE;
398	}
399
400	return 0;
401}
402
403/*
404 * This function is called after the card has woken up.
405 *
406 * The card configuration register is reset.
407 */
408static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
409{
410	dev_dbg(adapter->dev, "cmd: Wakeup device completed\n");
411
412	return 0;
413}
414
415/*
416 * This function disables the host interrupt.
417 *
418 * The host interrupt mask is read, the disable bit is reset and
419 * written back to the card host interrupt mask register.
420 */
421static int mwifiex_pcie_disable_host_int(struct mwifiex_adapter *adapter)
422{
423	if (mwifiex_pcie_ok_to_access_hw(adapter)) {
424		if (mwifiex_write_reg(adapter, PCIE_HOST_INT_MASK,
425				      0x00000000)) {
426			dev_warn(adapter->dev, "Disable host interrupt failed\n");
427			return -1;
428		}
429	}
430
431	return 0;
432}
433
434/*
435 * This function enables the host interrupt.
436 *
437 * The host interrupt enable mask is written to the card
438 * host interrupt mask register.
439 */
440static int mwifiex_pcie_enable_host_int(struct mwifiex_adapter *adapter)
441{
442	if (mwifiex_pcie_ok_to_access_hw(adapter)) {
443		/* Simply write the mask to the register */
444		if (mwifiex_write_reg(adapter, PCIE_HOST_INT_MASK,
445				      HOST_INTR_MASK)) {
446			dev_warn(adapter->dev, "Enable host interrupt failed\n");
447			return -1;
448		}
449	}
450
451	return 0;
452}
453
454/*
455 * This function initializes TX buffer ring descriptors
456 */
457static int mwifiex_init_txq_ring(struct mwifiex_adapter *adapter)
458{
459	struct pcie_service_card *card = adapter->card;
460	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
461	struct mwifiex_pcie_buf_desc *desc;
462	struct mwifiex_pfu_buf_desc *desc2;
463	int i;
464
465	for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
466		card->tx_buf_list[i] = NULL;
467		if (reg->pfu_enabled) {
468			card->txbd_ring[i] = (void *)card->txbd_ring_vbase +
469					     (sizeof(*desc2) * i);
470			desc2 = card->txbd_ring[i];
471			memset(desc2, 0, sizeof(*desc2));
472		} else {
473			card->txbd_ring[i] = (void *)card->txbd_ring_vbase +
474					     (sizeof(*desc) * i);
475			desc = card->txbd_ring[i];
476			memset(desc, 0, sizeof(*desc));
477		}
478	}
479
480	return 0;
481}
482
483/* This function initializes RX buffer ring descriptors. Each SKB is allocated
484 * here and after mapping PCI memory, its physical address is assigned to
485 * PCIE Rx buffer descriptor's physical address.
486 */
487static int mwifiex_init_rxq_ring(struct mwifiex_adapter *adapter)
488{
489	struct pcie_service_card *card = adapter->card;
490	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
491	struct sk_buff *skb;
492	struct mwifiex_pcie_buf_desc *desc;
493	struct mwifiex_pfu_buf_desc *desc2;
494	dma_addr_t buf_pa;
495	int i;
496
497	for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
498		/* Allocate skb here so that firmware can DMA data from it */
499		skb = mwifiex_alloc_dma_align_buf(MWIFIEX_RX_DATA_BUF_SIZE,
500						  GFP_KERNEL | GFP_DMA);
501		if (!skb) {
502			dev_err(adapter->dev,
503				"Unable to allocate skb for RX ring.\n");
504			kfree(card->rxbd_ring_vbase);
505			return -ENOMEM;
506		}
507
508		if (mwifiex_map_pci_memory(adapter, skb,
509					   MWIFIEX_RX_DATA_BUF_SIZE,
510					   PCI_DMA_FROMDEVICE))
511			return -1;
512
513		buf_pa = MWIFIEX_SKB_DMA_ADDR(skb);
514
515		dev_dbg(adapter->dev,
516			"info: RX ring: skb=%p len=%d data=%p buf_pa=%#x:%x\n",
517			skb, skb->len, skb->data, (u32)buf_pa,
518			(u32)((u64)buf_pa >> 32));
519
520		card->rx_buf_list[i] = skb;
521		if (reg->pfu_enabled) {
522			card->rxbd_ring[i] = (void *)card->rxbd_ring_vbase +
523					     (sizeof(*desc2) * i);
524			desc2 = card->rxbd_ring[i];
525			desc2->paddr = buf_pa;
526			desc2->len = (u16)skb->len;
527			desc2->frag_len = (u16)skb->len;
528			desc2->flags = reg->ring_flag_eop | reg->ring_flag_sop;
529			desc2->offset = 0;
530		} else {
531			card->rxbd_ring[i] = (void *)(card->rxbd_ring_vbase +
532					     (sizeof(*desc) * i));
533			desc = card->rxbd_ring[i];
534			desc->paddr = buf_pa;
535			desc->len = (u16)skb->len;
536			desc->flags = 0;
537		}
538	}
539
540	return 0;
541}
542
543/* This function initializes event buffer ring descriptors. Each SKB is
544 * allocated here and after mapping PCI memory, its physical address is assigned
545 * to PCIE Rx buffer descriptor's physical address
546 */
547static int mwifiex_pcie_init_evt_ring(struct mwifiex_adapter *adapter)
548{
549	struct pcie_service_card *card = adapter->card;
550	struct mwifiex_evt_buf_desc *desc;
551	struct sk_buff *skb;
552	dma_addr_t buf_pa;
553	int i;
554
555	for (i = 0; i < MWIFIEX_MAX_EVT_BD; i++) {
556		/* Allocate skb here so that firmware can DMA data from it */
557		skb = dev_alloc_skb(MAX_EVENT_SIZE);
558		if (!skb) {
559			dev_err(adapter->dev,
560				"Unable to allocate skb for EVENT buf.\n");
561			kfree(card->evtbd_ring_vbase);
562			return -ENOMEM;
563		}
564		skb_put(skb, MAX_EVENT_SIZE);
565
566		if (mwifiex_map_pci_memory(adapter, skb, MAX_EVENT_SIZE,
567					   PCI_DMA_FROMDEVICE))
568			return -1;
569
570		buf_pa = MWIFIEX_SKB_DMA_ADDR(skb);
571
572		dev_dbg(adapter->dev,
573			"info: EVT ring: skb=%p len=%d data=%p buf_pa=%#x:%x\n",
574			skb, skb->len, skb->data, (u32)buf_pa,
575			(u32)((u64)buf_pa >> 32));
576
577		card->evt_buf_list[i] = skb;
578		card->evtbd_ring[i] = (void *)(card->evtbd_ring_vbase +
579				      (sizeof(*desc) * i));
580		desc = card->evtbd_ring[i];
581		desc->paddr = buf_pa;
582		desc->len = (u16)skb->len;
583		desc->flags = 0;
584	}
585
586	return 0;
587}
588
589/* This function cleans up TX buffer rings. If any of the buffer list has valid
590 * SKB address, associated SKB is freed.
591 */
592static void mwifiex_cleanup_txq_ring(struct mwifiex_adapter *adapter)
593{
594	struct pcie_service_card *card = adapter->card;
595	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
596	struct sk_buff *skb;
597	struct mwifiex_pcie_buf_desc *desc;
598	struct mwifiex_pfu_buf_desc *desc2;
599	int i;
600
601	for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
602		if (reg->pfu_enabled) {
603			desc2 = card->txbd_ring[i];
604			if (card->tx_buf_list[i]) {
605				skb = card->tx_buf_list[i];
606				mwifiex_unmap_pci_memory(adapter, skb,
607							 PCI_DMA_TODEVICE);
608				dev_kfree_skb_any(skb);
609			}
610			memset(desc2, 0, sizeof(*desc2));
611		} else {
612			desc = card->txbd_ring[i];
613			if (card->tx_buf_list[i]) {
614				skb = card->tx_buf_list[i];
615				mwifiex_unmap_pci_memory(adapter, skb,
616							 PCI_DMA_TODEVICE);
617				dev_kfree_skb_any(skb);
618			}
619			memset(desc, 0, sizeof(*desc));
620		}
621		card->tx_buf_list[i] = NULL;
622	}
623
624	return;
625}
626
627/* This function cleans up RX buffer rings. If any of the buffer list has valid
628 * SKB address, associated SKB is freed.
629 */
630static void mwifiex_cleanup_rxq_ring(struct mwifiex_adapter *adapter)
631{
632	struct pcie_service_card *card = adapter->card;
633	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
634	struct mwifiex_pcie_buf_desc *desc;
635	struct mwifiex_pfu_buf_desc *desc2;
636	struct sk_buff *skb;
637	int i;
638
639	for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
640		if (reg->pfu_enabled) {
641			desc2 = card->rxbd_ring[i];
642			if (card->rx_buf_list[i]) {
643				skb = card->rx_buf_list[i];
644				mwifiex_unmap_pci_memory(adapter, skb,
645							 PCI_DMA_FROMDEVICE);
646				dev_kfree_skb_any(skb);
647			}
648			memset(desc2, 0, sizeof(*desc2));
649		} else {
650			desc = card->rxbd_ring[i];
651			if (card->rx_buf_list[i]) {
652				skb = card->rx_buf_list[i];
653				mwifiex_unmap_pci_memory(adapter, skb,
654							 PCI_DMA_FROMDEVICE);
655				dev_kfree_skb_any(skb);
656			}
657			memset(desc, 0, sizeof(*desc));
658		}
659		card->rx_buf_list[i] = NULL;
660	}
661
662	return;
663}
664
665/* This function cleans up event buffer rings. If any of the buffer list has
666 * valid SKB address, associated SKB is freed.
667 */
668static void mwifiex_cleanup_evt_ring(struct mwifiex_adapter *adapter)
669{
670	struct pcie_service_card *card = adapter->card;
671	struct mwifiex_evt_buf_desc *desc;
672	struct sk_buff *skb;
673	int i;
674
675	for (i = 0; i < MWIFIEX_MAX_EVT_BD; i++) {
676		desc = card->evtbd_ring[i];
677		if (card->evt_buf_list[i]) {
678			skb = card->evt_buf_list[i];
679			mwifiex_unmap_pci_memory(adapter, skb,
680						 PCI_DMA_FROMDEVICE);
681			dev_kfree_skb_any(skb);
682		}
683		card->evt_buf_list[i] = NULL;
684		memset(desc, 0, sizeof(*desc));
685	}
686
687	return;
688}
689
690/* This function creates buffer descriptor ring for TX
691 */
692static int mwifiex_pcie_create_txbd_ring(struct mwifiex_adapter *adapter)
693{
694	struct pcie_service_card *card = adapter->card;
695	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
696
697	/*
698	 * driver maintaines the write pointer and firmware maintaines the read
699	 * pointer. The write pointer starts at 0 (zero) while the read pointer
700	 * starts at zero with rollover bit set
701	 */
702	card->txbd_wrptr = 0;
703
704	if (reg->pfu_enabled)
705		card->txbd_rdptr = 0;
706	else
707		card->txbd_rdptr |= reg->tx_rollover_ind;
708
709	/* allocate shared memory for the BD ring and divide the same in to
710	   several descriptors */
711	if (reg->pfu_enabled)
712		card->txbd_ring_size = sizeof(struct mwifiex_pfu_buf_desc) *
713				       MWIFIEX_MAX_TXRX_BD;
714	else
715		card->txbd_ring_size = sizeof(struct mwifiex_pcie_buf_desc) *
716				       MWIFIEX_MAX_TXRX_BD;
717
718	dev_dbg(adapter->dev, "info: txbd_ring: Allocating %d bytes\n",
719		card->txbd_ring_size);
720	card->txbd_ring_vbase = pci_alloc_consistent(card->dev,
721						     card->txbd_ring_size,
722						     &card->txbd_ring_pbase);
723	if (!card->txbd_ring_vbase) {
724		dev_err(adapter->dev,
725			"allocate consistent memory (%d bytes) failed!\n",
726			card->txbd_ring_size);
727		return -ENOMEM;
728	}
729	dev_dbg(adapter->dev,
730		"info: txbd_ring - base: %p, pbase: %#x:%x, len: %x\n",
731		card->txbd_ring_vbase, (unsigned int)card->txbd_ring_pbase,
732		(u32)((u64)card->txbd_ring_pbase >> 32), card->txbd_ring_size);
733
734	return mwifiex_init_txq_ring(adapter);
735}
736
737static int mwifiex_pcie_delete_txbd_ring(struct mwifiex_adapter *adapter)
738{
739	struct pcie_service_card *card = adapter->card;
740	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
741
742	mwifiex_cleanup_txq_ring(adapter);
743
744	if (card->txbd_ring_vbase)
745		pci_free_consistent(card->dev, card->txbd_ring_size,
746				    card->txbd_ring_vbase,
747				    card->txbd_ring_pbase);
748	card->txbd_ring_size = 0;
749	card->txbd_wrptr = 0;
750	card->txbd_rdptr = 0 | reg->tx_rollover_ind;
751	card->txbd_ring_vbase = NULL;
752	card->txbd_ring_pbase = 0;
753
754	return 0;
755}
756
757/*
758 * This function creates buffer descriptor ring for RX
759 */
760static int mwifiex_pcie_create_rxbd_ring(struct mwifiex_adapter *adapter)
761{
762	struct pcie_service_card *card = adapter->card;
763	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
764
765	/*
766	 * driver maintaines the read pointer and firmware maintaines the write
767	 * pointer. The write pointer starts at 0 (zero) while the read pointer
768	 * starts at zero with rollover bit set
769	 */
770	card->rxbd_wrptr = 0;
771	card->rxbd_rdptr = reg->rx_rollover_ind;
772
773	if (reg->pfu_enabled)
774		card->rxbd_ring_size = sizeof(struct mwifiex_pfu_buf_desc) *
775				       MWIFIEX_MAX_TXRX_BD;
776	else
777		card->rxbd_ring_size = sizeof(struct mwifiex_pcie_buf_desc) *
778				       MWIFIEX_MAX_TXRX_BD;
779
780	dev_dbg(adapter->dev, "info: rxbd_ring: Allocating %d bytes\n",
781		card->rxbd_ring_size);
782	card->rxbd_ring_vbase = pci_alloc_consistent(card->dev,
783						     card->rxbd_ring_size,
784						     &card->rxbd_ring_pbase);
785	if (!card->rxbd_ring_vbase) {
786		dev_err(adapter->dev,
787			"allocate consistent memory (%d bytes) failed!\n",
788			card->rxbd_ring_size);
789		return -ENOMEM;
790	}
791
792	dev_dbg(adapter->dev,
793		"info: rxbd_ring - base: %p, pbase: %#x:%x, len: %#x\n",
794		card->rxbd_ring_vbase, (u32)card->rxbd_ring_pbase,
795		(u32)((u64)card->rxbd_ring_pbase >> 32),
796		card->rxbd_ring_size);
797
798	return mwifiex_init_rxq_ring(adapter);
799}
800
801/*
802 * This function deletes Buffer descriptor ring for RX
803 */
804static int mwifiex_pcie_delete_rxbd_ring(struct mwifiex_adapter *adapter)
805{
806	struct pcie_service_card *card = adapter->card;
807	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
808
809	mwifiex_cleanup_rxq_ring(adapter);
810
811	if (card->rxbd_ring_vbase)
812		pci_free_consistent(card->dev, card->rxbd_ring_size,
813				    card->rxbd_ring_vbase,
814				    card->rxbd_ring_pbase);
815	card->rxbd_ring_size = 0;
816	card->rxbd_wrptr = 0;
817	card->rxbd_rdptr = 0 | reg->rx_rollover_ind;
818	card->rxbd_ring_vbase = NULL;
819	card->rxbd_ring_pbase = 0;
820
821	return 0;
822}
823
824/*
825 * This function creates buffer descriptor ring for Events
826 */
827static int mwifiex_pcie_create_evtbd_ring(struct mwifiex_adapter *adapter)
828{
829	struct pcie_service_card *card = adapter->card;
830	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
831
832	/*
833	 * driver maintaines the read pointer and firmware maintaines the write
834	 * pointer. The write pointer starts at 0 (zero) while the read pointer
835	 * starts at zero with rollover bit set
836	 */
837	card->evtbd_wrptr = 0;
838	card->evtbd_rdptr = reg->evt_rollover_ind;
839
840	card->evtbd_ring_size = sizeof(struct mwifiex_evt_buf_desc) *
841				MWIFIEX_MAX_EVT_BD;
842
843	dev_dbg(adapter->dev, "info: evtbd_ring: Allocating %d bytes\n",
844		card->evtbd_ring_size);
845	card->evtbd_ring_vbase = pci_alloc_consistent(card->dev,
846						      card->evtbd_ring_size,
847						      &card->evtbd_ring_pbase);
848	if (!card->evtbd_ring_vbase) {
849		dev_err(adapter->dev,
850			"allocate consistent memory (%d bytes) failed!\n",
851			card->evtbd_ring_size);
852		return -ENOMEM;
853	}
854
855	dev_dbg(adapter->dev,
856		"info: CMDRSP/EVT bd_ring - base: %p pbase: %#x:%x len: %#x\n",
857		card->evtbd_ring_vbase, (u32)card->evtbd_ring_pbase,
858		(u32)((u64)card->evtbd_ring_pbase >> 32),
859		card->evtbd_ring_size);
860
861	return mwifiex_pcie_init_evt_ring(adapter);
862}
863
864/*
865 * This function deletes Buffer descriptor ring for Events
866 */
867static int mwifiex_pcie_delete_evtbd_ring(struct mwifiex_adapter *adapter)
868{
869	struct pcie_service_card *card = adapter->card;
870	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
871
872	mwifiex_cleanup_evt_ring(adapter);
873
874	if (card->evtbd_ring_vbase)
875		pci_free_consistent(card->dev, card->evtbd_ring_size,
876				    card->evtbd_ring_vbase,
877				    card->evtbd_ring_pbase);
878	card->evtbd_wrptr = 0;
879	card->evtbd_rdptr = 0 | reg->evt_rollover_ind;
880	card->evtbd_ring_size = 0;
881	card->evtbd_ring_vbase = NULL;
882	card->evtbd_ring_pbase = 0;
883
884	return 0;
885}
886
887/*
888 * This function allocates a buffer for CMDRSP
889 */
890static int mwifiex_pcie_alloc_cmdrsp_buf(struct mwifiex_adapter *adapter)
891{
892	struct pcie_service_card *card = adapter->card;
893	struct sk_buff *skb;
894
895	/* Allocate memory for receiving command response data */
896	skb = dev_alloc_skb(MWIFIEX_UPLD_SIZE);
897	if (!skb) {
898		dev_err(adapter->dev,
899			"Unable to allocate skb for command response data.\n");
900		return -ENOMEM;
901	}
902	skb_put(skb, MWIFIEX_UPLD_SIZE);
903	if (mwifiex_map_pci_memory(adapter, skb, MWIFIEX_UPLD_SIZE,
904				   PCI_DMA_FROMDEVICE))
905		return -1;
906
907	card->cmdrsp_buf = skb;
908
909	return 0;
910}
911
912/*
913 * This function deletes a buffer for CMDRSP
914 */
915static int mwifiex_pcie_delete_cmdrsp_buf(struct mwifiex_adapter *adapter)
916{
917	struct pcie_service_card *card;
918
919	if (!adapter)
920		return 0;
921
922	card = adapter->card;
923
924	if (card && card->cmdrsp_buf) {
925		mwifiex_unmap_pci_memory(adapter, card->cmdrsp_buf,
926					 PCI_DMA_FROMDEVICE);
927		dev_kfree_skb_any(card->cmdrsp_buf);
928	}
929
930	if (card && card->cmd_buf) {
931		mwifiex_unmap_pci_memory(adapter, card->cmd_buf,
932					 PCI_DMA_TODEVICE);
933	}
934	return 0;
935}
936
937/*
938 * This function allocates a buffer for sleep cookie
939 */
940static int mwifiex_pcie_alloc_sleep_cookie_buf(struct mwifiex_adapter *adapter)
941{
942	struct pcie_service_card *card = adapter->card;
943
944	card->sleep_cookie_vbase = pci_alloc_consistent(card->dev, sizeof(u32),
945						     &card->sleep_cookie_pbase);
946	if (!card->sleep_cookie_vbase) {
947		dev_err(adapter->dev, "pci_alloc_consistent failed!\n");
948		return -ENOMEM;
949	}
950	/* Init val of Sleep Cookie */
951	*(u32 *)card->sleep_cookie_vbase = FW_AWAKE_COOKIE;
952
953	dev_dbg(adapter->dev, "alloc_scook: sleep cookie=0x%x\n",
954		*((u32 *)card->sleep_cookie_vbase));
955
956	return 0;
957}
958
959/*
960 * This function deletes buffer for sleep cookie
961 */
962static int mwifiex_pcie_delete_sleep_cookie_buf(struct mwifiex_adapter *adapter)
963{
964	struct pcie_service_card *card;
965
966	if (!adapter)
967		return 0;
968
969	card = adapter->card;
970
971	if (card && card->sleep_cookie_vbase) {
972		pci_free_consistent(card->dev, sizeof(u32),
973				    card->sleep_cookie_vbase,
974				    card->sleep_cookie_pbase);
975		card->sleep_cookie_vbase = NULL;
976	}
977
978	return 0;
979}
980
981/* This function flushes the TX buffer descriptor ring
982 * This function defined as handler is also called while cleaning TXRX
983 * during disconnect/ bss stop.
984 */
985static int mwifiex_clean_pcie_ring_buf(struct mwifiex_adapter *adapter)
986{
987	struct pcie_service_card *card = adapter->card;
988
989	if (!mwifiex_pcie_txbd_empty(card, card->txbd_rdptr)) {
990		card->txbd_flush = 1;
991		/* write pointer already set at last send
992		 * send dnld-rdy intr again, wait for completion.
993		 */
994		if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
995				      CPU_INTR_DNLD_RDY)) {
996			dev_err(adapter->dev,
997				"failed to assert dnld-rdy interrupt.\n");
998			return -1;
999		}
1000	}
1001	return 0;
1002}
1003
1004/*
1005 * This function unmaps and frees downloaded data buffer
1006 */
1007static int mwifiex_pcie_send_data_complete(struct mwifiex_adapter *adapter)
1008{
1009	struct sk_buff *skb;
1010	u32 wrdoneidx, rdptr, num_tx_buffs, unmap_count = 0;
1011	struct mwifiex_pcie_buf_desc *desc;
1012	struct mwifiex_pfu_buf_desc *desc2;
1013	struct pcie_service_card *card = adapter->card;
1014	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1015
1016	if (!mwifiex_pcie_ok_to_access_hw(adapter))
1017		mwifiex_pm_wakeup_card(adapter);
1018
1019	/* Read the TX ring read pointer set by firmware */
1020	if (mwifiex_read_reg(adapter, reg->tx_rdptr, &rdptr)) {
1021		dev_err(adapter->dev,
1022			"SEND COMP: failed to read reg->tx_rdptr\n");
1023		return -1;
1024	}
1025
1026	dev_dbg(adapter->dev, "SEND COMP: rdptr_prev=0x%x, rdptr=0x%x\n",
1027		card->txbd_rdptr, rdptr);
1028
1029	num_tx_buffs = MWIFIEX_MAX_TXRX_BD << reg->tx_start_ptr;
1030	/* free from previous txbd_rdptr to current txbd_rdptr */
1031	while (((card->txbd_rdptr & reg->tx_mask) !=
1032		(rdptr & reg->tx_mask)) ||
1033	       ((card->txbd_rdptr & reg->tx_rollover_ind) !=
1034		(rdptr & reg->tx_rollover_ind))) {
1035		wrdoneidx = (card->txbd_rdptr & reg->tx_mask) >>
1036			    reg->tx_start_ptr;
1037
1038		skb = card->tx_buf_list[wrdoneidx];
1039
1040		if (skb) {
1041			dev_dbg(adapter->dev,
1042				"SEND COMP: Detach skb %p at txbd_rdidx=%d\n",
1043				skb, wrdoneidx);
1044			mwifiex_unmap_pci_memory(adapter, skb,
1045						 PCI_DMA_TODEVICE);
1046
1047			unmap_count++;
1048
1049			if (card->txbd_flush)
1050				mwifiex_write_data_complete(adapter, skb, 0,
1051							    -1);
1052			else
1053				mwifiex_write_data_complete(adapter, skb, 0, 0);
1054		}
1055
1056		card->tx_buf_list[wrdoneidx] = NULL;
1057
1058		if (reg->pfu_enabled) {
1059			desc2 = card->txbd_ring[wrdoneidx];
1060			memset(desc2, 0, sizeof(*desc2));
1061		} else {
1062			desc = card->txbd_ring[wrdoneidx];
1063			memset(desc, 0, sizeof(*desc));
1064		}
1065		switch (card->dev->device) {
1066		case PCIE_DEVICE_ID_MARVELL_88W8766P:
1067			card->txbd_rdptr++;
1068			break;
1069		case PCIE_DEVICE_ID_MARVELL_88W8897:
1070			card->txbd_rdptr += reg->ring_tx_start_ptr;
1071			break;
1072		}
1073
1074
1075		if ((card->txbd_rdptr & reg->tx_mask) == num_tx_buffs)
1076			card->txbd_rdptr = ((card->txbd_rdptr &
1077					     reg->tx_rollover_ind) ^
1078					     reg->tx_rollover_ind);
1079	}
1080
1081	if (unmap_count)
1082		adapter->data_sent = false;
1083
1084	if (card->txbd_flush) {
1085		if (mwifiex_pcie_txbd_empty(card, card->txbd_rdptr))
1086			card->txbd_flush = 0;
1087		else
1088			mwifiex_clean_pcie_ring_buf(adapter);
1089	}
1090
1091	return 0;
1092}
1093
1094/* This function sends data buffer to device. First 4 bytes of payload
1095 * are filled with payload length and payload type. Then this payload
1096 * is mapped to PCI device memory. Tx ring pointers are advanced accordingly.
1097 * Download ready interrupt to FW is deffered if Tx ring is not full and
1098 * additional payload can be accomodated.
1099 * Caller must ensure tx_param parameter to this function is not NULL.
1100 */
1101static int
1102mwifiex_pcie_send_data(struct mwifiex_adapter *adapter, struct sk_buff *skb,
1103		       struct mwifiex_tx_param *tx_param)
1104{
1105	struct pcie_service_card *card = adapter->card;
1106	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1107	u32 wrindx, num_tx_buffs, rx_val;
1108	int ret;
1109	dma_addr_t buf_pa;
1110	struct mwifiex_pcie_buf_desc *desc = NULL;
1111	struct mwifiex_pfu_buf_desc *desc2 = NULL;
1112	__le16 *tmp;
1113
1114	if (!(skb->data && skb->len)) {
1115		dev_err(adapter->dev, "%s(): invalid parameter <%p, %#x>\n",
1116			__func__, skb->data, skb->len);
1117		return -1;
1118	}
1119
1120	if (!mwifiex_pcie_ok_to_access_hw(adapter))
1121		mwifiex_pm_wakeup_card(adapter);
1122
1123	num_tx_buffs = MWIFIEX_MAX_TXRX_BD << reg->tx_start_ptr;
1124	dev_dbg(adapter->dev, "info: SEND DATA: <Rd: %#x, Wr: %#x>\n",
1125		card->txbd_rdptr, card->txbd_wrptr);
1126	if (mwifiex_pcie_txbd_not_full(card)) {
1127		u8 *payload;
1128
1129		adapter->data_sent = true;
1130		payload = skb->data;
1131		tmp = (__le16 *)&payload[0];
1132		*tmp = cpu_to_le16((u16)skb->len);
1133		tmp = (__le16 *)&payload[2];
1134		*tmp = cpu_to_le16(MWIFIEX_TYPE_DATA);
1135
1136		if (mwifiex_map_pci_memory(adapter, skb, skb->len,
1137					   PCI_DMA_TODEVICE))
1138			return -1;
1139
1140		wrindx = (card->txbd_wrptr & reg->tx_mask) >> reg->tx_start_ptr;
1141		buf_pa = MWIFIEX_SKB_DMA_ADDR(skb);
1142		card->tx_buf_list[wrindx] = skb;
1143
1144		if (reg->pfu_enabled) {
1145			desc2 = card->txbd_ring[wrindx];
1146			desc2->paddr = buf_pa;
1147			desc2->len = (u16)skb->len;
1148			desc2->frag_len = (u16)skb->len;
1149			desc2->offset = 0;
1150			desc2->flags = MWIFIEX_BD_FLAG_FIRST_DESC |
1151					 MWIFIEX_BD_FLAG_LAST_DESC;
1152		} else {
1153			desc = card->txbd_ring[wrindx];
1154			desc->paddr = buf_pa;
1155			desc->len = (u16)skb->len;
1156			desc->flags = MWIFIEX_BD_FLAG_FIRST_DESC |
1157				      MWIFIEX_BD_FLAG_LAST_DESC;
1158		}
1159
1160		switch (card->dev->device) {
1161		case PCIE_DEVICE_ID_MARVELL_88W8766P:
1162			card->txbd_wrptr++;
1163			break;
1164		case PCIE_DEVICE_ID_MARVELL_88W8897:
1165			card->txbd_wrptr += reg->ring_tx_start_ptr;
1166			break;
1167		}
1168
1169		if ((card->txbd_wrptr & reg->tx_mask) == num_tx_buffs)
1170			card->txbd_wrptr = ((card->txbd_wrptr &
1171						reg->tx_rollover_ind) ^
1172						reg->tx_rollover_ind);
1173
1174		rx_val = card->rxbd_rdptr & reg->rx_wrap_mask;
1175		/* Write the TX ring write pointer in to reg->tx_wrptr */
1176		if (mwifiex_write_reg(adapter, reg->tx_wrptr,
1177				      card->txbd_wrptr | rx_val)) {
1178			dev_err(adapter->dev,
1179				"SEND DATA: failed to write reg->tx_wrptr\n");
1180			ret = -1;
1181			goto done_unmap;
1182		}
1183		if ((mwifiex_pcie_txbd_not_full(card)) &&
1184		    tx_param->next_pkt_len) {
1185			/* have more packets and TxBD still can hold more */
1186			dev_dbg(adapter->dev,
1187				"SEND DATA: delay dnld-rdy interrupt.\n");
1188			adapter->data_sent = false;
1189		} else {
1190			/* Send the TX ready interrupt */
1191			if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
1192					      CPU_INTR_DNLD_RDY)) {
1193				dev_err(adapter->dev,
1194					"SEND DATA: failed to assert dnld-rdy interrupt.\n");
1195				ret = -1;
1196				goto done_unmap;
1197			}
1198		}
1199		dev_dbg(adapter->dev, "info: SEND DATA: Updated <Rd: %#x, Wr: "
1200			"%#x> and sent packet to firmware successfully\n",
1201			card->txbd_rdptr, card->txbd_wrptr);
1202	} else {
1203		dev_dbg(adapter->dev,
1204			"info: TX Ring full, can't send packets to fw\n");
1205		adapter->data_sent = true;
1206		/* Send the TX ready interrupt */
1207		if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
1208				      CPU_INTR_DNLD_RDY))
1209			dev_err(adapter->dev,
1210				"SEND DATA: failed to assert door-bell intr\n");
1211		return -EBUSY;
1212	}
1213
1214	return -EINPROGRESS;
1215done_unmap:
1216	mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_TODEVICE);
1217	card->tx_buf_list[wrindx] = NULL;
1218	if (reg->pfu_enabled)
1219		memset(desc2, 0, sizeof(*desc2));
1220	else
1221		memset(desc, 0, sizeof(*desc));
1222
1223	return ret;
1224}
1225
1226/*
1227 * This function handles received buffer ring and
1228 * dispatches packets to upper
1229 */
1230static int mwifiex_pcie_process_recv_data(struct mwifiex_adapter *adapter)
1231{
1232	struct pcie_service_card *card = adapter->card;
1233	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1234	u32 wrptr, rd_index, tx_val;
1235	dma_addr_t buf_pa;
1236	int ret = 0;
1237	struct sk_buff *skb_tmp = NULL;
1238	struct mwifiex_pcie_buf_desc *desc;
1239	struct mwifiex_pfu_buf_desc *desc2;
1240
1241	if (!mwifiex_pcie_ok_to_access_hw(adapter))
1242		mwifiex_pm_wakeup_card(adapter);
1243
1244	/* Read the RX ring Write pointer set by firmware */
1245	if (mwifiex_read_reg(adapter, reg->rx_wrptr, &wrptr)) {
1246		dev_err(adapter->dev,
1247			"RECV DATA: failed to read reg->rx_wrptr\n");
1248		ret = -1;
1249		goto done;
1250	}
1251	card->rxbd_wrptr = wrptr;
1252
1253	while (((wrptr & reg->rx_mask) !=
1254		(card->rxbd_rdptr & reg->rx_mask)) ||
1255	       ((wrptr & reg->rx_rollover_ind) ==
1256		(card->rxbd_rdptr & reg->rx_rollover_ind))) {
1257		struct sk_buff *skb_data;
1258		u16 rx_len;
1259		__le16 pkt_len;
1260
1261		rd_index = card->rxbd_rdptr & reg->rx_mask;
1262		skb_data = card->rx_buf_list[rd_index];
1263
1264		/* If skb allocation was failed earlier for Rx packet,
1265		 * rx_buf_list[rd_index] would have been left with a NULL.
1266		 */
1267		if (!skb_data)
1268			return -ENOMEM;
1269
1270		mwifiex_unmap_pci_memory(adapter, skb_data, PCI_DMA_FROMDEVICE);
1271		card->rx_buf_list[rd_index] = NULL;
1272
1273		/* Get data length from interface header -
1274		 * first 2 bytes for len, next 2 bytes is for type
1275		 */
1276		pkt_len = *((__le16 *)skb_data->data);
1277		rx_len = le16_to_cpu(pkt_len);
1278		if (WARN_ON(rx_len <= INTF_HEADER_LEN ||
1279			    rx_len > MWIFIEX_RX_DATA_BUF_SIZE)) {
1280			dev_err(adapter->dev,
1281				"Invalid RX len %d, Rd=%#x, Wr=%#x\n",
1282				rx_len, card->rxbd_rdptr, wrptr);
1283			dev_kfree_skb_any(skb_data);
1284		} else {
1285			skb_put(skb_data, rx_len);
1286			dev_dbg(adapter->dev,
1287				"info: RECV DATA: Rd=%#x, Wr=%#x, Len=%d\n",
1288				card->rxbd_rdptr, wrptr, rx_len);
1289			skb_pull(skb_data, INTF_HEADER_LEN);
1290			if (adapter->rx_work_enabled) {
1291				skb_queue_tail(&adapter->rx_data_q, skb_data);
1292				adapter->data_received = true;
1293				atomic_inc(&adapter->rx_pending);
1294			} else {
1295				mwifiex_handle_rx_packet(adapter, skb_data);
1296			}
1297		}
1298
1299		skb_tmp = mwifiex_alloc_dma_align_buf(MWIFIEX_RX_DATA_BUF_SIZE,
1300						      GFP_KERNEL | GFP_DMA);
1301		if (!skb_tmp) {
1302			dev_err(adapter->dev,
1303				"Unable to allocate skb.\n");
1304			return -ENOMEM;
1305		}
1306
1307		if (mwifiex_map_pci_memory(adapter, skb_tmp,
1308					   MWIFIEX_RX_DATA_BUF_SIZE,
1309					   PCI_DMA_FROMDEVICE))
1310			return -1;
1311
1312		buf_pa = MWIFIEX_SKB_DMA_ADDR(skb_tmp);
1313
1314		dev_dbg(adapter->dev,
1315			"RECV DATA: Attach new sk_buff %p at rxbd_rdidx=%d\n",
1316			skb_tmp, rd_index);
1317		card->rx_buf_list[rd_index] = skb_tmp;
1318
1319		if (reg->pfu_enabled) {
1320			desc2 = card->rxbd_ring[rd_index];
1321			desc2->paddr = buf_pa;
1322			desc2->len = skb_tmp->len;
1323			desc2->frag_len = skb_tmp->len;
1324			desc2->offset = 0;
1325			desc2->flags = reg->ring_flag_sop | reg->ring_flag_eop;
1326		} else {
1327			desc = card->rxbd_ring[rd_index];
1328			desc->paddr = buf_pa;
1329			desc->len = skb_tmp->len;
1330			desc->flags = 0;
1331		}
1332
1333		if ((++card->rxbd_rdptr & reg->rx_mask) ==
1334							MWIFIEX_MAX_TXRX_BD) {
1335			card->rxbd_rdptr = ((card->rxbd_rdptr &
1336					     reg->rx_rollover_ind) ^
1337					     reg->rx_rollover_ind);
1338		}
1339		dev_dbg(adapter->dev, "info: RECV DATA: <Rd: %#x, Wr: %#x>\n",
1340			card->rxbd_rdptr, wrptr);
1341
1342		tx_val = card->txbd_wrptr & reg->tx_wrap_mask;
1343		/* Write the RX ring read pointer in to reg->rx_rdptr */
1344		if (mwifiex_write_reg(adapter, reg->rx_rdptr,
1345				      card->rxbd_rdptr | tx_val)) {
1346			dev_err(adapter->dev,
1347				"RECV DATA: failed to write reg->rx_rdptr\n");
1348			ret = -1;
1349			goto done;
1350		}
1351
1352		/* Read the RX ring Write pointer set by firmware */
1353		if (mwifiex_read_reg(adapter, reg->rx_wrptr, &wrptr)) {
1354			dev_err(adapter->dev,
1355				"RECV DATA: failed to read reg->rx_wrptr\n");
1356			ret = -1;
1357			goto done;
1358		}
1359		dev_dbg(adapter->dev,
1360			"info: RECV DATA: Rcvd packet from fw successfully\n");
1361		card->rxbd_wrptr = wrptr;
1362	}
1363
1364done:
1365	return ret;
1366}
1367
1368/*
1369 * This function downloads the boot command to device
1370 */
1371static int
1372mwifiex_pcie_send_boot_cmd(struct mwifiex_adapter *adapter, struct sk_buff *skb)
1373{
1374	dma_addr_t buf_pa;
1375	struct pcie_service_card *card = adapter->card;
1376	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1377
1378	if (!(skb->data && skb->len)) {
1379		dev_err(adapter->dev,
1380			"Invalid parameter in %s <%p. len %d>\n",
1381			__func__, skb->data, skb->len);
1382		return -1;
1383	}
1384
1385	if (mwifiex_map_pci_memory(adapter, skb, skb->len , PCI_DMA_TODEVICE))
1386		return -1;
1387
1388	buf_pa = MWIFIEX_SKB_DMA_ADDR(skb);
1389
1390	/* Write the lower 32bits of the physical address to low command
1391	 * address scratch register
1392	 */
1393	if (mwifiex_write_reg(adapter, reg->cmd_addr_lo, (u32)buf_pa)) {
1394		dev_err(adapter->dev,
1395			"%s: failed to write download command to boot code.\n",
1396			__func__);
1397		mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_TODEVICE);
1398		return -1;
1399	}
1400
1401	/* Write the upper 32bits of the physical address to high command
1402	 * address scratch register
1403	 */
1404	if (mwifiex_write_reg(adapter, reg->cmd_addr_hi,
1405			      (u32)((u64)buf_pa >> 32))) {
1406		dev_err(adapter->dev,
1407			"%s: failed to write download command to boot code.\n",
1408			__func__);
1409		mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_TODEVICE);
1410		return -1;
1411	}
1412
1413	/* Write the command length to cmd_size scratch register */
1414	if (mwifiex_write_reg(adapter, reg->cmd_size, skb->len)) {
1415		dev_err(adapter->dev,
1416			"%s: failed to write command len to cmd_size scratch reg\n",
1417			__func__);
1418		mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_TODEVICE);
1419		return -1;
1420	}
1421
1422	/* Ring the door bell */
1423	if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
1424			      CPU_INTR_DOOR_BELL)) {
1425		dev_err(adapter->dev,
1426			"%s: failed to assert door-bell intr\n", __func__);
1427		mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_TODEVICE);
1428		return -1;
1429	}
1430
1431	return 0;
1432}
1433
1434/* This function init rx port in firmware which in turn enables to receive data
1435 * from device before transmitting any packet.
1436 */
1437static int mwifiex_pcie_init_fw_port(struct mwifiex_adapter *adapter)
1438{
1439	struct pcie_service_card *card = adapter->card;
1440	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1441	int tx_wrap = card->txbd_wrptr & reg->tx_wrap_mask;
1442
1443	/* Write the RX ring read pointer in to reg->rx_rdptr */
1444	if (mwifiex_write_reg(adapter, reg->rx_rdptr, card->rxbd_rdptr |
1445			      tx_wrap)) {
1446		dev_err(adapter->dev,
1447			"RECV DATA: failed to write reg->rx_rdptr\n");
1448		return -1;
1449	}
1450	return 0;
1451}
1452
1453/* This function downloads commands to the device
1454 */
1455static int
1456mwifiex_pcie_send_cmd(struct mwifiex_adapter *adapter, struct sk_buff *skb)
1457{
1458	struct pcie_service_card *card = adapter->card;
1459	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1460	int ret = 0;
1461	dma_addr_t cmd_buf_pa, cmdrsp_buf_pa;
1462	u8 *payload = (u8 *)skb->data;
1463
1464	if (!(skb->data && skb->len)) {
1465		dev_err(adapter->dev, "Invalid parameter in %s <%p, %#x>\n",
1466			__func__, skb->data, skb->len);
1467		return -1;
1468	}
1469
1470	/* Make sure a command response buffer is available */
1471	if (!card->cmdrsp_buf) {
1472		dev_err(adapter->dev,
1473			"No response buffer available, send command failed\n");
1474		return -EBUSY;
1475	}
1476
1477	if (!mwifiex_pcie_ok_to_access_hw(adapter))
1478		mwifiex_pm_wakeup_card(adapter);
1479
1480	adapter->cmd_sent = true;
1481
1482	*(__le16 *)&payload[0] = cpu_to_le16((u16)skb->len);
1483	*(__le16 *)&payload[2] = cpu_to_le16(MWIFIEX_TYPE_CMD);
1484
1485	if (mwifiex_map_pci_memory(adapter, skb, skb->len, PCI_DMA_TODEVICE))
1486		return -1;
1487
1488	card->cmd_buf = skb;
1489
1490	/* To send a command, the driver will:
1491		1. Write the 64bit physical address of the data buffer to
1492		   cmd response address low  + cmd response address high
1493		2. Ring the door bell (i.e. set the door bell interrupt)
1494
1495		In response to door bell interrupt, the firmware will perform
1496		the DMA of the command packet (first header to obtain the total
1497		length and then rest of the command).
1498	*/
1499
1500	if (card->cmdrsp_buf) {
1501		cmdrsp_buf_pa = MWIFIEX_SKB_DMA_ADDR(card->cmdrsp_buf);
1502		/* Write the lower 32bits of the cmdrsp buffer physical
1503		   address */
1504		if (mwifiex_write_reg(adapter, reg->cmdrsp_addr_lo,
1505				      (u32)cmdrsp_buf_pa)) {
1506			dev_err(adapter->dev,
1507				"Failed to write download cmd to boot code.\n");
1508			ret = -1;
1509			goto done;
1510		}
1511		/* Write the upper 32bits of the cmdrsp buffer physical
1512		   address */
1513		if (mwifiex_write_reg(adapter, reg->cmdrsp_addr_hi,
1514				      (u32)((u64)cmdrsp_buf_pa >> 32))) {
1515			dev_err(adapter->dev,
1516				"Failed to write download cmd to boot code.\n");
1517			ret = -1;
1518			goto done;
1519		}
1520	}
1521
1522	cmd_buf_pa = MWIFIEX_SKB_DMA_ADDR(card->cmd_buf);
1523	/* Write the lower 32bits of the physical address to reg->cmd_addr_lo */
1524	if (mwifiex_write_reg(adapter, reg->cmd_addr_lo,
1525			      (u32)cmd_buf_pa)) {
1526		dev_err(adapter->dev,
1527			"Failed to write download cmd to boot code.\n");
1528		ret = -1;
1529		goto done;
1530	}
1531	/* Write the upper 32bits of the physical address to reg->cmd_addr_hi */
1532	if (mwifiex_write_reg(adapter, reg->cmd_addr_hi,
1533			      (u32)((u64)cmd_buf_pa >> 32))) {
1534		dev_err(adapter->dev,
1535			"Failed to write download cmd to boot code.\n");
1536		ret = -1;
1537		goto done;
1538	}
1539
1540	/* Write the command length to reg->cmd_size */
1541	if (mwifiex_write_reg(adapter, reg->cmd_size,
1542			      card->cmd_buf->len)) {
1543		dev_err(adapter->dev,
1544			"Failed to write cmd len to reg->cmd_size\n");
1545		ret = -1;
1546		goto done;
1547	}
1548
1549	/* Ring the door bell */
1550	if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
1551			      CPU_INTR_DOOR_BELL)) {
1552		dev_err(adapter->dev,
1553			"Failed to assert door-bell intr\n");
1554		ret = -1;
1555		goto done;
1556	}
1557
1558done:
1559	if (ret)
1560		adapter->cmd_sent = false;
1561
1562	return 0;
1563}
1564
1565/*
1566 * This function handles command complete interrupt
1567 */
1568static int mwifiex_pcie_process_cmd_complete(struct mwifiex_adapter *adapter)
1569{
1570	struct pcie_service_card *card = adapter->card;
1571	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1572	struct sk_buff *skb = card->cmdrsp_buf;
1573	int count = 0;
1574	u16 rx_len;
1575	__le16 pkt_len;
1576
1577	dev_dbg(adapter->dev, "info: Rx CMD Response\n");
1578
1579	mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_FROMDEVICE);
1580
1581	/* Unmap the command as a response has been received. */
1582	if (card->cmd_buf) {
1583		mwifiex_unmap_pci_memory(adapter, card->cmd_buf,
1584					 PCI_DMA_TODEVICE);
1585		card->cmd_buf = NULL;
1586	}
1587
1588	pkt_len = *((__le16 *)skb->data);
1589	rx_len = le16_to_cpu(pkt_len);
1590	skb_trim(skb, rx_len);
1591	skb_pull(skb, INTF_HEADER_LEN);
1592
1593	if (!adapter->curr_cmd) {
1594		if (adapter->ps_state == PS_STATE_SLEEP_CFM) {
1595			mwifiex_process_sleep_confirm_resp(adapter, skb->data,
1596							   skb->len);
1597			mwifiex_pcie_enable_host_int(adapter);
1598			if (mwifiex_write_reg(adapter,
1599					      PCIE_CPU_INT_EVENT,
1600					      CPU_INTR_SLEEP_CFM_DONE)) {
1601				dev_warn(adapter->dev,
1602					 "Write register failed\n");
1603				return -1;
1604			}
1605			mwifiex_delay_for_sleep_cookie(adapter,
1606						       MWIFIEX_MAX_DELAY_COUNT);
1607			while (reg->sleep_cookie && (count++ < 10) &&
1608			       mwifiex_pcie_ok_to_access_hw(adapter))
1609				usleep_range(50, 60);
1610		} else {
1611			dev_err(adapter->dev,
1612				"There is no command but got cmdrsp\n");
1613		}
1614		memcpy(adapter->upld_buf, skb->data,
1615		       min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER, skb->len));
1616		skb_push(skb, INTF_HEADER_LEN);
1617		if (mwifiex_map_pci_memory(adapter, skb, MWIFIEX_UPLD_SIZE,
1618					   PCI_DMA_FROMDEVICE))
1619			return -1;
1620	} else if (mwifiex_pcie_ok_to_access_hw(adapter)) {
1621		adapter->curr_cmd->resp_skb = skb;
1622		adapter->cmd_resp_received = true;
1623		/* Take the pointer and set it to CMD node and will
1624		   return in the response complete callback */
1625		card->cmdrsp_buf = NULL;
1626
1627		/* Clear the cmd-rsp buffer address in scratch registers. This
1628		   will prevent firmware from writing to the same response
1629		   buffer again. */
1630		if (mwifiex_write_reg(adapter, reg->cmdrsp_addr_lo, 0)) {
1631			dev_err(adapter->dev,
1632				"cmd_done: failed to clear cmd_rsp_addr_lo\n");
1633			return -1;
1634		}
1635		/* Write the upper 32bits of the cmdrsp buffer physical
1636		   address */
1637		if (mwifiex_write_reg(adapter, reg->cmdrsp_addr_hi, 0)) {
1638			dev_err(adapter->dev,
1639				"cmd_done: failed to clear cmd_rsp_addr_hi\n");
1640			return -1;
1641		}
1642	}
1643
1644	return 0;
1645}
1646
1647/*
1648 * Command Response processing complete handler
1649 */
1650static int mwifiex_pcie_cmdrsp_complete(struct mwifiex_adapter *adapter,
1651					struct sk_buff *skb)
1652{
1653	struct pcie_service_card *card = adapter->card;
1654
1655	if (skb) {
1656		card->cmdrsp_buf = skb;
1657		skb_push(card->cmdrsp_buf, INTF_HEADER_LEN);
1658		if (mwifiex_map_pci_memory(adapter, skb, MWIFIEX_UPLD_SIZE,
1659					   PCI_DMA_FROMDEVICE))
1660			return -1;
1661	}
1662
1663	return 0;
1664}
1665
1666/*
1667 * This function handles firmware event ready interrupt
1668 */
1669static int mwifiex_pcie_process_event_ready(struct mwifiex_adapter *adapter)
1670{
1671	struct pcie_service_card *card = adapter->card;
1672	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1673	u32 rdptr = card->evtbd_rdptr & MWIFIEX_EVTBD_MASK;
1674	u32 wrptr, event;
1675	struct mwifiex_evt_buf_desc *desc;
1676
1677	if (!mwifiex_pcie_ok_to_access_hw(adapter))
1678		mwifiex_pm_wakeup_card(adapter);
1679
1680	if (adapter->event_received) {
1681		dev_dbg(adapter->dev, "info: Event being processed, "
1682			"do not process this interrupt just yet\n");
1683		return 0;
1684	}
1685
1686	if (rdptr >= MWIFIEX_MAX_EVT_BD) {
1687		dev_dbg(adapter->dev, "info: Invalid read pointer...\n");
1688		return -1;
1689	}
1690
1691	/* Read the event ring write pointer set by firmware */
1692	if (mwifiex_read_reg(adapter, reg->evt_wrptr, &wrptr)) {
1693		dev_err(adapter->dev,
1694			"EventReady: failed to read reg->evt_wrptr\n");
1695		return -1;
1696	}
1697
1698	dev_dbg(adapter->dev, "info: EventReady: Initial <Rd: 0x%x, Wr: 0x%x>",
1699		card->evtbd_rdptr, wrptr);
1700	if (((wrptr & MWIFIEX_EVTBD_MASK) != (card->evtbd_rdptr
1701					      & MWIFIEX_EVTBD_MASK)) ||
1702	    ((wrptr & reg->evt_rollover_ind) ==
1703	     (card->evtbd_rdptr & reg->evt_rollover_ind))) {
1704		struct sk_buff *skb_cmd;
1705		__le16 data_len = 0;
1706		u16 evt_len;
1707
1708		dev_dbg(adapter->dev, "info: Read Index: %d\n", rdptr);
1709		skb_cmd = card->evt_buf_list[rdptr];
1710		mwifiex_unmap_pci_memory(adapter, skb_cmd, PCI_DMA_FROMDEVICE);
1711
1712		/* Take the pointer and set it to event pointer in adapter
1713		   and will return back after event handling callback */
1714		card->evt_buf_list[rdptr] = NULL;
1715		desc = card->evtbd_ring[rdptr];
1716		memset(desc, 0, sizeof(*desc));
1717
1718		event = *(u32 *) &skb_cmd->data[INTF_HEADER_LEN];
1719		adapter->event_cause = event;
1720		/* The first 4bytes will be the event transfer header
1721		   len is 2 bytes followed by type which is 2 bytes */
1722		memcpy(&data_len, skb_cmd->data, sizeof(__le16));
1723		evt_len = le16_to_cpu(data_len);
1724
1725		skb_pull(skb_cmd, INTF_HEADER_LEN);
1726		dev_dbg(adapter->dev, "info: Event length: %d\n", evt_len);
1727
1728		if ((evt_len > 0) && (evt_len  < MAX_EVENT_SIZE))
1729			memcpy(adapter->event_body, skb_cmd->data +
1730			       MWIFIEX_EVENT_HEADER_LEN, evt_len -
1731			       MWIFIEX_EVENT_HEADER_LEN);
1732
1733		adapter->event_received = true;
1734		adapter->event_skb = skb_cmd;
1735
1736		/* Do not update the event read pointer here, wait till the
1737		   buffer is released. This is just to make things simpler,
1738		   we need to find a better method of managing these buffers.
1739		*/
1740	} else {
1741		if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
1742				      CPU_INTR_EVENT_DONE)) {
1743			dev_warn(adapter->dev,
1744				 "Write register failed\n");
1745			return -1;
1746		}
1747	}
1748
1749	return 0;
1750}
1751
1752/*
1753 * Event processing complete handler
1754 */
1755static int mwifiex_pcie_event_complete(struct mwifiex_adapter *adapter,
1756				       struct sk_buff *skb)
1757{
1758	struct pcie_service_card *card = adapter->card;
1759	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1760	int ret = 0;
1761	u32 rdptr = card->evtbd_rdptr & MWIFIEX_EVTBD_MASK;
1762	u32 wrptr;
1763	struct mwifiex_evt_buf_desc *desc;
1764
1765	if (!skb)
1766		return 0;
1767
1768	if (rdptr >= MWIFIEX_MAX_EVT_BD) {
1769		dev_err(adapter->dev, "event_complete: Invalid rdptr 0x%x\n",
1770			rdptr);
1771		return -EINVAL;
1772	}
1773
1774	/* Read the event ring write pointer set by firmware */
1775	if (mwifiex_read_reg(adapter, reg->evt_wrptr, &wrptr)) {
1776		dev_err(adapter->dev,
1777			"event_complete: failed to read reg->evt_wrptr\n");
1778		return -1;
1779	}
1780
1781	if (!card->evt_buf_list[rdptr]) {
1782		skb_push(skb, INTF_HEADER_LEN);
1783		if (mwifiex_map_pci_memory(adapter, skb,
1784					   MAX_EVENT_SIZE,
1785					   PCI_DMA_FROMDEVICE))
1786			return -1;
1787		card->evt_buf_list[rdptr] = skb;
1788		desc = card->evtbd_ring[rdptr];
1789		desc->paddr = MWIFIEX_SKB_DMA_ADDR(skb);
1790		desc->len = (u16)skb->len;
1791		desc->flags = 0;
1792		skb = NULL;
1793	} else {
1794		dev_dbg(adapter->dev,
1795			"info: ERROR: buf still valid at index %d, <%p, %p>\n",
1796			rdptr, card->evt_buf_list[rdptr], skb);
1797	}
1798
1799	if ((++card->evtbd_rdptr & MWIFIEX_EVTBD_MASK) == MWIFIEX_MAX_EVT_BD) {
1800		card->evtbd_rdptr = ((card->evtbd_rdptr &
1801					reg->evt_rollover_ind) ^
1802					reg->evt_rollover_ind);
1803	}
1804
1805	dev_dbg(adapter->dev, "info: Updated <Rd: 0x%x, Wr: 0x%x>",
1806		card->evtbd_rdptr, wrptr);
1807
1808	/* Write the event ring read pointer in to reg->evt_rdptr */
1809	if (mwifiex_write_reg(adapter, reg->evt_rdptr,
1810			      card->evtbd_rdptr)) {
1811		dev_err(adapter->dev,
1812			"event_complete: failed to read reg->evt_rdptr\n");
1813		return -1;
1814	}
1815
1816	dev_dbg(adapter->dev, "info: Check Events Again\n");
1817	ret = mwifiex_pcie_process_event_ready(adapter);
1818
1819	return ret;
1820}
1821
1822/*
1823 * This function downloads the firmware to the card.
1824 *
1825 * Firmware is downloaded to the card in blocks. Every block download
1826 * is tested for CRC errors, and retried a number of times before
1827 * returning failure.
1828 */
1829static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
1830				    struct mwifiex_fw_image *fw)
1831{
1832	int ret;
1833	u8 *firmware = fw->fw_buf;
1834	u32 firmware_len = fw->fw_len;
1835	u32 offset = 0;
1836	struct sk_buff *skb;
1837	u32 txlen, tx_blocks = 0, tries, len;
1838	u32 block_retry_cnt = 0;
1839	struct pcie_service_card *card = adapter->card;
1840	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1841
1842	if (!firmware || !firmware_len) {
1843		dev_err(adapter->dev,
1844			"No firmware image found! Terminating download\n");
1845		return -1;
1846	}
1847
1848	dev_dbg(adapter->dev, "info: Downloading FW image (%d bytes)\n",
1849		firmware_len);
1850
1851	if (mwifiex_pcie_disable_host_int(adapter)) {
1852		dev_err(adapter->dev,
1853			"%s: Disabling interrupts failed.\n", __func__);
1854		return -1;
1855	}
1856
1857	skb = dev_alloc_skb(MWIFIEX_UPLD_SIZE);
1858	if (!skb) {
1859		ret = -ENOMEM;
1860		goto done;
1861	}
1862
1863	/* Perform firmware data transfer */
1864	do {
1865		u32 ireg_intr = 0;
1866
1867		/* More data? */
1868		if (offset >= firmware_len)
1869			break;
1870
1871		for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
1872			ret = mwifiex_read_reg(adapter, reg->cmd_size,
1873					       &len);
1874			if (ret) {
1875				dev_warn(adapter->dev,
1876					 "Failed reading len from boot code\n");
1877				goto done;
1878			}
1879			if (len)
1880				break;
1881			usleep_range(10, 20);
1882		}
1883
1884		if (!len) {
1885			break;
1886		} else if (len > MWIFIEX_UPLD_SIZE) {
1887			pr_err("FW download failure @ %d, invalid length %d\n",
1888			       offset, len);
1889			ret = -1;
1890			goto done;
1891		}
1892
1893		txlen = len;
1894
1895		if (len & BIT(0)) {
1896			block_retry_cnt++;
1897			if (block_retry_cnt > MAX_WRITE_IOMEM_RETRY) {
1898				pr_err("FW download failure @ %d, over max "
1899				       "retry count\n", offset);
1900				ret = -1;
1901				goto done;
1902			}
1903			dev_err(adapter->dev, "FW CRC error indicated by the "
1904				"helper: len = 0x%04X, txlen = %d\n",
1905				len, txlen);
1906			len &= ~BIT(0);
1907			/* Setting this to 0 to resend from same offset */
1908			txlen = 0;
1909		} else {
1910			block_retry_cnt = 0;
1911			/* Set blocksize to transfer - checking for
1912			   last block */
1913			if (firmware_len - offset < txlen)
1914				txlen = firmware_len - offset;
1915
1916			dev_dbg(adapter->dev, ".");
1917
1918			tx_blocks = (txlen + card->pcie.blksz_fw_dl - 1) /
1919				    card->pcie.blksz_fw_dl;
1920
1921			/* Copy payload to buffer */
1922			memmove(skb->data, &firmware[offset], txlen);
1923		}
1924
1925		skb_put(skb, MWIFIEX_UPLD_SIZE - skb->len);
1926		skb_trim(skb, tx_blocks * card->pcie.blksz_fw_dl);
1927
1928		/* Send the boot command to device */
1929		if (mwifiex_pcie_send_boot_cmd(adapter, skb)) {
1930			dev_err(adapter->dev,
1931				"Failed to send firmware download command\n");
1932			ret = -1;
1933			goto done;
1934		}
1935
1936		/* Wait for the command done interrupt */
1937		do {
1938			if (mwifiex_read_reg(adapter, PCIE_CPU_INT_STATUS,
1939					     &ireg_intr)) {
1940				dev_err(adapter->dev, "%s: Failed to read "
1941					"interrupt status during fw dnld.\n",
1942					__func__);
1943				mwifiex_unmap_pci_memory(adapter, skb,
1944							 PCI_DMA_TODEVICE);
1945				ret = -1;
1946				goto done;
1947			}
1948		} while ((ireg_intr & CPU_INTR_DOOR_BELL) ==
1949			 CPU_INTR_DOOR_BELL);
1950
1951		mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_TODEVICE);
1952
1953		offset += txlen;
1954	} while (true);
1955
1956	dev_notice(adapter->dev,
1957		   "info: FW download over, size %d bytes\n", offset);
1958
1959	ret = 0;
1960
1961done:
1962	dev_kfree_skb_any(skb);
1963	return ret;
1964}
1965
1966/*
1967 * This function checks the firmware status in card.
1968 *
1969 * The winner interface is also determined by this function.
1970 */
1971static int
1972mwifiex_check_fw_status(struct mwifiex_adapter *adapter, u32 poll_num)
1973{
1974	int ret = 0;
1975	u32 firmware_stat, winner_status;
1976	struct pcie_service_card *card = adapter->card;
1977	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1978	u32 tries;
1979
1980	/* Mask spurios interrupts */
1981	if (mwifiex_write_reg(adapter, PCIE_HOST_INT_STATUS_MASK,
1982			      HOST_INTR_MASK)) {
1983		dev_warn(adapter->dev, "Write register failed\n");
1984		return -1;
1985	}
1986
1987	dev_dbg(adapter->dev, "Setting driver ready signature\n");
1988	if (mwifiex_write_reg(adapter, reg->drv_rdy,
1989			      FIRMWARE_READY_PCIE)) {
1990		dev_err(adapter->dev,
1991			"Failed to write driver ready signature\n");
1992		return -1;
1993	}
1994
1995	/* Wait for firmware initialization event */
1996	for (tries = 0; tries < poll_num; tries++) {
1997		if (mwifiex_read_reg(adapter, reg->fw_status,
1998				     &firmware_stat))
1999			ret = -1;
2000		else
2001			ret = 0;
2002		if (ret)
2003			continue;
2004		if (firmware_stat == FIRMWARE_READY_PCIE) {
2005			ret = 0;
2006			break;
2007		} else {
2008			msleep(100);
2009			ret = -1;
2010		}
2011	}
2012
2013	if (ret) {
2014		if (mwifiex_read_reg(adapter, reg->fw_status,
2015				     &winner_status))
2016			ret = -1;
2017		else if (!winner_status) {
2018			dev_err(adapter->dev, "PCI-E is the winner\n");
2019			adapter->winner = 1;
2020		} else {
2021			dev_err(adapter->dev,
2022				"PCI-E is not the winner <%#x,%d>, exit dnld\n",
2023				ret, adapter->winner);
2024		}
2025	}
2026
2027	return ret;
2028}
2029
2030/*
2031 * This function reads the interrupt status from card.
2032 */
2033static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
2034{
2035	u32 pcie_ireg;
2036	unsigned long flags;
2037
2038	if (!mwifiex_pcie_ok_to_access_hw(adapter))
2039		return;
2040
2041	if (mwifiex_read_reg(adapter, PCIE_HOST_INT_STATUS, &pcie_ireg)) {
2042		dev_warn(adapter->dev, "Read register failed\n");
2043		return;
2044	}
2045
2046	if ((pcie_ireg != 0xFFFFFFFF) && (pcie_ireg)) {
2047
2048		mwifiex_pcie_disable_host_int(adapter);
2049
2050		/* Clear the pending interrupts */
2051		if (mwifiex_write_reg(adapter, PCIE_HOST_INT_STATUS,
2052				      ~pcie_ireg)) {
2053			dev_warn(adapter->dev, "Write register failed\n");
2054			return;
2055		}
2056		spin_lock_irqsave(&adapter->int_lock, flags);
2057		adapter->int_status |= pcie_ireg;
2058		spin_unlock_irqrestore(&adapter->int_lock, flags);
2059
2060		if (!adapter->pps_uapsd_mode &&
2061		    adapter->ps_state == PS_STATE_SLEEP &&
2062		    mwifiex_pcie_ok_to_access_hw(adapter)) {
2063				/* Potentially for PCIe we could get other
2064				 * interrupts like shared. Don't change power
2065				 * state until cookie is set */
2066				adapter->ps_state = PS_STATE_AWAKE;
2067				adapter->pm_wakeup_fw_try = false;
2068				del_timer(&adapter->wakeup_timer);
2069		}
2070	}
2071}
2072
2073/*
2074 * Interrupt handler for PCIe root port
2075 *
2076 * This function reads the interrupt status from firmware and assigns
2077 * the main process in workqueue which will handle the interrupt.
2078 */
2079static irqreturn_t mwifiex_pcie_interrupt(int irq, void *context)
2080{
2081	struct pci_dev *pdev = (struct pci_dev *)context;
2082	struct pcie_service_card *card;
2083	struct mwifiex_adapter *adapter;
2084
2085	if (!pdev) {
2086		pr_debug("info: %s: pdev is NULL\n", (u8 *)pdev);
2087		goto exit;
2088	}
2089
2090	card = pci_get_drvdata(pdev);
2091	if (!card || !card->adapter) {
2092		pr_debug("info: %s: card=%p adapter=%p\n", __func__, card,
2093			 card ? card->adapter : NULL);
2094		goto exit;
2095	}
2096	adapter = card->adapter;
2097
2098	if (adapter->surprise_removed)
2099		goto exit;
2100
2101	mwifiex_interrupt_status(adapter);
2102	mwifiex_queue_main_work(adapter);
2103
2104exit:
2105	return IRQ_HANDLED;
2106}
2107
2108/*
2109 * This function checks the current interrupt status.
2110 *
2111 * The following interrupts are checked and handled by this function -
2112 *      - Data sent
2113 *      - Command sent
2114 *      - Command received
2115 *      - Packets received
2116 *      - Events received
2117 *
2118 * In case of Rx packets received, the packets are uploaded from card to
2119 * host and processed accordingly.
2120 */
2121static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
2122{
2123	int ret;
2124	u32 pcie_ireg;
2125	unsigned long flags;
2126
2127	spin_lock_irqsave(&adapter->int_lock, flags);
2128	/* Clear out unused interrupts */
2129	pcie_ireg = adapter->int_status;
2130	adapter->int_status = 0;
2131	spin_unlock_irqrestore(&adapter->int_lock, flags);
2132
2133	while (pcie_ireg & HOST_INTR_MASK) {
2134		if (pcie_ireg & HOST_INTR_DNLD_DONE) {
2135			pcie_ireg &= ~HOST_INTR_DNLD_DONE;
2136			dev_dbg(adapter->dev, "info: TX DNLD Done\n");
2137			ret = mwifiex_pcie_send_data_complete(adapter);
2138			if (ret)
2139				return ret;
2140		}
2141		if (pcie_ireg & HOST_INTR_UPLD_RDY) {
2142			pcie_ireg &= ~HOST_INTR_UPLD_RDY;
2143			dev_dbg(adapter->dev, "info: Rx DATA\n");
2144			ret = mwifiex_pcie_process_recv_data(adapter);
2145			if (ret)
2146				return ret;
2147		}
2148		if (pcie_ireg & HOST_INTR_EVENT_RDY) {
2149			pcie_ireg &= ~HOST_INTR_EVENT_RDY;
2150			dev_dbg(adapter->dev, "info: Rx EVENT\n");
2151			ret = mwifiex_pcie_process_event_ready(adapter);
2152			if (ret)
2153				return ret;
2154		}
2155
2156		if (pcie_ireg & HOST_INTR_CMD_DONE) {
2157			pcie_ireg &= ~HOST_INTR_CMD_DONE;
2158			if (adapter->cmd_sent) {
2159				dev_dbg(adapter->dev,
2160					"info: CMD sent Interrupt\n");
2161				adapter->cmd_sent = false;
2162			}
2163			/* Handle command response */
2164			ret = mwifiex_pcie_process_cmd_complete(adapter);
2165			if (ret)
2166				return ret;
2167		}
2168
2169		if (mwifiex_pcie_ok_to_access_hw(adapter)) {
2170			if (mwifiex_read_reg(adapter, PCIE_HOST_INT_STATUS,
2171					     &pcie_ireg)) {
2172				dev_warn(adapter->dev,
2173					 "Read register failed\n");
2174				return -1;
2175			}
2176
2177			if ((pcie_ireg != 0xFFFFFFFF) && (pcie_ireg)) {
2178				if (mwifiex_write_reg(adapter,
2179						      PCIE_HOST_INT_STATUS,
2180						      ~pcie_ireg)) {
2181					dev_warn(adapter->dev,
2182						 "Write register failed\n");
2183					return -1;
2184				}
2185			}
2186
2187		}
2188	}
2189	dev_dbg(adapter->dev, "info: cmd_sent=%d data_sent=%d\n",
2190		adapter->cmd_sent, adapter->data_sent);
2191	if (adapter->ps_state != PS_STATE_SLEEP)
2192		mwifiex_pcie_enable_host_int(adapter);
2193
2194	return 0;
2195}
2196
2197/*
2198 * This function downloads data from driver to card.
2199 *
2200 * Both commands and data packets are transferred to the card by this
2201 * function.
2202 *
2203 * This function adds the PCIE specific header to the front of the buffer
2204 * before transferring. The header contains the length of the packet and
2205 * the type. The firmware handles the packets based upon this set type.
2206 */
2207static int mwifiex_pcie_host_to_card(struct mwifiex_adapter *adapter, u8 type,
2208				     struct sk_buff *skb,
2209				     struct mwifiex_tx_param *tx_param)
2210{
2211	if (!skb) {
2212		dev_err(adapter->dev, "Passed NULL skb to %s\n", __func__);
2213		return -1;
2214	}
2215
2216	if (type == MWIFIEX_TYPE_DATA)
2217		return mwifiex_pcie_send_data(adapter, skb, tx_param);
2218	else if (type == MWIFIEX_TYPE_CMD)
2219		return mwifiex_pcie_send_cmd(adapter, skb);
2220
2221	return 0;
2222}
2223
2224/* This function read/write firmware */
2225static enum rdwr_status
2226mwifiex_pcie_rdwr_firmware(struct mwifiex_adapter *adapter, u8 doneflag)
2227{
2228	int ret, tries;
2229	u8 ctrl_data;
2230	struct pcie_service_card *card = adapter->card;
2231	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
2232
2233	ret = mwifiex_write_reg(adapter, reg->fw_dump_ctrl, FW_DUMP_HOST_READY);
2234	if (ret) {
2235		dev_err(adapter->dev, "PCIE write err\n");
2236		return RDWR_STATUS_FAILURE;
2237	}
2238
2239	for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
2240		mwifiex_read_reg_byte(adapter, reg->fw_dump_ctrl, &ctrl_data);
2241		if (ctrl_data == FW_DUMP_DONE)
2242			return RDWR_STATUS_SUCCESS;
2243		if (doneflag && ctrl_data == doneflag)
2244			return RDWR_STATUS_DONE;
2245		if (ctrl_data != FW_DUMP_HOST_READY) {
2246			dev_info(adapter->dev,
2247				 "The ctrl reg was changed, re-try again!\n");
2248			ret = mwifiex_write_reg(adapter, reg->fw_dump_ctrl,
2249						FW_DUMP_HOST_READY);
2250			if (ret) {
2251				dev_err(adapter->dev, "PCIE write err\n");
2252				return RDWR_STATUS_FAILURE;
2253			}
2254		}
2255		usleep_range(100, 200);
2256	}
2257
2258	dev_err(adapter->dev, "Fail to pull ctrl_data\n");
2259	return RDWR_STATUS_FAILURE;
2260}
2261
2262/* This function dump firmware memory to file */
2263static void mwifiex_pcie_fw_dump_work(struct mwifiex_adapter *adapter)
2264{
2265	struct pcie_service_card *card = adapter->card;
2266	const struct mwifiex_pcie_card_reg *creg = card->pcie.reg;
2267	unsigned int reg, reg_start, reg_end;
2268	u8 *dbg_ptr, *end_ptr, dump_num, idx, i, read_reg, doneflag = 0;
2269	enum rdwr_status stat;
2270	u32 memory_size;
2271	int ret;
2272	static char *env[] = { "DRIVER=mwifiex_pcie", "EVENT=fw_dump", NULL };
2273
2274	if (!card->pcie.can_dump_fw)
2275		return;
2276
2277	for (idx = 0; idx < ARRAY_SIZE(mem_type_mapping_tbl); idx++) {
2278		struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
2279
2280		if (entry->mem_ptr) {
2281			vfree(entry->mem_ptr);
2282			entry->mem_ptr = NULL;
2283		}
2284		entry->mem_size = 0;
2285	}
2286
2287	dev_info(adapter->dev, "== mwifiex firmware dump start ==\n");
2288
2289	/* Read the number of the memories which will dump */
2290	stat = mwifiex_pcie_rdwr_firmware(adapter, doneflag);
2291	if (stat == RDWR_STATUS_FAILURE)
2292		goto done;
2293
2294	reg = creg->fw_dump_start;
2295	mwifiex_read_reg_byte(adapter, reg, &dump_num);
2296
2297	/* Read the length of every memory which will dump */
2298	for (idx = 0; idx < dump_num; idx++) {
2299		struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
2300
2301		stat = mwifiex_pcie_rdwr_firmware(adapter, doneflag);
2302		if (stat == RDWR_STATUS_FAILURE)
2303			goto done;
2304
2305		memory_size = 0;
2306		reg = creg->fw_dump_start;
2307		for (i = 0; i < 4; i++) {
2308			mwifiex_read_reg_byte(adapter, reg, &read_reg);
2309			memory_size |= (read_reg << (i * 8));
2310			reg++;
2311		}
2312
2313		if (memory_size == 0) {
2314			dev_info(adapter->dev, "Firmware dump Finished!\n");
2315			ret = mwifiex_write_reg(adapter, creg->fw_dump_ctrl,
2316						FW_DUMP_READ_DONE);
2317			if (ret) {
2318				dev_err(adapter->dev, "PCIE write err\n");
2319				goto done;
2320			}
2321			break;
2322		}
2323
2324		dev_info(adapter->dev,
2325			 "%s_SIZE=0x%x\n", entry->mem_name, memory_size);
2326		entry->mem_ptr = vmalloc(memory_size + 1);
2327		entry->mem_size = memory_size;
2328		if (!entry->mem_ptr) {
2329			dev_err(adapter->dev,
2330				"Vmalloc %s failed\n", entry->mem_name);
2331			goto done;
2332		}
2333		dbg_ptr = entry->mem_ptr;
2334		end_ptr = dbg_ptr + memory_size;
2335
2336		doneflag = entry->done_flag;
2337		dev_info(adapter->dev, "Start %s output, please wait...\n",
2338			 entry->mem_name);
2339
2340		do {
2341			stat = mwifiex_pcie_rdwr_firmware(adapter, doneflag);
2342			if (RDWR_STATUS_FAILURE == stat)
2343				goto done;
2344
2345			reg_start = creg->fw_dump_start;
2346			reg_end = creg->fw_dump_end;
2347			for (reg = reg_start; reg <= reg_end; reg++) {
2348				mwifiex_read_reg_byte(adapter, reg, dbg_ptr);
2349				if (dbg_ptr < end_ptr) {
2350					dbg_ptr++;
2351				} else {
2352					dev_err(adapter->dev,
2353						"Allocated buf not enough\n");
2354					goto done;
2355				}
2356			}
2357
2358			if (stat != RDWR_STATUS_DONE)
2359				continue;
2360
2361			dev_info(adapter->dev, "%s done: size=0x%tx\n",
2362				 entry->mem_name, dbg_ptr - entry->mem_ptr);
2363			break;
2364		} while (true);
2365	}
2366	dev_info(adapter->dev, "== mwifiex firmware dump end ==\n");
2367
2368	kobject_uevent_env(&adapter->wiphy->dev.kobj, KOBJ_CHANGE, env);
2369
2370done:
2371	adapter->curr_mem_idx = 0;
2372}
2373
2374static unsigned long iface_work_flags;
2375static struct mwifiex_adapter *save_adapter;
2376static void mwifiex_pcie_work(struct work_struct *work)
2377{
2378	if (test_and_clear_bit(MWIFIEX_IFACE_WORK_FW_DUMP,
2379			       &iface_work_flags))
2380		mwifiex_pcie_fw_dump_work(save_adapter);
2381}
2382
2383static DECLARE_WORK(pcie_work, mwifiex_pcie_work);
2384/* This function dumps FW information */
2385static void mwifiex_pcie_fw_dump(struct mwifiex_adapter *adapter)
2386{
2387	save_adapter = adapter;
2388	if (test_bit(MWIFIEX_IFACE_WORK_FW_DUMP, &iface_work_flags))
2389		return;
2390
2391	set_bit(MWIFIEX_IFACE_WORK_FW_DUMP, &iface_work_flags);
2392
2393	schedule_work(&pcie_work);
2394}
2395
2396/*
2397 * This function initializes the PCI-E host memory space, WCB rings, etc.
2398 *
2399 * The following initializations steps are followed -
2400 *      - Allocate TXBD ring buffers
2401 *      - Allocate RXBD ring buffers
2402 *      - Allocate event BD ring buffers
2403 *      - Allocate command response ring buffer
2404 *      - Allocate sleep cookie buffer
2405 */
2406static int mwifiex_pcie_init(struct mwifiex_adapter *adapter)
2407{
2408	struct pcie_service_card *card = adapter->card;
2409	int ret;
2410	struct pci_dev *pdev = card->dev;
2411	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
2412
2413	pci_set_drvdata(pdev, card);
2414
2415	ret = pci_enable_device(pdev);
2416	if (ret)
2417		goto err_enable_dev;
2418
2419	pci_set_master(pdev);
2420
2421	dev_dbg(adapter->dev, "try set_consistent_dma_mask(32)\n");
2422	ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2423	if (ret) {
2424		dev_err(adapter->dev, "set_dma_mask(32) failed\n");
2425		goto err_set_dma_mask;
2426	}
2427
2428	ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2429	if (ret) {
2430		dev_err(adapter->dev, "set_consistent_dma_mask(64) failed\n");
2431		goto err_set_dma_mask;
2432	}
2433
2434	ret = pci_request_region(pdev, 0, DRV_NAME);
2435	if (ret) {
2436		dev_err(adapter->dev, "req_reg(0) error\n");
2437		goto err_req_region0;
2438	}
2439	card->pci_mmap = pci_iomap(pdev, 0, 0);
2440	if (!card->pci_mmap) {
2441		dev_err(adapter->dev, "iomap(0) error\n");
2442		ret = -EIO;
2443		goto err_iomap0;
2444	}
2445	ret = pci_request_region(pdev, 2, DRV_NAME);
2446	if (ret) {
2447		dev_err(adapter->dev, "req_reg(2) error\n");
2448		goto err_req_region2;
2449	}
2450	card->pci_mmap1 = pci_iomap(pdev, 2, 0);
2451	if (!card->pci_mmap1) {
2452		dev_err(adapter->dev, "iomap(2) error\n");
2453		ret = -EIO;
2454		goto err_iomap2;
2455	}
2456
2457	dev_dbg(adapter->dev,
2458		"PCI memory map Virt0: %p PCI memory map Virt2: %p\n",
2459		card->pci_mmap, card->pci_mmap1);
2460
2461	card->cmdrsp_buf = NULL;
2462	ret = mwifiex_pcie_create_txbd_ring(adapter);
2463	if (ret)
2464		goto err_cre_txbd;
2465	ret = mwifiex_pcie_create_rxbd_ring(adapter);
2466	if (ret)
2467		goto err_cre_rxbd;
2468	ret = mwifiex_pcie_create_evtbd_ring(adapter);
2469	if (ret)
2470		goto err_cre_evtbd;
2471	ret = mwifiex_pcie_alloc_cmdrsp_buf(adapter);
2472	if (ret)
2473		goto err_alloc_cmdbuf;
2474	if (reg->sleep_cookie) {
2475		ret = mwifiex_pcie_alloc_sleep_cookie_buf(adapter);
2476		if (ret)
2477			goto err_alloc_cookie;
2478	} else {
2479		card->sleep_cookie_vbase = NULL;
2480	}
2481	return ret;
2482
2483err_alloc_cookie:
2484	mwifiex_pcie_delete_cmdrsp_buf(adapter);
2485err_alloc_cmdbuf:
2486	mwifiex_pcie_delete_evtbd_ring(adapter);
2487err_cre_evtbd:
2488	mwifiex_pcie_delete_rxbd_ring(adapter);
2489err_cre_rxbd:
2490	mwifiex_pcie_delete_txbd_ring(adapter);
2491err_cre_txbd:
2492	pci_iounmap(pdev, card->pci_mmap1);
2493err_iomap2:
2494	pci_release_region(pdev, 2);
2495err_req_region2:
2496	pci_iounmap(pdev, card->pci_mmap);
2497err_iomap0:
2498	pci_release_region(pdev, 0);
2499err_req_region0:
2500err_set_dma_mask:
2501	pci_disable_device(pdev);
2502err_enable_dev:
2503	pci_set_drvdata(pdev, NULL);
2504	return ret;
2505}
2506
2507/*
2508 * This function cleans up the allocated card buffers.
2509 *
2510 * The following are freed by this function -
2511 *      - TXBD ring buffers
2512 *      - RXBD ring buffers
2513 *      - Event BD ring buffers
2514 *      - Command response ring buffer
2515 *      - Sleep cookie buffer
2516 */
2517static void mwifiex_pcie_cleanup(struct mwifiex_adapter *adapter)
2518{
2519	struct pcie_service_card *card = adapter->card;
2520	struct pci_dev *pdev = card->dev;
2521	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
2522
2523	if (user_rmmod) {
2524		dev_dbg(adapter->dev, "Clearing driver ready signature\n");
2525		if (mwifiex_write_reg(adapter, reg->drv_rdy, 0x00000000))
2526			dev_err(adapter->dev,
2527				"Failed to write driver not-ready signature\n");
2528	}
2529
2530	if (pdev) {
2531		pci_iounmap(pdev, card->pci_mmap);
2532		pci_iounmap(pdev, card->pci_mmap1);
2533		pci_disable_device(pdev);
2534		pci_release_region(pdev, 2);
2535		pci_release_region(pdev, 0);
2536		pci_set_drvdata(pdev, NULL);
2537	}
2538	kfree(card);
2539}
2540
2541/*
2542 * This function registers the PCIE device.
2543 *
2544 * PCIE IRQ is claimed, block size is set and driver data is initialized.
2545 */
2546static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
2547{
2548	int ret;
2549	struct pcie_service_card *card = adapter->card;
2550	struct pci_dev *pdev = card->dev;
2551
2552	/* save adapter pointer in card */
2553	card->adapter = adapter;
2554
2555	ret = request_irq(pdev->irq, mwifiex_pcie_interrupt, IRQF_SHARED,
2556			  "MRVL_PCIE", pdev);
2557	if (ret) {
2558		pr_err("request_irq failed: ret=%d\n", ret);
2559		adapter->card = NULL;
2560		return -1;
2561	}
2562
2563	adapter->dev = &pdev->dev;
2564	adapter->tx_buf_size = card->pcie.tx_buf_size;
2565	adapter->mem_type_mapping_tbl = mem_type_mapping_tbl;
2566	adapter->num_mem_types = ARRAY_SIZE(mem_type_mapping_tbl);
2567	strcpy(adapter->fw_name, card->pcie.firmware);
2568	adapter->ext_scan = card->pcie.can_ext_scan;
2569
2570	return 0;
2571}
2572
2573/*
2574 * This function unregisters the PCIE device.
2575 *
2576 * The PCIE IRQ is released, the function is disabled and driver
2577 * data is set to null.
2578 */
2579static void mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
2580{
2581	struct pcie_service_card *card = adapter->card;
2582	const struct mwifiex_pcie_card_reg *reg;
2583
2584	if (card) {
2585		dev_dbg(adapter->dev, "%s(): calling free_irq()\n", __func__);
2586		free_irq(card->dev->irq, card->dev);
2587
2588		reg = card->pcie.reg;
2589		if (reg->sleep_cookie)
2590			mwifiex_pcie_delete_sleep_cookie_buf(adapter);
2591
2592		mwifiex_pcie_delete_cmdrsp_buf(adapter);
2593		mwifiex_pcie_delete_evtbd_ring(adapter);
2594		mwifiex_pcie_delete_rxbd_ring(adapter);
2595		mwifiex_pcie_delete_txbd_ring(adapter);
2596		card->cmdrsp_buf = NULL;
2597	}
2598}
2599
2600static struct mwifiex_if_ops pcie_ops = {
2601	.init_if =			mwifiex_pcie_init,
2602	.cleanup_if =			mwifiex_pcie_cleanup,
2603	.check_fw_status =		mwifiex_check_fw_status,
2604	.prog_fw =			mwifiex_prog_fw_w_helper,
2605	.register_dev =			mwifiex_register_dev,
2606	.unregister_dev =		mwifiex_unregister_dev,
2607	.enable_int =			mwifiex_pcie_enable_host_int,
2608	.process_int_status =		mwifiex_process_int_status,
2609	.host_to_card =			mwifiex_pcie_host_to_card,
2610	.wakeup =			mwifiex_pm_wakeup_card,
2611	.wakeup_complete =		mwifiex_pm_wakeup_card_complete,
2612
2613	/* PCIE specific */
2614	.cmdrsp_complete =		mwifiex_pcie_cmdrsp_complete,
2615	.event_complete =		mwifiex_pcie_event_complete,
2616	.update_mp_end_port =		NULL,
2617	.cleanup_mpa_buf =		NULL,
2618	.init_fw_port =			mwifiex_pcie_init_fw_port,
2619	.clean_pcie_ring =		mwifiex_clean_pcie_ring_buf,
2620	.fw_dump =			mwifiex_pcie_fw_dump,
2621};
2622
2623/*
2624 * This function initializes the PCIE driver module.
2625 *
2626 * This initiates the semaphore and registers the device with
2627 * PCIE bus.
2628 */
2629static int mwifiex_pcie_init_module(void)
2630{
2631	int ret;
2632
2633	pr_debug("Marvell PCIe Driver\n");
2634
2635	sema_init(&add_remove_card_sem, 1);
2636
2637	/* Clear the flag in case user removes the card. */
2638	user_rmmod = 0;
2639
2640	ret = pci_register_driver(&mwifiex_pcie);
2641	if (ret)
2642		pr_err("Driver register failed!\n");
2643	else
2644		pr_debug("info: Driver registered successfully!\n");
2645
2646	return ret;
2647}
2648
2649/*
2650 * This function cleans up the PCIE driver.
2651 *
2652 * The following major steps are followed for cleanup -
2653 *      - Resume the device if its suspended
2654 *      - Disconnect the device if connected
2655 *      - Shutdown the firmware
2656 *      - Unregister the device from PCIE bus.
2657 */
2658static void mwifiex_pcie_cleanup_module(void)
2659{
2660	if (!down_interruptible(&add_remove_card_sem))
2661		up(&add_remove_card_sem);
2662
2663	/* Set the flag as user is removing this module. */
2664	user_rmmod = 1;
2665
2666	cancel_work_sync(&pcie_work);
2667	pci_unregister_driver(&mwifiex_pcie);
2668}
2669
2670module_init(mwifiex_pcie_init_module);
2671module_exit(mwifiex_pcie_cleanup_module);
2672
2673MODULE_AUTHOR("Marvell International Ltd.");
2674MODULE_DESCRIPTION("Marvell WiFi-Ex PCI-Express Driver version " PCIE_VERSION);
2675MODULE_VERSION(PCIE_VERSION);
2676MODULE_LICENSE("GPL v2");
2677MODULE_FIRMWARE(PCIE8766_DEFAULT_FW_NAME);
2678MODULE_FIRMWARE(PCIE8897_DEFAULT_FW_NAME);
2679