]> mj.ucw.cz Git - pciutils.git/blob - lspci.c
23d7d8e52adf542095dd6fa585d0dceaf2847c92
[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++ = 'x';
408         *c++ = '0' + (1 << (i + 2*agp3));
409       }
410   if (c != buf)
411     *c = 0;
412   else
413     strcpy(buf, "<none>");
414 }
415
416 static void
417 show_agp(struct device *d, int where, int cap)
418 {
419   u32 t;
420   char rate[8];
421   int ver, rev;
422   int agp3 = 0;
423
424   ver = (cap >> 4) & 0x0f;
425   rev = cap & 0x0f;
426   printf("AGP version %x.%x\n", ver, rev);
427   if (verbose < 2)
428     return;
429   if (!config_fetch(d, where + PCI_AGP_STATUS, PCI_AGP_SIZEOF - PCI_AGP_STATUS))
430     return;
431   t = get_conf_long(d, where + PCI_AGP_STATUS);
432   if (ver >= 3 && (t & PCI_AGP_STATUS_AGP3))
433     agp3 = 1;
434   format_agp_rate(t & 7, rate, agp3);
435   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",
436          ((t & PCI_AGP_STATUS_RQ_MASK) >> 24U) + 1,
437          FLAG(t, PCI_AGP_STATUS_ISOCH),
438          ((t & PCI_AGP_STATUS_ARQSZ_MASK) >> 13),
439          ((t & PCI_AGP_STATUS_CAL_MASK) >> 10),
440          FLAG(t, PCI_AGP_STATUS_SBA),
441          FLAG(t, PCI_AGP_STATUS_ITA_COH),
442          FLAG(t, PCI_AGP_STATUS_GART64),
443          FLAG(t, PCI_AGP_STATUS_HTRANS),
444          FLAG(t, PCI_AGP_STATUS_64BIT),
445          FLAG(t, PCI_AGP_STATUS_FW),
446          FLAG(t, PCI_AGP_STATUS_AGP3),
447          rate);
448   t = get_conf_long(d, where + PCI_AGP_COMMAND);
449   format_agp_rate(t & 7, rate, agp3);
450   printf("\t\tCommand: RQ=%d ArqSz=%d Cal=%d SBA%c AGP%c GART64%c 64bit%c FW%c Rate=%s\n",
451          ((t & PCI_AGP_COMMAND_RQ_MASK) >> 24U) + 1,
452          ((t & PCI_AGP_COMMAND_ARQSZ_MASK) >> 13),
453          ((t & PCI_AGP_COMMAND_CAL_MASK) >> 10),
454          FLAG(t, PCI_AGP_COMMAND_SBA),
455          FLAG(t, PCI_AGP_COMMAND_AGP),
456          FLAG(t, PCI_AGP_COMMAND_GART64),
457          FLAG(t, PCI_AGP_COMMAND_64BIT),
458          FLAG(t, PCI_AGP_COMMAND_FW),
459          rate);
460 }
461
462 static void
463 show_pcix_nobridge(struct device *d, int where)
464 {
465   u16 command;
466   u32 status;
467
468   printf("PCI-X non-bridge device.\n");
469       
470   if (verbose < 2)
471     return;
472
473   if (!config_fetch(d, where + PCI_PCIX_STATUS, 4))
474     return;
475   
476   command = get_conf_word(d, where + PCI_PCIX_COMMAND);
477   status = get_conf_long(d, where + PCI_PCIX_STATUS);
478   printf("\t\tCommand: DPERE%c ERO%c RBC=%d OST=%d\n",
479          FLAG(command, PCI_PCIX_COMMAND_DPERE),
480          FLAG(command, PCI_PCIX_COMMAND_ERO),
481          ((command & PCI_PCIX_COMMAND_MAX_MEM_READ_BYTE_COUNT) >> 2U),
482          ((command & PCI_PCIX_COMMAND_MAX_OUTSTANDING_SPLIT_TRANS) >> 4U));
483   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",
484          ((status >> 8) & 0xffU), // bus
485          ((status >> 3) & 0x1fU), // dev
486          (status & PCI_PCIX_STATUS_FUNCTION), // function
487          FLAG(status, PCI_PCIX_STATUS_64BIT),
488          FLAG(status, PCI_PCIX_STATUS_133MHZ),
489          FLAG(status, PCI_PCIX_STATUS_SC_DISCARDED),
490          FLAG(status, PCI_PCIX_STATUS_UNEXPECTED_SC),
491          ((status & PCI_PCIX_STATUS_DEVICE_COMPLEXITY) ? "bridge" : "simple"),
492          ((status >> 21) & 3U),
493          ((status >> 23) & 7U),
494          ((status >> 26) & 7U),
495          FLAG(status, PCI_PCIX_STATUS_RCVD_SC_ERR_MESS));
496 }
497
498 static void
499 show_pcix_bridge(struct device *d, int where)
500 {
501
502   u16 secstatus;
503   u32 status, upstcr, downstcr;
504   
505   printf("PCI-X bridge device.\n");
506   
507   if (verbose < 2)
508     return;
509   
510   if (!config_fetch(d, where + PCI_PCIX_BRIDGE_STATUS, 12))
511     return;
512   
513   secstatus = get_conf_word(d, where + PCI_PCIX_BRIDGE_SEC_STATUS);
514   printf("\t\tSecondary Status: 64bit%c, 133MHz%c, SCD%c, USC%c, SCO%c, SRD%c Freq=%d\n",
515          FLAG(secstatus, PCI_PCIX_BRIDGE_SEC_STATUS_64BIT),
516          FLAG(secstatus, PCI_PCIX_BRIDGE_SEC_STATUS_133MHZ),
517          FLAG(secstatus, PCI_PCIX_BRIDGE_SEC_STATUS_SC_DISCARDED),
518          FLAG(secstatus, PCI_PCIX_BRIDGE_SEC_STATUS_UNEXPECTED_SC),
519          FLAG(secstatus, PCI_PCIX_BRIDGE_SEC_STATUS_SC_OVERRUN),
520          FLAG(secstatus, PCI_PCIX_BRIDGE_SEC_STATUS_SPLIT_REQUEST_DELAYED),
521          ((secstatus >> 6) & 7));
522   status = get_conf_long(d, where + PCI_PCIX_BRIDGE_STATUS);
523   printf("\t\tStatus: Bus=%u Dev=%u Func=%u 64bit%c 133MHz%c SCD%c USC%c, SCO%c, SRD%c\n", 
524          ((status >> 8) & 0xff), // bus
525          ((status >> 3) & 0x1f), // dev
526          (status & PCI_PCIX_BRIDGE_STATUS_FUNCTION), // function
527          FLAG(status, PCI_PCIX_BRIDGE_STATUS_64BIT),
528          FLAG(status, PCI_PCIX_BRIDGE_STATUS_133MHZ),
529          FLAG(status, PCI_PCIX_BRIDGE_STATUS_SC_DISCARDED),
530          FLAG(status, PCI_PCIX_BRIDGE_STATUS_UNEXPECTED_SC),
531          FLAG(status, PCI_PCIX_BRIDGE_STATUS_SC_OVERRUN),
532          FLAG(status, PCI_PCIX_BRIDGE_STATUS_SPLIT_REQUEST_DELAYED));
533   upstcr = get_conf_long(d, where + PCI_PCIX_BRIDGE_UPSTREAM_SPLIT_TRANS_CTRL);
534   printf("\t\t: Upstream: Capacity=%u, Commitment Limit=%u\n",
535          (upstcr & PCI_PCIX_BRIDGE_STR_CAPACITY),
536          (upstcr >> 16) & 0xffff);
537   downstcr = get_conf_long(d, where + PCI_PCIX_BRIDGE_DOWNSTREAM_SPLIT_TRANS_CTRL);
538   printf("\t\t: Downstream: Capacity=%u, Commitment Limit=%u\n",
539          (downstcr & PCI_PCIX_BRIDGE_STR_CAPACITY),
540          (downstcr >> 16) & 0xffff);
541 }
542
543 static void
544 show_pcix(struct device *d, int where)
545 {
546   switch (get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f)
547     {
548     case PCI_HEADER_TYPE_NORMAL:
549       show_pcix_nobridge(d, where);
550       break;
551     case PCI_HEADER_TYPE_BRIDGE:
552       show_pcix_bridge(d, where);
553       break;
554     }
555 }
556
557 static void
558 show_rom(struct device *d)
559 {
560   struct pci_dev *p = d->dev;
561   pciaddr_t rom = p->rom_base_addr;
562   pciaddr_t len = (p->known_fields & PCI_FILL_SIZES) ? p->rom_size : 0;
563
564   if (!rom && !len)
565     return;
566   printf("\tExpansion ROM at ");
567   if (rom & PCI_ROM_ADDRESS_MASK)
568     printf(PCIADDR_T_FMT, rom & PCI_ROM_ADDRESS_MASK);
569   else
570     printf("<unassigned>");
571   if (!(rom & PCI_ROM_ADDRESS_ENABLE))
572     printf(" [disabled]");
573   show_size(len);
574   putchar('\n');
575 }
576
577 static void
578 show_msi(struct device *d, int where, int cap)
579 {
580   int is64;
581   u32 t;
582   u16 w;
583
584   printf("Message Signalled Interrupts: 64bit%c Queue=%d/%d Enable%c\n",
585          FLAG(cap, PCI_MSI_FLAGS_64BIT),
586          (cap & PCI_MSI_FLAGS_QSIZE) >> 4,
587          (cap & PCI_MSI_FLAGS_QMASK) >> 1,
588          FLAG(cap, PCI_MSI_FLAGS_ENABLE));
589   if (verbose < 2)
590     return;
591   is64 = cap & PCI_MSI_FLAGS_64BIT;
592   if (!config_fetch(d, where + PCI_MSI_ADDRESS_LO, (is64 ? PCI_MSI_DATA_64 : PCI_MSI_DATA_32) + 2 - PCI_MSI_ADDRESS_LO))
593     return;
594   printf("\t\tAddress: ");
595   if (is64)
596     {
597       t = get_conf_long(d, where + PCI_MSI_ADDRESS_HI);
598       w = get_conf_word(d, where + PCI_MSI_DATA_64);
599       printf("%08x", t);
600     }
601   else
602     w = get_conf_word(d, where + PCI_MSI_DATA_32);
603   t = get_conf_long(d, where + PCI_MSI_ADDRESS_LO);
604   printf("%08x  Data: %04x\n", t, w);
605 }
606
607 static void
608 show_slotid(int cap)
609 {
610   int esr = cap & 0xff;
611   int chs = cap >> 8;
612
613   printf("Slot ID: %d slots, First%c, chassis %02x\n",
614          esr & PCI_SID_ESR_NSLOTS,
615          FLAG(esr, PCI_SID_ESR_FIC),
616          chs);
617 }
618
619 static void
620 show_caps(struct device *d)
621 {
622   if (get_conf_word(d, PCI_STATUS) & PCI_STATUS_CAP_LIST)
623     {
624       int where = get_conf_byte(d, PCI_CAPABILITY_LIST) & ~3;
625       while (where)
626         {
627           int id, next, cap;
628           printf("\tCapabilities: ");
629           if (!config_fetch(d, where, 4))
630             {
631               puts("<available only to root>");
632               break;
633             }
634           id = get_conf_byte(d, where + PCI_CAP_LIST_ID);
635           next = get_conf_byte(d, where + PCI_CAP_LIST_NEXT) & ~3;
636           cap = get_conf_word(d, where + PCI_CAP_FLAGS);
637           printf("[%02x] ", where);
638           if (id == 0xff)
639             {
640               printf("<chain broken>\n");
641               break;
642             }
643           switch (id)
644             {
645             case PCI_CAP_ID_PM:
646               show_pm(d, where, cap);
647               break;
648             case PCI_CAP_ID_AGP:
649               show_agp(d, where, cap);
650               break;
651             case PCI_CAP_ID_VPD:
652               printf("Vital Product Data\n");
653               break;
654             case PCI_CAP_ID_SLOTID:
655               show_slotid(cap);
656               break;
657             case PCI_CAP_ID_MSI:
658               show_msi(d, where, cap);
659               break;
660             case PCI_CAP_ID_PCIX:
661               show_pcix(d, where);
662               break;
663             default:
664               printf("#%02x [%04x]\n", id, cap);
665             }
666           where = next;
667         }
668     }
669 }
670
671 static void
672 show_htype0(struct device *d)
673 {
674   show_bases(d, 6);
675   show_rom(d);
676   show_caps(d);
677 }
678
679 static void
680 show_htype1(struct device *d)
681 {
682   u32 io_base = get_conf_byte(d, PCI_IO_BASE);
683   u32 io_limit = get_conf_byte(d, PCI_IO_LIMIT);
684   u32 io_type = io_base & PCI_IO_RANGE_TYPE_MASK;
685   u32 mem_base = get_conf_word(d, PCI_MEMORY_BASE);
686   u32 mem_limit = get_conf_word(d, PCI_MEMORY_LIMIT);
687   u32 mem_type = mem_base & PCI_MEMORY_RANGE_TYPE_MASK;
688   u32 pref_base = get_conf_word(d, PCI_PREF_MEMORY_BASE);
689   u32 pref_limit = get_conf_word(d, PCI_PREF_MEMORY_LIMIT);
690   u32 pref_type = pref_base & PCI_PREF_RANGE_TYPE_MASK;
691   word sec_stat = get_conf_word(d, PCI_SEC_STATUS);
692   word brc = get_conf_word(d, PCI_BRIDGE_CONTROL);
693   int verb = verbose > 2;
694
695   show_bases(d, 2);
696   printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
697          get_conf_byte(d, PCI_PRIMARY_BUS),
698          get_conf_byte(d, PCI_SECONDARY_BUS),
699          get_conf_byte(d, PCI_SUBORDINATE_BUS),
700          get_conf_byte(d, PCI_SEC_LATENCY_TIMER));
701
702   if (io_type != (io_limit & PCI_IO_RANGE_TYPE_MASK) ||
703       (io_type != PCI_IO_RANGE_TYPE_16 && io_type != PCI_IO_RANGE_TYPE_32))
704     printf("\t!!! Unknown I/O range types %x/%x\n", io_base, io_limit);
705   else
706     {
707       io_base = (io_base & PCI_IO_RANGE_MASK) << 8;
708       io_limit = (io_limit & PCI_IO_RANGE_MASK) << 8;
709       if (io_type == PCI_IO_RANGE_TYPE_32)
710         {
711           io_base |= (get_conf_word(d, PCI_IO_BASE_UPPER16) << 16);
712           io_limit |= (get_conf_word(d, PCI_IO_LIMIT_UPPER16) << 16);
713         }
714       if (io_base <= io_limit || verb)
715         printf("\tI/O behind bridge: %08x-%08x\n", io_base, io_limit+0xfff);
716     }
717
718   if (mem_type != (mem_limit & PCI_MEMORY_RANGE_TYPE_MASK) ||
719       mem_type)
720     printf("\t!!! Unknown memory range types %x/%x\n", mem_base, mem_limit);
721   else
722     {
723       mem_base = (mem_base & PCI_MEMORY_RANGE_MASK) << 16;
724       mem_limit = (mem_limit & PCI_MEMORY_RANGE_MASK) << 16;
725       if (mem_base <= mem_limit || verb)
726         printf("\tMemory behind bridge: %08x-%08x\n", mem_base, mem_limit + 0xfffff);
727     }
728
729   if (pref_type != (pref_limit & PCI_PREF_RANGE_TYPE_MASK) ||
730       (pref_type != PCI_PREF_RANGE_TYPE_32 && pref_type != PCI_PREF_RANGE_TYPE_64))
731     printf("\t!!! Unknown prefetchable memory range types %x/%x\n", pref_base, pref_limit);
732   else
733     {
734       pref_base = (pref_base & PCI_PREF_RANGE_MASK) << 16;
735       pref_limit = (pref_limit & PCI_PREF_RANGE_MASK) << 16;
736       if (pref_base <= pref_limit || verb)
737         {
738           if (pref_type == PCI_PREF_RANGE_TYPE_32)
739             printf("\tPrefetchable memory behind bridge: %08x-%08x\n", pref_base, pref_limit + 0xfffff);
740           else
741             printf("\tPrefetchable memory behind bridge: %08x%08x-%08x%08x\n",
742                    get_conf_long(d, PCI_PREF_BASE_UPPER32),
743                    pref_base,
744                    get_conf_long(d, PCI_PREF_LIMIT_UPPER32),
745                    pref_limit);
746         }
747     }
748
749   if (verbose > 1)
750     printf("\tSecondary status: 66Mhz%c FastB2B%c ParErr%c DEVSEL=%s >TAbort%c <TAbort%c <MAbort%c <SERR%c <PERR%c\n",
751              FLAG(sec_stat, PCI_STATUS_66MHZ),
752              FLAG(sec_stat, PCI_STATUS_FAST_BACK),
753              FLAG(sec_stat, PCI_STATUS_PARITY),
754              ((sec_stat & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
755              ((sec_stat & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
756              ((sec_stat & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??",
757              FLAG(sec_stat, PCI_STATUS_SIG_TARGET_ABORT),
758              FLAG(sec_stat, PCI_STATUS_REC_TARGET_ABORT),
759              FLAG(sec_stat, PCI_STATUS_REC_MASTER_ABORT),
760              FLAG(sec_stat, PCI_STATUS_SIG_SYSTEM_ERROR),
761              FLAG(sec_stat, PCI_STATUS_DETECTED_PARITY));
762
763   show_rom(d);
764
765   if (verbose > 1)
766     printf("\tBridgeCtl: Parity%c SERR%c NoISA%c VGA%c MAbort%c >Reset%c FastB2B%c\n",
767            FLAG(brc, PCI_BRIDGE_CTL_PARITY),
768            FLAG(brc, PCI_BRIDGE_CTL_SERR),
769            FLAG(brc, PCI_BRIDGE_CTL_NO_ISA),
770            FLAG(brc, PCI_BRIDGE_CTL_VGA),
771            FLAG(brc, PCI_BRIDGE_CTL_MASTER_ABORT),
772            FLAG(brc, PCI_BRIDGE_CTL_BUS_RESET),
773            FLAG(brc, PCI_BRIDGE_CTL_FAST_BACK));
774
775   show_caps(d);
776 }
777
778 static void
779 show_htype2(struct device *d)
780 {
781   int i;
782   word cmd = get_conf_word(d, PCI_COMMAND);
783   word brc = get_conf_word(d, PCI_CB_BRIDGE_CONTROL);
784   word exca = get_conf_word(d, PCI_CB_LEGACY_MODE_BASE);
785   int verb = verbose > 2;
786
787   show_bases(d, 1);
788   printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
789          get_conf_byte(d, PCI_CB_PRIMARY_BUS),
790          get_conf_byte(d, PCI_CB_CARD_BUS),
791          get_conf_byte(d, PCI_CB_SUBORDINATE_BUS),
792          get_conf_byte(d, PCI_CB_LATENCY_TIMER));
793   for(i=0; i<2; i++)
794     {
795       int p = 8*i;
796       u32 base = get_conf_long(d, PCI_CB_MEMORY_BASE_0 + p);
797       u32 limit = get_conf_long(d, PCI_CB_MEMORY_LIMIT_0 + p);
798       if (limit > base || verb)
799         printf("\tMemory window %d: %08x-%08x%s%s\n", i, base, limit,
800                (cmd & PCI_COMMAND_MEMORY) ? "" : " [disabled]",
801                (brc & (PCI_CB_BRIDGE_CTL_PREFETCH_MEM0 << i)) ? " (prefetchable)" : "");
802     }
803   for(i=0; i<2; i++)
804     {
805       int p = 8*i;
806       u32 base = get_conf_long(d, PCI_CB_IO_BASE_0 + p);
807       u32 limit = get_conf_long(d, PCI_CB_IO_LIMIT_0 + p);
808       if (!(base & PCI_IO_RANGE_TYPE_32))
809         {
810           base &= 0xffff;
811           limit &= 0xffff;
812         }
813       base &= PCI_CB_IO_RANGE_MASK;
814       limit = (limit & PCI_CB_IO_RANGE_MASK) + 3;
815       if (base <= limit || verb)
816         printf("\tI/O window %d: %08x-%08x%s\n", i, base, limit,
817                (cmd & PCI_COMMAND_IO) ? "" : " [disabled]");
818     }
819
820   if (get_conf_word(d, PCI_CB_SEC_STATUS) & PCI_STATUS_SIG_SYSTEM_ERROR)
821     printf("\tSecondary status: SERR\n");
822   if (verbose > 1)
823     printf("\tBridgeCtl: Parity%c SERR%c ISA%c VGA%c MAbort%c >Reset%c 16bInt%c PostWrite%c\n",
824            FLAG(brc, PCI_CB_BRIDGE_CTL_PARITY),
825            FLAG(brc, PCI_CB_BRIDGE_CTL_SERR),
826            FLAG(brc, PCI_CB_BRIDGE_CTL_ISA),
827            FLAG(brc, PCI_CB_BRIDGE_CTL_VGA),
828            FLAG(brc, PCI_CB_BRIDGE_CTL_MASTER_ABORT),
829            FLAG(brc, PCI_CB_BRIDGE_CTL_CB_RESET),
830            FLAG(brc, PCI_CB_BRIDGE_CTL_16BIT_INT),
831            FLAG(brc, PCI_CB_BRIDGE_CTL_POST_WRITES));
832   if (exca)
833     printf("\t16-bit legacy interface ports at %04x\n", exca);
834 }
835
836 static void
837 show_verbose(struct device *d)
838 {
839   struct pci_dev *p = d->dev;
840   word status = get_conf_word(d, PCI_STATUS);
841   word cmd = get_conf_word(d, PCI_COMMAND);
842   word class = get_conf_word(d, PCI_CLASS_DEVICE);
843   byte bist = get_conf_byte(d, PCI_BIST);
844   byte htype = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
845   byte latency = get_conf_byte(d, PCI_LATENCY_TIMER);
846   byte cache_line = get_conf_byte(d, PCI_CACHE_LINE_SIZE);
847   byte max_lat, min_gnt;
848   byte int_pin = get_conf_byte(d, PCI_INTERRUPT_PIN);
849   unsigned int irq = p->irq;
850   word subsys_v, subsys_d;
851   char ssnamebuf[256];
852
853   show_terse(d);
854
855   switch (htype)
856     {
857     case PCI_HEADER_TYPE_NORMAL:
858       if (class == PCI_CLASS_BRIDGE_PCI)
859         printf("\t!!! Invalid class %04x for header type %02x\n", class, htype);
860       max_lat = get_conf_byte(d, PCI_MAX_LAT);
861       min_gnt = get_conf_byte(d, PCI_MIN_GNT);
862       subsys_v = get_conf_word(d, PCI_SUBSYSTEM_VENDOR_ID);
863       subsys_d = get_conf_word(d, PCI_SUBSYSTEM_ID);
864       break;
865     case PCI_HEADER_TYPE_BRIDGE:
866       if (class != PCI_CLASS_BRIDGE_PCI)
867         printf("\t!!! Invalid class %04x for header type %02x\n", class, htype);
868       irq = int_pin = min_gnt = max_lat = 0;
869       subsys_v = subsys_d = 0;
870       break;
871     case PCI_HEADER_TYPE_CARDBUS:
872       if ((class >> 8) != PCI_BASE_CLASS_BRIDGE)
873         printf("\t!!! Invalid class %04x for header type %02x\n", class, htype);
874       min_gnt = max_lat = 0;
875       subsys_v = get_conf_word(d, PCI_CB_SUBSYSTEM_VENDOR_ID);
876       subsys_d = get_conf_word(d, PCI_CB_SUBSYSTEM_ID);
877       break;
878     default:
879       printf("\t!!! Unknown header type %02x\n", htype);
880       return;
881     }
882
883   if (subsys_v && subsys_v != 0xffff)
884     printf("\tSubsystem: %s\n",
885            pci_lookup_name(pacc, ssnamebuf, sizeof(ssnamebuf),
886                            PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
887                            p->vendor_id, p->device_id, subsys_v, subsys_d));
888
889   if (verbose > 1)
890     {
891       printf("\tControl: I/O%c Mem%c BusMaster%c SpecCycle%c MemWINV%c VGASnoop%c ParErr%c Stepping%c SERR%c FastB2B%c\n",
892              FLAG(cmd, PCI_COMMAND_IO),
893              FLAG(cmd, PCI_COMMAND_MEMORY),
894              FLAG(cmd, PCI_COMMAND_MASTER),
895              FLAG(cmd, PCI_COMMAND_SPECIAL),
896              FLAG(cmd, PCI_COMMAND_INVALIDATE),
897              FLAG(cmd, PCI_COMMAND_VGA_PALETTE),
898              FLAG(cmd, PCI_COMMAND_PARITY),
899              FLAG(cmd, PCI_COMMAND_WAIT),
900              FLAG(cmd, PCI_COMMAND_SERR),
901              FLAG(cmd, PCI_COMMAND_FAST_BACK));
902       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",
903              FLAG(status, PCI_STATUS_CAP_LIST),
904              FLAG(status, PCI_STATUS_66MHZ),
905              FLAG(status, PCI_STATUS_UDF),
906              FLAG(status, PCI_STATUS_FAST_BACK),
907              FLAG(status, PCI_STATUS_PARITY),
908              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
909              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
910              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??",
911              FLAG(status, PCI_STATUS_SIG_TARGET_ABORT),
912              FLAG(status, PCI_STATUS_REC_TARGET_ABORT),
913              FLAG(status, PCI_STATUS_REC_MASTER_ABORT),
914              FLAG(status, PCI_STATUS_SIG_SYSTEM_ERROR),
915              FLAG(status, PCI_STATUS_DETECTED_PARITY));
916       if (cmd & PCI_COMMAND_MASTER)
917         {
918           printf("\tLatency: %d", latency);
919           if (min_gnt || max_lat)
920             {
921               printf(" (");
922               if (min_gnt)
923                 printf("%dns min", min_gnt*250);
924               if (min_gnt && max_lat)
925                 printf(", ");
926               if (max_lat)
927                 printf("%dns max", max_lat*250);
928               putchar(')');
929             }
930           if (cache_line)
931             printf(", Cache Line Size %02x", cache_line);
932           putchar('\n');
933         }
934       if (int_pin || irq)
935         printf("\tInterrupt: pin %c routed to IRQ " PCIIRQ_FMT "\n",
936                (int_pin ? 'A' + int_pin - 1 : '?'), irq);
937     }
938   else
939     {
940       printf("\tFlags: ");
941       if (cmd & PCI_COMMAND_MASTER)
942         printf("bus master, ");
943       if (cmd & PCI_COMMAND_VGA_PALETTE)
944         printf("VGA palette snoop, ");
945       if (cmd & PCI_COMMAND_WAIT)
946         printf("stepping, ");
947       if (cmd & PCI_COMMAND_FAST_BACK)
948         printf("fast Back2Back, ");
949       if (status & PCI_STATUS_66MHZ)
950         printf("66Mhz, ");
951       if (status & PCI_STATUS_UDF)
952         printf("user-definable features, ");
953       printf("%s devsel",
954              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
955              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
956              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??");
957       if (cmd & PCI_COMMAND_MASTER)
958         printf(", latency %d", latency);
959       if (irq)
960         printf(", IRQ " PCIIRQ_FMT, irq);
961       putchar('\n');
962     }
963
964   if (bist & PCI_BIST_CAPABLE)
965     {
966       if (bist & PCI_BIST_START)
967         printf("\tBIST is running\n");
968       else
969         printf("\tBIST result: %02x\n", bist & PCI_BIST_CODE_MASK);
970     }
971
972   switch (htype)
973     {
974     case PCI_HEADER_TYPE_NORMAL:
975       show_htype0(d);
976       break;
977     case PCI_HEADER_TYPE_BRIDGE:
978       show_htype1(d);
979       break;
980     case PCI_HEADER_TYPE_CARDBUS:
981       show_htype2(d);
982       break;
983     }
984 }
985
986 static void
987 show_hex_dump(struct device *d)
988 {
989   unsigned int i;
990
991   for(i=0; i<d->config_cnt; i++)
992     {
993       if (! (i & 15))
994         printf("%02x:", i);
995       printf(" %02x", get_conf_byte(d, i));
996       if ((i & 15) == 15)
997         putchar('\n');
998     }
999 }
1000
1001 static void
1002 show_machine(struct device *d)
1003 {
1004   struct pci_dev *p = d->dev;
1005   int c;
1006   word sv_id=0, sd_id=0;
1007   char classbuf[128], vendbuf[128], devbuf[128], svbuf[128], sdbuf[128];
1008
1009   switch (get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f)
1010     {
1011     case PCI_HEADER_TYPE_NORMAL:
1012       sv_id = get_conf_word(d, PCI_SUBSYSTEM_VENDOR_ID);
1013       sd_id = get_conf_word(d, PCI_SUBSYSTEM_ID);
1014       break;
1015     case PCI_HEADER_TYPE_CARDBUS:
1016       sv_id = get_conf_word(d, PCI_CB_SUBSYSTEM_VENDOR_ID);
1017       sd_id = get_conf_word(d, PCI_CB_SUBSYSTEM_ID);
1018       break;
1019     }
1020
1021   if (verbose)
1022     {
1023       printf("Device:\t");
1024       show_slot_name(d);
1025       putchar('\n');
1026       printf("Class:\t%s\n",
1027              pci_lookup_name(pacc, classbuf, sizeof(classbuf), PCI_LOOKUP_CLASS, get_conf_word(d, PCI_CLASS_DEVICE), 0, 0, 0));
1028       printf("Vendor:\t%s\n",
1029              pci_lookup_name(pacc, vendbuf, sizeof(vendbuf), PCI_LOOKUP_VENDOR, p->vendor_id, p->device_id, 0, 0));
1030       printf("Device:\t%s\n",
1031              pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id, 0, 0));
1032       if (sv_id && sv_id != 0xffff)
1033         {
1034           printf("SVendor:\t%s\n",
1035                  pci_lookup_name(pacc, svbuf, sizeof(svbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR, p->vendor_id, p->device_id, sv_id, sd_id));
1036           printf("SDevice:\t%s\n",
1037                  pci_lookup_name(pacc, sdbuf, sizeof(sdbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id, sv_id, sd_id));
1038         }
1039       if (c = get_conf_byte(d, PCI_REVISION_ID))
1040         printf("Rev:\t%02x\n", c);
1041       if (c = get_conf_byte(d, PCI_CLASS_PROG))
1042         printf("ProgIf:\t%02x\n", c);
1043     }
1044   else
1045     {
1046       show_slot_name(d);
1047       printf(" \"%s\" \"%s\" \"%s\"",
1048              pci_lookup_name(pacc, classbuf, sizeof(classbuf), PCI_LOOKUP_CLASS,
1049                              get_conf_word(d, PCI_CLASS_DEVICE), 0, 0, 0),
1050              pci_lookup_name(pacc, vendbuf, sizeof(vendbuf), PCI_LOOKUP_VENDOR,
1051                              p->vendor_id, p->device_id, 0, 0),
1052              pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_DEVICE,
1053                              p->vendor_id, p->device_id, 0, 0));
1054       if (c = get_conf_byte(d, PCI_REVISION_ID))
1055         printf(" -r%02x", c);
1056       if (c = get_conf_byte(d, PCI_CLASS_PROG))
1057         printf(" -p%02x", c);
1058       if (sv_id && sv_id != 0xffff)
1059         printf(" \"%s\" \"%s\"",
1060                pci_lookup_name(pacc, svbuf, sizeof(svbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR, p->vendor_id, p->device_id, sv_id, sd_id),
1061                pci_lookup_name(pacc, sdbuf, sizeof(sdbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id, sv_id, sd_id));
1062       else
1063         printf(" \"\" \"\"");
1064       putchar('\n');
1065     }
1066 }
1067
1068 static void
1069 show_device(struct device *d)
1070 {
1071   if (machine_readable)
1072     show_machine(d);
1073   else if (verbose)
1074     show_verbose(d);
1075   else
1076     show_terse(d);
1077   if (show_hex)
1078     show_hex_dump(d);
1079   if (verbose || show_hex)
1080     putchar('\n');
1081 }
1082
1083 static void
1084 show(void)
1085 {
1086   struct device *d;
1087
1088   for(d=first_dev; d; d=d->next)
1089     show_device(d);
1090 }
1091
1092 /* Tree output */
1093
1094 struct bridge {
1095   struct bridge *chain;                 /* Single-linked list of bridges */
1096   struct bridge *next, *child;          /* Tree of bridges */
1097   struct bus *first_bus;                /* List of busses connected to this bridge */
1098   unsigned int domain;
1099   unsigned int primary, secondary, subordinate; /* Bus numbers */
1100   struct device *br_dev;
1101 };
1102
1103 struct bus {
1104   unsigned int domain;
1105   unsigned int number;
1106   struct bus *sibling;
1107   struct device *first_dev, **last_dev;
1108 };
1109
1110 static struct bridge host_bridge = { NULL, NULL, NULL, NULL, 0, ~0, 0, ~0, NULL };
1111
1112 static struct bus *
1113 find_bus(struct bridge *b, unsigned int domain, unsigned int n)
1114 {
1115   struct bus *bus;
1116
1117   for(bus=b->first_bus; bus; bus=bus->sibling)
1118     if (bus->domain == domain && bus->number == n)
1119       break;
1120   return bus;
1121 }
1122
1123 static struct bus *
1124 new_bus(struct bridge *b, unsigned int domain, unsigned int n)
1125 {
1126   struct bus *bus = xmalloc(sizeof(struct bus));
1127
1128   bus = xmalloc(sizeof(struct bus));
1129   bus->domain = domain;
1130   bus->number = n;
1131   bus->sibling = b->first_bus;
1132   bus->first_dev = NULL;
1133   bus->last_dev = &bus->first_dev;
1134   b->first_bus = bus;
1135   return bus;
1136 }
1137
1138 static void
1139 insert_dev(struct device *d, struct bridge *b)
1140 {
1141   struct pci_dev *p = d->dev;
1142   struct bus *bus;
1143
1144   if (! (bus = find_bus(b, p->domain, p->bus)))
1145     {
1146       struct bridge *c;
1147       for(c=b->child; c; c=c->next)
1148         if (c->domain == p->domain && c->secondary <= p->bus && p->bus <= c->subordinate)
1149           {
1150             insert_dev(d, c);
1151             return;
1152           }
1153       bus = new_bus(b, p->domain, p->bus);
1154     }
1155   /* Simple insertion at the end _does_ guarantee the correct order as the
1156    * original device list was sorted by (domain, bus, devfn) lexicographically
1157    * and all devices on the new list have the same bus number.
1158    */
1159   *bus->last_dev = d;
1160   bus->last_dev = &d->next;
1161   d->next = NULL;
1162 }
1163
1164 static void
1165 grow_tree(void)
1166 {
1167   struct device *d, *d2;
1168   struct bridge **last_br, *b;
1169
1170   /* Build list of bridges */
1171
1172   last_br = &host_bridge.chain;
1173   for(d=first_dev; d; d=d->next)
1174     {
1175       word class = get_conf_word(d, PCI_CLASS_DEVICE);
1176       byte ht = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
1177       if (class == PCI_CLASS_BRIDGE_PCI &&
1178           (ht == PCI_HEADER_TYPE_BRIDGE || ht == PCI_HEADER_TYPE_CARDBUS))
1179         {
1180           b = xmalloc(sizeof(struct bridge));
1181           b->domain = d->dev->domain;
1182           if (ht == PCI_HEADER_TYPE_BRIDGE)
1183             {
1184               b->primary = get_conf_byte(d, PCI_CB_PRIMARY_BUS);
1185               b->secondary = get_conf_byte(d, PCI_CB_CARD_BUS);
1186               b->subordinate = get_conf_byte(d, PCI_CB_SUBORDINATE_BUS);
1187             }
1188           else
1189             {
1190               b->primary = get_conf_byte(d, PCI_PRIMARY_BUS);
1191               b->secondary = get_conf_byte(d, PCI_SECONDARY_BUS);
1192               b->subordinate = get_conf_byte(d, PCI_SUBORDINATE_BUS);
1193             }
1194           *last_br = b;
1195           last_br = &b->chain;
1196           b->next = b->child = NULL;
1197           b->first_bus = NULL;
1198           b->br_dev = d;
1199         }
1200     }
1201   *last_br = NULL;
1202
1203   /* Create a bridge tree */
1204
1205   for(b=&host_bridge; b; b=b->chain)
1206     {
1207       struct bridge *c, *best;
1208       best = NULL;
1209       for(c=&host_bridge; c; c=c->chain)
1210         if (c != b && (c == &host_bridge || b->domain == c->domain) &&
1211             b->primary >= c->secondary && b->primary <= c->subordinate &&
1212             (!best || best->subordinate - best->primary > c->subordinate - c->primary))
1213           best = c;
1214       if (best)
1215         {
1216           b->next = best->child;
1217           best->child = b;
1218         }
1219     }
1220
1221   /* Insert secondary bus for each bridge */
1222
1223   for(b=&host_bridge; b; b=b->chain)
1224     if (!find_bus(b, b->domain, b->secondary))
1225       new_bus(b, b->domain, b->secondary);
1226
1227   /* Create bus structs and link devices */
1228
1229   for(d=first_dev; d;)
1230     {
1231       d2 = d->next;
1232       insert_dev(d, &host_bridge);
1233       d = d2;
1234     }
1235 }
1236
1237 static void
1238 print_it(byte *line, byte *p)
1239 {
1240   *p++ = '\n';
1241   *p = 0;
1242   fputs(line, stdout);
1243   for(p=line; *p; p++)
1244     if (*p == '+' || *p == '|')
1245       *p = '|';
1246     else
1247       *p = ' ';
1248 }
1249
1250 static void show_tree_bridge(struct bridge *, byte *, byte *);
1251
1252 static void
1253 show_tree_dev(struct device *d, byte *line, byte *p)
1254 {
1255   struct pci_dev *q = d->dev;
1256   struct bridge *b;
1257   char namebuf[256];
1258
1259   p += sprintf(p, "%02x.%x", q->dev, q->func);
1260   for(b=&host_bridge; b; b=b->chain)
1261     if (b->br_dev == d)
1262       {
1263         if (b->secondary == b->subordinate)
1264           p += sprintf(p, "-[%04x:%02x]-", b->domain, b->secondary);
1265         else
1266           p += sprintf(p, "-[%04x:%02x-%02x]-", b->domain, b->secondary, b->subordinate);
1267         show_tree_bridge(b, line, p);
1268         return;
1269       }
1270   if (verbose)
1271     p += sprintf(p, "  %s",
1272                  pci_lookup_name(pacc, namebuf, sizeof(namebuf),
1273                                  PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
1274                                  q->vendor_id, q->device_id, 0, 0));
1275   print_it(line, p);
1276 }
1277
1278 static void
1279 show_tree_bus(struct bus *b, byte *line, byte *p)
1280 {
1281   if (!b->first_dev)
1282     print_it(line, p);
1283   else if (!b->first_dev->next)
1284     {
1285       *p++ = '-';
1286       *p++ = '-';
1287       show_tree_dev(b->first_dev, line, p);
1288     }
1289   else
1290     {
1291       struct device *d = b->first_dev;
1292       while (d->next)
1293         {
1294           p[0] = '+';
1295           p[1] = '-';
1296           show_tree_dev(d, line, p+2);
1297           d = d->next;
1298         }
1299       p[0] = '\\';
1300       p[1] = '-';
1301       show_tree_dev(d, line, p+2);
1302     }
1303 }
1304
1305 static void
1306 show_tree_bridge(struct bridge *b, byte *line, byte *p)
1307 {
1308   *p++ = '-';
1309   if (!b->first_bus->sibling)
1310     {
1311       if (b == &host_bridge)
1312         p += sprintf(p, "[%04x:%02x]-", b->domain, b->first_bus->number);
1313       show_tree_bus(b->first_bus, line, p);
1314     }
1315   else
1316     {
1317       struct bus *u = b->first_bus;
1318       byte *k;
1319
1320       while (u->sibling)
1321         {
1322           k = p + sprintf(p, "+-[%04x:%02x]-", u->domain, u->number);
1323           show_tree_bus(u, line, k);
1324           u = u->sibling;
1325         }
1326       k = p + sprintf(p, "\\-[%04x:%02x]-", u->domain, u->number);
1327       show_tree_bus(u, line, k);
1328     }
1329 }
1330
1331 static void
1332 show_forest(void)
1333 {
1334   char line[256];
1335
1336   grow_tree();
1337   show_tree_bridge(&host_bridge, line, line);
1338 }
1339
1340 /* Bus mapping mode */
1341
1342 struct bus_bridge {
1343   struct bus_bridge *next;
1344   byte this, dev, func, first, last, bug;
1345 };
1346
1347 struct bus_info {
1348   byte exists;
1349   byte guestbook;
1350   struct bus_bridge *bridges, *via;
1351 };
1352
1353 static struct bus_info *bus_info;
1354
1355 static void
1356 map_bridge(struct bus_info *bi, struct device *d, int np, int ns, int nl)
1357 {
1358   struct bus_bridge *b = xmalloc(sizeof(struct bus_bridge));
1359   struct pci_dev *p = d->dev;
1360
1361   b->next = bi->bridges;
1362   bi->bridges = b;
1363   b->this = get_conf_byte(d, np);
1364   b->dev = p->dev;
1365   b->func = p->func;
1366   b->first = get_conf_byte(d, ns);
1367   b->last = get_conf_byte(d, nl);
1368   printf("## %02x.%02x:%d is a bridge from %02x to %02x-%02x\n",
1369          p->bus, p->dev, p->func, b->this, b->first, b->last);
1370   if (b->this != p->bus)
1371     printf("!!! Bridge points to invalid primary bus.\n");
1372   if (b->first > b->last)
1373     {
1374       printf("!!! Bridge points to invalid bus range.\n");
1375       b->last = b->first;
1376     }
1377 }
1378
1379 static void
1380 do_map_bus(int bus)
1381 {
1382   int dev, func;
1383   int verbose = pacc->debugging;
1384   struct bus_info *bi = bus_info + bus;
1385   struct device *d;
1386
1387   if (verbose)
1388     printf("Mapping bus %02x\n", bus);
1389   for(dev = 0; dev < 32; dev++)
1390     if (filter.slot < 0 || filter.slot == dev)
1391       {
1392         int func_limit = 1;
1393         for(func = 0; func < func_limit; func++)
1394           if (filter.func < 0 || filter.func == func)
1395             {
1396               /* XXX: Bus mapping supports only domain 0 */
1397               struct pci_dev *p = pci_get_dev(pacc, 0, bus, dev, func);
1398               u16 vendor = pci_read_word(p, PCI_VENDOR_ID);
1399               if (vendor && vendor != 0xffff)
1400                 {
1401                   if (!func && (pci_read_byte(p, PCI_HEADER_TYPE) & 0x80))
1402                     func_limit = 8;
1403                   if (verbose)
1404                     printf("Discovered device %02x:%02x.%d\n", bus, dev, func);
1405                   bi->exists = 1;
1406                   if (d = scan_device(p))
1407                     {
1408                       show_device(d);
1409                       switch (get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f)
1410                         {
1411                         case PCI_HEADER_TYPE_BRIDGE:
1412                           map_bridge(bi, d, PCI_PRIMARY_BUS, PCI_SECONDARY_BUS, PCI_SUBORDINATE_BUS);
1413                           break;
1414                         case PCI_HEADER_TYPE_CARDBUS:
1415                           map_bridge(bi, d, PCI_CB_PRIMARY_BUS, PCI_CB_CARD_BUS, PCI_CB_SUBORDINATE_BUS);
1416                           break;
1417                         }
1418                       free(d);
1419                     }
1420                   else if (verbose)
1421                     printf("But it was filtered out.\n");
1422                 }
1423               pci_free_dev(p);
1424             }
1425       }
1426 }
1427
1428 static void
1429 do_map_bridges(int bus, int min, int max)
1430 {
1431   struct bus_info *bi = bus_info + bus;
1432   struct bus_bridge *b;
1433
1434   bi->guestbook = 1;
1435   for(b=bi->bridges; b; b=b->next)
1436     {
1437       if (bus_info[b->first].guestbook)
1438         b->bug = 1;
1439       else if (b->first < min || b->last > max)
1440         b->bug = 2;
1441       else
1442         {
1443           bus_info[b->first].via = b;
1444           do_map_bridges(b->first, b->first, b->last);
1445         }
1446     }
1447 }
1448
1449 static void
1450 map_bridges(void)
1451 {
1452   int i;
1453
1454   printf("\nSummary of buses:\n\n");
1455   for(i=0; i<256; i++)
1456     if (bus_info[i].exists && !bus_info[i].guestbook)
1457       do_map_bridges(i, 0, 255);
1458   for(i=0; i<256; i++)
1459     {
1460       struct bus_info *bi = bus_info + i;
1461       struct bus_bridge *b = bi->via;
1462
1463       if (bi->exists)
1464         {
1465           printf("%02x: ", i);
1466           if (b)
1467             printf("Entered via %02x:%02x.%d\n", b->this, b->dev, b->func);
1468           else if (!i)
1469             printf("Primary host bus\n");
1470           else
1471             printf("Secondary host bus (?)\n");
1472         }
1473       for(b=bi->bridges; b; b=b->next)
1474         {
1475           printf("\t%02x.%d Bridge to %02x-%02x", b->dev, b->func, b->first, b->last);
1476           switch (b->bug)
1477             {
1478             case 1:
1479               printf(" <overlap bug>");
1480               break;
1481             case 2:
1482               printf(" <crossing bug>");
1483               break;
1484             }
1485           putchar('\n');
1486         }
1487     }
1488 }
1489
1490 static void
1491 map_the_bus(void)
1492 {
1493   if (pacc->method == PCI_ACCESS_PROC_BUS_PCI ||
1494       pacc->method == PCI_ACCESS_DUMP)
1495     printf("WARNING: Bus mapping can be reliable only with direct hardware access enabled.\n\n");
1496   else if (!check_root())
1497     die("Only root can map the bus.");
1498   bus_info = xmalloc(sizeof(struct bus_info) * 256);
1499   bzero(bus_info, sizeof(struct bus_info) * 256);
1500   if (filter.bus >= 0)
1501     do_map_bus(filter.bus);
1502   else
1503     {
1504       int bus;
1505       for(bus=0; bus<256; bus++)
1506         do_map_bus(bus);
1507     }
1508   map_bridges();
1509 }
1510
1511 /* Main */
1512
1513 int
1514 main(int argc, char **argv)
1515 {
1516   int i;
1517   char *msg;
1518
1519   if (argc == 2 && !strcmp(argv[1], "--version"))
1520     {
1521       puts("lspci version " PCIUTILS_VERSION);
1522       return 0;
1523     }
1524
1525   pacc = pci_alloc();
1526   pacc->error = die;
1527   pci_filter_init(pacc, &filter);
1528
1529   while ((i = getopt(argc, argv, options)) != -1)
1530     switch (i)
1531       {
1532       case 'n':
1533         pacc->numeric_ids = 1;
1534         break;
1535       case 'v':
1536         verbose++;
1537         break;
1538       case 'b':
1539         pacc->buscentric = 1;
1540         buscentric_view = 1;
1541         break;
1542       case 's':
1543         if (msg = pci_filter_parse_slot(&filter, optarg))
1544           die("-s: %s", msg);
1545         break;
1546       case 'd':
1547         if (msg = pci_filter_parse_id(&filter, optarg))
1548           die("-d: %s", msg);
1549         break;
1550       case 'x':
1551         show_hex++;
1552         break;
1553       case 't':
1554         show_tree++;
1555         break;
1556       case 'i':
1557         pacc->id_file_name = optarg;
1558         break;
1559       case 'm':
1560         machine_readable++;
1561         break;
1562       case 'M':
1563         map_mode++;
1564         break;
1565       default:
1566         if (parse_generic_option(i, pacc, optarg))
1567           break;
1568       bad:
1569         fprintf(stderr, help_msg, pacc->id_file_name);
1570         return 1;
1571       }
1572   if (optind < argc)
1573     goto bad;
1574
1575   pci_init(pacc);
1576   if (map_mode)
1577     map_the_bus();
1578   else
1579     {
1580       scan_devices();
1581       sort_them();
1582       if (show_tree)
1583         show_forest();
1584       else
1585         show();
1586     }
1587   pci_cleanup(pacc);
1588
1589   return 0;
1590 }