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