blob: 1a70c5681768ad31d0c9c38b4f6432d2d302ce3b [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
Damjan Marion5a206ea2016-05-12 22:11:03 +02002 * Copyright (c) 2016 Cisco and/or its affiliates.
Ed Warnickecb9cada2015-12-08 15:45:58 -07003 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15/*
16 * pci.c: Linux user space PCI bus management.
17 *
18 * Copyright (c) 2008 Eliot Dresselhaus
19 *
20 * Permission is hereby granted, free of charge, to any person obtaining
21 * a copy of this software and associated documentation files (the
22 * "Software"), to deal in the Software without restriction, including
23 * without limitation the rights to use, copy, modify, merge, publish,
24 * distribute, sublicense, and/or sell copies of the Software, and to
25 * permit persons to whom the Software is furnished to do so, subject to
26 * the following conditions:
27 *
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
30 *
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 */
39
Damjan Marion01914ce2017-09-14 19:04:50 +020040#include <vppinfra/linux/sysfs.h>
41
Ed Warnickecb9cada2015-12-08 15:45:58 -070042#include <vlib/vlib.h>
43#include <vlib/pci/pci.h>
44#include <vlib/unix/unix.h>
Damjan Marion1ba0fa42018-03-04 17:19:08 +010045#include <vlib/linux/vfio.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070046
47#include <sys/types.h>
48#include <sys/stat.h>
49#include <fcntl.h>
50#include <dirent.h>
Damjan Mariona42cd342016-04-13 18:03:20 +020051#include <sys/ioctl.h>
52#include <net/if.h>
53#include <linux/ethtool.h>
54#include <linux/sockios.h>
Damjan Marioncef87f12017-10-05 15:32:41 +020055#include <linux/vfio.h>
56#include <sys/eventfd.h>
57
Ray Kinsella5714a492021-11-02 13:33:44 +000058#define SYSFS_DEVICES_PCI "/sys/devices/pci"
Damjan Marioncef87f12017-10-05 15:32:41 +020059static const char *sysfs_pci_dev_path = "/sys/bus/pci/devices";
60static const char *sysfs_pci_drv_path = "/sys/bus/pci/drivers";
Damjan Marion2752b892017-12-11 15:55:56 +010061static char *sysfs_mod_vfio_noiommu =
62 "/sys/module/vfio/parameters/enable_unsafe_noiommu_mode";
Damjan Mariona42cd342016-04-13 18:03:20 +020063
Damjan Marion00ea98a2023-07-28 13:19:49 +020064VLIB_REGISTER_LOG_CLASS (pci_log, static) = {
65 .class_name = "pci",
66 .subclass_name = "linux",
67};
68
69#define log_debug(p, f, ...) \
70 vlib_log (VLIB_LOG_LEVEL_DEBUG, pci_log.class, "%U: " f, \
71 format_vlib_pci_log, p->handle, ##__VA_ARGS__)
72#define log_err(p, f, ...) \
73 vlib_log (VLIB_LOG_LEVEL_ERR, pci_log.class, "%U: " f, format_vlib_pci_log, \
74 p->handle, ##__VA_ARGS__)
Damjan Marion23227982018-10-22 13:38:57 +020075
Dave Barach9b8ffd92016-07-08 08:13:45 -040076typedef struct
77{
Damjan Marion2060db82018-03-04 17:37:15 +010078 int fd;
79 void *addr;
80 size_t size;
81} linux_pci_region_t;
82
Damjan Marion599a16b2018-03-04 19:35:23 +010083typedef struct
84{
85 int fd;
86 u32 clib_file_index;
87 union
88 {
Damjan Mariond5ded2d2018-03-05 10:18:50 +010089 pci_intx_handler_function_t *intx_handler;
Damjan Marion599a16b2018-03-04 19:35:23 +010090 pci_msix_handler_function_t *msix_handler;
91 };
92} linux_pci_irq_t;
Damjan Marion2060db82018-03-04 17:37:15 +010093
94typedef enum
95{
96 LINUX_PCI_DEVICE_TYPE_UNKNOWN,
97 LINUX_PCI_DEVICE_TYPE_UIO,
98 LINUX_PCI_DEVICE_TYPE_VFIO,
99} linux_pci_device_type_t;
100
101typedef struct
102{
103 linux_pci_device_type_t type;
Damjan Marioncef87f12017-10-05 15:32:41 +0200104 vlib_pci_dev_handle_t handle;
105 vlib_pci_addr_t addr;
Damjan Mariond2bfb782019-01-07 20:56:04 +0100106 u32 numa_node;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200107
108 /* Resource file descriptors. */
Damjan Marion2060db82018-03-04 17:37:15 +0100109 linux_pci_region_t *regions;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200110
111 /* File descriptor for config space read/write. */
112 int config_fd;
Damjan Marionf9a968e2018-02-25 23:26:22 +0100113 u64 config_offset;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200114
Damjan Marionf9a968e2018-02-25 23:26:22 +0100115 /* Device File descriptor */
116 int fd;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200117
Mohsin Kazmi95276ca2018-09-28 11:45:01 +0200118 /* read/write file descriptor for io bar */
119 int io_fd;
120 u64 io_offset;
121
Damjan Marion5a206ea2016-05-12 22:11:03 +0200122 /* Minor device for uio device. */
123 u32 uio_minor;
124
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100125 /* Interrupt handlers */
126 linux_pci_irq_t intx_irq;
Damjan Marion599a16b2018-03-04 19:35:23 +0100127 linux_pci_irq_t *msix_irqs;
Damjan Marioncef87f12017-10-05 15:32:41 +0200128
129 /* private data */
130 uword private_data;
131
Damjan Marion23227982018-10-22 13:38:57 +0200132 u8 supports_va_dma;
133
Damjan Marion5a206ea2016-05-12 22:11:03 +0200134} linux_pci_device_t;
135
136/* Pool of PCI devices. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400137typedef struct
138{
139 vlib_main_t *vlib_main;
140 linux_pci_device_t *linux_pci_devices;
Damjan Marioncef87f12017-10-05 15:32:41 +0200141
Damjan Marion5a206ea2016-05-12 22:11:03 +0200142} linux_pci_main_t;
143
144extern linux_pci_main_t linux_pci_main;
145
Damjan Marion599a16b2018-03-04 19:35:23 +0100146static linux_pci_device_t *
Damjan Marioncef87f12017-10-05 15:32:41 +0200147linux_pci_get_device (vlib_pci_dev_handle_t h)
148{
149 linux_pci_main_t *lpm = &linux_pci_main;
150 return pool_elt_at_index (lpm->linux_pci_devices, h);
151}
152
153uword
Damjan Marion23227982018-10-22 13:38:57 +0200154vlib_pci_get_private_data (vlib_main_t * vm, vlib_pci_dev_handle_t h)
Damjan Marioncef87f12017-10-05 15:32:41 +0200155{
156 linux_pci_device_t *d = linux_pci_get_device (h);
157 return d->private_data;
158}
159
160void
Damjan Marion23227982018-10-22 13:38:57 +0200161vlib_pci_set_private_data (vlib_main_t * vm, vlib_pci_dev_handle_t h,
162 uword private_data)
Damjan Marioncef87f12017-10-05 15:32:41 +0200163{
164 linux_pci_device_t *d = linux_pci_get_device (h);
165 d->private_data = private_data;
166}
167
168vlib_pci_addr_t *
Damjan Marion23227982018-10-22 13:38:57 +0200169vlib_pci_get_addr (vlib_main_t * vm, vlib_pci_dev_handle_t h)
Damjan Marioncef87f12017-10-05 15:32:41 +0200170{
Damjan Marion599a16b2018-03-04 19:35:23 +0100171 linux_pci_device_t *d = linux_pci_get_device (h);
172 return &d->addr;
Damjan Marioncef87f12017-10-05 15:32:41 +0200173}
174
Damjan Mariond2bfb782019-01-07 20:56:04 +0100175u32
176vlib_pci_get_numa_node (vlib_main_t * vm, vlib_pci_dev_handle_t h)
177{
178 linux_pci_device_t *d = linux_pci_get_device (h);
179 return d->numa_node;
180}
181
Mohsin Kazmia402b832019-01-25 15:57:24 +0000182u32
183vlib_pci_get_num_msix_interrupts (vlib_main_t * vm, vlib_pci_dev_handle_t h)
184{
185 linux_pci_device_t *d = linux_pci_get_device (h);
186
187 if (d->type == LINUX_PCI_DEVICE_TYPE_VFIO)
188 {
189 struct vfio_irq_info ii = { 0 };
190
191 ii.argsz = sizeof (struct vfio_irq_info);
192 ii.index = VFIO_PCI_MSIX_IRQ_INDEX;
193 if (ioctl (d->fd, VFIO_DEVICE_GET_IRQ_INFO, &ii) < 0)
194 return 0;
195 return ii.count;
196 }
197 return 0;
198}
199
Damjan Marion5a206ea2016-05-12 22:11:03 +0200200/* Call to allocate/initialize the pci subsystem.
201 This is not an init function so that users can explicitly enable
202 pci only when it's needed. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400203clib_error_t *pci_bus_init (vlib_main_t * vm);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200204
Ed Warnickecb9cada2015-12-08 15:45:58 -0700205linux_pci_main_t linux_pci_main;
206
Damjan Marioncef87f12017-10-05 15:32:41 +0200207vlib_pci_device_info_t *
Damjan Marion23227982018-10-22 13:38:57 +0200208vlib_pci_get_device_info (vlib_main_t * vm, vlib_pci_addr_t * addr,
209 clib_error_t ** error)
Damjan Marioncef87f12017-10-05 15:32:41 +0200210{
Damjan Marioncef87f12017-10-05 15:32:41 +0200211 clib_error_t *err;
212 vlib_pci_device_info_t *di;
213 u8 *f = 0;
214 u32 tmp;
215 int fd;
Yulong Pei45495482019-10-17 18:41:52 +0800216 u8 *tmpstr;
Jieqiang Wang76c61592020-01-13 17:15:13 +0800217 clib_bitmap_t *bmp = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200218
219 di = clib_mem_alloc (sizeof (vlib_pci_device_info_t));
Dave Barachb7b92992018-10-17 10:38:51 -0400220 clib_memset (di, 0, sizeof (vlib_pci_device_info_t));
Damjan Marioncef87f12017-10-05 15:32:41 +0200221 di->addr.as_u32 = addr->as_u32;
222
223 u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
224 format_vlib_pci_addr, addr);
225
226 f = format (0, "%v/config%c", dev_dir_name, 0);
227 fd = open ((char *) f, O_RDWR);
228
229 /* Try read-only access if write fails. */
230 if (fd < 0)
231 fd = open ((char *) f, O_RDONLY);
232
233 if (fd < 0)
234 {
235 err = clib_error_return_unix (0, "open `%s'", f);
236 goto error;
237 }
238
239 /* You can only read more that 64 bytes of config space as root; so we try to
240 read the full space but fall back to just the first 64 bytes. */
Damjan Marionc9275da2023-10-12 17:41:14 +0000241 if (read (fd, &di->config, sizeof (di->config)) <
242 sizeof (vlib_pci_config_hdr_t))
Damjan Marioncef87f12017-10-05 15:32:41 +0200243 {
244 err = clib_error_return_unix (0, "read `%s'", f);
245 close (fd);
246 goto error;
247 }
248
Damjan Marioncef87f12017-10-05 15:32:41 +0200249 di->numa_node = -1;
250 vec_reset_length (f);
251 f = format (f, "%v/numa_node%c", dev_dir_name, 0);
Jieqiang Wang76c61592020-01-13 17:15:13 +0800252 err = clib_sysfs_read ((char *) f, "%d", &di->numa_node);
Damjan Marioncef87f12017-10-05 15:32:41 +0200253 if (err)
254 {
255 di->numa_node = -1;
256 clib_error_free (err);
257 }
Jieqiang Wang76c61592020-01-13 17:15:13 +0800258 if (di->numa_node == -1)
259 {
260 /* if '/sys/bus/pci/devices/<device id>/numa_node' returns -1 and
261 it is a SMP system, set numa_node to 0. */
262 if ((err = clib_sysfs_read ("/sys/devices/system/node/online", "%U",
263 unformat_bitmap_list, &bmp)))
264 clib_error_free (err);
265 if (clib_bitmap_count_set_bits (bmp) == 1)
266 di->numa_node = 0;
267 }
Damjan Marioncef87f12017-10-05 15:32:41 +0200268
269 vec_reset_length (f);
270 f = format (f, "%v/class%c", dev_dir_name, 0);
271 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
272 if (err)
273 goto error;
274 di->device_class = tmp >> 8;
275
276 vec_reset_length (f);
277 f = format (f, "%v/vendor%c", dev_dir_name, 0);
278 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
279 if (err)
280 goto error;
281 di->vendor_id = tmp;
282
283 vec_reset_length (f);
284 f = format (f, "%v/device%c", dev_dir_name, 0);
285 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
286 if (err)
287 goto error;
288 di->device_id = tmp;
289
290 vec_reset_length (f);
Damjan Marion00ea98a2023-07-28 13:19:49 +0200291 f = format (f, "%v/revision%c", dev_dir_name, 0);
292 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
293 if (err)
294 goto error;
295 di->revision = tmp;
296
Damjan Marion40f48102023-08-07 01:07:09 +0200297 di->driver_name =
298 clib_file_get_resolved_basename ("%v/driver", dev_dir_name);
Benoît Ganne0eae2bb2019-10-04 17:30:21 +0200299 if (!di->driver_name)
300 di->driver_name = format (0, "<NONE>%c", 0);
Damjan Marioncef87f12017-10-05 15:32:41 +0200301
302 di->iommu_group = -1;
Damjan Marion40f48102023-08-07 01:07:09 +0200303 tmpstr = clib_file_get_resolved_basename ("%v/iommu_group", dev_dir_name);
Yulong Pei45495482019-10-17 18:41:52 +0800304 if (tmpstr)
Damjan Marioncef87f12017-10-05 15:32:41 +0200305 {
Yulong Pei45495482019-10-17 18:41:52 +0800306 di->iommu_group = atoi ((char *) tmpstr);
307 vec_free (tmpstr);
Damjan Marioncef87f12017-10-05 15:32:41 +0200308 }
309
Yulong Pei45495482019-10-17 18:41:52 +0800310 vec_reset_length (f);
311 f = format (f, "%v/iommu_group/name%c", dev_dir_name, 0);
312 err = clib_sysfs_read ((char *) f, "%s", &tmpstr);
313 if (err == 0)
314 {
315 if (strncmp ((char *) tmpstr, "vfio-noiommu", 12) == 0)
316 di->flags |= VLIB_PCI_DEVICE_INFO_F_NOIOMMU;
317 vec_free (tmpstr);
318 }
319 else
320 clib_error_free (err);
321
Damjan Marion96504182017-12-10 23:54:46 +0100322 close (fd);
323
Damjan Marioncef87f12017-10-05 15:32:41 +0200324 vec_reset_length (f);
325 f = format (f, "%v/vpd%c", dev_dir_name, 0);
326 fd = open ((char *) f, O_RDONLY);
327 if (fd >= 0)
328 {
329 while (1)
330 {
331 u8 tag[3];
332 u8 *data = 0;
Damjan Marion96504182017-12-10 23:54:46 +0100333 uword len;
Damjan Marioncef87f12017-10-05 15:32:41 +0200334
335 if (read (fd, &tag, 3) != 3)
336 break;
337
338 if (tag[0] != 0x82 && tag[0] != 0x90 && tag[0] != 0x91)
339 break;
340
341 len = (tag[2] << 8) | tag[1];
Ray Kinsella03d0fbe2021-11-03 09:13:41 +0000342 vec_validate (data, len - 1);
Damjan Marioncef87f12017-10-05 15:32:41 +0200343
344 if (read (fd, data, len) != len)
345 {
346 vec_free (data);
347 break;
348 }
349 if (tag[0] == 0x82)
350 di->product_name = data;
351 else if (tag[0] == 0x90)
352 di->vpd_r = data;
353 else if (tag[0] == 0x91)
354 di->vpd_w = data;
355
356 data = 0;
357 }
358 close (fd);
359 }
360
361 goto done;
362
363error:
364 vlib_pci_free_device_info (di);
365 di = 0;
366
367done:
Jieqiang Wang76c61592020-01-13 17:15:13 +0800368 vec_free (bmp);
Damjan Marioncef87f12017-10-05 15:32:41 +0200369 vec_free (f);
370 vec_free (dev_dir_name);
371 if (error)
372 *error = err;
373 else
374 clib_error_free (err);
375 return di;
376}
377
Ray Kinsella5714a492021-11-02 13:33:44 +0000378clib_error_t *__attribute__ ((weak))
379vlib_pci_get_device_root_bus (vlib_pci_addr_t *addr, vlib_pci_addr_t *root_bus)
380{
381 u8 *rel_path = 0, *abs_path = 0, *link_path = 0;
382 unformat_input_t input;
Ray Kinsella81865bc2021-11-08 07:22:49 +0000383 int fd = open (sysfs_pci_dev_path, O_RDONLY);
Klement Sekerabd469072021-11-18 12:32:05 +0100384 ssize_t size = 0;
Ray Kinsella5714a492021-11-02 13:33:44 +0000385 u32 domain = 0, bus;
386 clib_error_t *err = NULL;
387
388 if (fd < 0)
389 return clib_error_return_unix (0, "failed to open %s", sysfs_pci_dev_path);
390
391 vec_alloc (rel_path, PATH_MAX);
392 vec_alloc (abs_path, PATH_MAX);
393
394 link_path =
395 format (0, "%s/%U", sysfs_pci_dev_path, format_vlib_pci_addr, addr);
396 size = readlinkat (fd, (char *) link_path, (char *) rel_path, PATH_MAX);
397 if (size < 0)
398 {
399 err = clib_error_return_unix (0, "failed to read %s", rel_path);
400 goto done;
401 }
402
403 rel_path[size] = '\0';
404 vec_free (link_path);
405
406 link_path = format (0, "%s/%s", sysfs_pci_dev_path, rel_path);
407 if (!realpath ((char *) link_path, (char *) abs_path))
408 {
409 err = clib_error_return_unix (0, "failed to resolve %s", link_path);
410 goto done;
411 }
412
413 unformat_init_string (&input, (char *) abs_path,
414 clib_strnlen ((char *) abs_path, PATH_MAX));
415
416 if (!unformat (&input, SYSFS_DEVICES_PCI "%x:%x/%s", &domain, &bus,
417 link_path))
418 {
419 err = clib_error_return (0, "unknown input '%U'", format_unformat_error,
420 input);
421 goto done;
422 }
423
424 root_bus->domain = domain;
425 root_bus->bus = bus;
426
427done:
428 vec_free (abs_path);
429 vec_free (link_path);
430 vec_free (rel_path);
431 close (fd);
432
433 return err;
434}
435
Damjan Marion2752b892017-12-11 15:55:56 +0100436static int
437directory_exists (char *path)
438{
439 struct stat s = { 0 };
440 if (stat (path, &s) == -1)
441 return 0;
442
443 return S_ISDIR (s.st_mode);
444}
445
Damjan Mariona42cd342016-04-13 18:03:20 +0200446clib_error_t *
Benoît Ganne6a073482022-10-13 17:22:26 +0200447vlib_pci_bind_to_uio (vlib_main_t *vm, vlib_pci_addr_t *addr,
448 char *uio_drv_name, int force)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700449{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400450 clib_error_t *error = 0;
Damjan Mariona7f74572017-05-23 18:32:21 +0200451 u8 *s = 0, *driver_name = 0;
Damjan Mariona42cd342016-04-13 18:03:20 +0200452 DIR *dir = 0;
453 struct dirent *e;
Damjan Marioncef87f12017-10-05 15:32:41 +0200454 vlib_pci_device_info_t *di;
Damjan Mariona7f74572017-05-23 18:32:21 +0200455 int fd, clear_driver_override = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200456 u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
457 format_vlib_pci_addr, addr);
458
Damjan Marion23227982018-10-22 13:38:57 +0200459 di = vlib_pci_get_device_info (vm, addr, &error);
Damjan Marioncef87f12017-10-05 15:32:41 +0200460
461 if (error)
462 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700463
Damjan Marion2752b892017-12-11 15:55:56 +0100464 if (strncmp ("auto", uio_drv_name, 5) == 0)
465 {
466 int vfio_pci_loaded = 0;
467
468 if (directory_exists ("/sys/module/vfio_pci"))
469 vfio_pci_loaded = 1;
470
471 if (di->iommu_group != -1)
472 {
473 /* device is bound to IOMMU group */
474 if (!vfio_pci_loaded)
475 {
476 error = clib_error_return (0, "Skipping PCI device %U: device "
477 "is bound to IOMMU group and "
478 "vfio-pci driver is not loaded",
479 format_vlib_pci_addr, addr);
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100480 goto err0;
Damjan Marion2752b892017-12-11 15:55:56 +0100481 }
482 else
483 uio_drv_name = "vfio-pci";
484 }
485 else
486 {
487 /* device is not bound to IOMMU group so we have multiple options */
488 if (vfio_pci_loaded &&
489 (error = clib_sysfs_write (sysfs_mod_vfio_noiommu, "Y")) == 0)
490 uio_drv_name = "vfio-pci";
491 else if (directory_exists ("/sys/module/uio_pci_generic"))
492 uio_drv_name = "uio_pci_generic";
493 else if (directory_exists ("/sys/module/igb_uio"))
494 uio_drv_name = "igb_uio";
495 else
496 {
497 clib_error_free (error);
498 error = clib_error_return (0, "Skipping PCI device %U: missing "
499 "kernel VFIO or UIO driver",
500 format_vlib_pci_addr, addr);
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100501 goto err0;
Damjan Marion2752b892017-12-11 15:55:56 +0100502 }
503 clib_error_free (error);
504 }
505 }
506
Damjan Marion40f48102023-08-07 01:07:09 +0200507 driver_name = clib_file_get_resolved_basename ("%v/driver", dev_dir_name);
Damjan Mariona42cd342016-04-13 18:03:20 +0200508
Damjan Mariona7f74572017-05-23 18:32:21 +0200509 if (driver_name &&
510 ((strcmp ("vfio-pci", (char *) driver_name) == 0) ||
511 (strcmp ("uio_pci_generic", (char *) driver_name) == 0) ||
512 (strcmp ("igb_uio", (char *) driver_name) == 0)))
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100513 goto err0;
Damjan Marion3c785e02017-05-08 18:37:54 +0200514
Benoît Ganne6a073482022-10-13 17:22:26 +0200515 if (!force)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700516 {
Benoît Ganne6a073482022-10-13 17:22:26 +0200517 /* walk trough all linux interfaces and if interface belonging to
518 this device is found check if interface is admin up */
519 dir = opendir ("/sys/class/net");
520 s = format (s, "%U%c", format_vlib_pci_addr, addr, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700521
Benoît Ganne6a073482022-10-13 17:22:26 +0200522 if (!dir)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400523 {
Benoît Ganne6a073482022-10-13 17:22:26 +0200524 error = clib_error_return (0,
525 "Skipping PCI device %U: failed to "
526 "read /sys/class/net",
527 format_vlib_pci_addr, addr);
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100528 goto err0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400529 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200530
Benoît Ganne6a073482022-10-13 17:22:26 +0200531 fd = socket (PF_INET, SOCK_DGRAM, 0);
532 if (fd < 0)
Damjan Mariona42cd342016-04-13 18:03:20 +0200533 {
Benoît Ganne6a073482022-10-13 17:22:26 +0200534 error = clib_error_return_unix (0, "socket");
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100535 goto err1;
Damjan Mariona42cd342016-04-13 18:03:20 +0200536 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700537
Benoît Ganne6a073482022-10-13 17:22:26 +0200538 while ((e = readdir (dir)))
539 {
540 struct ifreq ifr;
541 struct ethtool_drvinfo drvinfo;
542
543 if (e->d_name[0] == '.') /* skip . and .. */
544 continue;
545
546 clib_memset (&ifr, 0, sizeof ifr);
547 clib_memset (&drvinfo, 0, sizeof drvinfo);
548 ifr.ifr_data = (char *) &drvinfo;
549 clib_strncpy (ifr.ifr_name, e->d_name, sizeof (ifr.ifr_name) - 1);
550
551 drvinfo.cmd = ETHTOOL_GDRVINFO;
552 if (ioctl (fd, SIOCETHTOOL, &ifr) < 0)
553 {
554 /* Some interfaces (eg "lo") don't support this ioctl */
555 if ((errno != ENOTSUP) && (errno != ENODEV))
556 clib_unix_warning ("ioctl fetch intf %s bus info error",
557 e->d_name);
558 continue;
559 }
560
561 if (strcmp ((char *) s, drvinfo.bus_info))
562 continue;
563
564 clib_memset (&ifr, 0, sizeof (ifr));
565 clib_strncpy (ifr.ifr_name, e->d_name, sizeof (ifr.ifr_name) - 1);
566
567 if (ioctl (fd, SIOCGIFFLAGS, &ifr) < 0)
568 {
569 error = clib_error_return_unix (0, "ioctl fetch intf %s flags",
570 e->d_name);
571 close (fd);
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100572 goto err1;
Benoît Ganne6a073482022-10-13 17:22:26 +0200573 }
574
575 if (ifr.ifr_flags & IFF_UP)
576 {
577 vlib_log (VLIB_LOG_LEVEL_WARNING, pci_main.log_default,
578 "Skipping PCI device %U as host "
579 "interface %s is up",
580 format_vlib_pci_addr, addr, e->d_name);
581 close (fd);
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100582 goto err1;
Benoît Ganne6a073482022-10-13 17:22:26 +0200583 }
584 }
585
586 close (fd);
587 vec_reset_length (s);
588 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200589
590 s = format (s, "%v/driver/unbind%c", dev_dir_name, 0);
Damjan Marioncef87f12017-10-05 15:32:41 +0200591 clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
Damjan Mariona42cd342016-04-13 18:03:20 +0200592 vec_reset_length (s);
593
Damjan Mariona7f74572017-05-23 18:32:21 +0200594 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
595 if (access ((char *) s, F_OK) == 0)
596 {
Damjan Marioncef87f12017-10-05 15:32:41 +0200597 clib_sysfs_write ((char *) s, "%s", uio_drv_name);
Damjan Mariona7f74572017-05-23 18:32:21 +0200598 clear_driver_override = 1;
599 }
600 else
601 {
602 vec_reset_length (s);
Damjan Marioncef87f12017-10-05 15:32:41 +0200603 s = format (s, "%s/%s/new_id%c", sysfs_pci_drv_path, uio_drv_name, 0);
604 clib_sysfs_write ((char *) s, "0x%04x 0x%04x", di->vendor_id,
605 di->device_id);
Damjan Mariona7f74572017-05-23 18:32:21 +0200606 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200607 vec_reset_length (s);
608
Damjan Marioncef87f12017-10-05 15:32:41 +0200609 s = format (s, "%s/%s/bind%c", sysfs_pci_drv_path, uio_drv_name, 0);
610 clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
Damjan Mariona7f74572017-05-23 18:32:21 +0200611 vec_reset_length (s);
612
613 if (clear_driver_override)
614 {
615 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
Damjan Marion01914ce2017-09-14 19:04:50 +0200616 clib_sysfs_write ((char *) s, "%c", 0);
Damjan Mariona7f74572017-05-23 18:32:21 +0200617 vec_reset_length (s);
618 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200619
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100620err1:
Damjan Mariona42cd342016-04-13 18:03:20 +0200621 closedir (dir);
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100622err0:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700623 vec_free (s);
Damjan Mariona42cd342016-04-13 18:03:20 +0200624 vec_free (dev_dir_name);
Damjan Mariona7f74572017-05-23 18:32:21 +0200625 vec_free (driver_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700626 return error;
627}
628
Ed Warnickecb9cada2015-12-08 15:45:58 -0700629
630static clib_error_t *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400631scan_uio_dir (void *arg, u8 * path_name, u8 * file_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700632{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400633 linux_pci_device_t *l = arg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700634 unformat_input_t input;
635
636 unformat_init_string (&input, (char *) file_name, vec_len (file_name));
637
Dave Barach9b8ffd92016-07-08 08:13:45 -0400638 if (!unformat (&input, "uio%d", &l->uio_minor))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700639 abort ();
640
641 unformat_free (&input);
642 return 0;
643}
644
Dave Barach9b8ffd92016-07-08 08:13:45 -0400645static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200646vfio_set_irqs (vlib_main_t * vm, linux_pci_device_t * p, u32 index, u32 start,
647 u32 count, u32 flags, int *efds)
Damjan Marion599a16b2018-03-04 19:35:23 +0100648{
649 int data_len = efds ? count * sizeof (int) : 0;
650 u8 buf[sizeof (struct vfio_irq_set) + data_len];
Damjan Marion23227982018-10-22 13:38:57 +0200651 struct vfio_irq_info ii = { 0 };
Damjan Marion599a16b2018-03-04 19:35:23 +0100652 struct vfio_irq_set *irq_set = (struct vfio_irq_set *) buf;
653
654
Damjan Marion23227982018-10-22 13:38:57 +0200655 ii.argsz = sizeof (struct vfio_irq_info);
656 ii.index = index;
Damjan Marion599a16b2018-03-04 19:35:23 +0100657
Damjan Marion23227982018-10-22 13:38:57 +0200658 if (ioctl (p->fd, VFIO_DEVICE_GET_IRQ_INFO, &ii) < 0)
Damjan Marion599a16b2018-03-04 19:35:23 +0100659 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) "
660 "'%U'", format_vlib_pci_addr, &p->addr);
661
Damjan Marion00ea98a2023-07-28 13:19:49 +0200662 log_debug (p, "%s index:%u count:%u flags: %s%s%s%s(0x%x)", __func__,
663 ii.index, ii.count,
664 ii.flags & VFIO_IRQ_INFO_EVENTFD ? "eventfd " : "",
665 ii.flags & VFIO_IRQ_INFO_MASKABLE ? "maskable " : "",
666 ii.flags & VFIO_IRQ_INFO_AUTOMASKED ? "automasked " : "",
667 ii.flags & VFIO_IRQ_INFO_NORESIZE ? "noresize " : "", ii.flags);
Damjan Marion23227982018-10-22 13:38:57 +0200668
669 if (ii.count < start + count)
Damjan Marion599a16b2018-03-04 19:35:23 +0100670 return clib_error_return_unix (0, "vfio_set_irq: unexistng interrupt on "
671 "'%U'", format_vlib_pci_addr, &p->addr);
672
673
674 if (efds)
675 {
676 flags |= VFIO_IRQ_SET_DATA_EVENTFD;
Dave Barach178cf492018-11-13 16:34:13 -0500677 clib_memcpy_fast (&irq_set->data, efds, data_len);
Damjan Marion599a16b2018-03-04 19:35:23 +0100678 }
679 else
680 flags |= VFIO_IRQ_SET_DATA_NONE;
681
682 ASSERT ((flags & (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD)) !=
683 (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD));
684
685 irq_set->argsz = sizeof (struct vfio_irq_set) + data_len;
686 irq_set->index = index;
687 irq_set->start = start;
688 irq_set->count = count;
689 irq_set->flags = flags;
690
691 if (ioctl (p->fd, VFIO_DEVICE_SET_IRQS, irq_set) < 0)
692 return clib_error_return_unix (0, "%U:ioctl(VFIO_DEVICE_SET_IRQS) "
693 "[index = %u, start = %u, count = %u, "
694 "flags = 0x%x]",
695 format_vlib_pci_addr, &p->addr,
696 index, start, count, flags);
697 return 0;
698}
699
700static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +0200701linux_pci_uio_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700702{
Damjan Marion23227982018-10-22 13:38:57 +0200703 vlib_main_t *vm = vlib_get_main ();
Damjan Marion5a206ea2016-05-12 22:11:03 +0200704 int __attribute__ ((unused)) rv;
Damjan Marioncef87f12017-10-05 15:32:41 +0200705 vlib_pci_dev_handle_t h = uf->private_data;
706 linux_pci_device_t *p = linux_pci_get_device (h);
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100707 linux_pci_irq_t *irq = &p->intx_irq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700708
Damjan Marion5a206ea2016-05-12 22:11:03 +0200709 u32 icount;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400710 rv = read (uf->file_descriptor, &icount, 4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700711
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100712 if (irq->intx_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200713 irq->intx_handler (vm, h);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200714
Damjan Marion23227982018-10-22 13:38:57 +0200715 vlib_pci_intr_enable (vm, h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700716
717 return /* no error */ 0;
718}
719
Dave Barach9b8ffd92016-07-08 08:13:45 -0400720static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200721linux_pci_vfio_unmask_intx (vlib_main_t * vm, linux_pci_device_t * d)
Damjan Marioncef87f12017-10-05 15:32:41 +0200722{
Damjan Marion23227982018-10-22 13:38:57 +0200723 return vfio_set_irqs (vm, d, VFIO_PCI_INTX_IRQ_INDEX, 0, 1,
Damjan Marion599a16b2018-03-04 19:35:23 +0100724 VFIO_IRQ_SET_ACTION_UNMASK, 0);
725}
726
Damjan Marioncef87f12017-10-05 15:32:41 +0200727static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +0200728linux_pci_uio_error_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700729{
730 u32 error_index = (u32) uf->private_data;
731
732 return clib_error_return (0, "pci device %d: error", error_index);
733}
734
Damjan Marioncef87f12017-10-05 15:32:41 +0200735static clib_error_t *
Damjan Marion599a16b2018-03-04 19:35:23 +0100736linux_pci_vfio_msix_read_ready (clib_file_t * uf)
737{
Damjan Marion23227982018-10-22 13:38:57 +0200738 vlib_main_t *vm = vlib_get_main ();
Damjan Marion599a16b2018-03-04 19:35:23 +0100739 int __attribute__ ((unused)) rv;
740 vlib_pci_dev_handle_t h = uf->private_data >> 16;
741 u16 line = uf->private_data & 0xffff;
742 linux_pci_device_t *p = linux_pci_get_device (h);
743 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, line);
744
745 u64 icount;
746 rv = read (uf->file_descriptor, &icount, sizeof (icount));
747
748 if (irq->msix_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200749 irq->msix_handler (vm, h, line);
Damjan Marion599a16b2018-03-04 19:35:23 +0100750
751 return /* no error */ 0;
752}
753
754static clib_error_t *
755linux_pci_vfio_intx_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700756{
Damjan Marion23227982018-10-22 13:38:57 +0200757 vlib_main_t *vm = vlib_get_main ();
Damjan Marioncef87f12017-10-05 15:32:41 +0200758 int __attribute__ ((unused)) rv;
759 vlib_pci_dev_handle_t h = uf->private_data;
760 linux_pci_device_t *p = linux_pci_get_device (h);
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100761 linux_pci_irq_t *irq = &p->intx_irq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700762
Damjan Marioncef87f12017-10-05 15:32:41 +0200763 u64 icount;
764 rv = read (uf->file_descriptor, &icount, sizeof (icount));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700765
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100766 if (irq->intx_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200767 irq->intx_handler (vm, h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700768
Damjan Marion23227982018-10-22 13:38:57 +0200769 linux_pci_vfio_unmask_intx (vm, p);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700770
Damjan Marioncef87f12017-10-05 15:32:41 +0200771 return /* no error */ 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700772}
773
Damjan Marioncef87f12017-10-05 15:32:41 +0200774static clib_error_t *
775linux_pci_vfio_error_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700776{
Damjan Marioncef87f12017-10-05 15:32:41 +0200777 u32 error_index = (u32) uf->private_data;
778
779 return clib_error_return (0, "pci device %d: error", error_index);
780}
781
782static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200783add_device_uio (vlib_main_t * vm, linux_pci_device_t * p,
784 vlib_pci_device_info_t * di, pci_device_registration_t * r)
Damjan Marioncef87f12017-10-05 15:32:41 +0200785{
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100786 linux_pci_main_t *lpm = &linux_pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +0200787 clib_error_t *err = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200788 u8 *s = 0;
789
Damjan Marionf9a968e2018-02-25 23:26:22 +0100790 p->fd = -1;
Damjan Marion2060db82018-03-04 17:37:15 +0100791 p->type = LINUX_PCI_DEVICE_TYPE_UIO;
Damjan Marioncef87f12017-10-05 15:32:41 +0200792
793 s = format (s, "%s/%U/config%c", sysfs_pci_dev_path,
794 format_vlib_pci_addr, &di->addr, 0);
795
796 p->config_fd = open ((char *) s, O_RDWR);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100797 p->config_offset = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200798 vec_reset_length (s);
799
800 if (p->config_fd == -1)
801 {
802 err = clib_error_return_unix (0, "open '%s'", s);
803 goto error;
804 }
805
Benoît Ganne0b91bd62019-11-20 09:07:50 +0100806 s = format (0, "%s/%U/uio%c", sysfs_pci_dev_path,
807 format_vlib_pci_addr, &di->addr, 0);
Damjan Marioncef87f12017-10-05 15:32:41 +0200808 foreach_directory_file ((char *) s, scan_uio_dir, p, /* scan_dirs */
809 1);
810 vec_reset_length (s);
811
812 s = format (s, "/dev/uio%d%c", p->uio_minor, 0);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100813 p->fd = open ((char *) s, O_RDWR);
814 if (p->fd < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +0200815 {
816 err = clib_error_return_unix (0, "open '%s'", s);
817 goto error;
818 }
819
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100820 if (r && r->interrupt_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200821 vlib_pci_register_intx_handler (vm, p->handle, r->interrupt_handler);
Damjan Marioncef87f12017-10-05 15:32:41 +0200822
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100823 if (r && r->init_function)
824 err = r->init_function (lpm->vlib_main, p->handle);
Damjan Marioncef87f12017-10-05 15:32:41 +0200825
826error:
Damjan Marion20ba1642018-03-26 15:35:33 +0200827 vec_free (s);
Damjan Marioncef87f12017-10-05 15:32:41 +0200828 if (err)
829 {
Damjan Marion96504182017-12-10 23:54:46 +0100830 if (p->config_fd != -1)
831 close (p->config_fd);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100832 if (p->fd != -1)
833 close (p->fd);
Damjan Marioncef87f12017-10-05 15:32:41 +0200834 }
835 return err;
836}
837
Damjan Marion599a16b2018-03-04 19:35:23 +0100838clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200839vlib_pci_register_intx_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h,
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100840 pci_intx_handler_function_t * intx_handler)
841{
842 linux_pci_device_t *p = linux_pci_get_device (h);
843 clib_file_t t = { 0 };
844 linux_pci_irq_t *irq = &p->intx_irq;
845 ASSERT (irq->fd == -1);
846
847 if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
848 {
Damjan Marion23227982018-10-22 13:38:57 +0200849 struct vfio_irq_info ii = { 0 };
850 ii.argsz = sizeof (struct vfio_irq_info);
851 ii.index = VFIO_PCI_INTX_IRQ_INDEX;
852 if (ioctl (p->fd, VFIO_DEVICE_GET_IRQ_INFO, &ii) < 0)
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100853 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) '"
854 "%U'", format_vlib_pci_addr, &p->addr);
Damjan Marion00ea98a2023-07-28 13:19:49 +0200855 log_debug (
856 p, "%s index:%u count:%u flags: %s%s%s%s(0x%x)", __func__, ii.index,
857 ii.count, ii.flags & VFIO_IRQ_INFO_EVENTFD ? "eventfd " : "",
858 ii.flags & VFIO_IRQ_INFO_MASKABLE ? "maskable " : "",
859 ii.flags & VFIO_IRQ_INFO_AUTOMASKED ? "automasked " : "",
860 ii.flags & VFIO_IRQ_INFO_NORESIZE ? "noresize " : "", ii.flags);
Damjan Marion23227982018-10-22 13:38:57 +0200861 if (ii.count != 1)
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100862 return clib_error_return (0, "INTx interrupt does not exist on device"
863 "'%U'", format_vlib_pci_addr, &p->addr);
864
865 irq->fd = eventfd (0, EFD_NONBLOCK);
866 if (irq->fd == -1)
867 return clib_error_return_unix (0, "eventfd");
Damjan Marion23227982018-10-22 13:38:57 +0200868
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100869 t.file_descriptor = irq->fd;
870 t.read_function = linux_pci_vfio_intx_read_ready;
871 }
872 else if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
873 {
874 t.file_descriptor = p->fd;
875 t.read_function = linux_pci_uio_read_ready;
876 }
877 else
878 return 0;
879
880 t.error_function = linux_pci_uio_error_ready;
881 t.private_data = p->handle;
882 t.description = format (0, "PCI %U INTx", format_vlib_pci_addr, &p->addr);
883 irq->clib_file_index = clib_file_add (&file_main, &t);
884 irq->intx_handler = intx_handler;
885 return 0;
886}
887
888clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200889vlib_pci_register_msix_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h,
890 u32 start, u32 count,
Damjan Marion599a16b2018-03-04 19:35:23 +0100891 pci_msix_handler_function_t * msix_handler)
892{
893 clib_error_t *err = 0;
894 linux_pci_device_t *p = linux_pci_get_device (h);
895 u32 i;
896
897 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
898 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
899 "support");
900
901 /* *INDENT-OFF* */
902 vec_validate_init_empty (p->msix_irqs, start + count - 1, (linux_pci_irq_t)
903 { .fd = -1});
904 /* *INDENT-ON* */
905
906 for (i = start; i < start + count; i++)
907 {
908 clib_file_t t = { 0 };
909 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
910 ASSERT (irq->fd == -1);
911
912 irq->fd = eventfd (0, EFD_NONBLOCK);
913 if (irq->fd == -1)
914 {
915 err = clib_error_return_unix (0, "eventfd");
916 goto error;
917 }
918
919 t.read_function = linux_pci_vfio_msix_read_ready;
920 t.file_descriptor = irq->fd;
921 t.error_function = linux_pci_vfio_error_ready;
922 t.private_data = p->handle << 16 | i;
923 t.description = format (0, "PCI %U MSI-X #%u", format_vlib_pci_addr,
924 &p->addr, i);
925 irq->clib_file_index = clib_file_add (&file_main, &t);
926 irq->msix_handler = msix_handler;
927 }
928
929 return 0;
930
931error:
932 while (i-- > start)
933 {
934 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
935 if (irq->fd != -1)
936 {
937 clib_file_del_by_index (&file_main, irq->clib_file_index);
938 close (irq->fd);
939 irq->fd = -1;
940 }
941 }
942 return err;
943}
944
945clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200946vlib_pci_enable_msix_irq (vlib_main_t * vm, vlib_pci_dev_handle_t h,
947 u16 start, u16 count)
Damjan Marion599a16b2018-03-04 19:35:23 +0100948{
949 linux_pci_device_t *p = linux_pci_get_device (h);
950 int fds[count];
951 int i;
952
953 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
954 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
955 "support");
956
957 for (i = start; i < start + count; i++)
958 {
959 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
960 fds[i] = irq->fd;
961 }
962
Damjan Marion23227982018-10-22 13:38:57 +0200963 return vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
Damjan Marion599a16b2018-03-04 19:35:23 +0100964 VFIO_IRQ_SET_ACTION_TRIGGER, fds);
965}
966
Damjan Marion9c9490c2020-10-07 20:00:39 +0200967uword
968vlib_pci_get_msix_file_index (vlib_main_t * vm, vlib_pci_dev_handle_t h,
969 u16 index)
970{
971 linux_pci_device_t *p = linux_pci_get_device (h);
972 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, index);
973 if (irq->fd == -1)
974 return ~0;
975 return irq->clib_file_index;
976}
977
Damjan Marion599a16b2018-03-04 19:35:23 +0100978clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200979vlib_pci_disable_msix_irq (vlib_main_t * vm, vlib_pci_dev_handle_t h,
980 u16 start, u16 count)
Damjan Marion599a16b2018-03-04 19:35:23 +0100981{
982 linux_pci_device_t *p = linux_pci_get_device (h);
983 int i, fds[count];
984
985 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
986 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
987 "support");
988
989 for (i = start; i < start + count; i++)
990 fds[i] = -1;
991
Damjan Marion23227982018-10-22 13:38:57 +0200992 return vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
Damjan Marion599a16b2018-03-04 19:35:23 +0100993 VFIO_IRQ_SET_ACTION_TRIGGER, fds);
994}
995
Damjan Marioncef87f12017-10-05 15:32:41 +0200996static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200997add_device_vfio (vlib_main_t * vm, linux_pci_device_t * p,
998 vlib_pci_device_info_t * di, pci_device_registration_t * r)
Damjan Marioncef87f12017-10-05 15:32:41 +0200999{
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001000 linux_pci_main_t *lpm = &linux_pci_main;
Damjan Marionf9a968e2018-02-25 23:26:22 +01001001 struct vfio_device_info device_info = { 0 };
Damjan Marionf313b742018-03-05 14:08:33 +01001002 struct vfio_region_info reg = { 0 };
Damjan Marioncef87f12017-10-05 15:32:41 +02001003 clib_error_t *err = 0;
1004 u8 *s = 0;
Damjan Marion23227982018-10-22 13:38:57 +02001005 int is_noiommu;
Damjan Marioncef87f12017-10-05 15:32:41 +02001006
Damjan Marion2060db82018-03-04 17:37:15 +01001007 p->type = LINUX_PCI_DEVICE_TYPE_VFIO;
Damjan Marioncef87f12017-10-05 15:32:41 +02001008
Damjan Marion23227982018-10-22 13:38:57 +02001009 if ((err = linux_vfio_group_get_device_fd (&p->addr, &p->fd, &is_noiommu)))
Damjan Marioncef87f12017-10-05 15:32:41 +02001010 return err;
1011
Damjan Marion23227982018-10-22 13:38:57 +02001012 if (is_noiommu == 0)
1013 p->supports_va_dma = 1;
1014
Damjan Marioncef87f12017-10-05 15:32:41 +02001015 device_info.argsz = sizeof (device_info);
Damjan Marionf9a968e2018-02-25 23:26:22 +01001016 if (ioctl (p->fd, VFIO_DEVICE_GET_INFO, &device_info) < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +02001017 {
1018 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
1019 format_vlib_pci_addr, &di->addr);
1020 goto error;
1021 }
1022
Damjan Marionf313b742018-03-05 14:08:33 +01001023 reg.argsz = sizeof (struct vfio_region_info);
1024 reg.index = VFIO_PCI_CONFIG_REGION_INDEX;
1025 if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, &reg) < 0)
1026 {
1027 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
1028 format_vlib_pci_addr, &di->addr);
1029 goto error;
1030 }
Damjan Marion23227982018-10-22 13:38:57 +02001031
Damjan Marion00ea98a2023-07-28 13:19:49 +02001032 log_debug (p, "%s %U", __func__, format_vfio_region_info, &reg);
Damjan Marion23227982018-10-22 13:38:57 +02001033
Damjan Marionf313b742018-03-05 14:08:33 +01001034 p->config_offset = reg.offset;
1035 p->config_fd = p->fd;
1036
Damjan Marioncef87f12017-10-05 15:32:41 +02001037 /* reset if device supports it */
1038 if (device_info.flags & VFIO_DEVICE_FLAGS_RESET)
Damjan Marionf9a968e2018-02-25 23:26:22 +01001039 if (ioctl (p->fd, VFIO_DEVICE_RESET) < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +02001040 {
1041 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_RESET) '%U'",
1042 format_vlib_pci_addr, &di->addr);
1043 goto error;
1044 }
1045
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001046 if (r && r->interrupt_handler)
Damjan Marioncef87f12017-10-05 15:32:41 +02001047 {
Damjan Marion23227982018-10-22 13:38:57 +02001048 vlib_pci_register_intx_handler (vm, p->handle, r->interrupt_handler);
1049 linux_pci_vfio_unmask_intx (vm, p);
Damjan Marioncef87f12017-10-05 15:32:41 +02001050 }
1051
Damjan Marion68b4da62018-09-30 18:26:20 +02001052 if (p->supports_va_dma)
1053 {
1054 vlib_buffer_pool_t *bp;
1055 /* *INDENT-OFF* */
Damjan Mariond50e3472019-01-20 00:03:56 +01001056 vec_foreach (bp, vm->buffer_main->buffer_pools)
Damjan Marion68b4da62018-09-30 18:26:20 +02001057 {
1058 u32 i;
1059 vlib_physmem_map_t *pm;
1060 pm = vlib_physmem_get_map (vm, bp->physmem_map_index);
1061 for (i = 0; i < pm->n_pages; i++)
1062 vfio_map_physmem_page (vm, pm->base + (i << pm->log2_page_size));
1063 }
1064 /* *INDENT-ON* */
1065 }
1066
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001067 if (r && r->init_function)
1068 err = r->init_function (lpm->vlib_main, p->handle);
Damjan Marioncef87f12017-10-05 15:32:41 +02001069
1070error:
1071 vec_free (s);
1072 if (err)
1073 {
Damjan Marionf9a968e2018-02-25 23:26:22 +01001074 if (p->fd != -1)
1075 close (p->fd);
Chris Luke30684ac2018-03-29 12:56:58 -07001076 if (p->config_fd != -1 && p->config_fd != p->fd)
Damjan Marion96504182017-12-10 23:54:46 +01001077 close (p->config_fd);
Chris Luke30684ac2018-03-29 12:56:58 -07001078 p->config_fd = p->fd = -1;
Damjan Marioncef87f12017-10-05 15:32:41 +02001079 }
1080 return err;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001081}
1082
1083/* Configuration space read/write. */
1084clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001085vlib_pci_read_write_config (vlib_main_t * vm, vlib_pci_dev_handle_t h,
Damjan Marion5a206ea2016-05-12 22:11:03 +02001086 vlib_read_or_write_t read_or_write,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001087 uword address, void *data, u32 n_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001088{
Damjan Marioncef87f12017-10-05 15:32:41 +02001089 linux_pci_device_t *p = linux_pci_get_device (h);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001090 int n;
1091
Ed Warnickecb9cada2015-12-08 15:45:58 -07001092 if (read_or_write == VLIB_READ)
Damjan Marionf9a968e2018-02-25 23:26:22 +01001093 n = pread (p->config_fd, data, n_bytes, p->config_offset + address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001094 else
Damjan Marionf9a968e2018-02-25 23:26:22 +01001095 n = pwrite (p->config_fd, data, n_bytes, p->config_offset + address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001096
1097 if (n != n_bytes)
1098 return clib_error_return_unix (0, "%s",
1099 read_or_write == VLIB_READ
1100 ? "read" : "write");
1101
1102 return 0;
1103}
1104
1105static clib_error_t *
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001106vlib_pci_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 bar, int *fd,
1107 u64 * size, u64 * offset)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001108{
Damjan Marioncef87f12017-10-05 15:32:41 +02001109 linux_pci_device_t *p = linux_pci_get_device (h);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001110 clib_error_t *error = 0;
1111 int _fd = -1;
1112 u64 _size = 0, _offset = 0;
Damjan Marion2060db82018-03-04 17:37:15 +01001113
1114 ASSERT (bar <= 5);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001115
1116 error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001117
Damjan Marion2060db82018-03-04 17:37:15 +01001118 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001119 {
Damjan Marion2060db82018-03-04 17:37:15 +01001120 u8 *file_name;
1121 struct stat stat_buf;
1122 file_name = format (0, "%s/%U/resource%d%c", sysfs_pci_dev_path,
1123 format_vlib_pci_addr, &p->addr, bar, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001124
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001125 _fd = open ((char *) file_name, O_RDWR);
1126 if (_fd < 0)
Damjan Marion2060db82018-03-04 17:37:15 +01001127 {
1128 error = clib_error_return_unix (0, "open `%s'", file_name);
1129 vec_free (file_name);
1130 return error;
1131 }
1132
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001133 if (fstat (_fd, &stat_buf) < 0)
Damjan Marion2060db82018-03-04 17:37:15 +01001134 {
1135 error = clib_error_return_unix (0, "fstat `%s'", file_name);
1136 vec_free (file_name);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001137 close (_fd);
Damjan Marion2060db82018-03-04 17:37:15 +01001138 return error;
1139 }
1140
1141 vec_free (file_name);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001142 _size = stat_buf.st_size;
1143 _offset = 0;
Damjan Marion2060db82018-03-04 17:37:15 +01001144 }
1145 else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001146 {
Damjan Marion0792bb42020-05-25 19:31:42 +02001147 struct vfio_region_info *r;
1148 u32 sz = sizeof (struct vfio_region_info);
1149 again:
1150 r = clib_mem_alloc (sz);
1151 clib_memset (r, 0, sz);
1152 r->argsz = sz;
1153 r->index = bar;
1154 if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, r) < 0)
Damjan Marion2060db82018-03-04 17:37:15 +01001155 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) "
1156 "'%U'", format_vlib_pci_addr,
1157 &p->addr);
Damjan Marion0792bb42020-05-25 19:31:42 +02001158 if (sz != r->argsz)
1159 {
1160 sz = r->argsz;
1161 clib_mem_free (r);
1162 goto again;
1163 }
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001164 _fd = p->fd;
Damjan Marion0792bb42020-05-25 19:31:42 +02001165 _size = r->size;
1166 _offset = r->offset;
Damjan Marion00ea98a2023-07-28 13:19:49 +02001167 log_debug (p, "%s %U", __func__, format_vfio_region_info, r);
Damjan Marion0792bb42020-05-25 19:31:42 +02001168 clib_mem_free (r);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001169 }
Damjan Marion2060db82018-03-04 17:37:15 +01001170 else
1171 ASSERT (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001172
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001173 *fd = _fd;
1174 *size = _size;
1175 *offset = _offset;
1176
1177 return error;
1178}
1179
1180static clib_error_t *
1181vlib_pci_map_region_int (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1182 u32 bar, u8 * addr, void **result)
1183{
1184 linux_pci_device_t *p = linux_pci_get_device (h);
1185 int fd = -1;
1186 clib_error_t *error;
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001187 u64 size = 0, offset = 0;
Damjan Marion00ea98a2023-07-28 13:19:49 +02001188 vlib_pci_config_reg_command_t command;
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001189
Damjan Marion00ea98a2023-07-28 13:19:49 +02001190 log_debug (p, "map region %u to va %p", bar, addr);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001191
Damjan Marion00ea98a2023-07-28 13:19:49 +02001192 if ((error = vlib_pci_read_config_u16 (vm, h, 4, &command.as_u16)))
Mohammed Hawari70fc36f2020-10-21 16:41:30 +02001193 return error;
1194
Damjan Marion00ea98a2023-07-28 13:19:49 +02001195 if (!(command.mem_space))
Mohammed Hawari70fc36f2020-10-21 16:41:30 +02001196 {
Damjan Marion00ea98a2023-07-28 13:19:49 +02001197 log_debug (p, "setting memory enable bit");
1198 command.mem_space = 1;
1199 if ((error = vlib_pci_write_config_u16 (vm, h, 4, &command.as_u16)))
Mohammed Hawari70fc36f2020-10-21 16:41:30 +02001200 return error;
1201 }
1202
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001203 if ((error = vlib_pci_region (vm, h, bar, &fd, &size, &offset)))
1204 return error;
1205
Damjan Marion6bfd0762020-09-11 22:16:53 +02001206 *result = clib_mem_vm_map_shared (addr, size, fd, offset,
1207 "PCIe %U region %u", format_vlib_pci_addr,
1208 vlib_pci_get_addr (vm, h), bar);
1209 if (*result == CLIB_MEM_VM_MAP_FAILED)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001210 {
Damjan Marion2060db82018-03-04 17:37:15 +01001211 error = clib_error_return_unix (0, "mmap `BAR%u'", bar);
Dave Barachc72950e2020-05-04 14:47:45 -04001212 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO && (fd != -1))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001213 close (fd);
Damjan Marion2060db82018-03-04 17:37:15 +01001214 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001215 }
Damjan Marion2060db82018-03-04 17:37:15 +01001216
1217 /* *INDENT-OFF* */
1218 vec_validate_init_empty (p->regions, bar,
1219 (linux_pci_region_t) { .fd = -1});
1220 /* *INDENT-ON* */
1221 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1222 p->regions[bar].fd = fd;
1223 p->regions[bar].addr = *result;
1224 p->regions[bar].size = size;
1225 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001226}
1227
1228clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001229vlib_pci_map_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 resource,
1230 void **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001231{
Damjan Marion23227982018-10-22 13:38:57 +02001232 return (vlib_pci_map_region_int (vm, h, resource, 0 /* addr */ , result));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001233}
1234
1235clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001236vlib_pci_map_region_fixed (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1237 u32 resource, u8 * addr, void **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001238{
Damjan Marion23227982018-10-22 13:38:57 +02001239 return (vlib_pci_map_region_int (vm, h, resource, addr, result));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001240}
1241
Damjan Marionf313b742018-03-05 14:08:33 +01001242clib_error_t *
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001243vlib_pci_io_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 resource)
1244{
1245 linux_pci_device_t *p = linux_pci_get_device (h);
1246 clib_error_t *error = 0;
1247 int fd = -1;
1248 u64 size = 0, offset = 0;
1249
1250 if ((error = vlib_pci_region (vm, h, resource, &fd, &size, &offset)))
1251 return error;
1252
1253 p->io_fd = fd;
1254 p->io_offset = offset;
1255 return error;
1256}
1257
1258clib_error_t *
1259vlib_pci_read_write_io (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1260 vlib_read_or_write_t read_or_write,
1261 uword offset, void *data, u32 length)
1262{
1263 linux_pci_device_t *p = linux_pci_get_device (h);
1264 int n = 0;
1265
1266 if (read_or_write == VLIB_READ)
1267 n = pread (p->io_fd, data, length, p->io_offset + offset);
1268 else
1269 n = pwrite (p->io_fd, data, length, p->io_offset + offset);
1270
1271 if (n != length)
1272 return clib_error_return_unix (0, "%s",
1273 read_or_write == VLIB_READ
1274 ? "read" : "write");
1275 return 0;
1276}
1277
1278clib_error_t *
Damjan Marion68b4da62018-09-30 18:26:20 +02001279vlib_pci_map_dma (vlib_main_t * vm, vlib_pci_dev_handle_t h, void *ptr)
1280{
1281 linux_pci_device_t *p = linux_pci_get_device (h);
1282
1283 if (!p->supports_va_dma)
1284 return 0;
1285
1286 return vfio_map_physmem_page (vm, ptr);
1287}
1288
1289int
1290vlib_pci_supports_virtual_addr_dma (vlib_main_t * vm, vlib_pci_dev_handle_t h)
1291{
1292 linux_pci_device_t *p = linux_pci_get_device (h);
1293
1294 return p->supports_va_dma != 0;
1295}
1296
1297clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001298vlib_pci_device_open (vlib_main_t * vm, vlib_pci_addr_t * addr,
Damjan Marionf313b742018-03-05 14:08:33 +01001299 pci_device_id_t ids[], vlib_pci_dev_handle_t * handle)
1300{
1301 linux_pci_main_t *lpm = &linux_pci_main;
1302 vlib_pci_device_info_t *di;
1303 linux_pci_device_t *p;
1304 clib_error_t *err = 0;
1305 pci_device_id_t *i;
1306
Damjan Marion23227982018-10-22 13:38:57 +02001307 di = vlib_pci_get_device_info (vm, addr, &err);
Damjan Marionf313b742018-03-05 14:08:33 +01001308
1309 if (err)
1310 return err;
Damjan Marionf313b742018-03-05 14:08:33 +01001311
Damjan Marion00ea98a2023-07-28 13:19:49 +02001312 if (ids)
1313 {
1314 for (i = ids; i->vendor_id != 0; i++)
1315 if (i->vendor_id == di->vendor_id && i->device_id == di->device_id)
1316 break;
1317
1318 if (i->vendor_id == 0)
1319 {
1320 vlib_pci_free_device_info (di);
1321 return clib_error_return (0, "Wrong vendor or device id");
1322 }
1323 }
Damjan Marionf313b742018-03-05 14:08:33 +01001324
1325 pool_get (lpm->linux_pci_devices, p);
1326 p->handle = p - lpm->linux_pci_devices;
Damjan Marion23227982018-10-22 13:38:57 +02001327 p->addr.as_u32 = di->addr.as_u32;
Damjan Marionf313b742018-03-05 14:08:33 +01001328 p->intx_irq.fd = -1;
Mohsin Kazmi692f9b12019-04-02 11:09:49 +00001329 p->intx_irq.clib_file_index = -1;
Damjan Mariond2bfb782019-01-07 20:56:04 +01001330 p->numa_node = di->numa_node;
Mohsin Kazmi3d3b9552018-10-24 14:05:34 +02001331 /*
1332 * pci io bar read/write fd
1333 */
1334 p->io_fd = -1;
Damjan Marionf313b742018-03-05 14:08:33 +01001335
Damjan Marion00ea98a2023-07-28 13:19:49 +02001336 log_debug (p, "open vid:0x%04x did:0x%04x driver:%s iommu_group:%d",
1337 di->vendor_id, di->device_id, di->driver_name, di->iommu_group);
Damjan Marion23227982018-10-22 13:38:57 +02001338
Steven Luongab489922019-08-21 11:35:27 -07001339 if (clib_strncmp ("vfio-pci", (char *) di->driver_name, 8) == 0)
Damjan Marion23227982018-10-22 13:38:57 +02001340 err = add_device_vfio (vm, p, di, 0);
Steven Luongab489922019-08-21 11:35:27 -07001341 else if (clib_strncmp ("uio_pci_generic", (char *) di->driver_name, 8) == 0)
Damjan Marion23227982018-10-22 13:38:57 +02001342 err = add_device_uio (vm, p, di, 0);
Damjan Marionf313b742018-03-05 14:08:33 +01001343 else
Damjan Marion23227982018-10-22 13:38:57 +02001344 err = clib_error_create ("device not bound to 'vfio-pci' or "
1345 "'uio_pci_generic' kernel module");
Damjan Marionf313b742018-03-05 14:08:33 +01001346 if (err)
1347 goto error;
1348
1349 *handle = p->handle;
1350
1351error:
1352 vlib_pci_free_device_info (di);
1353 if (err)
1354 {
Damjan Marion00ea98a2023-07-28 13:19:49 +02001355 log_err (p, "%U", format_clib_error, err);
Dave Barachb7b92992018-10-17 10:38:51 -04001356 clib_memset (p, 0, sizeof (linux_pci_device_t));
Damjan Marionf313b742018-03-05 14:08:33 +01001357 pool_put (lpm->linux_pci_devices, p);
1358 }
1359
1360 return err;
1361}
1362
1363void
Damjan Marion23227982018-10-22 13:38:57 +02001364vlib_pci_device_close (vlib_main_t * vm, vlib_pci_dev_handle_t h)
Damjan Marionf313b742018-03-05 14:08:33 +01001365{
1366 linux_pci_main_t *lpm = &linux_pci_main;
1367 linux_pci_device_t *p = linux_pci_get_device (h);
1368 linux_pci_irq_t *irq;
1369 linux_pci_region_t *res;
1370 clib_error_t *err = 0;
1371
1372 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1373 {
1374 irq = &p->intx_irq;
Mohsin Kazmi692f9b12019-04-02 11:09:49 +00001375 if (irq->clib_file_index != -1)
1376 clib_file_del_by_index (&file_main, irq->clib_file_index);
Damjan Marionf313b742018-03-05 14:08:33 +01001377 close (p->config_fd);
Mohsin Kazmi3d3b9552018-10-24 14:05:34 +02001378 if (p->io_fd != -1)
1379 close (p->io_fd);
Damjan Marionf313b742018-03-05 14:08:33 +01001380 }
1381 else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
1382 {
1383 irq = &p->intx_irq;
1384 /* close INTx irqs */
1385 if (irq->fd != -1)
1386 {
Damjan Marion23227982018-10-22 13:38:57 +02001387 err = vfio_set_irqs (vm, p, VFIO_PCI_INTX_IRQ_INDEX, 0, 0,
Damjan Marionf313b742018-03-05 14:08:33 +01001388 VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1389 clib_error_free (err);
Mohsin Kazmi692f9b12019-04-02 11:09:49 +00001390 if (irq->clib_file_index != -1)
1391 clib_file_del_by_index (&file_main, irq->clib_file_index);
Damjan Marionf313b742018-03-05 14:08:33 +01001392 close (irq->fd);
1393 }
1394
1395 /* close MSI-X irqs */
1396 if (vec_len (p->msix_irqs))
1397 {
Damjan Marion23227982018-10-22 13:38:57 +02001398 err = vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, 0, 0,
Damjan Marionf313b742018-03-05 14:08:33 +01001399 VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1400 clib_error_free (err);
1401 /* *INDENT-OFF* */
1402 vec_foreach (irq, p->msix_irqs)
1403 {
1404 if (irq->fd == -1)
1405 continue;
1406 clib_file_del_by_index (&file_main, irq->clib_file_index);
1407 close (irq->fd);
1408 }
1409 /* *INDENT-ON* */
1410 vec_free (p->msix_irqs);
1411 }
1412 }
1413
1414 /* *INDENT-OFF* */
1415 vec_foreach (res, p->regions)
1416 {
1417 if (res->size == 0)
1418 continue;
Damjan Marion6bfd0762020-09-11 22:16:53 +02001419 clib_mem_vm_unmap (res->addr);
Damjan Marionf313b742018-03-05 14:08:33 +01001420 if (res->fd != -1)
1421 close (res->fd);
1422 }
1423 /* *INDENT-ON* */
1424 vec_free (p->regions);
1425
1426 close (p->fd);
Dave Barachb7b92992018-10-17 10:38:51 -04001427 clib_memset (p, 0, sizeof (linux_pci_device_t));
Damjan Marionf313b742018-03-05 14:08:33 +01001428 pool_put (lpm->linux_pci_devices, p);
1429}
1430
Dave Barach9b8ffd92016-07-08 08:13:45 -04001431void
Damjan Marion23227982018-10-22 13:38:57 +02001432init_device_from_registered (vlib_main_t * vm, vlib_pci_device_info_t * di)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001433{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001434 vlib_pci_main_t *pm = &pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +02001435 linux_pci_main_t *lpm = &linux_pci_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001436 pci_device_registration_t *r;
1437 pci_device_id_t *i;
Damjan Marioncef87f12017-10-05 15:32:41 +02001438 clib_error_t *err = 0;
1439 linux_pci_device_t *p;
1440
1441 pool_get (lpm->linux_pci_devices, p);
1442 p->handle = p - lpm->linux_pci_devices;
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001443 p->intx_irq.fd = -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001444
Damjan Marion5a206ea2016-05-12 22:11:03 +02001445 r = pm->pci_device_registrations;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001446
1447 while (r)
1448 {
1449 for (i = r->supported_devices; i->vendor_id != 0; i++)
Damjan Marion3a593822018-02-17 14:06:53 +01001450 if (i->vendor_id == di->vendor_id && i->device_id == di->device_id)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001451 {
Damjan Marioncef87f12017-10-05 15:32:41 +02001452 if (di->iommu_group != -1)
Damjan Marion23227982018-10-22 13:38:57 +02001453 err = add_device_vfio (vm, p, di, r);
Damjan Marioncef87f12017-10-05 15:32:41 +02001454 else
Damjan Marion23227982018-10-22 13:38:57 +02001455 err = add_device_uio (vm, p, di, r);
Damjan Marion5a206ea2016-05-12 22:11:03 +02001456
Damjan Marioncef87f12017-10-05 15:32:41 +02001457 if (err)
1458 clib_error_report (err);
1459 else
1460 return;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001461 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001462 r = r->next_registration;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001463 }
Damjan Marioncef87f12017-10-05 15:32:41 +02001464
Ed Warnickecb9cada2015-12-08 15:45:58 -07001465 /* No driver, close the PCI config-space FD */
Dave Barachb7b92992018-10-17 10:38:51 -04001466 clib_memset (p, 0, sizeof (linux_pci_device_t));
Damjan Marioncef87f12017-10-05 15:32:41 +02001467 pool_put (lpm->linux_pci_devices, p);
1468}
1469
1470static clib_error_t *
1471scan_pci_addr (void *arg, u8 * dev_dir_name, u8 * ignored)
1472{
1473 vlib_pci_addr_t addr, **addrv = arg;
1474 unformat_input_t input;
1475 clib_error_t *err = 0;
1476
1477 unformat_init_string (&input, (char *) dev_dir_name,
1478 vec_len (dev_dir_name));
1479
1480 if (!unformat (&input, "/sys/bus/pci/devices/%U",
1481 unformat_vlib_pci_addr, &addr))
1482 err = clib_error_return (0, "unformat error `%v`", dev_dir_name);
1483
1484 unformat_free (&input);
1485
1486 if (err)
1487 return err;
1488
1489 vec_add1 (*addrv, addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001490 return 0;
1491}
1492
Damjan Marioncef87f12017-10-05 15:32:41 +02001493static int
1494pci_addr_cmp (void *v1, void *v2)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001495{
Damjan Marioncef87f12017-10-05 15:32:41 +02001496 vlib_pci_addr_t *a1 = v1;
1497 vlib_pci_addr_t *a2 = v2;
1498
1499 if (a1->domain > a2->domain)
1500 return 1;
1501 if (a1->domain < a2->domain)
1502 return -1;
1503 if (a1->bus > a2->bus)
1504 return 1;
1505 if (a1->bus < a2->bus)
1506 return -1;
1507 if (a1->slot > a2->slot)
1508 return 1;
1509 if (a1->slot < a2->slot)
1510 return -1;
1511 if (a1->function > a2->function)
1512 return 1;
1513 if (a1->function < a2->function)
1514 return -1;
1515 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001516}
1517
Damjan Marioncef87f12017-10-05 15:32:41 +02001518vlib_pci_addr_t *
1519vlib_pci_get_all_dev_addrs ()
Ed Warnickecb9cada2015-12-08 15:45:58 -07001520{
Damjan Marioncef87f12017-10-05 15:32:41 +02001521 vlib_pci_addr_t *addrs = 0;
1522 clib_error_t *err;
1523 err = foreach_directory_file ((char *) sysfs_pci_dev_path, scan_pci_addr,
1524 &addrs, /* scan_dirs */ 0);
1525 if (err)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001526 {
Damjan Marioncef87f12017-10-05 15:32:41 +02001527 vec_free (addrs);
1528 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001529 }
1530
Damjan Marioncef87f12017-10-05 15:32:41 +02001531 vec_sort_with_function (addrs, pci_addr_cmp);
Damjan Mariona42cd342016-04-13 18:03:20 +02001532
Damjan Marioncef87f12017-10-05 15:32:41 +02001533 return addrs;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001534}
1535
Dave Barach9b8ffd92016-07-08 08:13:45 -04001536clib_error_t *
1537linux_pci_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001538{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001539 vlib_pci_main_t *pm = &pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +02001540 vlib_pci_addr_t *addr = 0, *addrs;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001541
1542 pm->vlib_main = vm;
1543
Dave Barach9b8ffd92016-07-08 08:13:45 -04001544 ASSERT (sizeof (vlib_pci_addr_t) == sizeof (u32));
Damjan Mariona42cd342016-04-13 18:03:20 +02001545
Damjan Marioncef87f12017-10-05 15:32:41 +02001546 addrs = vlib_pci_get_all_dev_addrs ();
1547 /* *INDENT-OFF* */
1548 vec_foreach (addr, addrs)
1549 {
1550 vlib_pci_device_info_t *d;
Damjan Marion23227982018-10-22 13:38:57 +02001551 if ((d = vlib_pci_get_device_info (vm, addr, 0)))
Damjan Marioncef87f12017-10-05 15:32:41 +02001552 {
Damjan Marion23227982018-10-22 13:38:57 +02001553 init_device_from_registered (vm, d);
Damjan Marioncef87f12017-10-05 15:32:41 +02001554 vlib_pci_free_device_info (d);
1555 }
1556 }
1557 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001558
Dave Barachf8d50682019-05-14 18:01:44 -04001559 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001560}
1561
Dave Barachf8d50682019-05-14 18:01:44 -04001562/* *INDENT-OFF* */
1563VLIB_INIT_FUNCTION (linux_pci_init) =
1564{
1565 .runs_after = VLIB_INITS("unix_input_init"),
1566};
1567/* *INDENT-ON* */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001568
1569/*
1570 * fd.io coding-style-patch-verification: ON
1571 *
1572 * Local Variables:
1573 * eval: (c-set-style "gnu")
1574 * End:
1575 */