Home | History | Annotate | Line # | Download | only in atactl
atactl.c revision 1.1
      1 /*	$NetBSD: atactl.c,v 1.1 1998/11/19 23:55:00 kenh 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 Ken Hornstein.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * wdctl(8) - a program to control wd (aka ATA) devices.
     41  */
     42 
     43 #include <sys/param.h>
     44 #include <sys/ioctl.h>
     45 #include <err.h>
     46 #include <errno.h>
     47 #include <fcntl.h>
     48 #include <stdio.h>
     49 #include <stdlib.h>
     50 #include <string.h>
     51 #include <unistd.h>
     52 #include <util.h>
     53 
     54 #include <dev/ata/atareg.h>
     55 #include <dev/ic/wdcreg.h>
     56 #include <sys/ataio.h>
     57 
     58 struct command {
     59 	const char *cmd_name;
     60 	void (*cmd_func) __P((int, char *[]));
     61 };
     62 
     63 struct bitinfo {
     64 	u_int bitmask;
     65 	const char *string;
     66 };
     67 
     68 int	main __P((int, char *[]));
     69 void	usage __P((void));
     70 void	ata_command __P((struct atareq *));
     71 void	print_bitinfo __P((const char *, u_int, struct bitinfo *));
     72 
     73 int	fd;				/* file descriptor for device */
     74 const	char *dvname;			/* device name */
     75 char	dvname_store[MAXPATHLEN];	/* for opendisk(3) */
     76 const	char *cmdname;			/* command user issued */
     77 
     78 extern const char *__progname;		/* from crt0.o */
     79 
     80 void	device_identify __P((int, char *[]));
     81 void	device_setidle __P((int, char *[]));
     82 void	device_idle __P((int, char *[]));
     83 
     84 struct command commands[] = {
     85 	{ "identify",	device_identify },
     86 	{ "setidle",	device_setidle },
     87 	{ "setstandby",	device_setidle },
     88 	{ "idle",	device_idle },
     89 	{ "standby",	device_idle },
     90 	{ "sleep",	device_idle },
     91 	{ NULL,		NULL },
     92 };
     93 
     94 /*
     95  * Tables containing bitmasks used for error reporting and
     96  * device identification.
     97  */
     98 
     99 struct bitinfo ata_caps[] = {
    100 	{ ATA_CAP_STBY, "ATA standby timer values" },
    101 	{ WDC_CAP_IORDY, "IORDY operation" },
    102 	{ WDC_CAP_IORDY_DSBL, "IORDY disabling" },
    103 	{ NULL, NULL },
    104 };
    105 
    106 struct bitinfo ata_vers[] = {
    107 	{ WDC_VER_ATA1,	"ATA-1" },
    108 	{ WDC_VER_ATA2,	"ATA-2" },
    109 	{ WDC_VER_ATA3,	"ATA-3" },
    110 	{ WDC_VER_ATA4,	"ATA-4" },
    111 	{ NULL, NULL },
    112 };
    113 
    114 struct bitinfo ata_cmd_set1[] = {
    115 	{ WDC_CMD1_NOP, "NOP command" },
    116 	{ WDC_CMD1_RB, "READ BUFFER command" },
    117 	{ WDC_CMD1_WB, "WRITE BUFFER command" },
    118 	{ WDC_CMD1_HPA, "Host Protected Area feature set" },
    119 	{ WDC_CMD1_DVRST, "DEVICE RESET command" },
    120 	{ WDC_CMD1_SRV, "SERVICE interrupt" },
    121 	{ WDC_CMD1_RLSE, "release interrupt" },
    122 	{ WDC_CMD1_AHEAD, "look-ahead" },
    123 	{ WDC_CMD1_CACHE, "write cache" },
    124 	{ WDC_CMD1_PKT, "PACKET command feature set" },
    125 	{ WDC_CMD1_PM, "Power Management feature set" },
    126 	{ WDC_CMD1_REMOV, "Removable Media feature set" },
    127 	{ WDC_CMD1_SEC, "Security Mode feature set" },
    128 	{ WDC_CMD1_SMART, "SMART feature set" },
    129 	{ NULL, NULL },
    130 };
    131 
    132 struct bitinfo ata_cmd_set2[] = {
    133 	{ WDC_CMD2_RMSN, "Removable Media Status Notification feature set" },
    134 	{ ATA_CMD2_APM, "Advanced Power Management feature set" },
    135 	{ ATA_CMD2_CFA, "CFA feature set" },
    136 	{ ATA_CMD2_RWQ, "READ/WRITE DMS QUEUED commands" },
    137 	{ WDC_CMD2_DM, "DOWNLOAD MICROCODE command" },
    138 	{ NULL, NULL },
    139 };
    140 
    141 int
    142 main(argc, argv)
    143 	int argc;
    144 	char *argv[];
    145 {
    146 	int i;
    147 
    148 	/* Must have at least: device command */
    149 	if (argc < 3)
    150 		usage();
    151 
    152 	/* Skip program name, get and skip device name and command. */
    153 	dvname = argv[1];
    154 	cmdname = argv[2];
    155 	argv += 3;
    156 	argc -= 3;
    157 
    158 	/*
    159 	 * Open the device
    160 	 */
    161 	fd = opendisk(dvname, O_RDWR, dvname_store, sizeof(dvname_store), 0);
    162 	if (fd == -1) {
    163 		if (errno == ENOENT) {
    164 			/*
    165 			 * Device doesn't exist.  Probably trying to open
    166 			 * a device which doesn't use disk semantics for
    167 			 * device name.  Try again, specifying "cooked",
    168 			 * which leaves off the "r" in front of the device's
    169 			 * name.
    170 			 */
    171 			fd = opendisk(dvname, O_RDWR, dvname_store,
    172 			    sizeof(dvname_store), 1);
    173 			if (fd == -1)
    174 				err(1, "%s", dvname);
    175 		}
    176 		err(1, "%s", dvname);
    177 	}
    178 
    179 	/*
    180 	 * Point the dvname at the actual device name that opendisk() opened.
    181 	 */
    182 	dvname = dvname_store;
    183 
    184 	/* Look up and call the command. */
    185 	for (i = 0; commands[i].cmd_name != NULL; i++)
    186 		if (strcmp(cmdname, commands[i].cmd_name) == 0)
    187 			break;
    188 	if (commands[i].cmd_name == NULL)
    189 		errx(1, "unknown command: %s\n", cmdname);
    190 
    191 	(*commands[i].cmd_func)(argc, argv);
    192 	exit(0);
    193 }
    194 
    195 void
    196 usage()
    197 {
    198 
    199 	fprintf(stderr, "usage: %s device command [arg [...]]\n",
    200 	    __progname);
    201 	exit(1);
    202 }
    203 
    204 /*
    205  * Wrapper that calls ATAIOCCOMMAND and checks for errors
    206  */
    207 
    208 void
    209 ata_command(req)
    210 	struct atareq *req;
    211 {
    212 	int error;
    213 
    214 	error = ioctl(fd, ATAIOCCOMMAND, req);
    215 
    216 	if (error == -1)
    217 		err(1, "ATAIOCCOMMAND failed");
    218 
    219 	switch (req->retsts) {
    220 
    221 	case ATACMD_OK:
    222 		return;
    223 	case ATACMD_TIMEOUT:
    224 		fprintf(stderr, "ATA command timed out\n");
    225 		exit(1);
    226 	case ATACMD_DF:
    227 		fprintf(stderr, "ATA device returned a Device Fault\n");
    228 		exit(1);
    229 	case ATACMD_ERROR:
    230 		if (req->error & WDCE_ABRT)
    231 			fprintf(stderr, "ATA device returned Aborted "
    232 				"Command\n");
    233 		else
    234 			fprintf(stderr, "ATA device returned error register "
    235 				"%0x\n", req->error);
    236 		exit(1);
    237 	default:
    238 		fprintf(stderr, "ATAIOCCOMMAND returned unknown result code "
    239 			"%d\n", req->retsts);
    240 		exit(1);
    241 	}
    242 }
    243 
    244 /*
    245  * Print out strings associated with particular bitmasks
    246  */
    247 
    248 void
    249 print_bitinfo(f, bits, binfo)
    250 	const char *f;
    251 	u_int bits;
    252 	struct bitinfo *binfo;
    253 {
    254 
    255 	for (; binfo->bitmask != NULL; binfo++)
    256 		if (bits & binfo->bitmask)
    257 			printf(f, binfo->string);
    258 }
    259 
    260 /*
    261  * DEVICE COMMANDS
    262  */
    263 
    264 /*
    265  * device_identify:
    266  *
    267  *	Display the identity of the device
    268  */
    269 void
    270 device_identify(argc, argv)
    271 	int argc;
    272 	char *argv[];
    273 {
    274 	struct ataparams *inqbuf;
    275 	struct atareq req;
    276 	unsigned char inbuf[DEV_BSIZE];
    277 	int i;
    278 #if BYTE_ORDER == LITTLE_ENDIAN
    279 	u_int16_t *p;
    280 #endif
    281 
    282 	/* No arguments. */
    283 	if (argc != 0)
    284 		goto usage;
    285 
    286 	memset(&inbuf, 0, sizeof(inbuf));
    287 	memset(&req, 0, sizeof(req));
    288 
    289 	inqbuf = (struct ataparams *) inbuf;
    290 
    291 	req.flags = ATACMD_READ;
    292 	req.command = WDCC_IDENTIFY;
    293 	req.databuf = (caddr_t) inbuf;
    294 	req.datalen = sizeof(inbuf);
    295 	req.timeout = 1000;
    296 
    297 	ata_command(&req);
    298 
    299 #if BYTE_ORDER == LITTLE_ENDIAN
    300 	/*
    301 	 * On little endian machines, we need to shuffle the string
    302 	 * byte order.  However, we don't have to do this for NEC or
    303 	 * Mitsumi ATAPI devices
    304 	 */
    305 
    306 	if (!((inqbuf->atap_config & WDC_CFG_ATAPI_MASK) == WDC_CFG_ATAPI &&
    307 	      ((inqbuf->atap_model[0] == 'N' &&
    308 		  inqbuf->atap_model[1] == 'E') ||
    309 	       (inqbuf->atap_model[0] == 'F' &&
    310 		  inqbuf->atap_model[1] == 'X')))) {
    311 		for (i = 0 ; i < sizeof(inqbuf->atap_model); i += 2) {
    312 			p = (u_short *) (inqbuf->atap_model + i);
    313 			*p = ntohs(*p);
    314 		}
    315 		for (i = 0 ; i < sizeof(inqbuf->atap_serial); i += 2) {
    316 			p = (u_short *) (inqbuf->atap_serial + i);
    317 			*p = ntohs(*p);
    318 		}
    319 		for (i = 0 ; i < sizeof(inqbuf->atap_revision); i += 2) {
    320 			p = (u_short *) (inqbuf->atap_revision + i);
    321 			*p = ntohs(*p);
    322 		}
    323 	}
    324 #endif
    325 
    326 	/*
    327 	 * Strip blanks off of the info strings.  Yuck, I wish this was
    328 	 * cleaner.
    329 	 */
    330 
    331 	if (inqbuf->atap_model[sizeof(inqbuf->atap_model) - 1] == ' ') {
    332 		inqbuf->atap_model[sizeof(inqbuf->atap_model) - 1] = '\0';
    333 		while (inqbuf->atap_model[strlen(inqbuf->atap_model) - 1] == ' ')
    334 			inqbuf->atap_model[strlen(inqbuf->atap_model) - 1] = '\0';
    335 	}
    336 
    337 	if (inqbuf->atap_revision[sizeof(inqbuf->atap_revision) - 1] == ' ') {
    338 		inqbuf->atap_revision[sizeof(inqbuf->atap_revision) - 1] = '\0';
    339 		while (inqbuf->atap_revision[strlen(inqbuf->atap_revision) - 1] == ' ')
    340 			inqbuf->atap_revision[strlen(inqbuf->atap_revision) - 1] = '\0';
    341 	}
    342 
    343 	if (inqbuf->atap_serial[sizeof(inqbuf->atap_serial) - 1] == ' ') {
    344 		inqbuf->atap_serial[sizeof(inqbuf->atap_serial) - 1] = '\0';
    345 		while (inqbuf->atap_serial[strlen(inqbuf->atap_serial) - 1] == ' ')
    346 			inqbuf->atap_serial[strlen(inqbuf->atap_serial) - 1] = '\0';
    347 	}
    348 
    349 	printf("Model: %.*s, Rev: %.*s, Serial #: %.*s\n",
    350 	       (int) sizeof(inqbuf->atap_model), inqbuf->atap_model,
    351 	       (int) sizeof(inqbuf->atap_revision), inqbuf->atap_revision,
    352 	       (int) sizeof(inqbuf->atap_serial), inqbuf->atap_serial);
    353 
    354 	printf("Device type: %s, %s\n", inqbuf->atap_config & WDC_CFG_ATAPI ?
    355 	       "ATAPI" : "ATA", inqbuf->atap_config & ATA_CFG_FIXED ? "fixed" :
    356 	       "removable");
    357 
    358 	if ((inqbuf->atap_config & WDC_CFG_ATAPI_MASK) == 0)
    359 		printf("Cylinders: %d, heads: %d, sec/track: %d, total "
    360 		       "sectors: %d\n", inqbuf->atap_cylinders,
    361 		       inqbuf->atap_heads, inqbuf->atap_sectors,
    362 		       (inqbuf->atap_capacity[1] << 16) |
    363 		       inqbuf->atap_capacity[0]);
    364 
    365 	if (inqbuf->atap_queuedepth & WDC_QUEUE_DEPTH_MASK)
    366 		printf("Device supports command queue depth of %d\n",
    367 		       inqbuf->atap_queuedepth & 0xf);
    368 
    369 	printf("Device capabilities:\n");
    370 	print_bitinfo("\t%s\n", inqbuf->atap_capabilities1, ata_caps);
    371 
    372 	if (inqbuf->atap_ata_major != 0 && inqbuf->atap_ata_major != 0xffff) {
    373 		printf("Device supports following standards:\n");
    374 		print_bitinfo("%s ", inqbuf->atap_ata_major, ata_vers);
    375 		printf("\n");
    376 	}
    377 
    378 	if (inqbuf->atap_cmd_set1 != 0 && inqbuf->atap_cmd_set1 != 0xffff &&
    379 	    inqbuf->atap_cmd_set2 != 0 && inqbuf->atap_cmd_set2 != 0xffff) {
    380 		printf("Command set support:\n");
    381 		print_bitinfo("\t%s\n", inqbuf->atap_cmd_set1, ata_cmd_set1);
    382 		print_bitinfo("\t%s\n", inqbuf->atap_cmd_set2, ata_cmd_set2);
    383 	}
    384 
    385 	if (inqbuf->atap_cmd_def != 0 && inqbuf->atap_cmd_def != 0xffff) {
    386 		printf("Command sets/features enabled:\n");
    387 		print_bitinfo("\t%s\n", inqbuf->atap_cmd_set1 &
    388 			      (WDC_CMD1_SRV | WDC_CMD1_RLSE | WDC_CMD1_AHEAD |
    389 			       WDC_CMD1_CACHE | WDC_CMD1_SEC | WDC_CMD1_SMART),
    390 			       ata_cmd_set1);
    391 		print_bitinfo("\t%s\n", inqbuf->atap_cmd_set2 &
    392 			      (WDC_CMD2_RMSN | ATA_CMD2_APM), ata_cmd_set2);
    393 	}
    394 
    395 	return;
    396 
    397 usage:
    398 	fprintf(stderr, "usage: %s device %s\n", __progname, cmdname);
    399 	exit(1);
    400 }
    401 
    402 /*
    403  * device idle:
    404  *
    405  * issue the IDLE IMMEDIATE command to the drive
    406  */
    407 
    408 void
    409 device_idle(argc, argv)
    410 	int argc;
    411 	char *argv[];
    412 {
    413 	struct atareq req;
    414 
    415 	/* No arguments. */
    416 	if (argc != 0)
    417 		goto usage;
    418 
    419 	memset(&req, 0, sizeof(req));
    420 
    421 	if (strcmp(cmdname, "idle") == 0)
    422 		req.command = WDCC_IDLE_IMMED;
    423 	else if (strcmp(cmdname, "standby") == 0)
    424 		req.command = WDCC_STANDBY_IMMED;
    425 	else
    426 		req.command = WDCC_SLEEP;
    427 
    428 	req.timeout = 1000;
    429 
    430 	ata_command(&req);
    431 
    432 	return;
    433 usage:
    434 	fprintf(stderr, "usage: %s device %s\n", __progname, cmdname);
    435 	exit(1);
    436 }
    437 
    438 /*
    439  * Set the idle timer on the disk.  Set it for either idle mode or
    440  * standby mode, depending on how we were invoked.
    441  */
    442 
    443 void
    444 device_setidle(argc, argv)
    445 	int argc;
    446 	char *argv[];
    447 {
    448 	unsigned long idle;
    449 	struct atareq req;
    450 	char *end;
    451 
    452 	/* Only one argument */
    453 	if (argc != 1)
    454 		goto usage;
    455 
    456 	idle = strtoul(argv[0], &end, 0);
    457 
    458 	if (*end != '\0') {
    459 		fprintf(stderr, "Invalid idle time: \"%s\"\n", argv[0]);
    460 		exit(1);
    461 	}
    462 
    463 	if (idle > 19800) {
    464 		fprintf(stderr, "Idle time has a maximum value of 5.5 "
    465 			"hours\n");
    466 		exit(1);
    467 	}
    468 
    469 	if (idle != 0 && idle < 5) {
    470 		fprintf(stderr, "Idle timer must be at least 5 seconds\n");
    471 		exit(1);
    472 	}
    473 
    474 	memset(&req, 0, sizeof(req));
    475 
    476 	if (idle <= 240*5)
    477 		req.sec_count = idle / 5;
    478 	else
    479 		req.sec_count = idle / (30*60) + 240;
    480 
    481 	req.command = cmdname[3] == 's' ? WDCC_STANDBY : WDCC_IDLE;
    482 	req.timeout = 1000;
    483 
    484 	ata_command(&req);
    485 
    486 	return;
    487 
    488 usage:
    489 	fprintf(stderr, "usage; %s device %s idle-time\n", __progname,
    490 		cmdname);
    491 	exit(1);
    492 }
    493