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