]> mj.ucw.cz Git - pciutils.git/blobdiff - ls-tree.c
lspci: Add test case for CXL device
[pciutils.git] / ls-tree.c
index aaa1ee99d9f2cef7415c39f05e4a565b541bddfc..af1e659105c8613696a1ff2e290806a908a6721d 100644 (file)
--- 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;
 }
 
   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)
 {
 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;
 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 (! (bus = find_bus(b, p->domain, p->bus)))
+  if (p->known_fields & PCI_FILL_PARENT)
+    parent = find_device(p->parent);
+
+  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)
     {
       struct bridge *c;
       for (c=b->child; c; c=c->next)
@@ -83,7 +107,7 @@ grow_tree(void)
     {
       struct pci_dev *dd = d->dev;
       word class = dd->device_class;
     {
       struct pci_dev *dd = d->dev;
       word class = dd->device_class;
-      byte ht = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
+      byte ht = d->no_config_access ? -1 : (get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f);
       if ((class >> 8) == PCI_BASE_CLASS_BRIDGE &&
          (ht == PCI_HEADER_TYPE_BRIDGE || ht == PCI_HEADER_TYPE_CARDBUS))
        {
       if ((class >> 8) == PCI_BASE_CLASS_BRIDGE &&
          (ht == PCI_HEADER_TYPE_BRIDGE || ht == PCI_HEADER_TYPE_CARDBUS))
        {
@@ -113,14 +137,47 @@ grow_tree(void)
            b->primary, b->secondary, b->subordinate);
        }
     }
            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)
     {
   *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 &&
       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 &&
@@ -162,7 +219,7 @@ print_it(char *line, char *p)
       *p = ' ';
 }
 
       *p = ' ';
 }
 
-static void show_tree_bridge(struct bridge *, char *, char *);
+static void show_tree_bridge(struct pci_filter *filter, struct bridge *, char *, char *);
 
 static char * FORMAT_CHECK(printf, 3, 4)
 tree_printf(char *line, char *p, char *fmt, ...)
 
 static char * FORMAT_CHECK(printf, 3, 4)
 tree_printf(char *line, char *p, char *fmt, ...)
@@ -182,7 +239,11 @@ tree_printf(char *line, char *p, char *fmt, ...)
       p += space;
     }
   else if (res >= space)
       p += space;
     }
   else if (res >= space)
-    p += space;
+    {
+      /* Ancient C libraries do not truncate the output properly. */
+      *(p+space-1) = 0;
+      p += space;
+    }
   else
     p += res;
 
   else
     p += res;
 
@@ -191,7 +252,7 @@ tree_printf(char *line, char *p, char *fmt, ...)
 }
 
 static void
 }
 
 static void
-show_tree_dev(struct device *d, char *line, char *p)
+show_tree_dev(struct pci_filter *filter, struct device *d, char *line, char *p)
 {
   struct pci_dev *q = d->dev;
   struct bridge *b;
 {
   struct pci_dev *q = d->dev;
   struct bridge *b;
@@ -201,11 +262,13 @@ show_tree_dev(struct device *d, char *line, char *p)
   for (b=&host_bridge; b; b=b->chain)
     if (b->br_dev == d)
       {
   for (b=&host_bridge; b; b=b->chain)
     if (b->br_dev == d)
       {
-       if (b->secondary == b->subordinate)
+       if (b->secondary == 0)
+         p = tree_printf(line, p, "-");
+       else if (b->secondary == b->subordinate)
          p = tree_printf(line, p, "-[%02x]-", b->secondary);
        else
          p = tree_printf(line, p, "-[%02x-%02x]-", b->secondary, b->subordinate);
          p = tree_printf(line, p, "-[%02x]-", b->secondary);
        else
          p = tree_printf(line, p, "-[%02x-%02x]-", b->secondary, b->subordinate);
-        show_tree_bridge(b, line, p);
+        show_tree_bridge(filter, b, line, p);
         return;
       }
   if (verbose)
         return;
       }
   if (verbose)
@@ -216,53 +279,127 @@ show_tree_dev(struct device *d, char *line, char *p)
   print_it(line, p);
 }
 
   print_it(line, p);
 }
 
+static int
+check_bus_filter(struct pci_filter *filter, struct bus *b);
+
+static int
+check_dev_filter(struct pci_filter *filter, struct device *d)
+{
+  struct bridge *br;
+  struct bus *b;
+
+  if (!filter)
+    return 1;
+
+  if (pci_filter_match(filter, d->dev))
+    return 1;
+
+  for (br = &host_bridge; br; br = br->chain)
+    if (br->br_dev == d)
+      {
+        for (b = br->first_bus; b; b = b->sibling)
+          if (check_bus_filter(filter, b))
+            return 1;
+        break;
+      }
+
+  return 0;
+}
+
+static int
+check_bus_filter(struct pci_filter *filter, struct bus *b)
+{
+  struct device *d;
+
+  if (!filter)
+    return 1;
+
+  for (d = b->first_dev; d; d = d->bus_next)
+    if (check_dev_filter(filter, d))
+      return 1;
+
+  return 0;
+}
+
 static void
 static void
-show_tree_bus(struct bus *b, char *line, char *p)
+show_tree_bus(struct pci_filter *filter, struct bus *b, char *line, char *p)
 {
   if (!b->first_dev)
     print_it(line, p);
   else if (!b->first_dev->bus_next)
     {
 {
   if (!b->first_dev)
     print_it(line, p);
   else if (!b->first_dev->bus_next)
     {
-      p = tree_printf(line, p, "--");
-      show_tree_dev(b->first_dev, line, p);
+      if (check_dev_filter(filter, b->first_dev))
+        {
+          p = tree_printf(line, p, "--");
+          show_tree_dev(filter, b->first_dev, line, p);
+        }
+      else
+        print_it(line, p);
     }
   else
     {
     }
   else
     {
+      int empty = 1;
       struct device *d = b->first_dev;
       while (d->bus_next)
        {
       struct device *d = b->first_dev;
       while (d->bus_next)
        {
-         char *p2 = tree_printf(line, p, "+-");
-         show_tree_dev(d, line, p2);
+         if (check_dev_filter(filter, d))
+           {
+             char *p2 = tree_printf(line, p, "+-");
+             show_tree_dev(filter, d, line, p2);
+             empty = 0;
+           }
          d = d->bus_next;
        }
          d = d->bus_next;
        }
-      p = tree_printf(line, p, "\\-");
-      show_tree_dev(d, line, p);
+      if (check_dev_filter(filter, d))
+        {
+          p = tree_printf(line, p, "\\-");
+          show_tree_dev(filter, d, line, p);
+          empty = 0;
+        }
+      if (empty)
+        print_it(line, p);
     }
 }
 
 static void
     }
 }
 
 static void
-show_tree_bridge(struct bridge *b, char *line, char *p)
+show_tree_bridge(struct pci_filter *filter, struct bridge *b, char *line, char *p)
 {
   *p++ = '-';
   if (!b->first_bus->sibling)
     {
 {
   *p++ = '-';
   if (!b->first_bus->sibling)
     {
-      if (b == &host_bridge)
-        p = tree_printf(line, p, "[%04x:%02x]-", b->domain, b->first_bus->number);
-      show_tree_bus(b->first_bus, line, p);
+      if (check_bus_filter(filter, b->first_bus))
+        {
+          if (b == &host_bridge)
+            p = tree_printf(line, p, "[%04x:%02x]-", b->domain, b->first_bus->number);
+          show_tree_bus(filter, b->first_bus, line, p);
+        }
+      else
+        print_it(line, p);
     }
   else
     {
     }
   else
     {
+      int empty = 1;
       struct bus *u = b->first_bus;
       char *k;
 
       while (u->sibling)
         {
       struct bus *u = b->first_bus;
       char *k;
 
       while (u->sibling)
         {
-          k = tree_printf(line, p, "+-[%04x:%02x]-", u->domain, u->number);
-          show_tree_bus(u, line, k);
+          if (check_bus_filter(filter, u))
+            {
+              k = tree_printf(line, p, "+-[%04x:%02x]-", u->domain, u->number);
+              show_tree_bus(filter, u, line, k);
+              empty = 0;
+            }
           u = u->sibling;
         }
           u = u->sibling;
         }
-      k = tree_printf(line, p, "\\-[%04x:%02x]-", u->domain, u->number);
-      show_tree_bus(u, line, k);
+      if (check_bus_filter(filter, u))
+        {
+          k = tree_printf(line, p, "\\-[%04x:%02x]-", u->domain, u->number);
+          show_tree_bus(filter, u, line, k);
+          empty = 0;
+        }
+      if (empty)
+        print_it(line, p);
     }
 }
 
     }
 }
 
@@ -270,20 +407,5 @@ void
 show_forest(struct pci_filter *filter)
 {
   char line[LINE_BUF_SIZE];
 show_forest(struct pci_filter *filter)
 {
   char line[LINE_BUF_SIZE];
-  if (filter == NULL)
-    show_tree_bridge(&host_bridge, line, line);
-  else
-    {
-      struct bridge *b;
-      for (b=&host_bridge; b; b=b->chain)
-        {
-          if (b->br_dev && pci_filter_match(filter, b->br_dev->dev))
-            {
-                struct pci_dev *d = b->br_dev->dev;
-                char *p = line;
-                p = tree_printf(line, p, "%04x:%02x:", d->domain_16, d->bus);
-                show_tree_dev(b->br_dev, line, p);
-            }
-        }
-    }
+  show_tree_bridge(filter, &host_bridge, line, line);
 }
 }