]> mj.ucw.cz Git - pciutils.git/blob - lspci.c
14c2ffa6c7695aebd9f1cc189a1a7e9b9ea91ad8
[pciutils.git] / lspci.c
1 /*
2  *      Linux PCI Utilities -- List All PCI Devices
3  *
4  *      Copyright (c) 1997--2002 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 [[<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 /* Format strings used for IRQ numbers and memory addresses */
51
52 #ifdef ARCH_SPARC64
53 #define IRQ_FORMAT "%08x"
54 #else
55 #define IRQ_FORMAT "%d"
56 #endif
57
58 #ifdef HAVE_64BIT_ADDRESS
59 #ifdef HAVE_LONG_ADDRESS
60 #define ADDR_FORMAT "%016Lx"
61 #else
62 #define ADDR_FORMAT "%016lx"
63 #endif
64 #else
65 #define ADDR_FORMAT "%08lx"
66 #endif
67
68 #ifdef ARCH_SPARC64
69 #define IO_FORMAT "%016Lx"
70 #elif defined(HAVE_LONG_ADDRESS)
71 #define IO_FORMAT "%04Lx"
72 #else
73 #define IO_FORMAT "%04lx"
74 #endif
75
76 /*
77  *  If we aren't being compiled by GCC, use malloc() instead of alloca().
78  *  This increases our memory footprint, but only slightly since we don't
79  *  use alloca() much.
80  */
81
82 #ifndef __GNUC__
83 #define alloca malloc
84 #endif
85
86 /* Our view of the PCI bus */
87
88 struct device {
89   struct device *next;
90   struct pci_dev *dev;
91   unsigned int config_cnt;
92   byte config[256];
93 };
94
95 static struct device *first_dev;
96
97 static struct device *
98 scan_device(struct pci_dev *p)
99 {
100   int how_much = (show_hex > 2) ? 256 : 64;
101   struct device *d;
102
103   if (!pci_filter_match(&filter, p))
104     return NULL;
105   d = xmalloc(sizeof(struct device));
106   bzero(d, sizeof(*d));
107   d->dev = p;
108   if (!pci_read_block(p, 0, d->config, how_much))
109     die("Unable to read %d bytes of configuration space.", how_much);
110   if (how_much < 128 && (d->config[PCI_HEADER_TYPE] & 0x7f) == PCI_HEADER_TYPE_CARDBUS)
111     {
112       /* For cardbus bridges, we need to fetch 64 bytes more to get the full standard header... */
113       if (!pci_read_block(p, 64, d->config+64, 64))
114         die("Unable to read cardbus bridge extension data.");
115       how_much = 128;
116     }
117   d->config_cnt = how_much;
118   pci_setup_cache(p, d->config, d->config_cnt);
119   pci_fill_info(p, PCI_FILL_IDENT | PCI_FILL_IRQ | PCI_FILL_BASES | PCI_FILL_ROM_BASE | PCI_FILL_SIZES);
120   return d;
121 }
122
123 static void
124 scan_devices(void)
125 {
126   struct device *d;
127   struct pci_dev *p;
128
129   pci_scan_bus(pacc);
130   for(p=pacc->devices; p; p=p->next)
131     if (d = scan_device(p))
132       {
133         d->next = first_dev;
134         first_dev = d;
135       }
136 }
137
138 static int
139 check_root(void)
140 {
141   static int is_root = -1;
142
143   if (is_root < 0)
144     is_root = !geteuid();
145   return is_root;
146 }
147
148 static int
149 config_fetch(struct device *d, unsigned int pos, unsigned int len)
150 {
151   if (pos + len < d->config_cnt)
152     return 1;
153   if (pacc->method != PCI_ACCESS_DUMP && !check_root())
154     return 0;
155   return pci_read_block(d->dev, pos, d->config + pos, len);
156 }
157
158 /* Config space accesses */
159
160 static inline byte
161 get_conf_byte(struct device *d, unsigned int pos)
162 {
163   return d->config[pos];
164 }
165
166 static word
167 get_conf_word(struct device *d, unsigned int pos)
168 {
169   return d->config[pos] | (d->config[pos+1] << 8);
170 }
171
172 static u32
173 get_conf_long(struct device *d, unsigned int pos)
174 {
175   return d->config[pos] |
176     (d->config[pos+1] << 8) |
177     (d->config[pos+2] << 16) |
178     (d->config[pos+3] << 24);
179 }
180
181 /* Sorting */
182
183 static int
184 compare_them(const void *A, const void *B)
185 {
186   const struct pci_dev *a = (*(const struct device **)A)->dev;
187   const struct pci_dev *b = (*(const struct device **)B)->dev;
188
189   if (a->bus < b->bus)
190     return -1;
191   if (a->bus > b->bus)
192     return 1;
193   if (a->dev < b->dev)
194     return -1;
195   if (a->dev > b->dev)
196     return 1;
197   if (a->func < b->func)
198     return -1;
199   if (a->func > b->func)
200     return 1;
201   return 0;
202 }
203
204 static void
205 sort_them(void)
206 {
207   struct device **index, **h, **last_dev;
208   int cnt;
209   struct device *d;
210
211   cnt = 0;
212   for(d=first_dev; d; d=d->next)
213     cnt++;
214   h = index = alloca(sizeof(struct device *) * cnt);
215   for(d=first_dev; d; d=d->next)
216     *h++ = d;
217   qsort(index, cnt, sizeof(struct device *), compare_them);
218   last_dev = &first_dev;
219   h = index;
220   while (cnt--)
221     {
222       *last_dev = *h;
223       last_dev = &(*h)->next;
224       h++;
225     }
226   *last_dev = NULL;
227 }
228
229 /* Normal output */
230
231 #define FLAG(x,y) ((x & y) ? '+' : '-')
232
233 static void
234 show_terse(struct device *d)
235 {
236   int c;
237   struct pci_dev *p = d->dev;
238   byte classbuf[128], devbuf[128];
239
240   printf("%02x:%02x.%x %s: %s",
241          p->bus,
242          p->dev,
243          p->func,
244          pci_lookup_name(pacc, classbuf, sizeof(classbuf),
245                          PCI_LOOKUP_CLASS,
246                          get_conf_word(d, PCI_CLASS_DEVICE), 0, 0, 0),
247          pci_lookup_name(pacc, devbuf, sizeof(devbuf),
248                          PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
249                          p->vendor_id, p->device_id, 0, 0));
250   if (c = get_conf_byte(d, PCI_REVISION_ID))
251     printf(" (rev %02x)", c);
252   if (verbose)
253     {
254       char *x;
255       c = get_conf_byte(d, PCI_CLASS_PROG);
256       x = pci_lookup_name(pacc, devbuf, sizeof(devbuf),
257                           PCI_LOOKUP_PROGIF,
258                           get_conf_word(d, PCI_CLASS_DEVICE), c, 0, 0);
259       if (c || x)
260         {
261           printf(" (prog-if %02x", c);
262           if (x)
263             printf(" [%s]", x);
264           putchar(')');
265         }
266     }
267   putchar('\n');
268 }
269
270 static void
271 show_size(pciaddr_t x)
272 {
273   if (!x)
274     return;
275   printf(" [size=");
276   if (x < 1024)
277     printf("%d", (int) x);
278   else if (x < 1048576)
279     printf("%dK", (int)(x / 1024));
280   else if (x < 0x80000000)
281     printf("%dM", (int)(x / 1048576));
282   else
283     printf(ADDR_FORMAT, x);
284   putchar(']');
285 }
286
287 static void
288 show_bases(struct device *d, int cnt)
289 {
290   struct pci_dev *p = d->dev;
291   word cmd = get_conf_word(d, PCI_COMMAND);
292   int i;
293
294   for(i=0; i<cnt; i++)
295     {
296       pciaddr_t pos = p->base_addr[i];
297       pciaddr_t len = (p->known_fields & PCI_FILL_SIZES) ? p->size[i] : 0;
298       u32 flg = get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i);
299       if (flg == 0xffffffff)
300         flg = 0;
301       if (!pos && !flg && !len)
302         continue;
303       if (verbose > 1)
304         printf("\tRegion %d: ", i);
305       else
306         putchar('\t');
307       if (pos && !flg)                  /* Reported by the OS, but not by the device */
308         {
309           printf("[virtual] ");
310           flg = pos;
311         }
312       if (flg & PCI_BASE_ADDRESS_SPACE_IO)
313         {
314           pciaddr_t a = pos & PCI_BASE_ADDRESS_IO_MASK;
315           printf("I/O ports at ");
316           if (a)
317             printf(IO_FORMAT, a);
318           else if (flg & PCI_BASE_ADDRESS_IO_MASK)
319             printf("<ignored>");
320           else
321             printf("<unassigned>");
322           if (!(cmd & PCI_COMMAND_IO))
323             printf(" [disabled]");
324         }
325       else
326         {
327           int t = flg & PCI_BASE_ADDRESS_MEM_TYPE_MASK;
328           pciaddr_t a = pos & PCI_ADDR_MEM_MASK;
329           int done = 0;
330           u32 z = 0;
331
332           printf("Memory at ");
333           if (t == PCI_BASE_ADDRESS_MEM_TYPE_64)
334             {
335               if (i >= cnt - 1)
336                 {
337                   printf("<invalid-64bit-slot>");
338                   done = 1;
339                 }
340               else
341                 {
342                   i++;
343                   z = get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i);
344                   if (buscentric_view)
345                     {
346                       if (a || z)
347                         printf("%08x" ADDR_FORMAT, z, a);
348                       else
349                         printf("<unassigned>");
350                       done = 1;
351                     }
352                 }
353             }
354           if (!done)
355             {
356               if (a)
357                 printf(ADDR_FORMAT, a);
358               else
359                 printf(((flg & PCI_BASE_ADDRESS_MEM_MASK) || z) ? "<ignored>" : "<unassigned>");
360             }
361           printf(" (%s, %sprefetchable)",
362                  (t == PCI_BASE_ADDRESS_MEM_TYPE_32) ? "32-bit" :
363                  (t == PCI_BASE_ADDRESS_MEM_TYPE_64) ? "64-bit" :
364                  (t == PCI_BASE_ADDRESS_MEM_TYPE_1M) ? "low-1M" : "type 3",
365                  (flg & PCI_BASE_ADDRESS_MEM_PREFETCH) ? "" : "non-");
366           if (!(cmd & PCI_COMMAND_MEMORY))
367             printf(" [disabled]");
368         }
369       show_size(len);
370       putchar('\n');
371     }
372 }
373
374 static void
375 show_pm(struct device *d, int where, int cap)
376 {
377   int t, b;
378   static int pm_aux_current[8] = { 0, 55, 100, 160, 220, 270, 320, 375 };
379
380   printf("Power Management version %d\n", cap & PCI_PM_CAP_VER_MASK);
381   if (verbose < 2)
382     return;
383   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",
384          FLAG(cap, PCI_PM_CAP_PME_CLOCK),
385          FLAG(cap, PCI_PM_CAP_DSI),
386          FLAG(cap, PCI_PM_CAP_D1),
387          FLAG(cap, PCI_PM_CAP_D2),
388          pm_aux_current[(cap >> 6) & 7],
389          FLAG(cap, PCI_PM_CAP_PME_D0),
390          FLAG(cap, PCI_PM_CAP_PME_D1),
391          FLAG(cap, PCI_PM_CAP_PME_D2),
392          FLAG(cap, PCI_PM_CAP_PME_D3_HOT),
393          FLAG(cap, PCI_PM_CAP_PME_D3_COLD));
394   config_fetch(d, where + PCI_PM_CTRL, PCI_PM_SIZEOF - PCI_PM_CTRL);
395   t = get_conf_word(d, where + PCI_PM_CTRL);
396   printf("\t\tStatus: D%d PME-Enable%c DSel=%d DScale=%d PME%c\n",
397          t & PCI_PM_CTRL_STATE_MASK,
398          FLAG(t, PCI_PM_CTRL_PME_ENABLE),
399          (t & PCI_PM_CTRL_DATA_SEL_MASK) >> 9,
400          (t & PCI_PM_CTRL_DATA_SCALE_MASK) >> 13,
401          FLAG(t, PCI_PM_CTRL_PME_STATUS));
402   b = get_conf_byte(d, where + PCI_PM_PPB_EXTENSIONS);
403   if (b)
404     printf("\t\tBridge: PM%c B3%c\n",
405            FLAG(t, PCI_PM_BPCC_ENABLE),
406            FLAG(~t, PCI_PM_PPB_B2_B3));
407 }
408
409 static void
410 format_agp_rate(int rate, char *buf, int agp3)
411 {
412   char *c = buf;
413   int i;
414
415   for(i=0; i<=2; i++)
416     if (rate & (1 << i))
417       {
418         if (c != buf)
419           *c++ = ',';
420         *c++ = 'x';
421         *c++ = '0' + (1 << (i + 2*agp3));
422       }
423   if (c != buf)
424     *c = 0;
425   else
426     strcpy(buf, "<none>");
427 }
428
429 static void
430 show_agp(struct device *d, int where, int cap)
431 {
432   u32 t;
433   char rate[8];
434   int ver, rev;
435   int agp3 = 0;
436
437   ver = (cap >> 4) & 0x0f;
438   rev = cap & 0x0f;
439   printf("AGP version %x.%x\n", ver, rev);
440   if (verbose < 2)
441     return;
442   config_fetch(d, where + PCI_AGP_STATUS, PCI_AGP_SIZEOF - PCI_AGP_STATUS);
443   t = get_conf_long(d, where + PCI_AGP_STATUS);
444   if (ver >= 3 && (t & PCI_AGP_STATUS_AGP3))
445     agp3 = 1;
446   format_agp_rate(t & 7, rate, agp3);
447   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",
448          ((t & PCI_AGP_STATUS_RQ_MASK) >> 24U) + 1,
449          FLAG(t, PCI_AGP_STATUS_ISOCH),
450          ((t & PCI_AGP_STATUS_ARQSZ_MASK) >> 13),
451          ((t & PCI_AGP_STATUS_CAL_MASK) >> 10),
452          FLAG(t, PCI_AGP_STATUS_SBA),
453          FLAG(t, PCI_AGP_STATUS_ITA_COH),
454          FLAG(t, PCI_AGP_STATUS_GART64),
455          FLAG(t, PCI_AGP_STATUS_HTRANS),
456          FLAG(t, PCI_AGP_STATUS_64BIT),
457          FLAG(t, PCI_AGP_STATUS_FW),
458          FLAG(t, PCI_AGP_STATUS_AGP3),
459          rate);
460   t = get_conf_long(d, where + PCI_AGP_COMMAND);
461   format_agp_rate(t & 7, rate, agp3);
462   printf("\t\tCommand: RQ=%d ArqSz=%d Cal=%d SBA%c AGP%c GART64%c 64bit%c FW%c Rate=%s\n",
463          ((t & PCI_AGP_COMMAND_RQ_MASK) >> 24U) + 1,
464          ((t & PCI_AGP_COMMAND_ARQSZ_MASK) >> 13),
465          ((t & PCI_AGP_COMMAND_CAL_MASK) >> 10),
466          FLAG(t, PCI_AGP_COMMAND_SBA),
467          FLAG(t, PCI_AGP_COMMAND_AGP),
468          FLAG(t, PCI_AGP_COMMAND_GART64),
469          FLAG(t, PCI_AGP_COMMAND_64BIT),
470          FLAG(t, PCI_AGP_COMMAND_FW),
471          rate);
472 }
473
474 static void
475 show_pcix_nobridge(struct device *d, int where)
476 {
477   u16 command = get_conf_word(d, where + PCI_PCIX_COMMAND);
478   u32 status = get_conf_long(d, where + PCI_PCIX_STATUS);
479   printf("PCI-X non-bridge device.\n");
480   if (verbose < 2)
481     return;
482   printf("\t\tCommand: DPERE%c ERO%c RBC=%d OST=%d\n",
483          FLAG(command, PCI_PCIX_COMMAND_DPERE),
484          FLAG(command, PCI_PCIX_COMMAND_ERO),
485          ((command & PCI_PCIX_COMMAND_MAX_MEM_READ_BYTE_COUNT) >> 2U),
486          ((command & PCI_PCIX_COMMAND_MAX_OUTSTANDING_SPLIT_TRANS) >> 4U));
487   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",
488          ((status >> 8) & 0xffU), // bus
489          ((status >> 3) & 0x1fU), // dev
490          (status & PCI_PCIX_BRIDGE_STATUS_FUNCTION), // function
491          FLAG(status, PCI_PCIX_STATUS_64BIT),
492          FLAG(status, PCI_PCIX_STATUS_133MHZ),
493          FLAG(status, PCI_PCIX_STATUS_SC_DISCARDED),
494          FLAG(status, PCI_PCIX_STATUS_UNEXPECTED_SC),
495          ((status & PCI_PCIX_STATUS_DEVICE_COMPLEXITY) ? "bridge" : "simple"),
496          ((status >> 21) & 3U),
497          ((status >> 23) & 7U),
498          ((status >> 26) & 7U),
499          FLAG(status, PCI_PCIX_STATUS_RCVD_SC_ERR_MESS));
500 }
501
502 static void
503 show_pcix_bridge(struct device *d, int where)
504 {
505   u16 secstatus;
506   u32 status, upstcr, downstcr;
507   printf("PCI-X bridge device.\n");
508   if (verbose < 2)
509     return;
510   secstatus = get_conf_word(d, where + PCI_PCIX_BRIDGE_SEC_STATUS);
511   printf("\t\tSecondary Status: 64bit%c, 133MHz%c, SCD%c, USC%c, SCO%c, SRD%c Freq=%d\n",
512          FLAG(secstatus, PCI_PCIX_BRIDGE_SEC_STATUS_64BIT),
513          FLAG(secstatus, PCI_PCIX_BRIDGE_SEC_STATUS_133MHZ),
514          FLAG(secstatus, PCI_PCIX_BRIDGE_SEC_STATUS_SC_DISCARDED),
515          FLAG(secstatus, PCI_PCIX_BRIDGE_SEC_STATUS_UNEXPECTED_SC),
516          FLAG(secstatus, PCI_PCIX_BRIDGE_SEC_STATUS_SC_OVERRUN),
517          FLAG(secstatus, PCI_PCIX_BRIDGE_SEC_STATUS_SPLIT_REQUEST_DELAYED),
518          ((secstatus >> 6) & 7));
519   status = get_conf_long(d, where + PCI_PCIX_BRIDGE_STATUS);
520   printf("\t\tStatus: Bus=%u Dev=%u Func=%u 64bit%c 133MHz%c SCD%c USC%c, SCO%c, SRD%c\n", 
521          ((status >> 8) & 0xff), // bus
522          ((status >> 3) & 0x1f), // dev
523          (status & PCI_PCIX_BRIDGE_STATUS_FUNCTION), // function
524          FLAG(status, PCI_PCIX_BRIDGE_STATUS_64BIT),
525          FLAG(status, PCI_PCIX_BRIDGE_STATUS_133MHZ),
526          FLAG(status, PCI_PCIX_BRIDGE_STATUS_SC_DISCARDED),
527          FLAG(status, PCI_PCIX_BRIDGE_STATUS_UNEXPECTED_SC),
528          FLAG(status, PCI_PCIX_BRIDGE_STATUS_SC_OVERRUN),
529          FLAG(status, PCI_PCIX_BRIDGE_STATUS_SPLIT_REQUEST_DELAYED));
530   upstcr = get_conf_long(d, where + PCI_PCIX_BRIDGE_UPSTREAM_SPLIT_TRANS_CTRL);
531   printf("\t\t: Upstream: Capacity=%u, Commitment Limit=%u\n",
532          (upstcr & PCI_PCIX_BRIDGE_STR_CAPACITY),
533          (upstcr >> 16) & 0xffff);
534   downstcr = get_conf_long(d, where + PCI_PCIX_BRIDGE_DOWNSTREAM_SPLIT_TRANS_CTRL);
535   printf("\t\t: Downstream: Capacity=%u, Commitment Limit=%u\n",
536          (downstcr & PCI_PCIX_BRIDGE_STR_CAPACITY),
537          (downstcr >> 16) & 0xffff);
538 }
539
540 static void
541 show_pcix(struct device *d, int where)
542 {
543   switch (d->dev->hdrtype)
544     {
545     case PCI_HEADER_TYPE_NORMAL:
546       show_pcix_nobridge(d, where);
547       break;
548     case PCI_HEADER_TYPE_BRIDGE:
549       show_pcix_bridge(d, where);
550       break;
551     }
552 }
553
554 static void
555 show_rom(struct device *d)
556 {
557   struct pci_dev *p = d->dev;
558   pciaddr_t rom = p->rom_base_addr;
559   pciaddr_t len = (p->known_fields & PCI_FILL_SIZES) ? p->rom_size : 0;
560
561   if (!rom && !len)
562     return;
563   printf("\tExpansion ROM at ");
564   if (rom & PCI_ROM_ADDRESS_MASK)
565     printf(ADDR_FORMAT, rom & PCI_ROM_ADDRESS_MASK);
566   else
567     printf("<unassigned>");
568   if (!(rom & PCI_ROM_ADDRESS_ENABLE))
569     printf(" [disabled]");
570   show_size(len);
571   putchar('\n');
572 }
573
574 static void
575 show_msi(struct device *d, int where, int cap)
576 {
577   int is64;
578   u32 t;
579   u16 w;
580
581   printf("Message Signalled Interrupts: 64bit%c Queue=%d/%d Enable%c\n",
582          FLAG(cap, PCI_MSI_FLAGS_64BIT),
583          (cap & PCI_MSI_FLAGS_QSIZE) >> 4,
584          (cap & PCI_MSI_FLAGS_QMASK) >> 1,
585          FLAG(cap, PCI_MSI_FLAGS_ENABLE));
586   if (verbose < 2)
587     return;
588   is64 = cap & PCI_MSI_FLAGS_64BIT;
589   config_fetch(d, where + PCI_MSI_ADDRESS_LO, (is64 ? PCI_MSI_DATA_64 : PCI_MSI_DATA_32) + 2 - PCI_MSI_ADDRESS_LO);
590   printf("\t\tAddress: ");
591   if (is64)
592     {
593       t = get_conf_long(d, where + PCI_MSI_ADDRESS_HI);
594       w = get_conf_word(d, where + PCI_MSI_DATA_64);
595       printf("%08x", t);
596     }
597   else
598     w = get_conf_word(d, where + PCI_MSI_DATA_32);
599   t = get_conf_long(d, where + PCI_MSI_ADDRESS_LO);
600   printf("%08x  Data: %04x\n", t, w);
601 }
602
603 static void
604 show_slotid(int cap)
605 {
606   int esr = cap & 0xff;
607   int chs = cap >> 8;
608
609   printf("Slot ID: %d slots, First%c, chassis %02x\n",
610          esr & PCI_SID_ESR_NSLOTS,
611          FLAG(esr, PCI_SID_ESR_FIC),
612          chs);
613 }
614
615 static void
616 show_caps(struct device *d)
617 {
618   if (get_conf_word(d, PCI_STATUS) & PCI_STATUS_CAP_LIST)
619     {
620       int where = get_conf_byte(d, PCI_CAPABILITY_LIST) & ~3;
621       while (where)
622         {
623           int id, next, cap;
624           printf("\tCapabilities: ");
625           if (!config_fetch(d, where, 4))
626             {
627               puts("<available only to root>");
628               break;
629             }
630           id = get_conf_byte(d, where + PCI_CAP_LIST_ID);
631           next = get_conf_byte(d, where + PCI_CAP_LIST_NEXT) & ~3;
632           cap = get_conf_word(d, where + PCI_CAP_FLAGS);
633           printf("[%02x] ", where);
634           if (id == 0xff)
635             {
636               printf("<chain broken>\n");
637               break;
638             }
639           switch (id)
640             {
641             case PCI_CAP_ID_PM:
642               show_pm(d, where, cap);
643               break;
644             case PCI_CAP_ID_AGP:
645               show_agp(d, where, cap);
646               break;
647             case PCI_CAP_ID_VPD:
648               printf("Vital Product Data\n");
649               break;
650             case PCI_CAP_ID_SLOTID:
651               show_slotid(cap);
652               break;
653             case PCI_CAP_ID_MSI:
654               show_msi(d, where, cap);
655               break;
656             case PCI_CAP_ID_PCIX:
657               show_pcix(d, where);
658               break;
659             default:
660               printf("#%02x [%04x]\n", id, cap);
661             }
662           where = next;
663         }
664     }
665 }
666
667 static void
668 show_htype0(struct device *d)
669 {
670   show_bases(d, 6);
671   show_rom(d);
672   show_caps(d);
673 }
674
675 static void
676 show_htype1(struct device *d)
677 {
678   u32 io_base = get_conf_byte(d, PCI_IO_BASE);
679   u32 io_limit = get_conf_byte(d, PCI_IO_LIMIT);
680   u32 io_type = io_base & PCI_IO_RANGE_TYPE_MASK;
681   u32 mem_base = get_conf_word(d, PCI_MEMORY_BASE);
682   u32 mem_limit = get_conf_word(d, PCI_MEMORY_LIMIT);
683   u32 mem_type = mem_base & PCI_MEMORY_RANGE_TYPE_MASK;
684   u32 pref_base = get_conf_word(d, PCI_PREF_MEMORY_BASE);
685   u32 pref_limit = get_conf_word(d, PCI_PREF_MEMORY_LIMIT);
686   u32 pref_type = pref_base & PCI_PREF_RANGE_TYPE_MASK;
687   word brc = get_conf_word(d, PCI_BRIDGE_CONTROL);
688   int verb = verbose > 2;
689
690   show_bases(d, 2);
691   printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
692          get_conf_byte(d, PCI_PRIMARY_BUS),
693          get_conf_byte(d, PCI_SECONDARY_BUS),
694          get_conf_byte(d, PCI_SUBORDINATE_BUS),
695          get_conf_byte(d, PCI_SEC_LATENCY_TIMER));
696
697   if (io_type != (io_limit & PCI_IO_RANGE_TYPE_MASK) ||
698       (io_type != PCI_IO_RANGE_TYPE_16 && io_type != PCI_IO_RANGE_TYPE_32))
699     printf("\t!!! Unknown I/O range types %x/%x\n", io_base, io_limit);
700   else
701     {
702       io_base = (io_base & PCI_IO_RANGE_MASK) << 8;
703       io_limit = (io_limit & PCI_IO_RANGE_MASK) << 8;
704       if (io_type == PCI_IO_RANGE_TYPE_32)
705         {
706           io_base |= (get_conf_word(d, PCI_IO_BASE_UPPER16) << 16);
707           io_limit |= (get_conf_word(d, PCI_IO_LIMIT_UPPER16) << 16);
708         }
709       if (io_base <= io_limit || verb)
710         printf("\tI/O behind bridge: %08x-%08x\n", io_base, io_limit+0xfff);
711     }
712
713   if (mem_type != (mem_limit & PCI_MEMORY_RANGE_TYPE_MASK) ||
714       mem_type)
715     printf("\t!!! Unknown memory range types %x/%x\n", mem_base, mem_limit);
716   else
717     {
718       mem_base = (mem_base & PCI_MEMORY_RANGE_MASK) << 16;
719       mem_limit = (mem_limit & PCI_MEMORY_RANGE_MASK) << 16;
720       if (mem_base <= mem_limit || verb)
721         printf("\tMemory behind bridge: %08x-%08x\n", mem_base, mem_limit + 0xfffff);
722     }
723
724   if (pref_type != (pref_limit & PCI_PREF_RANGE_TYPE_MASK) ||
725       (pref_type != PCI_PREF_RANGE_TYPE_32 && pref_type != PCI_PREF_RANGE_TYPE_64))
726     printf("\t!!! Unknown prefetchable memory range types %x/%x\n", pref_base, pref_limit);
727   else
728     {
729       pref_base = (pref_base & PCI_PREF_RANGE_MASK) << 16;
730       pref_limit = (pref_limit & PCI_PREF_RANGE_MASK) << 16;
731       if (pref_base <= pref_limit || verb)
732         {
733           if (pref_type == PCI_PREF_RANGE_TYPE_32)
734             printf("\tPrefetchable memory behind bridge: %08x-%08x\n", pref_base, pref_limit + 0xfffff);
735           else
736             printf("\tPrefetchable memory behind bridge: %08x%08x-%08x%08x\n",
737                    get_conf_long(d, PCI_PREF_BASE_UPPER32),
738                    pref_base,
739                    get_conf_long(d, PCI_PREF_LIMIT_UPPER32),
740                    pref_limit);
741         }
742     }
743
744   if (get_conf_word(d, PCI_SEC_STATUS) & PCI_STATUS_SIG_SYSTEM_ERROR)
745     printf("\tSecondary status: SERR\n");
746
747   show_rom(d);
748
749   if (verbose > 1)
750     printf("\tBridgeCtl: Parity%c SERR%c NoISA%c VGA%c MAbort%c >Reset%c FastB2B%c\n",
751            FLAG(brc, PCI_BRIDGE_CTL_PARITY),
752            FLAG(brc, PCI_BRIDGE_CTL_SERR),
753            FLAG(brc, PCI_BRIDGE_CTL_NO_ISA),
754            FLAG(brc, PCI_BRIDGE_CTL_VGA),
755            FLAG(brc, PCI_BRIDGE_CTL_MASTER_ABORT),
756            FLAG(brc, PCI_BRIDGE_CTL_BUS_RESET),
757            FLAG(brc, PCI_BRIDGE_CTL_FAST_BACK));
758
759   show_caps(d);
760 }
761
762 static void
763 show_htype2(struct device *d)
764 {
765   int i;
766   word cmd = get_conf_word(d, PCI_COMMAND);
767   word brc = get_conf_word(d, PCI_CB_BRIDGE_CONTROL);
768   word exca = get_conf_word(d, PCI_CB_LEGACY_MODE_BASE);
769   int verb = verbose > 2;
770
771   show_bases(d, 1);
772   printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
773          get_conf_byte(d, PCI_CB_PRIMARY_BUS),
774          get_conf_byte(d, PCI_CB_CARD_BUS),
775          get_conf_byte(d, PCI_CB_SUBORDINATE_BUS),
776          get_conf_byte(d, PCI_CB_LATENCY_TIMER));
777   for(i=0; i<2; i++)
778     {
779       int p = 8*i;
780       u32 base = get_conf_long(d, PCI_CB_MEMORY_BASE_0 + p);
781       u32 limit = get_conf_long(d, PCI_CB_MEMORY_LIMIT_0 + p);
782       if (limit > base || verb)
783         printf("\tMemory window %d: %08x-%08x%s%s\n", i, base, limit,
784                (cmd & PCI_COMMAND_MEMORY) ? "" : " [disabled]",
785                (brc & (PCI_CB_BRIDGE_CTL_PREFETCH_MEM0 << i)) ? " (prefetchable)" : "");
786     }
787   for(i=0; i<2; i++)
788     {
789       int p = 8*i;
790       u32 base = get_conf_long(d, PCI_CB_IO_BASE_0 + p);
791       u32 limit = get_conf_long(d, PCI_CB_IO_LIMIT_0 + p);
792       if (!(base & PCI_IO_RANGE_TYPE_32))
793         {
794           base &= 0xffff;
795           limit &= 0xffff;
796         }
797       base &= PCI_CB_IO_RANGE_MASK;
798       limit = (limit & PCI_CB_IO_RANGE_MASK) + 3;
799       if (base <= limit || verb)
800         printf("\tI/O window %d: %08x-%08x%s\n", i, base, limit,
801                (cmd & PCI_COMMAND_IO) ? "" : " [disabled]");
802     }
803
804   if (get_conf_word(d, PCI_CB_SEC_STATUS) & PCI_STATUS_SIG_SYSTEM_ERROR)
805     printf("\tSecondary status: SERR\n");
806   if (verbose > 1)
807     printf("\tBridgeCtl: Parity%c SERR%c ISA%c VGA%c MAbort%c >Reset%c 16bInt%c PostWrite%c\n",
808            FLAG(brc, PCI_CB_BRIDGE_CTL_PARITY),
809            FLAG(brc, PCI_CB_BRIDGE_CTL_SERR),
810            FLAG(brc, PCI_CB_BRIDGE_CTL_ISA),
811            FLAG(brc, PCI_CB_BRIDGE_CTL_VGA),
812            FLAG(brc, PCI_CB_BRIDGE_CTL_MASTER_ABORT),
813            FLAG(brc, PCI_CB_BRIDGE_CTL_CB_RESET),
814            FLAG(brc, PCI_CB_BRIDGE_CTL_16BIT_INT),
815            FLAG(brc, PCI_CB_BRIDGE_CTL_POST_WRITES));
816   if (exca)
817     printf("\t16-bit legacy interface ports at %04x\n", exca);
818 }
819
820 static void
821 show_verbose(struct device *d)
822 {
823   struct pci_dev *p = d->dev;
824   word status = get_conf_word(d, PCI_STATUS);
825   word cmd = get_conf_word(d, PCI_COMMAND);
826   word class = get_conf_word(d, PCI_CLASS_DEVICE);
827   byte bist = get_conf_byte(d, PCI_BIST);
828   byte htype = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
829   byte latency = get_conf_byte(d, PCI_LATENCY_TIMER);
830   byte cache_line = get_conf_byte(d, PCI_CACHE_LINE_SIZE);
831   byte max_lat, min_gnt;
832   byte int_pin = get_conf_byte(d, PCI_INTERRUPT_PIN);
833   unsigned int irq = p->irq;
834   word subsys_v, subsys_d;
835   char ssnamebuf[256];
836
837   show_terse(d);
838
839   switch (htype)
840     {
841     case PCI_HEADER_TYPE_NORMAL:
842       if (class == PCI_CLASS_BRIDGE_PCI)
843         printf("\t!!! Invalid class %04x for header type %02x\n", class, htype);
844       max_lat = get_conf_byte(d, PCI_MAX_LAT);
845       min_gnt = get_conf_byte(d, PCI_MIN_GNT);
846       subsys_v = get_conf_word(d, PCI_SUBSYSTEM_VENDOR_ID);
847       subsys_d = get_conf_word(d, PCI_SUBSYSTEM_ID);
848       break;
849     case PCI_HEADER_TYPE_BRIDGE:
850       if (class != PCI_CLASS_BRIDGE_PCI)
851         printf("\t!!! Invalid class %04x for header type %02x\n", class, htype);
852       irq = int_pin = min_gnt = max_lat = 0;
853       subsys_v = subsys_d = 0;
854       break;
855     case PCI_HEADER_TYPE_CARDBUS:
856       if ((class >> 8) != PCI_BASE_CLASS_BRIDGE)
857         printf("\t!!! Invalid class %04x for header type %02x\n", class, htype);
858       min_gnt = max_lat = 0;
859       subsys_v = get_conf_word(d, PCI_CB_SUBSYSTEM_VENDOR_ID);
860       subsys_d = get_conf_word(d, PCI_CB_SUBSYSTEM_ID);
861       break;
862     default:
863       printf("\t!!! Unknown header type %02x\n", htype);
864       return;
865     }
866
867   if (subsys_v && subsys_v != 0xffff)
868     printf("\tSubsystem: %s\n",
869            pci_lookup_name(pacc, ssnamebuf, sizeof(ssnamebuf),
870                            PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
871                            p->vendor_id, p->device_id, subsys_v, subsys_d));
872
873   if (verbose > 1)
874     {
875       printf("\tControl: I/O%c Mem%c BusMaster%c SpecCycle%c MemWINV%c VGASnoop%c ParErr%c Stepping%c SERR%c FastB2B%c\n",
876              FLAG(cmd, PCI_COMMAND_IO),
877              FLAG(cmd, PCI_COMMAND_MEMORY),
878              FLAG(cmd, PCI_COMMAND_MASTER),
879              FLAG(cmd, PCI_COMMAND_SPECIAL),
880              FLAG(cmd, PCI_COMMAND_INVALIDATE),
881              FLAG(cmd, PCI_COMMAND_VGA_PALETTE),
882              FLAG(cmd, PCI_COMMAND_PARITY),
883              FLAG(cmd, PCI_COMMAND_WAIT),
884              FLAG(cmd, PCI_COMMAND_SERR),
885              FLAG(cmd, PCI_COMMAND_FAST_BACK));
886       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",
887              FLAG(status, PCI_STATUS_CAP_LIST),
888              FLAG(status, PCI_STATUS_66MHZ),
889              FLAG(status, PCI_STATUS_UDF),
890              FLAG(status, PCI_STATUS_FAST_BACK),
891              FLAG(status, PCI_STATUS_PARITY),
892              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
893              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
894              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??",
895              FLAG(status, PCI_STATUS_SIG_TARGET_ABORT),
896              FLAG(status, PCI_STATUS_REC_TARGET_ABORT),
897              FLAG(status, PCI_STATUS_REC_MASTER_ABORT),
898              FLAG(status, PCI_STATUS_SIG_SYSTEM_ERROR),
899              FLAG(status, PCI_STATUS_DETECTED_PARITY));
900       if (cmd & PCI_COMMAND_MASTER)
901         {
902           printf("\tLatency: %d", latency);
903           if (min_gnt || max_lat)
904             {
905               printf(" (");
906               if (min_gnt)
907                 printf("%dns min", min_gnt*250);
908               if (min_gnt && max_lat)
909                 printf(", ");
910               if (max_lat)
911                 printf("%dns max", max_lat*250);
912               putchar(')');
913             }
914           if (cache_line)
915             printf(", cache line size %02x", cache_line);
916           putchar('\n');
917         }
918       if (int_pin || irq)
919         printf("\tInterrupt: pin %c routed to IRQ " IRQ_FORMAT "\n",
920                (int_pin ? 'A' + int_pin - 1 : '?'), irq);
921     }
922   else
923     {
924       printf("\tFlags: ");
925       if (cmd & PCI_COMMAND_MASTER)
926         printf("bus master, ");
927       if (cmd & PCI_COMMAND_VGA_PALETTE)
928         printf("VGA palette snoop, ");
929       if (cmd & PCI_COMMAND_WAIT)
930         printf("stepping, ");
931       if (cmd & PCI_COMMAND_FAST_BACK)
932         printf("fast Back2Back, ");
933       if (status & PCI_STATUS_66MHZ)
934         printf("66Mhz, ");
935       if (status & PCI_STATUS_UDF)
936         printf("user-definable features, ");
937       printf("%s devsel",
938              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
939              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
940              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??");
941       if (cmd & PCI_COMMAND_MASTER)
942         printf(", latency %d", latency);
943       if (irq)
944         printf(", IRQ " IRQ_FORMAT, irq);
945       putchar('\n');
946     }
947
948   if (bist & PCI_BIST_CAPABLE)
949     {
950       if (bist & PCI_BIST_START)
951         printf("\tBIST is running\n");
952       else
953         printf("\tBIST result: %02x\n", bist & PCI_BIST_CODE_MASK);
954     }
955
956   switch (htype)
957     {
958     case PCI_HEADER_TYPE_NORMAL:
959       show_htype0(d);
960       break;
961     case PCI_HEADER_TYPE_BRIDGE:
962       show_htype1(d);
963       break;
964     case PCI_HEADER_TYPE_CARDBUS:
965       show_htype2(d);
966       break;
967     }
968 }
969
970 static void
971 show_hex_dump(struct device *d)
972 {
973   unsigned int i;
974
975   for(i=0; i<d->config_cnt; i++)
976     {
977       if (! (i & 15))
978         printf("%02x:", i);
979       printf(" %02x", get_conf_byte(d, i));
980       if ((i & 15) == 15)
981         putchar('\n');
982     }
983 }
984
985 static void
986 show_machine(struct device *d)
987 {
988   struct pci_dev *p = d->dev;
989   int c;
990   word sv_id=0, sd_id=0;
991   char classbuf[128], vendbuf[128], devbuf[128], svbuf[128], sdbuf[128];
992
993   switch (get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f)
994     {
995     case PCI_HEADER_TYPE_NORMAL:
996       sv_id = get_conf_word(d, PCI_SUBSYSTEM_VENDOR_ID);
997       sd_id = get_conf_word(d, PCI_SUBSYSTEM_ID);
998       break;
999     case PCI_HEADER_TYPE_CARDBUS:
1000       sv_id = get_conf_word(d, PCI_CB_SUBSYSTEM_VENDOR_ID);
1001       sd_id = get_conf_word(d, PCI_CB_SUBSYSTEM_ID);
1002       break;
1003     }
1004
1005   if (verbose)
1006     {
1007       printf("Device:\t%02x:%02x.%x\n", p->bus, p->dev, p->func);
1008       printf("Class:\t%s\n",
1009              pci_lookup_name(pacc, classbuf, sizeof(classbuf), PCI_LOOKUP_CLASS, get_conf_word(d, PCI_CLASS_DEVICE), 0, 0, 0));
1010       printf("Vendor:\t%s\n",
1011              pci_lookup_name(pacc, vendbuf, sizeof(vendbuf), PCI_LOOKUP_VENDOR, p->vendor_id, p->device_id, 0, 0));
1012       printf("Device:\t%s\n",
1013              pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id, 0, 0));
1014       if (sv_id && sv_id != 0xffff)
1015         {
1016           printf("SVendor:\t%s\n",
1017                  pci_lookup_name(pacc, svbuf, sizeof(svbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR, p->vendor_id, p->device_id, sv_id, sd_id));
1018           printf("SDevice:\t%s\n",
1019                  pci_lookup_name(pacc, sdbuf, sizeof(sdbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id, sv_id, sd_id));
1020         }
1021       if (c = get_conf_byte(d, PCI_REVISION_ID))
1022         printf("Rev:\t%02x\n", c);
1023       if (c = get_conf_byte(d, PCI_CLASS_PROG))
1024         printf("ProgIf:\t%02x\n", c);
1025     }
1026   else
1027     {
1028       printf("%02x:%02x.%x ", p->bus, p->dev, p->func);
1029       printf("\"%s\" \"%s\" \"%s\"",
1030              pci_lookup_name(pacc, classbuf, sizeof(classbuf), PCI_LOOKUP_CLASS,
1031                              get_conf_word(d, PCI_CLASS_DEVICE), 0, 0, 0),
1032              pci_lookup_name(pacc, vendbuf, sizeof(vendbuf), PCI_LOOKUP_VENDOR,
1033                              p->vendor_id, p->device_id, 0, 0),
1034              pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_DEVICE,
1035                              p->vendor_id, p->device_id, 0, 0));
1036       if (c = get_conf_byte(d, PCI_REVISION_ID))
1037         printf(" -r%02x", c);
1038       if (c = get_conf_byte(d, PCI_CLASS_PROG))
1039         printf(" -p%02x", c);
1040       if (sv_id && sv_id != 0xffff)
1041         printf(" \"%s\" \"%s\"",
1042                pci_lookup_name(pacc, svbuf, sizeof(svbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR, p->vendor_id, p->device_id, sv_id, sd_id),
1043                pci_lookup_name(pacc, sdbuf, sizeof(sdbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id, sv_id, sd_id));
1044       else
1045         printf(" \"\" \"\"");
1046       putchar('\n');
1047     }
1048 }
1049
1050 static void
1051 show_device(struct device *d)
1052 {
1053   if (machine_readable)
1054     show_machine(d);
1055   else if (verbose)
1056     show_verbose(d);
1057   else
1058     show_terse(d);
1059   if (show_hex)
1060     show_hex_dump(d);
1061   if (verbose || show_hex)
1062     putchar('\n');
1063 }
1064
1065 static void
1066 show(void)
1067 {
1068   struct device *d;
1069
1070   for(d=first_dev; d; d=d->next)
1071     show_device(d);
1072 }
1073
1074 /* Tree output */
1075
1076 struct bridge {
1077   struct bridge *chain;                 /* Single-linked list of bridges */
1078   struct bridge *next, *child;          /* Tree of bridges */
1079   struct bus *first_bus;                /* List of busses connected to this bridge */
1080   unsigned int primary, secondary, subordinate; /* Bus numbers */
1081   struct device *br_dev;
1082 };
1083
1084 struct bus {
1085   unsigned int number;
1086   struct bus *sibling;
1087   struct device *first_dev, **last_dev;
1088 };
1089
1090 static struct bridge host_bridge = { NULL, NULL, NULL, NULL, ~0, 0, ~0, NULL };
1091
1092 static struct bus *
1093 find_bus(struct bridge *b, unsigned int n)
1094 {
1095   struct bus *bus;
1096
1097   for(bus=b->first_bus; bus; bus=bus->sibling)
1098     if (bus->number == n)
1099       break;
1100   return bus;
1101 }
1102
1103 static struct bus *
1104 new_bus(struct bridge *b, unsigned int n)
1105 {
1106   struct bus *bus = xmalloc(sizeof(struct bus));
1107
1108   bus = xmalloc(sizeof(struct bus));
1109   bus->number = n;
1110   bus->sibling = b->first_bus;
1111   bus->first_dev = NULL;
1112   bus->last_dev = &bus->first_dev;
1113   b->first_bus = bus;
1114   return bus;
1115 }
1116
1117 static void
1118 insert_dev(struct device *d, struct bridge *b)
1119 {
1120   struct pci_dev *p = d->dev;
1121   struct bus *bus;
1122
1123   if (! (bus = find_bus(b, p->bus)))
1124     {
1125       struct bridge *c;
1126       for(c=b->child; c; c=c->next)
1127         if (c->secondary <= p->bus && p->bus <= c->subordinate)
1128           {
1129             insert_dev(d, c);
1130             return;
1131           }
1132       bus = new_bus(b, p->bus);
1133     }
1134   /* Simple insertion at the end _does_ guarantee the correct order as the
1135    * original device list was sorted by (bus, devfn) lexicographically
1136    * and all devices on the new list have the same bus number.
1137    */
1138   *bus->last_dev = d;
1139   bus->last_dev = &d->next;
1140   d->next = NULL;
1141 }
1142
1143 static void
1144 grow_tree(void)
1145 {
1146   struct device *d, *d2;
1147   struct bridge **last_br, *b;
1148
1149   /* Build list of bridges */
1150
1151   last_br = &host_bridge.chain;
1152   for(d=first_dev; d; d=d->next)
1153     {
1154       word class = get_conf_word(d, PCI_CLASS_DEVICE);
1155       byte ht = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
1156       if (class == PCI_CLASS_BRIDGE_PCI &&
1157           (ht == PCI_HEADER_TYPE_BRIDGE || ht == PCI_HEADER_TYPE_CARDBUS))
1158         {
1159           b = xmalloc(sizeof(struct bridge));
1160           if (ht == PCI_HEADER_TYPE_BRIDGE)
1161             {
1162               b->primary = get_conf_byte(d, PCI_CB_PRIMARY_BUS);
1163               b->secondary = get_conf_byte(d, PCI_CB_CARD_BUS);
1164               b->subordinate = get_conf_byte(d, PCI_CB_SUBORDINATE_BUS);
1165             }
1166           else
1167             {
1168               b->primary = get_conf_byte(d, PCI_PRIMARY_BUS);
1169               b->secondary = get_conf_byte(d, PCI_SECONDARY_BUS);
1170               b->subordinate = get_conf_byte(d, PCI_SUBORDINATE_BUS);
1171             }
1172           *last_br = b;
1173           last_br = &b->chain;
1174           b->next = b->child = NULL;
1175           b->first_bus = NULL;
1176           b->br_dev = d;
1177         }
1178     }
1179   *last_br = NULL;
1180
1181   /* Create a bridge tree */
1182
1183   for(b=&host_bridge; b; b=b->chain)
1184     {
1185       struct bridge *c, *best;
1186       best = NULL;
1187       for(c=&host_bridge; c; c=c->chain)
1188         if (c != b && b->primary >= c->secondary && b->primary <= c->subordinate &&
1189             (!best || best->subordinate - best->primary > c->subordinate - c->primary))
1190           best = c;
1191       if (best)
1192         {
1193           b->next = best->child;
1194           best->child = b;
1195         }
1196     }
1197
1198   /* Insert secondary bus for each bridge */
1199
1200   for(b=&host_bridge; b; b=b->chain)
1201     if (!find_bus(b, b->secondary))
1202       new_bus(b, b->secondary);
1203
1204   /* Create bus structs and link devices */
1205
1206   for(d=first_dev; d;)
1207     {
1208       d2 = d->next;
1209       insert_dev(d, &host_bridge);
1210       d = d2;
1211     }
1212 }
1213
1214 static void
1215 print_it(byte *line, byte *p)
1216 {
1217   *p++ = '\n';
1218   *p = 0;
1219   fputs(line, stdout);
1220   for(p=line; *p; p++)
1221     if (*p == '+' || *p == '|')
1222       *p = '|';
1223     else
1224       *p = ' ';
1225 }
1226
1227 static void show_tree_bridge(struct bridge *, byte *, byte *);
1228
1229 static void
1230 show_tree_dev(struct device *d, byte *line, byte *p)
1231 {
1232   struct pci_dev *q = d->dev;
1233   struct bridge *b;
1234   char namebuf[256];
1235
1236   p += sprintf(p, "%02x.%x", q->dev, q->func);
1237   for(b=&host_bridge; b; b=b->chain)
1238     if (b->br_dev == d)
1239       {
1240         if (b->secondary == b->subordinate)
1241           p += sprintf(p, "-[%02x]-", b->secondary);
1242         else
1243           p += sprintf(p, "-[%02x-%02x]-", b->secondary, b->subordinate);
1244         show_tree_bridge(b, line, p);
1245         return;
1246       }
1247   if (verbose)
1248     p += sprintf(p, "  %s",
1249                  pci_lookup_name(pacc, namebuf, sizeof(namebuf),
1250                                  PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
1251                                  q->vendor_id, q->device_id, 0, 0));
1252   print_it(line, p);
1253 }
1254
1255 static void
1256 show_tree_bus(struct bus *b, byte *line, byte *p)
1257 {
1258   if (!b->first_dev)
1259     print_it(line, p);
1260   else if (!b->first_dev->next)
1261     {
1262       *p++ = '-';
1263       *p++ = '-';
1264       show_tree_dev(b->first_dev, line, p);
1265     }
1266   else
1267     {
1268       struct device *d = b->first_dev;
1269       while (d->next)
1270         {
1271           p[0] = '+';
1272           p[1] = '-';
1273           show_tree_dev(d, line, p+2);
1274           d = d->next;
1275         }
1276       p[0] = '\\';
1277       p[1] = '-';
1278       show_tree_dev(d, line, p+2);
1279     }
1280 }
1281
1282 static void
1283 show_tree_bridge(struct bridge *b, byte *line, byte *p)
1284 {
1285   *p++ = '-';
1286   if (!b->first_bus->sibling)
1287     {
1288       if (b == &host_bridge)
1289         p += sprintf(p, "[%02x]-", b->first_bus->number);
1290       show_tree_bus(b->first_bus, line, p);
1291     }
1292   else
1293     {
1294       struct bus *u = b->first_bus;
1295       byte *k;
1296
1297       while (u->sibling)
1298         {
1299           k = p + sprintf(p, "+-[%02x]-", u->number);
1300           show_tree_bus(u, line, k);
1301           u = u->sibling;
1302         }
1303       k = p + sprintf(p, "\\-[%02x]-", u->number);
1304       show_tree_bus(u, line, k);
1305     }
1306 }
1307
1308 static void
1309 show_forest(void)
1310 {
1311   char line[256];
1312
1313   grow_tree();
1314   show_tree_bridge(&host_bridge, line, line);
1315 }
1316
1317 /* Bus mapping mode */
1318
1319 struct bus_bridge {
1320   struct bus_bridge *next;
1321   byte this, dev, func, first, last, bug;
1322 };
1323
1324 struct bus_info {
1325   byte exists;
1326   byte guestbook;
1327   struct bus_bridge *bridges, *via;
1328 };
1329
1330 static struct bus_info *bus_info;
1331
1332 static void
1333 map_bridge(struct bus_info *bi, struct device *d, int np, int ns, int nl)
1334 {
1335   struct bus_bridge *b = xmalloc(sizeof(struct bus_bridge));
1336   struct pci_dev *p = d->dev;
1337
1338   b->next = bi->bridges;
1339   bi->bridges = b;
1340   b->this = get_conf_byte(d, np);
1341   b->dev = p->dev;
1342   b->func = p->func;
1343   b->first = get_conf_byte(d, ns);
1344   b->last = get_conf_byte(d, nl);
1345   printf("## %02x.%02x:%d is a bridge from %02x to %02x-%02x\n",
1346          p->bus, p->dev, p->func, b->this, b->first, b->last);
1347   if (b->this != p->bus)
1348     printf("!!! Bridge points to invalid primary bus.\n");
1349   if (b->first > b->last)
1350     {
1351       printf("!!! Bridge points to invalid bus range.\n");
1352       b->last = b->first;
1353     }
1354 }
1355
1356 static void
1357 do_map_bus(int bus)
1358 {
1359   int dev, func;
1360   int verbose = pacc->debugging;
1361   struct bus_info *bi = bus_info + bus;
1362   struct device *d;
1363
1364   if (verbose)
1365     printf("Mapping bus %02x\n", bus);
1366   for(dev = 0; dev < 32; dev++)
1367     if (filter.slot < 0 || filter.slot == dev)
1368       {
1369         int func_limit = 1;
1370         for(func = 0; func < func_limit; func++)
1371           if (filter.func < 0 || filter.func == func)
1372             {
1373               struct pci_dev *p = pci_get_dev(pacc, bus, dev, func);
1374               u16 vendor = pci_read_word(p, PCI_VENDOR_ID);
1375               if (vendor && vendor != 0xffff)
1376                 {
1377                   if (!func && (pci_read_byte(p, PCI_HEADER_TYPE) & 0x80))
1378                     func_limit = 8;
1379                   if (verbose)
1380                     printf("Discovered device %02x:%02x.%d\n", bus, dev, func);
1381                   bi->exists = 1;
1382                   if (d = scan_device(p))
1383                     {
1384                       show_device(d);
1385                       switch (get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f)
1386                         {
1387                         case PCI_HEADER_TYPE_BRIDGE:
1388                           map_bridge(bi, d, PCI_PRIMARY_BUS, PCI_SECONDARY_BUS, PCI_SUBORDINATE_BUS);
1389                           break;
1390                         case PCI_HEADER_TYPE_CARDBUS:
1391                           map_bridge(bi, d, PCI_CB_PRIMARY_BUS, PCI_CB_CARD_BUS, PCI_CB_SUBORDINATE_BUS);
1392                           break;
1393                         }
1394                       free(d);
1395                     }
1396                   else if (verbose)
1397                     printf("But it was filtered out.\n");
1398                 }
1399               pci_free_dev(p);
1400             }
1401       }
1402 }
1403
1404 static void
1405 do_map_bridges(int bus, int min, int max)
1406 {
1407   struct bus_info *bi = bus_info + bus;
1408   struct bus_bridge *b;
1409
1410   bi->guestbook = 1;
1411   for(b=bi->bridges; b; b=b->next)
1412     {
1413       if (bus_info[b->first].guestbook)
1414         b->bug = 1;
1415       else if (b->first < min || b->last > max)
1416         b->bug = 2;
1417       else
1418         {
1419           bus_info[b->first].via = b;
1420           do_map_bridges(b->first, b->first, b->last);
1421         }
1422     }
1423 }
1424
1425 static void
1426 map_bridges(void)
1427 {
1428   int i;
1429
1430   printf("\nSummary of buses:\n\n");
1431   for(i=0; i<256; i++)
1432     if (bus_info[i].exists && !bus_info[i].guestbook)
1433       do_map_bridges(i, 0, 255);
1434   for(i=0; i<256; i++)
1435     {
1436       struct bus_info *bi = bus_info + i;
1437       struct bus_bridge *b = bi->via;
1438
1439       if (bi->exists)
1440         {
1441           printf("%02x: ", i);
1442           if (b)
1443             printf("Entered via %02x:%02x.%d\n", b->this, b->dev, b->func);
1444           else if (!i)
1445             printf("Primary host bus\n");
1446           else
1447             printf("Secondary host bus (?)\n");
1448         }
1449       for(b=bi->bridges; b; b=b->next)
1450         {
1451           printf("\t%02x.%d Bridge to %02x-%02x", b->dev, b->func, b->first, b->last);
1452           switch (b->bug)
1453             {
1454             case 1:
1455               printf(" <overlap bug>");
1456               break;
1457             case 2:
1458               printf(" <crossing bug>");
1459               break;
1460             }
1461           putchar('\n');
1462         }
1463     }
1464 }
1465
1466 static void
1467 map_the_bus(void)
1468 {
1469   if (pacc->method == PCI_ACCESS_PROC_BUS_PCI ||
1470       pacc->method == PCI_ACCESS_DUMP)
1471     printf("WARNING: Bus mapping can be reliable only with direct hardware access enabled.\n\n");
1472   else if (!check_root())
1473     die("Only root can map the bus.");
1474   bus_info = xmalloc(sizeof(struct bus_info) * 256);
1475   bzero(bus_info, sizeof(struct bus_info) * 256);
1476   if (filter.bus >= 0)
1477     do_map_bus(filter.bus);
1478   else
1479     {
1480       int bus;
1481       for(bus=0; bus<256; bus++)
1482         do_map_bus(bus);
1483     }
1484   map_bridges();
1485 }
1486
1487 /* Main */
1488
1489 int
1490 main(int argc, char **argv)
1491 {
1492   int i;
1493   char *msg;
1494
1495   if (argc == 2 && !strcmp(argv[1], "--version"))
1496     {
1497       puts("lspci version " PCIUTILS_VERSION);
1498       return 0;
1499     }
1500
1501   pacc = pci_alloc();
1502   pacc->error = die;
1503   pci_filter_init(pacc, &filter);
1504
1505   while ((i = getopt(argc, argv, options)) != -1)
1506     switch (i)
1507       {
1508       case 'n':
1509         pacc->numeric_ids = 1;
1510         break;
1511       case 'v':
1512         verbose++;
1513         break;
1514       case 'b':
1515         pacc->buscentric = 1;
1516         buscentric_view = 1;
1517         break;
1518       case 's':
1519         if (msg = pci_filter_parse_slot(&filter, optarg))
1520           die("-f: %s", msg);
1521         break;
1522       case 'd':
1523         if (msg = pci_filter_parse_id(&filter, optarg))
1524           die("-d: %s", msg);
1525         break;
1526       case 'x':
1527         show_hex++;
1528         break;
1529       case 't':
1530         show_tree++;
1531         break;
1532       case 'i':
1533         pacc->id_file_name = optarg;
1534         break;
1535       case 'm':
1536         machine_readable++;
1537         break;
1538       case 'M':
1539         map_mode++;
1540         break;
1541       default:
1542         if (parse_generic_option(i, pacc, optarg))
1543           break;
1544       bad:
1545         fprintf(stderr, help_msg, pacc->id_file_name);
1546         return 1;
1547       }
1548   if (optind < argc)
1549     goto bad;
1550
1551   pci_init(pacc);
1552   if (map_mode)
1553     map_the_bus();
1554   else
1555     {
1556       scan_devices();
1557       sort_them();
1558       if (show_tree)
1559         show_forest();
1560       else
1561         show();
1562     }
1563   pci_cleanup(pacc);
1564
1565   return 0;
1566 }