2 * Copyright 1996 Massachusetts Institute of Technology
4 * Permission to use, copy, modify, and distribute this software and
5 * its documentation for any purpose and without fee is hereby
6 * granted, provided that both the above copyright notice and this
7 * permission notice appear in all copies, that both the above
8 * copyright notice and this permission notice appear in all
9 * supporting documentation, and that the name of M.I.T. not be used
10 * in advertising or publicity pertaining to distribution of the
11 * software without specific, written prior permission. M.I.T. makes
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied
16 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
17 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static const char rcsid[] =
35 #include <sys/types.h>
36 #include <sys/fcntl.h>
47 #include <sys/pciio.h>
48 #include <sys/queue.h>
50 #include <dev/pci/pcireg.h>
52 #include "pathnames.h"
55 struct pci_device_info
57 TAILQ_ENTRY(pci_device_info) link;
62 struct pci_vendor_info
64 TAILQ_ENTRY(pci_vendor_info) link;
65 TAILQ_HEAD(,pci_device_info) devs;
70 TAILQ_HEAD(,pci_vendor_info) pci_vendors;
72 static struct pcisel getsel(const char *str);
73 static void list_bridge(int fd, struct pci_conf *p);
74 static void list_bars(int fd, struct pci_conf *p);
75 static void list_devs(const char *name, int verbose, int bars, int bridge,
76 int caps, int errors, int vpd);
77 static void list_verbose(struct pci_conf *p);
78 static void list_vpd(int fd, struct pci_conf *p);
79 static const char *guess_class(struct pci_conf *p);
80 static const char *guess_subclass(struct pci_conf *p);
81 static int load_vendors(void);
82 static void readit(const char *, const char *, int);
83 static void writeit(const char *, const char *, const char *, int);
84 static void chkattached(const char *);
86 static int exitstatus = 0;
91 fprintf(stderr, "%s\n%s\n%s\n%s\n",
92 "usage: pciconf -l [-BbcevV] [device]",
94 " pciconf -r [-b | -h] device addr[:addr2]",
95 " pciconf -w [-b | -h] device addr value");
100 main(int argc, char **argv)
103 int listmode, readmode, writemode, attachedmode;
104 int bars, bridge, caps, errors, verbose, vpd;
107 listmode = readmode = writemode = attachedmode = 0;
108 bars = bridge = caps = errors = verbose = vpd = byte = isshort = 0;
110 while ((c = getopt(argc, argv, "aBbcehlrwVv")) != -1) {
162 if ((listmode && optind >= argc + 1)
163 || (writemode && optind + 3 != argc)
164 || (readmode && optind + 2 != argc)
165 || (attachedmode && optind + 1 != argc))
169 list_devs(optind + 1 == argc ? argv[optind] : NULL, verbose,
170 bars, bridge, caps, errors, vpd);
171 } else if (attachedmode) {
172 chkattached(argv[optind]);
173 } else if (readmode) {
174 readit(argv[optind], argv[optind + 1],
175 byte ? 1 : isshort ? 2 : 4);
176 } else if (writemode) {
177 writeit(argv[optind], argv[optind + 1], argv[optind + 2],
178 byte ? 1 : isshort ? 2 : 4);
187 list_devs(const char *name, int verbose, int bars, int bridge, int caps,
191 struct pci_conf_io pc;
192 struct pci_conf conf[255], *p;
193 struct pci_match_conf patterns[1];
199 fd = open(_PATH_DEVPCI, (bridge || caps || errors) ? O_RDWR : O_RDONLY,
202 err(1, "%s", _PATH_DEVPCI);
204 bzero(&pc, sizeof(struct pci_conf_io));
205 pc.match_buf_len = sizeof(conf);
208 bzero(&patterns, sizeof(patterns));
209 patterns[0].pc_sel = getsel(name);
210 patterns[0].flags = PCI_GETCONF_MATCH_DOMAIN |
211 PCI_GETCONF_MATCH_BUS | PCI_GETCONF_MATCH_DEV |
212 PCI_GETCONF_MATCH_FUNC;
214 pc.pat_buf_len = sizeof(patterns);
215 pc.patterns = patterns;
219 if (ioctl(fd, PCIOCGETCONF, &pc) == -1)
220 err(1, "ioctl(PCIOCGETCONF)");
223 * 255 entries should be more than enough for most people,
224 * but if someone has more devices, and then changes things
225 * around between ioctls, we'll do the cheesy thing and
226 * just bail. The alternative would be to go back to the
227 * beginning of the list, and print things twice, which may
230 if (pc.status == PCI_GETCONF_LIST_CHANGED) {
231 warnx("PCI device list changed, please try again");
235 } else if (pc.status == PCI_GETCONF_ERROR) {
236 warnx("error returned from PCIOCGETCONF ioctl");
241 for (p = conf; p < &conf[pc.num_matches]; p++) {
242 printf("%s%d@pci%d:%d:%d:%d:\tclass=0x%06x card=0x%08x "
243 "chip=0x%08x rev=0x%02x hdr=0x%02x\n",
244 (p->pd_name && *p->pd_name) ? p->pd_name :
246 (p->pd_name && *p->pd_name) ? (int)p->pd_unit :
247 none_count++, p->pc_sel.pc_domain,
248 p->pc_sel.pc_bus, p->pc_sel.pc_dev,
249 p->pc_sel.pc_func, (p->pc_class << 16) |
250 (p->pc_subclass << 8) | p->pc_progif,
251 (p->pc_subdevice << 16) | p->pc_subvendor,
252 (p->pc_device << 16) | p->pc_vendor,
253 p->pc_revid, p->pc_hdr);
267 } while (pc.status == PCI_GETCONF_MORE_DEVS);
273 print_bus_range(int fd, struct pci_conf *p, int secreg, int subreg)
275 uint8_t secbus, subbus;
277 secbus = read_config(fd, &p->pc_sel, secreg, 1);
278 subbus = read_config(fd, &p->pc_sel, subreg, 1);
279 printf(" bus range = %u-%u\n", secbus, subbus);
283 print_window(int reg, const char *type, int range, uint64_t base,
287 printf(" window[%02x] = type %s, range %2d, addr %#jx-%#jx, %s\n",
288 reg, type, range, (uintmax_t)base, (uintmax_t)limit,
289 base < limit ? "enabled" : "disabled");
293 print_special_decode(bool isa, bool vga, bool subtractive)
297 if (isa || vga || subtractive) {
299 printf(" decode = ");
305 printf("%sVGA", comma ? ", " : "");
309 printf("%ssubtractive", comma ? ", " : "");
315 print_bridge_windows(int fd, struct pci_conf *p)
317 uint64_t base, limit;
324 * XXX: This assumes that a window with a base and limit of 0
325 * is not implemented. In theory a window might be programmed
326 * at the smallest size with a base of 0, but those do not seem
327 * common in practice.
329 val = read_config(fd, &p->pc_sel, PCIR_IOBASEL_1, 1);
330 if (val != 0 || read_config(fd, &p->pc_sel, PCIR_IOLIMITL_1, 1) != 0) {
331 if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) {
332 base = PCI_PPBIOBASE(
333 read_config(fd, &p->pc_sel, PCIR_IOBASEH_1, 2),
335 limit = PCI_PPBIOLIMIT(
336 read_config(fd, &p->pc_sel, PCIR_IOLIMITH_1, 2),
337 read_config(fd, &p->pc_sel, PCIR_IOLIMITL_1, 1));
340 base = PCI_PPBIOBASE(0, val);
341 limit = PCI_PPBIOLIMIT(0,
342 read_config(fd, &p->pc_sel, PCIR_IOLIMITL_1, 1));
345 print_window(PCIR_IOBASEL_1, "I/O Port", range, base, limit);
348 base = PCI_PPBMEMBASE(0,
349 read_config(fd, &p->pc_sel, PCIR_MEMBASE_1, 2));
350 limit = PCI_PPBMEMLIMIT(0,
351 read_config(fd, &p->pc_sel, PCIR_MEMLIMIT_1, 2));
352 print_window(PCIR_MEMBASE_1, "Memory", 32, base, limit);
354 val = read_config(fd, &p->pc_sel, PCIR_PMBASEL_1, 2);
355 if (val != 0 || read_config(fd, &p->pc_sel, PCIR_PMLIMITL_1, 2) != 0) {
356 if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) {
357 base = PCI_PPBMEMBASE(
358 read_config(fd, &p->pc_sel, PCIR_PMBASEH_1, 4),
360 limit = PCI_PPBMEMLIMIT(
361 read_config(fd, &p->pc_sel, PCIR_PMLIMITH_1, 4),
362 read_config(fd, &p->pc_sel, PCIR_PMLIMITL_1, 2));
365 base = PCI_PPBMEMBASE(0, val);
366 limit = PCI_PPBMEMLIMIT(0,
367 read_config(fd, &p->pc_sel, PCIR_PMLIMITL_1, 2));
370 print_window(PCIR_PMBASEL_1, "Prefetchable Memory", range, base,
375 * XXX: This list of bridges that are subtractive but do not set
376 * progif to indicate it is copied from pci_pci.c.
378 subtractive = p->pc_progif == PCIP_BRIDGE_PCI_SUBTRACTIVE;
379 switch (p->pc_device << 16 | p->pc_vendor) {
380 case 0xa002177d: /* Cavium ThunderX */
381 case 0x124b8086: /* Intel 82380FB Mobile */
382 case 0x060513d7: /* Toshiba ???? */
385 if (p->pc_vendor == 0x8086 && (p->pc_device & 0xff00) == 0x2400)
388 bctl = read_config(fd, &p->pc_sel, PCIR_BRIDGECTL_1, 2);
389 print_special_decode(bctl & PCIB_BCR_ISA_ENABLE,
390 bctl & PCIB_BCR_VGA_ENABLE, subtractive);
394 print_cardbus_mem_window(int fd, struct pci_conf *p, int basereg, int limitreg,
398 print_window(basereg, prefetch ? "Prefetchable Memory" : "Memory", 32,
399 PCI_CBBMEMBASE(read_config(fd, &p->pc_sel, basereg, 4)),
400 PCI_CBBMEMLIMIT(read_config(fd, &p->pc_sel, limitreg, 4)));
404 print_cardbus_io_window(int fd, struct pci_conf *p, int basereg, int limitreg)
406 uint32_t base, limit;
410 val = read_config(fd, &p->pc_sel, basereg, 2);
411 if ((val & PCIM_CBBIO_MASK) == PCIM_CBBIO_32) {
412 base = PCI_CBBIOBASE(read_config(fd, &p->pc_sel, basereg, 4));
413 limit = PCI_CBBIOBASE(read_config(fd, &p->pc_sel, limitreg, 4));
416 base = PCI_CBBIOBASE(val);
417 limit = PCI_CBBIOBASE(read_config(fd, &p->pc_sel, limitreg, 2));
420 print_window(basereg, "I/O Port", range, base, limit);
424 print_cardbus_windows(int fd, struct pci_conf *p)
428 bctl = read_config(fd, &p->pc_sel, PCIR_BRIDGECTL_2, 2);
429 print_cardbus_mem_window(fd, p, PCIR_MEMBASE0_2, PCIR_MEMLIMIT0_2,
430 bctl & CBB_BCR_PREFETCH_0_ENABLE);
431 print_cardbus_mem_window(fd, p, PCIR_MEMBASE1_2, PCIR_MEMLIMIT1_2,
432 bctl & CBB_BCR_PREFETCH_1_ENABLE);
433 print_cardbus_io_window(fd, p, PCIR_IOBASE0_2, PCIR_IOLIMIT0_2);
434 print_cardbus_io_window(fd, p, PCIR_IOBASE1_2, PCIR_IOLIMIT1_2);
435 print_special_decode(bctl & CBB_BCR_ISA_ENABLE,
436 bctl & CBB_BCR_VGA_ENABLE, false);
440 list_bridge(int fd, struct pci_conf *p)
443 switch (p->pc_hdr & PCIM_HDRTYPE) {
444 case PCIM_HDRTYPE_BRIDGE:
445 print_bus_range(fd, p, PCIR_SECBUS_1, PCIR_SUBBUS_1);
446 print_bridge_windows(fd, p);
448 case PCIM_HDRTYPE_CARDBUS:
449 print_bus_range(fd, p, PCIR_SECBUS_2, PCIR_SUBBUS_2);
450 print_cardbus_windows(fd, p);
456 list_bars(int fd, struct pci_conf *p)
458 struct pci_bar_io bar;
463 switch (p->pc_hdr & PCIM_HDRTYPE) {
464 case PCIM_HDRTYPE_NORMAL:
465 max = PCIR_MAX_BAR_0;
467 case PCIM_HDRTYPE_BRIDGE:
468 max = PCIR_MAX_BAR_1;
470 case PCIM_HDRTYPE_CARDBUS:
471 max = PCIR_MAX_BAR_2;
477 for (i = 0; i <= max; i++) {
478 bar.pbi_sel = p->pc_sel;
479 bar.pbi_reg = PCIR_BAR(i);
480 if (ioctl(fd, PCIOCGETBAR, &bar) < 0)
482 if (PCI_BAR_IO(bar.pbi_base)) {
485 base = bar.pbi_base & PCIM_BAR_IO_BASE;
487 if (bar.pbi_base & PCIM_BAR_MEM_PREFETCH)
488 type = "Prefetchable Memory";
491 switch (bar.pbi_base & PCIM_BAR_MEM_TYPE) {
492 case PCIM_BAR_MEM_32:
495 case PCIM_BAR_MEM_1MB:
498 case PCIM_BAR_MEM_64:
504 base = bar.pbi_base & ~((uint64_t)0xf);
506 printf(" bar [%02x] = type %s, range %2d, base %#jx, ",
507 PCIR_BAR(i), type, range, (uintmax_t)base);
508 printf("size %ju, %s\n", (uintmax_t)bar.pbi_length,
509 bar.pbi_enabled ? "enabled" : "disabled");
514 list_verbose(struct pci_conf *p)
516 struct pci_vendor_info *vi;
517 struct pci_device_info *di;
520 TAILQ_FOREACH(vi, &pci_vendors, link) {
521 if (vi->id == p->pc_vendor) {
522 printf(" vendor = '%s'\n", vi->desc);
529 TAILQ_FOREACH(di, &vi->devs, link) {
530 if (di->id == p->pc_device) {
531 printf(" device = '%s'\n", di->desc);
536 if ((dp = guess_class(p)) != NULL)
537 printf(" class = %s\n", dp);
538 if ((dp = guess_subclass(p)) != NULL)
539 printf(" subclass = %s\n", dp);
543 list_vpd(int fd, struct pci_conf *p)
545 struct pci_list_vpd_io list;
546 struct pci_vpd_element *vpd, *end;
548 list.plvi_sel = p->pc_sel;
550 list.plvi_data = NULL;
551 if (ioctl(fd, PCIOCLISTVPD, &list) < 0 || list.plvi_len == 0)
554 list.plvi_data = malloc(list.plvi_len);
555 if (ioctl(fd, PCIOCLISTVPD, &list) < 0) {
556 free(list.plvi_data);
560 vpd = list.plvi_data;
561 end = (struct pci_vpd_element *)((char *)vpd + list.plvi_len);
562 for (; vpd < end; vpd = PVE_NEXT(vpd)) {
563 if (vpd->pve_flags == PVE_FLAG_IDENT) {
564 printf(" VPD ident = '%.*s'\n",
565 (int)vpd->pve_datalen, vpd->pve_data);
569 /* Ignore the checksum keyword. */
570 if (!(vpd->pve_flags & PVE_FLAG_RW) &&
571 memcmp(vpd->pve_keyword, "RV", 2) == 0)
574 /* Ignore remaining read-write space. */
575 if (vpd->pve_flags & PVE_FLAG_RW &&
576 memcmp(vpd->pve_keyword, "RW", 2) == 0)
579 /* Handle extended capability keyword. */
580 if (!(vpd->pve_flags & PVE_FLAG_RW) &&
581 memcmp(vpd->pve_keyword, "CP", 2) == 0) {
582 printf(" VPD ro CP = ID %02x in map 0x%x[0x%x]\n",
583 (unsigned int)vpd->pve_data[0],
584 PCIR_BAR((unsigned int)vpd->pve_data[1]),
585 (unsigned int)vpd->pve_data[3] << 8 |
586 (unsigned int)vpd->pve_data[2]);
590 /* Remaining keywords should all have ASCII values. */
591 printf(" VPD %s %c%c = '%.*s'\n",
592 vpd->pve_flags & PVE_FLAG_RW ? "rw" : "ro",
593 vpd->pve_keyword[0], vpd->pve_keyword[1],
594 (int)vpd->pve_datalen, vpd->pve_data);
596 free(list.plvi_data);
600 * This is a direct cut-and-paste from the table in sys/dev/pci/pci.c.
607 } pci_nomatch_tab[] = {
608 {PCIC_OLD, -1, "old"},
609 {PCIC_OLD, PCIS_OLD_NONVGA, "non-VGA display device"},
610 {PCIC_OLD, PCIS_OLD_VGA, "VGA-compatible display device"},
611 {PCIC_STORAGE, -1, "mass storage"},
612 {PCIC_STORAGE, PCIS_STORAGE_SCSI, "SCSI"},
613 {PCIC_STORAGE, PCIS_STORAGE_IDE, "ATA"},
614 {PCIC_STORAGE, PCIS_STORAGE_FLOPPY, "floppy disk"},
615 {PCIC_STORAGE, PCIS_STORAGE_IPI, "IPI"},
616 {PCIC_STORAGE, PCIS_STORAGE_RAID, "RAID"},
617 {PCIC_STORAGE, PCIS_STORAGE_ATA_ADMA, "ATA (ADMA)"},
618 {PCIC_STORAGE, PCIS_STORAGE_SATA, "SATA"},
619 {PCIC_STORAGE, PCIS_STORAGE_SAS, "SAS"},
620 {PCIC_STORAGE, PCIS_STORAGE_NVM, "NVM"},
621 {PCIC_NETWORK, -1, "network"},
622 {PCIC_NETWORK, PCIS_NETWORK_ETHERNET, "ethernet"},
623 {PCIC_NETWORK, PCIS_NETWORK_TOKENRING, "token ring"},
624 {PCIC_NETWORK, PCIS_NETWORK_FDDI, "fddi"},
625 {PCIC_NETWORK, PCIS_NETWORK_ATM, "ATM"},
626 {PCIC_NETWORK, PCIS_NETWORK_ISDN, "ISDN"},
627 {PCIC_DISPLAY, -1, "display"},
628 {PCIC_DISPLAY, PCIS_DISPLAY_VGA, "VGA"},
629 {PCIC_DISPLAY, PCIS_DISPLAY_XGA, "XGA"},
630 {PCIC_DISPLAY, PCIS_DISPLAY_3D, "3D"},
631 {PCIC_MULTIMEDIA, -1, "multimedia"},
632 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_VIDEO, "video"},
633 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_AUDIO, "audio"},
634 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_TELE, "telephony"},
635 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_HDA, "HDA"},
636 {PCIC_MEMORY, -1, "memory"},
637 {PCIC_MEMORY, PCIS_MEMORY_RAM, "RAM"},
638 {PCIC_MEMORY, PCIS_MEMORY_FLASH, "flash"},
639 {PCIC_BRIDGE, -1, "bridge"},
640 {PCIC_BRIDGE, PCIS_BRIDGE_HOST, "HOST-PCI"},
641 {PCIC_BRIDGE, PCIS_BRIDGE_ISA, "PCI-ISA"},
642 {PCIC_BRIDGE, PCIS_BRIDGE_EISA, "PCI-EISA"},
643 {PCIC_BRIDGE, PCIS_BRIDGE_MCA, "PCI-MCA"},
644 {PCIC_BRIDGE, PCIS_BRIDGE_PCI, "PCI-PCI"},
645 {PCIC_BRIDGE, PCIS_BRIDGE_PCMCIA, "PCI-PCMCIA"},
646 {PCIC_BRIDGE, PCIS_BRIDGE_NUBUS, "PCI-NuBus"},
647 {PCIC_BRIDGE, PCIS_BRIDGE_CARDBUS, "PCI-CardBus"},
648 {PCIC_BRIDGE, PCIS_BRIDGE_RACEWAY, "PCI-RACEway"},
649 {PCIC_SIMPLECOMM, -1, "simple comms"},
650 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_UART, "UART"}, /* could detect 16550 */
651 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_PAR, "parallel port"},
652 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_MULSER, "multiport serial"},
653 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_MODEM, "generic modem"},
654 {PCIC_BASEPERIPH, -1, "base peripheral"},
655 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_PIC, "interrupt controller"},
656 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_DMA, "DMA controller"},
657 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_TIMER, "timer"},
658 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_RTC, "realtime clock"},
659 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_PCIHOT, "PCI hot-plug controller"},
660 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_SDHC, "SD host controller"},
661 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_IOMMU, "IOMMU"},
662 {PCIC_INPUTDEV, -1, "input device"},
663 {PCIC_INPUTDEV, PCIS_INPUTDEV_KEYBOARD, "keyboard"},
664 {PCIC_INPUTDEV, PCIS_INPUTDEV_DIGITIZER,"digitizer"},
665 {PCIC_INPUTDEV, PCIS_INPUTDEV_MOUSE, "mouse"},
666 {PCIC_INPUTDEV, PCIS_INPUTDEV_SCANNER, "scanner"},
667 {PCIC_INPUTDEV, PCIS_INPUTDEV_GAMEPORT, "gameport"},
668 {PCIC_DOCKING, -1, "docking station"},
669 {PCIC_PROCESSOR, -1, "processor"},
670 {PCIC_SERIALBUS, -1, "serial bus"},
671 {PCIC_SERIALBUS, PCIS_SERIALBUS_FW, "FireWire"},
672 {PCIC_SERIALBUS, PCIS_SERIALBUS_ACCESS, "AccessBus"},
673 {PCIC_SERIALBUS, PCIS_SERIALBUS_SSA, "SSA"},
674 {PCIC_SERIALBUS, PCIS_SERIALBUS_USB, "USB"},
675 {PCIC_SERIALBUS, PCIS_SERIALBUS_FC, "Fibre Channel"},
676 {PCIC_SERIALBUS, PCIS_SERIALBUS_SMBUS, "SMBus"},
677 {PCIC_WIRELESS, -1, "wireless controller"},
678 {PCIC_WIRELESS, PCIS_WIRELESS_IRDA, "iRDA"},
679 {PCIC_WIRELESS, PCIS_WIRELESS_IR, "IR"},
680 {PCIC_WIRELESS, PCIS_WIRELESS_RF, "RF"},
681 {PCIC_INTELLIIO, -1, "intelligent I/O controller"},
682 {PCIC_INTELLIIO, PCIS_INTELLIIO_I2O, "I2O"},
683 {PCIC_SATCOM, -1, "satellite communication"},
684 {PCIC_SATCOM, PCIS_SATCOM_TV, "sat TV"},
685 {PCIC_SATCOM, PCIS_SATCOM_AUDIO, "sat audio"},
686 {PCIC_SATCOM, PCIS_SATCOM_VOICE, "sat voice"},
687 {PCIC_SATCOM, PCIS_SATCOM_DATA, "sat data"},
688 {PCIC_CRYPTO, -1, "encrypt/decrypt"},
689 {PCIC_CRYPTO, PCIS_CRYPTO_NETCOMP, "network/computer crypto"},
690 {PCIC_CRYPTO, PCIS_CRYPTO_NETCOMP, "entertainment crypto"},
691 {PCIC_DASP, -1, "dasp"},
692 {PCIC_DASP, PCIS_DASP_DPIO, "DPIO module"},
693 {PCIC_DASP, PCIS_DASP_PERFCNTRS, "performance counters"},
694 {PCIC_DASP, PCIS_DASP_COMM_SYNC, "communication synchronizer"},
695 {PCIC_DASP, PCIS_DASP_MGMT_CARD, "signal processing management"},
700 guess_class(struct pci_conf *p)
704 for (i = 0; pci_nomatch_tab[i].desc != NULL; i++) {
705 if (pci_nomatch_tab[i].class == p->pc_class)
706 return(pci_nomatch_tab[i].desc);
712 guess_subclass(struct pci_conf *p)
716 for (i = 0; pci_nomatch_tab[i].desc != NULL; i++) {
717 if ((pci_nomatch_tab[i].class == p->pc_class) &&
718 (pci_nomatch_tab[i].subclass == p->pc_subclass))
719 return(pci_nomatch_tab[i].desc);
729 struct pci_vendor_info *cv;
730 struct pci_device_info *cd;
731 char buf[1024], str[1024];
736 * Locate the database and initialise.
738 TAILQ_INIT(&pci_vendors);
739 if ((dbf = getenv("PCICONF_VENDOR_DATABASE")) == NULL)
741 if ((db = fopen(dbf, "r")) == NULL) {
743 if ((db = fopen(dbf, "r")) == NULL)
751 * Scan input lines from the database
754 if (fgets(buf, sizeof(buf), db) == NULL)
757 if ((ch = strchr(buf, '#')) != NULL)
759 ch = strchr(buf, '\0') - 1;
760 while (ch > buf && isspace(*ch))
765 /* Can't handle subvendor / subdevice entries yet */
766 if (buf[0] == '\t' && buf[1] == '\t')
769 /* Check for vendor entry */
770 if (buf[0] != '\t' && sscanf(buf, "%04x %[^\n]", &id, str) == 2) {
771 if ((id == 0) || (strlen(str) < 1))
773 if ((cv = malloc(sizeof(struct pci_vendor_info))) == NULL) {
774 warn("allocating vendor entry");
778 if ((cv->desc = strdup(str)) == NULL) {
780 warn("allocating vendor description");
785 TAILQ_INIT(&cv->devs);
786 TAILQ_INSERT_TAIL(&pci_vendors, cv, link);
790 /* Check for device entry */
791 if (buf[0] == '\t' && sscanf(buf + 1, "%04x %[^\n]", &id, str) == 2) {
792 if ((id == 0) || (strlen(str) < 1))
795 warnx("device entry with no vendor!");
798 if ((cd = malloc(sizeof(struct pci_device_info))) == NULL) {
799 warn("allocating device entry");
803 if ((cd->desc = strdup(str)) == NULL) {
805 warn("allocating device description");
810 TAILQ_INSERT_TAIL(&cv->devs, cd, link);
814 /* It's a comment or junk, ignore it */
824 read_config(int fd, struct pcisel *sel, long reg, int width)
832 if (ioctl(fd, PCIOCREAD, &pi) < 0)
833 err(1, "ioctl(PCIOCREAD)");
839 getdevice(const char *name)
841 struct pci_conf_io pc;
842 struct pci_conf conf[1];
843 struct pci_match_conf patterns[1];
847 fd = open(_PATH_DEVPCI, O_RDONLY, 0);
849 err(1, "%s", _PATH_DEVPCI);
851 bzero(&pc, sizeof(struct pci_conf_io));
852 pc.match_buf_len = sizeof(conf);
855 bzero(&patterns, sizeof(patterns));
858 * The pattern structure requires the unit to be split out from
859 * the driver name. Walk backwards from the end of the name to
860 * find the start of the unit.
863 errx(1, "Empty device name");
864 cp = strchr(name, '\0');
865 assert(cp != NULL && cp != name);
867 while (cp != name && isdigit(cp[-1]))
869 if (cp == name || !isdigit(*cp))
870 errx(1, "Invalid device name");
871 if ((size_t)(cp - name) + 1 > sizeof(patterns[0].pd_name))
872 errx(1, "Device name is too long");
873 memcpy(patterns[0].pd_name, name, cp - name);
874 patterns[0].pd_unit = strtol(cp, &cp, 10);
876 patterns[0].flags = PCI_GETCONF_MATCH_NAME | PCI_GETCONF_MATCH_UNIT;
878 pc.pat_buf_len = sizeof(patterns);
879 pc.patterns = patterns;
881 if (ioctl(fd, PCIOCGETCONF, &pc) == -1)
882 err(1, "ioctl(PCIOCGETCONF)");
883 if (pc.status != PCI_GETCONF_LAST_DEVICE &&
884 pc.status != PCI_GETCONF_MORE_DEVS)
885 errx(1, "error returned from PCIOCGETCONF ioctl");
887 if (pc.num_matches == 0)
888 errx(1, "Device not found");
889 return (conf[0].pc_sel);
893 parsesel(const char *str)
895 char *ep = strchr(str, '@');
898 unsigned long selarr[4];
908 if (strncmp(ep, "pci", 3) == 0) {
912 selarr[i++] = strtoul(ep, &ep, 10);
913 } while ((*ep == ':' || *ep == '.') && *++ep != '\0' && i < 4);
916 sel.pc_func = selarr[--i];
919 sel.pc_dev = selarr[--i];
920 sel.pc_bus = selarr[--i];
922 sel.pc_domain = selarr[--i];
926 if (*ep != '\x0' || ep == epbase)
927 errx(1, "cannot parse selector %s", str);
932 getsel(const char *str)
936 * No device names contain colons and selectors always contain
937 * at least one colon.
939 if (strchr(str, ':') == NULL)
940 return (getdevice(str));
942 return (parsesel(str));
946 readone(int fd, struct pcisel *sel, long reg, int width)
949 printf("%0*x", width*2, read_config(fd, sel, reg, width));
953 readit(const char *name, const char *reg, int width)
963 fd = open(_PATH_DEVPCI, O_RDWR, 0);
965 err(1, "%s", _PATH_DEVPCI);
967 rend = rstart = strtol(reg, &end, 0);
968 if (end && *end == ':') {
970 rend = strtol(end, (char **) 0, 0);
973 for (i = 1, r = rstart; r <= rend; i++, r += width) {
974 readone(fd, &sel, r, width);
977 putchar(i % (16/width) ? ' ' : '\n');
979 if (i % (16/width) != 1)
985 writeit(const char *name, const char *reg, const char *data, int width)
990 pi.pi_sel = getsel(name);
991 pi.pi_reg = strtoul(reg, (char **)0, 0); /* XXX error check */
993 pi.pi_data = strtoul(data, (char **)0, 0); /* XXX error check */
995 fd = open(_PATH_DEVPCI, O_RDWR, 0);
997 err(1, "%s", _PATH_DEVPCI);
999 if (ioctl(fd, PCIOCWRITE, &pi) < 0)
1000 err(1, "ioctl(PCIOCWRITE)");
1004 chkattached(const char *name)
1009 pi.pi_sel = getsel(name);
1011 fd = open(_PATH_DEVPCI, O_RDWR, 0);
1013 err(1, "%s", _PATH_DEVPCI);
1015 if (ioctl(fd, PCIOCATTACHED, &pi) < 0)
1016 err(1, "ioctl(PCIOCATTACHED)");
1018 exitstatus = pi.pi_data ? 0 : 2; /* exit(2), if NOT attached */
1019 printf("%s: %s%s\n", name, pi.pi_data == 0 ? "not " : "", "attached");