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