5
0
mirror of git://git.proxmox.com/git/qemu-server.git synced 2025-03-12 20:58:26 +03:00
Dominik Csapak 7583d156fd use new pcie port hardware
with qemu 4.0 we can make use of the new pcie-root-ports with settings
for the width/speed which can resolve issues with some hardware combinations
when negioating link speed

so we add a new q35 cfg that we include with machine types >= 4.0
to preserve live migration of machines without passthrough but q35

for details about the link speeds see:

pcie: Enhanced link speed and width support
https://lists.gnu.org/archive/html/qemu-devel/2018-12/msg02827.html

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
2019-07-08 13:31:55 +02:00

124 lines
3.4 KiB
Perl

package PVE::QemuServer::USB;
use strict;
use warnings;
use PVE::QemuServer::PCI qw(print_pci_addr);
use PVE::JSONSchema;
use base 'Exporter';
our @EXPORT_OK = qw(
parse_usb_device
get_usb_controllers
get_usb_devices
);
sub parse_usb_device {
my ($value) = @_;
return undef if !$value;
my $res = {};
if ($value =~ m/^(0x)?([0-9A-Fa-f]{4}):(0x)?([0-9A-Fa-f]{4})$/) {
$res->{vendorid} = $2;
$res->{productid} = $4;
} elsif ($value =~ m/^(\d+)\-(\d+(\.\d+)*)$/) {
$res->{hostbus} = $1;
$res->{hostport} = $2;
} elsif ($value =~ m/^spice$/i) {
$res->{spice} = 1;
} else {
return undef;
}
return $res;
}
sub get_usb_controllers {
my ($conf, $bridges, $arch, $machine, $format, $max_usb_devices) = @_;
my $devices = [];
my $pciaddr = "";
if ($arch eq 'aarch64') {
$pciaddr = print_pci_addr('ehci', $bridges, $arch, $machine);
push @$devices, '-device', "usb-ehci,id=ehci$pciaddr";
} elsif ($machine !~ /q35/) { # FIXME: combine this and machine_type_is_q35
$pciaddr = print_pci_addr("piix3", $bridges, $arch, $machine);
push @$devices, '-device', "piix3-usb-uhci,id=uhci$pciaddr.0x2";
my $use_usb2 = 0;
for (my $i = 0; $i < $max_usb_devices; $i++) {
next if !$conf->{"usb$i"};
my $d = eval { PVE::JSONSchema::parse_property_string($format,$conf->{"usb$i"}) };
next if !$d || $d->{usb3}; # do not add usb2 controller if we have only usb3 devices
$use_usb2 = 1;
}
# include usb device config
push @$devices, '-readconfig', '/usr/share/qemu-server/pve-usb.cfg' if $use_usb2;
}
# add usb3 controller if needed
my $use_usb3 = 0;
for (my $i = 0; $i < $max_usb_devices; $i++) {
next if !$conf->{"usb$i"};
my $d = eval { PVE::JSONSchema::parse_property_string($format,$conf->{"usb$i"}) };
next if !$d || !$d->{usb3};
$use_usb3 = 1;
}
$pciaddr = print_pci_addr("xhci", $bridges, $arch, $machine);
push @$devices, '-device', "nec-usb-xhci,id=xhci$pciaddr" if $use_usb3;
return @$devices;
}
sub get_usb_devices {
my ($conf, $format, $max_usb_devices) = @_;
my $devices = [];
for (my $i = 0; $i < $max_usb_devices; $i++) {
next if !$conf->{"usb$i"};
my $d = eval { PVE::JSONSchema::parse_property_string($format,$conf->{"usb$i"}) };
next if !$d;
if (defined($d->{host})) {
my $hostdevice = parse_usb_device($d->{host});
$hostdevice->{usb3} = $d->{usb3};
if (defined($hostdevice->{spice}) && $hostdevice->{spice}) {
# usb redir support for spice, currently no usb3
push @$devices, '-chardev', "spicevmc,id=usbredirchardev$i,name=usbredir";
push @$devices, '-device', "usb-redir,chardev=usbredirchardev$i,id=usbredirdev$i,bus=ehci.0";
} else {
push @$devices, '-device', print_usbdevice_full($conf, "usb$i", $hostdevice);
}
}
}
return @$devices;
}
sub print_usbdevice_full {
my ($conf, $deviceid, $device) = @_;
return if !$device;
my $usbdevice = "usb-host";
# if it is a usb3 device, attach it to the xhci controller, else omit the bus option
if($device->{usb3}) {
$usbdevice .= ",bus=xhci.0";
}
if (defined($device->{vendorid}) && defined($device->{productid})) {
$usbdevice .= ",vendorid=0x$device->{vendorid},productid=0x$device->{productid}";
} elsif (defined($device->{hostbus}) && defined($device->{hostport})) {
$usbdevice .= ",hostbus=$device->{hostbus},hostport=$device->{hostport}";
}
$usbdevice .= ",id=$deviceid";
return $usbdevice;
}
1;