xref: /freebsd/usr.sbin/pciconf/pciconf.c (revision aa0a1e58)
1 /*
2  * Copyright 1996 Massachusetts Institute of Technology
3  *
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
14  * warranty.
15  *
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
27  * SUCH DAMAGE.
28  */
29 
30 #ifndef lint
31 static const char rcsid[] =
32   "$FreeBSD$";
33 #endif /* not lint */
34 
35 #include <sys/types.h>
36 #include <sys/fcntl.h>
37 
38 #include <ctype.h>
39 #include <err.h>
40 #include <inttypes.h>
41 #include <stdlib.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include <sys/pciio.h>
46 #include <sys/queue.h>
47 
48 #include <dev/pci/pcireg.h>
49 
50 #include "pathnames.h"
51 #include "pciconf.h"
52 
53 struct pci_device_info
54 {
55     TAILQ_ENTRY(pci_device_info)	link;
56     int					id;
57     char				*desc;
58 };
59 
60 struct pci_vendor_info
61 {
62     TAILQ_ENTRY(pci_vendor_info)	link;
63     TAILQ_HEAD(,pci_device_info)	devs;
64     int					id;
65     char				*desc;
66 };
67 
68 TAILQ_HEAD(,pci_vendor_info)	pci_vendors;
69 
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 *);
79 
80 static int exitstatus = 0;
81 
82 static void
83 usage(void)
84 {
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");
90 	exit (1);
91 }
92 
93 int
94 main(int argc, char **argv)
95 {
96 	int c;
97 	int listmode, readmode, writemode, attachedmode, bars, caps, verbose;
98 	int byte, isshort;
99 
100 	listmode = readmode = writemode = attachedmode = bars = caps = verbose = byte = isshort = 0;
101 
102 	while ((c = getopt(argc, argv, "abchlrwv")) != -1) {
103 		switch(c) {
104 		case 'a':
105 			attachedmode = 1;
106 			break;
107 
108 		case 'b':
109 			bars = 1;
110 			byte = 1;
111 			break;
112 
113 		case 'c':
114 			caps = 1;
115 			break;
116 
117 		case 'h':
118 			isshort = 1;
119 			break;
120 
121 		case 'l':
122 			listmode = 1;
123 			break;
124 
125 		case 'r':
126 			readmode = 1;
127 			break;
128 
129 		case 'w':
130 			writemode = 1;
131 			break;
132 
133 		case 'v':
134 			verbose = 1;
135 			break;
136 
137 		default:
138 			usage();
139 		}
140 	}
141 
142 	if ((listmode && optind != argc)
143 	    || (writemode && optind + 3 != argc)
144 	    || (readmode && optind + 2 != argc)
145 	    || (attachedmode && optind + 1 != argc))
146 		usage();
147 
148 	if (listmode) {
149 		list_devs(verbose, bars, caps);
150 	} else if (attachedmode) {
151 		chkattached(argv[optind]);
152 	} else if (readmode) {
153 		readit(argv[optind], argv[optind + 1],
154 		    byte ? 1 : isshort ? 2 : 4);
155 	} else if (writemode) {
156 		writeit(argv[optind], argv[optind + 1], argv[optind + 2],
157 		    byte ? 1 : isshort ? 2 : 4);
158 	} else {
159 		usage();
160 	}
161 
162 	return exitstatus;
163 }
164 
165 static void
166 list_devs(int verbose, int bars, int caps)
167 {
168 	int fd;
169 	struct pci_conf_io pc;
170 	struct pci_conf conf[255], *p;
171 	int none_count = 0;
172 
173 	if (verbose)
174 		load_vendors();
175 
176 	fd = open(_PATH_DEVPCI, caps ? O_RDWR : O_RDONLY, 0);
177 	if (fd < 0)
178 		err(1, "%s", _PATH_DEVPCI);
179 
180 	bzero(&pc, sizeof(struct pci_conf_io));
181 	pc.match_buf_len = sizeof(conf);
182 	pc.matches = conf;
183 
184 	do {
185 		if (ioctl(fd, PCIOCGETCONF, &pc) == -1)
186 			err(1, "ioctl(PCIOCGETCONF)");
187 
188 		/*
189 		 * 255 entries should be more than enough for most people,
190 		 * but if someone has more devices, and then changes things
191 		 * around between ioctls, we'll do the cheezy thing and
192 		 * just bail.  The alternative would be to go back to the
193 		 * beginning of the list, and print things twice, which may
194 		 * not be desireable.
195 		 */
196 		if (pc.status == PCI_GETCONF_LIST_CHANGED) {
197 			warnx("PCI device list changed, please try again");
198 			exitstatus = 1;
199 			close(fd);
200 			return;
201 		} else if (pc.status ==  PCI_GETCONF_ERROR) {
202 			warnx("error returned from PCIOCGETCONF ioctl");
203 			exitstatus = 1;
204 			close(fd);
205 			return;
206 		}
207 		for (p = conf; p < &conf[pc.num_matches]; p++) {
208 			printf("%s%d@pci%d:%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 :
211 			    "none",
212 			    (p->pd_name && *p->pd_name) ? (int)p->pd_unit :
213 			    none_count++, p->pc_sel.pc_domain,
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);
220 			if (verbose)
221 				list_verbose(p);
222 			if (bars)
223 				list_bars(fd, p);
224 			if (caps)
225 				list_caps(fd, p);
226 		}
227 	} while (pc.status == PCI_GETCONF_MORE_DEVS);
228 
229 	close(fd);
230 }
231 
232 static void
233 list_bars(int fd, struct pci_conf *p)
234 {
235 	struct pci_bar_io bar;
236 	uint64_t base;
237 	const char *type;
238 	int i, range, max;
239 
240 	switch (p->pc_hdr & PCIM_HDRTYPE) {
241 	case PCIM_HDRTYPE_NORMAL:
242 		max = PCIR_MAX_BAR_0;
243 		break;
244 	case PCIM_HDRTYPE_BRIDGE:
245 		max = PCIR_MAX_BAR_1;
246 		break;
247 	case PCIM_HDRTYPE_CARDBUS:
248 		max = PCIR_MAX_BAR_2;
249 		break;
250 	default:
251 		return;
252 	}
253 
254 	for (i = 0; i <= max; i++) {
255 		bar.pbi_sel = p->pc_sel;
256 		bar.pbi_reg = PCIR_BAR(i);
257 		if (ioctl(fd, PCIOCGETBAR, &bar) < 0)
258 			continue;
259 		if (PCI_BAR_IO(bar.pbi_base)) {
260 			type = "I/O Port";
261 			range = 32;
262 			base = bar.pbi_base & PCIM_BAR_IO_BASE;
263 		} else {
264 			if (bar.pbi_base & PCIM_BAR_MEM_PREFETCH)
265 				type = "Prefetchable Memory";
266 			else
267 				type = "Memory";
268 			switch (bar.pbi_base & PCIM_BAR_MEM_TYPE) {
269 			case PCIM_BAR_MEM_32:
270 				range = 32;
271 				break;
272 			case PCIM_BAR_MEM_1MB:
273 				range = 20;
274 				break;
275 			case PCIM_BAR_MEM_64:
276 				range = 64;
277 				break;
278 			default:
279 				range = -1;
280 			}
281 			base = bar.pbi_base & ~((uint64_t)0xf);
282 		}
283 		printf("    bar   [%02x] = type %s, range %2d, base %#jx, ",
284 		    PCIR_BAR(i), type, range, (uintmax_t)base);
285 		printf("size %2d, %s\n", (int)bar.pbi_length,
286 		    bar.pbi_enabled ? "enabled" : "disabled");
287 	}
288 }
289 
290 static void
291 list_verbose(struct pci_conf *p)
292 {
293 	struct pci_vendor_info	*vi;
294 	struct pci_device_info	*di;
295 	const char *dp;
296 
297 	TAILQ_FOREACH(vi, &pci_vendors, link) {
298 		if (vi->id == p->pc_vendor) {
299 			printf("    vendor     = '%s'\n", vi->desc);
300 			break;
301 		}
302 	}
303 	if (vi == NULL) {
304 		di = NULL;
305 	} else {
306 		TAILQ_FOREACH(di, &vi->devs, link) {
307 			if (di->id == p->pc_device) {
308 				printf("    device     = '%s'\n", di->desc);
309 				break;
310 			}
311 		}
312 	}
313 	if ((dp = guess_class(p)) != NULL)
314 		printf("    class      = %s\n", dp);
315 	if ((dp = guess_subclass(p)) != NULL)
316 		printf("    subclass   = %s\n", dp);
317 }
318 
319 /*
320  * This is a direct cut-and-paste from the table in sys/dev/pci/pci.c.
321  */
322 static struct
323 {
324 	int	class;
325 	int	subclass;
326 	const char *desc;
327 } pci_nomatch_tab[] = {
328 	{PCIC_OLD,		-1,			"old"},
329 	{PCIC_OLD,		PCIS_OLD_NONVGA,	"non-VGA display device"},
330 	{PCIC_OLD,		PCIS_OLD_VGA,		"VGA-compatible display device"},
331 	{PCIC_STORAGE,		-1,			"mass storage"},
332 	{PCIC_STORAGE,		PCIS_STORAGE_SCSI,	"SCSI"},
333 	{PCIC_STORAGE,		PCIS_STORAGE_IDE,	"ATA"},
334 	{PCIC_STORAGE,		PCIS_STORAGE_FLOPPY,	"floppy disk"},
335 	{PCIC_STORAGE,		PCIS_STORAGE_IPI,	"IPI"},
336 	{PCIC_STORAGE,		PCIS_STORAGE_RAID,	"RAID"},
337 	{PCIC_STORAGE,		PCIS_STORAGE_ATA_ADMA,	"ATA (ADMA)"},
338 	{PCIC_STORAGE,		PCIS_STORAGE_SATA,	"SATA"},
339 	{PCIC_STORAGE,		PCIS_STORAGE_SAS,	"SAS"},
340 	{PCIC_NETWORK,		-1,			"network"},
341 	{PCIC_NETWORK,		PCIS_NETWORK_ETHERNET,	"ethernet"},
342 	{PCIC_NETWORK,		PCIS_NETWORK_TOKENRING,	"token ring"},
343 	{PCIC_NETWORK,		PCIS_NETWORK_FDDI,	"fddi"},
344 	{PCIC_NETWORK,		PCIS_NETWORK_ATM,	"ATM"},
345 	{PCIC_NETWORK,		PCIS_NETWORK_ISDN,	"ISDN"},
346 	{PCIC_DISPLAY,		-1,			"display"},
347 	{PCIC_DISPLAY,		PCIS_DISPLAY_VGA,	"VGA"},
348 	{PCIC_DISPLAY,		PCIS_DISPLAY_XGA,	"XGA"},
349 	{PCIC_DISPLAY,		PCIS_DISPLAY_3D,	"3D"},
350 	{PCIC_MULTIMEDIA,	-1,			"multimedia"},
351 	{PCIC_MULTIMEDIA,	PCIS_MULTIMEDIA_VIDEO,	"video"},
352 	{PCIC_MULTIMEDIA,	PCIS_MULTIMEDIA_AUDIO,	"audio"},
353 	{PCIC_MULTIMEDIA,	PCIS_MULTIMEDIA_TELE,	"telephony"},
354 	{PCIC_MULTIMEDIA,	PCIS_MULTIMEDIA_HDA,	"HDA"},
355 	{PCIC_MEMORY,		-1,			"memory"},
356 	{PCIC_MEMORY,		PCIS_MEMORY_RAM,	"RAM"},
357 	{PCIC_MEMORY,		PCIS_MEMORY_FLASH,	"flash"},
358 	{PCIC_BRIDGE,		-1,			"bridge"},
359 	{PCIC_BRIDGE,		PCIS_BRIDGE_HOST,	"HOST-PCI"},
360 	{PCIC_BRIDGE,		PCIS_BRIDGE_ISA,	"PCI-ISA"},
361 	{PCIC_BRIDGE,		PCIS_BRIDGE_EISA,	"PCI-EISA"},
362 	{PCIC_BRIDGE,		PCIS_BRIDGE_MCA,	"PCI-MCA"},
363 	{PCIC_BRIDGE,		PCIS_BRIDGE_PCI,	"PCI-PCI"},
364 	{PCIC_BRIDGE,		PCIS_BRIDGE_PCMCIA,	"PCI-PCMCIA"},
365 	{PCIC_BRIDGE,		PCIS_BRIDGE_NUBUS,	"PCI-NuBus"},
366 	{PCIC_BRIDGE,		PCIS_BRIDGE_CARDBUS,	"PCI-CardBus"},
367 	{PCIC_BRIDGE,		PCIS_BRIDGE_RACEWAY,	"PCI-RACEway"},
368 	{PCIC_SIMPLECOMM,	-1,			"simple comms"},
369 	{PCIC_SIMPLECOMM,	PCIS_SIMPLECOMM_UART,	"UART"},	/* could detect 16550 */
370 	{PCIC_SIMPLECOMM,	PCIS_SIMPLECOMM_PAR,	"parallel port"},
371 	{PCIC_SIMPLECOMM,	PCIS_SIMPLECOMM_MULSER,	"multiport serial"},
372 	{PCIC_SIMPLECOMM,	PCIS_SIMPLECOMM_MODEM,	"generic modem"},
373 	{PCIC_BASEPERIPH,	-1,			"base peripheral"},
374 	{PCIC_BASEPERIPH,	PCIS_BASEPERIPH_PIC,	"interrupt controller"},
375 	{PCIC_BASEPERIPH,	PCIS_BASEPERIPH_DMA,	"DMA controller"},
376 	{PCIC_BASEPERIPH,	PCIS_BASEPERIPH_TIMER,	"timer"},
377 	{PCIC_BASEPERIPH,	PCIS_BASEPERIPH_RTC,	"realtime clock"},
378 	{PCIC_BASEPERIPH,	PCIS_BASEPERIPH_PCIHOT,	"PCI hot-plug controller"},
379 	{PCIC_BASEPERIPH,	PCIS_BASEPERIPH_SDHC,	"SD host controller"},
380 	{PCIC_INPUTDEV,		-1,			"input device"},
381 	{PCIC_INPUTDEV,		PCIS_INPUTDEV_KEYBOARD,	"keyboard"},
382 	{PCIC_INPUTDEV,		PCIS_INPUTDEV_DIGITIZER,"digitizer"},
383 	{PCIC_INPUTDEV,		PCIS_INPUTDEV_MOUSE,	"mouse"},
384 	{PCIC_INPUTDEV,		PCIS_INPUTDEV_SCANNER,	"scanner"},
385 	{PCIC_INPUTDEV,		PCIS_INPUTDEV_GAMEPORT,	"gameport"},
386 	{PCIC_DOCKING,		-1,			"docking station"},
387 	{PCIC_PROCESSOR,	-1,			"processor"},
388 	{PCIC_SERIALBUS,	-1,			"serial bus"},
389 	{PCIC_SERIALBUS,	PCIS_SERIALBUS_FW,	"FireWire"},
390 	{PCIC_SERIALBUS,	PCIS_SERIALBUS_ACCESS,	"AccessBus"},
391 	{PCIC_SERIALBUS,	PCIS_SERIALBUS_SSA,	"SSA"},
392 	{PCIC_SERIALBUS,	PCIS_SERIALBUS_USB,	"USB"},
393 	{PCIC_SERIALBUS,	PCIS_SERIALBUS_FC,	"Fibre Channel"},
394 	{PCIC_SERIALBUS,	PCIS_SERIALBUS_SMBUS,	"SMBus"},
395 	{PCIC_WIRELESS,		-1,			"wireless controller"},
396 	{PCIC_WIRELESS,		PCIS_WIRELESS_IRDA,	"iRDA"},
397 	{PCIC_WIRELESS,		PCIS_WIRELESS_IR,	"IR"},
398 	{PCIC_WIRELESS,		PCIS_WIRELESS_RF,	"RF"},
399 	{PCIC_INTELLIIO,	-1,			"intelligent I/O controller"},
400 	{PCIC_INTELLIIO,	PCIS_INTELLIIO_I2O,	"I2O"},
401 	{PCIC_SATCOM,		-1,			"satellite communication"},
402 	{PCIC_SATCOM,		PCIS_SATCOM_TV,		"sat TV"},
403 	{PCIC_SATCOM,		PCIS_SATCOM_AUDIO,	"sat audio"},
404 	{PCIC_SATCOM,		PCIS_SATCOM_VOICE,	"sat voice"},
405 	{PCIC_SATCOM,		PCIS_SATCOM_DATA,	"sat data"},
406 	{PCIC_CRYPTO,		-1,			"encrypt/decrypt"},
407 	{PCIC_CRYPTO,		PCIS_CRYPTO_NETCOMP,	"network/computer crypto"},
408 	{PCIC_CRYPTO,		PCIS_CRYPTO_NETCOMP,	"entertainment crypto"},
409 	{PCIC_DASP,		-1,			"dasp"},
410 	{PCIC_DASP,		PCIS_DASP_DPIO,		"DPIO module"},
411 	{0, 0,		NULL}
412 };
413 
414 static const char *
415 guess_class(struct pci_conf *p)
416 {
417 	int	i;
418 
419 	for (i = 0; pci_nomatch_tab[i].desc != NULL; i++) {
420 		if (pci_nomatch_tab[i].class == p->pc_class)
421 			return(pci_nomatch_tab[i].desc);
422 	}
423 	return(NULL);
424 }
425 
426 static const char *
427 guess_subclass(struct pci_conf *p)
428 {
429 	int	i;
430 
431 	for (i = 0; pci_nomatch_tab[i].desc != NULL; i++) {
432 		if ((pci_nomatch_tab[i].class == p->pc_class) &&
433 		    (pci_nomatch_tab[i].subclass == p->pc_subclass))
434 			return(pci_nomatch_tab[i].desc);
435 	}
436 	return(NULL);
437 }
438 
439 static int
440 load_vendors(void)
441 {
442 	const char *dbf;
443 	FILE *db;
444 	struct pci_vendor_info *cv;
445 	struct pci_device_info *cd;
446 	char buf[1024], str[1024];
447 	char *ch;
448 	int id, error;
449 
450 	/*
451 	 * Locate the database and initialise.
452 	 */
453 	TAILQ_INIT(&pci_vendors);
454 	if ((dbf = getenv("PCICONF_VENDOR_DATABASE")) == NULL)
455 		dbf = _PATH_PCIVDB;
456 	if ((db = fopen(dbf, "r")) == NULL)
457 		return(1);
458 	cv = NULL;
459 	cd = NULL;
460 	error = 0;
461 
462 	/*
463 	 * Scan input lines from the database
464 	 */
465 	for (;;) {
466 		if (fgets(buf, sizeof(buf), db) == NULL)
467 			break;
468 
469 		if ((ch = strchr(buf, '#')) != NULL)
470 			*ch = '\0';
471 		ch = strchr(buf, '\0') - 1;
472 		while (ch > buf && isspace(*ch))
473 			*ch-- = '\0';
474 		if (ch <= buf)
475 			continue;
476 
477 		/* Can't handle subvendor / subdevice entries yet */
478 		if (buf[0] == '\t' && buf[1] == '\t')
479 			continue;
480 
481 		/* Check for vendor entry */
482 		if (buf[0] != '\t' && sscanf(buf, "%04x %[^\n]", &id, str) == 2) {
483 			if ((id == 0) || (strlen(str) < 1))
484 				continue;
485 			if ((cv = malloc(sizeof(struct pci_vendor_info))) == NULL) {
486 				warn("allocating vendor entry");
487 				error = 1;
488 				break;
489 			}
490 			if ((cv->desc = strdup(str)) == NULL) {
491 				free(cv);
492 				warn("allocating vendor description");
493 				error = 1;
494 				break;
495 			}
496 			cv->id = id;
497 			TAILQ_INIT(&cv->devs);
498 			TAILQ_INSERT_TAIL(&pci_vendors, cv, link);
499 			continue;
500 		}
501 
502 		/* Check for device entry */
503 		if (buf[0] == '\t' && sscanf(buf + 1, "%04x %[^\n]", &id, str) == 2) {
504 			if ((id == 0) || (strlen(str) < 1))
505 				continue;
506 			if (cv == NULL) {
507 				warnx("device entry with no vendor!");
508 				continue;
509 			}
510 			if ((cd = malloc(sizeof(struct pci_device_info))) == NULL) {
511 				warn("allocating device entry");
512 				error = 1;
513 				break;
514 			}
515 			if ((cd->desc = strdup(str)) == NULL) {
516 				free(cd);
517 				warn("allocating device description");
518 				error = 1;
519 				break;
520 			}
521 			cd->id = id;
522 			TAILQ_INSERT_TAIL(&cv->devs, cd, link);
523 			continue;
524 		}
525 
526 		/* It's a comment or junk, ignore it */
527 	}
528 	if (ferror(db))
529 		error = 1;
530 	fclose(db);
531 
532 	return(error);
533 }
534 
535 uint32_t
536 read_config(int fd, struct pcisel *sel, long reg, int width)
537 {
538 	struct pci_io pi;
539 
540 	pi.pi_sel = *sel;
541 	pi.pi_reg = reg;
542 	pi.pi_width = width;
543 
544 	if (ioctl(fd, PCIOCREAD, &pi) < 0)
545 		err(1, "ioctl(PCIOCREAD)");
546 
547 	return (pi.pi_data);
548 }
549 
550 static struct pcisel
551 getsel(const char *str)
552 {
553 	char *ep = strchr(str, '@');
554 	char *epbase;
555 	struct pcisel sel;
556 	unsigned long selarr[4];
557 	int i;
558 
559 	if (ep == NULL)
560 		ep = (char *)str;
561 	else
562 		ep++;
563 
564 	epbase = ep;
565 
566 	if (strncmp(ep, "pci", 3) == 0) {
567 		ep += 3;
568 		i = 0;
569 		do {
570 			selarr[i++] = strtoul(ep, &ep, 10);
571 		} while ((*ep == ':' || *ep == '.') && *++ep != '\0' && i < 4);
572 
573 		if (i > 2)
574 			sel.pc_func = selarr[--i];
575 		else
576 			sel.pc_func = 0;
577 		sel.pc_dev = selarr[--i];
578 		sel.pc_bus = selarr[--i];
579 		if (i > 0)
580 			sel.pc_domain = selarr[--i];
581 		else
582 			sel.pc_domain = 0;
583 	}
584 	if (*ep != '\x0' || ep == epbase)
585 		errx(1, "cannot parse selector %s", str);
586 	return sel;
587 }
588 
589 static void
590 readone(int fd, struct pcisel *sel, long reg, int width)
591 {
592 
593 	printf("%0*x", width*2, read_config(fd, sel, reg, width));
594 }
595 
596 static void
597 readit(const char *name, const char *reg, int width)
598 {
599 	long rstart;
600 	long rend;
601 	long r;
602 	char *end;
603 	int i;
604 	int fd;
605 	struct pcisel sel;
606 
607 	fd = open(_PATH_DEVPCI, O_RDWR, 0);
608 	if (fd < 0)
609 		err(1, "%s", _PATH_DEVPCI);
610 
611 	rend = rstart = strtol(reg, &end, 0);
612 	if (end && *end == ':') {
613 		end++;
614 		rend = strtol(end, (char **) 0, 0);
615 	}
616 	sel = getsel(name);
617 	for (i = 1, r = rstart; r <= rend; i++, r += width) {
618 		readone(fd, &sel, r, width);
619 		if (i && !(i % 8))
620 			putchar(' ');
621 		putchar(i % (16/width) ? ' ' : '\n');
622 	}
623 	if (i % (16/width) != 1)
624 		putchar('\n');
625 	close(fd);
626 }
627 
628 static void
629 writeit(const char *name, const char *reg, const char *data, int width)
630 {
631 	int fd;
632 	struct pci_io pi;
633 
634 	pi.pi_sel = getsel(name);
635 	pi.pi_reg = strtoul(reg, (char **)0, 0); /* XXX error check */
636 	pi.pi_width = width;
637 	pi.pi_data = strtoul(data, (char **)0, 0); /* XXX error check */
638 
639 	fd = open(_PATH_DEVPCI, O_RDWR, 0);
640 	if (fd < 0)
641 		err(1, "%s", _PATH_DEVPCI);
642 
643 	if (ioctl(fd, PCIOCWRITE, &pi) < 0)
644 		err(1, "ioctl(PCIOCWRITE)");
645 }
646 
647 static void
648 chkattached(const char *name)
649 {
650 	int fd;
651 	struct pci_io pi;
652 
653 	pi.pi_sel = getsel(name);
654 
655 	fd = open(_PATH_DEVPCI, O_RDWR, 0);
656 	if (fd < 0)
657 		err(1, "%s", _PATH_DEVPCI);
658 
659 	if (ioctl(fd, PCIOCATTACHED, &pi) < 0)
660 		err(1, "ioctl(PCIOCATTACHED)");
661 
662 	exitstatus = pi.pi_data ? 0 : 2; /* exit(2), if NOT attached */
663 	printf("%s: %s%s\n", name, pi.pi_data == 0 ? "not " : "", "attached");
664 }
665