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.t...@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