mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / net / wireless / mwifiex / pcie.c
blobcb84edcd794b69bef512727b871008e5c5d3ae09
1 /*
2 * Marvell Wireless LAN device driver: PCIE specific handling
4 * Copyright (C) 2011, Marvell International Ltd.
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.
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.
20 #include <linux/firmware.h>
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"
31 #define PCIE_VERSION "1.0"
32 #define DRV_NAME "Marvell mwifiex PCIe"
34 static u8 user_rmmod;
36 static struct mwifiex_if_ops pcie_ops;
38 static struct semaphore add_remove_card_sem;
40 static int
41 mwifiex_map_pci_memory(struct mwifiex_adapter *adapter, struct sk_buff *skb,
42 int size, int flags)
44 struct pcie_service_card *card = adapter->card;
45 dma_addr_t buf_pa;
47 buf_pa = pci_map_single(card->dev, skb->data, size, flags);
48 if (pci_dma_mapping_error(card->dev, buf_pa)) {
49 dev_err(adapter->dev, "failed to map pci memory!\n");
50 return -1;
52 memcpy(skb->cb, &buf_pa, sizeof(dma_addr_t));
53 return 0;
57 * This function reads sleep cookie and checks if FW is ready
59 static bool mwifiex_pcie_ok_to_access_hw(struct mwifiex_adapter *adapter)
61 u32 *cookie_addr;
62 struct pcie_service_card *card = adapter->card;
63 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
65 if (!reg->sleep_cookie)
66 return true;
68 if (card->sleep_cookie_vbase) {
69 cookie_addr = (u32 *)card->sleep_cookie_vbase;
70 dev_dbg(adapter->dev, "info: ACCESS_HW: sleep cookie=0x%x\n",
71 *cookie_addr);
72 if (*cookie_addr == FW_AWAKE_COOKIE)
73 return true;
76 return false;
79 #ifdef CONFIG_PM_SLEEP
81 * Kernel needs to suspend all functions separately. Therefore all
82 * registered functions must have drivers with suspend and resume
83 * methods. Failing that the kernel simply removes the whole card.
85 * If already not suspended, this function allocates and sends a host
86 * sleep activate request to the firmware and turns off the traffic.
88 static int mwifiex_pcie_suspend(struct device *dev)
90 struct mwifiex_adapter *adapter;
91 struct pcie_service_card *card;
92 int hs_actived;
93 struct pci_dev *pdev = to_pci_dev(dev);
95 if (pdev) {
96 card = (struct pcie_service_card *) pci_get_drvdata(pdev);
97 if (!card || !card->adapter) {
98 pr_err("Card or adapter structure is not valid\n");
99 return 0;
101 } else {
102 pr_err("PCIE device is not specified\n");
103 return 0;
106 adapter = card->adapter;
108 hs_actived = mwifiex_enable_hs(adapter);
110 /* Indicate device suspended */
111 adapter->is_suspended = true;
113 return 0;
117 * Kernel needs to suspend all functions separately. Therefore all
118 * registered functions must have drivers with suspend and resume
119 * methods. Failing that the kernel simply removes the whole card.
121 * If already not resumed, this function turns on the traffic and
122 * sends a host sleep cancel request to the firmware.
124 static int mwifiex_pcie_resume(struct device *dev)
126 struct mwifiex_adapter *adapter;
127 struct pcie_service_card *card;
128 struct pci_dev *pdev = to_pci_dev(dev);
130 if (pdev) {
131 card = (struct pcie_service_card *) pci_get_drvdata(pdev);
132 if (!card || !card->adapter) {
133 pr_err("Card or adapter structure is not valid\n");
134 return 0;
136 } else {
137 pr_err("PCIE device is not specified\n");
138 return 0;
141 adapter = card->adapter;
143 if (!adapter->is_suspended) {
144 dev_warn(adapter->dev, "Device already resumed\n");
145 return 0;
148 adapter->is_suspended = false;
150 mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
151 MWIFIEX_ASYNC_CMD);
153 return 0;
155 #endif
158 * This function probes an mwifiex device and registers it. It allocates
159 * the card structure, enables PCIE function number and initiates the
160 * device registration and initialization procedure by adding a logical
161 * interface.
163 static int mwifiex_pcie_probe(struct pci_dev *pdev,
164 const struct pci_device_id *ent)
166 struct pcie_service_card *card;
168 pr_debug("info: vendor=0x%4.04X device=0x%4.04X rev=%d\n",
169 pdev->vendor, pdev->device, pdev->revision);
171 card = kzalloc(sizeof(struct pcie_service_card), GFP_KERNEL);
172 if (!card)
173 return -ENOMEM;
175 card->dev = pdev;
177 if (ent->driver_data) {
178 struct mwifiex_pcie_device *data = (void *)ent->driver_data;
179 card->pcie.firmware = data->firmware;
180 card->pcie.reg = data->reg;
181 card->pcie.blksz_fw_dl = data->blksz_fw_dl;
184 if (mwifiex_add_card(card, &add_remove_card_sem, &pcie_ops,
185 MWIFIEX_PCIE)) {
186 pr_err("%s failed\n", __func__);
187 kfree(card);
188 return -1;
191 return 0;
195 * This function removes the interface and frees up the card structure.
197 static void mwifiex_pcie_remove(struct pci_dev *pdev)
199 struct pcie_service_card *card;
200 struct mwifiex_adapter *adapter;
201 struct mwifiex_private *priv;
202 int i;
204 card = pci_get_drvdata(pdev);
205 if (!card)
206 return;
208 adapter = card->adapter;
209 if (!adapter || !adapter->priv_num)
210 return;
212 /* In case driver is removed when asynchronous FW load is in progress */
213 wait_for_completion(&adapter->fw_load);
215 if (user_rmmod) {
216 #ifdef CONFIG_PM_SLEEP
217 if (adapter->is_suspended)
218 mwifiex_pcie_resume(&pdev->dev);
219 #endif
221 for (i = 0; i < adapter->priv_num; i++)
222 if ((GET_BSS_ROLE(adapter->priv[i]) ==
223 MWIFIEX_BSS_ROLE_STA) &&
224 adapter->priv[i]->media_connected)
225 mwifiex_deauthenticate(adapter->priv[i], NULL);
227 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
229 mwifiex_disable_auto_ds(priv);
231 mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
234 mwifiex_remove_card(card->adapter, &add_remove_card_sem);
235 kfree(card);
238 static void mwifiex_pcie_shutdown(struct pci_dev *pdev)
240 user_rmmod = 1;
241 mwifiex_pcie_remove(pdev);
243 return;
246 static DEFINE_PCI_DEVICE_TABLE(mwifiex_ids) = {
248 PCIE_VENDOR_ID_MARVELL, PCIE_DEVICE_ID_MARVELL_88W8766P,
249 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
250 .driver_data = (unsigned long) &mwifiex_pcie8766,
253 PCIE_VENDOR_ID_MARVELL, PCIE_DEVICE_ID_MARVELL_88W8897,
254 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
255 .driver_data = (unsigned long) &mwifiex_pcie8897,
260 MODULE_DEVICE_TABLE(pci, mwifiex_ids);
262 #ifdef CONFIG_PM_SLEEP
263 /* Power Management Hooks */
264 static SIMPLE_DEV_PM_OPS(mwifiex_pcie_pm_ops, mwifiex_pcie_suspend,
265 mwifiex_pcie_resume);
266 #endif
268 /* PCI Device Driver */
269 static struct pci_driver __refdata mwifiex_pcie = {
270 .name = "mwifiex_pcie",
271 .id_table = mwifiex_ids,
272 .probe = mwifiex_pcie_probe,
273 .remove = mwifiex_pcie_remove,
274 #ifdef CONFIG_PM_SLEEP
275 .driver = {
276 .pm = &mwifiex_pcie_pm_ops,
278 #endif
279 .shutdown = mwifiex_pcie_shutdown,
283 * This function writes data into PCIE card register.
285 static int mwifiex_write_reg(struct mwifiex_adapter *adapter, int reg, u32 data)
287 struct pcie_service_card *card = adapter->card;
289 iowrite32(data, card->pci_mmap1 + reg);
291 return 0;
295 * This function reads data from PCIE card register.
297 static int mwifiex_read_reg(struct mwifiex_adapter *adapter, int reg, u32 *data)
299 struct pcie_service_card *card = adapter->card;
301 *data = ioread32(card->pci_mmap1 + reg);
303 return 0;
307 * This function adds delay loop to ensure FW is awake before proceeding.
309 static void mwifiex_pcie_dev_wakeup_delay(struct mwifiex_adapter *adapter)
311 int i = 0;
313 while (mwifiex_pcie_ok_to_access_hw(adapter)) {
314 i++;
315 usleep_range(10, 20);
316 /* 50ms max wait */
317 if (i == 5000)
318 break;
321 return;
324 /* This function wakes up the card by reading fw_status register. */
325 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
327 u32 fw_status;
328 struct pcie_service_card *card = adapter->card;
329 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
331 dev_dbg(adapter->dev, "event: Wakeup device...\n");
333 if (reg->sleep_cookie)
334 mwifiex_pcie_dev_wakeup_delay(adapter);
336 /* Reading fw_status register will wakeup device */
337 if (mwifiex_read_reg(adapter, reg->fw_status, &fw_status)) {
338 dev_warn(adapter->dev, "Reading fw_status register failed\n");
339 return -1;
342 if (reg->sleep_cookie) {
343 mwifiex_pcie_dev_wakeup_delay(adapter);
344 dev_dbg(adapter->dev, "PCIE wakeup: Setting PS_STATE_AWAKE\n");
345 adapter->ps_state = PS_STATE_AWAKE;
348 return 0;
352 * This function is called after the card has woken up.
354 * The card configuration register is reset.
356 static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
358 dev_dbg(adapter->dev, "cmd: Wakeup device completed\n");
360 return 0;
364 * This function disables the host interrupt.
366 * The host interrupt mask is read, the disable bit is reset and
367 * written back to the card host interrupt mask register.
369 static int mwifiex_pcie_disable_host_int(struct mwifiex_adapter *adapter)
371 if (mwifiex_pcie_ok_to_access_hw(adapter)) {
372 if (mwifiex_write_reg(adapter, PCIE_HOST_INT_MASK,
373 0x00000000)) {
374 dev_warn(adapter->dev, "Disable host interrupt failed\n");
375 return -1;
379 return 0;
383 * This function enables the host interrupt.
385 * The host interrupt enable mask is written to the card
386 * host interrupt mask register.
388 static int mwifiex_pcie_enable_host_int(struct mwifiex_adapter *adapter)
390 if (mwifiex_pcie_ok_to_access_hw(adapter)) {
391 /* Simply write the mask to the register */
392 if (mwifiex_write_reg(adapter, PCIE_HOST_INT_MASK,
393 HOST_INTR_MASK)) {
394 dev_warn(adapter->dev, "Enable host interrupt failed\n");
395 return -1;
399 return 0;
403 * This function initializes TX buffer ring descriptors
405 static int mwifiex_init_txq_ring(struct mwifiex_adapter *adapter)
407 struct pcie_service_card *card = adapter->card;
408 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
409 struct mwifiex_pcie_buf_desc *desc;
410 struct mwifiex_pfu_buf_desc *desc2;
411 int i;
413 for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
414 card->tx_buf_list[i] = NULL;
415 if (reg->pfu_enabled) {
416 card->txbd_ring[i] = (void *)card->txbd_ring_vbase +
417 (sizeof(*desc2) * i);
418 desc2 = card->txbd_ring[i];
419 memset(desc2, 0, sizeof(*desc2));
420 } else {
421 card->txbd_ring[i] = (void *)card->txbd_ring_vbase +
422 (sizeof(*desc) * i);
423 desc = card->txbd_ring[i];
424 memset(desc, 0, sizeof(*desc));
428 return 0;
431 /* This function initializes RX buffer ring descriptors. Each SKB is allocated
432 * here and after mapping PCI memory, its physical address is assigned to
433 * PCIE Rx buffer descriptor's physical address.
435 static int mwifiex_init_rxq_ring(struct mwifiex_adapter *adapter)
437 struct pcie_service_card *card = adapter->card;
438 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
439 struct sk_buff *skb;
440 struct mwifiex_pcie_buf_desc *desc;
441 struct mwifiex_pfu_buf_desc *desc2;
442 dma_addr_t buf_pa;
443 int i;
445 for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
446 /* Allocate skb here so that firmware can DMA data from it */
447 skb = dev_alloc_skb(MWIFIEX_RX_DATA_BUF_SIZE);
448 if (!skb) {
449 dev_err(adapter->dev,
450 "Unable to allocate skb for RX ring.\n");
451 kfree(card->rxbd_ring_vbase);
452 return -ENOMEM;
455 if (mwifiex_map_pci_memory(adapter, skb,
456 MWIFIEX_RX_DATA_BUF_SIZE,
457 PCI_DMA_FROMDEVICE))
458 return -1;
460 MWIFIEX_SKB_PACB(skb, &buf_pa);
462 dev_dbg(adapter->dev,
463 "info: RX ring: skb=%p len=%d data=%p buf_pa=%#x:%x\n",
464 skb, skb->len, skb->data, (u32)buf_pa,
465 (u32)((u64)buf_pa >> 32));
467 card->rx_buf_list[i] = skb;
468 if (reg->pfu_enabled) {
469 card->rxbd_ring[i] = (void *)card->rxbd_ring_vbase +
470 (sizeof(*desc2) * i);
471 desc2 = card->rxbd_ring[i];
472 desc2->paddr = buf_pa;
473 desc2->len = (u16)skb->len;
474 desc2->frag_len = (u16)skb->len;
475 desc2->flags = reg->ring_flag_eop | reg->ring_flag_sop;
476 desc2->offset = 0;
477 } else {
478 card->rxbd_ring[i] = (void *)(card->rxbd_ring_vbase +
479 (sizeof(*desc) * i));
480 desc = card->rxbd_ring[i];
481 desc->paddr = buf_pa;
482 desc->len = (u16)skb->len;
483 desc->flags = 0;
487 return 0;
490 /* This function initializes event buffer ring descriptors. Each SKB is
491 * allocated here and after mapping PCI memory, its physical address is assigned
492 * to PCIE Rx buffer descriptor's physical address
494 static int mwifiex_pcie_init_evt_ring(struct mwifiex_adapter *adapter)
496 struct pcie_service_card *card = adapter->card;
497 struct mwifiex_evt_buf_desc *desc;
498 struct sk_buff *skb;
499 dma_addr_t buf_pa;
500 int i;
502 for (i = 0; i < MWIFIEX_MAX_EVT_BD; i++) {
503 /* Allocate skb here so that firmware can DMA data from it */
504 skb = dev_alloc_skb(MAX_EVENT_SIZE);
505 if (!skb) {
506 dev_err(adapter->dev,
507 "Unable to allocate skb for EVENT buf.\n");
508 kfree(card->evtbd_ring_vbase);
509 return -ENOMEM;
511 skb_put(skb, MAX_EVENT_SIZE);
513 if (mwifiex_map_pci_memory(adapter, skb, MAX_EVENT_SIZE,
514 PCI_DMA_FROMDEVICE))
515 return -1;
517 MWIFIEX_SKB_PACB(skb, &buf_pa);
519 dev_dbg(adapter->dev,
520 "info: EVT ring: skb=%p len=%d data=%p buf_pa=%#x:%x\n",
521 skb, skb->len, skb->data, (u32)buf_pa,
522 (u32)((u64)buf_pa >> 32));
524 card->evt_buf_list[i] = skb;
525 card->evtbd_ring[i] = (void *)(card->evtbd_ring_vbase +
526 (sizeof(*desc) * i));
527 desc = card->evtbd_ring[i];
528 desc->paddr = buf_pa;
529 desc->len = (u16)skb->len;
530 desc->flags = 0;
533 return 0;
536 /* This function cleans up TX buffer rings. If any of the buffer list has valid
537 * SKB address, associated SKB is freed.
539 static void mwifiex_cleanup_txq_ring(struct mwifiex_adapter *adapter)
541 struct pcie_service_card *card = adapter->card;
542 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
543 struct sk_buff *skb;
544 struct mwifiex_pcie_buf_desc *desc;
545 struct mwifiex_pfu_buf_desc *desc2;
546 int i;
548 for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
549 if (reg->pfu_enabled) {
550 desc2 = card->txbd_ring[i];
551 if (card->tx_buf_list[i]) {
552 skb = card->tx_buf_list[i];
553 pci_unmap_single(card->dev, desc2->paddr,
554 skb->len, PCI_DMA_TODEVICE);
555 dev_kfree_skb_any(skb);
557 memset(desc2, 0, sizeof(*desc2));
558 } else {
559 desc = card->txbd_ring[i];
560 if (card->tx_buf_list[i]) {
561 skb = card->tx_buf_list[i];
562 pci_unmap_single(card->dev, desc->paddr,
563 skb->len, PCI_DMA_TODEVICE);
564 dev_kfree_skb_any(skb);
566 memset(desc, 0, sizeof(*desc));
568 card->tx_buf_list[i] = NULL;
571 return;
574 /* This function cleans up RX buffer rings. If any of the buffer list has valid
575 * SKB address, associated SKB is freed.
577 static void mwifiex_cleanup_rxq_ring(struct mwifiex_adapter *adapter)
579 struct pcie_service_card *card = adapter->card;
580 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
581 struct mwifiex_pcie_buf_desc *desc;
582 struct mwifiex_pfu_buf_desc *desc2;
583 struct sk_buff *skb;
584 int i;
586 for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
587 if (reg->pfu_enabled) {
588 desc2 = card->rxbd_ring[i];
589 if (card->rx_buf_list[i]) {
590 skb = card->rx_buf_list[i];
591 pci_unmap_single(card->dev, desc2->paddr,
592 skb->len, PCI_DMA_FROMDEVICE);
593 dev_kfree_skb_any(skb);
595 memset(desc2, 0, sizeof(*desc2));
596 } else {
597 desc = card->rxbd_ring[i];
598 if (card->rx_buf_list[i]) {
599 skb = card->rx_buf_list[i];
600 pci_unmap_single(card->dev, desc->paddr,
601 skb->len, PCI_DMA_FROMDEVICE);
602 dev_kfree_skb_any(skb);
604 memset(desc, 0, sizeof(*desc));
606 card->rx_buf_list[i] = NULL;
609 return;
612 /* This function cleans up event buffer rings. If any of the buffer list has
613 * valid SKB address, associated SKB is freed.
615 static void mwifiex_cleanup_evt_ring(struct mwifiex_adapter *adapter)
617 struct pcie_service_card *card = adapter->card;
618 struct mwifiex_evt_buf_desc *desc;
619 struct sk_buff *skb;
620 int i;
622 for (i = 0; i < MWIFIEX_MAX_EVT_BD; i++) {
623 desc = card->evtbd_ring[i];
624 if (card->evt_buf_list[i]) {
625 skb = card->evt_buf_list[i];
626 pci_unmap_single(card->dev, desc->paddr, MAX_EVENT_SIZE,
627 PCI_DMA_FROMDEVICE);
628 dev_kfree_skb_any(skb);
630 card->evt_buf_list[i] = NULL;
631 memset(desc, 0, sizeof(*desc));
634 return;
637 /* This function creates buffer descriptor ring for TX
639 static int mwifiex_pcie_create_txbd_ring(struct mwifiex_adapter *adapter)
641 struct pcie_service_card *card = adapter->card;
642 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
645 * driver maintaines the write pointer and firmware maintaines the read
646 * pointer. The write pointer starts at 0 (zero) while the read pointer
647 * starts at zero with rollover bit set
649 card->txbd_wrptr = 0;
651 if (reg->pfu_enabled)
652 card->txbd_rdptr = 0;
653 else
654 card->txbd_rdptr |= reg->tx_rollover_ind;
656 /* allocate shared memory for the BD ring and divide the same in to
657 several descriptors */
658 if (reg->pfu_enabled)
659 card->txbd_ring_size = sizeof(struct mwifiex_pfu_buf_desc) *
660 MWIFIEX_MAX_TXRX_BD;
661 else
662 card->txbd_ring_size = sizeof(struct mwifiex_pcie_buf_desc) *
663 MWIFIEX_MAX_TXRX_BD;
665 dev_dbg(adapter->dev, "info: txbd_ring: Allocating %d bytes\n",
666 card->txbd_ring_size);
667 card->txbd_ring_vbase = pci_alloc_consistent(card->dev,
668 card->txbd_ring_size,
669 &card->txbd_ring_pbase);
670 if (!card->txbd_ring_vbase) {
671 dev_err(adapter->dev,
672 "allocate consistent memory (%d bytes) failed!\n",
673 card->txbd_ring_size);
674 return -ENOMEM;
676 dev_dbg(adapter->dev,
677 "info: txbd_ring - base: %p, pbase: %#x:%x, len: %x\n",
678 card->txbd_ring_vbase, (unsigned int)card->txbd_ring_pbase,
679 (u32)((u64)card->txbd_ring_pbase >> 32), card->txbd_ring_size);
681 return mwifiex_init_txq_ring(adapter);
684 static int mwifiex_pcie_delete_txbd_ring(struct mwifiex_adapter *adapter)
686 struct pcie_service_card *card = adapter->card;
687 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
689 mwifiex_cleanup_txq_ring(adapter);
691 if (card->txbd_ring_vbase)
692 pci_free_consistent(card->dev, card->txbd_ring_size,
693 card->txbd_ring_vbase,
694 card->txbd_ring_pbase);
695 card->txbd_ring_size = 0;
696 card->txbd_wrptr = 0;
697 card->txbd_rdptr = 0 | reg->tx_rollover_ind;
698 card->txbd_ring_vbase = NULL;
699 card->txbd_ring_pbase = 0;
701 return 0;
705 * This function creates buffer descriptor ring for RX
707 static int mwifiex_pcie_create_rxbd_ring(struct mwifiex_adapter *adapter)
709 struct pcie_service_card *card = adapter->card;
710 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
713 * driver maintaines the read pointer and firmware maintaines the write
714 * pointer. The write pointer starts at 0 (zero) while the read pointer
715 * starts at zero with rollover bit set
717 card->rxbd_wrptr = 0;
718 card->rxbd_rdptr = reg->rx_rollover_ind;
720 if (reg->pfu_enabled)
721 card->rxbd_ring_size = sizeof(struct mwifiex_pfu_buf_desc) *
722 MWIFIEX_MAX_TXRX_BD;
723 else
724 card->rxbd_ring_size = sizeof(struct mwifiex_pcie_buf_desc) *
725 MWIFIEX_MAX_TXRX_BD;
727 dev_dbg(adapter->dev, "info: rxbd_ring: Allocating %d bytes\n",
728 card->rxbd_ring_size);
729 card->rxbd_ring_vbase = pci_alloc_consistent(card->dev,
730 card->rxbd_ring_size,
731 &card->rxbd_ring_pbase);
732 if (!card->rxbd_ring_vbase) {
733 dev_err(adapter->dev,
734 "allocate consistent memory (%d bytes) failed!\n",
735 card->rxbd_ring_size);
736 return -ENOMEM;
739 dev_dbg(adapter->dev,
740 "info: rxbd_ring - base: %p, pbase: %#x:%x, len: %#x\n",
741 card->rxbd_ring_vbase, (u32)card->rxbd_ring_pbase,
742 (u32)((u64)card->rxbd_ring_pbase >> 32),
743 card->rxbd_ring_size);
745 return mwifiex_init_rxq_ring(adapter);
749 * This function deletes Buffer descriptor ring for RX
751 static int mwifiex_pcie_delete_rxbd_ring(struct mwifiex_adapter *adapter)
753 struct pcie_service_card *card = adapter->card;
754 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
756 mwifiex_cleanup_rxq_ring(adapter);
758 if (card->rxbd_ring_vbase)
759 pci_free_consistent(card->dev, card->rxbd_ring_size,
760 card->rxbd_ring_vbase,
761 card->rxbd_ring_pbase);
762 card->rxbd_ring_size = 0;
763 card->rxbd_wrptr = 0;
764 card->rxbd_rdptr = 0 | reg->rx_rollover_ind;
765 card->rxbd_ring_vbase = NULL;
766 card->rxbd_ring_pbase = 0;
768 return 0;
772 * This function creates buffer descriptor ring for Events
774 static int mwifiex_pcie_create_evtbd_ring(struct mwifiex_adapter *adapter)
776 struct pcie_service_card *card = adapter->card;
777 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
780 * driver maintaines the read pointer and firmware maintaines the write
781 * pointer. The write pointer starts at 0 (zero) while the read pointer
782 * starts at zero with rollover bit set
784 card->evtbd_wrptr = 0;
785 card->evtbd_rdptr = reg->evt_rollover_ind;
787 card->evtbd_ring_size = sizeof(struct mwifiex_evt_buf_desc) *
788 MWIFIEX_MAX_EVT_BD;
790 dev_dbg(adapter->dev, "info: evtbd_ring: Allocating %d bytes\n",
791 card->evtbd_ring_size);
792 card->evtbd_ring_vbase = pci_alloc_consistent(card->dev,
793 card->evtbd_ring_size,
794 &card->evtbd_ring_pbase);
795 if (!card->evtbd_ring_vbase) {
796 dev_err(adapter->dev,
797 "allocate consistent memory (%d bytes) failed!\n",
798 card->evtbd_ring_size);
799 return -ENOMEM;
802 dev_dbg(adapter->dev,
803 "info: CMDRSP/EVT bd_ring - base: %p pbase: %#x:%x len: %#x\n",
804 card->evtbd_ring_vbase, (u32)card->evtbd_ring_pbase,
805 (u32)((u64)card->evtbd_ring_pbase >> 32),
806 card->evtbd_ring_size);
808 return mwifiex_pcie_init_evt_ring(adapter);
812 * This function deletes Buffer descriptor ring for Events
814 static int mwifiex_pcie_delete_evtbd_ring(struct mwifiex_adapter *adapter)
816 struct pcie_service_card *card = adapter->card;
817 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
819 mwifiex_cleanup_evt_ring(adapter);
821 if (card->evtbd_ring_vbase)
822 pci_free_consistent(card->dev, card->evtbd_ring_size,
823 card->evtbd_ring_vbase,
824 card->evtbd_ring_pbase);
825 card->evtbd_wrptr = 0;
826 card->evtbd_rdptr = 0 | reg->evt_rollover_ind;
827 card->evtbd_ring_size = 0;
828 card->evtbd_ring_vbase = NULL;
829 card->evtbd_ring_pbase = 0;
831 return 0;
835 * This function allocates a buffer for CMDRSP
837 static int mwifiex_pcie_alloc_cmdrsp_buf(struct mwifiex_adapter *adapter)
839 struct pcie_service_card *card = adapter->card;
840 struct sk_buff *skb;
842 /* Allocate memory for receiving command response data */
843 skb = dev_alloc_skb(MWIFIEX_UPLD_SIZE);
844 if (!skb) {
845 dev_err(adapter->dev,
846 "Unable to allocate skb for command response data.\n");
847 return -ENOMEM;
849 skb_put(skb, MWIFIEX_UPLD_SIZE);
850 if (mwifiex_map_pci_memory(adapter, skb, MWIFIEX_UPLD_SIZE,
851 PCI_DMA_FROMDEVICE))
852 return -1;
854 card->cmdrsp_buf = skb;
856 return 0;
860 * This function deletes a buffer for CMDRSP
862 static int mwifiex_pcie_delete_cmdrsp_buf(struct mwifiex_adapter *adapter)
864 struct pcie_service_card *card;
865 dma_addr_t buf_pa;
867 if (!adapter)
868 return 0;
870 card = adapter->card;
872 if (card && card->cmdrsp_buf) {
873 MWIFIEX_SKB_PACB(card->cmdrsp_buf, &buf_pa);
874 pci_unmap_single(card->dev, buf_pa, MWIFIEX_UPLD_SIZE,
875 PCI_DMA_FROMDEVICE);
876 dev_kfree_skb_any(card->cmdrsp_buf);
879 if (card && card->cmd_buf) {
880 MWIFIEX_SKB_PACB(card->cmd_buf, &buf_pa);
881 pci_unmap_single(card->dev, buf_pa, card->cmd_buf->len,
882 PCI_DMA_TODEVICE);
884 return 0;
888 * This function allocates a buffer for sleep cookie
890 static int mwifiex_pcie_alloc_sleep_cookie_buf(struct mwifiex_adapter *adapter)
892 struct pcie_service_card *card = adapter->card;
894 card->sleep_cookie_vbase = pci_alloc_consistent(card->dev, sizeof(u32),
895 &card->sleep_cookie_pbase);
896 if (!card->sleep_cookie_vbase) {
897 dev_err(adapter->dev, "pci_alloc_consistent failed!\n");
898 return -ENOMEM;
900 /* Init val of Sleep Cookie */
901 *(u32 *)card->sleep_cookie_vbase = FW_AWAKE_COOKIE;
903 dev_dbg(adapter->dev, "alloc_scook: sleep cookie=0x%x\n",
904 *((u32 *)card->sleep_cookie_vbase));
906 return 0;
910 * This function deletes buffer for sleep cookie
912 static int mwifiex_pcie_delete_sleep_cookie_buf(struct mwifiex_adapter *adapter)
914 struct pcie_service_card *card;
916 if (!adapter)
917 return 0;
919 card = adapter->card;
921 if (card && card->sleep_cookie_vbase) {
922 pci_free_consistent(card->dev, sizeof(u32),
923 card->sleep_cookie_vbase,
924 card->sleep_cookie_pbase);
925 card->sleep_cookie_vbase = NULL;
928 return 0;
931 /* This function flushes the TX buffer descriptor ring
932 * This function defined as handler is also called while cleaning TXRX
933 * during disconnect/ bss stop.
935 static int mwifiex_clean_pcie_ring_buf(struct mwifiex_adapter *adapter)
937 struct pcie_service_card *card = adapter->card;
939 if (!mwifiex_pcie_txbd_empty(card, card->txbd_rdptr)) {
940 card->txbd_flush = 1;
941 /* write pointer already set at last send
942 * send dnld-rdy intr again, wait for completion.
944 if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
945 CPU_INTR_DNLD_RDY)) {
946 dev_err(adapter->dev,
947 "failed to assert dnld-rdy interrupt.\n");
948 return -1;
951 return 0;
955 * This function unmaps and frees downloaded data buffer
957 static int mwifiex_pcie_send_data_complete(struct mwifiex_adapter *adapter)
959 struct sk_buff *skb;
960 dma_addr_t buf_pa;
961 u32 wrdoneidx, rdptr, num_tx_buffs, unmap_count = 0;
962 struct mwifiex_pcie_buf_desc *desc;
963 struct mwifiex_pfu_buf_desc *desc2;
964 struct pcie_service_card *card = adapter->card;
965 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
967 if (!mwifiex_pcie_ok_to_access_hw(adapter))
968 mwifiex_pm_wakeup_card(adapter);
970 /* Read the TX ring read pointer set by firmware */
971 if (mwifiex_read_reg(adapter, reg->tx_rdptr, &rdptr)) {
972 dev_err(adapter->dev,
973 "SEND COMP: failed to read reg->tx_rdptr\n");
974 return -1;
977 dev_dbg(adapter->dev, "SEND COMP: rdptr_prev=0x%x, rdptr=0x%x\n",
978 card->txbd_rdptr, rdptr);
980 num_tx_buffs = MWIFIEX_MAX_TXRX_BD << reg->tx_start_ptr;
981 /* free from previous txbd_rdptr to current txbd_rdptr */
982 while (((card->txbd_rdptr & reg->tx_mask) !=
983 (rdptr & reg->tx_mask)) ||
984 ((card->txbd_rdptr & reg->tx_rollover_ind) !=
985 (rdptr & reg->tx_rollover_ind))) {
986 wrdoneidx = (card->txbd_rdptr & reg->tx_mask) >>
987 reg->tx_start_ptr;
989 skb = card->tx_buf_list[wrdoneidx];
990 if (skb) {
991 dev_dbg(adapter->dev,
992 "SEND COMP: Detach skb %p at txbd_rdidx=%d\n",
993 skb, wrdoneidx);
994 MWIFIEX_SKB_PACB(skb, &buf_pa);
995 pci_unmap_single(card->dev, buf_pa, skb->len,
996 PCI_DMA_TODEVICE);
998 unmap_count++;
1000 if (card->txbd_flush)
1001 mwifiex_write_data_complete(adapter, skb, 0,
1002 -1);
1003 else
1004 mwifiex_write_data_complete(adapter, skb, 0, 0);
1007 card->tx_buf_list[wrdoneidx] = NULL;
1009 if (reg->pfu_enabled) {
1010 desc2 = (void *)card->txbd_ring[wrdoneidx];
1011 memset(desc2, 0, sizeof(*desc2));
1012 } else {
1013 desc = card->txbd_ring[wrdoneidx];
1014 memset(desc, 0, sizeof(*desc));
1016 switch (card->dev->device) {
1017 case PCIE_DEVICE_ID_MARVELL_88W8766P:
1018 card->txbd_rdptr++;
1019 break;
1020 case PCIE_DEVICE_ID_MARVELL_88W8897:
1021 card->txbd_rdptr += reg->ring_tx_start_ptr;
1022 break;
1026 if ((card->txbd_rdptr & reg->tx_mask) == num_tx_buffs)
1027 card->txbd_rdptr = ((card->txbd_rdptr &
1028 reg->tx_rollover_ind) ^
1029 reg->tx_rollover_ind);
1032 if (unmap_count)
1033 adapter->data_sent = false;
1035 if (card->txbd_flush) {
1036 if (mwifiex_pcie_txbd_empty(card, card->txbd_rdptr))
1037 card->txbd_flush = 0;
1038 else
1039 mwifiex_clean_pcie_ring_buf(adapter);
1042 return 0;
1045 /* This function sends data buffer to device. First 4 bytes of payload
1046 * are filled with payload length and payload type. Then this payload
1047 * is mapped to PCI device memory. Tx ring pointers are advanced accordingly.
1048 * Download ready interrupt to FW is deffered if Tx ring is not full and
1049 * additional payload can be accomodated.
1051 static int
1052 mwifiex_pcie_send_data(struct mwifiex_adapter *adapter, struct sk_buff *skb,
1053 struct mwifiex_tx_param *tx_param)
1055 struct pcie_service_card *card = adapter->card;
1056 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1057 u32 wrindx, num_tx_buffs, rx_val;
1058 int ret;
1059 dma_addr_t buf_pa;
1060 struct mwifiex_pcie_buf_desc *desc = NULL;
1061 struct mwifiex_pfu_buf_desc *desc2 = NULL;
1062 __le16 *tmp;
1064 if (!(skb->data && skb->len)) {
1065 dev_err(adapter->dev, "%s(): invalid parameter <%p, %#x>\n",
1066 __func__, skb->data, skb->len);
1067 return -1;
1070 if (!mwifiex_pcie_ok_to_access_hw(adapter))
1071 mwifiex_pm_wakeup_card(adapter);
1073 num_tx_buffs = MWIFIEX_MAX_TXRX_BD << reg->tx_start_ptr;
1074 dev_dbg(adapter->dev, "info: SEND DATA: <Rd: %#x, Wr: %#x>\n",
1075 card->txbd_rdptr, card->txbd_wrptr);
1076 if (mwifiex_pcie_txbd_not_full(card)) {
1077 u8 *payload;
1079 adapter->data_sent = true;
1080 payload = skb->data;
1081 tmp = (__le16 *)&payload[0];
1082 *tmp = cpu_to_le16((u16)skb->len);
1083 tmp = (__le16 *)&payload[2];
1084 *tmp = cpu_to_le16(MWIFIEX_TYPE_DATA);
1086 if (mwifiex_map_pci_memory(adapter, skb, skb->len ,
1087 PCI_DMA_TODEVICE))
1088 return -1;
1090 wrindx = (card->txbd_wrptr & reg->tx_mask) >> reg->tx_start_ptr;
1091 MWIFIEX_SKB_PACB(skb, &buf_pa);
1092 card->tx_buf_list[wrindx] = skb;
1094 if (reg->pfu_enabled) {
1095 desc2 = (void *)card->txbd_ring[wrindx];
1096 desc2->paddr = buf_pa;
1097 desc2->len = (u16)skb->len;
1098 desc2->frag_len = (u16)skb->len;
1099 desc2->offset = 0;
1100 desc2->flags = MWIFIEX_BD_FLAG_FIRST_DESC |
1101 MWIFIEX_BD_FLAG_LAST_DESC;
1102 } else {
1103 desc = card->txbd_ring[wrindx];
1104 desc->paddr = buf_pa;
1105 desc->len = (u16)skb->len;
1106 desc->flags = MWIFIEX_BD_FLAG_FIRST_DESC |
1107 MWIFIEX_BD_FLAG_LAST_DESC;
1110 switch (card->dev->device) {
1111 case PCIE_DEVICE_ID_MARVELL_88W8766P:
1112 card->txbd_wrptr++;
1113 break;
1114 case PCIE_DEVICE_ID_MARVELL_88W8897:
1115 card->txbd_wrptr += reg->ring_tx_start_ptr;
1116 break;
1119 if ((card->txbd_wrptr & reg->tx_mask) == num_tx_buffs)
1120 card->txbd_wrptr = ((card->txbd_wrptr &
1121 reg->tx_rollover_ind) ^
1122 reg->tx_rollover_ind);
1124 rx_val = card->rxbd_rdptr & reg->rx_wrap_mask;
1125 /* Write the TX ring write pointer in to reg->tx_wrptr */
1126 if (mwifiex_write_reg(adapter, reg->tx_wrptr,
1127 card->txbd_wrptr | rx_val)) {
1128 dev_err(adapter->dev,
1129 "SEND DATA: failed to write reg->tx_wrptr\n");
1130 ret = -1;
1131 goto done_unmap;
1133 if ((mwifiex_pcie_txbd_not_full(card)) &&
1134 tx_param->next_pkt_len) {
1135 /* have more packets and TxBD still can hold more */
1136 dev_dbg(adapter->dev,
1137 "SEND DATA: delay dnld-rdy interrupt.\n");
1138 adapter->data_sent = false;
1139 } else {
1140 /* Send the TX ready interrupt */
1141 if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
1142 CPU_INTR_DNLD_RDY)) {
1143 dev_err(adapter->dev,
1144 "SEND DATA: failed to assert dnld-rdy interrupt.\n");
1145 ret = -1;
1146 goto done_unmap;
1149 dev_dbg(adapter->dev, "info: SEND DATA: Updated <Rd: %#x, Wr: "
1150 "%#x> and sent packet to firmware successfully\n",
1151 card->txbd_rdptr, card->txbd_wrptr);
1152 } else {
1153 dev_dbg(adapter->dev,
1154 "info: TX Ring full, can't send packets to fw\n");
1155 adapter->data_sent = true;
1156 /* Send the TX ready interrupt */
1157 if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
1158 CPU_INTR_DNLD_RDY))
1159 dev_err(adapter->dev,
1160 "SEND DATA: failed to assert door-bell intr\n");
1161 return -EBUSY;
1164 return -EINPROGRESS;
1165 done_unmap:
1166 MWIFIEX_SKB_PACB(skb, &buf_pa);
1167 pci_unmap_single(card->dev, buf_pa, skb->len, PCI_DMA_TODEVICE);
1168 card->tx_buf_list[wrindx] = NULL;
1169 if (reg->pfu_enabled)
1170 memset(desc2, 0, sizeof(*desc2));
1171 else
1172 memset(desc, 0, sizeof(*desc));
1174 return ret;
1178 * This function handles received buffer ring and
1179 * dispatches packets to upper
1181 static int mwifiex_pcie_process_recv_data(struct mwifiex_adapter *adapter)
1183 struct pcie_service_card *card = adapter->card;
1184 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1185 u32 wrptr, rd_index, tx_val;
1186 dma_addr_t buf_pa;
1187 int ret = 0;
1188 struct sk_buff *skb_tmp = NULL;
1189 struct mwifiex_pcie_buf_desc *desc;
1190 struct mwifiex_pfu_buf_desc *desc2;
1192 if (!mwifiex_pcie_ok_to_access_hw(adapter))
1193 mwifiex_pm_wakeup_card(adapter);
1195 /* Read the RX ring Write pointer set by firmware */
1196 if (mwifiex_read_reg(adapter, reg->rx_wrptr, &wrptr)) {
1197 dev_err(adapter->dev,
1198 "RECV DATA: failed to read reg->rx_wrptr\n");
1199 ret = -1;
1200 goto done;
1202 card->rxbd_wrptr = wrptr;
1204 while (((wrptr & reg->rx_mask) !=
1205 (card->rxbd_rdptr & reg->rx_mask)) ||
1206 ((wrptr & reg->rx_rollover_ind) ==
1207 (card->rxbd_rdptr & reg->rx_rollover_ind))) {
1208 struct sk_buff *skb_data;
1209 u16 rx_len;
1210 __le16 pkt_len;
1212 rd_index = card->rxbd_rdptr & reg->rx_mask;
1213 skb_data = card->rx_buf_list[rd_index];
1215 /* If skb allocation was failed earlier for Rx packet,
1216 * rx_buf_list[rd_index] would have been left with a NULL.
1218 if (!skb_data)
1219 return -ENOMEM;
1221 MWIFIEX_SKB_PACB(skb_data, &buf_pa);
1222 pci_unmap_single(card->dev, buf_pa, MWIFIEX_RX_DATA_BUF_SIZE,
1223 PCI_DMA_FROMDEVICE);
1224 card->rx_buf_list[rd_index] = NULL;
1226 /* Get data length from interface header -
1227 * first 2 bytes for len, next 2 bytes is for type
1229 pkt_len = *((__le16 *)skb_data->data);
1230 rx_len = le16_to_cpu(pkt_len);
1231 skb_put(skb_data, rx_len);
1232 dev_dbg(adapter->dev,
1233 "info: RECV DATA: Rd=%#x, Wr=%#x, Len=%d\n",
1234 card->rxbd_rdptr, wrptr, rx_len);
1235 skb_pull(skb_data, INTF_HEADER_LEN);
1236 mwifiex_handle_rx_packet(adapter, skb_data);
1238 skb_tmp = dev_alloc_skb(MWIFIEX_RX_DATA_BUF_SIZE);
1239 if (!skb_tmp) {
1240 dev_err(adapter->dev,
1241 "Unable to allocate skb.\n");
1242 return -ENOMEM;
1245 if (mwifiex_map_pci_memory(adapter, skb_tmp,
1246 MWIFIEX_RX_DATA_BUF_SIZE,
1247 PCI_DMA_FROMDEVICE))
1248 return -1;
1250 MWIFIEX_SKB_PACB(skb_tmp, &buf_pa);
1252 dev_dbg(adapter->dev,
1253 "RECV DATA: Attach new sk_buff %p at rxbd_rdidx=%d\n",
1254 skb_tmp, rd_index);
1255 card->rx_buf_list[rd_index] = skb_tmp;
1257 if (reg->pfu_enabled) {
1258 desc2 = (void *)card->rxbd_ring[rd_index];
1259 desc2->paddr = buf_pa;
1260 desc2->len = skb_tmp->len;
1261 desc2->frag_len = skb_tmp->len;
1262 desc2->offset = 0;
1263 desc2->flags = reg->ring_flag_sop | reg->ring_flag_eop;
1264 } else {
1265 desc = card->rxbd_ring[rd_index];
1266 desc->paddr = buf_pa;
1267 desc->len = skb_tmp->len;
1268 desc->flags = 0;
1271 if ((++card->rxbd_rdptr & reg->rx_mask) ==
1272 MWIFIEX_MAX_TXRX_BD) {
1273 card->rxbd_rdptr = ((card->rxbd_rdptr &
1274 reg->rx_rollover_ind) ^
1275 reg->rx_rollover_ind);
1277 dev_dbg(adapter->dev, "info: RECV DATA: <Rd: %#x, Wr: %#x>\n",
1278 card->rxbd_rdptr, wrptr);
1280 tx_val = card->txbd_wrptr & reg->tx_wrap_mask;
1281 /* Write the RX ring read pointer in to reg->rx_rdptr */
1282 if (mwifiex_write_reg(adapter, reg->rx_rdptr,
1283 card->rxbd_rdptr | tx_val)) {
1284 dev_err(adapter->dev,
1285 "RECV DATA: failed to write reg->rx_rdptr\n");
1286 ret = -1;
1287 goto done;
1290 /* Read the RX ring Write pointer set by firmware */
1291 if (mwifiex_read_reg(adapter, reg->rx_wrptr, &wrptr)) {
1292 dev_err(adapter->dev,
1293 "RECV DATA: failed to read reg->rx_wrptr\n");
1294 ret = -1;
1295 goto done;
1297 dev_dbg(adapter->dev,
1298 "info: RECV DATA: Rcvd packet from fw successfully\n");
1299 card->rxbd_wrptr = wrptr;
1302 done:
1303 return ret;
1307 * This function downloads the boot command to device
1309 static int
1310 mwifiex_pcie_send_boot_cmd(struct mwifiex_adapter *adapter, struct sk_buff *skb)
1312 dma_addr_t buf_pa;
1313 struct pcie_service_card *card = adapter->card;
1314 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1316 if (!(skb->data && skb->len)) {
1317 dev_err(adapter->dev,
1318 "Invalid parameter in %s <%p. len %d>\n",
1319 __func__, skb->data, skb->len);
1320 return -1;
1323 if (mwifiex_map_pci_memory(adapter, skb, skb->len , PCI_DMA_TODEVICE))
1324 return -1;
1326 MWIFIEX_SKB_PACB(skb, &buf_pa);
1328 /* Write the lower 32bits of the physical address to low command
1329 * address scratch register
1331 if (mwifiex_write_reg(adapter, reg->cmd_addr_lo, (u32)buf_pa)) {
1332 dev_err(adapter->dev,
1333 "%s: failed to write download command to boot code.\n",
1334 __func__);
1335 pci_unmap_single(card->dev, buf_pa, MWIFIEX_UPLD_SIZE,
1336 PCI_DMA_TODEVICE);
1337 return -1;
1340 /* Write the upper 32bits of the physical address to high command
1341 * address scratch register
1343 if (mwifiex_write_reg(adapter, reg->cmd_addr_hi,
1344 (u32)((u64)buf_pa >> 32))) {
1345 dev_err(adapter->dev,
1346 "%s: failed to write download command to boot code.\n",
1347 __func__);
1348 pci_unmap_single(card->dev, buf_pa, MWIFIEX_UPLD_SIZE,
1349 PCI_DMA_TODEVICE);
1350 return -1;
1353 /* Write the command length to cmd_size scratch register */
1354 if (mwifiex_write_reg(adapter, reg->cmd_size, skb->len)) {
1355 dev_err(adapter->dev,
1356 "%s: failed to write command len to cmd_size scratch reg\n",
1357 __func__);
1358 pci_unmap_single(card->dev, buf_pa, MWIFIEX_UPLD_SIZE,
1359 PCI_DMA_TODEVICE);
1360 return -1;
1363 /* Ring the door bell */
1364 if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
1365 CPU_INTR_DOOR_BELL)) {
1366 dev_err(adapter->dev,
1367 "%s: failed to assert door-bell intr\n", __func__);
1368 pci_unmap_single(card->dev, buf_pa,
1369 MWIFIEX_UPLD_SIZE, PCI_DMA_TODEVICE);
1370 return -1;
1373 return 0;
1376 /* This function init rx port in firmware which in turn enables to receive data
1377 * from device before transmitting any packet.
1379 static int mwifiex_pcie_init_fw_port(struct mwifiex_adapter *adapter)
1381 struct pcie_service_card *card = adapter->card;
1382 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1383 int tx_wrap = card->txbd_wrptr & reg->tx_wrap_mask;
1385 /* Write the RX ring read pointer in to reg->rx_rdptr */
1386 if (mwifiex_write_reg(adapter, reg->rx_rdptr, card->rxbd_rdptr |
1387 tx_wrap)) {
1388 dev_err(adapter->dev,
1389 "RECV DATA: failed to write reg->rx_rdptr\n");
1390 return -1;
1392 return 0;
1395 /* This function downloads commands to the device
1397 static int
1398 mwifiex_pcie_send_cmd(struct mwifiex_adapter *adapter, struct sk_buff *skb)
1400 struct pcie_service_card *card = adapter->card;
1401 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1402 int ret = 0;
1403 dma_addr_t cmd_buf_pa, cmdrsp_buf_pa;
1404 u8 *payload = (u8 *)skb->data;
1406 if (!(skb->data && skb->len)) {
1407 dev_err(adapter->dev, "Invalid parameter in %s <%p, %#x>\n",
1408 __func__, skb->data, skb->len);
1409 return -1;
1412 /* Make sure a command response buffer is available */
1413 if (!card->cmdrsp_buf) {
1414 dev_err(adapter->dev,
1415 "No response buffer available, send command failed\n");
1416 return -EBUSY;
1419 if (!mwifiex_pcie_ok_to_access_hw(adapter))
1420 mwifiex_pm_wakeup_card(adapter);
1422 adapter->cmd_sent = true;
1424 *(__le16 *)&payload[0] = cpu_to_le16((u16)skb->len);
1425 *(__le16 *)&payload[2] = cpu_to_le16(MWIFIEX_TYPE_CMD);
1427 if (mwifiex_map_pci_memory(adapter, skb, skb->len, PCI_DMA_TODEVICE))
1428 return -1;
1430 card->cmd_buf = skb;
1432 /* To send a command, the driver will:
1433 1. Write the 64bit physical address of the data buffer to
1434 cmd response address low + cmd response address high
1435 2. Ring the door bell (i.e. set the door bell interrupt)
1437 In response to door bell interrupt, the firmware will perform
1438 the DMA of the command packet (first header to obtain the total
1439 length and then rest of the command).
1442 if (card->cmdrsp_buf) {
1443 MWIFIEX_SKB_PACB(card->cmdrsp_buf, &cmdrsp_buf_pa);
1444 /* Write the lower 32bits of the cmdrsp buffer physical
1445 address */
1446 if (mwifiex_write_reg(adapter, reg->cmdrsp_addr_lo,
1447 (u32)cmdrsp_buf_pa)) {
1448 dev_err(adapter->dev,
1449 "Failed to write download cmd to boot code.\n");
1450 ret = -1;
1451 goto done;
1453 /* Write the upper 32bits of the cmdrsp buffer physical
1454 address */
1455 if (mwifiex_write_reg(adapter, reg->cmdrsp_addr_hi,
1456 (u32)((u64)cmdrsp_buf_pa >> 32))) {
1457 dev_err(adapter->dev,
1458 "Failed to write download cmd to boot code.\n");
1459 ret = -1;
1460 goto done;
1464 MWIFIEX_SKB_PACB(card->cmd_buf, &cmd_buf_pa);
1465 /* Write the lower 32bits of the physical address to reg->cmd_addr_lo */
1466 if (mwifiex_write_reg(adapter, reg->cmd_addr_lo,
1467 (u32)cmd_buf_pa)) {
1468 dev_err(adapter->dev,
1469 "Failed to write download cmd to boot code.\n");
1470 ret = -1;
1471 goto done;
1473 /* Write the upper 32bits of the physical address to reg->cmd_addr_hi */
1474 if (mwifiex_write_reg(adapter, reg->cmd_addr_hi,
1475 (u32)((u64)cmd_buf_pa >> 32))) {
1476 dev_err(adapter->dev,
1477 "Failed to write download cmd to boot code.\n");
1478 ret = -1;
1479 goto done;
1482 /* Write the command length to reg->cmd_size */
1483 if (mwifiex_write_reg(adapter, reg->cmd_size,
1484 card->cmd_buf->len)) {
1485 dev_err(adapter->dev,
1486 "Failed to write cmd len to reg->cmd_size\n");
1487 ret = -1;
1488 goto done;
1491 /* Ring the door bell */
1492 if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
1493 CPU_INTR_DOOR_BELL)) {
1494 dev_err(adapter->dev,
1495 "Failed to assert door-bell intr\n");
1496 ret = -1;
1497 goto done;
1500 done:
1501 if (ret)
1502 adapter->cmd_sent = false;
1504 return 0;
1508 * This function handles command complete interrupt
1510 static int mwifiex_pcie_process_cmd_complete(struct mwifiex_adapter *adapter)
1512 struct pcie_service_card *card = adapter->card;
1513 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1514 struct sk_buff *skb = card->cmdrsp_buf;
1515 int count = 0;
1516 u16 rx_len;
1517 __le16 pkt_len;
1518 dma_addr_t buf_pa;
1520 dev_dbg(adapter->dev, "info: Rx CMD Response\n");
1522 MWIFIEX_SKB_PACB(skb, &buf_pa);
1523 pci_unmap_single(card->dev, buf_pa, MWIFIEX_UPLD_SIZE,
1524 PCI_DMA_FROMDEVICE);
1526 pkt_len = *((__le16 *)skb->data);
1527 rx_len = le16_to_cpu(pkt_len);
1528 skb_trim(skb, rx_len);
1529 skb_pull(skb, INTF_HEADER_LEN);
1531 if (!adapter->curr_cmd) {
1532 if (adapter->ps_state == PS_STATE_SLEEP_CFM) {
1533 mwifiex_process_sleep_confirm_resp(adapter, skb->data,
1534 skb->len);
1535 mwifiex_pcie_enable_host_int(adapter);
1536 if (mwifiex_write_reg(adapter,
1537 PCIE_CPU_INT_EVENT,
1538 CPU_INTR_SLEEP_CFM_DONE)) {
1539 dev_warn(adapter->dev,
1540 "Write register failed\n");
1541 return -1;
1543 while (reg->sleep_cookie && (count++ < 10) &&
1544 mwifiex_pcie_ok_to_access_hw(adapter))
1545 usleep_range(50, 60);
1546 } else {
1547 dev_err(adapter->dev,
1548 "There is no command but got cmdrsp\n");
1550 memcpy(adapter->upld_buf, skb->data,
1551 min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER, skb->len));
1552 skb_push(skb, INTF_HEADER_LEN);
1553 if (mwifiex_map_pci_memory(adapter, skb, MWIFIEX_UPLD_SIZE,
1554 PCI_DMA_FROMDEVICE))
1555 return -1;
1557 MWIFIEX_SKB_PACB(skb, &buf_pa);
1558 } else if (mwifiex_pcie_ok_to_access_hw(adapter)) {
1559 adapter->curr_cmd->resp_skb = skb;
1560 adapter->cmd_resp_received = true;
1561 /* Take the pointer and set it to CMD node and will
1562 return in the response complete callback */
1563 card->cmdrsp_buf = NULL;
1565 /* Clear the cmd-rsp buffer address in scratch registers. This
1566 will prevent firmware from writing to the same response
1567 buffer again. */
1568 if (mwifiex_write_reg(adapter, reg->cmdrsp_addr_lo, 0)) {
1569 dev_err(adapter->dev,
1570 "cmd_done: failed to clear cmd_rsp_addr_lo\n");
1571 return -1;
1573 /* Write the upper 32bits of the cmdrsp buffer physical
1574 address */
1575 if (mwifiex_write_reg(adapter, reg->cmdrsp_addr_hi, 0)) {
1576 dev_err(adapter->dev,
1577 "cmd_done: failed to clear cmd_rsp_addr_hi\n");
1578 return -1;
1582 return 0;
1586 * Command Response processing complete handler
1588 static int mwifiex_pcie_cmdrsp_complete(struct mwifiex_adapter *adapter,
1589 struct sk_buff *skb)
1591 struct pcie_service_card *card = adapter->card;
1592 dma_addr_t buf_pa;
1593 struct sk_buff *skb_tmp;
1595 if (skb) {
1596 card->cmdrsp_buf = skb;
1597 skb_push(card->cmdrsp_buf, INTF_HEADER_LEN);
1598 if (mwifiex_map_pci_memory(adapter, skb, MWIFIEX_UPLD_SIZE,
1599 PCI_DMA_FROMDEVICE))
1600 return -1;
1603 skb_tmp = card->cmd_buf;
1604 if (skb_tmp) {
1605 MWIFIEX_SKB_PACB(skb_tmp, &buf_pa);
1606 pci_unmap_single(card->dev, buf_pa, skb_tmp->len,
1607 PCI_DMA_FROMDEVICE);
1608 card->cmd_buf = NULL;
1611 return 0;
1615 * This function handles firmware event ready interrupt
1617 static int mwifiex_pcie_process_event_ready(struct mwifiex_adapter *adapter)
1619 struct pcie_service_card *card = adapter->card;
1620 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1621 u32 rdptr = card->evtbd_rdptr & MWIFIEX_EVTBD_MASK;
1622 u32 wrptr, event;
1623 dma_addr_t buf_pa;
1624 struct mwifiex_evt_buf_desc *desc;
1626 if (!mwifiex_pcie_ok_to_access_hw(adapter))
1627 mwifiex_pm_wakeup_card(adapter);
1629 if (adapter->event_received) {
1630 dev_dbg(adapter->dev, "info: Event being processed, "
1631 "do not process this interrupt just yet\n");
1632 return 0;
1635 if (rdptr >= MWIFIEX_MAX_EVT_BD) {
1636 dev_dbg(adapter->dev, "info: Invalid read pointer...\n");
1637 return -1;
1640 /* Read the event ring write pointer set by firmware */
1641 if (mwifiex_read_reg(adapter, reg->evt_wrptr, &wrptr)) {
1642 dev_err(adapter->dev,
1643 "EventReady: failed to read reg->evt_wrptr\n");
1644 return -1;
1647 dev_dbg(adapter->dev, "info: EventReady: Initial <Rd: 0x%x, Wr: 0x%x>",
1648 card->evtbd_rdptr, wrptr);
1649 if (((wrptr & MWIFIEX_EVTBD_MASK) != (card->evtbd_rdptr
1650 & MWIFIEX_EVTBD_MASK)) ||
1651 ((wrptr & reg->evt_rollover_ind) ==
1652 (card->evtbd_rdptr & reg->evt_rollover_ind))) {
1653 struct sk_buff *skb_cmd;
1654 __le16 data_len = 0;
1655 u16 evt_len;
1657 dev_dbg(adapter->dev, "info: Read Index: %d\n", rdptr);
1658 skb_cmd = card->evt_buf_list[rdptr];
1659 MWIFIEX_SKB_PACB(skb_cmd, &buf_pa);
1660 pci_unmap_single(card->dev, buf_pa, MAX_EVENT_SIZE,
1661 PCI_DMA_FROMDEVICE);
1663 /* Take the pointer and set it to event pointer in adapter
1664 and will return back after event handling callback */
1665 card->evt_buf_list[rdptr] = NULL;
1666 desc = card->evtbd_ring[rdptr];
1667 memset(desc, 0, sizeof(*desc));
1669 event = *(u32 *) &skb_cmd->data[INTF_HEADER_LEN];
1670 adapter->event_cause = event;
1671 /* The first 4bytes will be the event transfer header
1672 len is 2 bytes followed by type which is 2 bytes */
1673 memcpy(&data_len, skb_cmd->data, sizeof(__le16));
1674 evt_len = le16_to_cpu(data_len);
1676 skb_pull(skb_cmd, INTF_HEADER_LEN);
1677 dev_dbg(adapter->dev, "info: Event length: %d\n", evt_len);
1679 if ((evt_len > 0) && (evt_len < MAX_EVENT_SIZE))
1680 memcpy(adapter->event_body, skb_cmd->data +
1681 MWIFIEX_EVENT_HEADER_LEN, evt_len -
1682 MWIFIEX_EVENT_HEADER_LEN);
1684 adapter->event_received = true;
1685 adapter->event_skb = skb_cmd;
1687 /* Do not update the event read pointer here, wait till the
1688 buffer is released. This is just to make things simpler,
1689 we need to find a better method of managing these buffers.
1693 return 0;
1697 * Event processing complete handler
1699 static int mwifiex_pcie_event_complete(struct mwifiex_adapter *adapter,
1700 struct sk_buff *skb)
1702 struct pcie_service_card *card = adapter->card;
1703 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1704 int ret = 0;
1705 u32 rdptr = card->evtbd_rdptr & MWIFIEX_EVTBD_MASK;
1706 u32 wrptr;
1707 dma_addr_t buf_pa;
1708 struct mwifiex_evt_buf_desc *desc;
1710 if (!skb)
1711 return 0;
1713 if (rdptr >= MWIFIEX_MAX_EVT_BD) {
1714 dev_err(adapter->dev, "event_complete: Invalid rdptr 0x%x\n",
1715 rdptr);
1716 return -EINVAL;
1719 /* Read the event ring write pointer set by firmware */
1720 if (mwifiex_read_reg(adapter, reg->evt_wrptr, &wrptr)) {
1721 dev_err(adapter->dev,
1722 "event_complete: failed to read reg->evt_wrptr\n");
1723 return -1;
1726 if (!card->evt_buf_list[rdptr]) {
1727 skb_push(skb, INTF_HEADER_LEN);
1728 if (mwifiex_map_pci_memory(adapter, skb,
1729 MAX_EVENT_SIZE,
1730 PCI_DMA_FROMDEVICE))
1731 return -1;
1732 MWIFIEX_SKB_PACB(skb, &buf_pa);
1733 card->evt_buf_list[rdptr] = skb;
1734 MWIFIEX_SKB_PACB(skb, &buf_pa);
1735 desc = card->evtbd_ring[rdptr];
1736 desc->paddr = buf_pa;
1737 desc->len = (u16)skb->len;
1738 desc->flags = 0;
1739 skb = NULL;
1740 } else {
1741 dev_dbg(adapter->dev,
1742 "info: ERROR: buf still valid at index %d, <%p, %p>\n",
1743 rdptr, card->evt_buf_list[rdptr], skb);
1746 if ((++card->evtbd_rdptr & MWIFIEX_EVTBD_MASK) == MWIFIEX_MAX_EVT_BD) {
1747 card->evtbd_rdptr = ((card->evtbd_rdptr &
1748 reg->evt_rollover_ind) ^
1749 reg->evt_rollover_ind);
1752 dev_dbg(adapter->dev, "info: Updated <Rd: 0x%x, Wr: 0x%x>",
1753 card->evtbd_rdptr, wrptr);
1755 /* Write the event ring read pointer in to reg->evt_rdptr */
1756 if (mwifiex_write_reg(adapter, reg->evt_rdptr,
1757 card->evtbd_rdptr)) {
1758 dev_err(adapter->dev,
1759 "event_complete: failed to read reg->evt_rdptr\n");
1760 return -1;
1763 dev_dbg(adapter->dev, "info: Check Events Again\n");
1764 ret = mwifiex_pcie_process_event_ready(adapter);
1766 return ret;
1770 * This function downloads the firmware to the card.
1772 * Firmware is downloaded to the card in blocks. Every block download
1773 * is tested for CRC errors, and retried a number of times before
1774 * returning failure.
1776 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
1777 struct mwifiex_fw_image *fw)
1779 int ret;
1780 u8 *firmware = fw->fw_buf;
1781 u32 firmware_len = fw->fw_len;
1782 u32 offset = 0;
1783 struct sk_buff *skb;
1784 u32 txlen, tx_blocks = 0, tries, len;
1785 u32 block_retry_cnt = 0;
1786 dma_addr_t buf_pa;
1787 struct pcie_service_card *card = adapter->card;
1788 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1790 if (!firmware || !firmware_len) {
1791 dev_err(adapter->dev,
1792 "No firmware image found! Terminating download\n");
1793 return -1;
1796 dev_dbg(adapter->dev, "info: Downloading FW image (%d bytes)\n",
1797 firmware_len);
1799 if (mwifiex_pcie_disable_host_int(adapter)) {
1800 dev_err(adapter->dev,
1801 "%s: Disabling interrupts failed.\n", __func__);
1802 return -1;
1805 skb = dev_alloc_skb(MWIFIEX_UPLD_SIZE);
1806 if (!skb) {
1807 ret = -ENOMEM;
1808 goto done;
1811 /* Perform firmware data transfer */
1812 do {
1813 u32 ireg_intr = 0;
1815 /* More data? */
1816 if (offset >= firmware_len)
1817 break;
1819 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
1820 ret = mwifiex_read_reg(adapter, reg->cmd_size,
1821 &len);
1822 if (ret) {
1823 dev_warn(adapter->dev,
1824 "Failed reading len from boot code\n");
1825 goto done;
1827 if (len)
1828 break;
1829 usleep_range(10, 20);
1832 if (!len) {
1833 break;
1834 } else if (len > MWIFIEX_UPLD_SIZE) {
1835 pr_err("FW download failure @ %d, invalid length %d\n",
1836 offset, len);
1837 ret = -1;
1838 goto done;
1841 txlen = len;
1843 if (len & BIT(0)) {
1844 block_retry_cnt++;
1845 if (block_retry_cnt > MAX_WRITE_IOMEM_RETRY) {
1846 pr_err("FW download failure @ %d, over max "
1847 "retry count\n", offset);
1848 ret = -1;
1849 goto done;
1851 dev_err(adapter->dev, "FW CRC error indicated by the "
1852 "helper: len = 0x%04X, txlen = %d\n",
1853 len, txlen);
1854 len &= ~BIT(0);
1855 /* Setting this to 0 to resend from same offset */
1856 txlen = 0;
1857 } else {
1858 block_retry_cnt = 0;
1859 /* Set blocksize to transfer - checking for
1860 last block */
1861 if (firmware_len - offset < txlen)
1862 txlen = firmware_len - offset;
1864 dev_dbg(adapter->dev, ".");
1866 tx_blocks = (txlen + card->pcie.blksz_fw_dl - 1) /
1867 card->pcie.blksz_fw_dl;
1869 /* Copy payload to buffer */
1870 memmove(skb->data, &firmware[offset], txlen);
1873 skb_put(skb, MWIFIEX_UPLD_SIZE - skb->len);
1874 skb_trim(skb, tx_blocks * card->pcie.blksz_fw_dl);
1876 /* Send the boot command to device */
1877 if (mwifiex_pcie_send_boot_cmd(adapter, skb)) {
1878 dev_err(adapter->dev,
1879 "Failed to send firmware download command\n");
1880 ret = -1;
1881 goto done;
1884 MWIFIEX_SKB_PACB(skb, &buf_pa);
1886 /* Wait for the command done interrupt */
1887 do {
1888 if (mwifiex_read_reg(adapter, PCIE_CPU_INT_STATUS,
1889 &ireg_intr)) {
1890 dev_err(adapter->dev, "%s: Failed to read "
1891 "interrupt status during fw dnld.\n",
1892 __func__);
1893 pci_unmap_single(card->dev, buf_pa, skb->len,
1894 PCI_DMA_TODEVICE);
1895 ret = -1;
1896 goto done;
1898 } while ((ireg_intr & CPU_INTR_DOOR_BELL) ==
1899 CPU_INTR_DOOR_BELL);
1901 pci_unmap_single(card->dev, buf_pa, skb->len,
1902 PCI_DMA_TODEVICE);
1904 offset += txlen;
1905 } while (true);
1907 dev_dbg(adapter->dev, "info:\nFW download over, size %d bytes\n",
1908 offset);
1910 ret = 0;
1912 done:
1913 dev_kfree_skb_any(skb);
1914 return ret;
1918 * This function checks the firmware status in card.
1920 * The winner interface is also determined by this function.
1922 static int
1923 mwifiex_check_fw_status(struct mwifiex_adapter *adapter, u32 poll_num)
1925 int ret = 0;
1926 u32 firmware_stat, winner_status;
1927 struct pcie_service_card *card = adapter->card;
1928 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1929 u32 tries;
1931 /* Mask spurios interrupts */
1932 if (mwifiex_write_reg(adapter, PCIE_HOST_INT_STATUS_MASK,
1933 HOST_INTR_MASK)) {
1934 dev_warn(adapter->dev, "Write register failed\n");
1935 return -1;
1938 dev_dbg(adapter->dev, "Setting driver ready signature\n");
1939 if (mwifiex_write_reg(adapter, reg->drv_rdy,
1940 FIRMWARE_READY_PCIE)) {
1941 dev_err(adapter->dev,
1942 "Failed to write driver ready signature\n");
1943 return -1;
1946 /* Wait for firmware initialization event */
1947 for (tries = 0; tries < poll_num; tries++) {
1948 if (mwifiex_read_reg(adapter, reg->fw_status,
1949 &firmware_stat))
1950 ret = -1;
1951 else
1952 ret = 0;
1953 if (ret)
1954 continue;
1955 if (firmware_stat == FIRMWARE_READY_PCIE) {
1956 ret = 0;
1957 break;
1958 } else {
1959 msleep(100);
1960 ret = -1;
1964 if (ret) {
1965 if (mwifiex_read_reg(adapter, reg->fw_status,
1966 &winner_status))
1967 ret = -1;
1968 else if (!winner_status) {
1969 dev_err(adapter->dev, "PCI-E is the winner\n");
1970 adapter->winner = 1;
1971 } else {
1972 dev_err(adapter->dev,
1973 "PCI-E is not the winner <%#x,%d>, exit dnld\n",
1974 ret, adapter->winner);
1978 return ret;
1982 * This function reads the interrupt status from card.
1984 static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
1986 u32 pcie_ireg;
1987 unsigned long flags;
1989 if (!mwifiex_pcie_ok_to_access_hw(adapter))
1990 return;
1992 if (mwifiex_read_reg(adapter, PCIE_HOST_INT_STATUS, &pcie_ireg)) {
1993 dev_warn(adapter->dev, "Read register failed\n");
1994 return;
1997 if ((pcie_ireg != 0xFFFFFFFF) && (pcie_ireg)) {
1999 mwifiex_pcie_disable_host_int(adapter);
2001 /* Clear the pending interrupts */
2002 if (mwifiex_write_reg(adapter, PCIE_HOST_INT_STATUS,
2003 ~pcie_ireg)) {
2004 dev_warn(adapter->dev, "Write register failed\n");
2005 return;
2007 spin_lock_irqsave(&adapter->int_lock, flags);
2008 adapter->int_status |= pcie_ireg;
2009 spin_unlock_irqrestore(&adapter->int_lock, flags);
2011 if (!adapter->pps_uapsd_mode &&
2012 adapter->ps_state == PS_STATE_SLEEP &&
2013 mwifiex_pcie_ok_to_access_hw(adapter)) {
2014 /* Potentially for PCIe we could get other
2015 * interrupts like shared. Don't change power
2016 * state until cookie is set */
2017 adapter->ps_state = PS_STATE_AWAKE;
2018 adapter->pm_wakeup_fw_try = false;
2024 * Interrupt handler for PCIe root port
2026 * This function reads the interrupt status from firmware and assigns
2027 * the main process in workqueue which will handle the interrupt.
2029 static irqreturn_t mwifiex_pcie_interrupt(int irq, void *context)
2031 struct pci_dev *pdev = (struct pci_dev *)context;
2032 struct pcie_service_card *card;
2033 struct mwifiex_adapter *adapter;
2035 if (!pdev) {
2036 pr_debug("info: %s: pdev is NULL\n", (u8 *)pdev);
2037 goto exit;
2040 card = (struct pcie_service_card *) pci_get_drvdata(pdev);
2041 if (!card || !card->adapter) {
2042 pr_debug("info: %s: card=%p adapter=%p\n", __func__, card,
2043 card ? card->adapter : NULL);
2044 goto exit;
2046 adapter = card->adapter;
2048 if (adapter->surprise_removed)
2049 goto exit;
2051 mwifiex_interrupt_status(adapter);
2052 queue_work(adapter->workqueue, &adapter->main_work);
2054 exit:
2055 return IRQ_HANDLED;
2059 * This function checks the current interrupt status.
2061 * The following interrupts are checked and handled by this function -
2062 * - Data sent
2063 * - Command sent
2064 * - Command received
2065 * - Packets received
2066 * - Events received
2068 * In case of Rx packets received, the packets are uploaded from card to
2069 * host and processed accordingly.
2071 static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
2073 int ret;
2074 u32 pcie_ireg;
2075 unsigned long flags;
2077 spin_lock_irqsave(&adapter->int_lock, flags);
2078 /* Clear out unused interrupts */
2079 pcie_ireg = adapter->int_status;
2080 adapter->int_status = 0;
2081 spin_unlock_irqrestore(&adapter->int_lock, flags);
2083 while (pcie_ireg & HOST_INTR_MASK) {
2084 if (pcie_ireg & HOST_INTR_DNLD_DONE) {
2085 pcie_ireg &= ~HOST_INTR_DNLD_DONE;
2086 dev_dbg(adapter->dev, "info: TX DNLD Done\n");
2087 ret = mwifiex_pcie_send_data_complete(adapter);
2088 if (ret)
2089 return ret;
2091 if (pcie_ireg & HOST_INTR_UPLD_RDY) {
2092 pcie_ireg &= ~HOST_INTR_UPLD_RDY;
2093 dev_dbg(adapter->dev, "info: Rx DATA\n");
2094 ret = mwifiex_pcie_process_recv_data(adapter);
2095 if (ret)
2096 return ret;
2098 if (pcie_ireg & HOST_INTR_EVENT_RDY) {
2099 pcie_ireg &= ~HOST_INTR_EVENT_RDY;
2100 dev_dbg(adapter->dev, "info: Rx EVENT\n");
2101 ret = mwifiex_pcie_process_event_ready(adapter);
2102 if (ret)
2103 return ret;
2106 if (pcie_ireg & HOST_INTR_CMD_DONE) {
2107 pcie_ireg &= ~HOST_INTR_CMD_DONE;
2108 if (adapter->cmd_sent) {
2109 dev_dbg(adapter->dev,
2110 "info: CMD sent Interrupt\n");
2111 adapter->cmd_sent = false;
2113 /* Handle command response */
2114 ret = mwifiex_pcie_process_cmd_complete(adapter);
2115 if (ret)
2116 return ret;
2119 if (mwifiex_pcie_ok_to_access_hw(adapter)) {
2120 if (mwifiex_read_reg(adapter, PCIE_HOST_INT_STATUS,
2121 &pcie_ireg)) {
2122 dev_warn(adapter->dev,
2123 "Read register failed\n");
2124 return -1;
2127 if ((pcie_ireg != 0xFFFFFFFF) && (pcie_ireg)) {
2128 if (mwifiex_write_reg(adapter,
2129 PCIE_HOST_INT_STATUS,
2130 ~pcie_ireg)) {
2131 dev_warn(adapter->dev,
2132 "Write register failed\n");
2133 return -1;
2139 dev_dbg(adapter->dev, "info: cmd_sent=%d data_sent=%d\n",
2140 adapter->cmd_sent, adapter->data_sent);
2141 if (adapter->ps_state != PS_STATE_SLEEP)
2142 mwifiex_pcie_enable_host_int(adapter);
2144 return 0;
2148 * This function downloads data from driver to card.
2150 * Both commands and data packets are transferred to the card by this
2151 * function.
2153 * This function adds the PCIE specific header to the front of the buffer
2154 * before transferring. The header contains the length of the packet and
2155 * the type. The firmware handles the packets based upon this set type.
2157 static int mwifiex_pcie_host_to_card(struct mwifiex_adapter *adapter, u8 type,
2158 struct sk_buff *skb,
2159 struct mwifiex_tx_param *tx_param)
2161 if (!skb) {
2162 dev_err(adapter->dev, "Passed NULL skb to %s\n", __func__);
2163 return -1;
2166 if (type == MWIFIEX_TYPE_DATA)
2167 return mwifiex_pcie_send_data(adapter, skb, tx_param);
2168 else if (type == MWIFIEX_TYPE_CMD)
2169 return mwifiex_pcie_send_cmd(adapter, skb);
2171 return 0;
2175 * This function initializes the PCI-E host memory space, WCB rings, etc.
2177 * The following initializations steps are followed -
2178 * - Allocate TXBD ring buffers
2179 * - Allocate RXBD ring buffers
2180 * - Allocate event BD ring buffers
2181 * - Allocate command response ring buffer
2182 * - Allocate sleep cookie buffer
2184 static int mwifiex_pcie_init(struct mwifiex_adapter *adapter)
2186 struct pcie_service_card *card = adapter->card;
2187 int ret;
2188 struct pci_dev *pdev = card->dev;
2189 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
2191 pci_set_drvdata(pdev, card);
2193 ret = pci_enable_device(pdev);
2194 if (ret)
2195 goto err_enable_dev;
2197 pci_set_master(pdev);
2199 dev_dbg(adapter->dev, "try set_consistent_dma_mask(32)\n");
2200 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2201 if (ret) {
2202 dev_err(adapter->dev, "set_dma_mask(32) failed\n");
2203 goto err_set_dma_mask;
2206 ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2207 if (ret) {
2208 dev_err(adapter->dev, "set_consistent_dma_mask(64) failed\n");
2209 goto err_set_dma_mask;
2212 ret = pci_request_region(pdev, 0, DRV_NAME);
2213 if (ret) {
2214 dev_err(adapter->dev, "req_reg(0) error\n");
2215 goto err_req_region0;
2217 card->pci_mmap = pci_iomap(pdev, 0, 0);
2218 if (!card->pci_mmap) {
2219 dev_err(adapter->dev, "iomap(0) error\n");
2220 ret = -EIO;
2221 goto err_iomap0;
2223 ret = pci_request_region(pdev, 2, DRV_NAME);
2224 if (ret) {
2225 dev_err(adapter->dev, "req_reg(2) error\n");
2226 goto err_req_region2;
2228 card->pci_mmap1 = pci_iomap(pdev, 2, 0);
2229 if (!card->pci_mmap1) {
2230 dev_err(adapter->dev, "iomap(2) error\n");
2231 ret = -EIO;
2232 goto err_iomap2;
2235 dev_dbg(adapter->dev,
2236 "PCI memory map Virt0: %p PCI memory map Virt2: %p\n",
2237 card->pci_mmap, card->pci_mmap1);
2239 card->cmdrsp_buf = NULL;
2240 ret = mwifiex_pcie_create_txbd_ring(adapter);
2241 if (ret)
2242 goto err_cre_txbd;
2243 ret = mwifiex_pcie_create_rxbd_ring(adapter);
2244 if (ret)
2245 goto err_cre_rxbd;
2246 ret = mwifiex_pcie_create_evtbd_ring(adapter);
2247 if (ret)
2248 goto err_cre_evtbd;
2249 ret = mwifiex_pcie_alloc_cmdrsp_buf(adapter);
2250 if (ret)
2251 goto err_alloc_cmdbuf;
2252 if (reg->sleep_cookie) {
2253 ret = mwifiex_pcie_alloc_sleep_cookie_buf(adapter);
2254 if (ret)
2255 goto err_alloc_cookie;
2256 } else {
2257 card->sleep_cookie_vbase = NULL;
2259 return ret;
2261 err_alloc_cookie:
2262 mwifiex_pcie_delete_cmdrsp_buf(adapter);
2263 err_alloc_cmdbuf:
2264 mwifiex_pcie_delete_evtbd_ring(adapter);
2265 err_cre_evtbd:
2266 mwifiex_pcie_delete_rxbd_ring(adapter);
2267 err_cre_rxbd:
2268 mwifiex_pcie_delete_txbd_ring(adapter);
2269 err_cre_txbd:
2270 pci_iounmap(pdev, card->pci_mmap1);
2271 err_iomap2:
2272 pci_release_region(pdev, 2);
2273 err_req_region2:
2274 pci_iounmap(pdev, card->pci_mmap);
2275 err_iomap0:
2276 pci_release_region(pdev, 0);
2277 err_req_region0:
2278 err_set_dma_mask:
2279 pci_disable_device(pdev);
2280 err_enable_dev:
2281 pci_set_drvdata(pdev, NULL);
2282 return ret;
2286 * This function cleans up the allocated card buffers.
2288 * The following are freed by this function -
2289 * - TXBD ring buffers
2290 * - RXBD ring buffers
2291 * - Event BD ring buffers
2292 * - Command response ring buffer
2293 * - Sleep cookie buffer
2295 static void mwifiex_pcie_cleanup(struct mwifiex_adapter *adapter)
2297 struct pcie_service_card *card = adapter->card;
2298 struct pci_dev *pdev = card->dev;
2299 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
2301 if (user_rmmod) {
2302 dev_dbg(adapter->dev, "Clearing driver ready signature\n");
2303 if (mwifiex_write_reg(adapter, reg->drv_rdy, 0x00000000))
2304 dev_err(adapter->dev,
2305 "Failed to write driver not-ready signature\n");
2308 if (pdev) {
2309 pci_iounmap(pdev, card->pci_mmap);
2310 pci_iounmap(pdev, card->pci_mmap1);
2311 pci_disable_device(pdev);
2312 pci_release_region(pdev, 2);
2313 pci_release_region(pdev, 0);
2314 pci_set_drvdata(pdev, NULL);
2319 * This function registers the PCIE device.
2321 * PCIE IRQ is claimed, block size is set and driver data is initialized.
2323 static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
2325 int ret;
2326 struct pcie_service_card *card = adapter->card;
2327 struct pci_dev *pdev = card->dev;
2329 /* save adapter pointer in card */
2330 card->adapter = adapter;
2332 ret = request_irq(pdev->irq, mwifiex_pcie_interrupt, IRQF_SHARED,
2333 "MRVL_PCIE", pdev);
2334 if (ret) {
2335 pr_err("request_irq failed: ret=%d\n", ret);
2336 adapter->card = NULL;
2337 return -1;
2340 adapter->dev = &pdev->dev;
2341 strcpy(adapter->fw_name, card->pcie.firmware);
2343 return 0;
2347 * This function unregisters the PCIE device.
2349 * The PCIE IRQ is released, the function is disabled and driver
2350 * data is set to null.
2352 static void mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
2354 struct pcie_service_card *card = adapter->card;
2355 const struct mwifiex_pcie_card_reg *reg;
2357 if (card) {
2358 dev_dbg(adapter->dev, "%s(): calling free_irq()\n", __func__);
2359 free_irq(card->dev->irq, card->dev);
2361 reg = card->pcie.reg;
2362 if (reg->sleep_cookie)
2363 mwifiex_pcie_delete_sleep_cookie_buf(adapter);
2365 mwifiex_pcie_delete_cmdrsp_buf(adapter);
2366 mwifiex_pcie_delete_evtbd_ring(adapter);
2367 mwifiex_pcie_delete_rxbd_ring(adapter);
2368 mwifiex_pcie_delete_txbd_ring(adapter);
2369 card->cmdrsp_buf = NULL;
2373 static struct mwifiex_if_ops pcie_ops = {
2374 .init_if = mwifiex_pcie_init,
2375 .cleanup_if = mwifiex_pcie_cleanup,
2376 .check_fw_status = mwifiex_check_fw_status,
2377 .prog_fw = mwifiex_prog_fw_w_helper,
2378 .register_dev = mwifiex_register_dev,
2379 .unregister_dev = mwifiex_unregister_dev,
2380 .enable_int = mwifiex_pcie_enable_host_int,
2381 .process_int_status = mwifiex_process_int_status,
2382 .host_to_card = mwifiex_pcie_host_to_card,
2383 .wakeup = mwifiex_pm_wakeup_card,
2384 .wakeup_complete = mwifiex_pm_wakeup_card_complete,
2386 /* PCIE specific */
2387 .cmdrsp_complete = mwifiex_pcie_cmdrsp_complete,
2388 .event_complete = mwifiex_pcie_event_complete,
2389 .update_mp_end_port = NULL,
2390 .cleanup_mpa_buf = NULL,
2391 .init_fw_port = mwifiex_pcie_init_fw_port,
2392 .clean_pcie_ring = mwifiex_clean_pcie_ring_buf,
2396 * This function initializes the PCIE driver module.
2398 * This initiates the semaphore and registers the device with
2399 * PCIE bus.
2401 static int mwifiex_pcie_init_module(void)
2403 int ret;
2405 pr_debug("Marvell PCIe Driver\n");
2407 sema_init(&add_remove_card_sem, 1);
2409 /* Clear the flag in case user removes the card. */
2410 user_rmmod = 0;
2412 ret = pci_register_driver(&mwifiex_pcie);
2413 if (ret)
2414 pr_err("Driver register failed!\n");
2415 else
2416 pr_debug("info: Driver registered successfully!\n");
2418 return ret;
2422 * This function cleans up the PCIE driver.
2424 * The following major steps are followed for cleanup -
2425 * - Resume the device if its suspended
2426 * - Disconnect the device if connected
2427 * - Shutdown the firmware
2428 * - Unregister the device from PCIE bus.
2430 static void mwifiex_pcie_cleanup_module(void)
2432 if (!down_interruptible(&add_remove_card_sem))
2433 up(&add_remove_card_sem);
2435 /* Set the flag as user is removing this module. */
2436 user_rmmod = 1;
2438 pci_unregister_driver(&mwifiex_pcie);
2441 module_init(mwifiex_pcie_init_module);
2442 module_exit(mwifiex_pcie_cleanup_module);
2444 MODULE_AUTHOR("Marvell International Ltd.");
2445 MODULE_DESCRIPTION("Marvell WiFi-Ex PCI-Express Driver version " PCIE_VERSION);
2446 MODULE_VERSION(PCIE_VERSION);
2447 MODULE_LICENSE("GPL v2");
2448 MODULE_FIRMWARE(PCIE8766_DEFAULT_FW_NAME);
2449 MODULE_FIRMWARE(PCIE8897_DEFAULT_FW_NAME);