blob: 9f0629f46bcd53a43be95947ba57a2081c08da98 [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 *
Damjan Marion23227982018-10-22 13:38:57 +0200456vlib_pci_bind_to_uio (vlib_main_t * vm, vlib_pci_addr_t * addr,
457 char *uio_drv_name)
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
Damjan Mariona42cd342016-04-13 18:03:20 +0200526 /* walk trough all linux interfaces and if interface belonging to
527 this device is founf check if interface is admin up */
528 dir = opendir ("/sys/class/net");
Damjan Marioncef87f12017-10-05 15:32:41 +0200529 s = format (s, "%U%c", format_vlib_pci_addr, addr, 0);
Damjan Mariona42cd342016-04-13 18:03:20 +0200530
531 if (!dir)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700532 {
Damjan Mariona42cd342016-04-13 18:03:20 +0200533 error = clib_error_return (0, "Skipping PCI device %U: failed to "
534 "read /sys/class/net",
Damjan Marioncef87f12017-10-05 15:32:41 +0200535 format_vlib_pci_addr, addr);
Damjan Mariona42cd342016-04-13 18:03:20 +0200536 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700537 }
538
Dave Barach9b8ffd92016-07-08 08:13:45 -0400539 fd = socket (PF_INET, SOCK_DGRAM, 0);
Chris Luke370e9e32016-07-07 11:01:17 -0400540 if (fd < 0)
541 {
542 error = clib_error_return_unix (0, "socket");
543 goto done;
544 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200545
Dave Barach9b8ffd92016-07-08 08:13:45 -0400546 while ((e = readdir (dir)))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700547 {
Damjan Mariona42cd342016-04-13 18:03:20 +0200548 struct ifreq ifr;
549 struct ethtool_drvinfo drvinfo;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700550
Dave Barach9b8ffd92016-07-08 08:13:45 -0400551 if (e->d_name[0] == '.') /* skip . and .. */
Damjan Mariona42cd342016-04-13 18:03:20 +0200552 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700553
Dave Barachb7b92992018-10-17 10:38:51 -0400554 clib_memset (&ifr, 0, sizeof ifr);
555 clib_memset (&drvinfo, 0, sizeof drvinfo);
Damjan Mariona42cd342016-04-13 18:03:20 +0200556 ifr.ifr_data = (char *) &drvinfo;
Steven Luongddccf7b2019-01-18 22:53:18 -0800557 clib_strncpy (ifr.ifr_name, e->d_name, sizeof (ifr.ifr_name) - 1);
Stephen Hemminger4e8a8312019-01-16 10:23:16 -0800558
Damjan Mariona42cd342016-04-13 18:03:20 +0200559 drvinfo.cmd = ETHTOOL_GDRVINFO;
Chris Luke370e9e32016-07-07 11:01:17 -0400560 if (ioctl (fd, SIOCETHTOOL, &ifr) < 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400561 {
John Loe2821212016-07-13 18:13:02 -0400562 /* Some interfaces (eg "lo") don't support this ioctl */
563 if ((errno != ENOTSUP) && (errno != ENODEV))
Ed Warnicke853e7202016-08-12 11:42:26 -0700564 clib_unix_warning ("ioctl fetch intf %s bus info error",
565 e->d_name);
John Loe2821212016-07-13 18:13:02 -0400566 continue;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400567 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200568
569 if (strcmp ((char *) s, drvinfo.bus_info))
570 continue;
571
Dave Barachb7b92992018-10-17 10:38:51 -0400572 clib_memset (&ifr, 0, sizeof (ifr));
Steven Luongddccf7b2019-01-18 22:53:18 -0800573 clib_strncpy (ifr.ifr_name, e->d_name, sizeof (ifr.ifr_name) - 1);
Stephen Hemminger4e8a8312019-01-16 10:23:16 -0800574
Chris Luke370e9e32016-07-07 11:01:17 -0400575 if (ioctl (fd, SIOCGIFFLAGS, &ifr) < 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400576 {
577 error = clib_error_return_unix (0, "ioctl fetch intf %s flags",
578 e->d_name);
579 close (fd);
580 goto done;
581 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200582
583 if (ifr.ifr_flags & IFF_UP)
584 {
Dave Barachecf98c02020-12-06 08:15:18 -0500585 vlib_log (VLIB_LOG_LEVEL_WARNING, pci_main.log_default,
586 "Skipping PCI device %U as host "
587 "interface %s is up", format_vlib_pci_addr, addr,
588 e->d_name);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400589 close (fd);
Damjan Mariona42cd342016-04-13 18:03:20 +0200590 goto done;
591 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700592 }
593
Damjan Mariona42cd342016-04-13 18:03:20 +0200594 close (fd);
595 vec_reset_length (s);
596
597 s = format (s, "%v/driver/unbind%c", dev_dir_name, 0);
Damjan Marioncef87f12017-10-05 15:32:41 +0200598 clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
Damjan Mariona42cd342016-04-13 18:03:20 +0200599 vec_reset_length (s);
600
Damjan Mariona7f74572017-05-23 18:32:21 +0200601 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
602 if (access ((char *) s, F_OK) == 0)
603 {
Damjan Marioncef87f12017-10-05 15:32:41 +0200604 clib_sysfs_write ((char *) s, "%s", uio_drv_name);
Damjan Mariona7f74572017-05-23 18:32:21 +0200605 clear_driver_override = 1;
606 }
607 else
608 {
609 vec_reset_length (s);
Damjan Marioncef87f12017-10-05 15:32:41 +0200610 s = format (s, "%s/%s/new_id%c", sysfs_pci_drv_path, uio_drv_name, 0);
611 clib_sysfs_write ((char *) s, "0x%04x 0x%04x", di->vendor_id,
612 di->device_id);
Damjan Mariona7f74572017-05-23 18:32:21 +0200613 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200614 vec_reset_length (s);
615
Damjan Marioncef87f12017-10-05 15:32:41 +0200616 s = format (s, "%s/%s/bind%c", sysfs_pci_drv_path, uio_drv_name, 0);
617 clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
Damjan Mariona7f74572017-05-23 18:32:21 +0200618 vec_reset_length (s);
619
620 if (clear_driver_override)
621 {
622 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
Damjan Marion01914ce2017-09-14 19:04:50 +0200623 clib_sysfs_write ((char *) s, "%c", 0);
Damjan Mariona7f74572017-05-23 18:32:21 +0200624 vec_reset_length (s);
625 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200626
627done:
628 closedir (dir);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700629 vec_free (s);
Damjan Mariona42cd342016-04-13 18:03:20 +0200630 vec_free (dev_dir_name);
Damjan Mariona7f74572017-05-23 18:32:21 +0200631 vec_free (driver_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700632 return error;
633}
634
Ed Warnickecb9cada2015-12-08 15:45:58 -0700635
636static clib_error_t *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400637scan_uio_dir (void *arg, u8 * path_name, u8 * file_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700638{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400639 linux_pci_device_t *l = arg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700640 unformat_input_t input;
641
642 unformat_init_string (&input, (char *) file_name, vec_len (file_name));
643
Dave Barach9b8ffd92016-07-08 08:13:45 -0400644 if (!unformat (&input, "uio%d", &l->uio_minor))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700645 abort ();
646
647 unformat_free (&input);
648 return 0;
649}
650
Dave Barach9b8ffd92016-07-08 08:13:45 -0400651static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200652vfio_set_irqs (vlib_main_t * vm, linux_pci_device_t * p, u32 index, u32 start,
653 u32 count, u32 flags, int *efds)
Damjan Marion599a16b2018-03-04 19:35:23 +0100654{
655 int data_len = efds ? count * sizeof (int) : 0;
656 u8 buf[sizeof (struct vfio_irq_set) + data_len];
Damjan Marion23227982018-10-22 13:38:57 +0200657 struct vfio_irq_info ii = { 0 };
Damjan Marion599a16b2018-03-04 19:35:23 +0100658 struct vfio_irq_set *irq_set = (struct vfio_irq_set *) buf;
659
660
Damjan Marion23227982018-10-22 13:38:57 +0200661 ii.argsz = sizeof (struct vfio_irq_info);
662 ii.index = index;
Damjan Marion599a16b2018-03-04 19:35:23 +0100663
Damjan Marion23227982018-10-22 13:38:57 +0200664 if (ioctl (p->fd, VFIO_DEVICE_GET_IRQ_INFO, &ii) < 0)
Damjan Marion599a16b2018-03-04 19:35:23 +0100665 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) "
666 "'%U'", format_vlib_pci_addr, &p->addr);
667
Damjan Marion23227982018-10-22 13:38:57 +0200668 pci_log_debug (vm, p, "%s index:%u count:%u flags: %s%s%s%s(0x%x)",
669 __func__, ii.index, ii.count,
670 ii.flags & VFIO_IRQ_INFO_EVENTFD ? "eventfd " : "",
671 ii.flags & VFIO_IRQ_INFO_MASKABLE ? "maskable " : "",
672 ii.flags & VFIO_IRQ_INFO_AUTOMASKED ? "automasked " : "",
673 ii.flags & VFIO_IRQ_INFO_NORESIZE ? "noresize " : "",
674 ii.flags);
675
676 if (ii.count < start + count)
Damjan Marion599a16b2018-03-04 19:35:23 +0100677 return clib_error_return_unix (0, "vfio_set_irq: unexistng interrupt on "
678 "'%U'", format_vlib_pci_addr, &p->addr);
679
680
681 if (efds)
682 {
683 flags |= VFIO_IRQ_SET_DATA_EVENTFD;
Dave Barach178cf492018-11-13 16:34:13 -0500684 clib_memcpy_fast (&irq_set->data, efds, data_len);
Damjan Marion599a16b2018-03-04 19:35:23 +0100685 }
686 else
687 flags |= VFIO_IRQ_SET_DATA_NONE;
688
689 ASSERT ((flags & (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD)) !=
690 (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD));
691
692 irq_set->argsz = sizeof (struct vfio_irq_set) + data_len;
693 irq_set->index = index;
694 irq_set->start = start;
695 irq_set->count = count;
696 irq_set->flags = flags;
697
698 if (ioctl (p->fd, VFIO_DEVICE_SET_IRQS, irq_set) < 0)
699 return clib_error_return_unix (0, "%U:ioctl(VFIO_DEVICE_SET_IRQS) "
700 "[index = %u, start = %u, count = %u, "
701 "flags = 0x%x]",
702 format_vlib_pci_addr, &p->addr,
703 index, start, count, flags);
704 return 0;
705}
706
707static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +0200708linux_pci_uio_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700709{
Damjan Marion23227982018-10-22 13:38:57 +0200710 vlib_main_t *vm = vlib_get_main ();
Damjan Marion5a206ea2016-05-12 22:11:03 +0200711 int __attribute__ ((unused)) rv;
Damjan Marioncef87f12017-10-05 15:32:41 +0200712 vlib_pci_dev_handle_t h = uf->private_data;
713 linux_pci_device_t *p = linux_pci_get_device (h);
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100714 linux_pci_irq_t *irq = &p->intx_irq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700715
Damjan Marion5a206ea2016-05-12 22:11:03 +0200716 u32 icount;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400717 rv = read (uf->file_descriptor, &icount, 4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700718
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100719 if (irq->intx_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200720 irq->intx_handler (vm, h);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200721
Damjan Marion23227982018-10-22 13:38:57 +0200722 vlib_pci_intr_enable (vm, h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700723
724 return /* no error */ 0;
725}
726
Dave Barach9b8ffd92016-07-08 08:13:45 -0400727static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200728linux_pci_vfio_unmask_intx (vlib_main_t * vm, linux_pci_device_t * d)
Damjan Marioncef87f12017-10-05 15:32:41 +0200729{
Damjan Marion23227982018-10-22 13:38:57 +0200730 return vfio_set_irqs (vm, d, VFIO_PCI_INTX_IRQ_INDEX, 0, 1,
Damjan Marion599a16b2018-03-04 19:35:23 +0100731 VFIO_IRQ_SET_ACTION_UNMASK, 0);
732}
733
Damjan Marioncef87f12017-10-05 15:32:41 +0200734static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +0200735linux_pci_uio_error_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700736{
737 u32 error_index = (u32) uf->private_data;
738
739 return clib_error_return (0, "pci device %d: error", error_index);
740}
741
Damjan Marioncef87f12017-10-05 15:32:41 +0200742static clib_error_t *
Damjan Marion599a16b2018-03-04 19:35:23 +0100743linux_pci_vfio_msix_read_ready (clib_file_t * uf)
744{
Damjan Marion23227982018-10-22 13:38:57 +0200745 vlib_main_t *vm = vlib_get_main ();
Damjan Marion599a16b2018-03-04 19:35:23 +0100746 int __attribute__ ((unused)) rv;
747 vlib_pci_dev_handle_t h = uf->private_data >> 16;
748 u16 line = uf->private_data & 0xffff;
749 linux_pci_device_t *p = linux_pci_get_device (h);
750 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, line);
751
752 u64 icount;
753 rv = read (uf->file_descriptor, &icount, sizeof (icount));
754
755 if (irq->msix_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200756 irq->msix_handler (vm, h, line);
Damjan Marion599a16b2018-03-04 19:35:23 +0100757
758 return /* no error */ 0;
759}
760
761static clib_error_t *
762linux_pci_vfio_intx_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700763{
Damjan Marion23227982018-10-22 13:38:57 +0200764 vlib_main_t *vm = vlib_get_main ();
Damjan Marioncef87f12017-10-05 15:32:41 +0200765 int __attribute__ ((unused)) rv;
766 vlib_pci_dev_handle_t h = uf->private_data;
767 linux_pci_device_t *p = linux_pci_get_device (h);
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100768 linux_pci_irq_t *irq = &p->intx_irq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700769
Damjan Marioncef87f12017-10-05 15:32:41 +0200770 u64 icount;
771 rv = read (uf->file_descriptor, &icount, sizeof (icount));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700772
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100773 if (irq->intx_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200774 irq->intx_handler (vm, h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700775
Damjan Marion23227982018-10-22 13:38:57 +0200776 linux_pci_vfio_unmask_intx (vm, p);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700777
Damjan Marioncef87f12017-10-05 15:32:41 +0200778 return /* no error */ 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700779}
780
Damjan Marioncef87f12017-10-05 15:32:41 +0200781static clib_error_t *
782linux_pci_vfio_error_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700783{
Damjan Marioncef87f12017-10-05 15:32:41 +0200784 u32 error_index = (u32) uf->private_data;
785
786 return clib_error_return (0, "pci device %d: error", error_index);
787}
788
789static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200790add_device_uio (vlib_main_t * vm, linux_pci_device_t * p,
791 vlib_pci_device_info_t * di, pci_device_registration_t * r)
Damjan Marioncef87f12017-10-05 15:32:41 +0200792{
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100793 linux_pci_main_t *lpm = &linux_pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +0200794 clib_error_t *err = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200795 u8 *s = 0;
796
Damjan Marionf9a968e2018-02-25 23:26:22 +0100797 p->fd = -1;
Damjan Marion2060db82018-03-04 17:37:15 +0100798 p->type = LINUX_PCI_DEVICE_TYPE_UIO;
Damjan Marioncef87f12017-10-05 15:32:41 +0200799
800 s = format (s, "%s/%U/config%c", sysfs_pci_dev_path,
801 format_vlib_pci_addr, &di->addr, 0);
802
803 p->config_fd = open ((char *) s, O_RDWR);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100804 p->config_offset = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200805 vec_reset_length (s);
806
807 if (p->config_fd == -1)
808 {
809 err = clib_error_return_unix (0, "open '%s'", s);
810 goto error;
811 }
812
Benoît Ganne0b91bd62019-11-20 09:07:50 +0100813 s = format (0, "%s/%U/uio%c", sysfs_pci_dev_path,
814 format_vlib_pci_addr, &di->addr, 0);
Damjan Marioncef87f12017-10-05 15:32:41 +0200815 foreach_directory_file ((char *) s, scan_uio_dir, p, /* scan_dirs */
816 1);
817 vec_reset_length (s);
818
819 s = format (s, "/dev/uio%d%c", p->uio_minor, 0);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100820 p->fd = open ((char *) s, O_RDWR);
821 if (p->fd < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +0200822 {
823 err = clib_error_return_unix (0, "open '%s'", s);
824 goto error;
825 }
826
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100827 if (r && r->interrupt_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200828 vlib_pci_register_intx_handler (vm, p->handle, r->interrupt_handler);
Damjan Marioncef87f12017-10-05 15:32:41 +0200829
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100830 if (r && r->init_function)
831 err = r->init_function (lpm->vlib_main, p->handle);
Damjan Marioncef87f12017-10-05 15:32:41 +0200832
833error:
Damjan Marion20ba1642018-03-26 15:35:33 +0200834 vec_free (s);
Damjan Marioncef87f12017-10-05 15:32:41 +0200835 if (err)
836 {
Damjan Marion96504182017-12-10 23:54:46 +0100837 if (p->config_fd != -1)
838 close (p->config_fd);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100839 if (p->fd != -1)
840 close (p->fd);
Damjan Marioncef87f12017-10-05 15:32:41 +0200841 }
842 return err;
843}
844
Damjan Marion599a16b2018-03-04 19:35:23 +0100845clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200846vlib_pci_register_intx_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h,
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100847 pci_intx_handler_function_t * intx_handler)
848{
849 linux_pci_device_t *p = linux_pci_get_device (h);
850 clib_file_t t = { 0 };
851 linux_pci_irq_t *irq = &p->intx_irq;
852 ASSERT (irq->fd == -1);
853
854 if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
855 {
Damjan Marion23227982018-10-22 13:38:57 +0200856 struct vfio_irq_info ii = { 0 };
857 ii.argsz = sizeof (struct vfio_irq_info);
858 ii.index = VFIO_PCI_INTX_IRQ_INDEX;
859 if (ioctl (p->fd, VFIO_DEVICE_GET_IRQ_INFO, &ii) < 0)
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100860 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) '"
861 "%U'", format_vlib_pci_addr, &p->addr);
Damjan Marion23227982018-10-22 13:38:57 +0200862 pci_log_debug (vm, p, "%s index:%u count:%u flags: %s%s%s%s(0x%x)",
863 __func__, ii.index, ii.count,
864 ii.flags & VFIO_IRQ_INFO_EVENTFD ? "eventfd " : "",
865 ii.flags & VFIO_IRQ_INFO_MASKABLE ? "maskable " : "",
866 ii.flags & VFIO_IRQ_INFO_AUTOMASKED ? "automasked " : "",
867 ii.flags & VFIO_IRQ_INFO_NORESIZE ? "noresize " : "",
868 ii.flags);
869 if (ii.count != 1)
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100870 return clib_error_return (0, "INTx interrupt does not exist on device"
871 "'%U'", format_vlib_pci_addr, &p->addr);
872
873 irq->fd = eventfd (0, EFD_NONBLOCK);
874 if (irq->fd == -1)
875 return clib_error_return_unix (0, "eventfd");
Damjan Marion23227982018-10-22 13:38:57 +0200876
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100877 t.file_descriptor = irq->fd;
878 t.read_function = linux_pci_vfio_intx_read_ready;
879 }
880 else if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
881 {
882 t.file_descriptor = p->fd;
883 t.read_function = linux_pci_uio_read_ready;
884 }
885 else
886 return 0;
887
888 t.error_function = linux_pci_uio_error_ready;
889 t.private_data = p->handle;
890 t.description = format (0, "PCI %U INTx", format_vlib_pci_addr, &p->addr);
891 irq->clib_file_index = clib_file_add (&file_main, &t);
892 irq->intx_handler = intx_handler;
893 return 0;
894}
895
896clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200897vlib_pci_register_msix_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h,
898 u32 start, u32 count,
Damjan Marion599a16b2018-03-04 19:35:23 +0100899 pci_msix_handler_function_t * msix_handler)
900{
901 clib_error_t *err = 0;
902 linux_pci_device_t *p = linux_pci_get_device (h);
903 u32 i;
904
905 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
906 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
907 "support");
908
909 /* *INDENT-OFF* */
910 vec_validate_init_empty (p->msix_irqs, start + count - 1, (linux_pci_irq_t)
911 { .fd = -1});
912 /* *INDENT-ON* */
913
914 for (i = start; i < start + count; i++)
915 {
916 clib_file_t t = { 0 };
917 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
918 ASSERT (irq->fd == -1);
919
920 irq->fd = eventfd (0, EFD_NONBLOCK);
921 if (irq->fd == -1)
922 {
923 err = clib_error_return_unix (0, "eventfd");
924 goto error;
925 }
926
927 t.read_function = linux_pci_vfio_msix_read_ready;
928 t.file_descriptor = irq->fd;
929 t.error_function = linux_pci_vfio_error_ready;
930 t.private_data = p->handle << 16 | i;
931 t.description = format (0, "PCI %U MSI-X #%u", format_vlib_pci_addr,
932 &p->addr, i);
933 irq->clib_file_index = clib_file_add (&file_main, &t);
934 irq->msix_handler = msix_handler;
935 }
936
937 return 0;
938
939error:
940 while (i-- > start)
941 {
942 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
943 if (irq->fd != -1)
944 {
945 clib_file_del_by_index (&file_main, irq->clib_file_index);
946 close (irq->fd);
947 irq->fd = -1;
948 }
949 }
950 return err;
951}
952
953clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200954vlib_pci_enable_msix_irq (vlib_main_t * vm, vlib_pci_dev_handle_t h,
955 u16 start, u16 count)
Damjan Marion599a16b2018-03-04 19:35:23 +0100956{
957 linux_pci_device_t *p = linux_pci_get_device (h);
958 int fds[count];
959 int i;
960
961 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
962 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
963 "support");
964
965 for (i = start; i < start + count; i++)
966 {
967 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
968 fds[i] = irq->fd;
969 }
970
Damjan Marion23227982018-10-22 13:38:57 +0200971 return vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
Damjan Marion599a16b2018-03-04 19:35:23 +0100972 VFIO_IRQ_SET_ACTION_TRIGGER, fds);
973}
974
Damjan Marion9c9490c2020-10-07 20:00:39 +0200975uword
976vlib_pci_get_msix_file_index (vlib_main_t * vm, vlib_pci_dev_handle_t h,
977 u16 index)
978{
979 linux_pci_device_t *p = linux_pci_get_device (h);
980 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, index);
981 if (irq->fd == -1)
982 return ~0;
983 return irq->clib_file_index;
984}
985
Damjan Marion599a16b2018-03-04 19:35:23 +0100986clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200987vlib_pci_disable_msix_irq (vlib_main_t * vm, vlib_pci_dev_handle_t h,
988 u16 start, u16 count)
Damjan Marion599a16b2018-03-04 19:35:23 +0100989{
990 linux_pci_device_t *p = linux_pci_get_device (h);
991 int i, fds[count];
992
993 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
994 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
995 "support");
996
997 for (i = start; i < start + count; i++)
998 fds[i] = -1;
999
Damjan Marion23227982018-10-22 13:38:57 +02001000 return vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
Damjan Marion599a16b2018-03-04 19:35:23 +01001001 VFIO_IRQ_SET_ACTION_TRIGGER, fds);
1002}
1003
Damjan Marioncef87f12017-10-05 15:32:41 +02001004static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001005add_device_vfio (vlib_main_t * vm, linux_pci_device_t * p,
1006 vlib_pci_device_info_t * di, pci_device_registration_t * r)
Damjan Marioncef87f12017-10-05 15:32:41 +02001007{
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001008 linux_pci_main_t *lpm = &linux_pci_main;
Damjan Marionf9a968e2018-02-25 23:26:22 +01001009 struct vfio_device_info device_info = { 0 };
Damjan Marionf313b742018-03-05 14:08:33 +01001010 struct vfio_region_info reg = { 0 };
Damjan Marioncef87f12017-10-05 15:32:41 +02001011 clib_error_t *err = 0;
1012 u8 *s = 0;
Damjan Marion23227982018-10-22 13:38:57 +02001013 int is_noiommu;
Damjan Marioncef87f12017-10-05 15:32:41 +02001014
Damjan Marion2060db82018-03-04 17:37:15 +01001015 p->type = LINUX_PCI_DEVICE_TYPE_VFIO;
Damjan Marioncef87f12017-10-05 15:32:41 +02001016
Damjan Marion23227982018-10-22 13:38:57 +02001017 if ((err = linux_vfio_group_get_device_fd (&p->addr, &p->fd, &is_noiommu)))
Damjan Marioncef87f12017-10-05 15:32:41 +02001018 return err;
1019
Damjan Marion23227982018-10-22 13:38:57 +02001020 if (is_noiommu == 0)
1021 p->supports_va_dma = 1;
1022
Damjan Marioncef87f12017-10-05 15:32:41 +02001023 device_info.argsz = sizeof (device_info);
Damjan Marionf9a968e2018-02-25 23:26:22 +01001024 if (ioctl (p->fd, VFIO_DEVICE_GET_INFO, &device_info) < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +02001025 {
1026 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
1027 format_vlib_pci_addr, &di->addr);
1028 goto error;
1029 }
1030
Damjan Marionf313b742018-03-05 14:08:33 +01001031 reg.argsz = sizeof (struct vfio_region_info);
1032 reg.index = VFIO_PCI_CONFIG_REGION_INDEX;
1033 if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, &reg) < 0)
1034 {
1035 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
1036 format_vlib_pci_addr, &di->addr);
1037 goto error;
1038 }
Damjan Marion23227982018-10-22 13:38:57 +02001039
Damjan Marion0792bb42020-05-25 19:31:42 +02001040 pci_log_debug (vm, p, "%s %U", __func__, format_vfio_region_info, &reg);
Damjan Marion23227982018-10-22 13:38:57 +02001041
Damjan Marionf313b742018-03-05 14:08:33 +01001042 p->config_offset = reg.offset;
1043 p->config_fd = p->fd;
1044
Damjan Marioncef87f12017-10-05 15:32:41 +02001045 /* reset if device supports it */
1046 if (device_info.flags & VFIO_DEVICE_FLAGS_RESET)
Damjan Marionf9a968e2018-02-25 23:26:22 +01001047 if (ioctl (p->fd, VFIO_DEVICE_RESET) < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +02001048 {
1049 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_RESET) '%U'",
1050 format_vlib_pci_addr, &di->addr);
1051 goto error;
1052 }
1053
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001054 if (r && r->interrupt_handler)
Damjan Marioncef87f12017-10-05 15:32:41 +02001055 {
Damjan Marion23227982018-10-22 13:38:57 +02001056 vlib_pci_register_intx_handler (vm, p->handle, r->interrupt_handler);
1057 linux_pci_vfio_unmask_intx (vm, p);
Damjan Marioncef87f12017-10-05 15:32:41 +02001058 }
1059
Damjan Marion68b4da62018-09-30 18:26:20 +02001060 if (p->supports_va_dma)
1061 {
1062 vlib_buffer_pool_t *bp;
1063 /* *INDENT-OFF* */
Damjan Mariond50e3472019-01-20 00:03:56 +01001064 vec_foreach (bp, vm->buffer_main->buffer_pools)
Damjan Marion68b4da62018-09-30 18:26:20 +02001065 {
1066 u32 i;
1067 vlib_physmem_map_t *pm;
1068 pm = vlib_physmem_get_map (vm, bp->physmem_map_index);
1069 for (i = 0; i < pm->n_pages; i++)
1070 vfio_map_physmem_page (vm, pm->base + (i << pm->log2_page_size));
1071 }
1072 /* *INDENT-ON* */
1073 }
1074
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001075 if (r && r->init_function)
1076 err = r->init_function (lpm->vlib_main, p->handle);
Damjan Marioncef87f12017-10-05 15:32:41 +02001077
1078error:
1079 vec_free (s);
1080 if (err)
1081 {
Damjan Marionf9a968e2018-02-25 23:26:22 +01001082 if (p->fd != -1)
1083 close (p->fd);
Chris Luke30684ac2018-03-29 12:56:58 -07001084 if (p->config_fd != -1 && p->config_fd != p->fd)
Damjan Marion96504182017-12-10 23:54:46 +01001085 close (p->config_fd);
Chris Luke30684ac2018-03-29 12:56:58 -07001086 p->config_fd = p->fd = -1;
Damjan Marioncef87f12017-10-05 15:32:41 +02001087 }
1088 return err;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001089}
1090
1091/* Configuration space read/write. */
1092clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001093vlib_pci_read_write_config (vlib_main_t * vm, vlib_pci_dev_handle_t h,
Damjan Marion5a206ea2016-05-12 22:11:03 +02001094 vlib_read_or_write_t read_or_write,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001095 uword address, void *data, u32 n_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001096{
Damjan Marioncef87f12017-10-05 15:32:41 +02001097 linux_pci_device_t *p = linux_pci_get_device (h);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001098 int n;
1099
Ed Warnickecb9cada2015-12-08 15:45:58 -07001100 if (read_or_write == VLIB_READ)
Damjan Marionf9a968e2018-02-25 23:26:22 +01001101 n = pread (p->config_fd, data, n_bytes, p->config_offset + address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001102 else
Damjan Marionf9a968e2018-02-25 23:26:22 +01001103 n = pwrite (p->config_fd, data, n_bytes, p->config_offset + address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001104
1105 if (n != n_bytes)
1106 return clib_error_return_unix (0, "%s",
1107 read_or_write == VLIB_READ
1108 ? "read" : "write");
1109
1110 return 0;
1111}
1112
1113static clib_error_t *
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001114vlib_pci_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 bar, int *fd,
1115 u64 * size, u64 * offset)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001116{
Damjan Marioncef87f12017-10-05 15:32:41 +02001117 linux_pci_device_t *p = linux_pci_get_device (h);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001118 clib_error_t *error = 0;
1119 int _fd = -1;
1120 u64 _size = 0, _offset = 0;
Damjan Marion2060db82018-03-04 17:37:15 +01001121
1122 ASSERT (bar <= 5);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001123
1124 error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001125
Damjan Marion2060db82018-03-04 17:37:15 +01001126 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001127 {
Damjan Marion2060db82018-03-04 17:37:15 +01001128 u8 *file_name;
1129 struct stat stat_buf;
1130 file_name = format (0, "%s/%U/resource%d%c", sysfs_pci_dev_path,
1131 format_vlib_pci_addr, &p->addr, bar, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001132
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001133 _fd = open ((char *) file_name, O_RDWR);
1134 if (_fd < 0)
Damjan Marion2060db82018-03-04 17:37:15 +01001135 {
1136 error = clib_error_return_unix (0, "open `%s'", file_name);
1137 vec_free (file_name);
1138 return error;
1139 }
1140
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001141 if (fstat (_fd, &stat_buf) < 0)
Damjan Marion2060db82018-03-04 17:37:15 +01001142 {
1143 error = clib_error_return_unix (0, "fstat `%s'", file_name);
1144 vec_free (file_name);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001145 close (_fd);
Damjan Marion2060db82018-03-04 17:37:15 +01001146 return error;
1147 }
1148
1149 vec_free (file_name);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001150 _size = stat_buf.st_size;
1151 _offset = 0;
Damjan Marion2060db82018-03-04 17:37:15 +01001152 }
1153 else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001154 {
Damjan Marion0792bb42020-05-25 19:31:42 +02001155 struct vfio_region_info *r;
1156 u32 sz = sizeof (struct vfio_region_info);
1157 again:
1158 r = clib_mem_alloc (sz);
1159 clib_memset (r, 0, sz);
1160 r->argsz = sz;
1161 r->index = bar;
1162 if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, r) < 0)
Damjan Marion2060db82018-03-04 17:37:15 +01001163 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) "
1164 "'%U'", format_vlib_pci_addr,
1165 &p->addr);
Damjan Marion0792bb42020-05-25 19:31:42 +02001166 if (sz != r->argsz)
1167 {
1168 sz = r->argsz;
1169 clib_mem_free (r);
1170 goto again;
1171 }
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001172 _fd = p->fd;
Damjan Marion0792bb42020-05-25 19:31:42 +02001173 _size = r->size;
1174 _offset = r->offset;
1175 pci_log_debug (vm, p, "%s %U", __func__, format_vfio_region_info, r);
1176 clib_mem_free (r);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001177 }
Damjan Marion2060db82018-03-04 17:37:15 +01001178 else
1179 ASSERT (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001180
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001181 *fd = _fd;
1182 *size = _size;
1183 *offset = _offset;
1184
1185 return error;
1186}
1187
1188static clib_error_t *
1189vlib_pci_map_region_int (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1190 u32 bar, u8 * addr, void **result)
1191{
1192 linux_pci_device_t *p = linux_pci_get_device (h);
1193 int fd = -1;
1194 clib_error_t *error;
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001195 u64 size = 0, offset = 0;
Mohammed Hawari70fc36f2020-10-21 16:41:30 +02001196 u16 command;
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001197
1198 pci_log_debug (vm, p, "map region %u to va %p", bar, addr);
1199
Mohammed Hawari70fc36f2020-10-21 16:41:30 +02001200 if ((error = vlib_pci_read_config_u16 (vm, h, 4, &command)))
1201 return error;
1202
1203 if (!(command & PCI_COMMAND_MEMORY))
1204 {
1205 pci_log_debug (vm, p, "setting memory enable bit");
1206 command |= PCI_COMMAND_MEMORY;
1207 if ((error = vlib_pci_write_config_u16 (vm, h, 4, &command)))
1208 return error;
1209 }
1210
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001211 if ((error = vlib_pci_region (vm, h, bar, &fd, &size, &offset)))
1212 return error;
1213
Damjan Marion6bfd0762020-09-11 22:16:53 +02001214 *result = clib_mem_vm_map_shared (addr, size, fd, offset,
1215 "PCIe %U region %u", format_vlib_pci_addr,
1216 vlib_pci_get_addr (vm, h), bar);
1217 if (*result == CLIB_MEM_VM_MAP_FAILED)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001218 {
Damjan Marion2060db82018-03-04 17:37:15 +01001219 error = clib_error_return_unix (0, "mmap `BAR%u'", bar);
Dave Barachc72950e2020-05-04 14:47:45 -04001220 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO && (fd != -1))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001221 close (fd);
Damjan Marion2060db82018-03-04 17:37:15 +01001222 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001223 }
Damjan Marion2060db82018-03-04 17:37:15 +01001224
1225 /* *INDENT-OFF* */
1226 vec_validate_init_empty (p->regions, bar,
1227 (linux_pci_region_t) { .fd = -1});
1228 /* *INDENT-ON* */
1229 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1230 p->regions[bar].fd = fd;
1231 p->regions[bar].addr = *result;
1232 p->regions[bar].size = size;
1233 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001234}
1235
1236clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001237vlib_pci_map_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 resource,
1238 void **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001239{
Damjan Marion23227982018-10-22 13:38:57 +02001240 return (vlib_pci_map_region_int (vm, h, resource, 0 /* addr */ , result));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001241}
1242
1243clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001244vlib_pci_map_region_fixed (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1245 u32 resource, u8 * addr, void **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001246{
Damjan Marion23227982018-10-22 13:38:57 +02001247 return (vlib_pci_map_region_int (vm, h, resource, addr, result));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001248}
1249
Damjan Marionf313b742018-03-05 14:08:33 +01001250clib_error_t *
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001251vlib_pci_io_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 resource)
1252{
1253 linux_pci_device_t *p = linux_pci_get_device (h);
1254 clib_error_t *error = 0;
1255 int fd = -1;
1256 u64 size = 0, offset = 0;
1257
1258 if ((error = vlib_pci_region (vm, h, resource, &fd, &size, &offset)))
1259 return error;
1260
1261 p->io_fd = fd;
1262 p->io_offset = offset;
1263 return error;
1264}
1265
1266clib_error_t *
1267vlib_pci_read_write_io (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1268 vlib_read_or_write_t read_or_write,
1269 uword offset, void *data, u32 length)
1270{
1271 linux_pci_device_t *p = linux_pci_get_device (h);
1272 int n = 0;
1273
1274 if (read_or_write == VLIB_READ)
1275 n = pread (p->io_fd, data, length, p->io_offset + offset);
1276 else
1277 n = pwrite (p->io_fd, data, length, p->io_offset + offset);
1278
1279 if (n != length)
1280 return clib_error_return_unix (0, "%s",
1281 read_or_write == VLIB_READ
1282 ? "read" : "write");
1283 return 0;
1284}
1285
1286clib_error_t *
Damjan Marion68b4da62018-09-30 18:26:20 +02001287vlib_pci_map_dma (vlib_main_t * vm, vlib_pci_dev_handle_t h, void *ptr)
1288{
1289 linux_pci_device_t *p = linux_pci_get_device (h);
1290
1291 if (!p->supports_va_dma)
1292 return 0;
1293
1294 return vfio_map_physmem_page (vm, ptr);
1295}
1296
1297int
1298vlib_pci_supports_virtual_addr_dma (vlib_main_t * vm, vlib_pci_dev_handle_t h)
1299{
1300 linux_pci_device_t *p = linux_pci_get_device (h);
1301
1302 return p->supports_va_dma != 0;
1303}
1304
1305clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001306vlib_pci_device_open (vlib_main_t * vm, vlib_pci_addr_t * addr,
Damjan Marionf313b742018-03-05 14:08:33 +01001307 pci_device_id_t ids[], vlib_pci_dev_handle_t * handle)
1308{
1309 linux_pci_main_t *lpm = &linux_pci_main;
1310 vlib_pci_device_info_t *di;
1311 linux_pci_device_t *p;
1312 clib_error_t *err = 0;
1313 pci_device_id_t *i;
1314
Damjan Marion23227982018-10-22 13:38:57 +02001315 di = vlib_pci_get_device_info (vm, addr, &err);
Damjan Marionf313b742018-03-05 14:08:33 +01001316
1317 if (err)
1318 return err;
1319 for (i = ids; i->vendor_id != 0; i++)
1320 if (i->vendor_id == di->vendor_id && i->device_id == di->device_id)
1321 break;
1322
1323 if (i->vendor_id == 0)
1324 return clib_error_return (0, "Wrong vendor or device id");
1325
1326 pool_get (lpm->linux_pci_devices, p);
1327 p->handle = p - lpm->linux_pci_devices;
Damjan Marion23227982018-10-22 13:38:57 +02001328 p->addr.as_u32 = di->addr.as_u32;
Damjan Marionf313b742018-03-05 14:08:33 +01001329 p->intx_irq.fd = -1;
Mohsin Kazmi692f9b12019-04-02 11:09:49 +00001330 p->intx_irq.clib_file_index = -1;
Damjan Mariond2bfb782019-01-07 20:56:04 +01001331 p->numa_node = di->numa_node;
Mohsin Kazmi3d3b9552018-10-24 14:05:34 +02001332 /*
1333 * pci io bar read/write fd
1334 */
1335 p->io_fd = -1;
Damjan Marionf313b742018-03-05 14:08:33 +01001336
Damjan Marion23227982018-10-22 13:38:57 +02001337 pci_log_debug (vm, p, "open vid:0x%04x did:0x%04x driver:%s iommu_group:%d",
1338 di->vendor_id, di->device_id, di->driver_name,
1339 di->iommu_group);
1340
Steven Luongab489922019-08-21 11:35:27 -07001341 if (clib_strncmp ("vfio-pci", (char *) di->driver_name, 8) == 0)
Damjan Marion23227982018-10-22 13:38:57 +02001342 err = add_device_vfio (vm, p, di, 0);
Steven Luongab489922019-08-21 11:35:27 -07001343 else if (clib_strncmp ("uio_pci_generic", (char *) di->driver_name, 8) == 0)
Damjan Marion23227982018-10-22 13:38:57 +02001344 err = add_device_uio (vm, p, di, 0);
Damjan Marionf313b742018-03-05 14:08:33 +01001345 else
Damjan Marion23227982018-10-22 13:38:57 +02001346 err = clib_error_create ("device not bound to 'vfio-pci' or "
1347 "'uio_pci_generic' kernel module");
Damjan Marionf313b742018-03-05 14:08:33 +01001348 if (err)
1349 goto error;
1350
1351 *handle = p->handle;
1352
1353error:
1354 vlib_pci_free_device_info (di);
1355 if (err)
1356 {
Damjan Marion23227982018-10-22 13:38:57 +02001357 pci_log_err (vm, p, "%U", format_clib_error, err);
Dave Barachb7b92992018-10-17 10:38:51 -04001358 clib_memset (p, 0, sizeof (linux_pci_device_t));
Damjan Marionf313b742018-03-05 14:08:33 +01001359 pool_put (lpm->linux_pci_devices, p);
1360 }
1361
1362 return err;
1363}
1364
1365void
Damjan Marion23227982018-10-22 13:38:57 +02001366vlib_pci_device_close (vlib_main_t * vm, vlib_pci_dev_handle_t h)
Damjan Marionf313b742018-03-05 14:08:33 +01001367{
1368 linux_pci_main_t *lpm = &linux_pci_main;
1369 linux_pci_device_t *p = linux_pci_get_device (h);
1370 linux_pci_irq_t *irq;
1371 linux_pci_region_t *res;
1372 clib_error_t *err = 0;
1373
1374 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1375 {
1376 irq = &p->intx_irq;
Mohsin Kazmi692f9b12019-04-02 11:09:49 +00001377 if (irq->clib_file_index != -1)
1378 clib_file_del_by_index (&file_main, irq->clib_file_index);
Damjan Marionf313b742018-03-05 14:08:33 +01001379 close (p->config_fd);
Mohsin Kazmi3d3b9552018-10-24 14:05:34 +02001380 if (p->io_fd != -1)
1381 close (p->io_fd);
Damjan Marionf313b742018-03-05 14:08:33 +01001382 }
1383 else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
1384 {
1385 irq = &p->intx_irq;
1386 /* close INTx irqs */
1387 if (irq->fd != -1)
1388 {
Damjan Marion23227982018-10-22 13:38:57 +02001389 err = vfio_set_irqs (vm, p, VFIO_PCI_INTX_IRQ_INDEX, 0, 0,
Damjan Marionf313b742018-03-05 14:08:33 +01001390 VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1391 clib_error_free (err);
Mohsin Kazmi692f9b12019-04-02 11:09:49 +00001392 if (irq->clib_file_index != -1)
1393 clib_file_del_by_index (&file_main, irq->clib_file_index);
Damjan Marionf313b742018-03-05 14:08:33 +01001394 close (irq->fd);
1395 }
1396
1397 /* close MSI-X irqs */
1398 if (vec_len (p->msix_irqs))
1399 {
Damjan Marion23227982018-10-22 13:38:57 +02001400 err = vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, 0, 0,
Damjan Marionf313b742018-03-05 14:08:33 +01001401 VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1402 clib_error_free (err);
1403 /* *INDENT-OFF* */
1404 vec_foreach (irq, p->msix_irqs)
1405 {
1406 if (irq->fd == -1)
1407 continue;
1408 clib_file_del_by_index (&file_main, irq->clib_file_index);
1409 close (irq->fd);
1410 }
1411 /* *INDENT-ON* */
1412 vec_free (p->msix_irqs);
1413 }
1414 }
1415
1416 /* *INDENT-OFF* */
1417 vec_foreach (res, p->regions)
1418 {
1419 if (res->size == 0)
1420 continue;
Damjan Marion6bfd0762020-09-11 22:16:53 +02001421 clib_mem_vm_unmap (res->addr);
Damjan Marionf313b742018-03-05 14:08:33 +01001422 if (res->fd != -1)
1423 close (res->fd);
1424 }
1425 /* *INDENT-ON* */
1426 vec_free (p->regions);
1427
1428 close (p->fd);
Dave Barachb7b92992018-10-17 10:38:51 -04001429 clib_memset (p, 0, sizeof (linux_pci_device_t));
Damjan Marionf313b742018-03-05 14:08:33 +01001430 pool_put (lpm->linux_pci_devices, p);
1431}
1432
Dave Barach9b8ffd92016-07-08 08:13:45 -04001433void
Damjan Marion23227982018-10-22 13:38:57 +02001434init_device_from_registered (vlib_main_t * vm, vlib_pci_device_info_t * di)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001435{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001436 vlib_pci_main_t *pm = &pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +02001437 linux_pci_main_t *lpm = &linux_pci_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001438 pci_device_registration_t *r;
1439 pci_device_id_t *i;
Damjan Marioncef87f12017-10-05 15:32:41 +02001440 clib_error_t *err = 0;
1441 linux_pci_device_t *p;
1442
1443 pool_get (lpm->linux_pci_devices, p);
1444 p->handle = p - lpm->linux_pci_devices;
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001445 p->intx_irq.fd = -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001446
Damjan Marion5a206ea2016-05-12 22:11:03 +02001447 r = pm->pci_device_registrations;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001448
1449 while (r)
1450 {
1451 for (i = r->supported_devices; i->vendor_id != 0; i++)
Damjan Marion3a593822018-02-17 14:06:53 +01001452 if (i->vendor_id == di->vendor_id && i->device_id == di->device_id)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001453 {
Damjan Marioncef87f12017-10-05 15:32:41 +02001454 if (di->iommu_group != -1)
Damjan Marion23227982018-10-22 13:38:57 +02001455 err = add_device_vfio (vm, p, di, r);
Damjan Marioncef87f12017-10-05 15:32:41 +02001456 else
Damjan Marion23227982018-10-22 13:38:57 +02001457 err = add_device_uio (vm, p, di, r);
Damjan Marion5a206ea2016-05-12 22:11:03 +02001458
Damjan Marioncef87f12017-10-05 15:32:41 +02001459 if (err)
1460 clib_error_report (err);
1461 else
1462 return;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001463 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001464 r = r->next_registration;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001465 }
Damjan Marioncef87f12017-10-05 15:32:41 +02001466
Ed Warnickecb9cada2015-12-08 15:45:58 -07001467 /* No driver, close the PCI config-space FD */
Dave Barachb7b92992018-10-17 10:38:51 -04001468 clib_memset (p, 0, sizeof (linux_pci_device_t));
Damjan Marioncef87f12017-10-05 15:32:41 +02001469 pool_put (lpm->linux_pci_devices, p);
1470}
1471
1472static clib_error_t *
1473scan_pci_addr (void *arg, u8 * dev_dir_name, u8 * ignored)
1474{
1475 vlib_pci_addr_t addr, **addrv = arg;
1476 unformat_input_t input;
1477 clib_error_t *err = 0;
1478
1479 unformat_init_string (&input, (char *) dev_dir_name,
1480 vec_len (dev_dir_name));
1481
1482 if (!unformat (&input, "/sys/bus/pci/devices/%U",
1483 unformat_vlib_pci_addr, &addr))
1484 err = clib_error_return (0, "unformat error `%v`", dev_dir_name);
1485
1486 unformat_free (&input);
1487
1488 if (err)
1489 return err;
1490
1491 vec_add1 (*addrv, addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001492 return 0;
1493}
1494
Damjan Marioncef87f12017-10-05 15:32:41 +02001495static int
1496pci_addr_cmp (void *v1, void *v2)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001497{
Damjan Marioncef87f12017-10-05 15:32:41 +02001498 vlib_pci_addr_t *a1 = v1;
1499 vlib_pci_addr_t *a2 = v2;
1500
1501 if (a1->domain > a2->domain)
1502 return 1;
1503 if (a1->domain < a2->domain)
1504 return -1;
1505 if (a1->bus > a2->bus)
1506 return 1;
1507 if (a1->bus < a2->bus)
1508 return -1;
1509 if (a1->slot > a2->slot)
1510 return 1;
1511 if (a1->slot < a2->slot)
1512 return -1;
1513 if (a1->function > a2->function)
1514 return 1;
1515 if (a1->function < a2->function)
1516 return -1;
1517 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001518}
1519
Damjan Marioncef87f12017-10-05 15:32:41 +02001520vlib_pci_addr_t *
1521vlib_pci_get_all_dev_addrs ()
Ed Warnickecb9cada2015-12-08 15:45:58 -07001522{
Damjan Marioncef87f12017-10-05 15:32:41 +02001523 vlib_pci_addr_t *addrs = 0;
1524 clib_error_t *err;
1525 err = foreach_directory_file ((char *) sysfs_pci_dev_path, scan_pci_addr,
1526 &addrs, /* scan_dirs */ 0);
1527 if (err)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001528 {
Damjan Marioncef87f12017-10-05 15:32:41 +02001529 vec_free (addrs);
1530 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001531 }
1532
Damjan Marioncef87f12017-10-05 15:32:41 +02001533 vec_sort_with_function (addrs, pci_addr_cmp);
Damjan Mariona42cd342016-04-13 18:03:20 +02001534
Damjan Marioncef87f12017-10-05 15:32:41 +02001535 return addrs;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001536}
1537
Dave Barach9b8ffd92016-07-08 08:13:45 -04001538clib_error_t *
1539linux_pci_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001540{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001541 vlib_pci_main_t *pm = &pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +02001542 vlib_pci_addr_t *addr = 0, *addrs;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001543
1544 pm->vlib_main = vm;
1545
Dave Barach9b8ffd92016-07-08 08:13:45 -04001546 ASSERT (sizeof (vlib_pci_addr_t) == sizeof (u32));
Damjan Mariona42cd342016-04-13 18:03:20 +02001547
Damjan Marioncef87f12017-10-05 15:32:41 +02001548 addrs = vlib_pci_get_all_dev_addrs ();
1549 /* *INDENT-OFF* */
1550 vec_foreach (addr, addrs)
1551 {
1552 vlib_pci_device_info_t *d;
Damjan Marion23227982018-10-22 13:38:57 +02001553 if ((d = vlib_pci_get_device_info (vm, addr, 0)))
Damjan Marioncef87f12017-10-05 15:32:41 +02001554 {
Damjan Marion23227982018-10-22 13:38:57 +02001555 init_device_from_registered (vm, d);
Damjan Marioncef87f12017-10-05 15:32:41 +02001556 vlib_pci_free_device_info (d);
1557 }
1558 }
1559 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001560
Dave Barachf8d50682019-05-14 18:01:44 -04001561 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001562}
1563
Dave Barachf8d50682019-05-14 18:01:44 -04001564/* *INDENT-OFF* */
1565VLIB_INIT_FUNCTION (linux_pci_init) =
1566{
1567 .runs_after = VLIB_INITS("unix_input_init"),
1568};
1569/* *INDENT-ON* */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001570
1571/*
1572 * fd.io coding-style-patch-verification: ON
1573 *
1574 * Local Variables:
1575 * eval: (c-set-style "gnu")
1576 * End:
1577 */