]> mj.ucw.cz Git - pciutils.git/blob - lspci.c
2dc6ae4bd24e8f13f3a212858113feed96cfa365
[pciutils.git] / lspci.c
1 /*
2  *      $Id: lspci.c,v 1.6 1998/02/08 10:58:54 mj Exp $
3  *
4  *      Linux PCI Utilities -- List All PCI Devices
5  *
6  *      Copyright (c) 1997, 1998 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
7  *
8  *      Can be freely distributed and used under the terms of the GNU GPL.
9  */
10
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <fcntl.h>
15 #include <unistd.h>
16 #include <linux/pci.h>
17
18 #include "pciutils.h"
19
20 /* Options */
21
22 static int verbose;                     /* Show detailed information */
23 static int buscentric_view;             /* Show bus addresses/IRQ's instead of CPU-visible ones */
24 static int show_hex;                    /* Show contents of config space as hexadecimal numbers */
25 static int bus_filter = -1;             /* Bus, slot, function, vendor and device ID filtering */
26 static int slot_filter = -1;
27 static int func_filter = -1;
28 static int vend_filter = -1;
29 static int dev_filter = -1;
30 static int show_tree;                   /* Show bus tree */
31 static char *pci_dir = PROC_BUS_PCI;
32
33 static char options[] = "nvbxB:S:F:V:D:ti:p:";
34
35 static char help_msg[] = "\
36 Usage: lspci [<switches>]\n\
37 \n\
38 -v\tBe verbose\n\
39 -n\tShow numeric ID's\n\
40 -b\tBus-centric view (PCI addresses and IRQ's instead of those seen by the CPU)\n\
41 -x\tShow hex-dump of config space (-xx shows full 256 bytes)\n\
42 -B <bus>, -S <slot>, -F <func>, -V <vendor>, -D <device>  Show only selected devices\n\
43 -t\tShow bus tree\n\
44 -i <file>\tUse specified ID database instead of " ETC_PCI_IDS "\n\
45 -p <dir>\tUse specified bus directory instead of " PROC_BUS_PCI "\n\
46 ";
47
48 /* Format strings used for IRQ numbers */
49
50 #ifdef ARCH_SPARC64
51 #define IRQ_FORMAT "%08x"
52 #else
53 #define IRQ_FORMAT "%d"
54 #endif
55
56 /* Our view of the PCI bus */
57
58 struct device {
59   struct device *next;
60   byte bus, devfn;
61   word vendid, devid;
62   unsigned int kernel_irq;
63   unsigned long kernel_base_addr[6];
64   byte config[256];
65 };
66
67 static struct device *first_dev, **last_dev = &first_dev;
68
69 /* Miscellaneous routines */
70
71 void *
72 xmalloc(unsigned int howmuch)
73 {
74   void *p = malloc(howmuch);
75   if (!p)
76     {
77       fprintf(stderr, "lspci: Unable to allocate %d bytes of memory\n", howmuch);
78       exit(1);
79     }
80   return p;
81 }
82
83 /* Filtering */
84
85 static inline int
86 filter_out(struct device *d)
87 {
88   return (bus_filter >= 0 && d->bus != bus_filter ||
89           slot_filter >= 0 && PCI_SLOT(d->devfn) != slot_filter ||
90           func_filter >= 0 && PCI_FUNC(d->devfn) != func_filter ||
91           vend_filter >= 0 && d->vendid != vend_filter ||
92           dev_filter >= 0 && d->devid != dev_filter);
93 }
94
95 /* Interface for /proc/bus/pci */
96
97 static void
98 scan_dev_list(void)
99 {
100   FILE *f;
101   byte line[256];
102   byte name[256];
103
104   sprintf(name, "%s/devices", pci_dir);
105   if (! (f = fopen(name, "r")))
106     {
107       perror(name);
108       exit(1);
109     }
110   while (fgets(line, sizeof(line), f))
111     {
112       struct device *d = xmalloc(sizeof(struct device));
113       unsigned int dfn, vend;
114
115       sscanf(line, "%x %x %x %lx %lx %lx %lx %lx %lx",
116              &dfn,
117              &vend,
118              &d->kernel_irq,
119              &d->kernel_base_addr[0],
120              &d->kernel_base_addr[1],
121              &d->kernel_base_addr[2],
122              &d->kernel_base_addr[3],
123              &d->kernel_base_addr[4],
124              &d->kernel_base_addr[5]);
125       d->bus = dfn >> 8U;
126       d->devfn = dfn & 0xff;
127       d->vendid = vend >> 16U;
128       d->devid = vend & 0xffff;
129       if (!filter_out(d))
130         {
131           *last_dev = d;
132           last_dev = &d->next;
133           d->next = NULL;
134         }
135     }
136   fclose(f);
137 }
138
139 static inline void
140 make_proc_pci_name(struct device *d, char *p)
141 {
142   sprintf(p, "%s/%02x/%02x.%x",
143           pci_dir, d->bus, PCI_SLOT(d->devfn), PCI_FUNC(d->devfn));
144 }
145
146 static void
147 scan_config(void)
148 {
149   struct device *d;
150   char name[64];
151   int fd, res;
152   int how_much = (show_hex > 1) ? 256 : 64;
153
154   for(d=first_dev; d; d=d->next)
155     {
156       make_proc_pci_name(d, name);
157       if ((fd = open(name, O_RDONLY)) < 0)
158         {
159           fprintf(stderr, "lspci: Unable to open %s: %m\n", name);
160           exit(1);
161         }
162       res = read(fd, d->config, how_much);
163       if (res < 0)
164         {
165           fprintf(stderr, "lspci: Error reading %s: %m\n", name);
166           exit(1);
167         }
168       if (res != how_much)
169         {
170           fprintf(stderr, "lspci: Only %d bytes of config space available to you\n", res);
171           exit(1);
172         }
173       close(fd);
174     }
175 }
176
177 static void
178 scan_proc(void)
179 {
180   scan_dev_list();
181   scan_config();
182 }
183
184 /* Config space accesses */
185
186 static inline byte
187 get_conf_byte(struct device *d, unsigned int pos)
188 {
189   return d->config[pos];
190 }
191
192 static word
193 get_conf_word(struct device *d, unsigned int pos)
194 {
195   return d->config[pos] | (d->config[pos+1] << 8);
196 }
197
198 static u32
199 get_conf_long(struct device *d, unsigned int pos)
200 {
201   return d->config[pos] |
202     (d->config[pos+1] << 8) |
203     (d->config[pos+2] << 16) |
204     (d->config[pos+3] << 24);
205 }
206
207 /* Sorting */
208
209 static int
210 compare_them(const void *A, const void *B)
211 {
212   const struct device *a = *(const struct device **)A;
213   const struct device *b = *(const struct device **)B;
214
215   if (a->bus < b->bus)
216     return -1;
217   if (a->bus > b->bus)
218     return 1;
219   if (a->devfn < b->devfn)
220     return -1;
221   if (a->devfn > b->devfn)
222     return 1;
223   return 0;
224 }
225
226 static void
227 sort_them(void)
228 {
229   struct device **index, **h;
230   int cnt;
231   struct device *d;
232
233   cnt = 0;
234   for(d=first_dev; d; d=d->next)
235     cnt++;
236   h = index = alloca(sizeof(struct device *) * cnt);
237   for(d=first_dev; d; d=d->next)
238     *h++ = d;
239   qsort(index, cnt, sizeof(struct device *), compare_them);
240   last_dev = &first_dev;
241   h = index;
242   while (cnt--)
243     {
244       *last_dev = *h;
245       last_dev = &(*h)->next;
246       h++;
247     }
248   *last_dev = NULL;
249 }
250
251 /* Normal output */
252
253 static void
254 show_terse(struct device *d)
255 {
256   int c;
257
258   printf("%02x:%02x.%x %s: %s",
259          d->bus,
260          PCI_SLOT(d->devfn),
261          PCI_FUNC(d->devfn),
262          lookup_class(get_conf_word(d, PCI_CLASS_DEVICE)),
263          lookup_device_full(d->vendid, d->devid));
264   if (c = get_conf_byte(d, PCI_REVISION_ID))
265     printf(" (rev %02x)", c);
266   if (verbose && (c = get_conf_byte(d, PCI_CLASS_PROG)))
267     printf(" (prog-if %02x)", c);
268   putchar('\n');
269 }
270
271 static void
272 show_bases(struct device *d, int cnt)
273 {
274   word cmd = get_conf_word(d, PCI_COMMAND);
275   int i;
276
277   for(i=0; i<6; i++)
278     {
279       unsigned long pos;
280       unsigned int flg = get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i);
281       if (buscentric_view)
282         pos = flg;
283       else
284         pos = d->kernel_base_addr[i];
285       if (!pos || pos == 0xffffffff)
286         continue;
287       if (flg & PCI_BASE_ADDRESS_SPACE_IO)
288         {
289           if (cmd & PCI_COMMAND_IO)
290             {
291               if (verbose > 1)
292                 printf("\tRegion %d: ", i);
293               else
294                 putchar('\t');
295               printf("I/O ports at %04lx\n", pos & PCI_BASE_ADDRESS_IO_MASK);
296             }
297         }
298       else if (cmd & PCI_COMMAND_MEMORY)
299         {
300           int t = flg & PCI_BASE_ADDRESS_MEM_TYPE_MASK;
301           if (verbose > 1)
302             printf("\tRegion %d: ", i);
303           else
304             putchar('\t');
305           printf("Memory at ");
306           if (t == PCI_BASE_ADDRESS_MEM_TYPE_64)
307             {
308               if (i < cnt - 1)
309                 {
310                   i++;
311                   if (!buscentric_view)
312                     printf("%08x", get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i));
313                 }
314               else
315                 printf("????????");
316             }
317           printf("%08lx (%s, %sprefetchable)\n",
318                  pos & PCI_BASE_ADDRESS_MEM_MASK,
319                  (t == PCI_BASE_ADDRESS_MEM_TYPE_32) ? "32-bit" :
320                  (t == PCI_BASE_ADDRESS_MEM_TYPE_64) ? "64-bit" :
321                  (t == PCI_BASE_ADDRESS_MEM_TYPE_1M) ? "low-1M 32-bit" : "???",
322                  (flg & PCI_BASE_ADDRESS_MEM_PREFETCH) ? "" : "non-");
323         }
324     }
325 }
326
327 static void
328 show_htype0(struct device *d)
329 {
330   u32 rom = get_conf_long(d, PCI_ROM_ADDRESS);
331
332   show_bases(d, 6);
333
334   if (rom & 1)
335     {
336       word cmd = get_conf_word(d, PCI_COMMAND);
337       printf("\tExpansion ROM at %08x%s\n", rom & ~0xfff,
338              (cmd & PCI_COMMAND_MEMORY) ? "" : " [disabled]");
339     }
340 }
341
342 static void
343 show_htype1(struct device *d)
344 {
345   u32 io_base = get_conf_byte(d, PCI_IO_BASE);
346   u32 io_limit = get_conf_byte(d, PCI_IO_LIMIT);
347   u32 io_type = io_base & PCI_IO_RANGE_TYPE_MASK;
348   u32 mem_base = get_conf_word(d, PCI_MEMORY_BASE);
349   u32 mem_limit = get_conf_word(d, PCI_MEMORY_LIMIT);
350   u32 mem_type = mem_base & PCI_MEMORY_RANGE_TYPE_MASK;
351   u32 pref_base = get_conf_word(d, PCI_PREF_MEMORY_BASE);
352   u32 pref_limit = get_conf_word(d, PCI_PREF_MEMORY_LIMIT);
353   u32 pref_type = pref_base & PCI_PREF_RANGE_TYPE_MASK;
354   u32 rom = get_conf_long(d, PCI_ROM_ADDRESS1);
355   word brc = get_conf_word(d, PCI_BRIDGE_CONTROL);
356
357   show_bases(d, 2);
358   printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
359          get_conf_byte(d, PCI_PRIMARY_BUS),
360          get_conf_byte(d, PCI_SECONDARY_BUS),
361          get_conf_byte(d, PCI_SUBORDINATE_BUS),
362          get_conf_byte(d, PCI_SEC_LATENCY_TIMER));
363
364   if (io_type != (io_limit & PCI_IO_RANGE_TYPE_MASK) ||
365       (io_type != PCI_IO_RANGE_TYPE_16 && io_type != PCI_IO_RANGE_TYPE_32))
366     printf("\t!!! Unknown I/O range types %x/%x\n", io_base, io_limit);
367   else
368     {
369       io_base = (io_base & PCI_IO_RANGE_MASK) << 8;
370       io_limit = (io_limit & PCI_IO_RANGE_MASK) << 8;
371       if (io_type == PCI_IO_RANGE_TYPE_32)
372         {
373           io_base |= (get_conf_word(d, PCI_IO_BASE_UPPER16) << 16);
374           io_limit |= (get_conf_word(d, PCI_IO_LIMIT_UPPER16) << 16);
375         }
376       if (io_base)
377         printf("\tI/O behind bridge: %08x-%08x\n", io_base, io_limit+0xfff);
378     }
379
380   if (mem_type != (mem_limit & PCI_MEMORY_RANGE_TYPE_MASK) ||
381       mem_type)
382     printf("\t!!! Unknown memory range types %x/%x\n", mem_base, mem_limit);
383   else if (mem_base)
384     {
385       mem_base = (mem_base & PCI_MEMORY_RANGE_MASK) << 16;
386       mem_limit = (mem_limit & PCI_MEMORY_RANGE_MASK) << 16;
387       printf("\tMemory behind bridge: %08x-%08x\n", mem_base, mem_limit + 0xfffff);
388     }
389
390   if (pref_type != (pref_limit & PCI_PREF_RANGE_TYPE_MASK) ||
391       (pref_type != PCI_PREF_RANGE_TYPE_32 && pref_type != PCI_PREF_RANGE_TYPE_64))
392     printf("\t!!! Unknown prefetchable memory range types %x/%x\n", pref_base, pref_limit);
393   else if (pref_base)
394     {
395       pref_base = (pref_base & PCI_PREF_RANGE_MASK) << 16;
396       pref_limit = (pref_limit & PCI_PREF_RANGE_MASK) << 16;
397       if (pref_type == PCI_PREF_RANGE_TYPE_32)
398         printf("\tPrefetchable memory behind bridge: %08x-%08x\n", pref_base, pref_limit);
399       else
400         printf("\tPrefetchable memory behind bridge: %08x%08x-%08x%08x\n",
401                get_conf_long(d, PCI_PREF_BASE_UPPER32),
402                pref_base,
403                get_conf_long(d, PCI_PREF_LIMIT_UPPER32),
404                pref_limit);
405     }
406
407   if (get_conf_word(d, PCI_SEC_STATUS) & PCI_STATUS_SIG_SYSTEM_ERROR)
408     printf("\tSecondary status: SERR\n");
409
410   if (rom & 1)
411     {
412       word cmd = get_conf_word(d, PCI_COMMAND);
413       printf("\tExpansion ROM at %08x%s\n", rom & ~0xfff,
414              (cmd & PCI_COMMAND_MEMORY) ? "" : " [disabled]");
415     }
416
417   if (verbose > 1)
418     printf("\tBridgeCtl: Parity%c SERR%c NoISA%c VGA%c MAbort%c >Reset%c FastB2B%c\n",
419            (brc & PCI_BRIDGE_CTL_PARITY) ? '+' : '-',
420            (brc & PCI_BRIDGE_CTL_SERR) ? '+' : '-',
421            (brc & PCI_BRIDGE_CTL_NO_ISA) ? '+' : '-',
422            (brc & PCI_BRIDGE_CTL_VGA) ? '+' : '-',
423            (brc & PCI_BRIDGE_CTL_MASTER_ABORT) ? '+' : '-',
424            (brc & PCI_BRIDGE_CTL_BUS_RESET) ? '+' : '-',
425            (brc & PCI_BRIDGE_CTL_FAST_BACK) ? '+' : '-');
426 }
427
428 static void
429 show_htype2(struct device *d)
430 {
431 }
432
433 static void
434 show_verbose(struct device *d)
435 {
436   word status = get_conf_word(d, PCI_STATUS);
437   word cmd = get_conf_word(d, PCI_COMMAND);
438   word class = get_conf_word(d, PCI_CLASS_DEVICE);
439   byte bist = get_conf_byte(d, PCI_BIST);
440   byte htype = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
441   byte latency = get_conf_byte(d, PCI_LATENCY_TIMER);
442   byte cache_line = get_conf_byte(d, PCI_CACHE_LINE_SIZE);
443   byte max_lat, min_gnt;
444   byte int_pin = get_conf_byte(d, PCI_INTERRUPT_PIN);
445   byte int_line = get_conf_byte(d, PCI_INTERRUPT_LINE);
446   unsigned int irq;
447   word subsys_v, subsys_d;
448
449   show_terse(d);
450
451   switch (htype)
452     {
453     case PCI_HEADER_TYPE_NORMAL:
454       if (class == PCI_CLASS_BRIDGE_PCI)
455         {
456         badhdr:
457           printf("\t!!! Header type %02x doesn't match class code %04x\n", htype, class);
458           return;
459         }
460       max_lat = get_conf_byte(d, PCI_MAX_LAT);
461       min_gnt = get_conf_byte(d, PCI_MIN_GNT);
462       subsys_v = get_conf_word(d, PCI_SUBSYSTEM_VENDOR_ID);
463       subsys_d = get_conf_word(d, PCI_SUBSYSTEM_ID);
464       break;
465     case PCI_HEADER_TYPE_BRIDGE:
466       if (class != PCI_CLASS_BRIDGE_PCI)
467         goto badhdr;
468       irq = int_line = int_pin = min_gnt = max_lat = 0;
469       subsys_v = subsys_d = 0;
470       break;
471     case PCI_HEADER_TYPE_CARDBUS:
472       if ((class >> 8) != PCI_BASE_CLASS_BRIDGE)
473         goto badhdr;
474       irq = int_line = int_pin = min_gnt = max_lat = 0;
475       subsys_v = subsys_d = 0;
476       break;
477     default:
478       printf("\t!!! Unknown header type %02x\n", htype);
479       return;
480     }
481
482   if (buscentric_view)
483     irq = int_line;
484   else
485     irq = d->kernel_irq;
486
487   if (verbose > 1)
488     {
489       if (subsys_v)
490         printf("\tSubsystem ID: %04x:%04x\n", subsys_v, subsys_d);
491       printf("\tControl: I/O%c Mem%c BusMaster%c SpecCycle%c MemWINV%c VGASnoop%c ParErr%c Stepping%c SERR%c FastB2B%c\n",
492              (cmd & PCI_COMMAND_IO) ? '+' : '-',
493              (cmd & PCI_COMMAND_MEMORY) ? '+' : '-',
494              (cmd & PCI_COMMAND_MASTER) ? '+' : '-',
495              (cmd & PCI_COMMAND_SPECIAL) ? '+' : '-',
496              (cmd & PCI_COMMAND_INVALIDATE) ? '+' : '-',
497              (cmd & PCI_COMMAND_VGA_PALETTE) ? '+' : '-',
498              (cmd & PCI_COMMAND_PARITY) ? '+' : '-',
499              (cmd & PCI_COMMAND_WAIT) ? '+' : '-',
500              (cmd & PCI_COMMAND_SERR) ? '+' : '-',
501              (cmd & PCI_COMMAND_FAST_BACK) ? '+' : '-');
502       printf("\tStatus: 66Mhz%c UDF%c FastB2B%c ParErr%c DEVSEL=%s >TAbort%c <TAbort%c <MAbort%c >SERR%c <PERR%c\n",
503              (status & PCI_STATUS_66MHZ) ? '+' : '-',
504              (status & PCI_STATUS_UDF) ? '+' : '-',
505              (status & PCI_STATUS_FAST_BACK) ? '+' : '-',
506              (status & PCI_STATUS_PARITY) ? '+' : '-',
507              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
508              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
509              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??",
510              (status & PCI_STATUS_SIG_TARGET_ABORT) ? '+' : '-',
511              (status & PCI_STATUS_REC_TARGET_ABORT) ? '+' : '-',
512              (status & PCI_STATUS_REC_MASTER_ABORT) ? '+' : '-',
513              (status & PCI_STATUS_SIG_SYSTEM_ERROR) ? '+' : '-',
514              (status & PCI_STATUS_DETECTED_PARITY) ? '+' : '-');
515       if (cmd & PCI_COMMAND_MASTER)
516         {
517           printf("\tLatency: ");
518           if (min_gnt)
519             printf("%d min, ", min_gnt);
520           if (max_lat)
521             printf("%d max, ", max_lat);
522           printf("%d set", latency);
523           if (cache_line)
524             printf(", cache line size %02x", cache_line);
525           putchar('\n');
526         }
527       if (int_pin)
528         printf("\tInterrupt: pin %c routed to IRQ " IRQ_FORMAT "\n", 'A' + int_pin - 1, irq);
529     }
530   else
531     {
532       printf("\tFlags: ");
533       if (cmd & PCI_COMMAND_MASTER)
534         printf("bus master, ");
535       if (cmd & PCI_COMMAND_VGA_PALETTE)
536         printf("VGA palette snoop, ");
537       if (cmd & PCI_COMMAND_WAIT)
538         printf("stepping, ");
539       if (cmd & PCI_COMMAND_FAST_BACK)
540         printf("fast Back2Back, ");
541       if (status & PCI_STATUS_66MHZ)
542         printf("66Mhz, ");
543       if (status & PCI_STATUS_UDF)
544         printf("user-definable features, ");
545       printf("%s devsel",
546              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
547              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
548              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??");
549       if (cmd & PCI_COMMAND_MASTER)
550         printf(", latency %d", latency);
551       if (int_pin)
552         if (d->kernel_irq)
553           printf(", IRQ " IRQ_FORMAT, irq);
554         else
555           printf(", IRQ ?");
556       putchar('\n');
557     }
558
559   if (bist & PCI_BIST_CAPABLE)
560     {
561       if (bist & PCI_BIST_START)
562         printf("\tBIST is running\n");
563       else
564         printf("\tBIST result: %02x\n", bist & PCI_BIST_CODE_MASK);
565     }
566
567   switch (htype)
568     {
569     case PCI_HEADER_TYPE_NORMAL:
570       show_htype0(d);
571       break;
572     case PCI_HEADER_TYPE_BRIDGE:
573       show_htype1(d);
574       break;
575     case PCI_HEADER_TYPE_CARDBUS:
576       show_htype2(d);
577       break;
578     }
579 }
580
581 static void
582 show_hex_dump(struct device *d)
583 {
584   int i;
585   int limit = (show_hex > 1) ? 256 : 64;
586
587   for(i=0; i<limit; i++)
588     {
589       if (! (i & 15))
590         printf("%02x:", i);
591       printf(" %02x", get_conf_byte(d, i));
592       if ((i & 15) == 15)
593         putchar('\n');
594     }
595 }
596
597 static void
598 show(void)
599 {
600   struct device *d;
601
602   for(d=first_dev; d; d=d->next)
603     {
604       if (verbose)
605         show_verbose(d);
606       else
607         show_terse(d);
608       if (show_hex)
609         show_hex_dump(d);
610       if (verbose || show_hex)
611         putchar('\n');
612     }
613 }
614
615 /* Tree output */
616
617 struct bridge {
618   struct bridge *chain;                 /* Single-linked list of bridges */
619   struct bridge *next, *child;          /* Tree of bridges */
620   struct bus *first_bus;                /* List of busses connected to this bridge */
621   unsigned int primary, secondary, subordinate; /* Bus numbers */
622   struct device *br_dev;
623 };
624
625 struct bus {
626   unsigned int number;
627   struct bus *sibling;
628   struct device *first_dev, **last_dev;
629 };
630
631 static struct bridge host_bridge = { NULL, NULL, NULL, NULL, ~0, 0, ~0, NULL };
632
633 static struct bus *
634 find_bus(struct bridge *b, unsigned int n)
635 {
636   struct bus *bus;
637
638   for(bus=b->first_bus; bus; bus=bus->sibling)
639     if (bus->number == n)
640       break;
641   return bus;
642 }
643
644 static struct bus *
645 new_bus(struct bridge *b, unsigned int n)
646 {
647   struct bus *bus = xmalloc(sizeof(struct bus));
648
649   bus = xmalloc(sizeof(struct bus));
650   bus->number = n;
651   bus->sibling = b->first_bus;
652   bus->first_dev = NULL;
653   bus->last_dev = &bus->first_dev;
654   b->first_bus = bus;
655   return bus;
656 }
657
658 static void
659 insert_dev(struct device *d, struct bridge *b)
660 {
661   struct bus *bus;
662
663   if (! (bus = find_bus(b, d->bus)))
664     {
665       struct bridge *c;
666       for(c=b->child; c; c=c->next)
667         if (c->secondary <= d->bus && d->bus <= c->subordinate)
668           return insert_dev(d, c);
669       bus = new_bus(b, d->bus);
670     }
671   /* Simple insertion at the end _does_ guarantee the correct order as the
672    * original device list was sorted by (bus, devfn) lexicographically
673    * and all devices on the new list have the same bus number.
674    */
675   *bus->last_dev = d;
676   bus->last_dev = &d->next;
677   d->next = NULL;
678 }
679
680 static void
681 grow_tree(void)
682 {
683   struct device *d, *d2;
684   struct bridge *first_br, *b;
685
686   /* Build list of bridges */
687
688   first_br = &host_bridge;
689   for(d=first_dev; d; d=d->next)
690     {
691       word class = get_conf_word(d, PCI_CLASS_DEVICE);
692       if (class == PCI_CLASS_BRIDGE_PCI && (get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f) == 1)
693         {
694           b = xmalloc(sizeof(struct bridge));
695           b->primary = get_conf_byte(d, PCI_PRIMARY_BUS);
696           b->secondary = get_conf_byte(d, PCI_SECONDARY_BUS);
697           b->subordinate = get_conf_byte(d, PCI_SUBORDINATE_BUS);
698           b->chain = first_br;
699           first_br = b;
700           b->next = b->child = NULL;
701           b->first_bus = NULL;
702           b->br_dev = d;
703         }
704     }
705
706   /* Create a bridge tree */
707
708   for(b=first_br; b; b=b->chain)
709     {
710       struct bridge *c, *best;
711       best = NULL;
712       for(c=first_br; c; c=c->chain)
713         if (c != b && b->primary >= c->secondary && b->primary <= c->subordinate &&
714             (!best || best->subordinate - best->primary > c->subordinate - c->primary))
715           best = c;
716       if (best)
717         {
718           b->next = best->child;
719           best->child = b;
720         }
721     }
722
723   /* Insert secondary bus for each bridge */
724
725   for(b=first_br; b; b=b->chain)
726     if (!find_bus(b, b->secondary))
727       new_bus(b, b->secondary);
728
729   /* Create bus structs and link devices */
730
731   for(d=first_dev; d;)
732     {
733       d2 = d->next;
734       insert_dev(d, &host_bridge);
735       d = d2;
736     }
737 }
738
739 static void
740 print_it(byte *line, byte *p)
741 {
742   *p++ = '\n';
743   *p = 0;
744   fputs(line, stdout);
745   for(p=line; *p; p++)
746     if (*p == '+')
747       *p = '|';
748     else
749       *p = ' ';
750 }
751
752 static void show_tree_bridge(struct bridge *, byte *, byte *);
753
754 static void
755 show_tree_dev(struct device *d, byte *line, byte *p)
756 {
757   struct bridge *b;
758
759   p += sprintf(p, "%02x.%x", PCI_SLOT(d->devfn), PCI_FUNC(d->devfn));
760   for(b=&host_bridge; b; b=b->chain)
761     if (b->br_dev == d)
762       {
763         p += sprintf(p, "-[%02x-%02x]-", b->secondary, b->subordinate);
764         show_tree_bridge(b, line, p);
765         return;
766       }
767   if (verbose)
768     p += sprintf(p, "  %s", lookup_device_full(d->vendid, d->devid));
769   print_it(line, p);
770 }
771
772 static void
773 show_tree_bus(struct bus *b, byte *line, byte *p)
774 {
775   if (!b->first_dev)
776     print_it(line, p);
777   else if (!b->first_dev->next)
778     {
779       *p++ = '-';
780       *p++ = '-';
781       show_tree_dev(b->first_dev, line, p);
782     }
783   else
784     {
785       struct device *d = b->first_dev;
786       while (d->next)
787         {
788           p[0] = '+';
789           p[1] = '-';
790           show_tree_dev(d, line, p+2);
791           d = d->next;
792         }
793       p[0] = '\\';
794       p[1] = '-';
795       show_tree_dev(d, line, p+2);
796     }
797 }
798
799 static void
800 show_tree_bridge(struct bridge *b, byte *line, byte *p)
801 {
802   *p++ = '-';
803   if (!b->first_bus->sibling)
804     {
805       if (b == &host_bridge)
806         p += sprintf(p, "[%02x]-", b->first_bus->number);
807       show_tree_bus(b->first_bus, line, p);
808     }
809   else
810     {
811       struct bus *u = b->first_bus;
812       byte *k;
813
814       while (u->sibling)
815         {
816           k = p + sprintf(p, "+-[%02x]-", u->number);
817           show_tree_bus(u, line, k);
818           u = u->sibling;
819         }
820       k = p + sprintf(p, "\\-[%02x]-", u->number);
821       show_tree_bus(u, line, k);
822     }
823 }
824
825 static void
826 show_forest(void)
827 {
828   char line[256];
829
830   grow_tree();
831   show_tree_bridge(&host_bridge, line, line);
832 }
833
834 /* Main */
835
836 int
837 main(int argc, char **argv)
838 {
839   int i;
840
841   while ((i = getopt(argc, argv, options)) != -1)
842     switch (i)
843       {
844       case 'n':
845         show_numeric_ids = 1;
846         break;
847       case 'v':
848         verbose++;
849         break;
850       case 'b':
851         buscentric_view = 1;
852         break;
853       case 'B':
854         bus_filter = strtol(optarg, NULL, 16);
855         break;
856       case 'S':
857         slot_filter = strtol(optarg, NULL, 16);
858         break;
859       case 'F':
860         func_filter = strtol(optarg, NULL, 16);
861         break;
862       case 'V':
863         vend_filter = strtol(optarg, NULL, 16);
864         break;
865       case 'D':
866         dev_filter = strtol(optarg, NULL, 16);
867         break;
868       case 'x':
869         show_hex++;
870         break;
871       case 't':
872         show_tree++;
873         break;
874       case 'i':
875         pci_ids = optarg;
876         break;
877       case 'p':
878         pci_dir = optarg;
879         break;
880       default:
881       bad:
882         fprintf(stderr, help_msg);
883         return 1;
884       }
885   if (optind < argc)
886     goto bad;
887
888   scan_proc();
889   sort_them();
890   if (show_tree)
891     show_forest();
892   else
893     show();
894
895   return 0;
896 }