1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2010-2016 Intel Corporation 3 */ 4 5 #include <stdint.h> 6 #include <string.h> 7 #include <stdio.h> 8 #include <errno.h> 9 #include <unistd.h> 10 11 #include <ethdev_driver.h> 12 #include <ethdev_pci.h> 13 #include <rte_pci.h> 14 #include <rte_bus_pci.h> 15 #include <rte_errno.h> 16 17 #include <rte_memory.h> 18 #include <rte_eal.h> 19 #include <rte_dev.h> 20 #include <rte_kvargs.h> 21 22 #include "virtio_ethdev.h" 23 #include "virtio_pci.h" 24 #include "virtio_logs.h" 25 26 /* 27 * The set of PCI devices this driver supports 28 */ 29 static const struct rte_pci_id pci_id_virtio_map[] = { 30 { RTE_PCI_DEVICE(VIRTIO_PCI_VENDORID, VIRTIO_PCI_LEGACY_DEVICEID_NET) }, 31 { RTE_PCI_DEVICE(VIRTIO_PCI_VENDORID, VIRTIO_PCI_MODERN_DEVICEID_NET) }, 32 { .vendor_id = 0, /* sentinel */ }, 33 }; 34 35 36 /* 37 * Remap the PCI device again (IO port map for legacy device and 38 * memory map for modern device), so that the secondary process 39 * could have the PCI initiated correctly. 40 */ 41 static int 42 virtio_remap_pci(struct rte_pci_device *pci_dev, struct virtio_pci_dev *dev) 43 { 44 struct virtio_hw *hw = &dev->hw; 45 46 if (dev->modern) { 47 /* 48 * We don't have to re-parse the PCI config space, since 49 * rte_pci_map_device() makes sure the mapped address 50 * in secondary process would equal to the one mapped in 51 * the primary process: error will be returned if that 52 * requirement is not met. 53 * 54 * That said, we could simply reuse all cap pointers 55 * (such as dev_cfg, common_cfg, etc.) parsed from the 56 * primary process, which is stored in shared memory. 57 */ 58 if (rte_pci_map_device(pci_dev)) { 59 PMD_INIT_LOG(DEBUG, "failed to map pci device!"); 60 return -1; 61 } 62 } else { 63 if (vtpci_legacy_ioport_map(hw) < 0) 64 return -1; 65 } 66 67 return 0; 68 } 69 70 static int 71 eth_virtio_pci_init(struct rte_eth_dev *eth_dev) 72 { 73 struct virtio_pci_dev *dev = eth_dev->data->dev_private; 74 struct virtio_hw *hw = &dev->hw; 75 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev); 76 int ret; 77 78 if (rte_eal_process_type() == RTE_PROC_PRIMARY) { 79 ret = vtpci_init(RTE_ETH_DEV_TO_PCI(eth_dev), dev); 80 if (ret) { 81 PMD_INIT_LOG(ERR, "Failed to init PCI device\n"); 82 return -1; 83 } 84 } else { 85 if (dev->modern) 86 VTPCI_OPS(hw) = &modern_ops; 87 else 88 VTPCI_OPS(hw) = &legacy_ops; 89 90 ret = virtio_remap_pci(RTE_ETH_DEV_TO_PCI(eth_dev), dev); 91 if (ret < 0) { 92 PMD_INIT_LOG(ERR, "Failed to remap PCI device\n"); 93 return -1; 94 } 95 } 96 97 ret = eth_virtio_dev_init(eth_dev); 98 if (ret < 0) { 99 PMD_INIT_LOG(ERR, "Failed to init virtio device\n"); 100 goto err_unmap; 101 } 102 103 PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x", 104 eth_dev->data->port_id, pci_dev->id.vendor_id, 105 pci_dev->id.device_id); 106 107 return 0; 108 109 err_unmap: 110 rte_pci_unmap_device(RTE_ETH_DEV_TO_PCI(eth_dev)); 111 if (!dev->modern) 112 vtpci_legacy_ioport_unmap(hw); 113 114 return ret; 115 } 116 117 static int 118 eth_virtio_pci_uninit(struct rte_eth_dev *eth_dev) 119 { 120 int ret; 121 PMD_INIT_FUNC_TRACE(); 122 123 if (rte_eal_process_type() == RTE_PROC_SECONDARY) 124 return 0; 125 126 ret = virtio_dev_stop(eth_dev); 127 virtio_dev_close(eth_dev); 128 129 PMD_INIT_LOG(DEBUG, "dev_uninit completed"); 130 131 return ret; 132 } 133 134 static int vdpa_check_handler(__rte_unused const char *key, 135 const char *value, void *ret_val) 136 { 137 if (strcmp(value, "1") == 0) 138 *(int *)ret_val = 1; 139 else 140 *(int *)ret_val = 0; 141 142 return 0; 143 } 144 145 #define VIRTIO_ARG_VDPA "vdpa" 146 147 static int 148 virtio_pci_devargs_parse(struct rte_devargs *devargs, int *vdpa) 149 { 150 struct rte_kvargs *kvlist; 151 int ret = 0; 152 153 if (devargs == NULL) 154 return 0; 155 156 kvlist = rte_kvargs_parse(devargs->args, NULL); 157 if (kvlist == NULL) { 158 PMD_INIT_LOG(ERR, "error when parsing param"); 159 return 0; 160 } 161 162 if (rte_kvargs_count(kvlist, VIRTIO_ARG_VDPA) == 1) { 163 /* vdpa mode selected when there's a key-value pair: 164 * vdpa=1 165 */ 166 ret = rte_kvargs_process(kvlist, VIRTIO_ARG_VDPA, 167 vdpa_check_handler, vdpa); 168 if (ret < 0) 169 PMD_INIT_LOG(ERR, "Failed to parse %s", VIRTIO_ARG_VDPA); 170 } 171 172 rte_kvargs_free(kvlist); 173 174 return ret; 175 } 176 177 static int eth_virtio_pci_probe(struct rte_pci_driver *pci_drv __rte_unused, 178 struct rte_pci_device *pci_dev) 179 { 180 int vdpa = 0; 181 int ret = 0; 182 183 ret = virtio_pci_devargs_parse(pci_dev->device.devargs, &vdpa); 184 if (ret < 0) { 185 PMD_INIT_LOG(ERR, "devargs parsing is failed"); 186 return ret; 187 } 188 /* virtio pmd skips probe if device needs to work in vdpa mode */ 189 if (vdpa == 1) 190 return 1; 191 192 return rte_eth_dev_pci_generic_probe(pci_dev, sizeof(struct virtio_pci_dev), 193 eth_virtio_pci_init); 194 } 195 196 static int eth_virtio_pci_remove(struct rte_pci_device *pci_dev) 197 { 198 int ret; 199 200 ret = rte_eth_dev_pci_generic_remove(pci_dev, eth_virtio_pci_uninit); 201 /* Port has already been released by close. */ 202 if (ret == -ENODEV) 203 ret = 0; 204 return ret; 205 } 206 207 static struct rte_pci_driver rte_virtio_net_pci_pmd = { 208 .driver = { 209 .name = "net_virtio", 210 }, 211 .id_table = pci_id_virtio_map, 212 .drv_flags = 0, 213 .probe = eth_virtio_pci_probe, 214 .remove = eth_virtio_pci_remove, 215 }; 216 217 RTE_INIT(rte_virtio_net_pci_pmd_init) 218 { 219 rte_eal_iopl_init(); 220 rte_pci_register(&rte_virtio_net_pci_pmd); 221 } 222 223 RTE_PMD_REGISTER_PCI_TABLE(net_virtio, pci_id_virtio_map); 224 RTE_PMD_REGISTER_KMOD_DEP(net_virtio, "* igb_uio | uio_pci_generic | vfio-pci"); 225 RTE_PMD_EXPORT_NAME(net_virtio, __COUNTER__); 226