2 * The PCI Utilities -- Show Bus Tree
4 * Copyright (c) 1997--2021 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
15 struct bridge host_bridge = { NULL, NULL, NULL, NULL, NULL, 0, ~0, 0, ~0, NULL };
18 find_bus(struct bridge *b, unsigned int domain, unsigned int n)
22 for (bus=b->first_bus; bus; bus=bus->sibling)
23 if (bus->domain == domain && bus->number == n)
28 static struct device *
29 find_device(struct pci_dev *dd)
35 for (d=first_dev; d; d=d->next)
42 new_bus(struct bridge *b, unsigned int domain, unsigned int n)
44 struct bus *bus = xmalloc(sizeof(struct bus));
48 bus->first_dev = NULL;
49 bus->last_dev = &bus->first_dev;
50 bus->parent_bridge = b;
52 b->last_bus->sibling = bus;
60 insert_dev(struct device *d, struct bridge *b)
62 struct pci_dev *p = d->dev;
63 struct device *parent = NULL;
64 struct bus *bus = NULL;
66 if (p->known_fields & PCI_FILL_PARENT)
67 parent = find_device(p->parent);
69 if (parent && parent->bridge)
71 bus = parent->bridge->first_bus;
73 bus = new_bus(parent->bridge, p->domain, p->bus);
76 if (!bus && ! (bus = find_bus(b, p->domain, p->bus)))
79 for (c=b->child; c; c=c->next)
80 if (c->domain == (unsigned)p->domain && c->secondary <= p->bus && p->bus <= c->subordinate)
85 bus = new_bus(b, p->domain, p->bus);
87 /* Simple insertion at the end _does_ guarantee the correct order as the
88 * original device list was sorted by (domain, bus, devfn) lexicographically
89 * and all devices on the new list have the same bus number.
92 bus->last_dev = &d->bus_next;
101 struct bridge **last_br, *b;
103 /* Build list of bridges */
105 last_br = &host_bridge.chain;
106 for (d=first_dev; d; d=d->next)
108 struct pci_dev *dd = d->dev;
109 word class = dd->device_class;
110 byte ht = d->no_config_access ? -1 : (get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f);
111 if ((class >> 8) == PCI_BASE_CLASS_BRIDGE &&
112 (ht == PCI_HEADER_TYPE_BRIDGE || ht == PCI_HEADER_TYPE_CARDBUS))
114 b = xmalloc(sizeof(struct bridge));
115 b->domain = dd->domain;
116 b->primary = dd->bus;
117 if (ht == PCI_HEADER_TYPE_BRIDGE)
119 b->secondary = get_conf_byte(d, PCI_SECONDARY_BUS);
120 b->subordinate = get_conf_byte(d, PCI_SUBORDINATE_BUS);
124 b->secondary = get_conf_byte(d, PCI_CB_CARD_BUS);
125 b->subordinate = get_conf_byte(d, PCI_CB_SUBORDINATE_BUS);
129 b->next = b->child = NULL;
134 pacc->debug("Tree: bridge %04x:%02x:%02x.%d: %02x -> %02x-%02x\n",
135 dd->domain, dd->bus, dd->dev, dd->func,
136 b->primary, b->secondary, b->subordinate);
140 /* Append additional bridges reported by libpci via d->parent */
142 for (d=first_dev; d; d=d->next)
144 struct device *parent = NULL;
145 if (d->dev->known_fields & PCI_FILL_PARENT)
146 parent = find_device(d->dev->parent);
147 if (!parent || parent->bridge)
149 b = xmalloc(sizeof(struct bridge));
150 b->domain = parent->dev->domain;
151 b->primary = parent->dev->bus;
152 b->secondary = d->dev->bus;
153 /* At this stage subordinate number is unknown, so set it to secondary bus number. */
154 b->subordinate = b->secondary;
157 b->next = b->child = NULL;
162 pacc->debug("Tree: bridge %04x:%02x:%02x.%d\n", b->domain,
163 parent->dev->bus, parent->dev->dev, parent->dev->func);
167 /* Create a bridge tree */
169 for (b=&host_bridge; b; b=b->chain)
171 struct device *br_dev = b->br_dev;
172 struct bridge *c, *best = NULL;
173 struct device *parent = NULL;
175 if (br_dev && (br_dev->dev->known_fields & PCI_FILL_PARENT))
176 parent = find_device(br_dev->dev->parent);
178 best = parent->bridge;
180 for (c=&host_bridge; c; c=c->chain)
181 if (c != b && (c == &host_bridge || b->domain == c->domain) &&
182 b->primary >= c->secondary && b->primary <= c->subordinate &&
183 (!best || best->subordinate - best->primary > c->subordinate - c->primary))
187 b->next = best->child;
192 /* Insert secondary bus for each bridge */
194 for (b=&host_bridge; b; b=b->chain)
195 if (!find_bus(b, b->domain, b->secondary))
196 new_bus(b, b->domain, b->secondary);
198 /* Create bus structs and link devices */
200 for (d=first_dev; d; d=d->next)
201 insert_dev(d, &host_bridge);
204 #define LINE_BUF_SIZE 1024
207 print_it(char *line, char *p)
211 if (p >= line + LINE_BUF_SIZE - 1)
212 fputs("...", stdout);
214 for (p=line; *p; p++)
215 if (*p == '+' || *p == '|')
221 static void show_tree_bridge(struct pci_filter *filter, struct bridge *, char *, char *);
223 static char * FORMAT_CHECK(printf, 3, 4)
224 tree_printf(char *line, char *p, char *fmt, ...)
227 int space = line + LINE_BUF_SIZE - 1 - p;
233 int res = vsnprintf(p, space, fmt, args);
236 /* Ancient C libraries return -1 on overflow and they do not truncate the output properly. */
240 else if (res >= space)
242 /* Ancient C libraries do not truncate the output properly. */
254 show_tree_dev(struct pci_filter *filter, struct device *d, char *line, char *p)
256 struct pci_dev *q = d->dev;
260 p = tree_printf(line, p, "%02x.%x", q->dev, q->func);
261 for (b=&host_bridge; b; b=b->chain)
264 if (b->secondary == 0)
265 p = tree_printf(line, p, "-");
266 else if (b->secondary == b->subordinate)
267 p = tree_printf(line, p, "-[%02x]-", b->secondary);
269 p = tree_printf(line, p, "-[%02x-%02x]-", b->secondary, b->subordinate);
270 show_tree_bridge(filter, b, line, p);
274 p = tree_printf(line, p, " %s",
275 pci_lookup_name(pacc, namebuf, sizeof(namebuf),
276 PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
277 q->vendor_id, q->device_id));
281 static struct pci_filter *
282 get_filter_for_child(struct pci_filter *filter, struct device *d)
287 if (pci_filter_match(filter, d->dev))
294 check_bus_filter(struct pci_filter *filter, struct bus *b);
297 check_dev_filter(struct pci_filter *filter, struct device *d)
305 if (pci_filter_match(filter, d->dev))
308 for (br = &host_bridge; br; br = br->chain)
311 for (b = br->first_bus; b; b = b->sibling)
312 if (check_bus_filter(filter, b))
321 check_bus_filter(struct pci_filter *filter, struct bus *b)
328 for (d = b->first_dev; d; d = d->bus_next)
329 if (check_dev_filter(filter, d))
336 show_tree_bus(struct pci_filter *filter, struct bus *b, char *line, char *p)
340 else if (!b->first_dev->bus_next)
342 if (check_dev_filter(filter, b->first_dev))
344 p = tree_printf(line, p, "--");
345 show_tree_dev(get_filter_for_child(filter, b->first_dev), b->first_dev, line, p);
353 struct device *d = b->first_dev;
357 if (check_dev_filter(filter, d))
363 for (i = 0, d = b->first_dev; d; d = d->bus_next)
365 if (!check_dev_filter(filter, d))
367 char *p2 = tree_printf(line, p, count == 1 ? "--" : count == i+1 ? "\\-" : "+-");
368 show_tree_dev(get_filter_for_child(filter, d), d, line, p2);
378 show_tree_bridge(struct pci_filter *filter, struct bridge *b, char *line, char *p)
381 if (!b->first_bus->sibling)
383 if (check_bus_filter(filter, b->first_bus))
385 if (b == &host_bridge)
386 p = tree_printf(line, p, "[%04x:%02x]-", b->domain, b->first_bus->number);
387 show_tree_bus(filter, b->first_bus, line, p);
395 struct bus *u = b->first_bus;
400 if (check_bus_filter(filter, u))
406 for (i = 0, u = b->first_bus; u; u = u->sibling)
408 if (!check_bus_filter(filter, u))
410 k = tree_printf(line, p, count == 1 ? "[%04x:%02x]-" : count == i+1 ? "\\-[%04x:%02x]-" : "+-[%04x:%02x]-", u->domain, u->number);
411 show_tree_bus(filter, u, line, k);
421 show_forest(struct pci_filter *filter)
423 char line[LINE_BUF_SIZE];
424 show_tree_bridge(filter, &host_bridge, line, line);