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