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])";
13 my $parse_pci_ids = sub {
16 open(my $fh, '<', "/usr/share/misc/pci.ids")
21 while (my $line = <$fh>) {
22 if ($line =~ m/^([0-9a-fA-F]{4})\s+(.*)$/) {
23 $curvendor = ($ids->{"0x$1"} = {});
24 $curvendor->{name
} = $2;
25 } elsif ($line =~ m/^\t([0-9a-fA-F]{4})\s+(.*)$/) {
26 $curdevice = ($curvendor->{devices
}->{"0x$1"} = {});
27 $curdevice->{name
} = $2;
28 } elsif ($line =~ m/^\t\t([0-9a-fA-F]{4}) ([0-9a-fA-F]{4})\s+(.*)$/) {
29 $curdevice->{subs
}->{"0x$1"}->{"0x$2"} = $3;
36 # returns a list of pci devices
38 # filter is either a string (then it tries to match to the id)
39 # or a sub ref (then it adds the device if the sub returns truthy)
41 # verbose also returns iommu groups, subvendor/device and the
42 # human readable names from /usr/share/misc/pci.ids
44 my ($filter, $verbose) = @_;
49 $ids = $parse_pci_ids->();
52 dir_glob_foreach
("$pcisysfs/devices", $pciregex, sub {
53 my ($fullid, $domain, $bus, $slot, $function) = @_;
54 my $id = "$bus:$slot.$function";
56 if (defined($filter) && !ref($filter) && $id !~ m/^\Q$filter\E/) {
57 return; # filter ids early
60 my $devdir = "$pcisysfs/devices/$fullid";
62 my $vendor = file_read_firstline
("$devdir/vendor");
63 my $device = file_read_firstline
("$devdir/device");
64 my $class = file_read_firstline
("$devdir/class");
73 if (defined($filter) && ref($filter) eq 'CODE' && !$filter->($res)) {
78 $res->{iommugroup
} = -1;
79 if (-e
"$devdir/iommu_group") {
80 my ($iommugroup) = (readlink("$devdir/iommu_group") =~ m
/\
/(\d+)$/);
81 $res->{iommugroup
} = int($iommugroup);
84 if (-d
"$devdir/mdev_supported_types") {
88 my $device_hash = $ids->{$vendor}->{devices
}->{$device} // {};
90 my $sub_vendor = file_read_firstline
("$devdir/subsystem_vendor");
91 my $sub_device = file_read_firstline
("$devdir/subsystem_device");
93 my $vendor_name = $ids->{$vendor}->{name
};
94 my $device_name = $device_hash->{name
};
95 my $sub_vendor_name = $ids->{$sub_vendor}->{name
};
96 my $sub_device_name = $device_hash->{subs
}->{$sub_vendor}->{$sub_device};
98 $res->{vendor_name
} = $vendor_name if defined($vendor_name);
99 $res->{device_name
} = $device_name if defined($device_name);
100 $res->{subsystem_vendor
} = $sub_vendor if defined($sub_vendor);
101 $res->{subsystem_device
} = $sub_device if defined($sub_device);
102 $res->{subsystem_vendor_name
} = $sub_vendor_name if defined($sub_vendor_name);
103 $res->{subsystem_device_name
} = $sub_device_name if defined($sub_device_name);
106 push @$devices, $res;
109 # Entries should be sorted by ids
110 $devices = [ sort { $a->{id
} cmp $b->{id
} } @$devices ];
119 if ($id !~ m/^[0-9a-fA-f]{4}:/) {
120 $fullid = "0000:$id";
125 my $mdev_path = "$pcisysfs/devices/$fullid/mdev_supported_types";
126 if (!-d
$mdev_path) {
130 dir_glob_foreach
($mdev_path, '[^\.].*', sub {
133 my $type_path = "$mdev_path/$type";
135 my $available = int(file_read_firstline
("$type_path/available_instances"));
136 my $description = PVE
::Tools
::file_get_contents
("$type_path/description");
140 description
=> $description,
141 available
=> $available,
148 sub check_iommu_support
{
149 # we have IOMMU support if /sys/class/iommu/ is populated
150 return PVE
::Tools
::dir_glob_regex
('/sys/class/iommu/', "[^\.].*");
154 my ($filename, $buf) = @_;
156 my $fh = IO
::File-
>new($filename, "w");
157 return undef if !$fh;
159 my $res = print $fh $buf;
166 sub pci_device_info
{
171 return undef if $name !~ m/^${pciregex}$/;
172 my ($domain, $bus, $slot, $func) = ($1, $2, $3, $4);
174 my $irq = file_read_firstline
("$pcisysfs/devices/$name/irq");
175 return undef if !defined($irq) || $irq !~ m/^\d+$/;
177 my $vendor = file_read_firstline
("$pcisysfs/devices/$name/vendor");
178 return undef if !defined($vendor) || $vendor !~ s/^0x//;
180 my $product = file_read_firstline
("$pcisysfs/devices/$name/device");
181 return undef if !defined($product) || $product !~ s/^0x//;
192 has_fl_reset
=> -f
"$pcisysfs/devices/$name/reset" || 0,
201 my $name = $dev->{name
};
203 my $fn = "$pcisysfs/devices/$name/reset";
205 return file_write
($fn, "1");
208 sub pci_dev_bind_to_vfio
{
211 my $name = $dev->{name
};
213 my $vfio_basedir = "$pcisysfs/drivers/vfio-pci";
215 if (!-d
$vfio_basedir) {
216 system("/sbin/modprobe vfio-pci >/dev/null 2>/dev/null");
218 die "Cannot find vfio-pci module!\n" if !-d
$vfio_basedir;
220 my $testdir = "$vfio_basedir/$name";
221 return 1 if -d
$testdir;
223 my $data = "$dev->{vendor} $dev->{product}";
224 return undef if !file_write
("$vfio_basedir/new_id", $data);
226 my $fn = "$pcisysfs/devices/$name/driver/unbind";
227 if (!file_write
($fn, $name)) {
228 return undef if -f
$fn;
231 $fn = "$vfio_basedir/bind";
233 return undef if !file_write
($fn, $name);
239 sub pci_dev_group_bind_to_vfio
{
242 my $vfio_basedir = "$pcisysfs/drivers/vfio-pci";
244 if (!-d
$vfio_basedir) {
245 system("/sbin/modprobe vfio-pci >/dev/null 2>/dev/null");
247 die "Cannot find vfio-pci module!\n" if !-d
$vfio_basedir;
249 # get IOMMU group devices
250 opendir(my $D, "$pcisysfs/devices/0000:$pciid/iommu_group/devices/") || die "Cannot open iommu_group: $!\n";
251 my @devs = grep /^0000:/, readdir($D);
254 foreach my $pciid (@devs) {
255 $pciid =~ m/^([:\.\da-f]+)$/ or die "PCI ID $pciid not valid!\n";
257 # pci bridges, switches or root ports are not supported
258 # they have a pci_bus subdirectory so skip them
259 next if (-e
"$pcisysfs/devices/$pciid/pci_bus");
261 my $info = pci_device_info
($1);
262 pci_dev_bind_to_vfio
($info) || die "Cannot bind $pciid to vfio\n";
268 sub pci_create_mdev_device
{
269 my ($pciid, $uuid, $type) = @_;
271 my $basedir = "$pcisysfs/devices/0000:$pciid";
272 my $mdev_dir = "$basedir/mdev_supported_types";
274 die "pci device '$pciid' does not support mediated devices \n"
277 die "pci device '$pciid' has no type '$type'\n"
278 if !-d
"$mdev_dir/$type";
280 if (-d
"$basedir/$uuid") {
281 # it already exists, checking type
282 my $typelink = readlink("$basedir/$uuid/mdev_type");
283 my ($existingtype) = $typelink =~ m
|/([^/]+)$|;
284 die "mdev instance '$uuid' already exits, but type is not '$type'\n"
285 if $type ne $existingtype;
287 # instance exists, so use it but warn the user
288 warn "mdev instance '$uuid' already existed, using it.\n";
292 my $instances = file_read_firstline
("$mdev_dir/$type/available_instances");
293 my ($avail) = $instances =~ m/^(\d+)$/;
294 die "pci device '$pciid' has no available instances of '$type'\n"
297 die "could not create 'type' for pci devices '$pciid'\n"
298 if !file_write
("$mdev_dir/$type/create", $uuid);
303 sub pci_cleanup_mdev_device
{
304 my ($pciid, $uuid) = @_;
306 my $basedir = "$pcisysfs/devices/0000:$pciid/$uuid";
309 return 1; # no cleanup necessary if it does not exist
312 return file_write
("$basedir/remove", "1");
315 # encode the hostpci index and vmid into the uuid
316 sub generate_mdev_uuid
{
317 my ($vmid, $index) = @_;
319 my $string = sprintf("%08d-0000-0000-0000-%012d", $index, $vmid);
324 # idea is from usbutils package (/usr/bin/usb-devices) script
325 sub __scan_usb_device
{
326 my ($res, $devpath, $parent, $level) = @_;
328 return if ! -d
$devpath;
329 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
330 my $port = $level ?
int($1 - 1) : 0;
332 my $busnum = int(file_read_firstline
("$devpath/busnum"));
333 my $devnum = int(file_read_firstline
("$devpath/devnum"));
340 speed
=> file_read_firstline
("$devpath/speed"),
341 class => hex(file_read_firstline
("$devpath/bDeviceClass")),
342 vendid
=> file_read_firstline
("$devpath/idVendor"),
343 prodid
=> file_read_firstline
("$devpath/idProduct"),
347 my $usbpath = $devpath;
348 $usbpath =~ s
|^.*/\d
+\
-||;
349 $d->{usbpath
} = $usbpath;
352 my $product = file_read_firstline
("$devpath/product");
353 $d->{product
} = $product if $product;
355 my $manu = file_read_firstline
("$devpath/manufacturer");
356 $d->{manufacturer
} = $manu if $manu;
358 my $serial => file_read_firstline
("$devpath/serial");
359 $d->{serial
} = $serial if $serial;
363 foreach my $subdev (<$devpath/$busnum-*>) {
364 next if $subdev !~ m
|/$busnum-[0-9]+(\
.[0-9]+)*$|;
365 __scan_usb_device
($res, $subdev, $devnum, $level + 1);
374 foreach my $device (</sys/bus
/usb
/devices
/usb
*>) {
375 __scan_usb_device
($devlist, $device, 0, 0);