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