]> mj.ucw.cz Git - pciutils.git/blob - lspci.c
3c07629e462aa229304b367dd1f6817d34d0a4f2
[pciutils.git] / lspci.c
1 /*
2  *      $Id: lspci.c,v 1.28 1999/08/20 08:30:44 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
540   show_bases(d, 2);
541   printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
542          get_conf_byte(d, PCI_PRIMARY_BUS),
543          get_conf_byte(d, PCI_SECONDARY_BUS),
544          get_conf_byte(d, PCI_SUBORDINATE_BUS),
545          get_conf_byte(d, PCI_SEC_LATENCY_TIMER));
546
547   if (io_type != (io_limit & PCI_IO_RANGE_TYPE_MASK) ||
548       (io_type != PCI_IO_RANGE_TYPE_16 && io_type != PCI_IO_RANGE_TYPE_32))
549     printf("\t!!! Unknown I/O range types %x/%x\n", io_base, io_limit);
550   else
551     {
552       io_base = (io_base & PCI_IO_RANGE_MASK) << 8;
553       io_limit = (io_limit & PCI_IO_RANGE_MASK) << 8;
554       if (io_type == PCI_IO_RANGE_TYPE_32)
555         {
556           io_base |= (get_conf_word(d, PCI_IO_BASE_UPPER16) << 16);
557           io_limit |= (get_conf_word(d, PCI_IO_LIMIT_UPPER16) << 16);
558         }
559       if (io_base)
560         printf("\tI/O behind bridge: %08x-%08x\n", io_base, io_limit+0xfff);
561     }
562
563   if (mem_type != (mem_limit & PCI_MEMORY_RANGE_TYPE_MASK) ||
564       mem_type)
565     printf("\t!!! Unknown memory range types %x/%x\n", mem_base, mem_limit);
566   else if (mem_base)
567     {
568       mem_base = (mem_base & PCI_MEMORY_RANGE_MASK) << 16;
569       mem_limit = (mem_limit & PCI_MEMORY_RANGE_MASK) << 16;
570       printf("\tMemory behind bridge: %08x-%08x\n", mem_base, mem_limit + 0xfffff);
571     }
572
573   if (pref_type != (pref_limit & PCI_PREF_RANGE_TYPE_MASK) ||
574       (pref_type != PCI_PREF_RANGE_TYPE_32 && pref_type != PCI_PREF_RANGE_TYPE_64))
575     printf("\t!!! Unknown prefetchable memory range types %x/%x\n", pref_base, pref_limit);
576   else if (pref_base)
577     {
578       pref_base = (pref_base & PCI_PREF_RANGE_MASK) << 16;
579       pref_limit = (pref_limit & PCI_PREF_RANGE_MASK) << 16;
580       if (pref_type == PCI_PREF_RANGE_TYPE_32)
581         printf("\tPrefetchable memory behind bridge: %08x-%08x\n", pref_base, pref_limit + 0xfffff);
582       else
583         printf("\tPrefetchable memory behind bridge: %08x%08x-%08x%08x\n",
584                get_conf_long(d, PCI_PREF_BASE_UPPER32),
585                pref_base,
586                get_conf_long(d, PCI_PREF_LIMIT_UPPER32),
587                pref_limit);
588     }
589
590   if (get_conf_word(d, PCI_SEC_STATUS) & PCI_STATUS_SIG_SYSTEM_ERROR)
591     printf("\tSecondary status: SERR\n");
592
593   show_rom(d);
594
595   if (verbose > 1)
596     printf("\tBridgeCtl: Parity%c SERR%c NoISA%c VGA%c MAbort%c >Reset%c FastB2B%c\n",
597            FLAG(brc, PCI_BRIDGE_CTL_PARITY),
598            FLAG(brc, PCI_BRIDGE_CTL_SERR),
599            FLAG(brc, PCI_BRIDGE_CTL_NO_ISA),
600            FLAG(brc, PCI_BRIDGE_CTL_VGA),
601            FLAG(brc, PCI_BRIDGE_CTL_MASTER_ABORT),
602            FLAG(brc, PCI_BRIDGE_CTL_BUS_RESET),
603            FLAG(brc, PCI_BRIDGE_CTL_FAST_BACK));
604
605   show_caps(d);
606 }
607
608 static void
609 show_htype2(struct device *d)
610 {
611   int i;
612   word cmd = get_conf_word(d, PCI_COMMAND);
613   word brc = get_conf_word(d, PCI_CB_BRIDGE_CONTROL);
614   word exca = get_conf_word(d, PCI_CB_LEGACY_MODE_BASE);
615
616   show_bases(d, 1);
617   printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
618          get_conf_byte(d, PCI_CB_PRIMARY_BUS),
619          get_conf_byte(d, PCI_CB_CARD_BUS),
620          get_conf_byte(d, PCI_CB_SUBORDINATE_BUS),
621          get_conf_byte(d, PCI_CB_LATENCY_TIMER));
622   for(i=0; i<2; i++)
623     {
624       int p = 8*i;
625       u32 base = get_conf_long(d, PCI_CB_MEMORY_BASE_0 + p);
626       u32 limit = get_conf_long(d, PCI_CB_MEMORY_LIMIT_0 + p);
627       if (limit > base)
628         printf("Memory window %d: %08x-%08x%s%s\n", i, base, limit,
629                (cmd & PCI_COMMAND_MEMORY) ? "" : " [disabled]",
630                (brc & (PCI_CB_BRIDGE_CTL_PREFETCH_MEM0 << i)) ? " (prefetchable)" : "");
631     }
632   for(i=0; i<2; i++)
633     {
634       int p = 8*i;
635       u32 base = get_conf_long(d, PCI_CB_IO_BASE_0 + p);
636       u32 limit = get_conf_long(d, PCI_CB_IO_LIMIT_0 + p);
637       if (!(base & PCI_IO_RANGE_TYPE_32))
638         {
639           base &= 0xffff;
640           limit &= 0xffff;
641         }
642       base &= PCI_CB_IO_RANGE_MASK;
643       if (!base)
644         continue;
645       limit = (limit & PCI_CB_IO_RANGE_MASK) + 3;
646       printf("I/O window %d: %08x-%08x%s\n", i, base, limit,
647              (cmd & PCI_COMMAND_IO) ? "" : " [disabled]");
648     }
649
650   if (get_conf_word(d, PCI_CB_SEC_STATUS) & PCI_STATUS_SIG_SYSTEM_ERROR)
651     printf("\tSecondary status: SERR\n");
652   if (verbose > 1)
653     printf("\tBridgeCtl: Parity%c SERR%c ISA%c VGA%c MAbort%c >Reset%c 16bInt%c PostWrite%c\n",
654            FLAG(brc, PCI_CB_BRIDGE_CTL_PARITY),
655            FLAG(brc, PCI_CB_BRIDGE_CTL_SERR),
656            FLAG(brc, PCI_CB_BRIDGE_CTL_ISA),
657            FLAG(brc, PCI_CB_BRIDGE_CTL_VGA),
658            FLAG(brc, PCI_CB_BRIDGE_CTL_MASTER_ABORT),
659            FLAG(brc, PCI_CB_BRIDGE_CTL_CB_RESET),
660            FLAG(brc, PCI_CB_BRIDGE_CTL_16BIT_INT),
661            FLAG(brc, PCI_CB_BRIDGE_CTL_POST_WRITES));
662   if (exca)
663     printf("\t16-bit legacy interface ports at %04x\n", exca);
664 }
665
666 static void
667 show_verbose(struct device *d)
668 {
669   struct pci_dev *p = d->dev;
670   word status = get_conf_word(d, PCI_STATUS);
671   word cmd = get_conf_word(d, PCI_COMMAND);
672   word class = get_conf_word(d, PCI_CLASS_DEVICE);
673   byte bist = get_conf_byte(d, PCI_BIST);
674   byte htype = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
675   byte latency = get_conf_byte(d, PCI_LATENCY_TIMER);
676   byte cache_line = get_conf_byte(d, PCI_CACHE_LINE_SIZE);
677   byte max_lat, min_gnt;
678   byte int_pin = get_conf_byte(d, PCI_INTERRUPT_PIN);
679   unsigned int irq = p->irq;
680   word subsys_v, subsys_d;
681   char ssnamebuf[256];
682
683   show_terse(d);
684
685   switch (htype)
686     {
687     case PCI_HEADER_TYPE_NORMAL:
688       if (class == PCI_CLASS_BRIDGE_PCI)
689         {
690         badhdr:
691           printf("\t!!! Header type %02x doesn't match class code %04x\n", htype, class);
692           return;
693         }
694       max_lat = get_conf_byte(d, PCI_MAX_LAT);
695       min_gnt = get_conf_byte(d, PCI_MIN_GNT);
696       subsys_v = get_conf_word(d, PCI_SUBSYSTEM_VENDOR_ID);
697       subsys_d = get_conf_word(d, PCI_SUBSYSTEM_ID);
698       break;
699     case PCI_HEADER_TYPE_BRIDGE:
700       if (class != PCI_CLASS_BRIDGE_PCI)
701         goto badhdr;
702       irq = int_pin = min_gnt = max_lat = 0;
703       subsys_v = subsys_d = 0;
704       break;
705     case PCI_HEADER_TYPE_CARDBUS:
706       if ((class >> 8) != PCI_BASE_CLASS_BRIDGE)
707         goto badhdr;
708       min_gnt = max_lat = 0;
709       subsys_v = get_conf_word(d, PCI_CB_SUBSYSTEM_VENDOR_ID);
710       subsys_d = get_conf_word(d, PCI_CB_SUBSYSTEM_ID);
711       break;
712     default:
713       printf("\t!!! Unknown header type %02x\n", htype);
714       return;
715     }
716
717   if (verbose && subsys_v && subsys_v != 0xffff)
718     printf("\tSubsystem: %s\n",
719            pci_lookup_name(pacc, ssnamebuf, sizeof(ssnamebuf),
720                            PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
721                            subsys_v, subsys_d));
722
723   if (verbose > 1)
724     {
725       printf("\tControl: I/O%c Mem%c BusMaster%c SpecCycle%c MemWINV%c VGASnoop%c ParErr%c Stepping%c SERR%c FastB2B%c\n",
726              FLAG(cmd, PCI_COMMAND_IO),
727              FLAG(cmd, PCI_COMMAND_MEMORY),
728              FLAG(cmd, PCI_COMMAND_MASTER),
729              FLAG(cmd, PCI_COMMAND_SPECIAL),
730              FLAG(cmd, PCI_COMMAND_INVALIDATE),
731              FLAG(cmd, PCI_COMMAND_VGA_PALETTE),
732              FLAG(cmd, PCI_COMMAND_PARITY),
733              FLAG(cmd, PCI_COMMAND_WAIT),
734              FLAG(cmd, PCI_COMMAND_SERR),
735              FLAG(cmd, PCI_COMMAND_FAST_BACK));
736       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",
737              FLAG(status, PCI_STATUS_CAP_LIST),
738              FLAG(status, PCI_STATUS_66MHZ),
739              FLAG(status, PCI_STATUS_UDF),
740              FLAG(status, PCI_STATUS_FAST_BACK),
741              FLAG(status, PCI_STATUS_PARITY),
742              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
743              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
744              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??",
745              FLAG(status, PCI_STATUS_SIG_TARGET_ABORT),
746              FLAG(status, PCI_STATUS_REC_TARGET_ABORT),
747              FLAG(status, PCI_STATUS_REC_MASTER_ABORT),
748              FLAG(status, PCI_STATUS_SIG_SYSTEM_ERROR),
749              FLAG(status, PCI_STATUS_DETECTED_PARITY));
750       if (cmd & PCI_COMMAND_MASTER)
751         {
752           printf("\tLatency: ");
753           if (min_gnt)
754             printf("%d min, ", min_gnt);
755           if (max_lat)
756             printf("%d max, ", max_lat);
757           printf("%d set", latency);
758           if (cache_line)
759             printf(", cache line size %02x", cache_line);
760           putchar('\n');
761         }
762       if (int_pin || irq)
763         printf("\tInterrupt: pin %c routed to IRQ " IRQ_FORMAT "\n",
764                (int_pin ? 'A' + int_pin - 1 : '?'), irq);
765     }
766   else
767     {
768       printf("\tFlags: ");
769       if (cmd & PCI_COMMAND_MASTER)
770         printf("bus master, ");
771       if (cmd & PCI_COMMAND_VGA_PALETTE)
772         printf("VGA palette snoop, ");
773       if (cmd & PCI_COMMAND_WAIT)
774         printf("stepping, ");
775       if (cmd & PCI_COMMAND_FAST_BACK)
776         printf("fast Back2Back, ");
777       if (status & PCI_STATUS_66MHZ)
778         printf("66Mhz, ");
779       if (status & PCI_STATUS_UDF)
780         printf("user-definable features, ");
781       printf("%s devsel",
782              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
783              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
784              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??");
785       if (cmd & PCI_COMMAND_MASTER)
786         printf(", latency %d", latency);
787       if (irq)
788         printf(", IRQ " IRQ_FORMAT, irq);
789       putchar('\n');
790     }
791
792   if (bist & PCI_BIST_CAPABLE)
793     {
794       if (bist & PCI_BIST_START)
795         printf("\tBIST is running\n");
796       else
797         printf("\tBIST result: %02x\n", bist & PCI_BIST_CODE_MASK);
798     }
799
800   switch (htype)
801     {
802     case PCI_HEADER_TYPE_NORMAL:
803       show_htype0(d);
804       break;
805     case PCI_HEADER_TYPE_BRIDGE:
806       show_htype1(d);
807       break;
808     case PCI_HEADER_TYPE_CARDBUS:
809       show_htype2(d);
810       break;
811     }
812 }
813
814 static void
815 show_hex_dump(struct device *d)
816 {
817   unsigned int i;
818
819   for(i=0; i<d->config_cnt; i++)
820     {
821       if (! (i & 15))
822         printf("%02x:", i);
823       printf(" %02x", get_conf_byte(d, i));
824       if ((i & 15) == 15)
825         putchar('\n');
826     }
827 }
828
829 static void
830 show_machine(struct device *d)
831 {
832   struct pci_dev *p = d->dev;
833   int c;
834   word sv_id=0, sd_id=0;
835   char classbuf[128], vendbuf[128], devbuf[128], svbuf[128], sdbuf[128];
836
837   switch (get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f)
838     {
839     case PCI_HEADER_TYPE_NORMAL:
840       sv_id = get_conf_word(d, PCI_SUBSYSTEM_VENDOR_ID);
841       sd_id = get_conf_word(d, PCI_SUBSYSTEM_ID);
842       break;
843     case PCI_HEADER_TYPE_CARDBUS:
844       sv_id = get_conf_word(d, PCI_CB_SUBSYSTEM_VENDOR_ID);
845       sd_id = get_conf_word(d, PCI_CB_SUBSYSTEM_ID);
846       break;
847     }
848
849   if (verbose)
850     {
851       printf("Device:\t%02x:%02x.%x\n", p->bus, p->dev, p->func);
852       printf("Class:\t%s\n",
853              pci_lookup_name(pacc, classbuf, sizeof(classbuf), PCI_LOOKUP_CLASS, get_conf_word(d, PCI_CLASS_DEVICE), 0));
854       printf("Vendor:\t%s\n",
855              pci_lookup_name(pacc, vendbuf, sizeof(vendbuf), PCI_LOOKUP_VENDOR, p->vendor_id, p->device_id));
856       printf("Device:\t%s\n",
857              pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id));
858       if (sv_id && sv_id != 0xffff)
859         {
860           printf("SVendor:\t%s\n",
861                  pci_lookup_name(pacc, svbuf, sizeof(svbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR, sv_id, sd_id));
862           printf("SDevice:\t%s\n",
863                  pci_lookup_name(pacc, sdbuf, sizeof(sdbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE, sv_id, sd_id));
864         }
865       if (c = get_conf_byte(d, PCI_REVISION_ID))
866         printf("Rev:\t%02x\n", c);
867       if (c = get_conf_byte(d, PCI_CLASS_PROG))
868         printf("ProgIf:\t%02x\n", c);
869     }
870   else
871     {
872       printf("%02x:%02x.%x ", p->bus, p->dev, p->func);
873       printf("\"%s\" \"%s\" \"%s\"",
874              pci_lookup_name(pacc, classbuf, sizeof(classbuf), PCI_LOOKUP_CLASS,
875                              get_conf_word(d, PCI_CLASS_DEVICE), 0),
876              pci_lookup_name(pacc, vendbuf, sizeof(vendbuf), PCI_LOOKUP_VENDOR,
877                              p->vendor_id, p->device_id),
878              pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_DEVICE,
879                              p->vendor_id, p->device_id));
880       if (c = get_conf_byte(d, PCI_REVISION_ID))
881         printf(" -r%02x", c);
882       if (c = get_conf_byte(d, PCI_CLASS_PROG))
883         printf(" -p%02x", c);
884       if (sv_id && sv_id != 0xffff)
885         printf(" \"%s\" \"%s\"",
886                pci_lookup_name(pacc, svbuf, sizeof(svbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR, sv_id, sd_id),
887                pci_lookup_name(pacc, sdbuf, sizeof(sdbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE, sv_id, sd_id));
888       else
889         printf(" \"\" \"\"");
890       putchar('\n');
891     }
892 }
893
894 static void
895 show_device(struct device *d)
896 {
897   if (machine_readable)
898     show_machine(d);
899   else if (verbose)
900     show_verbose(d);
901   else
902     show_terse(d);
903   if (show_hex)
904     show_hex_dump(d);
905   if (verbose || show_hex)
906     putchar('\n');
907 }
908
909 static void
910 show(void)
911 {
912   struct device *d;
913
914   for(d=first_dev; d; d=d->next)
915     show_device(d);
916 }
917
918 /* Tree output */
919
920 struct bridge {
921   struct bridge *chain;                 /* Single-linked list of bridges */
922   struct bridge *next, *child;          /* Tree of bridges */
923   struct bus *first_bus;                /* List of busses connected to this bridge */
924   unsigned int primary, secondary, subordinate; /* Bus numbers */
925   struct device *br_dev;
926 };
927
928 struct bus {
929   unsigned int number;
930   struct bus *sibling;
931   struct device *first_dev, **last_dev;
932 };
933
934 static struct bridge host_bridge = { NULL, NULL, NULL, NULL, ~0, 0, ~0, NULL };
935
936 static struct bus *
937 find_bus(struct bridge *b, unsigned int n)
938 {
939   struct bus *bus;
940
941   for(bus=b->first_bus; bus; bus=bus->sibling)
942     if (bus->number == n)
943       break;
944   return bus;
945 }
946
947 static struct bus *
948 new_bus(struct bridge *b, unsigned int n)
949 {
950   struct bus *bus = xmalloc(sizeof(struct bus));
951
952   bus = xmalloc(sizeof(struct bus));
953   bus->number = n;
954   bus->sibling = b->first_bus;
955   bus->first_dev = NULL;
956   bus->last_dev = &bus->first_dev;
957   b->first_bus = bus;
958   return bus;
959 }
960
961 static void
962 insert_dev(struct device *d, struct bridge *b)
963 {
964   struct pci_dev *p = d->dev;
965   struct bus *bus;
966
967   if (! (bus = find_bus(b, p->bus)))
968     {
969       struct bridge *c;
970       for(c=b->child; c; c=c->next)
971         if (c->secondary <= p->bus && p->bus <= c->subordinate)
972           return insert_dev(d, c);
973       bus = new_bus(b, p->bus);
974     }
975   /* Simple insertion at the end _does_ guarantee the correct order as the
976    * original device list was sorted by (bus, devfn) lexicographically
977    * and all devices on the new list have the same bus number.
978    */
979   *bus->last_dev = d;
980   bus->last_dev = &d->next;
981   d->next = NULL;
982 }
983
984 static void
985 grow_tree(void)
986 {
987   struct device *d, *d2;
988   struct bridge **last_br, *b;
989
990   /* Build list of bridges */
991
992   last_br = &host_bridge.chain;
993   for(d=first_dev; d; d=d->next)
994     {
995       word class = get_conf_word(d, PCI_CLASS_DEVICE);
996       byte ht = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
997       if (class == PCI_CLASS_BRIDGE_PCI &&
998           (ht == PCI_HEADER_TYPE_BRIDGE || ht == PCI_HEADER_TYPE_CARDBUS))
999         {
1000           b = xmalloc(sizeof(struct bridge));
1001           if (ht == PCI_HEADER_TYPE_BRIDGE)
1002             {
1003               b->primary = get_conf_byte(d, PCI_CB_PRIMARY_BUS);
1004               b->secondary = get_conf_byte(d, PCI_CB_CARD_BUS);
1005               b->subordinate = get_conf_byte(d, PCI_CB_SUBORDINATE_BUS);
1006             }
1007           else
1008             {
1009               b->primary = get_conf_byte(d, PCI_PRIMARY_BUS);
1010               b->secondary = get_conf_byte(d, PCI_SECONDARY_BUS);
1011               b->subordinate = get_conf_byte(d, PCI_SUBORDINATE_BUS);
1012             }
1013           *last_br = b;
1014           last_br = &b->chain;
1015           b->next = b->child = NULL;
1016           b->first_bus = NULL;
1017           b->br_dev = d;
1018         }
1019     }
1020   *last_br = NULL;
1021
1022   /* Create a bridge tree */
1023
1024   for(b=&host_bridge; b; b=b->chain)
1025     {
1026       struct bridge *c, *best;
1027       best = NULL;
1028       for(c=&host_bridge; c; c=c->chain)
1029         if (c != b && b->primary >= c->secondary && b->primary <= c->subordinate &&
1030             (!best || best->subordinate - best->primary > c->subordinate - c->primary))
1031           best = c;
1032       if (best)
1033         {
1034           b->next = best->child;
1035           best->child = b;
1036         }
1037     }
1038
1039   /* Insert secondary bus for each bridge */
1040
1041   for(b=&host_bridge; b; b=b->chain)
1042     if (!find_bus(b, b->secondary))
1043       new_bus(b, b->secondary);
1044
1045   /* Create bus structs and link devices */
1046
1047   for(d=first_dev; d;)
1048     {
1049       d2 = d->next;
1050       insert_dev(d, &host_bridge);
1051       d = d2;
1052     }
1053 }
1054
1055 static void
1056 print_it(byte *line, byte *p)
1057 {
1058   *p++ = '\n';
1059   *p = 0;
1060   fputs(line, stdout);
1061   for(p=line; *p; p++)
1062     if (*p == '+' || *p == '|')
1063       *p = '|';
1064     else
1065       *p = ' ';
1066 }
1067
1068 static void show_tree_bridge(struct bridge *, byte *, byte *);
1069
1070 static void
1071 show_tree_dev(struct device *d, byte *line, byte *p)
1072 {
1073   struct pci_dev *q = d->dev;
1074   struct bridge *b;
1075   char namebuf[256];
1076
1077   p += sprintf(p, "%02x.%x", q->dev, q->func);
1078   for(b=&host_bridge; b; b=b->chain)
1079     if (b->br_dev == d)
1080       {
1081         if (b->secondary == b->subordinate)
1082           p += sprintf(p, "-[%02x]-", b->secondary);
1083         else
1084           p += sprintf(p, "-[%02x-%02x]-", b->secondary, b->subordinate);
1085         show_tree_bridge(b, line, p);
1086         return;
1087       }
1088   if (verbose)
1089     p += sprintf(p, "  %s",
1090                  pci_lookup_name(pacc, namebuf, sizeof(namebuf),
1091                                  PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
1092                                  q->vendor_id, q->device_id));
1093   print_it(line, p);
1094 }
1095
1096 static void
1097 show_tree_bus(struct bus *b, byte *line, byte *p)
1098 {
1099   if (!b->first_dev)
1100     print_it(line, p);
1101   else if (!b->first_dev->next)
1102     {
1103       *p++ = '-';
1104       *p++ = '-';
1105       show_tree_dev(b->first_dev, line, p);
1106     }
1107   else
1108     {
1109       struct device *d = b->first_dev;
1110       while (d->next)
1111         {
1112           p[0] = '+';
1113           p[1] = '-';
1114           show_tree_dev(d, line, p+2);
1115           d = d->next;
1116         }
1117       p[0] = '\\';
1118       p[1] = '-';
1119       show_tree_dev(d, line, p+2);
1120     }
1121 }
1122
1123 static void
1124 show_tree_bridge(struct bridge *b, byte *line, byte *p)
1125 {
1126   *p++ = '-';
1127   if (!b->first_bus->sibling)
1128     {
1129       if (b == &host_bridge)
1130         p += sprintf(p, "[%02x]-", b->first_bus->number);
1131       show_tree_bus(b->first_bus, line, p);
1132     }
1133   else
1134     {
1135       struct bus *u = b->first_bus;
1136       byte *k;
1137
1138       while (u->sibling)
1139         {
1140           k = p + sprintf(p, "+-[%02x]-", u->number);
1141           show_tree_bus(u, line, k);
1142           u = u->sibling;
1143         }
1144       k = p + sprintf(p, "\\-[%02x]-", u->number);
1145       show_tree_bus(u, line, k);
1146     }
1147 }
1148
1149 static void
1150 show_forest(void)
1151 {
1152   char line[256];
1153
1154   grow_tree();
1155   show_tree_bridge(&host_bridge, line, line);
1156 }
1157
1158 /* Bus mapping mode */
1159
1160 struct bus_bridge {
1161   struct bus_bridge *next;
1162   byte this, dev, func, first, last, bug;
1163 };
1164
1165 struct bus_info {
1166   byte exists;
1167   byte guestbook;
1168   struct bus_bridge *bridges, *via;
1169 };
1170
1171 static struct bus_info *bus_info;
1172
1173 static void
1174 map_bridge(struct bus_info *bi, struct device *d, int np, int ns, int nl)
1175 {
1176   struct bus_bridge *b = xmalloc(sizeof(struct bus_bridge));
1177   struct pci_dev *p = d->dev;
1178
1179   b->next = bi->bridges;
1180   bi->bridges = b;
1181   b->this = get_conf_byte(d, np);
1182   b->dev = p->dev;
1183   b->func = p->func;
1184   b->first = get_conf_byte(d, ns);
1185   b->last = get_conf_byte(d, nl);
1186   printf("## %02x.%02x:%d is a bridge from %02x to %02x-%02x\n",
1187          p->bus, p->dev, p->func, b->this, b->first, b->last);
1188   if (b->this != p->bus)
1189     printf("!!! Bridge points to invalid primary bus.\n");
1190   if (b->first > b->last)
1191     {
1192       printf("!!! Bridge points to invalid bus range.\n");
1193       b->last = b->first;
1194     }
1195 }
1196
1197 static void
1198 do_map_bus(int bus)
1199 {
1200   int dev, func;
1201   int verbose = pacc->debugging;
1202   struct bus_info *bi = bus_info + bus;
1203   struct device *d;
1204
1205   if (verbose)
1206     printf("Mapping bus %02x\n", bus);
1207   for(dev = 0; dev < 32; dev++)
1208     if (filter.slot < 0 || filter.slot == dev)
1209       {
1210         int func_limit = 1;
1211         for(func = 0; func < func_limit; func++)
1212           if (filter.func < 0 || filter.func == func)
1213             {
1214               struct pci_dev *p = pci_get_dev(pacc, bus, dev, func);
1215               u16 vendor = pci_read_word(p, PCI_VENDOR_ID);
1216               if (vendor && vendor != 0xffff)
1217                 {
1218                   if (!func && (pci_read_byte(p, PCI_HEADER_TYPE) & 0x80))
1219                     func_limit = 8;
1220                   if (verbose)
1221                     printf("Discovered device %02x:%02x.%d\n", bus, dev, func);
1222                   bi->exists = 1;
1223                   if (d = scan_device(p))
1224                     {
1225                       show_device(d);
1226                       switch (get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f)
1227                         {
1228                         case PCI_HEADER_TYPE_BRIDGE:
1229                           map_bridge(bi, d, PCI_PRIMARY_BUS, PCI_SECONDARY_BUS, PCI_SUBORDINATE_BUS);
1230                           break;
1231                         case PCI_HEADER_TYPE_CARDBUS:
1232                           map_bridge(bi, d, PCI_CB_PRIMARY_BUS, PCI_CB_CARD_BUS, PCI_CB_SUBORDINATE_BUS);
1233                           break;
1234                         }
1235                       free(d);
1236                     }
1237                   else if (verbose)
1238                     printf("But it was filtered out.\n");
1239                 }
1240               pci_free_dev(p);
1241             }
1242       }
1243 }
1244
1245 static void
1246 do_map_bridges(int bus, int min, int max)
1247 {
1248   struct bus_info *bi = bus_info + bus;
1249   struct bus_bridge *b;
1250
1251   bi->guestbook = 1;
1252   for(b=bi->bridges; b; b=b->next)
1253     {
1254       if (bus_info[b->first].guestbook)
1255         b->bug = 1;
1256       else if (b->first < min || b->last > max)
1257         b->bug = 2;
1258       else
1259         {
1260           bus_info[b->first].via = b;
1261           do_map_bridges(b->first, b->first, b->last);
1262         }
1263     }
1264 }
1265
1266 static void
1267 map_bridges(void)
1268 {
1269   int i;
1270
1271   printf("\nSummary of buses:\n\n");
1272   for(i=0; i<256; i++)
1273     if (bus_info[i].exists && !bus_info[i].guestbook)
1274       do_map_bridges(i, 0, 255);
1275   for(i=0; i<256; i++)
1276     {
1277       struct bus_info *bi = bus_info + i;
1278       struct bus_bridge *b = bi->via;
1279
1280       if (bi->exists)
1281         {
1282           printf("%02x: ", i);
1283           if (b)
1284             printf("Entered via %02x:%02x.%d\n", b->this, b->dev, b->func);
1285           else if (!i)
1286             printf("Primary host bus\n");
1287           else
1288             printf("Secondary host bus (?)\n");
1289         }
1290       for(b=bi->bridges; b; b=b->next)
1291         {
1292           printf("\t%02x.%d Bridge to %02x-%02x", b->dev, b->func, b->first, b->last);
1293           switch (b->bug)
1294             {
1295             case 1:
1296               printf(" <overlap bug>");
1297               break;
1298             case 2:
1299               printf(" <crossing bug>");
1300               break;
1301             }
1302           putchar('\n');
1303         }
1304     }
1305 }
1306
1307 static void
1308 map_the_bus(void)
1309 {
1310   if (pacc->method == PCI_ACCESS_PROC_BUS_PCI ||
1311       pacc->method == PCI_ACCESS_DUMP)
1312     printf("WARNING: Bus mapping can be reliable only with direct hardware access enabled.\n\n");
1313   else if (!check_root())
1314     die("Only root can map the bus.");
1315   bus_info = xmalloc(sizeof(struct bus_info) * 256);
1316   bzero(bus_info, sizeof(struct bus_info) * 256);
1317   if (filter.bus >= 0)
1318     do_map_bus(filter.bus);
1319   else
1320     {
1321       int bus;
1322       for(bus=0; bus<256; bus++)
1323         do_map_bus(bus);
1324     }
1325   map_bridges();
1326 }
1327
1328 /* Main */
1329
1330 int
1331 main(int argc, char **argv)
1332 {
1333   int i;
1334   char *msg;
1335
1336   if (argc == 2 && !strcmp(argv[1], "--version"))
1337     {
1338       puts("lspci version " PCIUTILS_VERSION);
1339       return 0;
1340     }
1341
1342   pacc = pci_alloc();
1343   pacc->error = die;
1344   pci_filter_init(pacc, &filter);
1345
1346   while ((i = getopt(argc, argv, options)) != -1)
1347     switch (i)
1348       {
1349       case 'n':
1350         pacc->numeric_ids = 1;
1351         break;
1352       case 'v':
1353         verbose++;
1354         break;
1355       case 'b':
1356         pacc->buscentric = 1;
1357         buscentric_view = 1;
1358         break;
1359       case 's':
1360         if (msg = pci_filter_parse_slot(&filter, optarg))
1361           die("-f: %s", msg);
1362         break;
1363       case 'd':
1364         if (msg = pci_filter_parse_id(&filter, optarg))
1365           die("-d: %s", msg);
1366         break;
1367       case 'x':
1368         show_hex++;
1369         break;
1370       case 't':
1371         show_tree++;
1372         break;
1373       case 'i':
1374         pacc->id_file_name = optarg;
1375         break;
1376       case 'm':
1377         machine_readable++;
1378         break;
1379       case 'M':
1380         map_mode++;
1381         break;
1382       default:
1383         if (parse_generic_option(i, pacc, optarg))
1384           break;
1385       bad:
1386         fprintf(stderr, help_msg, pacc->id_file_name);
1387         return 1;
1388       }
1389   if (optind < argc)
1390     goto bad;
1391
1392   pci_init(pacc);
1393   if (map_mode)
1394     map_the_bus();
1395   else
1396     {
1397       scan_devices();
1398       sort_them();
1399       if (show_tree)
1400         show_forest();
1401       else
1402         show();
1403     }
1404   pci_cleanup(pacc);
1405
1406   return 0;
1407 }