Skip to contentSkip to conversation

pci-bridge: avoid linking a single downstream port more than once

PATCH Superseded Yao Xingtao via 1 participant 1 message Jul 4, 2024

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pci-bridge: avoid linking a single downstream port more than once

Since the downstream port is not checked, two slots can be linked to a single port. However, this can prevent the driver from detecting the device properly.

It is necessary to ensure that a downstream port is not linked more than once.

Links: https://lore.kernel.org/qemu-devel/OSZPR01MB6453BC61D2FF4035F18084EF8DDC2@OSZPR01MB6453.jpnprd01.prod.outlook.com

---
 hw/pci-bridge/cxl_downstream.c     | 6 ++++++
 hw/pci-bridge/pcie_root_port.c     | 6 ++++++
 hw/pci-bridge/xio3130_downstream.c | 6 ++++++
 3 files changed, 18 insertions(+)
hw/pci-bridge/cxl_downstream.c +6 -0
index 742da07a01..fa33cdb36b 100644
@@ -153,6 +153,12 @@ static void cxl_dsp_realize(PCIDevice *d, Error **errp)
         goto err_bridge;
     }
 
+    if (pcie_find_port_by_pn(pci_get_bus(d), p->port) != NULL) {
+        rc = -EBUSY;
+        error_setg(errp, "Can't link port, error %d", rc);
+        goto err_msi;
+    }
+
     rc = pcie_cap_init(d, CXL_DOWNSTREAM_PORT_EXP_OFFSET,
                        PCI_EXP_TYPE_DOWNSTREAM, p->port,
                        errp);
hw/pci-bridge/pcie_root_port.c +6 -0
index 09a34786bc..350137bd8c 100644
@@ -89,6 +89,12 @@ static void rp_realize(PCIDevice *d, Error **errp)
         }
     }
 
+    if (pcie_find_port_by_pn(pci_get_bus(d), p->port) != NULL) {
+        rc = -EBUSY;
+        error_setg(errp, "Can't link port, error %d", rc);
+        goto err_int;
+    }
+
     rc = pcie_cap_init(d, rpc->exp_offset, PCI_EXP_TYPE_ROOT_PORT,
                        p->port, errp);
     if (rc < 0) {
hw/pci-bridge/xio3130_downstream.c +6 -0
index 907d5105b0..af04d2efad 100644
@@ -88,6 +88,12 @@ static void xio3130_downstream_realize(PCIDevice *d, Error **errp)
         goto err_msi;
     }
 
+    if (pcie_find_port_by_pn(pci_get_bus(d), p->port) != NULL) {
+        rc = -EBUSY;
+        error_setg(errp, "Can't link port, error %d", rc);
+        goto err_msi;
+    }
+
     rc = pcie_cap_init(d, XIO3130_EXP_OFFSET, PCI_EXP_TYPE_DOWNSTREAM,
                        p->port, errp);
     if (rc < 0) {
Signature · 1 line
2.37.3