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