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 ];
40 if ($id !~ m/^[0-9a-fA-f]{4}:/) {
46 my $mdev_path = "$pcisysfs/devices/$fullid/mdev_supported_types";
51 dir_glob_foreach
($mdev_path, '[^\.].*', sub {
54 my $type_path = "$mdev_path/$type";
56 my $available = int(file_read_firstline
("$type_path/available_instances"));
57 my $description = PVE
::Tools
::file_get_contents
("$type_path/description");
61 description
=> $description,
62 available
=> $available,
69 sub check_iommu_support
{
70 # we have IOMMU support if /sys/class/iommu/ is populated
71 return PVE
::Tools
::dir_glob_regex
('/sys/class/iommu/', "[^\.].*");
75 my ($filename, $buf) = @_;
77 my $fh = IO
::File-
>new($filename, "w");
80 my $res = print $fh $buf;
92 return undef if $name !~ m/^${pciregex}$/;
93 my ($domain, $bus, $slot, $func) = ($1, $2, $3, $4);
95 my $irq = file_read_firstline
("$pcisysfs/devices/$name/irq");
96 return undef if !defined($irq) || $irq !~ m/^\d+$/;
98 my $vendor = file_read_firstline
("$pcisysfs/devices/$name/vendor");
99 return undef if !defined($vendor) || $vendor !~ s/^0x//;
101 my $product = file_read_firstline
("$pcisysfs/devices/$name/device");
102 return undef if !defined($product) || $product !~ s/^0x//;
113 has_fl_reset
=> -f
"$pcisysfs/devices/$name/reset" || 0,
122 my $name = $dev->{name
};
124 my $fn = "$pcisysfs/devices/$name/reset";
126 return file_write
($fn, "1");
129 sub pci_dev_bind_to_vfio
{
132 my $name = $dev->{name
};
134 my $vfio_basedir = "$pcisysfs/drivers/vfio-pci";
136 if (!-d
$vfio_basedir) {
137 system("/sbin/modprobe vfio-pci >/dev/null 2>/dev/null");
139 die "Cannot find vfio-pci module!\n" if !-d
$vfio_basedir;
141 my $testdir = "$vfio_basedir/$name";
142 return 1 if -d
$testdir;
144 my $data = "$dev->{vendor} $dev->{product}";
145 return undef if !file_write
("$vfio_basedir/new_id", $data);
147 my $fn = "$pcisysfs/devices/$name/driver/unbind";
148 if (!file_write
($fn, $name)) {
149 return undef if -f
$fn;
152 $fn = "$vfio_basedir/bind";
154 return undef if !file_write
($fn, $name);
160 sub pci_dev_group_bind_to_vfio
{
163 my $vfio_basedir = "$pcisysfs/drivers/vfio-pci";
165 if (!-d
$vfio_basedir) {
166 system("/sbin/modprobe vfio-pci >/dev/null 2>/dev/null");
168 die "Cannot find vfio-pci module!\n" if !-d
$vfio_basedir;
170 # get IOMMU group devices
171 opendir(my $D, "$pcisysfs/devices/0000:$pciid/iommu_group/devices/") || die "Cannot open iommu_group: $!\n";
172 my @devs = grep /^0000:/, readdir($D);
175 foreach my $pciid (@devs) {
176 $pciid =~ m/^([:\.\da-f]+)$/ or die "PCI ID $pciid not valid!\n";
178 # pci bridges, switches or root ports are not supported
179 # they have a pci_bus subdirectory so skip them
180 next if (-e
"$pcisysfs/devices/$pciid/pci_bus");
182 my $info = pci_device_info
($1);
183 pci_dev_bind_to_vfio
($info) || die "Cannot bind $pciid to vfio\n";
189 sub pci_create_mdev_device
{
190 my ($pciid, $uuid, $type) = @_;
192 my $basedir = "$pcisysfs/devices/0000:$pciid";
193 my $mdev_dir = "$basedir/mdev_supported_types";
195 die "pci device '$pciid' does not support mediated devices \n"
198 die "pci device '$pciid' has no type '$type'\n"
199 if !-d
"$mdev_dir/$type";
201 if (-d
"$basedir/$uuid") {
202 # it already exists, checking type
203 my $typelink = readlink("$basedir/$uuid/mdev_type");
204 my ($existingtype) = $typelink =~ m
|/([^/]+)$|;
205 die "mdev instance '$uuid' already exits, but type is not '$type'\n"
206 if $type ne $existingtype;
208 # instance exists, so use it but warn the user
209 warn "mdev instance '$uuid' already existed, using it.\n";
213 my $instances = file_read_firstline
("$mdev_dir/$type/available_instances");
214 my ($avail) = $instances =~ m/^(\d+)$/;
215 die "pci device '$pciid' has no available instances of '$type'\n"
218 die "could not create 'type' for pci devices '$pciid'\n"
219 if !file_write
("$mdev_dir/$type/create", $uuid);
224 sub pci_cleanup_mdev_device
{
225 my ($pciid, $uuid) = @_;
227 my $basedir = "$pcisysfs/devices/0000:$pciid/$uuid";
230 return 1; # no cleanup necessary if it does not exist
233 return file_write
("$basedir/remove", "1");
236 # encode the hostpci index and vmid into the uuid
237 sub generate_mdev_uuid
{
238 my ($vmid, $index) = @_;
240 my $string = sprintf("%08d-0000-0000-0000-%012d", $index, $vmid);
245 # idea is from usbutils package (/usr/bin/usb-devices) script
246 sub __scan_usb_device
{
247 my ($res, $devpath, $parent, $level) = @_;
249 return if ! -d
$devpath;
250 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
251 my $port = $level ?
int($1 - 1) : 0;
253 my $busnum = int(file_read_firstline
("$devpath/busnum"));
254 my $devnum = int(file_read_firstline
("$devpath/devnum"));
261 speed
=> file_read_firstline
("$devpath/speed"),
262 class => hex(file_read_firstline
("$devpath/bDeviceClass")),
263 vendid
=> file_read_firstline
("$devpath/idVendor"),
264 prodid
=> file_read_firstline
("$devpath/idProduct"),
268 my $usbpath = $devpath;
269 $usbpath =~ s
|^.*/\d
+\
-||;
270 $d->{usbpath
} = $usbpath;
273 my $product = file_read_firstline
("$devpath/product");
274 $d->{product
} = $product if $product;
276 my $manu = file_read_firstline
("$devpath/manufacturer");
277 $d->{manufacturer
} = $manu if $manu;
279 my $serial => file_read_firstline
("$devpath/serial");
280 $d->{serial
} = $serial if $serial;
284 foreach my $subdev (<$devpath/$busnum-*>) {
285 next if $subdev !~ m
|/$busnum-[0-9]+(\
.[0-9]+)*$|;
286 __scan_usb_device
($res, $subdev, $devnum, $level + 1);
295 foreach my $device (</sys/bus
/usb
/devices
/usb
*>) {
296 __scan_usb_device
($devlist, $device, 0, 0);