]> git.proxmox.com Git - pve-common.git/blob - src/PVE/SysFSTools.pm
SysFSTools: lspci: fixup: improve naming and refactor a bit
[pve-common.git] / src / PVE / SysFSTools.pm
1 package PVE::SysFSTools;
2
3 use strict;
4 use warnings;
5
6 use IO::File;
7
8 use PVE::Tools qw(file_read_firstline dir_glob_foreach);
9
10 my $pcisysfs = "/sys/bus/pci";
11 my $pciregex = "([a-f0-9]{4}):([a-f0-9]{2}):([a-f0-9]{2})\.([a-f0-9])";
12
13 sub lspci {
14 my ($id_filter) = @_;
15
16 my $devices = {};
17
18 dir_glob_foreach("$pcisysfs/devices", $pciregex, sub {
19 my (undef, undef, $bus, $slot, $function) = @_;
20
21 my $id = "$bus:$slot";
22 return if defined($id_filter) && $id_filter ne $id;
23
24 push @{$devices->{$id}}, { id => $id, function => $function };
25 });
26
27 # Entries should be sorted by functions.
28 foreach my $id (keys %$devices) {
29 my $dev = $devices->{$id};
30 $devices->{$id} = [ sort { $a->{function} <=> $b->{function} } @$dev ];
31 }
32
33 return $devices;
34 }
35
36 sub check_iommu_support{
37 # we have IOMMU support if /sys/class/iommu/ is populated
38 return PVE::Tools::dir_glob_regex('/sys/class/iommu/', "[^\.].*");
39 }
40
41 sub file_write {
42 my ($filename, $buf) = @_;
43
44 my $fh = IO::File->new($filename, "w");
45 return undef if !$fh;
46
47 my $res = print $fh $buf;
48
49 $fh->close();
50
51 return $res;
52 }
53
54 sub pci_device_info {
55 my ($name) = @_;
56
57 my $res;
58
59 return undef if $name !~ m/^${pciregex}$/;
60 my ($domain, $bus, $slot, $func) = ($1, $2, $3, $4);
61
62 my $irq = file_read_firstline("$pcisysfs/devices/$name/irq");
63 return undef if !defined($irq) || $irq !~ m/^\d+$/;
64
65 my $vendor = file_read_firstline("$pcisysfs/devices/$name/vendor");
66 return undef if !defined($vendor) || $vendor !~ s/^0x//;
67
68 my $product = file_read_firstline("$pcisysfs/devices/$name/device");
69 return undef if !defined($product) || $product !~ s/^0x//;
70
71 $res = {
72 name => $name,
73 vendor => $vendor,
74 product => $product,
75 domain => $domain,
76 bus => $bus,
77 slot => $slot,
78 func => $func,
79 irq => $irq,
80 has_fl_reset => -f "$pcisysfs/devices/$name/reset" || 0,
81 };
82
83 return $res;
84 }
85
86 sub pci_dev_reset {
87 my ($dev) = @_;
88
89 my $name = $dev->{name};
90
91 my $fn = "$pcisysfs/devices/$name/reset";
92
93 return file_write($fn, "1");
94 }
95
96 sub pci_dev_bind_to_vfio {
97 my ($dev) = @_;
98
99 my $name = $dev->{name};
100
101 my $vfio_basedir = "$pcisysfs/drivers/vfio-pci";
102
103 if (!-d $vfio_basedir) {
104 system("/sbin/modprobe vfio-pci >/dev/null 2>/dev/null");
105 }
106 die "Cannot find vfio-pci module!\n" if !-d $vfio_basedir;
107
108 my $testdir = "$vfio_basedir/$name";
109 return 1 if -d $testdir;
110
111 my $data = "$dev->{vendor} $dev->{product}";
112 return undef if !file_write("$vfio_basedir/new_id", $data);
113
114 my $fn = "$pcisysfs/devices/$name/driver/unbind";
115 if (!file_write($fn, $name)) {
116 return undef if -f $fn;
117 }
118
119 $fn = "$vfio_basedir/bind";
120 if (! -d $testdir) {
121 return undef if !file_write($fn, $name);
122 }
123
124 return -d $testdir;
125 }
126
127 sub pci_dev_group_bind_to_vfio {
128 my ($pciid) = @_;
129
130 my $vfio_basedir = "$pcisysfs/drivers/vfio-pci";
131
132 if (!-d $vfio_basedir) {
133 system("/sbin/modprobe vfio-pci >/dev/null 2>/dev/null");
134 }
135 die "Cannot find vfio-pci module!\n" if !-d $vfio_basedir;
136
137 # get IOMMU group devices
138 opendir(my $D, "$pcisysfs/devices/0000:$pciid/iommu_group/devices/") || die "Cannot open iommu_group: $!\n";
139 my @devs = grep /^0000:/, readdir($D);
140 closedir($D);
141
142 foreach my $pciid (@devs) {
143 $pciid =~ m/^([:\.\da-f]+)$/ or die "PCI ID $pciid not valid!\n";
144
145 # pci bridges, switches or root ports are not supported
146 # they have a pci_bus subdirectory so skip them
147 next if (-e "$pcisysfs/devices/$pciid/pci_bus");
148
149 my $info = pci_device_info($1);
150 pci_dev_bind_to_vfio($info) || die "Cannot bind $pciid to vfio\n";
151 }
152
153 return 1;
154 }
155
156 # idea is from usbutils package (/usr/bin/usb-devices) script
157 sub __scan_usb_device {
158 my ($res, $devpath, $parent, $level) = @_;
159
160 return if ! -d $devpath;
161 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
162 my $port = $level ? int($1 - 1) : 0;
163
164 my $busnum = int(file_read_firstline("$devpath/busnum"));
165 my $devnum = int(file_read_firstline("$devpath/devnum"));
166
167 my $d = {
168 port => $port,
169 level => $level,
170 busnum => $busnum,
171 devnum => $devnum,
172 speed => file_read_firstline("$devpath/speed"),
173 class => hex(file_read_firstline("$devpath/bDeviceClass")),
174 vendid => file_read_firstline("$devpath/idVendor"),
175 prodid => file_read_firstline("$devpath/idProduct"),
176 };
177
178 if ($level) {
179 my $usbpath = $devpath;
180 $usbpath =~ s|^.*/\d+\-||;
181 $d->{usbpath} = $usbpath;
182 }
183
184 my $product = file_read_firstline("$devpath/product");
185 $d->{product} = $product if $product;
186
187 my $manu = file_read_firstline("$devpath/manufacturer");
188 $d->{manufacturer} = $manu if $manu;
189
190 my $serial => file_read_firstline("$devpath/serial");
191 $d->{serial} = $serial if $serial;
192
193 push @$res, $d;
194
195 foreach my $subdev (<$devpath/$busnum-*>) {
196 next if $subdev !~ m|/$busnum-[0-9]+(\.[0-9]+)*$|;
197 __scan_usb_device($res, $subdev, $devnum, $level + 1);
198 }
199
200 };
201
202 sub scan_usb {
203
204 my $devlist = [];
205
206 foreach my $device (</sys/bus/usb/devices/usb*>) {
207 __scan_usb_device($devlist, $device, 0, 0);
208 }
209
210 return $devlist;
211 }
212
213 1;