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