X-Git-Url: https://git.proxmox.com/?p=pve-common.git;a=blobdiff_plain;f=src%2FPVE%2FSysFSTools.pm;h=b0a025f28ea051847b41938776fcdc53c5327b16;hp=8906f19acd1e56497dc04188bc43d0aba9ee1c94;hb=44a4db5b60b8a69e28d764682ffdb9874e67aba3;hpb=f0765c717400f2e566dd6026e3257a8b1e0b1f02 diff --git a/src/PVE/SysFSTools.pm b/src/PVE/SysFSTools.pm index 8906f19..b0a025f 100644 --- a/src/PVE/SysFSTools.pm +++ b/src/PVE/SysFSTools.pm @@ -8,15 +8,20 @@ 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])"; sub lspci { + my ($id_filter) = @_; my $devices = {}; - dir_glob_foreach("$pcisysfs/devices", '[a-f0-9]{4}:([a-f0-9]{2}:[a-f0-9]{2})\.([0-9])', sub { - my (undef, $id, $function) = @_; - my $res = { id => $id, function => $function}; - push @{$devices->{$id}}, $res; + dir_glob_foreach("$pcisysfs/devices", $pciregex, sub { + my (undef, undef, $bus, $slot, $function) = @_; + + my $id = "$bus:$slot"; + return if defined($id_filter) && $id_filter ne $id; + + push @{$devices->{$id}}, { id => $id, function => $function }; }); # Entries should be sorted by functions. @@ -28,13 +33,42 @@ sub lspci { return $devices; } -sub check_iommu_support{ - #fixme : need to check IOMMU support - #http://www.linux-kvm.org/page/How_to_assign_devices_with_VT-d_in_KVM +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 $iommu=1; - return $iommu; + 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/', "[^\.].*"); } sub file_write { @@ -55,7 +89,7 @@ sub pci_device_info { my $res; - return undef if $name !~ m/^([a-f0-9]{4}):([a-f0-9]{2}):([a-f0-9]{2})\.([a-f0-9])$/; + return undef if $name !~ m/^${pciregex}$/; my ($domain, $bus, $slot, $func) = ($1, $2, $3, $4); my $irq = file_read_firstline("$pcisysfs/devices/$name/irq"); @@ -152,4 +186,117 @@ 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) = @_; + + return if ! -d $devpath; + return if $level && $devpath !~ m/^.*[-.](\d+)$/; + my $port = $level ? int($1 - 1) : 0; + + my $busnum = int(file_read_firstline("$devpath/busnum")); + my $devnum = int(file_read_firstline("$devpath/devnum")); + + my $d = { + port => $port, + level => $level, + busnum => $busnum, + devnum => $devnum, + speed => file_read_firstline("$devpath/speed"), + class => hex(file_read_firstline("$devpath/bDeviceClass")), + vendid => file_read_firstline("$devpath/idVendor"), + prodid => file_read_firstline("$devpath/idProduct"), + }; + + if ($level) { + my $usbpath = $devpath; + $usbpath =~ s|^.*/\d+\-||; + $d->{usbpath} = $usbpath; + } + + my $product = file_read_firstline("$devpath/product"); + $d->{product} = $product if $product; + + my $manu = file_read_firstline("$devpath/manufacturer"); + $d->{manufacturer} = $manu if $manu; + + my $serial => file_read_firstline("$devpath/serial"); + $d->{serial} = $serial if $serial; + + push @$res, $d; + + foreach my $subdev (<$devpath/$busnum-*>) { + next if $subdev !~ m|/$busnum-[0-9]+(\.[0-9]+)*$|; + __scan_usb_device($res, $subdev, $devnum, $level + 1); + } + +}; + +sub scan_usb { + + my $devlist = []; + + foreach my $device () { + __scan_usb_device($devlist, $device, 0, 0); + } + + return $devlist; +} + 1;