Linux 3.11-rc3
[cris-mirror.git] / drivers / net / wireless / mwifiex / pcie.c
blob20c9c4c7b0b2eb64fa75bd4180fd85015d77b33e
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
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 pci_dev *pdev, pm_message_t state)
90 struct mwifiex_adapter *adapter;
91 struct pcie_service_card *card;
92 int hs_actived;
94 if (pdev) {
95 card = (struct pcie_service_card *) pci_get_drvdata(pdev);
96 if (!card || !card->adapter) {
97 pr_err("Card or adapter structure is not valid\n");
98 return 0;
100 } else {
101 pr_err("PCIE device is not specified\n");
102 return 0;
105 adapter = card->adapter;
107 hs_actived = mwifiex_enable_hs(adapter);
109 /* Indicate device suspended */
110 adapter->is_suspended = true;
112 return 0;
116 * Kernel needs to suspend all functions separately. Therefore all
117 * registered functions must have drivers with suspend and resume
118 * methods. Failing that the kernel simply removes the whole card.
120 * If already not resumed, this function turns on the traffic and
121 * sends a host sleep cancel request to the firmware.
123 static int mwifiex_pcie_resume(struct pci_dev *pdev)
125 struct mwifiex_adapter *adapter;
126 struct pcie_service_card *card;
128 if (pdev) {
129 card = (struct pcie_service_card *) pci_get_drvdata(pdev);
130 if (!card || !card->adapter) {
131 pr_err("Card or adapter structure is not valid\n");
132 return 0;
134 } else {
135 pr_err("PCIE device is not specified\n");
136 return 0;
139 adapter = card->adapter;
141 if (!adapter->is_suspended) {
142 dev_warn(adapter->dev, "Device already resumed\n");
143 return 0;
146 adapter->is_suspended = false;
148 mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
149 MWIFIEX_ASYNC_CMD);
151 return 0;
153 #endif
156 * This function probes an mwifiex device and registers it. It allocates
157 * the card structure, enables PCIE function number and initiates the
158 * device registration and initialization procedure by adding a logical
159 * interface.
161 static int mwifiex_pcie_probe(struct pci_dev *pdev,
162 const struct pci_device_id *ent)
164 struct pcie_service_card *card;
166 pr_debug("info: vendor=0x%4.04X device=0x%4.04X rev=%d\n",
167 pdev->vendor, pdev->device, pdev->revision);
169 card = kzalloc(sizeof(struct pcie_service_card), GFP_KERNEL);
170 if (!card)
171 return -ENOMEM;
173 card->dev = pdev;
175 if (ent->driver_data) {
176 struct mwifiex_pcie_device *data = (void *)ent->driver_data;
177 card->pcie.firmware = data->firmware;
178 card->pcie.reg = data->reg;
179 card->pcie.blksz_fw_dl = data->blksz_fw_dl;
182 if (mwifiex_add_card(card, &add_remove_card_sem, &pcie_ops,
183 MWIFIEX_PCIE)) {
184 pr_err("%s failed\n", __func__);
185 kfree(card);
186 return -1;
189 return 0;
193 * This function removes the interface and frees up the card structure.
195 static void mwifiex_pcie_remove(struct pci_dev *pdev)
197 struct pcie_service_card *card;
198 struct mwifiex_adapter *adapter;
199 struct mwifiex_private *priv;
200 int i;
202 card = pci_get_drvdata(pdev);
203 if (!card)
204 return;
206 adapter = card->adapter;
207 if (!adapter || !adapter->priv_num)
208 return;
210 /* In case driver is removed when asynchronous FW load is in progress */
211 wait_for_completion(&adapter->fw_load);
213 if (user_rmmod) {
214 #ifdef CONFIG_PM
215 if (adapter->is_suspended)
216 mwifiex_pcie_resume(pdev);
217 #endif
219 for (i = 0; i < adapter->priv_num; i++)
220 if ((GET_BSS_ROLE(adapter->priv[i]) ==
221 MWIFIEX_BSS_ROLE_STA) &&
222 adapter->priv[i]->media_connected)
223 mwifiex_deauthenticate(adapter->priv[i], NULL);
225 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
227 mwifiex_disable_auto_ds(priv);
229 mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
232 mwifiex_remove_card(card->adapter, &add_remove_card_sem);
233 kfree(card);
236 static DEFINE_PCI_DEVICE_TABLE(mwifiex_ids) = {
238 PCIE_VENDOR_ID_MARVELL, PCIE_DEVICE_ID_MARVELL_88W8766P,
239 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
240 .driver_data = (unsigned long) &mwifiex_pcie8766,
243 PCIE_VENDOR_ID_MARVELL, PCIE_DEVICE_ID_MARVELL_88W8897,
244 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
245 .driver_data = (unsigned long) &mwifiex_pcie8897,
250 MODULE_DEVICE_TABLE(pci, mwifiex_ids);
252 /* PCI Device Driver */
253 static struct pci_driver __refdata mwifiex_pcie = {
254 .name = "mwifiex_pcie",
255 .id_table = mwifiex_ids,
256 .probe = mwifiex_pcie_probe,
257 .remove = mwifiex_pcie_remove,
258 #ifdef CONFIG_PM
259 /* Power Management Hooks */
260 .suspend = mwifiex_pcie_suspend,
261 .resume = mwifiex_pcie_resume,
262 #endif
266 * This function writes data into PCIE card register.
268 static int mwifiex_write_reg(struct mwifiex_adapter *adapter, int reg, u32 data)
270 struct pcie_service_card *card = adapter->card;
272 iowrite32(data, card->pci_mmap1 + reg);
274 return 0;
278 * This function reads data from PCIE card register.
280 static int mwifiex_read_reg(struct mwifiex_adapter *adapter, int reg, u32 *data)
282 struct pcie_service_card *card = adapter->card;
284 *data = ioread32(card->pci_mmap1 + reg);
286 return 0;
290 * This function adds delay loop to ensure FW is awake before proceeding.
292 static void mwifiex_pcie_dev_wakeup_delay(struct mwifiex_adapter *adapter)
294 int i = 0;
296 while (mwifiex_pcie_ok_to_access_hw(adapter)) {
297 i++;
298 usleep_range(10, 20);
299 /* 50ms max wait */
300 if (i == 5000)
301 break;
304 return;
307 /* This function wakes up the card by reading fw_status register. */
308 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
310 u32 fw_status;
311 struct pcie_service_card *card = adapter->card;
312 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
314 dev_dbg(adapter->dev, "event: Wakeup device...\n");
316 if (reg->sleep_cookie)
317 mwifiex_pcie_dev_wakeup_delay(adapter);
319 /* Reading fw_status register will wakeup device */
320 if (mwifiex_read_reg(adapter, reg->fw_status, &fw_status)) {
321 dev_warn(adapter->dev, "Reading fw_status register failed\n");
322 return -1;
325 if (reg->sleep_cookie) {
326 mwifiex_pcie_dev_wakeup_delay(adapter);
327 dev_dbg(adapter->dev, "PCIE wakeup: Setting PS_STATE_AWAKE\n");
328 adapter->ps_state = PS_STATE_AWAKE;
331 return 0;
335 * This function is called after the card has woken up.
337 * The card configuration register is reset.
339 static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
341 dev_dbg(adapter->dev, "cmd: Wakeup device completed\n");
343 return 0;
347 * This function disables the host interrupt.
349 * The host interrupt mask is read, the disable bit is reset and
350 * written back to the card host interrupt mask register.
352 static int mwifiex_pcie_disable_host_int(struct mwifiex_adapter *adapter)
354 if (mwifiex_pcie_ok_to_access_hw(adapter)) {
355 if (mwifiex_write_reg(adapter, PCIE_HOST_INT_MASK,
356 0x00000000)) {
357 dev_warn(adapter->dev, "Disable host interrupt failed\n");
358 return -1;
362 return 0;
366 * This function enables the host interrupt.
368 * The host interrupt enable mask is written to the card
369 * host interrupt mask register.
371 static int mwifiex_pcie_enable_host_int(struct mwifiex_adapter *adapter)
373 if (mwifiex_pcie_ok_to_access_hw(adapter)) {
374 /* Simply write the mask to the register */
375 if (mwifiex_write_reg(adapter, PCIE_HOST_INT_MASK,
376 HOST_INTR_MASK)) {
377 dev_warn(adapter->dev, "Enable host interrupt failed\n");
378 return -1;
382 return 0;
386 * This function initializes TX buffer ring descriptors
388 static int mwifiex_init_txq_ring(struct mwifiex_adapter *adapter)
390 struct pcie_service_card *card = adapter->card;
391 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
392 struct mwifiex_pcie_buf_desc *desc;
393 struct mwifiex_pfu_buf_desc *desc2;
394 int i;
396 for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
397 card->tx_buf_list[i] = NULL;
398 if (reg->pfu_enabled) {
399 card->txbd_ring[i] = (void *)card->txbd_ring_vbase +
400 (sizeof(*desc2) * i);
401 desc2 = card->txbd_ring[i];
402 memset(desc2, 0, sizeof(*desc2));
403 } else {
404 card->txbd_ring[i] = (void *)card->txbd_ring_vbase +
405 (sizeof(*desc) * i);
406 desc = card->txbd_ring[i];
407 memset(desc, 0, sizeof(*desc));
411 return 0;
414 /* This function initializes RX buffer ring descriptors. Each SKB is allocated
415 * here and after mapping PCI memory, its physical address is assigned to
416 * PCIE Rx buffer descriptor's physical address.
418 static int mwifiex_init_rxq_ring(struct mwifiex_adapter *adapter)
420 struct pcie_service_card *card = adapter->card;
421 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
422 struct sk_buff *skb;
423 struct mwifiex_pcie_buf_desc *desc;
424 struct mwifiex_pfu_buf_desc *desc2;
425 dma_addr_t buf_pa;
426 int i;
428 for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
429 /* Allocate skb here so that firmware can DMA data from it */
430 skb = dev_alloc_skb(MWIFIEX_RX_DATA_BUF_SIZE);
431 if (!skb) {
432 dev_err(adapter->dev,
433 "Unable to allocate skb for RX ring.\n");
434 kfree(card->rxbd_ring_vbase);
435 return -ENOMEM;
438 if (mwifiex_map_pci_memory(adapter, skb,
439 MWIFIEX_RX_DATA_BUF_SIZE,
440 PCI_DMA_FROMDEVICE))
441 return -1;
443 MWIFIEX_SKB_PACB(skb, &buf_pa);
445 dev_dbg(adapter->dev,
446 "info: RX ring: skb=%p len=%d data=%p buf_pa=%#x:%x\n",
447 skb, skb->len, skb->data, (u32)buf_pa,
448 (u32)((u64)buf_pa >> 32));
450 card->rx_buf_list[i] = skb;
451 if (reg->pfu_enabled) {
452 card->rxbd_ring[i] = (void *)card->rxbd_ring_vbase +
453 (sizeof(*desc2) * i);
454 desc2 = card->rxbd_ring[i];
455 desc2->paddr = buf_pa;
456 desc2->len = (u16)skb->len;
457 desc2->frag_len = (u16)skb->len;
458 desc2->flags = reg->ring_flag_eop | reg->ring_flag_sop;
459 desc2->offset = 0;
460 } else {
461 card->rxbd_ring[i] = (void *)(card->rxbd_ring_vbase +
462 (sizeof(*desc) * i));
463 desc = card->rxbd_ring[i];
464 desc->paddr = buf_pa;
465 desc->len = (u16)skb->len;
466 desc->flags = 0;
470 return 0;
473 /* This function initializes event buffer ring descriptors. Each SKB is
474 * allocated here and after mapping PCI memory, its physical address is assigned
475 * to PCIE Rx buffer descriptor's physical address
477 static int mwifiex_pcie_init_evt_ring(struct mwifiex_adapter *adapter)
479 struct pcie_service_card *card = adapter->card;
480 struct mwifiex_evt_buf_desc *desc;
481 struct sk_buff *skb;
482 dma_addr_t buf_pa;
483 int i;
485 for (i = 0; i < MWIFIEX_MAX_EVT_BD; i++) {
486 /* Allocate skb here so that firmware can DMA data from it */
487 skb = dev_alloc_skb(MAX_EVENT_SIZE);
488 if (!skb) {
489 dev_err(adapter->dev,
490 "Unable to allocate skb for EVENT buf.\n");
491 kfree(card->evtbd_ring_vbase);
492 return -ENOMEM;
494 skb_put(skb, MAX_EVENT_SIZE);
496 if (mwifiex_map_pci_memory(adapter, skb, MAX_EVENT_SIZE,
497 PCI_DMA_FROMDEVICE))
498 return -1;
500 MWIFIEX_SKB_PACB(skb, &buf_pa);
502 dev_dbg(adapter->dev,
503 "info: EVT ring: skb=%p len=%d data=%p buf_pa=%#x:%x\n",
504 skb, skb->len, skb->data, (u32)buf_pa,
505 (u32)((u64)buf_pa >> 32));
507 card->evt_buf_list[i] = skb;
508 card->evtbd_ring[i] = (void *)(card->evtbd_ring_vbase +
509 (sizeof(*desc) * i));
510 desc = card->evtbd_ring[i];
511 desc->paddr = buf_pa;
512 desc->len = (u16)skb->len;
513 desc->flags = 0;
516 return 0;
519 /* This function cleans up TX buffer rings. If any of the buffer list has valid
520 * SKB address, associated SKB is freed.
522 static void mwifiex_cleanup_txq_ring(struct mwifiex_adapter *adapter)
524 struct pcie_service_card *card = adapter->card;
525 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
526 struct sk_buff *skb;
527 struct mwifiex_pcie_buf_desc *desc;
528 struct mwifiex_pfu_buf_desc *desc2;
529 int i;
531 for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
532 if (reg->pfu_enabled) {
533 desc2 = card->txbd_ring[i];
534 if (card->tx_buf_list[i]) {
535 skb = card->tx_buf_list[i];
536 pci_unmap_single(card->dev, desc2->paddr,
537 skb->len, PCI_DMA_TODEVICE);
538 dev_kfree_skb_any(skb);
540 memset(desc2, 0, sizeof(*desc2));
541 } else {
542 desc = card->txbd_ring[i];
543 if (card->tx_buf_list[i]) {
544 skb = card->tx_buf_list[i];
545 pci_unmap_single(card->dev, desc->paddr,
546 skb->len, PCI_DMA_TODEVICE);
547 dev_kfree_skb_any(skb);
549 memset(desc, 0, sizeof(*desc));
551 card->tx_buf_list[i] = NULL;
554 return;
557 /* This function cleans up RX buffer rings. If any of the buffer list has valid
558 * SKB address, associated SKB is freed.
560 static void mwifiex_cleanup_rxq_ring(struct mwifiex_adapter *adapter)
562 struct pcie_service_card *card = adapter->card;
563 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
564 struct mwifiex_pcie_buf_desc *desc;
565 struct mwifiex_pfu_buf_desc *desc2;
566 struct sk_buff *skb;
567 int i;
569 for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
570 if (reg->pfu_enabled) {
571 desc2 = card->rxbd_ring[i];
572 if (card->rx_buf_list[i]) {
573 skb = card->rx_buf_list[i];
574 pci_unmap_single(card->dev, desc2->paddr,
575 skb->len, PCI_DMA_FROMDEVICE);
576 dev_kfree_skb_any(skb);
578 memset(desc2, 0, sizeof(*desc2));
579 } else {
580 desc = card->rxbd_ring[i];
581 if (card->rx_buf_list[i]) {
582 skb = card->rx_buf_list[i];
583 pci_unmap_single(card->dev, desc->paddr,
584 skb->len, PCI_DMA_FROMDEVICE);
585 dev_kfree_skb_any(skb);
587 memset(desc, 0, sizeof(*desc));
589 card->rx_buf_list[i] = NULL;
592 return;
595 /* This function cleans up event buffer rings. If any of the buffer list has
596 * valid SKB address, associated SKB is freed.
598 static void mwifiex_cleanup_evt_ring(struct mwifiex_adapter *adapter)
600 struct pcie_service_card *card = adapter->card;
601 struct mwifiex_evt_buf_desc *desc;
602 struct sk_buff *skb;
603 int i;
605 for (i = 0; i < MWIFIEX_MAX_EVT_BD; i++) {
606 desc = card->evtbd_ring[i];
607 if (card->evt_buf_list[i]) {
608 skb = card->evt_buf_list[i];
609 pci_unmap_single(card->dev, desc->paddr, MAX_EVENT_SIZE,
610 PCI_DMA_FROMDEVICE);
611 dev_kfree_skb_any(skb);
613 card->evt_buf_list[i] = NULL;
614 memset(desc, 0, sizeof(*desc));
617 return;
620 /* This function creates buffer descriptor ring for TX
622 static int mwifiex_pcie_create_txbd_ring(struct mwifiex_adapter *adapter)
624 struct pcie_service_card *card = adapter->card;
625 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
628 * driver maintaines the write pointer and firmware maintaines the read
629 * pointer. The write pointer starts at 0 (zero) while the read pointer
630 * starts at zero with rollover bit set
632 card->txbd_wrptr = 0;
634 if (reg->pfu_enabled)
635 card->txbd_rdptr = 0;
636 else
637 card->txbd_rdptr |= reg->tx_rollover_ind;
639 /* allocate shared memory for the BD ring and divide the same in to
640 several descriptors */
641 if (reg->pfu_enabled)
642 card->txbd_ring_size = sizeof(struct mwifiex_pfu_buf_desc) *
643 MWIFIEX_MAX_TXRX_BD;
644 else
645 card->txbd_ring_size = sizeof(struct mwifiex_pcie_buf_desc) *
646 MWIFIEX_MAX_TXRX_BD;
648 dev_dbg(adapter->dev, "info: txbd_ring: Allocating %d bytes\n",
649 card->txbd_ring_size);
650 card->txbd_ring_vbase = pci_alloc_consistent(card->dev,
651 card->txbd_ring_size,
652 &card->txbd_ring_pbase);
653 if (!card->txbd_ring_vbase) {
654 dev_err(adapter->dev,
655 "allocate consistent memory (%d bytes) failed!\n",
656 card->txbd_ring_size);
657 return -ENOMEM;
659 dev_dbg(adapter->dev,
660 "info: txbd_ring - base: %p, pbase: %#x:%x, len: %x\n",
661 card->txbd_ring_vbase, (unsigned int)card->txbd_ring_pbase,
662 (u32)((u64)card->txbd_ring_pbase >> 32), card->txbd_ring_size);
664 return mwifiex_init_txq_ring(adapter);
667 static int mwifiex_pcie_delete_txbd_ring(struct mwifiex_adapter *adapter)
669 struct pcie_service_card *card = adapter->card;
670 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
672 mwifiex_cleanup_txq_ring(adapter);
674 if (card->txbd_ring_vbase)
675 pci_free_consistent(card->dev, card->txbd_ring_size,
676 card->txbd_ring_vbase,
677 card->txbd_ring_pbase);
678 card->txbd_ring_size = 0;
679 card->txbd_wrptr = 0;
680 card->txbd_rdptr = 0 | reg->tx_rollover_ind;
681 card->txbd_ring_vbase = NULL;
682 card->txbd_ring_pbase = 0;
684 return 0;
688 * This function creates buffer descriptor ring for RX
690 static int mwifiex_pcie_create_rxbd_ring(struct mwifiex_adapter *adapter)
692 struct pcie_service_card *card = adapter->card;
693 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
696 * driver maintaines the read pointer and firmware maintaines the write
697 * pointer. The write pointer starts at 0 (zero) while the read pointer
698 * starts at zero with rollover bit set
700 card->rxbd_wrptr = 0;
701 card->rxbd_rdptr = reg->rx_rollover_ind;
703 if (reg->pfu_enabled)
704 card->rxbd_ring_size = sizeof(struct mwifiex_pfu_buf_desc) *
705 MWIFIEX_MAX_TXRX_BD;
706 else
707 card->rxbd_ring_size = sizeof(struct mwifiex_pcie_buf_desc) *
708 MWIFIEX_MAX_TXRX_BD;
710 dev_dbg(adapter->dev, "info: rxbd_ring: Allocating %d bytes\n",
711 card->rxbd_ring_size);
712 card->rxbd_ring_vbase = pci_alloc_consistent(card->dev,
713 card->rxbd_ring_size,
714 &card->rxbd_ring_pbase);
715 if (!card->rxbd_ring_vbase) {
716 dev_err(adapter->dev,
717 "allocate consistent memory (%d bytes) failed!\n",
718 card->rxbd_ring_size);
719 return -ENOMEM;
722 dev_dbg(adapter->dev,
723 "info: rxbd_ring - base: %p, pbase: %#x:%x, len: %#x\n",
724 card->rxbd_ring_vbase, (u32)card->rxbd_ring_pbase,
725 (u32)((u64)card->rxbd_ring_pbase >> 32),
726 card->rxbd_ring_size);
728 return mwifiex_init_rxq_ring(adapter);
732 * This function deletes Buffer descriptor ring for RX
734 static int mwifiex_pcie_delete_rxbd_ring(struct mwifiex_adapter *adapter)
736 struct pcie_service_card *card = adapter->card;
737 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
739 mwifiex_cleanup_rxq_ring(adapter);
741 if (card->rxbd_ring_vbase)
742 pci_free_consistent(card->dev, card->rxbd_ring_size,
743 card->rxbd_ring_vbase,
744 card->rxbd_ring_pbase);
745 card->rxbd_ring_size = 0;
746 card->rxbd_wrptr = 0;
747 card->rxbd_rdptr = 0 | reg->rx_rollover_ind;
748 card->rxbd_ring_vbase = NULL;
749 card->rxbd_ring_pbase = 0;
751 return 0;
755 * This function creates buffer descriptor ring for Events
757 static int mwifiex_pcie_create_evtbd_ring(struct mwifiex_adapter *adapter)
759 struct pcie_service_card *card = adapter->card;
760 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
763 * driver maintaines the read pointer and firmware maintaines the write
764 * pointer. The write pointer starts at 0 (zero) while the read pointer
765 * starts at zero with rollover bit set
767 card->evtbd_wrptr = 0;
768 card->evtbd_rdptr = reg->evt_rollover_ind;
770 card->evtbd_ring_size = sizeof(struct mwifiex_evt_buf_desc) *
771 MWIFIEX_MAX_EVT_BD;
773 dev_dbg(adapter->dev, "info: evtbd_ring: Allocating %d bytes\n",
774 card->evtbd_ring_size);
775 card->evtbd_ring_vbase = pci_alloc_consistent(card->dev,
776 card->evtbd_ring_size,
777 &card->evtbd_ring_pbase);
778 if (!card->evtbd_ring_vbase) {
779 dev_err(adapter->dev,
780 "allocate consistent memory (%d bytes) failed!\n",
781 card->evtbd_ring_size);
782 return -ENOMEM;
785 dev_dbg(adapter->dev,
786 "info: CMDRSP/EVT bd_ring - base: %p pbase: %#x:%x len: %#x\n",
787 card->evtbd_ring_vbase, (u32)card->evtbd_ring_pbase,
788 (u32)((u64)card->evtbd_ring_pbase >> 32),
789 card->evtbd_ring_size);
791 return mwifiex_pcie_init_evt_ring(adapter);
795 * This function deletes Buffer descriptor ring for Events
797 static int mwifiex_pcie_delete_evtbd_ring(struct mwifiex_adapter *adapter)
799 struct pcie_service_card *card = adapter->card;
800 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
802 mwifiex_cleanup_evt_ring(adapter);
804 if (card->evtbd_ring_vbase)
805 pci_free_consistent(card->dev, card->evtbd_ring_size,
806 card->evtbd_ring_vbase,
807 card->evtbd_ring_pbase);
808 card->evtbd_wrptr = 0;
809 card->evtbd_rdptr = 0 | reg->evt_rollover_ind;
810 card->evtbd_ring_size = 0;
811 card->evtbd_ring_vbase = NULL;
812 card->evtbd_ring_pbase = 0;
814 return 0;
818 * This function allocates a buffer for CMDRSP
820 static int mwifiex_pcie_alloc_cmdrsp_buf(struct mwifiex_adapter *adapter)
822 struct pcie_service_card *card = adapter->card;
823 struct sk_buff *skb;
825 /* Allocate memory for receiving command response data */
826 skb = dev_alloc_skb(MWIFIEX_UPLD_SIZE);
827 if (!skb) {
828 dev_err(adapter->dev,
829 "Unable to allocate skb for command response data.\n");
830 return -ENOMEM;
832 skb_put(skb, MWIFIEX_UPLD_SIZE);
833 if (mwifiex_map_pci_memory(adapter, skb, MWIFIEX_UPLD_SIZE,
834 PCI_DMA_FROMDEVICE))
835 return -1;
837 card->cmdrsp_buf = skb;
839 return 0;
843 * This function deletes a buffer for CMDRSP
845 static int mwifiex_pcie_delete_cmdrsp_buf(struct mwifiex_adapter *adapter)
847 struct pcie_service_card *card;
848 dma_addr_t buf_pa;
850 if (!adapter)
851 return 0;
853 card = adapter->card;
855 if (card && card->cmdrsp_buf) {
856 MWIFIEX_SKB_PACB(card->cmdrsp_buf, &buf_pa);
857 pci_unmap_single(card->dev, buf_pa, MWIFIEX_UPLD_SIZE,
858 PCI_DMA_FROMDEVICE);
859 dev_kfree_skb_any(card->cmdrsp_buf);
862 if (card && card->cmd_buf) {
863 MWIFIEX_SKB_PACB(card->cmd_buf, &buf_pa);
864 pci_unmap_single(card->dev, buf_pa, card->cmd_buf->len,
865 PCI_DMA_TODEVICE);
867 return 0;
871 * This function allocates a buffer for sleep cookie
873 static int mwifiex_pcie_alloc_sleep_cookie_buf(struct mwifiex_adapter *adapter)
875 struct pcie_service_card *card = adapter->card;
877 card->sleep_cookie_vbase = pci_alloc_consistent(card->dev, sizeof(u32),
878 &card->sleep_cookie_pbase);
879 if (!card->sleep_cookie_vbase) {
880 dev_err(adapter->dev, "pci_alloc_consistent failed!\n");
881 return -ENOMEM;
883 /* Init val of Sleep Cookie */
884 *(u32 *)card->sleep_cookie_vbase = FW_AWAKE_COOKIE;
886 dev_dbg(adapter->dev, "alloc_scook: sleep cookie=0x%x\n",
887 *((u32 *)card->sleep_cookie_vbase));
889 return 0;
893 * This function deletes buffer for sleep cookie
895 static int mwifiex_pcie_delete_sleep_cookie_buf(struct mwifiex_adapter *adapter)
897 struct pcie_service_card *card;
899 if (!adapter)
900 return 0;
902 card = adapter->card;
904 if (card && card->sleep_cookie_vbase) {
905 pci_free_consistent(card->dev, sizeof(u32),
906 card->sleep_cookie_vbase,
907 card->sleep_cookie_pbase);
908 card->sleep_cookie_vbase = NULL;
911 return 0;
914 /* This function flushes the TX buffer descriptor ring
915 * This function defined as handler is also called while cleaning TXRX
916 * during disconnect/ bss stop.
918 static int mwifiex_clean_pcie_ring_buf(struct mwifiex_adapter *adapter)
920 struct pcie_service_card *card = adapter->card;
922 if (!mwifiex_pcie_txbd_empty(card, card->txbd_rdptr)) {
923 card->txbd_flush = 1;
924 /* write pointer already set at last send
925 * send dnld-rdy intr again, wait for completion.
927 if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
928 CPU_INTR_DNLD_RDY)) {
929 dev_err(adapter->dev,
930 "failed to assert dnld-rdy interrupt.\n");
931 return -1;
934 return 0;
938 * This function unmaps and frees downloaded data buffer
940 static int mwifiex_pcie_send_data_complete(struct mwifiex_adapter *adapter)
942 struct sk_buff *skb;
943 dma_addr_t buf_pa;
944 u32 wrdoneidx, rdptr, num_tx_buffs, unmap_count = 0;
945 struct mwifiex_pcie_buf_desc *desc;
946 struct mwifiex_pfu_buf_desc *desc2;
947 struct pcie_service_card *card = adapter->card;
948 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
950 if (!mwifiex_pcie_ok_to_access_hw(adapter))
951 mwifiex_pm_wakeup_card(adapter);
953 /* Read the TX ring read pointer set by firmware */
954 if (mwifiex_read_reg(adapter, reg->tx_rdptr, &rdptr)) {
955 dev_err(adapter->dev,
956 "SEND COMP: failed to read reg->tx_rdptr\n");
957 return -1;
960 dev_dbg(adapter->dev, "SEND COMP: rdptr_prev=0x%x, rdptr=0x%x\n",
961 card->txbd_rdptr, rdptr);
963 num_tx_buffs = MWIFIEX_MAX_TXRX_BD << reg->tx_start_ptr;
964 /* free from previous txbd_rdptr to current txbd_rdptr */
965 while (((card->txbd_rdptr & reg->tx_mask) !=
966 (rdptr & reg->tx_mask)) ||
967 ((card->txbd_rdptr & reg->tx_rollover_ind) !=
968 (rdptr & reg->tx_rollover_ind))) {
969 wrdoneidx = (card->txbd_rdptr & reg->tx_mask) >>
970 reg->tx_start_ptr;
972 skb = card->tx_buf_list[wrdoneidx];
973 if (skb) {
974 dev_dbg(adapter->dev,
975 "SEND COMP: Detach skb %p at txbd_rdidx=%d\n",
976 skb, wrdoneidx);
977 MWIFIEX_SKB_PACB(skb, &buf_pa);
978 pci_unmap_single(card->dev, buf_pa, skb->len,
979 PCI_DMA_TODEVICE);
981 unmap_count++;
983 if (card->txbd_flush)
984 mwifiex_write_data_complete(adapter, skb, 0,
985 -1);
986 else
987 mwifiex_write_data_complete(adapter, skb, 0, 0);
990 card->tx_buf_list[wrdoneidx] = NULL;
992 if (reg->pfu_enabled) {
993 desc2 = (void *)card->txbd_ring[wrdoneidx];
994 memset(desc2, 0, sizeof(*desc2));
995 } else {
996 desc = card->txbd_ring[wrdoneidx];
997 memset(desc, 0, sizeof(*desc));
999 switch (card->dev->device) {
1000 case PCIE_DEVICE_ID_MARVELL_88W8766P:
1001 card->txbd_rdptr++;
1002 break;
1003 case PCIE_DEVICE_ID_MARVELL_88W8897:
1004 card->txbd_rdptr += reg->ring_tx_start_ptr;
1005 break;
1009 if ((card->txbd_rdptr & reg->tx_mask) == num_tx_buffs)
1010 card->txbd_rdptr = ((card->txbd_rdptr &
1011 reg->tx_rollover_ind) ^
1012 reg->tx_rollover_ind);
1015 if (unmap_count)
1016 adapter->data_sent = false;
1018 if (card->txbd_flush) {
1019 if (mwifiex_pcie_txbd_empty(card, card->txbd_rdptr))
1020 card->txbd_flush = 0;
1021 else
1022 mwifiex_clean_pcie_ring_buf(adapter);
1025 return 0;
1028 /* This function sends data buffer to device. First 4 bytes of payload
1029 * are filled with payload length and payload type. Then this payload
1030 * is mapped to PCI device memory. Tx ring pointers are advanced accordingly.
1031 * Download ready interrupt to FW is deffered if Tx ring is not full and
1032 * additional payload can be accomodated.
1034 static int
1035 mwifiex_pcie_send_data(struct mwifiex_adapter *adapter, struct sk_buff *skb,
1036 struct mwifiex_tx_param *tx_param)
1038 struct pcie_service_card *card = adapter->card;
1039 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1040 u32 wrindx, num_tx_buffs, rx_val;
1041 int ret;
1042 dma_addr_t buf_pa;
1043 struct mwifiex_pcie_buf_desc *desc = NULL;
1044 struct mwifiex_pfu_buf_desc *desc2 = NULL;
1045 __le16 *tmp;
1047 if (!(skb->data && skb->len)) {
1048 dev_err(adapter->dev, "%s(): invalid parameter <%p, %#x>\n",
1049 __func__, skb->data, skb->len);
1050 return -1;
1053 if (!mwifiex_pcie_ok_to_access_hw(adapter))
1054 mwifiex_pm_wakeup_card(adapter);
1056 num_tx_buffs = MWIFIEX_MAX_TXRX_BD << reg->tx_start_ptr;
1057 dev_dbg(adapter->dev, "info: SEND DATA: <Rd: %#x, Wr: %#x>\n",
1058 card->txbd_rdptr, card->txbd_wrptr);
1059 if (mwifiex_pcie_txbd_not_full(card)) {
1060 u8 *payload;
1062 adapter->data_sent = true;
1063 payload = skb->data;
1064 tmp = (__le16 *)&payload[0];
1065 *tmp = cpu_to_le16((u16)skb->len);
1066 tmp = (__le16 *)&payload[2];
1067 *tmp = cpu_to_le16(MWIFIEX_TYPE_DATA);
1069 if (mwifiex_map_pci_memory(adapter, skb, skb->len ,
1070 PCI_DMA_TODEVICE))
1071 return -1;
1073 wrindx = (card->txbd_wrptr & reg->tx_mask) >> reg->tx_start_ptr;
1074 MWIFIEX_SKB_PACB(skb, &buf_pa);
1075 card->tx_buf_list[wrindx] = skb;
1077 if (reg->pfu_enabled) {
1078 desc2 = (void *)card->txbd_ring[wrindx];
1079 desc2->paddr = buf_pa;
1080 desc2->len = (u16)skb->len;
1081 desc2->frag_len = (u16)skb->len;
1082 desc2->offset = 0;
1083 desc2->flags = MWIFIEX_BD_FLAG_FIRST_DESC |
1084 MWIFIEX_BD_FLAG_LAST_DESC;
1085 } else {
1086 desc = card->txbd_ring[wrindx];
1087 desc->paddr = buf_pa;
1088 desc->len = (u16)skb->len;
1089 desc->flags = MWIFIEX_BD_FLAG_FIRST_DESC |
1090 MWIFIEX_BD_FLAG_LAST_DESC;
1093 switch (card->dev->device) {
1094 case PCIE_DEVICE_ID_MARVELL_88W8766P:
1095 card->txbd_wrptr++;
1096 break;
1097 case PCIE_DEVICE_ID_MARVELL_88W8897:
1098 card->txbd_wrptr += reg->ring_tx_start_ptr;
1099 break;
1102 if ((card->txbd_wrptr & reg->tx_mask) == num_tx_buffs)
1103 card->txbd_wrptr = ((card->txbd_wrptr &
1104 reg->tx_rollover_ind) ^
1105 reg->tx_rollover_ind);
1107 rx_val = card->rxbd_rdptr & reg->rx_wrap_mask;
1108 /* Write the TX ring write pointer in to reg->tx_wrptr */
1109 if (mwifiex_write_reg(adapter, reg->tx_wrptr,
1110 card->txbd_wrptr | rx_val)) {
1111 dev_err(adapter->dev,
1112 "SEND DATA: failed to write reg->tx_wrptr\n");
1113 ret = -1;
1114 goto done_unmap;
1116 if ((mwifiex_pcie_txbd_not_full(card)) &&
1117 tx_param->next_pkt_len) {
1118 /* have more packets and TxBD still can hold more */
1119 dev_dbg(adapter->dev,
1120 "SEND DATA: delay dnld-rdy interrupt.\n");
1121 adapter->data_sent = false;
1122 } else {
1123 /* Send the TX ready interrupt */
1124 if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
1125 CPU_INTR_DNLD_RDY)) {
1126 dev_err(adapter->dev,
1127 "SEND DATA: failed to assert dnld-rdy interrupt.\n");
1128 ret = -1;
1129 goto done_unmap;
1132 dev_dbg(adapter->dev, "info: SEND DATA: Updated <Rd: %#x, Wr: "
1133 "%#x> and sent packet to firmware successfully\n",
1134 card->txbd_rdptr, card->txbd_wrptr);
1135 } else {
1136 dev_dbg(adapter->dev,
1137 "info: TX Ring full, can't send packets to fw\n");
1138 adapter->data_sent = true;
1139 /* Send the TX ready interrupt */
1140 if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
1141 CPU_INTR_DNLD_RDY))
1142 dev_err(adapter->dev,
1143 "SEND DATA: failed to assert door-bell intr\n");
1144 return -EBUSY;
1147 return -EINPROGRESS;
1148 done_unmap:
1149 MWIFIEX_SKB_PACB(skb, &buf_pa);
1150 pci_unmap_single(card->dev, buf_pa, skb->len, PCI_DMA_TODEVICE);
1151 card->tx_buf_list[wrindx] = NULL;
1152 if (reg->pfu_enabled)
1153 memset(desc2, 0, sizeof(*desc2));
1154 else
1155 memset(desc, 0, sizeof(*desc));
1157 return ret;
1161 * This function handles received buffer ring and
1162 * dispatches packets to upper
1164 static int mwifiex_pcie_process_recv_data(struct mwifiex_adapter *adapter)
1166 struct pcie_service_card *card = adapter->card;
1167 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1168 u32 wrptr, rd_index, tx_val;
1169 dma_addr_t buf_pa;
1170 int ret = 0;
1171 struct sk_buff *skb_tmp = NULL;
1172 struct mwifiex_pcie_buf_desc *desc;
1173 struct mwifiex_pfu_buf_desc *desc2;
1175 if (!mwifiex_pcie_ok_to_access_hw(adapter))
1176 mwifiex_pm_wakeup_card(adapter);
1178 /* Read the RX ring Write pointer set by firmware */
1179 if (mwifiex_read_reg(adapter, reg->rx_wrptr, &wrptr)) {
1180 dev_err(adapter->dev,
1181 "RECV DATA: failed to read reg->rx_wrptr\n");
1182 ret = -1;
1183 goto done;
1185 card->rxbd_wrptr = wrptr;
1187 while (((wrptr & reg->rx_mask) !=
1188 (card->rxbd_rdptr & reg->rx_mask)) ||
1189 ((wrptr & reg->rx_rollover_ind) ==
1190 (card->rxbd_rdptr & reg->rx_rollover_ind))) {
1191 struct sk_buff *skb_data;
1192 u16 rx_len;
1193 __le16 pkt_len;
1195 rd_index = card->rxbd_rdptr & reg->rx_mask;
1196 skb_data = card->rx_buf_list[rd_index];
1198 MWIFIEX_SKB_PACB(skb_data, &buf_pa);
1199 pci_unmap_single(card->dev, buf_pa, MWIFIEX_RX_DATA_BUF_SIZE,
1200 PCI_DMA_FROMDEVICE);
1201 card->rx_buf_list[rd_index] = NULL;
1203 /* Get data length from interface header -
1204 * first 2 bytes for len, next 2 bytes is for type
1206 pkt_len = *((__le16 *)skb_data->data);
1207 rx_len = le16_to_cpu(pkt_len);
1208 skb_put(skb_data, rx_len);
1209 dev_dbg(adapter->dev,
1210 "info: RECV DATA: Rd=%#x, Wr=%#x, Len=%d\n",
1211 card->rxbd_rdptr, wrptr, rx_len);
1212 skb_pull(skb_data, INTF_HEADER_LEN);
1213 mwifiex_handle_rx_packet(adapter, skb_data);
1215 skb_tmp = dev_alloc_skb(MWIFIEX_RX_DATA_BUF_SIZE);
1216 if (!skb_tmp) {
1217 dev_err(adapter->dev,
1218 "Unable to allocate skb.\n");
1219 return -ENOMEM;
1222 if (mwifiex_map_pci_memory(adapter, skb_tmp,
1223 MWIFIEX_RX_DATA_BUF_SIZE,
1224 PCI_DMA_FROMDEVICE))
1225 return -1;
1227 MWIFIEX_SKB_PACB(skb_tmp, &buf_pa);
1229 dev_dbg(adapter->dev,
1230 "RECV DATA: Attach new sk_buff %p at rxbd_rdidx=%d\n",
1231 skb_tmp, rd_index);
1232 card->rx_buf_list[rd_index] = skb_tmp;
1234 if (reg->pfu_enabled) {
1235 desc2 = (void *)card->rxbd_ring[rd_index];
1236 desc2->paddr = buf_pa;
1237 desc2->len = skb_tmp->len;
1238 desc2->frag_len = skb_tmp->len;
1239 desc2->offset = 0;
1240 desc2->flags = reg->ring_flag_sop | reg->ring_flag_eop;
1241 } else {
1242 desc = card->rxbd_ring[rd_index];
1243 desc->paddr = buf_pa;
1244 desc->len = skb_tmp->len;
1245 desc->flags = 0;
1248 if ((++card->rxbd_rdptr & reg->rx_mask) ==
1249 MWIFIEX_MAX_TXRX_BD) {
1250 card->rxbd_rdptr = ((card->rxbd_rdptr &
1251 reg->rx_rollover_ind) ^
1252 reg->rx_rollover_ind);
1254 dev_dbg(adapter->dev, "info: RECV DATA: <Rd: %#x, Wr: %#x>\n",
1255 card->rxbd_rdptr, wrptr);
1257 tx_val = card->txbd_wrptr & reg->tx_wrap_mask;
1258 /* Write the RX ring read pointer in to reg->rx_rdptr */
1259 if (mwifiex_write_reg(adapter, reg->rx_rdptr,
1260 card->rxbd_rdptr | tx_val)) {
1261 dev_err(adapter->dev,
1262 "RECV DATA: failed to write reg->rx_rdptr\n");
1263 ret = -1;
1264 goto done;
1267 /* Read the RX ring Write pointer set by firmware */
1268 if (mwifiex_read_reg(adapter, reg->rx_wrptr, &wrptr)) {
1269 dev_err(adapter->dev,
1270 "RECV DATA: failed to read reg->rx_wrptr\n");
1271 ret = -1;
1272 goto done;
1274 dev_dbg(adapter->dev,
1275 "info: RECV DATA: Rcvd packet from fw successfully\n");
1276 card->rxbd_wrptr = wrptr;
1279 done:
1280 return ret;
1284 * This function downloads the boot command to device
1286 static int
1287 mwifiex_pcie_send_boot_cmd(struct mwifiex_adapter *adapter, struct sk_buff *skb)
1289 dma_addr_t buf_pa;
1290 struct pcie_service_card *card = adapter->card;
1291 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1293 if (!(skb->data && skb->len)) {
1294 dev_err(adapter->dev,
1295 "Invalid parameter in %s <%p. len %d>\n",
1296 __func__, skb->data, skb->len);
1297 return -1;
1300 if (mwifiex_map_pci_memory(adapter, skb, skb->len , PCI_DMA_TODEVICE))
1301 return -1;
1303 MWIFIEX_SKB_PACB(skb, &buf_pa);
1305 /* Write the lower 32bits of the physical address to low command
1306 * address scratch register
1308 if (mwifiex_write_reg(adapter, reg->cmd_addr_lo, (u32)buf_pa)) {
1309 dev_err(adapter->dev,
1310 "%s: failed to write download command to boot code.\n",
1311 __func__);
1312 pci_unmap_single(card->dev, buf_pa, MWIFIEX_UPLD_SIZE,
1313 PCI_DMA_TODEVICE);
1314 return -1;
1317 /* Write the upper 32bits of the physical address to high command
1318 * address scratch register
1320 if (mwifiex_write_reg(adapter, reg->cmd_addr_hi,
1321 (u32)((u64)buf_pa >> 32))) {
1322 dev_err(adapter->dev,
1323 "%s: failed to write download command to boot code.\n",
1324 __func__);
1325 pci_unmap_single(card->dev, buf_pa, MWIFIEX_UPLD_SIZE,
1326 PCI_DMA_TODEVICE);
1327 return -1;
1330 /* Write the command length to cmd_size scratch register */
1331 if (mwifiex_write_reg(adapter, reg->cmd_size, skb->len)) {
1332 dev_err(adapter->dev,
1333 "%s: failed to write command len to cmd_size scratch reg\n",
1334 __func__);
1335 pci_unmap_single(card->dev, buf_pa, MWIFIEX_UPLD_SIZE,
1336 PCI_DMA_TODEVICE);
1337 return -1;
1340 /* Ring the door bell */
1341 if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
1342 CPU_INTR_DOOR_BELL)) {
1343 dev_err(adapter->dev,
1344 "%s: failed to assert door-bell intr\n", __func__);
1345 pci_unmap_single(card->dev, buf_pa,
1346 MWIFIEX_UPLD_SIZE, PCI_DMA_TODEVICE);
1347 return -1;
1350 return 0;
1353 /* This function init rx port in firmware which in turn enables to receive data
1354 * from device before transmitting any packet.
1356 static int mwifiex_pcie_init_fw_port(struct mwifiex_adapter *adapter)
1358 struct pcie_service_card *card = adapter->card;
1359 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1360 int tx_wrap = card->txbd_wrptr & reg->tx_wrap_mask;
1362 /* Write the RX ring read pointer in to reg->rx_rdptr */
1363 if (mwifiex_write_reg(adapter, reg->rx_rdptr, card->rxbd_rdptr |
1364 tx_wrap)) {
1365 dev_err(adapter->dev,
1366 "RECV DATA: failed to write reg->rx_rdptr\n");
1367 return -1;
1369 return 0;
1372 /* This function downloads commands to the device
1374 static int
1375 mwifiex_pcie_send_cmd(struct mwifiex_adapter *adapter, struct sk_buff *skb)
1377 struct pcie_service_card *card = adapter->card;
1378 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1379 int ret = 0;
1380 dma_addr_t cmd_buf_pa, cmdrsp_buf_pa;
1381 u8 *payload = (u8 *)skb->data;
1383 if (!(skb->data && skb->len)) {
1384 dev_err(adapter->dev, "Invalid parameter in %s <%p, %#x>\n",
1385 __func__, skb->data, skb->len);
1386 return -1;
1389 /* Make sure a command response buffer is available */
1390 if (!card->cmdrsp_buf) {
1391 dev_err(adapter->dev,
1392 "No response buffer available, send command failed\n");
1393 return -EBUSY;
1396 if (!mwifiex_pcie_ok_to_access_hw(adapter))
1397 mwifiex_pm_wakeup_card(adapter);
1399 adapter->cmd_sent = true;
1401 *(__le16 *)&payload[0] = cpu_to_le16((u16)skb->len);
1402 *(__le16 *)&payload[2] = cpu_to_le16(MWIFIEX_TYPE_CMD);
1404 if (mwifiex_map_pci_memory(adapter, skb, skb->len, PCI_DMA_TODEVICE))
1405 return -1;
1407 card->cmd_buf = skb;
1409 /* To send a command, the driver will:
1410 1. Write the 64bit physical address of the data buffer to
1411 cmd response address low + cmd response address high
1412 2. Ring the door bell (i.e. set the door bell interrupt)
1414 In response to door bell interrupt, the firmware will perform
1415 the DMA of the command packet (first header to obtain the total
1416 length and then rest of the command).
1419 if (card->cmdrsp_buf) {
1420 MWIFIEX_SKB_PACB(card->cmdrsp_buf, &cmdrsp_buf_pa);
1421 /* Write the lower 32bits of the cmdrsp buffer physical
1422 address */
1423 if (mwifiex_write_reg(adapter, reg->cmdrsp_addr_lo,
1424 (u32)cmdrsp_buf_pa)) {
1425 dev_err(adapter->dev,
1426 "Failed to write download cmd to boot code.\n");
1427 ret = -1;
1428 goto done;
1430 /* Write the upper 32bits of the cmdrsp buffer physical
1431 address */
1432 if (mwifiex_write_reg(adapter, reg->cmdrsp_addr_hi,
1433 (u32)((u64)cmdrsp_buf_pa >> 32))) {
1434 dev_err(adapter->dev,
1435 "Failed to write download cmd to boot code.\n");
1436 ret = -1;
1437 goto done;
1441 MWIFIEX_SKB_PACB(card->cmd_buf, &cmd_buf_pa);
1442 /* Write the lower 32bits of the physical address to reg->cmd_addr_lo */
1443 if (mwifiex_write_reg(adapter, reg->cmd_addr_lo,
1444 (u32)cmd_buf_pa)) {
1445 dev_err(adapter->dev,
1446 "Failed to write download cmd to boot code.\n");
1447 ret = -1;
1448 goto done;
1450 /* Write the upper 32bits of the physical address to reg->cmd_addr_hi */
1451 if (mwifiex_write_reg(adapter, reg->cmd_addr_hi,
1452 (u32)((u64)cmd_buf_pa >> 32))) {
1453 dev_err(adapter->dev,
1454 "Failed to write download cmd to boot code.\n");
1455 ret = -1;
1456 goto done;
1459 /* Write the command length to reg->cmd_size */
1460 if (mwifiex_write_reg(adapter, reg->cmd_size,
1461 card->cmd_buf->len)) {
1462 dev_err(adapter->dev,
1463 "Failed to write cmd len to reg->cmd_size\n");
1464 ret = -1;
1465 goto done;
1468 /* Ring the door bell */
1469 if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
1470 CPU_INTR_DOOR_BELL)) {
1471 dev_err(adapter->dev,
1472 "Failed to assert door-bell intr\n");
1473 ret = -1;
1474 goto done;
1477 done:
1478 if (ret)
1479 adapter->cmd_sent = false;
1481 return 0;
1485 * This function handles command complete interrupt
1487 static int mwifiex_pcie_process_cmd_complete(struct mwifiex_adapter *adapter)
1489 struct pcie_service_card *card = adapter->card;
1490 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1491 struct sk_buff *skb = card->cmdrsp_buf;
1492 int count = 0;
1493 u16 rx_len;
1494 __le16 pkt_len;
1495 dma_addr_t buf_pa;
1497 dev_dbg(adapter->dev, "info: Rx CMD Response\n");
1499 MWIFIEX_SKB_PACB(skb, &buf_pa);
1500 pci_unmap_single(card->dev, buf_pa, MWIFIEX_UPLD_SIZE,
1501 PCI_DMA_FROMDEVICE);
1503 pkt_len = *((__le16 *)skb->data);
1504 rx_len = le16_to_cpu(pkt_len);
1505 skb_trim(skb, rx_len);
1506 skb_pull(skb, INTF_HEADER_LEN);
1508 if (!adapter->curr_cmd) {
1509 if (adapter->ps_state == PS_STATE_SLEEP_CFM) {
1510 mwifiex_process_sleep_confirm_resp(adapter, skb->data,
1511 skb->len);
1512 while (reg->sleep_cookie && (count++ < 10) &&
1513 mwifiex_pcie_ok_to_access_hw(adapter))
1514 usleep_range(50, 60);
1515 } else {
1516 dev_err(adapter->dev,
1517 "There is no command but got cmdrsp\n");
1519 memcpy(adapter->upld_buf, skb->data,
1520 min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER, skb->len));
1521 skb_push(skb, INTF_HEADER_LEN);
1522 if (mwifiex_map_pci_memory(adapter, skb, MWIFIEX_UPLD_SIZE,
1523 PCI_DMA_FROMDEVICE))
1524 return -1;
1526 MWIFIEX_SKB_PACB(skb, &buf_pa);
1527 } else if (mwifiex_pcie_ok_to_access_hw(adapter)) {
1528 adapter->curr_cmd->resp_skb = skb;
1529 adapter->cmd_resp_received = true;
1530 /* Take the pointer and set it to CMD node and will
1531 return in the response complete callback */
1532 card->cmdrsp_buf = NULL;
1534 /* Clear the cmd-rsp buffer address in scratch registers. This
1535 will prevent firmware from writing to the same response
1536 buffer again. */
1537 if (mwifiex_write_reg(adapter, reg->cmdrsp_addr_lo, 0)) {
1538 dev_err(adapter->dev,
1539 "cmd_done: failed to clear cmd_rsp_addr_lo\n");
1540 return -1;
1542 /* Write the upper 32bits of the cmdrsp buffer physical
1543 address */
1544 if (mwifiex_write_reg(adapter, reg->cmdrsp_addr_hi, 0)) {
1545 dev_err(adapter->dev,
1546 "cmd_done: failed to clear cmd_rsp_addr_hi\n");
1547 return -1;
1551 return 0;
1555 * Command Response processing complete handler
1557 static int mwifiex_pcie_cmdrsp_complete(struct mwifiex_adapter *adapter,
1558 struct sk_buff *skb)
1560 struct pcie_service_card *card = adapter->card;
1561 dma_addr_t buf_pa;
1562 struct sk_buff *skb_tmp;
1564 if (skb) {
1565 card->cmdrsp_buf = skb;
1566 skb_push(card->cmdrsp_buf, INTF_HEADER_LEN);
1567 if (mwifiex_map_pci_memory(adapter, skb, MWIFIEX_UPLD_SIZE,
1568 PCI_DMA_FROMDEVICE))
1569 return -1;
1572 skb_tmp = card->cmd_buf;
1573 if (skb_tmp) {
1574 MWIFIEX_SKB_PACB(skb_tmp, &buf_pa);
1575 pci_unmap_single(card->dev, buf_pa, skb_tmp->len,
1576 PCI_DMA_FROMDEVICE);
1577 card->cmd_buf = NULL;
1580 return 0;
1584 * This function handles firmware event ready interrupt
1586 static int mwifiex_pcie_process_event_ready(struct mwifiex_adapter *adapter)
1588 struct pcie_service_card *card = adapter->card;
1589 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1590 u32 rdptr = card->evtbd_rdptr & MWIFIEX_EVTBD_MASK;
1591 u32 wrptr, event;
1592 dma_addr_t buf_pa;
1593 struct mwifiex_evt_buf_desc *desc;
1595 if (!mwifiex_pcie_ok_to_access_hw(adapter))
1596 mwifiex_pm_wakeup_card(adapter);
1598 if (adapter->event_received) {
1599 dev_dbg(adapter->dev, "info: Event being processed, "
1600 "do not process this interrupt just yet\n");
1601 return 0;
1604 if (rdptr >= MWIFIEX_MAX_EVT_BD) {
1605 dev_dbg(adapter->dev, "info: Invalid read pointer...\n");
1606 return -1;
1609 /* Read the event ring write pointer set by firmware */
1610 if (mwifiex_read_reg(adapter, reg->evt_wrptr, &wrptr)) {
1611 dev_err(adapter->dev,
1612 "EventReady: failed to read reg->evt_wrptr\n");
1613 return -1;
1616 dev_dbg(adapter->dev, "info: EventReady: Initial <Rd: 0x%x, Wr: 0x%x>",
1617 card->evtbd_rdptr, wrptr);
1618 if (((wrptr & MWIFIEX_EVTBD_MASK) != (card->evtbd_rdptr
1619 & MWIFIEX_EVTBD_MASK)) ||
1620 ((wrptr & reg->evt_rollover_ind) ==
1621 (card->evtbd_rdptr & reg->evt_rollover_ind))) {
1622 struct sk_buff *skb_cmd;
1623 __le16 data_len = 0;
1624 u16 evt_len;
1626 dev_dbg(adapter->dev, "info: Read Index: %d\n", rdptr);
1627 skb_cmd = card->evt_buf_list[rdptr];
1628 MWIFIEX_SKB_PACB(skb_cmd, &buf_pa);
1629 pci_unmap_single(card->dev, buf_pa, MAX_EVENT_SIZE,
1630 PCI_DMA_FROMDEVICE);
1632 /* Take the pointer and set it to event pointer in adapter
1633 and will return back after event handling callback */
1634 card->evt_buf_list[rdptr] = NULL;
1635 desc = card->evtbd_ring[rdptr];
1636 memset(desc, 0, sizeof(*desc));
1638 event = *(u32 *) &skb_cmd->data[INTF_HEADER_LEN];
1639 adapter->event_cause = event;
1640 /* The first 4bytes will be the event transfer header
1641 len is 2 bytes followed by type which is 2 bytes */
1642 memcpy(&data_len, skb_cmd->data, sizeof(__le16));
1643 evt_len = le16_to_cpu(data_len);
1645 skb_pull(skb_cmd, INTF_HEADER_LEN);
1646 dev_dbg(adapter->dev, "info: Event length: %d\n", evt_len);
1648 if ((evt_len > 0) && (evt_len < MAX_EVENT_SIZE))
1649 memcpy(adapter->event_body, skb_cmd->data +
1650 MWIFIEX_EVENT_HEADER_LEN, evt_len -
1651 MWIFIEX_EVENT_HEADER_LEN);
1653 adapter->event_received = true;
1654 adapter->event_skb = skb_cmd;
1656 /* Do not update the event read pointer here, wait till the
1657 buffer is released. This is just to make things simpler,
1658 we need to find a better method of managing these buffers.
1662 return 0;
1666 * Event processing complete handler
1668 static int mwifiex_pcie_event_complete(struct mwifiex_adapter *adapter,
1669 struct sk_buff *skb)
1671 struct pcie_service_card *card = adapter->card;
1672 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1673 int ret = 0;
1674 u32 rdptr = card->evtbd_rdptr & MWIFIEX_EVTBD_MASK;
1675 u32 wrptr;
1676 dma_addr_t buf_pa;
1677 struct mwifiex_evt_buf_desc *desc;
1679 if (!skb)
1680 return 0;
1682 if (rdptr >= MWIFIEX_MAX_EVT_BD) {
1683 dev_err(adapter->dev, "event_complete: Invalid rdptr 0x%x\n",
1684 rdptr);
1685 return -EINVAL;
1688 /* Read the event ring write pointer set by firmware */
1689 if (mwifiex_read_reg(adapter, reg->evt_wrptr, &wrptr)) {
1690 dev_err(adapter->dev,
1691 "event_complete: failed to read reg->evt_wrptr\n");
1692 return -1;
1695 if (!card->evt_buf_list[rdptr]) {
1696 skb_push(skb, INTF_HEADER_LEN);
1697 if (mwifiex_map_pci_memory(adapter, skb,
1698 MAX_EVENT_SIZE,
1699 PCI_DMA_FROMDEVICE))
1700 return -1;
1701 MWIFIEX_SKB_PACB(skb, &buf_pa);
1702 card->evt_buf_list[rdptr] = skb;
1703 MWIFIEX_SKB_PACB(skb, &buf_pa);
1704 desc = card->evtbd_ring[rdptr];
1705 desc->paddr = buf_pa;
1706 desc->len = (u16)skb->len;
1707 desc->flags = 0;
1708 skb = NULL;
1709 } else {
1710 dev_dbg(adapter->dev,
1711 "info: ERROR: buf still valid at index %d, <%p, %p>\n",
1712 rdptr, card->evt_buf_list[rdptr], skb);
1715 if ((++card->evtbd_rdptr & MWIFIEX_EVTBD_MASK) == MWIFIEX_MAX_EVT_BD) {
1716 card->evtbd_rdptr = ((card->evtbd_rdptr &
1717 reg->evt_rollover_ind) ^
1718 reg->evt_rollover_ind);
1721 dev_dbg(adapter->dev, "info: Updated <Rd: 0x%x, Wr: 0x%x>",
1722 card->evtbd_rdptr, wrptr);
1724 /* Write the event ring read pointer in to reg->evt_rdptr */
1725 if (mwifiex_write_reg(adapter, reg->evt_rdptr,
1726 card->evtbd_rdptr)) {
1727 dev_err(adapter->dev,
1728 "event_complete: failed to read reg->evt_rdptr\n");
1729 return -1;
1732 dev_dbg(adapter->dev, "info: Check Events Again\n");
1733 ret = mwifiex_pcie_process_event_ready(adapter);
1735 return ret;
1739 * This function downloads the firmware to the card.
1741 * Firmware is downloaded to the card in blocks. Every block download
1742 * is tested for CRC errors, and retried a number of times before
1743 * returning failure.
1745 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
1746 struct mwifiex_fw_image *fw)
1748 int ret;
1749 u8 *firmware = fw->fw_buf;
1750 u32 firmware_len = fw->fw_len;
1751 u32 offset = 0;
1752 struct sk_buff *skb;
1753 u32 txlen, tx_blocks = 0, tries, len;
1754 u32 block_retry_cnt = 0;
1755 dma_addr_t buf_pa;
1756 struct pcie_service_card *card = adapter->card;
1757 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1759 if (!firmware || !firmware_len) {
1760 dev_err(adapter->dev,
1761 "No firmware image found! Terminating download\n");
1762 return -1;
1765 dev_dbg(adapter->dev, "info: Downloading FW image (%d bytes)\n",
1766 firmware_len);
1768 if (mwifiex_pcie_disable_host_int(adapter)) {
1769 dev_err(adapter->dev,
1770 "%s: Disabling interrupts failed.\n", __func__);
1771 return -1;
1774 skb = dev_alloc_skb(MWIFIEX_UPLD_SIZE);
1775 if (!skb) {
1776 ret = -ENOMEM;
1777 goto done;
1780 /* Perform firmware data transfer */
1781 do {
1782 u32 ireg_intr = 0;
1784 /* More data? */
1785 if (offset >= firmware_len)
1786 break;
1788 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
1789 ret = mwifiex_read_reg(adapter, reg->cmd_size,
1790 &len);
1791 if (ret) {
1792 dev_warn(adapter->dev,
1793 "Failed reading len from boot code\n");
1794 goto done;
1796 if (len)
1797 break;
1798 usleep_range(10, 20);
1801 if (!len) {
1802 break;
1803 } else if (len > MWIFIEX_UPLD_SIZE) {
1804 pr_err("FW download failure @ %d, invalid length %d\n",
1805 offset, len);
1806 ret = -1;
1807 goto done;
1810 txlen = len;
1812 if (len & BIT(0)) {
1813 block_retry_cnt++;
1814 if (block_retry_cnt > MAX_WRITE_IOMEM_RETRY) {
1815 pr_err("FW download failure @ %d, over max "
1816 "retry count\n", offset);
1817 ret = -1;
1818 goto done;
1820 dev_err(adapter->dev, "FW CRC error indicated by the "
1821 "helper: len = 0x%04X, txlen = %d\n",
1822 len, txlen);
1823 len &= ~BIT(0);
1824 /* Setting this to 0 to resend from same offset */
1825 txlen = 0;
1826 } else {
1827 block_retry_cnt = 0;
1828 /* Set blocksize to transfer - checking for
1829 last block */
1830 if (firmware_len - offset < txlen)
1831 txlen = firmware_len - offset;
1833 dev_dbg(adapter->dev, ".");
1835 tx_blocks = (txlen + card->pcie.blksz_fw_dl - 1) /
1836 card->pcie.blksz_fw_dl;
1838 /* Copy payload to buffer */
1839 memmove(skb->data, &firmware[offset], txlen);
1842 skb_put(skb, MWIFIEX_UPLD_SIZE - skb->len);
1843 skb_trim(skb, tx_blocks * card->pcie.blksz_fw_dl);
1845 /* Send the boot command to device */
1846 if (mwifiex_pcie_send_boot_cmd(adapter, skb)) {
1847 dev_err(adapter->dev,
1848 "Failed to send firmware download command\n");
1849 ret = -1;
1850 goto done;
1853 MWIFIEX_SKB_PACB(skb, &buf_pa);
1855 /* Wait for the command done interrupt */
1856 do {
1857 if (mwifiex_read_reg(adapter, PCIE_CPU_INT_STATUS,
1858 &ireg_intr)) {
1859 dev_err(adapter->dev, "%s: Failed to read "
1860 "interrupt status during fw dnld.\n",
1861 __func__);
1862 pci_unmap_single(card->dev, buf_pa, skb->len,
1863 PCI_DMA_TODEVICE);
1864 ret = -1;
1865 goto done;
1867 } while ((ireg_intr & CPU_INTR_DOOR_BELL) ==
1868 CPU_INTR_DOOR_BELL);
1870 pci_unmap_single(card->dev, buf_pa, skb->len,
1871 PCI_DMA_TODEVICE);
1873 offset += txlen;
1874 } while (true);
1876 dev_dbg(adapter->dev, "info:\nFW download over, size %d bytes\n",
1877 offset);
1879 ret = 0;
1881 done:
1882 dev_kfree_skb_any(skb);
1883 return ret;
1887 * This function checks the firmware status in card.
1889 * The winner interface is also determined by this function.
1891 static int
1892 mwifiex_check_fw_status(struct mwifiex_adapter *adapter, u32 poll_num)
1894 int ret = 0;
1895 u32 firmware_stat, winner_status;
1896 struct pcie_service_card *card = adapter->card;
1897 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1898 u32 tries;
1900 /* Mask spurios interrupts */
1901 if (mwifiex_write_reg(adapter, PCIE_HOST_INT_STATUS_MASK,
1902 HOST_INTR_MASK)) {
1903 dev_warn(adapter->dev, "Write register failed\n");
1904 return -1;
1907 dev_dbg(adapter->dev, "Setting driver ready signature\n");
1908 if (mwifiex_write_reg(adapter, reg->drv_rdy,
1909 FIRMWARE_READY_PCIE)) {
1910 dev_err(adapter->dev,
1911 "Failed to write driver ready signature\n");
1912 return -1;
1915 /* Wait for firmware initialization event */
1916 for (tries = 0; tries < poll_num; tries++) {
1917 if (mwifiex_read_reg(adapter, reg->fw_status,
1918 &firmware_stat))
1919 ret = -1;
1920 else
1921 ret = 0;
1922 if (ret)
1923 continue;
1924 if (firmware_stat == FIRMWARE_READY_PCIE) {
1925 ret = 0;
1926 break;
1927 } else {
1928 mdelay(100);
1929 ret = -1;
1933 if (ret) {
1934 if (mwifiex_read_reg(adapter, reg->fw_status,
1935 &winner_status))
1936 ret = -1;
1937 else if (!winner_status) {
1938 dev_err(adapter->dev, "PCI-E is the winner\n");
1939 adapter->winner = 1;
1940 ret = -1;
1941 } else {
1942 dev_err(adapter->dev,
1943 "PCI-E is not the winner <%#x,%d>, exit dnld\n",
1944 ret, adapter->winner);
1945 ret = 0;
1949 return ret;
1953 * This function reads the interrupt status from card.
1955 static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
1957 u32 pcie_ireg;
1958 unsigned long flags;
1960 if (!mwifiex_pcie_ok_to_access_hw(adapter))
1961 return;
1963 if (mwifiex_read_reg(adapter, PCIE_HOST_INT_STATUS, &pcie_ireg)) {
1964 dev_warn(adapter->dev, "Read register failed\n");
1965 return;
1968 if ((pcie_ireg != 0xFFFFFFFF) && (pcie_ireg)) {
1970 mwifiex_pcie_disable_host_int(adapter);
1972 /* Clear the pending interrupts */
1973 if (mwifiex_write_reg(adapter, PCIE_HOST_INT_STATUS,
1974 ~pcie_ireg)) {
1975 dev_warn(adapter->dev, "Write register failed\n");
1976 return;
1978 spin_lock_irqsave(&adapter->int_lock, flags);
1979 adapter->int_status |= pcie_ireg;
1980 spin_unlock_irqrestore(&adapter->int_lock, flags);
1982 if (pcie_ireg & HOST_INTR_CMD_DONE) {
1983 if ((adapter->ps_state == PS_STATE_SLEEP_CFM) ||
1984 (adapter->ps_state == PS_STATE_SLEEP)) {
1985 mwifiex_pcie_enable_host_int(adapter);
1986 if (mwifiex_write_reg(adapter,
1987 PCIE_CPU_INT_EVENT,
1988 CPU_INTR_SLEEP_CFM_DONE)
1990 dev_warn(adapter->dev,
1991 "Write register failed\n");
1992 return;
1996 } else if (!adapter->pps_uapsd_mode &&
1997 adapter->ps_state == PS_STATE_SLEEP &&
1998 mwifiex_pcie_ok_to_access_hw(adapter)) {
1999 /* Potentially for PCIe we could get other
2000 * interrupts like shared. Don't change power
2001 * state until cookie is set */
2002 adapter->ps_state = PS_STATE_AWAKE;
2003 adapter->pm_wakeup_fw_try = false;
2009 * Interrupt handler for PCIe root port
2011 * This function reads the interrupt status from firmware and assigns
2012 * the main process in workqueue which will handle the interrupt.
2014 static irqreturn_t mwifiex_pcie_interrupt(int irq, void *context)
2016 struct pci_dev *pdev = (struct pci_dev *)context;
2017 struct pcie_service_card *card;
2018 struct mwifiex_adapter *adapter;
2020 if (!pdev) {
2021 pr_debug("info: %s: pdev is NULL\n", (u8 *)pdev);
2022 goto exit;
2025 card = (struct pcie_service_card *) pci_get_drvdata(pdev);
2026 if (!card || !card->adapter) {
2027 pr_debug("info: %s: card=%p adapter=%p\n", __func__, card,
2028 card ? card->adapter : NULL);
2029 goto exit;
2031 adapter = card->adapter;
2033 if (adapter->surprise_removed)
2034 goto exit;
2036 mwifiex_interrupt_status(adapter);
2037 queue_work(adapter->workqueue, &adapter->main_work);
2039 exit:
2040 return IRQ_HANDLED;
2044 * This function checks the current interrupt status.
2046 * The following interrupts are checked and handled by this function -
2047 * - Data sent
2048 * - Command sent
2049 * - Command received
2050 * - Packets received
2051 * - Events received
2053 * In case of Rx packets received, the packets are uploaded from card to
2054 * host and processed accordingly.
2056 static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
2058 int ret;
2059 u32 pcie_ireg;
2060 unsigned long flags;
2062 spin_lock_irqsave(&adapter->int_lock, flags);
2063 /* Clear out unused interrupts */
2064 pcie_ireg = adapter->int_status;
2065 adapter->int_status = 0;
2066 spin_unlock_irqrestore(&adapter->int_lock, flags);
2068 while (pcie_ireg & HOST_INTR_MASK) {
2069 if (pcie_ireg & HOST_INTR_DNLD_DONE) {
2070 pcie_ireg &= ~HOST_INTR_DNLD_DONE;
2071 dev_dbg(adapter->dev, "info: TX DNLD Done\n");
2072 ret = mwifiex_pcie_send_data_complete(adapter);
2073 if (ret)
2074 return ret;
2076 if (pcie_ireg & HOST_INTR_UPLD_RDY) {
2077 pcie_ireg &= ~HOST_INTR_UPLD_RDY;
2078 dev_dbg(adapter->dev, "info: Rx DATA\n");
2079 ret = mwifiex_pcie_process_recv_data(adapter);
2080 if (ret)
2081 return ret;
2083 if (pcie_ireg & HOST_INTR_EVENT_RDY) {
2084 pcie_ireg &= ~HOST_INTR_EVENT_RDY;
2085 dev_dbg(adapter->dev, "info: Rx EVENT\n");
2086 ret = mwifiex_pcie_process_event_ready(adapter);
2087 if (ret)
2088 return ret;
2091 if (pcie_ireg & HOST_INTR_CMD_DONE) {
2092 pcie_ireg &= ~HOST_INTR_CMD_DONE;
2093 if (adapter->cmd_sent) {
2094 dev_dbg(adapter->dev,
2095 "info: CMD sent Interrupt\n");
2096 adapter->cmd_sent = false;
2098 /* Handle command response */
2099 ret = mwifiex_pcie_process_cmd_complete(adapter);
2100 if (ret)
2101 return ret;
2104 if (mwifiex_pcie_ok_to_access_hw(adapter)) {
2105 if (mwifiex_read_reg(adapter, PCIE_HOST_INT_STATUS,
2106 &pcie_ireg)) {
2107 dev_warn(adapter->dev,
2108 "Read register failed\n");
2109 return -1;
2112 if ((pcie_ireg != 0xFFFFFFFF) && (pcie_ireg)) {
2113 if (mwifiex_write_reg(adapter,
2114 PCIE_HOST_INT_STATUS,
2115 ~pcie_ireg)) {
2116 dev_warn(adapter->dev,
2117 "Write register failed\n");
2118 return -1;
2124 dev_dbg(adapter->dev, "info: cmd_sent=%d data_sent=%d\n",
2125 adapter->cmd_sent, adapter->data_sent);
2126 if (adapter->ps_state != PS_STATE_SLEEP)
2127 mwifiex_pcie_enable_host_int(adapter);
2129 return 0;
2133 * This function downloads data from driver to card.
2135 * Both commands and data packets are transferred to the card by this
2136 * function.
2138 * This function adds the PCIE specific header to the front of the buffer
2139 * before transferring. The header contains the length of the packet and
2140 * the type. The firmware handles the packets based upon this set type.
2142 static int mwifiex_pcie_host_to_card(struct mwifiex_adapter *adapter, u8 type,
2143 struct sk_buff *skb,
2144 struct mwifiex_tx_param *tx_param)
2146 if (!skb) {
2147 dev_err(adapter->dev, "Passed NULL skb to %s\n", __func__);
2148 return -1;
2151 if (type == MWIFIEX_TYPE_DATA)
2152 return mwifiex_pcie_send_data(adapter, skb, tx_param);
2153 else if (type == MWIFIEX_TYPE_CMD)
2154 return mwifiex_pcie_send_cmd(adapter, skb);
2156 return 0;
2160 * This function initializes the PCI-E host memory space, WCB rings, etc.
2162 * The following initializations steps are followed -
2163 * - Allocate TXBD ring buffers
2164 * - Allocate RXBD ring buffers
2165 * - Allocate event BD ring buffers
2166 * - Allocate command response ring buffer
2167 * - Allocate sleep cookie buffer
2169 static int mwifiex_pcie_init(struct mwifiex_adapter *adapter)
2171 struct pcie_service_card *card = adapter->card;
2172 int ret;
2173 struct pci_dev *pdev = card->dev;
2174 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
2176 pci_set_drvdata(pdev, card);
2178 ret = pci_enable_device(pdev);
2179 if (ret)
2180 goto err_enable_dev;
2182 pci_set_master(pdev);
2184 dev_dbg(adapter->dev, "try set_consistent_dma_mask(32)\n");
2185 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2186 if (ret) {
2187 dev_err(adapter->dev, "set_dma_mask(32) failed\n");
2188 goto err_set_dma_mask;
2191 ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2192 if (ret) {
2193 dev_err(adapter->dev, "set_consistent_dma_mask(64) failed\n");
2194 goto err_set_dma_mask;
2197 ret = pci_request_region(pdev, 0, DRV_NAME);
2198 if (ret) {
2199 dev_err(adapter->dev, "req_reg(0) error\n");
2200 goto err_req_region0;
2202 card->pci_mmap = pci_iomap(pdev, 0, 0);
2203 if (!card->pci_mmap) {
2204 dev_err(adapter->dev, "iomap(0) error\n");
2205 ret = -EIO;
2206 goto err_iomap0;
2208 ret = pci_request_region(pdev, 2, DRV_NAME);
2209 if (ret) {
2210 dev_err(adapter->dev, "req_reg(2) error\n");
2211 goto err_req_region2;
2213 card->pci_mmap1 = pci_iomap(pdev, 2, 0);
2214 if (!card->pci_mmap1) {
2215 dev_err(adapter->dev, "iomap(2) error\n");
2216 ret = -EIO;
2217 goto err_iomap2;
2220 dev_dbg(adapter->dev,
2221 "PCI memory map Virt0: %p PCI memory map Virt2: %p\n",
2222 card->pci_mmap, card->pci_mmap1);
2224 card->cmdrsp_buf = NULL;
2225 ret = mwifiex_pcie_create_txbd_ring(adapter);
2226 if (ret)
2227 goto err_cre_txbd;
2228 ret = mwifiex_pcie_create_rxbd_ring(adapter);
2229 if (ret)
2230 goto err_cre_rxbd;
2231 ret = mwifiex_pcie_create_evtbd_ring(adapter);
2232 if (ret)
2233 goto err_cre_evtbd;
2234 ret = mwifiex_pcie_alloc_cmdrsp_buf(adapter);
2235 if (ret)
2236 goto err_alloc_cmdbuf;
2237 if (reg->sleep_cookie) {
2238 ret = mwifiex_pcie_alloc_sleep_cookie_buf(adapter);
2239 if (ret)
2240 goto err_alloc_cookie;
2241 } else {
2242 card->sleep_cookie_vbase = NULL;
2244 return ret;
2246 err_alloc_cookie:
2247 mwifiex_pcie_delete_cmdrsp_buf(adapter);
2248 err_alloc_cmdbuf:
2249 mwifiex_pcie_delete_evtbd_ring(adapter);
2250 err_cre_evtbd:
2251 mwifiex_pcie_delete_rxbd_ring(adapter);
2252 err_cre_rxbd:
2253 mwifiex_pcie_delete_txbd_ring(adapter);
2254 err_cre_txbd:
2255 pci_iounmap(pdev, card->pci_mmap1);
2256 err_iomap2:
2257 pci_release_region(pdev, 2);
2258 err_req_region2:
2259 pci_iounmap(pdev, card->pci_mmap);
2260 err_iomap0:
2261 pci_release_region(pdev, 0);
2262 err_req_region0:
2263 err_set_dma_mask:
2264 pci_disable_device(pdev);
2265 err_enable_dev:
2266 pci_set_drvdata(pdev, NULL);
2267 return ret;
2271 * This function cleans up the allocated card buffers.
2273 * The following are freed by this function -
2274 * - TXBD ring buffers
2275 * - RXBD ring buffers
2276 * - Event BD ring buffers
2277 * - Command response ring buffer
2278 * - Sleep cookie buffer
2280 static void mwifiex_pcie_cleanup(struct mwifiex_adapter *adapter)
2282 struct pcie_service_card *card = adapter->card;
2283 struct pci_dev *pdev = card->dev;
2284 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
2286 if (user_rmmod) {
2287 dev_dbg(adapter->dev, "Clearing driver ready signature\n");
2288 if (mwifiex_write_reg(adapter, reg->drv_rdy, 0x00000000))
2289 dev_err(adapter->dev,
2290 "Failed to write driver not-ready signature\n");
2293 if (pdev) {
2294 pci_iounmap(pdev, card->pci_mmap);
2295 pci_iounmap(pdev, card->pci_mmap1);
2296 pci_disable_device(pdev);
2297 pci_release_region(pdev, 2);
2298 pci_release_region(pdev, 0);
2299 pci_set_drvdata(pdev, NULL);
2304 * This function registers the PCIE device.
2306 * PCIE IRQ is claimed, block size is set and driver data is initialized.
2308 static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
2310 int ret;
2311 struct pcie_service_card *card = adapter->card;
2312 struct pci_dev *pdev = card->dev;
2314 /* save adapter pointer in card */
2315 card->adapter = adapter;
2317 ret = request_irq(pdev->irq, mwifiex_pcie_interrupt, IRQF_SHARED,
2318 "MRVL_PCIE", pdev);
2319 if (ret) {
2320 pr_err("request_irq failed: ret=%d\n", ret);
2321 adapter->card = NULL;
2322 return -1;
2325 adapter->dev = &pdev->dev;
2326 strcpy(adapter->fw_name, card->pcie.firmware);
2328 return 0;
2332 * This function unregisters the PCIE device.
2334 * The PCIE IRQ is released, the function is disabled and driver
2335 * data is set to null.
2337 static void mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
2339 struct pcie_service_card *card = adapter->card;
2340 const struct mwifiex_pcie_card_reg *reg;
2342 if (card) {
2343 dev_dbg(adapter->dev, "%s(): calling free_irq()\n", __func__);
2344 free_irq(card->dev->irq, card->dev);
2346 reg = card->pcie.reg;
2347 if (reg->sleep_cookie)
2348 mwifiex_pcie_delete_sleep_cookie_buf(adapter);
2350 mwifiex_pcie_delete_cmdrsp_buf(adapter);
2351 mwifiex_pcie_delete_evtbd_ring(adapter);
2352 mwifiex_pcie_delete_rxbd_ring(adapter);
2353 mwifiex_pcie_delete_txbd_ring(adapter);
2354 card->cmdrsp_buf = NULL;
2358 static struct mwifiex_if_ops pcie_ops = {
2359 .init_if = mwifiex_pcie_init,
2360 .cleanup_if = mwifiex_pcie_cleanup,
2361 .check_fw_status = mwifiex_check_fw_status,
2362 .prog_fw = mwifiex_prog_fw_w_helper,
2363 .register_dev = mwifiex_register_dev,
2364 .unregister_dev = mwifiex_unregister_dev,
2365 .enable_int = mwifiex_pcie_enable_host_int,
2366 .process_int_status = mwifiex_process_int_status,
2367 .host_to_card = mwifiex_pcie_host_to_card,
2368 .wakeup = mwifiex_pm_wakeup_card,
2369 .wakeup_complete = mwifiex_pm_wakeup_card_complete,
2371 /* PCIE specific */
2372 .cmdrsp_complete = mwifiex_pcie_cmdrsp_complete,
2373 .event_complete = mwifiex_pcie_event_complete,
2374 .update_mp_end_port = NULL,
2375 .cleanup_mpa_buf = NULL,
2376 .init_fw_port = mwifiex_pcie_init_fw_port,
2377 .clean_pcie_ring = mwifiex_clean_pcie_ring_buf,
2381 * This function initializes the PCIE driver module.
2383 * This initiates the semaphore and registers the device with
2384 * PCIE bus.
2386 static int mwifiex_pcie_init_module(void)
2388 int ret;
2390 pr_debug("Marvell PCIe Driver\n");
2392 sema_init(&add_remove_card_sem, 1);
2394 /* Clear the flag in case user removes the card. */
2395 user_rmmod = 0;
2397 ret = pci_register_driver(&mwifiex_pcie);
2398 if (ret)
2399 pr_err("Driver register failed!\n");
2400 else
2401 pr_debug("info: Driver registered successfully!\n");
2403 return ret;
2407 * This function cleans up the PCIE driver.
2409 * The following major steps are followed for cleanup -
2410 * - Resume the device if its suspended
2411 * - Disconnect the device if connected
2412 * - Shutdown the firmware
2413 * - Unregister the device from PCIE bus.
2415 static void mwifiex_pcie_cleanup_module(void)
2417 if (!down_interruptible(&add_remove_card_sem))
2418 up(&add_remove_card_sem);
2420 /* Set the flag as user is removing this module. */
2421 user_rmmod = 1;
2423 pci_unregister_driver(&mwifiex_pcie);
2426 module_init(mwifiex_pcie_init_module);
2427 module_exit(mwifiex_pcie_cleanup_module);
2429 MODULE_AUTHOR("Marvell International Ltd.");
2430 MODULE_DESCRIPTION("Marvell WiFi-Ex PCI-Express Driver version " PCIE_VERSION);
2431 MODULE_VERSION(PCIE_VERSION);
2432 MODULE_LICENSE("GPL v2");
2433 MODULE_FIRMWARE(PCIE8766_DEFAULT_FW_NAME);
2434 MODULE_FIRMWARE(PCIE8897_DEFAULT_FW_NAME);