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