2 * Marvell Wireless LAN device driver: PCIE specific handling
4 * Copyright (C) 2011-2014, 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>
31 #define PCIE_VERSION "1.0"
32 #define DRV_NAME "Marvell mwifiex PCIe"
36 static struct mwifiex_if_ops pcie_ops;
38 static struct semaphore add_remove_card_sem;
41 mwifiex_map_pci_memory(struct mwifiex_adapter *adapter, struct sk_buff *skb,
42 size_t size, int flags)
44 struct pcie_service_card *card = adapter->card;
45 struct mwifiex_dma_mapping mapping;
47 mapping.addr = pci_map_single(card->dev, skb->data, size, flags);
48 if (pci_dma_mapping_error(card->dev, mapping.addr)) {
49 mwifiex_dbg(adapter, ERROR, "failed to map pci memory!\n");
53 mwifiex_store_mapping(skb, &mapping);
57 static void mwifiex_unmap_pci_memory(struct mwifiex_adapter *adapter,
58 struct sk_buff *skb, int flags)
60 struct pcie_service_card *card = adapter->card;
61 struct mwifiex_dma_mapping mapping;
63 mwifiex_get_mapping(skb, &mapping);
64 pci_unmap_single(card->dev, mapping.addr, mapping.len, flags);
68 * This function reads sleep cookie and checks if FW is ready
70 static bool mwifiex_pcie_ok_to_access_hw(struct mwifiex_adapter *adapter)
73 struct pcie_service_card *card = adapter->card;
74 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
76 if (!reg->sleep_cookie)
79 if (card->sleep_cookie_vbase) {
80 cookie_addr = (u32 *)card->sleep_cookie_vbase;
81 mwifiex_dbg(adapter, INFO,
82 "info: ACCESS_HW: sleep cookie=0x%x\n",
84 if (*cookie_addr == FW_AWAKE_COOKIE)
91 #ifdef CONFIG_PM_SLEEP
93 * Kernel needs to suspend all functions separately. Therefore all
94 * registered functions must have drivers with suspend and resume
95 * methods. Failing that the kernel simply removes the whole card.
97 * If already not suspended, this function allocates and sends a host
98 * sleep activate request to the firmware and turns off the traffic.
100 static int mwifiex_pcie_suspend(struct device *dev)
102 struct mwifiex_adapter *adapter;
103 struct pcie_service_card *card;
104 struct pci_dev *pdev = to_pci_dev(dev);
107 card = pci_get_drvdata(pdev);
108 if (!card || !card->adapter) {
109 pr_err("Card or adapter structure is not valid\n");
113 pr_err("PCIE device is not specified\n");
117 adapter = card->adapter;
119 /* Enable the Host Sleep */
120 if (!mwifiex_enable_hs(adapter)) {
121 mwifiex_dbg(adapter, ERROR,
122 "cmd: failed to suspend\n");
123 adapter->hs_enabling = false;
127 flush_workqueue(adapter->workqueue);
129 /* Indicate device suspended */
130 adapter->is_suspended = true;
131 adapter->hs_enabling = false;
137 * Kernel needs to suspend all functions separately. Therefore all
138 * registered functions must have drivers with suspend and resume
139 * methods. Failing that the kernel simply removes the whole card.
141 * If already not resumed, this function turns on the traffic and
142 * sends a host sleep cancel request to the firmware.
144 static int mwifiex_pcie_resume(struct device *dev)
146 struct mwifiex_adapter *adapter;
147 struct pcie_service_card *card;
148 struct pci_dev *pdev = to_pci_dev(dev);
151 card = pci_get_drvdata(pdev);
152 if (!card || !card->adapter) {
153 pr_err("Card or adapter structure is not valid\n");
157 pr_err("PCIE device is not specified\n");
161 adapter = card->adapter;
163 if (!adapter->is_suspended) {
164 mwifiex_dbg(adapter, WARN,
165 "Device already resumed\n");
169 adapter->is_suspended = false;
171 mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
179 * This function probes an mwifiex device and registers it. It allocates
180 * the card structure, enables PCIE function number and initiates the
181 * device registration and initialization procedure by adding a logical
184 static int mwifiex_pcie_probe(struct pci_dev *pdev,
185 const struct pci_device_id *ent)
187 struct pcie_service_card *card;
189 pr_debug("info: vendor=0x%4.04X device=0x%4.04X rev=%d\n",
190 pdev->vendor, pdev->device, pdev->revision);
192 card = kzalloc(sizeof(struct pcie_service_card), GFP_KERNEL);
198 if (ent->driver_data) {
199 struct mwifiex_pcie_device *data = (void *)ent->driver_data;
200 card->pcie.reg = data->reg;
201 card->pcie.blksz_fw_dl = data->blksz_fw_dl;
202 card->pcie.tx_buf_size = data->tx_buf_size;
203 card->pcie.can_dump_fw = data->can_dump_fw;
204 card->pcie.mem_type_mapping_tbl = data->mem_type_mapping_tbl;
205 card->pcie.num_mem_types = data->num_mem_types;
206 card->pcie.can_ext_scan = data->can_ext_scan;
209 if (mwifiex_add_card(card, &add_remove_card_sem, &pcie_ops,
211 pr_err("%s failed\n", __func__);
219 * This function removes the interface and frees up the card structure.
221 static void mwifiex_pcie_remove(struct pci_dev *pdev)
223 struct pcie_service_card *card;
224 struct mwifiex_adapter *adapter;
225 struct mwifiex_private *priv;
227 card = pci_get_drvdata(pdev);
231 adapter = card->adapter;
232 if (!adapter || !adapter->priv_num)
235 if (user_rmmod && !adapter->mfg_mode) {
236 #ifdef CONFIG_PM_SLEEP
237 if (adapter->is_suspended)
238 mwifiex_pcie_resume(&pdev->dev);
241 mwifiex_deauthenticate_all(adapter);
243 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
245 mwifiex_disable_auto_ds(priv);
247 mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
250 mwifiex_remove_card(card->adapter, &add_remove_card_sem);
253 static void mwifiex_pcie_shutdown(struct pci_dev *pdev)
256 mwifiex_pcie_remove(pdev);
261 static const struct pci_device_id mwifiex_ids[] = {
263 PCIE_VENDOR_ID_MARVELL, PCIE_DEVICE_ID_MARVELL_88W8766P,
264 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
265 .driver_data = (unsigned long)&mwifiex_pcie8766,
268 PCIE_VENDOR_ID_MARVELL, PCIE_DEVICE_ID_MARVELL_88W8897,
269 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
270 .driver_data = (unsigned long)&mwifiex_pcie8897,
273 PCIE_VENDOR_ID_MARVELL, PCIE_DEVICE_ID_MARVELL_88W8997,
274 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
275 .driver_data = (unsigned long)&mwifiex_pcie8997,
278 PCIE_VENDOR_ID_V2_MARVELL, PCIE_DEVICE_ID_MARVELL_88W8997,
279 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
280 .driver_data = (unsigned long)&mwifiex_pcie8997,
285 MODULE_DEVICE_TABLE(pci, mwifiex_ids);
287 static void mwifiex_pcie_reset_notify(struct pci_dev *pdev, bool prepare)
289 struct mwifiex_adapter *adapter;
290 struct pcie_service_card *card;
293 pr_err("%s: PCIe device is not specified\n", __func__);
297 card = (struct pcie_service_card *)pci_get_drvdata(pdev);
298 if (!card || !card->adapter) {
299 pr_err("%s: Card or adapter structure is not valid (%ld)\n",
300 __func__, (long)card);
304 adapter = card->adapter;
305 mwifiex_dbg(adapter, INFO,
306 "%s: vendor=0x%4.04x device=0x%4.04x rev=%d %s\n",
307 __func__, pdev->vendor, pdev->device,
309 prepare ? "Pre-FLR" : "Post-FLR");
312 /* Kernel would be performing FLR after this notification.
313 * Cleanup all software without cleaning anything related to
316 mwifiex_do_flr(adapter, prepare);
317 adapter->surprise_removed = true;
319 /* Kernel stores and restores PCIe function context before and
320 * after performing FLR respectively. Reconfigure the software
321 * and firmware including firmware redownload
323 adapter->surprise_removed = false;
324 mwifiex_do_flr(adapter, prepare);
326 mwifiex_dbg(adapter, INFO, "%s, successful\n", __func__);
329 static const struct pci_error_handlers mwifiex_pcie_err_handler[] = {
330 { .reset_notify = mwifiex_pcie_reset_notify, },
333 #ifdef CONFIG_PM_SLEEP
334 /* Power Management Hooks */
335 static SIMPLE_DEV_PM_OPS(mwifiex_pcie_pm_ops, mwifiex_pcie_suspend,
336 mwifiex_pcie_resume);
339 /* PCI Device Driver */
340 static struct pci_driver __refdata mwifiex_pcie = {
341 .name = "mwifiex_pcie",
342 .id_table = mwifiex_ids,
343 .probe = mwifiex_pcie_probe,
344 .remove = mwifiex_pcie_remove,
345 #ifdef CONFIG_PM_SLEEP
347 .pm = &mwifiex_pcie_pm_ops,
350 .shutdown = mwifiex_pcie_shutdown,
351 .err_handler = mwifiex_pcie_err_handler,
355 * This function writes data into PCIE card register.
357 static int mwifiex_write_reg(struct mwifiex_adapter *adapter, int reg, u32 data)
359 struct pcie_service_card *card = adapter->card;
361 iowrite32(data, card->pci_mmap1 + reg);
367 * This function reads data from PCIE card register.
369 static int mwifiex_read_reg(struct mwifiex_adapter *adapter, int reg, u32 *data)
371 struct pcie_service_card *card = adapter->card;
373 *data = ioread32(card->pci_mmap1 + reg);
374 if (*data == 0xffffffff)
380 /* This function reads u8 data from PCIE card register. */
381 static int mwifiex_read_reg_byte(struct mwifiex_adapter *adapter,
384 struct pcie_service_card *card = adapter->card;
386 *data = ioread8(card->pci_mmap1 + reg);
392 * This function adds delay loop to ensure FW is awake before proceeding.
394 static void mwifiex_pcie_dev_wakeup_delay(struct mwifiex_adapter *adapter)
398 while (mwifiex_pcie_ok_to_access_hw(adapter)) {
400 usleep_range(10, 20);
409 static void mwifiex_delay_for_sleep_cookie(struct mwifiex_adapter *adapter,
410 u32 max_delay_loop_cnt)
412 struct pcie_service_card *card = adapter->card;
414 u32 sleep_cookie, count;
416 for (count = 0; count < max_delay_loop_cnt; count++) {
417 buffer = card->cmdrsp_buf->data - INTF_HEADER_LEN;
418 sleep_cookie = *(u32 *)buffer;
420 if (sleep_cookie == MWIFIEX_DEF_SLEEP_COOKIE) {
421 mwifiex_dbg(adapter, INFO,
422 "sleep cookie found at count %d\n", count);
425 usleep_range(20, 30);
428 if (count >= max_delay_loop_cnt)
429 mwifiex_dbg(adapter, INFO,
430 "max count reached while accessing sleep cookie\n");
433 /* This function wakes up the card by reading fw_status register. */
434 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
437 struct pcie_service_card *card = adapter->card;
438 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
440 mwifiex_dbg(adapter, EVENT,
441 "event: Wakeup device...\n");
443 if (reg->sleep_cookie)
444 mwifiex_pcie_dev_wakeup_delay(adapter);
446 /* Reading fw_status register will wakeup device */
447 if (mwifiex_read_reg(adapter, reg->fw_status, &fw_status)) {
448 mwifiex_dbg(adapter, ERROR,
449 "Reading fw_status register failed\n");
453 if (reg->sleep_cookie) {
454 mwifiex_pcie_dev_wakeup_delay(adapter);
455 mwifiex_dbg(adapter, INFO,
456 "PCIE wakeup: Setting PS_STATE_AWAKE\n");
457 adapter->ps_state = PS_STATE_AWAKE;
464 * This function is called after the card has woken up.
466 * The card configuration register is reset.
468 static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
470 mwifiex_dbg(adapter, CMD,
471 "cmd: Wakeup device completed\n");
477 * This function disables the host interrupt.
479 * The host interrupt mask is read, the disable bit is reset and
480 * written back to the card host interrupt mask register.
482 static int mwifiex_pcie_disable_host_int(struct mwifiex_adapter *adapter)
484 if (mwifiex_pcie_ok_to_access_hw(adapter)) {
485 if (mwifiex_write_reg(adapter, PCIE_HOST_INT_MASK,
487 mwifiex_dbg(adapter, ERROR,
488 "Disable host interrupt failed\n");
496 static void mwifiex_pcie_disable_host_int_noerr(struct mwifiex_adapter *adapter)
498 WARN_ON(mwifiex_pcie_disable_host_int(adapter));
502 * This function enables the host interrupt.
504 * The host interrupt enable mask is written to the card
505 * host interrupt mask register.
507 static int mwifiex_pcie_enable_host_int(struct mwifiex_adapter *adapter)
509 if (mwifiex_pcie_ok_to_access_hw(adapter)) {
510 /* Simply write the mask to the register */
511 if (mwifiex_write_reg(adapter, PCIE_HOST_INT_MASK,
513 mwifiex_dbg(adapter, ERROR,
514 "Enable host interrupt failed\n");
523 * This function initializes TX buffer ring descriptors
525 static int mwifiex_init_txq_ring(struct mwifiex_adapter *adapter)
527 struct pcie_service_card *card = adapter->card;
528 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
529 struct mwifiex_pcie_buf_desc *desc;
530 struct mwifiex_pfu_buf_desc *desc2;
533 for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
534 card->tx_buf_list[i] = NULL;
535 if (reg->pfu_enabled) {
536 card->txbd_ring[i] = (void *)card->txbd_ring_vbase +
537 (sizeof(*desc2) * i);
538 desc2 = card->txbd_ring[i];
539 memset(desc2, 0, sizeof(*desc2));
541 card->txbd_ring[i] = (void *)card->txbd_ring_vbase +
543 desc = card->txbd_ring[i];
544 memset(desc, 0, sizeof(*desc));
551 /* This function initializes RX buffer ring descriptors. Each SKB is allocated
552 * here and after mapping PCI memory, its physical address is assigned to
553 * PCIE Rx buffer descriptor's physical address.
555 static int mwifiex_init_rxq_ring(struct mwifiex_adapter *adapter)
557 struct pcie_service_card *card = adapter->card;
558 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
560 struct mwifiex_pcie_buf_desc *desc;
561 struct mwifiex_pfu_buf_desc *desc2;
565 for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
566 /* Allocate skb here so that firmware can DMA data from it */
567 skb = mwifiex_alloc_dma_align_buf(MWIFIEX_RX_DATA_BUF_SIZE,
570 mwifiex_dbg(adapter, ERROR,
571 "Unable to allocate skb for RX ring.\n");
572 kfree(card->rxbd_ring_vbase);
576 if (mwifiex_map_pci_memory(adapter, skb,
577 MWIFIEX_RX_DATA_BUF_SIZE,
581 buf_pa = MWIFIEX_SKB_DMA_ADDR(skb);
583 mwifiex_dbg(adapter, INFO,
584 "info: RX ring: skb=%p len=%d data=%p buf_pa=%#x:%x\n",
585 skb, skb->len, skb->data, (u32)buf_pa,
586 (u32)((u64)buf_pa >> 32));
588 card->rx_buf_list[i] = skb;
589 if (reg->pfu_enabled) {
590 card->rxbd_ring[i] = (void *)card->rxbd_ring_vbase +
591 (sizeof(*desc2) * i);
592 desc2 = card->rxbd_ring[i];
593 desc2->paddr = buf_pa;
594 desc2->len = (u16)skb->len;
595 desc2->frag_len = (u16)skb->len;
596 desc2->flags = reg->ring_flag_eop | reg->ring_flag_sop;
599 card->rxbd_ring[i] = (void *)(card->rxbd_ring_vbase +
600 (sizeof(*desc) * i));
601 desc = card->rxbd_ring[i];
602 desc->paddr = buf_pa;
603 desc->len = (u16)skb->len;
611 /* This function initializes event buffer ring descriptors. Each SKB is
612 * allocated here and after mapping PCI memory, its physical address is assigned
613 * to PCIE Rx buffer descriptor's physical address
615 static int mwifiex_pcie_init_evt_ring(struct mwifiex_adapter *adapter)
617 struct pcie_service_card *card = adapter->card;
618 struct mwifiex_evt_buf_desc *desc;
623 for (i = 0; i < MWIFIEX_MAX_EVT_BD; i++) {
624 /* Allocate skb here so that firmware can DMA data from it */
625 skb = dev_alloc_skb(MAX_EVENT_SIZE);
627 mwifiex_dbg(adapter, ERROR,
628 "Unable to allocate skb for EVENT buf.\n");
629 kfree(card->evtbd_ring_vbase);
632 skb_put(skb, MAX_EVENT_SIZE);
634 if (mwifiex_map_pci_memory(adapter, skb, MAX_EVENT_SIZE,
635 PCI_DMA_FROMDEVICE)) {
637 kfree(card->evtbd_ring_vbase);
641 buf_pa = MWIFIEX_SKB_DMA_ADDR(skb);
643 mwifiex_dbg(adapter, EVENT,
644 "info: EVT ring: skb=%p len=%d data=%p buf_pa=%#x:%x\n",
645 skb, skb->len, skb->data, (u32)buf_pa,
646 (u32)((u64)buf_pa >> 32));
648 card->evt_buf_list[i] = skb;
649 card->evtbd_ring[i] = (void *)(card->evtbd_ring_vbase +
650 (sizeof(*desc) * i));
651 desc = card->evtbd_ring[i];
652 desc->paddr = buf_pa;
653 desc->len = (u16)skb->len;
660 /* This function cleans up TX buffer rings. If any of the buffer list has valid
661 * SKB address, associated SKB is freed.
663 static void mwifiex_cleanup_txq_ring(struct mwifiex_adapter *adapter)
665 struct pcie_service_card *card = adapter->card;
666 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
668 struct mwifiex_pcie_buf_desc *desc;
669 struct mwifiex_pfu_buf_desc *desc2;
672 for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
673 if (reg->pfu_enabled) {
674 desc2 = card->txbd_ring[i];
675 if (card->tx_buf_list[i]) {
676 skb = card->tx_buf_list[i];
677 mwifiex_unmap_pci_memory(adapter, skb,
679 dev_kfree_skb_any(skb);
681 memset(desc2, 0, sizeof(*desc2));
683 desc = card->txbd_ring[i];
684 if (card->tx_buf_list[i]) {
685 skb = card->tx_buf_list[i];
686 mwifiex_unmap_pci_memory(adapter, skb,
688 dev_kfree_skb_any(skb);
690 memset(desc, 0, sizeof(*desc));
692 card->tx_buf_list[i] = NULL;
698 /* This function cleans up RX buffer rings. If any of the buffer list has valid
699 * SKB address, associated SKB is freed.
701 static void mwifiex_cleanup_rxq_ring(struct mwifiex_adapter *adapter)
703 struct pcie_service_card *card = adapter->card;
704 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
705 struct mwifiex_pcie_buf_desc *desc;
706 struct mwifiex_pfu_buf_desc *desc2;
710 for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
711 if (reg->pfu_enabled) {
712 desc2 = card->rxbd_ring[i];
713 if (card->rx_buf_list[i]) {
714 skb = card->rx_buf_list[i];
715 mwifiex_unmap_pci_memory(adapter, skb,
717 dev_kfree_skb_any(skb);
719 memset(desc2, 0, sizeof(*desc2));
721 desc = card->rxbd_ring[i];
722 if (card->rx_buf_list[i]) {
723 skb = card->rx_buf_list[i];
724 mwifiex_unmap_pci_memory(adapter, skb,
726 dev_kfree_skb_any(skb);
728 memset(desc, 0, sizeof(*desc));
730 card->rx_buf_list[i] = NULL;
736 /* This function cleans up event buffer rings. If any of the buffer list has
737 * valid SKB address, associated SKB is freed.
739 static void mwifiex_cleanup_evt_ring(struct mwifiex_adapter *adapter)
741 struct pcie_service_card *card = adapter->card;
742 struct mwifiex_evt_buf_desc *desc;
746 for (i = 0; i < MWIFIEX_MAX_EVT_BD; i++) {
747 desc = card->evtbd_ring[i];
748 if (card->evt_buf_list[i]) {
749 skb = card->evt_buf_list[i];
750 mwifiex_unmap_pci_memory(adapter, skb,
752 dev_kfree_skb_any(skb);
754 card->evt_buf_list[i] = NULL;
755 memset(desc, 0, sizeof(*desc));
761 /* This function creates buffer descriptor ring for TX
763 static int mwifiex_pcie_create_txbd_ring(struct mwifiex_adapter *adapter)
765 struct pcie_service_card *card = adapter->card;
766 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
769 * driver maintaines the write pointer and firmware maintaines the read
770 * pointer. The write pointer starts at 0 (zero) while the read pointer
771 * starts at zero with rollover bit set
773 card->txbd_wrptr = 0;
775 if (reg->pfu_enabled)
776 card->txbd_rdptr = 0;
778 card->txbd_rdptr |= reg->tx_rollover_ind;
780 /* allocate shared memory for the BD ring and divide the same in to
781 several descriptors */
782 if (reg->pfu_enabled)
783 card->txbd_ring_size = sizeof(struct mwifiex_pfu_buf_desc) *
786 card->txbd_ring_size = sizeof(struct mwifiex_pcie_buf_desc) *
789 mwifiex_dbg(adapter, INFO,
790 "info: txbd_ring: Allocating %d bytes\n",
791 card->txbd_ring_size);
792 card->txbd_ring_vbase = pci_alloc_consistent(card->dev,
793 card->txbd_ring_size,
794 &card->txbd_ring_pbase);
795 if (!card->txbd_ring_vbase) {
796 mwifiex_dbg(adapter, ERROR,
797 "allocate consistent memory (%d bytes) failed!\n",
798 card->txbd_ring_size);
801 mwifiex_dbg(adapter, DATA,
802 "info: txbd_ring - base: %p, pbase: %#x:%x, len: %x\n",
803 card->txbd_ring_vbase, (unsigned int)card->txbd_ring_pbase,
804 (u32)((u64)card->txbd_ring_pbase >> 32),
805 card->txbd_ring_size);
807 return mwifiex_init_txq_ring(adapter);
810 static int mwifiex_pcie_delete_txbd_ring(struct mwifiex_adapter *adapter)
812 struct pcie_service_card *card = adapter->card;
813 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
815 mwifiex_cleanup_txq_ring(adapter);
817 if (card->txbd_ring_vbase)
818 pci_free_consistent(card->dev, card->txbd_ring_size,
819 card->txbd_ring_vbase,
820 card->txbd_ring_pbase);
821 card->txbd_ring_size = 0;
822 card->txbd_wrptr = 0;
823 card->txbd_rdptr = 0 | reg->tx_rollover_ind;
824 card->txbd_ring_vbase = NULL;
825 card->txbd_ring_pbase = 0;
831 * This function creates buffer descriptor ring for RX
833 static int mwifiex_pcie_create_rxbd_ring(struct mwifiex_adapter *adapter)
835 struct pcie_service_card *card = adapter->card;
836 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
839 * driver maintaines the read pointer and firmware maintaines the write
840 * pointer. The write pointer starts at 0 (zero) while the read pointer
841 * starts at zero with rollover bit set
843 card->rxbd_wrptr = 0;
844 card->rxbd_rdptr = reg->rx_rollover_ind;
846 if (reg->pfu_enabled)
847 card->rxbd_ring_size = sizeof(struct mwifiex_pfu_buf_desc) *
850 card->rxbd_ring_size = sizeof(struct mwifiex_pcie_buf_desc) *
853 mwifiex_dbg(adapter, INFO,
854 "info: rxbd_ring: Allocating %d bytes\n",
855 card->rxbd_ring_size);
856 card->rxbd_ring_vbase = pci_alloc_consistent(card->dev,
857 card->rxbd_ring_size,
858 &card->rxbd_ring_pbase);
859 if (!card->rxbd_ring_vbase) {
860 mwifiex_dbg(adapter, ERROR,
861 "allocate consistent memory (%d bytes) failed!\n",
862 card->rxbd_ring_size);
866 mwifiex_dbg(adapter, DATA,
867 "info: rxbd_ring - base: %p, pbase: %#x:%x, len: %#x\n",
868 card->rxbd_ring_vbase, (u32)card->rxbd_ring_pbase,
869 (u32)((u64)card->rxbd_ring_pbase >> 32),
870 card->rxbd_ring_size);
872 return mwifiex_init_rxq_ring(adapter);
876 * This function deletes Buffer descriptor ring for RX
878 static int mwifiex_pcie_delete_rxbd_ring(struct mwifiex_adapter *adapter)
880 struct pcie_service_card *card = adapter->card;
881 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
883 mwifiex_cleanup_rxq_ring(adapter);
885 if (card->rxbd_ring_vbase)
886 pci_free_consistent(card->dev, card->rxbd_ring_size,
887 card->rxbd_ring_vbase,
888 card->rxbd_ring_pbase);
889 card->rxbd_ring_size = 0;
890 card->rxbd_wrptr = 0;
891 card->rxbd_rdptr = 0 | reg->rx_rollover_ind;
892 card->rxbd_ring_vbase = NULL;
893 card->rxbd_ring_pbase = 0;
899 * This function creates buffer descriptor ring for Events
901 static int mwifiex_pcie_create_evtbd_ring(struct mwifiex_adapter *adapter)
903 struct pcie_service_card *card = adapter->card;
904 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
907 * driver maintaines the read pointer and firmware maintaines the write
908 * pointer. The write pointer starts at 0 (zero) while the read pointer
909 * starts at zero with rollover bit set
911 card->evtbd_wrptr = 0;
912 card->evtbd_rdptr = reg->evt_rollover_ind;
914 card->evtbd_ring_size = sizeof(struct mwifiex_evt_buf_desc) *
917 mwifiex_dbg(adapter, INFO,
918 "info: evtbd_ring: Allocating %d bytes\n",
919 card->evtbd_ring_size);
920 card->evtbd_ring_vbase = pci_alloc_consistent(card->dev,
921 card->evtbd_ring_size,
922 &card->evtbd_ring_pbase);
923 if (!card->evtbd_ring_vbase) {
924 mwifiex_dbg(adapter, ERROR,
925 "allocate consistent memory (%d bytes) failed!\n",
926 card->evtbd_ring_size);
930 mwifiex_dbg(adapter, EVENT,
931 "info: CMDRSP/EVT bd_ring - base: %p pbase: %#x:%x len: %#x\n",
932 card->evtbd_ring_vbase, (u32)card->evtbd_ring_pbase,
933 (u32)((u64)card->evtbd_ring_pbase >> 32),
934 card->evtbd_ring_size);
936 return mwifiex_pcie_init_evt_ring(adapter);
940 * This function deletes Buffer descriptor ring for Events
942 static int mwifiex_pcie_delete_evtbd_ring(struct mwifiex_adapter *adapter)
944 struct pcie_service_card *card = adapter->card;
945 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
947 mwifiex_cleanup_evt_ring(adapter);
949 if (card->evtbd_ring_vbase)
950 pci_free_consistent(card->dev, card->evtbd_ring_size,
951 card->evtbd_ring_vbase,
952 card->evtbd_ring_pbase);
953 card->evtbd_wrptr = 0;
954 card->evtbd_rdptr = 0 | reg->evt_rollover_ind;
955 card->evtbd_ring_size = 0;
956 card->evtbd_ring_vbase = NULL;
957 card->evtbd_ring_pbase = 0;
963 * This function allocates a buffer for CMDRSP
965 static int mwifiex_pcie_alloc_cmdrsp_buf(struct mwifiex_adapter *adapter)
967 struct pcie_service_card *card = adapter->card;
970 /* Allocate memory for receiving command response data */
971 skb = dev_alloc_skb(MWIFIEX_UPLD_SIZE);
973 mwifiex_dbg(adapter, ERROR,
974 "Unable to allocate skb for command response data.\n");
977 skb_put(skb, MWIFIEX_UPLD_SIZE);
978 if (mwifiex_map_pci_memory(adapter, skb, MWIFIEX_UPLD_SIZE,
979 PCI_DMA_FROMDEVICE)) {
984 card->cmdrsp_buf = skb;
990 * This function deletes a buffer for CMDRSP
992 static int mwifiex_pcie_delete_cmdrsp_buf(struct mwifiex_adapter *adapter)
994 struct pcie_service_card *card;
999 card = adapter->card;
1001 if (card && card->cmdrsp_buf) {
1002 mwifiex_unmap_pci_memory(adapter, card->cmdrsp_buf,
1003 PCI_DMA_FROMDEVICE);
1004 dev_kfree_skb_any(card->cmdrsp_buf);
1007 if (card && card->cmd_buf) {
1008 mwifiex_unmap_pci_memory(adapter, card->cmd_buf,
1010 dev_kfree_skb_any(card->cmd_buf);
1016 * This function allocates a buffer for sleep cookie
1018 static int mwifiex_pcie_alloc_sleep_cookie_buf(struct mwifiex_adapter *adapter)
1020 struct pcie_service_card *card = adapter->card;
1022 card->sleep_cookie_vbase = pci_alloc_consistent(card->dev, sizeof(u32),
1023 &card->sleep_cookie_pbase);
1024 if (!card->sleep_cookie_vbase) {
1025 mwifiex_dbg(adapter, ERROR,
1026 "pci_alloc_consistent failed!\n");
1029 /* Init val of Sleep Cookie */
1030 *(u32 *)card->sleep_cookie_vbase = FW_AWAKE_COOKIE;
1032 mwifiex_dbg(adapter, INFO,
1033 "alloc_scook: sleep cookie=0x%x\n",
1034 *((u32 *)card->sleep_cookie_vbase));
1040 * This function deletes buffer for sleep cookie
1042 static int mwifiex_pcie_delete_sleep_cookie_buf(struct mwifiex_adapter *adapter)
1044 struct pcie_service_card *card;
1049 card = adapter->card;
1051 if (card && card->sleep_cookie_vbase) {
1052 pci_free_consistent(card->dev, sizeof(u32),
1053 card->sleep_cookie_vbase,
1054 card->sleep_cookie_pbase);
1055 card->sleep_cookie_vbase = NULL;
1061 /* This function flushes the TX buffer descriptor ring
1062 * This function defined as handler is also called while cleaning TXRX
1063 * during disconnect/ bss stop.
1065 static int mwifiex_clean_pcie_ring_buf(struct mwifiex_adapter *adapter)
1067 struct pcie_service_card *card = adapter->card;
1069 if (!mwifiex_pcie_txbd_empty(card, card->txbd_rdptr)) {
1070 card->txbd_flush = 1;
1071 /* write pointer already set at last send
1072 * send dnld-rdy intr again, wait for completion.
1074 if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
1075 CPU_INTR_DNLD_RDY)) {
1076 mwifiex_dbg(adapter, ERROR,
1077 "failed to assert dnld-rdy interrupt.\n");
1085 * This function unmaps and frees downloaded data buffer
1087 static int mwifiex_pcie_send_data_complete(struct mwifiex_adapter *adapter)
1089 struct sk_buff *skb;
1090 u32 wrdoneidx, rdptr, num_tx_buffs, unmap_count = 0;
1091 struct mwifiex_pcie_buf_desc *desc;
1092 struct mwifiex_pfu_buf_desc *desc2;
1093 struct pcie_service_card *card = adapter->card;
1094 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1096 if (!mwifiex_pcie_ok_to_access_hw(adapter))
1097 mwifiex_pm_wakeup_card(adapter);
1099 /* Read the TX ring read pointer set by firmware */
1100 if (mwifiex_read_reg(adapter, reg->tx_rdptr, &rdptr)) {
1101 mwifiex_dbg(adapter, ERROR,
1102 "SEND COMP: failed to read reg->tx_rdptr\n");
1106 mwifiex_dbg(adapter, DATA,
1107 "SEND COMP: rdptr_prev=0x%x, rdptr=0x%x\n",
1108 card->txbd_rdptr, rdptr);
1110 num_tx_buffs = MWIFIEX_MAX_TXRX_BD << reg->tx_start_ptr;
1111 /* free from previous txbd_rdptr to current txbd_rdptr */
1112 while (((card->txbd_rdptr & reg->tx_mask) !=
1113 (rdptr & reg->tx_mask)) ||
1114 ((card->txbd_rdptr & reg->tx_rollover_ind) !=
1115 (rdptr & reg->tx_rollover_ind))) {
1116 wrdoneidx = (card->txbd_rdptr & reg->tx_mask) >>
1119 skb = card->tx_buf_list[wrdoneidx];
1122 mwifiex_dbg(adapter, DATA,
1123 "SEND COMP: Detach skb %p at txbd_rdidx=%d\n",
1125 mwifiex_unmap_pci_memory(adapter, skb,
1130 if (card->txbd_flush)
1131 mwifiex_write_data_complete(adapter, skb, 0,
1134 mwifiex_write_data_complete(adapter, skb, 0, 0);
1137 card->tx_buf_list[wrdoneidx] = NULL;
1139 if (reg->pfu_enabled) {
1140 desc2 = card->txbd_ring[wrdoneidx];
1141 memset(desc2, 0, sizeof(*desc2));
1143 desc = card->txbd_ring[wrdoneidx];
1144 memset(desc, 0, sizeof(*desc));
1146 switch (card->dev->device) {
1147 case PCIE_DEVICE_ID_MARVELL_88W8766P:
1150 case PCIE_DEVICE_ID_MARVELL_88W8897:
1151 case PCIE_DEVICE_ID_MARVELL_88W8997:
1152 card->txbd_rdptr += reg->ring_tx_start_ptr;
1157 if ((card->txbd_rdptr & reg->tx_mask) == num_tx_buffs)
1158 card->txbd_rdptr = ((card->txbd_rdptr &
1159 reg->tx_rollover_ind) ^
1160 reg->tx_rollover_ind);
1164 adapter->data_sent = false;
1166 if (card->txbd_flush) {
1167 if (mwifiex_pcie_txbd_empty(card, card->txbd_rdptr))
1168 card->txbd_flush = 0;
1170 mwifiex_clean_pcie_ring_buf(adapter);
1176 /* This function sends data buffer to device. First 4 bytes of payload
1177 * are filled with payload length and payload type. Then this payload
1178 * is mapped to PCI device memory. Tx ring pointers are advanced accordingly.
1179 * Download ready interrupt to FW is deffered if Tx ring is not full and
1180 * additional payload can be accomodated.
1181 * Caller must ensure tx_param parameter to this function is not NULL.
1184 mwifiex_pcie_send_data(struct mwifiex_adapter *adapter, struct sk_buff *skb,
1185 struct mwifiex_tx_param *tx_param)
1187 struct pcie_service_card *card = adapter->card;
1188 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1189 u32 wrindx, num_tx_buffs, rx_val;
1192 struct mwifiex_pcie_buf_desc *desc = NULL;
1193 struct mwifiex_pfu_buf_desc *desc2 = NULL;
1196 if (!(skb->data && skb->len)) {
1197 mwifiex_dbg(adapter, ERROR,
1198 "%s(): invalid parameter <%p, %#x>\n",
1199 __func__, skb->data, skb->len);
1203 if (!mwifiex_pcie_ok_to_access_hw(adapter))
1204 mwifiex_pm_wakeup_card(adapter);
1206 num_tx_buffs = MWIFIEX_MAX_TXRX_BD << reg->tx_start_ptr;
1207 mwifiex_dbg(adapter, DATA,
1208 "info: SEND DATA: <Rd: %#x, Wr: %#x>\n",
1209 card->txbd_rdptr, card->txbd_wrptr);
1210 if (mwifiex_pcie_txbd_not_full(card)) {
1213 adapter->data_sent = true;
1214 payload = skb->data;
1215 tmp = (__le16 *)&payload[0];
1216 *tmp = cpu_to_le16((u16)skb->len);
1217 tmp = (__le16 *)&payload[2];
1218 *tmp = cpu_to_le16(MWIFIEX_TYPE_DATA);
1220 if (mwifiex_map_pci_memory(adapter, skb, skb->len,
1224 wrindx = (card->txbd_wrptr & reg->tx_mask) >> reg->tx_start_ptr;
1225 buf_pa = MWIFIEX_SKB_DMA_ADDR(skb);
1226 card->tx_buf_list[wrindx] = skb;
1228 if (reg->pfu_enabled) {
1229 desc2 = card->txbd_ring[wrindx];
1230 desc2->paddr = buf_pa;
1231 desc2->len = (u16)skb->len;
1232 desc2->frag_len = (u16)skb->len;
1234 desc2->flags = MWIFIEX_BD_FLAG_FIRST_DESC |
1235 MWIFIEX_BD_FLAG_LAST_DESC;
1237 desc = card->txbd_ring[wrindx];
1238 desc->paddr = buf_pa;
1239 desc->len = (u16)skb->len;
1240 desc->flags = MWIFIEX_BD_FLAG_FIRST_DESC |
1241 MWIFIEX_BD_FLAG_LAST_DESC;
1244 switch (card->dev->device) {
1245 case PCIE_DEVICE_ID_MARVELL_88W8766P:
1248 case PCIE_DEVICE_ID_MARVELL_88W8897:
1249 case PCIE_DEVICE_ID_MARVELL_88W8997:
1250 card->txbd_wrptr += reg->ring_tx_start_ptr;
1254 if ((card->txbd_wrptr & reg->tx_mask) == num_tx_buffs)
1255 card->txbd_wrptr = ((card->txbd_wrptr &
1256 reg->tx_rollover_ind) ^
1257 reg->tx_rollover_ind);
1259 rx_val = card->rxbd_rdptr & reg->rx_wrap_mask;
1260 /* Write the TX ring write pointer in to reg->tx_wrptr */
1261 if (mwifiex_write_reg(adapter, reg->tx_wrptr,
1262 card->txbd_wrptr | rx_val)) {
1263 mwifiex_dbg(adapter, ERROR,
1264 "SEND DATA: failed to write reg->tx_wrptr\n");
1269 /* The firmware (latest version 15.68.19.p21) of the 88W8897 PCIe+USB card
1270 * seems to crash randomly after setting the TX ring write pointer when
1271 * ASPM powersaving is enabled. A workaround seems to be keeping the bus
1272 * busy by reading a random register afterwards.
1274 mwifiex_read_reg(adapter, PCI_VENDOR_ID, &rx_val);
1276 if ((mwifiex_pcie_txbd_not_full(card)) &&
1277 tx_param->next_pkt_len) {
1278 /* have more packets and TxBD still can hold more */
1279 mwifiex_dbg(adapter, DATA,
1280 "SEND DATA: delay dnld-rdy interrupt.\n");
1281 adapter->data_sent = false;
1283 /* Send the TX ready interrupt */
1284 if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
1285 CPU_INTR_DNLD_RDY)) {
1286 mwifiex_dbg(adapter, ERROR,
1287 "SEND DATA: failed to assert dnld-rdy interrupt.\n");
1292 mwifiex_dbg(adapter, DATA,
1293 "info: SEND DATA: Updated <Rd: %#x, Wr:\t"
1294 "%#x> and sent packet to firmware successfully\n",
1295 card->txbd_rdptr, card->txbd_wrptr);
1297 mwifiex_dbg(adapter, DATA,
1298 "info: TX Ring full, can't send packets to fw\n");
1299 adapter->data_sent = true;
1300 /* Send the TX ready interrupt */
1301 if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
1303 mwifiex_dbg(adapter, ERROR,
1304 "SEND DATA: failed to assert door-bell intr\n");
1308 return -EINPROGRESS;
1310 mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_TODEVICE);
1311 card->tx_buf_list[wrindx] = NULL;
1312 if (reg->pfu_enabled)
1313 memset(desc2, 0, sizeof(*desc2));
1315 memset(desc, 0, sizeof(*desc));
1321 * This function handles received buffer ring and
1322 * dispatches packets to upper
1324 static int mwifiex_pcie_process_recv_data(struct mwifiex_adapter *adapter)
1326 struct pcie_service_card *card = adapter->card;
1327 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1328 u32 wrptr, rd_index, tx_val;
1331 struct sk_buff *skb_tmp = NULL;
1332 struct mwifiex_pcie_buf_desc *desc;
1333 struct mwifiex_pfu_buf_desc *desc2;
1335 if (!mwifiex_pcie_ok_to_access_hw(adapter))
1336 mwifiex_pm_wakeup_card(adapter);
1338 /* Read the RX ring Write pointer set by firmware */
1339 if (mwifiex_read_reg(adapter, reg->rx_wrptr, &wrptr)) {
1340 mwifiex_dbg(adapter, ERROR,
1341 "RECV DATA: failed to read reg->rx_wrptr\n");
1345 card->rxbd_wrptr = wrptr;
1347 while (((wrptr & reg->rx_mask) !=
1348 (card->rxbd_rdptr & reg->rx_mask)) ||
1349 ((wrptr & reg->rx_rollover_ind) ==
1350 (card->rxbd_rdptr & reg->rx_rollover_ind))) {
1351 struct sk_buff *skb_data;
1355 rd_index = card->rxbd_rdptr & reg->rx_mask;
1356 skb_data = card->rx_buf_list[rd_index];
1358 /* If skb allocation was failed earlier for Rx packet,
1359 * rx_buf_list[rd_index] would have been left with a NULL.
1364 mwifiex_unmap_pci_memory(adapter, skb_data, PCI_DMA_FROMDEVICE);
1365 card->rx_buf_list[rd_index] = NULL;
1367 /* Get data length from interface header -
1368 * first 2 bytes for len, next 2 bytes is for type
1370 pkt_len = *((__le16 *)skb_data->data);
1371 rx_len = le16_to_cpu(pkt_len);
1372 if (WARN_ON(rx_len <= INTF_HEADER_LEN ||
1373 rx_len > MWIFIEX_RX_DATA_BUF_SIZE)) {
1374 mwifiex_dbg(adapter, ERROR,
1375 "Invalid RX len %d, Rd=%#x, Wr=%#x\n",
1376 rx_len, card->rxbd_rdptr, wrptr);
1377 dev_kfree_skb_any(skb_data);
1379 skb_put(skb_data, rx_len);
1380 mwifiex_dbg(adapter, DATA,
1381 "info: RECV DATA: Rd=%#x, Wr=%#x, Len=%d\n",
1382 card->rxbd_rdptr, wrptr, rx_len);
1383 skb_pull(skb_data, INTF_HEADER_LEN);
1384 if (adapter->rx_work_enabled) {
1385 skb_queue_tail(&adapter->rx_data_q, skb_data);
1386 adapter->data_received = true;
1387 atomic_inc(&adapter->rx_pending);
1389 mwifiex_handle_rx_packet(adapter, skb_data);
1393 skb_tmp = mwifiex_alloc_dma_align_buf(MWIFIEX_RX_DATA_BUF_SIZE,
1396 mwifiex_dbg(adapter, ERROR,
1397 "Unable to allocate skb.\n");
1401 if (mwifiex_map_pci_memory(adapter, skb_tmp,
1402 MWIFIEX_RX_DATA_BUF_SIZE,
1403 PCI_DMA_FROMDEVICE))
1406 buf_pa = MWIFIEX_SKB_DMA_ADDR(skb_tmp);
1408 mwifiex_dbg(adapter, INFO,
1409 "RECV DATA: Attach new sk_buff %p at rxbd_rdidx=%d\n",
1411 card->rx_buf_list[rd_index] = skb_tmp;
1413 if (reg->pfu_enabled) {
1414 desc2 = card->rxbd_ring[rd_index];
1415 desc2->paddr = buf_pa;
1416 desc2->len = skb_tmp->len;
1417 desc2->frag_len = skb_tmp->len;
1419 desc2->flags = reg->ring_flag_sop | reg->ring_flag_eop;
1421 desc = card->rxbd_ring[rd_index];
1422 desc->paddr = buf_pa;
1423 desc->len = skb_tmp->len;
1427 if ((++card->rxbd_rdptr & reg->rx_mask) ==
1428 MWIFIEX_MAX_TXRX_BD) {
1429 card->rxbd_rdptr = ((card->rxbd_rdptr &
1430 reg->rx_rollover_ind) ^
1431 reg->rx_rollover_ind);
1433 mwifiex_dbg(adapter, DATA,
1434 "info: RECV DATA: <Rd: %#x, Wr: %#x>\n",
1435 card->rxbd_rdptr, wrptr);
1437 tx_val = card->txbd_wrptr & reg->tx_wrap_mask;
1438 /* Write the RX ring read pointer in to reg->rx_rdptr */
1439 if (mwifiex_write_reg(adapter, reg->rx_rdptr,
1440 card->rxbd_rdptr | tx_val)) {
1441 mwifiex_dbg(adapter, DATA,
1442 "RECV DATA: failed to write reg->rx_rdptr\n");
1447 /* Read the RX ring Write pointer set by firmware */
1448 if (mwifiex_read_reg(adapter, reg->rx_wrptr, &wrptr)) {
1449 mwifiex_dbg(adapter, ERROR,
1450 "RECV DATA: failed to read reg->rx_wrptr\n");
1454 mwifiex_dbg(adapter, DATA,
1455 "info: RECV DATA: Rcvd packet from fw successfully\n");
1456 card->rxbd_wrptr = wrptr;
1464 * This function downloads the boot command to device
1467 mwifiex_pcie_send_boot_cmd(struct mwifiex_adapter *adapter, struct sk_buff *skb)
1470 struct pcie_service_card *card = adapter->card;
1471 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1473 if (!(skb->data && skb->len)) {
1474 mwifiex_dbg(adapter, ERROR,
1475 "Invalid parameter in %s <%p. len %d>\n",
1476 __func__, skb->data, skb->len);
1480 if (mwifiex_map_pci_memory(adapter, skb, skb->len, PCI_DMA_TODEVICE))
1483 buf_pa = MWIFIEX_SKB_DMA_ADDR(skb);
1485 /* Write the lower 32bits of the physical address to low command
1486 * address scratch register
1488 if (mwifiex_write_reg(adapter, reg->cmd_addr_lo, (u32)buf_pa)) {
1489 mwifiex_dbg(adapter, ERROR,
1490 "%s: failed to write download command to boot code.\n",
1492 mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_TODEVICE);
1496 /* Write the upper 32bits of the physical address to high command
1497 * address scratch register
1499 if (mwifiex_write_reg(adapter, reg->cmd_addr_hi,
1500 (u32)((u64)buf_pa >> 32))) {
1501 mwifiex_dbg(adapter, ERROR,
1502 "%s: failed to write download command to boot code.\n",
1504 mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_TODEVICE);
1508 /* Write the command length to cmd_size scratch register */
1509 if (mwifiex_write_reg(adapter, reg->cmd_size, skb->len)) {
1510 mwifiex_dbg(adapter, ERROR,
1511 "%s: failed to write command len to cmd_size scratch reg\n",
1513 mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_TODEVICE);
1517 /* Ring the door bell */
1518 if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
1519 CPU_INTR_DOOR_BELL)) {
1520 mwifiex_dbg(adapter, ERROR,
1521 "%s: failed to assert door-bell intr\n", __func__);
1522 mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_TODEVICE);
1529 /* This function init rx port in firmware which in turn enables to receive data
1530 * from device before transmitting any packet.
1532 static int mwifiex_pcie_init_fw_port(struct mwifiex_adapter *adapter)
1534 struct pcie_service_card *card = adapter->card;
1535 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1536 int tx_wrap = card->txbd_wrptr & reg->tx_wrap_mask;
1538 /* Write the RX ring read pointer in to reg->rx_rdptr */
1539 if (mwifiex_write_reg(adapter, reg->rx_rdptr, card->rxbd_rdptr |
1541 mwifiex_dbg(adapter, ERROR,
1542 "RECV DATA: failed to write reg->rx_rdptr\n");
1548 /* This function downloads commands to the device
1551 mwifiex_pcie_send_cmd(struct mwifiex_adapter *adapter, struct sk_buff *skb)
1553 struct pcie_service_card *card = adapter->card;
1554 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1556 dma_addr_t cmd_buf_pa, cmdrsp_buf_pa;
1557 u8 *payload = (u8 *)skb->data;
1559 if (!(skb->data && skb->len)) {
1560 mwifiex_dbg(adapter, ERROR,
1561 "Invalid parameter in %s <%p, %#x>\n",
1562 __func__, skb->data, skb->len);
1566 /* Make sure a command response buffer is available */
1567 if (!card->cmdrsp_buf) {
1568 mwifiex_dbg(adapter, ERROR,
1569 "No response buffer available, send command failed\n");
1573 if (!mwifiex_pcie_ok_to_access_hw(adapter))
1574 mwifiex_pm_wakeup_card(adapter);
1576 adapter->cmd_sent = true;
1578 *(__le16 *)&payload[0] = cpu_to_le16((u16)skb->len);
1579 *(__le16 *)&payload[2] = cpu_to_le16(MWIFIEX_TYPE_CMD);
1581 if (mwifiex_map_pci_memory(adapter, skb, skb->len, PCI_DMA_TODEVICE))
1584 card->cmd_buf = skb;
1586 * Need to keep a reference, since core driver might free up this
1587 * buffer before we've unmapped it.
1591 /* To send a command, the driver will:
1592 1. Write the 64bit physical address of the data buffer to
1593 cmd response address low + cmd response address high
1594 2. Ring the door bell (i.e. set the door bell interrupt)
1596 In response to door bell interrupt, the firmware will perform
1597 the DMA of the command packet (first header to obtain the total
1598 length and then rest of the command).
1601 if (card->cmdrsp_buf) {
1602 cmdrsp_buf_pa = MWIFIEX_SKB_DMA_ADDR(card->cmdrsp_buf);
1603 /* Write the lower 32bits of the cmdrsp buffer physical
1605 if (mwifiex_write_reg(adapter, reg->cmdrsp_addr_lo,
1606 (u32)cmdrsp_buf_pa)) {
1607 mwifiex_dbg(adapter, ERROR,
1608 "Failed to write download cmd to boot code.\n");
1612 /* Write the upper 32bits of the cmdrsp buffer physical
1614 if (mwifiex_write_reg(adapter, reg->cmdrsp_addr_hi,
1615 (u32)((u64)cmdrsp_buf_pa >> 32))) {
1616 mwifiex_dbg(adapter, ERROR,
1617 "Failed to write download cmd to boot code.\n");
1623 cmd_buf_pa = MWIFIEX_SKB_DMA_ADDR(card->cmd_buf);
1624 /* Write the lower 32bits of the physical address to reg->cmd_addr_lo */
1625 if (mwifiex_write_reg(adapter, reg->cmd_addr_lo,
1627 mwifiex_dbg(adapter, ERROR,
1628 "Failed to write download cmd to boot code.\n");
1632 /* Write the upper 32bits of the physical address to reg->cmd_addr_hi */
1633 if (mwifiex_write_reg(adapter, reg->cmd_addr_hi,
1634 (u32)((u64)cmd_buf_pa >> 32))) {
1635 mwifiex_dbg(adapter, ERROR,
1636 "Failed to write download cmd to boot code.\n");
1641 /* Write the command length to reg->cmd_size */
1642 if (mwifiex_write_reg(adapter, reg->cmd_size,
1643 card->cmd_buf->len)) {
1644 mwifiex_dbg(adapter, ERROR,
1645 "Failed to write cmd len to reg->cmd_size\n");
1650 /* Ring the door bell */
1651 if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
1652 CPU_INTR_DOOR_BELL)) {
1653 mwifiex_dbg(adapter, ERROR,
1654 "Failed to assert door-bell intr\n");
1661 adapter->cmd_sent = false;
1667 * This function handles command complete interrupt
1669 static int mwifiex_pcie_process_cmd_complete(struct mwifiex_adapter *adapter)
1671 struct pcie_service_card *card = adapter->card;
1672 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1673 struct sk_buff *skb = card->cmdrsp_buf;
1678 mwifiex_dbg(adapter, CMD,
1679 "info: Rx CMD Response\n");
1681 mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_FROMDEVICE);
1683 /* Unmap the command as a response has been received. */
1684 if (card->cmd_buf) {
1685 mwifiex_unmap_pci_memory(adapter, card->cmd_buf,
1687 dev_kfree_skb_any(card->cmd_buf);
1688 card->cmd_buf = NULL;
1691 pkt_len = *((__le16 *)skb->data);
1692 rx_len = le16_to_cpu(pkt_len);
1693 skb_put(skb, MWIFIEX_UPLD_SIZE - skb->len);
1694 skb_trim(skb, rx_len);
1695 skb_pull(skb, INTF_HEADER_LEN);
1697 if (!adapter->curr_cmd) {
1698 if (adapter->ps_state == PS_STATE_SLEEP_CFM) {
1699 if (mwifiex_write_reg(adapter,
1701 CPU_INTR_SLEEP_CFM_DONE)) {
1702 mwifiex_dbg(adapter, ERROR,
1703 "Write register failed\n");
1706 mwifiex_delay_for_sleep_cookie(adapter,
1707 MWIFIEX_MAX_DELAY_COUNT);
1708 while (reg->sleep_cookie && (count++ < 10) &&
1709 mwifiex_pcie_ok_to_access_hw(adapter))
1710 usleep_range(50, 60);
1711 mwifiex_pcie_enable_host_int(adapter);
1712 mwifiex_process_sleep_confirm_resp(adapter, skb->data,
1715 mwifiex_dbg(adapter, ERROR,
1716 "There is no command but got cmdrsp\n");
1718 memcpy(adapter->upld_buf, skb->data,
1719 min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER, skb->len));
1720 skb_push(skb, INTF_HEADER_LEN);
1721 if (mwifiex_map_pci_memory(adapter, skb, MWIFIEX_UPLD_SIZE,
1722 PCI_DMA_FROMDEVICE))
1724 } else if (mwifiex_pcie_ok_to_access_hw(adapter)) {
1725 adapter->curr_cmd->resp_skb = skb;
1726 adapter->cmd_resp_received = true;
1727 /* Take the pointer and set it to CMD node and will
1728 return in the response complete callback */
1729 card->cmdrsp_buf = NULL;
1731 /* Clear the cmd-rsp buffer address in scratch registers. This
1732 will prevent firmware from writing to the same response
1734 if (mwifiex_write_reg(adapter, reg->cmdrsp_addr_lo, 0)) {
1735 mwifiex_dbg(adapter, ERROR,
1736 "cmd_done: failed to clear cmd_rsp_addr_lo\n");
1739 /* Write the upper 32bits of the cmdrsp buffer physical
1741 if (mwifiex_write_reg(adapter, reg->cmdrsp_addr_hi, 0)) {
1742 mwifiex_dbg(adapter, ERROR,
1743 "cmd_done: failed to clear cmd_rsp_addr_hi\n");
1752 * Command Response processing complete handler
1754 static int mwifiex_pcie_cmdrsp_complete(struct mwifiex_adapter *adapter,
1755 struct sk_buff *skb)
1757 struct pcie_service_card *card = adapter->card;
1760 card->cmdrsp_buf = skb;
1761 skb_push(card->cmdrsp_buf, INTF_HEADER_LEN);
1762 if (mwifiex_map_pci_memory(adapter, skb, MWIFIEX_UPLD_SIZE,
1763 PCI_DMA_FROMDEVICE))
1771 * This function handles firmware event ready interrupt
1773 static int mwifiex_pcie_process_event_ready(struct mwifiex_adapter *adapter)
1775 struct pcie_service_card *card = adapter->card;
1776 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1777 u32 rdptr = card->evtbd_rdptr & MWIFIEX_EVTBD_MASK;
1779 struct mwifiex_evt_buf_desc *desc;
1781 if (!mwifiex_pcie_ok_to_access_hw(adapter))
1782 mwifiex_pm_wakeup_card(adapter);
1784 if (adapter->event_received) {
1785 mwifiex_dbg(adapter, EVENT,
1786 "info: Event being processed,\t"
1787 "do not process this interrupt just yet\n");
1791 if (rdptr >= MWIFIEX_MAX_EVT_BD) {
1792 mwifiex_dbg(adapter, ERROR,
1793 "info: Invalid read pointer...\n");
1797 /* Read the event ring write pointer set by firmware */
1798 if (mwifiex_read_reg(adapter, reg->evt_wrptr, &wrptr)) {
1799 mwifiex_dbg(adapter, ERROR,
1800 "EventReady: failed to read reg->evt_wrptr\n");
1804 mwifiex_dbg(adapter, EVENT,
1805 "info: EventReady: Initial <Rd: 0x%x, Wr: 0x%x>",
1806 card->evtbd_rdptr, wrptr);
1807 if (((wrptr & MWIFIEX_EVTBD_MASK) != (card->evtbd_rdptr
1808 & MWIFIEX_EVTBD_MASK)) ||
1809 ((wrptr & reg->evt_rollover_ind) ==
1810 (card->evtbd_rdptr & reg->evt_rollover_ind))) {
1811 struct sk_buff *skb_cmd;
1812 __le16 data_len = 0;
1815 mwifiex_dbg(adapter, INFO,
1816 "info: Read Index: %d\n", rdptr);
1817 skb_cmd = card->evt_buf_list[rdptr];
1818 mwifiex_unmap_pci_memory(adapter, skb_cmd, PCI_DMA_FROMDEVICE);
1820 /* Take the pointer and set it to event pointer in adapter
1821 and will return back after event handling callback */
1822 card->evt_buf_list[rdptr] = NULL;
1823 desc = card->evtbd_ring[rdptr];
1824 memset(desc, 0, sizeof(*desc));
1826 event = *(u32 *) &skb_cmd->data[INTF_HEADER_LEN];
1827 adapter->event_cause = event;
1828 /* The first 4bytes will be the event transfer header
1829 len is 2 bytes followed by type which is 2 bytes */
1830 memcpy(&data_len, skb_cmd->data, sizeof(__le16));
1831 evt_len = le16_to_cpu(data_len);
1832 skb_trim(skb_cmd, evt_len);
1833 skb_pull(skb_cmd, INTF_HEADER_LEN);
1834 mwifiex_dbg(adapter, EVENT,
1835 "info: Event length: %d\n", evt_len);
1837 if ((evt_len > 0) && (evt_len < MAX_EVENT_SIZE))
1838 memcpy(adapter->event_body, skb_cmd->data +
1839 MWIFIEX_EVENT_HEADER_LEN, evt_len -
1840 MWIFIEX_EVENT_HEADER_LEN);
1842 adapter->event_received = true;
1843 adapter->event_skb = skb_cmd;
1845 /* Do not update the event read pointer here, wait till the
1846 buffer is released. This is just to make things simpler,
1847 we need to find a better method of managing these buffers.
1850 if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
1851 CPU_INTR_EVENT_DONE)) {
1852 mwifiex_dbg(adapter, ERROR,
1853 "Write register failed\n");
1862 * Event processing complete handler
1864 static int mwifiex_pcie_event_complete(struct mwifiex_adapter *adapter,
1865 struct sk_buff *skb)
1867 struct pcie_service_card *card = adapter->card;
1868 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1870 u32 rdptr = card->evtbd_rdptr & MWIFIEX_EVTBD_MASK;
1872 struct mwifiex_evt_buf_desc *desc;
1877 if (rdptr >= MWIFIEX_MAX_EVT_BD) {
1878 mwifiex_dbg(adapter, ERROR,
1879 "event_complete: Invalid rdptr 0x%x\n",
1884 /* Read the event ring write pointer set by firmware */
1885 if (mwifiex_read_reg(adapter, reg->evt_wrptr, &wrptr)) {
1886 mwifiex_dbg(adapter, ERROR,
1887 "event_complete: failed to read reg->evt_wrptr\n");
1891 if (!card->evt_buf_list[rdptr]) {
1892 skb_push(skb, INTF_HEADER_LEN);
1893 skb_put(skb, MAX_EVENT_SIZE - skb->len);
1894 if (mwifiex_map_pci_memory(adapter, skb,
1896 PCI_DMA_FROMDEVICE))
1898 card->evt_buf_list[rdptr] = skb;
1899 desc = card->evtbd_ring[rdptr];
1900 desc->paddr = MWIFIEX_SKB_DMA_ADDR(skb);
1901 desc->len = (u16)skb->len;
1905 mwifiex_dbg(adapter, ERROR,
1906 "info: ERROR: buf still valid at index %d, <%p, %p>\n",
1907 rdptr, card->evt_buf_list[rdptr], skb);
1910 if ((++card->evtbd_rdptr & MWIFIEX_EVTBD_MASK) == MWIFIEX_MAX_EVT_BD) {
1911 card->evtbd_rdptr = ((card->evtbd_rdptr &
1912 reg->evt_rollover_ind) ^
1913 reg->evt_rollover_ind);
1916 mwifiex_dbg(adapter, EVENT,
1917 "info: Updated <Rd: 0x%x, Wr: 0x%x>",
1918 card->evtbd_rdptr, wrptr);
1920 /* Write the event ring read pointer in to reg->evt_rdptr */
1921 if (mwifiex_write_reg(adapter, reg->evt_rdptr,
1922 card->evtbd_rdptr)) {
1923 mwifiex_dbg(adapter, ERROR,
1924 "event_complete: failed to read reg->evt_rdptr\n");
1928 mwifiex_dbg(adapter, EVENT,
1929 "info: Check Events Again\n");
1930 ret = mwifiex_pcie_process_event_ready(adapter);
1936 * This function downloads the firmware to the card.
1938 * Firmware is downloaded to the card in blocks. Every block download
1939 * is tested for CRC errors, and retried a number of times before
1940 * returning failure.
1942 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
1943 struct mwifiex_fw_image *fw)
1946 u8 *firmware = fw->fw_buf;
1947 u32 firmware_len = fw->fw_len;
1949 struct sk_buff *skb;
1950 u32 txlen, tx_blocks = 0, tries, len;
1951 u32 block_retry_cnt = 0;
1952 struct pcie_service_card *card = adapter->card;
1953 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1955 if (!firmware || !firmware_len) {
1956 mwifiex_dbg(adapter, ERROR,
1957 "No firmware image found! Terminating download\n");
1961 mwifiex_dbg(adapter, INFO,
1962 "info: Downloading FW image (%d bytes)\n",
1965 if (mwifiex_pcie_disable_host_int(adapter)) {
1966 mwifiex_dbg(adapter, ERROR,
1967 "%s: Disabling interrupts failed.\n", __func__);
1971 skb = dev_alloc_skb(MWIFIEX_UPLD_SIZE);
1977 /* Perform firmware data transfer */
1982 if (offset >= firmware_len)
1985 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
1986 ret = mwifiex_read_reg(adapter, reg->cmd_size,
1989 mwifiex_dbg(adapter, FATAL,
1990 "Failed reading len from boot code\n");
1995 usleep_range(10, 20);
2000 } else if (len > MWIFIEX_UPLD_SIZE) {
2001 mwifiex_dbg(adapter, ERROR,
2002 "FW download failure @ %d, invalid length %d\n",
2012 if (block_retry_cnt > MAX_WRITE_IOMEM_RETRY) {
2013 mwifiex_dbg(adapter, ERROR,
2014 "FW download failure @ %d, over max\t"
2015 "retry count\n", offset);
2019 mwifiex_dbg(adapter, ERROR,
2020 "FW CRC error indicated by the\t"
2021 "helper: len = 0x%04X, txlen = %d\n",
2024 /* Setting this to 0 to resend from same offset */
2027 block_retry_cnt = 0;
2028 /* Set blocksize to transfer - checking for
2030 if (firmware_len - offset < txlen)
2031 txlen = firmware_len - offset;
2033 tx_blocks = (txlen + card->pcie.blksz_fw_dl - 1) /
2034 card->pcie.blksz_fw_dl;
2036 /* Copy payload to buffer */
2037 memmove(skb->data, &firmware[offset], txlen);
2040 skb_put(skb, MWIFIEX_UPLD_SIZE - skb->len);
2041 skb_trim(skb, tx_blocks * card->pcie.blksz_fw_dl);
2043 /* Send the boot command to device */
2044 if (mwifiex_pcie_send_boot_cmd(adapter, skb)) {
2045 mwifiex_dbg(adapter, ERROR,
2046 "Failed to send firmware download command\n");
2051 /* Wait for the command done interrupt */
2053 if (mwifiex_read_reg(adapter, PCIE_CPU_INT_STATUS,
2055 mwifiex_dbg(adapter, ERROR,
2056 "%s: Failed to read\t"
2057 "interrupt status during fw dnld.\n",
2059 mwifiex_unmap_pci_memory(adapter, skb,
2064 } while ((ireg_intr & CPU_INTR_DOOR_BELL) ==
2065 CPU_INTR_DOOR_BELL);
2067 mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_TODEVICE);
2072 mwifiex_dbg(adapter, MSG,
2073 "info: FW download over, size %d bytes\n", offset);
2078 dev_kfree_skb_any(skb);
2083 * This function checks the firmware status in card.
2086 mwifiex_check_fw_status(struct mwifiex_adapter *adapter, u32 poll_num)
2090 struct pcie_service_card *card = adapter->card;
2091 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
2094 /* Mask spurios interrupts */
2095 if (mwifiex_write_reg(adapter, PCIE_HOST_INT_STATUS_MASK,
2097 mwifiex_dbg(adapter, ERROR,
2098 "Write register failed\n");
2102 mwifiex_dbg(adapter, INFO,
2103 "Setting driver ready signature\n");
2104 if (mwifiex_write_reg(adapter, reg->drv_rdy,
2105 FIRMWARE_READY_PCIE)) {
2106 mwifiex_dbg(adapter, ERROR,
2107 "Failed to write driver ready signature\n");
2111 /* Wait for firmware initialization event */
2112 for (tries = 0; tries < poll_num; tries++) {
2113 if (mwifiex_read_reg(adapter, reg->fw_status,
2119 mwifiex_dbg(adapter, INFO, "Try %d if FW is ready <%d,%#x>",
2120 tries, ret, firmware_stat);
2124 if (firmware_stat == FIRMWARE_READY_PCIE) {
2136 /* This function checks if WLAN is the winner.
2139 mwifiex_check_winner_status(struct mwifiex_adapter *adapter)
2143 struct pcie_service_card *card = adapter->card;
2144 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
2146 if (mwifiex_read_reg(adapter, reg->fw_status, &winner)) {
2148 } else if (!winner) {
2149 mwifiex_dbg(adapter, INFO, "PCI-E is the winner\n");
2150 adapter->winner = 1;
2152 mwifiex_dbg(adapter, ERROR,
2153 "PCI-E is not the winner <%#x>", winner);
2160 * This function reads the interrupt status from card.
2162 static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter,
2166 unsigned long flags;
2167 struct pcie_service_card *card = adapter->card;
2169 if (card->msi_enable) {
2170 spin_lock_irqsave(&adapter->int_lock, flags);
2171 adapter->int_status = 1;
2172 spin_unlock_irqrestore(&adapter->int_lock, flags);
2176 if (!mwifiex_pcie_ok_to_access_hw(adapter))
2179 if (card->msix_enable && msg_id >= 0) {
2180 pcie_ireg = BIT(msg_id);
2182 if (mwifiex_read_reg(adapter, PCIE_HOST_INT_STATUS,
2184 mwifiex_dbg(adapter, ERROR, "Read register failed\n");
2188 if ((pcie_ireg == 0xFFFFFFFF) || !pcie_ireg)
2192 mwifiex_pcie_disable_host_int(adapter);
2194 /* Clear the pending interrupts */
2195 if (mwifiex_write_reg(adapter, PCIE_HOST_INT_STATUS,
2197 mwifiex_dbg(adapter, ERROR,
2198 "Write register failed\n");
2203 if (!adapter->pps_uapsd_mode &&
2204 adapter->ps_state == PS_STATE_SLEEP &&
2205 mwifiex_pcie_ok_to_access_hw(adapter)) {
2206 /* Potentially for PCIe we could get other
2207 * interrupts like shared. Don't change power
2208 * state until cookie is set
2210 adapter->ps_state = PS_STATE_AWAKE;
2211 adapter->pm_wakeup_fw_try = false;
2212 del_timer(&adapter->wakeup_timer);
2215 spin_lock_irqsave(&adapter->int_lock, flags);
2216 adapter->int_status |= pcie_ireg;
2217 spin_unlock_irqrestore(&adapter->int_lock, flags);
2218 mwifiex_dbg(adapter, INTR, "ireg: 0x%08x\n", pcie_ireg);
2222 * Interrupt handler for PCIe root port
2224 * This function reads the interrupt status from firmware and assigns
2225 * the main process in workqueue which will handle the interrupt.
2227 static irqreturn_t mwifiex_pcie_interrupt(int irq, void *context)
2229 struct mwifiex_msix_context *ctx = context;
2230 struct pci_dev *pdev = ctx->dev;
2231 struct pcie_service_card *card;
2232 struct mwifiex_adapter *adapter;
2235 pr_err("info: %s: pdev is NULL\n", __func__);
2239 card = pci_get_drvdata(pdev);
2240 if (!card || !card->adapter) {
2241 pr_err("info: %s: card=%p adapter=%p\n", __func__, card,
2242 card ? card->adapter : NULL);
2245 adapter = card->adapter;
2247 if (adapter->surprise_removed)
2250 if (card->msix_enable)
2251 mwifiex_interrupt_status(adapter, ctx->msg_id);
2253 mwifiex_interrupt_status(adapter, -1);
2255 mwifiex_queue_main_work(adapter);
2262 * This function checks the current interrupt status.
2264 * The following interrupts are checked and handled by this function -
2267 * - Command received
2268 * - Packets received
2271 * In case of Rx packets received, the packets are uploaded from card to
2272 * host and processed accordingly.
2274 static int mwifiex_process_pcie_int(struct mwifiex_adapter *adapter)
2278 unsigned long flags;
2279 struct pcie_service_card *card = adapter->card;
2281 spin_lock_irqsave(&adapter->int_lock, flags);
2282 if (!card->msi_enable) {
2283 /* Clear out unused interrupts */
2284 pcie_ireg = adapter->int_status;
2286 adapter->int_status = 0;
2287 spin_unlock_irqrestore(&adapter->int_lock, flags);
2289 if (card->msi_enable) {
2290 if (mwifiex_pcie_ok_to_access_hw(adapter)) {
2291 if (mwifiex_read_reg(adapter, PCIE_HOST_INT_STATUS,
2293 mwifiex_dbg(adapter, ERROR,
2294 "Read register failed\n");
2298 if ((pcie_ireg != 0xFFFFFFFF) && (pcie_ireg)) {
2299 if (mwifiex_write_reg(adapter,
2300 PCIE_HOST_INT_STATUS,
2302 mwifiex_dbg(adapter, ERROR,
2303 "Write register failed\n");
2306 if (!adapter->pps_uapsd_mode &&
2307 adapter->ps_state == PS_STATE_SLEEP) {
2308 adapter->ps_state = PS_STATE_AWAKE;
2309 adapter->pm_wakeup_fw_try = false;
2310 del_timer(&adapter->wakeup_timer);
2315 while (pcie_ireg & HOST_INTR_MASK) {
2316 if (pcie_ireg & HOST_INTR_DNLD_DONE) {
2317 pcie_ireg &= ~HOST_INTR_DNLD_DONE;
2318 mwifiex_dbg(adapter, INTR,
2319 "info: TX DNLD Done\n");
2320 ret = mwifiex_pcie_send_data_complete(adapter);
2324 if (pcie_ireg & HOST_INTR_UPLD_RDY) {
2325 pcie_ireg &= ~HOST_INTR_UPLD_RDY;
2326 mwifiex_dbg(adapter, INTR,
2328 ret = mwifiex_pcie_process_recv_data(adapter);
2332 if (pcie_ireg & HOST_INTR_EVENT_RDY) {
2333 pcie_ireg &= ~HOST_INTR_EVENT_RDY;
2334 mwifiex_dbg(adapter, INTR,
2335 "info: Rx EVENT\n");
2336 ret = mwifiex_pcie_process_event_ready(adapter);
2341 if (pcie_ireg & HOST_INTR_CMD_DONE) {
2342 pcie_ireg &= ~HOST_INTR_CMD_DONE;
2343 if (adapter->cmd_sent) {
2344 mwifiex_dbg(adapter, INTR,
2345 "info: CMD sent Interrupt\n");
2346 adapter->cmd_sent = false;
2348 /* Handle command response */
2349 ret = mwifiex_pcie_process_cmd_complete(adapter);
2352 if (adapter->hs_activated)
2356 if (card->msi_enable) {
2357 spin_lock_irqsave(&adapter->int_lock, flags);
2358 adapter->int_status = 0;
2359 spin_unlock_irqrestore(&adapter->int_lock, flags);
2362 if (mwifiex_pcie_ok_to_access_hw(adapter)) {
2363 if (mwifiex_read_reg(adapter, PCIE_HOST_INT_STATUS,
2365 mwifiex_dbg(adapter, ERROR,
2366 "Read register failed\n");
2370 if ((pcie_ireg != 0xFFFFFFFF) && (pcie_ireg)) {
2371 if (mwifiex_write_reg(adapter,
2372 PCIE_HOST_INT_STATUS,
2374 mwifiex_dbg(adapter, ERROR,
2375 "Write register failed\n");
2381 if (!card->msi_enable) {
2382 spin_lock_irqsave(&adapter->int_lock, flags);
2383 pcie_ireg |= adapter->int_status;
2384 adapter->int_status = 0;
2385 spin_unlock_irqrestore(&adapter->int_lock, flags);
2388 mwifiex_dbg(adapter, INTR,
2389 "info: cmd_sent=%d data_sent=%d\n",
2390 adapter->cmd_sent, adapter->data_sent);
2391 if (!card->msi_enable && adapter->ps_state != PS_STATE_SLEEP)
2392 mwifiex_pcie_enable_host_int(adapter);
2397 static int mwifiex_process_msix_int(struct mwifiex_adapter *adapter)
2401 unsigned long flags;
2403 spin_lock_irqsave(&adapter->int_lock, flags);
2404 /* Clear out unused interrupts */
2405 pcie_ireg = adapter->int_status;
2406 adapter->int_status = 0;
2407 spin_unlock_irqrestore(&adapter->int_lock, flags);
2409 if (pcie_ireg & HOST_INTR_DNLD_DONE) {
2410 mwifiex_dbg(adapter, INTR,
2411 "info: TX DNLD Done\n");
2412 ret = mwifiex_pcie_send_data_complete(adapter);
2416 if (pcie_ireg & HOST_INTR_UPLD_RDY) {
2417 mwifiex_dbg(adapter, INTR,
2419 ret = mwifiex_pcie_process_recv_data(adapter);
2423 if (pcie_ireg & HOST_INTR_EVENT_RDY) {
2424 mwifiex_dbg(adapter, INTR,
2425 "info: Rx EVENT\n");
2426 ret = mwifiex_pcie_process_event_ready(adapter);
2431 if (pcie_ireg & HOST_INTR_CMD_DONE) {
2432 if (adapter->cmd_sent) {
2433 mwifiex_dbg(adapter, INTR,
2434 "info: CMD sent Interrupt\n");
2435 adapter->cmd_sent = false;
2437 /* Handle command response */
2438 ret = mwifiex_pcie_process_cmd_complete(adapter);
2443 mwifiex_dbg(adapter, INTR,
2444 "info: cmd_sent=%d data_sent=%d\n",
2445 adapter->cmd_sent, adapter->data_sent);
2450 static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
2452 struct pcie_service_card *card = adapter->card;
2454 if (card->msix_enable)
2455 return mwifiex_process_msix_int(adapter);
2457 return mwifiex_process_pcie_int(adapter);
2461 * This function downloads data from driver to card.
2463 * Both commands and data packets are transferred to the card by this
2466 * This function adds the PCIE specific header to the front of the buffer
2467 * before transferring. The header contains the length of the packet and
2468 * the type. The firmware handles the packets based upon this set type.
2470 static int mwifiex_pcie_host_to_card(struct mwifiex_adapter *adapter, u8 type,
2471 struct sk_buff *skb,
2472 struct mwifiex_tx_param *tx_param)
2475 mwifiex_dbg(adapter, ERROR,
2476 "Passed NULL skb to %s\n", __func__);
2480 if (type == MWIFIEX_TYPE_DATA)
2481 return mwifiex_pcie_send_data(adapter, skb, tx_param);
2482 else if (type == MWIFIEX_TYPE_CMD)
2483 return mwifiex_pcie_send_cmd(adapter, skb);
2488 /* Function to dump PCIE scratch registers in case of FW crash
2491 mwifiex_pcie_reg_dump(struct mwifiex_adapter *adapter, char *drv_buf)
2494 char buf[256], *ptr;
2497 struct pcie_service_card *card = adapter->card;
2498 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
2499 int pcie_scratch_reg[] = {PCIE_SCRATCH_12_REG,
2500 PCIE_SCRATCH_13_REG,
2501 PCIE_SCRATCH_14_REG};
2506 mwifiex_dbg(adapter, MSG, "PCIE register dump start\n");
2508 if (mwifiex_read_reg(adapter, reg->fw_status, &value)) {
2509 mwifiex_dbg(adapter, ERROR, "failed to read firmware status");
2514 mwifiex_dbg(adapter, MSG, "pcie scratch register:");
2515 for (i = 0; i < ARRAY_SIZE(pcie_scratch_reg); i++) {
2516 mwifiex_read_reg(adapter, pcie_scratch_reg[i], &value);
2517 ptr += sprintf(ptr, "reg:0x%x, value=0x%x\n",
2518 pcie_scratch_reg[i], value);
2521 mwifiex_dbg(adapter, MSG, "%s\n", buf);
2522 p += sprintf(p, "%s\n", buf);
2524 mwifiex_dbg(adapter, MSG, "PCIE register dump end\n");
2529 /* This function read/write firmware */
2530 static enum rdwr_status
2531 mwifiex_pcie_rdwr_firmware(struct mwifiex_adapter *adapter, u8 doneflag)
2536 struct pcie_service_card *card = adapter->card;
2537 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
2539 if (mwifiex_read_reg(adapter, reg->fw_status, &fw_status))
2540 return RDWR_STATUS_FAILURE;
2542 ret = mwifiex_write_reg(adapter, reg->fw_dump_ctrl,
2543 reg->fw_dump_host_ready);
2545 mwifiex_dbg(adapter, ERROR,
2546 "PCIE write err\n");
2547 return RDWR_STATUS_FAILURE;
2550 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
2551 mwifiex_read_reg_byte(adapter, reg->fw_dump_ctrl, &ctrl_data);
2552 if (ctrl_data == FW_DUMP_DONE)
2553 return RDWR_STATUS_SUCCESS;
2554 if (doneflag && ctrl_data == doneflag)
2555 return RDWR_STATUS_DONE;
2556 if (ctrl_data != reg->fw_dump_host_ready) {
2557 mwifiex_dbg(adapter, WARN,
2558 "The ctrl reg was changed, re-try again!\n");
2559 ret = mwifiex_write_reg(adapter, reg->fw_dump_ctrl,
2560 reg->fw_dump_host_ready);
2562 mwifiex_dbg(adapter, ERROR,
2563 "PCIE write err\n");
2564 return RDWR_STATUS_FAILURE;
2567 usleep_range(100, 200);
2570 mwifiex_dbg(adapter, ERROR, "Fail to pull ctrl_data\n");
2571 return RDWR_STATUS_FAILURE;
2574 /* This function dump firmware memory to file */
2575 static void mwifiex_pcie_fw_dump(struct mwifiex_adapter *adapter)
2577 struct pcie_service_card *card = adapter->card;
2578 const struct mwifiex_pcie_card_reg *creg = card->pcie.reg;
2579 unsigned int reg, reg_start, reg_end;
2580 u8 *dbg_ptr, *end_ptr, *tmp_ptr, fw_dump_num, dump_num;
2581 u8 idx, i, read_reg, doneflag = 0;
2582 enum rdwr_status stat;
2586 if (!card->pcie.can_dump_fw)
2589 for (idx = 0; idx < adapter->num_mem_types; idx++) {
2590 struct memory_type_mapping *entry =
2591 &adapter->mem_type_mapping_tbl[idx];
2593 if (entry->mem_ptr) {
2594 vfree(entry->mem_ptr);
2595 entry->mem_ptr = NULL;
2597 entry->mem_size = 0;
2600 mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump start ==\n");
2602 /* Read the number of the memories which will dump */
2603 stat = mwifiex_pcie_rdwr_firmware(adapter, doneflag);
2604 if (stat == RDWR_STATUS_FAILURE)
2607 reg = creg->fw_dump_start;
2608 mwifiex_read_reg_byte(adapter, reg, &fw_dump_num);
2610 /* W8997 chipset firmware dump will be restore in single region*/
2611 if (fw_dump_num == 0)
2614 dump_num = fw_dump_num;
2616 /* Read the length of every memory which will dump */
2617 for (idx = 0; idx < dump_num; idx++) {
2618 struct memory_type_mapping *entry =
2619 &adapter->mem_type_mapping_tbl[idx];
2621 if (fw_dump_num != 0) {
2622 stat = mwifiex_pcie_rdwr_firmware(adapter, doneflag);
2623 if (stat == RDWR_STATUS_FAILURE)
2626 reg = creg->fw_dump_start;
2627 for (i = 0; i < 4; i++) {
2628 mwifiex_read_reg_byte(adapter, reg, &read_reg);
2629 memory_size |= (read_reg << (i * 8));
2633 memory_size = MWIFIEX_FW_DUMP_MAX_MEMSIZE;
2636 if (memory_size == 0) {
2637 mwifiex_dbg(adapter, MSG, "Firmware dump Finished!\n");
2638 ret = mwifiex_write_reg(adapter, creg->fw_dump_ctrl,
2639 creg->fw_dump_read_done);
2641 mwifiex_dbg(adapter, ERROR, "PCIE write err\n");
2647 mwifiex_dbg(adapter, DUMP,
2648 "%s_SIZE=0x%x\n", entry->mem_name, memory_size);
2649 entry->mem_ptr = vmalloc(memory_size + 1);
2650 entry->mem_size = memory_size;
2651 if (!entry->mem_ptr) {
2652 mwifiex_dbg(adapter, ERROR,
2653 "Vmalloc %s failed\n", entry->mem_name);
2656 dbg_ptr = entry->mem_ptr;
2657 end_ptr = dbg_ptr + memory_size;
2659 doneflag = entry->done_flag;
2660 mwifiex_dbg(adapter, DUMP, "Start %s output, please wait...\n",
2664 stat = mwifiex_pcie_rdwr_firmware(adapter, doneflag);
2665 if (RDWR_STATUS_FAILURE == stat)
2668 reg_start = creg->fw_dump_start;
2669 reg_end = creg->fw_dump_end;
2670 for (reg = reg_start; reg <= reg_end; reg++) {
2671 mwifiex_read_reg_byte(adapter, reg, dbg_ptr);
2672 if (dbg_ptr < end_ptr) {
2676 mwifiex_dbg(adapter, ERROR,
2677 "pre-allocated buf not enough\n");
2679 vzalloc(memory_size + MWIFIEX_SIZE_4K);
2682 memcpy(tmp_ptr, entry->mem_ptr, memory_size);
2683 vfree(entry->mem_ptr);
2684 entry->mem_ptr = tmp_ptr;
2686 dbg_ptr = entry->mem_ptr + memory_size;
2687 memory_size += MWIFIEX_SIZE_4K;
2688 end_ptr = entry->mem_ptr + memory_size;
2691 if (stat != RDWR_STATUS_DONE)
2694 mwifiex_dbg(adapter, DUMP,
2695 "%s done: size=0x%tx\n",
2696 entry->mem_name, dbg_ptr - entry->mem_ptr);
2700 mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump end ==\n");
2703 static void mwifiex_pcie_device_dump_work(struct mwifiex_adapter *adapter)
2705 mwifiex_drv_info_dump(adapter);
2706 mwifiex_pcie_fw_dump(adapter);
2707 mwifiex_upload_device_dump(adapter);
2710 static unsigned long iface_work_flags;
2711 static struct mwifiex_adapter *save_adapter;
2712 static void mwifiex_pcie_work(struct work_struct *work)
2714 if (test_and_clear_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP,
2716 mwifiex_pcie_device_dump_work(save_adapter);
2719 static DECLARE_WORK(pcie_work, mwifiex_pcie_work);
2720 /* This function dumps FW information */
2721 static void mwifiex_pcie_device_dump(struct mwifiex_adapter *adapter)
2723 save_adapter = adapter;
2724 if (test_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP, &iface_work_flags))
2727 set_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP, &iface_work_flags);
2729 schedule_work(&pcie_work);
2732 static void mwifiex_pcie_free_buffers(struct mwifiex_adapter *adapter)
2734 struct pcie_service_card *card = adapter->card;
2735 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
2737 if (reg->sleep_cookie)
2738 mwifiex_pcie_delete_sleep_cookie_buf(adapter);
2740 mwifiex_pcie_delete_cmdrsp_buf(adapter);
2741 mwifiex_pcie_delete_evtbd_ring(adapter);
2742 mwifiex_pcie_delete_rxbd_ring(adapter);
2743 mwifiex_pcie_delete_txbd_ring(adapter);
2744 card->cmdrsp_buf = NULL;
2748 * This function initializes the PCI-E host memory space, WCB rings, etc.
2750 * The following initializations steps are followed -
2751 * - Allocate TXBD ring buffers
2752 * - Allocate RXBD ring buffers
2753 * - Allocate event BD ring buffers
2754 * - Allocate command response ring buffer
2755 * - Allocate sleep cookie buffer
2757 static int mwifiex_pcie_init(struct mwifiex_adapter *adapter)
2759 struct pcie_service_card *card = adapter->card;
2761 struct pci_dev *pdev = card->dev;
2762 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
2764 pci_set_drvdata(pdev, card);
2766 ret = pci_enable_device(pdev);
2768 goto err_enable_dev;
2770 pci_set_master(pdev);
2772 pr_notice("try set_consistent_dma_mask(32)\n");
2773 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2775 pr_err("set_dma_mask(32) failed\n");
2776 goto err_set_dma_mask;
2779 ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2781 pr_err("set_consistent_dma_mask(64) failed\n");
2782 goto err_set_dma_mask;
2785 ret = pci_request_region(pdev, 0, DRV_NAME);
2787 pr_err("req_reg(0) error\n");
2788 goto err_req_region0;
2790 card->pci_mmap = pci_iomap(pdev, 0, 0);
2791 if (!card->pci_mmap) {
2792 pr_err("iomap(0) error\n");
2796 ret = pci_request_region(pdev, 2, DRV_NAME);
2798 pr_err("req_reg(2) error\n");
2799 goto err_req_region2;
2801 card->pci_mmap1 = pci_iomap(pdev, 2, 0);
2802 if (!card->pci_mmap1) {
2803 pr_err("iomap(2) error\n");
2808 pr_notice("PCI memory map Virt0: %p PCI memory map Virt2: %p\n",
2809 card->pci_mmap, card->pci_mmap1);
2811 card->cmdrsp_buf = NULL;
2812 ret = mwifiex_pcie_create_txbd_ring(adapter);
2815 ret = mwifiex_pcie_create_rxbd_ring(adapter);
2818 ret = mwifiex_pcie_create_evtbd_ring(adapter);
2821 ret = mwifiex_pcie_alloc_cmdrsp_buf(adapter);
2823 goto err_alloc_cmdbuf;
2824 if (reg->sleep_cookie) {
2825 ret = mwifiex_pcie_alloc_sleep_cookie_buf(adapter);
2827 goto err_alloc_cookie;
2829 card->sleep_cookie_vbase = NULL;
2834 mwifiex_pcie_delete_cmdrsp_buf(adapter);
2836 mwifiex_pcie_delete_evtbd_ring(adapter);
2838 mwifiex_pcie_delete_rxbd_ring(adapter);
2840 mwifiex_pcie_delete_txbd_ring(adapter);
2842 pci_iounmap(pdev, card->pci_mmap1);
2844 pci_release_region(pdev, 2);
2846 pci_iounmap(pdev, card->pci_mmap);
2848 pci_release_region(pdev, 0);
2851 pci_disable_device(pdev);
2853 pci_set_drvdata(pdev, NULL);
2858 * This function cleans up the allocated card buffers.
2860 static void mwifiex_pcie_cleanup(struct mwifiex_adapter *adapter)
2862 struct pcie_service_card *card = adapter->card;
2863 struct pci_dev *pdev = card->dev;
2864 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
2867 mwifiex_dbg(adapter, INFO,
2868 "Clearing driver ready signature\n");
2869 if (mwifiex_write_reg(adapter, reg->drv_rdy, 0x00000000))
2870 mwifiex_dbg(adapter, ERROR,
2871 "Failed to write driver not-ready signature\n");
2874 mwifiex_pcie_free_buffers(adapter);
2877 pci_iounmap(pdev, card->pci_mmap);
2878 pci_iounmap(pdev, card->pci_mmap1);
2879 pci_disable_device(pdev);
2880 pci_release_region(pdev, 2);
2881 pci_release_region(pdev, 0);
2882 pci_set_drvdata(pdev, NULL);
2887 static int mwifiex_pcie_request_irq(struct mwifiex_adapter *adapter)
2890 struct pcie_service_card *card = adapter->card;
2891 struct pci_dev *pdev = card->dev;
2893 if (card->pcie.reg->msix_support) {
2894 for (i = 0; i < MWIFIEX_NUM_MSIX_VECTORS; i++)
2895 card->msix_entries[i].entry = i;
2896 ret = pci_enable_msix_exact(pdev, card->msix_entries,
2897 MWIFIEX_NUM_MSIX_VECTORS);
2899 for (i = 0; i < MWIFIEX_NUM_MSIX_VECTORS; i++) {
2900 card->msix_ctx[i].dev = pdev;
2901 card->msix_ctx[i].msg_id = i;
2903 ret = request_irq(card->msix_entries[i].vector,
2904 mwifiex_pcie_interrupt, 0,
2905 "MWIFIEX_PCIE_MSIX",
2906 &card->msix_ctx[i]);
2912 mwifiex_dbg(adapter, INFO, "request_irq fail: %d\n",
2914 for (j = 0; j < i; j++)
2915 free_irq(card->msix_entries[j].vector,
2916 &card->msix_ctx[i]);
2917 pci_disable_msix(pdev);
2919 mwifiex_dbg(adapter, MSG, "MSIx enabled!");
2920 card->msix_enable = 1;
2926 if (pci_enable_msi(pdev) != 0)
2927 pci_disable_msi(pdev);
2929 card->msi_enable = 1;
2931 mwifiex_dbg(adapter, INFO, "msi_enable = %d\n", card->msi_enable);
2933 card->share_irq_ctx.dev = pdev;
2934 card->share_irq_ctx.msg_id = -1;
2935 ret = request_irq(pdev->irq, mwifiex_pcie_interrupt, IRQF_SHARED,
2936 "MRVL_PCIE", &card->share_irq_ctx);
2938 pr_err("request_irq failed: ret=%d\n", ret);
2946 * This function gets the firmware name for downloading by revision id
2948 * Read revision id register to get revision id
2950 static void mwifiex_pcie_get_fw_name(struct mwifiex_adapter *adapter)
2952 int revision_id = 0;
2954 struct pcie_service_card *card = adapter->card;
2956 switch (card->dev->device) {
2957 case PCIE_DEVICE_ID_MARVELL_88W8766P:
2958 strcpy(adapter->fw_name, PCIE8766_DEFAULT_FW_NAME);
2960 case PCIE_DEVICE_ID_MARVELL_88W8897:
2961 mwifiex_write_reg(adapter, 0x0c58, 0x80c00000);
2962 mwifiex_read_reg(adapter, 0x0c58, &revision_id);
2963 revision_id &= 0xff00;
2964 switch (revision_id) {
2966 strcpy(adapter->fw_name, PCIE8897_A0_FW_NAME);
2969 strcpy(adapter->fw_name, PCIE8897_B0_FW_NAME);
2972 strcpy(adapter->fw_name, PCIE8897_DEFAULT_FW_NAME);
2977 case PCIE_DEVICE_ID_MARVELL_88W8997:
2978 mwifiex_read_reg(adapter, 0x8, &revision_id);
2979 mwifiex_read_reg(adapter, 0x0cd0, &version);
2980 mwifiex_read_reg(adapter, 0x0cd4, &magic);
2981 revision_id &= 0xff;
2984 if (revision_id == PCIE8997_A1 &&
2985 magic == CHIP_MAGIC_VALUE &&
2986 version == CHIP_VER_PCIEUART)
2987 strcpy(adapter->fw_name, PCIEUART8997_FW_NAME_V4);
2989 strcpy(adapter->fw_name, PCIEUSB8997_FW_NAME_V4);
2997 * This function registers the PCIE device.
2999 * PCIE IRQ is claimed, block size is set and driver data is initialized.
3001 static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
3003 struct pcie_service_card *card = adapter->card;
3004 struct pci_dev *pdev = card->dev;
3006 /* save adapter pointer in card */
3007 card->adapter = adapter;
3008 adapter->dev = &pdev->dev;
3010 if (mwifiex_pcie_request_irq(adapter))
3013 adapter->tx_buf_size = card->pcie.tx_buf_size;
3014 adapter->mem_type_mapping_tbl = card->pcie.mem_type_mapping_tbl;
3015 adapter->num_mem_types = card->pcie.num_mem_types;
3016 adapter->ext_scan = card->pcie.can_ext_scan;
3017 mwifiex_pcie_get_fw_name(adapter);
3023 * This function unregisters the PCIE device.
3025 * The PCIE IRQ is released, the function is disabled and driver
3026 * data is set to null.
3028 static void mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
3030 struct pcie_service_card *card = adapter->card;
3031 struct pci_dev *pdev;
3036 if (card->msix_enable) {
3037 for (i = 0; i < MWIFIEX_NUM_MSIX_VECTORS; i++)
3038 synchronize_irq(card->msix_entries[i].vector);
3040 for (i = 0; i < MWIFIEX_NUM_MSIX_VECTORS; i++)
3041 free_irq(card->msix_entries[i].vector,
3042 &card->msix_ctx[i]);
3044 card->msix_enable = 0;
3045 pci_disable_msix(pdev);
3047 mwifiex_dbg(adapter, INFO,
3048 "%s(): calling free_irq()\n", __func__);
3049 free_irq(card->dev->irq, &card->share_irq_ctx);
3051 if (card->msi_enable)
3052 pci_disable_msi(pdev);
3057 /* This function initializes the PCI-E host memory space, WCB rings, etc.
3059 * The following initializations steps are followed -
3060 * - Allocate TXBD ring buffers
3061 * - Allocate RXBD ring buffers
3062 * - Allocate event BD ring buffers
3063 * - Allocate command response ring buffer
3064 * - Allocate sleep cookie buffer
3065 * Part of mwifiex_pcie_init(), not reset the PCIE registers
3067 static void mwifiex_pcie_up_dev(struct mwifiex_adapter *adapter)
3069 struct pcie_service_card *card = adapter->card;
3071 struct pci_dev *pdev = card->dev;
3072 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
3074 card->cmdrsp_buf = NULL;
3075 ret = mwifiex_pcie_create_txbd_ring(adapter);
3077 mwifiex_dbg(adapter, ERROR, "Failed to create txbd ring\n");
3081 ret = mwifiex_pcie_create_rxbd_ring(adapter);
3083 mwifiex_dbg(adapter, ERROR, "Failed to create rxbd ring\n");
3087 ret = mwifiex_pcie_create_evtbd_ring(adapter);
3089 mwifiex_dbg(adapter, ERROR, "Failed to create evtbd ring\n");
3093 ret = mwifiex_pcie_alloc_cmdrsp_buf(adapter);
3095 mwifiex_dbg(adapter, ERROR, "Failed to allocate cmdbuf buffer\n");
3096 goto err_alloc_cmdbuf;
3099 if (reg->sleep_cookie) {
3100 ret = mwifiex_pcie_alloc_sleep_cookie_buf(adapter);
3102 mwifiex_dbg(adapter, ERROR, "Failed to allocate sleep_cookie buffer\n");
3103 goto err_alloc_cookie;
3106 card->sleep_cookie_vbase = NULL;
3111 mwifiex_pcie_delete_cmdrsp_buf(adapter);
3113 mwifiex_pcie_delete_evtbd_ring(adapter);
3115 mwifiex_pcie_delete_rxbd_ring(adapter);
3117 mwifiex_pcie_delete_txbd_ring(adapter);
3119 pci_iounmap(pdev, card->pci_mmap1);
3122 /* This function cleans up the PCI-E host memory space. */
3123 static void mwifiex_pcie_down_dev(struct mwifiex_adapter *adapter)
3125 struct pcie_service_card *card = adapter->card;
3126 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
3128 if (mwifiex_write_reg(adapter, reg->drv_rdy, 0x00000000))
3129 mwifiex_dbg(adapter, ERROR, "Failed to write driver not-ready signature\n");
3131 adapter->seq_num = 0;
3132 adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_4K;
3135 mwifiex_pcie_free_buffers(adapter);
3140 static struct mwifiex_if_ops pcie_ops = {
3141 .init_if = mwifiex_pcie_init,
3142 .cleanup_if = mwifiex_pcie_cleanup,
3143 .check_fw_status = mwifiex_check_fw_status,
3144 .check_winner_status = mwifiex_check_winner_status,
3145 .prog_fw = mwifiex_prog_fw_w_helper,
3146 .register_dev = mwifiex_register_dev,
3147 .unregister_dev = mwifiex_unregister_dev,
3148 .enable_int = mwifiex_pcie_enable_host_int,
3149 .disable_int = mwifiex_pcie_disable_host_int_noerr,
3150 .process_int_status = mwifiex_process_int_status,
3151 .host_to_card = mwifiex_pcie_host_to_card,
3152 .wakeup = mwifiex_pm_wakeup_card,
3153 .wakeup_complete = mwifiex_pm_wakeup_card_complete,
3156 .cmdrsp_complete = mwifiex_pcie_cmdrsp_complete,
3157 .event_complete = mwifiex_pcie_event_complete,
3158 .update_mp_end_port = NULL,
3159 .cleanup_mpa_buf = NULL,
3160 .init_fw_port = mwifiex_pcie_init_fw_port,
3161 .clean_pcie_ring = mwifiex_clean_pcie_ring_buf,
3162 .reg_dump = mwifiex_pcie_reg_dump,
3163 .device_dump = mwifiex_pcie_device_dump,
3164 .down_dev = mwifiex_pcie_down_dev,
3165 .up_dev = mwifiex_pcie_up_dev,
3169 * This function initializes the PCIE driver module.
3171 * This initiates the semaphore and registers the device with
3174 static int mwifiex_pcie_init_module(void)
3178 pr_debug("Marvell PCIe Driver\n");
3180 sema_init(&add_remove_card_sem, 1);
3182 /* Clear the flag in case user removes the card. */
3185 ret = pci_register_driver(&mwifiex_pcie);
3187 pr_err("Driver register failed!\n");
3189 pr_debug("info: Driver registered successfully!\n");
3195 * This function cleans up the PCIE driver.
3197 * The following major steps are followed for cleanup -
3198 * - Resume the device if its suspended
3199 * - Disconnect the device if connected
3200 * - Shutdown the firmware
3201 * - Unregister the device from PCIE bus.
3203 static void mwifiex_pcie_cleanup_module(void)
3205 if (!down_interruptible(&add_remove_card_sem))
3206 up(&add_remove_card_sem);
3208 /* Set the flag as user is removing this module. */
3211 cancel_work_sync(&pcie_work);
3212 pci_unregister_driver(&mwifiex_pcie);
3215 module_init(mwifiex_pcie_init_module);
3216 module_exit(mwifiex_pcie_cleanup_module);
3218 MODULE_AUTHOR("Marvell International Ltd.");
3219 MODULE_DESCRIPTION("Marvell WiFi-Ex PCI-Express Driver version " PCIE_VERSION);
3220 MODULE_VERSION(PCIE_VERSION);
3221 MODULE_LICENSE("GPL v2");