]> mj.ucw.cz Git - pciutils.git/blob - lspci.c
e5ff280d0df4b614a7d74b4e2566e9ce7801cdcf
[pciutils.git] / lspci.c
1 /*
2  *      $Id: lspci.c,v 1.21 1999/01/27 14:52:55 mj Exp $
3  *
4  *      Linux PCI Utilities -- List All PCI Devices
5  *
6  *      Copyright (c) 1997--1999 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
7  *
8  *      Can be freely distributed and used under the terms of the GNU GPL.
9  */
10
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <stdarg.h>
15 #include <unistd.h>
16
17 #include "pciutils.h"
18
19 /* Options */
20
21 static int verbose;                     /* Show detailed information */
22 static int buscentric_view;             /* Show bus addresses/IRQ's instead of CPU-visible ones */
23 static int show_hex;                    /* Show contents of config space as hexadecimal numbers */
24 static struct pci_filter filter;        /* Device filter */
25 static int show_tree;                   /* Show bus tree */
26 static int machine_readable;            /* Generate machine-readable output */
27 static int map_mode;                    /* Bus mapping mode enabled */
28
29 static char options[] = "nvbxs:d:ti:mgM" GENERIC_OPTIONS ;
30
31 static char help_msg[] = "\
32 Usage: lspci [<switches>]\n\
33 \n\
34 -v\t\tBe verbose\n\
35 -n\t\tShow numeric ID's\n\
36 -b\t\tBus-centric view (PCI addresses and IRQ's instead of those seen by the CPU)\n\
37 -x\t\tShow hex-dump of config space\n\
38 -s [[<bus>]:][<slot>][.[<func>]]\tShow only devices in selected slots\n\
39 -d [<vendor>]:[<device>]\tShow only selected devices\n\
40 -t\t\tShow bus tree\n\
41 -m\t\tProduce machine-readable output\n\
42 -i <file>\tUse specified ID database instead of %s\n\
43 -M\t\tEnable `bus mapping' mode (dangerous; root only)\n"
44 GENERIC_HELP
45 ;
46
47 /* Communication with libpci */
48
49 static struct pci_access *pacc;
50
51 /* Format strings used for IRQ numbers and memory addresses */
52
53 #ifdef ARCH_SPARC64
54 #define IRQ_FORMAT "%08x"
55 #else
56 #define IRQ_FORMAT "%d"
57 #endif
58
59 #ifdef HAVE_64BIT_LONG_INT
60 #define LONG_FORMAT "%016lx"
61 #else
62 #define LONG_FORMAT "%08lx"
63 #endif
64
65 /* Our view of the PCI bus */
66
67 struct device {
68   struct device *next;
69   struct pci_dev *dev;
70   unsigned int config_cnt;
71   byte config[256];
72 };
73
74 static struct device *first_dev;
75
76 static struct device *
77 scan_device(struct pci_dev *p)
78 {
79   int how_much = (show_hex > 2) ? 256 : 64;
80   struct device *d;
81
82   if (!pci_filter_match(&filter, p))
83     return NULL;
84   d = xmalloc(sizeof(struct device));
85   bzero(d, sizeof(*d));
86   d->dev = p;
87   if (!pci_read_block(p, 0, d->config, how_much))
88     die("Unable to read %d bytes of configuration space.", how_much);
89   if (how_much < 128 && (d->config[PCI_HEADER_TYPE] & 0x7f) == PCI_HEADER_TYPE_CARDBUS)
90     {
91       /* For cardbus bridges, we need to fetch 64 bytes more to get the full standard header... */
92       if (!pci_read_block(p, 0, d->config+64, 64))
93         die("Unable to read cardbus bridge extension data.");
94       how_much = 128;
95     }
96   d->config_cnt = how_much;
97   pci_setup_cache(p, d->config, d->config_cnt);
98   pci_fill_info(p, PCI_FILL_IDENT | PCI_FILL_IRQ | PCI_FILL_BASES | PCI_FILL_ROM_BASE);
99   return d;
100 }
101
102 static void
103 scan_devices(void)
104 {
105   struct device *d;
106   struct pci_dev *p;
107
108   pci_scan_bus(pacc);
109   for(p=pacc->devices; p; p=p->next)
110     if (d = scan_device(p))
111       {
112         d->next = first_dev;
113         first_dev = d;
114       }
115 }
116
117 static int
118 check_root(void)
119 {
120   static int is_root = -1;
121
122   if (is_root < 0)
123     is_root = !geteuid();
124   return is_root;
125 }
126
127 static int
128 config_fetch(struct device *d, unsigned int pos, unsigned int len)
129 {
130   if (pos + len < d->config_cnt)
131     return 1;
132   if (pacc->method != PCI_ACCESS_DUMP && !check_root())
133     return 0;
134   return pci_read_block(d->dev, pos, d->config + pos, len);
135 }
136
137 /* Config space accesses */
138
139 static inline byte
140 get_conf_byte(struct device *d, unsigned int pos)
141 {
142   return d->config[pos];
143 }
144
145 static word
146 get_conf_word(struct device *d, unsigned int pos)
147 {
148   return d->config[pos] | (d->config[pos+1] << 8);
149 }
150
151 static u32
152 get_conf_long(struct device *d, unsigned int pos)
153 {
154   return d->config[pos] |
155     (d->config[pos+1] << 8) |
156     (d->config[pos+2] << 16) |
157     (d->config[pos+3] << 24);
158 }
159
160 /* Sorting */
161
162 static int
163 compare_them(const void *A, const void *B)
164 {
165   const struct pci_dev *a = (*(const struct device **)A)->dev;
166   const struct pci_dev *b = (*(const struct device **)B)->dev;
167
168   if (a->bus < b->bus)
169     return -1;
170   if (a->bus > b->bus)
171     return 1;
172   if (a->dev < b->dev)
173     return -1;
174   if (a->dev > b->dev)
175     return 1;
176   if (a->func < b->func)
177     return -1;
178   if (a->func > b->func)
179     return 1;
180   return 0;
181 }
182
183 static void
184 sort_them(void)
185 {
186   struct device **index, **h, **last_dev;
187   int cnt;
188   struct device *d;
189
190   cnt = 0;
191   for(d=first_dev; d; d=d->next)
192     cnt++;
193   h = index = alloca(sizeof(struct device *) * cnt);
194   for(d=first_dev; d; d=d->next)
195     *h++ = d;
196   qsort(index, cnt, sizeof(struct device *), compare_them);
197   last_dev = &first_dev;
198   h = index;
199   while (cnt--)
200     {
201       *last_dev = *h;
202       last_dev = &(*h)->next;
203       h++;
204     }
205   *last_dev = NULL;
206 }
207
208 /* Normal output */
209
210 static void
211 show_terse(struct device *d)
212 {
213   int c;
214   struct pci_dev *p = d->dev;
215   byte classbuf[128], devbuf[128];
216
217   printf("%02x:%02x.%x %s: %s",
218          p->bus,
219          p->dev,
220          p->func,
221          pci_lookup_name(pacc, classbuf, sizeof(classbuf),
222                          PCI_LOOKUP_CLASS,
223                          get_conf_word(d, PCI_CLASS_DEVICE), 0),
224          pci_lookup_name(pacc, devbuf, sizeof(devbuf),
225                          PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
226                          p->vendor_id, p->device_id));
227   if (c = get_conf_byte(d, PCI_REVISION_ID))
228     printf(" (rev %02x)", c);
229   if (verbose && (c = get_conf_byte(d, PCI_CLASS_PROG)))
230     printf(" (prog-if %02x)", c);
231   putchar('\n');
232 }
233
234 static void
235 show_bases(struct device *d, int cnt)
236 {
237   struct pci_dev *p = d->dev;
238   word cmd = get_conf_word(d, PCI_COMMAND);
239   int i;
240
241   for(i=0; i<cnt; i++)
242     {
243       unsigned long pos;
244       unsigned int flg = get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i);
245       pos = p->base_addr[i];
246       if (flg == 0xffffffff)
247         flg = 0;
248       if (!pos && !flg)
249         continue;
250       if (verbose > 1)
251         printf("\tRegion %d: ", i);
252       else
253         putchar('\t');
254       if (pos && !flg)                  /* Reported by the OS, but not by the device */
255         {
256           printf("[virtual] ");
257           flg = pos;
258         }
259       if (flg & PCI_BASE_ADDRESS_SPACE_IO)
260         {
261           unsigned long a = pos & PCI_BASE_ADDRESS_IO_MASK;
262           printf("I/O ports at ");
263           if (a)
264             printf("%04lx", a);
265           else if (flg & PCI_BASE_ADDRESS_IO_MASK)
266             printf("<ignored>");
267           else
268             printf("<unassigned>");
269           if (!(cmd & PCI_COMMAND_IO))
270             printf(" [disabled]");
271         }
272       else
273         {
274           int t = flg & PCI_BASE_ADDRESS_MEM_TYPE_MASK;
275           unsigned long a = pos & PCI_BASE_ADDRESS_MEM_MASK;
276           int done = 0;
277           u32 z = 0;
278
279           printf("Memory at ");
280           if (t == PCI_BASE_ADDRESS_MEM_TYPE_64)
281             {
282               if (i >= cnt - 1)
283                 {
284                   printf("<invalid-64bit-slot>\n");
285                   done = 1;
286                 }
287               else
288                 {
289                   i++;
290                   z = get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i);
291                   if (buscentric_view)
292                     {
293                       if (a || z)
294                         printf("%08x%08lx", z, a);
295                       else
296                         printf("<unassigned>");
297                       done = 1;
298                     }
299                 }
300             }
301           if (!done)
302             {
303               if (a)
304                 printf(LONG_FORMAT, a);
305               else
306                 printf(((flg & PCI_BASE_ADDRESS_MEM_MASK) || z) ? "<ignored>" : "<unassigned>");
307             }
308           printf(" (%s, %sprefetchable)",
309                  (t == PCI_BASE_ADDRESS_MEM_TYPE_32) ? "32-bit" :
310                  (t == PCI_BASE_ADDRESS_MEM_TYPE_64) ? "64-bit" :
311                  (t == PCI_BASE_ADDRESS_MEM_TYPE_1M) ? "low-1M" : "type 3",
312                  (flg & PCI_BASE_ADDRESS_MEM_PREFETCH) ? "" : "non-");
313           if (!(cmd & PCI_COMMAND_MEMORY))
314             printf(" [disabled]");
315         }
316       putchar('\n');
317     }
318 }
319
320 static void
321 show_htype0(struct device *d)
322 {
323   unsigned long rom = d->dev->rom_base_addr;
324
325   show_bases(d, 6);
326   if (rom & 1)
327     printf("\tExpansion ROM at %08lx%s\n", rom & PCI_ROM_ADDRESS_MASK,
328            (rom & PCI_ROM_ADDRESS_ENABLE) ? "" : " [disabled]");
329   if (get_conf_word(d, PCI_STATUS) & PCI_STATUS_CAP_LIST)
330     {
331       int where = get_conf_byte(d, PCI_CAPABILITY_LIST);
332       while (where)
333         {
334           int id, next, ver;
335           printf("\tCapabilities: ");
336           if (!config_fetch(d, where, 4))
337             {
338               puts("<available only to root>");
339               break;
340             }
341           id = get_conf_byte(d, where);
342           next = get_conf_byte(d, where+1);
343           ver = get_conf_byte(d, where+2);
344           if (id == 0xff)
345             {
346               printf("<chain broken at %02x>\n", where);
347               break;
348             }
349           switch (id)
350             {
351             case 2:
352               printf("AGP");
353               break;
354             default:
355               printf("C%02x", id);
356             }
357           printf(" version %x.%x at %02x\n", ver/16, ver%16, where);
358           where = next;
359         }
360     }
361 }
362
363 static void
364 show_htype1(struct device *d)
365 {
366   struct pci_dev *p = d->dev;
367   u32 io_base = get_conf_byte(d, PCI_IO_BASE);
368   u32 io_limit = get_conf_byte(d, PCI_IO_LIMIT);
369   u32 io_type = io_base & PCI_IO_RANGE_TYPE_MASK;
370   u32 mem_base = get_conf_word(d, PCI_MEMORY_BASE);
371   u32 mem_limit = get_conf_word(d, PCI_MEMORY_LIMIT);
372   u32 mem_type = mem_base & PCI_MEMORY_RANGE_TYPE_MASK;
373   u32 pref_base = get_conf_word(d, PCI_PREF_MEMORY_BASE);
374   u32 pref_limit = get_conf_word(d, PCI_PREF_MEMORY_LIMIT);
375   u32 pref_type = pref_base & PCI_PREF_RANGE_TYPE_MASK;
376   unsigned long rom = p->rom_base_addr;
377   word brc = get_conf_word(d, PCI_BRIDGE_CONTROL);
378
379   show_bases(d, 2);
380   printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
381          get_conf_byte(d, PCI_PRIMARY_BUS),
382          get_conf_byte(d, PCI_SECONDARY_BUS),
383          get_conf_byte(d, PCI_SUBORDINATE_BUS),
384          get_conf_byte(d, PCI_SEC_LATENCY_TIMER));
385
386   if (io_type != (io_limit & PCI_IO_RANGE_TYPE_MASK) ||
387       (io_type != PCI_IO_RANGE_TYPE_16 && io_type != PCI_IO_RANGE_TYPE_32))
388     printf("\t!!! Unknown I/O range types %x/%x\n", io_base, io_limit);
389   else
390     {
391       io_base = (io_base & PCI_IO_RANGE_MASK) << 8;
392       io_limit = (io_limit & PCI_IO_RANGE_MASK) << 8;
393       if (io_type == PCI_IO_RANGE_TYPE_32)
394         {
395           io_base |= (get_conf_word(d, PCI_IO_BASE_UPPER16) << 16);
396           io_limit |= (get_conf_word(d, PCI_IO_LIMIT_UPPER16) << 16);
397         }
398       if (io_base)
399         printf("\tI/O behind bridge: %08x-%08x\n", io_base, io_limit+0xfff);
400     }
401
402   if (mem_type != (mem_limit & PCI_MEMORY_RANGE_TYPE_MASK) ||
403       mem_type)
404     printf("\t!!! Unknown memory range types %x/%x\n", mem_base, mem_limit);
405   else if (mem_base)
406     {
407       mem_base = (mem_base & PCI_MEMORY_RANGE_MASK) << 16;
408       mem_limit = (mem_limit & PCI_MEMORY_RANGE_MASK) << 16;
409       printf("\tMemory behind bridge: %08x-%08x\n", mem_base, mem_limit + 0xfffff);
410     }
411
412   if (pref_type != (pref_limit & PCI_PREF_RANGE_TYPE_MASK) ||
413       (pref_type != PCI_PREF_RANGE_TYPE_32 && pref_type != PCI_PREF_RANGE_TYPE_64))
414     printf("\t!!! Unknown prefetchable memory range types %x/%x\n", pref_base, pref_limit);
415   else if (pref_base)
416     {
417       pref_base = (pref_base & PCI_PREF_RANGE_MASK) << 16;
418       pref_limit = (pref_limit & PCI_PREF_RANGE_MASK) << 16;
419       if (pref_type == PCI_PREF_RANGE_TYPE_32)
420         printf("\tPrefetchable memory behind bridge: %08x-%08x\n", pref_base, pref_limit + 0xfffff);
421       else
422         printf("\tPrefetchable memory behind bridge: %08x%08x-%08x%08x\n",
423                get_conf_long(d, PCI_PREF_BASE_UPPER32),
424                pref_base,
425                get_conf_long(d, PCI_PREF_LIMIT_UPPER32),
426                pref_limit);
427     }
428
429   if (get_conf_word(d, PCI_SEC_STATUS) & PCI_STATUS_SIG_SYSTEM_ERROR)
430     printf("\tSecondary status: SERR\n");
431
432   if (rom & 1)
433     printf("\tExpansion ROM at %08lx%s\n", rom & PCI_ROM_ADDRESS_MASK,
434            (rom & PCI_ROM_ADDRESS_ENABLE) ? "" : " [disabled]");
435
436   if (verbose > 1)
437     printf("\tBridgeCtl: Parity%c SERR%c NoISA%c VGA%c MAbort%c >Reset%c FastB2B%c\n",
438            (brc & PCI_BRIDGE_CTL_PARITY) ? '+' : '-',
439            (brc & PCI_BRIDGE_CTL_SERR) ? '+' : '-',
440            (brc & PCI_BRIDGE_CTL_NO_ISA) ? '+' : '-',
441            (brc & PCI_BRIDGE_CTL_VGA) ? '+' : '-',
442            (brc & PCI_BRIDGE_CTL_MASTER_ABORT) ? '+' : '-',
443            (brc & PCI_BRIDGE_CTL_BUS_RESET) ? '+' : '-',
444            (brc & PCI_BRIDGE_CTL_FAST_BACK) ? '+' : '-');
445 }
446
447 static void
448 show_htype2(struct device *d)
449 {
450   int i;
451   word cmd = get_conf_word(d, PCI_COMMAND);
452   word brc = get_conf_word(d, PCI_CB_BRIDGE_CONTROL);
453   word exca = get_conf_word(d, PCI_CB_LEGACY_MODE_BASE);
454
455   show_bases(d, 1);
456   printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
457          get_conf_byte(d, PCI_CB_PRIMARY_BUS),
458          get_conf_byte(d, PCI_CB_CARD_BUS),
459          get_conf_byte(d, PCI_CB_SUBORDINATE_BUS),
460          get_conf_byte(d, PCI_CB_LATENCY_TIMER));
461   for(i=0; i<2; i++)
462     {
463       int p = 8*i;
464       u32 base = get_conf_long(d, PCI_CB_MEMORY_BASE_0 + p);
465       u32 limit = get_conf_long(d, PCI_CB_MEMORY_LIMIT_0 + p);
466       if (limit > base)
467         printf("Memory window %d: %08x-%08x%s%s\n", i, base, limit,
468                (cmd & PCI_COMMAND_MEMORY) ? "" : " [disabled]",
469                (brc & (PCI_CB_BRIDGE_CTL_PREFETCH_MEM0 << i)) ? " (prefetchable)" : "");
470     }
471   for(i=0; i<2; i++)
472     {
473       int p = 8*i;
474       u32 base = get_conf_long(d, PCI_CB_IO_BASE_0 + p);
475       u32 limit = get_conf_long(d, PCI_CB_IO_LIMIT_0 + p);
476       if (!(base & PCI_IO_RANGE_TYPE_32))
477         {
478           base &= 0xffff;
479           limit &= 0xffff;
480         }
481       base &= PCI_CB_IO_RANGE_MASK;
482       if (!base)
483         continue;
484       limit = (limit & PCI_CB_IO_RANGE_MASK) + 3;
485       printf("I/O window %d: %08x-%08x%s\n", i, base, limit,
486              (cmd & PCI_COMMAND_IO) ? "" : " [disabled]");
487     }
488
489   if (get_conf_word(d, PCI_CB_SEC_STATUS) & PCI_STATUS_SIG_SYSTEM_ERROR)
490     printf("\tSecondary status: SERR\n");
491   if (verbose > 1)
492     printf("\tBridgeCtl: Parity%c SERR%c ISA%c VGA%c MAbort%c >Reset%c 16bInt%c PostWrite%c\n",
493            (brc & PCI_CB_BRIDGE_CTL_PARITY) ? '+' : '-',
494            (brc & PCI_CB_BRIDGE_CTL_SERR) ? '+' : '-',
495            (brc & PCI_CB_BRIDGE_CTL_ISA) ? '+' : '-',
496            (brc & PCI_CB_BRIDGE_CTL_VGA) ? '+' : '-',
497            (brc & PCI_CB_BRIDGE_CTL_MASTER_ABORT) ? '+' : '-',
498            (brc & PCI_CB_BRIDGE_CTL_CB_RESET) ? '+' : '-',
499            (brc & PCI_CB_BRIDGE_CTL_16BIT_INT) ? '+' : '-',
500            (brc & PCI_CB_BRIDGE_CTL_POST_WRITES) ? '+' : '-');
501   if (exca)
502     printf("\t16-bit legacy interface ports at %04x\n", exca);
503 }
504
505 static void
506 show_verbose(struct device *d)
507 {
508   struct pci_dev *p = d->dev;
509   word status = get_conf_word(d, PCI_STATUS);
510   word cmd = get_conf_word(d, PCI_COMMAND);
511   word class = get_conf_word(d, PCI_CLASS_DEVICE);
512   byte bist = get_conf_byte(d, PCI_BIST);
513   byte htype = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
514   byte latency = get_conf_byte(d, PCI_LATENCY_TIMER);
515   byte cache_line = get_conf_byte(d, PCI_CACHE_LINE_SIZE);
516   byte max_lat, min_gnt;
517   byte int_pin = get_conf_byte(d, PCI_INTERRUPT_PIN);
518   unsigned int irq = p->irq;
519   word subsys_v, subsys_d;
520   char ssnamebuf[256];
521
522   show_terse(d);
523
524   switch (htype)
525     {
526     case PCI_HEADER_TYPE_NORMAL:
527       if (class == PCI_CLASS_BRIDGE_PCI)
528         {
529         badhdr:
530           printf("\t!!! Header type %02x doesn't match class code %04x\n", htype, class);
531           return;
532         }
533       max_lat = get_conf_byte(d, PCI_MAX_LAT);
534       min_gnt = get_conf_byte(d, PCI_MIN_GNT);
535       subsys_v = get_conf_word(d, PCI_SUBSYSTEM_VENDOR_ID);
536       subsys_d = get_conf_word(d, PCI_SUBSYSTEM_ID);
537       break;
538     case PCI_HEADER_TYPE_BRIDGE:
539       if (class != PCI_CLASS_BRIDGE_PCI)
540         goto badhdr;
541       irq = int_pin = min_gnt = max_lat = 0;
542       subsys_v = subsys_d = 0;
543       break;
544     case PCI_HEADER_TYPE_CARDBUS:
545       if ((class >> 8) != PCI_BASE_CLASS_BRIDGE)
546         goto badhdr;
547       min_gnt = max_lat = 0;
548       subsys_v = get_conf_word(d, PCI_CB_SUBSYSTEM_VENDOR_ID);
549       subsys_d = get_conf_word(d, PCI_CB_SUBSYSTEM_ID);
550       break;
551     default:
552       printf("\t!!! Unknown header type %02x\n", htype);
553       return;
554     }
555
556   if (verbose && subsys_v && subsys_v != 0xffff)
557     printf("\tSubsystem: %s\n",
558            pci_lookup_name(pacc, ssnamebuf, sizeof(ssnamebuf),
559                            PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
560                            subsys_v, subsys_d));
561
562   if (verbose > 1)
563     {
564       printf("\tControl: I/O%c Mem%c BusMaster%c SpecCycle%c MemWINV%c VGASnoop%c ParErr%c Stepping%c SERR%c FastB2B%c\n",
565              (cmd & PCI_COMMAND_IO) ? '+' : '-',
566              (cmd & PCI_COMMAND_MEMORY) ? '+' : '-',
567              (cmd & PCI_COMMAND_MASTER) ? '+' : '-',
568              (cmd & PCI_COMMAND_SPECIAL) ? '+' : '-',
569              (cmd & PCI_COMMAND_INVALIDATE) ? '+' : '-',
570              (cmd & PCI_COMMAND_VGA_PALETTE) ? '+' : '-',
571              (cmd & PCI_COMMAND_PARITY) ? '+' : '-',
572              (cmd & PCI_COMMAND_WAIT) ? '+' : '-',
573              (cmd & PCI_COMMAND_SERR) ? '+' : '-',
574              (cmd & PCI_COMMAND_FAST_BACK) ? '+' : '-');
575       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",
576              (status & PCI_STATUS_CAP_LIST) ? '+' : '-',
577              (status & PCI_STATUS_66MHZ) ? '+' : '-',
578              (status & PCI_STATUS_UDF) ? '+' : '-',
579              (status & PCI_STATUS_FAST_BACK) ? '+' : '-',
580              (status & PCI_STATUS_PARITY) ? '+' : '-',
581              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
582              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
583              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??",
584              (status & PCI_STATUS_SIG_TARGET_ABORT) ? '+' : '-',
585              (status & PCI_STATUS_REC_TARGET_ABORT) ? '+' : '-',
586              (status & PCI_STATUS_REC_MASTER_ABORT) ? '+' : '-',
587              (status & PCI_STATUS_SIG_SYSTEM_ERROR) ? '+' : '-',
588              (status & PCI_STATUS_DETECTED_PARITY) ? '+' : '-');
589       if (cmd & PCI_COMMAND_MASTER)
590         {
591           printf("\tLatency: ");
592           if (min_gnt)
593             printf("%d min, ", min_gnt);
594           if (max_lat)
595             printf("%d max, ", max_lat);
596           printf("%d set", latency);
597           if (cache_line)
598             printf(", cache line size %02x", cache_line);
599           putchar('\n');
600         }
601       if (int_pin || irq)
602         printf("\tInterrupt: pin %c routed to IRQ " IRQ_FORMAT "\n",
603                (int_pin ? 'A' + int_pin - 1 : '?'), irq);
604     }
605   else
606     {
607       printf("\tFlags: ");
608       if (cmd & PCI_COMMAND_MASTER)
609         printf("bus master, ");
610       if (cmd & PCI_COMMAND_VGA_PALETTE)
611         printf("VGA palette snoop, ");
612       if (cmd & PCI_COMMAND_WAIT)
613         printf("stepping, ");
614       if (cmd & PCI_COMMAND_FAST_BACK)
615         printf("fast Back2Back, ");
616       if (status & PCI_STATUS_66MHZ)
617         printf("66Mhz, ");
618       if (status & PCI_STATUS_UDF)
619         printf("user-definable features, ");
620       printf("%s devsel",
621              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
622              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
623              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??");
624       if (cmd & PCI_COMMAND_MASTER)
625         printf(", latency %d", latency);
626       if (irq)
627         printf(", IRQ " IRQ_FORMAT, irq);
628       putchar('\n');
629     }
630
631   if (bist & PCI_BIST_CAPABLE)
632     {
633       if (bist & PCI_BIST_START)
634         printf("\tBIST is running\n");
635       else
636         printf("\tBIST result: %02x\n", bist & PCI_BIST_CODE_MASK);
637     }
638
639   switch (htype)
640     {
641     case PCI_HEADER_TYPE_NORMAL:
642       show_htype0(d);
643       break;
644     case PCI_HEADER_TYPE_BRIDGE:
645       show_htype1(d);
646       break;
647     case PCI_HEADER_TYPE_CARDBUS:
648       show_htype2(d);
649       break;
650     }
651 }
652
653 static void
654 show_hex_dump(struct device *d)
655 {
656   unsigned int i;
657
658   for(i=0; i<d->config_cnt; i++)
659     {
660       if (! (i & 15))
661         printf("%02x:", i);
662       printf(" %02x", get_conf_byte(d, i));
663       if ((i & 15) == 15)
664         putchar('\n');
665     }
666 }
667
668 static void
669 show_machine(struct device *d)
670 {
671   struct pci_dev *p = d->dev;
672   int c;
673   word sv_id=0, sd_id=0;
674   char classbuf[128], vendbuf[128], devbuf[128], svbuf[128], sdbuf[128];
675
676   switch (get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f)
677     {
678     case PCI_HEADER_TYPE_NORMAL:
679       sv_id = get_conf_word(d, PCI_SUBSYSTEM_VENDOR_ID);
680       sd_id = get_conf_word(d, PCI_SUBSYSTEM_ID);
681       break;
682     case PCI_HEADER_TYPE_CARDBUS:
683       sv_id = get_conf_word(d, PCI_CB_SUBSYSTEM_VENDOR_ID);
684       sd_id = get_conf_word(d, PCI_CB_SUBSYSTEM_ID);
685       break;
686     }
687
688   if (verbose)
689     {
690       printf("Device:\t%02x:%02x.%x\n", p->bus, p->dev, p->func);
691       printf("Class:\t%s\n",
692              pci_lookup_name(pacc, classbuf, sizeof(classbuf), PCI_LOOKUP_CLASS, get_conf_word(d, PCI_CLASS_DEVICE), 0));
693       printf("Vendor:\t%s\n",
694              pci_lookup_name(pacc, vendbuf, sizeof(vendbuf), PCI_LOOKUP_VENDOR, p->vendor_id, p->device_id));
695       printf("Device:\t%s\n",
696              pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id));
697       if (sv_id && sv_id != 0xffff)
698         {
699           printf("SVendor:\t%s\n",
700                  pci_lookup_name(pacc, svbuf, sizeof(svbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR, sv_id, sd_id));
701           printf("SDevice:\t%s\n",
702                  pci_lookup_name(pacc, sdbuf, sizeof(sdbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE, sv_id, sd_id));
703         }
704       if (c = get_conf_byte(d, PCI_REVISION_ID))
705         printf("Rev:\t%02x\n", c);
706       if (c = get_conf_byte(d, PCI_CLASS_PROG))
707         printf("ProgIf:\t%02x\n", c);
708     }
709   else
710     {
711       printf("%02x:%02x.%x ", p->bus, p->dev, p->func);
712       printf("\"%s\" \"%s\" \"%s\"",
713              pci_lookup_name(pacc, classbuf, sizeof(classbuf), PCI_LOOKUP_CLASS,
714                              get_conf_word(d, PCI_CLASS_DEVICE), 0),
715              pci_lookup_name(pacc, vendbuf, sizeof(vendbuf), PCI_LOOKUP_VENDOR,
716                              p->vendor_id, p->device_id),
717              pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_DEVICE,
718                              p->vendor_id, p->device_id));
719       if (c = get_conf_byte(d, PCI_REVISION_ID))
720         printf(" -r%02x", c);
721       if (c = get_conf_byte(d, PCI_CLASS_PROG))
722         printf(" -p%02x", c);
723       if (sv_id && sv_id != 0xffff)
724         printf(" \"%s\" \"%s\"",
725                pci_lookup_name(pacc, svbuf, sizeof(svbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR, sv_id, sd_id),
726                pci_lookup_name(pacc, sdbuf, sizeof(sdbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE, sv_id, sd_id));
727       else
728         printf(" \"\" \"\"");
729       putchar('\n');
730     }
731 }
732
733 static void
734 show_device(struct device *d)
735 {
736   if (machine_readable)
737     show_machine(d);
738   else if (verbose)
739     show_verbose(d);
740   else
741     show_terse(d);
742   if (show_hex)
743     show_hex_dump(d);
744   if (verbose || show_hex)
745     putchar('\n');
746 }
747
748 static void
749 show(void)
750 {
751   struct device *d;
752
753   for(d=first_dev; d; d=d->next)
754     show_device(d);
755 }
756
757 /* Tree output */
758
759 struct bridge {
760   struct bridge *chain;                 /* Single-linked list of bridges */
761   struct bridge *next, *child;          /* Tree of bridges */
762   struct bus *first_bus;                /* List of busses connected to this bridge */
763   unsigned int primary, secondary, subordinate; /* Bus numbers */
764   struct device *br_dev;
765 };
766
767 struct bus {
768   unsigned int number;
769   struct bus *sibling;
770   struct device *first_dev, **last_dev;
771 };
772
773 static struct bridge host_bridge = { NULL, NULL, NULL, NULL, ~0, 0, ~0, NULL };
774
775 static struct bus *
776 find_bus(struct bridge *b, unsigned int n)
777 {
778   struct bus *bus;
779
780   for(bus=b->first_bus; bus; bus=bus->sibling)
781     if (bus->number == n)
782       break;
783   return bus;
784 }
785
786 static struct bus *
787 new_bus(struct bridge *b, unsigned int n)
788 {
789   struct bus *bus = xmalloc(sizeof(struct bus));
790
791   bus = xmalloc(sizeof(struct bus));
792   bus->number = n;
793   bus->sibling = b->first_bus;
794   bus->first_dev = NULL;
795   bus->last_dev = &bus->first_dev;
796   b->first_bus = bus;
797   return bus;
798 }
799
800 static void
801 insert_dev(struct device *d, struct bridge *b)
802 {
803   struct pci_dev *p = d->dev;
804   struct bus *bus;
805
806   if (! (bus = find_bus(b, p->bus)))
807     {
808       struct bridge *c;
809       for(c=b->child; c; c=c->next)
810         if (c->secondary <= p->bus && p->bus <= c->subordinate)
811           return insert_dev(d, c);
812       bus = new_bus(b, p->bus);
813     }
814   /* Simple insertion at the end _does_ guarantee the correct order as the
815    * original device list was sorted by (bus, devfn) lexicographically
816    * and all devices on the new list have the same bus number.
817    */
818   *bus->last_dev = d;
819   bus->last_dev = &d->next;
820   d->next = NULL;
821 }
822
823 static void
824 grow_tree(void)
825 {
826   struct device *d, *d2;
827   struct bridge **last_br, *b;
828
829   /* Build list of bridges */
830
831   last_br = &host_bridge.chain;
832   for(d=first_dev; d; d=d->next)
833     {
834       word class = get_conf_word(d, PCI_CLASS_DEVICE);
835       byte ht = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
836       if (class == PCI_CLASS_BRIDGE_PCI &&
837           (ht == PCI_HEADER_TYPE_BRIDGE || ht == PCI_HEADER_TYPE_CARDBUS))
838         {
839           b = xmalloc(sizeof(struct bridge));
840           if (ht == PCI_HEADER_TYPE_BRIDGE)
841             {
842               b->primary = get_conf_byte(d, PCI_CB_PRIMARY_BUS);
843               b->secondary = get_conf_byte(d, PCI_CB_CARD_BUS);
844               b->subordinate = get_conf_byte(d, PCI_CB_SUBORDINATE_BUS);
845             }
846           else
847             {
848               b->primary = get_conf_byte(d, PCI_PRIMARY_BUS);
849               b->secondary = get_conf_byte(d, PCI_SECONDARY_BUS);
850               b->subordinate = get_conf_byte(d, PCI_SUBORDINATE_BUS);
851             }
852           *last_br = b;
853           last_br = &b->chain;
854           b->next = b->child = NULL;
855           b->first_bus = NULL;
856           b->br_dev = d;
857         }
858     }
859   *last_br = NULL;
860
861   /* Create a bridge tree */
862
863   for(b=&host_bridge; b; b=b->chain)
864     {
865       struct bridge *c, *best;
866       best = NULL;
867       for(c=&host_bridge; c; c=c->chain)
868         if (c != b && b->primary >= c->secondary && b->primary <= c->subordinate &&
869             (!best || best->subordinate - best->primary > c->subordinate - c->primary))
870           best = c;
871       if (best)
872         {
873           b->next = best->child;
874           best->child = b;
875         }
876     }
877
878   /* Insert secondary bus for each bridge */
879
880   for(b=&host_bridge; b; b=b->chain)
881     if (!find_bus(b, b->secondary))
882       new_bus(b, b->secondary);
883
884   /* Create bus structs and link devices */
885
886   for(d=first_dev; d;)
887     {
888       d2 = d->next;
889       insert_dev(d, &host_bridge);
890       d = d2;
891     }
892 }
893
894 static void
895 print_it(byte *line, byte *p)
896 {
897   *p++ = '\n';
898   *p = 0;
899   fputs(line, stdout);
900   for(p=line; *p; p++)
901     if (*p == '+' || *p == '|')
902       *p = '|';
903     else
904       *p = ' ';
905 }
906
907 static void show_tree_bridge(struct bridge *, byte *, byte *);
908
909 static void
910 show_tree_dev(struct device *d, byte *line, byte *p)
911 {
912   struct pci_dev *q = d->dev;
913   struct bridge *b;
914   char namebuf[256];
915
916   p += sprintf(p, "%02x.%x", q->dev, q->func);
917   for(b=&host_bridge; b; b=b->chain)
918     if (b->br_dev == d)
919       {
920         if (b->secondary == b->subordinate)
921           p += sprintf(p, "-[%02x]-", b->secondary);
922         else
923           p += sprintf(p, "-[%02x-%02x]-", b->secondary, b->subordinate);
924         show_tree_bridge(b, line, p);
925         return;
926       }
927   if (verbose)
928     p += sprintf(p, "  %s",
929                  pci_lookup_name(pacc, namebuf, sizeof(namebuf),
930                                  PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
931                                  q->vendor_id, q->device_id));
932   print_it(line, p);
933 }
934
935 static void
936 show_tree_bus(struct bus *b, byte *line, byte *p)
937 {
938   if (!b->first_dev)
939     print_it(line, p);
940   else if (!b->first_dev->next)
941     {
942       *p++ = '-';
943       *p++ = '-';
944       show_tree_dev(b->first_dev, line, p);
945     }
946   else
947     {
948       struct device *d = b->first_dev;
949       while (d->next)
950         {
951           p[0] = '+';
952           p[1] = '-';
953           show_tree_dev(d, line, p+2);
954           d = d->next;
955         }
956       p[0] = '\\';
957       p[1] = '-';
958       show_tree_dev(d, line, p+2);
959     }
960 }
961
962 static void
963 show_tree_bridge(struct bridge *b, byte *line, byte *p)
964 {
965   *p++ = '-';
966   if (!b->first_bus->sibling)
967     {
968       if (b == &host_bridge)
969         p += sprintf(p, "[%02x]-", b->first_bus->number);
970       show_tree_bus(b->first_bus, line, p);
971     }
972   else
973     {
974       struct bus *u = b->first_bus;
975       byte *k;
976
977       while (u->sibling)
978         {
979           k = p + sprintf(p, "+-[%02x]-", u->number);
980           show_tree_bus(u, line, k);
981           u = u->sibling;
982         }
983       k = p + sprintf(p, "\\-[%02x]-", u->number);
984       show_tree_bus(u, line, k);
985     }
986 }
987
988 static void
989 show_forest(void)
990 {
991   char line[256];
992
993   grow_tree();
994   show_tree_bridge(&host_bridge, line, line);
995 }
996
997 /* Bus mapping mode */
998
999 struct bus_bridge {
1000   struct bus_bridge *next;
1001   byte first, last;
1002 };
1003
1004 struct bus_info {
1005   byte exists;
1006   byte covered;
1007   struct bus_bridge *bridges;
1008 };
1009
1010 static struct bus_info *bus_info;
1011
1012 static void
1013 map_bridge(struct bus_info *bi, struct device *d, int np, int ns, int nl)
1014 {
1015   struct bus_bridge *b = xmalloc(sizeof(struct bus_bridge));
1016   struct pci_dev *p = d->dev;
1017   int prim = get_conf_byte(d, np);
1018
1019   b->next = bi->bridges;
1020   bi->bridges = b;
1021   b->first = get_conf_byte(d, ns);
1022   b->last = get_conf_byte(d, nl);
1023   printf("## %02x.%02x:%d is a bridge from %02x to %02x-%02x\n",
1024          p->bus, p->dev, p->func, prim, b->first, b->last);
1025   if (prim != p->bus)
1026     printf("!!! Bridge points to invalid primary bus.\n");
1027   if (b->first > b->last)
1028     {
1029       printf("!!! Bridge points to invalid bus range.\n");
1030       b->last = b->first;
1031     }
1032 }
1033
1034 static void
1035 do_map_bus(int bus)
1036 {
1037   int dev, func;
1038   int verbose = pacc->debugging;
1039   struct bus_info *bi = bus_info + bus;
1040   struct device *d;
1041
1042   if (verbose)
1043     printf("Mapping bus %02x\n", bus);
1044   for(dev = 0; dev < 32; dev++)
1045     if (filter.slot < 0 || filter.slot == dev)
1046       {
1047         for(func = 0; func < 8; func++)
1048           if (filter.func < 0 || filter.func == func)
1049             {
1050               struct pci_dev *p = pci_get_dev(pacc, bus, dev, func);
1051               u16 vendor = pci_read_word(p, PCI_VENDOR_ID);
1052               if (vendor && vendor != 0xffff)
1053                 {
1054                   if (verbose)
1055                     printf("Discovered device %02x:%02x.%d\n", bus, dev, func);
1056                   bi->exists = 1;
1057                   if (d = scan_device(p))
1058                     {
1059                       show_device(d);
1060                       switch (get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f)
1061                         {
1062                         case PCI_HEADER_TYPE_BRIDGE:
1063                           map_bridge(bi, d, PCI_PRIMARY_BUS, PCI_SECONDARY_BUS, PCI_SUBORDINATE_BUS);
1064                           break;
1065                         case PCI_HEADER_TYPE_CARDBUS:
1066                           map_bridge(bi, d, PCI_CB_PRIMARY_BUS, PCI_CB_CARD_BUS, PCI_CB_SUBORDINATE_BUS);
1067                           break;
1068                         }
1069                       free(d);
1070                     }
1071                   else if (verbose)
1072                     printf("But it was filtered out.\n");
1073                 }
1074               pci_free_dev(p);
1075             }
1076       }
1077 }
1078
1079 static void
1080 map_the_bus(void)
1081 {
1082   if (pacc->method == PCI_ACCESS_PROC_BUS_PCI ||
1083       pacc->method == PCI_ACCESS_DUMP)
1084     printf("WARNING: Bus mapping can be reliable only with direct hardware access enabled.\n\n");
1085   else if (!check_root())
1086     die("Only root can map the bus.");
1087   bus_info = xmalloc(sizeof(struct bus_info) * 256);
1088   bzero(bus_info, sizeof(struct bus_info) * 256);
1089   if (filter.bus >= 0)
1090     do_map_bus(filter.bus);
1091   else
1092     {
1093       int bus;
1094       for(bus=0; bus<256; bus++)
1095         do_map_bus(bus);
1096     }
1097 }
1098
1099 /* Main */
1100
1101 int
1102 main(int argc, char **argv)
1103 {
1104   int i;
1105   char *msg;
1106
1107   if (argc == 2 && !strcmp(argv[1], "--version"))
1108     {
1109       puts("lspci version " PCIUTILS_VERSION);
1110       return 0;
1111     }
1112
1113   pacc = pci_alloc();
1114   pacc->error = die;
1115   pci_filter_init(pacc, &filter);
1116
1117   while ((i = getopt(argc, argv, options)) != -1)
1118     switch (i)
1119       {
1120       case 'n':
1121         pacc->numeric_ids = 1;
1122         break;
1123       case 'v':
1124         verbose++;
1125         break;
1126       case 'b':
1127         pacc->buscentric = 1;
1128         buscentric_view = 1;
1129         break;
1130       case 's':
1131         if (msg = pci_filter_parse_slot(&filter, optarg))
1132           die("-f: %s", msg);
1133         break;
1134       case 'd':
1135         if (msg = pci_filter_parse_id(&filter, optarg))
1136           die("-d: %s", msg);
1137         break;
1138       case 'x':
1139         show_hex++;
1140         break;
1141       case 't':
1142         show_tree++;
1143         break;
1144       case 'i':
1145         pacc->id_file_name = optarg;
1146         break;
1147       case 'm':
1148         machine_readable++;
1149         break;
1150       case 'M':
1151         map_mode++;
1152         break;
1153       default:
1154         if (parse_generic_option(i, pacc, optarg))
1155           break;
1156       bad:
1157         fprintf(stderr, help_msg, pacc->id_file_name);
1158         return 1;
1159       }
1160   if (optind < argc)
1161     goto bad;
1162
1163   pci_init(pacc);
1164   if (map_mode)
1165     map_the_bus();
1166   else
1167     {
1168       scan_devices();
1169       sort_them();
1170       if (show_tree)
1171         show_forest();
1172       else
1173         show();
1174     }
1175   pci_cleanup(pacc);
1176
1177   return 0;
1178 }