blob: e94fee1bcbd06ce8aace7915b13c21ca3ac61cf6 [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 Marion00ea98a2023-07-28 13:19:49 +020064VLIB_REGISTER_LOG_CLASS (pci_log, static) = {
65 .class_name = "pci",
66 .subclass_name = "linux",
67};
68
69#define log_debug(p, f, ...) \
70 vlib_log (VLIB_LOG_LEVEL_DEBUG, pci_log.class, "%U: " f, \
71 format_vlib_pci_log, p->handle, ##__VA_ARGS__)
72#define log_err(p, f, ...) \
73 vlib_log (VLIB_LOG_LEVEL_ERR, pci_log.class, "%U: " f, format_vlib_pci_log, \
74 p->handle, ##__VA_ARGS__)
Damjan Marion23227982018-10-22 13:38:57 +020075
Dave Barach9b8ffd92016-07-08 08:13:45 -040076typedef struct
77{
Damjan Marion2060db82018-03-04 17:37:15 +010078 int fd;
79 void *addr;
80 size_t size;
81} linux_pci_region_t;
82
Damjan Marion599a16b2018-03-04 19:35:23 +010083typedef struct
84{
85 int fd;
86 u32 clib_file_index;
87 union
88 {
Damjan Mariond5ded2d2018-03-05 10:18:50 +010089 pci_intx_handler_function_t *intx_handler;
Damjan Marion599a16b2018-03-04 19:35:23 +010090 pci_msix_handler_function_t *msix_handler;
91 };
92} linux_pci_irq_t;
Damjan Marion2060db82018-03-04 17:37:15 +010093
94typedef enum
95{
96 LINUX_PCI_DEVICE_TYPE_UNKNOWN,
97 LINUX_PCI_DEVICE_TYPE_UIO,
98 LINUX_PCI_DEVICE_TYPE_VFIO,
99} linux_pci_device_type_t;
100
101typedef struct
102{
103 linux_pci_device_type_t type;
Damjan Marioncef87f12017-10-05 15:32:41 +0200104 vlib_pci_dev_handle_t handle;
105 vlib_pci_addr_t addr;
Damjan Mariond2bfb782019-01-07 20:56:04 +0100106 u32 numa_node;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200107
108 /* Resource file descriptors. */
Damjan Marion2060db82018-03-04 17:37:15 +0100109 linux_pci_region_t *regions;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200110
111 /* File descriptor for config space read/write. */
112 int config_fd;
Damjan Marionf9a968e2018-02-25 23:26:22 +0100113 u64 config_offset;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200114
Damjan Marionf9a968e2018-02-25 23:26:22 +0100115 /* Device File descriptor */
116 int fd;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200117
Mohsin Kazmi95276ca2018-09-28 11:45:01 +0200118 /* read/write file descriptor for io bar */
119 int io_fd;
120 u64 io_offset;
121
Damjan Marion5a206ea2016-05-12 22:11:03 +0200122 /* Minor device for uio device. */
123 u32 uio_minor;
124
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100125 /* Interrupt handlers */
126 linux_pci_irq_t intx_irq;
Damjan Marion599a16b2018-03-04 19:35:23 +0100127 linux_pci_irq_t *msix_irqs;
Damjan Marioncef87f12017-10-05 15:32:41 +0200128
129 /* private data */
130 uword private_data;
131
Damjan Marion23227982018-10-22 13:38:57 +0200132 u8 supports_va_dma;
133
Damjan Marion5a206ea2016-05-12 22:11:03 +0200134} linux_pci_device_t;
135
136/* Pool of PCI devices. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400137typedef struct
138{
139 vlib_main_t *vlib_main;
140 linux_pci_device_t *linux_pci_devices;
Damjan Marioncef87f12017-10-05 15:32:41 +0200141
Damjan Marion5a206ea2016-05-12 22:11:03 +0200142} linux_pci_main_t;
143
144extern linux_pci_main_t linux_pci_main;
145
Damjan Marion599a16b2018-03-04 19:35:23 +0100146static linux_pci_device_t *
Damjan Marioncef87f12017-10-05 15:32:41 +0200147linux_pci_get_device (vlib_pci_dev_handle_t h)
148{
149 linux_pci_main_t *lpm = &linux_pci_main;
150 return pool_elt_at_index (lpm->linux_pci_devices, h);
151}
152
153uword
Damjan Marion23227982018-10-22 13:38:57 +0200154vlib_pci_get_private_data (vlib_main_t * vm, vlib_pci_dev_handle_t h)
Damjan Marioncef87f12017-10-05 15:32:41 +0200155{
156 linux_pci_device_t *d = linux_pci_get_device (h);
157 return d->private_data;
158}
159
160void
Damjan Marion23227982018-10-22 13:38:57 +0200161vlib_pci_set_private_data (vlib_main_t * vm, vlib_pci_dev_handle_t h,
162 uword private_data)
Damjan Marioncef87f12017-10-05 15:32:41 +0200163{
164 linux_pci_device_t *d = linux_pci_get_device (h);
165 d->private_data = private_data;
166}
167
168vlib_pci_addr_t *
Damjan Marion23227982018-10-22 13:38:57 +0200169vlib_pci_get_addr (vlib_main_t * vm, vlib_pci_dev_handle_t h)
Damjan Marioncef87f12017-10-05 15:32:41 +0200170{
Damjan Marion599a16b2018-03-04 19:35:23 +0100171 linux_pci_device_t *d = linux_pci_get_device (h);
172 return &d->addr;
Damjan Marioncef87f12017-10-05 15:32:41 +0200173}
174
Damjan Mariond2bfb782019-01-07 20:56:04 +0100175u32
176vlib_pci_get_numa_node (vlib_main_t * vm, vlib_pci_dev_handle_t h)
177{
178 linux_pci_device_t *d = linux_pci_get_device (h);
179 return d->numa_node;
180}
181
Mohsin Kazmia402b832019-01-25 15:57:24 +0000182u32
183vlib_pci_get_num_msix_interrupts (vlib_main_t * vm, vlib_pci_dev_handle_t h)
184{
185 linux_pci_device_t *d = linux_pci_get_device (h);
186
187 if (d->type == LINUX_PCI_DEVICE_TYPE_VFIO)
188 {
189 struct vfio_irq_info ii = { 0 };
190
191 ii.argsz = sizeof (struct vfio_irq_info);
192 ii.index = VFIO_PCI_MSIX_IRQ_INDEX;
193 if (ioctl (d->fd, VFIO_DEVICE_GET_IRQ_INFO, &ii) < 0)
194 return 0;
195 return ii.count;
196 }
197 return 0;
198}
199
Damjan Marion5a206ea2016-05-12 22:11:03 +0200200/* Call to allocate/initialize the pci subsystem.
201 This is not an init function so that users can explicitly enable
202 pci only when it's needed. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400203clib_error_t *pci_bus_init (vlib_main_t * vm);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200204
Ed Warnickecb9cada2015-12-08 15:45:58 -0700205linux_pci_main_t linux_pci_main;
206
Damjan Marioncef87f12017-10-05 15:32:41 +0200207vlib_pci_device_info_t *
Damjan Marion23227982018-10-22 13:38:57 +0200208vlib_pci_get_device_info (vlib_main_t * vm, vlib_pci_addr_t * addr,
209 clib_error_t ** error)
Damjan Marioncef87f12017-10-05 15:32:41 +0200210{
Damjan Marioncef87f12017-10-05 15:32:41 +0200211 clib_error_t *err;
212 vlib_pci_device_info_t *di;
213 u8 *f = 0;
214 u32 tmp;
215 int fd;
Yulong Pei45495482019-10-17 18:41:52 +0800216 u8 *tmpstr;
Jieqiang Wang76c61592020-01-13 17:15:13 +0800217 clib_bitmap_t *bmp = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200218
219 di = clib_mem_alloc (sizeof (vlib_pci_device_info_t));
Dave Barachb7b92992018-10-17 10:38:51 -0400220 clib_memset (di, 0, sizeof (vlib_pci_device_info_t));
Damjan Marioncef87f12017-10-05 15:32:41 +0200221 di->addr.as_u32 = addr->as_u32;
222
223 u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
224 format_vlib_pci_addr, addr);
225
226 f = format (0, "%v/config%c", dev_dir_name, 0);
227 fd = open ((char *) f, O_RDWR);
228
229 /* Try read-only access if write fails. */
230 if (fd < 0)
231 fd = open ((char *) f, O_RDONLY);
232
233 if (fd < 0)
234 {
235 err = clib_error_return_unix (0, "open `%s'", f);
236 goto error;
237 }
238
239 /* You can only read more that 64 bytes of config space as root; so we try to
240 read the full space but fall back to just the first 64 bytes. */
Damjan Marion00ea98a2023-07-28 13:19:49 +0200241 if (read (fd, &di->config, sizeof (di->config)) < sizeof (di->config))
Damjan Marioncef87f12017-10-05 15:32:41 +0200242 {
243 err = clib_error_return_unix (0, "read `%s'", f);
244 close (fd);
245 goto error;
246 }
247
Damjan Marioncef87f12017-10-05 15:32:41 +0200248 di->numa_node = -1;
249 vec_reset_length (f);
250 f = format (f, "%v/numa_node%c", dev_dir_name, 0);
Jieqiang Wang76c61592020-01-13 17:15:13 +0800251 err = clib_sysfs_read ((char *) f, "%d", &di->numa_node);
Damjan Marioncef87f12017-10-05 15:32:41 +0200252 if (err)
253 {
254 di->numa_node = -1;
255 clib_error_free (err);
256 }
Jieqiang Wang76c61592020-01-13 17:15:13 +0800257 if (di->numa_node == -1)
258 {
259 /* if '/sys/bus/pci/devices/<device id>/numa_node' returns -1 and
260 it is a SMP system, set numa_node to 0. */
261 if ((err = clib_sysfs_read ("/sys/devices/system/node/online", "%U",
262 unformat_bitmap_list, &bmp)))
263 clib_error_free (err);
264 if (clib_bitmap_count_set_bits (bmp) == 1)
265 di->numa_node = 0;
266 }
Damjan Marioncef87f12017-10-05 15:32:41 +0200267
268 vec_reset_length (f);
269 f = format (f, "%v/class%c", dev_dir_name, 0);
270 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
271 if (err)
272 goto error;
273 di->device_class = tmp >> 8;
274
275 vec_reset_length (f);
276 f = format (f, "%v/vendor%c", dev_dir_name, 0);
277 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
278 if (err)
279 goto error;
280 di->vendor_id = tmp;
281
282 vec_reset_length (f);
283 f = format (f, "%v/device%c", dev_dir_name, 0);
284 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
285 if (err)
286 goto error;
287 di->device_id = tmp;
288
289 vec_reset_length (f);
Damjan Marion00ea98a2023-07-28 13:19:49 +0200290 f = format (f, "%v/revision%c", dev_dir_name, 0);
291 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
292 if (err)
293 goto error;
294 di->revision = tmp;
295
296 vec_reset_length (f);
Damjan Marioncef87f12017-10-05 15:32:41 +0200297 f = format (f, "%v/driver%c", dev_dir_name, 0);
298 di->driver_name = clib_sysfs_link_to_name ((char *) f);
Benoît Ganne0eae2bb2019-10-04 17:30:21 +0200299 if (!di->driver_name)
300 di->driver_name = format (0, "<NONE>%c", 0);
Damjan Marioncef87f12017-10-05 15:32:41 +0200301
302 di->iommu_group = -1;
Yulong Pei45495482019-10-17 18:41:52 +0800303 vec_reset_length (f);
304 f = format (f, "%v/iommu_group%c", dev_dir_name, 0);
305 tmpstr = clib_sysfs_link_to_name ((char *) f);
306 if (tmpstr)
Damjan Marioncef87f12017-10-05 15:32:41 +0200307 {
Yulong Pei45495482019-10-17 18:41:52 +0800308 di->iommu_group = atoi ((char *) tmpstr);
309 vec_free (tmpstr);
Damjan Marioncef87f12017-10-05 15:32:41 +0200310 }
311
Yulong Pei45495482019-10-17 18:41:52 +0800312 vec_reset_length (f);
313 f = format (f, "%v/iommu_group/name%c", dev_dir_name, 0);
314 err = clib_sysfs_read ((char *) f, "%s", &tmpstr);
315 if (err == 0)
316 {
317 if (strncmp ((char *) tmpstr, "vfio-noiommu", 12) == 0)
318 di->flags |= VLIB_PCI_DEVICE_INFO_F_NOIOMMU;
319 vec_free (tmpstr);
320 }
321 else
322 clib_error_free (err);
323
Damjan Marion96504182017-12-10 23:54:46 +0100324 close (fd);
325
Damjan Marioncef87f12017-10-05 15:32:41 +0200326 vec_reset_length (f);
327 f = format (f, "%v/vpd%c", dev_dir_name, 0);
328 fd = open ((char *) f, O_RDONLY);
329 if (fd >= 0)
330 {
331 while (1)
332 {
333 u8 tag[3];
334 u8 *data = 0;
Damjan Marion96504182017-12-10 23:54:46 +0100335 uword len;
Damjan Marioncef87f12017-10-05 15:32:41 +0200336
337 if (read (fd, &tag, 3) != 3)
338 break;
339
340 if (tag[0] != 0x82 && tag[0] != 0x90 && tag[0] != 0x91)
341 break;
342
343 len = (tag[2] << 8) | tag[1];
Ray Kinsella03d0fbe2021-11-03 09:13:41 +0000344 vec_validate (data, len - 1);
Damjan Marioncef87f12017-10-05 15:32:41 +0200345
346 if (read (fd, data, len) != len)
347 {
348 vec_free (data);
349 break;
350 }
351 if (tag[0] == 0x82)
352 di->product_name = data;
353 else if (tag[0] == 0x90)
354 di->vpd_r = data;
355 else if (tag[0] == 0x91)
356 di->vpd_w = data;
357
358 data = 0;
359 }
360 close (fd);
361 }
362
363 goto done;
364
365error:
366 vlib_pci_free_device_info (di);
367 di = 0;
368
369done:
Jieqiang Wang76c61592020-01-13 17:15:13 +0800370 vec_free (bmp);
Damjan Marioncef87f12017-10-05 15:32:41 +0200371 vec_free (f);
372 vec_free (dev_dir_name);
373 if (error)
374 *error = err;
375 else
376 clib_error_free (err);
377 return di;
378}
379
Ray Kinsella5714a492021-11-02 13:33:44 +0000380clib_error_t *__attribute__ ((weak))
381vlib_pci_get_device_root_bus (vlib_pci_addr_t *addr, vlib_pci_addr_t *root_bus)
382{
383 u8 *rel_path = 0, *abs_path = 0, *link_path = 0;
384 unformat_input_t input;
Ray Kinsella81865bc2021-11-08 07:22:49 +0000385 int fd = open (sysfs_pci_dev_path, O_RDONLY);
Klement Sekerabd469072021-11-18 12:32:05 +0100386 ssize_t size = 0;
Ray Kinsella5714a492021-11-02 13:33:44 +0000387 u32 domain = 0, bus;
388 clib_error_t *err = NULL;
389
390 if (fd < 0)
391 return clib_error_return_unix (0, "failed to open %s", sysfs_pci_dev_path);
392
393 vec_alloc (rel_path, PATH_MAX);
394 vec_alloc (abs_path, PATH_MAX);
395
396 link_path =
397 format (0, "%s/%U", sysfs_pci_dev_path, format_vlib_pci_addr, addr);
398 size = readlinkat (fd, (char *) link_path, (char *) rel_path, PATH_MAX);
399 if (size < 0)
400 {
401 err = clib_error_return_unix (0, "failed to read %s", rel_path);
402 goto done;
403 }
404
405 rel_path[size] = '\0';
406 vec_free (link_path);
407
408 link_path = format (0, "%s/%s", sysfs_pci_dev_path, rel_path);
409 if (!realpath ((char *) link_path, (char *) abs_path))
410 {
411 err = clib_error_return_unix (0, "failed to resolve %s", link_path);
412 goto done;
413 }
414
415 unformat_init_string (&input, (char *) abs_path,
416 clib_strnlen ((char *) abs_path, PATH_MAX));
417
418 if (!unformat (&input, SYSFS_DEVICES_PCI "%x:%x/%s", &domain, &bus,
419 link_path))
420 {
421 err = clib_error_return (0, "unknown input '%U'", format_unformat_error,
422 input);
423 goto done;
424 }
425
426 root_bus->domain = domain;
427 root_bus->bus = bus;
428
429done:
430 vec_free (abs_path);
431 vec_free (link_path);
432 vec_free (rel_path);
433 close (fd);
434
435 return err;
436}
437
Damjan Marion2752b892017-12-11 15:55:56 +0100438static int
439directory_exists (char *path)
440{
441 struct stat s = { 0 };
442 if (stat (path, &s) == -1)
443 return 0;
444
445 return S_ISDIR (s.st_mode);
446}
447
Damjan Mariona42cd342016-04-13 18:03:20 +0200448clib_error_t *
Benoît Ganne6a073482022-10-13 17:22:26 +0200449vlib_pci_bind_to_uio (vlib_main_t *vm, vlib_pci_addr_t *addr,
450 char *uio_drv_name, int force)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700451{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400452 clib_error_t *error = 0;
Damjan Mariona7f74572017-05-23 18:32:21 +0200453 u8 *s = 0, *driver_name = 0;
Damjan Mariona42cd342016-04-13 18:03:20 +0200454 DIR *dir = 0;
455 struct dirent *e;
Damjan Marioncef87f12017-10-05 15:32:41 +0200456 vlib_pci_device_info_t *di;
Damjan Mariona7f74572017-05-23 18:32:21 +0200457 int fd, clear_driver_override = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200458 u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
459 format_vlib_pci_addr, addr);
460
Damjan Marion23227982018-10-22 13:38:57 +0200461 di = vlib_pci_get_device_info (vm, addr, &error);
Damjan Marioncef87f12017-10-05 15:32:41 +0200462
463 if (error)
464 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700465
Damjan Marion2752b892017-12-11 15:55:56 +0100466 if (strncmp ("auto", uio_drv_name, 5) == 0)
467 {
468 int vfio_pci_loaded = 0;
469
470 if (directory_exists ("/sys/module/vfio_pci"))
471 vfio_pci_loaded = 1;
472
473 if (di->iommu_group != -1)
474 {
475 /* device is bound to IOMMU group */
476 if (!vfio_pci_loaded)
477 {
478 error = clib_error_return (0, "Skipping PCI device %U: device "
479 "is bound to IOMMU group and "
480 "vfio-pci driver is not loaded",
481 format_vlib_pci_addr, addr);
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100482 goto err0;
Damjan Marion2752b892017-12-11 15:55:56 +0100483 }
484 else
485 uio_drv_name = "vfio-pci";
486 }
487 else
488 {
489 /* device is not bound to IOMMU group so we have multiple options */
490 if (vfio_pci_loaded &&
491 (error = clib_sysfs_write (sysfs_mod_vfio_noiommu, "Y")) == 0)
492 uio_drv_name = "vfio-pci";
493 else if (directory_exists ("/sys/module/uio_pci_generic"))
494 uio_drv_name = "uio_pci_generic";
495 else if (directory_exists ("/sys/module/igb_uio"))
496 uio_drv_name = "igb_uio";
497 else
498 {
499 clib_error_free (error);
500 error = clib_error_return (0, "Skipping PCI device %U: missing "
501 "kernel VFIO or UIO driver",
502 format_vlib_pci_addr, addr);
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100503 goto err0;
Damjan Marion2752b892017-12-11 15:55:56 +0100504 }
505 clib_error_free (error);
506 }
507 }
508
Damjan Mariona7f74572017-05-23 18:32:21 +0200509 s = format (s, "%v/driver%c", dev_dir_name, 0);
Damjan Marion01914ce2017-09-14 19:04:50 +0200510 driver_name = clib_sysfs_link_to_name ((char *) s);
Damjan Mariona42cd342016-04-13 18:03:20 +0200511 vec_reset_length (s);
512
Damjan Mariona7f74572017-05-23 18:32:21 +0200513 if (driver_name &&
514 ((strcmp ("vfio-pci", (char *) driver_name) == 0) ||
515 (strcmp ("uio_pci_generic", (char *) driver_name) == 0) ||
516 (strcmp ("igb_uio", (char *) driver_name) == 0)))
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100517 goto err0;
Damjan Marion3c785e02017-05-08 18:37:54 +0200518
Benoît Ganne6a073482022-10-13 17:22:26 +0200519 if (!force)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700520 {
Benoît Ganne6a073482022-10-13 17:22:26 +0200521 /* walk trough all linux interfaces and if interface belonging to
522 this device is found check if interface is admin up */
523 dir = opendir ("/sys/class/net");
524 s = format (s, "%U%c", format_vlib_pci_addr, addr, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700525
Benoît Ganne6a073482022-10-13 17:22:26 +0200526 if (!dir)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400527 {
Benoît Ganne6a073482022-10-13 17:22:26 +0200528 error = clib_error_return (0,
529 "Skipping PCI device %U: failed to "
530 "read /sys/class/net",
531 format_vlib_pci_addr, addr);
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100532 goto err0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400533 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200534
Benoît Ganne6a073482022-10-13 17:22:26 +0200535 fd = socket (PF_INET, SOCK_DGRAM, 0);
536 if (fd < 0)
Damjan Mariona42cd342016-04-13 18:03:20 +0200537 {
Benoît Ganne6a073482022-10-13 17:22:26 +0200538 error = clib_error_return_unix (0, "socket");
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100539 goto err1;
Damjan Mariona42cd342016-04-13 18:03:20 +0200540 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700541
Benoît Ganne6a073482022-10-13 17:22:26 +0200542 while ((e = readdir (dir)))
543 {
544 struct ifreq ifr;
545 struct ethtool_drvinfo drvinfo;
546
547 if (e->d_name[0] == '.') /* skip . and .. */
548 continue;
549
550 clib_memset (&ifr, 0, sizeof ifr);
551 clib_memset (&drvinfo, 0, sizeof drvinfo);
552 ifr.ifr_data = (char *) &drvinfo;
553 clib_strncpy (ifr.ifr_name, e->d_name, sizeof (ifr.ifr_name) - 1);
554
555 drvinfo.cmd = ETHTOOL_GDRVINFO;
556 if (ioctl (fd, SIOCETHTOOL, &ifr) < 0)
557 {
558 /* Some interfaces (eg "lo") don't support this ioctl */
559 if ((errno != ENOTSUP) && (errno != ENODEV))
560 clib_unix_warning ("ioctl fetch intf %s bus info error",
561 e->d_name);
562 continue;
563 }
564
565 if (strcmp ((char *) s, drvinfo.bus_info))
566 continue;
567
568 clib_memset (&ifr, 0, sizeof (ifr));
569 clib_strncpy (ifr.ifr_name, e->d_name, sizeof (ifr.ifr_name) - 1);
570
571 if (ioctl (fd, SIOCGIFFLAGS, &ifr) < 0)
572 {
573 error = clib_error_return_unix (0, "ioctl fetch intf %s flags",
574 e->d_name);
575 close (fd);
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100576 goto err1;
Benoît Ganne6a073482022-10-13 17:22:26 +0200577 }
578
579 if (ifr.ifr_flags & IFF_UP)
580 {
581 vlib_log (VLIB_LOG_LEVEL_WARNING, pci_main.log_default,
582 "Skipping PCI device %U as host "
583 "interface %s is up",
584 format_vlib_pci_addr, addr, e->d_name);
585 close (fd);
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100586 goto err1;
Benoît Ganne6a073482022-10-13 17:22:26 +0200587 }
588 }
589
590 close (fd);
591 vec_reset_length (s);
592 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200593
594 s = format (s, "%v/driver/unbind%c", dev_dir_name, 0);
Damjan Marioncef87f12017-10-05 15:32:41 +0200595 clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
Damjan Mariona42cd342016-04-13 18:03:20 +0200596 vec_reset_length (s);
597
Damjan Mariona7f74572017-05-23 18:32:21 +0200598 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
599 if (access ((char *) s, F_OK) == 0)
600 {
Damjan Marioncef87f12017-10-05 15:32:41 +0200601 clib_sysfs_write ((char *) s, "%s", uio_drv_name);
Damjan Mariona7f74572017-05-23 18:32:21 +0200602 clear_driver_override = 1;
603 }
604 else
605 {
606 vec_reset_length (s);
Damjan Marioncef87f12017-10-05 15:32:41 +0200607 s = format (s, "%s/%s/new_id%c", sysfs_pci_drv_path, uio_drv_name, 0);
608 clib_sysfs_write ((char *) s, "0x%04x 0x%04x", di->vendor_id,
609 di->device_id);
Damjan Mariona7f74572017-05-23 18:32:21 +0200610 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200611 vec_reset_length (s);
612
Damjan Marioncef87f12017-10-05 15:32:41 +0200613 s = format (s, "%s/%s/bind%c", sysfs_pci_drv_path, uio_drv_name, 0);
614 clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
Damjan Mariona7f74572017-05-23 18:32:21 +0200615 vec_reset_length (s);
616
617 if (clear_driver_override)
618 {
619 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
Damjan Marion01914ce2017-09-14 19:04:50 +0200620 clib_sysfs_write ((char *) s, "%c", 0);
Damjan Mariona7f74572017-05-23 18:32:21 +0200621 vec_reset_length (s);
622 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200623
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100624err1:
Damjan Mariona42cd342016-04-13 18:03:20 +0200625 closedir (dir);
Benoît Gannecc16e7b2022-12-19 18:23:03 +0100626err0:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700627 vec_free (s);
Damjan Mariona42cd342016-04-13 18:03:20 +0200628 vec_free (dev_dir_name);
Damjan Mariona7f74572017-05-23 18:32:21 +0200629 vec_free (driver_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700630 return error;
631}
632
Ed Warnickecb9cada2015-12-08 15:45:58 -0700633
634static clib_error_t *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400635scan_uio_dir (void *arg, u8 * path_name, u8 * file_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700636{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400637 linux_pci_device_t *l = arg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700638 unformat_input_t input;
639
640 unformat_init_string (&input, (char *) file_name, vec_len (file_name));
641
Dave Barach9b8ffd92016-07-08 08:13:45 -0400642 if (!unformat (&input, "uio%d", &l->uio_minor))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700643 abort ();
644
645 unformat_free (&input);
646 return 0;
647}
648
Dave Barach9b8ffd92016-07-08 08:13:45 -0400649static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200650vfio_set_irqs (vlib_main_t * vm, linux_pci_device_t * p, u32 index, u32 start,
651 u32 count, u32 flags, int *efds)
Damjan Marion599a16b2018-03-04 19:35:23 +0100652{
653 int data_len = efds ? count * sizeof (int) : 0;
654 u8 buf[sizeof (struct vfio_irq_set) + data_len];
Damjan Marion23227982018-10-22 13:38:57 +0200655 struct vfio_irq_info ii = { 0 };
Damjan Marion599a16b2018-03-04 19:35:23 +0100656 struct vfio_irq_set *irq_set = (struct vfio_irq_set *) buf;
657
658
Damjan Marion23227982018-10-22 13:38:57 +0200659 ii.argsz = sizeof (struct vfio_irq_info);
660 ii.index = index;
Damjan Marion599a16b2018-03-04 19:35:23 +0100661
Damjan Marion23227982018-10-22 13:38:57 +0200662 if (ioctl (p->fd, VFIO_DEVICE_GET_IRQ_INFO, &ii) < 0)
Damjan Marion599a16b2018-03-04 19:35:23 +0100663 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) "
664 "'%U'", format_vlib_pci_addr, &p->addr);
665
Damjan Marion00ea98a2023-07-28 13:19:49 +0200666 log_debug (p, "%s index:%u count:%u flags: %s%s%s%s(0x%x)", __func__,
667 ii.index, ii.count,
668 ii.flags & VFIO_IRQ_INFO_EVENTFD ? "eventfd " : "",
669 ii.flags & VFIO_IRQ_INFO_MASKABLE ? "maskable " : "",
670 ii.flags & VFIO_IRQ_INFO_AUTOMASKED ? "automasked " : "",
671 ii.flags & VFIO_IRQ_INFO_NORESIZE ? "noresize " : "", ii.flags);
Damjan Marion23227982018-10-22 13:38:57 +0200672
673 if (ii.count < start + count)
Damjan Marion599a16b2018-03-04 19:35:23 +0100674 return clib_error_return_unix (0, "vfio_set_irq: unexistng interrupt on "
675 "'%U'", format_vlib_pci_addr, &p->addr);
676
677
678 if (efds)
679 {
680 flags |= VFIO_IRQ_SET_DATA_EVENTFD;
Dave Barach178cf492018-11-13 16:34:13 -0500681 clib_memcpy_fast (&irq_set->data, efds, data_len);
Damjan Marion599a16b2018-03-04 19:35:23 +0100682 }
683 else
684 flags |= VFIO_IRQ_SET_DATA_NONE;
685
686 ASSERT ((flags & (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD)) !=
687 (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD));
688
689 irq_set->argsz = sizeof (struct vfio_irq_set) + data_len;
690 irq_set->index = index;
691 irq_set->start = start;
692 irq_set->count = count;
693 irq_set->flags = flags;
694
695 if (ioctl (p->fd, VFIO_DEVICE_SET_IRQS, irq_set) < 0)
696 return clib_error_return_unix (0, "%U:ioctl(VFIO_DEVICE_SET_IRQS) "
697 "[index = %u, start = %u, count = %u, "
698 "flags = 0x%x]",
699 format_vlib_pci_addr, &p->addr,
700 index, start, count, flags);
701 return 0;
702}
703
704static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +0200705linux_pci_uio_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700706{
Damjan Marion23227982018-10-22 13:38:57 +0200707 vlib_main_t *vm = vlib_get_main ();
Damjan Marion5a206ea2016-05-12 22:11:03 +0200708 int __attribute__ ((unused)) rv;
Damjan Marioncef87f12017-10-05 15:32:41 +0200709 vlib_pci_dev_handle_t h = uf->private_data;
710 linux_pci_device_t *p = linux_pci_get_device (h);
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100711 linux_pci_irq_t *irq = &p->intx_irq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700712
Damjan Marion5a206ea2016-05-12 22:11:03 +0200713 u32 icount;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400714 rv = read (uf->file_descriptor, &icount, 4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700715
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100716 if (irq->intx_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200717 irq->intx_handler (vm, h);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200718
Damjan Marion23227982018-10-22 13:38:57 +0200719 vlib_pci_intr_enable (vm, h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700720
721 return /* no error */ 0;
722}
723
Dave Barach9b8ffd92016-07-08 08:13:45 -0400724static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200725linux_pci_vfio_unmask_intx (vlib_main_t * vm, linux_pci_device_t * d)
Damjan Marioncef87f12017-10-05 15:32:41 +0200726{
Damjan Marion23227982018-10-22 13:38:57 +0200727 return vfio_set_irqs (vm, d, VFIO_PCI_INTX_IRQ_INDEX, 0, 1,
Damjan Marion599a16b2018-03-04 19:35:23 +0100728 VFIO_IRQ_SET_ACTION_UNMASK, 0);
729}
730
Damjan Marioncef87f12017-10-05 15:32:41 +0200731static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +0200732linux_pci_uio_error_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700733{
734 u32 error_index = (u32) uf->private_data;
735
736 return clib_error_return (0, "pci device %d: error", error_index);
737}
738
Damjan Marioncef87f12017-10-05 15:32:41 +0200739static clib_error_t *
Damjan Marion599a16b2018-03-04 19:35:23 +0100740linux_pci_vfio_msix_read_ready (clib_file_t * uf)
741{
Damjan Marion23227982018-10-22 13:38:57 +0200742 vlib_main_t *vm = vlib_get_main ();
Damjan Marion599a16b2018-03-04 19:35:23 +0100743 int __attribute__ ((unused)) rv;
744 vlib_pci_dev_handle_t h = uf->private_data >> 16;
745 u16 line = uf->private_data & 0xffff;
746 linux_pci_device_t *p = linux_pci_get_device (h);
747 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, line);
748
749 u64 icount;
750 rv = read (uf->file_descriptor, &icount, sizeof (icount));
751
752 if (irq->msix_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200753 irq->msix_handler (vm, h, line);
Damjan Marion599a16b2018-03-04 19:35:23 +0100754
755 return /* no error */ 0;
756}
757
758static clib_error_t *
759linux_pci_vfio_intx_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700760{
Damjan Marion23227982018-10-22 13:38:57 +0200761 vlib_main_t *vm = vlib_get_main ();
Damjan Marioncef87f12017-10-05 15:32:41 +0200762 int __attribute__ ((unused)) rv;
763 vlib_pci_dev_handle_t h = uf->private_data;
764 linux_pci_device_t *p = linux_pci_get_device (h);
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100765 linux_pci_irq_t *irq = &p->intx_irq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700766
Damjan Marioncef87f12017-10-05 15:32:41 +0200767 u64 icount;
768 rv = read (uf->file_descriptor, &icount, sizeof (icount));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700769
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100770 if (irq->intx_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200771 irq->intx_handler (vm, h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700772
Damjan Marion23227982018-10-22 13:38:57 +0200773 linux_pci_vfio_unmask_intx (vm, p);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700774
Damjan Marioncef87f12017-10-05 15:32:41 +0200775 return /* no error */ 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700776}
777
Damjan Marioncef87f12017-10-05 15:32:41 +0200778static clib_error_t *
779linux_pci_vfio_error_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700780{
Damjan Marioncef87f12017-10-05 15:32:41 +0200781 u32 error_index = (u32) uf->private_data;
782
783 return clib_error_return (0, "pci device %d: error", error_index);
784}
785
786static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200787add_device_uio (vlib_main_t * vm, linux_pci_device_t * p,
788 vlib_pci_device_info_t * di, pci_device_registration_t * r)
Damjan Marioncef87f12017-10-05 15:32:41 +0200789{
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100790 linux_pci_main_t *lpm = &linux_pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +0200791 clib_error_t *err = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200792 u8 *s = 0;
793
Damjan Marionf9a968e2018-02-25 23:26:22 +0100794 p->fd = -1;
Damjan Marion2060db82018-03-04 17:37:15 +0100795 p->type = LINUX_PCI_DEVICE_TYPE_UIO;
Damjan Marioncef87f12017-10-05 15:32:41 +0200796
797 s = format (s, "%s/%U/config%c", sysfs_pci_dev_path,
798 format_vlib_pci_addr, &di->addr, 0);
799
800 p->config_fd = open ((char *) s, O_RDWR);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100801 p->config_offset = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200802 vec_reset_length (s);
803
804 if (p->config_fd == -1)
805 {
806 err = clib_error_return_unix (0, "open '%s'", s);
807 goto error;
808 }
809
Benoît Ganne0b91bd62019-11-20 09:07:50 +0100810 s = format (0, "%s/%U/uio%c", sysfs_pci_dev_path,
811 format_vlib_pci_addr, &di->addr, 0);
Damjan Marioncef87f12017-10-05 15:32:41 +0200812 foreach_directory_file ((char *) s, scan_uio_dir, p, /* scan_dirs */
813 1);
814 vec_reset_length (s);
815
816 s = format (s, "/dev/uio%d%c", p->uio_minor, 0);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100817 p->fd = open ((char *) s, O_RDWR);
818 if (p->fd < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +0200819 {
820 err = clib_error_return_unix (0, "open '%s'", s);
821 goto error;
822 }
823
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100824 if (r && r->interrupt_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200825 vlib_pci_register_intx_handler (vm, p->handle, r->interrupt_handler);
Damjan Marioncef87f12017-10-05 15:32:41 +0200826
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100827 if (r && r->init_function)
828 err = r->init_function (lpm->vlib_main, p->handle);
Damjan Marioncef87f12017-10-05 15:32:41 +0200829
830error:
Damjan Marion20ba1642018-03-26 15:35:33 +0200831 vec_free (s);
Damjan Marioncef87f12017-10-05 15:32:41 +0200832 if (err)
833 {
Damjan Marion96504182017-12-10 23:54:46 +0100834 if (p->config_fd != -1)
835 close (p->config_fd);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100836 if (p->fd != -1)
837 close (p->fd);
Damjan Marioncef87f12017-10-05 15:32:41 +0200838 }
839 return err;
840}
841
Damjan Marion599a16b2018-03-04 19:35:23 +0100842clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200843vlib_pci_register_intx_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h,
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100844 pci_intx_handler_function_t * intx_handler)
845{
846 linux_pci_device_t *p = linux_pci_get_device (h);
847 clib_file_t t = { 0 };
848 linux_pci_irq_t *irq = &p->intx_irq;
849 ASSERT (irq->fd == -1);
850
851 if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
852 {
Damjan Marion23227982018-10-22 13:38:57 +0200853 struct vfio_irq_info ii = { 0 };
854 ii.argsz = sizeof (struct vfio_irq_info);
855 ii.index = VFIO_PCI_INTX_IRQ_INDEX;
856 if (ioctl (p->fd, VFIO_DEVICE_GET_IRQ_INFO, &ii) < 0)
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100857 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) '"
858 "%U'", format_vlib_pci_addr, &p->addr);
Damjan Marion00ea98a2023-07-28 13:19:49 +0200859 log_debug (
860 p, "%s index:%u count:%u flags: %s%s%s%s(0x%x)", __func__, ii.index,
861 ii.count, ii.flags & VFIO_IRQ_INFO_EVENTFD ? "eventfd " : "",
862 ii.flags & VFIO_IRQ_INFO_MASKABLE ? "maskable " : "",
863 ii.flags & VFIO_IRQ_INFO_AUTOMASKED ? "automasked " : "",
864 ii.flags & VFIO_IRQ_INFO_NORESIZE ? "noresize " : "", ii.flags);
Damjan Marion23227982018-10-22 13:38:57 +0200865 if (ii.count != 1)
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100866 return clib_error_return (0, "INTx interrupt does not exist on device"
867 "'%U'", format_vlib_pci_addr, &p->addr);
868
869 irq->fd = eventfd (0, EFD_NONBLOCK);
870 if (irq->fd == -1)
871 return clib_error_return_unix (0, "eventfd");
Damjan Marion23227982018-10-22 13:38:57 +0200872
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100873 t.file_descriptor = irq->fd;
874 t.read_function = linux_pci_vfio_intx_read_ready;
875 }
876 else if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
877 {
878 t.file_descriptor = p->fd;
879 t.read_function = linux_pci_uio_read_ready;
880 }
881 else
882 return 0;
883
884 t.error_function = linux_pci_uio_error_ready;
885 t.private_data = p->handle;
886 t.description = format (0, "PCI %U INTx", format_vlib_pci_addr, &p->addr);
887 irq->clib_file_index = clib_file_add (&file_main, &t);
888 irq->intx_handler = intx_handler;
889 return 0;
890}
891
892clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200893vlib_pci_register_msix_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h,
894 u32 start, u32 count,
Damjan Marion599a16b2018-03-04 19:35:23 +0100895 pci_msix_handler_function_t * msix_handler)
896{
897 clib_error_t *err = 0;
898 linux_pci_device_t *p = linux_pci_get_device (h);
899 u32 i;
900
901 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
902 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
903 "support");
904
905 /* *INDENT-OFF* */
906 vec_validate_init_empty (p->msix_irqs, start + count - 1, (linux_pci_irq_t)
907 { .fd = -1});
908 /* *INDENT-ON* */
909
910 for (i = start; i < start + count; i++)
911 {
912 clib_file_t t = { 0 };
913 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
914 ASSERT (irq->fd == -1);
915
916 irq->fd = eventfd (0, EFD_NONBLOCK);
917 if (irq->fd == -1)
918 {
919 err = clib_error_return_unix (0, "eventfd");
920 goto error;
921 }
922
923 t.read_function = linux_pci_vfio_msix_read_ready;
924 t.file_descriptor = irq->fd;
925 t.error_function = linux_pci_vfio_error_ready;
926 t.private_data = p->handle << 16 | i;
927 t.description = format (0, "PCI %U MSI-X #%u", format_vlib_pci_addr,
928 &p->addr, i);
929 irq->clib_file_index = clib_file_add (&file_main, &t);
930 irq->msix_handler = msix_handler;
931 }
932
933 return 0;
934
935error:
936 while (i-- > start)
937 {
938 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
939 if (irq->fd != -1)
940 {
941 clib_file_del_by_index (&file_main, irq->clib_file_index);
942 close (irq->fd);
943 irq->fd = -1;
944 }
945 }
946 return err;
947}
948
949clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200950vlib_pci_enable_msix_irq (vlib_main_t * vm, vlib_pci_dev_handle_t h,
951 u16 start, u16 count)
Damjan Marion599a16b2018-03-04 19:35:23 +0100952{
953 linux_pci_device_t *p = linux_pci_get_device (h);
954 int fds[count];
955 int i;
956
957 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
958 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
959 "support");
960
961 for (i = start; i < start + count; i++)
962 {
963 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
964 fds[i] = irq->fd;
965 }
966
Damjan Marion23227982018-10-22 13:38:57 +0200967 return vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
Damjan Marion599a16b2018-03-04 19:35:23 +0100968 VFIO_IRQ_SET_ACTION_TRIGGER, fds);
969}
970
Damjan Marion9c9490c2020-10-07 20:00:39 +0200971uword
972vlib_pci_get_msix_file_index (vlib_main_t * vm, vlib_pci_dev_handle_t h,
973 u16 index)
974{
975 linux_pci_device_t *p = linux_pci_get_device (h);
976 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, index);
977 if (irq->fd == -1)
978 return ~0;
979 return irq->clib_file_index;
980}
981
Damjan Marion599a16b2018-03-04 19:35:23 +0100982clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200983vlib_pci_disable_msix_irq (vlib_main_t * vm, vlib_pci_dev_handle_t h,
984 u16 start, u16 count)
Damjan Marion599a16b2018-03-04 19:35:23 +0100985{
986 linux_pci_device_t *p = linux_pci_get_device (h);
987 int i, fds[count];
988
989 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
990 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
991 "support");
992
993 for (i = start; i < start + count; i++)
994 fds[i] = -1;
995
Damjan Marion23227982018-10-22 13:38:57 +0200996 return vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
Damjan Marion599a16b2018-03-04 19:35:23 +0100997 VFIO_IRQ_SET_ACTION_TRIGGER, fds);
998}
999
Damjan Marioncef87f12017-10-05 15:32:41 +02001000static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001001add_device_vfio (vlib_main_t * vm, linux_pci_device_t * p,
1002 vlib_pci_device_info_t * di, pci_device_registration_t * r)
Damjan Marioncef87f12017-10-05 15:32:41 +02001003{
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001004 linux_pci_main_t *lpm = &linux_pci_main;
Damjan Marionf9a968e2018-02-25 23:26:22 +01001005 struct vfio_device_info device_info = { 0 };
Damjan Marionf313b742018-03-05 14:08:33 +01001006 struct vfio_region_info reg = { 0 };
Damjan Marioncef87f12017-10-05 15:32:41 +02001007 clib_error_t *err = 0;
1008 u8 *s = 0;
Damjan Marion23227982018-10-22 13:38:57 +02001009 int is_noiommu;
Damjan Marioncef87f12017-10-05 15:32:41 +02001010
Damjan Marion2060db82018-03-04 17:37:15 +01001011 p->type = LINUX_PCI_DEVICE_TYPE_VFIO;
Damjan Marioncef87f12017-10-05 15:32:41 +02001012
Damjan Marion23227982018-10-22 13:38:57 +02001013 if ((err = linux_vfio_group_get_device_fd (&p->addr, &p->fd, &is_noiommu)))
Damjan Marioncef87f12017-10-05 15:32:41 +02001014 return err;
1015
Damjan Marion23227982018-10-22 13:38:57 +02001016 if (is_noiommu == 0)
1017 p->supports_va_dma = 1;
1018
Damjan Marioncef87f12017-10-05 15:32:41 +02001019 device_info.argsz = sizeof (device_info);
Damjan Marionf9a968e2018-02-25 23:26:22 +01001020 if (ioctl (p->fd, VFIO_DEVICE_GET_INFO, &device_info) < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +02001021 {
1022 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
1023 format_vlib_pci_addr, &di->addr);
1024 goto error;
1025 }
1026
Damjan Marionf313b742018-03-05 14:08:33 +01001027 reg.argsz = sizeof (struct vfio_region_info);
1028 reg.index = VFIO_PCI_CONFIG_REGION_INDEX;
1029 if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, &reg) < 0)
1030 {
1031 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
1032 format_vlib_pci_addr, &di->addr);
1033 goto error;
1034 }
Damjan Marion23227982018-10-22 13:38:57 +02001035
Damjan Marion00ea98a2023-07-28 13:19:49 +02001036 log_debug (p, "%s %U", __func__, format_vfio_region_info, &reg);
Damjan Marion23227982018-10-22 13:38:57 +02001037
Damjan Marionf313b742018-03-05 14:08:33 +01001038 p->config_offset = reg.offset;
1039 p->config_fd = p->fd;
1040
Damjan Marioncef87f12017-10-05 15:32:41 +02001041 /* reset if device supports it */
1042 if (device_info.flags & VFIO_DEVICE_FLAGS_RESET)
Damjan Marionf9a968e2018-02-25 23:26:22 +01001043 if (ioctl (p->fd, VFIO_DEVICE_RESET) < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +02001044 {
1045 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_RESET) '%U'",
1046 format_vlib_pci_addr, &di->addr);
1047 goto error;
1048 }
1049
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001050 if (r && r->interrupt_handler)
Damjan Marioncef87f12017-10-05 15:32:41 +02001051 {
Damjan Marion23227982018-10-22 13:38:57 +02001052 vlib_pci_register_intx_handler (vm, p->handle, r->interrupt_handler);
1053 linux_pci_vfio_unmask_intx (vm, p);
Damjan Marioncef87f12017-10-05 15:32:41 +02001054 }
1055
Damjan Marion68b4da62018-09-30 18:26:20 +02001056 if (p->supports_va_dma)
1057 {
1058 vlib_buffer_pool_t *bp;
1059 /* *INDENT-OFF* */
Damjan Mariond50e3472019-01-20 00:03:56 +01001060 vec_foreach (bp, vm->buffer_main->buffer_pools)
Damjan Marion68b4da62018-09-30 18:26:20 +02001061 {
1062 u32 i;
1063 vlib_physmem_map_t *pm;
1064 pm = vlib_physmem_get_map (vm, bp->physmem_map_index);
1065 for (i = 0; i < pm->n_pages; i++)
1066 vfio_map_physmem_page (vm, pm->base + (i << pm->log2_page_size));
1067 }
1068 /* *INDENT-ON* */
1069 }
1070
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001071 if (r && r->init_function)
1072 err = r->init_function (lpm->vlib_main, p->handle);
Damjan Marioncef87f12017-10-05 15:32:41 +02001073
1074error:
1075 vec_free (s);
1076 if (err)
1077 {
Damjan Marionf9a968e2018-02-25 23:26:22 +01001078 if (p->fd != -1)
1079 close (p->fd);
Chris Luke30684ac2018-03-29 12:56:58 -07001080 if (p->config_fd != -1 && p->config_fd != p->fd)
Damjan Marion96504182017-12-10 23:54:46 +01001081 close (p->config_fd);
Chris Luke30684ac2018-03-29 12:56:58 -07001082 p->config_fd = p->fd = -1;
Damjan Marioncef87f12017-10-05 15:32:41 +02001083 }
1084 return err;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001085}
1086
1087/* Configuration space read/write. */
1088clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001089vlib_pci_read_write_config (vlib_main_t * vm, vlib_pci_dev_handle_t h,
Damjan Marion5a206ea2016-05-12 22:11:03 +02001090 vlib_read_or_write_t read_or_write,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001091 uword address, void *data, u32 n_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001092{
Damjan Marioncef87f12017-10-05 15:32:41 +02001093 linux_pci_device_t *p = linux_pci_get_device (h);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001094 int n;
1095
Ed Warnickecb9cada2015-12-08 15:45:58 -07001096 if (read_or_write == VLIB_READ)
Damjan Marionf9a968e2018-02-25 23:26:22 +01001097 n = pread (p->config_fd, data, n_bytes, p->config_offset + address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001098 else
Damjan Marionf9a968e2018-02-25 23:26:22 +01001099 n = pwrite (p->config_fd, data, n_bytes, p->config_offset + address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001100
1101 if (n != n_bytes)
1102 return clib_error_return_unix (0, "%s",
1103 read_or_write == VLIB_READ
1104 ? "read" : "write");
1105
1106 return 0;
1107}
1108
1109static clib_error_t *
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001110vlib_pci_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 bar, int *fd,
1111 u64 * size, u64 * offset)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001112{
Damjan Marioncef87f12017-10-05 15:32:41 +02001113 linux_pci_device_t *p = linux_pci_get_device (h);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001114 clib_error_t *error = 0;
1115 int _fd = -1;
1116 u64 _size = 0, _offset = 0;
Damjan Marion2060db82018-03-04 17:37:15 +01001117
1118 ASSERT (bar <= 5);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001119
1120 error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001121
Damjan Marion2060db82018-03-04 17:37:15 +01001122 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001123 {
Damjan Marion2060db82018-03-04 17:37:15 +01001124 u8 *file_name;
1125 struct stat stat_buf;
1126 file_name = format (0, "%s/%U/resource%d%c", sysfs_pci_dev_path,
1127 format_vlib_pci_addr, &p->addr, bar, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001128
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001129 _fd = open ((char *) file_name, O_RDWR);
1130 if (_fd < 0)
Damjan Marion2060db82018-03-04 17:37:15 +01001131 {
1132 error = clib_error_return_unix (0, "open `%s'", file_name);
1133 vec_free (file_name);
1134 return error;
1135 }
1136
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001137 if (fstat (_fd, &stat_buf) < 0)
Damjan Marion2060db82018-03-04 17:37:15 +01001138 {
1139 error = clib_error_return_unix (0, "fstat `%s'", file_name);
1140 vec_free (file_name);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001141 close (_fd);
Damjan Marion2060db82018-03-04 17:37:15 +01001142 return error;
1143 }
1144
1145 vec_free (file_name);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001146 _size = stat_buf.st_size;
1147 _offset = 0;
Damjan Marion2060db82018-03-04 17:37:15 +01001148 }
1149 else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001150 {
Damjan Marion0792bb42020-05-25 19:31:42 +02001151 struct vfio_region_info *r;
1152 u32 sz = sizeof (struct vfio_region_info);
1153 again:
1154 r = clib_mem_alloc (sz);
1155 clib_memset (r, 0, sz);
1156 r->argsz = sz;
1157 r->index = bar;
1158 if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, r) < 0)
Damjan Marion2060db82018-03-04 17:37:15 +01001159 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) "
1160 "'%U'", format_vlib_pci_addr,
1161 &p->addr);
Damjan Marion0792bb42020-05-25 19:31:42 +02001162 if (sz != r->argsz)
1163 {
1164 sz = r->argsz;
1165 clib_mem_free (r);
1166 goto again;
1167 }
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001168 _fd = p->fd;
Damjan Marion0792bb42020-05-25 19:31:42 +02001169 _size = r->size;
1170 _offset = r->offset;
Damjan Marion00ea98a2023-07-28 13:19:49 +02001171 log_debug (p, "%s %U", __func__, format_vfio_region_info, r);
Damjan Marion0792bb42020-05-25 19:31:42 +02001172 clib_mem_free (r);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001173 }
Damjan Marion2060db82018-03-04 17:37:15 +01001174 else
1175 ASSERT (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001176
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001177 *fd = _fd;
1178 *size = _size;
1179 *offset = _offset;
1180
1181 return error;
1182}
1183
1184static clib_error_t *
1185vlib_pci_map_region_int (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1186 u32 bar, u8 * addr, void **result)
1187{
1188 linux_pci_device_t *p = linux_pci_get_device (h);
1189 int fd = -1;
1190 clib_error_t *error;
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001191 u64 size = 0, offset = 0;
Damjan Marion00ea98a2023-07-28 13:19:49 +02001192 vlib_pci_config_reg_command_t command;
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001193
Damjan Marion00ea98a2023-07-28 13:19:49 +02001194 log_debug (p, "map region %u to va %p", bar, addr);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001195
Damjan Marion00ea98a2023-07-28 13:19:49 +02001196 if ((error = vlib_pci_read_config_u16 (vm, h, 4, &command.as_u16)))
Mohammed Hawari70fc36f2020-10-21 16:41:30 +02001197 return error;
1198
Damjan Marion00ea98a2023-07-28 13:19:49 +02001199 if (!(command.mem_space))
Mohammed Hawari70fc36f2020-10-21 16:41:30 +02001200 {
Damjan Marion00ea98a2023-07-28 13:19:49 +02001201 log_debug (p, "setting memory enable bit");
1202 command.mem_space = 1;
1203 if ((error = vlib_pci_write_config_u16 (vm, h, 4, &command.as_u16)))
Mohammed Hawari70fc36f2020-10-21 16:41:30 +02001204 return error;
1205 }
1206
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001207 if ((error = vlib_pci_region (vm, h, bar, &fd, &size, &offset)))
1208 return error;
1209
Damjan Marion6bfd0762020-09-11 22:16:53 +02001210 *result = clib_mem_vm_map_shared (addr, size, fd, offset,
1211 "PCIe %U region %u", format_vlib_pci_addr,
1212 vlib_pci_get_addr (vm, h), bar);
1213 if (*result == CLIB_MEM_VM_MAP_FAILED)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001214 {
Damjan Marion2060db82018-03-04 17:37:15 +01001215 error = clib_error_return_unix (0, "mmap `BAR%u'", bar);
Dave Barachc72950e2020-05-04 14:47:45 -04001216 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO && (fd != -1))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001217 close (fd);
Damjan Marion2060db82018-03-04 17:37:15 +01001218 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001219 }
Damjan Marion2060db82018-03-04 17:37:15 +01001220
1221 /* *INDENT-OFF* */
1222 vec_validate_init_empty (p->regions, bar,
1223 (linux_pci_region_t) { .fd = -1});
1224 /* *INDENT-ON* */
1225 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1226 p->regions[bar].fd = fd;
1227 p->regions[bar].addr = *result;
1228 p->regions[bar].size = size;
1229 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001230}
1231
1232clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001233vlib_pci_map_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 resource,
1234 void **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001235{
Damjan Marion23227982018-10-22 13:38:57 +02001236 return (vlib_pci_map_region_int (vm, h, resource, 0 /* addr */ , result));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001237}
1238
1239clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001240vlib_pci_map_region_fixed (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1241 u32 resource, u8 * addr, void **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001242{
Damjan Marion23227982018-10-22 13:38:57 +02001243 return (vlib_pci_map_region_int (vm, h, resource, addr, result));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001244}
1245
Damjan Marionf313b742018-03-05 14:08:33 +01001246clib_error_t *
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001247vlib_pci_io_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 resource)
1248{
1249 linux_pci_device_t *p = linux_pci_get_device (h);
1250 clib_error_t *error = 0;
1251 int fd = -1;
1252 u64 size = 0, offset = 0;
1253
1254 if ((error = vlib_pci_region (vm, h, resource, &fd, &size, &offset)))
1255 return error;
1256
1257 p->io_fd = fd;
1258 p->io_offset = offset;
1259 return error;
1260}
1261
1262clib_error_t *
1263vlib_pci_read_write_io (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1264 vlib_read_or_write_t read_or_write,
1265 uword offset, void *data, u32 length)
1266{
1267 linux_pci_device_t *p = linux_pci_get_device (h);
1268 int n = 0;
1269
1270 if (read_or_write == VLIB_READ)
1271 n = pread (p->io_fd, data, length, p->io_offset + offset);
1272 else
1273 n = pwrite (p->io_fd, data, length, p->io_offset + offset);
1274
1275 if (n != length)
1276 return clib_error_return_unix (0, "%s",
1277 read_or_write == VLIB_READ
1278 ? "read" : "write");
1279 return 0;
1280}
1281
1282clib_error_t *
Damjan Marion68b4da62018-09-30 18:26:20 +02001283vlib_pci_map_dma (vlib_main_t * vm, vlib_pci_dev_handle_t h, void *ptr)
1284{
1285 linux_pci_device_t *p = linux_pci_get_device (h);
1286
1287 if (!p->supports_va_dma)
1288 return 0;
1289
1290 return vfio_map_physmem_page (vm, ptr);
1291}
1292
1293int
1294vlib_pci_supports_virtual_addr_dma (vlib_main_t * vm, vlib_pci_dev_handle_t h)
1295{
1296 linux_pci_device_t *p = linux_pci_get_device (h);
1297
1298 return p->supports_va_dma != 0;
1299}
1300
1301clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001302vlib_pci_device_open (vlib_main_t * vm, vlib_pci_addr_t * addr,
Damjan Marionf313b742018-03-05 14:08:33 +01001303 pci_device_id_t ids[], vlib_pci_dev_handle_t * handle)
1304{
1305 linux_pci_main_t *lpm = &linux_pci_main;
1306 vlib_pci_device_info_t *di;
1307 linux_pci_device_t *p;
1308 clib_error_t *err = 0;
1309 pci_device_id_t *i;
1310
Damjan Marion23227982018-10-22 13:38:57 +02001311 di = vlib_pci_get_device_info (vm, addr, &err);
Damjan Marionf313b742018-03-05 14:08:33 +01001312
1313 if (err)
1314 return err;
Damjan Marionf313b742018-03-05 14:08:33 +01001315
Damjan Marion00ea98a2023-07-28 13:19:49 +02001316 if (ids)
1317 {
1318 for (i = ids; i->vendor_id != 0; i++)
1319 if (i->vendor_id == di->vendor_id && i->device_id == di->device_id)
1320 break;
1321
1322 if (i->vendor_id == 0)
1323 {
1324 vlib_pci_free_device_info (di);
1325 return clib_error_return (0, "Wrong vendor or device id");
1326 }
1327 }
Damjan Marionf313b742018-03-05 14:08:33 +01001328
1329 pool_get (lpm->linux_pci_devices, p);
1330 p->handle = p - lpm->linux_pci_devices;
Damjan Marion23227982018-10-22 13:38:57 +02001331 p->addr.as_u32 = di->addr.as_u32;
Damjan Marionf313b742018-03-05 14:08:33 +01001332 p->intx_irq.fd = -1;
Mohsin Kazmi692f9b12019-04-02 11:09:49 +00001333 p->intx_irq.clib_file_index = -1;
Damjan Mariond2bfb782019-01-07 20:56:04 +01001334 p->numa_node = di->numa_node;
Mohsin Kazmi3d3b9552018-10-24 14:05:34 +02001335 /*
1336 * pci io bar read/write fd
1337 */
1338 p->io_fd = -1;
Damjan Marionf313b742018-03-05 14:08:33 +01001339
Damjan Marion00ea98a2023-07-28 13:19:49 +02001340 log_debug (p, "open vid:0x%04x did:0x%04x driver:%s iommu_group:%d",
1341 di->vendor_id, di->device_id, di->driver_name, di->iommu_group);
Damjan Marion23227982018-10-22 13:38:57 +02001342
Steven Luongab489922019-08-21 11:35:27 -07001343 if (clib_strncmp ("vfio-pci", (char *) di->driver_name, 8) == 0)
Damjan Marion23227982018-10-22 13:38:57 +02001344 err = add_device_vfio (vm, p, di, 0);
Steven Luongab489922019-08-21 11:35:27 -07001345 else if (clib_strncmp ("uio_pci_generic", (char *) di->driver_name, 8) == 0)
Damjan Marion23227982018-10-22 13:38:57 +02001346 err = add_device_uio (vm, p, di, 0);
Damjan Marionf313b742018-03-05 14:08:33 +01001347 else
Damjan Marion23227982018-10-22 13:38:57 +02001348 err = clib_error_create ("device not bound to 'vfio-pci' or "
1349 "'uio_pci_generic' kernel module");
Damjan Marionf313b742018-03-05 14:08:33 +01001350 if (err)
1351 goto error;
1352
1353 *handle = p->handle;
1354
1355error:
1356 vlib_pci_free_device_info (di);
1357 if (err)
1358 {
Damjan Marion00ea98a2023-07-28 13:19:49 +02001359 log_err (p, "%U", format_clib_error, err);
Dave Barachb7b92992018-10-17 10:38:51 -04001360 clib_memset (p, 0, sizeof (linux_pci_device_t));
Damjan Marionf313b742018-03-05 14:08:33 +01001361 pool_put (lpm->linux_pci_devices, p);
1362 }
1363
1364 return err;
1365}
1366
1367void
Damjan Marion23227982018-10-22 13:38:57 +02001368vlib_pci_device_close (vlib_main_t * vm, vlib_pci_dev_handle_t h)
Damjan Marionf313b742018-03-05 14:08:33 +01001369{
1370 linux_pci_main_t *lpm = &linux_pci_main;
1371 linux_pci_device_t *p = linux_pci_get_device (h);
1372 linux_pci_irq_t *irq;
1373 linux_pci_region_t *res;
1374 clib_error_t *err = 0;
1375
1376 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1377 {
1378 irq = &p->intx_irq;
Mohsin Kazmi692f9b12019-04-02 11:09:49 +00001379 if (irq->clib_file_index != -1)
1380 clib_file_del_by_index (&file_main, irq->clib_file_index);
Damjan Marionf313b742018-03-05 14:08:33 +01001381 close (p->config_fd);
Mohsin Kazmi3d3b9552018-10-24 14:05:34 +02001382 if (p->io_fd != -1)
1383 close (p->io_fd);
Damjan Marionf313b742018-03-05 14:08:33 +01001384 }
1385 else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
1386 {
1387 irq = &p->intx_irq;
1388 /* close INTx irqs */
1389 if (irq->fd != -1)
1390 {
Damjan Marion23227982018-10-22 13:38:57 +02001391 err = vfio_set_irqs (vm, p, VFIO_PCI_INTX_IRQ_INDEX, 0, 0,
Damjan Marionf313b742018-03-05 14:08:33 +01001392 VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1393 clib_error_free (err);
Mohsin Kazmi692f9b12019-04-02 11:09:49 +00001394 if (irq->clib_file_index != -1)
1395 clib_file_del_by_index (&file_main, irq->clib_file_index);
Damjan Marionf313b742018-03-05 14:08:33 +01001396 close (irq->fd);
1397 }
1398
1399 /* close MSI-X irqs */
1400 if (vec_len (p->msix_irqs))
1401 {
Damjan Marion23227982018-10-22 13:38:57 +02001402 err = vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, 0, 0,
Damjan Marionf313b742018-03-05 14:08:33 +01001403 VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1404 clib_error_free (err);
1405 /* *INDENT-OFF* */
1406 vec_foreach (irq, p->msix_irqs)
1407 {
1408 if (irq->fd == -1)
1409 continue;
1410 clib_file_del_by_index (&file_main, irq->clib_file_index);
1411 close (irq->fd);
1412 }
1413 /* *INDENT-ON* */
1414 vec_free (p->msix_irqs);
1415 }
1416 }
1417
1418 /* *INDENT-OFF* */
1419 vec_foreach (res, p->regions)
1420 {
1421 if (res->size == 0)
1422 continue;
Damjan Marion6bfd0762020-09-11 22:16:53 +02001423 clib_mem_vm_unmap (res->addr);
Damjan Marionf313b742018-03-05 14:08:33 +01001424 if (res->fd != -1)
1425 close (res->fd);
1426 }
1427 /* *INDENT-ON* */
1428 vec_free (p->regions);
1429
1430 close (p->fd);
Dave Barachb7b92992018-10-17 10:38:51 -04001431 clib_memset (p, 0, sizeof (linux_pci_device_t));
Damjan Marionf313b742018-03-05 14:08:33 +01001432 pool_put (lpm->linux_pci_devices, p);
1433}
1434
Dave Barach9b8ffd92016-07-08 08:13:45 -04001435void
Damjan Marion23227982018-10-22 13:38:57 +02001436init_device_from_registered (vlib_main_t * vm, vlib_pci_device_info_t * di)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001437{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001438 vlib_pci_main_t *pm = &pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +02001439 linux_pci_main_t *lpm = &linux_pci_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001440 pci_device_registration_t *r;
1441 pci_device_id_t *i;
Damjan Marioncef87f12017-10-05 15:32:41 +02001442 clib_error_t *err = 0;
1443 linux_pci_device_t *p;
1444
1445 pool_get (lpm->linux_pci_devices, p);
1446 p->handle = p - lpm->linux_pci_devices;
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001447 p->intx_irq.fd = -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001448
Damjan Marion5a206ea2016-05-12 22:11:03 +02001449 r = pm->pci_device_registrations;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001450
1451 while (r)
1452 {
1453 for (i = r->supported_devices; i->vendor_id != 0; i++)
Damjan Marion3a593822018-02-17 14:06:53 +01001454 if (i->vendor_id == di->vendor_id && i->device_id == di->device_id)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001455 {
Damjan Marioncef87f12017-10-05 15:32:41 +02001456 if (di->iommu_group != -1)
Damjan Marion23227982018-10-22 13:38:57 +02001457 err = add_device_vfio (vm, p, di, r);
Damjan Marioncef87f12017-10-05 15:32:41 +02001458 else
Damjan Marion23227982018-10-22 13:38:57 +02001459 err = add_device_uio (vm, p, di, r);
Damjan Marion5a206ea2016-05-12 22:11:03 +02001460
Damjan Marioncef87f12017-10-05 15:32:41 +02001461 if (err)
1462 clib_error_report (err);
1463 else
1464 return;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001465 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001466 r = r->next_registration;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001467 }
Damjan Marioncef87f12017-10-05 15:32:41 +02001468
Ed Warnickecb9cada2015-12-08 15:45:58 -07001469 /* No driver, close the PCI config-space FD */
Dave Barachb7b92992018-10-17 10:38:51 -04001470 clib_memset (p, 0, sizeof (linux_pci_device_t));
Damjan Marioncef87f12017-10-05 15:32:41 +02001471 pool_put (lpm->linux_pci_devices, p);
1472}
1473
1474static clib_error_t *
1475scan_pci_addr (void *arg, u8 * dev_dir_name, u8 * ignored)
1476{
1477 vlib_pci_addr_t addr, **addrv = arg;
1478 unformat_input_t input;
1479 clib_error_t *err = 0;
1480
1481 unformat_init_string (&input, (char *) dev_dir_name,
1482 vec_len (dev_dir_name));
1483
1484 if (!unformat (&input, "/sys/bus/pci/devices/%U",
1485 unformat_vlib_pci_addr, &addr))
1486 err = clib_error_return (0, "unformat error `%v`", dev_dir_name);
1487
1488 unformat_free (&input);
1489
1490 if (err)
1491 return err;
1492
1493 vec_add1 (*addrv, addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001494 return 0;
1495}
1496
Damjan Marioncef87f12017-10-05 15:32:41 +02001497static int
1498pci_addr_cmp (void *v1, void *v2)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001499{
Damjan Marioncef87f12017-10-05 15:32:41 +02001500 vlib_pci_addr_t *a1 = v1;
1501 vlib_pci_addr_t *a2 = v2;
1502
1503 if (a1->domain > a2->domain)
1504 return 1;
1505 if (a1->domain < a2->domain)
1506 return -1;
1507 if (a1->bus > a2->bus)
1508 return 1;
1509 if (a1->bus < a2->bus)
1510 return -1;
1511 if (a1->slot > a2->slot)
1512 return 1;
1513 if (a1->slot < a2->slot)
1514 return -1;
1515 if (a1->function > a2->function)
1516 return 1;
1517 if (a1->function < a2->function)
1518 return -1;
1519 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001520}
1521
Damjan Marioncef87f12017-10-05 15:32:41 +02001522vlib_pci_addr_t *
1523vlib_pci_get_all_dev_addrs ()
Ed Warnickecb9cada2015-12-08 15:45:58 -07001524{
Damjan Marioncef87f12017-10-05 15:32:41 +02001525 vlib_pci_addr_t *addrs = 0;
1526 clib_error_t *err;
1527 err = foreach_directory_file ((char *) sysfs_pci_dev_path, scan_pci_addr,
1528 &addrs, /* scan_dirs */ 0);
1529 if (err)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001530 {
Damjan Marioncef87f12017-10-05 15:32:41 +02001531 vec_free (addrs);
1532 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001533 }
1534
Damjan Marioncef87f12017-10-05 15:32:41 +02001535 vec_sort_with_function (addrs, pci_addr_cmp);
Damjan Mariona42cd342016-04-13 18:03:20 +02001536
Damjan Marioncef87f12017-10-05 15:32:41 +02001537 return addrs;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001538}
1539
Dave Barach9b8ffd92016-07-08 08:13:45 -04001540clib_error_t *
1541linux_pci_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001542{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001543 vlib_pci_main_t *pm = &pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +02001544 vlib_pci_addr_t *addr = 0, *addrs;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001545
1546 pm->vlib_main = vm;
1547
Dave Barach9b8ffd92016-07-08 08:13:45 -04001548 ASSERT (sizeof (vlib_pci_addr_t) == sizeof (u32));
Damjan Mariona42cd342016-04-13 18:03:20 +02001549
Damjan Marioncef87f12017-10-05 15:32:41 +02001550 addrs = vlib_pci_get_all_dev_addrs ();
1551 /* *INDENT-OFF* */
1552 vec_foreach (addr, addrs)
1553 {
1554 vlib_pci_device_info_t *d;
Damjan Marion23227982018-10-22 13:38:57 +02001555 if ((d = vlib_pci_get_device_info (vm, addr, 0)))
Damjan Marioncef87f12017-10-05 15:32:41 +02001556 {
Damjan Marion23227982018-10-22 13:38:57 +02001557 init_device_from_registered (vm, d);
Damjan Marioncef87f12017-10-05 15:32:41 +02001558 vlib_pci_free_device_info (d);
1559 }
1560 }
1561 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001562
Dave Barachf8d50682019-05-14 18:01:44 -04001563 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001564}
1565
Dave Barachf8d50682019-05-14 18:01:44 -04001566/* *INDENT-OFF* */
1567VLIB_INIT_FUNCTION (linux_pci_init) =
1568{
1569 .runs_after = VLIB_INITS("unix_input_init"),
1570};
1571/* *INDENT-ON* */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001572
1573/*
1574 * fd.io coding-style-patch-verification: ON
1575 *
1576 * Local Variables:
1577 * eval: (c-set-style "gnu")
1578 * End:
1579 */