]> mj.ucw.cz Git - pciutils.git/blob - lspci.c
Allow header type 1 for any bridge.
[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 -s [[[[<domain>]:]<bus>]:][<slot>][.[<func>]]\tShow only devices in selected slots\n\
38 -d [<vendor>]:[<device>]\tShow only selected devices\n\
39 -t\t\tShow bus tree\n\
40 -m\t\tProduce machine-readable output\n\
41 -i <file>\tUse specified ID database instead of %s\n\
42 -M\t\tEnable `bus mapping' mode (dangerous; root only)\n"
43 GENERIC_HELP
44 ;
45
46 /* Communication with libpci */
47
48 static struct pci_access *pacc;
49
50 /*
51  *  If we aren't being compiled by GCC, use malloc() instead of alloca().
52  *  This increases our memory footprint, but only slightly since we don't
53  *  use alloca() much.
54  */
55
56 #ifndef __GNUC__
57 #define alloca malloc
58 #endif
59
60 /* Our view of the PCI bus */
61
62 struct device {
63   struct device *next;
64   struct pci_dev *dev;
65   unsigned int config_cnt;
66   byte config[256];
67 };
68
69 static struct device *first_dev;
70
71 static struct device *
72 scan_device(struct pci_dev *p)
73 {
74   int how_much = (show_hex > 2) ? 256 : 64;
75   struct device *d;
76
77   if (!pci_filter_match(&filter, p))
78     return NULL;
79   d = xmalloc(sizeof(struct device));
80   bzero(d, sizeof(*d));
81   d->dev = p;
82   if (!pci_read_block(p, 0, d->config, how_much))
83     die("Unable to read %d bytes of configuration space.", how_much);
84   if (how_much < 128 && (d->config[PCI_HEADER_TYPE] & 0x7f) == PCI_HEADER_TYPE_CARDBUS)
85     {
86       /* For cardbus bridges, we need to fetch 64 bytes more to get the full standard header... */
87       if (!pci_read_block(p, 64, d->config+64, 64))
88         die("Unable to read cardbus bridge extension data.");
89       how_much = 128;
90     }
91   d->config_cnt = how_much;
92   pci_setup_cache(p, d->config, d->config_cnt);
93   pci_fill_info(p, PCI_FILL_IDENT | PCI_FILL_IRQ | PCI_FILL_BASES | PCI_FILL_ROM_BASE | PCI_FILL_SIZES);
94   return d;
95 }
96
97 static void
98 scan_devices(void)
99 {
100   struct device *d;
101   struct pci_dev *p;
102
103   pci_scan_bus(pacc);
104   for(p=pacc->devices; p; p=p->next)
105     if (d = scan_device(p))
106       {
107         d->next = first_dev;
108         first_dev = d;
109       }
110 }
111
112 static int
113 check_root(void)
114 {
115   static int is_root = -1;
116
117   if (is_root < 0)
118     is_root = !geteuid();
119   return is_root;
120 }
121
122 static int
123 config_fetch(struct device *d, unsigned int pos, unsigned int len)
124 {
125   if (pos + len < d->config_cnt)
126     return 1;
127   if (pacc->method != PCI_ACCESS_DUMP && !check_root())
128     return 0;
129   return pci_read_block(d->dev, pos, d->config + pos, len);
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
577   printf("HyperTransport: Slave or Primary Interface\n");
578   if (verbose < 2)
579     return;
580
581   printf("\t\tCommand: BaseUnitID=%u UnitCnt=%u MastHost%c DefDir%c DUL%c\n",
582          (cmd & PCI_HT_PRI_CMD_BUID),
583          (cmd & PCI_HT_PRI_CMD_UC) >> 5,
584          FLAG(cmd, PCI_HT_PRI_CMD_MH),
585          FLAG(cmd, PCI_HT_PRI_CMD_DD),
586          FLAG(cmd, PCI_HT_PRI_CMD_DUL));
587   config_fetch(d, where + PCI_HT_PRI_LCTR0, PCI_HT_PRI_SIZEOF - PCI_HT_PRI_LCTR0);
588   lctr0 = get_conf_word(d, where + PCI_HT_PRI_LCTR0);
589   printf("\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",
590          FLAG(lctr0, PCI_HT_LCTR_CFLE),
591          FLAG(lctr0, PCI_HT_LCTR_CST),
592          FLAG(lctr0, PCI_HT_LCTR_CFE),
593          FLAG(lctr0, PCI_HT_LCTR_LKFAIL),
594          FLAG(lctr0, PCI_HT_LCTR_INIT),
595          FLAG(lctr0, PCI_HT_LCTR_EOC),
596          FLAG(lctr0, PCI_HT_LCTR_TXO),
597          (lctr0 & PCI_HT_LCTR_CRCERR) >> 8,
598          FLAG(lctr0, PCI_HT_LCTR_ISOCEN),
599          FLAG(lctr0, PCI_HT_LCTR_LSEN),
600          FLAG(lctr0, PCI_HT_LCTR_EXTCTL),
601          FLAG(lctr0, PCI_HT_LCTR_64B));
602   lcnf0 = get_conf_word(d, where + PCI_HT_PRI_LCNF0);
603   printf("\t\tLink Config 0: MLWI=%s DwFcIn%c MLWO=%s DwFcOut%c LWI=%s DwFcInEn%c LWO=%s DwFcOutEn%c\n",
604          ht_link_width(lcnf0 & PCI_HT_LCNF_MLWI),
605          FLAG(lcnf0, PCI_HT_LCNF_DFI),
606          ht_link_width((lcnf0 & PCI_HT_LCNF_MLWO) >> 4),
607          FLAG(lcnf0, PCI_HT_LCNF_DFO),
608          ht_link_width((lcnf0 & PCI_HT_LCNF_LWI) >> 8),
609          FLAG(lcnf0, PCI_HT_LCNF_DFIE),
610          ht_link_width((lcnf0 & PCI_HT_LCNF_LWO) >> 12),
611          FLAG(lcnf0, PCI_HT_LCNF_DFOE));
612   lctr1 = get_conf_word(d, where + PCI_HT_PRI_LCTR1);
613   printf("\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",
614          FLAG(lctr1, PCI_HT_LCTR_CFLE),
615          FLAG(lctr1, PCI_HT_LCTR_CST),
616          FLAG(lctr1, PCI_HT_LCTR_CFE),
617          FLAG(lctr1, PCI_HT_LCTR_LKFAIL),
618          FLAG(lctr1, PCI_HT_LCTR_INIT),
619          FLAG(lctr1, PCI_HT_LCTR_EOC),
620          FLAG(lctr1, PCI_HT_LCTR_TXO),
621          (lctr1 & PCI_HT_LCTR_CRCERR) >> 8,
622          FLAG(lctr1, PCI_HT_LCTR_ISOCEN),
623          FLAG(lctr1, PCI_HT_LCTR_LSEN),
624          FLAG(lctr1, PCI_HT_LCTR_EXTCTL),
625          FLAG(lctr1, PCI_HT_LCTR_64B));
626   lcnf1 = get_conf_word(d, where + PCI_HT_PRI_LCNF1);
627   printf("\t\tLink Config 1: MLWI=%s DwFcIn%c MLWO=%s DwFcOut%c LWI=%s DwFcInEn%c LWO=%s DwFcOutEn%c\n",
628          ht_link_width(lcnf1 & PCI_HT_LCNF_MLWI),
629          FLAG(lcnf1, PCI_HT_LCNF_DFI),
630          ht_link_width((lcnf1 & PCI_HT_LCNF_MLWO) >> 4),
631          FLAG(lcnf1, PCI_HT_LCNF_DFO),
632          ht_link_width((lcnf1 & PCI_HT_LCNF_LWI) >> 8),
633          FLAG(lcnf1, PCI_HT_LCNF_DFIE),
634          ht_link_width((lcnf1 & PCI_HT_LCNF_LWO) >> 12),
635          FLAG(lcnf1, PCI_HT_LCNF_DFOE));
636   rid = get_conf_byte(d, where + PCI_HT_PRI_RID);
637   printf("\t\tRevision ID: %u.%02u\n",
638          (rid & PCI_HT_RID_MAJ) >> 5, (rid & PCI_HT_RID_MIN));
639   lfrer0 = get_conf_byte(d, where + PCI_HT_PRI_LFRER0);
640   printf("\t\tLink Frequency 0: %s\n", ht_link_freq(lfrer0 & PCI_HT_LFRER_FREQ));
641   printf("\t\tLink Error 0: <Prot%c <Ovfl%c <EOC%c CTLTm%c\n",
642          FLAG(lfrer0, PCI_HT_LFRER_PROT),
643          FLAG(lfrer0, PCI_HT_LFRER_OV),
644          FLAG(lfrer0, PCI_HT_LFRER_EOC),
645          FLAG(lfrer0, PCI_HT_LFRER_CTLT));
646   lfcap0 = get_conf_byte(d, where + PCI_HT_PRI_LFCAP0);
647   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",
648          FLAG(lfcap0, PCI_HT_LFCAP_200),
649          FLAG(lfcap0, PCI_HT_LFCAP_300),
650          FLAG(lfcap0, PCI_HT_LFCAP_400),
651          FLAG(lfcap0, PCI_HT_LFCAP_500),
652          FLAG(lfcap0, PCI_HT_LFCAP_600),
653          FLAG(lfcap0, PCI_HT_LFCAP_800),
654          FLAG(lfcap0, PCI_HT_LFCAP_1000),
655          FLAG(lfcap0, PCI_HT_LFCAP_1200),
656          FLAG(lfcap0, PCI_HT_LFCAP_1400),
657          FLAG(lfcap0, PCI_HT_LFCAP_1600),
658          FLAG(lfcap0, PCI_HT_LFCAP_VEND));
659   ftr = get_conf_byte(d, where + PCI_HT_PRI_FTR);
660   printf("\t\tFeature Capability: IsocFC%c LDTSTOP%c CRCTM%c ECTLT%c 64bA%c UIDRD%c\n",
661          FLAG(ftr, PCI_HT_FTR_ISOCFC),
662          FLAG(ftr, PCI_HT_FTR_LDTSTOP),
663          FLAG(ftr, PCI_HT_FTR_CRCTM),
664          FLAG(ftr, PCI_HT_FTR_ECTLT),
665          FLAG(ftr, PCI_HT_FTR_64BA),
666          FLAG(ftr, PCI_HT_FTR_UIDRD));
667   lfrer1 = get_conf_byte(d, where + PCI_HT_PRI_LFRER1);
668   printf("\t\tLink Frequency 1: %s\n", ht_link_freq(lfrer1 & PCI_HT_LFRER_FREQ));
669   printf("\t\tLink Error 1: <Prot%c <Ovfl%c <EOC%c CTLTm%c\n",
670          FLAG(lfrer1, PCI_HT_LFRER_PROT),
671          FLAG(lfrer1, PCI_HT_LFRER_OV),
672          FLAG(lfrer1, PCI_HT_LFRER_EOC),
673          FLAG(lfrer1, PCI_HT_LFRER_CTLT));
674   lfcap1 = get_conf_byte(d, where + PCI_HT_PRI_LFCAP1);
675   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",
676          FLAG(lfcap1, PCI_HT_LFCAP_200),
677          FLAG(lfcap1, PCI_HT_LFCAP_300),
678          FLAG(lfcap1, PCI_HT_LFCAP_400),
679          FLAG(lfcap1, PCI_HT_LFCAP_500),
680          FLAG(lfcap1, PCI_HT_LFCAP_600),
681          FLAG(lfcap1, PCI_HT_LFCAP_800),
682          FLAG(lfcap1, PCI_HT_LFCAP_1000),
683          FLAG(lfcap1, PCI_HT_LFCAP_1200),
684          FLAG(lfcap1, PCI_HT_LFCAP_1400),
685          FLAG(lfcap1, PCI_HT_LFCAP_1600),
686          FLAG(lfcap1, PCI_HT_LFCAP_VEND));
687   eh = get_conf_word(d, where + PCI_HT_PRI_EH);
688   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",
689          FLAG(eh, PCI_HT_EH_PFLE),
690          FLAG(eh, PCI_HT_EH_OFLE),
691          FLAG(eh, PCI_HT_EH_PFE),
692          FLAG(eh, PCI_HT_EH_OFE),
693          FLAG(eh, PCI_HT_EH_EOCFE),
694          FLAG(eh, PCI_HT_EH_RFE),
695          FLAG(eh, PCI_HT_EH_CRCFE),
696          FLAG(eh, PCI_HT_EH_SERRFE),
697          FLAG(eh, PCI_HT_EH_CF),
698          FLAG(eh, PCI_HT_EH_RE),
699          FLAG(eh, PCI_HT_EH_PNFE),
700          FLAG(eh, PCI_HT_EH_ONFE),
701          FLAG(eh, PCI_HT_EH_EOCNFE),
702          FLAG(eh, PCI_HT_EH_RNFE),
703          FLAG(eh, PCI_HT_EH_CRCNFE),
704          FLAG(eh, PCI_HT_EH_SERRNFE));
705   mbu = get_conf_byte(d, where + PCI_HT_PRI_MBU);
706   mlu = get_conf_byte(d, where + PCI_HT_PRI_MLU);
707   printf("\t\tPrefetchable memory behind bridge Upper: %02x-%02x\n", mbu, mlu);
708   bn = get_conf_byte(d, where + PCI_HT_PRI_BN);
709   printf("\t\tBus Number: %02x\n", bn);
710 }
711
712 static void
713 show_ht_sec(struct device *d, int where, int cmd)
714 {
715   u16 lctr, lcnf, ftr, eh;
716   u8 rid, lfrer, lfcap, mbu, mlu;
717
718   printf("HyperTransport: Host or Secondary Interface\n");
719   if (verbose < 2)
720     return;
721
722   printf("\t\tCommand: WarmRst%c DblEnd%c DevNum=%u ChainSide%c HostHide%c Slave%c <EOCErr%c DUL%c\n",
723          FLAG(cmd, PCI_HT_SEC_CMD_WR),
724          FLAG(cmd, PCI_HT_SEC_CMD_DE),
725          (cmd & PCI_HT_SEC_CMD_DN) >> 2,
726          FLAG(cmd, PCI_HT_SEC_CMD_CS),
727          FLAG(cmd, PCI_HT_SEC_CMD_HH),
728          FLAG(cmd, PCI_HT_SEC_CMD_AS),
729          FLAG(cmd, PCI_HT_SEC_CMD_HIECE),
730          FLAG(cmd, PCI_HT_SEC_CMD_DUL));
731   config_fetch(d, where + PCI_HT_SEC_LCTR, PCI_HT_SEC_SIZEOF - PCI_HT_SEC_LCTR);
732   lctr = get_conf_word(d, where + PCI_HT_SEC_LCTR);
733   printf("\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",
734          FLAG(lctr, PCI_HT_LCTR_CFLE),
735          FLAG(lctr, PCI_HT_LCTR_CST),
736          FLAG(lctr, PCI_HT_LCTR_CFE),
737          FLAG(lctr, PCI_HT_LCTR_LKFAIL),
738          FLAG(lctr, PCI_HT_LCTR_INIT),
739          FLAG(lctr, PCI_HT_LCTR_EOC),
740          FLAG(lctr, PCI_HT_LCTR_TXO),
741          (lctr & PCI_HT_LCTR_CRCERR) >> 8,
742          FLAG(lctr, PCI_HT_LCTR_ISOCEN),
743          FLAG(lctr, PCI_HT_LCTR_LSEN),
744          FLAG(lctr, PCI_HT_LCTR_EXTCTL),
745          FLAG(lctr, PCI_HT_LCTR_64B));
746   lcnf = get_conf_word(d, where + PCI_HT_SEC_LCNF);
747   printf("\t\tLink Config: MLWI=%s DwFcIn%c MLWO=%s DwFcOut%c LWI=%s DwFcInEn%c LWO=%s DwFcOutEn%c\n",
748          ht_link_width(lcnf & PCI_HT_LCNF_MLWI),
749          FLAG(lcnf, PCI_HT_LCNF_DFI),
750          ht_link_width((lcnf & PCI_HT_LCNF_MLWO) >> 4),
751          FLAG(lcnf, PCI_HT_LCNF_DFO),
752          ht_link_width((lcnf & PCI_HT_LCNF_LWI) >> 8),
753          FLAG(lcnf, PCI_HT_LCNF_DFIE),
754          ht_link_width((lcnf & PCI_HT_LCNF_LWO) >> 12),
755          FLAG(lcnf, PCI_HT_LCNF_DFOE));
756   rid = get_conf_byte(d, where + PCI_HT_SEC_RID);
757   printf("\t\tRevision ID: %u.%02u\n",
758          (rid & PCI_HT_RID_MAJ) >> 5, (rid & PCI_HT_RID_MIN));
759   lfrer = get_conf_byte(d, where + PCI_HT_SEC_LFRER);
760   printf("\t\tLink Frequency: %s\n", ht_link_freq(lfrer & PCI_HT_LFRER_FREQ));
761   printf("\t\tLink Error: <Prot%c <Ovfl%c <EOC%c CTLTm%c\n",
762          FLAG(lfrer, PCI_HT_LFRER_PROT),
763          FLAG(lfrer, PCI_HT_LFRER_OV),
764          FLAG(lfrer, PCI_HT_LFRER_EOC),
765          FLAG(lfrer, PCI_HT_LFRER_CTLT));
766   lfcap = get_conf_byte(d, where + PCI_HT_SEC_LFCAP);
767   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",
768          FLAG(lfcap, PCI_HT_LFCAP_200),
769          FLAG(lfcap, PCI_HT_LFCAP_300),
770          FLAG(lfcap, PCI_HT_LFCAP_400),
771          FLAG(lfcap, PCI_HT_LFCAP_500),
772          FLAG(lfcap, PCI_HT_LFCAP_600),
773          FLAG(lfcap, PCI_HT_LFCAP_800),
774          FLAG(lfcap, PCI_HT_LFCAP_1000),
775          FLAG(lfcap, PCI_HT_LFCAP_1200),
776          FLAG(lfcap, PCI_HT_LFCAP_1400),
777          FLAG(lfcap, PCI_HT_LFCAP_1600),
778          FLAG(lfcap, PCI_HT_LFCAP_VEND));
779   ftr = get_conf_word(d, where + PCI_HT_SEC_FTR);
780   printf("\t\tFeature Capability: IsocFC%c LDTSTOP%c CRCTM%c ECTLT%c 64bA%c UIDRD%c ExtRS%c UCnfE%c\n",
781          FLAG(ftr, PCI_HT_FTR_ISOCFC),
782          FLAG(ftr, PCI_HT_FTR_LDTSTOP),
783          FLAG(ftr, PCI_HT_FTR_CRCTM),
784          FLAG(ftr, PCI_HT_FTR_ECTLT),
785          FLAG(ftr, PCI_HT_FTR_64BA),
786          FLAG(ftr, PCI_HT_FTR_UIDRD),
787          FLAG(ftr, PCI_HT_SEC_FTR_EXTRS),
788          FLAG(ftr, PCI_HT_SEC_FTR_UCNFE));
789   if (ftr & PCI_HT_SEC_FTR_EXTRS)
790     {
791       eh = get_conf_word(d, where + PCI_HT_SEC_EH);
792       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",
793              FLAG(eh, PCI_HT_EH_PFLE),
794              FLAG(eh, PCI_HT_EH_OFLE),
795              FLAG(eh, PCI_HT_EH_PFE),
796              FLAG(eh, PCI_HT_EH_OFE),
797              FLAG(eh, PCI_HT_EH_EOCFE),
798              FLAG(eh, PCI_HT_EH_RFE),
799              FLAG(eh, PCI_HT_EH_CRCFE),
800              FLAG(eh, PCI_HT_EH_SERRFE),
801              FLAG(eh, PCI_HT_EH_CF),
802              FLAG(eh, PCI_HT_EH_RE),
803              FLAG(eh, PCI_HT_EH_PNFE),
804              FLAG(eh, PCI_HT_EH_ONFE),
805              FLAG(eh, PCI_HT_EH_EOCNFE),
806              FLAG(eh, PCI_HT_EH_RNFE),
807              FLAG(eh, PCI_HT_EH_CRCNFE),
808              FLAG(eh, PCI_HT_EH_SERRNFE));
809       mbu = get_conf_byte(d, where + PCI_HT_SEC_MBU);
810       mlu = get_conf_byte(d, where + PCI_HT_SEC_MLU);
811       printf("\t\tPrefetchable memory behind bridge Upper: %02x-%02x\n", mbu, mlu);
812     }
813 }
814
815 static void
816 show_ht(struct device *d, int where, int cmd)
817 {
818   int type;
819
820   switch (cmd & PCI_HT_CMD_TYP_HI)
821     {
822     case PCI_HT_CMD_TYP_HI_PRI:
823       show_ht_pri(d, where, cmd);
824       return;
825     case PCI_HT_CMD_TYP_HI_SEC:
826       show_ht_sec(d, where, cmd);
827       return;
828     }
829
830   type = cmd & PCI_HT_CMD_TYP;
831   switch (type)
832     {
833     case PCI_HT_CMD_TYP_SW:
834       printf("HyperTransport: Switch\n");
835       break;
836     case PCI_HT_CMD_TYP_IDC:
837       printf("HyperTransport: Interrupt Discovery and Configuration\n");
838       break;
839     case PCI_HT_CMD_TYP_RID:
840       printf("HyperTransport: Revision ID: %u.%02u\n",
841              (cmd & PCI_HT_RID_MAJ) >> 5, (cmd & PCI_HT_RID_MIN));
842       break;
843     case PCI_HT_CMD_TYP_UIDC:
844       printf("HyperTransport: UnitID Clumping\n");
845       break;
846     case PCI_HT_CMD_TYP_ECSA:
847       printf("HyperTransport: Extended Configuration Space Access\n");
848       break;
849     case PCI_HT_CMD_TYP_AM:
850       printf("HyperTransport: Address Mapping\n");
851       break;
852     case PCI_HT_CMD_TYP_MSIM:
853       printf("HyperTransport: MSI Mapping\n");
854       break;
855     case PCI_HT_CMD_TYP_DR:
856       printf("HyperTransport: DirectRoute\n");
857       break;
858     case PCI_HT_CMD_TYP_VCS:
859       printf("HyperTransport: VCSet\n");
860       break;
861     case PCI_HT_CMD_TYP_RM:
862       printf("HyperTransport: Retry Mode\n");
863       break;
864     case PCI_HT_CMD_TYP_X86:
865       printf("HyperTransport: X86 (reserved)\n");
866       break;
867     default:
868       printf("HyperTransport: #%02x\n", type >> 11);
869     }
870 }
871
872 static void
873 show_rom(struct device *d)
874 {
875   struct pci_dev *p = d->dev;
876   pciaddr_t rom = p->rom_base_addr;
877   pciaddr_t len = (p->known_fields & PCI_FILL_SIZES) ? p->rom_size : 0;
878
879   if (!rom && !len)
880     return;
881   printf("\tExpansion ROM at ");
882   if (rom & PCI_ROM_ADDRESS_MASK)
883     printf(PCIADDR_T_FMT, rom & PCI_ROM_ADDRESS_MASK);
884   else
885     printf("<unassigned>");
886   if (!(rom & PCI_ROM_ADDRESS_ENABLE))
887     printf(" [disabled]");
888   show_size(len);
889   putchar('\n');
890 }
891
892 static void
893 show_msi(struct device *d, int where, int cap)
894 {
895   int is64;
896   u32 t;
897   u16 w;
898
899   printf("Message Signalled Interrupts: 64bit%c Queue=%d/%d Enable%c\n",
900          FLAG(cap, PCI_MSI_FLAGS_64BIT),
901          (cap & PCI_MSI_FLAGS_QSIZE) >> 4,
902          (cap & PCI_MSI_FLAGS_QMASK) >> 1,
903          FLAG(cap, PCI_MSI_FLAGS_ENABLE));
904   if (verbose < 2)
905     return;
906   is64 = cap & PCI_MSI_FLAGS_64BIT;
907   if (!config_fetch(d, where + PCI_MSI_ADDRESS_LO, (is64 ? PCI_MSI_DATA_64 : PCI_MSI_DATA_32) + 2 - PCI_MSI_ADDRESS_LO))
908     return;
909   printf("\t\tAddress: ");
910   if (is64)
911     {
912       t = get_conf_long(d, where + PCI_MSI_ADDRESS_HI);
913       w = get_conf_word(d, where + PCI_MSI_DATA_64);
914       printf("%08x", t);
915     }
916   else
917     w = get_conf_word(d, where + PCI_MSI_DATA_32);
918   t = get_conf_long(d, where + PCI_MSI_ADDRESS_LO);
919   printf("%08x  Data: %04x\n", t, w);
920 }
921
922 static void
923 show_slotid(int cap)
924 {
925   int esr = cap & 0xff;
926   int chs = cap >> 8;
927
928   printf("Slot ID: %d slots, First%c, chassis %02x\n",
929          esr & PCI_SID_ESR_NSLOTS,
930          FLAG(esr, PCI_SID_ESR_FIC),
931          chs);
932 }
933
934 static void
935 show_caps(struct device *d)
936 {
937   if (get_conf_word(d, PCI_STATUS) & PCI_STATUS_CAP_LIST)
938     {
939       int where = get_conf_byte(d, PCI_CAPABILITY_LIST) & ~3;
940       while (where)
941         {
942           int id, next, cap;
943           printf("\tCapabilities: ");
944           if (!config_fetch(d, where, 4))
945             {
946               puts("<available only to root>");
947               break;
948             }
949           id = get_conf_byte(d, where + PCI_CAP_LIST_ID);
950           next = get_conf_byte(d, where + PCI_CAP_LIST_NEXT) & ~3;
951           cap = get_conf_word(d, where + PCI_CAP_FLAGS);
952           printf("[%02x] ", where);
953           if (id == 0xff)
954             {
955               printf("<chain broken>\n");
956               break;
957             }
958           switch (id)
959             {
960             case PCI_CAP_ID_PM:
961               show_pm(d, where, cap);
962               break;
963             case PCI_CAP_ID_AGP:
964               show_agp(d, where, cap);
965               break;
966             case PCI_CAP_ID_VPD:
967               printf("Vital Product Data\n");
968               break;
969             case PCI_CAP_ID_SLOTID:
970               show_slotid(cap);
971               break;
972             case PCI_CAP_ID_MSI:
973               show_msi(d, where, cap);
974               break;
975             case PCI_CAP_ID_PCIX:
976               show_pcix(d, where);
977               break;
978             case PCI_CAP_ID_HT:
979               show_ht(d, where, cap);
980               break;
981             default:
982               printf("#%02x [%04x]\n", id, cap);
983             }
984           where = next;
985         }
986     }
987 }
988
989 static void
990 show_htype0(struct device *d)
991 {
992   show_bases(d, 6);
993   show_rom(d);
994   show_caps(d);
995 }
996
997 static void
998 show_htype1(struct device *d)
999 {
1000   u32 io_base = get_conf_byte(d, PCI_IO_BASE);
1001   u32 io_limit = get_conf_byte(d, PCI_IO_LIMIT);
1002   u32 io_type = io_base & PCI_IO_RANGE_TYPE_MASK;
1003   u32 mem_base = get_conf_word(d, PCI_MEMORY_BASE);
1004   u32 mem_limit = get_conf_word(d, PCI_MEMORY_LIMIT);
1005   u32 mem_type = mem_base & PCI_MEMORY_RANGE_TYPE_MASK;
1006   u32 pref_base = get_conf_word(d, PCI_PREF_MEMORY_BASE);
1007   u32 pref_limit = get_conf_word(d, PCI_PREF_MEMORY_LIMIT);
1008   u32 pref_type = pref_base & PCI_PREF_RANGE_TYPE_MASK;
1009   word sec_stat = get_conf_word(d, PCI_SEC_STATUS);
1010   word brc = get_conf_word(d, PCI_BRIDGE_CONTROL);
1011   int verb = verbose > 2;
1012
1013   show_bases(d, 2);
1014   printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
1015          get_conf_byte(d, PCI_PRIMARY_BUS),
1016          get_conf_byte(d, PCI_SECONDARY_BUS),
1017          get_conf_byte(d, PCI_SUBORDINATE_BUS),
1018          get_conf_byte(d, PCI_SEC_LATENCY_TIMER));
1019
1020   if (io_type != (io_limit & PCI_IO_RANGE_TYPE_MASK) ||
1021       (io_type != PCI_IO_RANGE_TYPE_16 && io_type != PCI_IO_RANGE_TYPE_32))
1022     printf("\t!!! Unknown I/O range types %x/%x\n", io_base, io_limit);
1023   else
1024     {
1025       io_base = (io_base & PCI_IO_RANGE_MASK) << 8;
1026       io_limit = (io_limit & PCI_IO_RANGE_MASK) << 8;
1027       if (io_type == PCI_IO_RANGE_TYPE_32)
1028         {
1029           io_base |= (get_conf_word(d, PCI_IO_BASE_UPPER16) << 16);
1030           io_limit |= (get_conf_word(d, PCI_IO_LIMIT_UPPER16) << 16);
1031         }
1032       if (io_base <= io_limit || verb)
1033         printf("\tI/O behind bridge: %08x-%08x\n", io_base, io_limit+0xfff);
1034     }
1035
1036   if (mem_type != (mem_limit & PCI_MEMORY_RANGE_TYPE_MASK) ||
1037       mem_type)
1038     printf("\t!!! Unknown memory range types %x/%x\n", mem_base, mem_limit);
1039   else
1040     {
1041       mem_base = (mem_base & PCI_MEMORY_RANGE_MASK) << 16;
1042       mem_limit = (mem_limit & PCI_MEMORY_RANGE_MASK) << 16;
1043       if (mem_base <= mem_limit || verb)
1044         printf("\tMemory behind bridge: %08x-%08x\n", mem_base, mem_limit + 0xfffff);
1045     }
1046
1047   if (pref_type != (pref_limit & PCI_PREF_RANGE_TYPE_MASK) ||
1048       (pref_type != PCI_PREF_RANGE_TYPE_32 && pref_type != PCI_PREF_RANGE_TYPE_64))
1049     printf("\t!!! Unknown prefetchable memory range types %x/%x\n", pref_base, pref_limit);
1050   else
1051     {
1052       pref_base = (pref_base & PCI_PREF_RANGE_MASK) << 16;
1053       pref_limit = (pref_limit & PCI_PREF_RANGE_MASK) << 16;
1054       if (pref_base <= pref_limit || verb)
1055         {
1056           if (pref_type == PCI_PREF_RANGE_TYPE_32)
1057             printf("\tPrefetchable memory behind bridge: %08x-%08x\n", pref_base, pref_limit + 0xfffff);
1058           else
1059             printf("\tPrefetchable memory behind bridge: %08x%08x-%08x%08x\n",
1060                    get_conf_long(d, PCI_PREF_BASE_UPPER32),
1061                    pref_base,
1062                    get_conf_long(d, PCI_PREF_LIMIT_UPPER32),
1063                    pref_limit);
1064         }
1065     }
1066
1067   if (verbose > 1)
1068     printf("\tSecondary status: 66Mhz%c FastB2B%c ParErr%c DEVSEL=%s >TAbort%c <TAbort%c <MAbort%c <SERR%c <PERR%c\n",
1069              FLAG(sec_stat, PCI_STATUS_66MHZ),
1070              FLAG(sec_stat, PCI_STATUS_FAST_BACK),
1071              FLAG(sec_stat, PCI_STATUS_PARITY),
1072              ((sec_stat & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
1073              ((sec_stat & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
1074              ((sec_stat & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??",
1075              FLAG(sec_stat, PCI_STATUS_SIG_TARGET_ABORT),
1076              FLAG(sec_stat, PCI_STATUS_REC_TARGET_ABORT),
1077              FLAG(sec_stat, PCI_STATUS_REC_MASTER_ABORT),
1078              FLAG(sec_stat, PCI_STATUS_SIG_SYSTEM_ERROR),
1079              FLAG(sec_stat, PCI_STATUS_DETECTED_PARITY));
1080
1081   show_rom(d);
1082
1083   if (verbose > 1)
1084     printf("\tBridgeCtl: Parity%c SERR%c NoISA%c VGA%c MAbort%c >Reset%c FastB2B%c\n",
1085            FLAG(brc, PCI_BRIDGE_CTL_PARITY),
1086            FLAG(brc, PCI_BRIDGE_CTL_SERR),
1087            FLAG(brc, PCI_BRIDGE_CTL_NO_ISA),
1088            FLAG(brc, PCI_BRIDGE_CTL_VGA),
1089            FLAG(brc, PCI_BRIDGE_CTL_MASTER_ABORT),
1090            FLAG(brc, PCI_BRIDGE_CTL_BUS_RESET),
1091            FLAG(brc, PCI_BRIDGE_CTL_FAST_BACK));
1092
1093   show_caps(d);
1094 }
1095
1096 static void
1097 show_htype2(struct device *d)
1098 {
1099   int i;
1100   word cmd = get_conf_word(d, PCI_COMMAND);
1101   word brc = get_conf_word(d, PCI_CB_BRIDGE_CONTROL);
1102   word exca = get_conf_word(d, PCI_CB_LEGACY_MODE_BASE);
1103   int verb = verbose > 2;
1104
1105   show_bases(d, 1);
1106   printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
1107          get_conf_byte(d, PCI_CB_PRIMARY_BUS),
1108          get_conf_byte(d, PCI_CB_CARD_BUS),
1109          get_conf_byte(d, PCI_CB_SUBORDINATE_BUS),
1110          get_conf_byte(d, PCI_CB_LATENCY_TIMER));
1111   for(i=0; i<2; i++)
1112     {
1113       int p = 8*i;
1114       u32 base = get_conf_long(d, PCI_CB_MEMORY_BASE_0 + p);
1115       u32 limit = get_conf_long(d, PCI_CB_MEMORY_LIMIT_0 + p);
1116       if (limit > base || verb)
1117         printf("\tMemory window %d: %08x-%08x%s%s\n", i, base, limit,
1118                (cmd & PCI_COMMAND_MEMORY) ? "" : " [disabled]",
1119                (brc & (PCI_CB_BRIDGE_CTL_PREFETCH_MEM0 << i)) ? " (prefetchable)" : "");
1120     }
1121   for(i=0; i<2; i++)
1122     {
1123       int p = 8*i;
1124       u32 base = get_conf_long(d, PCI_CB_IO_BASE_0 + p);
1125       u32 limit = get_conf_long(d, PCI_CB_IO_LIMIT_0 + p);
1126       if (!(base & PCI_IO_RANGE_TYPE_32))
1127         {
1128           base &= 0xffff;
1129           limit &= 0xffff;
1130         }
1131       base &= PCI_CB_IO_RANGE_MASK;
1132       limit = (limit & PCI_CB_IO_RANGE_MASK) + 3;
1133       if (base <= limit || verb)
1134         printf("\tI/O window %d: %08x-%08x%s\n", i, base, limit,
1135                (cmd & PCI_COMMAND_IO) ? "" : " [disabled]");
1136     }
1137
1138   if (get_conf_word(d, PCI_CB_SEC_STATUS) & PCI_STATUS_SIG_SYSTEM_ERROR)
1139     printf("\tSecondary status: SERR\n");
1140   if (verbose > 1)
1141     printf("\tBridgeCtl: Parity%c SERR%c ISA%c VGA%c MAbort%c >Reset%c 16bInt%c PostWrite%c\n",
1142            FLAG(brc, PCI_CB_BRIDGE_CTL_PARITY),
1143            FLAG(brc, PCI_CB_BRIDGE_CTL_SERR),
1144            FLAG(brc, PCI_CB_BRIDGE_CTL_ISA),
1145            FLAG(brc, PCI_CB_BRIDGE_CTL_VGA),
1146            FLAG(brc, PCI_CB_BRIDGE_CTL_MASTER_ABORT),
1147            FLAG(brc, PCI_CB_BRIDGE_CTL_CB_RESET),
1148            FLAG(brc, PCI_CB_BRIDGE_CTL_16BIT_INT),
1149            FLAG(brc, PCI_CB_BRIDGE_CTL_POST_WRITES));
1150   if (exca)
1151     printf("\t16-bit legacy interface ports at %04x\n", exca);
1152 }
1153
1154 static void
1155 show_verbose(struct device *d)
1156 {
1157   struct pci_dev *p = d->dev;
1158   word status = get_conf_word(d, PCI_STATUS);
1159   word cmd = get_conf_word(d, PCI_COMMAND);
1160   word class = get_conf_word(d, PCI_CLASS_DEVICE);
1161   byte bist = get_conf_byte(d, PCI_BIST);
1162   byte htype = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
1163   byte latency = get_conf_byte(d, PCI_LATENCY_TIMER);
1164   byte cache_line = get_conf_byte(d, PCI_CACHE_LINE_SIZE);
1165   byte max_lat, min_gnt;
1166   byte int_pin = get_conf_byte(d, PCI_INTERRUPT_PIN);
1167   unsigned int irq = p->irq;
1168   word subsys_v, subsys_d;
1169   char ssnamebuf[256];
1170
1171   show_terse(d);
1172
1173   switch (htype)
1174     {
1175     case PCI_HEADER_TYPE_NORMAL:
1176       if (class == PCI_CLASS_BRIDGE_PCI)
1177         printf("\t!!! Invalid class %04x for header type %02x\n", class, htype);
1178       max_lat = get_conf_byte(d, PCI_MAX_LAT);
1179       min_gnt = get_conf_byte(d, PCI_MIN_GNT);
1180       subsys_v = get_conf_word(d, PCI_SUBSYSTEM_VENDOR_ID);
1181       subsys_d = get_conf_word(d, PCI_SUBSYSTEM_ID);
1182       break;
1183     case PCI_HEADER_TYPE_BRIDGE:
1184       if ((class >> 8) != PCI_BASE_CLASS_BRIDGE)
1185         printf("\t!!! Invalid class %04x for header type %02x\n", class, htype);
1186       irq = int_pin = min_gnt = max_lat = 0;
1187       subsys_v = subsys_d = 0;
1188       break;
1189     case PCI_HEADER_TYPE_CARDBUS:
1190       if ((class >> 8) != PCI_BASE_CLASS_BRIDGE)
1191         printf("\t!!! Invalid class %04x for header type %02x\n", class, htype);
1192       min_gnt = max_lat = 0;
1193       subsys_v = get_conf_word(d, PCI_CB_SUBSYSTEM_VENDOR_ID);
1194       subsys_d = get_conf_word(d, PCI_CB_SUBSYSTEM_ID);
1195       break;
1196     default:
1197       printf("\t!!! Unknown header type %02x\n", htype);
1198       return;
1199     }
1200
1201   if (subsys_v && subsys_v != 0xffff)
1202     printf("\tSubsystem: %s\n",
1203            pci_lookup_name(pacc, ssnamebuf, sizeof(ssnamebuf),
1204                            PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
1205                            p->vendor_id, p->device_id, subsys_v, subsys_d));
1206
1207   if (verbose > 1)
1208     {
1209       printf("\tControl: I/O%c Mem%c BusMaster%c SpecCycle%c MemWINV%c VGASnoop%c ParErr%c Stepping%c SERR%c FastB2B%c\n",
1210              FLAG(cmd, PCI_COMMAND_IO),
1211              FLAG(cmd, PCI_COMMAND_MEMORY),
1212              FLAG(cmd, PCI_COMMAND_MASTER),
1213              FLAG(cmd, PCI_COMMAND_SPECIAL),
1214              FLAG(cmd, PCI_COMMAND_INVALIDATE),
1215              FLAG(cmd, PCI_COMMAND_VGA_PALETTE),
1216              FLAG(cmd, PCI_COMMAND_PARITY),
1217              FLAG(cmd, PCI_COMMAND_WAIT),
1218              FLAG(cmd, PCI_COMMAND_SERR),
1219              FLAG(cmd, PCI_COMMAND_FAST_BACK));
1220       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",
1221              FLAG(status, PCI_STATUS_CAP_LIST),
1222              FLAG(status, PCI_STATUS_66MHZ),
1223              FLAG(status, PCI_STATUS_UDF),
1224              FLAG(status, PCI_STATUS_FAST_BACK),
1225              FLAG(status, PCI_STATUS_PARITY),
1226              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
1227              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
1228              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??",
1229              FLAG(status, PCI_STATUS_SIG_TARGET_ABORT),
1230              FLAG(status, PCI_STATUS_REC_TARGET_ABORT),
1231              FLAG(status, PCI_STATUS_REC_MASTER_ABORT),
1232              FLAG(status, PCI_STATUS_SIG_SYSTEM_ERROR),
1233              FLAG(status, PCI_STATUS_DETECTED_PARITY));
1234       if (cmd & PCI_COMMAND_MASTER)
1235         {
1236           printf("\tLatency: %d", latency);
1237           if (min_gnt || max_lat)
1238             {
1239               printf(" (");
1240               if (min_gnt)
1241                 printf("%dns min", min_gnt*250);
1242               if (min_gnt && max_lat)
1243                 printf(", ");
1244               if (max_lat)
1245                 printf("%dns max", max_lat*250);
1246               putchar(')');
1247             }
1248           if (cache_line)
1249             printf(", Cache Line Size %02x", cache_line);
1250           putchar('\n');
1251         }
1252       if (int_pin || irq)
1253         printf("\tInterrupt: pin %c routed to IRQ " PCIIRQ_FMT "\n",
1254                (int_pin ? 'A' + int_pin - 1 : '?'), irq);
1255     }
1256   else
1257     {
1258       printf("\tFlags: ");
1259       if (cmd & PCI_COMMAND_MASTER)
1260         printf("bus master, ");
1261       if (cmd & PCI_COMMAND_VGA_PALETTE)
1262         printf("VGA palette snoop, ");
1263       if (cmd & PCI_COMMAND_WAIT)
1264         printf("stepping, ");
1265       if (cmd & PCI_COMMAND_FAST_BACK)
1266         printf("fast Back2Back, ");
1267       if (status & PCI_STATUS_66MHZ)
1268         printf("66Mhz, ");
1269       if (status & PCI_STATUS_UDF)
1270         printf("user-definable features, ");
1271       printf("%s devsel",
1272              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
1273              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
1274              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??");
1275       if (cmd & PCI_COMMAND_MASTER)
1276         printf(", latency %d", latency);
1277       if (irq)
1278         printf(", IRQ " PCIIRQ_FMT, irq);
1279       putchar('\n');
1280     }
1281
1282   if (bist & PCI_BIST_CAPABLE)
1283     {
1284       if (bist & PCI_BIST_START)
1285         printf("\tBIST is running\n");
1286       else
1287         printf("\tBIST result: %02x\n", bist & PCI_BIST_CODE_MASK);
1288     }
1289
1290   switch (htype)
1291     {
1292     case PCI_HEADER_TYPE_NORMAL:
1293       show_htype0(d);
1294       break;
1295     case PCI_HEADER_TYPE_BRIDGE:
1296       show_htype1(d);
1297       break;
1298     case PCI_HEADER_TYPE_CARDBUS:
1299       show_htype2(d);
1300       break;
1301     }
1302 }
1303
1304 static void
1305 show_hex_dump(struct device *d)
1306 {
1307   unsigned int i;
1308
1309   for(i=0; i<d->config_cnt; i++)
1310     {
1311       if (! (i & 15))
1312         printf("%02x:", i);
1313       printf(" %02x", get_conf_byte(d, i));
1314       if ((i & 15) == 15)
1315         putchar('\n');
1316     }
1317 }
1318
1319 static void
1320 show_machine(struct device *d)
1321 {
1322   struct pci_dev *p = d->dev;
1323   int c;
1324   word sv_id=0, sd_id=0;
1325   char classbuf[128], vendbuf[128], devbuf[128], svbuf[128], sdbuf[128];
1326
1327   switch (get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f)
1328     {
1329     case PCI_HEADER_TYPE_NORMAL:
1330       sv_id = get_conf_word(d, PCI_SUBSYSTEM_VENDOR_ID);
1331       sd_id = get_conf_word(d, PCI_SUBSYSTEM_ID);
1332       break;
1333     case PCI_HEADER_TYPE_CARDBUS:
1334       sv_id = get_conf_word(d, PCI_CB_SUBSYSTEM_VENDOR_ID);
1335       sd_id = get_conf_word(d, PCI_CB_SUBSYSTEM_ID);
1336       break;
1337     }
1338
1339   if (verbose)
1340     {
1341       printf("Device:\t");
1342       show_slot_name(d);
1343       putchar('\n');
1344       printf("Class:\t%s\n",
1345              pci_lookup_name(pacc, classbuf, sizeof(classbuf), PCI_LOOKUP_CLASS, get_conf_word(d, PCI_CLASS_DEVICE), 0, 0, 0));
1346       printf("Vendor:\t%s\n",
1347              pci_lookup_name(pacc, vendbuf, sizeof(vendbuf), PCI_LOOKUP_VENDOR, p->vendor_id, p->device_id, 0, 0));
1348       printf("Device:\t%s\n",
1349              pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id, 0, 0));
1350       if (sv_id && sv_id != 0xffff)
1351         {
1352           printf("SVendor:\t%s\n",
1353                  pci_lookup_name(pacc, svbuf, sizeof(svbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR, p->vendor_id, p->device_id, sv_id, sd_id));
1354           printf("SDevice:\t%s\n",
1355                  pci_lookup_name(pacc, sdbuf, sizeof(sdbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id, sv_id, sd_id));
1356         }
1357       if (c = get_conf_byte(d, PCI_REVISION_ID))
1358         printf("Rev:\t%02x\n", c);
1359       if (c = get_conf_byte(d, PCI_CLASS_PROG))
1360         printf("ProgIf:\t%02x\n", c);
1361     }
1362   else
1363     {
1364       show_slot_name(d);
1365       printf(" \"%s\" \"%s\" \"%s\"",
1366              pci_lookup_name(pacc, classbuf, sizeof(classbuf), PCI_LOOKUP_CLASS,
1367                              get_conf_word(d, PCI_CLASS_DEVICE), 0, 0, 0),
1368              pci_lookup_name(pacc, vendbuf, sizeof(vendbuf), PCI_LOOKUP_VENDOR,
1369                              p->vendor_id, p->device_id, 0, 0),
1370              pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_DEVICE,
1371                              p->vendor_id, p->device_id, 0, 0));
1372       if (c = get_conf_byte(d, PCI_REVISION_ID))
1373         printf(" -r%02x", c);
1374       if (c = get_conf_byte(d, PCI_CLASS_PROG))
1375         printf(" -p%02x", c);
1376       if (sv_id && sv_id != 0xffff)
1377         printf(" \"%s\" \"%s\"",
1378                pci_lookup_name(pacc, svbuf, sizeof(svbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR, p->vendor_id, p->device_id, sv_id, sd_id),
1379                pci_lookup_name(pacc, sdbuf, sizeof(sdbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id, sv_id, sd_id));
1380       else
1381         printf(" \"\" \"\"");
1382       putchar('\n');
1383     }
1384 }
1385
1386 static void
1387 show_device(struct device *d)
1388 {
1389   if (machine_readable)
1390     show_machine(d);
1391   else if (verbose)
1392     show_verbose(d);
1393   else
1394     show_terse(d);
1395   if (show_hex)
1396     show_hex_dump(d);
1397   if (verbose || show_hex)
1398     putchar('\n');
1399 }
1400
1401 static void
1402 show(void)
1403 {
1404   struct device *d;
1405
1406   for(d=first_dev; d; d=d->next)
1407     show_device(d);
1408 }
1409
1410 /* Tree output */
1411
1412 struct bridge {
1413   struct bridge *chain;                 /* Single-linked list of bridges */
1414   struct bridge *next, *child;          /* Tree of bridges */
1415   struct bus *first_bus;                /* List of busses connected to this bridge */
1416   unsigned int domain;
1417   unsigned int primary, secondary, subordinate; /* Bus numbers */
1418   struct device *br_dev;
1419 };
1420
1421 struct bus {
1422   unsigned int domain;
1423   unsigned int number;
1424   struct bus *sibling;
1425   struct device *first_dev, **last_dev;
1426 };
1427
1428 static struct bridge host_bridge = { NULL, NULL, NULL, NULL, 0, ~0, 0, ~0, NULL };
1429
1430 static struct bus *
1431 find_bus(struct bridge *b, unsigned int domain, unsigned int n)
1432 {
1433   struct bus *bus;
1434
1435   for(bus=b->first_bus; bus; bus=bus->sibling)
1436     if (bus->domain == domain && bus->number == n)
1437       break;
1438   return bus;
1439 }
1440
1441 static struct bus *
1442 new_bus(struct bridge *b, unsigned int domain, unsigned int n)
1443 {
1444   struct bus *bus = xmalloc(sizeof(struct bus));
1445
1446   bus = xmalloc(sizeof(struct bus));
1447   bus->domain = domain;
1448   bus->number = n;
1449   bus->sibling = b->first_bus;
1450   bus->first_dev = NULL;
1451   bus->last_dev = &bus->first_dev;
1452   b->first_bus = bus;
1453   return bus;
1454 }
1455
1456 static void
1457 insert_dev(struct device *d, struct bridge *b)
1458 {
1459   struct pci_dev *p = d->dev;
1460   struct bus *bus;
1461
1462   if (! (bus = find_bus(b, p->domain, p->bus)))
1463     {
1464       struct bridge *c;
1465       for(c=b->child; c; c=c->next)
1466         if (c->domain == p->domain && c->secondary <= p->bus && p->bus <= c->subordinate)
1467           {
1468             insert_dev(d, c);
1469             return;
1470           }
1471       bus = new_bus(b, p->domain, p->bus);
1472     }
1473   /* Simple insertion at the end _does_ guarantee the correct order as the
1474    * original device list was sorted by (domain, bus, devfn) lexicographically
1475    * and all devices on the new list have the same bus number.
1476    */
1477   *bus->last_dev = d;
1478   bus->last_dev = &d->next;
1479   d->next = NULL;
1480 }
1481
1482 static void
1483 grow_tree(void)
1484 {
1485   struct device *d, *d2;
1486   struct bridge **last_br, *b;
1487
1488   /* Build list of bridges */
1489
1490   last_br = &host_bridge.chain;
1491   for(d=first_dev; d; d=d->next)
1492     {
1493       word class = get_conf_word(d, PCI_CLASS_DEVICE);
1494       byte ht = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
1495       if (class == PCI_CLASS_BRIDGE_PCI &&
1496           (ht == PCI_HEADER_TYPE_BRIDGE || ht == PCI_HEADER_TYPE_CARDBUS))
1497         {
1498           b = xmalloc(sizeof(struct bridge));
1499           b->domain = d->dev->domain;
1500           if (ht == PCI_HEADER_TYPE_BRIDGE)
1501             {
1502               b->primary = get_conf_byte(d, PCI_CB_PRIMARY_BUS);
1503               b->secondary = get_conf_byte(d, PCI_CB_CARD_BUS);
1504               b->subordinate = get_conf_byte(d, PCI_CB_SUBORDINATE_BUS);
1505             }
1506           else
1507             {
1508               b->primary = get_conf_byte(d, PCI_PRIMARY_BUS);
1509               b->secondary = get_conf_byte(d, PCI_SECONDARY_BUS);
1510               b->subordinate = get_conf_byte(d, PCI_SUBORDINATE_BUS);
1511             }
1512           *last_br = b;
1513           last_br = &b->chain;
1514           b->next = b->child = NULL;
1515           b->first_bus = NULL;
1516           b->br_dev = d;
1517         }
1518     }
1519   *last_br = NULL;
1520
1521   /* Create a bridge tree */
1522
1523   for(b=&host_bridge; b; b=b->chain)
1524     {
1525       struct bridge *c, *best;
1526       best = NULL;
1527       for(c=&host_bridge; c; c=c->chain)
1528         if (c != b && (c == &host_bridge || b->domain == c->domain) &&
1529             b->primary >= c->secondary && b->primary <= c->subordinate &&
1530             (!best || best->subordinate - best->primary > c->subordinate - c->primary))
1531           best = c;
1532       if (best)
1533         {
1534           b->next = best->child;
1535           best->child = b;
1536         }
1537     }
1538
1539   /* Insert secondary bus for each bridge */
1540
1541   for(b=&host_bridge; b; b=b->chain)
1542     if (!find_bus(b, b->domain, b->secondary))
1543       new_bus(b, b->domain, b->secondary);
1544
1545   /* Create bus structs and link devices */
1546
1547   for(d=first_dev; d;)
1548     {
1549       d2 = d->next;
1550       insert_dev(d, &host_bridge);
1551       d = d2;
1552     }
1553 }
1554
1555 static void
1556 print_it(byte *line, byte *p)
1557 {
1558   *p++ = '\n';
1559   *p = 0;
1560   fputs(line, stdout);
1561   for(p=line; *p; p++)
1562     if (*p == '+' || *p == '|')
1563       *p = '|';
1564     else
1565       *p = ' ';
1566 }
1567
1568 static void show_tree_bridge(struct bridge *, byte *, byte *);
1569
1570 static void
1571 show_tree_dev(struct device *d, byte *line, byte *p)
1572 {
1573   struct pci_dev *q = d->dev;
1574   struct bridge *b;
1575   char namebuf[256];
1576
1577   p += sprintf(p, "%02x.%x", q->dev, q->func);
1578   for(b=&host_bridge; b; b=b->chain)
1579     if (b->br_dev == d)
1580       {
1581         if (b->secondary == b->subordinate)
1582           p += sprintf(p, "-[%04x:%02x]-", b->domain, b->secondary);
1583         else
1584           p += sprintf(p, "-[%04x:%02x-%02x]-", b->domain, b->secondary, b->subordinate);
1585         show_tree_bridge(b, line, p);
1586         return;
1587       }
1588   if (verbose)
1589     p += sprintf(p, "  %s",
1590                  pci_lookup_name(pacc, namebuf, sizeof(namebuf),
1591                                  PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
1592                                  q->vendor_id, q->device_id, 0, 0));
1593   print_it(line, p);
1594 }
1595
1596 static void
1597 show_tree_bus(struct bus *b, byte *line, byte *p)
1598 {
1599   if (!b->first_dev)
1600     print_it(line, p);
1601   else if (!b->first_dev->next)
1602     {
1603       *p++ = '-';
1604       *p++ = '-';
1605       show_tree_dev(b->first_dev, line, p);
1606     }
1607   else
1608     {
1609       struct device *d = b->first_dev;
1610       while (d->next)
1611         {
1612           p[0] = '+';
1613           p[1] = '-';
1614           show_tree_dev(d, line, p+2);
1615           d = d->next;
1616         }
1617       p[0] = '\\';
1618       p[1] = '-';
1619       show_tree_dev(d, line, p+2);
1620     }
1621 }
1622
1623 static void
1624 show_tree_bridge(struct bridge *b, byte *line, byte *p)
1625 {
1626   *p++ = '-';
1627   if (!b->first_bus->sibling)
1628     {
1629       if (b == &host_bridge)
1630         p += sprintf(p, "[%04x:%02x]-", b->domain, b->first_bus->number);
1631       show_tree_bus(b->first_bus, line, p);
1632     }
1633   else
1634     {
1635       struct bus *u = b->first_bus;
1636       byte *k;
1637
1638       while (u->sibling)
1639         {
1640           k = p + sprintf(p, "+-[%04x:%02x]-", u->domain, u->number);
1641           show_tree_bus(u, line, k);
1642           u = u->sibling;
1643         }
1644       k = p + sprintf(p, "\\-[%04x:%02x]-", u->domain, u->number);
1645       show_tree_bus(u, line, k);
1646     }
1647 }
1648
1649 static void
1650 show_forest(void)
1651 {
1652   char line[256];
1653
1654   grow_tree();
1655   show_tree_bridge(&host_bridge, line, line);
1656 }
1657
1658 /* Bus mapping mode */
1659
1660 struct bus_bridge {
1661   struct bus_bridge *next;
1662   byte this, dev, func, first, last, bug;
1663 };
1664
1665 struct bus_info {
1666   byte exists;
1667   byte guestbook;
1668   struct bus_bridge *bridges, *via;
1669 };
1670
1671 static struct bus_info *bus_info;
1672
1673 static void
1674 map_bridge(struct bus_info *bi, struct device *d, int np, int ns, int nl)
1675 {
1676   struct bus_bridge *b = xmalloc(sizeof(struct bus_bridge));
1677   struct pci_dev *p = d->dev;
1678
1679   b->next = bi->bridges;
1680   bi->bridges = b;
1681   b->this = get_conf_byte(d, np);
1682   b->dev = p->dev;
1683   b->func = p->func;
1684   b->first = get_conf_byte(d, ns);
1685   b->last = get_conf_byte(d, nl);
1686   printf("## %02x.%02x:%d is a bridge from %02x to %02x-%02x\n",
1687          p->bus, p->dev, p->func, b->this, b->first, b->last);
1688   if (b->this != p->bus)
1689     printf("!!! Bridge points to invalid primary bus.\n");
1690   if (b->first > b->last)
1691     {
1692       printf("!!! Bridge points to invalid bus range.\n");
1693       b->last = b->first;
1694     }
1695 }
1696
1697 static void
1698 do_map_bus(int bus)
1699 {
1700   int dev, func;
1701   int verbose = pacc->debugging;
1702   struct bus_info *bi = bus_info + bus;
1703   struct device *d;
1704
1705   if (verbose)
1706     printf("Mapping bus %02x\n", bus);
1707   for(dev = 0; dev < 32; dev++)
1708     if (filter.slot < 0 || filter.slot == dev)
1709       {
1710         int func_limit = 1;
1711         for(func = 0; func < func_limit; func++)
1712           if (filter.func < 0 || filter.func == func)
1713             {
1714               /* XXX: Bus mapping supports only domain 0 */
1715               struct pci_dev *p = pci_get_dev(pacc, 0, bus, dev, func);
1716               u16 vendor = pci_read_word(p, PCI_VENDOR_ID);
1717               if (vendor && vendor != 0xffff)
1718                 {
1719                   if (!func && (pci_read_byte(p, PCI_HEADER_TYPE) & 0x80))
1720                     func_limit = 8;
1721                   if (verbose)
1722                     printf("Discovered device %02x:%02x.%d\n", bus, dev, func);
1723                   bi->exists = 1;
1724                   if (d = scan_device(p))
1725                     {
1726                       show_device(d);
1727                       switch (get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f)
1728                         {
1729                         case PCI_HEADER_TYPE_BRIDGE:
1730                           map_bridge(bi, d, PCI_PRIMARY_BUS, PCI_SECONDARY_BUS, PCI_SUBORDINATE_BUS);
1731                           break;
1732                         case PCI_HEADER_TYPE_CARDBUS:
1733                           map_bridge(bi, d, PCI_CB_PRIMARY_BUS, PCI_CB_CARD_BUS, PCI_CB_SUBORDINATE_BUS);
1734                           break;
1735                         }
1736                       free(d);
1737                     }
1738                   else if (verbose)
1739                     printf("But it was filtered out.\n");
1740                 }
1741               pci_free_dev(p);
1742             }
1743       }
1744 }
1745
1746 static void
1747 do_map_bridges(int bus, int min, int max)
1748 {
1749   struct bus_info *bi = bus_info + bus;
1750   struct bus_bridge *b;
1751
1752   bi->guestbook = 1;
1753   for(b=bi->bridges; b; b=b->next)
1754     {
1755       if (bus_info[b->first].guestbook)
1756         b->bug = 1;
1757       else if (b->first < min || b->last > max)
1758         b->bug = 2;
1759       else
1760         {
1761           bus_info[b->first].via = b;
1762           do_map_bridges(b->first, b->first, b->last);
1763         }
1764     }
1765 }
1766
1767 static void
1768 map_bridges(void)
1769 {
1770   int i;
1771
1772   printf("\nSummary of buses:\n\n");
1773   for(i=0; i<256; i++)
1774     if (bus_info[i].exists && !bus_info[i].guestbook)
1775       do_map_bridges(i, 0, 255);
1776   for(i=0; i<256; i++)
1777     {
1778       struct bus_info *bi = bus_info + i;
1779       struct bus_bridge *b = bi->via;
1780
1781       if (bi->exists)
1782         {
1783           printf("%02x: ", i);
1784           if (b)
1785             printf("Entered via %02x:%02x.%d\n", b->this, b->dev, b->func);
1786           else if (!i)
1787             printf("Primary host bus\n");
1788           else
1789             printf("Secondary host bus (?)\n");
1790         }
1791       for(b=bi->bridges; b; b=b->next)
1792         {
1793           printf("\t%02x.%d Bridge to %02x-%02x", b->dev, b->func, b->first, b->last);
1794           switch (b->bug)
1795             {
1796             case 1:
1797               printf(" <overlap bug>");
1798               break;
1799             case 2:
1800               printf(" <crossing bug>");
1801               break;
1802             }
1803           putchar('\n');
1804         }
1805     }
1806 }
1807
1808 static void
1809 map_the_bus(void)
1810 {
1811   if (pacc->method == PCI_ACCESS_PROC_BUS_PCI ||
1812       pacc->method == PCI_ACCESS_DUMP)
1813     printf("WARNING: Bus mapping can be reliable only with direct hardware access enabled.\n\n");
1814   else if (!check_root())
1815     die("Only root can map the bus.");
1816   bus_info = xmalloc(sizeof(struct bus_info) * 256);
1817   bzero(bus_info, sizeof(struct bus_info) * 256);
1818   if (filter.bus >= 0)
1819     do_map_bus(filter.bus);
1820   else
1821     {
1822       int bus;
1823       for(bus=0; bus<256; bus++)
1824         do_map_bus(bus);
1825     }
1826   map_bridges();
1827 }
1828
1829 /* Main */
1830
1831 int
1832 main(int argc, char **argv)
1833 {
1834   int i;
1835   char *msg;
1836
1837   if (argc == 2 && !strcmp(argv[1], "--version"))
1838     {
1839       puts("lspci version " PCIUTILS_VERSION);
1840       return 0;
1841     }
1842
1843   pacc = pci_alloc();
1844   pacc->error = die;
1845   pci_filter_init(pacc, &filter);
1846
1847   while ((i = getopt(argc, argv, options)) != -1)
1848     switch (i)
1849       {
1850       case 'n':
1851         pacc->numeric_ids = 1;
1852         break;
1853       case 'v':
1854         verbose++;
1855         break;
1856       case 'b':
1857         pacc->buscentric = 1;
1858         buscentric_view = 1;
1859         break;
1860       case 's':
1861         if (msg = pci_filter_parse_slot(&filter, optarg))
1862           die("-s: %s", msg);
1863         break;
1864       case 'd':
1865         if (msg = pci_filter_parse_id(&filter, optarg))
1866           die("-d: %s", msg);
1867         break;
1868       case 'x':
1869         show_hex++;
1870         break;
1871       case 't':
1872         show_tree++;
1873         break;
1874       case 'i':
1875         pacc->id_file_name = optarg;
1876         break;
1877       case 'm':
1878         machine_readable++;
1879         break;
1880       case 'M':
1881         map_mode++;
1882         break;
1883       default:
1884         if (parse_generic_option(i, pacc, optarg))
1885           break;
1886       bad:
1887         fprintf(stderr, help_msg, pacc->id_file_name);
1888         return 1;
1889       }
1890   if (optind < argc)
1891     goto bad;
1892
1893   pci_init(pacc);
1894   if (map_mode)
1895     map_the_bus();
1896   else
1897     {
1898       scan_devices();
1899       sort_them();
1900       if (show_tree)
1901         show_forest();
1902       else
1903         show();
1904     }
1905   pci_cleanup(pacc);
1906
1907   return 0;
1908 }