]> mj.ucw.cz Git - pciutils.git/blob - lspci.c
7879f9b88c51fa3ff0a897a395ada2cba56dcc4a
[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 name[1024], *drv, *base;
1588   int n;
1589
1590   if (dev->access->method != PCI_ACCESS_SYS_BUS_PCI)
1591     return NULL;
1592
1593   base = pci_get_param(dev->access, "sysfs.path");
1594   if (!base || !base[0])
1595     return NULL;
1596
1597   n = snprintf(name, sizeof(name), "%s/devices/%04x:%02x:%02x.%d/driver",
1598                base, dev->domain, dev->bus, dev->dev, dev->func);
1599   if (n < 0 || n >= (int)sizeof(name))
1600     die("show_driver: sysfs device name too long, why?");
1601
1602   n = readlink(name, buf, DRIVER_BUF_SIZE);
1603   if (n < 0)
1604     return NULL;
1605   if (n >= DRIVER_BUF_SIZE)
1606     return "<name-too-long>";
1607   buf[n] = 0;
1608
1609   if (drv = strrchr(buf, '/'))
1610     return drv+1;
1611   else
1612     return buf;
1613 }
1614
1615 static void
1616 show_kernel(struct device *d)
1617 {
1618   char buf[DRIVER_BUF_SIZE];
1619   char *driver;
1620   struct pcimap_entry *e, *last = NULL;
1621
1622   if (driver = find_driver(d, buf))
1623     printf("\tKernel driver in use: %s\n", driver);
1624
1625   load_pcimap();
1626   for (e=pcimap_head; e; e=e->next)
1627     if (match_pcimap(d, e) && (!last || strcmp(last->module, e->module)))
1628       {
1629         printf("%s %s", (last ? "," : "\tKernel modules:"), e->module);
1630         last = e;
1631       }
1632   if (last)
1633     putchar('\n');
1634 }
1635
1636 static void
1637 show_kernel_machine(struct device *d)
1638 {
1639   char buf[DRIVER_BUF_SIZE];
1640   char *driver;
1641   struct pcimap_entry *e, *last = NULL;
1642
1643   if (driver = find_driver(d, buf))
1644     printf("Driver:\t%s\n", driver);
1645
1646   load_pcimap();
1647   for (e=pcimap_head; e; e=e->next)
1648     if (match_pcimap(d, e) && (!last || strcmp(last->module, e->module)))
1649       {
1650         printf("Module:\t%s\n", e->module);
1651         last = e;
1652       }
1653 }
1654
1655 #else
1656
1657 static void
1658 show_kernel(struct device *d UNUSED)
1659 {
1660 }
1661
1662 static void
1663 show_kernel_machine(struct device *d UNUSED)
1664 {
1665 }
1666
1667 #endif
1668
1669 /*** Verbose output ***/
1670
1671 static void
1672 show_size(pciaddr_t x)
1673 {
1674   if (!x)
1675     return;
1676   printf(" [size=");
1677   if (x < 1024)
1678     printf("%d", (int) x);
1679   else if (x < 1048576)
1680     printf("%dK", (int)(x / 1024));
1681   else if (x < 0x80000000)
1682     printf("%dM", (int)(x / 1048576));
1683   else
1684     printf(PCIADDR_T_FMT, x);
1685   putchar(']');
1686 }
1687
1688 static void
1689 show_bases(struct device *d, int cnt)
1690 {
1691   struct pci_dev *p = d->dev;
1692   word cmd = get_conf_word(d, PCI_COMMAND);
1693   int i;
1694
1695   for(i=0; i<cnt; i++)
1696     {
1697       pciaddr_t pos = p->base_addr[i];
1698       pciaddr_t len = (p->known_fields & PCI_FILL_SIZES) ? p->size[i] : 0;
1699       u32 flg = get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i);
1700       if (flg == 0xffffffff)
1701         flg = 0;
1702       if (!pos && !flg && !len)
1703         continue;
1704       if (verbose > 1)
1705         printf("\tRegion %d: ", i);
1706       else
1707         putchar('\t');
1708       if (pos && !flg)                  /* Reported by the OS, but not by the device */
1709         {
1710           printf("[virtual] ");
1711           flg = pos;
1712         }
1713       if (flg & PCI_BASE_ADDRESS_SPACE_IO)
1714         {
1715           pciaddr_t a = pos & PCI_BASE_ADDRESS_IO_MASK;
1716           printf("I/O ports at ");
1717           if (a)
1718             printf(PCIADDR_PORT_FMT, a);
1719           else if (flg & PCI_BASE_ADDRESS_IO_MASK)
1720             printf("<ignored>");
1721           else
1722             printf("<unassigned>");
1723           if (!(cmd & PCI_COMMAND_IO))
1724             printf(" [disabled]");
1725         }
1726       else
1727         {
1728           int t = flg & PCI_BASE_ADDRESS_MEM_TYPE_MASK;
1729           pciaddr_t a = pos & PCI_ADDR_MEM_MASK;
1730           int done = 0;
1731           u32 z = 0;
1732
1733           printf("Memory at ");
1734           if (t == PCI_BASE_ADDRESS_MEM_TYPE_64)
1735             {
1736               if (i >= cnt - 1)
1737                 {
1738                   printf("<invalid-64bit-slot>");
1739                   done = 1;
1740                 }
1741               else
1742                 {
1743                   i++;
1744                   z = get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i);
1745                   if (opt_buscentric)
1746                     {
1747                       u32 y = a & 0xffffffff;
1748                       if (a || z)
1749                         printf("%08x%08x", z, y);
1750                       else
1751                         printf("<unassigned>");
1752                       done = 1;
1753                     }
1754                 }
1755             }
1756           if (!done)
1757             {
1758               if (a)
1759                 printf(PCIADDR_T_FMT, a);
1760               else
1761                 printf(((flg & PCI_BASE_ADDRESS_MEM_MASK) || z) ? "<ignored>" : "<unassigned>");
1762             }
1763           printf(" (%s, %sprefetchable)",
1764                  (t == PCI_BASE_ADDRESS_MEM_TYPE_32) ? "32-bit" :
1765                  (t == PCI_BASE_ADDRESS_MEM_TYPE_64) ? "64-bit" :
1766                  (t == PCI_BASE_ADDRESS_MEM_TYPE_1M) ? "low-1M" : "type 3",
1767                  (flg & PCI_BASE_ADDRESS_MEM_PREFETCH) ? "" : "non-");
1768           if (!(cmd & PCI_COMMAND_MEMORY))
1769             printf(" [disabled]");
1770         }
1771       show_size(len);
1772       putchar('\n');
1773     }
1774 }
1775
1776 static void
1777 show_rom(struct device *d, int reg)
1778 {
1779   struct pci_dev *p = d->dev;
1780   pciaddr_t rom = p->rom_base_addr;
1781   pciaddr_t len = (p->known_fields & PCI_FILL_SIZES) ? p->rom_size : 0;
1782   u32 flg = get_conf_long(d, reg);
1783   word cmd = get_conf_word(d, PCI_COMMAND);
1784
1785   if (!rom && !flg && !len)
1786     return;
1787   putchar('\t');
1788   if ((rom & PCI_ROM_ADDRESS_MASK) && !(flg & PCI_ROM_ADDRESS_MASK))
1789     {
1790       printf("[virtual] ");
1791       flg = rom;
1792     }
1793   printf("Expansion ROM at ");
1794   if (rom & PCI_ROM_ADDRESS_MASK)
1795     printf(PCIADDR_T_FMT, rom & PCI_ROM_ADDRESS_MASK);
1796   else if (flg & PCI_ROM_ADDRESS_MASK)
1797     printf("<ignored>");
1798   else
1799     printf("<unassigned>");
1800   if (!(flg & PCI_ROM_ADDRESS_ENABLE))
1801     printf(" [disabled]");
1802   else if (!(cmd & PCI_COMMAND_MEMORY))
1803     printf(" [disabled by cmd]");
1804   show_size(len);
1805   putchar('\n');
1806 }
1807
1808 static void
1809 show_htype0(struct device *d)
1810 {
1811   show_bases(d, 6);
1812   show_rom(d, PCI_ROM_ADDRESS);
1813   show_caps(d);
1814 }
1815
1816 static void
1817 show_htype1(struct device *d)
1818 {
1819   u32 io_base = get_conf_byte(d, PCI_IO_BASE);
1820   u32 io_limit = get_conf_byte(d, PCI_IO_LIMIT);
1821   u32 io_type = io_base & PCI_IO_RANGE_TYPE_MASK;
1822   u32 mem_base = get_conf_word(d, PCI_MEMORY_BASE);
1823   u32 mem_limit = get_conf_word(d, PCI_MEMORY_LIMIT);
1824   u32 mem_type = mem_base & PCI_MEMORY_RANGE_TYPE_MASK;
1825   u32 pref_base = get_conf_word(d, PCI_PREF_MEMORY_BASE);
1826   u32 pref_limit = get_conf_word(d, PCI_PREF_MEMORY_LIMIT);
1827   u32 pref_type = pref_base & PCI_PREF_RANGE_TYPE_MASK;
1828   word sec_stat = get_conf_word(d, PCI_SEC_STATUS);
1829   word brc = get_conf_word(d, PCI_BRIDGE_CONTROL);
1830   int verb = verbose > 2;
1831
1832   show_bases(d, 2);
1833   printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
1834          get_conf_byte(d, PCI_PRIMARY_BUS),
1835          get_conf_byte(d, PCI_SECONDARY_BUS),
1836          get_conf_byte(d, PCI_SUBORDINATE_BUS),
1837          get_conf_byte(d, PCI_SEC_LATENCY_TIMER));
1838
1839   if (io_type != (io_limit & PCI_IO_RANGE_TYPE_MASK) ||
1840       (io_type != PCI_IO_RANGE_TYPE_16 && io_type != PCI_IO_RANGE_TYPE_32))
1841     printf("\t!!! Unknown I/O range types %x/%x\n", io_base, io_limit);
1842   else
1843     {
1844       io_base = (io_base & PCI_IO_RANGE_MASK) << 8;
1845       io_limit = (io_limit & PCI_IO_RANGE_MASK) << 8;
1846       if (io_type == PCI_IO_RANGE_TYPE_32)
1847         {
1848           io_base |= (get_conf_word(d, PCI_IO_BASE_UPPER16) << 16);
1849           io_limit |= (get_conf_word(d, PCI_IO_LIMIT_UPPER16) << 16);
1850         }
1851       if (io_base <= io_limit || verb)
1852         printf("\tI/O behind bridge: %08x-%08x\n", io_base, io_limit+0xfff);
1853     }
1854
1855   if (mem_type != (mem_limit & PCI_MEMORY_RANGE_TYPE_MASK) ||
1856       mem_type)
1857     printf("\t!!! Unknown memory range types %x/%x\n", mem_base, mem_limit);
1858   else
1859     {
1860       mem_base = (mem_base & PCI_MEMORY_RANGE_MASK) << 16;
1861       mem_limit = (mem_limit & PCI_MEMORY_RANGE_MASK) << 16;
1862       if (mem_base <= mem_limit || verb)
1863         printf("\tMemory behind bridge: %08x-%08x\n", mem_base, mem_limit + 0xfffff);
1864     }
1865
1866   if (pref_type != (pref_limit & PCI_PREF_RANGE_TYPE_MASK) ||
1867       (pref_type != PCI_PREF_RANGE_TYPE_32 && pref_type != PCI_PREF_RANGE_TYPE_64))
1868     printf("\t!!! Unknown prefetchable memory range types %x/%x\n", pref_base, pref_limit);
1869   else
1870     {
1871       pref_base = (pref_base & PCI_PREF_RANGE_MASK) << 16;
1872       pref_limit = (pref_limit & PCI_PREF_RANGE_MASK) << 16;
1873       if (pref_base <= pref_limit || verb)
1874         {
1875           if (pref_type == PCI_PREF_RANGE_TYPE_32)
1876             printf("\tPrefetchable memory behind bridge: %08x-%08x\n", pref_base, pref_limit + 0xfffff);
1877           else
1878             printf("\tPrefetchable memory behind bridge: %08x%08x-%08x%08x\n",
1879                    get_conf_long(d, PCI_PREF_BASE_UPPER32),
1880                    pref_base,
1881                    get_conf_long(d, PCI_PREF_LIMIT_UPPER32),
1882                    pref_limit + 0xfffff);
1883         }
1884     }
1885
1886   if (verbose > 1)
1887     printf("\tSecondary status: 66MHz%c FastB2B%c ParErr%c DEVSEL=%s >TAbort%c <TAbort%c <MAbort%c <SERR%c <PERR%c\n",
1888              FLAG(sec_stat, PCI_STATUS_66MHZ),
1889              FLAG(sec_stat, PCI_STATUS_FAST_BACK),
1890              FLAG(sec_stat, PCI_STATUS_PARITY),
1891              ((sec_stat & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
1892              ((sec_stat & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
1893              ((sec_stat & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??",
1894              FLAG(sec_stat, PCI_STATUS_SIG_TARGET_ABORT),
1895              FLAG(sec_stat, PCI_STATUS_REC_TARGET_ABORT),
1896              FLAG(sec_stat, PCI_STATUS_REC_MASTER_ABORT),
1897              FLAG(sec_stat, PCI_STATUS_SIG_SYSTEM_ERROR),
1898              FLAG(sec_stat, PCI_STATUS_DETECTED_PARITY));
1899
1900   show_rom(d, PCI_ROM_ADDRESS1);
1901
1902   if (verbose > 1)
1903     {
1904       printf("\tBridgeCtl: Parity%c SERR%c NoISA%c VGA%c MAbort%c >Reset%c FastB2B%c\n",
1905         FLAG(brc, PCI_BRIDGE_CTL_PARITY),
1906         FLAG(brc, PCI_BRIDGE_CTL_SERR),
1907         FLAG(brc, PCI_BRIDGE_CTL_NO_ISA),
1908         FLAG(brc, PCI_BRIDGE_CTL_VGA),
1909         FLAG(brc, PCI_BRIDGE_CTL_MASTER_ABORT),
1910         FLAG(brc, PCI_BRIDGE_CTL_BUS_RESET),
1911         FLAG(brc, PCI_BRIDGE_CTL_FAST_BACK));
1912       printf("\t\tPriDiscTmr%c SecDiscTmr%c DiscTmrStat%c DiscTmrSERREn%c\n",
1913         FLAG(brc, PCI_BRIDGE_CTL_PRI_DISCARD_TIMER),
1914         FLAG(brc, PCI_BRIDGE_CTL_SEC_DISCARD_TIMER),
1915         FLAG(brc, PCI_BRIDGE_CTL_DISCARD_TIMER_STATUS),
1916         FLAG(brc, PCI_BRIDGE_CTL_DISCARD_TIMER_SERR_EN));
1917     }
1918
1919   show_caps(d);
1920 }
1921
1922 static void
1923 show_htype2(struct device *d)
1924 {
1925   int i;
1926   word cmd = get_conf_word(d, PCI_COMMAND);
1927   word brc = get_conf_word(d, PCI_CB_BRIDGE_CONTROL);
1928   word exca;
1929   int verb = verbose > 2;
1930
1931   show_bases(d, 1);
1932   printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
1933          get_conf_byte(d, PCI_CB_PRIMARY_BUS),
1934          get_conf_byte(d, PCI_CB_CARD_BUS),
1935          get_conf_byte(d, PCI_CB_SUBORDINATE_BUS),
1936          get_conf_byte(d, PCI_CB_LATENCY_TIMER));
1937   for(i=0; i<2; i++)
1938     {
1939       int p = 8*i;
1940       u32 base = get_conf_long(d, PCI_CB_MEMORY_BASE_0 + p);
1941       u32 limit = get_conf_long(d, PCI_CB_MEMORY_LIMIT_0 + p);
1942       if (limit > base || verb)
1943         printf("\tMemory window %d: %08x-%08x%s%s\n", i, base, limit,
1944                (cmd & PCI_COMMAND_MEMORY) ? "" : " [disabled]",
1945                (brc & (PCI_CB_BRIDGE_CTL_PREFETCH_MEM0 << i)) ? " (prefetchable)" : "");
1946     }
1947   for(i=0; i<2; i++)
1948     {
1949       int p = 8*i;
1950       u32 base = get_conf_long(d, PCI_CB_IO_BASE_0 + p);
1951       u32 limit = get_conf_long(d, PCI_CB_IO_LIMIT_0 + p);
1952       if (!(base & PCI_IO_RANGE_TYPE_32))
1953         {
1954           base &= 0xffff;
1955           limit &= 0xffff;
1956         }
1957       base &= PCI_CB_IO_RANGE_MASK;
1958       limit = (limit & PCI_CB_IO_RANGE_MASK) + 3;
1959       if (base <= limit || verb)
1960         printf("\tI/O window %d: %08x-%08x%s\n", i, base, limit,
1961                (cmd & PCI_COMMAND_IO) ? "" : " [disabled]");
1962     }
1963
1964   if (get_conf_word(d, PCI_CB_SEC_STATUS) & PCI_STATUS_SIG_SYSTEM_ERROR)
1965     printf("\tSecondary status: SERR\n");
1966   if (verbose > 1)
1967     printf("\tBridgeCtl: Parity%c SERR%c ISA%c VGA%c MAbort%c >Reset%c 16bInt%c PostWrite%c\n",
1968            FLAG(brc, PCI_CB_BRIDGE_CTL_PARITY),
1969            FLAG(brc, PCI_CB_BRIDGE_CTL_SERR),
1970            FLAG(brc, PCI_CB_BRIDGE_CTL_ISA),
1971            FLAG(brc, PCI_CB_BRIDGE_CTL_VGA),
1972            FLAG(brc, PCI_CB_BRIDGE_CTL_MASTER_ABORT),
1973            FLAG(brc, PCI_CB_BRIDGE_CTL_CB_RESET),
1974            FLAG(brc, PCI_CB_BRIDGE_CTL_16BIT_INT),
1975            FLAG(brc, PCI_CB_BRIDGE_CTL_POST_WRITES));
1976
1977   if (d->config_cached < 128)
1978     {
1979       printf("\t<access denied to the rest>\n");
1980       return;
1981     }
1982
1983   exca = get_conf_word(d, PCI_CB_LEGACY_MODE_BASE);
1984   if (exca)
1985     printf("\t16-bit legacy interface ports at %04x\n", exca);
1986 }
1987
1988 static void
1989 show_verbose(struct device *d)
1990 {
1991   struct pci_dev *p = d->dev;
1992   word status = get_conf_word(d, PCI_STATUS);
1993   word cmd = get_conf_word(d, PCI_COMMAND);
1994   word class = p->device_class;
1995   byte bist = get_conf_byte(d, PCI_BIST);
1996   byte htype = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
1997   byte latency = get_conf_byte(d, PCI_LATENCY_TIMER);
1998   byte cache_line = get_conf_byte(d, PCI_CACHE_LINE_SIZE);
1999   byte max_lat, min_gnt;
2000   byte int_pin = get_conf_byte(d, PCI_INTERRUPT_PIN);
2001   unsigned int irq = p->irq;
2002   word subsys_v, subsys_d;
2003   char ssnamebuf[256];
2004
2005   show_terse(d);
2006
2007   switch (htype)
2008     {
2009     case PCI_HEADER_TYPE_NORMAL:
2010       if (class == PCI_CLASS_BRIDGE_PCI)
2011         printf("\t!!! Invalid class %04x for header type %02x\n", class, htype);
2012       max_lat = get_conf_byte(d, PCI_MAX_LAT);
2013       min_gnt = get_conf_byte(d, PCI_MIN_GNT);
2014       break;
2015     case PCI_HEADER_TYPE_BRIDGE:
2016       if ((class >> 8) != PCI_BASE_CLASS_BRIDGE)
2017         printf("\t!!! Invalid class %04x for header type %02x\n", class, htype);
2018       irq = int_pin = min_gnt = max_lat = 0;
2019       break;
2020     case PCI_HEADER_TYPE_CARDBUS:
2021       if ((class >> 8) != PCI_BASE_CLASS_BRIDGE)
2022         printf("\t!!! Invalid class %04x for header type %02x\n", class, htype);
2023       min_gnt = max_lat = 0;
2024       break;
2025     default:
2026       printf("\t!!! Unknown header type %02x\n", htype);
2027       return;
2028     }
2029
2030   get_subid(d, &subsys_v, &subsys_d);
2031   if (subsys_v && subsys_v != 0xffff)
2032     printf("\tSubsystem: %s\n",
2033            pci_lookup_name(pacc, ssnamebuf, sizeof(ssnamebuf),
2034                            PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
2035                            p->vendor_id, p->device_id, subsys_v, subsys_d));
2036
2037   if (verbose > 1)
2038     {
2039       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",
2040              FLAG(cmd, PCI_COMMAND_IO),
2041              FLAG(cmd, PCI_COMMAND_MEMORY),
2042              FLAG(cmd, PCI_COMMAND_MASTER),
2043              FLAG(cmd, PCI_COMMAND_SPECIAL),
2044              FLAG(cmd, PCI_COMMAND_INVALIDATE),
2045              FLAG(cmd, PCI_COMMAND_VGA_PALETTE),
2046              FLAG(cmd, PCI_COMMAND_PARITY),
2047              FLAG(cmd, PCI_COMMAND_WAIT),
2048              FLAG(cmd, PCI_COMMAND_SERR),
2049              FLAG(cmd, PCI_COMMAND_FAST_BACK),
2050              FLAG(cmd, PCI_COMMAND_DISABLE_INTx));
2051       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",
2052              FLAG(status, PCI_STATUS_CAP_LIST),
2053              FLAG(status, PCI_STATUS_66MHZ),
2054              FLAG(status, PCI_STATUS_UDF),
2055              FLAG(status, PCI_STATUS_FAST_BACK),
2056              FLAG(status, PCI_STATUS_PARITY),
2057              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
2058              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
2059              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??",
2060              FLAG(status, PCI_STATUS_SIG_TARGET_ABORT),
2061              FLAG(status, PCI_STATUS_REC_TARGET_ABORT),
2062              FLAG(status, PCI_STATUS_REC_MASTER_ABORT),
2063              FLAG(status, PCI_STATUS_SIG_SYSTEM_ERROR),
2064              FLAG(status, PCI_STATUS_DETECTED_PARITY),
2065              FLAG(status, PCI_STATUS_INTx));
2066       if (cmd & PCI_COMMAND_MASTER)
2067         {
2068           printf("\tLatency: %d", latency);
2069           if (min_gnt || max_lat)
2070             {
2071               printf(" (");
2072               if (min_gnt)
2073                 printf("%dns min", min_gnt*250);
2074               if (min_gnt && max_lat)
2075                 printf(", ");
2076               if (max_lat)
2077                 printf("%dns max", max_lat*250);
2078               putchar(')');
2079             }
2080           if (cache_line)
2081             printf(", Cache Line Size: %d bytes", cache_line * 4);
2082           putchar('\n');
2083         }
2084       if (int_pin || irq)
2085         printf("\tInterrupt: pin %c routed to IRQ " PCIIRQ_FMT "\n",
2086                (int_pin ? 'A' + int_pin - 1 : '?'), irq);
2087     }
2088   else
2089     {
2090       printf("\tFlags: ");
2091       if (cmd & PCI_COMMAND_MASTER)
2092         printf("bus master, ");
2093       if (cmd & PCI_COMMAND_VGA_PALETTE)
2094         printf("VGA palette snoop, ");
2095       if (cmd & PCI_COMMAND_WAIT)
2096         printf("stepping, ");
2097       if (cmd & PCI_COMMAND_FAST_BACK)
2098         printf("fast Back2Back, ");
2099       if (status & PCI_STATUS_66MHZ)
2100         printf("66MHz, ");
2101       if (status & PCI_STATUS_UDF)
2102         printf("user-definable features, ");
2103       printf("%s devsel",
2104              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
2105              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
2106              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??");
2107       if (cmd & PCI_COMMAND_MASTER)
2108         printf(", latency %d", latency);
2109       if (irq)
2110         printf(", IRQ " PCIIRQ_FMT, irq);
2111       putchar('\n');
2112     }
2113
2114   if (bist & PCI_BIST_CAPABLE)
2115     {
2116       if (bist & PCI_BIST_START)
2117         printf("\tBIST is running\n");
2118       else
2119         printf("\tBIST result: %02x\n", bist & PCI_BIST_CODE_MASK);
2120     }
2121
2122   switch (htype)
2123     {
2124     case PCI_HEADER_TYPE_NORMAL:
2125       show_htype0(d);
2126       break;
2127     case PCI_HEADER_TYPE_BRIDGE:
2128       show_htype1(d);
2129       break;
2130     case PCI_HEADER_TYPE_CARDBUS:
2131       show_htype2(d);
2132       break;
2133     }
2134 }
2135
2136 /*** Machine-readable dumps ***/
2137
2138 static void
2139 show_hex_dump(struct device *d)
2140 {
2141   unsigned int i, cnt;
2142
2143   cnt = d->config_cached;
2144   if (opt_hex >= 3 && config_fetch(d, cnt, 256-cnt))
2145     {
2146       cnt = 256;
2147       if (opt_hex >= 4 && config_fetch(d, 256, 4096-256))
2148         cnt = 4096;
2149     }
2150
2151   for(i=0; i<cnt; i++)
2152     {
2153       if (! (i & 15))
2154         printf("%02x:", i);
2155       printf(" %02x", get_conf_byte(d, i));
2156       if ((i & 15) == 15)
2157         putchar('\n');
2158     }
2159 }
2160
2161 static void
2162 print_shell_escaped(char *c)
2163 {
2164   printf(" \"");
2165   while (*c)
2166     {
2167       if (*c == '"' || *c == '\\')
2168         putchar('\\');
2169       putchar(*c++);
2170     }
2171   putchar('"');
2172 }
2173
2174 static void
2175 show_machine(struct device *d)
2176 {
2177   struct pci_dev *p = d->dev;
2178   int c;
2179   word sv_id, sd_id;
2180   char classbuf[128], vendbuf[128], devbuf[128], svbuf[128], sdbuf[128];
2181
2182   get_subid(d, &sv_id, &sd_id);
2183
2184   if (verbose)
2185     {
2186       printf((opt_machine >= 2) ? "Slot:\t" : "Device:\t");
2187       show_slot_name(d);
2188       putchar('\n');
2189       printf("Class:\t%s\n",
2190              pci_lookup_name(pacc, classbuf, sizeof(classbuf), PCI_LOOKUP_CLASS, p->device_class));
2191       printf("Vendor:\t%s\n",
2192              pci_lookup_name(pacc, vendbuf, sizeof(vendbuf), PCI_LOOKUP_VENDOR, p->vendor_id, p->device_id));
2193       printf("Device:\t%s\n",
2194              pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id));
2195       if (sv_id && sv_id != 0xffff)
2196         {
2197           printf("SVendor:\t%s\n",
2198                  pci_lookup_name(pacc, svbuf, sizeof(svbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR, sv_id));
2199           printf("SDevice:\t%s\n",
2200                  pci_lookup_name(pacc, sdbuf, sizeof(sdbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id, sv_id, sd_id));
2201         }
2202       if (c = get_conf_byte(d, PCI_REVISION_ID))
2203         printf("Rev:\t%02x\n", c);
2204       if (c = get_conf_byte(d, PCI_CLASS_PROG))
2205         printf("ProgIf:\t%02x\n", c);
2206       if (opt_kernel)
2207         show_kernel_machine(d);
2208     }
2209   else
2210     {
2211       show_slot_name(d);
2212       print_shell_escaped(pci_lookup_name(pacc, classbuf, sizeof(classbuf), PCI_LOOKUP_CLASS, p->device_class));
2213       print_shell_escaped(pci_lookup_name(pacc, vendbuf, sizeof(vendbuf), PCI_LOOKUP_VENDOR, p->vendor_id, p->device_id));
2214       print_shell_escaped(pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id));
2215       if (c = get_conf_byte(d, PCI_REVISION_ID))
2216         printf(" -r%02x", c);
2217       if (c = get_conf_byte(d, PCI_CLASS_PROG))
2218         printf(" -p%02x", c);
2219       if (sv_id && sv_id != 0xffff)
2220         {
2221           print_shell_escaped(pci_lookup_name(pacc, svbuf, sizeof(svbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR, sv_id));
2222           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));
2223         }
2224       else
2225         printf(" \"\" \"\"");
2226       putchar('\n');
2227     }
2228 }
2229
2230 /*** Main show function ***/
2231
2232 static void
2233 show_device(struct device *d)
2234 {
2235   if (opt_machine)
2236     show_machine(d);
2237   else
2238     {
2239       if (verbose)
2240         show_verbose(d);
2241       else
2242         show_terse(d);
2243       if (opt_kernel || verbose)
2244         show_kernel(d);
2245     }
2246   if (opt_hex)
2247     show_hex_dump(d);
2248   if (verbose || opt_hex)
2249     putchar('\n');
2250 }
2251
2252 static void
2253 show(void)
2254 {
2255   struct device *d;
2256
2257   for(d=first_dev; d; d=d->next)
2258     show_device(d);
2259 }
2260
2261 /*** Tree output ***/
2262
2263 struct bridge {
2264   struct bridge *chain;                 /* Single-linked list of bridges */
2265   struct bridge *next, *child;          /* Tree of bridges */
2266   struct bus *first_bus;                /* List of buses connected to this bridge */
2267   unsigned int domain;
2268   unsigned int primary, secondary, subordinate; /* Bus numbers */
2269   struct device *br_dev;
2270 };
2271
2272 struct bus {
2273   unsigned int domain;
2274   unsigned int number;
2275   struct bus *sibling;
2276   struct device *first_dev, **last_dev;
2277 };
2278
2279 static struct bridge host_bridge = { NULL, NULL, NULL, NULL, 0, ~0, 0, ~0, NULL };
2280
2281 static struct bus *
2282 find_bus(struct bridge *b, unsigned int domain, unsigned int n)
2283 {
2284   struct bus *bus;
2285
2286   for(bus=b->first_bus; bus; bus=bus->sibling)
2287     if (bus->domain == domain && bus->number == n)
2288       break;
2289   return bus;
2290 }
2291
2292 static struct bus *
2293 new_bus(struct bridge *b, unsigned int domain, unsigned int n)
2294 {
2295   struct bus *bus = xmalloc(sizeof(struct bus));
2296   bus->domain = domain;
2297   bus->number = n;
2298   bus->sibling = b->first_bus;
2299   bus->first_dev = NULL;
2300   bus->last_dev = &bus->first_dev;
2301   b->first_bus = bus;
2302   return bus;
2303 }
2304
2305 static void
2306 insert_dev(struct device *d, struct bridge *b)
2307 {
2308   struct pci_dev *p = d->dev;
2309   struct bus *bus;
2310
2311   if (! (bus = find_bus(b, p->domain, p->bus)))
2312     {
2313       struct bridge *c;
2314       for(c=b->child; c; c=c->next)
2315         if (c->domain == p->domain && c->secondary <= p->bus && p->bus <= c->subordinate)
2316           {
2317             insert_dev(d, c);
2318             return;
2319           }
2320       bus = new_bus(b, p->domain, p->bus);
2321     }
2322   /* Simple insertion at the end _does_ guarantee the correct order as the
2323    * original device list was sorted by (domain, bus, devfn) lexicographically
2324    * and all devices on the new list have the same bus number.
2325    */
2326   *bus->last_dev = d;
2327   bus->last_dev = &d->next;
2328   d->next = NULL;
2329 }
2330
2331 static void
2332 grow_tree(void)
2333 {
2334   struct device *d, *d2;
2335   struct bridge **last_br, *b;
2336
2337   /* Build list of bridges */
2338
2339   last_br = &host_bridge.chain;
2340   for(d=first_dev; d; d=d->next)
2341     {
2342       word class = d->dev->device_class;
2343       byte ht = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
2344       if (class == PCI_CLASS_BRIDGE_PCI &&
2345           (ht == PCI_HEADER_TYPE_BRIDGE || ht == PCI_HEADER_TYPE_CARDBUS))
2346         {
2347           b = xmalloc(sizeof(struct bridge));
2348           b->domain = d->dev->domain;
2349           if (ht == PCI_HEADER_TYPE_BRIDGE)
2350             {
2351               b->primary = get_conf_byte(d, PCI_PRIMARY_BUS);
2352               b->secondary = get_conf_byte(d, PCI_SECONDARY_BUS);
2353               b->subordinate = get_conf_byte(d, PCI_SUBORDINATE_BUS);
2354             }
2355           else
2356             {
2357               b->primary = get_conf_byte(d, PCI_CB_PRIMARY_BUS);
2358               b->secondary = get_conf_byte(d, PCI_CB_CARD_BUS);
2359               b->subordinate = get_conf_byte(d, PCI_CB_SUBORDINATE_BUS);
2360             }
2361           *last_br = b;
2362           last_br = &b->chain;
2363           b->next = b->child = NULL;
2364           b->first_bus = NULL;
2365           b->br_dev = d;
2366         }
2367     }
2368   *last_br = NULL;
2369
2370   /* Create a bridge tree */
2371
2372   for(b=&host_bridge; b; b=b->chain)
2373     {
2374       struct bridge *c, *best;
2375       best = NULL;
2376       for(c=&host_bridge; c; c=c->chain)
2377         if (c != b && (c == &host_bridge || b->domain == c->domain) &&
2378             b->primary >= c->secondary && b->primary <= c->subordinate &&
2379             (!best || best->subordinate - best->primary > c->subordinate - c->primary))
2380           best = c;
2381       if (best)
2382         {
2383           b->next = best->child;
2384           best->child = b;
2385         }
2386     }
2387
2388   /* Insert secondary bus for each bridge */
2389
2390   for(b=&host_bridge; b; b=b->chain)
2391     if (!find_bus(b, b->domain, b->secondary))
2392       new_bus(b, b->domain, b->secondary);
2393
2394   /* Create bus structs and link devices */
2395
2396   for(d=first_dev; d;)
2397     {
2398       d2 = d->next;
2399       insert_dev(d, &host_bridge);
2400       d = d2;
2401     }
2402 }
2403
2404 static void
2405 print_it(char *line, char *p)
2406 {
2407   *p++ = '\n';
2408   *p = 0;
2409   fputs(line, stdout);
2410   for(p=line; *p; p++)
2411     if (*p == '+' || *p == '|')
2412       *p = '|';
2413     else
2414       *p = ' ';
2415 }
2416
2417 static void show_tree_bridge(struct bridge *, char *, char *);
2418
2419 static void
2420 show_tree_dev(struct device *d, char *line, char *p)
2421 {
2422   struct pci_dev *q = d->dev;
2423   struct bridge *b;
2424   char namebuf[256];
2425
2426   p += sprintf(p, "%02x.%x", q->dev, q->func);
2427   for(b=&host_bridge; b; b=b->chain)
2428     if (b->br_dev == d)
2429       {
2430         if (b->secondary == b->subordinate)
2431           p += sprintf(p, "-[%04x:%02x]-", b->domain, b->secondary);
2432         else
2433           p += sprintf(p, "-[%04x:%02x-%02x]-", b->domain, b->secondary, b->subordinate);
2434         show_tree_bridge(b, line, p);
2435         return;
2436       }
2437   if (verbose)
2438     p += sprintf(p, "  %s",
2439                  pci_lookup_name(pacc, namebuf, sizeof(namebuf),
2440                                  PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
2441                                  q->vendor_id, q->device_id));
2442   print_it(line, p);
2443 }
2444
2445 static void
2446 show_tree_bus(struct bus *b, char *line, char *p)
2447 {
2448   if (!b->first_dev)
2449     print_it(line, p);
2450   else if (!b->first_dev->next)
2451     {
2452       *p++ = '-';
2453       *p++ = '-';
2454       show_tree_dev(b->first_dev, line, p);
2455     }
2456   else
2457     {
2458       struct device *d = b->first_dev;
2459       while (d->next)
2460         {
2461           p[0] = '+';
2462           p[1] = '-';
2463           show_tree_dev(d, line, p+2);
2464           d = d->next;
2465         }
2466       p[0] = '\\';
2467       p[1] = '-';
2468       show_tree_dev(d, line, p+2);
2469     }
2470 }
2471
2472 static void
2473 show_tree_bridge(struct bridge *b, char *line, char *p)
2474 {
2475   *p++ = '-';
2476   if (!b->first_bus->sibling)
2477     {
2478       if (b == &host_bridge)
2479         p += sprintf(p, "[%04x:%02x]-", b->domain, b->first_bus->number);
2480       show_tree_bus(b->first_bus, line, p);
2481     }
2482   else
2483     {
2484       struct bus *u = b->first_bus;
2485       char *k;
2486
2487       while (u->sibling)
2488         {
2489           k = p + sprintf(p, "+-[%04x:%02x]-", u->domain, u->number);
2490           show_tree_bus(u, line, k);
2491           u = u->sibling;
2492         }
2493       k = p + sprintf(p, "\\-[%04x:%02x]-", u->domain, u->number);
2494       show_tree_bus(u, line, k);
2495     }
2496 }
2497
2498 static void
2499 show_forest(void)
2500 {
2501   char line[256];
2502
2503   grow_tree();
2504   show_tree_bridge(&host_bridge, line, line);
2505 }
2506
2507 /*** Bus mapping mode ***/
2508
2509 struct bus_bridge {
2510   struct bus_bridge *next;
2511   byte this, dev, func, first, last, bug;
2512 };
2513
2514 struct bus_info {
2515   byte exists;
2516   byte guestbook;
2517   struct bus_bridge *bridges, *via;
2518 };
2519
2520 static struct bus_info *bus_info;
2521
2522 static void
2523 map_bridge(struct bus_info *bi, struct device *d, int np, int ns, int nl)
2524 {
2525   struct bus_bridge *b = xmalloc(sizeof(struct bus_bridge));
2526   struct pci_dev *p = d->dev;
2527
2528   b->next = bi->bridges;
2529   bi->bridges = b;
2530   b->this = get_conf_byte(d, np);
2531   b->dev = p->dev;
2532   b->func = p->func;
2533   b->first = get_conf_byte(d, ns);
2534   b->last = get_conf_byte(d, nl);
2535   printf("## %02x.%02x:%d is a bridge from %02x to %02x-%02x\n",
2536          p->bus, p->dev, p->func, b->this, b->first, b->last);
2537   if (b->this != p->bus)
2538     printf("!!! Bridge points to invalid primary bus.\n");
2539   if (b->first > b->last)
2540     {
2541       printf("!!! Bridge points to invalid bus range.\n");
2542       b->last = b->first;
2543     }
2544 }
2545
2546 static void
2547 do_map_bus(int bus)
2548 {
2549   int dev, func;
2550   int verbose = pacc->debugging;
2551   struct bus_info *bi = bus_info + bus;
2552   struct device *d;
2553
2554   if (verbose)
2555     printf("Mapping bus %02x\n", bus);
2556   for(dev = 0; dev < 32; dev++)
2557     if (filter.slot < 0 || filter.slot == dev)
2558       {
2559         int func_limit = 1;
2560         for(func = 0; func < func_limit; func++)
2561           if (filter.func < 0 || filter.func == func)
2562             {
2563               /* XXX: Bus mapping supports only domain 0 */
2564               struct pci_dev *p = pci_get_dev(pacc, 0, bus, dev, func);
2565               u16 vendor = pci_read_word(p, PCI_VENDOR_ID);
2566               if (vendor && vendor != 0xffff)
2567                 {
2568                   if (!func && (pci_read_byte(p, PCI_HEADER_TYPE) & 0x80))
2569                     func_limit = 8;
2570                   if (verbose)
2571                     printf("Discovered device %02x:%02x.%d\n", bus, dev, func);
2572                   bi->exists = 1;
2573                   if (d = scan_device(p))
2574                     {
2575                       show_device(d);
2576                       switch (get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f)
2577                         {
2578                         case PCI_HEADER_TYPE_BRIDGE:
2579                           map_bridge(bi, d, PCI_PRIMARY_BUS, PCI_SECONDARY_BUS, PCI_SUBORDINATE_BUS);
2580                           break;
2581                         case PCI_HEADER_TYPE_CARDBUS:
2582                           map_bridge(bi, d, PCI_CB_PRIMARY_BUS, PCI_CB_CARD_BUS, PCI_CB_SUBORDINATE_BUS);
2583                           break;
2584                         }
2585                       free(d);
2586                     }
2587                   else if (verbose)
2588                     printf("But it was filtered out.\n");
2589                 }
2590               pci_free_dev(p);
2591             }
2592       }
2593 }
2594
2595 static void
2596 do_map_bridges(int bus, int min, int max)
2597 {
2598   struct bus_info *bi = bus_info + bus;
2599   struct bus_bridge *b;
2600
2601   bi->guestbook = 1;
2602   for(b=bi->bridges; b; b=b->next)
2603     {
2604       if (bus_info[b->first].guestbook)
2605         b->bug = 1;
2606       else if (b->first < min || b->last > max)
2607         b->bug = 2;
2608       else
2609         {
2610           bus_info[b->first].via = b;
2611           do_map_bridges(b->first, b->first, b->last);
2612         }
2613     }
2614 }
2615
2616 static void
2617 map_bridges(void)
2618 {
2619   int i;
2620
2621   printf("\nSummary of buses:\n\n");
2622   for(i=0; i<256; i++)
2623     if (bus_info[i].exists && !bus_info[i].guestbook)
2624       do_map_bridges(i, 0, 255);
2625   for(i=0; i<256; i++)
2626     {
2627       struct bus_info *bi = bus_info + i;
2628       struct bus_bridge *b = bi->via;
2629
2630       if (bi->exists)
2631         {
2632           printf("%02x: ", i);
2633           if (b)
2634             printf("Entered via %02x:%02x.%d\n", b->this, b->dev, b->func);
2635           else if (!i)
2636             printf("Primary host bus\n");
2637           else
2638             printf("Secondary host bus (?)\n");
2639         }
2640       for(b=bi->bridges; b; b=b->next)
2641         {
2642           printf("\t%02x.%d Bridge to %02x-%02x", b->dev, b->func, b->first, b->last);
2643           switch (b->bug)
2644             {
2645             case 1:
2646               printf(" <overlap bug>");
2647               break;
2648             case 2:
2649               printf(" <crossing bug>");
2650               break;
2651             }
2652           putchar('\n');
2653         }
2654     }
2655 }
2656
2657 static void
2658 map_the_bus(void)
2659 {
2660   if (pacc->method == PCI_ACCESS_PROC_BUS_PCI ||
2661       pacc->method == PCI_ACCESS_DUMP)
2662     printf("WARNING: Bus mapping can be reliable only with direct hardware access enabled.\n\n");
2663   bus_info = xmalloc(sizeof(struct bus_info) * 256);
2664   memset(bus_info, 0, sizeof(struct bus_info) * 256);
2665   if (filter.bus >= 0)
2666     do_map_bus(filter.bus);
2667   else
2668     {
2669       int bus;
2670       for(bus=0; bus<256; bus++)
2671         do_map_bus(bus);
2672     }
2673   map_bridges();
2674 }
2675
2676 /* Main */
2677
2678 int
2679 main(int argc, char **argv)
2680 {
2681   int i;
2682   char *msg;
2683
2684   if (argc == 2 && !strcmp(argv[1], "--version"))
2685     {
2686       puts("lspci version " PCIUTILS_VERSION);
2687       return 0;
2688     }
2689
2690   pacc = pci_alloc();
2691   pacc->error = die;
2692   pci_filter_init(pacc, &filter);
2693
2694   while ((i = getopt(argc, argv, options)) != -1)
2695     switch (i)
2696       {
2697       case 'n':
2698         pacc->numeric_ids++;
2699         break;
2700       case 'v':
2701         verbose++;
2702         break;
2703       case 'b':
2704         pacc->buscentric = 1;
2705         opt_buscentric = 1;
2706         break;
2707       case 's':
2708         if (msg = pci_filter_parse_slot(&filter, optarg))
2709           die("-s: %s", msg);
2710         break;
2711       case 'd':
2712         if (msg = pci_filter_parse_id(&filter, optarg))
2713           die("-d: %s", msg);
2714         break;
2715       case 'x':
2716         opt_hex++;
2717         break;
2718       case 't':
2719         opt_tree++;
2720         break;
2721       case 'i':
2722         pci_set_name_list_path(pacc, optarg, 0);
2723         break;
2724       case 'm':
2725         opt_machine++;
2726         break;
2727       case 'p':
2728         opt_pcimap = optarg;
2729         break;
2730       case 'k':
2731         opt_kernel++;
2732         break;
2733       case 'M':
2734         opt_map_mode++;
2735         break;
2736       case 'D':
2737         opt_domains = 2;
2738         break;
2739 #ifdef PCI_USE_DNS
2740       case 'q':
2741         opt_query_dns++;
2742         break;
2743       case 'Q':
2744         opt_query_all = 1;
2745         break;
2746 #else
2747       case 'q':
2748       case 'Q':
2749         die("DNS queries are not available in this version");
2750 #endif
2751       default:
2752         if (parse_generic_option(i, pacc, optarg))
2753           break;
2754       bad:
2755         fprintf(stderr, help_msg, pacc->id_file_name);
2756         return 1;
2757       }
2758   if (optind < argc)
2759     goto bad;
2760
2761   if (opt_query_dns)
2762     {
2763       pacc->id_lookup_mode |= PCI_LOOKUP_NETWORK;
2764       if (opt_query_dns > 1)
2765         pacc->id_lookup_mode |= PCI_LOOKUP_REFRESH_CACHE;
2766     }
2767   if (opt_query_all)
2768     pacc->id_lookup_mode |= PCI_LOOKUP_NETWORK | PCI_LOOKUP_SKIP_LOCAL;
2769
2770   pci_init(pacc);
2771   if (opt_map_mode)
2772     map_the_bus();
2773   else
2774     {
2775       scan_devices();
2776       sort_them();
2777       if (opt_tree)
2778         show_forest();
2779       else
2780         show();
2781     }
2782   pci_cleanup(pacc);
2783
2784   return (seen_errors ? 2 : 0);
2785 }