|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
| 2 | +/* Copyright (C) 2020 Marvell. */ |
| 3 | + |
| 4 | +#include "otx2_cpt_common.h" |
| 5 | +#include "otx2_cptvf.h" |
| 6 | +#include <rvu_reg.h> |
| 7 | + |
| 8 | +#define OTX2_CPTVF_DRV_NAME "octeontx2-cptvf" |
| 9 | + |
| 10 | +static void cptvf_enable_pfvf_mbox_intrs(struct otx2_cptvf_dev *cptvf) |
| 11 | +{ |
| 12 | + /* Clear interrupt if any */ |
| 13 | + otx2_cpt_write64(cptvf->reg_base, BLKADDR_RVUM, 0, OTX2_RVU_VF_INT, |
| 14 | + 0x1ULL); |
| 15 | + |
| 16 | + /* Enable PF-VF interrupt */ |
| 17 | + otx2_cpt_write64(cptvf->reg_base, BLKADDR_RVUM, 0, |
| 18 | + OTX2_RVU_VF_INT_ENA_W1S, 0x1ULL); |
| 19 | +} |
| 20 | + |
| 21 | +static void cptvf_disable_pfvf_mbox_intrs(struct otx2_cptvf_dev *cptvf) |
| 22 | +{ |
| 23 | + /* Disable PF-VF interrupt */ |
| 24 | + otx2_cpt_write64(cptvf->reg_base, BLKADDR_RVUM, 0, |
| 25 | + OTX2_RVU_VF_INT_ENA_W1C, 0x1ULL); |
| 26 | + |
| 27 | + /* Clear interrupt if any */ |
| 28 | + otx2_cpt_write64(cptvf->reg_base, BLKADDR_RVUM, 0, OTX2_RVU_VF_INT, |
| 29 | + 0x1ULL); |
| 30 | +} |
| 31 | + |
| 32 | +static int cptvf_register_interrupts(struct otx2_cptvf_dev *cptvf) |
| 33 | +{ |
| 34 | + int ret, irq; |
| 35 | + u32 num_vec; |
| 36 | + |
| 37 | + num_vec = pci_msix_vec_count(cptvf->pdev); |
| 38 | + if (num_vec <= 0) |
| 39 | + return -EINVAL; |
| 40 | + |
| 41 | + /* Enable MSI-X */ |
| 42 | + ret = pci_alloc_irq_vectors(cptvf->pdev, num_vec, num_vec, |
| 43 | + PCI_IRQ_MSIX); |
| 44 | + if (ret < 0) { |
| 45 | + dev_err(&cptvf->pdev->dev, |
| 46 | + "Request for %d msix vectors failed\n", num_vec); |
| 47 | + return ret; |
| 48 | + } |
| 49 | + irq = pci_irq_vector(cptvf->pdev, OTX2_CPT_VF_INT_VEC_E_MBOX); |
| 50 | + /* Register VF<=>PF mailbox interrupt handler */ |
| 51 | + ret = devm_request_irq(&cptvf->pdev->dev, irq, |
| 52 | + otx2_cptvf_pfvf_mbox_intr, 0, |
| 53 | + "CPTPFVF Mbox", cptvf); |
| 54 | + if (ret) |
| 55 | + return ret; |
| 56 | + /* Enable PF-VF mailbox interrupts */ |
| 57 | + cptvf_enable_pfvf_mbox_intrs(cptvf); |
| 58 | + |
| 59 | + ret = otx2_cpt_send_ready_msg(&cptvf->pfvf_mbox, cptvf->pdev); |
| 60 | + if (ret) { |
| 61 | + dev_warn(&cptvf->pdev->dev, |
| 62 | + "PF not responding to mailbox, deferring probe\n"); |
| 63 | + cptvf_disable_pfvf_mbox_intrs(cptvf); |
| 64 | + return -EPROBE_DEFER; |
| 65 | + } |
| 66 | + return 0; |
| 67 | +} |
| 68 | + |
| 69 | +static int cptvf_pfvf_mbox_init(struct otx2_cptvf_dev *cptvf) |
| 70 | +{ |
| 71 | + int ret; |
| 72 | + |
| 73 | + cptvf->pfvf_mbox_wq = alloc_workqueue("cpt_pfvf_mailbox", |
| 74 | + WQ_UNBOUND | WQ_HIGHPRI | |
| 75 | + WQ_MEM_RECLAIM, 1); |
| 76 | + if (!cptvf->pfvf_mbox_wq) |
| 77 | + return -ENOMEM; |
| 78 | + |
| 79 | + ret = otx2_mbox_init(&cptvf->pfvf_mbox, cptvf->pfvf_mbox_base, |
| 80 | + cptvf->pdev, cptvf->reg_base, MBOX_DIR_VFPF, 1); |
| 81 | + if (ret) |
| 82 | + goto free_wqe; |
| 83 | + |
| 84 | + INIT_WORK(&cptvf->pfvf_mbox_work, otx2_cptvf_pfvf_mbox_handler); |
| 85 | + return 0; |
| 86 | + |
| 87 | +free_wqe: |
| 88 | + destroy_workqueue(cptvf->pfvf_mbox_wq); |
| 89 | + return ret; |
| 90 | +} |
| 91 | + |
| 92 | +static void cptvf_pfvf_mbox_destroy(struct otx2_cptvf_dev *cptvf) |
| 93 | +{ |
| 94 | + destroy_workqueue(cptvf->pfvf_mbox_wq); |
| 95 | + otx2_mbox_destroy(&cptvf->pfvf_mbox); |
| 96 | +} |
| 97 | + |
| 98 | +static int otx2_cptvf_probe(struct pci_dev *pdev, |
| 99 | + const struct pci_device_id *ent) |
| 100 | +{ |
| 101 | + struct device *dev = &pdev->dev; |
| 102 | + resource_size_t offset, size; |
| 103 | + struct otx2_cptvf_dev *cptvf; |
| 104 | + int ret; |
| 105 | + |
| 106 | + cptvf = devm_kzalloc(dev, sizeof(*cptvf), GFP_KERNEL); |
| 107 | + if (!cptvf) |
| 108 | + return -ENOMEM; |
| 109 | + |
| 110 | + ret = pcim_enable_device(pdev); |
| 111 | + if (ret) { |
| 112 | + dev_err(dev, "Failed to enable PCI device\n"); |
| 113 | + goto clear_drvdata; |
| 114 | + } |
| 115 | + |
| 116 | + ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48)); |
| 117 | + if (ret) { |
| 118 | + dev_err(dev, "Unable to get usable DMA configuration\n"); |
| 119 | + goto clear_drvdata; |
| 120 | + } |
| 121 | + /* Map VF's configuration registers */ |
| 122 | + ret = pcim_iomap_regions_request_all(pdev, 1 << PCI_PF_REG_BAR_NUM, |
| 123 | + OTX2_CPTVF_DRV_NAME); |
| 124 | + if (ret) { |
| 125 | + dev_err(dev, "Couldn't get PCI resources 0x%x\n", ret); |
| 126 | + goto clear_drvdata; |
| 127 | + } |
| 128 | + pci_set_master(pdev); |
| 129 | + pci_set_drvdata(pdev, cptvf); |
| 130 | + cptvf->pdev = pdev; |
| 131 | + |
| 132 | + cptvf->reg_base = pcim_iomap_table(pdev)[PCI_PF_REG_BAR_NUM]; |
| 133 | + |
| 134 | + offset = pci_resource_start(pdev, PCI_MBOX_BAR_NUM); |
| 135 | + size = pci_resource_len(pdev, PCI_MBOX_BAR_NUM); |
| 136 | + /* Map PF-VF mailbox memory */ |
| 137 | + cptvf->pfvf_mbox_base = devm_ioremap_wc(dev, offset, size); |
| 138 | + if (!cptvf->pfvf_mbox_base) { |
| 139 | + dev_err(&pdev->dev, "Unable to map BAR4\n"); |
| 140 | + ret = -ENODEV; |
| 141 | + goto clear_drvdata; |
| 142 | + } |
| 143 | + /* Initialize PF<=>VF mailbox */ |
| 144 | + ret = cptvf_pfvf_mbox_init(cptvf); |
| 145 | + if (ret) |
| 146 | + goto clear_drvdata; |
| 147 | + |
| 148 | + /* Register interrupts */ |
| 149 | + ret = cptvf_register_interrupts(cptvf); |
| 150 | + if (ret) |
| 151 | + goto destroy_pfvf_mbox; |
| 152 | + |
| 153 | + return 0; |
| 154 | + |
| 155 | +destroy_pfvf_mbox: |
| 156 | + cptvf_pfvf_mbox_destroy(cptvf); |
| 157 | +clear_drvdata: |
| 158 | + pci_set_drvdata(pdev, NULL); |
| 159 | + |
| 160 | + return ret; |
| 161 | +} |
| 162 | + |
| 163 | +static void otx2_cptvf_remove(struct pci_dev *pdev) |
| 164 | +{ |
| 165 | + struct otx2_cptvf_dev *cptvf = pci_get_drvdata(pdev); |
| 166 | + |
| 167 | + if (!cptvf) { |
| 168 | + dev_err(&pdev->dev, "Invalid CPT VF device.\n"); |
| 169 | + return; |
| 170 | + } |
| 171 | + /* Disable PF-VF mailbox interrupt */ |
| 172 | + cptvf_disable_pfvf_mbox_intrs(cptvf); |
| 173 | + /* Destroy PF-VF mbox */ |
| 174 | + cptvf_pfvf_mbox_destroy(cptvf); |
| 175 | + pci_set_drvdata(pdev, NULL); |
| 176 | +} |
| 177 | + |
| 178 | +/* Supported devices */ |
| 179 | +static const struct pci_device_id otx2_cptvf_id_table[] = { |
| 180 | + {PCI_VDEVICE(CAVIUM, OTX2_CPT_PCI_VF_DEVICE_ID), 0}, |
| 181 | + { 0, } /* end of table */ |
| 182 | +}; |
| 183 | + |
| 184 | +static struct pci_driver otx2_cptvf_pci_driver = { |
| 185 | + .name = OTX2_CPTVF_DRV_NAME, |
| 186 | + .id_table = otx2_cptvf_id_table, |
| 187 | + .probe = otx2_cptvf_probe, |
| 188 | + .remove = otx2_cptvf_remove, |
| 189 | +}; |
| 190 | + |
| 191 | +module_pci_driver(otx2_cptvf_pci_driver); |
| 192 | + |
| 193 | +MODULE_AUTHOR("Marvell"); |
| 194 | +MODULE_DESCRIPTION("Marvell OcteonTX2 CPT Virtual Function Driver"); |
| 195 | +MODULE_LICENSE("GPL v2"); |
| 196 | +MODULE_DEVICE_TABLE(pci, otx2_cptvf_id_table); |
0 commit comments