46 #include <sys/types.h> 50 #include <sys/ioctl.h> 52 #include <linux/ethtool.h> 53 #include <linux/sockios.h> 54 #include <linux/vfio.h> 55 #include <sys/eventfd.h> 60 "/sys/module/vfio/parameters/enable_unsafe_noiommu_mode";
167 di->
addr.as_u32 = addr->as_u32;
172 f =
format (0,
"%v/config%c", dev_dir_name, 0);
173 fd = open ((
char *) f, O_RDWR);
177 fd = open ((
char *) f, O_RDONLY);
200 memset (&all_ones, ~0,
sizeof (all_ones));
202 if (!memcmp (&di->
config0.
header, &all_ones, sizeof (all_ones)))
217 f =
format (f,
"%v/numa_node%c", dev_dir_name, 0);
226 f =
format (f,
"%v/class%c", dev_dir_name, 0);
233 f =
format (f,
"%v/vendor%c", dev_dir_name, 0);
240 f =
format (f,
"%v/device%c", dev_dir_name, 0);
247 f =
format (f,
"%v/driver%c", dev_dir_name, 0);
255 f =
format (f,
"%v/iommu_group%c", dev_dir_name, 0);
267 f =
format (f,
"%v/vpd%c", dev_dir_name, 0);
268 fd = open ((
char *) f, O_RDONLY);
277 if (read (fd, &tag, 3) != 3)
280 if (tag[0] != 0x82 && tag[0] != 0x90 && tag[0] != 0x91)
283 len = (tag[2] << 8) | tag[1];
286 if (read (fd, data, len) != len)
293 else if (tag[0] == 0x90)
295 else if (tag[0] == 0x91)
322 struct stat s = { 0 };
323 if (stat (path, &s) == -1)
326 return S_ISDIR (s.st_mode);
333 u8 *s = 0, *driver_name = 0;
337 int fd, clear_driver_override = 0;
346 if (strncmp (
"auto", uio_drv_name, 5) == 0)
348 int vfio_pci_loaded = 0;
356 if (!vfio_pci_loaded)
359 "is bound to IOMMU group and " 360 "vfio-pci driver is not loaded",
365 uio_drv_name =
"vfio-pci";
370 if (vfio_pci_loaded &&
372 uio_drv_name =
"vfio-pci";
374 uio_drv_name =
"uio_pci_generic";
376 uio_drv_name =
"igb_uio";
381 "kernel VFIO or UIO driver",
389 s =
format (s,
"%v/driver%c", dev_dir_name, 0);
394 ((strcmp (
"vfio-pci", (
char *) driver_name) == 0) ||
395 (strcmp (
"uio_pci_generic", (
char *) driver_name) == 0) ||
396 (strcmp (
"igb_uio", (
char *) driver_name) == 0)))
401 dir = opendir (
"/sys/class/net");
407 "read /sys/class/net",
412 fd = socket (PF_INET, SOCK_DGRAM, 0);
419 while ((e = readdir (dir)))
422 struct ethtool_drvinfo drvinfo;
424 if (e->d_name[0] ==
'.')
427 memset (&ifr, 0,
sizeof ifr);
428 memset (&drvinfo, 0,
sizeof drvinfo);
429 ifr.ifr_data = (
char *) &drvinfo;
430 strncpy (ifr.ifr_name, e->d_name, IFNAMSIZ - 1);
431 drvinfo.cmd = ETHTOOL_GDRVINFO;
432 if (ioctl (fd, SIOCETHTOOL, &ifr) < 0)
435 if ((errno != ENOTSUP) && (errno != ENODEV))
441 if (strcmp ((
char *) s, drvinfo.bus_info))
444 memset (&ifr, 0,
sizeof (ifr));
445 strncpy (ifr.ifr_name, e->d_name, IFNAMSIZ - 1);
446 if (ioctl (fd, SIOCGIFFLAGS, &ifr) < 0)
454 if (ifr.ifr_flags & IFF_UP)
457 "interface %s is up",
467 s =
format (s,
"%v/driver/unbind%c", dev_dir_name, 0);
471 s =
format (s,
"%v/driver_override%c", dev_dir_name, 0);
472 if (access ((
char *) s, F_OK) == 0)
475 clear_driver_override = 1;
490 if (clear_driver_override)
492 s =
format (s,
"%v/driver_override%c", dev_dir_name, 0);
524 int __attribute__ ((unused)) rv;
543 struct vfio_irq_set i = {
544 .argsz =
sizeof (
struct vfio_irq_set),
547 .index = VFIO_PCI_INTX_IRQ_INDEX,
548 .
flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK,
570 int __attribute__ ((unused)) rv;
607 p->
config_fd = open ((
char *) s, O_RDWR);
623 p->
uio_fd = open ((
char *) s, O_RDWR);
637 err = r->init_function (vm, p->
handle);
668 struct vfio_group_status group_status;
678 s =
format (s,
"/dev/vfio/%u%c", group, 0);
679 fd = open ((
char *) s, O_RDWR);
683 group_status.argsz =
sizeof (group_status);
684 if (ioctl (fd, VFIO_GROUP_GET_STATUS, &group_status) < 0)
691 if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE))
694 "devices in this group bound to vfio-pci)",
735 struct vfio_device_info device_info;
736 struct vfio_irq_info irq_info = { 0 };
744 (strcmp (
"vfio-pci", (
char *) di->
driver_name) != 0))
755 if ((dfd = ioctl (g->
fd, VFIO_GROUP_GET_DEVICE_FD, (
char *) s)) < 0)
764 device_info.argsz =
sizeof (device_info);
765 if (ioctl (dfd, VFIO_DEVICE_GET_INFO, &device_info) < 0)
772 irq_info.argsz =
sizeof (
struct vfio_irq_info);
773 irq_info.index = VFIO_PCI_INTX_IRQ_INDEX;
774 if (ioctl (dfd, VFIO_DEVICE_GET_IRQ_INFO, &irq_info) < 0)
782 if (device_info.flags & VFIO_DEVICE_FLAGS_RESET)
783 if (ioctl (dfd, VFIO_DEVICE_RESET) < 0)
790 if ((irq_info.flags & VFIO_IRQ_INFO_EVENTFD) && irq_info.count == 1)
792 u8 buf[
sizeof (
struct vfio_irq_set) + sizeof (int)] = { 0 };
793 struct vfio_irq_set *irq_set = (
struct vfio_irq_set *) buf;
795 int efd = eventfd (0, EFD_NONBLOCK);
797 irq_set->argsz =
sizeof (buf);
799 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
801 VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
804 if (ioctl (dfd, VFIO_DEVICE_SET_IRQS, irq_set) < 0)
826 p->
config_fd = open ((
char *) s, O_RDWR);
835 err = r->init_function (vm, p->
handle);
853 uword address,
void *data,
u32 n_bytes)
859 n = pread (p->
config_fd, data, n_bytes, address);
861 n = pwrite (p->
config_fd, data, n_bytes, address);
876 struct stat stat_buf;
880 int flags = MAP_SHARED;
887 fd = open ((
char *) file_name, O_RDWR);
894 if (fstat (fd, &stat_buf) < 0)
905 *result = mmap (addr,
907 PROT_READ | PROT_WRITE, flags,
910 if (*result == (
void *) -1)
956 for (i = r->supported_devices; i->
vendor_id != 0; i++)
970 r = r->next_registration;
981 vlib_pci_addr_t
addr, **addrv = arg;
988 if (!
unformat (&input,
"/sys/bus/pci/devices/%U",
1004 vlib_pci_addr_t *a1 = v1;
1005 vlib_pci_addr_t *a2 = v2;
1007 if (a1->domain > a2->domain)
1009 if (a1->domain < a2->domain)
1011 if (a1->bus > a2->bus)
1013 if (a1->bus < a2->bus)
1015 if (a1->slot > a2->slot)
1017 if (a1->slot < a2->slot)
1019 if (a1->function > a2->function)
1021 if (a1->function < a2->function)
1029 vlib_pci_addr_t *addrs = 0;
1049 vlib_pci_addr_t *
addr = 0, *addrs;
1058 ASSERT (
sizeof (vlib_pci_addr_t) ==
sizeof (
u32));
1060 fd = open (
"/dev/vfio/vfio", O_RDWR);
1062 if ((fd != -1) && (ioctl (fd, VFIO_GET_API_VERSION) != VFIO_API_VERSION))
1068 if ((fd != -1) && (ioctl (fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU) == 0))
#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)
linux_pci_vfio_iommu_group_t * iommu_groups
#define hash_set(h, key, value)
sll srl srl sll sra u16x4 i
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)
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
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,...)
static char * sysfs_mod_vfio_noiommu
#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)
clib_error_t * vlib_pci_bind_to_uio(vlib_pci_addr_t *addr, char *uio_drv_name)
#define clib_error_return(e, args...)
clib_file_main_t file_main
struct _pci_device_registration pci_device_registration_t
#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,...)
void(* interrupt_handler)(vlib_pci_dev_handle_t h)
static clib_error_t * linux_pci_uio_read_ready(clib_file_t *uf)
static void pci_config_type0_little_to_host(pci_config_type0_regs_t *r)
static clib_error_t * add_device_vfio(vlib_main_t *vm, linux_pci_device_t *p, vlib_pci_device_info_t *di, pci_device_registration_t *r)
static clib_error_t * open_vfio_iommu_group(int group)
#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)
#define hash_create(elts, value_bytes)
linux_pci_device_t * linux_pci_get_device(vlib_pci_dev_handle_t h)
static uword clib_file_add(clib_file_main_t *um, clib_file_t *template)
pci_config_type0_regs_t config0
#define clib_error_report(e)
static void * clib_mem_alloc(uword size)
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_file_function_t * error_function
static linux_pci_vfio_iommu_group_t * get_vfio_iommu_group(int group)
static clib_error_t * vlib_pci_intr_enable(vlib_pci_dev_handle_t h)
static clib_error_t * add_device_uio(vlib_main_t *vm, linux_pci_device_t *p, vlib_pci_device_info_t *di, pci_device_registration_t *r)
#define vec_foreach(var, vec)
Vector iterator.
static const char * sysfs_pci_dev_path
static clib_error_t * linux_pci_vfio_read_ready(clib_file_t *uf)
static void vlib_pci_free_device_info(vlib_pci_device_info_t *di)
void init_device_from_registered(vlib_main_t *vm, vlib_pci_device_info_t *di)
clib_error_t * vlib_pci_map_resource_fixed(vlib_pci_dev_handle_t h, u32 resource, u8 *addr, void **result)
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)
static clib_error_t * vlib_pci_map_resource_int(vlib_pci_dev_handle_t h, u32 resource, u8 *addr, void **result)
u8 * clib_sysfs_link_to_name(char *link)
uword * iommu_pool_index_by_group
clib_error_t * vlib_pci_map_resource(vlib_pci_dev_handle_t h, u32 resource, void **result)
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)