blob: 83e9dde2f1617eff2c86356621a04214af8729fa [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
Damjan Marion5a206ea2016-05-12 22:11:03 +02002 * Copyright (c) 2016 Cisco and/or its affiliates.
Ed Warnickecb9cada2015-12-08 15:45:58 -07003 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15/*
16 * pci.c: Linux user space PCI bus management.
17 *
18 * Copyright (c) 2008 Eliot Dresselhaus
19 *
20 * Permission is hereby granted, free of charge, to any person obtaining
21 * a copy of this software and associated documentation files (the
22 * "Software"), to deal in the Software without restriction, including
23 * without limitation the rights to use, copy, modify, merge, publish,
24 * distribute, sublicense, and/or sell copies of the Software, and to
25 * permit persons to whom the Software is furnished to do so, subject to
26 * the following conditions:
27 *
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
30 *
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 */
39
Damjan Marion01914ce2017-09-14 19:04:50 +020040#include <vppinfra/linux/sysfs.h>
41
Ed Warnickecb9cada2015-12-08 15:45:58 -070042#include <vlib/vlib.h>
43#include <vlib/pci/pci.h>
44#include <vlib/unix/unix.h>
Damjan Marion1ba0fa42018-03-04 17:19:08 +010045#include <vlib/linux/vfio.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070046
47#include <sys/types.h>
48#include <sys/stat.h>
49#include <fcntl.h>
50#include <dirent.h>
Damjan Mariona42cd342016-04-13 18:03:20 +020051#include <sys/ioctl.h>
52#include <net/if.h>
53#include <linux/ethtool.h>
54#include <linux/sockios.h>
Damjan Marioncef87f12017-10-05 15:32:41 +020055#include <linux/vfio.h>
56#include <sys/eventfd.h>
57
Ray Kinsella5714a492021-11-02 13:33:44 +000058#define SYSFS_DEVICES_PCI "/sys/devices/pci"
Damjan Marioncef87f12017-10-05 15:32:41 +020059static const char *sysfs_pci_dev_path = "/sys/bus/pci/devices";
60static const char *sysfs_pci_drv_path = "/sys/bus/pci/drivers";
Damjan Marion2752b892017-12-11 15:55:56 +010061static char *sysfs_mod_vfio_noiommu =
62 "/sys/module/vfio/parameters/enable_unsafe_noiommu_mode";
Damjan Mariona42cd342016-04-13 18:03:20 +020063
Damjan Marion23227982018-10-22 13:38:57 +020064#define pci_log_debug(vm, dev, f, ...) \
65 vlib_log(VLIB_LOG_LEVEL_DEBUG, pci_main.log_default, "%U: " f, \
66 format_vlib_pci_addr, vlib_pci_get_addr(vm, dev->handle), ## __VA_ARGS__)
67#define pci_log_err(vm, dev, f, ...) \
68 vlib_log(VLIB_LOG_LEVEL_ERR, pci_main.log_default, "%U: " f, \
69 format_vlib_pci_addr, vlib_pci_get_addr(vm, dev->handle), ## __VA_ARGS__)
70
Dave Barach9b8ffd92016-07-08 08:13:45 -040071typedef struct
72{
Damjan Marion2060db82018-03-04 17:37:15 +010073 int fd;
74 void *addr;
75 size_t size;
76} linux_pci_region_t;
77
Damjan Marion599a16b2018-03-04 19:35:23 +010078typedef struct
79{
80 int fd;
81 u32 clib_file_index;
82 union
83 {
Damjan Mariond5ded2d2018-03-05 10:18:50 +010084 pci_intx_handler_function_t *intx_handler;
Damjan Marion599a16b2018-03-04 19:35:23 +010085 pci_msix_handler_function_t *msix_handler;
86 };
87} linux_pci_irq_t;
Damjan Marion2060db82018-03-04 17:37:15 +010088
89typedef enum
90{
91 LINUX_PCI_DEVICE_TYPE_UNKNOWN,
92 LINUX_PCI_DEVICE_TYPE_UIO,
93 LINUX_PCI_DEVICE_TYPE_VFIO,
94} linux_pci_device_type_t;
95
96typedef struct
97{
98 linux_pci_device_type_t type;
Damjan Marioncef87f12017-10-05 15:32:41 +020099 vlib_pci_dev_handle_t handle;
100 vlib_pci_addr_t addr;
Damjan Mariond2bfb782019-01-07 20:56:04 +0100101 u32 numa_node;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200102
103 /* Resource file descriptors. */
Damjan Marion2060db82018-03-04 17:37:15 +0100104 linux_pci_region_t *regions;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200105
106 /* File descriptor for config space read/write. */
107 int config_fd;
Damjan Marionf9a968e2018-02-25 23:26:22 +0100108 u64 config_offset;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200109
Damjan Marionf9a968e2018-02-25 23:26:22 +0100110 /* Device File descriptor */
111 int fd;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200112
Mohsin Kazmi95276ca2018-09-28 11:45:01 +0200113 /* read/write file descriptor for io bar */
114 int io_fd;
115 u64 io_offset;
116
Damjan Marion5a206ea2016-05-12 22:11:03 +0200117 /* Minor device for uio device. */
118 u32 uio_minor;
119
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100120 /* Interrupt handlers */
121 linux_pci_irq_t intx_irq;
Damjan Marion599a16b2018-03-04 19:35:23 +0100122 linux_pci_irq_t *msix_irqs;
Damjan Marioncef87f12017-10-05 15:32:41 +0200123
124 /* private data */
125 uword private_data;
126
Damjan Marion23227982018-10-22 13:38:57 +0200127 u8 supports_va_dma;
128
Damjan Marion5a206ea2016-05-12 22:11:03 +0200129} linux_pci_device_t;
130
131/* Pool of PCI devices. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400132typedef struct
133{
134 vlib_main_t *vlib_main;
135 linux_pci_device_t *linux_pci_devices;
Damjan Marioncef87f12017-10-05 15:32:41 +0200136
Damjan Marion5a206ea2016-05-12 22:11:03 +0200137} linux_pci_main_t;
138
139extern linux_pci_main_t linux_pci_main;
140
Damjan Marion599a16b2018-03-04 19:35:23 +0100141static linux_pci_device_t *
Damjan Marioncef87f12017-10-05 15:32:41 +0200142linux_pci_get_device (vlib_pci_dev_handle_t h)
143{
144 linux_pci_main_t *lpm = &linux_pci_main;
145 return pool_elt_at_index (lpm->linux_pci_devices, h);
146}
147
148uword
Damjan Marion23227982018-10-22 13:38:57 +0200149vlib_pci_get_private_data (vlib_main_t * vm, vlib_pci_dev_handle_t h)
Damjan Marioncef87f12017-10-05 15:32:41 +0200150{
151 linux_pci_device_t *d = linux_pci_get_device (h);
152 return d->private_data;
153}
154
155void
Damjan Marion23227982018-10-22 13:38:57 +0200156vlib_pci_set_private_data (vlib_main_t * vm, vlib_pci_dev_handle_t h,
157 uword private_data)
Damjan Marioncef87f12017-10-05 15:32:41 +0200158{
159 linux_pci_device_t *d = linux_pci_get_device (h);
160 d->private_data = private_data;
161}
162
163vlib_pci_addr_t *
Damjan Marion23227982018-10-22 13:38:57 +0200164vlib_pci_get_addr (vlib_main_t * vm, vlib_pci_dev_handle_t h)
Damjan Marioncef87f12017-10-05 15:32:41 +0200165{
Damjan Marion599a16b2018-03-04 19:35:23 +0100166 linux_pci_device_t *d = linux_pci_get_device (h);
167 return &d->addr;
Damjan Marioncef87f12017-10-05 15:32:41 +0200168}
169
Damjan Mariond2bfb782019-01-07 20:56:04 +0100170u32
171vlib_pci_get_numa_node (vlib_main_t * vm, vlib_pci_dev_handle_t h)
172{
173 linux_pci_device_t *d = linux_pci_get_device (h);
174 return d->numa_node;
175}
176
Mohsin Kazmia402b832019-01-25 15:57:24 +0000177u32
178vlib_pci_get_num_msix_interrupts (vlib_main_t * vm, vlib_pci_dev_handle_t h)
179{
180 linux_pci_device_t *d = linux_pci_get_device (h);
181
182 if (d->type == LINUX_PCI_DEVICE_TYPE_VFIO)
183 {
184 struct vfio_irq_info ii = { 0 };
185
186 ii.argsz = sizeof (struct vfio_irq_info);
187 ii.index = VFIO_PCI_MSIX_IRQ_INDEX;
188 if (ioctl (d->fd, VFIO_DEVICE_GET_IRQ_INFO, &ii) < 0)
189 return 0;
190 return ii.count;
191 }
192 return 0;
193}
194
Damjan Marion5a206ea2016-05-12 22:11:03 +0200195/* Call to allocate/initialize the pci subsystem.
196 This is not an init function so that users can explicitly enable
197 pci only when it's needed. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400198clib_error_t *pci_bus_init (vlib_main_t * vm);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200199
Ed Warnickecb9cada2015-12-08 15:45:58 -0700200linux_pci_main_t linux_pci_main;
201
Damjan Marioncef87f12017-10-05 15:32:41 +0200202vlib_pci_device_info_t *
Damjan Marion23227982018-10-22 13:38:57 +0200203vlib_pci_get_device_info (vlib_main_t * vm, vlib_pci_addr_t * addr,
204 clib_error_t ** error)
Damjan Marioncef87f12017-10-05 15:32:41 +0200205{
Damjan Marioncef87f12017-10-05 15:32:41 +0200206 clib_error_t *err;
207 vlib_pci_device_info_t *di;
208 u8 *f = 0;
209 u32 tmp;
210 int fd;
Yulong Pei45495482019-10-17 18:41:52 +0800211 u8 *tmpstr;
Jieqiang Wang76c61592020-01-13 17:15:13 +0800212 clib_bitmap_t *bmp = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200213
214 di = clib_mem_alloc (sizeof (vlib_pci_device_info_t));
Dave Barachb7b92992018-10-17 10:38:51 -0400215 clib_memset (di, 0, sizeof (vlib_pci_device_info_t));
Damjan Marioncef87f12017-10-05 15:32:41 +0200216 di->addr.as_u32 = addr->as_u32;
217
218 u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
219 format_vlib_pci_addr, addr);
220
221 f = format (0, "%v/config%c", dev_dir_name, 0);
222 fd = open ((char *) f, O_RDWR);
223
224 /* Try read-only access if write fails. */
225 if (fd < 0)
226 fd = open ((char *) f, O_RDONLY);
227
228 if (fd < 0)
229 {
230 err = clib_error_return_unix (0, "open `%s'", f);
231 goto error;
232 }
233
234 /* You can only read more that 64 bytes of config space as root; so we try to
235 read the full space but fall back to just the first 64 bytes. */
Damjan Marion44bcc202018-03-04 16:44:26 +0100236 if (read (fd, &di->config_data, sizeof (di->config_data)) <
237 sizeof (di->config0))
Damjan Marioncef87f12017-10-05 15:32:41 +0200238 {
239 err = clib_error_return_unix (0, "read `%s'", f);
240 close (fd);
241 goto error;
242 }
243
244 {
245 static pci_config_header_t all_ones;
246 if (all_ones.vendor_id == 0)
Dave Barachb7b92992018-10-17 10:38:51 -0400247 clib_memset (&all_ones, ~0, sizeof (all_ones));
Damjan Marioncef87f12017-10-05 15:32:41 +0200248
249 if (!memcmp (&di->config0.header, &all_ones, sizeof (all_ones)))
250 {
251 err = clib_error_return (0, "invalid PCI config for `%s'", f);
252 close (fd);
253 goto error;
254 }
255 }
256
257 if (di->config0.header.header_type == 0)
258 pci_config_type0_little_to_host (&di->config0);
259 else
260 pci_config_type1_little_to_host (&di->config1);
261
262 di->numa_node = -1;
263 vec_reset_length (f);
264 f = format (f, "%v/numa_node%c", dev_dir_name, 0);
Jieqiang Wang76c61592020-01-13 17:15:13 +0800265 err = clib_sysfs_read ((char *) f, "%d", &di->numa_node);
Damjan Marioncef87f12017-10-05 15:32:41 +0200266 if (err)
267 {
268 di->numa_node = -1;
269 clib_error_free (err);
270 }
Jieqiang Wang76c61592020-01-13 17:15:13 +0800271 if (di->numa_node == -1)
272 {
273 /* if '/sys/bus/pci/devices/<device id>/numa_node' returns -1 and
274 it is a SMP system, set numa_node to 0. */
275 if ((err = clib_sysfs_read ("/sys/devices/system/node/online", "%U",
276 unformat_bitmap_list, &bmp)))
277 clib_error_free (err);
278 if (clib_bitmap_count_set_bits (bmp) == 1)
279 di->numa_node = 0;
280 }
Damjan Marioncef87f12017-10-05 15:32:41 +0200281
282 vec_reset_length (f);
283 f = format (f, "%v/class%c", dev_dir_name, 0);
284 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
285 if (err)
286 goto error;
287 di->device_class = tmp >> 8;
288
289 vec_reset_length (f);
290 f = format (f, "%v/vendor%c", dev_dir_name, 0);
291 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
292 if (err)
293 goto error;
294 di->vendor_id = tmp;
295
296 vec_reset_length (f);
297 f = format (f, "%v/device%c", dev_dir_name, 0);
298 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
299 if (err)
300 goto error;
301 di->device_id = tmp;
302
303 vec_reset_length (f);
304 f = format (f, "%v/driver%c", dev_dir_name, 0);
305 di->driver_name = clib_sysfs_link_to_name ((char *) f);
Benoît Ganne0eae2bb2019-10-04 17:30:21 +0200306 if (!di->driver_name)
307 di->driver_name = format (0, "<NONE>%c", 0);
Damjan Marioncef87f12017-10-05 15:32:41 +0200308
309 di->iommu_group = -1;
Yulong Pei45495482019-10-17 18:41:52 +0800310 vec_reset_length (f);
311 f = format (f, "%v/iommu_group%c", dev_dir_name, 0);
312 tmpstr = clib_sysfs_link_to_name ((char *) f);
313 if (tmpstr)
Damjan Marioncef87f12017-10-05 15:32:41 +0200314 {
Yulong Pei45495482019-10-17 18:41:52 +0800315 di->iommu_group = atoi ((char *) tmpstr);
316 vec_free (tmpstr);
Damjan Marioncef87f12017-10-05 15:32:41 +0200317 }
318
Yulong Pei45495482019-10-17 18:41:52 +0800319 vec_reset_length (f);
320 f = format (f, "%v/iommu_group/name%c", dev_dir_name, 0);
321 err = clib_sysfs_read ((char *) f, "%s", &tmpstr);
322 if (err == 0)
323 {
324 if (strncmp ((char *) tmpstr, "vfio-noiommu", 12) == 0)
325 di->flags |= VLIB_PCI_DEVICE_INFO_F_NOIOMMU;
326 vec_free (tmpstr);
327 }
328 else
329 clib_error_free (err);
330
Damjan Marion96504182017-12-10 23:54:46 +0100331 close (fd);
332
Damjan Marioncef87f12017-10-05 15:32:41 +0200333 vec_reset_length (f);
334 f = format (f, "%v/vpd%c", dev_dir_name, 0);
335 fd = open ((char *) f, O_RDONLY);
336 if (fd >= 0)
337 {
338 while (1)
339 {
340 u8 tag[3];
341 u8 *data = 0;
Damjan Marion96504182017-12-10 23:54:46 +0100342 uword len;
Damjan Marioncef87f12017-10-05 15:32:41 +0200343
344 if (read (fd, &tag, 3) != 3)
345 break;
346
347 if (tag[0] != 0x82 && tag[0] != 0x90 && tag[0] != 0x91)
348 break;
349
350 len = (tag[2] << 8) | tag[1];
Ray Kinsella03d0fbe2021-11-03 09:13:41 +0000351 vec_validate (data, len - 1);
Damjan Marioncef87f12017-10-05 15:32:41 +0200352
353 if (read (fd, data, len) != len)
354 {
355 vec_free (data);
356 break;
357 }
358 if (tag[0] == 0x82)
359 di->product_name = data;
360 else if (tag[0] == 0x90)
361 di->vpd_r = data;
362 else if (tag[0] == 0x91)
363 di->vpd_w = data;
364
365 data = 0;
366 }
367 close (fd);
368 }
369
370 goto done;
371
372error:
373 vlib_pci_free_device_info (di);
374 di = 0;
375
376done:
Jieqiang Wang76c61592020-01-13 17:15:13 +0800377 vec_free (bmp);
Damjan Marioncef87f12017-10-05 15:32:41 +0200378 vec_free (f);
379 vec_free (dev_dir_name);
380 if (error)
381 *error = err;
382 else
383 clib_error_free (err);
384 return di;
385}
386
Ray Kinsella5714a492021-11-02 13:33:44 +0000387clib_error_t *__attribute__ ((weak))
388vlib_pci_get_device_root_bus (vlib_pci_addr_t *addr, vlib_pci_addr_t *root_bus)
389{
390 u8 *rel_path = 0, *abs_path = 0, *link_path = 0;
391 unformat_input_t input;
Ray Kinsella81865bc2021-11-08 07:22:49 +0000392 int fd = open (sysfs_pci_dev_path, O_RDONLY);
Klement Sekerabd469072021-11-18 12:32:05 +0100393 ssize_t size = 0;
Ray Kinsella5714a492021-11-02 13:33:44 +0000394 u32 domain = 0, bus;
395 clib_error_t *err = NULL;
396
397 if (fd < 0)
398 return clib_error_return_unix (0, "failed to open %s", sysfs_pci_dev_path);
399
400 vec_alloc (rel_path, PATH_MAX);
401 vec_alloc (abs_path, PATH_MAX);
402
403 link_path =
404 format (0, "%s/%U", sysfs_pci_dev_path, format_vlib_pci_addr, addr);
405 size = readlinkat (fd, (char *) link_path, (char *) rel_path, PATH_MAX);
406 if (size < 0)
407 {
408 err = clib_error_return_unix (0, "failed to read %s", rel_path);
409 goto done;
410 }
411
412 rel_path[size] = '\0';
413 vec_free (link_path);
414
415 link_path = format (0, "%s/%s", sysfs_pci_dev_path, rel_path);
416 if (!realpath ((char *) link_path, (char *) abs_path))
417 {
418 err = clib_error_return_unix (0, "failed to resolve %s", link_path);
419 goto done;
420 }
421
422 unformat_init_string (&input, (char *) abs_path,
423 clib_strnlen ((char *) abs_path, PATH_MAX));
424
425 if (!unformat (&input, SYSFS_DEVICES_PCI "%x:%x/%s", &domain, &bus,
426 link_path))
427 {
428 err = clib_error_return (0, "unknown input '%U'", format_unformat_error,
429 input);
430 goto done;
431 }
432
433 root_bus->domain = domain;
434 root_bus->bus = bus;
435
436done:
437 vec_free (abs_path);
438 vec_free (link_path);
439 vec_free (rel_path);
440 close (fd);
441
442 return err;
443}
444
Damjan Marion2752b892017-12-11 15:55:56 +0100445static int
446directory_exists (char *path)
447{
448 struct stat s = { 0 };
449 if (stat (path, &s) == -1)
450 return 0;
451
452 return S_ISDIR (s.st_mode);
453}
454
Damjan Mariona42cd342016-04-13 18:03:20 +0200455clib_error_t *
Benoît Ganne6a073482022-10-13 17:22:26 +0200456vlib_pci_bind_to_uio (vlib_main_t *vm, vlib_pci_addr_t *addr,
457 char *uio_drv_name, int force)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700458{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400459 clib_error_t *error = 0;
Damjan Mariona7f74572017-05-23 18:32:21 +0200460 u8 *s = 0, *driver_name = 0;
Damjan Mariona42cd342016-04-13 18:03:20 +0200461 DIR *dir = 0;
462 struct dirent *e;
Damjan Marioncef87f12017-10-05 15:32:41 +0200463 vlib_pci_device_info_t *di;
Damjan Mariona7f74572017-05-23 18:32:21 +0200464 int fd, clear_driver_override = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200465 u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
466 format_vlib_pci_addr, addr);
467
Damjan Marion23227982018-10-22 13:38:57 +0200468 di = vlib_pci_get_device_info (vm, addr, &error);
Damjan Marioncef87f12017-10-05 15:32:41 +0200469
470 if (error)
471 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700472
Damjan Marion2752b892017-12-11 15:55:56 +0100473 if (strncmp ("auto", uio_drv_name, 5) == 0)
474 {
475 int vfio_pci_loaded = 0;
476
477 if (directory_exists ("/sys/module/vfio_pci"))
478 vfio_pci_loaded = 1;
479
480 if (di->iommu_group != -1)
481 {
482 /* device is bound to IOMMU group */
483 if (!vfio_pci_loaded)
484 {
485 error = clib_error_return (0, "Skipping PCI device %U: device "
486 "is bound to IOMMU group and "
487 "vfio-pci driver is not loaded",
488 format_vlib_pci_addr, addr);
489 goto done;
490 }
491 else
492 uio_drv_name = "vfio-pci";
493 }
494 else
495 {
496 /* device is not bound to IOMMU group so we have multiple options */
497 if (vfio_pci_loaded &&
498 (error = clib_sysfs_write (sysfs_mod_vfio_noiommu, "Y")) == 0)
499 uio_drv_name = "vfio-pci";
500 else if (directory_exists ("/sys/module/uio_pci_generic"))
501 uio_drv_name = "uio_pci_generic";
502 else if (directory_exists ("/sys/module/igb_uio"))
503 uio_drv_name = "igb_uio";
504 else
505 {
506 clib_error_free (error);
507 error = clib_error_return (0, "Skipping PCI device %U: missing "
508 "kernel VFIO or UIO driver",
509 format_vlib_pci_addr, addr);
510 goto done;
511 }
512 clib_error_free (error);
513 }
514 }
515
Damjan Mariona7f74572017-05-23 18:32:21 +0200516 s = format (s, "%v/driver%c", dev_dir_name, 0);
Damjan Marion01914ce2017-09-14 19:04:50 +0200517 driver_name = clib_sysfs_link_to_name ((char *) s);
Damjan Mariona42cd342016-04-13 18:03:20 +0200518 vec_reset_length (s);
519
Damjan Mariona7f74572017-05-23 18:32:21 +0200520 if (driver_name &&
521 ((strcmp ("vfio-pci", (char *) driver_name) == 0) ||
522 (strcmp ("uio_pci_generic", (char *) driver_name) == 0) ||
523 (strcmp ("igb_uio", (char *) driver_name) == 0)))
Damjan Marion3c785e02017-05-08 18:37:54 +0200524 goto done;
Damjan Marion3c785e02017-05-08 18:37:54 +0200525
Benoît Ganne6a073482022-10-13 17:22:26 +0200526 if (!force)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700527 {
Benoît Ganne6a073482022-10-13 17:22:26 +0200528 /* walk trough all linux interfaces and if interface belonging to
529 this device is found check if interface is admin up */
530 dir = opendir ("/sys/class/net");
531 s = format (s, "%U%c", format_vlib_pci_addr, addr, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700532
Benoît Ganne6a073482022-10-13 17:22:26 +0200533 if (!dir)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400534 {
Benoît Ganne6a073482022-10-13 17:22:26 +0200535 error = clib_error_return (0,
536 "Skipping PCI device %U: failed to "
537 "read /sys/class/net",
538 format_vlib_pci_addr, addr);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400539 goto done;
540 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200541
Benoît Ganne6a073482022-10-13 17:22:26 +0200542 fd = socket (PF_INET, SOCK_DGRAM, 0);
543 if (fd < 0)
Damjan Mariona42cd342016-04-13 18:03:20 +0200544 {
Benoît Ganne6a073482022-10-13 17:22:26 +0200545 error = clib_error_return_unix (0, "socket");
Damjan Mariona42cd342016-04-13 18:03:20 +0200546 goto done;
547 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700548
Benoît Ganne6a073482022-10-13 17:22:26 +0200549 while ((e = readdir (dir)))
550 {
551 struct ifreq ifr;
552 struct ethtool_drvinfo drvinfo;
553
554 if (e->d_name[0] == '.') /* skip . and .. */
555 continue;
556
557 clib_memset (&ifr, 0, sizeof ifr);
558 clib_memset (&drvinfo, 0, sizeof drvinfo);
559 ifr.ifr_data = (char *) &drvinfo;
560 clib_strncpy (ifr.ifr_name, e->d_name, sizeof (ifr.ifr_name) - 1);
561
562 drvinfo.cmd = ETHTOOL_GDRVINFO;
563 if (ioctl (fd, SIOCETHTOOL, &ifr) < 0)
564 {
565 /* Some interfaces (eg "lo") don't support this ioctl */
566 if ((errno != ENOTSUP) && (errno != ENODEV))
567 clib_unix_warning ("ioctl fetch intf %s bus info error",
568 e->d_name);
569 continue;
570 }
571
572 if (strcmp ((char *) s, drvinfo.bus_info))
573 continue;
574
575 clib_memset (&ifr, 0, sizeof (ifr));
576 clib_strncpy (ifr.ifr_name, e->d_name, sizeof (ifr.ifr_name) - 1);
577
578 if (ioctl (fd, SIOCGIFFLAGS, &ifr) < 0)
579 {
580 error = clib_error_return_unix (0, "ioctl fetch intf %s flags",
581 e->d_name);
582 close (fd);
583 goto done;
584 }
585
586 if (ifr.ifr_flags & IFF_UP)
587 {
588 vlib_log (VLIB_LOG_LEVEL_WARNING, pci_main.log_default,
589 "Skipping PCI device %U as host "
590 "interface %s is up",
591 format_vlib_pci_addr, addr, e->d_name);
592 close (fd);
593 goto done;
594 }
595 }
596
597 close (fd);
598 vec_reset_length (s);
599 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200600
601 s = format (s, "%v/driver/unbind%c", dev_dir_name, 0);
Damjan Marioncef87f12017-10-05 15:32:41 +0200602 clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
Damjan Mariona42cd342016-04-13 18:03:20 +0200603 vec_reset_length (s);
604
Damjan Mariona7f74572017-05-23 18:32:21 +0200605 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
606 if (access ((char *) s, F_OK) == 0)
607 {
Damjan Marioncef87f12017-10-05 15:32:41 +0200608 clib_sysfs_write ((char *) s, "%s", uio_drv_name);
Damjan Mariona7f74572017-05-23 18:32:21 +0200609 clear_driver_override = 1;
610 }
611 else
612 {
613 vec_reset_length (s);
Damjan Marioncef87f12017-10-05 15:32:41 +0200614 s = format (s, "%s/%s/new_id%c", sysfs_pci_drv_path, uio_drv_name, 0);
615 clib_sysfs_write ((char *) s, "0x%04x 0x%04x", di->vendor_id,
616 di->device_id);
Damjan Mariona7f74572017-05-23 18:32:21 +0200617 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200618 vec_reset_length (s);
619
Damjan Marioncef87f12017-10-05 15:32:41 +0200620 s = format (s, "%s/%s/bind%c", sysfs_pci_drv_path, uio_drv_name, 0);
621 clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
Damjan Mariona7f74572017-05-23 18:32:21 +0200622 vec_reset_length (s);
623
624 if (clear_driver_override)
625 {
626 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
Damjan Marion01914ce2017-09-14 19:04:50 +0200627 clib_sysfs_write ((char *) s, "%c", 0);
Damjan Mariona7f74572017-05-23 18:32:21 +0200628 vec_reset_length (s);
629 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200630
631done:
632 closedir (dir);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700633 vec_free (s);
Damjan Mariona42cd342016-04-13 18:03:20 +0200634 vec_free (dev_dir_name);
Damjan Mariona7f74572017-05-23 18:32:21 +0200635 vec_free (driver_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700636 return error;
637}
638
Ed Warnickecb9cada2015-12-08 15:45:58 -0700639
640static clib_error_t *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400641scan_uio_dir (void *arg, u8 * path_name, u8 * file_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700642{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400643 linux_pci_device_t *l = arg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700644 unformat_input_t input;
645
646 unformat_init_string (&input, (char *) file_name, vec_len (file_name));
647
Dave Barach9b8ffd92016-07-08 08:13:45 -0400648 if (!unformat (&input, "uio%d", &l->uio_minor))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700649 abort ();
650
651 unformat_free (&input);
652 return 0;
653}
654
Dave Barach9b8ffd92016-07-08 08:13:45 -0400655static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200656vfio_set_irqs (vlib_main_t * vm, linux_pci_device_t * p, u32 index, u32 start,
657 u32 count, u32 flags, int *efds)
Damjan Marion599a16b2018-03-04 19:35:23 +0100658{
659 int data_len = efds ? count * sizeof (int) : 0;
660 u8 buf[sizeof (struct vfio_irq_set) + data_len];
Damjan Marion23227982018-10-22 13:38:57 +0200661 struct vfio_irq_info ii = { 0 };
Damjan Marion599a16b2018-03-04 19:35:23 +0100662 struct vfio_irq_set *irq_set = (struct vfio_irq_set *) buf;
663
664
Damjan Marion23227982018-10-22 13:38:57 +0200665 ii.argsz = sizeof (struct vfio_irq_info);
666 ii.index = index;
Damjan Marion599a16b2018-03-04 19:35:23 +0100667
Damjan Marion23227982018-10-22 13:38:57 +0200668 if (ioctl (p->fd, VFIO_DEVICE_GET_IRQ_INFO, &ii) < 0)
Damjan Marion599a16b2018-03-04 19:35:23 +0100669 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) "
670 "'%U'", format_vlib_pci_addr, &p->addr);
671
Damjan Marion23227982018-10-22 13:38:57 +0200672 pci_log_debug (vm, p, "%s index:%u count:%u flags: %s%s%s%s(0x%x)",
673 __func__, ii.index, ii.count,
674 ii.flags & VFIO_IRQ_INFO_EVENTFD ? "eventfd " : "",
675 ii.flags & VFIO_IRQ_INFO_MASKABLE ? "maskable " : "",
676 ii.flags & VFIO_IRQ_INFO_AUTOMASKED ? "automasked " : "",
677 ii.flags & VFIO_IRQ_INFO_NORESIZE ? "noresize " : "",
678 ii.flags);
679
680 if (ii.count < start + count)
Damjan Marion599a16b2018-03-04 19:35:23 +0100681 return clib_error_return_unix (0, "vfio_set_irq: unexistng interrupt on "
682 "'%U'", format_vlib_pci_addr, &p->addr);
683
684
685 if (efds)
686 {
687 flags |= VFIO_IRQ_SET_DATA_EVENTFD;
Dave Barach178cf492018-11-13 16:34:13 -0500688 clib_memcpy_fast (&irq_set->data, efds, data_len);
Damjan Marion599a16b2018-03-04 19:35:23 +0100689 }
690 else
691 flags |= VFIO_IRQ_SET_DATA_NONE;
692
693 ASSERT ((flags & (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD)) !=
694 (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD));
695
696 irq_set->argsz = sizeof (struct vfio_irq_set) + data_len;
697 irq_set->index = index;
698 irq_set->start = start;
699 irq_set->count = count;
700 irq_set->flags = flags;
701
702 if (ioctl (p->fd, VFIO_DEVICE_SET_IRQS, irq_set) < 0)
703 return clib_error_return_unix (0, "%U:ioctl(VFIO_DEVICE_SET_IRQS) "
704 "[index = %u, start = %u, count = %u, "
705 "flags = 0x%x]",
706 format_vlib_pci_addr, &p->addr,
707 index, start, count, flags);
708 return 0;
709}
710
711static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +0200712linux_pci_uio_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700713{
Damjan Marion23227982018-10-22 13:38:57 +0200714 vlib_main_t *vm = vlib_get_main ();
Damjan Marion5a206ea2016-05-12 22:11:03 +0200715 int __attribute__ ((unused)) rv;
Damjan Marioncef87f12017-10-05 15:32:41 +0200716 vlib_pci_dev_handle_t h = uf->private_data;
717 linux_pci_device_t *p = linux_pci_get_device (h);
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100718 linux_pci_irq_t *irq = &p->intx_irq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700719
Damjan Marion5a206ea2016-05-12 22:11:03 +0200720 u32 icount;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400721 rv = read (uf->file_descriptor, &icount, 4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700722
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100723 if (irq->intx_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200724 irq->intx_handler (vm, h);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200725
Damjan Marion23227982018-10-22 13:38:57 +0200726 vlib_pci_intr_enable (vm, h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700727
728 return /* no error */ 0;
729}
730
Dave Barach9b8ffd92016-07-08 08:13:45 -0400731static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200732linux_pci_vfio_unmask_intx (vlib_main_t * vm, linux_pci_device_t * d)
Damjan Marioncef87f12017-10-05 15:32:41 +0200733{
Damjan Marion23227982018-10-22 13:38:57 +0200734 return vfio_set_irqs (vm, d, VFIO_PCI_INTX_IRQ_INDEX, 0, 1,
Damjan Marion599a16b2018-03-04 19:35:23 +0100735 VFIO_IRQ_SET_ACTION_UNMASK, 0);
736}
737
Damjan Marioncef87f12017-10-05 15:32:41 +0200738static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +0200739linux_pci_uio_error_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700740{
741 u32 error_index = (u32) uf->private_data;
742
743 return clib_error_return (0, "pci device %d: error", error_index);
744}
745
Damjan Marioncef87f12017-10-05 15:32:41 +0200746static clib_error_t *
Damjan Marion599a16b2018-03-04 19:35:23 +0100747linux_pci_vfio_msix_read_ready (clib_file_t * uf)
748{
Damjan Marion23227982018-10-22 13:38:57 +0200749 vlib_main_t *vm = vlib_get_main ();
Damjan Marion599a16b2018-03-04 19:35:23 +0100750 int __attribute__ ((unused)) rv;
751 vlib_pci_dev_handle_t h = uf->private_data >> 16;
752 u16 line = uf->private_data & 0xffff;
753 linux_pci_device_t *p = linux_pci_get_device (h);
754 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, line);
755
756 u64 icount;
757 rv = read (uf->file_descriptor, &icount, sizeof (icount));
758
759 if (irq->msix_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200760 irq->msix_handler (vm, h, line);
Damjan Marion599a16b2018-03-04 19:35:23 +0100761
762 return /* no error */ 0;
763}
764
765static clib_error_t *
766linux_pci_vfio_intx_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700767{
Damjan Marion23227982018-10-22 13:38:57 +0200768 vlib_main_t *vm = vlib_get_main ();
Damjan Marioncef87f12017-10-05 15:32:41 +0200769 int __attribute__ ((unused)) rv;
770 vlib_pci_dev_handle_t h = uf->private_data;
771 linux_pci_device_t *p = linux_pci_get_device (h);
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100772 linux_pci_irq_t *irq = &p->intx_irq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700773
Damjan Marioncef87f12017-10-05 15:32:41 +0200774 u64 icount;
775 rv = read (uf->file_descriptor, &icount, sizeof (icount));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700776
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100777 if (irq->intx_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200778 irq->intx_handler (vm, h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700779
Damjan Marion23227982018-10-22 13:38:57 +0200780 linux_pci_vfio_unmask_intx (vm, p);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700781
Damjan Marioncef87f12017-10-05 15:32:41 +0200782 return /* no error */ 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700783}
784
Damjan Marioncef87f12017-10-05 15:32:41 +0200785static clib_error_t *
786linux_pci_vfio_error_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700787{
Damjan Marioncef87f12017-10-05 15:32:41 +0200788 u32 error_index = (u32) uf->private_data;
789
790 return clib_error_return (0, "pci device %d: error", error_index);
791}
792
793static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200794add_device_uio (vlib_main_t * vm, linux_pci_device_t * p,
795 vlib_pci_device_info_t * di, pci_device_registration_t * r)
Damjan Marioncef87f12017-10-05 15:32:41 +0200796{
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100797 linux_pci_main_t *lpm = &linux_pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +0200798 clib_error_t *err = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200799 u8 *s = 0;
800
Damjan Marionf9a968e2018-02-25 23:26:22 +0100801 p->fd = -1;
Damjan Marion2060db82018-03-04 17:37:15 +0100802 p->type = LINUX_PCI_DEVICE_TYPE_UIO;
Damjan Marioncef87f12017-10-05 15:32:41 +0200803
804 s = format (s, "%s/%U/config%c", sysfs_pci_dev_path,
805 format_vlib_pci_addr, &di->addr, 0);
806
807 p->config_fd = open ((char *) s, O_RDWR);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100808 p->config_offset = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200809 vec_reset_length (s);
810
811 if (p->config_fd == -1)
812 {
813 err = clib_error_return_unix (0, "open '%s'", s);
814 goto error;
815 }
816
Benoît Ganne0b91bd62019-11-20 09:07:50 +0100817 s = format (0, "%s/%U/uio%c", sysfs_pci_dev_path,
818 format_vlib_pci_addr, &di->addr, 0);
Damjan Marioncef87f12017-10-05 15:32:41 +0200819 foreach_directory_file ((char *) s, scan_uio_dir, p, /* scan_dirs */
820 1);
821 vec_reset_length (s);
822
823 s = format (s, "/dev/uio%d%c", p->uio_minor, 0);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100824 p->fd = open ((char *) s, O_RDWR);
825 if (p->fd < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +0200826 {
827 err = clib_error_return_unix (0, "open '%s'", s);
828 goto error;
829 }
830
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100831 if (r && r->interrupt_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200832 vlib_pci_register_intx_handler (vm, p->handle, r->interrupt_handler);
Damjan Marioncef87f12017-10-05 15:32:41 +0200833
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100834 if (r && r->init_function)
835 err = r->init_function (lpm->vlib_main, p->handle);
Damjan Marioncef87f12017-10-05 15:32:41 +0200836
837error:
Damjan Marion20ba1642018-03-26 15:35:33 +0200838 vec_free (s);
Damjan Marioncef87f12017-10-05 15:32:41 +0200839 if (err)
840 {
Damjan Marion96504182017-12-10 23:54:46 +0100841 if (p->config_fd != -1)
842 close (p->config_fd);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100843 if (p->fd != -1)
844 close (p->fd);
Damjan Marioncef87f12017-10-05 15:32:41 +0200845 }
846 return err;
847}
848
Damjan Marion599a16b2018-03-04 19:35:23 +0100849clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200850vlib_pci_register_intx_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h,
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100851 pci_intx_handler_function_t * intx_handler)
852{
853 linux_pci_device_t *p = linux_pci_get_device (h);
854 clib_file_t t = { 0 };
855 linux_pci_irq_t *irq = &p->intx_irq;
856 ASSERT (irq->fd == -1);
857
858 if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
859 {
Damjan Marion23227982018-10-22 13:38:57 +0200860 struct vfio_irq_info ii = { 0 };
861 ii.argsz = sizeof (struct vfio_irq_info);
862 ii.index = VFIO_PCI_INTX_IRQ_INDEX;
863 if (ioctl (p->fd, VFIO_DEVICE_GET_IRQ_INFO, &ii) < 0)
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100864 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) '"
865 "%U'", format_vlib_pci_addr, &p->addr);
Damjan Marion23227982018-10-22 13:38:57 +0200866 pci_log_debug (vm, p, "%s index:%u count:%u flags: %s%s%s%s(0x%x)",
867 __func__, ii.index, ii.count,
868 ii.flags & VFIO_IRQ_INFO_EVENTFD ? "eventfd " : "",
869 ii.flags & VFIO_IRQ_INFO_MASKABLE ? "maskable " : "",
870 ii.flags & VFIO_IRQ_INFO_AUTOMASKED ? "automasked " : "",
871 ii.flags & VFIO_IRQ_INFO_NORESIZE ? "noresize " : "",
872 ii.flags);
873 if (ii.count != 1)
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100874 return clib_error_return (0, "INTx interrupt does not exist on device"
875 "'%U'", format_vlib_pci_addr, &p->addr);
876
877 irq->fd = eventfd (0, EFD_NONBLOCK);
878 if (irq->fd == -1)
879 return clib_error_return_unix (0, "eventfd");
Damjan Marion23227982018-10-22 13:38:57 +0200880
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100881 t.file_descriptor = irq->fd;
882 t.read_function = linux_pci_vfio_intx_read_ready;
883 }
884 else if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
885 {
886 t.file_descriptor = p->fd;
887 t.read_function = linux_pci_uio_read_ready;
888 }
889 else
890 return 0;
891
892 t.error_function = linux_pci_uio_error_ready;
893 t.private_data = p->handle;
894 t.description = format (0, "PCI %U INTx", format_vlib_pci_addr, &p->addr);
895 irq->clib_file_index = clib_file_add (&file_main, &t);
896 irq->intx_handler = intx_handler;
897 return 0;
898}
899
900clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200901vlib_pci_register_msix_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h,
902 u32 start, u32 count,
Damjan Marion599a16b2018-03-04 19:35:23 +0100903 pci_msix_handler_function_t * msix_handler)
904{
905 clib_error_t *err = 0;
906 linux_pci_device_t *p = linux_pci_get_device (h);
907 u32 i;
908
909 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
910 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
911 "support");
912
913 /* *INDENT-OFF* */
914 vec_validate_init_empty (p->msix_irqs, start + count - 1, (linux_pci_irq_t)
915 { .fd = -1});
916 /* *INDENT-ON* */
917
918 for (i = start; i < start + count; i++)
919 {
920 clib_file_t t = { 0 };
921 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
922 ASSERT (irq->fd == -1);
923
924 irq->fd = eventfd (0, EFD_NONBLOCK);
925 if (irq->fd == -1)
926 {
927 err = clib_error_return_unix (0, "eventfd");
928 goto error;
929 }
930
931 t.read_function = linux_pci_vfio_msix_read_ready;
932 t.file_descriptor = irq->fd;
933 t.error_function = linux_pci_vfio_error_ready;
934 t.private_data = p->handle << 16 | i;
935 t.description = format (0, "PCI %U MSI-X #%u", format_vlib_pci_addr,
936 &p->addr, i);
937 irq->clib_file_index = clib_file_add (&file_main, &t);
938 irq->msix_handler = msix_handler;
939 }
940
941 return 0;
942
943error:
944 while (i-- > start)
945 {
946 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
947 if (irq->fd != -1)
948 {
949 clib_file_del_by_index (&file_main, irq->clib_file_index);
950 close (irq->fd);
951 irq->fd = -1;
952 }
953 }
954 return err;
955}
956
957clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200958vlib_pci_enable_msix_irq (vlib_main_t * vm, vlib_pci_dev_handle_t h,
959 u16 start, u16 count)
Damjan Marion599a16b2018-03-04 19:35:23 +0100960{
961 linux_pci_device_t *p = linux_pci_get_device (h);
962 int fds[count];
963 int i;
964
965 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
966 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
967 "support");
968
969 for (i = start; i < start + count; i++)
970 {
971 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
972 fds[i] = irq->fd;
973 }
974
Damjan Marion23227982018-10-22 13:38:57 +0200975 return vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
Damjan Marion599a16b2018-03-04 19:35:23 +0100976 VFIO_IRQ_SET_ACTION_TRIGGER, fds);
977}
978
Damjan Marion9c9490c2020-10-07 20:00:39 +0200979uword
980vlib_pci_get_msix_file_index (vlib_main_t * vm, vlib_pci_dev_handle_t h,
981 u16 index)
982{
983 linux_pci_device_t *p = linux_pci_get_device (h);
984 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, index);
985 if (irq->fd == -1)
986 return ~0;
987 return irq->clib_file_index;
988}
989
Damjan Marion599a16b2018-03-04 19:35:23 +0100990clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200991vlib_pci_disable_msix_irq (vlib_main_t * vm, vlib_pci_dev_handle_t h,
992 u16 start, u16 count)
Damjan Marion599a16b2018-03-04 19:35:23 +0100993{
994 linux_pci_device_t *p = linux_pci_get_device (h);
995 int i, fds[count];
996
997 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
998 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
999 "support");
1000
1001 for (i = start; i < start + count; i++)
1002 fds[i] = -1;
1003
Damjan Marion23227982018-10-22 13:38:57 +02001004 return vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
Damjan Marion599a16b2018-03-04 19:35:23 +01001005 VFIO_IRQ_SET_ACTION_TRIGGER, fds);
1006}
1007
Damjan Marioncef87f12017-10-05 15:32:41 +02001008static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001009add_device_vfio (vlib_main_t * vm, linux_pci_device_t * p,
1010 vlib_pci_device_info_t * di, pci_device_registration_t * r)
Damjan Marioncef87f12017-10-05 15:32:41 +02001011{
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001012 linux_pci_main_t *lpm = &linux_pci_main;
Damjan Marionf9a968e2018-02-25 23:26:22 +01001013 struct vfio_device_info device_info = { 0 };
Damjan Marionf313b742018-03-05 14:08:33 +01001014 struct vfio_region_info reg = { 0 };
Damjan Marioncef87f12017-10-05 15:32:41 +02001015 clib_error_t *err = 0;
1016 u8 *s = 0;
Damjan Marion23227982018-10-22 13:38:57 +02001017 int is_noiommu;
Damjan Marioncef87f12017-10-05 15:32:41 +02001018
Damjan Marion2060db82018-03-04 17:37:15 +01001019 p->type = LINUX_PCI_DEVICE_TYPE_VFIO;
Damjan Marioncef87f12017-10-05 15:32:41 +02001020
Damjan Marion23227982018-10-22 13:38:57 +02001021 if ((err = linux_vfio_group_get_device_fd (&p->addr, &p->fd, &is_noiommu)))
Damjan Marioncef87f12017-10-05 15:32:41 +02001022 return err;
1023
Damjan Marion23227982018-10-22 13:38:57 +02001024 if (is_noiommu == 0)
1025 p->supports_va_dma = 1;
1026
Damjan Marioncef87f12017-10-05 15:32:41 +02001027 device_info.argsz = sizeof (device_info);
Damjan Marionf9a968e2018-02-25 23:26:22 +01001028 if (ioctl (p->fd, VFIO_DEVICE_GET_INFO, &device_info) < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +02001029 {
1030 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
1031 format_vlib_pci_addr, &di->addr);
1032 goto error;
1033 }
1034
Damjan Marionf313b742018-03-05 14:08:33 +01001035 reg.argsz = sizeof (struct vfio_region_info);
1036 reg.index = VFIO_PCI_CONFIG_REGION_INDEX;
1037 if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, &reg) < 0)
1038 {
1039 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
1040 format_vlib_pci_addr, &di->addr);
1041 goto error;
1042 }
Damjan Marion23227982018-10-22 13:38:57 +02001043
Damjan Marion0792bb42020-05-25 19:31:42 +02001044 pci_log_debug (vm, p, "%s %U", __func__, format_vfio_region_info, &reg);
Damjan Marion23227982018-10-22 13:38:57 +02001045
Damjan Marionf313b742018-03-05 14:08:33 +01001046 p->config_offset = reg.offset;
1047 p->config_fd = p->fd;
1048
Damjan Marioncef87f12017-10-05 15:32:41 +02001049 /* reset if device supports it */
1050 if (device_info.flags & VFIO_DEVICE_FLAGS_RESET)
Damjan Marionf9a968e2018-02-25 23:26:22 +01001051 if (ioctl (p->fd, VFIO_DEVICE_RESET) < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +02001052 {
1053 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_RESET) '%U'",
1054 format_vlib_pci_addr, &di->addr);
1055 goto error;
1056 }
1057
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001058 if (r && r->interrupt_handler)
Damjan Marioncef87f12017-10-05 15:32:41 +02001059 {
Damjan Marion23227982018-10-22 13:38:57 +02001060 vlib_pci_register_intx_handler (vm, p->handle, r->interrupt_handler);
1061 linux_pci_vfio_unmask_intx (vm, p);
Damjan Marioncef87f12017-10-05 15:32:41 +02001062 }
1063
Damjan Marion68b4da62018-09-30 18:26:20 +02001064 if (p->supports_va_dma)
1065 {
1066 vlib_buffer_pool_t *bp;
1067 /* *INDENT-OFF* */
Damjan Mariond50e3472019-01-20 00:03:56 +01001068 vec_foreach (bp, vm->buffer_main->buffer_pools)
Damjan Marion68b4da62018-09-30 18:26:20 +02001069 {
1070 u32 i;
1071 vlib_physmem_map_t *pm;
1072 pm = vlib_physmem_get_map (vm, bp->physmem_map_index);
1073 for (i = 0; i < pm->n_pages; i++)
1074 vfio_map_physmem_page (vm, pm->base + (i << pm->log2_page_size));
1075 }
1076 /* *INDENT-ON* */
1077 }
1078
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001079 if (r && r->init_function)
1080 err = r->init_function (lpm->vlib_main, p->handle);
Damjan Marioncef87f12017-10-05 15:32:41 +02001081
1082error:
1083 vec_free (s);
1084 if (err)
1085 {
Damjan Marionf9a968e2018-02-25 23:26:22 +01001086 if (p->fd != -1)
1087 close (p->fd);
Chris Luke30684ac2018-03-29 12:56:58 -07001088 if (p->config_fd != -1 && p->config_fd != p->fd)
Damjan Marion96504182017-12-10 23:54:46 +01001089 close (p->config_fd);
Chris Luke30684ac2018-03-29 12:56:58 -07001090 p->config_fd = p->fd = -1;
Damjan Marioncef87f12017-10-05 15:32:41 +02001091 }
1092 return err;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001093}
1094
1095/* Configuration space read/write. */
1096clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001097vlib_pci_read_write_config (vlib_main_t * vm, vlib_pci_dev_handle_t h,
Damjan Marion5a206ea2016-05-12 22:11:03 +02001098 vlib_read_or_write_t read_or_write,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001099 uword address, void *data, u32 n_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001100{
Damjan Marioncef87f12017-10-05 15:32:41 +02001101 linux_pci_device_t *p = linux_pci_get_device (h);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001102 int n;
1103
Ed Warnickecb9cada2015-12-08 15:45:58 -07001104 if (read_or_write == VLIB_READ)
Damjan Marionf9a968e2018-02-25 23:26:22 +01001105 n = pread (p->config_fd, data, n_bytes, p->config_offset + address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001106 else
Damjan Marionf9a968e2018-02-25 23:26:22 +01001107 n = pwrite (p->config_fd, data, n_bytes, p->config_offset + address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001108
1109 if (n != n_bytes)
1110 return clib_error_return_unix (0, "%s",
1111 read_or_write == VLIB_READ
1112 ? "read" : "write");
1113
1114 return 0;
1115}
1116
1117static clib_error_t *
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001118vlib_pci_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 bar, int *fd,
1119 u64 * size, u64 * offset)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001120{
Damjan Marioncef87f12017-10-05 15:32:41 +02001121 linux_pci_device_t *p = linux_pci_get_device (h);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001122 clib_error_t *error = 0;
1123 int _fd = -1;
1124 u64 _size = 0, _offset = 0;
Damjan Marion2060db82018-03-04 17:37:15 +01001125
1126 ASSERT (bar <= 5);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001127
1128 error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001129
Damjan Marion2060db82018-03-04 17:37:15 +01001130 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001131 {
Damjan Marion2060db82018-03-04 17:37:15 +01001132 u8 *file_name;
1133 struct stat stat_buf;
1134 file_name = format (0, "%s/%U/resource%d%c", sysfs_pci_dev_path,
1135 format_vlib_pci_addr, &p->addr, bar, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001136
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001137 _fd = open ((char *) file_name, O_RDWR);
1138 if (_fd < 0)
Damjan Marion2060db82018-03-04 17:37:15 +01001139 {
1140 error = clib_error_return_unix (0, "open `%s'", file_name);
1141 vec_free (file_name);
1142 return error;
1143 }
1144
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001145 if (fstat (_fd, &stat_buf) < 0)
Damjan Marion2060db82018-03-04 17:37:15 +01001146 {
1147 error = clib_error_return_unix (0, "fstat `%s'", file_name);
1148 vec_free (file_name);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001149 close (_fd);
Damjan Marion2060db82018-03-04 17:37:15 +01001150 return error;
1151 }
1152
1153 vec_free (file_name);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001154 _size = stat_buf.st_size;
1155 _offset = 0;
Damjan Marion2060db82018-03-04 17:37:15 +01001156 }
1157 else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001158 {
Damjan Marion0792bb42020-05-25 19:31:42 +02001159 struct vfio_region_info *r;
1160 u32 sz = sizeof (struct vfio_region_info);
1161 again:
1162 r = clib_mem_alloc (sz);
1163 clib_memset (r, 0, sz);
1164 r->argsz = sz;
1165 r->index = bar;
1166 if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, r) < 0)
Damjan Marion2060db82018-03-04 17:37:15 +01001167 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) "
1168 "'%U'", format_vlib_pci_addr,
1169 &p->addr);
Damjan Marion0792bb42020-05-25 19:31:42 +02001170 if (sz != r->argsz)
1171 {
1172 sz = r->argsz;
1173 clib_mem_free (r);
1174 goto again;
1175 }
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001176 _fd = p->fd;
Damjan Marion0792bb42020-05-25 19:31:42 +02001177 _size = r->size;
1178 _offset = r->offset;
1179 pci_log_debug (vm, p, "%s %U", __func__, format_vfio_region_info, r);
1180 clib_mem_free (r);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001181 }
Damjan Marion2060db82018-03-04 17:37:15 +01001182 else
1183 ASSERT (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001184
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001185 *fd = _fd;
1186 *size = _size;
1187 *offset = _offset;
1188
1189 return error;
1190}
1191
1192static clib_error_t *
1193vlib_pci_map_region_int (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1194 u32 bar, u8 * addr, void **result)
1195{
1196 linux_pci_device_t *p = linux_pci_get_device (h);
1197 int fd = -1;
1198 clib_error_t *error;
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001199 u64 size = 0, offset = 0;
Mohammed Hawari70fc36f2020-10-21 16:41:30 +02001200 u16 command;
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001201
1202 pci_log_debug (vm, p, "map region %u to va %p", bar, addr);
1203
Mohammed Hawari70fc36f2020-10-21 16:41:30 +02001204 if ((error = vlib_pci_read_config_u16 (vm, h, 4, &command)))
1205 return error;
1206
1207 if (!(command & PCI_COMMAND_MEMORY))
1208 {
1209 pci_log_debug (vm, p, "setting memory enable bit");
1210 command |= PCI_COMMAND_MEMORY;
1211 if ((error = vlib_pci_write_config_u16 (vm, h, 4, &command)))
1212 return error;
1213 }
1214
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001215 if ((error = vlib_pci_region (vm, h, bar, &fd, &size, &offset)))
1216 return error;
1217
Damjan Marion6bfd0762020-09-11 22:16:53 +02001218 *result = clib_mem_vm_map_shared (addr, size, fd, offset,
1219 "PCIe %U region %u", format_vlib_pci_addr,
1220 vlib_pci_get_addr (vm, h), bar);
1221 if (*result == CLIB_MEM_VM_MAP_FAILED)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001222 {
Damjan Marion2060db82018-03-04 17:37:15 +01001223 error = clib_error_return_unix (0, "mmap `BAR%u'", bar);
Dave Barachc72950e2020-05-04 14:47:45 -04001224 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO && (fd != -1))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001225 close (fd);
Damjan Marion2060db82018-03-04 17:37:15 +01001226 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001227 }
Damjan Marion2060db82018-03-04 17:37:15 +01001228
1229 /* *INDENT-OFF* */
1230 vec_validate_init_empty (p->regions, bar,
1231 (linux_pci_region_t) { .fd = -1});
1232 /* *INDENT-ON* */
1233 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1234 p->regions[bar].fd = fd;
1235 p->regions[bar].addr = *result;
1236 p->regions[bar].size = size;
1237 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001238}
1239
1240clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001241vlib_pci_map_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 resource,
1242 void **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001243{
Damjan Marion23227982018-10-22 13:38:57 +02001244 return (vlib_pci_map_region_int (vm, h, resource, 0 /* addr */ , result));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001245}
1246
1247clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001248vlib_pci_map_region_fixed (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1249 u32 resource, u8 * addr, void **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001250{
Damjan Marion23227982018-10-22 13:38:57 +02001251 return (vlib_pci_map_region_int (vm, h, resource, addr, result));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001252}
1253
Damjan Marionf313b742018-03-05 14:08:33 +01001254clib_error_t *
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001255vlib_pci_io_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 resource)
1256{
1257 linux_pci_device_t *p = linux_pci_get_device (h);
1258 clib_error_t *error = 0;
1259 int fd = -1;
1260 u64 size = 0, offset = 0;
1261
1262 if ((error = vlib_pci_region (vm, h, resource, &fd, &size, &offset)))
1263 return error;
1264
1265 p->io_fd = fd;
1266 p->io_offset = offset;
1267 return error;
1268}
1269
1270clib_error_t *
1271vlib_pci_read_write_io (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1272 vlib_read_or_write_t read_or_write,
1273 uword offset, void *data, u32 length)
1274{
1275 linux_pci_device_t *p = linux_pci_get_device (h);
1276 int n = 0;
1277
1278 if (read_or_write == VLIB_READ)
1279 n = pread (p->io_fd, data, length, p->io_offset + offset);
1280 else
1281 n = pwrite (p->io_fd, data, length, p->io_offset + offset);
1282
1283 if (n != length)
1284 return clib_error_return_unix (0, "%s",
1285 read_or_write == VLIB_READ
1286 ? "read" : "write");
1287 return 0;
1288}
1289
1290clib_error_t *
Damjan Marion68b4da62018-09-30 18:26:20 +02001291vlib_pci_map_dma (vlib_main_t * vm, vlib_pci_dev_handle_t h, void *ptr)
1292{
1293 linux_pci_device_t *p = linux_pci_get_device (h);
1294
1295 if (!p->supports_va_dma)
1296 return 0;
1297
1298 return vfio_map_physmem_page (vm, ptr);
1299}
1300
1301int
1302vlib_pci_supports_virtual_addr_dma (vlib_main_t * vm, vlib_pci_dev_handle_t h)
1303{
1304 linux_pci_device_t *p = linux_pci_get_device (h);
1305
1306 return p->supports_va_dma != 0;
1307}
1308
1309clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001310vlib_pci_device_open (vlib_main_t * vm, vlib_pci_addr_t * addr,
Damjan Marionf313b742018-03-05 14:08:33 +01001311 pci_device_id_t ids[], vlib_pci_dev_handle_t * handle)
1312{
1313 linux_pci_main_t *lpm = &linux_pci_main;
1314 vlib_pci_device_info_t *di;
1315 linux_pci_device_t *p;
1316 clib_error_t *err = 0;
1317 pci_device_id_t *i;
1318
Damjan Marion23227982018-10-22 13:38:57 +02001319 di = vlib_pci_get_device_info (vm, addr, &err);
Damjan Marionf313b742018-03-05 14:08:33 +01001320
1321 if (err)
1322 return err;
1323 for (i = ids; i->vendor_id != 0; i++)
1324 if (i->vendor_id == di->vendor_id && i->device_id == di->device_id)
1325 break;
1326
1327 if (i->vendor_id == 0)
1328 return clib_error_return (0, "Wrong vendor or device id");
1329
1330 pool_get (lpm->linux_pci_devices, p);
1331 p->handle = p - lpm->linux_pci_devices;
Damjan Marion23227982018-10-22 13:38:57 +02001332 p->addr.as_u32 = di->addr.as_u32;
Damjan Marionf313b742018-03-05 14:08:33 +01001333 p->intx_irq.fd = -1;
Mohsin Kazmi692f9b12019-04-02 11:09:49 +00001334 p->intx_irq.clib_file_index = -1;
Damjan Mariond2bfb782019-01-07 20:56:04 +01001335 p->numa_node = di->numa_node;
Mohsin Kazmi3d3b9552018-10-24 14:05:34 +02001336 /*
1337 * pci io bar read/write fd
1338 */
1339 p->io_fd = -1;
Damjan Marionf313b742018-03-05 14:08:33 +01001340
Damjan Marion23227982018-10-22 13:38:57 +02001341 pci_log_debug (vm, p, "open vid:0x%04x did:0x%04x driver:%s iommu_group:%d",
1342 di->vendor_id, di->device_id, di->driver_name,
1343 di->iommu_group);
1344
Steven Luongab489922019-08-21 11:35:27 -07001345 if (clib_strncmp ("vfio-pci", (char *) di->driver_name, 8) == 0)
Damjan Marion23227982018-10-22 13:38:57 +02001346 err = add_device_vfio (vm, p, di, 0);
Steven Luongab489922019-08-21 11:35:27 -07001347 else if (clib_strncmp ("uio_pci_generic", (char *) di->driver_name, 8) == 0)
Damjan Marion23227982018-10-22 13:38:57 +02001348 err = add_device_uio (vm, p, di, 0);
Damjan Marionf313b742018-03-05 14:08:33 +01001349 else
Damjan Marion23227982018-10-22 13:38:57 +02001350 err = clib_error_create ("device not bound to 'vfio-pci' or "
1351 "'uio_pci_generic' kernel module");
Damjan Marionf313b742018-03-05 14:08:33 +01001352 if (err)
1353 goto error;
1354
1355 *handle = p->handle;
1356
1357error:
1358 vlib_pci_free_device_info (di);
1359 if (err)
1360 {
Damjan Marion23227982018-10-22 13:38:57 +02001361 pci_log_err (vm, p, "%U", format_clib_error, err);
Dave Barachb7b92992018-10-17 10:38:51 -04001362 clib_memset (p, 0, sizeof (linux_pci_device_t));
Damjan Marionf313b742018-03-05 14:08:33 +01001363 pool_put (lpm->linux_pci_devices, p);
1364 }
1365
1366 return err;
1367}
1368
1369void
Damjan Marion23227982018-10-22 13:38:57 +02001370vlib_pci_device_close (vlib_main_t * vm, vlib_pci_dev_handle_t h)
Damjan Marionf313b742018-03-05 14:08:33 +01001371{
1372 linux_pci_main_t *lpm = &linux_pci_main;
1373 linux_pci_device_t *p = linux_pci_get_device (h);
1374 linux_pci_irq_t *irq;
1375 linux_pci_region_t *res;
1376 clib_error_t *err = 0;
1377
1378 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1379 {
1380 irq = &p->intx_irq;
Mohsin Kazmi692f9b12019-04-02 11:09:49 +00001381 if (irq->clib_file_index != -1)
1382 clib_file_del_by_index (&file_main, irq->clib_file_index);
Damjan Marionf313b742018-03-05 14:08:33 +01001383 close (p->config_fd);
Mohsin Kazmi3d3b9552018-10-24 14:05:34 +02001384 if (p->io_fd != -1)
1385 close (p->io_fd);
Damjan Marionf313b742018-03-05 14:08:33 +01001386 }
1387 else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
1388 {
1389 irq = &p->intx_irq;
1390 /* close INTx irqs */
1391 if (irq->fd != -1)
1392 {
Damjan Marion23227982018-10-22 13:38:57 +02001393 err = vfio_set_irqs (vm, p, VFIO_PCI_INTX_IRQ_INDEX, 0, 0,
Damjan Marionf313b742018-03-05 14:08:33 +01001394 VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1395 clib_error_free (err);
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 (irq->fd);
1399 }
1400
1401 /* close MSI-X irqs */
1402 if (vec_len (p->msix_irqs))
1403 {
Damjan Marion23227982018-10-22 13:38:57 +02001404 err = vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, 0, 0,
Damjan Marionf313b742018-03-05 14:08:33 +01001405 VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1406 clib_error_free (err);
1407 /* *INDENT-OFF* */
1408 vec_foreach (irq, p->msix_irqs)
1409 {
1410 if (irq->fd == -1)
1411 continue;
1412 clib_file_del_by_index (&file_main, irq->clib_file_index);
1413 close (irq->fd);
1414 }
1415 /* *INDENT-ON* */
1416 vec_free (p->msix_irqs);
1417 }
1418 }
1419
1420 /* *INDENT-OFF* */
1421 vec_foreach (res, p->regions)
1422 {
1423 if (res->size == 0)
1424 continue;
Damjan Marion6bfd0762020-09-11 22:16:53 +02001425 clib_mem_vm_unmap (res->addr);
Damjan Marionf313b742018-03-05 14:08:33 +01001426 if (res->fd != -1)
1427 close (res->fd);
1428 }
1429 /* *INDENT-ON* */
1430 vec_free (p->regions);
1431
1432 close (p->fd);
Dave Barachb7b92992018-10-17 10:38:51 -04001433 clib_memset (p, 0, sizeof (linux_pci_device_t));
Damjan Marionf313b742018-03-05 14:08:33 +01001434 pool_put (lpm->linux_pci_devices, p);
1435}
1436
Dave Barach9b8ffd92016-07-08 08:13:45 -04001437void
Damjan Marion23227982018-10-22 13:38:57 +02001438init_device_from_registered (vlib_main_t * vm, vlib_pci_device_info_t * di)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001439{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001440 vlib_pci_main_t *pm = &pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +02001441 linux_pci_main_t *lpm = &linux_pci_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001442 pci_device_registration_t *r;
1443 pci_device_id_t *i;
Damjan Marioncef87f12017-10-05 15:32:41 +02001444 clib_error_t *err = 0;
1445 linux_pci_device_t *p;
1446
1447 pool_get (lpm->linux_pci_devices, p);
1448 p->handle = p - lpm->linux_pci_devices;
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001449 p->intx_irq.fd = -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001450
Damjan Marion5a206ea2016-05-12 22:11:03 +02001451 r = pm->pci_device_registrations;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001452
1453 while (r)
1454 {
1455 for (i = r->supported_devices; i->vendor_id != 0; i++)
Damjan Marion3a593822018-02-17 14:06:53 +01001456 if (i->vendor_id == di->vendor_id && i->device_id == di->device_id)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001457 {
Damjan Marioncef87f12017-10-05 15:32:41 +02001458 if (di->iommu_group != -1)
Damjan Marion23227982018-10-22 13:38:57 +02001459 err = add_device_vfio (vm, p, di, r);
Damjan Marioncef87f12017-10-05 15:32:41 +02001460 else
Damjan Marion23227982018-10-22 13:38:57 +02001461 err = add_device_uio (vm, p, di, r);
Damjan Marion5a206ea2016-05-12 22:11:03 +02001462
Damjan Marioncef87f12017-10-05 15:32:41 +02001463 if (err)
1464 clib_error_report (err);
1465 else
1466 return;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001467 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001468 r = r->next_registration;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001469 }
Damjan Marioncef87f12017-10-05 15:32:41 +02001470
Ed Warnickecb9cada2015-12-08 15:45:58 -07001471 /* No driver, close the PCI config-space FD */
Dave Barachb7b92992018-10-17 10:38:51 -04001472 clib_memset (p, 0, sizeof (linux_pci_device_t));
Damjan Marioncef87f12017-10-05 15:32:41 +02001473 pool_put (lpm->linux_pci_devices, p);
1474}
1475
1476static clib_error_t *
1477scan_pci_addr (void *arg, u8 * dev_dir_name, u8 * ignored)
1478{
1479 vlib_pci_addr_t addr, **addrv = arg;
1480 unformat_input_t input;
1481 clib_error_t *err = 0;
1482
1483 unformat_init_string (&input, (char *) dev_dir_name,
1484 vec_len (dev_dir_name));
1485
1486 if (!unformat (&input, "/sys/bus/pci/devices/%U",
1487 unformat_vlib_pci_addr, &addr))
1488 err = clib_error_return (0, "unformat error `%v`", dev_dir_name);
1489
1490 unformat_free (&input);
1491
1492 if (err)
1493 return err;
1494
1495 vec_add1 (*addrv, addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001496 return 0;
1497}
1498
Damjan Marioncef87f12017-10-05 15:32:41 +02001499static int
1500pci_addr_cmp (void *v1, void *v2)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001501{
Damjan Marioncef87f12017-10-05 15:32:41 +02001502 vlib_pci_addr_t *a1 = v1;
1503 vlib_pci_addr_t *a2 = v2;
1504
1505 if (a1->domain > a2->domain)
1506 return 1;
1507 if (a1->domain < a2->domain)
1508 return -1;
1509 if (a1->bus > a2->bus)
1510 return 1;
1511 if (a1->bus < a2->bus)
1512 return -1;
1513 if (a1->slot > a2->slot)
1514 return 1;
1515 if (a1->slot < a2->slot)
1516 return -1;
1517 if (a1->function > a2->function)
1518 return 1;
1519 if (a1->function < a2->function)
1520 return -1;
1521 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001522}
1523
Damjan Marioncef87f12017-10-05 15:32:41 +02001524vlib_pci_addr_t *
1525vlib_pci_get_all_dev_addrs ()
Ed Warnickecb9cada2015-12-08 15:45:58 -07001526{
Damjan Marioncef87f12017-10-05 15:32:41 +02001527 vlib_pci_addr_t *addrs = 0;
1528 clib_error_t *err;
1529 err = foreach_directory_file ((char *) sysfs_pci_dev_path, scan_pci_addr,
1530 &addrs, /* scan_dirs */ 0);
1531 if (err)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001532 {
Damjan Marioncef87f12017-10-05 15:32:41 +02001533 vec_free (addrs);
1534 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001535 }
1536
Damjan Marioncef87f12017-10-05 15:32:41 +02001537 vec_sort_with_function (addrs, pci_addr_cmp);
Damjan Mariona42cd342016-04-13 18:03:20 +02001538
Damjan Marioncef87f12017-10-05 15:32:41 +02001539 return addrs;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001540}
1541
Dave Barach9b8ffd92016-07-08 08:13:45 -04001542clib_error_t *
1543linux_pci_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001544{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001545 vlib_pci_main_t *pm = &pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +02001546 vlib_pci_addr_t *addr = 0, *addrs;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001547
1548 pm->vlib_main = vm;
1549
Dave Barach9b8ffd92016-07-08 08:13:45 -04001550 ASSERT (sizeof (vlib_pci_addr_t) == sizeof (u32));
Damjan Mariona42cd342016-04-13 18:03:20 +02001551
Damjan Marioncef87f12017-10-05 15:32:41 +02001552 addrs = vlib_pci_get_all_dev_addrs ();
1553 /* *INDENT-OFF* */
1554 vec_foreach (addr, addrs)
1555 {
1556 vlib_pci_device_info_t *d;
Damjan Marion23227982018-10-22 13:38:57 +02001557 if ((d = vlib_pci_get_device_info (vm, addr, 0)))
Damjan Marioncef87f12017-10-05 15:32:41 +02001558 {
Damjan Marion23227982018-10-22 13:38:57 +02001559 init_device_from_registered (vm, d);
Damjan Marioncef87f12017-10-05 15:32:41 +02001560 vlib_pci_free_device_info (d);
1561 }
1562 }
1563 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001564
Dave Barachf8d50682019-05-14 18:01:44 -04001565 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001566}
1567
Dave Barachf8d50682019-05-14 18:01:44 -04001568/* *INDENT-OFF* */
1569VLIB_INIT_FUNCTION (linux_pci_init) =
1570{
1571 .runs_after = VLIB_INITS("unix_input_init"),
1572};
1573/* *INDENT-ON* */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001574
1575/*
1576 * fd.io coding-style-patch-verification: ON
1577 *
1578 * Local Variables:
1579 * eval: (c-set-style "gnu")
1580 * End:
1581 */