blob: f7c63bc3607f12503b8835c51217e12650f5239a [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>
Damjan Marion4b661402024-02-29 16:14:27 +010041#include <vppinfra/bitmap.h>
42#include <vppinfra/unix.h>
Damjan Marion01914ce2017-09-14 19:04:50 +020043
Ed Warnickecb9cada2015-12-08 15:45:58 -070044#include <vlib/vlib.h>
45#include <vlib/pci/pci.h>
46#include <vlib/unix/unix.h>
Damjan Marion1ba0fa42018-03-04 17:19:08 +010047#include <vlib/linux/vfio.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070048
49#include <sys/types.h>
50#include <sys/stat.h>
51#include <fcntl.h>
52#include <dirent.h>
Damjan Mariona42cd342016-04-13 18:03:20 +020053#include <sys/ioctl.h>
54#include <net/if.h>
55#include <linux/ethtool.h>
56#include <linux/sockios.h>
Damjan Marioncef87f12017-10-05 15:32:41 +020057#include <linux/vfio.h>
Guillaume Solignac52bd5372023-04-20 18:02:59 +020058#include <limits.h>
Damjan Marioncef87f12017-10-05 15:32:41 +020059#include <sys/eventfd.h>
60
Ray Kinsella5714a492021-11-02 13:33:44 +000061#define SYSFS_DEVICES_PCI "/sys/devices/pci"
Damjan Marioncef87f12017-10-05 15:32:41 +020062static const char *sysfs_pci_dev_path = "/sys/bus/pci/devices";
63static const char *sysfs_pci_drv_path = "/sys/bus/pci/drivers";
Damjan Marion2752b892017-12-11 15:55:56 +010064static char *sysfs_mod_vfio_noiommu =
65 "/sys/module/vfio/parameters/enable_unsafe_noiommu_mode";
Damjan Mariona42cd342016-04-13 18:03:20 +020066
Damjan Marion00ea98a2023-07-28 13:19:49 +020067VLIB_REGISTER_LOG_CLASS (pci_log, static) = {
68 .class_name = "pci",
69 .subclass_name = "linux",
70};
71
72#define log_debug(p, f, ...) \
73 vlib_log (VLIB_LOG_LEVEL_DEBUG, pci_log.class, "%U: " f, \
74 format_vlib_pci_log, p->handle, ##__VA_ARGS__)
75#define log_err(p, f, ...) \
76 vlib_log (VLIB_LOG_LEVEL_ERR, pci_log.class, "%U: " f, format_vlib_pci_log, \
77 p->handle, ##__VA_ARGS__)
Damjan Marion23227982018-10-22 13:38:57 +020078
Dave Barach9b8ffd92016-07-08 08:13:45 -040079typedef struct
80{
Damjan Marion2060db82018-03-04 17:37:15 +010081 int fd;
82 void *addr;
83 size_t size;
84} linux_pci_region_t;
85
Damjan Marion599a16b2018-03-04 19:35:23 +010086typedef struct
87{
88 int fd;
89 u32 clib_file_index;
90 union
91 {
Damjan Mariond5ded2d2018-03-05 10:18:50 +010092 pci_intx_handler_function_t *intx_handler;
Damjan Marion599a16b2018-03-04 19:35:23 +010093 pci_msix_handler_function_t *msix_handler;
94 };
95} linux_pci_irq_t;
Damjan Marion2060db82018-03-04 17:37:15 +010096
97typedef enum
98{
99 LINUX_PCI_DEVICE_TYPE_UNKNOWN,
100 LINUX_PCI_DEVICE_TYPE_UIO,
101 LINUX_PCI_DEVICE_TYPE_VFIO,
102} linux_pci_device_type_t;
103
104typedef struct
105{
106 linux_pci_device_type_t type;
Damjan Marioncef87f12017-10-05 15:32:41 +0200107 vlib_pci_dev_handle_t handle;
108 vlib_pci_addr_t addr;
Damjan Mariond2bfb782019-01-07 20:56:04 +0100109 u32 numa_node;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200110
111 /* Resource file descriptors. */
Damjan Marion2060db82018-03-04 17:37:15 +0100112 linux_pci_region_t *regions;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200113
114 /* File descriptor for config space read/write. */
115 int config_fd;
Damjan Marionf9a968e2018-02-25 23:26:22 +0100116 u64 config_offset;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200117
Damjan Marionf9a968e2018-02-25 23:26:22 +0100118 /* Device File descriptor */
119 int fd;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200120
Mohsin Kazmi95276ca2018-09-28 11:45:01 +0200121 /* read/write file descriptor for io bar */
122 int io_fd;
123 u64 io_offset;
124
Damjan Marion5a206ea2016-05-12 22:11:03 +0200125 /* Minor device for uio device. */
126 u32 uio_minor;
127
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100128 /* Interrupt handlers */
129 linux_pci_irq_t intx_irq;
Damjan Marion599a16b2018-03-04 19:35:23 +0100130 linux_pci_irq_t *msix_irqs;
Damjan Marioncef87f12017-10-05 15:32:41 +0200131
132 /* private data */
133 uword private_data;
134
Damjan Marion23227982018-10-22 13:38:57 +0200135 u8 supports_va_dma;
136
Damjan Marion5a206ea2016-05-12 22:11:03 +0200137} linux_pci_device_t;
138
139/* Pool of PCI devices. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400140typedef struct
141{
142 vlib_main_t *vlib_main;
143 linux_pci_device_t *linux_pci_devices;
Damjan Marioncef87f12017-10-05 15:32:41 +0200144
Damjan Marion5a206ea2016-05-12 22:11:03 +0200145} linux_pci_main_t;
146
147extern linux_pci_main_t linux_pci_main;
148
Damjan Marion599a16b2018-03-04 19:35:23 +0100149static linux_pci_device_t *
Damjan Marioncef87f12017-10-05 15:32:41 +0200150linux_pci_get_device (vlib_pci_dev_handle_t h)
151{
152 linux_pci_main_t *lpm = &linux_pci_main;
153 return pool_elt_at_index (lpm->linux_pci_devices, h);
154}
155
156uword
Damjan Marion23227982018-10-22 13:38:57 +0200157vlib_pci_get_private_data (vlib_main_t * vm, vlib_pci_dev_handle_t h)
Damjan Marioncef87f12017-10-05 15:32:41 +0200158{
159 linux_pci_device_t *d = linux_pci_get_device (h);
160 return d->private_data;
161}
162
163void
Damjan Marion23227982018-10-22 13:38:57 +0200164vlib_pci_set_private_data (vlib_main_t * vm, vlib_pci_dev_handle_t h,
165 uword private_data)
Damjan Marioncef87f12017-10-05 15:32:41 +0200166{
167 linux_pci_device_t *d = linux_pci_get_device (h);
168 d->private_data = private_data;
169}
170
171vlib_pci_addr_t *
Damjan Marion23227982018-10-22 13:38:57 +0200172vlib_pci_get_addr (vlib_main_t * vm, vlib_pci_dev_handle_t h)
Damjan Marioncef87f12017-10-05 15:32:41 +0200173{
Damjan Marion599a16b2018-03-04 19:35:23 +0100174 linux_pci_device_t *d = linux_pci_get_device (h);
175 return &d->addr;
Damjan Marioncef87f12017-10-05 15:32:41 +0200176}
177
Damjan Mariond2bfb782019-01-07 20:56:04 +0100178u32
179vlib_pci_get_numa_node (vlib_main_t * vm, vlib_pci_dev_handle_t h)
180{
181 linux_pci_device_t *d = linux_pci_get_device (h);
182 return d->numa_node;
183}
184
Mohsin Kazmia402b832019-01-25 15:57:24 +0000185u32
186vlib_pci_get_num_msix_interrupts (vlib_main_t * vm, vlib_pci_dev_handle_t h)
187{
188 linux_pci_device_t *d = linux_pci_get_device (h);
189
190 if (d->type == LINUX_PCI_DEVICE_TYPE_VFIO)
191 {
192 struct vfio_irq_info ii = { 0 };
193
194 ii.argsz = sizeof (struct vfio_irq_info);
195 ii.index = VFIO_PCI_MSIX_IRQ_INDEX;
196 if (ioctl (d->fd, VFIO_DEVICE_GET_IRQ_INFO, &ii) < 0)
197 return 0;
198 return ii.count;
199 }
200 return 0;
201}
202
Damjan Marion5a206ea2016-05-12 22:11:03 +0200203/* Call to allocate/initialize the pci subsystem.
204 This is not an init function so that users can explicitly enable
205 pci only when it's needed. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400206clib_error_t *pci_bus_init (vlib_main_t * vm);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200207
Ed Warnickecb9cada2015-12-08 15:45:58 -0700208linux_pci_main_t linux_pci_main;
209
Damjan Marioncef87f12017-10-05 15:32:41 +0200210vlib_pci_device_info_t *
Damjan Marion23227982018-10-22 13:38:57 +0200211vlib_pci_get_device_info (vlib_main_t * vm, vlib_pci_addr_t * addr,
212 clib_error_t ** error)
Damjan Marioncef87f12017-10-05 15:32:41 +0200213{
Damjan Marioncef87f12017-10-05 15:32:41 +0200214 clib_error_t *err;
215 vlib_pci_device_info_t *di;
216 u8 *f = 0;
217 u32 tmp;
218 int fd;
Yulong Pei45495482019-10-17 18:41:52 +0800219 u8 *tmpstr;
Jieqiang Wang76c61592020-01-13 17:15:13 +0800220 clib_bitmap_t *bmp = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200221
222 di = clib_mem_alloc (sizeof (vlib_pci_device_info_t));
Dave Barachb7b92992018-10-17 10:38:51 -0400223 clib_memset (di, 0, sizeof (vlib_pci_device_info_t));
Damjan Marioncef87f12017-10-05 15:32:41 +0200224 di->addr.as_u32 = addr->as_u32;
225
226 u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
227 format_vlib_pci_addr, addr);
228
229 f = format (0, "%v/config%c", dev_dir_name, 0);
230 fd = open ((char *) f, O_RDWR);
231
232 /* Try read-only access if write fails. */
233 if (fd < 0)
234 fd = open ((char *) f, O_RDONLY);
235
236 if (fd < 0)
237 {
238 err = clib_error_return_unix (0, "open `%s'", f);
239 goto error;
240 }
241
242 /* You can only read more that 64 bytes of config space as root; so we try to
243 read the full space but fall back to just the first 64 bytes. */
Damjan Marionc9275da2023-10-12 17:41:14 +0000244 if (read (fd, &di->config, sizeof (di->config)) <
245 sizeof (vlib_pci_config_hdr_t))
Damjan Marioncef87f12017-10-05 15:32:41 +0200246 {
247 err = clib_error_return_unix (0, "read `%s'", f);
248 close (fd);
249 goto error;
250 }
251
Damjan Marioncef87f12017-10-05 15:32:41 +0200252 di->numa_node = -1;
253 vec_reset_length (f);
254 f = format (f, "%v/numa_node%c", dev_dir_name, 0);
Jieqiang Wang76c61592020-01-13 17:15:13 +0800255 err = clib_sysfs_read ((char *) f, "%d", &di->numa_node);
Damjan Marioncef87f12017-10-05 15:32:41 +0200256 if (err)
257 {
258 di->numa_node = -1;
259 clib_error_free (err);
260 }
Jieqiang Wang76c61592020-01-13 17:15:13 +0800261 if (di->numa_node == -1)
262 {
Damjan Marion4b661402024-02-29 16:14:27 +0100263 bmp = os_get_online_cpu_node_bitmap ();
Jieqiang Wang76c61592020-01-13 17:15:13 +0800264 if (clib_bitmap_count_set_bits (bmp) == 1)
265 di->numa_node = 0;
266 }
Damjan Marioncef87f12017-10-05 15:32:41 +0200267
268 vec_reset_length (f);
269 f = format (f, "%v/class%c", dev_dir_name, 0);
270 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
271 if (err)
272 goto error;
273 di->device_class = tmp >> 8;
274
275 vec_reset_length (f);
276 f = format (f, "%v/vendor%c", dev_dir_name, 0);
277 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
278 if (err)
279 goto error;
280 di->vendor_id = tmp;
281
282 vec_reset_length (f);
283 f = format (f, "%v/device%c", dev_dir_name, 0);
284 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
285 if (err)
286 goto error;
287 di->device_id = tmp;
288
289 vec_reset_length (f);
Damjan Marion00ea98a2023-07-28 13:19:49 +0200290 f = format (f, "%v/revision%c", dev_dir_name, 0);
291 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
292 if (err)
293 goto error;
294 di->revision = tmp;
295
Damjan Marion40f48102023-08-07 01:07:09 +0200296 di->driver_name =
297 clib_file_get_resolved_basename ("%v/driver", dev_dir_name);
Benoît Ganne0eae2bb2019-10-04 17:30:21 +0200298 if (!di->driver_name)
299 di->driver_name = format (0, "<NONE>%c", 0);
Damjan Marioncef87f12017-10-05 15:32:41 +0200300
301 di->iommu_group = -1;
Damjan Marion40f48102023-08-07 01:07:09 +0200302 tmpstr = clib_file_get_resolved_basename ("%v/iommu_group", dev_dir_name);
Yulong Pei45495482019-10-17 18:41:52 +0800303 if (tmpstr)
Damjan Marioncef87f12017-10-05 15:32:41 +0200304 {
Yulong Pei45495482019-10-17 18:41:52 +0800305 di->iommu_group = atoi ((char *) tmpstr);
306 vec_free (tmpstr);
Damjan Marioncef87f12017-10-05 15:32:41 +0200307 }
308
Yulong Pei45495482019-10-17 18:41:52 +0800309 vec_reset_length (f);
310 f = format (f, "%v/iommu_group/name%c", dev_dir_name, 0);
311 err = clib_sysfs_read ((char *) f, "%s", &tmpstr);
312 if (err == 0)
313 {
314 if (strncmp ((char *) tmpstr, "vfio-noiommu", 12) == 0)
315 di->flags |= VLIB_PCI_DEVICE_INFO_F_NOIOMMU;
316 vec_free (tmpstr);
317 }
318 else
319 clib_error_free (err);
320
Damjan Marion96504182017-12-10 23:54:46 +0100321 close (fd);
322
Damjan Marioncef87f12017-10-05 15:32:41 +0200323 vec_reset_length (f);
324 f = format (f, "%v/vpd%c", dev_dir_name, 0);
325 fd = open ((char *) f, O_RDONLY);
326 if (fd >= 0)
327 {
328 while (1)
329 {
330 u8 tag[3];
331 u8 *data = 0;
Damjan Marion96504182017-12-10 23:54:46 +0100332 uword len;
Damjan Marioncef87f12017-10-05 15:32:41 +0200333
334 if (read (fd, &tag, 3) != 3)
335 break;
336
337 if (tag[0] != 0x82 && tag[0] != 0x90 && tag[0] != 0x91)
338 break;
339
340 len = (tag[2] << 8) | tag[1];
Ray Kinsella03d0fbe2021-11-03 09:13:41 +0000341 vec_validate (data, len - 1);
Damjan Marioncef87f12017-10-05 15:32:41 +0200342
343 if (read (fd, data, len) != len)
344 {
345 vec_free (data);
346 break;
347 }
348 if (tag[0] == 0x82)
349 di->product_name = data;
350 else if (tag[0] == 0x90)
351 di->vpd_r = data;
352 else if (tag[0] == 0x91)
353 di->vpd_w = data;
354
355 data = 0;
356 }
357 close (fd);
358 }
359
360 goto done;
361
362error:
363 vlib_pci_free_device_info (di);
364 di = 0;
365
366done:
Jieqiang Wang76c61592020-01-13 17:15:13 +0800367 vec_free (bmp);
Damjan Marioncef87f12017-10-05 15:32:41 +0200368 vec_free (f);
369 vec_free (dev_dir_name);
370 if (error)
371 *error = err;
372 else
373 clib_error_free (err);
374 return di;
375}
376
Ray Kinsella5714a492021-11-02 13:33:44 +0000377clib_error_t *__attribute__ ((weak))
378vlib_pci_get_device_root_bus (vlib_pci_addr_t *addr, vlib_pci_addr_t *root_bus)
379{
380 u8 *rel_path = 0, *abs_path = 0, *link_path = 0;
381 unformat_input_t input;
Ray Kinsella81865bc2021-11-08 07:22:49 +0000382 int fd = open (sysfs_pci_dev_path, O_RDONLY);
Klement Sekerabd469072021-11-18 12:32:05 +0100383 ssize_t size = 0;
Ray Kinsella5714a492021-11-02 13:33:44 +0000384 u32 domain = 0, bus;
385 clib_error_t *err = NULL;
386
387 if (fd < 0)
388 return clib_error_return_unix (0, "failed to open %s", sysfs_pci_dev_path);
389
390 vec_alloc (rel_path, PATH_MAX);
391 vec_alloc (abs_path, PATH_MAX);
392
393 link_path =
394 format (0, "%s/%U", sysfs_pci_dev_path, format_vlib_pci_addr, addr);
395 size = readlinkat (fd, (char *) link_path, (char *) rel_path, PATH_MAX);
396 if (size < 0)
397 {
398 err = clib_error_return_unix (0, "failed to read %s", rel_path);
399 goto done;
400 }
401
402 rel_path[size] = '\0';
403 vec_free (link_path);
404
405 link_path = format (0, "%s/%s", sysfs_pci_dev_path, rel_path);
406 if (!realpath ((char *) link_path, (char *) abs_path))
407 {
408 err = clib_error_return_unix (0, "failed to resolve %s", link_path);
409 goto done;
410 }
411
412 unformat_init_string (&input, (char *) abs_path,
413 clib_strnlen ((char *) abs_path, PATH_MAX));
414
415 if (!unformat (&input, SYSFS_DEVICES_PCI "%x:%x/%s", &domain, &bus,
416 link_path))
417 {
418 err = clib_error_return (0, "unknown input '%U'", format_unformat_error,
419 input);
420 goto done;
421 }
422
423 root_bus->domain = domain;
424 root_bus->bus = bus;
425
426done:
427 vec_free (abs_path);
428 vec_free (link_path);
429 vec_free (rel_path);
430 close (fd);
431
432 return err;
433}
434
Damjan Marion2752b892017-12-11 15:55:56 +0100435static int
436directory_exists (char *path)
437{
438 struct stat s = { 0 };
439 if (stat (path, &s) == -1)
440 return 0;
441
442 return S_ISDIR (s.st_mode);
443}
444
Damjan Mariona42cd342016-04-13 18:03:20 +0200445clib_error_t *
Benoît Ganne6a073482022-10-13 17:22:26 +0200446vlib_pci_bind_to_uio (vlib_main_t *vm, vlib_pci_addr_t *addr,
447 char *uio_drv_name, int force)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700448{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400449 clib_error_t *error = 0;
Damjan Mariona7f74572017-05-23 18:32:21 +0200450 u8 *s = 0, *driver_name = 0;
Damjan Mariona42cd342016-04-13 18:03:20 +0200451 DIR *dir = 0;
452 struct dirent *e;
Damjan Marioncef87f12017-10-05 15:32:41 +0200453 vlib_pci_device_info_t *di;
Damjan Mariona7f74572017-05-23 18:32:21 +0200454 int fd, clear_driver_override = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200455 u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
456 format_vlib_pci_addr, addr);
457
Damjan Marion23227982018-10-22 13:38:57 +0200458 di = vlib_pci_get_device_info (vm, addr, &error);
Damjan Marioncef87f12017-10-05 15:32:41 +0200459
460 if (error)
461 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700462
Damjan Marion2752b892017-12-11 15:55:56 +0100463 if (strncmp ("auto", uio_drv_name, 5) == 0)
464 {
465 int vfio_pci_loaded = 0;
466
467 if (directory_exists ("/sys/module/vfio_pci"))
468 vfio_pci_loaded = 1;
469
470 if (di->iommu_group != -1)
471 {
472 /* device is bound to IOMMU group */
473 if (!vfio_pci_loaded)
474 {
475 error = clib_error_return (0, "Skipping PCI device %U: device "
476 "is bound to IOMMU group and "
477 "vfio-pci driver is not loaded",
478 format_vlib_pci_addr, addr);
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100479 goto err0;
Damjan Marion2752b892017-12-11 15:55:56 +0100480 }
481 else
482 uio_drv_name = "vfio-pci";
483 }
484 else
485 {
486 /* device is not bound to IOMMU group so we have multiple options */
487 if (vfio_pci_loaded &&
488 (error = clib_sysfs_write (sysfs_mod_vfio_noiommu, "Y")) == 0)
489 uio_drv_name = "vfio-pci";
490 else if (directory_exists ("/sys/module/uio_pci_generic"))
491 uio_drv_name = "uio_pci_generic";
492 else if (directory_exists ("/sys/module/igb_uio"))
493 uio_drv_name = "igb_uio";
494 else
495 {
496 clib_error_free (error);
497 error = clib_error_return (0, "Skipping PCI device %U: missing "
498 "kernel VFIO or UIO driver",
499 format_vlib_pci_addr, addr);
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100500 goto err0;
Damjan Marion2752b892017-12-11 15:55:56 +0100501 }
502 clib_error_free (error);
503 }
504 }
505
Damjan Marion40f48102023-08-07 01:07:09 +0200506 driver_name = clib_file_get_resolved_basename ("%v/driver", dev_dir_name);
Damjan Mariona42cd342016-04-13 18:03:20 +0200507
Damjan Mariona7f74572017-05-23 18:32:21 +0200508 if (driver_name &&
509 ((strcmp ("vfio-pci", (char *) driver_name) == 0) ||
510 (strcmp ("uio_pci_generic", (char *) driver_name) == 0) ||
511 (strcmp ("igb_uio", (char *) driver_name) == 0)))
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100512 goto err0;
Damjan Marion3c785e02017-05-08 18:37:54 +0200513
Benoît Ganne6a073482022-10-13 17:22:26 +0200514 if (!force)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700515 {
Benoît Ganne6a073482022-10-13 17:22:26 +0200516 /* walk trough all linux interfaces and if interface belonging to
517 this device is found check if interface is admin up */
518 dir = opendir ("/sys/class/net");
519 s = format (s, "%U%c", format_vlib_pci_addr, addr, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700520
Benoît Ganne6a073482022-10-13 17:22:26 +0200521 if (!dir)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400522 {
Benoît Ganne6a073482022-10-13 17:22:26 +0200523 error = clib_error_return (0,
524 "Skipping PCI device %U: failed to "
525 "read /sys/class/net",
526 format_vlib_pci_addr, addr);
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100527 goto err0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400528 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200529
Benoît Ganne6a073482022-10-13 17:22:26 +0200530 fd = socket (PF_INET, SOCK_DGRAM, 0);
531 if (fd < 0)
Damjan Mariona42cd342016-04-13 18:03:20 +0200532 {
Benoît Ganne6a073482022-10-13 17:22:26 +0200533 error = clib_error_return_unix (0, "socket");
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100534 goto err1;
Damjan Mariona42cd342016-04-13 18:03:20 +0200535 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700536
Benoît Ganne6a073482022-10-13 17:22:26 +0200537 while ((e = readdir (dir)))
538 {
539 struct ifreq ifr;
540 struct ethtool_drvinfo drvinfo;
541
542 if (e->d_name[0] == '.') /* skip . and .. */
543 continue;
544
545 clib_memset (&ifr, 0, sizeof ifr);
546 clib_memset (&drvinfo, 0, sizeof drvinfo);
547 ifr.ifr_data = (char *) &drvinfo;
548 clib_strncpy (ifr.ifr_name, e->d_name, sizeof (ifr.ifr_name) - 1);
549
550 drvinfo.cmd = ETHTOOL_GDRVINFO;
551 if (ioctl (fd, SIOCETHTOOL, &ifr) < 0)
552 {
553 /* Some interfaces (eg "lo") don't support this ioctl */
554 if ((errno != ENOTSUP) && (errno != ENODEV))
555 clib_unix_warning ("ioctl fetch intf %s bus info error",
556 e->d_name);
557 continue;
558 }
559
560 if (strcmp ((char *) s, drvinfo.bus_info))
561 continue;
562
563 clib_memset (&ifr, 0, sizeof (ifr));
564 clib_strncpy (ifr.ifr_name, e->d_name, sizeof (ifr.ifr_name) - 1);
565
566 if (ioctl (fd, SIOCGIFFLAGS, &ifr) < 0)
567 {
568 error = clib_error_return_unix (0, "ioctl fetch intf %s flags",
569 e->d_name);
570 close (fd);
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100571 goto err1;
Benoît Ganne6a073482022-10-13 17:22:26 +0200572 }
573
574 if (ifr.ifr_flags & IFF_UP)
575 {
576 vlib_log (VLIB_LOG_LEVEL_WARNING, pci_main.log_default,
577 "Skipping PCI device %U as host "
578 "interface %s is up",
579 format_vlib_pci_addr, addr, e->d_name);
580 close (fd);
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100581 goto err1;
Benoît Ganne6a073482022-10-13 17:22:26 +0200582 }
583 }
584
585 close (fd);
586 vec_reset_length (s);
587 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200588
589 s = format (s, "%v/driver/unbind%c", dev_dir_name, 0);
Damjan Marioncef87f12017-10-05 15:32:41 +0200590 clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
Damjan Mariona42cd342016-04-13 18:03:20 +0200591 vec_reset_length (s);
592
Damjan Mariona7f74572017-05-23 18:32:21 +0200593 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
594 if (access ((char *) s, F_OK) == 0)
595 {
Damjan Marioncef87f12017-10-05 15:32:41 +0200596 clib_sysfs_write ((char *) s, "%s", uio_drv_name);
Damjan Mariona7f74572017-05-23 18:32:21 +0200597 clear_driver_override = 1;
598 }
599 else
600 {
601 vec_reset_length (s);
Damjan Marioncef87f12017-10-05 15:32:41 +0200602 s = format (s, "%s/%s/new_id%c", sysfs_pci_drv_path, uio_drv_name, 0);
603 clib_sysfs_write ((char *) s, "0x%04x 0x%04x", di->vendor_id,
604 di->device_id);
Damjan Mariona7f74572017-05-23 18:32:21 +0200605 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200606 vec_reset_length (s);
607
Damjan Marioncef87f12017-10-05 15:32:41 +0200608 s = format (s, "%s/%s/bind%c", sysfs_pci_drv_path, uio_drv_name, 0);
609 clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
Damjan Mariona7f74572017-05-23 18:32:21 +0200610 vec_reset_length (s);
611
612 if (clear_driver_override)
613 {
614 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
Damjan Marion01914ce2017-09-14 19:04:50 +0200615 clib_sysfs_write ((char *) s, "%c", 0);
Damjan Mariona7f74572017-05-23 18:32:21 +0200616 vec_reset_length (s);
617 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200618
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100619err1:
Damjan Mariona42cd342016-04-13 18:03:20 +0200620 closedir (dir);
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100621err0:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700622 vec_free (s);
Damjan Mariona42cd342016-04-13 18:03:20 +0200623 vec_free (dev_dir_name);
Damjan Mariona7f74572017-05-23 18:32:21 +0200624 vec_free (driver_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700625 return error;
626}
627
Ed Warnickecb9cada2015-12-08 15:45:58 -0700628
629static clib_error_t *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400630scan_uio_dir (void *arg, u8 * path_name, u8 * file_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700631{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400632 linux_pci_device_t *l = arg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700633 unformat_input_t input;
634
635 unformat_init_string (&input, (char *) file_name, vec_len (file_name));
636
Dave Barach9b8ffd92016-07-08 08:13:45 -0400637 if (!unformat (&input, "uio%d", &l->uio_minor))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700638 abort ();
639
640 unformat_free (&input);
641 return 0;
642}
643
Dave Barach9b8ffd92016-07-08 08:13:45 -0400644static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200645vfio_set_irqs (vlib_main_t * vm, linux_pci_device_t * p, u32 index, u32 start,
646 u32 count, u32 flags, int *efds)
Damjan Marion599a16b2018-03-04 19:35:23 +0100647{
648 int data_len = efds ? count * sizeof (int) : 0;
649 u8 buf[sizeof (struct vfio_irq_set) + data_len];
Damjan Marion599a16b2018-03-04 19:35:23 +0100650 struct vfio_irq_set *irq_set = (struct vfio_irq_set *) buf;
651
Damjan Marion599a16b2018-03-04 19:35:23 +0100652 if (efds)
653 {
Damjan Marion7444fd22023-11-06 00:07:15 +0000654 int *data = (int *) irq_set->data;
Damjan Marion599a16b2018-03-04 19:35:23 +0100655 flags |= VFIO_IRQ_SET_DATA_EVENTFD;
Damjan Marion7444fd22023-11-06 00:07:15 +0000656 for (u32 i = 0; i < count; i++)
657 data[i] = efds[i];
Damjan Marion599a16b2018-03-04 19:35:23 +0100658 }
659 else
660 flags |= VFIO_IRQ_SET_DATA_NONE;
661
662 ASSERT ((flags & (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD)) !=
663 (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD));
664
665 irq_set->argsz = sizeof (struct vfio_irq_set) + data_len;
666 irq_set->index = index;
667 irq_set->start = start;
668 irq_set->count = count;
669 irq_set->flags = flags;
670
671 if (ioctl (p->fd, VFIO_DEVICE_SET_IRQS, irq_set) < 0)
Damjan Marion7444fd22023-11-06 00:07:15 +0000672 return clib_error_return_unix (0, "%U:ioctl(VFIO_DEVICE_SET_IRQS)\n%U",
Damjan Marion599a16b2018-03-04 19:35:23 +0100673 format_vlib_pci_addr, &p->addr,
Damjan Marion7444fd22023-11-06 00:07:15 +0000674 format_vfio_irq_set, irq_set);
675
676 log_debug (p, "%s:\n%U", __func__, format_vfio_irq_set, irq_set);
Damjan Marion599a16b2018-03-04 19:35:23 +0100677 return 0;
678}
679
680static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +0200681linux_pci_uio_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700682{
Damjan Marion23227982018-10-22 13:38:57 +0200683 vlib_main_t *vm = vlib_get_main ();
Damjan Marion5a206ea2016-05-12 22:11:03 +0200684 int __attribute__ ((unused)) rv;
Damjan Marioncef87f12017-10-05 15:32:41 +0200685 vlib_pci_dev_handle_t h = uf->private_data;
686 linux_pci_device_t *p = linux_pci_get_device (h);
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100687 linux_pci_irq_t *irq = &p->intx_irq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700688
Damjan Marion5a206ea2016-05-12 22:11:03 +0200689 u32 icount;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400690 rv = read (uf->file_descriptor, &icount, 4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700691
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100692 if (irq->intx_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200693 irq->intx_handler (vm, h);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200694
Damjan Marion23227982018-10-22 13:38:57 +0200695 vlib_pci_intr_enable (vm, h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700696
697 return /* no error */ 0;
698}
699
Dave Barach9b8ffd92016-07-08 08:13:45 -0400700static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200701linux_pci_vfio_unmask_intx (vlib_main_t * vm, linux_pci_device_t * d)
Damjan Marioncef87f12017-10-05 15:32:41 +0200702{
Damjan Marion23227982018-10-22 13:38:57 +0200703 return vfio_set_irqs (vm, d, VFIO_PCI_INTX_IRQ_INDEX, 0, 1,
Damjan Marion599a16b2018-03-04 19:35:23 +0100704 VFIO_IRQ_SET_ACTION_UNMASK, 0);
705}
706
Damjan Marioncef87f12017-10-05 15:32:41 +0200707static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +0200708linux_pci_uio_error_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700709{
710 u32 error_index = (u32) uf->private_data;
711
712 return clib_error_return (0, "pci device %d: error", error_index);
713}
714
Damjan Marioncef87f12017-10-05 15:32:41 +0200715static clib_error_t *
Damjan Marion599a16b2018-03-04 19:35:23 +0100716linux_pci_vfio_msix_read_ready (clib_file_t * uf)
717{
Damjan Marion23227982018-10-22 13:38:57 +0200718 vlib_main_t *vm = vlib_get_main ();
Damjan Marion599a16b2018-03-04 19:35:23 +0100719 int __attribute__ ((unused)) rv;
720 vlib_pci_dev_handle_t h = uf->private_data >> 16;
721 u16 line = uf->private_data & 0xffff;
722 linux_pci_device_t *p = linux_pci_get_device (h);
723 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, line);
724
725 u64 icount;
726 rv = read (uf->file_descriptor, &icount, sizeof (icount));
727
728 if (irq->msix_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200729 irq->msix_handler (vm, h, line);
Damjan Marion599a16b2018-03-04 19:35:23 +0100730
731 return /* no error */ 0;
732}
733
734static clib_error_t *
735linux_pci_vfio_intx_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700736{
Damjan Marion23227982018-10-22 13:38:57 +0200737 vlib_main_t *vm = vlib_get_main ();
Damjan Marioncef87f12017-10-05 15:32:41 +0200738 int __attribute__ ((unused)) rv;
739 vlib_pci_dev_handle_t h = uf->private_data;
740 linux_pci_device_t *p = linux_pci_get_device (h);
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100741 linux_pci_irq_t *irq = &p->intx_irq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700742
Damjan Marioncef87f12017-10-05 15:32:41 +0200743 u64 icount;
744 rv = read (uf->file_descriptor, &icount, sizeof (icount));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700745
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100746 if (irq->intx_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200747 irq->intx_handler (vm, h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700748
Damjan Marion23227982018-10-22 13:38:57 +0200749 linux_pci_vfio_unmask_intx (vm, p);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700750
Damjan Marioncef87f12017-10-05 15:32:41 +0200751 return /* no error */ 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700752}
753
Damjan Marioncef87f12017-10-05 15:32:41 +0200754static clib_error_t *
755linux_pci_vfio_error_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700756{
Damjan Marioncef87f12017-10-05 15:32:41 +0200757 u32 error_index = (u32) uf->private_data;
758
759 return clib_error_return (0, "pci device %d: error", error_index);
760}
761
762static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200763add_device_uio (vlib_main_t * vm, linux_pci_device_t * p,
764 vlib_pci_device_info_t * di, pci_device_registration_t * r)
Damjan Marioncef87f12017-10-05 15:32:41 +0200765{
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100766 linux_pci_main_t *lpm = &linux_pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +0200767 clib_error_t *err = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200768 u8 *s = 0;
769
Damjan Marionf9a968e2018-02-25 23:26:22 +0100770 p->fd = -1;
Damjan Marion2060db82018-03-04 17:37:15 +0100771 p->type = LINUX_PCI_DEVICE_TYPE_UIO;
Damjan Marioncef87f12017-10-05 15:32:41 +0200772
773 s = format (s, "%s/%U/config%c", sysfs_pci_dev_path,
774 format_vlib_pci_addr, &di->addr, 0);
775
776 p->config_fd = open ((char *) s, O_RDWR);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100777 p->config_offset = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200778 vec_reset_length (s);
779
780 if (p->config_fd == -1)
781 {
782 err = clib_error_return_unix (0, "open '%s'", s);
783 goto error;
784 }
785
Benoît Ganne0b91bd62019-11-20 09:07:50 +0100786 s = format (0, "%s/%U/uio%c", sysfs_pci_dev_path,
787 format_vlib_pci_addr, &di->addr, 0);
Damjan Marioncef87f12017-10-05 15:32:41 +0200788 foreach_directory_file ((char *) s, scan_uio_dir, p, /* scan_dirs */
789 1);
790 vec_reset_length (s);
791
792 s = format (s, "/dev/uio%d%c", p->uio_minor, 0);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100793 p->fd = open ((char *) s, O_RDWR);
794 if (p->fd < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +0200795 {
796 err = clib_error_return_unix (0, "open '%s'", s);
797 goto error;
798 }
799
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100800 if (r && r->interrupt_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200801 vlib_pci_register_intx_handler (vm, p->handle, r->interrupt_handler);
Damjan Marioncef87f12017-10-05 15:32:41 +0200802
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100803 if (r && r->init_function)
804 err = r->init_function (lpm->vlib_main, p->handle);
Damjan Marioncef87f12017-10-05 15:32:41 +0200805
806error:
Damjan Marion20ba1642018-03-26 15:35:33 +0200807 vec_free (s);
Damjan Marioncef87f12017-10-05 15:32:41 +0200808 if (err)
809 {
Damjan Marion96504182017-12-10 23:54:46 +0100810 if (p->config_fd != -1)
811 close (p->config_fd);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100812 if (p->fd != -1)
813 close (p->fd);
Damjan Marioncef87f12017-10-05 15:32:41 +0200814 }
815 return err;
816}
817
Damjan Marion599a16b2018-03-04 19:35:23 +0100818clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200819vlib_pci_register_intx_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h,
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100820 pci_intx_handler_function_t * intx_handler)
821{
822 linux_pci_device_t *p = linux_pci_get_device (h);
823 clib_file_t t = { 0 };
824 linux_pci_irq_t *irq = &p->intx_irq;
825 ASSERT (irq->fd == -1);
826
827 if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
828 {
Damjan Marion23227982018-10-22 13:38:57 +0200829 struct vfio_irq_info ii = { 0 };
830 ii.argsz = sizeof (struct vfio_irq_info);
831 ii.index = VFIO_PCI_INTX_IRQ_INDEX;
832 if (ioctl (p->fd, VFIO_DEVICE_GET_IRQ_INFO, &ii) < 0)
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100833 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) '"
834 "%U'", format_vlib_pci_addr, &p->addr);
Damjan Marion00ea98a2023-07-28 13:19:49 +0200835 log_debug (
836 p, "%s index:%u count:%u flags: %s%s%s%s(0x%x)", __func__, ii.index,
837 ii.count, ii.flags & VFIO_IRQ_INFO_EVENTFD ? "eventfd " : "",
838 ii.flags & VFIO_IRQ_INFO_MASKABLE ? "maskable " : "",
839 ii.flags & VFIO_IRQ_INFO_AUTOMASKED ? "automasked " : "",
840 ii.flags & VFIO_IRQ_INFO_NORESIZE ? "noresize " : "", ii.flags);
Damjan Marion23227982018-10-22 13:38:57 +0200841 if (ii.count != 1)
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100842 return clib_error_return (0, "INTx interrupt does not exist on device"
843 "'%U'", format_vlib_pci_addr, &p->addr);
844
845 irq->fd = eventfd (0, EFD_NONBLOCK);
846 if (irq->fd == -1)
847 return clib_error_return_unix (0, "eventfd");
Damjan Marion23227982018-10-22 13:38:57 +0200848
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100849 t.file_descriptor = irq->fd;
850 t.read_function = linux_pci_vfio_intx_read_ready;
851 }
852 else if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
853 {
854 t.file_descriptor = p->fd;
855 t.read_function = linux_pci_uio_read_ready;
856 }
857 else
858 return 0;
859
860 t.error_function = linux_pci_uio_error_ready;
861 t.private_data = p->handle;
862 t.description = format (0, "PCI %U INTx", format_vlib_pci_addr, &p->addr);
863 irq->clib_file_index = clib_file_add (&file_main, &t);
864 irq->intx_handler = intx_handler;
865 return 0;
866}
867
868clib_error_t *
Damjan Marion38c61912023-10-17 16:06:26 +0000869vlib_pci_unregister_intx_handler (vlib_main_t *vm, vlib_pci_dev_handle_t h)
870{
871 linux_pci_device_t *p = linux_pci_get_device (h);
872 linux_pci_irq_t *irq = &p->intx_irq;
873
874 if (irq->intx_handler == 0)
875 return 0;
876
877 clib_file_del_by_index (&file_main, irq->clib_file_index);
878 if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
879 {
880 close (irq->fd);
881 irq->fd = -1;
882 }
883
884 irq->intx_handler = 0;
885
886 return 0;
887}
888
889clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200890vlib_pci_register_msix_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h,
891 u32 start, u32 count,
Damjan Marion599a16b2018-03-04 19:35:23 +0100892 pci_msix_handler_function_t * msix_handler)
893{
894 clib_error_t *err = 0;
895 linux_pci_device_t *p = linux_pci_get_device (h);
896 u32 i;
897
898 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
899 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
900 "support");
901
Damjan Marion599a16b2018-03-04 19:35:23 +0100902 vec_validate_init_empty (p->msix_irqs, start + count - 1, (linux_pci_irq_t)
903 { .fd = -1});
Damjan Marion599a16b2018-03-04 19:35:23 +0100904
905 for (i = start; i < start + count; i++)
906 {
907 clib_file_t t = { 0 };
908 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
909 ASSERT (irq->fd == -1);
910
911 irq->fd = eventfd (0, EFD_NONBLOCK);
912 if (irq->fd == -1)
913 {
914 err = clib_error_return_unix (0, "eventfd");
915 goto error;
916 }
917
918 t.read_function = linux_pci_vfio_msix_read_ready;
919 t.file_descriptor = irq->fd;
920 t.error_function = linux_pci_vfio_error_ready;
921 t.private_data = p->handle << 16 | i;
922 t.description = format (0, "PCI %U MSI-X #%u", format_vlib_pci_addr,
923 &p->addr, i);
924 irq->clib_file_index = clib_file_add (&file_main, &t);
925 irq->msix_handler = msix_handler;
926 }
927
928 return 0;
929
930error:
931 while (i-- > start)
932 {
933 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
934 if (irq->fd != -1)
935 {
936 clib_file_del_by_index (&file_main, irq->clib_file_index);
937 close (irq->fd);
938 irq->fd = -1;
939 }
940 }
941 return err;
942}
943
944clib_error_t *
Damjan Marion38c61912023-10-17 16:06:26 +0000945vlib_pci_unregister_msix_handler (vlib_main_t *vm, vlib_pci_dev_handle_t h,
946 u32 start, u32 count)
947{
948 clib_error_t *err = 0;
949 linux_pci_device_t *p = linux_pci_get_device (h);
950 u32 i;
951
952 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
953 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
954 "support");
955
956 for (i = start; i < start + count; i++)
957 {
958 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
959
960 if (irq->fd != -1)
961 {
962 clib_file_del_by_index (&file_main, irq->clib_file_index);
963 close (irq->fd);
964 irq->fd = -1;
965 }
966 }
967
968 return err;
969}
970
971clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200972vlib_pci_enable_msix_irq (vlib_main_t * vm, vlib_pci_dev_handle_t h,
973 u16 start, u16 count)
Damjan Marion599a16b2018-03-04 19:35:23 +0100974{
975 linux_pci_device_t *p = linux_pci_get_device (h);
976 int fds[count];
977 int i;
978
979 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
980 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
981 "support");
982
Damjan Marion60529a82023-11-06 00:06:26 +0000983 for (i = 0; i < count; i++)
Damjan Marion599a16b2018-03-04 19:35:23 +0100984 {
Damjan Marion60529a82023-11-06 00:06:26 +0000985 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, start + i);
Damjan Marion599a16b2018-03-04 19:35:23 +0100986 fds[i] = irq->fd;
987 }
988
Damjan Marion23227982018-10-22 13:38:57 +0200989 return vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
Damjan Marion599a16b2018-03-04 19:35:23 +0100990 VFIO_IRQ_SET_ACTION_TRIGGER, fds);
991}
992
Damjan Marion9c9490c2020-10-07 20:00:39 +0200993uword
994vlib_pci_get_msix_file_index (vlib_main_t * vm, vlib_pci_dev_handle_t h,
995 u16 index)
996{
997 linux_pci_device_t *p = linux_pci_get_device (h);
998 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, index);
999 if (irq->fd == -1)
1000 return ~0;
1001 return irq->clib_file_index;
1002}
1003
Damjan Marion599a16b2018-03-04 19:35:23 +01001004clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001005vlib_pci_disable_msix_irq (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1006 u16 start, u16 count)
Damjan Marion599a16b2018-03-04 19:35:23 +01001007{
1008 linux_pci_device_t *p = linux_pci_get_device (h);
1009 int i, fds[count];
1010
1011 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
1012 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
1013 "support");
1014
1015 for (i = start; i < start + count; i++)
1016 fds[i] = -1;
1017
Damjan Marion23227982018-10-22 13:38:57 +02001018 return vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
Damjan Marion599a16b2018-03-04 19:35:23 +01001019 VFIO_IRQ_SET_ACTION_TRIGGER, fds);
1020}
1021
Damjan Marioncef87f12017-10-05 15:32:41 +02001022static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001023add_device_vfio (vlib_main_t * vm, linux_pci_device_t * p,
1024 vlib_pci_device_info_t * di, pci_device_registration_t * r)
Damjan Marioncef87f12017-10-05 15:32:41 +02001025{
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001026 linux_pci_main_t *lpm = &linux_pci_main;
Damjan Marionf9a968e2018-02-25 23:26:22 +01001027 struct vfio_device_info device_info = { 0 };
Damjan Marionf313b742018-03-05 14:08:33 +01001028 struct vfio_region_info reg = { 0 };
Damjan Marioncef87f12017-10-05 15:32:41 +02001029 clib_error_t *err = 0;
1030 u8 *s = 0;
Damjan Marion23227982018-10-22 13:38:57 +02001031 int is_noiommu;
Damjan Marioncef87f12017-10-05 15:32:41 +02001032
Damjan Marion2060db82018-03-04 17:37:15 +01001033 p->type = LINUX_PCI_DEVICE_TYPE_VFIO;
Damjan Marioncef87f12017-10-05 15:32:41 +02001034
Damjan Marion23227982018-10-22 13:38:57 +02001035 if ((err = linux_vfio_group_get_device_fd (&p->addr, &p->fd, &is_noiommu)))
Damjan Marioncef87f12017-10-05 15:32:41 +02001036 return err;
1037
Damjan Marion23227982018-10-22 13:38:57 +02001038 if (is_noiommu == 0)
1039 p->supports_va_dma = 1;
1040
Damjan Marioncef87f12017-10-05 15:32:41 +02001041 device_info.argsz = sizeof (device_info);
Damjan Marionf9a968e2018-02-25 23:26:22 +01001042 if (ioctl (p->fd, VFIO_DEVICE_GET_INFO, &device_info) < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +02001043 {
1044 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
1045 format_vlib_pci_addr, &di->addr);
1046 goto error;
1047 }
1048
Damjan Marionf313b742018-03-05 14:08:33 +01001049 reg.argsz = sizeof (struct vfio_region_info);
1050 reg.index = VFIO_PCI_CONFIG_REGION_INDEX;
1051 if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, &reg) < 0)
1052 {
1053 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
1054 format_vlib_pci_addr, &di->addr);
1055 goto error;
1056 }
Damjan Marion23227982018-10-22 13:38:57 +02001057
Damjan Marion00ea98a2023-07-28 13:19:49 +02001058 log_debug (p, "%s %U", __func__, format_vfio_region_info, &reg);
Damjan Marion23227982018-10-22 13:38:57 +02001059
Damjan Marionf313b742018-03-05 14:08:33 +01001060 p->config_offset = reg.offset;
1061 p->config_fd = p->fd;
1062
Damjan Marioncef87f12017-10-05 15:32:41 +02001063 /* reset if device supports it */
1064 if (device_info.flags & VFIO_DEVICE_FLAGS_RESET)
Damjan Marionf9a968e2018-02-25 23:26:22 +01001065 if (ioctl (p->fd, VFIO_DEVICE_RESET) < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +02001066 {
1067 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_RESET) '%U'",
1068 format_vlib_pci_addr, &di->addr);
1069 goto error;
1070 }
1071
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001072 if (r && r->interrupt_handler)
Damjan Marioncef87f12017-10-05 15:32:41 +02001073 {
Damjan Marion23227982018-10-22 13:38:57 +02001074 vlib_pci_register_intx_handler (vm, p->handle, r->interrupt_handler);
1075 linux_pci_vfio_unmask_intx (vm, p);
Damjan Marioncef87f12017-10-05 15:32:41 +02001076 }
1077
Damjan Marion68b4da62018-09-30 18:26:20 +02001078 if (p->supports_va_dma)
1079 {
1080 vlib_buffer_pool_t *bp;
Damjan Mariond50e3472019-01-20 00:03:56 +01001081 vec_foreach (bp, vm->buffer_main->buffer_pools)
Damjan Marion68b4da62018-09-30 18:26:20 +02001082 {
1083 u32 i;
1084 vlib_physmem_map_t *pm;
1085 pm = vlib_physmem_get_map (vm, bp->physmem_map_index);
1086 for (i = 0; i < pm->n_pages; i++)
1087 vfio_map_physmem_page (vm, pm->base + (i << pm->log2_page_size));
1088 }
Damjan Marion68b4da62018-09-30 18:26:20 +02001089 }
1090
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001091 if (r && r->init_function)
1092 err = r->init_function (lpm->vlib_main, p->handle);
Damjan Marioncef87f12017-10-05 15:32:41 +02001093
1094error:
1095 vec_free (s);
1096 if (err)
1097 {
Damjan Marionf9a968e2018-02-25 23:26:22 +01001098 if (p->fd != -1)
1099 close (p->fd);
Chris Luke30684ac2018-03-29 12:56:58 -07001100 if (p->config_fd != -1 && p->config_fd != p->fd)
Damjan Marion96504182017-12-10 23:54:46 +01001101 close (p->config_fd);
Chris Luke30684ac2018-03-29 12:56:58 -07001102 p->config_fd = p->fd = -1;
Damjan Marioncef87f12017-10-05 15:32:41 +02001103 }
1104 return err;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001105}
1106
1107/* Configuration space read/write. */
1108clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001109vlib_pci_read_write_config (vlib_main_t * vm, vlib_pci_dev_handle_t h,
Damjan Marion5a206ea2016-05-12 22:11:03 +02001110 vlib_read_or_write_t read_or_write,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001111 uword address, void *data, u32 n_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001112{
Damjan Marioncef87f12017-10-05 15:32:41 +02001113 linux_pci_device_t *p = linux_pci_get_device (h);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001114 int n;
1115
Ed Warnickecb9cada2015-12-08 15:45:58 -07001116 if (read_or_write == VLIB_READ)
Damjan Marionf9a968e2018-02-25 23:26:22 +01001117 n = pread (p->config_fd, data, n_bytes, p->config_offset + address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001118 else
Damjan Marionf9a968e2018-02-25 23:26:22 +01001119 n = pwrite (p->config_fd, data, n_bytes, p->config_offset + address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001120
1121 if (n != n_bytes)
1122 return clib_error_return_unix (0, "%s",
1123 read_or_write == VLIB_READ
1124 ? "read" : "write");
1125
1126 return 0;
1127}
1128
1129static clib_error_t *
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001130vlib_pci_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 bar, int *fd,
1131 u64 * size, u64 * offset)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001132{
Damjan Marioncef87f12017-10-05 15:32:41 +02001133 linux_pci_device_t *p = linux_pci_get_device (h);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001134 clib_error_t *error = 0;
1135 int _fd = -1;
1136 u64 _size = 0, _offset = 0;
Damjan Marion2060db82018-03-04 17:37:15 +01001137
1138 ASSERT (bar <= 5);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001139
1140 error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001141
Damjan Marion2060db82018-03-04 17:37:15 +01001142 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001143 {
Damjan Marion2060db82018-03-04 17:37:15 +01001144 u8 *file_name;
1145 struct stat stat_buf;
1146 file_name = format (0, "%s/%U/resource%d%c", sysfs_pci_dev_path,
1147 format_vlib_pci_addr, &p->addr, bar, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001148
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001149 _fd = open ((char *) file_name, O_RDWR);
1150 if (_fd < 0)
Damjan Marion2060db82018-03-04 17:37:15 +01001151 {
1152 error = clib_error_return_unix (0, "open `%s'", file_name);
1153 vec_free (file_name);
1154 return error;
1155 }
1156
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001157 if (fstat (_fd, &stat_buf) < 0)
Damjan Marion2060db82018-03-04 17:37:15 +01001158 {
1159 error = clib_error_return_unix (0, "fstat `%s'", file_name);
1160 vec_free (file_name);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001161 close (_fd);
Damjan Marion2060db82018-03-04 17:37:15 +01001162 return error;
1163 }
1164
1165 vec_free (file_name);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001166 _size = stat_buf.st_size;
1167 _offset = 0;
Damjan Marion2060db82018-03-04 17:37:15 +01001168 }
1169 else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001170 {
Damjan Marion0792bb42020-05-25 19:31:42 +02001171 struct vfio_region_info *r;
1172 u32 sz = sizeof (struct vfio_region_info);
1173 again:
1174 r = clib_mem_alloc (sz);
1175 clib_memset (r, 0, sz);
1176 r->argsz = sz;
1177 r->index = bar;
1178 if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, r) < 0)
Damjan Marion2060db82018-03-04 17:37:15 +01001179 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) "
1180 "'%U'", format_vlib_pci_addr,
1181 &p->addr);
Damjan Marion0792bb42020-05-25 19:31:42 +02001182 if (sz != r->argsz)
1183 {
1184 sz = r->argsz;
1185 clib_mem_free (r);
1186 goto again;
1187 }
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001188 _fd = p->fd;
Damjan Marion0792bb42020-05-25 19:31:42 +02001189 _size = r->size;
1190 _offset = r->offset;
Damjan Marion00ea98a2023-07-28 13:19:49 +02001191 log_debug (p, "%s %U", __func__, format_vfio_region_info, r);
Damjan Marion0792bb42020-05-25 19:31:42 +02001192 clib_mem_free (r);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001193 }
Damjan Marion2060db82018-03-04 17:37:15 +01001194 else
1195 ASSERT (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001196
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001197 *fd = _fd;
1198 *size = _size;
1199 *offset = _offset;
1200
1201 return error;
1202}
1203
1204static clib_error_t *
1205vlib_pci_map_region_int (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1206 u32 bar, u8 * addr, void **result)
1207{
1208 linux_pci_device_t *p = linux_pci_get_device (h);
1209 int fd = -1;
1210 clib_error_t *error;
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001211 u64 size = 0, offset = 0;
Damjan Marion00ea98a2023-07-28 13:19:49 +02001212 vlib_pci_config_reg_command_t command;
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001213
Damjan Marion00ea98a2023-07-28 13:19:49 +02001214 log_debug (p, "map region %u to va %p", bar, addr);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001215
Damjan Marion00ea98a2023-07-28 13:19:49 +02001216 if ((error = vlib_pci_read_config_u16 (vm, h, 4, &command.as_u16)))
Mohammed Hawari70fc36f2020-10-21 16:41:30 +02001217 return error;
1218
Damjan Marion00ea98a2023-07-28 13:19:49 +02001219 if (!(command.mem_space))
Mohammed Hawari70fc36f2020-10-21 16:41:30 +02001220 {
Damjan Marion00ea98a2023-07-28 13:19:49 +02001221 log_debug (p, "setting memory enable bit");
1222 command.mem_space = 1;
1223 if ((error = vlib_pci_write_config_u16 (vm, h, 4, &command.as_u16)))
Mohammed Hawari70fc36f2020-10-21 16:41:30 +02001224 return error;
1225 }
1226
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001227 if ((error = vlib_pci_region (vm, h, bar, &fd, &size, &offset)))
1228 return error;
1229
Damjan Marion6bfd0762020-09-11 22:16:53 +02001230 *result = clib_mem_vm_map_shared (addr, size, fd, offset,
1231 "PCIe %U region %u", format_vlib_pci_addr,
1232 vlib_pci_get_addr (vm, h), bar);
1233 if (*result == CLIB_MEM_VM_MAP_FAILED)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001234 {
Damjan Marion2060db82018-03-04 17:37:15 +01001235 error = clib_error_return_unix (0, "mmap `BAR%u'", bar);
Dave Barachc72950e2020-05-04 14:47:45 -04001236 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO && (fd != -1))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001237 close (fd);
Damjan Marion2060db82018-03-04 17:37:15 +01001238 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001239 }
Damjan Marion2060db82018-03-04 17:37:15 +01001240
Damjan Marion2060db82018-03-04 17:37:15 +01001241 vec_validate_init_empty (p->regions, bar,
1242 (linux_pci_region_t) { .fd = -1});
Damjan Marion2060db82018-03-04 17:37:15 +01001243 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1244 p->regions[bar].fd = fd;
1245 p->regions[bar].addr = *result;
1246 p->regions[bar].size = size;
1247 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001248}
1249
1250clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001251vlib_pci_map_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 resource,
1252 void **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001253{
Damjan Marion23227982018-10-22 13:38:57 +02001254 return (vlib_pci_map_region_int (vm, h, resource, 0 /* addr */ , result));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001255}
1256
1257clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001258vlib_pci_map_region_fixed (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1259 u32 resource, u8 * addr, 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, addr, result));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001262}
1263
Damjan Marionf313b742018-03-05 14:08:33 +01001264clib_error_t *
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001265vlib_pci_io_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 resource)
1266{
1267 linux_pci_device_t *p = linux_pci_get_device (h);
1268 clib_error_t *error = 0;
1269 int fd = -1;
1270 u64 size = 0, offset = 0;
1271
1272 if ((error = vlib_pci_region (vm, h, resource, &fd, &size, &offset)))
1273 return error;
1274
1275 p->io_fd = fd;
1276 p->io_offset = offset;
1277 return error;
1278}
1279
1280clib_error_t *
1281vlib_pci_read_write_io (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1282 vlib_read_or_write_t read_or_write,
1283 uword offset, void *data, u32 length)
1284{
1285 linux_pci_device_t *p = linux_pci_get_device (h);
1286 int n = 0;
1287
1288 if (read_or_write == VLIB_READ)
1289 n = pread (p->io_fd, data, length, p->io_offset + offset);
1290 else
1291 n = pwrite (p->io_fd, data, length, p->io_offset + offset);
1292
1293 if (n != length)
1294 return clib_error_return_unix (0, "%s",
1295 read_or_write == VLIB_READ
1296 ? "read" : "write");
1297 return 0;
1298}
1299
1300clib_error_t *
Damjan Marion68b4da62018-09-30 18:26:20 +02001301vlib_pci_map_dma (vlib_main_t * vm, vlib_pci_dev_handle_t h, void *ptr)
1302{
1303 linux_pci_device_t *p = linux_pci_get_device (h);
1304
1305 if (!p->supports_va_dma)
1306 return 0;
1307
1308 return vfio_map_physmem_page (vm, ptr);
1309}
1310
1311int
1312vlib_pci_supports_virtual_addr_dma (vlib_main_t * vm, vlib_pci_dev_handle_t h)
1313{
1314 linux_pci_device_t *p = linux_pci_get_device (h);
1315
1316 return p->supports_va_dma != 0;
1317}
1318
1319clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001320vlib_pci_device_open (vlib_main_t * vm, vlib_pci_addr_t * addr,
Damjan Marionf313b742018-03-05 14:08:33 +01001321 pci_device_id_t ids[], vlib_pci_dev_handle_t * handle)
1322{
1323 linux_pci_main_t *lpm = &linux_pci_main;
1324 vlib_pci_device_info_t *di;
1325 linux_pci_device_t *p;
1326 clib_error_t *err = 0;
1327 pci_device_id_t *i;
1328
Damjan Marion23227982018-10-22 13:38:57 +02001329 di = vlib_pci_get_device_info (vm, addr, &err);
Damjan Marionf313b742018-03-05 14:08:33 +01001330
1331 if (err)
1332 return err;
Damjan Marionf313b742018-03-05 14:08:33 +01001333
Damjan Marion00ea98a2023-07-28 13:19:49 +02001334 if (ids)
1335 {
1336 for (i = ids; i->vendor_id != 0; i++)
1337 if (i->vendor_id == di->vendor_id && i->device_id == di->device_id)
1338 break;
1339
1340 if (i->vendor_id == 0)
1341 {
1342 vlib_pci_free_device_info (di);
1343 return clib_error_return (0, "Wrong vendor or device id");
1344 }
1345 }
Damjan Marionf313b742018-03-05 14:08:33 +01001346
1347 pool_get (lpm->linux_pci_devices, p);
1348 p->handle = p - lpm->linux_pci_devices;
Damjan Marion23227982018-10-22 13:38:57 +02001349 p->addr.as_u32 = di->addr.as_u32;
Damjan Marionf313b742018-03-05 14:08:33 +01001350 p->intx_irq.fd = -1;
Mohsin Kazmi692f9b12019-04-02 11:09:49 +00001351 p->intx_irq.clib_file_index = -1;
Damjan Mariond2bfb782019-01-07 20:56:04 +01001352 p->numa_node = di->numa_node;
Mohsin Kazmi3d3b9552018-10-24 14:05:34 +02001353 /*
1354 * pci io bar read/write fd
1355 */
1356 p->io_fd = -1;
Damjan Marionf313b742018-03-05 14:08:33 +01001357
Damjan Marion00ea98a2023-07-28 13:19:49 +02001358 log_debug (p, "open vid:0x%04x did:0x%04x driver:%s iommu_group:%d",
1359 di->vendor_id, di->device_id, di->driver_name, di->iommu_group);
Damjan Marion23227982018-10-22 13:38:57 +02001360
Steven Luongab489922019-08-21 11:35:27 -07001361 if (clib_strncmp ("vfio-pci", (char *) di->driver_name, 8) == 0)
Damjan Marion23227982018-10-22 13:38:57 +02001362 err = add_device_vfio (vm, p, di, 0);
Steven Luongab489922019-08-21 11:35:27 -07001363 else if (clib_strncmp ("uio_pci_generic", (char *) di->driver_name, 8) == 0)
Damjan Marion23227982018-10-22 13:38:57 +02001364 err = add_device_uio (vm, p, di, 0);
Damjan Marionf313b742018-03-05 14:08:33 +01001365 else
Damjan Marion23227982018-10-22 13:38:57 +02001366 err = clib_error_create ("device not bound to 'vfio-pci' or "
1367 "'uio_pci_generic' kernel module");
Damjan Marionf313b742018-03-05 14:08:33 +01001368 if (err)
1369 goto error;
1370
1371 *handle = p->handle;
1372
1373error:
1374 vlib_pci_free_device_info (di);
1375 if (err)
1376 {
Damjan Marion00ea98a2023-07-28 13:19:49 +02001377 log_err (p, "%U", format_clib_error, err);
Dave Barachb7b92992018-10-17 10:38:51 -04001378 clib_memset (p, 0, sizeof (linux_pci_device_t));
Damjan Marionf313b742018-03-05 14:08:33 +01001379 pool_put (lpm->linux_pci_devices, p);
1380 }
1381
1382 return err;
1383}
1384
1385void
Damjan Marion23227982018-10-22 13:38:57 +02001386vlib_pci_device_close (vlib_main_t * vm, vlib_pci_dev_handle_t h)
Damjan Marionf313b742018-03-05 14:08:33 +01001387{
1388 linux_pci_main_t *lpm = &linux_pci_main;
1389 linux_pci_device_t *p = linux_pci_get_device (h);
1390 linux_pci_irq_t *irq;
1391 linux_pci_region_t *res;
1392 clib_error_t *err = 0;
1393
1394 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1395 {
1396 irq = &p->intx_irq;
Mohsin Kazmi692f9b12019-04-02 11:09:49 +00001397 if (irq->clib_file_index != -1)
1398 clib_file_del_by_index (&file_main, irq->clib_file_index);
Damjan Marionf313b742018-03-05 14:08:33 +01001399 close (p->config_fd);
Mohsin Kazmi3d3b9552018-10-24 14:05:34 +02001400 if (p->io_fd != -1)
1401 close (p->io_fd);
Damjan Marionf313b742018-03-05 14:08:33 +01001402 }
1403 else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
1404 {
1405 irq = &p->intx_irq;
1406 /* close INTx irqs */
1407 if (irq->fd != -1)
1408 {
Damjan Marion23227982018-10-22 13:38:57 +02001409 err = vfio_set_irqs (vm, p, VFIO_PCI_INTX_IRQ_INDEX, 0, 0,
Damjan Marionf313b742018-03-05 14:08:33 +01001410 VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1411 clib_error_free (err);
Mohsin Kazmi692f9b12019-04-02 11:09:49 +00001412 if (irq->clib_file_index != -1)
1413 clib_file_del_by_index (&file_main, irq->clib_file_index);
Damjan Marionf313b742018-03-05 14:08:33 +01001414 close (irq->fd);
1415 }
1416
1417 /* close MSI-X irqs */
1418 if (vec_len (p->msix_irqs))
1419 {
Damjan Marion23227982018-10-22 13:38:57 +02001420 err = vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, 0, 0,
Damjan Marionf313b742018-03-05 14:08:33 +01001421 VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1422 clib_error_free (err);
Damjan Marionf313b742018-03-05 14:08:33 +01001423 vec_foreach (irq, p->msix_irqs)
1424 {
1425 if (irq->fd == -1)
1426 continue;
1427 clib_file_del_by_index (&file_main, irq->clib_file_index);
1428 close (irq->fd);
1429 }
Damjan Marionf313b742018-03-05 14:08:33 +01001430 vec_free (p->msix_irqs);
1431 }
1432 }
1433
Damjan Marionf313b742018-03-05 14:08:33 +01001434 vec_foreach (res, p->regions)
1435 {
1436 if (res->size == 0)
1437 continue;
Damjan Marion6bfd0762020-09-11 22:16:53 +02001438 clib_mem_vm_unmap (res->addr);
Damjan Marionf313b742018-03-05 14:08:33 +01001439 if (res->fd != -1)
1440 close (res->fd);
1441 }
Damjan Marionf313b742018-03-05 14:08:33 +01001442 vec_free (p->regions);
1443
1444 close (p->fd);
Dave Barachb7b92992018-10-17 10:38:51 -04001445 clib_memset (p, 0, sizeof (linux_pci_device_t));
Damjan Marionf313b742018-03-05 14:08:33 +01001446 pool_put (lpm->linux_pci_devices, p);
1447}
1448
Dave Barach9b8ffd92016-07-08 08:13:45 -04001449void
Damjan Marion23227982018-10-22 13:38:57 +02001450init_device_from_registered (vlib_main_t * vm, vlib_pci_device_info_t * di)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001451{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001452 vlib_pci_main_t *pm = &pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +02001453 linux_pci_main_t *lpm = &linux_pci_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001454 pci_device_registration_t *r;
1455 pci_device_id_t *i;
Damjan Marioncef87f12017-10-05 15:32:41 +02001456 clib_error_t *err = 0;
1457 linux_pci_device_t *p;
1458
1459 pool_get (lpm->linux_pci_devices, p);
1460 p->handle = p - lpm->linux_pci_devices;
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001461 p->intx_irq.fd = -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001462
Damjan Marion5a206ea2016-05-12 22:11:03 +02001463 r = pm->pci_device_registrations;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001464
1465 while (r)
1466 {
1467 for (i = r->supported_devices; i->vendor_id != 0; i++)
Damjan Marion3a593822018-02-17 14:06:53 +01001468 if (i->vendor_id == di->vendor_id && i->device_id == di->device_id)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001469 {
Damjan Marioncef87f12017-10-05 15:32:41 +02001470 if (di->iommu_group != -1)
Damjan Marion23227982018-10-22 13:38:57 +02001471 err = add_device_vfio (vm, p, di, r);
Damjan Marioncef87f12017-10-05 15:32:41 +02001472 else
Damjan Marion23227982018-10-22 13:38:57 +02001473 err = add_device_uio (vm, p, di, r);
Damjan Marion5a206ea2016-05-12 22:11:03 +02001474
Damjan Marioncef87f12017-10-05 15:32:41 +02001475 if (err)
1476 clib_error_report (err);
1477 else
1478 return;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001479 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001480 r = r->next_registration;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001481 }
Damjan Marioncef87f12017-10-05 15:32:41 +02001482
Ed Warnickecb9cada2015-12-08 15:45:58 -07001483 /* No driver, close the PCI config-space FD */
Dave Barachb7b92992018-10-17 10:38:51 -04001484 clib_memset (p, 0, sizeof (linux_pci_device_t));
Damjan Marioncef87f12017-10-05 15:32:41 +02001485 pool_put (lpm->linux_pci_devices, p);
1486}
1487
1488static clib_error_t *
1489scan_pci_addr (void *arg, u8 * dev_dir_name, u8 * ignored)
1490{
1491 vlib_pci_addr_t addr, **addrv = arg;
1492 unformat_input_t input;
1493 clib_error_t *err = 0;
1494
1495 unformat_init_string (&input, (char *) dev_dir_name,
1496 vec_len (dev_dir_name));
1497
1498 if (!unformat (&input, "/sys/bus/pci/devices/%U",
1499 unformat_vlib_pci_addr, &addr))
1500 err = clib_error_return (0, "unformat error `%v`", dev_dir_name);
1501
1502 unformat_free (&input);
1503
1504 if (err)
1505 return err;
1506
1507 vec_add1 (*addrv, addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001508 return 0;
1509}
1510
Damjan Marioncef87f12017-10-05 15:32:41 +02001511static int
1512pci_addr_cmp (void *v1, void *v2)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001513{
Damjan Marioncef87f12017-10-05 15:32:41 +02001514 vlib_pci_addr_t *a1 = v1;
1515 vlib_pci_addr_t *a2 = v2;
1516
1517 if (a1->domain > a2->domain)
1518 return 1;
1519 if (a1->domain < a2->domain)
1520 return -1;
1521 if (a1->bus > a2->bus)
1522 return 1;
1523 if (a1->bus < a2->bus)
1524 return -1;
1525 if (a1->slot > a2->slot)
1526 return 1;
1527 if (a1->slot < a2->slot)
1528 return -1;
1529 if (a1->function > a2->function)
1530 return 1;
1531 if (a1->function < a2->function)
1532 return -1;
1533 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001534}
1535
Damjan Marioncef87f12017-10-05 15:32:41 +02001536vlib_pci_addr_t *
1537vlib_pci_get_all_dev_addrs ()
Ed Warnickecb9cada2015-12-08 15:45:58 -07001538{
Damjan Marioncef87f12017-10-05 15:32:41 +02001539 vlib_pci_addr_t *addrs = 0;
1540 clib_error_t *err;
1541 err = foreach_directory_file ((char *) sysfs_pci_dev_path, scan_pci_addr,
1542 &addrs, /* scan_dirs */ 0);
1543 if (err)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001544 {
Damjan Marioncef87f12017-10-05 15:32:41 +02001545 vec_free (addrs);
1546 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001547 }
1548
Damjan Marioncef87f12017-10-05 15:32:41 +02001549 vec_sort_with_function (addrs, pci_addr_cmp);
Damjan Mariona42cd342016-04-13 18:03:20 +02001550
Damjan Marioncef87f12017-10-05 15:32:41 +02001551 return addrs;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001552}
1553
Dave Barach9b8ffd92016-07-08 08:13:45 -04001554clib_error_t *
1555linux_pci_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001556{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001557 vlib_pci_main_t *pm = &pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +02001558 vlib_pci_addr_t *addr = 0, *addrs;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001559
1560 pm->vlib_main = vm;
1561
Dave Barach9b8ffd92016-07-08 08:13:45 -04001562 ASSERT (sizeof (vlib_pci_addr_t) == sizeof (u32));
Damjan Mariona42cd342016-04-13 18:03:20 +02001563
Damjan Marioncef87f12017-10-05 15:32:41 +02001564 addrs = vlib_pci_get_all_dev_addrs ();
Damjan Marioncef87f12017-10-05 15:32:41 +02001565 vec_foreach (addr, addrs)
1566 {
1567 vlib_pci_device_info_t *d;
Damjan Marion23227982018-10-22 13:38:57 +02001568 if ((d = vlib_pci_get_device_info (vm, addr, 0)))
Damjan Marioncef87f12017-10-05 15:32:41 +02001569 {
Damjan Marion23227982018-10-22 13:38:57 +02001570 init_device_from_registered (vm, d);
Damjan Marioncef87f12017-10-05 15:32:41 +02001571 vlib_pci_free_device_info (d);
1572 }
1573 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001574
Dave Barachf8d50682019-05-14 18:01:44 -04001575 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001576}
1577
Dave Barachf8d50682019-05-14 18:01:44 -04001578VLIB_INIT_FUNCTION (linux_pci_init) =
1579{
1580 .runs_after = VLIB_INITS("unix_input_init"),
1581};
Dave Barach9b8ffd92016-07-08 08:13:45 -04001582
1583/*
1584 * fd.io coding-style-patch-verification: ON
1585 *
1586 * Local Variables:
1587 * eval: (c-set-style "gnu")
1588 * End:
1589 */