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