Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2020-2021 Xilinx, Inc.
3 : : */
4 : :
5 : : #include <stdbool.h>
6 : : #include <stdint.h>
7 : : #include <pthread.h>
8 : : #include <sys/queue.h>
9 : :
10 : : #include <rte_common.h>
11 : : #include <rte_devargs.h>
12 : : #include <rte_errno.h>
13 : : #include <rte_kvargs.h>
14 : : #include <rte_string_fns.h>
15 : : #include <dev_vfio.h>
16 : : #include <rte_vhost.h>
17 : :
18 : : #include "efx.h"
19 : : #include "sfc_efx.h"
20 : : #include "sfc_vdpa.h"
21 : :
22 : : TAILQ_HEAD(sfc_vdpa_adapter_list_head, sfc_vdpa_adapter);
23 : : static struct sfc_vdpa_adapter_list_head sfc_vdpa_adapter_list =
24 : : TAILQ_HEAD_INITIALIZER(sfc_vdpa_adapter_list);
25 : :
26 : : static pthread_mutex_t sfc_vdpa_adapter_list_lock = PTHREAD_MUTEX_INITIALIZER;
27 : :
28 : : struct sfc_vdpa_adapter *
29 : 0 : sfc_vdpa_get_adapter_by_dev(struct rte_pci_device *pdev)
30 : : {
31 : : bool found = false;
32 : : struct sfc_vdpa_adapter *sva;
33 : :
34 : 0 : pthread_mutex_lock(&sfc_vdpa_adapter_list_lock);
35 : :
36 [ # # ]: 0 : TAILQ_FOREACH(sva, &sfc_vdpa_adapter_list, next) {
37 [ # # ]: 0 : if (pdev == sva->pdev) {
38 : : found = true;
39 : : break;
40 : : }
41 : : }
42 : :
43 : 0 : pthread_mutex_unlock(&sfc_vdpa_adapter_list_lock);
44 : :
45 [ # # ]: 0 : return found ? sva : NULL;
46 : : }
47 : :
48 : : struct sfc_vdpa_ops_data *
49 : 0 : sfc_vdpa_get_data_by_dev(struct rte_vdpa_device *vdpa_dev)
50 : : {
51 : : bool found = false;
52 : : struct sfc_vdpa_adapter *sva;
53 : :
54 : 0 : pthread_mutex_lock(&sfc_vdpa_adapter_list_lock);
55 : :
56 [ # # ]: 0 : TAILQ_FOREACH(sva, &sfc_vdpa_adapter_list, next) {
57 [ # # ]: 0 : if (vdpa_dev == sva->ops_data->vdpa_dev) {
58 : : found = true;
59 : : break;
60 : : }
61 : : }
62 : :
63 : 0 : pthread_mutex_unlock(&sfc_vdpa_adapter_list_lock);
64 : :
65 [ # # ]: 0 : return found ? sva->ops_data : NULL;
66 : : }
67 : :
68 : : static int
69 : 0 : sfc_vdpa_vfio_setup(struct sfc_vdpa_adapter *sva)
70 : : {
71 : 0 : struct rte_pci_device *dev = sva->pdev;
72 : 0 : char dev_name[RTE_DEV_NAME_MAX_LEN] = {0};
73 : : int rc;
74 : :
75 : 0 : rte_pci_device_name(&dev->addr, dev_name, RTE_DEV_NAME_MAX_LEN);
76 : :
77 : 0 : sva->vfio_container_fd = dev_vfio_container_create();
78 [ # # ]: 0 : if (sva->vfio_container_fd < 0) {
79 : 0 : sfc_vdpa_err(sva, "failed to create VFIO container");
80 : 0 : goto fail_container_create;
81 : : }
82 : :
83 : 0 : rc = dev_vfio_container_assign_device(sva->vfio_container_fd,
84 : : rte_pci_get_sysfs_path(), dev_name);
85 [ # # ]: 0 : if (rc < 0) {
86 : 0 : sfc_vdpa_err(sva, "failed to assign device %s to container %d",
87 : : dev_name, sva->vfio_container_fd);
88 : 0 : goto fail_device_assign;
89 : : }
90 : :
91 [ # # ]: 0 : if (rte_pci_map_device(dev) != 0) {
92 : 0 : sfc_vdpa_err(sva, "failed to map PCI device %s : %s",
93 : : dev_name, rte_strerror(rte_errno));
94 : 0 : goto fail_pci_map_device;
95 : : }
96 : :
97 : 0 : sva->vfio_dev_fd = rte_intr_dev_fd_get(dev->intr_handle);
98 : :
99 : 0 : return 0;
100 : :
101 : : fail_pci_map_device:
102 : 0 : fail_device_assign:
103 [ # # ]: 0 : if (dev_vfio_container_destroy(sva->vfio_container_fd) != 0) {
104 : 0 : sfc_vdpa_err(sva, "failed to destroy container %d",
105 : : sva->vfio_container_fd);
106 : : }
107 : :
108 : 0 : fail_container_create:
109 : : return -1;
110 : : }
111 : :
112 : : static void
113 : 0 : sfc_vdpa_vfio_teardown(struct sfc_vdpa_adapter *sva)
114 : : {
115 : 0 : rte_pci_unmap_device(sva->pdev);
116 : :
117 [ # # ]: 0 : if (dev_vfio_container_destroy(sva->vfio_container_fd) != 0) {
118 : 0 : sfc_vdpa_err(sva,
119 : : "failed to destroy container %d",
120 : : sva->vfio_container_fd);
121 : : }
122 : 0 : }
123 : :
124 : : static int
125 : 0 : sfc_vdpa_set_log_prefix(struct sfc_vdpa_adapter *sva)
126 : : {
127 : 0 : struct rte_pci_device *pci_dev = sva->pdev;
128 : : int ret;
129 : :
130 : 0 : ret = snprintf(sva->log_prefix, sizeof(sva->log_prefix),
131 : : "PMD: sfc_vdpa " PCI_PRI_FMT " : ",
132 : 0 : pci_dev->addr.domain, pci_dev->addr.bus,
133 [ # # ]: 0 : pci_dev->addr.devid, pci_dev->addr.function);
134 : :
135 [ # # ]: 0 : if (ret < 0 || ret >= (int)sizeof(sva->log_prefix)) {
136 : 0 : SFC_VDPA_GENERIC_LOG(ERR,
137 : : "reserved log prefix is too short for " PCI_PRI_FMT,
138 : : pci_dev->addr.domain, pci_dev->addr.bus,
139 : : pci_dev->addr.devid, pci_dev->addr.function);
140 : 0 : return -EINVAL;
141 : : }
142 : :
143 : : return 0;
144 : : }
145 : :
146 : : uint32_t
147 : 0 : sfc_vdpa_register_logtype(const struct rte_pci_addr *pci_addr,
148 : : const char *lt_prefix_str, uint32_t ll_default)
149 : : {
150 : 0 : size_t lt_prefix_str_size = strlen(lt_prefix_str);
151 : : size_t lt_str_size_max;
152 : : char *lt_str = NULL;
153 : : int ret;
154 : :
155 : : if (SIZE_MAX - PCI_PRI_STR_SIZE - 1 > lt_prefix_str_size) {
156 : 0 : ++lt_prefix_str_size; /* Reserve space for prefix separator */
157 : 0 : lt_str_size_max = lt_prefix_str_size + PCI_PRI_STR_SIZE + 1;
158 : : } else {
159 : : return sfc_vdpa_logtype_driver;
160 : : }
161 : :
162 : 0 : lt_str = rte_zmalloc("logtype_str", lt_str_size_max, 0);
163 [ # # ]: 0 : if (lt_str == NULL)
164 : 0 : return sfc_vdpa_logtype_driver;
165 : :
166 : : strncpy(lt_str, lt_prefix_str, lt_prefix_str_size);
167 : 0 : lt_str[lt_prefix_str_size - 1] = '.';
168 : 0 : rte_pci_device_name(pci_addr, lt_str + lt_prefix_str_size,
169 : : lt_str_size_max - lt_prefix_str_size);
170 : 0 : lt_str[lt_str_size_max - 1] = '\0';
171 : :
172 : 0 : ret = rte_log_register_type_and_pick_level(lt_str, ll_default);
173 : 0 : rte_free(lt_str);
174 : :
175 [ # # ]: 0 : return ret < 0 ? sfc_vdpa_logtype_driver : ret;
176 : : }
177 : :
178 : : static int
179 : 0 : sfc_vdpa_kvargs_parse(struct sfc_vdpa_adapter *sva)
180 : : {
181 : 0 : struct rte_pci_device *pci_dev = sva->pdev;
182 : 0 : struct rte_devargs *devargs = pci_dev->device.devargs;
183 : : /*
184 : : * To get the device class a mandatory param 'class' is being
185 : : * used so included SFC_EFX_KVARG_DEV_CLASS in the param list.
186 : : */
187 : 0 : const char **params = (const char *[]){
188 : : RTE_DEVARGS_KEY_CLASS,
189 : : SFC_VDPA_MAC_ADDR,
190 : : NULL,
191 : : };
192 : :
193 [ # # ]: 0 : if (devargs == NULL)
194 : : return 0;
195 : :
196 : 0 : sva->kvargs = rte_kvargs_parse(devargs->args, params);
197 [ # # ]: 0 : if (sva->kvargs == NULL)
198 : 0 : return -EINVAL;
199 : :
200 : : return 0;
201 : : }
202 : :
203 : : static struct rte_pci_id pci_id_sfc_vdpa_efx_map[] = {
204 : : { RTE_PCI_DEVICE(EFX_PCI_VENID_XILINX, EFX_PCI_DEVID_RIVERHEAD_VF) },
205 : : { .vendor_id = 0, /* sentinel */ },
206 : : };
207 : :
208 : : static int
209 : 0 : sfc_vdpa_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
210 : : struct rte_pci_device *pci_dev)
211 : : {
212 : : struct sfc_vdpa_adapter *sva = NULL;
213 : : uint32_t logtype_main;
214 : : int ret = 0;
215 : :
216 [ # # ]: 0 : if (sfc_efx_dev_class_get(pci_dev->device.devargs) !=
217 : : SFC_EFX_DEV_CLASS_VDPA) {
218 : 0 : SFC_VDPA_GENERIC_LOG(INFO,
219 : : "Incompatible device class: skip probing, should be probed by other sfc driver.");
220 : 0 : return 1;
221 : : }
222 : :
223 : : /*
224 : : * It will not be probed in the secondary process. As device class
225 : : * is vdpa so return 0 to avoid probe by other sfc driver
226 : : */
227 [ # # ]: 0 : if (rte_eal_process_type() != RTE_PROC_PRIMARY)
228 : : return 0;
229 : :
230 : 0 : logtype_main = sfc_vdpa_register_logtype(&pci_dev->addr,
231 : : SFC_VDPA_LOGTYPE_MAIN_STR,
232 : : RTE_LOG_NOTICE);
233 : :
234 : 0 : sva = rte_zmalloc("sfc_vdpa", sizeof(struct sfc_vdpa_adapter), 0);
235 [ # # ]: 0 : if (sva == NULL)
236 : 0 : goto fail_zmalloc;
237 : :
238 : 0 : sva->pdev = pci_dev;
239 : 0 : sva->logtype_main = logtype_main;
240 : :
241 : 0 : ret = sfc_vdpa_set_log_prefix(sva);
242 [ # # ]: 0 : if (ret != 0)
243 : 0 : goto fail_set_log_prefix;
244 : :
245 : 0 : ret = sfc_vdpa_kvargs_parse(sva);
246 [ # # ]: 0 : if (ret != 0)
247 : 0 : goto fail_kvargs_parse;
248 : :
249 : 0 : sfc_vdpa_log_init(sva, "entry");
250 : :
251 : : sfc_vdpa_adapter_lock_init(sva);
252 : :
253 : 0 : sfc_vdpa_log_init(sva, "vfio init");
254 [ # # ]: 0 : if (sfc_vdpa_vfio_setup(sva) < 0) {
255 : 0 : sfc_vdpa_err(sva, "failed to setup device %s", pci_dev->name);
256 : 0 : goto fail_vfio_setup;
257 : : }
258 : :
259 : 0 : sfc_vdpa_log_init(sva, "hw init");
260 [ # # ]: 0 : if (sfc_vdpa_hw_init(sva) != 0) {
261 : 0 : sfc_vdpa_err(sva, "failed to init HW %s", pci_dev->name);
262 : 0 : goto fail_hw_init;
263 : : }
264 : :
265 : 0 : sfc_vdpa_log_init(sva, "dev init");
266 : 0 : sva->ops_data = sfc_vdpa_device_init(sva, SFC_VDPA_AS_VF);
267 [ # # ]: 0 : if (sva->ops_data == NULL) {
268 : 0 : sfc_vdpa_err(sva, "failed vDPA dev init %s", pci_dev->name);
269 : 0 : goto fail_dev_init;
270 : : }
271 : :
272 : 0 : pthread_mutex_lock(&sfc_vdpa_adapter_list_lock);
273 : 0 : TAILQ_INSERT_TAIL(&sfc_vdpa_adapter_list, sva, next);
274 : 0 : pthread_mutex_unlock(&sfc_vdpa_adapter_list_lock);
275 : :
276 : 0 : sfc_vdpa_log_init(sva, "done");
277 : :
278 : 0 : return 0;
279 : :
280 : : fail_dev_init:
281 : 0 : sfc_vdpa_hw_fini(sva);
282 : :
283 : 0 : fail_hw_init:
284 : 0 : sfc_vdpa_vfio_teardown(sva);
285 : :
286 : 0 : fail_vfio_setup:
287 : : sfc_vdpa_adapter_lock_fini(sva);
288 : :
289 : 0 : fail_kvargs_parse:
290 : 0 : fail_set_log_prefix:
291 : 0 : rte_free(sva);
292 : :
293 : : fail_zmalloc:
294 : : return -1;
295 : : }
296 : :
297 : : static int
298 : 0 : sfc_vdpa_pci_remove(struct rte_pci_device *pci_dev)
299 : : {
300 : : struct sfc_vdpa_adapter *sva = NULL;
301 : :
302 [ # # ]: 0 : if (rte_eal_process_type() != RTE_PROC_PRIMARY)
303 : : return -1;
304 : :
305 : 0 : sva = sfc_vdpa_get_adapter_by_dev(pci_dev);
306 [ # # ]: 0 : if (sva == NULL) {
307 : 0 : SFC_VDPA_GENERIC_LOG(INFO,
308 : : "Invalid device: %s.", pci_dev->name);
309 : 0 : return -1;
310 : : }
311 : :
312 : 0 : pthread_mutex_lock(&sfc_vdpa_adapter_list_lock);
313 [ # # ]: 0 : TAILQ_REMOVE(&sfc_vdpa_adapter_list, sva, next);
314 : 0 : pthread_mutex_unlock(&sfc_vdpa_adapter_list_lock);
315 : :
316 : 0 : sfc_vdpa_device_fini(sva->ops_data);
317 : :
318 : 0 : sfc_vdpa_hw_fini(sva);
319 : :
320 : 0 : sfc_vdpa_vfio_teardown(sva);
321 : :
322 : : sfc_vdpa_adapter_lock_fini(sva);
323 : :
324 : 0 : rte_free(sva);
325 : :
326 : 0 : return 0;
327 : : }
328 : :
329 : : static struct rte_pci_driver rte_sfc_vdpa = {
330 : : .id_table = pci_id_sfc_vdpa_efx_map,
331 : : .drv_flags = 0,
332 : : .probe = sfc_vdpa_pci_probe,
333 : : .remove = sfc_vdpa_pci_remove,
334 : : };
335 : :
336 : 305 : RTE_PMD_REGISTER_PCI(net_sfc_vdpa, rte_sfc_vdpa);
337 : : RTE_PMD_REGISTER_PCI_TABLE(net_sfc_vdpa, pci_id_sfc_vdpa_efx_map);
338 : : RTE_PMD_REGISTER_KMOD_DEP(net_sfc_vdpa, "* vfio-pci");
339 [ - + ]: 305 : RTE_LOG_REGISTER_SUFFIX(sfc_vdpa_logtype_driver, driver, NOTICE);
|