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