forked from pciutils/pciutils
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ls-tree.c
241 lines (216 loc) · 5.64 KB
/
ls-tree.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
* The PCI Utilities -- Show Bus Tree
*
* Copyright (c) 1997--2018 Martin Mares <[email protected]>
*
* Can be freely distributed and used under the terms of the GNU GPL.
*/
#include <stdio.h>
#include <string.h>
#include "lspci.h"
struct bridge host_bridge = { NULL, NULL, NULL, NULL, 0, ~0, 0, ~0, NULL };
static struct bus *
find_bus(struct bridge *b, unsigned int domain, unsigned int n)
{
struct bus *bus;
for (bus=b->first_bus; bus; bus=bus->sibling)
if (bus->domain == domain && bus->number == n)
break;
return bus;
}
static struct bus *
new_bus(struct bridge *b, unsigned int domain, unsigned int n)
{
struct bus *bus = xmalloc(sizeof(struct bus));
bus->domain = domain;
bus->number = n;
bus->sibling = b->first_bus;
bus->first_dev = NULL;
bus->last_dev = &bus->first_dev;
bus->parent_bridge = b;
b->first_bus = bus;
return bus;
}
static void
insert_dev(struct device *d, struct bridge *b)
{
struct pci_dev *p = d->dev;
struct bus *bus;
if (! (bus = find_bus(b, p->domain, p->bus)))
{
struct bridge *c;
for (c=b->child; c; c=c->next)
if (c->domain == (unsigned)p->domain && c->secondary <= p->bus && p->bus <= c->subordinate)
{
insert_dev(d, c);
return;
}
bus = new_bus(b, p->domain, p->bus);
}
/* Simple insertion at the end _does_ guarantee the correct order as the
* original device list was sorted by (domain, bus, devfn) lexicographically
* and all devices on the new list have the same bus number.
*/
*bus->last_dev = d;
bus->last_dev = &d->bus_next;
d->bus_next = NULL;
d->parent_bus = bus;
}
void
grow_tree(void)
{
struct device *d;
struct bridge **last_br, *b;
/* Build list of bridges */
last_br = &host_bridge.chain;
for (d=first_dev; d; d=d->next)
{
struct pci_dev *dd = d->dev;
word class = dd->device_class;
byte ht = 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))
{
b = xmalloc(sizeof(struct bridge));
b->domain = dd->domain;
if (ht == PCI_HEADER_TYPE_BRIDGE)
{
b->primary = get_conf_byte(d, PCI_PRIMARY_BUS);
b->secondary = get_conf_byte(d, PCI_SECONDARY_BUS);
b->subordinate = get_conf_byte(d, PCI_SUBORDINATE_BUS);
}
else
{
b->primary = get_conf_byte(d, PCI_CB_PRIMARY_BUS);
b->secondary = get_conf_byte(d, PCI_CB_CARD_BUS);
b->subordinate = get_conf_byte(d, PCI_CB_SUBORDINATE_BUS);
}
*last_br = b;
last_br = &b->chain;
b->next = b->child = NULL;
b->first_bus = NULL;
b->br_dev = d;
d->bridge = b;
pacc->debug("Tree: bridge %04x:%02x:%02x.%d: %02x -> %02x-%02x\n",
dd->domain, dd->bus, dd->dev, dd->func,
b->primary, b->secondary, b->subordinate);
}
}
*last_br = NULL;
/* Create a bridge tree */
for (b=&host_bridge; b; b=b->chain)
{
struct bridge *c, *best;
best = NULL;
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 &&
(!best || best->subordinate - best->primary > c->subordinate - c->primary))
best = c;
if (best)
{
b->next = best->child;
best->child = b;
}
}
/* Insert secondary bus for each bridge */
for (b=&host_bridge; b; b=b->chain)
if (!find_bus(b, b->domain, b->secondary))
new_bus(b, b->domain, b->secondary);
/* Create bus structs and link devices */
for (d=first_dev; d; d=d->next)
insert_dev(d, &host_bridge);
}
static void
print_it(char *line, char *p)
{
*p++ = '\n';
*p = 0;
fputs(line, stdout);
for (p=line; *p; p++)
if (*p == '+' || *p == '|')
*p = '|';
else
*p = ' ';
}
static void show_tree_bridge(struct bridge *, char *, char *);
static void
show_tree_dev(struct device *d, char *line, char *p)
{
struct pci_dev *q = d->dev;
struct bridge *b;
char namebuf[256];
p += sprintf(p, "%02x.%x", q->dev, q->func);
for (b=&host_bridge; b; b=b->chain)
if (b->br_dev == d)
{
if (b->secondary == b->subordinate)
p += sprintf(p, "-[%02x]-", b->secondary);
else
p += sprintf(p, "-[%02x-%02x]-", b->secondary, b->subordinate);
show_tree_bridge(b, line, p);
return;
}
if (verbose)
p += sprintf(p, " %s",
pci_lookup_name(pacc, namebuf, sizeof(namebuf),
PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
q->vendor_id, q->device_id));
print_it(line, p);
}
static void
show_tree_bus(struct bus *b, char *line, char *p)
{
if (!b->first_dev)
print_it(line, p);
else if (!b->first_dev->bus_next)
{
*p++ = '-';
*p++ = '-';
show_tree_dev(b->first_dev, line, p);
}
else
{
struct device *d = b->first_dev;
while (d->bus_next)
{
p[0] = '+';
p[1] = '-';
show_tree_dev(d, line, p+2);
d = d->bus_next;
}
p[0] = '\\';
p[1] = '-';
show_tree_dev(d, line, p+2);
}
}
static void
show_tree_bridge(struct bridge *b, char *line, char *p)
{
*p++ = '-';
if (!b->first_bus->sibling)
{
if (b == &host_bridge)
p += sprintf(p, "[%04x:%02x]-", b->domain, b->first_bus->number);
show_tree_bus(b->first_bus, line, p);
}
else
{
struct bus *u = b->first_bus;
char *k;
while (u->sibling)
{
k = p + sprintf(p, "+-[%04x:%02x]-", u->domain, u->number);
show_tree_bus(u, line, k);
u = u->sibling;
}
k = p + sprintf(p, "\\-[%04x:%02x]-", u->domain, u->number);
show_tree_bus(u, line, k);
}
}
void
show_forest(void)
{
char line[256];
show_tree_bridge(&host_bridge, line, line);
}