]> mj.ucw.cz Git - pciutils.git/blob - lspci.c
Added support for the `bridge subsystem ID' capability.
[pciutils.git] / lspci.c
1 /*
2  *      The PCI Utilities -- List All PCI Devices
3  *
4  *      Copyright (c) 1997--2006 Martin Mares <mj@ucw.cz>
5  *
6  *      Can be freely distributed and used under the terms of the GNU GPL.
7  */
8
9 #include <stdio.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include <stdarg.h>
13 #include <unistd.h>
14
15 #include "pciutils.h"
16
17 /* Options */
18
19 static int verbose;                     /* Show detailed information */
20 static int buscentric_view;             /* Show bus addresses/IRQ's instead of CPU-visible ones */
21 static int show_hex;                    /* Show contents of config space as hexadecimal numbers */
22 static struct pci_filter filter;        /* Device filter */
23 static int show_tree;                   /* Show bus tree */
24 static int machine_readable;            /* Generate machine-readable output */
25 static int map_mode;                    /* Bus mapping mode enabled */
26 static int show_domains;                /* Show domain numbers (0=disabled, 1=auto-detected, 2=requested) */
27
28 const char program_name[] = "lspci";
29
30 static char options[] = "nvbxs:d:ti:mgMD" GENERIC_OPTIONS ;
31
32 static char help_msg[] = "\
33 Usage: lspci [<switches>]\n\
34 \n\
35 -v\t\tBe verbose\n\
36 -n\t\tShow numeric ID's\n\
37 -nn\t\tShow both textual and numeric ID's (names & numbers)\n\
38 -b\t\tBus-centric view (PCI addresses and IRQ's instead of those seen by the CPU)\n\
39 -x\t\tShow hex-dump of the standard portion of config space\n\
40 -xxx\t\tShow hex-dump of the whole config space (dangerous; root only)\n\
41 -xxxx\t\tShow hex-dump of the 4096-byte extended config space (root only)\n\
42 -s [[[[<domain>]:]<bus>]:][<slot>][.[<func>]]\tShow only devices in selected slots\n\
43 -d [<vendor>]:[<device>]\tShow only selected devices\n\
44 -t\t\tShow bus tree\n\
45 -m\t\tProduce machine-readable output\n\
46 -i <file>\tUse specified ID database instead of %s\n\
47 -D\t\tAlways show domain numbers\n\
48 -M\t\tEnable `bus mapping' mode (dangerous; root only)\n"
49 GENERIC_HELP
50 ;
51
52 /* Communication with libpci */
53
54 static struct pci_access *pacc;
55
56 /*
57  *  If we aren't being compiled by GCC, use xmalloc() instead of alloca().
58  *  This increases our memory footprint, but only slightly since we don't
59  *  use alloca() much.
60  */
61
62 #ifndef __GNUC__
63 #undef alloca
64 #define alloca xmalloc
65 #endif
66
67 /* Our view of the PCI bus */
68
69 struct device {
70   struct device *next;
71   struct pci_dev *dev;
72   unsigned int config_cached, config_bufsize;
73   byte *config;                         /* Cached configuration space data */
74   byte *present;                        /* Maps which configuration bytes are present */
75 };
76
77 static struct device *first_dev;
78
79 static int
80 config_fetch(struct device *d, unsigned int pos, unsigned int len)
81 {
82   unsigned int end = pos+len;
83   int result;
84
85   while (pos < d->config_bufsize && len && d->present[pos])
86     pos++, len--;
87   while (pos+len <= d->config_bufsize && len && d->present[pos+len-1])
88     len--;
89   if (!len)
90     return 1;
91
92   if (end > d->config_bufsize)
93     {
94       int orig_size = d->config_bufsize;
95       while (end > d->config_bufsize)
96         d->config_bufsize *= 2;
97       d->config = xrealloc(d->config, d->config_bufsize);
98       d->present = xrealloc(d->present, d->config_bufsize);
99       bzero(d->present + orig_size, d->config_bufsize - orig_size);
100     }
101   result = pci_read_block(d->dev, pos, d->config + pos, len);
102   if (result)
103     memset(d->present + pos, 1, len);
104   return result;
105 }
106
107 static struct device *
108 scan_device(struct pci_dev *p)
109 {
110   struct device *d;
111
112   if (p->domain && !show_domains)
113     show_domains = 1;
114   if (!pci_filter_match(&filter, p))
115     return NULL;
116   d = xmalloc(sizeof(struct device));
117   bzero(d, sizeof(*d));
118   d->dev = p;
119   d->config_cached = d->config_bufsize = 64;
120   d->config = xmalloc(64);
121   d->present = xmalloc(64);
122   memset(d->present, 1, 64);
123   if (!pci_read_block(p, 0, d->config, 64))
124     die("Unable to read the standard configuration space header");
125   if ((d->config[PCI_HEADER_TYPE] & 0x7f) == PCI_HEADER_TYPE_CARDBUS)
126     {
127       /* For cardbus bridges, we need to fetch 64 bytes more to get the
128        * full standard header... */
129       if (config_fetch(d, 64, 64))
130         d->config_cached += 64;
131     }
132   pci_setup_cache(p, d->config, d->config_cached);
133   pci_fill_info(p, PCI_FILL_IDENT | PCI_FILL_CLASS | PCI_FILL_IRQ | PCI_FILL_BASES | PCI_FILL_ROM_BASE | PCI_FILL_SIZES);
134   return d;
135 }
136
137 static void
138 scan_devices(void)
139 {
140   struct device *d;
141   struct pci_dev *p;
142
143   pci_scan_bus(pacc);
144   for(p=pacc->devices; p; p=p->next)
145     if (d = scan_device(p))
146       {
147         d->next = first_dev;
148         first_dev = d;
149       }
150 }
151
152 /* Config space accesses */
153
154 static void
155 check_conf_range(struct device *d, unsigned int pos, unsigned int len)
156 {
157   while (len)
158     if (!d->present[pos])
159       die("Internal bug: Accessing non-read configuration byte at position %x", pos);
160     else
161       pos++, len--;
162 }
163
164 static inline byte
165 get_conf_byte(struct device *d, unsigned int pos)
166 {
167   check_conf_range(d, pos, 1);
168   return d->config[pos];
169 }
170
171 static word
172 get_conf_word(struct device *d, unsigned int pos)
173 {
174   check_conf_range(d, pos, 2);
175   return d->config[pos] | (d->config[pos+1] << 8);
176 }
177
178 static u32
179 get_conf_long(struct device *d, unsigned int pos)
180 {
181   check_conf_range(d, pos, 4);
182   return d->config[pos] |
183     (d->config[pos+1] << 8) |
184     (d->config[pos+2] << 16) |
185     (d->config[pos+3] << 24);
186 }
187
188 /* Sorting */
189
190 static int
191 compare_them(const void *A, const void *B)
192 {
193   const struct pci_dev *a = (*(const struct device **)A)->dev;
194   const struct pci_dev *b = (*(const struct device **)B)->dev;
195
196   if (a->domain < b->domain)
197     return -1;
198   if (a->domain > b->domain)
199     return 1;
200   if (a->bus < b->bus)
201     return -1;
202   if (a->bus > b->bus)
203     return 1;
204   if (a->dev < b->dev)
205     return -1;
206   if (a->dev > b->dev)
207     return 1;
208   if (a->func < b->func)
209     return -1;
210   if (a->func > b->func)
211     return 1;
212   return 0;
213 }
214
215 static void
216 sort_them(void)
217 {
218   struct device **index, **h, **last_dev;
219   int cnt;
220   struct device *d;
221
222   cnt = 0;
223   for(d=first_dev; d; d=d->next)
224     cnt++;
225   h = index = alloca(sizeof(struct device *) * cnt);
226   for(d=first_dev; d; d=d->next)
227     *h++ = d;
228   qsort(index, cnt, sizeof(struct device *), compare_them);
229   last_dev = &first_dev;
230   h = index;
231   while (cnt--)
232     {
233       *last_dev = *h;
234       last_dev = &(*h)->next;
235       h++;
236     }
237   *last_dev = NULL;
238 }
239
240 /* Normal output */
241
242 #define FLAG(x,y) ((x & y) ? '+' : '-')
243
244 static void
245 show_slot_name(struct device *d)
246 {
247   struct pci_dev *p = d->dev;
248
249   if (!machine_readable ? show_domains : (p->domain || show_domains >= 2))
250     printf("%04x:", p->domain);
251   printf("%02x:%02x.%d", p->bus, p->dev, p->func);
252 }
253
254 static void
255 show_terse(struct device *d)
256 {
257   int c;
258   struct pci_dev *p = d->dev;
259   byte classbuf[128], devbuf[128];
260
261   show_slot_name(d);
262   printf(" %s: %s",
263          pci_lookup_name(pacc, classbuf, sizeof(classbuf),
264                          PCI_LOOKUP_CLASS,
265                          p->device_class),
266          pci_lookup_name(pacc, devbuf, sizeof(devbuf),
267                          PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
268                          p->vendor_id, p->device_id));
269   if (c = get_conf_byte(d, PCI_REVISION_ID))
270     printf(" (rev %02x)", c);
271   if (verbose)
272     {
273       char *x;
274       c = get_conf_byte(d, PCI_CLASS_PROG);
275       x = pci_lookup_name(pacc, devbuf, sizeof(devbuf),
276                           PCI_LOOKUP_PROGIF | PCI_LOOKUP_NO_NUMBERS,
277                           p->device_class, c);
278       if (c || x)
279         {
280           printf(" (prog-if %02x", c);
281           if (x)
282             printf(" [%s]", x);
283           putchar(')');
284         }
285     }
286   putchar('\n');
287 }
288
289 static void
290 show_size(pciaddr_t x)
291 {
292   if (!x)
293     return;
294   printf(" [size=");
295   if (x < 1024)
296     printf("%d", (int) x);
297   else if (x < 1048576)
298     printf("%dK", (int)(x / 1024));
299   else if (x < 0x80000000)
300     printf("%dM", (int)(x / 1048576));
301   else
302     printf(PCIADDR_T_FMT, x);
303   putchar(']');
304 }
305
306 static void
307 show_bases(struct device *d, int cnt)
308 {
309   struct pci_dev *p = d->dev;
310   word cmd = get_conf_word(d, PCI_COMMAND);
311   int i;
312
313   for(i=0; i<cnt; i++)
314     {
315       pciaddr_t pos = p->base_addr[i];
316       pciaddr_t len = (p->known_fields & PCI_FILL_SIZES) ? p->size[i] : 0;
317       u32 flg = get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i);
318       if (flg == 0xffffffff)
319         flg = 0;
320       if (!pos && !flg && !len)
321         continue;
322       if (verbose > 1)
323         printf("\tRegion %d: ", i);
324       else
325         putchar('\t');
326       if (pos && !flg)                  /* Reported by the OS, but not by the device */
327         {
328           printf("[virtual] ");
329           flg = pos;
330         }
331       if (flg & PCI_BASE_ADDRESS_SPACE_IO)
332         {
333           pciaddr_t a = pos & PCI_BASE_ADDRESS_IO_MASK;
334           printf("I/O ports at ");
335           if (a)
336             printf(PCIADDR_PORT_FMT, a);
337           else if (flg & PCI_BASE_ADDRESS_IO_MASK)
338             printf("<ignored>");
339           else
340             printf("<unassigned>");
341           if (!(cmd & PCI_COMMAND_IO))
342             printf(" [disabled]");
343         }
344       else
345         {
346           int t = flg & PCI_BASE_ADDRESS_MEM_TYPE_MASK;
347           pciaddr_t a = pos & PCI_ADDR_MEM_MASK;
348           int done = 0;
349           u32 z = 0;
350
351           printf("Memory at ");
352           if (t == PCI_BASE_ADDRESS_MEM_TYPE_64)
353             {
354               if (i >= cnt - 1)
355                 {
356                   printf("<invalid-64bit-slot>");
357                   done = 1;
358                 }
359               else
360                 {
361                   i++;
362                   z = get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i);
363                   if (buscentric_view)
364                     {
365                       u32 y = a & 0xffffffff;
366                       if (a || z)
367                         printf("%08x%08x", z, y);
368                       else
369                         printf("<unassigned>");
370                       done = 1;
371                     }
372                 }
373             }
374           if (!done)
375             {
376               if (a)
377                 printf(PCIADDR_T_FMT, a);
378               else
379                 printf(((flg & PCI_BASE_ADDRESS_MEM_MASK) || z) ? "<ignored>" : "<unassigned>");
380             }
381           printf(" (%s, %sprefetchable)",
382                  (t == PCI_BASE_ADDRESS_MEM_TYPE_32) ? "32-bit" :
383                  (t == PCI_BASE_ADDRESS_MEM_TYPE_64) ? "64-bit" :
384                  (t == PCI_BASE_ADDRESS_MEM_TYPE_1M) ? "low-1M" : "type 3",
385                  (flg & PCI_BASE_ADDRESS_MEM_PREFETCH) ? "" : "non-");
386           if (!(cmd & PCI_COMMAND_MEMORY))
387             printf(" [disabled]");
388         }
389       show_size(len);
390       putchar('\n');
391     }
392 }
393
394 static void
395 show_pm(struct device *d, int where, int cap)
396 {
397   int t, b;
398   static int pm_aux_current[8] = { 0, 55, 100, 160, 220, 270, 320, 375 };
399
400   printf("Power Management version %d\n", cap & PCI_PM_CAP_VER_MASK);
401   if (verbose < 2)
402     return;
403   printf("\t\tFlags: PMEClk%c DSI%c D1%c D2%c AuxCurrent=%dmA PME(D0%c,D1%c,D2%c,D3hot%c,D3cold%c)\n",
404          FLAG(cap, PCI_PM_CAP_PME_CLOCK),
405          FLAG(cap, PCI_PM_CAP_DSI),
406          FLAG(cap, PCI_PM_CAP_D1),
407          FLAG(cap, PCI_PM_CAP_D2),
408          pm_aux_current[(cap >> 6) & 7],
409          FLAG(cap, PCI_PM_CAP_PME_D0),
410          FLAG(cap, PCI_PM_CAP_PME_D1),
411          FLAG(cap, PCI_PM_CAP_PME_D2),
412          FLAG(cap, PCI_PM_CAP_PME_D3_HOT),
413          FLAG(cap, PCI_PM_CAP_PME_D3_COLD));
414   if (!config_fetch(d, where + PCI_PM_CTRL, PCI_PM_SIZEOF - PCI_PM_CTRL))
415     return;
416   t = get_conf_word(d, where + PCI_PM_CTRL);
417   printf("\t\tStatus: D%d PME-Enable%c DSel=%d DScale=%d PME%c\n",
418          t & PCI_PM_CTRL_STATE_MASK,
419          FLAG(t, PCI_PM_CTRL_PME_ENABLE),
420          (t & PCI_PM_CTRL_DATA_SEL_MASK) >> 9,
421          (t & PCI_PM_CTRL_DATA_SCALE_MASK) >> 13,
422          FLAG(t, PCI_PM_CTRL_PME_STATUS));
423   b = get_conf_byte(d, where + PCI_PM_PPB_EXTENSIONS);
424   if (b)
425     printf("\t\tBridge: PM%c B3%c\n",
426            FLAG(t, PCI_PM_BPCC_ENABLE),
427            FLAG(~t, PCI_PM_PPB_B2_B3));
428 }
429
430 static void
431 format_agp_rate(int rate, char *buf, int agp3)
432 {
433   char *c = buf;
434   int i;
435
436   for(i=0; i<=2; i++)
437     if (rate & (1 << i))
438       {
439         if (c != buf)
440           *c++ = ',';
441         c += sprintf(c, "x%d", 1 << (i + 2*agp3));
442       }
443   if (c != buf)
444     *c = 0;
445   else
446     strcpy(buf, "<none>");
447 }
448
449 static void
450 show_agp(struct device *d, int where, int cap)
451 {
452   u32 t;
453   char rate[16];
454   int ver, rev;
455   int agp3 = 0;
456
457   ver = (cap >> 4) & 0x0f;
458   rev = cap & 0x0f;
459   printf("AGP version %x.%x\n", ver, rev);
460   if (verbose < 2)
461     return;
462   if (!config_fetch(d, where + PCI_AGP_STATUS, PCI_AGP_SIZEOF - PCI_AGP_STATUS))
463     return;
464   t = get_conf_long(d, where + PCI_AGP_STATUS);
465   if (ver >= 3 && (t & PCI_AGP_STATUS_AGP3))
466     agp3 = 1;
467   format_agp_rate(t & 7, rate, agp3);
468   printf("\t\tStatus: RQ=%d Iso%c ArqSz=%d Cal=%d SBA%c ITACoh%c GART64%c HTrans%c 64bit%c FW%c AGP3%c Rate=%s\n",
469          ((t & PCI_AGP_STATUS_RQ_MASK) >> 24U) + 1,
470          FLAG(t, PCI_AGP_STATUS_ISOCH),
471          ((t & PCI_AGP_STATUS_ARQSZ_MASK) >> 13),
472          ((t & PCI_AGP_STATUS_CAL_MASK) >> 10),
473          FLAG(t, PCI_AGP_STATUS_SBA),
474          FLAG(t, PCI_AGP_STATUS_ITA_COH),
475          FLAG(t, PCI_AGP_STATUS_GART64),
476          FLAG(t, PCI_AGP_STATUS_HTRANS),
477          FLAG(t, PCI_AGP_STATUS_64BIT),
478          FLAG(t, PCI_AGP_STATUS_FW),
479          FLAG(t, PCI_AGP_STATUS_AGP3),
480          rate);
481   t = get_conf_long(d, where + PCI_AGP_COMMAND);
482   format_agp_rate(t & 7, rate, agp3);
483   printf("\t\tCommand: RQ=%d ArqSz=%d Cal=%d SBA%c AGP%c GART64%c 64bit%c FW%c Rate=%s\n",
484          ((t & PCI_AGP_COMMAND_RQ_MASK) >> 24U) + 1,
485          ((t & PCI_AGP_COMMAND_ARQSZ_MASK) >> 13),
486          ((t & PCI_AGP_COMMAND_CAL_MASK) >> 10),
487          FLAG(t, PCI_AGP_COMMAND_SBA),
488          FLAG(t, PCI_AGP_COMMAND_AGP),
489          FLAG(t, PCI_AGP_COMMAND_GART64),
490          FLAG(t, PCI_AGP_COMMAND_64BIT),
491          FLAG(t, PCI_AGP_COMMAND_FW),
492          rate);
493 }
494
495 static void
496 show_pcix_nobridge(struct device *d, int where)
497 {
498   u16 command;
499   u32 status;
500   static const byte max_outstanding[8] = { 1, 2, 3, 4, 8, 12, 16, 32 };
501
502   printf("PCI-X non-bridge device\n");
503       
504   if (verbose < 2)
505     return;
506
507   if (!config_fetch(d, where + PCI_PCIX_STATUS, 4))
508     return;
509   
510   command = get_conf_word(d, where + PCI_PCIX_COMMAND);
511   status = get_conf_long(d, where + PCI_PCIX_STATUS);
512   printf("\t\tCommand: DPERE%c ERO%c RBC=%d OST=%d\n",
513          FLAG(command, PCI_PCIX_COMMAND_DPERE),
514          FLAG(command, PCI_PCIX_COMMAND_ERO),
515          1 << (9 + ((command & PCI_PCIX_COMMAND_MAX_MEM_READ_BYTE_COUNT) >> 2U)),
516          max_outstanding[(command & PCI_PCIX_COMMAND_MAX_OUTSTANDING_SPLIT_TRANS) >> 4U]);
517   printf("\t\tStatus: Dev=%02x:%02x.%d 64bit%c 133MHz%c SCD%c USC%c DC=%s DMMRBC=%u DMOST=%u DMCRS=%u RSCEM%c 266MHz%c 533MHz%c\n",
518          ((status >> 8) & 0xff),
519          ((status >> 3) & 0x1f),
520          (status & PCI_PCIX_STATUS_FUNCTION),
521          FLAG(status, PCI_PCIX_STATUS_64BIT),
522          FLAG(status, PCI_PCIX_STATUS_133MHZ),
523          FLAG(status, PCI_PCIX_STATUS_SC_DISCARDED),
524          FLAG(status, PCI_PCIX_STATUS_UNEXPECTED_SC),
525          ((status & PCI_PCIX_STATUS_DEVICE_COMPLEXITY) ? "bridge" : "simple"),
526          1 << (9 + ((status >> 21) & 3U)),
527          max_outstanding[(status >> 23) & 7U],
528          1 << (3 + ((status >> 26) & 7U)),
529          FLAG(status, PCI_PCIX_STATUS_RCVD_SC_ERR_MESS),
530          FLAG(status, PCI_PCIX_STATUS_266MHZ),
531          FLAG(status, PCI_PCIX_STATUS_533MHZ));
532 }
533
534 static void
535 show_pcix_bridge(struct device *d, int where)
536 {
537   static const byte * const sec_clock_freq[8] = { "conv", "66MHz", "100MHz", "133MHz", "?4", "?5", "?6", "?7" };
538   u16 secstatus;
539   u32 status, upstcr, downstcr;
540   
541   printf("PCI-X bridge device\n");
542   
543   if (verbose < 2)
544     return;
545   
546   if (!config_fetch(d, where + PCI_PCIX_BRIDGE_STATUS, 12))
547     return;
548   
549   secstatus = get_conf_word(d, where + PCI_PCIX_BRIDGE_SEC_STATUS);
550   printf("\t\tSecondary Status: 64bit%c 133MHz%c SCD%c USC%c SCO%c SRD%c Freq=%s\n",
551          FLAG(secstatus, PCI_PCIX_BRIDGE_SEC_STATUS_64BIT),
552          FLAG(secstatus, PCI_PCIX_BRIDGE_SEC_STATUS_133MHZ),
553          FLAG(secstatus, PCI_PCIX_BRIDGE_SEC_STATUS_SC_DISCARDED),
554          FLAG(secstatus, PCI_PCIX_BRIDGE_SEC_STATUS_UNEXPECTED_SC),
555          FLAG(secstatus, PCI_PCIX_BRIDGE_SEC_STATUS_SC_OVERRUN),
556          FLAG(secstatus, PCI_PCIX_BRIDGE_SEC_STATUS_SPLIT_REQUEST_DELAYED),
557          sec_clock_freq[(secstatus >> 6) & 7]);
558   status = get_conf_long(d, where + PCI_PCIX_BRIDGE_STATUS);
559   printf("\t\tStatus: Dev=%02x:%02x.%d 64bit%c 133MHz%c SCD%c USC%c SCO%c SRD%c\n", 
560          ((status >> 8) & 0xff),
561          ((status >> 3) & 0x1f),
562          (status & PCI_PCIX_BRIDGE_STATUS_FUNCTION),
563          FLAG(status, PCI_PCIX_BRIDGE_STATUS_64BIT),
564          FLAG(status, PCI_PCIX_BRIDGE_STATUS_133MHZ),
565          FLAG(status, PCI_PCIX_BRIDGE_STATUS_SC_DISCARDED),
566          FLAG(status, PCI_PCIX_BRIDGE_STATUS_UNEXPECTED_SC),
567          FLAG(status, PCI_PCIX_BRIDGE_STATUS_SC_OVERRUN),
568          FLAG(status, PCI_PCIX_BRIDGE_STATUS_SPLIT_REQUEST_DELAYED));
569   upstcr = get_conf_long(d, where + PCI_PCIX_BRIDGE_UPSTREAM_SPLIT_TRANS_CTRL);
570   printf("\t\tUpstream: Capacity=%u CommitmentLimit=%u\n",
571          (upstcr & PCI_PCIX_BRIDGE_STR_CAPACITY),
572          (upstcr >> 16) & 0xffff);
573   downstcr = get_conf_long(d, where + PCI_PCIX_BRIDGE_DOWNSTREAM_SPLIT_TRANS_CTRL);
574   printf("\t\tDownstream: Capacity=%u CommitmentLimit=%u\n",
575          (downstcr & PCI_PCIX_BRIDGE_STR_CAPACITY),
576          (downstcr >> 16) & 0xffff);
577 }
578
579 static void
580 show_pcix(struct device *d, int where)
581 {
582   switch (get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f)
583     {
584     case PCI_HEADER_TYPE_NORMAL:
585       show_pcix_nobridge(d, where);
586       break;
587     case PCI_HEADER_TYPE_BRIDGE:
588       show_pcix_bridge(d, where);
589       break;
590     }
591 }
592
593 static inline char *
594 ht_link_width(unsigned width)
595 {
596   static char * const widths[8] = { "8bit", "16bit", "[2]", "32bit", "2bit", "4bit", "[6]", "N/C" };
597   return widths[width];
598 }
599
600 static inline char *
601 ht_link_freq(unsigned freq)
602 {
603   static char * const freqs[16] = { "200MHz", "300MHz", "400MHz", "500MHz", "600MHz", "800MHz", "1.0GHz", "1.2GHz",
604                                     "1.4GHz", "1.6GHz", "[a]", "[b]", "[c]", "[d]", "[e]", "Vend" };
605   return freqs[freq];
606 }
607
608 static void
609 show_ht_pri(struct device *d, int where, int cmd)
610 {
611   u16 lctr0, lcnf0, lctr1, lcnf1, eh;
612   u8 rid, lfrer0, lfcap0, ftr, lfrer1, lfcap1, mbu, mlu, bn;
613   char *fmt;
614
615   printf("HyperTransport: Slave or Primary Interface\n");
616   if (verbose < 2)
617     return;
618
619   if (!config_fetch(d, where + PCI_HT_PRI_LCTR0, PCI_HT_PRI_SIZEOF - PCI_HT_PRI_LCTR0))
620     return;
621   rid = get_conf_byte(d, where + PCI_HT_PRI_RID);
622   if (rid < 0x23 && rid > 0x11)
623     printf("\t\t!!! Possibly incomplete decoding\n");
624
625   if (rid >= 0x23)
626     fmt = "\t\tCommand: BaseUnitID=%u UnitCnt=%u MastHost%c DefDir%c DUL%c\n";
627   else
628     fmt = "\t\tCommand: BaseUnitID=%u UnitCnt=%u MastHost%c DefDir%c\n";
629   printf(fmt,
630          (cmd & PCI_HT_PRI_CMD_BUID),
631          (cmd & PCI_HT_PRI_CMD_UC) >> 5,
632          FLAG(cmd, PCI_HT_PRI_CMD_MH),
633          FLAG(cmd, PCI_HT_PRI_CMD_DD),
634          FLAG(cmd, PCI_HT_PRI_CMD_DUL));
635   lctr0 = get_conf_word(d, where + PCI_HT_PRI_LCTR0);
636   if (rid >= 0x23)
637     fmt = "\t\tLink Control 0: CFlE%c CST%c CFE%c <LkFail%c Init%c EOC%c TXO%c <CRCErr=%x IsocEn%c LSEn%c ExtCTL%c 64b%c\n";
638   else
639     fmt = "\t\tLink Control 0: CFlE%c CST%c CFE%c <LkFail%c Init%c EOC%c TXO%c <CRCErr=%x\n";
640   printf(fmt,
641          FLAG(lctr0, PCI_HT_LCTR_CFLE),
642          FLAG(lctr0, PCI_HT_LCTR_CST),
643          FLAG(lctr0, PCI_HT_LCTR_CFE),
644          FLAG(lctr0, PCI_HT_LCTR_LKFAIL),
645          FLAG(lctr0, PCI_HT_LCTR_INIT),
646          FLAG(lctr0, PCI_HT_LCTR_EOC),
647          FLAG(lctr0, PCI_HT_LCTR_TXO),
648          (lctr0 & PCI_HT_LCTR_CRCERR) >> 8,
649          FLAG(lctr0, PCI_HT_LCTR_ISOCEN),
650          FLAG(lctr0, PCI_HT_LCTR_LSEN),
651          FLAG(lctr0, PCI_HT_LCTR_EXTCTL),
652          FLAG(lctr0, PCI_HT_LCTR_64B));
653   lcnf0 = get_conf_word(d, where + PCI_HT_PRI_LCNF0);
654   if (rid >= 0x23)
655     fmt = "\t\tLink Config 0: MLWI=%1$s DwFcIn%5$c MLWO=%2$s DwFcOut%6$c LWI=%3$s DwFcInEn%7$c LWO=%4$s DwFcOutEn%8$c\n";
656   else
657     fmt = "\t\tLink Config 0: MLWI=%s MLWO=%s LWI=%s LWO=%s\n";
658   printf(fmt,
659          ht_link_width(lcnf0 & PCI_HT_LCNF_MLWI),
660          ht_link_width((lcnf0 & PCI_HT_LCNF_MLWO) >> 4),
661          ht_link_width((lcnf0 & PCI_HT_LCNF_LWI) >> 8),
662          ht_link_width((lcnf0 & PCI_HT_LCNF_LWO) >> 12),
663          FLAG(lcnf0, PCI_HT_LCNF_DFI),
664          FLAG(lcnf0, PCI_HT_LCNF_DFO),
665          FLAG(lcnf0, PCI_HT_LCNF_DFIE),
666          FLAG(lcnf0, PCI_HT_LCNF_DFOE));
667   lctr1 = get_conf_word(d, where + PCI_HT_PRI_LCTR1);
668   if (rid >= 0x23)
669     fmt = "\t\tLink Control 1: CFlE%c CST%c CFE%c <LkFail%c Init%c EOC%c TXO%c <CRCErr=%x IsocEn%c LSEn%c ExtCTL%c 64b%c\n";
670   else
671     fmt = "\t\tLink Control 1: CFlE%c CST%c CFE%c <LkFail%c Init%c EOC%c TXO%c <CRCErr=%x\n";
672   printf(fmt,
673          FLAG(lctr1, PCI_HT_LCTR_CFLE),
674          FLAG(lctr1, PCI_HT_LCTR_CST),
675          FLAG(lctr1, PCI_HT_LCTR_CFE),
676          FLAG(lctr1, PCI_HT_LCTR_LKFAIL),
677          FLAG(lctr1, PCI_HT_LCTR_INIT),
678          FLAG(lctr1, PCI_HT_LCTR_EOC),
679          FLAG(lctr1, PCI_HT_LCTR_TXO),
680          (lctr1 & PCI_HT_LCTR_CRCERR) >> 8,
681          FLAG(lctr1, PCI_HT_LCTR_ISOCEN),
682          FLAG(lctr1, PCI_HT_LCTR_LSEN),
683          FLAG(lctr1, PCI_HT_LCTR_EXTCTL),
684          FLAG(lctr1, PCI_HT_LCTR_64B));
685   lcnf1 = get_conf_word(d, where + PCI_HT_PRI_LCNF1);
686   if (rid >= 0x23)
687     fmt = "\t\tLink Config 1: MLWI=%1$s DwFcIn%5$c MLWO=%2$s DwFcOut%6$c LWI=%3$s DwFcInEn%7$c LWO=%4$s DwFcOutEn%8$c\n";
688   else
689     fmt = "\t\tLink Config 1: MLWI=%s MLWO=%s LWI=%s LWO=%s\n";
690   printf(fmt,
691          ht_link_width(lcnf1 & PCI_HT_LCNF_MLWI),
692          ht_link_width((lcnf1 & PCI_HT_LCNF_MLWO) >> 4),
693          ht_link_width((lcnf1 & PCI_HT_LCNF_LWI) >> 8),
694          ht_link_width((lcnf1 & PCI_HT_LCNF_LWO) >> 12),
695          FLAG(lcnf1, PCI_HT_LCNF_DFI),
696          FLAG(lcnf1, PCI_HT_LCNF_DFO),
697          FLAG(lcnf1, PCI_HT_LCNF_DFIE),
698          FLAG(lcnf1, PCI_HT_LCNF_DFOE));
699   printf("\t\tRevision ID: %u.%02u\n",
700          (rid & PCI_HT_RID_MAJ) >> 5, (rid & PCI_HT_RID_MIN));
701   if (rid < 0x23)
702     return;
703   lfrer0 = get_conf_byte(d, where + PCI_HT_PRI_LFRER0);
704   printf("\t\tLink Frequency 0: %s\n", ht_link_freq(lfrer0 & PCI_HT_LFRER_FREQ));
705   printf("\t\tLink Error 0: <Prot%c <Ovfl%c <EOC%c CTLTm%c\n",
706          FLAG(lfrer0, PCI_HT_LFRER_PROT),
707          FLAG(lfrer0, PCI_HT_LFRER_OV),
708          FLAG(lfrer0, PCI_HT_LFRER_EOC),
709          FLAG(lfrer0, PCI_HT_LFRER_CTLT));
710   lfcap0 = get_conf_byte(d, where + PCI_HT_PRI_LFCAP0);
711   printf("\t\tLink Frequency Capability 0: 200MHz%c 300MHz%c 400MHz%c 500MHz%c 600MHz%c 800MHz%c 1.0GHz%c 1.2GHz%c 1.4GHz%c 1.6GHz%c Vend%c\n",
712          FLAG(lfcap0, PCI_HT_LFCAP_200),
713          FLAG(lfcap0, PCI_HT_LFCAP_300),
714          FLAG(lfcap0, PCI_HT_LFCAP_400),
715          FLAG(lfcap0, PCI_HT_LFCAP_500),
716          FLAG(lfcap0, PCI_HT_LFCAP_600),
717          FLAG(lfcap0, PCI_HT_LFCAP_800),
718          FLAG(lfcap0, PCI_HT_LFCAP_1000),
719          FLAG(lfcap0, PCI_HT_LFCAP_1200),
720          FLAG(lfcap0, PCI_HT_LFCAP_1400),
721          FLAG(lfcap0, PCI_HT_LFCAP_1600),
722          FLAG(lfcap0, PCI_HT_LFCAP_VEND));
723   ftr = get_conf_byte(d, where + PCI_HT_PRI_FTR);
724   printf("\t\tFeature Capability: IsocFC%c LDTSTOP%c CRCTM%c ECTLT%c 64bA%c UIDRD%c\n",
725          FLAG(ftr, PCI_HT_FTR_ISOCFC),
726          FLAG(ftr, PCI_HT_FTR_LDTSTOP),
727          FLAG(ftr, PCI_HT_FTR_CRCTM),
728          FLAG(ftr, PCI_HT_FTR_ECTLT),
729          FLAG(ftr, PCI_HT_FTR_64BA),
730          FLAG(ftr, PCI_HT_FTR_UIDRD));
731   lfrer1 = get_conf_byte(d, where + PCI_HT_PRI_LFRER1);
732   printf("\t\tLink Frequency 1: %s\n", ht_link_freq(lfrer1 & PCI_HT_LFRER_FREQ));
733   printf("\t\tLink Error 1: <Prot%c <Ovfl%c <EOC%c CTLTm%c\n",
734          FLAG(lfrer1, PCI_HT_LFRER_PROT),
735          FLAG(lfrer1, PCI_HT_LFRER_OV),
736          FLAG(lfrer1, PCI_HT_LFRER_EOC),
737          FLAG(lfrer1, PCI_HT_LFRER_CTLT));
738   lfcap1 = get_conf_byte(d, where + PCI_HT_PRI_LFCAP1);
739   printf("\t\tLink Frequency Capability 1: 200MHz%c 300MHz%c 400MHz%c 500MHz%c 600MHz%c 800MHz%c 1.0GHz%c 1.2GHz%c 1.4GHz%c 1.6GHz%c Vend%c\n",
740          FLAG(lfcap1, PCI_HT_LFCAP_200),
741          FLAG(lfcap1, PCI_HT_LFCAP_300),
742          FLAG(lfcap1, PCI_HT_LFCAP_400),
743          FLAG(lfcap1, PCI_HT_LFCAP_500),
744          FLAG(lfcap1, PCI_HT_LFCAP_600),
745          FLAG(lfcap1, PCI_HT_LFCAP_800),
746          FLAG(lfcap1, PCI_HT_LFCAP_1000),
747          FLAG(lfcap1, PCI_HT_LFCAP_1200),
748          FLAG(lfcap1, PCI_HT_LFCAP_1400),
749          FLAG(lfcap1, PCI_HT_LFCAP_1600),
750          FLAG(lfcap1, PCI_HT_LFCAP_VEND));
751   eh = get_conf_word(d, where + PCI_HT_PRI_EH);
752   printf("\t\tError Handling: PFlE%c OFlE%c PFE%c OFE%c EOCFE%c RFE%c CRCFE%c SERRFE%c CF%c RE%c PNFE%c ONFE%c EOCNFE%c RNFE%c CRCNFE%c SERRNFE%c\n",
753          FLAG(eh, PCI_HT_EH_PFLE),
754          FLAG(eh, PCI_HT_EH_OFLE),
755          FLAG(eh, PCI_HT_EH_PFE),
756          FLAG(eh, PCI_HT_EH_OFE),
757          FLAG(eh, PCI_HT_EH_EOCFE),
758          FLAG(eh, PCI_HT_EH_RFE),
759          FLAG(eh, PCI_HT_EH_CRCFE),
760          FLAG(eh, PCI_HT_EH_SERRFE),
761          FLAG(eh, PCI_HT_EH_CF),
762          FLAG(eh, PCI_HT_EH_RE),
763          FLAG(eh, PCI_HT_EH_PNFE),
764          FLAG(eh, PCI_HT_EH_ONFE),
765          FLAG(eh, PCI_HT_EH_EOCNFE),
766          FLAG(eh, PCI_HT_EH_RNFE),
767          FLAG(eh, PCI_HT_EH_CRCNFE),
768          FLAG(eh, PCI_HT_EH_SERRNFE));
769   mbu = get_conf_byte(d, where + PCI_HT_PRI_MBU);
770   mlu = get_conf_byte(d, where + PCI_HT_PRI_MLU);
771   printf("\t\tPrefetchable memory behind bridge Upper: %02x-%02x\n", mbu, mlu);
772   bn = get_conf_byte(d, where + PCI_HT_PRI_BN);
773   printf("\t\tBus Number: %02x\n", bn);
774 }
775
776 static void
777 show_ht_sec(struct device *d, int where, int cmd)
778 {
779   u16 lctr, lcnf, ftr, eh;
780   u8 rid, lfrer, lfcap, mbu, mlu;
781   char *fmt;
782
783   printf("HyperTransport: Host or Secondary Interface\n");
784   if (verbose < 2)
785     return;
786
787   if (!config_fetch(d, where + PCI_HT_SEC_LCTR, PCI_HT_SEC_SIZEOF - PCI_HT_SEC_LCTR))
788     return;
789   rid = get_conf_byte(d, where + PCI_HT_SEC_RID);
790   if (rid < 0x23 && rid > 0x11)
791     printf("\t\t!!! Possibly incomplete decoding\n");
792
793   if (rid >= 0x23)
794     fmt = "\t\tCommand: WarmRst%c DblEnd%c DevNum=%u ChainSide%c HostHide%c Slave%c <EOCErr%c DUL%c\n";
795   else
796     fmt = "\t\tCommand: WarmRst%c DblEnd%c\n";
797   printf(fmt,
798          FLAG(cmd, PCI_HT_SEC_CMD_WR),
799          FLAG(cmd, PCI_HT_SEC_CMD_DE),
800          (cmd & PCI_HT_SEC_CMD_DN) >> 2,
801          FLAG(cmd, PCI_HT_SEC_CMD_CS),
802          FLAG(cmd, PCI_HT_SEC_CMD_HH),
803          FLAG(cmd, PCI_HT_SEC_CMD_AS),
804          FLAG(cmd, PCI_HT_SEC_CMD_HIECE),
805          FLAG(cmd, PCI_HT_SEC_CMD_DUL));
806   lctr = get_conf_word(d, where + PCI_HT_SEC_LCTR);
807   if (rid >= 0x23)
808     fmt = "\t\tLink Control: CFlE%c CST%c CFE%c <LkFail%c Init%c EOC%c TXO%c <CRCErr=%x IsocEn%c LSEn%c ExtCTL%c 64b%c\n";
809   else
810     fmt = "\t\tLink Control: CFlE%c CST%c CFE%c <LkFail%c Init%c EOC%c TXO%c <CRCErr=%x\n";
811   printf(fmt,
812          FLAG(lctr, PCI_HT_LCTR_CFLE),
813          FLAG(lctr, PCI_HT_LCTR_CST),
814          FLAG(lctr, PCI_HT_LCTR_CFE),
815          FLAG(lctr, PCI_HT_LCTR_LKFAIL),
816          FLAG(lctr, PCI_HT_LCTR_INIT),
817          FLAG(lctr, PCI_HT_LCTR_EOC),
818          FLAG(lctr, PCI_HT_LCTR_TXO),
819          (lctr & PCI_HT_LCTR_CRCERR) >> 8,
820          FLAG(lctr, PCI_HT_LCTR_ISOCEN),
821          FLAG(lctr, PCI_HT_LCTR_LSEN),
822          FLAG(lctr, PCI_HT_LCTR_EXTCTL),
823          FLAG(lctr, PCI_HT_LCTR_64B));
824   lcnf = get_conf_word(d, where + PCI_HT_SEC_LCNF);
825   if (rid >= 0x23)
826     fmt = "\t\tLink Config: MLWI=%1$s DwFcIn%5$c MLWO=%2$s DwFcOut%6$c LWI=%3$s DwFcInEn%7$c LWO=%4$s DwFcOutEn%8$c\n";
827   else
828     fmt = "\t\tLink Config: MLWI=%s MLWO=%s LWI=%s LWO=%s\n";
829   printf(fmt,
830          ht_link_width(lcnf & PCI_HT_LCNF_MLWI),
831          ht_link_width((lcnf & PCI_HT_LCNF_MLWO) >> 4),
832          ht_link_width((lcnf & PCI_HT_LCNF_LWI) >> 8),
833          ht_link_width((lcnf & PCI_HT_LCNF_LWO) >> 12),
834          FLAG(lcnf, PCI_HT_LCNF_DFI),
835          FLAG(lcnf, PCI_HT_LCNF_DFO),
836          FLAG(lcnf, PCI_HT_LCNF_DFIE),
837          FLAG(lcnf, PCI_HT_LCNF_DFOE));
838   printf("\t\tRevision ID: %u.%02u\n",
839          (rid & PCI_HT_RID_MAJ) >> 5, (rid & PCI_HT_RID_MIN));
840   if (rid < 0x23)
841     return;
842   lfrer = get_conf_byte(d, where + PCI_HT_SEC_LFRER);
843   printf("\t\tLink Frequency: %s\n", ht_link_freq(lfrer & PCI_HT_LFRER_FREQ));
844   printf("\t\tLink Error: <Prot%c <Ovfl%c <EOC%c CTLTm%c\n",
845          FLAG(lfrer, PCI_HT_LFRER_PROT),
846          FLAG(lfrer, PCI_HT_LFRER_OV),
847          FLAG(lfrer, PCI_HT_LFRER_EOC),
848          FLAG(lfrer, PCI_HT_LFRER_CTLT));
849   lfcap = get_conf_byte(d, where + PCI_HT_SEC_LFCAP);
850   printf("\t\tLink Frequency Capability: 200MHz%c 300MHz%c 400MHz%c 500MHz%c 600MHz%c 800MHz%c 1.0GHz%c 1.2GHz%c 1.4GHz%c 1.6GHz%c Vend%c\n",
851          FLAG(lfcap, PCI_HT_LFCAP_200),
852          FLAG(lfcap, PCI_HT_LFCAP_300),
853          FLAG(lfcap, PCI_HT_LFCAP_400),
854          FLAG(lfcap, PCI_HT_LFCAP_500),
855          FLAG(lfcap, PCI_HT_LFCAP_600),
856          FLAG(lfcap, PCI_HT_LFCAP_800),
857          FLAG(lfcap, PCI_HT_LFCAP_1000),
858          FLAG(lfcap, PCI_HT_LFCAP_1200),
859          FLAG(lfcap, PCI_HT_LFCAP_1400),
860          FLAG(lfcap, PCI_HT_LFCAP_1600),
861          FLAG(lfcap, PCI_HT_LFCAP_VEND));
862   ftr = get_conf_word(d, where + PCI_HT_SEC_FTR);
863   printf("\t\tFeature Capability: IsocFC%c LDTSTOP%c CRCTM%c ECTLT%c 64bA%c UIDRD%c ExtRS%c UCnfE%c\n",
864          FLAG(ftr, PCI_HT_FTR_ISOCFC),
865          FLAG(ftr, PCI_HT_FTR_LDTSTOP),
866          FLAG(ftr, PCI_HT_FTR_CRCTM),
867          FLAG(ftr, PCI_HT_FTR_ECTLT),
868          FLAG(ftr, PCI_HT_FTR_64BA),
869          FLAG(ftr, PCI_HT_FTR_UIDRD),
870          FLAG(ftr, PCI_HT_SEC_FTR_EXTRS),
871          FLAG(ftr, PCI_HT_SEC_FTR_UCNFE));
872   if (ftr & PCI_HT_SEC_FTR_EXTRS)
873     {
874       eh = get_conf_word(d, where + PCI_HT_SEC_EH);
875       printf("\t\tError Handling: PFlE%c OFlE%c PFE%c OFE%c EOCFE%c RFE%c CRCFE%c SERRFE%c CF%c RE%c PNFE%c ONFE%c EOCNFE%c RNFE%c CRCNFE%c SERRNFE%c\n",
876              FLAG(eh, PCI_HT_EH_PFLE),
877              FLAG(eh, PCI_HT_EH_OFLE),
878              FLAG(eh, PCI_HT_EH_PFE),
879              FLAG(eh, PCI_HT_EH_OFE),
880              FLAG(eh, PCI_HT_EH_EOCFE),
881              FLAG(eh, PCI_HT_EH_RFE),
882              FLAG(eh, PCI_HT_EH_CRCFE),
883              FLAG(eh, PCI_HT_EH_SERRFE),
884              FLAG(eh, PCI_HT_EH_CF),
885              FLAG(eh, PCI_HT_EH_RE),
886              FLAG(eh, PCI_HT_EH_PNFE),
887              FLAG(eh, PCI_HT_EH_ONFE),
888              FLAG(eh, PCI_HT_EH_EOCNFE),
889              FLAG(eh, PCI_HT_EH_RNFE),
890              FLAG(eh, PCI_HT_EH_CRCNFE),
891              FLAG(eh, PCI_HT_EH_SERRNFE));
892       mbu = get_conf_byte(d, where + PCI_HT_SEC_MBU);
893       mlu = get_conf_byte(d, where + PCI_HT_SEC_MLU);
894       printf("\t\tPrefetchable memory behind bridge Upper: %02x-%02x\n", mbu, mlu);
895     }
896 }
897
898 static void
899 show_ht(struct device *d, int where, int cmd)
900 {
901   int type;
902
903   switch (cmd & PCI_HT_CMD_TYP_HI)
904     {
905     case PCI_HT_CMD_TYP_HI_PRI:
906       show_ht_pri(d, where, cmd);
907       return;
908     case PCI_HT_CMD_TYP_HI_SEC:
909       show_ht_sec(d, where, cmd);
910       return;
911     }
912
913   type = cmd & PCI_HT_CMD_TYP;
914   switch (type)
915     {
916     case PCI_HT_CMD_TYP_SW:
917       printf("HyperTransport: Switch\n");
918       break;
919     case PCI_HT_CMD_TYP_IDC:
920       printf("HyperTransport: Interrupt Discovery and Configuration\n");
921       break;
922     case PCI_HT_CMD_TYP_RID:
923       printf("HyperTransport: Revision ID: %u.%02u\n",
924              (cmd & PCI_HT_RID_MAJ) >> 5, (cmd & PCI_HT_RID_MIN));
925       break;
926     case PCI_HT_CMD_TYP_UIDC:
927       printf("HyperTransport: UnitID Clumping\n");
928       break;
929     case PCI_HT_CMD_TYP_ECSA:
930       printf("HyperTransport: Extended Configuration Space Access\n");
931       break;
932     case PCI_HT_CMD_TYP_AM:
933       printf("HyperTransport: Address Mapping\n");
934       break;
935     case PCI_HT_CMD_TYP_MSIM:
936       printf("HyperTransport: MSI Mapping\n");
937       break;
938     case PCI_HT_CMD_TYP_DR:
939       printf("HyperTransport: DirectRoute\n");
940       break;
941     case PCI_HT_CMD_TYP_VCS:
942       printf("HyperTransport: VCSet\n");
943       break;
944     case PCI_HT_CMD_TYP_RM:
945       printf("HyperTransport: Retry Mode\n");
946       break;
947     case PCI_HT_CMD_TYP_X86:
948       printf("HyperTransport: X86 (reserved)\n");
949       break;
950     default:
951       printf("HyperTransport: #%02x\n", type >> 11);
952     }
953 }
954
955 static void
956 show_rom(struct device *d, int reg)
957 {
958   struct pci_dev *p = d->dev;
959   pciaddr_t rom = p->rom_base_addr;
960   pciaddr_t len = (p->known_fields & PCI_FILL_SIZES) ? p->rom_size : 0;
961   u32 flg = get_conf_long(d, reg);
962   word cmd = get_conf_word(d, PCI_COMMAND);
963
964   if (!rom && !flg && !len)
965     return;
966   putchar('\t');
967   if ((rom & PCI_ROM_ADDRESS_MASK) && !(flg & PCI_ROM_ADDRESS_MASK))
968     {
969       printf("[virtual] ");
970       flg = rom;
971     }
972   printf("Expansion ROM at ");
973   if (rom & PCI_ROM_ADDRESS_MASK)
974     printf(PCIADDR_T_FMT, rom & PCI_ROM_ADDRESS_MASK);
975   else if (flg & PCI_ROM_ADDRESS_MASK)
976     printf("<ignored>");
977   else
978     printf("<unassigned>");
979   if (!(flg & PCI_ROM_ADDRESS_ENABLE))
980     printf(" [disabled]");
981   else if (!(cmd & PCI_COMMAND_MEMORY))
982     printf(" [disabled by cmd]");
983   show_size(len);
984   putchar('\n');
985 }
986
987 static void
988 show_msi(struct device *d, int where, int cap)
989 {
990   int is64;
991   u32 t;
992   u16 w;
993
994   printf("Message Signalled Interrupts: 64bit%c Queue=%d/%d Enable%c\n",
995          FLAG(cap, PCI_MSI_FLAGS_64BIT),
996          (cap & PCI_MSI_FLAGS_QSIZE) >> 4,
997          (cap & PCI_MSI_FLAGS_QMASK) >> 1,
998          FLAG(cap, PCI_MSI_FLAGS_ENABLE));
999   if (verbose < 2)
1000     return;
1001   is64 = cap & PCI_MSI_FLAGS_64BIT;
1002   if (!config_fetch(d, where + PCI_MSI_ADDRESS_LO, (is64 ? PCI_MSI_DATA_64 : PCI_MSI_DATA_32) + 2 - PCI_MSI_ADDRESS_LO))
1003     return;
1004   printf("\t\tAddress: ");
1005   if (is64)
1006     {
1007       t = get_conf_long(d, where + PCI_MSI_ADDRESS_HI);
1008       w = get_conf_word(d, where + PCI_MSI_DATA_64);
1009       printf("%08x", t);
1010     }
1011   else
1012     w = get_conf_word(d, where + PCI_MSI_DATA_32);
1013   t = get_conf_long(d, where + PCI_MSI_ADDRESS_LO);
1014   printf("%08x  Data: %04x\n", t, w);
1015 }
1016
1017 static void show_vendor(void)
1018 {
1019   printf("Vendor Specific Information\n");
1020 }
1021
1022 static void show_debug(void)
1023 {
1024   printf("Debug port\n");
1025 }
1026
1027 static float power_limit(int value, int scale)
1028 {
1029   static const float scales[4] = { 1.0, 0.1, 0.01, 0.001 };
1030   return value * scales[scale];
1031 }
1032
1033 static const char *latency_l0s(int value)
1034 {
1035   static const char *latencies[] = { "<64ns", "<128ns", "<256ns", "<512ns", "<1us", "<2us", "<4us", "unlimited" };
1036   return latencies[value];
1037 }
1038
1039 static const char *latency_l1(int value)
1040 {
1041   static const char *latencies[] = { "<1us", "<2us", "<4us", "<8us", "<16us", "<32us", "<64us", "unlimited" };
1042   return latencies[value];
1043 }
1044
1045 static void show_express_dev(struct device *d, int where, int type)
1046 {
1047   u32 t;
1048   u16 w;
1049
1050   t = get_conf_long(d, where + PCI_EXP_DEVCAP);
1051   printf("\t\tDevice: Supported: MaxPayload %d bytes, PhantFunc %d, ExtTag%c\n",
1052         128 << (t & PCI_EXP_DEVCAP_PAYLOAD),
1053         (1 << ((t & PCI_EXP_DEVCAP_PHANTOM) >> 3)) - 1,
1054         FLAG(t, PCI_EXP_DEVCAP_EXT_TAG));
1055   printf("\t\tDevice: Latency L0s %s, L1 %s\n",
1056         latency_l0s((t & PCI_EXP_DEVCAP_L0S) >> 6),
1057         latency_l1((t & PCI_EXP_DEVCAP_L1) >> 9));
1058   if ((type == PCI_EXP_TYPE_ENDPOINT) || (type == PCI_EXP_TYPE_LEG_END) ||
1059       (type == PCI_EXP_TYPE_UPSTREAM) || (type == PCI_EXP_TYPE_PCI_BRIDGE))
1060     printf("\t\tDevice: AtnBtn%c AtnInd%c PwrInd%c\n",
1061         FLAG(t, PCI_EXP_DEVCAP_ATN_BUT),
1062         FLAG(t, PCI_EXP_DEVCAP_ATN_IND), FLAG(t, PCI_EXP_DEVCAP_PWR_IND));
1063   if (type == PCI_EXP_TYPE_UPSTREAM)
1064     printf("\t\tDevice: SlotPowerLimit %f\n",
1065         power_limit((t & PCI_EXP_DEVCAP_PWR_VAL) >> 18,
1066                     (t & PCI_EXP_DEVCAP_PWR_SCL) >> 26));
1067
1068   w = get_conf_word(d, where + PCI_EXP_DEVCTL);
1069   printf("\t\tDevice: Errors: Correctable%c Non-Fatal%c Fatal%c Unsupported%c\n",
1070         FLAG(w, PCI_EXP_DEVCTL_CERE), 
1071         FLAG(w, PCI_EXP_DEVCTL_NFERE), 
1072         FLAG(w, PCI_EXP_DEVCTL_FERE), 
1073         FLAG(w, PCI_EXP_DEVCTL_URRE));
1074   printf("\t\tDevice: RlxdOrd%c ExtTag%c PhantFunc%c AuxPwr%c NoSnoop%c\n",
1075         FLAG(w, PCI_EXP_DEVCTL_RELAXED),
1076         FLAG(w, PCI_EXP_DEVCTL_EXT_TAG),
1077         FLAG(w, PCI_EXP_DEVCTL_PHANTOM),
1078         FLAG(w, PCI_EXP_DEVCTL_AUX_PME),
1079         FLAG(w, PCI_EXP_DEVCTL_NOSNOOP));
1080   printf("\t\tDevice: MaxPayload %d bytes, MaxReadReq %d bytes\n",
1081         128 << ((w & PCI_EXP_DEVCTL_PAYLOAD) >> 5),
1082         128 << ((w & PCI_EXP_DEVCTL_READRQ) >> 12));
1083 }
1084
1085 static char *link_speed(int speed)
1086 {
1087   switch (speed)
1088     {
1089       case 1:
1090         return "2.5Gb/s";
1091       default:
1092         return "unknown";
1093     }
1094 }
1095
1096 static char *aspm_support(int code)
1097 {
1098   switch (code)
1099     {
1100       case 1:
1101         return "L0s";
1102       case 3:
1103         return "L0s L1";
1104       default:
1105         return "unknown";
1106     }
1107 }
1108
1109 static const char *aspm_enabled(int code)
1110 {
1111   static const char *desc[] = { "Disabled", "L0s Enabled", "L1 Enabled", "L0s L1 Enabled" };
1112   return desc[code];
1113 }
1114
1115 static void show_express_link(struct device *d, int where, int type)
1116 {
1117   u32 t;
1118   u16 w;
1119
1120   t = get_conf_long(d, where + PCI_EXP_LNKCAP);
1121   printf("\t\tLink: Supported Speed %s, Width x%d, ASPM %s, Port %d\n",
1122         link_speed(t & PCI_EXP_LNKCAP_SPEED), (t & PCI_EXP_LNKCAP_WIDTH) >> 4,
1123         aspm_support((t & PCI_EXP_LNKCAP_ASPM) >> 10),
1124         t >> 24);
1125   printf("\t\tLink: Latency L0s %s, L1 %s\n",
1126         latency_l0s((t & PCI_EXP_LNKCAP_L0S) >> 12),
1127         latency_l1((t & PCI_EXP_LNKCAP_L1) >> 15));
1128   w = get_conf_word(d, where + PCI_EXP_LNKCTL);
1129   printf("\t\tLink: ASPM %s", aspm_enabled(w & PCI_EXP_LNKCTL_ASPM));
1130   if ((type == PCI_EXP_TYPE_ROOT_PORT) || (type == PCI_EXP_TYPE_ENDPOINT) ||
1131       (type == PCI_EXP_TYPE_LEG_END))
1132     printf(" RCB %d bytes", w & PCI_EXP_LNKCTL_RCB ? 128 : 64);
1133   if (w & PCI_EXP_LNKCTL_DISABLE)
1134     printf(" Disabled");
1135   printf(" CommClk%c ExtSynch%c\n", FLAG(w, PCI_EXP_LNKCTL_CLOCK),
1136         FLAG(w, PCI_EXP_LNKCTL_XSYNCH));
1137   w = get_conf_word(d, where + PCI_EXP_LNKSTA);
1138   printf("\t\tLink: Speed %s, Width x%d\n",
1139         link_speed(w & PCI_EXP_LNKSTA_SPEED), (w & PCI_EXP_LNKSTA_WIDTH) >> 4);
1140 }
1141
1142 static const char *indicator(int code)
1143 {
1144   static const char *names[] = { "Unknown", "On", "Blink", "Off" };
1145   return names[code];
1146 }
1147
1148 static void show_express_slot(struct device *d, int where)
1149 {
1150   u32 t;
1151   u16 w;
1152
1153   t = get_conf_long(d, where + PCI_EXP_SLTCAP);
1154   printf("\t\tSlot: AtnBtn%c PwrCtrl%c MRL%c AtnInd%c PwrInd%c HotPlug%c Surpise%c\n",
1155         FLAG(t, PCI_EXP_SLTCAP_ATNB),
1156         FLAG(t, PCI_EXP_SLTCAP_PWRC),
1157         FLAG(t, PCI_EXP_SLTCAP_MRL),
1158         FLAG(t, PCI_EXP_SLTCAP_ATNI),
1159         FLAG(t, PCI_EXP_SLTCAP_PWRI),
1160         FLAG(t, PCI_EXP_SLTCAP_HPC),
1161         FLAG(t, PCI_EXP_SLTCAP_HPS));
1162   printf("\t\tSlot: Number %d, PowerLimit %f\n", t >> 19,
1163                 power_limit((t & PCI_EXP_SLTCAP_PWR_VAL) >> 7,
1164                         (t & PCI_EXP_SLTCAP_PWR_SCL) >> 15));
1165   w = get_conf_word(d, where + PCI_EXP_SLTCTL);
1166   printf("\t\tSlot: Enabled AtnBtn%c PwrFlt%c MRL%c PresDet%c CmdCplt%c HPIrq%c\n",
1167         FLAG(w, PCI_EXP_SLTCTL_ATNB),
1168         FLAG(w, PCI_EXP_SLTCTL_PWRF),
1169         FLAG(w, PCI_EXP_SLTCTL_MRLS),
1170         FLAG(w, PCI_EXP_SLTCTL_PRSD),
1171         FLAG(w, PCI_EXP_SLTCTL_CMDC),
1172         FLAG(w, PCI_EXP_SLTCTL_HPIE));
1173   printf("\t\tSlot: AttnInd %s, PwrInd %s, Power%c\n",
1174         indicator((w & PCI_EXP_SLTCTL_ATNI) >> 6),
1175         indicator((w & PCI_EXP_SLTCTL_PWRI) >> 8),
1176         FLAG(w, w & PCI_EXP_SLTCTL_PWRC));
1177 }
1178
1179 static void show_express_root(struct device *d, int where)
1180 {
1181   u16 w = get_conf_word(d, where + PCI_EXP_RTCTL);
1182   printf("\t\tRoot: Correctable%c Non-Fatal%c Fatal%c PME%c\n",
1183         FLAG(w, PCI_EXP_RTCTL_SECEE),
1184         FLAG(w, PCI_EXP_RTCTL_SENFEE),
1185         FLAG(w, PCI_EXP_RTCTL_SEFEE),
1186         FLAG(w, PCI_EXP_RTCTL_PMEIE));
1187 }
1188
1189 static void
1190 show_express(struct device *d, int where, int cap)
1191 {
1192   int type = (cap & PCI_EXP_FLAGS_TYPE) >> 4;
1193   int size;
1194   int slot = 0;
1195
1196   printf("Express ");
1197   switch (type)
1198     {
1199     case PCI_EXP_TYPE_ENDPOINT:
1200       printf("Endpoint");
1201       break;
1202     case PCI_EXP_TYPE_LEG_END:
1203       printf("Legacy Endpoint");
1204       break;
1205     case PCI_EXP_TYPE_ROOT_PORT:
1206       slot = cap & PCI_EXP_FLAGS_SLOT;
1207       printf("Root Port (Slot%c)", FLAG(cap, PCI_EXP_FLAGS_SLOT));
1208       break;
1209     case PCI_EXP_TYPE_UPSTREAM:
1210       printf("Upstream Port");
1211       break;
1212     case PCI_EXP_TYPE_DOWNSTREAM:
1213       slot = cap & PCI_EXP_FLAGS_SLOT;
1214       printf("Downstream Port (Slot%c)", FLAG(cap, PCI_EXP_FLAGS_SLOT));
1215       break;
1216     case PCI_EXP_TYPE_PCI_BRIDGE:
1217       printf("PCI/PCI-X Bridge");
1218       break;
1219     default:
1220       printf("Unknown type");
1221   }
1222   printf(" IRQ %d\n", (cap & PCI_EXP_FLAGS_IRQ) >> 9);
1223   if (verbose < 2)
1224     return;
1225
1226   size = 16;
1227   if (slot)
1228     size = 24;
1229   if (type == PCI_EXP_TYPE_ROOT_PORT)
1230     size = 32;
1231   if (!config_fetch(d, where + PCI_EXP_DEVCAP, size))
1232     return;
1233
1234   show_express_dev(d, where, type);
1235   show_express_link(d, where, type);
1236   if (slot)
1237     show_express_slot(d, where);
1238   if (type == PCI_EXP_TYPE_ROOT_PORT)
1239     show_express_root(d, where);
1240 }
1241
1242 static void
1243 show_msix(struct device *d, int where, int cap)
1244 {
1245   u32 off;
1246
1247   printf("MSI-X: Enable%c Mask%c TabSize=%d\n",
1248          FLAG(cap, PCI_MSIX_ENABLE),
1249          FLAG(cap, PCI_MSIX_MASK),
1250          (cap & PCI_MSIX_TABSIZE) + 1);
1251   if (verbose < 2 || !config_fetch(d, where + PCI_MSIX_TABLE, 8))
1252     return;
1253
1254   off = get_conf_long(d, where + PCI_MSIX_TABLE);
1255   printf("\t\tVector table: BAR=%d offset=%08x\n",
1256          off & PCI_MSIX_BIR, off & ~PCI_MSIX_BIR);
1257   off = get_conf_long(d, where + PCI_MSIX_PBA);
1258   printf("\t\tPBA: BAR=%d offset=%08x\n",
1259          off & PCI_MSIX_BIR, off & ~PCI_MSIX_BIR);
1260 }
1261
1262 static void
1263 show_slotid(int cap)
1264 {
1265   int esr = cap & 0xff;
1266   int chs = cap >> 8;
1267
1268   printf("Slot ID: %d slots, First%c, chassis %02x\n",
1269          esr & PCI_SID_ESR_NSLOTS,
1270          FLAG(esr, PCI_SID_ESR_FIC),
1271          chs);
1272 }
1273
1274 static void
1275 show_ssvid(struct device *d, int where)
1276 {
1277   u16 subsys_v, subsys_d;
1278   char ssnamebuf[256];
1279
1280   if (!config_fetch(d, where, 8))
1281     return;
1282   subsys_v = get_conf_word(d, where + PCI_SSVID_VENDOR);
1283   subsys_d = get_conf_word(d, where + PCI_SSVID_DEVICE);
1284   printf("Subsystem: %s\n",
1285            pci_lookup_name(pacc, ssnamebuf, sizeof(ssnamebuf),
1286                            PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
1287                            d->dev->vendor_id, d->dev->device_id, subsys_v, subsys_d));
1288 }
1289
1290 static void
1291 show_aer(struct device *d UNUSED, int where UNUSED)
1292 {
1293   printf("Advanced Error Reporting\n");
1294 }
1295
1296 static void
1297 show_vc(struct device *d UNUSED, int where UNUSED)
1298 {
1299   printf("Virtual Channel\n");
1300 }
1301
1302 static void
1303 show_dsn(struct device *d, int where)
1304 {
1305   u32 t1, t2;
1306   if (!config_fetch(d, where + 4, 8))
1307     return;
1308   t1 = get_conf_long(d, where + 4);
1309   t2 = get_conf_long(d, where + 8);
1310   printf("Device Serial Number %02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x\n",
1311         t1 & 0xff, (t1 >> 8) & 0xff, (t1 >> 16) & 0xff, t1 >> 24,
1312         t2 & 0xff, (t2 >> 8) & 0xff, (t2 >> 16) & 0xff, t2 >> 24);
1313 }
1314
1315 static void
1316 show_pb(struct device *d UNUSED, int where UNUSED)
1317 {
1318   printf("Power Budgeting\n");
1319 }
1320
1321 static void
1322 show_ext_caps(struct device *d)
1323 {
1324   int where = 0x100;
1325   do
1326     {
1327       u32 header;
1328       int id;
1329
1330       if (!config_fetch(d, where, 4))
1331         break;
1332       header = get_conf_long(d, where);
1333       if (!header)
1334         break;
1335       id = header & 0xffff;
1336       printf("\tCapabilities: [%03x] ", where);
1337       switch (id)
1338         {
1339           case PCI_EXT_CAP_ID_AER:
1340             show_aer(d, where);
1341             break;
1342           case PCI_EXT_CAP_ID_VC:
1343             show_vc(d, where);
1344             break;
1345           case PCI_EXT_CAP_ID_DSN:
1346             show_dsn(d, where);
1347             break;
1348           case PCI_EXT_CAP_ID_PB:
1349             show_pb(d, where);
1350             break;
1351           default:
1352             printf("Unknown (%d)\n", id);
1353             break;
1354         }
1355       where = header >> 20;
1356     } while (where);
1357 }
1358
1359 static void
1360 show_caps(struct device *d)
1361 {
1362   int can_have_ext_caps = 0;
1363
1364   if (get_conf_word(d, PCI_STATUS) & PCI_STATUS_CAP_LIST)
1365     {
1366       int where = get_conf_byte(d, PCI_CAPABILITY_LIST) & ~3;
1367       while (where)
1368         {
1369           int id, next, cap;
1370           printf("\tCapabilities: ");
1371           if (!config_fetch(d, where, 4))
1372             {
1373               puts("<access denied>");
1374               break;
1375             }
1376           id = get_conf_byte(d, where + PCI_CAP_LIST_ID);
1377           next = get_conf_byte(d, where + PCI_CAP_LIST_NEXT) & ~3;
1378           cap = get_conf_word(d, where + PCI_CAP_FLAGS);
1379           printf("[%02x] ", where);
1380           if (id == 0xff)
1381             {
1382               printf("<chain broken>\n");
1383               break;
1384             }
1385           switch (id)
1386             {
1387             case PCI_CAP_ID_PM:
1388               show_pm(d, where, cap);
1389               break;
1390             case PCI_CAP_ID_AGP:
1391               show_agp(d, where, cap);
1392               break;
1393             case PCI_CAP_ID_VPD:
1394               printf("Vital Product Data\n");
1395               break;
1396             case PCI_CAP_ID_SLOTID:
1397               show_slotid(cap);
1398               break;
1399             case PCI_CAP_ID_MSI:
1400               show_msi(d, where, cap);
1401               break;
1402             case PCI_CAP_ID_PCIX:
1403               show_pcix(d, where);
1404               can_have_ext_caps = 1;
1405               break;
1406             case PCI_CAP_ID_HT:
1407               show_ht(d, where, cap);
1408               break;
1409             case PCI_CAP_ID_VNDR:
1410               show_vendor();
1411               break;
1412             case PCI_CAP_ID_DBG:
1413               show_debug();
1414               break;
1415             case PCI_CAP_ID_SSVID:
1416               show_ssvid(d, where);
1417               break;
1418             case PCI_CAP_ID_EXP:
1419               show_express(d, where, cap);
1420               can_have_ext_caps = 1;
1421               break;
1422             case PCI_CAP_ID_MSIX:
1423               show_msix(d, where, cap);
1424               break;
1425             default:
1426               printf("#%02x [%04x]\n", id, cap);
1427             }
1428           where = next;
1429         }
1430     }
1431   if (can_have_ext_caps)
1432     show_ext_caps(d);
1433 }
1434
1435 static void
1436 show_htype0(struct device *d)
1437 {
1438   show_bases(d, 6);
1439   show_rom(d, PCI_ROM_ADDRESS);
1440   show_caps(d);
1441 }
1442
1443 static void
1444 show_htype1(struct device *d)
1445 {
1446   u32 io_base = get_conf_byte(d, PCI_IO_BASE);
1447   u32 io_limit = get_conf_byte(d, PCI_IO_LIMIT);
1448   u32 io_type = io_base & PCI_IO_RANGE_TYPE_MASK;
1449   u32 mem_base = get_conf_word(d, PCI_MEMORY_BASE);
1450   u32 mem_limit = get_conf_word(d, PCI_MEMORY_LIMIT);
1451   u32 mem_type = mem_base & PCI_MEMORY_RANGE_TYPE_MASK;
1452   u32 pref_base = get_conf_word(d, PCI_PREF_MEMORY_BASE);
1453   u32 pref_limit = get_conf_word(d, PCI_PREF_MEMORY_LIMIT);
1454   u32 pref_type = pref_base & PCI_PREF_RANGE_TYPE_MASK;
1455   word sec_stat = get_conf_word(d, PCI_SEC_STATUS);
1456   word brc = get_conf_word(d, PCI_BRIDGE_CONTROL);
1457   int verb = verbose > 2;
1458
1459   show_bases(d, 2);
1460   printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
1461          get_conf_byte(d, PCI_PRIMARY_BUS),
1462          get_conf_byte(d, PCI_SECONDARY_BUS),
1463          get_conf_byte(d, PCI_SUBORDINATE_BUS),
1464          get_conf_byte(d, PCI_SEC_LATENCY_TIMER));
1465
1466   if (io_type != (io_limit & PCI_IO_RANGE_TYPE_MASK) ||
1467       (io_type != PCI_IO_RANGE_TYPE_16 && io_type != PCI_IO_RANGE_TYPE_32))
1468     printf("\t!!! Unknown I/O range types %x/%x\n", io_base, io_limit);
1469   else
1470     {
1471       io_base = (io_base & PCI_IO_RANGE_MASK) << 8;
1472       io_limit = (io_limit & PCI_IO_RANGE_MASK) << 8;
1473       if (io_type == PCI_IO_RANGE_TYPE_32)
1474         {
1475           io_base |= (get_conf_word(d, PCI_IO_BASE_UPPER16) << 16);
1476           io_limit |= (get_conf_word(d, PCI_IO_LIMIT_UPPER16) << 16);
1477         }
1478       if (io_base <= io_limit || verb)
1479         printf("\tI/O behind bridge: %08x-%08x\n", io_base, io_limit+0xfff);
1480     }
1481
1482   if (mem_type != (mem_limit & PCI_MEMORY_RANGE_TYPE_MASK) ||
1483       mem_type)
1484     printf("\t!!! Unknown memory range types %x/%x\n", mem_base, mem_limit);
1485   else
1486     {
1487       mem_base = (mem_base & PCI_MEMORY_RANGE_MASK) << 16;
1488       mem_limit = (mem_limit & PCI_MEMORY_RANGE_MASK) << 16;
1489       if (mem_base <= mem_limit || verb)
1490         printf("\tMemory behind bridge: %08x-%08x\n", mem_base, mem_limit + 0xfffff);
1491     }
1492
1493   if (pref_type != (pref_limit & PCI_PREF_RANGE_TYPE_MASK) ||
1494       (pref_type != PCI_PREF_RANGE_TYPE_32 && pref_type != PCI_PREF_RANGE_TYPE_64))
1495     printf("\t!!! Unknown prefetchable memory range types %x/%x\n", pref_base, pref_limit);
1496   else
1497     {
1498       pref_base = (pref_base & PCI_PREF_RANGE_MASK) << 16;
1499       pref_limit = (pref_limit & PCI_PREF_RANGE_MASK) << 16;
1500       if (pref_base <= pref_limit || verb)
1501         {
1502           if (pref_type == PCI_PREF_RANGE_TYPE_32)
1503             printf("\tPrefetchable memory behind bridge: %08x-%08x\n", pref_base, pref_limit + 0xfffff);
1504           else
1505             printf("\tPrefetchable memory behind bridge: %08x%08x-%08x%08x\n",
1506                    get_conf_long(d, PCI_PREF_BASE_UPPER32),
1507                    pref_base,
1508                    get_conf_long(d, PCI_PREF_LIMIT_UPPER32),
1509                    pref_limit + 0xfffff);
1510         }
1511     }
1512
1513   if (verbose > 1)
1514     printf("\tSecondary status: 66MHz%c FastB2B%c ParErr%c DEVSEL=%s >TAbort%c <TAbort%c <MAbort%c <SERR%c <PERR%c\n",
1515              FLAG(sec_stat, PCI_STATUS_66MHZ),
1516              FLAG(sec_stat, PCI_STATUS_FAST_BACK),
1517              FLAG(sec_stat, PCI_STATUS_PARITY),
1518              ((sec_stat & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
1519              ((sec_stat & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
1520              ((sec_stat & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??",
1521              FLAG(sec_stat, PCI_STATUS_SIG_TARGET_ABORT),
1522              FLAG(sec_stat, PCI_STATUS_REC_TARGET_ABORT),
1523              FLAG(sec_stat, PCI_STATUS_REC_MASTER_ABORT),
1524              FLAG(sec_stat, PCI_STATUS_SIG_SYSTEM_ERROR),
1525              FLAG(sec_stat, PCI_STATUS_DETECTED_PARITY));
1526
1527   show_rom(d, PCI_ROM_ADDRESS1);
1528
1529   if (verbose > 1)
1530     printf("\tBridgeCtl: Parity%c SERR%c NoISA%c VGA%c MAbort%c >Reset%c FastB2B%c\n",
1531            FLAG(brc, PCI_BRIDGE_CTL_PARITY),
1532            FLAG(brc, PCI_BRIDGE_CTL_SERR),
1533            FLAG(brc, PCI_BRIDGE_CTL_NO_ISA),
1534            FLAG(brc, PCI_BRIDGE_CTL_VGA),
1535            FLAG(brc, PCI_BRIDGE_CTL_MASTER_ABORT),
1536            FLAG(brc, PCI_BRIDGE_CTL_BUS_RESET),
1537            FLAG(brc, PCI_BRIDGE_CTL_FAST_BACK));
1538
1539   show_caps(d);
1540 }
1541
1542 static void
1543 show_htype2(struct device *d)
1544 {
1545   int i;
1546   word cmd = get_conf_word(d, PCI_COMMAND);
1547   word brc = get_conf_word(d, PCI_CB_BRIDGE_CONTROL);
1548   word exca;
1549   int verb = verbose > 2;
1550
1551   show_bases(d, 1);
1552   printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
1553          get_conf_byte(d, PCI_CB_PRIMARY_BUS),
1554          get_conf_byte(d, PCI_CB_CARD_BUS),
1555          get_conf_byte(d, PCI_CB_SUBORDINATE_BUS),
1556          get_conf_byte(d, PCI_CB_LATENCY_TIMER));
1557   for(i=0; i<2; i++)
1558     {
1559       int p = 8*i;
1560       u32 base = get_conf_long(d, PCI_CB_MEMORY_BASE_0 + p);
1561       u32 limit = get_conf_long(d, PCI_CB_MEMORY_LIMIT_0 + p);
1562       if (limit > base || verb)
1563         printf("\tMemory window %d: %08x-%08x%s%s\n", i, base, limit,
1564                (cmd & PCI_COMMAND_MEMORY) ? "" : " [disabled]",
1565                (brc & (PCI_CB_BRIDGE_CTL_PREFETCH_MEM0 << i)) ? " (prefetchable)" : "");
1566     }
1567   for(i=0; i<2; i++)
1568     {
1569       int p = 8*i;
1570       u32 base = get_conf_long(d, PCI_CB_IO_BASE_0 + p);
1571       u32 limit = get_conf_long(d, PCI_CB_IO_LIMIT_0 + p);
1572       if (!(base & PCI_IO_RANGE_TYPE_32))
1573         {
1574           base &= 0xffff;
1575           limit &= 0xffff;
1576         }
1577       base &= PCI_CB_IO_RANGE_MASK;
1578       limit = (limit & PCI_CB_IO_RANGE_MASK) + 3;
1579       if (base <= limit || verb)
1580         printf("\tI/O window %d: %08x-%08x%s\n", i, base, limit,
1581                (cmd & PCI_COMMAND_IO) ? "" : " [disabled]");
1582     }
1583
1584   if (get_conf_word(d, PCI_CB_SEC_STATUS) & PCI_STATUS_SIG_SYSTEM_ERROR)
1585     printf("\tSecondary status: SERR\n");
1586   if (verbose > 1)
1587     printf("\tBridgeCtl: Parity%c SERR%c ISA%c VGA%c MAbort%c >Reset%c 16bInt%c PostWrite%c\n",
1588            FLAG(brc, PCI_CB_BRIDGE_CTL_PARITY),
1589            FLAG(brc, PCI_CB_BRIDGE_CTL_SERR),
1590            FLAG(brc, PCI_CB_BRIDGE_CTL_ISA),
1591            FLAG(brc, PCI_CB_BRIDGE_CTL_VGA),
1592            FLAG(brc, PCI_CB_BRIDGE_CTL_MASTER_ABORT),
1593            FLAG(brc, PCI_CB_BRIDGE_CTL_CB_RESET),
1594            FLAG(brc, PCI_CB_BRIDGE_CTL_16BIT_INT),
1595            FLAG(brc, PCI_CB_BRIDGE_CTL_POST_WRITES));
1596
1597   if (d->config_cached < 128)
1598     {
1599       printf("\t<access denied to the rest>\n");
1600       return;
1601     }
1602
1603   exca = get_conf_word(d, PCI_CB_LEGACY_MODE_BASE);
1604   if (exca)
1605     printf("\t16-bit legacy interface ports at %04x\n", exca);
1606 }
1607
1608 static void
1609 show_verbose(struct device *d)
1610 {
1611   struct pci_dev *p = d->dev;
1612   word status = get_conf_word(d, PCI_STATUS);
1613   word cmd = get_conf_word(d, PCI_COMMAND);
1614   word class = p->device_class;
1615   byte bist = get_conf_byte(d, PCI_BIST);
1616   byte htype = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
1617   byte latency = get_conf_byte(d, PCI_LATENCY_TIMER);
1618   byte cache_line = get_conf_byte(d, PCI_CACHE_LINE_SIZE);
1619   byte max_lat, min_gnt;
1620   byte int_pin = get_conf_byte(d, PCI_INTERRUPT_PIN);
1621   unsigned int irq = p->irq;
1622   word subsys_v = 0, subsys_d = 0;
1623   char ssnamebuf[256];
1624
1625   show_terse(d);
1626
1627   switch (htype)
1628     {
1629     case PCI_HEADER_TYPE_NORMAL:
1630       if (class == PCI_CLASS_BRIDGE_PCI)
1631         printf("\t!!! Invalid class %04x for header type %02x\n", class, htype);
1632       max_lat = get_conf_byte(d, PCI_MAX_LAT);
1633       min_gnt = get_conf_byte(d, PCI_MIN_GNT);
1634       subsys_v = get_conf_word(d, PCI_SUBSYSTEM_VENDOR_ID);
1635       subsys_d = get_conf_word(d, PCI_SUBSYSTEM_ID);
1636       break;
1637     case PCI_HEADER_TYPE_BRIDGE:
1638       if ((class >> 8) != PCI_BASE_CLASS_BRIDGE)
1639         printf("\t!!! Invalid class %04x for header type %02x\n", class, htype);
1640       irq = int_pin = min_gnt = max_lat = 0;
1641       break;
1642     case PCI_HEADER_TYPE_CARDBUS:
1643       if ((class >> 8) != PCI_BASE_CLASS_BRIDGE)
1644         printf("\t!!! Invalid class %04x for header type %02x\n", class, htype);
1645       min_gnt = max_lat = 0;
1646       if (d->config_cached >= 128)
1647         {
1648           subsys_v = get_conf_word(d, PCI_CB_SUBSYSTEM_VENDOR_ID);
1649           subsys_d = get_conf_word(d, PCI_CB_SUBSYSTEM_ID);
1650         }
1651       break;
1652     default:
1653       printf("\t!!! Unknown header type %02x\n", htype);
1654       return;
1655     }
1656
1657   if (subsys_v && subsys_v != 0xffff)
1658     printf("\tSubsystem: %s\n",
1659            pci_lookup_name(pacc, ssnamebuf, sizeof(ssnamebuf),
1660                            PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
1661                            p->vendor_id, p->device_id, subsys_v, subsys_d));
1662
1663   if (verbose > 1)
1664     {
1665       printf("\tControl: I/O%c Mem%c BusMaster%c SpecCycle%c MemWINV%c VGASnoop%c ParErr%c Stepping%c SERR%c FastB2B%c\n",
1666              FLAG(cmd, PCI_COMMAND_IO),
1667              FLAG(cmd, PCI_COMMAND_MEMORY),
1668              FLAG(cmd, PCI_COMMAND_MASTER),
1669              FLAG(cmd, PCI_COMMAND_SPECIAL),
1670              FLAG(cmd, PCI_COMMAND_INVALIDATE),
1671              FLAG(cmd, PCI_COMMAND_VGA_PALETTE),
1672              FLAG(cmd, PCI_COMMAND_PARITY),
1673              FLAG(cmd, PCI_COMMAND_WAIT),
1674              FLAG(cmd, PCI_COMMAND_SERR),
1675              FLAG(cmd, PCI_COMMAND_FAST_BACK));
1676       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",
1677              FLAG(status, PCI_STATUS_CAP_LIST),
1678              FLAG(status, PCI_STATUS_66MHZ),
1679              FLAG(status, PCI_STATUS_UDF),
1680              FLAG(status, PCI_STATUS_FAST_BACK),
1681              FLAG(status, PCI_STATUS_PARITY),
1682              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
1683              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
1684              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??",
1685              FLAG(status, PCI_STATUS_SIG_TARGET_ABORT),
1686              FLAG(status, PCI_STATUS_REC_TARGET_ABORT),
1687              FLAG(status, PCI_STATUS_REC_MASTER_ABORT),
1688              FLAG(status, PCI_STATUS_SIG_SYSTEM_ERROR),
1689              FLAG(status, PCI_STATUS_DETECTED_PARITY));
1690       if (cmd & PCI_COMMAND_MASTER)
1691         {
1692           printf("\tLatency: %d", latency);
1693           if (min_gnt || max_lat)
1694             {
1695               printf(" (");
1696               if (min_gnt)
1697                 printf("%dns min", min_gnt*250);
1698               if (min_gnt && max_lat)
1699                 printf(", ");
1700               if (max_lat)
1701                 printf("%dns max", max_lat*250);
1702               putchar(')');
1703             }
1704           if (cache_line)
1705             printf(", Cache Line Size: %d bytes", cache_line * 4);
1706           putchar('\n');
1707         }
1708       if (int_pin || irq)
1709         printf("\tInterrupt: pin %c routed to IRQ " PCIIRQ_FMT "\n",
1710                (int_pin ? 'A' + int_pin - 1 : '?'), irq);
1711     }
1712   else
1713     {
1714       printf("\tFlags: ");
1715       if (cmd & PCI_COMMAND_MASTER)
1716         printf("bus master, ");
1717       if (cmd & PCI_COMMAND_VGA_PALETTE)
1718         printf("VGA palette snoop, ");
1719       if (cmd & PCI_COMMAND_WAIT)
1720         printf("stepping, ");
1721       if (cmd & PCI_COMMAND_FAST_BACK)
1722         printf("fast Back2Back, ");
1723       if (status & PCI_STATUS_66MHZ)
1724         printf("66MHz, ");
1725       if (status & PCI_STATUS_UDF)
1726         printf("user-definable features, ");
1727       printf("%s devsel",
1728              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
1729              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
1730              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??");
1731       if (cmd & PCI_COMMAND_MASTER)
1732         printf(", latency %d", latency);
1733       if (irq)
1734         printf(", IRQ " PCIIRQ_FMT, irq);
1735       putchar('\n');
1736     }
1737
1738   if (bist & PCI_BIST_CAPABLE)
1739     {
1740       if (bist & PCI_BIST_START)
1741         printf("\tBIST is running\n");
1742       else
1743         printf("\tBIST result: %02x\n", bist & PCI_BIST_CODE_MASK);
1744     }
1745
1746   switch (htype)
1747     {
1748     case PCI_HEADER_TYPE_NORMAL:
1749       show_htype0(d);
1750       break;
1751     case PCI_HEADER_TYPE_BRIDGE:
1752       show_htype1(d);
1753       break;
1754     case PCI_HEADER_TYPE_CARDBUS:
1755       show_htype2(d);
1756       break;
1757     }
1758 }
1759
1760 static void
1761 show_hex_dump(struct device *d)
1762 {
1763   unsigned int i, cnt;
1764
1765   cnt = d->config_cached;
1766   if (show_hex >= 3 && config_fetch(d, cnt, 256-cnt))
1767     {
1768       cnt = 256;
1769       if (show_hex >= 4 && config_fetch(d, 256, 4096-256))
1770         cnt = 4096;
1771     }
1772
1773   for(i=0; i<cnt; i++)
1774     {
1775       if (! (i & 15))
1776         printf("%02x:", i);
1777       printf(" %02x", get_conf_byte(d, i));
1778       if ((i & 15) == 15)
1779         putchar('\n');
1780     }
1781 }
1782
1783 static void
1784 show_machine(struct device *d)
1785 {
1786   struct pci_dev *p = d->dev;
1787   int c;
1788   word sv_id=0, sd_id=0;
1789   char classbuf[128], vendbuf[128], devbuf[128], svbuf[128], sdbuf[128];
1790
1791   switch (get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f)
1792     {
1793     case PCI_HEADER_TYPE_NORMAL:
1794       sv_id = get_conf_word(d, PCI_SUBSYSTEM_VENDOR_ID);
1795       sd_id = get_conf_word(d, PCI_SUBSYSTEM_ID);
1796       break;
1797     case PCI_HEADER_TYPE_CARDBUS:
1798       if (d->config_cached >= 128)
1799         {
1800           sv_id = get_conf_word(d, PCI_CB_SUBSYSTEM_VENDOR_ID);
1801           sd_id = get_conf_word(d, PCI_CB_SUBSYSTEM_ID);
1802         }
1803       break;
1804     }
1805
1806   if (verbose)
1807     {
1808       printf("Device:\t");
1809       show_slot_name(d);
1810       putchar('\n');
1811       printf("Class:\t%s\n",
1812              pci_lookup_name(pacc, classbuf, sizeof(classbuf), PCI_LOOKUP_CLASS, p->device_class));
1813       printf("Vendor:\t%s\n",
1814              pci_lookup_name(pacc, vendbuf, sizeof(vendbuf), PCI_LOOKUP_VENDOR, p->vendor_id, p->device_id));
1815       printf("Device:\t%s\n",
1816              pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id));
1817       if (sv_id && sv_id != 0xffff)
1818         {
1819           printf("SVendor:\t%s\n",
1820                  pci_lookup_name(pacc, svbuf, sizeof(svbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR, sv_id));
1821           printf("SDevice:\t%s\n",
1822                  pci_lookup_name(pacc, sdbuf, sizeof(sdbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id, sv_id, sd_id));
1823         }
1824       if (c = get_conf_byte(d, PCI_REVISION_ID))
1825         printf("Rev:\t%02x\n", c);
1826       if (c = get_conf_byte(d, PCI_CLASS_PROG))
1827         printf("ProgIf:\t%02x\n", c);
1828     }
1829   else
1830     {
1831       show_slot_name(d);
1832       printf(" \"%s\" \"%s\" \"%s\"",
1833              pci_lookup_name(pacc, classbuf, sizeof(classbuf), PCI_LOOKUP_CLASS,
1834                              p->device_class),
1835              pci_lookup_name(pacc, vendbuf, sizeof(vendbuf), PCI_LOOKUP_VENDOR,
1836                              p->vendor_id, p->device_id),
1837              pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_DEVICE,
1838                              p->vendor_id, p->device_id));
1839       if (c = get_conf_byte(d, PCI_REVISION_ID))
1840         printf(" -r%02x", c);
1841       if (c = get_conf_byte(d, PCI_CLASS_PROG))
1842         printf(" -p%02x", c);
1843       if (sv_id && sv_id != 0xffff)
1844         printf(" \"%s\" \"%s\"",
1845                pci_lookup_name(pacc, svbuf, sizeof(svbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR, sv_id),
1846                pci_lookup_name(pacc, sdbuf, sizeof(sdbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id, sv_id, sd_id));
1847       else
1848         printf(" \"\" \"\"");
1849       putchar('\n');
1850     }
1851 }
1852
1853 static void
1854 show_device(struct device *d)
1855 {
1856   if (machine_readable)
1857     show_machine(d);
1858   else if (verbose)
1859     show_verbose(d);
1860   else
1861     show_terse(d);
1862   if (show_hex)
1863     show_hex_dump(d);
1864   if (verbose || show_hex)
1865     putchar('\n');
1866 }
1867
1868 static void
1869 show(void)
1870 {
1871   struct device *d;
1872
1873   for(d=first_dev; d; d=d->next)
1874     show_device(d);
1875 }
1876
1877 /* Tree output */
1878
1879 struct bridge {
1880   struct bridge *chain;                 /* Single-linked list of bridges */
1881   struct bridge *next, *child;          /* Tree of bridges */
1882   struct bus *first_bus;                /* List of buses connected to this bridge */
1883   unsigned int domain;
1884   unsigned int primary, secondary, subordinate; /* Bus numbers */
1885   struct device *br_dev;
1886 };
1887
1888 struct bus {
1889   unsigned int domain;
1890   unsigned int number;
1891   struct bus *sibling;
1892   struct device *first_dev, **last_dev;
1893 };
1894
1895 static struct bridge host_bridge = { NULL, NULL, NULL, NULL, 0, ~0, 0, ~0, NULL };
1896
1897 static struct bus *
1898 find_bus(struct bridge *b, unsigned int domain, unsigned int n)
1899 {
1900   struct bus *bus;
1901
1902   for(bus=b->first_bus; bus; bus=bus->sibling)
1903     if (bus->domain == domain && bus->number == n)
1904       break;
1905   return bus;
1906 }
1907
1908 static struct bus *
1909 new_bus(struct bridge *b, unsigned int domain, unsigned int n)
1910 {
1911   struct bus *bus = xmalloc(sizeof(struct bus));
1912   bus->domain = domain;
1913   bus->number = n;
1914   bus->sibling = b->first_bus;
1915   bus->first_dev = NULL;
1916   bus->last_dev = &bus->first_dev;
1917   b->first_bus = bus;
1918   return bus;
1919 }
1920
1921 static void
1922 insert_dev(struct device *d, struct bridge *b)
1923 {
1924   struct pci_dev *p = d->dev;
1925   struct bus *bus;
1926
1927   if (! (bus = find_bus(b, p->domain, p->bus)))
1928     {
1929       struct bridge *c;
1930       for(c=b->child; c; c=c->next)
1931         if (c->domain == p->domain && c->secondary <= p->bus && p->bus <= c->subordinate)
1932           {
1933             insert_dev(d, c);
1934             return;
1935           }
1936       bus = new_bus(b, p->domain, p->bus);
1937     }
1938   /* Simple insertion at the end _does_ guarantee the correct order as the
1939    * original device list was sorted by (domain, bus, devfn) lexicographically
1940    * and all devices on the new list have the same bus number.
1941    */
1942   *bus->last_dev = d;
1943   bus->last_dev = &d->next;
1944   d->next = NULL;
1945 }
1946
1947 static void
1948 grow_tree(void)
1949 {
1950   struct device *d, *d2;
1951   struct bridge **last_br, *b;
1952
1953   /* Build list of bridges */
1954
1955   last_br = &host_bridge.chain;
1956   for(d=first_dev; d; d=d->next)
1957     {
1958       word class = d->dev->device_class;
1959       byte ht = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
1960       if (class == PCI_CLASS_BRIDGE_PCI &&
1961           (ht == PCI_HEADER_TYPE_BRIDGE || ht == PCI_HEADER_TYPE_CARDBUS))
1962         {
1963           b = xmalloc(sizeof(struct bridge));
1964           b->domain = d->dev->domain;
1965           if (ht == PCI_HEADER_TYPE_BRIDGE)
1966             {
1967               b->primary = get_conf_byte(d, PCI_CB_PRIMARY_BUS);
1968               b->secondary = get_conf_byte(d, PCI_CB_CARD_BUS);
1969               b->subordinate = get_conf_byte(d, PCI_CB_SUBORDINATE_BUS);
1970             }
1971           else
1972             {
1973               b->primary = get_conf_byte(d, PCI_PRIMARY_BUS);
1974               b->secondary = get_conf_byte(d, PCI_SECONDARY_BUS);
1975               b->subordinate = get_conf_byte(d, PCI_SUBORDINATE_BUS);
1976             }
1977           *last_br = b;
1978           last_br = &b->chain;
1979           b->next = b->child = NULL;
1980           b->first_bus = NULL;
1981           b->br_dev = d;
1982         }
1983     }
1984   *last_br = NULL;
1985
1986   /* Create a bridge tree */
1987
1988   for(b=&host_bridge; b; b=b->chain)
1989     {
1990       struct bridge *c, *best;
1991       best = NULL;
1992       for(c=&host_bridge; c; c=c->chain)
1993         if (c != b && (c == &host_bridge || b->domain == c->domain) &&
1994             b->primary >= c->secondary && b->primary <= c->subordinate &&
1995             (!best || best->subordinate - best->primary > c->subordinate - c->primary))
1996           best = c;
1997       if (best)
1998         {
1999           b->next = best->child;
2000           best->child = b;
2001         }
2002     }
2003
2004   /* Insert secondary bus for each bridge */
2005
2006   for(b=&host_bridge; b; b=b->chain)
2007     if (!find_bus(b, b->domain, b->secondary))
2008       new_bus(b, b->domain, b->secondary);
2009
2010   /* Create bus structs and link devices */
2011
2012   for(d=first_dev; d;)
2013     {
2014       d2 = d->next;
2015       insert_dev(d, &host_bridge);
2016       d = d2;
2017     }
2018 }
2019
2020 static void
2021 print_it(byte *line, byte *p)
2022 {
2023   *p++ = '\n';
2024   *p = 0;
2025   fputs(line, stdout);
2026   for(p=line; *p; p++)
2027     if (*p == '+' || *p == '|')
2028       *p = '|';
2029     else
2030       *p = ' ';
2031 }
2032
2033 static void show_tree_bridge(struct bridge *, byte *, byte *);
2034
2035 static void
2036 show_tree_dev(struct device *d, byte *line, byte *p)
2037 {
2038   struct pci_dev *q = d->dev;
2039   struct bridge *b;
2040   char namebuf[256];
2041
2042   p += sprintf(p, "%02x.%x", q->dev, q->func);
2043   for(b=&host_bridge; b; b=b->chain)
2044     if (b->br_dev == d)
2045       {
2046         if (b->secondary == b->subordinate)
2047           p += sprintf(p, "-[%04x:%02x]-", b->domain, b->secondary);
2048         else
2049           p += sprintf(p, "-[%04x:%02x-%02x]-", b->domain, b->secondary, b->subordinate);
2050         show_tree_bridge(b, line, p);
2051         return;
2052       }
2053   if (verbose)
2054     p += sprintf(p, "  %s",
2055                  pci_lookup_name(pacc, namebuf, sizeof(namebuf),
2056                                  PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
2057                                  q->vendor_id, q->device_id));
2058   print_it(line, p);
2059 }
2060
2061 static void
2062 show_tree_bus(struct bus *b, byte *line, byte *p)
2063 {
2064   if (!b->first_dev)
2065     print_it(line, p);
2066   else if (!b->first_dev->next)
2067     {
2068       *p++ = '-';
2069       *p++ = '-';
2070       show_tree_dev(b->first_dev, line, p);
2071     }
2072   else
2073     {
2074       struct device *d = b->first_dev;
2075       while (d->next)
2076         {
2077           p[0] = '+';
2078           p[1] = '-';
2079           show_tree_dev(d, line, p+2);
2080           d = d->next;
2081         }
2082       p[0] = '\\';
2083       p[1] = '-';
2084       show_tree_dev(d, line, p+2);
2085     }
2086 }
2087
2088 static void
2089 show_tree_bridge(struct bridge *b, byte *line, byte *p)
2090 {
2091   *p++ = '-';
2092   if (!b->first_bus->sibling)
2093     {
2094       if (b == &host_bridge)
2095         p += sprintf(p, "[%04x:%02x]-", b->domain, b->first_bus->number);
2096       show_tree_bus(b->first_bus, line, p);
2097     }
2098   else
2099     {
2100       struct bus *u = b->first_bus;
2101       byte *k;
2102
2103       while (u->sibling)
2104         {
2105           k = p + sprintf(p, "+-[%04x:%02x]-", u->domain, u->number);
2106           show_tree_bus(u, line, k);
2107           u = u->sibling;
2108         }
2109       k = p + sprintf(p, "\\-[%04x:%02x]-", u->domain, u->number);
2110       show_tree_bus(u, line, k);
2111     }
2112 }
2113
2114 static void
2115 show_forest(void)
2116 {
2117   char line[256];
2118
2119   grow_tree();
2120   show_tree_bridge(&host_bridge, line, line);
2121 }
2122
2123 /* Bus mapping mode */
2124
2125 struct bus_bridge {
2126   struct bus_bridge *next;
2127   byte this, dev, func, first, last, bug;
2128 };
2129
2130 struct bus_info {
2131   byte exists;
2132   byte guestbook;
2133   struct bus_bridge *bridges, *via;
2134 };
2135
2136 static struct bus_info *bus_info;
2137
2138 static void
2139 map_bridge(struct bus_info *bi, struct device *d, int np, int ns, int nl)
2140 {
2141   struct bus_bridge *b = xmalloc(sizeof(struct bus_bridge));
2142   struct pci_dev *p = d->dev;
2143
2144   b->next = bi->bridges;
2145   bi->bridges = b;
2146   b->this = get_conf_byte(d, np);
2147   b->dev = p->dev;
2148   b->func = p->func;
2149   b->first = get_conf_byte(d, ns);
2150   b->last = get_conf_byte(d, nl);
2151   printf("## %02x.%02x:%d is a bridge from %02x to %02x-%02x\n",
2152          p->bus, p->dev, p->func, b->this, b->first, b->last);
2153   if (b->this != p->bus)
2154     printf("!!! Bridge points to invalid primary bus.\n");
2155   if (b->first > b->last)
2156     {
2157       printf("!!! Bridge points to invalid bus range.\n");
2158       b->last = b->first;
2159     }
2160 }
2161
2162 static void
2163 do_map_bus(int bus)
2164 {
2165   int dev, func;
2166   int verbose = pacc->debugging;
2167   struct bus_info *bi = bus_info + bus;
2168   struct device *d;
2169
2170   if (verbose)
2171     printf("Mapping bus %02x\n", bus);
2172   for(dev = 0; dev < 32; dev++)
2173     if (filter.slot < 0 || filter.slot == dev)
2174       {
2175         int func_limit = 1;
2176         for(func = 0; func < func_limit; func++)
2177           if (filter.func < 0 || filter.func == func)
2178             {
2179               /* XXX: Bus mapping supports only domain 0 */
2180               struct pci_dev *p = pci_get_dev(pacc, 0, bus, dev, func);
2181               u16 vendor = pci_read_word(p, PCI_VENDOR_ID);
2182               if (vendor && vendor != 0xffff)
2183                 {
2184                   if (!func && (pci_read_byte(p, PCI_HEADER_TYPE) & 0x80))
2185                     func_limit = 8;
2186                   if (verbose)
2187                     printf("Discovered device %02x:%02x.%d\n", bus, dev, func);
2188                   bi->exists = 1;
2189                   if (d = scan_device(p))
2190                     {
2191                       show_device(d);
2192                       switch (get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f)
2193                         {
2194                         case PCI_HEADER_TYPE_BRIDGE:
2195                           map_bridge(bi, d, PCI_PRIMARY_BUS, PCI_SECONDARY_BUS, PCI_SUBORDINATE_BUS);
2196                           break;
2197                         case PCI_HEADER_TYPE_CARDBUS:
2198                           map_bridge(bi, d, PCI_CB_PRIMARY_BUS, PCI_CB_CARD_BUS, PCI_CB_SUBORDINATE_BUS);
2199                           break;
2200                         }
2201                       free(d);
2202                     }
2203                   else if (verbose)
2204                     printf("But it was filtered out.\n");
2205                 }
2206               pci_free_dev(p);
2207             }
2208       }
2209 }
2210
2211 static void
2212 do_map_bridges(int bus, int min, int max)
2213 {
2214   struct bus_info *bi = bus_info + bus;
2215   struct bus_bridge *b;
2216
2217   bi->guestbook = 1;
2218   for(b=bi->bridges; b; b=b->next)
2219     {
2220       if (bus_info[b->first].guestbook)
2221         b->bug = 1;
2222       else if (b->first < min || b->last > max)
2223         b->bug = 2;
2224       else
2225         {
2226           bus_info[b->first].via = b;
2227           do_map_bridges(b->first, b->first, b->last);
2228         }
2229     }
2230 }
2231
2232 static void
2233 map_bridges(void)
2234 {
2235   int i;
2236
2237   printf("\nSummary of buses:\n\n");
2238   for(i=0; i<256; i++)
2239     if (bus_info[i].exists && !bus_info[i].guestbook)
2240       do_map_bridges(i, 0, 255);
2241   for(i=0; i<256; i++)
2242     {
2243       struct bus_info *bi = bus_info + i;
2244       struct bus_bridge *b = bi->via;
2245
2246       if (bi->exists)
2247         {
2248           printf("%02x: ", i);
2249           if (b)
2250             printf("Entered via %02x:%02x.%d\n", b->this, b->dev, b->func);
2251           else if (!i)
2252             printf("Primary host bus\n");
2253           else
2254             printf("Secondary host bus (?)\n");
2255         }
2256       for(b=bi->bridges; b; b=b->next)
2257         {
2258           printf("\t%02x.%d Bridge to %02x-%02x", b->dev, b->func, b->first, b->last);
2259           switch (b->bug)
2260             {
2261             case 1:
2262               printf(" <overlap bug>");
2263               break;
2264             case 2:
2265               printf(" <crossing bug>");
2266               break;
2267             }
2268           putchar('\n');
2269         }
2270     }
2271 }
2272
2273 static void
2274 map_the_bus(void)
2275 {
2276   if (pacc->method == PCI_ACCESS_PROC_BUS_PCI ||
2277       pacc->method == PCI_ACCESS_DUMP)
2278     printf("WARNING: Bus mapping can be reliable only with direct hardware access enabled.\n\n");
2279   bus_info = xmalloc(sizeof(struct bus_info) * 256);
2280   bzero(bus_info, sizeof(struct bus_info) * 256);
2281   if (filter.bus >= 0)
2282     do_map_bus(filter.bus);
2283   else
2284     {
2285       int bus;
2286       for(bus=0; bus<256; bus++)
2287         do_map_bus(bus);
2288     }
2289   map_bridges();
2290 }
2291
2292 /* Main */
2293
2294 int
2295 main(int argc, char **argv)
2296 {
2297   int i;
2298   char *msg;
2299
2300   if (argc == 2 && !strcmp(argv[1], "--version"))
2301     {
2302       puts("lspci version " PCIUTILS_VERSION);
2303       return 0;
2304     }
2305
2306   pacc = pci_alloc();
2307   pacc->error = die;
2308   pci_filter_init(pacc, &filter);
2309
2310   while ((i = getopt(argc, argv, options)) != -1)
2311     switch (i)
2312       {
2313       case 'n':
2314         pacc->numeric_ids++;
2315         break;
2316       case 'v':
2317         verbose++;
2318         break;
2319       case 'b':
2320         pacc->buscentric = 1;
2321         buscentric_view = 1;
2322         break;
2323       case 's':
2324         if (msg = pci_filter_parse_slot(&filter, optarg))
2325           die("-s: %s", msg);
2326         break;
2327       case 'd':
2328         if (msg = pci_filter_parse_id(&filter, optarg))
2329           die("-d: %s", msg);
2330         break;
2331       case 'x':
2332         show_hex++;
2333         break;
2334       case 't':
2335         show_tree++;
2336         break;
2337       case 'i':
2338         pacc->id_file_name = optarg;
2339         break;
2340       case 'm':
2341         machine_readable++;
2342         break;
2343       case 'M':
2344         map_mode++;
2345         break;
2346       case 'D':
2347         show_domains = 2;
2348         break;
2349       default:
2350         if (parse_generic_option(i, pacc, optarg))
2351           break;
2352       bad:
2353         fprintf(stderr, help_msg, pacc->id_file_name);
2354         return 1;
2355       }
2356   if (optind < argc)
2357     goto bad;
2358
2359   pci_init(pacc);
2360   if (map_mode)
2361     map_the_bus();
2362   else
2363     {
2364       scan_devices();
2365       sort_them();
2366       if (show_tree)
2367         show_forest();
2368       else
2369         show();
2370     }
2371   pci_cleanup(pacc);
2372
2373   return 0;
2374 }