blob: 9661827ea0dbec05cf1bec7dc8f8b03e65ed1cb3 [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 Marion00ea98a2023-07-28 13:19:49 +0200241 if (read (fd, &di->config, sizeof (di->config)) < sizeof (di->config))
Damjan Marioncef87f12017-10-05 15:32:41 +0200242 {
243 err = clib_error_return_unix (0, "read `%s'", f);
244 close (fd);
245 goto error;
246 }
247
Damjan Marioncef87f12017-10-05 15:32:41 +0200248 di->numa_node = -1;
249 vec_reset_length (f);
250 f = format (f, "%v/numa_node%c", dev_dir_name, 0);
Jieqiang Wang76c61592020-01-13 17:15:13 +0800251 err = clib_sysfs_read ((char *) f, "%d", &di->numa_node);
Damjan Marioncef87f12017-10-05 15:32:41 +0200252 if (err)
253 {
254 di->numa_node = -1;
255 clib_error_free (err);
256 }
Jieqiang Wang76c61592020-01-13 17:15:13 +0800257 if (di->numa_node == -1)
258 {
259 /* if '/sys/bus/pci/devices/<device id>/numa_node' returns -1 and
260 it is a SMP system, set numa_node to 0. */
261 if ((err = clib_sysfs_read ("/sys/devices/system/node/online", "%U",
262 unformat_bitmap_list, &bmp)))
263 clib_error_free (err);
264 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 Marion23227982018-10-22 13:38:57 +0200650 struct vfio_irq_info ii = { 0 };
Damjan Marion599a16b2018-03-04 19:35:23 +0100651 struct vfio_irq_set *irq_set = (struct vfio_irq_set *) buf;
652
653
Damjan Marion23227982018-10-22 13:38:57 +0200654 ii.argsz = sizeof (struct vfio_irq_info);
655 ii.index = index;
Damjan Marion599a16b2018-03-04 19:35:23 +0100656
Damjan Marion23227982018-10-22 13:38:57 +0200657 if (ioctl (p->fd, VFIO_DEVICE_GET_IRQ_INFO, &ii) < 0)
Damjan Marion599a16b2018-03-04 19:35:23 +0100658 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) "
659 "'%U'", format_vlib_pci_addr, &p->addr);
660
Damjan Marion00ea98a2023-07-28 13:19:49 +0200661 log_debug (p, "%s index:%u count:%u flags: %s%s%s%s(0x%x)", __func__,
662 ii.index, ii.count,
663 ii.flags & VFIO_IRQ_INFO_EVENTFD ? "eventfd " : "",
664 ii.flags & VFIO_IRQ_INFO_MASKABLE ? "maskable " : "",
665 ii.flags & VFIO_IRQ_INFO_AUTOMASKED ? "automasked " : "",
666 ii.flags & VFIO_IRQ_INFO_NORESIZE ? "noresize " : "", ii.flags);
Damjan Marion23227982018-10-22 13:38:57 +0200667
668 if (ii.count < start + count)
Damjan Marion599a16b2018-03-04 19:35:23 +0100669 return clib_error_return_unix (0, "vfio_set_irq: unexistng interrupt on "
670 "'%U'", format_vlib_pci_addr, &p->addr);
671
672
673 if (efds)
674 {
675 flags |= VFIO_IRQ_SET_DATA_EVENTFD;
Dave Barach178cf492018-11-13 16:34:13 -0500676 clib_memcpy_fast (&irq_set->data, efds, data_len);
Damjan Marion599a16b2018-03-04 19:35:23 +0100677 }
678 else
679 flags |= VFIO_IRQ_SET_DATA_NONE;
680
681 ASSERT ((flags & (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD)) !=
682 (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD));
683
684 irq_set->argsz = sizeof (struct vfio_irq_set) + data_len;
685 irq_set->index = index;
686 irq_set->start = start;
687 irq_set->count = count;
688 irq_set->flags = flags;
689
690 if (ioctl (p->fd, VFIO_DEVICE_SET_IRQS, irq_set) < 0)
691 return clib_error_return_unix (0, "%U:ioctl(VFIO_DEVICE_SET_IRQS) "
692 "[index = %u, start = %u, count = %u, "
693 "flags = 0x%x]",
694 format_vlib_pci_addr, &p->addr,
695 index, start, count, flags);
696 return 0;
697}
698
699static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +0200700linux_pci_uio_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700701{
Damjan Marion23227982018-10-22 13:38:57 +0200702 vlib_main_t *vm = vlib_get_main ();
Damjan Marion5a206ea2016-05-12 22:11:03 +0200703 int __attribute__ ((unused)) rv;
Damjan Marioncef87f12017-10-05 15:32:41 +0200704 vlib_pci_dev_handle_t h = uf->private_data;
705 linux_pci_device_t *p = linux_pci_get_device (h);
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100706 linux_pci_irq_t *irq = &p->intx_irq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700707
Damjan Marion5a206ea2016-05-12 22:11:03 +0200708 u32 icount;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400709 rv = read (uf->file_descriptor, &icount, 4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700710
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100711 if (irq->intx_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200712 irq->intx_handler (vm, h);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200713
Damjan Marion23227982018-10-22 13:38:57 +0200714 vlib_pci_intr_enable (vm, h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700715
716 return /* no error */ 0;
717}
718
Dave Barach9b8ffd92016-07-08 08:13:45 -0400719static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200720linux_pci_vfio_unmask_intx (vlib_main_t * vm, linux_pci_device_t * d)
Damjan Marioncef87f12017-10-05 15:32:41 +0200721{
Damjan Marion23227982018-10-22 13:38:57 +0200722 return vfio_set_irqs (vm, d, VFIO_PCI_INTX_IRQ_INDEX, 0, 1,
Damjan Marion599a16b2018-03-04 19:35:23 +0100723 VFIO_IRQ_SET_ACTION_UNMASK, 0);
724}
725
Damjan Marioncef87f12017-10-05 15:32:41 +0200726static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +0200727linux_pci_uio_error_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700728{
729 u32 error_index = (u32) uf->private_data;
730
731 return clib_error_return (0, "pci device %d: error", error_index);
732}
733
Damjan Marioncef87f12017-10-05 15:32:41 +0200734static clib_error_t *
Damjan Marion599a16b2018-03-04 19:35:23 +0100735linux_pci_vfio_msix_read_ready (clib_file_t * uf)
736{
Damjan Marion23227982018-10-22 13:38:57 +0200737 vlib_main_t *vm = vlib_get_main ();
Damjan Marion599a16b2018-03-04 19:35:23 +0100738 int __attribute__ ((unused)) rv;
739 vlib_pci_dev_handle_t h = uf->private_data >> 16;
740 u16 line = uf->private_data & 0xffff;
741 linux_pci_device_t *p = linux_pci_get_device (h);
742 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, line);
743
744 u64 icount;
745 rv = read (uf->file_descriptor, &icount, sizeof (icount));
746
747 if (irq->msix_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200748 irq->msix_handler (vm, h, line);
Damjan Marion599a16b2018-03-04 19:35:23 +0100749
750 return /* no error */ 0;
751}
752
753static clib_error_t *
754linux_pci_vfio_intx_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700755{
Damjan Marion23227982018-10-22 13:38:57 +0200756 vlib_main_t *vm = vlib_get_main ();
Damjan Marioncef87f12017-10-05 15:32:41 +0200757 int __attribute__ ((unused)) rv;
758 vlib_pci_dev_handle_t h = uf->private_data;
759 linux_pci_device_t *p = linux_pci_get_device (h);
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100760 linux_pci_irq_t *irq = &p->intx_irq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700761
Damjan Marioncef87f12017-10-05 15:32:41 +0200762 u64 icount;
763 rv = read (uf->file_descriptor, &icount, sizeof (icount));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700764
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100765 if (irq->intx_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200766 irq->intx_handler (vm, h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700767
Damjan Marion23227982018-10-22 13:38:57 +0200768 linux_pci_vfio_unmask_intx (vm, p);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700769
Damjan Marioncef87f12017-10-05 15:32:41 +0200770 return /* no error */ 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700771}
772
Damjan Marioncef87f12017-10-05 15:32:41 +0200773static clib_error_t *
774linux_pci_vfio_error_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700775{
Damjan Marioncef87f12017-10-05 15:32:41 +0200776 u32 error_index = (u32) uf->private_data;
777
778 return clib_error_return (0, "pci device %d: error", error_index);
779}
780
781static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200782add_device_uio (vlib_main_t * vm, linux_pci_device_t * p,
783 vlib_pci_device_info_t * di, pci_device_registration_t * r)
Damjan Marioncef87f12017-10-05 15:32:41 +0200784{
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100785 linux_pci_main_t *lpm = &linux_pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +0200786 clib_error_t *err = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200787 u8 *s = 0;
788
Damjan Marionf9a968e2018-02-25 23:26:22 +0100789 p->fd = -1;
Damjan Marion2060db82018-03-04 17:37:15 +0100790 p->type = LINUX_PCI_DEVICE_TYPE_UIO;
Damjan Marioncef87f12017-10-05 15:32:41 +0200791
792 s = format (s, "%s/%U/config%c", sysfs_pci_dev_path,
793 format_vlib_pci_addr, &di->addr, 0);
794
795 p->config_fd = open ((char *) s, O_RDWR);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100796 p->config_offset = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200797 vec_reset_length (s);
798
799 if (p->config_fd == -1)
800 {
801 err = clib_error_return_unix (0, "open '%s'", s);
802 goto error;
803 }
804
Benoît Ganne0b91bd62019-11-20 09:07:50 +0100805 s = format (0, "%s/%U/uio%c", sysfs_pci_dev_path,
806 format_vlib_pci_addr, &di->addr, 0);
Damjan Marioncef87f12017-10-05 15:32:41 +0200807 foreach_directory_file ((char *) s, scan_uio_dir, p, /* scan_dirs */
808 1);
809 vec_reset_length (s);
810
811 s = format (s, "/dev/uio%d%c", p->uio_minor, 0);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100812 p->fd = open ((char *) s, O_RDWR);
813 if (p->fd < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +0200814 {
815 err = clib_error_return_unix (0, "open '%s'", s);
816 goto error;
817 }
818
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100819 if (r && r->interrupt_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200820 vlib_pci_register_intx_handler (vm, p->handle, r->interrupt_handler);
Damjan Marioncef87f12017-10-05 15:32:41 +0200821
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100822 if (r && r->init_function)
823 err = r->init_function (lpm->vlib_main, p->handle);
Damjan Marioncef87f12017-10-05 15:32:41 +0200824
825error:
Damjan Marion20ba1642018-03-26 15:35:33 +0200826 vec_free (s);
Damjan Marioncef87f12017-10-05 15:32:41 +0200827 if (err)
828 {
Damjan Marion96504182017-12-10 23:54:46 +0100829 if (p->config_fd != -1)
830 close (p->config_fd);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100831 if (p->fd != -1)
832 close (p->fd);
Damjan Marioncef87f12017-10-05 15:32:41 +0200833 }
834 return err;
835}
836
Damjan Marion599a16b2018-03-04 19:35:23 +0100837clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200838vlib_pci_register_intx_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h,
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100839 pci_intx_handler_function_t * intx_handler)
840{
841 linux_pci_device_t *p = linux_pci_get_device (h);
842 clib_file_t t = { 0 };
843 linux_pci_irq_t *irq = &p->intx_irq;
844 ASSERT (irq->fd == -1);
845
846 if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
847 {
Damjan Marion23227982018-10-22 13:38:57 +0200848 struct vfio_irq_info ii = { 0 };
849 ii.argsz = sizeof (struct vfio_irq_info);
850 ii.index = VFIO_PCI_INTX_IRQ_INDEX;
851 if (ioctl (p->fd, VFIO_DEVICE_GET_IRQ_INFO, &ii) < 0)
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100852 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) '"
853 "%U'", format_vlib_pci_addr, &p->addr);
Damjan Marion00ea98a2023-07-28 13:19:49 +0200854 log_debug (
855 p, "%s index:%u count:%u flags: %s%s%s%s(0x%x)", __func__, ii.index,
856 ii.count, ii.flags & VFIO_IRQ_INFO_EVENTFD ? "eventfd " : "",
857 ii.flags & VFIO_IRQ_INFO_MASKABLE ? "maskable " : "",
858 ii.flags & VFIO_IRQ_INFO_AUTOMASKED ? "automasked " : "",
859 ii.flags & VFIO_IRQ_INFO_NORESIZE ? "noresize " : "", ii.flags);
Damjan Marion23227982018-10-22 13:38:57 +0200860 if (ii.count != 1)
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100861 return clib_error_return (0, "INTx interrupt does not exist on device"
862 "'%U'", format_vlib_pci_addr, &p->addr);
863
864 irq->fd = eventfd (0, EFD_NONBLOCK);
865 if (irq->fd == -1)
866 return clib_error_return_unix (0, "eventfd");
Damjan Marion23227982018-10-22 13:38:57 +0200867
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100868 t.file_descriptor = irq->fd;
869 t.read_function = linux_pci_vfio_intx_read_ready;
870 }
871 else if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
872 {
873 t.file_descriptor = p->fd;
874 t.read_function = linux_pci_uio_read_ready;
875 }
876 else
877 return 0;
878
879 t.error_function = linux_pci_uio_error_ready;
880 t.private_data = p->handle;
881 t.description = format (0, "PCI %U INTx", format_vlib_pci_addr, &p->addr);
882 irq->clib_file_index = clib_file_add (&file_main, &t);
883 irq->intx_handler = intx_handler;
884 return 0;
885}
886
887clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200888vlib_pci_register_msix_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h,
889 u32 start, u32 count,
Damjan Marion599a16b2018-03-04 19:35:23 +0100890 pci_msix_handler_function_t * msix_handler)
891{
892 clib_error_t *err = 0;
893 linux_pci_device_t *p = linux_pci_get_device (h);
894 u32 i;
895
896 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
897 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
898 "support");
899
900 /* *INDENT-OFF* */
901 vec_validate_init_empty (p->msix_irqs, start + count - 1, (linux_pci_irq_t)
902 { .fd = -1});
903 /* *INDENT-ON* */
904
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 Marion23227982018-10-22 13:38:57 +0200945vlib_pci_enable_msix_irq (vlib_main_t * vm, vlib_pci_dev_handle_t h,
946 u16 start, u16 count)
Damjan Marion599a16b2018-03-04 19:35:23 +0100947{
948 linux_pci_device_t *p = linux_pci_get_device (h);
949 int fds[count];
950 int 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 fds[i] = irq->fd;
960 }
961
Damjan Marion23227982018-10-22 13:38:57 +0200962 return vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
Damjan Marion599a16b2018-03-04 19:35:23 +0100963 VFIO_IRQ_SET_ACTION_TRIGGER, fds);
964}
965
Damjan Marion9c9490c2020-10-07 20:00:39 +0200966uword
967vlib_pci_get_msix_file_index (vlib_main_t * vm, vlib_pci_dev_handle_t h,
968 u16 index)
969{
970 linux_pci_device_t *p = linux_pci_get_device (h);
971 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, index);
972 if (irq->fd == -1)
973 return ~0;
974 return irq->clib_file_index;
975}
976
Damjan Marion599a16b2018-03-04 19:35:23 +0100977clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200978vlib_pci_disable_msix_irq (vlib_main_t * vm, vlib_pci_dev_handle_t h,
979 u16 start, u16 count)
Damjan Marion599a16b2018-03-04 19:35:23 +0100980{
981 linux_pci_device_t *p = linux_pci_get_device (h);
982 int i, fds[count];
983
984 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
985 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
986 "support");
987
988 for (i = start; i < start + count; i++)
989 fds[i] = -1;
990
Damjan Marion23227982018-10-22 13:38:57 +0200991 return vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
Damjan Marion599a16b2018-03-04 19:35:23 +0100992 VFIO_IRQ_SET_ACTION_TRIGGER, fds);
993}
994
Damjan Marioncef87f12017-10-05 15:32:41 +0200995static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200996add_device_vfio (vlib_main_t * vm, linux_pci_device_t * p,
997 vlib_pci_device_info_t * di, pci_device_registration_t * r)
Damjan Marioncef87f12017-10-05 15:32:41 +0200998{
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100999 linux_pci_main_t *lpm = &linux_pci_main;
Damjan Marionf9a968e2018-02-25 23:26:22 +01001000 struct vfio_device_info device_info = { 0 };
Damjan Marionf313b742018-03-05 14:08:33 +01001001 struct vfio_region_info reg = { 0 };
Damjan Marioncef87f12017-10-05 15:32:41 +02001002 clib_error_t *err = 0;
1003 u8 *s = 0;
Damjan Marion23227982018-10-22 13:38:57 +02001004 int is_noiommu;
Damjan Marioncef87f12017-10-05 15:32:41 +02001005
Damjan Marion2060db82018-03-04 17:37:15 +01001006 p->type = LINUX_PCI_DEVICE_TYPE_VFIO;
Damjan Marioncef87f12017-10-05 15:32:41 +02001007
Damjan Marion23227982018-10-22 13:38:57 +02001008 if ((err = linux_vfio_group_get_device_fd (&p->addr, &p->fd, &is_noiommu)))
Damjan Marioncef87f12017-10-05 15:32:41 +02001009 return err;
1010
Damjan Marion23227982018-10-22 13:38:57 +02001011 if (is_noiommu == 0)
1012 p->supports_va_dma = 1;
1013
Damjan Marioncef87f12017-10-05 15:32:41 +02001014 device_info.argsz = sizeof (device_info);
Damjan Marionf9a968e2018-02-25 23:26:22 +01001015 if (ioctl (p->fd, VFIO_DEVICE_GET_INFO, &device_info) < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +02001016 {
1017 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
1018 format_vlib_pci_addr, &di->addr);
1019 goto error;
1020 }
1021
Damjan Marionf313b742018-03-05 14:08:33 +01001022 reg.argsz = sizeof (struct vfio_region_info);
1023 reg.index = VFIO_PCI_CONFIG_REGION_INDEX;
1024 if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, &reg) < 0)
1025 {
1026 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
1027 format_vlib_pci_addr, &di->addr);
1028 goto error;
1029 }
Damjan Marion23227982018-10-22 13:38:57 +02001030
Damjan Marion00ea98a2023-07-28 13:19:49 +02001031 log_debug (p, "%s %U", __func__, format_vfio_region_info, &reg);
Damjan Marion23227982018-10-22 13:38:57 +02001032
Damjan Marionf313b742018-03-05 14:08:33 +01001033 p->config_offset = reg.offset;
1034 p->config_fd = p->fd;
1035
Damjan Marioncef87f12017-10-05 15:32:41 +02001036 /* reset if device supports it */
1037 if (device_info.flags & VFIO_DEVICE_FLAGS_RESET)
Damjan Marionf9a968e2018-02-25 23:26:22 +01001038 if (ioctl (p->fd, VFIO_DEVICE_RESET) < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +02001039 {
1040 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_RESET) '%U'",
1041 format_vlib_pci_addr, &di->addr);
1042 goto error;
1043 }
1044
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001045 if (r && r->interrupt_handler)
Damjan Marioncef87f12017-10-05 15:32:41 +02001046 {
Damjan Marion23227982018-10-22 13:38:57 +02001047 vlib_pci_register_intx_handler (vm, p->handle, r->interrupt_handler);
1048 linux_pci_vfio_unmask_intx (vm, p);
Damjan Marioncef87f12017-10-05 15:32:41 +02001049 }
1050
Damjan Marion68b4da62018-09-30 18:26:20 +02001051 if (p->supports_va_dma)
1052 {
1053 vlib_buffer_pool_t *bp;
1054 /* *INDENT-OFF* */
Damjan Mariond50e3472019-01-20 00:03:56 +01001055 vec_foreach (bp, vm->buffer_main->buffer_pools)
Damjan Marion68b4da62018-09-30 18:26:20 +02001056 {
1057 u32 i;
1058 vlib_physmem_map_t *pm;
1059 pm = vlib_physmem_get_map (vm, bp->physmem_map_index);
1060 for (i = 0; i < pm->n_pages; i++)
1061 vfio_map_physmem_page (vm, pm->base + (i << pm->log2_page_size));
1062 }
1063 /* *INDENT-ON* */
1064 }
1065
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001066 if (r && r->init_function)
1067 err = r->init_function (lpm->vlib_main, p->handle);
Damjan Marioncef87f12017-10-05 15:32:41 +02001068
1069error:
1070 vec_free (s);
1071 if (err)
1072 {
Damjan Marionf9a968e2018-02-25 23:26:22 +01001073 if (p->fd != -1)
1074 close (p->fd);
Chris Luke30684ac2018-03-29 12:56:58 -07001075 if (p->config_fd != -1 && p->config_fd != p->fd)
Damjan Marion96504182017-12-10 23:54:46 +01001076 close (p->config_fd);
Chris Luke30684ac2018-03-29 12:56:58 -07001077 p->config_fd = p->fd = -1;
Damjan Marioncef87f12017-10-05 15:32:41 +02001078 }
1079 return err;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001080}
1081
1082/* Configuration space read/write. */
1083clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001084vlib_pci_read_write_config (vlib_main_t * vm, vlib_pci_dev_handle_t h,
Damjan Marion5a206ea2016-05-12 22:11:03 +02001085 vlib_read_or_write_t read_or_write,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001086 uword address, void *data, u32 n_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001087{
Damjan Marioncef87f12017-10-05 15:32:41 +02001088 linux_pci_device_t *p = linux_pci_get_device (h);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001089 int n;
1090
Ed Warnickecb9cada2015-12-08 15:45:58 -07001091 if (read_or_write == VLIB_READ)
Damjan Marionf9a968e2018-02-25 23:26:22 +01001092 n = pread (p->config_fd, data, n_bytes, p->config_offset + address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001093 else
Damjan Marionf9a968e2018-02-25 23:26:22 +01001094 n = pwrite (p->config_fd, data, n_bytes, p->config_offset + address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001095
1096 if (n != n_bytes)
1097 return clib_error_return_unix (0, "%s",
1098 read_or_write == VLIB_READ
1099 ? "read" : "write");
1100
1101 return 0;
1102}
1103
1104static clib_error_t *
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001105vlib_pci_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 bar, int *fd,
1106 u64 * size, u64 * offset)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001107{
Damjan Marioncef87f12017-10-05 15:32:41 +02001108 linux_pci_device_t *p = linux_pci_get_device (h);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001109 clib_error_t *error = 0;
1110 int _fd = -1;
1111 u64 _size = 0, _offset = 0;
Damjan Marion2060db82018-03-04 17:37:15 +01001112
1113 ASSERT (bar <= 5);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001114
1115 error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001116
Damjan Marion2060db82018-03-04 17:37:15 +01001117 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001118 {
Damjan Marion2060db82018-03-04 17:37:15 +01001119 u8 *file_name;
1120 struct stat stat_buf;
1121 file_name = format (0, "%s/%U/resource%d%c", sysfs_pci_dev_path,
1122 format_vlib_pci_addr, &p->addr, bar, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001123
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001124 _fd = open ((char *) file_name, O_RDWR);
1125 if (_fd < 0)
Damjan Marion2060db82018-03-04 17:37:15 +01001126 {
1127 error = clib_error_return_unix (0, "open `%s'", file_name);
1128 vec_free (file_name);
1129 return error;
1130 }
1131
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001132 if (fstat (_fd, &stat_buf) < 0)
Damjan Marion2060db82018-03-04 17:37:15 +01001133 {
1134 error = clib_error_return_unix (0, "fstat `%s'", file_name);
1135 vec_free (file_name);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001136 close (_fd);
Damjan Marion2060db82018-03-04 17:37:15 +01001137 return error;
1138 }
1139
1140 vec_free (file_name);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001141 _size = stat_buf.st_size;
1142 _offset = 0;
Damjan Marion2060db82018-03-04 17:37:15 +01001143 }
1144 else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001145 {
Damjan Marion0792bb42020-05-25 19:31:42 +02001146 struct vfio_region_info *r;
1147 u32 sz = sizeof (struct vfio_region_info);
1148 again:
1149 r = clib_mem_alloc (sz);
1150 clib_memset (r, 0, sz);
1151 r->argsz = sz;
1152 r->index = bar;
1153 if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, r) < 0)
Damjan Marion2060db82018-03-04 17:37:15 +01001154 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) "
1155 "'%U'", format_vlib_pci_addr,
1156 &p->addr);
Damjan Marion0792bb42020-05-25 19:31:42 +02001157 if (sz != r->argsz)
1158 {
1159 sz = r->argsz;
1160 clib_mem_free (r);
1161 goto again;
1162 }
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001163 _fd = p->fd;
Damjan Marion0792bb42020-05-25 19:31:42 +02001164 _size = r->size;
1165 _offset = r->offset;
Damjan Marion00ea98a2023-07-28 13:19:49 +02001166 log_debug (p, "%s %U", __func__, format_vfio_region_info, r);
Damjan Marion0792bb42020-05-25 19:31:42 +02001167 clib_mem_free (r);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001168 }
Damjan Marion2060db82018-03-04 17:37:15 +01001169 else
1170 ASSERT (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001171
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001172 *fd = _fd;
1173 *size = _size;
1174 *offset = _offset;
1175
1176 return error;
1177}
1178
1179static clib_error_t *
1180vlib_pci_map_region_int (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1181 u32 bar, u8 * addr, void **result)
1182{
1183 linux_pci_device_t *p = linux_pci_get_device (h);
1184 int fd = -1;
1185 clib_error_t *error;
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001186 u64 size = 0, offset = 0;
Damjan Marion00ea98a2023-07-28 13:19:49 +02001187 vlib_pci_config_reg_command_t command;
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001188
Damjan Marion00ea98a2023-07-28 13:19:49 +02001189 log_debug (p, "map region %u to va %p", bar, addr);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001190
Damjan Marion00ea98a2023-07-28 13:19:49 +02001191 if ((error = vlib_pci_read_config_u16 (vm, h, 4, &command.as_u16)))
Mohammed Hawari70fc36f2020-10-21 16:41:30 +02001192 return error;
1193
Damjan Marion00ea98a2023-07-28 13:19:49 +02001194 if (!(command.mem_space))
Mohammed Hawari70fc36f2020-10-21 16:41:30 +02001195 {
Damjan Marion00ea98a2023-07-28 13:19:49 +02001196 log_debug (p, "setting memory enable bit");
1197 command.mem_space = 1;
1198 if ((error = vlib_pci_write_config_u16 (vm, h, 4, &command.as_u16)))
Mohammed Hawari70fc36f2020-10-21 16:41:30 +02001199 return error;
1200 }
1201
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001202 if ((error = vlib_pci_region (vm, h, bar, &fd, &size, &offset)))
1203 return error;
1204
Damjan Marion6bfd0762020-09-11 22:16:53 +02001205 *result = clib_mem_vm_map_shared (addr, size, fd, offset,
1206 "PCIe %U region %u", format_vlib_pci_addr,
1207 vlib_pci_get_addr (vm, h), bar);
1208 if (*result == CLIB_MEM_VM_MAP_FAILED)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001209 {
Damjan Marion2060db82018-03-04 17:37:15 +01001210 error = clib_error_return_unix (0, "mmap `BAR%u'", bar);
Dave Barachc72950e2020-05-04 14:47:45 -04001211 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO && (fd != -1))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001212 close (fd);
Damjan Marion2060db82018-03-04 17:37:15 +01001213 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001214 }
Damjan Marion2060db82018-03-04 17:37:15 +01001215
1216 /* *INDENT-OFF* */
1217 vec_validate_init_empty (p->regions, bar,
1218 (linux_pci_region_t) { .fd = -1});
1219 /* *INDENT-ON* */
1220 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1221 p->regions[bar].fd = fd;
1222 p->regions[bar].addr = *result;
1223 p->regions[bar].size = size;
1224 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001225}
1226
1227clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001228vlib_pci_map_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 resource,
1229 void **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001230{
Damjan Marion23227982018-10-22 13:38:57 +02001231 return (vlib_pci_map_region_int (vm, h, resource, 0 /* addr */ , result));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001232}
1233
1234clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001235vlib_pci_map_region_fixed (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1236 u32 resource, u8 * addr, void **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001237{
Damjan Marion23227982018-10-22 13:38:57 +02001238 return (vlib_pci_map_region_int (vm, h, resource, addr, result));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001239}
1240
Damjan Marionf313b742018-03-05 14:08:33 +01001241clib_error_t *
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001242vlib_pci_io_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 resource)
1243{
1244 linux_pci_device_t *p = linux_pci_get_device (h);
1245 clib_error_t *error = 0;
1246 int fd = -1;
1247 u64 size = 0, offset = 0;
1248
1249 if ((error = vlib_pci_region (vm, h, resource, &fd, &size, &offset)))
1250 return error;
1251
1252 p->io_fd = fd;
1253 p->io_offset = offset;
1254 return error;
1255}
1256
1257clib_error_t *
1258vlib_pci_read_write_io (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1259 vlib_read_or_write_t read_or_write,
1260 uword offset, void *data, u32 length)
1261{
1262 linux_pci_device_t *p = linux_pci_get_device (h);
1263 int n = 0;
1264
1265 if (read_or_write == VLIB_READ)
1266 n = pread (p->io_fd, data, length, p->io_offset + offset);
1267 else
1268 n = pwrite (p->io_fd, data, length, p->io_offset + offset);
1269
1270 if (n != length)
1271 return clib_error_return_unix (0, "%s",
1272 read_or_write == VLIB_READ
1273 ? "read" : "write");
1274 return 0;
1275}
1276
1277clib_error_t *
Damjan Marion68b4da62018-09-30 18:26:20 +02001278vlib_pci_map_dma (vlib_main_t * vm, vlib_pci_dev_handle_t h, void *ptr)
1279{
1280 linux_pci_device_t *p = linux_pci_get_device (h);
1281
1282 if (!p->supports_va_dma)
1283 return 0;
1284
1285 return vfio_map_physmem_page (vm, ptr);
1286}
1287
1288int
1289vlib_pci_supports_virtual_addr_dma (vlib_main_t * vm, vlib_pci_dev_handle_t h)
1290{
1291 linux_pci_device_t *p = linux_pci_get_device (h);
1292
1293 return p->supports_va_dma != 0;
1294}
1295
1296clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001297vlib_pci_device_open (vlib_main_t * vm, vlib_pci_addr_t * addr,
Damjan Marionf313b742018-03-05 14:08:33 +01001298 pci_device_id_t ids[], vlib_pci_dev_handle_t * handle)
1299{
1300 linux_pci_main_t *lpm = &linux_pci_main;
1301 vlib_pci_device_info_t *di;
1302 linux_pci_device_t *p;
1303 clib_error_t *err = 0;
1304 pci_device_id_t *i;
1305
Damjan Marion23227982018-10-22 13:38:57 +02001306 di = vlib_pci_get_device_info (vm, addr, &err);
Damjan Marionf313b742018-03-05 14:08:33 +01001307
1308 if (err)
1309 return err;
Damjan Marionf313b742018-03-05 14:08:33 +01001310
Damjan Marion00ea98a2023-07-28 13:19:49 +02001311 if (ids)
1312 {
1313 for (i = ids; i->vendor_id != 0; i++)
1314 if (i->vendor_id == di->vendor_id && i->device_id == di->device_id)
1315 break;
1316
1317 if (i->vendor_id == 0)
1318 {
1319 vlib_pci_free_device_info (di);
1320 return clib_error_return (0, "Wrong vendor or device id");
1321 }
1322 }
Damjan Marionf313b742018-03-05 14:08:33 +01001323
1324 pool_get (lpm->linux_pci_devices, p);
1325 p->handle = p - lpm->linux_pci_devices;
Damjan Marion23227982018-10-22 13:38:57 +02001326 p->addr.as_u32 = di->addr.as_u32;
Damjan Marionf313b742018-03-05 14:08:33 +01001327 p->intx_irq.fd = -1;
Mohsin Kazmi692f9b12019-04-02 11:09:49 +00001328 p->intx_irq.clib_file_index = -1;
Damjan Mariond2bfb782019-01-07 20:56:04 +01001329 p->numa_node = di->numa_node;
Mohsin Kazmi3d3b9552018-10-24 14:05:34 +02001330 /*
1331 * pci io bar read/write fd
1332 */
1333 p->io_fd = -1;
Damjan Marionf313b742018-03-05 14:08:33 +01001334
Damjan Marion00ea98a2023-07-28 13:19:49 +02001335 log_debug (p, "open vid:0x%04x did:0x%04x driver:%s iommu_group:%d",
1336 di->vendor_id, di->device_id, di->driver_name, di->iommu_group);
Damjan Marion23227982018-10-22 13:38:57 +02001337
Steven Luongab489922019-08-21 11:35:27 -07001338 if (clib_strncmp ("vfio-pci", (char *) di->driver_name, 8) == 0)
Damjan Marion23227982018-10-22 13:38:57 +02001339 err = add_device_vfio (vm, p, di, 0);
Steven Luongab489922019-08-21 11:35:27 -07001340 else if (clib_strncmp ("uio_pci_generic", (char *) di->driver_name, 8) == 0)
Damjan Marion23227982018-10-22 13:38:57 +02001341 err = add_device_uio (vm, p, di, 0);
Damjan Marionf313b742018-03-05 14:08:33 +01001342 else
Damjan Marion23227982018-10-22 13:38:57 +02001343 err = clib_error_create ("device not bound to 'vfio-pci' or "
1344 "'uio_pci_generic' kernel module");
Damjan Marionf313b742018-03-05 14:08:33 +01001345 if (err)
1346 goto error;
1347
1348 *handle = p->handle;
1349
1350error:
1351 vlib_pci_free_device_info (di);
1352 if (err)
1353 {
Damjan Marion00ea98a2023-07-28 13:19:49 +02001354 log_err (p, "%U", format_clib_error, err);
Dave Barachb7b92992018-10-17 10:38:51 -04001355 clib_memset (p, 0, sizeof (linux_pci_device_t));
Damjan Marionf313b742018-03-05 14:08:33 +01001356 pool_put (lpm->linux_pci_devices, p);
1357 }
1358
1359 return err;
1360}
1361
1362void
Damjan Marion23227982018-10-22 13:38:57 +02001363vlib_pci_device_close (vlib_main_t * vm, vlib_pci_dev_handle_t h)
Damjan Marionf313b742018-03-05 14:08:33 +01001364{
1365 linux_pci_main_t *lpm = &linux_pci_main;
1366 linux_pci_device_t *p = linux_pci_get_device (h);
1367 linux_pci_irq_t *irq;
1368 linux_pci_region_t *res;
1369 clib_error_t *err = 0;
1370
1371 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1372 {
1373 irq = &p->intx_irq;
Mohsin Kazmi692f9b12019-04-02 11:09:49 +00001374 if (irq->clib_file_index != -1)
1375 clib_file_del_by_index (&file_main, irq->clib_file_index);
Damjan Marionf313b742018-03-05 14:08:33 +01001376 close (p->config_fd);
Mohsin Kazmi3d3b9552018-10-24 14:05:34 +02001377 if (p->io_fd != -1)
1378 close (p->io_fd);
Damjan Marionf313b742018-03-05 14:08:33 +01001379 }
1380 else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
1381 {
1382 irq = &p->intx_irq;
1383 /* close INTx irqs */
1384 if (irq->fd != -1)
1385 {
Damjan Marion23227982018-10-22 13:38:57 +02001386 err = vfio_set_irqs (vm, p, VFIO_PCI_INTX_IRQ_INDEX, 0, 0,
Damjan Marionf313b742018-03-05 14:08:33 +01001387 VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1388 clib_error_free (err);
Mohsin Kazmi692f9b12019-04-02 11:09:49 +00001389 if (irq->clib_file_index != -1)
1390 clib_file_del_by_index (&file_main, irq->clib_file_index);
Damjan Marionf313b742018-03-05 14:08:33 +01001391 close (irq->fd);
1392 }
1393
1394 /* close MSI-X irqs */
1395 if (vec_len (p->msix_irqs))
1396 {
Damjan Marion23227982018-10-22 13:38:57 +02001397 err = vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, 0, 0,
Damjan Marionf313b742018-03-05 14:08:33 +01001398 VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1399 clib_error_free (err);
1400 /* *INDENT-OFF* */
1401 vec_foreach (irq, p->msix_irqs)
1402 {
1403 if (irq->fd == -1)
1404 continue;
1405 clib_file_del_by_index (&file_main, irq->clib_file_index);
1406 close (irq->fd);
1407 }
1408 /* *INDENT-ON* */
1409 vec_free (p->msix_irqs);
1410 }
1411 }
1412
1413 /* *INDENT-OFF* */
1414 vec_foreach (res, p->regions)
1415 {
1416 if (res->size == 0)
1417 continue;
Damjan Marion6bfd0762020-09-11 22:16:53 +02001418 clib_mem_vm_unmap (res->addr);
Damjan Marionf313b742018-03-05 14:08:33 +01001419 if (res->fd != -1)
1420 close (res->fd);
1421 }
1422 /* *INDENT-ON* */
1423 vec_free (p->regions);
1424
1425 close (p->fd);
Dave Barachb7b92992018-10-17 10:38:51 -04001426 clib_memset (p, 0, sizeof (linux_pci_device_t));
Damjan Marionf313b742018-03-05 14:08:33 +01001427 pool_put (lpm->linux_pci_devices, p);
1428}
1429
Dave Barach9b8ffd92016-07-08 08:13:45 -04001430void
Damjan Marion23227982018-10-22 13:38:57 +02001431init_device_from_registered (vlib_main_t * vm, vlib_pci_device_info_t * di)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001432{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001433 vlib_pci_main_t *pm = &pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +02001434 linux_pci_main_t *lpm = &linux_pci_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001435 pci_device_registration_t *r;
1436 pci_device_id_t *i;
Damjan Marioncef87f12017-10-05 15:32:41 +02001437 clib_error_t *err = 0;
1438 linux_pci_device_t *p;
1439
1440 pool_get (lpm->linux_pci_devices, p);
1441 p->handle = p - lpm->linux_pci_devices;
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001442 p->intx_irq.fd = -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001443
Damjan Marion5a206ea2016-05-12 22:11:03 +02001444 r = pm->pci_device_registrations;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001445
1446 while (r)
1447 {
1448 for (i = r->supported_devices; i->vendor_id != 0; i++)
Damjan Marion3a593822018-02-17 14:06:53 +01001449 if (i->vendor_id == di->vendor_id && i->device_id == di->device_id)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001450 {
Damjan Marioncef87f12017-10-05 15:32:41 +02001451 if (di->iommu_group != -1)
Damjan Marion23227982018-10-22 13:38:57 +02001452 err = add_device_vfio (vm, p, di, r);
Damjan Marioncef87f12017-10-05 15:32:41 +02001453 else
Damjan Marion23227982018-10-22 13:38:57 +02001454 err = add_device_uio (vm, p, di, r);
Damjan Marion5a206ea2016-05-12 22:11:03 +02001455
Damjan Marioncef87f12017-10-05 15:32:41 +02001456 if (err)
1457 clib_error_report (err);
1458 else
1459 return;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001460 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001461 r = r->next_registration;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001462 }
Damjan Marioncef87f12017-10-05 15:32:41 +02001463
Ed Warnickecb9cada2015-12-08 15:45:58 -07001464 /* No driver, close the PCI config-space FD */
Dave Barachb7b92992018-10-17 10:38:51 -04001465 clib_memset (p, 0, sizeof (linux_pci_device_t));
Damjan Marioncef87f12017-10-05 15:32:41 +02001466 pool_put (lpm->linux_pci_devices, p);
1467}
1468
1469static clib_error_t *
1470scan_pci_addr (void *arg, u8 * dev_dir_name, u8 * ignored)
1471{
1472 vlib_pci_addr_t addr, **addrv = arg;
1473 unformat_input_t input;
1474 clib_error_t *err = 0;
1475
1476 unformat_init_string (&input, (char *) dev_dir_name,
1477 vec_len (dev_dir_name));
1478
1479 if (!unformat (&input, "/sys/bus/pci/devices/%U",
1480 unformat_vlib_pci_addr, &addr))
1481 err = clib_error_return (0, "unformat error `%v`", dev_dir_name);
1482
1483 unformat_free (&input);
1484
1485 if (err)
1486 return err;
1487
1488 vec_add1 (*addrv, addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001489 return 0;
1490}
1491
Damjan Marioncef87f12017-10-05 15:32:41 +02001492static int
1493pci_addr_cmp (void *v1, void *v2)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001494{
Damjan Marioncef87f12017-10-05 15:32:41 +02001495 vlib_pci_addr_t *a1 = v1;
1496 vlib_pci_addr_t *a2 = v2;
1497
1498 if (a1->domain > a2->domain)
1499 return 1;
1500 if (a1->domain < a2->domain)
1501 return -1;
1502 if (a1->bus > a2->bus)
1503 return 1;
1504 if (a1->bus < a2->bus)
1505 return -1;
1506 if (a1->slot > a2->slot)
1507 return 1;
1508 if (a1->slot < a2->slot)
1509 return -1;
1510 if (a1->function > a2->function)
1511 return 1;
1512 if (a1->function < a2->function)
1513 return -1;
1514 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001515}
1516
Damjan Marioncef87f12017-10-05 15:32:41 +02001517vlib_pci_addr_t *
1518vlib_pci_get_all_dev_addrs ()
Ed Warnickecb9cada2015-12-08 15:45:58 -07001519{
Damjan Marioncef87f12017-10-05 15:32:41 +02001520 vlib_pci_addr_t *addrs = 0;
1521 clib_error_t *err;
1522 err = foreach_directory_file ((char *) sysfs_pci_dev_path, scan_pci_addr,
1523 &addrs, /* scan_dirs */ 0);
1524 if (err)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001525 {
Damjan Marioncef87f12017-10-05 15:32:41 +02001526 vec_free (addrs);
1527 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001528 }
1529
Damjan Marioncef87f12017-10-05 15:32:41 +02001530 vec_sort_with_function (addrs, pci_addr_cmp);
Damjan Mariona42cd342016-04-13 18:03:20 +02001531
Damjan Marioncef87f12017-10-05 15:32:41 +02001532 return addrs;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001533}
1534
Dave Barach9b8ffd92016-07-08 08:13:45 -04001535clib_error_t *
1536linux_pci_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001537{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001538 vlib_pci_main_t *pm = &pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +02001539 vlib_pci_addr_t *addr = 0, *addrs;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001540
1541 pm->vlib_main = vm;
1542
Dave Barach9b8ffd92016-07-08 08:13:45 -04001543 ASSERT (sizeof (vlib_pci_addr_t) == sizeof (u32));
Damjan Mariona42cd342016-04-13 18:03:20 +02001544
Damjan Marioncef87f12017-10-05 15:32:41 +02001545 addrs = vlib_pci_get_all_dev_addrs ();
1546 /* *INDENT-OFF* */
1547 vec_foreach (addr, addrs)
1548 {
1549 vlib_pci_device_info_t *d;
Damjan Marion23227982018-10-22 13:38:57 +02001550 if ((d = vlib_pci_get_device_info (vm, addr, 0)))
Damjan Marioncef87f12017-10-05 15:32:41 +02001551 {
Damjan Marion23227982018-10-22 13:38:57 +02001552 init_device_from_registered (vm, d);
Damjan Marioncef87f12017-10-05 15:32:41 +02001553 vlib_pci_free_device_info (d);
1554 }
1555 }
1556 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001557
Dave Barachf8d50682019-05-14 18:01:44 -04001558 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001559}
1560
Dave Barachf8d50682019-05-14 18:01:44 -04001561/* *INDENT-OFF* */
1562VLIB_INIT_FUNCTION (linux_pci_init) =
1563{
1564 .runs_after = VLIB_INITS("unix_input_init"),
1565};
1566/* *INDENT-ON* */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001567
1568/*
1569 * fd.io coding-style-patch-verification: ON
1570 *
1571 * Local Variables:
1572 * eval: (c-set-style "gnu")
1573 * End:
1574 */