blob: 08923bebcdf9da0050f5511a0a4da7548f00826b [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
Damjan Marion599a16b2018-03-04 19:35:23 +0100901 vec_validate_init_empty (p->msix_irqs, start + count - 1, (linux_pci_irq_t)
902 { .fd = -1});
Damjan Marion599a16b2018-03-04 19:35:23 +0100903
904 for (i = start; i < start + count; i++)
905 {
906 clib_file_t t = { 0 };
907 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
908 ASSERT (irq->fd == -1);
909
910 irq->fd = eventfd (0, EFD_NONBLOCK);
911 if (irq->fd == -1)
912 {
913 err = clib_error_return_unix (0, "eventfd");
914 goto error;
915 }
916
917 t.read_function = linux_pci_vfio_msix_read_ready;
918 t.file_descriptor = irq->fd;
919 t.error_function = linux_pci_vfio_error_ready;
920 t.private_data = p->handle << 16 | i;
921 t.description = format (0, "PCI %U MSI-X #%u", format_vlib_pci_addr,
922 &p->addr, i);
923 irq->clib_file_index = clib_file_add (&file_main, &t);
924 irq->msix_handler = msix_handler;
925 }
926
927 return 0;
928
929error:
930 while (i-- > start)
931 {
932 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
933 if (irq->fd != -1)
934 {
935 clib_file_del_by_index (&file_main, irq->clib_file_index);
936 close (irq->fd);
937 irq->fd = -1;
938 }
939 }
940 return err;
941}
942
943clib_error_t *
Damjan Marion38c61912023-10-17 16:06:26 +0000944vlib_pci_unregister_msix_handler (vlib_main_t *vm, vlib_pci_dev_handle_t h,
945 u32 start, u32 count)
946{
947 clib_error_t *err = 0;
948 linux_pci_device_t *p = linux_pci_get_device (h);
949 u32 i;
950
951 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
952 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
953 "support");
954
955 for (i = start; i < start + count; i++)
956 {
957 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
958
959 if (irq->fd != -1)
960 {
961 clib_file_del_by_index (&file_main, irq->clib_file_index);
962 close (irq->fd);
963 irq->fd = -1;
964 }
965 }
966
967 return err;
968}
969
970clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200971vlib_pci_enable_msix_irq (vlib_main_t * vm, vlib_pci_dev_handle_t h,
972 u16 start, u16 count)
Damjan Marion599a16b2018-03-04 19:35:23 +0100973{
974 linux_pci_device_t *p = linux_pci_get_device (h);
975 int fds[count];
976 int i;
977
978 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
979 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
980 "support");
981
Damjan Marion60529a82023-11-06 00:06:26 +0000982 for (i = 0; i < count; i++)
Damjan Marion599a16b2018-03-04 19:35:23 +0100983 {
Damjan Marion60529a82023-11-06 00:06:26 +0000984 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, start + i);
Damjan Marion599a16b2018-03-04 19:35:23 +0100985 fds[i] = irq->fd;
986 }
987
Damjan Marion23227982018-10-22 13:38:57 +0200988 return vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
Damjan Marion599a16b2018-03-04 19:35:23 +0100989 VFIO_IRQ_SET_ACTION_TRIGGER, fds);
990}
991
Damjan Marion9c9490c2020-10-07 20:00:39 +0200992uword
993vlib_pci_get_msix_file_index (vlib_main_t * vm, vlib_pci_dev_handle_t h,
994 u16 index)
995{
996 linux_pci_device_t *p = linux_pci_get_device (h);
997 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, index);
998 if (irq->fd == -1)
999 return ~0;
1000 return irq->clib_file_index;
1001}
1002
Damjan Marion599a16b2018-03-04 19:35:23 +01001003clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001004vlib_pci_disable_msix_irq (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1005 u16 start, u16 count)
Damjan Marion599a16b2018-03-04 19:35:23 +01001006{
1007 linux_pci_device_t *p = linux_pci_get_device (h);
1008 int i, fds[count];
1009
1010 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
1011 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
1012 "support");
1013
1014 for (i = start; i < start + count; i++)
1015 fds[i] = -1;
1016
Damjan Marion23227982018-10-22 13:38:57 +02001017 return vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
Damjan Marion599a16b2018-03-04 19:35:23 +01001018 VFIO_IRQ_SET_ACTION_TRIGGER, fds);
1019}
1020
Damjan Marioncef87f12017-10-05 15:32:41 +02001021static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001022add_device_vfio (vlib_main_t * vm, linux_pci_device_t * p,
1023 vlib_pci_device_info_t * di, pci_device_registration_t * r)
Damjan Marioncef87f12017-10-05 15:32:41 +02001024{
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001025 linux_pci_main_t *lpm = &linux_pci_main;
Damjan Marionf9a968e2018-02-25 23:26:22 +01001026 struct vfio_device_info device_info = { 0 };
Damjan Marionf313b742018-03-05 14:08:33 +01001027 struct vfio_region_info reg = { 0 };
Damjan Marioncef87f12017-10-05 15:32:41 +02001028 clib_error_t *err = 0;
1029 u8 *s = 0;
Damjan Marion23227982018-10-22 13:38:57 +02001030 int is_noiommu;
Damjan Marioncef87f12017-10-05 15:32:41 +02001031
Damjan Marion2060db82018-03-04 17:37:15 +01001032 p->type = LINUX_PCI_DEVICE_TYPE_VFIO;
Damjan Marioncef87f12017-10-05 15:32:41 +02001033
Damjan Marion23227982018-10-22 13:38:57 +02001034 if ((err = linux_vfio_group_get_device_fd (&p->addr, &p->fd, &is_noiommu)))
Damjan Marioncef87f12017-10-05 15:32:41 +02001035 return err;
1036
Damjan Marion23227982018-10-22 13:38:57 +02001037 if (is_noiommu == 0)
1038 p->supports_va_dma = 1;
1039
Damjan Marioncef87f12017-10-05 15:32:41 +02001040 device_info.argsz = sizeof (device_info);
Damjan Marionf9a968e2018-02-25 23:26:22 +01001041 if (ioctl (p->fd, VFIO_DEVICE_GET_INFO, &device_info) < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +02001042 {
1043 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
1044 format_vlib_pci_addr, &di->addr);
1045 goto error;
1046 }
1047
Damjan Marionf313b742018-03-05 14:08:33 +01001048 reg.argsz = sizeof (struct vfio_region_info);
1049 reg.index = VFIO_PCI_CONFIG_REGION_INDEX;
1050 if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, &reg) < 0)
1051 {
1052 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
1053 format_vlib_pci_addr, &di->addr);
1054 goto error;
1055 }
Damjan Marion23227982018-10-22 13:38:57 +02001056
Damjan Marion00ea98a2023-07-28 13:19:49 +02001057 log_debug (p, "%s %U", __func__, format_vfio_region_info, &reg);
Damjan Marion23227982018-10-22 13:38:57 +02001058
Damjan Marionf313b742018-03-05 14:08:33 +01001059 p->config_offset = reg.offset;
1060 p->config_fd = p->fd;
1061
Damjan Marioncef87f12017-10-05 15:32:41 +02001062 /* reset if device supports it */
1063 if (device_info.flags & VFIO_DEVICE_FLAGS_RESET)
Damjan Marionf9a968e2018-02-25 23:26:22 +01001064 if (ioctl (p->fd, VFIO_DEVICE_RESET) < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +02001065 {
1066 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_RESET) '%U'",
1067 format_vlib_pci_addr, &di->addr);
1068 goto error;
1069 }
1070
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001071 if (r && r->interrupt_handler)
Damjan Marioncef87f12017-10-05 15:32:41 +02001072 {
Damjan Marion23227982018-10-22 13:38:57 +02001073 vlib_pci_register_intx_handler (vm, p->handle, r->interrupt_handler);
1074 linux_pci_vfio_unmask_intx (vm, p);
Damjan Marioncef87f12017-10-05 15:32:41 +02001075 }
1076
Damjan Marion68b4da62018-09-30 18:26:20 +02001077 if (p->supports_va_dma)
1078 {
1079 vlib_buffer_pool_t *bp;
Damjan Mariond50e3472019-01-20 00:03:56 +01001080 vec_foreach (bp, vm->buffer_main->buffer_pools)
Damjan Marion68b4da62018-09-30 18:26:20 +02001081 {
1082 u32 i;
1083 vlib_physmem_map_t *pm;
1084 pm = vlib_physmem_get_map (vm, bp->physmem_map_index);
1085 for (i = 0; i < pm->n_pages; i++)
1086 vfio_map_physmem_page (vm, pm->base + (i << pm->log2_page_size));
1087 }
Damjan Marion68b4da62018-09-30 18:26:20 +02001088 }
1089
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001090 if (r && r->init_function)
1091 err = r->init_function (lpm->vlib_main, p->handle);
Damjan Marioncef87f12017-10-05 15:32:41 +02001092
1093error:
1094 vec_free (s);
1095 if (err)
1096 {
Damjan Marionf9a968e2018-02-25 23:26:22 +01001097 if (p->fd != -1)
1098 close (p->fd);
Chris Luke30684ac2018-03-29 12:56:58 -07001099 if (p->config_fd != -1 && p->config_fd != p->fd)
Damjan Marion96504182017-12-10 23:54:46 +01001100 close (p->config_fd);
Chris Luke30684ac2018-03-29 12:56:58 -07001101 p->config_fd = p->fd = -1;
Damjan Marioncef87f12017-10-05 15:32:41 +02001102 }
1103 return err;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001104}
1105
1106/* Configuration space read/write. */
1107clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001108vlib_pci_read_write_config (vlib_main_t * vm, vlib_pci_dev_handle_t h,
Damjan Marion5a206ea2016-05-12 22:11:03 +02001109 vlib_read_or_write_t read_or_write,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001110 uword address, void *data, u32 n_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001111{
Damjan Marioncef87f12017-10-05 15:32:41 +02001112 linux_pci_device_t *p = linux_pci_get_device (h);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001113 int n;
1114
Ed Warnickecb9cada2015-12-08 15:45:58 -07001115 if (read_or_write == VLIB_READ)
Damjan Marionf9a968e2018-02-25 23:26:22 +01001116 n = pread (p->config_fd, data, n_bytes, p->config_offset + address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001117 else
Damjan Marionf9a968e2018-02-25 23:26:22 +01001118 n = pwrite (p->config_fd, data, n_bytes, p->config_offset + address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001119
1120 if (n != n_bytes)
1121 return clib_error_return_unix (0, "%s",
1122 read_or_write == VLIB_READ
1123 ? "read" : "write");
1124
1125 return 0;
1126}
1127
1128static clib_error_t *
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001129vlib_pci_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 bar, int *fd,
1130 u64 * size, u64 * offset)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001131{
Damjan Marioncef87f12017-10-05 15:32:41 +02001132 linux_pci_device_t *p = linux_pci_get_device (h);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001133 clib_error_t *error = 0;
1134 int _fd = -1;
1135 u64 _size = 0, _offset = 0;
Damjan Marion2060db82018-03-04 17:37:15 +01001136
1137 ASSERT (bar <= 5);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001138
1139 error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001140
Damjan Marion2060db82018-03-04 17:37:15 +01001141 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001142 {
Damjan Marion2060db82018-03-04 17:37:15 +01001143 u8 *file_name;
1144 struct stat stat_buf;
1145 file_name = format (0, "%s/%U/resource%d%c", sysfs_pci_dev_path,
1146 format_vlib_pci_addr, &p->addr, bar, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001147
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001148 _fd = open ((char *) file_name, O_RDWR);
1149 if (_fd < 0)
Damjan Marion2060db82018-03-04 17:37:15 +01001150 {
1151 error = clib_error_return_unix (0, "open `%s'", file_name);
1152 vec_free (file_name);
1153 return error;
1154 }
1155
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001156 if (fstat (_fd, &stat_buf) < 0)
Damjan Marion2060db82018-03-04 17:37:15 +01001157 {
1158 error = clib_error_return_unix (0, "fstat `%s'", file_name);
1159 vec_free (file_name);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001160 close (_fd);
Damjan Marion2060db82018-03-04 17:37:15 +01001161 return error;
1162 }
1163
1164 vec_free (file_name);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001165 _size = stat_buf.st_size;
1166 _offset = 0;
Damjan Marion2060db82018-03-04 17:37:15 +01001167 }
1168 else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001169 {
Damjan Marion0792bb42020-05-25 19:31:42 +02001170 struct vfio_region_info *r;
1171 u32 sz = sizeof (struct vfio_region_info);
1172 again:
1173 r = clib_mem_alloc (sz);
1174 clib_memset (r, 0, sz);
1175 r->argsz = sz;
1176 r->index = bar;
1177 if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, r) < 0)
Damjan Marion2060db82018-03-04 17:37:15 +01001178 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) "
1179 "'%U'", format_vlib_pci_addr,
1180 &p->addr);
Damjan Marion0792bb42020-05-25 19:31:42 +02001181 if (sz != r->argsz)
1182 {
1183 sz = r->argsz;
1184 clib_mem_free (r);
1185 goto again;
1186 }
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001187 _fd = p->fd;
Damjan Marion0792bb42020-05-25 19:31:42 +02001188 _size = r->size;
1189 _offset = r->offset;
Damjan Marion00ea98a2023-07-28 13:19:49 +02001190 log_debug (p, "%s %U", __func__, format_vfio_region_info, r);
Damjan Marion0792bb42020-05-25 19:31:42 +02001191 clib_mem_free (r);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001192 }
Damjan Marion2060db82018-03-04 17:37:15 +01001193 else
1194 ASSERT (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001195
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001196 *fd = _fd;
1197 *size = _size;
1198 *offset = _offset;
1199
1200 return error;
1201}
1202
1203static clib_error_t *
1204vlib_pci_map_region_int (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1205 u32 bar, u8 * addr, void **result)
1206{
1207 linux_pci_device_t *p = linux_pci_get_device (h);
1208 int fd = -1;
1209 clib_error_t *error;
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001210 u64 size = 0, offset = 0;
Damjan Marion00ea98a2023-07-28 13:19:49 +02001211 vlib_pci_config_reg_command_t command;
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001212
Damjan Marion00ea98a2023-07-28 13:19:49 +02001213 log_debug (p, "map region %u to va %p", bar, addr);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001214
Damjan Marion00ea98a2023-07-28 13:19:49 +02001215 if ((error = vlib_pci_read_config_u16 (vm, h, 4, &command.as_u16)))
Mohammed Hawari70fc36f2020-10-21 16:41:30 +02001216 return error;
1217
Damjan Marion00ea98a2023-07-28 13:19:49 +02001218 if (!(command.mem_space))
Mohammed Hawari70fc36f2020-10-21 16:41:30 +02001219 {
Damjan Marion00ea98a2023-07-28 13:19:49 +02001220 log_debug (p, "setting memory enable bit");
1221 command.mem_space = 1;
1222 if ((error = vlib_pci_write_config_u16 (vm, h, 4, &command.as_u16)))
Mohammed Hawari70fc36f2020-10-21 16:41:30 +02001223 return error;
1224 }
1225
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001226 if ((error = vlib_pci_region (vm, h, bar, &fd, &size, &offset)))
1227 return error;
1228
Damjan Marion6bfd0762020-09-11 22:16:53 +02001229 *result = clib_mem_vm_map_shared (addr, size, fd, offset,
1230 "PCIe %U region %u", format_vlib_pci_addr,
1231 vlib_pci_get_addr (vm, h), bar);
1232 if (*result == CLIB_MEM_VM_MAP_FAILED)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001233 {
Damjan Marion2060db82018-03-04 17:37:15 +01001234 error = clib_error_return_unix (0, "mmap `BAR%u'", bar);
Dave Barachc72950e2020-05-04 14:47:45 -04001235 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO && (fd != -1))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001236 close (fd);
Damjan Marion2060db82018-03-04 17:37:15 +01001237 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001238 }
Damjan Marion2060db82018-03-04 17:37:15 +01001239
Damjan Marion2060db82018-03-04 17:37:15 +01001240 vec_validate_init_empty (p->regions, bar,
1241 (linux_pci_region_t) { .fd = -1});
Damjan Marion2060db82018-03-04 17:37:15 +01001242 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1243 p->regions[bar].fd = fd;
1244 p->regions[bar].addr = *result;
1245 p->regions[bar].size = size;
1246 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001247}
1248
1249clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001250vlib_pci_map_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 resource,
1251 void **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001252{
Damjan Marion23227982018-10-22 13:38:57 +02001253 return (vlib_pci_map_region_int (vm, h, resource, 0 /* addr */ , result));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001254}
1255
1256clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001257vlib_pci_map_region_fixed (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1258 u32 resource, u8 * addr, void **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001259{
Damjan Marion23227982018-10-22 13:38:57 +02001260 return (vlib_pci_map_region_int (vm, h, resource, addr, result));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001261}
1262
Damjan Marionf313b742018-03-05 14:08:33 +01001263clib_error_t *
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001264vlib_pci_io_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 resource)
1265{
1266 linux_pci_device_t *p = linux_pci_get_device (h);
1267 clib_error_t *error = 0;
1268 int fd = -1;
1269 u64 size = 0, offset = 0;
1270
1271 if ((error = vlib_pci_region (vm, h, resource, &fd, &size, &offset)))
1272 return error;
1273
1274 p->io_fd = fd;
1275 p->io_offset = offset;
1276 return error;
1277}
1278
1279clib_error_t *
1280vlib_pci_read_write_io (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1281 vlib_read_or_write_t read_or_write,
1282 uword offset, void *data, u32 length)
1283{
1284 linux_pci_device_t *p = linux_pci_get_device (h);
1285 int n = 0;
1286
1287 if (read_or_write == VLIB_READ)
1288 n = pread (p->io_fd, data, length, p->io_offset + offset);
1289 else
1290 n = pwrite (p->io_fd, data, length, p->io_offset + offset);
1291
1292 if (n != length)
1293 return clib_error_return_unix (0, "%s",
1294 read_or_write == VLIB_READ
1295 ? "read" : "write");
1296 return 0;
1297}
1298
1299clib_error_t *
Damjan Marion68b4da62018-09-30 18:26:20 +02001300vlib_pci_map_dma (vlib_main_t * vm, vlib_pci_dev_handle_t h, void *ptr)
1301{
1302 linux_pci_device_t *p = linux_pci_get_device (h);
1303
1304 if (!p->supports_va_dma)
1305 return 0;
1306
1307 return vfio_map_physmem_page (vm, ptr);
1308}
1309
1310int
1311vlib_pci_supports_virtual_addr_dma (vlib_main_t * vm, vlib_pci_dev_handle_t h)
1312{
1313 linux_pci_device_t *p = linux_pci_get_device (h);
1314
1315 return p->supports_va_dma != 0;
1316}
1317
1318clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001319vlib_pci_device_open (vlib_main_t * vm, vlib_pci_addr_t * addr,
Damjan Marionf313b742018-03-05 14:08:33 +01001320 pci_device_id_t ids[], vlib_pci_dev_handle_t * handle)
1321{
1322 linux_pci_main_t *lpm = &linux_pci_main;
1323 vlib_pci_device_info_t *di;
1324 linux_pci_device_t *p;
1325 clib_error_t *err = 0;
1326 pci_device_id_t *i;
1327
Damjan Marion23227982018-10-22 13:38:57 +02001328 di = vlib_pci_get_device_info (vm, addr, &err);
Damjan Marionf313b742018-03-05 14:08:33 +01001329
1330 if (err)
1331 return err;
Damjan Marionf313b742018-03-05 14:08:33 +01001332
Damjan Marion00ea98a2023-07-28 13:19:49 +02001333 if (ids)
1334 {
1335 for (i = ids; i->vendor_id != 0; i++)
1336 if (i->vendor_id == di->vendor_id && i->device_id == di->device_id)
1337 break;
1338
1339 if (i->vendor_id == 0)
1340 {
1341 vlib_pci_free_device_info (di);
1342 return clib_error_return (0, "Wrong vendor or device id");
1343 }
1344 }
Damjan Marionf313b742018-03-05 14:08:33 +01001345
1346 pool_get (lpm->linux_pci_devices, p);
1347 p->handle = p - lpm->linux_pci_devices;
Damjan Marion23227982018-10-22 13:38:57 +02001348 p->addr.as_u32 = di->addr.as_u32;
Damjan Marionf313b742018-03-05 14:08:33 +01001349 p->intx_irq.fd = -1;
Mohsin Kazmi692f9b12019-04-02 11:09:49 +00001350 p->intx_irq.clib_file_index = -1;
Damjan Mariond2bfb782019-01-07 20:56:04 +01001351 p->numa_node = di->numa_node;
Mohsin Kazmi3d3b9552018-10-24 14:05:34 +02001352 /*
1353 * pci io bar read/write fd
1354 */
1355 p->io_fd = -1;
Damjan Marionf313b742018-03-05 14:08:33 +01001356
Damjan Marion00ea98a2023-07-28 13:19:49 +02001357 log_debug (p, "open vid:0x%04x did:0x%04x driver:%s iommu_group:%d",
1358 di->vendor_id, di->device_id, di->driver_name, di->iommu_group);
Damjan Marion23227982018-10-22 13:38:57 +02001359
Steven Luongab489922019-08-21 11:35:27 -07001360 if (clib_strncmp ("vfio-pci", (char *) di->driver_name, 8) == 0)
Damjan Marion23227982018-10-22 13:38:57 +02001361 err = add_device_vfio (vm, p, di, 0);
Steven Luongab489922019-08-21 11:35:27 -07001362 else if (clib_strncmp ("uio_pci_generic", (char *) di->driver_name, 8) == 0)
Damjan Marion23227982018-10-22 13:38:57 +02001363 err = add_device_uio (vm, p, di, 0);
Damjan Marionf313b742018-03-05 14:08:33 +01001364 else
Damjan Marion23227982018-10-22 13:38:57 +02001365 err = clib_error_create ("device not bound to 'vfio-pci' or "
1366 "'uio_pci_generic' kernel module");
Damjan Marionf313b742018-03-05 14:08:33 +01001367 if (err)
1368 goto error;
1369
1370 *handle = p->handle;
1371
1372error:
1373 vlib_pci_free_device_info (di);
1374 if (err)
1375 {
Damjan Marion00ea98a2023-07-28 13:19:49 +02001376 log_err (p, "%U", format_clib_error, err);
Dave Barachb7b92992018-10-17 10:38:51 -04001377 clib_memset (p, 0, sizeof (linux_pci_device_t));
Damjan Marionf313b742018-03-05 14:08:33 +01001378 pool_put (lpm->linux_pci_devices, p);
1379 }
1380
1381 return err;
1382}
1383
1384void
Damjan Marion23227982018-10-22 13:38:57 +02001385vlib_pci_device_close (vlib_main_t * vm, vlib_pci_dev_handle_t h)
Damjan Marionf313b742018-03-05 14:08:33 +01001386{
1387 linux_pci_main_t *lpm = &linux_pci_main;
1388 linux_pci_device_t *p = linux_pci_get_device (h);
1389 linux_pci_irq_t *irq;
1390 linux_pci_region_t *res;
1391 clib_error_t *err = 0;
1392
1393 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1394 {
1395 irq = &p->intx_irq;
Mohsin Kazmi692f9b12019-04-02 11:09:49 +00001396 if (irq->clib_file_index != -1)
1397 clib_file_del_by_index (&file_main, irq->clib_file_index);
Damjan Marionf313b742018-03-05 14:08:33 +01001398 close (p->config_fd);
Mohsin Kazmi3d3b9552018-10-24 14:05:34 +02001399 if (p->io_fd != -1)
1400 close (p->io_fd);
Damjan Marionf313b742018-03-05 14:08:33 +01001401 }
1402 else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
1403 {
1404 irq = &p->intx_irq;
1405 /* close INTx irqs */
1406 if (irq->fd != -1)
1407 {
Damjan Marion23227982018-10-22 13:38:57 +02001408 err = vfio_set_irqs (vm, p, VFIO_PCI_INTX_IRQ_INDEX, 0, 0,
Damjan Marionf313b742018-03-05 14:08:33 +01001409 VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1410 clib_error_free (err);
Mohsin Kazmi692f9b12019-04-02 11:09:49 +00001411 if (irq->clib_file_index != -1)
1412 clib_file_del_by_index (&file_main, irq->clib_file_index);
Damjan Marionf313b742018-03-05 14:08:33 +01001413 close (irq->fd);
1414 }
1415
1416 /* close MSI-X irqs */
1417 if (vec_len (p->msix_irqs))
1418 {
Damjan Marion23227982018-10-22 13:38:57 +02001419 err = vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, 0, 0,
Damjan Marionf313b742018-03-05 14:08:33 +01001420 VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1421 clib_error_free (err);
Damjan Marionf313b742018-03-05 14:08:33 +01001422 vec_foreach (irq, p->msix_irqs)
1423 {
1424 if (irq->fd == -1)
1425 continue;
1426 clib_file_del_by_index (&file_main, irq->clib_file_index);
1427 close (irq->fd);
1428 }
Damjan Marionf313b742018-03-05 14:08:33 +01001429 vec_free (p->msix_irqs);
1430 }
1431 }
1432
Damjan Marionf313b742018-03-05 14:08:33 +01001433 vec_foreach (res, p->regions)
1434 {
1435 if (res->size == 0)
1436 continue;
Damjan Marion6bfd0762020-09-11 22:16:53 +02001437 clib_mem_vm_unmap (res->addr);
Damjan Marionf313b742018-03-05 14:08:33 +01001438 if (res->fd != -1)
1439 close (res->fd);
1440 }
Damjan Marionf313b742018-03-05 14:08:33 +01001441 vec_free (p->regions);
1442
1443 close (p->fd);
Dave Barachb7b92992018-10-17 10:38:51 -04001444 clib_memset (p, 0, sizeof (linux_pci_device_t));
Damjan Marionf313b742018-03-05 14:08:33 +01001445 pool_put (lpm->linux_pci_devices, p);
1446}
1447
Dave Barach9b8ffd92016-07-08 08:13:45 -04001448void
Damjan Marion23227982018-10-22 13:38:57 +02001449init_device_from_registered (vlib_main_t * vm, vlib_pci_device_info_t * di)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001450{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001451 vlib_pci_main_t *pm = &pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +02001452 linux_pci_main_t *lpm = &linux_pci_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001453 pci_device_registration_t *r;
1454 pci_device_id_t *i;
Damjan Marioncef87f12017-10-05 15:32:41 +02001455 clib_error_t *err = 0;
1456 linux_pci_device_t *p;
1457
1458 pool_get (lpm->linux_pci_devices, p);
1459 p->handle = p - lpm->linux_pci_devices;
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001460 p->intx_irq.fd = -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001461
Damjan Marion5a206ea2016-05-12 22:11:03 +02001462 r = pm->pci_device_registrations;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001463
1464 while (r)
1465 {
1466 for (i = r->supported_devices; i->vendor_id != 0; i++)
Damjan Marion3a593822018-02-17 14:06:53 +01001467 if (i->vendor_id == di->vendor_id && i->device_id == di->device_id)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001468 {
Damjan Marioncef87f12017-10-05 15:32:41 +02001469 if (di->iommu_group != -1)
Damjan Marion23227982018-10-22 13:38:57 +02001470 err = add_device_vfio (vm, p, di, r);
Damjan Marioncef87f12017-10-05 15:32:41 +02001471 else
Damjan Marion23227982018-10-22 13:38:57 +02001472 err = add_device_uio (vm, p, di, r);
Damjan Marion5a206ea2016-05-12 22:11:03 +02001473
Damjan Marioncef87f12017-10-05 15:32:41 +02001474 if (err)
1475 clib_error_report (err);
1476 else
1477 return;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001478 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001479 r = r->next_registration;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001480 }
Damjan Marioncef87f12017-10-05 15:32:41 +02001481
Ed Warnickecb9cada2015-12-08 15:45:58 -07001482 /* No driver, close the PCI config-space FD */
Dave Barachb7b92992018-10-17 10:38:51 -04001483 clib_memset (p, 0, sizeof (linux_pci_device_t));
Damjan Marioncef87f12017-10-05 15:32:41 +02001484 pool_put (lpm->linux_pci_devices, p);
1485}
1486
1487static clib_error_t *
1488scan_pci_addr (void *arg, u8 * dev_dir_name, u8 * ignored)
1489{
1490 vlib_pci_addr_t addr, **addrv = arg;
1491 unformat_input_t input;
1492 clib_error_t *err = 0;
1493
1494 unformat_init_string (&input, (char *) dev_dir_name,
1495 vec_len (dev_dir_name));
1496
1497 if (!unformat (&input, "/sys/bus/pci/devices/%U",
1498 unformat_vlib_pci_addr, &addr))
1499 err = clib_error_return (0, "unformat error `%v`", dev_dir_name);
1500
1501 unformat_free (&input);
1502
1503 if (err)
1504 return err;
1505
1506 vec_add1 (*addrv, addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001507 return 0;
1508}
1509
Damjan Marioncef87f12017-10-05 15:32:41 +02001510static int
1511pci_addr_cmp (void *v1, void *v2)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001512{
Damjan Marioncef87f12017-10-05 15:32:41 +02001513 vlib_pci_addr_t *a1 = v1;
1514 vlib_pci_addr_t *a2 = v2;
1515
1516 if (a1->domain > a2->domain)
1517 return 1;
1518 if (a1->domain < a2->domain)
1519 return -1;
1520 if (a1->bus > a2->bus)
1521 return 1;
1522 if (a1->bus < a2->bus)
1523 return -1;
1524 if (a1->slot > a2->slot)
1525 return 1;
1526 if (a1->slot < a2->slot)
1527 return -1;
1528 if (a1->function > a2->function)
1529 return 1;
1530 if (a1->function < a2->function)
1531 return -1;
1532 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001533}
1534
Damjan Marioncef87f12017-10-05 15:32:41 +02001535vlib_pci_addr_t *
1536vlib_pci_get_all_dev_addrs ()
Ed Warnickecb9cada2015-12-08 15:45:58 -07001537{
Damjan Marioncef87f12017-10-05 15:32:41 +02001538 vlib_pci_addr_t *addrs = 0;
1539 clib_error_t *err;
1540 err = foreach_directory_file ((char *) sysfs_pci_dev_path, scan_pci_addr,
1541 &addrs, /* scan_dirs */ 0);
1542 if (err)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001543 {
Damjan Marioncef87f12017-10-05 15:32:41 +02001544 vec_free (addrs);
1545 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001546 }
1547
Damjan Marioncef87f12017-10-05 15:32:41 +02001548 vec_sort_with_function (addrs, pci_addr_cmp);
Damjan Mariona42cd342016-04-13 18:03:20 +02001549
Damjan Marioncef87f12017-10-05 15:32:41 +02001550 return addrs;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001551}
1552
Dave Barach9b8ffd92016-07-08 08:13:45 -04001553clib_error_t *
1554linux_pci_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001555{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001556 vlib_pci_main_t *pm = &pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +02001557 vlib_pci_addr_t *addr = 0, *addrs;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001558
1559 pm->vlib_main = vm;
1560
Dave Barach9b8ffd92016-07-08 08:13:45 -04001561 ASSERT (sizeof (vlib_pci_addr_t) == sizeof (u32));
Damjan Mariona42cd342016-04-13 18:03:20 +02001562
Damjan Marioncef87f12017-10-05 15:32:41 +02001563 addrs = vlib_pci_get_all_dev_addrs ();
Damjan Marioncef87f12017-10-05 15:32:41 +02001564 vec_foreach (addr, addrs)
1565 {
1566 vlib_pci_device_info_t *d;
Damjan Marion23227982018-10-22 13:38:57 +02001567 if ((d = vlib_pci_get_device_info (vm, addr, 0)))
Damjan Marioncef87f12017-10-05 15:32:41 +02001568 {
Damjan Marion23227982018-10-22 13:38:57 +02001569 init_device_from_registered (vm, d);
Damjan Marioncef87f12017-10-05 15:32:41 +02001570 vlib_pci_free_device_info (d);
1571 }
1572 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001573
Dave Barachf8d50682019-05-14 18:01:44 -04001574 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001575}
1576
Dave Barachf8d50682019-05-14 18:01:44 -04001577VLIB_INIT_FUNCTION (linux_pci_init) =
1578{
1579 .runs_after = VLIB_INITS("unix_input_init"),
1580};
Dave Barach9b8ffd92016-07-08 08:13:45 -04001581
1582/*
1583 * fd.io coding-style-patch-verification: ON
1584 *
1585 * Local Variables:
1586 * eval: (c-set-style "gnu")
1587 * End:
1588 */