--- lib/sysfs.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/lib/sysfs.c b/lib/sysfs.c index 0dbd127ff53b..1db1b1a7752b 100644 --- a/lib/sysfs.c +++ b/lib/sysfs.c @@ -390,6 +390,37 @@ sysfs_fill_info(struct pci_dev *d, unsigned int flags) { done |= sysfs_get_resources(d); } + if (flags & PCI_FILL_PARENT) + { + unsigned int domain, bus, dev, func; + char *path_abs, *path_canon, *name; + char path_rel[OBJNAMELEN]; + struct pci_dev *parent; + + /* Construct sysfs path for parent device */ + sysfs_obj_name(d, "..", path_rel); + path_abs = realpath(path_rel, NULL); + name = path_abs ? strrchr(path_abs, '/') : NULL; + name = name ? name+1 : name; + parent = NULL; + + if (name && sscanf(name, "%x:%x:%x.%d", &domain, &bus, &dev, &func) == 4 && domain <= 0x7fffffff) + for (parent = d->access->devices; parent; parent = parent->next) + if (parent->domain == (int)domain && parent->bus == bus && parent->dev == dev && parent->func == func) + break; + + if (parent) + { + /* Check if parsed BDF address from parent sysfs device is really expected PCI device */ + sysfs_obj_name(parent, ".", path_rel); + path_canon = realpath(path_rel, NULL); + if (path_canon && strcmp(path_canon, path_abs) == 0) + { + d->parent = parent; + done |= PCI_FILL_PARENT; + } + } + } } if (flags & PCI_FILL_PHYS_SLOT) -- 2.20.1