]> mj.ucw.cz Git - pciutils.git/blob - lspci.c
Fix inconsistent bus centric view between the sysfs and proc methods
[pciutils.git] / lspci.c
1 /*
2  *      The PCI Utilities -- List All PCI Devices
3  *
4  *      Copyright (c) 1997--2008 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 "lspci.h"
16
17 /* Options */
18
19 int verbose;                            /* Show detailed information */
20 static int opt_buscentric;              /* Show bus addresses/IRQ's instead of CPU-visible ones */
21 static int opt_hex;                     /* Show contents of config space as hexadecimal numbers */
22 struct pci_filter filter;               /* Device filter */
23 static int opt_tree;                    /* Show bus tree */
24 static int opt_machine;                 /* Generate machine-readable output */
25 static int opt_map_mode;                /* Bus mapping mode enabled */
26 static int opt_domains;                 /* Show domain numbers (0=disabled, 1=auto-detected, 2=requested) */
27 static int opt_kernel;                  /* Show kernel drivers */
28 static int opt_query_dns;               /* Query the DNS (0=disabled, 1=enabled, 2=refresh cache) */
29 static int opt_query_all;               /* Query the DNS for all entries */
30 char *opt_pcimap;                       /* Override path to Linux modules.pcimap */
31
32 const char program_name[] = "lspci";
33
34 static char options[] = "nvbxs:d:ti:mgp:qkMDQ" GENERIC_OPTIONS ;
35
36 static char help_msg[] =
37 "Usage: lspci [<switches>]\n"
38 "\n"
39 "Basic display modes:\n"
40 "-mm\t\tProduce machine-readable output (single -m for an obsolete format)\n"
41 "-t\t\tShow bus tree\n"
42 "\n"
43 "Display options:\n"
44 "-v\t\tBe verbose (-vv for very verbose)\n"
45 #ifdef PCI_OS_LINUX
46 "-k\t\tShow kernel drivers handling each device\n"
47 #endif
48 "-x\t\tShow hex-dump of the standard part of the config space\n"
49 "-xxx\t\tShow hex-dump of the whole config space (dangerous; root only)\n"
50 "-xxxx\t\tShow hex-dump of the 4096-byte extended config space (root only)\n"
51 "-b\t\tBus-centric view (addresses and IRQ's as seen by the bus)\n"
52 "-D\t\tAlways show domain numbers\n"
53 "\n"
54 "Resolving of device ID's to names:\n"
55 "-n\t\tShow numeric ID's\n"
56 "-nn\t\tShow both textual and numeric ID's (names & numbers)\n"
57 #ifdef PCI_USE_DNS
58 "-q\t\tQuery the PCI ID database for unknown ID's via DNS\n"
59 "-qq\t\tAs above, but re-query locally cached entries\n"
60 "-Q\t\tQuery the PCI ID database for all ID's via DNS\n"
61 #endif
62 "\n"
63 "Selection of devices:\n"
64 "-s [[[[<domain>]:]<bus>]:][<slot>][.[<func>]]\tShow only devices in selected slots\n"
65 "-d [<vendor>]:[<device>]\t\t\tShow only devices with specified ID's\n"
66 "\n"
67 "Other options:\n"
68 "-i <file>\tUse specified ID database instead of %s\n"
69 #ifdef PCI_OS_LINUX
70 "-p <file>\tLook up kernel modules in a given file instead of default modules.pcimap\n"
71 #endif
72 "-M\t\tEnable `bus mapping' mode (dangerous; root only)\n"
73 "\n"
74 "PCI access options:\n"
75 GENERIC_HELP
76 ;
77
78 /*** Our view of the PCI bus ***/
79
80 struct pci_access *pacc;
81 struct device *first_dev;
82 static int seen_errors;
83
84 int
85 config_fetch(struct device *d, unsigned int pos, unsigned int len)
86 {
87   unsigned int end = pos+len;
88   int result;
89
90   while (pos < d->config_bufsize && len && d->present[pos])
91     pos++, len--;
92   while (pos+len <= d->config_bufsize && len && d->present[pos+len-1])
93     len--;
94   if (!len)
95     return 1;
96
97   if (end > d->config_bufsize)
98     {
99       int orig_size = d->config_bufsize;
100       while (end > d->config_bufsize)
101         d->config_bufsize *= 2;
102       d->config = xrealloc(d->config, d->config_bufsize);
103       d->present = xrealloc(d->present, d->config_bufsize);
104       memset(d->present + orig_size, 0, d->config_bufsize - orig_size);
105     }
106   result = pci_read_block(d->dev, pos, d->config + pos, len);
107   if (result)
108     memset(d->present + pos, 1, len);
109   return result;
110 }
111
112 struct device *
113 scan_device(struct pci_dev *p)
114 {
115   struct device *d;
116
117   if (p->domain && !opt_domains)
118     opt_domains = 1;
119   if (!pci_filter_match(&filter, p))
120     return NULL;
121   d = xmalloc(sizeof(struct device));
122   memset(d, 0, sizeof(*d));
123   d->dev = p;
124   d->config_cached = d->config_bufsize = 64;
125   d->config = xmalloc(64);
126   d->present = xmalloc(64);
127   memset(d->present, 1, 64);
128   if (!pci_read_block(p, 0, d->config, 64))
129     {
130       fprintf(stderr, "lspci: Unable to read the standard configuration space header of device %04x:%02x:%02x.%d\n",
131               p->domain, p->bus, p->dev, p->func);
132       seen_errors++;
133       return NULL;
134     }
135   if ((d->config[PCI_HEADER_TYPE] & 0x7f) == PCI_HEADER_TYPE_CARDBUS)
136     {
137       /* For cardbus bridges, we need to fetch 64 bytes more to get the
138        * full standard header... */
139       if (config_fetch(d, 64, 64))
140         d->config_cached += 64;
141     }
142   pci_setup_cache(p, d->config, d->config_cached);
143   pci_fill_info(p, PCI_FILL_IDENT | PCI_FILL_CLASS | PCI_FILL_IRQ | PCI_FILL_BASES | PCI_FILL_ROM_BASE | PCI_FILL_SIZES | PCI_FILL_PHYS_SLOT);
144   return d;
145 }
146
147 static void
148 scan_devices(void)
149 {
150   struct device *d;
151   struct pci_dev *p;
152
153   pci_scan_bus(pacc);
154   for (p=pacc->devices; p; p=p->next)
155     if (d = scan_device(p))
156       {
157         d->next = first_dev;
158         first_dev = d;
159       }
160 }
161
162 /*** Config space accesses ***/
163
164 static void
165 check_conf_range(struct device *d, unsigned int pos, unsigned int len)
166 {
167   while (len)
168     if (!d->present[pos])
169       die("Internal bug: Accessing non-read configuration byte at position %x", pos);
170     else
171       pos++, len--;
172 }
173
174 byte
175 get_conf_byte(struct device *d, unsigned int pos)
176 {
177   check_conf_range(d, pos, 1);
178   return d->config[pos];
179 }
180
181 word
182 get_conf_word(struct device *d, unsigned int pos)
183 {
184   check_conf_range(d, pos, 2);
185   return d->config[pos] | (d->config[pos+1] << 8);
186 }
187
188 u32
189 get_conf_long(struct device *d, unsigned int pos)
190 {
191   check_conf_range(d, pos, 4);
192   return d->config[pos] |
193     (d->config[pos+1] << 8) |
194     (d->config[pos+2] << 16) |
195     (d->config[pos+3] << 24);
196 }
197
198 /*** Sorting ***/
199
200 static int
201 compare_them(const void *A, const void *B)
202 {
203   const struct pci_dev *a = (*(const struct device **)A)->dev;
204   const struct pci_dev *b = (*(const struct device **)B)->dev;
205
206   if (a->domain < b->domain)
207     return -1;
208   if (a->domain > b->domain)
209     return 1;
210   if (a->bus < b->bus)
211     return -1;
212   if (a->bus > b->bus)
213     return 1;
214   if (a->dev < b->dev)
215     return -1;
216   if (a->dev > b->dev)
217     return 1;
218   if (a->func < b->func)
219     return -1;
220   if (a->func > b->func)
221     return 1;
222   return 0;
223 }
224
225 static void
226 sort_them(void)
227 {
228   struct device **index, **h, **last_dev;
229   int cnt;
230   struct device *d;
231
232   cnt = 0;
233   for (d=first_dev; d; d=d->next)
234     cnt++;
235   h = index = alloca(sizeof(struct device *) * cnt);
236   for (d=first_dev; d; d=d->next)
237     *h++ = d;
238   qsort(index, cnt, sizeof(struct device *), compare_them);
239   last_dev = &first_dev;
240   h = index;
241   while (cnt--)
242     {
243       *last_dev = *h;
244       last_dev = &(*h)->next;
245       h++;
246     }
247   *last_dev = NULL;
248 }
249
250 /*** Normal output ***/
251
252 static void
253 show_slot_name(struct device *d)
254 {
255   struct pci_dev *p = d->dev;
256
257   if (!opt_machine ? opt_domains : (p->domain || opt_domains >= 2))
258     printf("%04x:", p->domain);
259   printf("%02x:%02x.%d", p->bus, p->dev, p->func);
260 }
261
262 void
263 get_subid(struct device *d, word *subvp, word *subdp)
264 {
265   byte htype = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
266
267   if (htype == PCI_HEADER_TYPE_NORMAL)
268     {
269       *subvp = get_conf_word(d, PCI_SUBSYSTEM_VENDOR_ID);
270       *subdp = get_conf_word(d, PCI_SUBSYSTEM_ID);
271     }
272   else if (htype == PCI_HEADER_TYPE_CARDBUS && d->config_cached >= 128)
273     {
274       *subvp = get_conf_word(d, PCI_CB_SUBSYSTEM_VENDOR_ID);
275       *subdp = get_conf_word(d, PCI_CB_SUBSYSTEM_ID);
276     }
277   else
278     *subvp = *subdp = 0xffff;
279 }
280
281 static void
282 show_terse(struct device *d)
283 {
284   int c;
285   struct pci_dev *p = d->dev;
286   char classbuf[128], devbuf[128];
287
288   show_slot_name(d);
289   printf(" %s: %s",
290          pci_lookup_name(pacc, classbuf, sizeof(classbuf),
291                          PCI_LOOKUP_CLASS,
292                          p->device_class),
293          pci_lookup_name(pacc, devbuf, sizeof(devbuf),
294                          PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
295                          p->vendor_id, p->device_id));
296   if (c = get_conf_byte(d, PCI_REVISION_ID))
297     printf(" (rev %02x)", c);
298   if (verbose)
299     {
300       char *x;
301       c = get_conf_byte(d, PCI_CLASS_PROG);
302       x = pci_lookup_name(pacc, devbuf, sizeof(devbuf),
303                           PCI_LOOKUP_PROGIF | PCI_LOOKUP_NO_NUMBERS,
304                           p->device_class, c);
305       if (c || x)
306         {
307           printf(" (prog-if %02x", c);
308           if (x)
309             printf(" [%s]", x);
310           putchar(')');
311         }
312     }
313   putchar('\n');
314
315   if (verbose || opt_kernel)
316     {
317       word subsys_v, subsys_d;
318       char ssnamebuf[256];
319
320       get_subid(d, &subsys_v, &subsys_d);
321       if (subsys_v && subsys_v != 0xffff)
322         printf("\tSubsystem: %s\n",
323                 pci_lookup_name(pacc, ssnamebuf, sizeof(ssnamebuf),
324                         PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
325                         p->vendor_id, p->device_id, subsys_v, subsys_d));
326     }
327 }
328
329 /*** Verbose output ***/
330
331 static void
332 show_size(pciaddr_t x)
333 {
334   if (!x)
335     return;
336   printf(" [size=");
337   if (x < 1024)
338     printf("%d", (int) x);
339   else if (x < 1048576)
340     printf("%dK", (int)(x / 1024));
341   else if (x < 0x80000000)
342     printf("%dM", (int)(x / 1048576));
343   else
344     printf(PCIADDR_T_FMT, x);
345   putchar(']');
346 }
347
348 static void
349 show_bases(struct device *d, int cnt)
350 {
351   struct pci_dev *p = d->dev;
352   word cmd = get_conf_word(d, PCI_COMMAND);
353   int i;
354
355   for (i=0; i<cnt; i++)
356     {
357       pciaddr_t pos = p->base_addr[i];
358       pciaddr_t len = (p->known_fields & PCI_FILL_SIZES) ? p->size[i] : 0;
359       u32 flg = get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i);
360       if (flg == 0xffffffff)
361         flg = 0;
362       if (!pos && !flg && !len)
363         continue;
364       if (verbose > 1)
365         printf("\tRegion %d: ", i);
366       else
367         putchar('\t');
368       if (pos && !flg)                  /* Reported by the OS, but not by the device */
369         {
370           printf("[virtual] ");
371           flg = pos;
372         }
373       if (flg & PCI_BASE_ADDRESS_SPACE_IO)
374         {
375           pciaddr_t a = pos & PCI_BASE_ADDRESS_IO_MASK;
376           printf("I/O ports at ");
377           if (a)
378             printf(PCIADDR_PORT_FMT, a);
379           else if (flg & PCI_BASE_ADDRESS_IO_MASK)
380             printf("<ignored>");
381           else
382             printf("<unassigned>");
383           if (!(cmd & PCI_COMMAND_IO))
384             printf(" [disabled]");
385         }
386       else
387         {
388           int t = flg & PCI_BASE_ADDRESS_MEM_TYPE_MASK;
389           pciaddr_t a = pos & PCI_ADDR_MEM_MASK;
390           int done = 0;
391           u32 z = 0;
392
393           printf("Memory at ");
394           if (t == PCI_BASE_ADDRESS_MEM_TYPE_64)
395             {
396               if (i >= cnt - 1)
397                 {
398                   printf("<invalid-64bit-slot>");
399                   done = 1;
400                 }
401               else
402                 {
403                   i++;
404                   z = get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i);
405                   if (opt_buscentric)
406                     {
407                       u32 y = a & 0xffffffff;
408                       if (a || z)
409                         printf("%08x%08x", z, y);
410                       else
411                         printf("<unassigned>");
412                       done = 1;
413                     }
414                 }
415             }
416           if (!done)
417             {
418               if (a)
419                 printf(PCIADDR_T_FMT, a);
420               else
421                 printf(((flg & PCI_BASE_ADDRESS_MEM_MASK) || z) ? "<ignored>" : "<unassigned>");
422             }
423           printf(" (%s, %sprefetchable)",
424                  (t == PCI_BASE_ADDRESS_MEM_TYPE_32) ? "32-bit" :
425                  (t == PCI_BASE_ADDRESS_MEM_TYPE_64) ? "64-bit" :
426                  (t == PCI_BASE_ADDRESS_MEM_TYPE_1M) ? "low-1M" : "type 3",
427                  (flg & PCI_BASE_ADDRESS_MEM_PREFETCH) ? "" : "non-");
428           if (!(cmd & PCI_COMMAND_MEMORY))
429             printf(" [disabled]");
430         }
431       show_size(len);
432       putchar('\n');
433     }
434 }
435
436 static void
437 show_rom(struct device *d, int reg)
438 {
439   struct pci_dev *p = d->dev;
440   pciaddr_t rom = p->rom_base_addr;
441   pciaddr_t len = (p->known_fields & PCI_FILL_SIZES) ? p->rom_size : 0;
442   u32 flg = get_conf_long(d, reg);
443   word cmd = get_conf_word(d, PCI_COMMAND);
444
445   if (!rom && !flg && !len)
446     return;
447   putchar('\t');
448   if ((rom & PCI_ROM_ADDRESS_MASK) && !(flg & PCI_ROM_ADDRESS_MASK))
449     {
450       printf("[virtual] ");
451       flg = rom;
452     }
453   printf("Expansion ROM at ");
454   if (rom & PCI_ROM_ADDRESS_MASK)
455     printf(PCIADDR_T_FMT, rom & PCI_ROM_ADDRESS_MASK);
456   else if (flg & PCI_ROM_ADDRESS_MASK)
457     printf("<ignored>");
458   else
459     printf("<unassigned>");
460   if (!(flg & PCI_ROM_ADDRESS_ENABLE))
461     printf(" [disabled]");
462   else if (!(cmd & PCI_COMMAND_MEMORY))
463     printf(" [disabled by cmd]");
464   show_size(len);
465   putchar('\n');
466 }
467
468 static void
469 show_htype0(struct device *d)
470 {
471   show_bases(d, 6);
472   show_rom(d, PCI_ROM_ADDRESS);
473   show_caps(d);
474 }
475
476 static void
477 show_htype1(struct device *d)
478 {
479   u32 io_base = get_conf_byte(d, PCI_IO_BASE);
480   u32 io_limit = get_conf_byte(d, PCI_IO_LIMIT);
481   u32 io_type = io_base & PCI_IO_RANGE_TYPE_MASK;
482   u32 mem_base = get_conf_word(d, PCI_MEMORY_BASE);
483   u32 mem_limit = get_conf_word(d, PCI_MEMORY_LIMIT);
484   u32 mem_type = mem_base & PCI_MEMORY_RANGE_TYPE_MASK;
485   u32 pref_base = get_conf_word(d, PCI_PREF_MEMORY_BASE);
486   u32 pref_limit = get_conf_word(d, PCI_PREF_MEMORY_LIMIT);
487   u32 pref_type = pref_base & PCI_PREF_RANGE_TYPE_MASK;
488   word sec_stat = get_conf_word(d, PCI_SEC_STATUS);
489   word brc = get_conf_word(d, PCI_BRIDGE_CONTROL);
490   int verb = verbose > 2;
491
492   show_bases(d, 2);
493   printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
494          get_conf_byte(d, PCI_PRIMARY_BUS),
495          get_conf_byte(d, PCI_SECONDARY_BUS),
496          get_conf_byte(d, PCI_SUBORDINATE_BUS),
497          get_conf_byte(d, PCI_SEC_LATENCY_TIMER));
498
499   if (io_type != (io_limit & PCI_IO_RANGE_TYPE_MASK) ||
500       (io_type != PCI_IO_RANGE_TYPE_16 && io_type != PCI_IO_RANGE_TYPE_32))
501     printf("\t!!! Unknown I/O range types %x/%x\n", io_base, io_limit);
502   else
503     {
504       io_base = (io_base & PCI_IO_RANGE_MASK) << 8;
505       io_limit = (io_limit & PCI_IO_RANGE_MASK) << 8;
506       if (io_type == PCI_IO_RANGE_TYPE_32)
507         {
508           io_base |= (get_conf_word(d, PCI_IO_BASE_UPPER16) << 16);
509           io_limit |= (get_conf_word(d, PCI_IO_LIMIT_UPPER16) << 16);
510         }
511       if (io_base <= io_limit || verb)
512         printf("\tI/O behind bridge: %08x-%08x\n", io_base, io_limit+0xfff);
513     }
514
515   if (mem_type != (mem_limit & PCI_MEMORY_RANGE_TYPE_MASK) ||
516       mem_type)
517     printf("\t!!! Unknown memory range types %x/%x\n", mem_base, mem_limit);
518   else
519     {
520       mem_base = (mem_base & PCI_MEMORY_RANGE_MASK) << 16;
521       mem_limit = (mem_limit & PCI_MEMORY_RANGE_MASK) << 16;
522       if (mem_base <= mem_limit || verb)
523         printf("\tMemory behind bridge: %08x-%08x\n", mem_base, mem_limit + 0xfffff);
524     }
525
526   if (pref_type != (pref_limit & PCI_PREF_RANGE_TYPE_MASK) ||
527       (pref_type != PCI_PREF_RANGE_TYPE_32 && pref_type != PCI_PREF_RANGE_TYPE_64))
528     printf("\t!!! Unknown prefetchable memory range types %x/%x\n", pref_base, pref_limit);
529   else
530     {
531       pref_base = (pref_base & PCI_PREF_RANGE_MASK) << 16;
532       pref_limit = (pref_limit & PCI_PREF_RANGE_MASK) << 16;
533       if (pref_base <= pref_limit || verb)
534         {
535           if (pref_type == PCI_PREF_RANGE_TYPE_32)
536             printf("\tPrefetchable memory behind bridge: %08x-%08x\n", pref_base, pref_limit + 0xfffff);
537           else
538             printf("\tPrefetchable memory behind bridge: %08x%08x-%08x%08x\n",
539                    get_conf_long(d, PCI_PREF_BASE_UPPER32),
540                    pref_base,
541                    get_conf_long(d, PCI_PREF_LIMIT_UPPER32),
542                    pref_limit + 0xfffff);
543         }
544     }
545
546   if (verbose > 1)
547     printf("\tSecondary status: 66MHz%c FastB2B%c ParErr%c DEVSEL=%s >TAbort%c <TAbort%c <MAbort%c <SERR%c <PERR%c\n",
548              FLAG(sec_stat, PCI_STATUS_66MHZ),
549              FLAG(sec_stat, PCI_STATUS_FAST_BACK),
550              FLAG(sec_stat, PCI_STATUS_PARITY),
551              ((sec_stat & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
552              ((sec_stat & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
553              ((sec_stat & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??",
554              FLAG(sec_stat, PCI_STATUS_SIG_TARGET_ABORT),
555              FLAG(sec_stat, PCI_STATUS_REC_TARGET_ABORT),
556              FLAG(sec_stat, PCI_STATUS_REC_MASTER_ABORT),
557              FLAG(sec_stat, PCI_STATUS_SIG_SYSTEM_ERROR),
558              FLAG(sec_stat, PCI_STATUS_DETECTED_PARITY));
559
560   show_rom(d, PCI_ROM_ADDRESS1);
561
562   if (verbose > 1)
563     {
564       printf("\tBridgeCtl: Parity%c SERR%c NoISA%c VGA%c MAbort%c >Reset%c FastB2B%c\n",
565         FLAG(brc, PCI_BRIDGE_CTL_PARITY),
566         FLAG(brc, PCI_BRIDGE_CTL_SERR),
567         FLAG(brc, PCI_BRIDGE_CTL_NO_ISA),
568         FLAG(brc, PCI_BRIDGE_CTL_VGA),
569         FLAG(brc, PCI_BRIDGE_CTL_MASTER_ABORT),
570         FLAG(brc, PCI_BRIDGE_CTL_BUS_RESET),
571         FLAG(brc, PCI_BRIDGE_CTL_FAST_BACK));
572       printf("\t\tPriDiscTmr%c SecDiscTmr%c DiscTmrStat%c DiscTmrSERREn%c\n",
573         FLAG(brc, PCI_BRIDGE_CTL_PRI_DISCARD_TIMER),
574         FLAG(brc, PCI_BRIDGE_CTL_SEC_DISCARD_TIMER),
575         FLAG(brc, PCI_BRIDGE_CTL_DISCARD_TIMER_STATUS),
576         FLAG(brc, PCI_BRIDGE_CTL_DISCARD_TIMER_SERR_EN));
577     }
578
579   show_caps(d);
580 }
581
582 static void
583 show_htype2(struct device *d)
584 {
585   int i;
586   word cmd = get_conf_word(d, PCI_COMMAND);
587   word brc = get_conf_word(d, PCI_CB_BRIDGE_CONTROL);
588   word exca;
589   int verb = verbose > 2;
590
591   show_bases(d, 1);
592   printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
593          get_conf_byte(d, PCI_CB_PRIMARY_BUS),
594          get_conf_byte(d, PCI_CB_CARD_BUS),
595          get_conf_byte(d, PCI_CB_SUBORDINATE_BUS),
596          get_conf_byte(d, PCI_CB_LATENCY_TIMER));
597   for (i=0; i<2; i++)
598     {
599       int p = 8*i;
600       u32 base = get_conf_long(d, PCI_CB_MEMORY_BASE_0 + p);
601       u32 limit = get_conf_long(d, PCI_CB_MEMORY_LIMIT_0 + p);
602       if (limit > base || verb)
603         printf("\tMemory window %d: %08x-%08x%s%s\n", i, base, limit,
604                (cmd & PCI_COMMAND_MEMORY) ? "" : " [disabled]",
605                (brc & (PCI_CB_BRIDGE_CTL_PREFETCH_MEM0 << i)) ? " (prefetchable)" : "");
606     }
607   for (i=0; i<2; i++)
608     {
609       int p = 8*i;
610       u32 base = get_conf_long(d, PCI_CB_IO_BASE_0 + p);
611       u32 limit = get_conf_long(d, PCI_CB_IO_LIMIT_0 + p);
612       if (!(base & PCI_IO_RANGE_TYPE_32))
613         {
614           base &= 0xffff;
615           limit &= 0xffff;
616         }
617       base &= PCI_CB_IO_RANGE_MASK;
618       limit = (limit & PCI_CB_IO_RANGE_MASK) + 3;
619       if (base <= limit || verb)
620         printf("\tI/O window %d: %08x-%08x%s\n", i, base, limit,
621                (cmd & PCI_COMMAND_IO) ? "" : " [disabled]");
622     }
623
624   if (get_conf_word(d, PCI_CB_SEC_STATUS) & PCI_STATUS_SIG_SYSTEM_ERROR)
625     printf("\tSecondary status: SERR\n");
626   if (verbose > 1)
627     printf("\tBridgeCtl: Parity%c SERR%c ISA%c VGA%c MAbort%c >Reset%c 16bInt%c PostWrite%c\n",
628            FLAG(brc, PCI_CB_BRIDGE_CTL_PARITY),
629            FLAG(brc, PCI_CB_BRIDGE_CTL_SERR),
630            FLAG(brc, PCI_CB_BRIDGE_CTL_ISA),
631            FLAG(brc, PCI_CB_BRIDGE_CTL_VGA),
632            FLAG(brc, PCI_CB_BRIDGE_CTL_MASTER_ABORT),
633            FLAG(brc, PCI_CB_BRIDGE_CTL_CB_RESET),
634            FLAG(brc, PCI_CB_BRIDGE_CTL_16BIT_INT),
635            FLAG(brc, PCI_CB_BRIDGE_CTL_POST_WRITES));
636
637   if (d->config_cached < 128)
638     {
639       printf("\t<access denied to the rest>\n");
640       return;
641     }
642
643   exca = get_conf_word(d, PCI_CB_LEGACY_MODE_BASE);
644   if (exca)
645     printf("\t16-bit legacy interface ports at %04x\n", exca);
646 }
647
648 static void
649 show_verbose(struct device *d)
650 {
651   struct pci_dev *p = d->dev;
652   word status = get_conf_word(d, PCI_STATUS);
653   word cmd = get_conf_word(d, PCI_COMMAND);
654   word class = p->device_class;
655   byte bist = get_conf_byte(d, PCI_BIST);
656   byte htype = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
657   byte latency = get_conf_byte(d, PCI_LATENCY_TIMER);
658   byte cache_line = get_conf_byte(d, PCI_CACHE_LINE_SIZE);
659   byte max_lat, min_gnt;
660   byte int_pin = get_conf_byte(d, PCI_INTERRUPT_PIN);
661   unsigned int irq = p->irq;
662
663   show_terse(d);
664
665   switch (htype)
666     {
667     case PCI_HEADER_TYPE_NORMAL:
668       if (class == PCI_CLASS_BRIDGE_PCI)
669         printf("\t!!! Invalid class %04x for header type %02x\n", class, htype);
670       max_lat = get_conf_byte(d, PCI_MAX_LAT);
671       min_gnt = get_conf_byte(d, PCI_MIN_GNT);
672       break;
673     case PCI_HEADER_TYPE_BRIDGE:
674       if ((class >> 8) != PCI_BASE_CLASS_BRIDGE)
675         printf("\t!!! Invalid class %04x for header type %02x\n", class, htype);
676       irq = int_pin = min_gnt = max_lat = 0;
677       break;
678     case PCI_HEADER_TYPE_CARDBUS:
679       if ((class >> 8) != PCI_BASE_CLASS_BRIDGE)
680         printf("\t!!! Invalid class %04x for header type %02x\n", class, htype);
681       min_gnt = max_lat = 0;
682       break;
683     default:
684       printf("\t!!! Unknown header type %02x\n", htype);
685       return;
686     }
687
688   if (p->phy_slot)
689     printf("\tPhysical Slot: %s\n", p->phy_slot);
690
691   if (verbose > 1)
692     {
693       printf("\tControl: I/O%c Mem%c BusMaster%c SpecCycle%c MemWINV%c VGASnoop%c ParErr%c Stepping%c SERR%c FastB2B%c DisINTx%c\n",
694              FLAG(cmd, PCI_COMMAND_IO),
695              FLAG(cmd, PCI_COMMAND_MEMORY),
696              FLAG(cmd, PCI_COMMAND_MASTER),
697              FLAG(cmd, PCI_COMMAND_SPECIAL),
698              FLAG(cmd, PCI_COMMAND_INVALIDATE),
699              FLAG(cmd, PCI_COMMAND_VGA_PALETTE),
700              FLAG(cmd, PCI_COMMAND_PARITY),
701              FLAG(cmd, PCI_COMMAND_WAIT),
702              FLAG(cmd, PCI_COMMAND_SERR),
703              FLAG(cmd, PCI_COMMAND_FAST_BACK),
704              FLAG(cmd, PCI_COMMAND_DISABLE_INTx));
705       printf("\tStatus: Cap%c 66MHz%c UDF%c FastB2B%c ParErr%c DEVSEL=%s >TAbort%c <TAbort%c <MAbort%c >SERR%c <PERR%c INTx%c\n",
706              FLAG(status, PCI_STATUS_CAP_LIST),
707              FLAG(status, PCI_STATUS_66MHZ),
708              FLAG(status, PCI_STATUS_UDF),
709              FLAG(status, PCI_STATUS_FAST_BACK),
710              FLAG(status, PCI_STATUS_PARITY),
711              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
712              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
713              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??",
714              FLAG(status, PCI_STATUS_SIG_TARGET_ABORT),
715              FLAG(status, PCI_STATUS_REC_TARGET_ABORT),
716              FLAG(status, PCI_STATUS_REC_MASTER_ABORT),
717              FLAG(status, PCI_STATUS_SIG_SYSTEM_ERROR),
718              FLAG(status, PCI_STATUS_DETECTED_PARITY),
719              FLAG(status, PCI_STATUS_INTx));
720       if (cmd & PCI_COMMAND_MASTER)
721         {
722           printf("\tLatency: %d", latency);
723           if (min_gnt || max_lat)
724             {
725               printf(" (");
726               if (min_gnt)
727                 printf("%dns min", min_gnt*250);
728               if (min_gnt && max_lat)
729                 printf(", ");
730               if (max_lat)
731                 printf("%dns max", max_lat*250);
732               putchar(')');
733             }
734           if (cache_line)
735             printf(", Cache Line Size: %d bytes", cache_line * 4);
736           putchar('\n');
737         }
738       if (int_pin || irq)
739         printf("\tInterrupt: pin %c routed to IRQ " PCIIRQ_FMT "\n",
740                (int_pin ? 'A' + int_pin - 1 : '?'), irq);
741     }
742   else
743     {
744       printf("\tFlags: ");
745       if (cmd & PCI_COMMAND_MASTER)
746         printf("bus master, ");
747       if (cmd & PCI_COMMAND_VGA_PALETTE)
748         printf("VGA palette snoop, ");
749       if (cmd & PCI_COMMAND_WAIT)
750         printf("stepping, ");
751       if (cmd & PCI_COMMAND_FAST_BACK)
752         printf("fast Back2Back, ");
753       if (status & PCI_STATUS_66MHZ)
754         printf("66MHz, ");
755       if (status & PCI_STATUS_UDF)
756         printf("user-definable features, ");
757       printf("%s devsel",
758              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
759              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
760              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??");
761       if (cmd & PCI_COMMAND_MASTER)
762         printf(", latency %d", latency);
763       if (irq)
764         printf(", IRQ " PCIIRQ_FMT, irq);
765       putchar('\n');
766     }
767
768   if (bist & PCI_BIST_CAPABLE)
769     {
770       if (bist & PCI_BIST_START)
771         printf("\tBIST is running\n");
772       else
773         printf("\tBIST result: %02x\n", bist & PCI_BIST_CODE_MASK);
774     }
775
776   switch (htype)
777     {
778     case PCI_HEADER_TYPE_NORMAL:
779       show_htype0(d);
780       break;
781     case PCI_HEADER_TYPE_BRIDGE:
782       show_htype1(d);
783       break;
784     case PCI_HEADER_TYPE_CARDBUS:
785       show_htype2(d);
786       break;
787     }
788 }
789
790 /*** Machine-readable dumps ***/
791
792 static void
793 show_hex_dump(struct device *d)
794 {
795   unsigned int i, cnt;
796
797   cnt = d->config_cached;
798   if (opt_hex >= 3 && config_fetch(d, cnt, 256-cnt))
799     {
800       cnt = 256;
801       if (opt_hex >= 4 && config_fetch(d, 256, 4096-256))
802         cnt = 4096;
803     }
804
805   for (i=0; i<cnt; i++)
806     {
807       if (! (i & 15))
808         printf("%02x:", i);
809       printf(" %02x", get_conf_byte(d, i));
810       if ((i & 15) == 15)
811         putchar('\n');
812     }
813 }
814
815 static void
816 print_shell_escaped(char *c)
817 {
818   printf(" \"");
819   while (*c)
820     {
821       if (*c == '"' || *c == '\\')
822         putchar('\\');
823       putchar(*c++);
824     }
825   putchar('"');
826 }
827
828 static void
829 show_machine(struct device *d)
830 {
831   struct pci_dev *p = d->dev;
832   int c;
833   word sv_id, sd_id;
834   char classbuf[128], vendbuf[128], devbuf[128], svbuf[128], sdbuf[128];
835
836   get_subid(d, &sv_id, &sd_id);
837
838   if (verbose)
839     {
840       printf((opt_machine >= 2) ? "Slot:\t" : "Device:\t");
841       show_slot_name(d);
842       putchar('\n');
843       printf("Class:\t%s\n",
844              pci_lookup_name(pacc, classbuf, sizeof(classbuf), PCI_LOOKUP_CLASS, p->device_class));
845       printf("Vendor:\t%s\n",
846              pci_lookup_name(pacc, vendbuf, sizeof(vendbuf), PCI_LOOKUP_VENDOR, p->vendor_id, p->device_id));
847       printf("Device:\t%s\n",
848              pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id));
849       if (sv_id && sv_id != 0xffff)
850         {
851           printf("SVendor:\t%s\n",
852                  pci_lookup_name(pacc, svbuf, sizeof(svbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR, sv_id));
853           printf("SDevice:\t%s\n",
854                  pci_lookup_name(pacc, sdbuf, sizeof(sdbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id, sv_id, sd_id));
855         }
856       if (p->phy_slot)
857         printf("PhySlot:\t%s\n", p->phy_slot);
858       if (c = get_conf_byte(d, PCI_REVISION_ID))
859         printf("Rev:\t%02x\n", c);
860       if (c = get_conf_byte(d, PCI_CLASS_PROG))
861         printf("ProgIf:\t%02x\n", c);
862       if (opt_kernel)
863         show_kernel_machine(d);
864     }
865   else
866     {
867       show_slot_name(d);
868       print_shell_escaped(pci_lookup_name(pacc, classbuf, sizeof(classbuf), PCI_LOOKUP_CLASS, p->device_class));
869       print_shell_escaped(pci_lookup_name(pacc, vendbuf, sizeof(vendbuf), PCI_LOOKUP_VENDOR, p->vendor_id, p->device_id));
870       print_shell_escaped(pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id));
871       if (c = get_conf_byte(d, PCI_REVISION_ID))
872         printf(" -r%02x", c);
873       if (c = get_conf_byte(d, PCI_CLASS_PROG))
874         printf(" -p%02x", c);
875       if (sv_id && sv_id != 0xffff)
876         {
877           print_shell_escaped(pci_lookup_name(pacc, svbuf, sizeof(svbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR, sv_id));
878           print_shell_escaped(pci_lookup_name(pacc, sdbuf, sizeof(sdbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id, sv_id, sd_id));
879         }
880       else
881         printf(" \"\" \"\"");
882       putchar('\n');
883     }
884 }
885
886 /*** Main show function ***/
887
888 void
889 show_device(struct device *d)
890 {
891   if (opt_machine)
892     show_machine(d);
893   else
894     {
895       if (verbose)
896         show_verbose(d);
897       else
898         show_terse(d);
899       if (opt_kernel || verbose)
900         show_kernel(d);
901     }
902   if (opt_hex)
903     show_hex_dump(d);
904   if (verbose || opt_hex)
905     putchar('\n');
906 }
907
908 static void
909 show(void)
910 {
911   struct device *d;
912
913   for (d=first_dev; d; d=d->next)
914     show_device(d);
915 }
916
917 /* Main */
918
919 int
920 main(int argc, char **argv)
921 {
922   int i;
923   char *msg;
924
925   if (argc == 2 && !strcmp(argv[1], "--version"))
926     {
927       puts("lspci version " PCIUTILS_VERSION);
928       return 0;
929     }
930
931   pacc = pci_alloc();
932   pacc->error = die;
933   pci_filter_init(pacc, &filter);
934
935   while ((i = getopt(argc, argv, options)) != -1)
936     switch (i)
937       {
938       case 'n':
939         pacc->numeric_ids++;
940         break;
941       case 'v':
942         verbose++;
943         break;
944       case 'b':
945         pacc->buscentric = 1;
946         opt_buscentric = 1;
947         break;
948       case 's':
949         if (msg = pci_filter_parse_slot(&filter, optarg))
950           die("-s: %s", msg);
951         break;
952       case 'd':
953         if (msg = pci_filter_parse_id(&filter, optarg))
954           die("-d: %s", msg);
955         break;
956       case 'x':
957         opt_hex++;
958         break;
959       case 't':
960         opt_tree++;
961         break;
962       case 'i':
963         pci_set_name_list_path(pacc, optarg, 0);
964         break;
965       case 'm':
966         opt_machine++;
967         break;
968       case 'p':
969         opt_pcimap = optarg;
970         break;
971 #ifdef PCI_OS_LINUX
972       case 'k':
973         opt_kernel++;
974         break;
975 #endif
976       case 'M':
977         opt_map_mode++;
978         break;
979       case 'D':
980         opt_domains = 2;
981         break;
982 #ifdef PCI_USE_DNS
983       case 'q':
984         opt_query_dns++;
985         break;
986       case 'Q':
987         opt_query_all = 1;
988         break;
989 #else
990       case 'q':
991       case 'Q':
992         die("DNS queries are not available in this version");
993 #endif
994       default:
995         if (parse_generic_option(i, pacc, optarg))
996           break;
997       bad:
998         fprintf(stderr, help_msg, pacc->id_file_name);
999         return 1;
1000       }
1001   if (optind < argc)
1002     goto bad;
1003
1004   if (opt_query_dns)
1005     {
1006       pacc->id_lookup_mode |= PCI_LOOKUP_NETWORK;
1007       if (opt_query_dns > 1)
1008         pacc->id_lookup_mode |= PCI_LOOKUP_REFRESH_CACHE;
1009     }
1010   if (opt_query_all)
1011     pacc->id_lookup_mode |= PCI_LOOKUP_NETWORK | PCI_LOOKUP_SKIP_LOCAL;
1012
1013   pci_init(pacc);
1014   if (opt_map_mode)
1015     map_the_bus();
1016   else
1017     {
1018       scan_devices();
1019       sort_them();
1020       if (opt_tree)
1021         show_forest();
1022       else
1023         show();
1024     }
1025   pci_cleanup(pacc);
1026
1027   return (seen_errors ? 2 : 0);
1028 }