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
50 # class => '0x012345',
55 # vendor_name => 'Foo Inc.',
56 # device_name => 'Bar 9000AF',
57 # subsystem_vendor => '0xacac',
58 # subsystem_device => '0xfefe',
59 # subsystem_vendor_name => 'Foo Europe GmbH',
60 # subsystem_device_name => 'Bar 9001AF OC',
66 my ($filter, $verbose) = @_;
71 $ids = $parse_pci_ids->();
74 dir_glob_foreach
("$pcisysfs/devices", $pciregex, sub {
75 my ($fullid, $domain, $bus, $slot, $function) = @_;
76 my $id = "$domain:$bus:$slot.$function";
78 if (defined($filter) && !ref($filter) && $id !~ m/^(0000:)?\Q$filter\E/) {
79 return; # filter ids early
82 my $devdir = "$pcisysfs/devices/$fullid";
84 my $vendor = file_read_firstline
("$devdir/vendor");
85 my $device = file_read_firstline
("$devdir/device");
86 my $class = file_read_firstline
("$devdir/class");
95 if (defined($filter) && ref($filter) eq 'CODE' && !$filter->($res)) {
100 $res->{iommugroup
} = -1;
101 if (-e
"$devdir/iommu_group") {
102 my ($iommugroup) = (readlink("$devdir/iommu_group") =~ m
/\
/(\d+)$/);
103 $res->{iommugroup
} = int($iommugroup);
106 if (-d
"$devdir/mdev_supported_types") {
110 my $device_hash = $ids->{$vendor}->{devices
}->{$device} // {};
112 my $sub_vendor = file_read_firstline
("$devdir/subsystem_vendor");
113 my $sub_device = file_read_firstline
("$devdir/subsystem_device");
115 my $vendor_name = $ids->{$vendor}->{name
};
116 my $device_name = $device_hash->{name
};
117 my $sub_vendor_name = $ids->{$sub_vendor}->{name
};
118 my $sub_device_name = $device_hash->{subs
}->{$sub_vendor}->{$sub_device};
120 $res->{vendor_name
} = $vendor_name if defined($vendor_name);
121 $res->{device_name
} = $device_name if defined($device_name);
122 $res->{subsystem_vendor
} = $sub_vendor if defined($sub_vendor);
123 $res->{subsystem_device
} = $sub_device if defined($sub_device);
124 $res->{subsystem_vendor_name
} = $sub_vendor_name if defined($sub_vendor_name);
125 $res->{subsystem_device_name
} = $sub_device_name if defined($sub_device_name);
128 push @$devices, $res;
131 # Entries should be sorted by ids
132 $devices = [ sort { $a->{id
} cmp $b->{id
} } @$devices ];
141 # type => 'FooType_1',
142 # description => "a longer description with custom format\nand newlines",
152 if ($id !~ m/^[0-9a-fA-f]{4}:/) {
153 $fullid = "0000:$id";
158 my $mdev_path = "$pcisysfs/devices/$fullid/mdev_supported_types";
159 if (!-d
$mdev_path) {
163 dir_glob_foreach
($mdev_path, '[^\.].*', sub {
166 my $type_path = "$mdev_path/$type";
168 my $available = int(file_read_firstline
("$type_path/available_instances"));
169 my $description = PVE
::Tools
::file_get_contents
("$type_path/description");
173 description
=> $description,
174 available
=> $available,
181 sub check_iommu_support
{
182 # we have IOMMU support if /sys/class/iommu/ is populated
183 return PVE
::Tools
::dir_glob_regex
('/sys/class/iommu/', "[^\.].*");
187 my ($filename, $buf) = @_;
189 my $fh = IO
::File-
>new($filename, "w");
190 return undef if !$fh;
192 my $res = print $fh $buf;
199 sub pci_device_info
{
204 return undef if $name !~ m/^${pciregex}$/;
205 my ($domain, $bus, $slot, $func) = ($1, $2, $3, $4);
207 my $irq = file_read_firstline
("$pcisysfs/devices/$name/irq");
208 return undef if !defined($irq) || $irq !~ m/^\d+$/;
210 my $vendor = file_read_firstline
("$pcisysfs/devices/$name/vendor");
211 return undef if !defined($vendor) || $vendor !~ s/^0x//;
213 my $product = file_read_firstline
("$pcisysfs/devices/$name/device");
214 return undef if !defined($product) || $product !~ s/^0x//;
225 has_fl_reset
=> -f
"$pcisysfs/devices/$name/reset" || 0,
234 my $name = $dev->{name
};
236 my $fn = "$pcisysfs/devices/$name/reset";
238 return file_write
($fn, "1");
241 sub pci_dev_bind_to_vfio
{
244 my $name = $dev->{name
};
246 my $vfio_basedir = "$pcisysfs/drivers/vfio-pci";
248 if (!-d
$vfio_basedir) {
249 system("/sbin/modprobe vfio-pci >/dev/null 2>/dev/null");
251 die "Cannot find vfio-pci module!\n" if !-d
$vfio_basedir;
253 my $testdir = "$vfio_basedir/$name";
254 return 1 if -d
$testdir;
256 my $data = "$dev->{vendor} $dev->{product}";
257 return undef if !file_write
("$vfio_basedir/new_id", $data);
259 my $fn = "$pcisysfs/devices/$name/driver/unbind";
260 if (!file_write
($fn, $name)) {
261 return undef if -f
$fn;
264 $fn = "$vfio_basedir/bind";
266 return undef if !file_write
($fn, $name);
272 sub pci_dev_group_bind_to_vfio
{
275 my $vfio_basedir = "$pcisysfs/drivers/vfio-pci";
277 if (!-d
$vfio_basedir) {
278 system("/sbin/modprobe vfio-pci >/dev/null 2>/dev/null");
280 die "Cannot find vfio-pci module!\n" if !-d
$vfio_basedir;
282 $pciid = "0000:$pciid" if $pciid !~ m/^[0-9a-f]{4}:/;
284 # get IOMMU group devices
285 opendir(my $D, "$pcisysfs/devices/$pciid/iommu_group/devices/") || die "Cannot open iommu_group: $!\n";
286 my @devs = grep /^[0-9a-f]{4}:/, readdir($D);
289 foreach my $pciid (@devs) {
290 $pciid =~ m/^([:\.0-9a-f]+)$/ or die "PCI ID $pciid not valid!\n";
292 # pci bridges, switches or root ports are not supported
293 # they have a pci_bus subdirectory so skip them
294 next if (-e
"$pcisysfs/devices/$pciid/pci_bus");
296 my $info = pci_device_info
($1);
297 pci_dev_bind_to_vfio
($info) || die "Cannot bind $pciid to vfio\n";
303 sub pci_create_mdev_device
{
304 my ($pciid, $uuid, $type) = @_;
306 $pciid = "0000:$pciid" if $pciid !~ m/^[0-9a-f]{4}:/;
308 my $basedir = "$pcisysfs/devices/$pciid";
309 my $mdev_dir = "$basedir/mdev_supported_types";
311 die "pci device '$pciid' does not support mediated devices \n"
314 die "pci device '$pciid' has no type '$type'\n"
315 if !-d
"$mdev_dir/$type";
317 if (-d
"$basedir/$uuid") {
318 # it already exists, checking type
319 my $typelink = readlink("$basedir/$uuid/mdev_type");
320 my ($existingtype) = $typelink =~ m
|/([^/]+)$|;
321 die "mdev instance '$uuid' already exits, but type is not '$type'\n"
322 if $type ne $existingtype;
324 # instance exists, so use it but warn the user
325 warn "mdev instance '$uuid' already existed, using it.\n";
329 my $instances = file_read_firstline
("$mdev_dir/$type/available_instances");
330 my ($avail) = $instances =~ m/^(\d+)$/;
331 die "pci device '$pciid' has no available instances of '$type'\n"
334 die "could not create 'type' for pci devices '$pciid'\n"
335 if !file_write
("$mdev_dir/$type/create", $uuid);
340 sub pci_cleanup_mdev_device
{
341 my ($pciid, $uuid) = @_;
343 $pciid = "0000:$pciid" if $pciid !~ m/^[0-9a-f]{4}:/;
345 my $basedir = "$pcisysfs/devices/$pciid/$uuid";
348 return 1; # no cleanup necessary if it does not exist
351 return file_write
("$basedir/remove", "1");
354 # encode the hostpci index and vmid into the uuid
355 sub generate_mdev_uuid
{
356 my ($vmid, $index) = @_;
358 my $string = sprintf("%08d-0000-0000-0000-%012d", $index, $vmid);
363 # idea is from usbutils package (/usr/bin/usb-devices) script
364 sub __scan_usb_device
{
365 my ($res, $devpath, $parent, $level) = @_;
367 return if ! -d
$devpath;
368 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
369 my $port = $level ?
int($1 - 1) : 0;
371 my $busnum = int(file_read_firstline
("$devpath/busnum"));
372 my $devnum = int(file_read_firstline
("$devpath/devnum"));
379 speed
=> file_read_firstline
("$devpath/speed"),
380 class => hex(file_read_firstline
("$devpath/bDeviceClass")),
381 vendid
=> file_read_firstline
("$devpath/idVendor"),
382 prodid
=> file_read_firstline
("$devpath/idProduct"),
386 my $usbpath = $devpath;
387 $usbpath =~ s
|^.*/\d
+\
-||;
388 $d->{usbpath
} = $usbpath;
391 my $product = file_read_firstline
("$devpath/product");
392 $d->{product
} = $product if $product;
394 my $manu = file_read_firstline
("$devpath/manufacturer");
395 $d->{manufacturer
} = $manu if $manu;
397 my $serial => file_read_firstline
("$devpath/serial");
398 $d->{serial
} = $serial if $serial;
402 foreach my $subdev (<$devpath/$busnum-*>) {
403 next if $subdev !~ m
|/$busnum-[0-9]+(\
.[0-9]+)*$|;
404 __scan_usb_device
($res, $subdev, $devnum, $level + 1);
413 foreach my $device (</sys/bus
/usb
/devices
/usb
*>) {
414 __scan_usb_device
($devlist, $device, 0, 0);