blob: 3af97414ed86c3c085ac83a627a22e936b31cf0d [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
Damjan Marion5a206ea2016-05-12 22:11:03 +02002 * Copyright (c) 2016 Cisco and/or its affiliates.
Ed Warnickecb9cada2015-12-08 15:45:58 -07003 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15/*
16 * pci.c: Linux user space PCI bus management.
17 *
18 * Copyright (c) 2008 Eliot Dresselhaus
19 *
20 * Permission is hereby granted, free of charge, to any person obtaining
21 * a copy of this software and associated documentation files (the
22 * "Software"), to deal in the Software without restriction, including
23 * without limitation the rights to use, copy, modify, merge, publish,
24 * distribute, sublicense, and/or sell copies of the Software, and to
25 * permit persons to whom the Software is furnished to do so, subject to
26 * the following conditions:
27 *
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
30 *
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 */
39
Damjan Marion01914ce2017-09-14 19:04:50 +020040#include <vppinfra/linux/sysfs.h>
Damjan Marion4b661402024-02-29 16:14:27 +010041#include <vppinfra/bitmap.h>
42#include <vppinfra/unix.h>
Damjan Marion01914ce2017-09-14 19:04:50 +020043
Ed Warnickecb9cada2015-12-08 15:45:58 -070044#include <vlib/vlib.h>
45#include <vlib/pci/pci.h>
46#include <vlib/unix/unix.h>
Damjan Marion1ba0fa42018-03-04 17:19:08 +010047#include <vlib/linux/vfio.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070048
49#include <sys/types.h>
50#include <sys/stat.h>
51#include <fcntl.h>
52#include <dirent.h>
Damjan Mariona42cd342016-04-13 18:03:20 +020053#include <sys/ioctl.h>
54#include <net/if.h>
55#include <linux/ethtool.h>
56#include <linux/sockios.h>
Damjan Marioncef87f12017-10-05 15:32:41 +020057#include <linux/vfio.h>
58#include <sys/eventfd.h>
59
Ray Kinsella5714a492021-11-02 13:33:44 +000060#define SYSFS_DEVICES_PCI "/sys/devices/pci"
Damjan Marioncef87f12017-10-05 15:32:41 +020061static const char *sysfs_pci_dev_path = "/sys/bus/pci/devices";
62static const char *sysfs_pci_drv_path = "/sys/bus/pci/drivers";
Damjan Marion2752b892017-12-11 15:55:56 +010063static char *sysfs_mod_vfio_noiommu =
64 "/sys/module/vfio/parameters/enable_unsafe_noiommu_mode";
Damjan Mariona42cd342016-04-13 18:03:20 +020065
Damjan Marion00ea98a2023-07-28 13:19:49 +020066VLIB_REGISTER_LOG_CLASS (pci_log, static) = {
67 .class_name = "pci",
68 .subclass_name = "linux",
69};
70
71#define log_debug(p, f, ...) \
72 vlib_log (VLIB_LOG_LEVEL_DEBUG, pci_log.class, "%U: " f, \
73 format_vlib_pci_log, p->handle, ##__VA_ARGS__)
74#define log_err(p, f, ...) \
75 vlib_log (VLIB_LOG_LEVEL_ERR, pci_log.class, "%U: " f, format_vlib_pci_log, \
76 p->handle, ##__VA_ARGS__)
Damjan Marion23227982018-10-22 13:38:57 +020077
Dave Barach9b8ffd92016-07-08 08:13:45 -040078typedef struct
79{
Damjan Marion2060db82018-03-04 17:37:15 +010080 int fd;
81 void *addr;
82 size_t size;
83} linux_pci_region_t;
84
Damjan Marion599a16b2018-03-04 19:35:23 +010085typedef struct
86{
87 int fd;
88 u32 clib_file_index;
89 union
90 {
Damjan Mariond5ded2d2018-03-05 10:18:50 +010091 pci_intx_handler_function_t *intx_handler;
Damjan Marion599a16b2018-03-04 19:35:23 +010092 pci_msix_handler_function_t *msix_handler;
93 };
94} linux_pci_irq_t;
Damjan Marion2060db82018-03-04 17:37:15 +010095
96typedef enum
97{
98 LINUX_PCI_DEVICE_TYPE_UNKNOWN,
99 LINUX_PCI_DEVICE_TYPE_UIO,
100 LINUX_PCI_DEVICE_TYPE_VFIO,
101} linux_pci_device_type_t;
102
103typedef struct
104{
105 linux_pci_device_type_t type;
Damjan Marioncef87f12017-10-05 15:32:41 +0200106 vlib_pci_dev_handle_t handle;
107 vlib_pci_addr_t addr;
Damjan Mariond2bfb782019-01-07 20:56:04 +0100108 u32 numa_node;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200109
110 /* Resource file descriptors. */
Damjan Marion2060db82018-03-04 17:37:15 +0100111 linux_pci_region_t *regions;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200112
113 /* File descriptor for config space read/write. */
114 int config_fd;
Damjan Marionf9a968e2018-02-25 23:26:22 +0100115 u64 config_offset;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200116
Damjan Marionf9a968e2018-02-25 23:26:22 +0100117 /* Device File descriptor */
118 int fd;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200119
Mohsin Kazmi95276ca2018-09-28 11:45:01 +0200120 /* read/write file descriptor for io bar */
121 int io_fd;
122 u64 io_offset;
123
Damjan Marion5a206ea2016-05-12 22:11:03 +0200124 /* Minor device for uio device. */
125 u32 uio_minor;
126
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100127 /* Interrupt handlers */
128 linux_pci_irq_t intx_irq;
Damjan Marion599a16b2018-03-04 19:35:23 +0100129 linux_pci_irq_t *msix_irqs;
Damjan Marioncef87f12017-10-05 15:32:41 +0200130
131 /* private data */
132 uword private_data;
133
Damjan Marion23227982018-10-22 13:38:57 +0200134 u8 supports_va_dma;
135
Damjan Marion5a206ea2016-05-12 22:11:03 +0200136} linux_pci_device_t;
137
138/* Pool of PCI devices. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400139typedef struct
140{
141 vlib_main_t *vlib_main;
142 linux_pci_device_t *linux_pci_devices;
Damjan Marioncef87f12017-10-05 15:32:41 +0200143
Damjan Marion5a206ea2016-05-12 22:11:03 +0200144} linux_pci_main_t;
145
146extern linux_pci_main_t linux_pci_main;
147
Damjan Marion599a16b2018-03-04 19:35:23 +0100148static linux_pci_device_t *
Damjan Marioncef87f12017-10-05 15:32:41 +0200149linux_pci_get_device (vlib_pci_dev_handle_t h)
150{
151 linux_pci_main_t *lpm = &linux_pci_main;
152 return pool_elt_at_index (lpm->linux_pci_devices, h);
153}
154
155uword
Damjan Marion23227982018-10-22 13:38:57 +0200156vlib_pci_get_private_data (vlib_main_t * vm, vlib_pci_dev_handle_t h)
Damjan Marioncef87f12017-10-05 15:32:41 +0200157{
158 linux_pci_device_t *d = linux_pci_get_device (h);
159 return d->private_data;
160}
161
162void
Damjan Marion23227982018-10-22 13:38:57 +0200163vlib_pci_set_private_data (vlib_main_t * vm, vlib_pci_dev_handle_t h,
164 uword private_data)
Damjan Marioncef87f12017-10-05 15:32:41 +0200165{
166 linux_pci_device_t *d = linux_pci_get_device (h);
167 d->private_data = private_data;
168}
169
170vlib_pci_addr_t *
Damjan Marion23227982018-10-22 13:38:57 +0200171vlib_pci_get_addr (vlib_main_t * vm, vlib_pci_dev_handle_t h)
Damjan Marioncef87f12017-10-05 15:32:41 +0200172{
Damjan Marion599a16b2018-03-04 19:35:23 +0100173 linux_pci_device_t *d = linux_pci_get_device (h);
174 return &d->addr;
Damjan Marioncef87f12017-10-05 15:32:41 +0200175}
176
Damjan Mariond2bfb782019-01-07 20:56:04 +0100177u32
178vlib_pci_get_numa_node (vlib_main_t * vm, vlib_pci_dev_handle_t h)
179{
180 linux_pci_device_t *d = linux_pci_get_device (h);
181 return d->numa_node;
182}
183
Mohsin Kazmia402b832019-01-25 15:57:24 +0000184u32
185vlib_pci_get_num_msix_interrupts (vlib_main_t * vm, vlib_pci_dev_handle_t h)
186{
187 linux_pci_device_t *d = linux_pci_get_device (h);
188
189 if (d->type == LINUX_PCI_DEVICE_TYPE_VFIO)
190 {
191 struct vfio_irq_info ii = { 0 };
192
193 ii.argsz = sizeof (struct vfio_irq_info);
194 ii.index = VFIO_PCI_MSIX_IRQ_INDEX;
195 if (ioctl (d->fd, VFIO_DEVICE_GET_IRQ_INFO, &ii) < 0)
196 return 0;
197 return ii.count;
198 }
199 return 0;
200}
201
Damjan Marion5a206ea2016-05-12 22:11:03 +0200202/* Call to allocate/initialize the pci subsystem.
203 This is not an init function so that users can explicitly enable
204 pci only when it's needed. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400205clib_error_t *pci_bus_init (vlib_main_t * vm);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200206
Ed Warnickecb9cada2015-12-08 15:45:58 -0700207linux_pci_main_t linux_pci_main;
208
Damjan Marioncef87f12017-10-05 15:32:41 +0200209vlib_pci_device_info_t *
Damjan Marion23227982018-10-22 13:38:57 +0200210vlib_pci_get_device_info (vlib_main_t * vm, vlib_pci_addr_t * addr,
211 clib_error_t ** error)
Damjan Marioncef87f12017-10-05 15:32:41 +0200212{
Damjan Marioncef87f12017-10-05 15:32:41 +0200213 clib_error_t *err;
214 vlib_pci_device_info_t *di;
215 u8 *f = 0;
216 u32 tmp;
217 int fd;
Yulong Pei45495482019-10-17 18:41:52 +0800218 u8 *tmpstr;
Jieqiang Wang76c61592020-01-13 17:15:13 +0800219 clib_bitmap_t *bmp = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200220
221 di = clib_mem_alloc (sizeof (vlib_pci_device_info_t));
Dave Barachb7b92992018-10-17 10:38:51 -0400222 clib_memset (di, 0, sizeof (vlib_pci_device_info_t));
Damjan Marioncef87f12017-10-05 15:32:41 +0200223 di->addr.as_u32 = addr->as_u32;
224
225 u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
226 format_vlib_pci_addr, addr);
227
228 f = format (0, "%v/config%c", dev_dir_name, 0);
229 fd = open ((char *) f, O_RDWR);
230
231 /* Try read-only access if write fails. */
232 if (fd < 0)
233 fd = open ((char *) f, O_RDONLY);
234
235 if (fd < 0)
236 {
237 err = clib_error_return_unix (0, "open `%s'", f);
238 goto error;
239 }
240
241 /* You can only read more that 64 bytes of config space as root; so we try to
242 read the full space but fall back to just the first 64 bytes. */
Damjan Marionc9275da2023-10-12 17:41:14 +0000243 if (read (fd, &di->config, sizeof (di->config)) <
244 sizeof (vlib_pci_config_hdr_t))
Damjan Marioncef87f12017-10-05 15:32:41 +0200245 {
246 err = clib_error_return_unix (0, "read `%s'", f);
247 close (fd);
248 goto error;
249 }
250
Damjan Marioncef87f12017-10-05 15:32:41 +0200251 di->numa_node = -1;
252 vec_reset_length (f);
253 f = format (f, "%v/numa_node%c", dev_dir_name, 0);
Jieqiang Wang76c61592020-01-13 17:15:13 +0800254 err = clib_sysfs_read ((char *) f, "%d", &di->numa_node);
Damjan Marioncef87f12017-10-05 15:32:41 +0200255 if (err)
256 {
257 di->numa_node = -1;
258 clib_error_free (err);
259 }
Jieqiang Wang76c61592020-01-13 17:15:13 +0800260 if (di->numa_node == -1)
261 {
Damjan Marion4b661402024-02-29 16:14:27 +0100262 bmp = os_get_online_cpu_node_bitmap ();
Jieqiang Wang76c61592020-01-13 17:15:13 +0800263 if (clib_bitmap_count_set_bits (bmp) == 1)
264 di->numa_node = 0;
265 }
Damjan Marioncef87f12017-10-05 15:32:41 +0200266
267 vec_reset_length (f);
268 f = format (f, "%v/class%c", dev_dir_name, 0);
269 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
270 if (err)
271 goto error;
272 di->device_class = tmp >> 8;
273
274 vec_reset_length (f);
275 f = format (f, "%v/vendor%c", dev_dir_name, 0);
276 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
277 if (err)
278 goto error;
279 di->vendor_id = tmp;
280
281 vec_reset_length (f);
282 f = format (f, "%v/device%c", dev_dir_name, 0);
283 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
284 if (err)
285 goto error;
286 di->device_id = tmp;
287
288 vec_reset_length (f);
Damjan Marion00ea98a2023-07-28 13:19:49 +0200289 f = format (f, "%v/revision%c", dev_dir_name, 0);
290 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
291 if (err)
292 goto error;
293 di->revision = tmp;
294
Damjan Marion40f48102023-08-07 01:07:09 +0200295 di->driver_name =
296 clib_file_get_resolved_basename ("%v/driver", dev_dir_name);
Benoît Ganne0eae2bb2019-10-04 17:30:21 +0200297 if (!di->driver_name)
298 di->driver_name = format (0, "<NONE>%c", 0);
Damjan Marioncef87f12017-10-05 15:32:41 +0200299
300 di->iommu_group = -1;
Damjan Marion40f48102023-08-07 01:07:09 +0200301 tmpstr = clib_file_get_resolved_basename ("%v/iommu_group", dev_dir_name);
Yulong Pei45495482019-10-17 18:41:52 +0800302 if (tmpstr)
Damjan Marioncef87f12017-10-05 15:32:41 +0200303 {
Yulong Pei45495482019-10-17 18:41:52 +0800304 di->iommu_group = atoi ((char *) tmpstr);
305 vec_free (tmpstr);
Damjan Marioncef87f12017-10-05 15:32:41 +0200306 }
307
Yulong Pei45495482019-10-17 18:41:52 +0800308 vec_reset_length (f);
309 f = format (f, "%v/iommu_group/name%c", dev_dir_name, 0);
310 err = clib_sysfs_read ((char *) f, "%s", &tmpstr);
311 if (err == 0)
312 {
313 if (strncmp ((char *) tmpstr, "vfio-noiommu", 12) == 0)
314 di->flags |= VLIB_PCI_DEVICE_INFO_F_NOIOMMU;
315 vec_free (tmpstr);
316 }
317 else
318 clib_error_free (err);
319
Damjan Marion96504182017-12-10 23:54:46 +0100320 close (fd);
321
Damjan Marioncef87f12017-10-05 15:32:41 +0200322 vec_reset_length (f);
323 f = format (f, "%v/vpd%c", dev_dir_name, 0);
324 fd = open ((char *) f, O_RDONLY);
325 if (fd >= 0)
326 {
327 while (1)
328 {
329 u8 tag[3];
330 u8 *data = 0;
Damjan Marion96504182017-12-10 23:54:46 +0100331 uword len;
Damjan Marioncef87f12017-10-05 15:32:41 +0200332
333 if (read (fd, &tag, 3) != 3)
334 break;
335
336 if (tag[0] != 0x82 && tag[0] != 0x90 && tag[0] != 0x91)
337 break;
338
339 len = (tag[2] << 8) | tag[1];
Ray Kinsella03d0fbe2021-11-03 09:13:41 +0000340 vec_validate (data, len - 1);
Damjan Marioncef87f12017-10-05 15:32:41 +0200341
342 if (read (fd, data, len) != len)
343 {
344 vec_free (data);
345 break;
346 }
347 if (tag[0] == 0x82)
348 di->product_name = data;
349 else if (tag[0] == 0x90)
350 di->vpd_r = data;
351 else if (tag[0] == 0x91)
352 di->vpd_w = data;
353
354 data = 0;
355 }
356 close (fd);
357 }
358
359 goto done;
360
361error:
362 vlib_pci_free_device_info (di);
363 di = 0;
364
365done:
Jieqiang Wang76c61592020-01-13 17:15:13 +0800366 vec_free (bmp);
Damjan Marioncef87f12017-10-05 15:32:41 +0200367 vec_free (f);
368 vec_free (dev_dir_name);
369 if (error)
370 *error = err;
371 else
372 clib_error_free (err);
373 return di;
374}
375
Ray Kinsella5714a492021-11-02 13:33:44 +0000376clib_error_t *__attribute__ ((weak))
377vlib_pci_get_device_root_bus (vlib_pci_addr_t *addr, vlib_pci_addr_t *root_bus)
378{
379 u8 *rel_path = 0, *abs_path = 0, *link_path = 0;
380 unformat_input_t input;
Ray Kinsella81865bc2021-11-08 07:22:49 +0000381 int fd = open (sysfs_pci_dev_path, O_RDONLY);
Klement Sekerabd469072021-11-18 12:32:05 +0100382 ssize_t size = 0;
Ray Kinsella5714a492021-11-02 13:33:44 +0000383 u32 domain = 0, bus;
384 clib_error_t *err = NULL;
385
386 if (fd < 0)
387 return clib_error_return_unix (0, "failed to open %s", sysfs_pci_dev_path);
388
389 vec_alloc (rel_path, PATH_MAX);
390 vec_alloc (abs_path, PATH_MAX);
391
392 link_path =
393 format (0, "%s/%U", sysfs_pci_dev_path, format_vlib_pci_addr, addr);
394 size = readlinkat (fd, (char *) link_path, (char *) rel_path, PATH_MAX);
395 if (size < 0)
396 {
397 err = clib_error_return_unix (0, "failed to read %s", rel_path);
398 goto done;
399 }
400
401 rel_path[size] = '\0';
402 vec_free (link_path);
403
404 link_path = format (0, "%s/%s", sysfs_pci_dev_path, rel_path);
405 if (!realpath ((char *) link_path, (char *) abs_path))
406 {
407 err = clib_error_return_unix (0, "failed to resolve %s", link_path);
408 goto done;
409 }
410
411 unformat_init_string (&input, (char *) abs_path,
412 clib_strnlen ((char *) abs_path, PATH_MAX));
413
414 if (!unformat (&input, SYSFS_DEVICES_PCI "%x:%x/%s", &domain, &bus,
415 link_path))
416 {
417 err = clib_error_return (0, "unknown input '%U'", format_unformat_error,
418 input);
419 goto done;
420 }
421
422 root_bus->domain = domain;
423 root_bus->bus = bus;
424
425done:
426 vec_free (abs_path);
427 vec_free (link_path);
428 vec_free (rel_path);
429 close (fd);
430
431 return err;
432}
433
Damjan Marion2752b892017-12-11 15:55:56 +0100434static int
435directory_exists (char *path)
436{
437 struct stat s = { 0 };
438 if (stat (path, &s) == -1)
439 return 0;
440
441 return S_ISDIR (s.st_mode);
442}
443
Damjan Mariona42cd342016-04-13 18:03:20 +0200444clib_error_t *
Benoît Ganne6a073482022-10-13 17:22:26 +0200445vlib_pci_bind_to_uio (vlib_main_t *vm, vlib_pci_addr_t *addr,
446 char *uio_drv_name, int force)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700447{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400448 clib_error_t *error = 0;
Damjan Mariona7f74572017-05-23 18:32:21 +0200449 u8 *s = 0, *driver_name = 0;
Damjan Mariona42cd342016-04-13 18:03:20 +0200450 DIR *dir = 0;
451 struct dirent *e;
Damjan Marioncef87f12017-10-05 15:32:41 +0200452 vlib_pci_device_info_t *di;
Damjan Mariona7f74572017-05-23 18:32:21 +0200453 int fd, clear_driver_override = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200454 u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
455 format_vlib_pci_addr, addr);
456
Damjan Marion23227982018-10-22 13:38:57 +0200457 di = vlib_pci_get_device_info (vm, addr, &error);
Damjan Marioncef87f12017-10-05 15:32:41 +0200458
459 if (error)
460 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700461
Damjan Marion2752b892017-12-11 15:55:56 +0100462 if (strncmp ("auto", uio_drv_name, 5) == 0)
463 {
464 int vfio_pci_loaded = 0;
465
466 if (directory_exists ("/sys/module/vfio_pci"))
467 vfio_pci_loaded = 1;
468
469 if (di->iommu_group != -1)
470 {
471 /* device is bound to IOMMU group */
472 if (!vfio_pci_loaded)
473 {
474 error = clib_error_return (0, "Skipping PCI device %U: device "
475 "is bound to IOMMU group and "
476 "vfio-pci driver is not loaded",
477 format_vlib_pci_addr, addr);
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100478 goto err0;
Damjan Marion2752b892017-12-11 15:55:56 +0100479 }
480 else
481 uio_drv_name = "vfio-pci";
482 }
483 else
484 {
485 /* device is not bound to IOMMU group so we have multiple options */
486 if (vfio_pci_loaded &&
487 (error = clib_sysfs_write (sysfs_mod_vfio_noiommu, "Y")) == 0)
488 uio_drv_name = "vfio-pci";
489 else if (directory_exists ("/sys/module/uio_pci_generic"))
490 uio_drv_name = "uio_pci_generic";
491 else if (directory_exists ("/sys/module/igb_uio"))
492 uio_drv_name = "igb_uio";
493 else
494 {
495 clib_error_free (error);
496 error = clib_error_return (0, "Skipping PCI device %U: missing "
497 "kernel VFIO or UIO driver",
498 format_vlib_pci_addr, addr);
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100499 goto err0;
Damjan Marion2752b892017-12-11 15:55:56 +0100500 }
501 clib_error_free (error);
502 }
503 }
504
Damjan Marion40f48102023-08-07 01:07:09 +0200505 driver_name = clib_file_get_resolved_basename ("%v/driver", dev_dir_name);
Damjan Mariona42cd342016-04-13 18:03:20 +0200506
Damjan Mariona7f74572017-05-23 18:32:21 +0200507 if (driver_name &&
508 ((strcmp ("vfio-pci", (char *) driver_name) == 0) ||
509 (strcmp ("uio_pci_generic", (char *) driver_name) == 0) ||
510 (strcmp ("igb_uio", (char *) driver_name) == 0)))
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100511 goto err0;
Damjan Marion3c785e02017-05-08 18:37:54 +0200512
Benoît Ganne6a073482022-10-13 17:22:26 +0200513 if (!force)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700514 {
Benoît Ganne6a073482022-10-13 17:22:26 +0200515 /* walk trough all linux interfaces and if interface belonging to
516 this device is found check if interface is admin up */
517 dir = opendir ("/sys/class/net");
518 s = format (s, "%U%c", format_vlib_pci_addr, addr, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700519
Benoît Ganne6a073482022-10-13 17:22:26 +0200520 if (!dir)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400521 {
Benoît Ganne6a073482022-10-13 17:22:26 +0200522 error = clib_error_return (0,
523 "Skipping PCI device %U: failed to "
524 "read /sys/class/net",
525 format_vlib_pci_addr, addr);
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100526 goto err0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400527 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200528
Benoît Ganne6a073482022-10-13 17:22:26 +0200529 fd = socket (PF_INET, SOCK_DGRAM, 0);
530 if (fd < 0)
Damjan Mariona42cd342016-04-13 18:03:20 +0200531 {
Benoît Ganne6a073482022-10-13 17:22:26 +0200532 error = clib_error_return_unix (0, "socket");
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100533 goto err1;
Damjan Mariona42cd342016-04-13 18:03:20 +0200534 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700535
Benoît Ganne6a073482022-10-13 17:22:26 +0200536 while ((e = readdir (dir)))
537 {
538 struct ifreq ifr;
539 struct ethtool_drvinfo drvinfo;
540
541 if (e->d_name[0] == '.') /* skip . and .. */
542 continue;
543
544 clib_memset (&ifr, 0, sizeof ifr);
545 clib_memset (&drvinfo, 0, sizeof drvinfo);
546 ifr.ifr_data = (char *) &drvinfo;
547 clib_strncpy (ifr.ifr_name, e->d_name, sizeof (ifr.ifr_name) - 1);
548
549 drvinfo.cmd = ETHTOOL_GDRVINFO;
550 if (ioctl (fd, SIOCETHTOOL, &ifr) < 0)
551 {
552 /* Some interfaces (eg "lo") don't support this ioctl */
553 if ((errno != ENOTSUP) && (errno != ENODEV))
554 clib_unix_warning ("ioctl fetch intf %s bus info error",
555 e->d_name);
556 continue;
557 }
558
559 if (strcmp ((char *) s, drvinfo.bus_info))
560 continue;
561
562 clib_memset (&ifr, 0, sizeof (ifr));
563 clib_strncpy (ifr.ifr_name, e->d_name, sizeof (ifr.ifr_name) - 1);
564
565 if (ioctl (fd, SIOCGIFFLAGS, &ifr) < 0)
566 {
567 error = clib_error_return_unix (0, "ioctl fetch intf %s flags",
568 e->d_name);
569 close (fd);
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100570 goto err1;
Benoît Ganne6a073482022-10-13 17:22:26 +0200571 }
572
573 if (ifr.ifr_flags & IFF_UP)
574 {
575 vlib_log (VLIB_LOG_LEVEL_WARNING, pci_main.log_default,
576 "Skipping PCI device %U as host "
577 "interface %s is up",
578 format_vlib_pci_addr, addr, e->d_name);
579 close (fd);
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100580 goto err1;
Benoît Ganne6a073482022-10-13 17:22:26 +0200581 }
582 }
583
584 close (fd);
585 vec_reset_length (s);
586 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200587
588 s = format (s, "%v/driver/unbind%c", dev_dir_name, 0);
Damjan Marioncef87f12017-10-05 15:32:41 +0200589 clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
Damjan Mariona42cd342016-04-13 18:03:20 +0200590 vec_reset_length (s);
591
Damjan Mariona7f74572017-05-23 18:32:21 +0200592 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
593 if (access ((char *) s, F_OK) == 0)
594 {
Damjan Marioncef87f12017-10-05 15:32:41 +0200595 clib_sysfs_write ((char *) s, "%s", uio_drv_name);
Damjan Mariona7f74572017-05-23 18:32:21 +0200596 clear_driver_override = 1;
597 }
598 else
599 {
600 vec_reset_length (s);
Damjan Marioncef87f12017-10-05 15:32:41 +0200601 s = format (s, "%s/%s/new_id%c", sysfs_pci_drv_path, uio_drv_name, 0);
602 clib_sysfs_write ((char *) s, "0x%04x 0x%04x", di->vendor_id,
603 di->device_id);
Damjan Mariona7f74572017-05-23 18:32:21 +0200604 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200605 vec_reset_length (s);
606
Damjan Marioncef87f12017-10-05 15:32:41 +0200607 s = format (s, "%s/%s/bind%c", sysfs_pci_drv_path, uio_drv_name, 0);
608 clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
Damjan Mariona7f74572017-05-23 18:32:21 +0200609 vec_reset_length (s);
610
611 if (clear_driver_override)
612 {
613 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
Damjan Marion01914ce2017-09-14 19:04:50 +0200614 clib_sysfs_write ((char *) s, "%c", 0);
Damjan Mariona7f74572017-05-23 18:32:21 +0200615 vec_reset_length (s);
616 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200617
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100618err1:
Damjan Mariona42cd342016-04-13 18:03:20 +0200619 closedir (dir);
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100620err0:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700621 vec_free (s);
Damjan Mariona42cd342016-04-13 18:03:20 +0200622 vec_free (dev_dir_name);
Damjan Mariona7f74572017-05-23 18:32:21 +0200623 vec_free (driver_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700624 return error;
625}
626
Ed Warnickecb9cada2015-12-08 15:45:58 -0700627
628static clib_error_t *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400629scan_uio_dir (void *arg, u8 * path_name, u8 * file_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700630{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400631 linux_pci_device_t *l = arg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700632 unformat_input_t input;
633
634 unformat_init_string (&input, (char *) file_name, vec_len (file_name));
635
Dave Barach9b8ffd92016-07-08 08:13:45 -0400636 if (!unformat (&input, "uio%d", &l->uio_minor))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700637 abort ();
638
639 unformat_free (&input);
640 return 0;
641}
642
Dave Barach9b8ffd92016-07-08 08:13:45 -0400643static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200644vfio_set_irqs (vlib_main_t * vm, linux_pci_device_t * p, u32 index, u32 start,
645 u32 count, u32 flags, int *efds)
Damjan Marion599a16b2018-03-04 19:35:23 +0100646{
647 int data_len = efds ? count * sizeof (int) : 0;
648 u8 buf[sizeof (struct vfio_irq_set) + data_len];
Damjan Marion599a16b2018-03-04 19:35:23 +0100649 struct vfio_irq_set *irq_set = (struct vfio_irq_set *) buf;
650
Damjan Marion599a16b2018-03-04 19:35:23 +0100651 if (efds)
652 {
Damjan Marion7444fd22023-11-06 00:07:15 +0000653 int *data = (int *) irq_set->data;
Damjan Marion599a16b2018-03-04 19:35:23 +0100654 flags |= VFIO_IRQ_SET_DATA_EVENTFD;
Damjan Marion7444fd22023-11-06 00:07:15 +0000655 for (u32 i = 0; i < count; i++)
656 data[i] = efds[i];
Damjan Marion599a16b2018-03-04 19:35:23 +0100657 }
658 else
659 flags |= VFIO_IRQ_SET_DATA_NONE;
660
661 ASSERT ((flags & (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD)) !=
662 (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD));
663
664 irq_set->argsz = sizeof (struct vfio_irq_set) + data_len;
665 irq_set->index = index;
666 irq_set->start = start;
667 irq_set->count = count;
668 irq_set->flags = flags;
669
670 if (ioctl (p->fd, VFIO_DEVICE_SET_IRQS, irq_set) < 0)
Damjan Marion7444fd22023-11-06 00:07:15 +0000671 return clib_error_return_unix (0, "%U:ioctl(VFIO_DEVICE_SET_IRQS)\n%U",
Damjan Marion599a16b2018-03-04 19:35:23 +0100672 format_vlib_pci_addr, &p->addr,
Damjan Marion7444fd22023-11-06 00:07:15 +0000673 format_vfio_irq_set, irq_set);
674
675 log_debug (p, "%s:\n%U", __func__, format_vfio_irq_set, irq_set);
Damjan Marion599a16b2018-03-04 19:35:23 +0100676 return 0;
677}
678
679static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +0200680linux_pci_uio_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700681{
Damjan Marion23227982018-10-22 13:38:57 +0200682 vlib_main_t *vm = vlib_get_main ();
Damjan Marion5a206ea2016-05-12 22:11:03 +0200683 int __attribute__ ((unused)) rv;
Damjan Marioncef87f12017-10-05 15:32:41 +0200684 vlib_pci_dev_handle_t h = uf->private_data;
685 linux_pci_device_t *p = linux_pci_get_device (h);
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100686 linux_pci_irq_t *irq = &p->intx_irq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700687
Damjan Marion5a206ea2016-05-12 22:11:03 +0200688 u32 icount;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400689 rv = read (uf->file_descriptor, &icount, 4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700690
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100691 if (irq->intx_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200692 irq->intx_handler (vm, h);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200693
Damjan Marion23227982018-10-22 13:38:57 +0200694 vlib_pci_intr_enable (vm, h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700695
696 return /* no error */ 0;
697}
698
Dave Barach9b8ffd92016-07-08 08:13:45 -0400699static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200700linux_pci_vfio_unmask_intx (vlib_main_t * vm, linux_pci_device_t * d)
Damjan Marioncef87f12017-10-05 15:32:41 +0200701{
Damjan Marion23227982018-10-22 13:38:57 +0200702 return vfio_set_irqs (vm, d, VFIO_PCI_INTX_IRQ_INDEX, 0, 1,
Damjan Marion599a16b2018-03-04 19:35:23 +0100703 VFIO_IRQ_SET_ACTION_UNMASK, 0);
704}
705
Damjan Marioncef87f12017-10-05 15:32:41 +0200706static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +0200707linux_pci_uio_error_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700708{
709 u32 error_index = (u32) uf->private_data;
710
711 return clib_error_return (0, "pci device %d: error", error_index);
712}
713
Damjan Marioncef87f12017-10-05 15:32:41 +0200714static clib_error_t *
Damjan Marion599a16b2018-03-04 19:35:23 +0100715linux_pci_vfio_msix_read_ready (clib_file_t * uf)
716{
Damjan Marion23227982018-10-22 13:38:57 +0200717 vlib_main_t *vm = vlib_get_main ();
Damjan Marion599a16b2018-03-04 19:35:23 +0100718 int __attribute__ ((unused)) rv;
719 vlib_pci_dev_handle_t h = uf->private_data >> 16;
720 u16 line = uf->private_data & 0xffff;
721 linux_pci_device_t *p = linux_pci_get_device (h);
722 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, line);
723
724 u64 icount;
725 rv = read (uf->file_descriptor, &icount, sizeof (icount));
726
727 if (irq->msix_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200728 irq->msix_handler (vm, h, line);
Damjan Marion599a16b2018-03-04 19:35:23 +0100729
730 return /* no error */ 0;
731}
732
733static clib_error_t *
734linux_pci_vfio_intx_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700735{
Damjan Marion23227982018-10-22 13:38:57 +0200736 vlib_main_t *vm = vlib_get_main ();
Damjan Marioncef87f12017-10-05 15:32:41 +0200737 int __attribute__ ((unused)) rv;
738 vlib_pci_dev_handle_t h = uf->private_data;
739 linux_pci_device_t *p = linux_pci_get_device (h);
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100740 linux_pci_irq_t *irq = &p->intx_irq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700741
Damjan Marioncef87f12017-10-05 15:32:41 +0200742 u64 icount;
743 rv = read (uf->file_descriptor, &icount, sizeof (icount));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700744
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100745 if (irq->intx_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200746 irq->intx_handler (vm, h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700747
Damjan Marion23227982018-10-22 13:38:57 +0200748 linux_pci_vfio_unmask_intx (vm, p);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700749
Damjan Marioncef87f12017-10-05 15:32:41 +0200750 return /* no error */ 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700751}
752
Damjan Marioncef87f12017-10-05 15:32:41 +0200753static clib_error_t *
754linux_pci_vfio_error_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700755{
Damjan Marioncef87f12017-10-05 15:32:41 +0200756 u32 error_index = (u32) uf->private_data;
757
758 return clib_error_return (0, "pci device %d: error", error_index);
759}
760
761static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200762add_device_uio (vlib_main_t * vm, linux_pci_device_t * p,
763 vlib_pci_device_info_t * di, pci_device_registration_t * r)
Damjan Marioncef87f12017-10-05 15:32:41 +0200764{
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100765 linux_pci_main_t *lpm = &linux_pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +0200766 clib_error_t *err = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200767 u8 *s = 0;
768
Damjan Marionf9a968e2018-02-25 23:26:22 +0100769 p->fd = -1;
Damjan Marion2060db82018-03-04 17:37:15 +0100770 p->type = LINUX_PCI_DEVICE_TYPE_UIO;
Damjan Marioncef87f12017-10-05 15:32:41 +0200771
772 s = format (s, "%s/%U/config%c", sysfs_pci_dev_path,
773 format_vlib_pci_addr, &di->addr, 0);
774
775 p->config_fd = open ((char *) s, O_RDWR);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100776 p->config_offset = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200777 vec_reset_length (s);
778
779 if (p->config_fd == -1)
780 {
781 err = clib_error_return_unix (0, "open '%s'", s);
782 goto error;
783 }
784
Benoît Ganne0b91bd62019-11-20 09:07:50 +0100785 s = format (0, "%s/%U/uio%c", sysfs_pci_dev_path,
786 format_vlib_pci_addr, &di->addr, 0);
Damjan Marioncef87f12017-10-05 15:32:41 +0200787 foreach_directory_file ((char *) s, scan_uio_dir, p, /* scan_dirs */
788 1);
789 vec_reset_length (s);
790
791 s = format (s, "/dev/uio%d%c", p->uio_minor, 0);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100792 p->fd = open ((char *) s, O_RDWR);
793 if (p->fd < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +0200794 {
795 err = clib_error_return_unix (0, "open '%s'", s);
796 goto error;
797 }
798
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100799 if (r && r->interrupt_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200800 vlib_pci_register_intx_handler (vm, p->handle, r->interrupt_handler);
Damjan Marioncef87f12017-10-05 15:32:41 +0200801
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100802 if (r && r->init_function)
803 err = r->init_function (lpm->vlib_main, p->handle);
Damjan Marioncef87f12017-10-05 15:32:41 +0200804
805error:
Damjan Marion20ba1642018-03-26 15:35:33 +0200806 vec_free (s);
Damjan Marioncef87f12017-10-05 15:32:41 +0200807 if (err)
808 {
Damjan Marion96504182017-12-10 23:54:46 +0100809 if (p->config_fd != -1)
810 close (p->config_fd);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100811 if (p->fd != -1)
812 close (p->fd);
Damjan Marioncef87f12017-10-05 15:32:41 +0200813 }
814 return err;
815}
816
Damjan Marion599a16b2018-03-04 19:35:23 +0100817clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200818vlib_pci_register_intx_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h,
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100819 pci_intx_handler_function_t * intx_handler)
820{
821 linux_pci_device_t *p = linux_pci_get_device (h);
822 clib_file_t t = { 0 };
823 linux_pci_irq_t *irq = &p->intx_irq;
824 ASSERT (irq->fd == -1);
825
826 if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
827 {
Damjan Marion23227982018-10-22 13:38:57 +0200828 struct vfio_irq_info ii = { 0 };
829 ii.argsz = sizeof (struct vfio_irq_info);
830 ii.index = VFIO_PCI_INTX_IRQ_INDEX;
831 if (ioctl (p->fd, VFIO_DEVICE_GET_IRQ_INFO, &ii) < 0)
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100832 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) '"
833 "%U'", format_vlib_pci_addr, &p->addr);
Damjan Marion00ea98a2023-07-28 13:19:49 +0200834 log_debug (
835 p, "%s index:%u count:%u flags: %s%s%s%s(0x%x)", __func__, ii.index,
836 ii.count, ii.flags & VFIO_IRQ_INFO_EVENTFD ? "eventfd " : "",
837 ii.flags & VFIO_IRQ_INFO_MASKABLE ? "maskable " : "",
838 ii.flags & VFIO_IRQ_INFO_AUTOMASKED ? "automasked " : "",
839 ii.flags & VFIO_IRQ_INFO_NORESIZE ? "noresize " : "", ii.flags);
Damjan Marion23227982018-10-22 13:38:57 +0200840 if (ii.count != 1)
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100841 return clib_error_return (0, "INTx interrupt does not exist on device"
842 "'%U'", format_vlib_pci_addr, &p->addr);
843
844 irq->fd = eventfd (0, EFD_NONBLOCK);
845 if (irq->fd == -1)
846 return clib_error_return_unix (0, "eventfd");
Damjan Marion23227982018-10-22 13:38:57 +0200847
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100848 t.file_descriptor = irq->fd;
849 t.read_function = linux_pci_vfio_intx_read_ready;
850 }
851 else if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
852 {
853 t.file_descriptor = p->fd;
854 t.read_function = linux_pci_uio_read_ready;
855 }
856 else
857 return 0;
858
859 t.error_function = linux_pci_uio_error_ready;
860 t.private_data = p->handle;
861 t.description = format (0, "PCI %U INTx", format_vlib_pci_addr, &p->addr);
862 irq->clib_file_index = clib_file_add (&file_main, &t);
863 irq->intx_handler = intx_handler;
864 return 0;
865}
866
867clib_error_t *
Damjan Marion38c61912023-10-17 16:06:26 +0000868vlib_pci_unregister_intx_handler (vlib_main_t *vm, vlib_pci_dev_handle_t h)
869{
870 linux_pci_device_t *p = linux_pci_get_device (h);
871 linux_pci_irq_t *irq = &p->intx_irq;
872
873 if (irq->intx_handler == 0)
874 return 0;
875
876 clib_file_del_by_index (&file_main, irq->clib_file_index);
877 if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
878 {
879 close (irq->fd);
880 irq->fd = -1;
881 }
882
883 irq->intx_handler = 0;
884
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 Marion38c61912023-10-17 16:06:26 +0000946vlib_pci_unregister_msix_handler (vlib_main_t *vm, vlib_pci_dev_handle_t h,
947 u32 start, u32 count)
948{
949 clib_error_t *err = 0;
950 linux_pci_device_t *p = linux_pci_get_device (h);
951 u32 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
961 if (irq->fd != -1)
962 {
963 clib_file_del_by_index (&file_main, irq->clib_file_index);
964 close (irq->fd);
965 irq->fd = -1;
966 }
967 }
968
969 return err;
970}
971
972clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200973vlib_pci_enable_msix_irq (vlib_main_t * vm, vlib_pci_dev_handle_t h,
974 u16 start, u16 count)
Damjan Marion599a16b2018-03-04 19:35:23 +0100975{
976 linux_pci_device_t *p = linux_pci_get_device (h);
977 int fds[count];
978 int i;
979
980 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
981 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
982 "support");
983
Damjan Marion60529a82023-11-06 00:06:26 +0000984 for (i = 0; i < count; i++)
Damjan Marion599a16b2018-03-04 19:35:23 +0100985 {
Damjan Marion60529a82023-11-06 00:06:26 +0000986 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, start + i);
Damjan Marion599a16b2018-03-04 19:35:23 +0100987 fds[i] = irq->fd;
988 }
989
Damjan Marion23227982018-10-22 13:38:57 +0200990 return vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
Damjan Marion599a16b2018-03-04 19:35:23 +0100991 VFIO_IRQ_SET_ACTION_TRIGGER, fds);
992}
993
Damjan Marion9c9490c2020-10-07 20:00:39 +0200994uword
995vlib_pci_get_msix_file_index (vlib_main_t * vm, vlib_pci_dev_handle_t h,
996 u16 index)
997{
998 linux_pci_device_t *p = linux_pci_get_device (h);
999 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, index);
1000 if (irq->fd == -1)
1001 return ~0;
1002 return irq->clib_file_index;
1003}
1004
Damjan Marion599a16b2018-03-04 19:35:23 +01001005clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001006vlib_pci_disable_msix_irq (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1007 u16 start, u16 count)
Damjan Marion599a16b2018-03-04 19:35:23 +01001008{
1009 linux_pci_device_t *p = linux_pci_get_device (h);
1010 int i, fds[count];
1011
1012 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
1013 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
1014 "support");
1015
1016 for (i = start; i < start + count; i++)
1017 fds[i] = -1;
1018
Damjan Marion23227982018-10-22 13:38:57 +02001019 return vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
Damjan Marion599a16b2018-03-04 19:35:23 +01001020 VFIO_IRQ_SET_ACTION_TRIGGER, fds);
1021}
1022
Damjan Marioncef87f12017-10-05 15:32:41 +02001023static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001024add_device_vfio (vlib_main_t * vm, linux_pci_device_t * p,
1025 vlib_pci_device_info_t * di, pci_device_registration_t * r)
Damjan Marioncef87f12017-10-05 15:32:41 +02001026{
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001027 linux_pci_main_t *lpm = &linux_pci_main;
Damjan Marionf9a968e2018-02-25 23:26:22 +01001028 struct vfio_device_info device_info = { 0 };
Damjan Marionf313b742018-03-05 14:08:33 +01001029 struct vfio_region_info reg = { 0 };
Damjan Marioncef87f12017-10-05 15:32:41 +02001030 clib_error_t *err = 0;
1031 u8 *s = 0;
Damjan Marion23227982018-10-22 13:38:57 +02001032 int is_noiommu;
Damjan Marioncef87f12017-10-05 15:32:41 +02001033
Damjan Marion2060db82018-03-04 17:37:15 +01001034 p->type = LINUX_PCI_DEVICE_TYPE_VFIO;
Damjan Marioncef87f12017-10-05 15:32:41 +02001035
Damjan Marion23227982018-10-22 13:38:57 +02001036 if ((err = linux_vfio_group_get_device_fd (&p->addr, &p->fd, &is_noiommu)))
Damjan Marioncef87f12017-10-05 15:32:41 +02001037 return err;
1038
Damjan Marion23227982018-10-22 13:38:57 +02001039 if (is_noiommu == 0)
1040 p->supports_va_dma = 1;
1041
Damjan Marioncef87f12017-10-05 15:32:41 +02001042 device_info.argsz = sizeof (device_info);
Damjan Marionf9a968e2018-02-25 23:26:22 +01001043 if (ioctl (p->fd, VFIO_DEVICE_GET_INFO, &device_info) < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +02001044 {
1045 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
1046 format_vlib_pci_addr, &di->addr);
1047 goto error;
1048 }
1049
Damjan Marionf313b742018-03-05 14:08:33 +01001050 reg.argsz = sizeof (struct vfio_region_info);
1051 reg.index = VFIO_PCI_CONFIG_REGION_INDEX;
1052 if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, &reg) < 0)
1053 {
1054 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
1055 format_vlib_pci_addr, &di->addr);
1056 goto error;
1057 }
Damjan Marion23227982018-10-22 13:38:57 +02001058
Damjan Marion00ea98a2023-07-28 13:19:49 +02001059 log_debug (p, "%s %U", __func__, format_vfio_region_info, &reg);
Damjan Marion23227982018-10-22 13:38:57 +02001060
Damjan Marionf313b742018-03-05 14:08:33 +01001061 p->config_offset = reg.offset;
1062 p->config_fd = p->fd;
1063
Damjan Marioncef87f12017-10-05 15:32:41 +02001064 /* reset if device supports it */
1065 if (device_info.flags & VFIO_DEVICE_FLAGS_RESET)
Damjan Marionf9a968e2018-02-25 23:26:22 +01001066 if (ioctl (p->fd, VFIO_DEVICE_RESET) < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +02001067 {
1068 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_RESET) '%U'",
1069 format_vlib_pci_addr, &di->addr);
1070 goto error;
1071 }
1072
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001073 if (r && r->interrupt_handler)
Damjan Marioncef87f12017-10-05 15:32:41 +02001074 {
Damjan Marion23227982018-10-22 13:38:57 +02001075 vlib_pci_register_intx_handler (vm, p->handle, r->interrupt_handler);
1076 linux_pci_vfio_unmask_intx (vm, p);
Damjan Marioncef87f12017-10-05 15:32:41 +02001077 }
1078
Damjan Marion68b4da62018-09-30 18:26:20 +02001079 if (p->supports_va_dma)
1080 {
1081 vlib_buffer_pool_t *bp;
1082 /* *INDENT-OFF* */
Damjan Mariond50e3472019-01-20 00:03:56 +01001083 vec_foreach (bp, vm->buffer_main->buffer_pools)
Damjan Marion68b4da62018-09-30 18:26:20 +02001084 {
1085 u32 i;
1086 vlib_physmem_map_t *pm;
1087 pm = vlib_physmem_get_map (vm, bp->physmem_map_index);
1088 for (i = 0; i < pm->n_pages; i++)
1089 vfio_map_physmem_page (vm, pm->base + (i << pm->log2_page_size));
1090 }
1091 /* *INDENT-ON* */
1092 }
1093
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001094 if (r && r->init_function)
1095 err = r->init_function (lpm->vlib_main, p->handle);
Damjan Marioncef87f12017-10-05 15:32:41 +02001096
1097error:
1098 vec_free (s);
1099 if (err)
1100 {
Damjan Marionf9a968e2018-02-25 23:26:22 +01001101 if (p->fd != -1)
1102 close (p->fd);
Chris Luke30684ac2018-03-29 12:56:58 -07001103 if (p->config_fd != -1 && p->config_fd != p->fd)
Damjan Marion96504182017-12-10 23:54:46 +01001104 close (p->config_fd);
Chris Luke30684ac2018-03-29 12:56:58 -07001105 p->config_fd = p->fd = -1;
Damjan Marioncef87f12017-10-05 15:32:41 +02001106 }
1107 return err;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001108}
1109
1110/* Configuration space read/write. */
1111clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001112vlib_pci_read_write_config (vlib_main_t * vm, vlib_pci_dev_handle_t h,
Damjan Marion5a206ea2016-05-12 22:11:03 +02001113 vlib_read_or_write_t read_or_write,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001114 uword address, void *data, u32 n_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001115{
Damjan Marioncef87f12017-10-05 15:32:41 +02001116 linux_pci_device_t *p = linux_pci_get_device (h);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001117 int n;
1118
Ed Warnickecb9cada2015-12-08 15:45:58 -07001119 if (read_or_write == VLIB_READ)
Damjan Marionf9a968e2018-02-25 23:26:22 +01001120 n = pread (p->config_fd, data, n_bytes, p->config_offset + address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001121 else
Damjan Marionf9a968e2018-02-25 23:26:22 +01001122 n = pwrite (p->config_fd, data, n_bytes, p->config_offset + address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001123
1124 if (n != n_bytes)
1125 return clib_error_return_unix (0, "%s",
1126 read_or_write == VLIB_READ
1127 ? "read" : "write");
1128
1129 return 0;
1130}
1131
1132static clib_error_t *
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001133vlib_pci_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 bar, int *fd,
1134 u64 * size, u64 * offset)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001135{
Damjan Marioncef87f12017-10-05 15:32:41 +02001136 linux_pci_device_t *p = linux_pci_get_device (h);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001137 clib_error_t *error = 0;
1138 int _fd = -1;
1139 u64 _size = 0, _offset = 0;
Damjan Marion2060db82018-03-04 17:37:15 +01001140
1141 ASSERT (bar <= 5);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001142
1143 error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001144
Damjan Marion2060db82018-03-04 17:37:15 +01001145 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001146 {
Damjan Marion2060db82018-03-04 17:37:15 +01001147 u8 *file_name;
1148 struct stat stat_buf;
1149 file_name = format (0, "%s/%U/resource%d%c", sysfs_pci_dev_path,
1150 format_vlib_pci_addr, &p->addr, bar, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001151
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001152 _fd = open ((char *) file_name, O_RDWR);
1153 if (_fd < 0)
Damjan Marion2060db82018-03-04 17:37:15 +01001154 {
1155 error = clib_error_return_unix (0, "open `%s'", file_name);
1156 vec_free (file_name);
1157 return error;
1158 }
1159
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001160 if (fstat (_fd, &stat_buf) < 0)
Damjan Marion2060db82018-03-04 17:37:15 +01001161 {
1162 error = clib_error_return_unix (0, "fstat `%s'", file_name);
1163 vec_free (file_name);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001164 close (_fd);
Damjan Marion2060db82018-03-04 17:37:15 +01001165 return error;
1166 }
1167
1168 vec_free (file_name);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001169 _size = stat_buf.st_size;
1170 _offset = 0;
Damjan Marion2060db82018-03-04 17:37:15 +01001171 }
1172 else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001173 {
Damjan Marion0792bb42020-05-25 19:31:42 +02001174 struct vfio_region_info *r;
1175 u32 sz = sizeof (struct vfio_region_info);
1176 again:
1177 r = clib_mem_alloc (sz);
1178 clib_memset (r, 0, sz);
1179 r->argsz = sz;
1180 r->index = bar;
1181 if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, r) < 0)
Damjan Marion2060db82018-03-04 17:37:15 +01001182 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) "
1183 "'%U'", format_vlib_pci_addr,
1184 &p->addr);
Damjan Marion0792bb42020-05-25 19:31:42 +02001185 if (sz != r->argsz)
1186 {
1187 sz = r->argsz;
1188 clib_mem_free (r);
1189 goto again;
1190 }
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001191 _fd = p->fd;
Damjan Marion0792bb42020-05-25 19:31:42 +02001192 _size = r->size;
1193 _offset = r->offset;
Damjan Marion00ea98a2023-07-28 13:19:49 +02001194 log_debug (p, "%s %U", __func__, format_vfio_region_info, r);
Damjan Marion0792bb42020-05-25 19:31:42 +02001195 clib_mem_free (r);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001196 }
Damjan Marion2060db82018-03-04 17:37:15 +01001197 else
1198 ASSERT (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001199
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001200 *fd = _fd;
1201 *size = _size;
1202 *offset = _offset;
1203
1204 return error;
1205}
1206
1207static clib_error_t *
1208vlib_pci_map_region_int (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1209 u32 bar, u8 * addr, void **result)
1210{
1211 linux_pci_device_t *p = linux_pci_get_device (h);
1212 int fd = -1;
1213 clib_error_t *error;
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001214 u64 size = 0, offset = 0;
Damjan Marion00ea98a2023-07-28 13:19:49 +02001215 vlib_pci_config_reg_command_t command;
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001216
Damjan Marion00ea98a2023-07-28 13:19:49 +02001217 log_debug (p, "map region %u to va %p", bar, addr);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001218
Damjan Marion00ea98a2023-07-28 13:19:49 +02001219 if ((error = vlib_pci_read_config_u16 (vm, h, 4, &command.as_u16)))
Mohammed Hawari70fc36f2020-10-21 16:41:30 +02001220 return error;
1221
Damjan Marion00ea98a2023-07-28 13:19:49 +02001222 if (!(command.mem_space))
Mohammed Hawari70fc36f2020-10-21 16:41:30 +02001223 {
Damjan Marion00ea98a2023-07-28 13:19:49 +02001224 log_debug (p, "setting memory enable bit");
1225 command.mem_space = 1;
1226 if ((error = vlib_pci_write_config_u16 (vm, h, 4, &command.as_u16)))
Mohammed Hawari70fc36f2020-10-21 16:41:30 +02001227 return error;
1228 }
1229
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001230 if ((error = vlib_pci_region (vm, h, bar, &fd, &size, &offset)))
1231 return error;
1232
Damjan Marion6bfd0762020-09-11 22:16:53 +02001233 *result = clib_mem_vm_map_shared (addr, size, fd, offset,
1234 "PCIe %U region %u", format_vlib_pci_addr,
1235 vlib_pci_get_addr (vm, h), bar);
1236 if (*result == CLIB_MEM_VM_MAP_FAILED)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001237 {
Damjan Marion2060db82018-03-04 17:37:15 +01001238 error = clib_error_return_unix (0, "mmap `BAR%u'", bar);
Dave Barachc72950e2020-05-04 14:47:45 -04001239 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO && (fd != -1))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001240 close (fd);
Damjan Marion2060db82018-03-04 17:37:15 +01001241 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001242 }
Damjan Marion2060db82018-03-04 17:37:15 +01001243
1244 /* *INDENT-OFF* */
1245 vec_validate_init_empty (p->regions, bar,
1246 (linux_pci_region_t) { .fd = -1});
1247 /* *INDENT-ON* */
1248 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1249 p->regions[bar].fd = fd;
1250 p->regions[bar].addr = *result;
1251 p->regions[bar].size = size;
1252 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001253}
1254
1255clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001256vlib_pci_map_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 resource,
1257 void **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001258{
Damjan Marion23227982018-10-22 13:38:57 +02001259 return (vlib_pci_map_region_int (vm, h, resource, 0 /* addr */ , result));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001260}
1261
1262clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001263vlib_pci_map_region_fixed (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1264 u32 resource, u8 * addr, void **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001265{
Damjan Marion23227982018-10-22 13:38:57 +02001266 return (vlib_pci_map_region_int (vm, h, resource, addr, result));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001267}
1268
Damjan Marionf313b742018-03-05 14:08:33 +01001269clib_error_t *
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001270vlib_pci_io_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 resource)
1271{
1272 linux_pci_device_t *p = linux_pci_get_device (h);
1273 clib_error_t *error = 0;
1274 int fd = -1;
1275 u64 size = 0, offset = 0;
1276
1277 if ((error = vlib_pci_region (vm, h, resource, &fd, &size, &offset)))
1278 return error;
1279
1280 p->io_fd = fd;
1281 p->io_offset = offset;
1282 return error;
1283}
1284
1285clib_error_t *
1286vlib_pci_read_write_io (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1287 vlib_read_or_write_t read_or_write,
1288 uword offset, void *data, u32 length)
1289{
1290 linux_pci_device_t *p = linux_pci_get_device (h);
1291 int n = 0;
1292
1293 if (read_or_write == VLIB_READ)
1294 n = pread (p->io_fd, data, length, p->io_offset + offset);
1295 else
1296 n = pwrite (p->io_fd, data, length, p->io_offset + offset);
1297
1298 if (n != length)
1299 return clib_error_return_unix (0, "%s",
1300 read_or_write == VLIB_READ
1301 ? "read" : "write");
1302 return 0;
1303}
1304
1305clib_error_t *
Damjan Marion68b4da62018-09-30 18:26:20 +02001306vlib_pci_map_dma (vlib_main_t * vm, vlib_pci_dev_handle_t h, void *ptr)
1307{
1308 linux_pci_device_t *p = linux_pci_get_device (h);
1309
1310 if (!p->supports_va_dma)
1311 return 0;
1312
1313 return vfio_map_physmem_page (vm, ptr);
1314}
1315
1316int
1317vlib_pci_supports_virtual_addr_dma (vlib_main_t * vm, vlib_pci_dev_handle_t h)
1318{
1319 linux_pci_device_t *p = linux_pci_get_device (h);
1320
1321 return p->supports_va_dma != 0;
1322}
1323
1324clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001325vlib_pci_device_open (vlib_main_t * vm, vlib_pci_addr_t * addr,
Damjan Marionf313b742018-03-05 14:08:33 +01001326 pci_device_id_t ids[], vlib_pci_dev_handle_t * handle)
1327{
1328 linux_pci_main_t *lpm = &linux_pci_main;
1329 vlib_pci_device_info_t *di;
1330 linux_pci_device_t *p;
1331 clib_error_t *err = 0;
1332 pci_device_id_t *i;
1333
Damjan Marion23227982018-10-22 13:38:57 +02001334 di = vlib_pci_get_device_info (vm, addr, &err);
Damjan Marionf313b742018-03-05 14:08:33 +01001335
1336 if (err)
1337 return err;
Damjan Marionf313b742018-03-05 14:08:33 +01001338
Damjan Marion00ea98a2023-07-28 13:19:49 +02001339 if (ids)
1340 {
1341 for (i = ids; i->vendor_id != 0; i++)
1342 if (i->vendor_id == di->vendor_id && i->device_id == di->device_id)
1343 break;
1344
1345 if (i->vendor_id == 0)
1346 {
1347 vlib_pci_free_device_info (di);
1348 return clib_error_return (0, "Wrong vendor or device id");
1349 }
1350 }
Damjan Marionf313b742018-03-05 14:08:33 +01001351
1352 pool_get (lpm->linux_pci_devices, p);
1353 p->handle = p - lpm->linux_pci_devices;
Damjan Marion23227982018-10-22 13:38:57 +02001354 p->addr.as_u32 = di->addr.as_u32;
Damjan Marionf313b742018-03-05 14:08:33 +01001355 p->intx_irq.fd = -1;
Mohsin Kazmi692f9b12019-04-02 11:09:49 +00001356 p->intx_irq.clib_file_index = -1;
Damjan Mariond2bfb782019-01-07 20:56:04 +01001357 p->numa_node = di->numa_node;
Mohsin Kazmi3d3b9552018-10-24 14:05:34 +02001358 /*
1359 * pci io bar read/write fd
1360 */
1361 p->io_fd = -1;
Damjan Marionf313b742018-03-05 14:08:33 +01001362
Damjan Marion00ea98a2023-07-28 13:19:49 +02001363 log_debug (p, "open vid:0x%04x did:0x%04x driver:%s iommu_group:%d",
1364 di->vendor_id, di->device_id, di->driver_name, di->iommu_group);
Damjan Marion23227982018-10-22 13:38:57 +02001365
Steven Luongab489922019-08-21 11:35:27 -07001366 if (clib_strncmp ("vfio-pci", (char *) di->driver_name, 8) == 0)
Damjan Marion23227982018-10-22 13:38:57 +02001367 err = add_device_vfio (vm, p, di, 0);
Steven Luongab489922019-08-21 11:35:27 -07001368 else if (clib_strncmp ("uio_pci_generic", (char *) di->driver_name, 8) == 0)
Damjan Marion23227982018-10-22 13:38:57 +02001369 err = add_device_uio (vm, p, di, 0);
Damjan Marionf313b742018-03-05 14:08:33 +01001370 else
Damjan Marion23227982018-10-22 13:38:57 +02001371 err = clib_error_create ("device not bound to 'vfio-pci' or "
1372 "'uio_pci_generic' kernel module");
Damjan Marionf313b742018-03-05 14:08:33 +01001373 if (err)
1374 goto error;
1375
1376 *handle = p->handle;
1377
1378error:
1379 vlib_pci_free_device_info (di);
1380 if (err)
1381 {
Damjan Marion00ea98a2023-07-28 13:19:49 +02001382 log_err (p, "%U", format_clib_error, err);
Dave Barachb7b92992018-10-17 10:38:51 -04001383 clib_memset (p, 0, sizeof (linux_pci_device_t));
Damjan Marionf313b742018-03-05 14:08:33 +01001384 pool_put (lpm->linux_pci_devices, p);
1385 }
1386
1387 return err;
1388}
1389
1390void
Damjan Marion23227982018-10-22 13:38:57 +02001391vlib_pci_device_close (vlib_main_t * vm, vlib_pci_dev_handle_t h)
Damjan Marionf313b742018-03-05 14:08:33 +01001392{
1393 linux_pci_main_t *lpm = &linux_pci_main;
1394 linux_pci_device_t *p = linux_pci_get_device (h);
1395 linux_pci_irq_t *irq;
1396 linux_pci_region_t *res;
1397 clib_error_t *err = 0;
1398
1399 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1400 {
1401 irq = &p->intx_irq;
Mohsin Kazmi692f9b12019-04-02 11:09:49 +00001402 if (irq->clib_file_index != -1)
1403 clib_file_del_by_index (&file_main, irq->clib_file_index);
Damjan Marionf313b742018-03-05 14:08:33 +01001404 close (p->config_fd);
Mohsin Kazmi3d3b9552018-10-24 14:05:34 +02001405 if (p->io_fd != -1)
1406 close (p->io_fd);
Damjan Marionf313b742018-03-05 14:08:33 +01001407 }
1408 else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
1409 {
1410 irq = &p->intx_irq;
1411 /* close INTx irqs */
1412 if (irq->fd != -1)
1413 {
Damjan Marion23227982018-10-22 13:38:57 +02001414 err = vfio_set_irqs (vm, p, VFIO_PCI_INTX_IRQ_INDEX, 0, 0,
Damjan Marionf313b742018-03-05 14:08:33 +01001415 VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1416 clib_error_free (err);
Mohsin Kazmi692f9b12019-04-02 11:09:49 +00001417 if (irq->clib_file_index != -1)
1418 clib_file_del_by_index (&file_main, irq->clib_file_index);
Damjan Marionf313b742018-03-05 14:08:33 +01001419 close (irq->fd);
1420 }
1421
1422 /* close MSI-X irqs */
1423 if (vec_len (p->msix_irqs))
1424 {
Damjan Marion23227982018-10-22 13:38:57 +02001425 err = vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, 0, 0,
Damjan Marionf313b742018-03-05 14:08:33 +01001426 VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1427 clib_error_free (err);
1428 /* *INDENT-OFF* */
1429 vec_foreach (irq, p->msix_irqs)
1430 {
1431 if (irq->fd == -1)
1432 continue;
1433 clib_file_del_by_index (&file_main, irq->clib_file_index);
1434 close (irq->fd);
1435 }
1436 /* *INDENT-ON* */
1437 vec_free (p->msix_irqs);
1438 }
1439 }
1440
1441 /* *INDENT-OFF* */
1442 vec_foreach (res, p->regions)
1443 {
1444 if (res->size == 0)
1445 continue;
Damjan Marion6bfd0762020-09-11 22:16:53 +02001446 clib_mem_vm_unmap (res->addr);
Damjan Marionf313b742018-03-05 14:08:33 +01001447 if (res->fd != -1)
1448 close (res->fd);
1449 }
1450 /* *INDENT-ON* */
1451 vec_free (p->regions);
1452
1453 close (p->fd);
Dave Barachb7b92992018-10-17 10:38:51 -04001454 clib_memset (p, 0, sizeof (linux_pci_device_t));
Damjan Marionf313b742018-03-05 14:08:33 +01001455 pool_put (lpm->linux_pci_devices, p);
1456}
1457
Dave Barach9b8ffd92016-07-08 08:13:45 -04001458void
Damjan Marion23227982018-10-22 13:38:57 +02001459init_device_from_registered (vlib_main_t * vm, vlib_pci_device_info_t * di)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001460{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001461 vlib_pci_main_t *pm = &pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +02001462 linux_pci_main_t *lpm = &linux_pci_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001463 pci_device_registration_t *r;
1464 pci_device_id_t *i;
Damjan Marioncef87f12017-10-05 15:32:41 +02001465 clib_error_t *err = 0;
1466 linux_pci_device_t *p;
1467
1468 pool_get (lpm->linux_pci_devices, p);
1469 p->handle = p - lpm->linux_pci_devices;
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001470 p->intx_irq.fd = -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001471
Damjan Marion5a206ea2016-05-12 22:11:03 +02001472 r = pm->pci_device_registrations;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001473
1474 while (r)
1475 {
1476 for (i = r->supported_devices; i->vendor_id != 0; i++)
Damjan Marion3a593822018-02-17 14:06:53 +01001477 if (i->vendor_id == di->vendor_id && i->device_id == di->device_id)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001478 {
Damjan Marioncef87f12017-10-05 15:32:41 +02001479 if (di->iommu_group != -1)
Damjan Marion23227982018-10-22 13:38:57 +02001480 err = add_device_vfio (vm, p, di, r);
Damjan Marioncef87f12017-10-05 15:32:41 +02001481 else
Damjan Marion23227982018-10-22 13:38:57 +02001482 err = add_device_uio (vm, p, di, r);
Damjan Marion5a206ea2016-05-12 22:11:03 +02001483
Damjan Marioncef87f12017-10-05 15:32:41 +02001484 if (err)
1485 clib_error_report (err);
1486 else
1487 return;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001488 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001489 r = r->next_registration;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001490 }
Damjan Marioncef87f12017-10-05 15:32:41 +02001491
Ed Warnickecb9cada2015-12-08 15:45:58 -07001492 /* No driver, close the PCI config-space FD */
Dave Barachb7b92992018-10-17 10:38:51 -04001493 clib_memset (p, 0, sizeof (linux_pci_device_t));
Damjan Marioncef87f12017-10-05 15:32:41 +02001494 pool_put (lpm->linux_pci_devices, p);
1495}
1496
1497static clib_error_t *
1498scan_pci_addr (void *arg, u8 * dev_dir_name, u8 * ignored)
1499{
1500 vlib_pci_addr_t addr, **addrv = arg;
1501 unformat_input_t input;
1502 clib_error_t *err = 0;
1503
1504 unformat_init_string (&input, (char *) dev_dir_name,
1505 vec_len (dev_dir_name));
1506
1507 if (!unformat (&input, "/sys/bus/pci/devices/%U",
1508 unformat_vlib_pci_addr, &addr))
1509 err = clib_error_return (0, "unformat error `%v`", dev_dir_name);
1510
1511 unformat_free (&input);
1512
1513 if (err)
1514 return err;
1515
1516 vec_add1 (*addrv, addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001517 return 0;
1518}
1519
Damjan Marioncef87f12017-10-05 15:32:41 +02001520static int
1521pci_addr_cmp (void *v1, void *v2)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001522{
Damjan Marioncef87f12017-10-05 15:32:41 +02001523 vlib_pci_addr_t *a1 = v1;
1524 vlib_pci_addr_t *a2 = v2;
1525
1526 if (a1->domain > a2->domain)
1527 return 1;
1528 if (a1->domain < a2->domain)
1529 return -1;
1530 if (a1->bus > a2->bus)
1531 return 1;
1532 if (a1->bus < a2->bus)
1533 return -1;
1534 if (a1->slot > a2->slot)
1535 return 1;
1536 if (a1->slot < a2->slot)
1537 return -1;
1538 if (a1->function > a2->function)
1539 return 1;
1540 if (a1->function < a2->function)
1541 return -1;
1542 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001543}
1544
Damjan Marioncef87f12017-10-05 15:32:41 +02001545vlib_pci_addr_t *
1546vlib_pci_get_all_dev_addrs ()
Ed Warnickecb9cada2015-12-08 15:45:58 -07001547{
Damjan Marioncef87f12017-10-05 15:32:41 +02001548 vlib_pci_addr_t *addrs = 0;
1549 clib_error_t *err;
1550 err = foreach_directory_file ((char *) sysfs_pci_dev_path, scan_pci_addr,
1551 &addrs, /* scan_dirs */ 0);
1552 if (err)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001553 {
Damjan Marioncef87f12017-10-05 15:32:41 +02001554 vec_free (addrs);
1555 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001556 }
1557
Damjan Marioncef87f12017-10-05 15:32:41 +02001558 vec_sort_with_function (addrs, pci_addr_cmp);
Damjan Mariona42cd342016-04-13 18:03:20 +02001559
Damjan Marioncef87f12017-10-05 15:32:41 +02001560 return addrs;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001561}
1562
Dave Barach9b8ffd92016-07-08 08:13:45 -04001563clib_error_t *
1564linux_pci_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001565{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001566 vlib_pci_main_t *pm = &pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +02001567 vlib_pci_addr_t *addr = 0, *addrs;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001568
1569 pm->vlib_main = vm;
1570
Dave Barach9b8ffd92016-07-08 08:13:45 -04001571 ASSERT (sizeof (vlib_pci_addr_t) == sizeof (u32));
Damjan Mariona42cd342016-04-13 18:03:20 +02001572
Damjan Marioncef87f12017-10-05 15:32:41 +02001573 addrs = vlib_pci_get_all_dev_addrs ();
1574 /* *INDENT-OFF* */
1575 vec_foreach (addr, addrs)
1576 {
1577 vlib_pci_device_info_t *d;
Damjan Marion23227982018-10-22 13:38:57 +02001578 if ((d = vlib_pci_get_device_info (vm, addr, 0)))
Damjan Marioncef87f12017-10-05 15:32:41 +02001579 {
Damjan Marion23227982018-10-22 13:38:57 +02001580 init_device_from_registered (vm, d);
Damjan Marioncef87f12017-10-05 15:32:41 +02001581 vlib_pci_free_device_info (d);
1582 }
1583 }
1584 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001585
Dave Barachf8d50682019-05-14 18:01:44 -04001586 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001587}
1588
Dave Barachf8d50682019-05-14 18:01:44 -04001589/* *INDENT-OFF* */
1590VLIB_INIT_FUNCTION (linux_pci_init) =
1591{
1592 .runs_after = VLIB_INITS("unix_input_init"),
1593};
1594/* *INDENT-ON* */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001595
1596/*
1597 * fd.io coding-style-patch-verification: ON
1598 *
1599 * Local Variables:
1600 * eval: (c-set-style "gnu")
1601 * End:
1602 */