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