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