Home | History | Annotate | Line # | Download | only in pcictl
pcictl.c revision 1.3
      1 /*	$NetBSD: pcictl.c,v 1.3 2001/09/15 18:35:00 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright 2001 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed for the NetBSD Project by
     20  *	Wasabi Systems, Inc.
     21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  *    or promote products derived from this software without specific prior
     23  *    written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 /*
     39  * pcictl(8) -- a program to manipulate the PCI bus
     40  */
     41 
     42 #include <sys/param.h>
     43 #include <sys/ioctl.h>
     44 #include <err.h>
     45 #include <errno.h>
     46 #include <fcntl.h>
     47 #include <pci.h>
     48 #include <stdio.h>
     49 #include <stdlib.h>
     50 #include <string.h>
     51 #include <unistd.h>
     52 #include <util.h>
     53 
     54 #include <dev/pci/pcireg.h>
     55 #include <dev/pci/pcidevs.h>
     56 #include <dev/pci/pciio.h>
     57 
     58 struct command {
     59 	const char *cmd_name;
     60 	const char *arg_names;
     61 	void (*cmd_func)(int, char *[]);
     62 	int open_flags;
     63 };
     64 
     65 int	main(int, char *[]);
     66 void	usage(void);
     67 
     68 int	pcifd;
     69 
     70 struct pciio_businfo pci_businfo;
     71 
     72 const	char *dvname;
     73 char	dvname_store[MAXPATHLEN];
     74 const	char *cmdname;
     75 const	char *argnames;
     76 
     77 void	cmd_list(int, char *[]);
     78 void	cmd_dump(int, char *[]);
     79 
     80 const struct command commands[] = {
     81 	{ "list",
     82 	  "[-b bus] [-d device] [-f function]",
     83 	  cmd_list,
     84 	  O_RDONLY },
     85 
     86 	{ "dump",
     87 	  "[-b bus] -d device [-f function]",
     88 	  cmd_dump,
     89 	  O_RDONLY },
     90 
     91 	{ 0 },
     92 };
     93 
     94 int	parse_bdf(const char *);
     95 
     96 void	scan_pci(int, int, int, void (*)(u_int, u_int, u_int));
     97 
     98 void	scan_pci_list(u_int, u_int, u_int);
     99 void	scan_pci_dump(u_int, u_int, u_int);
    100 
    101 int
    102 main(int argc, char *argv[])
    103 {
    104 	int i;
    105 
    106 	/* Must have at least: device command */
    107 	if (argc < 3)
    108 		usage();
    109 
    110 	/* Skip program name, get and skip device name, get command. */
    111 	dvname = argv[1];
    112 	cmdname = argv[2];
    113 	argv += 2;
    114 	argc -= 2;
    115 
    116 	/* Look up and call the command. */
    117 	for (i = 0; commands[i].cmd_name != NULL; i++)
    118 		if (strcmp(cmdname, commands[i].cmd_name) == 0)
    119 			break;
    120 	if (commands[i].cmd_name == NULL)
    121 		errx(1, "unknown command: %s", cmdname);
    122 
    123 	argnames = commands[i].arg_names;
    124 
    125 	/* Open the device. */
    126 	pcifd = opendisk(dvname, commands[i].open_flags, dvname_store,
    127 	    sizeof(dvname_store), 1);
    128 	if (pcifd == -1)
    129 		err(1, "%s", dvname);
    130 
    131 	dvname = dvname_store;
    132 
    133 	/* Make sure the device is a PCI bus. */
    134 	if (ioctl(pcifd, PCI_IOC_BUSINFO, &pci_businfo) != 0)
    135 		errx(1, "%s: not a PCI bus device\n", dvname);
    136 
    137 	(*commands[i].cmd_func)(argc, argv);
    138 	exit(0);
    139 }
    140 
    141 void
    142 usage()
    143 {
    144 	int i;
    145 
    146 	fprintf(stderr, "Usage: %s device command [arg [...]]\n",
    147 	    getprogname());
    148 
    149 	fprintf(stderr, "   Available commands:\n");
    150 	for (i = 0; commands[i].cmd_name != NULL; i++)
    151 		fprintf(stderr, "\t%s %s\n", commands[i].cmd_name,
    152 		    commands[i].arg_names);
    153 
    154 	exit(1);
    155 }
    156 
    157 void
    158 cmd_list(int argc, char *argv[])
    159 {
    160 	int bus, dev, func;
    161 	int ch;
    162 
    163 	bus = pci_businfo.busno;
    164 	dev = func = -1;
    165 
    166 	while ((ch = getopt(argc, argv, "b:d:f:")) != -1) {
    167 		switch (ch) {
    168 		case 'b':
    169 			bus = parse_bdf(optarg);
    170 			break;
    171 		case 'd':
    172 			dev = parse_bdf(optarg);
    173 			break;
    174 		case 'f':
    175 			func = parse_bdf(optarg);
    176 			break;
    177 		default:
    178 			usage();
    179 		}
    180 	}
    181 	argv += optind;
    182 	argc -= optind;
    183 
    184 	if (argc != 0)
    185 		usage();
    186 
    187 	scan_pci(bus, dev, func, scan_pci_list);
    188 }
    189 
    190 void
    191 cmd_dump(int argc, char *argv[])
    192 {
    193 	int bus, dev, func;
    194 	int ch;
    195 
    196 	bus = pci_businfo.busno;
    197 	func = 0;
    198 	dev = -1;
    199 
    200 	while ((ch = getopt(argc, argv, "b:d:f:")) != -1) {
    201 		switch (ch) {
    202 		case 'b':
    203 			bus = parse_bdf(optarg);
    204 			break;
    205 		case 'd':
    206 			dev = parse_bdf(optarg);
    207 			break;
    208 		case 'f':
    209 			func = parse_bdf(optarg);
    210 			break;
    211 		default:
    212 			usage();
    213 		}
    214 	}
    215 	argv += optind;
    216 	argc -= optind;
    217 
    218 	if (argc != 0)
    219 		usage();
    220 
    221 	if (bus == -1)
    222 		errx(1, "dump: wildcard bus number not permitted");
    223 	if (dev == -1)
    224 		errx(1, "dump: must specify a device number");
    225 	if (func == -1)
    226 		errx(1, "dump: wildcard function number not permitted");
    227 
    228 	scan_pci(bus, dev, func, scan_pci_dump);
    229 }
    230 
    231 int
    232 parse_bdf(const char *str)
    233 {
    234 
    235 	if (strcmp(str, "all") == 0 ||
    236 	    strcmp(str, "any") == 0)
    237 		return (-1);
    238 
    239 	return (atoi(str));
    240 }
    241 
    242 void
    243 scan_pci(int busarg, int devarg, int funcarg, void (*cb)(u_int, u_int, u_int))
    244 {
    245 	u_int busmin, busmax;
    246 	u_int devmin, devmax;
    247 	u_int funcmin, funcmax;
    248 	u_int bus, dev, func;
    249 	pcireg_t id, bhlcr;
    250 
    251 	if (busarg == -1) {
    252 		busmin = 0;
    253 		busmax = 255;
    254 	} else
    255 		busmin = busmax = busarg;
    256 
    257 	if (devarg == -1) {
    258 		devmin = 0;
    259 		devmax = pci_businfo.maxdevs - 1;
    260 	} else
    261 		devmin = devmax = devarg;
    262 
    263 	for (bus = busmin; bus <= busmax; bus++) {
    264 		for (dev = devmin; dev <= devmax; dev++) {
    265 			if (pcibus_conf_read(pcifd, bus, dev, 0,
    266 			    PCI_BHLC_REG, &bhlcr) != 0)
    267 				continue;
    268 			if (funcarg == -1) {
    269 				funcmin = 0;
    270 				if (PCI_HDRTYPE_MULTIFN(bhlcr))
    271 					funcmax = 7;
    272 				else
    273 					funcmax = 0;
    274 			} else
    275 				funcmin = funcmax = funcarg;
    276 			for (func = funcmin; func <= funcmax; func++) {
    277 				if (pcibus_conf_read(pcifd, bus, dev,
    278 				    func, PCI_ID_REG, &id) != 0)
    279 					continue;
    280 
    281 				/* Invalid vendor ID value? */
    282 				if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
    283 					continue;
    284 				/*
    285 				 * XXX Not invalid, but we've done this
    286 				 * ~forever.
    287 				 */
    288 				if (PCI_VENDOR(id) == 0)
    289 					continue;
    290 
    291 				(*cb)(bus, dev, func);
    292 			}
    293 		}
    294 	}
    295 }
    296 
    297 void
    298 scan_pci_list(u_int bus, u_int dev, u_int func)
    299 {
    300 	pcireg_t id, class;
    301 	char devinfo[256];
    302 
    303 	if (pcibus_conf_read(pcifd, bus, dev, func, PCI_ID_REG, &id) != 0)
    304 		return;
    305 	if (pcibus_conf_read(pcifd, bus, dev, func, PCI_CLASS_REG, &class) != 0)
    306 		return;
    307 
    308 	pci_devinfo(id, class, 1, devinfo);
    309 
    310 	printf("%03u:%02u:%01u: %s\n", bus, dev, func, devinfo);
    311 }
    312 
    313 void
    314 scan_pci_dump(u_int bus, u_int dev, u_int func)
    315 {
    316 
    317 	pci_conf_print(pcifd, bus, dev, func);
    318 }
    319