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>
46 #include <sys/pciio.h>
47 #include <sys/queue.h>
49 #include <dev/pci/pcireg.h>
51 #include "pathnames.h"
54 struct pci_device_info
56 TAILQ_ENTRY(pci_device_info) link;
61 struct pci_vendor_info
63 TAILQ_ENTRY(pci_vendor_info) link;
64 TAILQ_HEAD(,pci_device_info) devs;
69 TAILQ_HEAD(,pci_vendor_info) pci_vendors;
71 static struct pcisel getsel(const char *str);
72 static void list_bars(int fd, struct pci_conf *p);
73 static void list_devs(const char *name, int verbose, int bars, int caps,
75 static void list_verbose(struct pci_conf *p);
76 static const char *guess_class(struct pci_conf *p);
77 static const char *guess_subclass(struct pci_conf *p);
78 static int load_vendors(void);
79 static void readit(const char *, const char *, int);
80 static void writeit(const char *, const char *, const char *, int);
81 static void chkattached(const char *);
83 static int exitstatus = 0;
88 fprintf(stderr, "%s\n%s\n%s\n%s\n",
89 "usage: pciconf -l [-bcev] [device]",
91 " pciconf -r [-b | -h] device addr[:addr2]",
92 " pciconf -w [-b | -h] device addr value");
97 main(int argc, char **argv)
100 int listmode, readmode, writemode, attachedmode;
101 int bars, caps, errors, verbose;
104 listmode = readmode = writemode = attachedmode = 0;
105 bars = caps = errors = verbose = byte = isshort = 0;
107 while ((c = getopt(argc, argv, "abcehlrwv")) != -1) {
151 if ((listmode && optind >= argc + 1)
152 || (writemode && optind + 3 != argc)
153 || (readmode && optind + 2 != argc)
154 || (attachedmode && optind + 1 != argc))
158 list_devs(optind + 1 == argc ? argv[optind] : NULL, verbose,
160 } else if (attachedmode) {
161 chkattached(argv[optind]);
162 } else if (readmode) {
163 readit(argv[optind], argv[optind + 1],
164 byte ? 1 : isshort ? 2 : 4);
165 } else if (writemode) {
166 writeit(argv[optind], argv[optind + 1], argv[optind + 2],
167 byte ? 1 : isshort ? 2 : 4);
176 list_devs(const char *name, int verbose, int bars, int caps, int errors)
179 struct pci_conf_io pc;
180 struct pci_conf conf[255], *p;
181 struct pci_match_conf patterns[1];
187 fd = open(_PATH_DEVPCI, (caps || errors) ? O_RDWR : O_RDONLY, 0);
189 err(1, "%s", _PATH_DEVPCI);
191 bzero(&pc, sizeof(struct pci_conf_io));
192 pc.match_buf_len = sizeof(conf);
195 bzero(&patterns, sizeof(patterns));
196 patterns[0].pc_sel = getsel(name);
197 patterns[0].flags = PCI_GETCONF_MATCH_DOMAIN |
198 PCI_GETCONF_MATCH_BUS | PCI_GETCONF_MATCH_DEV |
199 PCI_GETCONF_MATCH_FUNC;
201 pc.pat_buf_len = sizeof(patterns);
202 pc.patterns = patterns;
206 if (ioctl(fd, PCIOCGETCONF, &pc) == -1)
207 err(1, "ioctl(PCIOCGETCONF)");
210 * 255 entries should be more than enough for most people,
211 * but if someone has more devices, and then changes things
212 * around between ioctls, we'll do the cheezy thing and
213 * just bail. The alternative would be to go back to the
214 * beginning of the list, and print things twice, which may
217 if (pc.status == PCI_GETCONF_LIST_CHANGED) {
218 warnx("PCI device list changed, please try again");
222 } else if (pc.status == PCI_GETCONF_ERROR) {
223 warnx("error returned from PCIOCGETCONF ioctl");
228 for (p = conf; p < &conf[pc.num_matches]; p++) {
229 printf("%s%d@pci%d:%d:%d:%d:\tclass=0x%06x card=0x%08x "
230 "chip=0x%08x rev=0x%02x hdr=0x%02x\n",
231 (p->pd_name && *p->pd_name) ? p->pd_name :
233 (p->pd_name && *p->pd_name) ? (int)p->pd_unit :
234 none_count++, p->pc_sel.pc_domain,
235 p->pc_sel.pc_bus, p->pc_sel.pc_dev,
236 p->pc_sel.pc_func, (p->pc_class << 16) |
237 (p->pc_subclass << 8) | p->pc_progif,
238 (p->pc_subdevice << 16) | p->pc_subvendor,
239 (p->pc_device << 16) | p->pc_vendor,
240 p->pc_revid, p->pc_hdr);
250 } while (pc.status == PCI_GETCONF_MORE_DEVS);
256 list_bars(int fd, struct pci_conf *p)
258 struct pci_bar_io bar;
263 switch (p->pc_hdr & PCIM_HDRTYPE) {
264 case PCIM_HDRTYPE_NORMAL:
265 max = PCIR_MAX_BAR_0;
267 case PCIM_HDRTYPE_BRIDGE:
268 max = PCIR_MAX_BAR_1;
270 case PCIM_HDRTYPE_CARDBUS:
271 max = PCIR_MAX_BAR_2;
277 for (i = 0; i <= max; i++) {
278 bar.pbi_sel = p->pc_sel;
279 bar.pbi_reg = PCIR_BAR(i);
280 if (ioctl(fd, PCIOCGETBAR, &bar) < 0)
282 if (PCI_BAR_IO(bar.pbi_base)) {
285 base = bar.pbi_base & PCIM_BAR_IO_BASE;
287 if (bar.pbi_base & PCIM_BAR_MEM_PREFETCH)
288 type = "Prefetchable Memory";
291 switch (bar.pbi_base & PCIM_BAR_MEM_TYPE) {
292 case PCIM_BAR_MEM_32:
295 case PCIM_BAR_MEM_1MB:
298 case PCIM_BAR_MEM_64:
304 base = bar.pbi_base & ~((uint64_t)0xf);
306 printf(" bar [%02x] = type %s, range %2d, base %#jx, ",
307 PCIR_BAR(i), type, range, (uintmax_t)base);
308 printf("size %2d, %s\n", (int)bar.pbi_length,
309 bar.pbi_enabled ? "enabled" : "disabled");
314 list_verbose(struct pci_conf *p)
316 struct pci_vendor_info *vi;
317 struct pci_device_info *di;
320 TAILQ_FOREACH(vi, &pci_vendors, link) {
321 if (vi->id == p->pc_vendor) {
322 printf(" vendor = '%s'\n", vi->desc);
329 TAILQ_FOREACH(di, &vi->devs, link) {
330 if (di->id == p->pc_device) {
331 printf(" device = '%s'\n", di->desc);
336 if ((dp = guess_class(p)) != NULL)
337 printf(" class = %s\n", dp);
338 if ((dp = guess_subclass(p)) != NULL)
339 printf(" subclass = %s\n", dp);
343 * This is a direct cut-and-paste from the table in sys/dev/pci/pci.c.
350 } pci_nomatch_tab[] = {
351 {PCIC_OLD, -1, "old"},
352 {PCIC_OLD, PCIS_OLD_NONVGA, "non-VGA display device"},
353 {PCIC_OLD, PCIS_OLD_VGA, "VGA-compatible display device"},
354 {PCIC_STORAGE, -1, "mass storage"},
355 {PCIC_STORAGE, PCIS_STORAGE_SCSI, "SCSI"},
356 {PCIC_STORAGE, PCIS_STORAGE_IDE, "ATA"},
357 {PCIC_STORAGE, PCIS_STORAGE_FLOPPY, "floppy disk"},
358 {PCIC_STORAGE, PCIS_STORAGE_IPI, "IPI"},
359 {PCIC_STORAGE, PCIS_STORAGE_RAID, "RAID"},
360 {PCIC_STORAGE, PCIS_STORAGE_ATA_ADMA, "ATA (ADMA)"},
361 {PCIC_STORAGE, PCIS_STORAGE_SATA, "SATA"},
362 {PCIC_STORAGE, PCIS_STORAGE_SAS, "SAS"},
363 {PCIC_STORAGE, PCIS_STORAGE_NVM, "NVM"},
364 {PCIC_NETWORK, -1, "network"},
365 {PCIC_NETWORK, PCIS_NETWORK_ETHERNET, "ethernet"},
366 {PCIC_NETWORK, PCIS_NETWORK_TOKENRING, "token ring"},
367 {PCIC_NETWORK, PCIS_NETWORK_FDDI, "fddi"},
368 {PCIC_NETWORK, PCIS_NETWORK_ATM, "ATM"},
369 {PCIC_NETWORK, PCIS_NETWORK_ISDN, "ISDN"},
370 {PCIC_DISPLAY, -1, "display"},
371 {PCIC_DISPLAY, PCIS_DISPLAY_VGA, "VGA"},
372 {PCIC_DISPLAY, PCIS_DISPLAY_XGA, "XGA"},
373 {PCIC_DISPLAY, PCIS_DISPLAY_3D, "3D"},
374 {PCIC_MULTIMEDIA, -1, "multimedia"},
375 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_VIDEO, "video"},
376 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_AUDIO, "audio"},
377 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_TELE, "telephony"},
378 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_HDA, "HDA"},
379 {PCIC_MEMORY, -1, "memory"},
380 {PCIC_MEMORY, PCIS_MEMORY_RAM, "RAM"},
381 {PCIC_MEMORY, PCIS_MEMORY_FLASH, "flash"},
382 {PCIC_BRIDGE, -1, "bridge"},
383 {PCIC_BRIDGE, PCIS_BRIDGE_HOST, "HOST-PCI"},
384 {PCIC_BRIDGE, PCIS_BRIDGE_ISA, "PCI-ISA"},
385 {PCIC_BRIDGE, PCIS_BRIDGE_EISA, "PCI-EISA"},
386 {PCIC_BRIDGE, PCIS_BRIDGE_MCA, "PCI-MCA"},
387 {PCIC_BRIDGE, PCIS_BRIDGE_PCI, "PCI-PCI"},
388 {PCIC_BRIDGE, PCIS_BRIDGE_PCMCIA, "PCI-PCMCIA"},
389 {PCIC_BRIDGE, PCIS_BRIDGE_NUBUS, "PCI-NuBus"},
390 {PCIC_BRIDGE, PCIS_BRIDGE_CARDBUS, "PCI-CardBus"},
391 {PCIC_BRIDGE, PCIS_BRIDGE_RACEWAY, "PCI-RACEway"},
392 {PCIC_SIMPLECOMM, -1, "simple comms"},
393 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_UART, "UART"}, /* could detect 16550 */
394 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_PAR, "parallel port"},
395 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_MULSER, "multiport serial"},
396 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_MODEM, "generic modem"},
397 {PCIC_BASEPERIPH, -1, "base peripheral"},
398 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_PIC, "interrupt controller"},
399 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_DMA, "DMA controller"},
400 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_TIMER, "timer"},
401 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_RTC, "realtime clock"},
402 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_PCIHOT, "PCI hot-plug controller"},
403 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_SDHC, "SD host controller"},
404 {PCIC_INPUTDEV, -1, "input device"},
405 {PCIC_INPUTDEV, PCIS_INPUTDEV_KEYBOARD, "keyboard"},
406 {PCIC_INPUTDEV, PCIS_INPUTDEV_DIGITIZER,"digitizer"},
407 {PCIC_INPUTDEV, PCIS_INPUTDEV_MOUSE, "mouse"},
408 {PCIC_INPUTDEV, PCIS_INPUTDEV_SCANNER, "scanner"},
409 {PCIC_INPUTDEV, PCIS_INPUTDEV_GAMEPORT, "gameport"},
410 {PCIC_DOCKING, -1, "docking station"},
411 {PCIC_PROCESSOR, -1, "processor"},
412 {PCIC_SERIALBUS, -1, "serial bus"},
413 {PCIC_SERIALBUS, PCIS_SERIALBUS_FW, "FireWire"},
414 {PCIC_SERIALBUS, PCIS_SERIALBUS_ACCESS, "AccessBus"},
415 {PCIC_SERIALBUS, PCIS_SERIALBUS_SSA, "SSA"},
416 {PCIC_SERIALBUS, PCIS_SERIALBUS_USB, "USB"},
417 {PCIC_SERIALBUS, PCIS_SERIALBUS_FC, "Fibre Channel"},
418 {PCIC_SERIALBUS, PCIS_SERIALBUS_SMBUS, "SMBus"},
419 {PCIC_WIRELESS, -1, "wireless controller"},
420 {PCIC_WIRELESS, PCIS_WIRELESS_IRDA, "iRDA"},
421 {PCIC_WIRELESS, PCIS_WIRELESS_IR, "IR"},
422 {PCIC_WIRELESS, PCIS_WIRELESS_RF, "RF"},
423 {PCIC_INTELLIIO, -1, "intelligent I/O controller"},
424 {PCIC_INTELLIIO, PCIS_INTELLIIO_I2O, "I2O"},
425 {PCIC_SATCOM, -1, "satellite communication"},
426 {PCIC_SATCOM, PCIS_SATCOM_TV, "sat TV"},
427 {PCIC_SATCOM, PCIS_SATCOM_AUDIO, "sat audio"},
428 {PCIC_SATCOM, PCIS_SATCOM_VOICE, "sat voice"},
429 {PCIC_SATCOM, PCIS_SATCOM_DATA, "sat data"},
430 {PCIC_CRYPTO, -1, "encrypt/decrypt"},
431 {PCIC_CRYPTO, PCIS_CRYPTO_NETCOMP, "network/computer crypto"},
432 {PCIC_CRYPTO, PCIS_CRYPTO_NETCOMP, "entertainment crypto"},
433 {PCIC_DASP, -1, "dasp"},
434 {PCIC_DASP, PCIS_DASP_DPIO, "DPIO module"},
439 guess_class(struct pci_conf *p)
443 for (i = 0; pci_nomatch_tab[i].desc != NULL; i++) {
444 if (pci_nomatch_tab[i].class == p->pc_class)
445 return(pci_nomatch_tab[i].desc);
451 guess_subclass(struct pci_conf *p)
455 for (i = 0; pci_nomatch_tab[i].desc != NULL; i++) {
456 if ((pci_nomatch_tab[i].class == p->pc_class) &&
457 (pci_nomatch_tab[i].subclass == p->pc_subclass))
458 return(pci_nomatch_tab[i].desc);
468 struct pci_vendor_info *cv;
469 struct pci_device_info *cd;
470 char buf[1024], str[1024];
475 * Locate the database and initialise.
477 TAILQ_INIT(&pci_vendors);
478 if ((dbf = getenv("PCICONF_VENDOR_DATABASE")) == NULL)
480 if ((db = fopen(dbf, "r")) == NULL)
487 * Scan input lines from the database
490 if (fgets(buf, sizeof(buf), db) == NULL)
493 if ((ch = strchr(buf, '#')) != NULL)
495 ch = strchr(buf, '\0') - 1;
496 while (ch > buf && isspace(*ch))
501 /* Can't handle subvendor / subdevice entries yet */
502 if (buf[0] == '\t' && buf[1] == '\t')
505 /* Check for vendor entry */
506 if (buf[0] != '\t' && sscanf(buf, "%04x %[^\n]", &id, str) == 2) {
507 if ((id == 0) || (strlen(str) < 1))
509 if ((cv = malloc(sizeof(struct pci_vendor_info))) == NULL) {
510 warn("allocating vendor entry");
514 if ((cv->desc = strdup(str)) == NULL) {
516 warn("allocating vendor description");
521 TAILQ_INIT(&cv->devs);
522 TAILQ_INSERT_TAIL(&pci_vendors, cv, link);
526 /* Check for device entry */
527 if (buf[0] == '\t' && sscanf(buf + 1, "%04x %[^\n]", &id, str) == 2) {
528 if ((id == 0) || (strlen(str) < 1))
531 warnx("device entry with no vendor!");
534 if ((cd = malloc(sizeof(struct pci_device_info))) == NULL) {
535 warn("allocating device entry");
539 if ((cd->desc = strdup(str)) == NULL) {
541 warn("allocating device description");
546 TAILQ_INSERT_TAIL(&cv->devs, cd, link);
550 /* It's a comment or junk, ignore it */
560 read_config(int fd, struct pcisel *sel, long reg, int width)
568 if (ioctl(fd, PCIOCREAD, &pi) < 0)
569 err(1, "ioctl(PCIOCREAD)");
575 getdevice(const char *name)
577 struct pci_conf_io pc;
578 struct pci_conf conf[1];
579 struct pci_match_conf patterns[1];
583 fd = open(_PATH_DEVPCI, O_RDONLY, 0);
585 err(1, "%s", _PATH_DEVPCI);
587 bzero(&pc, sizeof(struct pci_conf_io));
588 pc.match_buf_len = sizeof(conf);
591 bzero(&patterns, sizeof(patterns));
594 * The pattern structure requires the unit to be split out from
595 * the driver name. Walk backwards from the end of the name to
596 * find the start of the unit.
599 err(1, "Empty device name");
600 cp = strchr(name, '\0');
601 assert(cp != NULL && cp != name);
603 while (cp != name && isdigit(cp[-1]))
606 errx(1, "Invalid device name");
607 if ((size_t)(cp - name) + 1 > sizeof(patterns[0].pd_name))
608 errx(1, "Device name i2s too long");
609 memcpy(patterns[0].pd_name, name, cp - name);
610 patterns[0].pd_unit = strtol(cp, &cp, 10);
612 patterns[0].flags = PCI_GETCONF_MATCH_NAME | PCI_GETCONF_MATCH_UNIT;
614 pc.pat_buf_len = sizeof(patterns);
615 pc.patterns = patterns;
617 if (ioctl(fd, PCIOCGETCONF, &pc) == -1)
618 err(1, "ioctl(PCIOCGETCONF)");
619 if (pc.status != PCI_GETCONF_LAST_DEVICE &&
620 pc.status != PCI_GETCONF_MORE_DEVS)
621 errx(1, "error returned from PCIOCGETCONF ioctl");
623 if (pc.num_matches == 0)
624 errx(1, "Device not found");
625 return (conf[0].pc_sel);
629 parsesel(const char *str)
631 char *ep = strchr(str, '@');
634 unsigned long selarr[4];
644 if (strncmp(ep, "pci", 3) == 0) {
648 selarr[i++] = strtoul(ep, &ep, 10);
649 } while ((*ep == ':' || *ep == '.') && *++ep != '\0' && i < 4);
652 sel.pc_func = selarr[--i];
655 sel.pc_dev = selarr[--i];
656 sel.pc_bus = selarr[--i];
658 sel.pc_domain = selarr[--i];
662 if (*ep != '\x0' || ep == epbase)
663 errx(1, "cannot parse selector %s", str);
668 getsel(const char *str)
672 * No device names contain colons and selectors always contain
673 * at least one colon.
675 if (strchr(str, ':') == NULL)
676 return (getdevice(str));
678 return (parsesel(str));
682 readone(int fd, struct pcisel *sel, long reg, int width)
685 printf("%0*x", width*2, read_config(fd, sel, reg, width));
689 readit(const char *name, const char *reg, int width)
699 fd = open(_PATH_DEVPCI, O_RDWR, 0);
701 err(1, "%s", _PATH_DEVPCI);
703 rend = rstart = strtol(reg, &end, 0);
704 if (end && *end == ':') {
706 rend = strtol(end, (char **) 0, 0);
709 for (i = 1, r = rstart; r <= rend; i++, r += width) {
710 readone(fd, &sel, r, width);
713 putchar(i % (16/width) ? ' ' : '\n');
715 if (i % (16/width) != 1)
721 writeit(const char *name, const char *reg, const char *data, int width)
726 pi.pi_sel = getsel(name);
727 pi.pi_reg = strtoul(reg, (char **)0, 0); /* XXX error check */
729 pi.pi_data = strtoul(data, (char **)0, 0); /* XXX error check */
731 fd = open(_PATH_DEVPCI, O_RDWR, 0);
733 err(1, "%s", _PATH_DEVPCI);
735 if (ioctl(fd, PCIOCWRITE, &pi) < 0)
736 err(1, "ioctl(PCIOCWRITE)");
740 chkattached(const char *name)
745 pi.pi_sel = getsel(name);
747 fd = open(_PATH_DEVPCI, O_RDWR, 0);
749 err(1, "%s", _PATH_DEVPCI);
751 if (ioctl(fd, PCIOCATTACHED, &pi) < 0)
752 err(1, "ioctl(PCIOCATTACHED)");
754 exitstatus = pi.pi_data ? 0 : 2; /* exit(2), if NOT attached */
755 printf("%s: %s%s\n", name, pi.pi_data == 0 ? "not " : "", "attached");