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