1 package PVE
::SysFSTools
;
8 use PVE
::Tools
qw(file_read_firstline dir_glob_foreach);
10 my $pcisysfs = "/sys/bus/pci";
11 my $pciregex = "([a-f0-9]{4}):([a-f0-9]{2}):([a-f0-9]{2})\.([a-f0-9])";
18 dir_glob_foreach
("$pcisysfs/devices", $pciregex, sub {
19 my (undef, undef, $bus, $slot, $function) = @_;
20 my $id = "$bus:$slot";
21 return if defined($filter) && $id ne $filter;
22 my $res = { id
=> $id, function
=> $function};
23 push @{$devices->{$id}}, $res;
26 # Entries should be sorted by functions.
27 foreach my $id (keys %$devices) {
28 my $dev = $devices->{$id};
29 $devices->{$id} = [ sort { $a->{function
} <=> $b->{function
} } @$dev ];
35 sub check_iommu_support
{
36 # we have IOMMU support if /sys/class/iommu/ is populated
37 return PVE
::Tools
::dir_glob_regex
('/sys/class/iommu/', "[^\.].*");
41 my ($filename, $buf) = @_;
43 my $fh = IO
::File-
>new($filename, "w");
46 my $res = print $fh $buf;
58 return undef if $name !~ m/^${pciregex}$/;
59 my ($domain, $bus, $slot, $func) = ($1, $2, $3, $4);
61 my $irq = file_read_firstline
("$pcisysfs/devices/$name/irq");
62 return undef if !defined($irq) || $irq !~ m/^\d+$/;
64 my $vendor = file_read_firstline
("$pcisysfs/devices/$name/vendor");
65 return undef if !defined($vendor) || $vendor !~ s/^0x//;
67 my $product = file_read_firstline
("$pcisysfs/devices/$name/device");
68 return undef if !defined($product) || $product !~ s/^0x//;
79 has_fl_reset
=> -f
"$pcisysfs/devices/$name/reset" || 0,
88 my $name = $dev->{name
};
90 my $fn = "$pcisysfs/devices/$name/reset";
92 return file_write
($fn, "1");
95 sub pci_dev_bind_to_vfio
{
98 my $name = $dev->{name
};
100 my $vfio_basedir = "$pcisysfs/drivers/vfio-pci";
102 if (!-d
$vfio_basedir) {
103 system("/sbin/modprobe vfio-pci >/dev/null 2>/dev/null");
105 die "Cannot find vfio-pci module!\n" if !-d
$vfio_basedir;
107 my $testdir = "$vfio_basedir/$name";
108 return 1 if -d
$testdir;
110 my $data = "$dev->{vendor} $dev->{product}";
111 return undef if !file_write
("$vfio_basedir/new_id", $data);
113 my $fn = "$pcisysfs/devices/$name/driver/unbind";
114 if (!file_write
($fn, $name)) {
115 return undef if -f
$fn;
118 $fn = "$vfio_basedir/bind";
120 return undef if !file_write
($fn, $name);
126 sub pci_dev_group_bind_to_vfio
{
129 my $vfio_basedir = "$pcisysfs/drivers/vfio-pci";
131 if (!-d
$vfio_basedir) {
132 system("/sbin/modprobe vfio-pci >/dev/null 2>/dev/null");
134 die "Cannot find vfio-pci module!\n" if !-d
$vfio_basedir;
136 # get IOMMU group devices
137 opendir(my $D, "$pcisysfs/devices/0000:$pciid/iommu_group/devices/") || die "Cannot open iommu_group: $!\n";
138 my @devs = grep /^0000:/, readdir($D);
141 foreach my $pciid (@devs) {
142 $pciid =~ m/^([:\.\da-f]+)$/ or die "PCI ID $pciid not valid!\n";
144 # pci bridges, switches or root ports are not supported
145 # they have a pci_bus subdirectory so skip them
146 next if (-e
"$pcisysfs/devices/$pciid/pci_bus");
148 my $info = pci_device_info
($1);
149 pci_dev_bind_to_vfio
($info) || die "Cannot bind $pciid to vfio\n";
155 # idea is from usbutils package (/usr/bin/usb-devices) script
156 sub __scan_usb_device
{
157 my ($res, $devpath, $parent, $level) = @_;
159 return if ! -d
$devpath;
160 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
161 my $port = $level ?
int($1 - 1) : 0;
163 my $busnum = int(file_read_firstline
("$devpath/busnum"));
164 my $devnum = int(file_read_firstline
("$devpath/devnum"));
171 speed
=> file_read_firstline
("$devpath/speed"),
172 class => hex(file_read_firstline
("$devpath/bDeviceClass")),
173 vendid
=> file_read_firstline
("$devpath/idVendor"),
174 prodid
=> file_read_firstline
("$devpath/idProduct"),
178 my $usbpath = $devpath;
179 $usbpath =~ s
|^.*/\d
+\
-||;
180 $d->{usbpath
} = $usbpath;
183 my $product = file_read_firstline
("$devpath/product");
184 $d->{product
} = $product if $product;
186 my $manu = file_read_firstline
("$devpath/manufacturer");
187 $d->{manufacturer
} = $manu if $manu;
189 my $serial => file_read_firstline
("$devpath/serial");
190 $d->{serial
} = $serial if $serial;
194 foreach my $subdev (<$devpath/$busnum-*>) {
195 next if $subdev !~ m
|/$busnum-[0-9]+(\
.[0-9]+)*$|;
196 __scan_usb_device
($res, $subdev, $devnum, $level + 1);
205 foreach my $device (</sys/bus
/usb
/devices
/usb
*>) {
206 __scan_usb_device
($devlist, $device, 0, 0);