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