Home | History | Annotate | Line # | Download | only in pcictl
pcictl.c revision 1.2
      1 /*	$NetBSD: pcictl.c,v 1.2 2001/09/14 17:28:36 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 = dev = func = -1;
    164 
    165 	while ((ch = getopt(argc, argv, "b:d:f:")) != -1) {
    166 		switch (ch) {
    167 		case 'b':
    168 			bus = parse_bdf(optarg);
    169 			break;
    170 		case 'd':
    171 			dev = parse_bdf(optarg);
    172 			break;
    173 		case 'f':
    174 			func = parse_bdf(optarg);
    175 			break;
    176 		default:
    177 			usage();
    178 		}
    179 	}
    180 	argv += optind;
    181 	argc -= optind;
    182 
    183 	if (argc != 0)
    184 		usage();
    185 
    186 	scan_pci(bus, dev, func, scan_pci_list);
    187 }
    188 
    189 void
    190 cmd_dump(int argc, char *argv[])
    191 {
    192 	int bus, dev, func;
    193 	int ch;
    194 
    195 	bus = pci_businfo.busno;
    196 	func = 0;
    197 	dev = -1;
    198 
    199 	while ((ch = getopt(argc, argv, "b:d:f:")) != -1) {
    200 		switch (ch) {
    201 		case 'b':
    202 			bus = parse_bdf(optarg);
    203 			break;
    204 		case 'd':
    205 			dev = parse_bdf(optarg);
    206 			break;
    207 		case 'f':
    208 			func = parse_bdf(optarg);
    209 			break;
    210 		default:
    211 			usage();
    212 		}
    213 	}
    214 	argv += optind;
    215 	argc -= optind;
    216 
    217 	if (argc != 0)
    218 		usage();
    219 
    220 	if (bus == -1)
    221 		errx(1, "dump: wildcard bus number not permitted");
    222 	if (dev == -1)
    223 		errx(1, "dump: must specify a device number");
    224 	if (func == -1)
    225 		errx(1, "dump: wildcard function number not permitted");
    226 
    227 	scan_pci(bus, dev, func, scan_pci_dump);
    228 }
    229 
    230 int
    231 parse_bdf(const char *str)
    232 {
    233 
    234 	if (strcmp(str, "all") == 0 ||
    235 	    strcmp(str, "any") == 0)
    236 		return (-1);
    237 
    238 	return (atoi(str));
    239 }
    240 
    241 void
    242 scan_pci(int busarg, int devarg, int funcarg, void (*cb)(u_int, u_int, u_int))
    243 {
    244 	u_int busmin, busmax;
    245 	u_int devmin, devmax;
    246 	u_int funcmin, funcmax;
    247 	u_int bus, dev, func;
    248 	pcireg_t id, bhlcr;
    249 
    250 	if (busarg == -1) {
    251 		busmin = 0;
    252 		busmax = 255;
    253 	} else
    254 		busmin = busmax = busarg;
    255 
    256 	if (devarg == -1) {
    257 		devmin = 0;
    258 		devmax = pci_businfo.maxdevs - 1;
    259 	} else
    260 		devmin = devmax = devarg;
    261 
    262 	for (bus = busmin; bus <= busmax; bus++) {
    263 		for (dev = devmin; dev <= devmax; dev++) {
    264 			if (pcibus_conf_read(pcifd, bus, dev, 0,
    265 			    PCI_BHLC_REG, &bhlcr) != 0)
    266 				continue;
    267 			if (funcarg == -1) {
    268 				funcmin = 0;
    269 				if (PCI_HDRTYPE_MULTIFN(bhlcr))
    270 					funcmax = 7;
    271 				else
    272 					funcmax = 0;
    273 			} else
    274 				funcmin = funcmax = funcarg;
    275 			for (func = funcmin; func <= funcmax; func++) {
    276 				if (pcibus_conf_read(pcifd, bus, dev,
    277 				    func, PCI_ID_REG, &id) != 0)
    278 					continue;
    279 
    280 				/* Invalid vendor ID value? */
    281 				if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
    282 					continue;
    283 				/*
    284 				 * XXX Not invalid, but we've done this
    285 				 * ~forever.
    286 				 */
    287 				if (PCI_VENDOR(id) == 0)
    288 					continue;
    289 
    290 				(*cb)(bus, dev, func);
    291 			}
    292 		}
    293 	}
    294 }
    295 
    296 void
    297 scan_pci_list(u_int bus, u_int dev, u_int func)
    298 {
    299 	pcireg_t id, class;
    300 	char devinfo[256];
    301 
    302 	if (pcibus_conf_read(pcifd, bus, dev, func, PCI_ID_REG, &id) != 0)
    303 		return;
    304 	if (pcibus_conf_read(pcifd, bus, dev, func, PCI_CLASS_REG, &class) != 0)
    305 		return;
    306 
    307 	pci_devinfo(id, class, 1, devinfo);
    308 
    309 	printf("%03u:%02u:%01u: %s\n", bus, dev, func, devinfo);
    310 }
    311 
    312 void
    313 scan_pci_dump(u_int bus, u_int dev, u_int func)
    314 {
    315 
    316 	pci_conf_print(pcifd, bus, dev, func);
    317 }
    318