pciconf.c revision 188018
1130860Srik/*
2130860Srik * Copyright 1996 Massachusetts Institute of Technology
3123159Simp *
423462Sjmg * Permission to use, copy, modify, and distribute this software and
5123159Simp * its documentation for any purpose and without fee is hereby
6123159Simp * granted, provided that both the above copyright notice and this
7123159Simp * permission notice appear in all copies, that both the above
8123159Simp * copyright notice and this permission notice appear in all
9123159Simp * supporting documentation, and that the name of M.I.T. not be used
10123159Simp * in advertising or publicity pertaining to distribution of the
11123159Simp * software without specific, written prior permission.  M.I.T. makes
12123159Simp * no representations about the suitability of this software for any
13130860Srik * purpose.  It is provided "as is" without express or implied
1450476Speter * warranty.
1523462Sjmg *
16358804Semaste * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
17123943Sru * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
1879538Sru * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
1920782Smpp * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
2060442Sphantom * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21148145Strhodes * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2268854Sru * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23151046Strhodes * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24152569Sru * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25148220Strhodes * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26148145Strhodes * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27123159Simp * SUCH DAMAGE.
28148145Strhodes */
29152569Sru
30151046Strhodes#ifndef lint
31151046Strhodesstatic const char rcsid[] =
32148145Strhodes  "$FreeBSD: head/usr.sbin/pciconf/pciconf.c 188018 2009-02-02 19:54:16Z jhb $";
33148145Strhodes#endif /* not lint */
34148145Strhodes
35148145Strhodes#include <sys/types.h>
3620782Smpp#include <sys/fcntl.h>
37123159Simp
38123159Simp#include <ctype.h>
39123159Simp#include <err.h>
40123159Simp#include <inttypes.h>
41123159Simp#include <stdlib.h>
4284877Syokota#include <stdio.h>
4384877Syokota#include <string.h>
4484877Syokota#include <unistd.h>
4584877Syokota#include <sys/pciio.h>
4684877Syokota#include <sys/queue.h>
4784877Syokota
48358804Semaste#include <dev/pci/pcireg.h>
49358804Semaste
50358804Semaste#include "pathnames.h"
51358804Semaste#include "pciconf.h"
52358804Semaste
53358804Semastestruct pci_device_info
54123159Simp{
55123159Simp    TAILQ_ENTRY(pci_device_info)	link;
56123159Simp    int					id;
57123159Simp    char				*desc;
58123159Simp};
59130582Sru
60123159Simpstruct pci_vendor_info
61130582Sru{
62130582Sru    TAILQ_ENTRY(pci_vendor_info)	link;
63130582Sru    TAILQ_HEAD(,pci_device_info)	devs;
64123159Simp    int					id;
65123159Simp    char				*desc;
66123159Simp};
67123159Simp
68126499SbruefferTAILQ_HEAD(,pci_vendor_info)	pci_vendors;
69126499Sbrueffer
70123159Simpstatic void list_bars(int fd, struct pci_conf *p);
71123159Simpstatic void list_devs(int verbose, int bars, int caps);
7284877Syokotastatic void list_verbose(struct pci_conf *p);
73126897Sbruefferstatic const char *guess_class(struct pci_conf *p);
7484877Syokotastatic const char *guess_subclass(struct pci_conf *p);
75126499Sbruefferstatic int load_vendors(void);
76126897Sbruefferstatic void readit(const char *, const char *, int);
77123159Simpstatic void writeit(const char *, const char *, const char *, int);
78123159Simpstatic void chkattached(const char *, int);
7920782Smpp
80123159Simpstatic int exitstatus = 0;
81123159Simp
824910Swollmanstatic void
83123159Simpusage(void)
844910Swollman{
85123159Simp	fprintf(stderr, "%s\n%s\n%s\n%s\n",
864910Swollman		"usage: pciconf -l [-bcv]",
8770466Sru		"       pciconf -a selector",
8820782Smpp		"       pciconf -r [-b | -h] selector addr[:addr2]",
8976175Sschweikh		"       pciconf -w [-b | -h] selector addr value");
90123159Simp	exit (1);
91123159Simp}
92126499Sbrueffer
93123159Simpint
94126499Sbrueffermain(int argc, char **argv)
95126499Sbrueffer{
9620782Smpp	int c;
97126499Sbrueffer	int listmode, readmode, writemode, attachedmode, bars, caps, verbose;
98123159Simp	int byte, isshort;
99123159Simp
100123159Simp	listmode = readmode = writemode = attachedmode = bars = caps = verbose = byte = isshort = 0;
101123159Simp
102131752Ssimon	while ((c = getopt(argc, argv, "abchlrwv")) != -1) {
103131752Ssimon		switch(c) {
104131752Ssimon		case 'a':
105131752Ssimon			attachedmode = 1;
106131752Ssimon			break;
107131752Ssimon
108131752Ssimon		case 'b':
109131752Ssimon			bars = 1;
110131752Ssimon			byte = 1;
111131752Ssimon			break;
112131752Ssimon
113131752Ssimon		case 'c':
114131752Ssimon			caps = 1;
115131752Ssimon			break;
116131752Ssimon
117131752Ssimon		case 'h':
118131752Ssimon			isshort = 1;
119131752Ssimon			break;
120131752Ssimon
121123159Simp		case 'l':
122130860Srik			listmode = 1;
123130860Srik			break;
124130582Sru
125130582Sru		case 'r':
126130582Sru			readmode = 1;
127123159Simp			break;
128123159Simp
12976175Sschweikh		case 'w':
130123159Simp			writemode = 1;
131123159Simp			break;
132130860Srik
133130860Srik		case 'v':
134130860Srik			verbose = 1;
135126499Sbrueffer			break;
136123159Simp
137123159Simp		default:
138123159Simp			usage();
139123159Simp		}
140123159Simp	}
141123159Simp
142123159Simp	if ((listmode && optind != argc)
143123159Simp	    || (writemode && optind + 3 != argc)
144123159Simp	    || (readmode && optind + 2 != argc)
145123159Simp	    || (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		    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);
159	} else {
160		usage();
161	}
162
163	return exitstatus;
164}
165
166static void
167list_devs(int verbose, int bars, int caps)
168{
169	int fd;
170	struct pci_conf_io pc;
171	struct pci_conf conf[255], *p;
172	int none_count = 0;
173
174	if (verbose)
175		load_vendors();
176
177	fd = open(_PATH_DEVPCI, caps ? O_RDWR : O_RDONLY, 0);
178	if (fd < 0)
179		err(1, "%s", _PATH_DEVPCI);
180
181	bzero(&pc, sizeof(struct pci_conf_io));
182	pc.match_buf_len = sizeof(conf);
183	pc.matches = conf;
184
185	do {
186		if (ioctl(fd, PCIOCGETCONF, &pc) == -1)
187			err(1, "ioctl(PCIOCGETCONF)");
188
189		/*
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
195		 * not be desireable.
196		 */
197		if (pc.status == PCI_GETCONF_LIST_CHANGED) {
198			warnx("PCI device list changed, please try again");
199			exitstatus = 1;
200			close(fd);
201			return;
202		} else if (pc.status ==  PCI_GETCONF_ERROR) {
203			warnx("error returned from PCIOCGETCONF ioctl");
204			exitstatus = 1;
205			close(fd);
206			return;
207		}
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 :
212			    "none",
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);
221			if (verbose)
222				list_verbose(p);
223			if (bars)
224				list_bars(fd, p);
225			if (caps)
226				list_caps(fd, p);
227		}
228	} while (pc.status == PCI_GETCONF_MORE_DEVS);
229
230	close(fd);
231}
232
233static void
234list_bars(int fd, struct pci_conf *p)
235{
236	struct pci_bar_io bar;
237	uint64_t base;
238	const char *type;
239	int i, range, max;
240
241	switch (p->pc_hdr & PCIM_HDRTYPE) {
242	case PCIM_HDRTYPE_NORMAL:
243		max = PCIR_MAX_BAR_0;
244		break;
245	case PCIM_HDRTYPE_BRIDGE:
246		max = PCIR_MAX_BAR_1;
247		break;
248	case PCIM_HDRTYPE_CARDBUS:
249		max = PCIR_MAX_BAR_2;
250		break;
251	default:
252		return;
253	}
254
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)
259			continue;
260		if (PCI_BAR_IO(bar.pbi_base)) {
261			type = "I/O Port";
262			range = 32;
263			base = bar.pbi_base & PCIM_BAR_IO_BASE;
264		} else {
265			if (bar.pbi_base & PCIM_BAR_MEM_PREFETCH)
266				type = "Prefetchable Memory";
267			else
268				type = "Memory";
269			switch (bar.pbi_base & PCIM_BAR_MEM_TYPE) {
270			case PCIM_BAR_MEM_32:
271				range = 32;
272				break;
273			case PCIM_BAR_MEM_1MB:
274				range = 20;
275				break;
276			case PCIM_BAR_MEM_64:
277				range = 64;
278				break;
279			default:
280				range = -1;
281			}
282			base = bar.pbi_base & ~((uint64_t)0xf);
283		}
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");
288	}
289}
290
291static void
292list_verbose(struct pci_conf *p)
293{
294	struct pci_vendor_info	*vi;
295	struct pci_device_info	*di;
296	const char *dp;
297
298	TAILQ_FOREACH(vi, &pci_vendors, link) {
299		if (vi->id == p->pc_vendor) {
300			printf("    vendor     = '%s'\n", vi->desc);
301			break;
302		}
303	}
304	if (vi == NULL) {
305		di = NULL;
306	} else {
307		TAILQ_FOREACH(di, &vi->devs, link) {
308			if (di->id == p->pc_device) {
309				printf("    device     = '%s'\n", di->desc);
310				break;
311			}
312		}
313	}
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);
318}
319
320/*
321 * This is a direct cut-and-paste from the table in sys/dev/pci/pci.c.
322 */
323static struct
324{
325	int	class;
326	int	subclass;
327	const char *desc;
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"},
412	{0, 0,		NULL}
413};
414
415static const char *
416guess_class(struct pci_conf *p)
417{
418	int	i;
419
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);
423	}
424	return(NULL);
425}
426
427static const char *
428guess_subclass(struct pci_conf *p)
429{
430	int	i;
431
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);
436	}
437	return(NULL);
438}
439
440static int
441load_vendors(void)
442{
443	const char *dbf;
444	FILE *db;
445	struct pci_vendor_info *cv;
446	struct pci_device_info *cd;
447	char buf[1024], str[1024];
448	char *ch;
449	int id, error;
450
451	/*
452	 * Locate the database and initialise.
453	 */
454	TAILQ_INIT(&pci_vendors);
455	if ((dbf = getenv("PCICONF_VENDOR_DATABASE")) == NULL)
456		dbf = _PATH_PCIVDB;
457	if ((db = fopen(dbf, "r")) == NULL)
458		return(1);
459	cv = NULL;
460	cd = NULL;
461	error = 0;
462
463	/*
464	 * Scan input lines from the database
465	 */
466	for (;;) {
467		if (fgets(buf, sizeof(buf), db) == NULL)
468			break;
469
470		if ((ch = strchr(buf, '#')) != NULL)
471			*ch = '\0';
472		ch = strchr(buf, '\0') - 1;
473		while (ch > buf && isspace(*ch))
474			*ch-- = '\0';
475		if (ch <= buf)
476			continue;
477
478		/* Can't handle subvendor / subdevice entries yet */
479		if (buf[0] == '\t' && buf[1] == '\t')
480			continue;
481
482		/* Check for vendor entry */
483		if (buf[0] != '\t' && sscanf(buf, "%04x %[^\n]", &id, str) == 2) {
484			if ((id == 0) || (strlen(str) < 1))
485				continue;
486			if ((cv = malloc(sizeof(struct pci_vendor_info))) == NULL) {
487				warn("allocating vendor entry");
488				error = 1;
489				break;
490			}
491			if ((cv->desc = strdup(str)) == NULL) {
492				free(cv);
493				warn("allocating vendor description");
494				error = 1;
495				break;
496			}
497			cv->id = id;
498			TAILQ_INIT(&cv->devs);
499			TAILQ_INSERT_TAIL(&pci_vendors, cv, link);
500			continue;
501		}
502
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))
506				continue;
507			if (cv == NULL) {
508				warnx("device entry with no vendor!");
509				continue;
510			}
511			if ((cd = malloc(sizeof(struct pci_device_info))) == NULL) {
512				warn("allocating device entry");
513				error = 1;
514				break;
515			}
516			if ((cd->desc = strdup(str)) == NULL) {
517				free(cd);
518				warn("allocating device description");
519				error = 1;
520				break;
521			}
522			cd->id = id;
523			TAILQ_INSERT_TAIL(&cv->devs, cd, link);
524			continue;
525		}
526
527		/* It's a comment or junk, ignore it */
528	}
529	if (ferror(db))
530		error = 1;
531	fclose(db);
532
533	return(error);
534}
535
536uint32_t
537read_config(int fd, struct pcisel *sel, long reg, int width)
538{
539	struct pci_io pi;
540
541	pi.pi_sel = *sel;
542	pi.pi_reg = reg;
543	pi.pi_width = width;
544
545	if (ioctl(fd, PCIOCREAD, &pi) < 0)
546		err(1, "ioctl(PCIOCREAD)");
547
548	return (pi.pi_data);
549}
550
551static struct pcisel
552getsel(const char *str)
553{
554	char *ep = strchr(str, '@');
555	char *epbase;
556	struct pcisel sel;
557	unsigned long selarr[4];
558	int i;
559
560	if (ep == NULL)
561		ep = (char *)str;
562	else
563		ep++;
564
565	epbase = ep;
566
567	if (strncmp(ep, "pci", 3) == 0) {
568		ep += 3;
569		i = 0;
570		do {
571			selarr[i++] = strtoul(ep, &ep, 10);
572		} while ((*ep == ':' || *ep == '.') && *++ep != '\0' && i < 4);
573
574		if (i > 2)
575			sel.pc_func = selarr[--i];
576		else
577			sel.pc_func = 0;
578		sel.pc_dev = selarr[--i];
579		sel.pc_bus = selarr[--i];
580		if (i > 0)
581			sel.pc_domain = selarr[--i];
582		else
583			sel.pc_domain = 0;
584	}
585	if (*ep != '\x0' || ep == epbase)
586		errx(1, "cannot parse selector %s", str);
587	return sel;
588}
589
590static void
591readone(int fd, struct pcisel *sel, long reg, int width)
592{
593
594	printf("%0*x", width*2, read_config(fd, sel, reg, width));
595}
596
597static void
598readit(const char *name, const char *reg, int width)
599{
600	long rstart;
601	long rend;
602	long r;
603	char *end;
604	int i;
605	int fd;
606	struct pcisel sel;
607
608	fd = open(_PATH_DEVPCI, O_RDWR, 0);
609	if (fd < 0)
610		err(1, "%s", _PATH_DEVPCI);
611
612	rend = rstart = strtol(reg, &end, 0);
613	if (end && *end == ':') {
614		end++;
615		rend = strtol(end, (char **) 0, 0);
616	}
617	sel = getsel(name);
618	for (i = 1, r = rstart; r <= rend; i++, r += width) {
619		readone(fd, &sel, r, width);
620		if (i && !(i % 8))
621			putchar(' ');
622		putchar(i % (16/width) ? ' ' : '\n');
623	}
624	if (i % (16/width) != 1)
625		putchar('\n');
626	close(fd);
627}
628
629static void
630writeit(const char *name, const char *reg, const char *data, int width)
631{
632	int fd;
633	struct pci_io pi;
634
635	pi.pi_sel = getsel(name);
636	pi.pi_reg = strtoul(reg, (char **)0, 0); /* XXX error check */
637	pi.pi_width = width;
638	pi.pi_data = strtoul(data, (char **)0, 0); /* XXX error check */
639
640	fd = open(_PATH_DEVPCI, O_RDWR, 0);
641	if (fd < 0)
642		err(1, "%s", _PATH_DEVPCI);
643
644	if (ioctl(fd, PCIOCWRITE, &pi) < 0)
645		err(1, "ioctl(PCIOCWRITE)");
646}
647
648static void
649chkattached(const char *name, int width)
650{
651	int fd;
652	struct pci_io pi;
653
654	pi.pi_sel = getsel(name);
655	pi.pi_reg = 0;
656	pi.pi_width = width;
657	pi.pi_data = 0;
658
659	fd = open(_PATH_DEVPCI, O_RDWR, 0);
660	if (fd < 0)
661		err(1, "%s", _PATH_DEVPCI);
662
663	if (ioctl(fd, PCIOCATTACHED, &pi) < 0)
664		err(1, "ioctl(PCIOCATTACHED)");
665
666	exitstatus = pi.pi_data ? 0 : 2; /* exit(2), if NOT attached */
667	printf("%s: %s%s\n", name, pi.pi_data == 0 ? "not " : "", "attached");
668}
669