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