From: Daniel Paziyski <[email protected]>
Handle two cases after realization, which were implicitly handled back when PCI
SR-IOV device registration was done after PCI realization before commit
432ea8cc7d27 ("pcie_sriov: register user created virtual function before
realizing it")
- If a device is a PF of user created VFs, make sure that it has initialized its
list of VFs. This check seems to be superfluous, because the only way it can
fail is:
- if PCI device realization forgets to initialize the user created virtual
functions via pcie_sriov_pf_init_from_user_created_vfs, despite having set
sriov_vf_user_creatable in the device class;
- if the pcie_sriov_pf_init_from_user_created_vfs function fails, but forgets
to set errp (which does not happen), and the device specific realization
code forgets to check and/or propagate if an error has happened.
However, this check was present, so let's include it again for good measure.
- If a device is a user created VF, make sure that the device is still a
PCI Express device. This edge case occurs on devices that before realization
have the PCIe capability set, but then during realization they clear it for
some reason. This only seems to happen to modern virtio-net-pci devices, which
have their PCIe capability on, as any other modern virtio device, on object
instantiation, but is then cleared in its realize function when it is located
at the root complex or not in a PCIe bus.
Cc: [email protected]
Fixes: 432ea8cc7d27 ("pcie_sriov: register user created virtual function before
realizing it")
Signed-off-by: Daniel Paziyski <[email protected]>
Reviewed-by: Michael S. Tsirkin <[email protected]>
Signed-off-by: Michael S. Tsirkin <[email protected]>
Message-ID: <[email protected]>
---
include/hw/pci/pcie_sriov.h | 2 ++
hw/pci/pci.c | 5 +++++
hw/pci/pcie_sriov.c | 19 +++++++++++++++++++
3 files changed, 26 insertions(+)
diff --git a/include/hw/pci/pcie_sriov.h b/include/hw/pci/pcie_sriov.h
index 5f81e3b1bb..41ccba8a71 100644
--- a/include/hw/pci/pcie_sriov.h
+++ b/include/hw/pci/pcie_sriov.h
@@ -57,6 +57,8 @@ int16_t pcie_sriov_pf_init_from_user_created_vfs(PCIDevice
*dev,
bool pcie_sriov_register_device(PCIDevice *dev, Error **errp);
void pcie_sriov_unregister_device(PCIDevice *dev);
+bool pcie_sriov_check_after_realize(PCIDevice *dev, Error **errp);
+
/*
* Default (minimal) page size support values
* as required by the SR/IOV standard:
diff --git a/hw/pci/pci.c b/hw/pci/pci.c
index 1ea388be37..e85b34d06d 100644
--- a/hw/pci/pci.c
+++ b/hw/pci/pci.c
@@ -2347,6 +2347,11 @@ static void pci_qdev_realize(DeviceState *qdev, Error
**errp)
}
}
+ if (!pcie_sriov_check_after_realize(pci_dev, errp)) {
+ pci_qdev_unrealize(DEVICE(pci_dev));
+ return;
+ }
+
/*
* A PCIe Downstream Port that do not have ARI Forwarding enabled must
* associate only Device 0 with the device attached to the bus
diff --git a/hw/pci/pcie_sriov.c b/hw/pci/pcie_sriov.c
index e6891436d3..63d19ea488 100644
--- a/hw/pci/pcie_sriov.c
+++ b/hw/pci/pcie_sriov.c
@@ -393,6 +393,25 @@ bool pcie_sriov_register_device(PCIDevice *dev, Error
**errp)
return true;
}
+bool pcie_sriov_check_after_realize(PCIDevice *dev, Error **errp)
+{
+ if (!dev->exp.sriov_pf.vf && dev->qdev.id &&
+ pfs && g_hash_table_contains(pfs, dev->qdev.id)) {
+ error_setg(errp, "attaching user-created SR-IOV VF unsupported");
+ return false;
+ }
+
+ if (dev->sriov_pf) {
+ /* some devices might clear their PCIe capability during realization */
+ if (!pci_is_express(dev)) {
+ error_setg(errp, "PCI Express is required for SR-IOV VF");
+ return false;
+ }
+ }
+
+ return true;
+}
+
void pcie_sriov_unregister_device(PCIDevice *dev)
{
if (dev->sriov_pf && pfs) {
--
MST