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>
45 #include <sys/pciio.h>
46 #include <sys/queue.h>
48 #include <dev/pci/pcireg.h>
50 #include "pathnames.h"
53 struct pci_device_info
55 TAILQ_ENTRY(pci_device_info) link;
60 struct pci_vendor_info
62 TAILQ_ENTRY(pci_vendor_info) link;
63 TAILQ_HEAD(,pci_device_info) devs;
68 TAILQ_HEAD(,pci_vendor_info) pci_vendors;
70 static void list_bars(int fd, struct pci_conf *p);
71 static void list_devs(int verbose, int bars, int caps, int errors);
72 static void list_verbose(struct pci_conf *p);
73 static const char *guess_class(struct pci_conf *p);
74 static const char *guess_subclass(struct pci_conf *p);
75 static int load_vendors(void);
76 static void readit(const char *, const char *, int);
77 static void writeit(const char *, const char *, const char *, int);
78 static void chkattached(const char *);
80 static int exitstatus = 0;
85 fprintf(stderr, "%s\n%s\n%s\n%s\n",
86 "usage: pciconf -l [-bcev]",
87 " pciconf -a selector",
88 " pciconf -r [-b | -h] selector addr[:addr2]",
89 " pciconf -w [-b | -h] selector addr value");
94 main(int argc, char **argv)
97 int listmode, readmode, writemode, attachedmode;
98 int bars, caps, errors, verbose;
101 listmode = readmode = writemode = attachedmode = 0;
102 bars = caps = errors = verbose = byte = isshort = 0;
104 while ((c = getopt(argc, argv, "abcehlrwv")) != -1) {
148 if ((listmode && optind != argc)
149 || (writemode && optind + 3 != argc)
150 || (readmode && optind + 2 != argc)
151 || (attachedmode && optind + 1 != argc))
155 list_devs(verbose, bars, caps, errors);
156 } else if (attachedmode) {
157 chkattached(argv[optind]);
158 } else if (readmode) {
159 readit(argv[optind], argv[optind + 1],
160 byte ? 1 : isshort ? 2 : 4);
161 } else if (writemode) {
162 writeit(argv[optind], argv[optind + 1], argv[optind + 2],
163 byte ? 1 : isshort ? 2 : 4);
172 list_devs(int verbose, int bars, int caps, int errors)
175 struct pci_conf_io pc;
176 struct pci_conf conf[255], *p;
182 fd = open(_PATH_DEVPCI, (caps || errors) ? O_RDWR : O_RDONLY, 0);
184 err(1, "%s", _PATH_DEVPCI);
186 bzero(&pc, sizeof(struct pci_conf_io));
187 pc.match_buf_len = sizeof(conf);
191 if (ioctl(fd, PCIOCGETCONF, &pc) == -1)
192 err(1, "ioctl(PCIOCGETCONF)");
195 * 255 entries should be more than enough for most people,
196 * but if someone has more devices, and then changes things
197 * around between ioctls, we'll do the cheesy thing and
198 * just bail. The alternative would be to go back to the
199 * beginning of the list, and print things twice, which may
202 if (pc.status == PCI_GETCONF_LIST_CHANGED) {
203 warnx("PCI device list changed, please try again");
207 } else if (pc.status == PCI_GETCONF_ERROR) {
208 warnx("error returned from PCIOCGETCONF ioctl");
213 for (p = conf; p < &conf[pc.num_matches]; p++) {
214 printf("%s%d@pci%d:%d:%d:%d:\tclass=0x%06x card=0x%08x "
215 "chip=0x%08x rev=0x%02x hdr=0x%02x\n",
216 (p->pd_name && *p->pd_name) ? p->pd_name :
218 (p->pd_name && *p->pd_name) ? (int)p->pd_unit :
219 none_count++, p->pc_sel.pc_domain,
220 p->pc_sel.pc_bus, p->pc_sel.pc_dev,
221 p->pc_sel.pc_func, (p->pc_class << 16) |
222 (p->pc_subclass << 8) | p->pc_progif,
223 (p->pc_subdevice << 16) | p->pc_subvendor,
224 (p->pc_device << 16) | p->pc_vendor,
225 p->pc_revid, p->pc_hdr);
235 } while (pc.status == PCI_GETCONF_MORE_DEVS);
241 list_bars(int fd, struct pci_conf *p)
243 struct pci_bar_io bar;
248 switch (p->pc_hdr & PCIM_HDRTYPE) {
249 case PCIM_HDRTYPE_NORMAL:
250 max = PCIR_MAX_BAR_0;
252 case PCIM_HDRTYPE_BRIDGE:
253 max = PCIR_MAX_BAR_1;
255 case PCIM_HDRTYPE_CARDBUS:
256 max = PCIR_MAX_BAR_2;
262 for (i = 0; i <= max; i++) {
263 bar.pbi_sel = p->pc_sel;
264 bar.pbi_reg = PCIR_BAR(i);
265 if (ioctl(fd, PCIOCGETBAR, &bar) < 0)
267 if (PCI_BAR_IO(bar.pbi_base)) {
270 base = bar.pbi_base & PCIM_BAR_IO_BASE;
272 if (bar.pbi_base & PCIM_BAR_MEM_PREFETCH)
273 type = "Prefetchable Memory";
276 switch (bar.pbi_base & PCIM_BAR_MEM_TYPE) {
277 case PCIM_BAR_MEM_32:
280 case PCIM_BAR_MEM_1MB:
283 case PCIM_BAR_MEM_64:
289 base = bar.pbi_base & ~((uint64_t)0xf);
291 printf(" bar [%02x] = type %s, range %2d, base %#jx, ",
292 PCIR_BAR(i), type, range, (uintmax_t)base);
293 printf("size %ju, %s\n", (uintmax_t)bar.pbi_length,
294 bar.pbi_enabled ? "enabled" : "disabled");
299 list_verbose(struct pci_conf *p)
301 struct pci_vendor_info *vi;
302 struct pci_device_info *di;
305 TAILQ_FOREACH(vi, &pci_vendors, link) {
306 if (vi->id == p->pc_vendor) {
307 printf(" vendor = '%s'\n", vi->desc);
314 TAILQ_FOREACH(di, &vi->devs, link) {
315 if (di->id == p->pc_device) {
316 printf(" device = '%s'\n", di->desc);
321 if ((dp = guess_class(p)) != NULL)
322 printf(" class = %s\n", dp);
323 if ((dp = guess_subclass(p)) != NULL)
324 printf(" subclass = %s\n", dp);
328 * This is a direct cut-and-paste from the table in sys/dev/pci/pci.c.
335 } pci_nomatch_tab[] = {
336 {PCIC_OLD, -1, "old"},
337 {PCIC_OLD, PCIS_OLD_NONVGA, "non-VGA display device"},
338 {PCIC_OLD, PCIS_OLD_VGA, "VGA-compatible display device"},
339 {PCIC_STORAGE, -1, "mass storage"},
340 {PCIC_STORAGE, PCIS_STORAGE_SCSI, "SCSI"},
341 {PCIC_STORAGE, PCIS_STORAGE_IDE, "ATA"},
342 {PCIC_STORAGE, PCIS_STORAGE_FLOPPY, "floppy disk"},
343 {PCIC_STORAGE, PCIS_STORAGE_IPI, "IPI"},
344 {PCIC_STORAGE, PCIS_STORAGE_RAID, "RAID"},
345 {PCIC_STORAGE, PCIS_STORAGE_ATA_ADMA, "ATA (ADMA)"},
346 {PCIC_STORAGE, PCIS_STORAGE_SATA, "SATA"},
347 {PCIC_STORAGE, PCIS_STORAGE_SAS, "SAS"},
348 {PCIC_STORAGE, PCIS_STORAGE_NVM, "NVM"},
349 {PCIC_NETWORK, -1, "network"},
350 {PCIC_NETWORK, PCIS_NETWORK_ETHERNET, "ethernet"},
351 {PCIC_NETWORK, PCIS_NETWORK_TOKENRING, "token ring"},
352 {PCIC_NETWORK, PCIS_NETWORK_FDDI, "fddi"},
353 {PCIC_NETWORK, PCIS_NETWORK_ATM, "ATM"},
354 {PCIC_NETWORK, PCIS_NETWORK_ISDN, "ISDN"},
355 {PCIC_DISPLAY, -1, "display"},
356 {PCIC_DISPLAY, PCIS_DISPLAY_VGA, "VGA"},
357 {PCIC_DISPLAY, PCIS_DISPLAY_XGA, "XGA"},
358 {PCIC_DISPLAY, PCIS_DISPLAY_3D, "3D"},
359 {PCIC_MULTIMEDIA, -1, "multimedia"},
360 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_VIDEO, "video"},
361 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_AUDIO, "audio"},
362 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_TELE, "telephony"},
363 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_HDA, "HDA"},
364 {PCIC_MEMORY, -1, "memory"},
365 {PCIC_MEMORY, PCIS_MEMORY_RAM, "RAM"},
366 {PCIC_MEMORY, PCIS_MEMORY_FLASH, "flash"},
367 {PCIC_BRIDGE, -1, "bridge"},
368 {PCIC_BRIDGE, PCIS_BRIDGE_HOST, "HOST-PCI"},
369 {PCIC_BRIDGE, PCIS_BRIDGE_ISA, "PCI-ISA"},
370 {PCIC_BRIDGE, PCIS_BRIDGE_EISA, "PCI-EISA"},
371 {PCIC_BRIDGE, PCIS_BRIDGE_MCA, "PCI-MCA"},
372 {PCIC_BRIDGE, PCIS_BRIDGE_PCI, "PCI-PCI"},
373 {PCIC_BRIDGE, PCIS_BRIDGE_PCMCIA, "PCI-PCMCIA"},
374 {PCIC_BRIDGE, PCIS_BRIDGE_NUBUS, "PCI-NuBus"},
375 {PCIC_BRIDGE, PCIS_BRIDGE_CARDBUS, "PCI-CardBus"},
376 {PCIC_BRIDGE, PCIS_BRIDGE_RACEWAY, "PCI-RACEway"},
377 {PCIC_SIMPLECOMM, -1, "simple comms"},
378 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_UART, "UART"}, /* could detect 16550 */
379 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_PAR, "parallel port"},
380 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_MULSER, "multiport serial"},
381 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_MODEM, "generic modem"},
382 {PCIC_BASEPERIPH, -1, "base peripheral"},
383 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_PIC, "interrupt controller"},
384 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_DMA, "DMA controller"},
385 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_TIMER, "timer"},
386 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_RTC, "realtime clock"},
387 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_PCIHOT, "PCI hot-plug controller"},
388 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_SDHC, "SD host controller"},
389 {PCIC_INPUTDEV, -1, "input device"},
390 {PCIC_INPUTDEV, PCIS_INPUTDEV_KEYBOARD, "keyboard"},
391 {PCIC_INPUTDEV, PCIS_INPUTDEV_DIGITIZER,"digitizer"},
392 {PCIC_INPUTDEV, PCIS_INPUTDEV_MOUSE, "mouse"},
393 {PCIC_INPUTDEV, PCIS_INPUTDEV_SCANNER, "scanner"},
394 {PCIC_INPUTDEV, PCIS_INPUTDEV_GAMEPORT, "gameport"},
395 {PCIC_DOCKING, -1, "docking station"},
396 {PCIC_PROCESSOR, -1, "processor"},
397 {PCIC_SERIALBUS, -1, "serial bus"},
398 {PCIC_SERIALBUS, PCIS_SERIALBUS_FW, "FireWire"},
399 {PCIC_SERIALBUS, PCIS_SERIALBUS_ACCESS, "AccessBus"},
400 {PCIC_SERIALBUS, PCIS_SERIALBUS_SSA, "SSA"},
401 {PCIC_SERIALBUS, PCIS_SERIALBUS_USB, "USB"},
402 {PCIC_SERIALBUS, PCIS_SERIALBUS_FC, "Fibre Channel"},
403 {PCIC_SERIALBUS, PCIS_SERIALBUS_SMBUS, "SMBus"},
404 {PCIC_WIRELESS, -1, "wireless controller"},
405 {PCIC_WIRELESS, PCIS_WIRELESS_IRDA, "iRDA"},
406 {PCIC_WIRELESS, PCIS_WIRELESS_IR, "IR"},
407 {PCIC_WIRELESS, PCIS_WIRELESS_RF, "RF"},
408 {PCIC_INTELLIIO, -1, "intelligent I/O controller"},
409 {PCIC_INTELLIIO, PCIS_INTELLIIO_I2O, "I2O"},
410 {PCIC_SATCOM, -1, "satellite communication"},
411 {PCIC_SATCOM, PCIS_SATCOM_TV, "sat TV"},
412 {PCIC_SATCOM, PCIS_SATCOM_AUDIO, "sat audio"},
413 {PCIC_SATCOM, PCIS_SATCOM_VOICE, "sat voice"},
414 {PCIC_SATCOM, PCIS_SATCOM_DATA, "sat data"},
415 {PCIC_CRYPTO, -1, "encrypt/decrypt"},
416 {PCIC_CRYPTO, PCIS_CRYPTO_NETCOMP, "network/computer crypto"},
417 {PCIC_CRYPTO, PCIS_CRYPTO_NETCOMP, "entertainment crypto"},
418 {PCIC_DASP, -1, "dasp"},
419 {PCIC_DASP, PCIS_DASP_DPIO, "DPIO module"},
424 guess_class(struct pci_conf *p)
428 for (i = 0; pci_nomatch_tab[i].desc != NULL; i++) {
429 if (pci_nomatch_tab[i].class == p->pc_class)
430 return(pci_nomatch_tab[i].desc);
436 guess_subclass(struct pci_conf *p)
440 for (i = 0; pci_nomatch_tab[i].desc != NULL; i++) {
441 if ((pci_nomatch_tab[i].class == p->pc_class) &&
442 (pci_nomatch_tab[i].subclass == p->pc_subclass))
443 return(pci_nomatch_tab[i].desc);
453 struct pci_vendor_info *cv;
454 struct pci_device_info *cd;
455 char buf[1024], str[1024];
460 * Locate the database and initialise.
462 TAILQ_INIT(&pci_vendors);
463 if ((dbf = getenv("PCICONF_VENDOR_DATABASE")) == NULL)
465 if ((db = fopen(dbf, "r")) == NULL)
472 * Scan input lines from the database
475 if (fgets(buf, sizeof(buf), db) == NULL)
478 if ((ch = strchr(buf, '#')) != NULL)
480 ch = strchr(buf, '\0') - 1;
481 while (ch > buf && isspace(*ch))
486 /* Can't handle subvendor / subdevice entries yet */
487 if (buf[0] == '\t' && buf[1] == '\t')
490 /* Check for vendor entry */
491 if (buf[0] != '\t' && sscanf(buf, "%04x %[^\n]", &id, str) == 2) {
492 if ((id == 0) || (strlen(str) < 1))
494 if ((cv = malloc(sizeof(struct pci_vendor_info))) == NULL) {
495 warn("allocating vendor entry");
499 if ((cv->desc = strdup(str)) == NULL) {
501 warn("allocating vendor description");
506 TAILQ_INIT(&cv->devs);
507 TAILQ_INSERT_TAIL(&pci_vendors, cv, link);
511 /* Check for device entry */
512 if (buf[0] == '\t' && sscanf(buf + 1, "%04x %[^\n]", &id, str) == 2) {
513 if ((id == 0) || (strlen(str) < 1))
516 warnx("device entry with no vendor!");
519 if ((cd = malloc(sizeof(struct pci_device_info))) == NULL) {
520 warn("allocating device entry");
524 if ((cd->desc = strdup(str)) == NULL) {
526 warn("allocating device description");
531 TAILQ_INSERT_TAIL(&cv->devs, cd, link);
535 /* It's a comment or junk, ignore it */
545 read_config(int fd, struct pcisel *sel, long reg, int width)
553 if (ioctl(fd, PCIOCREAD, &pi) < 0)
554 err(1, "ioctl(PCIOCREAD)");
560 getsel(const char *str)
562 char *ep = strchr(str, '@');
565 unsigned long selarr[4];
575 if (strncmp(ep, "pci", 3) == 0) {
579 selarr[i++] = strtoul(ep, &ep, 10);
580 } while ((*ep == ':' || *ep == '.') && *++ep != '\0' && i < 4);
583 sel.pc_func = selarr[--i];
586 sel.pc_dev = selarr[--i];
587 sel.pc_bus = selarr[--i];
589 sel.pc_domain = selarr[--i];
593 if (*ep != '\x0' || ep == epbase)
594 errx(1, "cannot parse selector %s", str);
599 readone(int fd, struct pcisel *sel, long reg, int width)
602 printf("%0*x", width*2, read_config(fd, sel, reg, width));
606 readit(const char *name, const char *reg, int width)
616 fd = open(_PATH_DEVPCI, O_RDWR, 0);
618 err(1, "%s", _PATH_DEVPCI);
620 rend = rstart = strtol(reg, &end, 0);
621 if (end && *end == ':') {
623 rend = strtol(end, (char **) 0, 0);
626 for (i = 1, r = rstart; r <= rend; i++, r += width) {
627 readone(fd, &sel, r, width);
630 putchar(i % (16/width) ? ' ' : '\n');
632 if (i % (16/width) != 1)
638 writeit(const char *name, const char *reg, const char *data, int width)
643 pi.pi_sel = getsel(name);
644 pi.pi_reg = strtoul(reg, (char **)0, 0); /* XXX error check */
646 pi.pi_data = strtoul(data, (char **)0, 0); /* XXX error check */
648 fd = open(_PATH_DEVPCI, O_RDWR, 0);
650 err(1, "%s", _PATH_DEVPCI);
652 if (ioctl(fd, PCIOCWRITE, &pi) < 0)
653 err(1, "ioctl(PCIOCWRITE)");
657 chkattached(const char *name)
662 pi.pi_sel = getsel(name);
664 fd = open(_PATH_DEVPCI, O_RDWR, 0);
666 err(1, "%s", _PATH_DEVPCI);
668 if (ioctl(fd, PCIOCATTACHED, &pi) < 0)
669 err(1, "ioctl(PCIOCATTACHED)");
671 exitstatus = pi.pi_data ? 0 : 2; /* exit(2), if NOT attached */
672 printf("%s: %s%s\n", name, pi.pi_data == 0 ? "not " : "", "attached");