]> mj.ucw.cz Git - pciutils.git/blob - lspci.c
lspci: mention "-xxx" option in the help.
[pciutils.git] / lspci.c
1 /*
2  *      $Id: lspci.c,v 1.42 2002/04/06 12:08:26 mj Exp $
3  *
4  *      Linux PCI Utilities -- List All PCI Devices
5  *
6  *      Copyright (c) 1997--2002 Martin Mares <mj@ucw.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 static int map_mode;                    /* Bus mapping mode enabled */
28
29 static char options[] = "nvbxs:d:ti:mgM" GENERIC_OPTIONS ;
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 the standard portion of config space\n\
38 -xxx\t\tShow hex-dump of the whole config space (dangerous; root only)\n\
39 -s [[<bus>]:][<slot>][.[<func>]]\tShow only devices in selected slots\n\
40 -d [<vendor>]:[<device>]\tShow only selected devices\n\
41 -t\t\tShow bus tree\n\
42 -m\t\tProduce machine-readable output\n\
43 -i <file>\tUse specified ID database instead of %s\n\
44 -M\t\tEnable `bus mapping' mode (dangerous; root only)\n"
45 GENERIC_HELP
46 ;
47
48 /* Communication with libpci */
49
50 static struct pci_access *pacc;
51
52 /* Format strings used for IRQ numbers and memory addresses */
53
54 #ifdef ARCH_SPARC64
55 #define IRQ_FORMAT "%08x"
56 #else
57 #define IRQ_FORMAT "%d"
58 #endif
59
60 #ifdef HAVE_64BIT_ADDRESS
61 #ifdef HAVE_LONG_ADDRESS
62 #define ADDR_FORMAT "%016Lx"
63 #else
64 #define ADDR_FORMAT "%016lx"
65 #endif
66 #else
67 #define ADDR_FORMAT "%08lx"
68 #endif
69
70 #ifdef ARCH_SPARC64
71 #define IO_FORMAT "%016Lx"
72 #elif defined(HAVE_LONG_ADDRESS)
73 #define IO_FORMAT "%04Lx"
74 #else
75 #define IO_FORMAT "%04lx"
76 #endif
77
78 /*
79  *  If we aren't being compiled by GCC, use malloc() instead of alloca().
80  *  This increases our memory footprint, but only slightly since we don't
81  *  use alloca() much.
82  */
83
84 #ifndef __GNUC__
85 #define alloca malloc
86 #endif
87
88 /* Our view of the PCI bus */
89
90 struct device {
91   struct device *next;
92   struct pci_dev *dev;
93   unsigned int config_cnt;
94   byte config[256];
95 };
96
97 static struct device *first_dev;
98
99 static struct device *
100 scan_device(struct pci_dev *p)
101 {
102   int how_much = (show_hex > 2) ? 256 : 64;
103   struct device *d;
104
105   if (!pci_filter_match(&filter, p))
106     return NULL;
107   d = xmalloc(sizeof(struct device));
108   bzero(d, sizeof(*d));
109   d->dev = p;
110   if (!pci_read_block(p, 0, d->config, how_much))
111     die("Unable to read %d bytes of configuration space.", how_much);
112   if (how_much < 128 && (d->config[PCI_HEADER_TYPE] & 0x7f) == PCI_HEADER_TYPE_CARDBUS)
113     {
114       /* For cardbus bridges, we need to fetch 64 bytes more to get the full standard header... */
115       if (!pci_read_block(p, 64, d->config+64, 64))
116         die("Unable to read cardbus bridge extension data.");
117       how_much = 128;
118     }
119   d->config_cnt = how_much;
120   pci_setup_cache(p, d->config, d->config_cnt);
121   pci_fill_info(p, PCI_FILL_IDENT | PCI_FILL_IRQ | PCI_FILL_BASES | PCI_FILL_ROM_BASE | PCI_FILL_SIZES);
122   return d;
123 }
124
125 static void
126 scan_devices(void)
127 {
128   struct device *d;
129   struct pci_dev *p;
130
131   pci_scan_bus(pacc);
132   for(p=pacc->devices; p; p=p->next)
133     if (d = scan_device(p))
134       {
135         d->next = first_dev;
136         first_dev = d;
137       }
138 }
139
140 static int
141 check_root(void)
142 {
143   static int is_root = -1;
144
145   if (is_root < 0)
146     is_root = !geteuid();
147   return is_root;
148 }
149
150 static int
151 config_fetch(struct device *d, unsigned int pos, unsigned int len)
152 {
153   if (pos + len < d->config_cnt)
154     return 1;
155   if (pacc->method != PCI_ACCESS_DUMP && !check_root())
156     return 0;
157   return pci_read_block(d->dev, pos, d->config + pos, len);
158 }
159
160 /* Config space accesses */
161
162 static inline byte
163 get_conf_byte(struct device *d, unsigned int pos)
164 {
165   return d->config[pos];
166 }
167
168 static word
169 get_conf_word(struct device *d, unsigned int pos)
170 {
171   return d->config[pos] | (d->config[pos+1] << 8);
172 }
173
174 static u32
175 get_conf_long(struct device *d, unsigned int pos)
176 {
177   return d->config[pos] |
178     (d->config[pos+1] << 8) |
179     (d->config[pos+2] << 16) |
180     (d->config[pos+3] << 24);
181 }
182
183 /* Sorting */
184
185 static int
186 compare_them(const void *A, const void *B)
187 {
188   const struct pci_dev *a = (*(const struct device **)A)->dev;
189   const struct pci_dev *b = (*(const struct device **)B)->dev;
190
191   if (a->bus < b->bus)
192     return -1;
193   if (a->bus > b->bus)
194     return 1;
195   if (a->dev < b->dev)
196     return -1;
197   if (a->dev > b->dev)
198     return 1;
199   if (a->func < b->func)
200     return -1;
201   if (a->func > b->func)
202     return 1;
203   return 0;
204 }
205
206 static void
207 sort_them(void)
208 {
209   struct device **index, **h, **last_dev;
210   int cnt;
211   struct device *d;
212
213   cnt = 0;
214   for(d=first_dev; d; d=d->next)
215     cnt++;
216   h = index = alloca(sizeof(struct device *) * cnt);
217   for(d=first_dev; d; d=d->next)
218     *h++ = d;
219   qsort(index, cnt, sizeof(struct device *), compare_them);
220   last_dev = &first_dev;
221   h = index;
222   while (cnt--)
223     {
224       *last_dev = *h;
225       last_dev = &(*h)->next;
226       h++;
227     }
228   *last_dev = NULL;
229 }
230
231 /* Normal output */
232
233 #define FLAG(x,y) ((x & y) ? '+' : '-')
234
235 static void
236 show_terse(struct device *d)
237 {
238   int c;
239   struct pci_dev *p = d->dev;
240   byte classbuf[128], devbuf[128];
241
242   printf("%02x:%02x.%x %s: %s",
243          p->bus,
244          p->dev,
245          p->func,
246          pci_lookup_name(pacc, classbuf, sizeof(classbuf),
247                          PCI_LOOKUP_CLASS,
248                          get_conf_word(d, PCI_CLASS_DEVICE), 0, 0, 0),
249          pci_lookup_name(pacc, devbuf, sizeof(devbuf),
250                          PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
251                          p->vendor_id, p->device_id, 0, 0));
252   if (c = get_conf_byte(d, PCI_REVISION_ID))
253     printf(" (rev %02x)", c);
254   if (verbose)
255     {
256       char *x;
257       c = get_conf_byte(d, PCI_CLASS_PROG);
258       x = pci_lookup_name(pacc, devbuf, sizeof(devbuf),
259                           PCI_LOOKUP_PROGIF,
260                           get_conf_word(d, PCI_CLASS_DEVICE), c, 0, 0);
261       if (c || x)
262         {
263           printf(" (prog-if %02x", c);
264           if (x)
265             printf(" [%s]", x);
266           putchar(')');
267         }
268     }
269   putchar('\n');
270 }
271
272 static void
273 show_size(pciaddr_t x)
274 {
275   if (!x)
276     return;
277   printf(" [size=");
278   if (x < 1024)
279     printf("%d", (int) x);
280   else if (x < 1048576)
281     printf("%dK", (int)(x / 1024));
282   else if (x < 0x80000000)
283     printf("%dM", (int)(x / 1048576));
284   else
285     printf(ADDR_FORMAT, x);
286   putchar(']');
287 }
288
289 static void
290 show_bases(struct device *d, int cnt)
291 {
292   struct pci_dev *p = d->dev;
293   word cmd = get_conf_word(d, PCI_COMMAND);
294   int i;
295
296   for(i=0; i<cnt; i++)
297     {
298       pciaddr_t pos = p->base_addr[i];
299       pciaddr_t len = (p->known_fields & PCI_FILL_SIZES) ? p->size[i] : 0;
300       u32 flg = get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i);
301       if (flg == 0xffffffff)
302         flg = 0;
303       if (!pos && !flg && !len)
304         continue;
305       if (verbose > 1)
306         printf("\tRegion %d: ", i);
307       else
308         putchar('\t');
309       if (pos && !flg)                  /* Reported by the OS, but not by the device */
310         {
311           printf("[virtual] ");
312           flg = pos;
313         }
314       if (flg & PCI_BASE_ADDRESS_SPACE_IO)
315         {
316           pciaddr_t a = pos & PCI_BASE_ADDRESS_IO_MASK;
317           printf("I/O ports at ");
318           if (a)
319             printf(IO_FORMAT, a);
320           else if (flg & PCI_BASE_ADDRESS_IO_MASK)
321             printf("<ignored>");
322           else
323             printf("<unassigned>");
324           if (!(cmd & PCI_COMMAND_IO))
325             printf(" [disabled]");
326         }
327       else
328         {
329           int t = flg & PCI_BASE_ADDRESS_MEM_TYPE_MASK;
330           pciaddr_t a = pos & PCI_ADDR_MEM_MASK;
331           int done = 0;
332           u32 z = 0;
333
334           printf("Memory at ");
335           if (t == PCI_BASE_ADDRESS_MEM_TYPE_64)
336             {
337               if (i >= cnt - 1)
338                 {
339                   printf("<invalid-64bit-slot>");
340                   done = 1;
341                 }
342               else
343                 {
344                   i++;
345                   z = get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i);
346                   if (buscentric_view)
347                     {
348                       if (a || z)
349                         printf("%08x" ADDR_FORMAT, z, a);
350                       else
351                         printf("<unassigned>");
352                       done = 1;
353                     }
354                 }
355             }
356           if (!done)
357             {
358               if (a)
359                 printf(ADDR_FORMAT, a);
360               else
361                 printf(((flg & PCI_BASE_ADDRESS_MEM_MASK) || z) ? "<ignored>" : "<unassigned>");
362             }
363           printf(" (%s, %sprefetchable)",
364                  (t == PCI_BASE_ADDRESS_MEM_TYPE_32) ? "32-bit" :
365                  (t == PCI_BASE_ADDRESS_MEM_TYPE_64) ? "64-bit" :
366                  (t == PCI_BASE_ADDRESS_MEM_TYPE_1M) ? "low-1M" : "type 3",
367                  (flg & PCI_BASE_ADDRESS_MEM_PREFETCH) ? "" : "non-");
368           if (!(cmd & PCI_COMMAND_MEMORY))
369             printf(" [disabled]");
370         }
371       show_size(len);
372       putchar('\n');
373     }
374 }
375
376 static void
377 show_pm(struct device *d, int where, int cap)
378 {
379   int t, b;
380   static int pm_aux_current[8] = { 0, 55, 100, 160, 220, 270, 320, 375 };
381
382   printf("Power Management version %d\n", cap & PCI_PM_CAP_VER_MASK);
383   if (verbose < 2)
384     return;
385   printf("\t\tFlags: PMEClk%c DSI%c D1%c D2%c AuxCurrent=%dmA PME(D0%c,D1%c,D2%c,D3hot%c,D3cold%c)\n",
386          FLAG(cap, PCI_PM_CAP_PME_CLOCK),
387          FLAG(cap, PCI_PM_CAP_DSI),
388          FLAG(cap, PCI_PM_CAP_D1),
389          FLAG(cap, PCI_PM_CAP_D2),
390          pm_aux_current[(cap >> 6) & 7],
391          FLAG(cap, PCI_PM_CAP_PME_D0),
392          FLAG(cap, PCI_PM_CAP_PME_D1),
393          FLAG(cap, PCI_PM_CAP_PME_D2),
394          FLAG(cap, PCI_PM_CAP_PME_D3_HOT),
395          FLAG(cap, PCI_PM_CAP_PME_D3_COLD));
396   config_fetch(d, where + PCI_PM_CTRL, PCI_PM_SIZEOF - PCI_PM_CTRL);
397   t = get_conf_word(d, where + PCI_PM_CTRL);
398   printf("\t\tStatus: D%d PME-Enable%c DSel=%d DScale=%d PME%c\n",
399          t & PCI_PM_CTRL_STATE_MASK,
400          FLAG(t, PCI_PM_CTRL_PME_ENABLE),
401          (t & PCI_PM_CTRL_DATA_SEL_MASK) >> 9,
402          (t & PCI_PM_CTRL_DATA_SCALE_MASK) >> 13,
403          FLAG(t, PCI_PM_CTRL_PME_STATUS));
404   b = get_conf_byte(d, where + PCI_PM_PPB_EXTENSIONS);
405   if (b)
406     printf("\t\tBridge: PM%c B3%c\n",
407            FLAG(t, PCI_PM_BPCC_ENABLE),
408            FLAG(~t, PCI_PM_PPB_B2_B3));
409 }
410
411 static void
412 format_agp_rate(int rate, char *buf)
413 {
414   char *c = buf;
415   int i;
416
417   for(i=0; i<=2; i++)
418     if (rate & (1 << i))
419       {
420         if (c != buf)
421           *c++ = ',';
422         *c++ = 'x';
423         *c++ = '0' + (1 << i);
424       }
425   if (c != buf)
426     *c = 0;
427   else
428     strcpy(buf, "<none>");
429 }
430
431 static void
432 show_agp(struct device *d, int where, int cap)
433 {
434   u32 t;
435   char rate[8];
436
437   t = cap & 0xff;
438   printf("AGP version %x.%x\n", cap/16, cap%16);
439   if (verbose < 2)
440     return;
441   config_fetch(d, where + PCI_AGP_STATUS, PCI_AGP_SIZEOF - PCI_AGP_STATUS);
442   t = get_conf_long(d, where + PCI_AGP_STATUS);
443   format_agp_rate(t & 7, rate);
444   printf("\t\tStatus: RQ=%d SBA%c 64bit%c FW%c Rate=%s\n",
445          (t & PCI_AGP_STATUS_RQ_MASK) >> 24U,
446          FLAG(t, PCI_AGP_STATUS_SBA),
447          FLAG(t, PCI_AGP_STATUS_64BIT),
448          FLAG(t, PCI_AGP_STATUS_FW),
449          rate);
450   t = get_conf_long(d, where + PCI_AGP_COMMAND);
451   format_agp_rate(t & 7, rate);
452   printf("\t\tCommand: RQ=%d SBA%c AGP%c 64bit%c FW%c Rate=%s\n",
453          (t & PCI_AGP_COMMAND_RQ_MASK) >> 24U,
454          FLAG(t, PCI_AGP_COMMAND_SBA),
455          FLAG(t, PCI_AGP_COMMAND_AGP),
456          FLAG(t, PCI_AGP_COMMAND_64BIT),
457          FLAG(t, PCI_AGP_COMMAND_FW),
458          rate);
459 }
460
461 static void
462 show_pcix_nobridge(struct device *d, int where)
463 {
464   u16 command = get_conf_word(d, where + PCI_PCIX_COMMAND);
465   u32 status = get_conf_long(d, where + PCI_PCIX_STATUS);
466   printf("PCI-X non-bridge device.\n");
467   if (verbose < 2)
468     return;
469   printf("\t\tCommand: DPERE%c ERO%c RBC=%d OST=%d\n",
470          FLAG(command, PCI_PCIX_COMMAND_DPERE),
471          FLAG(command, PCI_PCIX_COMMAND_ERO),
472          ((command & PCI_PCIX_COMMAND_MAX_MEM_READ_BYTE_COUNT) >> 2U),
473          ((command & PCI_PCIX_COMMAND_MAX_OUTSTANDING_SPLIT_TRANS) >> 4U));
474   printf("\t\tStatus: Bus=%u Dev=%u Func=%u 64bit%c 133MHz%c SCD%c USC%c, DC=%s, DMMRBC=%u, DMOST=%u, DMCRS=%u, RSCEM%c",
475          ((status >> 8) & 0xffU), // bus
476          ((status >> 3) & 0x1fU), // dev
477          (status & PCI_PCIX_BRIDGE_STATUS_FUNCTION), // function
478          FLAG(status, PCI_PCIX_STATUS_64BIT),
479          FLAG(status, PCI_PCIX_STATUS_133MHZ),
480          FLAG(status, PCI_PCIX_STATUS_SC_DISCARDED),
481          FLAG(status, PCI_PCIX_STATUS_UNEXPECTED_SC),
482          ((status & PCI_PCIX_STATUS_DEVICE_COMPLEXITY) ? "bridge" : "simple"),
483          ((status >> 21) & 3U),
484          ((status >> 23) & 7U),
485          ((status >> 26) & 7U),
486          FLAG(status, PCI_PCIX_STATUS_RCVD_SC_ERR_MESS));
487 }
488
489 static void
490 show_pcix_bridge(struct device *d, int where)
491 {
492   u16 secstatus;
493   u32 status, upstcr, downstcr;
494   printf("PCI-X bridge device.\n");
495   if (verbose < 2)
496     return;
497   secstatus = get_conf_word(d, where + PCI_PCIX_BRIDGE_SEC_STATUS);
498   printf("\t\tSecondary Status: 64bit%c, 133MHz%c, SCD%c, USC%c, SCO%c, SRD%c Freq=%d\n",
499          FLAG(secstatus, PCI_PCIX_BRIDGE_SEC_STATUS_64BIT),
500          FLAG(secstatus, PCI_PCIX_BRIDGE_SEC_STATUS_133MHZ),
501          FLAG(secstatus, PCI_PCIX_BRIDGE_SEC_STATUS_SC_DISCARDED),
502          FLAG(secstatus, PCI_PCIX_BRIDGE_SEC_STATUS_UNEXPECTED_SC),
503          FLAG(secstatus, PCI_PCIX_BRIDGE_SEC_STATUS_SC_OVERRUN),
504          FLAG(secstatus, PCI_PCIX_BRIDGE_SEC_STATUS_SPLIT_REQUEST_DELAYED),
505          ((secstatus >> 6) & 7));
506   status = get_conf_long(d, where + PCI_PCIX_BRIDGE_STATUS);
507   printf("\t\tStatus: Bus=%u Dev=%u Func=%u 64bit%c 133MHz%c SCD%c USC%c, SCO%c, SRD%c\n", 
508          ((status >> 8) & 0xff), // bus
509          ((status >> 3) & 0x1f), // dev
510          (status & PCI_PCIX_BRIDGE_STATUS_FUNCTION), // function
511          FLAG(status, PCI_PCIX_BRIDGE_STATUS_64BIT),
512          FLAG(status, PCI_PCIX_BRIDGE_STATUS_133MHZ),
513          FLAG(status, PCI_PCIX_BRIDGE_STATUS_SC_DISCARDED),
514          FLAG(status, PCI_PCIX_BRIDGE_STATUS_UNEXPECTED_SC),
515          FLAG(status, PCI_PCIX_BRIDGE_STATUS_SC_OVERRUN),
516          FLAG(status, PCI_PCIX_BRIDGE_STATUS_SPLIT_REQUEST_DELAYED));
517   upstcr = get_conf_long(d, where + PCI_PCIX_BRIDGE_UPSTREAM_SPLIT_TRANS_CTRL);
518   printf("\t\t: Upstream: Capacity=%u, Commitment Limit=%u\n",
519          (upstcr & PCI_PCIX_BRIDGE_STR_CAPACITY),
520          (upstcr >> 16) & 0xffff);
521   downstcr = get_conf_long(d, where + PCI_PCIX_BRIDGE_DOWNSTREAM_SPLIT_TRANS_CTRL);
522   printf("\t\t: Downstream: Capacity=%u, Commitment Limit=%u\n",
523          (downstcr & PCI_PCIX_BRIDGE_STR_CAPACITY),
524          (downstcr >> 16) & 0xffff);
525 }
526
527 static void
528 show_pcix(struct device *d, int where)
529 {
530   switch (d->dev->hdrtype)
531     {
532     case PCI_HEADER_TYPE_NORMAL:
533       show_pcix_nobridge(d, where);
534       break;
535     case PCI_HEADER_TYPE_BRIDGE:
536       show_pcix_bridge(d, where);
537       break;
538     }
539 }
540
541 static void
542 show_rom(struct device *d)
543 {
544   struct pci_dev *p = d->dev;
545   pciaddr_t rom = p->rom_base_addr;
546   pciaddr_t len = (p->known_fields & PCI_FILL_SIZES) ? p->rom_size : 0;
547
548   if (!rom && !len)
549     return;
550   printf("\tExpansion ROM at ");
551   if (rom & PCI_ROM_ADDRESS_MASK)
552     printf(ADDR_FORMAT, rom & PCI_ROM_ADDRESS_MASK);
553   else
554     printf("<unassigned>");
555   if (!(rom & PCI_ROM_ADDRESS_ENABLE))
556     printf(" [disabled]");
557   show_size(len);
558   putchar('\n');
559 }
560
561 static void
562 show_msi(struct device *d, int where, int cap)
563 {
564   int is64;
565   u32 t;
566   u16 w;
567
568   printf("Message Signalled Interrupts: 64bit%c Queue=%d/%d Enable%c\n",
569          FLAG(cap, PCI_MSI_FLAGS_64BIT),
570          (cap & PCI_MSI_FLAGS_QSIZE) >> 4,
571          (cap & PCI_MSI_FLAGS_QMASK) >> 1,
572          FLAG(cap, PCI_MSI_FLAGS_ENABLE));
573   if (verbose < 2)
574     return;
575   is64 = cap & PCI_MSI_FLAGS_64BIT;
576   config_fetch(d, where + PCI_MSI_ADDRESS_LO, (is64 ? PCI_MSI_DATA_64 : PCI_MSI_DATA_32) + 2 - PCI_MSI_ADDRESS_LO);
577   printf("\t\tAddress: ");
578   if (is64)
579     {
580       t = get_conf_long(d, where + PCI_MSI_ADDRESS_HI);
581       w = get_conf_word(d, where + PCI_MSI_DATA_64);
582       printf("%08x", t);
583     }
584   else
585     w = get_conf_word(d, where + PCI_MSI_DATA_32);
586   t = get_conf_long(d, where + PCI_MSI_ADDRESS_LO);
587   printf("%08x  Data: %04x\n", t, w);
588 }
589
590 static void
591 show_slotid(int cap)
592 {
593   int esr = cap & 0xff;
594   int chs = cap >> 8;
595
596   printf("Slot ID: %d slots, First%c, chassis %02x\n",
597          esr & PCI_SID_ESR_NSLOTS,
598          FLAG(esr, PCI_SID_ESR_FIC),
599          chs);
600 }
601
602 static void
603 show_caps(struct device *d)
604 {
605   if (get_conf_word(d, PCI_STATUS) & PCI_STATUS_CAP_LIST)
606     {
607       int where = get_conf_byte(d, PCI_CAPABILITY_LIST) & ~3;
608       while (where)
609         {
610           int id, next, cap;
611           printf("\tCapabilities: ");
612           if (!config_fetch(d, where, 4))
613             {
614               puts("<available only to root>");
615               break;
616             }
617           id = get_conf_byte(d, where + PCI_CAP_LIST_ID);
618           next = get_conf_byte(d, where + PCI_CAP_LIST_NEXT) & ~3;
619           cap = get_conf_word(d, where + PCI_CAP_FLAGS);
620           printf("[%02x] ", where);
621           if (id == 0xff)
622             {
623               printf("<chain broken>\n");
624               break;
625             }
626           switch (id)
627             {
628             case PCI_CAP_ID_PM:
629               show_pm(d, where, cap);
630               break;
631             case PCI_CAP_ID_AGP:
632               show_agp(d, where, cap);
633               break;
634             case PCI_CAP_ID_VPD:
635               printf("Vital Product Data\n");
636               break;
637             case PCI_CAP_ID_SLOTID:
638               show_slotid(cap);
639               break;
640             case PCI_CAP_ID_MSI:
641               show_msi(d, where, cap);
642               break;
643             case PCI_CAP_ID_PCIX:
644               show_pcix(d, where);
645               break;
646             default:
647               printf("#%02x [%04x]\n", id, cap);
648             }
649           where = next;
650         }
651     }
652 }
653
654 static void
655 show_htype0(struct device *d)
656 {
657   show_bases(d, 6);
658   show_rom(d);
659   show_caps(d);
660 }
661
662 static void
663 show_htype1(struct device *d)
664 {
665   u32 io_base = get_conf_byte(d, PCI_IO_BASE);
666   u32 io_limit = get_conf_byte(d, PCI_IO_LIMIT);
667   u32 io_type = io_base & PCI_IO_RANGE_TYPE_MASK;
668   u32 mem_base = get_conf_word(d, PCI_MEMORY_BASE);
669   u32 mem_limit = get_conf_word(d, PCI_MEMORY_LIMIT);
670   u32 mem_type = mem_base & PCI_MEMORY_RANGE_TYPE_MASK;
671   u32 pref_base = get_conf_word(d, PCI_PREF_MEMORY_BASE);
672   u32 pref_limit = get_conf_word(d, PCI_PREF_MEMORY_LIMIT);
673   u32 pref_type = pref_base & PCI_PREF_RANGE_TYPE_MASK;
674   word brc = get_conf_word(d, PCI_BRIDGE_CONTROL);
675   int verb = verbose > 2;
676
677   show_bases(d, 2);
678   printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
679          get_conf_byte(d, PCI_PRIMARY_BUS),
680          get_conf_byte(d, PCI_SECONDARY_BUS),
681          get_conf_byte(d, PCI_SUBORDINATE_BUS),
682          get_conf_byte(d, PCI_SEC_LATENCY_TIMER));
683
684   if (io_type != (io_limit & PCI_IO_RANGE_TYPE_MASK) ||
685       (io_type != PCI_IO_RANGE_TYPE_16 && io_type != PCI_IO_RANGE_TYPE_32))
686     printf("\t!!! Unknown I/O range types %x/%x\n", io_base, io_limit);
687   else
688     {
689       io_base = (io_base & PCI_IO_RANGE_MASK) << 8;
690       io_limit = (io_limit & PCI_IO_RANGE_MASK) << 8;
691       if (io_type == PCI_IO_RANGE_TYPE_32)
692         {
693           io_base |= (get_conf_word(d, PCI_IO_BASE_UPPER16) << 16);
694           io_limit |= (get_conf_word(d, PCI_IO_LIMIT_UPPER16) << 16);
695         }
696       if (io_base <= io_limit || verb)
697         printf("\tI/O behind bridge: %08x-%08x\n", io_base, io_limit+0xfff);
698     }
699
700   if (mem_type != (mem_limit & PCI_MEMORY_RANGE_TYPE_MASK) ||
701       mem_type)
702     printf("\t!!! Unknown memory range types %x/%x\n", mem_base, mem_limit);
703   else
704     {
705       mem_base = (mem_base & PCI_MEMORY_RANGE_MASK) << 16;
706       mem_limit = (mem_limit & PCI_MEMORY_RANGE_MASK) << 16;
707       if (mem_base <= mem_limit || verb)
708         printf("\tMemory behind bridge: %08x-%08x\n", mem_base, mem_limit + 0xfffff);
709     }
710
711   if (pref_type != (pref_limit & PCI_PREF_RANGE_TYPE_MASK) ||
712       (pref_type != PCI_PREF_RANGE_TYPE_32 && pref_type != PCI_PREF_RANGE_TYPE_64))
713     printf("\t!!! Unknown prefetchable memory range types %x/%x\n", pref_base, pref_limit);
714   else
715     {
716       pref_base = (pref_base & PCI_PREF_RANGE_MASK) << 16;
717       pref_limit = (pref_limit & PCI_PREF_RANGE_MASK) << 16;
718       if (pref_base <= pref_limit || verb)
719         {
720           if (pref_type == PCI_PREF_RANGE_TYPE_32)
721             printf("\tPrefetchable memory behind bridge: %08x-%08x\n", pref_base, pref_limit + 0xfffff);
722           else
723             printf("\tPrefetchable memory behind bridge: %08x%08x-%08x%08x\n",
724                    get_conf_long(d, PCI_PREF_BASE_UPPER32),
725                    pref_base,
726                    get_conf_long(d, PCI_PREF_LIMIT_UPPER32),
727                    pref_limit);
728         }
729     }
730
731   if (get_conf_word(d, PCI_SEC_STATUS) & PCI_STATUS_SIG_SYSTEM_ERROR)
732     printf("\tSecondary status: SERR\n");
733
734   show_rom(d);
735
736   if (verbose > 1)
737     printf("\tBridgeCtl: Parity%c SERR%c NoISA%c VGA%c MAbort%c >Reset%c FastB2B%c\n",
738            FLAG(brc, PCI_BRIDGE_CTL_PARITY),
739            FLAG(brc, PCI_BRIDGE_CTL_SERR),
740            FLAG(brc, PCI_BRIDGE_CTL_NO_ISA),
741            FLAG(brc, PCI_BRIDGE_CTL_VGA),
742            FLAG(brc, PCI_BRIDGE_CTL_MASTER_ABORT),
743            FLAG(brc, PCI_BRIDGE_CTL_BUS_RESET),
744            FLAG(brc, PCI_BRIDGE_CTL_FAST_BACK));
745
746   show_caps(d);
747 }
748
749 static void
750 show_htype2(struct device *d)
751 {
752   int i;
753   word cmd = get_conf_word(d, PCI_COMMAND);
754   word brc = get_conf_word(d, PCI_CB_BRIDGE_CONTROL);
755   word exca = get_conf_word(d, PCI_CB_LEGACY_MODE_BASE);
756   int verb = verbose > 2;
757
758   show_bases(d, 1);
759   printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
760          get_conf_byte(d, PCI_CB_PRIMARY_BUS),
761          get_conf_byte(d, PCI_CB_CARD_BUS),
762          get_conf_byte(d, PCI_CB_SUBORDINATE_BUS),
763          get_conf_byte(d, PCI_CB_LATENCY_TIMER));
764   for(i=0; i<2; i++)
765     {
766       int p = 8*i;
767       u32 base = get_conf_long(d, PCI_CB_MEMORY_BASE_0 + p);
768       u32 limit = get_conf_long(d, PCI_CB_MEMORY_LIMIT_0 + p);
769       if (limit > base || verb)
770         printf("\tMemory window %d: %08x-%08x%s%s\n", i, base, limit,
771                (cmd & PCI_COMMAND_MEMORY) ? "" : " [disabled]",
772                (brc & (PCI_CB_BRIDGE_CTL_PREFETCH_MEM0 << i)) ? " (prefetchable)" : "");
773     }
774   for(i=0; i<2; i++)
775     {
776       int p = 8*i;
777       u32 base = get_conf_long(d, PCI_CB_IO_BASE_0 + p);
778       u32 limit = get_conf_long(d, PCI_CB_IO_LIMIT_0 + p);
779       if (!(base & PCI_IO_RANGE_TYPE_32))
780         {
781           base &= 0xffff;
782           limit &= 0xffff;
783         }
784       base &= PCI_CB_IO_RANGE_MASK;
785       limit = (limit & PCI_CB_IO_RANGE_MASK) + 3;
786       if (base <= limit || verb)
787         printf("\tI/O window %d: %08x-%08x%s\n", i, base, limit,
788                (cmd & PCI_COMMAND_IO) ? "" : " [disabled]");
789     }
790
791   if (get_conf_word(d, PCI_CB_SEC_STATUS) & PCI_STATUS_SIG_SYSTEM_ERROR)
792     printf("\tSecondary status: SERR\n");
793   if (verbose > 1)
794     printf("\tBridgeCtl: Parity%c SERR%c ISA%c VGA%c MAbort%c >Reset%c 16bInt%c PostWrite%c\n",
795            FLAG(brc, PCI_CB_BRIDGE_CTL_PARITY),
796            FLAG(brc, PCI_CB_BRIDGE_CTL_SERR),
797            FLAG(brc, PCI_CB_BRIDGE_CTL_ISA),
798            FLAG(brc, PCI_CB_BRIDGE_CTL_VGA),
799            FLAG(brc, PCI_CB_BRIDGE_CTL_MASTER_ABORT),
800            FLAG(brc, PCI_CB_BRIDGE_CTL_CB_RESET),
801            FLAG(brc, PCI_CB_BRIDGE_CTL_16BIT_INT),
802            FLAG(brc, PCI_CB_BRIDGE_CTL_POST_WRITES));
803   if (exca)
804     printf("\t16-bit legacy interface ports at %04x\n", exca);
805 }
806
807 static void
808 show_verbose(struct device *d)
809 {
810   struct pci_dev *p = d->dev;
811   word status = get_conf_word(d, PCI_STATUS);
812   word cmd = get_conf_word(d, PCI_COMMAND);
813   word class = get_conf_word(d, PCI_CLASS_DEVICE);
814   byte bist = get_conf_byte(d, PCI_BIST);
815   byte htype = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
816   byte latency = get_conf_byte(d, PCI_LATENCY_TIMER);
817   byte cache_line = get_conf_byte(d, PCI_CACHE_LINE_SIZE);
818   byte max_lat, min_gnt;
819   byte int_pin = get_conf_byte(d, PCI_INTERRUPT_PIN);
820   unsigned int irq = p->irq;
821   word subsys_v, subsys_d;
822   char ssnamebuf[256];
823
824   show_terse(d);
825
826   switch (htype)
827     {
828     case PCI_HEADER_TYPE_NORMAL:
829       if (class == PCI_CLASS_BRIDGE_PCI)
830         printf("\t!!! Invalid class %04x for header type %02x\n", class, htype);
831       max_lat = get_conf_byte(d, PCI_MAX_LAT);
832       min_gnt = get_conf_byte(d, PCI_MIN_GNT);
833       subsys_v = get_conf_word(d, PCI_SUBSYSTEM_VENDOR_ID);
834       subsys_d = get_conf_word(d, PCI_SUBSYSTEM_ID);
835       break;
836     case PCI_HEADER_TYPE_BRIDGE:
837       if (class != PCI_CLASS_BRIDGE_PCI)
838         printf("\t!!! Invalid class %04x for header type %02x\n", class, htype);
839       irq = int_pin = min_gnt = max_lat = 0;
840       subsys_v = subsys_d = 0;
841       break;
842     case PCI_HEADER_TYPE_CARDBUS:
843       if ((class >> 8) != PCI_BASE_CLASS_BRIDGE)
844         printf("\t!!! Invalid class %04x for header type %02x\n", class, htype);
845       min_gnt = max_lat = 0;
846       subsys_v = get_conf_word(d, PCI_CB_SUBSYSTEM_VENDOR_ID);
847       subsys_d = get_conf_word(d, PCI_CB_SUBSYSTEM_ID);
848       break;
849     default:
850       printf("\t!!! Unknown header type %02x\n", htype);
851       return;
852     }
853
854   if (subsys_v && subsys_v != 0xffff)
855     printf("\tSubsystem: %s\n",
856            pci_lookup_name(pacc, ssnamebuf, sizeof(ssnamebuf),
857                            PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
858                            p->vendor_id, p->device_id, subsys_v, subsys_d));
859
860   if (verbose > 1)
861     {
862       printf("\tControl: I/O%c Mem%c BusMaster%c SpecCycle%c MemWINV%c VGASnoop%c ParErr%c Stepping%c SERR%c FastB2B%c\n",
863              FLAG(cmd, PCI_COMMAND_IO),
864              FLAG(cmd, PCI_COMMAND_MEMORY),
865              FLAG(cmd, PCI_COMMAND_MASTER),
866              FLAG(cmd, PCI_COMMAND_SPECIAL),
867              FLAG(cmd, PCI_COMMAND_INVALIDATE),
868              FLAG(cmd, PCI_COMMAND_VGA_PALETTE),
869              FLAG(cmd, PCI_COMMAND_PARITY),
870              FLAG(cmd, PCI_COMMAND_WAIT),
871              FLAG(cmd, PCI_COMMAND_SERR),
872              FLAG(cmd, PCI_COMMAND_FAST_BACK));
873       printf("\tStatus: Cap%c 66Mhz%c UDF%c FastB2B%c ParErr%c DEVSEL=%s >TAbort%c <TAbort%c <MAbort%c >SERR%c <PERR%c\n",
874              FLAG(status, PCI_STATUS_CAP_LIST),
875              FLAG(status, PCI_STATUS_66MHZ),
876              FLAG(status, PCI_STATUS_UDF),
877              FLAG(status, PCI_STATUS_FAST_BACK),
878              FLAG(status, PCI_STATUS_PARITY),
879              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
880              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
881              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??",
882              FLAG(status, PCI_STATUS_SIG_TARGET_ABORT),
883              FLAG(status, PCI_STATUS_REC_TARGET_ABORT),
884              FLAG(status, PCI_STATUS_REC_MASTER_ABORT),
885              FLAG(status, PCI_STATUS_SIG_SYSTEM_ERROR),
886              FLAG(status, PCI_STATUS_DETECTED_PARITY));
887       if (cmd & PCI_COMMAND_MASTER)
888         {
889           printf("\tLatency: %d", latency);
890           if (min_gnt || max_lat)
891             {
892               printf(" (");
893               if (min_gnt)
894                 printf("%dns min", min_gnt*250);
895               if (min_gnt && max_lat)
896                 printf(", ");
897               if (max_lat)
898                 printf("%dns max", max_lat*250);
899               putchar(')');
900             }
901           if (cache_line)
902             printf(", cache line size %02x", cache_line);
903           putchar('\n');
904         }
905       if (int_pin || irq)
906         printf("\tInterrupt: pin %c routed to IRQ " IRQ_FORMAT "\n",
907                (int_pin ? 'A' + int_pin - 1 : '?'), irq);
908     }
909   else
910     {
911       printf("\tFlags: ");
912       if (cmd & PCI_COMMAND_MASTER)
913         printf("bus master, ");
914       if (cmd & PCI_COMMAND_VGA_PALETTE)
915         printf("VGA palette snoop, ");
916       if (cmd & PCI_COMMAND_WAIT)
917         printf("stepping, ");
918       if (cmd & PCI_COMMAND_FAST_BACK)
919         printf("fast Back2Back, ");
920       if (status & PCI_STATUS_66MHZ)
921         printf("66Mhz, ");
922       if (status & PCI_STATUS_UDF)
923         printf("user-definable features, ");
924       printf("%s devsel",
925              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
926              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
927              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??");
928       if (cmd & PCI_COMMAND_MASTER)
929         printf(", latency %d", latency);
930       if (irq)
931         printf(", IRQ " IRQ_FORMAT, irq);
932       putchar('\n');
933     }
934
935   if (bist & PCI_BIST_CAPABLE)
936     {
937       if (bist & PCI_BIST_START)
938         printf("\tBIST is running\n");
939       else
940         printf("\tBIST result: %02x\n", bist & PCI_BIST_CODE_MASK);
941     }
942
943   switch (htype)
944     {
945     case PCI_HEADER_TYPE_NORMAL:
946       show_htype0(d);
947       break;
948     case PCI_HEADER_TYPE_BRIDGE:
949       show_htype1(d);
950       break;
951     case PCI_HEADER_TYPE_CARDBUS:
952       show_htype2(d);
953       break;
954     }
955 }
956
957 static void
958 show_hex_dump(struct device *d)
959 {
960   unsigned int i;
961
962   for(i=0; i<d->config_cnt; i++)
963     {
964       if (! (i & 15))
965         printf("%02x:", i);
966       printf(" %02x", get_conf_byte(d, i));
967       if ((i & 15) == 15)
968         putchar('\n');
969     }
970 }
971
972 static void
973 show_machine(struct device *d)
974 {
975   struct pci_dev *p = d->dev;
976   int c;
977   word sv_id=0, sd_id=0;
978   char classbuf[128], vendbuf[128], devbuf[128], svbuf[128], sdbuf[128];
979
980   switch (get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f)
981     {
982     case PCI_HEADER_TYPE_NORMAL:
983       sv_id = get_conf_word(d, PCI_SUBSYSTEM_VENDOR_ID);
984       sd_id = get_conf_word(d, PCI_SUBSYSTEM_ID);
985       break;
986     case PCI_HEADER_TYPE_CARDBUS:
987       sv_id = get_conf_word(d, PCI_CB_SUBSYSTEM_VENDOR_ID);
988       sd_id = get_conf_word(d, PCI_CB_SUBSYSTEM_ID);
989       break;
990     }
991
992   if (verbose)
993     {
994       printf("Device:\t%02x:%02x.%x\n", p->bus, p->dev, p->func);
995       printf("Class:\t%s\n",
996              pci_lookup_name(pacc, classbuf, sizeof(classbuf), PCI_LOOKUP_CLASS, get_conf_word(d, PCI_CLASS_DEVICE), 0, 0, 0));
997       printf("Vendor:\t%s\n",
998              pci_lookup_name(pacc, vendbuf, sizeof(vendbuf), PCI_LOOKUP_VENDOR, p->vendor_id, p->device_id, 0, 0));
999       printf("Device:\t%s\n",
1000              pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id, 0, 0));
1001       if (sv_id && sv_id != 0xffff)
1002         {
1003           printf("SVendor:\t%s\n",
1004                  pci_lookup_name(pacc, svbuf, sizeof(svbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR, p->vendor_id, p->device_id, sv_id, sd_id));
1005           printf("SDevice:\t%s\n",
1006                  pci_lookup_name(pacc, sdbuf, sizeof(sdbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id, sv_id, sd_id));
1007         }
1008       if (c = get_conf_byte(d, PCI_REVISION_ID))
1009         printf("Rev:\t%02x\n", c);
1010       if (c = get_conf_byte(d, PCI_CLASS_PROG))
1011         printf("ProgIf:\t%02x\n", c);
1012     }
1013   else
1014     {
1015       printf("%02x:%02x.%x ", p->bus, p->dev, p->func);
1016       printf("\"%s\" \"%s\" \"%s\"",
1017              pci_lookup_name(pacc, classbuf, sizeof(classbuf), PCI_LOOKUP_CLASS,
1018                              get_conf_word(d, PCI_CLASS_DEVICE), 0, 0, 0),
1019              pci_lookup_name(pacc, vendbuf, sizeof(vendbuf), PCI_LOOKUP_VENDOR,
1020                              p->vendor_id, p->device_id, 0, 0),
1021              pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_DEVICE,
1022                              p->vendor_id, p->device_id, 0, 0));
1023       if (c = get_conf_byte(d, PCI_REVISION_ID))
1024         printf(" -r%02x", c);
1025       if (c = get_conf_byte(d, PCI_CLASS_PROG))
1026         printf(" -p%02x", c);
1027       if (sv_id && sv_id != 0xffff)
1028         printf(" \"%s\" \"%s\"",
1029                pci_lookup_name(pacc, svbuf, sizeof(svbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR, p->vendor_id, p->device_id, sv_id, sd_id),
1030                pci_lookup_name(pacc, sdbuf, sizeof(sdbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id, sv_id, sd_id));
1031       else
1032         printf(" \"\" \"\"");
1033       putchar('\n');
1034     }
1035 }
1036
1037 static void
1038 show_device(struct device *d)
1039 {
1040   if (machine_readable)
1041     show_machine(d);
1042   else if (verbose)
1043     show_verbose(d);
1044   else
1045     show_terse(d);
1046   if (show_hex)
1047     show_hex_dump(d);
1048   if (verbose || show_hex)
1049     putchar('\n');
1050 }
1051
1052 static void
1053 show(void)
1054 {
1055   struct device *d;
1056
1057   for(d=first_dev; d; d=d->next)
1058     show_device(d);
1059 }
1060
1061 /* Tree output */
1062
1063 struct bridge {
1064   struct bridge *chain;                 /* Single-linked list of bridges */
1065   struct bridge *next, *child;          /* Tree of bridges */
1066   struct bus *first_bus;                /* List of busses connected to this bridge */
1067   unsigned int primary, secondary, subordinate; /* Bus numbers */
1068   struct device *br_dev;
1069 };
1070
1071 struct bus {
1072   unsigned int number;
1073   struct bus *sibling;
1074   struct device *first_dev, **last_dev;
1075 };
1076
1077 static struct bridge host_bridge = { NULL, NULL, NULL, NULL, ~0, 0, ~0, NULL };
1078
1079 static struct bus *
1080 find_bus(struct bridge *b, unsigned int n)
1081 {
1082   struct bus *bus;
1083
1084   for(bus=b->first_bus; bus; bus=bus->sibling)
1085     if (bus->number == n)
1086       break;
1087   return bus;
1088 }
1089
1090 static struct bus *
1091 new_bus(struct bridge *b, unsigned int n)
1092 {
1093   struct bus *bus = xmalloc(sizeof(struct bus));
1094
1095   bus = xmalloc(sizeof(struct bus));
1096   bus->number = n;
1097   bus->sibling = b->first_bus;
1098   bus->first_dev = NULL;
1099   bus->last_dev = &bus->first_dev;
1100   b->first_bus = bus;
1101   return bus;
1102 }
1103
1104 static void
1105 insert_dev(struct device *d, struct bridge *b)
1106 {
1107   struct pci_dev *p = d->dev;
1108   struct bus *bus;
1109
1110   if (! (bus = find_bus(b, p->bus)))
1111     {
1112       struct bridge *c;
1113       for(c=b->child; c; c=c->next)
1114         if (c->secondary <= p->bus && p->bus <= c->subordinate)
1115           {
1116             insert_dev(d, c);
1117             return;
1118           }
1119       bus = new_bus(b, p->bus);
1120     }
1121   /* Simple insertion at the end _does_ guarantee the correct order as the
1122    * original device list was sorted by (bus, devfn) lexicographically
1123    * and all devices on the new list have the same bus number.
1124    */
1125   *bus->last_dev = d;
1126   bus->last_dev = &d->next;
1127   d->next = NULL;
1128 }
1129
1130 static void
1131 grow_tree(void)
1132 {
1133   struct device *d, *d2;
1134   struct bridge **last_br, *b;
1135
1136   /* Build list of bridges */
1137
1138   last_br = &host_bridge.chain;
1139   for(d=first_dev; d; d=d->next)
1140     {
1141       word class = get_conf_word(d, PCI_CLASS_DEVICE);
1142       byte ht = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
1143       if (class == PCI_CLASS_BRIDGE_PCI &&
1144           (ht == PCI_HEADER_TYPE_BRIDGE || ht == PCI_HEADER_TYPE_CARDBUS))
1145         {
1146           b = xmalloc(sizeof(struct bridge));
1147           if (ht == PCI_HEADER_TYPE_BRIDGE)
1148             {
1149               b->primary = get_conf_byte(d, PCI_CB_PRIMARY_BUS);
1150               b->secondary = get_conf_byte(d, PCI_CB_CARD_BUS);
1151               b->subordinate = get_conf_byte(d, PCI_CB_SUBORDINATE_BUS);
1152             }
1153           else
1154             {
1155               b->primary = get_conf_byte(d, PCI_PRIMARY_BUS);
1156               b->secondary = get_conf_byte(d, PCI_SECONDARY_BUS);
1157               b->subordinate = get_conf_byte(d, PCI_SUBORDINATE_BUS);
1158             }
1159           *last_br = b;
1160           last_br = &b->chain;
1161           b->next = b->child = NULL;
1162           b->first_bus = NULL;
1163           b->br_dev = d;
1164         }
1165     }
1166   *last_br = NULL;
1167
1168   /* Create a bridge tree */
1169
1170   for(b=&host_bridge; b; b=b->chain)
1171     {
1172       struct bridge *c, *best;
1173       best = NULL;
1174       for(c=&host_bridge; c; c=c->chain)
1175         if (c != b && b->primary >= c->secondary && b->primary <= c->subordinate &&
1176             (!best || best->subordinate - best->primary > c->subordinate - c->primary))
1177           best = c;
1178       if (best)
1179         {
1180           b->next = best->child;
1181           best->child = b;
1182         }
1183     }
1184
1185   /* Insert secondary bus for each bridge */
1186
1187   for(b=&host_bridge; b; b=b->chain)
1188     if (!find_bus(b, b->secondary))
1189       new_bus(b, b->secondary);
1190
1191   /* Create bus structs and link devices */
1192
1193   for(d=first_dev; d;)
1194     {
1195       d2 = d->next;
1196       insert_dev(d, &host_bridge);
1197       d = d2;
1198     }
1199 }
1200
1201 static void
1202 print_it(byte *line, byte *p)
1203 {
1204   *p++ = '\n';
1205   *p = 0;
1206   fputs(line, stdout);
1207   for(p=line; *p; p++)
1208     if (*p == '+' || *p == '|')
1209       *p = '|';
1210     else
1211       *p = ' ';
1212 }
1213
1214 static void show_tree_bridge(struct bridge *, byte *, byte *);
1215
1216 static void
1217 show_tree_dev(struct device *d, byte *line, byte *p)
1218 {
1219   struct pci_dev *q = d->dev;
1220   struct bridge *b;
1221   char namebuf[256];
1222
1223   p += sprintf(p, "%02x.%x", q->dev, q->func);
1224   for(b=&host_bridge; b; b=b->chain)
1225     if (b->br_dev == d)
1226       {
1227         if (b->secondary == b->subordinate)
1228           p += sprintf(p, "-[%02x]-", b->secondary);
1229         else
1230           p += sprintf(p, "-[%02x-%02x]-", b->secondary, b->subordinate);
1231         show_tree_bridge(b, line, p);
1232         return;
1233       }
1234   if (verbose)
1235     p += sprintf(p, "  %s",
1236                  pci_lookup_name(pacc, namebuf, sizeof(namebuf),
1237                                  PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
1238                                  q->vendor_id, q->device_id, 0, 0));
1239   print_it(line, p);
1240 }
1241
1242 static void
1243 show_tree_bus(struct bus *b, byte *line, byte *p)
1244 {
1245   if (!b->first_dev)
1246     print_it(line, p);
1247   else if (!b->first_dev->next)
1248     {
1249       *p++ = '-';
1250       *p++ = '-';
1251       show_tree_dev(b->first_dev, line, p);
1252     }
1253   else
1254     {
1255       struct device *d = b->first_dev;
1256       while (d->next)
1257         {
1258           p[0] = '+';
1259           p[1] = '-';
1260           show_tree_dev(d, line, p+2);
1261           d = d->next;
1262         }
1263       p[0] = '\\';
1264       p[1] = '-';
1265       show_tree_dev(d, line, p+2);
1266     }
1267 }
1268
1269 static void
1270 show_tree_bridge(struct bridge *b, byte *line, byte *p)
1271 {
1272   *p++ = '-';
1273   if (!b->first_bus->sibling)
1274     {
1275       if (b == &host_bridge)
1276         p += sprintf(p, "[%02x]-", b->first_bus->number);
1277       show_tree_bus(b->first_bus, line, p);
1278     }
1279   else
1280     {
1281       struct bus *u = b->first_bus;
1282       byte *k;
1283
1284       while (u->sibling)
1285         {
1286           k = p + sprintf(p, "+-[%02x]-", u->number);
1287           show_tree_bus(u, line, k);
1288           u = u->sibling;
1289         }
1290       k = p + sprintf(p, "\\-[%02x]-", u->number);
1291       show_tree_bus(u, line, k);
1292     }
1293 }
1294
1295 static void
1296 show_forest(void)
1297 {
1298   char line[256];
1299
1300   grow_tree();
1301   show_tree_bridge(&host_bridge, line, line);
1302 }
1303
1304 /* Bus mapping mode */
1305
1306 struct bus_bridge {
1307   struct bus_bridge *next;
1308   byte this, dev, func, first, last, bug;
1309 };
1310
1311 struct bus_info {
1312   byte exists;
1313   byte guestbook;
1314   struct bus_bridge *bridges, *via;
1315 };
1316
1317 static struct bus_info *bus_info;
1318
1319 static void
1320 map_bridge(struct bus_info *bi, struct device *d, int np, int ns, int nl)
1321 {
1322   struct bus_bridge *b = xmalloc(sizeof(struct bus_bridge));
1323   struct pci_dev *p = d->dev;
1324
1325   b->next = bi->bridges;
1326   bi->bridges = b;
1327   b->this = get_conf_byte(d, np);
1328   b->dev = p->dev;
1329   b->func = p->func;
1330   b->first = get_conf_byte(d, ns);
1331   b->last = get_conf_byte(d, nl);
1332   printf("## %02x.%02x:%d is a bridge from %02x to %02x-%02x\n",
1333          p->bus, p->dev, p->func, b->this, b->first, b->last);
1334   if (b->this != p->bus)
1335     printf("!!! Bridge points to invalid primary bus.\n");
1336   if (b->first > b->last)
1337     {
1338       printf("!!! Bridge points to invalid bus range.\n");
1339       b->last = b->first;
1340     }
1341 }
1342
1343 static void
1344 do_map_bus(int bus)
1345 {
1346   int dev, func;
1347   int verbose = pacc->debugging;
1348   struct bus_info *bi = bus_info + bus;
1349   struct device *d;
1350
1351   if (verbose)
1352     printf("Mapping bus %02x\n", bus);
1353   for(dev = 0; dev < 32; dev++)
1354     if (filter.slot < 0 || filter.slot == dev)
1355       {
1356         int func_limit = 1;
1357         for(func = 0; func < func_limit; func++)
1358           if (filter.func < 0 || filter.func == func)
1359             {
1360               struct pci_dev *p = pci_get_dev(pacc, bus, dev, func);
1361               u16 vendor = pci_read_word(p, PCI_VENDOR_ID);
1362               if (vendor && vendor != 0xffff)
1363                 {
1364                   if (!func && (pci_read_byte(p, PCI_HEADER_TYPE) & 0x80))
1365                     func_limit = 8;
1366                   if (verbose)
1367                     printf("Discovered device %02x:%02x.%d\n", bus, dev, func);
1368                   bi->exists = 1;
1369                   if (d = scan_device(p))
1370                     {
1371                       show_device(d);
1372                       switch (get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f)
1373                         {
1374                         case PCI_HEADER_TYPE_BRIDGE:
1375                           map_bridge(bi, d, PCI_PRIMARY_BUS, PCI_SECONDARY_BUS, PCI_SUBORDINATE_BUS);
1376                           break;
1377                         case PCI_HEADER_TYPE_CARDBUS:
1378                           map_bridge(bi, d, PCI_CB_PRIMARY_BUS, PCI_CB_CARD_BUS, PCI_CB_SUBORDINATE_BUS);
1379                           break;
1380                         }
1381                       free(d);
1382                     }
1383                   else if (verbose)
1384                     printf("But it was filtered out.\n");
1385                 }
1386               pci_free_dev(p);
1387             }
1388       }
1389 }
1390
1391 static void
1392 do_map_bridges(int bus, int min, int max)
1393 {
1394   struct bus_info *bi = bus_info + bus;
1395   struct bus_bridge *b;
1396
1397   bi->guestbook = 1;
1398   for(b=bi->bridges; b; b=b->next)
1399     {
1400       if (bus_info[b->first].guestbook)
1401         b->bug = 1;
1402       else if (b->first < min || b->last > max)
1403         b->bug = 2;
1404       else
1405         {
1406           bus_info[b->first].via = b;
1407           do_map_bridges(b->first, b->first, b->last);
1408         }
1409     }
1410 }
1411
1412 static void
1413 map_bridges(void)
1414 {
1415   int i;
1416
1417   printf("\nSummary of buses:\n\n");
1418   for(i=0; i<256; i++)
1419     if (bus_info[i].exists && !bus_info[i].guestbook)
1420       do_map_bridges(i, 0, 255);
1421   for(i=0; i<256; i++)
1422     {
1423       struct bus_info *bi = bus_info + i;
1424       struct bus_bridge *b = bi->via;
1425
1426       if (bi->exists)
1427         {
1428           printf("%02x: ", i);
1429           if (b)
1430             printf("Entered via %02x:%02x.%d\n", b->this, b->dev, b->func);
1431           else if (!i)
1432             printf("Primary host bus\n");
1433           else
1434             printf("Secondary host bus (?)\n");
1435         }
1436       for(b=bi->bridges; b; b=b->next)
1437         {
1438           printf("\t%02x.%d Bridge to %02x-%02x", b->dev, b->func, b->first, b->last);
1439           switch (b->bug)
1440             {
1441             case 1:
1442               printf(" <overlap bug>");
1443               break;
1444             case 2:
1445               printf(" <crossing bug>");
1446               break;
1447             }
1448           putchar('\n');
1449         }
1450     }
1451 }
1452
1453 static void
1454 map_the_bus(void)
1455 {
1456   if (pacc->method == PCI_ACCESS_PROC_BUS_PCI ||
1457       pacc->method == PCI_ACCESS_DUMP)
1458     printf("WARNING: Bus mapping can be reliable only with direct hardware access enabled.\n\n");
1459   else if (!check_root())
1460     die("Only root can map the bus.");
1461   bus_info = xmalloc(sizeof(struct bus_info) * 256);
1462   bzero(bus_info, sizeof(struct bus_info) * 256);
1463   if (filter.bus >= 0)
1464     do_map_bus(filter.bus);
1465   else
1466     {
1467       int bus;
1468       for(bus=0; bus<256; bus++)
1469         do_map_bus(bus);
1470     }
1471   map_bridges();
1472 }
1473
1474 /* Main */
1475
1476 int
1477 main(int argc, char **argv)
1478 {
1479   int i;
1480   char *msg;
1481
1482   if (argc == 2 && !strcmp(argv[1], "--version"))
1483     {
1484       puts("lspci version " PCIUTILS_VERSION);
1485       return 0;
1486     }
1487
1488   pacc = pci_alloc();
1489   pacc->error = die;
1490   pci_filter_init(pacc, &filter);
1491
1492   while ((i = getopt(argc, argv, options)) != -1)
1493     switch (i)
1494       {
1495       case 'n':
1496         pacc->numeric_ids = 1;
1497         break;
1498       case 'v':
1499         verbose++;
1500         break;
1501       case 'b':
1502         pacc->buscentric = 1;
1503         buscentric_view = 1;
1504         break;
1505       case 's':
1506         if (msg = pci_filter_parse_slot(&filter, optarg))
1507           die("-f: %s", msg);
1508         break;
1509       case 'd':
1510         if (msg = pci_filter_parse_id(&filter, optarg))
1511           die("-d: %s", msg);
1512         break;
1513       case 'x':
1514         show_hex++;
1515         break;
1516       case 't':
1517         show_tree++;
1518         break;
1519       case 'i':
1520         pacc->id_file_name = optarg;
1521         break;
1522       case 'm':
1523         machine_readable++;
1524         break;
1525       case 'M':
1526         map_mode++;
1527         break;
1528       default:
1529         if (parse_generic_option(i, pacc, optarg))
1530           break;
1531       bad:
1532         fprintf(stderr, help_msg, pacc->id_file_name);
1533         return 1;
1534       }
1535   if (optind < argc)
1536     goto bad;
1537
1538   pci_init(pacc);
1539   if (map_mode)
1540     map_the_bus();
1541   else
1542     {
1543       scan_devices();
1544       sort_them();
1545       if (show_tree)
1546         show_forest();
1547       else
1548         show();
1549     }
1550   pci_cleanup(pacc);
1551
1552   return 0;
1553 }