linux-pci.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Pali Rohár" <pali@kernel.org>
To: "Martin Mares" <mj@ucw.cz>, "Bjorn Helgaas" <helgaas@kernel.org>,
	"Krzysztof Wilczyński" <kw@linux.com>,
	"Matthew Wilcox" <willy@infradead.org>,
	linux-pci@vger.kernel.org
Subject: [PATCH pciutils 2/4] libpci: sysfs: Implement support for PCI_FILL_PARENT
Date: Fri, 21 Jan 2022 15:22:56 +0100	[thread overview]
Message-ID: <20220121142258.28170-3-pali@kernel.org> (raw)
In-Reply-To: <20220121142258.28170-1-pali@kernel.org>

---
 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


  parent reply	other threads:[~2022-01-21 14:23 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-21 14:22 [PATCH pciutils 0/4] Support for PCI_FILL_PARENT Pali Rohár
2022-01-21 14:22 ` [PATCH pciutils 1/4] libpci: Add new option PCI_FILL_PARENT Pali Rohár
2022-01-21 14:22 ` Pali Rohár [this message]
2022-01-21 14:22 ` [PATCH pciutils 3/4] lspci: Build tree based on PCI_FILL_PARENT information Pali Rohár
2022-01-21 14:22 ` [PATCH pciutils 4/4] lspci: Do not show -[00]- bus in tree output Pali Rohár
2022-01-21 14:51 ` [PATCH pciutils 0/4] Support for PCI_FILL_PARENT Martin Mareš
2022-01-21 15:21   ` Pali Rohár
2022-01-21 15:28     ` Martin Mareš

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220121142258.28170-3-pali@kernel.org \
    --to=pali@kernel.org \
    --cc=helgaas@kernel.org \
    --cc=kw@linux.com \
    --cc=linux-pci@vger.kernel.org \
    --cc=mj@ucw.cz \
    --cc=willy@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).