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, sizeof (ifr.ifr_name));
446 ifr.ifr_name[
ARRAY_LEN (ifr.ifr_name) - 1] =
'\0';
447 drvinfo.cmd = ETHTOOL_GDRVINFO;
448 if (ioctl (fd, SIOCETHTOOL, &ifr) < 0)
451 if ((errno != ENOTSUP) && (errno != ENODEV))
457 if (strcmp ((
char *) s, drvinfo.bus_info))
460 memset (&ifr, 0,
sizeof (ifr));
461 strncpy (ifr.ifr_name, e->d_name, sizeof (ifr.ifr_name));
462 ifr.ifr_name[
ARRAY_LEN (ifr.ifr_name) - 1] =
'\0';
463 if (ioctl (fd, SIOCGIFFLAGS, &ifr) < 0)
471 if (ifr.ifr_flags & IFF_UP)
474 "interface %s is up",
484 s =
format (s,
"%v/driver/unbind%c", dev_dir_name, 0);
488 s =
format (s,
"%v/driver_override%c", dev_dir_name, 0);
489 if (access ((
char *) s, F_OK) == 0)
492 clear_driver_override = 1;
507 if (clear_driver_override)
509 s =
format (s,
"%v/driver_override%c", dev_dir_name, 0);
542 int data_len = efds ? count *
sizeof (int) : 0;
543 u8 buf[
sizeof (
struct vfio_irq_set) + data_len];
544 struct vfio_irq_info irq_info = { 0 };
545 struct vfio_irq_set *irq_set = (
struct vfio_irq_set *) buf;
548 irq_info.argsz =
sizeof (
struct vfio_irq_info);
549 irq_info.index = index;
551 if (ioctl (p->
fd, VFIO_DEVICE_GET_IRQ_INFO, &irq_info) < 0)
555 if (irq_info.count < start + count)
562 flags |= VFIO_IRQ_SET_DATA_EVENTFD;
566 flags |= VFIO_IRQ_SET_DATA_NONE;
568 ASSERT ((flags & (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD)) !=
569 (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD));
571 irq_set->argsz =
sizeof (
struct vfio_irq_set) + data_len;
572 irq_set->index = index;
573 irq_set->start = start;
574 irq_set->count =
count;
575 irq_set->flags =
flags;
577 if (ioctl (p->
fd, VFIO_DEVICE_SET_IRQS, irq_set) < 0)
579 "[index = %u, start = %u, count = %u, " 582 index, start, count, flags);
589 int __attribute__ ((unused)) rv;
609 VFIO_IRQ_SET_ACTION_UNMASK, 0);
623 int __attribute__ ((unused)) rv;
641 int __attribute__ ((unused)) rv;
680 p->
config_fd = open ((
char *) s, O_RDWR);
697 p->
fd = open ((
char *) s, O_RDWR);
704 if (r && r->interrupt_handler)
707 if (r && r->init_function)
733 struct vfio_irq_info irq_info = { 0 };
734 irq_info.argsz =
sizeof (
struct vfio_irq_info);
735 irq_info.index = VFIO_PCI_INTX_IRQ_INDEX;
736 if (ioctl (p->
fd, VFIO_DEVICE_GET_IRQ_INFO, &irq_info) < 0)
739 if (irq_info.count != 1)
743 irq->
fd = eventfd (0, EFD_NONBLOCK);
782 for (i = start; i < start +
count; i++)
788 irq->
fd = eventfd (0, EFD_NONBLOCK);
832 for (i = start; i < start +
count; i++)
838 return vfio_set_irqs (p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
839 VFIO_IRQ_SET_ACTION_TRIGGER, fds);
852 for (i = start; i < start +
count; i++)
855 return vfio_set_irqs (p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
856 VFIO_IRQ_SET_ACTION_TRIGGER, fds);
864 struct vfio_device_info device_info = { 0 };
865 struct vfio_region_info reg = { 0 };
873 (strcmp (
"vfio-pci", (
char *) di->
driver_name) != 0))
881 device_info.argsz =
sizeof (device_info);
882 if (ioctl (p->
fd, VFIO_DEVICE_GET_INFO, &device_info) < 0)
889 reg.argsz =
sizeof (
struct vfio_region_info);
890 reg.index = VFIO_PCI_CONFIG_REGION_INDEX;
891 if (ioctl (p->
fd, VFIO_DEVICE_GET_REGION_INFO, ®) < 0)
901 if (device_info.flags & VFIO_DEVICE_FLAGS_RESET)
902 if (ioctl (p->
fd, VFIO_DEVICE_RESET) < 0)
909 if (r && r->interrupt_handler)
915 if (r && r->init_function)
960 int flags = MAP_SHARED;
970 struct stat stat_buf;
974 fd = open ((
char *) file_name, O_RDWR);
982 if (fstat (fd, &stat_buf) < 0)
993 size = stat_buf.st_size;
998 struct vfio_region_info reg = { 0 };
999 reg.argsz =
sizeof (
struct vfio_region_info);
1001 if (ioctl (p->
fd, VFIO_DEVICE_GET_REGION_INFO, ®) < 0)
1012 *result = mmap (addr, size, PROT_READ | PROT_WRITE, flags, fd,
offset);
1013 if (*result == (
void *) -1)
1112 VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1122 VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1172 for (i = r->supported_devices; i->
vendor_id != 0; i++)
1185 r = r->next_registration;
1196 vlib_pci_addr_t
addr, **addrv = arg;
1203 if (!
unformat (&input,
"/sys/bus/pci/devices/%U",
1219 vlib_pci_addr_t *a1 = v1;
1220 vlib_pci_addr_t *a2 = v2;
1222 if (a1->domain > a2->domain)
1224 if (a1->domain < a2->domain)
1226 if (a1->bus > a2->bus)
1228 if (a1->bus < a2->bus)
1230 if (a1->slot > a2->slot)
1232 if (a1->slot < a2->slot)
1234 if (a1->function > a2->function)
1236 if (a1->function < a2->function)
1244 vlib_pci_addr_t *addrs = 0;
1263 vlib_pci_addr_t *
addr = 0, *addrs;
1271 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
memset(h->entries, 0, sizeof(h->entries[0])*entries)
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)