blob: 0435d569becd7706d7c60dc3b2e254926a0680ba [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
Damjan Marion5a206ea2016-05-12 22:11:03 +02002 * Copyright (c) 2016 Cisco and/or its affiliates.
Ed Warnickecb9cada2015-12-08 15:45:58 -07003 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15/*
16 * pci.c: Linux user space PCI bus management.
17 *
18 * Copyright (c) 2008 Eliot Dresselhaus
19 *
20 * Permission is hereby granted, free of charge, to any person obtaining
21 * a copy of this software and associated documentation files (the
22 * "Software"), to deal in the Software without restriction, including
23 * without limitation the rights to use, copy, modify, merge, publish,
24 * distribute, sublicense, and/or sell copies of the Software, and to
25 * permit persons to whom the Software is furnished to do so, subject to
26 * the following conditions:
27 *
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
30 *
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 */
39
Damjan Marion01914ce2017-09-14 19:04:50 +020040#include <vppinfra/linux/sysfs.h>
41
Ed Warnickecb9cada2015-12-08 15:45:58 -070042#include <vlib/vlib.h>
43#include <vlib/pci/pci.h>
44#include <vlib/unix/unix.h>
Damjan Marion1ba0fa42018-03-04 17:19:08 +010045#include <vlib/linux/vfio.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070046
47#include <sys/types.h>
48#include <sys/stat.h>
49#include <fcntl.h>
50#include <dirent.h>
Damjan Mariona42cd342016-04-13 18:03:20 +020051#include <sys/ioctl.h>
52#include <net/if.h>
53#include <linux/ethtool.h>
54#include <linux/sockios.h>
Damjan Marioncef87f12017-10-05 15:32:41 +020055#include <linux/vfio.h>
56#include <sys/eventfd.h>
57
Ray Kinsella5714a492021-11-02 13:33:44 +000058#define SYSFS_DEVICES_PCI "/sys/devices/pci"
Damjan Marioncef87f12017-10-05 15:32:41 +020059static const char *sysfs_pci_dev_path = "/sys/bus/pci/devices";
60static const char *sysfs_pci_drv_path = "/sys/bus/pci/drivers";
Damjan Marion2752b892017-12-11 15:55:56 +010061static char *sysfs_mod_vfio_noiommu =
62 "/sys/module/vfio/parameters/enable_unsafe_noiommu_mode";
Damjan Mariona42cd342016-04-13 18:03:20 +020063
Damjan Marion00ea98a2023-07-28 13:19:49 +020064VLIB_REGISTER_LOG_CLASS (pci_log, static) = {
65 .class_name = "pci",
66 .subclass_name = "linux",
67};
68
69#define log_debug(p, f, ...) \
70 vlib_log (VLIB_LOG_LEVEL_DEBUG, pci_log.class, "%U: " f, \
71 format_vlib_pci_log, p->handle, ##__VA_ARGS__)
72#define log_err(p, f, ...) \
73 vlib_log (VLIB_LOG_LEVEL_ERR, pci_log.class, "%U: " f, format_vlib_pci_log, \
74 p->handle, ##__VA_ARGS__)
Damjan Marion23227982018-10-22 13:38:57 +020075
Dave Barach9b8ffd92016-07-08 08:13:45 -040076typedef struct
77{
Damjan Marion2060db82018-03-04 17:37:15 +010078 int fd;
79 void *addr;
80 size_t size;
81} linux_pci_region_t;
82
Damjan Marion599a16b2018-03-04 19:35:23 +010083typedef struct
84{
85 int fd;
86 u32 clib_file_index;
87 union
88 {
Damjan Mariond5ded2d2018-03-05 10:18:50 +010089 pci_intx_handler_function_t *intx_handler;
Damjan Marion599a16b2018-03-04 19:35:23 +010090 pci_msix_handler_function_t *msix_handler;
91 };
92} linux_pci_irq_t;
Damjan Marion2060db82018-03-04 17:37:15 +010093
94typedef enum
95{
96 LINUX_PCI_DEVICE_TYPE_UNKNOWN,
97 LINUX_PCI_DEVICE_TYPE_UIO,
98 LINUX_PCI_DEVICE_TYPE_VFIO,
99} linux_pci_device_type_t;
100
101typedef struct
102{
103 linux_pci_device_type_t type;
Damjan Marioncef87f12017-10-05 15:32:41 +0200104 vlib_pci_dev_handle_t handle;
105 vlib_pci_addr_t addr;
Damjan Mariond2bfb782019-01-07 20:56:04 +0100106 u32 numa_node;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200107
108 /* Resource file descriptors. */
Damjan Marion2060db82018-03-04 17:37:15 +0100109 linux_pci_region_t *regions;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200110
111 /* File descriptor for config space read/write. */
112 int config_fd;
Damjan Marionf9a968e2018-02-25 23:26:22 +0100113 u64 config_offset;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200114
Damjan Marionf9a968e2018-02-25 23:26:22 +0100115 /* Device File descriptor */
116 int fd;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200117
Mohsin Kazmi95276ca2018-09-28 11:45:01 +0200118 /* read/write file descriptor for io bar */
119 int io_fd;
120 u64 io_offset;
121
Damjan Marion5a206ea2016-05-12 22:11:03 +0200122 /* Minor device for uio device. */
123 u32 uio_minor;
124
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100125 /* Interrupt handlers */
126 linux_pci_irq_t intx_irq;
Damjan Marion599a16b2018-03-04 19:35:23 +0100127 linux_pci_irq_t *msix_irqs;
Damjan Marioncef87f12017-10-05 15:32:41 +0200128
129 /* private data */
130 uword private_data;
131
Damjan Marion23227982018-10-22 13:38:57 +0200132 u8 supports_va_dma;
133
Damjan Marion5a206ea2016-05-12 22:11:03 +0200134} linux_pci_device_t;
135
136/* Pool of PCI devices. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400137typedef struct
138{
139 vlib_main_t *vlib_main;
140 linux_pci_device_t *linux_pci_devices;
Damjan Marioncef87f12017-10-05 15:32:41 +0200141
Damjan Marion5a206ea2016-05-12 22:11:03 +0200142} linux_pci_main_t;
143
144extern linux_pci_main_t linux_pci_main;
145
Damjan Marion599a16b2018-03-04 19:35:23 +0100146static linux_pci_device_t *
Damjan Marioncef87f12017-10-05 15:32:41 +0200147linux_pci_get_device (vlib_pci_dev_handle_t h)
148{
149 linux_pci_main_t *lpm = &linux_pci_main;
150 return pool_elt_at_index (lpm->linux_pci_devices, h);
151}
152
153uword
Damjan Marion23227982018-10-22 13:38:57 +0200154vlib_pci_get_private_data (vlib_main_t * vm, vlib_pci_dev_handle_t h)
Damjan Marioncef87f12017-10-05 15:32:41 +0200155{
156 linux_pci_device_t *d = linux_pci_get_device (h);
157 return d->private_data;
158}
159
160void
Damjan Marion23227982018-10-22 13:38:57 +0200161vlib_pci_set_private_data (vlib_main_t * vm, vlib_pci_dev_handle_t h,
162 uword private_data)
Damjan Marioncef87f12017-10-05 15:32:41 +0200163{
164 linux_pci_device_t *d = linux_pci_get_device (h);
165 d->private_data = private_data;
166}
167
168vlib_pci_addr_t *
Damjan Marion23227982018-10-22 13:38:57 +0200169vlib_pci_get_addr (vlib_main_t * vm, vlib_pci_dev_handle_t h)
Damjan Marioncef87f12017-10-05 15:32:41 +0200170{
Damjan Marion599a16b2018-03-04 19:35:23 +0100171 linux_pci_device_t *d = linux_pci_get_device (h);
172 return &d->addr;
Damjan Marioncef87f12017-10-05 15:32:41 +0200173}
174
Damjan Mariond2bfb782019-01-07 20:56:04 +0100175u32
176vlib_pci_get_numa_node (vlib_main_t * vm, vlib_pci_dev_handle_t h)
177{
178 linux_pci_device_t *d = linux_pci_get_device (h);
179 return d->numa_node;
180}
181
Mohsin Kazmia402b832019-01-25 15:57:24 +0000182u32
183vlib_pci_get_num_msix_interrupts (vlib_main_t * vm, vlib_pci_dev_handle_t h)
184{
185 linux_pci_device_t *d = linux_pci_get_device (h);
186
187 if (d->type == LINUX_PCI_DEVICE_TYPE_VFIO)
188 {
189 struct vfio_irq_info ii = { 0 };
190
191 ii.argsz = sizeof (struct vfio_irq_info);
192 ii.index = VFIO_PCI_MSIX_IRQ_INDEX;
193 if (ioctl (d->fd, VFIO_DEVICE_GET_IRQ_INFO, &ii) < 0)
194 return 0;
195 return ii.count;
196 }
197 return 0;
198}
199
Damjan Marion5a206ea2016-05-12 22:11:03 +0200200/* Call to allocate/initialize the pci subsystem.
201 This is not an init function so that users can explicitly enable
202 pci only when it's needed. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400203clib_error_t *pci_bus_init (vlib_main_t * vm);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200204
Ed Warnickecb9cada2015-12-08 15:45:58 -0700205linux_pci_main_t linux_pci_main;
206
Damjan Marioncef87f12017-10-05 15:32:41 +0200207vlib_pci_device_info_t *
Damjan Marion23227982018-10-22 13:38:57 +0200208vlib_pci_get_device_info (vlib_main_t * vm, vlib_pci_addr_t * addr,
209 clib_error_t ** error)
Damjan Marioncef87f12017-10-05 15:32:41 +0200210{
Damjan Marioncef87f12017-10-05 15:32:41 +0200211 clib_error_t *err;
212 vlib_pci_device_info_t *di;
213 u8 *f = 0;
214 u32 tmp;
215 int fd;
Yulong Pei45495482019-10-17 18:41:52 +0800216 u8 *tmpstr;
Jieqiang Wang76c61592020-01-13 17:15:13 +0800217 clib_bitmap_t *bmp = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200218
219 di = clib_mem_alloc (sizeof (vlib_pci_device_info_t));
Dave Barachb7b92992018-10-17 10:38:51 -0400220 clib_memset (di, 0, sizeof (vlib_pci_device_info_t));
Damjan Marioncef87f12017-10-05 15:32:41 +0200221 di->addr.as_u32 = addr->as_u32;
222
223 u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
224 format_vlib_pci_addr, addr);
225
226 f = format (0, "%v/config%c", dev_dir_name, 0);
227 fd = open ((char *) f, O_RDWR);
228
229 /* Try read-only access if write fails. */
230 if (fd < 0)
231 fd = open ((char *) f, O_RDONLY);
232
233 if (fd < 0)
234 {
235 err = clib_error_return_unix (0, "open `%s'", f);
236 goto error;
237 }
238
239 /* You can only read more that 64 bytes of config space as root; so we try to
240 read the full space but fall back to just the first 64 bytes. */
Damjan Marionc9275da2023-10-12 17:41:14 +0000241 if (read (fd, &di->config, sizeof (di->config)) <
242 sizeof (vlib_pci_config_hdr_t))
Damjan Marioncef87f12017-10-05 15:32:41 +0200243 {
244 err = clib_error_return_unix (0, "read `%s'", f);
245 close (fd);
246 goto error;
247 }
248
Damjan Marioncef87f12017-10-05 15:32:41 +0200249 di->numa_node = -1;
250 vec_reset_length (f);
251 f = format (f, "%v/numa_node%c", dev_dir_name, 0);
Jieqiang Wang76c61592020-01-13 17:15:13 +0800252 err = clib_sysfs_read ((char *) f, "%d", &di->numa_node);
Damjan Marioncef87f12017-10-05 15:32:41 +0200253 if (err)
254 {
255 di->numa_node = -1;
256 clib_error_free (err);
257 }
Jieqiang Wang76c61592020-01-13 17:15:13 +0800258 if (di->numa_node == -1)
259 {
260 /* if '/sys/bus/pci/devices/<device id>/numa_node' returns -1 and
261 it is a SMP system, set numa_node to 0. */
262 if ((err = clib_sysfs_read ("/sys/devices/system/node/online", "%U",
263 unformat_bitmap_list, &bmp)))
264 clib_error_free (err);
265 if (clib_bitmap_count_set_bits (bmp) == 1)
266 di->numa_node = 0;
267 }
Damjan Marioncef87f12017-10-05 15:32:41 +0200268
269 vec_reset_length (f);
270 f = format (f, "%v/class%c", dev_dir_name, 0);
271 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
272 if (err)
273 goto error;
274 di->device_class = tmp >> 8;
275
276 vec_reset_length (f);
277 f = format (f, "%v/vendor%c", dev_dir_name, 0);
278 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
279 if (err)
280 goto error;
281 di->vendor_id = tmp;
282
283 vec_reset_length (f);
284 f = format (f, "%v/device%c", dev_dir_name, 0);
285 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
286 if (err)
287 goto error;
288 di->device_id = tmp;
289
290 vec_reset_length (f);
Damjan Marion00ea98a2023-07-28 13:19:49 +0200291 f = format (f, "%v/revision%c", dev_dir_name, 0);
292 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
293 if (err)
294 goto error;
295 di->revision = tmp;
296
Damjan Marion40f48102023-08-07 01:07:09 +0200297 di->driver_name =
298 clib_file_get_resolved_basename ("%v/driver", dev_dir_name);
Benoît Ganne0eae2bb2019-10-04 17:30:21 +0200299 if (!di->driver_name)
300 di->driver_name = format (0, "<NONE>%c", 0);
Damjan Marioncef87f12017-10-05 15:32:41 +0200301
302 di->iommu_group = -1;
Damjan Marion40f48102023-08-07 01:07:09 +0200303 tmpstr = clib_file_get_resolved_basename ("%v/iommu_group", dev_dir_name);
Yulong Pei45495482019-10-17 18:41:52 +0800304 if (tmpstr)
Damjan Marioncef87f12017-10-05 15:32:41 +0200305 {
Yulong Pei45495482019-10-17 18:41:52 +0800306 di->iommu_group = atoi ((char *) tmpstr);
307 vec_free (tmpstr);
Damjan Marioncef87f12017-10-05 15:32:41 +0200308 }
309
Yulong Pei45495482019-10-17 18:41:52 +0800310 vec_reset_length (f);
311 f = format (f, "%v/iommu_group/name%c", dev_dir_name, 0);
312 err = clib_sysfs_read ((char *) f, "%s", &tmpstr);
313 if (err == 0)
314 {
315 if (strncmp ((char *) tmpstr, "vfio-noiommu", 12) == 0)
316 di->flags |= VLIB_PCI_DEVICE_INFO_F_NOIOMMU;
317 vec_free (tmpstr);
318 }
319 else
320 clib_error_free (err);
321
Damjan Marion96504182017-12-10 23:54:46 +0100322 close (fd);
323
Damjan Marioncef87f12017-10-05 15:32:41 +0200324 vec_reset_length (f);
325 f = format (f, "%v/vpd%c", dev_dir_name, 0);
326 fd = open ((char *) f, O_RDONLY);
327 if (fd >= 0)
328 {
329 while (1)
330 {
331 u8 tag[3];
332 u8 *data = 0;
Damjan Marion96504182017-12-10 23:54:46 +0100333 uword len;
Damjan Marioncef87f12017-10-05 15:32:41 +0200334
335 if (read (fd, &tag, 3) != 3)
336 break;
337
338 if (tag[0] != 0x82 && tag[0] != 0x90 && tag[0] != 0x91)
339 break;
340
341 len = (tag[2] << 8) | tag[1];
Ray Kinsella03d0fbe2021-11-03 09:13:41 +0000342 vec_validate (data, len - 1);
Damjan Marioncef87f12017-10-05 15:32:41 +0200343
344 if (read (fd, data, len) != len)
345 {
346 vec_free (data);
347 break;
348 }
349 if (tag[0] == 0x82)
350 di->product_name = data;
351 else if (tag[0] == 0x90)
352 di->vpd_r = data;
353 else if (tag[0] == 0x91)
354 di->vpd_w = data;
355
356 data = 0;
357 }
358 close (fd);
359 }
360
361 goto done;
362
363error:
364 vlib_pci_free_device_info (di);
365 di = 0;
366
367done:
Jieqiang Wang76c61592020-01-13 17:15:13 +0800368 vec_free (bmp);
Damjan Marioncef87f12017-10-05 15:32:41 +0200369 vec_free (f);
370 vec_free (dev_dir_name);
371 if (error)
372 *error = err;
373 else
374 clib_error_free (err);
375 return di;
376}
377
Ray Kinsella5714a492021-11-02 13:33:44 +0000378clib_error_t *__attribute__ ((weak))
379vlib_pci_get_device_root_bus (vlib_pci_addr_t *addr, vlib_pci_addr_t *root_bus)
380{
381 u8 *rel_path = 0, *abs_path = 0, *link_path = 0;
382 unformat_input_t input;
Ray Kinsella81865bc2021-11-08 07:22:49 +0000383 int fd = open (sysfs_pci_dev_path, O_RDONLY);
Klement Sekerabd469072021-11-18 12:32:05 +0100384 ssize_t size = 0;
Ray Kinsella5714a492021-11-02 13:33:44 +0000385 u32 domain = 0, bus;
386 clib_error_t *err = NULL;
387
388 if (fd < 0)
389 return clib_error_return_unix (0, "failed to open %s", sysfs_pci_dev_path);
390
391 vec_alloc (rel_path, PATH_MAX);
392 vec_alloc (abs_path, PATH_MAX);
393
394 link_path =
395 format (0, "%s/%U", sysfs_pci_dev_path, format_vlib_pci_addr, addr);
396 size = readlinkat (fd, (char *) link_path, (char *) rel_path, PATH_MAX);
397 if (size < 0)
398 {
399 err = clib_error_return_unix (0, "failed to read %s", rel_path);
400 goto done;
401 }
402
403 rel_path[size] = '\0';
404 vec_free (link_path);
405
406 link_path = format (0, "%s/%s", sysfs_pci_dev_path, rel_path);
407 if (!realpath ((char *) link_path, (char *) abs_path))
408 {
409 err = clib_error_return_unix (0, "failed to resolve %s", link_path);
410 goto done;
411 }
412
413 unformat_init_string (&input, (char *) abs_path,
414 clib_strnlen ((char *) abs_path, PATH_MAX));
415
416 if (!unformat (&input, SYSFS_DEVICES_PCI "%x:%x/%s", &domain, &bus,
417 link_path))
418 {
419 err = clib_error_return (0, "unknown input '%U'", format_unformat_error,
420 input);
421 goto done;
422 }
423
424 root_bus->domain = domain;
425 root_bus->bus = bus;
426
427done:
428 vec_free (abs_path);
429 vec_free (link_path);
430 vec_free (rel_path);
431 close (fd);
432
433 return err;
434}
435
Damjan Marion2752b892017-12-11 15:55:56 +0100436static int
437directory_exists (char *path)
438{
439 struct stat s = { 0 };
440 if (stat (path, &s) == -1)
441 return 0;
442
443 return S_ISDIR (s.st_mode);
444}
445
Damjan Mariona42cd342016-04-13 18:03:20 +0200446clib_error_t *
Benoît Ganne6a073482022-10-13 17:22:26 +0200447vlib_pci_bind_to_uio (vlib_main_t *vm, vlib_pci_addr_t *addr,
448 char *uio_drv_name, int force)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700449{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400450 clib_error_t *error = 0;
Damjan Mariona7f74572017-05-23 18:32:21 +0200451 u8 *s = 0, *driver_name = 0;
Damjan Mariona42cd342016-04-13 18:03:20 +0200452 DIR *dir = 0;
453 struct dirent *e;
Damjan Marioncef87f12017-10-05 15:32:41 +0200454 vlib_pci_device_info_t *di;
Damjan Mariona7f74572017-05-23 18:32:21 +0200455 int fd, clear_driver_override = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200456 u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
457 format_vlib_pci_addr, addr);
458
Damjan Marion23227982018-10-22 13:38:57 +0200459 di = vlib_pci_get_device_info (vm, addr, &error);
Damjan Marioncef87f12017-10-05 15:32:41 +0200460
461 if (error)
462 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700463
Damjan Marion2752b892017-12-11 15:55:56 +0100464 if (strncmp ("auto", uio_drv_name, 5) == 0)
465 {
466 int vfio_pci_loaded = 0;
467
468 if (directory_exists ("/sys/module/vfio_pci"))
469 vfio_pci_loaded = 1;
470
471 if (di->iommu_group != -1)
472 {
473 /* device is bound to IOMMU group */
474 if (!vfio_pci_loaded)
475 {
476 error = clib_error_return (0, "Skipping PCI device %U: device "
477 "is bound to IOMMU group and "
478 "vfio-pci driver is not loaded",
479 format_vlib_pci_addr, addr);
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100480 goto err0;
Damjan Marion2752b892017-12-11 15:55:56 +0100481 }
482 else
483 uio_drv_name = "vfio-pci";
484 }
485 else
486 {
487 /* device is not bound to IOMMU group so we have multiple options */
488 if (vfio_pci_loaded &&
489 (error = clib_sysfs_write (sysfs_mod_vfio_noiommu, "Y")) == 0)
490 uio_drv_name = "vfio-pci";
491 else if (directory_exists ("/sys/module/uio_pci_generic"))
492 uio_drv_name = "uio_pci_generic";
493 else if (directory_exists ("/sys/module/igb_uio"))
494 uio_drv_name = "igb_uio";
495 else
496 {
497 clib_error_free (error);
498 error = clib_error_return (0, "Skipping PCI device %U: missing "
499 "kernel VFIO or UIO driver",
500 format_vlib_pci_addr, addr);
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100501 goto err0;
Damjan Marion2752b892017-12-11 15:55:56 +0100502 }
503 clib_error_free (error);
504 }
505 }
506
Damjan Marion40f48102023-08-07 01:07:09 +0200507 driver_name = clib_file_get_resolved_basename ("%v/driver", dev_dir_name);
Damjan Mariona42cd342016-04-13 18:03:20 +0200508
Damjan Mariona7f74572017-05-23 18:32:21 +0200509 if (driver_name &&
510 ((strcmp ("vfio-pci", (char *) driver_name) == 0) ||
511 (strcmp ("uio_pci_generic", (char *) driver_name) == 0) ||
512 (strcmp ("igb_uio", (char *) driver_name) == 0)))
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100513 goto err0;
Damjan Marion3c785e02017-05-08 18:37:54 +0200514
Benoît Ganne6a073482022-10-13 17:22:26 +0200515 if (!force)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700516 {
Benoît Ganne6a073482022-10-13 17:22:26 +0200517 /* walk trough all linux interfaces and if interface belonging to
518 this device is found check if interface is admin up */
519 dir = opendir ("/sys/class/net");
520 s = format (s, "%U%c", format_vlib_pci_addr, addr, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700521
Benoît Ganne6a073482022-10-13 17:22:26 +0200522 if (!dir)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400523 {
Benoît Ganne6a073482022-10-13 17:22:26 +0200524 error = clib_error_return (0,
525 "Skipping PCI device %U: failed to "
526 "read /sys/class/net",
527 format_vlib_pci_addr, addr);
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100528 goto err0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400529 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200530
Benoît Ganne6a073482022-10-13 17:22:26 +0200531 fd = socket (PF_INET, SOCK_DGRAM, 0);
532 if (fd < 0)
Damjan Mariona42cd342016-04-13 18:03:20 +0200533 {
Benoît Ganne6a073482022-10-13 17:22:26 +0200534 error = clib_error_return_unix (0, "socket");
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100535 goto err1;
Damjan Mariona42cd342016-04-13 18:03:20 +0200536 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700537
Benoît Ganne6a073482022-10-13 17:22:26 +0200538 while ((e = readdir (dir)))
539 {
540 struct ifreq ifr;
541 struct ethtool_drvinfo drvinfo;
542
543 if (e->d_name[0] == '.') /* skip . and .. */
544 continue;
545
546 clib_memset (&ifr, 0, sizeof ifr);
547 clib_memset (&drvinfo, 0, sizeof drvinfo);
548 ifr.ifr_data = (char *) &drvinfo;
549 clib_strncpy (ifr.ifr_name, e->d_name, sizeof (ifr.ifr_name) - 1);
550
551 drvinfo.cmd = ETHTOOL_GDRVINFO;
552 if (ioctl (fd, SIOCETHTOOL, &ifr) < 0)
553 {
554 /* Some interfaces (eg "lo") don't support this ioctl */
555 if ((errno != ENOTSUP) && (errno != ENODEV))
556 clib_unix_warning ("ioctl fetch intf %s bus info error",
557 e->d_name);
558 continue;
559 }
560
561 if (strcmp ((char *) s, drvinfo.bus_info))
562 continue;
563
564 clib_memset (&ifr, 0, sizeof (ifr));
565 clib_strncpy (ifr.ifr_name, e->d_name, sizeof (ifr.ifr_name) - 1);
566
567 if (ioctl (fd, SIOCGIFFLAGS, &ifr) < 0)
568 {
569 error = clib_error_return_unix (0, "ioctl fetch intf %s flags",
570 e->d_name);
571 close (fd);
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100572 goto err1;
Benoît Ganne6a073482022-10-13 17:22:26 +0200573 }
574
575 if (ifr.ifr_flags & IFF_UP)
576 {
577 vlib_log (VLIB_LOG_LEVEL_WARNING, pci_main.log_default,
578 "Skipping PCI device %U as host "
579 "interface %s is up",
580 format_vlib_pci_addr, addr, e->d_name);
581 close (fd);
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100582 goto err1;
Benoît Ganne6a073482022-10-13 17:22:26 +0200583 }
584 }
585
586 close (fd);
587 vec_reset_length (s);
588 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200589
590 s = format (s, "%v/driver/unbind%c", dev_dir_name, 0);
Damjan Marioncef87f12017-10-05 15:32:41 +0200591 clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
Damjan Mariona42cd342016-04-13 18:03:20 +0200592 vec_reset_length (s);
593
Damjan Mariona7f74572017-05-23 18:32:21 +0200594 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
595 if (access ((char *) s, F_OK) == 0)
596 {
Damjan Marioncef87f12017-10-05 15:32:41 +0200597 clib_sysfs_write ((char *) s, "%s", uio_drv_name);
Damjan Mariona7f74572017-05-23 18:32:21 +0200598 clear_driver_override = 1;
599 }
600 else
601 {
602 vec_reset_length (s);
Damjan Marioncef87f12017-10-05 15:32:41 +0200603 s = format (s, "%s/%s/new_id%c", sysfs_pci_drv_path, uio_drv_name, 0);
604 clib_sysfs_write ((char *) s, "0x%04x 0x%04x", di->vendor_id,
605 di->device_id);
Damjan Mariona7f74572017-05-23 18:32:21 +0200606 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200607 vec_reset_length (s);
608
Damjan Marioncef87f12017-10-05 15:32:41 +0200609 s = format (s, "%s/%s/bind%c", sysfs_pci_drv_path, uio_drv_name, 0);
610 clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
Damjan Mariona7f74572017-05-23 18:32:21 +0200611 vec_reset_length (s);
612
613 if (clear_driver_override)
614 {
615 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
Damjan Marion01914ce2017-09-14 19:04:50 +0200616 clib_sysfs_write ((char *) s, "%c", 0);
Damjan Mariona7f74572017-05-23 18:32:21 +0200617 vec_reset_length (s);
618 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200619
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100620err1:
Damjan Mariona42cd342016-04-13 18:03:20 +0200621 closedir (dir);
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100622err0:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700623 vec_free (s);
Damjan Mariona42cd342016-04-13 18:03:20 +0200624 vec_free (dev_dir_name);
Damjan Mariona7f74572017-05-23 18:32:21 +0200625 vec_free (driver_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700626 return error;
627}
628
Ed Warnickecb9cada2015-12-08 15:45:58 -0700629
630static clib_error_t *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400631scan_uio_dir (void *arg, u8 * path_name, u8 * file_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700632{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400633 linux_pci_device_t *l = arg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700634 unformat_input_t input;
635
636 unformat_init_string (&input, (char *) file_name, vec_len (file_name));
637
Dave Barach9b8ffd92016-07-08 08:13:45 -0400638 if (!unformat (&input, "uio%d", &l->uio_minor))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700639 abort ();
640
641 unformat_free (&input);
642 return 0;
643}
644
Dave Barach9b8ffd92016-07-08 08:13:45 -0400645static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200646vfio_set_irqs (vlib_main_t * vm, linux_pci_device_t * p, u32 index, u32 start,
647 u32 count, u32 flags, int *efds)
Damjan Marion599a16b2018-03-04 19:35:23 +0100648{
649 int data_len = efds ? count * sizeof (int) : 0;
650 u8 buf[sizeof (struct vfio_irq_set) + data_len];
Damjan Marion599a16b2018-03-04 19:35:23 +0100651 struct vfio_irq_set *irq_set = (struct vfio_irq_set *) buf;
652
Damjan Marion599a16b2018-03-04 19:35:23 +0100653 if (efds)
654 {
Damjan Marion7444fd22023-11-06 00:07:15 +0000655 int *data = (int *) irq_set->data;
Damjan Marion599a16b2018-03-04 19:35:23 +0100656 flags |= VFIO_IRQ_SET_DATA_EVENTFD;
Damjan Marion7444fd22023-11-06 00:07:15 +0000657 for (u32 i = 0; i < count; i++)
658 data[i] = efds[i];
Damjan Marion599a16b2018-03-04 19:35:23 +0100659 }
660 else
661 flags |= VFIO_IRQ_SET_DATA_NONE;
662
663 ASSERT ((flags & (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD)) !=
664 (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD));
665
666 irq_set->argsz = sizeof (struct vfio_irq_set) + data_len;
667 irq_set->index = index;
668 irq_set->start = start;
669 irq_set->count = count;
670 irq_set->flags = flags;
671
672 if (ioctl (p->fd, VFIO_DEVICE_SET_IRQS, irq_set) < 0)
Damjan Marion7444fd22023-11-06 00:07:15 +0000673 return clib_error_return_unix (0, "%U:ioctl(VFIO_DEVICE_SET_IRQS)\n%U",
Damjan Marion599a16b2018-03-04 19:35:23 +0100674 format_vlib_pci_addr, &p->addr,
Damjan Marion7444fd22023-11-06 00:07:15 +0000675 format_vfio_irq_set, irq_set);
676
677 log_debug (p, "%s:\n%U", __func__, format_vfio_irq_set, irq_set);
Damjan Marion599a16b2018-03-04 19:35:23 +0100678 return 0;
679}
680
681static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +0200682linux_pci_uio_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700683{
Damjan Marion23227982018-10-22 13:38:57 +0200684 vlib_main_t *vm = vlib_get_main ();
Damjan Marion5a206ea2016-05-12 22:11:03 +0200685 int __attribute__ ((unused)) rv;
Damjan Marioncef87f12017-10-05 15:32:41 +0200686 vlib_pci_dev_handle_t h = uf->private_data;
687 linux_pci_device_t *p = linux_pci_get_device (h);
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100688 linux_pci_irq_t *irq = &p->intx_irq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700689
Damjan Marion5a206ea2016-05-12 22:11:03 +0200690 u32 icount;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400691 rv = read (uf->file_descriptor, &icount, 4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700692
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100693 if (irq->intx_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200694 irq->intx_handler (vm, h);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200695
Damjan Marion23227982018-10-22 13:38:57 +0200696 vlib_pci_intr_enable (vm, h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700697
698 return /* no error */ 0;
699}
700
Dave Barach9b8ffd92016-07-08 08:13:45 -0400701static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200702linux_pci_vfio_unmask_intx (vlib_main_t * vm, linux_pci_device_t * d)
Damjan Marioncef87f12017-10-05 15:32:41 +0200703{
Damjan Marion23227982018-10-22 13:38:57 +0200704 return vfio_set_irqs (vm, d, VFIO_PCI_INTX_IRQ_INDEX, 0, 1,
Damjan Marion599a16b2018-03-04 19:35:23 +0100705 VFIO_IRQ_SET_ACTION_UNMASK, 0);
706}
707
Damjan Marioncef87f12017-10-05 15:32:41 +0200708static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +0200709linux_pci_uio_error_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700710{
711 u32 error_index = (u32) uf->private_data;
712
713 return clib_error_return (0, "pci device %d: error", error_index);
714}
715
Damjan Marioncef87f12017-10-05 15:32:41 +0200716static clib_error_t *
Damjan Marion599a16b2018-03-04 19:35:23 +0100717linux_pci_vfio_msix_read_ready (clib_file_t * uf)
718{
Damjan Marion23227982018-10-22 13:38:57 +0200719 vlib_main_t *vm = vlib_get_main ();
Damjan Marion599a16b2018-03-04 19:35:23 +0100720 int __attribute__ ((unused)) rv;
721 vlib_pci_dev_handle_t h = uf->private_data >> 16;
722 u16 line = uf->private_data & 0xffff;
723 linux_pci_device_t *p = linux_pci_get_device (h);
724 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, line);
725
726 u64 icount;
727 rv = read (uf->file_descriptor, &icount, sizeof (icount));
728
729 if (irq->msix_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200730 irq->msix_handler (vm, h, line);
Damjan Marion599a16b2018-03-04 19:35:23 +0100731
732 return /* no error */ 0;
733}
734
735static clib_error_t *
736linux_pci_vfio_intx_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700737{
Damjan Marion23227982018-10-22 13:38:57 +0200738 vlib_main_t *vm = vlib_get_main ();
Damjan Marioncef87f12017-10-05 15:32:41 +0200739 int __attribute__ ((unused)) rv;
740 vlib_pci_dev_handle_t h = uf->private_data;
741 linux_pci_device_t *p = linux_pci_get_device (h);
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100742 linux_pci_irq_t *irq = &p->intx_irq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700743
Damjan Marioncef87f12017-10-05 15:32:41 +0200744 u64 icount;
745 rv = read (uf->file_descriptor, &icount, sizeof (icount));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700746
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100747 if (irq->intx_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200748 irq->intx_handler (vm, h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700749
Damjan Marion23227982018-10-22 13:38:57 +0200750 linux_pci_vfio_unmask_intx (vm, p);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700751
Damjan Marioncef87f12017-10-05 15:32:41 +0200752 return /* no error */ 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700753}
754
Damjan Marioncef87f12017-10-05 15:32:41 +0200755static clib_error_t *
756linux_pci_vfio_error_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700757{
Damjan Marioncef87f12017-10-05 15:32:41 +0200758 u32 error_index = (u32) uf->private_data;
759
760 return clib_error_return (0, "pci device %d: error", error_index);
761}
762
763static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200764add_device_uio (vlib_main_t * vm, linux_pci_device_t * p,
765 vlib_pci_device_info_t * di, pci_device_registration_t * r)
Damjan Marioncef87f12017-10-05 15:32:41 +0200766{
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100767 linux_pci_main_t *lpm = &linux_pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +0200768 clib_error_t *err = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200769 u8 *s = 0;
770
Damjan Marionf9a968e2018-02-25 23:26:22 +0100771 p->fd = -1;
Damjan Marion2060db82018-03-04 17:37:15 +0100772 p->type = LINUX_PCI_DEVICE_TYPE_UIO;
Damjan Marioncef87f12017-10-05 15:32:41 +0200773
774 s = format (s, "%s/%U/config%c", sysfs_pci_dev_path,
775 format_vlib_pci_addr, &di->addr, 0);
776
777 p->config_fd = open ((char *) s, O_RDWR);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100778 p->config_offset = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200779 vec_reset_length (s);
780
781 if (p->config_fd == -1)
782 {
783 err = clib_error_return_unix (0, "open '%s'", s);
784 goto error;
785 }
786
Benoît Ganne0b91bd62019-11-20 09:07:50 +0100787 s = format (0, "%s/%U/uio%c", sysfs_pci_dev_path,
788 format_vlib_pci_addr, &di->addr, 0);
Damjan Marioncef87f12017-10-05 15:32:41 +0200789 foreach_directory_file ((char *) s, scan_uio_dir, p, /* scan_dirs */
790 1);
791 vec_reset_length (s);
792
793 s = format (s, "/dev/uio%d%c", p->uio_minor, 0);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100794 p->fd = open ((char *) s, O_RDWR);
795 if (p->fd < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +0200796 {
797 err = clib_error_return_unix (0, "open '%s'", s);
798 goto error;
799 }
800
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100801 if (r && r->interrupt_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200802 vlib_pci_register_intx_handler (vm, p->handle, r->interrupt_handler);
Damjan Marioncef87f12017-10-05 15:32:41 +0200803
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100804 if (r && r->init_function)
805 err = r->init_function (lpm->vlib_main, p->handle);
Damjan Marioncef87f12017-10-05 15:32:41 +0200806
807error:
Damjan Marion20ba1642018-03-26 15:35:33 +0200808 vec_free (s);
Damjan Marioncef87f12017-10-05 15:32:41 +0200809 if (err)
810 {
Damjan Marion96504182017-12-10 23:54:46 +0100811 if (p->config_fd != -1)
812 close (p->config_fd);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100813 if (p->fd != -1)
814 close (p->fd);
Damjan Marioncef87f12017-10-05 15:32:41 +0200815 }
816 return err;
817}
818
Damjan Marion599a16b2018-03-04 19:35:23 +0100819clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200820vlib_pci_register_intx_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h,
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100821 pci_intx_handler_function_t * intx_handler)
822{
823 linux_pci_device_t *p = linux_pci_get_device (h);
824 clib_file_t t = { 0 };
825 linux_pci_irq_t *irq = &p->intx_irq;
826 ASSERT (irq->fd == -1);
827
828 if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
829 {
Damjan Marion23227982018-10-22 13:38:57 +0200830 struct vfio_irq_info ii = { 0 };
831 ii.argsz = sizeof (struct vfio_irq_info);
832 ii.index = VFIO_PCI_INTX_IRQ_INDEX;
833 if (ioctl (p->fd, VFIO_DEVICE_GET_IRQ_INFO, &ii) < 0)
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100834 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) '"
835 "%U'", format_vlib_pci_addr, &p->addr);
Damjan Marion00ea98a2023-07-28 13:19:49 +0200836 log_debug (
837 p, "%s index:%u count:%u flags: %s%s%s%s(0x%x)", __func__, ii.index,
838 ii.count, ii.flags & VFIO_IRQ_INFO_EVENTFD ? "eventfd " : "",
839 ii.flags & VFIO_IRQ_INFO_MASKABLE ? "maskable " : "",
840 ii.flags & VFIO_IRQ_INFO_AUTOMASKED ? "automasked " : "",
841 ii.flags & VFIO_IRQ_INFO_NORESIZE ? "noresize " : "", ii.flags);
Damjan Marion23227982018-10-22 13:38:57 +0200842 if (ii.count != 1)
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100843 return clib_error_return (0, "INTx interrupt does not exist on device"
844 "'%U'", format_vlib_pci_addr, &p->addr);
845
846 irq->fd = eventfd (0, EFD_NONBLOCK);
847 if (irq->fd == -1)
848 return clib_error_return_unix (0, "eventfd");
Damjan Marion23227982018-10-22 13:38:57 +0200849
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100850 t.file_descriptor = irq->fd;
851 t.read_function = linux_pci_vfio_intx_read_ready;
852 }
853 else if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
854 {
855 t.file_descriptor = p->fd;
856 t.read_function = linux_pci_uio_read_ready;
857 }
858 else
859 return 0;
860
861 t.error_function = linux_pci_uio_error_ready;
862 t.private_data = p->handle;
863 t.description = format (0, "PCI %U INTx", format_vlib_pci_addr, &p->addr);
864 irq->clib_file_index = clib_file_add (&file_main, &t);
865 irq->intx_handler = intx_handler;
866 return 0;
867}
868
869clib_error_t *
Damjan Marion38c61912023-10-17 16:06:26 +0000870vlib_pci_unregister_intx_handler (vlib_main_t *vm, vlib_pci_dev_handle_t h)
871{
872 linux_pci_device_t *p = linux_pci_get_device (h);
873 linux_pci_irq_t *irq = &p->intx_irq;
874
875 if (irq->intx_handler == 0)
876 return 0;
877
878 clib_file_del_by_index (&file_main, irq->clib_file_index);
879 if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
880 {
881 close (irq->fd);
882 irq->fd = -1;
883 }
884
885 irq->intx_handler = 0;
886
887 return 0;
888}
889
890clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200891vlib_pci_register_msix_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h,
892 u32 start, u32 count,
Damjan Marion599a16b2018-03-04 19:35:23 +0100893 pci_msix_handler_function_t * msix_handler)
894{
895 clib_error_t *err = 0;
896 linux_pci_device_t *p = linux_pci_get_device (h);
897 u32 i;
898
899 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
900 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
901 "support");
902
903 /* *INDENT-OFF* */
904 vec_validate_init_empty (p->msix_irqs, start + count - 1, (linux_pci_irq_t)
905 { .fd = -1});
906 /* *INDENT-ON* */
907
908 for (i = start; i < start + count; i++)
909 {
910 clib_file_t t = { 0 };
911 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
912 ASSERT (irq->fd == -1);
913
914 irq->fd = eventfd (0, EFD_NONBLOCK);
915 if (irq->fd == -1)
916 {
917 err = clib_error_return_unix (0, "eventfd");
918 goto error;
919 }
920
921 t.read_function = linux_pci_vfio_msix_read_ready;
922 t.file_descriptor = irq->fd;
923 t.error_function = linux_pci_vfio_error_ready;
924 t.private_data = p->handle << 16 | i;
925 t.description = format (0, "PCI %U MSI-X #%u", format_vlib_pci_addr,
926 &p->addr, i);
927 irq->clib_file_index = clib_file_add (&file_main, &t);
928 irq->msix_handler = msix_handler;
929 }
930
931 return 0;
932
933error:
934 while (i-- > start)
935 {
936 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
937 if (irq->fd != -1)
938 {
939 clib_file_del_by_index (&file_main, irq->clib_file_index);
940 close (irq->fd);
941 irq->fd = -1;
942 }
943 }
944 return err;
945}
946
947clib_error_t *
Damjan Marion38c61912023-10-17 16:06:26 +0000948vlib_pci_unregister_msix_handler (vlib_main_t *vm, vlib_pci_dev_handle_t h,
949 u32 start, u32 count)
950{
951 clib_error_t *err = 0;
952 linux_pci_device_t *p = linux_pci_get_device (h);
953 u32 i;
954
955 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
956 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
957 "support");
958
959 for (i = start; i < start + count; i++)
960 {
961 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
962
963 if (irq->fd != -1)
964 {
965 clib_file_del_by_index (&file_main, irq->clib_file_index);
966 close (irq->fd);
967 irq->fd = -1;
968 }
969 }
970
971 return err;
972}
973
974clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200975vlib_pci_enable_msix_irq (vlib_main_t * vm, vlib_pci_dev_handle_t h,
976 u16 start, u16 count)
Damjan Marion599a16b2018-03-04 19:35:23 +0100977{
978 linux_pci_device_t *p = linux_pci_get_device (h);
979 int fds[count];
980 int i;
981
982 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
983 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
984 "support");
985
Damjan Marion60529a82023-11-06 00:06:26 +0000986 for (i = 0; i < count; i++)
Damjan Marion599a16b2018-03-04 19:35:23 +0100987 {
Damjan Marion60529a82023-11-06 00:06:26 +0000988 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, start + i);
Damjan Marion599a16b2018-03-04 19:35:23 +0100989 fds[i] = irq->fd;
990 }
991
Damjan Marion23227982018-10-22 13:38:57 +0200992 return vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
Damjan Marion599a16b2018-03-04 19:35:23 +0100993 VFIO_IRQ_SET_ACTION_TRIGGER, fds);
994}
995
Damjan Marion9c9490c2020-10-07 20:00:39 +0200996uword
997vlib_pci_get_msix_file_index (vlib_main_t * vm, vlib_pci_dev_handle_t h,
998 u16 index)
999{
1000 linux_pci_device_t *p = linux_pci_get_device (h);
1001 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, index);
1002 if (irq->fd == -1)
1003 return ~0;
1004 return irq->clib_file_index;
1005}
1006
Damjan Marion599a16b2018-03-04 19:35:23 +01001007clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001008vlib_pci_disable_msix_irq (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1009 u16 start, u16 count)
Damjan Marion599a16b2018-03-04 19:35:23 +01001010{
1011 linux_pci_device_t *p = linux_pci_get_device (h);
1012 int i, fds[count];
1013
1014 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
1015 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
1016 "support");
1017
1018 for (i = start; i < start + count; i++)
1019 fds[i] = -1;
1020
Damjan Marion23227982018-10-22 13:38:57 +02001021 return vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
Damjan Marion599a16b2018-03-04 19:35:23 +01001022 VFIO_IRQ_SET_ACTION_TRIGGER, fds);
1023}
1024
Damjan Marioncef87f12017-10-05 15:32:41 +02001025static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001026add_device_vfio (vlib_main_t * vm, linux_pci_device_t * p,
1027 vlib_pci_device_info_t * di, pci_device_registration_t * r)
Damjan Marioncef87f12017-10-05 15:32:41 +02001028{
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001029 linux_pci_main_t *lpm = &linux_pci_main;
Damjan Marionf9a968e2018-02-25 23:26:22 +01001030 struct vfio_device_info device_info = { 0 };
Damjan Marionf313b742018-03-05 14:08:33 +01001031 struct vfio_region_info reg = { 0 };
Damjan Marioncef87f12017-10-05 15:32:41 +02001032 clib_error_t *err = 0;
1033 u8 *s = 0;
Damjan Marion23227982018-10-22 13:38:57 +02001034 int is_noiommu;
Damjan Marioncef87f12017-10-05 15:32:41 +02001035
Damjan Marion2060db82018-03-04 17:37:15 +01001036 p->type = LINUX_PCI_DEVICE_TYPE_VFIO;
Damjan Marioncef87f12017-10-05 15:32:41 +02001037
Damjan Marion23227982018-10-22 13:38:57 +02001038 if ((err = linux_vfio_group_get_device_fd (&p->addr, &p->fd, &is_noiommu)))
Damjan Marioncef87f12017-10-05 15:32:41 +02001039 return err;
1040
Damjan Marion23227982018-10-22 13:38:57 +02001041 if (is_noiommu == 0)
1042 p->supports_va_dma = 1;
1043
Damjan Marioncef87f12017-10-05 15:32:41 +02001044 device_info.argsz = sizeof (device_info);
Damjan Marionf9a968e2018-02-25 23:26:22 +01001045 if (ioctl (p->fd, VFIO_DEVICE_GET_INFO, &device_info) < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +02001046 {
1047 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
1048 format_vlib_pci_addr, &di->addr);
1049 goto error;
1050 }
1051
Damjan Marionf313b742018-03-05 14:08:33 +01001052 reg.argsz = sizeof (struct vfio_region_info);
1053 reg.index = VFIO_PCI_CONFIG_REGION_INDEX;
1054 if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, &reg) < 0)
1055 {
1056 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
1057 format_vlib_pci_addr, &di->addr);
1058 goto error;
1059 }
Damjan Marion23227982018-10-22 13:38:57 +02001060
Damjan Marion00ea98a2023-07-28 13:19:49 +02001061 log_debug (p, "%s %U", __func__, format_vfio_region_info, &reg);
Damjan Marion23227982018-10-22 13:38:57 +02001062
Damjan Marionf313b742018-03-05 14:08:33 +01001063 p->config_offset = reg.offset;
1064 p->config_fd = p->fd;
1065
Damjan Marioncef87f12017-10-05 15:32:41 +02001066 /* reset if device supports it */
1067 if (device_info.flags & VFIO_DEVICE_FLAGS_RESET)
Damjan Marionf9a968e2018-02-25 23:26:22 +01001068 if (ioctl (p->fd, VFIO_DEVICE_RESET) < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +02001069 {
1070 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_RESET) '%U'",
1071 format_vlib_pci_addr, &di->addr);
1072 goto error;
1073 }
1074
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001075 if (r && r->interrupt_handler)
Damjan Marioncef87f12017-10-05 15:32:41 +02001076 {
Damjan Marion23227982018-10-22 13:38:57 +02001077 vlib_pci_register_intx_handler (vm, p->handle, r->interrupt_handler);
1078 linux_pci_vfio_unmask_intx (vm, p);
Damjan Marioncef87f12017-10-05 15:32:41 +02001079 }
1080
Damjan Marion68b4da62018-09-30 18:26:20 +02001081 if (p->supports_va_dma)
1082 {
1083 vlib_buffer_pool_t *bp;
1084 /* *INDENT-OFF* */
Damjan Mariond50e3472019-01-20 00:03:56 +01001085 vec_foreach (bp, vm->buffer_main->buffer_pools)
Damjan Marion68b4da62018-09-30 18:26:20 +02001086 {
1087 u32 i;
1088 vlib_physmem_map_t *pm;
1089 pm = vlib_physmem_get_map (vm, bp->physmem_map_index);
1090 for (i = 0; i < pm->n_pages; i++)
1091 vfio_map_physmem_page (vm, pm->base + (i << pm->log2_page_size));
1092 }
1093 /* *INDENT-ON* */
1094 }
1095
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001096 if (r && r->init_function)
1097 err = r->init_function (lpm->vlib_main, p->handle);
Damjan Marioncef87f12017-10-05 15:32:41 +02001098
1099error:
1100 vec_free (s);
1101 if (err)
1102 {
Damjan Marionf9a968e2018-02-25 23:26:22 +01001103 if (p->fd != -1)
1104 close (p->fd);
Chris Luke30684ac2018-03-29 12:56:58 -07001105 if (p->config_fd != -1 && p->config_fd != p->fd)
Damjan Marion96504182017-12-10 23:54:46 +01001106 close (p->config_fd);
Chris Luke30684ac2018-03-29 12:56:58 -07001107 p->config_fd = p->fd = -1;
Damjan Marioncef87f12017-10-05 15:32:41 +02001108 }
1109 return err;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001110}
1111
1112/* Configuration space read/write. */
1113clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001114vlib_pci_read_write_config (vlib_main_t * vm, vlib_pci_dev_handle_t h,
Damjan Marion5a206ea2016-05-12 22:11:03 +02001115 vlib_read_or_write_t read_or_write,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001116 uword address, void *data, u32 n_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001117{
Damjan Marioncef87f12017-10-05 15:32:41 +02001118 linux_pci_device_t *p = linux_pci_get_device (h);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001119 int n;
1120
Ed Warnickecb9cada2015-12-08 15:45:58 -07001121 if (read_or_write == VLIB_READ)
Damjan Marionf9a968e2018-02-25 23:26:22 +01001122 n = pread (p->config_fd, data, n_bytes, p->config_offset + address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001123 else
Damjan Marionf9a968e2018-02-25 23:26:22 +01001124 n = pwrite (p->config_fd, data, n_bytes, p->config_offset + address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001125
1126 if (n != n_bytes)
1127 return clib_error_return_unix (0, "%s",
1128 read_or_write == VLIB_READ
1129 ? "read" : "write");
1130
1131 return 0;
1132}
1133
1134static clib_error_t *
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001135vlib_pci_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 bar, int *fd,
1136 u64 * size, u64 * offset)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001137{
Damjan Marioncef87f12017-10-05 15:32:41 +02001138 linux_pci_device_t *p = linux_pci_get_device (h);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001139 clib_error_t *error = 0;
1140 int _fd = -1;
1141 u64 _size = 0, _offset = 0;
Damjan Marion2060db82018-03-04 17:37:15 +01001142
1143 ASSERT (bar <= 5);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001144
1145 error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001146
Damjan Marion2060db82018-03-04 17:37:15 +01001147 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001148 {
Damjan Marion2060db82018-03-04 17:37:15 +01001149 u8 *file_name;
1150 struct stat stat_buf;
1151 file_name = format (0, "%s/%U/resource%d%c", sysfs_pci_dev_path,
1152 format_vlib_pci_addr, &p->addr, bar, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001153
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001154 _fd = open ((char *) file_name, O_RDWR);
1155 if (_fd < 0)
Damjan Marion2060db82018-03-04 17:37:15 +01001156 {
1157 error = clib_error_return_unix (0, "open `%s'", file_name);
1158 vec_free (file_name);
1159 return error;
1160 }
1161
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001162 if (fstat (_fd, &stat_buf) < 0)
Damjan Marion2060db82018-03-04 17:37:15 +01001163 {
1164 error = clib_error_return_unix (0, "fstat `%s'", file_name);
1165 vec_free (file_name);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001166 close (_fd);
Damjan Marion2060db82018-03-04 17:37:15 +01001167 return error;
1168 }
1169
1170 vec_free (file_name);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001171 _size = stat_buf.st_size;
1172 _offset = 0;
Damjan Marion2060db82018-03-04 17:37:15 +01001173 }
1174 else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001175 {
Damjan Marion0792bb42020-05-25 19:31:42 +02001176 struct vfio_region_info *r;
1177 u32 sz = sizeof (struct vfio_region_info);
1178 again:
1179 r = clib_mem_alloc (sz);
1180 clib_memset (r, 0, sz);
1181 r->argsz = sz;
1182 r->index = bar;
1183 if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, r) < 0)
Damjan Marion2060db82018-03-04 17:37:15 +01001184 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) "
1185 "'%U'", format_vlib_pci_addr,
1186 &p->addr);
Damjan Marion0792bb42020-05-25 19:31:42 +02001187 if (sz != r->argsz)
1188 {
1189 sz = r->argsz;
1190 clib_mem_free (r);
1191 goto again;
1192 }
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001193 _fd = p->fd;
Damjan Marion0792bb42020-05-25 19:31:42 +02001194 _size = r->size;
1195 _offset = r->offset;
Damjan Marion00ea98a2023-07-28 13:19:49 +02001196 log_debug (p, "%s %U", __func__, format_vfio_region_info, r);
Damjan Marion0792bb42020-05-25 19:31:42 +02001197 clib_mem_free (r);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001198 }
Damjan Marion2060db82018-03-04 17:37:15 +01001199 else
1200 ASSERT (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001201
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001202 *fd = _fd;
1203 *size = _size;
1204 *offset = _offset;
1205
1206 return error;
1207}
1208
1209static clib_error_t *
1210vlib_pci_map_region_int (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1211 u32 bar, u8 * addr, void **result)
1212{
1213 linux_pci_device_t *p = linux_pci_get_device (h);
1214 int fd = -1;
1215 clib_error_t *error;
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001216 u64 size = 0, offset = 0;
Damjan Marion00ea98a2023-07-28 13:19:49 +02001217 vlib_pci_config_reg_command_t command;
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001218
Damjan Marion00ea98a2023-07-28 13:19:49 +02001219 log_debug (p, "map region %u to va %p", bar, addr);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001220
Damjan Marion00ea98a2023-07-28 13:19:49 +02001221 if ((error = vlib_pci_read_config_u16 (vm, h, 4, &command.as_u16)))
Mohammed Hawari70fc36f2020-10-21 16:41:30 +02001222 return error;
1223
Damjan Marion00ea98a2023-07-28 13:19:49 +02001224 if (!(command.mem_space))
Mohammed Hawari70fc36f2020-10-21 16:41:30 +02001225 {
Damjan Marion00ea98a2023-07-28 13:19:49 +02001226 log_debug (p, "setting memory enable bit");
1227 command.mem_space = 1;
1228 if ((error = vlib_pci_write_config_u16 (vm, h, 4, &command.as_u16)))
Mohammed Hawari70fc36f2020-10-21 16:41:30 +02001229 return error;
1230 }
1231
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001232 if ((error = vlib_pci_region (vm, h, bar, &fd, &size, &offset)))
1233 return error;
1234
Damjan Marion6bfd0762020-09-11 22:16:53 +02001235 *result = clib_mem_vm_map_shared (addr, size, fd, offset,
1236 "PCIe %U region %u", format_vlib_pci_addr,
1237 vlib_pci_get_addr (vm, h), bar);
1238 if (*result == CLIB_MEM_VM_MAP_FAILED)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001239 {
Damjan Marion2060db82018-03-04 17:37:15 +01001240 error = clib_error_return_unix (0, "mmap `BAR%u'", bar);
Dave Barachc72950e2020-05-04 14:47:45 -04001241 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO && (fd != -1))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001242 close (fd);
Damjan Marion2060db82018-03-04 17:37:15 +01001243 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001244 }
Damjan Marion2060db82018-03-04 17:37:15 +01001245
1246 /* *INDENT-OFF* */
1247 vec_validate_init_empty (p->regions, bar,
1248 (linux_pci_region_t) { .fd = -1});
1249 /* *INDENT-ON* */
1250 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1251 p->regions[bar].fd = fd;
1252 p->regions[bar].addr = *result;
1253 p->regions[bar].size = size;
1254 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001255}
1256
1257clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001258vlib_pci_map_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 resource,
1259 void **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001260{
Damjan Marion23227982018-10-22 13:38:57 +02001261 return (vlib_pci_map_region_int (vm, h, resource, 0 /* addr */ , result));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001262}
1263
1264clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001265vlib_pci_map_region_fixed (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1266 u32 resource, u8 * addr, void **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001267{
Damjan Marion23227982018-10-22 13:38:57 +02001268 return (vlib_pci_map_region_int (vm, h, resource, addr, result));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001269}
1270
Damjan Marionf313b742018-03-05 14:08:33 +01001271clib_error_t *
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001272vlib_pci_io_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 resource)
1273{
1274 linux_pci_device_t *p = linux_pci_get_device (h);
1275 clib_error_t *error = 0;
1276 int fd = -1;
1277 u64 size = 0, offset = 0;
1278
1279 if ((error = vlib_pci_region (vm, h, resource, &fd, &size, &offset)))
1280 return error;
1281
1282 p->io_fd = fd;
1283 p->io_offset = offset;
1284 return error;
1285}
1286
1287clib_error_t *
1288vlib_pci_read_write_io (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1289 vlib_read_or_write_t read_or_write,
1290 uword offset, void *data, u32 length)
1291{
1292 linux_pci_device_t *p = linux_pci_get_device (h);
1293 int n = 0;
1294
1295 if (read_or_write == VLIB_READ)
1296 n = pread (p->io_fd, data, length, p->io_offset + offset);
1297 else
1298 n = pwrite (p->io_fd, data, length, p->io_offset + offset);
1299
1300 if (n != length)
1301 return clib_error_return_unix (0, "%s",
1302 read_or_write == VLIB_READ
1303 ? "read" : "write");
1304 return 0;
1305}
1306
1307clib_error_t *
Damjan Marion68b4da62018-09-30 18:26:20 +02001308vlib_pci_map_dma (vlib_main_t * vm, vlib_pci_dev_handle_t h, void *ptr)
1309{
1310 linux_pci_device_t *p = linux_pci_get_device (h);
1311
1312 if (!p->supports_va_dma)
1313 return 0;
1314
1315 return vfio_map_physmem_page (vm, ptr);
1316}
1317
1318int
1319vlib_pci_supports_virtual_addr_dma (vlib_main_t * vm, vlib_pci_dev_handle_t h)
1320{
1321 linux_pci_device_t *p = linux_pci_get_device (h);
1322
1323 return p->supports_va_dma != 0;
1324}
1325
1326clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001327vlib_pci_device_open (vlib_main_t * vm, vlib_pci_addr_t * addr,
Damjan Marionf313b742018-03-05 14:08:33 +01001328 pci_device_id_t ids[], vlib_pci_dev_handle_t * handle)
1329{
1330 linux_pci_main_t *lpm = &linux_pci_main;
1331 vlib_pci_device_info_t *di;
1332 linux_pci_device_t *p;
1333 clib_error_t *err = 0;
1334 pci_device_id_t *i;
1335
Damjan Marion23227982018-10-22 13:38:57 +02001336 di = vlib_pci_get_device_info (vm, addr, &err);
Damjan Marionf313b742018-03-05 14:08:33 +01001337
1338 if (err)
1339 return err;
Damjan Marionf313b742018-03-05 14:08:33 +01001340
Damjan Marion00ea98a2023-07-28 13:19:49 +02001341 if (ids)
1342 {
1343 for (i = ids; i->vendor_id != 0; i++)
1344 if (i->vendor_id == di->vendor_id && i->device_id == di->device_id)
1345 break;
1346
1347 if (i->vendor_id == 0)
1348 {
1349 vlib_pci_free_device_info (di);
1350 return clib_error_return (0, "Wrong vendor or device id");
1351 }
1352 }
Damjan Marionf313b742018-03-05 14:08:33 +01001353
1354 pool_get (lpm->linux_pci_devices, p);
1355 p->handle = p - lpm->linux_pci_devices;
Damjan Marion23227982018-10-22 13:38:57 +02001356 p->addr.as_u32 = di->addr.as_u32;
Damjan Marionf313b742018-03-05 14:08:33 +01001357 p->intx_irq.fd = -1;
Mohsin Kazmi692f9b12019-04-02 11:09:49 +00001358 p->intx_irq.clib_file_index = -1;
Damjan Mariond2bfb782019-01-07 20:56:04 +01001359 p->numa_node = di->numa_node;
Mohsin Kazmi3d3b9552018-10-24 14:05:34 +02001360 /*
1361 * pci io bar read/write fd
1362 */
1363 p->io_fd = -1;
Damjan Marionf313b742018-03-05 14:08:33 +01001364
Damjan Marion00ea98a2023-07-28 13:19:49 +02001365 log_debug (p, "open vid:0x%04x did:0x%04x driver:%s iommu_group:%d",
1366 di->vendor_id, di->device_id, di->driver_name, di->iommu_group);
Damjan Marion23227982018-10-22 13:38:57 +02001367
Steven Luongab489922019-08-21 11:35:27 -07001368 if (clib_strncmp ("vfio-pci", (char *) di->driver_name, 8) == 0)
Damjan Marion23227982018-10-22 13:38:57 +02001369 err = add_device_vfio (vm, p, di, 0);
Steven Luongab489922019-08-21 11:35:27 -07001370 else if (clib_strncmp ("uio_pci_generic", (char *) di->driver_name, 8) == 0)
Damjan Marion23227982018-10-22 13:38:57 +02001371 err = add_device_uio (vm, p, di, 0);
Damjan Marionf313b742018-03-05 14:08:33 +01001372 else
Damjan Marion23227982018-10-22 13:38:57 +02001373 err = clib_error_create ("device not bound to 'vfio-pci' or "
1374 "'uio_pci_generic' kernel module");
Damjan Marionf313b742018-03-05 14:08:33 +01001375 if (err)
1376 goto error;
1377
1378 *handle = p->handle;
1379
1380error:
1381 vlib_pci_free_device_info (di);
1382 if (err)
1383 {
Damjan Marion00ea98a2023-07-28 13:19:49 +02001384 log_err (p, "%U", format_clib_error, err);
Dave Barachb7b92992018-10-17 10:38:51 -04001385 clib_memset (p, 0, sizeof (linux_pci_device_t));
Damjan Marionf313b742018-03-05 14:08:33 +01001386 pool_put (lpm->linux_pci_devices, p);
1387 }
1388
1389 return err;
1390}
1391
1392void
Damjan Marion23227982018-10-22 13:38:57 +02001393vlib_pci_device_close (vlib_main_t * vm, vlib_pci_dev_handle_t h)
Damjan Marionf313b742018-03-05 14:08:33 +01001394{
1395 linux_pci_main_t *lpm = &linux_pci_main;
1396 linux_pci_device_t *p = linux_pci_get_device (h);
1397 linux_pci_irq_t *irq;
1398 linux_pci_region_t *res;
1399 clib_error_t *err = 0;
1400
1401 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1402 {
1403 irq = &p->intx_irq;
Mohsin Kazmi692f9b12019-04-02 11:09:49 +00001404 if (irq->clib_file_index != -1)
1405 clib_file_del_by_index (&file_main, irq->clib_file_index);
Damjan Marionf313b742018-03-05 14:08:33 +01001406 close (p->config_fd);
Mohsin Kazmi3d3b9552018-10-24 14:05:34 +02001407 if (p->io_fd != -1)
1408 close (p->io_fd);
Damjan Marionf313b742018-03-05 14:08:33 +01001409 }
1410 else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
1411 {
1412 irq = &p->intx_irq;
1413 /* close INTx irqs */
1414 if (irq->fd != -1)
1415 {
Damjan Marion23227982018-10-22 13:38:57 +02001416 err = vfio_set_irqs (vm, p, VFIO_PCI_INTX_IRQ_INDEX, 0, 0,
Damjan Marionf313b742018-03-05 14:08:33 +01001417 VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1418 clib_error_free (err);
Mohsin Kazmi692f9b12019-04-02 11:09:49 +00001419 if (irq->clib_file_index != -1)
1420 clib_file_del_by_index (&file_main, irq->clib_file_index);
Damjan Marionf313b742018-03-05 14:08:33 +01001421 close (irq->fd);
1422 }
1423
1424 /* close MSI-X irqs */
1425 if (vec_len (p->msix_irqs))
1426 {
Damjan Marion23227982018-10-22 13:38:57 +02001427 err = vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, 0, 0,
Damjan Marionf313b742018-03-05 14:08:33 +01001428 VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1429 clib_error_free (err);
1430 /* *INDENT-OFF* */
1431 vec_foreach (irq, p->msix_irqs)
1432 {
1433 if (irq->fd == -1)
1434 continue;
1435 clib_file_del_by_index (&file_main, irq->clib_file_index);
1436 close (irq->fd);
1437 }
1438 /* *INDENT-ON* */
1439 vec_free (p->msix_irqs);
1440 }
1441 }
1442
1443 /* *INDENT-OFF* */
1444 vec_foreach (res, p->regions)
1445 {
1446 if (res->size == 0)
1447 continue;
Damjan Marion6bfd0762020-09-11 22:16:53 +02001448 clib_mem_vm_unmap (res->addr);
Damjan Marionf313b742018-03-05 14:08:33 +01001449 if (res->fd != -1)
1450 close (res->fd);
1451 }
1452 /* *INDENT-ON* */
1453 vec_free (p->regions);
1454
1455 close (p->fd);
Dave Barachb7b92992018-10-17 10:38:51 -04001456 clib_memset (p, 0, sizeof (linux_pci_device_t));
Damjan Marionf313b742018-03-05 14:08:33 +01001457 pool_put (lpm->linux_pci_devices, p);
1458}
1459
Dave Barach9b8ffd92016-07-08 08:13:45 -04001460void
Damjan Marion23227982018-10-22 13:38:57 +02001461init_device_from_registered (vlib_main_t * vm, vlib_pci_device_info_t * di)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001462{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001463 vlib_pci_main_t *pm = &pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +02001464 linux_pci_main_t *lpm = &linux_pci_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001465 pci_device_registration_t *r;
1466 pci_device_id_t *i;
Damjan Marioncef87f12017-10-05 15:32:41 +02001467 clib_error_t *err = 0;
1468 linux_pci_device_t *p;
1469
1470 pool_get (lpm->linux_pci_devices, p);
1471 p->handle = p - lpm->linux_pci_devices;
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001472 p->intx_irq.fd = -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001473
Damjan Marion5a206ea2016-05-12 22:11:03 +02001474 r = pm->pci_device_registrations;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001475
1476 while (r)
1477 {
1478 for (i = r->supported_devices; i->vendor_id != 0; i++)
Damjan Marion3a593822018-02-17 14:06:53 +01001479 if (i->vendor_id == di->vendor_id && i->device_id == di->device_id)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001480 {
Damjan Marioncef87f12017-10-05 15:32:41 +02001481 if (di->iommu_group != -1)
Damjan Marion23227982018-10-22 13:38:57 +02001482 err = add_device_vfio (vm, p, di, r);
Damjan Marioncef87f12017-10-05 15:32:41 +02001483 else
Damjan Marion23227982018-10-22 13:38:57 +02001484 err = add_device_uio (vm, p, di, r);
Damjan Marion5a206ea2016-05-12 22:11:03 +02001485
Damjan Marioncef87f12017-10-05 15:32:41 +02001486 if (err)
1487 clib_error_report (err);
1488 else
1489 return;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001490 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001491 r = r->next_registration;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001492 }
Damjan Marioncef87f12017-10-05 15:32:41 +02001493
Ed Warnickecb9cada2015-12-08 15:45:58 -07001494 /* No driver, close the PCI config-space FD */
Dave Barachb7b92992018-10-17 10:38:51 -04001495 clib_memset (p, 0, sizeof (linux_pci_device_t));
Damjan Marioncef87f12017-10-05 15:32:41 +02001496 pool_put (lpm->linux_pci_devices, p);
1497}
1498
1499static clib_error_t *
1500scan_pci_addr (void *arg, u8 * dev_dir_name, u8 * ignored)
1501{
1502 vlib_pci_addr_t addr, **addrv = arg;
1503 unformat_input_t input;
1504 clib_error_t *err = 0;
1505
1506 unformat_init_string (&input, (char *) dev_dir_name,
1507 vec_len (dev_dir_name));
1508
1509 if (!unformat (&input, "/sys/bus/pci/devices/%U",
1510 unformat_vlib_pci_addr, &addr))
1511 err = clib_error_return (0, "unformat error `%v`", dev_dir_name);
1512
1513 unformat_free (&input);
1514
1515 if (err)
1516 return err;
1517
1518 vec_add1 (*addrv, addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001519 return 0;
1520}
1521
Damjan Marioncef87f12017-10-05 15:32:41 +02001522static int
1523pci_addr_cmp (void *v1, void *v2)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001524{
Damjan Marioncef87f12017-10-05 15:32:41 +02001525 vlib_pci_addr_t *a1 = v1;
1526 vlib_pci_addr_t *a2 = v2;
1527
1528 if (a1->domain > a2->domain)
1529 return 1;
1530 if (a1->domain < a2->domain)
1531 return -1;
1532 if (a1->bus > a2->bus)
1533 return 1;
1534 if (a1->bus < a2->bus)
1535 return -1;
1536 if (a1->slot > a2->slot)
1537 return 1;
1538 if (a1->slot < a2->slot)
1539 return -1;
1540 if (a1->function > a2->function)
1541 return 1;
1542 if (a1->function < a2->function)
1543 return -1;
1544 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001545}
1546
Damjan Marioncef87f12017-10-05 15:32:41 +02001547vlib_pci_addr_t *
1548vlib_pci_get_all_dev_addrs ()
Ed Warnickecb9cada2015-12-08 15:45:58 -07001549{
Damjan Marioncef87f12017-10-05 15:32:41 +02001550 vlib_pci_addr_t *addrs = 0;
1551 clib_error_t *err;
1552 err = foreach_directory_file ((char *) sysfs_pci_dev_path, scan_pci_addr,
1553 &addrs, /* scan_dirs */ 0);
1554 if (err)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001555 {
Damjan Marioncef87f12017-10-05 15:32:41 +02001556 vec_free (addrs);
1557 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001558 }
1559
Damjan Marioncef87f12017-10-05 15:32:41 +02001560 vec_sort_with_function (addrs, pci_addr_cmp);
Damjan Mariona42cd342016-04-13 18:03:20 +02001561
Damjan Marioncef87f12017-10-05 15:32:41 +02001562 return addrs;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001563}
1564
Dave Barach9b8ffd92016-07-08 08:13:45 -04001565clib_error_t *
1566linux_pci_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001567{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001568 vlib_pci_main_t *pm = &pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +02001569 vlib_pci_addr_t *addr = 0, *addrs;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001570
1571 pm->vlib_main = vm;
1572
Dave Barach9b8ffd92016-07-08 08:13:45 -04001573 ASSERT (sizeof (vlib_pci_addr_t) == sizeof (u32));
Damjan Mariona42cd342016-04-13 18:03:20 +02001574
Damjan Marioncef87f12017-10-05 15:32:41 +02001575 addrs = vlib_pci_get_all_dev_addrs ();
1576 /* *INDENT-OFF* */
1577 vec_foreach (addr, addrs)
1578 {
1579 vlib_pci_device_info_t *d;
Damjan Marion23227982018-10-22 13:38:57 +02001580 if ((d = vlib_pci_get_device_info (vm, addr, 0)))
Damjan Marioncef87f12017-10-05 15:32:41 +02001581 {
Damjan Marion23227982018-10-22 13:38:57 +02001582 init_device_from_registered (vm, d);
Damjan Marioncef87f12017-10-05 15:32:41 +02001583 vlib_pci_free_device_info (d);
1584 }
1585 }
1586 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001587
Dave Barachf8d50682019-05-14 18:01:44 -04001588 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001589}
1590
Dave Barachf8d50682019-05-14 18:01:44 -04001591/* *INDENT-OFF* */
1592VLIB_INIT_FUNCTION (linux_pci_init) =
1593{
1594 .runs_after = VLIB_INITS("unix_input_init"),
1595};
1596/* *INDENT-ON* */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001597
1598/*
1599 * fd.io coding-style-patch-verification: ON
1600 *
1601 * Local Variables:
1602 * eval: (c-set-style "gnu")
1603 * End:
1604 */