]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/SysFSTools.pm
bump version to 8.2.1
[pve-common.git] / src / PVE / SysFSTools.pm
index 7875b269bb40fb720766ddb8e2585c7c308aee2f..57f0ac832b41ce15f7043ec2f7322e8337115620 100644 (file)
@@ -8,7 +8,8 @@ use IO::File;
 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 $domainregex = "[a-f0-9]{4,}";
+my $pciregex = "($domainregex):([a-f0-9]{2}):([a-f0-9]{2})\.([a-f0-9])";
 
 my $parse_pci_ids = sub {
     my $ids = {};
@@ -33,6 +34,12 @@ my $parse_pci_ids = sub {
     return $ids;
 };
 
+my sub normalize_pci_id {
+    my ($id) = @_;
+    $id = "0000:$id" if $id !~ m/^${domainregex}:/;
+    return $id;
+};
+
 # returns a list of pci devices
 #
 # filter is either a string (then it tries to match to the id)
@@ -148,14 +155,11 @@ sub lspci {
 sub get_mdev_types {
     my ($id) = @_;
 
-    my $fullid = $id;
-    if ($id !~ m/^[0-9a-fA-f]{4}:/) {
-       $fullid = "0000:$id";
-    }
+    $id = normalize_pci_id($id);
 
     my $types = [];
 
-    my $mdev_path = "$pcisysfs/devices/$fullid/mdev_supported_types";
+    my $mdev_path = "$pcisysfs/devices/$id/mdev_supported_types";
     if (!-d $mdev_path) {
        return $types;
     }
@@ -168,11 +172,16 @@ sub get_mdev_types {
        my $available = int(file_read_firstline("$type_path/available_instances"));
        my $description = PVE::Tools::file_get_contents("$type_path/description");
 
-       push @$types, {
+       my $entry = {
            type => $type,
            description => $description,
            available => $available,
        };
+
+       my $name = file_read_firstline("$type_path/name");
+       $entry->{name} = $name if defined($name);
+
+       push @$types, $entry;
     });
 
     return $types;
@@ -218,7 +227,7 @@ sub pci_device_info {
     $res = {
        name => $name,
        vendor => $vendor,
-       product => $product,
+       device => $product,
        domain => $domain,
        bus => $bus,
        slot => $slot,
@@ -274,7 +283,7 @@ sub pci_dev_bind_to_vfio {
     my $testdir = "$vfio_basedir/$name";
     return 1 if -d $testdir;
 
-    my $data = "$dev->{vendor} $dev->{product}";
+    my $data = "$dev->{vendor} $dev->{device}";
     return undef if !file_write("$vfio_basedir/new_id", $data);
 
     my $fn = "$pcisysfs/devices/$name/driver/unbind";
@@ -300,19 +309,18 @@ sub pci_dev_group_bind_to_vfio {
     }
     die "Cannot find vfio-pci module!\n" if !-d $vfio_basedir;
 
-    $pciid = "0000:$pciid" if $pciid !~ m/^[0-9a-f]{4}:/;
+    $pciid = normalize_pci_id($pciid);
 
     # get IOMMU group devices
     opendir(my $D, "$pcisysfs/devices/$pciid/iommu_group/devices/") || die "Cannot open iommu_group: $!\n";
-      my @devs = grep /^[0-9a-f]{4}:/, readdir($D);
+    my @devs = grep /^${domainregex}:/, readdir($D);
     closedir($D);
 
     foreach my $pciid (@devs) {
        $pciid =~ m/^([:\.0-9a-f]+)$/ or die "PCI ID $pciid not valid!\n";
 
-        # pci bridges, switches or root ports are not supported
-        # they have a pci_bus subdirectory so skip them
-        next if (-e "$pcisysfs/devices/$pciid/pci_bus");
+       # PCI bridges, switches or root-ports aren't supported and all have a pci_bus dir we can test
+       next if (-e "$pcisysfs/devices/$pciid/pci_bus");
 
        my $info = pci_device_info($1);
        pci_dev_bind_to_vfio($info) || die "Cannot bind $pciid to vfio\n";
@@ -324,7 +332,7 @@ sub pci_dev_group_bind_to_vfio {
 sub pci_create_mdev_device {
     my ($pciid, $uuid, $type) = @_;
 
-    $pciid = "0000:$pciid" if $pciid !~ m/^[0-9a-f]{4}:/;
+    $pciid = normalize_pci_id($pciid);
 
     my $basedir = "$pcisysfs/devices/$pciid";
     my $mdev_dir = "$basedir/mdev_supported_types";
@@ -358,20 +366,6 @@ sub pci_create_mdev_device {
     return undef;
 }
 
-sub pci_cleanup_mdev_device {
-    my ($pciid, $uuid) = @_;
-
-    $pciid = "0000:$pciid" if $pciid !~ m/^[0-9a-f]{4}:/;
-
-    my $basedir = "$pcisysfs/devices/$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) = @_;