/*
* Copyright 2001 Wasabi Systems, Inc.
* All rights reserved.
*
* Written by Jason R. Thorpe for Wasabi Systems, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed for the NetBSD Project by
* Wasabi Systems, Inc.
* 4. The name of Wasabi Systems, Inc. may not be used to endorse
* or promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* pcictl(8) -- a program to manipulate the PCI bus
*/
/* Must have at least: device command */
if (argc < 3)
usage();
/* Skip program name, get and skip device name, get command. */
dvname = argv[1];
cmdname = argv[2];
argv += 2;
argc -= 2;
/* Look up and call the command. */
for (i = 0; commands[i].cmd_name != NULL; i++)
if (strcmp(cmdname, commands[i].cmd_name) == 0)
break;
if (commands[i].cmd_name == NULL)
errx(EXIT_FAILURE, "unknown command: %s", cmdname);
/* Open the device. */
if ((strchr(dvname, '/') == NULL) &&
(snprintf(dvname_store, sizeof(dvname_store), _PATH_DEV "%s",
dvname) < (int)sizeof(dvname_store)))
dvname = dvname_store;
pcifd = open(dvname, commands[i].open_flags);
if (pcifd < 0)
err(EXIT_FAILURE, "%s", dvname);
/* Make sure the device is a PCI bus. */
if (ioctl(pcifd, PCI_IOC_BUSINFO, &pci_businfo) != 0)
errx(EXIT_FAILURE, "%s: not a PCI bus device", dvname);
fprintf(stderr, " Available commands:\n");
for (i = 0; commands[i].cmd_name != NULL; i++)
fprintf(stderr, "\t%s %s\n", commands[i].cmd_name,
commands[i].arg_names);
exit(EXIT_FAILURE);
}
static void
cmd_list(int argc, char *argv[])
{
int bus, dev, func;
int ch;
bus = -1;
dev = func = -1;
while ((ch = getopt(argc, argv, "b:d:f:Nn")) != -1) {
switch (ch) {
case 'b':
bus = parse_bdf(optarg);
break;
case 'd':
dev = parse_bdf(optarg);
break;
case 'f':
func = parse_bdf(optarg);
break;
case 'n':
print_numbers = 1;
break;
case 'N':
print_names = 1;
break;
default:
usage();
}
}
argv += optind;
argc -= optind;
if (argc != 0)
usage();
scan_pci(bus, dev, func, scan_pci_list);
}
static void
cmd_dump(int argc, char *argv[])
{
int bus, dev, func;
int ch;
bus = pci_businfo.busno;
func = 0;
dev = -1;
while ((ch = getopt(argc, argv, "b:d:f:")) != -1) {
switch (ch) {
case 'b':
bus = parse_bdf(optarg);
break;
case 'd':
dev = parse_bdf(optarg);
break;
case 'f':
func = parse_bdf(optarg);
break;
default:
usage();
}
}
argv += optind;
argc -= optind;
if (argc != 0)
usage();
if (bus == -1)
errx(EXIT_FAILURE, "dump: wildcard bus number not permitted");
if (dev == -1)
errx(EXIT_FAILURE, "dump: must specify a device number");
if (func == -1)
errx(EXIT_FAILURE, "dump: wildcard function number not permitted");
scan_pci(bus, dev, func, scan_pci_dump);
}
static void
cmd_read(int argc, char *argv[])
{
int bus, dev, func;
u_int reg;
pcireg_t value;
int ch;
bus = pci_businfo.busno;
func = 0;
dev = -1;
while ((ch = getopt(argc, argv, "b:d:f:")) != -1) {
switch (ch) {
case 'b':
bus = parse_bdf(optarg);
break;
case 'd':
dev = parse_bdf(optarg);
break;
case 'f':
func = parse_bdf(optarg);
break;
default:
usage();
}
}
argv += optind;
argc -= optind;
if (argc != 1)
usage();
reg = parse_reg(argv[0]);
if (pcibus_conf_read(pcifd, bus, dev, func, reg, &value) == -1)
err(EXIT_FAILURE, "pcibus_conf_read"
"(bus %d dev %d func %d reg %u)", bus, dev, func, reg);
if (printf("%08x\n", value) < 0)
err(EXIT_FAILURE, "printf");
}
static void
cmd_write(int argc, char *argv[])
{
int bus, dev, func;
u_int reg;
pcireg_t value;
int ch;
bus = pci_businfo.busno;
func = 0;
dev = -1;
while ((ch = getopt(argc, argv, "b:d:f:")) != -1) {
switch (ch) {
case 'b':
bus = parse_bdf(optarg);
break;
case 'd':
dev = parse_bdf(optarg);
break;
case 'f':
func = parse_bdf(optarg);
break;
default:
usage();
}
}
argv += optind;
argc -= optind;
if (argc != 2)
usage();
reg = parse_reg(argv[0]);
__CTASSERT(sizeof(value) == sizeof(u_int));
value = parse_reg(argv[1]);
if (pcibus_conf_write(pcifd, bus, dev, func, reg, value) == -1)
err(EXIT_FAILURE, "pcibus_conf_write"
"(bus %d dev %d func %d reg %u value 0x%x)",
bus, dev, func, reg, value);
}
static int
parse_bdf(const char *str)
{
long value;
char *end;
errno = 0;
value = strtoul(str, &end, 0);
if (*end != '\0')
errx(EXIT_FAILURE, "\"%s\" is not a number", str);
if ((errno == ERANGE) && (value == ULONG_MAX))
errx(EXIT_FAILURE, "out of range: %s", str);
if (UINT_MAX < value)
errx(EXIT_FAILURE, "out of range: %lu", value);
for (bus = busmin; bus <= busmax; bus++) {
for (dev = devmin; dev <= devmax; dev++) {
if (pcibus_conf_read(pcifd, bus, dev, 0,
PCI_BHLC_REG, &bhlcr) != 0)
continue;
if (funcarg == -1) {
funcmin = 0;
if (PCI_HDRTYPE_MULTIFN(bhlcr))
funcmax = 7;
else
funcmax = 0;
} else
funcmin = funcmax = funcarg;
for (func = funcmin; func <= funcmax; func++) {
if (pcibus_conf_read(pcifd, bus, dev,
func, PCI_ID_REG, &id) != 0)
continue;
/* Invalid vendor ID value? */
if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
continue;
/*
* XXX Not invalid, but we've done this
* ~forever.
*/
if (PCI_VENDOR(id) == 0)
continue;