Commit | Line | Data |
---|---|---|
f0765c71 DC |
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"; | |
bf94e590 | 11 | my $pciregex = "([a-f0-9]{4}):([a-f0-9]{2}):([a-f0-9]{2})\.([a-f0-9])"; |
f0765c71 | 12 | |
a70972cd DC |
13 | my $parse_pci_ids = sub { |
14 | my $ids = {}; | |
15 | ||
16 | open(my $fh, '<', "/usr/share/misc/pci.ids") | |
17 | or return $ids; | |
18 | ||
19 | my $curvendor; | |
20 | my $curdevice; | |
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; | |
30 | } | |
31 | } | |
f0765c71 | 32 | |
a70972cd DC |
33 | return $ids; |
34 | }; | |
f0765c71 | 35 | |
a70972cd DC |
36 | # returns a list of pci devices |
37 | # | |
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) | |
40 | # | |
41 | # verbose also returns iommu groups, subvendor/device and the | |
42 | # human readable names from /usr/share/misc/pci.ids | |
639c3030 DC |
43 | # |
44 | # return format: | |
45 | # [ | |
46 | # { | |
47 | # id => '00:00.0', | |
48 | # vendor => '0xabab', | |
49 | # device => '0xefef', | |
50 | # class => '0x012345', | |
51 | # | |
52 | # # optional | |
53 | # iommugroup => '14', | |
54 | # mdev => 1, | |
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', | |
61 | # }, | |
62 | # ... | |
63 | # ] | |
64 | # | |
a70972cd DC |
65 | sub lspci { |
66 | my ($filter, $verbose) = @_; | |
b5dacd10 | 67 | |
a70972cd DC |
68 | my $devices = []; |
69 | my $ids = {}; | |
70 | if ($verbose) { | |
71 | $ids = $parse_pci_ids->(); | |
72 | } | |
b5dacd10 | 73 | |
a70972cd DC |
74 | dir_glob_foreach("$pcisysfs/devices", $pciregex, sub { |
75 | my ($fullid, $domain, $bus, $slot, $function) = @_; | |
882eaf6a | 76 | my $id = "$domain:$bus:$slot.$function"; |
a70972cd | 77 | |
882eaf6a | 78 | if (defined($filter) && !ref($filter) && $id !~ m/^(0000:)?\Q$filter\E/) { |
a70972cd DC |
79 | return; # filter ids early |
80 | } | |
81 | ||
82 | my $devdir = "$pcisysfs/devices/$fullid"; | |
83 | ||
84 | my $vendor = file_read_firstline("$devdir/vendor"); | |
85 | my $device = file_read_firstline("$devdir/device"); | |
86 | my $class = file_read_firstline("$devdir/class"); | |
87 | ||
88 | my $res = { | |
89 | id => $id, | |
90 | vendor => $vendor, | |
91 | device => $device, | |
92 | class => $class, | |
93 | }; | |
94 | ||
95 | if (defined($filter) && ref($filter) eq 'CODE' && !$filter->($res)) { | |
96 | return; | |
97 | } | |
98 | ||
99 | if ($verbose) { | |
100 | $res->{iommugroup} = -1; | |
101 | if (-e "$devdir/iommu_group") { | |
102 | my ($iommugroup) = (readlink("$devdir/iommu_group") =~ m/\/(\d+)$/); | |
103 | $res->{iommugroup} = int($iommugroup); | |
104 | } | |
105 | ||
106 | if (-d "$devdir/mdev_supported_types") { | |
107 | $res->{mdev} = 1; | |
108 | } | |
109 | ||
110 | my $device_hash = $ids->{$vendor}->{devices}->{$device} // {}; | |
111 | ||
112 | my $sub_vendor = file_read_firstline("$devdir/subsystem_vendor"); | |
113 | my $sub_device = file_read_firstline("$devdir/subsystem_device"); | |
114 | ||
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}; | |
119 | ||
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); | |
126 | } | |
127 | ||
128 | push @$devices, $res; | |
f0765c71 DC |
129 | }); |
130 | ||
a70972cd DC |
131 | # Entries should be sorted by ids |
132 | $devices = [ sort { $a->{id} cmp $b->{id} } @$devices ]; | |
f0765c71 DC |
133 | |
134 | return $devices; | |
135 | } | |
136 | ||
639c3030 DC |
137 | # |
138 | # return format: | |
139 | # [ | |
140 | # { | |
141 | # type => 'FooType_1', | |
142 | # description => "a longer description with custom format\nand newlines", | |
143 | # available => 5, | |
144 | # }, | |
145 | # ... | |
146 | # ] | |
147 | # | |
44a4db5b DC |
148 | sub get_mdev_types { |
149 | my ($id) = @_; | |
150 | ||
151 | my $fullid = $id; | |
152 | if ($id !~ m/^[0-9a-fA-f]{4}:/) { | |
153 | $fullid = "0000:$id"; | |
154 | } | |
155 | ||
156 | my $types = []; | |
157 | ||
158 | my $mdev_path = "$pcisysfs/devices/$fullid/mdev_supported_types"; | |
159 | if (!-d $mdev_path) { | |
160 | return $types; | |
161 | } | |
162 | ||
163 | dir_glob_foreach($mdev_path, '[^\.].*', sub { | |
164 | my ($type) = @_; | |
165 | ||
166 | my $type_path = "$mdev_path/$type"; | |
167 | ||
168 | my $available = int(file_read_firstline("$type_path/available_instances")); | |
169 | my $description = PVE::Tools::file_get_contents("$type_path/description"); | |
170 | ||
171 | push @$types, { | |
172 | type => $type, | |
173 | description => $description, | |
174 | available => $available, | |
175 | }; | |
176 | }); | |
177 | ||
178 | return $types; | |
179 | } | |
180 | ||
f0765c71 | 181 | sub check_iommu_support{ |
63075acb | 182 | # we have IOMMU support if /sys/class/iommu/ is populated |
a923104f | 183 | return PVE::Tools::dir_glob_regex('/sys/class/iommu/', "[^\.].*"); |
f0765c71 DC |
184 | } |
185 | ||
186 | sub file_write { | |
187 | my ($filename, $buf) = @_; | |
188 | ||
189 | my $fh = IO::File->new($filename, "w"); | |
190 | return undef if !$fh; | |
191 | ||
192 | my $res = print $fh $buf; | |
193 | ||
194 | $fh->close(); | |
195 | ||
196 | return $res; | |
197 | } | |
198 | ||
199 | sub pci_device_info { | |
aa59b112 | 200 | my ($name, $verbose) = @_; |
f0765c71 DC |
201 | |
202 | my $res; | |
203 | ||
bf94e590 | 204 | return undef if $name !~ m/^${pciregex}$/; |
f0765c71 DC |
205 | my ($domain, $bus, $slot, $func) = ($1, $2, $3, $4); |
206 | ||
aa59b112 DC |
207 | my $devdir = "$pcisysfs/devices/$name"; |
208 | ||
209 | my $irq = file_read_firstline("$devdir/irq"); | |
f0765c71 DC |
210 | return undef if !defined($irq) || $irq !~ m/^\d+$/; |
211 | ||
aa59b112 | 212 | my $vendor = file_read_firstline("$devdir/vendor"); |
f0765c71 DC |
213 | return undef if !defined($vendor) || $vendor !~ s/^0x//; |
214 | ||
aa59b112 | 215 | my $product = file_read_firstline("$devdir/device"); |
f0765c71 DC |
216 | return undef if !defined($product) || $product !~ s/^0x//; |
217 | ||
218 | $res = { | |
219 | name => $name, | |
220 | vendor => $vendor, | |
221 | product => $product, | |
222 | domain => $domain, | |
223 | bus => $bus, | |
224 | slot => $slot, | |
225 | func => $func, | |
226 | irq => $irq, | |
227 | has_fl_reset => -f "$pcisysfs/devices/$name/reset" || 0, | |
228 | }; | |
229 | ||
aa59b112 DC |
230 | if ($verbose) { |
231 | my $sub_vendor = file_read_firstline("$devdir/subsystem_vendor"); | |
232 | $sub_vendor =~ s/^0x// if defined($sub_vendor); | |
233 | my $sub_device = file_read_firstline("$devdir/subsystem_device"); | |
234 | $sub_device =~ s/^0x// if defined($sub_device); | |
235 | ||
236 | $res->{subsystem_vendor} = $sub_vendor if defined($sub_vendor); | |
237 | $res->{subsystem_device} = $sub_device if defined($sub_device); | |
238 | ||
239 | if (-e "$devdir/iommu_group") { | |
240 | my ($iommugroup) = (readlink("$devdir/iommu_group") =~ m/\/(\d+)$/); | |
241 | $res->{iommugroup} = int($iommugroup); | |
242 | } | |
243 | ||
244 | if (-d "$devdir/mdev_supported_types") { | |
245 | $res->{mdev} = 1; | |
246 | } | |
247 | } | |
248 | ||
f0765c71 DC |
249 | return $res; |
250 | } | |
251 | ||
252 | sub pci_dev_reset { | |
253 | my ($dev) = @_; | |
254 | ||
255 | my $name = $dev->{name}; | |
256 | ||
257 | my $fn = "$pcisysfs/devices/$name/reset"; | |
258 | ||
259 | return file_write($fn, "1"); | |
260 | } | |
261 | ||
262 | sub pci_dev_bind_to_vfio { | |
263 | my ($dev) = @_; | |
264 | ||
265 | my $name = $dev->{name}; | |
266 | ||
267 | my $vfio_basedir = "$pcisysfs/drivers/vfio-pci"; | |
268 | ||
269 | if (!-d $vfio_basedir) { | |
270 | system("/sbin/modprobe vfio-pci >/dev/null 2>/dev/null"); | |
271 | } | |
272 | die "Cannot find vfio-pci module!\n" if !-d $vfio_basedir; | |
273 | ||
274 | my $testdir = "$vfio_basedir/$name"; | |
275 | return 1 if -d $testdir; | |
276 | ||
277 | my $data = "$dev->{vendor} $dev->{product}"; | |
278 | return undef if !file_write("$vfio_basedir/new_id", $data); | |
279 | ||
280 | my $fn = "$pcisysfs/devices/$name/driver/unbind"; | |
281 | if (!file_write($fn, $name)) { | |
282 | return undef if -f $fn; | |
283 | } | |
284 | ||
285 | $fn = "$vfio_basedir/bind"; | |
286 | if (! -d $testdir) { | |
287 | return undef if !file_write($fn, $name); | |
288 | } | |
289 | ||
290 | return -d $testdir; | |
291 | } | |
292 | ||
293 | sub pci_dev_group_bind_to_vfio { | |
294 | my ($pciid) = @_; | |
295 | ||
296 | my $vfio_basedir = "$pcisysfs/drivers/vfio-pci"; | |
297 | ||
298 | if (!-d $vfio_basedir) { | |
299 | system("/sbin/modprobe vfio-pci >/dev/null 2>/dev/null"); | |
300 | } | |
301 | die "Cannot find vfio-pci module!\n" if !-d $vfio_basedir; | |
302 | ||
882eaf6a DC |
303 | $pciid = "0000:$pciid" if $pciid !~ m/^[0-9a-f]{4}:/; |
304 | ||
f0765c71 | 305 | # get IOMMU group devices |
882eaf6a DC |
306 | opendir(my $D, "$pcisysfs/devices/$pciid/iommu_group/devices/") || die "Cannot open iommu_group: $!\n"; |
307 | my @devs = grep /^[0-9a-f]{4}:/, readdir($D); | |
f0765c71 DC |
308 | closedir($D); |
309 | ||
310 | foreach my $pciid (@devs) { | |
882eaf6a | 311 | $pciid =~ m/^([:\.0-9a-f]+)$/ or die "PCI ID $pciid not valid!\n"; |
f0765c71 DC |
312 | |
313 | # pci bridges, switches or root ports are not supported | |
314 | # they have a pci_bus subdirectory so skip them | |
315 | next if (-e "$pcisysfs/devices/$pciid/pci_bus"); | |
316 | ||
317 | my $info = pci_device_info($1); | |
318 | pci_dev_bind_to_vfio($info) || die "Cannot bind $pciid to vfio\n"; | |
319 | } | |
320 | ||
321 | return 1; | |
322 | } | |
323 | ||
44a4db5b DC |
324 | sub pci_create_mdev_device { |
325 | my ($pciid, $uuid, $type) = @_; | |
326 | ||
882eaf6a DC |
327 | $pciid = "0000:$pciid" if $pciid !~ m/^[0-9a-f]{4}:/; |
328 | ||
329 | my $basedir = "$pcisysfs/devices/$pciid"; | |
44a4db5b DC |
330 | my $mdev_dir = "$basedir/mdev_supported_types"; |
331 | ||
332 | die "pci device '$pciid' does not support mediated devices \n" | |
333 | if !-d $mdev_dir; | |
334 | ||
335 | die "pci device '$pciid' has no type '$type'\n" | |
336 | if !-d "$mdev_dir/$type"; | |
337 | ||
338 | if (-d "$basedir/$uuid") { | |
339 | # it already exists, checking type | |
340 | my $typelink = readlink("$basedir/$uuid/mdev_type"); | |
341 | my ($existingtype) = $typelink =~ m|/([^/]+)$|; | |
342 | die "mdev instance '$uuid' already exits, but type is not '$type'\n" | |
343 | if $type ne $existingtype; | |
344 | ||
345 | # instance exists, so use it but warn the user | |
346 | warn "mdev instance '$uuid' already existed, using it.\n"; | |
347 | return undef; | |
348 | } | |
349 | ||
350 | my $instances = file_read_firstline("$mdev_dir/$type/available_instances"); | |
351 | my ($avail) = $instances =~ m/^(\d+)$/; | |
352 | die "pci device '$pciid' has no available instances of '$type'\n" | |
353 | if $avail < 1; | |
354 | ||
355 | die "could not create 'type' for pci devices '$pciid'\n" | |
356 | if !file_write("$mdev_dir/$type/create", $uuid); | |
357 | ||
358 | return undef; | |
359 | } | |
360 | ||
361 | sub pci_cleanup_mdev_device { | |
362 | my ($pciid, $uuid) = @_; | |
363 | ||
882eaf6a DC |
364 | $pciid = "0000:$pciid" if $pciid !~ m/^[0-9a-f]{4}:/; |
365 | ||
366 | my $basedir = "$pcisysfs/devices/$pciid/$uuid"; | |
44a4db5b DC |
367 | |
368 | if (! -e $basedir) { | |
369 | return 1; # no cleanup necessary if it does not exist | |
370 | } | |
371 | ||
372 | return file_write("$basedir/remove", "1"); | |
373 | } | |
374 | ||
375 | # encode the hostpci index and vmid into the uuid | |
376 | sub generate_mdev_uuid { | |
377 | my ($vmid, $index) = @_; | |
378 | ||
379 | my $string = sprintf("%08d-0000-0000-0000-%012d", $index, $vmid); | |
380 | ||
381 | return $string; | |
382 | } | |
383 | ||
6eb61ecd DC |
384 | # idea is from usbutils package (/usr/bin/usb-devices) script |
385 | sub __scan_usb_device { | |
386 | my ($res, $devpath, $parent, $level) = @_; | |
387 | ||
388 | return if ! -d $devpath; | |
389 | return if $level && $devpath !~ m/^.*[-.](\d+)$/; | |
390 | my $port = $level ? int($1 - 1) : 0; | |
391 | ||
392 | my $busnum = int(file_read_firstline("$devpath/busnum")); | |
393 | my $devnum = int(file_read_firstline("$devpath/devnum")); | |
394 | ||
395 | my $d = { | |
396 | port => $port, | |
397 | level => $level, | |
398 | busnum => $busnum, | |
399 | devnum => $devnum, | |
400 | speed => file_read_firstline("$devpath/speed"), | |
401 | class => hex(file_read_firstline("$devpath/bDeviceClass")), | |
402 | vendid => file_read_firstline("$devpath/idVendor"), | |
403 | prodid => file_read_firstline("$devpath/idProduct"), | |
404 | }; | |
405 | ||
406 | if ($level) { | |
407 | my $usbpath = $devpath; | |
408 | $usbpath =~ s|^.*/\d+\-||; | |
409 | $d->{usbpath} = $usbpath; | |
410 | } | |
411 | ||
412 | my $product = file_read_firstline("$devpath/product"); | |
413 | $d->{product} = $product if $product; | |
414 | ||
415 | my $manu = file_read_firstline("$devpath/manufacturer"); | |
416 | $d->{manufacturer} = $manu if $manu; | |
417 | ||
418 | my $serial => file_read_firstline("$devpath/serial"); | |
419 | $d->{serial} = $serial if $serial; | |
420 | ||
421 | push @$res, $d; | |
422 | ||
423 | foreach my $subdev (<$devpath/$busnum-*>) { | |
424 | next if $subdev !~ m|/$busnum-[0-9]+(\.[0-9]+)*$|; | |
425 | __scan_usb_device($res, $subdev, $devnum, $level + 1); | |
426 | } | |
427 | ||
428 | }; | |
429 | ||
430 | sub scan_usb { | |
431 | ||
432 | my $devlist = []; | |
433 | ||
434 | foreach my $device (</sys/bus/usb/devices/usb*>) { | |
435 | __scan_usb_device($devlist, $device, 0, 0); | |
436 | } | |
437 | ||
438 | return $devlist; | |
439 | } | |
440 | ||
f0765c71 | 441 | 1; |