1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation
11 #include <ethdev_driver.h>
12 #include <ethdev_pci.h>
14 #include <rte_bus_pci.h>
15 #include <rte_errno.h>
17 #include <rte_memory.h>
20 #include <rte_kvargs.h>
23 #include "virtio_ethdev.h"
24 #include "virtio_pci.h"
25 #include "virtio_logs.h"
28 * The set of PCI devices this driver supports
30 static const struct rte_pci_id pci_id_virtio_map[] = {
31 { RTE_PCI_DEVICE(VIRTIO_PCI_VENDORID, VIRTIO_PCI_LEGACY_DEVICEID_NET) },
32 { RTE_PCI_DEVICE(VIRTIO_PCI_VENDORID, VIRTIO_PCI_MODERN_DEVICEID_NET) },
33 { .vendor_id = 0, /* sentinel */ },
38 * Remap the PCI device again (IO port map for legacy device and
39 * memory map for modern device), so that the secondary process
40 * could have the PCI initiated correctly.
43 virtio_remap_pci(struct rte_pci_device *pci_dev, struct virtio_pci_dev *dev)
45 struct virtio_hw *hw = &dev->hw;
49 * We don't have to re-parse the PCI config space, since
50 * rte_pci_map_device() makes sure the mapped address
51 * in secondary process would equal to the one mapped in
52 * the primary process: error will be returned if that
53 * requirement is not met.
55 * That said, we could simply reuse all cap pointers
56 * (such as dev_cfg, common_cfg, etc.) parsed from the
57 * primary process, which is stored in shared memory.
59 if (rte_pci_map_device(pci_dev)) {
60 PMD_INIT_LOG(DEBUG, "failed to map pci device!");
64 if (vtpci_legacy_ioport_map(hw) < 0)
72 eth_virtio_pci_init(struct rte_eth_dev *eth_dev)
74 struct virtio_pci_dev *dev = eth_dev->data->dev_private;
75 struct virtio_hw *hw = &dev->hw;
76 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
79 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
80 ret = vtpci_init(RTE_ETH_DEV_TO_PCI(eth_dev), dev);
82 PMD_INIT_LOG(ERR, "Failed to init PCI device\n");
87 VIRTIO_OPS(hw) = &modern_ops;
89 VIRTIO_OPS(hw) = &legacy_ops;
91 ret = virtio_remap_pci(RTE_ETH_DEV_TO_PCI(eth_dev), dev);
93 PMD_INIT_LOG(ERR, "Failed to remap PCI device\n");
98 ret = eth_virtio_dev_init(eth_dev);
100 PMD_INIT_LOG(ERR, "Failed to init virtio device\n");
104 PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
105 eth_dev->data->port_id, pci_dev->id.vendor_id,
106 pci_dev->id.device_id);
111 rte_pci_unmap_device(RTE_ETH_DEV_TO_PCI(eth_dev));
113 vtpci_legacy_ioport_unmap(hw);
119 eth_virtio_pci_uninit(struct rte_eth_dev *eth_dev)
122 PMD_INIT_FUNC_TRACE();
124 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
127 ret = virtio_dev_stop(eth_dev);
128 virtio_dev_close(eth_dev);
130 PMD_INIT_LOG(DEBUG, "dev_uninit completed");
135 static int vdpa_check_handler(__rte_unused const char *key,
136 const char *value, void *ret_val)
138 if (strcmp(value, "1") == 0)
146 #define VIRTIO_ARG_VDPA "vdpa"
149 virtio_pci_devargs_parse(struct rte_devargs *devargs, int *vdpa)
151 struct rte_kvargs *kvlist;
157 kvlist = rte_kvargs_parse(devargs->args, NULL);
158 if (kvlist == NULL) {
159 PMD_INIT_LOG(ERR, "error when parsing param");
163 if (rte_kvargs_count(kvlist, VIRTIO_ARG_VDPA) == 1) {
164 /* vdpa mode selected when there's a key-value pair:
167 ret = rte_kvargs_process(kvlist, VIRTIO_ARG_VDPA,
168 vdpa_check_handler, vdpa);
170 PMD_INIT_LOG(ERR, "Failed to parse %s", VIRTIO_ARG_VDPA);
173 rte_kvargs_free(kvlist);
178 static int eth_virtio_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
179 struct rte_pci_device *pci_dev)
184 ret = virtio_pci_devargs_parse(pci_dev->device.devargs, &vdpa);
186 PMD_INIT_LOG(ERR, "devargs parsing is failed");
189 /* virtio pmd skips probe if device needs to work in vdpa mode */
193 return rte_eth_dev_pci_generic_probe(pci_dev, sizeof(struct virtio_pci_dev),
194 eth_virtio_pci_init);
197 static int eth_virtio_pci_remove(struct rte_pci_device *pci_dev)
201 ret = rte_eth_dev_pci_generic_remove(pci_dev, eth_virtio_pci_uninit);
202 /* Port has already been released by close. */
208 static struct rte_pci_driver rte_virtio_net_pci_pmd = {
210 .name = "net_virtio",
212 .id_table = pci_id_virtio_map,
214 .probe = eth_virtio_pci_probe,
215 .remove = eth_virtio_pci_remove,
218 RTE_INIT(rte_virtio_net_pci_pmd_init)
221 rte_pci_register(&rte_virtio_net_pci_pmd);
224 RTE_PMD_REGISTER_PCI_TABLE(net_virtio, pci_id_virtio_map);
225 RTE_PMD_REGISTER_KMOD_DEP(net_virtio, "* igb_uio | uio_pci_generic | vfio-pci");
226 RTE_PMD_EXPORT_NAME(net_virtio, __COUNTER__);