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