]> mj.ucw.cz Git - pciutils.git/blob - lspci.c
Sylixos port
[pciutils.git] / lspci.c
1 /*
2  *      The PCI Utilities -- List All PCI Devices
3  *
4  *      Copyright (c) 1997--2016 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(u64 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_range(char *prefix, u64 base, u64 limit, int is_64bit)
348 {
349   if (base > limit)
350     {
351       if (!verbose)
352         return;
353       else if (verbose < 3)
354         {
355           printf("%s: None\n", prefix);
356           return;
357         }
358     }
359
360   printf("%s: ", prefix);
361   if (is_64bit)
362     printf("%016" PCI_U64_FMT_X "-%016" PCI_U64_FMT_X, base, limit);
363   else
364     printf("%08x-%08x", (unsigned) base, (unsigned) limit);
365   if (base <= limit)
366     show_size(limit - base + 1);
367   else
368     printf(" [empty]");
369   putchar('\n');
370 }
371
372 static void
373 show_bases(struct device *d, int cnt)
374 {
375   struct pci_dev *p = d->dev;
376   word cmd = get_conf_word(d, PCI_COMMAND);
377   int i;
378   int virtual = 0;
379
380   for (i=0; i<cnt; i++)
381     {
382       pciaddr_t pos = p->base_addr[i];
383       pciaddr_t len = (p->known_fields & PCI_FILL_SIZES) ? p->size[i] : 0;
384       pciaddr_t ioflg = (p->known_fields & PCI_FILL_IO_FLAGS) ? p->flags[i] : 0;
385       u32 flg = get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i);
386       if (flg == 0xffffffff)
387         flg = 0;
388       if (!pos && !flg && !len)
389         continue;
390       if (verbose > 1)
391         printf("\tRegion %d: ", i);
392       else
393         putchar('\t');
394       if (ioflg & PCI_IORESOURCE_PCI_EA_BEI)
395           printf("[enhanced] ");
396       else if (pos && !flg)     /* Reported by the OS, but not by the device */
397         {
398           printf("[virtual] ");
399           flg = pos;
400           virtual = 1;
401         }
402       if (flg & PCI_BASE_ADDRESS_SPACE_IO)
403         {
404           pciaddr_t a = pos & PCI_BASE_ADDRESS_IO_MASK;
405           printf("I/O ports at ");
406           if (a || (cmd & PCI_COMMAND_IO))
407             printf(PCIADDR_PORT_FMT, a);
408           else if (flg & PCI_BASE_ADDRESS_IO_MASK)
409             printf("<ignored>");
410           else
411             printf("<unassigned>");
412           if (!virtual && !(cmd & PCI_COMMAND_IO))
413             printf(" [disabled]");
414         }
415       else
416         {
417           int t = flg & PCI_BASE_ADDRESS_MEM_TYPE_MASK;
418           pciaddr_t a = pos & PCI_ADDR_MEM_MASK;
419           int done = 0;
420           u32 z = 0;
421
422           printf("Memory at ");
423           if (t == PCI_BASE_ADDRESS_MEM_TYPE_64)
424             {
425               if (i >= cnt - 1)
426                 {
427                   printf("<invalid-64bit-slot>");
428                   done = 1;
429                 }
430               else
431                 {
432                   i++;
433                   z = get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i);
434                 }
435             }
436           if (!done)
437             {
438               if (a)
439                 printf(PCIADDR_T_FMT, a);
440               else
441                 printf(((flg & PCI_BASE_ADDRESS_MEM_MASK) || z) ? "<ignored>" : "<unassigned>");
442             }
443           printf(" (%s, %sprefetchable)",
444                  (t == PCI_BASE_ADDRESS_MEM_TYPE_32) ? "32-bit" :
445                  (t == PCI_BASE_ADDRESS_MEM_TYPE_64) ? "64-bit" :
446                  (t == PCI_BASE_ADDRESS_MEM_TYPE_1M) ? "low-1M" : "type 3",
447                  (flg & PCI_BASE_ADDRESS_MEM_PREFETCH) ? "" : "non-");
448           if (!virtual && !(cmd & PCI_COMMAND_MEMORY))
449             printf(" [disabled]");
450         }
451       show_size(len);
452       putchar('\n');
453     }
454 }
455
456 static void
457 show_rom(struct device *d, int reg)
458 {
459   struct pci_dev *p = d->dev;
460   pciaddr_t rom = p->rom_base_addr;
461   pciaddr_t len = (p->known_fields & PCI_FILL_SIZES) ? p->rom_size : 0;
462   pciaddr_t ioflg = (p->known_fields & PCI_FILL_IO_FLAGS) ? p->rom_flags : 0;
463   u32 flg = get_conf_long(d, reg);
464   word cmd = get_conf_word(d, PCI_COMMAND);
465   int virtual = 0;
466
467   if (!rom && !flg && !len)
468     return;
469   putchar('\t');
470   if (ioflg & PCI_IORESOURCE_PCI_EA_BEI)
471       printf("[enhanced] ");
472   else if ((rom & PCI_ROM_ADDRESS_MASK) && !(flg & PCI_ROM_ADDRESS_MASK))
473     {
474       printf("[virtual] ");
475       flg = rom;
476       virtual = 1;
477     }
478   printf("Expansion ROM at ");
479   if (rom & PCI_ROM_ADDRESS_MASK)
480     printf(PCIADDR_T_FMT, rom & PCI_ROM_ADDRESS_MASK);
481   else if (flg & PCI_ROM_ADDRESS_MASK)
482     printf("<ignored>");
483   else
484     printf("<unassigned>");
485   if (!(flg & PCI_ROM_ADDRESS_ENABLE))
486     printf(" [disabled]");
487   else if (!virtual && !(cmd & PCI_COMMAND_MEMORY))
488     printf(" [disabled by cmd]");
489   show_size(len);
490   putchar('\n');
491 }
492
493 static void
494 show_htype0(struct device *d)
495 {
496   show_bases(d, 6);
497   show_rom(d, PCI_ROM_ADDRESS);
498   show_caps(d, PCI_CAPABILITY_LIST);
499 }
500
501 static void
502 show_htype1(struct device *d)
503 {
504   u32 io_base = get_conf_byte(d, PCI_IO_BASE);
505   u32 io_limit = get_conf_byte(d, PCI_IO_LIMIT);
506   u32 io_type = io_base & PCI_IO_RANGE_TYPE_MASK;
507   u32 mem_base = get_conf_word(d, PCI_MEMORY_BASE);
508   u32 mem_limit = get_conf_word(d, PCI_MEMORY_LIMIT);
509   u32 mem_type = mem_base & PCI_MEMORY_RANGE_TYPE_MASK;
510   u32 pref_base = get_conf_word(d, PCI_PREF_MEMORY_BASE);
511   u32 pref_limit = get_conf_word(d, PCI_PREF_MEMORY_LIMIT);
512   u32 pref_type = pref_base & PCI_PREF_RANGE_TYPE_MASK;
513   word sec_stat = get_conf_word(d, PCI_SEC_STATUS);
514   word brc = get_conf_word(d, PCI_BRIDGE_CONTROL);
515
516   show_bases(d, 2);
517   printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
518          get_conf_byte(d, PCI_PRIMARY_BUS),
519          get_conf_byte(d, PCI_SECONDARY_BUS),
520          get_conf_byte(d, PCI_SUBORDINATE_BUS),
521          get_conf_byte(d, PCI_SEC_LATENCY_TIMER));
522
523   if (io_type != (io_limit & PCI_IO_RANGE_TYPE_MASK) ||
524       (io_type != PCI_IO_RANGE_TYPE_16 && io_type != PCI_IO_RANGE_TYPE_32))
525     printf("\t!!! Unknown I/O range types %x/%x\n", io_base, io_limit);
526   else
527     {
528       io_base = (io_base & PCI_IO_RANGE_MASK) << 8;
529       io_limit = (io_limit & PCI_IO_RANGE_MASK) << 8;
530       if (io_type == PCI_IO_RANGE_TYPE_32)
531         {
532           io_base |= (get_conf_word(d, PCI_IO_BASE_UPPER16) << 16);
533           io_limit |= (get_conf_word(d, PCI_IO_LIMIT_UPPER16) << 16);
534         }
535       show_range("\tI/O behind bridge", io_base, io_limit+0xfff, 0);
536     }
537
538   if (mem_type != (mem_limit & PCI_MEMORY_RANGE_TYPE_MASK) ||
539       mem_type)
540     printf("\t!!! Unknown memory range types %x/%x\n", mem_base, mem_limit);
541   else
542     {
543       mem_base = (mem_base & PCI_MEMORY_RANGE_MASK) << 16;
544       mem_limit = (mem_limit & PCI_MEMORY_RANGE_MASK) << 16;
545       show_range("\tMemory behind bridge", mem_base, mem_limit + 0xfffff, 0);
546     }
547
548   if (pref_type != (pref_limit & PCI_PREF_RANGE_TYPE_MASK) ||
549       (pref_type != PCI_PREF_RANGE_TYPE_32 && pref_type != PCI_PREF_RANGE_TYPE_64))
550     printf("\t!!! Unknown prefetchable memory range types %x/%x\n", pref_base, pref_limit);
551   else
552     {
553       u64 pref_base_64 = (pref_base & PCI_PREF_RANGE_MASK) << 16;
554       u64 pref_limit_64 = (pref_limit & PCI_PREF_RANGE_MASK) << 16;
555       if (pref_type == PCI_PREF_RANGE_TYPE_64)
556         {
557           pref_base_64 |= (u64) get_conf_long(d, PCI_PREF_BASE_UPPER32) << 32;
558           pref_limit_64 |= (u64) get_conf_long(d, PCI_PREF_LIMIT_UPPER32) << 32;
559         }
560       show_range("\tPrefetchable memory behind bridge", pref_base_64, pref_limit_64 + 0xfffff, (pref_type == PCI_PREF_RANGE_TYPE_64));
561     }
562
563   if (verbose > 1)
564     printf("\tSecondary status: 66MHz%c FastB2B%c ParErr%c DEVSEL=%s >TAbort%c <TAbort%c <MAbort%c <SERR%c <PERR%c\n",
565              FLAG(sec_stat, PCI_STATUS_66MHZ),
566              FLAG(sec_stat, PCI_STATUS_FAST_BACK),
567              FLAG(sec_stat, PCI_STATUS_PARITY),
568              ((sec_stat & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
569              ((sec_stat & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
570              ((sec_stat & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??",
571              FLAG(sec_stat, PCI_STATUS_SIG_TARGET_ABORT),
572              FLAG(sec_stat, PCI_STATUS_REC_TARGET_ABORT),
573              FLAG(sec_stat, PCI_STATUS_REC_MASTER_ABORT),
574              FLAG(sec_stat, PCI_STATUS_SIG_SYSTEM_ERROR),
575              FLAG(sec_stat, PCI_STATUS_DETECTED_PARITY));
576
577   show_rom(d, PCI_ROM_ADDRESS1);
578
579   if (verbose > 1)
580     {
581       printf("\tBridgeCtl: Parity%c SERR%c NoISA%c VGA%c VGA16%c MAbort%c >Reset%c FastB2B%c\n",
582         FLAG(brc, PCI_BRIDGE_CTL_PARITY),
583         FLAG(brc, PCI_BRIDGE_CTL_SERR),
584         FLAG(brc, PCI_BRIDGE_CTL_NO_ISA),
585         FLAG(brc, PCI_BRIDGE_CTL_VGA),
586         FLAG(brc, PCI_BRIDGE_CTL_VGA_16BIT),
587         FLAG(brc, PCI_BRIDGE_CTL_MASTER_ABORT),
588         FLAG(brc, PCI_BRIDGE_CTL_BUS_RESET),
589         FLAG(brc, PCI_BRIDGE_CTL_FAST_BACK));
590       printf("\t\tPriDiscTmr%c SecDiscTmr%c DiscTmrStat%c DiscTmrSERREn%c\n",
591         FLAG(brc, PCI_BRIDGE_CTL_PRI_DISCARD_TIMER),
592         FLAG(brc, PCI_BRIDGE_CTL_SEC_DISCARD_TIMER),
593         FLAG(brc, PCI_BRIDGE_CTL_DISCARD_TIMER_STATUS),
594         FLAG(brc, PCI_BRIDGE_CTL_DISCARD_TIMER_SERR_EN));
595     }
596
597   show_caps(d, PCI_CAPABILITY_LIST);
598 }
599
600 static void
601 show_htype2(struct device *d)
602 {
603   int i;
604   word cmd = get_conf_word(d, PCI_COMMAND);
605   word brc = get_conf_word(d, PCI_CB_BRIDGE_CONTROL);
606   word exca;
607   int verb = verbose > 2;
608
609   show_bases(d, 1);
610   printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
611          get_conf_byte(d, PCI_CB_PRIMARY_BUS),
612          get_conf_byte(d, PCI_CB_CARD_BUS),
613          get_conf_byte(d, PCI_CB_SUBORDINATE_BUS),
614          get_conf_byte(d, PCI_CB_LATENCY_TIMER));
615   for (i=0; i<2; i++)
616     {
617       int p = 8*i;
618       u32 base = get_conf_long(d, PCI_CB_MEMORY_BASE_0 + p);
619       u32 limit = get_conf_long(d, PCI_CB_MEMORY_LIMIT_0 + p);
620       limit = limit + 0xfff;
621       if (base <= limit || verb)
622         printf("\tMemory window %d: %08x-%08x%s%s\n", i, base, limit,
623                (cmd & PCI_COMMAND_MEMORY) ? "" : " [disabled]",
624                (brc & (PCI_CB_BRIDGE_CTL_PREFETCH_MEM0 << i)) ? " (prefetchable)" : "");
625     }
626   for (i=0; i<2; i++)
627     {
628       int p = 8*i;
629       u32 base = get_conf_long(d, PCI_CB_IO_BASE_0 + p);
630       u32 limit = get_conf_long(d, PCI_CB_IO_LIMIT_0 + p);
631       if (!(base & PCI_IO_RANGE_TYPE_32))
632         {
633           base &= 0xffff;
634           limit &= 0xffff;
635         }
636       base &= PCI_CB_IO_RANGE_MASK;
637       limit = (limit & PCI_CB_IO_RANGE_MASK) + 3;
638       if (base <= limit || verb)
639         printf("\tI/O window %d: %08x-%08x%s\n", i, base, limit,
640                (cmd & PCI_COMMAND_IO) ? "" : " [disabled]");
641     }
642
643   if (get_conf_word(d, PCI_CB_SEC_STATUS) & PCI_STATUS_SIG_SYSTEM_ERROR)
644     printf("\tSecondary status: SERR\n");
645   if (verbose > 1)
646     printf("\tBridgeCtl: Parity%c SERR%c ISA%c VGA%c MAbort%c >Reset%c 16bInt%c PostWrite%c\n",
647            FLAG(brc, PCI_CB_BRIDGE_CTL_PARITY),
648            FLAG(brc, PCI_CB_BRIDGE_CTL_SERR),
649            FLAG(brc, PCI_CB_BRIDGE_CTL_ISA),
650            FLAG(brc, PCI_CB_BRIDGE_CTL_VGA),
651            FLAG(brc, PCI_CB_BRIDGE_CTL_MASTER_ABORT),
652            FLAG(brc, PCI_CB_BRIDGE_CTL_CB_RESET),
653            FLAG(brc, PCI_CB_BRIDGE_CTL_16BIT_INT),
654            FLAG(brc, PCI_CB_BRIDGE_CTL_POST_WRITES));
655
656   if (d->config_cached < 128)
657     {
658       printf("\t<access denied to the rest>\n");
659       return;
660     }
661
662   exca = get_conf_word(d, PCI_CB_LEGACY_MODE_BASE);
663   if (exca)
664     printf("\t16-bit legacy interface ports at %04x\n", exca);
665   show_caps(d, PCI_CB_CAPABILITY_LIST);
666 }
667
668 static void
669 show_verbose(struct device *d)
670 {
671   struct pci_dev *p = d->dev;
672   word status = get_conf_word(d, PCI_STATUS);
673   word cmd = get_conf_word(d, PCI_COMMAND);
674   word class = p->device_class;
675   byte bist = get_conf_byte(d, PCI_BIST);
676   byte htype = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
677   byte latency = get_conf_byte(d, PCI_LATENCY_TIMER);
678   byte cache_line = get_conf_byte(d, PCI_CACHE_LINE_SIZE);
679   byte max_lat, min_gnt;
680   byte int_pin = get_conf_byte(d, PCI_INTERRUPT_PIN);
681   unsigned int irq;
682
683   show_terse(d);
684
685   pci_fill_info(p, PCI_FILL_IRQ | PCI_FILL_BASES | PCI_FILL_ROM_BASE | PCI_FILL_SIZES |
686     PCI_FILL_PHYS_SLOT | PCI_FILL_LABEL | PCI_FILL_NUMA_NODE);
687   irq = p->irq;
688
689   switch (htype)
690     {
691     case PCI_HEADER_TYPE_NORMAL:
692       if (class == PCI_CLASS_BRIDGE_PCI)
693         printf("\t!!! Invalid class %04x for header type %02x\n", class, htype);
694       max_lat = get_conf_byte(d, PCI_MAX_LAT);
695       min_gnt = get_conf_byte(d, PCI_MIN_GNT);
696       break;
697     case PCI_HEADER_TYPE_BRIDGE:
698       if ((class >> 8) != PCI_BASE_CLASS_BRIDGE)
699         printf("\t!!! Invalid class %04x for header type %02x\n", class, htype);
700       min_gnt = max_lat = 0;
701       break;
702     case PCI_HEADER_TYPE_CARDBUS:
703       if ((class >> 8) != PCI_BASE_CLASS_BRIDGE)
704         printf("\t!!! Invalid class %04x for header type %02x\n", class, htype);
705       min_gnt = max_lat = 0;
706       break;
707     default:
708       printf("\t!!! Unknown header type %02x\n", htype);
709       return;
710     }
711
712   if (p->phy_slot)
713     printf("\tPhysical Slot: %s\n", p->phy_slot);
714
715   if (verbose > 1)
716     {
717       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",
718              FLAG(cmd, PCI_COMMAND_IO),
719              FLAG(cmd, PCI_COMMAND_MEMORY),
720              FLAG(cmd, PCI_COMMAND_MASTER),
721              FLAG(cmd, PCI_COMMAND_SPECIAL),
722              FLAG(cmd, PCI_COMMAND_INVALIDATE),
723              FLAG(cmd, PCI_COMMAND_VGA_PALETTE),
724              FLAG(cmd, PCI_COMMAND_PARITY),
725              FLAG(cmd, PCI_COMMAND_WAIT),
726              FLAG(cmd, PCI_COMMAND_SERR),
727              FLAG(cmd, PCI_COMMAND_FAST_BACK),
728              FLAG(cmd, PCI_COMMAND_DISABLE_INTx));
729       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",
730              FLAG(status, PCI_STATUS_CAP_LIST),
731              FLAG(status, PCI_STATUS_66MHZ),
732              FLAG(status, PCI_STATUS_UDF),
733              FLAG(status, PCI_STATUS_FAST_BACK),
734              FLAG(status, PCI_STATUS_PARITY),
735              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
736              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
737              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??",
738              FLAG(status, PCI_STATUS_SIG_TARGET_ABORT),
739              FLAG(status, PCI_STATUS_REC_TARGET_ABORT),
740              FLAG(status, PCI_STATUS_REC_MASTER_ABORT),
741              FLAG(status, PCI_STATUS_SIG_SYSTEM_ERROR),
742              FLAG(status, PCI_STATUS_DETECTED_PARITY),
743              FLAG(status, PCI_STATUS_INTx));
744       if (cmd & PCI_COMMAND_MASTER)
745         {
746           printf("\tLatency: %d", latency);
747           if (min_gnt || max_lat)
748             {
749               printf(" (");
750               if (min_gnt)
751                 printf("%dns min", min_gnt*250);
752               if (min_gnt && max_lat)
753                 printf(", ");
754               if (max_lat)
755                 printf("%dns max", max_lat*250);
756               putchar(')');
757             }
758           if (cache_line)
759             printf(", Cache Line Size: %d bytes", cache_line * 4);
760           putchar('\n');
761         }
762       if (int_pin || irq)
763         printf("\tInterrupt: pin %c routed to IRQ " PCIIRQ_FMT "\n",
764                (int_pin ? 'A' + int_pin - 1 : '?'), irq);
765       if (p->numa_node != -1)
766         printf("\tNUMA node: %d\n", p->numa_node);
767     }
768   else
769     {
770       printf("\tFlags: ");
771       if (cmd & PCI_COMMAND_MASTER)
772         printf("bus master, ");
773       if (cmd & PCI_COMMAND_VGA_PALETTE)
774         printf("VGA palette snoop, ");
775       if (cmd & PCI_COMMAND_WAIT)
776         printf("stepping, ");
777       if (cmd & PCI_COMMAND_FAST_BACK)
778         printf("fast Back2Back, ");
779       if (status & PCI_STATUS_66MHZ)
780         printf("66MHz, ");
781       if (status & PCI_STATUS_UDF)
782         printf("user-definable features, ");
783       printf("%s devsel",
784              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
785              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
786              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??");
787       if (cmd & PCI_COMMAND_MASTER)
788         printf(", latency %d", latency);
789       if (irq)
790         printf(", IRQ " PCIIRQ_FMT, irq);
791       if (p->numa_node != -1)
792         printf(", NUMA node %d", p->numa_node);
793       putchar('\n');
794     }
795
796   if (bist & PCI_BIST_CAPABLE)
797     {
798       if (bist & PCI_BIST_START)
799         printf("\tBIST is running\n");
800       else
801         printf("\tBIST result: %02x\n", bist & PCI_BIST_CODE_MASK);
802     }
803
804   switch (htype)
805     {
806     case PCI_HEADER_TYPE_NORMAL:
807       show_htype0(d);
808       break;
809     case PCI_HEADER_TYPE_BRIDGE:
810       show_htype1(d);
811       break;
812     case PCI_HEADER_TYPE_CARDBUS:
813       show_htype2(d);
814       break;
815     }
816 }
817
818 /*** Machine-readable dumps ***/
819
820 static void
821 show_hex_dump(struct device *d)
822 {
823   unsigned int i, cnt;
824
825   cnt = d->config_cached;
826   if (opt_hex >= 3 && config_fetch(d, cnt, 256-cnt))
827     {
828       cnt = 256;
829       if (opt_hex >= 4 && config_fetch(d, 256, 4096-256))
830         cnt = 4096;
831     }
832
833   for (i=0; i<cnt; i++)
834     {
835       if (! (i & 15))
836         printf("%02x:", i);
837       printf(" %02x", get_conf_byte(d, i));
838       if ((i & 15) == 15)
839         putchar('\n');
840     }
841 }
842
843 static void
844 print_shell_escaped(char *c)
845 {
846   printf(" \"");
847   while (*c)
848     {
849       if (*c == '"' || *c == '\\')
850         putchar('\\');
851       putchar(*c++);
852     }
853   putchar('"');
854 }
855
856 static void
857 show_machine(struct device *d)
858 {
859   struct pci_dev *p = d->dev;
860   int c;
861   word sv_id, sd_id;
862   char classbuf[128], vendbuf[128], devbuf[128], svbuf[128], sdbuf[128];
863
864   get_subid(d, &sv_id, &sd_id);
865
866   if (verbose)
867     {
868       pci_fill_info(p, PCI_FILL_PHYS_SLOT | PCI_FILL_NUMA_NODE);
869       printf((opt_machine >= 2) ? "Slot:\t" : "Device:\t");
870       show_slot_name(d);
871       putchar('\n');
872       printf("Class:\t%s\n",
873              pci_lookup_name(pacc, classbuf, sizeof(classbuf), PCI_LOOKUP_CLASS, p->device_class));
874       printf("Vendor:\t%s\n",
875              pci_lookup_name(pacc, vendbuf, sizeof(vendbuf), PCI_LOOKUP_VENDOR, p->vendor_id, p->device_id));
876       printf("Device:\t%s\n",
877              pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id));
878       if (sv_id && sv_id != 0xffff)
879         {
880           printf("SVendor:\t%s\n",
881                  pci_lookup_name(pacc, svbuf, sizeof(svbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR, sv_id));
882           printf("SDevice:\t%s\n",
883                  pci_lookup_name(pacc, sdbuf, sizeof(sdbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id, sv_id, sd_id));
884         }
885       if (p->phy_slot)
886         printf("PhySlot:\t%s\n", p->phy_slot);
887       if (c = get_conf_byte(d, PCI_REVISION_ID))
888         printf("Rev:\t%02x\n", c);
889       if (c = get_conf_byte(d, PCI_CLASS_PROG))
890         printf("ProgIf:\t%02x\n", c);
891       if (opt_kernel)
892         show_kernel_machine(d);
893       if (p->numa_node != -1)
894         printf("NUMANode:\t%d\n", p->numa_node);
895     }
896   else
897     {
898       show_slot_name(d);
899       print_shell_escaped(pci_lookup_name(pacc, classbuf, sizeof(classbuf), PCI_LOOKUP_CLASS, p->device_class));
900       print_shell_escaped(pci_lookup_name(pacc, vendbuf, sizeof(vendbuf), PCI_LOOKUP_VENDOR, p->vendor_id, p->device_id));
901       print_shell_escaped(pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id));
902       if (c = get_conf_byte(d, PCI_REVISION_ID))
903         printf(" -r%02x", c);
904       if (c = get_conf_byte(d, PCI_CLASS_PROG))
905         printf(" -p%02x", c);
906       if (sv_id && sv_id != 0xffff)
907         {
908           print_shell_escaped(pci_lookup_name(pacc, svbuf, sizeof(svbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR, sv_id));
909           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));
910         }
911       else
912         printf(" \"\" \"\"");
913       putchar('\n');
914     }
915 }
916
917 /*** Main show function ***/
918
919 void
920 show_device(struct device *d)
921 {
922   if (opt_machine)
923     show_machine(d);
924   else
925     {
926       if (verbose)
927         show_verbose(d);
928       else
929         show_terse(d);
930       if (opt_kernel || verbose)
931         show_kernel(d);
932     }
933   if (opt_hex)
934     show_hex_dump(d);
935   if (verbose || opt_hex)
936     putchar('\n');
937 }
938
939 static void
940 show(void)
941 {
942   struct device *d;
943
944   for (d=first_dev; d; d=d->next)
945     show_device(d);
946 }
947
948 /* Main */
949
950 int
951 main(int argc, char **argv)
952 {
953   int i;
954   char *msg;
955
956   if (argc == 2 && !strcmp(argv[1], "--version"))
957     {
958       puts("lspci version " PCIUTILS_VERSION);
959       return 0;
960     }
961
962   pacc = pci_alloc();
963   pacc->error = die;
964   pci_filter_init(pacc, &filter);
965
966   while ((i = getopt(argc, argv, options)) != -1)
967     switch (i)
968       {
969       case 'n':
970         pacc->numeric_ids++;
971         break;
972       case 'v':
973         verbose++;
974         break;
975       case 'b':
976         pacc->buscentric = 1;
977         break;
978       case 's':
979         if (msg = pci_filter_parse_slot(&filter, optarg))
980           die("-s: %s", msg);
981         break;
982       case 'd':
983         if (msg = pci_filter_parse_id(&filter, optarg))
984           die("-d: %s", msg);
985         break;
986       case 'x':
987         opt_hex++;
988         break;
989       case 't':
990         opt_tree++;
991         break;
992       case 'i':
993         pci_set_name_list_path(pacc, optarg, 0);
994         break;
995       case 'm':
996         opt_machine++;
997         break;
998       case 'p':
999         opt_pcimap = optarg;
1000         break;
1001 #ifdef PCI_OS_LINUX
1002       case 'k':
1003         opt_kernel++;
1004         break;
1005 #endif
1006       case 'M':
1007         opt_map_mode++;
1008         break;
1009       case 'D':
1010         opt_domains = 2;
1011         break;
1012 #ifdef PCI_USE_DNS
1013       case 'q':
1014         opt_query_dns++;
1015         break;
1016       case 'Q':
1017         opt_query_all = 1;
1018         break;
1019 #else
1020       case 'q':
1021       case 'Q':
1022         die("DNS queries are not available in this version");
1023 #endif
1024       default:
1025         if (parse_generic_option(i, pacc, optarg))
1026           break;
1027       bad:
1028         fprintf(stderr, help_msg, pacc->id_file_name);
1029         return 1;
1030       }
1031   if (optind < argc)
1032     goto bad;
1033
1034   if (opt_query_dns)
1035     {
1036       pacc->id_lookup_mode |= PCI_LOOKUP_NETWORK;
1037       if (opt_query_dns > 1)
1038         pacc->id_lookup_mode |= PCI_LOOKUP_REFRESH_CACHE;
1039     }
1040   if (opt_query_all)
1041     pacc->id_lookup_mode |= PCI_LOOKUP_NETWORK | PCI_LOOKUP_SKIP_LOCAL;
1042
1043   pci_init(pacc);
1044   if (opt_map_mode)
1045     map_the_bus();
1046   else
1047     {
1048       scan_devices();
1049       sort_them();
1050       if (opt_tree)
1051         show_forest();
1052       else
1053         show();
1054     }
1055   show_kernel_cleanup();
1056   pci_cleanup(pacc);
1057
1058   return (seen_errors ? 2 : 0);
1059 }