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);
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 *, int);
80 static int exitstatus = 0;
85 fprintf(stderr, "%s\n%s\n%s\n%s\n",
86 "usage: pciconf -l [-bcv]",
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, bars, caps, verbose;
100 listmode = readmode = writemode = attachedmode = bars = caps = verbose = byte = isshort = 0;
102 while ((c = getopt(argc, argv, "abchlrwv")) != -1) {
142 if ((listmode && optind != argc)
143 || (writemode && optind + 3 != argc)
144 || (readmode && optind + 2 != argc)
145 || (attachedmode && optind + 1 != argc))
149 list_devs(verbose, bars, caps);
150 } else if (attachedmode) {
151 chkattached(argv[optind],
152 byte ? 1 : isshort ? 2 : 4);
153 } else if (readmode) {
154 readit(argv[optind], argv[optind + 1],
155 byte ? 1 : isshort ? 2 : 4);
156 } else if (writemode) {
157 writeit(argv[optind], argv[optind + 1], argv[optind + 2],
158 byte ? 1 : isshort ? 2 : 4);
167 list_devs(int verbose, int bars, int caps)
170 struct pci_conf_io pc;
171 struct pci_conf conf[255], *p;
177 fd = open(_PATH_DEVPCI, caps ? O_RDWR : O_RDONLY, 0);
179 err(1, "%s", _PATH_DEVPCI);
181 bzero(&pc, sizeof(struct pci_conf_io));
182 pc.match_buf_len = sizeof(conf);
186 if (ioctl(fd, PCIOCGETCONF, &pc) == -1)
187 err(1, "ioctl(PCIOCGETCONF)");
190 * 255 entries should be more than enough for most people,
191 * but if someone has more devices, and then changes things
192 * around between ioctls, we'll do the cheezy thing and
193 * just bail. The alternative would be to go back to the
194 * beginning of the list, and print things twice, which may
197 if (pc.status == PCI_GETCONF_LIST_CHANGED) {
198 warnx("PCI device list changed, please try again");
202 } else if (pc.status == PCI_GETCONF_ERROR) {
203 warnx("error returned from PCIOCGETCONF ioctl");
208 for (p = conf; p < &conf[pc.num_matches]; p++) {
209 printf("%s%d@pci%d:%d:%d:%d:\tclass=0x%06x card=0x%08x "
210 "chip=0x%08x rev=0x%02x hdr=0x%02x\n",
211 (p->pd_name && *p->pd_name) ? p->pd_name :
213 (p->pd_name && *p->pd_name) ? (int)p->pd_unit :
214 none_count++, p->pc_sel.pc_domain,
215 p->pc_sel.pc_bus, p->pc_sel.pc_dev,
216 p->pc_sel.pc_func, (p->pc_class << 16) |
217 (p->pc_subclass << 8) | p->pc_progif,
218 (p->pc_subdevice << 16) | p->pc_subvendor,
219 (p->pc_device << 16) | p->pc_vendor,
220 p->pc_revid, p->pc_hdr);
228 } while (pc.status == PCI_GETCONF_MORE_DEVS);
234 list_bars(int fd, struct pci_conf *p)
236 struct pci_bar_io bar;
241 switch (p->pc_hdr & PCIM_HDRTYPE) {
242 case PCIM_HDRTYPE_NORMAL:
243 max = PCIR_MAX_BAR_0;
245 case PCIM_HDRTYPE_BRIDGE:
246 max = PCIR_MAX_BAR_1;
248 case PCIM_HDRTYPE_CARDBUS:
249 max = PCIR_MAX_BAR_2;
255 for (i = 0; i <= max; i++) {
256 bar.pbi_sel = p->pc_sel;
257 bar.pbi_reg = PCIR_BAR(i);
258 if (ioctl(fd, PCIOCGETBAR, &bar) < 0)
260 if (PCI_BAR_IO(bar.pbi_base)) {
263 base = bar.pbi_base & PCIM_BAR_IO_BASE;
265 if (bar.pbi_base & PCIM_BAR_MEM_PREFETCH)
266 type = "Prefetchable Memory";
269 switch (bar.pbi_base & PCIM_BAR_MEM_TYPE) {
270 case PCIM_BAR_MEM_32:
273 case PCIM_BAR_MEM_1MB:
276 case PCIM_BAR_MEM_64:
282 base = bar.pbi_base & ~((uint64_t)0xf);
284 printf(" bar [%02x] = type %s, range %2d, base %#jx, ",
285 PCIR_BAR(i), type, range, (uintmax_t)base);
286 printf("size %2d, %s\n", (int)bar.pbi_length,
287 bar.pbi_enabled ? "enabled" : "disabled");
292 list_verbose(struct pci_conf *p)
294 struct pci_vendor_info *vi;
295 struct pci_device_info *di;
298 TAILQ_FOREACH(vi, &pci_vendors, link) {
299 if (vi->id == p->pc_vendor) {
300 printf(" vendor = '%s'\n", vi->desc);
307 TAILQ_FOREACH(di, &vi->devs, link) {
308 if (di->id == p->pc_device) {
309 printf(" device = '%s'\n", di->desc);
314 if ((dp = guess_class(p)) != NULL)
315 printf(" class = %s\n", dp);
316 if ((dp = guess_subclass(p)) != NULL)
317 printf(" subclass = %s\n", dp);
321 * This is a direct cut-and-paste from the table in sys/dev/pci/pci.c.
328 } pci_nomatch_tab[] = {
329 {PCIC_OLD, -1, "old"},
330 {PCIC_OLD, PCIS_OLD_NONVGA, "non-VGA display device"},
331 {PCIC_OLD, PCIS_OLD_VGA, "VGA-compatible display device"},
332 {PCIC_STORAGE, -1, "mass storage"},
333 {PCIC_STORAGE, PCIS_STORAGE_SCSI, "SCSI"},
334 {PCIC_STORAGE, PCIS_STORAGE_IDE, "ATA"},
335 {PCIC_STORAGE, PCIS_STORAGE_FLOPPY, "floppy disk"},
336 {PCIC_STORAGE, PCIS_STORAGE_IPI, "IPI"},
337 {PCIC_STORAGE, PCIS_STORAGE_RAID, "RAID"},
338 {PCIC_STORAGE, PCIS_STORAGE_ATA_ADMA, "ATA (ADMA)"},
339 {PCIC_STORAGE, PCIS_STORAGE_SATA, "SATA"},
340 {PCIC_STORAGE, PCIS_STORAGE_SAS, "SAS"},
341 {PCIC_NETWORK, -1, "network"},
342 {PCIC_NETWORK, PCIS_NETWORK_ETHERNET, "ethernet"},
343 {PCIC_NETWORK, PCIS_NETWORK_TOKENRING, "token ring"},
344 {PCIC_NETWORK, PCIS_NETWORK_FDDI, "fddi"},
345 {PCIC_NETWORK, PCIS_NETWORK_ATM, "ATM"},
346 {PCIC_NETWORK, PCIS_NETWORK_ISDN, "ISDN"},
347 {PCIC_DISPLAY, -1, "display"},
348 {PCIC_DISPLAY, PCIS_DISPLAY_VGA, "VGA"},
349 {PCIC_DISPLAY, PCIS_DISPLAY_XGA, "XGA"},
350 {PCIC_DISPLAY, PCIS_DISPLAY_3D, "3D"},
351 {PCIC_MULTIMEDIA, -1, "multimedia"},
352 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_VIDEO, "video"},
353 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_AUDIO, "audio"},
354 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_TELE, "telephony"},
355 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_HDA, "HDA"},
356 {PCIC_MEMORY, -1, "memory"},
357 {PCIC_MEMORY, PCIS_MEMORY_RAM, "RAM"},
358 {PCIC_MEMORY, PCIS_MEMORY_FLASH, "flash"},
359 {PCIC_BRIDGE, -1, "bridge"},
360 {PCIC_BRIDGE, PCIS_BRIDGE_HOST, "HOST-PCI"},
361 {PCIC_BRIDGE, PCIS_BRIDGE_ISA, "PCI-ISA"},
362 {PCIC_BRIDGE, PCIS_BRIDGE_EISA, "PCI-EISA"},
363 {PCIC_BRIDGE, PCIS_BRIDGE_MCA, "PCI-MCA"},
364 {PCIC_BRIDGE, PCIS_BRIDGE_PCI, "PCI-PCI"},
365 {PCIC_BRIDGE, PCIS_BRIDGE_PCMCIA, "PCI-PCMCIA"},
366 {PCIC_BRIDGE, PCIS_BRIDGE_NUBUS, "PCI-NuBus"},
367 {PCIC_BRIDGE, PCIS_BRIDGE_CARDBUS, "PCI-CardBus"},
368 {PCIC_BRIDGE, PCIS_BRIDGE_RACEWAY, "PCI-RACEway"},
369 {PCIC_SIMPLECOMM, -1, "simple comms"},
370 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_UART, "UART"}, /* could detect 16550 */
371 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_PAR, "parallel port"},
372 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_MULSER, "multiport serial"},
373 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_MODEM, "generic modem"},
374 {PCIC_BASEPERIPH, -1, "base peripheral"},
375 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_PIC, "interrupt controller"},
376 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_DMA, "DMA controller"},
377 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_TIMER, "timer"},
378 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_RTC, "realtime clock"},
379 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_PCIHOT, "PCI hot-plug controller"},
380 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_SDHC, "SD host controller"},
381 {PCIC_INPUTDEV, -1, "input device"},
382 {PCIC_INPUTDEV, PCIS_INPUTDEV_KEYBOARD, "keyboard"},
383 {PCIC_INPUTDEV, PCIS_INPUTDEV_DIGITIZER,"digitizer"},
384 {PCIC_INPUTDEV, PCIS_INPUTDEV_MOUSE, "mouse"},
385 {PCIC_INPUTDEV, PCIS_INPUTDEV_SCANNER, "scanner"},
386 {PCIC_INPUTDEV, PCIS_INPUTDEV_GAMEPORT, "gameport"},
387 {PCIC_DOCKING, -1, "docking station"},
388 {PCIC_PROCESSOR, -1, "processor"},
389 {PCIC_SERIALBUS, -1, "serial bus"},
390 {PCIC_SERIALBUS, PCIS_SERIALBUS_FW, "FireWire"},
391 {PCIC_SERIALBUS, PCIS_SERIALBUS_ACCESS, "AccessBus"},
392 {PCIC_SERIALBUS, PCIS_SERIALBUS_SSA, "SSA"},
393 {PCIC_SERIALBUS, PCIS_SERIALBUS_USB, "USB"},
394 {PCIC_SERIALBUS, PCIS_SERIALBUS_FC, "Fibre Channel"},
395 {PCIC_SERIALBUS, PCIS_SERIALBUS_SMBUS, "SMBus"},
396 {PCIC_WIRELESS, -1, "wireless controller"},
397 {PCIC_WIRELESS, PCIS_WIRELESS_IRDA, "iRDA"},
398 {PCIC_WIRELESS, PCIS_WIRELESS_IR, "IR"},
399 {PCIC_WIRELESS, PCIS_WIRELESS_RF, "RF"},
400 {PCIC_INTELLIIO, -1, "intelligent I/O controller"},
401 {PCIC_INTELLIIO, PCIS_INTELLIIO_I2O, "I2O"},
402 {PCIC_SATCOM, -1, "satellite communication"},
403 {PCIC_SATCOM, PCIS_SATCOM_TV, "sat TV"},
404 {PCIC_SATCOM, PCIS_SATCOM_AUDIO, "sat audio"},
405 {PCIC_SATCOM, PCIS_SATCOM_VOICE, "sat voice"},
406 {PCIC_SATCOM, PCIS_SATCOM_DATA, "sat data"},
407 {PCIC_CRYPTO, -1, "encrypt/decrypt"},
408 {PCIC_CRYPTO, PCIS_CRYPTO_NETCOMP, "network/computer crypto"},
409 {PCIC_CRYPTO, PCIS_CRYPTO_NETCOMP, "entertainment crypto"},
410 {PCIC_DASP, -1, "dasp"},
411 {PCIC_DASP, PCIS_DASP_DPIO, "DPIO module"},
416 guess_class(struct pci_conf *p)
420 for (i = 0; pci_nomatch_tab[i].desc != NULL; i++) {
421 if (pci_nomatch_tab[i].class == p->pc_class)
422 return(pci_nomatch_tab[i].desc);
428 guess_subclass(struct pci_conf *p)
432 for (i = 0; pci_nomatch_tab[i].desc != NULL; i++) {
433 if ((pci_nomatch_tab[i].class == p->pc_class) &&
434 (pci_nomatch_tab[i].subclass == p->pc_subclass))
435 return(pci_nomatch_tab[i].desc);
445 struct pci_vendor_info *cv;
446 struct pci_device_info *cd;
447 char buf[1024], str[1024];
452 * Locate the database and initialise.
454 TAILQ_INIT(&pci_vendors);
455 if ((dbf = getenv("PCICONF_VENDOR_DATABASE")) == NULL)
457 if ((db = fopen(dbf, "r")) == NULL)
464 * Scan input lines from the database
467 if (fgets(buf, sizeof(buf), db) == NULL)
470 if ((ch = strchr(buf, '#')) != NULL)
472 ch = strchr(buf, '\0') - 1;
473 while (ch > buf && isspace(*ch))
478 /* Can't handle subvendor / subdevice entries yet */
479 if (buf[0] == '\t' && buf[1] == '\t')
482 /* Check for vendor entry */
483 if (buf[0] != '\t' && sscanf(buf, "%04x %[^\n]", &id, str) == 2) {
484 if ((id == 0) || (strlen(str) < 1))
486 if ((cv = malloc(sizeof(struct pci_vendor_info))) == NULL) {
487 warn("allocating vendor entry");
491 if ((cv->desc = strdup(str)) == NULL) {
493 warn("allocating vendor description");
498 TAILQ_INIT(&cv->devs);
499 TAILQ_INSERT_TAIL(&pci_vendors, cv, link);
503 /* Check for device entry */
504 if (buf[0] == '\t' && sscanf(buf + 1, "%04x %[^\n]", &id, str) == 2) {
505 if ((id == 0) || (strlen(str) < 1))
508 warnx("device entry with no vendor!");
511 if ((cd = malloc(sizeof(struct pci_device_info))) == NULL) {
512 warn("allocating device entry");
516 if ((cd->desc = strdup(str)) == NULL) {
518 warn("allocating device description");
523 TAILQ_INSERT_TAIL(&cv->devs, cd, link);
527 /* It's a comment or junk, ignore it */
537 read_config(int fd, struct pcisel *sel, long reg, int width)
545 if (ioctl(fd, PCIOCREAD, &pi) < 0)
546 err(1, "ioctl(PCIOCREAD)");
552 getsel(const char *str)
554 char *ep = strchr(str, '@');
557 unsigned long selarr[4];
567 if (strncmp(ep, "pci", 3) == 0) {
571 selarr[i++] = strtoul(ep, &ep, 10);
572 } while ((*ep == ':' || *ep == '.') && *++ep != '\0' && i < 4);
575 sel.pc_func = selarr[--i];
578 sel.pc_dev = selarr[--i];
579 sel.pc_bus = selarr[--i];
581 sel.pc_domain = selarr[--i];
585 if (*ep != '\x0' || ep == epbase)
586 errx(1, "cannot parse selector %s", str);
591 readone(int fd, struct pcisel *sel, long reg, int width)
594 printf("%0*x", width*2, read_config(fd, sel, reg, width));
598 readit(const char *name, const char *reg, int width)
608 fd = open(_PATH_DEVPCI, O_RDWR, 0);
610 err(1, "%s", _PATH_DEVPCI);
612 rend = rstart = strtol(reg, &end, 0);
613 if (end && *end == ':') {
615 rend = strtol(end, (char **) 0, 0);
618 for (i = 1, r = rstart; r <= rend; i++, r += width) {
619 readone(fd, &sel, r, width);
620 if (i && !(i % 8)) putchar(' ');
621 putchar(i % (16/width) ? ' ' : '\n');
623 if (i % (16/width) != 1)
629 writeit(const char *name, const char *reg, const char *data, int width)
634 pi.pi_sel = getsel(name);
635 pi.pi_reg = strtoul(reg, (char **)0, 0); /* XXX error check */
637 pi.pi_data = strtoul(data, (char **)0, 0); /* XXX error check */
639 fd = open(_PATH_DEVPCI, O_RDWR, 0);
641 err(1, "%s", _PATH_DEVPCI);
643 if (ioctl(fd, PCIOCWRITE, &pi) < 0)
644 err(1, "ioctl(PCIOCWRITE)");
648 chkattached(const char *name, int width)
653 pi.pi_sel = getsel(name);
658 fd = open(_PATH_DEVPCI, O_RDWR, 0);
660 err(1, "%s", _PATH_DEVPCI);
662 if (ioctl(fd, PCIOCATTACHED, &pi) < 0)
663 err(1, "ioctl(PCIOCATTACHED)");
665 exitstatus = pi.pi_data ? 0 : 2; /* exit(2), if NOT attached */
666 printf("%s: %s%s\n", name, pi.pi_data == 0 ? "not " : "", "attached");