Home | History | Annotate | Line # | Download | only in scsictl
scsictl.c revision 1.3
      1  1.3  thorpej /*	$NetBSD: scsictl.c,v 1.3 1998/10/17 05:08:27 thorpej Exp $	*/
      2  1.1  thorpej 
      3  1.1  thorpej /*-
      4  1.1  thorpej  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  1.1  thorpej  * All rights reserved.
      6  1.1  thorpej  *
      7  1.1  thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  1.1  thorpej  * NASA Ames Research Center.
     10  1.1  thorpej  *
     11  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     12  1.1  thorpej  * modification, are permitted provided that the following conditions
     13  1.1  thorpej  * are met:
     14  1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     15  1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     16  1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     18  1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     19  1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     20  1.1  thorpej  *    must display the following acknowledgement:
     21  1.1  thorpej  *	This product includes software developed by the NetBSD
     22  1.1  thorpej  *	Foundation, Inc. and its contributors.
     23  1.1  thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  1.1  thorpej  *    contributors may be used to endorse or promote products derived
     25  1.1  thorpej  *    from this software without specific prior written permission.
     26  1.1  thorpej  *
     27  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  1.1  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  1.1  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  1.1  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  1.1  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  1.1  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  1.1  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  1.1  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  1.1  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  1.1  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  1.1  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     38  1.1  thorpej  */
     39  1.1  thorpej 
     40  1.1  thorpej /*
     41  1.1  thorpej  * scsictl(8) - a program to manipulate SCSI devices and busses.
     42  1.1  thorpej  */
     43  1.1  thorpej 
     44  1.1  thorpej #include <sys/param.h>
     45  1.1  thorpej #include <sys/ioctl.h>
     46  1.1  thorpej #include <sys/scsiio.h>
     47  1.1  thorpej #include <err.h>
     48  1.1  thorpej #include <errno.h>
     49  1.1  thorpej #include <fcntl.h>
     50  1.1  thorpej #include <stdio.h>
     51  1.1  thorpej #include <stdlib.h>
     52  1.1  thorpej #include <string.h>
     53  1.1  thorpej #include <unistd.h>
     54  1.1  thorpej #include <util.h>
     55  1.1  thorpej 
     56  1.1  thorpej #include <dev/scsipi/scsipi_all.h>
     57  1.1  thorpej #include <dev/scsipi/scsi_disk.h>
     58  1.1  thorpej #include <dev/scsipi/scsipiconf.h>
     59  1.1  thorpej 
     60  1.1  thorpej #include "extern.h"
     61  1.1  thorpej 
     62  1.1  thorpej struct command {
     63  1.1  thorpej 	const char *cmd_name;
     64  1.1  thorpej 	void (*cmd_func) __P((int, char *[]));
     65  1.1  thorpej };
     66  1.1  thorpej 
     67  1.1  thorpej int	main __P((int, char *[]));
     68  1.1  thorpej void	usage __P((void));
     69  1.1  thorpej 
     70  1.1  thorpej int	fd;				/* file descriptor for device */
     71  1.1  thorpej const	char *dvname;			/* device name */
     72  1.1  thorpej char	dvname_store[MAXPATHLEN];	/* for opendisk(3) */
     73  1.1  thorpej const	char *cmdname;			/* command user issued */
     74  1.1  thorpej struct	scsi_addr dvaddr;		/* SCSI device's address */
     75  1.1  thorpej 
     76  1.1  thorpej extern const char *__progname;		/* from crt0.o */
     77  1.1  thorpej 
     78  1.1  thorpej void	device_identify __P((int, char *[]));
     79  1.1  thorpej void	device_reassign __P((int, char *[]));
     80  1.1  thorpej void	device_reset __P((int, char *[]));
     81  1.1  thorpej 
     82  1.1  thorpej struct command device_commands[] = {
     83  1.1  thorpej 	{ "identify",	device_identify },
     84  1.1  thorpej 	{ "reassign",	device_reassign },
     85  1.1  thorpej 	{ "reset",	device_reset },
     86  1.1  thorpej 	{ NULL,		NULL },
     87  1.1  thorpej };
     88  1.1  thorpej 
     89  1.1  thorpej void	bus_reset __P((int, char *[]));
     90  1.1  thorpej void	bus_scan __P((int, char *[]));
     91  1.1  thorpej 
     92  1.1  thorpej struct command bus_commands[] = {
     93  1.1  thorpej 	{ "reset",	bus_reset },
     94  1.1  thorpej 	{ "scan",	bus_scan },
     95  1.1  thorpej 	{ NULL,		NULL },
     96  1.1  thorpej };
     97  1.1  thorpej 
     98  1.1  thorpej int
     99  1.1  thorpej main(argc, argv)
    100  1.1  thorpej 	int argc;
    101  1.1  thorpej 	char *argv[];
    102  1.1  thorpej {
    103  1.1  thorpej 	struct command *commands;
    104  1.1  thorpej 	int i;
    105  1.1  thorpej 
    106  1.1  thorpej 	/* Must have at least: device command */
    107  1.1  thorpej 	if (argc < 3)
    108  1.1  thorpej 		usage();
    109  1.1  thorpej 
    110  1.1  thorpej 	/* Skip program name, get and skip device name and command. */
    111  1.1  thorpej 	dvname = argv[1];
    112  1.1  thorpej 	cmdname = argv[2];
    113  1.1  thorpej 	argv += 3;
    114  1.1  thorpej 	argc -= 3;
    115  1.1  thorpej 
    116  1.1  thorpej 	/*
    117  1.1  thorpej 	 * Open the device and determine if it's a scsibus or an actual
    118  1.1  thorpej 	 * device.  Devices respond to the SCIOCIDENTIFY ioctl.
    119  1.1  thorpej 	 */
    120  1.1  thorpej 	fd = opendisk(dvname, O_RDWR, dvname_store, sizeof(dvname_store), 0);
    121  1.1  thorpej 	if (fd == -1) {
    122  1.1  thorpej 		if (errno == ENOENT) {
    123  1.1  thorpej 			/*
    124  1.1  thorpej 			 * Device doesn't exist.  Probably trying to open
    125  1.1  thorpej 			 * a device which doesn't use disk semantics for
    126  1.3  thorpej 			 * device name.  Try again, specifying "cooked",
    127  1.3  thorpej 			 * which leaves off the "r" in front of the device's
    128  1.3  thorpej 			 * name.
    129  1.1  thorpej 			 */
    130  1.3  thorpej 			fd = opendisk(dvname, O_RDWR, dvname_store,
    131  1.3  thorpej 			    sizeof(dvname_store), 1);
    132  1.1  thorpej 			if (fd == -1)
    133  1.1  thorpej 				err(1, "%s", dvname);
    134  1.1  thorpej 		}
    135  1.1  thorpej 		err(1, "%s", dvname);
    136  1.1  thorpej 	}
    137  1.3  thorpej 
    138  1.3  thorpej 	/*
    139  1.3  thorpej 	 * Point the dvname at the actual device name that opendisk() opened.
    140  1.3  thorpej 	 */
    141  1.3  thorpej 	dvname = dvname_store;
    142  1.1  thorpej 
    143  1.1  thorpej 	if (ioctl(fd, SCIOCIDENTIFY, &dvaddr) < 0)
    144  1.1  thorpej 		commands = bus_commands;
    145  1.1  thorpej 	else
    146  1.1  thorpej 		commands = device_commands;
    147  1.1  thorpej 
    148  1.1  thorpej 	/* Look up and call the command. */
    149  1.1  thorpej 	for (i = 0; commands[i].cmd_name != NULL; i++)
    150  1.1  thorpej 		if (strcmp(cmdname, commands[i].cmd_name) == 0)
    151  1.1  thorpej 			break;
    152  1.1  thorpej 	if (commands[i].cmd_name == NULL)
    153  1.1  thorpej 		errx(1, "unknown %s command: %s\n",
    154  1.1  thorpej 		    commands == bus_commands ? "bus" : "device", cmdname);
    155  1.1  thorpej 
    156  1.1  thorpej 	(*commands[i].cmd_func)(argc, argv);
    157  1.1  thorpej 	exit(0);
    158  1.1  thorpej }
    159  1.1  thorpej 
    160  1.1  thorpej void
    161  1.1  thorpej usage()
    162  1.1  thorpej {
    163  1.1  thorpej 
    164  1.1  thorpej 	fprintf(stderr, "usage: %s device command [arg [...]]\n",
    165  1.1  thorpej 	    __progname);
    166  1.1  thorpej 	exit(1);
    167  1.1  thorpej }
    168  1.1  thorpej 
    169  1.1  thorpej /*
    170  1.1  thorpej  * DEVICE COMMANDS
    171  1.1  thorpej  */
    172  1.1  thorpej 
    173  1.1  thorpej /*
    174  1.1  thorpej  * device_identify:
    175  1.1  thorpej  *
    176  1.1  thorpej  *	Display the identity of the device, including it's SCSI bus,
    177  1.1  thorpej  *	target, lun, and it's vendor/product/revision information.
    178  1.1  thorpej  */
    179  1.1  thorpej void
    180  1.1  thorpej device_identify(argc, argv)
    181  1.1  thorpej 	int argc;
    182  1.1  thorpej 	char *argv[];
    183  1.1  thorpej {
    184  1.1  thorpej 	struct scsipi_inquiry_data inqbuf;
    185  1.1  thorpej 	struct scsipi_inquiry cmd;
    186  1.1  thorpej 
    187  1.1  thorpej 	/* x4 in case every character is escaped, +1 for NUL. */
    188  1.1  thorpej 	char vendor[(sizeof(inqbuf.vendor) * 4) + 1],
    189  1.1  thorpej 	     product[(sizeof(inqbuf.product) * 4) + 1],
    190  1.1  thorpej 	     revision[(sizeof(inqbuf.revision) * 4) + 1];
    191  1.1  thorpej 
    192  1.1  thorpej 	/* No arguments. */
    193  1.1  thorpej 	if (argc != 0)
    194  1.1  thorpej 		goto usage;
    195  1.1  thorpej 
    196  1.1  thorpej 	memset(&cmd, 0, sizeof(cmd));
    197  1.1  thorpej 	memset(&inqbuf, 0, sizeof(inqbuf));
    198  1.1  thorpej 
    199  1.1  thorpej 	cmd.opcode = INQUIRY;
    200  1.1  thorpej 	cmd.length = sizeof(inqbuf);
    201  1.1  thorpej 
    202  1.1  thorpej 	scsi_command(fd, &cmd, sizeof(cmd), &inqbuf, sizeof(inqbuf),
    203  1.1  thorpej 	    10000, SCCMD_READ);
    204  1.1  thorpej 
    205  1.1  thorpej 	scsi_strvis(vendor, sizeof(vendor), inqbuf.vendor,
    206  1.1  thorpej 	    sizeof(inqbuf.vendor));
    207  1.1  thorpej 	scsi_strvis(product, sizeof(product), inqbuf.product,
    208  1.1  thorpej 	    sizeof(inqbuf.product));
    209  1.1  thorpej 	scsi_strvis(revision, sizeof(revision), inqbuf.revision,
    210  1.1  thorpej 	    sizeof(inqbuf.revision));
    211  1.1  thorpej 
    212  1.1  thorpej 	printf("%s: scsibus%d target %d lun %d <%s, %s, %s>\n",
    213  1.1  thorpej 	    dvname, dvaddr.addr.scsi.scbus, dvaddr.addr.scsi.target,
    214  1.1  thorpej 	    dvaddr.addr.scsi.lun, vendor, product, revision);
    215  1.1  thorpej 
    216  1.1  thorpej 	return;
    217  1.1  thorpej 
    218  1.1  thorpej  usage:
    219  1.2  thorpej 	fprintf(stderr, "usage: %s device %s\n", __progname, cmdname);
    220  1.1  thorpej 	exit(1);
    221  1.1  thorpej }
    222  1.1  thorpej 
    223  1.1  thorpej /*
    224  1.1  thorpej  * device_reassign:
    225  1.1  thorpej  *
    226  1.1  thorpej  *	Reassign bad blocks on a direct access device.
    227  1.1  thorpej  */
    228  1.1  thorpej void
    229  1.1  thorpej device_reassign(argc, argv)
    230  1.1  thorpej 	int argc;
    231  1.1  thorpej 	char *argv[];
    232  1.1  thorpej {
    233  1.1  thorpej 	struct scsi_reassign_blocks cmd;
    234  1.1  thorpej 	struct scsi_reassign_blocks_data *data;
    235  1.1  thorpej 	size_t dlen;
    236  1.1  thorpej 	u_int32_t blkno;
    237  1.1  thorpej 	int i;
    238  1.1  thorpej 	char *cp;
    239  1.1  thorpej 
    240  1.1  thorpej 	/* We get a list of block numbers. */
    241  1.1  thorpej 	if (argc < 1)
    242  1.1  thorpej 		goto usage;
    243  1.1  thorpej 
    244  1.1  thorpej 	/*
    245  1.1  thorpej 	 * Allocate the reassign blocks descriptor.  The 4 comes from the
    246  1.1  thorpej 	 * size of the block address in the defect descriptor.
    247  1.1  thorpej 	 */
    248  1.1  thorpej 	dlen = sizeof(struct scsi_reassign_blocks_data) + ((argc - 1) * 4);
    249  1.1  thorpej 	data = malloc(dlen);
    250  1.1  thorpej 	if (data == NULL)
    251  1.1  thorpej 		errx(1, "unable to allocate defect descriptor");
    252  1.1  thorpej 	memset(data, 0, dlen);
    253  1.1  thorpej 
    254  1.1  thorpej 	cmd.opcode = SCSI_REASSIGN_BLOCKS;
    255  1.1  thorpej 
    256  1.1  thorpej 	/* Defect descriptor length. */
    257  1.1  thorpej 	_lto2l(argc * 4, data->length);
    258  1.1  thorpej 
    259  1.1  thorpej 	/* Build the defect descriptor list. */
    260  1.1  thorpej 	for (i = 0; i < argc; i++) {
    261  1.1  thorpej 		blkno = strtoul(argv[i], &cp, 10);
    262  1.1  thorpej 		if (*cp != '\0')
    263  1.1  thorpej 			errx(1, "invalid block number: %s\n", argv[i]);
    264  1.1  thorpej 		_lto4l(blkno, data->defect_descriptor[i].dlbaddr);
    265  1.1  thorpej 	}
    266  1.1  thorpej 
    267  1.1  thorpej 	scsi_command(fd, &cmd, sizeof(cmd), data, dlen, 30000, SCCMD_WRITE);
    268  1.1  thorpej 
    269  1.1  thorpej 	free(data);
    270  1.1  thorpej 	return;
    271  1.1  thorpej 
    272  1.1  thorpej  usage:
    273  1.2  thorpej 	fprintf(stderr, "usage: %s device %s blkno [blkno [...]]\n",
    274  1.1  thorpej 	    __progname, cmdname);
    275  1.1  thorpej 	exit(1);
    276  1.1  thorpej }
    277  1.1  thorpej 
    278  1.1  thorpej /*
    279  1.1  thorpej  * device_reset:
    280  1.1  thorpej  *
    281  1.1  thorpej  *	Issue a reset to a SCSI device.
    282  1.1  thorpej  */
    283  1.1  thorpej void
    284  1.1  thorpej device_reset(argc, argv)
    285  1.1  thorpej 	int argc;
    286  1.1  thorpej 	char *argv[];
    287  1.1  thorpej {
    288  1.1  thorpej 
    289  1.1  thorpej 	/* No arguments. */
    290  1.1  thorpej 	if (argc != 0)
    291  1.1  thorpej 		goto usage;
    292  1.1  thorpej 
    293  1.1  thorpej 	if (ioctl(fd, SCIOCRESET, NULL) != 0)
    294  1.1  thorpej 		err(1, "SCIOCRESET");
    295  1.1  thorpej 
    296  1.1  thorpej 	return;
    297  1.1  thorpej 
    298  1.1  thorpej  usage:
    299  1.2  thorpej 	fprintf(stderr, "usage: %s device %s\n", __progname, cmdname);
    300  1.1  thorpej 	exit(1);
    301  1.1  thorpej }
    302  1.1  thorpej 
    303  1.1  thorpej /*
    304  1.1  thorpej  * BUS COMMANDS
    305  1.1  thorpej  */
    306  1.1  thorpej 
    307  1.1  thorpej /*
    308  1.1  thorpej  * bus_reset:
    309  1.1  thorpej  *
    310  1.1  thorpej  *	Issue a reset to a SCSI bus.
    311  1.1  thorpej  */
    312  1.1  thorpej void
    313  1.1  thorpej bus_reset(argc, argv)
    314  1.1  thorpej 	int argc;
    315  1.1  thorpej 	char *argv[];
    316  1.1  thorpej {
    317  1.1  thorpej 
    318  1.1  thorpej 	/* No arguments. */
    319  1.1  thorpej 	if (argc != 0)
    320  1.1  thorpej 		goto usage;
    321  1.1  thorpej 
    322  1.1  thorpej 	if (ioctl(fd, SCBUSIORESET, NULL) != 0)
    323  1.1  thorpej 		err(1, "SCBUSIORESET");
    324  1.1  thorpej 
    325  1.1  thorpej 	return;
    326  1.1  thorpej 
    327  1.1  thorpej  usage:
    328  1.2  thorpej 	fprintf(stderr, "usage: %s device %s\n", __progname, cmdname);
    329  1.1  thorpej 	exit(1);
    330  1.1  thorpej }
    331  1.1  thorpej 
    332  1.1  thorpej /*
    333  1.1  thorpej  * bus_scan:
    334  1.1  thorpej  *
    335  1.1  thorpej  *	Rescan a SCSI bus for new devices.
    336  1.1  thorpej  */
    337  1.1  thorpej void
    338  1.1  thorpej bus_scan(argc, argv)
    339  1.1  thorpej 	int argc;
    340  1.1  thorpej 	char *argv[];
    341  1.1  thorpej {
    342  1.1  thorpej 	struct scbusioscan_args args;
    343  1.1  thorpej 	char *cp;
    344  1.1  thorpej 
    345  1.1  thorpej 	/* Must have two args: target lun */
    346  1.1  thorpej 	if (argc != 2)
    347  1.1  thorpej 		goto usage;
    348  1.1  thorpej 
    349  1.1  thorpej 	if (strcmp(argv[0], "any") == 0)
    350  1.1  thorpej 		args.sa_target = -1;
    351  1.1  thorpej 	else {
    352  1.1  thorpej 		args.sa_target = strtol(argv[0], &cp, 10);
    353  1.1  thorpej 		if (*cp != '\0' || args.sa_target < 0)
    354  1.1  thorpej 			errx(1, "invalid target: %s\n", argv[0]);
    355  1.1  thorpej 	}
    356  1.1  thorpej 
    357  1.1  thorpej 	if (strcmp(argv[1], "any") == 0)
    358  1.1  thorpej 		args.sa_lun = -1;
    359  1.1  thorpej 	else {
    360  1.1  thorpej 		args.sa_lun = strtol(argv[1], &cp, 10);
    361  1.1  thorpej 		if (*cp != '\0' || args.sa_lun < 0)
    362  1.1  thorpej 			errx(1, "invalid lun: %s\n", argv[1]);
    363  1.1  thorpej 	}
    364  1.1  thorpej 
    365  1.1  thorpej 	if (ioctl(fd, SCBUSIOSCAN, &args) != 0)
    366  1.1  thorpej 		err(1, "SCBUSIOSCAN");
    367  1.1  thorpej 
    368  1.1  thorpej 	return;
    369  1.1  thorpej 
    370  1.1  thorpej  usage:
    371  1.2  thorpej 	fprintf(stderr, "usage: %s device %s target lun\n", __progname,
    372  1.2  thorpej 	    cmdname);
    373  1.1  thorpej 	fprintf(stderr, "       use `any' to wildcard target or lun\n");
    374  1.1  thorpej 	exit(1);
    375  1.1  thorpej }
    376