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