47 #include <sys/types.h> 51 #include <sys/ioctl.h> 53 #include <linux/ethtool.h> 54 #include <linux/sockios.h> 56 #include <sys/eventfd.h> 61 "/sys/module/vfio/parameters/enable_unsafe_noiommu_mode";
173 di->
addr.as_u32 = addr->as_u32;
178 f =
format (0,
"%v/config%c", dev_dir_name, 0);
179 fd = open ((
char *) f, O_RDWR);
183 fd = open ((
char *) f, O_RDONLY);
204 memset (&all_ones, ~0,
sizeof (all_ones));
206 if (!memcmp (&di->
config0.
header, &all_ones, sizeof (all_ones)))
221 f =
format (f,
"%v/numa_node%c", dev_dir_name, 0);
230 f =
format (f,
"%v/class%c", dev_dir_name, 0);
237 f =
format (f,
"%v/vendor%c", dev_dir_name, 0);
244 f =
format (f,
"%v/device%c", dev_dir_name, 0);
251 f =
format (f,
"%v/driver%c", dev_dir_name, 0);
259 f =
format (f,
"%v/iommu_group%c", dev_dir_name, 0);
267 f =
format (f,
"%v/iommu_group/name%c", dev_dir_name, 0);
271 if (strncmp ((
char *) tmpstr,
"vfio-noiommu", 12) == 0)
282 f =
format (f,
"%v/vpd%c", dev_dir_name, 0);
283 fd = open ((
char *) f, O_RDONLY);
292 if (read (fd, &tag, 3) != 3)
295 if (tag[0] != 0x82 && tag[0] != 0x90 && tag[0] != 0x91)
298 len = (tag[2] << 8) | tag[1];
301 if (read (fd, data, len) != len)
308 else if (tag[0] == 0x90)
310 else if (tag[0] == 0x91)
337 struct stat s = { 0 };
338 if (stat (path, &s) == -1)
341 return S_ISDIR (s.st_mode);
348 u8 *s = 0, *driver_name = 0;
352 int fd, clear_driver_override = 0;
361 if (strncmp (
"auto", uio_drv_name, 5) == 0)
363 int vfio_pci_loaded = 0;
371 if (!vfio_pci_loaded)
374 "is bound to IOMMU group and " 375 "vfio-pci driver is not loaded",
380 uio_drv_name =
"vfio-pci";
385 if (vfio_pci_loaded &&
387 uio_drv_name =
"vfio-pci";
389 uio_drv_name =
"uio_pci_generic";
391 uio_drv_name =
"igb_uio";
396 "kernel VFIO or UIO driver",
404 s =
format (s,
"%v/driver%c", dev_dir_name, 0);
409 ((strcmp (
"vfio-pci", (
char *) driver_name) == 0) ||
410 (strcmp (
"uio_pci_generic", (
char *) driver_name) == 0) ||
411 (strcmp (
"igb_uio", (
char *) driver_name) == 0)))
416 dir = opendir (
"/sys/class/net");
422 "read /sys/class/net",
427 fd = socket (PF_INET, SOCK_DGRAM, 0);
434 while ((e = readdir (dir)))
437 struct ethtool_drvinfo drvinfo;
439 if (e->d_name[0] ==
'.')
442 memset (&ifr, 0,
sizeof ifr);
443 memset (&drvinfo, 0,
sizeof drvinfo);
444 ifr.ifr_data = (
char *) &drvinfo;
445 strncpy (ifr.ifr_name, e->d_name, IFNAMSIZ - 1);
446 drvinfo.cmd = ETHTOOL_GDRVINFO;
447 if (ioctl (fd, SIOCETHTOOL, &ifr) < 0)
450 if ((errno != ENOTSUP) && (errno != ENODEV))
456 if (strcmp ((
char *) s, drvinfo.bus_info))
459 memset (&ifr, 0,
sizeof (ifr));
460 strncpy (ifr.ifr_name, e->d_name, IFNAMSIZ - 1);
461 if (ioctl (fd, SIOCGIFFLAGS, &ifr) < 0)
469 if (ifr.ifr_flags & IFF_UP)
472 "interface %s is up",
482 s =
format (s,
"%v/driver/unbind%c", dev_dir_name, 0);
486 s =
format (s,
"%v/driver_override%c", dev_dir_name, 0);
487 if (access ((
char *) s, F_OK) == 0)
490 clear_driver_override = 1;
505 if (clear_driver_override)
507 s =
format (s,
"%v/driver_override%c", dev_dir_name, 0);
540 int data_len = efds ? count *
sizeof (int) : 0;
541 u8 buf[
sizeof (
struct vfio_irq_set) + data_len];
542 struct vfio_irq_info irq_info = { 0 };
543 struct vfio_irq_set *irq_set = (
struct vfio_irq_set *) buf;
546 irq_info.argsz =
sizeof (
struct vfio_irq_info);
547 irq_info.index = index;
549 if (ioctl (p->
fd, VFIO_DEVICE_GET_IRQ_INFO, &irq_info) < 0)
553 if (irq_info.count < start + count)
560 flags |= VFIO_IRQ_SET_DATA_EVENTFD;
564 flags |= VFIO_IRQ_SET_DATA_NONE;
566 ASSERT ((flags & (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD)) !=
567 (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD));
569 irq_set->argsz =
sizeof (
struct vfio_irq_set) + data_len;
570 irq_set->index = index;
571 irq_set->start = start;
572 irq_set->count =
count;
573 irq_set->flags =
flags;
575 if (ioctl (p->
fd, VFIO_DEVICE_SET_IRQS, irq_set) < 0)
577 "[index = %u, start = %u, count = %u, " 580 index, start, count, flags);
587 int __attribute__ ((unused)) rv;
607 VFIO_IRQ_SET_ACTION_UNMASK, 0);
621 int __attribute__ ((unused)) rv;
639 int __attribute__ ((unused)) rv;
678 p->
config_fd = open ((
char *) s, O_RDWR);
695 p->
fd = open ((
char *) s, O_RDWR);
702 if (r && r->interrupt_handler)
705 if (r && r->init_function)
731 struct vfio_irq_info irq_info = { 0 };
732 irq_info.argsz =
sizeof (
struct vfio_irq_info);
733 irq_info.index = VFIO_PCI_INTX_IRQ_INDEX;
734 if (ioctl (p->
fd, VFIO_DEVICE_GET_IRQ_INFO, &irq_info) < 0)
737 if (irq_info.count != 1)
741 irq->
fd = eventfd (0, EFD_NONBLOCK);
780 for (i = start; i < start +
count; i++)
786 irq->
fd = eventfd (0, EFD_NONBLOCK);
830 for (i = start; i < start +
count; i++)
836 return vfio_set_irqs (p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
837 VFIO_IRQ_SET_ACTION_TRIGGER, fds);
850 for (i = start; i < start +
count; i++)
853 return vfio_set_irqs (p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
854 VFIO_IRQ_SET_ACTION_TRIGGER, fds);
862 struct vfio_device_info device_info = { 0 };
863 struct vfio_region_info reg = { 0 };
871 (strcmp (
"vfio-pci", (
char *) di->
driver_name) != 0))
879 device_info.argsz =
sizeof (device_info);
880 if (ioctl (p->
fd, VFIO_DEVICE_GET_INFO, &device_info) < 0)
887 reg.argsz =
sizeof (
struct vfio_region_info);
888 reg.index = VFIO_PCI_CONFIG_REGION_INDEX;
889 if (ioctl (p->
fd, VFIO_DEVICE_GET_REGION_INFO, ®) < 0)
899 if (device_info.flags & VFIO_DEVICE_FLAGS_RESET)
900 if (ioctl (p->
fd, VFIO_DEVICE_RESET) < 0)
907 if (r && r->interrupt_handler)
913 if (r && r->init_function)
958 int flags = MAP_SHARED;
968 struct stat stat_buf;
972 fd = open ((
char *) file_name, O_RDWR);
980 if (fstat (fd, &stat_buf) < 0)
991 size = stat_buf.st_size;
996 struct vfio_region_info reg = { 0 };
997 reg.argsz =
sizeof (
struct vfio_region_info);
999 if (ioctl (p->
fd, VFIO_DEVICE_GET_REGION_INFO, ®) < 0)
1010 *result = mmap (addr, size, PROT_READ | PROT_WRITE, flags, fd,
offset);
1011 if (*result == (
void *) -1)
1110 VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1120 VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1170 for (i = r->supported_devices; i->
vendor_id != 0; i++)
1183 r = r->next_registration;
1194 vlib_pci_addr_t
addr, **addrv = arg;
1201 if (!
unformat (&input,
"/sys/bus/pci/devices/%U",
1217 vlib_pci_addr_t *a1 = v1;
1218 vlib_pci_addr_t *a2 = v2;
1220 if (a1->domain > a2->domain)
1222 if (a1->domain < a2->domain)
1224 if (a1->bus > a2->bus)
1226 if (a1->bus < a2->bus)
1228 if (a1->slot > a2->slot)
1230 if (a1->slot < a2->slot)
1232 if (a1->function > a2->function)
1234 if (a1->function < a2->function)
1242 vlib_pci_addr_t *addrs = 0;
1261 vlib_pci_addr_t *
addr = 0, *addrs;
1269 ASSERT (
sizeof (vlib_pci_addr_t) ==
sizeof (
u32));
clib_error_t * vlib_pci_register_intx_handler(vlib_pci_dev_handle_t h, pci_intx_handler_function_t *intx_handler)
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
u8 * format_vlib_pci_addr(u8 *s, va_list *va)
static clib_error_t * add_device_vfio(linux_pci_device_t *p, vlib_pci_device_info_t *di, pci_device_registration_t *r)
linux_pci_device_type_t type
uword unformat_vlib_pci_addr(unformat_input_t *input, va_list *args)
pci_device_registration_t * pci_device_registrations
vlib_pci_device_info_t * vlib_pci_get_device_info(vlib_pci_addr_t *addr, clib_error_t **error)
vlib_pci_addr_t * vlib_pci_get_addr(vlib_pci_dev_handle_t h)
clib_error_t * linux_vfio_group_get_device_fd(vlib_pci_addr_t *addr, int *fdp)
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
clib_error_t * vlib_pci_disable_msix_irq(vlib_pci_dev_handle_t h, u16 start, u16 count)
static clib_error_t * linux_pci_uio_error_ready(clib_file_t *uf)
vlib_pci_addr_t * vlib_pci_get_all_dev_addrs()
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
clib_error_t * clib_sysfs_write(char *file_name, char *fmt,...)
clib_error_t * vlib_pci_map_region_fixed(vlib_pci_dev_handle_t h, u32 resource, u8 *addr, void **result)
static char * sysfs_mod_vfio_noiommu
clib_error_t * vlib_pci_register_msix_handler(vlib_pci_dev_handle_t h, u32 start, u32 count, pci_msix_handler_function_t *msix_handler)
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
clib_file_function_t * read_function
linux_pci_main_t linux_pci_main
static void pci_config_type1_little_to_host(pci_config_type1_regs_t *r)
#define VLIB_INIT_FUNCTION(x)
static clib_error_t * linux_pci_vfio_intx_read_ready(clib_file_t *uf)
clib_error_t * vlib_pci_bind_to_uio(vlib_pci_addr_t *addr, char *uio_drv_name)
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
#define clib_error_return(e, args...)
clib_file_main_t file_main
clib_error_t * vlib_pci_device_open(vlib_pci_addr_t *addr, pci_device_id_t ids[], vlib_pci_dev_handle_t *handle)
struct _pci_device_registration pci_device_registration_t
linux_pci_irq_t * msix_irqs
void init_device_from_registered(vlib_pci_device_info_t *di)
#define vlib_call_init_function(vm, x)
pci_config_type1_regs_t config1
vlib_pci_dev_handle_t handle
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
clib_error_t * linux_pci_init(vlib_main_t *vm)
static clib_error_t * scan_pci_addr(void *arg, u8 *dev_dir_name, u8 *ignored)
u32 vlib_pci_dev_handle_t
#define clib_error_return_unix(e, args...)
#define pool_put(P, E)
Free an object E in pool P.
static const char * sysfs_pci_drv_path
static clib_error_t * linux_pci_vfio_error_ready(clib_file_t *uf)
clib_error_t * clib_sysfs_read(char *file_name, char *fmt,...)
static clib_error_t * linux_pci_uio_read_ready(clib_file_t *uf)
static clib_error_t * add_device_uio(linux_pci_device_t *p, vlib_pci_device_info_t *di, pci_device_registration_t *r)
static void pci_config_type0_little_to_host(pci_config_type0_regs_t *r)
void( pci_intx_handler_function_t)(vlib_pci_dev_handle_t handle)
#define vec_free(V)
Free vector's memory (no header).
clib_error_t * pci_bus_init(vlib_main_t *vm)
#define clib_memcpy(a, b, c)
static linux_pci_device_t * linux_pci_get_device(vlib_pci_dev_handle_t h)
static clib_error_t * linux_pci_vfio_msix_read_ready(clib_file_t *uf)
pci_msix_handler_function_t * msix_handler
#define VLIB_PCI_DEVICE_INFO_F_NOIOMMU
void vlib_pci_device_close(vlib_pci_dev_handle_t h)
static clib_error_t * vlib_pci_map_region_int(vlib_pci_dev_handle_t h, u32 bar, u8 *addr, void **result)
void( pci_msix_handler_function_t)(vlib_pci_dev_handle_t handle, u16 line)
static uword clib_file_add(clib_file_main_t *um, clib_file_t *template)
static void clib_file_del_by_index(clib_file_main_t *um, uword index)
pci_config_type0_regs_t config0
#define clib_error_report(e)
static void * clib_mem_alloc(uword size)
template key/value backing page structure
static clib_error_t * vfio_set_irqs(linux_pci_device_t *p, u32 index, u32 start, u32 count, u32 flags, int *efds)
static int pci_addr_cmp(void *v1, void *v2)
clib_error_t * foreach_directory_file(char *dir_name, clib_error_t *(*f)(void *arg, u8 *path_name, u8 *file_name), void *arg, int scan_dirs)
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
static int directory_exists(char *path)
uword vlib_pci_get_private_data(vlib_pci_dev_handle_t h)
#define vec_sort_with_function(vec, f)
Sort a vector using the supplied element comparison function.
#define clib_unix_warning(format, args...)
void vlib_pci_set_private_data(vlib_pci_dev_handle_t h, uword private_data)
static clib_error_t * linux_pci_vfio_unmask_intx(linux_pci_device_t *d)
#define clib_error_free(e)
clib_error_t * vlib_pci_map_region(vlib_pci_dev_handle_t h, u32 resource, void **result)
clib_file_function_t * error_function
linux_pci_region_t * regions
pci_intx_handler_function_t * intx_handler
clib_error_t * vlib_pci_enable_msix_irq(vlib_pci_dev_handle_t h, u16 start, u16 count)
static clib_error_t * vlib_pci_intr_enable(vlib_pci_dev_handle_t h)
#define vec_foreach(var, vec)
Vector iterator.
static const char * sysfs_pci_dev_path
static void vlib_pci_free_device_info(vlib_pci_device_info_t *di)
#define vec_validate_init_empty(V, I, INIT)
Make sure vector is long enough for given index and initialize empty space (no header, unspecified alignment)
linux_pci_device_t * linux_pci_devices
pci_config_header_t header
static clib_error_t * scan_uio_dir(void *arg, u8 *path_name, u8 *file_name)
u8 * clib_sysfs_link_to_name(char *link)
linux_vfio_main_t vfio_main
clib_error_t * vlib_pci_read_write_config(vlib_pci_dev_handle_t h, vlib_read_or_write_t read_or_write, uword address, void *data, u32 n_bytes)