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