]> mj.ucw.cz Git - pciutils.git/blob - ls-tree.c
lspci: Show Slot Power Limit values above EFh
[pciutils.git] / ls-tree.c
1 /*
2  *      The PCI Utilities -- Show Bus Tree
3  *
4  *      Copyright (c) 1997--2020 Martin Mares <mj@ucw.cz>
5  *
6  *      Can be freely distributed and used under the terms of the GNU GPL.
7  */
8
9 #include <stdarg.h>
10 #include <stdio.h>
11 #include <string.h>
12
13 #include "lspci.h"
14
15 struct bridge host_bridge = { NULL, NULL, NULL, NULL, NULL, 0, ~0, 0, ~0, NULL };
16
17 static struct bus *
18 find_bus(struct bridge *b, unsigned int domain, unsigned int n)
19 {
20   struct bus *bus;
21
22   for (bus=b->first_bus; bus; bus=bus->sibling)
23     if (bus->domain == domain && bus->number == n)
24       break;
25   return bus;
26 }
27
28 static struct bus *
29 new_bus(struct bridge *b, unsigned int domain, unsigned int n)
30 {
31   struct bus *bus = xmalloc(sizeof(struct bus));
32   bus->domain = domain;
33   bus->number = n;
34   bus->sibling = NULL;
35   bus->first_dev = NULL;
36   bus->last_dev = &bus->first_dev;
37   bus->parent_bridge = b;
38   if (b->last_bus)
39     b->last_bus->sibling = bus;
40   b->last_bus = bus;
41   if (!b->first_bus)
42     b->first_bus = bus;
43   return bus;
44 }
45
46 static void
47 insert_dev(struct device *d, struct bridge *b)
48 {
49   struct pci_dev *p = d->dev;
50   struct bus *bus;
51
52   if (! (bus = find_bus(b, p->domain, p->bus)))
53     {
54       struct bridge *c;
55       for (c=b->child; c; c=c->next)
56         if (c->domain == (unsigned)p->domain && c->secondary <= p->bus && p->bus <= c->subordinate)
57           {
58             insert_dev(d, c);
59             return;
60           }
61       bus = new_bus(b, p->domain, p->bus);
62     }
63   /* Simple insertion at the end _does_ guarantee the correct order as the
64    * original device list was sorted by (domain, bus, devfn) lexicographically
65    * and all devices on the new list have the same bus number.
66    */
67   *bus->last_dev = d;
68   bus->last_dev = &d->bus_next;
69   d->bus_next = NULL;
70   d->parent_bus = bus;
71 }
72
73 void
74 grow_tree(void)
75 {
76   struct device *d;
77   struct bridge **last_br, *b;
78
79   /* Build list of bridges */
80
81   last_br = &host_bridge.chain;
82   for (d=first_dev; d; d=d->next)
83     {
84       struct pci_dev *dd = d->dev;
85       word class = dd->device_class;
86       byte ht = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
87       if ((class >> 8) == PCI_BASE_CLASS_BRIDGE &&
88           (ht == PCI_HEADER_TYPE_BRIDGE || ht == PCI_HEADER_TYPE_CARDBUS))
89         {
90           b = xmalloc(sizeof(struct bridge));
91           b->domain = dd->domain;
92           if (ht == PCI_HEADER_TYPE_BRIDGE)
93             {
94               b->primary = get_conf_byte(d, PCI_PRIMARY_BUS);
95               b->secondary = get_conf_byte(d, PCI_SECONDARY_BUS);
96               b->subordinate = get_conf_byte(d, PCI_SUBORDINATE_BUS);
97             }
98           else
99             {
100               b->primary = get_conf_byte(d, PCI_CB_PRIMARY_BUS);
101               b->secondary = get_conf_byte(d, PCI_CB_CARD_BUS);
102               b->subordinate = get_conf_byte(d, PCI_CB_SUBORDINATE_BUS);
103             }
104           *last_br = b;
105           last_br = &b->chain;
106           b->next = b->child = NULL;
107           b->first_bus = NULL;
108           b->last_bus = NULL;
109           b->br_dev = d;
110           d->bridge = b;
111           pacc->debug("Tree: bridge %04x:%02x:%02x.%d: %02x -> %02x-%02x\n",
112             dd->domain, dd->bus, dd->dev, dd->func,
113             b->primary, b->secondary, b->subordinate);
114         }
115     }
116   *last_br = NULL;
117
118   /* Create a bridge tree */
119
120   for (b=&host_bridge; b; b=b->chain)
121     {
122       struct bridge *c, *best;
123       best = NULL;
124       for (c=&host_bridge; c; c=c->chain)
125         if (c != b && (c == &host_bridge || b->domain == c->domain) &&
126             b->primary >= c->secondary && b->primary <= c->subordinate &&
127             (!best || best->subordinate - best->primary > c->subordinate - c->primary))
128           best = c;
129       if (best)
130         {
131           b->next = best->child;
132           best->child = b;
133         }
134     }
135
136   /* Insert secondary bus for each bridge */
137
138   for (b=&host_bridge; b; b=b->chain)
139     if (!find_bus(b, b->domain, b->secondary))
140       new_bus(b, b->domain, b->secondary);
141
142   /* Create bus structs and link devices */
143
144   for (d=first_dev; d; d=d->next)
145     insert_dev(d, &host_bridge);
146 }
147
148 static void
149 print_it(char *line, char *p)
150 {
151   *p++ = '\n';
152   *p = 0;
153   fputs(line, stdout);
154   for (p=line; *p; p++)
155     if (*p == '+' || *p == '|')
156       *p = '|';
157     else
158       *p = ' ';
159 }
160
161 static void show_tree_bridge(struct bridge *, char *, char *);
162
163 #define LINE_BUF_SIZE 1024
164
165 static char * FORMAT_CHECK(printf, 3, 4)
166 tree_printf(char *line, char *p, char *fmt, ...)
167 {
168   va_list args;
169   char *end = line + LINE_BUF_SIZE - 2;
170
171   if (p >= end)
172     return p;
173
174   va_start(args, fmt);
175   int res = vsnprintf(p, end - p, fmt, args);
176   if (res < 0)
177     {
178       /* Ancient C libraries return -1 on overflow */
179       p += strlen(p);
180     }
181   else
182     p += res;
183
184   va_end(args);
185   return p;
186 }
187
188 static void
189 show_tree_dev(struct device *d, char *line, char *p)
190 {
191   struct pci_dev *q = d->dev;
192   struct bridge *b;
193   char namebuf[256];
194
195   p = tree_printf(line, p, "%02x.%x", q->dev, q->func);
196   for (b=&host_bridge; b; b=b->chain)
197     if (b->br_dev == d)
198       {
199         if (b->secondary == b->subordinate)
200           p = tree_printf(line, p, "-[%02x]-", b->secondary);
201         else
202           p = tree_printf(line, p, "-[%02x-%02x]-", b->secondary, b->subordinate);
203         show_tree_bridge(b, line, p);
204         return;
205       }
206   if (verbose)
207     p = tree_printf(line, p, "  %s",
208                     pci_lookup_name(pacc, namebuf, sizeof(namebuf),
209                                     PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
210                                     q->vendor_id, q->device_id));
211   print_it(line, p);
212 }
213
214 static void
215 show_tree_bus(struct bus *b, char *line, char *p)
216 {
217   if (!b->first_dev)
218     print_it(line, p);
219   else if (!b->first_dev->bus_next)
220     {
221       p = tree_printf(line, p, "--");
222       show_tree_dev(b->first_dev, line, p);
223     }
224   else
225     {
226       struct device *d = b->first_dev;
227       while (d->bus_next)
228         {
229           char *p2 = tree_printf(line, p, "+-");
230           show_tree_dev(d, line, p2);
231           d = d->bus_next;
232         }
233       p = tree_printf(line, p, "\\-");
234       show_tree_dev(d, line, p);
235     }
236 }
237
238 static void
239 show_tree_bridge(struct bridge *b, char *line, char *p)
240 {
241   *p++ = '-';
242   if (!b->first_bus->sibling)
243     {
244       if (b == &host_bridge)
245         p = tree_printf(line, p, "[%04x:%02x]-", b->domain, b->first_bus->number);
246       show_tree_bus(b->first_bus, line, p);
247     }
248   else
249     {
250       struct bus *u = b->first_bus;
251       char *k;
252
253       while (u->sibling)
254         {
255           k = tree_printf(line, p, "+-[%04x:%02x]-", u->domain, u->number);
256           show_tree_bus(u, line, k);
257           u = u->sibling;
258         }
259       k = tree_printf(line, p, "\\-[%04x:%02x]-", u->domain, u->number);
260       show_tree_bus(u, line, k);
261     }
262 }
263
264 void
265 show_forest(struct pci_filter *filter)
266 {
267   char line[LINE_BUF_SIZE];
268   if (filter == NULL)
269     show_tree_bridge(&host_bridge, line, line);
270   else
271     {
272       struct bridge *b;
273       for (b=&host_bridge; b; b=b->chain)
274         {
275           if (b->br_dev && pci_filter_match(filter, b->br_dev->dev))
276             {
277                 struct pci_dev *d = b->br_dev->dev;
278                 char *p = line;
279                 p = tree_printf(line, p, "%04x:%02x:", d->domain_16, d->bus);
280                 show_tree_dev(b->br_dev, line, p);
281             }
282         }
283     }
284 }