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) = @_;
21 my $id = "$bus:$slot";
22 return if defined($id_filter) && $id_filter ne $id;
24 push @{$devices->{$id}}, { id
=> $id, function
=> $function };
27 # Entries should be sorted by functions.
28 foreach my $id (keys %$devices) {
29 my $dev = $devices->{$id};
30 $devices->{$id} = [ sort { $a->{function
} <=> $b->{function
} } @$dev ];
36 sub check_iommu_support
{
37 # we have IOMMU support if /sys/class/iommu/ is populated
38 return PVE
::Tools
::dir_glob_regex
('/sys/class/iommu/', "[^\.].*");
42 my ($filename, $buf) = @_;
44 my $fh = IO
::File-
>new($filename, "w");
47 my $res = print $fh $buf;
59 return undef if $name !~ m/^${pciregex}$/;
60 my ($domain, $bus, $slot, $func) = ($1, $2, $3, $4);
62 my $irq = file_read_firstline
("$pcisysfs/devices/$name/irq");
63 return undef if !defined($irq) || $irq !~ m/^\d+$/;
65 my $vendor = file_read_firstline
("$pcisysfs/devices/$name/vendor");
66 return undef if !defined($vendor) || $vendor !~ s/^0x//;
68 my $product = file_read_firstline
("$pcisysfs/devices/$name/device");
69 return undef if !defined($product) || $product !~ s/^0x//;
80 has_fl_reset
=> -f
"$pcisysfs/devices/$name/reset" || 0,
89 my $name = $dev->{name
};
91 my $fn = "$pcisysfs/devices/$name/reset";
93 return file_write
($fn, "1");
96 sub pci_dev_bind_to_vfio
{
99 my $name = $dev->{name
};
101 my $vfio_basedir = "$pcisysfs/drivers/vfio-pci";
103 if (!-d
$vfio_basedir) {
104 system("/sbin/modprobe vfio-pci >/dev/null 2>/dev/null");
106 die "Cannot find vfio-pci module!\n" if !-d
$vfio_basedir;
108 my $testdir = "$vfio_basedir/$name";
109 return 1 if -d
$testdir;
111 my $data = "$dev->{vendor} $dev->{product}";
112 return undef if !file_write
("$vfio_basedir/new_id", $data);
114 my $fn = "$pcisysfs/devices/$name/driver/unbind";
115 if (!file_write
($fn, $name)) {
116 return undef if -f
$fn;
119 $fn = "$vfio_basedir/bind";
121 return undef if !file_write
($fn, $name);
127 sub pci_dev_group_bind_to_vfio
{
130 my $vfio_basedir = "$pcisysfs/drivers/vfio-pci";
132 if (!-d
$vfio_basedir) {
133 system("/sbin/modprobe vfio-pci >/dev/null 2>/dev/null");
135 die "Cannot find vfio-pci module!\n" if !-d
$vfio_basedir;
137 # get IOMMU group devices
138 opendir(my $D, "$pcisysfs/devices/0000:$pciid/iommu_group/devices/") || die "Cannot open iommu_group: $!\n";
139 my @devs = grep /^0000:/, readdir($D);
142 foreach my $pciid (@devs) {
143 $pciid =~ m/^([:\.\da-f]+)$/ or die "PCI ID $pciid not valid!\n";
145 # pci bridges, switches or root ports are not supported
146 # they have a pci_bus subdirectory so skip them
147 next if (-e
"$pcisysfs/devices/$pciid/pci_bus");
149 my $info = pci_device_info
($1);
150 pci_dev_bind_to_vfio
($info) || die "Cannot bind $pciid to vfio\n";
156 # idea is from usbutils package (/usr/bin/usb-devices) script
157 sub __scan_usb_device
{
158 my ($res, $devpath, $parent, $level) = @_;
160 return if ! -d
$devpath;
161 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
162 my $port = $level ?
int($1 - 1) : 0;
164 my $busnum = int(file_read_firstline
("$devpath/busnum"));
165 my $devnum = int(file_read_firstline
("$devpath/devnum"));
172 speed
=> file_read_firstline
("$devpath/speed"),
173 class => hex(file_read_firstline
("$devpath/bDeviceClass")),
174 vendid
=> file_read_firstline
("$devpath/idVendor"),
175 prodid
=> file_read_firstline
("$devpath/idProduct"),
179 my $usbpath = $devpath;
180 $usbpath =~ s
|^.*/\d
+\
-||;
181 $d->{usbpath
} = $usbpath;
184 my $product = file_read_firstline
("$devpath/product");
185 $d->{product
} = $product if $product;
187 my $manu = file_read_firstline
("$devpath/manufacturer");
188 $d->{manufacturer
} = $manu if $manu;
190 my $serial => file_read_firstline
("$devpath/serial");
191 $d->{serial
} = $serial if $serial;
195 foreach my $subdev (<$devpath/$busnum-*>) {
196 next if $subdev !~ m
|/$busnum-[0-9]+(\
.[0-9]+)*$|;
197 __scan_usb_device
($res, $subdev, $devnum, $level + 1);
206 foreach my $device (</sys/bus
/usb
/devices
/usb
*>) {
207 __scan_usb_device
($devlist, $device, 0, 0);