44 #include <sys/types.h> 48 #include <sys/ioctl.h> 50 #include <linux/ethtool.h> 51 #include <linux/sockios.h> 90 char *uio_driver_name);
98 u8 *s = 0, *driver_name = 0;
101 int fd, clear_driver_override = 0;
102 u8 *dev_dir_name =
format (0,
"/sys/bus/pci/devices/%U",
105 s =
format (s,
"%v/driver%c", dev_dir_name, 0);
110 ((strcmp (
"vfio-pci", (
char *) driver_name) == 0) ||
111 (strcmp (
"uio_pci_generic", (
char *) driver_name) == 0) ||
112 (strcmp (
"igb_uio", (
char *) driver_name) == 0)))
117 dir = opendir (
"/sys/class/net");
123 "read /sys/class/net",
128 fd = socket (PF_INET, SOCK_DGRAM, 0);
135 while ((e = readdir (dir)))
138 struct ethtool_drvinfo drvinfo;
140 if (e->d_name[0] ==
'.')
143 memset (&ifr, 0,
sizeof ifr);
144 memset (&drvinfo, 0,
sizeof drvinfo);
145 ifr.ifr_data = (
char *) &drvinfo;
146 strncpy (ifr.ifr_name, e->d_name, IFNAMSIZ - 1);
147 drvinfo.cmd = ETHTOOL_GDRVINFO;
148 if (ioctl (fd, SIOCETHTOOL, &ifr) < 0)
151 if ((errno != ENOTSUP) && (errno != ENODEV))
157 if (strcmp ((
char *) s, drvinfo.bus_info))
160 memset (&ifr, 0,
sizeof (ifr));
161 strncpy (ifr.ifr_name, e->d_name, IFNAMSIZ - 1);
162 if (ioctl (fd, SIOCGIFFLAGS, &ifr) < 0)
170 if (ifr.ifr_flags & IFF_UP)
173 "interface %s is up",
184 s =
format (s,
"%v/driver/unbind%c", dev_dir_name, 0);
188 s =
format (s,
"%v/driver_override%c", dev_dir_name, 0);
189 if (access ((
char *) s, F_OK) == 0)
192 clear_driver_override = 1;
197 s =
format (s,
"/sys/bus/pci/drivers/%s/new_id%c", uio_driver_name, 0);
203 s =
format (s,
"/sys/bus/pci/drivers/%s/bind%c", uio_driver_name, 0);
207 if (clear_driver_override)
209 s =
format (s,
"%v/driver_override%c", dev_dir_name, 0);
243 int __attribute__ ((unused)) rv;
289 l->
uio_fd = open (uio_name, O_RDWR);
300 template.file_descriptor = l->
uio_fd;
302 template.private_data = dev - pm->
pci_devs;
327 uword address,
void *data,
u32 n_bytes)
336 n = pread (p->
config_fd, data, n_bytes, address);
338 n = pwrite (p->
config_fd, data, n_bytes, address);
354 struct stat stat_buf;
358 int flags = MAP_SHARED;
364 fd = open ((
char *) file_name, O_RDWR);
371 if (fstat (fd, &stat_buf) < 0)
382 *result = mmap (addr,
384 PROT_READ | PROT_WRITE, flags,
387 if (*result == (
void *) -1)
416 (dev->
os_handle, resource, addr, result));
436 for (
i = 0; r->supported_devices[
i].vendor_id != 0;
i++)
456 for (i = r->supported_devices; i->
vendor_id != 0; i++)
468 return r->init_function (vm, dev);
470 r = r->next_registration;
496 f =
format (0,
"%v/config%c", dev_dir_name, 0);
497 fd = open ((
char *) f, O_RDWR);
501 fd = open ((
char *) f, O_RDONLY);
527 memset (&all_ones, ~0,
sizeof (all_ones));
529 if (!memcmp (&dev->
config0.
header, &all_ones, sizeof (all_ones)))
550 if (!
unformat (&input,
"/sys/bus/pci/devices/%U",
566 f =
format (f,
"%v/vpd%c", dev_dir_name, 0);
567 fd = open ((
char *) f, O_RDONLY);
576 if (read (fd, &tag, 3) != 3)
579 if (tag[0] != 0x82 && tag[0] != 0x90 && tag[0] != 0x91)
582 len = (tag[2] << 8) | tag[1];
585 if (read (fd, data, len) != len)
592 else if (tag[0] == 0x90)
594 else if (tag[0] == 0x91)
604 f =
format (f,
"%v/numa_node%c", dev_dir_name, 0);
608 f =
format (f,
"%v/class%c", dev_dir_name, 0);
613 f =
format (f,
"%v/vendor%c", dev_dir_name, 0);
618 f =
format (f,
"%v/device%c", dev_dir_name, 0);
625 f =
format (f,
"%v/driver%c", dev_dir_name, 0);
644 ASSERT (
sizeof (vlib_pci_addr_t) ==
sizeof (
u32));
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
static clib_error_t * os_map_pci_resource_internal(uword os_handle, u32 resource, u8 *addr, void **result)
format_function_t format_vlib_pci_addr
#define hash_set(h, key, value)
sll srl srl sll sra u16x4 i
static void linux_pci_device_free(linux_pci_device_t *l)
static clib_error_t * init_device(vlib_main_t *vm, vlib_pci_device_t *dev, linux_pci_device_t *pdev)
pci_config_type1_regs_t config1
pci_device_registration_t * pci_device_registrations
clib_error_t * vlib_pci_bind_to_uio(vlib_pci_device_t *d, char *uio_driver_name)
unformat_function_t unformat_vlib_pci_addr
static void add_device(vlib_pci_device_t *dev, linux_pci_device_t *pdev)
u8 * vlib_sysfs_link_to_name(char *link)
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
vlib_pci_addr_t bus_address
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
#define clib_unix_error(format, args...)
static void pci_config_type1_little_to_host(pci_config_type1_regs_t *r)
#define VLIB_INIT_FUNCTION(x)
uword * pci_dev_index_by_pci_addr
clib_error_t * vlib_pci_map_resource_fixed(vlib_pci_device_t *dev, u32 resource, u8 *addr, void **result)
#define clib_error_return(e, args...)
struct _pci_device_registration pci_device_registration_t
clib_error_t * vlib_pci_read_write_config(vlib_pci_device_t *dev, vlib_read_or_write_t read_or_write, uword address, void *data, u32 n_bytes)
static clib_error_t * init_device_from_registered(vlib_main_t *vm, vlib_pci_device_t *dev, linux_pci_device_t *pdev)
#define vlib_call_init_function(vm, x)
static clib_error_t * linux_pci_uio_read_ready(unix_file_t *uf)
static uword unix_file_add(unix_main_t *um, unix_file_t *template)
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
clib_error_t * vlib_sysfs_read(char *file_name, char *fmt,...)
vlib_pci_device_t * pci_devs
#define clib_error_return_unix(e, args...)
#define pool_put(P, E)
Free an object E in pool P.
#define vec_dup(V)
Return copy of vector (no header, no alignment)
void(* interrupt_handler)(struct vlib_pci_device *dev)
static clib_error_t * vlib_pci_intr_enable(vlib_pci_device_t *dev)
clib_error_t * linux_pci_init(vlib_main_t *vm)
void vlib_pci_free_device(vlib_pci_device_t *dev)
static void pci_config_type0_little_to_host(pci_config_type0_regs_t *r)
#define vec_free(V)
Free vector's memory (no header).
pci_config_type0_regs_t config0
#define hash_create(elts, value_bytes)
#define clib_elf_section_data_next(a, extra)
static clib_error_t * scan_device(void *arg, u8 *dev_dir_name, u8 *ignored)
clib_error_t * pci_bus_init(vlib_main_t *vm)
static clib_error_t * scan_uio_dir(void *arg, u8 *path_name, u8 *file_name)
#define clib_error_report(e)
clib_error_t * vlib_pci_map_resource(vlib_pci_device_t *dev, u32 resource, void **result)
linux_pci_main_t linux_pci_main
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)
#define clib_unix_warning(format, args...)
pci_device_registration_t * pci_device_next_registered(pci_device_registration_t *r)
clib_error_t * vlib_sysfs_write(char *file_name, char *fmt,...)
static clib_error_t * linux_pci_uio_error_ready(unix_file_t *uf)
linux_pci_device_t * linux_pci_devices
pci_config_header_t header