qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/3] pcie_chassis: use an available slot number if not explicity set
@ 2021-03-31 16:08 Tom Yan
  2021-03-31 16:08 ` [PATCH 2/3] pcie_chassis: add slot_max field Tom Yan
  2021-03-31 16:08 ` [PATCH 3/3] pcie_root_port: reorder procedures in realize() Tom Yan
  0 siblings, 2 replies; 3+ messages in thread
From: Tom Yan @ 2021-03-31 16:08 UTC (permalink / raw)
  To: qemu-devel, mst; +Cc: marcel, yamahata, Tom Yan

Currently pcie_chassis_add_slot() simply fails if the requested
slot number is already used.

Make it find and use an available slot number if the requested one
does not appear to be explicitly set (i.e. is 0). This allows slot
numbers to be enumerated automatically.

Maintain the current behaviour if the requested slot number appears
to be explicitly set (i.e. is larger than 0), in case some users do
not wish the slot numbers in actual use could be different from what
were specified (they will need to avoid using 0 though).

Also fixing an apparent mistake in pcie_chassis_find_slot_with_chassis().
The type of the slot field of PCIESlot is uint16_t, which is also what
all of its users pass to it.

Signed-off-by: Tom Yan <tom.ty89@gmail.com>
---
 hw/pci/pcie_port.c | 28 ++++++++++++++++++++++++++--
 1 file changed, 26 insertions(+), 2 deletions(-)

diff --git a/hw/pci/pcie_port.c b/hw/pci/pcie_port.c
index eb563ad435..40fd80c4da 100644
--- a/hw/pci/pcie_port.c
+++ b/hw/pci/pcie_port.c
@@ -81,7 +81,7 @@ void pcie_chassis_create(uint8_t chassis_number)
 }
 
 static PCIESlot *pcie_chassis_find_slot_with_chassis(struct PCIEChassis *c,
-                                                     uint8_t slot)
+                                                     uint16_t slot)
 {
     PCIESlot *s;
     QLIST_FOREACH(s, &c->slots, next) {
@@ -92,6 +92,28 @@ static PCIESlot *pcie_chassis_find_slot_with_chassis(struct PCIEChassis *c,
     return s;
 }
 
+static void pcie_chassis_find_avail_slot_with_chassis(struct PCIEChassis *c, struct PCIESlot *slot)
+{
+    PCIESlot *s;
+    uint16_t slot_max = 0;
+    QLIST_FOREACH(s, &c->slots, next) {
+        if (s->slot > slot_max) {
+            slot_max = s->slot;
+        }
+    }
+
+    /*
+      find an available number to use from slot->slot (inclusive) to
+      slot_max; if there is none, use slot_max+1
+    */
+    while (slot->slot <= slot_max) {
+        if (!pcie_chassis_find_slot_with_chassis(c, slot->slot)) {
+            break;
+        }
+        slot->slot++;
+    }
+}
+
 PCIESlot *pcie_chassis_find_slot(uint8_t chassis_number, uint16_t slot)
 {
     struct PCIEChassis *c;
@@ -109,7 +131,9 @@ int pcie_chassis_add_slot(struct PCIESlot *slot)
     if (!c) {
         return -ENODEV;
     }
-    if (pcie_chassis_find_slot_with_chassis(c, slot->slot)) {
+    if (!slot->slot) { // slot number not explicity set
+        pcie_chassis_find_avail_slot_with_chassis(c, slot);
+    } else if (pcie_chassis_find_slot_with_chassis(c, slot->slot)) {
         return -EBUSY;
     }
     QLIST_INSERT_HEAD(&c->slots, slot, next);
-- 
2.31.1



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH 2/3] pcie_chassis: add slot_max field
  2021-03-31 16:08 [PATCH 1/3] pcie_chassis: use an available slot number if not explicity set Tom Yan
@ 2021-03-31 16:08 ` Tom Yan
  2021-03-31 16:08 ` [PATCH 3/3] pcie_root_port: reorder procedures in realize() Tom Yan
  1 sibling, 0 replies; 3+ messages in thread
From: Tom Yan @ 2021-03-31 16:08 UTC (permalink / raw)
  To: qemu-devel, mst; +Cc: marcel, yamahata, Tom Yan

Keep track of the largested allocated slot number when adding slot.

Signed-off-by: Tom Yan <tom.ty89@gmail.com>
---
 hw/pci/pcie_port.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)

diff --git a/hw/pci/pcie_port.c b/hw/pci/pcie_port.c
index 40fd80c4da..1e2348250c 100644
--- a/hw/pci/pcie_port.c
+++ b/hw/pci/pcie_port.c
@@ -49,6 +49,7 @@ void pcie_port_init_reg(PCIDevice *d)
  */
 struct PCIEChassis {
     uint8_t     number;
+    uint16_t    slot_max; // largest allocated slot number
 
     QLIST_HEAD(, PCIESlot) slots;
     QLIST_ENTRY(PCIEChassis) next;
@@ -94,19 +95,11 @@ static PCIESlot *pcie_chassis_find_slot_with_chassis(struct PCIEChassis *c,
 
 static void pcie_chassis_find_avail_slot_with_chassis(struct PCIEChassis *c, struct PCIESlot *slot)
 {
-    PCIESlot *s;
-    uint16_t slot_max = 0;
-    QLIST_FOREACH(s, &c->slots, next) {
-        if (s->slot > slot_max) {
-            slot_max = s->slot;
-        }
-    }
-
     /*
       find an available number to use from slot->slot (inclusive) to
-      slot_max; if there is none, use slot_max+1
+      c->slot_max; if there is none, use c->slot_max+1
     */
-    while (slot->slot <= slot_max) {
+    while (slot->slot <= c->slot_max) {
         if (!pcie_chassis_find_slot_with_chassis(c, slot->slot)) {
             break;
         }
@@ -137,6 +130,8 @@ int pcie_chassis_add_slot(struct PCIESlot *slot)
         return -EBUSY;
     }
     QLIST_INSERT_HEAD(&c->slots, slot, next);
+    if (slot->slot > c->slot_max)
+        c->slot_max = slot->slot;
     return 0;
 }
 
-- 
2.31.1



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH 3/3] pcie_root_port: reorder procedures in realize()
  2021-03-31 16:08 [PATCH 1/3] pcie_chassis: use an available slot number if not explicity set Tom Yan
  2021-03-31 16:08 ` [PATCH 2/3] pcie_chassis: add slot_max field Tom Yan
@ 2021-03-31 16:08 ` Tom Yan
  1 sibling, 0 replies; 3+ messages in thread
From: Tom Yan @ 2021-03-31 16:08 UTC (permalink / raw)
  To: qemu-devel, mst; +Cc: marcel, yamahata, Tom Yan

There are a bunch of pcie_cap_*_init() in the realize method of the
pcie root port devices that do not have return value. They can probably
be called after the slot is successfully added to an created chassis
(i.e. the slot number to use is final).
of them makes use of the slot number.

Signed-off-by: Tom Yan <tom.ty89@gmail.com>
---
 hw/pci-bridge/pcie_root_port.c     | 10 +++++-----
 hw/pci-bridge/xio3130_downstream.c |  9 +++++----
 2 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/hw/pci-bridge/pcie_root_port.c b/hw/pci-bridge/pcie_root_port.c
index f1cfe9d14a..2bd2d02ccb 100644
--- a/hw/pci-bridge/pcie_root_port.c
+++ b/hw/pci-bridge/pcie_root_port.c
@@ -92,11 +92,6 @@ static void rp_realize(PCIDevice *d, Error **errp)
         goto err_int;
     }
 
-    pcie_cap_arifwd_init(d);
-    pcie_cap_deverr_init(d);
-    pcie_cap_slot_init(d, s);
-    pcie_cap_root_init(d);
-
     pcie_chassis_create(s->chassis);
     rc = pcie_chassis_add_slot(s);
     if (rc < 0) {
@@ -104,6 +99,11 @@ static void rp_realize(PCIDevice *d, Error **errp)
         goto err_pcie_cap;
     }
 
+    pcie_cap_arifwd_init(d);
+    pcie_cap_deverr_init(d);
+    pcie_cap_slot_init(d, s);
+    pcie_cap_root_init(d);
+
     rc = pcie_aer_init(d, PCI_ERR_VER, rpc->aer_offset,
                        PCI_ERR_SIZEOF, errp);
     if (rc < 0) {
diff --git a/hw/pci-bridge/xio3130_downstream.c b/hw/pci-bridge/xio3130_downstream.c
index 04aae72cd6..b7a92693ee 100644
--- a/hw/pci-bridge/xio3130_downstream.c
+++ b/hw/pci-bridge/xio3130_downstream.c
@@ -92,10 +92,6 @@ static void xio3130_downstream_realize(PCIDevice *d, Error **errp)
     if (rc < 0) {
         goto err_msi;
     }
-    pcie_cap_flr_init(d);
-    pcie_cap_deverr_init(d);
-    pcie_cap_slot_init(d, s);
-    pcie_cap_arifwd_init(d);
 
     pcie_chassis_create(s->chassis);
     rc = pcie_chassis_add_slot(s);
@@ -104,6 +100,11 @@ static void xio3130_downstream_realize(PCIDevice *d, Error **errp)
         goto err_pcie_cap;
     }
 
+    pcie_cap_flr_init(d);
+    pcie_cap_deverr_init(d);
+    pcie_cap_slot_init(d, s);
+    pcie_cap_arifwd_init(d);
+
     rc = pcie_aer_init(d, PCI_ERR_VER, XIO3130_AER_OFFSET,
                        PCI_ERR_SIZEOF, errp);
     if (rc < 0) {
-- 
2.31.1



^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-03-31 16:21 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-31 16:08 [PATCH 1/3] pcie_chassis: use an available slot number if not explicity set Tom Yan
2021-03-31 16:08 ` [PATCH 2/3] pcie_chassis: add slot_max field Tom Yan
2021-03-31 16:08 ` [PATCH 3/3] pcie_root_port: reorder procedures in realize() Tom Yan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).