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