]> mj.ucw.cz Git - pciutils.git/blob - lspci.c
fe7b7fe0fd6c49fa140afeba77d2816e0c2699ff
[pciutils.git] / lspci.c
1 /*
2  *      The PCI Utilities -- List All PCI Devices
3  *
4  *      Copyright (c) 1997--2015 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);
142   return d;
143 }
144
145 static void
146 scan_devices(void)
147 {
148   struct device *d;
149   struct pci_dev *p;
150
151   pci_scan_bus(pacc);
152   for (p=pacc->devices; p; p=p->next)
153     if (d = scan_device(p))
154       {
155         d->next = first_dev;
156         first_dev = d;
157       }
158 }
159
160 /*** Config space accesses ***/
161
162 static void
163 check_conf_range(struct device *d, unsigned int pos, unsigned int len)
164 {
165   while (len)
166     if (!d->present[pos])
167       die("Internal bug: Accessing non-read configuration byte at position %x", pos);
168     else
169       pos++, len--;
170 }
171
172 byte
173 get_conf_byte(struct device *d, unsigned int pos)
174 {
175   check_conf_range(d, pos, 1);
176   return d->config[pos];
177 }
178
179 word
180 get_conf_word(struct device *d, unsigned int pos)
181 {
182   check_conf_range(d, pos, 2);
183   return d->config[pos] | (d->config[pos+1] << 8);
184 }
185
186 u32
187 get_conf_long(struct device *d, unsigned int pos)
188 {
189   check_conf_range(d, pos, 4);
190   return d->config[pos] |
191     (d->config[pos+1] << 8) |
192     (d->config[pos+2] << 16) |
193     (d->config[pos+3] << 24);
194 }
195
196 /*** Sorting ***/
197
198 static int
199 compare_them(const void *A, const void *B)
200 {
201   const struct pci_dev *a = (*(const struct device **)A)->dev;
202   const struct pci_dev *b = (*(const struct device **)B)->dev;
203
204   if (a->domain < b->domain)
205     return -1;
206   if (a->domain > b->domain)
207     return 1;
208   if (a->bus < b->bus)
209     return -1;
210   if (a->bus > b->bus)
211     return 1;
212   if (a->dev < b->dev)
213     return -1;
214   if (a->dev > b->dev)
215     return 1;
216   if (a->func < b->func)
217     return -1;
218   if (a->func > b->func)
219     return 1;
220   return 0;
221 }
222
223 static void
224 sort_them(void)
225 {
226   struct device **index, **h, **last_dev;
227   int cnt;
228   struct device *d;
229
230   cnt = 0;
231   for (d=first_dev; d; d=d->next)
232     cnt++;
233   h = index = alloca(sizeof(struct device *) * cnt);
234   for (d=first_dev; d; d=d->next)
235     *h++ = d;
236   qsort(index, cnt, sizeof(struct device *), compare_them);
237   last_dev = &first_dev;
238   h = index;
239   while (cnt--)
240     {
241       *last_dev = *h;
242       last_dev = &(*h)->next;
243       h++;
244     }
245   *last_dev = NULL;
246 }
247
248 /*** Normal output ***/
249
250 static void
251 show_slot_name(struct device *d)
252 {
253   struct pci_dev *p = d->dev;
254
255   if (!opt_machine ? opt_domains : (p->domain || opt_domains >= 2))
256     printf("%04x:", p->domain);
257   printf("%02x:%02x.%d", p->bus, p->dev, p->func);
258 }
259
260 void
261 get_subid(struct device *d, word *subvp, word *subdp)
262 {
263   byte htype = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
264
265   if (htype == PCI_HEADER_TYPE_NORMAL)
266     {
267       *subvp = get_conf_word(d, PCI_SUBSYSTEM_VENDOR_ID);
268       *subdp = get_conf_word(d, PCI_SUBSYSTEM_ID);
269     }
270   else if (htype == PCI_HEADER_TYPE_CARDBUS && d->config_cached >= 128)
271     {
272       *subvp = get_conf_word(d, PCI_CB_SUBSYSTEM_VENDOR_ID);
273       *subdp = get_conf_word(d, PCI_CB_SUBSYSTEM_ID);
274     }
275   else
276     *subvp = *subdp = 0xffff;
277 }
278
279 static void
280 show_terse(struct device *d)
281 {
282   int c;
283   struct pci_dev *p = d->dev;
284   char classbuf[128], devbuf[128];
285
286   show_slot_name(d);
287   printf(" %s: %s",
288          pci_lookup_name(pacc, classbuf, sizeof(classbuf),
289                          PCI_LOOKUP_CLASS,
290                          p->device_class),
291          pci_lookup_name(pacc, devbuf, sizeof(devbuf),
292                          PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
293                          p->vendor_id, p->device_id));
294   if (c = get_conf_byte(d, PCI_REVISION_ID))
295     printf(" (rev %02x)", c);
296   if (verbose)
297     {
298       char *x;
299       c = get_conf_byte(d, PCI_CLASS_PROG);
300       x = pci_lookup_name(pacc, devbuf, sizeof(devbuf),
301                           PCI_LOOKUP_PROGIF | PCI_LOOKUP_NO_NUMBERS,
302                           p->device_class, c);
303       if (c || x)
304         {
305           printf(" (prog-if %02x", c);
306           if (x)
307             printf(" [%s]", x);
308           putchar(')');
309         }
310     }
311   putchar('\n');
312
313   if (verbose || opt_kernel)
314     {
315       word subsys_v, subsys_d;
316       char ssnamebuf[256];
317
318       if (p->label)
319         printf("\tDeviceName: %s", p->label);
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   static const char suffix[][2] = { "", "K", "M", "G", "T" };
335   unsigned i;
336   if (!x)
337     return;
338   for (i = 0; i < (sizeof(suffix) / sizeof(*suffix) - 1); i++) {
339     if (x < 1024)
340       break;
341     x /= 1024;
342   }
343   printf(" [size=%u%s]", (unsigned)x, suffix[i]);
344 }
345
346 static void
347 show_bases(struct device *d, int cnt)
348 {
349   struct pci_dev *p = d->dev;
350   word cmd = get_conf_word(d, PCI_COMMAND);
351   int i;
352   int virtual = 0;
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           virtual = 1;
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 || (cmd & PCI_COMMAND_IO))
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 (!virtual && !(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                 }
406             }
407           if (!done)
408             {
409               if (a)
410                 printf(PCIADDR_T_FMT, a);
411               else
412                 printf(((flg & PCI_BASE_ADDRESS_MEM_MASK) || z) ? "<ignored>" : "<unassigned>");
413             }
414           printf(" (%s, %sprefetchable)",
415                  (t == PCI_BASE_ADDRESS_MEM_TYPE_32) ? "32-bit" :
416                  (t == PCI_BASE_ADDRESS_MEM_TYPE_64) ? "64-bit" :
417                  (t == PCI_BASE_ADDRESS_MEM_TYPE_1M) ? "low-1M" : "type 3",
418                  (flg & PCI_BASE_ADDRESS_MEM_PREFETCH) ? "" : "non-");
419           if (!virtual && !(cmd & PCI_COMMAND_MEMORY))
420             printf(" [disabled]");
421         }
422       show_size(len);
423       putchar('\n');
424     }
425 }
426
427 static void
428 show_rom(struct device *d, int reg)
429 {
430   struct pci_dev *p = d->dev;
431   pciaddr_t rom = p->rom_base_addr;
432   pciaddr_t len = (p->known_fields & PCI_FILL_SIZES) ? p->rom_size : 0;
433   u32 flg = get_conf_long(d, reg);
434   word cmd = get_conf_word(d, PCI_COMMAND);
435   int virtual = 0;
436
437   if (!rom && !flg && !len)
438     return;
439   putchar('\t');
440   if ((rom & PCI_ROM_ADDRESS_MASK) && !(flg & PCI_ROM_ADDRESS_MASK))
441     {
442       printf("[virtual] ");
443       flg = rom;
444       virtual = 1;
445     }
446   printf("Expansion ROM at ");
447   if (rom & PCI_ROM_ADDRESS_MASK)
448     printf(PCIADDR_T_FMT, rom & PCI_ROM_ADDRESS_MASK);
449   else if (flg & PCI_ROM_ADDRESS_MASK)
450     printf("<ignored>");
451   else
452     printf("<unassigned>");
453   if (!(flg & PCI_ROM_ADDRESS_ENABLE))
454     printf(" [disabled]");
455   else if (!virtual && !(cmd & PCI_COMMAND_MEMORY))
456     printf(" [disabled by cmd]");
457   show_size(len);
458   putchar('\n');
459 }
460
461 static void
462 show_htype0(struct device *d)
463 {
464   show_bases(d, 6);
465   show_rom(d, PCI_ROM_ADDRESS);
466   show_caps(d, PCI_CAPABILITY_LIST);
467 }
468
469 static void
470 show_htype1(struct device *d)
471 {
472   u32 io_base = get_conf_byte(d, PCI_IO_BASE);
473   u32 io_limit = get_conf_byte(d, PCI_IO_LIMIT);
474   u32 io_type = io_base & PCI_IO_RANGE_TYPE_MASK;
475   u32 mem_base = get_conf_word(d, PCI_MEMORY_BASE);
476   u32 mem_limit = get_conf_word(d, PCI_MEMORY_LIMIT);
477   u32 mem_type = mem_base & PCI_MEMORY_RANGE_TYPE_MASK;
478   u32 pref_base = get_conf_word(d, PCI_PREF_MEMORY_BASE);
479   u32 pref_limit = get_conf_word(d, PCI_PREF_MEMORY_LIMIT);
480   u32 pref_type = pref_base & PCI_PREF_RANGE_TYPE_MASK;
481   word sec_stat = get_conf_word(d, PCI_SEC_STATUS);
482   word brc = get_conf_word(d, PCI_BRIDGE_CONTROL);
483   int verb = verbose > 2;
484
485   show_bases(d, 2);
486   printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
487          get_conf_byte(d, PCI_PRIMARY_BUS),
488          get_conf_byte(d, PCI_SECONDARY_BUS),
489          get_conf_byte(d, PCI_SUBORDINATE_BUS),
490          get_conf_byte(d, PCI_SEC_LATENCY_TIMER));
491
492   if (io_type != (io_limit & PCI_IO_RANGE_TYPE_MASK) ||
493       (io_type != PCI_IO_RANGE_TYPE_16 && io_type != PCI_IO_RANGE_TYPE_32))
494     printf("\t!!! Unknown I/O range types %x/%x\n", io_base, io_limit);
495   else
496     {
497       io_base = (io_base & PCI_IO_RANGE_MASK) << 8;
498       io_limit = (io_limit & PCI_IO_RANGE_MASK) << 8;
499       if (io_type == PCI_IO_RANGE_TYPE_32)
500         {
501           io_base |= (get_conf_word(d, PCI_IO_BASE_UPPER16) << 16);
502           io_limit |= (get_conf_word(d, PCI_IO_LIMIT_UPPER16) << 16);
503         }
504       if (io_base <= io_limit || verb)
505         printf("\tI/O behind bridge: %08x-%08x\n", io_base, io_limit+0xfff);
506     }
507
508   if (mem_type != (mem_limit & PCI_MEMORY_RANGE_TYPE_MASK) ||
509       mem_type)
510     printf("\t!!! Unknown memory range types %x/%x\n", mem_base, mem_limit);
511   else
512     {
513       mem_base = (mem_base & PCI_MEMORY_RANGE_MASK) << 16;
514       mem_limit = (mem_limit & PCI_MEMORY_RANGE_MASK) << 16;
515       if (mem_base <= mem_limit || verb)
516         printf("\tMemory behind bridge: %08x-%08x\n", mem_base, mem_limit + 0xfffff);
517     }
518
519   if (pref_type != (pref_limit & PCI_PREF_RANGE_TYPE_MASK) ||
520       (pref_type != PCI_PREF_RANGE_TYPE_32 && pref_type != PCI_PREF_RANGE_TYPE_64))
521     printf("\t!!! Unknown prefetchable memory range types %x/%x\n", pref_base, pref_limit);
522   else
523     {
524       pref_base = (pref_base & PCI_PREF_RANGE_MASK) << 16;
525       pref_limit = (pref_limit & PCI_PREF_RANGE_MASK) << 16;
526       if (pref_base <= pref_limit || verb)
527         {
528           if (pref_type == PCI_PREF_RANGE_TYPE_32)
529             printf("\tPrefetchable memory behind bridge: %08x-%08x\n", pref_base, pref_limit + 0xfffff);
530           else
531             printf("\tPrefetchable memory behind bridge: %08x%08x-%08x%08x\n",
532                    get_conf_long(d, PCI_PREF_BASE_UPPER32),
533                    pref_base,
534                    get_conf_long(d, PCI_PREF_LIMIT_UPPER32),
535                    pref_limit + 0xfffff);
536         }
537     }
538
539   if (verbose > 1)
540     printf("\tSecondary status: 66MHz%c FastB2B%c ParErr%c DEVSEL=%s >TAbort%c <TAbort%c <MAbort%c <SERR%c <PERR%c\n",
541              FLAG(sec_stat, PCI_STATUS_66MHZ),
542              FLAG(sec_stat, PCI_STATUS_FAST_BACK),
543              FLAG(sec_stat, PCI_STATUS_PARITY),
544              ((sec_stat & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
545              ((sec_stat & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
546              ((sec_stat & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??",
547              FLAG(sec_stat, PCI_STATUS_SIG_TARGET_ABORT),
548              FLAG(sec_stat, PCI_STATUS_REC_TARGET_ABORT),
549              FLAG(sec_stat, PCI_STATUS_REC_MASTER_ABORT),
550              FLAG(sec_stat, PCI_STATUS_SIG_SYSTEM_ERROR),
551              FLAG(sec_stat, PCI_STATUS_DETECTED_PARITY));
552
553   show_rom(d, PCI_ROM_ADDRESS1);
554
555   if (verbose > 1)
556     {
557       printf("\tBridgeCtl: Parity%c SERR%c NoISA%c VGA%c MAbort%c >Reset%c FastB2B%c\n",
558         FLAG(brc, PCI_BRIDGE_CTL_PARITY),
559         FLAG(brc, PCI_BRIDGE_CTL_SERR),
560         FLAG(brc, PCI_BRIDGE_CTL_NO_ISA),
561         FLAG(brc, PCI_BRIDGE_CTL_VGA),
562         FLAG(brc, PCI_BRIDGE_CTL_MASTER_ABORT),
563         FLAG(brc, PCI_BRIDGE_CTL_BUS_RESET),
564         FLAG(brc, PCI_BRIDGE_CTL_FAST_BACK));
565       printf("\t\tPriDiscTmr%c SecDiscTmr%c DiscTmrStat%c DiscTmrSERREn%c\n",
566         FLAG(brc, PCI_BRIDGE_CTL_PRI_DISCARD_TIMER),
567         FLAG(brc, PCI_BRIDGE_CTL_SEC_DISCARD_TIMER),
568         FLAG(brc, PCI_BRIDGE_CTL_DISCARD_TIMER_STATUS),
569         FLAG(brc, PCI_BRIDGE_CTL_DISCARD_TIMER_SERR_EN));
570     }
571
572   show_caps(d, PCI_CAPABILITY_LIST);
573 }
574
575 static void
576 show_htype2(struct device *d)
577 {
578   int i;
579   word cmd = get_conf_word(d, PCI_COMMAND);
580   word brc = get_conf_word(d, PCI_CB_BRIDGE_CONTROL);
581   word exca;
582   int verb = verbose > 2;
583
584   show_bases(d, 1);
585   printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
586          get_conf_byte(d, PCI_CB_PRIMARY_BUS),
587          get_conf_byte(d, PCI_CB_CARD_BUS),
588          get_conf_byte(d, PCI_CB_SUBORDINATE_BUS),
589          get_conf_byte(d, PCI_CB_LATENCY_TIMER));
590   for (i=0; i<2; i++)
591     {
592       int p = 8*i;
593       u32 base = get_conf_long(d, PCI_CB_MEMORY_BASE_0 + p);
594       u32 limit = get_conf_long(d, PCI_CB_MEMORY_LIMIT_0 + p);
595       limit = limit + 0xfff;
596       if (base <= limit || verb)
597         printf("\tMemory window %d: %08x-%08x%s%s\n", i, base, limit,
598                (cmd & PCI_COMMAND_MEMORY) ? "" : " [disabled]",
599                (brc & (PCI_CB_BRIDGE_CTL_PREFETCH_MEM0 << i)) ? " (prefetchable)" : "");
600     }
601   for (i=0; i<2; i++)
602     {
603       int p = 8*i;
604       u32 base = get_conf_long(d, PCI_CB_IO_BASE_0 + p);
605       u32 limit = get_conf_long(d, PCI_CB_IO_LIMIT_0 + p);
606       if (!(base & PCI_IO_RANGE_TYPE_32))
607         {
608           base &= 0xffff;
609           limit &= 0xffff;
610         }
611       base &= PCI_CB_IO_RANGE_MASK;
612       limit = (limit & PCI_CB_IO_RANGE_MASK) + 3;
613       if (base <= limit || verb)
614         printf("\tI/O window %d: %08x-%08x%s\n", i, base, limit,
615                (cmd & PCI_COMMAND_IO) ? "" : " [disabled]");
616     }
617
618   if (get_conf_word(d, PCI_CB_SEC_STATUS) & PCI_STATUS_SIG_SYSTEM_ERROR)
619     printf("\tSecondary status: SERR\n");
620   if (verbose > 1)
621     printf("\tBridgeCtl: Parity%c SERR%c ISA%c VGA%c MAbort%c >Reset%c 16bInt%c PostWrite%c\n",
622            FLAG(brc, PCI_CB_BRIDGE_CTL_PARITY),
623            FLAG(brc, PCI_CB_BRIDGE_CTL_SERR),
624            FLAG(brc, PCI_CB_BRIDGE_CTL_ISA),
625            FLAG(brc, PCI_CB_BRIDGE_CTL_VGA),
626            FLAG(brc, PCI_CB_BRIDGE_CTL_MASTER_ABORT),
627            FLAG(brc, PCI_CB_BRIDGE_CTL_CB_RESET),
628            FLAG(brc, PCI_CB_BRIDGE_CTL_16BIT_INT),
629            FLAG(brc, PCI_CB_BRIDGE_CTL_POST_WRITES));
630
631   if (d->config_cached < 128)
632     {
633       printf("\t<access denied to the rest>\n");
634       return;
635     }
636
637   exca = get_conf_word(d, PCI_CB_LEGACY_MODE_BASE);
638   if (exca)
639     printf("\t16-bit legacy interface ports at %04x\n", exca);
640   show_caps(d, PCI_CB_CAPABILITY_LIST);
641 }
642
643 static void
644 show_verbose(struct device *d)
645 {
646   struct pci_dev *p = d->dev;
647   word status = get_conf_word(d, PCI_STATUS);
648   word cmd = get_conf_word(d, PCI_COMMAND);
649   word class = p->device_class;
650   byte bist = get_conf_byte(d, PCI_BIST);
651   byte htype = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
652   byte latency = get_conf_byte(d, PCI_LATENCY_TIMER);
653   byte cache_line = get_conf_byte(d, PCI_CACHE_LINE_SIZE);
654   byte max_lat, min_gnt;
655   byte int_pin = get_conf_byte(d, PCI_INTERRUPT_PIN);
656   unsigned int irq;
657
658   show_terse(d);
659
660   pci_fill_info(p, PCI_FILL_IRQ | PCI_FILL_BASES | PCI_FILL_ROM_BASE | PCI_FILL_SIZES |
661     PCI_FILL_PHYS_SLOT | PCI_FILL_LABEL | PCI_FILL_NUMA_NODE);
662   irq = p->irq;
663
664   switch (htype)
665     {
666     case PCI_HEADER_TYPE_NORMAL:
667       if (class == PCI_CLASS_BRIDGE_PCI)
668         printf("\t!!! Invalid class %04x for header type %02x\n", class, htype);
669       max_lat = get_conf_byte(d, PCI_MAX_LAT);
670       min_gnt = get_conf_byte(d, PCI_MIN_GNT);
671       break;
672     case PCI_HEADER_TYPE_BRIDGE:
673       if ((class >> 8) != PCI_BASE_CLASS_BRIDGE)
674         printf("\t!!! Invalid class %04x for header type %02x\n", class, htype);
675       min_gnt = max_lat = 0;
676       break;
677     case PCI_HEADER_TYPE_CARDBUS:
678       if ((class >> 8) != PCI_BASE_CLASS_BRIDGE)
679         printf("\t!!! Invalid class %04x for header type %02x\n", class, htype);
680       min_gnt = max_lat = 0;
681       break;
682     default:
683       printf("\t!!! Unknown header type %02x\n", htype);
684       return;
685     }
686
687   if (p->phy_slot)
688     printf("\tPhysical Slot: %s\n", p->phy_slot);
689
690   if (verbose > 1)
691     {
692       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",
693              FLAG(cmd, PCI_COMMAND_IO),
694              FLAG(cmd, PCI_COMMAND_MEMORY),
695              FLAG(cmd, PCI_COMMAND_MASTER),
696              FLAG(cmd, PCI_COMMAND_SPECIAL),
697              FLAG(cmd, PCI_COMMAND_INVALIDATE),
698              FLAG(cmd, PCI_COMMAND_VGA_PALETTE),
699              FLAG(cmd, PCI_COMMAND_PARITY),
700              FLAG(cmd, PCI_COMMAND_WAIT),
701              FLAG(cmd, PCI_COMMAND_SERR),
702              FLAG(cmd, PCI_COMMAND_FAST_BACK),
703              FLAG(cmd, PCI_COMMAND_DISABLE_INTx));
704       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",
705              FLAG(status, PCI_STATUS_CAP_LIST),
706              FLAG(status, PCI_STATUS_66MHZ),
707              FLAG(status, PCI_STATUS_UDF),
708              FLAG(status, PCI_STATUS_FAST_BACK),
709              FLAG(status, PCI_STATUS_PARITY),
710              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
711              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
712              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??",
713              FLAG(status, PCI_STATUS_SIG_TARGET_ABORT),
714              FLAG(status, PCI_STATUS_REC_TARGET_ABORT),
715              FLAG(status, PCI_STATUS_REC_MASTER_ABORT),
716              FLAG(status, PCI_STATUS_SIG_SYSTEM_ERROR),
717              FLAG(status, PCI_STATUS_DETECTED_PARITY),
718              FLAG(status, PCI_STATUS_INTx));
719       if (cmd & PCI_COMMAND_MASTER)
720         {
721           printf("\tLatency: %d", latency);
722           if (min_gnt || max_lat)
723             {
724               printf(" (");
725               if (min_gnt)
726                 printf("%dns min", min_gnt*250);
727               if (min_gnt && max_lat)
728                 printf(", ");
729               if (max_lat)
730                 printf("%dns max", max_lat*250);
731               putchar(')');
732             }
733           if (cache_line)
734             printf(", Cache Line Size: %d bytes", cache_line * 4);
735           putchar('\n');
736         }
737       if (int_pin || irq)
738         printf("\tInterrupt: pin %c routed to IRQ " PCIIRQ_FMT "\n",
739                (int_pin ? 'A' + int_pin - 1 : '?'), irq);
740       if (p->numa_node != -1)
741         printf("\tNUMA node: %d\n", p->numa_node);
742     }
743   else
744     {
745       printf("\tFlags: ");
746       if (cmd & PCI_COMMAND_MASTER)
747         printf("bus master, ");
748       if (cmd & PCI_COMMAND_VGA_PALETTE)
749         printf("VGA palette snoop, ");
750       if (cmd & PCI_COMMAND_WAIT)
751         printf("stepping, ");
752       if (cmd & PCI_COMMAND_FAST_BACK)
753         printf("fast Back2Back, ");
754       if (status & PCI_STATUS_66MHZ)
755         printf("66MHz, ");
756       if (status & PCI_STATUS_UDF)
757         printf("user-definable features, ");
758       printf("%s devsel",
759              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
760              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
761              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??");
762       if (cmd & PCI_COMMAND_MASTER)
763         printf(", latency %d", latency);
764       if (irq)
765         printf(", IRQ " PCIIRQ_FMT, irq);
766       if (p->numa_node != -1)
767         printf(", NUMA node %d", p->numa_node);
768       putchar('\n');
769     }
770
771   if (bist & PCI_BIST_CAPABLE)
772     {
773       if (bist & PCI_BIST_START)
774         printf("\tBIST is running\n");
775       else
776         printf("\tBIST result: %02x\n", bist & PCI_BIST_CODE_MASK);
777     }
778
779   switch (htype)
780     {
781     case PCI_HEADER_TYPE_NORMAL:
782       show_htype0(d);
783       break;
784     case PCI_HEADER_TYPE_BRIDGE:
785       show_htype1(d);
786       break;
787     case PCI_HEADER_TYPE_CARDBUS:
788       show_htype2(d);
789       break;
790     }
791 }
792
793 /*** Machine-readable dumps ***/
794
795 static void
796 show_hex_dump(struct device *d)
797 {
798   unsigned int i, cnt;
799
800   cnt = d->config_cached;
801   if (opt_hex >= 3 && config_fetch(d, cnt, 256-cnt))
802     {
803       cnt = 256;
804       if (opt_hex >= 4 && config_fetch(d, 256, 4096-256))
805         cnt = 4096;
806     }
807
808   for (i=0; i<cnt; i++)
809     {
810       if (! (i & 15))
811         printf("%02x:", i);
812       printf(" %02x", get_conf_byte(d, i));
813       if ((i & 15) == 15)
814         putchar('\n');
815     }
816 }
817
818 static void
819 print_shell_escaped(char *c)
820 {
821   printf(" \"");
822   while (*c)
823     {
824       if (*c == '"' || *c == '\\')
825         putchar('\\');
826       putchar(*c++);
827     }
828   putchar('"');
829 }
830
831 static void
832 show_machine(struct device *d)
833 {
834   struct pci_dev *p = d->dev;
835   int c;
836   word sv_id, sd_id;
837   char classbuf[128], vendbuf[128], devbuf[128], svbuf[128], sdbuf[128];
838
839   get_subid(d, &sv_id, &sd_id);
840
841   if (verbose)
842     {
843       pci_fill_info(p, PCI_FILL_PHYS_SLOT | PCI_FILL_NUMA_NODE);
844       printf((opt_machine >= 2) ? "Slot:\t" : "Device:\t");
845       show_slot_name(d);
846       putchar('\n');
847       printf("Class:\t%s\n",
848              pci_lookup_name(pacc, classbuf, sizeof(classbuf), PCI_LOOKUP_CLASS, p->device_class));
849       printf("Vendor:\t%s\n",
850              pci_lookup_name(pacc, vendbuf, sizeof(vendbuf), PCI_LOOKUP_VENDOR, p->vendor_id, p->device_id));
851       printf("Device:\t%s\n",
852              pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id));
853       if (sv_id && sv_id != 0xffff)
854         {
855           printf("SVendor:\t%s\n",
856                  pci_lookup_name(pacc, svbuf, sizeof(svbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR, sv_id));
857           printf("SDevice:\t%s\n",
858                  pci_lookup_name(pacc, sdbuf, sizeof(sdbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id, sv_id, sd_id));
859         }
860       if (p->phy_slot)
861         printf("PhySlot:\t%s\n", p->phy_slot);
862       if (c = get_conf_byte(d, PCI_REVISION_ID))
863         printf("Rev:\t%02x\n", c);
864       if (c = get_conf_byte(d, PCI_CLASS_PROG))
865         printf("ProgIf:\t%02x\n", c);
866       if (opt_kernel)
867         show_kernel_machine(d);
868       if (p->numa_node != -1)
869         printf("NUMANode:\t%d\n", p->numa_node);
870     }
871   else
872     {
873       show_slot_name(d);
874       print_shell_escaped(pci_lookup_name(pacc, classbuf, sizeof(classbuf), PCI_LOOKUP_CLASS, p->device_class));
875       print_shell_escaped(pci_lookup_name(pacc, vendbuf, sizeof(vendbuf), PCI_LOOKUP_VENDOR, p->vendor_id, p->device_id));
876       print_shell_escaped(pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id));
877       if (c = get_conf_byte(d, PCI_REVISION_ID))
878         printf(" -r%02x", c);
879       if (c = get_conf_byte(d, PCI_CLASS_PROG))
880         printf(" -p%02x", c);
881       if (sv_id && sv_id != 0xffff)
882         {
883           print_shell_escaped(pci_lookup_name(pacc, svbuf, sizeof(svbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR, sv_id));
884           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));
885         }
886       else
887         printf(" \"\" \"\"");
888       putchar('\n');
889     }
890 }
891
892 /*** Main show function ***/
893
894 void
895 show_device(struct device *d)
896 {
897   if (opt_machine)
898     show_machine(d);
899   else
900     {
901       if (verbose)
902         show_verbose(d);
903       else
904         show_terse(d);
905       if (opt_kernel || verbose)
906         show_kernel(d);
907     }
908   if (opt_hex)
909     show_hex_dump(d);
910   if (verbose || opt_hex)
911     putchar('\n');
912 }
913
914 static void
915 show(void)
916 {
917   struct device *d;
918
919   for (d=first_dev; d; d=d->next)
920     show_device(d);
921 }
922
923 /* Main */
924
925 int
926 main(int argc, char **argv)
927 {
928   int i;
929   char *msg;
930
931   if (argc == 2 && !strcmp(argv[1], "--version"))
932     {
933       puts("lspci version " PCIUTILS_VERSION);
934       return 0;
935     }
936
937   pacc = pci_alloc();
938   pacc->error = die;
939   pci_filter_init(pacc, &filter);
940
941   while ((i = getopt(argc, argv, options)) != -1)
942     switch (i)
943       {
944       case 'n':
945         pacc->numeric_ids++;
946         break;
947       case 'v':
948         verbose++;
949         break;
950       case 'b':
951         pacc->buscentric = 1;
952         break;
953       case 's':
954         if (msg = pci_filter_parse_slot(&filter, optarg))
955           die("-s: %s", msg);
956         break;
957       case 'd':
958         if (msg = pci_filter_parse_id(&filter, optarg))
959           die("-d: %s", msg);
960         break;
961       case 'x':
962         opt_hex++;
963         break;
964       case 't':
965         opt_tree++;
966         break;
967       case 'i':
968         pci_set_name_list_path(pacc, optarg, 0);
969         break;
970       case 'm':
971         opt_machine++;
972         break;
973       case 'p':
974         opt_pcimap = optarg;
975         break;
976 #ifdef PCI_OS_LINUX
977       case 'k':
978         opt_kernel++;
979         break;
980 #endif
981       case 'M':
982         opt_map_mode++;
983         break;
984       case 'D':
985         opt_domains = 2;
986         break;
987 #ifdef PCI_USE_DNS
988       case 'q':
989         opt_query_dns++;
990         break;
991       case 'Q':
992         opt_query_all = 1;
993         break;
994 #else
995       case 'q':
996       case 'Q':
997         die("DNS queries are not available in this version");
998 #endif
999       default:
1000         if (parse_generic_option(i, pacc, optarg))
1001           break;
1002       bad:
1003         fprintf(stderr, help_msg, pacc->id_file_name);
1004         return 1;
1005       }
1006   if (optind < argc)
1007     goto bad;
1008
1009   if (opt_query_dns)
1010     {
1011       pacc->id_lookup_mode |= PCI_LOOKUP_NETWORK;
1012       if (opt_query_dns > 1)
1013         pacc->id_lookup_mode |= PCI_LOOKUP_REFRESH_CACHE;
1014     }
1015   if (opt_query_all)
1016     pacc->id_lookup_mode |= PCI_LOOKUP_NETWORK | PCI_LOOKUP_SKIP_LOCAL;
1017
1018   pci_init(pacc);
1019   if (opt_map_mode)
1020     map_the_bus();
1021   else
1022     {
1023       scan_devices();
1024       sort_them();
1025       if (opt_tree)
1026         show_forest();
1027       else
1028         show();
1029     }
1030   show_kernel_cleanup();
1031   pci_cleanup(pacc);
1032
1033   return (seen_errors ? 2 : 0);
1034 }