blob: 75d9edf88490afce0c75ad110d8363ba9423095b [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);
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100489 goto err0;
Damjan Marion2752b892017-12-11 15:55:56 +0100490 }
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);
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100510 goto err0;
Damjan Marion2752b892017-12-11 15:55:56 +0100511 }
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)))
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100524 goto err0;
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);
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100539 goto err0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400540 }
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");
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100546 goto err1;
Damjan Mariona42cd342016-04-13 18:03:20 +0200547 }
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);
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100583 goto err1;
Benoît Ganne6a073482022-10-13 17:22:26 +0200584 }
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);
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100593 goto err1;
Benoît Ganne6a073482022-10-13 17:22:26 +0200594 }
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
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100631err1:
Damjan Mariona42cd342016-04-13 18:03:20 +0200632 closedir (dir);
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100633err0:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700634 vec_free (s);
Damjan Mariona42cd342016-04-13 18:03:20 +0200635 vec_free (dev_dir_name);
Damjan Mariona7f74572017-05-23 18:32:21 +0200636 vec_free (driver_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700637 return error;
638}
639
Ed Warnickecb9cada2015-12-08 15:45:58 -0700640
641static clib_error_t *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400642scan_uio_dir (void *arg, u8 * path_name, u8 * file_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700643{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400644 linux_pci_device_t *l = arg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700645 unformat_input_t input;
646
647 unformat_init_string (&input, (char *) file_name, vec_len (file_name));
648
Dave Barach9b8ffd92016-07-08 08:13:45 -0400649 if (!unformat (&input, "uio%d", &l->uio_minor))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700650 abort ();
651
652 unformat_free (&input);
653 return 0;
654}
655
Dave Barach9b8ffd92016-07-08 08:13:45 -0400656static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200657vfio_set_irqs (vlib_main_t * vm, linux_pci_device_t * p, u32 index, u32 start,
658 u32 count, u32 flags, int *efds)
Damjan Marion599a16b2018-03-04 19:35:23 +0100659{
660 int data_len = efds ? count * sizeof (int) : 0;
661 u8 buf[sizeof (struct vfio_irq_set) + data_len];
Damjan Marion23227982018-10-22 13:38:57 +0200662 struct vfio_irq_info ii = { 0 };
Damjan Marion599a16b2018-03-04 19:35:23 +0100663 struct vfio_irq_set *irq_set = (struct vfio_irq_set *) buf;
664
665
Damjan Marion23227982018-10-22 13:38:57 +0200666 ii.argsz = sizeof (struct vfio_irq_info);
667 ii.index = index;
Damjan Marion599a16b2018-03-04 19:35:23 +0100668
Damjan Marion23227982018-10-22 13:38:57 +0200669 if (ioctl (p->fd, VFIO_DEVICE_GET_IRQ_INFO, &ii) < 0)
Damjan Marion599a16b2018-03-04 19:35:23 +0100670 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) "
671 "'%U'", format_vlib_pci_addr, &p->addr);
672
Damjan Marion23227982018-10-22 13:38:57 +0200673 pci_log_debug (vm, p, "%s index:%u count:%u flags: %s%s%s%s(0x%x)",
674 __func__, ii.index, ii.count,
675 ii.flags & VFIO_IRQ_INFO_EVENTFD ? "eventfd " : "",
676 ii.flags & VFIO_IRQ_INFO_MASKABLE ? "maskable " : "",
677 ii.flags & VFIO_IRQ_INFO_AUTOMASKED ? "automasked " : "",
678 ii.flags & VFIO_IRQ_INFO_NORESIZE ? "noresize " : "",
679 ii.flags);
680
681 if (ii.count < start + count)
Damjan Marion599a16b2018-03-04 19:35:23 +0100682 return clib_error_return_unix (0, "vfio_set_irq: unexistng interrupt on "
683 "'%U'", format_vlib_pci_addr, &p->addr);
684
685
686 if (efds)
687 {
688 flags |= VFIO_IRQ_SET_DATA_EVENTFD;
Dave Barach178cf492018-11-13 16:34:13 -0500689 clib_memcpy_fast (&irq_set->data, efds, data_len);
Damjan Marion599a16b2018-03-04 19:35:23 +0100690 }
691 else
692 flags |= VFIO_IRQ_SET_DATA_NONE;
693
694 ASSERT ((flags & (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD)) !=
695 (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD));
696
697 irq_set->argsz = sizeof (struct vfio_irq_set) + data_len;
698 irq_set->index = index;
699 irq_set->start = start;
700 irq_set->count = count;
701 irq_set->flags = flags;
702
703 if (ioctl (p->fd, VFIO_DEVICE_SET_IRQS, irq_set) < 0)
704 return clib_error_return_unix (0, "%U:ioctl(VFIO_DEVICE_SET_IRQS) "
705 "[index = %u, start = %u, count = %u, "
706 "flags = 0x%x]",
707 format_vlib_pci_addr, &p->addr,
708 index, start, count, flags);
709 return 0;
710}
711
712static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +0200713linux_pci_uio_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700714{
Damjan Marion23227982018-10-22 13:38:57 +0200715 vlib_main_t *vm = vlib_get_main ();
Damjan Marion5a206ea2016-05-12 22:11:03 +0200716 int __attribute__ ((unused)) rv;
Damjan Marioncef87f12017-10-05 15:32:41 +0200717 vlib_pci_dev_handle_t h = uf->private_data;
718 linux_pci_device_t *p = linux_pci_get_device (h);
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100719 linux_pci_irq_t *irq = &p->intx_irq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700720
Damjan Marion5a206ea2016-05-12 22:11:03 +0200721 u32 icount;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400722 rv = read (uf->file_descriptor, &icount, 4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700723
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100724 if (irq->intx_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200725 irq->intx_handler (vm, h);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200726
Damjan Marion23227982018-10-22 13:38:57 +0200727 vlib_pci_intr_enable (vm, h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700728
729 return /* no error */ 0;
730}
731
Dave Barach9b8ffd92016-07-08 08:13:45 -0400732static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200733linux_pci_vfio_unmask_intx (vlib_main_t * vm, linux_pci_device_t * d)
Damjan Marioncef87f12017-10-05 15:32:41 +0200734{
Damjan Marion23227982018-10-22 13:38:57 +0200735 return vfio_set_irqs (vm, d, VFIO_PCI_INTX_IRQ_INDEX, 0, 1,
Damjan Marion599a16b2018-03-04 19:35:23 +0100736 VFIO_IRQ_SET_ACTION_UNMASK, 0);
737}
738
Damjan Marioncef87f12017-10-05 15:32:41 +0200739static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +0200740linux_pci_uio_error_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700741{
742 u32 error_index = (u32) uf->private_data;
743
744 return clib_error_return (0, "pci device %d: error", error_index);
745}
746
Damjan Marioncef87f12017-10-05 15:32:41 +0200747static clib_error_t *
Damjan Marion599a16b2018-03-04 19:35:23 +0100748linux_pci_vfio_msix_read_ready (clib_file_t * uf)
749{
Damjan Marion23227982018-10-22 13:38:57 +0200750 vlib_main_t *vm = vlib_get_main ();
Damjan Marion599a16b2018-03-04 19:35:23 +0100751 int __attribute__ ((unused)) rv;
752 vlib_pci_dev_handle_t h = uf->private_data >> 16;
753 u16 line = uf->private_data & 0xffff;
754 linux_pci_device_t *p = linux_pci_get_device (h);
755 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, line);
756
757 u64 icount;
758 rv = read (uf->file_descriptor, &icount, sizeof (icount));
759
760 if (irq->msix_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200761 irq->msix_handler (vm, h, line);
Damjan Marion599a16b2018-03-04 19:35:23 +0100762
763 return /* no error */ 0;
764}
765
766static clib_error_t *
767linux_pci_vfio_intx_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700768{
Damjan Marion23227982018-10-22 13:38:57 +0200769 vlib_main_t *vm = vlib_get_main ();
Damjan Marioncef87f12017-10-05 15:32:41 +0200770 int __attribute__ ((unused)) rv;
771 vlib_pci_dev_handle_t h = uf->private_data;
772 linux_pci_device_t *p = linux_pci_get_device (h);
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100773 linux_pci_irq_t *irq = &p->intx_irq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700774
Damjan Marioncef87f12017-10-05 15:32:41 +0200775 u64 icount;
776 rv = read (uf->file_descriptor, &icount, sizeof (icount));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700777
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100778 if (irq->intx_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200779 irq->intx_handler (vm, h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700780
Damjan Marion23227982018-10-22 13:38:57 +0200781 linux_pci_vfio_unmask_intx (vm, p);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700782
Damjan Marioncef87f12017-10-05 15:32:41 +0200783 return /* no error */ 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700784}
785
Damjan Marioncef87f12017-10-05 15:32:41 +0200786static clib_error_t *
787linux_pci_vfio_error_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700788{
Damjan Marioncef87f12017-10-05 15:32:41 +0200789 u32 error_index = (u32) uf->private_data;
790
791 return clib_error_return (0, "pci device %d: error", error_index);
792}
793
794static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200795add_device_uio (vlib_main_t * vm, linux_pci_device_t * p,
796 vlib_pci_device_info_t * di, pci_device_registration_t * r)
Damjan Marioncef87f12017-10-05 15:32:41 +0200797{
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100798 linux_pci_main_t *lpm = &linux_pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +0200799 clib_error_t *err = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200800 u8 *s = 0;
801
Damjan Marionf9a968e2018-02-25 23:26:22 +0100802 p->fd = -1;
Damjan Marion2060db82018-03-04 17:37:15 +0100803 p->type = LINUX_PCI_DEVICE_TYPE_UIO;
Damjan Marioncef87f12017-10-05 15:32:41 +0200804
805 s = format (s, "%s/%U/config%c", sysfs_pci_dev_path,
806 format_vlib_pci_addr, &di->addr, 0);
807
808 p->config_fd = open ((char *) s, O_RDWR);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100809 p->config_offset = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200810 vec_reset_length (s);
811
812 if (p->config_fd == -1)
813 {
814 err = clib_error_return_unix (0, "open '%s'", s);
815 goto error;
816 }
817
Benoît Ganne0b91bd62019-11-20 09:07:50 +0100818 s = format (0, "%s/%U/uio%c", sysfs_pci_dev_path,
819 format_vlib_pci_addr, &di->addr, 0);
Damjan Marioncef87f12017-10-05 15:32:41 +0200820 foreach_directory_file ((char *) s, scan_uio_dir, p, /* scan_dirs */
821 1);
822 vec_reset_length (s);
823
824 s = format (s, "/dev/uio%d%c", p->uio_minor, 0);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100825 p->fd = open ((char *) s, O_RDWR);
826 if (p->fd < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +0200827 {
828 err = clib_error_return_unix (0, "open '%s'", s);
829 goto error;
830 }
831
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100832 if (r && r->interrupt_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200833 vlib_pci_register_intx_handler (vm, p->handle, r->interrupt_handler);
Damjan Marioncef87f12017-10-05 15:32:41 +0200834
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100835 if (r && r->init_function)
836 err = r->init_function (lpm->vlib_main, p->handle);
Damjan Marioncef87f12017-10-05 15:32:41 +0200837
838error:
Damjan Marion20ba1642018-03-26 15:35:33 +0200839 vec_free (s);
Damjan Marioncef87f12017-10-05 15:32:41 +0200840 if (err)
841 {
Damjan Marion96504182017-12-10 23:54:46 +0100842 if (p->config_fd != -1)
843 close (p->config_fd);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100844 if (p->fd != -1)
845 close (p->fd);
Damjan Marioncef87f12017-10-05 15:32:41 +0200846 }
847 return err;
848}
849
Damjan Marion599a16b2018-03-04 19:35:23 +0100850clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200851vlib_pci_register_intx_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h,
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100852 pci_intx_handler_function_t * intx_handler)
853{
854 linux_pci_device_t *p = linux_pci_get_device (h);
855 clib_file_t t = { 0 };
856 linux_pci_irq_t *irq = &p->intx_irq;
857 ASSERT (irq->fd == -1);
858
859 if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
860 {
Damjan Marion23227982018-10-22 13:38:57 +0200861 struct vfio_irq_info ii = { 0 };
862 ii.argsz = sizeof (struct vfio_irq_info);
863 ii.index = VFIO_PCI_INTX_IRQ_INDEX;
864 if (ioctl (p->fd, VFIO_DEVICE_GET_IRQ_INFO, &ii) < 0)
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100865 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) '"
866 "%U'", format_vlib_pci_addr, &p->addr);
Damjan Marion23227982018-10-22 13:38:57 +0200867 pci_log_debug (vm, p, "%s index:%u count:%u flags: %s%s%s%s(0x%x)",
868 __func__, ii.index, ii.count,
869 ii.flags & VFIO_IRQ_INFO_EVENTFD ? "eventfd " : "",
870 ii.flags & VFIO_IRQ_INFO_MASKABLE ? "maskable " : "",
871 ii.flags & VFIO_IRQ_INFO_AUTOMASKED ? "automasked " : "",
872 ii.flags & VFIO_IRQ_INFO_NORESIZE ? "noresize " : "",
873 ii.flags);
874 if (ii.count != 1)
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100875 return clib_error_return (0, "INTx interrupt does not exist on device"
876 "'%U'", format_vlib_pci_addr, &p->addr);
877
878 irq->fd = eventfd (0, EFD_NONBLOCK);
879 if (irq->fd == -1)
880 return clib_error_return_unix (0, "eventfd");
Damjan Marion23227982018-10-22 13:38:57 +0200881
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100882 t.file_descriptor = irq->fd;
883 t.read_function = linux_pci_vfio_intx_read_ready;
884 }
885 else if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
886 {
887 t.file_descriptor = p->fd;
888 t.read_function = linux_pci_uio_read_ready;
889 }
890 else
891 return 0;
892
893 t.error_function = linux_pci_uio_error_ready;
894 t.private_data = p->handle;
895 t.description = format (0, "PCI %U INTx", format_vlib_pci_addr, &p->addr);
896 irq->clib_file_index = clib_file_add (&file_main, &t);
897 irq->intx_handler = intx_handler;
898 return 0;
899}
900
901clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200902vlib_pci_register_msix_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h,
903 u32 start, u32 count,
Damjan Marion599a16b2018-03-04 19:35:23 +0100904 pci_msix_handler_function_t * msix_handler)
905{
906 clib_error_t *err = 0;
907 linux_pci_device_t *p = linux_pci_get_device (h);
908 u32 i;
909
910 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
911 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
912 "support");
913
914 /* *INDENT-OFF* */
915 vec_validate_init_empty (p->msix_irqs, start + count - 1, (linux_pci_irq_t)
916 { .fd = -1});
917 /* *INDENT-ON* */
918
919 for (i = start; i < start + count; i++)
920 {
921 clib_file_t t = { 0 };
922 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
923 ASSERT (irq->fd == -1);
924
925 irq->fd = eventfd (0, EFD_NONBLOCK);
926 if (irq->fd == -1)
927 {
928 err = clib_error_return_unix (0, "eventfd");
929 goto error;
930 }
931
932 t.read_function = linux_pci_vfio_msix_read_ready;
933 t.file_descriptor = irq->fd;
934 t.error_function = linux_pci_vfio_error_ready;
935 t.private_data = p->handle << 16 | i;
936 t.description = format (0, "PCI %U MSI-X #%u", format_vlib_pci_addr,
937 &p->addr, i);
938 irq->clib_file_index = clib_file_add (&file_main, &t);
939 irq->msix_handler = msix_handler;
940 }
941
942 return 0;
943
944error:
945 while (i-- > start)
946 {
947 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
948 if (irq->fd != -1)
949 {
950 clib_file_del_by_index (&file_main, irq->clib_file_index);
951 close (irq->fd);
952 irq->fd = -1;
953 }
954 }
955 return err;
956}
957
958clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200959vlib_pci_enable_msix_irq (vlib_main_t * vm, vlib_pci_dev_handle_t h,
960 u16 start, u16 count)
Damjan Marion599a16b2018-03-04 19:35:23 +0100961{
962 linux_pci_device_t *p = linux_pci_get_device (h);
963 int fds[count];
964 int i;
965
966 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
967 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
968 "support");
969
970 for (i = start; i < start + count; i++)
971 {
972 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
973 fds[i] = irq->fd;
974 }
975
Damjan Marion23227982018-10-22 13:38:57 +0200976 return vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
Damjan Marion599a16b2018-03-04 19:35:23 +0100977 VFIO_IRQ_SET_ACTION_TRIGGER, fds);
978}
979
Damjan Marion9c9490c2020-10-07 20:00:39 +0200980uword
981vlib_pci_get_msix_file_index (vlib_main_t * vm, vlib_pci_dev_handle_t h,
982 u16 index)
983{
984 linux_pci_device_t *p = linux_pci_get_device (h);
985 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, index);
986 if (irq->fd == -1)
987 return ~0;
988 return irq->clib_file_index;
989}
990
Damjan Marion599a16b2018-03-04 19:35:23 +0100991clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200992vlib_pci_disable_msix_irq (vlib_main_t * vm, vlib_pci_dev_handle_t h,
993 u16 start, u16 count)
Damjan Marion599a16b2018-03-04 19:35:23 +0100994{
995 linux_pci_device_t *p = linux_pci_get_device (h);
996 int i, fds[count];
997
998 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
999 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
1000 "support");
1001
1002 for (i = start; i < start + count; i++)
1003 fds[i] = -1;
1004
Damjan Marion23227982018-10-22 13:38:57 +02001005 return vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
Damjan Marion599a16b2018-03-04 19:35:23 +01001006 VFIO_IRQ_SET_ACTION_TRIGGER, fds);
1007}
1008
Damjan Marioncef87f12017-10-05 15:32:41 +02001009static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001010add_device_vfio (vlib_main_t * vm, linux_pci_device_t * p,
1011 vlib_pci_device_info_t * di, pci_device_registration_t * r)
Damjan Marioncef87f12017-10-05 15:32:41 +02001012{
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001013 linux_pci_main_t *lpm = &linux_pci_main;
Damjan Marionf9a968e2018-02-25 23:26:22 +01001014 struct vfio_device_info device_info = { 0 };
Damjan Marionf313b742018-03-05 14:08:33 +01001015 struct vfio_region_info reg = { 0 };
Damjan Marioncef87f12017-10-05 15:32:41 +02001016 clib_error_t *err = 0;
1017 u8 *s = 0;
Damjan Marion23227982018-10-22 13:38:57 +02001018 int is_noiommu;
Damjan Marioncef87f12017-10-05 15:32:41 +02001019
Damjan Marion2060db82018-03-04 17:37:15 +01001020 p->type = LINUX_PCI_DEVICE_TYPE_VFIO;
Damjan Marioncef87f12017-10-05 15:32:41 +02001021
Damjan Marion23227982018-10-22 13:38:57 +02001022 if ((err = linux_vfio_group_get_device_fd (&p->addr, &p->fd, &is_noiommu)))
Damjan Marioncef87f12017-10-05 15:32:41 +02001023 return err;
1024
Damjan Marion23227982018-10-22 13:38:57 +02001025 if (is_noiommu == 0)
1026 p->supports_va_dma = 1;
1027
Damjan Marioncef87f12017-10-05 15:32:41 +02001028 device_info.argsz = sizeof (device_info);
Damjan Marionf9a968e2018-02-25 23:26:22 +01001029 if (ioctl (p->fd, VFIO_DEVICE_GET_INFO, &device_info) < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +02001030 {
1031 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
1032 format_vlib_pci_addr, &di->addr);
1033 goto error;
1034 }
1035
Damjan Marionf313b742018-03-05 14:08:33 +01001036 reg.argsz = sizeof (struct vfio_region_info);
1037 reg.index = VFIO_PCI_CONFIG_REGION_INDEX;
1038 if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, &reg) < 0)
1039 {
1040 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
1041 format_vlib_pci_addr, &di->addr);
1042 goto error;
1043 }
Damjan Marion23227982018-10-22 13:38:57 +02001044
Damjan Marion0792bb42020-05-25 19:31:42 +02001045 pci_log_debug (vm, p, "%s %U", __func__, format_vfio_region_info, &reg);
Damjan Marion23227982018-10-22 13:38:57 +02001046
Damjan Marionf313b742018-03-05 14:08:33 +01001047 p->config_offset = reg.offset;
1048 p->config_fd = p->fd;
1049
Damjan Marioncef87f12017-10-05 15:32:41 +02001050 /* reset if device supports it */
1051 if (device_info.flags & VFIO_DEVICE_FLAGS_RESET)
Damjan Marionf9a968e2018-02-25 23:26:22 +01001052 if (ioctl (p->fd, VFIO_DEVICE_RESET) < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +02001053 {
1054 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_RESET) '%U'",
1055 format_vlib_pci_addr, &di->addr);
1056 goto error;
1057 }
1058
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001059 if (r && r->interrupt_handler)
Damjan Marioncef87f12017-10-05 15:32:41 +02001060 {
Damjan Marion23227982018-10-22 13:38:57 +02001061 vlib_pci_register_intx_handler (vm, p->handle, r->interrupt_handler);
1062 linux_pci_vfio_unmask_intx (vm, p);
Damjan Marioncef87f12017-10-05 15:32:41 +02001063 }
1064
Damjan Marion68b4da62018-09-30 18:26:20 +02001065 if (p->supports_va_dma)
1066 {
1067 vlib_buffer_pool_t *bp;
1068 /* *INDENT-OFF* */
Damjan Mariond50e3472019-01-20 00:03:56 +01001069 vec_foreach (bp, vm->buffer_main->buffer_pools)
Damjan Marion68b4da62018-09-30 18:26:20 +02001070 {
1071 u32 i;
1072 vlib_physmem_map_t *pm;
1073 pm = vlib_physmem_get_map (vm, bp->physmem_map_index);
1074 for (i = 0; i < pm->n_pages; i++)
1075 vfio_map_physmem_page (vm, pm->base + (i << pm->log2_page_size));
1076 }
1077 /* *INDENT-ON* */
1078 }
1079
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001080 if (r && r->init_function)
1081 err = r->init_function (lpm->vlib_main, p->handle);
Damjan Marioncef87f12017-10-05 15:32:41 +02001082
1083error:
1084 vec_free (s);
1085 if (err)
1086 {
Damjan Marionf9a968e2018-02-25 23:26:22 +01001087 if (p->fd != -1)
1088 close (p->fd);
Chris Luke30684ac2018-03-29 12:56:58 -07001089 if (p->config_fd != -1 && p->config_fd != p->fd)
Damjan Marion96504182017-12-10 23:54:46 +01001090 close (p->config_fd);
Chris Luke30684ac2018-03-29 12:56:58 -07001091 p->config_fd = p->fd = -1;
Damjan Marioncef87f12017-10-05 15:32:41 +02001092 }
1093 return err;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001094}
1095
1096/* Configuration space read/write. */
1097clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001098vlib_pci_read_write_config (vlib_main_t * vm, vlib_pci_dev_handle_t h,
Damjan Marion5a206ea2016-05-12 22:11:03 +02001099 vlib_read_or_write_t read_or_write,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001100 uword address, void *data, u32 n_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001101{
Damjan Marioncef87f12017-10-05 15:32:41 +02001102 linux_pci_device_t *p = linux_pci_get_device (h);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001103 int n;
1104
Ed Warnickecb9cada2015-12-08 15:45:58 -07001105 if (read_or_write == VLIB_READ)
Damjan Marionf9a968e2018-02-25 23:26:22 +01001106 n = pread (p->config_fd, data, n_bytes, p->config_offset + address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001107 else
Damjan Marionf9a968e2018-02-25 23:26:22 +01001108 n = pwrite (p->config_fd, data, n_bytes, p->config_offset + address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001109
1110 if (n != n_bytes)
1111 return clib_error_return_unix (0, "%s",
1112 read_or_write == VLIB_READ
1113 ? "read" : "write");
1114
1115 return 0;
1116}
1117
1118static clib_error_t *
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001119vlib_pci_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 bar, int *fd,
1120 u64 * size, u64 * offset)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001121{
Damjan Marioncef87f12017-10-05 15:32:41 +02001122 linux_pci_device_t *p = linux_pci_get_device (h);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001123 clib_error_t *error = 0;
1124 int _fd = -1;
1125 u64 _size = 0, _offset = 0;
Damjan Marion2060db82018-03-04 17:37:15 +01001126
1127 ASSERT (bar <= 5);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001128
1129 error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001130
Damjan Marion2060db82018-03-04 17:37:15 +01001131 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001132 {
Damjan Marion2060db82018-03-04 17:37:15 +01001133 u8 *file_name;
1134 struct stat stat_buf;
1135 file_name = format (0, "%s/%U/resource%d%c", sysfs_pci_dev_path,
1136 format_vlib_pci_addr, &p->addr, bar, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001137
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001138 _fd = open ((char *) file_name, O_RDWR);
1139 if (_fd < 0)
Damjan Marion2060db82018-03-04 17:37:15 +01001140 {
1141 error = clib_error_return_unix (0, "open `%s'", file_name);
1142 vec_free (file_name);
1143 return error;
1144 }
1145
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001146 if (fstat (_fd, &stat_buf) < 0)
Damjan Marion2060db82018-03-04 17:37:15 +01001147 {
1148 error = clib_error_return_unix (0, "fstat `%s'", file_name);
1149 vec_free (file_name);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001150 close (_fd);
Damjan Marion2060db82018-03-04 17:37:15 +01001151 return error;
1152 }
1153
1154 vec_free (file_name);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001155 _size = stat_buf.st_size;
1156 _offset = 0;
Damjan Marion2060db82018-03-04 17:37:15 +01001157 }
1158 else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001159 {
Damjan Marion0792bb42020-05-25 19:31:42 +02001160 struct vfio_region_info *r;
1161 u32 sz = sizeof (struct vfio_region_info);
1162 again:
1163 r = clib_mem_alloc (sz);
1164 clib_memset (r, 0, sz);
1165 r->argsz = sz;
1166 r->index = bar;
1167 if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, r) < 0)
Damjan Marion2060db82018-03-04 17:37:15 +01001168 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) "
1169 "'%U'", format_vlib_pci_addr,
1170 &p->addr);
Damjan Marion0792bb42020-05-25 19:31:42 +02001171 if (sz != r->argsz)
1172 {
1173 sz = r->argsz;
1174 clib_mem_free (r);
1175 goto again;
1176 }
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001177 _fd = p->fd;
Damjan Marion0792bb42020-05-25 19:31:42 +02001178 _size = r->size;
1179 _offset = r->offset;
1180 pci_log_debug (vm, p, "%s %U", __func__, format_vfio_region_info, r);
1181 clib_mem_free (r);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001182 }
Damjan Marion2060db82018-03-04 17:37:15 +01001183 else
1184 ASSERT (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001185
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001186 *fd = _fd;
1187 *size = _size;
1188 *offset = _offset;
1189
1190 return error;
1191}
1192
1193static clib_error_t *
1194vlib_pci_map_region_int (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1195 u32 bar, u8 * addr, void **result)
1196{
1197 linux_pci_device_t *p = linux_pci_get_device (h);
1198 int fd = -1;
1199 clib_error_t *error;
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001200 u64 size = 0, offset = 0;
Mohammed Hawari70fc36f2020-10-21 16:41:30 +02001201 u16 command;
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001202
1203 pci_log_debug (vm, p, "map region %u to va %p", bar, addr);
1204
Mohammed Hawari70fc36f2020-10-21 16:41:30 +02001205 if ((error = vlib_pci_read_config_u16 (vm, h, 4, &command)))
1206 return error;
1207
1208 if (!(command & PCI_COMMAND_MEMORY))
1209 {
1210 pci_log_debug (vm, p, "setting memory enable bit");
1211 command |= PCI_COMMAND_MEMORY;
1212 if ((error = vlib_pci_write_config_u16 (vm, h, 4, &command)))
1213 return error;
1214 }
1215
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001216 if ((error = vlib_pci_region (vm, h, bar, &fd, &size, &offset)))
1217 return error;
1218
Damjan Marion6bfd0762020-09-11 22:16:53 +02001219 *result = clib_mem_vm_map_shared (addr, size, fd, offset,
1220 "PCIe %U region %u", format_vlib_pci_addr,
1221 vlib_pci_get_addr (vm, h), bar);
1222 if (*result == CLIB_MEM_VM_MAP_FAILED)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001223 {
Damjan Marion2060db82018-03-04 17:37:15 +01001224 error = clib_error_return_unix (0, "mmap `BAR%u'", bar);
Dave Barachc72950e2020-05-04 14:47:45 -04001225 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO && (fd != -1))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001226 close (fd);
Damjan Marion2060db82018-03-04 17:37:15 +01001227 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001228 }
Damjan Marion2060db82018-03-04 17:37:15 +01001229
1230 /* *INDENT-OFF* */
1231 vec_validate_init_empty (p->regions, bar,
1232 (linux_pci_region_t) { .fd = -1});
1233 /* *INDENT-ON* */
1234 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1235 p->regions[bar].fd = fd;
1236 p->regions[bar].addr = *result;
1237 p->regions[bar].size = size;
1238 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001239}
1240
1241clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001242vlib_pci_map_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 resource,
1243 void **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001244{
Damjan Marion23227982018-10-22 13:38:57 +02001245 return (vlib_pci_map_region_int (vm, h, resource, 0 /* addr */ , result));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001246}
1247
1248clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001249vlib_pci_map_region_fixed (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1250 u32 resource, u8 * addr, void **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001251{
Damjan Marion23227982018-10-22 13:38:57 +02001252 return (vlib_pci_map_region_int (vm, h, resource, addr, result));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001253}
1254
Damjan Marionf313b742018-03-05 14:08:33 +01001255clib_error_t *
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001256vlib_pci_io_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 resource)
1257{
1258 linux_pci_device_t *p = linux_pci_get_device (h);
1259 clib_error_t *error = 0;
1260 int fd = -1;
1261 u64 size = 0, offset = 0;
1262
1263 if ((error = vlib_pci_region (vm, h, resource, &fd, &size, &offset)))
1264 return error;
1265
1266 p->io_fd = fd;
1267 p->io_offset = offset;
1268 return error;
1269}
1270
1271clib_error_t *
1272vlib_pci_read_write_io (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1273 vlib_read_or_write_t read_or_write,
1274 uword offset, void *data, u32 length)
1275{
1276 linux_pci_device_t *p = linux_pci_get_device (h);
1277 int n = 0;
1278
1279 if (read_or_write == VLIB_READ)
1280 n = pread (p->io_fd, data, length, p->io_offset + offset);
1281 else
1282 n = pwrite (p->io_fd, data, length, p->io_offset + offset);
1283
1284 if (n != length)
1285 return clib_error_return_unix (0, "%s",
1286 read_or_write == VLIB_READ
1287 ? "read" : "write");
1288 return 0;
1289}
1290
1291clib_error_t *
Damjan Marion68b4da62018-09-30 18:26:20 +02001292vlib_pci_map_dma (vlib_main_t * vm, vlib_pci_dev_handle_t h, void *ptr)
1293{
1294 linux_pci_device_t *p = linux_pci_get_device (h);
1295
1296 if (!p->supports_va_dma)
1297 return 0;
1298
1299 return vfio_map_physmem_page (vm, ptr);
1300}
1301
1302int
1303vlib_pci_supports_virtual_addr_dma (vlib_main_t * vm, vlib_pci_dev_handle_t h)
1304{
1305 linux_pci_device_t *p = linux_pci_get_device (h);
1306
1307 return p->supports_va_dma != 0;
1308}
1309
1310clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001311vlib_pci_device_open (vlib_main_t * vm, vlib_pci_addr_t * addr,
Damjan Marionf313b742018-03-05 14:08:33 +01001312 pci_device_id_t ids[], vlib_pci_dev_handle_t * handle)
1313{
1314 linux_pci_main_t *lpm = &linux_pci_main;
1315 vlib_pci_device_info_t *di;
1316 linux_pci_device_t *p;
1317 clib_error_t *err = 0;
1318 pci_device_id_t *i;
1319
Damjan Marion23227982018-10-22 13:38:57 +02001320 di = vlib_pci_get_device_info (vm, addr, &err);
Damjan Marionf313b742018-03-05 14:08:33 +01001321
1322 if (err)
1323 return err;
1324 for (i = ids; i->vendor_id != 0; i++)
1325 if (i->vendor_id == di->vendor_id && i->device_id == di->device_id)
1326 break;
1327
1328 if (i->vendor_id == 0)
1329 return clib_error_return (0, "Wrong vendor or device id");
1330
1331 pool_get (lpm->linux_pci_devices, p);
1332 p->handle = p - lpm->linux_pci_devices;
Damjan Marion23227982018-10-22 13:38:57 +02001333 p->addr.as_u32 = di->addr.as_u32;
Damjan Marionf313b742018-03-05 14:08:33 +01001334 p->intx_irq.fd = -1;
Mohsin Kazmi692f9b12019-04-02 11:09:49 +00001335 p->intx_irq.clib_file_index = -1;
Damjan Mariond2bfb782019-01-07 20:56:04 +01001336 p->numa_node = di->numa_node;
Mohsin Kazmi3d3b9552018-10-24 14:05:34 +02001337 /*
1338 * pci io bar read/write fd
1339 */
1340 p->io_fd = -1;
Damjan Marionf313b742018-03-05 14:08:33 +01001341
Damjan Marion23227982018-10-22 13:38:57 +02001342 pci_log_debug (vm, p, "open vid:0x%04x did:0x%04x driver:%s iommu_group:%d",
1343 di->vendor_id, di->device_id, di->driver_name,
1344 di->iommu_group);
1345
Steven Luongab489922019-08-21 11:35:27 -07001346 if (clib_strncmp ("vfio-pci", (char *) di->driver_name, 8) == 0)
Damjan Marion23227982018-10-22 13:38:57 +02001347 err = add_device_vfio (vm, p, di, 0);
Steven Luongab489922019-08-21 11:35:27 -07001348 else if (clib_strncmp ("uio_pci_generic", (char *) di->driver_name, 8) == 0)
Damjan Marion23227982018-10-22 13:38:57 +02001349 err = add_device_uio (vm, p, di, 0);
Damjan Marionf313b742018-03-05 14:08:33 +01001350 else
Damjan Marion23227982018-10-22 13:38:57 +02001351 err = clib_error_create ("device not bound to 'vfio-pci' or "
1352 "'uio_pci_generic' kernel module");
Damjan Marionf313b742018-03-05 14:08:33 +01001353 if (err)
1354 goto error;
1355
1356 *handle = p->handle;
1357
1358error:
1359 vlib_pci_free_device_info (di);
1360 if (err)
1361 {
Damjan Marion23227982018-10-22 13:38:57 +02001362 pci_log_err (vm, p, "%U", format_clib_error, err);
Dave Barachb7b92992018-10-17 10:38:51 -04001363 clib_memset (p, 0, sizeof (linux_pci_device_t));
Damjan Marionf313b742018-03-05 14:08:33 +01001364 pool_put (lpm->linux_pci_devices, p);
1365 }
1366
1367 return err;
1368}
1369
1370void
Damjan Marion23227982018-10-22 13:38:57 +02001371vlib_pci_device_close (vlib_main_t * vm, vlib_pci_dev_handle_t h)
Damjan Marionf313b742018-03-05 14:08:33 +01001372{
1373 linux_pci_main_t *lpm = &linux_pci_main;
1374 linux_pci_device_t *p = linux_pci_get_device (h);
1375 linux_pci_irq_t *irq;
1376 linux_pci_region_t *res;
1377 clib_error_t *err = 0;
1378
1379 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1380 {
1381 irq = &p->intx_irq;
Mohsin Kazmi692f9b12019-04-02 11:09:49 +00001382 if (irq->clib_file_index != -1)
1383 clib_file_del_by_index (&file_main, irq->clib_file_index);
Damjan Marionf313b742018-03-05 14:08:33 +01001384 close (p->config_fd);
Mohsin Kazmi3d3b9552018-10-24 14:05:34 +02001385 if (p->io_fd != -1)
1386 close (p->io_fd);
Damjan Marionf313b742018-03-05 14:08:33 +01001387 }
1388 else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
1389 {
1390 irq = &p->intx_irq;
1391 /* close INTx irqs */
1392 if (irq->fd != -1)
1393 {
Damjan Marion23227982018-10-22 13:38:57 +02001394 err = vfio_set_irqs (vm, p, VFIO_PCI_INTX_IRQ_INDEX, 0, 0,
Damjan Marionf313b742018-03-05 14:08:33 +01001395 VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1396 clib_error_free (err);
Mohsin Kazmi692f9b12019-04-02 11:09:49 +00001397 if (irq->clib_file_index != -1)
1398 clib_file_del_by_index (&file_main, irq->clib_file_index);
Damjan Marionf313b742018-03-05 14:08:33 +01001399 close (irq->fd);
1400 }
1401
1402 /* close MSI-X irqs */
1403 if (vec_len (p->msix_irqs))
1404 {
Damjan Marion23227982018-10-22 13:38:57 +02001405 err = vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, 0, 0,
Damjan Marionf313b742018-03-05 14:08:33 +01001406 VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1407 clib_error_free (err);
1408 /* *INDENT-OFF* */
1409 vec_foreach (irq, p->msix_irqs)
1410 {
1411 if (irq->fd == -1)
1412 continue;
1413 clib_file_del_by_index (&file_main, irq->clib_file_index);
1414 close (irq->fd);
1415 }
1416 /* *INDENT-ON* */
1417 vec_free (p->msix_irqs);
1418 }
1419 }
1420
1421 /* *INDENT-OFF* */
1422 vec_foreach (res, p->regions)
1423 {
1424 if (res->size == 0)
1425 continue;
Damjan Marion6bfd0762020-09-11 22:16:53 +02001426 clib_mem_vm_unmap (res->addr);
Damjan Marionf313b742018-03-05 14:08:33 +01001427 if (res->fd != -1)
1428 close (res->fd);
1429 }
1430 /* *INDENT-ON* */
1431 vec_free (p->regions);
1432
1433 close (p->fd);
Dave Barachb7b92992018-10-17 10:38:51 -04001434 clib_memset (p, 0, sizeof (linux_pci_device_t));
Damjan Marionf313b742018-03-05 14:08:33 +01001435 pool_put (lpm->linux_pci_devices, p);
1436}
1437
Dave Barach9b8ffd92016-07-08 08:13:45 -04001438void
Damjan Marion23227982018-10-22 13:38:57 +02001439init_device_from_registered (vlib_main_t * vm, vlib_pci_device_info_t * di)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001440{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001441 vlib_pci_main_t *pm = &pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +02001442 linux_pci_main_t *lpm = &linux_pci_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001443 pci_device_registration_t *r;
1444 pci_device_id_t *i;
Damjan Marioncef87f12017-10-05 15:32:41 +02001445 clib_error_t *err = 0;
1446 linux_pci_device_t *p;
1447
1448 pool_get (lpm->linux_pci_devices, p);
1449 p->handle = p - lpm->linux_pci_devices;
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001450 p->intx_irq.fd = -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001451
Damjan Marion5a206ea2016-05-12 22:11:03 +02001452 r = pm->pci_device_registrations;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001453
1454 while (r)
1455 {
1456 for (i = r->supported_devices; i->vendor_id != 0; i++)
Damjan Marion3a593822018-02-17 14:06:53 +01001457 if (i->vendor_id == di->vendor_id && i->device_id == di->device_id)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001458 {
Damjan Marioncef87f12017-10-05 15:32:41 +02001459 if (di->iommu_group != -1)
Damjan Marion23227982018-10-22 13:38:57 +02001460 err = add_device_vfio (vm, p, di, r);
Damjan Marioncef87f12017-10-05 15:32:41 +02001461 else
Damjan Marion23227982018-10-22 13:38:57 +02001462 err = add_device_uio (vm, p, di, r);
Damjan Marion5a206ea2016-05-12 22:11:03 +02001463
Damjan Marioncef87f12017-10-05 15:32:41 +02001464 if (err)
1465 clib_error_report (err);
1466 else
1467 return;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001468 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001469 r = r->next_registration;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001470 }
Damjan Marioncef87f12017-10-05 15:32:41 +02001471
Ed Warnickecb9cada2015-12-08 15:45:58 -07001472 /* No driver, close the PCI config-space FD */
Dave Barachb7b92992018-10-17 10:38:51 -04001473 clib_memset (p, 0, sizeof (linux_pci_device_t));
Damjan Marioncef87f12017-10-05 15:32:41 +02001474 pool_put (lpm->linux_pci_devices, p);
1475}
1476
1477static clib_error_t *
1478scan_pci_addr (void *arg, u8 * dev_dir_name, u8 * ignored)
1479{
1480 vlib_pci_addr_t addr, **addrv = arg;
1481 unformat_input_t input;
1482 clib_error_t *err = 0;
1483
1484 unformat_init_string (&input, (char *) dev_dir_name,
1485 vec_len (dev_dir_name));
1486
1487 if (!unformat (&input, "/sys/bus/pci/devices/%U",
1488 unformat_vlib_pci_addr, &addr))
1489 err = clib_error_return (0, "unformat error `%v`", dev_dir_name);
1490
1491 unformat_free (&input);
1492
1493 if (err)
1494 return err;
1495
1496 vec_add1 (*addrv, addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001497 return 0;
1498}
1499
Damjan Marioncef87f12017-10-05 15:32:41 +02001500static int
1501pci_addr_cmp (void *v1, void *v2)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001502{
Damjan Marioncef87f12017-10-05 15:32:41 +02001503 vlib_pci_addr_t *a1 = v1;
1504 vlib_pci_addr_t *a2 = v2;
1505
1506 if (a1->domain > a2->domain)
1507 return 1;
1508 if (a1->domain < a2->domain)
1509 return -1;
1510 if (a1->bus > a2->bus)
1511 return 1;
1512 if (a1->bus < a2->bus)
1513 return -1;
1514 if (a1->slot > a2->slot)
1515 return 1;
1516 if (a1->slot < a2->slot)
1517 return -1;
1518 if (a1->function > a2->function)
1519 return 1;
1520 if (a1->function < a2->function)
1521 return -1;
1522 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001523}
1524
Damjan Marioncef87f12017-10-05 15:32:41 +02001525vlib_pci_addr_t *
1526vlib_pci_get_all_dev_addrs ()
Ed Warnickecb9cada2015-12-08 15:45:58 -07001527{
Damjan Marioncef87f12017-10-05 15:32:41 +02001528 vlib_pci_addr_t *addrs = 0;
1529 clib_error_t *err;
1530 err = foreach_directory_file ((char *) sysfs_pci_dev_path, scan_pci_addr,
1531 &addrs, /* scan_dirs */ 0);
1532 if (err)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001533 {
Damjan Marioncef87f12017-10-05 15:32:41 +02001534 vec_free (addrs);
1535 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001536 }
1537
Damjan Marioncef87f12017-10-05 15:32:41 +02001538 vec_sort_with_function (addrs, pci_addr_cmp);
Damjan Mariona42cd342016-04-13 18:03:20 +02001539
Damjan Marioncef87f12017-10-05 15:32:41 +02001540 return addrs;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001541}
1542
Dave Barach9b8ffd92016-07-08 08:13:45 -04001543clib_error_t *
1544linux_pci_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001545{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001546 vlib_pci_main_t *pm = &pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +02001547 vlib_pci_addr_t *addr = 0, *addrs;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001548
1549 pm->vlib_main = vm;
1550
Dave Barach9b8ffd92016-07-08 08:13:45 -04001551 ASSERT (sizeof (vlib_pci_addr_t) == sizeof (u32));
Damjan Mariona42cd342016-04-13 18:03:20 +02001552
Damjan Marioncef87f12017-10-05 15:32:41 +02001553 addrs = vlib_pci_get_all_dev_addrs ();
1554 /* *INDENT-OFF* */
1555 vec_foreach (addr, addrs)
1556 {
1557 vlib_pci_device_info_t *d;
Damjan Marion23227982018-10-22 13:38:57 +02001558 if ((d = vlib_pci_get_device_info (vm, addr, 0)))
Damjan Marioncef87f12017-10-05 15:32:41 +02001559 {
Damjan Marion23227982018-10-22 13:38:57 +02001560 init_device_from_registered (vm, d);
Damjan Marioncef87f12017-10-05 15:32:41 +02001561 vlib_pci_free_device_info (d);
1562 }
1563 }
1564 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001565
Dave Barachf8d50682019-05-14 18:01:44 -04001566 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001567}
1568
Dave Barachf8d50682019-05-14 18:01:44 -04001569/* *INDENT-OFF* */
1570VLIB_INIT_FUNCTION (linux_pci_init) =
1571{
1572 .runs_after = VLIB_INITS("unix_input_init"),
1573};
1574/* *INDENT-ON* */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001575
1576/*
1577 * fd.io coding-style-patch-verification: ON
1578 *
1579 * Local Variables:
1580 * eval: (c-set-style "gnu")
1581 * End:
1582 */