46 #include <sys/types.h> 50 #include <sys/ioctl.h> 52 #include <linux/ethtool.h> 53 #include <linux/sockios.h> 92 char *uio_driver_name);
100 u8 *s = 0, *driver_name = 0;
103 int fd, clear_driver_override = 0;
104 u8 *dev_dir_name =
format (0,
"/sys/bus/pci/devices/%U",
107 s =
format (s,
"%v/driver%c", dev_dir_name, 0);
112 ((strcmp (
"vfio-pci", (
char *) driver_name) == 0) ||
113 (strcmp (
"uio_pci_generic", (
char *) driver_name) == 0) ||
114 (strcmp (
"igb_uio", (
char *) driver_name) == 0)))
119 dir = opendir (
"/sys/class/net");
125 "read /sys/class/net",
130 fd = socket (PF_INET, SOCK_DGRAM, 0);
137 while ((e = readdir (dir)))
140 struct ethtool_drvinfo drvinfo;
142 if (e->d_name[0] ==
'.')
145 memset (&ifr, 0,
sizeof ifr);
146 memset (&drvinfo, 0,
sizeof drvinfo);
147 ifr.ifr_data = (
char *) &drvinfo;
148 strncpy (ifr.ifr_name, e->d_name, IFNAMSIZ - 1);
149 drvinfo.cmd = ETHTOOL_GDRVINFO;
150 if (ioctl (fd, SIOCETHTOOL, &ifr) < 0)
153 if ((errno != ENOTSUP) && (errno != ENODEV))
159 if (strcmp ((
char *) s, drvinfo.bus_info))
162 memset (&ifr, 0,
sizeof (ifr));
163 strncpy (ifr.ifr_name, e->d_name, IFNAMSIZ - 1);
164 if (ioctl (fd, SIOCGIFFLAGS, &ifr) < 0)
172 if (ifr.ifr_flags & IFF_UP)
175 "interface %s is up",
186 s =
format (s,
"%v/driver/unbind%c", dev_dir_name, 0);
190 s =
format (s,
"%v/driver_override%c", dev_dir_name, 0);
191 if (access ((
char *) s, F_OK) == 0)
194 clear_driver_override = 1;
199 s =
format (s,
"/sys/bus/pci/drivers/%s/new_id%c", uio_driver_name, 0);
205 s =
format (s,
"/sys/bus/pci/drivers/%s/bind%c", uio_driver_name, 0);
209 if (clear_driver_override)
211 s =
format (s,
"%v/driver_override%c", dev_dir_name, 0);
245 int __attribute__ ((unused)) rv;
291 l->
uio_fd = open (uio_name, O_RDWR);
301 template.file_descriptor = l->
uio_fd;
303 template.private_data = dev - pm->
pci_devs;
328 uword address,
void *data,
u32 n_bytes)
337 n = pread (p->
config_fd, data, n_bytes, address);
339 n = pwrite (p->
config_fd, data, n_bytes, address);
355 struct stat stat_buf;
359 int flags = MAP_SHARED;
365 fd = open ((
char *) file_name, O_RDWR);
372 if (fstat (fd, &stat_buf) < 0)
383 *result = mmap (addr,
385 PROT_READ | PROT_WRITE, flags,
388 if (*result == (
void *) -1)
417 (dev->
os_handle, resource, addr, result));
437 for (
i = 0; r->supported_devices[
i].vendor_id != 0;
i++)
457 for (i = r->supported_devices; i->
vendor_id != 0; i++)
469 return r->init_function (vm, dev);
471 r = r->next_registration;
497 f =
format (0,
"%v/config%c", dev_dir_name, 0);
498 fd = open ((
char *) f, O_RDWR);
502 fd = open ((
char *) f, O_RDONLY);
528 memset (&all_ones, ~0,
sizeof (all_ones));
530 if (!memcmp (&dev->
config0.
header, &all_ones, sizeof (all_ones)))
551 if (!
unformat (&input,
"/sys/bus/pci/devices/%U",
567 f =
format (f,
"%v/vpd%c", dev_dir_name, 0);
568 fd = open ((
char *) f, O_RDONLY);
577 if (read (fd, &tag, 3) != 3)
580 if (tag[0] != 0x82 && tag[0] != 0x90 && tag[0] != 0x91)
583 len = (tag[2] << 8) | tag[1];
586 if (read (fd, data, len) != len)
593 else if (tag[0] == 0x90)
595 else if (tag[0] == 0x91)
605 f =
format (f,
"%v/numa_node%c", dev_dir_name, 0);
609 f =
format (f,
"%v/class%c", dev_dir_name, 0);
614 f =
format (f,
"%v/vendor%c", dev_dir_name, 0);
619 f =
format (f,
"%v/device%c", dev_dir_name, 0);
626 f =
format (f,
"%v/driver%c", dev_dir_name, 0);
645 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) ...
void vlib_pci_free_device(vlib_pci_device_t *dev)
u8 * format_vlib_pci_addr(u8 *s, va_list *va)
#define hash_set(h, key, value)
sll srl srl sll sra u16x4 i
static clib_error_t * init_device_from_registered(vlib_main_t *vm, vlib_pci_device_t *dev, linux_pci_device_t *pdev)
uword unformat_vlib_pci_addr(unformat_input_t *input, va_list *args)
pci_config_type1_regs_t config1
pci_device_registration_t * pci_device_registrations
static clib_error_t * linux_pci_uio_error_ready(clib_file_t *uf)
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
vlib_pci_addr_t bus_address
clib_error_t * clib_sysfs_write(char *file_name, char *fmt,...)
static clib_error_t * init_device(vlib_main_t *vm, vlib_pci_device_t *dev, linux_pci_device_t *pdev)
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
#define clib_unix_error(format, args...)
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)
uword * pci_dev_index_by_pci_addr
#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)
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
clib_error_t * linux_pci_init(vlib_main_t *vm)
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 * clib_sysfs_read(char *file_name, char *fmt,...)
static clib_error_t * scan_device(void *arg, u8 *dev_dir_name, u8 *ignored)
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)
#define vec_free(V)
Free vector's memory (no header).
clib_error_t * pci_bus_init(vlib_main_t *vm)
pci_config_type0_regs_t config0
#define hash_create(elts, value_bytes)
#define clib_elf_section_data_next(a, extra)
clib_error_t * vlib_pci_map_resource(vlib_pci_device_t *dev, u32 resource, void **result)
clib_error_t * vlib_pci_map_resource_fixed(vlib_pci_device_t *dev, u32 resource, u8 *addr, void **result)
static void add_device(vlib_pci_device_t *dev, linux_pci_device_t *pdev)
static uword clib_file_add(clib_file_main_t *um, clib_file_t *template)
#define clib_error_report(e)
clib_error_t * vlib_pci_bind_to_uio(vlib_pci_device_t *d, char *uio_driver_name)
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...)
linux_pci_device_t * linux_pci_devices
pci_config_header_t header
static void linux_pci_device_free(linux_pci_device_t *l)
static clib_error_t * scan_uio_dir(void *arg, u8 *path_name, u8 *file_name)
pci_device_registration_t * pci_device_next_registered(pci_device_registration_t *r)
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)
u8 * clib_sysfs_link_to_name(char *link)
static clib_error_t * os_map_pci_resource_internal(uword os_handle, u32 resource, u8 *addr, void **result)