Received: from [140.186.70.92] (port=36395 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PFqge-0005Dy-6F for qemu-devel@nongnu.org; Tue, 09 Nov 2010 11:04:45 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PFqgZ-0007eC-PW for qemu-devel@nongnu.org; Tue, 09 Nov 2010 11:04:43 -0500 Received: from mx1.redhat.com ([209.132.183.28]:20156) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PFqgZ-0007ds-Ik for qemu-devel@nongnu.org; Tue, 09 Nov 2010 11:04:39 -0500 Date: Tue, 9 Nov 2010 18:04:33 +0200 From: "Michael S. Tsirkin" Message-ID: <20101109160433.GA26801@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Subject: [Qemu-devel] [PATCH] pci: fix device path for devices behind nested bridges List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Gleb Natapov Cc: yamahata@valinux.co.jp, Alex Williamson , qemu-devel@nongnu.org, armbru@redhat.com We were using bus number in the device path, which is clearly broken as this number is guest-assigned for all devices except the root. Fix by using hierarchical list of slot/function numbers, walking the path from root down to device, instead. Add :00 as bus number so that if there are no nested bridges, this is compatible with what we have now. Note: as pointed out by Gleb, using openfirmware paths might be cleaner, doing this would break compatibility though. Signed-off-by: Michael S. Tsirkin --- So this fixes the issue with bridges at function != 0. Untested. An alternative that will still preserve compatibility is to do something like if (domain || d->bus->parent_device) { use OF path } else { snprintf(path, sizeof(path), "0000:00:%02x.%x", PCI_SLOT(d->devfn), PCI_FUNC(d->devfn)); } Would that be better? Please comment. hw/pci.c | 44 ++++++++++++++++++++++++++++++++++++-------- 1 files changed, 36 insertions(+), 8 deletions(-) diff --git a/hw/pci.c b/hw/pci.c index 8f6fcf8..aed2d42 100644 --- a/hw/pci.c +++ b/hw/pci.c @@ -1826,13 +1826,41 @@ static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent) static char *pcibus_get_dev_path(DeviceState *dev) { - PCIDevice *d = (PCIDevice *)dev; - char path[16]; - - snprintf(path, sizeof(path), "%04x:%02x:%02x.%x", - pci_find_domain(d->bus), d->config[PCI_SECONDARY_BUS], - PCI_SLOT(d->devfn), PCI_FUNC(d->devfn)); - - return strdup(path); + PCIDevice *d = container_of(dev, PCIDevice, qdev); + PCIDevice *t; + int slot_depth; + /* Path format: Domain:00:Slot.Function:Slot.Function....:Slot.Function. + * 00 is added here to make this format compatible with + * domain:Bus:Slot.Func for systems without nested PCI bridges. + * Slot.Function list specifies the slot and function numbers for all + * devices on the path from root to the specific device. */ + int domain_len = strlen("DDDD:00"); + int slot_len = strlen(":SS.F"); + int path_len; + char *path, *p; + + /* Calculate # of slots on path between device and root. */; + slot_depth = 0; + for (t = d; t; t = t->bus->parent_dev) + ++slot_depth; + + path_len = domain_len + slot_len * slot_depth; + + /* Allocate memory, fill in the terminating null byte. */ + path = malloc(path_len + 1 /* For '\0' */); + path[path_len] = '\0'; + + /* First field is the domain. */ + snprintf(path, domain_len, "%04x", pci_find_domain(d->bus)); + + /* Fill in slot numbers. We walk up from device to root, so need to print + * them in the reverse order, last to first. */ + p = path + path_len; + for (t = d; t; t = t->bus->parent_dev) { + p -= slot_len; + snprintf(p, slot_len, ":%02x.%x", PCI_SLOT(t->devfn), PCI_FUNC(d->devfn)); + } + + return path; } -- 1.7.3.2.91.g446ac