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 3/4] lspci: Build tree based on PCI_FILL_PARENT information
Date: Fri, 21 Jan 2022 15:22:57 +0100	[thread overview]
Message-ID: <20220121142258.28170-4-pali@kernel.org> (raw)
In-Reply-To: <20220121142258.28170-1-pali@kernel.org>

Topology reported by system (libpci provider) may be different from
topology built based on primary/secondary/subordinate numbers from PCI
bridges by lspci.

This happens for example when some non-compliant PCI-to-PCI bridge
with Type 0 header (e.g. Marvell one) is available in the system.

So add additional edges reported by libpci when building tree in lspci.
---
 ls-tree.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++----
 lspci.c   |  2 +-
 2 files changed, 62 insertions(+), 5 deletions(-)

diff --git a/ls-tree.c b/ls-tree.c
index aaa1ee99d9f2..fede581156e4 100644
--- a/ls-tree.c
+++ b/ls-tree.c
@@ -25,6 +25,19 @@ find_bus(struct bridge *b, unsigned int domain, unsigned int n)
   return bus;
 }
 
+static struct device *
+find_device(struct pci_dev *dd)
+{
+  struct device *d;
+
+  if (!dd)
+    return NULL;
+  for (d=first_dev; d; d=d->next)
+    if (d->dev == dd)
+      break;
+  return d;
+}
+
 static struct bus *
 new_bus(struct bridge *b, unsigned int domain, unsigned int n)
 {
@@ -47,9 +60,20 @@ static void
 insert_dev(struct device *d, struct bridge *b)
 {
   struct pci_dev *p = d->dev;
-  struct bus *bus;
+  struct device *parent = NULL;
+  struct bus *bus = NULL;
+
+  if (p->known_fields & PCI_FILL_PARENT)
+    parent = find_device(p->parent);
 
-  if (! (bus = find_bus(b, p->domain, p->bus)))
+  if (parent && parent->bridge)
+    {
+      bus = parent->bridge->first_bus;
+      if (!bus)
+        bus = new_bus(parent->bridge, p->domain, p->bus);
+    }
+
+  if (!bus && ! (bus = find_bus(b, p->domain, p->bus)))
     {
       struct bridge *c;
       for (c=b->child; c; c=c->next)
@@ -113,14 +137,47 @@ grow_tree(void)
 	    b->primary, b->secondary, b->subordinate);
 	}
     }
+
+  /* Append additional bridges reported by libpci via d->parent */
+
+  for (d=first_dev; d; d=d->next)
+    {
+      struct device *parent = NULL;
+      if (d->dev->known_fields & PCI_FILL_PARENT)
+        parent = find_device(d->dev->parent);
+      if (!parent || parent->bridge)
+        continue;
+      b = xmalloc(sizeof(struct bridge));
+      b->domain = parent->dev->domain;
+      b->primary = parent->dev->bus;
+      b->secondary = d->dev->bus;
+      /* At this stage subordinate number is unknown, so set it to secondary bus number. */
+      b->subordinate = b->secondary;
+      *last_br = b;
+      last_br = &b->chain;
+      b->next = b->child = NULL;
+      b->first_bus = NULL;
+      b->last_bus = NULL;
+      b->br_dev = parent;
+      parent->bridge = b;
+      pacc->debug("Tree: bridge %04x:%02x:%02x.%d\n", b->domain,
+        parent->dev->bus, parent->dev->dev, parent->dev->func);
+    }
   *last_br = NULL;
 
   /* Create a bridge tree */
 
   for (b=&host_bridge; b; b=b->chain)
     {
-      struct bridge *c, *best;
-      best = NULL;
+      struct device *br_dev = b->br_dev;
+      struct bridge *c, *best = NULL;
+      struct device *parent = NULL;
+
+      if (br_dev && (br_dev->dev->known_fields & PCI_FILL_PARENT))
+        parent = find_device(br_dev->dev->parent);
+      if (parent)
+        best = parent->bridge;
+      if (!best)
       for (c=&host_bridge; c; c=c->chain)
 	if (c != b && (c == &host_bridge || b->domain == c->domain) &&
 	    b->primary >= c->secondary && b->primary <= c->subordinate &&
diff --git a/lspci.c b/lspci.c
index c4e6c93bc67a..b18ccba89049 100644
--- a/lspci.c
+++ b/lspci.c
@@ -143,7 +143,7 @@ scan_device(struct pci_dev *p)
 	d->config_cached += 64;
     }
   pci_setup_cache(p, d->config, d->config_cached);
-  pci_fill_info(p, PCI_FILL_IDENT | PCI_FILL_CLASS | PCI_FILL_PROGIF | PCI_FILL_REVID | PCI_FILL_SUBSYS);
+  pci_fill_info(p, PCI_FILL_IDENT | PCI_FILL_CLASS | PCI_FILL_PROGIF | PCI_FILL_REVID | PCI_FILL_SUBSYS | (need_topology ? PCI_FILL_PARENT : 0));
   return d;
 }
 
-- 
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 ` [PATCH pciutils 2/4] libpci: sysfs: Implement support for PCI_FILL_PARENT Pali Rohár
2022-01-21 14:22 ` Pali Rohár [this message]
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-4-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).