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>
43 #include <sys/pciio.h>
44 #include <sys/queue.h>
46 #include <dev/pci/pcireg.h>
48 #include "pathnames.h"
50 struct pci_device_info
52 TAILQ_ENTRY(pci_device_info) link;
57 struct pci_vendor_info
59 TAILQ_ENTRY(pci_vendor_info) link;
60 TAILQ_HEAD(,pci_device_info) devs;
65 TAILQ_HEAD(,pci_vendor_info) pci_vendors;
67 static void dobar(const char *);
68 static void list_devs(int vendors);
69 static void list_verbose(struct pci_conf *p);
70 static char *guess_class(struct pci_conf *p);
71 static char *guess_subclass(struct pci_conf *p);
72 static int load_vendors(void);
73 static void readit(const char *, const char *, int);
74 static void writeit(const char *, const char *, const char *, int);
75 static void chkattached(const char *, int);
77 static int exitstatus = 0;
82 fprintf(stderr, "%s\n%s\n%s\n%s\n",
83 "usage: pciconf -l [-v]",
84 " pciconf -a selector",
85 " pciconf -r [-b | -h] selector addr[:addr2]",
86 " pciconf -w [-b | -h] selector addr value");
91 main(int argc, char **argv)
94 int listmode, readmode, writemode, attachedmode, barmode, verbose;
97 barmode = listmode = readmode = writemode = attachedmode = verbose = byte = isshort = 0;
99 while ((c = getopt(argc, argv, "aBbhlrwv")) != -1) {
138 if ((listmode && optind != argc)
139 || (writemode && optind + 3 != argc)
140 || (readmode && optind + 2 != argc)
141 || (attachedmode && optind + 1 != argc)
142 || (barmode && optind + 1 != argc)
147 } else if (attachedmode) {
148 chkattached(argv[optind],
149 byte ? 1 : isshort ? 2 : 4);
150 } else if (readmode) {
151 readit(argv[optind], argv[optind + 1],
152 byte ? 1 : isshort ? 2 : 4);
153 } else if (writemode) {
154 writeit(argv[optind], argv[optind + 1], argv[optind + 2],
155 byte ? 1 : isshort ? 2 : 4);
156 } else if (barmode) {
165 list_devs(int verbose)
168 struct pci_conf_io pc;
169 struct pci_conf conf[255], *p;
175 fd = open(_PATH_DEVPCI, O_RDONLY, 0);
177 err(1, "%s", _PATH_DEVPCI);
179 bzero(&pc, sizeof(struct pci_conf_io));
180 pc.match_buf_len = sizeof(conf);
184 if (ioctl(fd, PCIOCGETCONF, &pc) == -1)
185 err(1, "ioctl(PCIOCGETCONF)");
188 * 255 entries should be more than enough for most people,
189 * but if someone has more devices, and then changes things
190 * around between ioctls, we'll do the cheezy thing and
191 * just bail. The alternative would be to go back to the
192 * beginning of the list, and print things twice, which may
195 if (pc.status == PCI_GETCONF_LIST_CHANGED) {
196 warnx("PCI device list changed, please try again");
200 } else if (pc.status == PCI_GETCONF_ERROR) {
201 warnx("error returned from PCIOCGETCONF ioctl");
206 for (p = conf; p < &conf[pc.num_matches]; p++) {
208 printf("%s%d@pci%d:%d:%d:\tclass=0x%06x card=0x%08x "
209 "chip=0x%08x rev=0x%02x hdr=0x%02x\n",
210 (p->pd_name && *p->pd_name) ? p->pd_name :
212 (p->pd_name && *p->pd_name) ? (int)p->pd_unit :
214 p->pc_sel.pc_bus, p->pc_sel.pc_dev,
215 p->pc_sel.pc_func, (p->pc_class << 16) |
216 (p->pc_subclass << 8) | p->pc_progif,
217 (p->pc_subdevice << 16) | p->pc_subvendor,
218 (p->pc_device << 16) | p->pc_vendor,
219 p->pc_revid, p->pc_hdr);
223 } while (pc.status == PCI_GETCONF_MORE_DEVS);
229 list_verbose(struct pci_conf *p)
231 struct pci_vendor_info *vi;
232 struct pci_device_info *di;
235 TAILQ_FOREACH(vi, &pci_vendors, link) {
236 if (vi->id == p->pc_vendor) {
237 printf(" vendor = '%s'\n", vi->desc);
244 TAILQ_FOREACH(di, &vi->devs, link) {
245 if (di->id == p->pc_device) {
246 printf(" device = '%s'\n", di->desc);
251 if ((dp = guess_class(p)) != NULL)
252 printf(" class = %s\n", dp);
253 if ((dp = guess_subclass(p)) != NULL)
254 printf(" subclass = %s\n", dp);
258 * This is a direct cut-and-paste from the table in sys/dev/pci/pci.c.
265 } pci_nomatch_tab[] = {
266 {PCIC_OLD, -1, "old"},
267 {PCIC_OLD, PCIS_OLD_NONVGA, "non-VGA display device"},
268 {PCIC_OLD, PCIS_OLD_VGA, "VGA-compatible display device"},
269 {PCIC_STORAGE, -1, "mass storage"},
270 {PCIC_STORAGE, PCIS_STORAGE_SCSI, "SCSI"},
271 {PCIC_STORAGE, PCIS_STORAGE_IDE, "ATA"},
272 {PCIC_STORAGE, PCIS_STORAGE_FLOPPY, "floppy disk"},
273 {PCIC_STORAGE, PCIS_STORAGE_IPI, "IPI"},
274 {PCIC_STORAGE, PCIS_STORAGE_RAID, "RAID"},
275 {PCIC_NETWORK, -1, "network"},
276 {PCIC_NETWORK, PCIS_NETWORK_ETHERNET, "ethernet"},
277 {PCIC_NETWORK, PCIS_NETWORK_TOKENRING, "token ring"},
278 {PCIC_NETWORK, PCIS_NETWORK_FDDI, "fddi"},
279 {PCIC_NETWORK, PCIS_NETWORK_ATM, "ATM"},
280 {PCIC_DISPLAY, -1, "display"},
281 {PCIC_DISPLAY, PCIS_DISPLAY_VGA, "VGA"},
282 {PCIC_DISPLAY, PCIS_DISPLAY_XGA, "XGA"},
283 {PCIC_MULTIMEDIA, -1, "multimedia"},
284 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_VIDEO, "video"},
285 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_AUDIO, "audio"},
286 {PCIC_MEMORY, -1, "memory"},
287 {PCIC_MEMORY, PCIS_MEMORY_RAM, "RAM"},
288 {PCIC_MEMORY, PCIS_MEMORY_FLASH, "flash"},
289 {PCIC_BRIDGE, -1, "bridge"},
290 {PCIC_BRIDGE, PCIS_BRIDGE_HOST, "HOST-PCI"},
291 {PCIC_BRIDGE, PCIS_BRIDGE_ISA, "PCI-ISA"},
292 {PCIC_BRIDGE, PCIS_BRIDGE_EISA, "PCI-EISA"},
293 {PCIC_BRIDGE, PCIS_BRIDGE_MCA, "PCI-MCA"},
294 {PCIC_BRIDGE, PCIS_BRIDGE_PCI, "PCI-PCI"},
295 {PCIC_BRIDGE, PCIS_BRIDGE_PCMCIA, "PCI-PCMCIA"},
296 {PCIC_BRIDGE, PCIS_BRIDGE_NUBUS, "PCI-NuBus"},
297 {PCIC_BRIDGE, PCIS_BRIDGE_CARDBUS, "PCI-CardBus"},
298 {PCIC_BRIDGE, PCIS_BRIDGE_OTHER, "PCI-unknown"},
299 {PCIC_SIMPLECOMM, -1, "simple comms"},
300 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_UART, "UART"}, /* could detect 16550 */
301 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_PAR, "parallel port"},
302 {PCIC_BASEPERIPH, -1, "base peripheral"},
303 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_PIC, "interrupt controller"},
304 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_DMA, "DMA controller"},
305 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_TIMER, "timer"},
306 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_RTC, "realtime clock"},
307 {PCIC_INPUTDEV, -1, "input device"},
308 {PCIC_INPUTDEV, PCIS_INPUTDEV_KEYBOARD, "keyboard"},
309 {PCIC_INPUTDEV, PCIS_INPUTDEV_DIGITIZER,"digitizer"},
310 {PCIC_INPUTDEV, PCIS_INPUTDEV_MOUSE, "mouse"},
311 {PCIC_DOCKING, -1, "docking station"},
312 {PCIC_PROCESSOR, -1, "processor"},
313 {PCIC_SERIALBUS, -1, "serial bus"},
314 {PCIC_SERIALBUS, PCIS_SERIALBUS_FW, "FireWire"},
315 {PCIC_SERIALBUS, PCIS_SERIALBUS_ACCESS, "AccessBus"},
316 {PCIC_SERIALBUS, PCIS_SERIALBUS_SSA, "SSA"},
317 {PCIC_SERIALBUS, PCIS_SERIALBUS_USB, "USB"},
318 {PCIC_SERIALBUS, PCIS_SERIALBUS_FC, "Fibre Channel"},
319 {PCIC_SERIALBUS, PCIS_SERIALBUS_SMBUS, "SMBus"},
324 guess_class(struct pci_conf *p)
328 for (i = 0; pci_nomatch_tab[i].desc != NULL; i++) {
329 if (pci_nomatch_tab[i].class == p->pc_class)
330 return(pci_nomatch_tab[i].desc);
336 guess_subclass(struct pci_conf *p)
340 for (i = 0; pci_nomatch_tab[i].desc != NULL; i++) {
341 if ((pci_nomatch_tab[i].class == p->pc_class) &&
342 (pci_nomatch_tab[i].subclass == p->pc_subclass))
343 return(pci_nomatch_tab[i].desc);
353 struct pci_vendor_info *cv;
354 struct pci_device_info *cd;
355 char buf[100], str[100];
359 * Locate the database and initialise.
361 TAILQ_INIT(&pci_vendors);
362 if ((dbf = getenv("PCICONF_VENDOR_DATABASE")) == NULL)
364 if ((db = fopen(dbf, "r")) == NULL)
371 * Scan input lines from the database
374 if (fgets(buf, sizeof(buf), db) == NULL)
377 /* Check for vendor entry */
378 if ((buf[0] != '\t') && (sscanf(buf, "%04x\t%[^\n]", &id, str) == 2)) {
379 if ((id == 0) || (strlen(str) < 1))
381 if ((cv = malloc(sizeof(struct pci_vendor_info))) == NULL) {
382 warn("allocating vendor entry");
386 if ((cv->desc = strdup(str)) == NULL) {
388 warn("allocating vendor description");
393 TAILQ_INIT(&cv->devs);
394 TAILQ_INSERT_TAIL(&pci_vendors, cv, link);
398 /* Check for device entry */
399 if ((buf[0] == '\t') && (sscanf(buf + 1, "%04x\t%[^\n]", &id, str) == 2)) {
400 if ((id == 0) || (strlen(str) < 1))
403 warnx("device entry with no vendor!");
406 if ((cd = malloc(sizeof(struct pci_device_info))) == NULL) {
407 warn("allocating device entry");
411 if ((cd->desc = strdup(str)) == NULL) {
413 warn("allocating device description");
418 TAILQ_INSERT_TAIL(&cv->devs, cd, link);
422 /* It's a comment or junk, ignore it */
433 getsel(const char *str)
435 char *ep = strchr(str, '@');
446 if (strncmp(ep, "pci", 3) == 0) {
448 sel.pc_bus = strtoul(ep, &ep, 0);
449 if (!ep || *ep++ != ':')
450 errx(1, "cannot parse selector %s", str);
451 sel.pc_dev = strtoul(ep, &ep, 0);
452 if (!ep || *ep != ':') {
456 sel.pc_func = strtoul(ep, &ep, 0);
461 if (*ep != '\x0' || ep == epbase)
462 errx(1, "cannot parse selector %s", str);
467 readone(int fd, struct pcisel *sel, long reg, int width)
475 if (ioctl(fd, PCIOCREAD, &pi) < 0)
476 err(1, "ioctl(PCIOCREAD)");
478 printf("%0*x", width*2, pi.pi_data);
482 dobar(const char *name)
496 fd = open(_PATH_DEVPCI, O_RDWR, 0);
498 err(1, "%s", _PATH_DEVPCI);
504 for (i = 0, r = rstart; r <= rend; i++, r += 4) {
508 if (ioctl(fd, PCIOCREAD, &pi) < 0)
509 err(1, "ioctl(PCIOCREAD)");
510 bars[i++] = pi.pi_data;
516 for (i = 0; i < NBAR; i++) {
530 readit(const char *name, const char *reg, int width)
540 fd = open(_PATH_DEVPCI, O_RDWR, 0);
542 err(1, "%s", _PATH_DEVPCI);
544 rend = rstart = strtol(reg, &end, 0);
545 if (end && *end == ':') {
547 rend = strtol(end, (char **) 0, 0);
550 for (i = 1, r = rstart; r <= rend; i++, r += width) {
551 readone(fd, &sel, r, width);
552 if (i && !(i % 8)) putchar(' ');
553 putchar(i % (16/width) ? ' ' : '\n');
555 if (i % (16/width) != 1)
561 writeit(const char *name, const char *reg, const char *data, int width)
566 pi.pi_sel = getsel(name);
567 pi.pi_reg = strtoul(reg, (char **)0, 0); /* XXX error check */
569 pi.pi_data = strtoul(data, (char **)0, 0); /* XXX error check */
571 fd = open(_PATH_DEVPCI, O_RDWR, 0);
573 err(1, "%s", _PATH_DEVPCI);
575 if (ioctl(fd, PCIOCWRITE, &pi) < 0)
576 err(1, "ioctl(PCIOCWRITE)");
580 chkattached (const char *name, int width)
585 pi.pi_sel = getsel(name);
590 fd = open(_PATH_DEVPCI, O_RDWR, 0);
592 err(1, "%s", _PATH_DEVPCI);
594 if (ioctl(fd, PCIOCATTACHED, &pi) < 0)
595 err(1, "ioctl(PCIOCATTACHED)");
597 exitstatus = pi.pi_data ? 0 : 2; /* exit(2), if NOT attached */
598 printf("%s: %s%s\n", name, pi.pi_data == 0 ? "not " : "", "attached");