]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/SysFSTools.pm
SysFSTools.pm: improve and extend lspci
[pve-common.git] / src / PVE / SysFSTools.pm
index c16e7d3720947c7b43684443c4c73650816fa9c2..2e7d0284156b8a153e4255c1b85de565a47be17e 100644 (file)
@@ -10,28 +10,141 @@ use PVE::Tools qw(file_read_firstline dir_glob_foreach);
 my $pcisysfs = "/sys/bus/pci";
 my $pciregex = "([a-f0-9]{4}):([a-f0-9]{2}):([a-f0-9]{2})\.([a-f0-9])";
 
+my $parse_pci_ids = sub {
+    my $ids = {};
+
+    open(my $fh, '<', "/usr/share/misc/pci.ids")
+       or return $ids;
+
+    my $curvendor;
+    my $curdevice;
+    while (my $line = <$fh>) {
+       if ($line =~ m/^([0-9a-fA-F]{4})\s+(.*)$/) {
+           $curvendor = ($ids->{"0x$1"} = {});
+           $curvendor->{name} = $2;
+       } elsif ($line =~ m/^\t([0-9a-fA-F]{4})\s+(.*)$/) {
+           $curdevice = ($curvendor->{devices}->{"0x$1"} = {});
+           $curdevice->{name} = $2;
+       } elsif ($line =~ m/^\t\t([0-9a-fA-F]{4}) ([0-9a-fA-F]{4})\s+(.*)$/) {
+           $curdevice->{subs}->{"0x$1"}->{"0x$2"} = $3;
+       }
+    }
+
+    return $ids;
+};
+
+# returns a list of pci devices
+#
+# filter is either a string (then it tries to match to the id)
+# or a sub ref (then it adds the device if the sub returns truthy)
+#
+# verbose also returns iommu groups, subvendor/device and the
+# human readable names from /usr/share/misc/pci.ids
 sub lspci {
-    my ($filter) = @_;
+    my ($filter, $verbose) = @_;
 
-    my $devices = {};
+    my $devices = [];
+    my $ids = {};
+    if ($verbose) {
+       $ids = $parse_pci_ids->();
+    }
 
     dir_glob_foreach("$pcisysfs/devices", $pciregex, sub {
-            my (undef, undef, $bus, $slot, $function) = @_;
-           my $id = "$bus:$slot";
-           return if defined($filter) && $id ne $filter;
-           my $res = { id => $id, function => $function};
-           push @{$devices->{$id}}, $res;
+       my ($fullid, $domain, $bus, $slot, $function) = @_;
+       my $id = "$bus:$slot.$function";
+
+       if (defined($filter) && !ref($filter) && $id !~ m/^\Q$filter\E/) {
+           return; # filter ids early
+       }
+
+       my $devdir = "$pcisysfs/devices/$fullid";
+
+       my $vendor = file_read_firstline("$devdir/vendor");
+       my $device = file_read_firstline("$devdir/device");
+       my $class = file_read_firstline("$devdir/class");
+
+       my $res = {
+           id => $id,
+           vendor => $vendor,
+           device => $device,
+           class => $class,
+       };
+
+       if (defined($filter) && ref($filter) eq 'CODE' && !$filter->($res)) {
+           return;
+       }
+
+       if ($verbose) {
+           $res->{iommugroup} = -1;
+           if (-e "$devdir/iommu_group") {
+               my ($iommugroup) = (readlink("$devdir/iommu_group") =~ m/\/(\d+)$/);
+               $res->{iommugroup} = int($iommugroup);
+           }
+
+           if (-d "$devdir/mdev_supported_types") {
+               $res->{mdev} = 1;
+           }
+
+           my $device_hash = $ids->{$vendor}->{devices}->{$device} // {};
+
+           my $sub_vendor = file_read_firstline("$devdir/subsystem_vendor");
+           my $sub_device = file_read_firstline("$devdir/subsystem_device");
+
+           my $vendor_name = $ids->{$vendor}->{name};
+           my $device_name = $device_hash->{name};
+           my $sub_vendor_name = $ids->{$sub_vendor}->{name};
+           my $sub_device_name = $device_hash->{subs}->{$sub_vendor}->{$sub_device};
+
+           $res->{vendor_name} = $vendor_name if defined($vendor_name);
+           $res->{device_name} = $device_name if defined($device_name);
+           $res->{subsystem_vendor} = $sub_vendor if defined($sub_vendor);
+           $res->{subsystem_device} = $sub_device if defined($sub_device);
+           $res->{subsystem_vendor_name} = $sub_vendor_name if defined($sub_vendor_name);
+           $res->{subsystem_device_name} = $sub_device_name if defined($sub_device_name);
+       }
+
+       push @$devices, $res;
     });
 
-    # Entries should be sorted by functions.
-    foreach my $id (keys %$devices) {
-       my $dev = $devices->{$id};
-       $devices->{$id} = [ sort { $a->{function} <=> $b->{function} } @$dev ];
-    }
+    # Entries should be sorted by ids
+    $devices = [ sort { $a->{id} cmp $b->{id} } @$devices ];
 
     return $devices;
 }
 
+sub get_mdev_types {
+    my ($id) = @_;
+
+    my $fullid = $id;
+    if ($id !~ m/^[0-9a-fA-f]{4}:/) {
+       $fullid = "0000:$id";
+    }
+
+    my $types = [];
+
+    my $mdev_path = "$pcisysfs/devices/$fullid/mdev_supported_types";
+    if (!-d $mdev_path) {
+       return $types;
+    }
+
+    dir_glob_foreach($mdev_path, '[^\.].*', sub {
+       my ($type) = @_;
+
+       my $type_path = "$mdev_path/$type";
+
+       my $available = int(file_read_firstline("$type_path/available_instances"));
+       my $description = PVE::Tools::file_get_contents("$type_path/description");
+
+       push @$types, {
+           type => $type,
+           description => $description,
+           available => $available,
+       };
+    });
+
+    return $types;
+}
+
 sub check_iommu_support{
     # we have IOMMU support if /sys/class/iommu/ is populated
     return PVE::Tools::dir_glob_regex('/sys/class/iommu/', "[^\.].*");
@@ -152,6 +265,62 @@ sub pci_dev_group_bind_to_vfio {
     return 1;
 }
 
+sub pci_create_mdev_device {
+    my ($pciid, $uuid, $type) = @_;
+
+    my $basedir = "$pcisysfs/devices/0000:$pciid";
+    my $mdev_dir = "$basedir/mdev_supported_types";
+
+    die "pci device '$pciid' does not support mediated devices \n"
+       if !-d $mdev_dir;
+
+    die "pci device '$pciid' has no type '$type'\n"
+       if !-d "$mdev_dir/$type";
+
+    if (-d "$basedir/$uuid") {
+       # it already exists, checking type
+       my $typelink = readlink("$basedir/$uuid/mdev_type");
+       my ($existingtype) = $typelink =~ m|/([^/]+)$|;
+       die "mdev instance '$uuid' already exits, but type is not '$type'\n"
+           if $type ne $existingtype;
+
+       # instance exists, so use it but warn the user
+       warn "mdev instance '$uuid' already existed, using it.\n";
+       return undef;
+    }
+
+    my $instances = file_read_firstline("$mdev_dir/$type/available_instances");
+    my ($avail) = $instances =~ m/^(\d+)$/;
+    die "pci device '$pciid' has no available instances of '$type'\n"
+       if $avail < 1;
+
+    die "could not create 'type' for pci devices '$pciid'\n"
+       if !file_write("$mdev_dir/$type/create", $uuid);
+
+    return undef;
+}
+
+sub pci_cleanup_mdev_device {
+    my ($pciid, $uuid) = @_;
+
+    my $basedir = "$pcisysfs/devices/0000:$pciid/$uuid";
+
+    if (! -e $basedir) {
+       return 1; # no cleanup necessary if it does not exist
+    }
+
+    return file_write("$basedir/remove", "1");
+}
+
+# encode the hostpci index and vmid into the uuid
+sub generate_mdev_uuid {
+    my ($vmid, $index) = @_;
+
+    my $string = sprintf("%08d-0000-0000-0000-%012d", $index, $vmid);
+
+    return $string;
+}
+
 # idea is from usbutils package (/usr/bin/usb-devices) script
 sub __scan_usb_device {
     my ($res, $devpath, $parent, $level) = @_;