]> mj.ucw.cz Git - pciutils.git/blob - lspci.c
Added support for compressed pci.ids.
[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   char 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 char * 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: Mask%c 64bit%c Queue=%d/%d Enable%c\n",
995          FLAG(cap, PCI_MSI_FLAGS_MASK_BIT),
996          FLAG(cap, PCI_MSI_FLAGS_64BIT),
997          (cap & PCI_MSI_FLAGS_QSIZE) >> 4,
998          (cap & PCI_MSI_FLAGS_QMASK) >> 1,
999          FLAG(cap, PCI_MSI_FLAGS_ENABLE));
1000   if (verbose < 2)
1001     return;
1002   is64 = cap & PCI_MSI_FLAGS_64BIT;
1003   if (!config_fetch(d, where + PCI_MSI_ADDRESS_LO, (is64 ? PCI_MSI_DATA_64 : PCI_MSI_DATA_32) + 2 - PCI_MSI_ADDRESS_LO))
1004     return;
1005   printf("\t\tAddress: ");
1006   if (is64)
1007     {
1008       t = get_conf_long(d, where + PCI_MSI_ADDRESS_HI);
1009       w = get_conf_word(d, where + PCI_MSI_DATA_64);
1010       printf("%08x", t);
1011     }
1012   else
1013     w = get_conf_word(d, where + PCI_MSI_DATA_32);
1014   t = get_conf_long(d, where + PCI_MSI_ADDRESS_LO);
1015   printf("%08x  Data: %04x\n", t, w);
1016   if (cap & PCI_MSI_FLAGS_MASK_BIT)
1017     {
1018       u32 mask, pending;
1019
1020       if (is64)
1021         {
1022           if (!config_fetch(d, where + PCI_MSI_MASK_BIT_64, 8))
1023             return;
1024           mask = get_conf_long(d, where + PCI_MSI_MASK_BIT_64);
1025           pending = get_conf_long(d, where + PCI_MSI_PENDING_64);
1026         }
1027       else
1028         {
1029           if (!config_fetch(d, where + PCI_MSI_MASK_BIT_32, 8))
1030             return;
1031           mask = get_conf_long(d, where + PCI_MSI_MASK_BIT_32);
1032           pending = get_conf_long(d, where + PCI_MSI_PENDING_32);
1033         }
1034       printf("\t\tMasking: %08x  Pending: %08x\n", mask, pending);
1035     }
1036 }
1037
1038 static void show_vendor(void)
1039 {
1040   printf("Vendor Specific Information\n");
1041 }
1042
1043 static void show_debug(void)
1044 {
1045   printf("Debug port\n");
1046 }
1047
1048 static float power_limit(int value, int scale)
1049 {
1050   static const float scales[4] = { 1.0, 0.1, 0.01, 0.001 };
1051   return value * scales[scale];
1052 }
1053
1054 static const char *latency_l0s(int value)
1055 {
1056   static const char *latencies[] = { "<64ns", "<128ns", "<256ns", "<512ns", "<1us", "<2us", "<4us", "unlimited" };
1057   return latencies[value];
1058 }
1059
1060 static const char *latency_l1(int value)
1061 {
1062   static const char *latencies[] = { "<1us", "<2us", "<4us", "<8us", "<16us", "<32us", "<64us", "unlimited" };
1063   return latencies[value];
1064 }
1065
1066 static void show_express_dev(struct device *d, int where, int type)
1067 {
1068   u32 t;
1069   u16 w;
1070
1071   t = get_conf_long(d, where + PCI_EXP_DEVCAP);
1072   printf("\t\tDevice: Supported: MaxPayload %d bytes, PhantFunc %d, ExtTag%c\n",
1073         128 << (t & PCI_EXP_DEVCAP_PAYLOAD),
1074         (1 << ((t & PCI_EXP_DEVCAP_PHANTOM) >> 3)) - 1,
1075         FLAG(t, PCI_EXP_DEVCAP_EXT_TAG));
1076   printf("\t\tDevice: Latency L0s %s, L1 %s\n",
1077         latency_l0s((t & PCI_EXP_DEVCAP_L0S) >> 6),
1078         latency_l1((t & PCI_EXP_DEVCAP_L1) >> 9));
1079   if ((type == PCI_EXP_TYPE_ENDPOINT) || (type == PCI_EXP_TYPE_LEG_END) ||
1080       (type == PCI_EXP_TYPE_UPSTREAM) || (type == PCI_EXP_TYPE_PCI_BRIDGE))
1081     printf("\t\tDevice: AtnBtn%c AtnInd%c PwrInd%c\n",
1082         FLAG(t, PCI_EXP_DEVCAP_ATN_BUT),
1083         FLAG(t, PCI_EXP_DEVCAP_ATN_IND), FLAG(t, PCI_EXP_DEVCAP_PWR_IND));
1084   if (type == PCI_EXP_TYPE_UPSTREAM)
1085     printf("\t\tDevice: SlotPowerLimit %f\n",
1086         power_limit((t & PCI_EXP_DEVCAP_PWR_VAL) >> 18,
1087                     (t & PCI_EXP_DEVCAP_PWR_SCL) >> 26));
1088
1089   w = get_conf_word(d, where + PCI_EXP_DEVCTL);
1090   printf("\t\tDevice: Errors: Correctable%c Non-Fatal%c Fatal%c Unsupported%c\n",
1091         FLAG(w, PCI_EXP_DEVCTL_CERE), 
1092         FLAG(w, PCI_EXP_DEVCTL_NFERE), 
1093         FLAG(w, PCI_EXP_DEVCTL_FERE), 
1094         FLAG(w, PCI_EXP_DEVCTL_URRE));
1095   printf("\t\tDevice: RlxdOrd%c ExtTag%c PhantFunc%c AuxPwr%c NoSnoop%c\n",
1096         FLAG(w, PCI_EXP_DEVCTL_RELAXED),
1097         FLAG(w, PCI_EXP_DEVCTL_EXT_TAG),
1098         FLAG(w, PCI_EXP_DEVCTL_PHANTOM),
1099         FLAG(w, PCI_EXP_DEVCTL_AUX_PME),
1100         FLAG(w, PCI_EXP_DEVCTL_NOSNOOP));
1101   printf("\t\tDevice: MaxPayload %d bytes, MaxReadReq %d bytes\n",
1102         128 << ((w & PCI_EXP_DEVCTL_PAYLOAD) >> 5),
1103         128 << ((w & PCI_EXP_DEVCTL_READRQ) >> 12));
1104 }
1105
1106 static char *link_speed(int speed)
1107 {
1108   switch (speed)
1109     {
1110       case 1:
1111         return "2.5Gb/s";
1112       default:
1113         return "unknown";
1114     }
1115 }
1116
1117 static char *aspm_support(int code)
1118 {
1119   switch (code)
1120     {
1121       case 1:
1122         return "L0s";
1123       case 3:
1124         return "L0s L1";
1125       default:
1126         return "unknown";
1127     }
1128 }
1129
1130 static const char *aspm_enabled(int code)
1131 {
1132   static const char *desc[] = { "Disabled", "L0s Enabled", "L1 Enabled", "L0s L1 Enabled" };
1133   return desc[code];
1134 }
1135
1136 static void show_express_link(struct device *d, int where, int type)
1137 {
1138   u32 t;
1139   u16 w;
1140
1141   t = get_conf_long(d, where + PCI_EXP_LNKCAP);
1142   printf("\t\tLink: Supported Speed %s, Width x%d, ASPM %s, Port %d\n",
1143         link_speed(t & PCI_EXP_LNKCAP_SPEED), (t & PCI_EXP_LNKCAP_WIDTH) >> 4,
1144         aspm_support((t & PCI_EXP_LNKCAP_ASPM) >> 10),
1145         t >> 24);
1146   printf("\t\tLink: Latency L0s %s, L1 %s\n",
1147         latency_l0s((t & PCI_EXP_LNKCAP_L0S) >> 12),
1148         latency_l1((t & PCI_EXP_LNKCAP_L1) >> 15));
1149   w = get_conf_word(d, where + PCI_EXP_LNKCTL);
1150   printf("\t\tLink: ASPM %s", aspm_enabled(w & PCI_EXP_LNKCTL_ASPM));
1151   if ((type == PCI_EXP_TYPE_ROOT_PORT) || (type == PCI_EXP_TYPE_ENDPOINT) ||
1152       (type == PCI_EXP_TYPE_LEG_END))
1153     printf(" RCB %d bytes", w & PCI_EXP_LNKCTL_RCB ? 128 : 64);
1154   if (w & PCI_EXP_LNKCTL_DISABLE)
1155     printf(" Disabled");
1156   printf(" CommClk%c ExtSynch%c\n", FLAG(w, PCI_EXP_LNKCTL_CLOCK),
1157         FLAG(w, PCI_EXP_LNKCTL_XSYNCH));
1158   w = get_conf_word(d, where + PCI_EXP_LNKSTA);
1159   printf("\t\tLink: Speed %s, Width x%d\n",
1160         link_speed(w & PCI_EXP_LNKSTA_SPEED), (w & PCI_EXP_LNKSTA_WIDTH) >> 4);
1161 }
1162
1163 static const char *indicator(int code)
1164 {
1165   static const char *names[] = { "Unknown", "On", "Blink", "Off" };
1166   return names[code];
1167 }
1168
1169 static void show_express_slot(struct device *d, int where)
1170 {
1171   u32 t;
1172   u16 w;
1173
1174   t = get_conf_long(d, where + PCI_EXP_SLTCAP);
1175   printf("\t\tSlot: AtnBtn%c PwrCtrl%c MRL%c AtnInd%c PwrInd%c HotPlug%c Surpise%c\n",
1176         FLAG(t, PCI_EXP_SLTCAP_ATNB),
1177         FLAG(t, PCI_EXP_SLTCAP_PWRC),
1178         FLAG(t, PCI_EXP_SLTCAP_MRL),
1179         FLAG(t, PCI_EXP_SLTCAP_ATNI),
1180         FLAG(t, PCI_EXP_SLTCAP_PWRI),
1181         FLAG(t, PCI_EXP_SLTCAP_HPC),
1182         FLAG(t, PCI_EXP_SLTCAP_HPS));
1183   printf("\t\tSlot: Number %d, PowerLimit %f\n", t >> 19,
1184                 power_limit((t & PCI_EXP_SLTCAP_PWR_VAL) >> 7,
1185                         (t & PCI_EXP_SLTCAP_PWR_SCL) >> 15));
1186   w = get_conf_word(d, where + PCI_EXP_SLTCTL);
1187   printf("\t\tSlot: Enabled AtnBtn%c PwrFlt%c MRL%c PresDet%c CmdCplt%c HPIrq%c\n",
1188         FLAG(w, PCI_EXP_SLTCTL_ATNB),
1189         FLAG(w, PCI_EXP_SLTCTL_PWRF),
1190         FLAG(w, PCI_EXP_SLTCTL_MRLS),
1191         FLAG(w, PCI_EXP_SLTCTL_PRSD),
1192         FLAG(w, PCI_EXP_SLTCTL_CMDC),
1193         FLAG(w, PCI_EXP_SLTCTL_HPIE));
1194   printf("\t\tSlot: AttnInd %s, PwrInd %s, Power%c\n",
1195         indicator((w & PCI_EXP_SLTCTL_ATNI) >> 6),
1196         indicator((w & PCI_EXP_SLTCTL_PWRI) >> 8),
1197         FLAG(w, w & PCI_EXP_SLTCTL_PWRC));
1198 }
1199
1200 static void show_express_root(struct device *d, int where)
1201 {
1202   u16 w = get_conf_word(d, where + PCI_EXP_RTCTL);
1203   printf("\t\tRoot: Correctable%c Non-Fatal%c Fatal%c PME%c\n",
1204         FLAG(w, PCI_EXP_RTCTL_SECEE),
1205         FLAG(w, PCI_EXP_RTCTL_SENFEE),
1206         FLAG(w, PCI_EXP_RTCTL_SEFEE),
1207         FLAG(w, PCI_EXP_RTCTL_PMEIE));
1208 }
1209
1210 static void
1211 show_express(struct device *d, int where, int cap)
1212 {
1213   int type = (cap & PCI_EXP_FLAGS_TYPE) >> 4;
1214   int size;
1215   int slot = 0;
1216
1217   printf("Express ");
1218   switch (type)
1219     {
1220     case PCI_EXP_TYPE_ENDPOINT:
1221       printf("Endpoint");
1222       break;
1223     case PCI_EXP_TYPE_LEG_END:
1224       printf("Legacy Endpoint");
1225       break;
1226     case PCI_EXP_TYPE_ROOT_PORT:
1227       slot = cap & PCI_EXP_FLAGS_SLOT;
1228       printf("Root Port (Slot%c)", FLAG(cap, PCI_EXP_FLAGS_SLOT));
1229       break;
1230     case PCI_EXP_TYPE_UPSTREAM:
1231       printf("Upstream Port");
1232       break;
1233     case PCI_EXP_TYPE_DOWNSTREAM:
1234       slot = cap & PCI_EXP_FLAGS_SLOT;
1235       printf("Downstream Port (Slot%c)", FLAG(cap, PCI_EXP_FLAGS_SLOT));
1236       break;
1237     case PCI_EXP_TYPE_PCI_BRIDGE:
1238       printf("PCI/PCI-X Bridge");
1239       break;
1240     default:
1241       printf("Unknown type");
1242   }
1243   printf(" IRQ %d\n", (cap & PCI_EXP_FLAGS_IRQ) >> 9);
1244   if (verbose < 2)
1245     return;
1246
1247   size = 16;
1248   if (slot)
1249     size = 24;
1250   if (type == PCI_EXP_TYPE_ROOT_PORT)
1251     size = 32;
1252   if (!config_fetch(d, where + PCI_EXP_DEVCAP, size))
1253     return;
1254
1255   show_express_dev(d, where, type);
1256   show_express_link(d, where, type);
1257   if (slot)
1258     show_express_slot(d, where);
1259   if (type == PCI_EXP_TYPE_ROOT_PORT)
1260     show_express_root(d, where);
1261 }
1262
1263 static void
1264 show_msix(struct device *d, int where, int cap)
1265 {
1266   u32 off;
1267
1268   printf("MSI-X: Enable%c Mask%c TabSize=%d\n",
1269          FLAG(cap, PCI_MSIX_ENABLE),
1270          FLAG(cap, PCI_MSIX_MASK),
1271          (cap & PCI_MSIX_TABSIZE) + 1);
1272   if (verbose < 2 || !config_fetch(d, where + PCI_MSIX_TABLE, 8))
1273     return;
1274
1275   off = get_conf_long(d, where + PCI_MSIX_TABLE);
1276   printf("\t\tVector table: BAR=%d offset=%08x\n",
1277          off & PCI_MSIX_BIR, off & ~PCI_MSIX_BIR);
1278   off = get_conf_long(d, where + PCI_MSIX_PBA);
1279   printf("\t\tPBA: BAR=%d offset=%08x\n",
1280          off & PCI_MSIX_BIR, off & ~PCI_MSIX_BIR);
1281 }
1282
1283 static void
1284 show_slotid(int cap)
1285 {
1286   int esr = cap & 0xff;
1287   int chs = cap >> 8;
1288
1289   printf("Slot ID: %d slots, First%c, chassis %02x\n",
1290          esr & PCI_SID_ESR_NSLOTS,
1291          FLAG(esr, PCI_SID_ESR_FIC),
1292          chs);
1293 }
1294
1295 static void
1296 show_ssvid(struct device *d, int where)
1297 {
1298   u16 subsys_v, subsys_d;
1299   char ssnamebuf[256];
1300
1301   if (!config_fetch(d, where, 8))
1302     return;
1303   subsys_v = get_conf_word(d, where + PCI_SSVID_VENDOR);
1304   subsys_d = get_conf_word(d, where + PCI_SSVID_DEVICE);
1305   printf("Subsystem: %s\n",
1306            pci_lookup_name(pacc, ssnamebuf, sizeof(ssnamebuf),
1307                            PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
1308                            d->dev->vendor_id, d->dev->device_id, subsys_v, subsys_d));
1309 }
1310
1311 static void
1312 show_aer(struct device *d UNUSED, int where UNUSED)
1313 {
1314   printf("Advanced Error Reporting\n");
1315 }
1316
1317 static void
1318 show_vc(struct device *d UNUSED, int where UNUSED)
1319 {
1320   printf("Virtual Channel\n");
1321 }
1322
1323 static void
1324 show_dsn(struct device *d, int where)
1325 {
1326   u32 t1, t2;
1327   if (!config_fetch(d, where + 4, 8))
1328     return;
1329   t1 = get_conf_long(d, where + 4);
1330   t2 = get_conf_long(d, where + 8);
1331   printf("Device Serial Number %02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x\n",
1332         t1 & 0xff, (t1 >> 8) & 0xff, (t1 >> 16) & 0xff, t1 >> 24,
1333         t2 & 0xff, (t2 >> 8) & 0xff, (t2 >> 16) & 0xff, t2 >> 24);
1334 }
1335
1336 static void
1337 show_pb(struct device *d UNUSED, int where UNUSED)
1338 {
1339   printf("Power Budgeting\n");
1340 }
1341
1342 static void
1343 show_ext_caps(struct device *d)
1344 {
1345   int where = 0x100;
1346   do
1347     {
1348       u32 header;
1349       int id;
1350
1351       if (!config_fetch(d, where, 4))
1352         break;
1353       header = get_conf_long(d, where);
1354       if (!header)
1355         break;
1356       id = header & 0xffff;
1357       printf("\tCapabilities: [%03x] ", where);
1358       switch (id)
1359         {
1360           case PCI_EXT_CAP_ID_AER:
1361             show_aer(d, where);
1362             break;
1363           case PCI_EXT_CAP_ID_VC:
1364             show_vc(d, where);
1365             break;
1366           case PCI_EXT_CAP_ID_DSN:
1367             show_dsn(d, where);
1368             break;
1369           case PCI_EXT_CAP_ID_PB:
1370             show_pb(d, where);
1371             break;
1372           default:
1373             printf("Unknown (%d)\n", id);
1374             break;
1375         }
1376       where = header >> 20;
1377     } while (where);
1378 }
1379
1380 static void
1381 show_caps(struct device *d)
1382 {
1383   int can_have_ext_caps = 0;
1384
1385   if (get_conf_word(d, PCI_STATUS) & PCI_STATUS_CAP_LIST)
1386     {
1387       int where = get_conf_byte(d, PCI_CAPABILITY_LIST) & ~3;
1388       while (where)
1389         {
1390           int id, next, cap;
1391           printf("\tCapabilities: ");
1392           if (!config_fetch(d, where, 4))
1393             {
1394               puts("<access denied>");
1395               break;
1396             }
1397           id = get_conf_byte(d, where + PCI_CAP_LIST_ID);
1398           next = get_conf_byte(d, where + PCI_CAP_LIST_NEXT) & ~3;
1399           cap = get_conf_word(d, where + PCI_CAP_FLAGS);
1400           printf("[%02x] ", where);
1401           if (id == 0xff)
1402             {
1403               printf("<chain broken>\n");
1404               break;
1405             }
1406           switch (id)
1407             {
1408             case PCI_CAP_ID_PM:
1409               show_pm(d, where, cap);
1410               break;
1411             case PCI_CAP_ID_AGP:
1412               show_agp(d, where, cap);
1413               break;
1414             case PCI_CAP_ID_VPD:
1415               printf("Vital Product Data\n");
1416               break;
1417             case PCI_CAP_ID_SLOTID:
1418               show_slotid(cap);
1419               break;
1420             case PCI_CAP_ID_MSI:
1421               show_msi(d, where, cap);
1422               break;
1423             case PCI_CAP_ID_PCIX:
1424               show_pcix(d, where);
1425               can_have_ext_caps = 1;
1426               break;
1427             case PCI_CAP_ID_HT:
1428               show_ht(d, where, cap);
1429               break;
1430             case PCI_CAP_ID_VNDR:
1431               show_vendor();
1432               break;
1433             case PCI_CAP_ID_DBG:
1434               show_debug();
1435               break;
1436             case PCI_CAP_ID_SSVID:
1437               show_ssvid(d, where);
1438               break;
1439             case PCI_CAP_ID_EXP:
1440               show_express(d, where, cap);
1441               can_have_ext_caps = 1;
1442               break;
1443             case PCI_CAP_ID_MSIX:
1444               show_msix(d, where, cap);
1445               break;
1446             default:
1447               printf("#%02x [%04x]\n", id, cap);
1448             }
1449           where = next;
1450         }
1451     }
1452   if (can_have_ext_caps)
1453     show_ext_caps(d);
1454 }
1455
1456 static void
1457 show_htype0(struct device *d)
1458 {
1459   show_bases(d, 6);
1460   show_rom(d, PCI_ROM_ADDRESS);
1461   show_caps(d);
1462 }
1463
1464 static void
1465 show_htype1(struct device *d)
1466 {
1467   u32 io_base = get_conf_byte(d, PCI_IO_BASE);
1468   u32 io_limit = get_conf_byte(d, PCI_IO_LIMIT);
1469   u32 io_type = io_base & PCI_IO_RANGE_TYPE_MASK;
1470   u32 mem_base = get_conf_word(d, PCI_MEMORY_BASE);
1471   u32 mem_limit = get_conf_word(d, PCI_MEMORY_LIMIT);
1472   u32 mem_type = mem_base & PCI_MEMORY_RANGE_TYPE_MASK;
1473   u32 pref_base = get_conf_word(d, PCI_PREF_MEMORY_BASE);
1474   u32 pref_limit = get_conf_word(d, PCI_PREF_MEMORY_LIMIT);
1475   u32 pref_type = pref_base & PCI_PREF_RANGE_TYPE_MASK;
1476   word sec_stat = get_conf_word(d, PCI_SEC_STATUS);
1477   word brc = get_conf_word(d, PCI_BRIDGE_CONTROL);
1478   int verb = verbose > 2;
1479
1480   show_bases(d, 2);
1481   printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
1482          get_conf_byte(d, PCI_PRIMARY_BUS),
1483          get_conf_byte(d, PCI_SECONDARY_BUS),
1484          get_conf_byte(d, PCI_SUBORDINATE_BUS),
1485          get_conf_byte(d, PCI_SEC_LATENCY_TIMER));
1486
1487   if (io_type != (io_limit & PCI_IO_RANGE_TYPE_MASK) ||
1488       (io_type != PCI_IO_RANGE_TYPE_16 && io_type != PCI_IO_RANGE_TYPE_32))
1489     printf("\t!!! Unknown I/O range types %x/%x\n", io_base, io_limit);
1490   else
1491     {
1492       io_base = (io_base & PCI_IO_RANGE_MASK) << 8;
1493       io_limit = (io_limit & PCI_IO_RANGE_MASK) << 8;
1494       if (io_type == PCI_IO_RANGE_TYPE_32)
1495         {
1496           io_base |= (get_conf_word(d, PCI_IO_BASE_UPPER16) << 16);
1497           io_limit |= (get_conf_word(d, PCI_IO_LIMIT_UPPER16) << 16);
1498         }
1499       if (io_base <= io_limit || verb)
1500         printf("\tI/O behind bridge: %08x-%08x\n", io_base, io_limit+0xfff);
1501     }
1502
1503   if (mem_type != (mem_limit & PCI_MEMORY_RANGE_TYPE_MASK) ||
1504       mem_type)
1505     printf("\t!!! Unknown memory range types %x/%x\n", mem_base, mem_limit);
1506   else
1507     {
1508       mem_base = (mem_base & PCI_MEMORY_RANGE_MASK) << 16;
1509       mem_limit = (mem_limit & PCI_MEMORY_RANGE_MASK) << 16;
1510       if (mem_base <= mem_limit || verb)
1511         printf("\tMemory behind bridge: %08x-%08x\n", mem_base, mem_limit + 0xfffff);
1512     }
1513
1514   if (pref_type != (pref_limit & PCI_PREF_RANGE_TYPE_MASK) ||
1515       (pref_type != PCI_PREF_RANGE_TYPE_32 && pref_type != PCI_PREF_RANGE_TYPE_64))
1516     printf("\t!!! Unknown prefetchable memory range types %x/%x\n", pref_base, pref_limit);
1517   else
1518     {
1519       pref_base = (pref_base & PCI_PREF_RANGE_MASK) << 16;
1520       pref_limit = (pref_limit & PCI_PREF_RANGE_MASK) << 16;
1521       if (pref_base <= pref_limit || verb)
1522         {
1523           if (pref_type == PCI_PREF_RANGE_TYPE_32)
1524             printf("\tPrefetchable memory behind bridge: %08x-%08x\n", pref_base, pref_limit + 0xfffff);
1525           else
1526             printf("\tPrefetchable memory behind bridge: %08x%08x-%08x%08x\n",
1527                    get_conf_long(d, PCI_PREF_BASE_UPPER32),
1528                    pref_base,
1529                    get_conf_long(d, PCI_PREF_LIMIT_UPPER32),
1530                    pref_limit + 0xfffff);
1531         }
1532     }
1533
1534   if (verbose > 1)
1535     printf("\tSecondary status: 66MHz%c FastB2B%c ParErr%c DEVSEL=%s >TAbort%c <TAbort%c <MAbort%c <SERR%c <PERR%c\n",
1536              FLAG(sec_stat, PCI_STATUS_66MHZ),
1537              FLAG(sec_stat, PCI_STATUS_FAST_BACK),
1538              FLAG(sec_stat, PCI_STATUS_PARITY),
1539              ((sec_stat & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
1540              ((sec_stat & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
1541              ((sec_stat & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??",
1542              FLAG(sec_stat, PCI_STATUS_SIG_TARGET_ABORT),
1543              FLAG(sec_stat, PCI_STATUS_REC_TARGET_ABORT),
1544              FLAG(sec_stat, PCI_STATUS_REC_MASTER_ABORT),
1545              FLAG(sec_stat, PCI_STATUS_SIG_SYSTEM_ERROR),
1546              FLAG(sec_stat, PCI_STATUS_DETECTED_PARITY));
1547
1548   show_rom(d, PCI_ROM_ADDRESS1);
1549
1550   if (verbose > 1)
1551     printf("\tBridgeCtl: Parity%c SERR%c NoISA%c VGA%c MAbort%c >Reset%c FastB2B%c\n",
1552            FLAG(brc, PCI_BRIDGE_CTL_PARITY),
1553            FLAG(brc, PCI_BRIDGE_CTL_SERR),
1554            FLAG(brc, PCI_BRIDGE_CTL_NO_ISA),
1555            FLAG(brc, PCI_BRIDGE_CTL_VGA),
1556            FLAG(brc, PCI_BRIDGE_CTL_MASTER_ABORT),
1557            FLAG(brc, PCI_BRIDGE_CTL_BUS_RESET),
1558            FLAG(brc, PCI_BRIDGE_CTL_FAST_BACK));
1559
1560   show_caps(d);
1561 }
1562
1563 static void
1564 show_htype2(struct device *d)
1565 {
1566   int i;
1567   word cmd = get_conf_word(d, PCI_COMMAND);
1568   word brc = get_conf_word(d, PCI_CB_BRIDGE_CONTROL);
1569   word exca;
1570   int verb = verbose > 2;
1571
1572   show_bases(d, 1);
1573   printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
1574          get_conf_byte(d, PCI_CB_PRIMARY_BUS),
1575          get_conf_byte(d, PCI_CB_CARD_BUS),
1576          get_conf_byte(d, PCI_CB_SUBORDINATE_BUS),
1577          get_conf_byte(d, PCI_CB_LATENCY_TIMER));
1578   for(i=0; i<2; i++)
1579     {
1580       int p = 8*i;
1581       u32 base = get_conf_long(d, PCI_CB_MEMORY_BASE_0 + p);
1582       u32 limit = get_conf_long(d, PCI_CB_MEMORY_LIMIT_0 + p);
1583       if (limit > base || verb)
1584         printf("\tMemory window %d: %08x-%08x%s%s\n", i, base, limit,
1585                (cmd & PCI_COMMAND_MEMORY) ? "" : " [disabled]",
1586                (brc & (PCI_CB_BRIDGE_CTL_PREFETCH_MEM0 << i)) ? " (prefetchable)" : "");
1587     }
1588   for(i=0; i<2; i++)
1589     {
1590       int p = 8*i;
1591       u32 base = get_conf_long(d, PCI_CB_IO_BASE_0 + p);
1592       u32 limit = get_conf_long(d, PCI_CB_IO_LIMIT_0 + p);
1593       if (!(base & PCI_IO_RANGE_TYPE_32))
1594         {
1595           base &= 0xffff;
1596           limit &= 0xffff;
1597         }
1598       base &= PCI_CB_IO_RANGE_MASK;
1599       limit = (limit & PCI_CB_IO_RANGE_MASK) + 3;
1600       if (base <= limit || verb)
1601         printf("\tI/O window %d: %08x-%08x%s\n", i, base, limit,
1602                (cmd & PCI_COMMAND_IO) ? "" : " [disabled]");
1603     }
1604
1605   if (get_conf_word(d, PCI_CB_SEC_STATUS) & PCI_STATUS_SIG_SYSTEM_ERROR)
1606     printf("\tSecondary status: SERR\n");
1607   if (verbose > 1)
1608     printf("\tBridgeCtl: Parity%c SERR%c ISA%c VGA%c MAbort%c >Reset%c 16bInt%c PostWrite%c\n",
1609            FLAG(brc, PCI_CB_BRIDGE_CTL_PARITY),
1610            FLAG(brc, PCI_CB_BRIDGE_CTL_SERR),
1611            FLAG(brc, PCI_CB_BRIDGE_CTL_ISA),
1612            FLAG(brc, PCI_CB_BRIDGE_CTL_VGA),
1613            FLAG(brc, PCI_CB_BRIDGE_CTL_MASTER_ABORT),
1614            FLAG(brc, PCI_CB_BRIDGE_CTL_CB_RESET),
1615            FLAG(brc, PCI_CB_BRIDGE_CTL_16BIT_INT),
1616            FLAG(brc, PCI_CB_BRIDGE_CTL_POST_WRITES));
1617
1618   if (d->config_cached < 128)
1619     {
1620       printf("\t<access denied to the rest>\n");
1621       return;
1622     }
1623
1624   exca = get_conf_word(d, PCI_CB_LEGACY_MODE_BASE);
1625   if (exca)
1626     printf("\t16-bit legacy interface ports at %04x\n", exca);
1627 }
1628
1629 static void
1630 show_verbose(struct device *d)
1631 {
1632   struct pci_dev *p = d->dev;
1633   word status = get_conf_word(d, PCI_STATUS);
1634   word cmd = get_conf_word(d, PCI_COMMAND);
1635   word class = p->device_class;
1636   byte bist = get_conf_byte(d, PCI_BIST);
1637   byte htype = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
1638   byte latency = get_conf_byte(d, PCI_LATENCY_TIMER);
1639   byte cache_line = get_conf_byte(d, PCI_CACHE_LINE_SIZE);
1640   byte max_lat, min_gnt;
1641   byte int_pin = get_conf_byte(d, PCI_INTERRUPT_PIN);
1642   unsigned int irq = p->irq;
1643   word subsys_v = 0, subsys_d = 0;
1644   char ssnamebuf[256];
1645
1646   show_terse(d);
1647
1648   switch (htype)
1649     {
1650     case PCI_HEADER_TYPE_NORMAL:
1651       if (class == PCI_CLASS_BRIDGE_PCI)
1652         printf("\t!!! Invalid class %04x for header type %02x\n", class, htype);
1653       max_lat = get_conf_byte(d, PCI_MAX_LAT);
1654       min_gnt = get_conf_byte(d, PCI_MIN_GNT);
1655       subsys_v = get_conf_word(d, PCI_SUBSYSTEM_VENDOR_ID);
1656       subsys_d = get_conf_word(d, PCI_SUBSYSTEM_ID);
1657       break;
1658     case PCI_HEADER_TYPE_BRIDGE:
1659       if ((class >> 8) != PCI_BASE_CLASS_BRIDGE)
1660         printf("\t!!! Invalid class %04x for header type %02x\n", class, htype);
1661       irq = int_pin = min_gnt = max_lat = 0;
1662       break;
1663     case PCI_HEADER_TYPE_CARDBUS:
1664       if ((class >> 8) != PCI_BASE_CLASS_BRIDGE)
1665         printf("\t!!! Invalid class %04x for header type %02x\n", class, htype);
1666       min_gnt = max_lat = 0;
1667       if (d->config_cached >= 128)
1668         {
1669           subsys_v = get_conf_word(d, PCI_CB_SUBSYSTEM_VENDOR_ID);
1670           subsys_d = get_conf_word(d, PCI_CB_SUBSYSTEM_ID);
1671         }
1672       break;
1673     default:
1674       printf("\t!!! Unknown header type %02x\n", htype);
1675       return;
1676     }
1677
1678   if (subsys_v && subsys_v != 0xffff)
1679     printf("\tSubsystem: %s\n",
1680            pci_lookup_name(pacc, ssnamebuf, sizeof(ssnamebuf),
1681                            PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
1682                            p->vendor_id, p->device_id, subsys_v, subsys_d));
1683
1684   if (verbose > 1)
1685     {
1686       printf("\tControl: I/O%c Mem%c BusMaster%c SpecCycle%c MemWINV%c VGASnoop%c ParErr%c Stepping%c SERR%c FastB2B%c\n",
1687              FLAG(cmd, PCI_COMMAND_IO),
1688              FLAG(cmd, PCI_COMMAND_MEMORY),
1689              FLAG(cmd, PCI_COMMAND_MASTER),
1690              FLAG(cmd, PCI_COMMAND_SPECIAL),
1691              FLAG(cmd, PCI_COMMAND_INVALIDATE),
1692              FLAG(cmd, PCI_COMMAND_VGA_PALETTE),
1693              FLAG(cmd, PCI_COMMAND_PARITY),
1694              FLAG(cmd, PCI_COMMAND_WAIT),
1695              FLAG(cmd, PCI_COMMAND_SERR),
1696              FLAG(cmd, PCI_COMMAND_FAST_BACK));
1697       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",
1698              FLAG(status, PCI_STATUS_CAP_LIST),
1699              FLAG(status, PCI_STATUS_66MHZ),
1700              FLAG(status, PCI_STATUS_UDF),
1701              FLAG(status, PCI_STATUS_FAST_BACK),
1702              FLAG(status, PCI_STATUS_PARITY),
1703              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
1704              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
1705              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??",
1706              FLAG(status, PCI_STATUS_SIG_TARGET_ABORT),
1707              FLAG(status, PCI_STATUS_REC_TARGET_ABORT),
1708              FLAG(status, PCI_STATUS_REC_MASTER_ABORT),
1709              FLAG(status, PCI_STATUS_SIG_SYSTEM_ERROR),
1710              FLAG(status, PCI_STATUS_DETECTED_PARITY));
1711       if (cmd & PCI_COMMAND_MASTER)
1712         {
1713           printf("\tLatency: %d", latency);
1714           if (min_gnt || max_lat)
1715             {
1716               printf(" (");
1717               if (min_gnt)
1718                 printf("%dns min", min_gnt*250);
1719               if (min_gnt && max_lat)
1720                 printf(", ");
1721               if (max_lat)
1722                 printf("%dns max", max_lat*250);
1723               putchar(')');
1724             }
1725           if (cache_line)
1726             printf(", Cache Line Size: %d bytes", cache_line * 4);
1727           putchar('\n');
1728         }
1729       if (int_pin || irq)
1730         printf("\tInterrupt: pin %c routed to IRQ " PCIIRQ_FMT "\n",
1731                (int_pin ? 'A' + int_pin - 1 : '?'), irq);
1732     }
1733   else
1734     {
1735       printf("\tFlags: ");
1736       if (cmd & PCI_COMMAND_MASTER)
1737         printf("bus master, ");
1738       if (cmd & PCI_COMMAND_VGA_PALETTE)
1739         printf("VGA palette snoop, ");
1740       if (cmd & PCI_COMMAND_WAIT)
1741         printf("stepping, ");
1742       if (cmd & PCI_COMMAND_FAST_BACK)
1743         printf("fast Back2Back, ");
1744       if (status & PCI_STATUS_66MHZ)
1745         printf("66MHz, ");
1746       if (status & PCI_STATUS_UDF)
1747         printf("user-definable features, ");
1748       printf("%s devsel",
1749              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
1750              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
1751              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??");
1752       if (cmd & PCI_COMMAND_MASTER)
1753         printf(", latency %d", latency);
1754       if (irq)
1755         printf(", IRQ " PCIIRQ_FMT, irq);
1756       putchar('\n');
1757     }
1758
1759   if (bist & PCI_BIST_CAPABLE)
1760     {
1761       if (bist & PCI_BIST_START)
1762         printf("\tBIST is running\n");
1763       else
1764         printf("\tBIST result: %02x\n", bist & PCI_BIST_CODE_MASK);
1765     }
1766
1767   switch (htype)
1768     {
1769     case PCI_HEADER_TYPE_NORMAL:
1770       show_htype0(d);
1771       break;
1772     case PCI_HEADER_TYPE_BRIDGE:
1773       show_htype1(d);
1774       break;
1775     case PCI_HEADER_TYPE_CARDBUS:
1776       show_htype2(d);
1777       break;
1778     }
1779 }
1780
1781 static void
1782 show_hex_dump(struct device *d)
1783 {
1784   unsigned int i, cnt;
1785
1786   cnt = d->config_cached;
1787   if (show_hex >= 3 && config_fetch(d, cnt, 256-cnt))
1788     {
1789       cnt = 256;
1790       if (show_hex >= 4 && config_fetch(d, 256, 4096-256))
1791         cnt = 4096;
1792     }
1793
1794   for(i=0; i<cnt; i++)
1795     {
1796       if (! (i & 15))
1797         printf("%02x:", i);
1798       printf(" %02x", get_conf_byte(d, i));
1799       if ((i & 15) == 15)
1800         putchar('\n');
1801     }
1802 }
1803
1804 static void
1805 show_machine(struct device *d)
1806 {
1807   struct pci_dev *p = d->dev;
1808   int c;
1809   word sv_id=0, sd_id=0;
1810   char classbuf[128], vendbuf[128], devbuf[128], svbuf[128], sdbuf[128];
1811
1812   switch (get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f)
1813     {
1814     case PCI_HEADER_TYPE_NORMAL:
1815       sv_id = get_conf_word(d, PCI_SUBSYSTEM_VENDOR_ID);
1816       sd_id = get_conf_word(d, PCI_SUBSYSTEM_ID);
1817       break;
1818     case PCI_HEADER_TYPE_CARDBUS:
1819       if (d->config_cached >= 128)
1820         {
1821           sv_id = get_conf_word(d, PCI_CB_SUBSYSTEM_VENDOR_ID);
1822           sd_id = get_conf_word(d, PCI_CB_SUBSYSTEM_ID);
1823         }
1824       break;
1825     }
1826
1827   if (verbose)
1828     {
1829       printf("Device:\t");
1830       show_slot_name(d);
1831       putchar('\n');
1832       printf("Class:\t%s\n",
1833              pci_lookup_name(pacc, classbuf, sizeof(classbuf), PCI_LOOKUP_CLASS, p->device_class));
1834       printf("Vendor:\t%s\n",
1835              pci_lookup_name(pacc, vendbuf, sizeof(vendbuf), PCI_LOOKUP_VENDOR, p->vendor_id, p->device_id));
1836       printf("Device:\t%s\n",
1837              pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id));
1838       if (sv_id && sv_id != 0xffff)
1839         {
1840           printf("SVendor:\t%s\n",
1841                  pci_lookup_name(pacc, svbuf, sizeof(svbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR, sv_id));
1842           printf("SDevice:\t%s\n",
1843                  pci_lookup_name(pacc, sdbuf, sizeof(sdbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id, sv_id, sd_id));
1844         }
1845       if (c = get_conf_byte(d, PCI_REVISION_ID))
1846         printf("Rev:\t%02x\n", c);
1847       if (c = get_conf_byte(d, PCI_CLASS_PROG))
1848         printf("ProgIf:\t%02x\n", c);
1849     }
1850   else
1851     {
1852       show_slot_name(d);
1853       printf(" \"%s\" \"%s\" \"%s\"",
1854              pci_lookup_name(pacc, classbuf, sizeof(classbuf), PCI_LOOKUP_CLASS,
1855                              p->device_class),
1856              pci_lookup_name(pacc, vendbuf, sizeof(vendbuf), PCI_LOOKUP_VENDOR,
1857                              p->vendor_id, p->device_id),
1858              pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_DEVICE,
1859                              p->vendor_id, p->device_id));
1860       if (c = get_conf_byte(d, PCI_REVISION_ID))
1861         printf(" -r%02x", c);
1862       if (c = get_conf_byte(d, PCI_CLASS_PROG))
1863         printf(" -p%02x", c);
1864       if (sv_id && sv_id != 0xffff)
1865         printf(" \"%s\" \"%s\"",
1866                pci_lookup_name(pacc, svbuf, sizeof(svbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR, sv_id),
1867                pci_lookup_name(pacc, sdbuf, sizeof(sdbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id, sv_id, sd_id));
1868       else
1869         printf(" \"\" \"\"");
1870       putchar('\n');
1871     }
1872 }
1873
1874 static void
1875 show_device(struct device *d)
1876 {
1877   if (machine_readable)
1878     show_machine(d);
1879   else if (verbose)
1880     show_verbose(d);
1881   else
1882     show_terse(d);
1883   if (show_hex)
1884     show_hex_dump(d);
1885   if (verbose || show_hex)
1886     putchar('\n');
1887 }
1888
1889 static void
1890 show(void)
1891 {
1892   struct device *d;
1893
1894   for(d=first_dev; d; d=d->next)
1895     show_device(d);
1896 }
1897
1898 /* Tree output */
1899
1900 struct bridge {
1901   struct bridge *chain;                 /* Single-linked list of bridges */
1902   struct bridge *next, *child;          /* Tree of bridges */
1903   struct bus *first_bus;                /* List of buses connected to this bridge */
1904   unsigned int domain;
1905   unsigned int primary, secondary, subordinate; /* Bus numbers */
1906   struct device *br_dev;
1907 };
1908
1909 struct bus {
1910   unsigned int domain;
1911   unsigned int number;
1912   struct bus *sibling;
1913   struct device *first_dev, **last_dev;
1914 };
1915
1916 static struct bridge host_bridge = { NULL, NULL, NULL, NULL, 0, ~0, 0, ~0, NULL };
1917
1918 static struct bus *
1919 find_bus(struct bridge *b, unsigned int domain, unsigned int n)
1920 {
1921   struct bus *bus;
1922
1923   for(bus=b->first_bus; bus; bus=bus->sibling)
1924     if (bus->domain == domain && bus->number == n)
1925       break;
1926   return bus;
1927 }
1928
1929 static struct bus *
1930 new_bus(struct bridge *b, unsigned int domain, unsigned int n)
1931 {
1932   struct bus *bus = xmalloc(sizeof(struct bus));
1933   bus->domain = domain;
1934   bus->number = n;
1935   bus->sibling = b->first_bus;
1936   bus->first_dev = NULL;
1937   bus->last_dev = &bus->first_dev;
1938   b->first_bus = bus;
1939   return bus;
1940 }
1941
1942 static void
1943 insert_dev(struct device *d, struct bridge *b)
1944 {
1945   struct pci_dev *p = d->dev;
1946   struct bus *bus;
1947
1948   if (! (bus = find_bus(b, p->domain, p->bus)))
1949     {
1950       struct bridge *c;
1951       for(c=b->child; c; c=c->next)
1952         if (c->domain == p->domain && c->secondary <= p->bus && p->bus <= c->subordinate)
1953           {
1954             insert_dev(d, c);
1955             return;
1956           }
1957       bus = new_bus(b, p->domain, p->bus);
1958     }
1959   /* Simple insertion at the end _does_ guarantee the correct order as the
1960    * original device list was sorted by (domain, bus, devfn) lexicographically
1961    * and all devices on the new list have the same bus number.
1962    */
1963   *bus->last_dev = d;
1964   bus->last_dev = &d->next;
1965   d->next = NULL;
1966 }
1967
1968 static void
1969 grow_tree(void)
1970 {
1971   struct device *d, *d2;
1972   struct bridge **last_br, *b;
1973
1974   /* Build list of bridges */
1975
1976   last_br = &host_bridge.chain;
1977   for(d=first_dev; d; d=d->next)
1978     {
1979       word class = d->dev->device_class;
1980       byte ht = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
1981       if (class == PCI_CLASS_BRIDGE_PCI &&
1982           (ht == PCI_HEADER_TYPE_BRIDGE || ht == PCI_HEADER_TYPE_CARDBUS))
1983         {
1984           b = xmalloc(sizeof(struct bridge));
1985           b->domain = d->dev->domain;
1986           if (ht == PCI_HEADER_TYPE_BRIDGE)
1987             {
1988               b->primary = get_conf_byte(d, PCI_CB_PRIMARY_BUS);
1989               b->secondary = get_conf_byte(d, PCI_CB_CARD_BUS);
1990               b->subordinate = get_conf_byte(d, PCI_CB_SUBORDINATE_BUS);
1991             }
1992           else
1993             {
1994               b->primary = get_conf_byte(d, PCI_PRIMARY_BUS);
1995               b->secondary = get_conf_byte(d, PCI_SECONDARY_BUS);
1996               b->subordinate = get_conf_byte(d, PCI_SUBORDINATE_BUS);
1997             }
1998           *last_br = b;
1999           last_br = &b->chain;
2000           b->next = b->child = NULL;
2001           b->first_bus = NULL;
2002           b->br_dev = d;
2003         }
2004     }
2005   *last_br = NULL;
2006
2007   /* Create a bridge tree */
2008
2009   for(b=&host_bridge; b; b=b->chain)
2010     {
2011       struct bridge *c, *best;
2012       best = NULL;
2013       for(c=&host_bridge; c; c=c->chain)
2014         if (c != b && (c == &host_bridge || b->domain == c->domain) &&
2015             b->primary >= c->secondary && b->primary <= c->subordinate &&
2016             (!best || best->subordinate - best->primary > c->subordinate - c->primary))
2017           best = c;
2018       if (best)
2019         {
2020           b->next = best->child;
2021           best->child = b;
2022         }
2023     }
2024
2025   /* Insert secondary bus for each bridge */
2026
2027   for(b=&host_bridge; b; b=b->chain)
2028     if (!find_bus(b, b->domain, b->secondary))
2029       new_bus(b, b->domain, b->secondary);
2030
2031   /* Create bus structs and link devices */
2032
2033   for(d=first_dev; d;)
2034     {
2035       d2 = d->next;
2036       insert_dev(d, &host_bridge);
2037       d = d2;
2038     }
2039 }
2040
2041 static void
2042 print_it(char *line, char *p)
2043 {
2044   *p++ = '\n';
2045   *p = 0;
2046   fputs(line, stdout);
2047   for(p=line; *p; p++)
2048     if (*p == '+' || *p == '|')
2049       *p = '|';
2050     else
2051       *p = ' ';
2052 }
2053
2054 static void show_tree_bridge(struct bridge *, char *, char *);
2055
2056 static void
2057 show_tree_dev(struct device *d, char *line, char *p)
2058 {
2059   struct pci_dev *q = d->dev;
2060   struct bridge *b;
2061   char namebuf[256];
2062
2063   p += sprintf(p, "%02x.%x", q->dev, q->func);
2064   for(b=&host_bridge; b; b=b->chain)
2065     if (b->br_dev == d)
2066       {
2067         if (b->secondary == b->subordinate)
2068           p += sprintf(p, "-[%04x:%02x]-", b->domain, b->secondary);
2069         else
2070           p += sprintf(p, "-[%04x:%02x-%02x]-", b->domain, b->secondary, b->subordinate);
2071         show_tree_bridge(b, line, p);
2072         return;
2073       }
2074   if (verbose)
2075     p += sprintf(p, "  %s",
2076                  pci_lookup_name(pacc, namebuf, sizeof(namebuf),
2077                                  PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
2078                                  q->vendor_id, q->device_id));
2079   print_it(line, p);
2080 }
2081
2082 static void
2083 show_tree_bus(struct bus *b, char*line, char *p)
2084 {
2085   if (!b->first_dev)
2086     print_it(line, p);
2087   else if (!b->first_dev->next)
2088     {
2089       *p++ = '-';
2090       *p++ = '-';
2091       show_tree_dev(b->first_dev, line, p);
2092     }
2093   else
2094     {
2095       struct device *d = b->first_dev;
2096       while (d->next)
2097         {
2098           p[0] = '+';
2099           p[1] = '-';
2100           show_tree_dev(d, line, p+2);
2101           d = d->next;
2102         }
2103       p[0] = '\\';
2104       p[1] = '-';
2105       show_tree_dev(d, line, p+2);
2106     }
2107 }
2108
2109 static void
2110 show_tree_bridge(struct bridge *b, char *line, char *p)
2111 {
2112   *p++ = '-';
2113   if (!b->first_bus->sibling)
2114     {
2115       if (b == &host_bridge)
2116         p += sprintf(p, "[%04x:%02x]-", b->domain, b->first_bus->number);
2117       show_tree_bus(b->first_bus, line, p);
2118     }
2119   else
2120     {
2121       struct bus *u = b->first_bus;
2122       char *k;
2123
2124       while (u->sibling)
2125         {
2126           k = p + sprintf(p, "+-[%04x:%02x]-", u->domain, u->number);
2127           show_tree_bus(u, line, k);
2128           u = u->sibling;
2129         }
2130       k = p + sprintf(p, "\\-[%04x:%02x]-", u->domain, u->number);
2131       show_tree_bus(u, line, k);
2132     }
2133 }
2134
2135 static void
2136 show_forest(void)
2137 {
2138   char line[256];
2139
2140   grow_tree();
2141   show_tree_bridge(&host_bridge, line, line);
2142 }
2143
2144 /* Bus mapping mode */
2145
2146 struct bus_bridge {
2147   struct bus_bridge *next;
2148   byte this, dev, func, first, last, bug;
2149 };
2150
2151 struct bus_info {
2152   byte exists;
2153   byte guestbook;
2154   struct bus_bridge *bridges, *via;
2155 };
2156
2157 static struct bus_info *bus_info;
2158
2159 static void
2160 map_bridge(struct bus_info *bi, struct device *d, int np, int ns, int nl)
2161 {
2162   struct bus_bridge *b = xmalloc(sizeof(struct bus_bridge));
2163   struct pci_dev *p = d->dev;
2164
2165   b->next = bi->bridges;
2166   bi->bridges = b;
2167   b->this = get_conf_byte(d, np);
2168   b->dev = p->dev;
2169   b->func = p->func;
2170   b->first = get_conf_byte(d, ns);
2171   b->last = get_conf_byte(d, nl);
2172   printf("## %02x.%02x:%d is a bridge from %02x to %02x-%02x\n",
2173          p->bus, p->dev, p->func, b->this, b->first, b->last);
2174   if (b->this != p->bus)
2175     printf("!!! Bridge points to invalid primary bus.\n");
2176   if (b->first > b->last)
2177     {
2178       printf("!!! Bridge points to invalid bus range.\n");
2179       b->last = b->first;
2180     }
2181 }
2182
2183 static void
2184 do_map_bus(int bus)
2185 {
2186   int dev, func;
2187   int verbose = pacc->debugging;
2188   struct bus_info *bi = bus_info + bus;
2189   struct device *d;
2190
2191   if (verbose)
2192     printf("Mapping bus %02x\n", bus);
2193   for(dev = 0; dev < 32; dev++)
2194     if (filter.slot < 0 || filter.slot == dev)
2195       {
2196         int func_limit = 1;
2197         for(func = 0; func < func_limit; func++)
2198           if (filter.func < 0 || filter.func == func)
2199             {
2200               /* XXX: Bus mapping supports only domain 0 */
2201               struct pci_dev *p = pci_get_dev(pacc, 0, bus, dev, func);
2202               u16 vendor = pci_read_word(p, PCI_VENDOR_ID);
2203               if (vendor && vendor != 0xffff)
2204                 {
2205                   if (!func && (pci_read_byte(p, PCI_HEADER_TYPE) & 0x80))
2206                     func_limit = 8;
2207                   if (verbose)
2208                     printf("Discovered device %02x:%02x.%d\n", bus, dev, func);
2209                   bi->exists = 1;
2210                   if (d = scan_device(p))
2211                     {
2212                       show_device(d);
2213                       switch (get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f)
2214                         {
2215                         case PCI_HEADER_TYPE_BRIDGE:
2216                           map_bridge(bi, d, PCI_PRIMARY_BUS, PCI_SECONDARY_BUS, PCI_SUBORDINATE_BUS);
2217                           break;
2218                         case PCI_HEADER_TYPE_CARDBUS:
2219                           map_bridge(bi, d, PCI_CB_PRIMARY_BUS, PCI_CB_CARD_BUS, PCI_CB_SUBORDINATE_BUS);
2220                           break;
2221                         }
2222                       free(d);
2223                     }
2224                   else if (verbose)
2225                     printf("But it was filtered out.\n");
2226                 }
2227               pci_free_dev(p);
2228             }
2229       }
2230 }
2231
2232 static void
2233 do_map_bridges(int bus, int min, int max)
2234 {
2235   struct bus_info *bi = bus_info + bus;
2236   struct bus_bridge *b;
2237
2238   bi->guestbook = 1;
2239   for(b=bi->bridges; b; b=b->next)
2240     {
2241       if (bus_info[b->first].guestbook)
2242         b->bug = 1;
2243       else if (b->first < min || b->last > max)
2244         b->bug = 2;
2245       else
2246         {
2247           bus_info[b->first].via = b;
2248           do_map_bridges(b->first, b->first, b->last);
2249         }
2250     }
2251 }
2252
2253 static void
2254 map_bridges(void)
2255 {
2256   int i;
2257
2258   printf("\nSummary of buses:\n\n");
2259   for(i=0; i<256; i++)
2260     if (bus_info[i].exists && !bus_info[i].guestbook)
2261       do_map_bridges(i, 0, 255);
2262   for(i=0; i<256; i++)
2263     {
2264       struct bus_info *bi = bus_info + i;
2265       struct bus_bridge *b = bi->via;
2266
2267       if (bi->exists)
2268         {
2269           printf("%02x: ", i);
2270           if (b)
2271             printf("Entered via %02x:%02x.%d\n", b->this, b->dev, b->func);
2272           else if (!i)
2273             printf("Primary host bus\n");
2274           else
2275             printf("Secondary host bus (?)\n");
2276         }
2277       for(b=bi->bridges; b; b=b->next)
2278         {
2279           printf("\t%02x.%d Bridge to %02x-%02x", b->dev, b->func, b->first, b->last);
2280           switch (b->bug)
2281             {
2282             case 1:
2283               printf(" <overlap bug>");
2284               break;
2285             case 2:
2286               printf(" <crossing bug>");
2287               break;
2288             }
2289           putchar('\n');
2290         }
2291     }
2292 }
2293
2294 static void
2295 map_the_bus(void)
2296 {
2297   if (pacc->method == PCI_ACCESS_PROC_BUS_PCI ||
2298       pacc->method == PCI_ACCESS_DUMP)
2299     printf("WARNING: Bus mapping can be reliable only with direct hardware access enabled.\n\n");
2300   bus_info = xmalloc(sizeof(struct bus_info) * 256);
2301   bzero(bus_info, sizeof(struct bus_info) * 256);
2302   if (filter.bus >= 0)
2303     do_map_bus(filter.bus);
2304   else
2305     {
2306       int bus;
2307       for(bus=0; bus<256; bus++)
2308         do_map_bus(bus);
2309     }
2310   map_bridges();
2311 }
2312
2313 /* Main */
2314
2315 int
2316 main(int argc, char **argv)
2317 {
2318   int i;
2319   char *msg;
2320
2321   if (argc == 2 && !strcmp(argv[1], "--version"))
2322     {
2323       puts("lspci version " PCIUTILS_VERSION);
2324       return 0;
2325     }
2326
2327   pacc = pci_alloc();
2328   pacc->error = die;
2329   pci_filter_init(pacc, &filter);
2330
2331   while ((i = getopt(argc, argv, options)) != -1)
2332     switch (i)
2333       {
2334       case 'n':
2335         pacc->numeric_ids++;
2336         break;
2337       case 'v':
2338         verbose++;
2339         break;
2340       case 'b':
2341         pacc->buscentric = 1;
2342         buscentric_view = 1;
2343         break;
2344       case 's':
2345         if (msg = pci_filter_parse_slot(&filter, optarg))
2346           die("-s: %s", msg);
2347         break;
2348       case 'd':
2349         if (msg = pci_filter_parse_id(&filter, optarg))
2350           die("-d: %s", msg);
2351         break;
2352       case 'x':
2353         show_hex++;
2354         break;
2355       case 't':
2356         show_tree++;
2357         break;
2358       case 'i':
2359         pci_set_name_list_path(pacc, optarg, 0);
2360         break;
2361       case 'm':
2362         machine_readable++;
2363         break;
2364       case 'M':
2365         map_mode++;
2366         break;
2367       case 'D':
2368         show_domains = 2;
2369         break;
2370       default:
2371         if (parse_generic_option(i, pacc, optarg))
2372           break;
2373       bad:
2374         fprintf(stderr, help_msg, pacc->id_file_name);
2375         return 1;
2376       }
2377   if (optind < argc)
2378     goto bad;
2379
2380   pci_init(pacc);
2381   if (map_mode)
2382     map_the_bus();
2383   else
2384     {
2385       scan_devices();
2386       sort_them();
2387       if (show_tree)
2388         show_forest();
2389       else
2390         show();
2391     }
2392   pci_cleanup(pacc);
2393
2394   return 0;
2395 }