Home | History | Annotate | Line # | Download | only in atactl
atactl.c revision 1.6
      1 /*	$NetBSD: atactl.c,v 1.6 1999/11/01 02:30:58 soren 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  * atactl(8) - a program to control 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 	const char *arg_names;
     61 	void (*cmd_func) __P((int, char *[]));
     62 };
     63 
     64 struct bitinfo {
     65 	u_int bitmask;
     66 	const char *string;
     67 };
     68 
     69 int	main __P((int, char *[]));
     70 void	usage __P((void));
     71 void	ata_command __P((struct atareq *));
     72 void	print_bitinfo __P((const char *, u_int, struct bitinfo *));
     73 
     74 int	fd;				/* file descriptor for device */
     75 const	char *dvname;			/* device name */
     76 char	dvname_store[MAXPATHLEN];	/* for opendisk(3) */
     77 const	char *cmdname;			/* command user issued */
     78 const	char *argnames;			/* helpstring: expected arguments */
     79 
     80 extern const char *__progname;		/* from crt0.o */
     81 
     82 void	device_identify __P((int, char *[]));
     83 void	device_setidle __P((int, char *[]));
     84 void	device_idle __P((int, char *[]));
     85 void	device_checkpower __P((int, char *[]));
     86 
     87 struct command commands[] = {
     88 	{ "identify",	"",			device_identify },
     89 	{ "setidle",	"idle-timer",		device_setidle },
     90 	{ "setstandby",	"standby-timer",	device_setidle },
     91 	{ "idle",	"",			device_idle },
     92 	{ "standby",	"",			device_idle },
     93 	{ "sleep",	"",			device_idle },
     94 	{ "checkpower",	"",			device_checkpower },
     95 	{ NULL,		NULL,			NULL },
     96 };
     97 
     98 /*
     99  * Tables containing bitmasks used for error reporting and
    100  * device identification.
    101  */
    102 
    103 struct bitinfo ata_caps[] = {
    104 	{ ATA_CAP_STBY, "ATA standby timer values" },
    105 	{ WDC_CAP_IORDY, "IORDY operation" },
    106 	{ WDC_CAP_IORDY_DSBL, "IORDY disabling" },
    107 	{ NULL, NULL },
    108 };
    109 
    110 struct bitinfo ata_vers[] = {
    111 	{ WDC_VER_ATA1,	"ATA-1" },
    112 	{ WDC_VER_ATA2,	"ATA-2" },
    113 	{ WDC_VER_ATA3,	"ATA-3" },
    114 	{ WDC_VER_ATA4,	"ATA-4" },
    115 	{ NULL, NULL },
    116 };
    117 
    118 struct bitinfo ata_cmd_set1[] = {
    119 	{ WDC_CMD1_NOP, "NOP command" },
    120 	{ WDC_CMD1_RB, "READ BUFFER command" },
    121 	{ WDC_CMD1_WB, "WRITE BUFFER command" },
    122 	{ WDC_CMD1_HPA, "Host Protected Area feature set" },
    123 	{ WDC_CMD1_DVRST, "DEVICE RESET command" },
    124 	{ WDC_CMD1_SRV, "SERVICE interrupt" },
    125 	{ WDC_CMD1_RLSE, "release interrupt" },
    126 	{ WDC_CMD1_AHEAD, "look-ahead" },
    127 	{ WDC_CMD1_CACHE, "write cache" },
    128 	{ WDC_CMD1_PKT, "PACKET command feature set" },
    129 	{ WDC_CMD1_PM, "Power Management feature set" },
    130 	{ WDC_CMD1_REMOV, "Removable Media feature set" },
    131 	{ WDC_CMD1_SEC, "Security Mode feature set" },
    132 	{ WDC_CMD1_SMART, "SMART feature set" },
    133 	{ NULL, NULL },
    134 };
    135 
    136 struct bitinfo ata_cmd_set2[] = {
    137 	{ WDC_CMD2_RMSN, "Removable Media Status Notification feature set" },
    138 	{ ATA_CMD2_APM, "Advanced Power Management feature set" },
    139 	{ ATA_CMD2_CFA, "CFA feature set" },
    140 	{ ATA_CMD2_RWQ, "READ/WRITE DMA QUEUED commands" },
    141 	{ WDC_CMD2_DM, "DOWNLOAD MICROCODE command" },
    142 	{ NULL, NULL },
    143 };
    144 
    145 int
    146 main(argc, argv)
    147 	int argc;
    148 	char *argv[];
    149 {
    150 	int i;
    151 
    152 	/* Must have at least: device command */
    153 	if (argc < 3)
    154 		usage();
    155 
    156 	/* Skip program name, get and skip device name and command. */
    157 	dvname = argv[1];
    158 	cmdname = argv[2];
    159 	argv += 3;
    160 	argc -= 3;
    161 
    162 	/*
    163 	 * Open the device
    164 	 */
    165 	fd = opendisk(dvname, O_RDWR, dvname_store, sizeof(dvname_store), 0);
    166 	if (fd == -1) {
    167 		if (errno == ENOENT) {
    168 			/*
    169 			 * Device doesn't exist.  Probably trying to open
    170 			 * a device which doesn't use disk semantics for
    171 			 * device name.  Try again, specifying "cooked",
    172 			 * which leaves off the "r" in front of the device's
    173 			 * name.
    174 			 */
    175 			fd = opendisk(dvname, O_RDWR, dvname_store,
    176 			    sizeof(dvname_store), 1);
    177 			if (fd == -1)
    178 				err(1, "%s", dvname);
    179 		} else
    180 			err(1, "%s", dvname);
    181 	}
    182 
    183 	/*
    184 	 * Point the dvname at the actual device name that opendisk() opened.
    185 	 */
    186 	dvname = dvname_store;
    187 
    188 	/* Look up and call the command. */
    189 	for (i = 0; commands[i].cmd_name != NULL; i++)
    190 		if (strcmp(cmdname, commands[i].cmd_name) == 0)
    191 			break;
    192 	if (commands[i].cmd_name == NULL)
    193 		errx(1, "unknown command: %s\n", cmdname);
    194 
    195 	argnames = commands[i].arg_names;
    196 
    197 	(*commands[i].cmd_func)(argc, argv);
    198 	exit(0);
    199 }
    200 
    201 void
    202 usage()
    203 {
    204 	int i;
    205 
    206 	fprintf(stderr, "Usage: %s device command [arg [...]]\n",
    207 	    __progname);
    208 
    209 	fprintf(stderr, "   Available device commands:\n");
    210 	for (i=0; commands[i].cmd_name != NULL; i++)
    211 		fprintf(stderr, "\t%s %s\n", commands[i].cmd_name,
    212 					    commands[i].arg_names);
    213 
    214 	exit(1);
    215 }
    216 
    217 /*
    218  * Wrapper that calls ATAIOCCOMMAND and checks for errors
    219  */
    220 
    221 void
    222 ata_command(req)
    223 	struct atareq *req;
    224 {
    225 	int error;
    226 
    227 	error = ioctl(fd, ATAIOCCOMMAND, req);
    228 
    229 	if (error == -1)
    230 		err(1, "ATAIOCCOMMAND failed");
    231 
    232 	switch (req->retsts) {
    233 
    234 	case ATACMD_OK:
    235 		return;
    236 	case ATACMD_TIMEOUT:
    237 		fprintf(stderr, "ATA command timed out\n");
    238 		exit(1);
    239 	case ATACMD_DF:
    240 		fprintf(stderr, "ATA device returned a Device Fault\n");
    241 		exit(1);
    242 	case ATACMD_ERROR:
    243 		if (req->error & WDCE_ABRT)
    244 			fprintf(stderr, "ATA device returned Aborted "
    245 				"Command\n");
    246 		else
    247 			fprintf(stderr, "ATA device returned error register "
    248 				"%0x\n", req->error);
    249 		exit(1);
    250 	default:
    251 		fprintf(stderr, "ATAIOCCOMMAND returned unknown result code "
    252 			"%d\n", req->retsts);
    253 		exit(1);
    254 	}
    255 }
    256 
    257 /*
    258  * Print out strings associated with particular bitmasks
    259  */
    260 
    261 void
    262 print_bitinfo(f, bits, binfo)
    263 	const char *f;
    264 	u_int bits;
    265 	struct bitinfo *binfo;
    266 {
    267 
    268 	for (; binfo->bitmask != NULL; binfo++)
    269 		if (bits & binfo->bitmask)
    270 			printf(f, binfo->string);
    271 }
    272 
    273 /*
    274  * DEVICE COMMANDS
    275  */
    276 
    277 /*
    278  * device_identify:
    279  *
    280  *	Display the identity of the device
    281  */
    282 void
    283 device_identify(argc, argv)
    284 	int argc;
    285 	char *argv[];
    286 {
    287 	struct ataparams *inqbuf;
    288 	struct atareq req;
    289 	unsigned char inbuf[DEV_BSIZE];
    290 #if BYTE_ORDER == LITTLE_ENDIAN
    291 	int i;
    292 	u_int16_t *p;
    293 #endif
    294 
    295 	/* No arguments. */
    296 	if (argc != 0)
    297 		usage();
    298 
    299 	memset(&inbuf, 0, sizeof(inbuf));
    300 	memset(&req, 0, sizeof(req));
    301 
    302 	inqbuf = (struct ataparams *) inbuf;
    303 
    304 	req.flags = ATACMD_READ;
    305 	req.command = WDCC_IDENTIFY;
    306 	req.databuf = (caddr_t) inbuf;
    307 	req.datalen = sizeof(inbuf);
    308 	req.timeout = 1000;
    309 
    310 	ata_command(&req);
    311 
    312 #if BYTE_ORDER == LITTLE_ENDIAN
    313 	/*
    314 	 * On little endian machines, we need to shuffle the string
    315 	 * byte order.  However, we don't have to do this for NEC or
    316 	 * Mitsumi ATAPI devices
    317 	 */
    318 
    319 	if (!((inqbuf->atap_config & WDC_CFG_ATAPI_MASK) == WDC_CFG_ATAPI &&
    320 	      ((inqbuf->atap_model[0] == 'N' &&
    321 		  inqbuf->atap_model[1] == 'E') ||
    322 	       (inqbuf->atap_model[0] == 'F' &&
    323 		  inqbuf->atap_model[1] == 'X')))) {
    324 		for (i = 0 ; i < sizeof(inqbuf->atap_model); i += 2) {
    325 			p = (u_short *) (inqbuf->atap_model + i);
    326 			*p = ntohs(*p);
    327 		}
    328 		for (i = 0 ; i < sizeof(inqbuf->atap_serial); i += 2) {
    329 			p = (u_short *) (inqbuf->atap_serial + i);
    330 			*p = ntohs(*p);
    331 		}
    332 		for (i = 0 ; i < sizeof(inqbuf->atap_revision); i += 2) {
    333 			p = (u_short *) (inqbuf->atap_revision + i);
    334 			*p = ntohs(*p);
    335 		}
    336 	}
    337 #endif
    338 
    339 	/*
    340 	 * Strip blanks off of the info strings.  Yuck, I wish this was
    341 	 * cleaner.
    342 	 */
    343 
    344 	if (inqbuf->atap_model[sizeof(inqbuf->atap_model) - 1] == ' ') {
    345 		inqbuf->atap_model[sizeof(inqbuf->atap_model) - 1] = '\0';
    346 		while (inqbuf->atap_model[strlen(inqbuf->atap_model) - 1] == ' ')
    347 			inqbuf->atap_model[strlen(inqbuf->atap_model) - 1] = '\0';
    348 	}
    349 
    350 	if (inqbuf->atap_revision[sizeof(inqbuf->atap_revision) - 1] == ' ') {
    351 		inqbuf->atap_revision[sizeof(inqbuf->atap_revision) - 1] = '\0';
    352 		while (inqbuf->atap_revision[strlen(inqbuf->atap_revision) - 1] == ' ')
    353 			inqbuf->atap_revision[strlen(inqbuf->atap_revision) - 1] = '\0';
    354 	}
    355 
    356 	if (inqbuf->atap_serial[sizeof(inqbuf->atap_serial) - 1] == ' ') {
    357 		inqbuf->atap_serial[sizeof(inqbuf->atap_serial) - 1] = '\0';
    358 		while (inqbuf->atap_serial[strlen(inqbuf->atap_serial) - 1] == ' ')
    359 			inqbuf->atap_serial[strlen(inqbuf->atap_serial) - 1] = '\0';
    360 	}
    361 
    362 	printf("Model: %.*s, Rev: %.*s, Serial #: %.*s\n",
    363 	       (int) sizeof(inqbuf->atap_model), inqbuf->atap_model,
    364 	       (int) sizeof(inqbuf->atap_revision), inqbuf->atap_revision,
    365 	       (int) sizeof(inqbuf->atap_serial), inqbuf->atap_serial);
    366 
    367 	printf("Device type: %s, %s\n", inqbuf->atap_config & WDC_CFG_ATAPI ?
    368 	       "ATAPI" : "ATA", inqbuf->atap_config & ATA_CFG_FIXED ? "fixed" :
    369 	       "removable");
    370 
    371 	if ((inqbuf->atap_config & WDC_CFG_ATAPI_MASK) == 0)
    372 		printf("Cylinders: %d, heads: %d, sec/track: %d, total "
    373 		       "sectors: %d\n", inqbuf->atap_cylinders,
    374 		       inqbuf->atap_heads, inqbuf->atap_sectors,
    375 		       (inqbuf->atap_capacity[1] << 16) |
    376 		       inqbuf->atap_capacity[0]);
    377 
    378 	if (inqbuf->atap_queuedepth & WDC_QUEUE_DEPTH_MASK)
    379 		printf("Device supports command queue depth of %d\n",
    380 		       inqbuf->atap_queuedepth & 0xf);
    381 
    382 	printf("Device capabilities:\n");
    383 	print_bitinfo("\t%s\n", inqbuf->atap_capabilities1, ata_caps);
    384 
    385 	if (inqbuf->atap_ata_major != 0 && inqbuf->atap_ata_major != 0xffff) {
    386 		printf("Device supports following standards:\n");
    387 		print_bitinfo("%s ", inqbuf->atap_ata_major, ata_vers);
    388 		printf("\n");
    389 	}
    390 
    391 	if (inqbuf->atap_cmd_set1 != 0 && inqbuf->atap_cmd_set1 != 0xffff &&
    392 	    inqbuf->atap_cmd_set2 != 0 && inqbuf->atap_cmd_set2 != 0xffff) {
    393 		printf("Command set support:\n");
    394 		print_bitinfo("\t%s\n", inqbuf->atap_cmd_set1, ata_cmd_set1);
    395 		print_bitinfo("\t%s\n", inqbuf->atap_cmd_set2, ata_cmd_set2);
    396 	}
    397 
    398 	if (inqbuf->atap_cmd_def != 0 && inqbuf->atap_cmd_def != 0xffff) {
    399 		printf("Command sets/features enabled:\n");
    400 		print_bitinfo("\t%s\n", inqbuf->atap_cmd_set1 &
    401 			      (WDC_CMD1_SRV | WDC_CMD1_RLSE | WDC_CMD1_AHEAD |
    402 			       WDC_CMD1_CACHE | WDC_CMD1_SEC | WDC_CMD1_SMART),
    403 			       ata_cmd_set1);
    404 		print_bitinfo("\t%s\n", inqbuf->atap_cmd_set2 &
    405 			      (WDC_CMD2_RMSN | ATA_CMD2_APM), ata_cmd_set2);
    406 	}
    407 
    408 	return;
    409 }
    410 
    411 /*
    412  * device idle:
    413  *
    414  * issue the IDLE IMMEDIATE command to the drive
    415  */
    416 
    417 void
    418 device_idle(argc, argv)
    419 	int argc;
    420 	char *argv[];
    421 {
    422 	struct atareq req;
    423 
    424 	/* No arguments. */
    425 	if (argc != 0)
    426 		usage();
    427 
    428 	memset(&req, 0, sizeof(req));
    429 
    430 	if (strcmp(cmdname, "idle") == 0)
    431 		req.command = WDCC_IDLE_IMMED;
    432 	else if (strcmp(cmdname, "standby") == 0)
    433 		req.command = WDCC_STANDBY_IMMED;
    434 	else
    435 		req.command = WDCC_SLEEP;
    436 
    437 	req.timeout = 1000;
    438 
    439 	ata_command(&req);
    440 
    441 	return;
    442 }
    443 
    444 /*
    445  * Set the idle timer on the disk.  Set it for either idle mode or
    446  * standby mode, depending on how we were invoked.
    447  */
    448 
    449 void
    450 device_setidle(argc, argv)
    451 	int argc;
    452 	char *argv[];
    453 {
    454 	unsigned long idle;
    455 	struct atareq req;
    456 	char *end;
    457 
    458 	/* Only one argument */
    459 	if (argc != 1)
    460 		usage();
    461 
    462 	idle = strtoul(argv[0], &end, 0);
    463 
    464 	if (*end != '\0') {
    465 		fprintf(stderr, "Invalid idle time: \"%s\"\n", argv[0]);
    466 		exit(1);
    467 	}
    468 
    469 	if (idle > 19800) {
    470 		fprintf(stderr, "Idle time has a maximum value of 5.5 "
    471 			"hours\n");
    472 		exit(1);
    473 	}
    474 
    475 	if (idle != 0 && idle < 5) {
    476 		fprintf(stderr, "Idle timer must be at least 5 seconds\n");
    477 		exit(1);
    478 	}
    479 
    480 	memset(&req, 0, sizeof(req));
    481 
    482 	if (idle <= 240*5)
    483 		req.sec_count = idle / 5;
    484 	else
    485 		req.sec_count = idle / (30*60) + 240;
    486 
    487 	req.command = cmdname[3] == 's' ? WDCC_STANDBY : WDCC_IDLE;
    488 	req.timeout = 1000;
    489 
    490 	ata_command(&req);
    491 
    492 	return;
    493 }
    494 
    495 /*
    496  * Query the device for the current power mode
    497  */
    498 
    499 void
    500 device_checkpower(argc, argv)
    501 	int argc;
    502 	char *argv[];
    503 {
    504 	struct atareq req;
    505 
    506 	/* No arguments. */
    507 	if (argc != 0)
    508 		usage();
    509 
    510 	memset(&req, 0, sizeof(req));
    511 
    512 	req.command = WDCC_CHECK_PWR;
    513 	req.timeout = 1000;
    514 	req.flags = ATACMD_READREG;
    515 
    516 	ata_command(&req);
    517 
    518 	printf("Current power status: ");
    519 
    520 	switch (req.sec_count) {
    521 	case 0x00:
    522 		printf("Standby mode\n");
    523 		break;
    524 	case 0x80:
    525 		printf("Idle mode\n");
    526 		break;
    527 	case 0xff:
    528 		printf("Active mode\n");
    529 		break;
    530 	default:
    531 		printf("Unknown power code (%02x)\n", req.sec_count);
    532 	}
    533 
    534 	return;
    535 }
    536