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