From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1O19uH-0008QE-96 for qemu-devel@nongnu.org; Sun, 11 Apr 2010 23:01:49 -0400 Received: from [140.186.70.92] (port=49041 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1O19uF-0008Q6-4w for qemu-devel@nongnu.org; Sun, 11 Apr 2010 23:01:48 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1O19uD-0005jZ-Jw for qemu-devel@nongnu.org; Sun, 11 Apr 2010 23:01:46 -0400 Received: from mail.valinux.co.jp ([210.128.90.3]:53021) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1O19uD-0005j4-5X for qemu-devel@nongnu.org; Sun, 11 Apr 2010 23:01:45 -0400 Date: Mon, 12 Apr 2010 11:58:59 +0900 From: Isaku Yamahata Message-ID: <20100412025859.GD1232@valinux.co.jp> References: <20100409101324.GC14603@valinux.co.jp> <20100409234835.GA1232@valinux.co.jp> <20100411105129.GC8992@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20100411105129.GC8992@redhat.com> Subject: [Qemu-devel] [PATCH v2] pci: fix pci_find_bus(). List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: "Michael S. Tsirkin" Cc: Blue Swirl , qemu-devel@nongnu.org When looking down child bus, it should look parent bridge's bus number, not child bus's. Optimized tail recursion and style fix. Cc: Blue Swirl Cc: "Michael S. Tsirkin" Signed-off-by: Isaku Yamahata --- hw/pci.c | 25 ++++++++++++++++--------- 1 files changed, 16 insertions(+), 9 deletions(-) diff --git a/hw/pci.c b/hw/pci.c index 2355232..6c0cc7b 100644 --- a/hw/pci.c +++ b/hw/pci.c @@ -1546,23 +1546,30 @@ static void pci_bridge_write_config(PCIDevice *d, PCIBus *pci_find_bus(PCIBus *bus, int bus_num) { - PCIBus *sec, *ret; + PCIBus *sec; - if (!bus) + if (!bus) { return NULL; + } if (pci_bus_num(bus) == bus_num) { return bus; } /* try child bus */ - QLIST_FOREACH(sec, &bus->child, sibling) { - if (!bus->parent_dev /* pci host bridge */ - || (pci_bus_num(sec) <= bus_num && - bus_num <= bus->parent_dev->config[PCI_SUBORDINATE_BUS]) ) { - ret = pci_find_bus(sec, bus_num); - if (ret) { - return ret; + if (!bus->parent_dev /* host pci bridge */ || + (bus->parent_dev->config[PCI_SECONDARY_BUS] < bus_num && + bus_num <= bus->parent_dev->config[PCI_SUBORDINATE_BUS])) { + for (; bus; bus = sec) { + QLIST_FOREACH(sec, &bus->child, sibling) { + assert(sec->parent_dev); + if (sec->parent_dev->config[PCI_SECONDARY_BUS] == bus_num) { + return sec; + } + if (sec->parent_dev->config[PCI_SECONDARY_BUS] < bus_num && + bus_num <= sec->parent_dev->config[PCI_SUBORDINATE_BUS]) { + break; + } } } } -- 1.6.6.1