Home | History | Annotate | Line # | Download | only in scsictl
scsictl.c revision 1.18
      1  1.18  thorpej /*	$NetBSD: scsictl.c,v 1.18 2002/09/03 16:56:05 thorpej Exp $	*/
      2   1.1  thorpej 
      3   1.1  thorpej /*-
      4  1.18  thorpej  * Copyright (c) 1998, 2002 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.4  thorpej #include <dev/scsipi/scsi_all.h>
     58   1.1  thorpej #include <dev/scsipi/scsi_disk.h>
     59   1.1  thorpej #include <dev/scsipi/scsipiconf.h>
     60   1.1  thorpej 
     61   1.1  thorpej #include "extern.h"
     62   1.1  thorpej 
     63   1.1  thorpej struct command {
     64   1.1  thorpej 	const char *cmd_name;
     65   1.6  hubertf 	const char *arg_names;
     66   1.1  thorpej 	void (*cmd_func) __P((int, char *[]));
     67   1.1  thorpej };
     68   1.1  thorpej 
     69   1.1  thorpej int	main __P((int, char *[]));
     70   1.1  thorpej void	usage __P((void));
     71   1.1  thorpej 
     72   1.1  thorpej int	fd;				/* file descriptor for device */
     73   1.1  thorpej const	char *dvname;			/* device name */
     74   1.1  thorpej char	dvname_store[MAXPATHLEN];	/* for opendisk(3) */
     75   1.1  thorpej const	char *cmdname;			/* command user issued */
     76   1.6  hubertf const	char *argnames;			/* helpstring: expected arguments */
     77   1.1  thorpej struct	scsi_addr dvaddr;		/* SCSI device's address */
     78   1.1  thorpej 
     79   1.4  thorpej void	device_format __P((int, char *[]));
     80   1.1  thorpej void	device_identify __P((int, char *[]));
     81   1.1  thorpej void	device_reassign __P((int, char *[]));
     82  1.16   mjacob void	device_release __P((int, char *[]));
     83  1.16   mjacob void	device_reserve __P((int, char *[]));
     84   1.1  thorpej void	device_reset __P((int, char *[]));
     85  1.16   mjacob void	device_start __P((int, char *[]));
     86  1.16   mjacob void	device_stop __P((int, char *[]));
     87  1.16   mjacob void	device_tur __P((int, char *[]));
     88  1.18  thorpej void	device_getcache __P((int, char *[]));
     89  1.18  thorpej void	device_setcache __P((int, char *[]));
     90   1.1  thorpej 
     91   1.1  thorpej struct command device_commands[] = {
     92  1.16   mjacob 	{ "format",	"[blocksize [immediate]]", 	device_format },
     93   1.7      mjl 	{ "identify",	"",			device_identify },
     94   1.7      mjl 	{ "reassign",	"blkno [blkno [...]]",	device_reassign },
     95  1.16   mjacob 	{ "release",	"",			device_release },
     96  1.16   mjacob 	{ "reserve",	"",			device_reserve },
     97   1.7      mjl 	{ "reset",	"",			device_reset },
     98  1.16   mjacob 	{ "start",	"",			device_start },
     99  1.16   mjacob 	{ "stop",	"",			device_stop },
    100  1.16   mjacob 	{ "tur",	"",			device_tur },
    101  1.18  thorpej 	{ "getcache",	"",			device_getcache },
    102  1.18  thorpej 	{ "setcache",	"none|r|w|rw [save]",	device_setcache },
    103   1.7      mjl 	{ NULL,		NULL,			NULL },
    104   1.1  thorpej };
    105   1.1  thorpej 
    106   1.1  thorpej void	bus_reset __P((int, char *[]));
    107   1.1  thorpej void	bus_scan __P((int, char *[]));
    108  1.14   bouyer void	bus_detach __P((int, char *[]));
    109   1.1  thorpej 
    110   1.1  thorpej struct command bus_commands[] = {
    111   1.7      mjl 	{ "reset",	"",			bus_reset },
    112   1.7      mjl 	{ "scan",	"target lun",		bus_scan },
    113  1.14   bouyer 	{ "detach",	"target lun",		bus_detach },
    114   1.6  hubertf 	{ NULL,		NULL,				NULL },
    115   1.1  thorpej };
    116   1.1  thorpej 
    117   1.1  thorpej int
    118   1.1  thorpej main(argc, argv)
    119   1.1  thorpej 	int argc;
    120   1.1  thorpej 	char *argv[];
    121   1.1  thorpej {
    122   1.1  thorpej 	struct command *commands;
    123   1.1  thorpej 	int i;
    124   1.1  thorpej 
    125   1.1  thorpej 	/* Must have at least: device command */
    126   1.1  thorpej 	if (argc < 3)
    127   1.1  thorpej 		usage();
    128   1.1  thorpej 
    129   1.1  thorpej 	/* Skip program name, get and skip device name and command. */
    130   1.1  thorpej 	dvname = argv[1];
    131   1.1  thorpej 	cmdname = argv[2];
    132   1.1  thorpej 	argv += 3;
    133   1.1  thorpej 	argc -= 3;
    134   1.1  thorpej 
    135   1.1  thorpej 	/*
    136   1.1  thorpej 	 * Open the device and determine if it's a scsibus or an actual
    137   1.1  thorpej 	 * device.  Devices respond to the SCIOCIDENTIFY ioctl.
    138   1.1  thorpej 	 */
    139   1.1  thorpej 	fd = opendisk(dvname, O_RDWR, dvname_store, sizeof(dvname_store), 0);
    140   1.1  thorpej 	if (fd == -1) {
    141   1.1  thorpej 		if (errno == ENOENT) {
    142   1.1  thorpej 			/*
    143   1.1  thorpej 			 * Device doesn't exist.  Probably trying to open
    144   1.1  thorpej 			 * a device which doesn't use disk semantics for
    145   1.3  thorpej 			 * device name.  Try again, specifying "cooked",
    146   1.3  thorpej 			 * which leaves off the "r" in front of the device's
    147   1.3  thorpej 			 * name.
    148   1.1  thorpej 			 */
    149   1.3  thorpej 			fd = opendisk(dvname, O_RDWR, dvname_store,
    150   1.3  thorpej 			    sizeof(dvname_store), 1);
    151   1.1  thorpej 			if (fd == -1)
    152   1.1  thorpej 				err(1, "%s", dvname);
    153   1.5    jwise 		} else
    154   1.5    jwise 			err(1, "%s", dvname);
    155   1.1  thorpej 	}
    156   1.3  thorpej 
    157   1.3  thorpej 	/*
    158   1.3  thorpej 	 * Point the dvname at the actual device name that opendisk() opened.
    159   1.3  thorpej 	 */
    160   1.3  thorpej 	dvname = dvname_store;
    161   1.1  thorpej 
    162   1.1  thorpej 	if (ioctl(fd, SCIOCIDENTIFY, &dvaddr) < 0)
    163   1.1  thorpej 		commands = bus_commands;
    164   1.1  thorpej 	else
    165   1.1  thorpej 		commands = device_commands;
    166   1.1  thorpej 
    167   1.1  thorpej 	/* Look up and call the command. */
    168   1.1  thorpej 	for (i = 0; commands[i].cmd_name != NULL; i++)
    169   1.1  thorpej 		if (strcmp(cmdname, commands[i].cmd_name) == 0)
    170   1.1  thorpej 			break;
    171   1.1  thorpej 	if (commands[i].cmd_name == NULL)
    172  1.12       ad 		errx(1, "unknown %s command: %s",
    173   1.1  thorpej 		    commands == bus_commands ? "bus" : "device", cmdname);
    174   1.1  thorpej 
    175   1.6  hubertf 	argnames = commands[i].arg_names;
    176   1.6  hubertf 
    177   1.1  thorpej 	(*commands[i].cmd_func)(argc, argv);
    178   1.1  thorpej 	exit(0);
    179   1.1  thorpej }
    180   1.1  thorpej 
    181   1.1  thorpej void
    182   1.1  thorpej usage()
    183   1.1  thorpej {
    184   1.6  hubertf 	int i;
    185   1.1  thorpej 
    186   1.6  hubertf 	fprintf(stderr, "Usage: %s device command [arg [...]]\n",
    187  1.11      cgd 	    getprogname());
    188   1.7      mjl 
    189   1.7      mjl 	fprintf(stderr, "   Commands pertaining to scsi devices:\n");
    190   1.6  hubertf 	for (i=0; device_commands[i].cmd_name != NULL; i++)
    191   1.7      mjl 		fprintf(stderr, "\t%s %s\n", device_commands[i].cmd_name,
    192   1.6  hubertf 					    device_commands[i].arg_names);
    193   1.7      mjl 	fprintf(stderr, "   Commands pertaining to scsi busses:\n");
    194   1.6  hubertf 	for (i=0; bus_commands[i].cmd_name != NULL; i++)
    195   1.7      mjl 		fprintf(stderr, "\t%s %s\n", bus_commands[i].cmd_name,
    196   1.6  hubertf 					    bus_commands[i].arg_names);
    197   1.8       ad 	fprintf(stderr, "   Use `any' or `all' to wildcard target or lun\n");
    198   1.6  hubertf 
    199   1.1  thorpej 	exit(1);
    200   1.1  thorpej }
    201   1.1  thorpej 
    202   1.1  thorpej /*
    203   1.1  thorpej  * DEVICE COMMANDS
    204   1.1  thorpej  */
    205   1.4  thorpej 
    206   1.4  thorpej /*
    207   1.4  thorpej  * device_format:
    208   1.4  thorpej  *
    209   1.4  thorpej  *	Format a direct access device.
    210   1.4  thorpej  */
    211   1.4  thorpej void
    212   1.4  thorpej device_format(argc, argv)
    213   1.4  thorpej 	int argc;
    214   1.4  thorpej 	char *argv[];
    215   1.4  thorpej {
    216  1.16   mjacob 	u_int32_t blksize;
    217  1.16   mjacob 	int i, j, immediate;
    218  1.16   mjacob #define	PC	(65536/10)
    219  1.16   mjacob 	static int complete[] = {
    220  1.16   mjacob 	    PC*1, PC*2, PC*3, PC*4, PC*5, PC*6, PC*7, PC*8, PC*9, 65536
    221  1.16   mjacob 	};
    222  1.16   mjacob 	char *cp, buffer[64];
    223  1.16   mjacob 	struct scsipi_sense_data sense;
    224   1.4  thorpej 	struct scsi_format_unit cmd;
    225   1.4  thorpej 	struct {
    226  1.16   mjacob 		struct scsi_format_unit_defect_list_header header;
    227  1.16   mjacob 		/* optional initialization pattern */
    228  1.16   mjacob 		/* optional defect list */
    229  1.16   mjacob 	} dfl;
    230  1.16   mjacob 	struct {
    231  1.13   bouyer 		struct scsipi_mode_header header;
    232   1.4  thorpej 		struct scsi_blk_desc blk_desc;
    233   1.4  thorpej 		struct page_disk_format format_page;
    234  1.16   mjacob 	} mode_page;
    235  1.16   mjacob 	struct {
    236  1.16   mjacob 		struct scsipi_mode_header header;
    237  1.16   mjacob 		struct scsi_blk_desc blk_desc;
    238  1.16   mjacob 	} data_select;
    239  1.16   mjacob 
    240   1.4  thorpej 
    241  1.16   mjacob 	/* Blocksize is an optional argument. */
    242  1.16   mjacob 	if (argc > 2)
    243   1.6  hubertf 		usage();
    244   1.4  thorpej 
    245   1.4  thorpej 	/*
    246  1.16   mjacob 	 * Loop doing Request Sense to clear any pending Unit Attention.
    247  1.16   mjacob 	 *
    248  1.16   mjacob 	 * Multiple conditions may exist on the drive which are returned
    249  1.16   mjacob 	 * in priority order.
    250  1.16   mjacob 	 */
    251  1.16   mjacob 	for (i = 0; i < 8; i++) {
    252  1.16   mjacob 		scsi_request_sense(fd, &sense, sizeof (sense));
    253  1.16   mjacob 		if ((j = sense.flags & SSD_KEY) == SKEY_NO_SENSE)
    254  1.16   mjacob 			break;
    255  1.16   mjacob 	}
    256  1.16   mjacob 	/*
    257  1.16   mjacob 	 * Make sure we cleared any pending Unit Attention
    258  1.16   mjacob 	 */
    259  1.16   mjacob 	if (j != SKEY_NO_SENSE) {
    260  1.16   mjacob 		cp = scsi_decode_sense((const unsigned char *) &sense, 2,
    261  1.16   mjacob 		    buffer, sizeof (buffer));
    262  1.17    grant 		errx(1, "failed to clean Unit Attention: %s", cp);
    263  1.16   mjacob 	}
    264  1.16   mjacob 
    265  1.16   mjacob 	/*
    266   1.4  thorpej 	 * Get the DISK FORMAT mode page.  SCSI-2 recommends specifying the
    267   1.4  thorpej 	 * interleave read from this page in the FORMAT UNIT command.
    268   1.4  thorpej 	 */
    269  1.16   mjacob 	scsi_mode_sense(fd, 0x03, 0x00, &mode_page, sizeof(mode_page));
    270  1.16   mjacob 
    271  1.16   mjacob 	j = (mode_page.format_page.bytes_s[0] << 8) |
    272  1.16   mjacob 	    (mode_page.format_page.bytes_s[1]);
    273  1.16   mjacob 
    274  1.16   mjacob 	if (j != DEV_BSIZE)
    275  1.16   mjacob 		printf("current disk sector size: %hd\n", j);
    276   1.4  thorpej 
    277   1.4  thorpej 	memset(&cmd, 0, sizeof(cmd));
    278   1.4  thorpej 
    279   1.4  thorpej 	cmd.opcode = SCSI_FORMAT_UNIT;
    280  1.16   mjacob 	memcpy(cmd.interleave, mode_page.format_page.interleave,
    281   1.4  thorpej 	    sizeof(cmd.interleave));
    282   1.4  thorpej 
    283  1.16   mjacob 	/*
    284  1.16   mjacob 	 * The blocksize on the device is only changed if the user
    285  1.16   mjacob 	 * specified a new blocksize. If not specified the blocksize
    286  1.16   mjacob 	 * used for the device will be the Default value in the device.
    287  1.16   mjacob 	 * We don't specify the number of blocks since the format
    288  1.16   mjacob 	 * command will always reformat the entire drive.  Also by
    289  1.16   mjacob 	 * not specifying a block count the drive will reset the
    290  1.16   mjacob 	 * block count to the maximum available after the format
    291  1.16   mjacob 	 * completes if the blocksize was changed in the format.
    292  1.16   mjacob 	 * Finally, the new disk geometry will not but updated on
    293  1.16   mjacob 	 * the drive in permanent storage until _AFTER_ the format
    294  1.16   mjacob 	 * completes successfully.
    295  1.16   mjacob 	 */
    296  1.16   mjacob 	if (argc > 0) {
    297  1.16   mjacob 		blksize = strtoul(argv[0], &cp, 10);
    298  1.16   mjacob 		if (*cp != '\0')
    299  1.17    grant 			errx(1, "invalid block size: %s", argv[0]);
    300  1.16   mjacob 
    301  1.16   mjacob 		memset(&data_select, 0, sizeof(data_select));
    302  1.16   mjacob 
    303  1.16   mjacob 		data_select.header.blk_desc_len = sizeof(struct scsi_blk_desc);
    304  1.16   mjacob 		/*
    305  1.16   mjacob 		 * blklen in desc is 3 bytes with a leading reserved byte
    306  1.16   mjacob 		 */
    307  1.16   mjacob 		_lto4b(blksize, &data_select.blk_desc.reserved);
    308  1.16   mjacob 
    309  1.16   mjacob 		/*
    310  1.16   mjacob 		 * Issue Mode Select to modify the device blocksize to be
    311  1.16   mjacob 		 * used on the Format.  The modified device geometry will
    312  1.16   mjacob 		 * be stored as Current and Saved Page 3 parameters when
    313  1.16   mjacob 		 * the Format completes.
    314  1.16   mjacob 		 */
    315  1.16   mjacob 		scsi_mode_select(fd, 0, &data_select, sizeof(data_select));
    316  1.16   mjacob 
    317  1.16   mjacob 		/*
    318  1.16   mjacob 		 * Since user specified a specific block size make sure it
    319  1.16   mjacob 		 * gets stored in the device when the format completes.
    320  1.16   mjacob 		 *
    321  1.16   mjacob 		 * Also scrub the defect list back to the manufacturers
    322  1.16   mjacob 		 * original.
    323  1.16   mjacob 		 */
    324  1.16   mjacob 		cmd.flags = SFU_CMPLST | SFU_FMTDATA;
    325  1.16   mjacob 	}
    326  1.16   mjacob 
    327  1.16   mjacob 	memset(&dfl, 0, sizeof(dfl));
    328   1.4  thorpej 
    329  1.16   mjacob 	if (argc > 1 && strncmp(argv[1], "imm", 3) == 0) {
    330  1.16   mjacob 		/*
    331  1.16   mjacob 		 * Signal target for an immediate return from Format.
    332  1.16   mjacob 		 *
    333  1.16   mjacob 		 * We'll poll for completion status.
    334  1.16   mjacob 		 */
    335  1.16   mjacob 		dfl.header.flags = DLH_IMMED;
    336  1.16   mjacob 		immediate = 1;
    337  1.16   mjacob 	} else {
    338  1.16   mjacob 		immediate = 0;
    339  1.16   mjacob 	}
    340  1.16   mjacob 
    341  1.16   mjacob 	scsi_command(fd, &cmd, sizeof(cmd), &dfl, sizeof(dfl),
    342  1.16   mjacob 	    8 * 60 * 60 * 1000, 0);
    343  1.16   mjacob 
    344  1.16   mjacob 	/*
    345  1.16   mjacob 	 * Poll device for completion of Format
    346  1.16   mjacob 	 */
    347  1.16   mjacob 	if (immediate) {
    348  1.16   mjacob 		i = 0;
    349  1.16   mjacob 		printf("formatting.");
    350  1.16   mjacob 		fflush(stdout);
    351  1.16   mjacob 		do {
    352  1.16   mjacob 			scsireq_t req;
    353  1.16   mjacob 			struct scsipi_test_unit_ready tcmd;
    354  1.16   mjacob 
    355  1.16   mjacob 			memset(&tcmd, 0, sizeof(cmd));
    356  1.16   mjacob 			tcmd.opcode = TEST_UNIT_READY;
    357  1.16   mjacob 
    358  1.16   mjacob 			memset(&req, 0, sizeof(req));
    359  1.16   mjacob 			memcpy(req.cmd, &tcmd, 6);
    360  1.16   mjacob 			req.cmdlen = 6;
    361  1.16   mjacob 			req.timeout = 10000;
    362  1.16   mjacob 			req.senselen = SENSEBUFLEN;
    363  1.16   mjacob 
    364  1.16   mjacob 			if (ioctl(fd, SCIOCCOMMAND, &req) == -1) {
    365  1.16   mjacob 				err(1, "SCIOCCOMMAND");
    366  1.16   mjacob 			}
    367  1.16   mjacob 
    368  1.16   mjacob 			if (req.retsts == SCCMD_OK) {
    369  1.16   mjacob 				break;
    370  1.16   mjacob 			} else if (req.retsts == SCCMD_TIMEOUT) {
    371  1.16   mjacob 				fprintf(stderr, "%s: SCSI command timed out",
    372  1.16   mjacob 				    dvname);
    373  1.16   mjacob 				break;
    374  1.16   mjacob 			} else if (req.retsts == SCCMD_BUSY) {
    375  1.16   mjacob 				fprintf(stderr, "%s: device is busy",
    376  1.16   mjacob 				    dvname);
    377  1.16   mjacob 				break;
    378  1.16   mjacob 			} else if (req.retsts != SCCMD_SENSE) {
    379  1.16   mjacob 				fprintf(stderr,
    380  1.16   mjacob 				    "%s: device had unknown status %x", dvname,
    381  1.16   mjacob 				    req.retsts);
    382  1.16   mjacob 				break;
    383  1.16   mjacob 			}
    384  1.16   mjacob 			memcpy(&sense, req.sense, SENSEBUFLEN);
    385  1.16   mjacob 			if (sense.sense_key_spec_1 == SSD_SCS_VALID) {
    386  1.16   mjacob 				j = (sense.sense_key_spec_2 << 8) |
    387  1.16   mjacob 				    (sense.sense_key_spec_3);
    388  1.16   mjacob 				if (j >= complete[i]) {
    389  1.16   mjacob 					printf(".%d0%%.", ++i);
    390  1.16   mjacob 					fflush(stdout);
    391  1.16   mjacob 				}
    392  1.16   mjacob 			}
    393  1.16   mjacob 			sleep(10);
    394  1.16   mjacob 		} while ((sense.flags & SSD_KEY) == SKEY_NOT_READY);
    395  1.16   mjacob 		printf(".100%%..done.\n");
    396  1.16   mjacob 	}
    397   1.4  thorpej 	return;
    398   1.4  thorpej }
    399   1.1  thorpej 
    400   1.1  thorpej /*
    401   1.1  thorpej  * device_identify:
    402   1.1  thorpej  *
    403   1.1  thorpej  *	Display the identity of the device, including it's SCSI bus,
    404   1.1  thorpej  *	target, lun, and it's vendor/product/revision information.
    405   1.1  thorpej  */
    406   1.1  thorpej void
    407   1.1  thorpej device_identify(argc, argv)
    408   1.1  thorpej 	int argc;
    409   1.1  thorpej 	char *argv[];
    410   1.1  thorpej {
    411   1.1  thorpej 	struct scsipi_inquiry_data inqbuf;
    412   1.1  thorpej 	struct scsipi_inquiry cmd;
    413   1.1  thorpej 
    414   1.1  thorpej 	/* x4 in case every character is escaped, +1 for NUL. */
    415   1.1  thorpej 	char vendor[(sizeof(inqbuf.vendor) * 4) + 1],
    416   1.1  thorpej 	     product[(sizeof(inqbuf.product) * 4) + 1],
    417   1.1  thorpej 	     revision[(sizeof(inqbuf.revision) * 4) + 1];
    418   1.1  thorpej 
    419   1.1  thorpej 	/* No arguments. */
    420   1.1  thorpej 	if (argc != 0)
    421   1.6  hubertf 		usage();
    422   1.1  thorpej 
    423   1.1  thorpej 	memset(&cmd, 0, sizeof(cmd));
    424   1.1  thorpej 	memset(&inqbuf, 0, sizeof(inqbuf));
    425   1.1  thorpej 
    426   1.1  thorpej 	cmd.opcode = INQUIRY;
    427   1.1  thorpej 	cmd.length = sizeof(inqbuf);
    428   1.1  thorpej 
    429   1.1  thorpej 	scsi_command(fd, &cmd, sizeof(cmd), &inqbuf, sizeof(inqbuf),
    430   1.1  thorpej 	    10000, SCCMD_READ);
    431   1.1  thorpej 
    432   1.1  thorpej 	scsi_strvis(vendor, sizeof(vendor), inqbuf.vendor,
    433   1.1  thorpej 	    sizeof(inqbuf.vendor));
    434   1.1  thorpej 	scsi_strvis(product, sizeof(product), inqbuf.product,
    435   1.1  thorpej 	    sizeof(inqbuf.product));
    436   1.1  thorpej 	scsi_strvis(revision, sizeof(revision), inqbuf.revision,
    437   1.1  thorpej 	    sizeof(inqbuf.revision));
    438   1.1  thorpej 
    439   1.1  thorpej 	printf("%s: scsibus%d target %d lun %d <%s, %s, %s>\n",
    440   1.1  thorpej 	    dvname, dvaddr.addr.scsi.scbus, dvaddr.addr.scsi.target,
    441   1.1  thorpej 	    dvaddr.addr.scsi.lun, vendor, product, revision);
    442   1.1  thorpej 
    443   1.1  thorpej 	return;
    444   1.1  thorpej }
    445   1.1  thorpej 
    446   1.1  thorpej /*
    447   1.1  thorpej  * device_reassign:
    448   1.1  thorpej  *
    449   1.1  thorpej  *	Reassign bad blocks on a direct access device.
    450   1.1  thorpej  */
    451   1.1  thorpej void
    452   1.1  thorpej device_reassign(argc, argv)
    453   1.1  thorpej 	int argc;
    454   1.1  thorpej 	char *argv[];
    455   1.1  thorpej {
    456   1.1  thorpej 	struct scsi_reassign_blocks cmd;
    457   1.1  thorpej 	struct scsi_reassign_blocks_data *data;
    458   1.1  thorpej 	size_t dlen;
    459   1.1  thorpej 	u_int32_t blkno;
    460   1.1  thorpej 	int i;
    461   1.1  thorpej 	char *cp;
    462   1.1  thorpej 
    463   1.1  thorpej 	/* We get a list of block numbers. */
    464   1.1  thorpej 	if (argc < 1)
    465   1.6  hubertf 		usage();
    466   1.1  thorpej 
    467   1.1  thorpej 	/*
    468   1.1  thorpej 	 * Allocate the reassign blocks descriptor.  The 4 comes from the
    469   1.1  thorpej 	 * size of the block address in the defect descriptor.
    470   1.1  thorpej 	 */
    471   1.1  thorpej 	dlen = sizeof(struct scsi_reassign_blocks_data) + ((argc - 1) * 4);
    472   1.1  thorpej 	data = malloc(dlen);
    473   1.1  thorpej 	if (data == NULL)
    474   1.1  thorpej 		errx(1, "unable to allocate defect descriptor");
    475   1.1  thorpej 	memset(data, 0, dlen);
    476   1.1  thorpej 
    477   1.1  thorpej 	cmd.opcode = SCSI_REASSIGN_BLOCKS;
    478   1.9  mycroft 	cmd.byte2 = 0;
    479   1.9  mycroft 	cmd.unused[0] = 0;
    480   1.9  mycroft 	cmd.unused[1] = 0;
    481   1.9  mycroft 	cmd.unused[2] = 0;
    482   1.9  mycroft 	cmd.control = 0;
    483   1.1  thorpej 
    484   1.1  thorpej 	/* Defect descriptor length. */
    485   1.9  mycroft 	_lto2b(argc * 4, data->length);
    486   1.1  thorpej 
    487   1.1  thorpej 	/* Build the defect descriptor list. */
    488   1.1  thorpej 	for (i = 0; i < argc; i++) {
    489   1.1  thorpej 		blkno = strtoul(argv[i], &cp, 10);
    490   1.1  thorpej 		if (*cp != '\0')
    491  1.12       ad 			errx(1, "invalid block number: %s", argv[i]);
    492   1.9  mycroft 		_lto4b(blkno, data->defect_descriptor[i].dlbaddr);
    493   1.1  thorpej 	}
    494   1.1  thorpej 
    495   1.1  thorpej 	scsi_command(fd, &cmd, sizeof(cmd), data, dlen, 30000, SCCMD_WRITE);
    496   1.1  thorpej 
    497   1.1  thorpej 	free(data);
    498   1.1  thorpej 	return;
    499   1.1  thorpej }
    500   1.1  thorpej 
    501   1.1  thorpej /*
    502  1.16   mjacob  * device_release:
    503  1.16   mjacob  *
    504  1.16   mjacob  *      Issue a RELEASE command to a SCSI drevice
    505  1.16   mjacob  */
    506  1.16   mjacob #ifndef	SCSI_RELEASE
    507  1.16   mjacob #define	SCSI_RELEASE	0x17
    508  1.16   mjacob #endif
    509  1.16   mjacob void
    510  1.16   mjacob device_release(argc, argv)
    511  1.16   mjacob 	int argc;
    512  1.16   mjacob 	char *argv[];
    513  1.16   mjacob {
    514  1.16   mjacob 	struct scsipi_test_unit_ready cmd;	/* close enough */
    515  1.16   mjacob 
    516  1.16   mjacob 	/* No arguments. */
    517  1.16   mjacob 	if (argc != 0)
    518  1.16   mjacob 		usage();
    519  1.16   mjacob 
    520  1.16   mjacob 	memset(&cmd, 0, sizeof(cmd));
    521  1.16   mjacob 
    522  1.16   mjacob 	cmd.opcode = SCSI_RELEASE;
    523  1.16   mjacob 
    524  1.16   mjacob 	scsi_command(fd, &cmd, sizeof(cmd), NULL, 0, 10000, 0);
    525  1.16   mjacob 
    526  1.16   mjacob 	return;
    527  1.16   mjacob }
    528  1.16   mjacob 
    529  1.16   mjacob 
    530  1.16   mjacob 
    531  1.16   mjacob /*
    532  1.16   mjacob  * device_reserve:
    533  1.16   mjacob  *
    534  1.16   mjacob  *      Issue a RESERVE command to a SCSI drevice
    535  1.16   mjacob  */
    536  1.16   mjacob #ifndef	SCSI_RESERVE
    537  1.16   mjacob #define	SCSI_RESERVE	0x16
    538  1.16   mjacob #endif
    539  1.16   mjacob void
    540  1.16   mjacob device_reserve(argc, argv)
    541  1.16   mjacob 	int argc;
    542  1.16   mjacob 	char *argv[];
    543  1.16   mjacob {
    544  1.16   mjacob 	struct scsipi_test_unit_ready cmd;	/* close enough */
    545  1.16   mjacob 
    546  1.16   mjacob 	/* No arguments. */
    547  1.16   mjacob 	if (argc != 0)
    548  1.16   mjacob 		usage();
    549  1.16   mjacob 
    550  1.16   mjacob 	memset(&cmd, 0, sizeof(cmd));
    551  1.16   mjacob 
    552  1.16   mjacob 	cmd.opcode = SCSI_RESERVE;
    553  1.16   mjacob 
    554  1.16   mjacob 	scsi_command(fd, &cmd, sizeof(cmd), NULL, 0, 10000, 0);
    555  1.16   mjacob 
    556  1.16   mjacob 	return;
    557  1.16   mjacob }
    558  1.16   mjacob 
    559  1.16   mjacob /*
    560   1.1  thorpej  * device_reset:
    561   1.1  thorpej  *
    562   1.1  thorpej  *	Issue a reset to a SCSI device.
    563   1.1  thorpej  */
    564   1.1  thorpej void
    565   1.1  thorpej device_reset(argc, argv)
    566   1.1  thorpej 	int argc;
    567   1.1  thorpej 	char *argv[];
    568   1.1  thorpej {
    569   1.1  thorpej 
    570   1.1  thorpej 	/* No arguments. */
    571   1.1  thorpej 	if (argc != 0)
    572   1.6  hubertf 		usage();
    573   1.1  thorpej 
    574   1.1  thorpej 	if (ioctl(fd, SCIOCRESET, NULL) != 0)
    575   1.1  thorpej 		err(1, "SCIOCRESET");
    576   1.1  thorpej 
    577   1.1  thorpej 	return;
    578   1.1  thorpej }
    579   1.1  thorpej 
    580   1.1  thorpej /*
    581  1.18  thorpej  * device_getcache:
    582  1.18  thorpej  *
    583  1.18  thorpej  *	Get the caching parameters for a SCSI disk.
    584   1.1  thorpej  */
    585  1.18  thorpej void
    586  1.18  thorpej device_getcache(argc, argv)
    587  1.18  thorpej 	int argc;
    588  1.18  thorpej 	char *argv[];
    589  1.18  thorpej {
    590  1.18  thorpej 	struct {
    591  1.18  thorpej 		struct scsipi_mode_header header;
    592  1.18  thorpej 		struct scsi_blk_desc blk_desc;
    593  1.18  thorpej 		struct page_caching caching_params;
    594  1.18  thorpej 	} data;
    595  1.18  thorpej 
    596  1.18  thorpej 	/* No arguments. */
    597  1.18  thorpej 	if (argc != 0)
    598  1.18  thorpej 		usage();
    599  1.18  thorpej 
    600  1.18  thorpej 	scsi_mode_sense(fd, 0x08, 0x00, &data, sizeof(data));
    601  1.18  thorpej 
    602  1.18  thorpej 	if ((data.caching_params.flags & (CACHING_RCD|CACHING_WCE)) ==
    603  1.18  thorpej 	    CACHING_RCD)
    604  1.18  thorpej 		printf("%s: no caches enabled\n", dvname);
    605  1.18  thorpej 	else {
    606  1.18  thorpej 		printf("%s: read cache %senabled\n", dvname,
    607  1.18  thorpej 		    (data.caching_params.flags & CACHING_RCD) ? "not " : "");
    608  1.18  thorpej 		printf("%s: write-back cache %senabled\n", dvname,
    609  1.18  thorpej 		    (data.caching_params.flags & CACHING_WCE) ? "" : "not ");
    610  1.18  thorpej 	}
    611  1.18  thorpej 	printf("%s: caching parameters are %ssavable\n", dvname,
    612  1.18  thorpej 	    (data.caching_params.pg_code & PGCODE_PS) ? "" : "not ");
    613  1.18  thorpej }
    614   1.1  thorpej 
    615   1.1  thorpej /*
    616  1.18  thorpej  * device_setcache:
    617   1.1  thorpej  *
    618  1.18  thorpej  *	Set cache enables for a SCSI disk.
    619   1.1  thorpej  */
    620   1.1  thorpej void
    621  1.18  thorpej device_setcache(argc, argv)
    622   1.1  thorpej 	int argc;
    623   1.1  thorpej 	char *argv[];
    624   1.1  thorpej {
    625  1.18  thorpej 	struct {
    626  1.18  thorpej 		struct scsipi_mode_header header;
    627  1.18  thorpej 		struct scsi_blk_desc blk_desc;
    628  1.18  thorpej 		struct page_caching caching_params;
    629  1.18  thorpej 	} data;
    630  1.18  thorpej 	int dlen;
    631  1.18  thorpej 	u_int8_t flags, byte2;
    632  1.18  thorpej 
    633  1.18  thorpej 	if (argc > 2 || argc == 0)
    634  1.18  thorpej 		usage();
    635  1.18  thorpej 
    636  1.18  thorpej 	if (strcmp(argv[0], "none") == 0)
    637  1.18  thorpej 		flags = CACHING_RCD;
    638  1.18  thorpej 	else if (strcmp(argv[0], "r") == 0)
    639  1.18  thorpej 		flags = 0;
    640  1.18  thorpej 	else if (strcmp(argv[0], "w") == 0)
    641  1.18  thorpej 		flags = CACHING_RCD|CACHING_WCE;
    642  1.18  thorpej 	else if (strcmp(argv[0], "rw") == 0)
    643  1.18  thorpej 		flags = CACHING_WCE;
    644  1.18  thorpej 	else
    645  1.18  thorpej 		usage();
    646  1.18  thorpej 
    647  1.18  thorpej 	if (argc == 2) {
    648  1.18  thorpej 		if (strcmp(argv[1], "save") == 0)
    649  1.18  thorpej 			byte2 = SMS_SP;
    650  1.18  thorpej 		else
    651  1.18  thorpej 			usage();
    652  1.18  thorpej 	}
    653  1.18  thorpej 
    654  1.18  thorpej 	scsi_mode_sense(fd, 0x08, 0x00, &data, sizeof(data));
    655  1.18  thorpej 
    656  1.18  thorpej 	data.caching_params.pg_code &= PGCODE_MASK;
    657  1.18  thorpej 	data.caching_params.flags =
    658  1.18  thorpej 	    (data.caching_params.flags & ~(CACHING_RCD|CACHING_WCE)) | flags;
    659   1.1  thorpej 
    660  1.18  thorpej 	data.caching_params.cache_segment_size[0] = 0;
    661  1.18  thorpej 	data.caching_params.cache_segment_size[1] = 0;
    662  1.18  thorpej 
    663  1.18  thorpej 	data.header.data_length = 0;
    664   1.1  thorpej 
    665  1.18  thorpej 	dlen = sizeof(data.header) + sizeof(data.blk_desc) + 2 +
    666  1.18  thorpej 	    data.caching_params.pg_length;
    667   1.1  thorpej 
    668  1.18  thorpej 	scsi_mode_select(fd, byte2, &data, dlen);
    669   1.1  thorpej }
    670  1.16   mjacob 
    671  1.16   mjacob /*
    672  1.16   mjacob  * device_start:
    673  1.16   mjacob  *
    674  1.16   mjacob  *      Issue a start to a SCSI device.
    675  1.16   mjacob  */
    676  1.16   mjacob void
    677  1.16   mjacob device_start(argc, argv)
    678  1.16   mjacob 	int argc;
    679  1.16   mjacob 	char *argv[];
    680  1.16   mjacob {
    681  1.16   mjacob 	struct scsipi_start_stop cmd;
    682  1.16   mjacob 
    683  1.16   mjacob 	/* No arguments. */
    684  1.16   mjacob 	if (argc != 0)
    685  1.16   mjacob 		usage();
    686  1.16   mjacob 
    687  1.16   mjacob 	memset(&cmd, 0, sizeof(cmd));
    688  1.16   mjacob 
    689  1.16   mjacob 	cmd.opcode = START_STOP;
    690  1.16   mjacob 	cmd.how = SSS_START;
    691  1.16   mjacob 
    692  1.16   mjacob 	scsi_command(fd, &cmd, sizeof(cmd), NULL, 0, 10000, 0);
    693  1.16   mjacob 
    694  1.16   mjacob 	return;
    695  1.16   mjacob }
    696  1.16   mjacob 
    697  1.16   mjacob /*
    698  1.16   mjacob  * device_stop:
    699  1.16   mjacob  *
    700  1.16   mjacob  *      Issue a stop to a SCSI device.
    701  1.16   mjacob  */
    702  1.16   mjacob void
    703  1.16   mjacob device_stop(argc, argv)
    704  1.16   mjacob 	int argc;
    705  1.16   mjacob 	char *argv[];
    706  1.16   mjacob {
    707  1.16   mjacob 	struct scsipi_start_stop cmd;
    708  1.16   mjacob 
    709  1.16   mjacob 	/* No arguments. */
    710  1.16   mjacob 	if (argc != 0)
    711  1.16   mjacob 		usage();
    712  1.16   mjacob 
    713  1.16   mjacob 	memset(&cmd, 0, sizeof(cmd));
    714  1.16   mjacob 
    715  1.16   mjacob 	cmd.opcode = START_STOP;
    716  1.16   mjacob 	cmd.how = SSS_STOP;
    717  1.16   mjacob 
    718  1.16   mjacob 	scsi_command(fd, &cmd, sizeof(cmd), NULL, 0, 10000, 0);
    719  1.16   mjacob 
    720  1.16   mjacob 	return;
    721  1.16   mjacob }
    722  1.16   mjacob 
    723  1.16   mjacob /*
    724  1.16   mjacob  * device_tur:
    725  1.16   mjacob  *
    726  1.16   mjacob  *      Issue a TEST UNIT READY to a SCSI drevice
    727  1.16   mjacob  */
    728  1.16   mjacob void
    729  1.16   mjacob device_tur(argc, argv)
    730  1.16   mjacob 	int argc;
    731  1.16   mjacob 	char *argv[];
    732  1.16   mjacob {
    733  1.16   mjacob 	struct scsipi_test_unit_ready cmd;
    734  1.16   mjacob 
    735  1.16   mjacob 	/* No arguments. */
    736  1.16   mjacob 	if (argc != 0)
    737  1.16   mjacob 		usage();
    738  1.16   mjacob 
    739  1.16   mjacob 	memset(&cmd, 0, sizeof(cmd));
    740  1.16   mjacob 
    741  1.16   mjacob 	cmd.opcode = TEST_UNIT_READY;
    742  1.16   mjacob 
    743  1.16   mjacob 	scsi_command(fd, &cmd, sizeof(cmd), NULL, 0, 10000, 0);
    744  1.16   mjacob 
    745  1.16   mjacob 	return;
    746  1.16   mjacob }
    747  1.16   mjacob 
    748  1.18  thorpej /*
    749  1.18  thorpej  * BUS COMMANDS
    750  1.18  thorpej  */
    751  1.18  thorpej 
    752  1.18  thorpej /*
    753  1.18  thorpej  * bus_reset:
    754  1.18  thorpej  *
    755  1.18  thorpej  *	Issue a reset to a SCSI bus.
    756  1.18  thorpej  */
    757  1.18  thorpej void
    758  1.18  thorpej bus_reset(argc, argv)
    759  1.18  thorpej 	int argc;
    760  1.18  thorpej 	char *argv[];
    761  1.18  thorpej {
    762  1.18  thorpej 
    763  1.18  thorpej 	/* No arguments. */
    764  1.18  thorpej 	if (argc != 0)
    765  1.18  thorpej 		usage();
    766  1.16   mjacob 
    767  1.18  thorpej 	if (ioctl(fd, SCBUSIORESET, NULL) != 0)
    768  1.18  thorpej 		err(1, "SCBUSIORESET");
    769  1.18  thorpej 
    770  1.18  thorpej 	return;
    771  1.18  thorpej }
    772   1.1  thorpej 
    773   1.1  thorpej /*
    774   1.1  thorpej  * bus_scan:
    775   1.1  thorpej  *
    776   1.1  thorpej  *	Rescan a SCSI bus for new devices.
    777   1.1  thorpej  */
    778   1.1  thorpej void
    779   1.1  thorpej bus_scan(argc, argv)
    780   1.1  thorpej 	int argc;
    781   1.1  thorpej 	char *argv[];
    782   1.1  thorpej {
    783   1.1  thorpej 	struct scbusioscan_args args;
    784   1.1  thorpej 	char *cp;
    785   1.1  thorpej 
    786   1.1  thorpej 	/* Must have two args: target lun */
    787   1.1  thorpej 	if (argc != 2)
    788   1.6  hubertf 		usage();
    789   1.1  thorpej 
    790   1.8       ad 	if (strcmp(argv[0], "any") == 0 || strcmp(argv[0], "all") == 0)
    791   1.1  thorpej 		args.sa_target = -1;
    792   1.1  thorpej 	else {
    793   1.1  thorpej 		args.sa_target = strtol(argv[0], &cp, 10);
    794   1.1  thorpej 		if (*cp != '\0' || args.sa_target < 0)
    795  1.12       ad 			errx(1, "invalid target: %s", argv[0]);
    796   1.1  thorpej 	}
    797   1.1  thorpej 
    798   1.8       ad 	if (strcmp(argv[1], "any") == 0 || strcmp(argv[1], "all") == 0)
    799   1.1  thorpej 		args.sa_lun = -1;
    800   1.1  thorpej 	else {
    801   1.1  thorpej 		args.sa_lun = strtol(argv[1], &cp, 10);
    802   1.1  thorpej 		if (*cp != '\0' || args.sa_lun < 0)
    803  1.12       ad 			errx(1, "invalid lun: %s", argv[1]);
    804   1.1  thorpej 	}
    805   1.1  thorpej 
    806   1.1  thorpej 	if (ioctl(fd, SCBUSIOSCAN, &args) != 0)
    807   1.1  thorpej 		err(1, "SCBUSIOSCAN");
    808  1.14   bouyer 
    809  1.14   bouyer 	return;
    810  1.14   bouyer }
    811  1.14   bouyer 
    812  1.14   bouyer /*
    813  1.14   bouyer  * bus_detach:
    814  1.14   bouyer  *
    815  1.14   bouyer  *	detach SCSI devices from a bus.
    816  1.14   bouyer  */
    817  1.14   bouyer void
    818  1.14   bouyer bus_detach(argc, argv)
    819  1.14   bouyer 	int argc;
    820  1.14   bouyer 	char *argv[];
    821  1.14   bouyer {
    822  1.14   bouyer 	struct scbusiodetach_args args;
    823  1.14   bouyer 	char *cp;
    824  1.14   bouyer 
    825  1.14   bouyer 	/* Must have two args: target lun */
    826  1.14   bouyer 	if (argc != 2)
    827  1.14   bouyer 		usage();
    828  1.14   bouyer 
    829  1.14   bouyer 	if (strcmp(argv[0], "any") == 0 || strcmp(argv[0], "all") == 0)
    830  1.14   bouyer 		args.sa_target = -1;
    831  1.14   bouyer 	else {
    832  1.14   bouyer 		args.sa_target = strtol(argv[0], &cp, 10);
    833  1.14   bouyer 		if (*cp != '\0' || args.sa_target < 0)
    834  1.17    grant 			errx(1, "invalid target: %s", argv[0]);
    835  1.14   bouyer 	}
    836  1.14   bouyer 
    837  1.14   bouyer 	if (strcmp(argv[1], "any") == 0 || strcmp(argv[1], "all") == 0)
    838  1.14   bouyer 		args.sa_lun = -1;
    839  1.14   bouyer 	else {
    840  1.14   bouyer 		args.sa_lun = strtol(argv[1], &cp, 10);
    841  1.14   bouyer 		if (*cp != '\0' || args.sa_lun < 0)
    842  1.17    grant 			errx(1, "invalid lun: %s", argv[1]);
    843  1.14   bouyer 	}
    844  1.14   bouyer 
    845  1.14   bouyer 	if (ioctl(fd, SCBUSIODETACH, &args) != 0)
    846  1.14   bouyer 		err(1, "SCBUSIODETACH");
    847   1.1  thorpej 
    848   1.1  thorpej 	return;
    849   1.1  thorpej }
    850