xref: /dpdk/drivers/net/virtio/virtio_pci_ethdev.c (revision 512e27eeb7435780cfe6915e17dbf840e0f4df20)
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_hw *hw)
43 {
44 	if (hw->bus_type == VIRTIO_BUS_PCI_MODERN) {
45 		/*
46 		 * We don't have to re-parse the PCI config space, since
47 		 * rte_pci_map_device() makes sure the mapped address
48 		 * in secondary process would equal to the one mapped in
49 		 * the primary process: error will be returned if that
50 		 * requirement is not met.
51 		 *
52 		 * That said, we could simply reuse all cap pointers
53 		 * (such as dev_cfg, common_cfg, etc.) parsed from the
54 		 * primary process, which is stored in shared memory.
55 		 */
56 		if (rte_pci_map_device(pci_dev)) {
57 			PMD_INIT_LOG(DEBUG, "failed to map pci device!");
58 			return -1;
59 		}
60 	} else if (hw->bus_type == VIRTIO_BUS_PCI_LEGACY) {
61 		if (rte_pci_ioport_map(pci_dev, 0, VTPCI_IO(hw)) < 0)
62 			return -1;
63 	}
64 
65 	return 0;
66 }
67 
68 static int
69 eth_virtio_pci_init(struct rte_eth_dev *eth_dev)
70 {
71 	struct virtio_pci_dev *dev = eth_dev->data->dev_private;
72 	struct virtio_hw *hw = &dev->hw;
73 	struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
74 	int ret;
75 
76 	if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
77 		ret = vtpci_init(RTE_ETH_DEV_TO_PCI(eth_dev), hw);
78 		if (ret) {
79 			PMD_INIT_LOG(ERR, "Failed to init PCI device\n");
80 			return -1;
81 		}
82 	} else {
83 		ret = virtio_remap_pci(RTE_ETH_DEV_TO_PCI(eth_dev), hw);
84 		if (ret < 0) {
85 			PMD_INIT_LOG(ERR, "Failed to remap PCI device\n");
86 			return -1;
87 		}
88 	}
89 
90 	ret = eth_virtio_dev_init(eth_dev);
91 	if (ret < 0) {
92 		PMD_INIT_LOG(ERR, "Failed to init virtio device\n");
93 		goto err_unmap;
94 	}
95 
96 	PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
97 		eth_dev->data->port_id, pci_dev->id.vendor_id,
98 		pci_dev->id.device_id);
99 
100 	return 0;
101 
102 err_unmap:
103 	rte_pci_unmap_device(RTE_ETH_DEV_TO_PCI(eth_dev));
104 	if (hw->bus_type == VIRTIO_BUS_PCI_LEGACY)
105 		rte_pci_ioport_unmap(VTPCI_IO(hw));
106 
107 	return ret;
108 }
109 
110 static int
111 eth_virtio_pci_uninit(struct rte_eth_dev *eth_dev)
112 {
113 	int ret;
114 	PMD_INIT_FUNC_TRACE();
115 
116 	if (rte_eal_process_type() == RTE_PROC_SECONDARY)
117 		return 0;
118 
119 	ret = virtio_dev_stop(eth_dev);
120 	virtio_dev_close(eth_dev);
121 
122 	PMD_INIT_LOG(DEBUG, "dev_uninit completed");
123 
124 	return ret;
125 }
126 
127 static int vdpa_check_handler(__rte_unused const char *key,
128 		const char *value, void *ret_val)
129 {
130 	if (strcmp(value, "1") == 0)
131 		*(int *)ret_val = 1;
132 	else
133 		*(int *)ret_val = 0;
134 
135 	return 0;
136 }
137 
138 #define VIRTIO_ARG_VDPA       "vdpa"
139 
140 static int
141 virtio_pci_devargs_parse(struct rte_devargs *devargs, int *vdpa)
142 {
143 	struct rte_kvargs *kvlist;
144 	int ret = 0;
145 
146 	if (devargs == NULL)
147 		return 0;
148 
149 	kvlist = rte_kvargs_parse(devargs->args, NULL);
150 	if (kvlist == NULL) {
151 		PMD_INIT_LOG(ERR, "error when parsing param");
152 		return 0;
153 	}
154 
155 	if (rte_kvargs_count(kvlist, VIRTIO_ARG_VDPA) == 1) {
156 		/* vdpa mode selected when there's a key-value pair:
157 		 * vdpa=1
158 		 */
159 		ret = rte_kvargs_process(kvlist, VIRTIO_ARG_VDPA,
160 				vdpa_check_handler, vdpa);
161 		if (ret < 0)
162 			PMD_INIT_LOG(ERR, "Failed to parse %s", VIRTIO_ARG_VDPA);
163 	}
164 
165 	rte_kvargs_free(kvlist);
166 
167 	return ret;
168 }
169 
170 static int eth_virtio_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
171 	struct rte_pci_device *pci_dev)
172 {
173 	int vdpa = 0;
174 	int ret = 0;
175 
176 	ret = virtio_pci_devargs_parse(pci_dev->device.devargs, &vdpa);
177 	if (ret < 0) {
178 		PMD_INIT_LOG(ERR, "devargs parsing is failed");
179 		return ret;
180 	}
181 	/* virtio pmd skips probe if device needs to work in vdpa mode */
182 	if (vdpa == 1)
183 		return 1;
184 
185 	return rte_eth_dev_pci_generic_probe(pci_dev, sizeof(struct virtio_pci_dev),
186 		eth_virtio_pci_init);
187 }
188 
189 static int eth_virtio_pci_remove(struct rte_pci_device *pci_dev)
190 {
191 	int ret;
192 
193 	ret = rte_eth_dev_pci_generic_remove(pci_dev, eth_virtio_pci_uninit);
194 	/* Port has already been released by close. */
195 	if (ret == -ENODEV)
196 		ret = 0;
197 	return ret;
198 }
199 
200 static struct rte_pci_driver rte_virtio_net_pci_pmd = {
201 	.driver = {
202 		.name = "net_virtio",
203 	},
204 	.id_table = pci_id_virtio_map,
205 	.drv_flags = 0,
206 	.probe = eth_virtio_pci_probe,
207 	.remove = eth_virtio_pci_remove,
208 };
209 
210 RTE_INIT(rte_virtio_net_pci_pmd_init)
211 {
212 	rte_eal_iopl_init();
213 	rte_pci_register(&rte_virtio_net_pci_pmd);
214 }
215 
216 RTE_PMD_REGISTER_PCI_TABLE(net_virtio, pci_id_virtio_map);
217 RTE_PMD_REGISTER_KMOD_DEP(net_virtio, "* igb_uio | uio_pci_generic | vfio-pci");
218 RTE_PMD_EXPORT_NAME(net_virtio, __COUNTER__);
219