pciconf.c revision 133249
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
31static const char rcsid[] =
32  "$FreeBSD: head/usr.sbin/pciconf/pciconf.c 133249 2004-08-07 04:28:56Z imp $";
33#endif /* not lint */
34
35#include <sys/types.h>
36#include <sys/fcntl.h>
37
38#include <err.h>
39#include <stdlib.h>
40#include <stdio.h>
41#include <string.h>
42#include <unistd.h>
43#include <sys/pciio.h>
44#include <sys/queue.h>
45
46#include <dev/pci/pcireg.h>
47
48#include "pathnames.h"
49
50struct pci_device_info
51{
52    TAILQ_ENTRY(pci_device_info)	link;
53    int					id;
54    char				*desc;
55};
56
57struct pci_vendor_info
58{
59    TAILQ_ENTRY(pci_vendor_info)	link;
60    TAILQ_HEAD(,pci_device_info)	devs;
61    int					id;
62    char				*desc;
63};
64
65TAILQ_HEAD(,pci_vendor_info)	pci_vendors;
66
67static void dobar(const char *);
68static void list_devs(int vendors);
69static void list_verbose(struct pci_conf *p);
70static char *guess_class(struct pci_conf *p);
71static char *guess_subclass(struct pci_conf *p);
72static int load_vendors(void);
73static void readit(const char *, const char *, int);
74static void writeit(const char *, const char *, const char *, int);
75static void chkattached(const char *, int);
76
77static int exitstatus = 0;
78
79static void
80usage()
81{
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");
87	exit (1);
88}
89
90int
91main(int argc, char **argv)
92{
93	int c;
94	int listmode, readmode, writemode, attachedmode, barmode, verbose;
95	int byte, isshort;
96
97	barmode = listmode = readmode = writemode = attachedmode = verbose = byte = isshort = 0;
98
99	while ((c = getopt(argc, argv, "aBbhlrwv")) != -1) {
100		switch(c) {
101		case 'a':
102			attachedmode = 1;
103			break;
104
105		case 'B':
106			barmode = 1;
107			break;
108
109		case 'b':
110			byte = 1;
111			break;
112
113		case 'h':
114			isshort = 1;
115			break;
116
117		case 'l':
118			listmode = 1;
119			break;
120
121		case 'r':
122			readmode = 1;
123			break;
124
125		case 'w':
126			writemode = 1;
127			break;
128
129		case 'v':
130			verbose = 1;
131			break;
132
133		default:
134			usage();
135		}
136	}
137
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)
143		usage();
144
145	if (listmode) {
146		list_devs(verbose);
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) {
157	} else {
158 		usage();
159	}
160
161	return exitstatus;
162}
163
164static void
165list_devs(int verbose)
166{
167	int fd;
168	struct pci_conf_io pc;
169	struct pci_conf conf[255], *p;
170	int none_count = 0;
171
172	if (verbose)
173		load_vendors();
174
175	fd = open(_PATH_DEVPCI, O_RDONLY, 0);
176	if (fd < 0)
177		err(1, "%s", _PATH_DEVPCI);
178
179	bzero(&pc, sizeof(struct pci_conf_io));
180	pc.match_buf_len = sizeof(conf);
181	pc.matches = conf;
182
183	do {
184		if (ioctl(fd, PCIOCGETCONF, &pc) == -1)
185			err(1, "ioctl(PCIOCGETCONF)");
186
187		/*
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
193		 * not be desireable.
194		 */
195		if (pc.status == PCI_GETCONF_LIST_CHANGED) {
196			warnx("PCI device list changed, please try again");
197			exitstatus = 1;
198			close(fd);
199			return;
200		} else if (pc.status ==  PCI_GETCONF_ERROR) {
201			warnx("error returned from PCIOCGETCONF ioctl");
202			exitstatus = 1;
203			close(fd);
204			return;
205		}
206		for (p = conf; p < &conf[pc.num_matches]; p++) {
207
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 :
211			       "none",
212			       (p->pd_name && *p->pd_name) ? (int)p->pd_unit :
213			       none_count++,
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		}
223	} while (pc.status == PCI_GETCONF_MORE_DEVS);
224
225	close(fd);
226}
227
228static void
229list_verbose(struct pci_conf *p)
230{
231	struct pci_vendor_info	*vi;
232	struct pci_device_info	*di;
233	char *dp;
234
235	TAILQ_FOREACH(vi, &pci_vendors, link) {
236		if (vi->id == p->pc_vendor) {
237			printf("    vendor   = '%s'\n", vi->desc);
238			break;
239		}
240	}
241	if (vi == NULL) {
242		di = NULL;
243	} else {
244		TAILQ_FOREACH(di, &vi->devs, link) {
245			if (di->id == p->pc_device) {
246				printf("    device   = '%s'\n", di->desc);
247				break;
248			}
249		}
250	}
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);
255}
256
257/*
258 * This is a direct cut-and-paste from the table in sys/dev/pci/pci.c.
259 */
260static struct
261{
262	int	class;
263	int	subclass;
264	char	*desc;
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"},
320	{0, 0,		NULL}
321};
322
323static char *
324guess_class(struct pci_conf *p)
325{
326	int	i;
327
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);
331	}
332	return(NULL);
333}
334
335static char *
336guess_subclass(struct pci_conf *p)
337{
338	int	i;
339
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);
344	}
345	return(NULL);
346}
347
348static int
349load_vendors(void)
350{
351	char *dbf;
352	FILE *db;
353	struct pci_vendor_info *cv;
354	struct pci_device_info *cd;
355	char buf[100], str[100];
356	int id, error;
357
358	/*
359	 * Locate the database and initialise.
360	 */
361	TAILQ_INIT(&pci_vendors);
362	if ((dbf = getenv("PCICONF_VENDOR_DATABASE")) == NULL)
363		dbf = _PATH_PCIVDB;
364	if ((db = fopen(dbf, "r")) == NULL)
365		return(1);
366	cv = NULL;
367	cd = NULL;
368	error = 0;
369
370	/*
371	 * Scan input lines from the database
372	 */
373	for (;;) {
374		if (fgets(buf, sizeof(buf), db) == NULL)
375			break;
376
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))
380				continue;
381			if ((cv = malloc(sizeof(struct pci_vendor_info))) == NULL) {
382				warn("allocating vendor entry");
383				error = 1;
384				break;
385			}
386			if ((cv->desc = strdup(str)) == NULL) {
387				free(cv);
388				warn("allocating vendor description");
389				error = 1;
390				break;
391			}
392			cv->id = id;
393			TAILQ_INIT(&cv->devs);
394			TAILQ_INSERT_TAIL(&pci_vendors, cv, link);
395			continue;
396		}
397
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))
401				continue;
402			if (cv == NULL) {
403				warnx("device entry with no vendor!");
404				continue;
405			}
406			if ((cd = malloc(sizeof(struct pci_device_info))) == NULL) {
407				warn("allocating device entry");
408				error = 1;
409				break;
410			}
411			if ((cd->desc = strdup(str)) == NULL) {
412				free(cd);
413				warn("allocating device description");
414				error = 1;
415				break;
416			}
417			cd->id = id;
418			TAILQ_INSERT_TAIL(&cv->devs, cd, link);
419			continue;
420		}
421
422		/* It's a comment or junk, ignore it */
423	}
424	if (ferror(db))
425		error = 1;
426	fclose(db);
427
428	return(error);
429}
430
431
432static struct pcisel
433getsel(const char *str)
434{
435	char *ep = strchr(str, '@');
436	char *epbase;
437	struct pcisel sel;
438
439	if (ep == NULL)
440		ep = (char *)str;
441	else
442		ep++;
443
444	epbase = ep;
445
446	if (strncmp(ep, "pci", 3) == 0) {
447		ep += 3;
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 != ':') {
453			sel.pc_func = 0;
454		} else {
455			ep++;
456			sel.pc_func = strtoul(ep, &ep, 0);
457		}
458		if (*ep == ':')
459			ep++;
460	}
461	if (*ep != '\x0' || ep == epbase)
462		errx(1, "cannot parse selector %s", str);
463	return sel;
464}
465
466static void
467readone(int fd, struct pcisel *sel, long reg, int width)
468{
469	struct pci_io pi;
470
471	pi.pi_sel = *sel;
472	pi.pi_reg = reg;
473	pi.pi_width = width;
474
475	if (ioctl(fd, PCIOCREAD, &pi) < 0)
476		err(1, "ioctl(PCIOCREAD)");
477
478	printf("%0*x", width*2, pi.pi_data);
479}
480
481static void
482dobar(const char *name)
483{
484#define NBAR 6
485	long rstart = 0x10;
486	long rend = 0x24;
487	long r;
488	char *end;
489	int i;
490	int fd;
491	uint32_t bars[NBAR];
492	struct pcisel sel;
493	struct pci_io pi;
494
495
496	fd = open(_PATH_DEVPCI, O_RDWR, 0);
497	if (fd < 0)
498		err(1, "%s", _PATH_DEVPCI);
499
500	/*
501	 * Read in the bars
502	 */
503	sel = getsel(name);
504	for (i = 0, r = rstart; r <= rend; i++, r += 4) {
505		pi.pi_sel = sel;
506		pi.pi_reg = r;
507		pi.pi_width = 4;
508		if (ioctl(fd, PCIOCREAD, &pi) < 0)
509			err(1, "ioctl(PCIOCREAD)");
510		bars[i++] = pi.pi_data;
511	}
512
513	/*
514	 * Print the bars
515	 */
516	for (i = 0; i < NBAR; i++) {
517		if (bars[i] == 0)
518			next;
519		if (bars[i] & 1) {
520			/* I/O bar */
521		} else {
522			/* Memory bar */
523		}
524	}
525
526	close(fd);
527}
528
529static void
530readit(const char *name, const char *reg, int width)
531{
532	long rstart;
533	long rend;
534	long r;
535	char *end;
536	int i;
537	int fd;
538	struct pcisel sel;
539
540	fd = open(_PATH_DEVPCI, O_RDWR, 0);
541	if (fd < 0)
542		err(1, "%s", _PATH_DEVPCI);
543
544	rend = rstart = strtol(reg, &end, 0);
545	if (end && *end == ':') {
546		end++;
547		rend = strtol(end, (char **) 0, 0);
548	}
549	sel = getsel(name);
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');
554	}
555	if (i % (16/width) != 1)
556		putchar('\n');
557	close(fd);
558}
559
560static void
561writeit(const char *name, const char *reg, const char *data, int width)
562{
563	int fd;
564	struct pci_io pi;
565
566	pi.pi_sel = getsel(name);
567	pi.pi_reg = strtoul(reg, (char **)0, 0); /* XXX error check */
568	pi.pi_width = width;
569	pi.pi_data = strtoul(data, (char **)0, 0); /* XXX error check */
570
571	fd = open(_PATH_DEVPCI, O_RDWR, 0);
572	if (fd < 0)
573		err(1, "%s", _PATH_DEVPCI);
574
575	if (ioctl(fd, PCIOCWRITE, &pi) < 0)
576		err(1, "ioctl(PCIOCWRITE)");
577}
578
579static void
580chkattached (const char *name, int width)
581{
582	int fd;
583	struct pci_io pi;
584
585	pi.pi_sel = getsel(name);
586	pi.pi_reg = 0;
587	pi.pi_width = width;
588	pi.pi_data = 0;
589
590	fd = open(_PATH_DEVPCI, O_RDWR, 0);
591	if (fd < 0)
592		err(1, "%s", _PATH_DEVPCI);
593
594	if (ioctl(fd, PCIOCATTACHED, &pi) < 0)
595		err(1, "ioctl(PCIOCATTACHED)");
596
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");
599}
600